From 9e077d4d88b059769bfe6f87f70def2b6fbfe359 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sat, 1 Apr 2023 11:43:26 +0200 Subject: [PATCH] Unique names for polymorphic types --- core_codegen_c_language.cpp | 25 ++----------------------- core_polymorph.cpp | 17 +++++++++++++++++ examples/_polymorphism.core | 14 +++++++++++++- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/core_codegen_c_language.cpp b/core_codegen_c_language.cpp index 9009220..2c1824f 100644 --- a/core_codegen_c_language.cpp +++ b/core_codegen_c_language.cpp @@ -41,27 +41,6 @@ gen_last_line() { } } -CORE_Static String -string_scope_name(Ast_Scope *scope) { - String string = {}; - if (scope->parent_scope) string = string_scope_name(scope->parent_scope); - assert_message(scope->scope_id != 0, "Scope id is equal to 0 which is invalid, scope didn't initialize id"); - string = pctx->fmt("%QS%u_", string, scope->scope_id); - return string; -} - -CORE_Static void -gen_scope_name(Ast_Scope *scope) { - gen("%Q", string_scope_name(scope)); -} - -CORE_Static Intern_String -get_unique_name(Ast *ast) { - String result = string_scope_name(ast->parent_scope); - assert(result.len); - return pctx->internf("%Q%d", result, ast->pos->line); -} - global String prefixed_string_type; CORE_Static const char * get_ctype_name_for_type(Ast_Type *type) { @@ -565,7 +544,7 @@ gen_ast(Ast *ast) { if (is_tuple(node->resolved_type)) { Scoped_Arena scratch(pctx->scratch); - Intern_String tuple_name = get_unique_name(node); + Intern_String tuple_name = get_unique_name(); gen_simple_decl(node->resolved_type, tuple_name); gen(";"); @@ -804,7 +783,7 @@ gen_ast(Ast *ast) { Scoped_Arena scratch(pctx->scratch); - Intern_String var_name = get_unique_name(node); + Intern_String var_name = get_unique_name(); gen_simple_decl(node->resolved_type, var_name); gen(" = "); gen_expr(node->expr); diff --git a/core_polymorph.cpp b/core_polymorph.cpp index 7ca72aa..ffd3542 100644 --- a/core_polymorph.cpp +++ b/core_polymorph.cpp @@ -1,4 +1,18 @@ +CORE_Static Intern_String +get_unique_name() { + static uint64_t counter; + static char char_counter; + if (char_counter == 0 || char_counter > 'Z') char_counter = 'A'; + return pctx->internf("%c%uUnique", char_counter++, counter++); +} + +CORE_Static Intern_String +get_unique_name_for_decl(Ast_Decl *decl) { + static char char_counter; + if (char_counter == 0 || char_counter > 'Z') char_counter = 'A'; + return pctx->internf("%c%u_%Q", char_counter++, decl->di, decl->name); +} #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) { @@ -297,6 +311,9 @@ Ast_Decl *get_or_instantiate_polymorph_type(Token *pos, Ast_Decl *poly, Arrayflags, AST_POLYMORPH); result->type_val = type_incomplete(result); result->polymorph_hash = hash; + + assert(result->di != poly->di); + result->name = get_unique_name_for_decl(result); poly->polymorphs.add(result); } diff --git a/examples/_polymorphism.core b/examples/_polymorphism.core index 27aec35..9c12e74 100644 --- a/examples/_polymorphism.core +++ b/examples/_polymorphism.core @@ -24,9 +24,21 @@ Array :: struct($T: Type) len: int cap: int +make_array :: (a: *int, count: int): Array(int) + result := Array(int) { + data = a, + len = count, + cap = count + } + return result + + main :: (argc: int, argv: **char): int - array: Array(int) + buff: *int + array: Array(int) = {len = 10, cap = 10, data = buff} second_array: Array(int) third_array: Array(int) + fourth: Array(F32) + fifth: Array(F32) return 0 \ No newline at end of file