]> git.lizzy.rs Git - rust.git/blob - src/librustc/metadata/csearch.rs
Auto merge of #28869 - alexcrichton:allocator-dox, r=steveklabnik
[rust.git] / src / librustc / metadata / csearch.rs
1 // Copyright 2012-2014 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.
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
11 // Searching for information from the cstore
12
13 use front::map as ast_map;
14 use metadata::cstore;
15 use metadata::decoder;
16 use metadata::inline::InlinedItem;
17 use middle::def_id::{DefId, DefIndex};
18 use middle::lang_items;
19 use middle::ty;
20 use util::nodemap::FnvHashMap;
21
22 use std::rc::Rc;
23 use syntax::ast;
24 use syntax::attr;
25 use rustc_front::hir;
26
27 #[derive(Copy, Clone)]
28 pub struct MethodInfo {
29     pub name: ast::Name,
30     pub def_id: DefId,
31     pub vis: hir::Visibility,
32 }
33
34 pub fn get_symbol(cstore: &cstore::CStore, def: DefId) -> String {
35     let cdata = cstore.get_crate_data(def.krate);
36     decoder::get_symbol(&cdata, def.index)
37 }
38
39 /// Iterates over all the language items in the given crate.
40 pub fn each_lang_item<F>(cstore: &cstore::CStore,
41                          cnum: ast::CrateNum,
42                          f: F)
43                          -> bool where
44     F: FnMut(DefIndex, usize) -> bool,
45 {
46     let crate_data = cstore.get_crate_data(cnum);
47     decoder::each_lang_item(&*crate_data, f)
48 }
49
50 /// Iterates over each child of the given item.
51 pub fn each_child_of_item<F>(cstore: &cstore::CStore,
52                              def_id: DefId,
53                              callback: F) where
54     F: FnMut(decoder::DefLike, ast::Name, hir::Visibility),
55 {
56     let crate_data = cstore.get_crate_data(def_id.krate);
57     let get_crate_data = |cnum| {
58         cstore.get_crate_data(cnum)
59     };
60     decoder::each_child_of_item(cstore.intr.clone(),
61                                 &*crate_data,
62                                 def_id.index,
63                                 get_crate_data,
64                                 callback)
65 }
66
67 /// Iterates over each top-level crate item.
68 pub fn each_top_level_item_of_crate<F>(cstore: &cstore::CStore,
69                                        cnum: ast::CrateNum,
70                                        callback: F) where
71     F: FnMut(decoder::DefLike, ast::Name, hir::Visibility),
72 {
73     let crate_data = cstore.get_crate_data(cnum);
74     let get_crate_data = |cnum| {
75         cstore.get_crate_data(cnum)
76     };
77     decoder::each_top_level_item_of_crate(cstore.intr.clone(),
78                                           &*crate_data,
79                                           get_crate_data,
80                                           callback)
81 }
82
83 pub fn get_item_path(tcx: &ty::ctxt, def: DefId) -> Vec<ast_map::PathElem> {
84     let cstore = &tcx.sess.cstore;
85     let cdata = cstore.get_crate_data(def.krate);
86     let path = decoder::get_item_path(&*cdata, def.index);
87
88     cdata.with_local_path(|cpath| {
89         let mut r = Vec::with_capacity(cpath.len() + path.len());
90         r.push_all(cpath);
91         r.push_all(&path);
92         r
93     })
94 }
95
96 pub fn get_item_name(tcx: &ty::ctxt, def: DefId) -> ast::Name {
97     let cstore = &tcx.sess.cstore;
98     let cdata = cstore.get_crate_data(def.krate);
99     decoder::get_item_name(&cstore.intr, &cdata, def.index)
100 }
101
102 pub enum FoundAst<'ast> {
103     Found(&'ast InlinedItem),
104     FoundParent(DefId, &'ast InlinedItem),
105     NotFound,
106 }
107
108 // Finds the AST for this item in the crate metadata, if any.  If the item was
109 // not marked for inlining, then the AST will not be present and hence none
110 // will be returned.
111 pub fn maybe_get_item_ast<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId,
112                                 decode_inlined_item: decoder::DecodeInlinedItem)
113                                 -> FoundAst<'tcx> {
114     let cstore = &tcx.sess.cstore;
115     let cdata = cstore.get_crate_data(def.krate);
116     decoder::maybe_get_item_ast(&*cdata, tcx, def.index, decode_inlined_item)
117 }
118
119 /// Returns information about the given implementation.
120 pub fn get_impl_items(cstore: &cstore::CStore, impl_def_id: DefId)
121                       -> Vec<ty::ImplOrTraitItemId> {
122     let cdata = cstore.get_crate_data(impl_def_id.krate);
123     decoder::get_impl_items(&*cdata, impl_def_id.index)
124 }
125
126 pub fn get_impl_or_trait_item<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId)
127                                     -> ty::ImplOrTraitItem<'tcx> {
128     let cdata = tcx.sess.cstore.get_crate_data(def.krate);
129     decoder::get_impl_or_trait_item(tcx.sess.cstore.intr.clone(),
130                                     &*cdata,
131                                     def.index,
132                                     tcx)
133 }
134
135 pub fn get_trait_name(cstore: &cstore::CStore, def: DefId) -> ast::Name {
136     let cdata = cstore.get_crate_data(def.krate);
137     decoder::get_trait_name(cstore.intr.clone(),
138                             &*cdata,
139                             def.index)
140 }
141
142 pub fn is_static_method(cstore: &cstore::CStore, def: DefId) -> bool {
143     let cdata = cstore.get_crate_data(def.krate);
144     decoder::is_static_method(&*cdata, def.index)
145 }
146
147 pub fn get_trait_item_def_ids(cstore: &cstore::CStore, def: DefId)
148                               -> Vec<ty::ImplOrTraitItemId> {
149     let cdata = cstore.get_crate_data(def.krate);
150     decoder::get_trait_item_def_ids(&*cdata, def.index)
151 }
152
153 pub fn get_item_variances(cstore: &cstore::CStore,
154                           def: DefId) -> ty::ItemVariances {
155     let cdata = cstore.get_crate_data(def.krate);
156     decoder::get_item_variances(&*cdata, def.index)
157 }
158
159 pub fn get_provided_trait_methods<'tcx>(tcx: &ty::ctxt<'tcx>,
160                                         def: DefId)
161                                         -> Vec<Rc<ty::Method<'tcx>>> {
162     let cstore = &tcx.sess.cstore;
163     let cdata = cstore.get_crate_data(def.krate);
164     decoder::get_provided_trait_methods(cstore.intr.clone(), &*cdata, def.index, tcx)
165 }
166
167 pub fn get_associated_consts<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId)
168                                    -> Vec<Rc<ty::AssociatedConst<'tcx>>> {
169     let cstore = &tcx.sess.cstore;
170     let cdata = cstore.get_crate_data(def.krate);
171     decoder::get_associated_consts(cstore.intr.clone(), &*cdata, def.index, tcx)
172 }
173
174 pub fn get_methods_if_impl(cstore: &cstore::CStore,
175                                   def: DefId)
176                                -> Option<Vec<MethodInfo> > {
177     let cdata = cstore.get_crate_data(def.krate);
178     decoder::get_methods_if_impl(cstore.intr.clone(), &*cdata, def.index)
179 }
180
181 pub fn get_item_attrs(cstore: &cstore::CStore,
182                       def_id: DefId)
183                       -> Vec<ast::Attribute> {
184     let cdata = cstore.get_crate_data(def_id.krate);
185     decoder::get_item_attrs(&*cdata, def_id.index)
186 }
187
188 pub fn get_struct_field_names(cstore: &cstore::CStore, def: DefId) -> Vec<ast::Name> {
189     let cdata = cstore.get_crate_data(def.krate);
190     decoder::get_struct_field_names(&cstore.intr, &*cdata, def.index)
191 }
192
193 pub fn get_struct_field_attrs(cstore: &cstore::CStore, def: DefId)
194                               -> FnvHashMap<DefId, Vec<ast::Attribute>> {
195     let cdata = cstore.get_crate_data(def.krate);
196     decoder::get_struct_field_attrs(&*cdata)
197 }
198
199 pub fn get_type<'tcx>(tcx: &ty::ctxt<'tcx>,
200                       def: DefId)
201                       -> ty::TypeScheme<'tcx> {
202     let cstore = &tcx.sess.cstore;
203     let cdata = cstore.get_crate_data(def.krate);
204     decoder::get_type(&*cdata, def.index, tcx)
205 }
206
207 pub fn get_trait_def<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::TraitDef<'tcx> {
208     let cstore = &tcx.sess.cstore;
209     let cdata = cstore.get_crate_data(def.krate);
210     decoder::get_trait_def(&*cdata, def.index, tcx)
211 }
212
213 pub fn get_adt_def<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx> {
214     let cstore = &tcx.sess.cstore;
215     let cdata = cstore.get_crate_data(def.krate);
216     decoder::get_adt_def(&cstore.intr, &*cdata, def.index, tcx)
217 }
218
219 pub fn get_predicates<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId)
220                             -> ty::GenericPredicates<'tcx>
221 {
222     let cstore = &tcx.sess.cstore;
223     let cdata = cstore.get_crate_data(def.krate);
224     decoder::get_predicates(&*cdata, def.index, tcx)
225 }
226
227 pub fn get_super_predicates<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId)
228                                   -> ty::GenericPredicates<'tcx>
229 {
230     let cstore = &tcx.sess.cstore;
231     let cdata = cstore.get_crate_data(def.krate);
232     decoder::get_super_predicates(&*cdata, def.index, tcx)
233 }
234
235 pub fn get_impl_polarity<'tcx>(tcx: &ty::ctxt<'tcx>,
236                                def: DefId)
237                                -> Option<hir::ImplPolarity>
238 {
239     let cstore = &tcx.sess.cstore;
240     let cdata = cstore.get_crate_data(def.krate);
241     decoder::get_impl_polarity(&*cdata, def.index)
242 }
243
244 pub fn get_custom_coerce_unsized_kind<'tcx>(
245     tcx: &ty::ctxt<'tcx>,
246     def: DefId)
247     -> Option<ty::adjustment::CustomCoerceUnsized>
248 {
249     let cstore = &tcx.sess.cstore;
250     let cdata = cstore.get_crate_data(def.krate);
251     decoder::get_custom_coerce_unsized_kind(&*cdata, def.index)
252 }
253
254 // Given a def_id for an impl, return the trait it implements,
255 // if there is one.
256 pub fn get_impl_trait<'tcx>(tcx: &ty::ctxt<'tcx>,
257                             def: DefId)
258                             -> Option<ty::TraitRef<'tcx>> {
259     let cstore = &tcx.sess.cstore;
260     let cdata = cstore.get_crate_data(def.krate);
261     decoder::get_impl_trait(&*cdata, def.index, tcx)
262 }
263
264 pub fn get_native_libraries(cstore: &cstore::CStore, crate_num: ast::CrateNum)
265                             -> Vec<(cstore::NativeLibraryKind, String)> {
266     let cdata = cstore.get_crate_data(crate_num);
267     decoder::get_native_libraries(&*cdata)
268 }
269
270 pub fn each_inherent_implementation_for_type<F>(cstore: &cstore::CStore,
271                                                 def_id: DefId,
272                                                 callback: F) where
273     F: FnMut(DefId),
274 {
275     let cdata = cstore.get_crate_data(def_id.krate);
276     decoder::each_inherent_implementation_for_type(&*cdata, def_id.index, callback)
277 }
278
279 pub fn each_implementation_for_trait<F>(cstore: &cstore::CStore,
280                                         def_id: DefId,
281                                         mut callback: F) where
282     F: FnMut(DefId),
283 {
284     cstore.iter_crate_data(|_, cdata| {
285         decoder::each_implementation_for_trait(cdata, def_id, &mut callback)
286     })
287 }
288
289 /// If the given def ID describes an item belonging to a trait (either a
290 /// default method or an implementation of a trait method), returns the ID of
291 /// the trait that the method belongs to. Otherwise, returns `None`.
292 pub fn get_trait_of_item(cstore: &cstore::CStore,
293                          def_id: DefId,
294                          tcx: &ty::ctxt)
295                          -> Option<DefId> {
296     let cdata = cstore.get_crate_data(def_id.krate);
297     decoder::get_trait_of_item(&*cdata, def_id.index, tcx)
298 }
299
300 pub fn get_tuple_struct_definition_if_ctor(cstore: &cstore::CStore,
301                                            def_id: DefId)
302     -> Option<DefId>
303 {
304     let cdata = cstore.get_crate_data(def_id.krate);
305     decoder::get_tuple_struct_definition_if_ctor(&*cdata, def_id.index)
306 }
307
308 pub fn get_dylib_dependency_formats(cstore: &cstore::CStore,
309                                     cnum: ast::CrateNum)
310     -> Vec<(ast::CrateNum, cstore::LinkagePreference)>
311 {
312     let cdata = cstore.get_crate_data(cnum);
313     decoder::get_dylib_dependency_formats(&*cdata)
314 }
315
316 pub fn get_missing_lang_items(cstore: &cstore::CStore, cnum: ast::CrateNum)
317     -> Vec<lang_items::LangItem>
318 {
319     let cdata = cstore.get_crate_data(cnum);
320     decoder::get_missing_lang_items(&*cdata)
321 }
322
323 pub fn get_method_arg_names(cstore: &cstore::CStore, did: DefId)
324     -> Vec<String>
325 {
326     let cdata = cstore.get_crate_data(did.krate);
327     decoder::get_method_arg_names(&*cdata, did.index)
328 }
329
330 pub fn get_reachable_ids(cstore: &cstore::CStore, cnum: ast::CrateNum)
331     -> Vec<DefId>
332 {
333     let cdata = cstore.get_crate_data(cnum);
334     decoder::get_reachable_ids(&*cdata)
335 }
336
337 pub fn is_typedef(cstore: &cstore::CStore, did: DefId) -> bool {
338     let cdata = cstore.get_crate_data(did.krate);
339     decoder::is_typedef(&*cdata, did.index)
340 }
341
342 pub fn is_const_fn(cstore: &cstore::CStore, did: DefId) -> bool {
343     let cdata = cstore.get_crate_data(did.krate);
344     decoder::is_const_fn(&*cdata, did.index)
345 }
346
347 pub fn is_impl(cstore: &cstore::CStore, did: DefId) -> bool {
348     let cdata = cstore.get_crate_data(did.krate);
349     decoder::is_impl(&*cdata, did.index)
350 }
351
352 pub fn get_stability(cstore: &cstore::CStore,
353                      def: DefId)
354                      -> Option<attr::Stability> {
355     let cdata = cstore.get_crate_data(def.krate);
356     decoder::get_stability(&*cdata, def.index)
357 }
358
359 pub fn is_staged_api(cstore: &cstore::CStore, krate: ast::CrateNum) -> bool {
360     cstore.get_crate_data(krate).staged_api
361 }
362
363 pub fn get_repr_attrs(cstore: &cstore::CStore, def: DefId)
364                       -> Vec<attr::ReprAttr> {
365     let cdata = cstore.get_crate_data(def.krate);
366     decoder::get_repr_attrs(&*cdata, def.index)
367 }
368
369 pub fn is_defaulted_trait(cstore: &cstore::CStore, trait_def_id: DefId) -> bool {
370     let cdata = cstore.get_crate_data(trait_def_id.krate);
371     decoder::is_defaulted_trait(&*cdata, trait_def_id.index)
372 }
373
374 pub fn is_default_impl(cstore: &cstore::CStore, impl_did: DefId) -> bool {
375     let cdata = cstore.get_crate_data(impl_did.krate);
376     decoder::is_default_impl(&*cdata, impl_did.index)
377 }
378
379 pub fn is_extern_fn(cstore: &cstore::CStore, did: DefId,
380                     tcx: &ty::ctxt) -> bool {
381     let cdata = cstore.get_crate_data(did.krate);
382     decoder::is_extern_fn(&*cdata, did.index, tcx)
383 }
384
385 pub fn closure_kind<'tcx>(tcx: &ty::ctxt<'tcx>, def_id: DefId) -> ty::ClosureKind {
386     assert!(!def_id.is_local());
387     let cdata = tcx.sess.cstore.get_crate_data(def_id.krate);
388     decoder::closure_kind(&*cdata, def_id.index)
389 }
390
391 pub fn closure_ty<'tcx>(tcx: &ty::ctxt<'tcx>, def_id: DefId) -> ty::ClosureTy<'tcx> {
392     assert!(!def_id.is_local());
393     let cdata = tcx.sess.cstore.get_crate_data(def_id.krate);
394     decoder::closure_ty(&*cdata, def_id.index, tcx)
395 }
396
397 pub fn def_path(tcx: &ty::ctxt, def: DefId) -> ast_map::DefPath {
398     let cstore = &tcx.sess.cstore;
399     let cdata = cstore.get_crate_data(def.krate);
400     let path = decoder::def_path(&*cdata, def.index);
401     let local_path = cdata.local_def_path();
402     local_path.into_iter().chain(path).collect()
403 }
404