feat(ast2ir): 添加数组初始化支持和AST转IR优化

添加了对未知长度数组的自动长度推导功能,支持字符串字面量和复合
初始化的数组长度计算。新增辅助函数resolve_array_length用于计算
数组实际长度,以及emit_array_initialization用于生成数组初始化
代码。

同时将AST转IR过程中的参数改为const引用,提高代码安全性。
新增IR构建器的借用检查机制,防止在借用期间进行重分配操作。

fix(ast): 为AST结构体添加详细注释说明字段用途
This commit is contained in:
zzy
2026-04-13 11:36:52 +08:00
parent 694778e4a0
commit ffb23afaf4
13 changed files with 480 additions and 220 deletions

View File

@@ -10,6 +10,7 @@ typedef struct {
int verbose;
scc_argparse_list_t include_paths;
scc_argparse_list_t define_macros;
const char *entry_point_symbol;
cbool emit_lex;
cbool emit_pp;
cbool emit_ast;
@@ -28,6 +29,8 @@ static void setup_argparse(scc_argparse_t *argparse, scc_config_t *config,
SCC_HINT_TARGET_DESC,
SCC_HINT_VERBOSE,
SCC_HINT_ENTRY_POINT_SYMBOL,
SCC_HINT_EMIT_LEX,
SCC_HINT_EMIT_PP,
SCC_HINT_EMIT_AST,
@@ -44,6 +47,9 @@ static void setup_argparse(scc_argparse_t *argparse, scc_config_t *config,
[SCC_HINT_TARGET_DESC] =
"Target description(eg. x86_64-pc-windows-msvc)",
[SCC_HINT_VERBOSE] = "Increase verbosity (can be used multiple times)",
[SCC_HINT_ENTRY_POINT_SYMBOL] = "Entry point symbol name",
[SCC_HINT_EMIT_LEX] = "Generate lexer sources tokens and exit",
[SCC_HINT_EMIT_PP] = "Generate preprocessed tokens and exit",
[SCC_HINT_EMIT_AST] = "Generate AST and exit",
@@ -58,6 +64,9 @@ static void setup_argparse(scc_argparse_t *argparse, scc_config_t *config,
[SCC_HINT_DEFINED_MACRO] = "定义宏",
[SCC_HINT_TARGET_DESC] = "目标机器描述(eg. x86_64-pc-windows-msvc)",
[SCC_HINT_VERBOSE] = "增加详细输出(可多次使用)",
[SCC_HINT_ENTRY_POINT_SYMBOL] = "入口点符号名称",
[SCC_HINT_EMIT_LEX] = "生成`源代码的词法单元`并退出",
[SCC_HINT_EMIT_PP] = "生成`预处理后的词法单元`并退出",
[SCC_HINT_EMIT_AST] = "生成`抽象语法树`并退出",
@@ -110,6 +119,14 @@ static void setup_argparse(scc_argparse_t *argparse, scc_config_t *config,
scc_argparse_spec_setup_list(&opt_define.spec, &(config->define_macros));
scc_argparse_cmd_add_opt(root, &opt_define);
// --entry-point-symbol (设置入口点符号名称)
scc_argparse_opt_t opt_entry_point_symbol;
scc_argparse_opt_init(&opt_entry_point_symbol, 0, "entry-point-symbol",
scc_hints[SCC_HINT_ENTRY_POINT_SYMBOL]);
scc_argparse_spec_setup_string(&opt_entry_point_symbol.spec,
&(config->entry_point_symbol));
scc_argparse_cmd_add_opt(root, &opt_entry_point_symbol);
// --target
scc_argparse_opt_t opt_target;
scc_argparse_opt_init(&opt_target, 0, "target",

View File

@@ -80,6 +80,7 @@ int main(int argc, const char **argv, const char **envp) {
.input_file = nullptr,
.verbose = 0,
.output_file = nullptr,
.entry_point_symbol = nullptr,
.emit_ast = false,
.emit_ir = false,
.target_description = "x86_64-pc-windows-msvc",
@@ -243,6 +244,8 @@ sstream_drop:
scc_ir2mcode(&ir2mcode_ctx);
scc_ir2mcode_drop(&ir2mcode_ctx);
sccf_builder_set_entry_symbol_name(&sccf_builder,
config.entry_point_symbol);
const sccf_t *sccf = sccf_builder_to_sccf(&sccf_builder);
scc_pe_builder_t pe_builder;
sccf2pe(&pe_builder, sccf);