]> git.lizzy.rs Git - rust.git/blob - src/librustc_incremental/calculate_svh/mod.rs
a22b51ac0446167b980a832a429073e2b877ac06
[rust.git] / src / librustc_incremental / calculate_svh / mod.rs
1 // Copyright 2012-2014 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 //! Calculation of the (misnamed) "strict version hash" for crates and
12 //! items. This hash is used to tell when the HIR changed in such a
13 //! way that results from previous compilations may no longer be
14 //! applicable and hence must be recomputed. It should probably be
15 //! renamed to the ICH (incremental compilation hash).
16 //!
17 //! The hashes for all items are computed once at the beginning of
18 //! compilation and stored into a map. In addition, a hash is computed
19 //! of the **entire crate**.
20 //!
21 //! Storing the hashes in a map avoids the need to compute them twice
22 //! (once when loading prior incremental results and once when
23 //! saving), but it is also important for correctness: at least as of
24 //! the time of this writing, the typeck passes rewrites entries in
25 //! the dep-map in-place to accommodate UFCS resolutions. Since name
26 //! resolution is part of the hash, the result is that hashes computed
27 //! at the end of compilation would be different from those computed
28 //! at the beginning.
29
30 use syntax::ast;
31 use std::cell::RefCell;
32 use std::hash::{Hash, Hasher};
33 use std::collections::hash_map::DefaultHasher;
34 use rustc::dep_graph::DepNode;
35 use rustc::hir;
36 use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
37 use rustc::hir::intravisit as visit;
38 use rustc::ty::TyCtxt;
39 use rustc_data_structures::fnv::FnvHashMap;
40 use rustc::util::common::record_time;
41 use rustc::session::config::DebugInfoLevel::NoDebugInfo;
42
43 use self::def_path_hash::DefPathHashes;
44 use self::svh_visitor::StrictVersionHashVisitor;
45 use self::caching_codemap_view::CachingCodemapView;
46
47 mod def_path_hash;
48 mod svh_visitor;
49 mod caching_codemap_view;
50
51 pub struct IncrementalHashesMap {
52     hashes: FnvHashMap<DepNode<DefId>, u64>,
53
54     // These are the metadata hashes for the current crate as they were stored
55     // during the last compilation session. They are only loaded if
56     // -Z query-dep-graph was specified and are needed for auto-tests using
57     // the #[rustc_metadata_dirty] and #[rustc_metadata_clean] attributes to
58     // check whether some metadata hash has changed in between two revisions.
59     pub prev_metadata_hashes: RefCell<FnvHashMap<DefId, u64>>,
60 }
61
62 impl IncrementalHashesMap {
63     pub fn new() -> IncrementalHashesMap {
64         IncrementalHashesMap {
65             hashes: FnvHashMap(),
66             prev_metadata_hashes: RefCell::new(FnvHashMap()),
67         }
68     }
69
70     pub fn insert(&mut self, k: DepNode<DefId>, v: u64) -> Option<u64> {
71         self.hashes.insert(k, v)
72     }
73
74     pub fn iter<'a>(&'a self) -> ::std::collections::hash_map::Iter<'a, DepNode<DefId>, u64> {
75         self.hashes.iter()
76     }
77 }
78
79 impl<'a> ::std::ops::Index<&'a DepNode<DefId>> for IncrementalHashesMap {
80     type Output = u64;
81
82     fn index(&self, index: &'a DepNode<DefId>) -> &u64 {
83         &self.hashes[index]
84     }
85 }
86
87
88 pub fn compute_incremental_hashes_map<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>)
89                                                     -> IncrementalHashesMap {
90     let _ignore = tcx.dep_graph.in_ignore();
91     let krate = tcx.map.krate();
92     let hash_spans = tcx.sess.opts.debuginfo != NoDebugInfo;
93     let mut visitor = HashItemsVisitor {
94         tcx: tcx,
95         hashes: IncrementalHashesMap::new(),
96         def_path_hashes: DefPathHashes::new(tcx),
97         codemap: CachingCodemapView::new(tcx),
98         hash_spans: hash_spans,
99     };
100     record_time(&tcx.sess.perf_stats.incr_comp_hashes_time, || {
101         visitor.calculate_def_id(DefId::local(CRATE_DEF_INDEX),
102                                  |v| visit::walk_crate(v, krate));
103         krate.visit_all_items(&mut visitor);
104     });
105     record_time(&tcx.sess.perf_stats.svh_time, || visitor.compute_crate_hash());
106     visitor.hashes
107 }
108
109 struct HashItemsVisitor<'a, 'tcx: 'a> {
110     tcx: TyCtxt<'a, 'tcx, 'tcx>,
111     def_path_hashes: DefPathHashes<'a, 'tcx>,
112     codemap: CachingCodemapView<'tcx>,
113     hashes: IncrementalHashesMap,
114     hash_spans: bool,
115 }
116
117 impl<'a, 'tcx> HashItemsVisitor<'a, 'tcx> {
118     fn calculate_node_id<W>(&mut self, id: ast::NodeId, walk_op: W)
119         where W: for<'v> FnMut(&mut StrictVersionHashVisitor<'v, 'a, 'tcx>)
120     {
121         let def_id = self.tcx.map.local_def_id(id);
122         self.calculate_def_id(def_id, walk_op)
123     }
124
125     fn calculate_def_id<W>(&mut self, def_id: DefId, mut walk_op: W)
126         where W: for<'v> FnMut(&mut StrictVersionHashVisitor<'v, 'a, 'tcx>)
127     {
128         assert!(def_id.is_local());
129         debug!("HashItemsVisitor::calculate(def_id={:?})", def_id);
130         // FIXME: this should use SHA1, not DefaultHasher. DefaultHasher is not
131         // built to avoid collisions.
132         let mut state = DefaultHasher::new();
133         walk_op(&mut StrictVersionHashVisitor::new(&mut state,
134                                                    self.tcx,
135                                                    &mut self.def_path_hashes,
136                                                    &mut self.codemap,
137                                                    self.hash_spans));
138         let item_hash = state.finish();
139         self.hashes.insert(DepNode::Hir(def_id), item_hash);
140         debug!("calculate_item_hash: def_id={:?} hash={:?}", def_id, item_hash);
141     }
142
143     fn compute_crate_hash(&mut self) {
144         let krate = self.tcx.map.krate();
145
146         let mut crate_state = DefaultHasher::new();
147
148         let crate_disambiguator = self.tcx.sess.local_crate_disambiguator();
149         "crate_disambiguator".hash(&mut crate_state);
150         crate_disambiguator.len().hash(&mut crate_state);
151         crate_disambiguator.hash(&mut crate_state);
152
153         // add each item (in some deterministic order) to the overall
154         // crate hash.
155         {
156             let def_path_hashes = &mut self.def_path_hashes;
157             let mut item_hashes: Vec<_> =
158                 self.hashes.iter()
159                            .map(|(item_dep_node, &item_hash)| {
160                                // convert from a DepNode<DefId> tp a
161                                // DepNode<u64> where the u64 is the
162                                // hash of the def-id's def-path:
163                                let item_dep_node =
164                                    item_dep_node.map_def(|&did| Some(def_path_hashes.hash(did)))
165                                                 .unwrap();
166                                (item_dep_node, item_hash)
167                            })
168                            .collect();
169             item_hashes.sort(); // avoid artificial dependencies on item ordering
170             item_hashes.hash(&mut crate_state);
171         }
172
173         {
174             let mut visitor = StrictVersionHashVisitor::new(&mut crate_state,
175                                                             self.tcx,
176                                                             &mut self.def_path_hashes,
177                                                             &mut self.codemap,
178                                                             self.hash_spans);
179             visitor.hash_attributes(&krate.attrs);
180         }
181
182         let crate_hash = crate_state.finish();
183         self.hashes.insert(DepNode::Krate, crate_hash);
184         debug!("calculate_crate_hash: crate_hash={:?}", crate_hash);
185     }
186 }
187
188
189 impl<'a, 'tcx> visit::Visitor<'tcx> for HashItemsVisitor<'a, 'tcx> {
190     fn visit_item(&mut self, item: &'tcx hir::Item) {
191         self.calculate_node_id(item.id, |v| v.visit_item(item));
192         visit::walk_item(self, item);
193     }
194
195     fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem) {
196         self.calculate_node_id(item.id, |v| v.visit_foreign_item(item));
197         visit::walk_foreign_item(self, item);
198     }
199 }
200