]> git.lizzy.rs Git - rust.git/blob - src/librustc/metadata/csearch.rs
core: rename strbuf::StrBuf to string::String
[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::ty;
20 use middle::typeck;
21
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     pub ident: ast::Ident,
31     pub def_id: ast::DefId,
32     pub fn_style: ast::FnStyle,
33     pub vis: ast::Visibility,
34 }
35
36 pub fn get_symbol(cstore: &cstore::CStore, def: ast::DefId) -> String {
37     let cdata = cstore.get_crate_data(def.krate);
38     decoder::get_symbol(cdata.data(), def.node)
39 }
40
41 /// Iterates over all the language items in the given crate.
42 pub fn each_lang_item(cstore: &cstore::CStore,
43                       cnum: ast::CrateNum,
44                       f: |ast::NodeId, uint| -> bool)
45                       -> bool {
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(cstore: &cstore::CStore,
52                           def_id: ast::DefId,
53                           callback: |decoder::DefLike,
54                                      ast::Ident,
55                                      ast::Visibility|) {
56     let crate_data = cstore.get_crate_data(def_id.krate);
57     let get_crate_data: decoder::GetCrateDataCb = |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(cstore: &cstore::CStore,
69                                     cnum: ast::CrateNum,
70                                     callback: |decoder::DefLike,
71                                                ast::Ident,
72                                                ast::Visibility|) {
73     let crate_data = cstore.get_crate_data(cnum);
74     let get_crate_data: decoder::GetCrateDataCb = |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: ast::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     // FIXME #1920: This path is not always correct if the crate is not linked
89     // into the root namespace.
90     (vec!(ast_map::PathMod(token::intern(cdata.name.as_slice())))).append(
91         path.as_slice())
92 }
93
94 pub enum found_ast {
95     found(ast::InlinedItem),
96     found_parent(ast::DefId, ast::InlinedItem),
97     not_found,
98 }
99
100 // Finds the AST for this item in the crate metadata, if any.  If the item was
101 // not marked for inlining, then the AST will not be present and hence none
102 // will be returned.
103 pub fn maybe_get_item_ast(tcx: &ty::ctxt, def: ast::DefId,
104                           decode_inlined_item: decoder::DecodeInlinedItem)
105                        -> found_ast {
106     let cstore = &tcx.sess.cstore;
107     let cdata = cstore.get_crate_data(def.krate);
108     decoder::maybe_get_item_ast(&*cdata, tcx, def.node, decode_inlined_item)
109 }
110
111 pub fn get_enum_variants(tcx: &ty::ctxt, def: ast::DefId)
112                       -> Vec<Rc<ty::VariantInfo>> {
113     let cstore = &tcx.sess.cstore;
114     let cdata = cstore.get_crate_data(def.krate);
115     decoder::get_enum_variants(cstore.intr.clone(), &*cdata, def.node, tcx)
116 }
117
118 /// Returns information about the given implementation.
119 pub fn get_impl_methods(cstore: &cstore::CStore, impl_def_id: ast::DefId)
120                         -> Vec<ast::DefId> {
121     let cdata = cstore.get_crate_data(impl_def_id.krate);
122     decoder::get_impl_methods(&*cdata, impl_def_id.node)
123 }
124
125 pub fn get_method(tcx: &ty::ctxt, def: ast::DefId) -> ty::Method {
126     let cdata = tcx.sess.cstore.get_crate_data(def.krate);
127     decoder::get_method(tcx.sess.cstore.intr.clone(), &*cdata, def.node, tcx)
128 }
129
130 pub fn get_method_name_and_explicit_self(cstore: &cstore::CStore,
131                                          def: ast::DefId)
132                                      -> (ast::Ident, ast::ExplicitSelf_)
133 {
134     let cdata = cstore.get_crate_data(def.krate);
135     decoder::get_method_name_and_explicit_self(cstore.intr.clone(), &*cdata, def.node)
136 }
137
138 pub fn get_trait_method_def_ids(cstore: &cstore::CStore,
139                                 def: ast::DefId) -> Vec<ast::DefId> {
140     let cdata = cstore.get_crate_data(def.krate);
141     decoder::get_trait_method_def_ids(&*cdata, def.node)
142 }
143
144 pub fn get_item_variances(cstore: &cstore::CStore,
145                           def: ast::DefId) -> ty::ItemVariances {
146     let cdata = cstore.get_crate_data(def.krate);
147     decoder::get_item_variances(&*cdata, def.node)
148 }
149
150 pub fn get_provided_trait_methods(tcx: &ty::ctxt,
151                                   def: ast::DefId)
152                                -> Vec<Rc<ty::Method>> {
153     let cstore = &tcx.sess.cstore;
154     let cdata = cstore.get_crate_data(def.krate);
155     decoder::get_provided_trait_methods(cstore.intr.clone(), &*cdata, def.node, tcx)
156 }
157
158 pub fn get_supertraits(tcx: &ty::ctxt, def: ast::DefId) -> Vec<Rc<ty::TraitRef>> {
159     let cstore = &tcx.sess.cstore;
160     let cdata = cstore.get_crate_data(def.krate);
161     decoder::get_supertraits(&*cdata, def.node, tcx)
162 }
163
164 pub fn get_type_name_if_impl(cstore: &cstore::CStore, def: ast::DefId)
165                           -> Option<ast::Ident> {
166     let cdata = cstore.get_crate_data(def.krate);
167     decoder::get_type_name_if_impl(&*cdata, def.node)
168 }
169
170 pub fn get_static_methods_if_impl(cstore: &cstore::CStore,
171                                   def: ast::DefId)
172                                -> Option<Vec<StaticMethodInfo> > {
173     let cdata = cstore.get_crate_data(def.krate);
174     decoder::get_static_methods_if_impl(cstore.intr.clone(), &*cdata, def.node)
175 }
176
177 pub fn get_item_attrs(cstore: &cstore::CStore,
178                       def_id: ast::DefId,
179                       f: |Vec<ast::Attribute> |) {
180     let cdata = cstore.get_crate_data(def_id.krate);
181     decoder::get_item_attrs(&*cdata, def_id.node, f)
182 }
183
184 pub fn get_struct_fields(cstore: &cstore::CStore,
185                          def: ast::DefId)
186                       -> Vec<ty::field_ty> {
187     let cdata = cstore.get_crate_data(def.krate);
188     decoder::get_struct_fields(cstore.intr.clone(), &*cdata, def.node)
189 }
190
191 pub fn get_type(tcx: &ty::ctxt,
192                 def: ast::DefId)
193              -> ty::ty_param_bounds_and_ty {
194     let cstore = &tcx.sess.cstore;
195     let cdata = cstore.get_crate_data(def.krate);
196     decoder::get_type(&*cdata, def.node, tcx)
197 }
198
199 pub fn get_trait_def(tcx: &ty::ctxt, def: ast::DefId) -> ty::TraitDef {
200     let cstore = &tcx.sess.cstore;
201     let cdata = cstore.get_crate_data(def.krate);
202     decoder::get_trait_def(&*cdata, def.node, tcx)
203 }
204
205 pub fn get_field_type(tcx: &ty::ctxt, class_id: ast::DefId,
206                       def: ast::DefId) -> ty::ty_param_bounds_and_ty {
207     let cstore = &tcx.sess.cstore;
208     let cdata = cstore.get_crate_data(class_id.krate);
209     let all_items = reader::get_doc(reader::Doc(cdata.data()), tag_items);
210     let class_doc = expect(tcx.sess.diagnostic(),
211                            decoder::maybe_find_item(class_id.node, all_items),
212                            || {
213         (format!("get_field_type: class ID {:?} not found",
214                  class_id)).to_strbuf()
215     });
216     let the_field = expect(tcx.sess.diagnostic(),
217         decoder::maybe_find_item(def.node, class_doc),
218         || {
219             (format!("get_field_type: in class {:?}, field ID {:?} not found",
220                     class_id,
221                     def)).to_strbuf()
222         });
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<Rc<ty::TraitRef>> {
235     let cstore = &tcx.sess.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.sess.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_native_libraries(cstore: &cstore::CStore,
249                             crate_num: ast::CrateNum)
250                                 -> Vec<(cstore::NativeLibaryKind, String)> {
251     let cdata = cstore.get_crate_data(crate_num);
252     decoder::get_native_libraries(&*cdata)
253 }
254
255 pub fn each_impl(cstore: &cstore::CStore,
256                  crate_num: ast::CrateNum,
257                  callback: |ast::DefId|) {
258     let cdata = cstore.get_crate_data(crate_num);
259     decoder::each_impl(&*cdata, callback)
260 }
261
262 pub fn each_implementation_for_type(cstore: &cstore::CStore,
263                                     def_id: ast::DefId,
264                                     callback: |ast::DefId|) {
265     let cdata = cstore.get_crate_data(def_id.krate);
266     decoder::each_implementation_for_type(&*cdata, def_id.node, callback)
267 }
268
269 pub fn each_implementation_for_trait(cstore: &cstore::CStore,
270                                      def_id: ast::DefId,
271                                      callback: |ast::DefId|) {
272     let cdata = cstore.get_crate_data(def_id.krate);
273     decoder::each_implementation_for_trait(&*cdata, def_id.node, callback)
274 }
275
276 /// If the given def ID describes a method belonging to a trait (either a
277 /// default method or an implementation of a trait method), returns the ID of
278 /// the trait that the method belongs to. Otherwise, returns `None`.
279 pub fn get_trait_of_method(cstore: &cstore::CStore,
280                            def_id: ast::DefId,
281                            tcx: &ty::ctxt)
282                            -> Option<ast::DefId> {
283     let cdata = cstore.get_crate_data(def_id.krate);
284     decoder::get_trait_of_method(&*cdata, def_id.node, tcx)
285 }
286
287 pub fn get_tuple_struct_definition_if_ctor(cstore: &cstore::CStore,
288                                            def_id: ast::DefId)
289     -> Option<ast::DefId>
290 {
291     let cdata = cstore.get_crate_data(def_id.krate);
292     decoder::get_tuple_struct_definition_if_ctor(&*cdata, def_id.node)
293 }
294
295 pub fn get_dylib_dependency_formats(cstore: &cstore::CStore,
296                                     cnum: ast::CrateNum)
297     -> Vec<(ast::CrateNum, cstore::LinkagePreference)>
298 {
299     let cdata = cstore.get_crate_data(cnum);
300     decoder::get_dylib_dependency_formats(&*cdata)
301 }
302
303 pub fn get_missing_lang_items(cstore: &cstore::CStore, cnum: ast::CrateNum)
304     -> Vec<lang_items::LangItem>
305 {
306     let cdata = cstore.get_crate_data(cnum);
307     decoder::get_missing_lang_items(&*cdata)
308 }