]>
git.proxmox.com Git - rustc.git/blob - 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.
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.
12 // Searching for information from the cstore
16 use metadata
::common
::*;
18 use metadata
::decoder
;
20 use middle
::{ty, resolve}
;
23 use reader
= std
::ebml
::reader
;
26 use syntax
::diagnostic
::expect
;
28 pub struct ProvidedTraitMethodInfo
{
33 pub struct StaticMethodInfo
{
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
);
44 pub fn get_type_param_count(cstore
: @
mut cstore
::CStore
, def
: ast
::def_id
)
46 let cdata
= cstore
::get_crate_data(cstore
, def
.crate).data
;
47 return decoder
::get_type_param_count(cdata
, def
.node
);
50 /// Iterates over all the language items in the given crate.
51 pub fn each_lang_item(cstore
: @
mut cstore
::CStore
,
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
)
58 /// Iterates over all the paths in the given crate.
59 pub fn each_path(cstore
: @
mut cstore
::CStore
,
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
)
66 decoder
::each_path(cstore
.intr
, crate_data
, get_crate_data
, f
);
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
);
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
)
81 found(ast
::inlined_item
),
82 found_parent(ast
::def_id
, ast
::inlined_item
),
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
89 pub fn maybe_get_item_ast(tcx
: ty
::ctxt
, def
: ast
::def_id
,
90 decode_inlined_item
: decoder
::decode_inlined_item
)
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
,
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
)
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
)
114 pub fn get_trait_methods(tcx
: ty
::ctxt
,
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
)
122 pub fn get_provided_trait_methods(tcx
: ty
::ctxt
,
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
)
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
)
136 pub fn get_method_names_if_trait(cstore
: @
mut cstore
::CStore
,
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
);
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
)
149 pub fn get_static_methods_if_impl(cstore
: @
mut cstore
::CStore
,
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
)
156 pub fn get_item_attrs(cstore
: @
mut cstore
::CStore
,
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
)
163 pub fn get_struct_fields(cstore
: @
mut cstore
::CStore
,
166 let cdata
= cstore
::get_crate_data(cstore
, def
.crate);
167 decoder
::get_struct_fields(cstore
.intr
, cdata
, def
.node
)
170 pub fn get_type(tcx
: ty
::ctxt
,
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
)
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
);
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",
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",
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
{
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
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
)
217 pub fn get_impl_method(cstore
: @
mut cstore
::CStore
,
221 let cdata
= cstore
::get_crate_data(cstore
, def
.crate);
222 decoder
::get_impl_method(cstore
.intr
, cdata
, def
.node
, mname
)
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
)
232 pub fn get_item_visibility(cstore
: @
mut cstore
::CStore
,
235 let cdata
= cstore
::get_crate_data(cstore
, def_id
.crate);
236 decoder
::get_item_visibility(cdata
, def_id
.node
)
239 pub fn get_link_args_for_crate(cstore
: @
mut cstore
::CStore
,
240 crate_num
: ast
::crate_num
)
242 let cdata
= cstore
::get_crate_data(cstore
, crate_num
);
243 decoder
::get_link_args_for_crate(cdata
)
249 // indent-tabs-mode: nil
251 // buffer-file-coding-system: utf-8-unix