stage1 大量重构

This commit is contained in:
zzy committed 2026-09-16 20:09:24 +08:00
1 parent e4347cc3ea
commit 2a84b61e3d
18 files changed
+1831 -1354

No files matched your search

+93 -78
View File
@@ -48,7 +48,7 @@ static spl_ir_node_ref_t ast2ir_intrin_field(spl_ast2ir_t *a, spl_ast_node_t *n,
static spl_ir_node_ref_t ast2ir_intrin_len(spl_ast2ir_t *a, spl_ast_node_t *n);
static spl_ir_node_ref_t emit_zero_value(spl_ast2ir_t *a, spl_type_id_t tid) {
spl_type_node_t *tn = spl_type_node(a->sema->type, tid);
spl_type_node_t *tn = spl_type_node(&a->sema->type->ir, tid);
if (!tn)
return 0;
spl_type_node_kind_t tk = tn->kind;
@@ -142,12 +142,12 @@ static spl_ir_node_ref_t ast2ir_intrin_cast(spl_ast2ir_t *a, spl_ast_node_t *n,
spl_ir_node_ref_t v = emit_expr(a, vec_at(*args, 0));
if (!v)
return 0;
spl_ast_node_t *src = spl_ast_node(a->sema->ast, vec_at(*args, 0));
spl_ast_node_t *src = spl_ast_node(a->ast, vec_at(*args, 0));
spl_type_id_t ft = node_ir(a, src);
spl_type_id_t tt = 0;
if (nargs >= 2) {
spl_ast_node_t *targ = spl_ast_node(a->sema->ast, vec_at(*args, 1));
spl_ast_node_t *targ = spl_ast_node(a->ast, vec_at(*args, 1));
tt = node_ir(a, targ);
} else {
tt = node_ir(a, n);
@@ -161,11 +161,11 @@ static spl_ir_node_ref_t ast2ir_intrin_call(spl_ast2ir_t *a, spl_ast_node_t *n,
spl_ast_node_ref_vec_t *args) {
switch (in) {
case SPL_INTRIN_SIZEOF: {
spl_ast_node_t *arg = spl_ast_node(a->sema->ast, vec_at(*args, 0));
spl_ast_node_t *arg = spl_ast_node(a->ast, vec_at(*args, 0));
return spl_ir_builder_type_sizeof(a->ir, node_ir(a, arg));
}
case SPL_INTRIN_ALIGNOF: {
spl_ast_node_t *arg = spl_ast_node(a->sema->ast, vec_at(*args, 0));
spl_ast_node_t *arg = spl_ast_node(a->ast, vec_at(*args, 0));
return spl_ir_builder_type_alignof(a->ir, node_ir(a, arg));
}
case SPL_INTRIN_ASSERT: {
@@ -183,7 +183,7 @@ static spl_ir_node_ref_t ast2ir_intrin_call(spl_ast2ir_t *a, spl_ast_node_t *n,
const char *fname = n->dbg.fname ? n->dbg.fname : "?";
const char *user_msg = NULL;
if (vec_size(*args) == 2) {
spl_ast_node_t *mn = spl_ast_node(a->sema->ast, vec_at(*args, 1));
spl_ast_node_t *mn = spl_ast_node(a->ast, vec_at(*args, 1));
if (mn && mn->kind == SPL_AST_EXPR_STRING_LIT && mn->string_lit.data)
user_msg = mn->string_lit.data;
}
@@ -241,7 +241,7 @@ static spl_ir_node_ref_t ast2ir_intrin_field(spl_ast2ir_t *a, spl_ast_node_t *n,
}
static spl_ir_node_ref_t ast2ir_intrin_len(spl_ast2ir_t *a, spl_ast_node_t *n) {
spl_ast_node_t *obj = spl_ast_node(a->sema->ast, n->field.obj);
spl_ast_node_t *obj = spl_ast_node(a->ast, n->field.obj);
spl_lang_type_id_t recv_lang = node_lang(a, obj);
spl_lang_type_node_t *recv = spl_lang_type_node(a->sema->type, recv_lang);
if (!recv)
@@ -252,7 +252,7 @@ static spl_ir_node_ref_t ast2ir_intrin_len(spl_ast2ir_t *a, spl_ast_node_t *n) {
switch (recv->kind) {
case SPL_LANG_TYPE_ARRAY: {
spl_type_node_t *arr = spl_type_node(a->sema->type, recv->ir_type_id);
spl_type_node_t *arr = spl_type_node(&a->sema->type->ir, recv->ir_type_id);
if (!arr)
return 0;
return spl_ir_builder_type_const_int(a->ir, usize_ir, arr->array_type.len);
@@ -270,7 +270,7 @@ static spl_ir_node_ref_t ast2ir_intrin_len(spl_ast2ir_t *a, spl_ast_node_t *n) {
}
static spl_ir_node_ref_t emit_lvalue(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
spl_ast_node_t *n = spl_ast_node(a->sema->ast, ref);
spl_ast_node_t *n = spl_ast_node(a->ast, ref);
if (!n)
return 0;
@@ -290,7 +290,7 @@ static spl_ir_node_ref_t emit_lvalue(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
return emit_expr(a, n->unary.operand);
}
case SPL_AST_EXPR_FIELD: {
spl_ast_node_t *obj = spl_ast_node(a->sema->ast, n->field.obj);
spl_ast_node_t *obj = spl_ast_node(a->ast, n->field.obj);
if (!obj)
return 0;
spl_lang_type_id_t obj_lang = node_lang(a, obj);
@@ -320,7 +320,7 @@ static spl_ir_node_ref_t emit_lvalue(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
return 0;
usize idx = 0;
if (!spl_type_agg_member_idx(a->sema->type, agg_ir, n->field.field, &idx))
if (!spl_type_agg_member_idx(&a->sema->type->ir, agg_ir, n->field.field, &idx))
return 0;
spl_lang_type_node_t *agg_ln = spl_lang_type_node(a->sema->type, agg_lang);
@@ -330,7 +330,7 @@ static spl_ir_node_ref_t emit_lvalue(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
return spl_ir_builder_mem_field(a->ir, agg_ir, base, idx);
}
case SPL_AST_EXPR_INDEX: {
spl_ast_node_t *obj = spl_ast_node(a->sema->ast, n->index.obj);
spl_ast_node_t *obj = spl_ast_node(a->ast, n->index.obj);
if (!obj)
return 0;
spl_lang_type_id_t obj_lang = node_lang(a, obj);
@@ -345,7 +345,7 @@ static spl_ir_node_ref_t emit_lvalue(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
obj_elem = obj_ln->extra.elem;
if (obj_kind == SPL_LANG_TYPE_TUPLE) {
spl_ast_node_t *idx_node = spl_ast_node(a->sema->ast, n->index.index);
spl_ast_node_t *idx_node = spl_ast_node(a->ast, n->index.index);
if (!idx_node || idx_node->kind != SPL_AST_EXPR_INT_LIT)
return 0;
usize i = (usize)idx_node->int_lit.bits;
@@ -370,7 +370,8 @@ static spl_ir_node_ref_t emit_lvalue(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
spl_ir_node_ref_t slice_val = emit_expr(a, n->index.obj);
if (!slice_val)
return 0;
spl_type_id_t ptr_ir = spl_type_builder_ptr(a->sema->type, to_ir_type(a, obj_elem));
spl_type_id_t ptr_ir =
spl_type_builder_ptr(&a->sema->type->ir, to_ir_type(a, obj_elem));
spl_ir_node_ref_t ptr = spl_ir_builder_agg_extract(a->ir, ptr_ir, ptr_ir,
SPL_SLICE_PTR_FIELD_IDX, slice_val);
base = ptr;
@@ -400,7 +401,7 @@ static spl_ir_node_ref_t ast2ir_emit_cast_builtin(spl_ast2ir_t *a, spl_ast_node_
spl_type_id_t tt, spl_ir_node_ref_t v) {
switch (in) {
case SPL_INTRIN_INT_CAST: {
spl_type_node_t *fs = spl_type_node(a->sema->type, ft);
spl_type_node_t *fs = spl_type_node(&a->sema->type->ir, ft);
if (fs && fs->int_type.is_signed)
return spl_ir_builder_cast_sext(a->ir, ft, tt, v);
return spl_ir_builder_cast_zext(a->ir, ft, tt, v);
@@ -410,8 +411,8 @@ static spl_ir_node_ref_t ast2ir_emit_cast_builtin(spl_ast2ir_t *a, spl_ast_node_
case SPL_INTRIN_BIT_CAST:
return spl_ir_builder_cast_bitcast(a->ir, ft, tt, v);
case SPL_INTRIN_FLOAT_CAST: {
spl_type_node_t *fs = spl_type_node(a->sema->type, ft);
spl_type_node_t *ts = spl_type_node(a->sema->type, tt);
spl_type_node_t *fs = spl_type_node(&a->sema->type->ir, ft);
spl_type_node_t *ts = spl_type_node(&a->sema->type->ir, tt);
if (fs && ts && fs->float_type.bits > ts->float_type.bits)
return spl_ir_builder_cast_ftrunc(a->ir, ft, tt, v);
return spl_ir_builder_cast_fext(a->ir, ft, tt, v);
@@ -438,14 +439,14 @@ static spl_ir_node_ref_t emit_cast(spl_ast2ir_t *a, spl_ast_node_t *n) {
spl_ir_node_ref_t v = emit_expr_raw(a, n->cast.expr);
if (!v)
return 0;
spl_ast_node_t *src = spl_ast_node(a->sema->ast, n->cast.expr);
spl_ast_node_t *dst = spl_ast_node(a->sema->ast, n->cast.type);
spl_ast_node_t *src = spl_ast_node(a->ast, n->cast.expr);
spl_ast_node_t *dst = spl_ast_node(a->ast, n->cast.type);
spl_type_id_t ft = node_ir(a, src);
spl_type_id_t tt = node_ir(a, dst);
if (!ft || !tt)
return 0;
spl_intrin_t in = spl_intrin_lookup_cast(a->sema->type, ft, tt);
spl_intrin_t in = spl_intrin_lookup_cast(&a->sema->type->ir, ft, tt);
if (in == SPL_INTRIN_NONE) {
spl_diag_error(a->diag, SPL_DIAG_KIND_CAST_INVALID, &n->dbg,
"as: no conversion between the given types");
@@ -459,7 +460,7 @@ static spl_ir_node_ref_t emit_expr(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
if (!val)
return 0;
spl_ast_node_t *n = spl_ast_node(a->sema->ast, ref);
spl_ast_node_t *n = spl_ast_node(a->ast, ref);
if (!n || n->expected_lang_tid == 0)
return val;
@@ -479,14 +480,14 @@ static spl_ir_node_ref_t emit_expr(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
}
static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
spl_ast_node_t *n = spl_ast_node(a->sema->ast, ref);
spl_ast_node_t *n = spl_ast_node(a->ast, ref);
if (!n)
return 0;
switch (n->kind) {
case SPL_AST_EXPR_INT_LIT: {
spl_type_id_t it = node_ir(a, n);
spl_type_node_t *itn = spl_type_node(a->sema->type, it);
spl_type_node_t *itn = spl_type_node(&a->sema->type->ir, it);
if (itn && itn->kind == SPL_TYPE_FLOAT) {
double d = (double)(int64_t)n->int_lit.bits;
return spl_ir_builder_type_const_float(a->ir, it, d);
@@ -501,7 +502,7 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref)
usize len = n->string_lit.len;
spl_lang_type_id_t u8_lang = spl_lang_make_int(a->sema->type, 8, 0);
spl_type_id_t u8_ir = to_ir_type(a, u8_lang);
spl_type_id_t ptr_u8_ir = spl_type_builder_ptr(a->sema->type, u8_ir);
spl_type_id_t ptr_u8_ir = spl_type_builder_ptr(&a->sema->type->ir, u8_ir);
spl_lang_type_id_t usize_lang = spl_lang_make_int(a->sema->type, sizeof(void *) * 8, 0);
spl_type_id_t usize_ir = to_ir_type(a, usize_lang);
spl_lang_type_id_t arr_lang = spl_lang_make_array(a->sema->type, u8_lang, len);
@@ -567,7 +568,7 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref)
spl_ir_node_ref_t v = emit_expr(a, n->binary.right);
if (!ptr || !v)
return 0;
spl_ast_node_t *ln = spl_ast_node(a->sema->ast, n->binary.left);
spl_ast_node_t *ln = spl_ast_node(a->ast, n->binary.left);
spl_type_id_t t = node_ir(a, ln);
spl_ir_builder_mem_store(a->ir, t, ptr, v);
return v;
@@ -576,7 +577,7 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref)
spl_ir_node_ref_t ptr = emit_lvalue(a, n->binary.left);
if (!ptr)
return 0;
spl_ast_node_t *ln = spl_ast_node(a->sema->ast, n->binary.left);
spl_ast_node_t *ln = spl_ast_node(a->ast, n->binary.left);
spl_type_id_t t = node_ir(a, ln);
spl_ir_node_ref_t lv = spl_ir_builder_mem_load(a->ir, t, ptr);
spl_ir_node_ref_t rv = emit_expr(a, n->binary.right);
@@ -627,14 +628,14 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref)
if (!l || !r)
return 0;
spl_ast_node_t *ln = spl_ast_node(a->sema->ast, n->binary.left);
spl_ast_node_t *ln = spl_ast_node(a->ast, n->binary.left);
spl_lang_type_id_t lt = node_lang(a, ln);
spl_lang_type_node_t *lln = spl_lang_type_node(a->sema->type, lt);
if (lln && lln->kind == SPL_LANG_TYPE_PTR && (op == SPL_OP_ADD || op == SPL_OP_SUB)) {
spl_lang_type_id_t elem_lang = lln->extra.elem;
spl_type_id_t elem_ir = to_ir_type(a, elem_lang);
spl_type_id_t rt_ir = node_ir(a, spl_ast_node(a->sema->ast, n->binary.right));
spl_type_id_t rt_ir = node_ir(a, spl_ast_node(a->ast, n->binary.right));
spl_ir_node_ref_t off = r;
if (op == SPL_OP_SUB)
@@ -671,7 +672,7 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref)
case SPL_OP_CMP_LE:
case SPL_OP_CMP_GT:
case SPL_OP_CMP_GE: {
spl_ast_node_t *cmln = spl_ast_node(a->sema->ast, n->binary.left);
spl_ast_node_t *cmln = spl_ast_node(a->ast, n->binary.left);
spl_type_id_t op_tid = cmln ? node_ir(a, cmln) : 0;
switch (op) {
case SPL_OP_CMP_EQ:
@@ -691,12 +692,12 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref)
}
}
case SPL_OP_BOOL_OR: {
spl_ast_node_t *bmln = spl_ast_node(a->sema->ast, n->binary.left);
spl_ast_node_t *bmln = spl_ast_node(a->ast, n->binary.left);
spl_type_id_t op_tid = bmln ? node_ir(a, bmln) : 0;
return spl_ir_builder_arith_or(a->ir, op_tid, l, r);
}
case SPL_OP_BOOL_AND: {
spl_ast_node_t *bmln = spl_ast_node(a->sema->ast, n->binary.left);
spl_ast_node_t *bmln = spl_ast_node(a->ast, n->binary.left);
spl_type_id_t op_tid = bmln ? node_ir(a, bmln) : 0;
return spl_ir_builder_arith_and(a->ir, op_tid, l, r);
}
@@ -705,7 +706,7 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref)
}
}
case SPL_AST_EXPR_CALL: {
spl_ast_node_t *callee = spl_ast_node(a->sema->ast, n->call.callee);
spl_ast_node_t *callee = spl_ast_node(a->ast, n->call.callee);
if (callee && callee->kind == SPL_AST_EXPR_IDENT) {
spl_intrin_t in = spl_intrin_lookup_call(callee->ident.name);
if (in != SPL_INTRIN_NONE)
@@ -736,7 +737,7 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref)
return call;
}
case SPL_AST_EXPR_FIELD: {
spl_ast_node_t *obj = spl_ast_node(a->sema->ast, n->field.obj);
spl_ast_node_t *obj = spl_ast_node(a->ast, n->field.obj);
spl_lang_type_id_t obj_lang = node_lang(a, obj);
spl_lang_type_node_t *obj_ln = spl_lang_type_node(a->sema->type, obj_lang);
if (obj_ln) {
@@ -823,7 +824,7 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref)
if (!t)
Panic("aggregate literal has no type after sema");
spl_type_node_t *tn = spl_type_node(a->sema->type, t);
spl_type_node_t *tn = spl_type_node(&a->sema->type->ir, t);
if (!tn)
Panic("aggregate type node missing");
@@ -832,13 +833,12 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref)
if (tn_kind == SPL_TYPE_ENUM) {
if (vec_size(n->aggregate_literal.fields) != 1)
Panic("enum literal must have exactly one variant");
spl_ast_node_t *item =
spl_ast_node(a->sema->ast, vec_at(n->aggregate_literal.fields, 0));
spl_ast_node_t *item = spl_ast_node(a->ast, vec_at(n->aggregate_literal.fields, 0));
if (!item || item->kind != SPL_AST_STRUCT_INIT_ITEM)
Panic("invalid enum literal");
usize vidx = 0;
if (!spl_type_agg_member_idx(a->sema->type, t, item->struct_init_item.name, &vidx))
if (!spl_type_agg_member_idx(&a->sema->type->ir, t, item->struct_init_item.name, &vidx))
Panic("enum variant `%s` not found (sema should have caught this)",
item->struct_init_item.name);
@@ -873,13 +873,12 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref)
vec_push(fields, 0);
for (usize i = 0; i < vec_size(n->aggregate_literal.fields); ++i) {
spl_ast_node_t *item =
spl_ast_node(a->sema->ast, vec_at(n->aggregate_literal.fields, i));
spl_ast_node_t *item = spl_ast_node(a->ast, vec_at(n->aggregate_literal.fields, i));
if (!item || item->kind != SPL_AST_STRUCT_INIT_ITEM)
continue;
usize idx = 0;
if (!spl_type_agg_member_idx(a->sema->type, t, item->struct_init_item.name, &idx))
if (!spl_type_agg_member_idx(&a->sema->type->ir, t, item->struct_init_item.name, &idx))
Panic("aggregate field `%s` not found (sema should have caught this)",
item->struct_init_item.name);
@@ -936,7 +935,7 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref)
case SPL_AST_EXPR_LOOP:
return emit_loop_like(a, ref);
case SPL_AST_EXPR_SLICE: {
spl_ast_node_t *obj = spl_ast_node(a->sema->ast, n->slice.obj);
spl_ast_node_t *obj = spl_ast_node(a->ast, n->slice.obj);
spl_lang_type_id_t obj_lang = node_lang(a, obj);
spl_lang_type_node_t *obj_ln = spl_lang_type_node(a->sema->type, obj_lang);
if (!obj_ln)
@@ -962,7 +961,7 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref)
spl_ir_node_ref_t slice_val = emit_expr(a, n->slice.obj);
if (!slice_val)
return 0;
spl_type_id_t ptr_ir = spl_type_builder_ptr((spl_type_t *)a->sema->type, elem_ir);
spl_type_id_t ptr_ir = spl_type_builder_ptr(&a->sema->type->ir, elem_ir);
base = spl_ir_builder_agg_extract(a->ir, obj_ir, ptr_ir, SPL_SLICE_PTR_FIELD_IDX,
slice_val);
} else {
@@ -987,7 +986,7 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref)
if (n->slice.end) {
end_v = emit_expr(a, n->slice.end);
} else if (obj_kind == SPL_LANG_TYPE_ARRAY) {
spl_type_node_t *arr = spl_type_node(a->sema->type, obj_ir);
spl_type_node_t *arr = spl_type_node(&a->sema->type->ir, obj_ir);
if (!arr)
return 0;
usize arr_len = arr->array_type.len;
@@ -1024,13 +1023,13 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref)
}
static spl_ir_node_ref_t emit_if_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
spl_ast_node_t *n = spl_ast_node(a->sema->ast, ref);
spl_ast_node_t *n = spl_ast_node(a->ast, ref);
if (!n)
return 0;
bool is_ifvar = (n->kind == SPL_AST_EXPR_IFVAR);
spl_type_id_t result_t = node_ir(a, n);
spl_type_node_t *rtn = result_t ? spl_type_node(a->sema->type, result_t) : NULL;
spl_type_node_t *rtn = result_t ? spl_type_node(&a->sema->type->ir, result_t) : NULL;
bool need_result = rtn && rtn->kind != SPL_TYPE_UNIT;
spl_ir_node_ref_t result_slot = 0;
@@ -1043,7 +1042,7 @@ static spl_ir_node_ref_t emit_if_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
if (is_ifvar) {
emit_decl(a, n->ifvar_expr.var_decl);
spl_ast_node_t *vd = spl_ast_node(a->sema->ast, n->ifvar_expr.var_decl);
spl_ast_node_t *vd = spl_ast_node(a->ast, n->ifvar_expr.var_decl);
if (!vd || !vd->var_const_decl.init_expr)
Panic("ifvar requires initializer");
spl_ir_node_ref_t slot = 0;
@@ -1098,13 +1097,13 @@ static spl_ir_node_ref_t emit_if_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
}
static spl_ir_node_ref_t emit_loop_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
spl_ast_node_t *n = spl_ast_node(a->sema->ast, ref);
spl_ast_node_t *n = spl_ast_node(a->ast, ref);
if (!n)
return 0;
bool is_while = (n->kind == SPL_AST_EXPR_WHILE);
spl_type_id_t result_t = node_ir(a, n);
spl_type_node_t *rtn = result_t ? spl_type_node(a->sema->type, result_t) : NULL;
spl_type_node_t *rtn = result_t ? spl_type_node(&a->sema->type->ir, result_t) : NULL;
bool need_result = rtn && rtn->kind != SPL_TYPE_UNIT;
spl_ir_node_ref_t result_slot = 0;
@@ -1139,7 +1138,7 @@ static spl_ir_node_ref_t emit_loop_like(spl_ast2ir_t *a, spl_ast_node_ref_t ref)
spl_ast_node_ref_t label_ref = is_while ? n->while_expr.label : n->loop_expr.label;
if (label_ref) {
spl_ast_node_t *ln = spl_ast_node(a->sema->ast, label_ref);
spl_ast_node_t *ln = spl_ast_node(a->ast, label_ref);
if (ln && ln->kind == SPL_AST_LABEL)
ctx.label = ln->label.name;
}
@@ -1186,7 +1185,7 @@ static void emit_break_stmt(spl_ast2ir_t *a, spl_ast_node_t *n) {
const char *lbl = NULL;
if (n->statement_break.label) {
spl_ast_node_t *ln = spl_ast_node(a->sema->ast, n->statement_break.label);
spl_ast_node_t *ln = spl_ast_node(a->ast, n->statement_break.label);
if (ln && ln->kind == SPL_AST_LABEL)
lbl = ln->label.name;
}
@@ -1217,7 +1216,7 @@ static void emit_break_stmt(spl_ast2ir_t *a, spl_ast_node_t *n) {
}
static void emit_stmt(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
spl_ast_node_t *n = spl_ast_node(a->sema->ast, ref);
spl_ast_node_t *n = spl_ast_node(a->ast, ref);
if (!n)
return;
@@ -1272,7 +1271,7 @@ static void emit_stmt(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
const char *lbl = NULL;
if (n->statement_continue.label) {
spl_ast_node_t *ln = spl_ast_node(a->sema->ast, n->statement_continue.label);
spl_ast_node_t *ln = spl_ast_node(a->ast, n->statement_continue.label);
if (ln && ln->kind == SPL_AST_LABEL)
lbl = ln->label.name;
}
@@ -1304,7 +1303,7 @@ static void emit_stmt(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
}
static void emit_decl(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
spl_ast_node_t *n = spl_ast_node(a->sema->ast, ref);
spl_ast_node_t *n = spl_ast_node(a->ast, ref);
if (!n)
return;
@@ -1316,7 +1315,7 @@ static void emit_decl(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
if (!n->resolved_def_id)
return;
spl_ast_node_t *pat = spl_ast_node(a->sema->ast, n->var_const_decl.pattern);
spl_ast_node_t *pat = spl_ast_node(a->ast, n->var_const_decl.pattern);
if (!pat || pat->kind != SPL_AST_PATTERN_IDENT)
return;
@@ -1393,7 +1392,7 @@ static void emit_fn_body(spl_ast2ir_t *a, spl_ast_node_t *n) {
for (usize i = 0; i < vec_size(n->fn_decl.params); ++i) {
spl_ast_node_ref_t pref = vec_at(n->fn_decl.params, i);
spl_ast_node_t *pd = spl_ast_node(a->sema->ast, pref);
spl_ast_node_t *pd = spl_ast_node(a->ast, pref);
if (!pd || pd->kind != SPL_AST_PARAM_DECL)
continue;
if (!pd->resolved_def_id)
@@ -1451,13 +1450,13 @@ static void emit_fn_body(spl_ast2ir_t *a, spl_ast_node_t *n) {
static void emit_aggregate_fns_reserve(spl_ast2ir_t *a, spl_ast_node_t *te) {
for (usize i = 0; i < vec_size(te->type_aggregate.members); ++i) {
spl_ast_node_t *mn = spl_ast_node(a->sema->ast, vec_at(te->type_aggregate.members, i));
spl_ast_node_t *mn = spl_ast_node(a->ast, vec_at(te->type_aggregate.members, i));
if (!mn)
continue;
if (mn->kind == SPL_AST_FN_DECL) {
emit_fn_reserve(a, mn);
} else if (mn->kind == SPL_AST_MEMBER_DECL && mn->member_decl.type_expr) {
spl_ast_node_t *mt = spl_ast_node(a->sema->ast, mn->member_decl.type_expr);
spl_ast_node_t *mt = spl_ast_node(a->ast, mn->member_decl.type_expr);
if (mt && mt->kind == SPL_AST_TYPE_AGGREGATE)
emit_aggregate_fns_reserve(a, mt);
}
@@ -1466,13 +1465,13 @@ static void emit_aggregate_fns_reserve(spl_ast2ir_t *a, spl_ast_node_t *te) {
static void emit_aggregate_fns_body(spl_ast2ir_t *a, spl_ast_node_t *te) {
for (usize i = 0; i < vec_size(te->type_aggregate.members); ++i) {
spl_ast_node_t *mn = spl_ast_node(a->sema->ast, vec_at(te->type_aggregate.members, i));
spl_ast_node_t *mn = spl_ast_node(a->ast, vec_at(te->type_aggregate.members, i));
if (!mn)
continue;
if (mn->kind == SPL_AST_FN_DECL) {
emit_fn_body(a, mn);
} else if (mn->kind == SPL_AST_MEMBER_DECL && mn->member_decl.type_expr) {
spl_ast_node_t *mt = spl_ast_node(a->sema->ast, mn->member_decl.type_expr);
spl_ast_node_t *mt = spl_ast_node(a->ast, mn->member_decl.type_expr);
if (mt && mt->kind == SPL_AST_TYPE_AGGREGATE)
emit_aggregate_fns_body(a, mt);
}
@@ -1480,7 +1479,7 @@ static void emit_aggregate_fns_body(spl_ast2ir_t *a, spl_ast_node_t *te) {
}
static void emit_container_reserve(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
spl_ast_node_t *n = spl_ast_node(a->sema->ast, ref);
spl_ast_node_t *n = spl_ast_node(a->ast, ref);
if (!n)
return;
if (n->kind == SPL_AST_TOP_LEVEL_BLOCK) {
@@ -1493,7 +1492,7 @@ static void emit_container_reserve(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
for (usize i = 0; i < vec_size(n->container_members.declarations); ++i) {
spl_ast_node_ref_t dref = vec_at(n->container_members.declarations, i);
spl_ast_node_t *d = spl_ast_node(a->sema->ast, dref);
spl_ast_node_t *d = spl_ast_node(a->ast, dref);
if (!d)
continue;
switch (d->kind) {
@@ -1501,7 +1500,7 @@ static void emit_container_reserve(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
emit_fn_reserve(a, d);
break;
case SPL_AST_TYPE_DECL: {
spl_ast_node_t *te = spl_ast_node(a->sema->ast, d->type_decl.type_expr);
spl_ast_node_t *te = spl_ast_node(a->ast, d->type_decl.type_expr);
if (te && te->kind == SPL_AST_TYPE_AGGREGATE)
emit_aggregate_fns_reserve(a, te);
break;
@@ -1516,7 +1515,7 @@ static void emit_container_reserve(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
}
static void emit_container_body(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
spl_ast_node_t *n = spl_ast_node(a->sema->ast, ref);
spl_ast_node_t *n = spl_ast_node(a->ast, ref);
if (!n)
return;
if (n->kind == SPL_AST_TOP_LEVEL_BLOCK) {
@@ -1529,7 +1528,7 @@ static void emit_container_body(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
for (usize i = 0; i < vec_size(n->container_members.declarations); ++i) {
spl_ast_node_ref_t dref = vec_at(n->container_members.declarations, i);
spl_ast_node_t *d = spl_ast_node(a->sema->ast, dref);
spl_ast_node_t *d = spl_ast_node(a->ast, dref);
if (!d)
continue;
switch (d->kind) {
@@ -1537,7 +1536,7 @@ static void emit_container_body(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
emit_fn_body(a, d);
break;
case SPL_AST_TYPE_DECL: {
spl_ast_node_t *te = spl_ast_node(a->sema->ast, d->type_decl.type_expr);
spl_ast_node_t *te = spl_ast_node(a->ast, d->type_decl.type_expr);
if (te && te->kind == SPL_AST_TYPE_AGGREGATE)
emit_aggregate_fns_body(a, te);
break;
@@ -1555,8 +1554,8 @@ static spl_ir_node_ref_t emit_implicit_cast(spl_ast2ir_t *a, spl_ast_node_t *n,
spl_type_id_t tt, spl_ir_node_ref_t v) {
if (!ft || !tt || ft == tt)
return v;
spl_type_node_t *fs = spl_type_node(a->sema->type, ft);
spl_type_node_t *ts = spl_type_node(a->sema->type, tt);
spl_type_node_t *fs = spl_type_node(&a->sema->type->ir, ft);
spl_type_node_t *ts = spl_type_node(&a->sema->type->ir, tt);
if (!fs || !ts)
return v;
@@ -1577,8 +1576,6 @@ static spl_ir_node_ref_t emit_implicit_cast(spl_ast2ir_t *a, spl_ast_node_t *n,
if (fs->kind == SPL_TYPE_BOOL && ts->kind == SPL_TYPE_INT)
return spl_ir_builder_cast_bool2int(a->ir, ft, tt, v);
/* 指针之间:IR 层不区分 pointee,值语义相同,直接透传。
覆盖 *never -> *T(null 字面量)和 *T -> *any。 */
if (fs->kind == SPL_TYPE_PTR && ts->kind == SPL_TYPE_PTR)
return v;
@@ -1598,6 +1595,7 @@ static int spl_int_map_cmp(usize a, usize b) { return MAP_CMP_INT(a, b); }
void spl_ast2ir_init(spl_ast2ir_t *ast2ir, spl_ir_builder_t *ir, const spl_sema_t *sema) {
ast2ir->sema = sema;
ast2ir->ir = ir;
ast2ir->ast = sema->ast;
map_init(ast2ir->def2ir_map, spl_int_map_hash, spl_int_map_cmp);
vec_init(ast2ir->loop_stack);
vec_init(ast2ir->defer_stack);
@@ -1615,13 +1613,30 @@ void spl_ast2ir_drop(spl_ast2ir_t *ast2ir) {
map_free(ast2ir->def2ir_map);
}
void spl_ast2ir_run(spl_ast2ir_t *ast2ir) {
spl_ast_t *ast = ast2ir->sema->ast;
if (!ast || !ast->root)
return;
spl_ast_node_t *root = spl_ast_node(ast, ast->root);
if (!root || root->kind != SPL_AST_ROOT)
return;
emit_container_reserve(ast2ir, root->root.members);
emit_container_body(ast2ir, root->root.members);
void spl_ast2ir_run(spl_ast2ir_t *ast2ir, spl_ast_ptr_vec_t ast_vec) {
usize n = vec_size(ast_vec);
for (usize i = 0; i < n; ++i) {
spl_ast_t *ast = vec_at(ast_vec, i);
if (!ast || !ast->root)
continue;
ast2ir->ast = ast;
spl_ast_node_t *root = spl_ast_node(ast, ast->root);
if (!root || root->kind != SPL_AST_ROOT)
continue;
emit_container_reserve(ast2ir, root->root.members);
}
for (usize i = 0; i < n; ++i) {
spl_ast_t *ast = vec_at(ast_vec, i);
if (!ast || !ast->root)
continue;
ast2ir->ast = ast;
spl_ast_node_t *root = spl_ast_node(ast, ast->root);
if (!root || root->kind != SPL_AST_ROOT)
continue;
emit_container_body(ast2ir, root->root.members);
}
vec_free(ast_vec);
}