From: Brendan Hansen Date: Wed, 23 Dec 2020 18:28:02 +0000 (-0600) Subject: better error message on mismatched function arguments X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=45d3ddfc2f351b5e3dae196bff73acaf9e01a525;p=onyx.git better error message on mismatched function arguments --- diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index ffb8c60d..35d32a21 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -640,6 +640,8 @@ struct AstAlias { struct AstGlobal { AstTyped_base; + OnyxToken* name; + union { // NOTE: Used when a global is exported with a specific name OnyxToken* exported_name; @@ -675,6 +677,8 @@ struct AstFunction { // then resolved to an overloaded function. AstNode *overloaded_function; + OnyxToken* name; + union { // NOTE: Used when a function is exported with a specific name OnyxToken* exported_name; @@ -860,6 +864,7 @@ void promote_numlit_to_larger(AstNumLit* num); b32 convert_numlit_to_type(AstNumLit* num, Type* type); b32 type_check_or_auto_cast(AstTyped** pnode, Type* type); Type* resolve_expression_type(AstTyped* node); +char* get_function_name(AstFunction* func); typedef enum PolyProcLookupMethod { PPLM_By_Call, diff --git a/include/onyxutils.h b/include/onyxutils.h index 78ba0a4e..dd7492dd 100644 --- a/include/onyxutils.h +++ b/include/onyxutils.h @@ -14,7 +14,7 @@ void program_info_init(ProgramInfo* prog, bh_allocator alloc); Package* program_info_package_lookup(ProgramInfo* prog, char* package_name); Package* program_info_package_lookup_or_create(ProgramInfo* prog, char* package_name, Scope* parent_scope, bh_allocator alloc); -void scope_include(Scope* target, Scope* source); +void scope_include(Scope* target, Scope* source, OnyxFilePos pos); b32 symbol_introduce(Scope* scope, OnyxToken* tkn, AstNode* symbol); b32 symbol_raw_introduce(Scope* scope, char* tkn, OnyxFilePos pos, AstNode* symbol); void symbol_builtin_introduce(Scope* scope, char* sym, AstNode *node); diff --git a/onyx b/onyx index 1ae07bcc..2b80fa46 100755 Binary files a/onyx and b/onyx differ diff --git a/src/onyxchecker.c b/src/onyxchecker.c index 7af9ab88..e6598289 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -421,8 +421,8 @@ b32 check_call(AstCall* call) { if (arg_pos >= bh_arr_length(arg_arr)) goto type_checking_done; if (!type_check_or_auto_cast(&arg_arr[arg_pos]->value, formal_params[arg_pos])) { onyx_report_error(arg_arr[arg_pos]->token->pos, - "The function '%b' expects a value of type '%s' for %d%s parameter, got '%s'.", - callee->token->text, callee->token->length, + "The procedure '%s' expects a value of type '%s' for %d%s parameter, got '%s'.", + get_function_name(callee), type_get_name(formal_params[arg_pos]), arg_pos + 1, bh_num_suffix(arg_pos + 1), @@ -440,8 +440,8 @@ b32 check_call(AstCall* call) { if (arg_pos >= bh_arr_length(arg_arr)) goto type_checking_done; if (!type_check_or_auto_cast(&arg_arr[arg_pos]->value, variadic_type)) { onyx_report_error(arg_arr[arg_pos]->token->pos, - "The function '%b' expects a value of type '%s' for the variadic parameter, '%b', got '%s'.", - callee->token->text, callee->token->length, + "The procedure '%s' expects a value of type '%s' for the variadic parameter, '%b', got '%s'.", + get_function_name(callee), type_get_name(variadic_type), variadic_param->local->token->text, variadic_param->local->token->length, diff --git a/src/onyxparser.c b/src/onyxparser.c index 33a54797..2f7e7a5b 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -1972,12 +1972,24 @@ static AstNode* parse_top_level_statement(OnyxParser* parser) { if (func->exported_name == NULL) func->exported_name = symbol; + func->name = symbol; + + } else if (node->kind == Ast_Kind_Polymorphic_Proc) { + AstPolyProc* proc = (AstPolyProc *) node; + + if (proc->base_func->exported_name == NULL) + proc->base_func->exported_name = symbol; + + proc->base_func->name = symbol; + } else if (node->kind == Ast_Kind_Global) { AstGlobal* global = (AstGlobal *) node; if (global->exported_name == NULL) global->exported_name = symbol; + global->name = symbol; + } else if (node->kind != Ast_Kind_Overloaded_Function && node->kind != Ast_Kind_StrLit) { diff --git a/src/onyxsymres.c b/src/onyxsymres.c index e3327c8e..0e7fe68c 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -796,7 +796,11 @@ static void symres_use_package(AstUsePackage* package) { } if (package->alias == NULL && package->only == NULL) { - scope_include(semstate.curr_scope, p->scope); + OnyxFilePos pos = { 0 }; + if (package->token != NULL) + pos = package->token->pos; + + scope_include(semstate.curr_scope, p->scope, pos); } } diff --git a/src/onyxutils.c b/src/onyxutils.c index 714f47f4..85bb259d 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -177,9 +177,9 @@ Scope* scope_create(bh_allocator a, Scope* parent, OnyxFilePos created_at) { return scope; } -void scope_include(Scope* target, Scope* source) { +void scope_include(Scope* target, Scope* source, OnyxFilePos pos) { bh_table_each_start(AstNode *, source->symbols); - symbol_raw_introduce(target, (char *) key, value->token->pos, value); + symbol_raw_introduce(target, (char *) key, pos, value); bh_table_each_end; } @@ -194,8 +194,12 @@ b32 symbol_introduce(Scope* scope, OnyxToken* tkn, AstNode* symbol) { b32 symbol_raw_introduce(Scope* scope, char* name, OnyxFilePos pos, AstNode* symbol) { if (bh_table_has(AstNode *, scope->symbols, name)) { - onyx_report_error(pos, "Redeclaration of symbol '%s'.", name); - return 0; + if (bh_table_get(AstNode *, scope->symbols, name) != symbol) { + onyx_report_error(pos, "Redeclaration of symbol '%s'.", name); + return 0; + } + + return 1; } bh_table_put(AstNode *, scope->symbols, name, symbol); @@ -869,3 +873,17 @@ Type* resolve_expression_type(AstTyped* node) { return node->type; } +char* get_function_name(AstFunction* func) { + if (func->name != NULL) { + return bh_aprintf(global_scratch_allocator, "%b", func->name->text, func->name->length); + } + + if (func->exported_name != NULL) { + return bh_aprintf(global_scratch_allocator, + "EXPORTED:%b", + func->exported_name->text, + func->exported_name->length); + } + + return ""; +}