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