-
Notifications
You must be signed in to change notification settings - Fork 13.4k
refactor to remove trans::adt and make rustc::ty::layout authoritative #36151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
f16068e
4038189
12ff05f
f2f7ace
467454b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -328,6 +328,43 @@ pub enum Integer { | |
} | ||
|
||
impl Integer { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra newline. |
||
pub fn size(&self) -> Size { | ||
match *self { | ||
I1 => Size::from_bits(1), | ||
I8 => Size::from_bytes(1), | ||
I16 => Size::from_bytes(2), | ||
I32 => Size::from_bytes(4), | ||
I64 => Size::from_bytes(8), | ||
} | ||
} | ||
|
||
pub fn align(&self, dl: &TargetDataLayout)-> Align { | ||
match *self { | ||
I1 => dl.i1_align, | ||
I8 => dl.i8_align, | ||
I16 => dl.i16_align, | ||
I32 => dl.i32_align, | ||
I64 => dl.i64_align, | ||
} | ||
} | ||
|
||
pub fn to_ty<'a, 'tcx>(&self, tcx: &ty::TyCtxt<'a, 'tcx, 'tcx>, | ||
signed: bool) -> Ty<'tcx> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
match (*self, signed) { | ||
(I1, false) => tcx.types.u8, | ||
(I8, false) => tcx.types.u8, | ||
(I16, false) => tcx.types.u16, | ||
(I32, false) => tcx.types.u32, | ||
(I64, false) => tcx.types.u64, | ||
(I1, true) => tcx.types.i8, | ||
(I8, true) => tcx.types.i8, | ||
(I16, true) => tcx.types.i16, | ||
(I32, true) => tcx.types.i32, | ||
(I64, true) => tcx.types.i64, | ||
} | ||
} | ||
|
||
/// Find the smallest Integer type which can represent the signed value. | ||
pub fn fit_signed(x: i64) -> Integer { | ||
match x { | ||
|
@@ -350,6 +387,18 @@ impl Integer { | |
} | ||
} | ||
|
||
//Find the smallest integer with the given alignment. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Space after |
||
pub fn for_abi_align(dl: &TargetDataLayout, align: Align) -> Option<Integer> { | ||
let wanted = align.abi(); | ||
for &candidate in &[I8, I16, I32, I64] { | ||
let ty = Int(candidate); | ||
if wanted == ty.align(dl).abi() && wanted == ty.size(dl).bytes() { | ||
return Some(candidate); | ||
} | ||
} | ||
None | ||
} | ||
|
||
/// Get the Integer type from an attr::IntType. | ||
pub fn from_attr(dl: &TargetDataLayout, ity: attr::IntType) -> Integer { | ||
match ity { | ||
|
@@ -623,6 +672,15 @@ impl<'a, 'gcx, 'tcx> Struct { | |
} | ||
Ok(None) | ||
} | ||
|
||
pub fn offset_of_field(&self, index: usize) -> Size { | ||
assert!(index < self.offset_after_field.len()); | ||
if index == 0 { | ||
Size::from_bytes(0) | ||
} else { | ||
self.offset_after_field[index-1] | ||
} | ||
} | ||
} | ||
|
||
/// An untagged union. | ||
|
@@ -912,7 +970,7 @@ impl<'a, 'gcx, 'tcx> Layout { | |
Univariant { variant: unit, non_zero: false } | ||
} | ||
|
||
// Tuples. | ||
// Tuples and closures. | ||
ty::TyClosure(_, ty::ClosureSubsts { upvar_tys: tys, .. }) | | ||
ty::TyTuple(tys) => { | ||
let mut st = Struct::new(dl, false); | ||
|
@@ -975,7 +1033,7 @@ impl<'a, 'gcx, 'tcx> Layout { | |
if def.variants.len() == 1 { | ||
// Struct, or union, or univariant enum equivalent to a struct. | ||
// (Typechecking will reject discriminant-sizing attrs.) | ||
assert!(!def.is_enum() || hint == attr::ReprAny); | ||
|
||
let fields = def.variants[0].fields.iter().map(|field| { | ||
field.ty(tcx, substs).layout(infcx) | ||
}); | ||
|
@@ -1003,6 +1061,16 @@ impl<'a, 'gcx, 'tcx> Layout { | |
} | ||
} | ||
|
||
if def.variants.len() == 1 && hint == attr::ReprAny{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing space before |
||
// Equivalent to a struct/tuple/newtype. | ||
let fields = def.variants[0].fields.iter().map(|field| { | ||
field.ty(tcx, substs).layout(infcx) | ||
}); | ||
let mut st = Struct::new(dl, false); | ||
st.extend(dl, fields, ty)?; | ||
return success(Univariant { variant: st, non_zero: false }); | ||
} | ||
|
||
// Cache the substituted and normalized variant field types. | ||
let variants = def.variants.iter().map(|v| { | ||
v.fields.iter().map(|field| field.ty(tcx, substs)).collect::<Vec<_>>() | ||
|
@@ -1103,20 +1171,7 @@ impl<'a, 'gcx, 'tcx> Layout { | |
// won't be so conservative. | ||
|
||
// Use the initial field alignment | ||
let wanted = start_align.abi(); | ||
let mut ity = min_ity; | ||
for &candidate in &[I16, I32, I64] { | ||
let ty = Int(candidate); | ||
if wanted == ty.align(dl).abi() && wanted == ty.size(dl).bytes() { | ||
ity = candidate; | ||
break; | ||
} | ||
} | ||
|
||
// FIXME(eddyb) conservative only to avoid diverging from trans::adt. | ||
if align.abi() != start_align.abi() { | ||
ity = min_ity; | ||
} | ||
let mut ity = Integer::for_abi_align(dl, start_align).unwrap_or(min_ity); | ||
|
||
// If the alignment is not larger than the chosen discriminant size, | ||
// don't use the alignment as the final size. | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: don't put random mutable parameters on the tcx. Use a
LayoutContext
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can't, because the recursion doesn't go through a dedicated context. You might be able to put it in
TargetDataLayout
, I guess, but that's about it.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space after
//
. Also,///
should be used (signifying doc comments).