2012-12-03 16:48:01 -08: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.
|
|
|
|
|
2013-01-07 14:16:52 -08:00
|
|
|
|
2011-07-07 23:29:09 -07:00
|
|
|
// Searching for information from the cstore
|
|
|
|
|
2013-01-08 19:37:25 -08:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-12-13 13:05:22 -08:00
|
|
|
use metadata::common::*;
|
2012-12-23 17:41:37 -05:00
|
|
|
use metadata::cstore;
|
|
|
|
use metadata::decoder;
|
|
|
|
use metadata;
|
2012-12-13 13:05:22 -08:00
|
|
|
use middle::ty;
|
|
|
|
|
|
|
|
use core::dvec::DVec;
|
2012-12-23 17:41:37 -05:00
|
|
|
use core::vec;
|
2012-12-06 16:13:54 -08:00
|
|
|
use reader = std::ebml::reader;
|
2012-12-13 13:05:22 -08:00
|
|
|
use std::ebml;
|
|
|
|
use std::map::HashMap;
|
2012-09-04 11:54:36 -07:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::ast_map;
|
2012-12-13 13:05:22 -08:00
|
|
|
use syntax::ast_util::dummy_sp;
|
|
|
|
use syntax::ast_util;
|
2012-09-04 11:54:36 -07:00
|
|
|
use syntax::diagnostic::expect;
|
2012-12-13 13:05:22 -08:00
|
|
|
use syntax::diagnostic::span_handler;
|
2011-07-08 14:53:25 -07:00
|
|
|
|
2012-12-10 13:47:54 -08:00
|
|
|
export struct_dtor;
|
2011-07-08 14:53:25 -07:00
|
|
|
export get_symbol;
|
2012-12-10 13:47:54 -08:00
|
|
|
export get_struct_fields;
|
2012-03-19 10:19:00 -07:00
|
|
|
export get_field_type;
|
2011-07-08 14:53:25 -07:00
|
|
|
export get_type_param_count;
|
2012-07-11 10:28:30 -07:00
|
|
|
export get_region_param;
|
2012-01-25 14:34:31 +01:00
|
|
|
export get_enum_variants;
|
2011-12-16 14:41:12 +01:00
|
|
|
export get_impls_for_mod;
|
2012-07-03 16:30:42 -07:00
|
|
|
export get_trait_methods;
|
2012-10-08 12:39:30 -07:00
|
|
|
export get_provided_trait_methods;
|
2012-10-19 21:27:01 -07:00
|
|
|
export get_supertraits;
|
2012-07-11 15:00:40 -07:00
|
|
|
export get_method_names_if_trait;
|
2012-10-18 13:29:34 -07:00
|
|
|
export get_type_name_if_impl;
|
|
|
|
export get_static_methods_if_impl;
|
2012-07-25 18:36:18 -07:00
|
|
|
export get_item_attrs;
|
2013-01-07 10:51:53 -08:00
|
|
|
export each_lang_item;
|
2012-05-22 10:54:12 -07:00
|
|
|
export each_path;
|
2011-07-08 14:53:25 -07:00
|
|
|
export get_type;
|
2012-07-18 17:34:59 -07:00
|
|
|
export get_impl_traits;
|
2012-03-08 12:15:02 +01:00
|
|
|
export get_impl_method;
|
2012-02-10 06:01:32 -08:00
|
|
|
export get_item_path;
|
2013-01-06 12:05:34 -08:00
|
|
|
export get_lang_items;
|
2012-03-08 23:13:57 +01:00
|
|
|
export maybe_get_item_ast, found_ast, found, found_parent, not_found;
|
2012-10-08 12:39:30 -07:00
|
|
|
export ProvidedTraitMethodInfo;
|
2012-10-18 13:29:34 -07:00
|
|
|
export StaticMethodInfo;
|
2012-10-08 12:39:30 -07:00
|
|
|
|
|
|
|
struct ProvidedTraitMethodInfo {
|
|
|
|
ty: ty::method,
|
|
|
|
def_id: ast::def_id
|
|
|
|
}
|
2011-07-07 22:18:38 -07:00
|
|
|
|
2012-10-18 13:29:34 -07:00
|
|
|
struct StaticMethodInfo {
|
|
|
|
ident: ast::ident,
|
|
|
|
def_id: ast::def_id,
|
|
|
|
purity: ast::purity
|
|
|
|
}
|
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn get_symbol(cstore: cstore::CStore, def: ast::def_id) -> ~str {
|
2011-07-27 14:19:39 +02:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate).data;
|
2012-08-01 17:30:05 -07:00
|
|
|
return decoder::get_symbol(cdata, def.node);
|
2011-07-07 22:18:38 -07:00
|
|
|
}
|
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn get_type_param_count(cstore: cstore::CStore, def: ast::def_id) -> uint {
|
2011-07-27 14:19:39 +02:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate).data;
|
2012-08-01 17:30:05 -07:00
|
|
|
return decoder::get_type_param_count(cdata, def.node);
|
2011-07-07 22:18:38 -07:00
|
|
|
}
|
|
|
|
|
2013-01-07 10:51:53 -08:00
|
|
|
/// Iterates over all the language items in the given crate.
|
|
|
|
fn each_lang_item(cstore: cstore::CStore,
|
|
|
|
cnum: ast::crate_num,
|
|
|
|
f: &fn(ast::node_id, uint) -> bool) {
|
|
|
|
let crate_data = cstore::get_crate_data(cstore, cnum);
|
|
|
|
decoder::each_lang_item(crate_data, f)
|
|
|
|
}
|
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
/// Iterates over all the paths in the given crate.
|
2012-10-15 14:56:42 -07:00
|
|
|
fn each_path(cstore: cstore::CStore, cnum: ast::crate_num,
|
2013-01-21 20:32:13 -08:00
|
|
|
f: fn(&str, decoder::def_like) -> bool) {
|
2012-05-22 10:54:12 -07:00
|
|
|
let crate_data = cstore::get_crate_data(cstore, cnum);
|
2012-11-08 16:52:21 -08: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 10:54:12 -07:00
|
|
|
}
|
|
|
|
|
2012-02-10 06:01:32 -08:00
|
|
|
fn get_item_path(tcx: ty::ctxt, def: ast::def_id) -> ast_map::path {
|
2012-05-22 17:48:04 -07:00
|
|
|
let cstore = tcx.cstore;
|
2012-02-10 06:01:32 -08:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-07-18 16:18:02 -07:00
|
|
|
let path = decoder::get_item_path(cstore.intr, cdata, def.node);
|
2012-03-02 20:06:08 -08:00
|
|
|
|
|
|
|
// FIXME #1920: This path is not always correct if the crate is not linked
|
|
|
|
// into the root namespace.
|
2013-01-07 14:16:52 -08:00
|
|
|
vec::append(~[ast_map::path_mod(tcx.sess.ident_of(
|
|
|
|
/*bad*/copy cdata.name))], path)
|
2012-02-10 06:01:32 -08:00
|
|
|
}
|
|
|
|
|
2012-03-08 23:13:57 +01:00
|
|
|
enum found_ast {
|
|
|
|
found(ast::inlined_item),
|
|
|
|
found_parent(ast::def_id, ast::inlined_item),
|
|
|
|
not_found,
|
|
|
|
}
|
|
|
|
|
2012-02-14 15:21:53 -08: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.
|
2012-05-14 17:46:45 -07:00
|
|
|
fn maybe_get_item_ast(tcx: ty::ctxt, def: ast::def_id,
|
|
|
|
decode_inlined_item: decoder::decode_inlined_item)
|
2012-03-08 23:13:57 +01:00
|
|
|
-> found_ast {
|
2012-05-22 17:48:04 -07:00
|
|
|
let cstore = tcx.cstore;
|
2012-02-14 15:21:53 -08:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-07-18 16:18:02 -07:00
|
|
|
decoder::maybe_get_item_ast(cstore.intr, cdata, tcx, def.node,
|
2012-05-14 17:46:45 -07:00
|
|
|
decode_inlined_item)
|
2012-02-14 15:21:53 -08:00
|
|
|
}
|
|
|
|
|
2012-06-25 20:00:46 -07:00
|
|
|
fn get_enum_variants(tcx: ty::ctxt, def: ast::def_id)
|
2012-11-30 11:24:16 -08:00
|
|
|
-> ~[ty::VariantInfo] {
|
2012-05-22 17:48:04 -07:00
|
|
|
let cstore = tcx.cstore;
|
2012-01-05 16:04:59 +01:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-07-18 16:18:02 -07:00
|
|
|
return decoder::get_enum_variants(cstore.intr, cdata, def.node, tcx)
|
2011-07-07 22:18:38 -07:00
|
|
|
}
|
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn get_impls_for_mod(cstore: cstore::CStore, def: ast::def_id,
|
2012-08-20 12:23:37 -07:00
|
|
|
name: Option<ast::ident>)
|
2012-06-29 16:26:56 -07:00
|
|
|
-> @~[@decoder::_impl] {
|
2012-01-05 16:04:59 +01:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-07-18 16:18:02 -07:00
|
|
|
do decoder::get_impls_for_mod(cstore.intr, cdata, def.node, name) |cnum| {
|
2012-04-16 14:58:58 -07:00
|
|
|
cstore::get_crate_data(cstore, cnum)
|
|
|
|
}
|
2011-12-16 14:41:12 +01:00
|
|
|
}
|
|
|
|
|
2012-07-03 16:30:42 -07:00
|
|
|
fn get_trait_methods(tcx: ty::ctxt, def: ast::def_id) -> @~[ty::method] {
|
2012-05-22 17:48:04 -07:00
|
|
|
let cstore = tcx.cstore;
|
2012-01-05 16:04:59 +01:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-07-18 16:18:02 -07:00
|
|
|
decoder::get_trait_methods(cstore.intr, cdata, def.node, tcx)
|
2011-12-16 14:41:12 +01:00
|
|
|
}
|
|
|
|
|
2012-10-08 12:39:30 -07:00
|
|
|
fn get_provided_trait_methods(tcx: ty::ctxt, def: ast::def_id) ->
|
|
|
|
~[ProvidedTraitMethodInfo] {
|
|
|
|
let cstore = tcx.cstore;
|
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
|
|
|
decoder::get_provided_trait_methods(cstore.intr, cdata, def.node, tcx)
|
|
|
|
}
|
|
|
|
|
2012-10-19 21:27:01 -07:00
|
|
|
fn get_supertraits(tcx: ty::ctxt, def: ast::def_id) -> ~[ty::t] {
|
|
|
|
let cstore = tcx.cstore;
|
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
|
|
|
decoder::get_supertraits(cdata, def.node, tcx)
|
|
|
|
}
|
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn get_method_names_if_trait(cstore: cstore::CStore, def: ast::def_id)
|
2012-08-20 12:23:37 -07:00
|
|
|
-> Option<@DVec<(ast::ident, ast::self_ty_)>> {
|
2012-07-11 15:00:40 -07:00
|
|
|
|
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-07-18 16:18:02 -07:00
|
|
|
return decoder::get_method_names_if_trait(cstore.intr, cdata, def.node);
|
2012-07-11 15:00:40 -07:00
|
|
|
}
|
|
|
|
|
2012-10-18 13:29:34 -07:00
|
|
|
fn get_type_name_if_impl(cstore: cstore::CStore, def: ast::def_id) ->
|
|
|
|
Option<ast::ident> {
|
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
|
|
|
decoder::get_type_name_if_impl(cstore.intr, cdata, def.node)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_static_methods_if_impl(cstore: cstore::CStore, def: ast::def_id) ->
|
|
|
|
Option<~[StaticMethodInfo]> {
|
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
|
|
|
decoder::get_static_methods_if_impl(cstore.intr, cdata, def.node)
|
|
|
|
}
|
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn get_item_attrs(cstore: cstore::CStore,
|
2012-07-25 18:36:18 -07:00
|
|
|
def_id: ast::def_id,
|
2012-09-20 18:15:39 -07:00
|
|
|
f: fn(~[@ast::meta_item])) {
|
2012-07-25 18:36:18 -07:00
|
|
|
|
|
|
|
let cdata = cstore::get_crate_data(cstore, def_id.crate);
|
|
|
|
decoder::get_item_attrs(cdata, def_id.node, f)
|
|
|
|
}
|
|
|
|
|
2012-12-10 13:47:54 -08:00
|
|
|
fn get_struct_fields(tcx: ty::ctxt, def: ast::def_id) -> ~[ty::field_ty] {
|
2012-05-22 17:48:04 -07:00
|
|
|
let cstore = tcx.cstore;
|
2012-03-06 08:02:13 -08:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-12-10 13:47:54 -08:00
|
|
|
decoder::get_struct_fields(cstore.intr, cdata, def.node)
|
2012-03-06 08:02:13 -08:00
|
|
|
}
|
|
|
|
|
2011-12-28 17:50:12 +01:00
|
|
|
fn get_type(tcx: ty::ctxt, def: ast::def_id) -> ty::ty_param_bounds_and_ty {
|
2012-05-22 17:48:04 -07:00
|
|
|
let cstore = tcx.cstore;
|
2012-01-05 16:04:59 +01:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
|
|
|
decoder::get_type(cdata, def.node, tcx)
|
2011-07-07 22:18:38 -07:00
|
|
|
}
|
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn get_region_param(cstore: metadata::cstore::CStore,
|
2012-08-20 12:23:37 -07:00
|
|
|
def: ast::def_id) -> Option<ty::region_variance> {
|
2012-07-11 10:28:30 -07:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-08-01 17:30:05 -07:00
|
|
|
return decoder::get_region_param(cdata, def.node);
|
2012-07-11 10:28:30 -07:00
|
|
|
}
|
|
|
|
|
2012-03-19 10:19:00 -07:00
|
|
|
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 17:48:04 -07:00
|
|
|
let cstore = tcx.cstore;
|
2012-03-19 10:19:00 -07:00
|
|
|
let cdata = cstore::get_crate_data(cstore, class_id.crate);
|
2012-12-06 16:13:54 -08:00
|
|
|
let all_items = reader::get_doc(reader::Doc(cdata.data), tag_items);
|
2012-08-22 17:24:52 -07:00
|
|
|
debug!("Looking up %?", class_id);
|
2012-05-22 17:48:04 -07:00
|
|
|
let class_doc = expect(tcx.diag,
|
2012-03-19 10:19:00 -07:00
|
|
|
decoder::maybe_find_item(class_id.node, all_items),
|
2012-08-22 17:24:52 -07:00
|
|
|
|| fmt!("get_field_type: class ID %? not found",
|
|
|
|
class_id) );
|
|
|
|
debug!("looking up %? : %?", def, class_doc);
|
2012-05-22 17:48:04 -07:00
|
|
|
let the_field = expect(tcx.diag,
|
2012-03-19 10:19:00 -07:00
|
|
|
decoder::maybe_find_item(def.node, class_doc),
|
2012-08-22 17:24:52 -07:00
|
|
|
|| fmt!("get_field_type: in class %?, field ID %? not found",
|
|
|
|
class_id, def) );
|
|
|
|
debug!("got field data %?", the_field);
|
2012-03-19 10:19:00 -07:00
|
|
|
let ty = decoder::item_type(def, the_field, tcx, cdata);
|
2012-08-09 09:59:50 -07:00
|
|
|
return {bounds: @~[],
|
2012-08-20 12:23:37 -07:00
|
|
|
region_param: None,
|
2012-08-09 09:59:50 -07:00
|
|
|
ty: ty};
|
2012-03-19 10:19:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-18 17:34:59 -07: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
|
|
|
|
fn get_impl_traits(tcx: ty::ctxt, def: ast::def_id) -> ~[ty::t] {
|
2012-05-22 17:48:04 -07:00
|
|
|
let cstore = tcx.cstore;
|
2012-01-05 16:04:59 +01:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-07-18 17:34:59 -07:00
|
|
|
decoder::get_impl_traits(cdata, def.node, tcx)
|
2011-07-07 22:18:38 -07:00
|
|
|
}
|
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
fn get_impl_method(cstore: cstore::CStore,
|
2012-06-10 00:49:59 -07:00
|
|
|
def: ast::def_id, mname: ast::ident)
|
2012-03-08 12:15:02 +01:00
|
|
|
-> ast::def_id {
|
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-07-18 16:18:02 -07:00
|
|
|
decoder::get_impl_method(cstore.intr, cdata, def.node, mname)
|
2012-03-08 12:15:02 +01:00
|
|
|
}
|
|
|
|
|
2012-05-15 17:59:55 -07:00
|
|
|
/* If def names a class with a dtor, return it. Otherwise, return none. */
|
2012-12-10 13:47:54 -08:00
|
|
|
fn struct_dtor(cstore: cstore::CStore, def: ast::def_id)
|
2012-08-20 12:23:37 -07:00
|
|
|
-> Option<ast::def_id> {
|
2012-05-15 17:59:55 -07:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-12-10 13:47:54 -08:00
|
|
|
decoder::struct_dtor(cdata, def.node)
|
2012-05-15 17:59:55 -07:00
|
|
|
}
|
2011-07-07 22:18:38 -07:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|