1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
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.
11 use dep_graph::{DepConstructor, DepNode};
12 use errors::DiagnosticBuilder;
13 use hir::def_id::{CrateNum, DefId, DefIndex};
14 use hir::def::{Def, Export};
15 use hir::{self, TraitCandidate, ItemLocalId};
18 use middle::const_val;
19 use middle::cstore::{ExternCrate, LinkagePreference, NativeLibrary,
20 ExternBodyNestedBodies};
21 use middle::cstore::{NativeLibraryKind, DepKind, CrateSource, ExternConstBody};
22 use middle::privacy::AccessLevels;
23 use middle::reachable::ReachableSet;
25 use middle::resolve_lifetime::{Region, ObjectLifetimeDefault};
26 use middle::stability::{self, DeprecationEntry};
27 use middle::lang_items::{LanguageItems, LangItem};
28 use middle::exported_symbols::SymbolExportLevel;
29 use middle::trans::{CodegenUnit, Stats};
31 use session::CompileResult;
32 use session::config::OutputFilenames;
33 use traits::specialization_graph;
34 use ty::{self, CrateInherentImpls, Ty, TyCtxt};
35 use ty::layout::{Layout, LayoutError};
37 use ty::subst::Substs;
38 use util::nodemap::{DefIdSet, DefIdMap};
39 use util::common::{profq_msg, ProfileQueriesMsg};
41 use rustc_data_structures::indexed_set::IdxSetBuf;
42 use rustc_back::PanicStrategy;
43 use rustc_data_structures::indexed_vec::IndexVec;
44 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
45 use std::cell::{RefCell, Cell};
50 use syntax_pos::{Span, DUMMY_SP};
51 use syntax_pos::symbol::InternedString;
54 use syntax::symbol::Symbol;
58 use self::plumbing::*;
61 pub use self::keys::Key;
64 use self::values::Value;
67 pub use self::config::QueryConfig;
68 use self::config::QueryDescription;
70 // Each of these maps also corresponds to a method on a
71 // `Provider` trait for requesting a value of that type,
72 // and a method on `Maps` itself for doing that in a
73 // a way that memoizes and does dep-graph tracking,
74 // wrapping around the actual chain of providers that
75 // the driver creates (using several `rustc_*` crates).
77 /// Records the type of every item.
78 [] fn type_of: TypeOfItem(DefId) -> Ty<'tcx>,
80 /// Maps from the def-id of an item (trait/struct/enum/fn) to its
81 /// associated generics and predicates.
82 [] fn generics_of: GenericsOfItem(DefId) -> &'tcx ty::Generics,
83 [] fn predicates_of: PredicatesOfItem(DefId) -> ty::GenericPredicates<'tcx>,
85 /// Maps from the def-id of a trait to the list of
86 /// super-predicates. This is a subset of the full list of
87 /// predicates. We store these in a separate map because we must
88 /// evaluate them even during type conversion, often before the
89 /// full predicates are available (note that supertraits have
90 /// additional acyclicity requirements).
91 [] fn super_predicates_of: SuperPredicatesOfItem(DefId) -> ty::GenericPredicates<'tcx>,
93 /// To avoid cycles within the predicates of a single item we compute
94 /// per-type-parameter predicates for resolving `T::AssocTy`.
95 [] fn type_param_predicates: type_param_predicates((DefId, DefId))
96 -> ty::GenericPredicates<'tcx>,
98 [] fn trait_def: TraitDefOfItem(DefId) -> &'tcx ty::TraitDef,
99 [] fn adt_def: AdtDefOfItem(DefId) -> &'tcx ty::AdtDef,
100 [] fn adt_destructor: AdtDestructor(DefId) -> Option<ty::Destructor>,
101 [] fn adt_sized_constraint: SizedConstraint(DefId) -> &'tcx [Ty<'tcx>],
102 [] fn adt_dtorck_constraint: DtorckConstraint(DefId) -> ty::DtorckConstraint<'tcx>,
104 /// True if this is a const fn
105 [] fn is_const_fn: IsConstFn(DefId) -> bool,
107 /// True if this is a foreign item (i.e., linked via `extern { ... }`).
108 [] fn is_foreign_item: IsForeignItem(DefId) -> bool,
110 /// True if this is a default impl (aka impl Foo for ..)
111 [] fn is_default_impl: IsDefaultImpl(DefId) -> bool,
113 /// Get a map with the variance of every item; use `item_variance`
115 [] fn crate_variances: crate_variances(CrateNum) -> Rc<ty::CrateVariancesMap>,
117 /// Maps from def-id of a type or region parameter to its
118 /// (inferred) variance.
119 [] fn variances_of: ItemVariances(DefId) -> Rc<Vec<ty::Variance>>,
121 /// Maps from an impl/trait def-id to a list of the def-ids of its items
122 [] fn associated_item_def_ids: AssociatedItemDefIds(DefId) -> Rc<Vec<DefId>>,
124 /// Maps from a trait item to the trait item "descriptor"
125 [] fn associated_item: AssociatedItems(DefId) -> ty::AssociatedItem,
127 [] fn impl_trait_ref: ImplTraitRef(DefId) -> Option<ty::TraitRef<'tcx>>,
128 [] fn impl_polarity: ImplPolarity(DefId) -> hir::ImplPolarity,
130 /// Maps a DefId of a type to a list of its inherent impls.
131 /// Contains implementations of methods that are inherent to a type.
132 /// Methods in these implementations don't need to be exported.
133 [] fn inherent_impls: InherentImpls(DefId) -> Rc<Vec<DefId>>,
135 /// Set of all the def-ids in this crate that have MIR associated with
136 /// them. This includes all the body owners, but also things like struct
138 [] fn mir_keys: mir_keys(CrateNum) -> Rc<DefIdSet>,
140 /// Maps DefId's that have an associated Mir to the result
141 /// of the MIR qualify_consts pass. The actual meaning of
142 /// the value isn't known except to the pass itself.
143 [] fn mir_const_qualif: MirConstQualif(DefId) -> (u8, Rc<IdxSetBuf<mir::Local>>),
145 /// Fetch the MIR for a given def-id up till the point where it is
146 /// ready for const evaluation.
148 /// See the README for the `mir` module for details.
149 [] fn mir_const: MirConst(DefId) -> &'tcx Steal<mir::Mir<'tcx>>,
151 [] fn mir_validated: MirValidated(DefId) -> &'tcx Steal<mir::Mir<'tcx>>,
153 /// MIR after our optimization passes have run. This is MIR that is ready
154 /// for trans. This is also the only query that can fetch non-local MIR, at present.
155 [] fn optimized_mir: MirOptimized(DefId) -> &'tcx mir::Mir<'tcx>,
157 /// Type of each closure. The def ID is the ID of the
158 /// expression defining the closure.
159 [] fn closure_kind: ClosureKind(DefId) -> ty::ClosureKind,
161 /// The signature of functions and closures.
162 [] fn fn_sig: FnSignature(DefId) -> ty::PolyFnSig<'tcx>,
164 /// Records the signature of each generator. The def ID is the ID of the
165 /// expression defining the closure.
166 [] fn generator_sig: GenSignature(DefId) -> Option<ty::PolyGenSig<'tcx>>,
168 /// Caches CoerceUnsized kinds for impls on custom types.
169 [] fn coerce_unsized_info: CoerceUnsizedInfo(DefId)
170 -> ty::adjustment::CoerceUnsizedInfo,
172 [] fn typeck_item_bodies: typeck_item_bodies_dep_node(CrateNum) -> CompileResult,
174 [] fn typeck_tables_of: TypeckTables(DefId) -> &'tcx ty::TypeckTables<'tcx>,
176 [] fn has_typeck_tables: HasTypeckTables(DefId) -> bool,
178 [] fn coherent_trait: coherent_trait_dep_node((CrateNum, DefId)) -> (),
180 [] fn borrowck: BorrowCheck(DefId) -> (),
181 // FIXME: shouldn't this return a `Result<(), BorrowckErrors>` instead?
182 [] fn mir_borrowck: MirBorrowCheck(DefId) -> (),
184 /// Gets a complete map from all types to their inherent impls.
185 /// Not meant to be used directly outside of coherence.
186 /// (Defined only for LOCAL_CRATE)
187 [] fn crate_inherent_impls: crate_inherent_impls_dep_node(CrateNum) -> CrateInherentImpls,
189 /// Checks all types in the krate for overlap in their inherent impls. Reports errors.
190 /// Not meant to be used directly outside of coherence.
191 /// (Defined only for LOCAL_CRATE)
192 [] fn crate_inherent_impls_overlap_check: inherent_impls_overlap_check_dep_node(CrateNum) -> (),
194 /// Results of evaluating const items or constants embedded in
195 /// other items (such as enum variant explicit discriminants).
196 [] fn const_eval: const_eval_dep_node(ty::ParamEnvAnd<'tcx, (DefId, &'tcx Substs<'tcx>)>)
197 -> const_val::EvalResult<'tcx>,
199 /// Performs the privacy check and computes "access levels".
200 [] fn privacy_access_levels: PrivacyAccessLevels(CrateNum) -> Rc<AccessLevels>,
202 [] fn reachable_set: reachability_dep_node(CrateNum) -> ReachableSet,
204 /// Per-body `region::ScopeTree`. The `DefId` should be the owner-def-id for the body;
205 /// in the case of closures, this will be redirected to the enclosing function.
206 [] fn region_scope_tree: RegionScopeTree(DefId) -> Rc<region::ScopeTree>,
208 [] fn mir_shims: mir_shim_dep_node(ty::InstanceDef<'tcx>) -> &'tcx mir::Mir<'tcx>,
210 [] fn def_symbol_name: SymbolName(DefId) -> ty::SymbolName,
211 [] fn symbol_name: symbol_name_dep_node(ty::Instance<'tcx>) -> ty::SymbolName,
213 [] fn describe_def: DescribeDef(DefId) -> Option<Def>,
214 [] fn def_span: DefSpan(DefId) -> Span,
215 [] fn lookup_stability: LookupStability(DefId) -> Option<&'tcx attr::Stability>,
216 [] fn lookup_deprecation_entry: LookupDeprecationEntry(DefId) -> Option<DeprecationEntry>,
217 [] fn item_attrs: ItemAttrs(DefId) -> Rc<[ast::Attribute]>,
218 [] fn fn_arg_names: FnArgNames(DefId) -> Vec<ast::Name>,
219 [] fn impl_parent: ImplParent(DefId) -> Option<DefId>,
220 [] fn trait_of_item: TraitOfItem(DefId) -> Option<DefId>,
221 [] fn is_exported_symbol: IsExportedSymbol(DefId) -> bool,
222 [] fn item_body_nested_bodies: ItemBodyNestedBodies(DefId) -> ExternBodyNestedBodies,
223 [] fn const_is_rvalue_promotable_to_static: ConstIsRvaluePromotableToStatic(DefId) -> bool,
224 [] fn is_mir_available: IsMirAvailable(DefId) -> bool,
226 [] fn trait_impls_of: TraitImpls(DefId) -> Rc<ty::trait_def::TraitImpls>,
227 [] fn specialization_graph_of: SpecializationGraph(DefId) -> Rc<specialization_graph::Graph>,
228 [] fn is_object_safe: ObjectSafety(DefId) -> bool,
230 // Get the ParameterEnvironment for a given item; this environment
231 // will be in "user-facing" mode, meaning that it is suitabe for
232 // type-checking etc, and it does not normalize specializable
233 // associated types. This is almost always what you want,
234 // unless you are doing MIR optimizations, in which case you
235 // might want to use `reveal_all()` method to change modes.
236 [] fn param_env: ParamEnv(DefId) -> ty::ParamEnv<'tcx>,
238 // Trait selection queries. These are best used by invoking `ty.moves_by_default()`,
239 // `ty.is_copy()`, etc, since that will prune the environment where possible.
240 [] fn is_copy_raw: is_copy_dep_node(ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool,
241 [] fn is_sized_raw: is_sized_dep_node(ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool,
242 [] fn is_freeze_raw: is_freeze_dep_node(ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool,
243 [] fn needs_drop_raw: needs_drop_dep_node(ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool,
244 [] fn layout_raw: layout_dep_node(ty::ParamEnvAnd<'tcx, Ty<'tcx>>)
245 -> Result<&'tcx Layout, LayoutError<'tcx>>,
247 [] fn dylib_dependency_formats: DylibDepFormats(CrateNum)
248 -> Rc<Vec<(CrateNum, LinkagePreference)>>,
250 [] fn is_panic_runtime: IsPanicRuntime(CrateNum) -> bool,
251 [] fn is_compiler_builtins: IsCompilerBuiltins(CrateNum) -> bool,
252 [] fn has_global_allocator: HasGlobalAllocator(CrateNum) -> bool,
253 [] fn is_sanitizer_runtime: IsSanitizerRuntime(CrateNum) -> bool,
254 [] fn is_profiler_runtime: IsProfilerRuntime(CrateNum) -> bool,
255 [] fn panic_strategy: GetPanicStrategy(CrateNum) -> PanicStrategy,
256 [] fn is_no_builtins: IsNoBuiltins(CrateNum) -> bool,
258 [] fn extern_crate: ExternCrate(DefId) -> Rc<Option<ExternCrate>>,
260 [] fn specializes: specializes_node((DefId, DefId)) -> bool,
261 [] fn in_scope_traits_map: InScopeTraits(DefIndex)
262 -> Option<Rc<FxHashMap<ItemLocalId, Rc<Vec<TraitCandidate>>>>>,
263 [] fn module_exports: ModuleExports(DefId) -> Option<Rc<Vec<Export>>>,
264 [] fn lint_levels: lint_levels_node(CrateNum) -> Rc<lint::LintLevelMap>,
266 [] fn impl_defaultness: ImplDefaultness(DefId) -> hir::Defaultness,
267 [] fn exported_symbol_ids: ExportedSymbolIds(CrateNum) -> Rc<DefIdSet>,
268 [] fn native_libraries: NativeLibraries(CrateNum) -> Rc<Vec<NativeLibrary>>,
269 [] fn plugin_registrar_fn: PluginRegistrarFn(CrateNum) -> Option<DefId>,
270 [] fn derive_registrar_fn: DeriveRegistrarFn(CrateNum) -> Option<DefId>,
271 [] fn crate_disambiguator: CrateDisambiguator(CrateNum) -> Symbol,
272 [] fn crate_hash: CrateHash(CrateNum) -> Svh,
273 [] fn original_crate_name: OriginalCrateName(CrateNum) -> Symbol,
275 [] fn implementations_of_trait: implementations_of_trait_node((CrateNum, DefId))
277 [] fn all_trait_implementations: AllTraitImplementations(CrateNum)
280 [] fn is_dllimport_foreign_item: IsDllimportForeignItem(DefId) -> bool,
281 [] fn is_statically_included_foreign_item: IsStaticallyIncludedForeignItem(DefId) -> bool,
282 [] fn native_library_kind: NativeLibraryKind(DefId)
283 -> Option<NativeLibraryKind>,
284 [] fn link_args: link_args_node(CrateNum) -> Rc<Vec<String>>,
286 [] fn named_region_map: NamedRegion(DefIndex) ->
287 Option<Rc<FxHashMap<ItemLocalId, Region>>>,
288 [] fn is_late_bound_map: IsLateBound(DefIndex) ->
289 Option<Rc<FxHashSet<ItemLocalId>>>,
290 [] fn object_lifetime_defaults_map: ObjectLifetimeDefaults(DefIndex)
291 -> Option<Rc<FxHashMap<ItemLocalId, Rc<Vec<ObjectLifetimeDefault>>>>>,
293 [] fn visibility: Visibility(DefId) -> ty::Visibility,
294 [] fn dep_kind: DepKind(CrateNum) -> DepKind,
295 [] fn crate_name: CrateName(CrateNum) -> Symbol,
296 [] fn item_children: ItemChildren(DefId) -> Rc<Vec<Export>>,
297 [] fn extern_mod_stmt_cnum: ExternModStmtCnum(DefId) -> Option<CrateNum>,
299 [] fn get_lang_items: get_lang_items_node(CrateNum) -> Rc<LanguageItems>,
300 [] fn defined_lang_items: DefinedLangItems(CrateNum) -> Rc<Vec<(DefId, usize)>>,
301 [] fn missing_lang_items: MissingLangItems(CrateNum) -> Rc<Vec<LangItem>>,
302 [] fn extern_const_body: ExternConstBody(DefId) -> ExternConstBody<'tcx>,
303 [] fn visible_parent_map: visible_parent_map_node(CrateNum)
304 -> Rc<DefIdMap<DefId>>,
305 [] fn missing_extern_crate_item: MissingExternCrateItem(CrateNum) -> bool,
306 [] fn used_crate_source: UsedCrateSource(CrateNum) -> Rc<CrateSource>,
307 [] fn postorder_cnums: postorder_cnums_node(CrateNum) -> Rc<Vec<CrateNum>>,
309 [] fn freevars: Freevars(DefId) -> Option<Rc<Vec<hir::Freevar>>>,
310 [] fn maybe_unused_trait_import: MaybeUnusedTraitImport(DefId) -> bool,
311 [] fn maybe_unused_extern_crates: maybe_unused_extern_crates_node(CrateNum)
312 -> Rc<Vec<(DefId, Span)>>,
314 [] fn stability_index: stability_index_node(CrateNum) -> Rc<stability::Index<'tcx>>,
315 [] fn all_crate_nums: all_crate_nums_node(CrateNum) -> Rc<Vec<CrateNum>>,
317 [] fn exported_symbols: ExportedSymbols(CrateNum)
318 -> Arc<Vec<(String, Option<DefId>, SymbolExportLevel)>>,
319 [] fn collect_and_partition_translation_items:
320 collect_and_partition_translation_items_node(CrateNum)
321 -> (Arc<DefIdSet>, Arc<Vec<Arc<CodegenUnit<'tcx>>>>),
322 [] fn export_name: ExportName(DefId) -> Option<Symbol>,
323 [] fn contains_extern_indicator: ContainsExternIndicator(DefId) -> bool,
324 [] fn is_translated_function: IsTranslatedFunction(DefId) -> bool,
325 [] fn codegen_unit: CodegenUnit(InternedString) -> Arc<CodegenUnit<'tcx>>,
326 [] fn compile_codegen_unit: CompileCodegenUnit(InternedString) -> Stats,
327 [] fn output_filenames: output_filenames_node(CrateNum)
328 -> Arc<OutputFilenames>,
330 [] fn has_copy_closures: HasCopyClosures(CrateNum) -> bool,
331 [] fn has_clone_closures: HasCloneClosures(CrateNum) -> bool,
334 //////////////////////////////////////////////////////////////////////
335 // These functions are little shims used to find the dep-node for a
336 // given query when there is not a *direct* mapping:
338 fn type_param_predicates<'tcx>((item_id, param_id): (DefId, DefId)) -> DepConstructor<'tcx> {
339 DepConstructor::TypeParamPredicates {
345 fn coherent_trait_dep_node<'tcx>((_, def_id): (CrateNum, DefId)) -> DepConstructor<'tcx> {
346 DepConstructor::CoherenceCheckTrait(def_id)
349 fn crate_inherent_impls_dep_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
350 DepConstructor::Coherence
353 fn inherent_impls_overlap_check_dep_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
354 DepConstructor::CoherenceInherentImplOverlapCheck
357 fn reachability_dep_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
358 DepConstructor::Reachability
361 fn mir_shim_dep_node<'tcx>(instance_def: ty::InstanceDef<'tcx>) -> DepConstructor<'tcx> {
362 DepConstructor::MirShim {
367 fn symbol_name_dep_node<'tcx>(instance: ty::Instance<'tcx>) -> DepConstructor<'tcx> {
368 DepConstructor::InstanceSymbolName { instance }
371 fn typeck_item_bodies_dep_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
372 DepConstructor::TypeckBodiesKrate
375 fn const_eval_dep_node<'tcx>(_: ty::ParamEnvAnd<'tcx, (DefId, &'tcx Substs<'tcx>)>)
376 -> DepConstructor<'tcx> {
377 DepConstructor::ConstEval
380 fn mir_keys<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
381 DepConstructor::MirKeys
384 fn crate_variances<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
385 DepConstructor::CrateVariances
388 fn is_copy_dep_node<'tcx>(_: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> DepConstructor<'tcx> {
389 DepConstructor::IsCopy
392 fn is_sized_dep_node<'tcx>(_: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> DepConstructor<'tcx> {
393 DepConstructor::IsSized
396 fn is_freeze_dep_node<'tcx>(_: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> DepConstructor<'tcx> {
397 DepConstructor::IsFreeze
400 fn needs_drop_dep_node<'tcx>(_: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> DepConstructor<'tcx> {
401 DepConstructor::NeedsDrop
404 fn layout_dep_node<'tcx>(_: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> DepConstructor<'tcx> {
405 DepConstructor::Layout
408 fn lint_levels_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
409 DepConstructor::LintLevels
412 fn specializes_node<'tcx>((a, b): (DefId, DefId)) -> DepConstructor<'tcx> {
413 DepConstructor::Specializes { impl1: a, impl2: b }
416 fn implementations_of_trait_node<'tcx>((krate, trait_id): (CrateNum, DefId))
417 -> DepConstructor<'tcx>
419 DepConstructor::ImplementationsOfTrait { krate, trait_id }
422 fn link_args_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
423 DepConstructor::LinkArgs
426 fn get_lang_items_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
427 DepConstructor::GetLangItems
430 fn visible_parent_map_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
431 DepConstructor::VisibleParentMap
434 fn postorder_cnums_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
435 DepConstructor::PostorderCnums
438 fn maybe_unused_extern_crates_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
439 DepConstructor::MaybeUnusedExternCrates
442 fn stability_index_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
443 DepConstructor::StabilityIndex
446 fn all_crate_nums_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
447 DepConstructor::AllCrateNums
450 fn collect_and_partition_translation_items_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
451 DepConstructor::CollectAndPartitionTranslationItems
454 fn output_filenames_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
455 DepConstructor::OutputFilenames