refactor(ast): 调整AST结构体和枚举类型的声明表示方式

将结构体、联合和枚举类型的字段表示从向量改为声明指针,
允许name和decl字段为null,更新相关初始化函数的断言检查,
使结构更加灵活并支持不完整类型定义。

BREAKING CHANGE: 修改了scc_ast_type结构体中record和enumeration
子类型的字段表示方法,从fields向量改为decl指针。
This commit is contained in:
zzy
2026-03-11 21:53:19 +08:00
parent ce5414f2eb
commit e6511c508c
12 changed files with 338 additions and 116 deletions

View File

@@ -181,11 +181,11 @@ struct scc_ast_type {
} function;
struct {
const char *name;
scc_ast_decl_vec_t fields; // 结构体/联合字段
scc_ast_decl_t *decl; // can be null
} record;
struct {
const char *name;
scc_ast_expr_vec_t enumerators; // 枚举项
scc_ast_decl_t *decl; // can be null
} enumeration;
struct {
const char *name;

View File

@@ -57,11 +57,11 @@ static inline void scc_ast_decl_param_init(scc_ast_decl_t *decl,
decl->param.type = type;
}
// fields can be null
// name and fields can be null
static inline void scc_ast_decl_struct_init(scc_ast_decl_t *decl,
const char *name,
scc_ast_decl_vec_t *fields_move) {
Assert(decl != null && name != null);
Assert(decl != null);
decl->base.loc = scc_pos_create();
decl->base.type = SCC_AST_DECL_STRUCT;
decl->name = name;
@@ -73,11 +73,11 @@ static inline void scc_ast_decl_struct_init(scc_ast_decl_t *decl,
}
}
// fields can be null
// name and fields can be null
static inline void scc_ast_decl_union_init(scc_ast_decl_t *decl,
const char *name,
scc_ast_decl_vec_t *fields_move) {
Assert(decl != null && name != null);
Assert(decl != null);
decl->base.loc = scc_pos_create();
decl->base.type = SCC_AST_DECL_UNION;
decl->name = name;
@@ -89,11 +89,11 @@ static inline void scc_ast_decl_union_init(scc_ast_decl_t *decl,
}
}
// fields can be null
// name and fields can be null
static inline void scc_ast_decl_enum_init(scc_ast_decl_t *decl,
const char *name,
scc_ast_expr_vec_t *fields_move) {
Assert(decl != null && name != null);
Assert(decl != null);
decl->base.loc = scc_pos_create();
decl->base.type = SCC_AST_DECL_ENUM;
decl->name = name;
@@ -454,48 +454,39 @@ static inline void scc_ast_type_function_init(scc_ast_type_t *type,
static inline void _scc_ast_type_record_init(scc_ast_type_t *type,
scc_ast_node_type_t type_kind,
const char *name,
scc_ast_decl_vec_t *members) {
Assert(type != null && name != null);
scc_ast_decl_t *decl) {
Assert(type != null);
type->base.loc = scc_pos_create();
type->base.type = type_kind;
type->quals = (scc_ast_decl_specifier_t){0}; // FIXME
if (members == null) {
scc_vec_init(type->record.fields);
} else {
type->record.fields = *members;
scc_vec_init(*members);
}
type->record.name = name;
type->record.decl = decl;
}
// name and members can be null
// name and decl can be null
static inline void scc_ast_type_struct_init(scc_ast_type_t *type,
const char *name,
scc_ast_decl_vec_t *members) {
_scc_ast_type_record_init(type, SCC_AST_TYPE_STRUCT, name, members);
scc_ast_decl_t *decl) {
_scc_ast_type_record_init(type, SCC_AST_TYPE_STRUCT, name, decl);
}
// name and members can be null
// name and decl can be null
static inline void scc_ast_type_union_init(scc_ast_type_t *type,
const char *name,
scc_ast_decl_vec_t *members) {
_scc_ast_type_record_init(type, SCC_AST_TYPE_UNION, name, members);
scc_ast_decl_t *decl) {
_scc_ast_type_record_init(type, SCC_AST_TYPE_UNION, name, decl);
}
// name and members can be null
// name and decl can be null
static inline void scc_ast_type_enum_init(scc_ast_type_t *type,
const char *name,
scc_ast_expr_vec_t *members) {
scc_ast_decl_t *decl) {
Assert(type != null);
type->base.loc = scc_pos_create();
type->base.type = SCC_AST_TYPE_ENUM;
type->quals = (scc_ast_decl_specifier_t){0}; // FIXME
type->enumeration.name = name;
if (members == null) {
scc_vec_init(type->enumeration.enumerators);
} else {
type->enumeration.enumerators = *members;
scc_vec_init(*members);
}
type->enumeration.decl = null;
}
static inline void scc_ast_type_typedef_init(scc_ast_type_t *type,

View File

@@ -282,33 +282,19 @@ static void dump_type_impl(scc_ast_type_t *type, scc_tree_dump_ctx_t *ctx) {
break;
case SCC_AST_TYPE_FUNCTION:
PRINT_QUOTED_VALUE(ctx, "function");
scc_tree_dump_printf(ctx, "\n");
if (type->function.return_type) {
dump_type_impl(type->function.return_type, ctx);
}
if (scc_vec_size(type->function.param_types) != 0) {
scc_vec_foreach(type->function.param_types, i) {
scc_ast_decl_t *param =
scc_vec_at(type->function.param_types, i);
if (param->name) {
// FIXME param name
}
dump_type_impl(param->param.type, ctx);
}
}
break;
case SCC_AST_TYPE_STRUCT:
if (type->record.name) {
BUILD_TYPE_NAME(ctx, "struct ", type->record.name);
} else {
PRINT_QUOTED_VALUE(ctx, "anonymous struct");
PRINT_QUOTED_VALUE(ctx, "<anonymous struct>");
}
break;
case SCC_AST_TYPE_UNION:
if (type->record.name) {
BUILD_TYPE_NAME(ctx, "union ", type->record.name);
} else {
PRINT_QUOTED_VALUE(ctx, "anonymous union");
PRINT_QUOTED_VALUE(ctx, "<anonymous union>");
}
break;
case SCC_AST_TYPE_ENUM:
@@ -339,6 +325,40 @@ static void dump_type_impl(scc_ast_type_t *type, scc_tree_dump_ctx_t *ctx) {
dump_child_node((scc_ast_node_t *)type->array.size, ctx, true);
}
break;
case SCC_AST_TYPE_STRUCT:
if (type->enumeration.decl) {
dump_child_node((scc_ast_node_t *)type->enumeration.decl, ctx,
true);
}
break;
case SCC_AST_TYPE_ENUM:
if (type->record.decl) {
dump_child_node((scc_ast_node_t *)type->record.decl, ctx, true);
}
break;
case SCC_AST_TYPE_FUNCTION:
scc_vec_push(ctx->stack, false);
scc_tree_print_indent(ctx);
SCC_TREE_DUMP_PRINT_PURE(ctx, ctx->node_color, "%s\n", "ReturnType");
dump_child_node((scc_ast_node_t *)type->function.return_type, ctx,
true);
scc_vec_pop(ctx->stack);
scc_vec_push(ctx->stack, true);
if (scc_vec_size(type->function.param_types) != 0) {
scc_vec_foreach(type->function.param_types, i) {
scc_ast_decl_t *param =
scc_vec_at(type->function.param_types, i);
if (param->name) {
// FIXME param name
}
dump_type_impl(param->param.type, ctx);
}
} else {
PRINT_QUOTED_VALUE(ctx, "()");
}
scc_vec_pop(ctx->stack);
break;
default:
break;
}
@@ -416,10 +436,13 @@ static void dump_expr_impl(scc_ast_expr_t *expr, scc_tree_dump_ctx_t *ctx) {
case SCC_AST_EXPR_MEMBER:
case SCC_AST_EXPR_PTR_MEMBER:
dump_child_node((scc_ast_node_t *)expr->member.base, ctx, false);
// 打印成员访问信息
scc_vec_push(ctx->stack, true);
scc_tree_print_indent(ctx);
PRINT_NODE(ctx, "Member [\"%s\"]", expr->member.name);
scc_tree_dump_printf(ctx, "\n");
SCC_TREE_DUMP_PRINT_PURE(
ctx, ctx->node_color, "%s %s\n",
expr->base.type == SCC_AST_EXPR_MEMBER ? "Member" : "PtrMember",
expr->member.name);
scc_vec_pop(ctx->stack);
break;
case SCC_AST_EXPR_CAST: