2011-07-08 01:29:09 -05:00
|
|
|
// Decoding metadata from a single crate's metadata
|
2011-06-27 18:03:01 -05:00
|
|
|
|
2012-03-12 22:04:27 -05:00
|
|
|
import std::{ebml, map};
|
2012-03-07 18:48:57 -06:00
|
|
|
import std::map::hashmap;
|
2012-01-11 08:15:54 -06:00
|
|
|
import io::writer_util;
|
2011-09-12 18:13:28 -05:00
|
|
|
import syntax::{ast, ast_util};
|
2012-01-30 23:00:57 -06:00
|
|
|
import driver::session::session;
|
2011-06-30 19:03:08 -05:00
|
|
|
import front::attr;
|
2011-06-27 17:53:27 -05:00
|
|
|
import middle::ty;
|
2012-02-10 08:01:32 -06:00
|
|
|
import middle::ast_map;
|
2011-07-07 14:22:39 -05:00
|
|
|
import common::*;
|
2012-01-05 09:04:59 -06:00
|
|
|
import tydecode::{parse_ty_data, parse_def_id, parse_bounds_data};
|
2011-07-05 04:48:19 -05:00
|
|
|
import syntax::print::pprust;
|
2012-01-05 09:04:59 -06:00
|
|
|
import cmd=cstore::crate_metadata;
|
2012-02-14 17:21:53 -06:00
|
|
|
import middle::trans::common::maps;
|
2011-06-27 17:53:27 -05:00
|
|
|
|
|
|
|
export get_symbol;
|
2012-01-25 07:34:31 -06:00
|
|
|
export get_enum_variants;
|
2011-06-27 17:53:27 -05:00
|
|
|
export get_type;
|
2011-06-30 05:35:19 -05:00
|
|
|
export get_type_param_count;
|
2012-01-05 03:57:19 -06:00
|
|
|
export get_impl_iface;
|
2012-03-08 05:15:02 -06:00
|
|
|
export get_impl_method;
|
2011-12-15 04:42:27 -06:00
|
|
|
export lookup_def;
|
2011-12-16 07:41:12 -06:00
|
|
|
export lookup_item_name;
|
2012-01-05 03:57:19 -06:00
|
|
|
export get_impl_iface;
|
2011-12-15 04:42:27 -06:00
|
|
|
export resolve_path;
|
2011-06-28 14:53:59 -05:00
|
|
|
export get_crate_attributes;
|
2011-06-27 17:53:27 -05:00
|
|
|
export list_crate_metadata;
|
2011-07-08 14:55:38 -05:00
|
|
|
export crate_dep;
|
|
|
|
export get_crate_deps;
|
2011-12-11 09:23:38 -06:00
|
|
|
export get_crate_hash;
|
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-01-20 12:45:25 -06:00
|
|
|
export get_crate_module_paths;
|
2012-02-10 08:01:32 -06:00
|
|
|
export get_item_path;
|
2012-02-14 17:21:53 -06:00
|
|
|
export maybe_get_item_ast;
|
|
|
|
|
|
|
|
// Used internally by astencode:
|
|
|
|
export translate_def_id;
|
2012-01-20 12:45:25 -06:00
|
|
|
|
2011-07-08 16:53:25 -05:00
|
|
|
// A function that takes a def_id relative to the crate being searched and
|
|
|
|
// returns a def_id relative to the compilation environment, i.e. if we hit a
|
|
|
|
// def_id for an item defined in another crate, somebody needs to figure out
|
|
|
|
// what crate that's in and give us a def_id that makes sense for the current
|
|
|
|
// build.
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-10-18 17:07:40 -05:00
|
|
|
fn lookup_hash(d: ebml::doc, eq_fn: fn@([u8]) -> bool, hash: uint) ->
|
2011-08-12 00:58:14 -05:00
|
|
|
[ebml::doc] {
|
|
|
|
let index = ebml::get_doc(d, tag_index);
|
|
|
|
let table = ebml::get_doc(index, tag_index_table);
|
2011-07-27 07:19:39 -05:00
|
|
|
let hash_pos = table.start + hash % 256u * 4u;
|
2012-02-14 17:21:53 -06:00
|
|
|
let pos = io::u64_from_be_bytes(*d.data, hash_pos, 4u) as uint;
|
2012-02-10 18:38:33 -06:00
|
|
|
let {tag:_, doc:bucket} = ebml::doc_at(d.data, pos);
|
2011-06-27 17:53:27 -05:00
|
|
|
// Awkward logic because we can't ret from foreach yet
|
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
let result: [ebml::doc] = [];
|
2011-07-27 07:19:39 -05:00
|
|
|
let belt = tag_index_buckets_bucket_elt;
|
2011-10-21 05:31:48 -05:00
|
|
|
ebml::tagged_docs(bucket, belt) {|elt|
|
2012-02-14 17:21:53 -06:00
|
|
|
let pos = io::u64_from_be_bytes(*elt.data, elt.start, 4u) as uint;
|
2011-08-13 02:09:25 -05:00
|
|
|
if eq_fn(vec::slice::<u8>(*elt.data, elt.start + 4u, elt.end)) {
|
2012-02-10 18:38:33 -06:00
|
|
|
result += [ebml::doc_at(d.data, pos).doc];
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
2011-10-21 05:31:48 -05:00
|
|
|
};
|
2011-06-27 17:53:27 -05:00
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2012-01-31 19:05:20 -06:00
|
|
|
fn maybe_find_item(item_id: int, items: ebml::doc) -> option<ebml::doc> {
|
2011-09-12 04:27:30 -05:00
|
|
|
fn eq_item(bytes: [u8], item_id: int) -> bool {
|
2012-02-14 17:21:53 -06:00
|
|
|
ret io::u64_from_be_bytes(bytes, 0u, 4u) as int == item_id;
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
let eqer = bind eq_item(_, item_id);
|
|
|
|
let found = lookup_hash(items, eqer, hash_node_id(item_id));
|
2011-08-15 18:38:23 -05:00
|
|
|
if vec::len(found) == 0u {
|
2011-08-13 02:09:25 -05:00
|
|
|
ret option::none::<ebml::doc>;
|
2011-08-19 17:16:48 -05:00
|
|
|
} else { ret option::some::<ebml::doc>(found[0]); }
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn find_item(item_id: int, items: ebml::doc) -> ebml::doc {
|
2011-06-27 17:53:27 -05:00
|
|
|
ret option::get(maybe_find_item(item_id, items));
|
|
|
|
}
|
|
|
|
|
2011-08-12 00:58:14 -05:00
|
|
|
// Looks up an item in the given metadata and returns an ebml doc pointing
|
2011-06-27 17:53:27 -05:00
|
|
|
// to the item data.
|
2011-09-12 04:27:30 -05:00
|
|
|
fn lookup_item(item_id: int, data: @[u8]) -> ebml::doc {
|
2012-03-14 14:07:23 -05:00
|
|
|
let items = ebml::get_doc(ebml::doc(data), tag_items);
|
2011-06-27 17:53:27 -05:00
|
|
|
ret find_item(item_id, items);
|
|
|
|
}
|
|
|
|
|
2012-02-13 13:55:23 -06:00
|
|
|
fn item_family(item: ebml::doc) -> char {
|
2011-08-12 00:58:14 -05:00
|
|
|
let fam = ebml::get_doc(item, tag_items_data_item_family);
|
2012-02-13 13:55:23 -06:00
|
|
|
ebml::doc_as_u8(fam) as char
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn item_symbol(item: ebml::doc) -> str {
|
2011-08-12 00:58:14 -05:00
|
|
|
let sym = ebml::get_doc(item, tag_items_data_item_symbol);
|
2012-01-25 03:20:55 -06:00
|
|
|
ret str::from_bytes(ebml::doc_data(sym));
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
|
|
|
|
2012-03-08 16:13:57 -06:00
|
|
|
fn item_parent_item(d: ebml::doc) -> option<ast::def_id> {
|
|
|
|
let found = none;
|
|
|
|
ebml::tagged_docs(d, tag_items_data_parent_item) {|did|
|
|
|
|
found = some(parse_def_id(ebml::doc_data(did)));
|
|
|
|
}
|
|
|
|
found
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
|
|
|
|
2012-01-31 19:05:20 -06:00
|
|
|
fn variant_disr_val(d: ebml::doc) -> option<int> {
|
2012-02-22 06:18:15 -06:00
|
|
|
option::chain(ebml::maybe_get_doc(d, tag_disr_val)) {|val_doc|
|
|
|
|
int::parse_buf(ebml::doc_data(val_doc), 10u)
|
2012-01-10 15:50:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn doc_type(doc: ebml::doc, tcx: ty::ctxt, cdata: cmd) -> ty::t {
|
2012-01-05 06:57:27 -06:00
|
|
|
let tp = ebml::get_doc(doc, tag_items_data_item_type);
|
2012-01-05 09:04:59 -06:00
|
|
|
parse_ty_data(tp.data, cdata.cnum, tp.start, tcx, {|did|
|
|
|
|
translate_def_id(cdata, did)
|
|
|
|
})
|
2012-01-05 06:57:27 -06:00
|
|
|
}
|
|
|
|
|
2012-02-10 12:28:35 -06:00
|
|
|
fn item_type(item_id: ast::def_id, item: ebml::doc,
|
|
|
|
tcx: ty::ctxt, cdata: cmd) -> ty::t {
|
2012-01-05 09:04:59 -06:00
|
|
|
let t = doc_type(item, tcx, cdata);
|
2011-12-19 06:52:58 -06:00
|
|
|
if family_names_type(item_family(item)) {
|
2012-02-10 12:28:35 -06:00
|
|
|
ty::mk_with_id(tcx, t, item_id)
|
2012-01-05 06:57:27 -06:00
|
|
|
} else { t }
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn item_impl_iface(item: ebml::doc, tcx: ty::ctxt, cdata: cmd)
|
2012-01-31 19:05:20 -06:00
|
|
|
-> option<ty::t> {
|
2012-01-05 03:57:19 -06:00
|
|
|
let result = none;
|
|
|
|
ebml::tagged_docs(item, tag_impl_iface) {|ity|
|
2012-01-05 09:04:59 -06:00
|
|
|
let t = parse_ty_data(ity.data, cdata.cnum, ity.start, tcx, {|did|
|
|
|
|
translate_def_id(cdata, did)
|
|
|
|
});
|
2012-01-05 03:57:19 -06:00
|
|
|
result = some(t);
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2012-01-30 04:36:18 -06:00
|
|
|
fn item_ty_param_bounds(item: ebml::doc, tcx: ty::ctxt, cdata: cmd)
|
|
|
|
-> @[ty::param_bounds] {
|
|
|
|
let bounds = [];
|
2011-12-28 10:50:12 -06:00
|
|
|
ebml::tagged_docs(item, tag_items_data_item_ty_param_bounds) {|p|
|
2012-01-05 09:04:59 -06:00
|
|
|
let bd = parse_bounds_data(p.data, p.start, cdata.cnum, tcx, {|did|
|
|
|
|
translate_def_id(cdata, did)
|
|
|
|
});
|
2012-01-30 04:36:18 -06:00
|
|
|
bounds += [bd];
|
2011-12-28 10:50:12 -06:00
|
|
|
}
|
2012-01-02 05:09:26 -06:00
|
|
|
@bounds
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
|
|
|
|
2011-12-16 07:41:12 -06:00
|
|
|
fn item_ty_param_count(item: ebml::doc) -> uint {
|
|
|
|
let n = 0u;
|
2011-12-29 04:23:35 -06:00
|
|
|
ebml::tagged_docs(item, tag_items_data_item_ty_param_bounds,
|
|
|
|
{|_p| n += 1u; });
|
2011-12-16 07:41:12 -06:00
|
|
|
n
|
|
|
|
}
|
|
|
|
|
2012-01-25 07:34:31 -06:00
|
|
|
fn enum_variant_ids(item: ebml::doc, cdata: cmd) -> [ast::def_id] {
|
2011-08-19 17:16:48 -05:00
|
|
|
let ids: [ast::def_id] = [];
|
2011-07-27 07:19:39 -05:00
|
|
|
let v = tag_items_data_item_variant;
|
2011-10-21 05:31:48 -05:00
|
|
|
ebml::tagged_docs(item, v) {|p|
|
2011-08-12 00:58:14 -05:00
|
|
|
let ext = parse_def_id(ebml::doc_data(p));
|
2012-01-05 09:04:59 -06:00
|
|
|
ids += [{crate: cdata.cnum, node: ext.node}];
|
2011-10-21 05:31:48 -05:00
|
|
|
};
|
2011-06-27 17:53:27 -05:00
|
|
|
ret ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a path and serialized crate metadata, returns the ID of the
|
|
|
|
// definition the path refers to.
|
2011-09-12 04:27:30 -05:00
|
|
|
fn resolve_path(path: [ast::ident], data: @[u8]) -> [ast::def_id] {
|
|
|
|
fn eq_item(data: [u8], s: str) -> bool {
|
2012-01-25 03:20:55 -06:00
|
|
|
ret str::eq(str::from_bytes(data), s);
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
2011-09-02 17:34:58 -05:00
|
|
|
let s = str::connect(path, "::");
|
2012-03-14 14:07:23 -05:00
|
|
|
let md = ebml::doc(data);
|
2011-08-12 00:58:14 -05:00
|
|
|
let paths = ebml::get_doc(md, tag_paths);
|
2011-07-27 07:19:39 -05:00
|
|
|
let eqer = bind eq_item(_, s);
|
2011-08-19 17:16:48 -05:00
|
|
|
let result: [ast::def_id] = [];
|
2011-09-02 17:34:58 -05:00
|
|
|
for doc: ebml::doc in lookup_hash(paths, eqer, hash_path(s)) {
|
2011-08-12 00:58:14 -05:00
|
|
|
let did_doc = ebml::get_doc(doc, tag_def_id);
|
2011-08-19 17:16:48 -05:00
|
|
|
result += [parse_def_id(ebml::doc_data(did_doc))];
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2012-02-10 08:01:32 -06:00
|
|
|
fn item_path(item_doc: ebml::doc) -> ast_map::path {
|
|
|
|
let path_doc = ebml::get_doc(item_doc, tag_path);
|
|
|
|
|
|
|
|
let len_doc = ebml::get_doc(path_doc, tag_path_len);
|
2012-02-14 17:21:53 -06:00
|
|
|
let len = ebml::doc_as_u32(len_doc) as uint;
|
2012-02-10 08:01:32 -06:00
|
|
|
|
|
|
|
let result = [];
|
|
|
|
vec::reserve(result, len);
|
|
|
|
|
|
|
|
ebml::docs(path_doc) {|tag, elt_doc|
|
|
|
|
if tag == tag_path_elt_mod {
|
2012-02-14 17:21:53 -06:00
|
|
|
let str = ebml::doc_as_str(elt_doc);
|
2012-02-10 08:01:32 -06:00
|
|
|
result += [ast_map::path_mod(str)];
|
|
|
|
} else if tag == tag_path_elt_name {
|
2012-02-14 17:21:53 -06:00
|
|
|
let str = ebml::doc_as_str(elt_doc);
|
2012-02-10 08:01:32 -06:00
|
|
|
result += [ast_map::path_name(str)];
|
|
|
|
} else {
|
|
|
|
// ignore tag_path_len element
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2011-12-16 07:41:12 -06:00
|
|
|
fn item_name(item: ebml::doc) -> ast::ident {
|
|
|
|
let name = ebml::get_doc(item, tag_paths_data_name);
|
2012-01-25 03:20:55 -06:00
|
|
|
str::from_bytes(ebml::doc_data(name))
|
2011-12-16 07:41:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lookup_item_name(data: @[u8], id: ast::node_id) -> ast::ident {
|
|
|
|
item_name(lookup_item(id, data))
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn lookup_def(cnum: ast::crate_num, data: @[u8], did_: ast::def_id) ->
|
2011-07-27 07:19:39 -05:00
|
|
|
ast::def {
|
|
|
|
let item = lookup_item(did_.node, data);
|
2011-07-29 18:40:23 -05:00
|
|
|
let fam_ch = item_family(item);
|
2011-07-27 07:19:39 -05:00
|
|
|
let did = {crate: cnum, node: did_.node};
|
2012-01-25 07:34:31 -06:00
|
|
|
// We treat references to enums as references to types.
|
2012-02-15 02:40:42 -06:00
|
|
|
alt check fam_ch {
|
2012-02-13 13:55:23 -06:00
|
|
|
'c' { ast::def_const(did) }
|
|
|
|
'u' { ast::def_fn(did, ast::unsafe_fn) }
|
|
|
|
'f' { ast::def_fn(did, ast::impure_fn) }
|
|
|
|
'p' { ast::def_fn(did, ast::pure_fn) }
|
|
|
|
'y' { ast::def_ty(did) }
|
|
|
|
't' { ast::def_ty(did) }
|
|
|
|
'm' { ast::def_mod(did) }
|
|
|
|
'n' { ast::def_native_mod(did) }
|
|
|
|
'v' {
|
2012-03-08 16:13:57 -06:00
|
|
|
let tid = option::get(item_parent_item(item));
|
2012-02-13 13:55:23 -06:00
|
|
|
tid = {crate: cnum, node: tid.node};
|
|
|
|
ast::def_variant(tid, did)
|
|
|
|
}
|
|
|
|
'I' { ast::def_ty(did) }
|
|
|
|
}
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn get_type(cdata: cmd, id: ast::node_id, tcx: ty::ctxt)
|
|
|
|
-> ty::ty_param_bounds_and_ty {
|
|
|
|
let item = lookup_item(id, cdata.data);
|
2012-02-10 12:28:35 -06:00
|
|
|
let t = item_type({crate: cdata.cnum, node: id}, item, tcx, cdata);
|
2012-01-30 04:36:18 -06:00
|
|
|
let tp_bounds = if family_has_type_params(item_family(item)) {
|
|
|
|
item_ty_param_bounds(item, tcx, cdata)
|
2012-01-02 05:09:26 -06:00
|
|
|
} else { @[] };
|
|
|
|
ret {bounds: tp_bounds, ty: t};
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
|
|
|
|
2011-08-04 18:20:09 -05:00
|
|
|
fn get_type_param_count(data: @[u8], id: ast::node_id) -> uint {
|
2011-12-16 07:41:12 -06:00
|
|
|
item_ty_param_count(lookup_item(id, data))
|
2011-07-29 18:40:23 -05:00
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn get_impl_iface(cdata: cmd, id: ast::node_id, tcx: ty::ctxt)
|
2012-01-31 19:05:20 -06:00
|
|
|
-> option<ty::t> {
|
2012-01-05 09:04:59 -06:00
|
|
|
item_impl_iface(lookup_item(id, cdata.data), tcx, cdata)
|
2012-01-05 03:57:19 -06:00
|
|
|
}
|
|
|
|
|
2012-03-08 05:15:02 -06:00
|
|
|
fn get_impl_method(cdata: cmd, id: ast::node_id, name: str) -> ast::def_id {
|
2012-03-08 09:10:25 -06:00
|
|
|
let items = ebml::get_doc(ebml::doc(cdata.data), tag_items);
|
2012-03-08 05:15:02 -06:00
|
|
|
let found = none;
|
|
|
|
ebml::tagged_docs(find_item(id, items), tag_item_method) {|mid|
|
|
|
|
let m_did = parse_def_id(ebml::doc_data(mid));
|
|
|
|
if item_name(find_item(m_did.node, items)) == name {
|
|
|
|
found = some(translate_def_id(cdata, m_did));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
option::get(found)
|
|
|
|
}
|
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
fn get_symbol(data: @[u8], id: ast::node_id) -> str {
|
2011-07-08 00:30:07 -05:00
|
|
|
ret item_symbol(lookup_item(id, data));
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
|
|
|
|
2012-02-10 08:01:32 -06:00
|
|
|
fn get_item_path(cdata: cmd, id: ast::node_id) -> ast_map::path {
|
|
|
|
item_path(lookup_item(id, cdata.data))
|
|
|
|
}
|
|
|
|
|
2012-02-14 17:21:53 -06:00
|
|
|
fn maybe_get_item_ast(cdata: cmd, tcx: ty::ctxt, maps: maps,
|
2012-03-08 16:13:57 -06:00
|
|
|
id: ast::node_id) -> csearch::found_ast {
|
2012-02-14 17:21:53 -06:00
|
|
|
let item_doc = lookup_item(id, cdata.data);
|
|
|
|
let path = vec::init(item_path(item_doc));
|
2012-03-08 16:13:57 -06:00
|
|
|
alt astencode::decode_inlined_item(cdata, tcx, maps, path, item_doc) {
|
|
|
|
some(ii) { csearch::found(ii) }
|
|
|
|
none {
|
|
|
|
alt item_parent_item(item_doc) {
|
|
|
|
some(did) {
|
|
|
|
let did = translate_def_id(cdata, did);
|
|
|
|
let parent_item = lookup_item(did.node, cdata.data);
|
|
|
|
alt astencode::decode_inlined_item(cdata, tcx, maps, path,
|
|
|
|
parent_item) {
|
|
|
|
some(ii) { csearch::found_parent(did, ii) }
|
|
|
|
none { csearch::not_found }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
none { csearch::not_found }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-14 17:21:53 -06:00
|
|
|
}
|
|
|
|
|
2012-01-25 07:34:31 -06:00
|
|
|
fn get_enum_variants(cdata: cmd, id: ast::node_id, tcx: ty::ctxt)
|
2012-01-05 09:04:59 -06:00
|
|
|
-> [ty::variant_info] {
|
|
|
|
let data = cdata.data;
|
2012-03-14 14:07:23 -05:00
|
|
|
let items = ebml::get_doc(ebml::doc(data), tag_items);
|
2012-01-05 09:04:59 -06:00
|
|
|
let item = find_item(id, items);
|
2011-08-19 17:16:48 -05:00
|
|
|
let infos: [ty::variant_info] = [];
|
2012-01-25 07:34:31 -06:00
|
|
|
let variant_ids = enum_variant_ids(item, cdata);
|
2012-01-10 15:50:40 -06:00
|
|
|
let disr_val = 0;
|
2011-08-15 23:54:52 -05:00
|
|
|
for did: ast::def_id in variant_ids {
|
2011-07-27 07:19:39 -05:00
|
|
|
let item = find_item(did.node, items);
|
2012-02-10 12:28:35 -06:00
|
|
|
let ctor_ty = item_type({crate: cdata.cnum, node: id}, item,
|
|
|
|
tcx, cdata);
|
2012-01-15 23:42:10 -06:00
|
|
|
let name = item_name(item);
|
2011-08-19 17:16:48 -05:00
|
|
|
let arg_tys: [ty::t] = [];
|
2012-02-03 08:15:28 -06:00
|
|
|
alt ty::get(ctor_ty).struct {
|
2011-12-23 09:09:52 -06:00
|
|
|
ty::ty_fn(f) {
|
|
|
|
for a: ty::arg in f.inputs { arg_tys += [a.ty]; }
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-01-19 16:24:03 -06:00
|
|
|
_ { /* Nullary enum variant. */ }
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
2012-01-10 15:50:40 -06:00
|
|
|
alt variant_disr_val(item) {
|
|
|
|
some(val) { disr_val = val; }
|
|
|
|
_ { /* empty */ }
|
|
|
|
}
|
2012-01-15 23:42:10 -06:00
|
|
|
infos += [@{args: arg_tys, ctor_ty: ctor_ty, name: name,
|
|
|
|
id: did, disr_val: disr_val}];
|
2012-01-10 15:50:40 -06:00
|
|
|
disr_val += 1;
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
|
|
|
ret infos;
|
|
|
|
}
|
|
|
|
|
2012-01-11 10:11:56 -06:00
|
|
|
fn item_impl_methods(cdata: cmd, item: ebml::doc, base_tps: uint)
|
2011-12-16 07:41:12 -06:00
|
|
|
-> [@middle::resolve::method_info] {
|
2012-01-05 06:57:27 -06:00
|
|
|
let rslt = [];
|
|
|
|
ebml::tagged_docs(item, tag_item_method) {|doc|
|
2011-12-16 07:41:12 -06:00
|
|
|
let m_did = parse_def_id(ebml::doc_data(doc));
|
2012-01-11 10:11:56 -06:00
|
|
|
let mth_item = lookup_item(m_did.node, cdata.data);
|
|
|
|
rslt += [@{did: translate_def_id(cdata, m_did),
|
2011-12-16 07:41:12 -06:00
|
|
|
n_tps: item_ty_param_count(mth_item) - base_tps,
|
|
|
|
ident: item_name(mth_item)}];
|
|
|
|
}
|
|
|
|
rslt
|
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn get_impls_for_mod(cdata: cmd, m_id: ast::node_id,
|
2012-01-31 19:05:20 -06:00
|
|
|
name: option<ast::ident>)
|
2012-01-05 06:57:27 -06:00
|
|
|
-> @[@middle::resolve::_impl] {
|
2012-01-05 09:04:59 -06:00
|
|
|
let data = cdata.data;
|
|
|
|
let mod_item = lookup_item(m_id, data), result = [];
|
2012-01-05 06:57:27 -06:00
|
|
|
ebml::tagged_docs(mod_item, tag_mod_impl) {|doc|
|
2012-01-05 09:04:59 -06:00
|
|
|
let did = translate_def_id(cdata, parse_def_id(ebml::doc_data(doc)));
|
2012-01-05 06:57:27 -06:00
|
|
|
let item = lookup_item(did.node, data), nm = item_name(item);
|
2012-01-19 00:37:22 -06:00
|
|
|
if alt name { some(n) { n == nm } none { true } } {
|
2012-02-09 10:08:54 -06:00
|
|
|
let base_tps = item_ty_param_count(item);
|
2012-01-11 14:40:13 -06:00
|
|
|
result += [@{did: did, ident: nm,
|
2012-01-11 10:11:56 -06:00
|
|
|
methods: item_impl_methods(cdata, item, base_tps)}];
|
2012-01-05 06:57:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
@result
|
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
fn get_iface_methods(cdata: cmd, id: ast::node_id, tcx: ty::ctxt)
|
|
|
|
-> @[ty::method] {
|
|
|
|
let data = cdata.data;
|
|
|
|
let item = lookup_item(id, data), result = [];
|
2012-01-05 06:57:27 -06:00
|
|
|
ebml::tagged_docs(item, tag_item_method) {|mth|
|
2012-01-30 04:36:18 -06:00
|
|
|
let bounds = item_ty_param_bounds(mth, tcx, cdata);
|
2012-01-05 06:57:27 -06:00
|
|
|
let name = item_name(mth);
|
2012-01-05 09:04:59 -06:00
|
|
|
let ty = doc_type(mth, tcx, cdata);
|
2012-02-03 08:15:28 -06:00
|
|
|
let fty = alt ty::get(ty).struct { ty::ty_fn(f) { f }
|
2012-01-30 23:00:57 -06:00
|
|
|
_ { tcx.sess.bug("get_iface_methods: id has non-function type");
|
|
|
|
} };
|
2012-02-13 11:56:09 -06:00
|
|
|
result += [{ident: name, tps: bounds, fty: fty,
|
2012-02-15 02:40:42 -06:00
|
|
|
purity: alt check item_family(mth) {
|
2012-02-13 11:56:09 -06:00
|
|
|
'u' { ast::unsafe_fn }
|
|
|
|
'f' { ast::impure_fn }
|
|
|
|
'p' { ast::pure_fn }
|
|
|
|
}}];
|
2012-01-05 06:57:27 -06:00
|
|
|
}
|
|
|
|
@result
|
|
|
|
}
|
|
|
|
|
2012-02-13 13:55:23 -06:00
|
|
|
fn family_has_type_params(fam_ch: char) -> bool {
|
2012-02-15 02:40:42 -06:00
|
|
|
alt check fam_ch {
|
2012-01-05 06:57:27 -06:00
|
|
|
'c' | 'T' | 'm' | 'n' { false }
|
|
|
|
'f' | 'u' | 'p' | 'F' | 'U' | 'P' | 'y' | 't' | 'v' | 'i' | 'I' { true }
|
|
|
|
}
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
|
|
|
|
2012-02-13 13:55:23 -06:00
|
|
|
fn family_names_type(fam_ch: char) -> bool {
|
|
|
|
alt fam_ch { 'y' | 't' | 'I' { true } _ { false } }
|
2011-12-19 06:52:58 -06:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn read_path(d: ebml::doc) -> {path: str, pos: uint} {
|
2011-08-12 00:58:14 -05:00
|
|
|
let desc = ebml::doc_data(d);
|
2012-02-14 17:21:53 -06:00
|
|
|
let pos = io::u64_from_be_bytes(desc, 0u, 4u) as uint;
|
2011-08-13 02:09:25 -05:00
|
|
|
let pathbytes = vec::slice::<u8>(desc, 4u, vec::len::<u8>(desc));
|
2012-01-25 03:20:55 -06:00
|
|
|
let path = str::from_bytes(pathbytes);
|
2011-07-27 07:19:39 -05:00
|
|
|
ret {path: path, pos: pos};
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn describe_def(items: ebml::doc, id: ast::def_id) -> str {
|
2011-09-02 17:34:58 -05:00
|
|
|
if id.crate != ast::local_crate { ret "external"; }
|
2011-07-29 18:40:23 -05:00
|
|
|
ret item_family_to_str(item_family(find_item(id.node, items)));
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
|
|
|
|
2012-02-13 13:55:23 -06:00
|
|
|
fn item_family_to_str(fam: char) -> str {
|
2012-02-15 02:40:42 -06:00
|
|
|
alt check fam {
|
2011-09-02 17:34:58 -05:00
|
|
|
'c' { ret "const"; }
|
|
|
|
'f' { ret "fn"; }
|
2011-10-07 13:04:54 -05:00
|
|
|
'u' { ret "unsafe fn"; }
|
2011-09-02 17:34:58 -05:00
|
|
|
'p' { ret "pure fn"; }
|
|
|
|
'F' { ret "native fn"; }
|
2011-10-12 15:31:41 -05:00
|
|
|
'U' { ret "unsafe native fn"; }
|
|
|
|
'P' { ret "pure native fn"; }
|
2011-09-02 17:34:58 -05:00
|
|
|
'y' { ret "type"; }
|
|
|
|
'T' { ret "native type"; }
|
|
|
|
't' { ret "type"; }
|
|
|
|
'm' { ret "mod"; }
|
|
|
|
'n' { ret "native mod"; }
|
2012-01-19 16:24:03 -06:00
|
|
|
'v' { ret "enum"; }
|
2012-01-05 06:57:27 -06:00
|
|
|
'i' { ret "impl"; }
|
|
|
|
'I' { ret "iface"; }
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn get_meta_items(md: ebml::doc) -> [@ast::meta_item] {
|
2011-08-19 17:16:48 -05:00
|
|
|
let items: [@ast::meta_item] = [];
|
2011-10-21 05:31:48 -05:00
|
|
|
ebml::tagged_docs(md, tag_meta_item_word) {|meta_item_doc|
|
2011-08-12 00:58:14 -05:00
|
|
|
let nd = ebml::get_doc(meta_item_doc, tag_meta_item_name);
|
2012-01-25 03:20:55 -06:00
|
|
|
let n = str::from_bytes(ebml::doc_data(nd));
|
2011-08-19 17:16:48 -05:00
|
|
|
items += [attr::mk_word_item(n)];
|
2011-10-21 05:31:48 -05:00
|
|
|
};
|
|
|
|
ebml::tagged_docs(md, tag_meta_item_name_value) {|meta_item_doc|
|
2011-08-12 00:58:14 -05:00
|
|
|
let nd = ebml::get_doc(meta_item_doc, tag_meta_item_name);
|
|
|
|
let vd = ebml::get_doc(meta_item_doc, tag_meta_item_value);
|
2012-01-25 03:20:55 -06:00
|
|
|
let n = str::from_bytes(ebml::doc_data(nd));
|
|
|
|
let v = str::from_bytes(ebml::doc_data(vd));
|
2011-07-05 19:01:23 -05:00
|
|
|
// FIXME (#611): Should be able to decode meta_name_value variants,
|
|
|
|
// but currently they can't be encoded
|
2011-08-19 17:16:48 -05:00
|
|
|
items += [attr::mk_name_value_item_str(n, v)];
|
2011-10-21 05:31:48 -05:00
|
|
|
};
|
|
|
|
ebml::tagged_docs(md, tag_meta_item_list) {|meta_item_doc|
|
2011-08-12 00:58:14 -05:00
|
|
|
let nd = ebml::get_doc(meta_item_doc, tag_meta_item_name);
|
2012-01-25 03:20:55 -06:00
|
|
|
let n = str::from_bytes(ebml::doc_data(nd));
|
2011-07-27 07:19:39 -05:00
|
|
|
let subitems = get_meta_items(meta_item_doc);
|
2011-08-19 17:16:48 -05:00
|
|
|
items += [attr::mk_list_item(n, subitems)];
|
2011-10-21 05:31:48 -05:00
|
|
|
};
|
2011-06-27 20:32:15 -05:00
|
|
|
ret items;
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn get_attributes(md: ebml::doc) -> [ast::attribute] {
|
2011-08-19 17:16:48 -05:00
|
|
|
let attrs: [ast::attribute] = [];
|
2011-08-12 00:58:14 -05:00
|
|
|
alt ebml::maybe_get_doc(md, tag_attributes) {
|
2011-07-27 07:19:39 -05:00
|
|
|
option::some(attrs_d) {
|
2011-10-21 05:31:48 -05:00
|
|
|
ebml::tagged_docs(attrs_d, tag_attribute) {|attr_doc|
|
2011-07-27 07:19:39 -05:00
|
|
|
let meta_items = get_meta_items(attr_doc);
|
|
|
|
// Currently it's only possible to have a single meta item on
|
|
|
|
// an attribute
|
2011-08-15 18:38:23 -05:00
|
|
|
assert (vec::len(meta_items) == 1u);
|
2011-08-19 17:16:48 -05:00
|
|
|
let meta_item = meta_items[0];
|
2011-07-27 07:19:39 -05:00
|
|
|
attrs +=
|
2011-08-19 17:16:48 -05:00
|
|
|
[{node: {style: ast::attr_outer, value: *meta_item},
|
2011-08-21 23:44:41 -05:00
|
|
|
span: ast_util::dummy_sp()}];
|
2011-10-21 05:31:48 -05:00
|
|
|
};
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-01-19 00:37:22 -06:00
|
|
|
option::none { }
|
2011-06-27 21:41:48 -05:00
|
|
|
}
|
|
|
|
ret attrs;
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn list_meta_items(meta_items: ebml::doc, out: io::writer) {
|
2011-08-15 23:54:52 -05:00
|
|
|
for mi: @ast::meta_item in get_meta_items(meta_items) {
|
2011-09-02 17:34:58 -05:00
|
|
|
out.write_str(#fmt["%s\n", pprust::meta_item_to_str(*mi)]);
|
2011-06-27 20:32:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-11 09:23:38 -06:00
|
|
|
fn list_crate_attributes(md: ebml::doc, hash: str, out: io::writer) {
|
|
|
|
out.write_str(#fmt("=Crate Attributes (%s)=\n", hash));
|
2011-06-27 21:41:48 -05:00
|
|
|
|
2011-08-15 23:54:52 -05:00
|
|
|
for attr: ast::attribute in get_attributes(md) {
|
2011-09-02 17:34:58 -05:00
|
|
|
out.write_str(#fmt["%s\n", pprust::attribute_to_str(attr)]);
|
2011-06-27 21:41:48 -05:00
|
|
|
}
|
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
out.write_str("\n\n");
|
2011-06-27 20:32:15 -05:00
|
|
|
}
|
|
|
|
|
2011-08-04 18:20:09 -05:00
|
|
|
fn get_crate_attributes(data: @[u8]) -> [ast::attribute] {
|
2012-03-14 14:07:23 -05:00
|
|
|
ret get_attributes(ebml::doc(data));
|
2011-06-28 13:24:24 -05:00
|
|
|
}
|
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
type crate_dep = {cnum: ast::crate_num, ident: str};
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-08-04 18:20:09 -05:00
|
|
|
fn get_crate_deps(data: @[u8]) -> [crate_dep] {
|
2011-08-19 17:16:48 -05:00
|
|
|
let deps: [crate_dep] = [];
|
2012-03-14 14:07:23 -05:00
|
|
|
let cratedoc = ebml::doc(data);
|
2011-08-12 00:58:14 -05:00
|
|
|
let depsdoc = ebml::get_doc(cratedoc, tag_crate_deps);
|
2011-07-27 07:19:39 -05:00
|
|
|
let crate_num = 1;
|
2011-10-21 05:31:48 -05:00
|
|
|
ebml::tagged_docs(depsdoc, tag_crate_dep) {|depdoc|
|
2012-01-25 03:20:55 -06:00
|
|
|
let depname = str::from_bytes(ebml::doc_data(depdoc));
|
2011-08-19 17:16:48 -05:00
|
|
|
deps += [{cnum: crate_num, ident: depname}];
|
2011-07-08 13:29:56 -05:00
|
|
|
crate_num += 1;
|
2011-10-21 05:31:48 -05:00
|
|
|
};
|
2011-07-08 13:29:56 -05:00
|
|
|
ret deps;
|
|
|
|
}
|
|
|
|
|
2011-08-11 21:14:38 -05:00
|
|
|
fn list_crate_deps(data: @[u8], out: io::writer) {
|
2011-09-02 17:34:58 -05:00
|
|
|
out.write_str("=External Dependencies=\n");
|
2011-07-08 13:29:56 -05:00
|
|
|
|
2011-08-15 23:54:52 -05:00
|
|
|
for dep: crate_dep in get_crate_deps(data) {
|
2011-09-02 17:34:58 -05:00
|
|
|
out.write_str(#fmt["%d %s\n", dep.cnum, dep.ident]);
|
2011-07-08 13:29:56 -05:00
|
|
|
}
|
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
out.write_str("\n");
|
2011-07-08 13:29:56 -05:00
|
|
|
}
|
|
|
|
|
2011-12-11 09:23:38 -06:00
|
|
|
fn get_crate_hash(data: @[u8]) -> str {
|
2012-03-14 14:07:23 -05:00
|
|
|
let cratedoc = ebml::doc(data);
|
2011-12-11 09:23:38 -06:00
|
|
|
let hashdoc = ebml::get_doc(cratedoc, tag_crate_hash);
|
2012-01-25 03:20:55 -06:00
|
|
|
ret str::from_bytes(ebml::doc_data(hashdoc));
|
2011-12-11 09:23:38 -06:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn list_crate_items(bytes: @[u8], md: ebml::doc, out: io::writer) {
|
2011-09-02 17:34:58 -05:00
|
|
|
out.write_str("=Items=\n");
|
2011-08-12 00:58:14 -05:00
|
|
|
let items = ebml::get_doc(md, tag_items);
|
2012-01-20 12:45:25 -06:00
|
|
|
iter_crate_items(bytes) {|path, did|
|
|
|
|
out.write_str(#fmt["%s (%s)\n", path, describe_def(items, did)]);
|
|
|
|
}
|
|
|
|
out.write_str("\n");
|
|
|
|
}
|
|
|
|
|
2012-01-23 16:59:00 -06:00
|
|
|
fn iter_crate_items(bytes: @[u8], proc: fn(str, ast::def_id)) {
|
2012-03-14 14:07:23 -05:00
|
|
|
let md = ebml::doc(bytes);
|
2012-01-20 12:45:25 -06:00
|
|
|
let paths = ebml::get_doc(md, tag_paths);
|
2011-08-12 00:58:14 -05:00
|
|
|
let index = ebml::get_doc(paths, tag_index);
|
|
|
|
let bs = ebml::get_doc(index, tag_index_buckets);
|
2011-10-21 05:31:48 -05:00
|
|
|
ebml::tagged_docs(bs, tag_index_buckets_bucket) {|bucket|
|
2011-07-27 07:19:39 -05:00
|
|
|
let et = tag_index_buckets_bucket_elt;
|
2011-10-21 05:31:48 -05:00
|
|
|
ebml::tagged_docs(bucket, et) {|elt|
|
2011-07-27 07:19:39 -05:00
|
|
|
let data = read_path(elt);
|
2012-02-10 18:38:33 -06:00
|
|
|
let {tag:_, doc:def} = ebml::doc_at(bytes, data.pos);
|
2011-08-12 00:58:14 -05:00
|
|
|
let did_doc = ebml::get_doc(def, tag_def_id);
|
|
|
|
let did = parse_def_id(ebml::doc_data(did_doc));
|
2012-01-20 12:45:25 -06:00
|
|
|
proc(data.path, did);
|
2011-10-21 05:31:48 -05:00
|
|
|
};
|
|
|
|
};
|
2012-01-20 12:45:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_crate_module_paths(bytes: @[u8]) -> [(ast::def_id, str)] {
|
|
|
|
fn mod_of_path(p: str) -> str {
|
|
|
|
str::connect(vec::init(str::split_str(p, "::")), "::")
|
|
|
|
}
|
|
|
|
|
|
|
|
// find all module (path, def_ids), which are not
|
|
|
|
// fowarded path due to renamed import or reexport
|
|
|
|
let res = [];
|
2012-03-14 14:07:23 -05:00
|
|
|
let mods = map::str_hash();
|
2012-01-20 12:45:25 -06:00
|
|
|
iter_crate_items(bytes) {|path, did|
|
|
|
|
let m = mod_of_path(path);
|
|
|
|
if str::is_not_empty(m) {
|
|
|
|
// if m has a sub-item, it must be a module
|
|
|
|
mods.insert(m, true);
|
|
|
|
}
|
|
|
|
// Collect everything by now. There might be multiple
|
|
|
|
// paths pointing to the same did. Those will be
|
|
|
|
// unified later by using the mods map
|
|
|
|
res += [(did, path)];
|
|
|
|
}
|
|
|
|
ret vec::filter(res) {|x|
|
|
|
|
let (_, xp) = x;
|
|
|
|
mods.contains_key(xp)
|
|
|
|
}
|
2011-06-27 20:32:15 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn list_crate_metadata(bytes: @[u8], out: io::writer) {
|
2011-12-11 09:23:38 -06:00
|
|
|
let hash = get_crate_hash(bytes);
|
2012-03-14 14:07:23 -05:00
|
|
|
let md = ebml::doc(bytes);
|
2011-12-11 09:23:38 -06:00
|
|
|
list_crate_attributes(md, hash, out);
|
2011-07-08 13:29:56 -05:00
|
|
|
list_crate_deps(bytes, out);
|
2011-06-27 20:32:15 -05:00
|
|
|
list_crate_items(bytes, md, out);
|
2011-06-27 17:53:27 -05:00
|
|
|
}
|
|
|
|
|
2012-01-05 09:04:59 -06:00
|
|
|
// Translates a def_id from an external crate to a def_id for the current
|
|
|
|
// compilation environment. We use this when trying to load types from
|
|
|
|
// external crates - if those types further refer to types in other crates
|
|
|
|
// then we must translate the crate number from that encoded in the external
|
|
|
|
// crate to the correct local crate number.
|
|
|
|
fn translate_def_id(cdata: cmd, did: ast::def_id) -> ast::def_id {
|
|
|
|
if did.crate == ast::local_crate {
|
|
|
|
ret {crate: cdata.cnum, node: did.node};
|
|
|
|
}
|
|
|
|
|
|
|
|
alt cdata.cnum_map.find(did.crate) {
|
|
|
|
option::some(n) { ret {crate: n, node: did.node}; }
|
2012-01-19 00:37:22 -06:00
|
|
|
option::none { fail "didn't find a crate in the cnum_map"; }
|
2012-01-05 09:04:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-27 17:53:27 -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:
|