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;
15 use rustc::ich::Fingerprint;
17 use rustc_data_structures::fx::FxHashMap;
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<SerializedEdgeSet>,
26 /// These are output nodes that have no incoming edges. We track
27 /// these separately so that when we reload all edges, we don't
28 /// lose track of these nodes.
29 pub bootstrap_outputs: Vec<DepNode<DefPathIndex>>,
31 /// These are hashes of two things:
32 /// - the HIR nodes in this crate
33 /// - the metadata nodes from dependent crates we use
35 /// In each case, we store a hash summarizing the contents of
36 /// those items as they were at the time we did this compilation.
37 /// In the case of HIR nodes, this hash is derived by walking the
38 /// HIR itself. In the case of metadata nodes, the hash is loaded
41 /// When we do the next compile, we will load these back up and
42 /// compare them against the hashes we see at that time, which
43 /// will tell us what has changed, either in this crate or in some
44 /// crate that we depend on.
46 /// Because they will be reloaded, we don't store the DefId (which
47 /// will be different when we next compile) related to each node,
48 /// but rather the `DefPathIndex`. This can then be retraced
49 /// to find the current def-id.
50 pub hashes: Vec<SerializedHash>,
53 /// Represents a set of "reduced" dependency edge. We group the
54 /// outgoing edges from a single source together.
55 #[derive(Debug, RustcEncodable, RustcDecodable)]
56 pub struct SerializedEdgeSet {
57 pub source: DepNode<DefPathIndex>,
58 pub targets: Vec<DepNode<DefPathIndex>>
61 #[derive(Debug, RustcEncodable, RustcDecodable)]
62 pub struct SerializedHash {
63 /// def-id of thing being hashed
64 pub dep_node: DepNode<DefPathIndex>,
66 /// the hash as of previous compilation, computed by code in
68 pub hash: Fingerprint,
71 #[derive(Debug, RustcEncodable, RustcDecodable)]
72 pub struct SerializedWorkProduct {
73 /// node that produced the work-product
74 pub id: Arc<WorkProductId>,
76 /// work-product data itself
77 pub work_product: WorkProduct,
80 /// Data for use when downstream crates get recompiled.
81 #[derive(Debug, RustcEncodable, RustcDecodable)]
82 pub struct SerializedMetadataHashes {
83 /// For each def-id defined in this crate that appears in the
84 /// metadata, we hash all the inputs that were used when producing
85 /// the metadata. We save this after compilation is done. Then,
86 /// when some downstream crate is being recompiled, it can compare
87 /// the hashes we saved against the hashes that it saw from
88 /// before; this will tell it which of the items in this crate
89 /// changed, which in turn implies what items in the downstream
90 /// crate need to be recompiled.
92 /// Note that we store the def-ids here. This is because we don't
93 /// reload this file when we recompile this crate, we will just
94 /// regenerate it completely with the current hashes and new def-ids.
96 /// Then downstream creates will load up their
97 /// `SerializedDepGraph`, which may contain `MetaData(X)` nodes
98 /// where `X` refers to some item in this crate. That `X` will be
99 /// a `DefPathIndex` that gets retracted to the current `DefId`
100 /// (matching the one found in this structure).
101 pub hashes: Vec<SerializedMetadataHash>,
103 /// For each DefIndex (as it occurs in SerializedMetadataHash), this
104 /// map stores the DefPathIndex (as it occurs in DefIdDirectory), so
105 /// that we can find the new DefId for a SerializedMetadataHash in a
106 /// subsequent compilation session.
108 /// This map is only needed for running auto-tests using the
109 /// #[rustc_metadata_dirty] and #[rustc_metadata_clean] attributes, and
110 /// is only populated if -Z query-dep-graph is specified. It will be
111 /// empty otherwise. Importing crates are perfectly happy with just having
113 pub index_map: FxHashMap<DefIndex, DefPathIndex>
116 /// The hash for some metadata that (when saving) will be exported
117 /// from this crate, or which (when importing) was exported by an
119 #[derive(Debug, RustcEncodable, RustcDecodable)]
120 pub struct SerializedMetadataHash {
121 pub def_index: DefIndex,
123 /// the hash itself, computed by `calculate_item_hash`
124 pub hash: Fingerprint,