From: Brendan Hansen Date: Sat, 30 Jan 2021 00:26:30 +0000 (-0600) Subject: top level bindings in struct scopes are even more useful X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=12183a3c2aa5f046f8126721745144f9cc8f0809;p=onyx.git top level bindings in struct scopes are even more useful --- diff --git a/bin/onyx b/bin/onyx index 039edd7e..c7bfabc0 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/onyx.exe b/onyx.exe index 61ba581e..5f4d6e76 100644 Binary files a/onyx.exe and b/onyx.exe differ diff --git a/src/onyxparser.c b/src/onyxparser.c index 731a0ec2..dc06f373 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -1829,6 +1829,13 @@ static AstStructType* parse_struct(OnyxParser* parser) { } expect_token(parser, '{'); + + // HACK: There should be a better way to change which scope symbols will + // be placed in when converted to entities. Right now, you have to mess + // with parser->file_scope to change that. There is the block stack mechanism, + // but that feels very limited for this purpose. + Scope* parent_scope = parser->file_scope; + b32 member_is_used = 0; while (parser->curr->type != '}') { if (parser->hit_unexpected_token) return s_node; @@ -1848,7 +1855,8 @@ static AstStructType* parse_struct(OnyxParser* parser) { if (!s_node->scope) { // NOTE: The parent scope will be filled out during symbol resolution. - s_node->scope = scope_create(context.ast_alloc, NULL, s_node->token->pos); + s_node->scope = scope_create(context.ast_alloc, parent_scope, s_node->token->pos); + parser->file_scope = s_node->scope; } AstBinding* binding = parse_top_level_binding(parser, member_name); @@ -1881,6 +1889,8 @@ static AstStructType* parse_struct(OnyxParser* parser) { expect_token(parser, '}'); + parser->file_scope = parent_scope; + if (poly_struct != NULL) { // NOTE: Not a StructType return (AstStructType *) poly_struct; diff --git a/src/onyxsymres.c b/src/onyxsymres.c index 77c0047e..3eaddbb1 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -548,7 +548,7 @@ static void symres_switch(AstSwitch* switchnode) { static void symres_use(AstUse* use) { symres_expression(&use->expr); - if (use->expr == NULL) return; + if (use->expr == NULL || use->expr->kind == Ast_Kind_Error) return; if (use->expr->kind == Ast_Kind_Enum_Type) { AstEnumType* et = (AstEnumType *) use->expr; @@ -559,6 +559,15 @@ static void symres_use(AstUse* use) { return; } + if (use->expr->kind == Ast_Kind_Struct_Type) { + AstStructType* st = (AstStructType *) use->expr; + + if (st->scope) + scope_include(curr_scope, st->scope, use->token->pos); + + return; + } + if (use->expr->type_node == NULL && use->expr->type == NULL) goto cannot_use; AstType* effective_type = use->expr->type_node; diff --git a/src/onyxtypes.c b/src/onyxtypes.c index 94ff6094..26ccd63a 100644 --- a/src/onyxtypes.c +++ b/src/onyxtypes.c @@ -376,7 +376,7 @@ Type* type_build_from_ast(bh_allocator alloc, AstType* type_node) { s_type->Struct.memarr = NULL; s_type->Struct.poly_sln = NULL; - bh_table_init(global_heap_allocator, s_type->Struct.members, s_type->Struct.mem_count); + bh_table_init(global_heap_allocator, s_type->Struct.members, s_type->Struct.mem_count + 1); bh_arr_new(global_heap_allocator, s_type->Struct.memarr, s_type->Struct.mem_count); b32 is_union = (s_node->flags & Ast_Flag_Struct_Is_Union) != 0;