mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-18 08:41:31 -07:00
Subtyping Polymorphic arguments; using procedure parameters
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
+87
-86
@@ -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;
|
||||
}
|
||||
|
||||
+58
-23
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user