From 2578a2a31a375ee43b8422f1353d6346bc802d03 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 21 Apr 2023 11:38:00 +0200 Subject: [PATCH] Initial add parent_ast to scopes --- core_ast.cpp | 5 +++++ core_compiler_interface.hpp | 24 ++++-------------------- core_parsing.cpp | 14 +++++++------- 3 files changed, 16 insertions(+), 27 deletions(-) diff --git a/core_ast.cpp b/core_ast.cpp index 37c9275..7871c41 100644 --- a/core_ast.cpp +++ b/core_ast.cpp @@ -116,6 +116,7 @@ ast_lambda(Token *pos, Array params, Array ret, Ast_Scop result->args = params.tight_copy(pctx->perm); result->ret = ret.tight_copy(pctx->perm); result->scope = scope; + scope->parent_ast = result; For(params) { if (is_flag_set(it->flags, AST_POLYMORPH)) { @@ -141,6 +142,7 @@ ast_for(Token *pos, Ast_Expr *init, Ast_Expr *cond, Ast_Expr *iter, Ast_Scope *s result->cond = cond; result->iter = iter; result->scope = scope; + scope->parent_ast = result; return result; } @@ -178,6 +180,7 @@ ast_if_node(Token *pos, Ast_Expr *init, Ast_Expr *expr, Ast_Scope *scope) { result->scope = scope; result->expr = expr; result->init = (Ast_Binary *)init; + scope->parent_ast = result; if (result->init) { assert(init->kind == AST_VAR); } @@ -233,6 +236,7 @@ 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; + scope->parent_decl = result; if (polymorph_parameters.len) { result->polymorph_parameters = polymorph_parameters; set_flag(result->flags, AST_POLYMORPH); @@ -246,6 +250,7 @@ ast_enum(Token *pos, Ast_Expr *typespec, Ast_Scope *scope) { AST_NEW(Decl, ENUM, pos, AST_DECL); result->scope = scope; result->typespec = typespec; + scope->parent_decl = result; set_flag_typespec(typespec); return result; } diff --git a/core_compiler_interface.hpp b/core_compiler_interface.hpp index b1d5104..3b1e978 100644 --- a/core_compiler_interface.hpp +++ b/core_compiler_interface.hpp @@ -567,25 +567,6 @@ struct Ast_Switch : Ast { Ast_Scope *default_scope; }; -/* - How does current declaration order resolver works: - * First we put all the global declarations into the global scope (when parsing) all unresolved - * All the types are declared INCOMPLETE and RESOLVED - * We descent the tree by resolving each of the named declarations, we resolve by their name - When we start resolving we set RESOLVING flag and when we complete RESOLVED flag - and put into ordered list - * When we meet a symbol (named declaration) while descending the tree, - we resolve that symbol instead before resolving current declaration. - * When we meet a declaration that requires size of a type - field access, var assignment, - we need to call "complete_type", it sets COMPLETING flag. - This call resolves all the dependencies of that type, - sets size of type and marks it as COMPLETE and puts into ordered list. - If it detects COMPLETING while - resolving, we got a circular dependency. That might happen when we have - that struct without pointer inside itself. - - */ - struct Ast_Scope : Ast { String debug_name; // Only for debug purposes, dont depend on it List implicit_imports; @@ -595,7 +576,10 @@ struct Ast_Scope : Ast { uint32_t scope_id; Ast_Scope *file; // Self referential for file and module Ast_Module *module; - Ast_Decl *parent_decl; // @language_todo: add parent decl + union { + Ast *parent_ast; // @language_todo: add parent decl + Ast_Decl *parent_decl; + }; }; enum Ast_Module_State { diff --git a/core_parsing.cpp b/core_parsing.cpp index ffe9078..549386b 100644 --- a/core_parsing.cpp +++ b/core_parsing.cpp @@ -937,22 +937,23 @@ parse_decl(B32 is_global) { // implementing actual parse_lambda or something. Probably needs less // ambigious syntax. if (expr->kind == AST_LAMBDA_EXPR) { - auto a = (Ast_Lambda *)expr; - if (a->scope || is_flag_set(flags, AST_FOREIGN)) { + auto lambda = (Ast_Lambda *)expr; + if (lambda->scope || is_flag_set(flags, AST_FOREIGN)) { result->kind = AST_LAMBDA; - if (is_flag_set(a->flags, AST_POLYMORPH)) { + lambda->scope->parent_decl = result; + if (is_flag_set(lambda->flags, AST_POLYMORPH)) { set_flag(result->flags, AST_POLYMORPH); set_flag(result->flags, AST_PARENT_POLYMORPH); int polymorph_count = 0; - For(a->args) { + For(lambda->args) { if (it->flags & AST_POLYMORPH) { polymorph_count += 1; } } result->polymorph_parameters.init(pctx->perm, polymorph_count); - For(a->args) { + For(lambda->args) { if (it->flags & AST_POLYMORPH) { result->polymorph_parameters.add(it); } @@ -967,8 +968,7 @@ parse_decl(B32 is_global) { else if (token_match(TK_StringLit, TK_DoubleColon)) { // @cleanup: consider simplifying lambdas, removing AST_LAMBDA_EXPR and - // implementing actual parse_lambda or something. Probably needs less - // ambigious syntax. + // implementing actual parse_lambda or something. Ast_Lambda *expr = (Ast_Lambda *)parse_expr(); if (expr->kind != AST_LAMBDA_EXPR) { compiler_error(tname, "Operator overload is required to be a lambda function");