feat(ast2ir): 添加浮点类型支持和复合初始化功能
- 在ABI类型计算中添加FLOAT和DOUBLE类型的映射 - 修复AST操作符注释中的歧义描述 - 为ast2ir上下文添加类型缓存以解决递归结构体定义问题 - 实现复合初始化表达式的支持,包括数组和结构体初始化 - 添加前置和后置自增/自减操作符的IR转换 - 实现三元条件表达式的IR生成 - 添加类型转换(cast)和sizeof操作符的支持 - 重构数组长度推断逻辑并添加类型大小计算函数 - 实现结构体和联合体的递归类型解析 - 添加函数指针调用相关的IR节点类型定义 fix(ast): 修正间接操作符的注释说明 refactor(ast2ir): 优化代码结构并添加必要的断言验证
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#define __SCC_SEMA_H__
|
||||
|
||||
#include <scc_ast.h>
|
||||
#include <scc_utils.h>
|
||||
|
||||
typedef struct scc_sema_ctx scc_sema_ctx_t;
|
||||
/**
|
||||
@@ -13,6 +14,12 @@ typedef void (*scc_sema_callback_t)(scc_sema_ctx_t *context,
|
||||
typedef scc_ast_qual_type_t *(*scc_sema_got_type_t)(scc_sema_ctx_t *context,
|
||||
const char *name);
|
||||
|
||||
typedef struct scc_label_scope {
|
||||
int function_scope_depth;
|
||||
scc_hashtable_t labels; // 标签名 -> scc_ast_stmt_t* (LABEL节点)
|
||||
scc_ast_stmt_vec_t pending_gotos; // 尚未找到目标的goto语句
|
||||
} scc_sema_label_scope_t;
|
||||
|
||||
/**
|
||||
* @brief 语义分析回调集合
|
||||
*/
|
||||
@@ -26,6 +33,7 @@ struct scc_sema_ctx {
|
||||
|
||||
scc_ast_stmt_vec_t break_stack;
|
||||
scc_ast_stmt_vec_t continue_stack;
|
||||
scc_sema_label_scope_t label_scope;
|
||||
void *context;
|
||||
};
|
||||
|
||||
|
||||
@@ -290,7 +290,10 @@ CONTINUE:
|
||||
decl->var.init = scc_parse_initializer(parser, lvalue);
|
||||
} else if (tok_ptr->type == SCC_TOK_L_BRACE) {
|
||||
scc_parse_decl_sema(parser, decl);
|
||||
scc_sema_decl(parser, scc_ast_decl_t_BEGIN, nullptr);
|
||||
|
||||
// 进入函数作用域(用于参数和标签)
|
||||
scc_sema_decl(parser, scc_ast_decl_t_BEGIN, decl);
|
||||
|
||||
// FIXME hack struct
|
||||
scc_vec_foreach(decl->func.type->type->function.params, i) {
|
||||
scc_ast_decl_t *param =
|
||||
@@ -299,7 +302,10 @@ CONTINUE:
|
||||
scc_parse_decl_sema(parser, param);
|
||||
}
|
||||
scc_ast_stmt_t *body = scc_parse_statement(parser);
|
||||
scc_sema_decl(parser, scc_ast_decl_t_END, nullptr);
|
||||
|
||||
// 退出函数作用域
|
||||
scc_sema_decl(parser, scc_ast_decl_t_END, decl);
|
||||
|
||||
Assert(decl->base.type == SCC_AST_DECL_FUNC);
|
||||
decl->func.body = body;
|
||||
Assert(decl->func.type != nullptr);
|
||||
|
||||
@@ -73,6 +73,7 @@ scc_ast_translation_unit_t *scc_parse_translation_unit(scc_parser_t *parser) {
|
||||
if (decl != nullptr) {
|
||||
scc_vec_push(unit->declarations, decl);
|
||||
} else {
|
||||
parser->errcode = 1;
|
||||
break;
|
||||
}
|
||||
if (parser->errcode != 0) { // FIXME errcode
|
||||
|
||||
@@ -133,37 +133,72 @@ static void stmt_callback(scc_sema_ctx_t *sema_ctx,
|
||||
}
|
||||
scc_ast_stmt_t *stmt = SCC_AST_CAST_TO(scc_ast_stmt_t, node);
|
||||
switch (stmt->base.type) {
|
||||
case SCC_AST_STMT_BREAK:
|
||||
case SCC_AST_STMT_BREAK: {
|
||||
if (scc_vec_size(sema_ctx->break_stack) == 0)
|
||||
SCC_ERROR(stmt->base.loc, "break not in loop/switch");
|
||||
else
|
||||
stmt->jump._target = scc_vec_at(
|
||||
sema_ctx->break_stack, scc_vec_size(sema_ctx->break_stack) - 1);
|
||||
break;
|
||||
case SCC_AST_STMT_CONTINUE:
|
||||
} break;
|
||||
case SCC_AST_STMT_CONTINUE: {
|
||||
if (scc_vec_size(sema_ctx->continue_stack) == 0)
|
||||
SCC_ERROR(stmt->base.loc, "continue not in loop");
|
||||
else
|
||||
stmt->jump._target =
|
||||
scc_vec_at(sema_ctx->continue_stack,
|
||||
scc_vec_size(sema_ctx->continue_stack) - 1);
|
||||
case SCC_AST_STMT_GOTO:
|
||||
scc_ast_node_t *target_node =
|
||||
scc_sema_symtab_lookup_symbol(sema_symtab, stmt->goto_stmt.label);
|
||||
if (target_node == nullptr) {
|
||||
SCC_ERROR(stmt->base.loc, "");
|
||||
} break;
|
||||
case SCC_AST_STMT_GOTO: {
|
||||
scc_ast_stmt_t *goto_stmt = SCC_AST_CAST_TO(scc_ast_stmt_t, node);
|
||||
scc_sema_label_scope_t *scope = &sema_ctx->label_scope;
|
||||
if (scope->function_scope_depth != 1) {
|
||||
SCC_ERROR(goto_stmt->base.loc, "goto not inside any function");
|
||||
break;
|
||||
}
|
||||
if (!SCC_AST_IS_A(scc_ast_stmt_t, target_node)) {
|
||||
SCC_ERROR(stmt->base.loc, "");
|
||||
|
||||
// 尝试直接查找标签
|
||||
scc_ast_node_t *target =
|
||||
scc_hashtable_get(&scope->labels, goto_stmt->goto_stmt.label);
|
||||
if (target) {
|
||||
goto_stmt->goto_stmt._target =
|
||||
SCC_AST_CAST_TO(scc_ast_stmt_t, target);
|
||||
} else {
|
||||
// 未找到则加入待处理列表
|
||||
scc_vec_push(scope->pending_gotos, goto_stmt);
|
||||
}
|
||||
} break;
|
||||
case SCC_AST_STMT_LABEL: {
|
||||
scc_ast_stmt_t *label_stmt = SCC_AST_CAST_TO(scc_ast_stmt_t, node);
|
||||
scc_sema_label_scope_t *scope = &sema_ctx->label_scope;
|
||||
// 获取当前函数标签作用域
|
||||
if (scope->function_scope_depth != 1) {
|
||||
SCC_ERROR(label_stmt->base.loc, "label not inside any function");
|
||||
break;
|
||||
}
|
||||
stmt->goto_stmt._target = SCC_AST_CAST_TO(scc_ast_stmt_t, target_node);
|
||||
break;
|
||||
case SCC_AST_STMT_LABEL:
|
||||
scc_sema_symtab_add_symbol(sema_symtab, stmt->label_stmt.label,
|
||||
&stmt->base);
|
||||
break;
|
||||
|
||||
// 检查重复定义
|
||||
if (scc_hashtable_get(&scope->labels, label_stmt->label_stmt.label)) {
|
||||
SCC_ERROR(label_stmt->base.loc, "duplicate label '%s'",
|
||||
label_stmt->label_stmt.label);
|
||||
break;
|
||||
}
|
||||
|
||||
// 添加标签
|
||||
scc_hashtable_set(&scope->labels, label_stmt->label_stmt.label,
|
||||
&label_stmt->base);
|
||||
|
||||
// 检查是否有等待该标签的 goto
|
||||
scc_vec_foreach(scope->pending_gotos, i) {
|
||||
scc_ast_stmt_t *goto_stmt = scc_vec_at(scope->pending_gotos, i);
|
||||
if (goto_stmt == nullptr)
|
||||
continue;
|
||||
if (scc_strcmp(goto_stmt->goto_stmt.label,
|
||||
label_stmt->label_stmt.label) == 0) {
|
||||
goto_stmt->goto_stmt._target = label_stmt;
|
||||
scc_vec_at(scope->pending_gotos, i) = nullptr;
|
||||
}
|
||||
}
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -175,10 +210,37 @@ static void decl_callback(scc_sema_ctx_t *sema_ctx,
|
||||
scc_sema_symtab_t *sema_symtab = sema_ctx->context;
|
||||
|
||||
// Function declaration scope
|
||||
// FIXME 使用 node_type 区分其他未来的scope
|
||||
if (node_type == scc_ast_decl_t_BEGIN) {
|
||||
// 创建标签作用域
|
||||
scc_sema_label_scope_t *scope = &sema_ctx->label_scope;
|
||||
scope->function_scope_depth += 1;
|
||||
scc_hashtable_cstr_init(&scope->labels);
|
||||
scc_vec_init(scope->pending_gotos);
|
||||
|
||||
// 进入普通符号表作用域(用于参数和局部变量)
|
||||
scc_sema_symtab_enter_scope(sema_symtab);
|
||||
return;
|
||||
} else if (node_type == scc_ast_decl_t_END) {
|
||||
scc_sema_label_scope_t *scope = &sema_ctx->label_scope;
|
||||
scope->function_scope_depth -= 1;
|
||||
|
||||
// 处理尚未绑定的 goto
|
||||
scc_vec_foreach(scope->pending_gotos, i) {
|
||||
scc_ast_stmt_t *goto_stmt = scc_vec_at(scope->pending_gotos, i);
|
||||
if (goto_stmt == nullptr) {
|
||||
continue;
|
||||
}
|
||||
// 标签未定义 -> 报错
|
||||
SCC_ERROR(goto_stmt->base.loc, "label '%s' used but not defined",
|
||||
goto_stmt->goto_stmt.label);
|
||||
}
|
||||
|
||||
// 清理
|
||||
scc_hashtable_drop(&scope->labels);
|
||||
scc_vec_free(scope->pending_gotos);
|
||||
|
||||
// 退出普通符号表作用域
|
||||
scc_sema_symtab_leave_scope(sema_symtab);
|
||||
return;
|
||||
}
|
||||
@@ -242,6 +304,7 @@ void scc_sema_init(scc_sema_ctx_t *sema_ctx, scc_ast_ctx_t *ast_ctx) {
|
||||
sema_ctx->on_stmt = stmt_callback;
|
||||
sema_ctx->on_type = type_callback;
|
||||
sema_ctx->got_type = got_type_callback;
|
||||
sema_ctx->label_scope = (scc_sema_label_scope_t){0};
|
||||
|
||||
scc_sema_symtab_init(sema_symtab);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user