]> git.lizzy.rs Git - rust.git/blob - src/librustc/metadata/csearch.rs
Convert most code to new inner attribute syntax.
[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::ty;
19 use middle::typeck;
20
21 use reader = serialize::ebml::reader;
22 use std::rc::Rc;
23 use std::vec;
24 use syntax::ast;
25 use syntax::ast_map;
26 use syntax::diagnostic::expect;
27 use syntax::parse::token;
28
29 pub struct StaticMethodInfo {
30     ident: ast::Ident,
31     def_id: ast::DefId,
32     purity: ast::Purity,
33     vis: ast::Visibility,
34 }
35
36 pub fn get_symbol(cstore: &cstore::CStore, def: ast::DefId) -> ~str {
37     let cdata = cstore.get_crate_data(def.krate).data();
38     return decoder::get_symbol(cdata, def.node);
39 }
40
41 pub fn get_type_param_count(cstore: &cstore::CStore, def: ast::DefId)
42                          -> uint {
43     let cdata = cstore.get_crate_data(def.krate).data();
44     return decoder::get_type_param_count(cdata, 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,
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,
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::append(vec!(ast_map::PathMod(token::intern(cdata.name))),
97                    path.as_slice())
98 }
99
100 pub enum found_ast {
101     found(ast::InlinedItem),
102     found_parent(ast::DefId, 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: &ty::ctxt, def: ast::DefId,
110                           decode_inlined_item: decoder::DecodeInlinedItem)
111                        -> found_ast {
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<@ty::VariantInfo> {
119     let cstore = &tcx.sess.cstore;
120     let cdata = cstore.get_crate_data(def.krate);
121     return decoder::get_enum_variants(cstore.intr, cdata, def.node, tcx)
122 }
123
124 /// Returns information about the given implementation.
125 pub fn get_impl(tcx: &ty::ctxt, impl_def_id: ast::DefId)
126                 -> ty::Impl {
127     let cdata = tcx.sess.cstore.get_crate_data(impl_def_id.krate);
128     decoder::get_impl(tcx.sess.cstore.intr, cdata, impl_def_id.node, tcx)
129 }
130
131 pub fn get_method(tcx: &ty::ctxt, def: ast::DefId) -> ty::Method {
132     let cdata = tcx.sess.cstore.get_crate_data(def.krate);
133     decoder::get_method(tcx.sess.cstore.intr, cdata, def.node, tcx)
134 }
135
136 pub fn get_method_name_and_explicit_self(cstore: &cstore::CStore,
137                                          def: ast::DefId)
138                                      -> (ast::Ident, ast::ExplicitSelf_)
139 {
140     let cdata = cstore.get_crate_data(def.krate);
141     decoder::get_method_name_and_explicit_self(cstore.intr, cdata, def.node)
142 }
143
144 pub fn get_trait_method_def_ids(cstore: &cstore::CStore,
145                                 def: ast::DefId) -> Vec<ast::DefId> {
146     let cdata = cstore.get_crate_data(def.krate);
147     decoder::get_trait_method_def_ids(cdata, def.node)
148 }
149
150 pub fn get_item_variances(cstore: &cstore::CStore,
151                           def: ast::DefId) -> ty::ItemVariances {
152     let cdata = cstore.get_crate_data(def.krate);
153     decoder::get_item_variances(cdata, def.node)
154 }
155
156 pub fn get_provided_trait_methods(tcx: &ty::ctxt,
157                                   def: ast::DefId)
158                                -> Vec<@ty::Method> {
159     let cstore = &tcx.sess.cstore;
160     let cdata = cstore.get_crate_data(def.krate);
161     decoder::get_provided_trait_methods(cstore.intr, cdata, def.node, tcx)
162 }
163
164 pub fn get_supertraits(tcx: &ty::ctxt, def: ast::DefId) -> Vec<@ty::TraitRef> {
165     let cstore = &tcx.sess.cstore;
166     let cdata = cstore.get_crate_data(def.krate);
167     decoder::get_supertraits(cdata, def.node, tcx)
168 }
169
170 pub fn get_type_name_if_impl(cstore: &cstore::CStore, def: ast::DefId)
171                           -> Option<ast::Ident> {
172     let cdata = cstore.get_crate_data(def.krate);
173     decoder::get_type_name_if_impl(cdata, def.node)
174 }
175
176 pub fn get_static_methods_if_impl(cstore: &cstore::CStore,
177                                   def: ast::DefId)
178                                -> Option<Vec<StaticMethodInfo> > {
179     let cdata = cstore.get_crate_data(def.krate);
180     decoder::get_static_methods_if_impl(cstore.intr, cdata, def.node)
181 }
182
183 pub fn get_item_attrs(cstore: &cstore::CStore,
184                       def_id: ast::DefId,
185                       f: |Vec<@ast::MetaItem> |) {
186     let cdata = cstore.get_crate_data(def_id.krate);
187     decoder::get_item_attrs(cdata, def_id.node, f)
188 }
189
190 pub fn get_struct_fields(cstore: &cstore::CStore,
191                          def: ast::DefId)
192                       -> Vec<ty::field_ty> {
193     let cdata = cstore.get_crate_data(def.krate);
194     decoder::get_struct_fields(cstore.intr, cdata, def.node)
195 }
196
197 pub fn get_type(tcx: &ty::ctxt,
198                 def: ast::DefId)
199              -> ty::ty_param_bounds_and_ty {
200     let cstore = &tcx.sess.cstore;
201     let cdata = cstore.get_crate_data(def.krate);
202     decoder::get_type(cdata, def.node, tcx)
203 }
204
205 pub fn get_trait_def(tcx: &ty::ctxt, def: ast::DefId) -> ty::TraitDef {
206     let cstore = &tcx.sess.cstore;
207     let cdata = cstore.get_crate_data(def.krate);
208     decoder::get_trait_def(cdata, def.node, tcx)
209 }
210
211 pub fn get_field_type(tcx: &ty::ctxt, class_id: ast::DefId,
212                       def: ast::DefId) -> ty::ty_param_bounds_and_ty {
213     let cstore = &tcx.sess.cstore;
214     let cdata = cstore.get_crate_data(class_id.krate);
215     let all_items = reader::get_doc(reader::Doc(cdata.data()), tag_items);
216     let class_doc = expect(tcx.sess.diagnostic(),
217                            decoder::maybe_find_item(class_id.node, all_items),
218                            || format!("get_field_type: class ID {:?} not found",
219                                    class_id) );
220     let the_field = expect(tcx.sess.diagnostic(),
221         decoder::maybe_find_item(def.node, class_doc),
222         || format!("get_field_type: in class {:?}, field ID {:?} not found",
223                  class_id, def) );
224     let ty = decoder::item_type(def, the_field, tcx, cdata);
225     ty::ty_param_bounds_and_ty {
226         generics: ty::Generics {type_param_defs: Rc::new(Vec::new()),
227                                 region_param_defs: Rc::new(Vec::new())},
228         ty: ty
229     }
230 }
231
232 // Given a def_id for an impl, return the trait it implements,
233 // if there is one.
234 pub fn get_impl_trait(tcx: &ty::ctxt,
235                       def: ast::DefId) -> Option<@ty::TraitRef> {
236     let cstore = &tcx.sess.cstore;
237     let cdata = cstore.get_crate_data(def.krate);
238     decoder::get_impl_trait(cdata, def.node, tcx)
239 }
240
241 // Given a def_id for an impl, return information about its vtables
242 pub fn get_impl_vtables(tcx: &ty::ctxt,
243                         def: ast::DefId) -> typeck::impl_res {
244     let cstore = &tcx.sess.cstore;
245     let cdata = cstore.get_crate_data(def.krate);
246     decoder::get_impl_vtables(cdata, def.node, tcx)
247 }
248
249 pub fn get_impl_method(cstore: &cstore::CStore,
250                        def: ast::DefId,
251                        mname: ast::Ident)
252                     -> Option<ast::DefId> {
253     let cdata = cstore.get_crate_data(def.krate);
254     decoder::get_impl_method(cstore.intr, cdata, def.node, mname)
255 }
256
257 pub fn get_item_visibility(cstore: &cstore::CStore,
258                            def_id: ast::DefId)
259                         -> ast::Visibility {
260     let cdata = cstore.get_crate_data(def_id.krate);
261     decoder::get_item_visibility(cdata, def_id.node)
262 }
263
264 pub fn get_native_libraries(cstore: &cstore::CStore,
265                             crate_num: ast::CrateNum)
266                                 -> Vec<(cstore::NativeLibaryKind, ~str)> {
267     let cdata = cstore.get_crate_data(crate_num);
268     decoder::get_native_libraries(cdata)
269 }
270
271 pub fn each_impl(cstore: &cstore::CStore,
272                  crate_num: ast::CrateNum,
273                  callback: |ast::DefId|) {
274     let cdata = cstore.get_crate_data(crate_num);
275     decoder::each_impl(cdata, callback)
276 }
277
278 pub fn each_implementation_for_type(cstore: &cstore::CStore,
279                                     def_id: ast::DefId,
280                                     callback: |ast::DefId|) {
281     let cdata = cstore.get_crate_data(def_id.krate);
282     decoder::each_implementation_for_type(cdata, def_id.node, callback)
283 }
284
285 pub fn each_implementation_for_trait(cstore: &cstore::CStore,
286                                      def_id: ast::DefId,
287                                      callback: |ast::DefId|) {
288     let cdata = cstore.get_crate_data(def_id.krate);
289     decoder::each_implementation_for_trait(cdata, def_id.node, callback)
290 }
291
292 /// If the given def ID describes a method belonging to a trait (either a
293 /// default method or an implementation of a trait method), returns the ID of
294 /// the trait that the method belongs to. Otherwise, returns `None`.
295 pub fn get_trait_of_method(cstore: &cstore::CStore,
296                            def_id: ast::DefId,
297                            tcx: &ty::ctxt)
298                            -> Option<ast::DefId> {
299     let cdata = cstore.get_crate_data(def_id.krate);
300     decoder::get_trait_of_method(cdata, def_id.node, tcx)
301 }
302
303 pub fn get_macro_registrar_fn(cstore: &cstore::CStore,
304                               crate_num: ast::CrateNum)
305                               -> Option<ast::DefId> {
306     let cdata = cstore.get_crate_data(crate_num);
307     decoder::get_macro_registrar_fn(cdata)
308 }
309
310 pub fn get_exported_macros(cstore: &cstore::CStore,
311                            crate_num: ast::CrateNum)
312                            -> Vec<~str> {
313     let cdata = cstore.get_crate_data(crate_num);
314     decoder::get_exported_macros(cdata)
315 }