Add enum type info and fix enum casting

This commit is contained in:
Ginger Bill
2017-01-01 16:58:38 +00:00
parent 6fef74317c
commit 311b5cb6e2
11 changed files with 178 additions and 193 deletions
+1 -2
View File
@@ -1177,7 +1177,6 @@ void check_global_collect_entities_from_file(Checker *c, Scope *parent_scope, As
case_end;
case_ast_node(vs, ValueSpec, spec);
switch (vs->keyword) {
case Token_let:
case Token_var: {
// NOTE(bill): You need to store the entity information here unline a constant declaration
isize entity_count = vs->names.count;
@@ -1202,7 +1201,7 @@ void check_global_collect_entities_from_file(Checker *c, Scope *parent_scope, As
error_node(name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[name->kind]));
continue;
}
Entity *e = make_entity_variable(c->allocator, parent_scope, name->Ident, NULL, vs->keyword == Token_let);
Entity *e = make_entity_variable(c->allocator, parent_scope, name->Ident, NULL);
e->identifier = name;
entities[entity_index++] = e;
+4 -2
View File
@@ -95,18 +95,20 @@ void check_init_variables(Checker *c, Entity **lhs, isize lhs_count, AstNodeArra
error(lhs[0]->token, "Assignment count mismatch `%td` = `%td`", lhs_count, rhs_count);
}
#if 0
if (lhs[0]->kind == Entity_Variable &&
lhs[0]->Variable.is_let) {
if (lhs_count != rhs_count) {
error(lhs[0]->token, "`let` variables must be initialized, `%td` = `%td`", lhs_count, rhs_count);
}
}
#endif
gb_temp_arena_memory_end(tmp);
}
void check_var_spec_node(Checker *c, AstNodeValueSpec *vs) {
GB_ASSERT(vs->keyword == Token_var || vs->keyword == Token_let);
GB_ASSERT(vs->keyword == Token_var);
isize entity_count = vs->names.count;
isize entity_index = 0;
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
@@ -123,7 +125,7 @@ void check_var_spec_node(Checker *c, AstNodeValueSpec *vs) {
found = current_scope_lookup_entity(c->context.scope, str);
}
if (found == NULL) {
entity = make_entity_variable(c->allocator, c->context.scope, token, NULL, vs->keyword == Token_let);
entity = make_entity_variable(c->allocator, c->context.scope, token, NULL);
add_entity_definition(&c->info, name, entity);
} else {
TokenPos pos = found->token.pos;
+5 -7
View File
@@ -57,7 +57,6 @@ struct Entity {
struct {
i32 field_index;
i32 field_src_index;
bool is_let;
} Variable;
i32 TypeName;
struct {
@@ -97,9 +96,8 @@ Entity *alloc_entity(gbAllocator a, EntityKind kind, Scope *scope, Token token,
return entity;
}
Entity *make_entity_variable(gbAllocator a, Scope *scope, Token token, Type *type, bool is_let) {
Entity *make_entity_variable(gbAllocator a, Scope *scope, Token token, Type *type) {
Entity *entity = alloc_entity(a, Entity_Variable, scope, token, type);
entity->Variable.is_let = is_let;
return entity;
}
@@ -124,7 +122,7 @@ Entity *make_entity_type_name(gbAllocator a, Scope *scope, Token token, Type *ty
}
Entity *make_entity_param(gbAllocator a, Scope *scope, Token token, Type *type, bool anonymous) {
Entity *entity = make_entity_variable(a, scope, token, type, false);
Entity *entity = make_entity_variable(a, scope, token, type);
entity->flags |= EntityFlag_Used;
entity->flags |= EntityFlag_Anonymous*(anonymous != 0);
entity->flags |= EntityFlag_Param;
@@ -132,7 +130,7 @@ Entity *make_entity_param(gbAllocator a, Scope *scope, Token token, Type *type,
}
Entity *make_entity_field(gbAllocator a, Scope *scope, Token token, Type *type, bool anonymous, i32 field_src_index) {
Entity *entity = make_entity_variable(a, scope, token, type, false);
Entity *entity = make_entity_variable(a, scope, token, type);
entity->Variable.field_src_index = field_src_index;
entity->Variable.field_index = field_src_index;
entity->flags |= EntityFlag_Field;
@@ -141,7 +139,7 @@ Entity *make_entity_field(gbAllocator a, Scope *scope, Token token, Type *type,
}
Entity *make_entity_vector_elem(gbAllocator a, Scope *scope, Token token, Type *type, i32 field_src_index) {
Entity *entity = make_entity_variable(a, scope, token, type, false);
Entity *entity = make_entity_variable(a, scope, token, type);
entity->Variable.field_src_index = field_src_index;
entity->Variable.field_index = field_src_index;
entity->flags |= EntityFlag_Field;
@@ -186,6 +184,6 @@ Entity *make_entity_implicit_value(gbAllocator a, String name, Type *type, Impli
Entity *make_entity_dummy_variable(gbAllocator a, Scope *file_scope, Token token) {
token.string = str_lit("_");
return make_entity_variable(a, file_scope, token, NULL, false);
return make_entity_variable(a, file_scope, token, NULL);
}
+7 -7
View File
@@ -791,7 +791,7 @@ void check_enum_type(Checker *c, Type *enum_type, Type *named_type, AstNode *nod
continue;
}
GB_ASSERT(c->context.iota.kind == ExactValue_Invalid);
ExactValue context_iota = c->context.iota;
c->context.iota = e->Constant.value;
e->Constant.value = (ExactValue){0};
@@ -799,7 +799,7 @@ void check_enum_type(Checker *c, Type *enum_type, Type *named_type, AstNode *nod
check_expr(c, &operand, init);
check_init_constant(c, e, &operand);
c->context.iota = (ExactValue){0};
c->context.iota = context_iota;
if (operand.mode == Addressing_Constant) {
HashKey key = hash_string(name);
@@ -1001,9 +1001,9 @@ void check_identifier(Checker *c, Operand *o, AstNode *n, Type *named_type) {
}
#else
o->mode = Addressing_Variable;
if (e->Variable.is_let) {
o->mode = Addressing_Value;
}
// if (e->Variable.is_let) {
// o->mode = Addressing_Value;
// }
#endif
break;
@@ -1770,8 +1770,8 @@ bool check_is_castable_to(Checker *c, Operand *operand, Type *y) {
}
Type *x = operand->type;
Type *xb = base_type(x);
Type *yb = base_type(y);
Type *xb = base_type(base_enum_type(x));
Type *yb = base_type(base_enum_type(y));
if (are_types_identical(xb, yb)) {
return true;
}
+2 -3
View File
@@ -848,7 +848,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
tt = make_type_pointer(c->allocator, case_type);
add_type_info_type(c, tt);
}
Entity *tag_var = make_entity_variable(c->allocator, c->context.scope, ms->var->Ident, tt, false);
Entity *tag_var = make_entity_variable(c->allocator, c->context.scope, ms->var->Ident, tt);
tag_var->flags |= EntityFlag_Used;
add_entity(c, c->context.scope, ms->var, tag_var);
add_entity_use(c, ms->var, tag_var);
@@ -1106,7 +1106,6 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
case_end;
case_ast_node(vs, ValueSpec, spec);
switch (vs->keyword) {
case Token_let:
case Token_var: {
isize entity_count = vs->names.count;
isize entity_index = 0;
@@ -1124,7 +1123,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
found = current_scope_lookup_entity(c->context.scope, str);
}
if (found == NULL) {
entity = make_entity_variable(c->allocator, c->context.scope, token, NULL, vs->keyword == Token_let);
entity = make_entity_variable(c->allocator, c->context.scope, token, NULL);
add_entity_definition(&c->info, name, entity);
} else {
TokenPos pos = found->token.pos;
+4 -9
View File
@@ -1211,7 +1211,6 @@ void fix_advance_to_next_stmt(AstFile *f) {
return;
case Token_var:
case Token_let:
case Token_const:
case Token_type:
case Token_proc:
@@ -2154,7 +2153,6 @@ PARSE_SPEC_PROC(parse_value_spec) {
switch (keyword) {
case Token_var:
case Token_let:
if (type == NULL && values.count == 0 && names.count > 0) {
syntax_error(f->curr_token, "Missing type or initialization");
return make_bad_decl(f, f->curr_token, f->curr_token);
@@ -2240,7 +2238,6 @@ PARSE_SPEC_PROC(parse_include_spec) {
AstNode *parse_decl(AstFile *f) {
switch (f->curr_token.kind) {
case Token_var:
case Token_let:
case Token_const:
return parse_generic_decl(f, f->curr_token.kind, parse_value_spec);
@@ -2269,7 +2266,6 @@ AstNode *parse_decl(AstFile *f) {
AstNode *parse_simple_stmt(AstFile *f) {
switch (f->curr_token.kind) {
case Token_var:
case Token_let:
case Token_const:
return parse_decl(f);
}
@@ -2555,9 +2551,11 @@ AstNode *parse_identifier_or_type(AstFile *f) {
base_type = parse_type(f);
}
Token open = expect_token(f, Token_OpenBrace);
AstNodeArray fields = parse_element_list(f);
AstNodeArray values = parse_element_list(f);
Token close = expect_token(f, Token_CloseBrace);
return make_enum_type(f, token, base_type, fields);
return make_enum_type(f, token, base_type, values);
}
case Token_proc: {
@@ -3026,7 +3024,6 @@ AstNode *parse_stmt(AstFile *f) {
return s;
case Token_var:
case Token_let:
case Token_const:
case Token_proc:
case Token_type:
@@ -3071,8 +3068,6 @@ AstNode *parse_stmt(AstFile *f) {
case AstNode_GenericDecl:
if (node->GenericDecl.token.kind == Token_var) {
valid = true;
} else if (node->GenericDecl.token.kind == Token_let) {
valid = true;
}
break;
}
+6 -8
View File
@@ -1233,8 +1233,7 @@ ssaValue *ssa_add_local_generated(ssaProcedure *proc, Type *type) {
Entity *e = make_entity_variable(proc->module->allocator,
scope,
empty_token,
type,
false);
type);
return ssa_add_local(proc, e);
}
@@ -1920,8 +1919,8 @@ ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) {
return value;
}
Type *src = base_type(src_type);
Type *dst = base_type(t);
Type *src = base_type(base_enum_type(src_type));
Type *dst = base_type(base_enum_type(t));
if (value->kind == ssaValue_Constant) {
if (is_type_any(dst)) {
@@ -3934,7 +3933,6 @@ void ssa_build_stmt_internal(ssaProcedure *proc, AstNode *node) {
switch (vs->keyword) {
case Token_const:
break;
case Token_let:
case Token_var: {
ssaModule *m = proc->module;
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&m->tmp_arena);
@@ -4784,7 +4782,7 @@ void ssa_init_module(ssaModule *m, Checker *c, BuildContext *build_context) {
{
String name = str_lit(SSA_TYPE_INFO_DATA_NAME);
isize count = c->info.type_info_map.entries.count;
Entity *e = make_entity_variable(m->allocator, NULL, make_token_ident(name), make_type_array(m->allocator, t_type_info, count), false);
Entity *e = make_entity_variable(m->allocator, NULL, make_token_ident(name), make_type_array(m->allocator, t_type_info, count));
ssaValue *g = ssa_make_value_global(m->allocator, e, NULL);
g->Global.is_private = true;
ssa_module_add_value(m, e, g);
@@ -4816,7 +4814,7 @@ void ssa_init_module(ssaModule *m, Checker *c, BuildContext *build_context) {
String name = str_lit(SSA_TYPE_INFO_DATA_MEMBER_NAME);
Entity *e = make_entity_variable(m->allocator, NULL, make_token_ident(name),
make_type_array(m->allocator, t_type_info_member, count), false);
make_type_array(m->allocator, t_type_info_member, count));
ssaValue *g = ssa_make_value_global(m->allocator, e, NULL);
ssa_module_add_value(m, e, g);
map_ssa_value_set(&m->members, hash_string(name), g);
@@ -5504,7 +5502,7 @@ void ssa_gen_tree(ssaGen *s) {
token.string.text = gb_alloc_array(a, u8, name_len);
token.string.len = gb_snprintf(cast(char *)token.string.text, name_len,
"%s-%d", name_base, id)-1;
Entity *e = make_entity_variable(a, NULL, token, make_type_array(a, t_string, count), false);
Entity *e = make_entity_variable(a, NULL, token, make_type_array(a, t_string, count));
name_array = ssa_make_value_global(a, e, NULL);
name_array->Global.is_private = true;
ssa_module_add_value(m, e, name_array);
-1
View File
@@ -85,7 +85,6 @@ TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \
TOKEN_KIND(Token_type, "type"), \
TOKEN_KIND(Token_proc, "proc"), \
TOKEN_KIND(Token_var, "var"), \
TOKEN_KIND(Token_let, "let"), \
TOKEN_KIND(Token_const, "const"), \
TOKEN_KIND(Token_import, "import"), \
TOKEN_KIND(Token_include, "include"), \