Skip to content

Commit

Permalink
rustc_metadata: move opt_item_name to TyCtxt::item_name.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Sep 20, 2016
1 parent dc26a23 commit dadbaa4
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 55 deletions.
26 changes: 25 additions & 1 deletion src/librustc/hir/map/definitions.rs
Expand Up @@ -14,7 +14,7 @@ use rustc_data_structures::fnv::FnvHashMap;
use std::fmt::Write;
use std::hash::{Hash, Hasher, SipHasher};
use syntax::{ast, visit};
use syntax::parse::token::InternedString;
use syntax::parse::token::{self, InternedString};
use ty::TyCtxt;
use util::nodemap::NodeMap;

Expand Down Expand Up @@ -326,6 +326,30 @@ impl Definitions {
}

impl DefPathData {
pub fn get_opt_name(&self) -> Option<ast::Name> {
use self::DefPathData::*;
match *self {
TypeNs(ref name) |
ValueNs(ref name) |
Module(ref name) |
MacroDef(ref name) |
TypeParam(ref name) |
LifetimeDef(ref name) |
EnumVariant(ref name) |
Binding(ref name) |
Field(ref name) => Some(token::intern(name)),

Impl |
CrateRoot |
InlinedRoot(_) |
Misc |
ClosureExpr |
StructCtor |
Initializer |
ImplTrait => None
}
}

pub fn as_interned_str(&self) -> InternedString {
use self::DefPathData::*;
match *self {
Expand Down
4 changes: 1 addition & 3 deletions src/librustc/middle/cstore.rs
Expand Up @@ -132,7 +132,6 @@ pub trait CrateStore<'tcx> {
fn item_type<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
-> Ty<'tcx>;
fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap<DefId>>;
fn opt_item_name(&self, def: DefId) -> Option<ast::Name>;
fn item_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
-> ty::GenericPredicates<'tcx>;
fn item_super_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
Expand Down Expand Up @@ -293,7 +292,6 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap<DefId>> {
bug!("visible_parent_map")
}
fn opt_item_name(&self, def: DefId) -> Option<ast::Name> { bug!("opt_item_name") }
fn item_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
-> ty::GenericPredicates<'tcx> { bug!("item_predicates") }
fn item_super_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
Expand Down Expand Up @@ -424,4 +422,4 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {

pub trait MacroLoader {
fn load_crate(&mut self, extern_crate: &ast::Item, allows_macros: bool) -> Vec<LoadedMacro>;
}
}
3 changes: 2 additions & 1 deletion src/librustc/ty/item_path.rs
Expand Up @@ -138,7 +138,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}
}

cur_path.push(self.sess.cstore.opt_item_name(cur_def).unwrap_or_else(||
cur_path.push(self.sess.cstore.def_key(cur_def)
.disambiguated_data.data.get_opt_name().unwrap_or_else(||
token::intern("<unnamed>")));
match visible_parent_map.get(&cur_def) {
Some(&def) => cur_def = def,
Expand Down
21 changes: 16 additions & 5 deletions src/librustc/ty/mod.rs
Expand Up @@ -21,7 +21,7 @@ use dep_graph::{self, DepNode};
use hir::map as ast_map;
use middle;
use hir::def::{Def, PathResolution, ExportMap};
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
use middle::region::{CodeExtent, ROOT_CODE_EXTENT};
use traits;
Expand All @@ -43,7 +43,7 @@ use std::slice;
use std::vec::IntoIter;
use syntax::ast::{self, Name, NodeId};
use syntax::attr;
use syntax::parse::token::InternedString;
use syntax::parse::token::{self, InternedString};
use syntax_pos::{DUMMY_SP, Span};

use rustc_const_math::ConstInt;
Expand Down Expand Up @@ -2390,10 +2390,21 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
pub fn item_name(self, id: DefId) -> ast::Name {
if let Some(id) = self.map.as_local_node_id(id) {
self.map.name(id)
} else if id.index == CRATE_DEF_INDEX {
token::intern(&self.sess.cstore.original_crate_name(id.krate))
} else {
self.sess.cstore.opt_item_name(id).unwrap_or_else(|| {
bug!("item_name: no name for {:?}", self.def_path(id));
})
let def_key = self.sess.cstore.def_key(id);
// The name of a StructCtor is that of its struct parent.
if let ast_map::DefPathData::StructCtor = def_key.disambiguated_data.data {
self.item_name(DefId {
krate: id.krate,
index: def_key.parent.unwrap()
})
} else {
def_key.disambiguated_data.data.get_opt_name().unwrap_or_else(|| {
bug!("item_name: no name for {:?}", self.def_path(id));
})
}
}
}

Expand Down
10 changes: 0 additions & 10 deletions src/librustc_metadata/csearch.rs
Expand Up @@ -127,16 +127,6 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
self.get_crate_data(did.krate).get_fn_arg_names(did.index)
}

fn opt_item_name(&self, def: DefId) -> Option<ast::Name> {
self.dep_graph.read(DepNode::MetaData(def));
let cdata = self.get_crate_data(def.krate);
if def.index == CRATE_DEF_INDEX {
Some(token::intern(&cdata.name()))
} else {
cdata.maybe_get_item_name(def.index)
}
}

fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>
{
self.dep_graph.read(DepNode::MetaData(def_id));
Expand Down
39 changes: 5 additions & 34 deletions src/librustc_metadata/decoder.rs
Expand Up @@ -43,7 +43,6 @@ use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque};
use syntax::attr;
use syntax::ast::{self, NodeId};
use syntax::codemap;
use syntax::parse::token;
use syntax_pos::{self, Span, BytePos, Pos};

pub struct DecodeContext<'a, 'tcx: 'a> {
Expand Down Expand Up @@ -469,32 +468,6 @@ impl<'tcx> EntryKind<'tcx> {
}
}

fn def_key_name(def_key: &hir_map::DefKey) -> Option<ast::Name> {
match def_key.disambiguated_data.data {
DefPathData::TypeNs(ref name) |
DefPathData::ValueNs(ref name) |
DefPathData::Module(ref name) |
DefPathData::MacroDef(ref name) |
DefPathData::TypeParam(ref name) |
DefPathData::LifetimeDef(ref name) |
DefPathData::EnumVariant(ref name) |
DefPathData::Field(ref name) |
DefPathData::Binding(ref name) => {
Some(token::intern(name))
}

DefPathData::InlinedRoot(_) => bug!("unexpected DefPathData"),

DefPathData::CrateRoot |
DefPathData::Misc |
DefPathData::Impl |
DefPathData::ClosureExpr |
DefPathData::StructCtor |
DefPathData::Initializer |
DefPathData::ImplTrait => None
}
}

impl<'a, 'tcx> CrateMetadata {
fn maybe_entry(&self, item_id: DefIndex) -> Option<Lazy<Entry<'tcx>>> {
self.root.index.lookup(self.blob.raw_bytes(), item_id)
Expand All @@ -518,7 +491,8 @@ impl<'a, 'tcx> CrateMetadata {
}

fn item_name(&self, item: &Entry<'tcx>) -> ast::Name {
def_key_name(&item.def_key.decode(self)).expect("no name in item_name")
item.def_key.decode(self).disambiguated_data.data.get_opt_name()
.expect("no name in item_name")
}

pub fn get_def(&self, index: DefIndex) -> Option<Def> {
Expand Down Expand Up @@ -708,7 +682,8 @@ impl<'a, 'tcx> CrateMetadata {
_ => {}
}

if let Some(name) = def_key_name(&child.def_key.decode(self)) {
let def_key = child.def_key.decode(self);
if let Some(name) = def_key.disambiguated_data.data.get_opt_name() {
callback(def::Export {
def_id: self.local_def_id(child_index),
name: name
Expand All @@ -724,10 +699,6 @@ impl<'a, 'tcx> CrateMetadata {
}
}

pub fn maybe_get_item_name(&self, id: DefIndex) -> Option<ast::Name> {
def_key_name(&self.entry(id).def_key.decode(self))
}

pub fn maybe_get_item_ast(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, id: DefIndex)
-> Option<&'tcx InlinedItem> {
debug!("Looking up item: {:?}", id);
Expand Down Expand Up @@ -757,7 +728,7 @@ impl<'a, 'tcx> CrateMetadata {
let parent_and_name = || {
let def_key = item.def_key.decode(self);
(self.local_def_id(def_key.parent.unwrap()),
def_key_name(&def_key).unwrap())
def_key.disambiguated_data.data.get_opt_name().unwrap())
};

Some(match item.kind {
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -436,7 +436,8 @@ impl<'b> Resolver<'b> {
let trait_item_def_ids = self.session.cstore.impl_or_trait_items(def_id);
for &trait_item_def in &trait_item_def_ids {
let trait_item_name =
self.session.cstore.opt_item_name(trait_item_def)
self.session.cstore.def_key(trait_item_def)
.disambiguated_data.data.get_opt_name()
.expect("opt_item_name returned None for trait");

debug!("(building reduced graph for external crate) ... adding trait item \
Expand Down

0 comments on commit dadbaa4

Please sign in to comment.