Update ImplicitValue "architecture"

This commit is contained in:
Ginger Bill
2016-10-15 22:30:58 +01:00
parent f3209584a3
commit 79af939522
10 changed files with 130 additions and 89 deletions
+44 -2
View File
@@ -181,6 +181,23 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_Count] = {
{STR_LIT("enum_to_string"), 1, false, Expr_Expr},
};
enum ImplicitValueId {
ImplicitValue_Invalid,
ImplicitValue_context,
ImplicitValue_Count,
};
struct ImplicitValueInfo {
String name;
String backing_name;
Type * type;
};
// NOTE(bill): This is initialized later
gb_global ImplicitValueInfo implicit_value_infos[ImplicitValue_Count] = {};
struct CheckerContext {
Scope *scope;
DeclInfo *decl;
@@ -199,6 +216,8 @@ struct CheckerInfo {
Map<isize> type_info_map; // Key: Type *
Map<AstFile *> files; // Key: String
isize type_info_index;
Entity * implicit_values[ImplicitValue_Count];
};
struct Checker {
@@ -423,8 +442,9 @@ Entity *scope_insert_entity(Scope *s, Entity *entity) {
String name = entity->token.string;
HashKey key = hash_string(name);
Entity **found = map_get(&s->elements, key);
if (found)
if (found) {
return *found;
}
map_set(&s->elements, key, entity);
if (entity->scope == NULL) {
entity->scope = s;
@@ -950,8 +970,17 @@ void init_preload_types(Checker *c) {
t_context = e->type;
t_context_ptr = make_type_pointer(c->allocator, t_context);
add_global_entity(make_entity_implicit_value(gb_heap_allocator(), make_string("context"), t_context));
}
}
void add_implicit_value(Checker *c, ImplicitValueId id, String name, String backing_name, Type *type) {
ImplicitValueInfo info = {name, backing_name, type};
Entity *value = make_entity_implicit_value(c->allocator, info.name, info.type, id);
Entity *prev = scope_insert_entity(c->global_scope, value);
GB_ASSERT(prev == NULL);
implicit_value_infos[id] = info;
c->info.implicit_values[id] = value;
}
void check_parsed_files(Checker *c) {
@@ -1228,11 +1257,24 @@ void check_parsed_files(Checker *c) {
check_global_entity(c, Entity_TypeName);
init_preload_types(c);
add_implicit_value(c, ImplicitValue_context, make_string("context"), make_string("__context"), t_context);
check_global_entity(c, Entity_Constant);
check_global_entity(c, Entity_Procedure);
check_global_entity(c, Entity_Variable);
for (isize i = 1; i < ImplicitValue_Count; i++) {
// NOTE(bill): First is invalid
Entity *e = c->info.implicit_values[i];
GB_ASSERT(e->kind == Entity_ImplicitValue);
ImplicitValueInfo *ivi = &implicit_value_infos[i];
Entity *backing = scope_lookup_entity(e->scope, ivi->backing_name);
GB_ASSERT(backing != NULL);
e->ImplicitValue.backing = backing;
}
// Check procedure bodies
for_array(i, c->procs) {
ProcedureInfo *pi = &c->procs[i];
+8 -3
View File
@@ -2,6 +2,7 @@ struct Scope;
struct Checker;
struct Type;
enum BuiltinProcId;
enum ImplicitValueId;
#define ENTITY_KINDS \
ENTITY_KIND(Invalid), \
@@ -63,10 +64,13 @@ struct Entity {
String path;
String name;
Scope *scope;
b32 used;
b32 used;
} ImportName;
struct {} Nil;
struct {} ImplicitValue;
struct {
ImplicitValueId id;
Entity * backing;
} ImplicitValue;
};
};
@@ -160,9 +164,10 @@ Entity *make_entity_nil(gbAllocator a, String name, Type *type) {
return entity;
}
Entity *make_entity_implicit_value(gbAllocator a, String name, Type *type) {
Entity *make_entity_implicit_value(gbAllocator a, String name, Type *type, ImplicitValueId id) {
Token token = make_token_ident(name);
Entity *entity = alloc_entity(a, Entity_ImplicitValue, NULL, token, type);
entity->ImplicitValue.id = id;
return entity;
}
+6 -1
View File
@@ -3677,7 +3677,7 @@ ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint
default: {
gbString str = type_to_string(type);
error(ast_node_token(node), "Invalid or unyet supported compound literal type `%s`", str);
error(ast_node_token(node), "Invalid compound literal type `%s`", str);
gb_string_free(str);
goto error;
} break;
@@ -4187,6 +4187,11 @@ gbString write_expr_to_string(gbString str, AstNode *node) {
str = write_expr_to_string(str, pt->type);
case_end;
case_ast_node(mt, MaybeType, node);
str = gb_string_appendc(str, "?");
str = write_expr_to_string(str, mt->type);
case_end;
case_ast_node(at, ArrayType, node);
str = gb_string_appendc(str, "[");
str = write_expr_to_string(str, at->count);
+10 -6
View File
@@ -1102,8 +1102,9 @@ i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
BasicKind kind = t->Basic.kind;
if (kind < gb_count_of(basic_type_sizes)) {
i64 size = basic_type_sizes[kind];
if (size > 0)
if (size > 0) {
return size;
}
}
if (kind == Basic_string) {
return 2 * s.word_size;
@@ -1114,8 +1115,9 @@ i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
case Type_Array: {
i64 count = t->Array.count;
if (count == 0)
if (count == 0) {
return 0;
}
i64 align = type_align_of(s, allocator, t->Array.elem);
i64 size = type_size_of(s, allocator, t->Array.elem);
i64 alignment = align_formula(size, align);
@@ -1124,8 +1126,9 @@ i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
case Type_Vector: {
i64 count = t->Vector.count;
if (count == 0)
if (count == 0) {
return 0;
}
// i64 align = type_align_of(s, allocator, t->Vector.elem);
i64 bit_size = 8*type_size_of(s, allocator, t->Vector.elem);
if (is_type_boolean(t->Vector.elem)) {
@@ -1169,8 +1172,9 @@ i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
// NOTE(bill): Zeroth field is invalid
for (isize i = 1; i < count; i++) {
i64 size = type_size_of(s, allocator, t->Record.fields[i]->type);
if (max < size)
if (max < size) {
max = size;
}
}
// NOTE(bill): Align to int
i64 align = type_align_of(s, allocator, t);
@@ -1184,8 +1188,9 @@ i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
i64 max = 0;
for (isize i = 0; i < count; i++) {
i64 size = type_size_of(s, allocator, t->Record.fields[i]->type);
if (max < size)
if (max < size) {
max = size;
}
}
// TODO(bill): Is this how it should work?
i64 align = type_align_of(s, allocator, t);
@@ -1211,7 +1216,6 @@ i64 type_offset_of(BaseTypeSizes s, gbAllocator allocator, Type *t, isize index)
return t->Record.struct_offsets[index];
}
} else if (t->kind == Type_Basic) {
gb_printf_err("here!!\n");
if (t->Basic.kind == Basic_string) {
switch (index) {
case 0: return 0;