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