#ifndef __SCC_AST_H__ #define __SCC_AST_H__ #include "scc_ast_def.h" #include 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; /** 所有 AST 节点的 name 字符串由 name_pool 统一管理生命周期 */ scc_strpool_t name_pool; } scc_ast_module_t; 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_module_get_builtin_type(scc_ast_module_t *module, scc_ast_builtin_type_t kind); 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(module->all_nodes, (scc_ast_node_t *)ptr); return ptr; } #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) static inline void 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_module_get_builtin_type(module, builtin_type); type->quals = (scc_ast_decl_specifier_t){0}; } #endif /* __SCC_AST_H__ */