src/onyxutils.c
// NOTE: This can return either a AstFunction or an AstPolyProc, depending if enough parameters were
// supplied to remove all the polymorphic variables from the function.
This can return either a AstFunction or an AstPolyProc, depending if enough parameters were
supplied to remove all the polymorphic variables from the function.
This can return either a AstFunction or an AstPolyProc 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, OnyxFilePos pos) {
    i32 valid_argument_count = 0;
    bh_arr_each(AstPolySolution, sln, slns) {
        b32 found_match = 0;
        bh_arr_each(AstPolyParam, param, pp->poly_params) {
            if (token_equals(sln->poly_sym->token, param->poly_sym->token)) {
                found_match = 1;
                break;
            }
        }
        if (found_match) {
            valid_argument_count++;
        } else {
            onyx_report_error(pos, "'%b' is not a type variable of '%b'.",
                sln->poly_sym->token->text, sln->poly_sym->token->length,
                pp->token->text, pp->token->length);
            return (AstNode *) pp;
        }
    }
    if (valid_argument_count == bh_arr_length(pp->poly_params)) {
        return (AstNode *) polymorphic_proc_solidify(pp, slns, pos);
    } else {
        AstPolyProc* new_pp = onyx_ast_node_new(semstate.node_allocator, sizeof(AstPolyProc), Ast_Kind_Polymorphic_Proc);
        new_pp->token = pp->token;                            
        new_pp->base_func = pp->base_func;
        new_pp->poly_scope = new_pp->poly_scope;
        new_pp->flags = pp->flags;
        new_pp->poly_params = pp->poly_params;
        if (pp->concrete_funcs == NULL) {
            bh_table_init(global_heap_allocator, pp->concrete_funcs, 8);
        }
        new_pp->concrete_funcs = pp->concrete_funcs;
        new_pp->known_slns = NULL;
        bh_arr_new(global_heap_allocator, new_pp->known_slns, bh_arr_length(pp->known_slns) + bh_arr_length(slns));
        bh_arr_each(AstPolySolution, sln, pp->known_slns) bh_arr_push(new_pp->known_slns, *sln);
        bh_arr_each(AstPolySolution, sln, slns)           bh_arr_push(new_pp->known_slns, *sln);
        return (AstNode *) new_pp;
    }
}
AstNode * polymorphic_proc_try_solidify ( AstPolyProc * pp , bh_arr ( AstPolySolution ) slns , OnyxFilePos pos ) { i32 valid_argument_count = 0 ; bh_arr_each ( AstPolySolution , sln , slns ) { b32 found_match = 0 ; bh_arr_each ( AstPolyParam , param , pp -> poly_params ) { if ( token_equals ( sln -> poly_sym -> token , param -> poly_sym -> token ) ) { found_match = 1 ; break ; } } if ( found_match ) { valid_argument_count ++ ; } else { onyx_report_error ( pos , " " , sln -> poly_sym -> token -> text , sln -> poly_sym -> token -> length , pp -> token -> text , pp -> token -> length ) ; return ( AstNode * ) pp ; } } if ( valid_argument_count == bh_arr_length ( pp -> poly_params ) ) { return ( AstNode * ) polymorphic_proc_solidify ( pp , slns , pos ) ; } else { AstPolyProc * new_pp = onyx_ast_node_new ( semstate . node_allocator , sizeof ( AstPolyProc ) , Ast_Kind_Polymorphic_Proc ) ; new_pp -> token = pp -> token ; new_pp -> base_func = pp -> base_func ; new_pp -> poly_scope = new_pp -> poly_scope ; new_pp -> flags = pp -> flags ; new_pp -> poly_params = pp -> poly_params ; if ( pp -> concrete_funcs == NULL ) { bh_table_init ( global_heap_allocator , pp -> concrete_funcs , 8 ) ; } new_pp -> concrete_funcs = pp -> concrete_funcs ; new_pp -> known_slns = NULL ; bh_arr_new ( global_heap_allocator , new_pp -> known_slns , bh_arr_length ( pp -> known_slns ) + bh_arr_length ( slns ) ) ; bh_arr_each ( AstPolySolution , sln , pp -> known_slns ) bh_arr_push ( new_pp -> known_slns , * sln ) ; bh_arr_each ( AstPolySolution , sln , slns ) bh_arr_push ( new_pp -> known_slns , * sln ) ; return ( AstNode * ) new_pp ; } }
NOTE: This can return either a AstFunction or an AstPolyProc, depending if enough parameters were
supplied to remove all the polymorphic variables from the function.
NOTE : This can return either a AstFunction or an AstPolyProc depending if enough parameters were supplied to remove all the polymorphic variables from the function .
// HACK: Some of these initializations assume that the entity for this polyproc has // made it through the symbol resolution phase. //                                                    - brendanfh 2020/12/25 // TODO: Change this to be the solidify->token // POTENTIAL BUG: Copying this doesn't feel right...