feat(ast): 将AST上下文重构为模块并添加字符串池支持

- 将scc_ast_ctx重命名为scc_ast_module以更好地反映其功能
- 添加scc_strpool_t用于统一管理AST节点名称字符串的生命周期
- 实现scc_ast_module_intern函数用于字符串驻留
- 更新所有相关的初始化、销毁和访问函数命名
- 修改内存分配宏以使用新的模块结构

refactor(parser): 更新解析器以使用AST模块和字符串池

- 将解析器中的ast_ctx字段替换为ast_module
- 在创建AST节点时使用新的ast_module参数
- 使用scc_ast_module_intern函数处理标识符和字符串字面量
- 确保所有字符串都被正确驻留到模块的字符串池中

refactor(sema): 更新语义分析器使用AST模块

- 将sema_ctx中的ast_ctx字段替换为ast_module
- 更新语义分析器初始化函数参数

refactor(ast2ir): 更新AST到IR转换器使用AST模块

- 将ast_ctx字段替换为ast_module
- 更新上下文初始化函数参数和实现

fix(cfg): 修复CFG模块中的符号查找错误

- 修正scc_cfg_module_unsafe_get_symbol函数中的边界检查条件

perf(ir): 完善各IR层模块的内存清理机制

- 为HIR模块添加函数和基本块元数据的释放逻辑
- 为MIR和LIR模块完善完整的资源清理和内存释放
- 确保所有分配的元数据结构都能被正确释放

chore(deps): 添加scc_utils依赖到AST库

- 在libs/ast/cbuild.toml中添加对scc_utils的依赖
This commit is contained in:
zzy
2026-05-31 19:56:19 +08:00
parent d2eafa9dc6
commit 8b817da3b6
31 changed files with 409 additions and 179 deletions

View File

@@ -2,47 +2,59 @@
#define __SCC_AST_H__
#include "scc_ast_def.h"
#include <scc_strpool.h>
typedef struct scc_ast_ctx {
typedef struct scc_ast_module {
scc_ast_canon_type_vec_t canonical_type_pool;
scc_ast_canon_type_t *builtin_types[SCC_AST_BUILTIN_TYPE_COUNT];
scc_ast_node_vec_t all_nodes;
} scc_ast_ctx_t;
void scc_ast_ctx_init(scc_ast_ctx_t *ctx);
void scc_ast_ctx_drop(scc_ast_ctx_t *ctx);
/** 所有 AST 节点的 name 字符串由 name_pool 统一管理生命周期 */
scc_strpool_t name_pool;
} scc_ast_module_t;
scc_ast_canon_type_t *scc_ast_ctx_get_builtin_type(scc_ast_ctx_t *ctx,
scc_ast_builtin_type_t kind);
void scc_ast_module_init(scc_ast_module_t *module);
void scc_ast_module_drop(scc_ast_module_t *module);
scc_ast_canon_type_t *scc_ast_ctx_alloc_type(scc_ast_ctx_t *ctx);
scc_ast_canon_type_t *
scc_ast_module_get_builtin_type(scc_ast_module_t *module,
scc_ast_builtin_type_t kind);
static inline void *scc_ast_ctx_alloc_node(scc_ast_ctx_t *ctx, usize size) {
scc_ast_canon_type_t *scc_ast_module_alloc_type(scc_ast_module_t *module);
/**
* @brief 将字符串 intern 到 module 的 name_pool 中
* @return 指向 module 所拥有的持久化副本的指针
* (在 module_drop 前始终有效)
*/
const char *scc_ast_module_intern(scc_ast_module_t *module, const char *str);
static inline void *scc_ast_module_alloc_node(scc_ast_module_t *module,
usize size) {
void *ptr = scc_malloc(size);
if (ptr == nullptr) {
Panic("Out of memory");
}
scc_memset(ptr, 0, size);
scc_vec_push(ctx->all_nodes, (scc_ast_node_t *)ptr);
scc_vec_push(module->all_nodes, (scc_ast_node_t *)ptr);
return ptr;
}
#define SCC_AST_ALLOC(ctx, type) \
((type *)scc_ast_ctx_alloc_node(ctx, sizeof(type)))
#define SCC_AST_ALLOC_QUAL_TYPE(ctx) SCC_AST_ALLOC(ctx, scc_ast_qual_type_t)
#define SCC_AST_ALLOC_DECL(ctx) SCC_AST_ALLOC(ctx, scc_ast_decl_t)
#define SCC_AST_ALLOC_EXPR(ctx) SCC_AST_ALLOC(ctx, scc_ast_expr_t)
#define SCC_AST_ALLOC_STMT(ctx) SCC_AST_ALLOC(ctx, scc_ast_stmt_t)
#define SCC_AST_ALLOC(module, type) \
((type *)scc_ast_module_alloc_node(module, sizeof(type)))
#define SCC_AST_ALLOC_QUAL_TYPE(module) SCC_AST_ALLOC(module, scc_ast_qual_type_t)
#define SCC_AST_ALLOC_DECL(module) SCC_AST_ALLOC(module, scc_ast_decl_t)
#define SCC_AST_ALLOC_EXPR(module) SCC_AST_ALLOC(module, scc_ast_expr_t)
#define SCC_AST_ALLOC_STMT(module) SCC_AST_ALLOC(module, scc_ast_stmt_t)
// have defined cannoical_type type
static inline void
scc_ast_type_builtin_init(scc_ast_qual_type_t *type, scc_ast_ctx_t *ctx,
scc_ast_type_builtin_init(scc_ast_qual_type_t *type, scc_ast_module_t *module,
scc_ast_builtin_type_t builtin_type, scc_pos_t loc) {
Assert(type != nullptr);
type->base.loc = loc;
type->base.type = SCC_AST_TYPE_BUILTIN;
type->type = scc_ast_ctx_get_builtin_type(ctx, builtin_type);
type->quals = (scc_ast_decl_specifier_t){0}; // FIXME
type->type = scc_ast_module_get_builtin_type(module, builtin_type);
type->quals = (scc_ast_decl_specifier_t){0};
}
#endif /* __SCC_AST_H__*/
#endif /* __SCC_AST_H__ */