refactor(ast): 统一记录类型结构并移除成员访问操作符

- 移除了枚举类型的独立结构定义,统一使用record结构
- 移除了成员访问操作符SCC_AST_OP_MEMBER_ACCESS和SCC_AST_OP_PTR_MEMBER_ACCESS
- 更新了for循环语句中init字段的类型从scc_ast_type_t*到scc_ast_node_t*
- 修改了声明初始化函数以支持统一的记录类型处理

fix(ast2ir): 完善二元表达式处理和for循环代码生成

- 重构了赋值操作符的处理逻辑,通过临时表达式实现复合赋值
- 添加了对for循环语句的完整IR代码生成功能
- 修复了if-else语句中错误的基本块跳转问题
- 改进了标识符未找到时的错误提示信息

chore: 清理代码和修复潜在问题

- 移除了未使用的自动标签生成功能
- 统一了IR基本块标签格式化输出
- 修复了机器码生成中的寄存器存储控制流问题
- 改进了词法分析器中十六进制数字的处理逻辑
This commit is contained in:
zzy
2026-03-21 10:33:17 +08:00
parent a64e1f8330
commit 35a704a1cb
25 changed files with 1335 additions and 2095 deletions

View File

@@ -186,10 +186,6 @@ struct scc_ast_type {
const char *name;
scc_ast_decl_t *decl; // can be null
} record;
struct {
const char *name;
scc_ast_decl_t *decl; // can be null
} enumeration;
struct {
const char *name;
scc_ast_decl_t *decl;
@@ -255,9 +251,6 @@ typedef enum scc_ast_expr_op {
SCC_AST_OP_PREFIX_DECREMENT, // -- (前缀)
SCC_AST_OP_POSTFIX_INCREMENT, // ++ (后缀)
SCC_AST_OP_POSTFIX_DECREMENT, // -- (后缀)
/* 成员访问 */
SCC_AST_OP_MEMBER_ACCESS, // .
SCC_AST_OP_PTR_MEMBER_ACCESS, // ->
} scc_ast_expr_op_t;
typedef enum {
@@ -371,7 +364,7 @@ struct scc_ast_stmt {
} while_stmt;
// for 语句
struct {
scc_ast_type_t *init; // expr or decl or null
scc_ast_node_t *init; // expr or decl or null
scc_ast_expr_t *cond; // 可为 null
scc_ast_expr_t *incr; // 可为 null
scc_ast_stmt_t *body;
@@ -435,14 +428,10 @@ struct scc_ast_decl {
struct {
scc_ast_type_t *type;
} param;
// 结构体/联合声明
// 结构体/联合/枚举声明
struct {
scc_ast_decl_vec_t fields;
} record;
// 枚举声明
struct {
scc_ast_expr_vec_t enumerators;
} enumeration;
// typedef 声明
struct {
scc_ast_type_t *type;

View File

@@ -82,14 +82,14 @@ static inline void scc_ast_decl_param_init(scc_ast_decl_t *decl,
decl->param.type = type;
}
// 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,
scc_pos_t loc) {
static inline void _scc_ast_decl_record_init(scc_ast_decl_t *decl,
scc_ast_node_type_t type,
const char *name,
scc_ast_decl_vec_t *fields_move,
scc_pos_t loc) {
Assert(decl != null);
decl->base.loc = loc;
decl->base.type = SCC_AST_DECL_STRUCT;
decl->base.type = type;
decl->name = name;
if (fields_move == null) {
scc_vec_init(decl->record.fields);
@@ -99,38 +99,29 @@ static inline void scc_ast_decl_struct_init(scc_ast_decl_t *decl,
}
}
// 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,
scc_pos_t loc) {
_scc_ast_decl_record_init(decl, SCC_AST_DECL_STRUCT, name, fields_move,
loc);
}
// 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,
scc_pos_t loc) {
Assert(decl != null);
decl->base.loc = loc;
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);
}
_scc_ast_decl_record_init(decl, SCC_AST_DECL_UNION, name, fields_move, loc);
}
// 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,
scc_ast_decl_vec_t *fields_move,
scc_pos_t loc) {
Assert(decl != null);
decl->base.loc = loc;
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);
}
_scc_ast_decl_record_init(decl, SCC_AST_DECL_ENUM, name, fields_move, loc);
}
static inline void scc_ast_decl_typedef_init(scc_ast_decl_t *decl,
@@ -205,7 +196,7 @@ static inline void scc_ast_stmt_do_while_init(scc_ast_stmt_t *stmt,
// FIXME
static inline void scc_ast_stmt_for_init(scc_ast_stmt_t *stmt,
scc_ast_type_t *init,
scc_ast_node_t *init,
scc_ast_expr_t *cond,
scc_ast_expr_t *incr,
scc_ast_stmt_t *body, scc_pos_t loc) {
@@ -591,12 +582,7 @@ static inline void scc_ast_type_union_init(scc_ast_type_t *type,
static inline void scc_ast_type_enum_init(scc_ast_type_t *type,
const char *name,
scc_ast_decl_t *decl, scc_pos_t loc) {
Assert(type != null);
type->base.loc = loc;
type->base.type = SCC_AST_TYPE_ENUM;
type->quals = (scc_ast_decl_specifier_t){0}; // FIXME
type->enumeration.name = name;
type->enumeration.decl = decl;
_scc_ast_type_record_init(type, SCC_AST_TYPE_ENUM, name, decl, loc);
}
static inline void scc_ast_type_typedef_init(scc_ast_type_t *type,

View File

@@ -195,10 +195,6 @@ static const char *get_op_str(scc_ast_expr_op_t op) {
return "++";
case SCC_AST_OP_POSTFIX_DECREMENT:
return "--";
case SCC_AST_OP_MEMBER_ACCESS:
return ".";
case SCC_AST_OP_PTR_MEMBER_ACCESS:
return "->";
default:
return "<op>";
}
@@ -300,8 +296,8 @@ static void dump_type_impl(scc_ast_type_t *type, scc_tree_dump_ctx_t *ctx) {
}
break;
case SCC_AST_TYPE_ENUM:
if (type->enumeration.name) {
BUILD_TYPE_NAME(ctx, "enum ", type->enumeration.name);
if (type->record.name) {
BUILD_TYPE_NAME(ctx, "enum ", type->record.name);
} else {
PRINT_QUOTED_VALUE(ctx, "anonymous enum");
}
@@ -327,17 +323,6 @@ static void dump_type_impl(scc_ast_type_t *type, scc_tree_dump_ctx_t *ctx) {
dump_child_node((scc_ast_node_t *)type->array.size, ctx, true);
}
break;
case SCC_AST_TYPE_STRUCT:
if (type->enumeration.decl) {
dump_child_node((scc_ast_node_t *)type->enumeration.decl, ctx,
true);
}
break;
case SCC_AST_TYPE_ENUM:
if (type->record.decl) {
dump_child_node((scc_ast_node_t *)type->record.decl, ctx, true);
}
break;
case SCC_AST_TYPE_FUNCTION:
scc_vec_push(ctx->stack, false);
scc_tree_print_indent(ctx);
@@ -585,6 +570,7 @@ static void dump_stmt_impl(scc_ast_stmt_t *stmt, scc_tree_dump_ctx_t *ctx) {
case SCC_AST_STMT_BREAK:
case SCC_AST_STMT_CONTINUE:
end_node_dump(ctx);
break;
case SCC_AST_STMT_DEFAULT:
@@ -645,6 +631,7 @@ static void dump_decl_impl(scc_ast_decl_t *decl, scc_tree_dump_ctx_t *ctx) {
case SCC_AST_DECL_STRUCT:
case SCC_AST_DECL_UNION:
case SCC_AST_DECL_ENUM:
scc_vec_foreach(decl->record.fields, i) {
dump_child_node(
(scc_ast_node_t *)scc_vec_at(decl->record.fields, i), ctx,
@@ -652,14 +639,6 @@ static void dump_decl_impl(scc_ast_decl_t *decl, scc_tree_dump_ctx_t *ctx) {
}
break;
case SCC_AST_DECL_ENUM:
scc_vec_foreach(decl->enumeration.enumerators, i) {
dump_child_node(
(scc_ast_node_t *)scc_vec_at(decl->enumeration.enumerators, i),
ctx, i + 1 == scc_vec_size(decl->enumeration.enumerators));
}
break;
case SCC_AST_DECL_TYPEDEF:
if (decl->typedef_decl.type) {
dump_child_node((scc_ast_node_t *)decl->typedef_decl.type, ctx,

View File

@@ -99,105 +99,123 @@ scc_ir_node_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
return 0;
}
cbool is_assign = true;
switch (expr->base.type) {
case SCC_AST_EXPR_BINARY: {
scc_ast_expr_t tmp_expr;
scc_ir_node_ref_t lhs, rhs;
cbool is_assign = false;
switch (expr->binary.op) {
case SCC_AST_OP_ASSIGN: // =
case SCC_AST_OP_ASSIGN_ADD: // +=
case SCC_AST_OP_ASSIGN_SUB: // -=
case SCC_AST_OP_ASSIGN_MUL: // *=
case SCC_AST_OP_ASSIGN_DIV: // /=
case SCC_AST_OP_ASSIGN_MOD: // %=
case SCC_AST_OP_ASSIGN_AND: // &=
case SCC_AST_OP_ASSIGN_XOR: // ^=
case SCC_AST_OP_ASSIGN_OR: // |=
case SCC_AST_OP_ASSIGN: // =
rhs = scc_ast2ir_expr(ctx, expr->binary.rhs, false);
break;
case SCC_AST_OP_ASSIGN_ADD: // +=
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_ADD,
expr->binary.lhs, expr->binary.rhs,
expr->base.loc);
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
break;
case SCC_AST_OP_ASSIGN_SUB: // -=
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_SUB,
expr->binary.lhs, expr->binary.rhs,
expr->base.loc);
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
break;
case SCC_AST_OP_ASSIGN_MUL: // *=
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_MUL,
expr->binary.lhs, expr->binary.rhs,
expr->base.loc);
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
break;
case SCC_AST_OP_ASSIGN_DIV: // /=
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_DIV,
expr->binary.lhs, expr->binary.rhs,
expr->base.loc);
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
break;
case SCC_AST_OP_ASSIGN_MOD: // %=
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_MOD,
expr->binary.lhs, expr->binary.rhs,
expr->base.loc);
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
break;
case SCC_AST_OP_ASSIGN_AND: // &=
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_BITWISE_AND,
expr->binary.lhs, expr->binary.rhs,
expr->base.loc);
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
break;
case SCC_AST_OP_ASSIGN_XOR: // ^=
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_BITWISE_XOR,
expr->binary.lhs, expr->binary.rhs,
expr->base.loc);
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
break;
case SCC_AST_OP_ASSIGN_OR: // |=
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_BITWISE_OR,
expr->binary.lhs, expr->binary.rhs,
expr->base.loc);
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
break;
case SCC_AST_OP_ASSIGN_LSHIFT: // <<=
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_LEFT_SHIFT,
expr->binary.lhs, expr->binary.rhs,
expr->base.loc);
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
break;
case SCC_AST_OP_ASSIGN_RSHIFT: // >>=
is_assign = true;
scc_ast_expr_binary_init(&tmp_expr, SCC_AST_OP_RIGHT_SHIFT,
expr->binary.lhs, expr->binary.rhs,
expr->base.loc);
rhs = scc_ast2ir_expr(ctx, &tmp_expr, false);
break;
default:
is_assign = false;
break;
}
lhs = scc_ast2ir_expr(ctx, expr->binary.lhs, is_assign);
rhs = scc_ast2ir_expr(ctx, expr->binary.rhs, false);
if (is_assign) {
lhs = scc_ast2ir_expr(ctx, expr->binary.lhs, true);
return scc_ir_builder_store(&ctx->builder, lhs, rhs);
}
rhs = scc_ast2ir_expr(ctx, expr->binary.rhs, false);
lhs = scc_ast2ir_expr(ctx, expr->binary.lhs, false);
// 映射操作符
scc_ir_op_type_t op;
switch (expr->binary.op) {
/* clang-format off */
case SCC_AST_OP_ADD: op = SCC_IR_OP_ADD; break;
case SCC_AST_OP_SUB: op = SCC_IR_OP_SUB; break;
case SCC_AST_OP_MUL: op = SCC_IR_OP_MUL; break;
case SCC_AST_OP_DIV: op = SCC_IR_OP_DIV; break;
case SCC_AST_OP_MOD: op = SCC_IR_OP_MOD; break;
case SCC_AST_OP_LOGICAL_AND: op = SCC_IR_OP_AND; break;
case SCC_AST_OP_LOGICAL_OR: op = SCC_IR_OP_OR; break;
case SCC_AST_OP_BITWISE_XOR: op = SCC_IR_OP_XOR; break;
case SCC_AST_OP_LEFT_SHIFT: op = SCC_IR_OP_SHL; break;
case SCC_AST_OP_RIGHT_SHIFT: op = SCC_IR_OP_SHR; break;
case SCC_AST_OP_EQUAL: op = SCC_IR_OP_EQ; break;
case SCC_AST_OP_NOT_EQUAL: op = SCC_IR_OP_NEQ; break;
case SCC_AST_OP_LESS: op = SCC_IR_OP_LT; break;
case SCC_AST_OP_LESS_EQUAL: op = SCC_IR_OP_LE; break;
case SCC_AST_OP_GREATER: op = SCC_IR_OP_GT; break;
case SCC_AST_OP_GREATER_EQUAL: op = SCC_IR_OP_GE; break;
case SCC_AST_OP_ASSIGN: {
return scc_ir_builder_store(&ctx->builder, lhs, rhs);
case SCC_AST_OP_ADD: op = SCC_IR_OP_ADD; break;
case SCC_AST_OP_SUB: op = SCC_IR_OP_SUB; break;
case SCC_AST_OP_MUL: op = SCC_IR_OP_MUL; break;
case SCC_AST_OP_DIV: op = SCC_IR_OP_DIV; break;
case SCC_AST_OP_MOD: op = SCC_IR_OP_MOD; break;
case SCC_AST_OP_BITWISE_AND: op = SCC_IR_OP_AND; break;
case SCC_AST_OP_BITWISE_OR: op = SCC_IR_OP_OR; break;
case SCC_AST_OP_BITWISE_XOR: op = SCC_IR_OP_XOR; break;
case SCC_AST_OP_LEFT_SHIFT: op = SCC_IR_OP_SHL; break;
case SCC_AST_OP_RIGHT_SHIFT: {
op = SCC_IR_OP_SHR;
// FIXME op = SCC_IR_OP_SAR;
break;
}
case SCC_AST_OP_ASSIGN_ADD:
case SCC_AST_OP_EQUAL: op = SCC_IR_OP_EQ; break;
case SCC_AST_OP_NOT_EQUAL: op = SCC_IR_OP_NEQ; break;
case SCC_AST_OP_LESS: op = SCC_IR_OP_LT; break;
case SCC_AST_OP_LESS_EQUAL: op = SCC_IR_OP_LE; break;
case SCC_AST_OP_GREATER: op = SCC_IR_OP_GT; break;
case SCC_AST_OP_GREATER_EQUAL: op = SCC_IR_OP_GE; break;
case SCC_AST_OP_COMMA: {
// 逗号运算符:计算左表达式,丢弃结果,返回右表达式
return rhs;
}
/* 逻辑操作符 */
case SCC_AST_OP_LOGICAL_OR: // ||
case SCC_AST_OP_LOGICAL_AND: // &&
TODO();
// /* 赋值操作符 */
// /* 条件操作符 */
// SCC_AST_OP_CONDITIONAL, // ?:
// /* 逗号操作符 */
// SCC_AST_OP_COMMA, // ,
// /* 逻辑操作符 */
// SCC_AST_OP_LOGICAL_OR, // ||
// SCC_AST_OP_LOGICAL_AND, // &&
// /* 位操作符 */
// SCC_AST_OP_BITWISE_OR, // |
// SCC_AST_OP_BITWISE_XOR, // ^
// SCC_AST_OP_BITWISE_AND, // &
// /* 相等性操作符 */
// SCC_AST_OP_EQUAL, // ==
// SCC_AST_OP_NOT_EQUAL, // !=
// /* 关系操作符 */
// SCC_AST_OP_LESS, // <
// SCC_AST_OP_GREATER, // >
// SCC_AST_OP_LESS_EQUAL, // <=
// SCC_AST_OP_GREATER_EQUAL, // >=
// /* 移位操作符 */
// SCC_AST_OP_LEFT_SHIFT, // <<
// SCC_AST_OP_RIGHT_SHIFT, // >>
// /* 算术操作符 */
// SCC_AST_OP_ADD, // +
// SCC_AST_OP_SUB, // -
// SCC_AST_OP_MUL, // *
// SCC_AST_OP_DIV, // /
// SCC_AST_OP_MOD, // %
// /* 一元操作符 */
// SCC_AST_OP_UNARY_PLUS, // + (一元)
// SCC_AST_OP_UNARY_MINUS, // - (一元)
// SCC_AST_OP_ADDRESS_OF, // &
// SCC_AST_OP_INDIRECTION, // *
// SCC_AST_OP_BITWISE_NOT, // ~
// SCC_AST_OP_LOGICAL_NOT, // !
// SCC_AST_OP_PREFIX_INCREMENT, // ++ (前缀)
// SCC_AST_OP_PREFIX_DECREMENT, // -- (前缀)
// SCC_AST_OP_POSTFIX_INCREMENT, // ++ (后缀)
// SCC_AST_OP_POSTFIX_DECREMENT, // -- (后缀)
// /* 成员访问 */
// SCC_AST_OP_MEMBER_ACCESS, // .
// SCC_AST_OP_PTR_MEMBER_ACCESS, // ->
/* clang-format on */
/* clang-format on */
default:
LOG_FATAL("Unsupported binary operator: %d", expr->binary.op);
return 0;
}
// 创建操作节点
return scc_ir_builder_binop(&ctx->builder, op, lhs, rhs);
}
@@ -205,10 +223,20 @@ scc_ir_node_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
case SCC_AST_EXPR_UNARY: {
scc_ir_node_ref_t operand =
scc_ast2ir_expr(ctx, expr->unary.operand, is_lvalue);
// /* 一元操作符 */
// SCC_AST_OP_UNARY_PLUS, // + (一元)
// SCC_AST_OP_UNARY_MINUS, // - (一元)
// SCC_AST_OP_ADDRESS_OF, // &
// SCC_AST_OP_INDIRECTION, // *
// SCC_AST_OP_BITWISE_NOT, // ~
// SCC_AST_OP_LOGICAL_NOT, // !
// SCC_AST_OP_PREFIX_INCREMENT, // ++ (前缀)
// SCC_AST_OP_PREFIX_DECREMENT, // -- (前缀)
// SCC_AST_OP_POSTFIX_INCREMENT, // ++ (后缀)
// SCC_AST_OP_POSTFIX_DECREMENT, // -- (后缀)
// 映射一元操作符
switch (expr->unary.op) {
case SCC_AST_OP_SUB:
case SCC_AST_OP_UNARY_MINUS:
// 负号
// 实现为0 - operand
return scc_ir_builder_binop(
@@ -266,7 +294,7 @@ scc_ir_node_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
// SCC_AST_EXPR_ALIGN_OF, // _Alignof
// SCC_AST_EXPR_COMPOUND, // 复合字面量
// SCC_AST_EXPR_LVALUE, // 右值
// SCC_AST_EXPR_BUILTIN, // 内置表达式
// SCC_AST_EXPR_BUILTIN,// 内置表达式 ... directive map to ir builtin
case SCC_AST_EXPR_INT_LITERAL: {
// FIXME maybe using some array to int;
@@ -284,7 +312,7 @@ scc_ir_node_ref_t scc_ast2ir_expr(scc_ast2ir_ctx_t *ctx, scc_ast_expr_t *expr,
case SCC_AST_EXPR_IDENTIFIER: {
if (expr->identifier._target == null) {
LOG_ERROR("unknown identifier");
LOG_ERROR("unknown identifier %s", expr->identifier.name);
}
// FIXME hack hashtable
scc_ir_node_ref_t in = (scc_ir_node_ref_t)(usize)scc_hashtable_get(
@@ -365,7 +393,7 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, scc_ast_stmt_t *stmt) {
// 生成false分支
if (stmt->if_stmt.opt_else_stmt) {
scc_ir_builder_set_current_bblock(&ctx->builder, true_block);
scc_ir_builder_set_current_bblock(&ctx->builder, false_block);
scc_ast2ir_stmt(ctx, stmt->if_stmt.opt_else_stmt);
scc_ir_builder_jump(&ctx->builder, merge_block);
}
@@ -421,13 +449,46 @@ void scc_ast2ir_stmt(scc_ast2ir_ctx_t *ctx, scc_ast_stmt_t *stmt) {
}
case SCC_AST_STMT_FOR: {
TODO();
scc_ir_bblock_ref_t cond_block =
scc_ir_builder_bblock(&ctx->builder, "for_while_cond");
scc_ir_bblock_ref_t body_block =
scc_ir_builder_bblock(&ctx->builder, "for_while_body");
scc_ir_bblock_ref_t exit_block =
scc_ir_builder_bblock(&ctx->builder, "for_while_exit");
if (stmt->for_stmt.init) {
if (SCC_AST_IS_A(scc_ast_decl_t, stmt->for_stmt.init)) {
scc_ast2ir_decl(
ctx, SCC_AST_CAST_TO(scc_ast_decl_t, stmt->for_stmt.init));
} else if (SCC_AST_IS_A(scc_ast_expr_t, stmt->for_stmt.init)) {
scc_ast2ir_expr(
ctx, SCC_AST_CAST_TO(scc_ast_expr_t, stmt->for_stmt.init),
false);
} else {
LOG_FATAL("invalid for init statement");
}
}
scc_ir_builder_jump(&ctx->builder, cond_block);
scc_ir_builder_set_current_bblock(&ctx->builder, cond_block);
if (stmt->for_stmt.cond) {
scc_ir_node_ref_t cond_node =
scc_ast2ir_expr(ctx, stmt->for_stmt.cond, false);
scc_ir_builder_branch(&ctx->builder, cond_node, body_block,
exit_block);
} else {
scc_ir_builder_jump(&ctx->builder, body_block);
}
scc_ir_builder_set_current_bblock(&ctx->builder, body_block);
scc_ast2ir_stmt(ctx, stmt->for_stmt.body);
if (stmt->for_stmt.incr) {
scc_ast2ir_expr(ctx, stmt->for_stmt.incr, false);
}
scc_ir_builder_jump(&ctx->builder, cond_block);
scc_ir_builder_set_current_bblock(&ctx->builder, exit_block);
break;
}
@@ -488,12 +549,12 @@ void scc_ast2ir_decl(scc_ast2ir_ctx_t *ctx, scc_ast_decl_t *decl) {
}
case SCC_AST_DECL_FUNC: {
// TODO params name
scc_ir_type_ref_t func_type = scc_ast2ir_type(ctx, decl->func.type);
if (decl->func.body == null) {
// function decl
break;
}
// TODO params name
scc_ir_builder_begin_func(&ctx->builder, decl->name, func_type, null);
scc_ir_builder_begin_bblock(&ctx->builder, "entry");
scc_ast2ir_stmt(ctx, decl->func.body);
@@ -546,6 +607,5 @@ void scc_ast2ir_ctx_init(scc_ast2ir_ctx_t *ctx, const scc_type_abi_t *abi) {
Assert(ctx != null);
ctx->abi = abi;
scc_ir_builder_init(&ctx->builder);
scc_hashtable_init(&ctx->node2ir, scc_hash_node, scc_cmp_node);
}

View File

@@ -83,23 +83,18 @@ scc_ir_func_ref_t scc_ir_builder_current_func(scc_ir_builder_t *builder) {
scc_ir_bblock_ref_t scc_ir_builder_bblock(scc_ir_builder_t *builder,
const char *label) {
scc_ir_bblock_t bblock = {0};
if (label) {
bblock.label = label;
} else {
// TODO 自动生成标签
char *auto_label = scc_malloc(sizeof(char) * 64);
if (auto_label)
LOG_FATAL("allocate memory failed");
static int auto_counter = 0;
scc_snprintf(auto_label, sizeof(auto_label), ".BB%d", auto_counter++);
bblock.label = auto_label;
}
scc_vec_init(bblock.instrs);
scc_ir_bblock_ref_t bblock_ref =
scc_ir_ctx_new_bblock(&builder->ctx, &bblock);
scc_ir_func_t *current_func =
scc_ir_ctx_get_func(&builder->ctx, builder->current_func);
if (current_func) {
scc_vec_push(current_func->bblocks, bblock_ref);
}
return bblock_ref;
}
@@ -107,14 +102,6 @@ scc_ir_bblock_ref_t scc_ir_builder_begin_bblock(scc_ir_builder_t *builder,
const char *label) {
builder->current_bblock = scc_ir_builder_bblock(builder, label);
// 将基本块添加到当前函数
scc_ir_func_t *current_func =
scc_ir_ctx_get_func(&builder->ctx, builder->current_func);
if (current_func) {
scc_vec_push(current_func->bblocks, builder->current_bblock);
}
return builder->current_bblock;
}

View File

@@ -679,11 +679,11 @@ void scc_ir_dump_node_linear(scc_ir_dump_ctx_t *ctx,
char cond_buf[64];
format_node_ref_or_value(ctx, cond_buf, sizeof(cond_buf),
node->data.branch.cond);
p += scc_snprintf(p, remaining, "br %s, label %%%u, label %%%u",
p += scc_snprintf(p, remaining, "br %s, label %%L%u, label %%L%u",
cond_buf, node->data.branch.true_bblock,
node->data.branch.false_bblock);
} else {
p += scc_snprintf(p, remaining, "br label %%%u",
p += scc_snprintf(p, remaining, "br label %%L%u",
node->data.branch.true_bblock);
}
break;
@@ -694,6 +694,7 @@ void scc_ir_dump_node_linear(scc_ir_dump_ctx_t *ctx,
break;
case SCC_IR_NODE_CALL: {
char node_name[256] = "";
char args_buf[256] = "";
char *args_p = args_buf;
usize args_remaining = sizeof(args_buf);
@@ -712,8 +713,10 @@ void scc_ir_dump_node_linear(scc_ir_dump_ctx_t *ctx,
args_remaining = sizeof(args_buf) - (args_p - args_buf);
}
p += scc_snprintf(p, remaining, "call @%%%u(%s)",
node->data.call.callee, args_buf);
format_node_ref_or_value(ctx, node_name, sizeof(node_name),
node->data.call.callee);
p += scc_snprintf(p, remaining, "call @%s(%s)", node_name, args_buf);
break;
}
@@ -751,9 +754,11 @@ void scc_ir_dump_bblock_linear(scc_ir_dump_ctx_t *ctx,
// 打印基本块标签
static char label_buff[128];
if (bblock->label && bblock->label[0] != '\0') {
scc_snprintf(label_buff, sizeof(label_buff), "%%%s:", bblock->label);
scc_snprintf(label_buff, sizeof(label_buff), "%%L%d %s:", bblock_ref,
bblock->label);
} else {
scc_snprintf(label_buff, sizeof(label_buff), "<unnamed>:");
scc_snprintf(label_buff, sizeof(label_buff),
"%%L%d <unnamed>:", bblock_ref);
}
PRINT_NODE(ctx->dump_ctx, label_buff);

View File

@@ -63,6 +63,7 @@ static void store_value_from_reg(scc_mcode_t *mcode, scc_reg_loc_t *loc,
if (loc->idx != reg) {
scc_mcode_amd64_mov_r64_r64(mcode, loc->idx, reg);
}
break;
case SCC_REG_KIND_STACK:
scc_mcode_amd64_mov_m64_disp32_r64(mcode, SCC_AMD64_RBP, -loc->idx,
reg);
@@ -72,6 +73,7 @@ static void store_value_from_reg(scc_mcode_t *mcode, scc_reg_loc_t *loc,
break;
default:
LOG_FATAL("unsupported location");
break;
}
}
@@ -87,7 +89,7 @@ static void parse_node(scc_ir2mcode_ctx_t *ctx, scc_ir_bblock_ref_t node_ref,
case SCC_IR_NODE_CONV: ///< 类型转换
case SCC_IR_NODE_FUNC_ARG_REF: ///< 函数参数引用
case SCC_IR_NODE_BLOCK_ARG_REF: ///< 基本块参数引用
LOG_ERROR("Unsupported node type: %d", node->tag);
LOG_FATAL("Unsupported node type: %d", node->tag);
break;
case SCC_IR_NODE_ALLOC: ///< 分配内存(stack)
case SCC_IR_NODE_GLOBAL_ALLOC: ///< 全局分配(bss)
@@ -153,7 +155,7 @@ static void parse_node(scc_ir2mcode_ctx_t *ctx, scc_ir_bblock_ref_t node_ref,
case SCC_IR_NODE_BRANCH: ///< 有条件分支
case SCC_IR_NODE_JUMP: ///< 无条件跳转
case SCC_IR_NODE_CALL: ///< 调用函数
LOG_ERROR("Unsupported node type: %d", node->tag);
LOG_FATAL("Unsupported node type: %d", node->tag);
break;
case SCC_IR_NODE_RET: ///< 函数返回
if (node->data.ret.ret_val) {

View File

@@ -173,6 +173,7 @@ void scc_lexer_get_token(scc_lexer_t *lexer, scc_lexer_tok_t *token) {
// 数字字面量(整数/浮点)
token->type = SCC_TOK_INT_LITERAL; // 先假定整数
cbool maybe_float = false;
cbool is_hex = false;
while (1) {
next_char(lexer, &lex, &cur); // 消费当前数字
if (!peek_char(lexer, &cur))
@@ -185,12 +186,16 @@ void scc_lexer_get_token(scc_lexer_t *lexer, scc_lexer_tok_t *token) {
}
if (ch == 'e' || ch == 'E' || ch == 'p' || ch == 'P') {
maybe_float = true;
// 后面可能跟符号或数字
continue;
}
if (is_hex &&
((ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F'))) {
continue;
}
if (ch == 'x' || ch == 'X') {
// 十六进制前缀,需特殊处理
// 这里简化:将整个序列作为整数(保留前缀)
is_hex = true;
continue;
} else if (ch == 'b' || ch == 'B') {
continue;
}
break;

View File

@@ -17,10 +17,13 @@ static void free_token(scc_lexer_tok_t *tok) { scc_cstring_free(&tok->lexeme); }
scc_lexer_init(&lexer, ref); \
scc_lexer_get_token(&lexer, &token); \
\
TEST_CHECK(token.type == expected_type); \
TEST_CHECK(token.type == expected_type && \
scc_strcmp(input, scc_cstring_as_cstr(&token.lexeme)) == \
0); \
TEST_MSG("Input: '%s'", input); \
TEST_MSG("Expected: %s", scc_get_tok_name(expected_type)); \
TEST_MSG("Got: %s", scc_get_tok_name(token.type)); \
TEST_MSG("Expected: %s `%s`", scc_get_tok_name(expected_type), input); \
TEST_MSG("Got: %s `%s`", scc_get_tok_name(token.type), \
scc_cstring_as_cstr(&token.lexeme)); \
\
free_token(&token); \
scc_sstream_drop_ring(ref); \
@@ -167,7 +170,7 @@ void test_literals() {
TEST_TOKEN("0x1A3F", SCC_TOK_INT_LITERAL);
TEST_TOKEN("0XABCDEF", SCC_TOK_INT_LITERAL);
TEST_TOKEN("0x123abc", SCC_TOK_INT_LITERAL);
TEST_TOKEN("0XFF", SCC_TOK_INT_LITERAL);
TEST_TOKEN("0xFF", SCC_TOK_INT_LITERAL);
TEST_CASE("Integer literals - octal");
TEST_TOKEN("0123", SCC_TOK_INT_LITERAL);

View File

@@ -288,8 +288,15 @@ CONTINUE:
scc_ast_expr_lvalue_init(lvalue, decl->var.type, decl->base.loc);
decl->var.init = scc_parse_initializer(parser, lvalue);
} else if (tok_ptr->type == SCC_TOK_L_BRACE) {
scc_parse_decl_sema(parser, decl);
parser->sema_callbacks.on_decl(parser->sema_callbacks.context,
scc_ast_decl_t_BEGIN, null);
scc_vec_foreach(decl->func.type->function.params, i) {
scc_ast_decl_t *param =
scc_vec_at(decl->func.type->function.params, i);
// Add params to decl
scc_parse_decl_sema(parser, param);
}
scc_ast_stmt_t *body = scc_parse_statement(parser);
parser->sema_callbacks.on_decl(parser->sema_callbacks.context,
scc_ast_decl_t_END, null);

View File

@@ -269,15 +269,15 @@ static scc_ast_stmt_t *parse_for_statement(scc_parser_t *parser,
"Expected '(' before like `( expression )` .");
}
scc_ast_type_t *init = null;
scc_ast_node_t *init = null;
scc_ast_expr_t *cond = null;
scc_ast_expr_t *incr = null;
scc_ast_stmt_t *body = null;
// TODO use decl or expr
init = (scc_ast_type_t *)scc_parse_declaration(parser);
init = (scc_ast_node_t *)scc_parse_declaration(parser);
if (init == null) {
init = (scc_ast_type_t *)scc_parse_expression(parser);
init = (scc_ast_node_t *)scc_parse_expression(parser);
if (!scc_parser_consume_if(parser, SCC_TOK_SEMICOLON)) {
SCC_ERROR(scc_parser_got_current_pos(parser),
"Expected semicolon in for statement.");

View File

@@ -508,22 +508,25 @@ static scc_ast_type_t *build_type_from_info(type_spec_info_t *info,
if (info->is_unsigned)
builtin = SCC_AST_BUILTIN_TYPE_UNSIGNED_CHAR;
else if (!info->is_signed)
builtin = SCC_AST_BUILTIN_TYPE_CHAR; // plain char (可能区别于
// signed char)
builtin = SCC_AST_BUILTIN_TYPE_CHAR;
else
builtin = SCC_AST_BUILTIN_TYPE_SIGNED_CHAR;
} else if (info->is_short) {
builtin = info->is_unsigned ? SCC_AST_BUILTIN_TYPE_UNSIGNED_SHORT
: info->is_signed ? SCC_AST_BUILTIN_TYPE_SIGNED_SHORT
: SCC_AST_BUILTIN_TYPE_SHORT;
} else if (info->is_long_long) {
builtin = info->is_unsigned
? SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG_LONG
: SCC_AST_BUILTIN_TYPE_LONG_LONG;
: info->is_signed ? SCC_AST_BUILTIN_TYPE_SIGNED_LONG_LONG
: SCC_AST_BUILTIN_TYPE_LONG_LONG;
} else if (info->is_long) {
builtin = info->is_unsigned ? SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG
: info->is_signed ? SCC_AST_BUILTIN_TYPE_SIGNED_LONG
: SCC_AST_BUILTIN_TYPE_LONG;
} else { // int
} else {
builtin = info->is_unsigned ? SCC_AST_BUILTIN_TYPE_UNSIGNED_INT
: info->is_signed ? SCC_AST_BUILTIN_TYPE_SIGNED_INT
: SCC_AST_BUILTIN_TYPE_INT;
}
}
@@ -571,6 +574,10 @@ static scc_ast_type_t *parse_record_type(scc_parser_t *parser,
scc_lexer_tok_drop(&tok);
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
if (tok_ptr == null) {
SCC_ERROR(scc_parser_got_current_pos(parser), "Unexpected EOF");
return null;
}
const char *name = null;
scc_ast_decl_t *decl = null;
scc_ast_decl_vec_t member;
@@ -609,7 +616,7 @@ static scc_ast_type_t *parse_record_type(scc_parser_t *parser,
break;
} else {
SCC_ERROR(scc_parser_got_current_pos(parser),
"Unexpected token in enum specifier");
"Unexpected token in struct/union specifier");
scc_parser_next_consume(parser, null);
break;
}
@@ -663,9 +670,14 @@ static scc_ast_type_t *parse_enum_type(scc_parser_t *parser) {
scc_lexer_tok_drop(&tok);
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
if (tok_ptr == null) {
SCC_ERROR(scc_parser_got_current_pos(parser), "Unexpected EOF");
return null;
}
scc_ast_type_t *type = ast_type_alloc();
const char *name = null;
scc_ast_decl_t *decl = null;
scc_ast_expr_vec_t member;
scc_ast_decl_vec_t member;
scc_vec_init(member);
if (tok_ptr->type == SCC_TOK_IDENT) {
@@ -684,20 +696,42 @@ static scc_ast_type_t *parse_enum_type(scc_parser_t *parser) {
break;
}
scc_ast_expr_t *expr = scc_parse_assignment_expression(parser);
if (expr != null) {
scc_vec_push(member, expr);
continue;
scc_parser_next_consume(parser, &tok);
if (tok.type != SCC_TOK_IDENT) {
SCC_ERROR(scc_parser_got_current_pos(parser),
"Expected identifier in enum specifier");
break;
}
if (tok_ptr->type == SCC_TOK_COMMA) {
// FIXME check comma
scc_ast_expr_t *enum_item_init = null;
if (scc_parser_consume_if(parser, SCC_TOK_ASSIGN)) {
enum_item_init = scc_parser_constant_expression(parser);
}
scc_ast_decl_t *enum_item_decl = scc_malloc(sizeof(scc_ast_decl_t));
Assert(enum_item_decl != null);
scc_ast_decl_val_init(enum_item_decl, type,
scc_cstring_as_cstr(&tok.lexeme),
enum_item_init, tok.loc);
scc_vec_push(member, enum_item_decl);
tok_ptr = scc_parser_peek(parser);
cbool got_comma = false;
if (tok_ptr && tok_ptr->type == SCC_TOK_COMMA) {
scc_parser_next_consume(parser, null);
} else if (tok_ptr->type == SCC_TOK_R_BRACE) {
tok_ptr = scc_parser_peek(parser);
got_comma = true;
}
if (tok_ptr && tok_ptr->type == SCC_TOK_R_BRACE) {
scc_parser_next_consume(parser, null);
break;
} else {
SCC_ERROR(scc_parser_got_current_pos(parser),
if (got_comma) {
continue;
}
SCC_ERROR(tok_ptr != null ? tok_ptr->loc
: scc_parser_got_current_pos(parser),
"Unexpected token in enum specifier");
scc_parser_next_consume(parser, null);
break;
@@ -716,7 +750,6 @@ static scc_ast_type_t *parse_enum_type(scc_parser_t *parser) {
}
}
scc_ast_type_t *type = ast_type_alloc();
scc_ast_type_enum_init(type, name, decl, pos);
return type;
}
@@ -1071,7 +1104,10 @@ parse_direct_declarator(scc_parser_t *parser, scc_ast_type_t *base,
tok_ident);
} else {
ret = parse_declarator(parser, null, &delay_pointee, tok_ident);
Assert(ret != null);
if (ret == null) {
SCC_ERROR(tok_ident->loc, "parse_declarator failed");
Panic("parse_declarator failed ret == null");
}
if (!scc_parser_consume_if(parser, SCC_TOK_R_PAREN)) {
SCC_ERROR(scc_parser_got_current_pos(parser), "expect ')'");
}
@@ -1199,31 +1235,25 @@ scc_ast_decl_t *scc_parse_declarator(scc_parser_t *parser,
if (decl_name_tok.type != SCC_TOK_IDENT) {
if (decl_type->base.type == SCC_AST_TYPE_STRUCT ||
decl_type->base.type == SCC_AST_TYPE_UNION) {
decl_type->base.type == SCC_AST_TYPE_UNION ||
decl_type->base.type == SCC_AST_TYPE_ENUM) {
if (decl_type->record.decl == null) {
decl = scc_malloc(sizeof(scc_ast_decl_t));
Assert(decl != null);
if (decl_type->base.type == SCC_AST_TYPE_STRUCT) {
scc_ast_decl_struct_init(decl, decl_type->record.name, null,
decl_type->base.loc);
} else {
} else if (decl_type->base.type == SCC_AST_TYPE_UNION) {
scc_ast_decl_union_init(decl, decl_type->record.name, null,
decl_type->base.loc);
} else {
scc_ast_decl_enum_init(decl, type->record.name, null,
decl_type->base.loc);
}
} else {
decl = decl_type->record.decl;
scc_free(decl_type); // FIXME
}
} else if (decl_type->base.type == SCC_AST_TYPE_ENUM) {
if (decl_type->enumeration.decl == null) {
decl = scc_malloc(sizeof(scc_ast_decl_t));
Assert(decl != null);
scc_ast_decl_enum_init(decl, type->enumeration.name, null,
decl_type->base.loc);
} else {
decl = type->enumeration.decl;
scc_free(decl_type); // FIXME
}
} else {
decl = scc_malloc(sizeof(scc_ast_decl_t));
scc_ast_decl_unsafe_val_init(decl, type, null, null,

View File

@@ -23,10 +23,11 @@ static void expr_callback(void *context, scc_ast_node_type_t node_type,
scc_ast_node_t *node =
scc_sema_symtab_lookup_symbol(sema_symtab, expr->identifier.name);
if (node == null) {
SCC_ERROR(expr->base.loc, "Identifier '%s' not found",
SCC_ERROR(expr->base.loc, "sema error: Identifier '%s' not found",
expr->identifier.name);
} else if (!SCC_AST_IS_A(scc_ast_decl_t, node)) {
SCC_ERROR(expr->base.loc, "Identifier '%s' is not a variable",
SCC_ERROR(expr->base.loc,
"sema error: Identifier '%s' is not a variable",
expr->identifier.name);
} else {
expr->identifier._target = SCC_AST_CAST_TO(scc_ast_decl_t, node);
@@ -72,34 +73,71 @@ static void decl_callback(void *context, scc_ast_node_type_t node_type,
scc_ast_type_t *type = scc_malloc(sizeof(scc_ast_type_t));
Assert(type != null);
if (decl->name == null) {
return;
}
if (node_type == SCC_AST_DECL_STRUCT) {
scc_ast_type_struct_init(type, decl->name, decl, decl->base.loc);
// FIXME memory leak
scc_cstring_t name = scc_cstring_from_cstr("$S_");
if (decl->name == null) {
decl->name = "<anonymous struct>";
}
scc_cstring_append_cstr(&name, decl->name, scc_strlen(decl->name));
scc_sema_symtab_add_symbol(sema_symtab, scc_cstring_as_cstr(&name),
&type->base);
} else if (node_type == SCC_AST_DECL_UNION) {
scc_ast_type_union_init(type, decl->name, decl, decl->base.loc);
scc_cstring_t name = scc_cstring_from_cstr("$U_");
if (decl->name == null) {
decl->name = "<anonymous union>";
}
scc_cstring_append_cstr(&name, decl->name, scc_strlen(decl->name));
scc_sema_symtab_add_symbol(sema_symtab, scc_cstring_as_cstr(&name),
&type->base);
} else if (node_type == SCC_AST_DECL_ENUM) {
scc_ast_type_enum_init(type, decl->name, decl, decl->base.loc);
scc_cstring_t name = scc_cstring_from_cstr("$E_");
if (decl->name == null) {
decl->name = "<anonymous enum>";
}
scc_cstring_append_cstr(&name, decl->name, scc_strlen(decl->name));
scc_sema_symtab_add_symbol(sema_symtab, scc_cstring_as_cstr(&name),
&type->base);
scc_vec_foreach(decl->record.fields, i) {
scc_ast_decl_t *enum_decl = scc_vec_at(decl->record.fields, i);
scc_sema_symtab_add_symbol(sema_symtab, enum_decl->name,
&enum_decl->base);
// LOG_INFO("enum added %s", enum_decl->name);
}
} else if (node_type == SCC_AST_DECL_TYPEDEF) {
if (decl->name == null) {
SCC_ERROR(decl->base.loc, "typedef without name");
return;
}
scc_ast_type_typedef_init(type, decl->name, decl, decl->base.loc);
scc_sema_symtab_add_symbol(sema_symtab, decl->name, &type->base);
} else if (node_type == SCC_AST_DECL_VAR) {
scc_sema_symtab_add_symbol(sema_symtab, decl->name, &decl->base);
} else if (node_type == SCC_AST_DECL_PARAM) {
if (decl->name == null) {
if (decl->param.type->base.type == SCC_AST_TYPE_BUILTIN &&
(decl->param.type->builtin.type ==
SCC_AST_BUILTIN_TYPE_VA_LIST ||
decl->param.type->builtin.type == SCC_AST_BUILTIN_TYPE_VOID)) {
return;
}
SCC_ERROR(decl->base.loc, "sema error: Parameter must have a name");
return;
}
scc_sema_symtab_add_symbol(sema_symtab, decl->name, &decl->base);
} else if (node_type == SCC_AST_DECL_FUNC) {
if (decl->name == null) {
SCC_ERROR(decl->base.loc, "sema error: Function must have a name");
} else {
// FIXME 重名函数...
scc_sema_symtab_add_symbol(sema_symtab, decl->name, &decl->base);
}
}
return;
}
@@ -136,6 +174,25 @@ void scc_sema_init(scc_sema_callbacks_t *callbacks) {
scc_pos_create());
scc_sema_symtab_add_symbol(sema_symtab, "__scc_builtin_va_list",
&type->base);
scc_ast_decl_t *decl = scc_malloc(sizeof(scc_ast_decl_t));
scc_ast_decl_val_init(decl, type, "__scc_builtin__", null,
scc_pos_create());
scc_sema_symtab_add_symbol(sema_symtab, "__func__", &decl->base);
scc_ast_type_t *built_func_type = scc_malloc(sizeof(scc_ast_type_t));
scc_ast_type_function_init(built_func_type, null, null, scc_pos_create());
scc_ast_decl_t *builin_func = scc_malloc(sizeof(scc_ast_decl_t));
scc_ast_decl_func_init(builin_func, built_func_type, "__scc_builtin_func",
null, scc_pos_create());
scc_sema_symtab_add_symbol(sema_symtab, "__scc_builtin_va_start",
&builin_func->base);
scc_sema_symtab_add_symbol(sema_symtab, "__scc_builtin_va_end",
&builin_func->base);
scc_sema_symtab_add_symbol(sema_symtab, "__scc_builtin_va_arg",
&builin_func->base);
scc_sema_symtab_add_symbol(sema_symtab, "__scc_builtin_va_copy",
&builin_func->base);
}
void scc_sema_drop(scc_sema_callbacks_t *callbacks) {}

View File

@@ -35,7 +35,7 @@ static scc_ast_node_t *process_input(const char *input,
scc_ring_not_eof(*parser.ring, not_eof);
if (not_eof == true) {
// FIXME MAYBE free
LOG_ERROR("Didn't consume all tokens");
LOG_FATAL("Didn't consume all tokens");
return null;
}

View File

@@ -46,6 +46,7 @@ static void test_builtin_type(void) {
CHECK_BUILTIN_TYPE(SCC_AST_BUILTIN_TYPE_UNSIGNED_CHAR, "unsigned char");
CHECK_BUILTIN_TYPE(SCC_AST_BUILTIN_TYPE_UNSIGNED_SHORT, "unsigned short");
CHECK_BUILTIN_TYPE(SCC_AST_BUILTIN_TYPE_UNSIGNED_INT, "unsigned int");
CHECK_BUILTIN_TYPE(SCC_AST_BUILTIN_TYPE_UNSIGNED_INT, "unsigned");
CHECK_BUILTIN_TYPE(SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG, "unsigned long");
CHECK_BUILTIN_TYPE(SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG, "unsigned long int");
CHECK_BUILTIN_TYPE(SCC_AST_BUILTIN_TYPE_UNSIGNED_LONG_LONG,
@@ -55,6 +56,7 @@ static void test_builtin_type(void) {
CHECK_BUILTIN_TYPE(SCC_AST_BUILTIN_TYPE_SIGNED_CHAR, "signed char");
CHECK_BUILTIN_TYPE(SCC_AST_BUILTIN_TYPE_SIGNED_SHORT, "signed short");
CHECK_BUILTIN_TYPE(SCC_AST_BUILTIN_TYPE_SIGNED_INT, "signed int");
CHECK_BUILTIN_TYPE(SCC_AST_BUILTIN_TYPE_SIGNED_INT, "signed");
CHECK_BUILTIN_TYPE(SCC_AST_BUILTIN_TYPE_SIGNED_LONG, "signed long");
CHECK_BUILTIN_TYPE(SCC_AST_BUILTIN_TYPE_SIGNED_LONG, "signed long int");
CHECK_BUILTIN_TYPE(SCC_AST_BUILTIN_TYPE_SIGNED_LONG_LONG,
@@ -104,7 +106,7 @@ static void test_array_type(void) {
{
// int []
scc_ast_type_t array_unknown_int;
scc_ast_type_array_init(&array_unknown_int, &int_type, NULL,
scc_ast_type_array_init(&array_unknown_int, &int_type, null,
scc_pos_create());
SCC_CHECK_AST(&array_unknown_int.base, "int []", scc_parse_type_name);
}
@@ -157,7 +159,7 @@ static void test_function_type(void) {
{
// int ()
scc_ast_type_t func_type;
scc_ast_type_function_init(&func_type, &int_type, NULL,
scc_ast_type_function_init(&func_type, &int_type, null,
scc_pos_create());
SCC_CHECK_AST(&func_type.base, "int ()", scc_parse_type_name);
}
@@ -166,7 +168,7 @@ static void test_function_type(void) {
{
// int (void)
scc_ast_decl_t void_param;
scc_ast_decl_param_init(&void_param, &void_type, NULL,
scc_ast_decl_param_init(&void_param, &void_type, null,
scc_pos_create());
scc_ast_decl_vec_t params;
@@ -187,8 +189,8 @@ static void test_function_type(void) {
scc_pos_create());
scc_ast_decl_t param_int, param_float;
scc_ast_decl_param_init(&param_int, &int_type, NULL, scc_pos_create());
scc_ast_decl_param_init(&param_float, &float_type, NULL,
scc_ast_decl_param_init(&param_int, &int_type, null, scc_pos_create());
scc_ast_decl_param_init(&param_float, &float_type, null,
scc_pos_create());
scc_ast_decl_vec_t params;
@@ -210,7 +212,7 @@ static void test_function_type(void) {
scc_pos_create());
scc_ast_decl_t param_int, param_var;
scc_ast_decl_param_init(&param_int, &int_type, NULL, scc_pos_create());
scc_ast_decl_param_init(&param_int, &int_type, null, scc_pos_create());
scc_ast_decl_param_init(&param_var, &va_list_type, "...",
scc_pos_create());
@@ -229,7 +231,7 @@ static void test_function_type(void) {
{
// int *()
scc_ast_type_t func_type;
scc_ast_type_function_init(&func_type, &pointer_int_type, NULL,
scc_ast_type_function_init(&func_type, &pointer_int_type, null,
scc_pos_create());
SCC_CHECK_AST(&func_type.base, "int *()", scc_parse_type_name);
}
@@ -238,7 +240,7 @@ static void test_function_type(void) {
{
// int (*)(void)
scc_ast_decl_t void_param;
scc_ast_decl_param_init(&void_param, &void_type, NULL,
scc_ast_decl_param_init(&void_param, &void_type, null,
scc_pos_create());
scc_ast_decl_vec_t params;
@@ -260,7 +262,7 @@ static void test_struct_union_type(void) {
{
// struct S
scc_ast_type_t struct_type;
scc_ast_type_struct_init(&struct_type, "S", NULL, scc_pos_create());
scc_ast_type_struct_init(&struct_type, "S", null, scc_pos_create());
SCC_CHECK_AST(&struct_type.base, "struct S", scc_parse_type_name);
}
@@ -268,7 +270,7 @@ static void test_struct_union_type(void) {
{
// union U
scc_ast_type_t union_type;
scc_ast_type_union_init(&union_type, "U", NULL, scc_pos_create());
scc_ast_type_union_init(&union_type, "U", null, scc_pos_create());
SCC_CHECK_AST(&union_type.base, "union U", scc_parse_type_name);
}
@@ -276,17 +278,17 @@ static void test_struct_union_type(void) {
{
// struct { int x; }
scc_ast_decl_t field_x;
scc_ast_decl_val_init(&field_x, &int_type, "x", NULL, scc_pos_create());
scc_ast_decl_val_init(&field_x, &int_type, "x", null, scc_pos_create());
scc_ast_decl_vec_t fields;
scc_vec_init(fields);
scc_vec_push(fields, &field_x);
scc_ast_decl_t struct_decl;
scc_ast_decl_struct_init(&struct_decl, NULL, &fields, scc_pos_create());
scc_ast_decl_struct_init(&struct_decl, null, &fields, scc_pos_create());
scc_ast_type_t struct_type;
scc_ast_type_struct_init(&struct_type, NULL, &struct_decl,
scc_ast_type_struct_init(&struct_type, null, &struct_decl,
scc_pos_create());
SCC_CHECK_AST(&struct_type.base, "struct { int x; }",
scc_parse_type_name);
@@ -296,7 +298,7 @@ static void test_struct_union_type(void) {
{
// struct S { int x; }
scc_ast_decl_t field_x;
scc_ast_decl_val_init(&field_x, &int_type, "x", NULL, scc_pos_create());
scc_ast_decl_val_init(&field_x, &int_type, "x", null, scc_pos_create());
scc_ast_decl_vec_t fields;
scc_vec_init(fields);
@@ -320,8 +322,8 @@ static void test_struct_union_type(void) {
scc_pos_create());
scc_ast_decl_t field_a, field_b;
scc_ast_decl_val_init(&field_a, &int_type, "a", NULL, scc_pos_create());
scc_ast_decl_val_init(&field_b, &float_type, "b", NULL,
scc_ast_decl_val_init(&field_a, &int_type, "a", null, scc_pos_create());
scc_ast_decl_val_init(&field_b, &float_type, "b", null,
scc_pos_create());
scc_ast_decl_vec_t fields;
@@ -330,10 +332,10 @@ static void test_struct_union_type(void) {
scc_vec_push(fields, &field_b);
scc_ast_decl_t union_decl;
scc_ast_decl_union_init(&union_decl, NULL, &fields, scc_pos_create());
scc_ast_decl_union_init(&union_decl, null, &fields, scc_pos_create());
scc_ast_type_t union_type;
scc_ast_type_union_init(&union_type, NULL, &union_decl,
scc_ast_type_union_init(&union_type, null, &union_decl,
scc_pos_create());
SCC_CHECK_AST(&union_type.base, "union { int a; float b; }",
scc_parse_type_name);
@@ -345,54 +347,61 @@ static void test_enum_type(void) {
{
// enum E
scc_ast_type_t enum_type;
scc_ast_type_enum_init(&enum_type, "E", NULL, scc_pos_create());
scc_ast_type_enum_init(&enum_type, "E", null, scc_pos_create());
SCC_CHECK_AST(&enum_type.base, "enum E", scc_parse_type_name);
}
TEST_CASE("anonymous enum definition");
{
// enum { RED, GREEN, BLUE }
scc_ast_expr_t red, green, blue;
scc_ast_expr_identifier_init(&red, "RED", scc_pos_create());
scc_ast_expr_identifier_init(&green, "GREEN", scc_pos_create());
scc_ast_expr_identifier_init(&blue, "BLUE", scc_pos_create());
scc_ast_type_t enum_type;
scc_ast_expr_vec_t enumerators;
scc_ast_decl_t red, green, blue;
scc_ast_decl_val_init(&red, &enum_type, "RED", null, scc_pos_create());
scc_ast_decl_val_init(&green, &enum_type, "GREEN", null,
scc_pos_create());
scc_ast_decl_val_init(&blue, &enum_type, "BLUE", null,
scc_pos_create());
scc_ast_decl_vec_t enumerators;
scc_vec_init(enumerators);
scc_vec_push(enumerators, &red);
scc_vec_push(enumerators, &green);
scc_vec_push(enumerators, &blue);
scc_ast_decl_t enum_decl;
scc_ast_decl_enum_init(&enum_decl, NULL, &enumerators,
scc_ast_decl_enum_init(&enum_decl, null, &enumerators,
scc_pos_create());
scc_ast_type_t enum_type;
scc_ast_type_enum_init(&enum_type, NULL, &enum_decl, scc_pos_create());
scc_ast_type_enum_init(&enum_type, null, &enum_decl, scc_pos_create());
SCC_CHECK_AST(&enum_type.base, "enum { RED, GREEN, BLUE }",
scc_parse_type_name);
}
TEST_CASE("named enum definition");
{
// enum E { RED, GREEN, BLUE }
scc_ast_expr_t red, green, blue;
scc_ast_expr_identifier_init(&red, "RED", scc_pos_create());
scc_ast_expr_identifier_init(&green, "GREEN", scc_pos_create());
scc_ast_expr_identifier_init(&blue, "BLUE", scc_pos_create());
// enum E { RED, GREEN, BLUE, }
scc_ast_type_t enum_type;
scc_ast_expr_vec_t enumerators;
scc_ast_decl_t red, green, blue;
scc_ast_decl_val_init(&red, &enum_type, "RED", null, scc_pos_create());
scc_ast_decl_val_init(&green, &enum_type, "GREEN", null,
scc_pos_create());
scc_ast_decl_val_init(&blue, &enum_type, "BLUE", null,
scc_pos_create());
scc_ast_decl_vec_t enumerators;
scc_vec_init(enumerators);
scc_vec_push(enumerators, &red);
scc_vec_push(enumerators, &green);
scc_vec_push(enumerators, &blue);
scc_ast_decl_t enum_decl;
scc_ast_decl_enum_init(&enum_decl, "E", &enumerators, scc_pos_create());
scc_ast_decl_enum_init(&enum_decl, null, &enumerators,
scc_pos_create());
scc_ast_type_t enum_type;
scc_ast_type_enum_init(&enum_type, "E", &enum_decl, scc_pos_create());
SCC_CHECK_AST(&enum_type.base, "enum E { RED, GREEN, BLUE }",
SCC_CHECK_AST(&enum_type.base, "enum E { RED, GREEN, BLUE, }",
scc_parse_type_name);
}
}
@@ -476,7 +485,7 @@ static void test_hard_type(void) {
{
// 1) 函数类型 int (void)
scc_ast_decl_t void_param;
scc_ast_decl_param_init(&void_param, &void_type, NULL,
scc_ast_decl_param_init(&void_param, &void_type, null,
scc_pos_create());
scc_ast_decl_vec_t params;
scc_vec_init(params);
@@ -524,7 +533,7 @@ static void test_hard_type(void) {
// 3) 函数类型,返回上述指针,无参数
scc_ast_decl_t void_param;
scc_ast_decl_param_init(&void_param, &void_type, NULL,
scc_ast_decl_param_init(&void_param, &void_type, null,
scc_pos_create());
scc_ast_decl_vec_t params;
scc_vec_init(params);
@@ -547,7 +556,7 @@ static void test_hard_type(void) {
{
// 1) 函数类型 int (void)
scc_ast_decl_t void_param;
scc_ast_decl_param_init(&void_param, &void_type, NULL,
scc_ast_decl_param_init(&void_param, &void_type, null,
scc_pos_create());
scc_ast_decl_vec_t params;
scc_vec_init(params);
@@ -563,7 +572,7 @@ static void test_hard_type(void) {
// 3) 外部函数类型,返回上述指针,无参数
scc_ast_type_t outer_func;
scc_ast_type_function_init(&outer_func, &ptr_to_func, NULL,
scc_ast_type_function_init(&outer_func, &ptr_to_func, null,
scc_pos_create());
SCC_CHECK_AST(&outer_func.base, "int (*())(void)", scc_parse_type_name);
@@ -579,5 +588,5 @@ TEST_LIST = {
{"test_enum_type", test_enum_type},
{"test_specifier_type", test_specifier_type},
{"test_hard_type", test_hard_type},
{NULL, NULL},
{null, null},
};

View File

@@ -0,0 +1,869 @@
void init_func(void);
#define TEST_INIT init_func()
#include "parser_test.h"
static scc_ast_type_t int_type;
static scc_ast_type_t char_type;
static scc_ast_type_t void_type;
static scc_ast_type_t pointer_int_type;
static scc_ast_type_t pointer_char_type;
static scc_ast_type_t pointer_void_type;
static scc_ast_type_t pointer_pointer_int_type;
static scc_ast_type_t va_list_type;
void init_func(void) {
scc_ast_type_builtin_init(&int_type, SCC_AST_BUILTIN_TYPE_INT,
scc_pos_create());
scc_ast_type_builtin_init(&char_type, SCC_AST_BUILTIN_TYPE_CHAR,
scc_pos_create());
scc_ast_type_builtin_init(&void_type, SCC_AST_BUILTIN_TYPE_VOID,
scc_pos_create());
scc_ast_type_pointer_init(&pointer_int_type, &int_type, scc_pos_create());
scc_ast_type_pointer_init(&pointer_char_type, &char_type, scc_pos_create());
scc_ast_type_pointer_init(&pointer_void_type, &void_type, scc_pos_create());
scc_ast_type_pointer_init(&pointer_pointer_int_type, &pointer_int_type,
scc_pos_create());
scc_ast_type_builtin_init(&va_list_type, SCC_AST_BUILTIN_TYPE_VA_LIST,
scc_pos_create());
}
static void test_parser_unit(void) {
// 1. 变量声明 int a;
{
scc_ast_decl_t int_decl;
scc_ast_decl_val_init(&int_decl, &int_type, "a", null,
scc_pos_create());
SCC_CHECK_AST(&int_decl.base, "int a;", scc_parse_declaration);
}
// 2. 函数声明 int main(void) {}
{
// 构造函数类型:返回 int参数为空
scc_ast_type_t func_type;
scc_ast_decl_vec_t func_params;
scc_ast_decl_t void_decl;
scc_ast_decl_param_init(&void_decl, &void_type, null, scc_pos_create());
scc_ast_decl_t *array[] = {&void_decl};
scc_vec_unsafe_from_static_array(func_params, array);
scc_ast_type_function_init(&func_type, &int_type, &func_params,
scc_pos_create());
// 构造复合语句块(空)
scc_ast_stmt_t compound;
scc_ast_stmt_compound_init(&compound, null, scc_pos_create());
// 构造函数声明
scc_ast_decl_t func_decl;
scc_ast_decl_func_init(&func_decl, &func_type, "main", &compound,
scc_pos_create());
SCC_CHECK_AST(&func_decl.base, "int main(void) {}",
scc_parse_declaration);
}
// 3. 翻译单元包含一个函数定义
{
scc_ast_type_t func_type;
scc_ast_decl_vec_t func_params;
scc_ast_decl_t void_decl;
scc_ast_decl_param_init(&void_decl, &void_type, null, scc_pos_create());
scc_ast_decl_t *array[] = {&void_decl};
scc_vec_unsafe_from_static_array(func_params, array);
scc_ast_type_function_init(&func_type, &int_type, &func_params,
scc_pos_create());
scc_ast_stmt_t compound;
scc_ast_stmt_compound_init(&compound, null, scc_pos_create());
scc_ast_decl_t func_decl;
scc_ast_decl_func_init(&func_decl, &func_type, "main", &compound,
scc_pos_create());
// 构造翻译单元
scc_ast_decl_vec_t tu_decls;
scc_vec_init(tu_decls);
scc_vec_push(tu_decls, &func_decl);
scc_ast_translation_unit_t tu;
scc_ast_translation_unit_init(&tu, &tu_decls, scc_pos_create());
SCC_CHECK_AST(&tu.base, "int main(void) {}",
scc_parse_translation_unit);
}
// 4. 带返回语句的函数 int main(void) { return 65536; }
{
// 返回语句中的整数常量
scc_ast_expr_t ret_val;
scc_ast_expr_literal_int_init(&ret_val, "65536", false,
scc_pos_create());
scc_ast_stmt_t ret_stmt;
scc_ast_stmt_return_init(&ret_stmt, &ret_val, scc_pos_create());
// 复合语句包含该返回语句
scc_ast_block_item_vec_t items;
scc_vec_init(items);
scc_vec_push(items, (scc_ast_node_t *)&ret_stmt);
scc_ast_stmt_t compound;
scc_ast_stmt_compound_init(&compound, &items,
scc_pos_create()); // items 被移动
// 函数类型
scc_ast_type_t func_type;
scc_ast_decl_vec_t func_params;
scc_ast_decl_t void_decl;
scc_ast_decl_param_init(&void_decl, &void_type, null, scc_pos_create());
scc_ast_decl_t *array[] = {&void_decl};
scc_vec_unsafe_from_static_array(func_params, array);
scc_ast_type_function_init(&func_type, &int_type, &func_params,
scc_pos_create());
scc_ast_decl_t func_decl;
scc_ast_decl_func_init(&func_decl, &func_type, "main", &compound,
scc_pos_create());
scc_ast_decl_vec_t tu_decls;
scc_vec_init(tu_decls);
scc_vec_push(tu_decls, &func_decl);
scc_ast_translation_unit_t tu;
scc_ast_translation_unit_init(&tu, &tu_decls, scc_pos_create());
SCC_CHECK_AST(&tu.base, "int main(void) { return 65536; }",
scc_parse_translation_unit);
}
// 5. 多语句函数(复杂示例)
{
// 变量声明 int a;
scc_ast_decl_t a_decl;
scc_ast_decl_val_init(&a_decl, &int_type, "a", null, scc_pos_create());
// 变量声明 int b;
scc_ast_decl_t b_decl;
scc_ast_decl_val_init(&b_decl, &int_type, "b", null, scc_pos_create());
// 表达式 1 + 2 * 3
scc_ast_expr_t lit1, lit2, lit3, mul, add;
scc_ast_expr_literal_int_init(&lit1, "1", false, scc_pos_create());
scc_ast_expr_literal_int_init(&lit2, "2", false, scc_pos_create());
scc_ast_expr_literal_int_init(&lit3, "3", false, scc_pos_create());
scc_ast_expr_binary_init(&mul, SCC_AST_OP_MUL, &lit2, &lit3,
scc_pos_create());
scc_ast_expr_binary_init(&add, SCC_AST_OP_ADD, &lit1, &mul,
scc_pos_create());
// 赋值 a = 1 + 2 * 3;
scc_ast_expr_t a_ref1, assign1;
scc_ast_expr_identifier_init(&a_ref1, "a", scc_pos_create());
scc_ast_expr_binary_init(&assign1, SCC_AST_OP_ASSIGN, &a_ref1, &add,
scc_pos_create());
scc_ast_stmt_t assign1_stmt;
scc_ast_stmt_expr_init(&assign1_stmt, &assign1, scc_pos_create());
// 赋值 b = 7;
scc_ast_expr_t lit7;
scc_ast_expr_literal_int_init(&lit7, "7", false, scc_pos_create());
scc_ast_expr_t b_ref1, assign2;
scc_ast_expr_identifier_init(&b_ref1, "b", scc_pos_create());
scc_ast_expr_binary_init(&assign2, SCC_AST_OP_ASSIGN, &b_ref1, &lit7,
scc_pos_create());
scc_ast_stmt_t assign2_stmt;
scc_ast_stmt_expr_init(&assign2_stmt, &assign2, scc_pos_create());
// 表达式 a - b + 1
scc_ast_expr_t a_ref2, b_ref2, sub, add2, lit1_2;
scc_ast_expr_identifier_init(&a_ref2, "a", scc_pos_create());
scc_ast_expr_identifier_init(&b_ref2, "b", scc_pos_create());
scc_ast_expr_binary_init(&sub, SCC_AST_OP_SUB, &a_ref2, &b_ref2,
scc_pos_create());
scc_ast_expr_literal_int_init(&lit1_2, "1", false, scc_pos_create());
scc_ast_expr_binary_init(&add2, SCC_AST_OP_ADD, &sub, &lit1_2,
scc_pos_create());
// 赋值 a = a - b + 1;
scc_ast_expr_t a_ref3, assign3;
scc_ast_expr_identifier_init(&a_ref3, "a", scc_pos_create());
scc_ast_expr_binary_init(&assign3, SCC_AST_OP_ASSIGN, &a_ref3, &add2,
scc_pos_create());
scc_ast_stmt_t assign3_stmt;
scc_ast_stmt_expr_init(&assign3_stmt, &assign3, scc_pos_create());
// return a;
scc_ast_expr_t a_ref4;
scc_ast_expr_identifier_init(&a_ref4, "a", scc_pos_create());
scc_ast_stmt_t ret_stmt;
scc_ast_stmt_return_init(&ret_stmt, &a_ref4, scc_pos_create());
// 复合语句块,按顺序放入
scc_ast_block_item_vec_t items;
scc_vec_init(items);
scc_vec_push(items, (scc_ast_node_t *)&a_decl);
scc_vec_push(items, (scc_ast_node_t *)&b_decl);
scc_vec_push(items, (scc_ast_node_t *)&assign1_stmt);
scc_vec_push(items, (scc_ast_node_t *)&assign2_stmt);
scc_vec_push(items, (scc_ast_node_t *)&assign3_stmt);
scc_vec_push(items, (scc_ast_node_t *)&ret_stmt);
scc_ast_stmt_t compound;
scc_ast_stmt_compound_init(&compound, &items, scc_pos_create());
// 函数类型
scc_ast_type_t func_type;
scc_ast_type_function_init(&func_type, (scc_ast_type_t *)&int_type,
null, scc_pos_create());
scc_ast_decl_t func_decl;
scc_ast_decl_func_init(&func_decl, &func_type, "main", &compound,
scc_pos_create());
scc_ast_decl_vec_t tu_decls;
scc_vec_init(tu_decls);
scc_vec_push(tu_decls, &func_decl);
scc_ast_translation_unit_t tu;
scc_ast_translation_unit_init(&tu, &tu_decls, scc_pos_create());
const char *input = "int main() {\n"
" int a;\n"
" int b;\n"
" a = 1 + 2 * 3;\n"
" b = 7;\n"
" a = a - b + 1;\n"
" return a;\n"
"}\n";
SCC_CHECK_AST(&tu.base, input, scc_parse_translation_unit);
}
{
// 整数字面量 10
scc_ast_expr_t lit10;
scc_ast_expr_literal_int_init(&lit10, "10", false, scc_pos_create());
// 变量声明 int x = 10;
scc_ast_decl_t x_decl;
scc_ast_decl_val_init(&x_decl, (scc_ast_type_t *)&int_type, "x", &lit10,
scc_pos_create());
// 表达式 x + 1
scc_ast_expr_t x_ref1, lit1, add;
scc_ast_expr_identifier_init(&x_ref1, "x", scc_pos_create());
scc_ast_expr_literal_int_init(&lit1, "1", false, scc_pos_create());
scc_ast_expr_binary_init(&add, SCC_AST_OP_ADD, &x_ref1, &lit1,
scc_pos_create());
// 赋值 x = x + 1
scc_ast_expr_t x_ref2, assign;
scc_ast_expr_identifier_init(&x_ref2, "x", scc_pos_create());
scc_ast_expr_binary_init(&assign, SCC_AST_OP_ASSIGN, &x_ref2, &add,
scc_pos_create());
// 表达式语句
scc_ast_stmt_t assign_stmt;
scc_ast_stmt_expr_init(&assign_stmt, &assign, scc_pos_create());
// return x
scc_ast_expr_t x_ref3;
scc_ast_expr_identifier_init(&x_ref3, "x", scc_pos_create());
scc_ast_stmt_t ret_stmt;
scc_ast_stmt_return_init(&ret_stmt, &x_ref3, scc_pos_create());
// 复合语句块
scc_ast_block_item_vec_t items;
scc_vec_init(items);
scc_vec_push(items, (scc_ast_node_t *)&x_decl);
scc_vec_push(items, (scc_ast_node_t *)&assign_stmt);
scc_vec_push(items, (scc_ast_node_t *)&ret_stmt);
scc_ast_stmt_t compound;
scc_ast_stmt_compound_init(&compound, &items, scc_pos_create());
// 函数类型(返回 int无参数
scc_ast_type_t func_type;
scc_ast_type_function_init(&func_type, (scc_ast_type_t *)&int_type,
null,
scc_pos_create()); // null 表示无参数
// 函数声明 int main() { ... }
scc_ast_decl_t func_decl;
scc_ast_decl_func_init(&func_decl, &func_type, "main", &compound,
scc_pos_create());
// 翻译单元
scc_ast_decl_vec_t tu_decls;
scc_vec_init(tu_decls);
scc_vec_push(tu_decls, &func_decl);
scc_ast_translation_unit_t tu;
scc_ast_translation_unit_init(&tu, &tu_decls, scc_pos_create());
// 输入源代码
const char *input = "int main() {\n"
" int x = 10;\n"
" x = x + 1;\n"
" return x;\n"
"}\n";
// 比较解析结果与期望 AST
SCC_CHECK_AST(&tu.base, input, scc_parse_translation_unit);
}
{
scc_ast_decl_vec_t params;
scc_ast_decl_t param0;
scc_ast_decl_param_init(&param0, (scc_ast_type_t *)&int_type, "a",
scc_pos_create());
scc_ast_decl_t param1;
scc_ast_decl_param_init(&param1, (scc_ast_type_t *)&int_type, "b",
scc_pos_create());
scc_ast_decl_t param2;
scc_ast_decl_param_init(&param2, (scc_ast_type_t *)&va_list_type, null,
scc_pos_create());
scc_ast_decl_t *params_array[] = {&param0, &param1, &param2};
scc_vec_unsafe_from_static_array(params, params_array);
scc_ast_type_t decl_func_type;
scc_ast_type_function_init(&decl_func_type, (scc_ast_type_t *)&int_type,
&params, scc_pos_create());
scc_ast_decl_t decl_func;
scc_ast_decl_func_init(&decl_func, &decl_func_type, "add", null,
scc_pos_create());
SCC_CHECK_AST(&decl_func.base, "int add(int a, int b, ...);",
scc_parse_declaration);
}
{
scc_ast_decl_t typedef_decl;
scc_ast_decl_typedef_init(&typedef_decl, "int32_t", &int_type,
scc_pos_create());
scc_ast_type_t typedef_type;
scc_ast_type_typedef_init(&typedef_type, "int32_t", &typedef_decl,
scc_pos_create());
scc_ast_decl_t i32a_decl;
scc_ast_decl_val_init(&i32a_decl, &typedef_type, "a", null,
scc_pos_create());
scc_ast_node_t *array[] = {&typedef_decl.base, &i32a_decl.base};
scc_ast_stmt_t stmt;
scc_ast_block_item_vec_t items;
scc_vec_unsafe_from_static_array(items, array);
scc_ast_stmt_compound_init(&stmt, &items, scc_pos_create());
SCC_CHECK_AST_WITH_SEMA(&stmt.base, "{typedef int int32_t;int32_t a;}",
scc_parse_statement);
scc_ast_type_t void_ptr;
scc_ast_type_pointer_init(&void_ptr, &void_type, scc_pos_create());
scc_ast_decl_t void_ptr_decl;
scc_ast_decl_typedef_init(&void_ptr_decl, "void_ptr", &void_ptr,
scc_pos_create());
scc_ast_type_t void_ptr_type;
scc_ast_type_typedef_init(&void_ptr_type, "void_ptr", &void_ptr_decl,
scc_pos_create());
scc_ast_decl_t void_ptr_a_decl;
scc_ast_decl_val_init(&void_ptr_a_decl, &void_ptr_type, "a", null,
scc_pos_create());
scc_ast_node_t *array2[] = {&void_ptr_decl.base, &void_ptr_a_decl.base};
scc_vec_unsafe_from_static_array(items, array2);
scc_ast_stmt_compound_init(&stmt, &items, scc_pos_create());
SCC_CHECK_AST_WITH_SEMA(&stmt.base,
"{typedef void* void_ptr; void_ptr a;}",
scc_parse_statement);
SCC_CHECK_AST_WITH_SEMA(&void_ptr_decl.base,
"typedef void * void_ptr; ",
scc_parse_declaration);
}
{
// struct { int x; } (匿名结构体定义)
scc_ast_decl_t field;
scc_ast_decl_val_init(&field, (scc_ast_type_t *)&int_type, "x", null,
scc_pos_create());
scc_ast_decl_vec_t fields;
scc_vec_init(fields);
scc_vec_push(fields, &field);
scc_ast_decl_t struct_def;
scc_ast_decl_struct_init(&struct_def, null, &fields, scc_pos_create());
SCC_CHECK_AST(&struct_def.base, "struct { int x;};",
scc_parse_declaration);
scc_ast_type_t struct_type;
scc_ast_type_struct_init(&struct_type, null, &struct_def,
scc_pos_create());
scc_ast_type_t typedef_type;
scc_ast_type_typedef_init(&typedef_type, "struct_t", &struct_def,
scc_pos_create());
scc_ast_decl_t typedef_decl;
scc_ast_decl_typedef_init(&typedef_decl, "struct_t", &struct_type,
scc_pos_create());
SCC_CHECK_AST_WITH_SEMA(&typedef_decl.base,
"typedef struct { int x; } struct_t;",
scc_parse_declaration);
scc_ast_decl_t typedef_impl_decl;
scc_ast_decl_val_init(&typedef_impl_decl, &typedef_type, "a", null,
scc_pos_create());
scc_ast_node_t *array[] = {&typedef_decl.base, &typedef_impl_decl.base};
scc_ast_stmt_t stmt;
scc_ast_block_item_vec_t items;
scc_vec_unsafe_from_static_array(items, array);
scc_ast_stmt_compound_init(&stmt, &items, scc_pos_create());
SCC_CHECK_AST_WITH_SEMA(
&stmt.base, "{typedef struct { int x; } struct_t; struct_t a;}",
scc_parse_statement);
}
{
scc_ast_decl_t type_decl;
scc_ast_decl_typedef_init(&type_decl, "size_t", &int_type,
scc_pos_create());
scc_ast_type_t type_type;
scc_ast_type_typedef_init(&type_type, "size_t", &type_decl,
scc_pos_create());
scc_ast_decl_t param1;
scc_ast_decl_param_init(&param1, &type_type, "a", scc_pos_create());
scc_ast_decl_t param2;
scc_ast_decl_param_init(&param2, &int_type, "b", scc_pos_create());
scc_ast_decl_t param3;
scc_ast_decl_param_init(&param3, &va_list_type, null, scc_pos_create());
scc_ast_decl_t *params_array[] = {&param1, &param2, &param3};
scc_ast_decl_vec_t func_params;
scc_vec_unsafe_from_static_array(func_params, params_array);
scc_ast_type_t func_type;
scc_ast_type_t return_type;
scc_ast_type_pointer_init(&return_type, &void_type, scc_pos_create());
scc_ast_type_function_init(&func_type, &return_type, &func_params,
scc_pos_create());
scc_ast_decl_t func_decl;
scc_ast_decl_func_init(&func_decl, &func_type, "func", null,
scc_pos_create());
scc_ast_decl_t *decls_array[] = {&type_decl, &func_decl};
scc_ast_translation_unit_t tu;
scc_ast_decl_vec_t decls;
scc_vec_unsafe_from_static_array(decls, decls_array);
scc_ast_translation_unit_init(&tu, &decls, scc_pos_create());
SCC_CHECK_AST_WITH_SEMA(&tu.base,
"typedef int size_t;"
"void *func(size_t a, int b, ...);",
scc_parse_translation_unit);
scc_ast_type_t type_func_ptr_type;
scc_ast_type_pointer_init(&type_func_ptr_type, &func_type,
scc_pos_create());
scc_ast_decl_t type_func_ptr_decl;
scc_ast_decl_typedef_init(&type_func_ptr_decl, "func_t",
&type_func_ptr_type, scc_pos_create());
scc_ast_decl_t *decls_array2[] = {&type_decl, &type_func_ptr_decl};
scc_vec_unsafe_from_static_array(decls, decls_array2);
scc_ast_translation_unit_init(&tu, &decls, scc_pos_create());
SCC_CHECK_AST_WITH_SEMA(
&tu.base,
"typedef int size_t;"
"typedef void *(*func_t)(size_t a, int b, ...);",
scc_parse_translation_unit);
}
{
// 1. 构造参数类型volatile const char *restrict并附加 register
// 存储类
scc_ast_type_t char_type;
scc_ast_type_builtin_init(&char_type, SCC_AST_BUILTIN_TYPE_CHAR,
scc_pos_create());
char_type.quals.is_const = true; // const
char_type.quals.is_volatile = true; // volatile
scc_ast_type_t ptr_to_char;
scc_ast_type_pointer_init(&ptr_to_char, &char_type, scc_pos_create());
ptr_to_char.quals.is_restrict = true; // restrict限定指针
ptr_to_char.quals.is_register = true; // register存储类作用于参数
// 2. 参数声明
scc_ast_decl_t param_decl;
scc_ast_decl_param_init(&param_decl, &ptr_to_char, "fmt",
scc_pos_create());
// 3. 返回类型void *
scc_ast_type_t void_type;
scc_ast_type_builtin_init(&void_type, SCC_AST_BUILTIN_TYPE_VOID,
scc_pos_create());
scc_ast_type_t ptr_to_void;
scc_ast_type_pointer_init(&ptr_to_void, &void_type, scc_pos_create());
// 4. 参数列表
scc_ast_decl_vec_t params;
scc_vec_init(params);
scc_vec_push(params, &param_decl);
// 5. 函数类型(包含 static 和 inline
scc_ast_type_t func_type;
scc_ast_type_function_init(&func_type, &ptr_to_void, &params,
scc_pos_create());
func_type.quals.is_static = true;
func_type.quals.is_inline = true;
// 6. 函数声明
scc_ast_decl_t decl;
scc_ast_decl_func_init(&decl, &func_type, "call", null,
scc_pos_create());
// 7. 与解析结果比较
SCC_CHECK_AST_WITH_SEMA(&decl.base,
"static inline void *call(volatile register "
"const char *restrict fmt);",
scc_parse_declaration);
}
{
scc_ast_expr_t lvalue;
scc_ast_expr_lvalue_init(&lvalue, &void_type, scc_pos_create());
scc_ast_expr_t lhs1;
scc_ast_expr_member_init(&lhs1, &lvalue, "data", scc_pos_create());
scc_ast_expr_t lhs2;
scc_ast_expr_member_init(&lhs2, &lvalue, "size", scc_pos_create());
scc_ast_expr_t lhs3;
scc_ast_expr_member_init(&lhs3, &lvalue, "cap", scc_pos_create());
scc_ast_expr_t rl0;
scc_ast_expr_literal_int_init(&rl0, "0", false, scc_pos_create());
scc_ast_type_t void_ptr;
scc_ast_type_pointer_init(&void_ptr, &void_type, scc_pos_create());
scc_ast_expr_t rhs1;
scc_ast_expr_cast_init(&rhs1, &void_ptr, &rl0, scc_pos_create());
scc_ast_expr_t rhs2;
scc_ast_expr_literal_int_init(&rhs2, "0", false, scc_pos_create());
scc_ast_expr_t rhs3;
scc_ast_expr_literal_int_init(&rhs3, "0", false, scc_pos_create());
scc_ast_expr_vec_t lhs_exprs;
scc_ast_expr_t *lhs_array[] = {&lhs1, &lhs2, &lhs3};
scc_vec_unsafe_from_static_array(lhs_exprs, lhs_array);
scc_ast_expr_vec_t rhs_exprs;
scc_ast_expr_t *rhs_array[] = {&rhs1, &rhs2, &rhs3};
scc_vec_unsafe_from_static_array(rhs_exprs, rhs_array);
scc_ast_expr_t expr;
scc_ast_expr_compound_init(&expr, &lvalue, &lhs_exprs, &rhs_exprs,
scc_pos_create());
// FIXME use real records type
SCC_CHECK_AST(&expr.base,
"(void){.data = ((void *)0), .size = 0, .cap = 0}",
scc_parse_expression);
scc_ast_stmt_t stmt;
scc_ast_stmt_return_init(&stmt, &expr, scc_pos_create());
SCC_CHECK_AST(
&stmt.base,
"return (void){.data = (void *)0, .size = 0, .cap = 0 }; ",
scc_parse_statement);
scc_ast_expr_t lhs4;
scc_ast_expr_t lhs5;
scc_ast_expr_member_init(&lhs4, &lvalue, "a", scc_pos_create());
scc_ast_expr_member_init(&lhs5, &lhs4, "b", scc_pos_create());
scc_ast_expr_t lhs6;
scc_ast_expr_t lhs7;
scc_ast_expr_member_init(&lhs6, &lvalue, "c", scc_pos_create());
scc_ast_expr_array_subscript_init(&lhs7, &lhs6, &rl0, scc_pos_create());
scc_ast_expr_t *lhs_array_hard[] = {&lhs5, &lhs7};
scc_vec_unsafe_from_static_array(lhs_exprs, lhs_array_hard);
scc_ast_expr_t *rhs_array_hard[] = {&rhs2, &rhs3};
scc_vec_unsafe_from_static_array(rhs_exprs, rhs_array_hard);
scc_ast_expr_compound_init(&expr, &lvalue, &lhs_exprs, &rhs_exprs,
scc_pos_create());
SCC_CHECK_AST(&expr.base, "(void){.a.b = 0, .c[0] = 0}",
scc_parse_expression);
}
{
// 测试 struct S; 仅标记声明
{
scc_ast_decl_t struct_decl;
scc_ast_decl_vec_t empty_members;
scc_vec_init(empty_members);
scc_ast_decl_struct_init(&struct_decl, "S", &empty_members,
scc_pos_create());
SCC_CHECK_AST(&struct_decl.base, "struct S;",
scc_parse_declaration);
}
// 测试 union U; 仅标记声明
{
scc_ast_decl_t union_decl;
scc_ast_decl_vec_t empty_members;
scc_vec_init(empty_members);
scc_ast_decl_union_init(&union_decl, "U", &empty_members,
scc_pos_create());
SCC_CHECK_AST(&union_decl.base, "union U;", scc_parse_declaration);
}
// 测试 enum E; 仅标记声明
{
scc_ast_decl_t enum_decl;
scc_ast_decl_vec_t empty_enumerators;
scc_vec_init(empty_enumerators);
scc_ast_decl_enum_init(&enum_decl, "E", &empty_enumerators,
scc_pos_create());
SCC_CHECK_AST(&enum_decl.base, "enum E;", scc_parse_declaration);
}
}
{
scc_ast_stmt_t continue_stmt;
scc_ast_stmt_continue_init(&continue_stmt, scc_pos_create());
scc_ast_stmt_t stmt;
scc_ast_stmt_label_init(&stmt, "NEXT", &continue_stmt,
scc_pos_create());
SCC_CHECK_AST(&stmt.base, "NEXT: continue;", scc_parse_statement);
}
{
// 测试 int a = *(int*)b;
{
// 构造类型 int*
scc_ast_type_t ptr_to_int;
scc_ast_type_pointer_init(&ptr_to_int, (scc_ast_type_t *)&int_type,
scc_pos_create());
// 标识符 b
scc_ast_expr_t b_expr;
scc_ast_expr_identifier_init(&b_expr, "b", scc_pos_create());
// 类型转换 (int*)b
scc_ast_expr_t cast_expr;
scc_ast_expr_cast_init(&cast_expr, &ptr_to_int, &b_expr,
scc_pos_create());
// 解引用 *(int*)b
scc_ast_expr_t deref_expr;
scc_ast_expr_unary_init(&deref_expr, SCC_AST_OP_INDIRECTION,
&cast_expr, scc_pos_create());
// 声明 int a = *(int*)b;
scc_ast_decl_t decl;
scc_ast_decl_val_init(&decl, (scc_ast_type_t *)&int_type, "a",
&deref_expr, scc_pos_create());
SCC_CHECK_AST(&decl.base, "int a = *(int*)b;",
scc_parse_declaration);
}
// 测试 int a, b;
{
scc_ast_decl_t decl_a, decl_b;
scc_ast_decl_val_init(&decl_a, (scc_ast_type_t *)&int_type, "a",
null, scc_pos_create());
scc_ast_decl_val_init(&decl_b, (scc_ast_type_t *)&int_type, "b",
null, scc_pos_create());
scc_ast_decl_vec_t decl_vec;
scc_vec_init(decl_vec);
scc_vec_push(decl_vec, &decl_a);
scc_vec_push(decl_vec, &decl_b);
scc_ast_decl_t decl_list;
scc_ast_decl_list_init(&decl_list, &decl_vec,
scc_pos_create()); // 假设存在该函数
SCC_CHECK_AST(&decl_list.base, "int a, b;", scc_parse_declaration);
}
// 测试 int a = 1, b = 2;
{
scc_ast_expr_t lit1, lit2;
scc_ast_expr_literal_int_init(&lit1, "1", false, scc_pos_create());
scc_ast_expr_literal_int_init(&lit2, "2", false, scc_pos_create());
scc_ast_decl_t decl_a, decl_b;
scc_ast_decl_val_init(&decl_a, (scc_ast_type_t *)&int_type, "a",
&lit1, scc_pos_create());
scc_ast_decl_val_init(&decl_b, (scc_ast_type_t *)&int_type, "b",
&lit2, scc_pos_create());
scc_ast_decl_vec_t decl_vec;
scc_vec_init(decl_vec);
scc_vec_push(decl_vec, &decl_a);
scc_vec_push(decl_vec, &decl_b);
scc_ast_decl_t decl_list;
scc_ast_decl_list_init(&decl_list, &decl_vec,
scc_pos_create()); // 假设存在该函数
SCC_CHECK_AST(&decl_list.base, "int a = 1, b = 2;",
scc_parse_declaration);
}
// 测试 "struct list_head *next, *prev;"
{
// 构造 struct list_head 类型(不完整)
scc_ast_type_t struct_list_head;
scc_ast_type_struct_init(&struct_list_head, "list_head", null,
scc_pos_create());
// 构造两个指针类型(分别用于 next 和 prev指向同一结构体
scc_ast_type_t ptr_to_struct1, ptr_to_struct2;
scc_ast_type_pointer_init(&ptr_to_struct1, &struct_list_head,
scc_pos_create());
scc_ast_type_pointer_init(&ptr_to_struct2, &struct_list_head,
scc_pos_create());
// 构造变量声明 next 和 prev
scc_ast_decl_t next_decl, prev_decl;
scc_ast_decl_val_init(&next_decl, &ptr_to_struct1, "next", null,
scc_pos_create());
scc_ast_decl_val_init(&prev_decl, &ptr_to_struct2, "prev", null,
scc_pos_create());
// 构造声明列表
scc_ast_decl_vec_t decl_vec;
scc_vec_init(decl_vec);
scc_vec_push(decl_vec, &next_decl);
scc_vec_push(decl_vec, &prev_decl);
scc_ast_decl_t decl_list;
scc_ast_decl_list_init(&decl_list, &decl_vec, scc_pos_create());
SCC_CHECK_AST(&decl_list.base, "struct list_head *next, *prev;",
scc_parse_declaration);
}
// 测试 "typedef struct { int a; } struct_t, *struct_ptr_t;"
{
// 构造字段 int a;
scc_ast_decl_t field_a;
scc_ast_decl_val_init(&field_a, (scc_ast_type_t *)&int_type, "a",
null, scc_pos_create());
scc_ast_decl_vec_t fields;
scc_vec_init(fields);
scc_vec_push(fields, &field_a);
// 构造匿名结构体定义声明
scc_ast_decl_t struct_def;
scc_ast_decl_struct_init(&struct_def, null, &fields,
scc_pos_create()); // fields 被移动
// 构造匿名结构体类型
scc_ast_type_t anon_struct_type;
scc_ast_type_struct_init(&anon_struct_type, null, &struct_def,
scc_pos_create());
// 构造指针类型指向该匿名结构体
scc_ast_type_t ptr_to_anon;
scc_ast_type_pointer_init(&ptr_to_anon, &anon_struct_type,
scc_pos_create());
// 构造 typedef 声明 struct_t
scc_ast_decl_t typedef_struct_t;
scc_ast_decl_typedef_init(&typedef_struct_t, "struct_t",
&anon_struct_type, scc_pos_create());
// 构造 typedef 声明 struct_ptr_t
scc_ast_decl_t typedef_struct_ptr_t;
scc_ast_decl_typedef_init(&typedef_struct_ptr_t, "struct_ptr_t",
&ptr_to_anon, scc_pos_create());
// 构造声明列表
scc_ast_decl_vec_t typedef_vec;
scc_vec_init(typedef_vec);
scc_vec_push(typedef_vec, &typedef_struct_t);
scc_vec_push(typedef_vec, &typedef_struct_ptr_t);
scc_ast_decl_t decl_list;
scc_ast_decl_list_init(&decl_list, &typedef_vec, scc_pos_create());
SCC_CHECK_AST(&decl_list.base,
"typedef struct { int a; } struct_t, *struct_ptr_t;",
scc_parse_declaration);
}
"__scc_builtin_va_arg(ag, int)";
"__scc_builtin_va_arg(ag, long long)";
"typedef struct a;int a;struct a b;";
}
{
// int (*(*)(void))[5] (指向函数的指针,该函数返回指向数组的指针)
// 步骤:
// 1) 数组类型int [5]
scc_ast_expr_t size_5;
scc_ast_expr_literal_int_init(&size_5, "5", false, scc_pos_create());
scc_ast_type_t array_of_5_int;
scc_ast_type_array_init(&array_of_5_int, (scc_ast_type_t *)&int_type,
&size_5, scc_pos_create());
// 2) 函数类型:返回指向数组的指针,无参数
scc_ast_type_t ptr_to_array;
scc_ast_type_pointer_init(&ptr_to_array, &array_of_5_int,
scc_pos_create());
scc_ast_type_t func_type;
scc_ast_decl_vec_t func_params;
scc_ast_decl_t void_decl;
scc_ast_decl_param_init(&void_decl, &void_type, null, scc_pos_create());
scc_ast_decl_t *array[] = {&void_decl};
scc_vec_unsafe_from_static_array(func_params, array);
scc_ast_type_function_init(&func_type, &ptr_to_array, &func_params,
scc_pos_create()); // 无参数
// 3) 指向该函数的指针
scc_ast_type_t ptr_to_func;
scc_ast_type_pointer_init(&ptr_to_func, &func_type, scc_pos_create());
scc_ast_decl_t ptr_to_func_decl;
scc_ast_decl_val_init(&ptr_to_func_decl, &ptr_to_func, "foo", null,
scc_pos_create());
SCC_CHECK_AST(&ptr_to_func_decl.base, "int (*(*foo)(void))[5];",
scc_parse_declaration);
scc_ast_decl_t typedef_func_decl;
scc_ast_decl_typedef_init(&typedef_func_decl, "func_t", &ptr_to_func,
scc_pos_create());
scc_ast_type_t typedef_func_type;
scc_ast_type_typedef_init(&typedef_func_type, "func_t",
&typedef_func_decl, scc_pos_create());
scc_ast_decl_t func_hard_decl;
scc_ast_type_t func_hard_type;
scc_ast_decl_t param1;
scc_ast_decl_param_init(&param1, &ptr_to_func, "bar", scc_pos_create());
scc_ast_decl_t param2;
scc_ast_decl_param_init(&param2, &typedef_func_type, "a",
scc_pos_create());
scc_ast_decl_t param3;
scc_ast_decl_param_init(&param1, &va_list_type, null, scc_pos_create());
scc_ast_decl_t *func_hard_array[] = {&param1, &param2, &param3};
scc_ast_decl_vec_t func_hard_params;
scc_vec_unsafe_from_static_array(func_hard_params, func_hard_array);
scc_ast_type_function_init(&func_hard_type, &ptr_to_array,
&func_hard_params, scc_pos_create());
scc_ast_decl_func_init(&func_hard_decl, &func_hard_type, "bar", null,
scc_pos_create());
scc_ast_decl_vec_t decls;
scc_ast_decl_t *decls_array[] = {
&typedef_func_decl,
&func_hard_decl,
};
scc_vec_unsafe_from_static_array(decls, decls_array);
scc_ast_translation_unit_t tu;
scc_ast_translation_unit_init(&tu, &decls, scc_pos_create());
// SCC_CHECK_AST_WITH_SEMA(
// &tu.base,
// "typedef int (*(*func_t)(void))[5];"
// "int (*(*(bar)(int(*(*foo)(void))[5], func_t a,
// ...))(void))[5];", scc_parse_translation_unit);
}
}
TEST_LIST = {
{"parser_unit", test_parser_unit},
{null, null},
};

File diff suppressed because it is too large Load Diff

View File

@@ -10,6 +10,10 @@ typedef enum {
SCC_PP_MACRO_NONE, // 不是宏
SCC_PP_MACRO_OBJECT, // 对象宏
SCC_PP_MACRO_FUNCTION, // 函数宏
SCC_PP_MACRO_BUILTIN__FILE__,
SCC_PP_MACRO_BUILTIN__LINE__,
SCC_PP_MACRO_BUILTIN__DATE__, // TODO
SCC_PP_MACRO_BUILTIN__TIME__, // TODO
} scc_pproc_macro_type_t;
typedef SCC_VEC(scc_lexer_tok_vec_t) scc_pproc_macro_extened_params_t;

View File

@@ -658,10 +658,14 @@ void scc_pproc_expand_macro(scc_pproc_expand_t *expand_ctx) {
expand_ctx->need_rescan = true;
expand_ctx->call_pos = tok.loc;
if (macro->type == SCC_PP_MACRO_OBJECT) {
switch (macro->type) {
case SCC_PP_MACRO_OBJECT: {
scc_lexer_tok_drop(&tok);
expand_object_macro(expand_ctx, macro);
} else if (macro->type == SCC_PP_MACRO_FUNCTION) {
break;
}
case SCC_PP_MACRO_FUNCTION: {
scc_lexer_tok_t expect_tok;
scc_ring_peek(*expand_ctx->input, expect_tok, ok);
if (ok == false || expect_tok.type != SCC_TOK_L_PAREN) {
@@ -671,8 +675,30 @@ void scc_pproc_expand_macro(scc_pproc_expand_t *expand_ctx) {
scc_lexer_tok_drop(&tok);
expand_function_macro(expand_ctx, macro);
} else {
break;
}
// FIXME 这可能不符合c语义
case SCC_PP_MACRO_BUILTIN__FILE__:
scc_cstring_free(&tok.lexeme);
scc_cstring_append_ch(&tok.lexeme, '"');
scc_cstring_append_cstr(&tok.lexeme, tok.loc.name,
scc_strlen(tok.loc.name));
scc_cstring_append_ch(&tok.lexeme, '"');
tok.type = SCC_TOK_STRING_LITERAL;
scc_vec_push(expand_ctx->output, tok);
break;
case SCC_PP_MACRO_BUILTIN__LINE__:
scc_cstring_free(&tok.lexeme);
char *buff = scc_malloc(32);
scc_snprintf(buff, 32, "%zu", tok.loc.line);
tok.lexeme = scc_cstring_from_cstr(buff);
scc_free(buff);
tok.type = SCC_TOK_INT_LITERAL;
scc_vec_push(expand_ctx->output, tok);
break;
default:
UNREACHABLE();
break;
}
}
}

View File

@@ -101,6 +101,23 @@ CONTINUE:
goto CONTINUE;
}
void scc_pproc_add_builtin_macros(scc_pproc_macro_table_t *macro_table) {
struct {
const char *name;
scc_pproc_macro_type_t type;
} builin_table[] = {
{"__FILE__", SCC_PP_MACRO_BUILTIN__FILE__},
{"__LINE__", SCC_PP_MACRO_BUILTIN__LINE__},
};
for (usize i = 0; i < SCC_ARRLEN(builin_table); i += 1) {
scc_cstring_t builtin_name =
scc_cstring_from_cstr(builin_table[i].name);
scc_pproc_macro_t *builtin =
scc_pproc_macro_new(&builtin_name, builin_table[i].type);
scc_pproc_macro_table_set(macro_table, builtin);
}
}
void scc_pproc_init(scc_pproc_t *pp, scc_lexer_tok_ring_t *input) {
Assert(pp != null && input != null);
pp->org_ring = input;
@@ -115,10 +132,7 @@ void scc_pproc_init(scc_pproc_t *pp, scc_lexer_tok_ring_t *input) {
pp->config.max_include_depth = 32;
pp->config.keep_original_pos = true;
}
void scc_pproc_add_builtin_macros() {
// TODO
scc_pproc_add_builtin_macros(&pp->macro_table);
}
static cbool fill_token(scc_lexer_tok_t *tok, void *userdata) {

View File

@@ -275,7 +275,6 @@ static inline void sccf_write(const sccf_t *sccf, sccf_buffer_t *buffer) {
scc_vec_size(*buffer) = size;
}
u8 *data = scc_vec_unsafe_get_data(*buffer);
usize offset = 0;
scc_memcpy(data, &sccf->header, sizeof(sccf_header_t));
data += sizeof(sccf_header_t);
scc_memcpy(data, scc_vec_unsafe_get_data(sccf->sect_headers),
@@ -287,7 +286,7 @@ static inline void sccf_write(const sccf_t *sccf, sccf_buffer_t *buffer) {
scc_memcpy(data, scc_vec_unsafe_get_data(*sect_data), sect_data->size);
data += sect_data->size;
}
if (data - scc_vec_unsafe_get_data(*buffer) != size) {
if ((usize)(data - scc_vec_unsafe_get_data(*buffer)) != size) {
Panic();
}
}

View File

@@ -92,8 +92,8 @@ int scc_sstream_init(scc_sstream_t *stream, const char *fname, int ring_size) {
stream->fill_pos = scc_pos_create();
stream->fill_pos.name = fname;
SCC_WARN(stream->fill_pos, "file size is 0");
scc_fclose(file);
return 2;
scc_sstream_init_by_buffer(stream, "", 0, false, ring_size);
return 0;
}
char *buffer = (char *)scc_malloc(fsize);
scc_memset(buffer, 0, fsize);
@@ -101,7 +101,7 @@ int scc_sstream_init(scc_sstream_t *stream, const char *fname, int ring_size) {
Assert(read_ret == fsize); /* read bytes assert it */
scc_fclose(file);
scc_sstream_init_by_buffer(stream, buffer, read_ret, 1, ring_size);
scc_sstream_init_by_buffer(stream, buffer, read_ret, true, ring_size);
stream->fname = fname;
stream->fill_pos.name = stream->fname;
return 0;

View File

@@ -538,19 +538,20 @@ typedef struct _IMAGE_RESOURCE_DIRECTORY {
typedef struct _IMAGE_RESOURCE_DIRECTORY_ENTRY {
union {
struct {
DWORD NameOffset : 31;
DWORD NameIsString : 1;
};
// FIXME scc not supported bit field
// struct {
// DWORD NameOffset : 31;
// DWORD NameIsString : 1;
// };
DWORD Name;
WORD Id;
};
union {
DWORD OffsetToData;
struct {
DWORD OffsetToDirectory : 31;
DWORD DataIsDirectory : 1;
};
// struct {
// DWORD OffsetToDirectory : 31;
// DWORD DataIsDirectory : 1;
// };
};
} IMAGE_RESOURCE_DIRECTORY_ENTRY, *PIMAGE_RESOURCE_DIRECTORY_ENTRY;