]> git.lizzy.rs Git - rust.git/blob - src/librustc/metadata/csearch.rs
rustdoc: show struct field docs when inlined
[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 use middle::subst::VecPerParamSpace;
22
23 use serialize::ebml;
24 use serialize::ebml::reader;
25 use std::rc::Rc;
26 use syntax::ast;
27 use syntax::ast_map;
28 use syntax::attr;
29 use syntax::diagnostic::expect;
30 use syntax::parse::token;
31
32 use std::collections::hashmap::HashMap;
33
34 pub struct StaticMethodInfo {
35     pub ident: ast::Ident,
36     pub def_id: ast::DefId,
37     pub fn_style: ast::FnStyle,
38     pub vis: ast::Visibility,
39 }
40
41 pub fn get_symbol(cstore: &cstore::CStore, def: ast::DefId) -> String {
42     let cdata = cstore.get_crate_data(def.krate);
43     decoder::get_symbol(cdata.data(), def.node)
44 }
45
46 /// Iterates over all the language items in the given crate.
47 pub fn each_lang_item(cstore: &cstore::CStore,
48                       cnum: ast::CrateNum,
49                       f: |ast::NodeId, uint| -> bool)
50                       -> bool {
51     let crate_data = cstore.get_crate_data(cnum);
52     decoder::each_lang_item(&*crate_data, f)
53 }
54
55 /// Iterates over each child of the given item.
56 pub fn each_child_of_item(cstore: &cstore::CStore,
57                           def_id: ast::DefId,
58                           callback: |decoder::DefLike,
59                                      ast::Ident,
60                                      ast::Visibility|) {
61     let crate_data = cstore.get_crate_data(def_id.krate);
62     let get_crate_data: decoder::GetCrateDataCb = |cnum| {
63         cstore.get_crate_data(cnum)
64     };
65     decoder::each_child_of_item(cstore.intr.clone(),
66                                 &*crate_data,
67                                 def_id.node,
68                                 get_crate_data,
69                                 callback)
70 }
71
72 /// Iterates over each top-level crate item.
73 pub fn each_top_level_item_of_crate(cstore: &cstore::CStore,
74                                     cnum: ast::CrateNum,
75                                     callback: |decoder::DefLike,
76                                                ast::Ident,
77                                                ast::Visibility|) {
78     let crate_data = cstore.get_crate_data(cnum);
79     let get_crate_data: decoder::GetCrateDataCb = |cnum| {
80         cstore.get_crate_data(cnum)
81     };
82     decoder::each_top_level_item_of_crate(cstore.intr.clone(),
83                                           &*crate_data,
84                                           get_crate_data,
85                                           callback)
86 }
87
88 pub fn get_item_path(tcx: &ty::ctxt, def: ast::DefId) -> Vec<ast_map::PathElem> {
89     let cstore = &tcx.sess.cstore;
90     let cdata = cstore.get_crate_data(def.krate);
91     let path = decoder::get_item_path(&*cdata, def.node);
92
93     // FIXME #1920: This path is not always correct if the crate is not linked
94     // into the root namespace.
95     (vec!(ast_map::PathMod(token::intern(cdata.name.as_slice())))).append(
96         path.as_slice())
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.sess.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<Rc<ty::VariantInfo>> {
118     let cstore = &tcx.sess.cstore;
119     let cdata = cstore.get_crate_data(def.krate);
120     decoder::get_enum_variants(cstore.intr.clone(), &*cdata, def.node, tcx)
121 }
122
123 /// Returns information about the given implementation.
124 pub fn get_impl_methods(cstore: &cstore::CStore, impl_def_id: ast::DefId)
125                         -> Vec<ast::DefId> {
126     let cdata = cstore.get_crate_data(impl_def_id.krate);
127     decoder::get_impl_methods(&*cdata, impl_def_id.node)
128 }
129
130 pub fn get_method(tcx: &ty::ctxt, def: ast::DefId) -> ty::Method {
131     let cdata = tcx.sess.cstore.get_crate_data(def.krate);
132     decoder::get_method(tcx.sess.cstore.intr.clone(), &*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,
138                                              ty::ExplicitSelfCategory)
139 {
140     let cdata = cstore.get_crate_data(def.krate);
141     decoder::get_method_name_and_explicit_self(cstore.intr.clone(), &*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<Rc<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.clone(), &*cdata, def.node, tcx)
162 }
163
164 pub fn get_supertraits(tcx: &ty::ctxt, def: ast::DefId) -> Vec<Rc<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.clone(), &*cdata, def.node)
181 }
182
183 pub fn get_item_attrs(cstore: &cstore::CStore,
184                       def_id: ast::DefId,
185                       f: |Vec<ast::Attribute>|) {
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.clone(), &*cdata, def.node)
195 }
196
197 pub fn get_struct_field_attrs(cstore: &cstore::CStore, def: ast::DefId) -> HashMap<ast::NodeId,
198         Vec<ast::Attribute>> {
199     let cdata = cstore.get_crate_data(def.krate);
200     decoder::get_struct_field_attrs(&*cdata)
201 }
202
203 pub fn get_type(tcx: &ty::ctxt,
204                 def: ast::DefId)
205              -> ty::Polytype {
206     let cstore = &tcx.sess.cstore;
207     let cdata = cstore.get_crate_data(def.krate);
208     decoder::get_type(&*cdata, def.node, tcx)
209 }
210
211 pub fn get_trait_def(tcx: &ty::ctxt, def: ast::DefId) -> ty::TraitDef {
212     let cstore = &tcx.sess.cstore;
213     let cdata = cstore.get_crate_data(def.krate);
214     decoder::get_trait_def(&*cdata, def.node, tcx)
215 }
216
217 pub fn get_field_type(tcx: &ty::ctxt, class_id: ast::DefId,
218                       def: ast::DefId) -> ty::Polytype {
219     let cstore = &tcx.sess.cstore;
220     let cdata = cstore.get_crate_data(class_id.krate);
221     let all_items = reader::get_doc(ebml::Doc::new(cdata.data()), tag_items);
222     let class_doc = expect(tcx.sess.diagnostic(),
223                            decoder::maybe_find_item(class_id.node, all_items),
224                            || {
225         (format!("get_field_type: class ID {:?} not found",
226                  class_id)).to_string()
227     });
228     let the_field = expect(tcx.sess.diagnostic(),
229         decoder::maybe_find_item(def.node, class_doc),
230         || {
231             (format!("get_field_type: in class {:?}, field ID {:?} not found",
232                     class_id,
233                     def)).to_string()
234         });
235     let ty = decoder::item_type(def, the_field, tcx, &*cdata);
236     ty::Polytype {
237         generics: ty::Generics {types: VecPerParamSpace::empty(),
238                                 regions: VecPerParamSpace::empty()},
239         ty: ty
240     }
241 }
242
243 // Given a def_id for an impl, return the trait it implements,
244 // if there is one.
245 pub fn get_impl_trait(tcx: &ty::ctxt,
246                       def: ast::DefId) -> Option<Rc<ty::TraitRef>> {
247     let cstore = &tcx.sess.cstore;
248     let cdata = cstore.get_crate_data(def.krate);
249     decoder::get_impl_trait(&*cdata, def.node, tcx)
250 }
251
252 // Given a def_id for an impl, return information about its vtables
253 pub fn get_impl_vtables(tcx: &ty::ctxt,
254                         def: ast::DefId)
255                         -> typeck::vtable_res {
256     let cstore = &tcx.sess.cstore;
257     let cdata = cstore.get_crate_data(def.krate);
258     decoder::get_impl_vtables(&*cdata, def.node, tcx)
259 }
260
261 pub fn get_native_libraries(cstore: &cstore::CStore,
262                             crate_num: ast::CrateNum)
263                                 -> Vec<(cstore::NativeLibaryKind, String)> {
264     let cdata = cstore.get_crate_data(crate_num);
265     decoder::get_native_libraries(&*cdata)
266 }
267
268 pub fn each_impl(cstore: &cstore::CStore,
269                  crate_num: ast::CrateNum,
270                  callback: |ast::DefId|) {
271     let cdata = cstore.get_crate_data(crate_num);
272     decoder::each_impl(&*cdata, callback)
273 }
274
275 pub fn each_implementation_for_type(cstore: &cstore::CStore,
276                                     def_id: ast::DefId,
277                                     callback: |ast::DefId|) {
278     let cdata = cstore.get_crate_data(def_id.krate);
279     decoder::each_implementation_for_type(&*cdata, def_id.node, callback)
280 }
281
282 pub fn each_implementation_for_trait(cstore: &cstore::CStore,
283                                      def_id: ast::DefId,
284                                      callback: |ast::DefId|) {
285     let cdata = cstore.get_crate_data(def_id.krate);
286     decoder::each_implementation_for_trait(&*cdata, def_id.node, callback)
287 }
288
289 /// If the given def ID describes a method belonging to a trait (either a
290 /// default method or an implementation of a trait method), returns the ID of
291 /// the trait that the method belongs to. Otherwise, returns `None`.
292 pub fn get_trait_of_method(cstore: &cstore::CStore,
293                            def_id: ast::DefId,
294                            tcx: &ty::ctxt)
295                            -> Option<ast::DefId> {
296     let cdata = cstore.get_crate_data(def_id.krate);
297     decoder::get_trait_of_method(&*cdata, def_id.node, tcx)
298 }
299
300 pub fn get_tuple_struct_definition_if_ctor(cstore: &cstore::CStore,
301                                            def_id: ast::DefId)
302     -> Option<ast::DefId>
303 {
304     let cdata = cstore.get_crate_data(def_id.krate);
305     decoder::get_tuple_struct_definition_if_ctor(&*cdata, def_id.node)
306 }
307
308 pub fn get_dylib_dependency_formats(cstore: &cstore::CStore,
309                                     cnum: ast::CrateNum)
310     -> Vec<(ast::CrateNum, cstore::LinkagePreference)>
311 {
312     let cdata = cstore.get_crate_data(cnum);
313     decoder::get_dylib_dependency_formats(&*cdata)
314 }
315
316 pub fn get_missing_lang_items(cstore: &cstore::CStore, cnum: ast::CrateNum)
317     -> Vec<lang_items::LangItem>
318 {
319     let cdata = cstore.get_crate_data(cnum);
320     decoder::get_missing_lang_items(&*cdata)
321 }
322
323 pub fn get_method_arg_names(cstore: &cstore::CStore, did: ast::DefId)
324     -> Vec<String>
325 {
326     let cdata = cstore.get_crate_data(did.krate);
327     decoder::get_method_arg_names(&*cdata, did.node)
328 }
329
330 pub fn get_reachable_extern_fns(cstore: &cstore::CStore, cnum: ast::CrateNum)
331     -> Vec<ast::DefId>
332 {
333     let cdata = cstore.get_crate_data(cnum);
334     decoder::get_reachable_extern_fns(&*cdata)
335 }
336
337 pub fn is_typedef(cstore: &cstore::CStore, did: ast::DefId) -> bool {
338     let cdata = cstore.get_crate_data(did.krate);
339     decoder::is_typedef(&*cdata, did.node)
340 }
341
342 pub fn get_stability(cstore: &cstore::CStore,
343                      def: ast::DefId)
344                      -> Option<attr::Stability> {
345     let cdata = cstore.get_crate_data(def.krate);
346     decoder::get_stability(&*cdata, def.node)
347 }