diff options
| -rw-r--r-- | src/parser/parser.h | 4 | ||||
| -rw-r--r-- | src/parser/parser_expr.c | 15 | ||||
| -rw-r--r-- | src/parser/parser_utils.c | 12 | ||||
| -rw-r--r-- | tests/features/test_alias.zc | 13 |
4 files changed, 39 insertions, 5 deletions
diff --git a/src/parser/parser.h b/src/parser/parser.h index f566979..6f62435 100644 --- a/src/parser/parser.h +++ b/src/parser/parser.h @@ -370,6 +370,10 @@ void register_selective_import(ParserContext *ctx, const char *symbol, const cha const char *source_module); SelectiveImport *find_selective_import(ParserContext *ctx, const char *name); +// Type Aliases +void register_type_alias(ParserContext *ctx, const char *alias, const char *original); +const char *find_type_alias(ParserContext *ctx, const char *alias); + // External symbol tracking (C interop) void register_extern_symbol(ParserContext *ctx, const char *name); int is_extern_symbol(ParserContext *ctx, const char *name); diff --git a/src/parser/parser_expr.c b/src/parser/parser_expr.c index 092b86b..2d0e10b 100644 --- a/src/parser/parser_expr.c +++ b/src/parser/parser_expr.c @@ -1384,6 +1384,21 @@ ASTNode *parse_primary(ParserContext *ctx, Lexer *l) char *tmp = xmalloc(strlen(acc) + suffix.len + 3); ASTNode *def = find_struct_def(ctx, acc); + + // If not found as a struct, check if it's an alias + if (!def) + { + const char *aliased = find_type_alias(ctx, acc); + if (aliased) + { + // Found an alias: replace acc with the aliased name + free(acc); + acc = xstrdup(aliased); + // Try finding the struct definition again with the resolved name + def = find_struct_def(ctx, acc); + } + } + if (def) { int is_variant = 0; diff --git a/src/parser/parser_utils.c b/src/parser/parser_utils.c index 4973111..afd6f33 100644 --- a/src/parser/parser_utils.c +++ b/src/parser/parser_utils.c @@ -2666,8 +2666,10 @@ char *rewrite_expr_methods(ParserContext *ctx, char *raw) char mangled[256]; - // Resolve alias - Module *mod = find_module(ctx, func_name); + const char *aliased = find_type_alias(ctx, func_name); + const char *use_name = aliased ? aliased : func_name; + + Module *mod = find_module(ctx, use_name); if (mod) { if (mod->is_c_header) @@ -2681,14 +2683,14 @@ char *rewrite_expr_methods(ParserContext *ctx, char *raw) } else { - ASTNode *sdef = find_struct_def(ctx, func_name); + ASTNode *sdef = find_struct_def(ctx, use_name); if (sdef) { - snprintf(mangled, sizeof(mangled), "%s__%s", func_name, method); + snprintf(mangled, sizeof(mangled), "%s__%s", use_name, method); } else { - snprintf(mangled, sizeof(mangled), "%s_%s", func_name, method); + snprintf(mangled, sizeof(mangled), "%s_%s", use_name, method); } } diff --git a/tests/features/test_alias.zc b/tests/features/test_alias.zc index 7b0eaa4..5ec14f1 100644 --- a/tests/features/test_alias.zc +++ b/tests/features/test_alias.zc @@ -30,6 +30,9 @@ impl Vec2<T> { fn add(self, rhs: Vec2<T>) -> Vec2<T> { return Vec2<T>{x: self.x + rhs.x, y: self.y + rhs.y}; } + static fn zero() -> Vec2<T> { + return Vec2<T>{x: 0, y: 0}; + } } alias Vec2f = Vec2<f32>; @@ -92,3 +95,13 @@ test "alias operator overloading" { assert(v2.x == -2.0, "Control generic operator overloading failed"); } + +test "alias static methods" { + var v1 = Vec2f::zero(); + assert(v1.x == 0.0, "Direct static call on alias failed"); + + println "Static call in f-string: {Vec2f::zero().x}"; + + var v2 = Vec2<f32>::zero(); + assert(v2.x == 0.0, "Direct static call on generic failed"); +} |
