mirror of
https://github.com/odin-lang/Odin.git
synced 2026-03-01 14:18:19 +00:00
Subtyping Polymorphic arguments; using procedure parameters
This commit is contained in:
@@ -3,26 +3,28 @@
|
||||
#load "game.odin"
|
||||
|
||||
main :: proc() {
|
||||
|
||||
Thing :: type struct {
|
||||
using x: struct { a, b: int }
|
||||
Vec3 :: type struct { x, y, z: f32 }
|
||||
Entity :: type struct {
|
||||
using pos: Vec3
|
||||
name: string
|
||||
}
|
||||
|
||||
{
|
||||
using t := new(Thing)
|
||||
defer delete(t)
|
||||
a = 321
|
||||
print_int(a); nl()
|
||||
Frog :: type struct {
|
||||
using entity: Entity
|
||||
jump_height: f32
|
||||
}
|
||||
|
||||
// {
|
||||
// using t := new(Thing)
|
||||
// defer delete(t)
|
||||
// a = 1337
|
||||
// print_int(a); nl()
|
||||
// }
|
||||
f := Frog{}
|
||||
f.name = "ribbit"
|
||||
|
||||
print_name :: proc(using e: Entity) {
|
||||
print_string(name); nl()
|
||||
}
|
||||
|
||||
print_name(f.entity)
|
||||
print_name(f)
|
||||
|
||||
|
||||
// run_game()
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -93,9 +93,10 @@ Entity *make_entity_type_name(gbAllocator a, Scope *scope, Token token, Type *ty
|
||||
return entity;
|
||||
}
|
||||
|
||||
Entity *make_entity_param(gbAllocator a, Scope *scope, Token token, Type *type) {
|
||||
Entity *make_entity_param(gbAllocator a, Scope *scope, Token token, Type *type, b32 is_anonymous) {
|
||||
Entity *entity = make_entity_variable(a, scope, token, type);
|
||||
entity->Variable.used = true;
|
||||
entity->Variable.anonymous = cast(b8)is_anonymous;
|
||||
return entity;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,28 @@ void update_expr_type (Checker *c, AstNode *e, Type *type, b32 fina
|
||||
|
||||
|
||||
b32 check_is_assignable_to_using_subtype(Checker *c, Type *dst, Type *src) {
|
||||
Type *prev_src = src;
|
||||
// Type *prev_dst = dst;
|
||||
src = get_base_type(type_deref(src));
|
||||
// dst = get_base_type(type_deref(dst));
|
||||
b32 src_is_ptr = src != prev_src;
|
||||
// b32 dst_is_ptr = dst != prev_dst;
|
||||
|
||||
if (src->kind == Type_Struct) {
|
||||
for (isize i = 0; i < src->Struct.field_count; i++) {
|
||||
Entity *f = src->Struct.fields[i];
|
||||
if (f->kind == Entity_Variable && f->Variable.anonymous) {
|
||||
if (are_types_identical(dst, f->type)) {
|
||||
return true;
|
||||
}
|
||||
if (src_is_ptr && is_type_pointer(dst)) {
|
||||
if (are_types_identical(type_deref(dst), f->type)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -68,8 +90,8 @@ b32 check_is_assignable_to(Checker *c, Operand *operand, Type *type, b32 is_argu
|
||||
}
|
||||
|
||||
if (is_argument) {
|
||||
// Polymorphism for subtyping
|
||||
if (check_is_assignable_to_using_subtype(c, dst, src)) {
|
||||
// NOTE(bill): Polymorphism for subtyping
|
||||
if (check_is_assignable_to_using_subtype(c, type, src)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -154,96 +176,27 @@ void populate_using_entity_map(Checker *c, AstNode *node, Type *t, Map<Entity *>
|
||||
|
||||
}
|
||||
|
||||
void check_fields(Checker *c, AstNode *node,
|
||||
AstNode *field_list, Entity **fields, isize field_count,
|
||||
CycleChecker *cycle_checker, String context) {
|
||||
Map<Entity *> entity_map = {};
|
||||
map_init(&entity_map, gb_heap_allocator());
|
||||
defer (map_destroy(&entity_map));
|
||||
|
||||
isize field_index = 0;
|
||||
for (AstNode *field = field_list; field != NULL; field = field->next) {
|
||||
ast_node(f, Field, field);
|
||||
Type *type = check_type(c, f->type, NULL, cycle_checker);
|
||||
|
||||
if (f->is_using) {
|
||||
if (f->name_count > 1) {
|
||||
error(&c->error_collector, ast_node_token(f->name_list),
|
||||
"Cannot apply `using` to more than one of the same type");
|
||||
}
|
||||
}
|
||||
|
||||
for (AstNode *name = f->name_list; name != NULL; name = name->next) {
|
||||
ast_node(i, Ident, name);
|
||||
Token name_token = i->token;
|
||||
|
||||
Entity *e = make_entity_field(c->allocator, c->context.scope, name_token, type, f->is_using);
|
||||
HashKey key = hash_string(name_token.string);
|
||||
if (map_get(&entity_map, key) != NULL) {
|
||||
// TODO(bill): Scope checking already checks the declaration
|
||||
error(&c->error_collector, name_token, "`%.*s` is already declared in this %.*s", LIT(name_token.string), LIT(context));
|
||||
} else {
|
||||
map_set(&entity_map, key, e);
|
||||
fields[field_index++] = e;
|
||||
}
|
||||
add_entity_use(&c->info, name, e);
|
||||
}
|
||||
|
||||
|
||||
if (f->is_using) {
|
||||
Type *t = get_base_type(type_deref(type));
|
||||
if (t->kind != Type_Struct &&
|
||||
t->kind != Type_Union) {
|
||||
Token name_token = f->name_list->Ident.token;
|
||||
error(&c->error_collector, name_token, "`using` on a field `%.*s` must be a structure or union", LIT(name_token.string));
|
||||
continue;
|
||||
}
|
||||
|
||||
populate_using_entity_map(c, node, type, &entity_map);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void check_const_decl(Checker *c, Entity *e, AstNode *type_expr, AstNode *init_expr);
|
||||
|
||||
void check_struct_type(Checker *c, Type *struct_type, AstNode *node, CycleChecker *cycle_checker) {
|
||||
GB_ASSERT(node->kind == AstNode_StructType);
|
||||
GB_ASSERT(struct_type->kind == Type_Struct);
|
||||
ast_node(st, StructType, node);
|
||||
|
||||
isize field_count = 0;
|
||||
isize other_field_count = 0;
|
||||
for (AstNode *decl = st->decl_list; decl != NULL; decl = decl->next) {
|
||||
switch (decl->kind) {
|
||||
case_ast_node(vd, VarDecl, decl);
|
||||
if (vd->kind == Declaration_Mutable) {
|
||||
field_count += vd->name_count;
|
||||
} else {
|
||||
other_field_count += vd->name_count;
|
||||
}
|
||||
case_end;
|
||||
|
||||
case_ast_node(td, TypeDecl, decl);
|
||||
other_field_count += 1;
|
||||
case_end;
|
||||
}
|
||||
}
|
||||
|
||||
Entity **fields = gb_alloc_array(c->allocator, Entity *, field_count);
|
||||
void check_fields(Checker *c, AstNode *node, AstNode *decl_list,
|
||||
Entity **fields, isize field_count,
|
||||
Entity **other_fields, isize other_field_count,
|
||||
CycleChecker *cycle_checker, String context) {
|
||||
|
||||
Map<Entity *> entity_map = {};
|
||||
map_init(&entity_map, gb_heap_allocator());
|
||||
defer (map_destroy(&entity_map));
|
||||
|
||||
Entity **other_fields = gb_alloc_array(c->allocator, Entity *, other_field_count);
|
||||
isize other_field_index = 0;
|
||||
|
||||
|
||||
// TODO(bill): Random declarations with DeclInfo
|
||||
#if 0
|
||||
Entity *e;
|
||||
DeclInfo *d;d
|
||||
check_entity_decl(c, e, d, NULL);
|
||||
#endif
|
||||
for (AstNode *decl = st->decl_list; decl != NULL; decl = decl->next) {
|
||||
for (AstNode *decl = decl_list; decl != NULL; decl = decl->next) {
|
||||
if (decl->kind == AstNode_VarDecl) {
|
||||
ast_node(vd, VarDecl, decl);
|
||||
if (vd->kind != Declaration_Immutable)
|
||||
@@ -312,7 +265,7 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node, CycleChecke
|
||||
}
|
||||
|
||||
isize field_index = 0;
|
||||
for (AstNode *decl = st->decl_list; decl != NULL; decl = decl->next) {
|
||||
for (AstNode *decl = decl_list; decl != NULL; decl = decl->next) {
|
||||
if (decl->kind != AstNode_VarDecl)
|
||||
continue;
|
||||
ast_node(vd, VarDecl, decl);
|
||||
@@ -360,6 +313,39 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node, CycleChecke
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void check_struct_type(Checker *c, Type *struct_type, AstNode *node, CycleChecker *cycle_checker) {
|
||||
GB_ASSERT(node->kind == AstNode_StructType);
|
||||
GB_ASSERT(struct_type->kind == Type_Struct);
|
||||
ast_node(st, StructType, node);
|
||||
|
||||
// TODO(bill): check_struct_type and check_union_type are very similar so why not and try to merge them better
|
||||
|
||||
isize field_count = 0;
|
||||
isize other_field_count = 0;
|
||||
for (AstNode *decl = st->decl_list; decl != NULL; decl = decl->next) {
|
||||
switch (decl->kind) {
|
||||
case_ast_node(vd, VarDecl, decl);
|
||||
if (vd->kind == Declaration_Mutable) {
|
||||
field_count += vd->name_count;
|
||||
} else {
|
||||
other_field_count += vd->name_count;
|
||||
}
|
||||
case_end;
|
||||
|
||||
case_ast_node(td, TypeDecl, decl);
|
||||
other_field_count += 1;
|
||||
case_end;
|
||||
}
|
||||
}
|
||||
|
||||
Entity **fields = gb_alloc_array(c->allocator, Entity *, field_count);
|
||||
Entity **other_fields = gb_alloc_array(c->allocator, Entity *, other_field_count);
|
||||
|
||||
check_fields(c, node, st->decl_list, fields, field_count, other_fields, other_field_count, cycle_checker, make_string("struct"));
|
||||
|
||||
struct_type->Struct.is_packed = st->is_packed;
|
||||
struct_type->Struct.fields = fields;
|
||||
struct_type->Struct.field_count = field_count;
|
||||
@@ -373,17 +359,32 @@ void check_union_type(Checker *c, Type *union_type, AstNode *node, CycleChecker
|
||||
ast_node(ut, UnionType, node);
|
||||
|
||||
isize field_count = 0;
|
||||
for (AstNode *field = ut->field_list; field != NULL; field = field->next) {
|
||||
for (AstNode *name = field->Field.name_list; name != NULL; name = name->next) {
|
||||
GB_ASSERT(name->kind == AstNode_Ident);
|
||||
field_count++;
|
||||
isize other_field_count = 0;
|
||||
for (AstNode *decl = ut->decl_list; decl != NULL; decl = decl->next) {
|
||||
switch (decl->kind) {
|
||||
case_ast_node(vd, VarDecl, decl);
|
||||
if (vd->kind == Declaration_Mutable) {
|
||||
field_count += vd->name_count;
|
||||
} else {
|
||||
other_field_count += vd->name_count;
|
||||
}
|
||||
case_end;
|
||||
|
||||
case_ast_node(td, TypeDecl, decl);
|
||||
other_field_count += 1;
|
||||
case_end;
|
||||
}
|
||||
}
|
||||
|
||||
Entity **fields = gb_alloc_array(c->allocator, Entity *, field_count);
|
||||
check_fields(c, node, ut->field_list, fields, field_count, cycle_checker, make_string("union"));
|
||||
Entity **other_fields = gb_alloc_array(c->allocator, Entity *, other_field_count);
|
||||
|
||||
check_fields(c, node, ut->decl_list, fields, field_count, other_fields, other_field_count, cycle_checker, make_string("union"));
|
||||
|
||||
union_type->Union.fields = fields;
|
||||
union_type->Union.field_count = field_count;
|
||||
union_type->Union.other_fields = other_fields;
|
||||
union_type->Union.other_field_count = other_field_count;
|
||||
}
|
||||
|
||||
|
||||
@@ -470,7 +471,7 @@ Type *check_get_params(Checker *c, Scope *scope, AstNode *field_list, isize fiel
|
||||
for (AstNode *name = f->name_list; name != NULL; name = name->next) {
|
||||
if (name->kind == AstNode_Ident) {
|
||||
ast_node(i, Ident, name);
|
||||
Entity *param = make_entity_param(c->allocator, scope, i->token, type);
|
||||
Entity *param = make_entity_param(c->allocator, scope, i->token, type, f->is_using);
|
||||
add_entity(c, scope, name, param);
|
||||
variables[variable_index++] = param;
|
||||
} else {
|
||||
@@ -497,7 +498,7 @@ Type *check_get_results(Checker *c, Scope *scope, AstNode *list, isize list_coun
|
||||
Token token = ast_node_token(item);
|
||||
token.string = make_string(""); // NOTE(bill): results are not named
|
||||
// TODO(bill): Should I have named results?
|
||||
Entity *param = make_entity_param(c->allocator, scope, token, type);
|
||||
Entity *param = make_entity_param(c->allocator, scope, token, type, false);
|
||||
// NOTE(bill): No need to record
|
||||
variables[variable_index++] = param;
|
||||
}
|
||||
|
||||
@@ -296,6 +296,39 @@ void check_proc_body(Checker *c, Token token, DeclInfo *decl, Type *type, AstNod
|
||||
c->context.scope = decl->scope;
|
||||
c->context.decl = decl;
|
||||
|
||||
GB_ASSERT(type->kind == Type_Proc);
|
||||
if (type->Proc.param_count > 0) {
|
||||
auto *params = &type->Proc.params->Tuple;
|
||||
for (isize i = 0; i < params->variable_count; i++) {
|
||||
Entity *e = params->variables[i];
|
||||
GB_ASSERT(e->kind == Entity_Variable);
|
||||
if (!e->Variable.anonymous)
|
||||
continue;
|
||||
String name = e->token.string;
|
||||
Type *t = get_base_type(type_deref(e->type));
|
||||
if (t->kind == Type_Struct || t->kind == Type_Union) {
|
||||
// IMPORTANT HACK(bill): Entity_(Struct|Union) overlap in some memory allowing
|
||||
// for some variables to accessed to same
|
||||
Scope **found = map_get(&c->info.scopes, hash_pointer(t->Struct.node));
|
||||
GB_ASSERT(found != NULL);
|
||||
gb_for_array(i, (*found)->elements.entries) {
|
||||
Entity *f = (*found)->elements.entries[i].value;
|
||||
if (f->kind == Entity_Variable) {
|
||||
Entity *uvar = make_entity_using_variable(c->allocator, e, f->token, f->type);
|
||||
Entity *prev = scope_insert_entity(c->context.scope, uvar);
|
||||
if (prev != NULL) {
|
||||
error(&c->error_collector, e->token, "Namespace collision while `using` `%.*s` of: %.*s", LIT(name), LIT(prev->token.string));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
error(&c->error_collector, e->token, "`using` can only be applied to variables of type struct or union");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
push_procedure(c, type);
|
||||
ast_node(bs, BlockStmt, body);
|
||||
// TODO(bill): Check declarations first (except mutable variable declarations)
|
||||
@@ -789,6 +822,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
||||
case_ast_node(us, UsingStmt, node);
|
||||
switch (us->node->kind) {
|
||||
case_ast_node(es, ExprStmt, us->node);
|
||||
// TODO(bill): Allow for just a LHS expression list rather than this silly code
|
||||
Entity *e = NULL;
|
||||
|
||||
b32 is_selector = false;
|
||||
@@ -889,40 +923,41 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
|
||||
} break;
|
||||
|
||||
default:
|
||||
GB_PANIC("TODO(bill): using Ident");
|
||||
GB_PANIC("TODO(bill): using other expressions?");
|
||||
}
|
||||
case_end;
|
||||
|
||||
case_ast_node(vd, VarDecl, us->node);
|
||||
if (vd->name_count > 1) {
|
||||
if (vd->name_count > 1 && vd->type != NULL) {
|
||||
error(&c->error_collector, us->token, "`using` can only be applied to one variable of the same type");
|
||||
}
|
||||
check_var_decl(c, us->node);
|
||||
ast_node(i, Ident, vd->name_list);
|
||||
|
||||
String name = i->token.string;
|
||||
Entity *e = scope_lookup_entity(c, c->context.scope, name);
|
||||
|
||||
Type *t = get_base_type(type_deref(e->type));
|
||||
if (t->kind == Type_Struct || t->kind == Type_Union) {
|
||||
// IMPORTANT HACK(bill): Entity_(Struct|Union) overlap in some memory allowing
|
||||
// for some variables to accessed to same
|
||||
Scope **found = map_get(&c->info.scopes, hash_pointer(t->Struct.node));
|
||||
GB_ASSERT(found != NULL);
|
||||
gb_for_array(i, (*found)->elements.entries) {
|
||||
Entity *f = (*found)->elements.entries[i].value;
|
||||
if (f->kind == Entity_Variable) {
|
||||
Entity *uvar = make_entity_using_variable(c->allocator, e, f->token, f->type);
|
||||
Entity *prev = scope_insert_entity(c->context.scope, uvar);
|
||||
if (prev != NULL) {
|
||||
error(&c->error_collector, us->token, "Namespace collision while `using` `%.*s` of: %.*s", LIT(name), LIT(prev->token.string));
|
||||
return;
|
||||
for (AstNode *item = vd->name_list; item != NULL; item = item->next) {
|
||||
ast_node(i, Ident, item);
|
||||
String name = i->token.string;
|
||||
Entity *e = scope_lookup_entity(c, c->context.scope, name);
|
||||
Type *t = get_base_type(type_deref(e->type));
|
||||
if (t->kind == Type_Struct || t->kind == Type_Union) {
|
||||
// IMPORTANT HACK(bill): Entity_(Struct|Union) overlap in some memory allowing
|
||||
// for some variables to accessed to same
|
||||
Scope **found = map_get(&c->info.scopes, hash_pointer(t->Struct.node));
|
||||
GB_ASSERT(found != NULL);
|
||||
gb_for_array(i, (*found)->elements.entries) {
|
||||
Entity *f = (*found)->elements.entries[i].value;
|
||||
if (f->kind == Entity_Variable) {
|
||||
Entity *uvar = make_entity_using_variable(c->allocator, e, f->token, f->type);
|
||||
Entity *prev = scope_insert_entity(c->context.scope, uvar);
|
||||
if (prev != NULL) {
|
||||
error(&c->error_collector, us->token, "Namespace collision while `using` `%.*s` of: %.*s", LIT(name), LIT(prev->token.string));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
error(&c->error_collector, us->token, "`using` can only be applied to variables of type struct or union");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
error(&c->error_collector, us->token, "`using` can only be applied to variables of type struct or union");
|
||||
return;
|
||||
}
|
||||
case_end;
|
||||
|
||||
|
||||
@@ -107,21 +107,21 @@ struct Type {
|
||||
// Theses are arrays
|
||||
Entity **fields; // Entity_Variable
|
||||
isize field_count; // == offset_count
|
||||
Entity **other_fields; // Entity_Constant or Entity_TypeName
|
||||
isize other_field_count;
|
||||
AstNode *node;
|
||||
|
||||
i64 * offsets;
|
||||
b32 are_offsets_set;
|
||||
b32 is_packed;
|
||||
|
||||
Entity **other_fields; // Entity_Constant or Entity_TypeName
|
||||
isize other_field_count;
|
||||
|
||||
} Struct;
|
||||
struct {
|
||||
// IMPORTANT HACK(bill): The positions of fields, field_count, and node
|
||||
// must be same for Struct and Union
|
||||
Entity **fields; // Entity_Variable
|
||||
isize field_count;
|
||||
Entity **other_fields; // Entity_Constant or Entity_TypeName
|
||||
isize other_field_count;
|
||||
AstNode *node;
|
||||
} Union;
|
||||
struct { Type *elem; } Pointer;
|
||||
|
||||
@@ -440,7 +440,7 @@ void ssa_set_type(ssaValue *value, Type *type) {
|
||||
ssaValue *ssa_build_expr(ssaProcedure *proc, AstNode *expr);
|
||||
ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue *tv);
|
||||
ssaAddr ssa_build_addr(ssaProcedure *proc, AstNode *expr);
|
||||
ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *a_type);
|
||||
ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *a_type, b32 is_argument = false);
|
||||
ssaValue *ssa_emit_transmute(ssaProcedure *proc, ssaValue *value, Type *a_type);
|
||||
void ssa_build_proc(ssaValue *value, ssaProcedure *parent);
|
||||
|
||||
@@ -1050,6 +1050,70 @@ ssaValue *ssa_emit_struct_ev(ssaProcedure *proc, ssaValue *s, i32 index, Type *r
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_emit_deep_field_gep(ssaProcedure *proc, Type *type, ssaValue *e, Selection sel) {
|
||||
GB_ASSERT(gb_array_count(sel.index) > 0);
|
||||
|
||||
gb_for_array(i, sel.index) {
|
||||
isize index = sel.index[i];
|
||||
if (is_type_pointer(type)) {
|
||||
type = type_deref(type);
|
||||
e = ssa_emit_load(proc, e);
|
||||
e = ssa_emit_ptr_offset(proc, e, v_zero);
|
||||
ssa_set_type(e, type);
|
||||
}
|
||||
type = get_base_type(type);
|
||||
|
||||
|
||||
if (type->kind == Type_Union) {
|
||||
ssaValue *v = ssa_emit_ptr_offset(proc, e, v_zero);
|
||||
ssa_set_type(v, make_type_pointer(proc->module->allocator, type));
|
||||
type = type->Union.fields[index]->type;
|
||||
e = ssa_emit_conv(proc, v, make_type_pointer(proc->module->allocator, type));
|
||||
e = ssa_emit_ptr_offset(proc, e, v_zero);
|
||||
ssa_set_type(e, type);
|
||||
} else {
|
||||
type = type->Union.fields[index]->type;
|
||||
e = ssa_emit_struct_gep(proc, e, index, type);
|
||||
}
|
||||
}
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_emit_deep_field_ev(ssaProcedure *proc, Type *type, ssaValue *e, Selection sel) {
|
||||
GB_ASSERT(gb_array_count(sel.index) > 0);
|
||||
|
||||
gb_for_array(i, sel.index) {
|
||||
isize index = sel.index[i];
|
||||
if (is_type_pointer(type)) {
|
||||
type = type_deref(type);
|
||||
e = ssa_emit_load(proc, e);
|
||||
e = ssa_emit_ptr_offset(proc, e, v_zero);
|
||||
ssa_set_type(e, type);
|
||||
}
|
||||
type = get_base_type(type);
|
||||
|
||||
|
||||
if (type->kind == Type_Union) {
|
||||
ssaValue *v = ssa_emit_ptr_offset(proc, e, v_zero);
|
||||
ssa_set_type(v, make_type_pointer(proc->module->allocator, type));
|
||||
type = type->Union.fields[index]->type;
|
||||
e = ssa_emit_conv(proc, v, make_type_pointer(proc->module->allocator, type));
|
||||
e = ssa_emit_ptr_offset(proc, e, v_zero);
|
||||
ssa_set_type(e, type);
|
||||
} else {
|
||||
type = type->Union.fields[index]->type;
|
||||
e = ssa_emit_struct_ev(proc, e, index, type);
|
||||
}
|
||||
}
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
ssaValue *ssa_array_elem(ssaProcedure *proc, ssaValue *array) {
|
||||
Type *t = ssa_type(array);
|
||||
@@ -1227,7 +1291,7 @@ ssaValue *ssa_emit_string(ssaProcedure *proc, ssaValue *elem, ssaValue *len) {
|
||||
|
||||
|
||||
|
||||
ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) {
|
||||
ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t, b32 is_argument) {
|
||||
Type *src_type = ssa_type(value);
|
||||
if (are_types_identical(t, src_type)) {
|
||||
return value;
|
||||
@@ -1323,11 +1387,48 @@ ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) {
|
||||
return ssa_emit(proc, ssa_make_instr_conv(proc, ssaConv_inttoptr, value, src, dst));
|
||||
}
|
||||
|
||||
|
||||
// NOTE(bill): This has to be done beofre `Pointer <-> Pointer` as it's
|
||||
// subtype polymorphism casting
|
||||
if (is_argument) {
|
||||
Type *sb = get_base_type(type_deref(src));
|
||||
b32 src_is_ptr = src != sb;
|
||||
if (sb->kind == Type_Struct) {
|
||||
// NOTE(bill): Check
|
||||
String field_name = {};
|
||||
for (isize i = 0; i < sb->Struct.field_count; i++) {
|
||||
Entity *f = sb->Struct.fields[i];
|
||||
if (f->kind == Entity_Variable && f->Variable.anonymous) {
|
||||
if (are_types_identical(t, f->type)) {
|
||||
field_name = f->token.string;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (field_name.len > 0) {
|
||||
// NOTE(bill): It can be casted
|
||||
Selection sel = lookup_field(sb, field_name, Addressing_Variable);
|
||||
if (sel.entity != NULL) {
|
||||
if (src_is_ptr) {
|
||||
value = ssa_emit_load(proc, value);
|
||||
ssa_set_type(value, type_deref(src));
|
||||
}
|
||||
return ssa_emit_deep_field_ev(proc, sb, value, sel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Pointer <-> Pointer
|
||||
if (is_type_pointer(src) && is_type_pointer(dst)) {
|
||||
return ssa_emit(proc, ssa_make_instr_conv(proc, ssaConv_bitcast, value, src, dst));
|
||||
}
|
||||
|
||||
|
||||
|
||||
// proc <-> proc
|
||||
if (is_type_proc(src) && is_type_proc(dst)) {
|
||||
return ssa_emit(proc, ssa_make_instr_conv(proc, ssaConv_bitcast, value, src, dst));
|
||||
@@ -1381,6 +1482,7 @@ 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));
|
||||
|
||||
@@ -1725,7 +1827,7 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
|
||||
args[1] = elem_align;
|
||||
|
||||
ssaValue *call = ssa_emit(proc, ssa_make_instr_call(proc, alloc_align_proc, args, 2, t_rawptr));
|
||||
ssaValue *ptr = ssa_emit_conv(proc, call, ptr_type);
|
||||
ssaValue *ptr = ssa_emit_conv(proc, call, ptr_type, true);
|
||||
ssaValue *slice = ssa_add_local_generated(proc, slice_type);
|
||||
ssa_emit_store(proc, ssa_emit_struct_gep(proc, slice, v_zero32, ptr_type), ptr);
|
||||
ssa_emit_store(proc, ssa_emit_struct_gep(proc, slice, v_one32, t_int), len);
|
||||
@@ -1748,7 +1850,7 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
|
||||
}
|
||||
|
||||
ssaValue **args = gb_alloc_array(allocator, ssaValue *, 1);
|
||||
args[0] = ssa_emit_conv(proc, value, t_rawptr);
|
||||
args[0] = ssa_emit_conv(proc, value, t_rawptr, true);
|
||||
|
||||
return ssa_emit(proc, ssa_make_instr_call(proc, dealloc_proc, args, 1, NULL));
|
||||
} break;
|
||||
@@ -1786,8 +1888,8 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
|
||||
ssa_emit_store(proc, d, dst_slice);
|
||||
ssa_emit_store(proc, s, src_slice);
|
||||
|
||||
ssaValue *dst = ssa_emit_conv(proc, ssa_slice_elem(proc, d), t_rawptr);
|
||||
ssaValue *src = ssa_emit_conv(proc, ssa_slice_elem(proc, s), t_rawptr);
|
||||
ssaValue *dst = ssa_emit_conv(proc, ssa_slice_elem(proc, d), t_rawptr, true);
|
||||
ssaValue *src = ssa_emit_conv(proc, ssa_slice_elem(proc, s), t_rawptr, true);
|
||||
|
||||
ssaValue *len_dst = ssa_slice_len(proc, d);
|
||||
ssaValue *len_src = ssa_slice_len(proc, s);
|
||||
@@ -1820,7 +1922,7 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
|
||||
Type *elem_type = type_deref(ssa_type(elem));
|
||||
|
||||
ssaValue *item_value = ssa_build_expr(proc, item_node);
|
||||
item_value = ssa_emit_conv(proc, item_value, elem_type);
|
||||
item_value = ssa_emit_conv(proc, item_value, elem_type, true);
|
||||
|
||||
ssaValue *item = ssa_add_local_generated(proc, elem_type);
|
||||
ssa_emit_store(proc, item, item_value);
|
||||
@@ -1840,10 +1942,10 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
|
||||
i64 item_size = type_size_of(proc->module->sizes, proc->module->allocator, elem_type);
|
||||
ssaValue *byte_count = ssa_make_value_constant(proc->module->allocator, t_int,
|
||||
make_exact_value_integer(item_size));
|
||||
offset = ssa_emit_conv(proc, offset, t_rawptr);
|
||||
offset = ssa_emit_conv(proc, offset, t_rawptr, true);
|
||||
item = ssa_emit_ptr_offset(proc, item, v_zero);
|
||||
ssa_set_type(item, make_type_pointer(proc->module->allocator, ssa_type(item)));
|
||||
item = ssa_emit_conv(proc, item, t_rawptr);
|
||||
item = ssa_emit_conv(proc, item, t_rawptr, true);
|
||||
ssa_emit(proc, ssa_make_instr_copy_memory(proc, offset, item, byte_count, 1, false));
|
||||
|
||||
// Increment slice length
|
||||
@@ -1856,7 +1958,7 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
|
||||
gb_array_append(proc->blocks, done);
|
||||
proc->curr_block = done;
|
||||
|
||||
return ssa_emit_conv(proc, cond, t_bool);
|
||||
return ssa_emit_conv(proc, cond, t_bool, true);
|
||||
} break;
|
||||
|
||||
case BuiltinProc_swizzle: {
|
||||
@@ -1908,11 +2010,11 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
|
||||
ssaValue *len = ssa_build_expr(proc, ce->arg_list->next);
|
||||
ssaValue *cap = len;
|
||||
|
||||
len = ssa_emit_conv(proc, len, t_int);
|
||||
len = ssa_emit_conv(proc, len, t_int, true);
|
||||
|
||||
if (ce->arg_list->next->next != NULL) {
|
||||
cap = ssa_build_expr(proc, ce->arg_list->next->next);
|
||||
cap = ssa_emit_conv(proc, cap, t_int);
|
||||
cap = ssa_emit_conv(proc, cap, t_int, true);
|
||||
}
|
||||
|
||||
|
||||
@@ -1954,7 +2056,7 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
|
||||
|
||||
auto *pt = &proc_type_->Proc.params->Tuple;
|
||||
for (isize i = 0; i < arg_count; i++) {
|
||||
args[i] = ssa_emit_conv(proc, args[i], pt->variables[i]->type);
|
||||
args[i] = ssa_emit_conv(proc, args[i], pt->variables[i]->type, true);
|
||||
}
|
||||
|
||||
ssaValue *call = ssa_make_instr_call(proc, value, args, arg_count, type->results);
|
||||
@@ -2003,36 +2105,6 @@ ssaValue *ssa_build_expr(ssaProcedure *proc, AstNode *expr) {
|
||||
return value;
|
||||
}
|
||||
|
||||
ssaValue *ssa_emit_deep_field_gep(ssaProcedure *proc, Type *type, ssaValue *e, Selection sel) {
|
||||
GB_ASSERT(gb_array_count(sel.index) > 0);
|
||||
|
||||
gb_for_array(i, sel.index) {
|
||||
isize index = sel.index[i];
|
||||
if (is_type_pointer(type)) {
|
||||
type = type_deref(type);
|
||||
e = ssa_emit_load(proc, e);
|
||||
e = ssa_emit_ptr_offset(proc, e, v_zero);
|
||||
ssa_set_type(e, type);
|
||||
}
|
||||
type = get_base_type(type);
|
||||
|
||||
|
||||
if (type->kind == Type_Union) {
|
||||
ssaValue *v = ssa_emit_ptr_offset(proc, e, v_zero);
|
||||
ssa_set_type(v, make_type_pointer(proc->module->allocator, type));
|
||||
type = type->Union.fields[index]->type;
|
||||
e = ssa_emit_conv(proc, v, make_type_pointer(proc->module->allocator, type));
|
||||
e = ssa_emit_ptr_offset(proc, e, v_zero);
|
||||
ssa_set_type(e, type);
|
||||
} else {
|
||||
type = type->Union.fields[index]->type;
|
||||
e = ssa_emit_struct_gep(proc, e, index, type);
|
||||
}
|
||||
}
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_add_using_variable(ssaProcedure *proc, Entity *e) {
|
||||
GB_ASSERT(e->kind == Entity_UsingVariable);
|
||||
|
||||
300
src/parser.cpp
300
src/parser.cpp
@@ -91,94 +91,94 @@ enum CallExprKind {
|
||||
};
|
||||
|
||||
#define AST_NODE_KINDS \
|
||||
AST_NODE_KIND(Invalid, struct{}) \
|
||||
AST_NODE_KIND(BasicLit, Token) \
|
||||
AST_NODE_KIND(Ident, struct { \
|
||||
AST_NODE_KIND(Invalid, "invalid node", struct{}) \
|
||||
AST_NODE_KIND(BasicLit, "basic literal", Token) \
|
||||
AST_NODE_KIND(Ident, "identifier", struct { \
|
||||
Token token; \
|
||||
AstEntity *entity; \
|
||||
}) \
|
||||
AST_NODE_KIND(ProcLit, struct { \
|
||||
AST_NODE_KIND(ProcLit, "procedure literal", struct { \
|
||||
AstNode *type; \
|
||||
AstNode *body; \
|
||||
u64 tags; \
|
||||
}) \
|
||||
AST_NODE_KIND(CompoundLit, struct { \
|
||||
AST_NODE_KIND(CompoundLit, "compound literal", struct { \
|
||||
AstNode *type; \
|
||||
AstNode *elem_list; \
|
||||
isize elem_count; \
|
||||
Token open, close; \
|
||||
}) \
|
||||
AST_NODE_KIND(_ExprBegin, struct{}) \
|
||||
AST_NODE_KIND(BadExpr, struct { Token begin, end; }) \
|
||||
AST_NODE_KIND(TagExpr, struct { Token token, name; AstNode *expr; }) \
|
||||
AST_NODE_KIND(UnaryExpr, struct { Token op; AstNode *expr; }) \
|
||||
AST_NODE_KIND(BinaryExpr, struct { Token op; AstNode *left, *right; } ) \
|
||||
AST_NODE_KIND(ParenExpr, struct { AstNode *expr; Token open, close; }) \
|
||||
AST_NODE_KIND(SelectorExpr, struct { Token token; AstNode *expr, *selector; }) \
|
||||
AST_NODE_KIND(IndexExpr, struct { AstNode *expr, *index; Token open, close; }) \
|
||||
AST_NODE_KIND(DerefExpr, struct { Token op; AstNode *expr; }) \
|
||||
AST_NODE_KIND(CallExpr, struct { \
|
||||
AST_NODE_KIND(_ExprBegin, "", struct{}) \
|
||||
AST_NODE_KIND(BadExpr, "bad expression", struct { Token begin, end; }) \
|
||||
AST_NODE_KIND(TagExpr, "tag expression", struct { Token token, name; AstNode *expr; }) \
|
||||
AST_NODE_KIND(UnaryExpr, "unary expression", struct { Token op; AstNode *expr; }) \
|
||||
AST_NODE_KIND(BinaryExpr, "binary expression", struct { Token op; AstNode *left, *right; } ) \
|
||||
AST_NODE_KIND(ParenExpr, "parentheses expression", struct { AstNode *expr; Token open, close; }) \
|
||||
AST_NODE_KIND(SelectorExpr, "selector expression", struct { Token token; AstNode *expr, *selector; }) \
|
||||
AST_NODE_KIND(IndexExpr, "index expression", struct { AstNode *expr, *index; Token open, close; }) \
|
||||
AST_NODE_KIND(DerefExpr, "dereference expression", struct { Token op; AstNode *expr; }) \
|
||||
AST_NODE_KIND(CallExpr, "call expression", struct { \
|
||||
AstNode *proc, *arg_list; \
|
||||
isize arg_list_count; \
|
||||
Token open, close; \
|
||||
CallExprKind kind; \
|
||||
}) \
|
||||
AST_NODE_KIND(SliceExpr, struct { \
|
||||
AST_NODE_KIND(SliceExpr, "slice expression", struct { \
|
||||
AstNode *expr; \
|
||||
Token open, close; \
|
||||
AstNode *low, *high, *max; \
|
||||
b32 triple_indexed; \
|
||||
}) \
|
||||
AST_NODE_KIND(FieldValue, struct { Token eq; AstNode *field, *value; }) \
|
||||
AST_NODE_KIND(Ellipsis, struct { Token token; }) \
|
||||
AST_NODE_KIND(_ExprEnd, struct{}) \
|
||||
AST_NODE_KIND(_StmtBegin, struct{}) \
|
||||
AST_NODE_KIND(BadStmt, struct { Token begin, end; }) \
|
||||
AST_NODE_KIND(EmptyStmt, struct { Token token; }) \
|
||||
AST_NODE_KIND(ExprStmt, struct { AstNode *expr; } ) \
|
||||
AST_NODE_KIND(IncDecStmt, struct { Token op; AstNode *expr; }) \
|
||||
AST_NODE_KIND(TagStmt, struct { \
|
||||
AST_NODE_KIND(FieldValue, "field value", struct { Token eq; AstNode *field, *value; }) \
|
||||
AST_NODE_KIND(Ellipsis, "ellipsis", struct { Token token; }) \
|
||||
AST_NODE_KIND(_ExprEnd, "", struct{}) \
|
||||
AST_NODE_KIND(_StmtBegin, "", struct{}) \
|
||||
AST_NODE_KIND(BadStmt, "bad statement", struct { Token begin, end; }) \
|
||||
AST_NODE_KIND(EmptyStmt, "empty statement", struct { Token token; }) \
|
||||
AST_NODE_KIND(ExprStmt, "expression statement", struct { AstNode *expr; } ) \
|
||||
AST_NODE_KIND(IncDecStmt, "increment/decrement statement", struct { Token op; AstNode *expr; }) \
|
||||
AST_NODE_KIND(TagStmt, "tag statement", struct { \
|
||||
Token token; \
|
||||
Token name; \
|
||||
AstNode *stmt; \
|
||||
}) \
|
||||
AST_NODE_KIND(AssignStmt, struct { \
|
||||
AST_NODE_KIND(AssignStmt, "assign statement", struct { \
|
||||
Token op; \
|
||||
AstNode *lhs_list, *rhs_list; \
|
||||
isize lhs_count, rhs_count; \
|
||||
}) \
|
||||
AST_NODE_KIND(_ComplexStmtBegin, struct{}) \
|
||||
AST_NODE_KIND(BlockStmt, struct { \
|
||||
AST_NODE_KIND(_ComplexStmtBegin, "", struct{}) \
|
||||
AST_NODE_KIND(BlockStmt, "block statement", struct { \
|
||||
AstNode *list; \
|
||||
isize list_count; \
|
||||
Token open, close; \
|
||||
}) \
|
||||
AST_NODE_KIND(IfStmt, struct { \
|
||||
AST_NODE_KIND(IfStmt, "if statement", struct { \
|
||||
Token token; \
|
||||
AstNode *init; \
|
||||
AstNode *cond; \
|
||||
AstNode *body; \
|
||||
AstNode *else_stmt; \
|
||||
}) \
|
||||
AST_NODE_KIND(ReturnStmt, struct { \
|
||||
AST_NODE_KIND(ReturnStmt, "return statement", struct { \
|
||||
Token token; \
|
||||
AstNode *result_list; \
|
||||
isize result_count; \
|
||||
}) \
|
||||
AST_NODE_KIND(ForStmt, struct { \
|
||||
AST_NODE_KIND(ForStmt, "for statement", struct { \
|
||||
Token token; \
|
||||
AstNode *init, *cond, *post; \
|
||||
AstNode *body; \
|
||||
}) \
|
||||
AST_NODE_KIND(DeferStmt, struct { Token token; AstNode *stmt; }) \
|
||||
AST_NODE_KIND(BranchStmt, struct { Token token; }) \
|
||||
AST_NODE_KIND(UsingStmt, struct { Token token; AstNode *node; }) \
|
||||
AST_NODE_KIND(DeferStmt, "defer statement", struct { Token token; AstNode *stmt; }) \
|
||||
AST_NODE_KIND(BranchStmt, "branch statement", struct { Token token; }) \
|
||||
AST_NODE_KIND(UsingStmt, "using statement", struct { Token token; AstNode *node; }) \
|
||||
\
|
||||
AST_NODE_KIND(_ComplexStmtEnd, struct{}) \
|
||||
AST_NODE_KIND(_StmtEnd, struct{}) \
|
||||
AST_NODE_KIND(_DeclBegin, struct{}) \
|
||||
AST_NODE_KIND(BadDecl, struct { Token begin, end; }) \
|
||||
AST_NODE_KIND(VarDecl, struct { \
|
||||
AST_NODE_KIND(_ComplexStmtEnd, "", struct{}) \
|
||||
AST_NODE_KIND(_StmtEnd, "", struct{}) \
|
||||
AST_NODE_KIND(_DeclBegin, "", struct{}) \
|
||||
AST_NODE_KIND(BadDecl, "bad declaration", struct { Token begin, end; }) \
|
||||
AST_NODE_KIND(VarDecl, "variable declaration", struct { \
|
||||
DeclKind kind; \
|
||||
u32 tags; \
|
||||
b32 is_using; \
|
||||
@@ -187,73 +187,73 @@ AST_NODE_KIND(_DeclBegin, struct{}) \
|
||||
AstNode *value_list; \
|
||||
isize name_count, value_count; \
|
||||
}) \
|
||||
AST_NODE_KIND(ProcDecl, struct { \
|
||||
AstNode *name; \
|
||||
AstNode *type; \
|
||||
AstNode *body; \
|
||||
u64 tags; \
|
||||
String foreign_name; \
|
||||
AST_NODE_KIND(ProcDecl, "procedure declaration", struct { \
|
||||
AstNode *name; \
|
||||
AstNode *type; \
|
||||
AstNode *body; \
|
||||
u64 tags; \
|
||||
String foreign_name; \
|
||||
}) \
|
||||
AST_NODE_KIND(TypeDecl, struct { Token token; AstNode *name, *type; }) \
|
||||
AST_NODE_KIND(LoadDecl, struct { Token token, filepath; }) \
|
||||
AST_NODE_KIND(ForeignSystemLibrary, struct { Token token, filepath; }) \
|
||||
AST_NODE_KIND(_DeclEnd, struct{}) \
|
||||
AST_NODE_KIND(_TypeBegin, struct{}) \
|
||||
AST_NODE_KIND(Field, struct { \
|
||||
AST_NODE_KIND(TypeDecl, "type declaration", struct { Token token; AstNode *name, *type; }) \
|
||||
AST_NODE_KIND(LoadDecl, "load declaration", struct { Token token, filepath; }) \
|
||||
AST_NODE_KIND(ForeignSystemLibrary, "foreign system library", struct { Token token, filepath; }) \
|
||||
AST_NODE_KIND(_DeclEnd, "", struct{}) \
|
||||
AST_NODE_KIND(_TypeBegin, "", struct{}) \
|
||||
AST_NODE_KIND(Field, "field", struct { \
|
||||
AstNode *name_list; \
|
||||
isize name_count; \
|
||||
AstNode *type; \
|
||||
b32 is_using; \
|
||||
}) \
|
||||
AST_NODE_KIND(ProcType, struct { \
|
||||
AST_NODE_KIND(ProcType, "procedure type", struct { \
|
||||
Token token; \
|
||||
AstNode *param_list; \
|
||||
AstNode *result_list; \
|
||||
isize param_count; \
|
||||
isize result_count; \
|
||||
}) \
|
||||
AST_NODE_KIND(PointerType, struct { \
|
||||
AST_NODE_KIND(PointerType, "pointer type", struct { \
|
||||
Token token; \
|
||||
AstNode *type; \
|
||||
}) \
|
||||
AST_NODE_KIND(ArrayType, struct { \
|
||||
AST_NODE_KIND(ArrayType, "array type", struct { \
|
||||
Token token; \
|
||||
AstNode *count; \
|
||||
AstNode *elem; \
|
||||
}) \
|
||||
AST_NODE_KIND(VectorType, struct { \
|
||||
AST_NODE_KIND(VectorType, "vector type", struct { \
|
||||
Token token; \
|
||||
AstNode *count; \
|
||||
AstNode *elem; \
|
||||
}) \
|
||||
AST_NODE_KIND(StructType, struct { \
|
||||
AST_NODE_KIND(StructType, "struct type", struct { \
|
||||
Token token; \
|
||||
AstNode *decl_list; \
|
||||
isize decl_count; \
|
||||
b32 is_packed; \
|
||||
}) \
|
||||
AST_NODE_KIND(UnionType, struct { \
|
||||
AST_NODE_KIND(UnionType, "union type", struct { \
|
||||
Token token; \
|
||||
AstNode *field_list; \
|
||||
isize field_count; \
|
||||
AstNode *decl_list; \
|
||||
isize decl_count; \
|
||||
}) \
|
||||
AST_NODE_KIND(EnumType, struct { \
|
||||
AST_NODE_KIND(EnumType, "enum type", struct { \
|
||||
Token token; \
|
||||
AstNode *base_type; \
|
||||
AstNode *field_list; \
|
||||
isize field_count; \
|
||||
}) \
|
||||
AST_NODE_KIND(_TypeEnd, struct{}) \
|
||||
AST_NODE_KIND(Count, struct{})
|
||||
AST_NODE_KIND(_TypeEnd, "", struct{}) \
|
||||
AST_NODE_KIND(Count, "", struct{})
|
||||
|
||||
enum AstNodeKind {
|
||||
#define AST_NODE_KIND(name, ...) GB_JOIN2(AstNode_, name),
|
||||
#define AST_NODE_KIND(_kind_name_, ...) GB_JOIN2(AstNode_, _kind_name_),
|
||||
AST_NODE_KINDS
|
||||
#undef AST_NODE_KIND
|
||||
};
|
||||
|
||||
String const ast_node_strings[] = {
|
||||
#define AST_NODE_KIND(name, ...) {cast(u8 *)#name, gb_size_of(#name)-1},
|
||||
#define AST_NODE_KIND(_kind_name_, name, ...) {cast(u8 *)name, gb_size_of(name)-1},
|
||||
AST_NODE_KINDS
|
||||
#undef AST_NODE_KIND
|
||||
};
|
||||
@@ -262,7 +262,7 @@ struct AstNode {
|
||||
AstNodeKind kind;
|
||||
AstNode *prev, *next; // NOTE(bill): allow for Linked list
|
||||
union {
|
||||
#define AST_NODE_KIND(_kind_name_, ...) __VA_ARGS__ _kind_name_;
|
||||
#define AST_NODE_KIND(_kind_name_, name, ...) __VA_ARGS__ _kind_name_;
|
||||
AST_NODE_KINDS
|
||||
#undef AST_NODE_KIND
|
||||
};
|
||||
@@ -804,11 +804,11 @@ gb_inline AstNode *make_struct_type(AstFile *f, Token token, AstNode *decl_list,
|
||||
return result;
|
||||
}
|
||||
|
||||
gb_inline AstNode *make_union_type(AstFile *f, Token token, AstNode *field_list, isize field_count) {
|
||||
gb_inline AstNode *make_union_type(AstFile *f, Token token, AstNode *decl_list, isize decl_count) {
|
||||
AstNode *result = make_node(f, AstNode_UnionType);
|
||||
result->UnionType.token = token;
|
||||
result->UnionType.field_list = field_list;
|
||||
result->UnionType.field_count = field_count;
|
||||
result->UnionType.decl_list = decl_list;
|
||||
result->UnionType.decl_count = decl_count;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -962,21 +962,21 @@ void fix_advance_to_next_stmt(AstFile *f) {
|
||||
}
|
||||
|
||||
b32 expect_semicolon_after_stmt(AstFile *f, AstNode *s) {
|
||||
if (s != NULL) {
|
||||
switch (s->kind) {
|
||||
case AstNode_ProcDecl:
|
||||
return true;
|
||||
case AstNode_TypeDecl: {
|
||||
switch (s->TypeDecl.type->kind) {
|
||||
case AstNode_StructType:
|
||||
case AstNode_UnionType:
|
||||
case AstNode_EnumType:
|
||||
case AstNode_ProcType:
|
||||
return true;
|
||||
}
|
||||
} break;
|
||||
}
|
||||
}
|
||||
// if (s != NULL) {
|
||||
// switch (s->kind) {
|
||||
// case AstNode_ProcDecl:
|
||||
// return true;
|
||||
// case AstNode_TypeDecl: {
|
||||
// switch (s->TypeDecl.type->kind) {
|
||||
// case AstNode_StructType:
|
||||
// case AstNode_UnionType:
|
||||
// case AstNode_EnumType:
|
||||
// case AstNode_ProcType:
|
||||
// return true;
|
||||
// }
|
||||
// } break;
|
||||
// }
|
||||
// }
|
||||
|
||||
if (!allow_token(f, Token_Semicolon)) {
|
||||
if (f->cursor[0].pos.line == f->cursor[-1].pos.line) {
|
||||
@@ -1710,6 +1710,65 @@ AstNode *parse_parameter_list(AstFile *f, AstScope *scope, isize *param_count_,
|
||||
}
|
||||
|
||||
|
||||
AstNode *parse_struct_params(AstFile *f, isize *decl_count_) {
|
||||
AstNode *decls = NULL;
|
||||
AstNode *decls_curr = NULL;
|
||||
isize decl_count = 0;
|
||||
|
||||
while (f->cursor[0].kind == Token_Identifier ||
|
||||
f->cursor[0].kind == Token_using) {
|
||||
b32 is_using = false;
|
||||
if (allow_token(f, Token_using)) {
|
||||
is_using = true;
|
||||
}
|
||||
isize name_count = 0;
|
||||
AstNode *name_list = parse_lhs_expr_list(f, &name_count);
|
||||
if (name_count == 0) {
|
||||
ast_file_err(f, f->cursor[0], "Empty field declaration");
|
||||
}
|
||||
|
||||
if (name_count > 1 && is_using) {
|
||||
ast_file_err(f, f->cursor[0], "Cannot apply `using` to more than one of the same type");
|
||||
}
|
||||
|
||||
AstNode *decl = NULL;
|
||||
|
||||
if (f->cursor[0].kind == Token_Colon) {
|
||||
decl = parse_decl(f, name_list, name_count);
|
||||
|
||||
if (decl->kind == AstNode_ProcDecl) {
|
||||
ast_file_err(f, f->cursor[0], "Procedure declarations are not allowed within a structure");
|
||||
decl = make_bad_decl(f, ast_node_token(name_list), f->cursor[0]);
|
||||
}
|
||||
} else {
|
||||
ast_file_err(f, f->cursor[0], "Illegal structure field");
|
||||
decl = make_bad_decl(f, ast_node_token(name_list), f->cursor[0]);
|
||||
}
|
||||
|
||||
expect_semicolon_after_stmt(f, decl);
|
||||
|
||||
if (decl != NULL && is_ast_node_decl(decl)) {
|
||||
DLIST_APPEND(decls, decls_curr, decl);
|
||||
if (decl->kind == AstNode_VarDecl) {
|
||||
decl_count += decl->VarDecl.name_count;
|
||||
decl->VarDecl.is_using = is_using;
|
||||
|
||||
if (decl->VarDecl.kind == Declaration_Mutable) {
|
||||
if (decl->VarDecl.value_count > 0) {
|
||||
ast_file_err(f, f->cursor[0], "Default variable assignments within a structure will be ignored (at the moment)");
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
decl_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (decl_count_) *decl_count_ = decl_count;
|
||||
return decls;
|
||||
}
|
||||
|
||||
AstNode *parse_identifier_or_type(AstFile *f) {
|
||||
switch (f->cursor[0].kind) {
|
||||
case Token_Identifier: {
|
||||
@@ -1770,78 +1829,27 @@ AstNode *parse_identifier_or_type(AstFile *f) {
|
||||
|
||||
|
||||
Token open = expect_token(f, Token_OpenBrace);
|
||||
AstNode *decls = NULL;
|
||||
AstNode *decls_curr = NULL;
|
||||
isize decl_count = 0;
|
||||
|
||||
while (f->cursor[0].kind == Token_Identifier ||
|
||||
f->cursor[0].kind == Token_using) {
|
||||
b32 is_using = false;
|
||||
if (allow_token(f, Token_using)) {
|
||||
is_using = true;
|
||||
}
|
||||
isize name_count = 0;
|
||||
AstNode *name_list = parse_lhs_expr_list(f, &name_count);
|
||||
if (name_count == 0) {
|
||||
ast_file_err(f, f->cursor[0], "Empty field declaration");
|
||||
}
|
||||
|
||||
if (name_count > 1 && is_using) {
|
||||
ast_file_err(f, f->cursor[0], "Cannot apply `using` to more than one of the same type");
|
||||
}
|
||||
|
||||
AstNode *decl = NULL;
|
||||
|
||||
if (f->cursor[0].kind == Token_Colon) {
|
||||
decl = parse_decl(f, name_list, name_count);
|
||||
|
||||
if (decl->kind == AstNode_ProcDecl) {
|
||||
ast_file_err(f, f->cursor[0], "Procedure declarations are not allowed within a structure");
|
||||
decl = make_bad_decl(f, ast_node_token(name_list), f->cursor[0]);
|
||||
}
|
||||
} else {
|
||||
ast_file_err(f, f->cursor[0], "Illegal structure field");
|
||||
decl = make_bad_decl(f, ast_node_token(name_list), f->cursor[0]);
|
||||
}
|
||||
|
||||
expect_semicolon_after_stmt(f, decl);
|
||||
|
||||
if (decl != NULL && is_ast_node_decl(decl)) {
|
||||
DLIST_APPEND(decls, decls_curr, decl);
|
||||
if (decl->kind == AstNode_VarDecl) {
|
||||
decl_count += decl->VarDecl.name_count;
|
||||
decl->VarDecl.is_using = is_using;
|
||||
|
||||
if (decl->VarDecl.kind == Declaration_Mutable) {
|
||||
if (decl->VarDecl.value_count > 0) {
|
||||
ast_file_err(f, f->cursor[0], "Default variable assignments within a structure will be ignored (at the moment)");
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
decl_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
AstNode *decls = parse_struct_params(f, &decl_count);
|
||||
Token close = expect_token(f, Token_CloseBrace);
|
||||
|
||||
// params = parse_parameter_list(f, scope, ¶m_count, Token_Semicolon, true);
|
||||
|
||||
return make_struct_type(f, token, decls, decl_count, is_packed);
|
||||
} break;
|
||||
|
||||
case Token_union: {
|
||||
Token token = expect_token(f, Token_union);
|
||||
Token open, close;
|
||||
AstNode *params = NULL;
|
||||
isize param_count = 0;
|
||||
AstScope *scope = make_ast_scope(f, NULL); // NOTE(bill): The union needs its own scope with NO parent
|
||||
AstScope *scope = make_ast_scope(f, NULL); // NOTE(bill): The struct needs its own scope with NO parent
|
||||
AstScope *curr_scope = f->curr_scope;
|
||||
f->curr_scope = scope;
|
||||
defer (f->curr_scope = curr_scope);
|
||||
|
||||
open = expect_token(f, Token_OpenBrace);
|
||||
params = parse_parameter_list(f, scope, ¶m_count, Token_Semicolon, true);
|
||||
close = expect_token(f, Token_CloseBrace);
|
||||
|
||||
return make_union_type(f, token, params, param_count);
|
||||
Token open = expect_token(f, Token_OpenBrace);
|
||||
isize decl_count = 0;
|
||||
AstNode *decls = parse_struct_params(f, &decl_count);
|
||||
Token close = expect_token(f, Token_CloseBrace);
|
||||
|
||||
return make_union_type(f, token, decls, decl_count);
|
||||
}
|
||||
|
||||
case Token_enum: {
|
||||
@@ -1952,7 +1960,7 @@ Token parse_procedure_signature(AstFile *f, AstScope *scope,
|
||||
AstNode **result_list, isize *result_count) {
|
||||
Token proc_token = expect_token(f, Token_proc);
|
||||
expect_token(f, Token_OpenParen);
|
||||
*param_list = parse_parameter_list(f, scope, param_count, Token_Comma, false);
|
||||
*param_list = parse_parameter_list(f, scope, param_count, Token_Comma, true);
|
||||
expect_token(f, Token_CloseParen);
|
||||
*result_list = parse_results(f, scope, result_count);
|
||||
return proc_token;
|
||||
|
||||
@@ -501,7 +501,7 @@ exponent:
|
||||
return token;
|
||||
}
|
||||
|
||||
// Quote == " for string and ' for char
|
||||
// Quote == " for string
|
||||
b32 scan_escape(Tokenizer *t, Rune quote) {
|
||||
isize len = 0;
|
||||
u32 base = 0, max = 0, x = 0;
|
||||
|
||||
Reference in New Issue
Block a user