1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! # Representation of Algebraic Data Types
13 //! This module determines how to represent enums, structs, and tuples
14 //! based on their monomorphized types; it is responsible both for
15 //! choosing a representation and translating basic operations on
16 //! values of those types. (Note: exporting the representations for
17 //! debuggers is handled in debuginfo.rs, not here.)
19 //! Note that the interface treats everything as a general case of an
20 //! enum, so structs/tuples/etc. have one pseudo-variant with
21 //! discriminant 0; i.e., as if they were a univariant enum.
23 //! Having everything in one place will enable improvements to data
24 //! structure representation; possibilities include:
26 //! - User-specified alignment (e.g., cacheline-aligning parts of
27 //! concurrently accessed data structures); LLVM can't represent this
28 //! directly, so we'd have to insert padding fields in any structure
29 //! that might contain one and adjust GEP indices accordingly. See
32 //! - Store nested enums' discriminants in the same word. Rather, if
33 //! some variants start with enums, and those enums representations
34 //! have unused alignment padding between discriminant and body, the
35 //! outer enum's discriminant can be stored there and those variants
36 //! can start at offset 0. Kind of fancy, and might need work to
37 //! make copies of the inner enum type cooperate, but it could help
38 //! with `Option` or `Result` wrapped around another enum.
40 //! - Tagged pointers would be neat, but given that any type can be
41 //! used unboxed and any field can have pointers (including mutable)
42 //! taken to it, implementing them for Rust seems difficult.
44 pub use self::Repr::*;
48 use llvm::{ValueRef, True, IntEQ, IntNE};
49 use back::abi::FAT_PTR_ADDR;
51 use middle::ty::{self, Ty};
55 use syntax::attr::IntType;
59 use trans::cleanup::CleanupMethods;
62 use trans::debuginfo::DebugLoc;
64 use trans::monomorphize;
65 use trans::type_::Type;
68 type Hint = attr::ReprAttr;
71 #[derive(Eq, PartialEq, Debug)]
73 /// C-like enums; basically an int.
74 CEnum(IntType, Disr, Disr), // discriminant range (signedness based on the IntType)
75 /// Single-case variants, and structs/tuples/records.
77 /// Structs with destructors need a dynamic destroyedness flag to
78 /// avoid running the destructor too many times; this is included
79 /// in the `Struct` if present.
80 /// (The flag if nonzero, represents the initialization value to use;
81 /// if zero, then use no flag at all.)
82 Univariant(Struct<'tcx>, u8),
83 /// General-case enums: for each case there is a struct, and they
84 /// all start with a field for the discriminant.
86 /// Types with destructors need a dynamic destroyedness flag to
87 /// avoid running the destructor too many times; the last argument
88 /// indicates whether such a flag is present.
89 /// (The flag, if nonzero, represents the initialization value to use;
90 /// if zero, then use no flag at all.)
91 General(IntType, Vec<Struct<'tcx>>, u8),
92 /// Two cases distinguished by a nullable pointer: the case with discriminant
93 /// `nndiscr` must have single field which is known to be nonnull due to its type.
94 /// The other case is known to be zero sized. Hence we represent the enum
95 /// as simply a nullable pointer: if not null it indicates the `nndiscr` variant,
96 /// otherwise it indicates the other case.
100 nullfields: Vec<Ty<'tcx>>
102 /// Two cases distinguished by a nullable pointer: the case with discriminant
103 /// `nndiscr` is represented by the struct `nonnull`, where the `discrfield`th
104 /// field is known to be nonnull due to its type; if that field is null, then
105 /// it represents the other case, which is inhabited by at most one value
106 /// (and all other fields are undefined/unused).
108 /// For example, `std::option::Option` instantiated at a safe pointer type
109 /// is represented such that `None` is a null pointer and `Some` is the
110 /// identity function.
111 StructWrappedNullablePointer {
112 nonnull: Struct<'tcx>,
114 discrfield: DiscrField,
115 nullfields: Vec<Ty<'tcx>>,
119 /// For structs, and struct-like parts of anything fancier.
120 #[derive(Eq, PartialEq, Debug)]
121 pub struct Struct<'tcx> {
122 // If the struct is DST, then the size and alignment do not take into
123 // account the unsized fields of the struct.
128 pub fields: Vec<Ty<'tcx>>
131 /// Convenience for `represent_type`. There should probably be more or
132 /// these, for places in trans where the `Ty` isn't directly
134 pub fn represent_node<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
135 node: ast::NodeId) -> Rc<Repr<'tcx>> {
136 represent_type(bcx.ccx(), node_id_type(bcx, node))
139 /// Decides how to represent a given type.
140 pub fn represent_type<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
143 debug!("Representing: {}", t);
144 match cx.adt_reprs().borrow().get(&t) {
145 Some(repr) => return repr.clone(),
149 let repr = Rc::new(represent_type_uncached(cx, t));
150 debug!("Represented as: {:?}", repr);
151 cx.adt_reprs().borrow_mut().insert(t, repr.clone());
155 macro_rules! repeat_u8_as_u32 {
156 ($name:expr) => { (($name as u32) << 24 |
157 ($name as u32) << 16 |
158 ($name as u32) << 8 |
161 macro_rules! repeat_u8_as_u64 {
162 ($name:expr) => { ((repeat_u8_as_u32!($name) as u64) << 32 |
163 (repeat_u8_as_u32!($name) as u64)) }
166 pub const DTOR_NEEDED: u8 = 0xd4;
167 pub const DTOR_NEEDED_U32: u32 = repeat_u8_as_u32!(DTOR_NEEDED);
168 pub const DTOR_NEEDED_U64: u64 = repeat_u8_as_u64!(DTOR_NEEDED);
170 pub fn dtor_needed_usize(ccx: &CrateContext) -> usize {
171 match &ccx.tcx().sess.target.target.target_pointer_width[..] {
172 "32" => DTOR_NEEDED_U32 as usize,
173 "64" => DTOR_NEEDED_U64 as usize,
174 tws => panic!("Unsupported target word size for int: {}", tws),
178 pub const DTOR_DONE: u8 = 0x1d;
179 pub const DTOR_DONE_U32: u32 = repeat_u8_as_u32!(DTOR_DONE);
180 pub const DTOR_DONE_U64: u64 = repeat_u8_as_u64!(DTOR_DONE);
182 pub fn dtor_done_usize(ccx: &CrateContext) -> usize {
183 match &ccx.tcx().sess.target.target.target_pointer_width[..] {
184 "32" => DTOR_DONE_U32 as usize,
185 "64" => DTOR_DONE_U64 as usize,
186 tws => panic!("Unsupported target word size for int: {}", tws),
190 fn dtor_to_init_u8(dtor: bool) -> u8 {
191 if dtor { DTOR_NEEDED } else { 0 }
194 pub trait GetDtorType<'tcx> { fn dtor_type(&self) -> Ty<'tcx>; }
195 impl<'tcx> GetDtorType<'tcx> for ty::ctxt<'tcx> {
196 fn dtor_type(&self) -> Ty<'tcx> { self.types.u8 }
199 fn dtor_active(flag: u8) -> bool {
203 fn represent_type_uncached<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
204 t: Ty<'tcx>) -> Repr<'tcx> {
206 ty::TyTuple(ref elems) => {
207 Univariant(mk_struct(cx, &elems[..], false, t), 0)
209 ty::TyStruct(def_id, substs) => {
210 let fields = cx.tcx().lookup_struct_fields(def_id);
211 let mut ftys = fields.iter().map(|field| {
212 let fty = cx.tcx().lookup_field_type(def_id, field.id, substs);
213 monomorphize::normalize_associated_type(cx.tcx(), &fty)
214 }).collect::<Vec<_>>();
215 let packed = cx.tcx().lookup_packed(def_id);
216 let dtor = cx.tcx().ty_dtor(def_id).has_drop_flag();
218 ftys.push(cx.tcx().dtor_type());
221 Univariant(mk_struct(cx, &ftys[..], packed, t), dtor_to_init_u8(dtor))
223 ty::TyClosure(_, ref substs) => {
224 Univariant(mk_struct(cx, &substs.upvar_tys, false, t), 0)
226 ty::TyEnum(def_id, substs) => {
227 let cases = get_cases(cx.tcx(), def_id, substs);
228 let hint = *cx.tcx().lookup_repr_hints(def_id).get(0)
229 .unwrap_or(&attr::ReprAny);
231 let dtor = cx.tcx().ty_dtor(def_id).has_drop_flag();
233 if cases.is_empty() {
234 // Uninhabitable; represent as unit
235 // (Typechecking will reject discriminant-sizing attrs.)
236 assert_eq!(hint, attr::ReprAny);
237 let ftys = if dtor { vec!(cx.tcx().dtor_type()) } else { vec!() };
238 return Univariant(mk_struct(cx, &ftys[..], false, t),
239 dtor_to_init_u8(dtor));
242 if !dtor && cases.iter().all(|c| c.tys.is_empty()) {
243 // All bodies empty -> intlike
244 let discrs: Vec<u64> = cases.iter().map(|c| c.discr).collect();
245 let bounds = IntBounds {
246 ulo: *discrs.iter().min().unwrap(),
247 uhi: *discrs.iter().max().unwrap(),
248 slo: discrs.iter().map(|n| *n as i64).min().unwrap(),
249 shi: discrs.iter().map(|n| *n as i64).max().unwrap()
251 return mk_cenum(cx, hint, &bounds);
254 // Since there's at least one
255 // non-empty body, explicit discriminants should have
256 // been rejected by a checker before this point.
257 if !cases.iter().enumerate().all(|(i,c)| c.discr == (i as Disr)) {
258 cx.sess().bug(&format!("non-C-like enum {} with specified \
260 cx.tcx().item_path_str(def_id)));
263 if cases.len() == 1 {
264 // Equivalent to a struct/tuple/newtype.
265 // (Typechecking will reject discriminant-sizing attrs.)
266 assert_eq!(hint, attr::ReprAny);
267 let mut ftys = cases[0].tys.clone();
268 if dtor { ftys.push(cx.tcx().dtor_type()); }
269 return Univariant(mk_struct(cx, &ftys[..], false, t),
270 dtor_to_init_u8(dtor));
273 if !dtor && cases.len() == 2 && hint == attr::ReprAny {
274 // Nullable pointer optimization
277 if cases[1 - discr].is_zerolen(cx, t) {
278 let st = mk_struct(cx, &cases[discr].tys,
280 match cases[discr].find_ptr(cx) {
281 Some(ref df) if df.len() == 1 && st.fields.len() == 1 => {
282 return RawNullablePointer {
283 nndiscr: discr as Disr,
285 nullfields: cases[1 - discr].tys.clone()
288 Some(mut discrfield) => {
290 discrfield.reverse();
291 return StructWrappedNullablePointer {
292 nndiscr: discr as Disr,
294 discrfield: discrfield,
295 nullfields: cases[1 - discr].tys.clone()
306 assert!((cases.len() - 1) as i64 >= 0);
307 let bounds = IntBounds { ulo: 0, uhi: (cases.len() - 1) as u64,
308 slo: 0, shi: (cases.len() - 1) as i64 };
309 let min_ity = range_to_inttype(cx, hint, &bounds);
311 // Create the set of structs that represent each variant
312 // Use the minimum integer type we figured out above
313 let fields : Vec<_> = cases.iter().map(|c| {
314 let mut ftys = vec!(ty_of_inttype(cx.tcx(), min_ity));
315 ftys.push_all(&c.tys);
316 if dtor { ftys.push(cx.tcx().dtor_type()); }
317 mk_struct(cx, &ftys, false, t)
321 // Check to see if we should use a different type for the
322 // discriminant. If the overall alignment of the type is
323 // the same as the first field in each variant, we can safely use
324 // an alignment-sized type.
325 // We increase the size of the discriminant to avoid LLVM copying
326 // padding when it doesn't need to. This normally causes unaligned
327 // load/stores and excessive memcpy/memset operations. By using a
328 // bigger integer size, LLVM can be sure about it's contents and
329 // won't be so conservative.
330 // This check is needed to avoid increasing the size of types when
331 // the alignment of the first field is smaller than the overall
332 // alignment of the type.
333 let (_, align) = union_size_and_align(&fields);
334 let mut use_align = true;
336 // Get the first non-zero-sized field
337 let field = st.fields.iter().skip(1).filter(|ty| {
338 let t = type_of::sizing_type_of(cx, **ty);
339 machine::llsize_of_real(cx, t) != 0 ||
340 // This case is only relevant for zero-sized types with large alignment
341 machine::llalign_of_min(cx, t) != 1
344 if let Some(field) = field {
345 let field_align = type_of::align_of(cx, *field);
346 if field_align != align {
352 let ity = if use_align {
353 // Use the overall alignment
355 1 => attr::UnsignedInt(ast::TyU8),
356 2 => attr::UnsignedInt(ast::TyU16),
357 4 => attr::UnsignedInt(ast::TyU32),
358 8 if machine::llalign_of_min(cx, Type::i64(cx)) == 8 =>
359 attr::UnsignedInt(ast::TyU64),
360 _ => min_ity // use min_ity as a fallback
366 let fields : Vec<_> = cases.iter().map(|c| {
367 let mut ftys = vec!(ty_of_inttype(cx.tcx(), ity));
368 ftys.push_all(&c.tys);
369 if dtor { ftys.push(cx.tcx().dtor_type()); }
370 mk_struct(cx, &ftys[..], false, t)
373 ensure_enum_fits_in_address_space(cx, &fields[..], t);
375 General(ity, fields, dtor_to_init_u8(dtor))
377 _ => cx.sess().bug(&format!("adt::represent_type called on non-ADT type: {}", t))
381 // this should probably all be in ty
387 /// This represents the (GEP) indices to follow to get to the discriminant field
388 pub type DiscrField = Vec<usize>;
390 fn find_discr_field_candidate<'tcx>(tcx: &ty::ctxt<'tcx>,
392 mut path: DiscrField) -> Option<DiscrField> {
394 // Fat &T/&mut T/Box<T> i.e. T is [T], str, or Trait
395 ty::TyRef(_, ty::TypeAndMut { ty, .. }) | ty::TyBox(ty) if !type_is_sized(tcx, ty) => {
396 path.push(FAT_PTR_ADDR);
400 // Regular thin pointer: &T/&mut T/Box<T>
401 ty::TyRef(..) | ty::TyBox(..) => Some(path),
403 // Functions are just pointers
404 ty::TyBareFn(..) => Some(path),
406 // Is this the NonZero lang item wrapping a pointer or integer type?
407 ty::TyStruct(did, substs) if Some(did) == tcx.lang_items.non_zero() => {
408 let nonzero_fields = tcx.lookup_struct_fields(did);
409 assert_eq!(nonzero_fields.len(), 1);
410 let nonzero_field = tcx.lookup_field_type(did, nonzero_fields[0].id, substs);
411 match nonzero_field.sty {
412 ty::TyRawPtr(ty::TypeAndMut { ty, .. }) if !type_is_sized(tcx, ty) => {
413 path.push_all(&[0, FAT_PTR_ADDR]);
416 ty::TyRawPtr(..) | ty::TyInt(..) | ty::TyUint(..) => {
424 // Perhaps one of the fields of this struct is non-zero
425 // let's recurse and find out
426 ty::TyStruct(def_id, substs) => {
427 let fields = tcx.lookup_struct_fields(def_id);
428 for (j, field) in fields.iter().enumerate() {
429 let field_ty = tcx.lookup_field_type(def_id, field.id, substs);
430 if let Some(mut fpath) = find_discr_field_candidate(tcx, field_ty, path.clone()) {
438 // Perhaps one of the upvars of this struct is non-zero
439 // Let's recurse and find out!
440 ty::TyClosure(_, ref substs) => {
441 for (j, &ty) in substs.upvar_tys.iter().enumerate() {
442 if let Some(mut fpath) = find_discr_field_candidate(tcx, ty, path.clone()) {
450 // Can we use one of the fields in this tuple?
451 ty::TyTuple(ref tys) => {
452 for (j, &ty) in tys.iter().enumerate() {
453 if let Some(mut fpath) = find_discr_field_candidate(tcx, ty, path.clone()) {
461 // Is this a fixed-size array of something non-zero
462 // with at least one element?
463 ty::TyArray(ety, d) if d > 0 => {
464 if let Some(mut vpath) = find_discr_field_candidate(tcx, ety, path) {
472 // Anything else is not a pointer
477 impl<'tcx> Case<'tcx> {
478 fn is_zerolen<'a>(&self, cx: &CrateContext<'a, 'tcx>, scapegoat: Ty<'tcx>) -> bool {
479 mk_struct(cx, &self.tys, false, scapegoat).size == 0
482 fn find_ptr<'a>(&self, cx: &CrateContext<'a, 'tcx>) -> Option<DiscrField> {
483 for (i, &ty) in self.tys.iter().enumerate() {
484 if let Some(mut path) = find_discr_field_candidate(cx.tcx(), ty, vec![]) {
493 fn get_cases<'tcx>(tcx: &ty::ctxt<'tcx>,
495 substs: &subst::Substs<'tcx>)
497 tcx.enum_variants(def_id).iter().map(|vi| {
498 let arg_tys = vi.args.iter().map(|&raw_ty| {
499 monomorphize::apply_param_substs(tcx, substs, &raw_ty)
501 Case { discr: vi.disr_val, tys: arg_tys }
505 fn mk_struct<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
506 tys: &[Ty<'tcx>], packed: bool,
509 let sized = tys.iter().all(|&ty| type_is_sized(cx.tcx(), ty));
510 let lltys : Vec<Type> = if sized {
511 tys.iter().map(|&ty| type_of::sizing_type_of(cx, ty)).collect()
513 tys.iter().filter(|&ty| type_is_sized(cx.tcx(), *ty))
514 .map(|&ty| type_of::sizing_type_of(cx, ty)).collect()
517 ensure_struct_fits_in_address_space(cx, &lltys[..], packed, scapegoat);
519 let llty_rec = Type::struct_(cx, &lltys[..], packed);
521 size: machine::llsize_of_alloc(cx, llty_rec),
522 align: machine::llalign_of_min(cx, llty_rec),
525 fields: tys.to_vec(),
537 fn mk_cenum<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
538 hint: Hint, bounds: &IntBounds)
540 let it = range_to_inttype(cx, hint, bounds);
542 attr::SignedInt(_) => CEnum(it, bounds.slo as Disr, bounds.shi as Disr),
543 attr::UnsignedInt(_) => CEnum(it, bounds.ulo, bounds.uhi)
547 fn range_to_inttype(cx: &CrateContext, hint: Hint, bounds: &IntBounds) -> IntType {
548 debug!("range_to_inttype: {:?} {:?}", hint, bounds);
549 // Lists of sizes to try. u64 is always allowed as a fallback.
550 #[allow(non_upper_case_globals)]
551 const choose_shortest: &'static [IntType] = &[
552 attr::UnsignedInt(ast::TyU8), attr::SignedInt(ast::TyI8),
553 attr::UnsignedInt(ast::TyU16), attr::SignedInt(ast::TyI16),
554 attr::UnsignedInt(ast::TyU32), attr::SignedInt(ast::TyI32)];
555 #[allow(non_upper_case_globals)]
556 const at_least_32: &'static [IntType] = &[
557 attr::UnsignedInt(ast::TyU32), attr::SignedInt(ast::TyI32)];
561 attr::ReprInt(span, ity) => {
562 if !bounds_usable(cx, ity, bounds) {
563 cx.sess().span_bug(span, "representation hint insufficient for discriminant range")
567 attr::ReprExtern => {
568 attempts = match &cx.sess().target.target.arch[..] {
569 // WARNING: the ARM EABI has two variants; the one corresponding to `at_least_32`
570 // appears to be used on Linux and NetBSD, but some systems may use the variant
571 // corresponding to `choose_shortest`. However, we don't run on those yet...?
572 "arm" => at_least_32,
577 attempts = choose_shortest;
579 attr::ReprPacked => {
580 cx.tcx().sess.bug("range_to_inttype: found ReprPacked on an enum");
583 for &ity in attempts {
584 if bounds_usable(cx, ity, bounds) {
588 return attr::UnsignedInt(ast::TyU64);
591 pub fn ll_inttype(cx: &CrateContext, ity: IntType) -> Type {
593 attr::SignedInt(t) => Type::int_from_ty(cx, t),
594 attr::UnsignedInt(t) => Type::uint_from_ty(cx, t)
598 fn bounds_usable(cx: &CrateContext, ity: IntType, bounds: &IntBounds) -> bool {
599 debug!("bounds_usable: {:?} {:?}", ity, bounds);
601 attr::SignedInt(_) => {
602 let lllo = C_integral(ll_inttype(cx, ity), bounds.slo as u64, true);
603 let llhi = C_integral(ll_inttype(cx, ity), bounds.shi as u64, true);
604 bounds.slo == const_to_int(lllo) as i64 && bounds.shi == const_to_int(llhi) as i64
606 attr::UnsignedInt(_) => {
607 let lllo = C_integral(ll_inttype(cx, ity), bounds.ulo, false);
608 let llhi = C_integral(ll_inttype(cx, ity), bounds.uhi, false);
609 bounds.ulo == const_to_uint(lllo) as u64 && bounds.uhi == const_to_uint(llhi) as u64
614 pub fn ty_of_inttype<'tcx>(tcx: &ty::ctxt<'tcx>, ity: IntType) -> Ty<'tcx> {
616 attr::SignedInt(t) => tcx.mk_mach_int(t),
617 attr::UnsignedInt(t) => tcx.mk_mach_uint(t)
621 // LLVM doesn't like types that don't fit in the address space
622 fn ensure_struct_fits_in_address_space<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
625 scapegoat: Ty<'tcx>) {
627 for &llty in fields {
628 // Invariant: offset < ccx.obj_size_bound() <= 1<<61
630 let type_align = machine::llalign_of_min(ccx, llty);
631 offset = roundup(offset, type_align);
633 // type_align is a power-of-2, so still offset < ccx.obj_size_bound()
634 // llsize_of_alloc(ccx, llty) is also less than ccx.obj_size_bound()
635 // so the sum is less than 1<<62 (and therefore can't overflow).
636 offset += machine::llsize_of_alloc(ccx, llty);
638 if offset >= ccx.obj_size_bound() {
639 ccx.report_overbig_object(scapegoat);
644 fn union_size_and_align(sts: &[Struct]) -> (machine::llsize, machine::llalign) {
645 let size = sts.iter().map(|st| st.size).max().unwrap();
646 let align = sts.iter().map(|st| st.align).max().unwrap();
647 (roundup(size, align), align)
650 fn ensure_enum_fits_in_address_space<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
652 scapegoat: Ty<'tcx>) {
653 let (total_size, _) = union_size_and_align(fields);
655 if total_size >= ccx.obj_size_bound() {
656 ccx.report_overbig_object(scapegoat);
661 /// LLVM-level types are a little complicated.
663 /// C-like enums need to be actual ints, not wrapped in a struct,
664 /// because that changes the ABI on some platforms (see issue #10308).
666 /// For nominal types, in some cases, we need to use LLVM named structs
667 /// and fill in the actual contents in a second pass to prevent
668 /// unbounded recursion; see also the comments in `trans::type_of`.
669 pub fn type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, r: &Repr<'tcx>) -> Type {
670 generic_type_of(cx, r, None, false, false)
672 // Pass dst=true if the type you are passing is a DST. Yes, we could figure
673 // this out, but if you call this on an unsized type without realising it, you
674 // are going to get the wrong type (it will not include the unsized parts of it).
675 pub fn sizing_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
676 r: &Repr<'tcx>, dst: bool) -> Type {
677 generic_type_of(cx, r, None, true, dst)
679 pub fn incomplete_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
680 r: &Repr<'tcx>, name: &str) -> Type {
681 generic_type_of(cx, r, Some(name), false, false)
683 pub fn finish_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
684 r: &Repr<'tcx>, llty: &mut Type) {
686 CEnum(..) | General(..) | RawNullablePointer { .. } => { }
687 Univariant(ref st, _) | StructWrappedNullablePointer { nonnull: ref st, .. } =>
688 llty.set_struct_body(&struct_llfields(cx, st, false, false),
693 fn generic_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
699 CEnum(ity, _, _) => ll_inttype(cx, ity),
700 RawNullablePointer { nnty, .. } => type_of::sizing_type_of(cx, nnty),
701 Univariant(ref st, _) | StructWrappedNullablePointer { nonnull: ref st, .. } => {
704 Type::struct_(cx, &struct_llfields(cx, st, sizing, dst),
707 Some(name) => { assert_eq!(sizing, false); Type::named_struct(cx, name) }
710 General(ity, ref sts, _) => {
711 // We need a representation that has:
712 // * The alignment of the most-aligned field
713 // * The size of the largest variant (rounded up to that alignment)
714 // * No alignment padding anywhere any variant has actual data
715 // (currently matters only for enums small enough to be immediate)
716 // * The discriminant in an obvious place.
718 // So we start with the discriminant, pad it up to the alignment with
719 // more of its own type, then use alignment-sized ints to get the rest
722 // FIXME #10604: this breaks when vector types are present.
723 let (size, align) = union_size_and_align(&sts[..]);
724 let align_s = align as u64;
725 assert_eq!(size % align_s, 0);
726 let align_units = size / align_s - 1;
728 let discr_ty = ll_inttype(cx, ity);
729 let discr_size = machine::llsize_of_alloc(cx, discr_ty);
730 let fill_ty = match align_s {
731 1 => Type::array(&Type::i8(cx), align_units),
732 2 => Type::array(&Type::i16(cx), align_units),
733 4 => Type::array(&Type::i32(cx), align_units),
734 8 if machine::llalign_of_min(cx, Type::i64(cx)) == 8 =>
735 Type::array(&Type::i64(cx), align_units),
736 a if a.count_ones() == 1 => Type::array(&Type::vector(&Type::i32(cx), a / 4),
738 _ => panic!("unsupported enum alignment: {}", align)
740 assert_eq!(machine::llalign_of_min(cx, fill_ty), align);
741 assert_eq!(align_s % discr_size, 0);
742 let fields = [discr_ty,
743 Type::array(&discr_ty, align_s / discr_size - 1),
746 None => Type::struct_(cx, &fields[..], false),
748 let mut llty = Type::named_struct(cx, name);
749 llty.set_struct_body(&fields[..], false);
757 fn struct_llfields<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, st: &Struct<'tcx>,
758 sizing: bool, dst: bool) -> Vec<Type> {
760 st.fields.iter().filter(|&ty| !dst || type_is_sized(cx.tcx(), *ty))
761 .map(|&ty| type_of::sizing_type_of(cx, ty)).collect()
763 st.fields.iter().map(|&ty| type_of::in_memory_type_of(cx, ty)).collect()
767 /// Obtain a representation of the discriminant sufficient to translate
768 /// destructuring; this may or may not involve the actual discriminant.
770 /// This should ideally be less tightly tied to `_match`.
771 pub fn trans_switch<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
772 r: &Repr<'tcx>, scrutinee: ValueRef)
773 -> (_match::BranchKind, Option<ValueRef>) {
775 CEnum(..) | General(..) |
776 RawNullablePointer { .. } | StructWrappedNullablePointer { .. } => {
777 (_match::Switch, Some(trans_get_discr(bcx, r, scrutinee, None)))
780 // N.B.: Univariant means <= 1 enum variants (*not* == 1 variants).
781 (_match::Single, None)
786 pub fn is_discr_signed<'tcx>(r: &Repr<'tcx>) -> bool {
788 CEnum(ity, _, _) => ity.is_signed(),
789 General(ity, _, _) => ity.is_signed(),
790 Univariant(..) => false,
791 RawNullablePointer { .. } => false,
792 StructWrappedNullablePointer { .. } => false,
796 /// Obtain the actual discriminant of a value.
797 pub fn trans_get_discr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, r: &Repr<'tcx>,
798 scrutinee: ValueRef, cast_to: Option<Type>)
800 debug!("trans_get_discr r: {:?}", r);
802 CEnum(ity, min, max) => load_discr(bcx, ity, scrutinee, min, max),
803 General(ity, ref cases, _) => {
804 let ptr = GEPi(bcx, scrutinee, &[0, 0]);
805 load_discr(bcx, ity, ptr, 0, (cases.len() - 1) as Disr)
807 Univariant(..) => C_u8(bcx.ccx(), 0),
808 RawNullablePointer { nndiscr, nnty, .. } => {
809 let cmp = if nndiscr == 0 { IntEQ } else { IntNE };
810 let llptrty = type_of::sizing_type_of(bcx.ccx(), nnty);
811 ICmp(bcx, cmp, Load(bcx, scrutinee), C_null(llptrty), DebugLoc::None)
813 StructWrappedNullablePointer { nndiscr, ref discrfield, .. } => {
814 struct_wrapped_nullable_bitdiscr(bcx, nndiscr, discrfield, scrutinee)
819 Some(llty) => if is_discr_signed(r) { SExt(bcx, val, llty) } else { ZExt(bcx, val, llty) }
823 fn struct_wrapped_nullable_bitdiscr(bcx: Block, nndiscr: Disr, discrfield: &DiscrField,
824 scrutinee: ValueRef) -> ValueRef {
825 let llptrptr = GEPi(bcx, scrutinee, &discrfield[..]);
826 let llptr = Load(bcx, llptrptr);
827 let cmp = if nndiscr == 0 { IntEQ } else { IntNE };
828 ICmp(bcx, cmp, llptr, C_null(val_ty(llptr)), DebugLoc::None)
831 /// Helper for cases where the discriminant is simply loaded.
832 fn load_discr(bcx: Block, ity: IntType, ptr: ValueRef, min: Disr, max: Disr)
834 let llty = ll_inttype(bcx.ccx(), ity);
835 assert_eq!(val_ty(ptr), llty.ptr_to());
836 let bits = machine::llbitsize_of_real(bcx.ccx(), llty);
838 let bits = bits as usize;
839 let mask = (!0u64 >> (64 - bits)) as Disr;
840 // For a (max) discr of -1, max will be `-1 as usize`, which overflows.
841 // However, that is fine here (it would still represent the full range),
842 if (max.wrapping_add(1)) & mask == min & mask {
843 // i.e., if the range is everything. The lo==hi case would be
844 // rejected by the LLVM verifier (it would mean either an
845 // empty set, which is impossible, or the entire range of the
846 // type, which is pointless).
849 // llvm::ConstantRange can deal with ranges that wrap around,
850 // so an overflow on (max + 1) is fine.
851 LoadRangeAssert(bcx, ptr, min, (max.wrapping_add(1)), /* signed: */ True)
855 /// Yield information about how to dispatch a case of the
856 /// discriminant-like value returned by `trans_switch`.
858 /// This should ideally be less tightly tied to `_match`.
859 pub fn trans_case<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, r: &Repr, discr: Disr)
860 -> _match::OptResult<'blk, 'tcx> {
862 CEnum(ity, _, _) => {
863 _match::SingleResult(Result::new(bcx, C_integral(ll_inttype(bcx.ccx(), ity),
864 discr as u64, true)))
866 General(ity, _, _) => {
867 _match::SingleResult(Result::new(bcx, C_integral(ll_inttype(bcx.ccx(), ity),
868 discr as u64, true)))
871 bcx.ccx().sess().bug("no cases for univariants or structs")
873 RawNullablePointer { .. } |
874 StructWrappedNullablePointer { .. } => {
875 assert!(discr == 0 || discr == 1);
876 _match::SingleResult(Result::new(bcx, C_bool(bcx.ccx(), discr != 0)))
881 /// Set the discriminant for a new value of the given case of the given
883 pub fn trans_set_discr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, r: &Repr<'tcx>,
884 val: ValueRef, discr: Disr) {
886 CEnum(ity, min, max) => {
887 assert_discr_in_range(ity, min, max, discr);
888 Store(bcx, C_integral(ll_inttype(bcx.ccx(), ity), discr as u64, true),
891 General(ity, ref cases, dtor) => {
892 if dtor_active(dtor) {
893 let ptr = trans_field_ptr(bcx, r, val, discr,
894 cases[discr as usize].fields.len() - 2);
895 Store(bcx, C_u8(bcx.ccx(), DTOR_NEEDED as usize), ptr);
897 Store(bcx, C_integral(ll_inttype(bcx.ccx(), ity), discr as u64, true),
898 GEPi(bcx, val, &[0, 0]));
900 Univariant(ref st, dtor) => {
901 assert_eq!(discr, 0);
902 if dtor_active(dtor) {
903 Store(bcx, C_u8(bcx.ccx(), DTOR_NEEDED as usize),
904 GEPi(bcx, val, &[0, st.fields.len() - 1]));
907 RawNullablePointer { nndiscr, nnty, ..} => {
908 if discr != nndiscr {
909 let llptrty = type_of::sizing_type_of(bcx.ccx(), nnty);
910 Store(bcx, C_null(llptrty), val);
913 StructWrappedNullablePointer { nndiscr, ref discrfield, .. } => {
914 if discr != nndiscr {
915 let llptrptr = GEPi(bcx, val, &discrfield[..]);
916 let llptrty = val_ty(llptrptr).element_type();
917 Store(bcx, C_null(llptrty), llptrptr);
923 fn assert_discr_in_range(ity: IntType, min: Disr, max: Disr, discr: Disr) {
925 attr::UnsignedInt(_) => assert!(min <= discr && discr <= max),
926 attr::SignedInt(_) => assert!(min as i64 <= discr as i64 && discr as i64 <= max as i64)
930 /// The number of fields in a given case; for use when obtaining this
931 /// information from the type or definition is less convenient.
932 pub fn num_args(r: &Repr, discr: Disr) -> usize {
935 Univariant(ref st, dtor) => {
936 assert_eq!(discr, 0);
937 st.fields.len() - (if dtor_active(dtor) { 1 } else { 0 })
939 General(_, ref cases, dtor) => {
940 cases[discr as usize].fields.len() - 1 - (if dtor_active(dtor) { 1 } else { 0 })
942 RawNullablePointer { nndiscr, ref nullfields, .. } => {
943 if discr == nndiscr { 1 } else { nullfields.len() }
945 StructWrappedNullablePointer { ref nonnull, nndiscr,
946 ref nullfields, .. } => {
947 if discr == nndiscr { nonnull.fields.len() } else { nullfields.len() }
952 /// Access a field, at a point when the value's case is known.
953 pub fn trans_field_ptr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, r: &Repr<'tcx>,
954 val: ValueRef, discr: Disr, ix: usize) -> ValueRef {
955 // Note: if this ever needs to generate conditionals (e.g., if we
956 // decide to do some kind of cdr-coding-like non-unique repr
957 // someday), it will need to return a possibly-new bcx as well.
960 bcx.ccx().sess().bug("element access in C-like enum")
962 Univariant(ref st, _dtor) => {
963 assert_eq!(discr, 0);
964 struct_field_ptr(bcx, st, val, ix, false)
966 General(_, ref cases, _) => {
967 struct_field_ptr(bcx, &cases[discr as usize], val, ix + 1, true)
969 RawNullablePointer { nndiscr, ref nullfields, .. } |
970 StructWrappedNullablePointer { nndiscr, ref nullfields, .. } if discr != nndiscr => {
971 // The unit-like case might have a nonzero number of unit-like fields.
972 // (e.d., Result of Either with (), as one side.)
973 let ty = type_of::type_of(bcx.ccx(), nullfields[ix]);
974 assert_eq!(machine::llsize_of_alloc(bcx.ccx(), ty), 0);
975 // The contents of memory at this pointer can't matter, but use
976 // the value that's "reasonable" in case of pointer comparison.
977 PointerCast(bcx, val, ty.ptr_to())
979 RawNullablePointer { nndiscr, nnty, .. } => {
981 assert_eq!(discr, nndiscr);
982 let ty = type_of::type_of(bcx.ccx(), nnty);
983 PointerCast(bcx, val, ty.ptr_to())
985 StructWrappedNullablePointer { ref nonnull, nndiscr, .. } => {
986 assert_eq!(discr, nndiscr);
987 struct_field_ptr(bcx, nonnull, val, ix, false)
992 pub fn struct_field_ptr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, st: &Struct<'tcx>, val: ValueRef,
993 ix: usize, needs_cast: bool) -> ValueRef {
994 let val = if needs_cast {
996 let fields = st.fields.iter().map(|&ty| type_of::type_of(ccx, ty)).collect::<Vec<_>>();
997 let real_ty = Type::struct_(ccx, &fields[..], st.packed);
998 PointerCast(bcx, val, real_ty.ptr_to())
1003 GEPi(bcx, val, &[0, ix])
1006 pub fn fold_variants<'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
1010 -> Block<'blk, 'tcx> where
1011 F: FnMut(Block<'blk, 'tcx>, &Struct<'tcx>, ValueRef) -> Block<'blk, 'tcx>,
1015 Univariant(ref st, _) => {
1018 General(ity, ref cases, _) => {
1019 let ccx = bcx.ccx();
1021 // See the comments in trans/base.rs for more information (inside
1022 // iter_structural_ty), but the gist here is that if the enum's
1023 // discriminant is *not* in the range that we're expecting (in which
1024 // case we'll take the fall-through branch on the switch
1025 // instruction) then we can't just optimize this to an Unreachable
1028 // Currently we still have filling drop, so this means that the drop
1029 // glue for enums may be called when the enum has been paved over
1030 // with the "I've been dropped" value. In this case the default
1031 // branch of the switch instruction will actually be taken at
1032 // runtime, so the basic block isn't actually unreachable, so we
1033 // need to make it do something with defined behavior. In this case
1034 // we just return early from the function.
1035 let ret_void_cx = fcx.new_temp_block("enum-variant-iter-ret-void");
1036 RetVoid(ret_void_cx, DebugLoc::None);
1038 let discr_val = trans_get_discr(bcx, r, value, None);
1039 let llswitch = Switch(bcx, discr_val, ret_void_cx.llbb, cases.len());
1040 let bcx_next = fcx.new_temp_block("enum-variant-iter-next");
1042 for (discr, case) in cases.iter().enumerate() {
1043 let mut variant_cx = fcx.new_temp_block(
1044 &format!("enum-variant-iter-{}", &discr.to_string())
1046 let rhs_val = C_integral(ll_inttype(ccx, ity), discr as u64, true);
1047 AddCase(llswitch, rhs_val, variant_cx.llbb);
1049 let fields = case.fields.iter().map(|&ty|
1050 type_of::type_of(bcx.ccx(), ty)).collect::<Vec<_>>();
1051 let real_ty = Type::struct_(ccx, &fields[..], case.packed);
1052 let variant_value = PointerCast(variant_cx, value, real_ty.ptr_to());
1054 variant_cx = f(variant_cx, case, variant_value);
1055 Br(variant_cx, bcx_next.llbb, DebugLoc::None);
1064 /// Access the struct drop flag, if present.
1065 pub fn trans_drop_flag_ptr<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
1068 -> datum::DatumBlock<'blk, 'tcx, datum::Expr>
1070 let tcx = bcx.tcx();
1071 let ptr_ty = bcx.tcx().mk_imm_ptr(tcx.dtor_type());
1073 Univariant(ref st, dtor) if dtor_active(dtor) => {
1074 let flag_ptr = GEPi(bcx, val, &[0, st.fields.len() - 1]);
1075 datum::immediate_rvalue_bcx(bcx, flag_ptr, ptr_ty).to_expr_datumblock()
1077 General(_, _, dtor) if dtor_active(dtor) => {
1079 let custom_cleanup_scope = fcx.push_custom_cleanup_scope();
1080 let scratch = unpack_datum!(bcx, datum::lvalue_scratch_datum(
1081 bcx, tcx.dtor_type(), "drop_flag",
1082 cleanup::CustomScope(custom_cleanup_scope), (), |_, bcx, _| bcx
1084 bcx = fold_variants(bcx, r, val, |variant_cx, st, value| {
1085 let ptr = struct_field_ptr(variant_cx, st, value, (st.fields.len() - 1), false);
1086 datum::Datum::new(ptr, ptr_ty, datum::Lvalue::new("adt::trans_drop_flag_ptr"))
1087 .store_to(variant_cx, scratch.val)
1089 let expr_datum = scratch.to_expr_datum();
1090 fcx.pop_custom_cleanup_scope(custom_cleanup_scope);
1091 datum::DatumBlock::new(bcx, expr_datum)
1093 _ => bcx.ccx().sess().bug("tried to get drop flag of non-droppable type")
1097 /// Construct a constant value, suitable for initializing a
1098 /// GlobalVariable, given a case and constant values for its fields.
1099 /// Note that this may have a different LLVM type (and different
1100 /// alignment!) from the representation's `type_of`, so it needs a
1101 /// pointer cast before use.
1103 /// The LLVM type system does not directly support unions, and only
1104 /// pointers can be bitcast, so a constant (and, by extension, the
1105 /// GlobalVariable initialized by it) will have a type that can vary
1106 /// depending on which case of an enum it is.
1108 /// To understand the alignment situation, consider `enum E { V64(u64),
1109 /// V32(u32, u32) }` on Windows. The type has 8-byte alignment to
1110 /// accommodate the u64, but `V32(x, y)` would have LLVM type `{i32,
1111 /// i32, i32}`, which is 4-byte aligned.
1113 /// Currently the returned value has the same size as the type, but
1114 /// this could be changed in the future to avoid allocating unnecessary
1115 /// space after values of shorter-than-maximum cases.
1116 pub fn trans_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, r: &Repr<'tcx>, discr: Disr,
1117 vals: &[ValueRef]) -> ValueRef {
1119 CEnum(ity, min, max) => {
1120 assert_eq!(vals.len(), 0);
1121 assert_discr_in_range(ity, min, max, discr);
1122 C_integral(ll_inttype(ccx, ity), discr as u64, true)
1124 General(ity, ref cases, _) => {
1125 let case = &cases[discr as usize];
1126 let (max_sz, _) = union_size_and_align(&cases[..]);
1127 let lldiscr = C_integral(ll_inttype(ccx, ity), discr as u64, true);
1128 let mut f = vec![lldiscr];
1130 let mut contents = build_const_struct(ccx, case, &f[..]);
1131 contents.push_all(&[padding(ccx, max_sz - case.size)]);
1132 C_struct(ccx, &contents[..], false)
1134 Univariant(ref st, _dro) => {
1135 assert!(discr == 0);
1136 let contents = build_const_struct(ccx, st, vals);
1137 C_struct(ccx, &contents[..], st.packed)
1139 RawNullablePointer { nndiscr, nnty, .. } => {
1140 if discr == nndiscr {
1141 assert_eq!(vals.len(), 1);
1144 C_null(type_of::sizing_type_of(ccx, nnty))
1147 StructWrappedNullablePointer { ref nonnull, nndiscr, .. } => {
1148 if discr == nndiscr {
1149 C_struct(ccx, &build_const_struct(ccx,
1154 let vals = nonnull.fields.iter().map(|&ty| {
1155 // Always use null even if it's not the `discrfield`th
1156 // field; see #8506.
1157 C_null(type_of::sizing_type_of(ccx, ty))
1158 }).collect::<Vec<ValueRef>>();
1159 C_struct(ccx, &build_const_struct(ccx,
1168 /// Compute struct field offsets relative to struct begin.
1169 fn compute_struct_field_offsets<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
1170 st: &Struct<'tcx>) -> Vec<u64> {
1171 let mut offsets = vec!();
1174 for &ty in &st.fields {
1175 let llty = type_of::sizing_type_of(ccx, ty);
1177 let type_align = type_of::align_of(ccx, ty);
1178 offset = roundup(offset, type_align);
1180 offsets.push(offset);
1181 offset += machine::llsize_of_alloc(ccx, llty);
1183 assert_eq!(st.fields.len(), offsets.len());
1187 /// Building structs is a little complicated, because we might need to
1188 /// insert padding if a field's value is less aligned than its type.
1190 /// Continuing the example from `trans_const`, a value of type `(u32,
1191 /// E)` should have the `E` at offset 8, but if that field's
1192 /// initializer is 4-byte aligned then simply translating the tuple as
1193 /// a two-element struct will locate it at offset 4, and accesses to it
1194 /// will read the wrong memory.
1195 fn build_const_struct<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
1196 st: &Struct<'tcx>, vals: &[ValueRef])
1198 assert_eq!(vals.len(), st.fields.len());
1200 let target_offsets = compute_struct_field_offsets(ccx, st);
1202 // offset of current value
1204 let mut cfields = Vec::new();
1205 for (&val, target_offset) in vals.iter().zip(target_offsets) {
1207 let val_align = machine::llalign_of_min(ccx, val_ty(val));
1208 offset = roundup(offset, val_align);
1210 if offset != target_offset {
1211 cfields.push(padding(ccx, target_offset - offset));
1212 offset = target_offset;
1214 assert!(!is_undef(val));
1216 offset += machine::llsize_of_alloc(ccx, val_ty(val));
1219 assert!(st.sized && offset <= st.size);
1220 if offset != st.size {
1221 cfields.push(padding(ccx, st.size - offset));
1227 fn padding(ccx: &CrateContext, size: u64) -> ValueRef {
1228 C_undef(Type::array(&Type::i8(ccx), size))
1231 // FIXME this utility routine should be somewhere more general
1233 fn roundup(x: u64, a: u32) -> u64 { let a = a as u64; ((x + (a - 1)) / a) * a }
1235 /// Get the discriminant of a constant value.
1236 pub fn const_get_discrim(ccx: &CrateContext, r: &Repr, val: ValueRef) -> Disr {
1238 CEnum(ity, _, _) => {
1240 attr::SignedInt(..) => const_to_int(val) as Disr,
1241 attr::UnsignedInt(..) => const_to_uint(val) as Disr
1244 General(ity, _, _) => {
1246 attr::SignedInt(..) => const_to_int(const_get_elt(ccx, val, &[0])) as Disr,
1247 attr::UnsignedInt(..) => const_to_uint(const_get_elt(ccx, val, &[0])) as Disr
1250 Univariant(..) => 0,
1251 RawNullablePointer { .. } | StructWrappedNullablePointer { .. } => {
1252 ccx.sess().bug("const discrim access of non c-like enum")
1257 /// Extract a field of a constant value, as appropriate for its
1260 /// (Not to be confused with `common::const_get_elt`, which operates on
1261 /// raw LLVM-level structs and arrays.)
1262 pub fn const_get_field(ccx: &CrateContext, r: &Repr, val: ValueRef,
1263 _discr: Disr, ix: usize) -> ValueRef {
1265 CEnum(..) => ccx.sess().bug("element access in C-like enum const"),
1266 Univariant(..) => const_struct_field(ccx, val, ix),
1267 General(..) => const_struct_field(ccx, val, ix + 1),
1268 RawNullablePointer { .. } => {
1272 StructWrappedNullablePointer{ .. } => const_struct_field(ccx, val, ix)
1276 /// Extract field of struct-like const, skipping our alignment padding.
1277 fn const_struct_field(ccx: &CrateContext, val: ValueRef, ix: usize) -> ValueRef {
1278 // Get the ix-th non-undef element of the struct.
1279 let mut real_ix = 0; // actual position in the struct
1280 let mut ix = ix; // logical index relative to real_ix
1284 field = const_get_elt(ccx, val, &[real_ix]);
1285 if !is_undef(field) {
1288 real_ix = real_ix + 1;
1294 real_ix = real_ix + 1;