commit 36dab6cacf3823767ca35433cb177e25927138c5
parent 452068984f2d6e599a3007569c02d3d304354f51
Author: Drew DeVault <sir@cmpwn.com>
Date: Tue, 5 Oct 2021 10:08:34 +0200
Updates to reflection following standardization
Diffstat:
3 files changed, 20 insertions(+), 22 deletions(-)
diff --git a/src/gen.c b/src/gen.c
@@ -3504,7 +3504,7 @@ gen_type_info(struct gen_context *ctx,
item->value.name = ref.name;
break;
case STORAGE_SLICE:
- *repr_name = "slice_repr";
+ *repr_name = "slice";
item->value = constw(type_hash(&repr));
item->next = xcalloc(1, sizeof(struct qbe_data_item));
item = item->next;
diff --git a/tests/34-reflect.ha b/tests/34-reflect.ha
@@ -1,6 +1,16 @@
use types;
use types::{builtin};
+fn unwrap(in: type) *types::typeinfo = {
+ const info = types::reflect(in);
+ match (info.repr) {
+ case a: types::alias =>
+ return unwrap(a.secondary);
+ case =>
+ return info;
+ };
+};
+
fn builtins() void = {
const cases: [_](type, size, size, builtin) = [
(type(u8), 1, 1, builtin::U8),
@@ -55,7 +65,7 @@ type watchmen = enum {
};
fn enums() void = {
- let ty = types::unwrap(type(watchmen));
+ let ty = unwrap(type(watchmen));
let em = ty.repr as types::enumerated;
assert(em.storage == builtin::INT);
@@ -69,7 +79,7 @@ fn enums() void = {
for (let i = 0z; i < len(em.values); i += 1) {
const val = em.values[i];
const expect = cases[i];
- assert(val.0 == expect.0 && val.1 == expect.1);
+ assert(val.0 == expect.0 && val.1.u == expect.1);
};
};
@@ -86,7 +96,7 @@ fn slices() void = {
let ty = types::reflect(type([]int));
assert(ty.sz == size(*int) + size(size) + size(size));
assert(ty.al == size(*int));
- let sl = ty.repr as types::slice_repr;
+ let sl = ty.repr as types::slice;
assert(sl == type(int));
};
diff --git a/types/reflect.ha b/types/reflect.ha
@@ -12,19 +12,7 @@ export type typeinfo = struct {
};
// Returns [[typeinfo]] for the provided type.
-export fn reflect(in: type) *typeinfo = in: *typeinfo;
-
-// Returns [[typeinfo]] for the provided type, unwrapping any aliases along the
-// way.
-export fn unwrap(in: type) *typeinfo = {
- let info = reflect(in);
- match (info.repr) {
- case a: alias =>
- return unwrap(a.secondary);
- case =>
- return info;
- };
-};
+export fn reflect(in: type) const *typeinfo = in: *typeinfo;
// Type flags.
export type flags = enum uint {
@@ -35,7 +23,7 @@ export type flags = enum uint {
// Details of the type representation.
export type repr = (alias | array | builtin
- | enumerated | func | pointer | slice_repr
+ | enumerated | func | pointer | slice
| struct_union | tagged | tuple);
// A type alias.
@@ -59,7 +47,7 @@ export type builtin = enum uint {
// An enum type.
export type enumerated = struct {
storage: builtin,
- values: [](str, u64),
+ values: [](str, union { u: u64, i: i64 }),
};
// Indicates the variadism of a [[func]].
@@ -71,6 +59,7 @@ export type variadism = enum uint {
// Indicats if a [[func]] has the @noreturn attribute.
export type func_flags = enum uint {
+ NONE = 0,
NORETURN = 1 << 0,
};
@@ -94,9 +83,8 @@ export type pointer = struct {
flags: pointer_flags,
};
-// Type information for slice members. Distinct from [[slice]], which is the
-// representation of a slice object at runtime.
-export type slice_repr = type;
+// Type information for slice members.
+export type slice = type;
// Indicates if a [[_struct]] was declared as a struct or union type.
export type struct_kind = enum uint {