From: Brendan Hansen Date: Wed, 30 Dec 2020 19:40:56 +0000 (-0600) Subject: code cleanup with `use` on struct variables X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=ead366d624d3f073e6ab4ef70172eb55cf93a755;p=onyx.git code cleanup with `use` on struct variables --- diff --git a/onyx b/onyx index 0445ec27..b10a3c95 100755 Binary files a/onyx and b/onyx differ diff --git a/src/onyxsymres.c b/src/onyxsymres.c index 95d7e4bf..8e15a731 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -567,7 +567,7 @@ static void symres_use(AstUse* use) { return; } - if (use->expr->type_node == NULL) goto cannot_use; + if (use->expr->type_node == NULL && use->expr->type == NULL) goto cannot_use; AstType* effective_type = use->expr->type_node; if (effective_type->kind == Ast_Kind_Pointer_Type) @@ -584,16 +584,16 @@ static void symres_use(AstUse* use) { if (st->kind == Type_Kind_Pointer) st = st->Pointer.elem; - bh_arr_each(StructMember *, smem, st->Struct.memarr) { - AstFieldAccess* fa = make_field_access(use->expr, (*smem)->name); - symbol_raw_introduce(semstate.curr_scope, (*smem)->name, use->token->pos, (AstNode *) fa); - } + bh_table_each_start(StructMember, st->Struct.members); + AstFieldAccess* fa = make_field_access(use->expr, value.name); + symbol_raw_introduce(semstate.curr_scope, value.name, use->token->pos, (AstNode *) fa); + bh_table_each_end; return; } cannot_use: - onyx_report_error(use->token->pos, "Cannot use this."); + onyx_report_error(use->token->pos, "Cannot use this because its type is unknown."); } static void symres_directive_solidify(AstDirectiveSolidify** psolid) { @@ -762,14 +762,8 @@ void symres_function_header(AstFunction* func) { } bh_table_each_start(StructMember, st->Struct.members); - AstFieldAccess* fa = onyx_ast_node_new(semstate.node_allocator, sizeof(AstFieldAccess), Ast_Kind_Field_Access); - fa->expr = (AstTyped *) param->local; - fa->field = value.name; - - symbol_raw_introduce(semstate.curr_scope, - value.name, - param->local->token->pos, - (AstNode *) fa); + AstFieldAccess* fa = make_field_access((AstTyped *) param->local, value.name); + symbol_raw_introduce(semstate.curr_scope, value.name, param->local->token->pos, (AstNode *) fa); bh_table_each_end; } else { diff --git a/tests/struct_robustness.onyx b/tests/struct_robustness.onyx index 2ba09551..7e7586f7 100644 --- a/tests/struct_robustness.onyx +++ b/tests/struct_robustness.onyx @@ -22,12 +22,13 @@ main :: proc (args: [] cstr) { name : str; } - ss := SimpleStruct.{ 41, 67, "Steve" }; + ss: SimpleStruct = SimpleStruct.{ 41, 67, "Steve" }; + use ss; printf("SimpleStruct<%i, %i>(%i, %i, %s)\n", sizeof SimpleStruct, alignof SimpleStruct, - cast(u32) ss.age, ss.height, ss.name); + cast(u32) age, height, name); } test_simple_union :: proc () {