aboutsummaryrefslogtreecommitdiff
path: root/src/checker/type.cpp
diff options
context:
space:
mode:
authorgingerBill <ginger.bill.22@gmail.com>2016-08-22 15:43:13 +0100
committergingerBill <ginger.bill.22@gmail.com>2016-08-22 15:43:13 +0100
commit81c592b5e92411e4b64744a152bd445bb6154433 (patch)
tree1034a7abdff999d72561aa29b8a90ecb3fb19c76 /src/checker/type.cpp
parenta98e93f03f7bd62ebf589b473c61ab5daf37f02c (diff)
Integer Enumerations
Diffstat (limited to 'src/checker/type.cpp')
-rw-r--r--src/checker/type.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/checker/type.cpp b/src/checker/type.cpp
index 6613228e6..ed8130d1e 100644
--- a/src/checker/type.cpp
+++ b/src/checker/type.cpp
@@ -62,6 +62,7 @@ struct BasicType {
TYPE_KIND(Vector), \
TYPE_KIND(Slice), \
TYPE_KIND(Structure), \
+ TYPE_KIND(Enumeration), \
TYPE_KIND(Pointer), \
TYPE_KIND(Named), \
TYPE_KIND(Tuple), \
@@ -126,6 +127,11 @@ struct Type {
isize param_count;
isize result_count;
} proc;
+ struct {
+ Type * base; // Default is `int`
+ Entity **fields; // Entity_Constant
+ isize field_count;
+ } enumeration;
};
};
@@ -181,6 +187,11 @@ Type *make_type_structure(gbAllocator a) {
return t;
}
+Type *make_type_enumeration(gbAllocator a) {
+ Type *t = alloc_type(a, Type_Enumeration);
+ return t;
+}
+
Type *make_type_pointer(gbAllocator a, Type *elem) {
Type *t = alloc_type(a, Type_Pointer);
t->pointer.elem = elem;
@@ -396,6 +407,17 @@ Type *base_vector_type(Type *t) {
}
return t;
}
+b32 is_type_enum(Type *t) {
+ t = get_base_type(t);
+ return t->kind == Type_Enumeration;
+}
+Type *get_enum_base_type(Type *t) {
+ Type *bt = get_base_type(t);
+ if (is_type_enum(bt)) {
+ return bt->enumeration.base;
+ }
+ return t;
+}
@@ -417,6 +439,8 @@ b32 is_type_comparable(Type *t) {
return is_type_comparable(t->array.elem);
case Type_Vector:
return is_type_comparable(t->vector.elem);
+ case Type_Enumeration:
+ return is_type_comparable(t->enumeration.base);
case Type_Proc:
return true;
}
@@ -581,6 +605,9 @@ i64 type_align_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
return max;
}
} break;
+
+ case Type_Enumeration:
+ return type_align_of(s, allocator, t->enumeration.base);
}
return gb_clamp(next_pow2(type_size_of(s, allocator, t)), 1, s.max_align);
@@ -671,6 +698,9 @@ i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
type_set_offsets(s, allocator, t);
return t->structure.offsets[count-1] + type_size_of(s, allocator, t->structure.fields[count-1]->type);
} break;
+
+ case Type_Enumeration:
+ return type_size_of(s, allocator, t->enumeration.base);
}
// Catch all
@@ -727,6 +757,11 @@ gbString write_type_to_string(gbString str, Type *type) {
str = gb_string_appendc(str, "}");
} break;
+ case Type_Enumeration: {
+ str = gb_string_appendc(str, "enum ");
+ str = write_type_to_string(str, type->enumeration.base);
+ } break;
+
case Type_Pointer:
str = gb_string_appendc(str, "^");
str = write_type_to_string(str, type->pointer.elem);