2013-02-18 16:16:21 -06:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/*!
|
|
|
|
* # Representation of Algebraic Data Types
|
|
|
|
*
|
2013-03-05 20:06:53 -06:00
|
|
|
* This module determines how to represent enums, structs, and tuples
|
|
|
|
* based on their monomorphized types; it is responsible both for
|
|
|
|
* choosing a representation and translating basic operations on
|
2013-07-01 00:42:30 -05:00
|
|
|
* values of those types. (Note: exporting the representations for
|
|
|
|
* debuggers is handled in debuginfo.rs, not here.)
|
2013-02-28 01:08:52 -06:00
|
|
|
*
|
|
|
|
* Note that the interface treats everything as a general case of an
|
|
|
|
* enum, so structs/tuples/etc. have one pseudo-variant with
|
|
|
|
* discriminant 0; i.e., as if they were a univariant enum.
|
|
|
|
*
|
|
|
|
* Having everything in one place will enable improvements to data
|
|
|
|
* structure representation; possibilities include:
|
|
|
|
*
|
|
|
|
* - User-specified alignment (e.g., cacheline-aligning parts of
|
|
|
|
* concurrently accessed data structures); LLVM can't represent this
|
|
|
|
* directly, so we'd have to insert padding fields in any structure
|
|
|
|
* that might contain one and adjust GEP indices accordingly. See
|
|
|
|
* issue #4578.
|
|
|
|
*
|
|
|
|
* - Store nested enums' discriminants in the same word. Rather, if
|
|
|
|
* some variants start with enums, and those enums representations
|
|
|
|
* have unused alignment padding between discriminant and body, the
|
|
|
|
* outer enum's discriminant can be stored there and those variants
|
|
|
|
* can start at offset 0. Kind of fancy, and might need work to
|
|
|
|
* make copies of the inner enum type cooperate, but it could help
|
|
|
|
* with `Option` or `Result` wrapped around another enum.
|
|
|
|
*
|
|
|
|
* - Tagged pointers would be neat, but given that any type can be
|
|
|
|
* used unboxed and any field can have pointers (including mutable)
|
|
|
|
* taken to it, implementing them for Rust seems difficult.
|
|
|
|
*/
|
|
|
|
|
2014-05-02 17:13:26 -05:00
|
|
|
#![allow(unsigned_negate)]
|
|
|
|
|
2014-05-27 01:56:52 -05:00
|
|
|
use std::collections::Map;
|
|
|
|
use std::num::Int;
|
2014-04-21 19:03:02 -05:00
|
|
|
use std::rc::Rc;
|
2013-02-28 01:08:52 -06:00
|
|
|
|
2014-07-07 19:58:01 -05:00
|
|
|
use llvm::{ValueRef, True, IntEQ, IntNE};
|
2014-05-13 10:35:42 -05:00
|
|
|
use middle::subst;
|
|
|
|
use middle::subst::Subst;
|
2013-02-18 16:16:21 -06:00
|
|
|
use middle::trans::_match;
|
|
|
|
use middle::trans::build::*;
|
2014-06-14 08:55:55 -05:00
|
|
|
use middle::trans::cleanup;
|
|
|
|
use middle::trans::cleanup::CleanupMethods;
|
2013-02-18 16:16:21 -06:00
|
|
|
use middle::trans::common::*;
|
2014-06-14 08:55:55 -05:00
|
|
|
use middle::trans::datum;
|
2013-02-18 16:16:21 -06:00
|
|
|
use middle::trans::machine;
|
2014-03-08 14:36:22 -06:00
|
|
|
use middle::trans::type_::Type;
|
2013-02-18 16:16:21 -06:00
|
|
|
use middle::trans::type_of;
|
|
|
|
use middle::ty;
|
2013-03-12 00:37:01 -05:00
|
|
|
use middle::ty::Disr;
|
2014-06-17 02:16:03 -05:00
|
|
|
use syntax::abi::{X86, X86_64, Arm, Mips, Mipsel};
|
2013-02-18 16:16:21 -06:00
|
|
|
use syntax::ast;
|
2013-07-01 00:42:30 -05:00
|
|
|
use syntax::attr;
|
|
|
|
use syntax::attr::IntType;
|
2014-06-21 05:39:03 -05:00
|
|
|
use util::ppaux::ty_to_string;
|
2013-02-18 16:16:21 -06:00
|
|
|
|
2013-07-01 00:42:30 -05:00
|
|
|
type Hint = attr::ReprAttr;
|
|
|
|
|
2013-02-18 16:16:21 -06:00
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/// Representations.
|
2014-08-30 09:22:19 -05:00
|
|
|
#[deriving(Eq, PartialEq)]
|
2013-02-18 16:16:21 -06:00
|
|
|
pub enum Repr {
|
2013-02-28 01:08:52 -06:00
|
|
|
/// C-like enums; basically an int.
|
2013-07-01 00:42:30 -05:00
|
|
|
CEnum(IntType, Disr, Disr), // discriminant range (signedness based on the IntType)
|
2013-03-11 00:58:44 -05:00
|
|
|
/**
|
|
|
|
* Single-case variants, and structs/tuples/records.
|
|
|
|
*
|
|
|
|
* Structs with destructors need a dynamic destroyedness flag to
|
|
|
|
* avoid running the destructor too many times; this is included
|
|
|
|
* in the `Struct` if present.
|
|
|
|
*/
|
|
|
|
Univariant(Struct, bool),
|
2013-02-28 01:08:52 -06:00
|
|
|
/**
|
2013-03-11 03:04:08 -05:00
|
|
|
* General-case enums: for each case there is a struct, and they
|
|
|
|
* all start with a field for the discriminant.
|
2014-06-14 08:55:55 -05:00
|
|
|
*
|
|
|
|
* Types with destructors need a dynamic destroyedness flag to
|
|
|
|
* avoid running the destructor too many times; the last argument
|
|
|
|
* indicates whether such a flag is present.
|
2013-02-28 01:08:52 -06:00
|
|
|
*/
|
2014-06-14 08:55:55 -05:00
|
|
|
General(IntType, Vec<Struct>, bool),
|
2014-05-15 19:55:23 -05:00
|
|
|
/**
|
|
|
|
* Two cases distinguished by a nullable pointer: the case with discriminant
|
|
|
|
* `nndiscr` must have single field which is known to be nonnull due to its type.
|
|
|
|
* The other case is known to be zero sized. Hence we represent the enum
|
|
|
|
* as simply a nullable pointer: if not null it indicates the `nndiscr` variant,
|
|
|
|
* otherwise it indicates the other case.
|
|
|
|
*/
|
|
|
|
RawNullablePointer {
|
|
|
|
pub nndiscr: Disr,
|
|
|
|
pub nnty: ty::t,
|
|
|
|
pub nullfields: Vec<ty::t>
|
|
|
|
},
|
2013-03-31 17:55:30 -05:00
|
|
|
/**
|
|
|
|
* Two cases distinguished by a nullable pointer: the case with discriminant
|
|
|
|
* `nndiscr` is represented by the struct `nonnull`, where the `ptrfield`th
|
|
|
|
* field is known to be nonnull due to its type; if that field is null, then
|
|
|
|
* it represents the other case, which is inhabited by at most one value
|
|
|
|
* (and all other fields are undefined/unused).
|
|
|
|
*
|
2013-06-28 17:32:26 -05:00
|
|
|
* For example, `std::option::Option` instantiated at a safe pointer type
|
2013-03-31 17:55:30 -05:00
|
|
|
* is represented such that `None` is a null pointer and `Some` is the
|
|
|
|
* identity function.
|
|
|
|
*/
|
2014-05-15 19:55:23 -05:00
|
|
|
StructWrappedNullablePointer {
|
2014-03-28 12:05:27 -05:00
|
|
|
pub nonnull: Struct,
|
|
|
|
pub nndiscr: Disr,
|
2014-07-03 21:26:38 -05:00
|
|
|
pub ptrfield: PointerField,
|
2014-03-28 12:05:27 -05:00
|
|
|
pub nullfields: Vec<ty::t>,
|
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/// For structs, and struct-like parts of anything fancier.
|
2014-08-30 09:22:19 -05:00
|
|
|
#[deriving(Eq, PartialEq)]
|
2013-07-11 05:51:47 -05:00
|
|
|
pub struct Struct {
|
2014-08-06 04:59:40 -05:00
|
|
|
// If the struct is DST, then the size and alignment do not take into
|
|
|
|
// account the unsized fields of the struct.
|
|
|
|
pub size: u64,
|
2014-10-14 15:36:11 -05:00
|
|
|
pub align: u32,
|
2014-08-06 04:59:40 -05:00
|
|
|
pub sized: bool,
|
2014-03-28 12:05:27 -05:00
|
|
|
pub packed: bool,
|
2014-06-14 08:55:55 -05:00
|
|
|
pub fields: Vec<ty::t>
|
2014-03-28 12:05:27 -05:00
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/**
|
|
|
|
* Convenience for `represent_type`. There should probably be more or
|
|
|
|
* these, for places in trans where the `ty::t` isn't directly
|
|
|
|
* available.
|
|
|
|
*/
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn represent_node(bcx: Block, node: ast::NodeId) -> Rc<Repr> {
|
2013-02-18 16:16:21 -06:00
|
|
|
represent_type(bcx.ccx(), node_id_type(bcx, node))
|
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/// Decides how to represent a given type.
|
2014-04-21 19:03:02 -05:00
|
|
|
pub fn represent_type(cx: &CrateContext, t: ty::t) -> Rc<Repr> {
|
2014-06-21 05:39:03 -05:00
|
|
|
debug!("Representing: {}", ty_to_string(cx.tcx(), t));
|
2014-09-05 11:18:53 -05:00
|
|
|
match cx.adt_reprs().borrow().find(&t) {
|
2014-04-21 19:03:02 -05:00
|
|
|
Some(repr) => return repr.clone(),
|
2014-03-20 21:49:20 -05:00
|
|
|
None => {}
|
2013-02-25 03:49:21 -06:00
|
|
|
}
|
2013-12-18 19:58:24 -06:00
|
|
|
|
2014-04-21 19:03:02 -05:00
|
|
|
let repr = Rc::new(represent_type_uncached(cx, t));
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("Represented as: {:?}", repr)
|
2014-09-05 11:18:53 -05:00
|
|
|
cx.adt_reprs().borrow_mut().insert(t, repr.clone());
|
2014-04-21 19:03:02 -05:00
|
|
|
repr
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
|
|
|
|
2013-12-19 18:47:15 -06:00
|
|
|
fn represent_type_uncached(cx: &CrateContext, t: ty::t) -> Repr {
|
2013-03-31 17:55:30 -05:00
|
|
|
match ty::get(t).sty {
|
2013-02-18 16:16:21 -06:00
|
|
|
ty::ty_tup(ref elems) => {
|
2014-03-08 14:36:22 -06:00
|
|
|
return Univariant(mk_struct(cx, elems.as_slice(), false), false)
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
ty::ty_struct(def_id, ref substs) => {
|
2014-03-15 15:29:34 -05:00
|
|
|
let fields = ty::lookup_struct_fields(cx.tcx(), def_id);
|
2014-03-28 14:42:34 -05:00
|
|
|
let mut ftys = fields.iter().map(|field| {
|
2014-03-15 15:29:34 -05:00
|
|
|
ty::lookup_field_type(cx.tcx(), def_id, field.id, substs)
|
2014-03-28 14:42:34 -05:00
|
|
|
}).collect::<Vec<_>>();
|
2014-03-15 15:29:34 -05:00
|
|
|
let packed = ty::lookup_packed(cx.tcx(), def_id);
|
|
|
|
let dtor = ty::ty_dtor(cx.tcx(), def_id).has_drop_flag();
|
2013-07-02 21:13:00 -05:00
|
|
|
if dtor { ftys.push(ty::mk_bool()); }
|
|
|
|
|
2014-03-08 14:36:22 -06:00
|
|
|
return Univariant(mk_struct(cx, ftys.as_slice(), packed), dtor)
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2014-07-30 00:08:39 -05:00
|
|
|
ty::ty_unboxed_closure(def_id, _) => {
|
2014-05-29 00:26:56 -05:00
|
|
|
let upvars = ty::unboxed_closure_upvars(cx.tcx(), def_id);
|
|
|
|
let upvar_types = upvars.iter().map(|u| u.ty).collect::<Vec<_>>();
|
|
|
|
return Univariant(mk_struct(cx, upvar_types.as_slice(), false),
|
|
|
|
false)
|
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
ty::ty_enum(def_id, ref substs) => {
|
2014-03-15 15:29:34 -05:00
|
|
|
let cases = get_cases(cx.tcx(), def_id, substs);
|
2014-05-27 01:56:52 -05:00
|
|
|
let hint = *ty::lookup_repr_hints(cx.tcx(), def_id).as_slice().get(0)
|
|
|
|
.unwrap_or(&attr::ReprAny);
|
2013-03-31 17:55:30 -05:00
|
|
|
|
2014-06-14 08:55:55 -05:00
|
|
|
let dtor = ty::ty_dtor(cx.tcx(), def_id).has_drop_flag();
|
|
|
|
|
2013-02-18 16:16:21 -06:00
|
|
|
if cases.len() == 0 {
|
|
|
|
// Uninhabitable; represent as unit
|
2013-08-30 01:45:06 -05:00
|
|
|
// (Typechecking will reject discriminant-sizing attrs.)
|
|
|
|
assert_eq!(hint, attr::ReprAny);
|
2014-06-14 08:55:55 -05:00
|
|
|
let ftys = if dtor { vec!(ty::mk_bool()) } else { vec!() };
|
|
|
|
return Univariant(mk_struct(cx, ftys.as_slice(), false), dtor);
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
|
|
|
|
2014-06-14 08:55:55 -05:00
|
|
|
if !dtor && cases.iter().all(|c| c.tys.len() == 0) {
|
2013-02-28 01:08:52 -06:00
|
|
|
// All bodies empty -> intlike
|
2014-03-28 14:42:34 -05:00
|
|
|
let discrs: Vec<u64> = cases.iter().map(|c| c.discr).collect();
|
2013-07-01 00:42:30 -05:00
|
|
|
let bounds = IntBounds {
|
|
|
|
ulo: *discrs.iter().min().unwrap(),
|
|
|
|
uhi: *discrs.iter().max().unwrap(),
|
|
|
|
slo: discrs.iter().map(|n| *n as i64).min().unwrap(),
|
|
|
|
shi: discrs.iter().map(|n| *n as i64).max().unwrap()
|
|
|
|
};
|
|
|
|
return mk_cenum(cx, hint, &bounds);
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Since there's at least one
|
|
|
|
// non-empty body, explicit discriminants should have
|
|
|
|
// been rejected by a checker before this point.
|
2013-03-12 00:37:01 -05:00
|
|
|
if !cases.iter().enumerate().all(|(i,c)| c.discr == (i as Disr)) {
|
2014-03-05 08:36:01 -06:00
|
|
|
cx.sess().bug(format!("non-C-like enum {} with specified \
|
|
|
|
discriminants",
|
2014-05-16 12:45:16 -05:00
|
|
|
ty::item_path_str(cx.tcx(),
|
2014-06-14 08:55:55 -05:00
|
|
|
def_id)).as_slice());
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
|
|
|
|
2013-08-30 01:45:06 -05:00
|
|
|
if cases.len() == 1 {
|
|
|
|
// Equivalent to a struct/tuple/newtype.
|
|
|
|
// (Typechecking will reject discriminant-sizing attrs.)
|
|
|
|
assert_eq!(hint, attr::ReprAny);
|
2014-06-14 08:55:55 -05:00
|
|
|
let mut ftys = cases.get(0).tys.clone();
|
|
|
|
if dtor { ftys.push(ty::mk_bool()); }
|
|
|
|
return Univariant(mk_struct(cx, ftys.as_slice(), false), dtor);
|
2013-08-30 01:45:06 -05:00
|
|
|
}
|
|
|
|
|
2014-06-14 08:55:55 -05:00
|
|
|
if !dtor && cases.len() == 2 && hint == attr::ReprAny {
|
2013-08-30 01:45:06 -05:00
|
|
|
// Nullable pointer optimization
|
2013-03-31 17:55:30 -05:00
|
|
|
let mut discr = 0;
|
|
|
|
while discr < 2 {
|
2014-03-08 14:36:22 -06:00
|
|
|
if cases.get(1 - discr).is_zerolen(cx) {
|
2014-07-03 21:26:38 -05:00
|
|
|
let st = mk_struct(cx, cases.get(discr).tys.as_slice(), false);
|
2014-03-08 14:36:22 -06:00
|
|
|
match cases.get(discr).find_ptr() {
|
2014-07-03 21:26:38 -05:00
|
|
|
Some(ThinPointer(_)) if st.fields.len() == 1 => {
|
|
|
|
return RawNullablePointer {
|
|
|
|
nndiscr: discr as Disr,
|
|
|
|
nnty: *st.fields.get(0),
|
|
|
|
nullfields: cases.get(1 - discr).tys.clone()
|
|
|
|
};
|
|
|
|
}
|
2013-03-31 17:55:30 -05:00
|
|
|
Some(ptrfield) => {
|
2014-07-03 21:26:38 -05:00
|
|
|
return StructWrappedNullablePointer {
|
|
|
|
nndiscr: discr as Disr,
|
|
|
|
nonnull: st,
|
|
|
|
ptrfield: ptrfield,
|
|
|
|
nullfields: cases.get(1 - discr).tys.clone()
|
2014-05-15 19:55:23 -05:00
|
|
|
};
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
|
|
|
None => { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
discr += 1;
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
2013-03-31 17:55:30 -05:00
|
|
|
|
|
|
|
// The general case.
|
2013-07-01 00:42:30 -05:00
|
|
|
assert!((cases.len() - 1) as i64 >= 0);
|
|
|
|
let bounds = IntBounds { ulo: 0, uhi: (cases.len() - 1) as u64,
|
|
|
|
slo: 0, shi: (cases.len() - 1) as i64 };
|
|
|
|
let ity = range_to_inttype(cx, hint, &bounds);
|
2014-06-14 08:55:55 -05:00
|
|
|
|
2014-03-28 14:42:34 -05:00
|
|
|
return General(ity, cases.iter().map(|c| {
|
2014-06-14 08:55:55 -05:00
|
|
|
let mut ftys = vec!(ty_of_inttype(ity)).append(c.tys.as_slice());
|
|
|
|
if dtor { ftys.push(ty::mk_bool()); }
|
|
|
|
mk_struct(cx, ftys.as_slice(), false)
|
|
|
|
}).collect(), dtor);
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
_ => cx.sess().bug(format!("adt::represent_type called on non-ADT type: {}",
|
2014-08-06 04:59:40 -05:00
|
|
|
ty_to_string(cx.tcx(), t)).as_slice())
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
|
2013-11-28 14:22:53 -06:00
|
|
|
// this should probably all be in ty
|
2014-07-03 21:26:38 -05:00
|
|
|
struct Case {
|
|
|
|
discr: Disr,
|
|
|
|
tys: Vec<ty::t>
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-30 09:22:19 -05:00
|
|
|
#[deriving(Eq, PartialEq, Show)]
|
2014-07-03 21:26:38 -05:00
|
|
|
pub enum PointerField {
|
|
|
|
ThinPointer(uint),
|
|
|
|
FatPointer(uint, uint)
|
|
|
|
}
|
|
|
|
|
2013-06-02 15:03:35 -05:00
|
|
|
impl Case {
|
2013-12-19 18:47:15 -06:00
|
|
|
fn is_zerolen(&self, cx: &CrateContext) -> bool {
|
2014-08-06 04:59:40 -05:00
|
|
|
mk_struct(cx, self.tys.as_slice(), false).size == 0
|
2013-06-02 15:03:35 -05:00
|
|
|
}
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
|
2014-07-03 21:26:38 -05:00
|
|
|
fn find_ptr(&self) -> Option<PointerField> {
|
|
|
|
use back::abi::{fn_field_code, slice_elt_base, trt_field_box};
|
|
|
|
|
|
|
|
for (i, &ty) in self.tys.iter().enumerate() {
|
2014-04-20 07:43:37 -05:00
|
|
|
match ty::get(ty).sty {
|
2014-09-11 20:58:01 -05:00
|
|
|
// &T/&mut T could either be a thin or fat pointer depending on T
|
|
|
|
ty::ty_rptr(_, ty::mt { ty, .. }) => match ty::get(ty).sty {
|
2014-07-03 21:26:38 -05:00
|
|
|
// &[T] and &str are a pointer and length pair
|
|
|
|
ty::ty_vec(_, None) | ty::ty_str => return Some(FatPointer(i, slice_elt_base)),
|
|
|
|
|
|
|
|
// &Trait/&mut Trait are a pair of pointers: the actual object and a vtable
|
|
|
|
ty::ty_trait(..) => return Some(FatPointer(i, trt_field_box)),
|
|
|
|
|
|
|
|
// Any other &T/&mut T is just a pointer
|
|
|
|
_ => return Some(ThinPointer(i))
|
|
|
|
},
|
|
|
|
|
|
|
|
// Box<T> could either be a thin or fat pointer depending on T
|
|
|
|
ty::ty_uniq(t) => match ty::get(t).sty {
|
2014-08-06 04:59:40 -05:00
|
|
|
ty::ty_vec(_, None) => return Some(FatPointer(i, slice_elt_base)),
|
2014-07-03 21:26:38 -05:00
|
|
|
|
|
|
|
// Box<Trait> is a pair of pointers: the actual object and a vtable
|
|
|
|
ty::ty_trait(..) => return Some(FatPointer(i, trt_field_box)),
|
|
|
|
|
|
|
|
// Any other Box<T> is just a pointer
|
|
|
|
_ => return Some(ThinPointer(i))
|
2014-04-20 07:43:37 -05:00
|
|
|
},
|
2014-07-03 21:26:38 -05:00
|
|
|
|
|
|
|
// Functions are just pointers
|
|
|
|
ty::ty_bare_fn(..) => return Some(ThinPointer(i)),
|
|
|
|
|
|
|
|
// Closures are a pair of pointers: the code and environment
|
|
|
|
ty::ty_closure(..) => return Some(FatPointer(i, fn_field_code)),
|
|
|
|
|
|
|
|
// Anything else is not a pointer
|
|
|
|
_ => continue
|
2014-04-20 07:43:37 -05:00
|
|
|
}
|
2014-07-03 21:26:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
None
|
2013-06-02 15:03:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 10:35:42 -05:00
|
|
|
fn get_cases(tcx: &ty::ctxt, def_id: ast::DefId, substs: &subst::Substs) -> Vec<Case> {
|
2014-03-28 14:42:34 -05:00
|
|
|
ty::enum_variants(tcx, def_id).iter().map(|vi| {
|
|
|
|
let arg_tys = vi.args.iter().map(|&raw_ty| {
|
2014-05-13 10:35:42 -05:00
|
|
|
raw_ty.subst(tcx, substs)
|
2014-03-28 14:42:34 -05:00
|
|
|
}).collect();
|
2013-06-02 15:03:35 -05:00
|
|
|
Case { discr: vi.disr_val, tys: arg_tys }
|
2014-03-28 14:42:34 -05:00
|
|
|
}).collect()
|
2013-06-02 15:03:35 -05:00
|
|
|
}
|
|
|
|
|
2013-12-19 18:47:15 -06:00
|
|
|
fn mk_struct(cx: &CrateContext, tys: &[ty::t], packed: bool) -> Struct {
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
if tys.iter().all(|&ty| ty::type_is_sized(cx.tcx(), ty)) {
|
|
|
|
let lltys = tys.iter().map(|&ty| type_of::sizing_type_of(cx, ty)).collect::<Vec<_>>();
|
|
|
|
let llty_rec = Type::struct_(cx, lltys.as_slice(), packed);
|
|
|
|
Struct {
|
2014-08-06 04:59:40 -05:00
|
|
|
size: machine::llsize_of_alloc(cx, llty_rec),
|
|
|
|
align: machine::llalign_of_min(cx, llty_rec),
|
|
|
|
sized: true,
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
packed: packed,
|
|
|
|
fields: Vec::from_slice(tys),
|
|
|
|
}
|
|
|
|
} else {
|
2014-08-06 04:59:40 -05:00
|
|
|
// Ignore any dynamically sized fields.
|
|
|
|
let lltys = tys.iter().filter(|&ty| ty::type_is_sized(cx.tcx(), *ty))
|
|
|
|
.map(|&ty| type_of::sizing_type_of(cx, ty)).collect::<Vec<_>>();
|
|
|
|
let llty_rec = Type::struct_(cx, lltys.as_slice(), packed);
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
Struct {
|
2014-08-06 04:59:40 -05:00
|
|
|
size: machine::llsize_of_alloc(cx, llty_rec),
|
|
|
|
align: machine::llalign_of_min(cx, llty_rec),
|
|
|
|
sized: false,
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
packed: packed,
|
|
|
|
fields: Vec::from_slice(tys),
|
2014-08-06 04:59:40 -05:00
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-01 00:42:30 -05:00
|
|
|
struct IntBounds {
|
|
|
|
slo: i64,
|
|
|
|
shi: i64,
|
|
|
|
ulo: u64,
|
|
|
|
uhi: u64
|
|
|
|
}
|
|
|
|
|
2013-12-19 18:47:15 -06:00
|
|
|
fn mk_cenum(cx: &CrateContext, hint: Hint, bounds: &IntBounds) -> Repr {
|
2013-07-01 00:42:30 -05:00
|
|
|
let it = range_to_inttype(cx, hint, bounds);
|
|
|
|
match it {
|
|
|
|
attr::SignedInt(_) => CEnum(it, bounds.slo as Disr, bounds.shi as Disr),
|
|
|
|
attr::UnsignedInt(_) => CEnum(it, bounds.ulo, bounds.uhi)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 18:47:15 -06:00
|
|
|
fn range_to_inttype(cx: &CrateContext, hint: Hint, bounds: &IntBounds) -> IntType {
|
2013-07-01 00:42:30 -05:00
|
|
|
debug!("range_to_inttype: {:?} {:?}", hint, bounds);
|
|
|
|
// Lists of sizes to try. u64 is always allowed as a fallback.
|
2014-09-12 20:55:37 -05:00
|
|
|
#[allow(non_uppercase_statics)]
|
2013-07-01 00:42:30 -05:00
|
|
|
static choose_shortest: &'static[IntType] = &[
|
2014-01-09 07:05:33 -06:00
|
|
|
attr::UnsignedInt(ast::TyU8), attr::SignedInt(ast::TyI8),
|
|
|
|
attr::UnsignedInt(ast::TyU16), attr::SignedInt(ast::TyI16),
|
|
|
|
attr::UnsignedInt(ast::TyU32), attr::SignedInt(ast::TyI32)];
|
2014-09-12 20:55:37 -05:00
|
|
|
#[allow(non_uppercase_statics)]
|
2013-07-01 00:42:30 -05:00
|
|
|
static at_least_32: &'static[IntType] = &[
|
2014-01-09 07:05:33 -06:00
|
|
|
attr::UnsignedInt(ast::TyU32), attr::SignedInt(ast::TyI32)];
|
2013-07-01 00:42:30 -05:00
|
|
|
|
|
|
|
let attempts;
|
|
|
|
match hint {
|
|
|
|
attr::ReprInt(span, ity) => {
|
|
|
|
if !bounds_usable(cx, ity, bounds) {
|
2014-03-05 08:36:01 -06:00
|
|
|
cx.sess().span_bug(span, "representation hint insufficient for discriminant range")
|
2013-07-01 00:42:30 -05:00
|
|
|
}
|
|
|
|
return ity;
|
|
|
|
}
|
|
|
|
attr::ReprExtern => {
|
2014-03-05 08:36:01 -06:00
|
|
|
attempts = match cx.sess().targ_cfg.arch {
|
2013-07-01 00:42:30 -05:00
|
|
|
X86 | X86_64 => at_least_32,
|
|
|
|
// WARNING: the ARM EABI has two variants; the one corresponding to `at_least_32`
|
|
|
|
// appears to be used on Linux and NetBSD, but some systems may use the variant
|
|
|
|
// corresponding to `choose_shortest`. However, we don't run on those yet...?
|
|
|
|
Arm => at_least_32,
|
|
|
|
Mips => at_least_32,
|
2014-06-17 02:16:03 -05:00
|
|
|
Mipsel => at_least_32,
|
2013-07-01 00:42:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
attr::ReprAny => {
|
|
|
|
attempts = choose_shortest;
|
2014-05-27 01:56:52 -05:00
|
|
|
},
|
|
|
|
attr::ReprPacked => {
|
2014-09-05 11:18:53 -05:00
|
|
|
cx.tcx().sess.bug("range_to_inttype: found ReprPacked on an enum");
|
2013-07-01 00:42:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for &ity in attempts.iter() {
|
|
|
|
if bounds_usable(cx, ity, bounds) {
|
2013-10-03 15:58:01 -05:00
|
|
|
return ity;
|
2013-07-01 00:42:30 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
return attr::UnsignedInt(ast::TyU64);
|
2013-07-01 00:42:30 -05:00
|
|
|
}
|
|
|
|
|
2013-12-19 18:47:15 -06:00
|
|
|
pub fn ll_inttype(cx: &CrateContext, ity: IntType) -> Type {
|
2013-07-01 00:42:30 -05:00
|
|
|
match ity {
|
|
|
|
attr::SignedInt(t) => Type::int_from_ty(cx, t),
|
|
|
|
attr::UnsignedInt(t) => Type::uint_from_ty(cx, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 18:47:15 -06:00
|
|
|
fn bounds_usable(cx: &CrateContext, ity: IntType, bounds: &IntBounds) -> bool {
|
2013-07-01 00:42:30 -05:00
|
|
|
debug!("bounds_usable: {:?} {:?}", ity, bounds);
|
|
|
|
match ity {
|
|
|
|
attr::SignedInt(_) => {
|
|
|
|
let lllo = C_integral(ll_inttype(cx, ity), bounds.slo as u64, true);
|
|
|
|
let llhi = C_integral(ll_inttype(cx, ity), bounds.shi as u64, true);
|
|
|
|
bounds.slo == const_to_int(lllo) as i64 && bounds.shi == const_to_int(llhi) as i64
|
|
|
|
}
|
|
|
|
attr::UnsignedInt(_) => {
|
|
|
|
let lllo = C_integral(ll_inttype(cx, ity), bounds.ulo, false);
|
|
|
|
let llhi = C_integral(ll_inttype(cx, ity), bounds.uhi, false);
|
|
|
|
bounds.ulo == const_to_uint(lllo) as u64 && bounds.uhi == const_to_uint(llhi) as u64
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-25 11:41:10 -05:00
|
|
|
pub fn ty_of_inttype(ity: IntType) -> ty::t {
|
2013-07-01 00:42:30 -05:00
|
|
|
match ity {
|
|
|
|
attr::SignedInt(t) => ty::mk_mach_int(t),
|
|
|
|
attr::UnsignedInt(t) => ty::mk_mach_uint(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-30 01:45:06 -05:00
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/**
|
2013-11-22 03:16:17 -06:00
|
|
|
* LLVM-level types are a little complicated.
|
|
|
|
*
|
|
|
|
* C-like enums need to be actual ints, not wrapped in a struct,
|
|
|
|
* because that changes the ABI on some platforms (see issue #10308).
|
|
|
|
*
|
|
|
|
* For nominal types, in some cases, we need to use LLVM named structs
|
|
|
|
* and fill in the actual contents in a second pass to prevent
|
|
|
|
* unbounded recursion; see also the comments in `trans::type_of`.
|
2013-02-28 01:08:52 -06:00
|
|
|
*/
|
2013-12-19 18:47:15 -06:00
|
|
|
pub fn type_of(cx: &CrateContext, r: &Repr) -> Type {
|
2014-08-06 04:59:40 -05:00
|
|
|
generic_type_of(cx, r, None, false, false)
|
2013-11-22 03:16:17 -06:00
|
|
|
}
|
2014-08-06 04:59:40 -05:00
|
|
|
// Pass dst=true if the type you are passing is a DST. Yes, we could figure
|
|
|
|
// this out, but if you call this on an unsized type without realising it, you
|
|
|
|
// are going to get the wrong type (it will not include the unsized parts of it).
|
|
|
|
pub fn sizing_type_of(cx: &CrateContext, r: &Repr, dst: bool) -> Type {
|
|
|
|
generic_type_of(cx, r, None, true, dst)
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2013-12-19 18:47:15 -06:00
|
|
|
pub fn incomplete_type_of(cx: &CrateContext, r: &Repr, name: &str) -> Type {
|
2014-08-06 04:59:40 -05:00
|
|
|
generic_type_of(cx, r, Some(name), false, false)
|
2013-11-22 03:16:17 -06:00
|
|
|
}
|
2013-12-19 18:47:15 -06:00
|
|
|
pub fn finish_type_of(cx: &CrateContext, r: &Repr, llty: &mut Type) {
|
2013-11-22 03:16:17 -06:00
|
|
|
match *r {
|
2014-05-15 19:55:23 -05:00
|
|
|
CEnum(..) | General(..) | RawNullablePointer { .. } => { }
|
|
|
|
Univariant(ref st, _) | StructWrappedNullablePointer { nonnull: ref st, .. } =>
|
2014-08-06 04:59:40 -05:00
|
|
|
llty.set_struct_body(struct_llfields(cx, st, false, false).as_slice(),
|
2014-03-08 14:36:22 -06:00
|
|
|
st.packed)
|
2013-11-22 03:16:17 -06:00
|
|
|
}
|
2013-02-28 01:08:52 -06:00
|
|
|
}
|
2013-11-22 03:16:17 -06:00
|
|
|
|
2014-08-06 04:59:40 -05:00
|
|
|
fn generic_type_of(cx: &CrateContext,
|
|
|
|
r: &Repr,
|
|
|
|
name: Option<&str>,
|
|
|
|
sizing: bool,
|
|
|
|
dst: bool) -> Type {
|
2013-02-18 16:16:21 -06:00
|
|
|
match *r {
|
2013-11-22 03:16:17 -06:00
|
|
|
CEnum(ity, _, _) => ll_inttype(cx, ity),
|
2014-05-15 19:55:23 -05:00
|
|
|
RawNullablePointer { nnty, .. } => type_of::sizing_type_of(cx, nnty),
|
|
|
|
Univariant(ref st, _) | StructWrappedNullablePointer { nonnull: ref st, .. } => {
|
2013-11-22 03:16:17 -06:00
|
|
|
match name {
|
2014-03-08 14:36:22 -06:00
|
|
|
None => {
|
2014-08-06 04:59:40 -05:00
|
|
|
Type::struct_(cx, struct_llfields(cx, st, sizing, dst).as_slice(),
|
2014-03-08 14:36:22 -06:00
|
|
|
st.packed)
|
|
|
|
}
|
2014-03-15 15:29:34 -05:00
|
|
|
Some(name) => { assert_eq!(sizing, false); Type::named_struct(cx, name) }
|
2013-11-22 03:16:17 -06:00
|
|
|
}
|
|
|
|
}
|
2014-06-14 08:55:55 -05:00
|
|
|
General(ity, ref sts, _) => {
|
2013-10-26 14:12:53 -05:00
|
|
|
// We need a representation that has:
|
|
|
|
// * The alignment of the most-aligned field
|
|
|
|
// * The size of the largest variant (rounded up to that alignment)
|
|
|
|
// * No alignment padding anywhere any variant has actual data
|
|
|
|
// (currently matters only for enums small enough to be immediate)
|
|
|
|
// * The discriminant in an obvious place.
|
|
|
|
//
|
|
|
|
// So we start with the discriminant, pad it up to the alignment with
|
|
|
|
// more of its own type, then use alignment-sized ints to get the rest
|
|
|
|
// of the size.
|
|
|
|
//
|
2013-11-22 03:16:17 -06:00
|
|
|
// FIXME #10604: this breaks when vector types are present.
|
2014-08-06 04:59:40 -05:00
|
|
|
let size = sts.iter().map(|st| st.size).max().unwrap();
|
|
|
|
let most_aligned = sts.iter().max_by(|st| st.align).unwrap();
|
|
|
|
let align = most_aligned.align;
|
2013-10-26 14:12:53 -05:00
|
|
|
let discr_ty = ll_inttype(cx, ity);
|
|
|
|
let discr_size = machine::llsize_of_alloc(cx, discr_ty) as u64;
|
2014-01-21 23:12:41 -06:00
|
|
|
let align_units = (size + align - 1) / align - 1;
|
2013-10-26 14:12:53 -05:00
|
|
|
let pad_ty = match align {
|
2014-03-15 15:29:34 -05:00
|
|
|
1 => Type::array(&Type::i8(cx), align_units),
|
|
|
|
2 => Type::array(&Type::i16(cx), align_units),
|
|
|
|
4 => Type::array(&Type::i32(cx), align_units),
|
|
|
|
8 if machine::llalign_of_min(cx, Type::i64(cx)) == 8 =>
|
|
|
|
Type::array(&Type::i64(cx), align_units),
|
|
|
|
a if a.count_ones() == 1 => Type::array(&Type::vector(&Type::i32(cx), a / 4),
|
2014-01-21 23:12:41 -06:00
|
|
|
align_units),
|
2014-02-06 03:38:08 -06:00
|
|
|
_ => fail!("unsupported enum alignment: {:?}", align)
|
2013-10-26 14:12:53 -05:00
|
|
|
};
|
|
|
|
assert_eq!(machine::llalign_of_min(cx, pad_ty) as u64, align);
|
|
|
|
assert_eq!(align % discr_size, 0);
|
2014-03-04 12:02:49 -06:00
|
|
|
let fields = vec!(discr_ty,
|
2014-01-21 23:12:41 -06:00
|
|
|
Type::array(&discr_ty, align / discr_size - 1),
|
2014-03-04 12:02:49 -06:00
|
|
|
pad_ty);
|
2013-11-22 03:16:17 -06:00
|
|
|
match name {
|
2014-03-15 15:29:34 -05:00
|
|
|
None => Type::struct_(cx, fields.as_slice(), false),
|
2013-11-22 03:16:17 -06:00
|
|
|
Some(name) => {
|
2014-03-15 15:29:34 -05:00
|
|
|
let mut llty = Type::named_struct(cx, name);
|
2014-03-08 14:36:22 -06:00
|
|
|
llty.set_struct_body(fields.as_slice(), false);
|
2013-11-22 03:16:17 -06:00
|
|
|
llty
|
|
|
|
}
|
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-06 04:59:40 -05:00
|
|
|
fn struct_llfields(cx: &CrateContext, st: &Struct, sizing: bool, dst: bool) -> Vec<Type> {
|
2013-03-11 03:04:08 -05:00
|
|
|
if sizing {
|
2014-08-06 04:59:40 -05:00
|
|
|
st.fields.iter().filter(|&ty| !dst || ty::type_is_sized(cx.tcx(), *ty))
|
|
|
|
.map(|&ty| type_of::sizing_type_of(cx, ty)).collect()
|
2013-03-11 03:04:08 -05:00
|
|
|
} else {
|
2014-03-28 14:42:34 -05:00
|
|
|
st.fields.iter().map(|&ty| type_of::type_of(cx, ty)).collect()
|
2013-03-11 03:04:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/**
|
2013-02-28 14:13:00 -06:00
|
|
|
* Obtain a representation of the discriminant sufficient to translate
|
|
|
|
* destructuring; this may or may not involve the actual discriminant.
|
|
|
|
*
|
2013-02-28 01:08:52 -06:00
|
|
|
* This should ideally be less tightly tied to `_match`.
|
|
|
|
*/
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn trans_switch(bcx: Block, r: &Repr, scrutinee: ValueRef)
|
2014-08-30 09:22:19 -05:00
|
|
|
-> (_match::BranchKind, Option<ValueRef>) {
|
2013-02-18 16:16:21 -06:00
|
|
|
match *r {
|
2014-05-15 19:55:23 -05:00
|
|
|
CEnum(..) | General(..) |
|
|
|
|
RawNullablePointer { .. } | StructWrappedNullablePointer { .. } => {
|
2014-08-30 09:22:19 -05:00
|
|
|
(_match::Switch, Some(trans_get_discr(bcx, r, scrutinee, None)))
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
Univariant(..) => {
|
2014-08-30 09:22:19 -05:00
|
|
|
(_match::Single, None)
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2013-02-28 13:43:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-31 17:55:30 -05:00
|
|
|
|
|
|
|
|
2013-02-28 14:13:00 -06:00
|
|
|
/// Obtain the actual discriminant of a value.
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn trans_get_discr(bcx: Block, r: &Repr, scrutinee: ValueRef, cast_to: Option<Type>)
|
2013-02-28 13:43:20 -06:00
|
|
|
-> ValueRef {
|
2013-07-01 00:42:30 -05:00
|
|
|
let signed;
|
|
|
|
let val;
|
2013-02-28 13:43:20 -06:00
|
|
|
match *r {
|
2013-07-01 00:42:30 -05:00
|
|
|
CEnum(ity, min, max) => {
|
|
|
|
val = load_discr(bcx, ity, scrutinee, min, max);
|
|
|
|
signed = ity.is_signed();
|
|
|
|
}
|
2014-06-14 08:55:55 -05:00
|
|
|
General(ity, ref cases, _) => {
|
2013-11-22 03:16:17 -06:00
|
|
|
let ptr = GEPi(bcx, scrutinee, [0, 0]);
|
|
|
|
val = load_discr(bcx, ity, ptr, 0, (cases.len() - 1) as Disr);
|
2013-07-01 00:42:30 -05:00
|
|
|
signed = ity.is_signed();
|
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
Univariant(..) => {
|
2014-03-15 15:29:34 -05:00
|
|
|
val = C_u8(bcx.ccx(), 0);
|
2013-07-01 00:42:30 -05:00
|
|
|
signed = false;
|
|
|
|
}
|
2014-05-15 19:55:23 -05:00
|
|
|
RawNullablePointer { nndiscr, nnty, .. } => {
|
|
|
|
let cmp = if nndiscr == 0 { IntEQ } else { IntNE };
|
|
|
|
let llptrty = type_of::sizing_type_of(bcx.ccx(), nnty);
|
|
|
|
val = ICmp(bcx, cmp, Load(bcx, scrutinee), C_null(llptrty));
|
|
|
|
signed = false;
|
|
|
|
}
|
2014-07-03 21:26:38 -05:00
|
|
|
StructWrappedNullablePointer { nndiscr, ptrfield, .. } => {
|
|
|
|
val = struct_wrapped_nullable_bitdiscr(bcx, nndiscr, ptrfield, scrutinee);
|
2013-07-01 00:42:30 -05:00
|
|
|
signed = false;
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2013-07-01 00:42:30 -05:00
|
|
|
match cast_to {
|
|
|
|
None => val,
|
|
|
|
Some(llty) => if signed { SExt(bcx, val, llty) } else { ZExt(bcx, val, llty) }
|
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
|
2014-09-06 11:13:04 -05:00
|
|
|
fn struct_wrapped_nullable_bitdiscr(bcx: Block, nndiscr: Disr, ptrfield: PointerField,
|
2014-05-15 19:55:23 -05:00
|
|
|
scrutinee: ValueRef) -> ValueRef {
|
2014-07-03 21:26:38 -05:00
|
|
|
let llptrptr = match ptrfield {
|
|
|
|
ThinPointer(field) => GEPi(bcx, scrutinee, [0, field]),
|
|
|
|
FatPointer(field, pair) => GEPi(bcx, scrutinee, [0, field, pair])
|
|
|
|
};
|
|
|
|
let llptr = Load(bcx, llptrptr);
|
2013-03-31 17:55:30 -05:00
|
|
|
let cmp = if nndiscr == 0 { IntEQ } else { IntNE };
|
2014-07-03 21:26:38 -05:00
|
|
|
ICmp(bcx, cmp, llptr, C_null(val_ty(llptr)))
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
|
|
|
|
2013-02-28 14:13:00 -06:00
|
|
|
/// Helper for cases where the discriminant is simply loaded.
|
2014-09-06 11:13:04 -05:00
|
|
|
fn load_discr(bcx: Block, ity: IntType, ptr: ValueRef, min: Disr, max: Disr)
|
2013-02-28 13:24:30 -06:00
|
|
|
-> ValueRef {
|
2013-07-01 00:42:30 -05:00
|
|
|
let llty = ll_inttype(bcx.ccx(), ity);
|
|
|
|
assert_eq!(val_ty(ptr), llty.ptr_to());
|
|
|
|
let bits = machine::llbitsize_of_real(bcx.ccx(), llty);
|
|
|
|
assert!(bits <= 64);
|
2014-04-21 16:58:52 -05:00
|
|
|
let bits = bits as uint;
|
2013-07-01 00:42:30 -05:00
|
|
|
let mask = (-1u64 >> (64 - bits)) as Disr;
|
|
|
|
if (max + 1) & mask == min & mask {
|
2013-02-28 13:24:30 -06:00
|
|
|
// i.e., if the range is everything. The lo==hi case would be
|
|
|
|
// rejected by the LLVM verifier (it would mean either an
|
|
|
|
// empty set, which is impossible, or the entire range of the
|
|
|
|
// type, which is pointless).
|
|
|
|
Load(bcx, ptr)
|
|
|
|
} else {
|
|
|
|
// llvm::ConstantRange can deal with ranges that wrap around,
|
|
|
|
// so an overflow on (max + 1) is fine.
|
2014-10-14 15:36:11 -05:00
|
|
|
LoadRangeAssert(bcx, ptr, min, (max+1), /* signed: */ True)
|
2013-02-28 13:24:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/**
|
|
|
|
* Yield information about how to dispatch a case of the
|
|
|
|
* discriminant-like value returned by `trans_switch`.
|
2013-02-28 14:13:00 -06:00
|
|
|
*
|
2013-02-28 01:08:52 -06:00
|
|
|
* This should ideally be less tightly tied to `_match`.
|
|
|
|
*/
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn trans_case<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, r: &Repr, discr: Disr)
|
|
|
|
-> _match::OptResult<'blk, 'tcx> {
|
2013-02-18 16:16:21 -06:00
|
|
|
match *r {
|
2013-07-01 00:42:30 -05:00
|
|
|
CEnum(ity, _, _) => {
|
2014-08-30 09:22:19 -05:00
|
|
|
_match::SingleResult(Result::new(bcx, C_integral(ll_inttype(bcx.ccx(), ity),
|
2014-05-03 06:14:56 -05:00
|
|
|
discr as u64, true)))
|
2013-07-01 00:42:30 -05:00
|
|
|
}
|
2014-06-14 08:55:55 -05:00
|
|
|
General(ity, _, _) => {
|
2014-08-30 09:22:19 -05:00
|
|
|
_match::SingleResult(Result::new(bcx, C_integral(ll_inttype(bcx.ccx(), ity),
|
2014-05-03 06:14:56 -05:00
|
|
|
discr as u64, true)))
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
Univariant(..) => {
|
2014-03-05 08:36:01 -06:00
|
|
|
bcx.ccx().sess().bug("no cases for univariants or structs")
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2014-05-15 19:55:23 -05:00
|
|
|
RawNullablePointer { .. } |
|
|
|
|
StructWrappedNullablePointer { .. } => {
|
2013-03-31 17:55:30 -05:00
|
|
|
assert!(discr == 0 || discr == 1);
|
2014-08-30 09:22:19 -05:00
|
|
|
_match::SingleResult(Result::new(bcx, C_bool(bcx.ccx(), discr != 0)))
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/**
|
2014-07-10 17:09:21 -05:00
|
|
|
* Set the discriminant for a new value of the given case of the given
|
|
|
|
* representation.
|
2013-02-28 01:08:52 -06:00
|
|
|
*/
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn trans_set_discr(bcx: Block, r: &Repr, val: ValueRef, discr: Disr) {
|
2013-02-18 16:16:21 -06:00
|
|
|
match *r {
|
2013-07-01 00:42:30 -05:00
|
|
|
CEnum(ity, min, max) => {
|
|
|
|
assert_discr_in_range(ity, min, max, discr);
|
|
|
|
Store(bcx, C_integral(ll_inttype(bcx.ccx(), ity), discr as u64, true),
|
2013-11-22 03:16:17 -06:00
|
|
|
val)
|
2013-07-01 00:42:30 -05:00
|
|
|
}
|
2014-06-14 08:55:55 -05:00
|
|
|
General(ity, ref cases, dtor) => {
|
|
|
|
if dtor {
|
|
|
|
let ptr = trans_field_ptr(bcx, r, val, discr,
|
|
|
|
cases.get(discr as uint).fields.len() - 2);
|
|
|
|
Store(bcx, C_u8(bcx.ccx(), 1), ptr);
|
|
|
|
}
|
2013-07-01 00:42:30 -05:00
|
|
|
Store(bcx, C_integral(ll_inttype(bcx.ccx(), ity), discr as u64, true),
|
|
|
|
GEPi(bcx, val, [0, 0]))
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2014-06-14 08:55:55 -05:00
|
|
|
Univariant(ref st, dtor) => {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(discr, 0);
|
2014-06-14 08:55:55 -05:00
|
|
|
if dtor {
|
|
|
|
Store(bcx, C_u8(bcx.ccx(), 1),
|
|
|
|
GEPi(bcx, val, [0, st.fields.len() - 1]));
|
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2014-05-15 19:55:23 -05:00
|
|
|
RawNullablePointer { nndiscr, nnty, ..} => {
|
2013-03-31 17:55:30 -05:00
|
|
|
if discr != nndiscr {
|
2014-05-15 19:55:23 -05:00
|
|
|
let llptrty = type_of::sizing_type_of(bcx.ccx(), nnty);
|
|
|
|
Store(bcx, C_null(llptrty), val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StructWrappedNullablePointer { nonnull: ref nonnull, nndiscr, ptrfield, .. } => {
|
|
|
|
if discr != nndiscr {
|
2014-07-03 21:26:38 -05:00
|
|
|
let (llptrptr, llptrty) = match ptrfield {
|
|
|
|
ThinPointer(field) =>
|
|
|
|
(GEPi(bcx, val, [0, field]),
|
|
|
|
type_of::type_of(bcx.ccx(), *nonnull.fields.get(field))),
|
|
|
|
FatPointer(field, pair) => {
|
|
|
|
let v = GEPi(bcx, val, [0, field, pair]);
|
|
|
|
(v, val_ty(v).element_type())
|
|
|
|
}
|
|
|
|
};
|
2013-03-31 17:55:30 -05:00
|
|
|
Store(bcx, C_null(llptrty), llptrptr)
|
|
|
|
}
|
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-01 00:42:30 -05:00
|
|
|
fn assert_discr_in_range(ity: IntType, min: Disr, max: Disr, discr: Disr) {
|
|
|
|
match ity {
|
|
|
|
attr::UnsignedInt(_) => assert!(min <= discr && discr <= max),
|
|
|
|
attr::SignedInt(_) => assert!(min as i64 <= discr as i64 && discr as i64 <= max as i64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/**
|
|
|
|
* The number of fields in a given case; for use when obtaining this
|
|
|
|
* information from the type or definition is less convenient.
|
|
|
|
*/
|
2013-03-12 00:37:01 -05:00
|
|
|
pub fn num_args(r: &Repr, discr: Disr) -> uint {
|
2013-02-18 16:16:21 -06:00
|
|
|
match *r {
|
2013-11-28 14:22:53 -06:00
|
|
|
CEnum(..) => 0,
|
2013-03-11 00:58:44 -05:00
|
|
|
Univariant(ref st, dtor) => {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(discr, 0);
|
2013-03-11 00:58:44 -05:00
|
|
|
st.fields.len() - (if dtor { 1 } else { 0 })
|
|
|
|
}
|
2014-06-14 08:55:55 -05:00
|
|
|
General(_, ref cases, dtor) => {
|
|
|
|
cases.get(discr as uint).fields.len() - 1 - (if dtor { 1 } else { 0 })
|
|
|
|
}
|
2014-05-15 19:55:23 -05:00
|
|
|
RawNullablePointer { nndiscr, ref nullfields, .. } => {
|
|
|
|
if discr == nndiscr { 1 } else { nullfields.len() }
|
|
|
|
}
|
|
|
|
StructWrappedNullablePointer { nonnull: ref nonnull, nndiscr,
|
|
|
|
nullfields: ref nullfields, .. } => {
|
2013-04-30 13:36:22 -05:00
|
|
|
if discr == nndiscr { nonnull.fields.len() } else { nullfields.len() }
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/// Access a field, at a point when the value's case is known.
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn trans_field_ptr(bcx: Block, r: &Repr, val: ValueRef, discr: Disr,
|
2013-03-02 18:08:49 -06:00
|
|
|
ix: uint) -> ValueRef {
|
2013-02-18 16:16:21 -06:00
|
|
|
// Note: if this ever needs to generate conditionals (e.g., if we
|
|
|
|
// decide to do some kind of cdr-coding-like non-unique repr
|
2013-02-28 01:08:52 -06:00
|
|
|
// someday), it will need to return a possibly-new bcx as well.
|
2013-02-18 16:16:21 -06:00
|
|
|
match *r {
|
2013-11-28 14:22:53 -06:00
|
|
|
CEnum(..) => {
|
2014-03-05 08:36:01 -06:00
|
|
|
bcx.ccx().sess().bug("element access in C-like enum")
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2013-03-11 00:58:44 -05:00
|
|
|
Univariant(ref st, _dtor) => {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(discr, 0);
|
2013-03-02 18:08:49 -06:00
|
|
|
struct_field_ptr(bcx, st, val, ix, false)
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2014-06-14 08:55:55 -05:00
|
|
|
General(_, ref cases, _) => {
|
2014-03-08 14:36:22 -06:00
|
|
|
struct_field_ptr(bcx, cases.get(discr as uint), val, ix + 1, true)
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2014-05-15 19:55:23 -05:00
|
|
|
RawNullablePointer { nndiscr, ref nullfields, .. } |
|
|
|
|
StructWrappedNullablePointer { nndiscr, ref nullfields, .. } if discr != nndiscr => {
|
|
|
|
// The unit-like case might have a nonzero number of unit-like fields.
|
|
|
|
// (e.d., Result of Either with (), as one side.)
|
|
|
|
let ty = type_of::type_of(bcx.ccx(), *nullfields.get(ix));
|
|
|
|
assert_eq!(machine::llsize_of_alloc(bcx.ccx(), ty), 0);
|
|
|
|
// The contents of memory at this pointer can't matter, but use
|
2014-06-08 23:00:52 -05:00
|
|
|
// the value that's "reasonable" in case of pointer comparison.
|
2014-05-15 19:55:23 -05:00
|
|
|
PointerCast(bcx, val, ty.ptr_to())
|
|
|
|
}
|
|
|
|
RawNullablePointer { nndiscr, nnty, .. } => {
|
|
|
|
assert_eq!(ix, 0);
|
|
|
|
assert_eq!(discr, nndiscr);
|
|
|
|
let ty = type_of::type_of(bcx.ccx(), nnty);
|
|
|
|
PointerCast(bcx, val, ty.ptr_to())
|
|
|
|
}
|
|
|
|
StructWrappedNullablePointer { ref nonnull, nndiscr, .. } => {
|
|
|
|
assert_eq!(discr, nndiscr);
|
|
|
|
struct_field_ptr(bcx, nonnull, val, ix, false)
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn struct_field_ptr(bcx: Block, st: &Struct, val: ValueRef,
|
2014-06-14 08:55:55 -05:00
|
|
|
ix: uint, needs_cast: bool) -> ValueRef {
|
2013-02-23 00:03:59 -06:00
|
|
|
let val = if needs_cast {
|
2014-06-14 08:55:55 -05:00
|
|
|
let ccx = bcx.ccx();
|
2014-03-28 14:42:34 -05:00
|
|
|
let fields = st.fields.iter().map(|&ty| type_of::type_of(ccx, ty)).collect::<Vec<_>>();
|
2014-03-15 15:29:34 -05:00
|
|
|
let real_ty = Type::struct_(ccx, fields.as_slice(), st.packed);
|
2013-06-16 05:52:44 -05:00
|
|
|
PointerCast(bcx, val, real_ty.ptr_to())
|
2013-02-23 00:03:59 -06:00
|
|
|
} else {
|
|
|
|
val
|
|
|
|
};
|
2013-02-18 16:16:21 -06:00
|
|
|
|
2013-02-23 00:03:59 -06:00
|
|
|
GEPi(bcx, val, [0, ix])
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn fold_variants<'blk, 'tcx>(
|
|
|
|
bcx: Block<'blk, 'tcx>, r: &Repr, value: ValueRef,
|
|
|
|
f: |Block<'blk, 'tcx>, &Struct, ValueRef| -> Block<'blk, 'tcx>)
|
|
|
|
-> Block<'blk, 'tcx> {
|
2014-06-14 08:55:55 -05:00
|
|
|
let fcx = bcx.fcx;
|
|
|
|
match *r {
|
|
|
|
Univariant(ref st, _) => {
|
|
|
|
f(bcx, st, value)
|
|
|
|
}
|
|
|
|
General(ity, ref cases, _) => {
|
|
|
|
let ccx = bcx.ccx();
|
|
|
|
let unr_cx = fcx.new_temp_block("enum-variant-iter-unr");
|
|
|
|
Unreachable(unr_cx);
|
|
|
|
|
|
|
|
let discr_val = trans_get_discr(bcx, r, value, None);
|
|
|
|
let llswitch = Switch(bcx, discr_val, unr_cx.llbb, cases.len());
|
|
|
|
let bcx_next = fcx.new_temp_block("enum-variant-iter-next");
|
|
|
|
|
|
|
|
for (discr, case) in cases.iter().enumerate() {
|
|
|
|
let mut variant_cx = fcx.new_temp_block(
|
|
|
|
format!("enum-variant-iter-{}", discr.to_string()).as_slice()
|
|
|
|
);
|
|
|
|
let rhs_val = C_integral(ll_inttype(ccx, ity), discr as u64, true);
|
|
|
|
AddCase(llswitch, rhs_val, variant_cx.llbb);
|
|
|
|
|
|
|
|
let fields = case.fields.iter().map(|&ty|
|
|
|
|
type_of::type_of(bcx.ccx(), ty)).collect::<Vec<_>>();
|
|
|
|
let real_ty = Type::struct_(ccx, fields.as_slice(), case.packed);
|
|
|
|
let variant_value = PointerCast(variant_cx, value, real_ty.ptr_to());
|
|
|
|
|
|
|
|
variant_cx = f(variant_cx, case, variant_value);
|
|
|
|
Br(variant_cx, bcx_next.llbb);
|
|
|
|
}
|
|
|
|
|
|
|
|
bcx_next
|
|
|
|
}
|
|
|
|
_ => unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/// Access the struct drop flag, if present.
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn trans_drop_flag_ptr<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>, r: &Repr, val: ValueRef)
|
|
|
|
-> datum::DatumBlock<'blk, 'tcx, datum::Expr> {
|
2014-06-14 08:55:55 -05:00
|
|
|
let ptr_ty = ty::mk_imm_ptr(bcx.tcx(), ty::mk_bool());
|
2013-02-25 01:20:08 -06:00
|
|
|
match *r {
|
2014-06-14 08:55:55 -05:00
|
|
|
Univariant(ref st, true) => {
|
|
|
|
let flag_ptr = GEPi(bcx, val, [0, st.fields.len() - 1]);
|
|
|
|
datum::immediate_rvalue_bcx(bcx, flag_ptr, ptr_ty).to_expr_datumblock()
|
|
|
|
}
|
|
|
|
General(_, _, true) => {
|
|
|
|
let fcx = bcx.fcx;
|
|
|
|
let custom_cleanup_scope = fcx.push_custom_cleanup_scope();
|
|
|
|
let scratch = unpack_datum!(bcx, datum::lvalue_scratch_datum(
|
|
|
|
bcx, ty::mk_bool(), "drop_flag", false,
|
|
|
|
cleanup::CustomScope(custom_cleanup_scope), (), |_, bcx, _| bcx
|
|
|
|
));
|
|
|
|
bcx = fold_variants(bcx, r, val, |variant_cx, st, value| {
|
|
|
|
let ptr = struct_field_ptr(variant_cx, st, value, (st.fields.len() - 1), false);
|
|
|
|
datum::Datum::new(ptr, ptr_ty, datum::Rvalue::new(datum::ByRef))
|
|
|
|
.store_to(variant_cx, scratch.val)
|
|
|
|
});
|
|
|
|
let expr_datum = scratch.to_expr_datum();
|
|
|
|
fcx.pop_custom_cleanup_scope(custom_cleanup_scope);
|
|
|
|
datum::DatumBlock::new(bcx, expr_datum)
|
|
|
|
}
|
2014-03-05 08:36:01 -06:00
|
|
|
_ => bcx.ccx().sess().bug("tried to get drop flag of non-droppable type")
|
2013-02-25 01:20:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/**
|
|
|
|
* Construct a constant value, suitable for initializing a
|
|
|
|
* GlobalVariable, given a case and constant values for its fields.
|
|
|
|
* Note that this may have a different LLVM type (and different
|
|
|
|
* alignment!) from the representation's `type_of`, so it needs a
|
|
|
|
* pointer cast before use.
|
|
|
|
*
|
2013-03-02 16:03:41 -06:00
|
|
|
* The LLVM type system does not directly support unions, and only
|
|
|
|
* pointers can be bitcast, so a constant (and, by extension, the
|
|
|
|
* GlobalVariable initialized by it) will have a type that can vary
|
|
|
|
* depending on which case of an enum it is.
|
|
|
|
*
|
|
|
|
* To understand the alignment situation, consider `enum E { V64(u64),
|
2014-08-23 03:12:59 -05:00
|
|
|
* V32(u32, u32) }` on Windows. The type has 8-byte alignment to
|
2013-03-11 03:04:08 -05:00
|
|
|
* accommodate the u64, but `V32(x, y)` would have LLVM type `{i32,
|
|
|
|
* i32, i32}`, which is 4-byte aligned.
|
2013-03-02 16:03:41 -06:00
|
|
|
*
|
|
|
|
* Currently the returned value has the same size as the type, but
|
2013-03-11 03:04:08 -05:00
|
|
|
* this could be changed in the future to avoid allocating unnecessary
|
2013-03-02 16:03:41 -06:00
|
|
|
* space after values of shorter-than-maximum cases.
|
2013-02-28 01:08:52 -06:00
|
|
|
*/
|
2013-12-19 18:47:15 -06:00
|
|
|
pub fn trans_const(ccx: &CrateContext, r: &Repr, discr: Disr,
|
2013-02-18 16:16:21 -06:00
|
|
|
vals: &[ValueRef]) -> ValueRef {
|
|
|
|
match *r {
|
2013-07-01 00:42:30 -05:00
|
|
|
CEnum(ity, min, max) => {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(vals.len(), 0);
|
2013-07-01 00:42:30 -05:00
|
|
|
assert_discr_in_range(ity, min, max, discr);
|
|
|
|
C_integral(ll_inttype(ccx, ity), discr as u64, true)
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2014-06-14 08:55:55 -05:00
|
|
|
General(ity, ref cases, _) => {
|
2014-03-08 14:36:22 -06:00
|
|
|
let case = cases.get(discr as uint);
|
2014-08-06 04:59:40 -05:00
|
|
|
let max_sz = cases.iter().map(|x| x.size).max().unwrap();
|
2013-07-01 00:42:30 -05:00
|
|
|
let lldiscr = C_integral(ll_inttype(ccx, ity), discr as u64, true);
|
2014-03-08 14:36:22 -06:00
|
|
|
let contents = build_const_struct(ccx,
|
|
|
|
case,
|
2014-03-30 22:53:26 -05:00
|
|
|
(vec!(lldiscr)).append(vals).as_slice());
|
2014-08-06 04:59:40 -05:00
|
|
|
C_struct(ccx, contents.append([padding(ccx, max_sz - case.size)]).as_slice(),
|
2014-03-08 14:36:22 -06:00
|
|
|
false)
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
2013-07-01 00:42:30 -05:00
|
|
|
Univariant(ref st, _dro) => {
|
|
|
|
assert!(discr == 0);
|
|
|
|
let contents = build_const_struct(ccx, st, vals);
|
2014-03-15 15:29:34 -05:00
|
|
|
C_struct(ccx, contents.as_slice(), st.packed)
|
2013-07-01 00:42:30 -05:00
|
|
|
}
|
2014-05-15 19:55:23 -05:00
|
|
|
RawNullablePointer { nndiscr, nnty, .. } => {
|
2014-05-11 20:56:53 -05:00
|
|
|
if discr == nndiscr {
|
|
|
|
assert_eq!(vals.len(), 1);
|
|
|
|
vals[0]
|
|
|
|
} else {
|
2014-05-15 19:55:23 -05:00
|
|
|
C_null(type_of::sizing_type_of(ccx, nnty))
|
2014-05-11 20:56:53 -05:00
|
|
|
}
|
|
|
|
}
|
2014-05-15 19:55:23 -05:00
|
|
|
StructWrappedNullablePointer { nonnull: ref nonnull, nndiscr, .. } => {
|
2013-03-31 17:55:30 -05:00
|
|
|
if discr == nndiscr {
|
2014-03-15 15:29:34 -05:00
|
|
|
C_struct(ccx, build_const_struct(ccx,
|
|
|
|
nonnull,
|
|
|
|
vals).as_slice(),
|
2014-03-08 14:36:22 -06:00
|
|
|
false)
|
2013-03-31 17:55:30 -05:00
|
|
|
} else {
|
2014-03-28 14:42:34 -05:00
|
|
|
let vals = nonnull.fields.iter().map(|&ty| {
|
2014-03-04 22:41:21 -06:00
|
|
|
// Always use null even if it's not the `ptrfield`th
|
|
|
|
// field; see #8506.
|
|
|
|
C_null(type_of::sizing_type_of(ccx, ty))
|
2014-03-28 14:42:34 -05:00
|
|
|
}).collect::<Vec<ValueRef>>();
|
2014-03-15 15:29:34 -05:00
|
|
|
C_struct(ccx, build_const_struct(ccx,
|
|
|
|
nonnull,
|
|
|
|
vals.as_slice()).as_slice(),
|
2014-03-08 14:36:22 -06:00
|
|
|
false)
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-12 13:56:34 -05:00
|
|
|
/**
|
|
|
|
* Compute struct field offsets relative to struct begin.
|
|
|
|
*/
|
|
|
|
fn compute_struct_field_offsets(ccx: &CrateContext, st: &Struct) -> Vec<u64> {
|
|
|
|
let mut offsets = vec!();
|
|
|
|
|
|
|
|
let mut offset = 0;
|
|
|
|
for &ty in st.fields.iter() {
|
|
|
|
let llty = type_of::sizing_type_of(ccx, ty);
|
|
|
|
if !st.packed {
|
2014-10-14 15:36:11 -05:00
|
|
|
let type_align = type_of::align_of(ccx, ty);
|
2014-04-12 13:56:34 -05:00
|
|
|
offset = roundup(offset, type_align);
|
|
|
|
}
|
|
|
|
offsets.push(offset);
|
2014-10-14 15:36:11 -05:00
|
|
|
offset += machine::llsize_of_alloc(ccx, llty);
|
2014-04-12 13:56:34 -05:00
|
|
|
}
|
|
|
|
assert_eq!(st.fields.len(), offsets.len());
|
|
|
|
offsets
|
|
|
|
}
|
|
|
|
|
2013-02-28 13:24:30 -06:00
|
|
|
/**
|
|
|
|
* Building structs is a little complicated, because we might need to
|
|
|
|
* insert padding if a field's value is less aligned than its type.
|
2013-03-02 16:03:41 -06:00
|
|
|
*
|
|
|
|
* Continuing the example from `trans_const`, a value of type `(u32,
|
|
|
|
* E)` should have the `E` at offset 8, but if that field's
|
|
|
|
* initializer is 4-byte aligned then simply translating the tuple as
|
|
|
|
* a two-element struct will locate it at offset 4, and accesses to it
|
|
|
|
* will read the wrong memory.
|
2013-02-28 13:24:30 -06:00
|
|
|
*/
|
2013-12-19 18:47:15 -06:00
|
|
|
fn build_const_struct(ccx: &CrateContext, st: &Struct, vals: &[ValueRef])
|
2014-03-04 12:02:49 -06:00
|
|
|
-> Vec<ValueRef> {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(vals.len(), st.fields.len());
|
2013-02-18 16:16:21 -06:00
|
|
|
|
2014-04-12 13:56:34 -05:00
|
|
|
let target_offsets = compute_struct_field_offsets(ccx, st);
|
|
|
|
|
|
|
|
// offset of current value
|
2013-02-18 16:16:21 -06:00
|
|
|
let mut offset = 0;
|
2014-03-04 12:02:49 -06:00
|
|
|
let mut cfields = Vec::new();
|
2014-04-12 13:56:34 -05:00
|
|
|
for (&val, &target_offset) in vals.iter().zip(target_offsets.iter()) {
|
|
|
|
if !st.packed {
|
2014-10-14 15:36:11 -05:00
|
|
|
let val_align = machine::llalign_of_min(ccx, val_ty(val));
|
2014-04-12 13:56:34 -05:00
|
|
|
offset = roundup(offset, val_align);
|
|
|
|
}
|
2014-01-19 02:21:14 -06:00
|
|
|
if offset != target_offset {
|
2014-03-15 15:29:34 -05:00
|
|
|
cfields.push(padding(ccx, target_offset - offset));
|
2013-02-18 16:16:21 -06:00
|
|
|
offset = target_offset;
|
|
|
|
}
|
2014-04-12 13:56:34 -05:00
|
|
|
assert!(!is_undef(val));
|
|
|
|
cfields.push(val);
|
2014-10-14 15:36:11 -05:00
|
|
|
offset += machine::llsize_of_alloc(ccx, val_ty(val));
|
2014-04-12 13:56:34 -05:00
|
|
|
}
|
|
|
|
|
2014-08-06 04:59:40 -05:00
|
|
|
assert!(st.sized && offset <= st.size);
|
|
|
|
if offset != st.size {
|
|
|
|
cfields.push(padding(ccx, st.size - offset));
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
|
2014-04-12 13:56:34 -05:00
|
|
|
cfields
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
|
2014-03-15 15:29:34 -05:00
|
|
|
fn padding(ccx: &CrateContext, size: u64) -> ValueRef {
|
|
|
|
C_undef(Type::array(&Type::i8(ccx), size))
|
2013-02-28 13:24:30 -06:00
|
|
|
}
|
|
|
|
|
2014-01-26 02:43:42 -06:00
|
|
|
// FIXME this utility routine should be somewhere more general
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2014-10-14 15:36:11 -05:00
|
|
|
fn roundup(x: u64, a: u32) -> u64 { let a = a as u64; ((x + (a - 1)) / a) * a }
|
2013-02-18 16:16:21 -06:00
|
|
|
|
2013-02-28 01:08:52 -06:00
|
|
|
/// Get the discriminant of a constant value. (Not currently used.)
|
2013-12-19 18:47:15 -06:00
|
|
|
pub fn const_get_discrim(ccx: &CrateContext, r: &Repr, val: ValueRef)
|
2013-03-12 00:37:01 -05:00
|
|
|
-> Disr {
|
2013-02-18 16:16:21 -06:00
|
|
|
match *r {
|
2013-07-01 00:42:30 -05:00
|
|
|
CEnum(ity, _, _) => {
|
|
|
|
match ity {
|
2013-11-28 14:22:53 -06:00
|
|
|
attr::SignedInt(..) => const_to_int(val) as Disr,
|
|
|
|
attr::UnsignedInt(..) => const_to_uint(val) as Disr
|
2013-07-01 00:42:30 -05:00
|
|
|
}
|
|
|
|
}
|
2014-06-14 08:55:55 -05:00
|
|
|
General(ity, _, _) => {
|
2013-07-01 00:42:30 -05:00
|
|
|
match ity {
|
2013-11-28 14:22:53 -06:00
|
|
|
attr::SignedInt(..) => const_to_int(const_get_elt(ccx, val, [0])) as Disr,
|
|
|
|
attr::UnsignedInt(..) => const_to_uint(const_get_elt(ccx, val, [0])) as Disr
|
2013-07-01 00:42:30 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
Univariant(..) => 0,
|
2014-05-15 19:55:23 -05:00
|
|
|
RawNullablePointer { nndiscr, .. } => {
|
2014-05-11 20:56:53 -05:00
|
|
|
if is_null(val) {
|
|
|
|
/* subtraction as uint is ok because nndiscr is either 0 or 1 */
|
|
|
|
(1 - nndiscr) as Disr
|
|
|
|
} else {
|
|
|
|
nndiscr
|
|
|
|
}
|
|
|
|
}
|
2014-05-15 19:55:23 -05:00
|
|
|
StructWrappedNullablePointer { nndiscr, ptrfield, .. } => {
|
2014-07-03 21:26:38 -05:00
|
|
|
let (idx, sub_idx) = match ptrfield {
|
|
|
|
ThinPointer(field) => (field, None),
|
|
|
|
FatPointer(field, pair) => (field, Some(pair))
|
|
|
|
};
|
|
|
|
if is_null(const_struct_field(ccx, val, idx, sub_idx)) {
|
2013-07-23 10:52:44 -05:00
|
|
|
/* subtraction as uint is ok because nndiscr is either 0 or 1 */
|
2013-03-12 00:37:01 -05:00
|
|
|
(1 - nndiscr) as Disr
|
2013-07-23 10:52:44 -05:00
|
|
|
} else {
|
|
|
|
nndiscr
|
|
|
|
}
|
2013-03-31 17:55:30 -05:00
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 18:08:49 -06:00
|
|
|
/**
|
|
|
|
* Extract a field of a constant value, as appropriate for its
|
|
|
|
* representation.
|
|
|
|
*
|
|
|
|
* (Not to be confused with `common::const_get_elt`, which operates on
|
|
|
|
* raw LLVM-level structs and arrays.)
|
|
|
|
*/
|
2013-12-19 18:47:15 -06:00
|
|
|
pub fn const_get_field(ccx: &CrateContext, r: &Repr, val: ValueRef,
|
2013-03-12 00:37:01 -05:00
|
|
|
_discr: Disr, ix: uint) -> ValueRef {
|
2013-02-18 16:16:21 -06:00
|
|
|
match *r {
|
2014-03-05 08:36:01 -06:00
|
|
|
CEnum(..) => ccx.sess().bug("element access in C-like enum const"),
|
2014-07-03 21:26:38 -05:00
|
|
|
Univariant(..) => const_struct_field(ccx, val, ix, None),
|
|
|
|
General(..) => const_struct_field(ccx, val, ix + 1, None),
|
2014-05-15 19:55:23 -05:00
|
|
|
RawNullablePointer { .. } => {
|
2014-05-11 20:56:53 -05:00
|
|
|
assert_eq!(ix, 0);
|
|
|
|
val
|
|
|
|
}
|
2014-07-03 21:26:38 -05:00
|
|
|
StructWrappedNullablePointer{ .. } => const_struct_field(ccx, val, ix, None)
|
2013-02-18 16:16:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 13:24:30 -06:00
|
|
|
/// Extract field of struct-like const, skipping our alignment padding.
|
2014-07-03 21:26:38 -05:00
|
|
|
fn const_struct_field(ccx: &CrateContext, val: ValueRef, ix: uint, sub_idx: Option<uint>)
|
2013-02-18 16:16:21 -06:00
|
|
|
-> ValueRef {
|
|
|
|
// Get the ix-th non-undef element of the struct.
|
|
|
|
let mut real_ix = 0; // actual position in the struct
|
|
|
|
let mut ix = ix; // logical index relative to real_ix
|
|
|
|
let mut field;
|
|
|
|
loop {
|
|
|
|
loop {
|
2014-07-03 21:26:38 -05:00
|
|
|
field = match sub_idx {
|
|
|
|
Some(si) => const_get_elt(ccx, val, [real_ix, si as u32]),
|
|
|
|
None => const_get_elt(ccx, val, [real_ix])
|
|
|
|
};
|
2013-02-18 16:16:21 -06:00
|
|
|
if !is_undef(field) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
real_ix = real_ix + 1;
|
|
|
|
}
|
|
|
|
if ix == 0 {
|
|
|
|
return field;
|
|
|
|
}
|
|
|
|
ix = ix - 1;
|
|
|
|
real_ix = real_ix + 1;
|
|
|
|
}
|
|
|
|
}
|