From 45b3531e29c1b596f1be123de94b7c618c92bf8a Mon Sep 17 00:00:00 2001 From: Kimplul Date: Sat, 11 Jan 2025 16:30:39 +0200 Subject: multiple import stuff --- src/scope.c | 58 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 20 deletions(-) (limited to 'src/scope.c') diff --git a/src/scope.c b/src/scope.c index 198e2c0..6687d98 100644 --- a/src/scope.c +++ b/src/scope.c @@ -17,6 +17,12 @@ #include #include +static bool same_src_scope(struct scope *a, struct scope *b) +{ + /** @todo a bit ridiculous, is there a less hacky way? */ + return a->fctx.fbuf == b->fctx.fbuf; +} + struct scope *create_scope() { /* if I ever try making the parser multithreaded, this should be atomic. */ @@ -87,7 +93,7 @@ void scope_set_flags(struct scope *scope, enum scope_flags flags) unsigned scope_flags(struct scope *scope, enum scope_flags flags) { assert(scope); - return scope->flags & flags; + return (scope->flags & flags) == flags; } static struct visible *create_visible(char *id, struct ast *node) @@ -166,6 +172,20 @@ struct visible *create_proc(struct scope *scope, char *id, struct ast *proc) return n; } +static bool scope_add_recurse(struct scope *scope, struct ast *node) +{ + if (!scope->parent) + return false; + + if (!ast_flags(node, AST_FLAG_PUBLIC)) + return false; + + if (same_src_scope(scope, node->scope)) + return true; + + return scope_flags(scope, SCOPE_PUBLIC); +} + int scope_add_var(struct scope *scope, struct ast *var) { struct ast *exists = scope_find_symbol(scope, var_id(var)); @@ -176,8 +196,7 @@ int scope_add_var(struct scope *scope, struct ast *var) } create_var(scope, var_id(var), var); - if (scope->parent && - scope_flags(scope, SCOPE_FILE) && ast_flags(var, AST_FLAG_PUBLIC)) + if (scope_add_recurse(scope, var)) return scope_add_var(scope->parent, var); return 0; @@ -193,8 +212,7 @@ int scope_add_type(struct scope *scope, char *id, struct ast *type) } create_type(scope, id, type); - if (scope->parent && - scope_flags(scope, SCOPE_FILE) && ast_flags(type, AST_FLAG_PUBLIC)) + if (scope_add_recurse(scope, type)) return scope_add_type(scope->parent, id, type); return 0; @@ -251,8 +269,7 @@ int scope_add_chain(struct scope *scope, char *id, struct ast *type) insert_chain(scope, id, type); - if (scope->parent && - scope_flags(scope, SCOPE_FILE) && ast_flags(type, AST_FLAG_PUBLIC)) + if (scope_add_recurse(scope, type)) return scope_add_chain(scope->parent, id, type); return 0; @@ -270,8 +287,7 @@ int scope_add_macro(struct scope *scope, struct ast *macro) /* always add to scope, do resolve checking later */ create_macro(scope, macro_def_id(macro), macro); - if (scope->parent && - scope_flags(scope, SCOPE_FILE) && ast_flags(macro, AST_FLAG_PUBLIC)) + if (scope_add_recurse(scope, macro)) return scope_add_macro(scope->parent, macro); return 0; @@ -282,15 +298,14 @@ int scope_add_proc(struct scope *scope, struct ast *proc) assert(proc->k == AST_PROC_DEF); struct ast *exists = file_scope_find_symbol(scope, proc_id(proc)); if (exists) { - semantic_error(scope->fctx, proc, "proc redefined"); - semantic_info(scope->fctx, exists, "previously here"); + semantic_error(proc->scope->fctx, proc, "proc redefined"); + semantic_info(exists->scope->fctx, exists, "previously here"); return -1; } /* always add to scope, do resolve checking later */ create_proc(scope, proc_id(proc), proc); - if (scope->parent && - scope_flags(scope, SCOPE_FILE) && ast_flags(proc, AST_FLAG_PUBLIC)) + if (scope_add_recurse(scope, proc)) return scope_add_proc(scope->parent, proc); return 0; @@ -309,8 +324,7 @@ int scope_add_trait(struct scope *scope, struct ast *trait) } create_type(scope, id, trait); - if (scope->parent && - scope_flags(scope, SCOPE_FILE) && ast_flags(trait, AST_FLAG_PUBLIC)) + if (scope_add_recurse(scope, trait)) return scope_add_trait(scope->parent, trait); return 0; @@ -324,8 +338,7 @@ int scope_add_expd_struct(struct scope *scope, struct ast *def, assert(file_scope_find_expd_struct(scope, def, types) == NULL); create_expanded(scope, def, types, expd); - if (scope->parent && - scope_flags(scope, SCOPE_FILE) && ast_flags(def, AST_FLAG_PUBLIC)) + if (scope_add_recurse(scope, expd)) return scope_add_expd_struct(scope->parent, def, types, expd); return 0; @@ -386,9 +399,8 @@ int scope_add_expd_chain(struct scope *scope, struct ast *def, insert_expd_chain(scope, def, types, expd); - if (scope->parent && - scope_flags(scope, SCOPE_FILE) && ast_flags(def, AST_FLAG_PUBLIC)) - return scope_add_expd_struct(scope->parent, def, types, expd); + if (scope_add_recurse(scope, expd)) + return scope_add_expd_chain(scope->parent, def, types, expd); return 0; } @@ -563,3 +575,9 @@ void scope_add_scope(struct scope *parent, struct scope *child) child->next = parent->children; parent->children = child; } + +bool same_src(struct ast *a, struct ast *b) +{ + return same_src_scope(a->scope, b->scope); +} + -- cgit v1.3