2011-07-07 20:39:44 -05:00
|
|
|
// The crate store - a central repo for information collected about external
|
|
|
|
// crates and libraries
|
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
import std::map;
|
2012-03-07 18:48:57 -06:00
|
|
|
import std::map::hashmap;
|
2012-04-06 05:45:49 -05:00
|
|
|
import syntax::{ast, attr};
|
2012-05-23 02:38:39 -05:00
|
|
|
import syntax::ast_util::new_def_hash;
|
2011-07-07 20:00:16 -05:00
|
|
|
|
2012-05-22 12:54:12 -05:00
|
|
|
export cstore;
|
2011-07-10 00:56:12 -05:00
|
|
|
export cnum_map;
|
|
|
|
export crate_metadata;
|
|
|
|
export mk_cstore;
|
|
|
|
export get_crate_data;
|
|
|
|
export set_crate_data;
|
2012-04-06 05:45:49 -05:00
|
|
|
export get_crate_hash;
|
|
|
|
export get_crate_vers;
|
2011-07-10 00:56:12 -05:00
|
|
|
export have_crate_data;
|
|
|
|
export iter_crate_data;
|
|
|
|
export add_used_crate_file;
|
|
|
|
export get_used_crate_files;
|
|
|
|
export add_used_library;
|
|
|
|
export get_used_libraries;
|
|
|
|
export add_used_link_args;
|
|
|
|
export get_used_link_args;
|
|
|
|
export add_use_stmt_cnum;
|
2012-02-17 19:34:03 -06:00
|
|
|
export find_use_stmt_cnum;
|
2011-12-11 09:42:32 -06:00
|
|
|
export get_dep_hashes;
|
2012-01-20 12:45:25 -06:00
|
|
|
export get_path;
|
2011-12-11 09:42:32 -06:00
|
|
|
|
2011-07-10 00:56:12 -05:00
|
|
|
|
2011-07-08 14:08:43 -05:00
|
|
|
// A map from external crate numbers (as decoded from some crate file) to
|
|
|
|
// local crate numbers (as generated during this session). Each external
|
|
|
|
// crate may refer to types in other external crates, and each has their
|
|
|
|
// own crate numbers.
|
2011-08-12 09:15:18 -05:00
|
|
|
type cnum_map = map::hashmap<ast::crate_num, ast::crate_num>;
|
2011-07-08 14:08:43 -05:00
|
|
|
|
2012-01-20 12:45:25 -06:00
|
|
|
// Multiple items may have the same def_id in crate metadata. They may be
|
|
|
|
// renamed imports or reexports. This map keeps the "real" module path
|
|
|
|
// and def_id.
|
2012-07-14 00:57:48 -05:00
|
|
|
type mod_path_map = map::hashmap<ast::def_id, @~str>;
|
2012-01-20 12:45:25 -06:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
type crate_metadata = @{name: ~str,
|
2012-06-29 18:26:56 -05:00
|
|
|
data: @~[u8],
|
2012-01-05 09:04:59 -06:00
|
|
|
cnum_map: cnum_map,
|
|
|
|
cnum: ast::crate_num};
|
2011-07-07 20:00:16 -05:00
|
|
|
|
2011-07-10 00:56:12 -05:00
|
|
|
// This is a bit of an experiment at encapsulating the data in cstore. By
|
2012-01-19 16:24:03 -06:00
|
|
|
// keeping all the data in a non-exported enum variant, it's impossible for
|
2011-07-10 00:56:12 -05:00
|
|
|
// other modules to access the cstore's private data. This could also be
|
|
|
|
// achieved with an obj, but at the expense of a vtable. Not sure if this is a
|
|
|
|
// good pattern or not.
|
2012-01-19 19:56:05 -06:00
|
|
|
enum cstore { private(cstore_private), }
|
2011-07-10 00:56:12 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type cstore_private =
|
2011-08-12 09:15:18 -05:00
|
|
|
@{metas: map::hashmap<ast::crate_num, crate_metadata>,
|
2011-07-27 07:19:39 -05:00
|
|
|
use_crate_map: use_crate_map,
|
2012-01-20 12:45:25 -06:00
|
|
|
mod_path_map: mod_path_map,
|
2012-07-14 00:57:48 -05:00
|
|
|
mut used_crate_files: ~[~str],
|
|
|
|
mut used_libraries: ~[~str],
|
|
|
|
mut used_link_args: ~[~str]};
|
2011-07-10 00:56:12 -05:00
|
|
|
|
2011-07-07 23:37:56 -05:00
|
|
|
// Map from node_id's of local use statements to crate numbers
|
2011-08-12 09:15:18 -05:00
|
|
|
type use_crate_map = map::hashmap<ast::node_id, ast::crate_num>;
|
2011-07-07 23:37:56 -05:00
|
|
|
|
2011-07-10 00:56:12 -05:00
|
|
|
// Internal method to retrieve the data from the cstore
|
2012-06-04 10:03:14 -05:00
|
|
|
pure fn p(cstore: cstore) -> cstore_private {
|
|
|
|
alt cstore { private(p) { p } }
|
|
|
|
}
|
2011-07-07 20:00:16 -05:00
|
|
|
|
|
|
|
fn mk_cstore() -> cstore {
|
2012-03-14 14:07:23 -05:00
|
|
|
let meta_cache = map::int_hash::<crate_metadata>();
|
|
|
|
let crate_map = map::int_hash::<ast::crate_num>();
|
2012-01-20 12:45:25 -06:00
|
|
|
let mod_path_map = new_def_hash();
|
2012-08-01 19:30:05 -05:00
|
|
|
return private(@{metas: meta_cache,
|
2011-07-27 07:19:39 -05:00
|
|
|
use_crate_map: crate_map,
|
2012-01-20 12:45:25 -06:00
|
|
|
mod_path_map: mod_path_map,
|
2012-06-29 18:26:56 -05:00
|
|
|
mut used_crate_files: ~[],
|
|
|
|
mut used_libraries: ~[],
|
|
|
|
mut used_link_args: ~[]});
|
2011-07-07 20:00:16 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn get_crate_data(cstore: cstore, cnum: ast::crate_num) -> crate_metadata {
|
2012-08-01 19:30:05 -05:00
|
|
|
return p(cstore).metas.get(cnum);
|
2011-07-07 20:00:16 -05:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn get_crate_hash(cstore: cstore, cnum: ast::crate_num) -> @~str {
|
2012-04-06 05:45:49 -05:00
|
|
|
let cdata = get_crate_data(cstore, cnum);
|
2012-08-01 19:30:05 -05:00
|
|
|
return decoder::get_crate_hash(cdata.data);
|
2012-04-06 05:45:49 -05:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn get_crate_vers(cstore: cstore, cnum: ast::crate_num) -> @~str {
|
2012-04-06 05:45:49 -05:00
|
|
|
let cdata = get_crate_data(cstore, cnum);
|
2012-08-01 19:30:05 -05:00
|
|
|
return decoder::get_crate_vers(cdata.data);
|
2012-04-06 05:45:49 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn set_crate_data(cstore: cstore, cnum: ast::crate_num,
|
|
|
|
data: crate_metadata) {
|
2011-07-10 00:56:12 -05:00
|
|
|
p(cstore).metas.insert(cnum, data);
|
2012-06-30 18:19:07 -05:00
|
|
|
do vec::iter(decoder::get_crate_module_paths(data.data)) |dp| {
|
2012-01-20 12:45:25 -06:00
|
|
|
let (did, path) = dp;
|
|
|
|
let d = {crate: cnum, node: did.node};
|
2012-06-10 02:49:59 -05:00
|
|
|
p(cstore).mod_path_map.insert(d, @path);
|
2012-01-20 12:45:25 -06:00
|
|
|
}
|
2011-07-07 20:00:16 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn have_crate_data(cstore: cstore, cnum: ast::crate_num) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return p(cstore).metas.contains_key(cnum);
|
2011-07-10 00:56:12 -05:00
|
|
|
}
|
|
|
|
|
2012-01-23 16:59:00 -06:00
|
|
|
fn iter_crate_data(cstore: cstore, i: fn(ast::crate_num, crate_metadata)) {
|
2012-06-30 18:19:07 -05:00
|
|
|
for p(cstore).metas.each |k,v| { i(k, v);};
|
2011-07-07 20:00:16 -05:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn add_used_crate_file(cstore: cstore, lib: ~str) {
|
2012-02-11 19:54:49 -06:00
|
|
|
if !vec::contains(p(cstore).used_crate_files, lib) {
|
2012-06-26 02:39:18 -05:00
|
|
|
vec::push(p(cstore).used_crate_files, lib);
|
2011-07-07 20:25:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn get_used_crate_files(cstore: cstore) -> ~[~str] {
|
2012-08-01 19:30:05 -05:00
|
|
|
return p(cstore).used_crate_files;
|
2011-07-07 20:25:56 -05:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn add_used_library(cstore: cstore, lib: ~str) -> bool {
|
|
|
|
assert lib != ~"";
|
2011-07-07 20:33:59 -05:00
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
if vec::contains(p(cstore).used_libraries, lib) { return false; }
|
2012-06-26 02:39:18 -05:00
|
|
|
vec::push(p(cstore).used_libraries, lib);
|
2012-08-01 19:30:05 -05:00
|
|
|
return true;
|
2011-07-07 20:33:59 -05:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn get_used_libraries(cstore: cstore) -> ~[~str] {
|
2012-08-01 19:30:05 -05:00
|
|
|
return p(cstore).used_libraries;
|
2011-07-07 20:33:59 -05:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn add_used_link_args(cstore: cstore, args: ~str) {
|
2012-06-28 17:00:03 -05:00
|
|
|
vec::push_all(p(cstore).used_link_args, str::split_char(args, ' '));
|
2011-07-07 20:38:42 -05:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn get_used_link_args(cstore: cstore) -> ~[~str] {
|
2012-08-01 19:30:05 -05:00
|
|
|
return p(cstore).used_link_args;
|
2011-07-07 20:38:42 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn add_use_stmt_cnum(cstore: cstore, use_id: ast::node_id,
|
2011-07-27 07:19:39 -05:00
|
|
|
cnum: ast::crate_num) {
|
2011-07-10 00:56:12 -05:00
|
|
|
p(cstore).use_crate_map.insert(use_id, cnum);
|
|
|
|
}
|
|
|
|
|
2012-02-17 19:34:03 -06:00
|
|
|
fn find_use_stmt_cnum(cstore: cstore,
|
|
|
|
use_id: ast::node_id) -> option<ast::crate_num> {
|
|
|
|
p(cstore).use_crate_map.find(use_id)
|
2011-07-07 23:37:56 -05:00
|
|
|
}
|
|
|
|
|
2011-12-11 09:42:32 -06:00
|
|
|
// returns hashes of crates directly used by this crate. Hashes are
|
|
|
|
// sorted by crate name.
|
2012-07-14 00:57:48 -05:00
|
|
|
fn get_dep_hashes(cstore: cstore) -> ~[@~str] {
|
|
|
|
type crate_hash = {name: @~str, hash: @~str};
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut result = ~[];
|
2011-12-11 09:42:32 -06:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
for p(cstore).use_crate_map.each_value |cnum| {
|
2011-12-11 09:42:32 -06:00
|
|
|
let cdata = cstore::get_crate_data(cstore, cnum);
|
|
|
|
let hash = decoder::get_crate_hash(cdata.data);
|
2012-07-30 18:01:07 -05:00
|
|
|
debug!{"Add hash[%s]: %s", cdata.name, *hash};
|
2012-06-26 02:39:18 -05:00
|
|
|
vec::push(result, {name: @cdata.name, hash: hash});
|
2011-12-11 09:42:32 -06:00
|
|
|
};
|
|
|
|
fn lteq(a: crate_hash, b: crate_hash) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return *a.name <= *b.name;
|
2011-12-11 09:42:32 -06:00
|
|
|
}
|
|
|
|
let sorted = std::sort::merge_sort(lteq, result);
|
2012-07-30 18:01:07 -05:00
|
|
|
debug!{"sorted:"};
|
2012-06-30 18:19:07 -05:00
|
|
|
for sorted.each |x| {
|
2012-07-30 18:01:07 -05:00
|
|
|
debug!{" hash[%s]: %s", *x.name, *x.hash};
|
2011-12-11 09:42:32 -06:00
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
fn mapper(ch: crate_hash) -> @~str { return ch.hash; }
|
|
|
|
return vec::map(sorted, mapper);
|
2011-12-11 09:42:32 -06:00
|
|
|
}
|
2012-01-20 12:45:25 -06:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn get_path(cstore: cstore, d: ast::def_id) -> ~[ast::ident] {
|
2012-03-06 10:02:13 -06:00
|
|
|
// let f = bind str::split_str(_, "::");
|
2012-06-29 18:26:56 -05:00
|
|
|
option::map_default(p(cstore).mod_path_map.find(d), ~[],
|
2012-07-14 00:57:48 -05:00
|
|
|
|ds| str::split_str(*ds, ~"::").map(|x| @x ) )
|
2012-01-20 12:45:25 -06:00
|
|
|
}
|
2011-07-07 20:00:16 -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:
|