aboutsummaryrefslogtreecommitdiff
path: root/src/checker
diff options
context:
space:
mode:
Diffstat (limited to 'src/checker')
-rw-r--r--src/checker/checker.cpp97
-rw-r--r--src/checker/entity.cpp22
-rw-r--r--src/checker/expr.cpp91
-rw-r--r--src/checker/types.cpp98
4 files changed, 143 insertions, 165 deletions
diff --git a/src/checker/checker.cpp b/src/checker/checker.cpp
index e0d51f84c..7461aefff 100644
--- a/src/checker/checker.cpp
+++ b/src/checker/checker.cpp
@@ -7,7 +7,7 @@
#define MAP_NAME MapEntity
#include "../map.c"
-enum AddressingMode {
+typedef enum AddressingMode {
Addressing_Invalid,
Addressing_NoValue,
Addressing_Value,
@@ -16,25 +16,25 @@ enum AddressingMode {
Addressing_Type,
Addressing_Builtin,
Addressing_Count,
-};
+} AddressingMode;
-struct Operand {
+typedef struct Operand {
AddressingMode mode;
Type * type;
ExactValue value;
AstNode * expr;
BuiltinProcId builtin_id;
-};
+} Operand;
-struct TypeAndValue {
+typedef struct TypeAndValue {
AddressingMode mode;
Type * type;
ExactValue value;
-};
+} TypeAndValue;
-struct DeclInfo {
+typedef struct DeclInfo {
Scope *scope;
Entity **entities;
@@ -46,30 +46,30 @@ struct DeclInfo {
u32 var_decl_tags;
MapBool deps; // Key: Entity *
-};
+} DeclInfo;
-struct ExprInfo {
+typedef struct ExprInfo {
bool is_lhs; // Debug info
AddressingMode mode;
Type * type; // Type_Basic
ExactValue value;
-};
+} ExprInfo;
ExprInfo make_expr_info(bool is_lhs, AddressingMode mode, Type *type, ExactValue value) {
ExprInfo ei = {is_lhs, mode, type, value};
return ei;
}
-struct ProcedureInfo {
+typedef struct ProcedureInfo {
AstFile * file;
Token token;
DeclInfo *decl;
Type * type; // Type_Procedure
AstNode * body; // AstNode_BlockStatement
u32 tags;
-};
+} ProcedureInfo;
-struct Scope {
+typedef struct Scope {
Scope * parent;
Scope * prev, *next;
Scope * first_child;
@@ -84,15 +84,15 @@ struct Scope {
bool is_file;
bool is_init;
AstFile * file;
-};
+} Scope;
gb_global Scope *universal_scope = NULL;
-enum ExprKind {
+typedef enum ExprKind {
Expr_Expr,
Expr_Stmt,
-};
+} ExprKind;
-enum BuiltinProcId {
+typedef enum BuiltinProcId {
BuiltinProc_Invalid,
BuiltinProc_new,
@@ -129,13 +129,13 @@ enum BuiltinProcId {
BuiltinProc_enum_to_string,
BuiltinProc_Count,
-};
-struct BuiltinProc {
+} BuiltinProcId;
+typedef struct BuiltinProc {
String name;
isize arg_count;
bool variadic;
ExprKind kind;
-};
+} BuiltinProc;
gb_global BuiltinProc builtin_procs[BuiltinProc_Count] = {
{STR_LIT(""), 0, false, Expr_Stmt},
@@ -173,28 +173,28 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_Count] = {
{STR_LIT("enum_to_string"), 1, false, Expr_Expr},
};
-enum ImplicitValueId {
+typedef enum ImplicitValueId {
ImplicitValue_Invalid,
ImplicitValue_context,
ImplicitValue_Count,
-};
-struct ImplicitValueInfo {
+} ImplicitValueId;
+typedef struct ImplicitValueInfo {
String name;
String backing_name;
Type * type;
-};
+} ImplicitValueInfo;
// NOTE(bill): This is initialized later
gb_global ImplicitValueInfo implicit_value_infos[ImplicitValue_Count] = {};
-struct CheckerContext {
+typedef struct CheckerContext {
Scope * scope;
DeclInfo *decl;
u32 stmt_state_flags;
-};
+} CheckerContext;
#define MAP_TYPE TypeAndValue
#define MAP_FUNC map_tav_
@@ -223,7 +223,7 @@ struct CheckerContext {
// NOTE(bill): Symbol tables
-struct CheckerInfo {
+typedef struct CheckerInfo {
MapTypeAndValue types; // Key: AstNode * | Expression -> Type (and value)
MapEntity definitions; // Key: AstNode * | Identifier -> Entity
MapEntity uses; // Key: AstNode * | Identifier -> Entity
@@ -235,9 +235,9 @@ struct CheckerInfo {
MapIsize type_info_map; // Key: Type *
isize type_info_count;
Entity * implicit_values[ImplicitValue_Count];
-};
+} CheckerInfo;
-struct Checker {
+typedef struct Checker {
Parser * parser;
CheckerInfo info;
@@ -255,11 +255,11 @@ struct Checker {
Array(Type *) proc_stack;
bool in_defer; // TODO(bill): Actually handle correctly
-};
+} Checker;
-struct CycleChecker {
+typedef struct CycleChecker {
Array(Entity *) path; // Entity_TypeName
-};
+} CycleChecker;
@@ -766,7 +766,7 @@ void add_type_info_type(Checker *c, Type *t) {
isize ti_index = -1;
for_array(i, c->info.type_info_map.entries) {
- auto *e = &c->info.type_info_map.entries.e[i];
+ MapIsizeEntry *e = &c->info.type_info_map.entries.e[i];
Type *prev_type = cast(Type *)e->key.ptr;
if (are_types_identical(t, prev_type)) {
// Duplicate entry
@@ -931,7 +931,7 @@ MapEntity generate_minimum_dependency_map(CheckerInfo *info, Entity *start) {
map_entity_init(&map, heap_allocator());
for_array(i, info->entities.entries) {
- auto *entry = &info->entities.entries.e[i];
+ MapDeclInfoEntry *entry = &info->entities.entries.e[i];
Entity *e = cast(Entity *)cast(uintptr)entry->key.key;
if (e->scope->is_global) {
// NOTE(bill): Require runtime stuff
@@ -1023,7 +1023,7 @@ void add_implicit_value(Checker *c, ImplicitValueId id, String name, String back
void check_global_entity(Checker *c, EntityKind kind) {
for_array(i, c->info.entities.entries) {
- auto *entry = &c->info.entities.entries.e[i];
+ MapDeclInfoEntry *entry = &c->info.entities.entries.e[i];
Entity *e = cast(Entity *)cast(uintptr)entry->key.key;
if (e->kind == kind) {
DeclInfo *d = entry->value;
@@ -1203,7 +1203,7 @@ void check_parsed_files(Checker *c) {
ast_node(id, ImportDecl, decl);
HashKey key = hash_string(id->fullpath);
- auto found = map_scope_get(&file_scopes, key);
+ Scope **found = map_scope_get(&file_scopes, key);
GB_ASSERT_MSG(found != NULL, "Unable to find scope for file: %.*s", LIT(id->fullpath));
Scope *scope = *found;
@@ -1338,7 +1338,7 @@ void check_parsed_files(Checker *c) {
// Add untyped expression values
for_array(i, c->info.untyped.entries) {
- auto *entry = &c->info.untyped.entries.e[i];
+ MapExprInfoEntry *entry = &c->info.untyped.entries.e[i];
HashKey key = entry->key;
AstNode *expr = cast(AstNode *)cast(uintptr)key.key;
ExprInfo *info = &entry->value;
@@ -1364,29 +1364,6 @@ void check_parsed_files(Checker *c) {
}
}
- // for_array(i, c->info.type_info_map.entries) {
- // auto *e = &c->info.type_info_map.entries[i];
- // Type *prev_type = cast(Type *)e->key.ptr;
- // gb_printf("%td - %s\n", i, type_to_string(prev_type));
- // }
-
- // for_array(i, c->info.type_info_map.entries) {
- // auto *p = &c->info.type_info_map.entries[i];
- // for (isize j = 0; j < i-1; j++) {
- // auto *q = &c->info.type_info_map.entries[j];
- // Type *a = cast(Type *)p->key.ptr;
- // Type *b = cast(Type *)q->key.ptr;
- // p->value = i;
- // // GB_ASSERT(!are_types_identical(a, b));
- // }
- // }
-
- // for_array(i, c->info.type_info_map.entries) {
- // auto *e = &c->info.type_info_map.entries[i];
- // Type *prev_type = cast(Type *)e->key.ptr;
- // gb_printf("%td - %s\n", e->value, type_to_string(prev_type));
- // }
-
map_scope_destroy(&file_scopes);
array_free(&import_decls);
}
diff --git a/src/checker/entity.cpp b/src/checker/entity.cpp
index 2fdc0f480..83ea1d782 100644
--- a/src/checker/entity.cpp
+++ b/src/checker/entity.cpp
@@ -1,8 +1,8 @@
-struct Scope;
-struct Checker;
-struct Type;
-enum BuiltinProcId;
-enum ImplicitValueId;
+typedef struct Scope Scope;
+typedef struct Checker Checker;
+typedef struct Type Type;
+typedef enum BuiltinProcId BuiltinProcId;
+typedef enum ImplicitValueId ImplicitValueId;
#define ENTITY_KINDS \
ENTITY_KIND(Invalid) \
@@ -16,11 +16,11 @@ enum ImplicitValueId;
ENTITY_KIND(ImplicitValue) \
ENTITY_KIND(Count)
-enum EntityKind {
+typedef enum EntityKind {
#define ENTITY_KIND(k) GB_JOIN2(Entity_, k),
ENTITY_KINDS
#undef ENTITY_KIND
-};
+} EntityKind;
String const entity_strings[] = {
#define ENTITY_KIND(k) {cast(u8 *)#k, gb_size_of(#k)-1},
@@ -28,16 +28,16 @@ String const entity_strings[] = {
#undef ENTITY_KIND
};
-enum EntityFlag : u32 {
+typedef enum EntityFlag {
EntityFlag_Visited = 1<<0,
EntityFlag_Used = 1<<1,
EntityFlag_Anonymous = 1<<2,
EntityFlag_Field = 1<<3,
EntityFlag_Param = 1<<4,
EntityFlag_VectorElem = 1<<5,
-};
+} EntityFlag;
-struct Entity {
+typedef struct Entity {
EntityKind kind;
u32 flags;
Token token;
@@ -75,7 +75,7 @@ struct Entity {
Entity * backing;
} ImplicitValue;
};
-};
+} Entity;
bool is_entity_exported(Entity *e) {
if (e->kind == Entity_ImportName) {
diff --git a/src/checker/expr.cpp b/src/checker/expr.cpp
index e6ddc0d07..5ce522d38 100644
--- a/src/checker/expr.cpp
+++ b/src/checker/expr.cpp
@@ -3573,6 +3573,49 @@ void check_expr_with_type_hint(Checker *c, Operand *o, AstNode *e, Type *t) {
}
}
+bool check_set_index_data(Operand *o, Type *t, i64 *max_count) {
+ t = base_type(type_deref(t));
+
+ switch (t->kind) {
+ case Type_Basic:
+ if (is_type_string(t)) {
+ if (o->mode == Addressing_Constant) {
+ *max_count = o->value.value_string.len;
+ }
+ if (o->mode != Addressing_Variable) {
+ o->mode = Addressing_Value;
+ }
+ o->type = t_u8;
+ return true;
+ }
+ break;
+
+ case Type_Array:
+ *max_count = t->Array.count;
+ if (o->mode != Addressing_Variable) {
+ o->mode = Addressing_Value;
+ }
+ o->type = t->Array.elem;
+ return true;
+
+ case Type_Vector:
+ *max_count = t->Vector.count;
+ if (o->mode != Addressing_Variable) {
+ o->mode = Addressing_Value;
+ }
+ o->type = t->Vector.elem;
+ return true;
+
+
+ case Type_Slice:
+ o->type = t->Slice.elem;
+ o->mode = Addressing_Variable;
+ return true;
+ }
+
+ return false;
+}
+
ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint) {
ExprKind kind = Expr_Stmt;
@@ -3892,52 +3935,8 @@ ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint
Type *t = base_type(type_deref(o->type));
bool is_const = o->mode == Addressing_Constant;
-
- auto set_index_data = [](Operand *o, Type *t, i64 *max_count) -> bool {
- t = base_type(type_deref(t));
-
- switch (t->kind) {
- case Type_Basic:
- if (is_type_string(t)) {
- if (o->mode == Addressing_Constant) {
- *max_count = o->value.value_string.len;
- }
- if (o->mode != Addressing_Variable) {
- o->mode = Addressing_Value;
- }
- o->type = t_u8;
- return true;
- }
- break;
-
- case Type_Array:
- *max_count = t->Array.count;
- if (o->mode != Addressing_Variable) {
- o->mode = Addressing_Value;
- }
- o->type = t->Array.elem;
- return true;
-
- case Type_Vector:
- *max_count = t->Vector.count;
- if (o->mode != Addressing_Variable) {
- o->mode = Addressing_Value;
- }
- o->type = t->Vector.elem;
- return true;
-
-
- case Type_Slice:
- o->type = t->Slice.elem;
- o->mode = Addressing_Variable;
- return true;
- }
-
- return false;
- };
-
i64 max_count = -1;
- bool valid = set_index_data(o, t, &max_count);
+ bool valid = check_set_index_data(o, t, &max_count);
if (is_const) {
valid = false;
@@ -3946,7 +3945,7 @@ ExprKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *type_hint
if (!valid && (is_type_struct(t) || is_type_raw_union(t))) {
Entity *found = find_using_index_expr(t);
if (found != NULL) {
- valid = set_index_data(o, found->type, &max_count);
+ valid = check_set_index_data(o, found->type, &max_count);
}
}
diff --git a/src/checker/types.cpp b/src/checker/types.cpp
index fc756ad52..e229aa70f 100644
--- a/src/checker/types.cpp
+++ b/src/checker/types.cpp
@@ -1,6 +1,6 @@
-struct Scope;
+typedef struct Scope Scope;
-enum BasicKind {
+typedef enum BasicKind {
Basic_Invalid,
Basic_bool,
Basic_i8,
@@ -34,9 +34,9 @@ enum BasicKind {
Basic_byte = Basic_u8,
Basic_rune = Basic_i32,
-};
+} BasicKind;
-enum BasicFlag : u32 {
+typedef enum BasicFlag {
BasicFlag_Boolean = GB_BIT(0),
BasicFlag_Integer = GB_BIT(1),
BasicFlag_Unsigned = GB_BIT(2),
@@ -49,14 +49,14 @@ enum BasicFlag : u32 {
BasicFlag_Numeric = BasicFlag_Integer | BasicFlag_Float,
BasicFlag_Ordered = BasicFlag_Numeric | BasicFlag_String | BasicFlag_Pointer,
BasicFlag_ConstantType = BasicFlag_Boolean | BasicFlag_Numeric | BasicFlag_Pointer | BasicFlag_String | BasicFlag_Rune,
-};
+} BasicFlag;
-struct BasicType {
+typedef struct BasicType {
BasicKind kind;
u32 flags;
i64 size; // -1 if arch. dep.
String name;
-};
+} BasicType;
@@ -74,19 +74,19 @@ struct BasicType {
TYPE_KIND(Proc), \
TYPE_KIND(Count),
-enum TypeKind {
-#define TYPE_KIND(k) GB_JOIN2(Type_, k)
+typedef enum TypeKind {
+#define TYPE_KIND(k, ...) GB_JOIN2(Type_, k)
TYPE_KINDS
#undef TYPE_KIND
-};
+} TypeKind;
String const type_strings[] = {
-#define TYPE_KIND(k) {cast(u8 *)#k, gb_size_of(#k)-1}
+#define TYPE_KIND(k, ...) {cast(u8 *)#k, gb_size_of(#k)-1}
TYPE_KINDS
#undef TYPE_KIND
};
-enum TypeRecordKind {
+typedef enum TypeRecordKind {
TypeRecord_Invalid,
TypeRecord_Struct,
@@ -95,9 +95,9 @@ enum TypeRecordKind {
TypeRecord_Union, // Tagged
TypeRecord_Count,
-};
+} TypeRecordKind;
-struct Type {
+typedef struct Type {
TypeKind kind;
union {
BasicType Basic;
@@ -167,7 +167,42 @@ struct Type {
bool variadic;
} Proc;
};
-};
+} Type;
+
+// NOTE(bill): Internal sizes of certain types
+// string: 2*word_size (ptr+len)
+// slice: 3*word_size (ptr+len+cap)
+// array: count*size_of(elem) aligned
+
+// NOTE(bill): Alignment of structures and other types are to be compatible with C
+
+typedef struct BaseTypeSizes {
+ i64 word_size;
+ i64 max_align;
+} BaseTypeSizes;
+
+typedef Array(isize) Array_isize;
+
+typedef struct Selection {
+ Entity * entity;
+ Array_isize index;
+ bool indirect; // Set if there was a pointer deref anywhere down the line
+} Selection;
+Selection empty_selection = {};
+
+Selection make_selection(Entity *entity, Array_isize index, bool indirect) {
+ Selection s = {entity, index, indirect};
+ return s;
+}
+
+void selection_add_index(Selection *s, isize index) {
+ // IMPORTANT NOTE(bill): this requires a stretchy buffer/dynamic array so it requires some form
+ // of heap allocation
+ if (s->index.e == NULL) {
+ array_init(&s->index, heap_allocator());
+ }
+ array_add(&s->index, index);
+}
@@ -793,40 +828,7 @@ Type *default_type(Type *type) {
}
-// NOTE(bill): Internal sizes of certain types
-// string: 2*word_size (ptr+len)
-// slice: 3*word_size (ptr+len+cap)
-// array: count*size_of(elem) aligned
-
-// NOTE(bill): Alignment of structures and other types are to be compatible with C
-
-struct BaseTypeSizes {
- i64 word_size;
- i64 max_align;
-};
-
-typedef Array(isize) Array_isize;
-
-struct Selection {
- Entity * entity;
- Array_isize index;
- bool indirect; // Set if there was a pointer deref anywhere down the line
-};
-Selection empty_selection = {};
-
-Selection make_selection(Entity *entity, Array_isize index, bool indirect) {
- Selection s = {entity, index, indirect};
- return s;
-}
-void selection_add_index(Selection *s, isize index) {
- // IMPORTANT NOTE(bill): this requires a stretchy buffer/dynamic array so it requires some form
- // of heap allocation
- if (s->index.e == NULL) {
- array_init(&s->index, heap_allocator());
- }
- array_add(&s->index, index);
-}
gb_global Entity *entity__any_type_info = NULL;
gb_global Entity *entity__any_data = NULL;