2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2011-07-08 01:29:09 -05:00
|
|
|
// Searching for information from the cstore
|
|
|
|
|
2012-12-13 15:05:22 -06:00
|
|
|
use metadata::common::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use metadata::cstore;
|
|
|
|
use metadata::decoder;
|
|
|
|
use metadata;
|
2013-02-19 01:40:42 -06:00
|
|
|
use middle::{ty, resolve};
|
2012-12-13 15:05:22 -06:00
|
|
|
|
2012-12-06 18:13:54 -06:00
|
|
|
use reader = std::ebml::reader;
|
2012-09-04 13:54:36 -05:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::ast_map;
|
|
|
|
use syntax::diagnostic::expect;
|
2011-07-08 16:53:25 -05:00
|
|
|
|
2013-01-29 18:51:16 -06:00
|
|
|
pub struct ProvidedTraitMethodInfo {
|
2012-10-08 14:39:30 -05:00
|
|
|
ty: ty::method,
|
|
|
|
def_id: ast::def_id
|
|
|
|
}
|
2011-07-08 00:18:38 -05:00
|
|
|
|
2013-01-29 18:51:16 -06:00
|
|
|
pub struct StaticMethodInfo {
|
2012-10-18 15:29:34 -05:00
|
|
|
ident: ast::ident,
|
|
|
|
def_id: ast::def_id,
|
|
|
|
purity: ast::purity
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn get_symbol(cstore: @mut cstore::CStore, def: ast::def_id) -> ~str {
|
2011-07-27 07:19:39 -05:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate).data;
|
2012-08-01 19:30:05 -05:00
|
|
|
return decoder::get_symbol(cdata, def.node);
|
2011-07-08 00:18:38 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn get_type_param_count(cstore: @mut cstore::CStore, def: ast::def_id)
|
2013-01-29 18:51:16 -06:00
|
|
|
-> uint {
|
2011-07-27 07:19:39 -05:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate).data;
|
2012-08-01 19:30:05 -05:00
|
|
|
return decoder::get_type_param_count(cdata, def.node);
|
2011-07-08 00:18:38 -05:00
|
|
|
}
|
|
|
|
|
2013-01-07 12:51:53 -06:00
|
|
|
/// Iterates over all the language items in the given crate.
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn each_lang_item(cstore: @mut cstore::CStore,
|
2013-01-29 18:51:16 -06:00
|
|
|
cnum: ast::crate_num,
|
|
|
|
f: &fn(ast::node_id, uint) -> bool) {
|
2013-01-07 12:51:53 -06:00
|
|
|
let crate_data = cstore::get_crate_data(cstore, cnum);
|
|
|
|
decoder::each_lang_item(crate_data, f)
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Iterates over all the paths in the given crate.
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn each_path(cstore: @mut cstore::CStore,
|
|
|
|
cnum: ast::crate_num,
|
|
|
|
f: &fn(&str, decoder::def_like) -> bool) {
|
2012-05-22 12:54:12 -05:00
|
|
|
let crate_data = cstore::get_crate_data(cstore, cnum);
|
2012-11-08 18:52:21 -06:00
|
|
|
let get_crate_data: decoder::GetCrateDataCb = |cnum| {
|
|
|
|
cstore::get_crate_data(cstore, cnum)
|
|
|
|
};
|
|
|
|
decoder::each_path(cstore.intr, crate_data, get_crate_data, f);
|
2012-05-22 12:54:12 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 18:51:16 -06:00
|
|
|
pub fn get_item_path(tcx: ty::ctxt, def: ast::def_id) -> ast_map::path {
|
2012-05-22 19:48:04 -05:00
|
|
|
let cstore = tcx.cstore;
|
2012-02-10 08:01:32 -06:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-07-18 18:18:02 -05:00
|
|
|
let path = decoder::get_item_path(cstore.intr, cdata, def.node);
|
2012-03-02 22:06:08 -06:00
|
|
|
|
|
|
|
// FIXME #1920: This path is not always correct if the crate is not linked
|
|
|
|
// into the root namespace.
|
2013-01-07 16:16:52 -06:00
|
|
|
vec::append(~[ast_map::path_mod(tcx.sess.ident_of(
|
2013-02-16 12:16:32 -06:00
|
|
|
/*bad*/copy *cdata.name))], path)
|
2012-02-10 08:01:32 -06:00
|
|
|
}
|
|
|
|
|
2013-01-29 18:51:16 -06:00
|
|
|
pub enum found_ast {
|
2012-03-08 16:13:57 -06:00
|
|
|
found(ast::inlined_item),
|
|
|
|
found_parent(ast::def_id, ast::inlined_item),
|
|
|
|
not_found,
|
|
|
|
}
|
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
// Finds the AST for this item in the crate metadata, if any. If the item was
|
|
|
|
// not marked for inlining, then the AST will not be present and hence none
|
|
|
|
// will be returned.
|
2013-01-29 18:51:16 -06:00
|
|
|
pub fn maybe_get_item_ast(tcx: ty::ctxt, def: ast::def_id,
|
|
|
|
decode_inlined_item: decoder::decode_inlined_item)
|
|
|
|
-> found_ast {
|
2012-05-22 19:48:04 -05:00
|
|
|
let cstore = tcx.cstore;
|
2012-02-14 17:21:53 -06:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-07-18 18:18:02 -05:00
|
|
|
decoder::maybe_get_item_ast(cstore.intr, cdata, tcx, def.node,
|
2012-05-14 19:46:45 -05:00
|
|
|
decode_inlined_item)
|
2012-02-14 17:21:53 -06:00
|
|
|
}
|
|
|
|
|
2013-01-29 18:51:16 -06:00
|
|
|
pub fn get_enum_variants(tcx: ty::ctxt, def: ast::def_id)
|
|
|
|
-> ~[ty::VariantInfo] {
|
2012-05-22 19:48:04 -05:00
|
|
|
let cstore = tcx.cstore;
|
2012-01-05 09:04:59 -06:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-07-18 18:18:02 -05:00
|
|
|
return decoder::get_enum_variants(cstore.intr, cdata, def.node, tcx)
|
2011-07-08 00:18:38 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn get_impls_for_mod(cstore: @mut cstore::CStore, def: ast::def_id,
|
2013-01-29 18:51:16 -06:00
|
|
|
name: Option<ast::ident>)
|
2013-02-19 01:40:42 -06:00
|
|
|
-> @~[@resolve::Impl] {
|
2012-01-05 09:04:59 -06:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-07-18 18:18:02 -05:00
|
|
|
do decoder::get_impls_for_mod(cstore.intr, cdata, def.node, name) |cnum| {
|
2012-04-16 16:58:58 -05:00
|
|
|
cstore::get_crate_data(cstore, cnum)
|
|
|
|
}
|
2011-12-16 07:41:12 -06:00
|
|
|
}
|
|
|
|
|
2013-03-27 09:26:57 -05:00
|
|
|
pub fn get_method(tcx: ty::ctxt,
|
|
|
|
def: ast::def_id) -> ty::method
|
|
|
|
{
|
|
|
|
let cdata = cstore::get_crate_data(tcx.cstore, def.crate);
|
|
|
|
decoder::get_method(tcx.cstore.intr, cdata, def.node, tcx)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_method_name_and_self_ty(cstore: @mut cstore::CStore,
|
|
|
|
def: ast::def_id) -> (ast::ident, ast::self_ty_)
|
|
|
|
{
|
2012-01-05 09:04:59 -06:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2013-03-27 09:26:57 -05:00
|
|
|
decoder::get_method_name_and_self_ty(cstore.intr, cdata, def.node)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_trait_method_def_ids(cstore: @mut cstore::CStore,
|
|
|
|
def: ast::def_id) -> ~[ast::def_id] {
|
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
|
|
|
decoder::get_trait_method_def_ids(cdata, def.node)
|
2011-12-16 07:41:12 -06:00
|
|
|
}
|
|
|
|
|
2013-01-29 18:51:16 -06:00
|
|
|
pub fn get_provided_trait_methods(tcx: ty::ctxt,
|
|
|
|
def: ast::def_id)
|
|
|
|
-> ~[ProvidedTraitMethodInfo] {
|
2012-10-08 14:39:30 -05:00
|
|
|
let cstore = tcx.cstore;
|
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
|
|
|
decoder::get_provided_trait_methods(cstore.intr, cdata, def.node, tcx)
|
|
|
|
}
|
|
|
|
|
2013-03-27 05:16:28 -05:00
|
|
|
pub fn get_supertraits(tcx: ty::ctxt, def: ast::def_id) -> ~[@ty::TraitRef] {
|
2012-10-19 23:27:01 -05:00
|
|
|
let cstore = tcx.cstore;
|
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
|
|
|
decoder::get_supertraits(cdata, def.node, tcx)
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn get_type_name_if_impl(cstore: @mut cstore::CStore, def: ast::def_id)
|
|
|
|
-> Option<ast::ident> {
|
2012-10-18 15:29:34 -05:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
|
|
|
decoder::get_type_name_if_impl(cstore.intr, cdata, def.node)
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn get_static_methods_if_impl(cstore: @mut cstore::CStore,
|
2013-01-29 18:51:16 -06:00
|
|
|
def: ast::def_id)
|
|
|
|
-> Option<~[StaticMethodInfo]> {
|
2012-10-18 15:29:34 -05:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
|
|
|
decoder::get_static_methods_if_impl(cstore.intr, cdata, def.node)
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn get_item_attrs(cstore: @mut cstore::CStore,
|
2013-01-29 18:51:16 -06:00
|
|
|
def_id: ast::def_id,
|
2013-03-07 16:38:38 -06:00
|
|
|
f: &fn(~[@ast::meta_item])) {
|
2012-07-25 20:36:18 -05:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def_id.crate);
|
|
|
|
decoder::get_item_attrs(cdata, def_id.node, f)
|
|
|
|
}
|
|
|
|
|
2013-02-11 18:28:39 -06:00
|
|
|
pub fn get_struct_fields(cstore: @mut cstore::CStore,
|
|
|
|
def: ast::def_id)
|
|
|
|
-> ~[ty::field_ty] {
|
2012-03-06 10:02:13 -06:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-12-10 15:47:54 -06:00
|
|
|
decoder::get_struct_fields(cstore.intr, cdata, def.node)
|
2012-03-06 10:02:13 -06:00
|
|
|
}
|
|
|
|
|
2013-01-29 18:51:16 -06:00
|
|
|
pub fn get_type(tcx: ty::ctxt,
|
|
|
|
def: ast::def_id)
|
|
|
|
-> ty::ty_param_bounds_and_ty {
|
2012-05-22 19:48:04 -05:00
|
|
|
let cstore = tcx.cstore;
|
2012-01-05 09:04:59 -06:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
|
|
|
decoder::get_type(cdata, def.node, tcx)
|
2011-07-08 00:18:38 -05:00
|
|
|
}
|
|
|
|
|
2013-03-27 05:16:28 -05:00
|
|
|
pub fn get_trait_def(tcx: ty::ctxt, def: ast::def_id) -> ty::TraitDef {
|
|
|
|
let cstore = tcx.cstore;
|
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
|
|
|
decoder::get_trait_def(cdata, def.node, tcx)
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn get_region_param(cstore: @mut metadata::cstore::CStore,
|
2013-01-29 18:51:16 -06:00
|
|
|
def: ast::def_id) -> Option<ty::region_variance> {
|
2012-07-11 12:28:30 -05:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-08-01 19:30:05 -05:00
|
|
|
return decoder::get_region_param(cdata, def.node);
|
2012-07-11 12:28:30 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 18:51:16 -06:00
|
|
|
pub fn get_field_type(tcx: ty::ctxt, class_id: ast::def_id,
|
|
|
|
def: ast::def_id) -> ty::ty_param_bounds_and_ty {
|
2012-05-22 19:48:04 -05:00
|
|
|
let cstore = tcx.cstore;
|
2012-03-19 12:19:00 -05:00
|
|
|
let cdata = cstore::get_crate_data(cstore, class_id.crate);
|
2012-12-06 18:13:54 -06:00
|
|
|
let all_items = reader::get_doc(reader::Doc(cdata.data), tag_items);
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("Looking up %?", class_id);
|
2012-05-22 19:48:04 -05:00
|
|
|
let class_doc = expect(tcx.diag,
|
2012-03-19 12:19:00 -05:00
|
|
|
decoder::maybe_find_item(class_id.node, all_items),
|
2012-08-22 19:24:52 -05:00
|
|
|
|| fmt!("get_field_type: class ID %? not found",
|
|
|
|
class_id) );
|
|
|
|
debug!("looking up %? : %?", def, class_doc);
|
2012-05-22 19:48:04 -05:00
|
|
|
let the_field = expect(tcx.diag,
|
2012-03-19 12:19:00 -05:00
|
|
|
decoder::maybe_find_item(def.node, class_doc),
|
2012-08-22 19:24:52 -05:00
|
|
|
|| fmt!("get_field_type: in class %?, field ID %? not found",
|
|
|
|
class_id, def) );
|
|
|
|
debug!("got field data %?", the_field);
|
2012-03-19 12:19:00 -05:00
|
|
|
let ty = decoder::item_type(def, the_field, tcx, cdata);
|
2013-02-19 01:40:42 -06:00
|
|
|
ty::ty_param_bounds_and_ty {
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
generics: ty::Generics {type_param_defs: @~[],
|
2013-03-27 05:16:28 -05:00
|
|
|
region_param: None},
|
2013-02-19 01:40:42 -06:00
|
|
|
ty: ty
|
|
|
|
}
|
2012-03-19 12:19:00 -05:00
|
|
|
}
|
|
|
|
|
2012-07-18 19:34:59 -05:00
|
|
|
// Given a def_id for an impl or class, return the traits it implements,
|
|
|
|
// or the empty vector if it's not for an impl or for a class that implements
|
|
|
|
// traits
|
2013-03-27 05:16:28 -05:00
|
|
|
pub fn get_impl_traits(tcx: ty::ctxt,
|
|
|
|
def: ast::def_id) -> ~[@ty::TraitRef] {
|
2012-05-22 19:48:04 -05:00
|
|
|
let cstore = tcx.cstore;
|
2012-01-05 09:04:59 -06:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-07-18 19:34:59 -05:00
|
|
|
decoder::get_impl_traits(cdata, def.node, tcx)
|
2011-07-08 00:18:38 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn get_impl_method(cstore: @mut cstore::CStore,
|
2013-01-29 18:51:16 -06:00
|
|
|
def: ast::def_id,
|
|
|
|
mname: ast::ident)
|
|
|
|
-> ast::def_id {
|
2012-03-08 05:15:02 -06:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-07-18 18:18:02 -05:00
|
|
|
decoder::get_impl_method(cstore.intr, cdata, def.node, mname)
|
2012-03-08 05:15:02 -06:00
|
|
|
}
|
|
|
|
|
2013-03-20 16:38:57 -05:00
|
|
|
pub fn get_item_visibility(cstore: @mut cstore::CStore,
|
|
|
|
def_id: ast::def_id)
|
|
|
|
-> ast::visibility {
|
2013-02-27 15:45:37 -06:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def_id.crate);
|
2013-03-20 16:38:57 -05:00
|
|
|
decoder::get_item_visibility(cdata, def_id.node)
|
2013-02-27 15:45:37 -06:00
|
|
|
}
|
|
|
|
|
2013-03-07 00:06:53 -06:00
|
|
|
pub fn get_link_args_for_crate(cstore: @mut cstore::CStore,
|
|
|
|
crate_num: ast::crate_num)
|
|
|
|
-> ~[~str] {
|
|
|
|
let cdata = cstore::get_crate_data(cstore, crate_num);
|
|
|
|
decoder::get_link_args_for_crate(cdata)
|
|
|
|
}
|