diff --git a/core_ast.cpp b/core_ast.cpp index ac4acfc..8c3caa4 100644 --- a/core_ast.cpp +++ b/core_ast.cpp @@ -20,6 +20,39 @@ _ast_new(size_t size, Ast_Kind kind, Token *pos, Ast_Flag flags = 0) { return result; } +CORE_Static void +set_flag_typespec(Ast_Expr *expr) { + if (!expr) return; + set_flag(expr->flags, AST_TYPESPEC); + + switch (expr->kind) { + case AST_UNARY: { + Ast_Unary *n = (Ast_Unary *)expr; + set_flag_typespec(n->expr); + } break; + case AST_ARRAY: { + Ast_Array *n = (Ast_Array *)expr; + set_flag_typespec(n->base); + set_flag_typespec(n->expr); + } break; + + case AST_CALL: { + Ast_Call *n = (Ast_Call *)expr; + For(n->exprs) set_flag_typespec(it); + set_flag_typespec(n->name); + } break; + case AST_CALL_ITEM: { + Ast_Call_Item *n = (Ast_Call_Item *)expr; + set_flag_typespec(n->item); + set_flag_typespec(n->index); + } break; + + case AST_IDENT: + break; + invalid_default_case; + } +} + CORE_Static void propagate_polymorphic(Ast *to, Ast *from) { if (is_flag_set(from->flags, AST_IDENT_POLYMORPH)) set_flag(to->flags, AST_IDENT_POLYMORPH); @@ -230,8 +263,11 @@ ast_struct(Token *pos, Ast_Scope *scope, Ast_Kind kind, Array polymo AST_NEW(Decl, STRUCT, pos, AST_DECL | AST_AGGREGATE); result->kind = kind; result->scope = scope; - result->polymorph_parameters = polymorph_parameters; - if (polymorph_parameters.len) set_flag(result->flags, AST_POLYMORPH); + if (polymorph_parameters.len) { + result->polymorph_parameters = polymorph_parameters; + set_flag(result->flags, AST_POLYMORPH); + result->polymorphs.allocator = pctx->heap; + } return result; } @@ -240,7 +276,7 @@ ast_enum(Token *pos, Ast_Expr *typespec, Ast_Scope *scope) { AST_NEW(Decl, ENUM, pos, AST_DECL); result->scope = scope; result->typespec = typespec; - if (typespec) set_flag(typespec->flags, AST_TYPESPEC); + set_flag_typespec(typespec); return result; } @@ -249,7 +285,7 @@ ast_var(Token *pos, Ast_Expr *typespec, Intern_String name, Ast_Expr *expr) { AST_NEW(Decl, VAR, pos, AST_DECL); result->name = name; result->typespec = typespec; - if (typespec) set_flag(typespec->flags, AST_TYPESPEC); + set_flag_typespec(typespec); result->expr = expr; return result; } diff --git a/core_compiler_interface.hpp b/core_compiler_interface.hpp index 29ce74c..6582e9b 100644 --- a/core_compiler_interface.hpp +++ b/core_compiler_interface.hpp @@ -606,6 +606,7 @@ struct Ast_Decl : Ast { uint64_t operator_overload_arguments_hash; Ast_Operator_Info *overload_op_info; + uint64_t polymorph_hash; Array polymorph_parameters; Array polymorphs; // instantiated polymorphs diff --git a/core_parsing.cpp b/core_parsing.cpp index 49d2e08..bb81467 100644 --- a/core_parsing.cpp +++ b/core_parsing.cpp @@ -476,7 +476,7 @@ parse_parameter_list(Arena *arena) { Ast_Decl *param = ast_new(Ast_Decl, AST_VAR, name, AST_DECL); param->name = name->intern_val; param->typespec = parse_expr(); - set_flag(param->typespec->flags, AST_TYPESPEC); + set_flag_typespec(param->typespec); propagate_polymorphic(param, param->typespec); propagate_polymorphic(param, name); @@ -506,7 +506,8 @@ parse_lambda(Token *token) { if (token_match(TK_Colon)) { do { Ast_Expr *typespec = parse_expr(); - set_flag(typespec->flags, AST_TYPESPEC); + set_flag_typespec(typespec); + ret.add(typespec); } while (token_match(TK_Comma)); } @@ -743,7 +744,7 @@ parse_struct(Token *pos, Ast_Kind kind) { token_expect(TK_Colon); Ast_Expr *typespec = parse_expr(); - set_flag(typespec->flags, AST_TYPESPEC); + set_flag_typespec(typespec); Ast_Decl *decl = ast_var(token, typespec, token->intern_val, 0); set_flag(decl->flags, AST_AGGREGATE_CHILD); @@ -967,7 +968,8 @@ parse_decl(B32 is_global) { else if (token_match(TK_Identifier, TK_Colon)) { Ast_Expr *typespec = parse_expr(); - set_flag(typespec->flags, AST_TYPESPEC); + set_flag_typespec(typespec); + Ast_Expr *expr = parse_assign_expr(); if (token_match_pound(pctx->intern_foreign)) set_flag(flags, AST_FOREIGN); diff --git a/core_polymorph.cpp b/core_polymorph.cpp index 24720a3..6db8659 100644 --- a/core_polymorph.cpp +++ b/core_polymorph.cpp @@ -1,22 +1,5 @@ -Ast_Decl *get_or_instantiate_polymorph_type(Token *pos, Ast_Decl *poly, Array params, Ast_Scope *field_access_scope) { - if (params.len != poly->polymorph_parameters.len) { - compiler_error(pos, "Invalid count of polymorphic arguments"); - } - For(params) { - bool indexed = (it->flags & CALL_INDEX); - bool named = (it->flags & CALL_NAME); - if (indexed == false && named == false) { - compiler_error(it->pos, "Polymorphic type cannot have named/indexed arguments"); - } - - Operand op = resolve_expr(it->item, AST_CANT_BE_NULL, 0, field_access_scope); - } - - return 0; -} - #define ast_create_copy(parent_scope, T, ast) (T *)ast__create_copy(parent_scope, sizeof(T), ast) Ast *ast__create_copy(Ast_Scope *parent_scope, size_t size, Ast *ast) { if (ast == 0) return 0; @@ -28,7 +11,7 @@ Ast *ast__create_copy(Ast_Scope *parent_scope, size_t size, Ast *ast) { // We are not copying module and file Ast's // We are not copying resolved data -Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) { +Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array *replace, Array *with) { if (!ast) return 0; switch (ast->kind) { case AST_SCOPE: { @@ -37,13 +20,13 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) { dst->decls = {}; For(src->decls) { - Ast_Decl *copy = (Ast_Decl *)ast_copy(it, src); + Ast_Decl *copy = (Ast_Decl *)ast_copy(it, src, replace, with); add(pctx->perm, &dst->decls, copy); } dst->stmts.init(pctx->perm, src->stmts.len); For(src->stmts) { - Ast *copy = ast_copy(it, dst); + Ast *copy = ast_copy(it, dst, replace, with); dst->stmts.add(copy); } return dst; @@ -52,7 +35,20 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) { case AST_MODULE: invalid_codepath; break; case AST_FILE: invalid_codepath; break; - case AST_IDENT: + case AST_IDENT: { + Ast_Atom *src = (Ast_Atom *)ast; + Ast_Atom *dst = ast_create_copy(parent_scope, Ast_Atom, ast); + + if ((dst->flags & AST_TYPESPEC)) { + For(*replace) { + if (it->name == dst->intern_val) { + Breakpoint; + } + } + } + + return dst; + } break; case AST_VALUE: { Ast_Atom *src = (Ast_Atom *)ast; Ast_Atom *dst = ast_create_copy(parent_scope, Ast_Atom, ast); @@ -62,35 +58,35 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) { case AST_INDEX: { Ast_Index *src = (Ast_Index *)ast; Ast_Index *dst = ast_create_copy(parent_scope, Ast_Index, ast); - dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope); - dst->index = (Ast_Expr *)ast_copy(src->index, parent_scope); + dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with); + dst->index = (Ast_Expr *)ast_copy(src->index, parent_scope, replace, with); return dst; } break; case AST_UNARY: { Ast_Unary *src = (Ast_Unary *)ast; Ast_Unary *dst = ast_create_copy(parent_scope, Ast_Unary, ast); - dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope); + dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with); return dst; } break; case AST_BINARY: { Ast_Binary *src = (Ast_Binary *)ast; Ast_Binary *dst = ast_create_copy(parent_scope, Ast_Binary, ast); - dst->left = (Ast_Expr *)ast_copy(src->left, parent_scope); - dst->right = (Ast_Expr *)ast_copy(src->right, parent_scope); + dst->left = (Ast_Expr *)ast_copy(src->left, parent_scope, replace, with); + dst->right = (Ast_Expr *)ast_copy(src->right, parent_scope, replace, with); return dst; } break; case AST_CALL_ITEM: { Ast_Call_Item *src = (Ast_Call_Item *)ast; Ast_Call_Item *dst = ast_create_copy(parent_scope, Ast_Call_Item, ast); - dst->item = (Ast_Expr *)ast_copy(src->item, parent_scope); + dst->item = (Ast_Expr *)ast_copy(src->item, parent_scope, replace, with); if (src->call_flags & CALL_INDEX) { - dst->index = (Ast_Expr *)ast_copy(src->index, parent_scope); + dst->index = (Ast_Expr *)ast_copy(src->index, parent_scope, replace, with); } else if (src->call_flags & CALL_NAME) { - dst->name = (Ast_Atom *)ast_copy(src->name, parent_scope); + dst->name = (Ast_Atom *)ast_copy(src->name, parent_scope, replace, with); } return dst; } break; @@ -99,11 +95,11 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) { case AST_CALL: { Ast_Call *src = (Ast_Call *)ast; Ast_Call *dst = ast_create_copy(parent_scope, Ast_Call, ast); - dst->name = (Ast_Expr *)ast_copy(src->name, parent_scope); + dst->name = (Ast_Expr *)ast_copy(src->name, parent_scope, replace, with); dst->exprs.init(pctx->perm, src->exprs.len); For(dst->exprs) { - auto copy = (Ast_Call_Item *)ast_copy(it, parent_scope); + auto copy = (Ast_Call_Item *)ast_copy(it, parent_scope, replace, with); dst->exprs.add(copy); } return dst; @@ -117,19 +113,19 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) { case AST_CONSTANT_ASSERT: { Ast_Builtin *src = (Ast_Builtin *)ast; Ast_Builtin *dst = ast_create_copy(parent_scope, Ast_Builtin, ast); - dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope); + dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with); return dst; } break; case AST_SWITCH: { Ast_Switch *src = (Ast_Switch *)ast; Ast_Switch *dst = ast_create_copy(parent_scope, Ast_Switch, ast); - dst->value = (Ast_Expr *)ast_copy(src->value, parent_scope); - dst->default_scope = (Ast_Scope *)ast_copy(src->default_scope, parent_scope); + dst->value = (Ast_Expr *)ast_copy(src->value, parent_scope, replace, with); + dst->default_scope = (Ast_Scope *)ast_copy(src->default_scope, parent_scope, replace, with); dst->cases.init(pctx->perm, src->cases.len); For(src->cases) { - auto copy = (Ast_Switch_Case *)ast_copy(it, parent_scope); + auto copy = (Ast_Switch_Case *)ast_copy(it, parent_scope, replace, with); assert(copy->kind == AST_SWITCH_CASE); dst->cases.add(copy); } @@ -140,10 +136,10 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) { Ast_Switch_Case *src = (Ast_Switch_Case *)ast; Ast_Switch_Case *dst = ast_create_copy(parent_scope, Ast_Switch_Case, ast); - dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope); + dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, replace, with); dst->labels.init(pctx->perm, src->labels.len); For(src->labels) { - auto copy = (Ast_Expr *)ast_copy(it, parent_scope); + auto copy = (Ast_Expr *)ast_copy(it, parent_scope, replace, with); dst->labels.add(copy); } return dst; @@ -153,10 +149,10 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) { Ast_Var_Unpack *src = (Ast_Var_Unpack *)ast; Ast_Var_Unpack *dst = ast_create_copy(parent_scope, Ast_Var_Unpack, ast); - dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope); + dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with); dst->vars.init(pctx->perm, src->vars.len); For(src->vars) { - auto copy = (Ast_Decl *)ast_copy(it, parent_scope); + auto copy = (Ast_Decl *)ast_copy(it, parent_scope, replace, with); dst->vars.add(copy); } return dst; @@ -182,26 +178,26 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) { // dst->overload_op_info = ast_create_copy(parent_scope, Ast_Operator_Info, src->overload_op_info); // omitting polymorphs // omitting polymorph parameters - dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope); - dst->typespec = (Ast_Expr *)ast_copy(src->typespec, parent_scope); - dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope); + dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, replace, with); + dst->typespec = (Ast_Expr *)ast_copy(src->typespec, parent_scope, replace, with); + dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with); return dst; } break; case AST_ARRAY: { Ast_Array *src = (Ast_Array *)ast; Ast_Array *dst = ast_create_copy(parent_scope, Ast_Array, ast); - dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope); + dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with); return dst; } break; case AST_FOR: { Ast_For *src = (Ast_For *)ast; Ast_For *dst = ast_create_copy(parent_scope, Ast_For, ast); - dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope); - dst->init = (Ast_Expr *)ast_copy(src->init, parent_scope); - dst->cond = (Ast_Expr *)ast_copy(src->cond, parent_scope); - dst->iter = (Ast_Expr *)ast_copy(src->iter, parent_scope); + dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, replace, with); + dst->init = (Ast_Expr *)ast_copy(src->init, parent_scope, replace, with); + dst->cond = (Ast_Expr *)ast_copy(src->cond, parent_scope, replace, with); + dst->iter = (Ast_Expr *)ast_copy(src->iter, parent_scope, replace, with); return dst; } break; @@ -210,7 +206,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) { Ast_If *dst = ast_create_copy(parent_scope, Ast_If, ast); dst->ifs.init(pctx->perm, src->ifs.len); For(src->ifs) { - auto copy = (Ast_If_Node *)ast_copy(it, parent_scope); + auto copy = (Ast_If_Node *)ast_copy(it, parent_scope, replace, with); assert(copy->kind == AST_IF_NODE); dst->ifs.add(copy); } @@ -220,9 +216,9 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) { case AST_IF_NODE: { Ast_If_Node *src = (Ast_If_Node *)ast; Ast_If_Node *dst = ast_create_copy(parent_scope, Ast_If_Node, ast); - dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope); - dst->init = (Ast_Binary *)ast_copy(src->init, parent_scope); - dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope); + dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with); + dst->init = (Ast_Binary *)ast_copy(src->init, parent_scope, replace, with); + dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, replace, with); return dst; } break; @@ -232,7 +228,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) { dst->expr.init(pctx->perm, src->expr.len); For(src->expr) { - auto copy = (Ast_Expr *)ast_copy(it, parent_scope); + auto copy = (Ast_Expr *)ast_copy(it, parent_scope, replace, with); dst->expr.add(copy); } return dst; @@ -244,16 +240,16 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) { dst->args.init(pctx->perm, src->args.len); For(src->args) { - auto copy = (Ast_Decl *)ast_copy(it, parent_scope); + auto copy = (Ast_Decl *)ast_copy(it, parent_scope, replace, with); dst->args.add(copy); } dst->ret.init(pctx->perm, src->ret.len); For(src->ret) { - auto copy = (Ast_Expr *)ast_copy(it, parent_scope); + auto copy = (Ast_Expr *)ast_copy(it, parent_scope, replace, with); dst->ret.add(copy); } - dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope); + dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, replace, with); return dst; } break; @@ -262,7 +258,46 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) { invalid_return; } +Ast_Decl *get_or_instantiate_polymorph_type(Token *pos, Ast_Decl *poly, Array params, Ast_Scope *field_access_scope) { + if (params.len != poly->polymorph_parameters.len) compiler_error(pos, "Invalid count of polymorphic arguments"); + + int i = 0; + uint64_t hash = 91; + For(params) { + bool indexed = (it->flags & CALL_INDEX); + bool named = (it->flags & CALL_NAME); + if (indexed == false && named == false) compiler_error(it->pos, "Polymorphic type cannot have named/indexed arguments"); + + Ast_Decl *poly_decl = poly->polymorph_parameters[i++]; + resolve_decl(poly_decl); + if (poly_decl->type != pctx->type_type) compiler_error(poly_decl->pos, "Invalid type of polymorphic struct argument"); + + Operand op = resolve_expr(it->item, AST_CANT_BE_NULL, 0, field_access_scope); + if (!op.is_const) compiler_error(it->pos, "Argument is required to be compile time known"); + if (op.type != pctx->type_type) compiler_error(it->pos, "Struct argument required to be a type"); + + hash = hash_mix(hash, hash_ptr(op.type_val)); + } + + Ast_Decl *result = 0; + For(poly->polymorphs) { + if (it->polymorph_hash == hash) { + result = it; + break; + } + } + + if (!result) { + result = (Ast_Decl *)ast_copy(poly, poly->parent_scope, &poly->polymorph_parameters, ¶ms); + poly->polymorphs.add(result); + } + + return result; +} + +// // ITERATOR +// const unsigned AST_NODE_END = 128;