2012-12-03 16:48:01 -08: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 14:16:52 -08:00
|
|
|
|
2011-07-07 18:39:44 -07:00
|
|
|
// The crate store - a central repo for information collected about external
|
|
|
|
// crates and libraries
|
|
|
|
|
2013-05-17 15:28:44 -07:00
|
|
|
|
2012-12-23 17:41:37 -05:00
|
|
|
use metadata::cstore;
|
|
|
|
use metadata::decoder;
|
|
|
|
|
2013-06-28 18:32:26 -04:00
|
|
|
use std::hashmap::HashMap;
|
2013-05-18 12:39:17 -07:00
|
|
|
use extra;
|
2013-03-26 16:38:07 -04:00
|
|
|
use syntax::ast;
|
2012-09-04 11:54:36 -07:00
|
|
|
use syntax::parse::token::ident_interner;
|
2011-07-07 18:00:16 -07:00
|
|
|
|
2011-07-08 12:08:43 -07: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-07-19 07:38:55 +02:00
|
|
|
pub type cnum_map = @mut HashMap<ast::CrateNum, ast::CrateNum>;
|
2011-07-08 12:08:43 -07:00
|
|
|
|
2013-02-19 02:40:42 -05:00
|
|
|
pub struct crate_metadata {
|
2013-06-13 03:02:55 +10:00
|
|
|
name: @str,
|
2013-08-25 20:21:13 -07:00
|
|
|
data: @~[u8],
|
2013-02-19 02:40:42 -05:00
|
|
|
cnum_map: cnum_map,
|
2013-07-19 07:38:55 +02:00
|
|
|
cnum: ast::CrateNum
|
2013-02-19 02:40:42 -05:00
|
|
|
}
|
2011-07-07 18:00:16 -07:00
|
|
|
|
2013-02-04 14:02:01 -08:00
|
|
|
pub struct CStore {
|
2013-07-19 07:38:55 +02:00
|
|
|
priv metas: HashMap <ast::CrateNum, @crate_metadata>,
|
2013-02-17 21:45:00 -05:00
|
|
|
priv extern_mod_crate_map: extern_mod_crate_map,
|
2013-02-04 14:02:01 -08:00
|
|
|
priv used_crate_files: ~[Path],
|
2013-06-13 03:02:55 +10:00
|
|
|
priv used_libraries: ~[@str],
|
|
|
|
priv used_link_args: ~[@str],
|
2013-02-04 14:02:01 -08:00
|
|
|
intr: @ident_interner
|
|
|
|
}
|
2011-07-09 22:56:12 -07:00
|
|
|
|
2013-07-27 10:25:59 +02:00
|
|
|
// Map from NodeId's of local extern mod statements to crate numbers
|
|
|
|
type extern_mod_crate_map = HashMap<ast::NodeId, ast::CrateNum>;
|
2011-07-07 21:37:56 -07:00
|
|
|
|
2013-01-29 16:51:16 -08:00
|
|
|
pub fn mk_cstore(intr: @ident_interner) -> CStore {
|
2013-02-04 14:02:01 -08:00
|
|
|
return CStore {
|
2013-04-03 09:28:36 -04:00
|
|
|
metas: HashMap::new(),
|
|
|
|
extern_mod_crate_map: HashMap::new(),
|
2013-02-04 14:02:01 -08:00
|
|
|
used_crate_files: ~[],
|
|
|
|
used_libraries: ~[],
|
|
|
|
used_link_args: ~[],
|
|
|
|
intr: intr
|
|
|
|
};
|
2011-07-07 18:00:16 -07:00
|
|
|
}
|
|
|
|
|
2013-07-19 07:38:55 +02:00
|
|
|
pub fn get_crate_data(cstore: &CStore, cnum: ast::CrateNum)
|
2013-02-19 02:40:42 -05:00
|
|
|
-> @crate_metadata {
|
2013-03-22 22:26:41 -04:00
|
|
|
return *cstore.metas.get(&cnum);
|
2011-07-07 18:00:16 -07:00
|
|
|
}
|
|
|
|
|
2013-07-19 07:38:55 +02:00
|
|
|
pub fn get_crate_hash(cstore: &CStore, cnum: ast::CrateNum) -> @str {
|
2012-04-06 18:45:49 +08:00
|
|
|
let cdata = get_crate_data(cstore, cnum);
|
2013-02-16 09:48:28 -08:00
|
|
|
decoder::get_crate_hash(cdata.data)
|
2012-04-06 18:45:49 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 07:38:55 +02:00
|
|
|
pub fn get_crate_vers(cstore: &CStore, cnum: ast::CrateNum) -> @str {
|
2012-04-06 18:45:49 +08:00
|
|
|
let cdata = get_crate_data(cstore, cnum);
|
2013-02-16 09:48:28 -08:00
|
|
|
decoder::get_crate_vers(cdata.data)
|
2012-04-06 18:45:49 +08:00
|
|
|
}
|
|
|
|
|
2013-03-22 22:26:41 -04:00
|
|
|
pub fn set_crate_data(cstore: &mut CStore,
|
2013-07-19 07:38:55 +02:00
|
|
|
cnum: ast::CrateNum,
|
2013-02-19 02:40:42 -05:00
|
|
|
data: @crate_metadata) {
|
2013-03-22 22:26:41 -04:00
|
|
|
cstore.metas.insert(cnum, data);
|
2011-07-07 18:00:16 -07:00
|
|
|
}
|
|
|
|
|
2013-07-19 07:38:55 +02:00
|
|
|
pub fn have_crate_data(cstore: &CStore, cnum: ast::CrateNum) -> bool {
|
2013-02-08 17:08:02 -05:00
|
|
|
cstore.metas.contains_key(&cnum)
|
2011-07-09 22:56:12 -07:00
|
|
|
}
|
|
|
|
|
2013-11-19 13:22:03 -08:00
|
|
|
pub fn iter_crate_data(cstore: &CStore, i: |ast::CrateNum, @crate_metadata|) {
|
2013-08-03 12:45:23 -04:00
|
|
|
for (&k, &v) in cstore.metas.iter() {
|
2013-02-04 14:02:01 -08:00
|
|
|
i(k, v);
|
|
|
|
}
|
2011-07-07 18:00:16 -07:00
|
|
|
}
|
|
|
|
|
2013-03-22 22:26:41 -04:00
|
|
|
pub fn add_used_crate_file(cstore: &mut CStore, lib: &Path) {
|
2013-06-29 02:08:32 +10:00
|
|
|
if !cstore.used_crate_files.contains(lib) {
|
2013-07-02 12:47:32 -07:00
|
|
|
cstore.used_crate_files.push((*lib).clone());
|
2011-07-07 18:25:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-22 22:26:41 -04:00
|
|
|
pub fn get_used_crate_files(cstore: &CStore) -> ~[Path] {
|
2013-07-02 12:47:32 -07:00
|
|
|
// XXX(pcwalton): Bad copy.
|
|
|
|
return cstore.used_crate_files.clone();
|
2011-07-07 18:25:56 -07:00
|
|
|
}
|
|
|
|
|
2013-06-13 03:02:55 +10:00
|
|
|
pub fn add_used_library(cstore: &mut CStore, lib: @str) -> bool {
|
|
|
|
assert!(!lib.is_empty());
|
2011-07-07 18:33:59 -07:00
|
|
|
|
2013-07-04 22:13:26 -04:00
|
|
|
if cstore.used_libraries.iter().any(|x| x == &lib) { return false; }
|
2013-06-13 03:02:55 +10:00
|
|
|
cstore.used_libraries.push(lib);
|
2013-02-16 09:48:28 -08:00
|
|
|
true
|
2011-07-07 18:33:59 -07:00
|
|
|
}
|
|
|
|
|
2013-06-13 03:02:55 +10:00
|
|
|
pub fn get_used_libraries<'a>(cstore: &'a CStore) -> &'a [@str] {
|
|
|
|
let slice: &'a [@str] = cstore.used_libraries;
|
|
|
|
slice
|
2011-07-07 18:33:59 -07:00
|
|
|
}
|
|
|
|
|
2013-03-22 22:26:41 -04:00
|
|
|
pub fn add_used_link_args(cstore: &mut CStore, args: &str) {
|
2013-11-23 11:18:51 +01:00
|
|
|
for s in args.split(' ') {
|
2013-06-13 03:02:55 +10:00
|
|
|
cstore.used_link_args.push(s.to_managed());
|
2013-03-24 07:51:18 +01:00
|
|
|
}
|
2011-07-07 18:38:42 -07:00
|
|
|
}
|
|
|
|
|
2013-06-13 03:02:55 +10:00
|
|
|
pub fn get_used_link_args<'a>(cstore: &'a CStore) -> &'a [@str] {
|
|
|
|
let slice: &'a [@str] = cstore.used_link_args;
|
|
|
|
slice
|
2011-07-07 18:38:42 -07:00
|
|
|
}
|
|
|
|
|
2013-03-22 22:26:41 -04:00
|
|
|
pub fn add_extern_mod_stmt_cnum(cstore: &mut CStore,
|
2013-07-27 10:25:59 +02:00
|
|
|
emod_id: ast::NodeId,
|
2013-07-19 07:38:55 +02:00
|
|
|
cnum: ast::CrateNum) {
|
2013-03-22 22:26:41 -04:00
|
|
|
cstore.extern_mod_crate_map.insert(emod_id, cnum);
|
2011-07-09 22:56:12 -07:00
|
|
|
}
|
|
|
|
|
2013-03-22 22:26:41 -04:00
|
|
|
pub fn find_extern_mod_stmt_cnum(cstore: &CStore,
|
2013-07-27 10:25:59 +02:00
|
|
|
emod_id: ast::NodeId)
|
2013-07-19 07:38:55 +02:00
|
|
|
-> Option<ast::CrateNum> {
|
2013-09-20 02:08:47 -04:00
|
|
|
cstore.extern_mod_crate_map.find(&emod_id).map(|x| *x)
|
2011-07-07 21:37:56 -07:00
|
|
|
}
|
|
|
|
|
2013-07-02 12:47:32 -07:00
|
|
|
#[deriving(Clone)]
|
|
|
|
struct crate_hash {
|
|
|
|
name: @str,
|
|
|
|
vers: @str,
|
|
|
|
hash: @str,
|
|
|
|
}
|
|
|
|
|
2013-03-23 21:15:26 -03:00
|
|
|
// returns hashes of crates directly used by this crate. Hashes are sorted by
|
|
|
|
// (crate name, crate version, crate hash) in lexicographic order (not semver)
|
2013-06-13 03:02:55 +10:00
|
|
|
pub fn get_dep_hashes(cstore: &CStore) -> ~[@str] {
|
2012-06-29 16:26:56 -07:00
|
|
|
let mut result = ~[];
|
2011-12-11 23:42:32 +08:00
|
|
|
|
2013-08-03 12:45:23 -04:00
|
|
|
for (_, &cnum) in cstore.extern_mod_crate_map.iter() {
|
2011-12-11 23:42:32 +08:00
|
|
|
let cdata = cstore::get_crate_data(cstore, cnum);
|
|
|
|
let hash = decoder::get_crate_hash(cdata.data);
|
2013-03-23 21:15:26 -03:00
|
|
|
let vers = decoder::get_crate_vers(cdata.data);
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("Add hash[{}]: {} {}", cdata.name, vers, hash);
|
2013-02-19 02:40:42 -05:00
|
|
|
result.push(crate_hash {
|
2013-02-20 16:41:21 -08:00
|
|
|
name: cdata.name,
|
2013-03-23 21:15:26 -03:00
|
|
|
vers: vers,
|
2013-02-19 02:40:42 -05:00
|
|
|
hash: hash
|
|
|
|
});
|
2013-02-04 14:02:01 -08:00
|
|
|
}
|
|
|
|
|
2013-05-18 12:39:17 -07:00
|
|
|
let sorted = do extra::sort::merge_sort(result) |a, b| {
|
2013-03-23 21:15:26 -03:00
|
|
|
(a.name, a.vers, a.hash) <= (b.name, b.vers, b.hash)
|
|
|
|
};
|
2013-02-04 14:02:01 -08:00
|
|
|
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("sorted:");
|
2013-08-03 12:45:23 -04:00
|
|
|
for x in sorted.iter() {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(" hash[{}]: {}", x.name, x.hash);
|
2011-12-11 23:42:32 +08:00
|
|
|
}
|
2013-02-04 14:02:01 -08:00
|
|
|
|
2013-06-13 03:02:55 +10:00
|
|
|
sorted.map(|ch| ch.hash)
|
2011-12-11 23:42:32 +08:00
|
|
|
}
|