2014-07-02 10:50:18 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! This module provides linkage between rustc::middle::graph and
|
|
|
|
//! libgraphviz traits, specialized to attaching borrowck analysis
|
|
|
|
//! data to rendered labels.
|
|
|
|
|
2014-11-06 02:05:53 -06:00
|
|
|
pub use self::Variant::*;
|
|
|
|
|
2014-12-05 13:17:35 -06:00
|
|
|
pub use rustc::middle::cfg::graphviz::{Node, Edge};
|
|
|
|
use rustc::middle::cfg::graphviz as cfg_dot;
|
|
|
|
|
|
|
|
use borrowck;
|
|
|
|
use borrowck::{BorrowckCtxt, LoanPath};
|
|
|
|
use dot;
|
|
|
|
use rustc::middle::cfg::{CFGIndex};
|
|
|
|
use rustc::middle::dataflow::{DataFlowOperator, DataFlowContext, EntryOrExit};
|
|
|
|
use rustc::middle::dataflow;
|
2014-07-02 10:50:18 -05:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
#[deriving(Show)]
|
|
|
|
pub enum Variant {
|
|
|
|
Loans,
|
|
|
|
Moves,
|
|
|
|
Assigns,
|
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for Variant {}
|
|
|
|
|
2014-07-02 10:50:18 -05:00
|
|
|
impl Variant {
|
|
|
|
pub fn short_name(&self) -> &'static str {
|
|
|
|
match *self {
|
|
|
|
Loans => "loans",
|
|
|
|
Moves => "moves",
|
|
|
|
Assigns => "assigns",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
pub struct DataflowLabeller<'a, 'tcx: 'a> {
|
2014-09-07 12:09:06 -05:00
|
|
|
pub inner: cfg_dot::LabelledCFG<'a, 'tcx>,
|
2014-07-02 10:50:18 -05:00
|
|
|
pub variants: Vec<Variant>,
|
2014-04-22 07:56:37 -05:00
|
|
|
pub borrowck_ctxt: &'a BorrowckCtxt<'a, 'tcx>,
|
|
|
|
pub analysis_data: &'a borrowck::AnalysisData<'a, 'tcx>,
|
2014-07-02 10:50:18 -05:00
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
impl<'a, 'tcx> DataflowLabeller<'a, 'tcx> {
|
2014-07-02 10:50:18 -05:00
|
|
|
fn dataflow_for(&self, e: EntryOrExit, n: &Node<'a>) -> String {
|
|
|
|
let id = n.val1().data.id;
|
|
|
|
debug!("dataflow_for({}, id={}) {}", e, id, self.variants);
|
|
|
|
let mut sets = "".to_string();
|
|
|
|
let mut seen_one = false;
|
|
|
|
for &variant in self.variants.iter() {
|
|
|
|
if seen_one { sets.push_str(" "); } else { seen_one = true; }
|
|
|
|
sets.push_str(variant.short_name());
|
|
|
|
sets.push_str(": ");
|
|
|
|
sets.push_str(self.dataflow_for_variant(e, n, variant).as_slice());
|
|
|
|
}
|
|
|
|
sets
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dataflow_for_variant(&self, e: EntryOrExit, n: &Node, v: Variant) -> String {
|
|
|
|
let cfgidx = n.val0();
|
|
|
|
match v {
|
|
|
|
Loans => self.dataflow_loans_for(e, cfgidx),
|
|
|
|
Moves => self.dataflow_moves_for(e, cfgidx),
|
|
|
|
Assigns => self.dataflow_assigns_for(e, cfgidx),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_set<O:DataFlowOperator>(&self,
|
|
|
|
e: EntryOrExit,
|
|
|
|
cfgidx: CFGIndex,
|
2014-04-22 07:56:37 -05:00
|
|
|
dfcx: &DataFlowContext<'a, 'tcx, O>,
|
2014-09-18 00:58:26 -05:00
|
|
|
to_lp: |uint| -> Rc<LoanPath<'tcx>>) -> String {
|
2014-07-02 10:50:18 -05:00
|
|
|
let mut saw_some = false;
|
|
|
|
let mut set = "{".to_string();
|
|
|
|
dfcx.each_bit_for_node(e, cfgidx, |index| {
|
|
|
|
let lp = to_lp(index);
|
|
|
|
if saw_some {
|
|
|
|
set.push_str(", ");
|
|
|
|
}
|
|
|
|
let loan_str = self.borrowck_ctxt.loan_path_to_string(&*lp);
|
|
|
|
set.push_str(loan_str.as_slice());
|
|
|
|
saw_some = true;
|
|
|
|
true
|
|
|
|
});
|
2014-10-15 01:05:01 -05:00
|
|
|
set.push_str("}");
|
|
|
|
set
|
2014-07-02 10:50:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn dataflow_loans_for(&self, e: EntryOrExit, cfgidx: CFGIndex) -> String {
|
|
|
|
let dfcx = &self.analysis_data.loans;
|
|
|
|
let loan_index_to_path = |loan_index| {
|
|
|
|
let all_loans = &self.analysis_data.all_loans;
|
2014-10-15 01:05:01 -05:00
|
|
|
all_loans[loan_index].loan_path()
|
2014-07-02 10:50:18 -05:00
|
|
|
};
|
|
|
|
self.build_set(e, cfgidx, dfcx, loan_index_to_path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dataflow_moves_for(&self, e: EntryOrExit, cfgidx: CFGIndex) -> String {
|
|
|
|
let dfcx = &self.analysis_data.move_data.dfcx_moves;
|
|
|
|
let move_index_to_path = |move_index| {
|
|
|
|
let move_data = &self.analysis_data.move_data.move_data;
|
|
|
|
let moves = move_data.moves.borrow();
|
2014-10-15 01:05:01 -05:00
|
|
|
let the_move = &(*moves)[move_index];
|
2014-09-24 12:58:53 -05:00
|
|
|
move_data.path_loan_path(the_move.path)
|
2014-07-02 10:50:18 -05:00
|
|
|
};
|
|
|
|
self.build_set(e, cfgidx, dfcx, move_index_to_path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dataflow_assigns_for(&self, e: EntryOrExit, cfgidx: CFGIndex) -> String {
|
|
|
|
let dfcx = &self.analysis_data.move_data.dfcx_assign;
|
|
|
|
let assign_index_to_path = |assign_index| {
|
|
|
|
let move_data = &self.analysis_data.move_data.move_data;
|
|
|
|
let assignments = move_data.var_assignments.borrow();
|
2014-10-15 01:05:01 -05:00
|
|
|
let assignment = &(*assignments)[assign_index];
|
2014-07-02 10:50:18 -05:00
|
|
|
move_data.path_loan_path(assignment.path)
|
|
|
|
};
|
|
|
|
self.build_set(e, cfgidx, dfcx, assign_index_to_path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
impl<'a, 'tcx> dot::Labeller<'a, Node<'a>, Edge<'a>> for DataflowLabeller<'a, 'tcx> {
|
2014-07-02 10:50:18 -05:00
|
|
|
fn graph_id(&'a self) -> dot::Id<'a> { self.inner.graph_id() }
|
|
|
|
fn node_id(&'a self, n: &Node<'a>) -> dot::Id<'a> { self.inner.node_id(n) }
|
|
|
|
fn node_label(&'a self, n: &Node<'a>) -> dot::LabelText<'a> {
|
|
|
|
let prefix = self.dataflow_for(dataflow::Entry, n);
|
|
|
|
let suffix = self.dataflow_for(dataflow::Exit, n);
|
|
|
|
let inner_label = self.inner.node_label(n);
|
|
|
|
inner_label
|
2014-11-21 16:10:42 -06:00
|
|
|
.prefix_line(dot::LabelStr(prefix.into_cow()))
|
|
|
|
.suffix_line(dot::LabelStr(suffix.into_cow()))
|
2014-07-02 10:50:18 -05:00
|
|
|
}
|
|
|
|
fn edge_label(&'a self, e: &Edge<'a>) -> dot::LabelText<'a> { self.inner.edge_label(e) }
|
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
impl<'a, 'tcx> dot::GraphWalk<'a, Node<'a>, Edge<'a>> for DataflowLabeller<'a, 'tcx> {
|
2014-10-17 08:11:10 -05:00
|
|
|
fn nodes(&'a self) -> dot::Nodes<'a, Node<'a>> { self.inner.nodes() }
|
|
|
|
fn edges(&'a self) -> dot::Edges<'a, Edge<'a>> { self.inner.edges() }
|
|
|
|
fn source(&'a self, edge: &Edge<'a>) -> Node<'a> { self.inner.source(edge) }
|
|
|
|
fn target(&'a self, edge: &Edge<'a>) -> Node<'a> { self.inner.target(edge) }
|
2014-07-02 10:50:18 -05:00
|
|
|
}
|