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.
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.
11 //! The data that we will serialize and deserialize.
13 use rustc::dep_graph::{DepNode, WorkProduct, WorkProductId};
14 use rustc::hir::def_id::DefIndex;
16 use rustc_data_structures::fnv::FnvHashMap;
19 use super::directory::DefPathIndex;
21 /// Data for use when recompiling the **current crate**.
22 #[derive(Debug, RustcEncodable, RustcDecodable)]
23 pub struct SerializedDepGraph {
24 pub edges: Vec<SerializedEdge>,
26 /// These are hashes of two things:
27 /// - the HIR nodes in this crate
28 /// - the metadata nodes from dependent crates we use
30 /// In each case, we store a hash summarizing the contents of
31 /// those items as they were at the time we did this compilation.
32 /// In the case of HIR nodes, this hash is derived by walking the
33 /// HIR itself. In the case of metadata nodes, the hash is loaded
36 /// When we do the next compile, we will load these back up and
37 /// compare them against the hashes we see at that time, which
38 /// will tell us what has changed, either in this crate or in some
39 /// crate that we depend on.
41 /// Because they will be reloaded, we don't store the DefId (which
42 /// will be different when we next compile) related to each node,
43 /// but rather the `DefPathIndex`. This can then be retraced
44 /// to find the current def-id.
45 pub hashes: Vec<SerializedHash>,
48 /// Represents a "reduced" dependency edge. Unlike the full dep-graph,
49 /// the dep-graph we serialize contains only edges `S -> T` where the
50 /// source `S` is something hashable (a HIR node or foreign metadata)
51 /// and the target `T` is something significant, like a work-product.
52 /// Normally, significant nodes are only those that have saved data on
53 /// disk, but in unit-testing the set of significant nodes can be
55 pub type SerializedEdge = (DepNode<DefPathIndex>, DepNode<DefPathIndex>);
57 #[derive(Debug, RustcEncodable, RustcDecodable)]
58 pub struct SerializedHash {
59 /// def-id of thing being hashed
60 pub dep_node: DepNode<DefPathIndex>,
62 /// the hash as of previous compilation, computed by code in
64 pub hash: Fingerprint,
67 #[derive(Debug, RustcEncodable, RustcDecodable)]
68 pub struct SerializedWorkProduct {
69 /// node that produced the work-product
70 pub id: Arc<WorkProductId>,
72 /// work-product data itself
73 pub work_product: WorkProduct,
76 /// Data for use when downstream crates get recompiled.
77 #[derive(Debug, RustcEncodable, RustcDecodable)]
78 pub struct SerializedMetadataHashes {
79 /// For each def-id defined in this crate that appears in the
80 /// metadata, we hash all the inputs that were used when producing
81 /// the metadata. We save this after compilation is done. Then,
82 /// when some downstream crate is being recompiled, it can compare
83 /// the hashes we saved against the hashes that it saw from
84 /// before; this will tell it which of the items in this crate
85 /// changed, which in turn implies what items in the downstream
86 /// crate need to be recompiled.
88 /// Note that we store the def-ids here. This is because we don't
89 /// reload this file when we recompile this crate, we will just
90 /// regenerate it completely with the current hashes and new def-ids.
92 /// Then downstream creates will load up their
93 /// `SerializedDepGraph`, which may contain `MetaData(X)` nodes
94 /// where `X` refers to some item in this crate. That `X` will be
95 /// a `DefPathIndex` that gets retracted to the current `DefId`
96 /// (matching the one found in this structure).
97 pub hashes: Vec<SerializedMetadataHash>,
99 /// For each DefIndex (as it occurs in SerializedMetadataHash), this
100 /// map stores the DefPathIndex (as it occurs in DefIdDirectory), so
101 /// that we can find the new DefId for a SerializedMetadataHash in a
102 /// subsequent compilation session.
104 /// This map is only needed for running auto-tests using the
105 /// #[rustc_metadata_dirty] and #[rustc_metadata_clean] attributes, and
106 /// is only populated if -Z query-dep-graph is specified. It will be
107 /// empty otherwise. Importing crates are perfectly happy with just having
109 pub index_map: FnvHashMap<DefIndex, DefPathIndex>
112 /// The hash for some metadata that (when saving) will be exported
113 /// from this crate, or which (when importing) was exported by an
115 #[derive(Debug, RustcEncodable, RustcDecodable)]
116 pub struct SerializedMetadataHash {
117 pub def_index: DefIndex,
119 /// the hash itself, computed by `calculate_item_hash`
120 pub hash: Fingerprint,