Skip to content

Commit 541aa8a

Browse files
committed
Introduce ReprOptions, a struct for holding info from the repr attributes. This effectively deprecates lookup_repr_hints.
1 parent 324b175 commit 541aa8a

File tree

6 files changed

+77
-31
lines changed

6 files changed

+77
-31
lines changed

src/librustc/ty/context.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use middle::resolve_lifetime;
2626
use middle::stability;
2727
use mir::Mir;
2828
use ty::subst::{Kind, Substs};
29+
use ty::ReprOptions;
2930
use traits;
3031
use ty::{self, TraitRef, Ty, TypeAndMut};
3132
use ty::{TyS, TypeVariants, Slice};
@@ -672,9 +673,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
672673
pub fn alloc_adt_def(self,
673674
did: DefId,
674675
kind: AdtKind,
675-
variants: Vec<ty::VariantDef>)
676+
variants: Vec<ty::VariantDef>,
677+
repr: ReprOptions)
676678
-> &'gcx ty::AdtDef {
677-
let def = ty::AdtDef::new(self, did, kind, variants);
679+
let def = ty::AdtDef::new(self, did, kind, variants, repr);
678680
self.global_arenas.adt_def.alloc(def)
679681
}
680682

src/librustc/ty/mod.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,8 @@ pub struct AdtDef {
13271327
pub did: DefId,
13281328
pub variants: Vec<VariantDef>,
13291329
destructor: Cell<Option<DefId>>,
1330-
flags: Cell<AdtFlags>
1330+
flags: Cell<AdtFlags>,
1331+
pub repr: ReprOptions,
13311332
}
13321333

13331334
impl PartialEq for AdtDef {
@@ -1356,11 +1357,38 @@ impl<'tcx> serialize::UseSpecializedDecodable for &'tcx AdtDef {}
13561357
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
13571358
pub enum AdtKind { Struct, Union, Enum }
13581359

1360+
/// Represents the repr options provided by the user,
1361+
#[derive(Copy, Clone, Eq, PartialEq, RustcEncodable, RustcDecodable, Default)]
1362+
pub struct ReprOptions {
1363+
pub c: bool,
1364+
pub packed: bool,
1365+
pub simd: bool,
1366+
pub int: Option<attr::IntType>,
1367+
}
1368+
1369+
impl ReprOptions {
1370+
pub fn new<'a, 'gcx, 'tcx>(tcx: &TyCtxt<'a, 'gcx, 'tcx>, did: DefId) -> ReprOptions {
1371+
let mut ret = ReprOptions::default();
1372+
let attrs = tcx.lookup_repr_hints(did);
1373+
for r in attrs.iter() {
1374+
match *r {
1375+
attr::ReprExtern => ret.c = true,
1376+
attr::ReprPacked => ret.packed = true,
1377+
attr::ReprSimd => ret.simd = true,
1378+
attr::ReprInt(i) => ret.int = Some(i),
1379+
attr::ReprAny => (),
1380+
}
1381+
}
1382+
ret
1383+
}
1384+
}
1385+
13591386
impl<'a, 'gcx, 'tcx> AdtDef {
13601387
fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>,
13611388
did: DefId,
13621389
kind: AdtKind,
1363-
variants: Vec<VariantDef>) -> Self {
1390+
variants: Vec<VariantDef>,
1391+
repr: ReprOptions) -> Self {
13641392
let mut flags = AdtFlags::NO_ADT_FLAGS;
13651393
let attrs = tcx.get_attrs(did);
13661394
if attr::contains_name(&attrs, "fundamental") {
@@ -1385,6 +1413,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
13851413
variants: variants,
13861414
flags: Cell::new(flags),
13871415
destructor: Cell::new(None),
1416+
repr: repr,
13881417
}
13891418
}
13901419

src/librustc_metadata/decoder.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,8 @@ impl<'tcx> EntryKind<'tcx> {
425425
EntryKind::ForeignImmStatic => Def::Static(did, false),
426426
EntryKind::MutStatic |
427427
EntryKind::ForeignMutStatic => Def::Static(did, true),
428-
EntryKind::Struct(_) => Def::Struct(did),
429-
EntryKind::Union(_) => Def::Union(did),
428+
EntryKind::Struct(_, _) => Def::Struct(did),
429+
EntryKind::Union(_, _) => Def::Union(did),
430430
EntryKind::Fn(_) |
431431
EntryKind::ForeignFn(_) => Def::Fn(did),
432432
EntryKind::Method(_) => Def::Method(did),
@@ -435,7 +435,7 @@ impl<'tcx> EntryKind<'tcx> {
435435
EntryKind::Mod(_) => Def::Mod(did),
436436
EntryKind::Variant(_) => Def::Variant(did),
437437
EntryKind::Trait(_) => Def::Trait(did),
438-
EntryKind::Enum => Def::Enum(did),
438+
EntryKind::Enum(_) => Def::Enum(did),
439439
EntryKind::MacroDef(_) => Def::Macro(did),
440440

441441
EntryKind::ForeignMod |
@@ -519,8 +519,8 @@ impl<'a, 'tcx> CrateMetadata {
519519
-> (ty::VariantDef, Option<DefIndex>) {
520520
let data = match item.kind {
521521
EntryKind::Variant(data) |
522-
EntryKind::Struct(data) |
523-
EntryKind::Union(data) => data.decode(self),
522+
EntryKind::Struct(data, _) |
523+
EntryKind::Union(data, _) => data.decode(self),
524524
_ => bug!(),
525525
};
526526

@@ -547,7 +547,7 @@ impl<'a, 'tcx> CrateMetadata {
547547
let item = self.entry(item_id);
548548
let did = self.local_def_id(item_id);
549549
let mut ctor_index = None;
550-
let variants = if let EntryKind::Enum = item.kind {
550+
let variants = if let EntryKind::Enum(_) = item.kind {
551551
item.children
552552
.decode(self)
553553
.map(|index| {
@@ -561,14 +561,14 @@ impl<'a, 'tcx> CrateMetadata {
561561
ctor_index = struct_ctor;
562562
vec![variant]
563563
};
564-
let kind = match item.kind {
565-
EntryKind::Enum => ty::AdtKind::Enum,
566-
EntryKind::Struct(_) => ty::AdtKind::Struct,
567-
EntryKind::Union(_) => ty::AdtKind::Union,
564+
let (kind, repr) = match item.kind {
565+
EntryKind::Enum(repr) => (ty::AdtKind::Enum, repr),
566+
EntryKind::Struct(_, repr) => (ty::AdtKind::Struct, repr),
567+
EntryKind::Union(_, repr) => (ty::AdtKind::Union, repr),
568568
_ => bug!("get_adt_def called on a non-ADT {:?}", did),
569569
};
570570

571-
let adt = tcx.alloc_adt_def(did, kind, variants);
571+
let adt = tcx.alloc_adt_def(did, kind, variants, repr);
572572
if let Some(ctor_index) = ctor_index {
573573
// Make adt definition available through constructor id as well.
574574
tcx.adt_defs.borrow_mut().insert(self.local_def_id(ctor_index), adt);
@@ -881,16 +881,16 @@ impl<'a, 'tcx> CrateMetadata {
881881

882882
pub fn get_ctor_kind(&self, node_id: DefIndex) -> CtorKind {
883883
match self.entry(node_id).kind {
884-
EntryKind::Struct(data) |
885-
EntryKind::Union(data) |
884+
EntryKind::Struct(data, _) |
885+
EntryKind::Union(data, _) |
886886
EntryKind::Variant(data) => data.decode(self).ctor_kind,
887887
_ => CtorKind::Fictive,
888888
}
889889
}
890890

891891
pub fn get_struct_ctor_def_id(&self, node_id: DefIndex) -> Option<DefId> {
892892
match self.entry(node_id).kind {
893-
EntryKind::Struct(data) => {
893+
EntryKind::Struct(data, _) => {
894894
data.decode(self).struct_ctor.map(|index| self.local_def_id(index))
895895
}
896896
_ => None,

src/librustc_metadata/encoder.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc::middle::dependency_format::Linkage;
2020
use rustc::middle::lang_items;
2121
use rustc::mir;
2222
use rustc::traits::specialization_graph;
23-
use rustc::ty::{self, Ty, TyCtxt};
23+
use rustc::ty::{self, Ty, TyCtxt, ReprOptions};
2424

2525
use rustc::session::config::{self, CrateTypeProcMacro};
2626
use rustc::util::nodemap::{FxHashMap, NodeSet};
@@ -401,8 +401,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
401401
}
402402
}
403403

404+
let repr_options = get_repr_options(&tcx, adt_def_id);
405+
404406
Entry {
405-
kind: EntryKind::Struct(self.lazy(&data)),
407+
kind: EntryKind::Struct(self.lazy(&data), repr_options),
406408
visibility: self.lazy(&ctor_vis),
407409
span: self.lazy(&tcx.def_span(def_id)),
408410
attributes: LazySeq::empty(),
@@ -659,7 +661,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
659661
}
660662
hir::ItemForeignMod(_) => EntryKind::ForeignMod,
661663
hir::ItemTy(..) => EntryKind::Type,
662-
hir::ItemEnum(..) => EntryKind::Enum,
664+
hir::ItemEnum(..) => EntryKind::Enum(get_repr_options(&tcx, def_id)),
663665
hir::ItemStruct(ref struct_def, _) => {
664666
let variant = tcx.lookup_adt_def(def_id).struct_variant();
665667

@@ -671,20 +673,24 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
671673
} else {
672674
None
673675
};
676+
677+
let repr_options = get_repr_options(&tcx, def_id);
678+
674679
EntryKind::Struct(self.lazy(&VariantData {
675680
ctor_kind: variant.ctor_kind,
676681
disr: variant.disr_val.to_u128_unchecked(),
677682
struct_ctor: struct_ctor,
678-
}))
683+
}), repr_options)
679684
}
680685
hir::ItemUnion(..) => {
681686
let variant = tcx.lookup_adt_def(def_id).struct_variant();
687+
let repr_options = get_repr_options(&tcx, def_id);
682688

683689
EntryKind::Union(self.lazy(&VariantData {
684690
ctor_kind: variant.ctor_kind,
685691
disr: variant.disr_val.to_u128_unchecked(),
686692
struct_ctor: None,
687-
}))
693+
}), repr_options)
688694
}
689695
hir::ItemDefaultImpl(..) => {
690696
let data = ImplData {
@@ -1419,3 +1425,11 @@ pub fn encode_metadata<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
14191425

14201426
result
14211427
}
1428+
1429+
pub fn get_repr_options<'a, 'tcx, 'gcx>(tcx: &TyCtxt<'a, 'tcx, 'gcx>, did: DefId) -> ReprOptions {
1430+
let ty = tcx.item_type(did);
1431+
match ty.sty {
1432+
ty::TyAdt(ref def, _) => return def.repr,
1433+
_ => bug!("{} is not an ADT", ty),
1434+
}
1435+
}

src/librustc_metadata/schema.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc::middle::cstore::{DepKind, LinkagePreference, NativeLibrary};
1818
use rustc::middle::lang_items;
1919
use rustc::middle::resolve_lifetime::ObjectLifetimeDefault;
2020
use rustc::mir;
21-
use rustc::ty::{self, Ty};
21+
use rustc::ty::{self, Ty, ReprOptions};
2222
use rustc_back::PanicStrategy;
2323

2424
use rustc_serialize as serialize;
@@ -228,11 +228,11 @@ pub enum EntryKind<'tcx> {
228228
ForeignMutStatic,
229229
ForeignMod,
230230
Type,
231-
Enum,
231+
Enum(ReprOptions),
232232
Field,
233233
Variant(Lazy<VariantData>),
234-
Struct(Lazy<VariantData>),
235-
Union(Lazy<VariantData>),
234+
Struct(Lazy<VariantData>, ReprOptions),
235+
Union(Lazy<VariantData>, ReprOptions),
236236
Fn(Lazy<FnData>),
237237
ForeignFn(Lazy<FnData>),
238238
Mod(Lazy<ModData>),

src/librustc_typeck/collect.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use middle::const_val::ConstVal;
6565
use rustc_const_eval::EvalHint::UncheckedExprHint;
6666
use rustc_const_eval::{ConstContext, report_const_eval_err};
6767
use rustc::ty::subst::Substs;
68-
use rustc::ty::{ToPredicate, ImplContainer, AssociatedItemContainer, TraitContainer};
68+
use rustc::ty::{ToPredicate, ImplContainer, AssociatedItemContainer, TraitContainer, ReprOptions};
6969
use rustc::ty::{self, AdtKind, ToPolyTraitRef, Ty, TyCtxt};
7070
use rustc::ty::util::IntTypeExt;
7171
use rustc::dep_graph::DepNode;
@@ -1006,7 +1006,8 @@ fn convert_struct_def<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
10061006
let ctor_id = if !def.is_struct() { Some(ccx.tcx.hir.local_def_id(def.id())) } else { None };
10071007
let variants = vec![convert_struct_variant(ccx, ctor_id.unwrap_or(did), it.name,
10081008
ConstInt::Infer(0), def)];
1009-
let adt = ccx.tcx.alloc_adt_def(did, AdtKind::Struct, variants);
1009+
let adt = ccx.tcx.alloc_adt_def(did, AdtKind::Struct, variants,
1010+
ReprOptions::new(&ccx.tcx, did));
10101011
if let Some(ctor_id) = ctor_id {
10111012
// Make adt definition available through constructor id as well.
10121013
ccx.tcx.adt_defs.borrow_mut().insert(ctor_id, adt);
@@ -1024,7 +1025,7 @@ fn convert_union_def<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
10241025
let did = ccx.tcx.hir.local_def_id(it.id);
10251026
let variants = vec![convert_struct_variant(ccx, did, it.name, ConstInt::Infer(0), def)];
10261027

1027-
let adt = ccx.tcx.alloc_adt_def(did, AdtKind::Union, variants);
1028+
let adt = ccx.tcx.alloc_adt_def(did, AdtKind::Union, variants, ReprOptions::new(&ccx.tcx, did));
10281029
ccx.tcx.adt_defs.borrow_mut().insert(did, adt);
10291030
adt
10301031
}
@@ -1112,7 +1113,7 @@ fn convert_enum_def<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
11121113
convert_struct_variant(ccx, did, v.node.name, disr, &v.node.data)
11131114
}).collect();
11141115

1115-
let adt = tcx.alloc_adt_def(did, AdtKind::Enum, variants);
1116+
let adt = tcx.alloc_adt_def(did, AdtKind::Enum, variants, ReprOptions::new(&ccx.tcx, did));
11161117
tcx.adt_defs.borrow_mut().insert(did, adt);
11171118
adt
11181119
}

0 commit comments

Comments
 (0)