Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve proc-macro def ids #38278

Merged
merged 1 commit into from
Dec 14, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 42 additions & 22 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque};
use syntax::attr;
use syntax::ast::{self, NodeId};
use syntax::codemap;
use syntax_pos::{self, Span, BytePos, Pos};
use syntax_pos::{self, Span, BytePos, Pos, DUMMY_SP};

pub struct DecodeContext<'a, 'tcx: 'a> {
opaque: opaque::Decoder<'a>,
Expand Down Expand Up @@ -507,7 +507,12 @@ impl<'tcx> EntryKind<'tcx> {
}

impl<'a, 'tcx> CrateMetadata {
fn is_proc_macro(&self, id: DefIndex) -> bool {
self.proc_macros.is_some() && id != CRATE_DEF_INDEX
}

fn maybe_entry(&self, item_id: DefIndex) -> Option<Lazy<Entry<'tcx>>> {
assert!(!self.is_proc_macro(item_id));
self.root.index.lookup(self.blob.raw_bytes(), item_id)
}

Expand Down Expand Up @@ -540,18 +545,17 @@ impl<'a, 'tcx> CrateMetadata {
}

pub fn get_def(&self, index: DefIndex) -> Option<Def> {
if self.proc_macros.is_some() {
Some(match index {
CRATE_DEF_INDEX => Def::Mod(self.local_def_id(index)),
_ => Def::Macro(self.local_def_id(index)),
})
} else {
self.entry(index).kind.to_def(self.local_def_id(index))
match self.is_proc_macro(index) {
true => Some(Def::Macro(self.local_def_id(index))),
false => self.entry(index).kind.to_def(self.local_def_id(index)),
}
}

pub fn get_span(&self, index: DefIndex, sess: &Session) -> Span {
self.entry(index).span.decode((self, sess))
match self.is_proc_macro(index) {
true => DUMMY_SP,
false => self.entry(index).span.decode((self, sess)),
}
}

pub fn get_trait_def(&self,
Expand Down Expand Up @@ -662,23 +666,23 @@ impl<'a, 'tcx> CrateMetadata {
}

pub fn get_stability(&self, id: DefIndex) -> Option<attr::Stability> {
match self.proc_macros {
Some(_) if id != CRATE_DEF_INDEX => None,
_ => self.entry(id).stability.map(|stab| stab.decode(self)),
match self.is_proc_macro(id) {
true => None,
false => self.entry(id).stability.map(|stab| stab.decode(self)),
}
}

pub fn get_deprecation(&self, id: DefIndex) -> Option<attr::Deprecation> {
match self.proc_macros {
Some(_) if id != CRATE_DEF_INDEX => None,
_ => self.entry(id).deprecation.map(|depr| depr.decode(self)),
match self.is_proc_macro(id) {
true => None,
false => self.entry(id).deprecation.map(|depr| depr.decode(self)),
}
}

pub fn get_visibility(&self, id: DefIndex) -> ty::Visibility {
match self.proc_macros {
Some(_) => ty::Visibility::Public,
_ => self.entry(id).visibility,
match self.is_proc_macro(id) {
true => ty::Visibility::Public,
false => self.entry(id).visibility,
}
}

Expand Down Expand Up @@ -824,6 +828,7 @@ impl<'a, 'tcx> CrateMetadata {
id: DefIndex)
-> Option<&'tcx InlinedItem> {
debug!("Looking up item: {:?}", id);
if self.is_proc_macro(id) { return None; }
let item_doc = self.entry(id);
let item_did = self.local_def_id(id);
let parent_def_id = self.local_def_id(self.def_key(id).parent.unwrap());
Expand All @@ -836,14 +841,18 @@ impl<'a, 'tcx> CrateMetadata {
}

pub fn is_item_mir_available(&self, id: DefIndex) -> bool {
!self.is_proc_macro(id) &&
self.maybe_entry(id).and_then(|item| item.decode(self).mir).is_some()
}

pub fn maybe_get_item_mir(&self,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
id: DefIndex)
-> Option<Mir<'tcx>> {
self.entry(id).mir.map(|mir| mir.decode((self, tcx)))
match self.is_proc_macro(id) {
true => None,
false => self.entry(id).mir.map(|mir| mir.decode((self, tcx))),
}
}

pub fn get_associated_item(&self, id: DefIndex) -> Option<ty::AssociatedItem> {
Expand Down Expand Up @@ -919,7 +928,7 @@ impl<'a, 'tcx> CrateMetadata {
}

pub fn get_item_attrs(&self, node_id: DefIndex) -> Vec<ast::Attribute> {
if self.proc_macros.is_some() && node_id != CRATE_DEF_INDEX {
if self.is_proc_macro(node_id) {
return Vec::new();
}
// The attributes for a tuple struct are attached to the definition, not the ctor;
Expand Down Expand Up @@ -1105,15 +1114,26 @@ impl<'a, 'tcx> CrateMetadata {

pub fn def_key(&self, id: DefIndex) -> hir_map::DefKey {
debug!("def_key: id={:?}", id);
self.entry(id).def_key.decode(self)
if self.is_proc_macro(id) {
let name = self.proc_macros.as_ref().unwrap()[id.as_usize() - 1].0;
hir_map::DefKey {
parent: Some(CRATE_DEF_INDEX),
disambiguated_data: hir_map::DisambiguatedDefPathData {
data: hir_map::DefPathData::MacroDef(name.as_str()),
disambiguator: 0,
},
}
} else {
self.entry(id).def_key.decode(self)
}
}

// Returns the path leading to the thing with this `id`. Note that
// some def-ids don't wind up in the metadata, so `def_path` sometimes
// returns `None`
pub fn def_path(&self, id: DefIndex) -> Option<hir_map::DefPath> {
debug!("def_path(id={:?})", id);
if self.maybe_entry(id).is_some() {
if self.is_proc_macro(id) || self.maybe_entry(id).is_some() {
Some(hir_map::DefPath::make(self.cnum, id, |parent| self.def_key(parent)))
} else {
None
Expand Down