]>
Commit | Line | Data |
---|---|---|
c34b1796 | 1 | // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT |
223e47cc LB |
2 | // file at the top-level directory of this distribution and at |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
9e0c209e | 11 | use index::Index; |
7cac9316 XL |
12 | use index_builder::{FromId, IndexBuilder, Untracked}; |
13 | use isolated_encoder::IsolatedEncoder; | |
9e0c209e | 14 | use schema::*; |
92a42be0 | 15 | |
cc61c64b | 16 | use rustc::middle::cstore::{LinkMeta, LinkagePreference, NativeLibrary, |
041b39d2 XL |
17 | EncodedMetadata, EncodedMetadataHashes, |
18 | EncodedMetadataHash}; | |
19 | use rustc::hir::def::CtorKind; | |
cc61c64b | 20 | use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefIndex, DefId, LOCAL_CRATE}; |
041b39d2 | 21 | use rustc::hir::map::definitions::{DefPathTable, GlobalMetaDataKind}; |
7cac9316 | 22 | use rustc::ich::Fingerprint; |
9e0c209e SL |
23 | use rustc::middle::dependency_format::Linkage; |
24 | use rustc::middle::lang_items; | |
25 | use rustc::mir; | |
54a0048b | 26 | use rustc::traits::specialization_graph; |
8bb4bdeb | 27 | use rustc::ty::{self, Ty, TyCtxt, ReprOptions}; |
abe05a73 | 28 | use rustc::ty::codec::{self as ty_codec, TyEncoder}; |
92a42be0 | 29 | |
c30ab7b3 | 30 | use rustc::session::config::{self, CrateTypeProcMacro}; |
476ff2be | 31 | use rustc::util::nodemap::{FxHashMap, NodeSet}; |
1a4d82fc | 32 | |
9e0c209e | 33 | use rustc_serialize::{Encodable, Encoder, SpecializedEncoder, opaque}; |
7cac9316 | 34 | |
c34b1796 | 35 | use std::io::prelude::*; |
9e0c209e | 36 | use std::io::Cursor; |
7cac9316 | 37 | use std::path::Path; |
e9174d1e | 38 | use std::rc::Rc; |
b039eaaf | 39 | use std::u32; |
9e0c209e | 40 | use syntax::ast::{self, CRATE_NODE_ID}; |
32a655c1 | 41 | use syntax::codemap::Spanned; |
9e0c209e | 42 | use syntax::attr; |
476ff2be | 43 | use syntax::symbol::Symbol; |
7cac9316 | 44 | use syntax_pos; |
1a4d82fc | 45 | |
54a0048b | 46 | use rustc::hir::{self, PatKind}; |
476ff2be SL |
47 | use rustc::hir::itemlikevisit::ItemLikeVisitor; |
48 | use rustc::hir::intravisit::{Visitor, NestedVisitorMap}; | |
54a0048b | 49 | use rustc::hir::intravisit; |
9e0c209e | 50 | |
1a4d82fc | 51 | pub struct EncodeContext<'a, 'tcx: 'a> { |
9e0c209e | 52 | opaque: opaque::Encoder<'a>, |
a7813a04 | 53 | pub tcx: TyCtxt<'a, 'tcx, 'tcx>, |
9e0c209e | 54 | link_meta: &'a LinkMeta, |
476ff2be | 55 | exported_symbols: &'a NodeSet, |
9e0c209e SL |
56 | |
57 | lazy_state: LazyState, | |
476ff2be SL |
58 | type_shorthands: FxHashMap<Ty<'tcx>, usize>, |
59 | predicate_shorthands: FxHashMap<ty::Predicate<'tcx>, usize>, | |
cc61c64b | 60 | |
7cac9316 XL |
61 | pub metadata_hashes: EncodedMetadataHashes, |
62 | pub compute_ich: bool, | |
9e0c209e SL |
63 | } |
64 | ||
65 | macro_rules! encoder_methods { | |
66 | ($($name:ident($ty:ty);)*) => { | |
67 | $(fn $name(&mut self, value: $ty) -> Result<(), Self::Error> { | |
68 | self.opaque.$name(value) | |
69 | })* | |
b039eaaf SL |
70 | } |
71 | } | |
72 | ||
9e0c209e SL |
73 | impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> { |
74 | type Error = <opaque::Encoder<'a> as Encoder>::Error; | |
b039eaaf | 75 | |
9e0c209e SL |
76 | fn emit_nil(&mut self) -> Result<(), Self::Error> { |
77 | Ok(()) | |
b039eaaf SL |
78 | } |
79 | ||
9e0c209e SL |
80 | encoder_methods! { |
81 | emit_usize(usize); | |
32a655c1 | 82 | emit_u128(u128); |
9e0c209e SL |
83 | emit_u64(u64); |
84 | emit_u32(u32); | |
85 | emit_u16(u16); | |
86 | emit_u8(u8); | |
87 | ||
88 | emit_isize(isize); | |
32a655c1 | 89 | emit_i128(i128); |
9e0c209e SL |
90 | emit_i64(i64); |
91 | emit_i32(i32); | |
92 | emit_i16(i16); | |
93 | emit_i8(i8); | |
94 | ||
95 | emit_bool(bool); | |
96 | emit_f64(f64); | |
97 | emit_f32(f32); | |
98 | emit_char(char); | |
99 | emit_str(&str); | |
b039eaaf SL |
100 | } |
101 | } | |
102 | ||
9e0c209e SL |
103 | impl<'a, 'tcx, T> SpecializedEncoder<Lazy<T>> for EncodeContext<'a, 'tcx> { |
104 | fn specialized_encode(&mut self, lazy: &Lazy<T>) -> Result<(), Self::Error> { | |
105 | self.emit_lazy_distance(lazy.position, Lazy::<T>::min_size()) | |
106 | } | |
223e47cc LB |
107 | } |
108 | ||
9e0c209e SL |
109 | impl<'a, 'tcx, T> SpecializedEncoder<LazySeq<T>> for EncodeContext<'a, 'tcx> { |
110 | fn specialized_encode(&mut self, seq: &LazySeq<T>) -> Result<(), Self::Error> { | |
111 | self.emit_usize(seq.len)?; | |
112 | if seq.len == 0 { | |
113 | return Ok(()); | |
114 | } | |
115 | self.emit_lazy_distance(seq.position, LazySeq::<T>::min_size(seq.len)) | |
116 | } | |
223e47cc LB |
117 | } |
118 | ||
abe05a73 XL |
119 | impl<'a, 'tcx> SpecializedEncoder<CrateNum> for EncodeContext<'a, 'tcx> { |
120 | #[inline] | |
121 | fn specialized_encode(&mut self, cnum: &CrateNum) -> Result<(), Self::Error> { | |
122 | self.emit_u32(cnum.as_u32()) | |
123 | } | |
124 | } | |
125 | ||
126 | impl<'a, 'tcx> SpecializedEncoder<DefId> for EncodeContext<'a, 'tcx> { | |
127 | #[inline] | |
128 | fn specialized_encode(&mut self, def_id: &DefId) -> Result<(), Self::Error> { | |
129 | let DefId { | |
130 | krate, | |
131 | index, | |
132 | } = *def_id; | |
133 | ||
134 | krate.encode(self)?; | |
135 | index.encode(self) | |
136 | } | |
137 | } | |
138 | ||
139 | impl<'a, 'tcx> SpecializedEncoder<DefIndex> for EncodeContext<'a, 'tcx> { | |
140 | #[inline] | |
141 | fn specialized_encode(&mut self, def_index: &DefIndex) -> Result<(), Self::Error> { | |
142 | self.emit_u32(def_index.as_u32()) | |
143 | } | |
144 | } | |
145 | ||
9e0c209e SL |
146 | impl<'a, 'tcx> SpecializedEncoder<Ty<'tcx>> for EncodeContext<'a, 'tcx> { |
147 | fn specialized_encode(&mut self, ty: &Ty<'tcx>) -> Result<(), Self::Error> { | |
abe05a73 | 148 | ty_codec::encode_with_shorthand(self, ty, |ecx| &mut ecx.type_shorthands) |
9e0c209e | 149 | } |
d9579d0f AL |
150 | } |
151 | ||
9e0c209e | 152 | impl<'a, 'tcx> SpecializedEncoder<ty::GenericPredicates<'tcx>> for EncodeContext<'a, 'tcx> { |
c30ab7b3 SL |
153 | fn specialized_encode(&mut self, |
154 | predicates: &ty::GenericPredicates<'tcx>) | |
9e0c209e | 155 | -> Result<(), Self::Error> { |
abe05a73 | 156 | ty_codec::encode_predicates(self, predicates, |ecx| &mut ecx.predicate_shorthands) |
9e0c209e | 157 | } |
970d7e83 | 158 | } |
223e47cc | 159 | |
abe05a73 XL |
160 | impl<'a, 'tcx> TyEncoder for EncodeContext<'a, 'tcx> { |
161 | fn position(&self) -> usize { | |
9e0c209e SL |
162 | self.opaque.position() |
163 | } | |
abe05a73 XL |
164 | } |
165 | ||
166 | impl<'a, 'tcx> EncodeContext<'a, 'tcx> { | |
1a4d82fc | 167 | |
9e0c209e SL |
168 | fn emit_node<F: FnOnce(&mut Self, usize) -> R, R>(&mut self, f: F) -> R { |
169 | assert_eq!(self.lazy_state, LazyState::NoNode); | |
170 | let pos = self.position(); | |
171 | self.lazy_state = LazyState::NodeStart(pos); | |
172 | let r = f(self, pos); | |
173 | self.lazy_state = LazyState::NoNode; | |
174 | r | |
175 | } | |
85aaf69f | 176 | |
c30ab7b3 SL |
177 | fn emit_lazy_distance(&mut self, |
178 | position: usize, | |
179 | min_size: usize) | |
9e0c209e SL |
180 | -> Result<(), <Self as Encoder>::Error> { |
181 | let min_end = position + min_size; | |
182 | let distance = match self.lazy_state { | |
c30ab7b3 | 183 | LazyState::NoNode => bug!("emit_lazy_distance: outside of a metadata node"), |
9e0c209e SL |
184 | LazyState::NodeStart(start) => { |
185 | assert!(min_end <= start); | |
186 | start - min_end | |
187 | } | |
188 | LazyState::Previous(last_min_end) => { | |
189 | assert!(last_min_end <= position); | |
190 | position - last_min_end | |
191 | } | |
192 | }; | |
193 | self.lazy_state = LazyState::Previous(min_end); | |
194 | self.emit_usize(distance) | |
195 | } | |
1a4d82fc | 196 | |
9e0c209e SL |
197 | pub fn lazy<T: Encodable>(&mut self, value: &T) -> Lazy<T> { |
198 | self.emit_node(|ecx, pos| { | |
199 | value.encode(ecx).unwrap(); | |
1a4d82fc | 200 | |
9e0c209e SL |
201 | assert!(pos + Lazy::<T>::min_size() <= ecx.position()); |
202 | Lazy::with_position(pos) | |
203 | }) | |
204 | } | |
223e47cc | 205 | |
cc61c64b | 206 | pub fn lazy_seq<I, T>(&mut self, iter: I) -> LazySeq<T> |
c30ab7b3 SL |
207 | where I: IntoIterator<Item = T>, |
208 | T: Encodable | |
209 | { | |
9e0c209e SL |
210 | self.emit_node(|ecx, pos| { |
211 | let len = iter.into_iter().map(|value| value.encode(ecx).unwrap()).count(); | |
223e47cc | 212 | |
9e0c209e SL |
213 | assert!(pos + LazySeq::<T>::min_size(len) <= ecx.position()); |
214 | LazySeq::with_position_and_length(pos, len) | |
215 | }) | |
216 | } | |
1a4d82fc | 217 | |
cc61c64b | 218 | pub fn lazy_seq_ref<'b, I, T>(&mut self, iter: I) -> LazySeq<T> |
c30ab7b3 SL |
219 | where I: IntoIterator<Item = &'b T>, |
220 | T: 'b + Encodable | |
221 | { | |
9e0c209e SL |
222 | self.emit_node(|ecx, pos| { |
223 | let len = iter.into_iter().map(|value| value.encode(ecx).unwrap()).count(); | |
1a4d82fc | 224 | |
9e0c209e SL |
225 | assert!(pos + LazySeq::<T>::min_size(len) <= ecx.position()); |
226 | LazySeq::with_position_and_length(pos, len) | |
227 | }) | |
1a4d82fc | 228 | } |
223e47cc | 229 | |
7cac9316 XL |
230 | // Encodes something that corresponds to a single DepNode::GlobalMetaData |
231 | // and registers the Fingerprint in the `metadata_hashes` map. | |
232 | pub fn tracked<'x, DATA, R>(&'x mut self, | |
041b39d2 | 233 | def_index: DefIndex, |
7cac9316 XL |
234 | op: fn(&mut IsolatedEncoder<'x, 'a, 'tcx>, DATA) -> R, |
235 | data: DATA) | |
ea8adc8c | 236 | -> R { |
7cac9316 XL |
237 | let mut entry_builder = IsolatedEncoder::new(self); |
238 | let ret = op(&mut entry_builder, data); | |
239 | let (fingerprint, this) = entry_builder.finish(); | |
240 | ||
241 | if let Some(fingerprint) = fingerprint { | |
041b39d2 | 242 | this.metadata_hashes.hashes.push(EncodedMetadataHash { |
abe05a73 | 243 | def_index: def_index.as_u32(), |
041b39d2 XL |
244 | hash: fingerprint, |
245 | }) | |
7cac9316 XL |
246 | } |
247 | ||
ea8adc8c | 248 | ret |
7cac9316 XL |
249 | } |
250 | ||
251 | fn encode_info_for_items(&mut self) -> Index { | |
252 | let krate = self.tcx.hir.krate(); | |
253 | let mut index = IndexBuilder::new(self); | |
254 | index.record(DefId::local(CRATE_DEF_INDEX), | |
255 | IsolatedEncoder::encode_info_for_mod, | |
256 | FromId(CRATE_NODE_ID, (&krate.module, &krate.attrs, &hir::Public))); | |
257 | let mut visitor = EncodeVisitor { index: index }; | |
258 | krate.visit_all_item_likes(&mut visitor.as_deep_visitor()); | |
259 | for macro_def in &krate.exported_macros { | |
260 | visitor.visit_macro_def(macro_def); | |
261 | } | |
262 | visitor.index.into_items() | |
263 | } | |
264 | ||
265 | fn encode_def_path_table(&mut self) -> Lazy<DefPathTable> { | |
266 | let definitions = self.tcx.hir.definitions(); | |
267 | self.lazy(definitions.def_path_table()) | |
268 | } | |
269 | ||
270 | fn encode_codemap(&mut self) -> LazySeq<syntax_pos::FileMap> { | |
271 | let codemap = self.tcx.sess.codemap(); | |
272 | let all_filemaps = codemap.files(); | |
273 | ||
274 | let (working_dir, working_dir_was_remapped) = self.tcx.sess.working_dir.clone(); | |
275 | ||
276 | let adapted = all_filemaps.iter() | |
277 | .filter(|filemap| { | |
278 | // No need to re-export imported filemaps, as any downstream | |
279 | // crate will import them from their original source. | |
280 | !filemap.is_imported() | |
281 | }) | |
282 | .map(|filemap| { | |
283 | // When exporting FileMaps, we expand all paths to absolute | |
284 | // paths because any relative paths are potentially relative to | |
285 | // a wrong directory. | |
286 | // However, if a path has been modified via | |
287 | // `-Zremap-path-prefix` we assume the user has already set | |
288 | // things up the way they want and don't touch the path values | |
289 | // anymore. | |
290 | let name = Path::new(&filemap.name); | |
291 | if filemap.name_was_remapped || | |
292 | (name.is_relative() && working_dir_was_remapped) { | |
293 | // This path of this FileMap has been modified by | |
294 | // path-remapping, so we use it verbatim (and avoid cloning | |
295 | // the whole map in the process). | |
296 | filemap.clone() | |
297 | } else { | |
298 | let mut adapted = (**filemap).clone(); | |
299 | let abs_path = Path::new(&working_dir).join(name) | |
300 | .to_string_lossy() | |
301 | .into_owned(); | |
302 | adapted.name = abs_path; | |
303 | Rc::new(adapted) | |
304 | } | |
305 | }) | |
306 | .collect::<Vec<_>>(); | |
307 | ||
308 | self.lazy_seq_ref(adapted.iter().map(|rc| &**rc)) | |
309 | } | |
310 | ||
311 | fn encode_crate_root(&mut self) -> Lazy<CrateRoot> { | |
312 | let mut i = self.position(); | |
313 | ||
041b39d2 XL |
314 | let tcx = self.tcx; |
315 | let global_metadata_def_index = move |kind: GlobalMetaDataKind| { | |
316 | kind.def_index(tcx.hir.definitions().def_path_table()) | |
317 | }; | |
318 | ||
7cac9316 | 319 | let crate_deps = self.tracked( |
041b39d2 | 320 | global_metadata_def_index(GlobalMetaDataKind::CrateDeps), |
7cac9316 XL |
321 | IsolatedEncoder::encode_crate_deps, |
322 | ()); | |
323 | let dylib_dependency_formats = self.tracked( | |
041b39d2 | 324 | global_metadata_def_index(GlobalMetaDataKind::DylibDependencyFormats), |
7cac9316 XL |
325 | IsolatedEncoder::encode_dylib_dependency_formats, |
326 | ()); | |
327 | let dep_bytes = self.position() - i; | |
328 | ||
329 | // Encode the language items. | |
330 | i = self.position(); | |
331 | let lang_items = self.tracked( | |
041b39d2 | 332 | global_metadata_def_index(GlobalMetaDataKind::LangItems), |
7cac9316 XL |
333 | IsolatedEncoder::encode_lang_items, |
334 | ()); | |
335 | ||
336 | let lang_items_missing = self.tracked( | |
041b39d2 | 337 | global_metadata_def_index(GlobalMetaDataKind::LangItemsMissing), |
7cac9316 XL |
338 | IsolatedEncoder::encode_lang_items_missing, |
339 | ()); | |
340 | let lang_item_bytes = self.position() - i; | |
341 | ||
342 | // Encode the native libraries used | |
343 | i = self.position(); | |
344 | let native_libraries = self.tracked( | |
041b39d2 | 345 | global_metadata_def_index(GlobalMetaDataKind::NativeLibraries), |
7cac9316 XL |
346 | IsolatedEncoder::encode_native_libraries, |
347 | ()); | |
348 | let native_lib_bytes = self.position() - i; | |
349 | ||
350 | // Encode codemap | |
351 | i = self.position(); | |
352 | let codemap = self.encode_codemap(); | |
353 | let codemap_bytes = self.position() - i; | |
354 | ||
355 | // Encode DefPathTable | |
356 | i = self.position(); | |
357 | let def_path_table = self.encode_def_path_table(); | |
358 | let def_path_table_bytes = self.position() - i; | |
359 | ||
360 | // Encode the def IDs of impls, for coherence checking. | |
361 | i = self.position(); | |
362 | let impls = self.tracked( | |
041b39d2 | 363 | global_metadata_def_index(GlobalMetaDataKind::Impls), |
7cac9316 XL |
364 | IsolatedEncoder::encode_impls, |
365 | ()); | |
366 | let impl_bytes = self.position() - i; | |
367 | ||
368 | // Encode exported symbols info. | |
369 | i = self.position(); | |
370 | let exported_symbols = self.tracked( | |
041b39d2 | 371 | global_metadata_def_index(GlobalMetaDataKind::ExportedSymbols), |
7cac9316 XL |
372 | IsolatedEncoder::encode_exported_symbols, |
373 | self.exported_symbols); | |
374 | let exported_symbols_bytes = self.position() - i; | |
375 | ||
376 | // Encode and index the items. | |
377 | i = self.position(); | |
378 | let items = self.encode_info_for_items(); | |
379 | let item_bytes = self.position() - i; | |
380 | ||
381 | i = self.position(); | |
382 | let index = items.write_index(&mut self.opaque.cursor); | |
383 | let index_bytes = self.position() - i; | |
384 | ||
385 | let tcx = self.tcx; | |
386 | let link_meta = self.link_meta; | |
387 | let is_proc_macro = tcx.sess.crate_types.borrow().contains(&CrateTypeProcMacro); | |
041b39d2 XL |
388 | let has_default_lib_allocator = |
389 | attr::contains_name(tcx.hir.krate_attrs(), "default_lib_allocator"); | |
390 | let has_global_allocator = tcx.sess.has_global_allocator.get(); | |
7cac9316 XL |
391 | let root = self.lazy(&CrateRoot { |
392 | name: tcx.crate_name(LOCAL_CRATE), | |
393 | triple: tcx.sess.opts.target_triple.clone(), | |
394 | hash: link_meta.crate_hash, | |
395 | disambiguator: tcx.sess.local_crate_disambiguator(), | |
ea8adc8c XL |
396 | panic_strategy: tcx.sess.panic_strategy(), |
397 | has_global_allocator: has_global_allocator, | |
398 | has_default_lib_allocator: has_default_lib_allocator, | |
7cac9316 XL |
399 | plugin_registrar_fn: tcx.sess |
400 | .plugin_registrar_fn | |
401 | .get() | |
402 | .map(|id| tcx.hir.local_def_id(id).index), | |
403 | macro_derive_registrar: if is_proc_macro { | |
404 | let id = tcx.sess.derive_registrar_fn.get().unwrap(); | |
405 | Some(tcx.hir.local_def_id(id).index) | |
406 | } else { | |
407 | None | |
408 | }, | |
409 | ||
3b2f2976 XL |
410 | crate_deps, |
411 | dylib_dependency_formats, | |
412 | lang_items, | |
413 | lang_items_missing, | |
414 | native_libraries, | |
415 | codemap, | |
416 | def_path_table, | |
417 | impls, | |
418 | exported_symbols, | |
419 | index, | |
7cac9316 XL |
420 | }); |
421 | ||
422 | let total_bytes = self.position(); | |
423 | ||
041b39d2 | 424 | self.metadata_hashes.hashes.push(EncodedMetadataHash { |
abe05a73 | 425 | def_index: global_metadata_def_index(GlobalMetaDataKind::Krate).as_u32(), |
041b39d2 XL |
426 | hash: Fingerprint::from_smaller_hash(link_meta.crate_hash.as_u64()) |
427 | }); | |
7cac9316 XL |
428 | |
429 | if self.tcx.sess.meta_stats() { | |
430 | let mut zero_bytes = 0; | |
431 | for e in self.opaque.cursor.get_ref() { | |
432 | if *e == 0 { | |
433 | zero_bytes += 1; | |
434 | } | |
435 | } | |
436 | ||
437 | println!("metadata stats:"); | |
438 | println!(" dep bytes: {}", dep_bytes); | |
439 | println!(" lang item bytes: {}", lang_item_bytes); | |
440 | println!(" native bytes: {}", native_lib_bytes); | |
441 | println!(" codemap bytes: {}", codemap_bytes); | |
442 | println!(" impl bytes: {}", impl_bytes); | |
443 | println!(" exp. symbols bytes: {}", exported_symbols_bytes); | |
444 | println!(" def-path table bytes: {}", def_path_table_bytes); | |
445 | println!(" item bytes: {}", item_bytes); | |
446 | println!(" index bytes: {}", index_bytes); | |
447 | println!(" zero bytes: {}", zero_bytes); | |
448 | println!(" total bytes: {}", total_bytes); | |
449 | } | |
450 | ||
451 | root | |
452 | } | |
cc61c64b | 453 | } |
1a4d82fc | 454 | |
7cac9316 XL |
455 | // These are methods for encoding various things. They are meant to be used with |
456 | // IndexBuilder::record() and EncodeContext::tracked(). They actually | |
457 | // would not have to be methods of IsolatedEncoder (free standing functions | |
458 | // taking IsolatedEncoder as first argument would be just fine) but by making | |
459 | // them methods we don't have to repeat the lengthy `<'a, 'b: 'a, 'tcx: 'b>` | |
460 | // clause again and again. | |
461 | impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> { | |
462 | fn encode_variances_of(&mut self, def_id: DefId) -> LazySeq<ty::Variance> { | |
463 | debug!("IsolatedEncoder::encode_variances_of({:?})", def_id); | |
9e0c209e | 464 | let tcx = self.tcx; |
7cac9316 | 465 | self.lazy_seq_from_slice(&tcx.variances_of(def_id)) |
223e47cc | 466 | } |
970d7e83 | 467 | |
9e0c209e SL |
468 | fn encode_item_type(&mut self, def_id: DefId) -> Lazy<Ty<'tcx>> { |
469 | let tcx = self.tcx; | |
7cac9316 XL |
470 | let ty = tcx.type_of(def_id); |
471 | debug!("IsolatedEncoder::encode_item_type({:?}) => {:?}", def_id, ty); | |
cc61c64b | 472 | self.lazy(&ty) |
970d7e83 LB |
473 | } |
474 | ||
9e0c209e SL |
475 | /// Encode data for the given variant of the given ADT. The |
476 | /// index of the variant is untracked: this is ok because we | |
477 | /// will have to lookup the adt-def by its id, and that gives us | |
478 | /// the right to access any information in the adt-def (including, | |
479 | /// e.g., the length of the various vectors). | |
480 | fn encode_enum_variant_info(&mut self, | |
c30ab7b3 SL |
481 | (enum_did, Untracked(index)): (DefId, Untracked<usize>)) |
482 | -> Entry<'tcx> { | |
9e0c209e | 483 | let tcx = self.tcx; |
7cac9316 | 484 | let def = tcx.adt_def(enum_did); |
9e0c209e SL |
485 | let variant = &def.variants[index]; |
486 | let def_id = variant.did; | |
7cac9316 | 487 | debug!("IsolatedEncoder::encode_enum_variant_info({:?})", def_id); |
9e0c209e SL |
488 | |
489 | let data = VariantData { | |
c30ab7b3 | 490 | ctor_kind: variant.ctor_kind, |
8bb4bdeb | 491 | discr: variant.discr, |
c30ab7b3 | 492 | struct_ctor: None, |
041b39d2 XL |
493 | ctor_sig: if variant.ctor_kind == CtorKind::Fn { |
494 | Some(self.lazy(&tcx.fn_sig(def_id))) | |
495 | } else { | |
496 | None | |
497 | } | |
9e0c209e | 498 | }; |
970d7e83 | 499 | |
32a655c1 SL |
500 | let enum_id = tcx.hir.as_local_node_id(enum_did).unwrap(); |
501 | let enum_vis = &tcx.hir.expect_item(enum_id).vis; | |
9e0c209e SL |
502 | |
503 | Entry { | |
504 | kind: EntryKind::Variant(self.lazy(&data)), | |
32a655c1 | 505 | visibility: self.lazy(&ty::Visibility::from_hir(enum_vis, enum_id, tcx)), |
476ff2be | 506 | span: self.lazy(&tcx.def_span(def_id)), |
9e0c209e SL |
507 | attributes: self.encode_attributes(&tcx.get_attrs(def_id)), |
508 | children: self.lazy_seq(variant.fields.iter().map(|f| { | |
509 | assert!(f.did.is_local()); | |
510 | f.did.index | |
511 | })), | |
512 | stability: self.encode_stability(def_id), | |
513 | deprecation: self.encode_deprecation(def_id), | |
514 | ||
515 | ty: Some(self.encode_item_type(def_id)), | |
516 | inherent_impls: LazySeq::empty(), | |
041b39d2 XL |
517 | variances: if variant.ctor_kind == CtorKind::Fn { |
518 | self.encode_variances_of(def_id) | |
519 | } else { | |
520 | LazySeq::empty() | |
521 | }, | |
9e0c209e SL |
522 | generics: Some(self.encode_generics(def_id)), |
523 | predicates: Some(self.encode_predicates(def_id)), | |
524 | ||
525 | ast: None, | |
7cac9316 | 526 | mir: self.encode_optimized_mir(def_id), |
9e0c209e | 527 | } |
970d7e83 | 528 | } |
223e47cc | 529 | |
9e0c209e | 530 | fn encode_info_for_mod(&mut self, |
c30ab7b3 SL |
531 | FromId(id, (md, attrs, vis)): FromId<(&hir::Mod, |
532 | &[ast::Attribute], | |
533 | &hir::Visibility)>) | |
9e0c209e SL |
534 | -> Entry<'tcx> { |
535 | let tcx = self.tcx; | |
32a655c1 | 536 | let def_id = tcx.hir.local_def_id(id); |
7cac9316 | 537 | debug!("IsolatedEncoder::encode_info_for_mod({:?})", def_id); |
9e0c209e SL |
538 | |
539 | let data = ModData { | |
ea8adc8c XL |
540 | reexports: match tcx.module_exports(def_id) { |
541 | Some(ref exports) if *vis == hir::Public => { | |
cc61c64b XL |
542 | self.lazy_seq_from_slice(exports.as_slice()) |
543 | } | |
c30ab7b3 SL |
544 | _ => LazySeq::empty(), |
545 | }, | |
9e0c209e | 546 | }; |
223e47cc | 547 | |
9e0c209e SL |
548 | Entry { |
549 | kind: EntryKind::Mod(self.lazy(&data)), | |
32a655c1 | 550 | visibility: self.lazy(&ty::Visibility::from_hir(vis, id, tcx)), |
3b2f2976 | 551 | span: self.lazy(&tcx.def_span(def_id)), |
9e0c209e SL |
552 | attributes: self.encode_attributes(attrs), |
553 | children: self.lazy_seq(md.item_ids.iter().map(|item_id| { | |
32a655c1 | 554 | tcx.hir.local_def_id(item_id.id).index |
9e0c209e SL |
555 | })), |
556 | stability: self.encode_stability(def_id), | |
557 | deprecation: self.encode_deprecation(def_id), | |
558 | ||
559 | ty: None, | |
560 | inherent_impls: LazySeq::empty(), | |
561 | variances: LazySeq::empty(), | |
562 | generics: None, | |
563 | predicates: None, | |
564 | ||
565 | ast: None, | |
566 | mir: None | |
567 | } | |
568 | } | |
223e47cc | 569 | |
9e0c209e SL |
570 | /// Encode data for the given field of the given variant of the |
571 | /// given ADT. The indices of the variant/field are untracked: | |
572 | /// this is ok because we will have to lookup the adt-def by its | |
573 | /// id, and that gives us the right to access any information in | |
574 | /// the adt-def (including, e.g., the length of the various | |
575 | /// vectors). | |
576 | fn encode_field(&mut self, | |
c30ab7b3 SL |
577 | (adt_def_id, Untracked((variant_index, field_index))): (DefId, |
578 | Untracked<(usize, | |
579 | usize)>)) | |
580 | -> Entry<'tcx> { | |
9e0c209e | 581 | let tcx = self.tcx; |
7cac9316 | 582 | let variant = &tcx.adt_def(adt_def_id).variants[variant_index]; |
9e0c209e SL |
583 | let field = &variant.fields[field_index]; |
584 | ||
585 | let def_id = field.did; | |
7cac9316 | 586 | debug!("IsolatedEncoder::encode_field({:?})", def_id); |
cc61c64b | 587 | |
32a655c1 SL |
588 | let variant_id = tcx.hir.as_local_node_id(variant.did).unwrap(); |
589 | let variant_data = tcx.hir.expect_variant_data(variant_id); | |
9e0c209e SL |
590 | |
591 | Entry { | |
592 | kind: EntryKind::Field, | |
32a655c1 | 593 | visibility: self.lazy(&field.vis), |
476ff2be | 594 | span: self.lazy(&tcx.def_span(def_id)), |
9e0c209e SL |
595 | attributes: self.encode_attributes(&variant_data.fields()[field_index].attrs), |
596 | children: LazySeq::empty(), | |
597 | stability: self.encode_stability(def_id), | |
598 | deprecation: self.encode_deprecation(def_id), | |
599 | ||
600 | ty: Some(self.encode_item_type(def_id)), | |
601 | inherent_impls: LazySeq::empty(), | |
602 | variances: LazySeq::empty(), | |
603 | generics: Some(self.encode_generics(def_id)), | |
604 | predicates: Some(self.encode_predicates(def_id)), | |
605 | ||
606 | ast: None, | |
c30ab7b3 | 607 | mir: None, |
223e47cc LB |
608 | } |
609 | } | |
223e47cc | 610 | |
c30ab7b3 | 611 | fn encode_struct_ctor(&mut self, (adt_def_id, def_id): (DefId, DefId)) -> Entry<'tcx> { |
7cac9316 | 612 | debug!("IsolatedEncoder::encode_struct_ctor({:?})", def_id); |
c30ab7b3 | 613 | let tcx = self.tcx; |
abe05a73 XL |
614 | let adt_def = tcx.adt_def(adt_def_id); |
615 | let variant = adt_def.struct_variant(); | |
1a4d82fc | 616 | |
9e0c209e | 617 | let data = VariantData { |
c30ab7b3 | 618 | ctor_kind: variant.ctor_kind, |
8bb4bdeb | 619 | discr: variant.discr, |
c30ab7b3 | 620 | struct_ctor: Some(def_id.index), |
041b39d2 XL |
621 | ctor_sig: if variant.ctor_kind == CtorKind::Fn { |
622 | Some(self.lazy(&tcx.fn_sig(def_id))) | |
623 | } else { | |
624 | None | |
625 | } | |
9e0c209e | 626 | }; |
223e47cc | 627 | |
32a655c1 SL |
628 | let struct_id = tcx.hir.as_local_node_id(adt_def_id).unwrap(); |
629 | let struct_vis = &tcx.hir.expect_item(struct_id).vis; | |
8bb4bdeb XL |
630 | let mut ctor_vis = ty::Visibility::from_hir(struct_vis, struct_id, tcx); |
631 | for field in &variant.fields { | |
632 | if ctor_vis.is_at_least(field.vis, tcx) { | |
633 | ctor_vis = field.vis; | |
634 | } | |
635 | } | |
636 | ||
abe05a73 XL |
637 | // If the structure is marked as non_exhaustive then lower the visibility |
638 | // to within the crate. | |
639 | if adt_def.is_non_exhaustive() && ctor_vis == ty::Visibility::Public { | |
640 | ctor_vis = ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX)); | |
641 | } | |
642 | ||
8bb4bdeb | 643 | let repr_options = get_repr_options(&tcx, adt_def_id); |
c30ab7b3 | 644 | |
9e0c209e | 645 | Entry { |
8bb4bdeb XL |
646 | kind: EntryKind::Struct(self.lazy(&data), repr_options), |
647 | visibility: self.lazy(&ctor_vis), | |
476ff2be | 648 | span: self.lazy(&tcx.def_span(def_id)), |
9e0c209e SL |
649 | attributes: LazySeq::empty(), |
650 | children: LazySeq::empty(), | |
651 | stability: self.encode_stability(def_id), | |
652 | deprecation: self.encode_deprecation(def_id), | |
653 | ||
654 | ty: Some(self.encode_item_type(def_id)), | |
655 | inherent_impls: LazySeq::empty(), | |
041b39d2 XL |
656 | variances: if variant.ctor_kind == CtorKind::Fn { |
657 | self.encode_variances_of(def_id) | |
658 | } else { | |
659 | LazySeq::empty() | |
660 | }, | |
9e0c209e SL |
661 | generics: Some(self.encode_generics(def_id)), |
662 | predicates: Some(self.encode_predicates(def_id)), | |
663 | ||
664 | ast: None, | |
7cac9316 | 665 | mir: self.encode_optimized_mir(def_id), |
9e0c209e SL |
666 | } |
667 | } | |
1a4d82fc | 668 | |
8bb4bdeb | 669 | fn encode_generics(&mut self, def_id: DefId) -> Lazy<ty::Generics> { |
7cac9316 | 670 | debug!("IsolatedEncoder::encode_generics({:?})", def_id); |
9e0c209e | 671 | let tcx = self.tcx; |
7cac9316 | 672 | self.lazy(tcx.generics_of(def_id)) |
1a4d82fc JJ |
673 | } |
674 | ||
9e0c209e | 675 | fn encode_predicates(&mut self, def_id: DefId) -> Lazy<ty::GenericPredicates<'tcx>> { |
7cac9316 | 676 | debug!("IsolatedEncoder::encode_predicates({:?})", def_id); |
9e0c209e | 677 | let tcx = self.tcx; |
7cac9316 | 678 | self.lazy(&tcx.predicates_of(def_id)) |
1a4d82fc JJ |
679 | } |
680 | ||
9e0c209e | 681 | fn encode_info_for_trait_item(&mut self, def_id: DefId) -> Entry<'tcx> { |
7cac9316 | 682 | debug!("IsolatedEncoder::encode_info_for_trait_item({:?})", def_id); |
9e0c209e | 683 | let tcx = self.tcx; |
c34b1796 | 684 | |
32a655c1 SL |
685 | let node_id = tcx.hir.as_local_node_id(def_id).unwrap(); |
686 | let ast_item = tcx.hir.expect_trait_item(node_id); | |
476ff2be SL |
687 | let trait_item = tcx.associated_item(def_id); |
688 | ||
689 | let container = match trait_item.defaultness { | |
690 | hir::Defaultness::Default { has_value: true } => | |
691 | AssociatedContainer::TraitWithDefault, | |
692 | hir::Defaultness::Default { has_value: false } => | |
693 | AssociatedContainer::TraitRequired, | |
694 | hir::Defaultness::Final => | |
695 | span_bug!(ast_item.span, "traits cannot have final items"), | |
b039eaaf | 696 | }; |
1a4d82fc | 697 | |
476ff2be | 698 | let kind = match trait_item.kind { |
8bb4bdeb XL |
699 | ty::AssociatedKind::Const => { |
700 | EntryKind::AssociatedConst(container, 0) | |
701 | } | |
476ff2be | 702 | ty::AssociatedKind::Method => { |
32a655c1 SL |
703 | let fn_data = if let hir::TraitItemKind::Method(_, ref m) = ast_item.node { |
704 | let arg_names = match *m { | |
705 | hir::TraitMethod::Required(ref names) => { | |
706 | self.encode_fn_arg_names(names) | |
707 | } | |
708 | hir::TraitMethod::Provided(body) => { | |
709 | self.encode_fn_arg_names_for_body(body) | |
710 | } | |
711 | }; | |
9e0c209e SL |
712 | FnData { |
713 | constness: hir::Constness::NotConst, | |
3b2f2976 | 714 | arg_names, |
041b39d2 | 715 | sig: self.lazy(&tcx.fn_sig(def_id)), |
9e0c209e SL |
716 | } |
717 | } else { | |
718 | bug!() | |
719 | }; | |
476ff2be | 720 | EntryKind::Method(self.lazy(&MethodData { |
3b2f2976 XL |
721 | fn_data, |
722 | container, | |
476ff2be SL |
723 | has_self: trait_item.method_has_self_argument, |
724 | })) | |
9e0c209e | 725 | } |
476ff2be | 726 | ty::AssociatedKind::Type => EntryKind::AssociatedType(container), |
9e0c209e | 727 | }; |
1a4d82fc | 728 | |
9e0c209e | 729 | Entry { |
3b2f2976 | 730 | kind, |
32a655c1 | 731 | visibility: self.lazy(&trait_item.vis), |
476ff2be | 732 | span: self.lazy(&ast_item.span), |
9e0c209e SL |
733 | attributes: self.encode_attributes(&ast_item.attrs), |
734 | children: LazySeq::empty(), | |
735 | stability: self.encode_stability(def_id), | |
736 | deprecation: self.encode_deprecation(def_id), | |
737 | ||
476ff2be SL |
738 | ty: match trait_item.kind { |
739 | ty::AssociatedKind::Const | | |
740 | ty::AssociatedKind::Method => { | |
741 | Some(self.encode_item_type(def_id)) | |
742 | } | |
743 | ty::AssociatedKind::Type => { | |
744 | if trait_item.defaultness.has_value() { | |
745 | Some(self.encode_item_type(def_id)) | |
746 | } else { | |
747 | None | |
748 | } | |
9e0c209e SL |
749 | } |
750 | }, | |
751 | inherent_impls: LazySeq::empty(), | |
041b39d2 XL |
752 | variances: if trait_item.kind == ty::AssociatedKind::Method { |
753 | self.encode_variances_of(def_id) | |
754 | } else { | |
755 | LazySeq::empty() | |
756 | }, | |
9e0c209e SL |
757 | generics: Some(self.encode_generics(def_id)), |
758 | predicates: Some(self.encode_predicates(def_id)), | |
759 | ||
32a655c1 SL |
760 | ast: if let hir::TraitItemKind::Const(_, Some(body)) = ast_item.node { |
761 | Some(self.encode_body(body)) | |
9e0c209e SL |
762 | } else { |
763 | None | |
764 | }, | |
7cac9316 | 765 | mir: self.encode_optimized_mir(def_id), |
1a4d82fc | 766 | } |
1a4d82fc | 767 | } |
1a4d82fc | 768 | |
9e0c209e | 769 | fn encode_info_for_impl_item(&mut self, def_id: DefId) -> Entry<'tcx> { |
7cac9316 | 770 | debug!("IsolatedEncoder::encode_info_for_impl_item({:?})", def_id); |
041b39d2 XL |
771 | let tcx = self.tcx; |
772 | ||
32a655c1 SL |
773 | let node_id = self.tcx.hir.as_local_node_id(def_id).unwrap(); |
774 | let ast_item = self.tcx.hir.expect_impl_item(node_id); | |
476ff2be | 775 | let impl_item = self.tcx.associated_item(def_id); |
d9579d0f | 776 | |
476ff2be SL |
777 | let container = match impl_item.defaultness { |
778 | hir::Defaultness::Default { has_value: true } => AssociatedContainer::ImplDefault, | |
c30ab7b3 | 779 | hir::Defaultness::Final => AssociatedContainer::ImplFinal, |
476ff2be SL |
780 | hir::Defaultness::Default { has_value: false } => |
781 | span_bug!(ast_item.span, "impl items always have values (currently)"), | |
9e0c209e | 782 | }; |
d9579d0f | 783 | |
476ff2be | 784 | let kind = match impl_item.kind { |
8bb4bdeb XL |
785 | ty::AssociatedKind::Const => { |
786 | EntryKind::AssociatedConst(container, | |
ea8adc8c | 787 | self.tcx.at(ast_item.span).mir_const_qualif(def_id).0) |
8bb4bdeb | 788 | } |
476ff2be | 789 | ty::AssociatedKind::Method => { |
32a655c1 | 790 | let fn_data = if let hir::ImplItemKind::Method(ref sig, body) = ast_item.node { |
9e0c209e SL |
791 | FnData { |
792 | constness: sig.constness, | |
32a655c1 | 793 | arg_names: self.encode_fn_arg_names_for_body(body), |
041b39d2 | 794 | sig: self.lazy(&tcx.fn_sig(def_id)), |
9e0c209e SL |
795 | } |
796 | } else { | |
797 | bug!() | |
798 | }; | |
476ff2be | 799 | EntryKind::Method(self.lazy(&MethodData { |
3b2f2976 XL |
800 | fn_data, |
801 | container, | |
476ff2be SL |
802 | has_self: impl_item.method_has_self_argument, |
803 | })) | |
9e0c209e | 804 | } |
476ff2be | 805 | ty::AssociatedKind::Type => EntryKind::AssociatedType(container) |
9e0c209e | 806 | }; |
223e47cc | 807 | |
32a655c1 SL |
808 | let (ast, mir) = if let hir::ImplItemKind::Const(_, body) = ast_item.node { |
809 | (Some(body), true) | |
810 | } else if let hir::ImplItemKind::Method(ref sig, body) = ast_item.node { | |
7cac9316 | 811 | let generics = self.tcx.generics_of(def_id); |
9e0c209e SL |
812 | let types = generics.parent_types as usize + generics.types.len(); |
813 | let needs_inline = types > 0 || attr::requests_inline(&ast_item.attrs); | |
814 | let is_const_fn = sig.constness == hir::Constness::Const; | |
32a655c1 | 815 | let ast = if is_const_fn { Some(body) } else { None }; |
476ff2be | 816 | let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir; |
32a655c1 | 817 | (ast, needs_inline || is_const_fn || always_encode_mir) |
c34b1796 | 818 | } else { |
32a655c1 | 819 | (None, false) |
9e0c209e SL |
820 | }; |
821 | ||
822 | Entry { | |
3b2f2976 | 823 | kind, |
32a655c1 | 824 | visibility: self.lazy(&impl_item.vis), |
476ff2be | 825 | span: self.lazy(&ast_item.span), |
9e0c209e SL |
826 | attributes: self.encode_attributes(&ast_item.attrs), |
827 | children: LazySeq::empty(), | |
828 | stability: self.encode_stability(def_id), | |
829 | deprecation: self.encode_deprecation(def_id), | |
830 | ||
476ff2be | 831 | ty: Some(self.encode_item_type(def_id)), |
9e0c209e | 832 | inherent_impls: LazySeq::empty(), |
041b39d2 XL |
833 | variances: if impl_item.kind == ty::AssociatedKind::Method { |
834 | self.encode_variances_of(def_id) | |
835 | } else { | |
836 | LazySeq::empty() | |
837 | }, | |
9e0c209e SL |
838 | generics: Some(self.encode_generics(def_id)), |
839 | predicates: Some(self.encode_predicates(def_id)), | |
840 | ||
32a655c1 | 841 | ast: ast.map(|body| self.encode_body(body)), |
7cac9316 | 842 | mir: if mir { self.encode_optimized_mir(def_id) } else { None }, |
1a4d82fc | 843 | } |
223e47cc | 844 | } |
1a4d82fc | 845 | |
32a655c1 SL |
846 | fn encode_fn_arg_names_for_body(&mut self, body_id: hir::BodyId) |
847 | -> LazySeq<ast::Name> { | |
848 | let _ignore = self.tcx.dep_graph.in_ignore(); | |
849 | let body = self.tcx.hir.body(body_id); | |
850 | self.lazy_seq(body.arguments.iter().map(|arg| { | |
851 | match arg.pat.node { | |
852 | PatKind::Binding(_, _, name, _) => name.node, | |
853 | _ => Symbol::intern("") | |
9e0c209e SL |
854 | } |
855 | })) | |
1a4d82fc | 856 | } |
1a4d82fc | 857 | |
32a655c1 SL |
858 | fn encode_fn_arg_names(&mut self, names: &[Spanned<ast::Name>]) |
859 | -> LazySeq<ast::Name> { | |
860 | self.lazy_seq(names.iter().map(|name| name.node)) | |
861 | } | |
862 | ||
7cac9316 | 863 | fn encode_optimized_mir(&mut self, def_id: DefId) -> Option<Lazy<mir::Mir<'tcx>>> { |
cc61c64b | 864 | debug!("EntryBuilder::encode_mir({:?})", def_id); |
7cac9316 XL |
865 | if self.tcx.mir_keys(LOCAL_CRATE).contains(&def_id) { |
866 | let mir = self.tcx.optimized_mir(def_id); | |
867 | Some(self.lazy(&mir)) | |
868 | } else { | |
869 | None | |
870 | } | |
9cc50fc6 | 871 | } |
223e47cc | 872 | |
9e0c209e SL |
873 | // Encodes the inherent implementations of a structure, enumeration, or trait. |
874 | fn encode_inherent_implementations(&mut self, def_id: DefId) -> LazySeq<DefIndex> { | |
7cac9316 XL |
875 | debug!("IsolatedEncoder::encode_inherent_implementations({:?})", def_id); |
876 | let implementations = self.tcx.inherent_impls(def_id); | |
877 | if implementations.is_empty() { | |
878 | LazySeq::empty() | |
879 | } else { | |
880 | self.lazy_seq(implementations.iter().map(|&def_id| { | |
881 | assert!(def_id.is_local()); | |
882 | def_id.index | |
883 | })) | |
1a4d82fc JJ |
884 | } |
885 | } | |
223e47cc | 886 | |
9e0c209e | 887 | fn encode_stability(&mut self, def_id: DefId) -> Option<Lazy<attr::Stability>> { |
7cac9316 | 888 | debug!("IsolatedEncoder::encode_stability({:?})", def_id); |
9e0c209e SL |
889 | self.tcx.lookup_stability(def_id).map(|stab| self.lazy(stab)) |
890 | } | |
1a4d82fc | 891 | |
9e0c209e | 892 | fn encode_deprecation(&mut self, def_id: DefId) -> Option<Lazy<attr::Deprecation>> { |
7cac9316 | 893 | debug!("IsolatedEncoder::encode_deprecation({:?})", def_id); |
9e0c209e SL |
894 | self.tcx.lookup_deprecation(def_id).map(|depr| self.lazy(&depr)) |
895 | } | |
9cc50fc6 | 896 | |
476ff2be | 897 | fn encode_info_for_item(&mut self, (def_id, item): (DefId, &'tcx hir::Item)) -> Entry<'tcx> { |
9e0c209e SL |
898 | let tcx = self.tcx; |
899 | ||
7cac9316 | 900 | debug!("IsolatedEncoder::encode_info_for_item({:?})", def_id); |
9e0c209e SL |
901 | |
902 | let kind = match item.node { | |
903 | hir::ItemStatic(_, hir::MutMutable, _) => EntryKind::MutStatic, | |
904 | hir::ItemStatic(_, hir::MutImmutable, _) => EntryKind::ImmStatic, | |
8bb4bdeb | 905 | hir::ItemConst(..) => { |
ea8adc8c | 906 | EntryKind::Const(tcx.at(item.span).mir_const_qualif(def_id).0) |
8bb4bdeb | 907 | } |
32a655c1 | 908 | hir::ItemFn(_, _, constness, .., body) => { |
9e0c209e | 909 | let data = FnData { |
3b2f2976 | 910 | constness, |
32a655c1 | 911 | arg_names: self.encode_fn_arg_names_for_body(body), |
041b39d2 | 912 | sig: self.lazy(&tcx.fn_sig(def_id)), |
9e0c209e | 913 | }; |
54a0048b | 914 | |
9e0c209e | 915 | EntryKind::Fn(self.lazy(&data)) |
b039eaaf | 916 | } |
9e0c209e SL |
917 | hir::ItemMod(ref m) => { |
918 | return self.encode_info_for_mod(FromId(item.id, (m, &item.attrs, &item.vis))); | |
919 | } | |
920 | hir::ItemForeignMod(_) => EntryKind::ForeignMod, | |
cc61c64b | 921 | hir::ItemGlobalAsm(..) => EntryKind::GlobalAsm, |
9e0c209e | 922 | hir::ItemTy(..) => EntryKind::Type, |
8bb4bdeb | 923 | hir::ItemEnum(..) => EntryKind::Enum(get_repr_options(&tcx, def_id)), |
9e0c209e | 924 | hir::ItemStruct(ref struct_def, _) => { |
7cac9316 | 925 | let variant = tcx.adt_def(def_id).struct_variant(); |
9e0c209e | 926 | |
c30ab7b3 SL |
927 | // Encode def_ids for each field and method |
928 | // for methods, write all the stuff get_trait_method | |
929 | // needs to know | |
9e0c209e | 930 | let struct_ctor = if !struct_def.is_struct() { |
32a655c1 | 931 | Some(tcx.hir.local_def_id(struct_def.id()).index) |
9e0c209e SL |
932 | } else { |
933 | None | |
934 | }; | |
8bb4bdeb XL |
935 | |
936 | let repr_options = get_repr_options(&tcx, def_id); | |
937 | ||
9e0c209e | 938 | EntryKind::Struct(self.lazy(&VariantData { |
c30ab7b3 | 939 | ctor_kind: variant.ctor_kind, |
8bb4bdeb | 940 | discr: variant.discr, |
3b2f2976 | 941 | struct_ctor, |
041b39d2 | 942 | ctor_sig: None, |
8bb4bdeb | 943 | }), repr_options) |
9e0c209e SL |
944 | } |
945 | hir::ItemUnion(..) => { | |
7cac9316 | 946 | let variant = tcx.adt_def(def_id).struct_variant(); |
8bb4bdeb | 947 | let repr_options = get_repr_options(&tcx, def_id); |
9e0c209e SL |
948 | |
949 | EntryKind::Union(self.lazy(&VariantData { | |
c30ab7b3 | 950 | ctor_kind: variant.ctor_kind, |
8bb4bdeb | 951 | discr: variant.discr, |
c30ab7b3 | 952 | struct_ctor: None, |
041b39d2 | 953 | ctor_sig: None, |
8bb4bdeb | 954 | }), repr_options) |
9e0c209e | 955 | } |
abe05a73 | 956 | hir::ItemAutoImpl(..) => { |
9e0c209e SL |
957 | let data = ImplData { |
958 | polarity: hir::ImplPolarity::Positive, | |
7cac9316 | 959 | defaultness: hir::Defaultness::Final, |
9e0c209e | 960 | parent_impl: None, |
cc61c64b | 961 | coerce_unsized_info: None, |
c30ab7b3 | 962 | trait_ref: tcx.impl_trait_ref(def_id).map(|trait_ref| self.lazy(&trait_ref)), |
9e0c209e | 963 | }; |
b039eaaf | 964 | |
abe05a73 | 965 | EntryKind::AutoImpl(self.lazy(&data)) |
9e0c209e | 966 | } |
7cac9316 | 967 | hir::ItemImpl(_, polarity, defaultness, ..) => { |
9e0c209e SL |
968 | let trait_ref = tcx.impl_trait_ref(def_id); |
969 | let parent = if let Some(trait_ref) = trait_ref { | |
7cac9316 XL |
970 | let trait_def = tcx.trait_def(trait_ref.def_id); |
971 | trait_def.ancestors(tcx, def_id).skip(1).next().and_then(|node| { | |
9e0c209e SL |
972 | match node { |
973 | specialization_graph::Node::Impl(parent) => Some(parent), | |
974 | _ => None, | |
975 | } | |
976 | }) | |
977 | } else { | |
978 | None | |
979 | }; | |
b039eaaf | 980 | |
cc61c64b XL |
981 | // if this is an impl of `CoerceUnsized`, create its |
982 | // "unsized info", else just store None | |
983 | let coerce_unsized_info = | |
984 | trait_ref.and_then(|t| { | |
ea8adc8c | 985 | if Some(t.def_id) == tcx.lang_items().coerce_unsized_trait() { |
7cac9316 | 986 | Some(tcx.at(item.span).coerce_unsized_info(def_id)) |
cc61c64b XL |
987 | } else { |
988 | None | |
989 | } | |
990 | }); | |
991 | ||
9e0c209e | 992 | let data = ImplData { |
3b2f2976 XL |
993 | polarity, |
994 | defaultness, | |
9e0c209e | 995 | parent_impl: parent, |
3b2f2976 | 996 | coerce_unsized_info, |
c30ab7b3 | 997 | trait_ref: trait_ref.map(|trait_ref| self.lazy(&trait_ref)), |
9e0c209e | 998 | }; |
1a4d82fc | 999 | |
9e0c209e SL |
1000 | EntryKind::Impl(self.lazy(&data)) |
1001 | } | |
1002 | hir::ItemTrait(..) => { | |
7cac9316 | 1003 | let trait_def = tcx.trait_def(def_id); |
9e0c209e SL |
1004 | let data = TraitData { |
1005 | unsafety: trait_def.unsafety, | |
1006 | paren_sugar: trait_def.paren_sugar, | |
abe05a73 | 1007 | has_auto_impl: tcx.trait_is_auto(def_id), |
7cac9316 | 1008 | super_predicates: self.lazy(&tcx.super_predicates_of(def_id)), |
9e0c209e | 1009 | }; |
b039eaaf | 1010 | |
9e0c209e | 1011 | EntryKind::Trait(self.lazy(&data)) |
d9579d0f | 1012 | } |
c30ab7b3 | 1013 | hir::ItemExternCrate(_) | |
476ff2be | 1014 | hir::ItemUse(..) => bug!("cannot encode info for item {:?}", item), |
9e0c209e | 1015 | }; |
d9579d0f | 1016 | |
9e0c209e | 1017 | Entry { |
3b2f2976 | 1018 | kind, |
32a655c1 | 1019 | visibility: self.lazy(&ty::Visibility::from_hir(&item.vis, item.id, tcx)), |
476ff2be | 1020 | span: self.lazy(&item.span), |
9e0c209e SL |
1021 | attributes: self.encode_attributes(&item.attrs), |
1022 | children: match item.node { | |
1023 | hir::ItemForeignMod(ref fm) => { | |
c30ab7b3 SL |
1024 | self.lazy_seq(fm.items |
1025 | .iter() | |
32a655c1 | 1026 | .map(|foreign_item| tcx.hir.local_def_id(foreign_item.id).index)) |
d9579d0f | 1027 | } |
9e0c209e | 1028 | hir::ItemEnum(..) => { |
7cac9316 | 1029 | let def = self.tcx.adt_def(def_id); |
9e0c209e SL |
1030 | self.lazy_seq(def.variants.iter().map(|v| { |
1031 | assert!(v.did.is_local()); | |
1032 | v.did.index | |
1033 | })) | |
1a4d82fc | 1034 | } |
9e0c209e SL |
1035 | hir::ItemStruct(..) | |
1036 | hir::ItemUnion(..) => { | |
7cac9316 | 1037 | let def = self.tcx.adt_def(def_id); |
9e0c209e SL |
1038 | self.lazy_seq(def.struct_variant().fields.iter().map(|f| { |
1039 | assert!(f.did.is_local()); | |
1040 | f.did.index | |
1041 | })) | |
1a4d82fc | 1042 | } |
9e0c209e SL |
1043 | hir::ItemImpl(..) | |
1044 | hir::ItemTrait(..) => { | |
476ff2be | 1045 | self.lazy_seq(tcx.associated_item_def_ids(def_id).iter().map(|&def_id| { |
9e0c209e SL |
1046 | assert!(def_id.is_local()); |
1047 | def_id.index | |
1048 | })) | |
d9579d0f | 1049 | } |
c30ab7b3 | 1050 | _ => LazySeq::empty(), |
9e0c209e SL |
1051 | }, |
1052 | stability: self.encode_stability(def_id), | |
1053 | deprecation: self.encode_deprecation(def_id), | |
1054 | ||
1055 | ty: match item.node { | |
1056 | hir::ItemStatic(..) | | |
1057 | hir::ItemConst(..) | | |
1058 | hir::ItemFn(..) | | |
1059 | hir::ItemTy(..) | | |
1060 | hir::ItemEnum(..) | | |
1061 | hir::ItemStruct(..) | | |
1062 | hir::ItemUnion(..) | | |
c30ab7b3 SL |
1063 | hir::ItemImpl(..) => Some(self.encode_item_type(def_id)), |
1064 | _ => None, | |
9e0c209e SL |
1065 | }, |
1066 | inherent_impls: self.encode_inherent_implementations(def_id), | |
1067 | variances: match item.node { | |
1068 | hir::ItemEnum(..) | | |
1069 | hir::ItemStruct(..) | | |
1070 | hir::ItemUnion(..) | | |
041b39d2 | 1071 | hir::ItemFn(..) => self.encode_variances_of(def_id), |
c30ab7b3 | 1072 | _ => LazySeq::empty(), |
9e0c209e SL |
1073 | }, |
1074 | generics: match item.node { | |
1075 | hir::ItemStatic(..) | | |
1076 | hir::ItemConst(..) | | |
1077 | hir::ItemFn(..) | | |
1078 | hir::ItemTy(..) | | |
1079 | hir::ItemEnum(..) | | |
1080 | hir::ItemStruct(..) | | |
1081 | hir::ItemUnion(..) | | |
1082 | hir::ItemImpl(..) | | |
c30ab7b3 SL |
1083 | hir::ItemTrait(..) => Some(self.encode_generics(def_id)), |
1084 | _ => None, | |
9e0c209e SL |
1085 | }, |
1086 | predicates: match item.node { | |
1087 | hir::ItemStatic(..) | | |
1088 | hir::ItemConst(..) | | |
1089 | hir::ItemFn(..) | | |
1090 | hir::ItemTy(..) | | |
1091 | hir::ItemEnum(..) | | |
1092 | hir::ItemStruct(..) | | |
1093 | hir::ItemUnion(..) | | |
1094 | hir::ItemImpl(..) | | |
c30ab7b3 SL |
1095 | hir::ItemTrait(..) => Some(self.encode_predicates(def_id)), |
1096 | _ => None, | |
9e0c209e SL |
1097 | }, |
1098 | ||
1099 | ast: match item.node { | |
32a655c1 SL |
1100 | hir::ItemConst(_, body) | |
1101 | hir::ItemFn(_, _, hir::Constness::Const, _, _, body) => { | |
1102 | Some(self.encode_body(body)) | |
d9579d0f | 1103 | } |
c30ab7b3 | 1104 | _ => None, |
9e0c209e SL |
1105 | }, |
1106 | mir: match item.node { | |
32a655c1 | 1107 | hir::ItemStatic(..) if self.tcx.sess.opts.debugging_opts.always_encode_mir => { |
7cac9316 | 1108 | self.encode_optimized_mir(def_id) |
32a655c1 | 1109 | } |
7cac9316 | 1110 | hir::ItemConst(..) => self.encode_optimized_mir(def_id), |
9e0c209e SL |
1111 | hir::ItemFn(_, _, constness, _, ref generics, _) => { |
1112 | let tps_len = generics.ty_params.len(); | |
1113 | let needs_inline = tps_len > 0 || attr::requests_inline(&item.attrs); | |
476ff2be SL |
1114 | let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir; |
1115 | if needs_inline || constness == hir::Constness::Const || always_encode_mir { | |
7cac9316 | 1116 | self.encode_optimized_mir(def_id) |
9e0c209e SL |
1117 | } else { |
1118 | None | |
1119 | } | |
1a4d82fc | 1120 | } |
c30ab7b3 SL |
1121 | _ => None, |
1122 | }, | |
223e47cc | 1123 | } |
9e0c209e | 1124 | } |
476ff2be SL |
1125 | |
1126 | /// Serialize the text of exported macros | |
1127 | fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef) -> Entry<'tcx> { | |
8bb4bdeb | 1128 | use syntax::print::pprust; |
041b39d2 | 1129 | let def_id = self.tcx.hir.local_def_id(macro_def.id); |
476ff2be SL |
1130 | Entry { |
1131 | kind: EntryKind::MacroDef(self.lazy(&MacroDef { | |
8bb4bdeb | 1132 | body: pprust::tts_to_string(¯o_def.body.trees().collect::<Vec<_>>()), |
7cac9316 | 1133 | legacy: macro_def.legacy, |
476ff2be | 1134 | })), |
32a655c1 | 1135 | visibility: self.lazy(&ty::Visibility::Public), |
476ff2be | 1136 | span: self.lazy(¯o_def.span), |
476ff2be | 1137 | attributes: self.encode_attributes(¯o_def.attrs), |
041b39d2 XL |
1138 | stability: self.encode_stability(def_id), |
1139 | deprecation: self.encode_deprecation(def_id), | |
1140 | ||
476ff2be | 1141 | children: LazySeq::empty(), |
476ff2be SL |
1142 | ty: None, |
1143 | inherent_impls: LazySeq::empty(), | |
1144 | variances: LazySeq::empty(), | |
1145 | generics: None, | |
1146 | predicates: None, | |
1147 | ast: None, | |
1148 | mir: None, | |
1149 | } | |
1150 | } | |
223e47cc | 1151 | |
7cac9316 XL |
1152 | fn encode_info_for_ty_param(&mut self, |
1153 | (def_id, Untracked(has_default)): (DefId, Untracked<bool>)) | |
1154 | -> Entry<'tcx> { | |
1155 | debug!("IsolatedEncoder::encode_info_for_ty_param({:?})", def_id); | |
1156 | let tcx = self.tcx; | |
1157 | Entry { | |
1158 | kind: EntryKind::Type, | |
1159 | visibility: self.lazy(&ty::Visibility::Public), | |
1160 | span: self.lazy(&tcx.def_span(def_id)), | |
1161 | attributes: LazySeq::empty(), | |
1162 | children: LazySeq::empty(), | |
1163 | stability: None, | |
1164 | deprecation: None, | |
9e0c209e | 1165 | |
7cac9316 XL |
1166 | ty: if has_default { |
1167 | Some(self.encode_item_type(def_id)) | |
1168 | } else { | |
1169 | None | |
1170 | }, | |
1171 | inherent_impls: LazySeq::empty(), | |
1172 | variances: LazySeq::empty(), | |
1173 | generics: None, | |
1174 | predicates: None, | |
970d7e83 | 1175 | |
7cac9316 XL |
1176 | ast: None, |
1177 | mir: None, | |
223e47cc | 1178 | } |
223e47cc | 1179 | } |
223e47cc | 1180 | |
7cac9316 XL |
1181 | fn encode_info_for_anon_ty(&mut self, def_id: DefId) -> Entry<'tcx> { |
1182 | debug!("IsolatedEncoder::encode_info_for_anon_ty({:?})", def_id); | |
9e0c209e | 1183 | let tcx = self.tcx; |
7cac9316 XL |
1184 | Entry { |
1185 | kind: EntryKind::Type, | |
1186 | visibility: self.lazy(&ty::Visibility::Public), | |
1187 | span: self.lazy(&tcx.def_span(def_id)), | |
1188 | attributes: LazySeq::empty(), | |
1189 | children: LazySeq::empty(), | |
1190 | stability: None, | |
1191 | deprecation: None, | |
b039eaaf | 1192 | |
7cac9316 XL |
1193 | ty: Some(self.encode_item_type(def_id)), |
1194 | inherent_impls: LazySeq::empty(), | |
1195 | variances: LazySeq::empty(), | |
1196 | generics: Some(self.encode_generics(def_id)), | |
1197 | predicates: Some(self.encode_predicates(def_id)), | |
9e0c209e SL |
1198 | |
1199 | ast: None, | |
c30ab7b3 | 1200 | mir: None, |
9e0c209e | 1201 | } |
223e47cc | 1202 | } |
223e47cc | 1203 | |
9e0c209e | 1204 | fn encode_info_for_closure(&mut self, def_id: DefId) -> Entry<'tcx> { |
7cac9316 | 1205 | debug!("IsolatedEncoder::encode_info_for_closure({:?})", def_id); |
9e0c209e | 1206 | let tcx = self.tcx; |
223e47cc | 1207 | |
ea8adc8c XL |
1208 | let kind = if let Some(sig) = self.tcx.generator_sig(def_id) { |
1209 | let layout = self.tcx.generator_layout(def_id); | |
1210 | let data = GeneratorData { | |
1211 | sig, | |
1212 | layout: layout.clone(), | |
1213 | }; | |
1214 | EntryKind::Generator(self.lazy(&data)) | |
1215 | } else { | |
1216 | let data = ClosureData { | |
1217 | kind: tcx.closure_kind(def_id), | |
1218 | sig: self.lazy(&tcx.fn_sig(def_id)), | |
1219 | }; | |
1220 | EntryKind::Closure(self.lazy(&data)) | |
9e0c209e | 1221 | }; |
223e47cc | 1222 | |
9e0c209e | 1223 | Entry { |
ea8adc8c | 1224 | kind, |
32a655c1 | 1225 | visibility: self.lazy(&ty::Visibility::Public), |
476ff2be | 1226 | span: self.lazy(&tcx.def_span(def_id)), |
9e0c209e SL |
1227 | attributes: self.encode_attributes(&tcx.get_attrs(def_id)), |
1228 | children: LazySeq::empty(), | |
1229 | stability: None, | |
1230 | deprecation: None, | |
1231 | ||
476ff2be | 1232 | ty: Some(self.encode_item_type(def_id)), |
9e0c209e SL |
1233 | inherent_impls: LazySeq::empty(), |
1234 | variances: LazySeq::empty(), | |
476ff2be | 1235 | generics: Some(self.encode_generics(def_id)), |
9e0c209e SL |
1236 | predicates: None, |
1237 | ||
1238 | ast: None, | |
7cac9316 | 1239 | mir: self.encode_optimized_mir(def_id), |
223e47cc | 1240 | } |
223e47cc LB |
1241 | } |
1242 | ||
cc61c64b | 1243 | fn encode_info_for_embedded_const(&mut self, def_id: DefId) -> Entry<'tcx> { |
7cac9316 | 1244 | debug!("IsolatedEncoder::encode_info_for_embedded_const({:?})", def_id); |
cc61c64b XL |
1245 | let tcx = self.tcx; |
1246 | let id = tcx.hir.as_local_node_id(def_id).unwrap(); | |
1247 | let body = tcx.hir.body_owned_by(id); | |
1248 | ||
1249 | Entry { | |
ea8adc8c | 1250 | kind: EntryKind::Const(tcx.mir_const_qualif(def_id).0), |
cc61c64b XL |
1251 | visibility: self.lazy(&ty::Visibility::Public), |
1252 | span: self.lazy(&tcx.def_span(def_id)), | |
1253 | attributes: LazySeq::empty(), | |
1254 | children: LazySeq::empty(), | |
1255 | stability: None, | |
1256 | deprecation: None, | |
1257 | ||
1258 | ty: Some(self.encode_item_type(def_id)), | |
1259 | inherent_impls: LazySeq::empty(), | |
1260 | variances: LazySeq::empty(), | |
1261 | generics: Some(self.encode_generics(def_id)), | |
1262 | predicates: Some(self.encode_predicates(def_id)), | |
1263 | ||
1264 | ast: Some(self.encode_body(body)), | |
7cac9316 | 1265 | mir: self.encode_optimized_mir(def_id), |
cc61c64b XL |
1266 | } |
1267 | } | |
1268 | ||
1269 | fn encode_attributes(&mut self, attrs: &[ast::Attribute]) -> LazySeq<ast::Attribute> { | |
1270 | // NOTE: This must use lazy_seq_from_slice(), not lazy_seq() because | |
7cac9316 | 1271 | // we rely on the HashStable specialization for [Attribute] |
cc61c64b XL |
1272 | // to properly filter things out. |
1273 | self.lazy_seq_from_slice(attrs) | |
1274 | } | |
cc61c64b | 1275 | |
7cac9316 | 1276 | fn encode_native_libraries(&mut self, _: ()) -> LazySeq<NativeLibrary> { |
ea8adc8c XL |
1277 | let used_libraries = self.tcx.native_libraries(LOCAL_CRATE); |
1278 | self.lazy_seq(used_libraries.iter().cloned()) | |
1a4d82fc | 1279 | } |
1a4d82fc | 1280 | |
7cac9316 | 1281 | fn encode_crate_deps(&mut self, _: ()) -> LazySeq<CrateDep> { |
ea8adc8c | 1282 | let crates = self.tcx.crates(); |
9e0c209e | 1283 | |
7cac9316 XL |
1284 | let mut deps = crates |
1285 | .iter() | |
1286 | .map(|&cnum| { | |
1287 | let dep = CrateDep { | |
ea8adc8c XL |
1288 | name: self.tcx.original_crate_name(cnum), |
1289 | hash: self.tcx.crate_hash(cnum), | |
1290 | kind: self.tcx.dep_kind(cnum), | |
7cac9316 XL |
1291 | }; |
1292 | (cnum, dep) | |
1293 | }) | |
1294 | .collect::<Vec<_>>(); | |
1295 | ||
1296 | deps.sort_by_key(|&(cnum, _)| cnum); | |
9e0c209e | 1297 | |
7cac9316 | 1298 | { |
9e0c209e SL |
1299 | // Sanity-check the crate numbers |
1300 | let mut expected_cnum = 1; | |
1301 | for &(n, _) in &deps { | |
1302 | assert_eq!(n, CrateNum::new(expected_cnum)); | |
1303 | expected_cnum += 1; | |
1a4d82fc JJ |
1304 | } |
1305 | } | |
1a4d82fc | 1306 | |
9e0c209e SL |
1307 | // We're just going to write a list of crate 'name-hash-version's, with |
1308 | // the assumption that they are numbered 1 to n. | |
1309 | // FIXME (#2166): This is not nearly enough to support correct versioning | |
1310 | // but is enough to get transitive crate dependencies working. | |
7cac9316 | 1311 | self.lazy_seq_ref(deps.iter().map(|&(_, ref dep)| dep)) |
223e47cc | 1312 | } |
1a4d82fc | 1313 | |
7cac9316 | 1314 | fn encode_lang_items(&mut self, _: ()) -> LazySeq<(DefIndex, usize)> { |
9e0c209e | 1315 | let tcx = self.tcx; |
ea8adc8c XL |
1316 | let lang_items = tcx.lang_items(); |
1317 | let lang_items = lang_items.items().iter(); | |
7cac9316 | 1318 | self.lazy_seq(lang_items.enumerate().filter_map(|(i, &opt_def_id)| { |
9e0c209e SL |
1319 | if let Some(def_id) = opt_def_id { |
1320 | if def_id.is_local() { | |
1321 | return Some((def_id.index, i)); | |
1322 | } | |
1a4d82fc | 1323 | } |
9e0c209e | 1324 | None |
7cac9316 | 1325 | })) |
1a4d82fc | 1326 | } |
476ff2be | 1327 | |
7cac9316 XL |
1328 | fn encode_lang_items_missing(&mut self, _: ()) -> LazySeq<lang_items::LangItem> { |
1329 | let tcx = self.tcx; | |
ea8adc8c | 1330 | self.lazy_seq_ref(&tcx.lang_items().missing) |
476ff2be | 1331 | } |
1a4d82fc | 1332 | |
9e0c209e | 1333 | /// Encodes an index, mapping each trait to its (local) implementations. |
7cac9316 XL |
1334 | fn encode_impls(&mut self, _: ()) -> LazySeq<TraitImpls> { |
1335 | debug!("IsolatedEncoder::encode_impls()"); | |
1336 | let tcx = self.tcx; | |
9e0c209e | 1337 | let mut visitor = ImplVisitor { |
3b2f2976 | 1338 | tcx, |
476ff2be | 1339 | impls: FxHashMap(), |
9e0c209e | 1340 | }; |
7cac9316 | 1341 | tcx.hir.krate().visit_all_item_likes(&mut visitor); |
1a4d82fc | 1342 | |
7cac9316 XL |
1343 | let mut all_impls: Vec<_> = visitor.impls.into_iter().collect(); |
1344 | ||
1345 | // Bring everything into deterministic order for hashing | |
1346 | all_impls.sort_unstable_by_key(|&(trait_def_id, _)| { | |
1347 | tcx.def_path_hash(trait_def_id) | |
1348 | }); | |
1349 | ||
1350 | let all_impls: Vec<_> = all_impls | |
c30ab7b3 | 1351 | .into_iter() |
7cac9316 XL |
1352 | .map(|(trait_def_id, mut impls)| { |
1353 | // Bring everything into deterministic order for hashing | |
1354 | impls.sort_unstable_by_key(|&def_index| { | |
1355 | tcx.hir.definitions().def_path_hash(def_index) | |
1356 | }); | |
1357 | ||
c30ab7b3 SL |
1358 | TraitImpls { |
1359 | trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index), | |
7cac9316 | 1360 | impls: self.lazy_seq_from_slice(&impls[..]), |
c30ab7b3 SL |
1361 | } |
1362 | }) | |
1363 | .collect(); | |
1a4d82fc | 1364 | |
7cac9316 | 1365 | self.lazy_seq_from_slice(&all_impls[..]) |
1a4d82fc | 1366 | } |
970d7e83 | 1367 | |
476ff2be | 1368 | // Encodes all symbols exported from this crate into the metadata. |
9e0c209e SL |
1369 | // |
1370 | // This pass is seeded off the reachability list calculated in the | |
1371 | // middle::reachable module but filters out items that either don't have a | |
1372 | // symbol associated with them (they weren't translated) or if they're an FFI | |
1373 | // definition (as that's not defined in this crate). | |
7cac9316 | 1374 | fn encode_exported_symbols(&mut self, exported_symbols: &NodeSet) -> LazySeq<DefIndex> { |
9e0c209e | 1375 | let tcx = self.tcx; |
32a655c1 | 1376 | self.lazy_seq(exported_symbols.iter().map(|&id| tcx.hir.local_def_id(id).index)) |
1a4d82fc | 1377 | } |
223e47cc | 1378 | |
7cac9316 | 1379 | fn encode_dylib_dependency_formats(&mut self, _: ()) -> LazySeq<Option<LinkagePreference>> { |
9e0c209e SL |
1380 | match self.tcx.sess.dependency_formats.borrow().get(&config::CrateTypeDylib) { |
1381 | Some(arr) => { | |
1382 | self.lazy_seq(arr.iter().map(|slot| { | |
1383 | match *slot { | |
1384 | Linkage::NotLinked | | |
1385 | Linkage::IncludedFromDylib => None, | |
1386 | ||
1387 | Linkage::Dynamic => Some(LinkagePreference::RequireDynamic), | |
1388 | Linkage::Static => Some(LinkagePreference::RequireStatic), | |
1389 | } | |
1390 | })) | |
1391 | } | |
c30ab7b3 | 1392 | None => LazySeq::empty(), |
a7813a04 XL |
1393 | } |
1394 | } | |
1a4d82fc | 1395 | |
7cac9316 XL |
1396 | fn encode_info_for_foreign_item(&mut self, |
1397 | (def_id, nitem): (DefId, &hir::ForeignItem)) | |
1398 | -> Entry<'tcx> { | |
1399 | let tcx = self.tcx; | |
9e0c209e | 1400 | |
7cac9316 | 1401 | debug!("IsolatedEncoder::encode_info_for_foreign_item({:?})", def_id); |
9e0c209e | 1402 | |
7cac9316 XL |
1403 | let kind = match nitem.node { |
1404 | hir::ForeignItemFn(_, ref names, _) => { | |
1405 | let data = FnData { | |
1406 | constness: hir::Constness::NotConst, | |
1407 | arg_names: self.encode_fn_arg_names(names), | |
041b39d2 | 1408 | sig: self.lazy(&tcx.fn_sig(def_id)), |
7cac9316 XL |
1409 | }; |
1410 | EntryKind::ForeignFn(self.lazy(&data)) | |
1411 | } | |
1412 | hir::ForeignItemStatic(_, true) => EntryKind::ForeignMutStatic, | |
1413 | hir::ForeignItemStatic(_, false) => EntryKind::ForeignImmStatic, | |
abe05a73 | 1414 | hir::ForeignItemType => EntryKind::ForeignType, |
7cac9316 | 1415 | }; |
9e0c209e | 1416 | |
7cac9316 | 1417 | Entry { |
3b2f2976 | 1418 | kind, |
7cac9316 XL |
1419 | visibility: self.lazy(&ty::Visibility::from_hir(&nitem.vis, nitem.id, tcx)), |
1420 | span: self.lazy(&nitem.span), | |
1421 | attributes: self.encode_attributes(&nitem.attrs), | |
1422 | children: LazySeq::empty(), | |
1423 | stability: self.encode_stability(def_id), | |
1424 | deprecation: self.encode_deprecation(def_id), | |
9e0c209e | 1425 | |
7cac9316 XL |
1426 | ty: Some(self.encode_item_type(def_id)), |
1427 | inherent_impls: LazySeq::empty(), | |
041b39d2 XL |
1428 | variances: match nitem.node { |
1429 | hir::ForeignItemFn(..) => self.encode_variances_of(def_id), | |
1430 | _ => LazySeq::empty(), | |
1431 | }, | |
7cac9316 XL |
1432 | generics: Some(self.encode_generics(def_id)), |
1433 | predicates: Some(self.encode_predicates(def_id)), | |
32a655c1 | 1434 | |
7cac9316 XL |
1435 | ast: None, |
1436 | mir: None, | |
1437 | } | |
1438 | } | |
1439 | } | |
9e0c209e | 1440 | |
7cac9316 XL |
1441 | struct EncodeVisitor<'a, 'b: 'a, 'tcx: 'b> { |
1442 | index: IndexBuilder<'a, 'b, 'tcx>, | |
1443 | } | |
9e0c209e | 1444 | |
7cac9316 XL |
1445 | impl<'a, 'b, 'tcx> Visitor<'tcx> for EncodeVisitor<'a, 'b, 'tcx> { |
1446 | fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> { | |
1447 | NestedVisitorMap::OnlyBodies(&self.index.tcx.hir) | |
1448 | } | |
1449 | fn visit_expr(&mut self, ex: &'tcx hir::Expr) { | |
1450 | intravisit::walk_expr(self, ex); | |
1451 | self.index.encode_info_for_expr(ex); | |
1452 | } | |
1453 | fn visit_item(&mut self, item: &'tcx hir::Item) { | |
1454 | intravisit::walk_item(self, item); | |
1455 | let def_id = self.index.tcx.hir.local_def_id(item.id); | |
1456 | match item.node { | |
1457 | hir::ItemExternCrate(_) | | |
1458 | hir::ItemUse(..) => (), // ignore these | |
1459 | _ => self.index.record(def_id, IsolatedEncoder::encode_info_for_item, (def_id, item)), | |
1460 | } | |
1461 | self.index.encode_addl_info_for_item(item); | |
1462 | } | |
1463 | fn visit_foreign_item(&mut self, ni: &'tcx hir::ForeignItem) { | |
1464 | intravisit::walk_foreign_item(self, ni); | |
1465 | let def_id = self.index.tcx.hir.local_def_id(ni.id); | |
1466 | self.index.record(def_id, | |
1467 | IsolatedEncoder::encode_info_for_foreign_item, | |
1468 | (def_id, ni)); | |
1469 | } | |
1470 | fn visit_variant(&mut self, | |
1471 | v: &'tcx hir::Variant, | |
1472 | g: &'tcx hir::Generics, | |
1473 | id: ast::NodeId) { | |
1474 | intravisit::walk_variant(self, v, g, id); | |
9e0c209e | 1475 | |
7cac9316 XL |
1476 | if let Some(discr) = v.node.disr_expr { |
1477 | let def_id = self.index.tcx.hir.body_owner_def_id(discr); | |
1478 | self.index.record(def_id, IsolatedEncoder::encode_info_for_embedded_const, def_id); | |
1479 | } | |
1480 | } | |
1481 | fn visit_generics(&mut self, generics: &'tcx hir::Generics) { | |
1482 | intravisit::walk_generics(self, generics); | |
1483 | self.index.encode_info_for_generics(generics); | |
1484 | } | |
1485 | fn visit_ty(&mut self, ty: &'tcx hir::Ty) { | |
1486 | intravisit::walk_ty(self, ty); | |
1487 | self.index.encode_info_for_ty(ty); | |
1488 | } | |
1489 | fn visit_macro_def(&mut self, macro_def: &'tcx hir::MacroDef) { | |
1490 | let def_id = self.index.tcx.hir.local_def_id(macro_def.id); | |
1491 | self.index.record(def_id, IsolatedEncoder::encode_info_for_macro_def, macro_def); | |
1492 | } | |
1493 | } | |
9e0c209e | 1494 | |
7cac9316 XL |
1495 | impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> { |
1496 | fn encode_fields(&mut self, adt_def_id: DefId) { | |
1497 | let def = self.tcx.adt_def(adt_def_id); | |
1498 | for (variant_index, variant) in def.variants.iter().enumerate() { | |
1499 | for (field_index, field) in variant.fields.iter().enumerate() { | |
1500 | self.record(field.did, | |
1501 | IsolatedEncoder::encode_field, | |
1502 | (adt_def_id, Untracked((variant_index, field_index)))); | |
1503 | } | |
1504 | } | |
1505 | } | |
9e0c209e | 1506 | |
7cac9316 XL |
1507 | fn encode_info_for_generics(&mut self, generics: &hir::Generics) { |
1508 | for ty_param in &generics.ty_params { | |
1509 | let def_id = self.tcx.hir.local_def_id(ty_param.id); | |
1510 | let has_default = Untracked(ty_param.default.is_some()); | |
1511 | self.record(def_id, IsolatedEncoder::encode_info_for_ty_param, (def_id, has_default)); | |
1512 | } | |
1513 | } | |
9cc50fc6 | 1514 | |
7cac9316 | 1515 | fn encode_info_for_ty(&mut self, ty: &hir::Ty) { |
ea8adc8c | 1516 | match ty.node { |
abe05a73 | 1517 | hir::TyImplTraitExistential(_) => { |
ea8adc8c XL |
1518 | let def_id = self.tcx.hir.local_def_id(ty.id); |
1519 | self.record(def_id, IsolatedEncoder::encode_info_for_anon_ty, def_id); | |
1520 | } | |
1521 | hir::TyArray(_, len) => { | |
1522 | let def_id = self.tcx.hir.body_owner_def_id(len); | |
1523 | self.record(def_id, IsolatedEncoder::encode_info_for_embedded_const, def_id); | |
1524 | } | |
1525 | _ => {} | |
7cac9316 XL |
1526 | } |
1527 | } | |
c34b1796 | 1528 | |
7cac9316 XL |
1529 | fn encode_info_for_expr(&mut self, expr: &hir::Expr) { |
1530 | match expr.node { | |
1531 | hir::ExprClosure(..) => { | |
1532 | let def_id = self.tcx.hir.local_def_id(expr.id); | |
1533 | self.record(def_id, IsolatedEncoder::encode_info_for_closure, def_id); | |
1534 | } | |
1535 | _ => {} | |
1536 | } | |
1537 | } | |
1538 | ||
1539 | /// In some cases, along with the item itself, we also | |
1540 | /// encode some sub-items. Usually we want some info from the item | |
1541 | /// so it's easier to do that here then to wait until we would encounter | |
1542 | /// normally in the visitor walk. | |
1543 | fn encode_addl_info_for_item(&mut self, item: &hir::Item) { | |
1544 | let def_id = self.tcx.hir.local_def_id(item.id); | |
1545 | match item.node { | |
1546 | hir::ItemStatic(..) | | |
1547 | hir::ItemConst(..) | | |
1548 | hir::ItemFn(..) | | |
1549 | hir::ItemMod(..) | | |
1550 | hir::ItemForeignMod(..) | | |
1551 | hir::ItemGlobalAsm(..) | | |
1552 | hir::ItemExternCrate(..) | | |
1553 | hir::ItemUse(..) | | |
abe05a73 | 1554 | hir::ItemAutoImpl(..) | |
7cac9316 XL |
1555 | hir::ItemTy(..) => { |
1556 | // no sub-item recording needed in these cases | |
1557 | } | |
1558 | hir::ItemEnum(..) => { | |
1559 | self.encode_fields(def_id); | |
1560 | ||
1561 | let def = self.tcx.adt_def(def_id); | |
1562 | for (i, variant) in def.variants.iter().enumerate() { | |
1563 | self.record(variant.did, | |
1564 | IsolatedEncoder::encode_enum_variant_info, | |
1565 | (def_id, Untracked(i))); | |
1566 | } | |
1567 | } | |
1568 | hir::ItemStruct(ref struct_def, _) => { | |
1569 | self.encode_fields(def_id); | |
1570 | ||
1571 | // If the struct has a constructor, encode it. | |
1572 | if !struct_def.is_struct() { | |
1573 | let ctor_def_id = self.tcx.hir.local_def_id(struct_def.id()); | |
1574 | self.record(ctor_def_id, | |
1575 | IsolatedEncoder::encode_struct_ctor, | |
1576 | (def_id, ctor_def_id)); | |
1577 | } | |
1578 | } | |
1579 | hir::ItemUnion(..) => { | |
1580 | self.encode_fields(def_id); | |
1581 | } | |
1582 | hir::ItemImpl(..) => { | |
1583 | for &trait_item_def_id in self.tcx.associated_item_def_ids(def_id).iter() { | |
1584 | self.record(trait_item_def_id, | |
1585 | IsolatedEncoder::encode_info_for_impl_item, | |
1586 | trait_item_def_id); | |
1587 | } | |
1588 | } | |
1589 | hir::ItemTrait(..) => { | |
1590 | for &item_def_id in self.tcx.associated_item_def_ids(def_id).iter() { | |
1591 | self.record(item_def_id, | |
1592 | IsolatedEncoder::encode_info_for_trait_item, | |
1593 | item_def_id); | |
9e0c209e | 1594 | } |
223e47cc | 1595 | } |
7cac9316 XL |
1596 | } |
1597 | } | |
1598 | } | |
9e0c209e | 1599 | |
7cac9316 XL |
1600 | struct ImplVisitor<'a, 'tcx: 'a> { |
1601 | tcx: TyCtxt<'a, 'tcx, 'tcx>, | |
1602 | impls: FxHashMap<DefId, Vec<DefIndex>>, | |
1603 | } | |
1604 | ||
1605 | impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for ImplVisitor<'a, 'tcx> { | |
1606 | fn visit_item(&mut self, item: &hir::Item) { | |
1607 | if let hir::ItemImpl(..) = item.node { | |
1608 | let impl_id = self.tcx.hir.local_def_id(item.id); | |
1609 | if let Some(trait_ref) = self.tcx.impl_trait_ref(impl_id) { | |
1610 | self.impls | |
1611 | .entry(trait_ref.def_id) | |
1612 | .or_insert(vec![]) | |
1613 | .push(impl_id.index); | |
1614 | } | |
223e47cc | 1615 | } |
7cac9316 | 1616 | } |
223e47cc | 1617 | |
7cac9316 XL |
1618 | fn visit_trait_item(&mut self, _trait_item: &'v hir::TraitItem) {} |
1619 | ||
1620 | fn visit_impl_item(&mut self, _impl_item: &'v hir::ImplItem) { | |
1621 | // handled in `visit_item` above | |
223e47cc | 1622 | } |
223e47cc LB |
1623 | } |
1624 | ||
9e0c209e SL |
1625 | // NOTE(eddyb) The following comment was preserved for posterity, even |
1626 | // though it's no longer relevant as EBML (which uses nested & tagged | |
1627 | // "documents") was replaced with a scheme that can't go out of bounds. | |
1628 | // | |
1629 | // And here we run into yet another obscure archive bug: in which metadata | |
1630 | // loaded from archives may have trailing garbage bytes. Awhile back one of | |
cc61c64b | 1631 | // our tests was failing sporadically on the macOS 64-bit builders (both nopt |
9e0c209e SL |
1632 | // and opt) by having ebml generate an out-of-bounds panic when looking at |
1633 | // metadata. | |
1634 | // | |
1635 | // Upon investigation it turned out that the metadata file inside of an rlib | |
1636 | // (and ar archive) was being corrupted. Some compilations would generate a | |
1637 | // metadata file which would end in a few extra bytes, while other | |
1638 | // compilations would not have these extra bytes appended to the end. These | |
1639 | // extra bytes were interpreted by ebml as an extra tag, so they ended up | |
1640 | // being interpreted causing the out-of-bounds. | |
1641 | // | |
1642 | // The root cause of why these extra bytes were appearing was never | |
1643 | // discovered, and in the meantime the solution we're employing is to insert | |
1644 | // the length of the metadata to the start of the metadata. Later on this | |
1645 | // will allow us to slice the metadata to the precise length that we just | |
1646 | // generated regardless of trailing bytes that end up in it. | |
1647 | ||
1648 | pub fn encode_metadata<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, | |
9e0c209e | 1649 | link_meta: &LinkMeta, |
476ff2be | 1650 | exported_symbols: &NodeSet) |
3b2f2976 | 1651 | -> (EncodedMetadata, EncodedMetadataHashes) |
cc61c64b | 1652 | { |
9e0c209e SL |
1653 | let mut cursor = Cursor::new(vec![]); |
1654 | cursor.write_all(METADATA_HEADER).unwrap(); | |
1655 | ||
7cac9316 | 1656 | // Will be filled with the root position after encoding everything. |
9e0c209e SL |
1657 | cursor.write_all(&[0, 0, 0, 0]).unwrap(); |
1658 | ||
7cac9316 XL |
1659 | let compute_ich = (tcx.sess.opts.debugging_opts.query_dep_graph || |
1660 | tcx.sess.opts.debugging_opts.incremental_cc) && | |
1661 | tcx.sess.opts.build_dep_graph(); | |
1662 | ||
cc61c64b | 1663 | let (root, metadata_hashes) = { |
476ff2be | 1664 | let mut ecx = EncodeContext { |
c30ab7b3 | 1665 | opaque: opaque::Encoder::new(&mut cursor), |
3b2f2976 XL |
1666 | tcx, |
1667 | link_meta, | |
1668 | exported_symbols, | |
c30ab7b3 SL |
1669 | lazy_state: LazyState::NoNode, |
1670 | type_shorthands: Default::default(), | |
1671 | predicate_shorthands: Default::default(), | |
7cac9316 | 1672 | metadata_hashes: EncodedMetadataHashes::new(), |
3b2f2976 | 1673 | compute_ich, |
476ff2be SL |
1674 | }; |
1675 | ||
1676 | // Encode the rustc version string in a predictable location. | |
1677 | rustc_version().encode(&mut ecx).unwrap(); | |
1678 | ||
1679 | // Encode all the entries and extra information in the crate, | |
1680 | // culminating in the `CrateRoot` which points to all of it. | |
cc61c64b XL |
1681 | let root = ecx.encode_crate_root(); |
1682 | (root, ecx.metadata_hashes) | |
476ff2be | 1683 | }; |
9e0c209e SL |
1684 | let mut result = cursor.into_inner(); |
1685 | ||
1686 | // Encode the root position. | |
1687 | let header = METADATA_HEADER.len(); | |
1688 | let pos = root.position; | |
1689 | result[header + 0] = (pos >> 24) as u8; | |
1690 | result[header + 1] = (pos >> 16) as u8; | |
c30ab7b3 SL |
1691 | result[header + 2] = (pos >> 8) as u8; |
1692 | result[header + 3] = (pos >> 0) as u8; | |
9e0c209e | 1693 | |
3b2f2976 | 1694 | (EncodedMetadata { raw_data: result }, metadata_hashes) |
223e47cc | 1695 | } |
8bb4bdeb XL |
1696 | |
1697 | pub fn get_repr_options<'a, 'tcx, 'gcx>(tcx: &TyCtxt<'a, 'tcx, 'gcx>, did: DefId) -> ReprOptions { | |
7cac9316 | 1698 | let ty = tcx.type_of(did); |
8bb4bdeb XL |
1699 | match ty.sty { |
1700 | ty::TyAdt(ref def, _) => return def.repr, | |
1701 | _ => bug!("{} is not an ADT", ty), | |
1702 | } | |
1703 | } |