feat(ast): 添加完整的内置类型支持并重构类型定义
- 添加 va_list、bool、signed/unsigned 各种整数类型等内置类型 - 重新排列内置类型枚举顺序,增加 UNKNOWN 类型 - 为复合字面量、指针类型、数组类型添加初始化函数 - 添加结构体、联合体、枚举、typedef 类型的初始化函数 - 更新类型转储功能以支持新的内置类型显示 refactor(parser): 优化类型解析和声明处理逻辑 - 修改类型解析函数返回类型为通用 AST 节点 - 重构声明解析逻辑,支持变量和函数声明的不同处理路径 - 更新语法分析规则中的空格标记处理 - 简化表达式解析中的错误处理流程 fix(ast): 修复参数声明和类型转储相关问题 - 修正参数声明初始化函数中对 name 参数可为空的处理 - 修复类型转储中修饰符显示和指针类型显示问题 - 更新 AST 转储中空值检查使用正确的 null 比较
This commit is contained in:
@@ -3,28 +3,29 @@
|
||||
|
||||
#include "ast_def.h"
|
||||
|
||||
extern scc_ast_type_t scc_ast_builtin_type_va_list;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_void;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_bool;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_char;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_short;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_long;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_long_long;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_int;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_float;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_double;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_char;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_void;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_long;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_long_long;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_short;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_bool;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_long_double;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_complex_float;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_complex_double;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_complex_long_double;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_unsigned_char;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_unsigned_short;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_unsigned_int;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_unsigned_long;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_unsigned_long_long;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_unsigned_short;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_unsigned_char;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_signed_char;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_signed_short;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_signed_int;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_signed_long;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_signed_long_long;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_signed_short;
|
||||
extern scc_ast_type_t scc_ast_builtin_type_signed_char;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -95,20 +95,34 @@ typedef struct {
|
||||
* @brief 内置类型枚举
|
||||
*/
|
||||
typedef enum {
|
||||
SCC_AST_BUILTIN_TYPE_UNKNOWN,
|
||||
SCC_AST_BUILTIN_TYPE_VA_LIST,
|
||||
SCC_AST_BUILTIN_TYPE_VOID,
|
||||
SCC_AST_BUILTIN_TYPE_BOOL,
|
||||
SCC_AST_BUILTIN_TYPE_CHAR,
|
||||
SCC_AST_BUILTIN_TYPE_SHORT,
|
||||
SCC_AST_BUILTIN_TYPE_INT,
|
||||
SCC_AST_BUILTIN_TYPE_LONG,
|
||||
SCC_AST_BUILTIN_TYPE_LONG_LONG,
|
||||
|
||||
SCC_AST_BUILTIN_TYPE_UNSIGNED_CHAR,
|
||||
SCC_AST_BUILTIN_TYPE_UNSIGNED_SHORT,
|
||||
SCC_AST_BUILTIN_TYPE_UNSIGNED_INT,
|
||||
SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG,
|
||||
SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG_LONG,
|
||||
|
||||
SCC_AST_BUILTIN_TYPE_SIGNED_CHAR,
|
||||
SCC_AST_BUILTIN_TYPE_SIGNED_SHORT,
|
||||
SCC_AST_BUILTIN_TYPE_SIGNED_INT,
|
||||
SCC_AST_BUILTIN_TYPE_SIGNED_LONG,
|
||||
SCC_AST_BUILTIN_TYPE_SIGNED_LONG_LONG,
|
||||
|
||||
SCC_AST_BUILTIN_TYPE_FLOAT,
|
||||
SCC_AST_BUILTIN_TYPE_DOUBLE,
|
||||
SCC_AST_BUILTIN_TYPE_LONG_DOUBLE,
|
||||
SCC_AST_BUILTIN_TYPE_BOOL,
|
||||
SCC_AST_BUILTIN_TYPE_COMPLEX_FLOAT,
|
||||
SCC_AST_BUILTIN_TYPE_COMPLEX_DOUBLE,
|
||||
SCC_AST_BUILTIN_TYPE_COMPLEX_LONG_DOUBLE,
|
||||
SCC_AST_BUILTIN_TYPE_VA_LIST,
|
||||
} scc_ast_builtin_type_t;
|
||||
|
||||
/**
|
||||
@@ -284,7 +298,7 @@ struct scc_ast_expr {
|
||||
const char *name;
|
||||
usize _target_idx;
|
||||
} member;
|
||||
// 类型转换
|
||||
// cast 类型转换
|
||||
struct {
|
||||
scc_ast_type_t *type;
|
||||
scc_ast_expr_t *expr;
|
||||
|
||||
@@ -46,12 +46,11 @@ static inline void scc_ast_decl_func_init(scc_ast_decl_t *decl,
|
||||
decl->func.body = body;
|
||||
}
|
||||
|
||||
// body can be null
|
||||
// name can be null
|
||||
static inline void scc_ast_decl_param_init(scc_ast_decl_t *decl,
|
||||
scc_ast_type_t *type,
|
||||
const char *name,
|
||||
scc_ast_stmt_t *body) {
|
||||
Assert(decl != null && name != null && type != null);
|
||||
const char *name) {
|
||||
Assert(decl != null && type != null);
|
||||
decl->base.loc = scc_pos_create();
|
||||
decl->base.type = SCC_AST_DECL_PARAM;
|
||||
decl->name = name;
|
||||
@@ -342,10 +341,26 @@ static inline void scc_ast_expr_ptr_member_init(scc_ast_expr_t *expr,
|
||||
_scc_ast_expr_member_init(expr, SCC_AST_EXPR_PTR_MEMBER, object, member);
|
||||
}
|
||||
|
||||
// TODO
|
||||
// SCC_AST_EXPR_CAST, // 类型转换
|
||||
// SCC_AST_EXPR_SIZE_OF, // sizeof
|
||||
// SCC_AST_EXPR_ALIGN_OF, // _Alignof
|
||||
// SCC_AST_EXPR_COMPOUND_LITERAL, // 复合字面量
|
||||
|
||||
// init can be null
|
||||
static inline void
|
||||
scc_ast_expr_compound_literal_init(scc_ast_expr_t *expr, scc_ast_type_t *type,
|
||||
scc_ast_expr_vec_t *init) {
|
||||
Assert(expr != null && type != null);
|
||||
expr->base.loc = scc_pos_create();
|
||||
expr->base.type = SCC_AST_EXPR_COMPOUND_LITERAL;
|
||||
expr->compound_literal.type = type;
|
||||
if (init == null) {
|
||||
scc_vec_init(expr->compound_literal.init_list);
|
||||
} else {
|
||||
expr->compound_literal.init_list = *init;
|
||||
scc_vec_init(*init);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_literal_init(scc_ast_expr_t *expr,
|
||||
scc_ast_node_type_t type,
|
||||
@@ -388,9 +403,36 @@ static inline void scc_ast_expr_identifier_init(scc_ast_expr_t *expr,
|
||||
expr->identifier._target = null;
|
||||
}
|
||||
|
||||
// SCC_AST_TYPE_BUILTIN, // 内置类型
|
||||
// SCC_AST_TYPE_POINTER, // 指针类型
|
||||
// SCC_AST_TYPE_ARRAY, // 数组类型
|
||||
// have defined builtin type
|
||||
static inline void _scc_ast_type_builtin_init(scc_ast_type_t *type,
|
||||
scc_ast_builtin_type_t builtin) {
|
||||
Assert(type != null);
|
||||
type->base.loc = scc_pos_create();
|
||||
type->base.type = SCC_AST_TYPE_BUILTIN;
|
||||
type->builtin.type = builtin;
|
||||
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) {
|
||||
Assert(type != null && pointee != null);
|
||||
type->base.loc = scc_pos_create();
|
||||
type->base.type = SCC_AST_TYPE_POINTER;
|
||||
type->quals = (scc_ast_decl_specifier_t){0}; // FIXME
|
||||
type->pointer.pointee = pointee;
|
||||
}
|
||||
|
||||
// size can be null
|
||||
static inline void scc_ast_type_array_init(scc_ast_type_t *type,
|
||||
scc_ast_type_t *element,
|
||||
scc_ast_expr_t *size) {
|
||||
Assert(type != null && element != null);
|
||||
type->base.loc = scc_pos_create();
|
||||
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 null
|
||||
static inline void scc_ast_type_function_init(scc_ast_type_t *type,
|
||||
@@ -400,6 +442,7 @@ static inline void scc_ast_type_function_init(scc_ast_type_t *type,
|
||||
type->base.loc = scc_pos_create();
|
||||
type->base.type = SCC_AST_TYPE_FUNCTION;
|
||||
type->function.return_type = return_type;
|
||||
type->quals = (scc_ast_decl_specifier_t){0}; // FIXME
|
||||
if (params == null) {
|
||||
scc_vec_init(type->function.param_types);
|
||||
} else {
|
||||
@@ -408,9 +451,62 @@ static inline void scc_ast_type_function_init(scc_ast_type_t *type,
|
||||
}
|
||||
}
|
||||
|
||||
// SCC_AST_TYPE_STRUCT, // 结构体类型
|
||||
// SCC_AST_TYPE_UNION, // 联合类型
|
||||
// SCC_AST_TYPE_ENUM, // 枚举类型
|
||||
// SCC_AST_TYPE_TYPEDEF, // typedef 类型
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// name and members 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);
|
||||
}
|
||||
|
||||
// name and members 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);
|
||||
}
|
||||
|
||||
// name and members 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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void scc_ast_type_typedef_init(scc_ast_type_t *type,
|
||||
const char *name,
|
||||
scc_ast_type_t *target) {
|
||||
Assert(type != null && target != null);
|
||||
type->base.loc = scc_pos_create();
|
||||
type->base.type = SCC_AST_TYPE_TYPEDEF;
|
||||
type->quals = (scc_ast_decl_specifier_t){0}; // FIXME
|
||||
type->typedef_type.name = name;
|
||||
type->typedef_type.underlying = target;
|
||||
}
|
||||
|
||||
#endif /* __SCC_AST_H__ */
|
||||
|
||||
Reference in New Issue
Block a user