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