Initial polymorph seeking works!

This commit is contained in:
Krzosa Karol
2023-04-03 16:15:50 +02:00
parent b543e1df9d
commit 39f4b081a8
4 changed files with 167 additions and 77 deletions

View File

@@ -66,6 +66,10 @@ Ast_Expr *create_typespec_from_type(Token *pos, Ast_Scope *parent_scope, Ast_Typ
case TYPE_STRUCT:
case TYPE_UNION:
case TYPE_ENUM: {
// @consider: Maybe instead of using AST_IDENT another case should be
// added like AST_RESOLVED or something because currently the identifier
// is a bit bogus.
// We fill out the resolved_decl here because the typespecs for
// polymorphic types can be really complex and very context dependent.
// For example for a namespaced polymorph we would need to figure out the
@@ -89,6 +93,70 @@ Ast_Expr *create_typespec_from_type(Token *pos, Ast_Scope *parent_scope, Ast_Typ
return result;
}
const int POLY_TYPE_REPLACEMENT = 0;
const int POLY_AST_REPLACEMENT = 1;
struct Poly_Replacement {
int kind;
Intern_String poly_roli;
Ast_Type *type;
Ast *ast;
};
CORE_Static void
extract_polymorph_type(Array<Poly_Replacement> *polys, Ast_Expr *polymorph_typespec, Ast_Type *type) {
assert(polymorph_typespec->flags & AST_TYPESPEC);
switch (polymorph_typespec->kind) {
case AST_UNARY: {
Ast_Unary *n = (Ast_Unary *)polymorph_typespec;
assert(n->op == TK_Pointer);
if (type->kind != TYPE_POINTER) {
compiler_error(polymorph_typespec->pos, "Passed in value's type doesn't match the polymorphic type");
return;
}
extract_polymorph_type(polys, n->expr, type->base);
} break;
case AST_ARRAY: {
Ast_Array *n = (Ast_Array *)polymorph_typespec;
if (type->kind != TYPE_ARRAY && type->kind != TYPE_SLICE) {
compiler_error(polymorph_typespec->pos, "Passed in value's type doesn't match the polymorphic type");
return;
}
extract_polymorph_type(polys, n->base, type->base);
} break;
case AST_CALL: {
Ast_Call *n = (Ast_Call *)polymorph_typespec;
bool is_aggregate = type->kind == TYPE_STRUCT || type->kind == TYPE_UNION;
bool is_poly = type->ast->flags & AST_POLYMORPH_INSTANCE;
if (!is_aggregate || !is_poly) {
compiler_error(polymorph_typespec->pos, "Passed in value's type doesn't match the polymorphic type");
return;
}
Ast_Decl *decl = (Ast_Decl *)type->ast;
if (decl->polymorph_resolved_parameter_types.len != n->exprs.len) {
compiler_error(polymorph_typespec->pos, "Different types, the polymorph has different number of type arguments");
return;
}
for (int i = 0; i < n->exprs.len; i += 1) {
Ast_Type *t = decl->polymorph_resolved_parameter_types[i];
Ast_Call_Item *spec = n->exprs[i];
extract_polymorph_type(polys, spec->item, t);
}
} break;
case AST_IDENT: {
Ast_Atom *n = (Ast_Atom *)polymorph_typespec;
bool is_poly = n->flags & AST_POLYMORPH;
if (is_poly) polys->add({POLY_TYPE_REPLACEMENT, n->intern_val, type});
} break;
invalid_default_case;
}
}
#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;
@@ -100,7 +168,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, Array<Ast_Decl *> *replace, Array<Ast_Call_Item *> *with) {
Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Poly_Replacement> *repl) {
if (!ast) return 0;
Ast *result = 0;
switch (ast->kind) {
@@ -110,13 +178,13 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
dst->decls = {};
For(src->decls) {
Ast_Decl *copy = (Ast_Decl *)ast_copy(it, src, replace, with);
Ast_Decl *copy = (Ast_Decl *)ast_copy(it, src, repl);
add(pctx->perm, &dst->decls, copy);
}
dst->stmts.init(pctx->perm, src->stmts.len);
For(src->stmts) {
Ast *copy = ast_copy(it, dst, replace, with);
Ast *copy = ast_copy(it, dst, repl);
dst->stmts.add(copy);
}
result = dst;
@@ -129,36 +197,17 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
Ast_Atom *src = (Ast_Atom *)ast;
Ast_Atom *dst = ast_create_copy(parent_scope, Ast_Atom, ast);
if (replace && with) {
if (repl) {
// @todo: IF ITS STRUCT we only want to replace TYPESPECS
For(*replace) {
if (it->flags & AST_IDENT_POLYMORPH) {
assert(it->type == pctx->type_type);
if (it->name == dst->intern_val) {
S64 it_index = replace->get_index(it);
Ast_Call_Item *replacement = with[0][it_index];
Ast *replacement_v = replacement->item;
dst = (Ast_Atom *)ast_copy(replacement_v, parent_scope, 0, 0);
}
For(*repl) {
if (it.poly_roli != dst->intern_val) continue;
if (it.kind == POLY_AST_REPLACEMENT) {
dst = (Ast_Atom *)ast_copy(it.ast, parent_scope, 0);
}
else if (it->flags & AST_TYPE_POLYMORPH) {
Ast_Expr *typespec = it->typespec;
assert(typespec);
assert(typespec->kind == AST_IDENT);
Ast_Atom *t = (Ast_Atom *)typespec;
if (t->intern_val == dst->intern_val) {
S64 it_index = replace->get_index(it);
// @todo: This probably should be resolved
// before calling the ast_copy function
// or something
Ast_Call_Item *replacement = with[0][it_index];
Ast_Type *type_to_replace = replacement->item->resolved_type;
dst = (Ast_Atom *)create_typespec_from_type(dst->pos, parent_scope, type_to_replace);
// dst = (Ast_Atom *)ast_copy(replacement_v, parent_scope, 0, 0);
}
else if (it.kind == POLY_TYPE_REPLACEMENT) {
dst = (Ast_Atom *)create_typespec_from_type(dst->pos, parent_scope, it.type);
}
else invalid_codepath;
}
}
@@ -173,35 +222,35 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
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, replace, with);
dst->index = (Ast_Expr *)ast_copy(src->index, parent_scope, replace, with);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, repl);
dst->index = (Ast_Expr *)ast_copy(src->index, parent_scope, repl);
result = 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, replace, with);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, repl);
result = 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, replace, with);
dst->right = (Ast_Expr *)ast_copy(src->right, parent_scope, replace, with);
dst->left = (Ast_Expr *)ast_copy(src->left, parent_scope, repl);
dst->right = (Ast_Expr *)ast_copy(src->right, parent_scope, repl);
result = 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, replace, with);
dst->item = (Ast_Expr *)ast_copy(src->item, parent_scope, repl);
if (src->call_flags & CALL_INDEX) {
dst->index = (Ast_Expr *)ast_copy(src->index, parent_scope, replace, with);
dst->index = (Ast_Expr *)ast_copy(src->index, parent_scope, repl);
}
else if (src->call_flags & CALL_NAME) {
dst->name = (Ast_Atom *)ast_copy(src->name, parent_scope, replace, with);
dst->name = (Ast_Atom *)ast_copy(src->name, parent_scope, repl);
}
result = dst;
} break;
@@ -210,11 +259,11 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
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, replace, with);
dst->name = (Ast_Expr *)ast_copy(src->name, parent_scope, repl);
dst->exprs.init(pctx->perm, src->exprs.len);
For(src->exprs) {
auto copy = (Ast_Call_Item *)ast_copy(it, parent_scope, replace, with);
auto copy = (Ast_Call_Item *)ast_copy(it, parent_scope, repl);
dst->exprs.add(copy);
}
result = dst;
@@ -228,19 +277,19 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
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, replace, with);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, repl);
result = 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, replace, with);
dst->default_scope = (Ast_Scope *)ast_copy(src->default_scope, parent_scope, replace, with);
dst->value = (Ast_Expr *)ast_copy(src->value, parent_scope, repl);
dst->default_scope = (Ast_Scope *)ast_copy(src->default_scope, parent_scope, repl);
dst->cases.init(pctx->perm, src->cases.len);
For(src->cases) {
auto copy = (Ast_Switch_Case *)ast_copy(it, parent_scope, replace, with);
auto copy = (Ast_Switch_Case *)ast_copy(it, parent_scope, repl);
assert(copy->kind == AST_SWITCH_CASE);
dst->cases.add(copy);
}
@@ -251,10 +300,10 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
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, replace, with);
dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, repl);
dst->labels.init(pctx->perm, src->labels.len);
For(src->labels) {
auto copy = (Ast_Expr *)ast_copy(it, parent_scope, replace, with);
auto copy = (Ast_Expr *)ast_copy(it, parent_scope, repl);
dst->labels.add(copy);
}
result = dst;
@@ -264,10 +313,10 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
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, replace, with);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, repl);
dst->vars.init(pctx->perm, src->vars.len);
For(src->vars) {
auto copy = (Ast_Decl *)ast_copy(it, parent_scope, replace, with);
auto copy = (Ast_Decl *)ast_copy(it, parent_scope, repl);
dst->vars.add(copy);
}
result = dst;
@@ -293,26 +342,26 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
// 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, 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);
dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, repl);
dst->typespec = (Ast_Expr *)ast_copy(src->typespec, parent_scope, repl);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, repl);
result = 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, replace, with);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, repl);
result = 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, 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);
dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, repl);
dst->init = (Ast_Expr *)ast_copy(src->init, parent_scope, repl);
dst->cond = (Ast_Expr *)ast_copy(src->cond, parent_scope, repl);
dst->iter = (Ast_Expr *)ast_copy(src->iter, parent_scope, repl);
result = dst;
} break;
@@ -321,7 +370,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
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, replace, with);
auto copy = (Ast_If_Node *)ast_copy(it, parent_scope, repl);
assert(copy->kind == AST_IF_NODE);
dst->ifs.add(copy);
}
@@ -331,9 +380,9 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
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, 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);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, repl);
dst->init = (Ast_Binary *)ast_copy(src->init, parent_scope, repl);
dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, repl);
result = dst;
} break;
@@ -343,7 +392,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
dst->expr.init(pctx->perm, src->expr.len);
For(src->expr) {
auto copy = (Ast_Expr *)ast_copy(it, parent_scope, replace, with);
auto copy = (Ast_Expr *)ast_copy(it, parent_scope, repl);
dst->expr.add(copy);
}
result = dst;
@@ -356,16 +405,16 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
dst->args.init(pctx->perm, src->args.len);
For(src->args) {
if (it->flags & AST_IDENT_POLYMORPH) continue;
auto copy = (Ast_Decl *)ast_copy(it, parent_scope, replace, with);
auto copy = (Ast_Decl *)ast_copy(it, parent_scope, repl);
dst->args.add(copy);
}
dst->ret.init(pctx->perm, src->ret.len);
For(src->ret) {
auto copy = (Ast_Expr *)ast_copy(it, parent_scope, replace, with);
auto copy = (Ast_Expr *)ast_copy(it, parent_scope, repl);
dst->ret.add(copy);
}
dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, replace, with);
dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, repl);
result = dst;
} break;
@@ -379,8 +428,14 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
return result;
}
// @todo: check for multiple poly_roli of same name
Ast_Decl *get_or_instantiate_polymorph_type(Token *pos, Ast_Decl *poly, Array<Ast_Call_Item *> params, Ast_Scope *field_access_scope) {
if (params.len != poly->polymorph_parameters.len) compiler_error(pos, "Invalid count of polymorphic arguments");
Scoped_Arena scratch(pctx->scratch);
Array<Ast_Type *> resolved_type_params = {scratch.arena};
Array<Poly_Replacement> repl = {scratch.arena};
int i = 0;
uint64_t hash = 91;
@@ -398,8 +453,12 @@ Ast_Decl *get_or_instantiate_polymorph_type(Token *pos, Ast_Decl *poly, Array<As
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));
resolved_type_params.add(op.type_val);
repl.add({POLY_AST_REPLACEMENT, poly_decl->name, 0, it->item});
}
if (repl.len == 0) compiler_error(pos, "No polymorphic variables found to replace");
Ast_Decl *result = 0;
For(poly->polymorphs) {
if (it->polymorph_hash == hash) {
@@ -408,18 +467,15 @@ Ast_Decl *get_or_instantiate_polymorph_type(Token *pos, Ast_Decl *poly, Array<As
}
}
if (poly->kind == AST_LAMBDA) Breakpoint;
if (!result) {
result = (Ast_Decl *)ast_copy(poly, poly->parent_scope, &poly->polymorph_parameters, &params);
result = (Ast_Decl *)ast_copy(poly, poly->parent_scope, &repl);
result->type_val = type_incomplete(result);
result->polymorph_hash = hash;
result->instantiation_call_items = params.tight_copy(pctx->perm);
assert(result->di != poly->di);
result->polymorph_resolved_parameter_types = resolved_type_params.tight_copy(pctx->perm);
result->unique_name = get_unique_name_for_decl(result);
assert(result->di != poly->di);
poly->polymorphs.allocator = pctx->heap;
if (!poly->polymorphs.allocator) poly->polymorphs.allocator = pctx->heap;
poly->polymorphs.add(result);
}
@@ -432,6 +488,9 @@ Ast_Decl *get_or_instantiate_polymorph_lambda(Token *pos, Ast_Decl *poly, Array<
if (params.len != poly->polymorph_parameters.len) compiler_error(pos, "Invalid count of polymorphic arguments");
Scoped_Arena scratch(pctx->scratch);
Array<Ast_Type *> resolved_type_params = {scratch.arena};
Array<Poly_Replacement> repl = {scratch.arena};
int i = 0;
uint64_t hash = 91;
For(params) {
@@ -445,6 +504,8 @@ Ast_Decl *get_or_instantiate_polymorph_lambda(Token *pos, Ast_Decl *poly, Array<
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));
resolved_type_params.add(op.type_val);
repl.add({POLY_AST_REPLACEMENT, poly_decl->name, 0, it->item});
}
else {
Operand op = resolve_expr(it->item, AST_CANT_BE_NULL, 0, field_access_scope);
@@ -453,10 +514,15 @@ Ast_Decl *get_or_instantiate_polymorph_lambda(Token *pos, Ast_Decl *poly, Array<
// type_complete(op.type);
assert(it->item->resolved_type == op.type);
extract_polymorph_type(&repl, poly_decl->typespec, op.type);
hash = hash_mix(hash, hash_ptr(op.type));
resolved_type_params.add(op.type);
}
}
if (repl.len == 0) compiler_error(pos, "No polymorphic variables found to replace");
Ast_Decl *result = 0;
For(poly->polymorphs) {
if (it->polymorph_hash == hash) {
@@ -466,14 +532,16 @@ Ast_Decl *get_or_instantiate_polymorph_lambda(Token *pos, Ast_Decl *poly, Array<
}
if (!result) {
result = (Ast_Decl *)ast_copy(poly, poly->parent_scope, &poly->polymorph_parameters, &params);
result = (Ast_Decl *)ast_copy(poly, poly->parent_scope, &repl);
result->polymorph_hash = hash;
assert(result->di != poly->di);
result->polymorph_resolved_parameter_types = resolved_type_params.tight_copy(pctx->perm);
result->unique_name = get_unique_name_for_decl(result);
poly->polymorphs.allocator = pctx->heap;
if (!poly->polymorphs.allocator) poly->polymorphs.allocator = pctx->heap;
poly->polymorphs.add(result);
assert(result->polymorph_resolved_parameter_types.len == poly->polymorph_parameters.len);
assert(result->di != poly->di);
}
resolve_decl(result);