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:
@@ -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;
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user