2015-03-02 14:34:16 +09:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// 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.
|
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
use index::Index;
|
2017-04-27 16:12:57 +02:00
|
|
|
use index_builder::{FromId, IndexBuilder, Untracked};
|
|
|
|
use isolated_encoder::IsolatedEncoder;
|
2016-09-16 17:25:54 +03:00
|
|
|
use schema::*;
|
2015-11-25 00:00:26 +02:00
|
|
|
|
2017-04-05 23:39:02 +02:00
|
|
|
use rustc::middle::cstore::{LinkMeta, LinkagePreference, NativeLibrary,
|
2018-02-10 14:28:17 -08:00
|
|
|
EncodedMetadata, ForeignModule};
|
2017-05-13 17:11:52 +03:00
|
|
|
use rustc::hir::def::CtorKind;
|
2018-01-06 03:36:39 +00:00
|
|
|
use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefIndex, DefId, LocalDefId, LOCAL_CRATE};
|
2017-11-29 16:28:25 +01:00
|
|
|
use rustc::hir::map::definitions::DefPathTable;
|
2018-08-03 18:34:23 -06:00
|
|
|
use rustc_data_structures::fingerprint::Fingerprint;
|
2016-09-08 19:05:50 +03:00
|
|
|
use rustc::middle::dependency_format::Linkage;
|
2018-03-05 17:41:11 +01:00
|
|
|
use rustc::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel,
|
|
|
|
metadata_symbol_name};
|
2016-09-16 17:25:54 +03:00
|
|
|
use rustc::middle::lang_items;
|
2018-01-16 09:12:54 +01:00
|
|
|
use rustc::mir::{self, interpret};
|
2016-03-22 17:30:57 +02:00
|
|
|
use rustc::traits::specialization_graph;
|
2018-03-05 17:41:11 +01:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt, ReprOptions, SymbolName};
|
2017-10-18 17:48:36 +02:00
|
|
|
use rustc::ty::codec::{self as ty_codec, TyEncoder};
|
2015-11-25 00:00:26 +02:00
|
|
|
|
2018-07-26 11:13:11 -06:00
|
|
|
use rustc::session::config::{self, CrateType};
|
2018-04-13 18:48:41 +02:00
|
|
|
use rustc::util::nodemap::FxHashMap;
|
2012-04-26 12:15:46 -07:00
|
|
|
|
2017-12-19 15:14:41 +01:00
|
|
|
use rustc_data_structures::stable_hasher::StableHasher;
|
2016-09-02 11:08:16 +03:00
|
|
|
use rustc_serialize::{Encodable, Encoder, SpecializedEncoder, opaque};
|
2017-04-27 16:12:57 +02:00
|
|
|
|
2017-12-19 15:14:41 +01:00
|
|
|
use std::hash::Hash;
|
2017-04-24 19:01:19 +02:00
|
|
|
use std::path::Path;
|
2018-02-27 17:11:14 +01:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2015-09-17 14:29:59 -04:00
|
|
|
use std::u32;
|
2016-09-08 19:05:50 +03:00
|
|
|
use syntax::ast::{self, CRATE_NODE_ID};
|
2016-08-19 18:58:14 -07:00
|
|
|
use syntax::attr;
|
2018-08-18 12:14:03 +02:00
|
|
|
use syntax::source_map::Spanned;
|
2018-06-10 19:33:30 +03:00
|
|
|
use syntax::symbol::keywords;
|
2018-08-18 12:13:52 +02:00
|
|
|
use syntax_pos::{self, hygiene, FileName, SourceFile, Span};
|
2012-09-19 13:13:24 -07:00
|
|
|
|
2016-03-29 08:50:44 +03:00
|
|
|
use rustc::hir::{self, PatKind};
|
2016-11-02 18:22:59 -04:00
|
|
|
use rustc::hir::itemlikevisit::ItemLikeVisitor;
|
2016-11-28 14:00:26 -05:00
|
|
|
use rustc::hir::intravisit::{Visitor, NestedVisitorMap};
|
2016-03-29 08:50:44 +03:00
|
|
|
use rustc::hir::intravisit;
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
pub struct EncodeContext<'a, 'tcx: 'a> {
|
2018-06-04 22:14:02 +02:00
|
|
|
opaque: opaque::Encoder,
|
2016-05-03 05:23:22 +03:00
|
|
|
pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-09-02 11:08:16 +03:00
|
|
|
link_meta: &'a LinkMeta,
|
2016-09-19 23:49:01 +03:00
|
|
|
|
2016-09-17 10:33:47 +03:00
|
|
|
lazy_state: LazyState,
|
2016-11-08 14:02:55 +11:00
|
|
|
type_shorthands: FxHashMap<Ty<'tcx>, usize>,
|
|
|
|
predicate_shorthands: FxHashMap<ty::Predicate<'tcx>, usize>,
|
2018-04-10 09:58:46 +02:00
|
|
|
|
|
|
|
interpret_allocs: FxHashMap<interpret::AllocId, usize>,
|
|
|
|
interpret_allocs_inverse: Vec<interpret::AllocId>,
|
2018-01-04 11:26:47 +01:00
|
|
|
|
|
|
|
// This is used to speed up Span encoding.
|
2018-08-18 12:13:56 +02:00
|
|
|
source_file_cache: Lrc<SourceFile>,
|
2012-04-23 23:40:53 -07:00
|
|
|
}
|
|
|
|
|
2016-09-01 16:55:33 +03:00
|
|
|
macro_rules! encoder_methods {
|
|
|
|
($($name:ident($ty:ty);)*) => {
|
|
|
|
$(fn $name(&mut self, value: $ty) -> Result<(), Self::Error> {
|
|
|
|
self.opaque.$name(value)
|
|
|
|
})*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:49:01 +03:00
|
|
|
impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> {
|
2018-06-04 22:14:02 +02:00
|
|
|
type Error = <opaque::Encoder as Encoder>::Error;
|
2016-09-01 16:55:33 +03:00
|
|
|
|
|
|
|
fn emit_nil(&mut self) -> Result<(), Self::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
encoder_methods! {
|
|
|
|
emit_usize(usize);
|
2016-09-27 19:06:44 +03:00
|
|
|
emit_u128(u128);
|
2016-09-01 16:55:33 +03:00
|
|
|
emit_u64(u64);
|
|
|
|
emit_u32(u32);
|
|
|
|
emit_u16(u16);
|
|
|
|
emit_u8(u8);
|
|
|
|
|
|
|
|
emit_isize(isize);
|
2016-09-27 19:06:44 +03:00
|
|
|
emit_i128(i128);
|
2016-09-01 16:55:33 +03:00
|
|
|
emit_i64(i64);
|
|
|
|
emit_i32(i32);
|
|
|
|
emit_i16(i16);
|
|
|
|
emit_i8(i8);
|
|
|
|
|
|
|
|
emit_bool(bool);
|
|
|
|
emit_f64(f64);
|
|
|
|
emit_f32(f32);
|
|
|
|
emit_char(char);
|
|
|
|
emit_str(&str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
impl<'a, 'tcx, T> SpecializedEncoder<Lazy<T>> for EncodeContext<'a, 'tcx> {
|
|
|
|
fn specialized_encode(&mut self, lazy: &Lazy<T>) -> Result<(), Self::Error> {
|
2016-09-17 10:33:47 +03:00
|
|
|
self.emit_lazy_distance(lazy.position, Lazy::<T>::min_size())
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx, T> SpecializedEncoder<LazySeq<T>> for EncodeContext<'a, 'tcx> {
|
|
|
|
fn specialized_encode(&mut self, seq: &LazySeq<T>) -> Result<(), Self::Error> {
|
|
|
|
self.emit_usize(seq.len)?;
|
2016-09-17 10:33:47 +03:00
|
|
|
if seq.len == 0 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
self.emit_lazy_distance(seq.position, LazySeq::<T>::min_size(seq.len))
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-16 17:13:39 +01:00
|
|
|
impl<'a, 'tcx> SpecializedEncoder<CrateNum> for EncodeContext<'a, 'tcx> {
|
|
|
|
#[inline]
|
|
|
|
fn specialized_encode(&mut self, cnum: &CrateNum) -> Result<(), Self::Error> {
|
|
|
|
self.emit_u32(cnum.as_u32())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> SpecializedEncoder<DefId> for EncodeContext<'a, 'tcx> {
|
|
|
|
#[inline]
|
|
|
|
fn specialized_encode(&mut self, def_id: &DefId) -> Result<(), Self::Error> {
|
|
|
|
let DefId {
|
|
|
|
krate,
|
|
|
|
index,
|
|
|
|
} = *def_id;
|
|
|
|
|
|
|
|
krate.encode(self)?;
|
|
|
|
index.encode(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> SpecializedEncoder<DefIndex> for EncodeContext<'a, 'tcx> {
|
|
|
|
#[inline]
|
|
|
|
fn specialized_encode(&mut self, def_index: &DefIndex) -> Result<(), Self::Error> {
|
2018-01-02 06:36:12 -05:00
|
|
|
self.emit_u32(def_index.as_raw_u32())
|
2017-11-16 17:13:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 11:26:47 +01:00
|
|
|
impl<'a, 'tcx> SpecializedEncoder<Span> for EncodeContext<'a, 'tcx> {
|
|
|
|
fn specialized_encode(&mut self, span: &Span) -> Result<(), Self::Error> {
|
2018-06-25 01:00:21 +03:00
|
|
|
if span.is_dummy() {
|
2018-01-04 11:26:47 +01:00
|
|
|
return TAG_INVALID_SPAN.encode(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
let span = span.data();
|
|
|
|
|
2018-01-04 13:13:32 +01:00
|
|
|
// The Span infrastructure should make sure that this invariant holds:
|
|
|
|
debug_assert!(span.lo <= span.hi);
|
2018-01-04 11:26:47 +01:00
|
|
|
|
2018-08-18 12:13:56 +02:00
|
|
|
if !self.source_file_cache.contains(span.lo) {
|
2018-08-18 12:14:14 +02:00
|
|
|
let source_map = self.tcx.sess.source_map();
|
|
|
|
let source_file_index = source_map.lookup_source_file_idx(span.lo);
|
|
|
|
self.source_file_cache = source_map.files()[source_file_index].clone();
|
2018-01-04 11:26:47 +01:00
|
|
|
}
|
|
|
|
|
2018-08-18 12:13:56 +02:00
|
|
|
if !self.source_file_cache.contains(span.hi) {
|
2018-01-04 13:13:32 +01:00
|
|
|
// Unfortunately, macro expansion still sometimes generates Spans
|
|
|
|
// that malformed in this way.
|
2018-01-04 11:26:47 +01:00
|
|
|
return TAG_INVALID_SPAN.encode(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
TAG_VALID_SPAN.encode(self)?;
|
|
|
|
span.lo.encode(self)?;
|
|
|
|
|
|
|
|
// Encode length which is usually less than span.hi and profits more
|
|
|
|
// from the variable-length integer encoding that we use.
|
|
|
|
let len = span.hi - span.lo;
|
|
|
|
len.encode(self)
|
|
|
|
|
|
|
|
// Don't encode the expansion context.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-06 03:36:39 +00:00
|
|
|
impl<'a, 'tcx> SpecializedEncoder<LocalDefId> for EncodeContext<'a, 'tcx> {
|
|
|
|
#[inline]
|
|
|
|
fn specialized_encode(&mut self, def_id: &LocalDefId) -> Result<(), Self::Error> {
|
|
|
|
self.specialized_encode(&def_id.to_def_id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:46:31 +03:00
|
|
|
impl<'a, 'tcx> SpecializedEncoder<Ty<'tcx>> for EncodeContext<'a, 'tcx> {
|
|
|
|
fn specialized_encode(&mut self, ty: &Ty<'tcx>) -> Result<(), Self::Error> {
|
2017-10-18 17:48:36 +02:00
|
|
|
ty_codec::encode_with_shorthand(self, ty, |ecx| &mut ecx.type_shorthands)
|
2016-09-08 21:36:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 09:12:54 +01:00
|
|
|
impl<'a, 'tcx> SpecializedEncoder<interpret::AllocId> for EncodeContext<'a, 'tcx> {
|
|
|
|
fn specialized_encode(&mut self, alloc_id: &interpret::AllocId) -> Result<(), Self::Error> {
|
2018-04-13 18:48:41 +02:00
|
|
|
use std::collections::hash_map::Entry;
|
|
|
|
let index = match self.interpret_allocs.entry(*alloc_id) {
|
|
|
|
Entry::Occupied(e) => *e.get(),
|
|
|
|
Entry::Vacant(e) => {
|
|
|
|
let idx = self.interpret_allocs_inverse.len();
|
|
|
|
self.interpret_allocs_inverse.push(*alloc_id);
|
|
|
|
e.insert(idx);
|
|
|
|
idx
|
|
|
|
},
|
2018-03-16 09:59:42 +01:00
|
|
|
};
|
2018-04-10 09:58:46 +02:00
|
|
|
|
|
|
|
index.encode(self)
|
2018-01-16 09:12:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
impl<'a, 'tcx> SpecializedEncoder<ty::GenericPredicates<'tcx>> for EncodeContext<'a, 'tcx> {
|
2016-10-22 18:44:32 +05:30
|
|
|
fn specialized_encode(&mut self,
|
|
|
|
predicates: &ty::GenericPredicates<'tcx>)
|
2016-09-16 17:25:54 +03:00
|
|
|
-> Result<(), Self::Error> {
|
2017-10-18 17:48:36 +02:00
|
|
|
ty_codec::encode_predicates(self, predicates, |ecx| &mut ecx.predicate_shorthands)
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-22 22:41:09 -05:00
|
|
|
impl<'a, 'tcx> SpecializedEncoder<Fingerprint> for EncodeContext<'a, 'tcx> {
|
|
|
|
fn specialized_encode(&mut self, f: &Fingerprint) -> Result<(), Self::Error> {
|
|
|
|
f.encode_opaque(&mut self.opaque)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 13:47:50 +01:00
|
|
|
impl<'a, 'tcx, T: Encodable> SpecializedEncoder<mir::ClearCrossCrate<T>>
|
|
|
|
for EncodeContext<'a, 'tcx> {
|
|
|
|
fn specialized_encode(&mut self,
|
|
|
|
_: &mir::ClearCrossCrate<T>)
|
|
|
|
-> Result<(), Self::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 17:48:36 +02:00
|
|
|
impl<'a, 'tcx> TyEncoder for EncodeContext<'a, 'tcx> {
|
|
|
|
fn position(&self) -> usize {
|
2016-09-16 17:25:54 +03:00
|
|
|
self.opaque.position()
|
|
|
|
}
|
2017-10-18 17:48:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2016-09-17 10:33:47 +03:00
|
|
|
fn emit_node<F: FnOnce(&mut Self, usize) -> R, R>(&mut self, f: F) -> R {
|
|
|
|
assert_eq!(self.lazy_state, LazyState::NoNode);
|
2016-09-16 17:25:54 +03:00
|
|
|
let pos = self.position();
|
2016-09-17 10:33:47 +03:00
|
|
|
self.lazy_state = LazyState::NodeStart(pos);
|
|
|
|
let r = f(self, pos);
|
|
|
|
self.lazy_state = LazyState::NoNode;
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2016-10-22 18:44:32 +05:30
|
|
|
fn emit_lazy_distance(&mut self,
|
|
|
|
position: usize,
|
|
|
|
min_size: usize)
|
2016-09-17 10:33:47 +03:00
|
|
|
-> Result<(), <Self as Encoder>::Error> {
|
|
|
|
let min_end = position + min_size;
|
|
|
|
let distance = match self.lazy_state {
|
2016-10-22 18:44:32 +05:30
|
|
|
LazyState::NoNode => bug!("emit_lazy_distance: outside of a metadata node"),
|
2016-09-17 10:33:47 +03:00
|
|
|
LazyState::NodeStart(start) => {
|
|
|
|
assert!(min_end <= start);
|
|
|
|
start - min_end
|
|
|
|
}
|
|
|
|
LazyState::Previous(last_min_end) => {
|
2018-04-11 10:47:52 +02:00
|
|
|
assert!(
|
|
|
|
last_min_end <= position,
|
|
|
|
"make sure that the calls to `lazy*` \
|
|
|
|
are in the same order as the metadata fields",
|
|
|
|
);
|
2016-09-17 10:33:47 +03:00
|
|
|
position - last_min_end
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.lazy_state = LazyState::Previous(min_end);
|
|
|
|
self.emit_usize(distance)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lazy<T: Encodable>(&mut self, value: &T) -> Lazy<T> {
|
|
|
|
self.emit_node(|ecx, pos| {
|
|
|
|
value.encode(ecx).unwrap();
|
|
|
|
|
|
|
|
assert!(pos + Lazy::<T>::min_size() <= ecx.position());
|
|
|
|
Lazy::with_position(pos)
|
|
|
|
})
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
|
|
|
|
2017-04-05 13:00:17 +02:00
|
|
|
pub fn lazy_seq<I, T>(&mut self, iter: I) -> LazySeq<T>
|
2016-10-22 18:44:32 +05:30
|
|
|
where I: IntoIterator<Item = T>,
|
|
|
|
T: Encodable
|
|
|
|
{
|
2016-09-17 10:33:47 +03:00
|
|
|
self.emit_node(|ecx, pos| {
|
|
|
|
let len = iter.into_iter().map(|value| value.encode(ecx).unwrap()).count();
|
|
|
|
|
|
|
|
assert!(pos + LazySeq::<T>::min_size(len) <= ecx.position());
|
|
|
|
LazySeq::with_position_and_length(pos, len)
|
|
|
|
})
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
|
|
|
|
2017-04-05 13:00:17 +02:00
|
|
|
pub fn lazy_seq_ref<'b, I, T>(&mut self, iter: I) -> LazySeq<T>
|
2016-10-22 18:44:32 +05:30
|
|
|
where I: IntoIterator<Item = &'b T>,
|
|
|
|
T: 'b + Encodable
|
|
|
|
{
|
2016-09-17 10:33:47 +03:00
|
|
|
self.emit_node(|ecx, pos| {
|
|
|
|
let len = iter.into_iter().map(|value| value.encode(ecx).unwrap()).count();
|
|
|
|
|
|
|
|
assert!(pos + LazySeq::<T>::min_size(len) <= ecx.position());
|
|
|
|
LazySeq::with_position_and_length(pos, len)
|
|
|
|
})
|
2016-09-08 21:36:22 +03:00
|
|
|
}
|
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
// Encodes something that corresponds to a single DepNode::GlobalMetaData
|
|
|
|
// and registers the Fingerprint in the `metadata_hashes` map.
|
|
|
|
pub fn tracked<'x, DATA, R>(&'x mut self,
|
|
|
|
op: fn(&mut IsolatedEncoder<'x, 'a, 'tcx>, DATA) -> R,
|
|
|
|
data: DATA)
|
2017-09-07 12:35:21 -07:00
|
|
|
-> R {
|
2017-11-29 16:28:25 +01:00
|
|
|
op(&mut IsolatedEncoder::new(self), data)
|
2017-04-27 16:12:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn encode_info_for_items(&mut self) -> Index {
|
|
|
|
let krate = self.tcx.hir.krate();
|
|
|
|
let mut index = IndexBuilder::new(self);
|
2018-07-01 11:05:10 -07:00
|
|
|
let vis = Spanned { span: syntax_pos::DUMMY_SP, node: hir::VisibilityKind::Public };
|
2017-04-27 16:12:57 +02:00
|
|
|
index.record(DefId::local(CRATE_DEF_INDEX),
|
|
|
|
IsolatedEncoder::encode_info_for_mod,
|
2018-06-30 20:34:18 -07:00
|
|
|
FromId(CRATE_NODE_ID, (&krate.module, &krate.attrs, &vis)));
|
2017-04-27 16:12:57 +02:00
|
|
|
let mut visitor = EncodeVisitor { index: index };
|
|
|
|
krate.visit_all_item_likes(&mut visitor.as_deep_visitor());
|
|
|
|
for macro_def in &krate.exported_macros {
|
|
|
|
visitor.visit_macro_def(macro_def);
|
|
|
|
}
|
|
|
|
visitor.index.into_items()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn encode_def_path_table(&mut self) -> Lazy<DefPathTable> {
|
|
|
|
let definitions = self.tcx.hir.definitions();
|
|
|
|
self.lazy(definitions.def_path_table())
|
|
|
|
}
|
|
|
|
|
2018-08-18 12:14:09 +02:00
|
|
|
fn encode_source_map(&mut self) -> LazySeq<syntax_pos::SourceFile> {
|
2018-08-18 12:14:14 +02:00
|
|
|
let source_map = self.tcx.sess.source_map();
|
|
|
|
let all_source_files = source_map.files();
|
2017-04-27 16:12:57 +02:00
|
|
|
|
|
|
|
let (working_dir, working_dir_was_remapped) = self.tcx.sess.working_dir.clone();
|
|
|
|
|
2018-08-18 12:13:56 +02:00
|
|
|
let adapted = all_source_files.iter()
|
|
|
|
.filter(|source_file| {
|
|
|
|
// No need to re-export imported source_files, as any downstream
|
2017-04-27 16:12:57 +02:00
|
|
|
// crate will import them from their original source.
|
2018-08-18 12:13:56 +02:00
|
|
|
!source_file.is_imported()
|
2017-04-27 16:12:57 +02:00
|
|
|
})
|
2018-08-18 12:13:56 +02:00
|
|
|
.map(|source_file| {
|
2018-08-18 12:13:52 +02:00
|
|
|
// When exporting SourceFiles, we expand all paths to absolute
|
2017-04-27 16:12:57 +02:00
|
|
|
// paths because any relative paths are potentially relative to
|
|
|
|
// a wrong directory.
|
|
|
|
// However, if a path has been modified via
|
2018-02-18 15:05:24 -08:00
|
|
|
// `--remap-path-prefix` we assume the user has already set
|
2017-04-27 16:12:57 +02:00
|
|
|
// things up the way they want and don't touch the path values
|
|
|
|
// anymore.
|
2018-08-18 12:13:56 +02:00
|
|
|
match source_file.name {
|
2017-12-14 08:09:19 +01:00
|
|
|
FileName::Real(ref name) => {
|
2018-08-18 12:13:56 +02:00
|
|
|
if source_file.name_was_remapped ||
|
2017-12-14 08:09:19 +01:00
|
|
|
(name.is_relative() && working_dir_was_remapped) {
|
2018-08-18 12:13:52 +02:00
|
|
|
// This path of this SourceFile has been modified by
|
2017-12-14 08:09:19 +01:00
|
|
|
// path-remapping, so we use it verbatim (and avoid cloning
|
|
|
|
// the whole map in the process).
|
2018-08-18 12:13:56 +02:00
|
|
|
source_file.clone()
|
2017-12-14 08:09:19 +01:00
|
|
|
} else {
|
2018-08-18 12:13:56 +02:00
|
|
|
let mut adapted = (**source_file).clone();
|
2017-12-14 08:09:19 +01:00
|
|
|
adapted.name = Path::new(&working_dir).join(name).into();
|
2017-12-19 15:14:41 +01:00
|
|
|
adapted.name_hash = {
|
|
|
|
let mut hasher: StableHasher<u128> = StableHasher::new();
|
|
|
|
adapted.name.hash(&mut hasher);
|
|
|
|
hasher.finish()
|
|
|
|
};
|
2018-02-27 17:11:14 +01:00
|
|
|
Lrc::new(adapted)
|
2017-12-14 08:09:19 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
// expanded code, not from a file
|
2018-08-18 12:13:56 +02:00
|
|
|
_ => source_file.clone(),
|
2017-04-27 16:12:57 +02:00
|
|
|
}
|
2017-05-23 16:21:24 +02:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2017-04-27 16:12:57 +02:00
|
|
|
|
2017-05-23 16:21:24 +02:00
|
|
|
self.lazy_seq_ref(adapted.iter().map(|rc| &**rc))
|
2017-04-27 16:12:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn encode_crate_root(&mut self) -> Lazy<CrateRoot> {
|
|
|
|
let mut i = self.position();
|
|
|
|
|
2017-11-29 16:28:25 +01:00
|
|
|
let crate_deps = self.tracked(IsolatedEncoder::encode_crate_deps, ());
|
2017-04-27 16:12:57 +02:00
|
|
|
let dylib_dependency_formats = self.tracked(
|
|
|
|
IsolatedEncoder::encode_dylib_dependency_formats,
|
|
|
|
());
|
|
|
|
let dep_bytes = self.position() - i;
|
|
|
|
|
2018-07-23 01:20:33 +01:00
|
|
|
// Encode the lib features.
|
|
|
|
i = self.position();
|
|
|
|
let lib_features = self.tracked(IsolatedEncoder::encode_lib_features, ());
|
|
|
|
let lib_feature_bytes = self.position() - i;
|
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
// Encode the language items.
|
|
|
|
i = self.position();
|
2017-11-29 16:28:25 +01:00
|
|
|
let lang_items = self.tracked(IsolatedEncoder::encode_lang_items, ());
|
2017-04-27 16:12:57 +02:00
|
|
|
let lang_items_missing = self.tracked(
|
|
|
|
IsolatedEncoder::encode_lang_items_missing,
|
|
|
|
());
|
|
|
|
let lang_item_bytes = self.position() - i;
|
|
|
|
|
|
|
|
// Encode the native libraries used
|
|
|
|
i = self.position();
|
|
|
|
let native_libraries = self.tracked(
|
|
|
|
IsolatedEncoder::encode_native_libraries,
|
|
|
|
());
|
|
|
|
let native_lib_bytes = self.position() - i;
|
|
|
|
|
2018-02-10 14:28:17 -08:00
|
|
|
let foreign_modules = self.tracked(
|
|
|
|
IsolatedEncoder::encode_foreign_modules,
|
|
|
|
());
|
|
|
|
|
2018-08-18 12:14:14 +02:00
|
|
|
// Encode source_map
|
2017-04-27 16:12:57 +02:00
|
|
|
i = self.position();
|
2018-08-18 12:14:14 +02:00
|
|
|
let source_map = self.encode_source_map();
|
|
|
|
let source_map_bytes = self.position() - i;
|
2017-04-27 16:12:57 +02:00
|
|
|
|
|
|
|
// Encode DefPathTable
|
|
|
|
i = self.position();
|
|
|
|
let def_path_table = self.encode_def_path_table();
|
|
|
|
let def_path_table_bytes = self.position() - i;
|
|
|
|
|
|
|
|
// Encode the def IDs of impls, for coherence checking.
|
|
|
|
i = self.position();
|
2017-11-29 16:28:25 +01:00
|
|
|
let impls = self.tracked(IsolatedEncoder::encode_impls, ());
|
2017-04-27 16:12:57 +02:00
|
|
|
let impl_bytes = self.position() - i;
|
|
|
|
|
|
|
|
// Encode exported symbols info.
|
|
|
|
i = self.position();
|
2018-02-27 19:28:21 +01:00
|
|
|
let exported_symbols = self.tcx.exported_symbols(LOCAL_CRATE);
|
|
|
|
let exported_symbols = self.tracked(
|
|
|
|
IsolatedEncoder::encode_exported_symbols,
|
|
|
|
&exported_symbols);
|
|
|
|
let exported_symbols_bytes = self.position() - i;
|
2017-04-27 16:12:57 +02:00
|
|
|
|
|
|
|
let tcx = self.tcx;
|
2018-04-10 09:58:46 +02:00
|
|
|
|
2018-04-11 13:31:37 +02:00
|
|
|
// Encode the items.
|
|
|
|
i = self.position();
|
|
|
|
let items = self.encode_info_for_items();
|
|
|
|
let item_bytes = self.position() - i;
|
|
|
|
|
2018-04-10 09:58:46 +02:00
|
|
|
// Encode the allocation index
|
|
|
|
let interpret_alloc_index = {
|
|
|
|
let mut interpret_alloc_index = Vec::new();
|
|
|
|
let mut n = 0;
|
2018-04-11 13:31:37 +02:00
|
|
|
trace!("beginning to encode alloc ids");
|
2018-04-10 09:58:46 +02:00
|
|
|
loop {
|
2018-04-13 18:48:41 +02:00
|
|
|
let new_n = self.interpret_allocs_inverse.len();
|
2018-04-11 13:31:37 +02:00
|
|
|
// if we have found new ids, serialize those, too
|
|
|
|
if n == new_n {
|
|
|
|
// otherwise, abort
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
trace!("encoding {} further alloc ids", new_n - n);
|
2018-04-10 09:58:46 +02:00
|
|
|
for idx in n..new_n {
|
|
|
|
let id = self.interpret_allocs_inverse[idx];
|
|
|
|
let pos = self.position() as u32;
|
|
|
|
interpret_alloc_index.push(pos);
|
|
|
|
interpret::specialized_encode_alloc_id(
|
|
|
|
self,
|
|
|
|
tcx,
|
|
|
|
id,
|
|
|
|
).unwrap();
|
|
|
|
}
|
|
|
|
n = new_n;
|
|
|
|
}
|
2018-04-11 10:47:52 +02:00
|
|
|
self.lazy_seq(interpret_alloc_index)
|
2018-04-10 09:58:46 +02:00
|
|
|
};
|
|
|
|
|
2018-04-11 13:31:37 +02:00
|
|
|
// Index the items
|
2018-04-11 10:47:52 +02:00
|
|
|
i = self.position();
|
2018-06-04 22:14:02 +02:00
|
|
|
let index = items.write_index(&mut self.opaque);
|
2018-04-11 10:47:52 +02:00
|
|
|
let index_bytes = self.position() - i;
|
|
|
|
|
2018-05-12 10:20:53 -07:00
|
|
|
let attrs = tcx.hir.krate_attrs();
|
2018-04-11 10:47:52 +02:00
|
|
|
let link_meta = self.link_meta;
|
2018-07-26 11:13:11 -06:00
|
|
|
let is_proc_macro = tcx.sess.crate_types.borrow().contains(&CrateType::ProcMacro);
|
2018-05-12 10:20:53 -07:00
|
|
|
let has_default_lib_allocator = attr::contains_name(&attrs, "default_lib_allocator");
|
2018-04-13 18:48:41 +02:00
|
|
|
let has_global_allocator = *tcx.sess.has_global_allocator.get();
|
2018-04-11 10:47:52 +02:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
let root = self.lazy(&CrateRoot {
|
|
|
|
name: tcx.crate_name(LOCAL_CRATE),
|
2018-03-13 11:58:53 -07:00
|
|
|
extra_filename: tcx.sess.opts.cg.extra_filename.clone(),
|
2017-04-27 16:12:57 +02:00
|
|
|
triple: tcx.sess.opts.target_triple.clone(),
|
|
|
|
hash: link_meta.crate_hash,
|
|
|
|
disambiguator: tcx.sess.local_crate_disambiguator(),
|
2017-09-07 12:35:21 -07:00
|
|
|
panic_strategy: tcx.sess.panic_strategy(),
|
2018-05-13 03:50:39 +03:00
|
|
|
edition: hygiene::default_edition(),
|
2017-09-07 12:35:21 -07:00
|
|
|
has_global_allocator: has_global_allocator,
|
|
|
|
has_default_lib_allocator: has_default_lib_allocator,
|
2017-04-27 16:12:57 +02:00
|
|
|
plugin_registrar_fn: tcx.sess
|
|
|
|
.plugin_registrar_fn
|
|
|
|
.get()
|
|
|
|
.map(|id| tcx.hir.local_def_id(id).index),
|
|
|
|
macro_derive_registrar: if is_proc_macro {
|
|
|
|
let id = tcx.sess.derive_registrar_fn.get().unwrap();
|
|
|
|
Some(tcx.hir.local_def_id(id).index)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
|
2018-05-12 10:20:53 -07:00
|
|
|
compiler_builtins: attr::contains_name(&attrs, "compiler_builtins"),
|
|
|
|
needs_allocator: attr::contains_name(&attrs, "needs_allocator"),
|
|
|
|
needs_panic_runtime: attr::contains_name(&attrs, "needs_panic_runtime"),
|
|
|
|
no_builtins: attr::contains_name(&attrs, "no_builtins"),
|
|
|
|
panic_runtime: attr::contains_name(&attrs, "panic_runtime"),
|
|
|
|
profiler_runtime: attr::contains_name(&attrs, "profiler_runtime"),
|
|
|
|
sanitizer_runtime: attr::contains_name(&attrs, "sanitizer_runtime"),
|
|
|
|
|
2017-08-06 22:54:09 -07:00
|
|
|
crate_deps,
|
|
|
|
dylib_dependency_formats,
|
2018-07-23 01:20:33 +01:00
|
|
|
lib_features,
|
2017-08-06 22:54:09 -07:00
|
|
|
lang_items,
|
|
|
|
lang_items_missing,
|
|
|
|
native_libraries,
|
2018-02-10 14:28:17 -08:00
|
|
|
foreign_modules,
|
2018-08-18 12:14:14 +02:00
|
|
|
source_map,
|
2017-08-06 22:54:09 -07:00
|
|
|
def_path_table,
|
|
|
|
impls,
|
2018-02-27 19:28:21 +01:00
|
|
|
exported_symbols,
|
2018-04-10 09:58:46 +02:00
|
|
|
interpret_alloc_index,
|
2018-04-11 10:47:52 +02:00
|
|
|
index,
|
2017-04-27 16:12:57 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
let total_bytes = self.position();
|
|
|
|
|
|
|
|
if self.tcx.sess.meta_stats() {
|
|
|
|
let mut zero_bytes = 0;
|
2018-06-04 22:14:02 +02:00
|
|
|
for e in self.opaque.data.iter() {
|
2017-04-27 16:12:57 +02:00
|
|
|
if *e == 0 {
|
|
|
|
zero_bytes += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("metadata stats:");
|
|
|
|
println!(" dep bytes: {}", dep_bytes);
|
2018-07-23 01:20:33 +01:00
|
|
|
println!(" lib feature bytes: {}", lib_feature_bytes);
|
2017-04-27 16:12:57 +02:00
|
|
|
println!(" lang item bytes: {}", lang_item_bytes);
|
|
|
|
println!(" native bytes: {}", native_lib_bytes);
|
2018-08-18 12:14:14 +02:00
|
|
|
println!(" source_map bytes: {}", source_map_bytes);
|
2017-04-27 16:12:57 +02:00
|
|
|
println!(" impl bytes: {}", impl_bytes);
|
2018-02-27 19:28:21 +01:00
|
|
|
println!(" exp. symbols bytes: {}", exported_symbols_bytes);
|
2017-04-27 16:12:57 +02:00
|
|
|
println!(" def-path table bytes: {}", def_path_table_bytes);
|
|
|
|
println!(" item bytes: {}", item_bytes);
|
|
|
|
println!(" index bytes: {}", index_bytes);
|
|
|
|
println!(" zero bytes: {}", zero_bytes);
|
|
|
|
println!(" total bytes: {}", total_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
root
|
|
|
|
}
|
2017-04-05 13:00:17 +02:00
|
|
|
}
|
2015-09-17 14:29:59 -04:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
// These are methods for encoding various things. They are meant to be used with
|
|
|
|
// IndexBuilder::record() and EncodeContext::tracked(). They actually
|
|
|
|
// would not have to be methods of IsolatedEncoder (free standing functions
|
|
|
|
// taking IsolatedEncoder as first argument would be just fine) but by making
|
|
|
|
// them methods we don't have to repeat the lengthy `<'a, 'b: 'a, 'tcx: 'b>`
|
|
|
|
// clause again and again.
|
|
|
|
impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
|
2017-05-05 14:34:42 -04:00
|
|
|
fn encode_variances_of(&mut self, def_id: DefId) -> LazySeq<ty::Variance> {
|
2017-04-27 16:12:57 +02:00
|
|
|
debug!("IsolatedEncoder::encode_variances_of({:?})", def_id);
|
2016-09-16 17:25:54 +03:00
|
|
|
let tcx = self.tcx;
|
2017-04-24 15:20:46 +03:00
|
|
|
self.lazy_seq_from_slice(&tcx.variances_of(def_id))
|
2016-08-10 15:33:36 -04:00
|
|
|
}
|
2012-01-10 14:50:40 -07:00
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
fn encode_item_type(&mut self, def_id: DefId) -> Lazy<Ty<'tcx>> {
|
|
|
|
let tcx = self.tcx;
|
2017-04-24 15:20:46 +03:00
|
|
|
let ty = tcx.type_of(def_id);
|
2017-04-27 16:12:57 +02:00
|
|
|
debug!("IsolatedEncoder::encode_item_type({:?}) => {:?}", def_id, ty);
|
2017-04-05 23:39:02 +02:00
|
|
|
self.lazy(&ty)
|
2016-08-12 15:59:53 -04:00
|
|
|
}
|
Add stability inheritance
This commit makes several changes to the stability index infrastructure:
* Stability levels are now inherited lexically, i.e., each item's
stability level becomes the default for any nested items.
* The computed stability level for an item is stored as part of the
metadata. When using an item from an external crate, this data is
looked up and cached.
* The stability lint works from the computed stability level, rather
than manual stability attribute annotations. However, the lint still
checks only a limited set of item uses (e.g., it does not check every
component of a path on import). This will be addressed in a later PR,
as part of issue #8962.
* The stability lint only applies to items originating from external
crates, since the stability index is intended as a promise to
downstream crates.
* The "experimental" lint is now _allow_ by default. This is because
almost all existing crates have been marked "experimental", pending
library stabilization. With inheritance in place, this would generate
a massive explosion of warnings for every Rust program.
The lint should be changed back to deny-by-default after library
stabilization is complete.
* The "deprecated" lint still warns by default.
The net result: we can begin tracking stability index for the standard
libraries as we stabilize, without impacting most clients.
Closes #13540.
2014-06-11 17:23:11 -07:00
|
|
|
|
2016-08-16 11:42:30 -04:00
|
|
|
/// Encode data for the given variant of the given ADT. The
|
|
|
|
/// index of the variant is untracked: this is ok because we
|
|
|
|
/// will have to lookup the adt-def by its id, and that gives us
|
|
|
|
/// the right to access any information in the adt-def (including,
|
|
|
|
/// e.g., the length of the various vectors).
|
2016-08-12 15:59:53 -04:00
|
|
|
fn encode_enum_variant_info(&mut self,
|
2016-10-22 18:44:32 +05:30
|
|
|
(enum_did, Untracked(index)): (DefId, Untracked<usize>))
|
|
|
|
-> Entry<'tcx> {
|
2016-08-29 08:55:40 +03:00
|
|
|
let tcx = self.tcx;
|
2017-04-24 15:20:46 +03:00
|
|
|
let def = tcx.adt_def(enum_did);
|
2016-08-12 15:59:53 -04:00
|
|
|
let variant = &def.variants[index];
|
2016-09-16 17:25:54 +03:00
|
|
|
let def_id = variant.did;
|
2017-04-27 16:12:57 +02:00
|
|
|
debug!("IsolatedEncoder::encode_enum_variant_info({:?})", def_id);
|
2016-09-16 17:25:54 +03:00
|
|
|
|
|
|
|
let data = VariantData {
|
2016-09-15 00:51:46 +03:00
|
|
|
ctor_kind: variant.ctor_kind,
|
2017-02-05 07:01:48 +02:00
|
|
|
discr: variant.discr,
|
2016-10-22 18:44:32 +05:30
|
|
|
struct_ctor: None,
|
2017-05-13 17:11:52 +03:00
|
|
|
ctor_sig: if variant.ctor_kind == CtorKind::Fn {
|
|
|
|
Some(self.lazy(&tcx.fn_sig(def_id)))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2016-09-16 17:25:54 +03:00
|
|
|
};
|
2016-08-14 21:16:16 -04:00
|
|
|
|
2017-01-26 02:41:06 +02:00
|
|
|
let enum_id = tcx.hir.as_local_node_id(enum_did).unwrap();
|
|
|
|
let enum_vis = &tcx.hir.expect_item(enum_id).vis;
|
2014-02-14 07:07:09 +02:00
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
Entry {
|
|
|
|
kind: EntryKind::Variant(self.lazy(&data)),
|
2016-12-20 08:32:15 +00:00
|
|
|
visibility: self.lazy(&ty::Visibility::from_hir(enum_vis, enum_id, tcx)),
|
2016-11-24 01:39:13 +02:00
|
|
|
span: self.lazy(&tcx.def_span(def_id)),
|
2016-09-16 17:25:54 +03:00
|
|
|
attributes: self.encode_attributes(&tcx.get_attrs(def_id)),
|
|
|
|
children: self.lazy_seq(variant.fields.iter().map(|f| {
|
|
|
|
assert!(f.did.is_local());
|
|
|
|
f.did.index
|
|
|
|
})),
|
|
|
|
stability: self.encode_stability(def_id),
|
|
|
|
deprecation: self.encode_deprecation(def_id),
|
|
|
|
|
|
|
|
ty: Some(self.encode_item_type(def_id)),
|
|
|
|
inherent_impls: LazySeq::empty(),
|
2017-06-02 22:05:41 +03:00
|
|
|
variances: if variant.ctor_kind == CtorKind::Fn {
|
|
|
|
self.encode_variances_of(def_id)
|
|
|
|
} else {
|
|
|
|
LazySeq::empty()
|
|
|
|
},
|
2016-09-16 17:25:54 +03:00
|
|
|
generics: Some(self.encode_generics(def_id)),
|
|
|
|
predicates: Some(self.encode_predicates(def_id)),
|
2018-07-02 10:35:30 -04:00
|
|
|
predicates_defined_on: None,
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2017-05-02 06:08:13 -04:00
|
|
|
mir: self.encode_optimized_mir(def_id),
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
2011-06-27 15:20:17 -07:00
|
|
|
}
|
|
|
|
|
2016-08-10 15:33:36 -04:00
|
|
|
fn encode_info_for_mod(&mut self,
|
2016-10-22 18:44:32 +05:30
|
|
|
FromId(id, (md, attrs, vis)): FromId<(&hir::Mod,
|
|
|
|
&[ast::Attribute],
|
|
|
|
&hir::Visibility)>)
|
2016-09-16 17:25:54 +03:00
|
|
|
-> Entry<'tcx> {
|
2016-08-29 08:55:40 +03:00
|
|
|
let tcx = self.tcx;
|
2017-01-26 02:41:06 +02:00
|
|
|
let def_id = tcx.hir.local_def_id(id);
|
2017-04-27 16:12:57 +02:00
|
|
|
debug!("IsolatedEncoder::encode_info_for_mod({:?})", def_id);
|
2012-05-16 22:28:01 -07:00
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
let data = ModData {
|
2017-09-08 13:51:57 -07:00
|
|
|
reexports: match tcx.module_exports(def_id) {
|
2017-11-29 11:20:49 -08:00
|
|
|
Some(ref exports) => self.lazy_seq_from_slice(exports.as_slice()),
|
2016-10-22 18:44:32 +05:30
|
|
|
_ => LazySeq::empty(),
|
|
|
|
},
|
2016-09-08 19:05:50 +03:00
|
|
|
};
|
2012-08-17 12:41:34 -07:00
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
Entry {
|
|
|
|
kind: EntryKind::Mod(self.lazy(&data)),
|
2016-12-20 08:32:15 +00:00
|
|
|
visibility: self.lazy(&ty::Visibility::from_hir(vis, id, tcx)),
|
2017-08-08 15:12:39 +02:00
|
|
|
span: self.lazy(&tcx.def_span(def_id)),
|
2016-09-16 17:25:54 +03:00
|
|
|
attributes: self.encode_attributes(attrs),
|
|
|
|
children: self.lazy_seq(md.item_ids.iter().map(|item_id| {
|
2017-01-26 02:41:06 +02:00
|
|
|
tcx.hir.local_def_id(item_id.id).index
|
2016-09-16 17:25:54 +03:00
|
|
|
})),
|
|
|
|
stability: self.encode_stability(def_id),
|
|
|
|
deprecation: self.encode_deprecation(def_id),
|
|
|
|
|
|
|
|
ty: None,
|
|
|
|
inherent_impls: LazySeq::empty(),
|
|
|
|
variances: LazySeq::empty(),
|
|
|
|
generics: None,
|
|
|
|
predicates: None,
|
2018-07-02 10:35:30 -04:00
|
|
|
predicates_defined_on: None,
|
2016-09-16 17:25:54 +03:00
|
|
|
|
|
|
|
mir: None
|
|
|
|
}
|
2016-08-10 15:33:36 -04:00
|
|
|
}
|
2016-08-12 16:18:22 -04:00
|
|
|
|
2016-08-16 11:42:30 -04:00
|
|
|
/// Encode data for the given field of the given variant of the
|
|
|
|
/// given ADT. The indices of the variant/field are untracked:
|
|
|
|
/// this is ok because we will have to lookup the adt-def by its
|
|
|
|
/// id, and that gives us the right to access any information in
|
|
|
|
/// the adt-def (including, e.g., the length of the various
|
|
|
|
/// vectors).
|
2016-08-10 15:33:36 -04:00
|
|
|
fn encode_field(&mut self,
|
2016-10-22 18:44:32 +05:30
|
|
|
(adt_def_id, Untracked((variant_index, field_index))): (DefId,
|
|
|
|
Untracked<(usize,
|
|
|
|
usize)>))
|
|
|
|
-> Entry<'tcx> {
|
2016-08-29 08:55:40 +03:00
|
|
|
let tcx = self.tcx;
|
2017-04-24 15:20:46 +03:00
|
|
|
let variant = &tcx.adt_def(adt_def_id).variants[variant_index];
|
2016-08-12 16:18:22 -04:00
|
|
|
let field = &variant.fields[field_index];
|
2013-02-11 16:28:39 -08:00
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
let def_id = field.did;
|
2017-04-27 16:12:57 +02:00
|
|
|
debug!("IsolatedEncoder::encode_field({:?})", def_id);
|
2017-04-05 23:39:02 +02:00
|
|
|
|
2017-01-26 02:41:06 +02:00
|
|
|
let variant_id = tcx.hir.as_local_node_id(variant.did).unwrap();
|
|
|
|
let variant_data = tcx.hir.expect_variant_data(variant_id);
|
2016-09-16 17:25:54 +03:00
|
|
|
|
|
|
|
Entry {
|
|
|
|
kind: EntryKind::Field,
|
2016-12-20 08:32:15 +00:00
|
|
|
visibility: self.lazy(&field.vis),
|
2016-11-24 01:39:13 +02:00
|
|
|
span: self.lazy(&tcx.def_span(def_id)),
|
2016-09-16 17:25:54 +03:00
|
|
|
attributes: self.encode_attributes(&variant_data.fields()[field_index].attrs),
|
|
|
|
children: LazySeq::empty(),
|
|
|
|
stability: self.encode_stability(def_id),
|
|
|
|
deprecation: self.encode_deprecation(def_id),
|
|
|
|
|
|
|
|
ty: Some(self.encode_item_type(def_id)),
|
|
|
|
inherent_impls: LazySeq::empty(),
|
|
|
|
variances: LazySeq::empty(),
|
|
|
|
generics: Some(self.encode_generics(def_id)),
|
|
|
|
predicates: Some(self.encode_predicates(def_id)),
|
2018-07-02 10:35:30 -04:00
|
|
|
predicates_defined_on: None,
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2016-10-22 18:44:32 +05:30
|
|
|
mir: None,
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
2016-08-10 15:33:36 -04:00
|
|
|
}
|
2013-02-11 16:28:39 -08:00
|
|
|
|
2016-10-22 18:44:32 +05:30
|
|
|
fn encode_struct_ctor(&mut self, (adt_def_id, def_id): (DefId, DefId)) -> Entry<'tcx> {
|
2017-04-27 16:12:57 +02:00
|
|
|
debug!("IsolatedEncoder::encode_struct_ctor({:?})", def_id);
|
2016-09-15 00:51:46 +03:00
|
|
|
let tcx = self.tcx;
|
2017-11-03 19:17:54 +00:00
|
|
|
let adt_def = tcx.adt_def(adt_def_id);
|
2018-01-07 22:41:41 +01:00
|
|
|
let variant = adt_def.non_enum_variant();
|
2016-09-08 19:05:50 +03:00
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
let data = VariantData {
|
2016-09-15 00:51:46 +03:00
|
|
|
ctor_kind: variant.ctor_kind,
|
2017-02-05 07:01:48 +02:00
|
|
|
discr: variant.discr,
|
2016-10-22 18:44:32 +05:30
|
|
|
struct_ctor: Some(def_id.index),
|
2017-05-13 17:11:52 +03:00
|
|
|
ctor_sig: if variant.ctor_kind == CtorKind::Fn {
|
|
|
|
Some(self.lazy(&tcx.fn_sig(def_id)))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2016-09-16 17:25:54 +03:00
|
|
|
};
|
|
|
|
|
2017-01-26 02:41:06 +02:00
|
|
|
let struct_id = tcx.hir.as_local_node_id(adt_def_id).unwrap();
|
|
|
|
let struct_vis = &tcx.hir.expect_item(struct_id).vis;
|
2017-01-09 00:21:35 +03:00
|
|
|
let mut ctor_vis = ty::Visibility::from_hir(struct_vis, struct_id, tcx);
|
|
|
|
for field in &variant.fields {
|
|
|
|
if ctor_vis.is_at_least(field.vis, tcx) {
|
|
|
|
ctor_vis = field.vis;
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 00:51:46 +03:00
|
|
|
|
2017-11-03 19:17:54 +00:00
|
|
|
// If the structure is marked as non_exhaustive then lower the visibility
|
|
|
|
// to within the crate.
|
|
|
|
if adt_def.is_non_exhaustive() && ctor_vis == ty::Visibility::Public {
|
|
|
|
ctor_vis = ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));
|
|
|
|
}
|
|
|
|
|
2017-02-06 15:26:32 -05:00
|
|
|
let repr_options = get_repr_options(&tcx, adt_def_id);
|
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
Entry {
|
2017-02-06 15:26:32 -05:00
|
|
|
kind: EntryKind::Struct(self.lazy(&data), repr_options),
|
2017-01-09 00:21:35 +03:00
|
|
|
visibility: self.lazy(&ctor_vis),
|
2016-11-24 01:39:13 +02:00
|
|
|
span: self.lazy(&tcx.def_span(def_id)),
|
2016-09-16 17:25:54 +03:00
|
|
|
attributes: LazySeq::empty(),
|
|
|
|
children: LazySeq::empty(),
|
|
|
|
stability: self.encode_stability(def_id),
|
|
|
|
deprecation: self.encode_deprecation(def_id),
|
|
|
|
|
|
|
|
ty: Some(self.encode_item_type(def_id)),
|
|
|
|
inherent_impls: LazySeq::empty(),
|
2017-06-02 22:05:41 +03:00
|
|
|
variances: if variant.ctor_kind == CtorKind::Fn {
|
|
|
|
self.encode_variances_of(def_id)
|
|
|
|
} else {
|
|
|
|
LazySeq::empty()
|
|
|
|
},
|
2016-09-16 17:25:54 +03:00
|
|
|
generics: Some(self.encode_generics(def_id)),
|
|
|
|
predicates: Some(self.encode_predicates(def_id)),
|
2018-07-02 10:35:30 -04:00
|
|
|
predicates_defined_on: None,
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2017-05-02 06:08:13 -04:00
|
|
|
mir: self.encode_optimized_mir(def_id),
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
2016-08-11 09:19:42 +03:00
|
|
|
}
|
2016-08-10 15:33:36 -04:00
|
|
|
|
2017-01-25 22:01:11 +02:00
|
|
|
fn encode_generics(&mut self, def_id: DefId) -> Lazy<ty::Generics> {
|
2017-04-27 16:12:57 +02:00
|
|
|
debug!("IsolatedEncoder::encode_generics({:?})", def_id);
|
2016-09-16 17:25:54 +03:00
|
|
|
let tcx = self.tcx;
|
2017-04-24 15:20:46 +03:00
|
|
|
self.lazy(tcx.generics_of(def_id))
|
2016-08-08 23:39:49 +03:00
|
|
|
}
|
2014-08-27 21:46:52 -04:00
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
fn encode_predicates(&mut self, def_id: DefId) -> Lazy<ty::GenericPredicates<'tcx>> {
|
2017-04-27 16:12:57 +02:00
|
|
|
debug!("IsolatedEncoder::encode_predicates({:?})", def_id);
|
2016-09-16 17:25:54 +03:00
|
|
|
let tcx = self.tcx;
|
2017-04-24 15:20:46 +03:00
|
|
|
self.lazy(&tcx.predicates_of(def_id))
|
2013-07-16 16:28:33 -07:00
|
|
|
}
|
2013-03-28 14:27:54 -04:00
|
|
|
|
2018-07-02 10:35:30 -04:00
|
|
|
fn encode_predicates_defined_on(&mut self, def_id: DefId) -> Lazy<ty::GenericPredicates<'tcx>> {
|
|
|
|
debug!("IsolatedEncoder::encode_predicates_defined_on({:?})", def_id);
|
|
|
|
let tcx = self.tcx;
|
|
|
|
self.lazy(&tcx.predicates_defined_on(def_id))
|
|
|
|
}
|
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
fn encode_info_for_trait_item(&mut self, def_id: DefId) -> Entry<'tcx> {
|
2017-04-27 16:12:57 +02:00
|
|
|
debug!("IsolatedEncoder::encode_info_for_trait_item({:?})", def_id);
|
2016-08-29 08:55:40 +03:00
|
|
|
let tcx = self.tcx;
|
2016-08-14 18:38:42 -04:00
|
|
|
|
2017-01-26 02:41:06 +02:00
|
|
|
let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
|
|
|
|
let ast_item = tcx.hir.expect_trait_item(node_id);
|
2016-11-10 02:06:34 +02:00
|
|
|
let trait_item = tcx.associated_item(def_id);
|
2016-08-14 18:38:42 -04:00
|
|
|
|
2016-11-14 11:00:02 -05:00
|
|
|
let container = match trait_item.defaultness {
|
|
|
|
hir::Defaultness::Default { has_value: true } =>
|
|
|
|
AssociatedContainer::TraitWithDefault,
|
|
|
|
hir::Defaultness::Default { has_value: false } =>
|
|
|
|
AssociatedContainer::TraitRequired,
|
|
|
|
hir::Defaultness::Final =>
|
|
|
|
span_bug!(ast_item.span, "traits cannot have final items"),
|
2016-09-16 17:25:54 +03:00
|
|
|
};
|
2016-08-14 18:38:42 -04:00
|
|
|
|
2016-11-10 02:06:34 +02:00
|
|
|
let kind = match trait_item.kind {
|
2017-02-20 03:55:28 +02:00
|
|
|
ty::AssociatedKind::Const => {
|
2018-04-15 19:41:33 -04:00
|
|
|
let const_qualif =
|
|
|
|
if let hir::TraitItemKind::Const(_, Some(body)) = ast_item.node {
|
|
|
|
self.const_qualif(0, body)
|
|
|
|
} else {
|
|
|
|
ConstQualif { mir: 0, ast_promotable: false }
|
|
|
|
};
|
|
|
|
|
|
|
|
let rendered =
|
|
|
|
hir::print::to_string(&self.tcx.hir, |s| s.print_trait_item(ast_item));
|
|
|
|
let rendered_const = self.lazy(&RenderedConst(rendered));
|
|
|
|
|
|
|
|
EntryKind::AssociatedConst(container, const_qualif, rendered_const)
|
2017-02-20 03:55:28 +02:00
|
|
|
}
|
2016-11-10 02:06:34 +02:00
|
|
|
ty::AssociatedKind::Method => {
|
2016-12-20 22:46:11 +02:00
|
|
|
let fn_data = if let hir::TraitItemKind::Method(_, ref m) = ast_item.node {
|
|
|
|
let arg_names = match *m {
|
|
|
|
hir::TraitMethod::Required(ref names) => {
|
|
|
|
self.encode_fn_arg_names(names)
|
|
|
|
}
|
|
|
|
hir::TraitMethod::Provided(body) => {
|
|
|
|
self.encode_fn_arg_names_for_body(body)
|
|
|
|
}
|
|
|
|
};
|
2016-09-16 17:25:54 +03:00
|
|
|
FnData {
|
|
|
|
constness: hir::Constness::NotConst,
|
2017-08-06 22:54:09 -07:00
|
|
|
arg_names,
|
2017-05-13 17:11:52 +03:00
|
|
|
sig: self.lazy(&tcx.fn_sig(def_id)),
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bug!()
|
|
|
|
};
|
2016-11-10 02:06:34 +02:00
|
|
|
EntryKind::Method(self.lazy(&MethodData {
|
2017-08-06 22:54:09 -07:00
|
|
|
fn_data,
|
|
|
|
container,
|
2016-11-10 02:06:34 +02:00
|
|
|
has_self: trait_item.method_has_self_argument,
|
|
|
|
}))
|
2016-09-08 19:05:50 +03:00
|
|
|
}
|
2016-11-10 02:06:34 +02:00
|
|
|
ty::AssociatedKind::Type => EntryKind::AssociatedType(container),
|
2018-07-03 19:38:14 +02:00
|
|
|
ty::AssociatedKind::Existential =>
|
|
|
|
span_bug!(ast_item.span, "existential type in trait"),
|
2016-09-08 19:05:50 +03:00
|
|
|
};
|
2016-08-14 18:38:42 -04:00
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
Entry {
|
2017-08-06 22:54:09 -07:00
|
|
|
kind,
|
2016-12-20 08:32:15 +00:00
|
|
|
visibility: self.lazy(&trait_item.vis),
|
2016-11-24 01:39:13 +02:00
|
|
|
span: self.lazy(&ast_item.span),
|
2016-09-16 17:25:54 +03:00
|
|
|
attributes: self.encode_attributes(&ast_item.attrs),
|
|
|
|
children: LazySeq::empty(),
|
|
|
|
stability: self.encode_stability(def_id),
|
|
|
|
deprecation: self.encode_deprecation(def_id),
|
|
|
|
|
2016-11-10 02:06:34 +02:00
|
|
|
ty: match trait_item.kind {
|
|
|
|
ty::AssociatedKind::Const |
|
|
|
|
ty::AssociatedKind::Method => {
|
|
|
|
Some(self.encode_item_type(def_id))
|
|
|
|
}
|
|
|
|
ty::AssociatedKind::Type => {
|
2016-11-14 11:00:02 -05:00
|
|
|
if trait_item.defaultness.has_value() {
|
2016-11-10 02:06:34 +02:00
|
|
|
Some(self.encode_item_type(def_id))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
2018-07-03 19:38:14 +02:00
|
|
|
ty::AssociatedKind::Existential => unreachable!(),
|
2016-09-16 17:25:54 +03:00
|
|
|
},
|
|
|
|
inherent_impls: LazySeq::empty(),
|
2017-06-02 22:05:41 +03:00
|
|
|
variances: if trait_item.kind == ty::AssociatedKind::Method {
|
|
|
|
self.encode_variances_of(def_id)
|
|
|
|
} else {
|
|
|
|
LazySeq::empty()
|
|
|
|
},
|
2016-09-16 17:25:54 +03:00
|
|
|
generics: Some(self.encode_generics(def_id)),
|
|
|
|
predicates: Some(self.encode_predicates(def_id)),
|
2018-07-02 10:35:30 -04:00
|
|
|
predicates_defined_on: None,
|
2016-09-08 19:05:50 +03:00
|
|
|
|
2017-05-02 06:08:13 -04:00
|
|
|
mir: self.encode_optimized_mir(def_id),
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
2016-08-14 18:38:42 -04:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:53:30 +01:00
|
|
|
fn metadata_output_only(&self) -> bool {
|
|
|
|
// MIR optimisation can be skipped when we're just interested in the metadata.
|
2018-05-08 16:10:16 +03:00
|
|
|
!self.tcx.sess.opts.output_types.should_codegen()
|
2018-03-27 22:53:30 +01:00
|
|
|
}
|
|
|
|
|
2018-04-15 19:41:33 -04:00
|
|
|
fn const_qualif(&self, mir: u8, body_id: hir::BodyId) -> ConstQualif {
|
|
|
|
let body_owner_def_id = self.tcx.hir.body_owner_def_id(body_id);
|
|
|
|
let ast_promotable = self.tcx.const_is_rvalue_promotable_to_static(body_owner_def_id);
|
|
|
|
|
|
|
|
ConstQualif { mir, ast_promotable }
|
|
|
|
}
|
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
fn encode_info_for_impl_item(&mut self, def_id: DefId) -> Entry<'tcx> {
|
2017-04-27 16:12:57 +02:00
|
|
|
debug!("IsolatedEncoder::encode_info_for_impl_item({:?})", def_id);
|
2017-05-13 17:11:52 +03:00
|
|
|
let tcx = self.tcx;
|
|
|
|
|
2017-01-26 02:41:06 +02:00
|
|
|
let node_id = self.tcx.hir.as_local_node_id(def_id).unwrap();
|
|
|
|
let ast_item = self.tcx.hir.expect_impl_item(node_id);
|
2016-11-10 02:06:34 +02:00
|
|
|
let impl_item = self.tcx.associated_item(def_id);
|
2016-09-08 19:05:50 +03:00
|
|
|
|
2016-11-10 02:06:34 +02:00
|
|
|
let container = match impl_item.defaultness {
|
2016-11-14 11:00:02 -05:00
|
|
|
hir::Defaultness::Default { has_value: true } => AssociatedContainer::ImplDefault,
|
2016-10-22 18:44:32 +05:30
|
|
|
hir::Defaultness::Final => AssociatedContainer::ImplFinal,
|
2016-11-14 11:00:02 -05:00
|
|
|
hir::Defaultness::Default { has_value: false } =>
|
|
|
|
span_bug!(ast_item.span, "impl items always have values (currently)"),
|
2016-09-16 17:25:54 +03:00
|
|
|
};
|
2016-09-08 19:05:50 +03:00
|
|
|
|
2016-11-10 02:06:34 +02:00
|
|
|
let kind = match impl_item.kind {
|
2017-02-20 03:55:28 +02:00
|
|
|
ty::AssociatedKind::Const => {
|
2018-04-15 19:41:33 -04:00
|
|
|
if let hir::ImplItemKind::Const(_, body_id) = ast_item.node {
|
|
|
|
let mir = self.tcx.at(ast_item.span).mir_const_qualif(def_id).0;
|
|
|
|
|
|
|
|
EntryKind::AssociatedConst(container,
|
|
|
|
self.const_qualif(mir, body_id),
|
|
|
|
self.encode_rendered_const_for_body(body_id))
|
|
|
|
} else {
|
|
|
|
bug!()
|
|
|
|
}
|
2017-02-20 03:55:28 +02:00
|
|
|
}
|
2016-11-10 02:06:34 +02:00
|
|
|
ty::AssociatedKind::Method => {
|
2016-12-20 22:46:11 +02:00
|
|
|
let fn_data = if let hir::ImplItemKind::Method(ref sig, body) = ast_item.node {
|
2016-09-16 17:25:54 +03:00
|
|
|
FnData {
|
2018-05-16 22:55:18 -07:00
|
|
|
constness: sig.header.constness,
|
2016-12-20 22:46:11 +02:00
|
|
|
arg_names: self.encode_fn_arg_names_for_body(body),
|
2017-05-13 17:11:52 +03:00
|
|
|
sig: self.lazy(&tcx.fn_sig(def_id)),
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bug!()
|
|
|
|
};
|
2016-11-10 02:06:34 +02:00
|
|
|
EntryKind::Method(self.lazy(&MethodData {
|
2017-08-06 22:54:09 -07:00
|
|
|
fn_data,
|
|
|
|
container,
|
2016-11-10 02:06:34 +02:00
|
|
|
has_self: impl_item.method_has_self_argument,
|
|
|
|
}))
|
2016-08-14 18:27:57 -04:00
|
|
|
}
|
2018-07-03 19:38:14 +02:00
|
|
|
ty::AssociatedKind::Existential => EntryKind::AssociatedExistential(container),
|
2016-11-10 02:06:34 +02:00
|
|
|
ty::AssociatedKind::Type => EntryKind::AssociatedType(container)
|
2016-09-08 19:05:50 +03:00
|
|
|
};
|
2016-08-10 15:33:36 -04:00
|
|
|
|
2018-04-15 19:41:33 -04:00
|
|
|
let mir =
|
2018-04-20 19:05:54 -04:00
|
|
|
match ast_item.node {
|
|
|
|
hir::ImplItemKind::Const(..) => true,
|
|
|
|
hir::ImplItemKind::Method(ref sig, _) => {
|
|
|
|
let generics = self.tcx.generics_of(def_id);
|
2018-04-12 17:51:08 +01:00
|
|
|
let needs_inline = (generics.requires_monomorphization(self.tcx) ||
|
2018-05-08 16:10:16 +03:00
|
|
|
tcx.codegen_fn_attrs(def_id).requests_inline()) &&
|
2018-02-24 18:29:40 +00:00
|
|
|
!self.metadata_output_only();
|
2018-05-16 22:55:18 -07:00
|
|
|
let is_const_fn = sig.header.constness == hir::Constness::Const;
|
2018-04-20 19:05:54 -04:00
|
|
|
let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
|
|
|
|
needs_inline || is_const_fn || always_encode_mir
|
|
|
|
},
|
2018-07-03 19:38:14 +02:00
|
|
|
hir::ImplItemKind::Existential(..) |
|
2018-04-20 19:05:54 -04:00
|
|
|
hir::ImplItemKind::Type(..) => false,
|
2018-04-15 19:41:33 -04:00
|
|
|
};
|
2016-09-19 23:49:01 +03:00
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
Entry {
|
2017-08-06 22:54:09 -07:00
|
|
|
kind,
|
2016-12-20 08:32:15 +00:00
|
|
|
visibility: self.lazy(&impl_item.vis),
|
2016-11-24 01:39:13 +02:00
|
|
|
span: self.lazy(&ast_item.span),
|
2016-09-16 17:25:54 +03:00
|
|
|
attributes: self.encode_attributes(&ast_item.attrs),
|
|
|
|
children: LazySeq::empty(),
|
|
|
|
stability: self.encode_stability(def_id),
|
|
|
|
deprecation: self.encode_deprecation(def_id),
|
|
|
|
|
2016-11-10 02:06:34 +02:00
|
|
|
ty: Some(self.encode_item_type(def_id)),
|
2016-09-16 17:25:54 +03:00
|
|
|
inherent_impls: LazySeq::empty(),
|
2017-06-02 22:05:41 +03:00
|
|
|
variances: if impl_item.kind == ty::AssociatedKind::Method {
|
|
|
|
self.encode_variances_of(def_id)
|
|
|
|
} else {
|
|
|
|
LazySeq::empty()
|
|
|
|
},
|
2016-09-16 17:25:54 +03:00
|
|
|
generics: Some(self.encode_generics(def_id)),
|
|
|
|
predicates: Some(self.encode_predicates(def_id)),
|
2018-07-02 10:35:30 -04:00
|
|
|
predicates_defined_on: None,
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2017-05-02 06:08:13 -04:00
|
|
|
mir: if mir { self.encode_optimized_mir(def_id) } else { None },
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
2012-03-19 10:19:00 -07:00
|
|
|
}
|
|
|
|
|
2016-12-20 22:46:11 +02:00
|
|
|
fn encode_fn_arg_names_for_body(&mut self, body_id: hir::BodyId)
|
|
|
|
-> LazySeq<ast::Name> {
|
2017-12-28 06:05:45 +01:00
|
|
|
self.tcx.dep_graph.with_ignore(|| {
|
|
|
|
let body = self.tcx.hir.body(body_id);
|
|
|
|
self.lazy_seq(body.arguments.iter().map(|arg| {
|
|
|
|
match arg.pat.node {
|
2018-06-10 19:33:30 +03:00
|
|
|
PatKind::Binding(_, _, ident, _) => ident.name,
|
|
|
|
_ => keywords::Invalid.name(),
|
2017-12-28 06:05:45 +01:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
})
|
2014-09-29 21:52:06 -07:00
|
|
|
}
|
|
|
|
|
2018-06-10 19:33:30 +03:00
|
|
|
fn encode_fn_arg_names(&mut self, param_names: &[ast::Ident]) -> LazySeq<ast::Name> {
|
|
|
|
self.lazy_seq(param_names.iter().map(|ident| ident.name))
|
2016-12-20 22:46:11 +02:00
|
|
|
}
|
|
|
|
|
2017-05-02 06:08:13 -04:00
|
|
|
fn encode_optimized_mir(&mut self, def_id: DefId) -> Option<Lazy<mir::Mir<'tcx>>> {
|
2017-04-05 23:39:02 +02:00
|
|
|
debug!("EntryBuilder::encode_mir({:?})", def_id);
|
2017-04-25 15:56:02 -04:00
|
|
|
if self.tcx.mir_keys(LOCAL_CRATE).contains(&def_id) {
|
2017-05-02 06:08:13 -04:00
|
|
|
let mir = self.tcx.optimized_mir(def_id);
|
2017-04-25 15:56:02 -04:00
|
|
|
Some(self.lazy(&mir))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2016-08-10 15:33:36 -04:00
|
|
|
}
|
2014-04-21 23:25:18 -07:00
|
|
|
|
2016-09-15 11:04:00 +03:00
|
|
|
// Encodes the inherent implementations of a structure, enumeration, or trait.
|
2016-09-16 17:25:54 +03:00
|
|
|
fn encode_inherent_implementations(&mut self, def_id: DefId) -> LazySeq<DefIndex> {
|
2017-04-27 16:12:57 +02:00
|
|
|
debug!("IsolatedEncoder::encode_inherent_implementations({:?})", def_id);
|
2017-04-18 10:53:35 -04:00
|
|
|
let implementations = self.tcx.inherent_impls(def_id);
|
|
|
|
if implementations.is_empty() {
|
|
|
|
LazySeq::empty()
|
|
|
|
} else {
|
|
|
|
self.lazy_seq(implementations.iter().map(|&def_id| {
|
|
|
|
assert!(def_id.is_local());
|
|
|
|
def_id.index
|
|
|
|
}))
|
2016-09-15 11:04:00 +03:00
|
|
|
}
|
2013-08-23 14:34:00 -07:00
|
|
|
}
|
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
fn encode_stability(&mut self, def_id: DefId) -> Option<Lazy<attr::Stability>> {
|
2017-04-27 16:12:57 +02:00
|
|
|
debug!("IsolatedEncoder::encode_stability({:?})", def_id);
|
2016-09-16 17:25:54 +03:00
|
|
|
self.tcx.lookup_stability(def_id).map(|stab| self.lazy(stab))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn encode_deprecation(&mut self, def_id: DefId) -> Option<Lazy<attr::Deprecation>> {
|
2017-04-27 16:12:57 +02:00
|
|
|
debug!("IsolatedEncoder::encode_deprecation({:?})", def_id);
|
2016-09-16 17:25:54 +03:00
|
|
|
self.tcx.lookup_deprecation(def_id).map(|depr| self.lazy(&depr))
|
2016-09-15 11:04:00 +03:00
|
|
|
}
|
2015-12-04 19:34:28 +03:00
|
|
|
|
2018-04-15 19:41:33 -04:00
|
|
|
fn encode_rendered_const_for_body(&mut self, body_id: hir::BodyId) -> Lazy<RenderedConst> {
|
|
|
|
let body = self.tcx.hir.body(body_id);
|
|
|
|
let rendered = hir::print::to_string(&self.tcx.hir, |s| s.print_expr(&body.value));
|
|
|
|
let rendered_const = &RenderedConst(rendered);
|
|
|
|
self.lazy(rendered_const)
|
|
|
|
}
|
|
|
|
|
2016-10-29 14:39:24 +02:00
|
|
|
fn encode_info_for_item(&mut self, (def_id, item): (DefId, &'tcx hir::Item)) -> Entry<'tcx> {
|
2016-08-29 08:55:40 +03:00
|
|
|
let tcx = self.tcx;
|
2013-05-01 17:54:54 -07:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
debug!("IsolatedEncoder::encode_info_for_item({:?})", def_id);
|
2016-04-06 14:49:50 -04:00
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
let kind = match item.node {
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Static(_, hir::MutMutable, _) => EntryKind::MutStatic,
|
|
|
|
hir::ItemKind::Static(_, hir::MutImmutable, _) => EntryKind::ImmStatic,
|
|
|
|
hir::ItemKind::Const(_, body_id) => {
|
2018-04-15 19:41:33 -04:00
|
|
|
let mir = tcx.at(item.span).mir_const_qualif(def_id).0;
|
|
|
|
EntryKind::Const(
|
|
|
|
self.const_qualif(mir, body_id),
|
|
|
|
self.encode_rendered_const_for_body(body_id)
|
|
|
|
)
|
2017-02-20 03:55:28 +02:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Fn(_, header, .., body) => {
|
2016-09-16 17:25:54 +03:00
|
|
|
let data = FnData {
|
2018-05-16 22:55:18 -07:00
|
|
|
constness: header.constness,
|
2016-12-20 22:46:11 +02:00
|
|
|
arg_names: self.encode_fn_arg_names_for_body(body),
|
2017-05-13 17:11:52 +03:00
|
|
|
sig: self.lazy(&tcx.fn_sig(def_id)),
|
2016-09-16 17:25:54 +03:00
|
|
|
};
|
2016-09-08 19:05:50 +03:00
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
EntryKind::Fn(self.lazy(&data))
|
2016-08-10 15:33:36 -04:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Mod(ref m) => {
|
2016-09-16 17:25:54 +03:00
|
|
|
return self.encode_info_for_mod(FromId(item.id, (m, &item.attrs, &item.vis)));
|
2014-08-04 13:56:56 -07:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::ForeignMod(_) => EntryKind::ForeignMod,
|
|
|
|
hir::ItemKind::GlobalAsm(..) => EntryKind::GlobalAsm,
|
|
|
|
hir::ItemKind::Ty(..) => EntryKind::Type,
|
|
|
|
hir::ItemKind::Existential(..) => EntryKind::Existential,
|
|
|
|
hir::ItemKind::Enum(..) => EntryKind::Enum(get_repr_options(&tcx, def_id)),
|
|
|
|
hir::ItemKind::Struct(ref struct_def, _) => {
|
2018-01-07 22:41:41 +01:00
|
|
|
let variant = tcx.adt_def(def_id).non_enum_variant();
|
2016-08-12 14:54:46 -04:00
|
|
|
|
2016-10-22 18:44:32 +05:30
|
|
|
// Encode def_ids for each field and method
|
|
|
|
// for methods, write all the stuff get_trait_method
|
|
|
|
// needs to know
|
2016-09-08 19:05:50 +03:00
|
|
|
let struct_ctor = if !struct_def.is_struct() {
|
2017-01-26 02:41:06 +02:00
|
|
|
Some(tcx.hir.local_def_id(struct_def.id()).index)
|
2016-09-08 19:05:50 +03:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2017-02-06 15:26:32 -05:00
|
|
|
|
|
|
|
let repr_options = get_repr_options(&tcx, def_id);
|
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
EntryKind::Struct(self.lazy(&VariantData {
|
2016-09-15 00:51:46 +03:00
|
|
|
ctor_kind: variant.ctor_kind,
|
2017-02-05 07:01:48 +02:00
|
|
|
discr: variant.discr,
|
2017-08-06 22:54:09 -07:00
|
|
|
struct_ctor,
|
2017-05-13 17:11:52 +03:00
|
|
|
ctor_sig: None,
|
2017-02-06 15:26:32 -05:00
|
|
|
}), repr_options)
|
2014-08-05 19:44:21 -07:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Union(..) => {
|
2018-01-07 22:41:41 +01:00
|
|
|
let variant = tcx.adt_def(def_id).non_enum_variant();
|
2017-02-06 15:26:32 -05:00
|
|
|
let repr_options = get_repr_options(&tcx, def_id);
|
2016-08-22 15:17:05 +03:00
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
EntryKind::Union(self.lazy(&VariantData {
|
2016-09-15 00:51:46 +03:00
|
|
|
ctor_kind: variant.ctor_kind,
|
2017-02-05 07:01:48 +02:00
|
|
|
discr: variant.discr,
|
2016-10-22 18:44:32 +05:30
|
|
|
struct_ctor: None,
|
2017-05-13 17:11:52 +03:00
|
|
|
ctor_sig: None,
|
2017-02-06 15:26:32 -05:00
|
|
|
}), repr_options)
|
2016-08-22 15:17:05 +03:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Impl(_, polarity, defaultness, ..) => {
|
2016-09-08 19:05:50 +03:00
|
|
|
let trait_ref = tcx.impl_trait_ref(def_id);
|
|
|
|
let parent = if let Some(trait_ref) = trait_ref {
|
2017-04-24 15:20:46 +03:00
|
|
|
let trait_def = tcx.trait_def(trait_ref.def_id);
|
2017-05-11 15:47:15 +02:00
|
|
|
trait_def.ancestors(tcx, def_id).skip(1).next().and_then(|node| {
|
2016-09-08 19:05:50 +03:00
|
|
|
match node {
|
|
|
|
specialization_graph::Node::Impl(parent) => Some(parent),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2016-08-10 15:33:36 -04:00
|
|
|
|
2017-03-17 16:17:45 -04:00
|
|
|
// if this is an impl of `CoerceUnsized`, create its
|
|
|
|
// "unsized info", else just store None
|
|
|
|
let coerce_unsized_info =
|
|
|
|
trait_ref.and_then(|t| {
|
2017-08-31 08:57:41 -07:00
|
|
|
if Some(t.def_id) == tcx.lang_items().coerce_unsized_trait() {
|
2017-04-24 18:06:39 +03:00
|
|
|
Some(tcx.at(item.span).coerce_unsized_info(def_id))
|
2017-03-17 16:17:45 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
let data = ImplData {
|
2017-08-06 22:54:09 -07:00
|
|
|
polarity,
|
|
|
|
defaultness,
|
2016-09-08 19:05:50 +03:00
|
|
|
parent_impl: parent,
|
2017-08-06 22:54:09 -07:00
|
|
|
coerce_unsized_info,
|
2016-10-22 18:44:32 +05:30
|
|
|
trait_ref: trait_ref.map(|trait_ref| self.lazy(&trait_ref)),
|
2016-09-16 17:25:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
EntryKind::Impl(self.lazy(&data))
|
2016-08-10 15:33:36 -04:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Trait(..) => {
|
2017-04-24 15:20:46 +03:00
|
|
|
let trait_def = tcx.trait_def(def_id);
|
2016-09-16 17:25:54 +03:00
|
|
|
let data = TraitData {
|
2016-09-08 19:05:50 +03:00
|
|
|
unsafety: trait_def.unsafety,
|
|
|
|
paren_sugar: trait_def.paren_sugar,
|
2017-10-16 17:33:45 -02:00
|
|
|
has_auto_impl: tcx.trait_is_auto(def_id),
|
2017-04-24 15:20:46 +03:00
|
|
|
super_predicates: self.lazy(&tcx.super_predicates_of(def_id)),
|
2016-09-16 17:25:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
EntryKind::Trait(self.lazy(&data))
|
2016-08-12 14:18:59 -04:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::ExternCrate(_) |
|
|
|
|
hir::ItemKind::TraitAlias(..) |
|
|
|
|
hir::ItemKind::Use(..) => bug!("cannot encode info for item {:?}", item),
|
2016-09-08 19:05:50 +03:00
|
|
|
};
|
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
Entry {
|
2017-08-06 22:54:09 -07:00
|
|
|
kind,
|
2016-12-20 08:32:15 +00:00
|
|
|
visibility: self.lazy(&ty::Visibility::from_hir(&item.vis, item.id, tcx)),
|
2016-11-24 01:39:13 +02:00
|
|
|
span: self.lazy(&item.span),
|
2016-09-16 17:25:54 +03:00
|
|
|
attributes: self.encode_attributes(&item.attrs),
|
|
|
|
children: match item.node {
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::ForeignMod(ref fm) => {
|
2016-10-22 18:44:32 +05:30
|
|
|
self.lazy_seq(fm.items
|
|
|
|
.iter()
|
2017-01-26 02:41:06 +02:00
|
|
|
.map(|foreign_item| tcx.hir.local_def_id(foreign_item.id).index))
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Enum(..) => {
|
2017-04-24 15:20:46 +03:00
|
|
|
let def = self.tcx.adt_def(def_id);
|
2016-09-16 17:25:54 +03:00
|
|
|
self.lazy_seq(def.variants.iter().map(|v| {
|
|
|
|
assert!(v.did.is_local());
|
|
|
|
v.did.index
|
|
|
|
}))
|
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Struct(..) |
|
|
|
|
hir::ItemKind::Union(..) => {
|
2017-04-24 15:20:46 +03:00
|
|
|
let def = self.tcx.adt_def(def_id);
|
2018-01-07 22:41:41 +01:00
|
|
|
self.lazy_seq(def.non_enum_variant().fields.iter().map(|f| {
|
2016-09-16 17:25:54 +03:00
|
|
|
assert!(f.did.is_local());
|
|
|
|
f.did.index
|
|
|
|
}))
|
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Impl(..) |
|
|
|
|
hir::ItemKind::Trait(..) => {
|
2016-11-10 02:06:34 +02:00
|
|
|
self.lazy_seq(tcx.associated_item_def_ids(def_id).iter().map(|&def_id| {
|
2016-09-16 17:25:54 +03:00
|
|
|
assert!(def_id.is_local());
|
|
|
|
def_id.index
|
|
|
|
}))
|
|
|
|
}
|
2016-10-22 18:44:32 +05:30
|
|
|
_ => LazySeq::empty(),
|
2016-09-16 17:25:54 +03:00
|
|
|
},
|
|
|
|
stability: self.encode_stability(def_id),
|
|
|
|
deprecation: self.encode_deprecation(def_id),
|
|
|
|
|
|
|
|
ty: match item.node {
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Static(..) |
|
|
|
|
hir::ItemKind::Const(..) |
|
|
|
|
hir::ItemKind::Fn(..) |
|
|
|
|
hir::ItemKind::Ty(..) |
|
|
|
|
hir::ItemKind::Existential(..) |
|
|
|
|
hir::ItemKind::Enum(..) |
|
|
|
|
hir::ItemKind::Struct(..) |
|
|
|
|
hir::ItemKind::Union(..) |
|
|
|
|
hir::ItemKind::Impl(..) => Some(self.encode_item_type(def_id)),
|
2016-10-22 18:44:32 +05:30
|
|
|
_ => None,
|
2016-09-16 17:25:54 +03:00
|
|
|
},
|
|
|
|
inherent_impls: self.encode_inherent_implementations(def_id),
|
|
|
|
variances: match item.node {
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Enum(..) |
|
|
|
|
hir::ItemKind::Struct(..) |
|
|
|
|
hir::ItemKind::Union(..) |
|
|
|
|
hir::ItemKind::Fn(..) => self.encode_variances_of(def_id),
|
2016-10-22 18:44:32 +05:30
|
|
|
_ => LazySeq::empty(),
|
2016-09-16 17:25:54 +03:00
|
|
|
},
|
|
|
|
generics: match item.node {
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Static(..) |
|
|
|
|
hir::ItemKind::Const(..) |
|
|
|
|
hir::ItemKind::Fn(..) |
|
|
|
|
hir::ItemKind::Ty(..) |
|
|
|
|
hir::ItemKind::Enum(..) |
|
|
|
|
hir::ItemKind::Struct(..) |
|
|
|
|
hir::ItemKind::Union(..) |
|
|
|
|
hir::ItemKind::Impl(..) |
|
|
|
|
hir::ItemKind::Existential(..) |
|
|
|
|
hir::ItemKind::Trait(..) => Some(self.encode_generics(def_id)),
|
2016-10-22 18:44:32 +05:30
|
|
|
_ => None,
|
2016-09-16 17:25:54 +03:00
|
|
|
},
|
|
|
|
predicates: match item.node {
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Static(..) |
|
|
|
|
hir::ItemKind::Const(..) |
|
|
|
|
hir::ItemKind::Fn(..) |
|
|
|
|
hir::ItemKind::Ty(..) |
|
|
|
|
hir::ItemKind::Enum(..) |
|
|
|
|
hir::ItemKind::Struct(..) |
|
|
|
|
hir::ItemKind::Union(..) |
|
|
|
|
hir::ItemKind::Impl(..) |
|
|
|
|
hir::ItemKind::Existential(..) |
|
|
|
|
hir::ItemKind::Trait(..) => Some(self.encode_predicates(def_id)),
|
2016-10-22 18:44:32 +05:30
|
|
|
_ => None,
|
2016-09-16 17:25:54 +03:00
|
|
|
},
|
2016-09-08 19:05:50 +03:00
|
|
|
|
2018-07-02 10:35:30 -04:00
|
|
|
// The only time that `predicates_defined_on` is used (on
|
|
|
|
// an external item) is for traits, during chalk lowering,
|
|
|
|
// so only encode it in that case as an efficiency
|
|
|
|
// hack. (No reason not to expand it in the future if
|
|
|
|
// necessary.)
|
|
|
|
predicates_defined_on: match item.node {
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Trait(..) => Some(self.encode_predicates_defined_on(def_id)),
|
2018-07-02 10:35:30 -04:00
|
|
|
_ => None, // not *wrong* for other kinds of items, but not needed
|
|
|
|
},
|
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
mir: match item.node {
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Static(..) => {
|
2017-05-02 06:08:13 -04:00
|
|
|
self.encode_optimized_mir(def_id)
|
2016-12-21 12:32:59 +02:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Const(..) => self.encode_optimized_mir(def_id),
|
|
|
|
hir::ItemKind::Fn(_, header, ..) => {
|
2018-06-15 11:45:12 +01:00
|
|
|
let generics = tcx.generics_of(def_id);
|
2018-05-27 01:43:03 +01:00
|
|
|
let has_types = generics.params.iter().any(|param| match param.kind {
|
2018-06-15 11:45:12 +01:00
|
|
|
ty::GenericParamDefKind::Type { .. } => true,
|
2018-05-27 01:43:03 +01:00
|
|
|
_ => false,
|
|
|
|
});
|
2018-05-08 16:10:16 +03:00
|
|
|
let needs_inline =
|
2018-05-26 13:11:39 +01:00
|
|
|
(has_types || tcx.codegen_fn_attrs(def_id).requests_inline()) &&
|
2018-05-08 16:10:16 +03:00
|
|
|
!self.metadata_output_only();
|
2016-12-07 13:22:21 +01:00
|
|
|
let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
|
2018-06-06 15:50:59 -07:00
|
|
|
if needs_inline
|
|
|
|
|| header.constness == hir::Constness::Const
|
|
|
|
|| always_encode_mir
|
|
|
|
{
|
2017-05-02 06:08:13 -04:00
|
|
|
self.encode_optimized_mir(def_id)
|
2016-09-16 17:25:54 +03:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2016-10-22 18:44:32 +05:30
|
|
|
_ => None,
|
|
|
|
},
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
2016-08-12 14:18:59 -04:00
|
|
|
}
|
2016-10-28 06:52:45 +00:00
|
|
|
|
|
|
|
/// Serialize the text of exported macros
|
|
|
|
fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef) -> Entry<'tcx> {
|
2017-02-21 05:05:59 +00:00
|
|
|
use syntax::print::pprust;
|
2017-03-17 23:41:09 +00:00
|
|
|
let def_id = self.tcx.hir.local_def_id(macro_def.id);
|
2016-10-28 06:52:45 +00:00
|
|
|
Entry {
|
2016-11-24 01:39:13 +02:00
|
|
|
kind: EntryKind::MacroDef(self.lazy(&MacroDef {
|
2017-02-21 05:05:59 +00:00
|
|
|
body: pprust::tts_to_string(¯o_def.body.trees().collect::<Vec<_>>()),
|
2017-03-22 08:39:51 +00:00
|
|
|
legacy: macro_def.legacy,
|
2016-11-24 01:39:13 +02:00
|
|
|
})),
|
2016-12-20 08:32:15 +00:00
|
|
|
visibility: self.lazy(&ty::Visibility::Public),
|
2016-11-24 01:39:13 +02:00
|
|
|
span: self.lazy(¯o_def.span),
|
|
|
|
attributes: self.encode_attributes(¯o_def.attrs),
|
2017-03-17 23:41:09 +00:00
|
|
|
stability: self.encode_stability(def_id),
|
|
|
|
deprecation: self.encode_deprecation(def_id),
|
|
|
|
|
2016-10-28 06:52:45 +00:00
|
|
|
children: LazySeq::empty(),
|
|
|
|
ty: None,
|
|
|
|
inherent_impls: LazySeq::empty(),
|
|
|
|
variances: LazySeq::empty(),
|
|
|
|
generics: None,
|
|
|
|
predicates: None,
|
2018-07-02 10:35:30 -04:00
|
|
|
predicates_defined_on: None,
|
2016-10-28 06:52:45 +00:00
|
|
|
mir: None,
|
|
|
|
}
|
|
|
|
}
|
2013-03-27 10:26:57 -04:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
fn encode_info_for_ty_param(&mut self,
|
|
|
|
(def_id, Untracked(has_default)): (DefId, Untracked<bool>))
|
|
|
|
-> Entry<'tcx> {
|
|
|
|
debug!("IsolatedEncoder::encode_info_for_ty_param({:?})", def_id);
|
|
|
|
let tcx = self.tcx;
|
|
|
|
Entry {
|
|
|
|
kind: EntryKind::Type,
|
|
|
|
visibility: self.lazy(&ty::Visibility::Public),
|
|
|
|
span: self.lazy(&tcx.def_span(def_id)),
|
|
|
|
attributes: LazySeq::empty(),
|
|
|
|
children: LazySeq::empty(),
|
|
|
|
stability: None,
|
|
|
|
deprecation: None,
|
2016-09-08 19:05:50 +03:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
ty: if has_default {
|
|
|
|
Some(self.encode_item_type(def_id))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
inherent_impls: LazySeq::empty(),
|
|
|
|
variances: LazySeq::empty(),
|
|
|
|
generics: None,
|
|
|
|
predicates: None,
|
2018-07-02 10:35:30 -04:00
|
|
|
predicates_defined_on: None,
|
2016-09-08 19:05:50 +03:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
mir: None,
|
2017-01-25 22:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
fn encode_info_for_closure(&mut self, def_id: DefId) -> Entry<'tcx> {
|
2017-04-27 16:12:57 +02:00
|
|
|
debug!("IsolatedEncoder::encode_info_for_closure({:?})", def_id);
|
2016-08-29 08:55:40 +03:00
|
|
|
let tcx = self.tcx;
|
2016-08-14 18:50:53 -04:00
|
|
|
|
2017-11-09 15:44:29 -05:00
|
|
|
let tables = self.tcx.typeck_tables_of(def_id);
|
|
|
|
let node_id = self.tcx.hir.as_local_node_id(def_id).unwrap();
|
|
|
|
let hir_id = self.tcx.hir.node_to_hir_id(node_id);
|
|
|
|
let kind = match tables.node_id_to_type(hir_id).sty {
|
|
|
|
ty::TyGenerator(def_id, ..) => {
|
|
|
|
let layout = self.tcx.generator_layout(def_id);
|
|
|
|
let data = GeneratorData {
|
|
|
|
layout: layout.clone(),
|
|
|
|
};
|
|
|
|
EntryKind::Generator(self.lazy(&data))
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::TyClosure(def_id, substs) => {
|
|
|
|
let sig = substs.closure_sig(def_id, self.tcx);
|
|
|
|
let data = ClosureData { sig: self.lazy(&sig) };
|
|
|
|
EntryKind::Closure(self.lazy(&data))
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => bug!("closure that is neither generator nor closure")
|
2016-08-29 08:55:40 +03:00
|
|
|
};
|
2013-08-12 14:05:43 +02:00
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
Entry {
|
2016-12-26 14:34:03 +01:00
|
|
|
kind,
|
2016-12-20 08:32:15 +00:00
|
|
|
visibility: self.lazy(&ty::Visibility::Public),
|
2016-11-24 01:39:13 +02:00
|
|
|
span: self.lazy(&tcx.def_span(def_id)),
|
2016-09-16 17:25:54 +03:00
|
|
|
attributes: self.encode_attributes(&tcx.get_attrs(def_id)),
|
|
|
|
children: LazySeq::empty(),
|
|
|
|
stability: None,
|
|
|
|
deprecation: None,
|
|
|
|
|
2016-11-09 20:41:03 +02:00
|
|
|
ty: Some(self.encode_item_type(def_id)),
|
2016-09-16 17:25:54 +03:00
|
|
|
inherent_impls: LazySeq::empty(),
|
|
|
|
variances: LazySeq::empty(),
|
2016-11-03 22:19:33 +02:00
|
|
|
generics: Some(self.encode_generics(def_id)),
|
2016-09-16 17:25:54 +03:00
|
|
|
predicates: None,
|
2018-07-02 10:35:30 -04:00
|
|
|
predicates_defined_on: None,
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2017-05-02 06:08:13 -04:00
|
|
|
mir: self.encode_optimized_mir(def_id),
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
2016-09-15 11:04:00 +03:00
|
|
|
}
|
2011-06-27 15:20:17 -07:00
|
|
|
|
2018-05-17 21:28:50 +03:00
|
|
|
fn encode_info_for_anon_const(&mut self, def_id: DefId) -> Entry<'tcx> {
|
|
|
|
debug!("IsolatedEncoder::encode_info_for_anon_const({:?})", def_id);
|
2017-04-15 04:14:44 +03:00
|
|
|
let tcx = self.tcx;
|
|
|
|
let id = tcx.hir.as_local_node_id(def_id).unwrap();
|
2018-04-15 19:41:33 -04:00
|
|
|
let body_id = tcx.hir.body_owned_by(id);
|
|
|
|
let const_data = self.encode_rendered_const_for_body(body_id);
|
|
|
|
let mir = tcx.mir_const_qualif(def_id).0;
|
2017-04-15 04:14:44 +03:00
|
|
|
|
|
|
|
Entry {
|
2018-04-15 19:41:33 -04:00
|
|
|
kind: EntryKind::Const(self.const_qualif(mir, body_id), const_data),
|
2017-04-15 04:14:44 +03:00
|
|
|
visibility: self.lazy(&ty::Visibility::Public),
|
|
|
|
span: self.lazy(&tcx.def_span(def_id)),
|
|
|
|
attributes: LazySeq::empty(),
|
|
|
|
children: LazySeq::empty(),
|
|
|
|
stability: None,
|
|
|
|
deprecation: None,
|
|
|
|
|
|
|
|
ty: Some(self.encode_item_type(def_id)),
|
|
|
|
inherent_impls: LazySeq::empty(),
|
|
|
|
variances: LazySeq::empty(),
|
|
|
|
generics: Some(self.encode_generics(def_id)),
|
|
|
|
predicates: Some(self.encode_predicates(def_id)),
|
2018-07-02 10:35:30 -04:00
|
|
|
predicates_defined_on: None,
|
2017-04-15 04:14:44 +03:00
|
|
|
|
2017-05-02 06:08:13 -04:00
|
|
|
mir: self.encode_optimized_mir(def_id),
|
2017-04-15 04:14:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-05 13:00:17 +02:00
|
|
|
fn encode_attributes(&mut self, attrs: &[ast::Attribute]) -> LazySeq<ast::Attribute> {
|
2017-04-05 23:39:02 +02:00
|
|
|
// NOTE: This must use lazy_seq_from_slice(), not lazy_seq() because
|
2017-04-27 16:12:57 +02:00
|
|
|
// we rely on the HashStable specialization for [Attribute]
|
2017-04-05 23:39:02 +02:00
|
|
|
// to properly filter things out.
|
2017-04-05 13:00:17 +02:00
|
|
|
self.lazy_seq_from_slice(attrs)
|
|
|
|
}
|
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
fn encode_native_libraries(&mut self, _: ()) -> LazySeq<NativeLibrary> {
|
2017-08-30 14:48:57 -07:00
|
|
|
let used_libraries = self.tcx.native_libraries(LOCAL_CRATE);
|
|
|
|
self.lazy_seq(used_libraries.iter().cloned())
|
2016-09-15 11:04:00 +03:00
|
|
|
}
|
2011-06-27 15:20:17 -07:00
|
|
|
|
2018-02-10 14:28:17 -08:00
|
|
|
fn encode_foreign_modules(&mut self, _: ()) -> LazySeq<ForeignModule> {
|
|
|
|
let foreign_modules = self.tcx.foreign_modules(LOCAL_CRATE);
|
|
|
|
self.lazy_seq(foreign_modules.iter().cloned())
|
|
|
|
}
|
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
fn encode_crate_deps(&mut self, _: ()) -> LazySeq<CrateDep> {
|
2017-09-07 08:13:41 -07:00
|
|
|
let crates = self.tcx.crates();
|
2017-04-27 16:12:57 +02:00
|
|
|
|
|
|
|
let mut deps = crates
|
|
|
|
.iter()
|
|
|
|
.map(|&cnum| {
|
|
|
|
let dep = CrateDep {
|
2017-08-28 17:30:27 -07:00
|
|
|
name: self.tcx.original_crate_name(cnum),
|
2017-08-28 17:30:27 -07:00
|
|
|
hash: self.tcx.crate_hash(cnum),
|
2017-08-31 08:07:39 -07:00
|
|
|
kind: self.tcx.dep_kind(cnum),
|
2018-03-13 11:58:53 -07:00
|
|
|
extra_filename: self.tcx.extra_filename(cnum),
|
2017-04-27 16:12:57 +02:00
|
|
|
};
|
|
|
|
(cnum, dep)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2011-07-08 11:29:56 -07:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
deps.sort_by_key(|&(cnum, _)| cnum);
|
2011-07-08 11:29:56 -07:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
{
|
2016-09-15 11:04:00 +03:00
|
|
|
// Sanity-check the crate numbers
|
|
|
|
let mut expected_cnum = 1;
|
|
|
|
for &(n, _) in &deps {
|
|
|
|
assert_eq!(n, CrateNum::new(expected_cnum));
|
|
|
|
expected_cnum += 1;
|
|
|
|
}
|
2011-07-08 11:29:56 -07:00
|
|
|
}
|
|
|
|
|
2016-09-15 11:04:00 +03:00
|
|
|
// We're just going to write a list of crate 'name-hash-version's, with
|
|
|
|
// the assumption that they are numbered 1 to n.
|
|
|
|
// FIXME (#2166): This is not nearly enough to support correct versioning
|
|
|
|
// but is enough to get transitive crate dependencies working.
|
2017-04-27 16:12:57 +02:00
|
|
|
self.lazy_seq_ref(deps.iter().map(|&(_, ref dep)| dep))
|
2011-07-08 11:29:56 -07:00
|
|
|
}
|
|
|
|
|
2018-07-23 01:20:33 +01:00
|
|
|
fn encode_lib_features(&mut self, _: ()) -> LazySeq<(ast::Name, Option<ast::Name>)> {
|
|
|
|
let tcx = self.tcx;
|
|
|
|
let lib_features = tcx.lib_features();
|
2018-07-27 00:06:57 +01:00
|
|
|
self.lazy_seq(lib_features.to_vec())
|
2018-07-23 01:20:33 +01:00
|
|
|
}
|
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
fn encode_lang_items(&mut self, _: ()) -> LazySeq<(DefIndex, usize)> {
|
2016-09-15 11:04:00 +03:00
|
|
|
let tcx = self.tcx;
|
2017-08-31 08:57:41 -07:00
|
|
|
let lang_items = tcx.lang_items();
|
|
|
|
let lang_items = lang_items.items().iter();
|
2017-04-27 16:12:57 +02:00
|
|
|
self.lazy_seq(lang_items.enumerate().filter_map(|(i, &opt_def_id)| {
|
2016-09-16 17:25:54 +03:00
|
|
|
if let Some(def_id) = opt_def_id {
|
|
|
|
if def_id.is_local() {
|
|
|
|
return Some((def_id.index, i));
|
2016-09-19 23:49:01 +03:00
|
|
|
}
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
|
|
|
None
|
2017-04-27 16:12:57 +02:00
|
|
|
}))
|
2013-08-20 14:55:54 -07:00
|
|
|
}
|
2016-11-04 18:20:15 -04:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
fn encode_lang_items_missing(&mut self, _: ()) -> LazySeq<lang_items::LangItem> {
|
|
|
|
let tcx = self.tcx;
|
2017-08-31 08:57:41 -07:00
|
|
|
self.lazy_seq_ref(&tcx.lang_items().missing)
|
2016-11-04 18:20:15 -04:00
|
|
|
}
|
2013-08-20 14:55:54 -07:00
|
|
|
|
2016-09-15 11:04:00 +03:00
|
|
|
/// Encodes an index, mapping each trait to its (local) implementations.
|
2017-04-27 16:12:57 +02:00
|
|
|
fn encode_impls(&mut self, _: ()) -> LazySeq<TraitImpls> {
|
2017-05-11 15:47:15 +02:00
|
|
|
debug!("IsolatedEncoder::encode_impls()");
|
|
|
|
let tcx = self.tcx;
|
2016-09-15 11:04:00 +03:00
|
|
|
let mut visitor = ImplVisitor {
|
2017-08-06 22:54:09 -07:00
|
|
|
tcx,
|
2016-11-08 14:02:55 +11:00
|
|
|
impls: FxHashMap(),
|
2016-09-15 11:04:00 +03:00
|
|
|
};
|
2017-05-11 15:47:15 +02:00
|
|
|
tcx.hir.krate().visit_all_item_likes(&mut visitor);
|
|
|
|
|
|
|
|
let mut all_impls: Vec<_> = visitor.impls.into_iter().collect();
|
2016-09-15 11:04:00 +03:00
|
|
|
|
2017-05-11 15:47:15 +02:00
|
|
|
// Bring everything into deterministic order for hashing
|
2018-03-30 10:40:25 +01:00
|
|
|
all_impls.sort_by_cached_key(|&(trait_def_id, _)| {
|
2017-05-11 15:47:15 +02:00
|
|
|
tcx.def_path_hash(trait_def_id)
|
|
|
|
});
|
|
|
|
|
|
|
|
let all_impls: Vec<_> = all_impls
|
2016-10-22 18:44:32 +05:30
|
|
|
.into_iter()
|
2017-05-11 15:47:15 +02:00
|
|
|
.map(|(trait_def_id, mut impls)| {
|
|
|
|
// Bring everything into deterministic order for hashing
|
2018-03-30 10:40:25 +01:00
|
|
|
impls.sort_by_cached_key(|&def_index| {
|
2017-05-11 15:47:15 +02:00
|
|
|
tcx.hir.definitions().def_path_hash(def_index)
|
|
|
|
});
|
|
|
|
|
2016-10-22 18:44:32 +05:30
|
|
|
TraitImpls {
|
|
|
|
trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index),
|
2017-05-11 15:47:15 +02:00
|
|
|
impls: self.lazy_seq_from_slice(&impls[..]),
|
2016-10-22 18:44:32 +05:30
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2017-05-11 15:47:15 +02:00
|
|
|
self.lazy_seq_from_slice(&all_impls[..])
|
2013-08-20 14:55:54 -07:00
|
|
|
}
|
|
|
|
|
2016-11-28 18:05:53 -05:00
|
|
|
// Encodes all symbols exported from this crate into the metadata.
|
2016-09-15 11:04:00 +03:00
|
|
|
//
|
|
|
|
// This pass is seeded off the reachability list calculated in the
|
|
|
|
// middle::reachable module but filters out items that either don't have a
|
|
|
|
// symbol associated with them (they weren't translated) or if they're an FFI
|
|
|
|
// definition (as that's not defined in this crate).
|
2018-02-27 19:28:21 +01:00
|
|
|
fn encode_exported_symbols(&mut self,
|
|
|
|
exported_symbols: &[(ExportedSymbol, SymbolExportLevel)])
|
2018-03-01 16:50:08 +01:00
|
|
|
-> EncodedExportedSymbols {
|
2018-03-05 17:41:11 +01:00
|
|
|
// The metadata symbol name is special. It should not show up in
|
|
|
|
// downstream crates.
|
|
|
|
let metadata_symbol_name = SymbolName::new(&metadata_symbol_name(self.tcx));
|
|
|
|
|
2018-03-01 16:50:08 +01:00
|
|
|
let lazy_seq = self.lazy_seq(exported_symbols
|
2018-03-05 17:41:11 +01:00
|
|
|
.iter()
|
|
|
|
.filter(|&&(ref exported_symbol, _)| {
|
|
|
|
match *exported_symbol {
|
|
|
|
ExportedSymbol::NoDefId(symbol_name) => {
|
|
|
|
symbol_name != metadata_symbol_name
|
|
|
|
},
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
})
|
2018-03-01 16:50:08 +01:00
|
|
|
.cloned());
|
|
|
|
|
|
|
|
EncodedExportedSymbols {
|
|
|
|
len: lazy_seq.len,
|
|
|
|
position: lazy_seq.position,
|
|
|
|
}
|
2016-09-15 11:04:00 +03:00
|
|
|
}
|
2014-04-17 16:52:25 +01:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
fn encode_dylib_dependency_formats(&mut self, _: ()) -> LazySeq<Option<LinkagePreference>> {
|
2018-07-26 11:13:11 -06:00
|
|
|
match self.tcx.sess.dependency_formats.borrow().get(&config::CrateType::Dylib) {
|
2016-09-15 11:04:00 +03:00
|
|
|
Some(arr) => {
|
2016-09-16 17:25:54 +03:00
|
|
|
self.lazy_seq(arr.iter().map(|slot| {
|
2016-09-15 11:04:00 +03:00
|
|
|
match *slot {
|
|
|
|
Linkage::NotLinked |
|
|
|
|
Linkage::IncludedFromDylib => None,
|
|
|
|
|
|
|
|
Linkage::Dynamic => Some(LinkagePreference::RequireDynamic),
|
|
|
|
Linkage::Static => Some(LinkagePreference::RequireStatic),
|
|
|
|
}
|
2016-09-16 17:25:54 +03:00
|
|
|
}))
|
2016-09-15 11:04:00 +03:00
|
|
|
}
|
2016-10-22 18:44:32 +05:30
|
|
|
None => LazySeq::empty(),
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
fn encode_info_for_foreign_item(&mut self,
|
|
|
|
(def_id, nitem): (DefId, &hir::ForeignItem))
|
|
|
|
-> Entry<'tcx> {
|
|
|
|
let tcx = self.tcx;
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
debug!("IsolatedEncoder::encode_info_for_foreign_item({:?})", def_id);
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
let kind = match nitem.node {
|
2018-07-11 22:56:44 +08:00
|
|
|
hir::ForeignItemKind::Fn(_, ref names, _) => {
|
2017-04-27 16:12:57 +02:00
|
|
|
let data = FnData {
|
|
|
|
constness: hir::Constness::NotConst,
|
|
|
|
arg_names: self.encode_fn_arg_names(names),
|
2017-05-13 17:11:52 +03:00
|
|
|
sig: self.lazy(&tcx.fn_sig(def_id)),
|
2017-04-27 16:12:57 +02:00
|
|
|
};
|
|
|
|
EntryKind::ForeignFn(self.lazy(&data))
|
|
|
|
}
|
2018-07-11 22:56:44 +08:00
|
|
|
hir::ForeignItemKind::Static(_, true) => EntryKind::ForeignMutStatic,
|
|
|
|
hir::ForeignItemKind::Static(_, false) => EntryKind::ForeignImmStatic,
|
|
|
|
hir::ForeignItemKind::Type => EntryKind::ForeignType,
|
2017-04-27 16:12:57 +02:00
|
|
|
};
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
Entry {
|
2017-08-06 22:54:09 -07:00
|
|
|
kind,
|
2017-04-27 16:12:57 +02:00
|
|
|
visibility: self.lazy(&ty::Visibility::from_hir(&nitem.vis, nitem.id, tcx)),
|
|
|
|
span: self.lazy(&nitem.span),
|
|
|
|
attributes: self.encode_attributes(&nitem.attrs),
|
|
|
|
children: LazySeq::empty(),
|
|
|
|
stability: self.encode_stability(def_id),
|
|
|
|
deprecation: self.encode_deprecation(def_id),
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
ty: Some(self.encode_item_type(def_id)),
|
|
|
|
inherent_impls: LazySeq::empty(),
|
2017-06-02 22:05:41 +03:00
|
|
|
variances: match nitem.node {
|
2018-07-11 22:56:44 +08:00
|
|
|
hir::ForeignItemKind::Fn(..) => self.encode_variances_of(def_id),
|
2017-06-02 22:05:41 +03:00
|
|
|
_ => LazySeq::empty(),
|
|
|
|
},
|
2017-04-27 16:12:57 +02:00
|
|
|
generics: Some(self.encode_generics(def_id)),
|
|
|
|
predicates: Some(self.encode_predicates(def_id)),
|
2018-07-02 10:35:30 -04:00
|
|
|
predicates_defined_on: None,
|
2016-12-16 12:51:36 -05:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
mir: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
struct EncodeVisitor<'a, 'b: 'a, 'tcx: 'b> {
|
|
|
|
index: IndexBuilder<'a, 'b, 'tcx>,
|
|
|
|
}
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
impl<'a, 'b, 'tcx> Visitor<'tcx> for EncodeVisitor<'a, 'b, 'tcx> {
|
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
|
|
|
NestedVisitorMap::OnlyBodies(&self.index.tcx.hir)
|
|
|
|
}
|
|
|
|
fn visit_expr(&mut self, ex: &'tcx hir::Expr) {
|
|
|
|
intravisit::walk_expr(self, ex);
|
|
|
|
self.index.encode_info_for_expr(ex);
|
|
|
|
}
|
|
|
|
fn visit_item(&mut self, item: &'tcx hir::Item) {
|
|
|
|
intravisit::walk_item(self, item);
|
|
|
|
let def_id = self.index.tcx.hir.local_def_id(item.id);
|
|
|
|
match item.node {
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::ExternCrate(_) |
|
|
|
|
hir::ItemKind::Use(..) => (), // ignore these
|
2017-04-27 16:12:57 +02:00
|
|
|
_ => self.index.record(def_id, IsolatedEncoder::encode_info_for_item, (def_id, item)),
|
|
|
|
}
|
|
|
|
self.index.encode_addl_info_for_item(item);
|
|
|
|
}
|
|
|
|
fn visit_foreign_item(&mut self, ni: &'tcx hir::ForeignItem) {
|
|
|
|
intravisit::walk_foreign_item(self, ni);
|
|
|
|
let def_id = self.index.tcx.hir.local_def_id(ni.id);
|
|
|
|
self.index.record(def_id,
|
|
|
|
IsolatedEncoder::encode_info_for_foreign_item,
|
|
|
|
(def_id, ni));
|
|
|
|
}
|
|
|
|
fn visit_variant(&mut self,
|
|
|
|
v: &'tcx hir::Variant,
|
|
|
|
g: &'tcx hir::Generics,
|
|
|
|
id: ast::NodeId) {
|
|
|
|
intravisit::walk_variant(self, v, g, id);
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2018-05-17 21:28:50 +03:00
|
|
|
if let Some(ref discr) = v.node.disr_expr {
|
|
|
|
let def_id = self.index.tcx.hir.local_def_id(discr.id);
|
|
|
|
self.index.record(def_id, IsolatedEncoder::encode_info_for_anon_const, def_id);
|
2017-04-27 16:12:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn visit_generics(&mut self, generics: &'tcx hir::Generics) {
|
|
|
|
intravisit::walk_generics(self, generics);
|
|
|
|
self.index.encode_info_for_generics(generics);
|
|
|
|
}
|
|
|
|
fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
|
|
|
|
intravisit::walk_ty(self, ty);
|
|
|
|
self.index.encode_info_for_ty(ty);
|
|
|
|
}
|
|
|
|
fn visit_macro_def(&mut self, macro_def: &'tcx hir::MacroDef) {
|
|
|
|
let def_id = self.index.tcx.hir.local_def_id(macro_def.id);
|
|
|
|
self.index.record(def_id, IsolatedEncoder::encode_info_for_macro_def, macro_def);
|
|
|
|
}
|
|
|
|
}
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> {
|
|
|
|
fn encode_fields(&mut self, adt_def_id: DefId) {
|
|
|
|
let def = self.tcx.adt_def(adt_def_id);
|
|
|
|
for (variant_index, variant) in def.variants.iter().enumerate() {
|
|
|
|
for (field_index, field) in variant.fields.iter().enumerate() {
|
|
|
|
self.record(field.did,
|
|
|
|
IsolatedEncoder::encode_field,
|
|
|
|
(adt_def_id, Untracked((variant_index, field_index))));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
fn encode_info_for_generics(&mut self, generics: &hir::Generics) {
|
2018-05-27 16:56:01 +01:00
|
|
|
generics.params.iter().for_each(|param| match param.kind {
|
|
|
|
hir::GenericParamKind::Lifetime { .. } => {}
|
|
|
|
hir::GenericParamKind::Type { ref default, .. } => {
|
|
|
|
let def_id = self.tcx.hir.local_def_id(param.id);
|
|
|
|
let has_default = Untracked(default.is_some());
|
|
|
|
let encode_info = IsolatedEncoder::encode_info_for_ty_param;
|
|
|
|
self.record(def_id, encode_info, (def_id, has_default));
|
2018-05-26 00:27:54 +01:00
|
|
|
}
|
2018-05-27 16:56:01 +01:00
|
|
|
});
|
2017-04-27 16:12:57 +02:00
|
|
|
}
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
fn encode_info_for_ty(&mut self, ty: &hir::Ty) {
|
2017-08-07 08:08:53 +03:00
|
|
|
match ty.node {
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::Array(_, ref length) => {
|
2018-05-17 21:28:50 +03:00
|
|
|
let def_id = self.tcx.hir.local_def_id(length.id);
|
|
|
|
self.record(def_id, IsolatedEncoder::encode_info_for_anon_const, def_id);
|
2017-08-07 08:08:53 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
2017-04-27 16:12:57 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
fn encode_info_for_expr(&mut self, expr: &hir::Expr) {
|
|
|
|
match expr.node {
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Closure(..) => {
|
2017-04-27 16:12:57 +02:00
|
|
|
let def_id = self.tcx.hir.local_def_id(expr.id);
|
|
|
|
self.record(def_id, IsolatedEncoder::encode_info_for_closure, def_id);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// In some cases, along with the item itself, we also
|
|
|
|
/// encode some sub-items. Usually we want some info from the item
|
|
|
|
/// so it's easier to do that here then to wait until we would encounter
|
|
|
|
/// normally in the visitor walk.
|
|
|
|
fn encode_addl_info_for_item(&mut self, item: &hir::Item) {
|
|
|
|
let def_id = self.tcx.hir.local_def_id(item.id);
|
|
|
|
match item.node {
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Static(..) |
|
|
|
|
hir::ItemKind::Const(..) |
|
|
|
|
hir::ItemKind::Fn(..) |
|
|
|
|
hir::ItemKind::Mod(..) |
|
|
|
|
hir::ItemKind::ForeignMod(..) |
|
|
|
|
hir::ItemKind::GlobalAsm(..) |
|
|
|
|
hir::ItemKind::ExternCrate(..) |
|
|
|
|
hir::ItemKind::Use(..) |
|
|
|
|
hir::ItemKind::Ty(..) |
|
|
|
|
hir::ItemKind::Existential(..) |
|
|
|
|
hir::ItemKind::TraitAlias(..) => {
|
2017-04-27 16:12:57 +02:00
|
|
|
// no sub-item recording needed in these cases
|
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Enum(..) => {
|
2017-04-27 16:12:57 +02:00
|
|
|
self.encode_fields(def_id);
|
|
|
|
|
|
|
|
let def = self.tcx.adt_def(def_id);
|
|
|
|
for (i, variant) in def.variants.iter().enumerate() {
|
|
|
|
self.record(variant.did,
|
|
|
|
IsolatedEncoder::encode_enum_variant_info,
|
|
|
|
(def_id, Untracked(i)));
|
2016-09-16 17:25:54 +03:00
|
|
|
}
|
2016-09-15 11:04:00 +03:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Struct(ref struct_def, _) => {
|
2017-04-27 16:12:57 +02:00
|
|
|
self.encode_fields(def_id);
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
// If the struct has a constructor, encode it.
|
|
|
|
if !struct_def.is_struct() {
|
|
|
|
let ctor_def_id = self.tcx.hir.local_def_id(struct_def.id());
|
|
|
|
self.record(ctor_def_id,
|
|
|
|
IsolatedEncoder::encode_struct_ctor,
|
|
|
|
(def_id, ctor_def_id));
|
|
|
|
}
|
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Union(..) => {
|
2017-04-27 16:12:57 +02:00
|
|
|
self.encode_fields(def_id);
|
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Impl(..) => {
|
2017-04-27 16:12:57 +02:00
|
|
|
for &trait_item_def_id in self.tcx.associated_item_def_ids(def_id).iter() {
|
|
|
|
self.record(trait_item_def_id,
|
|
|
|
IsolatedEncoder::encode_info_for_impl_item,
|
|
|
|
trait_item_def_id);
|
|
|
|
}
|
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Trait(..) => {
|
2017-04-27 16:12:57 +02:00
|
|
|
for &item_def_id in self.tcx.associated_item_def_ids(def_id).iter() {
|
|
|
|
self.record(item_def_id,
|
|
|
|
IsolatedEncoder::encode_info_for_trait_item,
|
|
|
|
item_def_id);
|
|
|
|
}
|
|
|
|
}
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
}
|
2017-04-27 16:12:57 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
struct ImplVisitor<'a, 'tcx: 'a> {
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
impls: FxHashMap<DefId, Vec<DefIndex>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for ImplVisitor<'a, 'tcx> {
|
|
|
|
fn visit_item(&mut self, item: &hir::Item) {
|
2018-07-11 23:36:06 +08:00
|
|
|
if let hir::ItemKind::Impl(..) = item.node {
|
2017-04-27 16:12:57 +02:00
|
|
|
let impl_id = self.tcx.hir.local_def_id(item.id);
|
|
|
|
if let Some(trait_ref) = self.tcx.impl_trait_ref(impl_id) {
|
|
|
|
self.impls
|
|
|
|
.entry(trait_ref.def_id)
|
2018-07-21 22:43:31 +03:00
|
|
|
.or_default()
|
2017-04-27 16:12:57 +02:00
|
|
|
.push(impl_id.index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_trait_item(&mut self, _trait_item: &'v hir::TraitItem) {}
|
|
|
|
|
|
|
|
fn visit_impl_item(&mut self, _impl_item: &'v hir::ImplItem) {
|
|
|
|
// handled in `visit_item` above
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
// NOTE(eddyb) The following comment was preserved for posterity, even
|
|
|
|
// though it's no longer relevant as EBML (which uses nested & tagged
|
|
|
|
// "documents") was replaced with a scheme that can't go out of bounds.
|
|
|
|
//
|
|
|
|
// And here we run into yet another obscure archive bug: in which metadata
|
|
|
|
// loaded from archives may have trailing garbage bytes. Awhile back one of
|
2017-03-12 14:13:35 -04:00
|
|
|
// our tests was failing sporadically on the macOS 64-bit builders (both nopt
|
2016-09-16 17:25:54 +03:00
|
|
|
// and opt) by having ebml generate an out-of-bounds panic when looking at
|
|
|
|
// metadata.
|
|
|
|
//
|
|
|
|
// Upon investigation it turned out that the metadata file inside of an rlib
|
|
|
|
// (and ar archive) was being corrupted. Some compilations would generate a
|
|
|
|
// metadata file which would end in a few extra bytes, while other
|
|
|
|
// compilations would not have these extra bytes appended to the end. These
|
|
|
|
// extra bytes were interpreted by ebml as an extra tag, so they ended up
|
|
|
|
// being interpreted causing the out-of-bounds.
|
|
|
|
//
|
|
|
|
// The root cause of why these extra bytes were appearing was never
|
|
|
|
// discovered, and in the meantime the solution we're employing is to insert
|
|
|
|
// the length of the metadata to the start of the metadata. Later on this
|
|
|
|
// will allow us to slice the metadata to the precise length that we just
|
|
|
|
// generated regardless of trailing bytes that end up in it.
|
|
|
|
|
2016-09-01 16:55:33 +03:00
|
|
|
pub fn encode_metadata<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2018-02-23 16:25:03 +01:00
|
|
|
link_meta: &LinkMeta)
|
2017-11-29 16:28:25 +01:00
|
|
|
-> EncodedMetadata
|
2017-04-05 23:39:02 +02:00
|
|
|
{
|
2018-06-04 22:14:02 +02:00
|
|
|
let mut encoder = opaque::Encoder::new(vec![]);
|
|
|
|
encoder.emit_raw_bytes(METADATA_HEADER);
|
2016-09-16 17:25:54 +03:00
|
|
|
|
2017-04-27 16:12:57 +02:00
|
|
|
// Will be filled with the root position after encoding everything.
|
2018-06-04 22:14:02 +02:00
|
|
|
encoder.emit_raw_bytes(&[0, 0, 0, 0]);
|
2016-09-01 16:55:33 +03:00
|
|
|
|
2018-06-04 22:14:02 +02:00
|
|
|
let (root, mut result) = {
|
2016-11-22 14:51:55 +02:00
|
|
|
let mut ecx = EncodeContext {
|
2018-06-04 22:14:02 +02:00
|
|
|
opaque: encoder,
|
2017-08-06 22:54:09 -07:00
|
|
|
tcx,
|
|
|
|
link_meta,
|
2016-10-22 18:44:32 +05:30
|
|
|
lazy_state: LazyState::NoNode,
|
|
|
|
type_shorthands: Default::default(),
|
|
|
|
predicate_shorthands: Default::default(),
|
2018-08-18 12:14:09 +02:00
|
|
|
source_file_cache: tcx.sess.source_map().files()[0].clone(),
|
2018-04-10 09:58:46 +02:00
|
|
|
interpret_allocs: Default::default(),
|
|
|
|
interpret_allocs_inverse: Default::default(),
|
2016-11-22 14:51:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Encode the rustc version string in a predictable location.
|
|
|
|
rustc_version().encode(&mut ecx).unwrap();
|
|
|
|
|
|
|
|
// Encode all the entries and extra information in the crate,
|
|
|
|
// culminating in the `CrateRoot` which points to all of it.
|
2018-06-04 22:14:02 +02:00
|
|
|
let root = ecx.encode_crate_root();
|
|
|
|
(root, ecx.opaque.into_inner())
|
2016-11-22 14:51:55 +02:00
|
|
|
};
|
2014-01-03 16:28:26 -08:00
|
|
|
|
2016-09-16 17:25:54 +03:00
|
|
|
// Encode the root position.
|
|
|
|
let header = METADATA_HEADER.len();
|
|
|
|
let pos = root.position;
|
|
|
|
result[header + 0] = (pos >> 24) as u8;
|
|
|
|
result[header + 1] = (pos >> 16) as u8;
|
2016-10-22 18:44:32 +05:30
|
|
|
result[header + 2] = (pos >> 8) as u8;
|
|
|
|
result[header + 3] = (pos >> 0) as u8;
|
2012-08-27 16:53:54 -07:00
|
|
|
|
2017-11-29 16:28:25 +01:00
|
|
|
EncodedMetadata { raw_data: result }
|
2011-06-27 15:20:17 -07:00
|
|
|
}
|
2017-02-06 15:26:32 -05:00
|
|
|
|
|
|
|
pub fn get_repr_options<'a, 'tcx, 'gcx>(tcx: &TyCtxt<'a, 'tcx, 'gcx>, did: DefId) -> ReprOptions {
|
2017-04-24 15:20:46 +03:00
|
|
|
let ty = tcx.type_of(did);
|
2017-02-06 15:26:32 -05:00
|
|
|
match ty.sty {
|
|
|
|
ty::TyAdt(ref def, _) => return def.repr,
|
|
|
|
_ => bug!("{} is not an ADT", ty),
|
|
|
|
}
|
|
|
|
}
|