Skip to content

Commit 0fb816f

Browse files
committed
feat: made enum casting work
1 parent 39ddeb3 commit 0fb816f

9 files changed

Lines changed: 28 additions & 16 deletions

File tree

compiler/astoir_hir/src/nodes.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ impl HIRNode {
152152
}
153153

154154
if self_type.can_transmute(&t, &context.type_storage) {
155-
println!("Can transmute {:#?} -> {:#?}", t, self);
156-
157155
match &self.kind {
158156
HIRNodeKind::IntegerLiteral { value, int_type: _ } => {
159157
return Ok(self.with(HIRNodeKind::IntegerLiteral { value: *value, int_type: t }));

compiler/astoir_hir_lowering/src/unwraps.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use ast::tree::{ASTTreeNode, ASTTreeNodeKind};
22
use astoir_hir::{ctx::{HIRBranchedContext, HIRContext}, nodes::{HIRNode, HIRNodeKind}};
33
use diagnostics::DiagnosticResult;
44

5-
use crate::{types::lower_ast_type, values::lower_ast_value};
5+
use crate::{types::lower_ast_type, values::lower_ast_value, var::lower_ast_variable_reference};
66

77
pub fn lower_ast_condition_unwrap(context: &mut HIRContext, curr_ctx: &mut HIRBranchedContext, node: Box<ASTTreeNode>) -> DiagnosticResult<Box<HIRNode>> {
88
if let ASTTreeNodeKind::UnwrapCondition { original, target_type, unsafe_unwrap, target_var } = node.clone().kind {
9-
let original = lower_ast_value(context, curr_ctx, original)?;
9+
let original = lower_ast_variable_reference(context, curr_ctx, original, true)?;
1010
let target_type = lower_ast_type(context, target_type, &*node)?;
1111

1212
if target_var.is_none() {

compiler/astoir_mir/src/vals/refer.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use compiler_typing::storage::TypeStorage;
12
use diagnostics::{DiagnosticResult, builders::make_invalid_assign_diff_type_ir, unsure_panic};
23

34
use crate::{blocks::refer::MIRBlockReference, builder::{build_load, build_store}, ctx::MIRContext, vals::{base::BaseMIRValue, ptr::{MIRPointerValue}}};
@@ -39,7 +40,7 @@ impl MIRVariableReference {
3940
}
4041
}
4142

42-
pub fn write(&self, block: MIRBlockReference, ctx: &mut MIRContext, val: BaseMIRValue) -> DiagnosticResult<bool> {
43+
pub fn write(&self, block: MIRBlockReference, ctx: &mut MIRContext, val: BaseMIRValue, storage: &TypeStorage) -> DiagnosticResult<bool> {
4344
if self.is_pointer_ref() {
4445
let mut ptr_ref = self.as_pointer_ref()?;
4546
let hint = ctx.ssa_hints.get_hint(BaseMIRValue::from(ptr_ref.clone().into()).get_ssa_index());
@@ -48,7 +49,7 @@ impl MIRVariableReference {
4849
ptr_ref = build_load(ctx, ptr_ref)?.as_ptr()?;
4950
}
5051

51-
build_store(ctx, ptr_ref, val)?;
52+
build_store(ctx, storage, ptr_ref, val)?;
5253

5354
return Ok(true);
5455
}

compiler/astoir_mir_lowering/src/arrays.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn lower_hir_array_modify(block: MIRBlockReference, node: Box<HIRNode>, ctx:
3030

3131
let index_pointer = build_index_pointer(&mut ctx.mir_ctx, array, index)?;
3232

33-
build_store(&mut ctx.mir_ctx, index_pointer, val)?;
33+
build_store(&mut ctx.mir_ctx, &ctx.hir_ctx.type_storage, index_pointer, val)?;
3434

3535
return Ok(true);
3636
}

compiler/astoir_mir_lowering/src/casts.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ pub fn lower_cast(block: MIRBlockReference, node: Box<HIRNode>, ctx: &mut MIRLow
99
let value = lower_hir_value(block, value, ctx)?;
1010
let old_type = lower_hir_type(ctx, old_type)?;
1111

12-
println!("Old: {:#?}", old_type);
13-
1412
let new_type = lower_hir_type(ctx, new_type)?;
15-
println!("New: {:#?}", new_type);
1613

1714
if old_type.get_generic(&ctx.hir_ctx.type_storage).is_enum_child() && new_type.get_generic(&ctx.hir_ctx.type_storage).is_enum_parent() {
1815
match ctx.mir_ctx.ssa_hints.vec[value.get_ssa_index()] {

compiler/astoir_mir_lowering/src/introductions.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ pub fn handle_var_introduction_queue(block: MIRBlockReference, node: Box<HIRNode
2121
let ptr = build_stack_alloc(&mut ctx.mir_ctx, new_type.get_size(&new_type, false, &ctx .hir_ctx.type_storage), new_type)?;
2222

2323
ctx.mir_ctx.blocks[block].variables.insert(new_var, MIRBlockVariableSSAHint { kind: MIRBlockVariableType::Pointer, hint: Some(ptr.clone().into()) });
24-
build_store(&mut ctx.mir_ctx, ptr.clone(), casted)?;
24+
build_store(&mut ctx.mir_ctx, &ctx.hir_ctx.type_storage, ptr.clone(), casted)?;
2525
}
2626

27+
return Ok(())
2728
}
2829

2930
panic!("Invalid node")

compiler/astoir_mir_lowering/src/math.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn lower_hir_math_operation(block: MIRBlockReference, node: Box<HIRNode>, ct
3434
if assignment {
3535
let v = ptr.unwrap();
3636

37-
v.write(block, &mut ctx.mir_ctx, val.clone())?;
37+
v.write(block, &mut ctx.mir_ctx, val.clone(), &ctx.hir_ctx.type_storage)?;
3838
}
3939

4040
return Ok(val)

compiler/astoir_mir_lowering/src/type_tools.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use astoir_mir::{blocks::refer::MIRBlockReference, builder::{build_comp_eq, buil
55
use compiler_typing::{SizedType, raw::RawType, tree::Type};
66
use diagnostics::{DiagnosticResult, DiagnosticSpanOrigin, builders::{make_req_type_kind, make_type_not_partof}};
77

8-
use crate::{MIRLoweringContext, lower_hir_type, values::lower_hir_value};
8+
use crate::{MIRLoweringContext, lower_hir_type, values::lower_hir_value, vars::lower_hir_variable_reference};
99

1010
pub fn is_enum_value_of_kind<K: DiagnosticSpanOrigin>(block: MIRBlockReference, val: BaseMIRValue, enum_entry: RawType, ctx: &mut MIRLoweringContext, origin: &K) -> DiagnosticResult<MIRIntValue> {
1111
let enum_type = match ctx.mir_ctx.ssa_hints.get_hint(val.get_ssa_index()).get_type().as_generic_lowered_safe(origin)? {
@@ -51,11 +51,26 @@ pub fn is_enum_value_of_kind<K: DiagnosticSpanOrigin>(block: MIRBlockReference,
5151
pub fn cast_to_enum_child<K: DiagnosticSpanOrigin>(block: MIRBlockReference, val: BaseMIRValue, enum_entry: RawType, ctx: &mut MIRLoweringContext, origin: &K) -> DiagnosticResult<BaseMIRValue> {
5252
let enum_type = match ctx.mir_ctx.ssa_hints.get_hint(val.get_ssa_index()).get_type().as_generic_lowered_safe(origin)? {
5353
RawType::Enum(v) => v,
54+
RawType::LoweredStruct(_, container) => {
55+
if !container.is_lowered_enum_parent {
56+
return Err(make_req_type_kind(origin, &"enum parent".to_string()).into())
57+
}
58+
59+
container.lowered_enum_parent.unwrap()
60+
},
61+
5462
_ => return Err(make_req_type_kind(origin, &"enum parent".to_string()).into())
5563
};
5664

5765
let enum_entry_container = match &enum_entry {
5866
RawType::EnumEntry(v) => v,
67+
RawType::LoweredStruct(_, container) => {
68+
if !container.is_lowered_enum_child {
69+
return Err(make_req_type_kind(origin, &"enum parent".to_string()).into())
70+
}
71+
72+
container.lowered_enum_child.as_ref().unwrap()
73+
},
5974
_ => return Err(make_req_type_kind(origin, &"enum child".to_string()).into())
6075
};
6176

@@ -68,7 +83,7 @@ pub fn cast_to_enum_child<K: DiagnosticSpanOrigin>(block: MIRBlockReference, val
6883

6984
pub fn lower_hir_unwrap_cond(block: MIRBlockReference, node: Box<HIRNode>, ctx: &mut MIRLoweringContext) -> DiagnosticResult<BaseMIRValue> {
7085
if let HIRNodeKind::UnwrapCondition { original, new_type, new_var, unsafe_unwrap } = node.kind.clone() {
71-
let original = lower_hir_value(block, original, ctx)?;
86+
let original = lower_hir_variable_reference(block, &original, ctx)?.as_pointer_ref()?.into();
7287
let new_type = lower_hir_type(ctx, new_type)?;
7388

7489
let cond;

compiler/astoir_mir_lowering/src/vars.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn lower_hir_variable_declaration(block_id: MIRBlockReference, node: Box<HIR
3030
if default_val.is_some() {
3131
let val = lower_hir_value(block_id, default_val.unwrap(), ctx)?;
3232

33-
build_store(&mut ctx.mir_ctx, ptr.clone(), val)?;
33+
build_store(&mut ctx.mir_ctx, &ctx.hir_ctx.type_storage, ptr.clone(), val)?;
3434
}
3535
}
3636

@@ -64,7 +64,7 @@ pub fn lower_hir_variable_assignment(block: MIRBlockReference, node: Box<HIRNode
6464

6565
let val = lower_hir_value(block, val, ctx)?;
6666

67-
variable_ref.write(block, &mut ctx.mir_ctx, val)?;
67+
variable_ref.write(block, &mut ctx.mir_ctx, val, &ctx.hir_ctx.type_storage)?;
6868
return Ok(true);
6969
}
7070

0 commit comments

Comments
 (0)