]> git.lizzy.rs Git - rust.git/blob - src/librustc/metadata/cstore.rs
Fix bug in `match`ing struct patterns
[rust.git] / src / librustc / metadata / cstore.rs
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.
4 //
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.
10
11
12 // The crate store - a central repo for information collected about external
13 // crates and libraries
14
15
16 use metadata::cstore;
17 use metadata::decoder;
18
19 use std::hashmap::HashMap;
20 use extra;
21 use syntax::ast;
22 use syntax::parse::token::ident_interner;
23
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
27 // own crate numbers.
28 pub type cnum_map = @mut HashMap<ast::CrateNum, ast::CrateNum>;
29
30 pub struct crate_metadata {
31     name: @str,
32     data: @~[u8],
33     cnum_map: cnum_map,
34     cnum: ast::CrateNum
35 }
36
37 pub struct CStore {
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],
43     intr: @ident_interner
44 }
45
46 // Map from NodeId's of local extern mod statements to crate numbers
47 type extern_mod_crate_map = HashMap<ast::NodeId, ast::CrateNum>;
48
49 pub fn mk_cstore(intr: @ident_interner) -> CStore {
50     return CStore {
51         metas: HashMap::new(),
52         extern_mod_crate_map: HashMap::new(),
53         used_crate_files: ~[],
54         used_libraries: ~[],
55         used_link_args: ~[],
56         intr: intr
57     };
58 }
59
60 pub fn get_crate_data(cstore: &CStore, cnum: ast::CrateNum)
61                    -> @crate_metadata {
62     return *cstore.metas.get(&cnum);
63 }
64
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)
68 }
69
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)
73 }
74
75 pub fn set_crate_data(cstore: &mut CStore,
76                       cnum: ast::CrateNum,
77                       data: @crate_metadata) {
78     cstore.metas.insert(cnum, data);
79 }
80
81 pub fn have_crate_data(cstore: &CStore, cnum: ast::CrateNum) -> bool {
82     cstore.metas.contains_key(&cnum)
83 }
84
85 pub fn iter_crate_data(cstore: &CStore,
86                        i: &fn(ast::CrateNum, @crate_metadata)) {
87     for (&k, &v) in cstore.metas.iter() {
88         i(k, v);
89     }
90 }
91
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());
95     }
96 }
97
98 pub fn get_used_crate_files(cstore: &CStore) -> ~[Path] {
99     // XXX(pcwalton): Bad copy.
100     return cstore.used_crate_files.clone();
101 }
102
103 pub fn add_used_library(cstore: &mut CStore, lib: @str) -> bool {
104     assert!(!lib.is_empty());
105
106     if cstore.used_libraries.iter().any(|x| x == &lib) { return false; }
107     cstore.used_libraries.push(lib);
108     true
109 }
110
111 pub fn get_used_libraries<'a>(cstore: &'a CStore) -> &'a [@str] {
112     let slice: &'a [@str] = cstore.used_libraries;
113     slice
114 }
115
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());
119     }
120 }
121
122 pub fn get_used_link_args<'a>(cstore: &'a CStore) -> &'a [@str] {
123     let slice: &'a [@str] = cstore.used_link_args;
124     slice
125 }
126
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);
131 }
132
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)
137 }
138
139 #[deriving(Clone)]
140 struct crate_hash {
141     name: @str,
142     vers: @str,
143     hash: @str,
144 }
145
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 = ~[];
150
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 {
157             name: cdata.name,
158             vers: vers,
159             hash: hash
160         });
161     }
162
163     let sorted = do extra::sort::merge_sort(result) |a, b| {
164         (a.name, a.vers, a.hash) <= (b.name, b.vers, b.hash)
165     };
166
167     debug!("sorted:");
168     for x in sorted.iter() {
169         debug!("  hash[%s]: %s", x.name, x.hash);
170     }
171
172     sorted.map(|ch| ch.hash)
173 }