|
| 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 | +/// For clarity, rename the graphviz crate locally to dot. |
| 16 | +use dot = graphviz; |
| 17 | +pub use middle::cfg::graphviz::{Node, Edge}; |
| 18 | +use cfg_dot = middle::cfg::graphviz; |
| 19 | + |
| 20 | +use middle::borrowck; |
| 21 | +use middle::borrowck::{BorrowckCtxt, LoanPath}; |
| 22 | +use middle::cfg::{CFGIndex}; |
| 23 | +use middle::dataflow::{DataFlowOperator, DataFlowContext, EntryOrExit}; |
| 24 | +use middle::dataflow; |
| 25 | + |
| 26 | +use std::rc::Rc; |
| 27 | +use std::str; |
| 28 | + |
| 29 | +#[deriving(Show)] |
| 30 | +pub enum Variant { |
| 31 | + Loans, |
| 32 | + Moves, |
| 33 | + Assigns, |
| 34 | +} |
| 35 | + |
| 36 | +impl Variant { |
| 37 | + pub fn short_name(&self) -> &'static str { |
| 38 | + match *self { |
| 39 | + Loans => "loans", |
| 40 | + Moves => "moves", |
| 41 | + Assigns => "assigns", |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +pub struct DataflowLabeller<'a> { |
| 47 | + pub inner: cfg_dot::LabelledCFG<'a>, |
| 48 | + pub variants: Vec<Variant>, |
| 49 | + pub borrowck_ctxt: &'a BorrowckCtxt<'a>, |
| 50 | + pub analysis_data: &'a borrowck::AnalysisData<'a>, |
| 51 | +} |
| 52 | + |
| 53 | +impl<'a> DataflowLabeller<'a> { |
| 54 | + fn dataflow_for(&self, e: EntryOrExit, n: &Node<'a>) -> String { |
| 55 | + let id = n.val1().data.id; |
| 56 | + debug!("dataflow_for({}, id={}) {}", e, id, self.variants); |
| 57 | + let mut sets = "".to_string(); |
| 58 | + let mut seen_one = false; |
| 59 | + for &variant in self.variants.iter() { |
| 60 | + if seen_one { sets.push_str(" "); } else { seen_one = true; } |
| 61 | + sets.push_str(variant.short_name()); |
| 62 | + sets.push_str(": "); |
| 63 | + sets.push_str(self.dataflow_for_variant(e, n, variant).as_slice()); |
| 64 | + } |
| 65 | + sets |
| 66 | + } |
| 67 | + |
| 68 | + fn dataflow_for_variant(&self, e: EntryOrExit, n: &Node, v: Variant) -> String { |
| 69 | + let cfgidx = n.val0(); |
| 70 | + match v { |
| 71 | + Loans => self.dataflow_loans_for(e, cfgidx), |
| 72 | + Moves => self.dataflow_moves_for(e, cfgidx), |
| 73 | + Assigns => self.dataflow_assigns_for(e, cfgidx), |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + fn build_set<O:DataFlowOperator>(&self, |
| 78 | + e: EntryOrExit, |
| 79 | + cfgidx: CFGIndex, |
| 80 | + dfcx: &DataFlowContext<'a, O>, |
| 81 | + to_lp: |uint| -> Rc<LoanPath>) -> String { |
| 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.as_slice()); |
| 91 | + saw_some = true; |
| 92 | + true |
| 93 | + }); |
| 94 | + set.append("}") |
| 95 | + } |
| 96 | + |
| 97 | + fn dataflow_loans_for(&self, e: EntryOrExit, cfgidx: CFGIndex) -> String { |
| 98 | + let dfcx = &self.analysis_data.loans; |
| 99 | + let loan_index_to_path = |loan_index| { |
| 100 | + let all_loans = &self.analysis_data.all_loans; |
| 101 | + all_loans.get(loan_index).loan_path() |
| 102 | + }; |
| 103 | + self.build_set(e, cfgidx, dfcx, loan_index_to_path) |
| 104 | + } |
| 105 | + |
| 106 | + fn dataflow_moves_for(&self, e: EntryOrExit, cfgidx: CFGIndex) -> String { |
| 107 | + let dfcx = &self.analysis_data.move_data.dfcx_moves; |
| 108 | + let move_index_to_path = |move_index| { |
| 109 | + let move_data = &self.analysis_data.move_data.move_data; |
| 110 | + let moves = move_data.moves.borrow(); |
| 111 | + let move = moves.get(move_index); |
| 112 | + move_data.path_loan_path(move.path) |
| 113 | + }; |
| 114 | + self.build_set(e, cfgidx, dfcx, move_index_to_path) |
| 115 | + } |
| 116 | + |
| 117 | + fn dataflow_assigns_for(&self, e: EntryOrExit, cfgidx: CFGIndex) -> String { |
| 118 | + let dfcx = &self.analysis_data.move_data.dfcx_assign; |
| 119 | + let assign_index_to_path = |assign_index| { |
| 120 | + let move_data = &self.analysis_data.move_data.move_data; |
| 121 | + let assignments = move_data.var_assignments.borrow(); |
| 122 | + let assignment = assignments.get(assign_index); |
| 123 | + move_data.path_loan_path(assignment.path) |
| 124 | + }; |
| 125 | + self.build_set(e, cfgidx, dfcx, assign_index_to_path) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +impl<'a> dot::Labeller<'a, Node<'a>, Edge<'a>> for DataflowLabeller<'a> { |
| 130 | + fn graph_id(&'a self) -> dot::Id<'a> { self.inner.graph_id() } |
| 131 | + fn node_id(&'a self, n: &Node<'a>) -> dot::Id<'a> { self.inner.node_id(n) } |
| 132 | + fn node_label(&'a self, n: &Node<'a>) -> dot::LabelText<'a> { |
| 133 | + let prefix = self.dataflow_for(dataflow::Entry, n); |
| 134 | + let suffix = self.dataflow_for(dataflow::Exit, n); |
| 135 | + let inner_label = self.inner.node_label(n); |
| 136 | + inner_label |
| 137 | + .prefix_line(dot::LabelStr(str::Owned(prefix))) |
| 138 | + .suffix_line(dot::LabelStr(str::Owned(suffix))) |
| 139 | + } |
| 140 | + fn edge_label(&'a self, e: &Edge<'a>) -> dot::LabelText<'a> { self.inner.edge_label(e) } |
| 141 | +} |
| 142 | + |
| 143 | +impl<'a> dot::GraphWalk<'a, Node<'a>, Edge<'a>> for DataflowLabeller<'a> { |
| 144 | + fn nodes(&self) -> dot::Nodes<'a, Node<'a>> { self.inner.nodes() } |
| 145 | + fn edges(&self) -> dot::Edges<'a, Edge<'a>> { self.inner.edges() } |
| 146 | + fn source(&self, edge: &Edge<'a>) -> Node<'a> { self.inner.source(edge) } |
| 147 | + fn target(&self, edge: &Edge<'a>) -> Node<'a> { self.inner.target(edge) } |
| 148 | +} |
0 commit comments