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