feat(ast): 添加内置类型定义和AST节点初始化函数
添加了完整的内置类型支持,包括整数、浮点数、字符、布尔等基本类型, 以及它们的有符号/无符号变体。同时添加了大量的AST节点初始化函数, 简化了AST节点的创建过程。 BREAKING CHANGE: 重构了AST表达式和声明结构,移除了冗余字段, 统一了命名规范,并修改了函数调用和成员访问的表示方式。
This commit is contained in:
30
libs/ast/include/ast_builtin.h
Normal file
30
libs/ast/include/ast_builtin.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef __SCC_AST_BUILTIN_H__
|
||||
#define __SCC_AST_BUILTIN_H__
|
||||
|
||||
#include "ast_def.h"
|
||||
|
||||
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_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_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
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -1,7 +1,416 @@
|
||||
#ifndef __SCC_AST_H__
|
||||
#define __SCC_AST_H__
|
||||
|
||||
#include "ast_builtin.h"
|
||||
#include "ast_def.h"
|
||||
#include "ast_dump.h"
|
||||
|
||||
// decls can be null but maybe warning
|
||||
static inline void
|
||||
scc_ast_translation_unit_init(scc_ast_translation_unit_t *translation_unit,
|
||||
scc_ast_decl_vec_t *decls) {
|
||||
Assert(translation_unit != null);
|
||||
translation_unit->base.type = SCC_AST_TRANSLATION_UNIT;
|
||||
translation_unit->base.loc = scc_pos_create();
|
||||
if (decls == null) {
|
||||
scc_vec_init(translation_unit->declarations);
|
||||
} else {
|
||||
translation_unit->declarations = *decls;
|
||||
scc_vec_init(*decls);
|
||||
}
|
||||
}
|
||||
|
||||
// var_init can be null
|
||||
static inline void scc_ast_decl_val_init(scc_ast_decl_t *decl,
|
||||
scc_ast_type_t *type, const char *name,
|
||||
scc_ast_expr_t *var_init) {
|
||||
Assert(decl != null && name != null && type != null);
|
||||
decl->base.loc = scc_pos_create();
|
||||
decl->base.type = SCC_AST_DECL_VAR;
|
||||
decl->name = name;
|
||||
decl->var.type = type;
|
||||
decl->var.init = var_init;
|
||||
}
|
||||
|
||||
// body can be null
|
||||
static inline void scc_ast_decl_func_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);
|
||||
decl->base.loc = scc_pos_create();
|
||||
decl->base.type = SCC_AST_DECL_FUNC;
|
||||
decl->name = name;
|
||||
decl->func.type = type;
|
||||
Assert(type->base.type == SCC_AST_TYPE_FUNCTION);
|
||||
decl->func.body = body;
|
||||
}
|
||||
|
||||
// body 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);
|
||||
decl->base.loc = scc_pos_create();
|
||||
decl->base.type = SCC_AST_DECL_PARAM;
|
||||
decl->name = name;
|
||||
decl->param.type = type;
|
||||
}
|
||||
|
||||
// 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);
|
||||
decl->base.loc = scc_pos_create();
|
||||
decl->base.type = SCC_AST_DECL_STRUCT;
|
||||
decl->name = name;
|
||||
if (fields_move == null) {
|
||||
scc_vec_init(decl->record.fields);
|
||||
} else {
|
||||
decl->record.fields = *fields_move;
|
||||
scc_vec_init(*fields_move);
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
decl->base.loc = scc_pos_create();
|
||||
decl->base.type = SCC_AST_DECL_UNION;
|
||||
decl->name = name;
|
||||
if (fields_move == null) {
|
||||
scc_vec_init(decl->record.fields);
|
||||
} else {
|
||||
decl->record.fields = *fields_move;
|
||||
scc_vec_init(*fields_move);
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
decl->base.loc = scc_pos_create();
|
||||
decl->base.type = SCC_AST_DECL_ENUM;
|
||||
decl->name = name;
|
||||
if (fields_move == null) {
|
||||
scc_vec_init(decl->enumeration.enumerators);
|
||||
} else {
|
||||
decl->enumeration.enumerators = *fields_move;
|
||||
scc_vec_init(*fields_move);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void scc_ast_decl_typedef_init(scc_ast_decl_t *decl,
|
||||
const char *name,
|
||||
scc_ast_type_t *type) {
|
||||
Assert(decl != null && name != null && type != null);
|
||||
decl->base.loc = scc_pos_create();
|
||||
decl->base.type = SCC_AST_DECL_TYPEDEF;
|
||||
decl->name = name;
|
||||
decl->typedef_decl.type = type;
|
||||
}
|
||||
|
||||
// items can be null
|
||||
static inline void
|
||||
scc_ast_stmt_compound_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_block_item_vec_t *items_move) {
|
||||
Assert(stmt != null);
|
||||
stmt->base.loc = scc_pos_create();
|
||||
stmt->base.type = SCC_AST_STMT_COMPOUND;
|
||||
if (items_move == null) {
|
||||
scc_vec_init(stmt->compound.block_items);
|
||||
} else {
|
||||
stmt->compound.block_items = *items_move;
|
||||
scc_vec_init(*items_move);
|
||||
}
|
||||
}
|
||||
|
||||
// expr can be null
|
||||
static inline void scc_ast_stmt_expr_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *expr) {
|
||||
Assert(stmt != null);
|
||||
stmt->base.loc = scc_pos_create();
|
||||
stmt->base.type = SCC_AST_STMT_EXPR;
|
||||
stmt->expr.expr = expr;
|
||||
}
|
||||
|
||||
// opt_else can be null
|
||||
static inline void scc_ast_stmt_if_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_stmt_t *then,
|
||||
scc_ast_stmt_t *opt_else) {
|
||||
Assert(stmt != null && cond != null && then != null);
|
||||
stmt->base.loc = scc_pos_create();
|
||||
stmt->base.type = SCC_AST_STMT_IF;
|
||||
stmt->if_stmt.cond = cond;
|
||||
stmt->if_stmt.then_stmt = then;
|
||||
stmt->if_stmt.opt_else_stmt = opt_else;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_while_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_stmt_t *body) {
|
||||
Assert(stmt != null && cond != null && body != null);
|
||||
stmt->base.loc = scc_pos_create();
|
||||
stmt->base.type = SCC_AST_STMT_WHILE;
|
||||
stmt->while_stmt.cond = cond;
|
||||
stmt->while_stmt.body = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_do_while_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_stmt_t *body) {
|
||||
Assert(stmt != null && cond != null && body != null);
|
||||
stmt->base.loc = scc_pos_create();
|
||||
stmt->base.type = SCC_AST_STMT_DO_WHILE;
|
||||
stmt->while_stmt.cond = cond;
|
||||
stmt->while_stmt.body = body;
|
||||
}
|
||||
|
||||
// FIXME
|
||||
static inline void scc_ast_stmt_for_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_type_t *init,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_expr_t *incr,
|
||||
scc_ast_stmt_t *body) {
|
||||
Assert(stmt != null && body != null);
|
||||
stmt->base.loc = scc_pos_create();
|
||||
stmt->base.type = SCC_AST_STMT_FOR;
|
||||
stmt->for_stmt.init = init;
|
||||
stmt->for_stmt.cond = cond;
|
||||
stmt->for_stmt.incr = incr;
|
||||
stmt->for_stmt.body = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_switch_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_stmt_t *body) {
|
||||
Assert(stmt != null && cond != null && body != null);
|
||||
stmt->base.loc = scc_pos_create();
|
||||
stmt->base.type = SCC_AST_STMT_SWITCH;
|
||||
stmt->switch_stmt.cond = cond;
|
||||
stmt->switch_stmt.body = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_case_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_stmt_t *body) {
|
||||
Assert(stmt != null && cond != null && body != null);
|
||||
stmt->base.loc = scc_pos_create();
|
||||
stmt->base.type = SCC_AST_STMT_CASE;
|
||||
stmt->case_stmt.expr = cond;
|
||||
stmt->case_stmt.stmt = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_default_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_stmt_t *body) {
|
||||
Assert(stmt != null && body != null);
|
||||
stmt->base.loc = scc_pos_create();
|
||||
stmt->base.type = SCC_AST_STMT_DEFAULT;
|
||||
stmt->case_stmt.stmt = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_break_init(scc_ast_stmt_t *stmt) {
|
||||
Assert(stmt != null);
|
||||
stmt->base.loc = scc_pos_create();
|
||||
stmt->base.type = SCC_AST_STMT_BREAK;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_continue_init(scc_ast_stmt_t *stmt) {
|
||||
Assert(stmt != null);
|
||||
stmt->base.loc = scc_pos_create();
|
||||
stmt->base.type = SCC_AST_STMT_CONTINUE;
|
||||
}
|
||||
|
||||
// expr can be null
|
||||
static inline void scc_ast_stmt_return_init(scc_ast_stmt_t *stmt,
|
||||
scc_ast_expr_t *expr) {
|
||||
Assert(stmt != null);
|
||||
stmt->base.loc = scc_pos_create();
|
||||
stmt->base.type = SCC_AST_STMT_RETURN;
|
||||
stmt->return_stmt.expr = expr;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_goto_init(scc_ast_stmt_t *stmt,
|
||||
const char *label) {
|
||||
Assert(stmt != null && label != null);
|
||||
stmt->base.loc = scc_pos_create();
|
||||
stmt->base.type = SCC_AST_STMT_GOTO;
|
||||
stmt->goto_stmt.label = label;
|
||||
stmt->goto_stmt._target = null;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_label_init(scc_ast_stmt_t *stmt,
|
||||
const char *label,
|
||||
scc_ast_stmt_t *body) {
|
||||
Assert(stmt != null);
|
||||
stmt->base.loc = scc_pos_create();
|
||||
stmt->base.type = SCC_AST_STMT_LABEL;
|
||||
stmt->label_stmt.label = label;
|
||||
stmt->label_stmt.stmt = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_binary_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_op_t op,
|
||||
scc_ast_expr_t *lhs,
|
||||
scc_ast_expr_t *rhs) {
|
||||
Assert(expr != null && lhs != null && rhs != null);
|
||||
expr->base.loc = scc_pos_create();
|
||||
expr->base.type = SCC_AST_EXPR_BINARY;
|
||||
expr->binary.op = op;
|
||||
expr->binary.lhs = lhs;
|
||||
expr->binary.rhs = rhs;
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_unary_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_op_t op,
|
||||
scc_ast_expr_t *operand) {
|
||||
Assert(expr != null && operand != null);
|
||||
expr->base.loc = scc_pos_create();
|
||||
expr->base.type = SCC_AST_EXPR_UNARY;
|
||||
expr->unary.op = op;
|
||||
expr->unary.operand = operand;
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_cond_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_t *cond,
|
||||
scc_ast_expr_t *then_expr,
|
||||
scc_ast_expr_t *else_expr) {
|
||||
Assert(expr != null && cond != null && then_expr != null &&
|
||||
else_expr != null);
|
||||
expr->base.loc = scc_pos_create();
|
||||
expr->base.type = SCC_AST_EXPR_COND;
|
||||
expr->cond.cond = cond;
|
||||
expr->cond.then_expr = then_expr;
|
||||
expr->cond.else_expr = else_expr;
|
||||
}
|
||||
|
||||
// args can be null
|
||||
static inline void scc_ast_expr_call_init(scc_ast_expr_t *expr,
|
||||
const char *name,
|
||||
scc_ast_expr_vec_t *args) {
|
||||
Assert(expr != null && name != null);
|
||||
expr->base.loc = scc_pos_create();
|
||||
expr->base.type = SCC_AST_EXPR_CALL;
|
||||
expr->call.name = name;
|
||||
expr->call._target = null;
|
||||
if (args == null) {
|
||||
scc_vec_init(expr->call.args);
|
||||
} else {
|
||||
expr->call.args = *args;
|
||||
}
|
||||
}
|
||||
|
||||
// SCC_AST_EXPR_ARRAY_SUBSCRIPT, // 数组下标
|
||||
static inline void scc_ast_expr_array_subscript_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_t *array,
|
||||
scc_ast_expr_t *index) {
|
||||
Assert(expr != null && array != null && index != null);
|
||||
expr->base.loc = scc_pos_create();
|
||||
expr->base.type = SCC_AST_EXPR_ARRAY_SUBSCRIPT;
|
||||
expr->subscript.array = array;
|
||||
expr->subscript.index = index;
|
||||
}
|
||||
|
||||
static inline void _scc_ast_expr_member_init(scc_ast_expr_t *expr,
|
||||
scc_ast_node_type_t type,
|
||||
scc_ast_expr_t *object,
|
||||
const char *member) {
|
||||
Assert(expr != null && object != null && member != null);
|
||||
expr->base.loc = scc_pos_create();
|
||||
expr->base.type = type;
|
||||
expr->member.base = object;
|
||||
expr->member.name = member;
|
||||
expr->member._target_idx = 0;
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_member_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_t *object,
|
||||
const char *member) {
|
||||
_scc_ast_expr_member_init(expr, SCC_AST_EXPR_MEMBER, object, member);
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_ptr_member_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_t *object,
|
||||
const char *member) {
|
||||
_scc_ast_expr_member_init(expr, SCC_AST_EXPR_PTR_MEMBER, object, member);
|
||||
}
|
||||
|
||||
// SCC_AST_EXPR_CAST, // 类型转换
|
||||
// SCC_AST_EXPR_SIZE_OF, // sizeof
|
||||
// SCC_AST_EXPR_ALIGN_OF, // _Alignof
|
||||
// SCC_AST_EXPR_COMPOUND_LITERAL, // 复合字面量
|
||||
|
||||
static inline void scc_ast_expr_literal_init(scc_ast_expr_t *expr,
|
||||
scc_ast_node_type_t type,
|
||||
const char *value, cbool owned) {
|
||||
Assert(expr != null && value != null);
|
||||
expr->base.loc = scc_pos_create();
|
||||
expr->base.type = type;
|
||||
expr->literal.lexme = value;
|
||||
expr->literal.owned = owned;
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_literal_int_init(scc_ast_expr_t *expr,
|
||||
const char *value,
|
||||
cbool owned) {
|
||||
scc_ast_expr_literal_init(expr, SCC_AST_EXPR_INT_LITERAL, value, owned);
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_literal_float_init(scc_ast_expr_t *expr,
|
||||
const char *value,
|
||||
cbool owned) {
|
||||
scc_ast_expr_literal_init(expr, SCC_AST_EXPR_FLOAT_LITERAL, value, owned);
|
||||
}
|
||||
static inline void scc_ast_expr_literal_char_init(scc_ast_expr_t *expr,
|
||||
const char *value,
|
||||
cbool owned) {
|
||||
scc_ast_expr_literal_init(expr, SCC_AST_EXPR_CHAR_LITERAL, value, owned);
|
||||
}
|
||||
static inline void scc_ast_expr_literal_string_init(scc_ast_expr_t *expr,
|
||||
const char *value,
|
||||
cbool owned) {
|
||||
scc_ast_expr_literal_init(expr, SCC_AST_EXPR_STRING_LITERAL, value, owned);
|
||||
}
|
||||
|
||||
static inline void scc_ast_expr_identifier_init(scc_ast_expr_t *expr,
|
||||
const char *name) {
|
||||
Assert(expr != null && name != null);
|
||||
expr->base.loc = scc_pos_create();
|
||||
expr->base.type = SCC_AST_EXPR_IDENTIFIER;
|
||||
expr->identifier.name = name;
|
||||
expr->identifier._target = null;
|
||||
}
|
||||
|
||||
// SCC_AST_TYPE_BUILTIN, // 内置类型
|
||||
// SCC_AST_TYPE_POINTER, // 指针类型
|
||||
// SCC_AST_TYPE_ARRAY, // 数组类型
|
||||
|
||||
// return_type and params can be null
|
||||
static inline void scc_ast_type_function_init(scc_ast_type_t *type,
|
||||
scc_ast_type_t *return_type,
|
||||
scc_ast_decl_vec_t *params) {
|
||||
Assert(type != null);
|
||||
type->base.loc = scc_pos_create();
|
||||
type->base.type = SCC_AST_TYPE_FUNCTION;
|
||||
type->function.return_type = return_type;
|
||||
if (params == null) {
|
||||
scc_vec_init(type->function.param_types);
|
||||
} else {
|
||||
type->function.param_types = *params;
|
||||
scc_vec_init(*params);
|
||||
}
|
||||
}
|
||||
|
||||
// SCC_AST_TYPE_STRUCT, // 结构体类型
|
||||
// SCC_AST_TYPE_UNION, // 联合类型
|
||||
// SCC_AST_TYPE_ENUM, // 枚举类型
|
||||
// SCC_AST_TYPE_TYPEDEF, // typedef 类型
|
||||
|
||||
#endif /* __SCC_AST_H__ */
|
||||
|
||||
70
libs/ast/src/ast_builtin.c
Normal file
70
libs/ast/src/ast_builtin.c
Normal file
@@ -0,0 +1,70 @@
|
||||
#include <ast_builtin.h>
|
||||
|
||||
#define SCC_AST_BUILTIN_TYPE_HEADER \
|
||||
.base.type = SCC_AST_TYPE_BUILTIN, .base.loc.col = 0, .base.loc.line = 0, \
|
||||
.base.loc.name = "__scc_ast_builtin_type", .base.loc.offset = 0
|
||||
|
||||
scc_ast_type_t scc_ast_builtin_type_int = {
|
||||
SCC_AST_BUILTIN_TYPE_HEADER,
|
||||
.builtin.type = SCC_AST_BUILTIN_TYPE_INT,
|
||||
};
|
||||
|
||||
scc_ast_type_t scc_ast_builtin_type_long = {
|
||||
SCC_AST_BUILTIN_TYPE_HEADER,
|
||||
.builtin.type = SCC_AST_BUILTIN_TYPE_LONG,
|
||||
};
|
||||
|
||||
scc_ast_type_t scc_ast_builtin_type_long_long = {
|
||||
SCC_AST_BUILTIN_TYPE_HEADER,
|
||||
.builtin.type = SCC_AST_BUILTIN_TYPE_LONG_LONG,
|
||||
};
|
||||
|
||||
scc_ast_type_t scc_ast_builtin_type_short = {
|
||||
SCC_AST_BUILTIN_TYPE_HEADER,
|
||||
.builtin.type = SCC_AST_BUILTIN_TYPE_SHORT,
|
||||
};
|
||||
|
||||
scc_ast_type_t scc_ast_builtin_type_char = {
|
||||
SCC_AST_BUILTIN_TYPE_HEADER,
|
||||
.builtin.type = SCC_AST_BUILTIN_TYPE_CHAR,
|
||||
};
|
||||
|
||||
scc_ast_type_t scc_ast_builtin_type_void = {
|
||||
SCC_AST_BUILTIN_TYPE_HEADER,
|
||||
.builtin.type = SCC_AST_BUILTIN_TYPE_VOID,
|
||||
};
|
||||
|
||||
scc_ast_type_t scc_ast_builtin_type_bool = {
|
||||
SCC_AST_BUILTIN_TYPE_HEADER,
|
||||
.builtin.type = SCC_AST_BUILTIN_TYPE_BOOL,
|
||||
};
|
||||
|
||||
scc_ast_type_t scc_ast_builtin_type_float = {
|
||||
SCC_AST_BUILTIN_TYPE_HEADER,
|
||||
.builtin.type = SCC_AST_BUILTIN_TYPE_FLOAT,
|
||||
};
|
||||
|
||||
scc_ast_type_t scc_ast_builtin_type_double = {
|
||||
SCC_AST_BUILTIN_TYPE_HEADER,
|
||||
.builtin.type = SCC_AST_BUILTIN_TYPE_DOUBLE,
|
||||
};
|
||||
|
||||
scc_ast_type_t scc_ast_builtin_type_long_double = {
|
||||
SCC_AST_BUILTIN_TYPE_HEADER,
|
||||
.builtin.type = SCC_AST_BUILTIN_TYPE_LONG_DOUBLE,
|
||||
};
|
||||
|
||||
scc_ast_type_t scc_ast_builtin_type_complex_float = {
|
||||
SCC_AST_BUILTIN_TYPE_HEADER,
|
||||
.builtin.type = SCC_AST_BUILTIN_TYPE_COMPLEX_FLOAT,
|
||||
};
|
||||
|
||||
scc_ast_type_t scc_ast_builtin_type_complex_double = {
|
||||
SCC_AST_BUILTIN_TYPE_HEADER,
|
||||
.builtin.type = SCC_AST_BUILTIN_TYPE_COMPLEX_DOUBLE,
|
||||
};
|
||||
|
||||
scc_ast_type_t scc_ast_builtin_type_complex_long_double = {
|
||||
SCC_AST_BUILTIN_TYPE_HEADER,
|
||||
.builtin.type = SCC_AST_BUILTIN_TYPE_COMPLEX_LONG_DOUBLE,
|
||||
};
|
||||
@@ -324,7 +324,12 @@ static void dump_type_impl(scc_ast_type_t *type, scc_tree_dump_ctx_t *ctx) {
|
||||
}
|
||||
if (scc_vec_size(type->function.param_types) != 0) {
|
||||
scc_vec_foreach(type->function.param_types, i) {
|
||||
dump_type_impl(scc_vec_at(type->function.param_types, i), ctx);
|
||||
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 {
|
||||
start_node_dump(&type->base, ctx);
|
||||
@@ -435,7 +440,8 @@ static void dump_expr_impl(scc_ast_expr_t *expr, scc_tree_dump_ctx_t *ctx) {
|
||||
break;
|
||||
|
||||
case SCC_AST_EXPR_CALL:
|
||||
dump_child_node((scc_ast_node_t *)expr->call.callee, ctx, false);
|
||||
// dump_child_node((scc_ast_node_t *)expr->call._target, ctx, false);
|
||||
PRINT_NODE(ctx, "%s", expr->call.name);
|
||||
// 转储参数
|
||||
for (size_t i = 0; i < expr->call.args.size; i++) {
|
||||
dump_child_node((scc_ast_node_t *)expr->call.args.data[i], ctx,
|
||||
@@ -453,7 +459,7 @@ static void dump_expr_impl(scc_ast_expr_t *expr, scc_tree_dump_ctx_t *ctx) {
|
||||
dump_child_node((scc_ast_node_t *)expr->member.base, ctx, false);
|
||||
// 打印成员访问信息
|
||||
scc_tree_print_indent(ctx);
|
||||
PRINT_NODE(ctx, "Member [\"%s\"]", expr->member.member_name);
|
||||
PRINT_NODE(ctx, "Member [\"%s\"]", expr->member.name);
|
||||
scc_tree_dump_printf(ctx, "\n");
|
||||
break;
|
||||
|
||||
@@ -512,8 +518,8 @@ static void dump_stmt_impl(scc_ast_stmt_t *stmt, scc_tree_dump_ctx_t *ctx) {
|
||||
return;
|
||||
case SCC_AST_STMT_DO_WHILE:
|
||||
end_node_dump(ctx);
|
||||
dump_child_node((scc_ast_node_t *)stmt->do_while_stmt.body, ctx, false);
|
||||
dump_child_node((scc_ast_node_t *)stmt->do_while_stmt.cond, ctx, true);
|
||||
dump_child_node((scc_ast_node_t *)stmt->while_stmt.body, ctx, false);
|
||||
dump_child_node((scc_ast_node_t *)stmt->while_stmt.cond, ctx, true);
|
||||
return;
|
||||
case SCC_AST_STMT_SWITCH:
|
||||
end_node_dump(ctx);
|
||||
@@ -528,8 +534,8 @@ static void dump_stmt_impl(scc_ast_stmt_t *stmt, scc_tree_dump_ctx_t *ctx) {
|
||||
if (stmt->for_stmt.cond) {
|
||||
dump_child_node((scc_ast_node_t *)stmt->for_stmt.cond, ctx, false);
|
||||
}
|
||||
if (stmt->for_stmt.iter) {
|
||||
dump_child_node((scc_ast_node_t *)stmt->for_stmt.iter, ctx, false);
|
||||
if (stmt->for_stmt.incr) {
|
||||
dump_child_node((scc_ast_node_t *)stmt->for_stmt.incr, ctx, false);
|
||||
}
|
||||
dump_child_node((scc_ast_node_t *)stmt->for_stmt.body, ctx, true);
|
||||
return;
|
||||
@@ -594,46 +600,8 @@ static void dump_decl_impl(scc_ast_decl_t *decl, scc_tree_dump_ctx_t *ctx) {
|
||||
return;
|
||||
|
||||
start_node_dump(&decl->base, ctx);
|
||||
|
||||
// 根据声明类型输出特定信息
|
||||
switch (decl->base.type) {
|
||||
case SCC_AST_DECL_VAR:
|
||||
if (decl->var.name) {
|
||||
PRINT_QUOTED_VALUE(ctx, decl->var.name);
|
||||
}
|
||||
break;
|
||||
case SCC_AST_DECL_FUNC:
|
||||
if (decl->func.name) {
|
||||
PRINT_QUOTED_VALUE(ctx, decl->func.name);
|
||||
}
|
||||
break;
|
||||
case SCC_AST_DECL_PARAM:
|
||||
if (decl->param.name) {
|
||||
PRINT_QUOTED_VALUE(ctx, decl->param.name);
|
||||
}
|
||||
break;
|
||||
case SCC_AST_DECL_STRUCT:
|
||||
if (decl->record.name) {
|
||||
PRINT_QUOTED_VALUE(ctx, decl->record.name);
|
||||
}
|
||||
break;
|
||||
case SCC_AST_DECL_UNION:
|
||||
if (decl->record.name) {
|
||||
PRINT_QUOTED_VALUE(ctx, decl->record.name);
|
||||
}
|
||||
break;
|
||||
case SCC_AST_DECL_ENUM:
|
||||
if (decl->enumeration.name) {
|
||||
PRINT_QUOTED_VALUE(ctx, decl->enumeration.name);
|
||||
}
|
||||
break;
|
||||
case SCC_AST_DECL_TYPEDEF:
|
||||
if (decl->typedef_decl.name) {
|
||||
PRINT_QUOTED_VALUE(ctx, decl->typedef_decl.name);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
if (decl->name) {
|
||||
PRINT_QUOTED_VALUE(ctx, decl->name);
|
||||
}
|
||||
|
||||
end_node_dump(ctx);
|
||||
|
||||
Reference in New Issue
Block a user