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::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 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 set of "reduced" dependency edge. We group the
49 /// outgoing edges from a single source together.
50 #[derive(Debug, RustcEncodable, RustcDecodable)]
51 pub struct SerializedEdgeSet {
52 pub source: DepNode<DefPathIndex>,
53 pub targets: Vec<DepNode<DefPathIndex>>
56 #[derive(Debug, RustcEncodable, RustcDecodable)]
57 pub struct SerializedHash {
58 /// def-id of thing being hashed
59 pub dep_node: DepNode<DefPathIndex>,
61 /// the hash as of previous compilation, computed by code in
63 pub hash: Fingerprint,
66 #[derive(Debug, RustcEncodable, RustcDecodable)]
67 pub struct SerializedWorkProduct {
68 /// node that produced the work-product
69 pub id: Arc<WorkProductId>,
71 /// work-product data itself
72 pub work_product: WorkProduct,
75 /// Data for use when downstream crates get recompiled.
76 #[derive(Debug, RustcEncodable, RustcDecodable)]
77 pub struct SerializedMetadataHashes {
78 /// For each def-id defined in this crate that appears in the
79 /// metadata, we hash all the inputs that were used when producing
80 /// the metadata. We save this after compilation is done. Then,
81 /// when some downstream crate is being recompiled, it can compare
82 /// the hashes we saved against the hashes that it saw from
83 /// before; this will tell it which of the items in this crate
84 /// changed, which in turn implies what items in the downstream
85 /// crate need to be recompiled.
87 /// Note that we store the def-ids here. This is because we don't
88 /// reload this file when we recompile this crate, we will just
89 /// regenerate it completely with the current hashes and new def-ids.
91 /// Then downstream creates will load up their
92 /// `SerializedDepGraph`, which may contain `MetaData(X)` nodes
93 /// where `X` refers to some item in this crate. That `X` will be
94 /// a `DefPathIndex` that gets retracted to the current `DefId`
95 /// (matching the one found in this structure).
96 pub hashes: Vec<SerializedMetadataHash>,
98 /// For each DefIndex (as it occurs in SerializedMetadataHash), this
99 /// map stores the DefPathIndex (as it occurs in DefIdDirectory), so
100 /// that we can find the new DefId for a SerializedMetadataHash in a
101 /// subsequent compilation session.
103 /// This map is only needed for running auto-tests using the
104 /// #[rustc_metadata_dirty] and #[rustc_metadata_clean] attributes, and
105 /// is only populated if -Z query-dep-graph is specified. It will be
106 /// empty otherwise. Importing crates are perfectly happy with just having
108 pub index_map: FxHashMap<DefIndex, DefPathIndex>
111 /// The hash for some metadata that (when saving) will be exported
112 /// from this crate, or which (when importing) was exported by an
114 #[derive(Debug, RustcEncodable, RustcDecodable)]
115 pub struct SerializedMetadataHash {
116 pub def_index: DefIndex,
118 /// the hash itself, computed by `calculate_item_hash`
119 pub hash: Fingerprint,