]> git.lizzy.rs Git - rust.git/blob - src/librustc/metadata/csearch.rs
rollup merge of #17355 : gamazeps/issue17210
[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 #![allow(non_camel_case_types)]
14
15 use metadata::common::*;
16 use metadata::cstore;
17 use metadata::decoder;
18 use middle::lang_items;
19 use middle::resolve;
20 use middle::ty;
21 use middle::typeck;
22 use middle::subst::VecPerParamSpace;
23
24 use rbml;
25 use rbml::reader;
26 use std::rc::Rc;
27 use syntax::ast;
28 use syntax::ast_map;
29 use syntax::attr;
30 use syntax::diagnostic::expect;
31 use syntax::parse::token;
32
33 use std::collections::hashmap::HashMap;
34
35 pub struct StaticMethodInfo {
36     pub ident: ast::Ident,
37     pub def_id: ast::DefId,
38     pub fn_style: ast::FnStyle,
39     pub vis: ast::Visibility,
40 }
41
42 pub fn get_symbol(cstore: &cstore::CStore, def: ast::DefId) -> String {
43     let cdata = cstore.get_crate_data(def.krate);
44     decoder::get_symbol(cdata.data(), def.node)
45 }
46
47 /// Iterates over all the language items in the given crate.
48 pub fn each_lang_item(cstore: &cstore::CStore,
49                       cnum: ast::CrateNum,
50                       f: |ast::NodeId, uint| -> bool)
51                       -> bool {
52     let crate_data = cstore.get_crate_data(cnum);
53     decoder::each_lang_item(&*crate_data, f)
54 }
55
56 /// Iterates over each child of the given item.
57 pub fn each_child_of_item(cstore: &cstore::CStore,
58                           def_id: ast::DefId,
59                           callback: |decoder::DefLike,
60                                      ast::Ident,
61                                      ast::Visibility|) {
62     let crate_data = cstore.get_crate_data(def_id.krate);
63     let get_crate_data: decoder::GetCrateDataCb = |cnum| {
64         cstore.get_crate_data(cnum)
65     };
66     decoder::each_child_of_item(cstore.intr.clone(),
67                                 &*crate_data,
68                                 def_id.node,
69                                 get_crate_data,
70                                 callback)
71 }
72
73 /// Iterates over each top-level crate item.
74 pub fn each_top_level_item_of_crate(cstore: &cstore::CStore,
75                                     cnum: ast::CrateNum,
76                                     callback: |decoder::DefLike,
77                                                ast::Ident,
78                                                ast::Visibility|) {
79     let crate_data = cstore.get_crate_data(cnum);
80     let get_crate_data: decoder::GetCrateDataCb = |cnum| {
81         cstore.get_crate_data(cnum)
82     };
83     decoder::each_top_level_item_of_crate(cstore.intr.clone(),
84                                           &*crate_data,
85                                           get_crate_data,
86                                           callback)
87 }
88
89 pub fn get_item_path(tcx: &ty::ctxt, def: ast::DefId) -> Vec<ast_map::PathElem> {
90     let cstore = &tcx.sess.cstore;
91     let cdata = cstore.get_crate_data(def.krate);
92     let path = decoder::get_item_path(&*cdata, def.node);
93
94     // FIXME #1920: This path is not always correct if the crate is not linked
95     // into the root namespace.
96     (vec!(ast_map::PathMod(token::intern(cdata.name.as_slice())))).append(
97         path.as_slice())
98 }
99
100 pub enum found_ast<'ast> {
101     found(&'ast ast::InlinedItem),
102     found_parent(ast::DefId, &'ast ast::InlinedItem),
103     not_found,
104 }
105
106 // Finds the AST for this item in the crate metadata, if any.  If the item was
107 // not marked for inlining, then the AST will not be present and hence none
108 // will be returned.
109 pub fn maybe_get_item_ast<'tcx>(tcx: &ty::ctxt<'tcx>, def: ast::DefId,
110                                 decode_inlined_item: decoder::DecodeInlinedItem)
111                                 -> found_ast<'tcx> {
112     let cstore = &tcx.sess.cstore;
113     let cdata = cstore.get_crate_data(def.krate);
114     decoder::maybe_get_item_ast(&*cdata, tcx, def.node, decode_inlined_item)
115 }
116
117 pub fn get_enum_variants(tcx: &ty::ctxt, def: ast::DefId)
118                       -> Vec<Rc<ty::VariantInfo>> {
119     let cstore = &tcx.sess.cstore;
120     let cdata = cstore.get_crate_data(def.krate);
121     decoder::get_enum_variants(cstore.intr.clone(), &*cdata, def.node, tcx)
122 }
123
124 /// Returns information about the given implementation.
125 pub fn get_impl_items(cstore: &cstore::CStore, impl_def_id: ast::DefId)
126                       -> Vec<ty::ImplOrTraitItemId> {
127     let cdata = cstore.get_crate_data(impl_def_id.krate);
128     decoder::get_impl_items(&*cdata, impl_def_id.node)
129 }
130
131 pub fn get_impl_or_trait_item(tcx: &ty::ctxt, def: ast::DefId)
132                               -> ty::ImplOrTraitItem {
133     let cdata = tcx.sess.cstore.get_crate_data(def.krate);
134     decoder::get_impl_or_trait_item(tcx.sess.cstore.intr.clone(),
135                                     &*cdata,
136                                     def.node,
137                                     tcx)
138 }
139
140 pub fn get_trait_item_name_and_kind(cstore: &cstore::CStore, def: ast::DefId)
141                                     -> (ast::Ident, resolve::TraitItemKind) {
142     let cdata = cstore.get_crate_data(def.krate);
143     decoder::get_trait_item_name_and_kind(cstore.intr.clone(),
144                                           &*cdata,
145                                           def.node)
146 }
147
148 pub fn get_trait_item_def_ids(cstore: &cstore::CStore, def: ast::DefId)
149                               -> Vec<ty::ImplOrTraitItemId> {
150     let cdata = cstore.get_crate_data(def.krate);
151     decoder::get_trait_item_def_ids(&*cdata, def.node)
152 }
153
154 pub fn get_item_variances(cstore: &cstore::CStore,
155                           def: ast::DefId) -> ty::ItemVariances {
156     let cdata = cstore.get_crate_data(def.krate);
157     decoder::get_item_variances(&*cdata, def.node)
158 }
159
160 pub fn get_provided_trait_methods(tcx: &ty::ctxt,
161                                   def: ast::DefId)
162                                -> Vec<Rc<ty::Method>> {
163     let cstore = &tcx.sess.cstore;
164     let cdata = cstore.get_crate_data(def.krate);
165     decoder::get_provided_trait_methods(cstore.intr.clone(), &*cdata, def.node, tcx)
166 }
167
168 pub fn get_supertraits(tcx: &ty::ctxt, def: ast::DefId) -> Vec<Rc<ty::TraitRef>> {
169     let cstore = &tcx.sess.cstore;
170     let cdata = cstore.get_crate_data(def.krate);
171     decoder::get_supertraits(&*cdata, def.node, tcx)
172 }
173
174 pub fn get_type_name_if_impl(cstore: &cstore::CStore, def: ast::DefId)
175                           -> Option<ast::Ident> {
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_static_methods_if_impl(cstore: &cstore::CStore,
181                                   def: ast::DefId)
182                                -> Option<Vec<StaticMethodInfo> > {
183     let cdata = cstore.get_crate_data(def.krate);
184     decoder::get_static_methods_if_impl(cstore.intr.clone(), &*cdata, def.node)
185 }
186
187 pub fn get_item_attrs(cstore: &cstore::CStore,
188                       def_id: ast::DefId,
189                       f: |Vec<ast::Attribute>|) {
190     let cdata = cstore.get_crate_data(def_id.krate);
191     decoder::get_item_attrs(&*cdata, def_id.node, f)
192 }
193
194 pub fn get_struct_fields(cstore: &cstore::CStore,
195                          def: ast::DefId)
196                       -> Vec<ty::field_ty> {
197     let cdata = cstore.get_crate_data(def.krate);
198     decoder::get_struct_fields(cstore.intr.clone(), &*cdata, def.node)
199 }
200
201 pub fn get_struct_field_attrs(cstore: &cstore::CStore, def: ast::DefId) -> HashMap<ast::NodeId,
202         Vec<ast::Attribute>> {
203     let cdata = cstore.get_crate_data(def.krate);
204     decoder::get_struct_field_attrs(&*cdata)
205 }
206
207 pub fn get_type(tcx: &ty::ctxt,
208                 def: ast::DefId)
209              -> ty::Polytype {
210     let cstore = &tcx.sess.cstore;
211     let cdata = cstore.get_crate_data(def.krate);
212     decoder::get_type(&*cdata, def.node, tcx)
213 }
214
215 pub fn get_trait_def(tcx: &ty::ctxt, def: ast::DefId) -> ty::TraitDef {
216     let cstore = &tcx.sess.cstore;
217     let cdata = cstore.get_crate_data(def.krate);
218     decoder::get_trait_def(&*cdata, def.node, tcx)
219 }
220
221 pub fn get_field_type(tcx: &ty::ctxt, class_id: ast::DefId,
222                       def: ast::DefId) -> ty::Polytype {
223     let cstore = &tcx.sess.cstore;
224     let cdata = cstore.get_crate_data(class_id.krate);
225     let all_items = reader::get_doc(rbml::Doc::new(cdata.data()), tag_items);
226     let class_doc = expect(tcx.sess.diagnostic(),
227                            decoder::maybe_find_item(class_id.node, all_items),
228                            || {
229         (format!("get_field_type: class ID {:?} not found",
230                  class_id)).to_string()
231     });
232     let the_field = expect(tcx.sess.diagnostic(),
233         decoder::maybe_find_item(def.node, class_doc),
234         || {
235             (format!("get_field_type: in class {:?}, field ID {:?} not found",
236                     class_id,
237                     def)).to_string()
238         });
239     let ty = decoder::item_type(def, the_field, tcx, &*cdata);
240     ty::Polytype {
241         generics: ty::Generics {types: VecPerParamSpace::empty(),
242                                 regions: VecPerParamSpace::empty()},
243         ty: ty
244     }
245 }
246
247 // Given a def_id for an impl, return the trait it implements,
248 // if there is one.
249 pub fn get_impl_trait(tcx: &ty::ctxt,
250                       def: ast::DefId) -> Option<Rc<ty::TraitRef>> {
251     let cstore = &tcx.sess.cstore;
252     let cdata = cstore.get_crate_data(def.krate);
253     decoder::get_impl_trait(&*cdata, def.node, tcx)
254 }
255
256 // Given a def_id for an impl, return information about its vtables
257 pub fn get_impl_vtables(tcx: &ty::ctxt,
258                         def: ast::DefId)
259                         -> typeck::vtable_res {
260     let cstore = &tcx.sess.cstore;
261     let cdata = cstore.get_crate_data(def.krate);
262     decoder::get_impl_vtables(&*cdata, def.node, tcx)
263 }
264
265 pub fn get_native_libraries(cstore: &cstore::CStore,
266                             crate_num: ast::CrateNum)
267                                 -> Vec<(cstore::NativeLibaryKind, String)> {
268     let cdata = cstore.get_crate_data(crate_num);
269     decoder::get_native_libraries(&*cdata)
270 }
271
272 pub fn each_impl(cstore: &cstore::CStore,
273                  crate_num: ast::CrateNum,
274                  callback: |ast::DefId|) {
275     let cdata = cstore.get_crate_data(crate_num);
276     decoder::each_impl(&*cdata, callback)
277 }
278
279 pub fn each_implementation_for_type(cstore: &cstore::CStore,
280                                     def_id: ast::DefId,
281                                     callback: |ast::DefId|) {
282     let cdata = cstore.get_crate_data(def_id.krate);
283     decoder::each_implementation_for_type(&*cdata, def_id.node, callback)
284 }
285
286 pub fn each_implementation_for_trait(cstore: &cstore::CStore,
287                                      def_id: ast::DefId,
288                                      callback: |ast::DefId|) {
289     let cdata = cstore.get_crate_data(def_id.krate);
290     decoder::each_implementation_for_trait(&*cdata, def_id.node, callback)
291 }
292
293 /// If the given def ID describes an item belonging to a trait (either a
294 /// default method or an implementation of a trait method), returns the ID of
295 /// the trait that the method belongs to. Otherwise, returns `None`.
296 pub fn get_trait_of_item(cstore: &cstore::CStore,
297                          def_id: ast::DefId,
298                          tcx: &ty::ctxt)
299                          -> Option<ast::DefId> {
300     let cdata = cstore.get_crate_data(def_id.krate);
301     decoder::get_trait_of_item(&*cdata, def_id.node, tcx)
302 }
303
304 pub fn get_tuple_struct_definition_if_ctor(cstore: &cstore::CStore,
305                                            def_id: ast::DefId)
306     -> Option<ast::DefId>
307 {
308     let cdata = cstore.get_crate_data(def_id.krate);
309     decoder::get_tuple_struct_definition_if_ctor(&*cdata, def_id.node)
310 }
311
312 pub fn get_dylib_dependency_formats(cstore: &cstore::CStore,
313                                     cnum: ast::CrateNum)
314     -> Vec<(ast::CrateNum, cstore::LinkagePreference)>
315 {
316     let cdata = cstore.get_crate_data(cnum);
317     decoder::get_dylib_dependency_formats(&*cdata)
318 }
319
320 pub fn get_missing_lang_items(cstore: &cstore::CStore, cnum: ast::CrateNum)
321     -> Vec<lang_items::LangItem>
322 {
323     let cdata = cstore.get_crate_data(cnum);
324     decoder::get_missing_lang_items(&*cdata)
325 }
326
327 pub fn get_method_arg_names(cstore: &cstore::CStore, did: ast::DefId)
328     -> Vec<String>
329 {
330     let cdata = cstore.get_crate_data(did.krate);
331     decoder::get_method_arg_names(&*cdata, did.node)
332 }
333
334 pub fn get_reachable_extern_fns(cstore: &cstore::CStore, cnum: ast::CrateNum)
335     -> Vec<ast::DefId>
336 {
337     let cdata = cstore.get_crate_data(cnum);
338     decoder::get_reachable_extern_fns(&*cdata)
339 }
340
341 pub fn is_typedef(cstore: &cstore::CStore, did: ast::DefId) -> bool {
342     let cdata = cstore.get_crate_data(did.krate);
343     decoder::is_typedef(&*cdata, did.node)
344 }
345
346 pub fn get_stability(cstore: &cstore::CStore,
347                      def: ast::DefId)
348                      -> Option<attr::Stability> {
349     let cdata = cstore.get_crate_data(def.krate);
350     decoder::get_stability(&*cdata, def.node)
351 }
352
353 pub fn is_associated_type(cstore: &cstore::CStore, def: ast::DefId) -> bool {
354     let cdata = cstore.get_crate_data(def.krate);
355     decoder::is_associated_type(&*cdata, def.node)
356 }
357