Skip to content

Commit 386dcf1

Browse files
committed
Fixed field type morphization bugs.
1 parent df51028 commit 386dcf1

File tree

7 files changed

+56
-70
lines changed

7 files changed

+56
-70
lines changed

src/compile_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,8 @@ macro_rules! cargo_test_ignored {
443443
.current_dir(test_dir)
444444
.args([
445445
"build",
446-
"--release", //"--target",
446+
"--release",
447+
"-j1" //"--target",
447448
//"clr64-unknown-clr"
448449
])
449450
.output()

src/place/adress.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,11 @@ pub fn place_elem_adress<'ctx>(
3939
}
4040
PlaceElem::Field(index, field_type) => match curr_type {
4141
PlaceTy::Ty(curr_type) => {
42-
let curr_type = crate::utilis::monomorphize(&method_instance, curr_type, tyctx);
43-
let gen_field_type = crate::utilis::generic_field_ty(
44-
curr_type,
45-
index.as_u32(),
46-
tyctx,
47-
method_instance,
48-
);
4942
//TODO: Why was this commented out?
5043
let field_type = crate::utilis::monomorphize(&method_instance, *field_type, tyctx);
51-
let field_name = field_name(curr_type, index.as_u32());
52-
let curr_type = crate::r#type::Type::from_ty(curr_type, tyctx, &method_instance);
53-
let curr_type = if let crate::r#type::Type::DotnetType(dotnet_type) = curr_type {
54-
dotnet_type.as_ref().clone()
55-
} else {
56-
panic!();
57-
};
58-
59-
let field_desc = FieldDescriptor::boxed(curr_type, gen_field_type, field_name);
60-
((field_type).into(), vec![CILOp::LDFieldAdress(field_desc)])
44+
let curr_type = crate::utilis::monomorphize(&method_instance, curr_type, tyctx);
45+
let field_desc = crate::utilis::field_descrptor(curr_type,(*index).into(),tyctx,method_instance);
46+
((field_type).into(), vec![CILOp::LDFieldAdress(field_desc.into())])
6147
}
6248
PlaceTy::EnumVariant(enm, var_idx) => {
6349
let owner = crate::utilis::monomorphize(&method_instance, enm, tyctx);

src/place/body.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,14 @@ pub fn place_elem_body<'ctx>(
3838
}
3939
PlaceElem::Field(index, field_type) => match curr_type {
4040
PlaceTy::Ty(curr_type) => {
41-
let curr_type = crate::utilis::monomorphize(&method_instance, curr_type, tyctx);
42-
let gen_field_type = crate::utilis::generic_field_ty(
43-
curr_type,
44-
index.as_u32(),
45-
tyctx,
46-
method_instance,
47-
);
4841
//TODO: Why was this commented out?
4942
let field_type = crate::utilis::monomorphize(&method_instance, *field_type, tyctx);
50-
let field_name = field_name(curr_type, index.as_u32());
51-
let curr_type = crate::r#type::Type::from_ty(curr_type, tyctx, &method_instance);
52-
let curr_type = if let crate::r#type::Type::DotnetType(dotnet_type) = curr_type {
53-
dotnet_type.as_ref().clone()
54-
} else {
55-
panic!();
56-
};
57-
58-
let field_desc = FieldDescriptor::boxed(curr_type, gen_field_type, field_name);
43+
let curr_type = crate::utilis::monomorphize(&method_instance, curr_type, tyctx);
44+
let field_desc = crate::utilis::field_descrptor(curr_type,(*index).into(),tyctx,method_instance);
5945
if body_ty_is_by_adress(&field_type) {
60-
((field_type).into(), vec![CILOp::LDFieldAdress(field_desc)])
46+
((field_type).into(), vec![CILOp::LDFieldAdress(field_desc.into())])
6147
} else {
62-
((field_type).into(), vec![CILOp::LDField(field_desc)])
48+
((field_type).into(), vec![CILOp::LDField(field_desc.into())])
6349
}
6450
}
6551
PlaceTy::EnumVariant(enm, var_idx) => {

src/place/get.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,8 @@ fn place_elem_get<'a>(
7676
PlaceElem::Field(index, _field_type) => match curr_type {
7777
super::PlaceTy::Ty(curr_type) => {
7878
let curr_type = crate::utilis::monomorphize(&method_instance, curr_type, ctx);
79-
let field_type = crate::utilis::generic_field_ty(
80-
curr_type,
81-
index.as_u32(),
82-
ctx,
83-
method_instance,
84-
);
85-
86-
let field_name = field_name(curr_type, index.as_u32());
87-
let curr_type = crate::r#type::Type::from_ty(curr_type, ctx, &method_instance);
88-
let curr_type = if let crate::r#type::Type::DotnetType(dotnet_type) = curr_type {
89-
dotnet_type.as_ref().clone()
90-
} else {
91-
panic!();
92-
};
93-
let field_desc = FieldDescriptor::boxed(curr_type, field_type, field_name);
94-
vec![CILOp::LDField(field_desc)]
79+
let field_desc = crate::utilis::field_descrptor(curr_type,(*index).into(),ctx,method_instance);
80+
vec![CILOp::LDField(field_desc.into())]
9581
}
9682
super::PlaceTy::EnumVariant(enm, var_idx) => {
9783
let owner = crate::utilis::monomorphize(&method_instance, enm, ctx);

src/place/set.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,8 @@ pub fn place_elem_set<'a>(
5252
PlaceElem::Field(index, _field_type) => {
5353
if let PlaceTy::Ty(curr_type) = curr_type {
5454
let curr_type = crate::utilis::monomorphize(&method_instance, curr_type, ctx);
55-
let field_type = crate::utilis::generic_field_ty(
56-
curr_type,
57-
index.as_u32(),
58-
ctx,
59-
method_instance,
60-
);
61-
let field_name = field_name(curr_type, index.as_u32());
62-
63-
let curr_type = crate::r#type::Type::from_ty(curr_type, ctx, &method_instance);
64-
let curr_type = if let crate::r#type::Type::DotnetType(dotnet_type) = curr_type {
65-
dotnet_type.as_ref().clone()
66-
} else {
67-
panic!();
68-
};
69-
let field_desc = FieldDescriptor::boxed(curr_type, field_type, field_name);
70-
vec![CILOp::STField(field_desc)]
55+
let field_desc = crate::utilis::field_descrptor(curr_type,(*index).into(),ctx,method_instance);
56+
vec![CILOp::STField(field_desc.into())]
7157
} else {
7258
todo!("Can't set fields of enum variants yet!");
7359
}

src/type_def.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
method::Method,
44
r#type::{DotnetTypeRef, Type},
55
utilis::{enum_tag_size, monomorphize, tag_from_enum_variants},
6-
IString,
6+
IString, cil_op::FieldDescriptor,
77
};
88
use rustc_middle::ty::{
99
AdtDef, AdtKind, AliasKind, GenericArg, Instance, List, Ty, TyCtxt, TyKind,
@@ -32,6 +32,23 @@ impl TypeDef {
3232
pub fn set_generic_count(&mut self, generic_count: u32) {
3333
self.gargc = generic_count;
3434
}
35+
/// Gets a [`FieldDescriptor`] describing to a rust filed at rust field index `rust_field_idx`.
36+
pub fn field_desc_from_rust_field_idx(&self,self_tpe_ref:DotnetTypeRef,rust_field_idx:u32)->FieldDescriptor{
37+
let mut field_iter = self.fields.iter();
38+
// If explicit offsets present, check for enum tags
39+
if let Some(offsets) = &self.explicit_offsets{
40+
if offsets[0] == 0 && self.fields()[0].0.as_ref() == "_tag"{
41+
field_iter.next();
42+
}
43+
};
44+
// Get the nth field
45+
let (field_name,field_type) = field_iter.nth(rust_field_idx as usize).expect("`field_desc_from_rust_field_idx` could not find field info!");
46+
FieldDescriptor::new(
47+
self_tpe_ref,
48+
field_type.clone(),
49+
field_name.clone(),
50+
)
51+
}
3552
pub fn gargc(&self) -> u32 {
3653
self.gargc
3754
}
@@ -74,6 +91,8 @@ impl TypeDef {
7491
explicit_offsets: None,
7592
}
7693
}
94+
/// Gets the definition from type `ty`. The TypeDef corresponding to `ty` is the last one.
95+
// TODO: refactor this!
7796
pub fn from_ty<'tyctx>(
7897
ty: Ty<'tyctx>,
7998
ctx: TyCtxt<'tyctx>,

src/utilis.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub const MANAGED_CALL_VIRT_FN_NAME: &str = "rustc_clr_interop_managed_call_virt
1212
pub fn is_function_magic(name: &str) -> bool {
1313
name.contains(CTOR_FN_NAME) || name.contains(MANAGED_CALL_FN_NAME)
1414
}
15-
use crate::{codegen_error::MethodCodegenError, r#type::DotnetTypeRef};
15+
use crate::{codegen_error::MethodCodegenError, r#type::{DotnetTypeRef, Type}, type_def::TypeDef, cil_op::FieldDescriptor};
1616
pub fn skip_binder_if_no_generic_types<T>(binder: Binder<T>) -> Result<T, MethodCodegenError> {
1717
/*
1818
if binder
@@ -73,13 +73,33 @@ pub fn monomorphize<'tcx, T: TypeFoldable<TyCtxt<'tcx>> + Clone>(
7373
EarlyBinder::bind(ty),
7474
)
7575
}
76+
pub fn field_descrptor<'ctx>(
77+
owner_ty: Ty<'ctx>,
78+
field_idx: u32,
79+
ctx: TyCtxt<'ctx>,
80+
method_instance: Instance<'ctx>,
81+
) -> FieldDescriptor {
82+
if let TyKind::Tuple(elements) = owner_ty.kind(){
83+
assert!(elements.len() < 8,"Tuples with more than 8 elements are not supported!");
84+
return FieldDescriptor::new(
85+
crate::r#type::tuple_type(&elements.iter().map(|tpe|Type::from_ty(tpe,ctx,&method_instance)).collect::<Vec<_>>()),
86+
Type::GenericArg(field_idx),
87+
format!("Item{}",field_idx + 1).into(),
88+
);
89+
}
90+
let defs = TypeDef::from_ty(owner_ty, ctx, &method_instance);
91+
let type_ref = Type::from_ty(owner_ty, ctx, &method_instance).as_dotnet().expect("Field owner not a dotnet type!");
92+
let def = defs.iter().last().expect("`crate::utilis::field_descrptor` called on type without a definition.");
93+
def.field_desc_from_rust_field_idx(type_ref,field_idx)
94+
}
7695
/// Gets the type of field with index `field_idx`, returning a GenericArg if the types field is generic
7796
pub fn generic_field_ty<'ctx>(
7897
owner_ty: Ty<'ctx>,
7998
field_idx: u32,
8099
ctx: TyCtxt<'ctx>,
81100
method_instance: Instance<'ctx>,
82101
) -> crate::r#type::Type {
102+
83103
match owner_ty.kind() {
84104
TyKind::Adt(adt_def, _) => {
85105
let ty = ctx
@@ -191,6 +211,8 @@ pub fn compiletime_sizeof(ty: Ty) -> usize {
191211
_ => todo!("Can't compute compiletime sizeof {int:?}"),
192212
},
193213
TyKind::Uint(int) => match int {
214+
UintTy::U8 => std::mem::size_of::<u8>(),
215+
UintTy::U16 => std::mem::size_of::<u16>(),
194216
UintTy::U32 => std::mem::size_of::<u32>(),
195217
UintTy::U128 => std::mem::size_of::<u128>(),
196218
UintTy::Usize => {

0 commit comments

Comments
 (0)