1 // Copyright 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 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
12 // file at the top-level directory of this distribution and at
13 // http://rust-lang.org/COPYRIGHT.
15 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
16 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
17 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
18 // option. This file may not be copied, modified, or distributed
19 // except according to those terms.
21 // the rustc crate store interface. This also includes types that
22 // are *mostly* used as a part of that interface, but these should
23 // probably get a better home if someone can find one.
25 use hir
::def
::{self, Def}
;
26 use hir
::def_id
::{CrateNum, DefId, DefIndex}
;
27 use hir
::map
as hir_map
;
28 use hir
::map
::definitions
::{Definitions, DefKey}
;
30 use middle
::lang_items
;
31 use ty
::{self, Ty, TyCtxt}
;
34 use session
::search_paths
::PathKind
;
35 use util
::nodemap
::{NodeSet, DefIdMap}
;
36 use std
::path
::PathBuf
;
39 use syntax
::ext
::base
::SyntaxExtension
;
41 use syntax
::parse
::token
::InternedString
;
43 use rustc_back
::target
::Target
;
45 use hir
::intravisit
::Visitor
;
46 use rustc_back
::PanicStrategy
;
48 pub use self::NativeLibraryKind
::{NativeStatic, NativeFramework, NativeUnknown}
;
50 // lonely orphan structs and enums looking for a better home
52 #[derive(Clone, Debug)]
54 pub crate_name
: String
,
58 // Where a crate came from on the local filesystem. One of these two options
60 #[derive(PartialEq, Clone, Debug)]
61 pub struct CrateSource
{
62 pub dylib
: Option
<(PathBuf
, PathKind
)>,
63 pub rlib
: Option
<(PathBuf
, PathKind
)>,
67 #[derive(Copy, Debug, PartialEq, Clone, RustcEncodable, RustcDecodable)]
68 pub enum LinkagePreference
{
73 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
74 pub enum NativeLibraryKind
{
75 NativeStatic
, // native static library (.a archive)
76 NativeFramework
, // OSX-specific
77 NativeUnknown
, // default way to specify a dynamic library
80 /// The data we save and restore about an inlined item or method. This is not
81 /// part of the AST that we parse from a file, but it becomes part of the tree
83 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
84 pub enum InlinedItem
{
85 Item(DefId
/* def-id in source crate */, P
<hir
::Item
>),
86 TraitItem(DefId
/* impl id */, P
<hir
::TraitItem
>),
87 ImplItem(DefId
/* impl id */, P
<hir
::ImplItem
>)
90 /// A borrowed version of `hir::InlinedItem`.
91 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, Hash, Debug)]
92 pub enum InlinedItemRef
<'a
> {
93 Item(DefId
, &'a hir
::Item
),
94 TraitItem(DefId
, &'a hir
::TraitItem
),
95 ImplItem(DefId
, &'a hir
::ImplItem
)
98 #[derive(Copy, Clone, Debug)]
99 pub struct ExternCrate
{
100 /// def_id of an `extern crate` in the current crate that caused
101 /// this crate to be loaded; note that there could be multiple
105 /// span of the extern crate that caused this to be loaded
108 /// If true, then this crate is the crate named by the extern
109 /// crate referenced above. If false, then this crate is a dep
113 /// Number of links to reach the extern crate `def_id`
114 /// declaration; used to select the extern crate with the shortest
119 /// A store of Rust crates, through with their metadata
121 pub trait CrateStore
<'tcx
> {
123 fn describe_def(&self, def
: DefId
) -> Option
<Def
>;
124 fn stability(&self, def
: DefId
) -> Option
<attr
::Stability
>;
125 fn deprecation(&self, def
: DefId
) -> Option
<attr
::Deprecation
>;
126 fn visibility(&self, def
: DefId
) -> ty
::Visibility
;
127 fn closure_kind(&self, def_id
: DefId
) -> ty
::ClosureKind
;
128 fn closure_ty
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def_id
: DefId
)
129 -> ty
::ClosureTy
<'tcx
>;
130 fn item_variances(&self, def
: DefId
) -> Vec
<ty
::Variance
>;
131 fn item_type
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
)
133 fn visible_parent_map
<'a
>(&'a
self) -> ::std
::cell
::RefMut
<'a
, DefIdMap
<DefId
>>;
134 fn item_predicates
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
)
135 -> ty
::GenericPredicates
<'tcx
>;
136 fn item_super_predicates
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
)
137 -> ty
::GenericPredicates
<'tcx
>;
138 fn item_generics
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
)
139 -> ty
::Generics
<'tcx
>;
140 fn item_attrs(&self, def_id
: DefId
) -> Vec
<ast
::Attribute
>;
141 fn trait_def
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
)-> ty
::TraitDef
<'tcx
>;
142 fn adt_def
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
) -> ty
::AdtDefMaster
<'tcx
>;
143 fn fn_arg_names(&self, did
: DefId
) -> Vec
<ast
::Name
>;
144 fn inherent_implementations_for_type(&self, def_id
: DefId
) -> Vec
<DefId
>;
147 fn implementations_of_trait(&self, filter
: Option
<DefId
>) -> Vec
<DefId
>;
150 fn impl_or_trait_items(&self, def_id
: DefId
) -> Vec
<DefId
>;
151 fn impl_trait_ref
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
)
152 -> Option
<ty
::TraitRef
<'tcx
>>;
153 fn impl_polarity(&self, def
: DefId
) -> hir
::ImplPolarity
;
154 fn custom_coerce_unsized_kind(&self, def
: DefId
)
155 -> Option
<ty
::adjustment
::CustomCoerceUnsized
>;
156 fn impl_parent(&self, impl_def_id
: DefId
) -> Option
<DefId
>;
158 // trait/impl-item info
159 fn trait_of_item(&self, def_id
: DefId
) -> Option
<DefId
>;
160 fn impl_or_trait_item
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
)
161 -> Option
<ty
::ImplOrTraitItem
<'tcx
>>;
164 fn is_const_fn(&self, did
: DefId
) -> bool
;
165 fn is_defaulted_trait(&self, did
: DefId
) -> bool
;
166 fn is_default_impl(&self, impl_did
: DefId
) -> bool
;
167 fn is_foreign_item(&self, did
: DefId
) -> bool
;
168 fn is_statically_included_foreign_item(&self, id
: ast
::NodeId
) -> bool
;
171 fn dylib_dependency_formats(&self, cnum
: CrateNum
)
172 -> Vec
<(CrateNum
, LinkagePreference
)>;
173 fn lang_items(&self, cnum
: CrateNum
) -> Vec
<(DefIndex
, usize)>;
174 fn missing_lang_items(&self, cnum
: CrateNum
) -> Vec
<lang_items
::LangItem
>;
175 fn is_staged_api(&self, cnum
: CrateNum
) -> bool
;
176 fn is_explicitly_linked(&self, cnum
: CrateNum
) -> bool
;
177 fn is_allocator(&self, cnum
: CrateNum
) -> bool
;
178 fn is_panic_runtime(&self, cnum
: CrateNum
) -> bool
;
179 fn is_compiler_builtins(&self, cnum
: CrateNum
) -> bool
;
180 fn panic_strategy(&self, cnum
: CrateNum
) -> PanicStrategy
;
181 fn extern_crate(&self, cnum
: CrateNum
) -> Option
<ExternCrate
>;
182 /// The name of the crate as it is referred to in source code of the current
184 fn crate_name(&self, cnum
: CrateNum
) -> InternedString
;
185 /// The name of the crate as it is stored in the crate's metadata.
186 fn original_crate_name(&self, cnum
: CrateNum
) -> InternedString
;
187 fn crate_hash(&self, cnum
: CrateNum
) -> Svh
;
188 fn crate_disambiguator(&self, cnum
: CrateNum
) -> InternedString
;
189 fn plugin_registrar_fn(&self, cnum
: CrateNum
) -> Option
<DefId
>;
190 fn native_libraries(&self, cnum
: CrateNum
) -> Vec
<(NativeLibraryKind
, String
)>;
191 fn reachable_ids(&self, cnum
: CrateNum
) -> Vec
<DefId
>;
192 fn is_no_builtins(&self, cnum
: CrateNum
) -> bool
;
195 fn def_index_for_def_key(&self,
199 fn def_key(&self, def
: DefId
) -> hir_map
::DefKey
;
200 fn relative_def_path(&self, def
: DefId
) -> Option
<hir_map
::DefPath
>;
201 fn struct_field_names(&self, def
: DefId
) -> Vec
<ast
::Name
>;
202 fn item_children(&self, did
: DefId
) -> Vec
<def
::Export
>;
205 fn maybe_get_item_ast
<'a
>(&'tcx
self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
)
206 -> Option
<(&'tcx InlinedItem
, ast
::NodeId
)>;
207 fn local_node_for_inlined_defid(&'tcx
self, def_id
: DefId
) -> Option
<ast
::NodeId
>;
208 fn defid_for_inlined_node(&'tcx
self, node_id
: ast
::NodeId
) -> Option
<DefId
>;
210 fn get_item_mir
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
) -> Mir
<'tcx
>;
211 fn is_item_mir_available(&self, def
: DefId
) -> bool
;
213 // This is basically a 1-based range of ints, which is a little
214 // silly - I may fix that.
215 fn crates(&self) -> Vec
<CrateNum
>;
216 fn used_libraries(&self) -> Vec
<(String
, NativeLibraryKind
)>;
217 fn used_link_args(&self) -> Vec
<String
>;
220 fn metadata_filename(&self) -> &str;
221 fn metadata_section_name(&self, target
: &Target
) -> &str;
222 fn used_crates(&self, prefer
: LinkagePreference
) -> Vec
<(CrateNum
, Option
<PathBuf
>)>;
223 fn used_crate_source(&self, cnum
: CrateNum
) -> CrateSource
;
224 fn extern_mod_stmt_cnum(&self, emod_id
: ast
::NodeId
) -> Option
<CrateNum
>;
225 fn encode_metadata
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>,
226 reexports
: &def
::ExportMap
,
227 link_meta
: &LinkMeta
,
228 reachable
: &NodeSet
) -> Vec
<u8>;
229 fn metadata_encoding_version(&self) -> &[u8];
233 pub fn visit
<'ast
,V
>(&'ast
self, visitor
: &mut V
)
234 where V
: Visitor
<'ast
>
237 InlinedItem
::Item(_
, ref i
) => visitor
.visit_item(&i
),
238 InlinedItem
::TraitItem(_
, ref ti
) => visitor
.visit_trait_item(ti
),
239 InlinedItem
::ImplItem(_
, ref ii
) => visitor
.visit_impl_item(ii
),
244 // FIXME: find a better place for this?
245 pub fn validate_crate_name(sess
: Option
<&Session
>, s
: &str, sp
: Option
<Span
>) {
246 let mut err_count
= 0;
248 let mut say
= |s
: &str| {
250 (_
, None
) => bug
!("{}", s
),
251 (Some(sp
), Some(sess
)) => sess
.span_err(sp
, s
),
252 (None
, Some(sess
)) => sess
.err(s
),
257 say("crate name must not be empty");
260 if c
.is_alphanumeric() { continue }
261 if c
== '_' { continue }
262 say(&format
!("invalid character `{}` in crate name: `{}`", c
, s
));
267 sess
.unwrap().abort_if_errors();
271 /// A dummy crate store that does not support any non-local crates,
272 /// for test purposes.
273 pub struct DummyCrateStore
;
274 #[allow(unused_variables)]
275 impl<'tcx
> CrateStore
<'tcx
> for DummyCrateStore
{
277 fn describe_def(&self, def
: DefId
) -> Option
<Def
> { bug!("describe_def") }
278 fn stability(&self, def
: DefId
) -> Option
<attr
::Stability
> { bug!("stability") }
279 fn deprecation(&self, def
: DefId
) -> Option
<attr
::Deprecation
> { bug!("deprecation") }
280 fn visibility(&self, def
: DefId
) -> ty
::Visibility { bug!("visibility") }
281 fn closure_kind(&self, def_id
: DefId
) -> ty
::ClosureKind { bug!("closure_kind") }
282 fn closure_ty
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def_id
: DefId
)
283 -> ty
::ClosureTy
<'tcx
> { bug!("closure_ty") }
284 fn item_variances(&self, def
: DefId
) -> Vec
<ty
::Variance
> { bug!("item_variances") }
285 fn item_type
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
)
286 -> Ty
<'tcx
> { bug!("item_type") }
287 fn visible_parent_map
<'a
>(&'a
self) -> ::std
::cell
::RefMut
<'a
, DefIdMap
<DefId
>> {
288 bug
!("visible_parent_map")
290 fn item_predicates
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
)
291 -> ty
::GenericPredicates
<'tcx
> { bug!("item_predicates") }
292 fn item_super_predicates
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
)
293 -> ty
::GenericPredicates
<'tcx
> { bug!("item_super_predicates") }
294 fn item_generics
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
)
295 -> ty
::Generics
<'tcx
> { bug!("item_generics") }
296 fn item_attrs(&self, def_id
: DefId
) -> Vec
<ast
::Attribute
> { bug!("item_attrs") }
297 fn trait_def
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
)-> ty
::TraitDef
<'tcx
>
298 { bug!("trait_def") }
299 fn adt_def
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
) -> ty
::AdtDefMaster
<'tcx
>
301 fn fn_arg_names(&self, did
: DefId
) -> Vec
<ast
::Name
> { bug!("fn_arg_names") }
302 fn inherent_implementations_for_type(&self, def_id
: DefId
) -> Vec
<DefId
> { vec![] }
305 fn implementations_of_trait(&self, filter
: Option
<DefId
>) -> Vec
<DefId
> { vec![] }
306 fn def_index_for_def_key(&self,
309 -> Option
<DefIndex
> {
314 fn impl_or_trait_items(&self, def_id
: DefId
) -> Vec
<DefId
>
315 { bug!("impl_or_trait_items") }
316 fn impl_trait_ref
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
)
317 -> Option
<ty
::TraitRef
<'tcx
>> { bug!("impl_trait_ref") }
318 fn impl_polarity(&self, def
: DefId
) -> hir
::ImplPolarity { bug!("impl_polarity") }
319 fn custom_coerce_unsized_kind(&self, def
: DefId
)
320 -> Option
<ty
::adjustment
::CustomCoerceUnsized
>
321 { bug!("custom_coerce_unsized_kind") }
322 fn impl_parent(&self, def
: DefId
) -> Option
<DefId
> { bug!("impl_parent") }
324 // trait/impl-item info
325 fn trait_of_item(&self, def_id
: DefId
) -> Option
<DefId
> { bug!("trait_of_item") }
326 fn impl_or_trait_item
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
)
327 -> Option
<ty
::ImplOrTraitItem
<'tcx
>> { bug!("impl_or_trait_item") }
330 fn is_const_fn(&self, did
: DefId
) -> bool { bug!("is_const_fn") }
331 fn is_defaulted_trait(&self, did
: DefId
) -> bool { bug!("is_defaulted_trait") }
332 fn is_default_impl(&self, impl_did
: DefId
) -> bool { bug!("is_default_impl") }
333 fn is_foreign_item(&self, did
: DefId
) -> bool { bug!("is_foreign_item") }
334 fn is_statically_included_foreign_item(&self, id
: ast
::NodeId
) -> bool { false }
337 fn dylib_dependency_formats(&self, cnum
: CrateNum
)
338 -> Vec
<(CrateNum
, LinkagePreference
)>
339 { bug!("dylib_dependency_formats") }
340 fn lang_items(&self, cnum
: CrateNum
) -> Vec
<(DefIndex
, usize)>
341 { bug!("lang_items") }
342 fn missing_lang_items(&self, cnum
: CrateNum
) -> Vec
<lang_items
::LangItem
>
343 { bug!("missing_lang_items") }
344 fn is_staged_api(&self, cnum
: CrateNum
) -> bool { bug!("is_staged_api") }
345 fn is_explicitly_linked(&self, cnum
: CrateNum
) -> bool { bug!("is_explicitly_linked") }
346 fn is_allocator(&self, cnum
: CrateNum
) -> bool { bug!("is_allocator") }
347 fn is_panic_runtime(&self, cnum
: CrateNum
) -> bool { bug!("is_panic_runtime") }
348 fn is_compiler_builtins(&self, cnum
: CrateNum
) -> bool { bug!("is_compiler_builtins") }
349 fn panic_strategy(&self, cnum
: CrateNum
) -> PanicStrategy
{
350 bug
!("panic_strategy")
352 fn extern_crate(&self, cnum
: CrateNum
) -> Option
<ExternCrate
> { bug!("extern_crate") }
353 fn crate_name(&self, cnum
: CrateNum
) -> InternedString { bug!("crate_name") }
354 fn original_crate_name(&self, cnum
: CrateNum
) -> InternedString
{
355 bug
!("original_crate_name")
357 fn crate_hash(&self, cnum
: CrateNum
) -> Svh { bug!("crate_hash") }
358 fn crate_disambiguator(&self, cnum
: CrateNum
)
359 -> InternedString { bug!("crate_disambiguator") }
360 fn plugin_registrar_fn(&self, cnum
: CrateNum
) -> Option
<DefId
>
361 { bug!("plugin_registrar_fn") }
362 fn native_libraries(&self, cnum
: CrateNum
) -> Vec
<(NativeLibraryKind
, String
)>
363 { bug!("native_libraries") }
364 fn reachable_ids(&self, cnum
: CrateNum
) -> Vec
<DefId
> { bug!("reachable_ids") }
365 fn is_no_builtins(&self, cnum
: CrateNum
) -> bool { bug!("is_no_builtins") }
368 fn def_key(&self, def
: DefId
) -> hir_map
::DefKey { bug!("def_key") }
369 fn relative_def_path(&self, def
: DefId
) -> Option
<hir_map
::DefPath
> {
370 bug
!("relative_def_path")
372 fn struct_field_names(&self, def
: DefId
) -> Vec
<ast
::Name
> { bug!("struct_field_names") }
373 fn item_children(&self, did
: DefId
) -> Vec
<def
::Export
> { bug!("item_children") }
376 fn maybe_get_item_ast
<'a
>(&'tcx
self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
)
377 -> Option
<(&'tcx InlinedItem
, ast
::NodeId
)> {
378 bug
!("maybe_get_item_ast")
380 fn local_node_for_inlined_defid(&'tcx
self, def_id
: DefId
) -> Option
<ast
::NodeId
> {
381 bug
!("local_node_for_inlined_defid")
383 fn defid_for_inlined_node(&'tcx
self, node_id
: ast
::NodeId
) -> Option
<DefId
> {
384 bug
!("defid_for_inlined_node")
387 fn get_item_mir
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>, def
: DefId
)
388 -> Mir
<'tcx
> { bug!("get_item_mir") }
389 fn is_item_mir_available(&self, def
: DefId
) -> bool
{
390 bug
!("is_item_mir_available")
393 // This is basically a 1-based range of ints, which is a little
394 // silly - I may fix that.
395 fn crates(&self) -> Vec
<CrateNum
> { vec![] }
396 fn used_libraries(&self) -> Vec
<(String
, NativeLibraryKind
)> { vec![] }
397 fn used_link_args(&self) -> Vec
<String
> { vec![] }
400 fn metadata_filename(&self) -> &str { bug!("metadata_filename") }
401 fn metadata_section_name(&self, target
: &Target
) -> &str { bug!("metadata_section_name") }
402 fn used_crates(&self, prefer
: LinkagePreference
) -> Vec
<(CrateNum
, Option
<PathBuf
>)>
404 fn used_crate_source(&self, cnum
: CrateNum
) -> CrateSource { bug!("used_crate_source") }
405 fn extern_mod_stmt_cnum(&self, emod_id
: ast
::NodeId
) -> Option
<CrateNum
> { None }
406 fn encode_metadata
<'a
>(&self, tcx
: TyCtxt
<'a
, 'tcx
, 'tcx
>,
407 reexports
: &def
::ExportMap
,
408 link_meta
: &LinkMeta
,
409 reachable
: &NodeSet
) -> Vec
<u8> { vec![] }
410 fn metadata_encoding_version(&self) -> &[u8] { bug!("metadata_encoding_version") }
413 pub enum LoadedMacros
{
414 MacroRules(Vec
<ast
::MacroDef
>),
415 ProcMacros(Vec
<(ast
::Name
, SyntaxExtension
)>),
419 pub fn is_proc_macros(&self) -> bool
{
421 LoadedMacros
::ProcMacros(_
) => true,
427 pub trait CrateLoader
{
428 fn process_item(&mut self, item
: &ast
::Item
, defs
: &Definitions
, load_macros
: bool
)
429 -> Option
<LoadedMacros
>;
430 fn postprocess(&mut self, krate
: &ast
::Crate
);