2011-07-07 20:39:44 -05:00
|
|
|
// The crate store - a central repo for information collected about external
|
|
|
|
// crates and libraries
|
|
|
|
|
2011-07-14 19:26:10 -05:00
|
|
|
import std::ivec;
|
2011-07-07 20:00:16 -05:00
|
|
|
import std::map;
|
2011-07-07 20:38:42 -05:00
|
|
|
import std::str;
|
2011-07-07 23:37:56 -05:00
|
|
|
import syntax::ast;
|
2011-07-07 20:00:16 -05:00
|
|
|
|
2011-07-10 00:56:12 -05:00
|
|
|
export cstore;
|
|
|
|
export cnum_map;
|
|
|
|
export crate_metadata;
|
|
|
|
export mk_cstore;
|
|
|
|
export get_crate_data;
|
|
|
|
export set_crate_data;
|
|
|
|
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;
|
|
|
|
export get_use_stmt_cnum;
|
|
|
|
|
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.
|
|
|
|
type cnum_map = map::hashmap[ast::crate_num, ast::crate_num];
|
|
|
|
|
2011-08-04 18:20:09 -05:00
|
|
|
type crate_metadata = {name: str, data: @[u8], cnum_map: cnum_map};
|
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
|
|
|
|
// keeping all the data in a non-exported tag variant, it's impossible for
|
|
|
|
// 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.
|
2011-07-27 07:19:39 -05:00
|
|
|
tag cstore { private(cstore_private); }
|
2011-07-10 00:56:12 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type cstore_private =
|
|
|
|
@{metas: map::hashmap[ast::crate_num, crate_metadata],
|
|
|
|
use_crate_map: use_crate_map,
|
2011-08-04 18:20:09 -05:00
|
|
|
mutable used_crate_files: [str],
|
|
|
|
mutable used_libraries: [str],
|
|
|
|
mutable 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
|
|
|
|
type use_crate_map = map::hashmap[ast::node_id, ast::crate_num];
|
|
|
|
|
2011-07-10 00:56:12 -05:00
|
|
|
// Internal method to retrieve the data from the cstore
|
2011-07-27 07:19:39 -05:00
|
|
|
fn p(cstore: &cstore) -> cstore_private { alt cstore { private(p) { p } } }
|
2011-07-07 20:00:16 -05:00
|
|
|
|
|
|
|
fn mk_cstore() -> cstore {
|
2011-07-27 07:19:39 -05:00
|
|
|
let meta_cache = map::new_int_hash[crate_metadata]();
|
|
|
|
let crate_map = map::new_int_hash[ast::crate_num]();
|
|
|
|
ret private(@{metas: meta_cache,
|
|
|
|
use_crate_map: crate_map,
|
|
|
|
mutable used_crate_files: ~[],
|
|
|
|
mutable used_libraries: ~[],
|
|
|
|
mutable used_link_args: ~[]});
|
2011-07-07 20:00:16 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn get_crate_data(cstore: &cstore, cnum: ast::crate_num) -> crate_metadata {
|
2011-07-10 00:56:12 -05:00
|
|
|
ret p(cstore).metas.get(cnum);
|
2011-07-07 20:00:16 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -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);
|
2011-07-07 20:00:16 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn have_crate_data(cstore: &cstore, cnum: ast::crate_num) -> bool {
|
2011-07-10 00:56:12 -05:00
|
|
|
ret p(cstore).metas.contains_key(cnum);
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
iter iter_crate_data(cstore: &cstore) ->
|
|
|
|
@{key: ast::crate_num, val: crate_metadata} {
|
|
|
|
for each kv: @{key: ast::crate_num, val: crate_metadata} in
|
|
|
|
p(cstore).metas.items() {
|
2011-07-10 00:56:12 -05:00
|
|
|
put kv;
|
|
|
|
}
|
2011-07-07 20:00:16 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn add_used_crate_file(cstore: &cstore, lib: &str) {
|
|
|
|
if !ivec::member(lib, p(cstore).used_crate_files) {
|
2011-07-14 19:26:10 -05:00
|
|
|
p(cstore).used_crate_files += ~[lib];
|
2011-07-07 20:25:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-04 18:20:09 -05:00
|
|
|
fn get_used_crate_files(cstore: &cstore) -> [str] {
|
2011-07-10 00:56:12 -05:00
|
|
|
ret p(cstore).used_crate_files;
|
2011-07-07 20:25:56 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn add_used_library(cstore: &cstore, lib: &str) -> bool {
|
|
|
|
if lib == "" { ret false; }
|
2011-07-07 20:33:59 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
if ivec::member(lib, p(cstore).used_libraries) { ret false; }
|
2011-07-07 20:33:59 -05:00
|
|
|
|
2011-07-14 19:26:10 -05:00
|
|
|
p(cstore).used_libraries += ~[lib];
|
2011-07-07 20:33:59 -05:00
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
|
2011-08-04 18:20:09 -05:00
|
|
|
fn get_used_libraries(cstore: &cstore) -> [str] {
|
2011-07-10 00:56:12 -05:00
|
|
|
ret p(cstore).used_libraries;
|
2011-07-07 20:33:59 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn add_used_link_args(cstore: &cstore, args: &str) {
|
2011-08-11 19:29:22 -05:00
|
|
|
p(cstore).used_link_args += str::split_ivec(args, ' ' as u8);
|
2011-07-07 20:38:42 -05:00
|
|
|
}
|
|
|
|
|
2011-08-04 18:20:09 -05:00
|
|
|
fn get_used_link_args(cstore: &cstore) -> [str] {
|
2011-07-10 00:56:12 -05:00
|
|
|
ret p(cstore).used_link_args;
|
2011-07-07 20:38:42 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn add_use_stmt_cnum(cstore: &cstore, use_id: ast::node_id,
|
|
|
|
cnum: ast::crate_num) {
|
2011-07-10 00:56:12 -05:00
|
|
|
p(cstore).use_crate_map.insert(use_id, cnum);
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn get_use_stmt_cnum(cstore: &cstore, use_id: ast::node_id) ->
|
|
|
|
ast::crate_num {
|
2011-07-10 00:56:12 -05:00
|
|
|
ret p(cstore).use_crate_map.get(use_id);
|
2011-07-07 23:37:56 -05: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
|
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|