Skip to content

Commit

Permalink
remove ty::{VariantInfo, FieldTy}
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariel Ben-Yehuda authored and arielb1 committed Aug 6, 2015
1 parent 49e7432 commit 3494233
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 212 deletions.
13 changes: 2 additions & 11 deletions src/librustc/metadata/csearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,6 @@ pub fn maybe_get_item_ast<'tcx>(tcx: &ty::ctxt<'tcx>, def: ast::DefId,
decoder::maybe_get_item_ast(&*cdata, tcx, def.node, decode_inlined_item)
}

pub fn get_enum_variants<'tcx>(tcx: &ty::ctxt<'tcx>, def: ast::DefId)
-> Vec<Rc<ty::VariantInfo<'tcx>>> {
let cstore = &tcx.sess.cstore;
let cdata = cstore.get_crate_data(def.krate);
decoder::get_enum_variants(cstore.intr.clone(), &*cdata, def.node, tcx)
}

/// Returns information about the given implementation.
pub fn get_impl_items(cstore: &cstore::CStore, impl_def_id: ast::DefId)
-> Vec<ty::ImplOrTraitItemId> {
Expand Down Expand Up @@ -195,11 +188,9 @@ pub fn get_item_attrs(cstore: &cstore::CStore,
decoder::get_item_attrs(&*cdata, def_id.node)
}

pub fn get_struct_fields(cstore: &cstore::CStore,
def: ast::DefId)
-> Vec<ty::FieldTy> {
pub fn get_struct_field_names(cstore: &cstore::CStore, def: ast::DefId) -> Vec<ast::Name> {
let cdata = cstore.get_crate_data(def.krate);
decoder::get_struct_fields(cstore.intr.clone(), &*cdata, def.node)
decoder::get_struct_field_names(&cstore.intr, &*cdata, def.node)
}

pub fn get_struct_field_attrs(cstore: &cstore::CStore, def: ast::DefId) -> HashMap<ast::NodeId,
Expand Down
84 changes: 6 additions & 78 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,55 +794,6 @@ pub fn maybe_get_item_ast<'tcx>(cdata: Cmd, tcx: &ty::ctxt<'tcx>, id: ast::NodeI
}
}

pub fn get_enum_variants<'tcx>(intr: Rc<IdentInterner>, cdata: Cmd, id: ast::NodeId,
tcx: &ty::ctxt<'tcx>) -> Vec<Rc<ty::VariantInfo<'tcx>>> {
let data = cdata.data();
let items = reader::get_doc(rbml::Doc::new(data), tag_items);
let item = find_item(id, items);
let mut disr_val = 0;
reader::tagged_docs(item, tag_items_data_item_variant).map(|p| {
let did = translated_def_id(cdata, p);
let item = find_item(did.node, items);
let ctor_ty = item_type(ast::DefId { krate: cdata.cnum, node: id},
item, tcx, cdata);
let name = item_name(&*intr, item);
let (ctor_ty, arg_tys, arg_names) = match ctor_ty.sty {
ty::TyBareFn(_, ref f) =>
(Some(ctor_ty), f.sig.0.inputs.clone(), None),
_ => { // Nullary or struct enum variant.
let mut arg_names = Vec::new();
let arg_tys = get_struct_fields(intr.clone(), cdata, did.node)
.iter()
.map(|field_ty| {
arg_names.push(field_ty.name);
get_type(cdata, field_ty.id.node, tcx).ty
})
.collect();
let arg_names = if arg_names.is_empty() { None } else { Some(arg_names) };

(None, arg_tys, arg_names)
}
};
match variant_disr_val(item) {
Some(val) => { disr_val = val; }
_ => { /* empty */ }
}
let old_disr_val = disr_val;
disr_val = disr_val.wrapping_add(1);
Rc::new(ty::VariantInfo {
args: arg_tys,
arg_names: arg_names,
ctor_ty: ctor_ty,
name: name,
// I'm not even sure if we encode visibility
// for variants -- TEST -- tjc
id: did,
disr_val: old_disr_val,
vis: ast::Inherited
})
}).collect()
}

fn get_explicit_self(item: rbml::Doc) -> ty::ExplicitSelfCategory {
fn get_mutability(ch: u8) -> ast::Mutability {
match ch as char {
Expand Down Expand Up @@ -1136,37 +1087,14 @@ fn struct_field_family_to_visibility(family: Family) -> ast::Visibility {
}
}

pub fn get_struct_fields(intr: Rc<IdentInterner>, cdata: Cmd, id: ast::NodeId)
-> Vec<ty::FieldTy> {
pub fn get_struct_field_names(intr: &IdentInterner, cdata: Cmd, id: ast::NodeId)
-> Vec<ast::Name> {
let data = cdata.data();
let item = lookup_item(id, data);
reader::tagged_docs(item, tag_item_field).filter_map(|an_item| {
let f = item_family(an_item);
if f == PublicField || f == InheritedField {
let name = item_name(&*intr, an_item);
let did = item_def_id(an_item, cdata);
let tagdoc = reader::get_doc(an_item, tag_item_field_origin);
let origin_id = translated_def_id(cdata, tagdoc);
Some(ty::FieldTy {
name: name,
id: did,
vis: struct_field_family_to_visibility(f),
origin: origin_id,
})
} else {
None
}
}).chain(reader::tagged_docs(item, tag_item_unnamed_field).map(|an_item| {
let did = item_def_id(an_item, cdata);
let tagdoc = reader::get_doc(an_item, tag_item_field_origin);
let f = item_family(an_item);
let origin_id = translated_def_id(cdata, tagdoc);
ty::FieldTy {
name: special_idents::unnamed_field.name,
id: did,
vis: struct_field_family_to_visibility(f),
origin: origin_id,
}
reader::tagged_docs(item, tag_item_field).map(|an_item| {
item_name(intr, an_item)
}).chain(reader::tagged_docs(item, tag_item_unnamed_field).map(|_| {
special_idents::unnamed_field.name
})).collect()
}

Expand Down
112 changes: 1 addition & 111 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ use std::collections::{HashMap, HashSet};
use rustc_data_structures::ivar;
use syntax::abi;
use syntax::ast::{CrateNum, DefId, ItemImpl, ItemTrait, LOCAL_CRATE};
use syntax::ast::{MutImmutable, MutMutable, Name, NamedField, NodeId};
use syntax::ast::{StructField, UnnamedField, Visibility};
use syntax::ast::{MutImmutable, MutMutable, Name, NodeId, Visibility};
use syntax::ast_util::{self, is_local, local_def};
use syntax::attr::{self, AttrMetaMethods, SignedInt, UnsignedInt};
use syntax::codemap::Span;
Expand All @@ -112,83 +111,6 @@ pub struct CrateAnalysis {
pub glob_map: Option<GlobMap>,
}

#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Field<'tcx> {
pub name: ast::Name,
pub mt: TypeAndMut<'tcx>
}

// Enum information
#[derive(Clone)]
pub struct VariantInfo<'tcx> {
pub args: Vec<Ty<'tcx>>,
pub arg_names: Option<Vec<ast::Name>>,
pub ctor_ty: Option<Ty<'tcx>>,
pub name: ast::Name,
pub id: ast::DefId,
pub disr_val: Disr,
pub vis: Visibility
}

impl<'tcx> VariantInfo<'tcx> {

/// Creates a new VariantInfo from the corresponding ast representation.
///
/// Does not do any caching of the value in the type context.
pub fn from_ast_variant(cx: &ctxt<'tcx>,
ast_variant: &ast::Variant,
discriminant: Disr) -> VariantInfo<'tcx> {
let ctor_ty = cx.node_id_to_type(ast_variant.node.id);

match ast_variant.node.kind {
ast::TupleVariantKind(ref args) => {
let arg_tys = if !args.is_empty() {
// the regions in the argument types come from the
// enum def'n, and hence will all be early bound
cx.no_late_bound_regions(&ctor_ty.fn_args()).unwrap()
} else {
Vec::new()
};

return VariantInfo {
args: arg_tys,
arg_names: None,
ctor_ty: Some(ctor_ty),
name: ast_variant.node.name.name,
id: ast_util::local_def(ast_variant.node.id),
disr_val: discriminant,
vis: ast_variant.node.vis
};
},
ast::StructVariantKind(ref struct_def) => {
let fields: &[StructField] = &struct_def.fields;

assert!(!fields.is_empty());

let arg_tys = struct_def.fields.iter()
.map(|field| cx.node_id_to_type(field.node.id)).collect();
let arg_names = fields.iter().map(|field| {
match field.node.kind {
NamedField(ident, _) => ident.name,
UnnamedField(..) => cx.sess.bug(
"enum_variants: all fields in struct must have a name")
}
}).collect();

return VariantInfo {
args: arg_tys,
arg_names: Some(arg_names),
ctor_ty: None,
name: ast_variant.node.name.name,
id: ast_util::local_def(ast_variant.node.id),
disr_val: discriminant,
vis: ast_variant.node.vis
};
}
}
}
}

#[derive(Copy, Clone)]
pub enum DtorKind {
NoDtor,
Expand Down Expand Up @@ -495,14 +417,6 @@ pub struct TypeAndMut<'tcx> {
pub mutbl: ast::Mutability,
}

#[derive(Clone, Copy, Debug)]
pub struct FieldTy {
pub name: Name,
pub id: DefId,
pub vis: ast::Visibility,
pub origin: ast::DefId, // The DefId of the struct in which the field is declared.
}

#[derive(Clone, PartialEq, RustcDecodable, RustcEncodable)]
pub struct ItemVariances {
pub types: VecPerParamSpace<Variance>,
Expand Down Expand Up @@ -5667,18 +5581,6 @@ impl<'tcx> ctxt<'tcx> {
}
}

pub fn field_idx_strict(&self, name: ast::Name, fields: &[Field<'tcx>])
-> usize {
let mut i = 0;
for f in fields { if f.name == name { return i; } i += 1; }
self.sess.bug(&format!(
"no field named `{}` found in the list of fields `{:?}`",
name,
fields.iter()
.map(|f| f.name.to_string())
.collect::<Vec<String>>()));
}

pub fn note_and_explain_type_err(&self, err: &TypeError<'tcx>, sp: Span) {
use self::TypeError::*;

Expand Down Expand Up @@ -7341,12 +7243,6 @@ impl<'tcx> HasTypeFlags for FnSig<'tcx> {
}
}

impl<'tcx> HasTypeFlags for Field<'tcx> {
fn has_type_flags(&self, flags: TypeFlags) -> bool {
self.mt.ty.has_type_flags(flags)
}
}

impl<'tcx> HasTypeFlags for BareFnTy<'tcx> {
fn has_type_flags(&self, flags: TypeFlags) -> bool {
self.sig.has_type_flags(flags)
Expand Down Expand Up @@ -7377,12 +7273,6 @@ impl<'tcx> fmt::Debug for ClosureUpvar<'tcx> {
}
}

impl<'tcx> fmt::Debug for Field<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "field({},{})", self.name, self.mt)
}
}

impl<'a, 'tcx> fmt::Debug for ParameterEnvironment<'a, 'tcx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ParameterEnvironment(\
Expand Down
9 changes: 0 additions & 9 deletions src/librustc/middle/ty_fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,6 @@ impl<'tcx> TypeFoldable<'tcx> for ty::TraitRef<'tcx> {
}
}

impl<'tcx> TypeFoldable<'tcx> for ty::Field<'tcx> {
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::Field<'tcx> {
ty::Field {
name: self.name,
mt: self.mt.fold_with(folder),
}
}
}

impl<'tcx> TypeFoldable<'tcx> for ty::Region {
fn fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> ty::Region {
folder.fold_region(*self)
Expand Down
4 changes: 1 addition & 3 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,9 +791,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
crate) building type and value for {}",
final_ident);
child_name_bindings.define_type(def, DUMMY_SP, modifiers);
let fields = csearch::get_struct_fields(&self.session.cstore, def_id).iter().map(|f| {
f.name
}).collect::<Vec<_>>();
let fields = csearch::get_struct_field_names(&self.session.cstore, def_id);

if fields.is_empty() {
child_name_bindings.define_value(def, DUMMY_SP, modifiers);
Expand Down

0 comments on commit 3494233

Please sign in to comment.