1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 // The crate store - a central repo for information collected about external
13 // crates and libraries
17 use metadata::decoder;
19 use std::hashmap::HashMap;
22 use syntax::parse::token::ident_interner;
24 // A map from external crate numbers (as decoded from some crate file) to
25 // local crate numbers (as generated during this session). Each external
26 // crate may refer to types in other external crates, and each has their
28 pub type cnum_map = @mut HashMap<ast::CrateNum, ast::CrateNum>;
30 pub struct crate_metadata {
38 priv metas: HashMap <ast::CrateNum, @crate_metadata>,
39 priv extern_mod_crate_map: extern_mod_crate_map,
40 priv used_crate_files: ~[Path],
41 priv used_libraries: ~[@str],
42 priv used_link_args: ~[@str],
46 // Map from NodeId's of local extern mod statements to crate numbers
47 type extern_mod_crate_map = HashMap<ast::NodeId, ast::CrateNum>;
49 pub fn mk_cstore(intr: @ident_interner) -> CStore {
51 metas: HashMap::new(),
52 extern_mod_crate_map: HashMap::new(),
53 used_crate_files: ~[],
60 pub fn get_crate_data(cstore: &CStore, cnum: ast::CrateNum)
62 return *cstore.metas.get(&cnum);
65 pub fn get_crate_hash(cstore: &CStore, cnum: ast::CrateNum) -> @str {
66 let cdata = get_crate_data(cstore, cnum);
67 decoder::get_crate_hash(cdata.data)
70 pub fn get_crate_vers(cstore: &CStore, cnum: ast::CrateNum) -> @str {
71 let cdata = get_crate_data(cstore, cnum);
72 decoder::get_crate_vers(cdata.data)
75 pub fn set_crate_data(cstore: &mut CStore,
77 data: @crate_metadata) {
78 cstore.metas.insert(cnum, data);
81 pub fn have_crate_data(cstore: &CStore, cnum: ast::CrateNum) -> bool {
82 cstore.metas.contains_key(&cnum)
85 pub fn iter_crate_data(cstore: &CStore,
86 i: &fn(ast::CrateNum, @crate_metadata)) {
87 for (&k, &v) in cstore.metas.iter() {
92 pub fn add_used_crate_file(cstore: &mut CStore, lib: &Path) {
93 if !cstore.used_crate_files.contains(lib) {
94 cstore.used_crate_files.push((*lib).clone());
98 pub fn get_used_crate_files(cstore: &CStore) -> ~[Path] {
99 // XXX(pcwalton): Bad copy.
100 return cstore.used_crate_files.clone();
103 pub fn add_used_library(cstore: &mut CStore, lib: @str) -> bool {
104 assert!(!lib.is_empty());
106 if cstore.used_libraries.iter().any(|x| x == &lib) { return false; }
107 cstore.used_libraries.push(lib);
111 pub fn get_used_libraries<'a>(cstore: &'a CStore) -> &'a [@str] {
112 let slice: &'a [@str] = cstore.used_libraries;
116 pub fn add_used_link_args(cstore: &mut CStore, args: &str) {
117 for s in args.split_iter(' ') {
118 cstore.used_link_args.push(s.to_managed());
122 pub fn get_used_link_args<'a>(cstore: &'a CStore) -> &'a [@str] {
123 let slice: &'a [@str] = cstore.used_link_args;
127 pub fn add_extern_mod_stmt_cnum(cstore: &mut CStore,
128 emod_id: ast::NodeId,
129 cnum: ast::CrateNum) {
130 cstore.extern_mod_crate_map.insert(emod_id, cnum);
133 pub fn find_extern_mod_stmt_cnum(cstore: &CStore,
134 emod_id: ast::NodeId)
135 -> Option<ast::CrateNum> {
136 cstore.extern_mod_crate_map.find(&emod_id).map_consume(|x| *x)
146 // returns hashes of crates directly used by this crate. Hashes are sorted by
147 // (crate name, crate version, crate hash) in lexicographic order (not semver)
148 pub fn get_dep_hashes(cstore: &CStore) -> ~[@str] {
149 let mut result = ~[];
151 for (_, &cnum) in cstore.extern_mod_crate_map.iter() {
152 let cdata = cstore::get_crate_data(cstore, cnum);
153 let hash = decoder::get_crate_hash(cdata.data);
154 let vers = decoder::get_crate_vers(cdata.data);
155 debug!("Add hash[%s]: %s %s", cdata.name, vers, hash);
156 result.push(crate_hash {
163 let sorted = do extra::sort::merge_sort(result) |a, b| {
164 (a.name, a.vers, a.hash) <= (b.name, b.vers, b.hash)
168 for x in sorted.iter() {
169 debug!(" hash[%s]: %s", x.name, x.hash);
172 sorted.map(|ch| ch.hash)