2011-07-08 01:29:09 -05:00
|
|
|
// Searching for information from the cstore
|
|
|
|
|
2012-03-19 12:19:00 -05:00
|
|
|
import std::{ebml};
|
2011-07-08 00:18:38 -05:00
|
|
|
import syntax::ast;
|
2012-01-20 12:45:25 -06:00
|
|
|
import syntax::ast_util;
|
2012-05-22 01:34:34 -05:00
|
|
|
import syntax::ast_map;
|
|
|
|
import middle::ty;
|
2012-02-10 08:01:32 -06:00
|
|
|
import option::{some, none};
|
2012-05-22 19:48:04 -05:00
|
|
|
import syntax::diagnostic::span_handler;
|
|
|
|
import syntax::diagnostic::expect;
|
2012-03-19 12:19:00 -05:00
|
|
|
import common::*;
|
2012-03-07 18:48:57 -06:00
|
|
|
import std::map::hashmap;
|
2011-07-08 16:53:25 -05:00
|
|
|
|
2012-05-15 19:59:55 -05:00
|
|
|
export class_dtor;
|
2011-07-08 16:53:25 -05:00
|
|
|
export get_symbol;
|
2012-03-19 12:19:00 -05:00
|
|
|
export get_class_fields;
|
2012-03-22 20:03:12 -05:00
|
|
|
export get_class_method;
|
2012-03-19 12:19:00 -05:00
|
|
|
export get_field_type;
|
2011-07-08 16:53:25 -05:00
|
|
|
export get_type_param_count;
|
|
|
|
export lookup_defs;
|
2012-02-13 11:56:09 -06:00
|
|
|
export lookup_method_purity;
|
2012-01-25 07:34:31 -06:00
|
|
|
export get_enum_variants;
|
2011-12-16 07:41:12 -06:00
|
|
|
export get_impls_for_mod;
|
2012-01-05 06:57:27 -06:00
|
|
|
export get_iface_methods;
|
2012-05-22 12:54:12 -05:00
|
|
|
export each_path;
|
2011-07-08 16:53:25 -05:00
|
|
|
export get_type;
|
2012-01-05 03:57:19 -06:00
|
|
|
export get_impl_iface;
|
2012-03-08 05:15:02 -06:00
|
|
|
export get_impl_method;
|
2012-02-10 08:01:32 -06:00
|
|
|
export get_item_path;
|
2012-03-08 16:13:57 -06:00
|
|
|
export maybe_get_item_ast, found_ast, found, found_parent, not_found;
|
2011-07-08 00:18:38 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn get_symbol(cstore: 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;
|
2011-07-26 07:06:02 -05:00
|
|
|
ret decoder::get_symbol(cdata, def.node);
|
2011-07-08 00:18:38 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn get_type_param_count(cstore: cstore::cstore, def: ast::def_id) -> uint {
|
2011-07-27 07:19:39 -05:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate).data;
|
2011-07-26 07:06:02 -05:00
|
|
|
ret decoder::get_type_param_count(cdata, def.node);
|
2011-07-08 00:18:38 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn lookup_defs(cstore: cstore::cstore, cnum: ast::crate_num,
|
2012-06-29 18:26:56 -05:00
|
|
|
path: ~[ast::ident]) -> ~[ast::def] {
|
|
|
|
let mut result = ~[];
|
2012-03-06 10:02:13 -06:00
|
|
|
#debug("lookup_defs: path = %? cnum = %?", path, cnum);
|
2012-06-30 18:19:07 -05:00
|
|
|
for resolve_path(cstore, cnum, path).each |elt| {
|
2012-04-06 13:01:43 -05:00
|
|
|
let (c, data, def) = elt;
|
2012-06-26 02:39:18 -05:00
|
|
|
vec::push(result, decoder::lookup_def(c, data, def));
|
2011-12-15 04:42:27 -06:00
|
|
|
}
|
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2012-02-13 11:56:09 -06:00
|
|
|
fn lookup_method_purity(cstore: cstore::cstore, did: ast::def_id)
|
|
|
|
-> ast::purity {
|
|
|
|
let cdata = cstore::get_crate_data(cstore, did.crate).data;
|
2012-02-15 08:42:35 -06:00
|
|
|
alt check decoder::lookup_def(did.crate, cdata, did) {
|
2012-02-13 11:56:09 -06:00
|
|
|
ast::def_fn(_, p) { p }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-16 21:19:37 -05:00
|
|
|
/* Returns a vector of possible def IDs for a given path,
|
|
|
|
in a given crate */
|
2011-12-15 04:42:27 -06:00
|
|
|
fn resolve_path(cstore: cstore::cstore, cnum: ast::crate_num,
|
2012-06-29 18:26:56 -05:00
|
|
|
path: ~[ast::ident]) ->
|
|
|
|
~[(ast::crate_num, @~[u8], ast::def_id)] {
|
2011-12-15 04:42:27 -06:00
|
|
|
let cm = cstore::get_crate_data(cstore, cnum);
|
2011-12-22 18:13:40 -06:00
|
|
|
#debug("resolve_path %s in crates[%d]:%s",
|
2012-06-10 02:49:59 -05:00
|
|
|
ast_util::path_name_i(path), cnum, cm.name);
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut result = ~[];
|
2012-06-30 18:19:07 -05:00
|
|
|
for decoder::resolve_path(path, cm.data).each |def| {
|
2011-12-15 04:42:27 -06:00
|
|
|
if def.crate == ast::local_crate {
|
2012-06-26 02:39:18 -05:00
|
|
|
vec::push(result, (cnum, cm.data, def));
|
2011-12-15 04:42:27 -06:00
|
|
|
} else {
|
|
|
|
if cm.cnum_map.contains_key(def.crate) {
|
2012-05-01 01:26:21 -05:00
|
|
|
// This reexport is itself a reexport from another crate
|
2011-12-15 04:42:27 -06:00
|
|
|
let next_cnum = cm.cnum_map.get(def.crate);
|
|
|
|
let next_cm_data = cstore::get_crate_data(cstore, next_cnum);
|
2012-06-26 02:39:18 -05:00
|
|
|
vec::push(result, (next_cnum, next_cm_data.data, def));
|
2011-12-15 04:42:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret result;
|
2011-07-08 16:53:25 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Iterates over all the paths in the given crate.
|
2012-05-22 12:54:12 -05:00
|
|
|
fn each_path(cstore: cstore::cstore, cnum: ast::crate_num,
|
|
|
|
f: fn(decoder::path_entry) -> bool) {
|
|
|
|
let crate_data = cstore::get_crate_data(cstore, cnum);
|
|
|
|
decoder::each_path(crate_data, f);
|
|
|
|
}
|
|
|
|
|
2012-02-10 08:01:32 -06:00
|
|
|
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-02-11 11:49:38 -06:00
|
|
|
let path = decoder::get_item_path(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.
|
2012-06-29 18:26:56 -05:00
|
|
|
vec::append(~[ast_map::path_mod(@cdata.name)], path)
|
2012-02-10 08:01:32 -06:00
|
|
|
}
|
|
|
|
|
2012-03-08 16:13:57 -06:00
|
|
|
enum found_ast {
|
|
|
|
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.
|
2012-05-14 19:46:45 -05:00
|
|
|
fn maybe_get_item_ast(tcx: ty::ctxt, def: ast::def_id,
|
|
|
|
decode_inlined_item: decoder::decode_inlined_item)
|
2012-03-08 16:13:57 -06:00
|
|
|
-> 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-05-14 19:46:45 -05:00
|
|
|
decoder::maybe_get_item_ast(cdata, tcx, def.node,
|
|
|
|
decode_inlined_item)
|
2012-02-14 17:21:53 -06:00
|
|
|
}
|
|
|
|
|
2012-06-25 22:00:46 -05:00
|
|
|
fn get_enum_variants(tcx: ty::ctxt, def: ast::def_id)
|
2012-06-29 18:26:56 -05:00
|
|
|
-> ~[ty::variant_info] {
|
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-01-25 07:34:31 -06:00
|
|
|
ret decoder::get_enum_variants(cdata, def.node, tcx)
|
2011-07-08 00:18:38 -05:00
|
|
|
}
|
|
|
|
|
2011-12-16 07:41:12 -06:00
|
|
|
fn get_impls_for_mod(cstore: cstore::cstore, def: ast::def_id,
|
2012-01-31 19:05:20 -06:00
|
|
|
name: option<ast::ident>)
|
2012-06-29 18:26:56 -05:00
|
|
|
-> @~[@decoder::_impl] {
|
2012-01-05 09:04:59 -06:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-06-30 18:19:07 -05:00
|
|
|
do decoder::get_impls_for_mod(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
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn get_iface_methods(tcx: ty::ctxt, def: ast::def_id) -> @~[ty::method] {
|
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_iface_methods(cdata, def.node, tcx)
|
2011-12-16 07:41:12 -06:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn get_class_fields(tcx: ty::ctxt, def: ast::def_id) -> ~[ty::field_ty] {
|
2012-05-22 19:48:04 -05:00
|
|
|
let cstore = tcx.cstore;
|
2012-03-06 10:02:13 -06:00
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
2012-03-21 14:42:34 -05:00
|
|
|
decoder::get_class_fields(cdata, def.node)
|
2012-03-06 10:02:13 -06:00
|
|
|
}
|
|
|
|
|
2011-12-28 10:50:12 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-03-19 12:19:00 -05: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 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);
|
|
|
|
let all_items = ebml::get_doc(ebml::doc(cdata.data), tag_items);
|
|
|
|
#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-06-30 18:19:07 -05:00
|
|
|
|| #fmt("get_field_type: class ID %? not found",
|
|
|
|
class_id) );
|
2012-03-19 12:19:00 -05:00
|
|
|
#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-06-30 18:19:07 -05:00
|
|
|
|| #fmt("get_field_type: in class %?, field ID %? not found",
|
|
|
|
class_id, def) );
|
2012-03-19 12:19:00 -05:00
|
|
|
#debug("got field data %?", the_field);
|
|
|
|
let ty = decoder::item_type(def, the_field, tcx, cdata);
|
2012-06-29 18:26:56 -05:00
|
|
|
ret {bounds: @~[], rp: ast::rp_none, ty: ty};
|
2012-03-19 12:19:00 -05:00
|
|
|
}
|
|
|
|
|
2012-04-13 14:22:35 -05:00
|
|
|
// Given a def_id for an impl or class, return the iface it implements,
|
|
|
|
// or none if it's not for an impl or for a class that implements ifaces
|
|
|
|
fn get_impl_iface(tcx: ty::ctxt, def: ast::def_id) -> option<ty::t> {
|
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_impl_iface(cdata, def.node, tcx)
|
2011-07-08 00:18:38 -05:00
|
|
|
}
|
|
|
|
|
2012-06-10 02:49:59 -05:00
|
|
|
fn get_impl_method(cstore: cstore::cstore,
|
|
|
|
def: ast::def_id, mname: ast::ident)
|
2012-03-08 05:15:02 -06:00
|
|
|
-> ast::def_id {
|
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
|
|
|
decoder::get_impl_method(cdata, def.node, mname)
|
|
|
|
}
|
|
|
|
|
2012-03-22 20:03:12 -05:00
|
|
|
/* Because classes use the iface format rather than the impl format
|
|
|
|
for their methods (so that get_iface_methods can be reused to get
|
|
|
|
class methods), classes require a slightly different version of
|
|
|
|
get_impl_method. Sigh. */
|
2012-06-10 02:49:59 -05:00
|
|
|
fn get_class_method(cstore: cstore::cstore,
|
|
|
|
def: ast::def_id, mname: ast::ident)
|
2012-03-22 20:03:12 -05:00
|
|
|
-> ast::def_id {
|
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
|
|
|
decoder::get_class_method(cdata, def.node, mname)
|
|
|
|
}
|
|
|
|
|
2012-05-15 19:59:55 -05:00
|
|
|
/* If def names a class with a dtor, return it. Otherwise, return none. */
|
|
|
|
fn class_dtor(cstore: cstore::cstore, def: ast::def_id)
|
|
|
|
-> option<ast::def_id> {
|
|
|
|
let cdata = cstore::get_crate_data(cstore, def.crate);
|
|
|
|
decoder::class_dtor(cdata, def.node)
|
|
|
|
}
|
2011-07-08 00:18:38 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|