feat(ast): 更新AST dump功能以使用新的树转储接口

- 将头文件中的tree_dump.h替换为scc_tree_dump.h
- 修改函数签名将scc_tree_dump_ctx_t改为scc_tree_dump_t
- 移除过时的宏定义和内联函数实现
- 使用新的scc_tree_dump_* API替代旧的PRINT_*宏
- 简化类型、表达式、语句和声明的转储逻辑
- 统一使用新的树转储接口进行节点和值的输出

feat(ast2ir): 实现逻辑运算符和一元运算符的IR转换

- 添加scc_ast2ir_logical_expr函数处理&&和||运算符
- 实现短路求值逻辑,包含分支控制流
- 添加对一元正号运算符的支持
- 实现取地址和间接寻址运算符
- 添加字符字面量解析支持转义序列

fix(ir): 修复字符串常量构建中的长度计算错误

- 修正数组长度计算从len+1改为len-1
- 调整字符串内容复制逻辑跳过引号边界
- 修正内存分配大小与实际数据长度匹配

refactor(ir): 更新IR转储模块使用统一的树转储接口

- 将IR转储上下文中的tree_dump_ctx_t替换为scc_tree_dump_t
- 更新初始化函数签名以使用新的转储接口类型
This commit is contained in:
zzy
2026-04-03 20:10:51 +08:00
parent 78e7c800ba
commit ca187c78f1
42 changed files with 1264 additions and 1212 deletions

View File

@@ -204,8 +204,8 @@ scc_ast_expr_t *scc_parse_initializer(scc_parser_t *parser,
scc_parser_next_consume(parser, &tok);
lhs = scc_malloc(sizeof(scc_ast_expr_t));
Assert(lhs != null);
scc_ast_expr_member_init(
lhs, ptr, scc_cstring_as_cstr(&tok.lexeme), tok.loc);
scc_ast_expr_member_init(lhs, ptr, scc_str_as_cstr(&tok.lexeme),
tok.loc);
if (!scc_parser_consume_if(parser, SCC_TOK_ASSIGN)) {
ptr = lhs;
continue;

View File

@@ -728,7 +728,7 @@ static scc_ast_expr_t *parse_postfix_expression(scc_parser_t *parser) {
parser_sync(parser);
return null;
}
const char *name = scc_cstring_as_cstr(&ident_tok.lexeme);
const char *name = scc_str_as_cstr(&ident_tok.lexeme);
scc_ast_expr_t *member = scc_malloc(sizeof(scc_ast_expr_t));
Assert(member != null);
@@ -786,7 +786,7 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
return null;
expr = scc_malloc(sizeof(scc_ast_expr_t));
Assert(expr != null);
scc_ast_expr_identifier_init(expr, scc_cstring_as_cstr(&tok.lexeme),
scc_ast_expr_identifier_init(expr, scc_str_as_cstr(&tok.lexeme),
tok.loc);
break;
}
@@ -795,8 +795,8 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
return null;
expr = scc_malloc(sizeof(scc_ast_expr_t));
Assert(expr != null);
scc_ast_expr_literal_int_init(expr, scc_cstring_as_cstr(&tok.lexeme),
false, tok.loc);
scc_ast_expr_literal_int_init(expr, scc_str_as_cstr(&tok.lexeme), false,
tok.loc);
break;
}
case SCC_TOK_FLOAT_LITERAL: {
@@ -804,7 +804,7 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
return null;
expr = scc_malloc(sizeof(scc_ast_expr_t));
Assert(expr != null);
scc_ast_expr_literal_float_init(expr, scc_cstring_as_cstr(&tok.lexeme),
scc_ast_expr_literal_float_init(expr, scc_str_as_cstr(&tok.lexeme),
false, tok.loc);
break;
}
@@ -813,12 +813,12 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
return null;
expr = scc_malloc(sizeof(scc_ast_expr_t));
Assert(expr != null);
scc_ast_expr_literal_char_init(expr, scc_cstring_as_cstr(&tok.lexeme),
scc_ast_expr_literal_char_init(expr, scc_str_as_cstr(&tok.lexeme),
false, tok.loc);
break;
}
case SCC_TOK_STRING_LITERAL: {
scc_cstring_t string = scc_cstring_create();
scc_str_t string = scc_str_empty();
scc_lexer_tok_t tok;
while (1) {
tok_ptr = scc_parser_peek(parser);
@@ -829,16 +829,16 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
break;
}
scc_parser_next_consume(parser, &tok);
scc_cstring_t tmp = scc_cstring_move(&tok.lexeme);
scc_str_t tmp = scc_str_move(&tok.lexeme);
scc_lexer_tok_drop(&tok);
scc_cstring_append(&string, &tmp);
scc_str_append(&string, &tmp);
}
expr = scc_malloc(sizeof(scc_ast_expr_t));
Assert(expr != null);
// FIXME loc
scc_ast_expr_literal_string_init(expr, scc_cstring_as_cstr(&string),
true, tok.loc);
scc_ast_expr_literal_string_init(expr, scc_str_as_cstr(&string), true,
tok.loc);
break;
}
case SCC_TOK_L_PAREN:

View File

@@ -92,8 +92,7 @@ static scc_ast_stmt_t *parse_label_statement(scc_parser_t *parser,
scc_ast_stmt_t *stmt = ast_stmt_alloc();
Assert(stmt != null);
scc_ast_stmt_label_init(stmt, scc_cstring_as_cstr(&tok.lexeme), statement,
pos);
scc_ast_stmt_label_init(stmt, scc_str_as_cstr(&tok.lexeme), statement, pos);
return stmt;
}
@@ -312,7 +311,7 @@ static scc_ast_stmt_t *parse_jump_statement(scc_parser_t *parser,
if (scc_parser_consume_if(parser, SCC_TOK_GOTO)) {
scc_lexer_tok_t tok = {0};
if (scc_parser_next_consume(parser, &tok)) {
scc_ast_stmt_goto_init(stmt, scc_cstring_as_cstr(&tok.lexeme), pos);
scc_ast_stmt_goto_init(stmt, scc_str_as_cstr(&tok.lexeme), pos);
} else {
SCC_ERROR(scc_parser_got_current_pos(parser),
"Expected label after goto.");

View File

@@ -229,8 +229,8 @@ cbool scc_parse_is_decl_specifier_start(scc_parser_t *parser) {
return true;
// typedef 名称(标识符也可能是类型说明符)
case SCC_TOK_IDENT:
return scc_parse_got_type(
parser, scc_cstring_as_cstr(&tok_ptr->lexeme)) != null;
return scc_parse_got_type(parser, scc_str_as_cstr(&tok_ptr->lexeme)) !=
null;
default:
return false;
}
@@ -260,8 +260,8 @@ cbool scc_parse_is_type_specifier_start(scc_parser_t *parser) {
// typedef 名称
case SCC_TOK_IDENT:
// 需要检查标识符是否在符号表中定义为 typedef
return scc_parse_got_type(
parser, scc_cstring_as_cstr(&tok_ptr->lexeme)) != null;
return scc_parse_got_type(parser, scc_str_as_cstr(&tok_ptr->lexeme)) !=
null;
default:
return false;
}
@@ -585,7 +585,7 @@ static scc_ast_type_t *parse_record_type(scc_parser_t *parser,
if (tok_ptr->type == SCC_TOK_IDENT) {
scc_parser_next_consume(parser, &tok);
name = scc_cstring_as_cstr(&tok.lexeme);
name = scc_str_as_cstr(&tok.lexeme);
tok_ptr = scc_parser_peek(parser);
}
@@ -683,7 +683,7 @@ static scc_ast_type_t *parse_enum_type(scc_parser_t *parser) {
if (tok_ptr->type == SCC_TOK_IDENT) {
scc_parser_next_consume(parser, &tok);
tok_ptr = scc_parser_peek(parser);
name = scc_cstring_as_cstr(&tok.lexeme);
name = scc_str_as_cstr(&tok.lexeme);
}
if (tok_ptr && tok_ptr->type == SCC_TOK_L_BRACE) {
@@ -711,8 +711,8 @@ static scc_ast_type_t *parse_enum_type(scc_parser_t *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_str_as_cstr(&tok.lexeme), enum_item_init,
tok.loc);
scc_vec_push(member, enum_item_decl);
tok_ptr = scc_parser_peek(parser);
@@ -777,11 +777,11 @@ static scc_ast_type_t *parse_type_specifier(scc_parser_t *parser) {
goto done;
case SCC_TOK_IDENT:
info.user_type =
scc_parse_got_type(parser, scc_cstring_as_cstr(&tok_ptr->lexeme));
scc_parse_got_type(parser, scc_str_as_cstr(&tok_ptr->lexeme));
if (info.user_type == null) {
SCC_ERROR(scc_parser_got_current_pos(parser),
"Expected type specifier %s",
scc_cstring_as_cstr(&tok_ptr->lexeme));
scc_str_as_cstr(&tok_ptr->lexeme));
}
scc_parser_next_consume(parser, null);
Assert(info.user_type != null);
@@ -1217,7 +1217,7 @@ scc_ast_decl_t *scc_parse_declarator(scc_parser_t *parser,
// FIXME memory leak
const char *name = decl_name_tok.type == SCC_TOK_IDENT
? scc_cstring_as_cstr(&decl_name_tok.lexeme)
? scc_str_as_cstr(&decl_name_tok.lexeme)
: null;
if (decl_type->base.type == SCC_AST_TYPE_FUNCTION) {

View File

@@ -77,31 +77,31 @@ static void decl_callback(void *context, scc_ast_node_type_t node_type,
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_");
scc_str_t name = scc_str_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),
scc_str_append_cstr(&name, decl->name, scc_strlen(decl->name));
scc_sema_symtab_add_symbol(sema_symtab, scc_str_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_");
scc_str_t name = scc_str_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),
scc_str_append_cstr(&name, decl->name, scc_strlen(decl->name));
scc_sema_symtab_add_symbol(sema_symtab, scc_str_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_");
scc_str_t name = scc_str_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),
scc_str_append_cstr(&name, decl->name, scc_strlen(decl->name));
scc_sema_symtab_add_symbol(sema_symtab, scc_str_as_cstr(&name),
&type->base);
scc_vec_foreach(decl->record.fields, i) {