feat(abi): 添加VA_LIST类型支持并完善ABI实现

- 在scc_abi_base_type_kind枚举中添加SCC_ABI_TYPE_VA_LIST类型
- 为Windows x64平台实现VA_LIST类型的ABI规则,遵循MSVC调用约定
- 在AST到ABI类型转换中处理SCC_AST_BUILTIN_TYPE_VA_LIST和BOOL类型

refactor(ast2ir): 实现循环控制流和跳转语句转换

- 添加break和continue缓存表用于语句跳转目标管理
- 实现while、do-while和for循环的正确控制流结构
- 添加对break、continue、goto和label语句的IR转换支持
- 修复复合表达式和声明类型的处理逻辑

refactor(parser): 重构语义分析接口和循环语句解析

- 将语义分析上下文从回调结构改为指针传递
- 为switch、while、do-while和for语句添加BEGIN/END语义标记
- 实现匿名结构体/联合体/枚举的符号命名处理
- 优化结构体/联合体/枚举声明的类型解析逻辑

feat(parser): 实现跳转语句语义分析和符号绑定

- 添加break_stack和continue_stack用于跟踪循环层级
- 实现break和continue语句的目标语句绑定检查
- 支持goto和label语句的标签符号查找和绑定
- 添加对跳转语句位置的有效性验证

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
zzy
2026-04-28 12:25:51 +08:00
parent f6bc40ae4a
commit 85d698acdf
14 changed files with 303 additions and 139 deletions

View File

@@ -15,7 +15,8 @@ typedef struct scc_parser {
usize checkpoint;
scc_ast_ctx_t *ast_ctx;
scc_sema_ctx_t sema_callbacks;
scc_sema_ctx_t *sema_ctx;
int owned_sema;
scc_ast_translation_unit_t *translation_unit;
int errcode;
} scc_parser_t;
@@ -79,38 +80,49 @@ scc_ast_decl_t *scc_parse_declarator(scc_parser_t *parser,
scc_ast_qual_type_t *scc_parse_declaration_specifiers(scc_parser_t *parser);
scc_ast_qual_type_t *scc_parse_type_name(scc_parser_t *parser);
static inline void scc_sema_decl(scc_parser_t *parser, scc_ast_node_kind_t kind,
scc_ast_decl_t *decl) {
parser->sema_ctx->on_decl(parser->sema_ctx, kind, decl);
}
static inline void scc_sema_expr(scc_parser_t *parser, scc_ast_node_kind_t kind,
scc_ast_expr_t *expr) {
parser->sema_ctx->on_expr(parser->sema_ctx, kind, expr);
}
static inline void scc_sema_stmt(scc_parser_t *parser, scc_ast_node_kind_t kind,
scc_ast_stmt_t *stmt) {
parser->sema_ctx->on_stmt(parser->sema_ctx, kind, stmt);
}
static inline void scc_sema_type(scc_parser_t *parser, scc_ast_node_kind_t kind,
scc_ast_qual_type_t *type) {
parser->sema_ctx->on_type(parser->sema_ctx, kind, type);
}
static inline void scc_parse_decl_sema(scc_parser_t *parser,
scc_ast_decl_t *decl) {
parser->sema_callbacks.on_decl(parser->sema_callbacks.context,
decl ? decl->base.type : SCC_AST_UNKNOWN,
decl);
scc_sema_decl(parser, decl ? decl->base.type : SCC_AST_UNKNOWN, decl);
}
static inline void scc_parse_expr_sema(scc_parser_t *parser,
scc_ast_expr_t *expr) {
parser->sema_callbacks.on_expr(parser->sema_callbacks.context,
expr ? expr->base.type : SCC_AST_UNKNOWN,
expr);
scc_sema_expr(parser, expr ? expr->base.type : SCC_AST_UNKNOWN, expr);
}
static inline void scc_parse_stmt_sema(scc_parser_t *parser,
scc_ast_stmt_t *stmt) {
parser->sema_callbacks.on_stmt(parser->sema_callbacks.context,
stmt ? stmt->base.type : SCC_AST_UNKNOWN,
stmt);
scc_sema_stmt(parser, stmt ? stmt->base.type : SCC_AST_UNKNOWN, stmt);
}
static inline void scc_parse_type_sema(scc_parser_t *parser,
scc_ast_qual_type_t *type) {
parser->sema_callbacks.on_type(parser->sema_callbacks.context,
type ? type->base.type : SCC_AST_UNKNOWN,
type);
scc_sema_type(parser, type ? type->base.type : SCC_AST_UNKNOWN, type);
}
static inline const scc_ast_qual_type_t *
scc_parse_got_type(scc_parser_t *parser, const char *name) {
return parser->sema_callbacks.got_type(parser->sema_callbacks.context,
name);
return parser->sema_ctx->got_type(parser->sema_ctx, name);
}
#endif /* __SCC_PARSER_H__ */

View File

@@ -3,27 +3,31 @@
#include <scc_ast.h>
typedef struct scc_sema_ctx scc_sema_ctx_t;
/**
* @brief 语义分析回调函数类型
*/
typedef void (*scc_sema_callback_t)(void *context,
typedef void (*scc_sema_callback_t)(scc_sema_ctx_t *context,
scc_ast_node_kind_t node_type, void *node);
typedef const scc_ast_qual_type_t *(*scc_sema_got_type_t)(void *context,
const char *name);
typedef const scc_ast_qual_type_t *(*scc_sema_got_type_t)(
scc_sema_ctx_t *context, const char *name);
/**
* @brief 语义分析回调集合
*/
typedef struct scc_sema_ctx {
struct scc_sema_ctx {
scc_sema_callback_t on_decl;
scc_sema_callback_t on_stmt;
scc_sema_callback_t on_expr;
scc_sema_callback_t on_type;
scc_sema_got_type_t got_type;
scc_ast_ctx_t *ast_ctx;
scc_ast_stmt_vec_t break_stack;
scc_ast_stmt_vec_t continue_stack;
void *context;
} scc_sema_ctx_t;
};
void scc_sema_init(scc_sema_ctx_t *sema_ctx, scc_ast_ctx_t *ast_ctx);
void scc_sema_drop(scc_sema_ctx_t *sema_ctx);

View File

@@ -290,8 +290,7 @@ CONTINUE:
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, nullptr);
scc_sema_decl(parser, scc_ast_decl_t_BEGIN, nullptr);
// FIXME hack struct
scc_vec_foreach(decl->func.type->type->function.params, i) {
scc_ast_decl_t *param =
@@ -300,8 +299,7 @@ CONTINUE:
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, nullptr);
scc_sema_decl(parser, scc_ast_decl_t_END, nullptr);
Assert(decl->base.type == SCC_AST_DECL_FUNC);
decl->func.body = body;
Assert(decl->func.type != nullptr);

View File

@@ -149,8 +149,7 @@ static scc_ast_stmt_t *parse_compound_statement(scc_parser_t *parser,
scc_ast_block_item_vec_t block_items;
scc_vec_init(block_items);
parser->sema_callbacks.on_stmt(parser->sema_callbacks.context,
scc_ast_stmt_t_BEGIN, nullptr);
scc_sema_stmt(parser, scc_ast_stmt_t_BEGIN, nullptr);
while (!scc_parser_consume_if(parser, SCC_TOK_R_BRACE)) {
/// TODO
// scc_parse_is_decl();
@@ -167,8 +166,7 @@ static scc_ast_stmt_t *parse_compound_statement(scc_parser_t *parser,
}
scc_vec_push(block_items, ret);
}
parser->sema_callbacks.on_stmt(parser->sema_callbacks.context,
scc_ast_stmt_t_END, nullptr);
scc_sema_stmt(parser, scc_ast_stmt_t_END, nullptr);
scc_ast_stmt_t *stmt = SCC_AST_ALLOC_STMT(parser->ast_ctx);
scc_ast_stmt_compound_init(stmt, &block_items, pos);
@@ -200,11 +198,15 @@ static scc_ast_stmt_t *parse_switch_statement(scc_parser_t *parser,
if (!scc_parser_consume_if(parser, SCC_TOK_SWITCH)) {
return nullptr;
}
scc_ast_stmt_t *stmt = SCC_AST_ALLOC_STMT(parser->ast_ctx);
stmt->base.type = SCC_AST_STMT_SWITCH;
scc_ast_expr_t *expression = ast_parse_paren_expression(parser);
scc_ast_stmt_t *statement = scc_parse_statement(parser);
scc_ast_stmt_t *stmt = SCC_AST_ALLOC_STMT(parser->ast_ctx);
scc_sema_stmt(parser, scc_ast_stmt_t_BEGIN, stmt);
scc_ast_stmt_t *statement = scc_parse_statement(parser);
scc_sema_stmt(parser, scc_ast_stmt_t_END, stmt);
scc_ast_stmt_switch_init(stmt, expression, statement, pos);
return stmt;
}
@@ -214,11 +216,15 @@ static scc_ast_stmt_t *parse_while_statement(scc_parser_t *parser,
if (!scc_parser_consume_if(parser, SCC_TOK_WHILE)) {
return nullptr;
}
scc_ast_stmt_t *stmt = SCC_AST_ALLOC_STMT(parser->ast_ctx);
stmt->base.type = SCC_AST_STMT_WHILE;
scc_ast_expr_t *expression = ast_parse_paren_expression(parser);
scc_ast_stmt_t *statement = scc_parse_statement(parser);
scc_ast_stmt_t *stmt = SCC_AST_ALLOC_STMT(parser->ast_ctx);
scc_sema_stmt(parser, scc_ast_stmt_t_BEGIN, stmt);
scc_ast_stmt_t *statement = scc_parse_statement(parser);
scc_sema_stmt(parser, scc_ast_stmt_t_END, stmt);
scc_ast_stmt_while_init(stmt, expression, statement, pos);
return stmt;
}
@@ -228,8 +234,12 @@ static scc_ast_stmt_t *parse_do_while_statement(scc_parser_t *parser,
if (!scc_parser_consume_if(parser, SCC_TOK_DO)) {
return nullptr;
}
scc_ast_stmt_t *stmt = SCC_AST_ALLOC_STMT(parser->ast_ctx);
stmt->base.type = SCC_AST_STMT_DO_WHILE;
scc_sema_stmt(parser, scc_ast_stmt_t_BEGIN, stmt);
scc_ast_stmt_t *statement = scc_parse_statement(parser);
scc_sema_stmt(parser, scc_ast_stmt_t_END, stmt);
if (!scc_parser_consume_if(parser, SCC_TOK_WHILE)) {
SCC_ERROR(scc_parser_got_current_pos(parser),
@@ -243,7 +253,6 @@ static scc_ast_stmt_t *parse_do_while_statement(scc_parser_t *parser,
SCC_ERROR(scc_parser_got_current_pos(parser),
"Expected semicolon after jump statement.");
}
scc_ast_stmt_t *stmt = SCC_AST_ALLOC_STMT(parser->ast_ctx);
scc_ast_stmt_do_while_init(stmt, expression, statement, pos);
return stmt;
}
@@ -263,6 +272,8 @@ static scc_ast_stmt_t *parse_for_statement(scc_parser_t *parser,
SCC_ERROR(scc_parser_got_current_pos(parser),
"Expected '(' before like `( expression )` .");
}
scc_ast_stmt_t *stmt = SCC_AST_ALLOC_STMT(parser->ast_ctx);
stmt->base.type = SCC_AST_STMT_FOR;
scc_ast_node_t *init = nullptr;
scc_ast_expr_t *cond = nullptr;
@@ -293,9 +304,10 @@ static scc_ast_stmt_t *parse_for_statement(scc_parser_t *parser,
"Expected ')' after like `( expression )` .");
}
scc_sema_stmt(parser, scc_ast_stmt_t_BEGIN, stmt);
body = scc_parse_statement(parser);
scc_sema_stmt(parser, scc_ast_stmt_t_END, stmt);
scc_ast_stmt_t *stmt = SCC_AST_ALLOC_STMT(parser->ast_ctx);
scc_ast_stmt_for_init(stmt, init, cond, incr, body, pos);
return stmt;
}

View File

@@ -575,22 +575,32 @@ static scc_ast_qual_type_t *parse_record_type(scc_parser_t *parser,
}
const scc_ast_qual_type_t *type = nullptr;
if (name == nullptr) {
scc_ast_canon_type_t *canon = scc_ast_ctx_alloc_type(parser->ast_ctx);
scc_ast_decl_t *decl = SCC_AST_ALLOC_DECL(parser->ast_ctx);
_scc_ast_decl_record_init(decl, type_kind, name, nullptr, *pos);
const scc_ast_qual_type_t *type =
SCC_AST_ALLOC_QUAL_TYPE(parser->ast_ctx);
_scc_ast_type_record_init(type, canon, type_kind, name, decl, *pos);
return type;
name = "<anonymous>";
}
scc_str_append_cstr(&symbol_name, name, scc_strlen(name));
type = scc_parse_got_type(parser, scc_str_as_cstr(&symbol_name));
if (type == nullptr) {
new_type:
scc_ast_canon_type_t *canon = scc_ast_ctx_alloc_type(parser->ast_ctx);
scc_ast_decl_t *decl = SCC_AST_ALLOC_DECL(parser->ast_ctx);
_scc_ast_decl_record_init(decl, type_kind, name, nullptr, *pos);
scc_ast_node_kind_t decl_kind;
switch (type_kind) {
case SCC_AST_TYPE_ENUM:
decl_kind = SCC_AST_DECL_ENUM;
break;
case SCC_AST_TYPE_STRUCT:
decl_kind = SCC_AST_DECL_STRUCT;
break;
case SCC_AST_TYPE_UNION:
decl_kind = SCC_AST_DECL_UNION;
break;
default:
UNREACHABLE();
break;
}
_scc_ast_decl_record_init(decl, decl_kind, name, nullptr, *pos);
const scc_ast_qual_type_t *qual_type =
SCC_AST_ALLOC_QUAL_TYPE(parser->ast_ctx);
@@ -1264,36 +1274,21 @@ scc_ast_decl_t *scc_parse_declarator(scc_parser_t *parser,
decl_name_tok.loc);
}
if (decl_name_tok.type != SCC_TOK_IDENT) {
// TODO();
// if (decl_type->base.type == SCC_AST_TYPE_STRUCT ||
// decl_type->base.type == SCC_AST_TYPE_UNION ||
// decl_type->base.type == SCC_AST_TYPE_ENUM) {
// if (scc_ast_canon_type(decl_type)->record.decl == nullptr) {
// decl = SCC_AST_ALLOC_DECL(parser->ast_ctx);
// Assert(decl != nullptr);
// if (decl_type->base.type == SCC_AST_TYPE_STRUCT) {
// scc_ast_decl_struct_init(
// decl, scc_ast_canon_type(decl_type)->record.name,
// nullptr, decl_type->base.loc);
// } else if (decl_type->base.type == SCC_AST_TYPE_UNION) {
// scc_ast_decl_union_init(
// decl, scc_ast_canon_type(decl_type)->record.name,
// nullptr, decl_type->base.loc);
// } else {
// scc_ast_decl_enum_init(
// decl, scc_ast_canon_type(decl_type)->record.name,
// nullptr, decl_type->base.loc);
// }
// } else {
// decl = scc_ast_canon_type(decl_type)->record.decl;
// }
// } else {
// decl = SCC_AST_ALLOC_DECL(parser->ast_ctx);
// scc_ast_decl_unsafe_val_init(decl, type, nullptr, nullptr,
// decl_type->base.loc);
// }
}
// 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_ENUM) {
// if (scc_ast_canon_type(decl_type)->record.decl == nullptr) {
// Panic("Expected a struct/union/enum declaration name");
// }
// decl = scc_ast_canon_type(decl_type)->record.decl;
// } else {
// // TODO();
// // decl = SCC_AST_ALLOC_DECL(parser->ast_ctx);
// // scc_ast_decl_unsafe_val_init(decl, type, nullptr, nullptr,
// // decl_type->base.loc);
// }
// }
return decl;
}

View File

@@ -1,16 +1,16 @@
#include <parser_utils.h>
#include <scc_parser.h>
static void dummy_sema_callback(void *context, scc_ast_node_kind_t node_type,
void *node) {
static void dummy_sema_callback(scc_sema_ctx_t *context,
scc_ast_node_kind_t node_type, void *node) {
(void)context;
(void)node_type;
(void)node;
return;
}
static const scc_ast_qual_type_t *dummy_got_type_callback(void *context,
const char *name) {
static const scc_ast_qual_type_t *
dummy_got_type_callback(scc_sema_ctx_t *context, const char *name) {
(void)context;
(void)name;
return nullptr;
@@ -20,7 +20,7 @@ static const scc_ast_qual_type_t *dummy_got_type_callback(void *context,
assigned_val = value ? value : default
void scc_parser_init(scc_parser_t *parser, scc_lexer_tok_ring_t *tok_ring,
scc_ast_ctx_t *ast_ctx, scc_sema_ctx_t *callbacks) {
scc_ast_ctx_t *ast_ctx, scc_sema_ctx_t *sema_ctx) {
Assert(parser != nullptr && tok_ring != nullptr);
Assert(ast_ctx != nullptr);
parser->ast_ctx = ast_ctx;
@@ -28,32 +28,30 @@ void scc_parser_init(scc_parser_t *parser, scc_lexer_tok_ring_t *tok_ring,
parser->ring = tok_ring;
parser->errcode = 0;
parser->translation_unit = nullptr;
if (callbacks) {
ASSIGN_PTR_OR_DEFAULT(parser->sema_callbacks.on_decl,
callbacks->on_decl, dummy_sema_callback);
ASSIGN_PTR_OR_DEFAULT(parser->sema_callbacks.on_stmt,
callbacks->on_stmt, dummy_sema_callback);
ASSIGN_PTR_OR_DEFAULT(parser->sema_callbacks.on_expr,
callbacks->on_expr, dummy_sema_callback);
ASSIGN_PTR_OR_DEFAULT(parser->sema_callbacks.on_type,
callbacks->on_type, dummy_sema_callback);
ASSIGN_PTR_OR_DEFAULT(parser->sema_callbacks.got_type,
callbacks->got_type, dummy_got_type_callback);
parser->sema_callbacks.context = callbacks->context;
if (sema_ctx == nullptr) {
parser->sema_ctx = scc_malloc(sizeof(scc_sema_ctx_t));
Assert(parser->sema_ctx != nullptr);
parser->sema_ctx->on_decl = dummy_sema_callback;
parser->sema_ctx->on_stmt = dummy_sema_callback;
parser->sema_ctx->on_expr = dummy_sema_callback;
parser->sema_ctx->on_type = dummy_sema_callback;
parser->sema_ctx->got_type = dummy_got_type_callback;
parser->sema_ctx->context = nullptr;
parser->owned_sema = true;
} else {
parser->sema_callbacks.on_decl = dummy_sema_callback;
parser->sema_callbacks.on_stmt = dummy_sema_callback;
parser->sema_callbacks.on_expr = dummy_sema_callback;
parser->sema_callbacks.on_type = dummy_sema_callback;
parser->sema_callbacks.got_type = dummy_got_type_callback;
parser->sema_callbacks.context = nullptr;
parser->owned_sema = false;
parser->sema_ctx = sema_ctx;
}
}
void scc_parser_drop(scc_parser_t *parser) {
// TODO: 释放 AST 内存
scc_ring_free(*parser->ring);
scc_sema_drop(&parser->sema_callbacks);
if (parser->owned_sema) {
scc_free(parser->sema_ctx);
} else {
scc_sema_drop(parser->sema_ctx);
}
}
scc_ast_translation_unit_t *scc_parse_translation_unit(scc_parser_t *parser) {

View File

@@ -3,20 +3,20 @@
#include <scc_sema.h>
#include <sema_symtab.h>
static const scc_ast_qual_type_t *got_type_callback(void *context,
static const scc_ast_qual_type_t *got_type_callback(scc_sema_ctx_t *sema_ctx,
const char *name);
static void gen_symbol_name(const scc_ast_decl_t *decl, scc_str_t *name) {
switch (decl->base.type) {
case SCC_AST_TYPE_ENUM:
case SCC_AST_DECL_ENUM:
*name = scc_str_from_cstr("$E_");
scc_str_append_cstr(name, decl->name, scc_strlen(decl->name));
break;
case SCC_AST_TYPE_STRUCT:
case SCC_AST_DECL_STRUCT:
*name = scc_str_from_cstr("$S_");
scc_str_append_cstr(name, decl->name, scc_strlen(decl->name));
break;
case SCC_AST_TYPE_UNION:
case SCC_AST_DECL_UNION:
*name = scc_str_from_cstr("$U_");
scc_str_append_cstr(name, decl->name, scc_strlen(decl->name));
break;
@@ -42,9 +42,9 @@ static void symtab_add_symbol(scc_sema_symtab_t *sema_symtab,
}
}
static void type_callback(void *context, scc_ast_node_kind_t node_type,
void *node) {
scc_sema_symtab_t *sema_symtab = context;
static void type_callback(scc_sema_ctx_t *sema_ctx,
scc_ast_node_kind_t node_type, void *node) {
scc_sema_symtab_t *sema_symtab = sema_ctx->context;
scc_ast_qual_type_t *type = SCC_AST_CAST_TO(scc_ast_qual_type_t, node);
if (node_type == SCC_AST_TYPE_STRUCT) {
@@ -57,9 +57,9 @@ static void type_callback(void *context, scc_ast_node_kind_t node_type,
return;
}
static void expr_callback(void *context, scc_ast_node_kind_t node_type,
void *node) {
scc_sema_symtab_t *sema_symtab = context;
static void expr_callback(scc_sema_ctx_t *sema_ctx,
scc_ast_node_kind_t node_type, void *node) {
scc_sema_symtab_t *sema_symtab = sema_ctx->context;
if (node_type == SCC_AST_UNKNOWN || node == nullptr) {
return;
@@ -83,26 +83,96 @@ static void expr_callback(void *context, scc_ast_node_kind_t node_type,
return;
}
static void stmt_callback(void *context, scc_ast_node_kind_t node_type,
void *node) {
scc_sema_symtab_t *sema_symtab = context;
static void stmt_callback(scc_sema_ctx_t *sema_ctx,
scc_ast_node_kind_t node_type, void *node) {
scc_sema_symtab_t *sema_symtab = sema_ctx->context;
if (node_type == scc_ast_stmt_t_BEGIN) {
scc_sema_symtab_enter_scope(sema_symtab);
if (node == nullptr) {
scc_sema_symtab_enter_scope(sema_symtab);
return;
}
switch (((scc_ast_stmt_t *)node)->base.type) {
case SCC_AST_STMT_WHILE:
case SCC_AST_STMT_DO_WHILE:
case SCC_AST_STMT_FOR:
scc_vec_push(sema_ctx->break_stack, (scc_ast_stmt_t *)node);
scc_vec_push(sema_ctx->continue_stack, (scc_ast_stmt_t *)node);
break;
case SCC_AST_STMT_SWITCH:
scc_vec_push(sema_ctx->break_stack, (scc_ast_stmt_t *)node);
break;
default:
Panic("Unhandled statement type");
break;
}
return;
} else if (node_type == scc_ast_stmt_t_END) {
scc_sema_symtab_leave_scope(sema_symtab);
if (node == nullptr) {
scc_sema_symtab_leave_scope(sema_symtab);
return;
}
switch (((scc_ast_stmt_t *)node)->base.type) {
case SCC_AST_STMT_WHILE:
case SCC_AST_STMT_DO_WHILE:
case SCC_AST_STMT_FOR:
scc_vec_pop(sema_ctx->break_stack);
scc_vec_pop(sema_ctx->continue_stack);
break;
case SCC_AST_STMT_SWITCH:
scc_vec_pop(sema_ctx->break_stack);
break;
default:
Panic("Unhandled statement type");
break;
}
return;
}
if (node_type == SCC_AST_UNKNOWN || node == nullptr) {
return;
}
scc_ast_stmt_t *stmt = SCC_AST_CAST_TO(scc_ast_stmt_t, node);
switch (stmt->base.type) {
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:
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;
}
if (!SCC_AST_IS_A(scc_ast_stmt_t, target_node)) {
SCC_ERROR(stmt->base.loc, "");
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;
default:
break;
}
return;
}
static void decl_callback(void *context, scc_ast_node_kind_t node_type,
void *node) {
scc_sema_symtab_t *sema_symtab = context;
static void decl_callback(scc_sema_ctx_t *sema_ctx,
scc_ast_node_kind_t node_type, void *node) {
scc_sema_symtab_t *sema_symtab = sema_ctx->context;
// Function declaration scope
if (node_type == scc_ast_decl_t_BEGIN) {
@@ -149,9 +219,9 @@ static void decl_callback(void *context, scc_ast_node_kind_t node_type,
return;
}
static const scc_ast_qual_type_t *got_type_callback(void *context,
static const scc_ast_qual_type_t *got_type_callback(scc_sema_ctx_t *sema_ctx,
const char *name) {
scc_sema_symtab_t *sema_symtab = context;
scc_sema_symtab_t *sema_symtab = sema_ctx->context;
scc_ast_node_t *node = scc_sema_symtab_lookup_symbol(sema_symtab, name);
if (SCC_AST_IS_A(scc_ast_qual_type_t, node)) {
return (scc_ast_qual_type_t *)node;
@@ -175,6 +245,9 @@ void scc_sema_init(scc_sema_ctx_t *sema_ctx, scc_ast_ctx_t *ast_ctx) {
scc_sema_symtab_init(sema_symtab);
scc_vec_init(sema_ctx->break_stack);
scc_vec_init(sema_ctx->continue_stack);
// FIXME memory leak
scc_ast_qual_type_t *type = SCC_AST_ALLOC_QUAL_TYPE(sema_ctx->ast_ctx);
@@ -207,6 +280,11 @@ void scc_sema_init(scc_sema_ctx_t *sema_ctx, scc_ast_ctx_t *ast_ctx) {
}
void scc_sema_drop(scc_sema_ctx_t *sema_ctx) {
Assert(scc_vec_size(sema_ctx->break_stack) == 0 &&
scc_vec_size(sema_ctx->continue_stack) == 0);
scc_vec_free(sema_ctx->break_stack);
scc_vec_free(sema_ctx->continue_stack);
// FIXME drop obj
if (sema_ctx->context) {
scc_sema_symtab_drop(sema_ctx->context);