|
| 1 | +// Copyright 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 | +use calculate_svh::SvhCalculate; |
| 12 | +use rbml::Error; |
| 13 | +use rbml::opaque::Decoder; |
| 14 | +use rustc::dep_graph::DepNode; |
| 15 | +use rustc::hir::def_id::DefId; |
| 16 | +use rustc::hir::svh::Svh; |
| 17 | +use rustc::ty::TyCtxt; |
| 18 | +use rustc_data_structures::fnv::FnvHashMap; |
| 19 | +use rustc_serialize::Decodable; |
| 20 | +use std::io::{ErrorKind, Read}; |
| 21 | +use std::fs::File; |
| 22 | +use syntax::ast; |
| 23 | + |
| 24 | +use super::data::*; |
| 25 | +use super::util::*; |
| 26 | + |
| 27 | +pub struct HashContext<'a, 'tcx: 'a> { |
| 28 | + pub tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 29 | + item_metadata_hashes: FnvHashMap<DefId, u64>, |
| 30 | + crate_hashes: FnvHashMap<ast::CrateNum, Svh>, |
| 31 | +} |
| 32 | + |
| 33 | +impl<'a, 'tcx> HashContext<'a, 'tcx> { |
| 34 | + pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Self { |
| 35 | + HashContext { |
| 36 | + tcx: tcx, |
| 37 | + item_metadata_hashes: FnvHashMap(), |
| 38 | + crate_hashes: FnvHashMap(), |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + pub fn hash(&mut self, dep_node: DepNode<DefId>) -> Option<u64> { |
| 43 | + match dep_node { |
| 44 | + // HIR nodes (which always come from our crate) are an input: |
| 45 | + DepNode::Hir(def_id) => { |
| 46 | + assert!(def_id.is_local()); |
| 47 | + Some(self.hir_hash(def_id)) |
| 48 | + } |
| 49 | + |
| 50 | + // MetaData from other crates is an *input* to us. |
| 51 | + // MetaData nodes from *our* crates are an *output*; we |
| 52 | + // don't hash them, but we do compute a hash for them and |
| 53 | + // save it for others to use. |
| 54 | + DepNode::MetaData(def_id) if !def_id.is_local() => { |
| 55 | + Some(self.metadata_hash(def_id)) |
| 56 | + } |
| 57 | + |
| 58 | + _ => { |
| 59 | + // Other kinds of nodes represent computed by-products |
| 60 | + // that we don't hash directly; instead, they should |
| 61 | + // have some transitive dependency on a Hir or |
| 62 | + // MetaData node, so we'll just hash that |
| 63 | + None |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + fn hir_hash(&mut self, def_id: DefId) -> u64 { |
| 69 | + assert!(def_id.is_local()); |
| 70 | + // FIXME(#32753) -- should we use a distinct hash here |
| 71 | + self.tcx.calculate_item_hash(def_id) |
| 72 | + } |
| 73 | + |
| 74 | + fn metadata_hash(&mut self, def_id: DefId) -> u64 { |
| 75 | + debug!("metadata_hash(def_id={:?})", def_id); |
| 76 | + |
| 77 | + assert!(!def_id.is_local()); |
| 78 | + loop { |
| 79 | + // check whether we have a result cached for this def-id |
| 80 | + if let Some(&hash) = self.item_metadata_hashes.get(&def_id) { |
| 81 | + debug!("metadata_hash: def_id={:?} hash={:?}", def_id, hash); |
| 82 | + return hash; |
| 83 | + } |
| 84 | + |
| 85 | + // check whether we did not find detailed metadata for this |
| 86 | + // krate; in that case, we just use the krate's overall hash |
| 87 | + if let Some(&hash) = self.crate_hashes.get(&def_id.krate) { |
| 88 | + debug!("metadata_hash: def_id={:?} crate_hash={:?}", def_id, hash); |
| 89 | + return hash.as_u64(); |
| 90 | + } |
| 91 | + |
| 92 | + // otherwise, load the data and repeat. |
| 93 | + self.load_data(def_id.krate); |
| 94 | + assert!(self.crate_hashes.contains_key(&def_id.krate)); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + fn load_data(&mut self, cnum: ast::CrateNum) { |
| 99 | + debug!("load_data(cnum={})", cnum); |
| 100 | + |
| 101 | + let svh = self.tcx.sess.cstore.crate_hash(cnum); |
| 102 | + let old = self.crate_hashes.insert(cnum, svh); |
| 103 | + debug!("load_data: svh={}", svh); |
| 104 | + assert!(old.is_none(), "loaded data for crate {:?} twice", cnum); |
| 105 | + |
| 106 | + if let Some(path) = metadata_hash_path(self.tcx, cnum) { |
| 107 | + debug!("load_data: path={:?}", path); |
| 108 | + let mut data = vec![]; |
| 109 | + match |
| 110 | + File::open(&path) |
| 111 | + .and_then(|mut file| file.read_to_end(&mut data)) |
| 112 | + { |
| 113 | + Ok(_) => { |
| 114 | + match self.load_from_data(cnum, &data) { |
| 115 | + Ok(()) => { } |
| 116 | + Err(err) => { |
| 117 | + bug!("decoding error in dep-graph from `{}`: {}", |
| 118 | + path.display(), err); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + Err(err) => { |
| 123 | + match err.kind() { |
| 124 | + ErrorKind::NotFound => { |
| 125 | + // If the file is not found, that's ok. |
| 126 | + } |
| 127 | + _ => { |
| 128 | + self.tcx.sess.err( |
| 129 | + &format!("could not load dep information from `{}`: {}", |
| 130 | + path.display(), err)); |
| 131 | + return; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + fn load_from_data(&mut self, cnum: ast::CrateNum, data: &[u8]) -> Result<(), Error> { |
| 140 | + debug!("load_from_data(cnum={})", cnum); |
| 141 | + |
| 142 | + // Load up the hashes for the def-ids from this crate. |
| 143 | + let mut decoder = Decoder::new(data, 0); |
| 144 | + let serialized_hashes = try!(SerializedMetadataHashes::decode(&mut decoder)); |
| 145 | + for serialized_hash in serialized_hashes.hashes { |
| 146 | + // the hashes are stored with just a def-index, which is |
| 147 | + // always relative to the old crate; convert that to use |
| 148 | + // our internal crate number |
| 149 | + let def_id = DefId { krate: cnum, index: serialized_hash.def_index }; |
| 150 | + |
| 151 | + // record the hash for this dep-node |
| 152 | + let old = self.item_metadata_hashes.insert(def_id, serialized_hash.hash); |
| 153 | + debug!("load_from_data: def_id={:?} hash={}", def_id, serialized_hash.hash); |
| 154 | + assert!(old.is_none(), "already have hash for {:?}", def_id); |
| 155 | + } |
| 156 | + Ok(()) |
| 157 | + } |
| 158 | +} |
0 commit comments