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 //! This module provides linkage between rustc::middle::graph and
12 //! libgraphviz traits, specialized to attaching borrowck analysis
13 //! data to rendered labels.
15 pub use self::Variant::*;
17 pub use rustc::cfg::graphviz::{Node, Edge};
18 use rustc::cfg::graphviz as cfg_dot;
21 use borrowck::{BorrowckCtxt, LoanPath};
23 use rustc::cfg::CFGIndex;
24 use dataflow::{DataFlowOperator, DataFlowContext, EntryOrExit};
27 #[derive(Debug, Copy, Clone)]
35 pub fn short_name(&self) -> &'static str {
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>,
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());
61 sets.push_str(&self.dataflow_for_variant(e, n, variant));
66 fn dataflow_for_variant(&self, e: EntryOrExit, n: &Node, v: Variant) -> String {
69 Loans => self.dataflow_loans_for(e, cfgidx),
70 Moves => self.dataflow_moves_for(e, cfgidx),
71 Assigns => self.dataflow_assigns_for(e, cfgidx),
75 fn build_set<O:DataFlowOperator, F>(&self,
78 dfcx: &DataFlowContext<'a, 'tcx, O>,
79 mut to_lp: F) -> String where
80 F: FnMut(usize) -> Rc<LoanPath<'tcx>>,
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);
89 let loan_str = self.borrowck_ctxt.loan_path_to_string(&lp);
90 set.push_str(&loan_str);
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];
105 self.build_set(e, cfgidx, dfcx, loan_index_to_path)
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)
116 self.build_set(e, cfgidx, dfcx, move_index_to_path)
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)
127 self.build_set(e, cfgidx, dfcx, assign_index_to_path)
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);
141 .prefix_line(dot::LabelText::LabelStr(prefix.into()))
142 .suffix_line(dot::LabelText::LabelStr(suffix.into()))
144 fn edge_label(&'a self, e: &Edge<'a>) -> dot::LabelText<'a> { self.inner.edge_label(e) }
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) }