refactored polymorphic procs and funcs together
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 4 Jan 2022 17:42:10 +0000 (11:42 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 4 Jan 2022 17:42:10 +0000 (11:42 -0600)
include/astnodes.h
src/astnodes.c
src/checker.c
src/clone.c
src/entities.c
src/parser.c
src/polymorph.h
src/symres.c
src/utils.c

index 1269ad62fb8027acecc51a43d935104da87f31c9..9bff6f58bbf2673c49d31e1773149344e79f2bb9 100644 (file)
@@ -710,7 +710,7 @@ struct AstDoBlock {
 struct AstDirectiveSolidify {
     AstTyped_base;
 
-    AstPolyProc* poly_proc;
+    AstFunction* poly_proc;
     bh_arr(AstPolySolution) known_polyvars;
 
     AstNode* resolved_proc;
@@ -987,48 +987,6 @@ struct AstParam {
     b32 use_processed : 1;
     b32 is_baked      : 1;
 };
-struct AstFunction {
-    AstTyped_base;
-
-    Scope *scope;
-
-    bh_arr(AstParam) params;
-    AstType* return_type;
-
-    AstBlock *body;
-
-    char* name;
-
-    // NOTE: This is NULL, unless this function was generated from a polymorphic
-    // procedure call. Then it is set to the token of the call node.
-    OnyxToken* generated_from;
-    Scope*     poly_scope;
-
-    // NOTE: This is NULL, unless this function is used in a "#export" directive.
-    // It is undefined which name it will have if there are multiple export directives
-    // for this particular function.
-    OnyxToken* exported_name;
-
-    union {
-        OnyxToken* intrinsic_name;
-
-        // NOTE: Used when the function is declared as foreign
-        struct {
-            OnyxToken* foreign_module;
-            OnyxToken* foreign_name;
-        };
-    };
-
-    struct Entity* entity_header;
-    struct Entity* entity_body;
-
-    ConstraintContext constraints;
-
-    b32 is_exported  : 1;
-    b32 is_foreign   : 1;
-    b32 is_intrinsic : 1;
-};
-
 typedef struct OverloadOption OverloadOption;
 struct OverloadOption {
     // This is u64 because padding will make it that anyway.
@@ -1161,25 +1119,61 @@ struct AstSolidifiedFunction {
     struct Entity *func_header_entity;
 };
 
-struct AstPolyProc {
-    // While the "type" of a polymorphic procedure will never be set, it is necessary
-    // for contexts where it used in an expression.
+struct AstFunction {
     AstTyped_base;
 
-    Scope *poly_scope;
+    Scope *scope;
+
+    bh_arr(AstParam) params;
+    AstType* return_type;
+
+    AstBlock *body;
+
+    char* name;
+
+    // NOTE: This is NULL, unless this function was generated from a polymorphic
+    // procedure call. Then it is set to the token of the call node.
+    OnyxToken* generated_from;
+    Scope*     poly_scope;
+
+    // NOTE: This is NULL, unless this function is used in a "#export" directive.
+    // It is undefined which name it will have if there are multiple export directives
+    // for this particular function.
+    OnyxToken* exported_name;
+
+    union {
+        OnyxToken* intrinsic_name;
+
+        // NOTE: Used when the function is declared as foreign
+        struct {
+            OnyxToken* foreign_module;
+            OnyxToken* foreign_name;
+        };
+    };
+
+    struct Entity* entity_header;
+    struct Entity* entity_body;
+
+    ConstraintContext constraints;
+
+    // Polymorphic procedures use the following fields
+    Scope *parent_scope_of_poly_proc;
     bh_arr(AstPolyParam) poly_params;
 
     bh_arr(AstPolySolution) known_slns;
 
-    AstFunction* base_func;
     Table(AstSolidifiedFunction) concrete_funcs;
     bh_imap active_queries;
+
+    b32 is_exported  : 1;
+    b32 is_foreign   : 1;
+    b32 is_intrinsic : 1;
 };
 
 struct AstPolyQuery {
     AstNode_base;
 
-    AstPolyProc *proc;
+    AstFunction *proc;
     PolyProcLookupMethod pp_lookup;
     ptr given;
     OnyxToken *error_loc;
@@ -1385,7 +1379,7 @@ typedef struct Entity {
         AstEnumType           *enum_type;
         AstEnumValue          *enum_value;
         AstMemRes             *mem_res;
-        AstPolyProc           *poly_proc;
+        AstFunction           *poly_proc;
         AstPolyQuery          *poly_query;
         AstForeignBlock       *foreign_block;
         AstMacro              *macro;
@@ -1634,11 +1628,11 @@ b32 static_if_resolution(AstIf* static_if);
 
 void insert_poly_sln_into_scope(Scope* scope, AstPolySolution *sln);
 TypeMatch find_polymorphic_sln(AstPolySolution *out, AstPolyParam *param, AstFunction *func, PolyProcLookupMethod pp_lookup, ptr actual, char** err_msg);
-AstFunction* polymorphic_proc_lookup(AstPolyProc* pp, PolyProcLookupMethod pp_lookup, ptr actual, OnyxToken* tkn);
-AstFunction* polymorphic_proc_solidify(AstPolyProc* pp, bh_arr(AstPolySolution) slns, OnyxToken* tkn);
-AstNode* polymorphic_proc_try_solidify(AstPolyProc* pp, bh_arr(AstPolySolution) slns, OnyxToken* tkn);
-AstFunction* polymorphic_proc_build_only_header(AstPolyProc* pp, PolyProcLookupMethod pp_lookup, ptr actual);
-AstFunction* polymorphic_proc_build_only_header_with_slns(AstPolyProc* pp, bh_arr(AstPolySolution) slns, b32 error_if_failed);
+AstFunction* polymorphic_proc_lookup(AstFunction* pp, PolyProcLookupMethod pp_lookup, ptr actual, OnyxToken* tkn);
+AstFunction* polymorphic_proc_solidify(AstFunction* pp, bh_arr(AstPolySolution) slns, OnyxToken* tkn);
+AstNode* polymorphic_proc_try_solidify(AstFunction* pp, bh_arr(AstPolySolution) slns, OnyxToken* tkn);
+AstFunction* polymorphic_proc_build_only_header(AstFunction* pp, PolyProcLookupMethod pp_lookup, ptr actual);
+AstFunction* polymorphic_proc_build_only_header_with_slns(AstFunction* pp, bh_arr(AstPolySolution) slns, b32 error_if_failed);
 
 void add_overload_option(bh_arr(OverloadOption)* poverloads, u64 precedence, AstTyped* overload);
 AstTyped* find_matching_overload_by_arguments(bh_arr(OverloadOption) overloads, Arguments* args);
@@ -1712,7 +1706,7 @@ static inline ParamPassType type_get_param_pass(Type* type) {
 
 static inline AstFunction* get_function_from_node(AstNode* node) {
     if (node->kind == Ast_Kind_Function) return (AstFunction *) node;
-    if (node->kind == Ast_Kind_Polymorphic_Proc) return ((AstPolyProc *) node)->base_func;
+    if (node->kind == Ast_Kind_Polymorphic_Proc) return (AstFunction *) node;
     if (node->kind == Ast_Kind_Macro) return get_function_from_node((AstNode*) ((AstMacro *) node)->body);
     return NULL;
 }
index a63658802ee2edf9d8fb0d301db16ad1b4a42265..338a33d236b2301a8d2246390e90416f3e0d5e5a 100644 (file)
@@ -616,7 +616,7 @@ TypeMatch unify_node_and_type_(AstTyped** pnode, Type* type, b32 permanent) {
     }
 
     if (node->kind == Ast_Kind_Polymorphic_Proc) {
-        AstFunction* func = polymorphic_proc_lookup((AstPolyProc *) node, PPLM_By_Function_Type, type, node->token);
+        AstFunction* func = polymorphic_proc_lookup((AstFunction *) node, PPLM_By_Function_Type, type, node->token);
         if (func == NULL) return TYPE_MATCH_FAILED;
         if (func == (AstFunction *) &node_that_signals_a_yield) return TYPE_MATCH_YIELD;
 
index 8157341a84810864ffb34e00b86405be29c43156..df8e3f054d37e04a8dcb912641c5cef205bb9d9d 100644 (file)
@@ -490,7 +490,7 @@ static CheckStatus check_resolve_callee(AstCall* call, AstTyped** effective_call
         callee = new_callee;
 
     } else if (callee->kind == Ast_Kind_Polymorphic_Proc) {
-        AstTyped* new_callee = (AstTyped *) polymorphic_proc_lookup((AstPolyProc *) callee, PPLM_By_Arguments, &call->args, call->token);
+        AstTyped* new_callee = (AstTyped *) polymorphic_proc_lookup((AstFunction *) callee, PPLM_By_Arguments, &call->args, call->token);
         if (new_callee == NULL) return Check_Error;
         if (new_callee == (AstTyped *) &node_that_signals_a_yield) {
             YIELD(call->token->pos, "Waiting for polymorphic procedure header to pass type-checking.");
@@ -2688,7 +2688,7 @@ CheckStatus check_constraint_context(ConstraintContext *cc, Scope *scope, OnyxFi
 
 CheckStatus check_polyquery(AstPolyQuery *query) {
     if (query->function_header->scope == NULL)
-        query->function_header->scope = scope_create(context.ast_alloc, query->proc->poly_scope, query->token->pos);
+        query->function_header->scope = scope_create(context.ast_alloc, query->proc->parent_scope_of_poly_proc, query->token->pos);
 
     CheckStatus header_check = check_temp_function_header(query->function_header);
     if (header_check == Check_Return_To_Symres) return Check_Return_To_Symres;
index e2b577d1ccc391241eec186008b732236e057571..e55a43f9d8c92a7a741fac4f92e11be731fe80ce 100644 (file)
@@ -14,7 +14,6 @@ static inline b32 should_clone(AstNode* node) {
         case Ast_Kind_Enum_Type:
         case Ast_Kind_Enum_Value:
         case Ast_Kind_Overloaded_Function:
-        case Ast_Kind_Polymorphic_Proc:
         case Ast_Kind_Alias:
         case Ast_Kind_Code_Block:
         case Ast_Kind_Macro:
@@ -36,7 +35,7 @@ static inline i32 ast_kind_to_size(AstNode* node) {
         case Ast_Kind_Binding: return sizeof(AstBinding);
         case Ast_Kind_Function: return sizeof(AstFunction);
         case Ast_Kind_Overloaded_Function: return sizeof(AstOverloadedFunction);
-        case Ast_Kind_Polymorphic_Proc: return sizeof(AstPolyProc);
+        case Ast_Kind_Polymorphic_Proc: return sizeof(AstFunction);
         case Ast_Kind_Block: return sizeof(AstBlock);
         case Ast_Kind_Local: return sizeof(AstLocal);
         case Ast_Kind_Global: return sizeof(AstGlobal);
@@ -395,7 +394,8 @@ AstNode* ast_clone(bh_allocator a, void* n) {
             C(AstBinding, node);
             break;
 
-        case Ast_Kind_Function: {
+        case Ast_Kind_Function:
+        case Ast_Kind_Polymorphic_Proc: {
             if (clone_depth > 1) {
                 clone_depth--;
                 return node;
@@ -404,6 +404,18 @@ AstNode* ast_clone(bh_allocator a, void* n) {
             AstFunction* df = (AstFunction *) nn;
             AstFunction* sf = (AstFunction *) node;
 
+            if (node->kind == Ast_Kind_Polymorphic_Proc) {
+                df->kind = Ast_Kind_Function;
+                df->parent_scope_of_poly_proc = NULL;
+                df->poly_params = NULL;
+                df->known_slns = NULL;
+                df->concrete_funcs = NULL;
+                df->active_queries.hashes = NULL;
+                df->active_queries.entries = NULL;
+                df->poly_scope = NULL;
+                df->entity = NULL;
+            }
+
             if (sf->is_foreign) return node;
             assert(df->scope == NULL);
 
@@ -459,7 +471,7 @@ AstNode* ast_clone(bh_allocator a, void* n) {
             AstDirectiveSolidify* dd = (AstDirectiveSolidify *) nn;
             AstDirectiveSolidify* sd = (AstDirectiveSolidify *) node;
 
-            dd->poly_proc = (AstPolyProc *) ast_clone(a, (AstNode *) sd->poly_proc);
+            dd->poly_proc = (AstFunction *) ast_clone(a, (AstNode *) sd->poly_proc);
             dd->resolved_proc = NULL;
 
             dd->known_polyvars = NULL;
@@ -521,7 +533,7 @@ AstNode* ast_clone(bh_allocator a, void* n) {
 #undef C
 
 AstFunction* clone_function_header(bh_allocator a, AstFunction* func) {
-    if (func->kind != Ast_Kind_Function) return NULL;
+    if (func->kind != Ast_Kind_Function && func->kind != Ast_Kind_Polymorphic_Proc) return NULL;
 
     if (func->is_foreign) return func;
 
@@ -529,6 +541,19 @@ AstFunction* clone_function_header(bh_allocator a, AstFunction* func) {
     memmove(new_func, func, sizeof(AstFunction));
     assert(new_func->scope == NULL);
 
+    if (func->kind == Ast_Kind_Polymorphic_Proc) {
+        new_func->kind = Ast_Kind_Function;
+        new_func->parent_scope_of_poly_proc = NULL;
+        new_func->poly_params = NULL;
+        new_func->known_slns = NULL;
+        new_func->concrete_funcs = NULL;
+        new_func->active_queries.hashes = NULL;
+        new_func->active_queries.entries = NULL;
+        new_func->poly_scope = NULL;
+        new_func->entity = NULL;
+    }
+
+    // new_func->body = NULL;
     new_func->return_type = (AstType *) ast_clone(a, func->return_type);
 
     new_func->params = NULL;
@@ -558,7 +583,7 @@ AstFunction* clone_function_header(bh_allocator a, AstFunction* func) {
 // a function from `clone_function_header`.
 void clone_function_body(bh_allocator a, AstFunction* dest, AstFunction* source) {
     if (dest->kind != Ast_Kind_Function) return;
-    if (source->kind != Ast_Kind_Function) return;
+    if (source->kind != Ast_Kind_Polymorphic_Proc && source->kind != Ast_Kind_Function) return;
 
     dest->body = (AstBlock *) ast_clone(a, source->body);
 }
index f58da9fcc1dd25216d8261c75f2ef90dafdbc907..2bf46c94b0270d6fdf2a59bc3abb920306bd606c 100644 (file)
@@ -311,7 +311,7 @@ void add_entities_for_node(bh_arr(Entity *) *target_arr, AstNode* node, Scope* s
 
         case Ast_Kind_Polymorphic_Proc: {
             ent.type = Entity_Type_Polymorphic_Proc;
-            ent.poly_proc = (AstPolyProc *) node;
+            ent.poly_proc = (AstFunction *) node;
             ENTITY_INSERT(ent);
             break;
         }
index a3013564073f4eca736c215d756e65ba2bd6424d..188dcde0ce0a4cbb517dd574a256dbf7f680caae 100644 (file)
@@ -594,7 +594,7 @@ static AstTyped* parse_factor(OnyxParser* parser) {
                 // :LinearTokenDependent
                 solid->token = parser->curr - 1;
 
-                solid->poly_proc = (AstPolyProc *) parse_factor(parser);
+                solid->poly_proc = (AstFunction *) parse_factor(parser);
 
                 solid->known_polyvars = NULL;
                 bh_arr_new(global_heap_allocator, solid->known_polyvars, 2);
@@ -2373,17 +2373,14 @@ static AstFunction* parse_function_definition(OnyxParser* parser, OnyxToken* tok
 
 function_defined:
     if (bh_arr_length(polymorphic_vars) > 0) {
-        AstPolyProc* pp = make_node(AstPolyProc, Ast_Kind_Polymorphic_Proc);
-        pp->token = func_def->token;
-        pp->poly_params = polymorphic_vars;
-        pp->base_func = func_def;
-
-        return (AstFunction *) pp;
+        func_def->kind = Ast_Kind_Polymorphic_Proc;
+        func_def->poly_params = polymorphic_vars;
 
     } else {
         bh_arr_free(polymorphic_vars);
-        return func_def;
     }
+    
+    return func_def;
 }
 
 static b32 parse_possible_function_definition_no_consume(OnyxParser* parser) {
@@ -2497,15 +2494,14 @@ static b32 parse_possible_quick_function_definition(OnyxParser* parser, AstTyped
         bh_arr_push(poly_params, type_node);
     }
 
-    AstFunction* func_node = make_node(AstFunction, Ast_Kind_Function);
-    AstPolyProc* poly_proc = make_node(AstPolyProc, Ast_Kind_Polymorphic_Proc);
+    AstFunction* poly_proc = make_node(AstFunction, Ast_Kind_Polymorphic_Proc);
 
-    bh_arr_new(global_heap_allocator, func_node->params, bh_arr_length(params));
+    bh_arr_new(global_heap_allocator, poly_proc->params, bh_arr_length(params));
     fori (i, 0, bh_arr_length(params)) {
         AstLocal* param_local = make_local(parser->allocator, params[i].token, (AstType *) poly_params[i]);
         param_local->kind = Ast_Kind_Param;
 
-        bh_arr_push(func_node->params, ((AstParam) {
+        bh_arr_push(poly_proc->params, ((AstParam) {
             .local = param_local,
             .default_value = NULL,
 
@@ -2544,9 +2540,9 @@ static b32 parse_possible_quick_function_definition(OnyxParser* parser, AstTyped
         return_type = (AstType *) return_type_of;
     }
 
-    func_node->token = proc_token;
-    func_node->body = body_block;
-    func_node->return_type = (AstType *) return_type;
+    poly_proc->token = proc_token;
+    poly_proc->body = body_block;
+    poly_proc->return_type = (AstType *) return_type;
 
     poly_proc->token = proc_token;
     bh_arr_new(global_heap_allocator, poly_proc->poly_params, bh_arr_length(params));
@@ -2563,7 +2559,6 @@ static b32 parse_possible_quick_function_definition(OnyxParser* parser, AstTyped
             // This is not handled currently, as you cannot say f :: ($x: $T) yet, which is what this would have to do.
         }
     }
-    poly_proc->base_func = func_node;
 
     *ret = (AstTyped *) poly_proc;
 
@@ -2803,7 +2798,8 @@ static AstBinding* parse_top_level_binding(OnyxParser* parser, OnyxToken* symbol
         return NULL;
 
     switch (node->kind) {
-        case Ast_Kind_Function: {
+        case Ast_Kind_Function:
+        case Ast_Kind_Polymorphic_Proc: {
             AstFunction* func = (AstFunction *) node;
 
             if (func->intrinsic_name == NULL) func->intrinsic_name = symbol;
@@ -2812,22 +2808,10 @@ static AstBinding* parse_top_level_binding(OnyxParser* parser, OnyxToken* symbol
             break;
         }
 
-        case Ast_Kind_Polymorphic_Proc: {
-            AstPolyProc* proc = (AstPolyProc *) node;
-
-            if (proc->base_func->intrinsic_name == NULL) proc->base_func->intrinsic_name = symbol;
-
-            proc->base_func->name = generate_name_within_scope(parser, symbol);
-            break;
-        }
-
         case Ast_Kind_Macro: {
             AstMacro* macro = (AstMacro *) node;
 
             AstFunction* func = (AstFunction *) macro->body;
-            if (func->kind == Ast_Kind_Polymorphic_Proc)
-                func = (AstFunction *) ((AstPolyProc *) func)->base_func;
-
             func->name = generate_name_within_scope(parser, symbol);
             break;
         }
index d1ae0ee127e373e9b92887ab89f46f0d284d86f1..0a82db77851878f8546d4c9f08a54e9e907d4df0 100644 (file)
@@ -20,7 +20,7 @@ static b32 doing_nested_polymorph_lookup = 0;
 AstTyped node_that_signals_a_yield = { Ast_Kind_Function, 0 };
 AstTyped node_that_signals_failure = { Ast_Kind_Error, 0 };
 
-static void ensure_polyproc_cache_is_created(AstPolyProc* pp) {
+static void ensure_polyproc_cache_is_created(AstFunction* pp) {
     if (pp->concrete_funcs == NULL)        sh_new_arena(pp->concrete_funcs);
     if (pp->active_queries.hashes == NULL) bh_imap_init(&pp->active_queries, global_heap_allocator, 31);
 }
@@ -128,6 +128,7 @@ static b32 add_solidified_function_entities(AstSolidifiedFunction *solidified_fu
     solidified_func->func_header_entity  = entity_header;
     solidified_func->func->entity_header = entity_header;
     solidified_func->func->entity_body   = entity_body;
+    solidified_func->func->entity        = entity_body;
     return 1;
 }
 
@@ -136,7 +137,7 @@ static b32 add_solidified_function_entities(AstSolidifiedFunction *solidified_fu
 // generate the header of the function, which is useful for cases such as checking if a
 // set of arguments works for a polymorphic overload option.
 static AstSolidifiedFunction generate_solidified_function(
-    AstPolyProc* pp,
+    AstFunction* pp,
     bh_arr(AstPolySolution) slns,
     OnyxToken* tkn,
     b32 header_only) {
@@ -149,14 +150,14 @@ static AstSolidifiedFunction generate_solidified_function(
     if (tkn) poly_scope_pos = tkn->pos;
 
     if (header_only) {
-        solidified_func.func = (AstFunction *) clone_function_header(context.ast_alloc, pp->base_func);
+        solidified_func.func = (AstFunction *) clone_function_header(context.ast_alloc, pp);
         solidified_func.func->flags |= Ast_Flag_Incomplete_Body;
 
     } else {
-        solidified_func.func = (AstFunction *) ast_clone(context.ast_alloc, pp->base_func);
+        solidified_func.func = (AstFunction *) ast_clone(context.ast_alloc, pp);
     }
 
-    solidified_func.func->poly_scope = scope_create(context.ast_alloc, pp->poly_scope, poly_scope_pos);
+    solidified_func.func->poly_scope = scope_create(context.ast_alloc, pp->parent_scope_of_poly_proc, poly_scope_pos);
     insert_poly_slns_into_scope(solidified_func.func->poly_scope, slns);
 
     solidified_func.func->flags |= Ast_Flag_From_Polymorphism;
@@ -178,9 +179,9 @@ static AstSolidifiedFunction generate_solidified_function(
     return solidified_func;
 }
 
-static void ensure_solidified_function_has_body(AstPolyProc* pp, AstSolidifiedFunction *solidified_func) {
+static void ensure_solidified_function_has_body(AstFunction* pp, AstSolidifiedFunction *solidified_func) {
     if (solidified_func->func->flags & Ast_Flag_Incomplete_Body) {
-        clone_function_body(context.ast_alloc, solidified_func->func, pp->base_func);
+        clone_function_body(context.ast_alloc, solidified_func->func, pp);
 
         // HACK: I'm asserting that this function should return without an error, because
         // the only case where it can return an error is if there was a problem with the
@@ -417,7 +418,7 @@ static AstTyped* lookup_param_in_arguments(AstFunction* func, AstPolyParam* para
     return NULL;
 }
 
-static AstTyped* try_lookup_based_on_partial_function_type(AstPolyProc *pp, AstFunctionType *ft) {
+static AstTyped* try_lookup_based_on_partial_function_type(AstFunction *pp, AstFunctionType *ft) {
     if (ft->partial_function_type == NULL) {
         AstType *old_return_type = ft->return_type;
         ft->return_type = (AstType *) &basic_type_void;
@@ -475,7 +476,7 @@ static void solve_for_polymorphic_param_type(PolySolveResult* resolved, AstFunct
                             }
 
                             if (all_types) {
-                                typed_param = try_lookup_based_on_partial_function_type((AstPolyProc *) potential, ft);
+                                typed_param = try_lookup_based_on_partial_function_type((AstFunction *) potential, ft);
                             }
                         }
                     }
@@ -645,7 +646,7 @@ TypeMatch find_polymorphic_sln(AstPolySolution *out, AstPolyParam *param, AstFun
 // NOTE: The job of this function is to take a polymorphic procedure, as well as a method of
 // solving for the polymorphic variables, in order to return an array of the solutions for all
 // of the polymorphic variables.
-static bh_arr(AstPolySolution) find_polymorphic_slns(AstPolyProc* pp, PolyProcLookupMethod pp_lookup, ptr actual, OnyxToken *tkn, b32 necessary) {
+static bh_arr(AstPolySolution) find_polymorphic_slns(AstFunction* pp, PolyProcLookupMethod pp_lookup, ptr actual, OnyxToken *tkn, b32 necessary) {
     ensure_polyproc_cache_is_created(pp);
     if (bh_imap_has(&pp->active_queries, (u64) actual)) {
         AstPolyQuery *query = (AstPolyQuery *) bh_imap_get(&pp->active_queries, (u64) actual);
@@ -674,7 +675,7 @@ static bh_arr(AstPolySolution) find_polymorphic_slns(AstPolyProc* pp, PolyProcLo
     query->given = actual;
     query->error_loc = tkn;
     query->slns = slns;
-    query->function_header = clone_function_header(context.ast_alloc, pp->base_func);
+    query->function_header = clone_function_header(context.ast_alloc, pp);
     query->function_header->flags |= Ast_Flag_Header_Check_No_Error;
     query->function_header->scope = NULL;
     query->error_on_fail = necessary;
@@ -690,7 +691,7 @@ static bh_arr(AstPolySolution) find_polymorphic_slns(AstPolyProc* pp, PolyProcLo
 // NOTE: The job of this function is to be a wrapper to other functions, providing an error
 // message if a solution could not be found. This can't be merged with polymorphic_proc_solidify
 // because polymorphic_proc_try_solidify uses the aforementioned function.
-AstFunction* polymorphic_proc_lookup(AstPolyProc* pp, PolyProcLookupMethod pp_lookup, ptr actual, OnyxToken* tkn) {
+AstFunction* polymorphic_proc_lookup(AstFunction* pp, PolyProcLookupMethod pp_lookup, ptr actual, OnyxToken* tkn) {
     ensure_polyproc_cache_is_created(pp);
 
     bh_arr(AstPolySolution) slns = find_polymorphic_slns(pp, pp_lookup, actual, tkn, 1);
@@ -707,7 +708,7 @@ AstFunction* polymorphic_proc_lookup(AstPolyProc* pp, PolyProcLookupMethod pp_lo
     return result;
 }
 
-AstFunction* polymorphic_proc_solidify(AstPolyProc* pp, bh_arr(AstPolySolution) slns, OnyxToken* tkn) {
+AstFunction* polymorphic_proc_solidify(AstFunction* pp, bh_arr(AstPolySolution) slns, OnyxToken* tkn) {
     ensure_polyproc_cache_is_created(pp);
 
     // NOTE: Check if a version of this polyproc has already been created.
@@ -739,9 +740,9 @@ AstFunction* polymorphic_proc_solidify(AstPolyProc* pp, bh_arr(AstPolySolution)
     return (AstFunction *) &node_that_signals_a_yield;
 }
 
-// NOTE: This can return either a AstFunction or an AstPolyProc, depending if enough parameters were
+// NOTE: This can return either a AstFunction or an AstFunction, depending if enough parameters were
 // supplied to remove all the polymorphic variables from the function.
-AstNode* polymorphic_proc_try_solidify(AstPolyProc* pp, bh_arr(AstPolySolution) slns, OnyxToken* tkn) {
+AstNode* polymorphic_proc_try_solidify(AstFunction* pp, bh_arr(AstPolySolution) slns, OnyxToken* tkn) {
     i32 valid_argument_count = 0;
 
     bh_arr_each(AstPolySolution, sln, slns) {
@@ -771,10 +772,9 @@ AstNode* polymorphic_proc_try_solidify(AstPolyProc* pp, bh_arr(AstPolySolution)
         // HACK: Some of these initializations assume that the entity for this polyproc has
         // made it through the symbol resolution phase.
         //                                                    - brendanfh 2020/12/25
-        AstPolyProc* new_pp = onyx_ast_node_new(context.ast_alloc, sizeof(AstPolyProc), Ast_Kind_Polymorphic_Proc);
+        AstFunction* new_pp = onyx_ast_node_new(context.ast_alloc, sizeof(AstFunction), Ast_Kind_Polymorphic_Proc);
+        memcpy(new_pp, pp, sizeof(AstFunction));
         new_pp->token = tkn;
-        new_pp->base_func = pp->base_func;
-        new_pp->flags = pp->flags;
         new_pp->poly_params = bh_arr_copy(context.ast_alloc, pp->poly_params);
 
         ensure_polyproc_cache_is_created(pp);
@@ -790,7 +790,7 @@ AstNode* polymorphic_proc_try_solidify(AstPolyProc* pp, bh_arr(AstPolySolution)
     }
 }
 
-AstFunction* polymorphic_proc_build_only_header(AstPolyProc* pp, PolyProcLookupMethod pp_lookup, ptr actual) {
+AstFunction* polymorphic_proc_build_only_header(AstFunction* pp, PolyProcLookupMethod pp_lookup, ptr actual) {
     ensure_polyproc_cache_is_created(pp);
     bh_arr(AstPolySolution) slns = find_polymorphic_slns(pp, pp_lookup, actual, NULL, 0);
     if (flag_to_yield) {
@@ -804,7 +804,7 @@ AstFunction* polymorphic_proc_build_only_header(AstPolyProc* pp, PolyProcLookupM
     return polymorphic_proc_build_only_header_with_slns(pp, slns, 0);
 }
 
-AstFunction* polymorphic_proc_build_only_header_with_slns(AstPolyProc* pp, bh_arr(AstPolySolution) slns, b32 error_if_failed) {
+AstFunction* polymorphic_proc_build_only_header_with_slns(AstFunction* pp, bh_arr(AstPolySolution) slns, b32 error_if_failed) {
     AstSolidifiedFunction solidified_func;
 
     char* unique_key = build_poly_slns_unique_key(slns);
index c095b622e8626bef5cf049d88490d17cbb09d2fd..91f89e880f9945d3f9c804aae439b493ae979525 100644 (file)
@@ -758,7 +758,7 @@ static SymresStatus symres_directive_solidify(AstDirectiveSolidify** psolid) {
 
     SYMRES(expression, (AstTyped **) &solid->poly_proc);
     if (solid->poly_proc && solid->poly_proc->kind == Ast_Kind_Directive_Solidify) {
-        AstPolyProc* potentially_resolved_proc = (AstPolyProc *) ((AstDirectiveSolidify *) solid->poly_proc)->resolved_proc;
+        AstFunction* potentially_resolved_proc = (AstFunction *) ((AstDirectiveSolidify *) solid->poly_proc)->resolved_proc;
         if (!potentially_resolved_proc) return Symres_Yield_Micro;
 
         solid->poly_proc = potentially_resolved_proc;
@@ -1185,9 +1185,9 @@ static SymresStatus symres_struct_defaults(AstType* t) {
     return Symres_Success;
 }
 
-static SymresStatus symres_polyproc(AstPolyProc* pp) {
+static SymresStatus symres_polyproc(AstFunction* pp) {
     pp->flags |= Ast_Flag_Comptime;
-    pp->poly_scope = curr_scope;
+    pp->parent_scope_of_poly_proc = curr_scope;
     return Symres_Success;
 }
 
@@ -1301,7 +1301,7 @@ static SymresStatus symres_macro(AstMacro* macro) {
         SYMRES(function_header, (AstFunction *) macro->body);
     }
     else if (macro->body->kind == Ast_Kind_Polymorphic_Proc) {
-        SYMRES(polyproc, (AstPolyProc *) macro->body);
+        SYMRES(polyproc, (AstFunction *) macro->body);
     }
 
     macro->flags |= Ast_Flag_Comptime;
@@ -1338,7 +1338,7 @@ static SymresStatus symres_polyquery(AstPolyQuery *query) {
     query->successful_symres = 0;
 
     if (query->function_header->scope == NULL)
-        query->function_header->scope = scope_create(context.ast_alloc, query->proc->poly_scope, query->token->pos);
+        query->function_header->scope = scope_create(context.ast_alloc, query->proc->parent_scope_of_poly_proc, query->token->pos);
 
     scope_enter(query->function_header->scope);
 
index 1f0b32daa581f5581a9047c39f79b70799ccfcec..51436c6d375c5c34cd70d934d729af6196cb3292 100644 (file)
@@ -390,7 +390,7 @@ AstTyped* find_matching_overload_by_arguments(bh_arr(OverloadOption) overloads,
         AstFunction* overload = NULL;
         switch (node->kind) {
             case Ast_Kind_Macro:            overload = macro_resolve_header((AstMacro *) node, param_args, NULL, 0); break;
-            case Ast_Kind_Polymorphic_Proc: overload = polymorphic_proc_build_only_header((AstPolyProc *) node, PPLM_By_Arguments, param_args); break;
+            case Ast_Kind_Polymorphic_Proc: overload = polymorphic_proc_build_only_header((AstFunction *) node, PPLM_By_Arguments, param_args); break;
             case Ast_Kind_Function:
                 overload = (AstFunction *) node;
                 arguments_clear_baked_flags(&args);
@@ -562,7 +562,7 @@ AstFunction* macro_resolve_header(AstMacro* macro, Arguments* args, OnyxToken* c
         case Ast_Kind_Function: return (AstFunction *) macro->body;
 
         case Ast_Kind_Polymorphic_Proc: {
-            AstPolyProc* pp = (AstPolyProc *) macro->body;
+            AstFunction* pp = (AstFunction *) macro->body;
             ensure_polyproc_cache_is_created(pp);
 
             bh_arr(AstPolySolution) slns = find_polymorphic_slns(pp, PPLM_By_Arguments, args, callsite, error_if_failed);
@@ -1125,7 +1125,7 @@ all_types_peeled_off:
 
         case Ast_Kind_Poly_Call_Type: {
             AstPolyCallType* pcall = (AstPolyCallType *) node;
-            return find_closest_symbol_in_node(pcall->callee, sym);
+            return find_closest_symbol_in_node((AstNode *) pcall->callee, sym);
         }
     }