]> git.lizzy.rs Git - rust.git/blob - src/librustc_incremental/persist/preds/mod.rs
Fix invalid associated type rendering in rustdoc
[rust.git] / src / librustc_incremental / persist / preds / mod.rs
1 // Copyright 2012-2015 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 rustc::dep_graph::{DepGraphQuery, DepNode};
12 use rustc::hir::def_id::DefId;
13 use rustc::ich::Fingerprint;
14 use rustc_data_structures::fx::FxHashMap;
15 use rustc_data_structures::graph::{Graph, NodeIndex};
16
17 use super::hash::*;
18
19 mod compress;
20
21 /// A data-structure that makes it easy to enumerate the hashable
22 /// predecessors of any given dep-node.
23 pub struct Predecessors<'query> {
24     // A reduced version of the input graph that contains fewer nodes.
25     // This is intended to keep all of the base inputs (i.e., HIR
26     // nodes) and all of the "work-products" we may care about
27     // later. Other nodes may be retained if it keeps the overall size
28     // of the graph down.
29     pub reduced_graph: Graph<&'query DepNode<DefId>, ()>,
30
31     // These are output nodes that have no incoming edges. We have to
32     // track these specially because, when we load the data back up
33     // again, we want to make sure and recreate these nodes (we want
34     // to recreate the nodes where all incoming edges are clean; but
35     // since we ordinarily just serialize edges, we wind up just
36     // forgetting that bootstrap outputs even exist in that case.)
37     pub bootstrap_outputs: Vec<&'query DepNode<DefId>>,
38
39     // For the inputs (hir/foreign-metadata), we include hashes.
40     pub hashes: FxHashMap<&'query DepNode<DefId>, Fingerprint>,
41 }
42
43 impl<'q> Predecessors<'q> {
44     pub fn new(query: &'q DepGraphQuery<DefId>, hcx: &mut HashContext) -> Self {
45         let tcx = hcx.tcx;
46
47         let collect_for_metadata = tcx.sess.opts.debugging_opts.incremental_cc ||
48             tcx.sess.opts.debugging_opts.query_dep_graph;
49
50         // Find the set of "start nodes". These are nodes that we will
51         // possibly query later.
52         let is_output = |node: &DepNode<DefId>| -> bool {
53             match *node {
54                 DepNode::WorkProduct(_) => true,
55                 DepNode::MetaData(ref def_id) => collect_for_metadata && def_id.is_local(),
56
57                 // if -Z query-dep-graph is passed, save more extended data
58                 // to enable better unit testing
59                 DepNode::TypeckTables(_) |
60                 DepNode::TransCrateItem(_) => tcx.sess.opts.debugging_opts.query_dep_graph,
61
62                 _ => false,
63             }
64         };
65
66         // Reduce the graph to the most important nodes.
67         let compress::Reduction { graph, input_nodes } =
68             compress::reduce_graph(&query.graph, HashContext::is_hashable, |n| is_output(n));
69
70         let mut hashes = FxHashMap();
71         for input_index in input_nodes {
72             let input = *graph.node_data(input_index);
73             debug!("computing hash for input node `{:?}`", input);
74             hashes.entry(input)
75                   .or_insert_with(|| hcx.hash(input).unwrap());
76         }
77
78         let bootstrap_outputs: Vec<&'q DepNode<DefId>> =
79             (0 .. graph.len_nodes())
80             .map(NodeIndex)
81             .filter(|&n| graph.incoming_edges(n).next().is_none())
82             .map(|n| *graph.node_data(n))
83             .filter(|n| is_output(n))
84             .collect();
85
86         Predecessors {
87             reduced_graph: graph,
88             bootstrap_outputs: bootstrap_outputs,
89             hashes: hashes,
90         }
91     }
92 }