diff options
| author | gingerBill <ginger.bill.22@gmail.com> | 2016-07-25 11:14:25 +0100 |
|---|---|---|
| committer | gingerBill <ginger.bill.22@gmail.com> | 2016-07-25 11:14:25 +0100 |
| commit | 32ab8fcf99df786c264ca566799b022c66cca34b (patch) | |
| tree | 79b3f9642c1e49c3995b417fd8b599a20974f564 /src/checker/type.cpp | |
| parent | 9d8355d3612e65a5764640fb972bc4ef9f013088 (diff) | |
`alias` and unified parameters lists for procedures and structures.
Diffstat (limited to 'src/checker/type.cpp')
| -rw-r--r-- | src/checker/type.cpp | 52 |
1 files changed, 42 insertions, 10 deletions
diff --git a/src/checker/type.cpp b/src/checker/type.cpp index e767a6d07..3a66b62ac 100644 --- a/src/checker/type.cpp +++ b/src/checker/type.cpp @@ -60,6 +60,7 @@ enum TypeKind { Type_Structure, Type_Pointer, Type_Named, + Type_Alias, Type_Tuple, Type_Procedure, @@ -81,7 +82,7 @@ struct Type { Entity **fields; // Entity_Variable isize field_count; // == offset_count i64 * offsets; - b32 offsets_set; + b32 are_offsets_set; } structure; struct { Type *element; } pointer; struct { @@ -90,6 +91,11 @@ struct Type { Entity *type_name; // Entity_TypeName } named; struct { + String name; + Type * base; + Entity *alias_name; // Entity_AliasName + } alias; + struct { Entity **variables; // Entity_Variable isize variable_count; } tuple; @@ -97,15 +103,18 @@ struct Type { Scope *scope; Type * params; // Type_Tuple Type * results; // Type_Tuple - isize params_count; - isize results_count; + isize param_count; + isize result_count; } procedure; }; }; Type *get_base_type(Type *t) { - while (t->kind == Type_Named) { - t = t->named.base; + while (t->kind == Type_Named || t->kind == Type_Alias) { + if (t->kind == Type_Named) + t = t->named.base; + else + t = t->alias.base; } return t; } @@ -113,6 +122,8 @@ Type *get_base_type(Type *t) { void set_base_type(Type *t, Type *base) { if (t && t->kind == Type_Named) { t->named.base = base; + } else if (t && t->kind == Type_Alias) { + t->alias.base = base; } } @@ -162,18 +173,26 @@ Type *make_type_named(gbAllocator a, String name, Type *base, Entity *type_name) return t; } +Type *make_type_alias(gbAllocator a, String name, Type *base, Entity *alias_name) { + Type *t = alloc_type(a, Type_Alias); + t->alias.name = name; + t->alias.base = base; + t->alias.alias_name = alias_name; + return t; +} + Type *make_type_tuple(gbAllocator a) { Type *t = alloc_type(a, Type_Tuple); return t; } -Type *make_type_procedure(gbAllocator a, Scope *scope, Type *params, isize params_count, Type *results, isize results_count) { +Type *make_type_procedure(gbAllocator a, Scope *scope, Type *params, isize param_count, Type *results, isize result_count) { Type *t = alloc_type(a, Type_Procedure); t->procedure.scope = scope; t->procedure.params = params; - t->procedure.params_count = params_count; + t->procedure.param_count = param_count; t->procedure.results = results; - t->procedure.results_count = results_count; + t->procedure.result_count = result_count; return t; } @@ -343,6 +362,10 @@ b32 are_types_identical(Type *x, Type *y) { return are_types_identical(x->pointer.element, y->pointer.element); break; + + case Type_Alias: + return are_types_identical(get_base_type(x), y); + case Type_Named: if (y->kind == Type_Named) return x->named.base == y->named.base; @@ -463,9 +486,9 @@ i64 *type_set_offsets_of(BaseTypeSizes s, gbAllocator allocator, Entity **fields b32 type_set_offsets(BaseTypeSizes s, gbAllocator allocator, Type *t) { GB_ASSERT(t->kind == Type_Structure); - if (!t->structure.offsets_set) { + if (!t->structure.are_offsets_set) { t->structure.offsets = type_set_offsets_of(s, allocator, t->structure.fields, t->structure.field_count); - t->structure.offsets_set = true; + t->structure.are_offsets_set = true; return true; } return false; @@ -571,6 +594,15 @@ gbString write_type_to_string(gbString str, Type *type) { } break; + case Type_Alias: + if (type->alias.alias_name != NULL) { + str = gb_string_append_length(str, type->alias.name.text, type->alias.name.len); + } else { + // NOTE(bill): Just in case + str = gb_string_appendc(str, "<alias type>"); + } + break; + case Type_Tuple: if (type->tuple.variable_count > 0) { for (isize i = 0; i < type->tuple.variable_count; i++) { |