rust/src/librustc_metadata/decoder.rs

1133 lines
41 KiB
Rust
Raw Normal View History

// Copyright 2012-2014 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.
2011-07-08 08:29:09 +02:00
// Decoding metadata from a single crate's metadata
2011-06-28 01:03:01 +02:00
use astencode::decode_inlined_item;
use cstore::{self, CrateMetadata, MetadataBlob, NativeLibraryKind};
use index::Index;
use schema::*;
2016-03-29 07:50:44 +02:00
use rustc::hir::map as hir_map;
use rustc::hir::map::{DefKey, DefPathData};
use rustc::util::nodemap::FnvHashMap;
2016-03-29 07:50:44 +02:00
use rustc::hir;
use rustc::hir::intravisit::IdRange;
2015-07-31 09:04:06 +02:00
use rustc::middle::cstore::{InlinedItem, LinkagePreference};
use rustc::hir::def::{self, Def};
use rustc::hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
use rustc::middle::lang_items;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::subst::Substs;
use rustc_const_math::ConstInt;
2015-12-16 18:44:15 +01:00
use rustc::mir::repr::Mir;
use std::cell::Ref;
use std::io;
use std::mem;
2014-02-01 05:57:59 +01:00
use std::rc::Rc;
use std::str;
use std::u32;
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};
2015-07-31 09:04:06 +02:00
pub struct DecodeContext<'a, 'tcx: 'a> {
opaque: opaque::Decoder<'a>,
tcx: Option<TyCtxt<'a, 'tcx, 'tcx>>,
cdata: Option<&'a CrateMetadata>,
from_id_range: IdRange,
to_id_range: IdRange,
// Cache the last used filemap for translating spans as an optimization.
last_filemap_index: usize,
lazy_state: LazyState
}
/// Abstract over the various ways one can create metadata decoders.
pub trait Metadata<'a, 'tcx>: Copy {
fn raw_bytes(self) -> &'a [u8];
fn cdata(self) -> Option<&'a CrateMetadata> { None }
fn tcx(self) -> Option<TyCtxt<'a, 'tcx, 'tcx>> { None }
fn decoder(self, pos: usize) -> DecodeContext<'a, 'tcx> {
let id_range = IdRange {
min: NodeId::from_u32(u32::MIN),
max: NodeId::from_u32(u32::MAX)
};
DecodeContext {
opaque: opaque::Decoder::new(self.raw_bytes(), pos),
cdata: self.cdata(),
tcx: self.tcx(),
from_id_range: id_range,
to_id_range: id_range,
last_filemap_index: 0,
lazy_state: LazyState::NoNode
}
}
}
impl<'a, 'tcx> Metadata<'a, 'tcx> for &'a MetadataBlob {
fn raw_bytes(self) -> &'a [u8] {
match *self {
MetadataBlob::Inflated(ref vec) => &vec[..],
MetadataBlob::Archive(ref ar) => ar.as_slice(),
}
}
}
impl<'a, 'tcx> Metadata<'a, 'tcx> for &'a CrateMetadata {
fn raw_bytes(self) -> &'a [u8] { self.blob.raw_bytes() }
fn cdata(self) -> Option<&'a CrateMetadata> { Some(self) }
}
impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadata, TyCtxt<'a, 'tcx, 'tcx>) {
fn raw_bytes(self) -> &'a [u8] { self.0.raw_bytes() }
fn cdata(self) -> Option<&'a CrateMetadata> { Some(self.0) }
fn tcx(self) -> Option<TyCtxt<'a, 'tcx, 'tcx>> { Some(self.1) }
}
// HACK(eddyb) Only used by astencode to customize the from/to IdRange's.
impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadata, TyCtxt<'a, 'tcx, 'tcx>, [IdRange; 2]) {
fn raw_bytes(self) -> &'a [u8] { self.0.raw_bytes() }
fn cdata(self) -> Option<&'a CrateMetadata> { Some(self.0) }
fn tcx(self) -> Option<TyCtxt<'a, 'tcx, 'tcx>> { Some(self.1) }
fn decoder(self, pos: usize) -> DecodeContext<'a, 'tcx> {
let mut dcx = (self.0, self.1).decoder(pos);
dcx.from_id_range = self.2[0];
dcx.to_id_range = self.2[1];
dcx
}
}
impl<'a, 'tcx: 'a, T: Decodable> Lazy<T> {
pub fn decode<M: Metadata<'a, 'tcx>>(self, meta: M) -> T {
let mut dcx = meta.decoder(self.position);
dcx.lazy_state = LazyState::NodeStart(self.position);
T::decode(&mut dcx).unwrap()
}
}
impl<'a, 'tcx: 'a, T: Decodable> LazySeq<T> {
pub fn decode<M: Metadata<'a, 'tcx>>(self, meta: M) -> impl Iterator<Item=T> + 'a {
let mut dcx = meta.decoder(self.position);
dcx.lazy_state = LazyState::NodeStart(self.position);
(0..self.len).map(move |_| {
T::decode(&mut dcx).unwrap()
})
}
}
impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
pub fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx> {
self.tcx.expect("missing TyCtxt in DecodeContext")
}
pub fn cdata(&self) -> &'a CrateMetadata {
self.cdata.expect("missing CrateMetadata in DecodeContext")
}
fn with_position<F: FnOnce(&mut Self) -> R, R>(&mut self, pos: usize, f: F) -> R {
let new_opaque = opaque::Decoder::new(self.opaque.data, pos);
let old_opaque = mem::replace(&mut self.opaque, new_opaque);
let old_state = mem::replace(&mut self.lazy_state, LazyState::NoNode);
let r = f(self);
self.opaque = old_opaque;
self.lazy_state = old_state;
r
}
fn read_lazy_distance(&mut self, min_size: usize)
-> Result<usize, <Self as Decoder>::Error> {
let distance = self.read_usize()?;
let position = match self.lazy_state {
LazyState::NoNode => {
bug!("read_lazy_distance: outside of a metadata node")
}
LazyState::NodeStart(start) => {
assert!(distance + min_size <= start);
start - distance - min_size
}
LazyState::Previous(last_min_end) => {
last_min_end + distance
}
};
self.lazy_state = LazyState::Previous(position + min_size);
Ok(position)
}
}
macro_rules! decoder_methods {
($($name:ident -> $ty:ty;)*) => {
$(fn $name(&mut self) -> Result<$ty, Self::Error> {
self.opaque.$name()
})*
}
}
impl<'doc, 'tcx> Decoder for DecodeContext<'doc, 'tcx> {
type Error = <opaque::Decoder<'doc> as Decoder>::Error;
decoder_methods! {
read_nil -> ();
read_u64 -> u64;
read_u32 -> u32;
read_u16 -> u16;
read_u8 -> u8;
read_usize -> usize;
read_i64 -> i64;
read_i32 -> i32;
read_i16 -> i16;
read_i8 -> i8;
read_isize -> isize;
read_bool -> bool;
read_f64 -> f64;
read_f32 -> f32;
read_char -> char;
read_str -> String;
}
fn error(&mut self, err: &str) -> Self::Error {
self.opaque.error(err)
}
}
impl<'a, 'tcx, T> SpecializedDecoder<Lazy<T>> for DecodeContext<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<Lazy<T>, Self::Error> {
Ok(Lazy::with_position(self.read_lazy_distance(Lazy::<T>::min_size())?))
}
}
impl<'a, 'tcx, T> SpecializedDecoder<LazySeq<T>> for DecodeContext<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<LazySeq<T>, Self::Error> {
let len = self.read_usize()?;
let position = if len == 0 {
0
} else {
self.read_lazy_distance(LazySeq::<T>::min_size(len))?
};
Ok(LazySeq::with_position_and_length(position, len))
}
}
impl<'a, 'tcx> SpecializedDecoder<NodeId> for DecodeContext<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<NodeId, Self::Error> {
let id = u32::decode(self)?;
// from_id_range should be non-empty
assert!(!self.from_id_range.empty());
// Make sure that translating the NodeId will actually yield a
// meaningful result
if !self.from_id_range.contains(NodeId::from_u32(id)) {
bug!("NodeId::decode: {} out of DecodeContext range ({:?} -> {:?})",
id, self.from_id_range, self.to_id_range);
}
// Use wrapping arithmetic because otherwise it introduces control flow.
// Maybe we should just have the control flow? -- aatch
Ok(NodeId::from_u32(id.wrapping_sub(self.from_id_range.min.as_u32())
.wrapping_add(self.to_id_range.min.as_u32())))
}
}
impl<'a, 'tcx> SpecializedDecoder<CrateNum> for DecodeContext<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<CrateNum, Self::Error> {
let cnum = CrateNum::from_u32(u32::decode(self)?);
if cnum == LOCAL_CRATE {
Ok(self.cdata().cnum)
} else {
Ok(self.cdata().cnum_map.borrow()[cnum])
}
}
}
impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<Span, Self::Error> {
let lo = BytePos::decode(self)?;
let hi = BytePos::decode(self)?;
let tcx = if let Some(tcx) = self.tcx {
tcx
} else {
return Ok(syntax_pos::mk_sp(lo, hi));
};
let (lo, hi) = if lo > hi {
// Currently macro expansion sometimes produces invalid Span values
// where lo > hi. In order not to crash the compiler when trying to
// translate these values, let's transform them into something we
// can handle (and which will produce useful debug locations at
// least some of the time).
// This workaround is only necessary as long as macro expansion is
// not fixed. FIXME(#23480)
(lo, lo)
} else {
(lo, hi)
};
let imported_filemaps = self.cdata().imported_filemaps(&tcx.sess.codemap());
let filemap = {
// Optimize for the case that most spans within a translated item
// originate from the same filemap.
let last_filemap = &imported_filemaps[self.last_filemap_index];
if lo >= last_filemap.original_start_pos &&
lo <= last_filemap.original_end_pos &&
hi >= last_filemap.original_start_pos &&
hi <= last_filemap.original_end_pos {
last_filemap
} else {
let mut a = 0;
let mut b = imported_filemaps.len();
while b - a > 1 {
let m = (a + b) / 2;
if imported_filemaps[m].original_start_pos > lo {
b = m;
} else {
a = m;
}
}
self.last_filemap_index = a;
&imported_filemaps[a]
}
};
let lo = (lo - filemap.original_start_pos) +
filemap.translated_filemap.start_pos;
let hi = (hi - filemap.original_start_pos) +
filemap.translated_filemap.start_pos;
Ok(syntax_pos::mk_sp(lo, hi))
}
}
// FIXME(#36588) These impls are horribly unsound as they allow
// the caller to pick any lifetime for 'tcx, including 'static,
// by using the unspecialized proxies to them.
impl<'a, 'tcx> SpecializedDecoder<Ty<'tcx>> for DecodeContext<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<Ty<'tcx>, Self::Error> {
let tcx = self.tcx();
// Handle shorthands first, if we have an usize > 0x80.
if self.opaque.data[self.opaque.position()] & 0x80 != 0 {
let pos = self.read_usize()?;
assert!(pos >= SHORTHAND_OFFSET);
let key = ty::CReaderCacheKey {
cnum: self.cdata().cnum,
pos: pos - SHORTHAND_OFFSET
};
if let Some(ty) = tcx.rcache.borrow().get(&key).cloned() {
return Ok(ty);
}
let ty = self.with_position(key.pos, Ty::decode)?;
tcx.rcache.borrow_mut().insert(key, ty);
Ok(ty)
} else {
Ok(tcx.mk_ty(ty::TypeVariants::decode(self)?))
}
}
}
impl<'a, 'tcx> SpecializedDecoder<ty::GenericPredicates<'tcx>> for DecodeContext<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<ty::GenericPredicates<'tcx>, Self::Error> {
Ok(ty::GenericPredicates {
parent: Decodable::decode(self)?,
predicates: (0..self.read_usize()?).map(|_| {
// Handle shorthands first, if we have an usize > 0x80.
if self.opaque.data[self.opaque.position()] & 0x80 != 0 {
let pos = self.read_usize()?;
assert!(pos >= SHORTHAND_OFFSET);
let pos = pos - SHORTHAND_OFFSET;
self.with_position(pos, ty::Predicate::decode)
} else {
ty::Predicate::decode(self)
}
}).collect()?
})
}
}
impl<'a, 'tcx> SpecializedDecoder<&'tcx Substs<'tcx>> for DecodeContext<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<&'tcx Substs<'tcx>, Self::Error> {
Ok(self.tcx().mk_substs(Decodable::decode(self)?))
}
}
impl<'a, 'tcx> SpecializedDecoder<&'tcx ty::Region> for DecodeContext<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<&'tcx ty::Region, Self::Error> {
Ok(self.tcx().mk_region(Decodable::decode(self)?))
}
}
impl<'a, 'tcx> SpecializedDecoder<&'tcx ty::Slice<Ty<'tcx>>> for DecodeContext<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<&'tcx ty::Slice<Ty<'tcx>>, Self::Error> {
Ok(self.tcx().mk_type_list(Decodable::decode(self)?))
}
}
impl<'a, 'tcx> SpecializedDecoder<&'tcx ty::BareFnTy<'tcx>> for DecodeContext<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<&'tcx ty::BareFnTy<'tcx>, Self::Error> {
Ok(self.tcx().mk_bare_fn(Decodable::decode(self)?))
}
}
impl<'a, 'tcx> SpecializedDecoder<ty::AdtDef<'tcx>> for DecodeContext<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<ty::AdtDef<'tcx>, Self::Error> {
let def_id = DefId::decode(self)?;
Ok(self.tcx().lookup_adt_def(def_id))
}
}
impl<'a, 'tcx> MetadataBlob {
pub fn is_compatible(&self) -> bool {
self.raw_bytes().starts_with(METADATA_HEADER)
}
pub fn get_root(&self) -> CrateRoot {
let slice = self.raw_bytes();
let offset = METADATA_HEADER.len();
let pos = (((slice[offset + 0] as u32) << 24) |
((slice[offset + 1] as u32) << 16) |
((slice[offset + 2] as u32) << 8) |
((slice[offset + 3] as u32) << 0)) as usize;
Lazy::with_position(pos).decode(self)
}
/// Go through each item in the metadata and create a map from that
/// item's def-key to the item's DefIndex.
pub fn load_key_map(&self, index: LazySeq<Index>) -> FnvHashMap<DefKey, DefIndex> {
index.iter_enumerated(self.raw_bytes()).map(|(index, item)| {
(item.decode(self).def_key.decode(self), index)
}).collect()
}
pub fn list_crate_metadata(&self, out: &mut io::Write) -> io::Result<()> {
write!(out, "=External Dependencies=\n")?;
let root = self.get_root();
for (i, dep) in root.crate_deps.decode(self).enumerate() {
write!(out, "{} {}-{}\n", i + 1, dep.name, dep.hash)?;
}
write!(out, "\n")?;
Ok(())
}
}
impl<'tcx> EntryKind<'tcx> {
fn to_def(&self, did: DefId) -> Option<Def> {
Some(match *self {
EntryKind::Const => Def::Const(did),
EntryKind::AssociatedConst(_) => Def::AssociatedConst(did),
EntryKind::ImmStatic |
EntryKind::ForeignImmStatic => Def::Static(did, false),
EntryKind::MutStatic |
EntryKind::ForeignMutStatic => Def::Static(did, true),
EntryKind::Struct(_) => Def::Struct(did),
EntryKind::Union(_) => Def::Union(did),
EntryKind::Fn(_) |
EntryKind::ForeignFn(_) => Def::Fn(did),
EntryKind::Method(_) => Def::Method(did),
EntryKind::Type => Def::TyAlias(did),
EntryKind::AssociatedType(_) => Def::AssociatedTy(did),
EntryKind::Mod(_) => Def::Mod(did),
EntryKind::Variant(_) => Def::Variant(did),
EntryKind::Trait(_) => Def::Trait(did),
EntryKind::Enum => Def::Enum(did),
EntryKind::ForeignMod |
EntryKind::Impl(_) |
EntryKind::DefaultImpl(_) |
EntryKind::Field |
EntryKind::Closure (_) => {
return 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)
}
fn entry(&self, item_id: DefIndex) -> Entry<'tcx> {
match self.maybe_entry(item_id) {
None => bug!("entry: id not found: {:?} in crate {:?} with number {}",
item_id,
self.name,
self.cnum),
Some(d) => d.decode(self)
}
}
fn local_def_id(&self, index: DefIndex) -> DefId {
DefId {
krate: self.cnum,
index: index
}
}
fn item_name(&self, item: &Entry<'tcx>) -> ast::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> {
self.entry(index).kind.to_def(self.local_def_id(index))
}
pub fn get_trait_def(&self,
item_id: DefIndex,
tcx: TyCtxt<'a, 'tcx, 'tcx>) -> ty::TraitDef<'tcx> {
let data = match self.entry(item_id).kind {
EntryKind::Trait(data) => data.decode(self),
_ => bug!()
};
ty::TraitDef::new(data.unsafety, data.paren_sugar,
tcx.lookup_generics(self.local_def_id(item_id)),
data.trait_ref.decode((self, tcx)),
self.def_path(item_id).unwrap().deterministic_hash(tcx))
}
fn get_variant(&self, item: &Entry<'tcx>, index: DefIndex)
-> (ty::VariantDefData<'tcx, 'tcx>, Option<DefIndex>) {
let data = match item.kind {
EntryKind::Variant(data) |
EntryKind::Struct(data) |
EntryKind::Union(data) => data.decode(self),
_ => bug!()
2014-04-17 14:06:25 +02:00
};
let fields = item.children.decode(self).map(|index| {
let f = self.entry(index);
ty::FieldDefData::new(self.local_def_id(index),
self.item_name(&f),
f.visibility)
}).collect();
(ty::VariantDefData {
did: self.local_def_id(data.struct_ctor.unwrap_or(index)),
name: self.item_name(item),
fields: fields,
disr_val: ConstInt::Infer(data.disr),
kind: data.kind,
}, data.struct_ctor)
}
pub fn get_adt_def(&self, item_id: DefIndex, tcx: TyCtxt<'a, 'tcx, 'tcx>)
-> ty::AdtDefMaster<'tcx> {
let item = self.entry(item_id);
let did = self.local_def_id(item_id);
let mut ctor_index = None;
let variants = if let EntryKind::Enum = item.kind {
item.children.decode(self).map(|index| {
let (variant, struct_ctor) = self.get_variant(&self.entry(index), index);
assert_eq!(struct_ctor, None);
variant
}).collect()
} else{
let (variant, struct_ctor) = self.get_variant(&item, item_id);
ctor_index = struct_ctor;
vec![variant]
};
let kind = match item.kind {
EntryKind::Enum => ty::AdtKind::Enum,
EntryKind::Struct(_) => ty::AdtKind::Struct,
EntryKind::Union(_) => ty::AdtKind::Union,
_ => bug!("get_adt_def called on a non-ADT {:?}", did)
};
let adt = tcx.intern_adt_def(did, kind, variants);
if let Some(ctor_index) = ctor_index {
// Make adt definition available through constructor id as well.
tcx.insert_adt_def(self.local_def_id(ctor_index), adt);
}
// this needs to be done *after* the variant is interned,
// to support recursive structures
for variant in &adt.variants {
for field in &variant.fields {
debug!("evaluating the type of {:?}::{:?}", variant.name, field.name);
let ty = self.get_type(field.did.index, tcx);
field.fulfill_ty(ty);
debug!("evaluating the type of {:?}::{:?}: {:?}",
variant.name, field.name, ty);
}
}
adt
}
pub fn get_predicates(&self, item_id: DefIndex, tcx: TyCtxt<'a, 'tcx, 'tcx>)
-> ty::GenericPredicates<'tcx> {
self.entry(item_id).predicates.unwrap().decode((self, tcx))
}
2016-05-29 18:27:05 +02:00
pub fn get_super_predicates(&self, item_id: DefIndex, tcx: TyCtxt<'a, 'tcx, 'tcx>)
-> ty::GenericPredicates<'tcx> {
match self.entry(item_id).kind {
EntryKind::Trait(data) => {
data.decode(self).super_predicates.decode((self, tcx))
}
_ => bug!()
}
}
pub fn get_generics(&self, item_id: DefIndex, tcx: TyCtxt<'a, 'tcx, 'tcx>)
-> ty::Generics<'tcx> {
self.entry(item_id).generics.unwrap().decode((self, tcx))
}
pub fn get_type(&self, id: DefIndex, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Ty<'tcx> {
self.entry(id).ty.unwrap().decode((self, tcx))
}
pub fn get_stability(&self, id: DefIndex) -> Option<attr::Stability> {
self.entry(id).stability.map(|stab| stab.decode(self))
}
pub fn get_deprecation(&self, id: DefIndex) -> Option<attr::Deprecation> {
self.entry(id).deprecation.map(|depr| depr.decode(self))
}
pub fn get_visibility(&self, id: DefIndex) -> ty::Visibility {
self.entry(id).visibility
}
fn get_impl_data(&self, id: DefIndex) -> ImplData<'tcx> {
match self.entry(id).kind {
EntryKind::Impl(data) => data.decode(self),
_ => bug!()
}
}
pub fn get_parent_impl(&self, id: DefIndex) -> Option<DefId> {
self.get_impl_data(id).parent_impl
}
pub fn get_impl_polarity(&self, id: DefIndex) -> hir::ImplPolarity {
self.get_impl_data(id).polarity
}
pub fn get_custom_coerce_unsized_kind(&self, id: DefIndex)
-> Option<ty::adjustment::CustomCoerceUnsized> {
self.get_impl_data(id).coerce_unsized_kind
}
pub fn get_impl_trait(&self,
id: DefIndex,
tcx: TyCtxt<'a, 'tcx, 'tcx>)
-> Option<ty::TraitRef<'tcx>> {
self.get_impl_data(id).trait_ref.map(|tr| tr.decode((self, tcx)))
}
/// Iterates over the language items in the given crate.
pub fn get_lang_items(&self) -> Vec<(DefIndex, usize)> {
self.root.lang_items.decode(self).collect()
}
/// Iterates over each child of the given item.
pub fn each_child_of_item<F>(&self, id: DefIndex, mut callback: F)
where F: FnMut(def::Export)
{
// Find the item.
let item = match self.maybe_entry(id) {
None => return,
Some(item) => item.decode(self),
};
// Iterate over all children.
for child_index in item.children.decode(self) {
// Get the item.
if let Some(child) = self.maybe_entry(child_index) {
let child = child.decode(self);
// Hand off the item to the callback.
match child.kind {
// FIXME(eddyb) Don't encode these in children.
EntryKind::ForeignMod => {
for child_index in child.children.decode(self) {
callback(def::Export {
def_id: self.local_def_id(child_index),
name: self.item_name(&self.entry(child_index))
});
}
continue;
}
EntryKind::Impl(_) | EntryKind::DefaultImpl(_) => continue,
_ => {}
}
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
});
}
}
}
if let EntryKind::Mod(data) = item.kind {
for exp in data.decode(self).reexports.decode(self) {
callback(exp);
}
}
}
pub fn maybe_get_item_ast(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, id: DefIndex)
-> Option<&'tcx InlinedItem> {
debug!("Looking up item: {:?}", id);
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());
let mut parent_def_path = self.def_path(id).unwrap();
parent_def_path.data.pop();
item_doc.ast.map(|ast| {
let ast = ast.decode(self);
decode_inlined_item(self, tcx, parent_def_path, parent_def_id, ast, item_did)
})
}
pub fn is_item_mir_available(&self, id: DefIndex) -> bool {
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)))
}
pub fn get_impl_or_trait_item(&self, id: DefIndex, tcx: TyCtxt<'a, 'tcx, 'tcx>)
-> Option<ty::ImplOrTraitItem<'tcx>> {
let item = self.entry(id);
let parent_and_name = || {
let def_key = item.def_key.decode(self);
(self.local_def_id(def_key.parent.unwrap()),
def_key.disambiguated_data.data.get_opt_name().unwrap())
};
2011-07-27 14:19:39 +02:00
Some(match item.kind {
EntryKind::AssociatedConst(container) => {
let (parent, name) = parent_and_name();
ty::ConstTraitItem(Rc::new(ty::AssociatedConst {
name: name,
ty: item.ty.unwrap().decode((self, tcx)),
vis: item.visibility,
defaultness: container.defaultness(),
def_id: self.local_def_id(id),
container: container.with_def_id(parent),
has_value: container.has_body(),
}))
}
EntryKind::Method(data) => {
let (parent, name) = parent_and_name();
let ity = item.ty.unwrap().decode((self, tcx));
let fty = match ity.sty {
ty::TyFnDef(.., fty) => fty,
_ => bug!(
"the type {:?} of the method {:?} is not a function?",
ity, name)
};
let data = data.decode(self);
ty::MethodTraitItem(Rc::new(ty::Method {
name: name,
generics: tcx.lookup_generics(self.local_def_id(id)),
predicates: item.predicates.unwrap().decode((self, tcx)),
fty: fty,
explicit_self: data.explicit_self.decode((self, tcx)),
vis: item.visibility,
defaultness: data.container.defaultness(),
has_body: data.container.has_body(),
def_id: self.local_def_id(id),
container: data.container.with_def_id(parent),
}))
}
EntryKind::AssociatedType(container) => {
let (parent, name) = parent_and_name();
ty::TypeTraitItem(Rc::new(ty::AssociatedType {
name: name,
ty: item.ty.map(|ty| ty.decode((self, tcx))),
vis: item.visibility,
defaultness: container.defaultness(),
def_id: self.local_def_id(id),
container: container.with_def_id(parent),
}))
}
_ => return None
})
}
pub fn get_item_variances(&self, id: DefIndex) -> Vec<ty::Variance> {
self.entry(id).variances.decode(self).collect()
}
pub fn get_struct_ctor_def_id(&self, node_id: DefIndex) -> Option<DefId> {
match self.entry(node_id).kind {
EntryKind::Struct(data) => {
data.decode(self).struct_ctor.map(|index| self.local_def_id(index))
}
_ => None
}
}
pub fn get_item_attrs(&self, node_id: DefIndex) -> Vec<ast::Attribute> {
// The attributes for a tuple struct are attached to the definition, not the ctor;
// we assume that someone passing in a tuple struct ctor is actually wanting to
// look at the definition
let mut item = self.entry(node_id);
let def_key = item.def_key.decode(self);
if def_key.disambiguated_data.data == DefPathData::StructCtor {
item = self.entry(def_key.parent.unwrap());
}
self.get_attributes(&item)
}
pub fn get_struct_field_names(&self, id: DefIndex) -> Vec<ast::Name> {
self.entry(id).children.decode(self).map(|index| {
self.item_name(&self.entry(index))
}).collect()
}
fn get_attributes(&self, item: &Entry<'tcx>) -> Vec<ast::Attribute> {
item.attributes.decode(self).map(|mut attr| {
// Need new unique IDs: old thread-local IDs won't map to new threads.
attr.node.id = attr::mk_attr_id();
attr
}).collect()
}
// Translate a DefId from the current compilation environment to a DefId
// for an external crate.
fn reverse_translate_def_id(&self, did: DefId) -> Option<DefId> {
for (local, &global) in self.cnum_map.borrow().iter_enumerated() {
if global == did.krate {
return Some(DefId { krate: local, index: did.index });
2015-10-02 15:44:26 +02:00
}
}
None
}
pub fn get_inherent_implementations_for_type(&self, id: DefIndex) -> Vec<DefId> {
self.entry(id).inherent_impls.decode(self).map(|index| {
self.local_def_id(index)
}).collect()
}
pub fn get_implementations_for_trait(&self, filter: Option<DefId>, result: &mut Vec<DefId>) {
// Do a reverse lookup beforehand to avoid touching the crate_num
// hash map in the loop below.
let filter = match filter.map(|def_id| self.reverse_translate_def_id(def_id)) {
Some(Some(def_id)) => Some((def_id.krate.as_u32(), def_id.index)),
Some(None) => return,
None => None
};
// FIXME(eddyb) Make this O(1) instead of O(n).
for trait_impls in self.root.impls.decode(self) {
if filter.is_some() && filter != Some(trait_impls.trait_id) {
continue;
}
2013-12-25 19:10:33 +01:00
result.extend(trait_impls.impls.decode(self).map(|index| {
self.local_def_id(index)
}));
if filter.is_some() {
break;
}
}
}
pub fn get_trait_of_item(&self, id: DefIndex) -> Option<DefId> {
self.entry(id).def_key.decode(self).parent.and_then(|parent_index| {
match self.entry(parent_index).kind {
EntryKind::Trait(_) => Some(self.local_def_id(parent_index)),
_ => None
}
})
}
pub fn get_native_libraries(&self) -> Vec<(NativeLibraryKind, String)> {
self.root.native_libraries.decode(self).collect()
}
pub fn get_dylib_dependency_formats(&self) -> Vec<(CrateNum, LinkagePreference)> {
self.root.dylib_dependency_formats.decode(self).enumerate().flat_map(|(i, link)| {
let cnum = CrateNum::new(i + 1);
link.map(|link| (self.cnum_map.borrow()[cnum], link))
}).collect()
}
pub fn get_missing_lang_items(&self) -> Vec<lang_items::LangItem> {
self.root.lang_items_missing.decode(self).collect()
}
pub fn get_fn_arg_names(&self, id: DefIndex) -> Vec<ast::Name> {
let arg_names = match self.entry(id).kind {
EntryKind::Fn(data) |
EntryKind::ForeignFn(data) => data.decode(self).arg_names,
EntryKind::Method(data) => data.decode(self).fn_data.arg_names,
_ => LazySeq::empty()
};
arg_names.decode(self).collect()
}
pub fn get_reachable_ids(&self) -> Vec<DefId> {
self.root.reachable_ids.decode(self).map(|index| self.local_def_id(index)).collect()
}
pub fn is_const_fn(&self, id: DefIndex) -> bool {
let constness = match self.entry(id).kind {
EntryKind::Method(data) => data.decode(self).fn_data.constness,
EntryKind::Fn(data) => data.decode(self).constness,
_ => hir::Constness::NotConst
};
constness == hir::Constness::Const
}
pub fn is_extern_item(&self, id: DefIndex, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> bool {
let item = match self.maybe_entry(id) {
Some(item) => item.decode(self),
None => return false,
};
let applicable = match item.kind {
EntryKind::ImmStatic |
EntryKind::MutStatic |
EntryKind::ForeignImmStatic |
EntryKind::ForeignMutStatic => true,
EntryKind::Fn(_) | EntryKind::ForeignFn(_) => {
self.get_generics(id, tcx).types.is_empty()
}
_ => false,
};
if applicable {
attr::contains_extern_indicator(tcx.sess.diagnostic(),
&self.get_attributes(&item))
} else {
false
}
}
pub fn is_foreign_item(&self, id: DefIndex) -> bool {
match self.entry(id).kind {
EntryKind::ForeignImmStatic |
EntryKind::ForeignMutStatic |
EntryKind::ForeignFn(_) => true,
_ => false
}
}
pub fn is_defaulted_trait(&self, trait_id: DefIndex) -> bool {
match self.entry(trait_id).kind {
EntryKind::Trait(data) => data.decode(self).has_default_impl,
_ => bug!()
}
}
pub fn is_default_impl(&self, impl_id: DefIndex) -> bool {
match self.entry(impl_id).kind {
EntryKind::DefaultImpl(_) => true,
_ => false
}
}
pub fn closure_kind(&self, closure_id: DefIndex) -> ty::ClosureKind {
match self.entry(closure_id).kind {
EntryKind::Closure(data) => data.decode(self).kind,
_ => bug!()
}
}
pub fn closure_ty(&self, closure_id: DefIndex, tcx: TyCtxt<'a, 'tcx, 'tcx>)
-> ty::ClosureTy<'tcx> {
match self.entry(closure_id).kind {
EntryKind::Closure(data) => data.decode(self).ty.decode((self, tcx)),
_ => bug!()
}
}
pub fn def_key(&self, id: DefIndex) -> hir_map::DefKey {
debug!("def_key: id={:?}", id);
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() {
Some(hir_map::DefPath::make(self.cnum, id, |parent| self.def_key(parent)))
} else {
None
}
}
/// Imports the codemap from an external crate into the codemap of the crate
/// currently being compiled (the "local crate").
///
/// The import algorithm works analogous to how AST items are inlined from an
/// external crate's metadata:
/// For every FileMap in the external codemap an 'inline' copy is created in the
/// local codemap. The correspondence relation between external and local
/// FileMaps is recorded in the `ImportedFileMap` objects returned from this
/// function. When an item from an external crate is later inlined into this
/// crate, this correspondence information is used to translate the span
/// information of the inlined item so that it refers the correct positions in
/// the local codemap (see `<decoder::DecodeContext as SpecializedDecoder<Span>>`).
///
/// The import algorithm in the function below will reuse FileMaps already
/// existing in the local codemap. For example, even if the FileMap of some
/// source file of libstd gets imported many times, there will only ever be
/// one FileMap object for the corresponding file in the local codemap.
///
/// Note that imported FileMaps do not actually contain the source code of the
/// file they represent, just information about length, line breaks, and
/// multibyte characters. This information is enough to generate valid debuginfo
/// for items inlined from other crates.
pub fn imported_filemaps(&'a self, local_codemap: &codemap::CodeMap)
-> Ref<'a, Vec<cstore::ImportedFileMap>> {
{
let filemaps = self.codemap_import_info.borrow();
if !filemaps.is_empty() {
return filemaps;
}
}
let external_codemap = self.root.codemap.decode(self);
let imported_filemaps = external_codemap.map(|filemap_to_import| {
// Try to find an existing FileMap that can be reused for the filemap to
// be imported. A FileMap is reusable if it is exactly the same, just
// positioned at a different offset within the codemap.
let reusable_filemap = {
local_codemap.files
.borrow()
.iter()
.find(|fm| are_equal_modulo_startpos(&fm, &filemap_to_import))
.map(|rc| rc.clone())
};
match reusable_filemap {
Some(fm) => {
cstore::ImportedFileMap {
original_start_pos: filemap_to_import.start_pos,
original_end_pos: filemap_to_import.end_pos,
translated_filemap: fm
}
}
None => {
// We can't reuse an existing FileMap, so allocate a new one
// containing the information we need.
let syntax_pos::FileMap {
name,
abs_path,
start_pos,
end_pos,
lines,
multibyte_chars,
..
} = filemap_to_import;
let source_length = (end_pos - start_pos).to_usize();
// Translate line-start positions and multibyte character
// position into frame of reference local to file.
// `CodeMap::new_imported_filemap()` will then translate those
// coordinates to their new global frame of reference when the
// offset of the FileMap is known.
let mut lines = lines.into_inner();
for pos in &mut lines {
*pos = *pos - start_pos;
}
let mut multibyte_chars = multibyte_chars.into_inner();
for mbc in &mut multibyte_chars {
mbc.pos = mbc.pos - start_pos;
}
let local_version = local_codemap.new_imported_filemap(name,
abs_path,
source_length,
lines,
multibyte_chars);
cstore::ImportedFileMap {
original_start_pos: start_pos,
original_end_pos: end_pos,
translated_filemap: local_version
}
}
}
}).collect();
// This shouldn't borrow twice, but there is no way to downgrade RefMut to Ref.
*self.codemap_import_info.borrow_mut() = imported_filemaps;
self.codemap_import_info.borrow()
}
}
fn are_equal_modulo_startpos(fm1: &syntax_pos::FileMap, fm2: &syntax_pos::FileMap) -> bool {
if fm1.name != fm2.name {
return false;
}
let lines1 = fm1.lines.borrow();
let lines2 = fm2.lines.borrow();
if lines1.len() != lines2.len() {
return false;
}
for (&line1, &line2) in lines1.iter().zip(lines2.iter()) {
if (line1 - fm1.start_pos) != (line2 - fm2.start_pos) {
return false;
}
}
let multibytes1 = fm1.multibyte_chars.borrow();
let multibytes2 = fm2.multibyte_chars.borrow();
if multibytes1.len() != multibytes2.len() {
return false;
}
for (mb1, mb2) in multibytes1.iter().zip(multibytes2.iter()) {
if (mb1.bytes != mb2.bytes) ||
((mb1.pos - fm1.start_pos) != (mb2.pos - fm2.start_pos)) {
return false;
}
}
true
}