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.
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.
11 // Searching for information from the cstore
14 use metadata
::common
::*;
16 use metadata
::decoder
;
17 use middle
::lang_items
;
25 use syntax
::attr
::AttrMetaMethods
;
26 use syntax
::diagnostic
::expect
;
27 use syntax
::parse
::token
;
29 use std
::collections
::hash_map
::HashMap
;
31 #[derive(Copy, Clone)]
32 pub struct MethodInfo
{
34 pub def_id
: ast
::DefId
,
35 pub vis
: ast
::Visibility
,
38 pub fn get_symbol(cstore
: &cstore
::CStore
, def
: ast
::DefId
) -> String
{
39 let cdata
= cstore
.get_crate_data(def
.krate
);
40 decoder
::get_symbol(cdata
.data(), def
.node
)
43 /// Iterates over all the language items in the given crate.
44 pub fn each_lang_item
<F
>(cstore
: &cstore
::CStore
,
48 F
: FnMut(ast
::NodeId
, usize) -> bool
,
50 let crate_data
= cstore
.get_crate_data(cnum
);
51 decoder
::each_lang_item(&*crate_data
, f
)
54 /// Iterates over each child of the given item.
55 pub fn each_child_of_item
<F
>(cstore
: &cstore
::CStore
,
58 F
: FnMut(decoder
::DefLike
, ast
::Name
, ast
::Visibility
),
60 let crate_data
= cstore
.get_crate_data(def_id
.krate
);
61 let get_crate_data
= |cnum
| {
62 cstore
.get_crate_data(cnum
)
64 decoder
::each_child_of_item(cstore
.intr
.clone(),
71 /// Iterates over each top-level crate item.
72 pub fn each_top_level_item_of_crate
<F
>(cstore
: &cstore
::CStore
,
75 F
: FnMut(decoder
::DefLike
, ast
::Name
, ast
::Visibility
),
77 let crate_data
= cstore
.get_crate_data(cnum
);
78 let get_crate_data
= |cnum
| {
79 cstore
.get_crate_data(cnum
)
81 decoder
::each_top_level_item_of_crate(cstore
.intr
.clone(),
87 pub fn get_item_path(tcx
: &ty
::ctxt
, def
: ast
::DefId
) -> Vec
<ast_map
::PathElem
> {
88 let cstore
= &tcx
.sess
.cstore
;
89 let cdata
= cstore
.get_crate_data(def
.krate
);
90 let path
= decoder
::get_item_path(&*cdata
, def
.node
);
92 // FIXME #1920: This path is not always correct if the crate is not linked
93 // into the root namespace.
94 let mut r
= vec
![ast_map
::PathMod(token
::intern(&cdata
.name
))];
99 pub enum FoundAst
<'ast
> {
100 Found(&'ast ast
::InlinedItem
),
101 FoundParent(ast
::DefId
, &'ast ast
::InlinedItem
),
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
108 pub fn maybe_get_item_ast
<'tcx
>(tcx
: &ty
::ctxt
<'tcx
>, def
: ast
::DefId
,
109 decode_inlined_item
: decoder
::DecodeInlinedItem
)
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
)
116 pub fn get_enum_variants
<'tcx
>(tcx
: &ty
::ctxt
<'tcx
>, def
: ast
::DefId
)
117 -> Vec
<Rc
<ty
::VariantInfo
<'tcx
>>> {
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
)
123 /// Returns information about the given implementation.
124 pub fn get_impl_items(cstore
: &cstore
::CStore
, impl_def_id
: ast
::DefId
)
125 -> Vec
<ty
::ImplOrTraitItemId
> {
126 let cdata
= cstore
.get_crate_data(impl_def_id
.krate
);
127 decoder
::get_impl_items(&*cdata
, impl_def_id
.node
)
130 pub fn get_impl_or_trait_item
<'tcx
>(tcx
: &ty
::ctxt
<'tcx
>, def
: ast
::DefId
)
131 -> ty
::ImplOrTraitItem
<'tcx
> {
132 let cdata
= tcx
.sess
.cstore
.get_crate_data(def
.krate
);
133 decoder
::get_impl_or_trait_item(tcx
.sess
.cstore
.intr
.clone(),
139 pub fn get_trait_name(cstore
: &cstore
::CStore
, def
: ast
::DefId
) -> ast
::Name
{
140 let cdata
= cstore
.get_crate_data(def
.krate
);
141 decoder
::get_trait_name(cstore
.intr
.clone(),
146 pub fn is_static_method(cstore
: &cstore
::CStore
, def
: ast
::DefId
) -> bool
{
147 let cdata
= cstore
.get_crate_data(def
.krate
);
148 decoder
::is_static_method(&*cdata
, def
.node
)
151 pub fn get_trait_item_def_ids(cstore
: &cstore
::CStore
, def
: ast
::DefId
)
152 -> Vec
<ty
::ImplOrTraitItemId
> {
153 let cdata
= cstore
.get_crate_data(def
.krate
);
154 decoder
::get_trait_item_def_ids(&*cdata
, def
.node
)
157 pub fn get_item_variances(cstore
: &cstore
::CStore
,
158 def
: ast
::DefId
) -> ty
::ItemVariances
{
159 let cdata
= cstore
.get_crate_data(def
.krate
);
160 decoder
::get_item_variances(&*cdata
, def
.node
)
163 pub fn get_provided_trait_methods
<'tcx
>(tcx
: &ty
::ctxt
<'tcx
>,
165 -> Vec
<Rc
<ty
::Method
<'tcx
>>> {
166 let cstore
= &tcx
.sess
.cstore
;
167 let cdata
= cstore
.get_crate_data(def
.krate
);
168 decoder
::get_provided_trait_methods(cstore
.intr
.clone(), &*cdata
, def
.node
, tcx
)
171 pub fn get_associated_consts
<'tcx
>(tcx
: &ty
::ctxt
<'tcx
>, def
: ast
::DefId
)
172 -> Vec
<Rc
<ty
::AssociatedConst
<'tcx
>>> {
173 let cstore
= &tcx
.sess
.cstore
;
174 let cdata
= cstore
.get_crate_data(def
.krate
);
175 decoder
::get_associated_consts(cstore
.intr
.clone(), &*cdata
, def
.node
, tcx
)
178 pub fn get_type_name_if_impl(cstore
: &cstore
::CStore
, def
: ast
::DefId
)
179 -> Option
<ast
::Name
> {
180 let cdata
= cstore
.get_crate_data(def
.krate
);
181 decoder
::get_type_name_if_impl(&*cdata
, def
.node
)
184 pub fn get_methods_if_impl(cstore
: &cstore
::CStore
,
186 -> Option
<Vec
<MethodInfo
> > {
187 let cdata
= cstore
.get_crate_data(def
.krate
);
188 decoder
::get_methods_if_impl(cstore
.intr
.clone(), &*cdata
, def
.node
)
191 pub fn get_item_attrs(cstore
: &cstore
::CStore
,
193 -> Vec
<ast
::Attribute
> {
194 let cdata
= cstore
.get_crate_data(def_id
.krate
);
195 decoder
::get_item_attrs(&*cdata
, def_id
.node
)
198 pub fn get_struct_fields(cstore
: &cstore
::CStore
,
200 -> Vec
<ty
::FieldTy
> {
201 let cdata
= cstore
.get_crate_data(def
.krate
);
202 decoder
::get_struct_fields(cstore
.intr
.clone(), &*cdata
, def
.node
)
205 pub fn get_struct_field_attrs(cstore
: &cstore
::CStore
, def
: ast
::DefId
) -> HashMap
<ast
::NodeId
,
206 Vec
<ast
::Attribute
>> {
207 let cdata
= cstore
.get_crate_data(def
.krate
);
208 decoder
::get_struct_field_attrs(&*cdata
)
211 pub fn get_type
<'tcx
>(tcx
: &ty
::ctxt
<'tcx
>,
213 -> ty
::TypeScheme
<'tcx
> {
214 let cstore
= &tcx
.sess
.cstore
;
215 let cdata
= cstore
.get_crate_data(def
.krate
);
216 decoder
::get_type(&*cdata
, def
.node
, tcx
)
219 pub fn get_trait_def
<'tcx
>(tcx
: &ty
::ctxt
<'tcx
>, def
: ast
::DefId
) -> ty
::TraitDef
<'tcx
> {
220 let cstore
= &tcx
.sess
.cstore
;
221 let cdata
= cstore
.get_crate_data(def
.krate
);
222 decoder
::get_trait_def(&*cdata
, def
.node
, tcx
)
225 pub fn get_predicates
<'tcx
>(tcx
: &ty
::ctxt
<'tcx
>, def
: ast
::DefId
)
226 -> ty
::GenericPredicates
<'tcx
>
228 let cstore
= &tcx
.sess
.cstore
;
229 let cdata
= cstore
.get_crate_data(def
.krate
);
230 decoder
::get_predicates(&*cdata
, def
.node
, tcx
)
233 pub fn get_super_predicates
<'tcx
>(tcx
: &ty
::ctxt
<'tcx
>, def
: ast
::DefId
)
234 -> ty
::GenericPredicates
<'tcx
>
236 let cstore
= &tcx
.sess
.cstore
;
237 let cdata
= cstore
.get_crate_data(def
.krate
);
238 decoder
::get_super_predicates(&*cdata
, def
.node
, tcx
)
241 pub fn get_field_type
<'tcx
>(tcx
: &ty
::ctxt
<'tcx
>, class_id
: ast
::DefId
,
242 def
: ast
::DefId
) -> ty
::TypeScheme
<'tcx
> {
243 let cstore
= &tcx
.sess
.cstore
;
244 let cdata
= cstore
.get_crate_data(class_id
.krate
);
245 let all_items
= reader
::get_doc(rbml
::Doc
::new(cdata
.data()), tag_items
);
246 let class_doc
= expect(tcx
.sess
.diagnostic(),
247 decoder
::maybe_find_item(class_id
.node
, all_items
),
249 (format
!("get_field_type: class ID {:?} not found",
250 class_id
)).to_string()
252 let the_field
= expect(tcx
.sess
.diagnostic(),
253 decoder
::maybe_find_item(def
.node
, class_doc
),
255 (format
!("get_field_type: in class {:?}, field ID {:?} not found",
259 let ty
= decoder
::item_type(def
, the_field
, tcx
, &*cdata
);
261 generics
: ty
::Generics
::empty(),
266 pub fn get_impl_polarity
<'tcx
>(tcx
: &ty
::ctxt
<'tcx
>,
268 -> Option
<ast
::ImplPolarity
>
270 let cstore
= &tcx
.sess
.cstore
;
271 let cdata
= cstore
.get_crate_data(def
.krate
);
272 decoder
::get_impl_polarity(&*cdata
, def
.node
)
275 pub fn get_custom_coerce_unsized_kind
<'tcx
>(tcx
: &ty
::ctxt
<'tcx
>,
277 -> Option
<ty
::CustomCoerceUnsized
> {
278 let cstore
= &tcx
.sess
.cstore
;
279 let cdata
= cstore
.get_crate_data(def
.krate
);
280 decoder
::get_custom_coerce_unsized_kind(&*cdata
, def
.node
)
283 // Given a def_id for an impl, return the trait it implements,
285 pub fn get_impl_trait
<'tcx
>(tcx
: &ty
::ctxt
<'tcx
>,
287 -> Option
<ty
::TraitRef
<'tcx
>> {
288 let cstore
= &tcx
.sess
.cstore
;
289 let cdata
= cstore
.get_crate_data(def
.krate
);
290 decoder
::get_impl_trait(&*cdata
, def
.node
, tcx
)
293 pub fn get_native_libraries(cstore
: &cstore
::CStore
, crate_num
: ast
::CrateNum
)
294 -> Vec
<(cstore
::NativeLibraryKind
, String
)> {
295 let cdata
= cstore
.get_crate_data(crate_num
);
296 decoder
::get_native_libraries(&*cdata
)
299 pub fn each_inherent_implementation_for_type
<F
>(cstore
: &cstore
::CStore
,
302 F
: FnMut(ast
::DefId
),
304 let cdata
= cstore
.get_crate_data(def_id
.krate
);
305 decoder
::each_inherent_implementation_for_type(&*cdata
, def_id
.node
, callback
)
308 pub fn each_implementation_for_trait
<F
>(cstore
: &cstore
::CStore
,
310 mut callback
: F
) where
311 F
: FnMut(ast
::DefId
),
313 cstore
.iter_crate_data(|_
, cdata
| {
314 decoder
::each_implementation_for_trait(cdata
, def_id
, &mut callback
)
318 /// If the given def ID describes an item belonging to a trait (either a
319 /// default method or an implementation of a trait method), returns the ID of
320 /// the trait that the method belongs to. Otherwise, returns `None`.
321 pub fn get_trait_of_item(cstore
: &cstore
::CStore
,
324 -> Option
<ast
::DefId
> {
325 let cdata
= cstore
.get_crate_data(def_id
.krate
);
326 decoder
::get_trait_of_item(&*cdata
, def_id
.node
, tcx
)
329 pub fn get_tuple_struct_definition_if_ctor(cstore
: &cstore
::CStore
,
331 -> Option
<ast
::DefId
>
333 let cdata
= cstore
.get_crate_data(def_id
.krate
);
334 decoder
::get_tuple_struct_definition_if_ctor(&*cdata
, def_id
.node
)
337 pub fn get_dylib_dependency_formats(cstore
: &cstore
::CStore
,
339 -> Vec
<(ast
::CrateNum
, cstore
::LinkagePreference
)>
341 let cdata
= cstore
.get_crate_data(cnum
);
342 decoder
::get_dylib_dependency_formats(&*cdata
)
345 pub fn get_missing_lang_items(cstore
: &cstore
::CStore
, cnum
: ast
::CrateNum
)
346 -> Vec
<lang_items
::LangItem
>
348 let cdata
= cstore
.get_crate_data(cnum
);
349 decoder
::get_missing_lang_items(&*cdata
)
352 pub fn get_method_arg_names(cstore
: &cstore
::CStore
, did
: ast
::DefId
)
355 let cdata
= cstore
.get_crate_data(did
.krate
);
356 decoder
::get_method_arg_names(&*cdata
, did
.node
)
359 pub fn get_reachable_extern_fns(cstore
: &cstore
::CStore
, cnum
: ast
::CrateNum
)
362 let cdata
= cstore
.get_crate_data(cnum
);
363 decoder
::get_reachable_extern_fns(&*cdata
)
366 pub fn is_typedef(cstore
: &cstore
::CStore
, did
: ast
::DefId
) -> bool
{
367 let cdata
= cstore
.get_crate_data(did
.krate
);
368 decoder
::is_typedef(&*cdata
, did
.node
)
371 pub fn is_const_fn(cstore
: &cstore
::CStore
, did
: ast
::DefId
) -> bool
{
372 let cdata
= cstore
.get_crate_data(did
.krate
);
373 decoder
::is_const_fn(&*cdata
, did
.node
)
376 pub fn is_impl(cstore
: &cstore
::CStore
, did
: ast
::DefId
) -> bool
{
377 let cdata
= cstore
.get_crate_data(did
.krate
);
378 decoder
::is_impl(&*cdata
, did
.node
)
381 pub fn get_stability(cstore
: &cstore
::CStore
,
383 -> Option
<attr
::Stability
> {
384 let cdata
= cstore
.get_crate_data(def
.krate
);
385 decoder
::get_stability(&*cdata
, def
.node
)
388 pub fn is_staged_api(cstore
: &cstore
::CStore
, krate
: ast
::CrateNum
) -> bool
{
389 let cdata
= cstore
.get_crate_data(krate
);
390 let attrs
= decoder
::get_crate_attributes(cdata
.data());
392 if &attr
.name()[..] == "staged_api" {
393 match attr
.node
.value
.node { ast::MetaWord(_) => return true, _ => (/*pass*/) }
400 pub fn get_repr_attrs(cstore
: &cstore
::CStore
, def
: ast
::DefId
)
401 -> Vec
<attr
::ReprAttr
> {
402 let cdata
= cstore
.get_crate_data(def
.krate
);
403 decoder
::get_repr_attrs(&*cdata
, def
.node
)
406 pub fn is_defaulted_trait(cstore
: &cstore
::CStore
, trait_def_id
: ast
::DefId
) -> bool
{
407 let cdata
= cstore
.get_crate_data(trait_def_id
.krate
);
408 decoder
::is_defaulted_trait(&*cdata
, trait_def_id
.node
)
411 pub fn is_default_impl(cstore
: &cstore
::CStore
, impl_did
: ast
::DefId
) -> bool
{
412 let cdata
= cstore
.get_crate_data(impl_did
.krate
);
413 decoder
::is_default_impl(&*cdata
, impl_did
.node
)