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