]> git.lizzy.rs Git - rust.git/blob - src/librustc/metadata/csearch.rs
split ty::util and ty::adjustment
[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;
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 rustc_front::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.node)
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(ast::NodeId, 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.node,
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.node);
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.node)
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.node, 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.node)
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.node,
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.node)
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.node)
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.node)
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.node)
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.node, 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.node, tcx)
172 }
173
174 pub fn get_type_name_if_impl(cstore: &cstore::CStore, def: DefId)
175                           -> Option<ast::Name> {
176     let cdata = cstore.get_crate_data(def.krate);
177     decoder::get_type_name_if_impl(&*cdata, def.node)
178 }
179
180 pub fn get_methods_if_impl(cstore: &cstore::CStore,
181                                   def: DefId)
182                                -> Option<Vec<MethodInfo> > {
183     let cdata = cstore.get_crate_data(def.krate);
184     decoder::get_methods_if_impl(cstore.intr.clone(), &*cdata, def.node)
185 }
186
187 pub fn get_item_attrs(cstore: &cstore::CStore,
188                       def_id: DefId)
189                       -> Vec<hir::Attribute> {
190     let cdata = cstore.get_crate_data(def_id.krate);
191     decoder::get_item_attrs(&*cdata, def_id.node)
192 }
193
194 pub fn get_struct_field_names(cstore: &cstore::CStore, def: DefId) -> Vec<ast::Name> {
195     let cdata = cstore.get_crate_data(def.krate);
196     decoder::get_struct_field_names(&cstore.intr, &*cdata, def.node)
197 }
198
199 pub fn get_struct_field_attrs(cstore: &cstore::CStore, def: DefId) -> FnvHashMap<ast::NodeId,
200         Vec<hir::Attribute>> {
201     let cdata = cstore.get_crate_data(def.krate);
202     decoder::get_struct_field_attrs(&*cdata)
203 }
204
205 pub fn get_type<'tcx>(tcx: &ty::ctxt<'tcx>,
206                       def: DefId)
207                       -> ty::TypeScheme<'tcx> {
208     let cstore = &tcx.sess.cstore;
209     let cdata = cstore.get_crate_data(def.krate);
210     decoder::get_type(&*cdata, def.node, tcx)
211 }
212
213 pub fn get_trait_def<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::TraitDef<'tcx> {
214     let cstore = &tcx.sess.cstore;
215     let cdata = cstore.get_crate_data(def.krate);
216     decoder::get_trait_def(&*cdata, def.node, tcx)
217 }
218
219 pub fn get_adt_def<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx> {
220     let cstore = &tcx.sess.cstore;
221     let cdata = cstore.get_crate_data(def.krate);
222     decoder::get_adt_def(&cstore.intr, &*cdata, def.node, tcx)
223 }
224
225 pub fn get_predicates<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId)
226                             -> ty::GenericPredicates<'tcx>
227 {
228     let cstore = &tcx.sess.cstore;
229     let cdata = cstore.get_crate_data(def.krate);
230     decoder::get_predicates(&*cdata, def.node, tcx)
231 }
232
233 pub fn get_super_predicates<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId)
234                                   -> ty::GenericPredicates<'tcx>
235 {
236     let cstore = &tcx.sess.cstore;
237     let cdata = cstore.get_crate_data(def.krate);
238     decoder::get_super_predicates(&*cdata, def.node, tcx)
239 }
240
241 pub fn get_impl_polarity<'tcx>(tcx: &ty::ctxt<'tcx>,
242                                def: DefId)
243                                -> Option<hir::ImplPolarity>
244 {
245     let cstore = &tcx.sess.cstore;
246     let cdata = cstore.get_crate_data(def.krate);
247     decoder::get_impl_polarity(&*cdata, def.node)
248 }
249
250 pub fn get_custom_coerce_unsized_kind<'tcx>(
251     tcx: &ty::ctxt<'tcx>,
252     def: DefId)
253     -> Option<ty::adjustment::CustomCoerceUnsized>
254 {
255     let cstore = &tcx.sess.cstore;
256     let cdata = cstore.get_crate_data(def.krate);
257     decoder::get_custom_coerce_unsized_kind(&*cdata, def.node)
258 }
259
260 // Given a def_id for an impl, return the trait it implements,
261 // if there is one.
262 pub fn get_impl_trait<'tcx>(tcx: &ty::ctxt<'tcx>,
263                             def: DefId)
264                             -> Option<ty::TraitRef<'tcx>> {
265     let cstore = &tcx.sess.cstore;
266     let cdata = cstore.get_crate_data(def.krate);
267     decoder::get_impl_trait(&*cdata, def.node, tcx)
268 }
269
270 pub fn get_native_libraries(cstore: &cstore::CStore, crate_num: ast::CrateNum)
271                             -> Vec<(cstore::NativeLibraryKind, String)> {
272     let cdata = cstore.get_crate_data(crate_num);
273     decoder::get_native_libraries(&*cdata)
274 }
275
276 pub fn each_inherent_implementation_for_type<F>(cstore: &cstore::CStore,
277                                                 def_id: DefId,
278                                                 callback: F) where
279     F: FnMut(DefId),
280 {
281     let cdata = cstore.get_crate_data(def_id.krate);
282     decoder::each_inherent_implementation_for_type(&*cdata, def_id.node, callback)
283 }
284
285 pub fn each_implementation_for_trait<F>(cstore: &cstore::CStore,
286                                         def_id: DefId,
287                                         mut callback: F) where
288     F: FnMut(DefId),
289 {
290     cstore.iter_crate_data(|_, cdata| {
291         decoder::each_implementation_for_trait(cdata, def_id, &mut callback)
292     })
293 }
294
295 /// If the given def ID describes an item belonging to a trait (either a
296 /// default method or an implementation of a trait method), returns the ID of
297 /// the trait that the method belongs to. Otherwise, returns `None`.
298 pub fn get_trait_of_item(cstore: &cstore::CStore,
299                          def_id: DefId,
300                          tcx: &ty::ctxt)
301                          -> Option<DefId> {
302     let cdata = cstore.get_crate_data(def_id.krate);
303     decoder::get_trait_of_item(&*cdata, def_id.node, tcx)
304 }
305
306 pub fn get_tuple_struct_definition_if_ctor(cstore: &cstore::CStore,
307                                            def_id: DefId)
308     -> Option<DefId>
309 {
310     let cdata = cstore.get_crate_data(def_id.krate);
311     decoder::get_tuple_struct_definition_if_ctor(&*cdata, def_id.node)
312 }
313
314 pub fn get_dylib_dependency_formats(cstore: &cstore::CStore,
315                                     cnum: ast::CrateNum)
316     -> Vec<(ast::CrateNum, cstore::LinkagePreference)>
317 {
318     let cdata = cstore.get_crate_data(cnum);
319     decoder::get_dylib_dependency_formats(&*cdata)
320 }
321
322 pub fn get_missing_lang_items(cstore: &cstore::CStore, cnum: ast::CrateNum)
323     -> Vec<lang_items::LangItem>
324 {
325     let cdata = cstore.get_crate_data(cnum);
326     decoder::get_missing_lang_items(&*cdata)
327 }
328
329 pub fn get_method_arg_names(cstore: &cstore::CStore, did: DefId)
330     -> Vec<String>
331 {
332     let cdata = cstore.get_crate_data(did.krate);
333     decoder::get_method_arg_names(&*cdata, did.node)
334 }
335
336 pub fn get_reachable_ids(cstore: &cstore::CStore, cnum: ast::CrateNum)
337     -> Vec<DefId>
338 {
339     let cdata = cstore.get_crate_data(cnum);
340     decoder::get_reachable_ids(&*cdata)
341 }
342
343 pub fn is_typedef(cstore: &cstore::CStore, did: DefId) -> bool {
344     let cdata = cstore.get_crate_data(did.krate);
345     decoder::is_typedef(&*cdata, did.node)
346 }
347
348 pub fn is_const_fn(cstore: &cstore::CStore, did: DefId) -> bool {
349     let cdata = cstore.get_crate_data(did.krate);
350     decoder::is_const_fn(&*cdata, did.node)
351 }
352
353 pub fn is_impl(cstore: &cstore::CStore, did: DefId) -> bool {
354     let cdata = cstore.get_crate_data(did.krate);
355     decoder::is_impl(&*cdata, did.node)
356 }
357
358 pub fn get_stability(cstore: &cstore::CStore,
359                      def: DefId)
360                      -> Option<attr::Stability> {
361     let cdata = cstore.get_crate_data(def.krate);
362     decoder::get_stability(&*cdata, def.node)
363 }
364
365 pub fn is_staged_api(cstore: &cstore::CStore, krate: ast::CrateNum) -> bool {
366     cstore.get_crate_data(krate).staged_api
367 }
368
369 pub fn get_repr_attrs(cstore: &cstore::CStore, def: DefId)
370                       -> Vec<attr::ReprAttr> {
371     let cdata = cstore.get_crate_data(def.krate);
372     decoder::get_repr_attrs(&*cdata, def.node)
373 }
374
375 pub fn is_defaulted_trait(cstore: &cstore::CStore, trait_def_id: DefId) -> bool {
376     let cdata = cstore.get_crate_data(trait_def_id.krate);
377     decoder::is_defaulted_trait(&*cdata, trait_def_id.node)
378 }
379
380 pub fn is_default_impl(cstore: &cstore::CStore, impl_did: DefId) -> bool {
381     let cdata = cstore.get_crate_data(impl_did.krate);
382     decoder::is_default_impl(&*cdata, impl_did.node)
383 }
384
385 pub fn is_extern_fn(cstore: &cstore::CStore, did: DefId,
386                     tcx: &ty::ctxt) -> bool {
387     let cdata = cstore.get_crate_data(did.krate);
388     decoder::is_extern_fn(&*cdata, did.node, tcx)
389 }