Skip to content

Commit aa05a39

Browse files
committed
lower ADT constructor calls directly
1 parent d021e94 commit aa05a39

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

‎src/lower1/control_flow.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use super::{
99
};
1010
use crate::oomir;
1111

12+
use rustc_hir::def::DefKind;
1213
use rustc_middle::{
1314
mir::{
1415
BasicBlock, BasicBlockData, Body, Local, Location, NonDivergingIntrinsic,
@@ -1995,6 +1996,37 @@ pub(super) fn convert_basic_block<'tcx>(
19951996
&mut instructions,
19961997
);
19971998
}
1999+
} else if matches!(tcx.def_kind(func_instance.def_id()), DefKind::Ctor(..)) {
2000+
let TyKind::Adt(adt_def, _) = fn_output.kind() else {
2001+
panic!("constructor returned non-ADT type {fn_output:?}")
2002+
};
2003+
if let Some(dest) = effective_dest {
2004+
let base_class = oomir_output_type
2005+
.get_class_name()
2006+
.expect("constructor result has a JVM class");
2007+
let class_name = if adt_def.is_enum() {
2008+
let variant_def_id = tcx.parent(func_instance.def_id());
2009+
format!(
2010+
"{}${}",
2011+
base_class,
2012+
jvm_names::member_name(tcx.item_name(variant_def_id).as_str())
2013+
)
2014+
} else {
2015+
base_class.to_string()
2016+
};
2017+
let constructor_args = oomir_operands
2018+
.into_iter()
2019+
.filter_map(|operand| {
2020+
let operand_ty = operand.get_type()?;
2021+
operand_ty.has_jvm_value().then_some((operand, operand_ty))
2022+
})
2023+
.collect();
2024+
instructions.push(oomir::Instruction::ConstructObject {
2025+
dest,
2026+
class_name,
2027+
args: constructor_args,
2028+
});
2029+
}
19982030
} else if let Some(item) = assoc_item {
19992031
if item.is_method() {
20002032
let receiver_mir_ty = args[0].node.ty(mir, tcx);

0 commit comments

Comments
 (0)