From 663adf6cae2700ae0a05e7a769d65c4f7edd4ade Mon Sep 17 00:00:00 2001 From: zzy <2450266535@qq.com> Date: Wed, 2 Dec 2026 13:02:58 +0800 Subject: [PATCH] =?UTF-8?q?ai:=20=E5=AE=9E=E7=8E=B0mod=E8=AF=AD=E6=B3=95?= =?UTF-8?q?=20=E6=8F=90=E4=BE=9Bnamespace=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stage1/driver/splc1.c | 28 +- stage1/include/spl_ast.h | 2 + stage1/include/spl_module.h | 63 ++++ stage1/include/spl_namespace.h | 71 +++++ stage1/include/spl_scope.h | 42 --- stage1/include/spl_sema.h | 8 +- stage1/lower/spl_ast2ir.c | 95 +++--- stage1/module/spl_module.c | 166 ++++++++++ stage1/namespace/spl_namespace.c | 167 +++++++++++ stage1/sema/spl_scope.c | 46 --- stage1/sema/spl_sema.c | 500 +++++++++++++++++++++++++++---- 11 files changed, 1001 insertions(+), 187 deletions(-) create mode 100644 stage1/include/spl_module.h create mode 100644 stage1/include/spl_namespace.h delete mode 100644 stage1/include/spl_scope.h create mode 100644 stage1/module/spl_module.c create mode 100644 stage1/namespace/spl_namespace.c delete mode 100644 stage1/sema/spl_scope.c diff --git a/stage1/driver/splc1.c b/stage1/driver/splc1.c index 71be5d7..908c8d6 100644 --- a/stage1/driver/splc1.c +++ b/stage1/driver/splc1.c @@ -21,6 +21,7 @@ #include "spl_ir2c.h" #include "spl_ir2vm.h" #include "spl_lexer.h" +#include "spl_module.h" #include "spl_parser.h" #include "spl_sema.h" #include "spl_tok.h" @@ -49,10 +50,17 @@ typedef struct { spl_driver_module_vec_t modules; MAP(const char *, usize) path_to_idx; + spl_module_registry_t module_registry; spl_diag_t *diag; } spl_driver_t; +static spl_module_id_t driver_register_module(spl_driver_t *d, spl_ast_t *ast, const char *path, + spl_diag_t *diag) { + return spl_module_register(&d->module_registry, ast, path, spl_module_declared_name(ast), + diag); +} + static char *read_file(const char *path, long *out_len) { FILE *f = fopen(path, "rb"); if (!f) { @@ -219,6 +227,8 @@ static spl_ast_t *driver_load(void *user, const char *from_fname, const char *pa vec_at(d->modules, idx).ast = ast; vec_at(d->modules, idx).src = src; + if (!driver_register_module(d, ast, key, diag)) + return NULL; return ast; } @@ -236,6 +246,7 @@ static int compile_spl(const char *src, const char *fname, const char *outpath, spl_ast_init(&drv.main_ast, &drv.main_toks); map_init(drv.path_to_idx, MAP_HASH_STR, MAP_CMP_STR); vec_init(drv.modules); + spl_module_registry_init(&drv.module_registry); int rc = 0; @@ -245,6 +256,7 @@ static int compile_spl(const char *src, const char *fname, const char *outpath, dump_tokens(drv.main_toks); free(drv.main_path); map_free(drv.path_to_idx); + spl_module_registry_drop(&drv.module_registry); spl_diag_drop(&diag); return 0; } @@ -253,7 +265,7 @@ static int compile_spl(const char *src, const char *fname, const char *outpath, spl_lang_type_t lang_type; spl_layout_target_t layout_target; spl_layout_t layout; - spl_scope_t scope; + spl_namespace_t ns; spl_sema_t sema; spl_ast2ir_t a2ir; spl_ir_builder_t ir_builder; @@ -279,6 +291,12 @@ static int compile_spl(const char *src, const char *fname, const char *outpath, goto cleanup; } + if (!driver_register_module(&drv, &drv.main_ast, drv.main_path, &diag)) { + spl_diag_summary(&diag); + rc = 1; + goto cleanup; + } + spl_attr_init(&attr); attr_ok = 1; spl_lang_type_init(&lang_type); @@ -292,10 +310,11 @@ static int compile_spl(const char *src, const char *fname, const char *outpath, spl_layout_init(&layout, &layout_target); layout_ok = 1; - spl_scope_init(&scope); + spl_ns_init(&ns); scope_ok = 1; - spl_sema_init(&sema, &drv.main_ast, &lang_type, &layout, &scope, &attr); + spl_sema_init(&sema, &drv.main_ast, &lang_type, &layout, &ns, &attr); + sema.modules = &drv.module_registry; sema_ok = 1; spl_module_loader_t loader = {.load = driver_load, .user = &drv}; sema.loader = &loader; @@ -397,7 +416,7 @@ cleanup: if (sema_ok) spl_sema_drop(&sema); if (scope_ok) - spl_scope_drop(&scope); + spl_ns_drop(&ns); if (layout_ok) spl_layout_drop(&layout); if (lang_type_ok) @@ -418,6 +437,7 @@ cleanup: vec_free(drv.modules); /* map 的 key 是 module 里 strdup 出的 abs_path,已随 module 释放 */ map_free(drv.path_to_idx); + spl_module_registry_drop(&drv.module_registry); free(drv.main_path); spl_diag_drop(&diag); return rc; diff --git a/stage1/include/spl_ast.h b/stage1/include/spl_ast.h index 678c23b..a480b40 100644 --- a/stage1/include/spl_ast.h +++ b/stage1/include/spl_ast.h @@ -159,7 +159,9 @@ struct spl_ast_node { spl_dbg_node_t dbg; usize resolved_def_id; + usize resolved_outer_def_id; usize resolved_lang_tid; + usize resolved_ns_id; usize expected_lang_tid; spl_attr_id_vec_t attrs; diff --git a/stage1/include/spl_module.h b/stage1/include/spl_module.h new file mode 100644 index 0000000..fc42823 --- /dev/null +++ b/stage1/include/spl_module.h @@ -0,0 +1,63 @@ +#ifndef __SPL_MODULE_H__ +#define __SPL_MODULE_H__ + +#include "spl_ast.h" +#include "spl_diag.h" +#include "spl_lang_type.h" +#include "spl_namespace.h" + +typedef usize spl_module_id_t; + +typedef enum { + SPL_MODULE_LOADING, + SPL_MODULE_LOADED, + SPL_MODULE_ERROR, +} spl_module_state_t; + +typedef struct { + spl_module_id_t id; + char *name; + char *file_path; + spl_ast_t *ast; + spl_ast_node_ref_t container; + spl_module_id_t parent; + spl_def_id_t def_id; + spl_lang_type_id_t lang_id; + spl_ns_id_t ns; + spl_module_state_t state; + int sema_state; +} spl_module_t; + +typedef struct { + VEC(spl_module_t) modules; + MAP(const char *, spl_module_id_t) by_name; + MAP(const char *, spl_module_id_t) by_path; +} spl_module_registry_t; + +void spl_module_registry_init(spl_module_registry_t *reg); +void spl_module_registry_drop(spl_module_registry_t *reg); + +/* 由文件路径推导默认模块名:去扩展名,非法标识符字符映射为 '_'。 + 结果写入 out,成功返回 true。 */ +bool spl_module_name_from_path(const char *path, char *out, usize cap); + +/* 读取文件头 mod 声明的点分路径;没有声明返回 NULL。 */ +const char *spl_module_declared_name(spl_ast_t *ast); + +/* 登记一个已解析的文件模块。declared_name 为空时使用文件名推导。 + 重名或重路径冲突返回 0 并写诊断。 */ +spl_module_id_t spl_module_register(spl_module_registry_t *reg, spl_ast_t *ast, + const char *file_path, const char *declared_name, + spl_diag_t *diag); + +spl_module_t *spl_module_get(spl_module_registry_t *reg, spl_module_id_t id); +spl_module_id_t spl_module_find_name(spl_module_registry_t *reg, const char *name); +spl_module_id_t spl_module_find_ast(spl_module_registry_t *reg, spl_ast_t *ast); + +/* 返回模块名最后一个点分段的起点;名字中必须含该段。 */ +const char *spl_module_last_segment(const char *name); + +/* 返回模块 container_members 节点引用;无 AST 返回 0。 */ +spl_ast_node_ref_t spl_module_container(spl_module_t *mod); + +#endif /* __SPL_MODULE_H__ */ diff --git a/stage1/include/spl_namespace.h b/stage1/include/spl_namespace.h new file mode 100644 index 0000000..f7534c3 --- /dev/null +++ b/stage1/include/spl_namespace.h @@ -0,0 +1,71 @@ +#ifndef __SPL_NAMESPACE_H__ +#define __SPL_NAMESPACE_H__ + +#include "spl_lang_type.h" + +typedef usize spl_ns_id_t; + +typedef enum { + SPL_SYMBOL_KIND_ERROR, + SPL_SYMBOL_KIND_VAR, + SPL_SYMBOL_KIND_MEMBER, + SPL_SYMBOL_KIND_FN, + SPL_SYMBOL_KIND_TYPE, +} spl_symbol_kind_t; + +typedef struct { + const char *name; + spl_symbol_kind_t kind; + spl_def_id_t node; +} spl_symbol_t; + +typedef VEC(const char *) spl_symbol_path_t; + +typedef enum { + SPL_NS_ROOT, + SPL_NS_MODULE, + SPL_NS_AGGREGATE, + SPL_NS_FUNCTION, + SPL_NS_BLOCK, +} spl_ns_kind_t; + +typedef struct { + spl_ns_id_t id; + spl_ns_id_t parent; + spl_ns_kind_t kind; + const char *name; + const char *qualified; + MAP(const char *, spl_symbol_t) symbols; + usize owner_kind; + usize owner_id; +} spl_ns_node_t; +typedef VEC(spl_ns_node_t) spl_ns_node_vec_t; + +typedef struct { + spl_ns_node_vec_t nodes; + spl_ns_id_t root; + spl_ns_id_t current; +} spl_namespace_t; + +void spl_ns_init(spl_namespace_t *ns); +void spl_ns_drop(spl_namespace_t *ns); + +spl_ns_id_t spl_ns_root(spl_namespace_t *ns); +spl_ns_id_t spl_ns_create(spl_namespace_t *ns, spl_ns_id_t parent, spl_ns_kind_t kind, + const char *name, usize owner_kind, usize owner_id); +spl_ns_id_t spl_ns_alloc(spl_namespace_t *ns, spl_ns_kind_t kind, const char *name); + +bool spl_ns_enter(spl_namespace_t *ns, spl_ns_id_t id); +bool spl_ns_leave(spl_namespace_t *ns); + +bool spl_ns_insert(spl_namespace_t *ns, spl_ns_id_t id, spl_symbol_t symbol); +bool spl_ns_lookup(spl_namespace_t *ns, const char *name, spl_symbol_t *out); +bool spl_ns_lookup_local(spl_namespace_t *ns, spl_ns_id_t id, const char *name, + spl_symbol_t *out); + +const char *spl_ns_qualified(spl_namespace_t *ns, spl_ns_id_t id); +bool spl_ns_symbol_qualified(spl_namespace_t *ns, spl_ns_id_t id, const char *local_name, + char *out, usize cap); +spl_ns_id_t spl_ns_child(spl_namespace_t *ns, spl_ns_id_t parent, const char *name); + +#endif /* __SPL_NAMESPACE_H__ */ diff --git a/stage1/include/spl_scope.h b/stage1/include/spl_scope.h deleted file mode 100644 index a626331..0000000 --- a/stage1/include/spl_scope.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef __SPL_SCOPE_H__ -#define __SPL_SCOPE_H__ - -#include "spl_lang_type.h" - -typedef usize spl_scope_id_t; - -typedef enum { - SPL_SYMBOL_KIND_ERROR, - SPL_SYMBOL_KIND_VAR, - SPL_SYMBOL_KIND_MEMBER, - SPL_SYMBOL_KIND_FN, - SPL_SYMBOL_KIND_TYPE, -} spl_symbol_kind_t; - -typedef struct { - const char *name; - spl_symbol_kind_t kind; - spl_def_id_t node; -} spl_symbol_t; - -typedef VEC(const char *) spl_symbol_path_t; - -typedef struct { - spl_scope_id_t parent; - MAP(const char *, spl_symbol_t) symbols; -} spl_scope_node_t; -typedef VEC(spl_scope_node_t) spl_scope_node_vec_t; - -typedef struct { - spl_scope_node_vec_t scopes; - spl_scope_id_t root_scope; - spl_scope_id_t current_scope; -} spl_scope_t; - -void spl_scope_init(spl_scope_t *scope); -void spl_scope_drop(spl_scope_t *scope); -spl_scope_id_t spl_scope_alloc(spl_scope_t *scope); -bool spl_scope_insert(spl_scope_t *scope, spl_scope_id_t id, spl_symbol_t symbol); -bool spl_scope_find(spl_scope_t *scop, const char *symbol_name, spl_symbol_t *out); - -#endif /* __SPL_SCOPE_H__ */ diff --git a/stage1/include/spl_sema.h b/stage1/include/spl_sema.h index 9068c10..c6650c1 100644 --- a/stage1/include/spl_sema.h +++ b/stage1/include/spl_sema.h @@ -7,7 +7,8 @@ #include "spl_lang_type.h" #include "spl_layout.h" #include "spl_mpa.h" -#include "spl_scope.h" +#include "spl_module.h" +#include "spl_namespace.h" #include "spl_type.h" typedef struct spl_module_loader spl_module_loader_t; @@ -23,7 +24,7 @@ typedef struct { spl_ast_t *ast; spl_lang_type_t *type; spl_layout_t *layout; /* 由 splc0 拥有并解析;sema 仅借用 */ - spl_scope_t *scope; + spl_namespace_t *ns; spl_symbol_t root; spl_attr_t *attr; spl_lang_type_id_t current_aggregate_lang; @@ -31,11 +32,12 @@ typedef struct { spl_lang_type_id_t current_loop_break_tid; spl_symbol_path_t label_stack; const spl_module_loader_t *loader; + spl_module_registry_t *modules; spl_diag_t *diag; } spl_sema_t; void spl_sema_init(spl_sema_t *sema, spl_ast_t *ast, spl_lang_type_t *type, - spl_layout_t *layout, spl_scope_t *scope, spl_attr_t *attr); + spl_layout_t *layout, spl_namespace_t *ns, spl_attr_t *attr); void spl_sema_drop(spl_sema_t *sema); void spl_sema_run_module(spl_sema_t *sema, spl_ast_node_ref_t module_ref); diff --git a/stage1/lower/spl_ast2ir.c b/stage1/lower/spl_ast2ir.c index 9242349..7864396 100644 --- a/stage1/lower/spl_ast2ir.c +++ b/stage1/lower/spl_ast2ir.c @@ -2113,20 +2113,9 @@ static void emit_aggregate_fns_body(spl_ast2ir_t *a, spl_ast_node_t *te) { } } -static void emit_container_reserve(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { - spl_ast_node_t *n = spl_ast_node(a->ast, ref); - if (!n) - return; - if (n->kind == SPL_AST_TOP_LEVEL_BLOCK) { - if (n->top_level_block.block) - emit_container_reserve(a, n->top_level_block.block); - return; - } - if (n->kind != SPL_AST_CONTAINER_MEMBERS) - return; - - for (usize i = 0; i < vec_size(n->container_members.declarations); ++i) { - spl_ast_node_ref_t dref = vec_at(n->container_members.declarations, i); +static void emit_decls_reserve(spl_ast2ir_t *a, spl_ast_node_ref_vec_t *decls) { + for (usize i = 0; i < vec_size(*decls); ++i) { + spl_ast_node_ref_t dref = vec_at(*decls, i); spl_ast_node_t *d = spl_ast_node(a->ast, dref); if (!d) continue; @@ -2143,8 +2132,55 @@ static void emit_container_reserve(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { emit_aggregate_fns_reserve(a, te); break; } - case SPL_AST_TOP_LEVEL_BLOCK: - emit_container_reserve(a, dref); + case SPL_AST_VAR_DECL: + case SPL_AST_CONST_DECL: + break; + case SPL_AST_MOD_DECL: + if (d->mod_decl.has_body) + emit_decls_reserve(a, &d->mod_decl.decls); + break; + default: + break; + } + } +} + +static void emit_container_reserve(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { + spl_ast_node_t *n = spl_ast_node(a->ast, ref); + if (!n) + return; + if (n->kind == SPL_AST_TOP_LEVEL_BLOCK) { + if (n->top_level_block.block) + emit_container_reserve(a, n->top_level_block.block); + return; + } + if (n->kind != SPL_AST_CONTAINER_MEMBERS) + return; + emit_decls_reserve(a, &n->container_members.declarations); +} + +static void emit_decls_body(spl_ast2ir_t *a, spl_ast_node_ref_vec_t *decls) { + for (usize i = 0; i < vec_size(*decls); ++i) { + spl_ast_node_ref_t dref = vec_at(*decls, i); + spl_ast_node_t *d = spl_ast_node(a->ast, dref); + if (!d) + continue; + switch (d->kind) { + case SPL_AST_FN_DECL: + emit_fn_body(a, d); + break; + case SPL_AST_TYPE_DECL: { + spl_ast_node_t *tpat = spl_ast_node(a->ast, d->type_decl.pattern); + if (!tpat || tpat->kind != SPL_AST_PATTERN_IDENT) + break; + spl_ast_node_t *te = spl_ast_node(a->ast, d->type_decl.type_expr); + if (te && te->kind == SPL_AST_TYPE_AGGREGATE) + emit_aggregate_fns_body(a, te); + break; + } + case SPL_AST_MOD_DECL: + if (d->mod_decl.has_body) + emit_decls_body(a, &d->mod_decl.decls); break; default: break; @@ -2163,32 +2199,7 @@ static void emit_container_body(spl_ast2ir_t *a, spl_ast_node_ref_t ref) { } if (n->kind != SPL_AST_CONTAINER_MEMBERS) return; - - for (usize i = 0; i < vec_size(n->container_members.declarations); ++i) { - spl_ast_node_ref_t dref = vec_at(n->container_members.declarations, i); - spl_ast_node_t *d = spl_ast_node(a->ast, dref); - if (!d) - continue; - switch (d->kind) { - case SPL_AST_FN_DECL: - emit_fn_body(a, d); - break; - case SPL_AST_TYPE_DECL: { - spl_ast_node_t *tpat = spl_ast_node(a->ast, d->type_decl.pattern); - if (!tpat || tpat->kind != SPL_AST_PATTERN_IDENT) - break; - spl_ast_node_t *te = spl_ast_node(a->ast, d->type_decl.type_expr); - if (te && te->kind == SPL_AST_TYPE_AGGREGATE) - emit_aggregate_fns_body(a, te); - break; - } - case SPL_AST_TOP_LEVEL_BLOCK: - emit_container_body(a, dref); - break; - default: - break; - } - } + emit_decls_body(a, &n->container_members.declarations); } static spl_ir_node_ref_t emit_implicit_cast(spl_ast2ir_t *a, spl_ast_node_t *n, spl_type_id_t ft, diff --git a/stage1/module/spl_module.c b/stage1/module/spl_module.c new file mode 100644 index 0000000..6b29327 --- /dev/null +++ b/stage1/module/spl_module.c @@ -0,0 +1,166 @@ +#include "spl_module.h" +#include +#include +#include + +static const char *path_basename(const char *path) { + const char *base = path; + for (const char *p = path; *p; ++p) + if (*p == '/' || *p == '\\') + base = p + 1; + return base; +} + +bool spl_module_name_from_path(const char *path, char *out, usize cap) { + if (!path || !out || cap == 0) + return false; + + const char *base = path_basename(path); + const char *dot = strrchr(base, '.'); + usize len = dot && dot != base ? (usize)(dot - base) : strlen(base); + if (len == 0) + return false; + + usize w = 0; + if (isdigit((unsigned char)base[0])) { + if (w + 1 >= cap) + return false; + out[w++] = '_'; + } + for (usize i = 0; i < len && w + 1 < cap; ++i) { + unsigned char c = (unsigned char)base[i]; + out[w++] = (isalnum(c) || c == '_') ? (char)c : '_'; + } + out[w] = '\0'; + return w > 0; +} + +const char *spl_module_declared_name(spl_ast_t *ast) { + if (!ast || !ast->root) + return NULL; + spl_ast_node_t *root = spl_ast_node(ast, ast->root); + if (!root || root->kind != SPL_AST_ROOT || !root->root.members) + return NULL; + spl_ast_node_t *cm = spl_ast_node(ast, root->root.members); + if (!cm || cm->kind != SPL_AST_CONTAINER_MEMBERS || !cm->container_members.module) + return NULL; + spl_ast_node_t *mod = spl_ast_node(ast, cm->container_members.module); + if (!mod || mod->kind != SPL_AST_MOD_DECL) + return NULL; + return mod->mod_decl.path; +} + +spl_ast_node_ref_t spl_module_container(spl_module_t *mod) { + if (!mod || !mod->ast || !mod->ast->root) + return 0; + spl_ast_node_t *root = spl_ast_node(mod->ast, mod->ast->root); + if (!root || root->kind != SPL_AST_ROOT) + return 0; + return root->root.members; +} + +const char *spl_module_last_segment(const char *name) { + if (!name) + return NULL; + const char *last = name; + for (const char *p = name; *p; ++p) + if (*p == '.') + last = p + 1; + return last; +} + +void spl_module_registry_init(spl_module_registry_t *reg) { + vec_init(reg->modules); + map_init(reg->by_name, MAP_HASH_STR, MAP_CMP_STR); + map_init(reg->by_path, MAP_HASH_STR, MAP_CMP_STR); +} + +void spl_module_registry_drop(spl_module_registry_t *reg) { + for (usize i = 0; i < vec_size(reg->modules); ++i) { + free(vec_at(reg->modules, i).name); + free(vec_at(reg->modules, i).file_path); + } + vec_free(reg->modules); + map_free(reg->by_name); + map_free(reg->by_path); +} + +spl_module_t *spl_module_get(spl_module_registry_t *reg, spl_module_id_t id) { + if (!reg || id == 0 || id > vec_size(reg->modules)) + return NULL; + return &vec_at(reg->modules, id - 1); +} + +spl_module_id_t spl_module_find_name(spl_module_registry_t *reg, const char *name) { + spl_module_id_t id = 0; + if (!reg || !name) + return 0; + if (map_get(reg->by_name, name, &id)) + return id; + return 0; +} + +spl_module_id_t spl_module_find_ast(spl_module_registry_t *reg, spl_ast_t *ast) { + if (!reg || !ast) + return 0; + for (usize i = 0; i < vec_size(reg->modules); ++i) + if (vec_at(reg->modules, i).ast == ast) + return vec_at(reg->modules, i).id; + return 0; +} + +spl_module_id_t spl_module_register(spl_module_registry_t *reg, spl_ast_t *ast, + const char *file_path, const char *declared_name, + spl_diag_t *diag) { + if (!reg || !ast) + return 0; + + char derived[256]; + const char *name = declared_name; + if (!name || !*name) { + if (!file_path || !spl_module_name_from_path(file_path, derived, sizeof derived)) { + spl_diag_error(diag, SPL_DIAG_KIND_INTERNAL, NULL, "cannot derive module name"); + return 0; + } + name = derived; + } + + spl_module_id_t existing_path = 0; + if (file_path && map_get(reg->by_path, file_path, &existing_path)) + return existing_path; + + spl_module_id_t existing_name = 0; + if (map_get(reg->by_name, name, &existing_name)) { + spl_module_t *old = spl_module_get(reg, existing_name); + spl_diag_error(diag, SPL_DIAG_KIND_DUPLICATE_SYMBOL, NULL, + "duplicate module `%s` (already loaded from `%s`)", name, + old && old->file_path ? old->file_path : "?"); + return 0; + } + + spl_module_t mod = {0}; + mod.id = vec_size(reg->modules) + 1; + mod.name = strdup(name); + mod.file_path = file_path ? strdup(file_path) : NULL; + mod.ast = ast; + mod.state = SPL_MODULE_LOADED; + mod.sema_state = 0; + + spl_ast_node_t *root = spl_ast_node(ast, ast->root); + if (root && root->kind == SPL_AST_ROOT && root->root.members) { + mod.container = root->root.members; + spl_ast_node_t *cm = spl_ast_node(ast, mod.container); + if (cm && cm->kind == SPL_AST_CONTAINER_MEMBERS && cm->container_members.module) { + spl_ast_node_t *header = spl_ast_node(ast, cm->container_members.module); + if (header && header->kind == SPL_AST_MOD_DECL) + mod.state = SPL_MODULE_LOADED; + } + } + + vec_push(reg->modules, mod); + spl_module_t *stored = &vec_at(reg->modules, vec_size(reg->modules) - 1); + map_put(reg->by_name, stored->name, stored->id); + if (stored->file_path) + map_put(reg->by_path, stored->file_path, stored->id); + return stored->id; +} diff --git a/stage1/namespace/spl_namespace.c b/stage1/namespace/spl_namespace.c new file mode 100644 index 0000000..6a4de04 --- /dev/null +++ b/stage1/namespace/spl_namespace.c @@ -0,0 +1,167 @@ +#include "spl_namespace.h" +#include +#include +#include + +static char *ns_strdup(const char *s) { + if (!s) + return NULL; + usize n = strlen(s); + char *r = malloc(n + 1); + if (!r) + return NULL; + memcpy(r, s, n + 1); + return r; +} + +static const char *ns_name_or_default(const char *name, spl_ns_id_t id, char *buf, usize cap) { + if (name && *name) + return name; + snprintf(buf, cap, "$ns%zu", id); + return buf; +} + +void spl_ns_init(spl_namespace_t *ns) { + vec_init(ns->nodes); + spl_ns_node_t dummy = {0}; + vec_push(ns->nodes, dummy); + ns->root = 0; + ns->current = 0; + ns->root = spl_ns_create(ns, 0, SPL_NS_ROOT, "pkg", 0, 0); + ns->current = ns->root; +} + +void spl_ns_drop(spl_namespace_t *ns) { + for (usize i = 0; i < vec_size(ns->nodes); ++i) { + free((char *)vec_at(ns->nodes, i).name); + free((char *)vec_at(ns->nodes, i).qualified); + map_free(vec_at(ns->nodes, i).symbols); + } + vec_free(ns->nodes); + ns->root = 0; + ns->current = 0; +} + +spl_ns_id_t spl_ns_root(spl_namespace_t *ns) { + if (!ns) + return 0; + if (ns->root == 0) + ns->root = spl_ns_create(ns, 0, SPL_NS_ROOT, "pkg", 0, 0); + return ns->root; +} + +spl_ns_id_t spl_ns_create(spl_namespace_t *ns, spl_ns_id_t parent, spl_ns_kind_t kind, + const char *name, usize owner_kind, usize owner_id) { + if (!ns) + return 0; + + spl_ns_id_t id = vec_size(ns->nodes); + char name_buf[32]; + const char *resolved_name = ns_name_or_default(name, id, name_buf, sizeof name_buf); + + const char *parent_qualified = NULL; + if (parent && parent < vec_size(ns->nodes)) + parent_qualified = vec_at(ns->nodes, parent).qualified; + + usize cap = strlen(resolved_name) + (parent_qualified ? strlen(parent_qualified) + 2 : 1); + char *qualified = malloc(cap); + if (!qualified) + return 0; + if (parent_qualified && *parent_qualified) + snprintf(qualified, cap, "%s.%s", parent_qualified, resolved_name); + else + snprintf(qualified, cap, "%s", resolved_name); + + spl_ns_node_t node = {0}; + node.id = id; + node.parent = parent; + node.kind = kind; + node.name = ns_strdup(resolved_name); + node.qualified = qualified; + node.owner_kind = owner_kind; + node.owner_id = owner_id; + map_init(node.symbols, MAP_HASH_STR, MAP_CMP_STR); + vec_push(ns->nodes, node); + + if (!ns->root) + ns->root = id; + return id; +} + +spl_ns_id_t spl_ns_alloc(spl_namespace_t *ns, spl_ns_kind_t kind, const char *name) { + if (!ns) + return 0; + spl_ns_id_t parent = ns->current ? ns->current : spl_ns_root(ns); + return spl_ns_create(ns, parent, kind, name, 0, 0); +} + +bool spl_ns_enter(spl_namespace_t *ns, spl_ns_id_t id) { + if (!ns || id == 0 || id >= vec_size(ns->nodes)) + return false; + ns->current = id; + return true; +} + +bool spl_ns_leave(spl_namespace_t *ns) { + if (!ns || ns->current == 0) + return false; + ns->current = vec_at(ns->nodes, ns->current).parent; + return true; +} + +bool spl_ns_insert(spl_namespace_t *ns, spl_ns_id_t id, spl_symbol_t symbol) { + if (!ns || id == 0 || id >= vec_size(ns->nodes) || !symbol.name) + return false; + spl_ns_node_t *node = &vec_at(ns->nodes, id); + spl_symbol_t old = {0}; + if (map_get(node->symbols, symbol.name, &old)) + return false; + map_put(node->symbols, symbol.name, symbol); + return true; +} + +bool spl_ns_lookup(spl_namespace_t *ns, const char *name, spl_symbol_t *out) { + if (!ns || !name || !out) + return false; + spl_ns_id_t id = ns->current ? ns->current : ns->root; + for (; id != 0 && id < vec_size(ns->nodes); id = vec_at(ns->nodes, id).parent) { + if (map_get(vec_at(ns->nodes, id).symbols, name, out)) + return true; + } + return false; +} + +bool spl_ns_lookup_local(spl_namespace_t *ns, spl_ns_id_t id, const char *name, + spl_symbol_t *out) { + if (!ns || id == 0 || id >= vec_size(ns->nodes) || !name || !out) + return false; + return map_get(vec_at(ns->nodes, id).symbols, name, out); +} + +const char *spl_ns_qualified(spl_namespace_t *ns, spl_ns_id_t id) { + if (!ns || id == 0 || id >= vec_size(ns->nodes)) + return NULL; + return vec_at(ns->nodes, id).qualified; +} + +bool spl_ns_symbol_qualified(spl_namespace_t *ns, spl_ns_id_t id, const char *local_name, + char *out, usize cap) { + if (!out || cap == 0) + return false; + const char *prefix = spl_ns_qualified(ns, id); + if (!prefix || !local_name) + return false; + int n = snprintf(out, cap, "%s.%s", prefix, local_name); + return n >= 0 && (usize)n < cap; +} + +spl_ns_id_t spl_ns_child(spl_namespace_t *ns, spl_ns_id_t parent, const char *name) { + if (!ns || !name) + return 0; + for (usize i = 1; i < vec_size(ns->nodes); ++i) { + spl_ns_node_t *n = &vec_at(ns->nodes, i); + if (n->parent == parent && n->name && strcmp(n->name, name) == 0) + return n->id; + } + return 0; +} diff --git a/stage1/sema/spl_scope.c b/stage1/sema/spl_scope.c deleted file mode 100644 index e48204e..0000000 --- a/stage1/sema/spl_scope.c +++ /dev/null @@ -1,46 +0,0 @@ -#include "spl_scope.h" - -void spl_scope_init(spl_scope_t *scope) { - vec_init(scope->scopes); - vec_push(scope->scopes, (spl_scope_node_t){0}); - scope->current_scope = 0; - scope->root_scope = 0; -} - -void spl_scope_drop(spl_scope_t *scope) { - for (usize i = 0; i < vec_size(scope->scopes); ++i) { - map_free(vec_at(scope->scopes, i).symbols); - } - vec_free(scope->scopes); - scope->current_scope = 0; - scope->root_scope = 0; -} - -spl_scope_id_t spl_scope_alloc(spl_scope_t *scope) { - spl_scope_node_t n = {0}; - n.parent = scope->current_scope; - map_init(n.symbols, MAP_HASH_STR, MAP_CMP_STR); - vec_push(scope->scopes, n); - return vec_size(scope->scopes) - 1; -} - -bool spl_scope_insert(spl_scope_t *scope, spl_scope_id_t id, spl_symbol_t symbol) { - Assert(id != 0 && id < vec_size(scope->scopes)); - spl_scope_node_t *node = &vec_at(scope->scopes, id); - spl_symbol_t old = {0}; - if (map_get(node->symbols, symbol.name, &old)) - return false; - map_put(node->symbols, symbol.name, symbol); - return true; -} - -bool spl_scope_find(spl_scope_t *scope, const char *name, spl_symbol_t *out) { - if (!scope || !name || !out) - return false; - for (spl_scope_id_t id = scope->current_scope; id != 0; id = vec_at(scope->scopes, id).parent) { - spl_scope_node_t *s = &vec_at(scope->scopes, id); - if (map_get(s->symbols, name, out)) - return true; - } - return false; -} \ No newline at end of file diff --git a/stage1/sema/spl_sema.c b/stage1/sema/spl_sema.c index 078adb6..ec76c34 100644 --- a/stage1/sema/spl_sema.c +++ b/stage1/sema/spl_sema.c @@ -222,16 +222,22 @@ static void sema_type_to_str(spl_sema_t *sema, spl_lang_type_id_t tid, char *buf } } +static inline spl_ns_id_t sema_push_scope_kind(spl_sema_t *sema, spl_ns_kind_t kind, + const char *name) { + spl_ns_id_t new_scope = spl_ns_alloc(sema->ns, kind, name); + sema->ns->current = new_scope; + return new_scope; +} + static inline void sema_push_scope(spl_sema_t *sema) { - spl_scope_id_t new_scope = spl_scope_alloc(sema->scope); - sema->scope->current_scope = new_scope; + (void)sema_push_scope_kind(sema, SPL_NS_BLOCK, NULL); } static inline void sema_pop_scope(spl_sema_t *sema) { - spl_scope_id_t cur = sema->scope->current_scope; - if (cur == 0 || cur >= vec_size(sema->scope->scopes)) + spl_ns_id_t cur = sema->ns->current; + if (cur == 0 || cur >= vec_size(sema->ns->nodes)) Panic("sema_pop_scope: bad current_scope %zu", cur); - sema->scope->current_scope = vec_at(sema->scope->scopes, cur).parent; + sema->ns->current = vec_at(sema->ns->nodes, cur).parent; } static spl_lang_type_id_t sema_for_elem_type(spl_sema_t *sema, spl_lang_type_id_t lang_tid) { @@ -325,7 +331,7 @@ static spl_lang_type_id_t sema_build_aggregate(spl_sema_t *sema, spl_ast_node_t spl_lang_type_id_t saved_agg = sema->current_aggregate_lang; sema->current_aggregate_lang = agg_lang; /* 聚合体自成命名空间:成员类型可引用同体声明的嵌套类型(裸名)。 */ - sema_push_scope(sema); + (void)sema_push_scope_kind(sema, SPL_NS_AGGREGATE, NULL); spl_type_member_vec_t members; vec_init(members); @@ -354,7 +360,7 @@ static spl_lang_type_id_t sema_build_aggregate(spl_sema_t *sema, spl_ast_node_t vec_push(methods, meth); spl_symbol_t fsym = {.name = mn->fn_decl.name, .kind = SPL_SYMBOL_KIND_FN}; fsym.node = fn_def; - spl_scope_insert(sema->scope, sema->scope->current_scope, fsym); + spl_ns_insert(sema->ns, sema->ns->current, fsym); } else if (mn->kind == SPL_AST_TYPE_DECL) { spl_ast_node_t *tpat = spl_ast_node(sema->ast, mn->type_decl.pattern); if (!tpat || tpat->kind != SPL_AST_PATTERN_IDENT) @@ -375,7 +381,7 @@ static spl_lang_type_id_t sema_build_aggregate(spl_sema_t *sema, spl_ast_node_t vec_push(methods, meth); spl_symbol_t tsym = {.name = tname, .kind = SPL_SYMBOL_KIND_TYPE}; tsym.node = tdef; - spl_scope_insert(sema->scope, sema->scope->current_scope, tsym); + spl_ns_insert(sema->ns, sema->ns->current, tsym); } } @@ -420,8 +426,9 @@ static spl_lang_type_id_t sema_build_aggregate(spl_sema_t *sema, spl_ast_node_t /* 文件 container 建成匿名 struct:FN_DECL / TYPE_DECL / VAR_DECL / CONST_DECL 进 methods(复用为 names),MEMBER_DECL 进字段。 */ -static spl_lang_type_id_t sema_build_container_aggregate(spl_sema_t *sema, spl_ast_node_t *n, - spl_def_id_t owner_def_id) { +static spl_lang_type_id_t sema_build_decl_aggregate(spl_sema_t *sema, + spl_ast_node_ref_vec_t *decls, + spl_def_id_t owner_def_id) { spl_type_id_t agg_ir = spl_lang_reserve_ir(sema->type); spl_lang_type_node_t ln = {0}; @@ -444,8 +451,8 @@ static spl_lang_type_id_t sema_build_container_aggregate(spl_sema_t *sema, spl_a spl_lang_method_vec_t methods; vec_init(methods); - for (usize i = 0; i < vec_size(n->container_members.declarations); ++i) { - spl_ast_node_ref_t dref = vec_at(n->container_members.declarations, i); + for (usize i = 0; i < vec_size(*decls); ++i) { + spl_ast_node_ref_t dref = vec_at(*decls, i); spl_ast_node_t *d = spl_ast_node(sema->ast, dref); if (!d) continue; @@ -486,6 +493,23 @@ static spl_lang_type_id_t sema_build_container_aggregate(spl_sema_t *sema, spl_a continue; spl_lang_method_t meth = {.name = pat->pattern_ident.name, .def_id = vdef}; vec_push(methods, meth); + } else if (d->kind == SPL_AST_MOD_DECL && d->mod_decl.has_body) { + spl_def_id_t mdef = d->resolved_outer_def_id ? d->resolved_outer_def_id + : d->resolved_def_id; + if (!mdef) + continue; + const char *mname = spl_module_last_segment(d->mod_decl.path); + spl_ns_id_t top_ns = d->resolved_ns_id; + while (top_ns && top_ns < vec_size(sema->ns->nodes)) { + spl_ns_node_t *node = &vec_at(sema->ns->nodes, top_ns); + if (node->parent == sema->ns->current) { + mname = node->name; + break; + } + top_ns = node->parent; + } + spl_lang_method_t meth = {.name = mname, .def_id = mdef}; + vec_push(methods, meth); } } @@ -508,6 +532,22 @@ static spl_lang_type_id_t sema_build_container_aggregate(spl_sema_t *sema, spl_a return agg_lang; } +static spl_lang_type_id_t sema_build_container_aggregate(spl_sema_t *sema, spl_ast_node_t *n, + spl_def_id_t owner_def_id) { + return sema_build_decl_aggregate(sema, &n->container_members.declarations, owner_def_id); +} + +static spl_lang_type_id_t sema_build_inline_aggregate(spl_sema_t *sema, spl_ast_node_t *n, + spl_def_id_t owner_def_id) { + spl_ns_id_t saved = sema->ns->current; + if (n->resolved_ns_id) + sema->ns->current = n->resolved_ns_id; + spl_lang_type_id_t result = + sema_build_decl_aggregate(sema, &n->mod_decl.decls, owner_def_id); + sema->ns->current = saved; + return result; +} + static spl_lang_type_id_t sema_import(spl_sema_t *sema, spl_ast_node_t *n, spl_ast_node_ref_vec_t *args) { if (!sema->loader || !sema->loader->load) { @@ -547,13 +587,11 @@ static spl_lang_type_id_t sema_import(spl_sema_t *sema, spl_ast_node_t *n, } spl_ast_t *saved_ast = sema->ast; - spl_scope_id_t saved_root = sema->scope->root_scope; - spl_scope_id_t saved_cur = sema->scope->current_scope; + spl_ns_id_t saved_cur = sema->ns->current; spl_lang_type_id_t saved_agg = sema->current_aggregate_lang; sema->ast = mod_ast; - sema->scope->root_scope = 0; - sema->scope->current_scope = 0; + sema->ns->current = 0; sema->current_aggregate_lang = 0; spl_sema_run_module(sema, cm_ref); @@ -567,8 +605,7 @@ static spl_lang_type_id_t sema_import(spl_sema_t *sema, spl_ast_node_t *n, } sema->ast = saved_ast; - sema->scope->root_scope = saved_root; - sema->scope->current_scope = saved_cur; + sema->ns->current = saved_cur; sema->current_aggregate_lang = saved_agg; return t; @@ -662,6 +699,10 @@ static spl_lang_type_id_t sema_eval_decl(spl_sema_t *sema, spl_def_id_t def_id) result = sema_build_container_aggregate(sema, n, def_id); n->resolved_lang_tid = result; break; + case SPL_AST_MOD_DECL: + result = sema_build_inline_aggregate(sema, n, def_id); + n->resolved_lang_tid = result; + break; default: break; } @@ -680,6 +721,300 @@ static spl_lang_type_id_t sema_eval_decl(spl_sema_t *sema, spl_def_id_t def_id) return result; } +static spl_module_id_t sema_current_module_id(spl_sema_t *sema) { + if (!sema->modules || !sema->ast) + return 0; + return spl_module_find_ast(sema->modules, sema->ast); +} + +static spl_def_id_t sema_ensure_module_def(spl_sema_t *sema, spl_module_t *mod) { + if (!mod) + return 0; + if (mod->def_id) + return mod->def_id; + + spl_ast_node_t *cm = spl_ast_node(mod->ast, mod->container); + if (!cm || cm->kind != SPL_AST_CONTAINER_MEMBERS) + return 0; + + spl_def_id_t def = + spl_def_alloc(sema->type, SPL_DEF_AGG, mod->name, cm->dbg, mod->ast, mod->container); + mod->def_id = def; + if (sema->ast == mod->ast) + cm->resolved_def_id = def; + return def; +} + +static spl_lang_type_id_t sema_ensure_module(spl_sema_t *sema, spl_module_id_t id) { + spl_module_t *mod = spl_module_get(sema->modules, id); + if (!mod) + return 0; + if (mod->sema_state == 2) + return mod->lang_id; + if (mod->sema_state == 1) { + spl_diag_error(sema->diag, SPL_DIAG_KIND_CIRCULAR_DEP, NULL, + "circular module import involving `%s`", mod->name); + return 0; + } + + spl_ast_t *saved_ast = sema->ast; + spl_ns_id_t saved_cur = sema->ns->current; + spl_lang_type_id_t saved_agg = sema->current_aggregate_lang; + + sema->ast = mod->ast; + sema->ns->current = 0; + sema->current_aggregate_lang = 0; + + spl_sema_run_module(sema, mod->container); + + spl_ast_node_t *cm = spl_ast_node(mod->ast, mod->container); + if (cm && cm->resolved_def_id) { + mod->def_id = cm->resolved_def_id; + spl_def_node_t *def = spl_def_node(sema->type, mod->def_id); + if (def) { + mod->lang_id = def->lang_id; + def->state = SPL_DECL_RESOLVED; + } + } + + sema->ast = saved_ast; + sema->ns->current = saved_cur; + sema->current_aggregate_lang = saved_agg; + return mod->lang_id; +} + +static spl_module_id_t sema_resolve_import(spl_sema_t *sema, spl_ast_node_t *n) { + if (!sema->modules || !sema->loader || !sema->loader->load) { + spl_diag_error(sema->diag, SPL_DIAG_KIND_INTERNAL, &n->dbg, + "import: no module loader configured"); + return 0; + } + + if (!n->import_decl.is_path) { + spl_ast_t *imported = + sema->loader->load(sema->loader->user, n->dbg.fname, n->import_decl.path, sema->diag); + if (!imported) + return 0; + spl_module_id_t id = spl_module_find_ast(sema->modules, imported); + if (!id) + spl_diag_error(sema->diag, SPL_DIAG_KIND_INTERNAL, &n->dbg, + "imported file `%s` was not registered", n->import_decl.path); + return id; + } + + spl_module_id_t id = spl_module_find_name(sema->modules, n->import_decl.path); + if (!id) + spl_diag_error(sema->diag, SPL_DIAG_KIND_UNDEFINED_SYMBOL, &n->dbg, + "module `%s` is not loaded", n->import_decl.path); + return id; +} + +static void sema_bind_import(spl_sema_t *sema, spl_ast_node_t *n) { + spl_module_id_t id = sema_resolve_import(sema, n); + if (!id) + return; + + spl_module_t *mod = spl_module_get(sema->modules, id); + if (!mod) + return; + + spl_def_id_t def = sema_ensure_module_def(sema, mod); + if (!def) + return; + + const char *local = n->import_decl.alias; + if (!local || !*local) + local = spl_module_last_segment(mod->name); + if (!local || !*local) + return; + + spl_symbol_t sym = {.name = local, .kind = SPL_SYMBOL_KIND_TYPE, .node = def}; + if (!spl_ns_insert(sema->ns, sema->ns->current, sym)) { + spl_diag_error(sema->diag, SPL_DIAG_KIND_DUPLICATE_SYMBOL, &n->dbg, + "duplicate local name `%s` for imported module", local); + return; + } + + (void)sema_ensure_module(sema, id); +} + +static void sema_collect_names(spl_sema_t *sema, spl_ast_node_ref_t ref); + +static spl_ns_id_t sema_ns_path(spl_sema_t *sema, spl_ns_id_t parent, const char *path, + spl_ns_id_t *out_last_parent) { + if (out_last_parent) + *out_last_parent = parent; + if (!path || !*path) + return parent; + + usize len = strlen(path); + char *buf = malloc(len + 1); + if (!buf) + return parent; + memcpy(buf, path, len + 1); + + spl_ns_id_t ns = parent; + char *seg = buf; + while (seg && *seg) { + char *dot = strchr(seg, '.'); + if (dot) + *dot = '\0'; + spl_ns_id_t child = spl_ns_child(sema->ns, ns, seg); + if (!child) + child = spl_ns_create(sema->ns, ns, SPL_NS_MODULE, seg, 0, 0); + if (out_last_parent && (!dot || !dot[1])) + *out_last_parent = ns; + ns = child; + if (!dot) + break; + seg = dot + 1; + } + free(buf); + return ns; +} + +static void sema_ns_aggregate_add_method(spl_sema_t *sema, spl_def_id_t def_id, + const char *method_name, spl_def_id_t member_def) { + spl_def_node_t *def = spl_def_node(sema->type, def_id); + if (!def || !def_id) + return; + + if (def->lang_id == 0) { + spl_type_id_t agg_ir = spl_lang_reserve_ir(sema->type); + spl_lang_type_node_t ln = {0}; + ln.kind = SPL_LANG_TYPE_STRUCT; + ln.ir_type_id = agg_ir; + vec_init(ln.methods); + spl_lang_type_id_t lang = spl_lang_node_push(sema->type, &ln); + def->lang_id = lang; + + spl_type_node_t tn = {0}; + tn.kind = SPL_TYPE_STRUCT; + tn.layout.mode = SPL_TYPE_LAYOUT_AUTO; + spl_lang_define_ir(sema->type, agg_ir, &tn); + } + + spl_lang_type_node_t *ln = spl_lang_type_node(sema->type, def->lang_id); + if (!ln) + return; + for (usize i = 0; i < vec_size(ln->methods); ++i) + if (strcmp(vec_at(ln->methods, i).name, method_name) == 0) + return; + spl_lang_method_t meth = {.name = method_name, .def_id = member_def}; + vec_push(ln->methods, meth); +} + +static spl_def_id_t sema_build_inline_prefixes(spl_sema_t *sema, const char *path, + spl_def_id_t final_def, spl_ns_id_t parent_ns, + spl_dbg_node_t dbg, spl_ns_id_t *out_top_ns) { + if (out_top_ns) + *out_top_ns = 0; + if (!path || !*path) + return final_def; + + usize len = strlen(path); + char *buf = malloc(len + 1); + if (!buf) + return final_def; + memcpy(buf, path, len + 1); + + VEC(const char *) segs; + vec_init(segs); + char *p = buf; + while (p && *p) { + vec_push(segs, p); + char *dot = strchr(p, '.'); + if (!dot) + break; + *dot = '\0'; + p = dot + 1; + } + + usize count = vec_size(segs); + if (count == 0) { + vec_free(segs); + free(buf); + return final_def; + } + if (out_top_ns) { + spl_ns_id_t ns = parent_ns; + for (usize i = 0; i < count; ++i) { + ns = spl_ns_child(sema->ns, ns, segs.data[i]); + if (!ns) + break; + } + if (count == 1) + *out_top_ns = ns; + else + *out_top_ns = spl_ns_child(sema->ns, parent_ns, segs.data[0]); + } + + spl_def_id_t child_def = final_def; + for (usize i = count - 1; i > 0; --i) { + spl_ns_id_t prefix_ns = parent_ns; + for (usize j = 0; j < i; ++j) { + prefix_ns = spl_ns_child(sema->ns, prefix_ns, segs.data[j]); + if (!prefix_ns) + break; + } + if (!prefix_ns) + break; + + spl_ns_node_t *node = &vec_at(sema->ns->nodes, prefix_ns); + if (!node->owner_id) { + const char *qual = node->qualified; + node->owner_kind = SPL_NS_AGGREGATE; + node->owner_id = + spl_def_alloc(sema->type, SPL_DEF_AGG, qual ? qual : segs.data[i - 1], dbg, + sema->ast, 0); + } + spl_def_id_t prefix_def = node->owner_id; + sema_ns_aggregate_add_method(sema, prefix_def, segs.data[i], child_def); + child_def = prefix_def; + } + + vec_free(segs); + free(buf); + return child_def; +} + +static void sema_collect_inline_module(spl_sema_t *sema, spl_ast_node_t *n, + spl_ast_node_ref_t ref) { + if (!n || n->kind != SPL_AST_MOD_DECL || !n->mod_decl.has_body) + return; + if (n->resolved_def_id) + return; + + spl_ns_id_t last_parent = sema->ns->current; + spl_ns_id_t inline_ns = sema_ns_path(sema, sema->ns->current, n->mod_decl.path, &last_parent); + n->resolved_ns_id = inline_ns; + + const char *full = spl_ns_qualified(sema->ns, inline_ns); + spl_def_id_t def = spl_def_alloc(sema->type, SPL_DEF_AGG, full ? full : n->mod_decl.path, + n->dbg, sema->ast, ref); + n->resolved_def_id = def; + + spl_ns_id_t top_ns = 0; + spl_def_id_t outer_def = + sema_build_inline_prefixes(sema, n->mod_decl.path, def, sema->ns->current, n->dbg, + &top_ns); + n->resolved_outer_def_id = outer_def; + const char *local = top_ns ? vec_at(sema->ns->nodes, top_ns).name + : spl_module_last_segment(n->mod_decl.path); + spl_symbol_t sym = {.name = local, .kind = SPL_SYMBOL_KIND_TYPE, .node = outer_def}; + if (!spl_ns_insert(sema->ns, sema->ns->current, sym)) { + spl_diag_error(sema->diag, SPL_DIAG_KIND_DUPLICATE_SYMBOL, &n->dbg, + "duplicate symbol `%s`", local ? local : "?"); + return; + } + + spl_ns_id_t saved = sema->ns->current; + sema->ns->current = inline_ns; + for (usize i = 0; i < vec_size(n->mod_decl.decls); ++i) + sema_collect_names(sema, vec_at(n->mod_decl.decls, i)); + sema->ns->current = saved; +} + static void sema_collect_names(spl_sema_t *sema, spl_ast_node_ref_t ref) { spl_ast_node_t *n = spl_ast_node(sema->ast, ref); if (!n) @@ -687,25 +1022,46 @@ static void sema_collect_names(spl_sema_t *sema, spl_ast_node_ref_t ref) { switch (n->kind) { case SPL_AST_CONTAINER_MEMBERS: { - if (sema->scope->root_scope == 0) { - spl_scope_id_t root = spl_scope_alloc(sema->scope); - sema->scope->root_scope = root; + if (sema->ns->root == 0) { + spl_ns_id_t root = spl_ns_alloc(sema->ns, SPL_NS_BLOCK, NULL); + sema->ns->root = root; } - sema->scope->current_scope = sema->scope->root_scope; - spl_scope_id_t mod_scope = spl_scope_alloc(sema->scope); - sema->scope->current_scope = mod_scope; + spl_module_id_t mid = sema_current_module_id(sema); + spl_module_t *mod = spl_module_get(sema->modules, mid); - spl_symbol_t file_sym = {.name = "$file", .kind = SPL_SYMBOL_KIND_TYPE}; - file_sym.node = spl_def_alloc(sema->type, SPL_DEF_AGG, "$file", n->dbg, sema->ast, ref); - n->resolved_def_id = file_sym.node; - spl_scope_insert(sema->scope, mod_scope, file_sym); + sema->ns->current = sema->ns->root; + spl_ns_id_t mod_scope = spl_ns_alloc(sema->ns, SPL_NS_MODULE, mod ? mod->name : NULL); + sema->ns->current = mod_scope; + n->resolved_ns_id = mod_scope; + if (mod) + mod->ns = mod_scope; + + spl_symbol_t file_sym = {.kind = SPL_SYMBOL_KIND_TYPE}; + if (mod) { + file_sym.name = spl_module_last_segment(mod->name); + file_sym.node = sema_ensure_module_def(sema, mod); + n->resolved_def_id = file_sym.node; + } else { + file_sym.name = "$file"; + file_sym.node = + spl_def_alloc(sema->type, SPL_DEF_AGG, "$file", n->dbg, sema->ast, ref); + n->resolved_def_id = file_sym.node; + if (file_sym.node) + spl_ns_insert(sema->ns, mod_scope, file_sym); + } for (usize i = 0; i < vec_size(n->container_members.declarations); ++i) { sema_collect_names(sema, vec_at(n->container_members.declarations, i)); } break; } + case SPL_AST_IMPORT_DECL: + sema_bind_import(sema, n); + break; + case SPL_AST_MOD_DECL: + sema_collect_inline_module(sema, n, ref); + break; case SPL_AST_TYPE_DECL: { spl_ast_node_t *pat = spl_ast_node(sema->ast, n->type_decl.pattern); if (!pat) @@ -726,7 +1082,8 @@ static void sema_collect_names(spl_sema_t *sema, spl_ast_node_ref_t ref) { else sym.node = spl_def_alloc(sema->type, SPL_DEF_SCALAR, name, te->dbg, sema->ast, ref); n->resolved_def_id = sym.node; - if (!spl_scope_insert(sema->scope, sema->scope->current_scope, sym)) { + n->resolved_ns_id = sema->ns->current; + if (!spl_ns_insert(sema->ns, sema->ns->current, sym)) { spl_diag_error(sema->diag, SPL_DIAG_KIND_DUPLICATE_SYMBOL, &n->dbg, "duplicate symbol `%s`", name); } @@ -736,7 +1093,8 @@ static void sema_collect_names(spl_sema_t *sema, spl_ast_node_ref_t ref) { spl_symbol_t sym = {.name = n->fn_decl.name, .kind = SPL_SYMBOL_KIND_FN}; sym.node = spl_def_alloc(sema->type, SPL_DEF_FN, sym.name, n->dbg, sema->ast, ref); n->resolved_def_id = sym.node; - if (!spl_scope_insert(sema->scope, sema->scope->current_scope, sym)) { + n->resolved_ns_id = sema->ns->current; + if (!spl_ns_insert(sema->ns, sema->ns->current, sym)) { spl_diag_error(sema->diag, SPL_DIAG_KIND_DUPLICATE_SYMBOL, &n->dbg, "duplicate symbol `%s`", sym.name); } @@ -756,7 +1114,8 @@ static void sema_collect_names(spl_sema_t *sema, spl_ast_node_ref_t ref) { sym.node = spl_def_alloc(sema->type, SPL_DEF_VAR, name, n->dbg, sema->ast, ref); /* def_id 挂在 pattern 的 ident 上,不在 VAR_DECL 节点上 */ pat->resolved_def_id = sym.node; - if (!spl_scope_insert(sema->scope, sema->scope->current_scope, sym)) { + n->resolved_ns_id = sema->ns->current; + if (!spl_ns_insert(sema->ns, sema->ns->current, sym)) { spl_diag_error(sema->diag, SPL_DIAG_KIND_DUPLICATE_SYMBOL, &n->dbg, "duplicate symbol `%s`", name); } @@ -869,7 +1228,7 @@ static spl_lang_type_id_t sema_infer_type_context(spl_sema_t *sema, spl_ast_node } spl_symbol_t symbol; - if (!spl_scope_find(sema->scope, n->ident.name, &symbol) || + if (!spl_ns_lookup(sema->ns, n->ident.name, &symbol) || symbol.kind != SPL_SYMBOL_KIND_TYPE) { spl_diag_error(sema->diag, SPL_DIAG_KIND_TYPE_MISMATCH, &n->dbg, "`%s` is not a type", n->ident.name); @@ -1166,7 +1525,7 @@ static void sema_bind_pattern(spl_sema_t *sema, spl_ast_node_ref_t pat_ref, spl_symbol_t sym = {.name = name, .kind = SPL_SYMBOL_KIND_VAR}; sym.node = spl_def_alloc(sema->type, SPL_DEF_VAR, name, pat->dbg, sema->ast, pat_ref); spl_def_resolve(sema->type, sym.node, matched_tid); - if (!spl_scope_insert(sema->scope, sema->scope->current_scope, sym)) { + if (!spl_ns_insert(sema->ns, sema->ns->current, sym)) { spl_diag_error(sema->diag, SPL_DIAG_KIND_DUPLICATE_SYMBOL, &pat->dbg, "duplicate symbol `%s`", name); } @@ -1333,7 +1692,7 @@ static void sema_type_unpack_one(spl_sema_t *sema, spl_ast_node_t *pat, const ch } spl_symbol_t sym = {.name = name, .kind = k, .node = m->def_id}; - if (!spl_scope_insert(sema->scope, sema->scope->current_scope, sym)) { + if (!spl_ns_insert(sema->ns, sema->ns->current, sym)) { spl_diag_error(sema->diag, SPL_DIAG_KIND_DUPLICATE_SYMBOL, &pat->dbg, "type unpack: duplicate symbol `%s`", name); } @@ -1631,7 +1990,7 @@ static spl_lang_type_id_t sema_coerce(spl_sema_t *sema, spl_ast_node_ref_t ref, if (n->aggregate_literal.name) { spl_symbol_t sym; - if (!spl_scope_find(sema->scope, n->aggregate_literal.name, &sym)) { + if (!spl_ns_lookup(sema->ns, n->aggregate_literal.name, &sym)) { spl_diag_error(sema->diag, SPL_DIAG_KIND_UNDEFINED_SYMBOL, &n->dbg, "unknown type `%s`", n->aggregate_literal.name); return 0; @@ -1846,7 +2205,7 @@ static spl_lang_type_id_t sema_infer_undefined_lit(spl_sema_t *sema, spl_ast_nod static spl_lang_type_id_t sema_infer_ident(spl_sema_t *sema, spl_ast_node_t *n) { const char *name = n->ident.name; spl_symbol_t sym; - if (!spl_scope_find(sema->scope, name, &sym)) { + if (!spl_ns_lookup(sema->ns, name, &sym)) { spl_diag_error(sema->diag, SPL_DIAG_KIND_UNDEFINED_SYMBOL, &n->dbg, "undefined identifier `%s`", name); return 0; @@ -2307,7 +2666,7 @@ static spl_lang_type_id_t sema_infer_aggregate_lit(spl_sema_t *sema, spl_ast_nod return inf; } spl_symbol_t sym; - if (!spl_scope_find(sema->scope, type_name, &sym)) { + if (!spl_ns_lookup(sema->ns, type_name, &sym)) { spl_diag_error(sema->diag, SPL_DIAG_KIND_UNDEFINED_SYMBOL, &n->dbg, "unknown type `%s`", type_name); return 0; @@ -2544,7 +2903,7 @@ static spl_lang_type_id_t sema_infer_type_path(spl_sema_t *sema, spl_ast_node_t return b; } spl_symbol_t sym; - if (!spl_scope_find(sema->scope, name, &sym)) { + if (!spl_ns_lookup(sema->ns, name, &sym)) { spl_diag_error(sema->diag, SPL_DIAG_KIND_UNDEFINED_SYMBOL, &n->dbg, "type `%s` not found", name); return 0; @@ -2662,7 +3021,7 @@ static spl_lang_type_id_t sema_infer_fn_decl(spl_sema_t *sema, spl_ast_node_t *n sema->current_aggregate_lang = owner->lang_id; } - sema_push_scope(sema); + (void)sema_push_scope_kind(sema, SPL_NS_FUNCTION, n->fn_decl.name); for (usize i = 0; i < vec_size(n->fn_decl.params); ++i) { spl_ast_node_t *pd = spl_ast_node(sema->ast, vec_at(n->fn_decl.params, i)); if (pd && pd->kind == SPL_AST_PARAM_DECL) { @@ -2676,7 +3035,7 @@ static spl_lang_type_id_t sema_infer_fn_decl(spl_sema_t *sema, spl_ast_node_t *n pdef->state = p_tid ? SPL_DECL_RESOLVED : SPL_DECL_ERROR; } pd->resolved_def_id = psym.node; - if (!spl_scope_insert(sema->scope, sema->scope->current_scope, psym)) { + if (!spl_ns_insert(sema->ns, sema->ns->current, psym)) { spl_diag_error(sema->diag, SPL_DIAG_KIND_DUPLICATE_SYMBOL, &pd->dbg, "duplicate symbol `%s`", psym.name); } @@ -2776,12 +3135,27 @@ static spl_lang_type_id_t sema_infer(spl_sema_t *sema, spl_ast_node_ref_t ref) { switch (n->kind) { - case SPL_AST_MOD_DECL: case SPL_AST_IMPORT_DECL: - spl_diag_error(sema->diag, SPL_DIAG_KIND_UNHANDLED_NODE, &n->dbg, - "module system is not implemented yet"); return 0; + case SPL_AST_MOD_DECL: { + if (!n->mod_decl.has_body) + return 0; + if (!n->resolved_def_id) + return 0; + if (!n->resolved_def_id) + return 0; + if (n->resolved_lang_tid == 0) + (void)sema_eval_decl(sema, n->resolved_def_id); + spl_ns_id_t saved = sema->ns->current; + if (n->resolved_ns_id) + sema->ns->current = n->resolved_ns_id; + for (usize i = 0; i < vec_size(n->mod_decl.decls); ++i) + sema_infer(sema, vec_at(n->mod_decl.decls, i)); + sema->ns->current = saved; + return 0; + } + case SPL_AST_EXPR_INT_LIT: return sema_infer_int_lit(sema, n); case SPL_AST_EXPR_FLOAT_LIT: @@ -2960,12 +3334,12 @@ static spl_lang_type_id_t sema_infer(spl_sema_t *sema, spl_ast_node_ref_t ref) { } void spl_sema_init(spl_sema_t *sema, spl_ast_t *ast, spl_lang_type_t *type, spl_layout_t *layout, - spl_scope_t *scope, spl_attr_t *attr) { - Assert(sema != NULL && ast != NULL && type != NULL && scope != NULL && attr != NULL); + spl_namespace_t *ns, spl_attr_t *attr) { + Assert(sema != NULL && ast != NULL && type != NULL && ns != NULL && attr != NULL); sema->ast = ast; sema->type = type; sema->layout = layout; - sema->scope = scope; + sema->ns = ns; sema->current_fn_ret_tid = 0; sema->current_aggregate_lang = 0; sema->current_loop_break_tid = 0; @@ -2973,6 +3347,7 @@ void spl_sema_init(spl_sema_t *sema, spl_ast_t *ast, spl_lang_type_t *type, spl_ sema->root = (spl_symbol_t){0}; vec_init(sema->label_stack); sema->loader = NULL; + sema->modules = NULL; sema->diag = NULL; } @@ -2986,7 +3361,20 @@ void spl_sema_run_module(spl_sema_t *sema, spl_ast_node_ref_t module_ref) { if (!mod || mod->kind != SPL_AST_CONTAINER_MEMBERS) return; - spl_scope_id_t saved_scope = sema->scope->current_scope; + spl_module_id_t mid = sema_current_module_id(sema); + spl_module_t *module = spl_module_get(sema->modules, mid); + if (module) { + if (module->sema_state == 2) + return; + if (module->sema_state == 1) { + spl_diag_error(sema->diag, SPL_DIAG_KIND_CIRCULAR_DEP, NULL, + "circular module import involving `%s`", module->name); + return; + } + module->sema_state = 1; + } + + spl_ns_id_t saved_scope = sema->ns->current; spl_lang_type_id_t saved_agg = sema->current_aggregate_lang; sema_collect_names(sema, module_ref); @@ -3002,7 +3390,19 @@ void spl_sema_run_module(spl_sema_t *sema, spl_ast_node_ref_t module_ref) { sema_infer(sema, module_ref); sema->current_aggregate_lang = saved_agg; - sema->scope->current_scope = saved_scope; + sema->ns->current = saved_scope; + + module = spl_module_get(sema->modules, mid); + if (module) { + spl_ast_node_t *cm = spl_ast_node(sema->ast, module_ref); + if (cm && cm->resolved_def_id) { + module->def_id = cm->resolved_def_id; + spl_def_node_t *def = spl_def_node(sema->type, module->def_id); + if (def) + module->lang_id = def->lang_id; + } + module->sema_state = 2; + } } void spl_sema_run(spl_sema_t *sema) { @@ -3025,8 +3425,8 @@ static int spl_sema_sym_cmp(const void *a, const void *b) { void spl_sema_dump(spl_sema_t *sema) { printf("Scopes:\n"); - for (usize i = 1; i < vec_size(sema->scope->scopes); ++i) { - spl_scope_node_t *scope = &vec_at(sema->scope->scopes, i); + for (usize i = 1; i < vec_size(sema->ns->nodes); ++i) { + spl_ns_node_t *scope = &vec_at(sema->ns->nodes, i); Assert(scope != NULL); printf("scope[%zu] parent=%zu\n", i, scope->parent);