aboutsummaryrefslogtreecommitdiff
path: root/src/types.cpp
diff options
context:
space:
mode:
authorGinger Bill <bill@gingerbill.org>2017-07-10 22:32:21 +0100
committerGinger Bill <bill@gingerbill.org>2017-07-10 22:32:21 +0100
commitfd8c4d58bb476f858b5238287b6e9911dd5c333c (patch)
tree31e138edb4f51148838bd01e10402e51160547e8 /src/types.cpp
parentce4b7b8b7d54e889413cf2d43d85f2e4f4a0b007 (diff)
`union` type allow for any types and removes common fields
Diffstat (limited to 'src/types.cpp')
-rw-r--r--src/types.cpp46
1 files changed, 8 insertions, 38 deletions
diff --git a/src/types.cpp b/src/types.cpp
index d8e87ff71..a43921d1f 100644
--- a/src/types.cpp
+++ b/src/types.cpp
@@ -93,7 +93,7 @@ struct TypeRecord {
Scope * scope;
// Entity_TypeName - union
- Entity **variants;
+ Type ** variants;
i32 variant_count;
Entity * union__tag;
i64 variant_block_size; // NOTE(bill): Internal use only
@@ -1002,7 +1002,7 @@ bool is_type_polymorphic(Type *t) {
}
}
for (isize i = 1; i < t->Record.variant_count; i++) {
- if (is_type_polymorphic(t->Record.variants[i]->type)) {
+ if (is_type_polymorphic(t->Record.variants[i])) {
return true;
}
}
@@ -1163,10 +1163,7 @@ bool are_types_identical(Type *x, Type *y) {
}
// NOTE(bill): zeroth variant is nullptr
for (isize i = 1; i < x->Record.variant_count; i++) {
- if (!are_types_identical(x->Record.variants[i]->type, y->Record.variants[i]->type)) {
- return false;
- }
- if (x->Record.variants[i]->token.string != y->Record.variants[i]->token.string) {
+ if (!are_types_identical(x->Record.variants[i], y->Record.variants[i])) {
return false;
}
}
@@ -1555,19 +1552,7 @@ Selection lookup_field_with_selection(gbAllocator a, Type *type_, String field_n
}
}
- if (is_type_union(type)) {
- for (isize i = 1; i < type->Record.variant_count; i++) {
- Entity *f = type->Record.variants[i];
- GB_ASSERT(f->kind == Entity_TypeName);
- String str = f->token.string;
-
- if (str == field_name) {
- sel.entity = f;
- // selection_add_index(&sel, i);
- return sel;
- }
- }
- } else if (is_type_enum(type)) {
+ if (is_type_enum(type)) {
// NOTE(bill): These may not have been added yet, so check in case
if (type->Record.enum_count != nullptr) {
if (field_name == "count") {
@@ -1880,7 +1865,7 @@ i64 type_align_of_internal(gbAllocator allocator, Type *t, TypePath *path) {
}
// NOTE(bill): field zero is null
for (isize i = 1; i < t->Record.variant_count; i++) {
- Type *variant = t->Record.variants[i]->type;
+ Type *variant = t->Record.variants[i];
type_path_push(path, variant);
if (path->failure) {
return FAILURE_ALIGNMENT;
@@ -2139,7 +2124,7 @@ i64 type_size_of_internal(gbAllocator allocator, Type *t, TypePath *path) {
i64 field_size = max;
for (isize i = 1; i < variant_count; i++) {
- Type *variant_type = t->Record.variants[i]->type;
+ Type *variant_type = t->Record.variants[i];
i64 size = type_size_of_internal(allocator, variant_type, path);
if (max < size) {
max = size;
@@ -2369,26 +2354,11 @@ gbString write_type_to_string(gbString str, Type *type) {
str = write_type_to_string(str, f->type);
}
for (isize i = 1; i < type->Record.variant_count; i++) {
- Entity *f = type->Record.variants[i];
- GB_ASSERT(f->kind == Entity_TypeName);
+ Type *t = type->Record.variants[i];
if (i > 1 || type->Record.field_count > 1) {
str = gb_string_appendc(str, ", ");
}
- str = gb_string_append_length(str, f->token.string.text, f->token.string.len);
- str = gb_string_appendc(str, "{");
- Type *variant = base_type(f->type);
- for (isize i = 0; i < variant->Record.field_count; i++) {
- Entity *f = variant->Record.fields[i];
- GB_ASSERT(f->kind == Entity_Variable);
- if (i > 0) {
- str = gb_string_appendc(str, ", ");
- }
- str = gb_string_append_length(str, f->token.string.text, f->token.string.len);
- str = gb_string_appendc(str, ": ");
- str = write_type_to_string(str, f->type);
- }
- str = gb_string_appendc(str, "{");
-
+ str = write_type_to_string(str, t);
}
str = gb_string_appendc(str, "}");
break;