feat(ast): 添加内置类型定义和AST节点初始化函数
添加了完整的内置类型支持,包括整数、浮点数、字符、布尔等基本类型, 以及它们的有符号/无符号变体。同时添加了大量的AST节点初始化函数, 简化了AST节点的创建过程。 BREAKING CHANGE: 重构了AST表达式和声明结构,移除了冗余字段, 统一了命名规范,并修改了函数调用和成员访问的表示方式。
This commit is contained in:
@@ -108,6 +108,7 @@ 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_VA_LIST,
|
||||
} scc_ast_builtin_type_t;
|
||||
|
||||
/**
|
||||
@@ -148,14 +149,13 @@ typedef SCC_VEC(scc_ast_node_t *) scc_ast_block_item_vec_t;
|
||||
*/
|
||||
struct scc_ast_type {
|
||||
scc_ast_node_t base;
|
||||
scc_ast_decl_specifier_t quals;
|
||||
union {
|
||||
struct {
|
||||
scc_ast_builtin_type_t type;
|
||||
scc_ast_decl_specifier_t quals;
|
||||
} builtin;
|
||||
struct {
|
||||
scc_ast_type_t *pointee;
|
||||
scc_ast_decl_specifier_t quals;
|
||||
} pointer;
|
||||
struct {
|
||||
scc_ast_type_t *element;
|
||||
@@ -163,8 +163,7 @@ struct scc_ast_type {
|
||||
} array;
|
||||
struct {
|
||||
scc_ast_type_t *return_type;
|
||||
scc_ast_type_vec_t param_types;
|
||||
cbool is_variadic; // va_arg <=> ...
|
||||
scc_ast_decl_vec_t param_types; // va_list <=> ...
|
||||
} function;
|
||||
struct {
|
||||
const char *name;
|
||||
@@ -269,7 +268,8 @@ struct scc_ast_expr {
|
||||
} cond;
|
||||
// 函数调用
|
||||
struct {
|
||||
scc_ast_expr_t *callee;
|
||||
const char *name;
|
||||
scc_ast_expr_t *_target;
|
||||
scc_ast_expr_vec_t args;
|
||||
} call;
|
||||
// 数组下标
|
||||
@@ -277,16 +277,12 @@ struct scc_ast_expr {
|
||||
scc_ast_expr_t *array;
|
||||
scc_ast_expr_t *index;
|
||||
} subscript;
|
||||
// 成员访问
|
||||
// 成员访问 指针成员访问
|
||||
struct {
|
||||
scc_ast_expr_t *base;
|
||||
const char *member_name;
|
||||
const char *name;
|
||||
usize _target_idx;
|
||||
} member;
|
||||
// 指针成员访问
|
||||
struct {
|
||||
scc_ast_expr_t *base;
|
||||
const char *member_name;
|
||||
} ptr_member;
|
||||
// 类型转换
|
||||
struct {
|
||||
scc_ast_type_t *type;
|
||||
@@ -310,6 +306,7 @@ struct scc_ast_expr {
|
||||
// 标识符
|
||||
struct {
|
||||
const char *name;
|
||||
scc_ast_decl_t *_target;
|
||||
} identifier;
|
||||
};
|
||||
};
|
||||
@@ -334,21 +331,16 @@ struct scc_ast_stmt {
|
||||
scc_ast_stmt_t *then_stmt;
|
||||
scc_ast_stmt_t *opt_else_stmt; // stmt or null
|
||||
} if_stmt;
|
||||
// while 语句
|
||||
// while do-while 语句
|
||||
struct {
|
||||
scc_ast_expr_t *cond;
|
||||
scc_ast_stmt_t *body;
|
||||
} while_stmt;
|
||||
// do-while 语句
|
||||
struct {
|
||||
scc_ast_stmt_t *body;
|
||||
scc_ast_expr_t *cond;
|
||||
} do_while_stmt;
|
||||
// for 语句
|
||||
struct {
|
||||
scc_ast_type_t *init; // expr or decl or null
|
||||
scc_ast_expr_t *cond; // 可为 null
|
||||
scc_ast_expr_t *iter; // 可为 null
|
||||
scc_ast_expr_t *incr; // 可为 null
|
||||
scc_ast_stmt_t *body;
|
||||
} for_stmt;
|
||||
// switch 语句
|
||||
@@ -367,15 +359,15 @@ struct scc_ast_stmt {
|
||||
} default_stmt;
|
||||
// break/continue
|
||||
struct {
|
||||
// 无额外字段
|
||||
} jump;
|
||||
// return 语句
|
||||
struct {
|
||||
scc_ast_expr_t *expr; // 可为 NULL
|
||||
scc_ast_expr_t *expr; // 可为 null
|
||||
} return_stmt;
|
||||
// goto 语句
|
||||
struct {
|
||||
const char *label;
|
||||
scc_ast_stmt_t *_target; // fill by sema
|
||||
} goto_stmt;
|
||||
// 标签语句
|
||||
struct {
|
||||
@@ -390,37 +382,32 @@ struct scc_ast_stmt {
|
||||
*/
|
||||
struct scc_ast_decl {
|
||||
scc_ast_node_t base;
|
||||
const char *name;
|
||||
union {
|
||||
// 变量声明
|
||||
struct {
|
||||
const char *name;
|
||||
scc_ast_type_t *type;
|
||||
scc_ast_expr_t *init; // 可为 NULL
|
||||
} var;
|
||||
// 函数声明
|
||||
struct {
|
||||
const char *name;
|
||||
scc_ast_type_t *type; // 函数类型
|
||||
scc_ast_stmt_t *body; // 可为 null 表示只有声明
|
||||
} func;
|
||||
// 参数声明
|
||||
struct {
|
||||
const char *name;
|
||||
scc_ast_type_t *type;
|
||||
} param;
|
||||
// 结构体/联合声明
|
||||
struct {
|
||||
const char *name;
|
||||
scc_ast_decl_vec_t fields;
|
||||
} record;
|
||||
// 枚举声明
|
||||
struct {
|
||||
const char *name;
|
||||
scc_ast_expr_vec_t enumerators;
|
||||
} enumeration;
|
||||
// typedef 声明
|
||||
struct {
|
||||
const char *name;
|
||||
scc_ast_type_t *type;
|
||||
} typedef_decl;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user