better error message on mismatched function arguments
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 23 Dec 2020 18:28:02 +0000 (12:28 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 23 Dec 2020 18:28:02 +0000 (12:28 -0600)
include/onyxastnodes.h
include/onyxutils.h
onyx
src/onyxchecker.c
src/onyxparser.c
src/onyxsymres.c
src/onyxutils.c

index ffb8c60dca7b8275236bf1afde885a8d45f411c1..35d32a216e3893aab175b6493be0ec8474f5e164 100644 (file)
@@ -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,
index 78ba0a4e8bc34ad1e811392e934945a029d64e2f..dd7492ddaaf96c851181ba0d6ebc82397070b9f9 100644 (file)
@@ -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 1ae07bcc524c07076828cca628df310216a1b647..2b80fa462d6484562d2b5a5e8756dccf51848cf5 100755 (executable)
Binary files a/onyx and b/onyx differ
index 7af9ab8839537b55389dbd3da5f568a183e09b95..e65982890642371c7c242aeed578fa1f3f67bbc5 100644 (file)
@@ -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,
index 33a547975533b8e32337302abb34acdd605cfce0..2f7e7a5bdb4dd9c944051342f44ae73f4fd07fb2 100644 (file)
@@ -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) {
 
index e3327c8eb61923c542bc5a07a8335aeb8e661176..0e7fe68c4e1f59e7cc58109438655c7c508a351a 100644 (file)
@@ -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);
     }
 }
 
index 714f47f460cbfdf85ef33d9ba24a8d7f9eb8bae5..85bb259d2d62041720ee2a4636e5eeb360c36801 100644 (file)
@@ -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 "<anonymous procedure>";
+}