2012-12-03 18:48:01 -06: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 16:16:52 -06:00
|
|
|
|
2011-07-07 20:39:44 -05:00
|
|
|
// The crate store - a central repo for information collected about external
|
|
|
|
// crates and libraries
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use metadata::creader;
|
|
|
|
use metadata::cstore;
|
|
|
|
use metadata::decoder;
|
|
|
|
|
|
|
|
use core::option;
|
|
|
|
use core::str;
|
|
|
|
use core::vec;
|
2013-02-01 01:13:36 -06:00
|
|
|
use std::oldmap::HashMap;
|
|
|
|
use std::oldmap;
|
2012-12-23 16:41:37 -06:00
|
|
|
use std;
|
2012-09-04 13:54:36 -05:00
|
|
|
use syntax::{ast, attr};
|
|
|
|
use syntax::parse::token::ident_interner;
|
2011-07-07 20:00:16 -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.
|
2013-02-01 01:13:36 -06:00
|
|
|
pub type cnum_map = oldmap::HashMap<ast::crate_num, ast::crate_num>;
|
2011-07-08 14:08:43 -05:00
|
|
|
|
2013-01-29 18:51:16 -06:00
|
|
|
pub type crate_metadata = @{name: ~str,
|
|
|
|
data: @~[u8],
|
|
|
|
cnum_map: cnum_map,
|
|
|
|
cnum: ast::crate_num};
|
2011-07-07 20:00:16 -05:00
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub struct CStore {
|
|
|
|
priv metas: oldmap::HashMap<ast::crate_num, crate_metadata>,
|
|
|
|
priv use_crate_map: use_crate_map,
|
|
|
|
priv used_crate_files: ~[Path],
|
|
|
|
priv used_libraries: ~[~str],
|
|
|
|
priv used_link_args: ~[~str],
|
|
|
|
intr: @ident_interner
|
|
|
|
}
|
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
|
2013-02-01 01:13:36 -06:00
|
|
|
type use_crate_map = oldmap::HashMap<ast::node_id, ast::crate_num>;
|
2011-07-07 23:37:56 -05:00
|
|
|
|
2013-01-29 18:51:16 -06:00
|
|
|
pub fn mk_cstore(intr: @ident_interner) -> CStore {
|
2013-02-01 01:13:36 -06:00
|
|
|
let meta_cache = oldmap::HashMap();
|
|
|
|
let crate_map = oldmap::HashMap();
|
2013-02-04 16:02:01 -06:00
|
|
|
return CStore {
|
|
|
|
metas: meta_cache,
|
|
|
|
use_crate_map: crate_map,
|
|
|
|
used_crate_files: ~[],
|
|
|
|
used_libraries: ~[],
|
|
|
|
used_link_args: ~[],
|
|
|
|
intr: intr
|
|
|
|
};
|
2011-07-07 20:00:16 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn get_crate_data(cstore: @mut CStore, cnum: ast::crate_num)
|
2013-01-29 18:51:16 -06:00
|
|
|
-> crate_metadata {
|
2013-02-04 16:02:01 -06:00
|
|
|
return cstore.metas.get(&cnum);
|
2011-07-07 20:00:16 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn get_crate_hash(cstore: @mut 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
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn get_crate_vers(cstore: @mut 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
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn set_crate_data(cstore: @mut CStore,
|
2013-01-29 18:51:16 -06:00
|
|
|
cnum: ast::crate_num,
|
|
|
|
data: crate_metadata) {
|
2013-02-04 16:02:01 -06:00
|
|
|
let metas = cstore.metas;
|
|
|
|
metas.insert(cnum, data);
|
2011-07-07 20:00:16 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn have_crate_data(cstore: @mut CStore, cnum: ast::crate_num) -> bool {
|
2013-02-08 16:08:02 -06:00
|
|
|
cstore.metas.contains_key(&cnum)
|
2011-07-10 00:56:12 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn iter_crate_data(cstore: @mut CStore,
|
2013-01-29 18:51:16 -06:00
|
|
|
i: fn(ast::crate_num, crate_metadata)) {
|
2013-02-04 16:02:01 -06:00
|
|
|
let metas = cstore.metas;
|
2013-02-08 16:08:02 -06:00
|
|
|
for metas.each |&k, &v| {
|
2013-02-04 16:02:01 -06:00
|
|
|
i(k, v);
|
|
|
|
}
|
2011-07-07 20:00:16 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn add_used_crate_file(cstore: @mut CStore, lib: &Path) {
|
|
|
|
if !vec::contains(cstore.used_crate_files, lib) {
|
|
|
|
cstore.used_crate_files.push(copy *lib);
|
2011-07-07 20:25:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn get_used_crate_files(cstore: @mut CStore) -> ~[Path] {
|
|
|
|
return /*bad*/copy cstore.used_crate_files;
|
2011-07-07 20:25:56 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn add_used_library(cstore: @mut CStore, +lib: ~str) -> bool {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert lib != ~"";
|
2011-07-07 20:33:59 -05:00
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
if vec::contains(cstore.used_libraries, &lib) { return false; }
|
|
|
|
cstore.used_libraries.push(lib);
|
2012-08-01 19:30:05 -05:00
|
|
|
return true;
|
2011-07-07 20:33:59 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn get_used_libraries(cstore: @mut CStore) -> ~[~str] {
|
|
|
|
return /*bad*/copy cstore.used_libraries;
|
2011-07-07 20:33:59 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn add_used_link_args(cstore: @mut CStore, args: ~str) {
|
|
|
|
cstore.used_link_args.push_all(str::split_char(args, ' '));
|
2011-07-07 20:38:42 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn get_used_link_args(cstore: @mut CStore) -> ~[~str] {
|
|
|
|
return /*bad*/copy cstore.used_link_args;
|
2011-07-07 20:38:42 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn add_use_stmt_cnum(cstore: @mut CStore,
|
|
|
|
use_id: ast::node_id,
|
2013-01-29 18:51:16 -06:00
|
|
|
cnum: ast::crate_num) {
|
2013-02-04 16:02:01 -06:00
|
|
|
let use_crate_map = cstore.use_crate_map;
|
|
|
|
use_crate_map.insert(use_id, cnum);
|
2011-07-10 00:56:12 -05:00
|
|
|
}
|
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn find_use_stmt_cnum(cstore: @mut CStore,
|
|
|
|
use_id: ast::node_id)
|
|
|
|
-> Option<ast::crate_num> {
|
|
|
|
let use_crate_map = cstore.use_crate_map;
|
|
|
|
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.
|
2013-02-04 16:02:01 -06:00
|
|
|
pub fn get_dep_hashes(cstore: @mut CStore) -> ~[~str] {
|
2012-07-18 18:18:02 -05:00
|
|
|
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
|
|
|
|
2013-02-04 16:02:01 -06:00
|
|
|
let use_crate_map = cstore.use_crate_map;
|
2013-02-08 16:08:02 -06:00
|
|
|
for 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-08-22 19:24:52 -05:00
|
|
|
debug!("Add hash[%s]: %s", cdata.name, hash);
|
2013-01-07 16:16:52 -06:00
|
|
|
result.push({name: /*bad*/copy cdata.name, hash: hash});
|
2013-02-04 16:02:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pure fn lteq(a: &crate_hash, b: &crate_hash) -> bool {
|
|
|
|
a.name <= b.name
|
|
|
|
}
|
|
|
|
|
2012-09-27 19:05:13 -05:00
|
|
|
let sorted = std::sort::merge_sort(result, lteq);
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("sorted:");
|
2012-06-30 18:19:07 -05:00
|
|
|
for sorted.each |x| {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!(" hash[%s]: %s", x.name, x.hash);
|
2011-12-11 09:42:32 -06:00
|
|
|
}
|
2013-02-04 16:02:01 -06:00
|
|
|
|
|
|
|
fn mapper(ch: &crate_hash) -> ~str {
|
|
|
|
return /*bad*/copy ch.hash;
|
|
|
|
}
|
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
return vec::map(sorted, mapper);
|
2011-12-11 09:42:32 -06:00
|
|
|
}
|
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:
|