ai: 实现mod语法 提供namespace框架

This commit is contained in:
zzy committed 2026-12-02 13:02:58 +08:00
1 parent e531aab2d3
commit 663adf6cae
11 files changed
+999 -185

No files matched your search

+24 -4
View File
@@ -21,6 +21,7 @@
#include "spl_ir2c.h" #include "spl_ir2c.h"
#include "spl_ir2vm.h" #include "spl_ir2vm.h"
#include "spl_lexer.h" #include "spl_lexer.h"
#include "spl_module.h"
#include "spl_parser.h" #include "spl_parser.h"
#include "spl_sema.h" #include "spl_sema.h"
#include "spl_tok.h" #include "spl_tok.h"
@@ -49,10 +50,17 @@ typedef struct {
spl_driver_module_vec_t modules; spl_driver_module_vec_t modules;
MAP(const char *, usize) path_to_idx; MAP(const char *, usize) path_to_idx;
spl_module_registry_t module_registry;
spl_diag_t *diag; spl_diag_t *diag;
} spl_driver_t; } 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) { static char *read_file(const char *path, long *out_len) {
FILE *f = fopen(path, "rb"); FILE *f = fopen(path, "rb");
if (!f) { 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).ast = ast;
vec_at(d->modules, idx).src = src; vec_at(d->modules, idx).src = src;
if (!driver_register_module(d, ast, key, diag))
return NULL;
return ast; 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); spl_ast_init(&drv.main_ast, &drv.main_toks);
map_init(drv.path_to_idx, MAP_HASH_STR, MAP_CMP_STR); map_init(drv.path_to_idx, MAP_HASH_STR, MAP_CMP_STR);
vec_init(drv.modules); vec_init(drv.modules);
spl_module_registry_init(&drv.module_registry);
int rc = 0; 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); dump_tokens(drv.main_toks);
free(drv.main_path); free(drv.main_path);
map_free(drv.path_to_idx); map_free(drv.path_to_idx);
spl_module_registry_drop(&drv.module_registry);
spl_diag_drop(&diag); spl_diag_drop(&diag);
return 0; 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_lang_type_t lang_type;
spl_layout_target_t layout_target; spl_layout_target_t layout_target;
spl_layout_t layout; spl_layout_t layout;
spl_scope_t scope; spl_namespace_t ns;
spl_sema_t sema; spl_sema_t sema;
spl_ast2ir_t a2ir; spl_ast2ir_t a2ir;
spl_ir_builder_t ir_builder; 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; 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); spl_attr_init(&attr);
attr_ok = 1; attr_ok = 1;
spl_lang_type_init(&lang_type); 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); spl_layout_init(&layout, &layout_target);
layout_ok = 1; layout_ok = 1;
spl_scope_init(&scope); spl_ns_init(&ns);
scope_ok = 1; 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; sema_ok = 1;
spl_module_loader_t loader = {.load = driver_load, .user = &drv}; spl_module_loader_t loader = {.load = driver_load, .user = &drv};
sema.loader = &loader; sema.loader = &loader;
@@ -397,7 +416,7 @@ cleanup:
if (sema_ok) if (sema_ok)
spl_sema_drop(&sema); spl_sema_drop(&sema);
if (scope_ok) if (scope_ok)
spl_scope_drop(&scope); spl_ns_drop(&ns);
if (layout_ok) if (layout_ok)
spl_layout_drop(&layout); spl_layout_drop(&layout);
if (lang_type_ok) if (lang_type_ok)
@@ -418,6 +437,7 @@ cleanup:
vec_free(drv.modules); vec_free(drv.modules);
/* map 的 key 是 module 里 strdup 出的 abs_path,已随 module 释放 */ /* map 的 key 是 module 里 strdup 出的 abs_path,已随 module 释放 */
map_free(drv.path_to_idx); map_free(drv.path_to_idx);
spl_module_registry_drop(&drv.module_registry);
free(drv.main_path); free(drv.main_path);
spl_diag_drop(&diag); spl_diag_drop(&diag);
return rc; return rc;
+2
View File
@@ -159,7 +159,9 @@ struct spl_ast_node {
spl_dbg_node_t dbg; spl_dbg_node_t dbg;
usize resolved_def_id; usize resolved_def_id;
usize resolved_outer_def_id;
usize resolved_lang_tid; usize resolved_lang_tid;
usize resolved_ns_id;
usize expected_lang_tid; usize expected_lang_tid;
spl_attr_id_vec_t attrs; spl_attr_id_vec_t attrs;
+63
View File
@@ -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__ */
+71
View File
@@ -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__ */
-42
View File
@@ -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__ */
+5 -3
View File
@@ -7,7 +7,8 @@
#include "spl_lang_type.h" #include "spl_lang_type.h"
#include "spl_layout.h" #include "spl_layout.h"
#include "spl_mpa.h" #include "spl_mpa.h"
#include "spl_scope.h" #include "spl_module.h"
#include "spl_namespace.h"
#include "spl_type.h" #include "spl_type.h"
typedef struct spl_module_loader spl_module_loader_t; typedef struct spl_module_loader spl_module_loader_t;
@@ -23,7 +24,7 @@ typedef struct {
spl_ast_t *ast; spl_ast_t *ast;
spl_lang_type_t *type; spl_lang_type_t *type;
spl_layout_t *layout; /* 由 splc0 拥有并解析;sema 仅借用 */ spl_layout_t *layout; /* 由 splc0 拥有并解析;sema 仅借用 */
spl_scope_t *scope; spl_namespace_t *ns;
spl_symbol_t root; spl_symbol_t root;
spl_attr_t *attr; spl_attr_t *attr;
spl_lang_type_id_t current_aggregate_lang; spl_lang_type_id_t current_aggregate_lang;
@@ -31,11 +32,12 @@ typedef struct {
spl_lang_type_id_t current_loop_break_tid; spl_lang_type_id_t current_loop_break_tid;
spl_symbol_path_t label_stack; spl_symbol_path_t label_stack;
const spl_module_loader_t *loader; const spl_module_loader_t *loader;
spl_module_registry_t *modules;
spl_diag_t *diag; spl_diag_t *diag;
} spl_sema_t; } spl_sema_t;
void spl_sema_init(spl_sema_t *sema, spl_ast_t *ast, spl_lang_type_t *type, 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_drop(spl_sema_t *sema);
void spl_sema_run_module(spl_sema_t *sema, spl_ast_node_ref_t module_ref); void spl_sema_run_module(spl_sema_t *sema, spl_ast_node_ref_t module_ref);
+53 -42
View File
@@ -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) { static void emit_decls_reserve(spl_ast2ir_t *a, spl_ast_node_ref_vec_t *decls) {
spl_ast_node_t *n = spl_ast_node(a->ast, ref); for (usize i = 0; i < vec_size(*decls); ++i) {
if (!n) spl_ast_node_ref_t dref = vec_at(*decls, i);
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);
spl_ast_node_t *d = spl_ast_node(a->ast, dref); spl_ast_node_t *d = spl_ast_node(a->ast, dref);
if (!d) if (!d)
continue; 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); emit_aggregate_fns_reserve(a, te);
break; break;
} }
case SPL_AST_TOP_LEVEL_BLOCK: case SPL_AST_VAR_DECL:
emit_container_reserve(a, dref); 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; break;
default: default:
break; 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) if (n->kind != SPL_AST_CONTAINER_MEMBERS)
return; return;
emit_decls_body(a, &n->container_members.declarations);
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;
}
}
} }
static spl_ir_node_ref_t emit_implicit_cast(spl_ast2ir_t *a, spl_ast_node_t *n, spl_type_id_t ft, static spl_ir_node_ref_t emit_implicit_cast(spl_ast2ir_t *a, spl_ast_node_t *n, spl_type_id_t ft,
+166
View File
@@ -0,0 +1,166 @@
#include "spl_module.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
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;
}
+167
View File
@@ -0,0 +1,167 @@
#include "spl_namespace.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}
-46
View File
@@ -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;
}
+448 -48
View File
@@ -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) { static inline void sema_push_scope(spl_sema_t *sema) {
spl_scope_id_t new_scope = spl_scope_alloc(sema->scope); (void)sema_push_scope_kind(sema, SPL_NS_BLOCK, NULL);
sema->scope->current_scope = new_scope;
} }
static inline void sema_pop_scope(spl_sema_t *sema) { static inline void sema_pop_scope(spl_sema_t *sema) {
spl_scope_id_t cur = sema->scope->current_scope; spl_ns_id_t cur = sema->ns->current;
if (cur == 0 || cur >= vec_size(sema->scope->scopes)) if (cur == 0 || cur >= vec_size(sema->ns->nodes))
Panic("sema_pop_scope: bad current_scope %zu", cur); 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) { 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; spl_lang_type_id_t saved_agg = sema->current_aggregate_lang;
sema->current_aggregate_lang = agg_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; spl_type_member_vec_t members;
vec_init(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); vec_push(methods, meth);
spl_symbol_t fsym = {.name = mn->fn_decl.name, .kind = SPL_SYMBOL_KIND_FN}; spl_symbol_t fsym = {.name = mn->fn_decl.name, .kind = SPL_SYMBOL_KIND_FN};
fsym.node = fn_def; 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) { } else if (mn->kind == SPL_AST_TYPE_DECL) {
spl_ast_node_t *tpat = spl_ast_node(sema->ast, mn->type_decl.pattern); spl_ast_node_t *tpat = spl_ast_node(sema->ast, mn->type_decl.pattern);
if (!tpat || tpat->kind != SPL_AST_PATTERN_IDENT) 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); vec_push(methods, meth);
spl_symbol_t tsym = {.name = tname, .kind = SPL_SYMBOL_KIND_TYPE}; spl_symbol_t tsym = {.name = tname, .kind = SPL_SYMBOL_KIND_TYPE};
tsym.node = tdef; tsym.node = tdef;
spl_scope_insert(sema->scope, sema->scope->current_scope, tsym); spl_ns_insert(sema->ns, sema->ns->current, tsym);
} }
} }
@@ -420,7 +426,8 @@ 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 /* 文件 container 建成匿名 struct:FN_DECL / TYPE_DECL / VAR_DECL / CONST_DECL
进 methods(复用为 names),MEMBER_DECL 进字段。 */ 进 methods(复用为 names),MEMBER_DECL 进字段。 */
static spl_lang_type_id_t sema_build_container_aggregate(spl_sema_t *sema, spl_ast_node_t *n, 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_def_id_t owner_def_id) {
spl_type_id_t agg_ir = spl_lang_reserve_ir(sema->type); spl_type_id_t agg_ir = spl_lang_reserve_ir(sema->type);
@@ -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; spl_lang_method_vec_t methods;
vec_init(methods); vec_init(methods);
for (usize i = 0; i < vec_size(n->container_members.declarations); ++i) { for (usize i = 0; i < vec_size(*decls); ++i) {
spl_ast_node_ref_t dref = vec_at(n->container_members.declarations, i); spl_ast_node_ref_t dref = vec_at(*decls, i);
spl_ast_node_t *d = spl_ast_node(sema->ast, dref); spl_ast_node_t *d = spl_ast_node(sema->ast, dref);
if (!d) if (!d)
continue; continue;
@@ -486,6 +493,23 @@ static spl_lang_type_id_t sema_build_container_aggregate(spl_sema_t *sema, spl_a
continue; continue;
spl_lang_method_t meth = {.name = pat->pattern_ident.name, .def_id = vdef}; spl_lang_method_t meth = {.name = pat->pattern_ident.name, .def_id = vdef};
vec_push(methods, meth); 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; 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, static spl_lang_type_id_t sema_import(spl_sema_t *sema, spl_ast_node_t *n,
spl_ast_node_ref_vec_t *args) { spl_ast_node_ref_vec_t *args) {
if (!sema->loader || !sema->loader->load) { 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_ast_t *saved_ast = sema->ast;
spl_scope_id_t saved_root = sema->scope->root_scope; spl_ns_id_t saved_cur = sema->ns->current;
spl_scope_id_t saved_cur = sema->scope->current_scope;
spl_lang_type_id_t saved_agg = sema->current_aggregate_lang; spl_lang_type_id_t saved_agg = sema->current_aggregate_lang;
sema->ast = mod_ast; sema->ast = mod_ast;
sema->scope->root_scope = 0; sema->ns->current = 0;
sema->scope->current_scope = 0;
sema->current_aggregate_lang = 0; sema->current_aggregate_lang = 0;
spl_sema_run_module(sema, cm_ref); 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->ast = saved_ast;
sema->scope->root_scope = saved_root; sema->ns->current = saved_cur;
sema->scope->current_scope = saved_cur;
sema->current_aggregate_lang = saved_agg; sema->current_aggregate_lang = saved_agg;
return t; 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); result = sema_build_container_aggregate(sema, n, def_id);
n->resolved_lang_tid = result; n->resolved_lang_tid = result;
break; break;
case SPL_AST_MOD_DECL:
result = sema_build_inline_aggregate(sema, n, def_id);
n->resolved_lang_tid = result;
break;
default: default:
break; 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; 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) { 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); spl_ast_node_t *n = spl_ast_node(sema->ast, ref);
if (!n) if (!n)
@@ -687,25 +1022,46 @@ static void sema_collect_names(spl_sema_t *sema, spl_ast_node_ref_t ref) {
switch (n->kind) { switch (n->kind) {
case SPL_AST_CONTAINER_MEMBERS: { case SPL_AST_CONTAINER_MEMBERS: {
if (sema->scope->root_scope == 0) { if (sema->ns->root == 0) {
spl_scope_id_t root = spl_scope_alloc(sema->scope); spl_ns_id_t root = spl_ns_alloc(sema->ns, SPL_NS_BLOCK, NULL);
sema->scope->root_scope = root; sema->ns->root = root;
} }
sema->scope->current_scope = sema->scope->root_scope; spl_module_id_t mid = sema_current_module_id(sema);
spl_scope_id_t mod_scope = spl_scope_alloc(sema->scope); spl_module_t *mod = spl_module_get(sema->modules, mid);
sema->scope->current_scope = mod_scope;
spl_symbol_t file_sym = {.name = "$file", .kind = SPL_SYMBOL_KIND_TYPE}; sema->ns->current = sema->ns->root;
file_sym.node = spl_def_alloc(sema->type, SPL_DEF_AGG, "$file", n->dbg, sema->ast, ref); 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; n->resolved_def_id = file_sym.node;
spl_scope_insert(sema->scope, mod_scope, file_sym); } 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) { for (usize i = 0; i < vec_size(n->container_members.declarations); ++i) {
sema_collect_names(sema, vec_at(n->container_members.declarations, i)); sema_collect_names(sema, vec_at(n->container_members.declarations, i));
} }
break; 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: { case SPL_AST_TYPE_DECL: {
spl_ast_node_t *pat = spl_ast_node(sema->ast, n->type_decl.pattern); spl_ast_node_t *pat = spl_ast_node(sema->ast, n->type_decl.pattern);
if (!pat) if (!pat)
@@ -726,7 +1082,8 @@ static void sema_collect_names(spl_sema_t *sema, spl_ast_node_ref_t ref) {
else else
sym.node = spl_def_alloc(sema->type, SPL_DEF_SCALAR, name, te->dbg, sema->ast, ref); sym.node = spl_def_alloc(sema->type, SPL_DEF_SCALAR, name, te->dbg, sema->ast, ref);
n->resolved_def_id = sym.node; 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, spl_diag_error(sema->diag, SPL_DIAG_KIND_DUPLICATE_SYMBOL, &n->dbg,
"duplicate symbol `%s`", name); "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}; 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); sym.node = spl_def_alloc(sema->type, SPL_DEF_FN, sym.name, n->dbg, sema->ast, ref);
n->resolved_def_id = sym.node; 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, spl_diag_error(sema->diag, SPL_DIAG_KIND_DUPLICATE_SYMBOL, &n->dbg,
"duplicate symbol `%s`", sym.name); "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); sym.node = spl_def_alloc(sema->type, SPL_DEF_VAR, name, n->dbg, sema->ast, ref);
/* def_id 挂在 pattern 的 ident 上,不在 VAR_DECL 节点上 */ /* def_id 挂在 pattern 的 ident 上,不在 VAR_DECL 节点上 */
pat->resolved_def_id = sym.node; 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, spl_diag_error(sema->diag, SPL_DIAG_KIND_DUPLICATE_SYMBOL, &n->dbg,
"duplicate symbol `%s`", name); "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; 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) { symbol.kind != SPL_SYMBOL_KIND_TYPE) {
spl_diag_error(sema->diag, SPL_DIAG_KIND_TYPE_MISMATCH, &n->dbg, "`%s` is not a type", spl_diag_error(sema->diag, SPL_DIAG_KIND_TYPE_MISMATCH, &n->dbg, "`%s` is not a type",
n->ident.name); 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}; 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); 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); 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, spl_diag_error(sema->diag, SPL_DIAG_KIND_DUPLICATE_SYMBOL, &pat->dbg,
"duplicate symbol `%s`", name); "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}; 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, spl_diag_error(sema->diag, SPL_DIAG_KIND_DUPLICATE_SYMBOL, &pat->dbg,
"type unpack: duplicate symbol `%s`", name); "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) { if (n->aggregate_literal.name) {
spl_symbol_t sym; 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, spl_diag_error(sema->diag, SPL_DIAG_KIND_UNDEFINED_SYMBOL, &n->dbg,
"unknown type `%s`", n->aggregate_literal.name); "unknown type `%s`", n->aggregate_literal.name);
return 0; 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) { static spl_lang_type_id_t sema_infer_ident(spl_sema_t *sema, spl_ast_node_t *n) {
const char *name = n->ident.name; const char *name = n->ident.name;
spl_symbol_t sym; 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, spl_diag_error(sema->diag, SPL_DIAG_KIND_UNDEFINED_SYMBOL, &n->dbg,
"undefined identifier `%s`", name); "undefined identifier `%s`", name);
return 0; 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; return inf;
} }
spl_symbol_t sym; 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`", spl_diag_error(sema->diag, SPL_DIAG_KIND_UNDEFINED_SYMBOL, &n->dbg, "unknown type `%s`",
type_name); type_name);
return 0; 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; return b;
} }
spl_symbol_t sym; 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", spl_diag_error(sema->diag, SPL_DIAG_KIND_UNDEFINED_SYMBOL, &n->dbg, "type `%s` not found",
name); name);
return 0; 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->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) { 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)); 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) { 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; pdef->state = p_tid ? SPL_DECL_RESOLVED : SPL_DECL_ERROR;
} }
pd->resolved_def_id = psym.node; 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, spl_diag_error(sema->diag, SPL_DIAG_KIND_DUPLICATE_SYMBOL, &pd->dbg,
"duplicate symbol `%s`", psym.name); "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) { switch (n->kind) {
case SPL_AST_MOD_DECL:
case SPL_AST_IMPORT_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; 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: case SPL_AST_EXPR_INT_LIT:
return sema_infer_int_lit(sema, n); return sema_infer_int_lit(sema, n);
case SPL_AST_EXPR_FLOAT_LIT: 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, 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_namespace_t *ns, spl_attr_t *attr) {
Assert(sema != NULL && ast != NULL && type != NULL && scope != NULL && attr != NULL); Assert(sema != NULL && ast != NULL && type != NULL && ns != NULL && attr != NULL);
sema->ast = ast; sema->ast = ast;
sema->type = type; sema->type = type;
sema->layout = layout; sema->layout = layout;
sema->scope = scope; sema->ns = ns;
sema->current_fn_ret_tid = 0; sema->current_fn_ret_tid = 0;
sema->current_aggregate_lang = 0; sema->current_aggregate_lang = 0;
sema->current_loop_break_tid = 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}; sema->root = (spl_symbol_t){0};
vec_init(sema->label_stack); vec_init(sema->label_stack);
sema->loader = NULL; sema->loader = NULL;
sema->modules = NULL;
sema->diag = 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) if (!mod || mod->kind != SPL_AST_CONTAINER_MEMBERS)
return; 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; spl_lang_type_id_t saved_agg = sema->current_aggregate_lang;
sema_collect_names(sema, module_ref); 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_infer(sema, module_ref);
sema->current_aggregate_lang = saved_agg; 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) { 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) { void spl_sema_dump(spl_sema_t *sema) {
printf("Scopes:\n"); printf("Scopes:\n");
for (usize i = 1; i < vec_size(sema->scope->scopes); ++i) { for (usize i = 1; i < vec_size(sema->ns->nodes); ++i) {
spl_scope_node_t *scope = &vec_at(sema->scope->scopes, i); spl_ns_node_t *scope = &vec_at(sema->ns->nodes, i);
Assert(scope != NULL); Assert(scope != NULL);
printf("scope[%zu] parent=%zu\n", i, scope->parent); printf("scope[%zu] parent=%zu\n", i, scope->parent);