Skip to content

Commit 857cbc1

Browse files
committed
fix: Fix struct literal SSA translation and type conversion
- Add Type::Struct to HirType::Struct conversion in SSA translator - Fix struct literal translation to use Alloca + InsertValue for field init - This enables struct field access via ExtractValue/InsertValue with GEP Direct struct literal field access now works: return Point{ .x = 42, .y = 10 }.x; // returns 42 Struct variable field access still needs type inference fix in zpeg runtime.
1 parent 80946c7 commit 857cbc1

1 file changed

Lines changed: 29 additions & 5 deletions

File tree

crates/compiler/src/ssa.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::sync::Arc;
99
use zyntax_typed_ast::{InternedString, Type, ConstValue, typed_ast::{TypedNode, TypedExpression}};
1010
use petgraph::visit::EdgeRef; // For .source() method on edges
1111
use crate::hir::{
12-
HirId, HirFunction, HirBlock, HirInstruction, HirValueKind,
12+
HirId, HirFunction, HirBlock, HirInstruction, HirValueKind, HirConstant,
1313
HirType, HirPhi, HirTerminator, CastOp, HirParam, HirFunctionSignature
1414
};
1515
use crate::cfg::{ControlFlowGraph, BasicBlock};
@@ -1620,9 +1620,12 @@ impl SsaBuilder {
16201620
}
16211621

16221622
TypedExpression::Struct(struct_lit) => {
1623-
// Allocate struct
1623+
// Allocate struct on stack
16241624
let struct_ty = self.convert_type(&expr.ty);
1625-
let alloc_result = self.create_value(struct_ty.clone(), HirValueKind::Instruction);
1625+
let alloc_result = self.create_value(
1626+
HirType::Ptr(Box::new(struct_ty.clone())),
1627+
HirValueKind::Instruction
1628+
);
16261629

16271630
self.add_instruction(block_id, HirInstruction::Alloca {
16281631
result: alloc_result,
@@ -1631,20 +1634,27 @@ impl SsaBuilder {
16311634
align: 8,
16321635
});
16331636

1634-
// Initialize each field
1637+
// Store each field value at the appropriate offset
16351638
for (i, field) in struct_lit.fields.iter().enumerate() {
16361639
let field_val = self.translate_expression(block_id, &field.value)?;
16371640

1638-
let insert_result = self.create_value(struct_ty.clone(), HirValueKind::Instruction);
1641+
// Use InsertValue with the alloc as the aggregate (pointer)
1642+
let insert_result = self.create_value(
1643+
HirType::Ptr(Box::new(struct_ty.clone())),
1644+
HirValueKind::Instruction
1645+
);
16391646
self.add_instruction(block_id, HirInstruction::InsertValue {
16401647
result: insert_result,
16411648
ty: struct_ty.clone(),
16421649
aggregate: alloc_result,
16431650
value: field_val,
16441651
indices: vec![i as u32],
16451652
});
1653+
self.add_use(alloc_result, insert_result);
1654+
self.add_use(field_val, insert_result);
16461655
}
16471656

1657+
// Return the pointer to the allocated struct
16481658
Ok(alloc_result)
16491659
}
16501660

@@ -2730,6 +2740,20 @@ impl SsaBuilder {
27302740
is_c_union: false,
27312741
}))
27322742
},
2743+
Type::Struct { fields, .. } => {
2744+
// Convert inline struct type to HirType::Struct
2745+
use crate::hir::HirStructType;
2746+
2747+
let hir_fields: Vec<HirType> = fields.iter()
2748+
.map(|field| self.convert_type(&field.ty))
2749+
.collect();
2750+
2751+
HirType::Struct(HirStructType {
2752+
name: None,
2753+
fields: hir_fields,
2754+
packed: false,
2755+
})
2756+
},
27332757
_ => HirType::I64, // Default for complex types
27342758
}
27352759
}

0 commit comments

Comments
 (0)