]> git.lizzy.rs Git - rust.git/blob - src/librustc_borrowck/graphviz.rs
Rollup merge of #54824 - Munksgaard:fix-49713, r=QuietMisdreavus
[rust.git] / src / librustc_borrowck / graphviz.rs
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.
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 //! This module provides linkage between rustc::middle::graph and
12 //! libgraphviz traits, specialized to attaching borrowck analysis
13 //! data to rendered labels.
14
15 pub use self::Variant::*;
16
17 pub use rustc::cfg::graphviz::{Node, Edge};
18 use rustc::cfg::graphviz as cfg_dot;
19
20 use borrowck;
21 use borrowck::{BorrowckCtxt, LoanPath};
22 use dot;
23 use rustc::cfg::CFGIndex;
24 use dataflow::{DataFlowOperator, DataFlowContext, EntryOrExit};
25 use std::rc::Rc;
26
27 #[derive(Debug, Copy, Clone)]
28 pub enum Variant {
29     Loans,
30     Moves,
31     Assigns,
32 }
33
34 impl Variant {
35     pub fn short_name(&self) -> &'static str {
36         match *self {
37             Loans   => "loans",
38             Moves   => "moves",
39             Assigns => "assigns",
40         }
41     }
42 }
43
44 pub struct DataflowLabeller<'a, 'tcx: 'a> {
45     pub inner: cfg_dot::LabelledCFG<'a, 'tcx>,
46     pub variants: Vec<Variant>,
47     pub borrowck_ctxt: &'a BorrowckCtxt<'a, 'tcx>,
48     pub analysis_data: &'a borrowck::AnalysisData<'a, 'tcx>,
49 }
50
51 impl<'a, 'tcx> DataflowLabeller<'a, 'tcx> {
52     fn dataflow_for(&self, e: EntryOrExit, n: &Node<'a>) -> String {
53         let id = n.1.data.id();
54         debug!("dataflow_for({:?}, id={:?}) {:?}", e, id, self.variants);
55         let mut sets = String::new();
56         let mut seen_one = false;
57         for &variant in &self.variants {
58             if seen_one { sets.push_str(" "); } else { seen_one = true; }
59             sets.push_str(variant.short_name());
60             sets.push_str(": ");
61             sets.push_str(&self.dataflow_for_variant(e, n, variant));
62         }
63         sets
64     }
65
66     fn dataflow_for_variant(&self, e: EntryOrExit, n: &Node, v: Variant) -> String {
67         let cfgidx = n.0;
68         match v {
69             Loans   => self.dataflow_loans_for(e, cfgidx),
70             Moves   => self.dataflow_moves_for(e, cfgidx),
71             Assigns => self.dataflow_assigns_for(e, cfgidx),
72         }
73     }
74
75     fn build_set<O:DataFlowOperator, F>(&self,
76                                         e: EntryOrExit,
77                                         cfgidx: CFGIndex,
78                                         dfcx: &DataFlowContext<'a, 'tcx, O>,
79                                         mut to_lp: F) -> String where
80         F: FnMut(usize) -> Rc<LoanPath<'tcx>>,
81     {
82         let mut saw_some = false;
83         let mut set = "{".to_string();
84         dfcx.each_bit_for_node(e, cfgidx, |index| {
85             let lp = to_lp(index);
86             if saw_some {
87                 set.push_str(", ");
88             }
89             let loan_str = self.borrowck_ctxt.loan_path_to_string(&lp);
90             set.push_str(&loan_str);
91             saw_some = true;
92             true
93         });
94         set.push_str("}");
95         set
96     }
97
98     fn dataflow_loans_for(&self, e: EntryOrExit, cfgidx: CFGIndex) -> String {
99         let dfcx = &self.analysis_data.loans;
100         let loan_index_to_path = |loan_index| {
101             let all_loans = &self.analysis_data.all_loans;
102             let l: &borrowck::Loan = &all_loans[loan_index];
103             l.loan_path()
104         };
105         self.build_set(e, cfgidx, dfcx, loan_index_to_path)
106     }
107
108     fn dataflow_moves_for(&self, e: EntryOrExit, cfgidx: CFGIndex) -> String {
109         let dfcx = &self.analysis_data.move_data.dfcx_moves;
110         let move_index_to_path = |move_index| {
111             let move_data = &self.analysis_data.move_data.move_data;
112             let moves = move_data.moves.borrow();
113             let the_move: &borrowck::move_data::Move = &(*moves)[move_index];
114             move_data.path_loan_path(the_move.path)
115         };
116         self.build_set(e, cfgidx, dfcx, move_index_to_path)
117     }
118
119     fn dataflow_assigns_for(&self, e: EntryOrExit, cfgidx: CFGIndex) -> String {
120         let dfcx = &self.analysis_data.move_data.dfcx_assign;
121         let assign_index_to_path = |assign_index| {
122             let move_data = &self.analysis_data.move_data.move_data;
123             let assignments = move_data.var_assignments.borrow();
124             let assignment: &borrowck::move_data::Assignment = &(*assignments)[assign_index];
125             move_data.path_loan_path(assignment.path)
126         };
127         self.build_set(e, cfgidx, dfcx, assign_index_to_path)
128     }
129 }
130
131 impl<'a, 'tcx> dot::Labeller<'a> for DataflowLabeller<'a, 'tcx> {
132     type Node = Node<'a>;
133     type Edge = Edge<'a>;
134     fn graph_id(&'a self) -> dot::Id<'a> { self.inner.graph_id() }
135     fn node_id(&'a self, n: &Node<'a>) -> dot::Id<'a> { self.inner.node_id(n) }
136     fn node_label(&'a self, n: &Node<'a>) -> dot::LabelText<'a> {
137         let prefix = self.dataflow_for(EntryOrExit::Entry, n);
138         let suffix = self.dataflow_for(EntryOrExit::Exit, n);
139         let inner_label = self.inner.node_label(n);
140         inner_label
141             .prefix_line(dot::LabelText::LabelStr(prefix.into()))
142             .suffix_line(dot::LabelText::LabelStr(suffix.into()))
143     }
144     fn edge_label(&'a self, e: &Edge<'a>) -> dot::LabelText<'a> { self.inner.edge_label(e) }
145 }
146
147 impl<'a, 'tcx> dot::GraphWalk<'a> for DataflowLabeller<'a, 'tcx> {
148     type Node = Node<'a>;
149     type Edge = Edge<'a>;
150     fn nodes(&'a self) -> dot::Nodes<'a, Node<'a>> { self.inner.nodes() }
151     fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> { self.inner.edges() }
152     fn source(&'a self, edge: &Edge<'a>) -> Node<'a> { self.inner.source(edge) }
153     fn target(&'a self, edge: &Edge<'a>) -> Node<'a> { self.inner.target(edge) }
154 }