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

@@ -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,