feat: in/out 参数 — out 关键字引用传递

fn swap(out x: i64, out y: i64) 声明 out 参数,codegen 层面
函数签名变为 T* 指针,调用点自动传 &variable 地址。
in 是默认行为(值传递),无需显式标注。

Token → Parser → Sema → Codegen 全流水线:
- TOK_OUT + "out" 关键字注册
- AST parameter.is_out 字段
- parse_function 解析 out 前缀
- Sema: out 参数注册为 SYM_VARIABLE+is_mut(可赋值)
- Codegen: LLVM 函数签名使用 T*,调用点传 alloca

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-06-07 14:45:38 +08:00
parent 0a0667776a
commit c8da286d31
14 changed files with 162 additions and 28 deletions
+22
View File
@@ -0,0 +1,22 @@
// out 参数测试 — 引用传递
fn swap(out x: i64, out y: i64) -> void {
let t = x;
x = y;
y = t;
}
fn increment(out x: i64) -> void {
x = x + 1;
}
fn main() -> void {
let a = 10;
let b = 20;
print_i64(a);
print_i64(b);
swap(a, b);
print_i64(a);
print_i64(b);
increment(a);
print_i64(a);
}
+22
View File
@@ -0,0 +1,22 @@
// out 参数 + 结构体测试
struct Point { x: i64, y: i64 }
fn init_point(out p: Point) -> void {
p = Point { x: 100, y: 200 };
}
fn offset_point(out p: Point) -> void {
p = Point { x: p.x + 50, y: p.y + 100 };
}
fn main() -> void {
let p = Point { x: 0, y: 0 };
print_i64(p.x);
print_i64(p.y);
init_point(p);
print_i64(p.x);
print_i64(p.y);
offset_point(p);
print_i64(p.x);
print_i64(p.y);
}
+7 -7
View File
@@ -129,8 +129,8 @@ void test_codegen_struct_decl() {
/* 构造 AST: struct Point { x: i64, y: i64 } */
AstNode* fields[2];
fields[0] = ast_make_parameter(&a, "x", TYPE_I64, NULL, loc_at(1, 1));
fields[1] = ast_make_parameter(&a, "y", TYPE_I64, NULL, loc_at(1, 1));
fields[0] = ast_make_parameter(&a, "x", TYPE_I64, NULL, false, loc_at(1, 1));
fields[1] = ast_make_parameter(&a, "y", TYPE_I64, NULL, false, loc_at(1, 1));
AstNode* struct_decl = ast_make_struct_decl(&a, "Point", fields, 2, loc_at(1, 1));
AstNode* structs[] = { struct_decl };
@@ -185,8 +185,8 @@ void test_codegen_struct_field_access() {
/* 构造 AST: struct Point { x: i64, y: i64 } */
AstNode* fields[2];
fields[0] = ast_make_parameter(&a, "x", TYPE_I64, NULL, loc_at(1, 1));
fields[1] = ast_make_parameter(&a, "y", TYPE_I64, NULL, loc_at(1, 1));
fields[0] = ast_make_parameter(&a, "x", TYPE_I64, NULL, false, loc_at(1, 1));
fields[1] = ast_make_parameter(&a, "y", TYPE_I64, NULL, false, loc_at(1, 1));
AstNode* struct_decl = ast_make_struct_decl(&a, "Point", fields, 2, loc_at(1, 1));
AstNode* structs[] = { struct_decl };
@@ -356,13 +356,13 @@ void test_codegen_method_call() {
/* struct Point { x: i64, y: i64 } */
AstNode* fields[2];
fields[0] = ast_make_parameter(&a, "x", TYPE_I64, NULL, loc_at(1, 1));
fields[1] = ast_make_parameter(&a, "y", TYPE_I64, NULL, loc_at(1, 1));
fields[0] = ast_make_parameter(&a, "x", TYPE_I64, NULL, false, loc_at(1, 1));
fields[1] = ast_make_parameter(&a, "y", TYPE_I64, NULL, false, loc_at(1, 1));
AstNode* struct_decl = ast_make_struct_decl(&a, "Point", fields, 2, loc_at(1, 1));
AstNode* structs[] = { struct_decl };
/* fn Point$get_x(self: Point) -> i64 { return self.x; } */
AstNode* self_param = ast_make_parameter(&a, "self", TYPE_STRUCT, "Point", loc_at(1, 1));
AstNode* self_param = ast_make_parameter(&a, "self", TYPE_STRUCT, "Point", false, loc_at(1, 1));
AstNode* params[] = { self_param };
AstNode* self_ident = ast_make_ident(&a, "self", loc_at(1, 1));
self_ident->type.kind = TYPE_STRUCT;
+36
View File
@@ -418,6 +418,40 @@ void test_match_wildcard_only_sema_ok() {
arena_destroy(&a);
}
void test_out_param_assign_ok() {
Arena a = arena_create(1);
size_t tc; ErrorInfo lex_err = {0};
Token* toks = lex(&a,
"fn swap(out x: i64, out y: i64) -> void { let t = x; x = y; y = t; return; }"
"fn main() -> void { let a = 10; let b = 20; swap(a, b); return; }",
"test", &tc, &lex_err);
ASSERT(toks != NULL);
ErrorInfo parse_err = {0};
AstNode* ast = parse(&a, toks, tc, "test", &parse_err);
ASSERT(ast != NULL);
ErrorList errors; error_init(&errors, &a);
sema_analyze(ast, &errors, &a);
ASSERT(errors.count == 0); // out 参数赋值不应报错
arena_destroy(&a);
}
void test_in_param_assign_error() {
Arena a = arena_create(1);
size_t tc; ErrorInfo lex_err = {0};
Token* toks = lex(&a,
"fn bad(x: i64) -> void { x = 42; return; }"
"fn main() -> void { bad(10); return; }",
"test", &tc, &lex_err);
ASSERT(toks != NULL);
ErrorInfo parse_err = {0};
AstNode* ast = parse(&a, toks, tc, "test", &parse_err);
ASSERT(ast != NULL);
ErrorList errors; error_init(&errors, &a);
sema_analyze(ast, &errors, &a);
ASSERT(errors.count > 0); // 非 out 参数赋值应报错
arena_destroy(&a);
}
int main(void) {
TEST_RUN(test_type_error);
TEST_RUN(test_undefined_var);
@@ -443,5 +477,7 @@ int main(void) {
TEST_RUN(test_match_enum_sema_ok);
TEST_RUN(test_match_int_sema_ok);
TEST_RUN(test_match_wildcard_only_sema_ok);
TEST_RUN(test_out_param_assign_ok);
TEST_RUN(test_in_param_assign_error);
return test_summary();
}