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.
26 use front
::map
as hir_map
;
28 use middle
::lang_items
;
29 use middle
::ty
::{self, Ty}
;
30 use middle
::def_id
::{DefId, DefIndex}
;
32 use session
::search_paths
::PathKind
;
33 use util
::nodemap
::{FnvHashMap, NodeMap, NodeSet}
;
35 use std
::cell
::RefCell
;
37 use std
::path
::PathBuf
;
39 use syntax
::ast_util
::{IdVisitingOperation}
;
41 use syntax
::codemap
::Span
;
43 use rustc_back
::target
::Target
;
45 use rustc_front
::intravisit
::Visitor
;
46 use rustc_front
::util
::IdVisitor
;
48 pub use self::DefLike
::{DlDef, DlField, DlImpl}
;
49 pub use self::NativeLibraryKind
::{NativeStatic, NativeFramework, NativeUnknown}
;
51 // lonely orphan structs and enums looking for a better home
53 #[derive(Clone, Debug)]
55 pub crate_name
: String
,
59 // Where a crate came from on the local filesystem. One of these two options
61 #[derive(PartialEq, Clone, Debug)]
62 pub struct CrateSource
{
63 pub dylib
: Option
<(PathBuf
, PathKind
)>,
64 pub rlib
: Option
<(PathBuf
, PathKind
)>,
65 pub cnum
: ast
::CrateNum
,
68 #[derive(Copy, Debug, PartialEq, Clone)]
69 pub enum LinkagePreference
{
75 #[derive(Copy, Clone, PartialEq)]
76 pub enum NativeLibraryKind
{
77 NativeStatic
, // native static library (.a archive)
78 NativeFramework
, // OSX-specific
79 NativeUnknown
, // default way to specify a dynamic library
83 // Something that a name can resolve to.
84 #[derive(Copy, Clone, Debug)]
91 /// The data we save and restore about an inlined item or method. This is not
92 /// part of the AST that we parse from a file, but it becomes part of the tree
94 #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
95 pub enum InlinedItem
{
97 TraitItem(DefId
/* impl id */, P
<hir
::TraitItem
>),
98 ImplItem(DefId
/* impl id */, P
<hir
::ImplItem
>),
99 Foreign(P
<hir
::ForeignItem
>),
102 /// A borrowed version of `hir::InlinedItem`.
103 pub enum InlinedItemRef
<'a
> {
105 TraitItem(DefId
, &'a hir
::TraitItem
),
106 ImplItem(DefId
, &'a hir
::ImplItem
),
107 Foreign(&'a hir
::ForeignItem
)
110 /// Item definitions in the currently-compiled crate would have the CrateNum
111 /// LOCAL_CRATE in their DefId.
112 pub const LOCAL_CRATE
: ast
::CrateNum
= 0;
114 pub struct ChildItem
{
117 pub vis
: hir
::Visibility
120 pub enum FoundAst
<'ast
> {
121 Found(&'ast InlinedItem
),
122 FoundParent(DefId
, &'ast InlinedItem
),
126 /// A store of Rust crates, through with their metadata
129 /// The `: Any` bound is a temporary measure that allows access
130 /// to the backing `rustc_metadata::cstore::CStore` object. It
131 /// will be removed in the near future - if you need to access
132 /// internal APIs, please tell us.
133 pub trait CrateStore
<'tcx
> : Any
{
135 fn stability(&self, def
: DefId
) -> Option
<attr
::Stability
>;
136 fn closure_kind(&self, tcx
: &ty
::ctxt
<'tcx
>, def_id
: DefId
)
138 fn closure_ty(&self, tcx
: &ty
::ctxt
<'tcx
>, def_id
: DefId
)
139 -> ty
::ClosureTy
<'tcx
>;
140 fn item_variances(&self, def
: DefId
) -> ty
::ItemVariances
;
141 fn repr_attrs(&self, def
: DefId
) -> Vec
<attr
::ReprAttr
>;
142 fn item_type(&self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
)
143 -> ty
::TypeScheme
<'tcx
>;
144 fn item_path(&self, def
: DefId
) -> Vec
<hir_map
::PathElem
>;
145 fn extern_item_path(&self, def
: DefId
) -> Vec
<hir_map
::PathElem
>;
146 fn item_name(&self, def
: DefId
) -> ast
::Name
;
147 fn item_predicates(&self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
)
148 -> ty
::GenericPredicates
<'tcx
>;
149 fn item_super_predicates(&self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
)
150 -> ty
::GenericPredicates
<'tcx
>;
151 fn item_attrs(&self, def_id
: DefId
) -> Vec
<ast
::Attribute
>;
152 fn item_symbol(&self, def
: DefId
) -> String
;
153 fn trait_def(&self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
)-> ty
::TraitDef
<'tcx
>;
154 fn adt_def(&self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
) -> ty
::AdtDefMaster
<'tcx
>;
155 fn method_arg_names(&self, did
: DefId
) -> Vec
<String
>;
156 fn inherent_implementations_for_type(&self, def_id
: DefId
) -> Vec
<DefId
>;
159 fn implementations_of_trait(&self, def_id
: DefId
) -> Vec
<DefId
>;
160 fn provided_trait_methods(&self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
)
161 -> Vec
<Rc
<ty
::Method
<'tcx
>>>;
162 fn trait_item_def_ids(&self, def
: DefId
)
163 -> Vec
<ty
::ImplOrTraitItemId
>;
166 fn impl_items(&self, impl_def_id
: DefId
) -> Vec
<ty
::ImplOrTraitItemId
>;
167 fn impl_trait_ref(&self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
)
168 -> Option
<ty
::TraitRef
<'tcx
>>;
169 fn impl_polarity(&self, def
: DefId
) -> Option
<hir
::ImplPolarity
>;
170 fn custom_coerce_unsized_kind(&self, def
: DefId
)
171 -> Option
<ty
::adjustment
::CustomCoerceUnsized
>;
172 fn associated_consts(&self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
)
173 -> Vec
<Rc
<ty
::AssociatedConst
<'tcx
>>>;
175 // trait/impl-item info
176 fn trait_of_item(&self, tcx
: &ty
::ctxt
<'tcx
>, def_id
: DefId
)
178 fn impl_or_trait_item(&self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
)
179 -> ty
::ImplOrTraitItem
<'tcx
>;
182 fn is_const_fn(&self, did
: DefId
) -> bool
;
183 fn is_defaulted_trait(&self, did
: DefId
) -> bool
;
184 fn is_impl(&self, did
: DefId
) -> bool
;
185 fn is_default_impl(&self, impl_did
: DefId
) -> bool
;
186 fn is_extern_fn(&self, tcx
: &ty
::ctxt
<'tcx
>, did
: DefId
) -> bool
;
187 fn is_static(&self, did
: DefId
) -> bool
;
188 fn is_static_method(&self, did
: DefId
) -> bool
;
189 fn is_statically_included_foreign_item(&self, id
: ast
::NodeId
) -> bool
;
190 fn is_typedef(&self, did
: DefId
) -> bool
;
193 fn dylib_dependency_formats(&self, cnum
: ast
::CrateNum
)
194 -> Vec
<(ast
::CrateNum
, LinkagePreference
)>;
195 fn lang_items(&self, cnum
: ast
::CrateNum
) -> Vec
<(DefIndex
, usize)>;
196 fn missing_lang_items(&self, cnum
: ast
::CrateNum
) -> Vec
<lang_items
::LangItem
>;
197 fn is_staged_api(&self, cnum
: ast
::CrateNum
) -> bool
;
198 fn is_explicitly_linked(&self, cnum
: ast
::CrateNum
) -> bool
;
199 fn is_allocator(&self, cnum
: ast
::CrateNum
) -> bool
;
200 fn crate_attrs(&self, cnum
: ast
::CrateNum
) -> Vec
<ast
::Attribute
>;
201 fn crate_name(&self, cnum
: ast
::CrateNum
) -> String
;
202 fn crate_hash(&self, cnum
: ast
::CrateNum
) -> Svh
;
203 fn crate_struct_field_attrs(&self, cnum
: ast
::CrateNum
)
204 -> FnvHashMap
<DefId
, Vec
<ast
::Attribute
>>;
205 fn plugin_registrar_fn(&self, cnum
: ast
::CrateNum
) -> Option
<DefId
>;
206 fn native_libraries(&self, cnum
: ast
::CrateNum
) -> Vec
<(NativeLibraryKind
, String
)>;
207 fn reachable_ids(&self, cnum
: ast
::CrateNum
) -> Vec
<DefId
>;
210 fn def_path(&self, def
: DefId
) -> hir_map
::DefPath
;
211 fn tuple_struct_definition_if_ctor(&self, did
: DefId
) -> Option
<DefId
>;
212 fn struct_field_names(&self, def
: DefId
) -> Vec
<ast
::Name
>;
213 fn item_children(&self, did
: DefId
) -> Vec
<ChildItem
>;
214 fn crate_top_level_items(&self, cnum
: ast
::CrateNum
) -> Vec
<ChildItem
>;
217 fn maybe_get_item_ast(&'tcx
self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
)
219 // This is basically a 1-based range of ints, which is a little
220 // silly - I may fix that.
221 fn crates(&self) -> Vec
<ast
::CrateNum
>;
222 fn used_libraries(&self) -> Vec
<(String
, NativeLibraryKind
)>;
223 fn used_link_args(&self) -> Vec
<String
>;
226 fn metadata_filename(&self) -> &str;
227 fn metadata_section_name(&self, target
: &Target
) -> &str;
228 fn encode_type(&self, tcx
: &ty
::ctxt
<'tcx
>, ty
: Ty
<'tcx
>) -> Vec
<u8>;
229 fn used_crates(&self, prefer
: LinkagePreference
) -> Vec
<(ast
::CrateNum
, Option
<PathBuf
>)>;
230 fn used_crate_source(&self, cnum
: ast
::CrateNum
) -> CrateSource
;
231 fn extern_mod_stmt_cnum(&self, emod_id
: ast
::NodeId
) -> Option
<ast
::CrateNum
>;
232 fn encode_metadata(&self,
233 tcx
: &ty
::ctxt
<'tcx
>,
234 reexports
: &def
::ExportMap
,
235 item_symbols
: &RefCell
<NodeMap
<String
>>,
236 link_meta
: &LinkMeta
,
238 krate
: &hir
::Crate
) -> Vec
<u8>;
239 fn metadata_encoding_version(&self) -> &[u8];
243 pub fn visit
<'ast
,V
>(&'ast
self, visitor
: &mut V
)
244 where V
: Visitor
<'ast
>
247 InlinedItem
::Item(ref i
) => visitor
.visit_item(&**i
),
248 InlinedItem
::Foreign(ref i
) => visitor
.visit_foreign_item(&**i
),
249 InlinedItem
::TraitItem(_
, ref ti
) => visitor
.visit_trait_item(ti
),
250 InlinedItem
::ImplItem(_
, ref ii
) => visitor
.visit_impl_item(ii
),
254 pub fn visit_ids
<O
: IdVisitingOperation
>(&self, operation
: &mut O
) {
255 let mut id_visitor
= IdVisitor
::new(operation
);
256 self.visit(&mut id_visitor
);
260 // FIXME: find a better place for this?
261 pub fn validate_crate_name(sess
: Option
<&Session
>, s
: &str, sp
: Option
<Span
>) {
262 let say
= |s
: &str| {
264 (_
, None
) => panic
!("{}", s
),
265 (Some(sp
), Some(sess
)) => sess
.span_err(sp
, s
),
266 (None
, Some(sess
)) => sess
.err(s
),
270 say("crate name must not be empty");
273 if c
.is_alphanumeric() { continue }
274 if c
== '_' { continue }
275 say(&format
!("invalid character `{}` in crate name: `{}`", c
, s
));
278 Some(sess
) => sess
.abort_if_errors(),
283 /// A dummy crate store that does not support any non-local crates,
284 /// for test purposes.
285 pub struct DummyCrateStore
;
286 #[allow(unused_variables)]
287 impl<'tcx
> CrateStore
<'tcx
> for DummyCrateStore
{
289 fn stability(&self, def
: DefId
) -> Option
<attr
::Stability
> { unimplemented!() }
290 fn closure_kind(&self, tcx
: &ty
::ctxt
<'tcx
>, def_id
: DefId
)
291 -> ty
::ClosureKind { unimplemented!() }
292 fn closure_ty(&self, tcx
: &ty
::ctxt
<'tcx
>, def_id
: DefId
)
293 -> ty
::ClosureTy
<'tcx
> { unimplemented!() }
294 fn item_variances(&self, def
: DefId
) -> ty
::ItemVariances { unimplemented!() }
295 fn repr_attrs(&self, def
: DefId
) -> Vec
<attr
::ReprAttr
> { unimplemented!() }
296 fn item_type(&self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
)
297 -> ty
::TypeScheme
<'tcx
> { unimplemented!() }
298 fn item_path(&self, def
: DefId
) -> Vec
<hir_map
::PathElem
> { unimplemented!() }
299 fn extern_item_path(&self, def
: DefId
) -> Vec
<hir_map
::PathElem
> { unimplemented!() }
300 fn item_name(&self, def
: DefId
) -> ast
::Name { unimplemented!() }
301 fn item_predicates(&self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
)
302 -> ty
::GenericPredicates
<'tcx
> { unimplemented!() }
303 fn item_super_predicates(&self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
)
304 -> ty
::GenericPredicates
<'tcx
> { unimplemented!() }
305 fn item_attrs(&self, def_id
: DefId
) -> Vec
<ast
::Attribute
> { unimplemented!() }
306 fn item_symbol(&self, def
: DefId
) -> String { unimplemented!() }
307 fn trait_def(&self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
)-> ty
::TraitDef
<'tcx
>
309 fn adt_def(&self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
) -> ty
::AdtDefMaster
<'tcx
>
311 fn method_arg_names(&self, did
: DefId
) -> Vec
<String
> { unimplemented!() }
312 fn inherent_implementations_for_type(&self, def_id
: DefId
) -> Vec
<DefId
> { vec![] }
315 fn implementations_of_trait(&self, def_id
: DefId
) -> Vec
<DefId
> { vec![] }
316 fn provided_trait_methods(&self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
)
317 -> Vec
<Rc
<ty
::Method
<'tcx
>>> { unimplemented!() }
318 fn trait_item_def_ids(&self, def
: DefId
)
319 -> Vec
<ty
::ImplOrTraitItemId
> { unimplemented!() }
322 fn impl_items(&self, impl_def_id
: DefId
) -> Vec
<ty
::ImplOrTraitItemId
>
324 fn impl_trait_ref(&self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
)
325 -> Option
<ty
::TraitRef
<'tcx
>> { unimplemented!() }
326 fn impl_polarity(&self, def
: DefId
) -> Option
<hir
::ImplPolarity
> { unimplemented!() }
327 fn custom_coerce_unsized_kind(&self, def
: DefId
)
328 -> Option
<ty
::adjustment
::CustomCoerceUnsized
>
330 fn associated_consts(&self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
)
331 -> Vec
<Rc
<ty
::AssociatedConst
<'tcx
>>> { unimplemented!() }
333 // trait/impl-item info
334 fn trait_of_item(&self, tcx
: &ty
::ctxt
<'tcx
>, def_id
: DefId
)
335 -> Option
<DefId
> { unimplemented!() }
336 fn impl_or_trait_item(&self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
)
337 -> ty
::ImplOrTraitItem
<'tcx
> { unimplemented!() }
340 fn is_const_fn(&self, did
: DefId
) -> bool { unimplemented!() }
341 fn is_defaulted_trait(&self, did
: DefId
) -> bool { unimplemented!() }
342 fn is_impl(&self, did
: DefId
) -> bool { unimplemented!() }
343 fn is_default_impl(&self, impl_did
: DefId
) -> bool { unimplemented!() }
344 fn is_extern_fn(&self, tcx
: &ty
::ctxt
<'tcx
>, did
: DefId
) -> bool { unimplemented!() }
345 fn is_static(&self, did
: DefId
) -> bool { unimplemented!() }
346 fn is_static_method(&self, did
: DefId
) -> bool { unimplemented!() }
347 fn is_statically_included_foreign_item(&self, id
: ast
::NodeId
) -> bool { false }
348 fn is_typedef(&self, did
: DefId
) -> bool { unimplemented!() }
351 fn dylib_dependency_formats(&self, cnum
: ast
::CrateNum
)
352 -> Vec
<(ast
::CrateNum
, LinkagePreference
)>
354 fn lang_items(&self, cnum
: ast
::CrateNum
) -> Vec
<(DefIndex
, usize)>
356 fn missing_lang_items(&self, cnum
: ast
::CrateNum
) -> Vec
<lang_items
::LangItem
>
358 fn is_staged_api(&self, cnum
: ast
::CrateNum
) -> bool { unimplemented!() }
359 fn is_explicitly_linked(&self, cnum
: ast
::CrateNum
) -> bool { unimplemented!() }
360 fn is_allocator(&self, cnum
: ast
::CrateNum
) -> bool { unimplemented!() }
361 fn crate_attrs(&self, cnum
: ast
::CrateNum
) -> Vec
<ast
::Attribute
>
363 fn crate_name(&self, cnum
: ast
::CrateNum
) -> String { unimplemented!() }
364 fn crate_hash(&self, cnum
: ast
::CrateNum
) -> Svh { unimplemented!() }
365 fn crate_struct_field_attrs(&self, cnum
: ast
::CrateNum
)
366 -> FnvHashMap
<DefId
, Vec
<ast
::Attribute
>>
368 fn plugin_registrar_fn(&self, cnum
: ast
::CrateNum
) -> Option
<DefId
>
370 fn native_libraries(&self, cnum
: ast
::CrateNum
) -> Vec
<(NativeLibraryKind
, String
)>
372 fn reachable_ids(&self, cnum
: ast
::CrateNum
) -> Vec
<DefId
> { unimplemented!() }
375 fn def_path(&self, def
: DefId
) -> hir_map
::DefPath { unimplemented!() }
376 fn tuple_struct_definition_if_ctor(&self, did
: DefId
) -> Option
<DefId
>
378 fn struct_field_names(&self, def
: DefId
) -> Vec
<ast
::Name
> { unimplemented!() }
379 fn item_children(&self, did
: DefId
) -> Vec
<ChildItem
> { unimplemented!() }
380 fn crate_top_level_items(&self, cnum
: ast
::CrateNum
) -> Vec
<ChildItem
>
384 fn maybe_get_item_ast(&'tcx
self, tcx
: &ty
::ctxt
<'tcx
>, def
: DefId
)
385 -> FoundAst
<'tcx
> { unimplemented!() }
386 // This is basically a 1-based range of ints, which is a little
387 // silly - I may fix that.
388 fn crates(&self) -> Vec
<ast
::CrateNum
> { vec![] }
389 fn used_libraries(&self) -> Vec
<(String
, NativeLibraryKind
)> { vec![] }
390 fn used_link_args(&self) -> Vec
<String
> { vec![] }
393 fn metadata_filename(&self) -> &str { unimplemented!() }
394 fn metadata_section_name(&self, target
: &Target
) -> &str { unimplemented!() }
395 fn encode_type(&self, tcx
: &ty
::ctxt
<'tcx
>, ty
: Ty
<'tcx
>) -> Vec
<u8>
397 fn used_crates(&self, prefer
: LinkagePreference
) -> Vec
<(ast
::CrateNum
, Option
<PathBuf
>)>
399 fn used_crate_source(&self, cnum
: ast
::CrateNum
) -> CrateSource { unimplemented!() }
400 fn extern_mod_stmt_cnum(&self, emod_id
: ast
::NodeId
) -> Option
<ast
::CrateNum
> { None }
401 fn encode_metadata(&self,
402 tcx
: &ty
::ctxt
<'tcx
>,
403 reexports
: &def
::ExportMap
,
404 item_symbols
: &RefCell
<NodeMap
<String
>>,
405 link_meta
: &LinkMeta
,
407 krate
: &hir
::Crate
) -> Vec
<u8> { vec![] }
408 fn metadata_encoding_version(&self) -> &[u8] { unimplemented!() }