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::visit::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 pub trait CrateStore<'tcx> : Any {
128 fn stability(&self, def: DefId) -> Option<attr::Stability>;
129 fn closure_kind(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId)
131 fn closure_ty(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId)
132 -> ty::ClosureTy<'tcx>;
133 fn item_variances(&self, def: DefId) -> ty::ItemVariances;
134 fn repr_attrs(&self, def: DefId) -> Vec<attr::ReprAttr>;
135 fn item_type(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
136 -> ty::TypeScheme<'tcx>;
137 fn item_path(&self, def: DefId) -> Vec<hir_map::PathElem>;
138 fn item_name(&self, def: DefId) -> ast::Name;
139 fn item_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
140 -> ty::GenericPredicates<'tcx>;
141 fn item_super_predicates(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
142 -> ty::GenericPredicates<'tcx>;
143 fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>;
144 fn item_symbol(&self, def: DefId) -> String;
145 fn trait_def(&self, tcx: &ty::ctxt<'tcx>, def: DefId)-> ty::TraitDef<'tcx>;
146 fn adt_def(&self, tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>;
147 fn method_arg_names(&self, did: DefId) -> Vec<String>;
148 fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>;
151 fn implementations_of_trait(&self, def_id: DefId) -> Vec<DefId>;
152 fn provided_trait_methods(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
153 -> Vec<Rc<ty::Method<'tcx>>>;
154 fn trait_item_def_ids(&self, def: DefId)
155 -> Vec<ty::ImplOrTraitItemId>;
158 fn impl_items(&self, impl_def_id: DefId) -> Vec<ty::ImplOrTraitItemId>;
159 fn impl_trait_ref(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
160 -> Option<ty::TraitRef<'tcx>>;
161 fn impl_polarity(&self, def: DefId) -> Option<hir::ImplPolarity>;
162 fn custom_coerce_unsized_kind(&self, def: DefId)
163 -> Option<ty::adjustment::CustomCoerceUnsized>;
164 fn associated_consts(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
165 -> Vec<Rc<ty::AssociatedConst<'tcx>>>;
167 // trait/impl-item info
168 fn trait_of_item(&self, tcx: &ty::ctxt<'tcx>, def_id: DefId)
170 fn impl_or_trait_item(&self, tcx: &ty::ctxt<'tcx>, def: DefId)
171 -> ty::ImplOrTraitItem<'tcx>;
174 fn is_const_fn(&self, did: DefId) -> bool;
175 fn is_defaulted_trait(&self, did: DefId) -> bool;
176 fn is_impl(&self, did: DefId) -> bool;
177 fn is_default_impl(&self, impl_did: DefId) -> bool;
178 fn is_extern_fn(&self, tcx: &ty::ctxt<'tcx>, did: DefId) -> bool;
179 fn is_static(&self, did: DefId) -> bool;
180 fn is_static_method(&self, did: DefId) -> bool;
181 fn is_statically_included_foreign_item(&self, id: ast::NodeId) -> bool;
182 fn is_typedef(&self, did: DefId) -> bool;
185 fn dylib_dependency_formats(&self, cnum: ast::CrateNum)
186 -> Vec<(ast::CrateNum, LinkagePreference)>;
187 fn lang_items(&self, cnum: ast::CrateNum) -> Vec<(DefIndex, usize)>;
188 fn missing_lang_items(&self, cnum: ast::CrateNum) -> Vec<lang_items::LangItem>;
189 fn is_staged_api(&self, cnum: ast::CrateNum) -> bool;
190 fn is_explicitly_linked(&self, cnum: ast::CrateNum) -> bool;
191 fn is_allocator(&self, cnum: ast::CrateNum) -> bool;
192 fn crate_attrs(&self, cnum: ast::CrateNum) -> Vec<ast::Attribute>;
193 fn crate_name(&self, cnum: ast::CrateNum) -> String;
194 fn crate_hash(&self, cnum: ast::CrateNum) -> Svh;
195 fn crate_struct_field_attrs(&self, cnum: ast::CrateNum)
196 -> FnvHashMap<DefId, Vec<ast::Attribute>>;
197 fn plugin_registrar_fn(&self, cnum: ast::CrateNum) -> Option<DefId>;
198 fn native_libraries(&self, cnum: ast::CrateNum) -> Vec<(NativeLibraryKind, String)>;
199 fn reachable_ids(&self, cnum: ast::CrateNum) -> Vec<DefId>;
202 fn def_path(&self, def: DefId) -> hir_map::DefPath;
203 fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>;
204 fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
205 fn item_children(&self, did: DefId) -> Vec<ChildItem>;
206 fn crate_top_level_items(&self, cnum: ast::CrateNum) -> Vec<ChildItem>;
209 fn maybe_get_item_ast(&'tcx self, tcx: &ty::ctxt<'tcx>, def: DefId)
211 // This is basically a 1-based range of ints, which is a little
212 // silly - I may fix that.
213 fn crates(&self) -> Vec<ast::CrateNum>;
214 fn used_libraries(&self) -> Vec<(String, NativeLibraryKind)>;
215 fn used_link_args(&self) -> Vec<String>;
218 fn metadata_filename(&self) -> &str;
219 fn metadata_section_name(&self, target: &Target) -> &str;
220 fn encode_type(&self, tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> Vec<u8>;
221 fn used_crates(&self, prefer: LinkagePreference) -> Vec<(ast::CrateNum, Option<PathBuf>)>;
222 fn used_crate_source(&self, cnum: ast::CrateNum) -> CrateSource;
223 fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<ast::CrateNum>;
224 fn encode_metadata(&self,
225 tcx: &ty::ctxt<'tcx>,
226 reexports: &def::ExportMap,
227 item_symbols: &RefCell<NodeMap<String>>,
228 link_meta: &LinkMeta,
230 krate: &hir::Crate) -> Vec<u8>;
231 fn metadata_encoding_version(&self) -> &[u8];
235 pub fn visit<'ast,V>(&'ast self, visitor: &mut V)
236 where V: Visitor<'ast>
239 InlinedItem::Item(ref i) => visitor.visit_item(&**i),
240 InlinedItem::Foreign(ref i) => visitor.visit_foreign_item(&**i),
241 InlinedItem::TraitItem(_, ref ti) => visitor.visit_trait_item(ti),
242 InlinedItem::ImplItem(_, ref ii) => visitor.visit_impl_item(ii),
246 pub fn visit_ids<O: IdVisitingOperation>(&self, operation: &mut O) {
247 let mut id_visitor = IdVisitor {
248 operation: operation,
249 pass_through_items: true,
250 visited_outermost: false,
252 self.visit(&mut id_visitor);
256 // FIXME: find a better place for this?
257 pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option<Span>) {
258 let say = |s: &str| {
260 (_, None) => panic!("{}", s),
261 (Some(sp), Some(sess)) => sess.span_err(sp, s),
262 (None, Some(sess)) => sess.err(s),
266 say("crate name must not be empty");
269 if c.is_alphanumeric() { continue }
270 if c == '_' { continue }
271 say(&format!("invalid character `{}` in crate name: `{}`", c, s));
274 Some(sess) => sess.abort_if_errors(),