aboutsummaryrefslogtreecommitdiff
path: root/src
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
parenta98e93f03f7bd62ebf589b473c61ab5daf37f02c (diff)
Integer Enumerations
Diffstat (limited to 'src')
-rw-r--r--src/checker/expr.cpp118
-rw-r--r--src/checker/type.cpp35
-rw-r--r--src/codegen/print_llvm.cpp3
-rw-r--r--src/codegen/ssa.cpp8
-rw-r--r--src/parser.cpp59
5 files changed, 204 insertions, 19 deletions
diff --git a/src/checker/expr.cpp b/src/checker/expr.cpp
index 415bbe545..128d9e6e0 100644
--- a/src/checker/expr.cpp
+++ b/src/checker/expr.cpp
@@ -57,6 +57,73 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node) {
struct_type->structure.is_packed = st->is_packed;
}
+void check_enum_type(Checker *c, Type *enum_type, AstNode *node) {
+ GB_ASSERT(node->kind == AstNode_EnumType);
+ GB_ASSERT(enum_type->kind == Type_Enumeration);
+ ast_node(et, EnumType, node);
+
+ Map<Entity *> entity_map = {};
+ map_init(&entity_map, gb_heap_allocator());
+ defer (map_destroy(&entity_map));
+
+ Type *base_type = t_int;
+ if (et->base_type != NULL) {
+ base_type = check_type(c, et->base_type);
+ }
+
+ if (base_type == NULL || !is_type_integer(base_type)) {
+ error(&c->error_collector, et->token, "Base type for enumeration must be an integer");
+ return;
+ } else
+ if (base_type == NULL) {
+ base_type = t_int;
+ }
+ enum_type->enumeration.base = base_type;
+
+ Entity **fields = gb_alloc_array(c->allocator, Entity *, et->field_count);
+ isize field_index = 0;
+ ExactValue iota = make_exact_value_integer(-1);
+ for (AstNode *field = et->field_list; field != NULL; field = field->next) {
+ ast_node(f, FieldValue, field);
+ Token name_token = f->field->Ident.token;
+
+ Operand o = {};
+ if (f->value != NULL) {
+ check_expr(c, &o, f->value);
+ if (o.mode != Addressing_Constant) {
+ error(&c->error_collector, ast_node_token(f->value), "Enumeration value must be a constant integer");
+ o.mode = Addressing_Invalid;
+ }
+ if (o.mode != Addressing_Invalid) {
+ check_assignment(c, &o, base_type, make_string("enumeration"));
+ }
+ if (o.mode != Addressing_Invalid) {
+ iota = o.value;
+ } else {
+ Token add_token = {Token_Add};
+ iota = exact_binary_operator_value(add_token, iota, make_exact_value_integer(1));
+ }
+ } else {
+ Token add_token = {Token_Add};
+ iota = exact_binary_operator_value(add_token, iota, make_exact_value_integer(1));
+ }
+
+ Entity *e = make_entity_constant(c->allocator, c->context.scope, name_token, enum_type, iota);
+
+ HashKey key = hash_string(name_token.string);
+ if (map_get(&entity_map, key)) {
+ // TODO(bill): Scope checking already checks the declaration
+ error(&c->error_collector, name_token, "`%.*s` is already declared in this enumeration", LIT(name_token.string));
+ } else {
+ map_set(&entity_map, key, e);
+ fields[field_index++] = e;
+ }
+ add_entity_use(&c->info, f->field, e);
+ }
+ enum_type->enumeration.fields = fields;
+ enum_type->enumeration.field_count = et->field_count;
+}
+
Type *check_get_params(Checker *c, Scope *scope, AstNode *field_list, isize field_count) {
if (field_list == NULL || field_count == 0)
return NULL;
@@ -399,6 +466,13 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type) {
goto end;
case_end;
+ case_ast_node(et, EnumType, e);
+ type = make_type_enumeration(c->allocator);
+ set_base_type(named_type, type);
+ check_enum_type(c, type, e);
+ goto end;
+ case_end;
+
case_ast_node(pt, PointerType, e);
type = make_type_pointer(c->allocator, check_type(c, pt->type));
set_base_type(named_type, type);
@@ -584,6 +658,7 @@ b32 check_value_is_expressible(Checker *c, ExactValue in_value, Type *type, Exac
if (out_value) *out_value = in_value;
}
+
return false;
}
@@ -821,8 +896,8 @@ b32 check_castable_to(Checker *c, Operand *operand, Type *y) {
return true;
Type *x = operand->type;
- Type *xb = get_base_type(x);
- Type *yb = get_base_type(y);
+ Type *xb = get_enum_base_type(get_base_type(x));
+ Type *yb = get_enum_base_type(get_base_type(y));
if (are_types_identical(xb, yb))
return true;
@@ -870,13 +945,12 @@ b32 check_castable_to(Checker *c, Operand *operand, Type *y) {
}
// proc <-> proc
- if (is_type_proc(xb), is_type_proc(yb)) {
+ if (is_type_proc(xb) && is_type_proc(yb)) {
return true;
}
-
// proc -> rawptr
- if (is_type_proc(xb), is_type_rawptr(yb)) {
+ if (is_type_proc(xb) && is_type_rawptr(yb)) {
return true;
}
@@ -903,6 +977,7 @@ void check_binary_expr(Checker *c, Operand *x, AstNode *node) {
Type *base_type = get_base_type(type);
if (is_const_expr && is_type_constant_type(base_type)) {
+
if (base_type->kind == Type_Basic) {
if (check_value_is_expressible(c, x->value, base_type, &x->value)) {
can_convert = true;
@@ -1167,7 +1242,7 @@ void convert_to_typed(Checker *c, Operand *operand, Type *target_type) {
return;
}
- Type *t = get_base_type(target_type);
+ Type *t = get_enum_base_type(get_base_type(target_type));
switch (t->kind) {
case Type_Basic:
if (operand->mode == Addressing_Constant) {
@@ -1239,7 +1314,7 @@ b32 check_index_value(Checker *c, AstNode *index_value, i64 max_count, i64 *valu
return false;
}
- if (!is_type_integer(operand.type)) {
+ if (!is_type_integer(get_enum_base_type(operand.type))) {
gbString expr_str = expr_to_string(operand.expr);
error(&c->error_collector, ast_node_token(operand.expr),
"Index `%s` must be an integer", expr_str);
@@ -1300,6 +1375,18 @@ Entity *lookup_field(Type *type, AstNode *field_node, isize *index = NULL) {
}
}
break;
+ case Type_Enumeration:
+ for (isize i = 0; i < type->enumeration.field_count; i++) {
+ Entity *f = type->enumeration.fields[i];
+ GB_ASSERT(f->kind == Entity_Constant);
+ String str = f->token.string;
+ if (are_strings_equal(field_str, str)) {
+ if (index) *index = i;
+ return f;
+ }
+ }
+ break;
+ break;
// TODO(bill): Other types and extra "hidden" fields (e.g. introspection stuff)
// TODO(bill): Allow for access of field through index? e.g. `x.3` will get member of index 3
// Or is this only suitable if tuples are first-class?
@@ -1328,10 +1415,17 @@ void check_selector(Checker *c, Operand *operand, AstNode *node) {
}
add_entity_use(&c->info, selector, entity);
- operand->type = entity->type;
- operand->expr = node;
- if (operand->mode != Addressing_Variable)
- operand->mode = Addressing_Value;
+ if (is_type_enum(operand->type)) {
+ operand->type = entity->type;
+ operand->expr = node;
+ operand->mode = Addressing_Constant;
+ operand->value = entity->Constant.value;
+ } else {
+ operand->type = entity->type;
+ operand->expr = node;
+ if (operand->mode != Addressing_Variable)
+ operand->mode = Addressing_Value;
+ }
} else {
operand->mode = Addressing_Invalid;
operand->expr = node;
@@ -2046,7 +2140,7 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
} break;
default: {
- gbString str = type_to_string(t);
+ gbString str = type_to_string(type);
error(&c->error_collector, ast_node_token(node), "Invalid compound literal type `%s`", str);
gb_string_free(str);
goto error;
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);
diff --git a/src/codegen/print_llvm.cpp b/src/codegen/print_llvm.cpp
index 6a983d09d..cb2e1e3ad 100644
--- a/src/codegen/print_llvm.cpp
+++ b/src/codegen/print_llvm.cpp
@@ -157,6 +157,9 @@ void ssa_print_type(gbFile *f, BaseTypeSizes s, Type *t) {
}
break;
+ case Type_Enumeration:
+ ssa_print_type(f, s, t->enumeration.base);
+ break;
case Type_Pointer:
ssa_print_type(f, s, t->pointer.elem);
ssa_fprintf(f, "*");
diff --git a/src/codegen/ssa.cpp b/src/codegen/ssa.cpp
index 0d9780e74..ad5e997ed 100644
--- a/src/codegen/ssa.cpp
+++ b/src/codegen/ssa.cpp
@@ -1237,10 +1237,11 @@ ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) {
}
- Type *src = get_base_type(src_type);
- Type *dst = get_base_type(t);
- if (are_types_identical(t, src_type))
+ Type *src = get_enum_base_type(get_base_type(src_type));
+ Type *dst = get_enum_base_type(get_base_type(t));
+ if (are_types_identical(src, dst)) {
return value;
+ }
if (value->kind == ssaValue_Constant) {
if (dst->kind == Type_Basic) {
@@ -1383,7 +1384,6 @@ ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) {
return v;
}
-
gb_printf_err("Not Identical %s != %s\n", type_to_string(src_type), type_to_string(t));
gb_printf_err("Not Identical %s != %s\n", type_to_string(src), type_to_string(dst));
diff --git a/src/parser.cpp b/src/parser.cpp
index a0d6084a7..beb0eb49f 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -218,6 +218,12 @@ AST_NODE_KIND(_TypeBegin, struct{}) \
isize field_count; \
b32 is_packed; \
}) \
+ AST_NODE_KIND(EnumType, struct { \
+ Token token; \
+ AstNode *base_type; \
+ AstNode *field_list; \
+ isize field_count; \
+ }) \
AST_NODE_KIND(_TypeEnd, struct{}) \
AST_NODE_KIND(Count, struct{})
@@ -361,6 +367,8 @@ Token ast_node_token(AstNode *node) {
return node->VectorType.token;
case AstNode_StructType:
return node->StructType.token;
+ case AstNode_EnumType:
+ return node->EnumType.token;
}
return empty_token;
@@ -763,6 +771,16 @@ gb_inline AstNode *make_struct_type(AstFile *f, Token token, AstNode *field_list
return result;
}
+
+gb_inline AstNode *make_enum_type(AstFile *f, Token token, AstNode *base_type, AstNode *field_list, isize field_count) {
+ AstNode *result = make_node(f, AstNode_EnumType);
+ result->EnumType.token = token;
+ result->EnumType.base_type = base_type;
+ result->EnumType.field_list = field_list;
+ result->EnumType.field_count = field_count;
+ return result;
+}
+
gb_inline AstNode *make_type_decl(AstFile *f, Token token, AstNode *name, AstNode *type) {
AstNode *result = make_node(f, AstNode_TypeDecl);
result->TypeDecl.token = token;
@@ -1616,6 +1634,43 @@ AstNode *parse_identifier_or_type(AstFile *f) {
return make_struct_type(f, token, params, param_count, is_packed);
}
+ case Token_enum: {
+ Token token = expect_token(f, Token_enum);
+ AstNode *base_type = NULL;
+ Token open, close;
+
+ if (f->cursor[0].kind != Token_OpenBrace) {
+ base_type = parse_type(f);
+ }
+
+ AstNode *root = NULL;
+ AstNode *curr = NULL;
+ isize field_count = 0;
+
+ open = expect_token(f, Token_OpenBrace);
+
+ while (f->cursor[0].kind != Token_CloseBrace &&
+ f->cursor[0].kind != Token_EOF) {
+ AstNode *name = parse_identifier(f);
+ AstNode *value = NULL;
+ Token eq = empty_token;
+ if (f->cursor[0].kind == Token_Eq) {
+ eq = expect_token(f, Token_Eq);
+ value = parse_value(f);
+ }
+ AstNode *field = make_field_value(f, name, value, eq);
+ DLIST_APPEND(root, curr, field);
+ field_count++;
+ if (f->cursor[0].kind != Token_Comma)
+ break;
+ next_token(f);
+ }
+
+ close = expect_token(f, Token_CloseBrace);
+
+ return make_enum_type(f, token, base_type, root, field_count);
+ }
+
case Token_proc:
return parse_proc_type(f, NULL);
@@ -1763,9 +1818,6 @@ AstNode *parse_decl(AstFile *f, AstNode *name_list, isize name_count) {
}
AstNode *type = parse_type(f);
- // if (type->kind != AstNode_StructType) {
- // expect_token(f, Token_Semicolon);
- // }
return make_type_decl(f, token, name_list, type);
} else if (f->cursor[0].kind == Token_proc &&
declaration_kind == Declaration_Immutable) {
@@ -1980,6 +2032,7 @@ AstNode *parse_stmt(AstFile *f) {
if (s->kind != AstNode_ProcDecl &&
(s->kind == AstNode_TypeDecl &&
s->TypeDecl.type->kind != AstNode_StructType &&
+ s->TypeDecl.type->kind != AstNode_EnumType &&
s->TypeDecl.type->kind != AstNode_ProcType) &&
!allow_token(f, Token_Semicolon)) {
// CLEANUP(bill): Semicolon handling in parser