]> git.lizzy.rs Git - rust.git/blob - src/librustc/metadata/csearch.rs
Remove 'Local Variable' comments
[rust.git] / src / librustc / metadata / csearch.rs
1 // Copyright 2012 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 use metadata::common::*;
14 use metadata::cstore;
15 use metadata::decoder;
16 use metadata;
17 use middle::{ty, resolve};
18
19 use reader = std::ebml::reader;
20 use syntax::ast;
21 use syntax::ast_map;
22 use syntax::diagnostic::expect;
23
24 pub struct ProvidedTraitMethodInfo {
25     ty: ty::method,
26     def_id: ast::def_id
27 }
28
29 pub struct StaticMethodInfo {
30     ident: ast::ident,
31     def_id: ast::def_id,
32     purity: ast::purity
33 }
34
35 pub fn get_symbol(cstore: @mut cstore::CStore, def: ast::def_id) -> ~str {
36     let cdata = cstore::get_crate_data(cstore, def.crate).data;
37     return decoder::get_symbol(cdata, def.node);
38 }
39
40 pub fn get_type_param_count(cstore: @mut cstore::CStore, def: ast::def_id)
41                          -> uint {
42     let cdata = cstore::get_crate_data(cstore, def.crate).data;
43     return decoder::get_type_param_count(cdata, def.node);
44 }
45
46 /// Iterates over all the language items in the given crate.
47 pub fn each_lang_item(cstore: @mut cstore::CStore,
48                       cnum: ast::crate_num,
49                       f: &fn(ast::node_id, uint) -> bool) {
50     let crate_data = cstore::get_crate_data(cstore, cnum);
51     decoder::each_lang_item(crate_data, f)
52 }
53
54 /// Iterates over all the paths in the given crate.
55 pub fn each_path(cstore: @mut cstore::CStore,
56                  cnum: ast::crate_num,
57                  f: &fn(&str, decoder::def_like) -> bool) {
58     let crate_data = cstore::get_crate_data(cstore, cnum);
59     let get_crate_data: decoder::GetCrateDataCb = |cnum| {
60         cstore::get_crate_data(cstore, cnum)
61     };
62     decoder::each_path(cstore.intr, crate_data, get_crate_data, f);
63 }
64
65 pub fn get_item_path(tcx: ty::ctxt, def: ast::def_id) -> ast_map::path {
66     let cstore = tcx.cstore;
67     let cdata = cstore::get_crate_data(cstore, def.crate);
68     let path = decoder::get_item_path(cstore.intr, cdata, def.node);
69
70     // FIXME #1920: This path is not always correct if the crate is not linked
71     // into the root namespace.
72     vec::append(~[ast_map::path_mod(tcx.sess.ident_of(
73         /*bad*/copy *cdata.name))], path)
74 }
75
76 pub enum found_ast {
77     found(ast::inlined_item),
78     found_parent(ast::def_id, ast::inlined_item),
79     not_found,
80 }
81
82 // Finds the AST for this item in the crate metadata, if any.  If the item was
83 // not marked for inlining, then the AST will not be present and hence none
84 // will be returned.
85 pub fn maybe_get_item_ast(tcx: ty::ctxt, def: ast::def_id,
86                           decode_inlined_item: decoder::decode_inlined_item)
87                        -> found_ast {
88     let cstore = tcx.cstore;
89     let cdata = cstore::get_crate_data(cstore, def.crate);
90     decoder::maybe_get_item_ast(cstore.intr, cdata, tcx, def.node,
91                                 decode_inlined_item)
92 }
93
94 pub fn get_enum_variants(tcx: ty::ctxt, def: ast::def_id)
95                       -> ~[ty::VariantInfo] {
96     let cstore = tcx.cstore;
97     let cdata = cstore::get_crate_data(cstore, def.crate);
98     return decoder::get_enum_variants(cstore.intr, cdata, def.node, tcx)
99 }
100
101 pub fn get_impls_for_mod(cstore: @mut cstore::CStore, def: ast::def_id,
102                          name: Option<ast::ident>)
103                       -> @~[@resolve::Impl] {
104     let cdata = cstore::get_crate_data(cstore, def.crate);
105     do decoder::get_impls_for_mod(cstore.intr, cdata, def.node, name) |cnum| {
106         cstore::get_crate_data(cstore, cnum)
107     }
108 }
109
110 pub fn get_method(tcx: ty::ctxt,
111                   def: ast::def_id) -> ty::method
112 {
113     let cdata = cstore::get_crate_data(tcx.cstore, def.crate);
114     decoder::get_method(tcx.cstore.intr, cdata, def.node, tcx)
115 }
116
117 pub fn get_method_name_and_self_ty(cstore: @mut cstore::CStore,
118                                    def: ast::def_id) -> (ast::ident, ast::self_ty_)
119 {
120     let cdata = cstore::get_crate_data(cstore, def.crate);
121     decoder::get_method_name_and_self_ty(cstore.intr, cdata, def.node)
122 }
123
124 pub fn get_trait_method_def_ids(cstore: @mut cstore::CStore,
125                                 def: ast::def_id) -> ~[ast::def_id] {
126     let cdata = cstore::get_crate_data(cstore, def.crate);
127     decoder::get_trait_method_def_ids(cdata, def.node)
128 }
129
130 pub fn get_provided_trait_methods(tcx: ty::ctxt,
131                                   def: ast::def_id)
132                                -> ~[ProvidedTraitMethodInfo] {
133     let cstore = tcx.cstore;
134     let cdata = cstore::get_crate_data(cstore, def.crate);
135     decoder::get_provided_trait_methods(cstore.intr, cdata, def.node, tcx)
136 }
137
138 pub fn get_supertraits(tcx: ty::ctxt, def: ast::def_id) -> ~[@ty::TraitRef] {
139     let cstore = tcx.cstore;
140     let cdata = cstore::get_crate_data(cstore, def.crate);
141     decoder::get_supertraits(cdata, def.node, tcx)
142 }
143
144 pub fn get_type_name_if_impl(cstore: @mut cstore::CStore, def: ast::def_id)
145                           -> Option<ast::ident> {
146     let cdata = cstore::get_crate_data(cstore, def.crate);
147     decoder::get_type_name_if_impl(cstore.intr, cdata, def.node)
148 }
149
150 pub fn get_static_methods_if_impl(cstore: @mut cstore::CStore,
151                                   def: ast::def_id)
152                                -> Option<~[StaticMethodInfo]> {
153     let cdata = cstore::get_crate_data(cstore, def.crate);
154     decoder::get_static_methods_if_impl(cstore.intr, cdata, def.node)
155 }
156
157 pub fn get_item_attrs(cstore: @mut cstore::CStore,
158                       def_id: ast::def_id,
159                       f: &fn(~[@ast::meta_item])) {
160     let cdata = cstore::get_crate_data(cstore, def_id.crate);
161     decoder::get_item_attrs(cdata, def_id.node, f)
162 }
163
164 pub fn get_struct_fields(cstore: @mut cstore::CStore,
165                          def: ast::def_id)
166                       -> ~[ty::field_ty] {
167     let cdata = cstore::get_crate_data(cstore, def.crate);
168     decoder::get_struct_fields(cstore.intr, cdata, def.node)
169 }
170
171 pub fn get_type(tcx: ty::ctxt,
172                 def: ast::def_id)
173              -> ty::ty_param_bounds_and_ty {
174     let cstore = tcx.cstore;
175     let cdata = cstore::get_crate_data(cstore, def.crate);
176     decoder::get_type(cdata, def.node, tcx)
177 }
178
179 pub fn get_trait_def(tcx: ty::ctxt, def: ast::def_id) -> ty::TraitDef {
180     let cstore = tcx.cstore;
181     let cdata = cstore::get_crate_data(cstore, def.crate);
182     decoder::get_trait_def(cdata, def.node, tcx)
183 }
184
185 pub fn get_region_param(cstore: @mut metadata::cstore::CStore,
186                         def: ast::def_id) -> Option<ty::region_variance> {
187     let cdata = cstore::get_crate_data(cstore, def.crate);
188     return decoder::get_region_param(cdata, def.node);
189 }
190
191 pub fn get_field_type(tcx: ty::ctxt, class_id: ast::def_id,
192                       def: ast::def_id) -> ty::ty_param_bounds_and_ty {
193     let cstore = tcx.cstore;
194     let cdata = cstore::get_crate_data(cstore, class_id.crate);
195     let all_items = reader::get_doc(reader::Doc(cdata.data), tag_items);
196     debug!("Looking up %?", class_id);
197     let class_doc = expect(tcx.diag,
198                            decoder::maybe_find_item(class_id.node, all_items),
199                            || fmt!("get_field_type: class ID %? not found",
200                                    class_id) );
201     debug!("looking up %? : %?", def, class_doc);
202     let the_field = expect(tcx.diag,
203         decoder::maybe_find_item(def.node, class_doc),
204         || fmt!("get_field_type: in class %?, field ID %? not found",
205                  class_id, def) );
206     debug!("got field data %?", the_field);
207     let ty = decoder::item_type(def, the_field, tcx, cdata);
208     ty::ty_param_bounds_and_ty {
209         generics: ty::Generics {type_param_defs: @~[],
210                                 region_param: None},
211         ty: ty
212     }
213 }
214
215 // Given a def_id for an impl or class, return the traits it implements,
216 // or the empty vector if it's not for an impl or for a class that implements
217 // traits
218 pub fn get_impl_traits(tcx: ty::ctxt,
219                        def: ast::def_id) -> ~[@ty::TraitRef] {
220     let cstore = tcx.cstore;
221     let cdata = cstore::get_crate_data(cstore, def.crate);
222     decoder::get_impl_traits(cdata, def.node, tcx)
223 }
224
225 pub fn get_impl_method(cstore: @mut cstore::CStore,
226                        def: ast::def_id,
227                        mname: ast::ident)
228                     -> ast::def_id {
229     let cdata = cstore::get_crate_data(cstore, def.crate);
230     decoder::get_impl_method(cstore.intr, cdata, def.node, mname)
231 }
232
233 pub fn get_item_visibility(cstore: @mut cstore::CStore,
234                            def_id: ast::def_id)
235                         -> ast::visibility {
236     let cdata = cstore::get_crate_data(cstore, def_id.crate);
237     decoder::get_item_visibility(cdata, def_id.node)
238 }
239
240 pub fn get_link_args_for_crate(cstore: @mut cstore::CStore,
241                                crate_num: ast::crate_num)
242                             -> ~[~str] {
243     let cdata = cstore::get_crate_data(cstore, crate_num);
244     decoder::get_link_args_for_crate(cdata)
245 }