refactor(ast): 将AST类型系统重构为规范类型系统
- 将scc_ast_type_t替换为scc_ast_qual_type_t,引入规范类型概念 - 添加scc_ast_canonical_type_t联合体用于表示规范类型 - 修改头文件结构,移除大量内联初始化函数,改为使用AST上下文分配器 - 添加SCC_AST_ALLOC宏用于统一节点分配管理 - 更新builtin类型枚举定义,添加类型计数常量 feat(ast): 引入AST上下文管理器 - 创建scc_ast_ctx_t结构体用于管理AST节点生命周期 - 实现类型池化机制,支持内置类型的统一管理 - 添加canonical类型获取和分配接口 refactor(abi): 适配新的AST类型系统 - 更新头文件包含,从<scc_ir.h>改为<scc_hir.h> - 适配函数参数类型,使用qual_type替代原始type - 使用scc_ast_canon_type()函数获取规范类型进行处理 Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -3,599 +3,46 @@
|
||||
|
||||
#include "scc_ast_def.h"
|
||||
|
||||
// decls can be nullptr but maybe warning
|
||||
typedef struct scc_ast_ctx {
|
||||
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);
|
||||
|
||||
scc_ast_canon_type_t *scc_ast_ctx_get_builtin_type(scc_ast_ctx_t *ctx,
|
||||
scc_ast_builtin_type_t kind);
|
||||
|
||||
scc_ast_canon_type_t *scc_ast_ctx_alloc_type(scc_ast_ctx_t *ctx);
|
||||
|
||||
static inline void *scc_ast_ctx_alloc_node(scc_ast_ctx_t *ctx, 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);
|
||||
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)
|
||||
|
||||
// have defined cannoical_type type
|
||||
static inline void
|
||||
scc_ast_translation_unit_init(scc_ast_translation_unit_t *translation_unit,
|
||||
scc_ast_decl_vec_t *decls, scc_pos_t loc) {
|
||||
Assert(translation_unit != nullptr);
|
||||
translation_unit->base.type = SCC_AST_TRANSLATION_UNIT;
|
||||
translation_unit->base.loc = loc;
|
||||
if (decls == nullptr) {
|
||||
scc_vec_init(translation_unit->declarations);
|
||||
} else {
|
||||
translation_unit->declarations = *decls;
|
||||
scc_vec_init(*decls);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void scc_ast_decl_list_init(scc_ast_decl_t *decl,
|
||||
scc_ast_decl_vec_t *list_move,
|
||||
scc_pos_t loc) {
|
||||
Assert(decl != nullptr && list_move != nullptr);
|
||||
decl->base.loc = loc;
|
||||
decl->base.type = SCC_AST_DECL_LIST;
|
||||
decl->name = nullptr;
|
||||
decl->list.vars = *list_move;
|
||||
scc_vec_init(*list_move);
|
||||
}
|
||||
|
||||
// name and var_init can be nullptr
|
||||
static inline void scc_ast_decl_unsafe_val_init(scc_ast_decl_t *decl,
|
||||
scc_ast_type_t *type,
|
||||
const char *name,
|
||||
scc_ast_expr_t *var_init,
|
||||
scc_pos_t loc) {
|
||||
Assert(decl != nullptr && type != nullptr);
|
||||
decl->base.loc = loc;
|
||||
decl->base.type = SCC_AST_DECL_VAR;
|
||||
decl->name = name;
|
||||
decl->var.type = type;
|
||||
decl->var.init = var_init;
|
||||
}
|
||||
|
||||
// var_init can be nullptr
|
||||
static inline void scc_ast_decl_val_init(scc_ast_decl_t *decl,
|
||||
scc_ast_type_t *type, const char *name,
|
||||
scc_ast_expr_t *var_init,
|
||||
scc_pos_t loc) {
|
||||
Assert(decl != nullptr && name != nullptr && type != nullptr);
|
||||
decl->base.loc = loc;
|
||||
decl->base.type = SCC_AST_DECL_VAR;
|
||||
decl->name = name;
|
||||
decl->var.type = type;
|
||||
decl->var.init = var_init;
|
||||
}
|
||||
|
||||
// body can be nullptr
|
||||
static inline void scc_ast_decl_func_init(scc_ast_decl_t *decl,
|
||||
scc_ast_type_t *type,
|
||||
const char *name,
|
||||
scc_ast_stmt_t *body, scc_pos_t loc) {
|
||||
Assert(decl != nullptr && name != nullptr && type != nullptr);
|
||||
decl->base.loc = loc;
|
||||
decl->base.type = SCC_AST_DECL_FUNC;
|
||||
decl->name = name;
|
||||
decl->func.type = type;
|
||||
Assert(type->base.type == SCC_AST_TYPE_FUNCTION);
|
||||
decl->func.body = body;
|
||||
}
|
||||
|
||||
// name can be nullptr
|
||||
static inline void scc_ast_decl_param_init(scc_ast_decl_t *decl,
|
||||
scc_ast_type_t *type,
|
||||
const char *name, int idx,
|
||||
scc_pos_t loc) {
|
||||
Assert(decl != nullptr && type != nullptr);
|
||||
decl->base.loc = loc;
|
||||
decl->base.type = SCC_AST_DECL_PARAM;
|
||||
decl->name = name;
|
||||
decl->param.type = type;
|
||||
decl->param.param_idx = idx;
|
||||
}
|
||||
|
||||
static inline void _scc_ast_decl_record_init(scc_ast_decl_t *decl,
|
||||
scc_ast_node_kind_t type,
|
||||
const char *name,
|
||||
scc_ast_decl_vec_t *fields_move,
|
||||
scc_pos_t loc) {
|
||||
Assert(decl != nullptr);
|
||||
decl->base.loc = loc;
|
||||
decl->base.type = type;
|
||||
decl->name = name;
|
||||
if (fields_move == nullptr) {
|
||||
scc_vec_init(decl->record.fields);
|
||||
} else {
|
||||
decl->record.fields = *fields_move;
|
||||
scc_vec_init(*fields_move);
|
||||
}
|
||||
}
|
||||
|
||||
// name and fields can be nullptr
|
||||
static inline void scc_ast_decl_struct_init(scc_ast_decl_t *decl,
|
||||
const char *name,
|
||||
scc_ast_decl_vec_t *fields_move,
|
||||
scc_pos_t loc) {
|
||||
_scc_ast_decl_record_init(decl, SCC_AST_DECL_STRUCT, name, fields_move,
|
||||
loc);
|
||||
}
|
||||
|
||||
// name and fields can be nullptr
|
||||
static inline void scc_ast_decl_union_init(scc_ast_decl_t *decl,
|
||||
const char *name,
|
||||
scc_ast_decl_vec_t *fields_move,
|
||||
scc_pos_t loc) {
|
||||
_scc_ast_decl_record_init(decl, SCC_AST_DECL_UNION, name, fields_move, loc);
|
||||
}
|
||||
|
||||
// name and fields can be nullptr
|
||||
static inline void scc_ast_decl_enum_init(scc_ast_decl_t *decl,
|
||||
const char *name,
|
||||
scc_ast_decl_vec_t *fields_move,
|
||||
scc_pos_t loc) {
|
||||
_scc_ast_decl_record_init(decl, SCC_AST_DECL_ENUM, name, fields_move, loc);
|
||||
}
|
||||
|
||||
static inline void scc_ast_decl_typedef_init(scc_ast_decl_t *decl,
|
||||
const char *name,
|
||||
scc_ast_type_t *type,
|
||||
scc_pos_t loc) {
|
||||
Assert(decl != nullptr && name != nullptr && type != nullptr);
|
||||
decl->base.loc = loc;
|
||||
decl->base.type = SCC_AST_DECL_TYPEDEF;
|
||||
decl->name = name;
|
||||
decl->typedef_decl.type = type;
|
||||
}
|
||||
|
||||
// items can be nullptr
|
||||
static inline void scc_ast_stmt_compound_init(
|
||||
scc_ast_stmt_t *stmt, scc_ast_block_item_vec_t *items_move, scc_pos_t loc) {
|
||||
Assert(stmt != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_COMPOUND;
|
||||
if (items_move == nullptr) {
|
||||
scc_vec_init(stmt->compound.block_items);
|
||||
} else {
|
||||
stmt->compound.block_items = *items_move;
|
||||
scc_vec_init(*items_move);
|
||||
}
|
||||
}
|
||||
|
||||
// expr can be nullptr
|
||||
static inline void scc_ast_stmt_expr_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *expr, scc_pos_t loc) {
|
||||
Assert(stmt != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_EXPR;
|
||||
stmt->expr.expr = expr;
|
||||
}
|
||||
|
||||
// opt_else can be nullptr
|
||||
static inline void scc_ast_stmt_if_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_stmt_t *then,
|
||||
scc_ast_stmt_t *opt_else,
|
||||
scc_pos_t loc) {
|
||||
Assert(stmt != nullptr && cond != nullptr && then != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_IF;
|
||||
stmt->if_stmt.cond = cond;
|
||||
stmt->if_stmt.then_stmt = then;
|
||||
stmt->if_stmt.opt_else_stmt = opt_else;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_while_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_stmt_t *body,
|
||||
scc_pos_t loc) {
|
||||
Assert(stmt != nullptr && cond != nullptr && body != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_WHILE;
|
||||
stmt->while_stmt.cond = cond;
|
||||
stmt->while_stmt.body = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_do_while_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_stmt_t *body,
|
||||
scc_pos_t loc) {
|
||||
Assert(stmt != nullptr && cond != nullptr && body != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_DO_WHILE;
|
||||
stmt->while_stmt.cond = cond;
|
||||
stmt->while_stmt.body = body;
|
||||
}
|
||||
|
||||
// FIXME
|
||||
static inline void scc_ast_stmt_for_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_node_t *init,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_expr_t *incr,
|
||||
scc_ast_stmt_t *body, scc_pos_t loc) {
|
||||
Assert(stmt != nullptr && body != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_FOR;
|
||||
stmt->for_stmt.init = init;
|
||||
stmt->for_stmt.cond = cond;
|
||||
stmt->for_stmt.incr = incr;
|
||||
stmt->for_stmt.body = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_switch_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_stmt_t *body,
|
||||
scc_pos_t loc) {
|
||||
Assert(stmt != nullptr && cond != nullptr && body != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_SWITCH;
|
||||
stmt->switch_stmt.cond = cond;
|
||||
stmt->switch_stmt.body = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_case_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_stmt_t *body, scc_pos_t loc) {
|
||||
Assert(stmt != nullptr && cond != nullptr && body != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_CASE;
|
||||
stmt->case_stmt.expr = cond;
|
||||
stmt->case_stmt.stmt = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_default_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_stmt_t *body,
|
||||
scc_pos_t loc) {
|
||||
Assert(stmt != nullptr && body != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_DEFAULT;
|
||||
stmt->default_stmt.stmt = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_break_init(scc_ast_stmt_t *stmt,
|
||||
scc_pos_t loc) {
|
||||
Assert(stmt != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_BREAK;
|
||||
stmt->jump._target = nullptr;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_continue_init(scc_ast_stmt_t *stmt,
|
||||
scc_pos_t loc) {
|
||||
Assert(stmt != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_CONTINUE;
|
||||
stmt->jump._target = nullptr;
|
||||
}
|
||||
|
||||
// expr can be nullptr
|
||||
static inline void scc_ast_stmt_return_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *expr,
|
||||
scc_pos_t loc) {
|
||||
Assert(stmt != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_RETURN;
|
||||
stmt->return_stmt.expr = expr;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_goto_init(scc_ast_stmt_t *stmt,
|
||||
const char *label, scc_pos_t loc) {
|
||||
Assert(stmt != nullptr && label != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_GOTO;
|
||||
stmt->goto_stmt.label = label;
|
||||
stmt->goto_stmt._target = nullptr;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_label_init(scc_ast_stmt_t *stmt,
|
||||
const char *label,
|
||||
scc_ast_stmt_t *body,
|
||||
scc_pos_t loc) {
|
||||
Assert(stmt != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_LABEL;
|
||||
stmt->label_stmt.label = label;
|
||||
stmt->label_stmt.stmt = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_binary_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_op_t op,
|
||||
scc_ast_expr_t *lhs,
|
||||
scc_ast_expr_t *rhs,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && lhs != nullptr && rhs != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_BINARY;
|
||||
expr->binary.op = op;
|
||||
expr->binary.lhs = lhs;
|
||||
expr->binary.rhs = rhs;
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_unary_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_op_t op,
|
||||
scc_ast_expr_t *operand,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && operand != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_UNARY;
|
||||
expr->unary.op = op;
|
||||
expr->unary.operand = operand;
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_cond_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_expr_t *then_expr,
|
||||
scc_ast_expr_t *else_expr,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && cond != nullptr && then_expr != nullptr &&
|
||||
else_expr != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_COND;
|
||||
expr->cond.cond = cond;
|
||||
expr->cond.then_expr = then_expr;
|
||||
expr->cond.else_expr = else_expr;
|
||||
}
|
||||
|
||||
// args can be nullptr
|
||||
static inline void scc_ast_expr_call_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_t *callee,
|
||||
scc_ast_expr_vec_t *args,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && callee != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_CALL;
|
||||
expr->call.callee = callee;
|
||||
if (args == nullptr) {
|
||||
scc_vec_init(expr->call.args);
|
||||
} else {
|
||||
expr->call.args = *args;
|
||||
}
|
||||
}
|
||||
|
||||
// SCC_AST_EXPR_ARRAY_SUBSCRIPT, // 数组下标
|
||||
static inline void scc_ast_expr_array_subscript_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_t *array,
|
||||
scc_ast_expr_t *index,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && array != nullptr && index != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_ARRAY_SUBSCRIPT;
|
||||
expr->subscript.array = array;
|
||||
expr->subscript.index = index;
|
||||
}
|
||||
|
||||
static inline void _scc_ast_expr_member_init(scc_ast_expr_t *expr,
|
||||
scc_ast_node_kind_t type,
|
||||
scc_ast_expr_t *object,
|
||||
const char *member,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && object != nullptr && member != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = type;
|
||||
expr->member.base = object;
|
||||
expr->member.name = member;
|
||||
expr->member._target_idx = 0;
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_member_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_t *object,
|
||||
const char *member, scc_pos_t loc) {
|
||||
_scc_ast_expr_member_init(expr, SCC_AST_EXPR_MEMBER, object, member, loc);
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_ptr_member_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_t *object,
|
||||
const char *member,
|
||||
scc_pos_t loc) {
|
||||
_scc_ast_expr_member_init(expr, SCC_AST_EXPR_PTR_MEMBER, object, member,
|
||||
loc);
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_cast_init(scc_ast_expr_t *expr,
|
||||
scc_ast_type_t *type,
|
||||
scc_ast_expr_t *operand,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && type != nullptr && operand != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_CAST;
|
||||
expr->cast.type = type;
|
||||
expr->cast.expr = operand;
|
||||
}
|
||||
|
||||
// type and target_expr can be nullptr but it only one of them can be nullptr
|
||||
static inline void scc_ast_expr_sizeof_init(scc_ast_expr_t *expr,
|
||||
scc_ast_type_t *type,
|
||||
scc_ast_expr_t *target_expr,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_SIZE_OF;
|
||||
expr->attr_of.type = type;
|
||||
expr->attr_of.expr = target_expr;
|
||||
}
|
||||
|
||||
// type and target_expr can be nullptr but it only one of them can be nullptr
|
||||
static inline void scc_ast_expr_alignof_init(scc_ast_expr_t *expr,
|
||||
scc_ast_type_t *type,
|
||||
scc_ast_expr_t *target_expr,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type =
|
||||
SCC_AST_EXPR_SIZE_OF; // 注意:这里使用了 SIZE_OF,可能需要改为 ALIGN_OF
|
||||
expr->attr_of.type = type;
|
||||
expr->attr_of.expr = target_expr;
|
||||
}
|
||||
|
||||
// lhs_exprs and rhs_exprs can be nullptr
|
||||
static inline void scc_ast_expr_compound_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_t *base,
|
||||
scc_ast_expr_vec_t *lhs_exprs,
|
||||
scc_ast_expr_vec_t *rhs_exprs,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && base != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_COMPOUND;
|
||||
expr->compound.base = base;
|
||||
|
||||
if (lhs_exprs == nullptr) {
|
||||
scc_vec_init(expr->compound.lhs_exprs);
|
||||
} else {
|
||||
expr->compound.lhs_exprs = *lhs_exprs;
|
||||
scc_vec_init(*lhs_exprs);
|
||||
}
|
||||
|
||||
if (rhs_exprs == nullptr) {
|
||||
scc_vec_init(expr->compound.rhs_exprs);
|
||||
} else {
|
||||
expr->compound.rhs_exprs = *rhs_exprs;
|
||||
scc_vec_init(*rhs_exprs);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_literal_init(scc_ast_expr_t *expr,
|
||||
scc_ast_node_kind_t type,
|
||||
const char *value, cbool owned,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && value != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = type;
|
||||
expr->literal.lexme = value;
|
||||
expr->literal.owned = owned;
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_literal_int_init(scc_ast_expr_t *expr,
|
||||
const char *value, cbool owned,
|
||||
scc_pos_t loc) {
|
||||
scc_ast_expr_literal_init(expr, SCC_AST_EXPR_INT_LITERAL, value, owned,
|
||||
loc);
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_literal_float_init(scc_ast_expr_t *expr,
|
||||
const char *value,
|
||||
cbool owned, scc_pos_t loc) {
|
||||
scc_ast_expr_literal_init(expr, SCC_AST_EXPR_FLOAT_LITERAL, value, owned,
|
||||
loc);
|
||||
}
|
||||
static inline void scc_ast_expr_literal_char_init(scc_ast_expr_t *expr,
|
||||
const char *value,
|
||||
cbool owned, scc_pos_t loc) {
|
||||
scc_ast_expr_literal_init(expr, SCC_AST_EXPR_CHAR_LITERAL, value, owned,
|
||||
loc);
|
||||
}
|
||||
static inline void scc_ast_expr_literal_string_init(scc_ast_expr_t *expr,
|
||||
const char *value,
|
||||
cbool owned,
|
||||
scc_pos_t loc) {
|
||||
scc_ast_expr_literal_init(expr, SCC_AST_EXPR_STRING_LITERAL, value, owned,
|
||||
loc);
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_identifier_init(scc_ast_expr_t *expr,
|
||||
const char *name,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && name != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_IDENTIFIER;
|
||||
expr->identifier.name = name;
|
||||
expr->identifier._target = nullptr;
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_lvalue_init(scc_ast_expr_t *expr,
|
||||
scc_ast_type_t *type,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && type != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_LVALUE;
|
||||
expr->lvalue.type = type;
|
||||
}
|
||||
|
||||
// have defined builtin type
|
||||
static inline void scc_ast_type_builtin_init(scc_ast_type_t *type,
|
||||
scc_ast_builtin_type_t builtin,
|
||||
scc_pos_t loc) {
|
||||
scc_ast_type_builtin_init(scc_ast_qual_type_t *type, scc_ast_ctx_t *ctx,
|
||||
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->builtin.type = builtin;
|
||||
type->type = scc_ast_ctx_get_builtin_type(ctx, builtin_type);
|
||||
type->quals = (scc_ast_decl_specifier_t){0}; // FIXME
|
||||
}
|
||||
|
||||
static inline void scc_ast_type_pointer_init(scc_ast_type_t *type,
|
||||
scc_ast_type_t *pointee,
|
||||
scc_pos_t loc) {
|
||||
Assert(type != nullptr && pointee != nullptr);
|
||||
type->base.loc = loc;
|
||||
type->base.type = SCC_AST_TYPE_POINTER;
|
||||
type->quals = (scc_ast_decl_specifier_t){0}; // FIXME
|
||||
type->pointer.pointee = pointee;
|
||||
}
|
||||
|
||||
// size can be nullptr
|
||||
static inline void scc_ast_type_array_init(scc_ast_type_t *type,
|
||||
scc_ast_type_t *element,
|
||||
scc_ast_expr_t *size,
|
||||
scc_pos_t loc) {
|
||||
Assert(type != nullptr && element != nullptr);
|
||||
type->base.loc = loc;
|
||||
type->base.type = SCC_AST_TYPE_ARRAY;
|
||||
type->quals = (scc_ast_decl_specifier_t){0}; // FIXME
|
||||
type->array.element = element;
|
||||
type->array.size = size;
|
||||
}
|
||||
|
||||
// return_type and params can be nullptr
|
||||
static inline void scc_ast_type_function_init(scc_ast_type_t *type,
|
||||
scc_ast_type_t *return_type,
|
||||
scc_ast_decl_vec_t *params,
|
||||
scc_pos_t loc) {
|
||||
Assert(type != nullptr);
|
||||
type->base.loc = loc;
|
||||
type->base.type = SCC_AST_TYPE_FUNCTION;
|
||||
type->function.return_type = return_type;
|
||||
type->quals = (scc_ast_decl_specifier_t){0}; // FIXME
|
||||
if (params == nullptr) {
|
||||
scc_vec_init(type->function.params);
|
||||
} else {
|
||||
type->function.params = *params;
|
||||
scc_vec_init(*params);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void _scc_ast_type_record_init(scc_ast_type_t *type,
|
||||
scc_ast_node_kind_t type_kind,
|
||||
const char *name,
|
||||
scc_ast_decl_t *decl,
|
||||
scc_pos_t loc) {
|
||||
Assert(type != nullptr);
|
||||
type->base.loc = loc;
|
||||
type->base.type = type_kind;
|
||||
type->quals = (scc_ast_decl_specifier_t){0}; // FIXME
|
||||
type->record.name = name;
|
||||
type->record.decl = decl;
|
||||
}
|
||||
|
||||
// name and decl can be nullptr
|
||||
static inline void scc_ast_type_struct_init(scc_ast_type_t *type,
|
||||
const char *name,
|
||||
scc_ast_decl_t *decl,
|
||||
scc_pos_t loc) {
|
||||
_scc_ast_type_record_init(type, SCC_AST_TYPE_STRUCT, name, decl, loc);
|
||||
}
|
||||
|
||||
// name and decl can be nullptr
|
||||
static inline void scc_ast_type_union_init(scc_ast_type_t *type,
|
||||
const char *name,
|
||||
scc_ast_decl_t *decl,
|
||||
scc_pos_t loc) {
|
||||
_scc_ast_type_record_init(type, SCC_AST_TYPE_UNION, name, decl, loc);
|
||||
}
|
||||
|
||||
// name and decl can be nullptr
|
||||
static inline void scc_ast_type_enum_init(scc_ast_type_t *type,
|
||||
const char *name,
|
||||
scc_ast_decl_t *decl, scc_pos_t loc) {
|
||||
_scc_ast_type_record_init(type, SCC_AST_TYPE_ENUM, name, decl, loc);
|
||||
}
|
||||
|
||||
static inline void scc_ast_type_typedef_init(scc_ast_type_t *type,
|
||||
const char *name,
|
||||
scc_ast_decl_t *target,
|
||||
scc_pos_t loc) {
|
||||
Assert(type != nullptr && target != nullptr);
|
||||
type->base.loc = loc;
|
||||
type->base.type = SCC_AST_TYPE_TYPEDEF;
|
||||
type->quals = (scc_ast_decl_specifier_t){0}; // FIXME
|
||||
type->typedef_type.name = name;
|
||||
type->typedef_type.decl = target;
|
||||
}
|
||||
|
||||
#endif /* __SCC_AST_H__ */
|
||||
#endif /* __SCC_AST_H__*/
|
||||
|
||||
@@ -64,16 +64,16 @@ typedef enum {
|
||||
scc_ast_expr_t_END, // 表达式结束
|
||||
|
||||
// 类型
|
||||
scc_ast_type_t_BEGIN, // 类型开始
|
||||
SCC_AST_TYPE_BUILTIN, // 内置类型
|
||||
SCC_AST_TYPE_POINTER, // 指针类型
|
||||
SCC_AST_TYPE_ARRAY, // 数组类型
|
||||
SCC_AST_TYPE_FUNCTION, // 函数类型
|
||||
SCC_AST_TYPE_STRUCT, // 结构体类型
|
||||
SCC_AST_TYPE_UNION, // 联合类型
|
||||
SCC_AST_TYPE_ENUM, // 枚举类型
|
||||
SCC_AST_TYPE_TYPEDEF, // typedef 类型
|
||||
scc_ast_type_t_END, // 类型结束
|
||||
scc_ast_qual_type_t_BEGIN, // 类型开始
|
||||
SCC_AST_TYPE_BUILTIN, // 内置类型
|
||||
SCC_AST_TYPE_POINTER, // 指针类型
|
||||
SCC_AST_TYPE_ARRAY, // 数组类型
|
||||
SCC_AST_TYPE_FUNCTION, // 函数类型
|
||||
SCC_AST_TYPE_STRUCT, // 结构体类型
|
||||
SCC_AST_TYPE_UNION, // 联合类型
|
||||
SCC_AST_TYPE_ENUM, // 枚举类型
|
||||
SCC_AST_TYPE_TYPEDEF, // typedef 类型
|
||||
scc_ast_qual_type_t_END, // 类型结束
|
||||
|
||||
// 其他
|
||||
scc_ast_translation_unit_t_BEGIN,
|
||||
@@ -85,6 +85,7 @@ typedef struct scc_ast_node {
|
||||
scc_ast_node_kind_t type;
|
||||
scc_pos_t loc;
|
||||
} scc_ast_node_t;
|
||||
typedef SCC_VEC(scc_ast_node_t *) scc_ast_node_vec_t;
|
||||
|
||||
#define SCC_AST_CAST_TO(kind, expr) \
|
||||
((kind *)(Assert(((scc_ast_node_t *)expr)->type > kind##_BEGIN && \
|
||||
@@ -126,6 +127,8 @@ typedef enum {
|
||||
SCC_AST_BUILTIN_TYPE_COMPLEX_FLOAT,
|
||||
SCC_AST_BUILTIN_TYPE_COMPLEX_DOUBLE,
|
||||
SCC_AST_BUILTIN_TYPE_COMPLEX_LONG_DOUBLE,
|
||||
|
||||
SCC_AST_BUILTIN_TYPE_COUNT,
|
||||
} scc_ast_builtin_type_t;
|
||||
|
||||
/**
|
||||
@@ -147,13 +150,14 @@ typedef struct {
|
||||
cbool is_inline;
|
||||
} scc_ast_decl_specifier_t;
|
||||
|
||||
// 前向声明
|
||||
typedef struct scc_ast_type scc_ast_type_t;
|
||||
typedef union scc_ast_canonical_type scc_ast_canon_type_t;
|
||||
typedef struct scc_ast_qual_type scc_ast_qual_type_t;
|
||||
typedef struct scc_ast_expr scc_ast_expr_t;
|
||||
typedef struct scc_ast_stmt scc_ast_stmt_t;
|
||||
typedef struct scc_ast_decl scc_ast_decl_t;
|
||||
|
||||
typedef SCC_VEC(scc_ast_type_t *) scc_ast_type_vec_t;
|
||||
typedef SCC_VEC(scc_ast_canon_type_t *) scc_ast_canon_type_vec_t;
|
||||
typedef SCC_VEC(scc_ast_qual_type_t *) scc_ast_qual_type_vec_t;
|
||||
typedef SCC_VEC(scc_ast_expr_t *) scc_ast_expr_vec_t;
|
||||
typedef SCC_VEC(scc_ast_stmt_t *) scc_ast_stmt_vec_t;
|
||||
typedef SCC_VEC(scc_ast_decl_t *) scc_ast_decl_vec_t;
|
||||
@@ -161,39 +165,52 @@ typedef SCC_VEC(scc_ast_decl_t *) scc_ast_decl_vec_t;
|
||||
// 通过指针实现泛型 only stmt or decl
|
||||
typedef SCC_VEC(scc_ast_node_t *) scc_ast_block_item_vec_t;
|
||||
|
||||
// 规范类型 Canonical Type
|
||||
union scc_ast_canonical_type {
|
||||
struct {
|
||||
scc_ast_builtin_type_t type;
|
||||
} builtin;
|
||||
struct {
|
||||
scc_ast_qual_type_t *pointee;
|
||||
} pointer;
|
||||
struct {
|
||||
scc_ast_qual_type_t *element;
|
||||
scc_ast_expr_t *size; // 可为 nullptr <=> 不定长数组
|
||||
} array;
|
||||
struct {
|
||||
scc_ast_qual_type_t *return_type;
|
||||
scc_ast_decl_vec_t params; // va_list <=> ...
|
||||
} function;
|
||||
struct {
|
||||
const char *name;
|
||||
scc_ast_decl_t *decl; // can be nullptr
|
||||
} record;
|
||||
struct {
|
||||
const char *name;
|
||||
/// @brief 指向typedef的声明(可以间接找到typedef的指向的类型)
|
||||
scc_ast_decl_t *decl;
|
||||
} typedef_type;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 类型表示
|
||||
*/
|
||||
struct scc_ast_type {
|
||||
struct scc_ast_qual_type {
|
||||
scc_ast_node_t base;
|
||||
scc_ast_decl_specifier_t quals;
|
||||
union {
|
||||
struct {
|
||||
scc_ast_builtin_type_t type;
|
||||
} builtin;
|
||||
struct {
|
||||
scc_ast_type_t *pointee;
|
||||
} pointer;
|
||||
struct {
|
||||
scc_ast_type_t *element;
|
||||
scc_ast_expr_t *size; // 可为 nullptr <=> 不定长数组
|
||||
} array;
|
||||
struct {
|
||||
scc_ast_type_t *return_type;
|
||||
scc_ast_decl_vec_t params; // va_list <=> ...
|
||||
} function;
|
||||
struct {
|
||||
const char *name;
|
||||
scc_ast_decl_t *decl; // can be nullptr
|
||||
} record;
|
||||
struct {
|
||||
const char *name;
|
||||
/// @brief 指向typedef的声明(可以间接找到typedef的指向的类型)
|
||||
scc_ast_decl_t *decl;
|
||||
} typedef_type;
|
||||
};
|
||||
scc_ast_canon_type_t *type;
|
||||
};
|
||||
|
||||
static inline const scc_ast_canon_type_t *
|
||||
scc_ast_canon_type(const scc_ast_qual_type_t *type) {
|
||||
return type->type;
|
||||
}
|
||||
|
||||
static inline scc_ast_canon_type_t *
|
||||
scc_ast_mut_canon_type(scc_ast_qual_type_t *type) {
|
||||
return type->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief AST 操作符枚举
|
||||
* 这个枚举定义了所有在AST中使用的操作符,与词法token分离
|
||||
@@ -302,12 +319,12 @@ struct scc_ast_expr {
|
||||
} member;
|
||||
// cast 类型转换
|
||||
struct {
|
||||
scc_ast_type_t *type;
|
||||
scc_ast_qual_type_t *type;
|
||||
scc_ast_expr_t *expr;
|
||||
} cast;
|
||||
// sizeof / _Alignof / ...
|
||||
union {
|
||||
scc_ast_type_t *type;
|
||||
scc_ast_qual_type_t *type;
|
||||
scc_ast_expr_t *expr;
|
||||
} attr_of;
|
||||
// 复合字面量
|
||||
@@ -329,7 +346,7 @@ struct scc_ast_expr {
|
||||
scc_ast_decl_t *_target; ///< fill by sema
|
||||
} identifier;
|
||||
struct {
|
||||
scc_ast_type_t *type;
|
||||
scc_ast_qual_type_t *type;
|
||||
} lvalue;
|
||||
// 内置表达式
|
||||
struct {
|
||||
@@ -418,17 +435,17 @@ struct scc_ast_decl {
|
||||
} list;
|
||||
// 变量声明
|
||||
struct {
|
||||
scc_ast_type_t *type;
|
||||
scc_ast_qual_type_t *type;
|
||||
scc_ast_expr_t *init; // 可为 nullptr
|
||||
} var;
|
||||
// 函数声明
|
||||
struct {
|
||||
scc_ast_type_t *type; // 函数类型
|
||||
scc_ast_stmt_t *body; // 可为 nullptr 表示只有声明
|
||||
scc_ast_qual_type_t *type; // 函数类型
|
||||
scc_ast_stmt_t *body; // 可为 nullptr 表示只有声明
|
||||
} func;
|
||||
// 参数声明
|
||||
struct {
|
||||
scc_ast_type_t *type;
|
||||
scc_ast_qual_type_t *type;
|
||||
int param_idx;
|
||||
} param;
|
||||
// 结构体/联合/枚举声明
|
||||
@@ -438,7 +455,7 @@ struct scc_ast_decl {
|
||||
} record;
|
||||
struct {
|
||||
/// @brief 被 typedef 的类型
|
||||
scc_ast_type_t *type;
|
||||
scc_ast_qual_type_t *type;
|
||||
} typedef_decl;
|
||||
};
|
||||
};
|
||||
|
||||
505
libs/ast/include/scc_ast_utils.h
Normal file
505
libs/ast/include/scc_ast_utils.h
Normal file
@@ -0,0 +1,505 @@
|
||||
#ifndef __SCC_AST_UTILS_H__
|
||||
#define __SCC_AST_UTILS_H__
|
||||
|
||||
#include "scc_ast_def.h"
|
||||
|
||||
// decls can be nullptr but maybe warning
|
||||
static inline void
|
||||
scc_ast_translation_unit_init(scc_ast_translation_unit_t *translation_unit,
|
||||
scc_ast_decl_vec_t *decls, scc_pos_t loc) {
|
||||
Assert(translation_unit != nullptr);
|
||||
translation_unit->base.type = SCC_AST_TRANSLATION_UNIT;
|
||||
translation_unit->base.loc = loc;
|
||||
if (decls == nullptr) {
|
||||
scc_vec_init(translation_unit->declarations);
|
||||
} else {
|
||||
translation_unit->declarations = *decls;
|
||||
scc_vec_init(*decls);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void scc_ast_decl_list_init(scc_ast_decl_t *decl,
|
||||
scc_ast_decl_vec_t *list_move,
|
||||
scc_pos_t loc) {
|
||||
Assert(decl != nullptr && list_move != nullptr);
|
||||
decl->base.loc = loc;
|
||||
decl->base.type = SCC_AST_DECL_LIST;
|
||||
decl->name = nullptr;
|
||||
decl->list.vars = *list_move;
|
||||
scc_vec_init(*list_move);
|
||||
}
|
||||
|
||||
// name and var_init can be nullptr
|
||||
static inline void scc_ast_decl_unsafe_val_init(scc_ast_decl_t *decl,
|
||||
scc_ast_qual_type_t *type,
|
||||
const char *name,
|
||||
scc_ast_expr_t *var_init,
|
||||
scc_pos_t loc) {
|
||||
Assert(decl != nullptr && type != nullptr);
|
||||
decl->base.loc = loc;
|
||||
decl->base.type = SCC_AST_DECL_VAR;
|
||||
decl->name = name;
|
||||
decl->var.type = type;
|
||||
decl->var.init = var_init;
|
||||
}
|
||||
|
||||
// var_init can be nullptr
|
||||
static inline void scc_ast_decl_val_init(scc_ast_decl_t *decl,
|
||||
scc_ast_qual_type_t *type,
|
||||
const char *name,
|
||||
scc_ast_expr_t *var_init,
|
||||
scc_pos_t loc) {
|
||||
Assert(decl != nullptr && name != nullptr && type != nullptr);
|
||||
decl->base.loc = loc;
|
||||
decl->base.type = SCC_AST_DECL_VAR;
|
||||
decl->name = name;
|
||||
decl->var.type = type;
|
||||
decl->var.init = var_init;
|
||||
}
|
||||
|
||||
// body can be nullptr
|
||||
static inline void scc_ast_decl_func_init(scc_ast_decl_t *decl,
|
||||
scc_ast_qual_type_t *type,
|
||||
const char *name,
|
||||
scc_ast_stmt_t *body, scc_pos_t loc) {
|
||||
Assert(decl != nullptr && name != nullptr && type != nullptr);
|
||||
decl->base.loc = loc;
|
||||
decl->base.type = SCC_AST_DECL_FUNC;
|
||||
decl->name = name;
|
||||
decl->func.type = type;
|
||||
Assert(type->base.type == SCC_AST_TYPE_FUNCTION);
|
||||
decl->func.body = body;
|
||||
}
|
||||
|
||||
// name can be nullptr
|
||||
static inline void scc_ast_decl_param_init(scc_ast_decl_t *decl,
|
||||
scc_ast_qual_type_t *type,
|
||||
const char *name, int idx,
|
||||
scc_pos_t loc) {
|
||||
Assert(decl != nullptr && type != nullptr);
|
||||
decl->base.loc = loc;
|
||||
decl->base.type = SCC_AST_DECL_PARAM;
|
||||
decl->name = name;
|
||||
decl->param.type = type;
|
||||
decl->param.param_idx = idx;
|
||||
}
|
||||
|
||||
static inline void _scc_ast_decl_record_init(scc_ast_decl_t *decl,
|
||||
scc_ast_node_kind_t type,
|
||||
const char *name,
|
||||
scc_ast_decl_vec_t *fields_move,
|
||||
scc_pos_t loc) {
|
||||
Assert(decl != nullptr);
|
||||
if (name == nullptr) {
|
||||
name = "<anonymous>";
|
||||
}
|
||||
decl->base.loc = loc;
|
||||
decl->base.type = type;
|
||||
decl->name = name;
|
||||
if (fields_move == nullptr) {
|
||||
scc_vec_init(decl->record.fields);
|
||||
} else {
|
||||
decl->record.fields = *fields_move;
|
||||
scc_vec_init(*fields_move);
|
||||
}
|
||||
}
|
||||
|
||||
// name and fields can be nullptr
|
||||
static inline void scc_ast_decl_struct_init(scc_ast_decl_t *decl,
|
||||
const char *name,
|
||||
scc_ast_decl_vec_t *fields_move,
|
||||
scc_pos_t loc) {
|
||||
_scc_ast_decl_record_init(decl, SCC_AST_DECL_STRUCT, name, fields_move,
|
||||
loc);
|
||||
}
|
||||
|
||||
// name and fields can be nullptr
|
||||
static inline void scc_ast_decl_union_init(scc_ast_decl_t *decl,
|
||||
const char *name,
|
||||
scc_ast_decl_vec_t *fields_move,
|
||||
scc_pos_t loc) {
|
||||
_scc_ast_decl_record_init(decl, SCC_AST_DECL_UNION, name, fields_move, loc);
|
||||
}
|
||||
|
||||
// name and fields can be nullptr
|
||||
static inline void scc_ast_decl_enum_init(scc_ast_decl_t *decl,
|
||||
const char *name,
|
||||
scc_ast_decl_vec_t *fields_move,
|
||||
scc_pos_t loc) {
|
||||
_scc_ast_decl_record_init(decl, SCC_AST_DECL_ENUM, name, fields_move, loc);
|
||||
}
|
||||
|
||||
static inline void scc_ast_decl_typedef_init(scc_ast_decl_t *decl,
|
||||
const char *name,
|
||||
scc_ast_qual_type_t *type,
|
||||
scc_pos_t loc) {
|
||||
Assert(decl != nullptr && name != nullptr && type != nullptr);
|
||||
decl->base.loc = loc;
|
||||
decl->base.type = SCC_AST_DECL_TYPEDEF;
|
||||
decl->name = name;
|
||||
decl->typedef_decl.type = type;
|
||||
}
|
||||
|
||||
// items can be nullptr
|
||||
static inline void scc_ast_stmt_compound_init(
|
||||
scc_ast_stmt_t *stmt, scc_ast_block_item_vec_t *items_move, scc_pos_t loc) {
|
||||
Assert(stmt != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_COMPOUND;
|
||||
if (items_move == nullptr) {
|
||||
scc_vec_init(stmt->compound.block_items);
|
||||
} else {
|
||||
stmt->compound.block_items = *items_move;
|
||||
scc_vec_init(*items_move);
|
||||
}
|
||||
}
|
||||
|
||||
// expr can be nullptr
|
||||
static inline void scc_ast_stmt_expr_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *expr, scc_pos_t loc) {
|
||||
Assert(stmt != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_EXPR;
|
||||
stmt->expr.expr = expr;
|
||||
}
|
||||
|
||||
// opt_else can be nullptr
|
||||
static inline void scc_ast_stmt_if_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_stmt_t *then,
|
||||
scc_ast_stmt_t *opt_else,
|
||||
scc_pos_t loc) {
|
||||
Assert(stmt != nullptr && cond != nullptr && then != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_IF;
|
||||
stmt->if_stmt.cond = cond;
|
||||
stmt->if_stmt.then_stmt = then;
|
||||
stmt->if_stmt.opt_else_stmt = opt_else;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_while_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_stmt_t *body,
|
||||
scc_pos_t loc) {
|
||||
Assert(stmt != nullptr && cond != nullptr && body != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_WHILE;
|
||||
stmt->while_stmt.cond = cond;
|
||||
stmt->while_stmt.body = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_do_while_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_stmt_t *body,
|
||||
scc_pos_t loc) {
|
||||
Assert(stmt != nullptr && cond != nullptr && body != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_DO_WHILE;
|
||||
stmt->while_stmt.cond = cond;
|
||||
stmt->while_stmt.body = body;
|
||||
}
|
||||
|
||||
// FIXME
|
||||
static inline void scc_ast_stmt_for_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_node_t *init,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_expr_t *incr,
|
||||
scc_ast_stmt_t *body, scc_pos_t loc) {
|
||||
Assert(stmt != nullptr && body != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_FOR;
|
||||
stmt->for_stmt.init = init;
|
||||
stmt->for_stmt.cond = cond;
|
||||
stmt->for_stmt.incr = incr;
|
||||
stmt->for_stmt.body = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_switch_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_stmt_t *body,
|
||||
scc_pos_t loc) {
|
||||
Assert(stmt != nullptr && cond != nullptr && body != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_SWITCH;
|
||||
stmt->switch_stmt.cond = cond;
|
||||
stmt->switch_stmt.body = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_case_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_stmt_t *body, scc_pos_t loc) {
|
||||
Assert(stmt != nullptr && cond != nullptr && body != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_CASE;
|
||||
stmt->case_stmt.expr = cond;
|
||||
stmt->case_stmt.stmt = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_default_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_stmt_t *body,
|
||||
scc_pos_t loc) {
|
||||
Assert(stmt != nullptr && body != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_DEFAULT;
|
||||
stmt->default_stmt.stmt = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_break_init(scc_ast_stmt_t *stmt,
|
||||
scc_pos_t loc) {
|
||||
Assert(stmt != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_BREAK;
|
||||
stmt->jump._target = nullptr;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_continue_init(scc_ast_stmt_t *stmt,
|
||||
scc_pos_t loc) {
|
||||
Assert(stmt != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_CONTINUE;
|
||||
stmt->jump._target = nullptr;
|
||||
}
|
||||
|
||||
// expr can be nullptr
|
||||
static inline void scc_ast_stmt_return_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *expr,
|
||||
scc_pos_t loc) {
|
||||
Assert(stmt != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_RETURN;
|
||||
stmt->return_stmt.expr = expr;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_goto_init(scc_ast_stmt_t *stmt,
|
||||
const char *label, scc_pos_t loc) {
|
||||
Assert(stmt != nullptr && label != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_GOTO;
|
||||
stmt->goto_stmt.label = label;
|
||||
stmt->goto_stmt._target = nullptr;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_label_init(scc_ast_stmt_t *stmt,
|
||||
const char *label,
|
||||
scc_ast_stmt_t *body,
|
||||
scc_pos_t loc) {
|
||||
Assert(stmt != nullptr);
|
||||
stmt->base.loc = loc;
|
||||
stmt->base.type = SCC_AST_STMT_LABEL;
|
||||
stmt->label_stmt.label = label;
|
||||
stmt->label_stmt.stmt = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_binary_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_op_t op,
|
||||
scc_ast_expr_t *lhs,
|
||||
scc_ast_expr_t *rhs,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && lhs != nullptr && rhs != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_BINARY;
|
||||
expr->binary.op = op;
|
||||
expr->binary.lhs = lhs;
|
||||
expr->binary.rhs = rhs;
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_unary_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_op_t op,
|
||||
scc_ast_expr_t *operand,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && operand != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_UNARY;
|
||||
expr->unary.op = op;
|
||||
expr->unary.operand = operand;
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_cond_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_expr_t *then_expr,
|
||||
scc_ast_expr_t *else_expr,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && cond != nullptr && then_expr != nullptr &&
|
||||
else_expr != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_COND;
|
||||
expr->cond.cond = cond;
|
||||
expr->cond.then_expr = then_expr;
|
||||
expr->cond.else_expr = else_expr;
|
||||
}
|
||||
|
||||
// args can be nullptr
|
||||
static inline void scc_ast_expr_call_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_t *callee,
|
||||
scc_ast_expr_vec_t *args,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && callee != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_CALL;
|
||||
expr->call.callee = callee;
|
||||
if (args == nullptr) {
|
||||
scc_vec_init(expr->call.args);
|
||||
} else {
|
||||
expr->call.args = *args;
|
||||
}
|
||||
}
|
||||
|
||||
// SCC_AST_EXPR_ARRAY_SUBSCRIPT, // 数组下标
|
||||
static inline void scc_ast_expr_array_subscript_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_t *array,
|
||||
scc_ast_expr_t *index,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && array != nullptr && index != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_ARRAY_SUBSCRIPT;
|
||||
expr->subscript.array = array;
|
||||
expr->subscript.index = index;
|
||||
}
|
||||
|
||||
static inline void _scc_ast_expr_member_init(scc_ast_expr_t *expr,
|
||||
scc_ast_node_kind_t type,
|
||||
scc_ast_expr_t *object,
|
||||
const char *member,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && object != nullptr && member != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = type;
|
||||
expr->member.base = object;
|
||||
expr->member.name = member;
|
||||
expr->member._target_idx = 0;
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_member_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_t *object,
|
||||
const char *member, scc_pos_t loc) {
|
||||
_scc_ast_expr_member_init(expr, SCC_AST_EXPR_MEMBER, object, member, loc);
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_ptr_member_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_t *object,
|
||||
const char *member,
|
||||
scc_pos_t loc) {
|
||||
_scc_ast_expr_member_init(expr, SCC_AST_EXPR_PTR_MEMBER, object, member,
|
||||
loc);
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_cast_init(scc_ast_expr_t *expr,
|
||||
scc_ast_qual_type_t *type,
|
||||
scc_ast_expr_t *operand,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && type != nullptr && operand != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_CAST;
|
||||
expr->cast.type = type;
|
||||
expr->cast.expr = operand;
|
||||
}
|
||||
|
||||
// type and target_expr can be nullptr but it only one of them can be nullptr
|
||||
static inline void scc_ast_expr_sizeof_init(scc_ast_expr_t *expr,
|
||||
scc_ast_qual_type_t *type,
|
||||
scc_ast_expr_t *target_expr,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_SIZE_OF;
|
||||
expr->attr_of.type = type;
|
||||
expr->attr_of.expr = target_expr;
|
||||
}
|
||||
|
||||
// type and target_expr can be nullptr but it only one of them can be nullptr
|
||||
static inline void scc_ast_expr_alignof_init(scc_ast_expr_t *expr,
|
||||
scc_ast_qual_type_t *type,
|
||||
scc_ast_expr_t *target_expr,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type =
|
||||
SCC_AST_EXPR_SIZE_OF; // 注意:这里使用了 SIZE_OF,可能需要改为 ALIGN_OF
|
||||
expr->attr_of.type = type;
|
||||
expr->attr_of.expr = target_expr;
|
||||
}
|
||||
|
||||
// lhs_exprs and rhs_exprs can be nullptr
|
||||
static inline void scc_ast_expr_compound_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_t *base,
|
||||
scc_ast_expr_vec_t *lhs_exprs,
|
||||
scc_ast_expr_vec_t *rhs_exprs,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && base != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_COMPOUND;
|
||||
expr->compound.base = base;
|
||||
|
||||
if (lhs_exprs == nullptr) {
|
||||
scc_vec_init(expr->compound.lhs_exprs);
|
||||
} else {
|
||||
expr->compound.lhs_exprs = *lhs_exprs;
|
||||
scc_vec_init(*lhs_exprs);
|
||||
}
|
||||
|
||||
if (rhs_exprs == nullptr) {
|
||||
scc_vec_init(expr->compound.rhs_exprs);
|
||||
} else {
|
||||
expr->compound.rhs_exprs = *rhs_exprs;
|
||||
scc_vec_init(*rhs_exprs);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_literal_init(scc_ast_expr_t *expr,
|
||||
scc_ast_node_kind_t type,
|
||||
const char *value, cbool owned,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && value != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = type;
|
||||
expr->literal.lexme = value;
|
||||
expr->literal.owned = owned;
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_literal_int_init(scc_ast_expr_t *expr,
|
||||
const char *value, cbool owned,
|
||||
scc_pos_t loc) {
|
||||
scc_ast_expr_literal_init(expr, SCC_AST_EXPR_INT_LITERAL, value, owned,
|
||||
loc);
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_literal_float_init(scc_ast_expr_t *expr,
|
||||
const char *value,
|
||||
cbool owned, scc_pos_t loc) {
|
||||
scc_ast_expr_literal_init(expr, SCC_AST_EXPR_FLOAT_LITERAL, value, owned,
|
||||
loc);
|
||||
}
|
||||
static inline void scc_ast_expr_literal_char_init(scc_ast_expr_t *expr,
|
||||
const char *value,
|
||||
cbool owned, scc_pos_t loc) {
|
||||
scc_ast_expr_literal_init(expr, SCC_AST_EXPR_CHAR_LITERAL, value, owned,
|
||||
loc);
|
||||
}
|
||||
static inline void scc_ast_expr_literal_string_init(scc_ast_expr_t *expr,
|
||||
const char *value,
|
||||
cbool owned,
|
||||
scc_pos_t loc) {
|
||||
scc_ast_expr_literal_init(expr, SCC_AST_EXPR_STRING_LITERAL, value, owned,
|
||||
loc);
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_identifier_init(scc_ast_expr_t *expr,
|
||||
const char *name,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && name != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_IDENTIFIER;
|
||||
expr->identifier.name = name;
|
||||
expr->identifier._target = nullptr;
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_lvalue_init(scc_ast_expr_t *expr,
|
||||
scc_ast_qual_type_t *type,
|
||||
scc_pos_t loc) {
|
||||
Assert(expr != nullptr && type != nullptr);
|
||||
expr->base.loc = loc;
|
||||
expr->base.type = SCC_AST_EXPR_LVALUE;
|
||||
expr->lvalue.type = type;
|
||||
}
|
||||
|
||||
#endif /* __SCC_AST_UTILS_H__ */
|
||||
@@ -53,7 +53,7 @@ static const char *node_type_names[] = {
|
||||
[SCC_AST_EXPR_STRING_LITERAL] = "StringLiteralExpr",
|
||||
[SCC_AST_EXPR_IDENTIFIER] = "IdentifierExpr",
|
||||
[scc_ast_expr_t_END] = "ERROR",
|
||||
[scc_ast_type_t_BEGIN] = "ERROR",
|
||||
[scc_ast_qual_type_t_BEGIN] = "ERROR",
|
||||
[SCC_AST_TYPE_BUILTIN] = "BuiltinType",
|
||||
[SCC_AST_TYPE_POINTER] = "PointerType",
|
||||
[SCC_AST_TYPE_ARRAY] = "ArrayType",
|
||||
@@ -62,7 +62,7 @@ static const char *node_type_names[] = {
|
||||
[SCC_AST_TYPE_UNION] = "UnionType",
|
||||
[SCC_AST_TYPE_ENUM] = "EnumType",
|
||||
[SCC_AST_TYPE_TYPEDEF] = "TypedefType",
|
||||
[scc_ast_type_t_END] = "ERROR",
|
||||
[scc_ast_qual_type_t_END] = "ERROR",
|
||||
[scc_ast_translation_unit_t_BEGIN] = "ERROR",
|
||||
[SCC_AST_TRANSLATION_UNIT] = "TranslationUnit",
|
||||
[scc_ast_translation_unit_t_END] = "ERROR",
|
||||
@@ -200,8 +200,8 @@ static inline void dump_child_node(scc_tree_dump_t *td, scc_ast_node_t *child,
|
||||
scc_tree_dump_pop(td);
|
||||
}
|
||||
|
||||
static inline void dump_quals(scc_ast_decl_specifier_t quals,
|
||||
scc_tree_dump_t *td) {
|
||||
static inline void dump_quals(scc_tree_dump_t *td,
|
||||
scc_ast_decl_specifier_t quals) {
|
||||
if (quals.is_atomic)
|
||||
scc_tree_dump_value(td, " atomic");
|
||||
if (quals.is_restrict)
|
||||
@@ -216,18 +216,20 @@ static inline void dump_quals(scc_ast_decl_specifier_t quals,
|
||||
scc_tree_dump_value(td, " extern");
|
||||
}
|
||||
|
||||
static void dump_type_impl(scc_ast_type_t *type, scc_tree_dump_t *td) {
|
||||
static void dump_type_impl(scc_tree_dump_t *td,
|
||||
const scc_ast_qual_type_t *type) {
|
||||
if (!type)
|
||||
return;
|
||||
|
||||
scc_tree_dump_begin_line(td);
|
||||
scc_tree_dump_node(td, "%s", get_node_type_str(type->base.type));
|
||||
dump_quals(type->quals, td);
|
||||
dump_quals(td, type->quals);
|
||||
|
||||
switch (type->base.type) {
|
||||
case SCC_AST_TYPE_BUILTIN:
|
||||
scc_tree_dump_value(td, " '%s'",
|
||||
get_builtin_type_str(type->builtin.type));
|
||||
scc_tree_dump_value(
|
||||
td, " '%s'",
|
||||
get_builtin_type_str(scc_ast_canon_type(type)->builtin.type));
|
||||
break;
|
||||
case SCC_AST_TYPE_POINTER:
|
||||
scc_tree_dump_value(td, " pointer");
|
||||
@@ -239,25 +241,29 @@ static void dump_type_impl(scc_ast_type_t *type, scc_tree_dump_t *td) {
|
||||
scc_tree_dump_value(td, " function");
|
||||
break;
|
||||
case SCC_AST_TYPE_STRUCT:
|
||||
if (type->record.name)
|
||||
scc_tree_dump_value(td, " 'struct %s'", type->record.name);
|
||||
if (scc_ast_canon_type(type)->record.name)
|
||||
scc_tree_dump_value(td, " 'struct %s'",
|
||||
scc_ast_canon_type(type)->record.name);
|
||||
else
|
||||
scc_tree_dump_value(td, " <anonymous struct>");
|
||||
break;
|
||||
case SCC_AST_TYPE_UNION:
|
||||
if (type->record.name)
|
||||
scc_tree_dump_value(td, " 'union %s'", type->record.name);
|
||||
if (scc_ast_canon_type(type)->record.name)
|
||||
scc_tree_dump_value(td, " 'union %s'",
|
||||
scc_ast_canon_type(type)->record.name);
|
||||
else
|
||||
scc_tree_dump_value(td, " <anonymous union>");
|
||||
break;
|
||||
case SCC_AST_TYPE_ENUM:
|
||||
if (type->record.name)
|
||||
scc_tree_dump_value(td, " 'enum %s'", type->record.name);
|
||||
if (scc_ast_canon_type(type)->record.name)
|
||||
scc_tree_dump_value(td, " 'enum %s'",
|
||||
scc_ast_canon_type(type)->record.name);
|
||||
else
|
||||
scc_tree_dump_value(td, " anonymous enum");
|
||||
break;
|
||||
case SCC_AST_TYPE_TYPEDEF:
|
||||
scc_tree_dump_value(td, " '%s'", type->typedef_type.name);
|
||||
scc_tree_dump_value(td, " '%s'",
|
||||
scc_ast_canon_type(type)->typedef_type.name);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -265,26 +271,35 @@ static void dump_type_impl(scc_ast_type_t *type, scc_tree_dump_t *td) {
|
||||
|
||||
switch (type->base.type) {
|
||||
case SCC_AST_TYPE_POINTER:
|
||||
dump_child_node(td, (scc_ast_node_t *)type->pointer.pointee, true);
|
||||
dump_child_node(
|
||||
td, (scc_ast_node_t *)scc_ast_canon_type(type)->pointer.pointee,
|
||||
true);
|
||||
break;
|
||||
case SCC_AST_TYPE_ARRAY:
|
||||
dump_child_node(td, (scc_ast_node_t *)type->array.element,
|
||||
type->array.size == nullptr);
|
||||
if (type->array.size)
|
||||
dump_child_node(td, (scc_ast_node_t *)type->array.size, true);
|
||||
dump_child_node(
|
||||
td, (scc_ast_node_t *)scc_ast_canon_type(type)->array.element,
|
||||
scc_ast_canon_type(type)->array.size == nullptr);
|
||||
if (scc_ast_canon_type(type)->array.size)
|
||||
dump_child_node(
|
||||
td, (scc_ast_node_t *)scc_ast_canon_type(type)->array.size,
|
||||
true);
|
||||
break;
|
||||
case SCC_AST_TYPE_FUNCTION:
|
||||
scc_tree_dump_push(td, false);
|
||||
scc_tree_dump_begin_line(td);
|
||||
scc_tree_dump_node(td, "%s", "ReturnType");
|
||||
dump_child_node(td, (scc_ast_node_t *)type->function.return_type, true);
|
||||
dump_child_node(
|
||||
td,
|
||||
(scc_ast_node_t *)scc_ast_canon_type(type)->function.return_type,
|
||||
true);
|
||||
scc_tree_dump_pop(td);
|
||||
|
||||
scc_tree_dump_push(td, true);
|
||||
if (scc_vec_size(type->function.params) != 0) {
|
||||
scc_vec_foreach(type->function.params, i) {
|
||||
scc_ast_decl_t *param = scc_vec_at(type->function.params, i);
|
||||
dump_type_impl(param->param.type, td);
|
||||
if (scc_vec_size(scc_ast_canon_type(type)->function.params) != 0) {
|
||||
scc_vec_foreach(scc_ast_canon_type(type)->function.params, i) {
|
||||
scc_ast_decl_t *param =
|
||||
scc_vec_at(scc_ast_canon_type(type)->function.params, i);
|
||||
dump_type_impl(td, param->param.type);
|
||||
}
|
||||
} else {
|
||||
scc_tree_dump_begin_line(td);
|
||||
@@ -297,7 +312,7 @@ static void dump_type_impl(scc_ast_type_t *type, scc_tree_dump_t *td) {
|
||||
}
|
||||
}
|
||||
|
||||
static void dump_expr_impl(scc_ast_expr_t *expr, scc_tree_dump_t *td) {
|
||||
static void dump_expr_impl(scc_tree_dump_t *td, const scc_ast_expr_t *expr) {
|
||||
if (!expr)
|
||||
return;
|
||||
|
||||
@@ -401,7 +416,7 @@ static void dump_expr_impl(scc_ast_expr_t *expr, scc_tree_dump_t *td) {
|
||||
}
|
||||
}
|
||||
|
||||
static void dump_stmt_impl(scc_ast_stmt_t *stmt, scc_tree_dump_t *td) {
|
||||
static void dump_stmt_impl(scc_tree_dump_t *td, const scc_ast_stmt_t *stmt) {
|
||||
if (!stmt)
|
||||
return;
|
||||
|
||||
@@ -479,7 +494,7 @@ static void dump_stmt_impl(scc_ast_stmt_t *stmt, scc_tree_dump_t *td) {
|
||||
}
|
||||
}
|
||||
|
||||
static void dump_decl_impl(scc_ast_decl_t *decl, scc_tree_dump_t *td) {
|
||||
static void dump_decl_impl(scc_tree_dump_t *td, const scc_ast_decl_t *decl) {
|
||||
if (!decl)
|
||||
return;
|
||||
|
||||
@@ -535,8 +550,8 @@ static void dump_decl_impl(scc_ast_decl_t *decl, scc_tree_dump_t *td) {
|
||||
}
|
||||
}
|
||||
|
||||
static void dump_unit_impl(scc_ast_translation_unit_t *unit,
|
||||
scc_tree_dump_t *td) {
|
||||
static void dump_unit_impl(scc_tree_dump_t *td,
|
||||
const scc_ast_translation_unit_t *unit) {
|
||||
if (!unit)
|
||||
return;
|
||||
|
||||
@@ -552,13 +567,13 @@ void scc_ast_dump_node(scc_tree_dump_t *td, const scc_ast_node_t *node) {
|
||||
if (!node)
|
||||
return;
|
||||
if (SCC_AST_IS_A(scc_ast_expr_t, node))
|
||||
dump_expr_impl(SCC_AST_CAST_TO(scc_ast_expr_t, node), td);
|
||||
dump_expr_impl(td, SCC_AST_CAST_TO(scc_ast_expr_t, node));
|
||||
else if (SCC_AST_IS_A(scc_ast_stmt_t, node))
|
||||
dump_stmt_impl(SCC_AST_CAST_TO(scc_ast_stmt_t, node), td);
|
||||
dump_stmt_impl(td, SCC_AST_CAST_TO(scc_ast_stmt_t, node));
|
||||
else if (SCC_AST_IS_A(scc_ast_decl_t, node))
|
||||
dump_decl_impl(SCC_AST_CAST_TO(scc_ast_decl_t, node), td);
|
||||
else if (SCC_AST_IS_A(scc_ast_type_t, node))
|
||||
dump_type_impl(SCC_AST_CAST_TO(scc_ast_type_t, node), td);
|
||||
dump_decl_impl(td, SCC_AST_CAST_TO(scc_ast_decl_t, node));
|
||||
else if (SCC_AST_IS_A(scc_ast_qual_type_t, node))
|
||||
dump_type_impl(td, SCC_AST_CAST_TO(scc_ast_qual_type_t, node));
|
||||
else if (SCC_AST_IS_A(scc_ast_translation_unit_t, node))
|
||||
dump_unit_impl(SCC_AST_CAST_TO(scc_ast_translation_unit_t, node), td);
|
||||
dump_unit_impl(td, SCC_AST_CAST_TO(scc_ast_translation_unit_t, node));
|
||||
}
|
||||
|
||||
42
libs/ast/src/scc_ast.c
Normal file
42
libs/ast/src/scc_ast.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <scc_ast.h>
|
||||
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user