#include static scc_ast_canon_type_t *alloc_canonical_type(scc_ast_ctx_t *ctx) { scc_ast_canon_type_t *type = scc_malloc(sizeof(scc_ast_canon_type_t)); if (type == nullptr) { Panic("alloc_canonical_type: malloc failed"); } scc_vec_push(ctx->canonical_type_pool, type); return type; } void scc_ast_ctx_init(scc_ast_ctx_t *ctx) { scc_vec_init(ctx->canonical_type_pool); scc_vec_init(ctx->all_nodes); // 创建全部内置类型 for (int i = 0; i < SCC_AST_BUILTIN_TYPE_COUNT; i += 1) { scc_ast_canon_type_t *t = alloc_canonical_type(ctx); t->builtin.type = (scc_ast_builtin_type_t)i; // 直接按顺序对应 ctx->builtin_types[i] = t; } } void scc_ast_ctx_drop(scc_ast_ctx_t *ctx) { // 释放所有规范类型 scc_vec_foreach(ctx->canonical_type_pool, i) { scc_free(ctx->canonical_type_pool.data[i]); } scc_vec_foreach(ctx->all_nodes, i) { scc_free(ctx->all_nodes.data[i]); } scc_vec_free(ctx->canonical_type_pool); } scc_ast_canon_type_t * scc_ast_ctx_get_builtin_type(scc_ast_ctx_t *ctx, scc_ast_builtin_type_t kind) { assert(kind < SCC_AST_BUILTIN_TYPE_COUNT && kind > SCC_AST_BUILTIN_TYPE_UNKNOWN); return ctx->builtin_types[kind]; } scc_ast_canon_type_t *scc_ast_ctx_alloc_type(scc_ast_ctx_t *ctx) { return alloc_canonical_type(ctx); }