Skip to content

Commit ff74a67

Browse files
committed
Revisions to borrowck::mir to make it easier to call from other contexts
(like (cough)trans(cough)...)
1 parent 23129ae commit ff74a67

File tree

3 files changed

+58
-42
lines changed

3 files changed

+58
-42
lines changed

src/librustc_borrowck/borrowck/mir/dataflow.rs

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -171,46 +171,21 @@ impl<'b, 'a: 'b, 'tcx: 'a, BD> MirBorrowckCtxtPreDataflow<'b, 'a, 'tcx, BD>
171171
BD::Bit: Debug
172172
{
173173
fn pre_dataflow_instrumentation(&self, context: &str) -> io::Result<()> {
174-
self.if_attr_meta_name_found(
175-
"borrowck_graphviz_preflow",
176-
|this, path: &str| {
177-
let c = format!("{}_preflow", context);
178-
graphviz::print_borrowck_graph_to(this, &c, path)
179-
})
174+
if let Some(ref path) = self.print_preflow_to {
175+
let c = format!("{}_preflow", context);
176+
graphviz::print_borrowck_graph_to(self, &c, path)
177+
} else {
178+
Ok(())
179+
}
180180
}
181181

182182
fn post_dataflow_instrumentation(&self, context: &str) -> io::Result<()> {
183-
self.if_attr_meta_name_found(
184-
"borrowck_graphviz_postflow",
185-
|this, path: &str| {
186-
let c = format!("{}_postflow", context);
187-
graphviz::print_borrowck_graph_to(this, &c, path)
188-
})
189-
}
190-
191-
fn if_attr_meta_name_found<F>(&self,
192-
name: &str,
193-
callback: F) -> io::Result<()>
194-
where F: for <'aa, 'bb> FnOnce(&'aa Self, &'bb str) -> io::Result<()>
195-
{
196-
for attr in self.attributes {
197-
if attr.check_name("rustc_mir") {
198-
let items = attr.meta_item_list();
199-
for item in items.iter().flat_map(|l| l.iter()) {
200-
if item.check_name(name) {
201-
if let Some(s) = item.value_str() {
202-
return callback(self, &s);
203-
} else {
204-
self.bcx.tcx.sess.span_err(
205-
item.span,
206-
&format!("{} attribute requires a path", item.name()));
207-
}
208-
}
209-
}
210-
}
183+
if let Some(ref path) = self.print_postflow_to {
184+
let c = format!("{}_postflow", context);
185+
graphviz::print_borrowck_graph_to(self, &c, path)
186+
} else {
187+
Ok(())
211188
}
212-
213-
Ok(())
214189
}
215190
}
216191

src/librustc_borrowck/borrowck/mir/mod.rs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_front::hir;
1717
use rustc_front::intravisit::{FnKind};
1818

1919
use rustc::mir::repr::{BasicBlock, BasicBlockData, Mir, Statement, Terminator};
20+
use rustc::session::Session;
2021

2122
mod abs_domain;
2223
mod dataflow;
@@ -30,6 +31,12 @@ use self::gather_moves::{MoveData};
3031

3132
use std::fmt::Debug;
3233

34+
pub struct BorrowckMirData<'tcx> {
35+
move_data: MoveData<'tcx>,
36+
flow_inits: DataflowState<MaybeInitializedLvals<'tcx>>,
37+
flow_uninits: DataflowState<MaybeUninitializedLvals<'tcx>>,
38+
}
39+
3340
pub fn borrowck_mir<'b, 'a: 'b, 'tcx: 'a>(
3441
bcx: &'b mut BorrowckCtxt<'a, 'tcx>,
3542
fk: FnKind,
@@ -38,7 +45,7 @@ pub fn borrowck_mir<'b, 'a: 'b, 'tcx: 'a>(
3845
body: &hir::Block,
3946
_sp: Span,
4047
id: ast::NodeId,
41-
attributes: &[ast::Attribute]) {
48+
attributes: &[ast::Attribute]) -> BorrowckMirData<'tcx> {
4249
match fk {
4350
FnKind::ItemFn(name, _, _, _, _, _, _) |
4451
FnKind::Method(name, _, _, _) => {
@@ -60,7 +67,6 @@ pub fn borrowck_mir<'b, 'a: 'b, 'tcx: 'a>(
6067
bcx: bcx,
6168
mir: mir,
6269
node_id: id,
63-
attributes: attributes,
6470
move_data: move_data,
6571
flow_inits: flow_inits,
6672
flow_uninits: flow_uninits,
@@ -72,6 +78,12 @@ pub fn borrowck_mir<'b, 'a: 'b, 'tcx: 'a>(
7278

7379
debug!("borrowck_mir done");
7480

81+
return BorrowckMirData {
82+
move_data: mbcx.move_data,
83+
flow_inits: mbcx.flow_inits,
84+
flow_uninits: mbcx.flow_uninits,
85+
};
86+
7587
fn do_dataflow<'a, 'tcx, BD>(bcx: &mut BorrowckCtxt<'a, 'tcx>,
7688
mir: &'a Mir<'tcx>,
7789
node_id: NodeId,
@@ -80,11 +92,40 @@ pub fn borrowck_mir<'b, 'a: 'b, 'tcx: 'a>(
8092
bd: BD) -> (MoveData<'tcx>, DataflowState<BD>)
8193
where BD: BitDenotation<Ctxt=MoveData<'tcx>>, BD::Bit: Debug
8294
{
95+
use syntax::attr::AttrMetaMethods;
96+
97+
let attr_meta_name_found = |sess: &Session, attributes: &[ast::Attribute], name| -> Option<String> {
98+
for attr in attributes {
99+
if attr.check_name("rustc_mir") {
100+
let items = attr.meta_item_list();
101+
for item in items.iter().flat_map(|l| l.iter()) {
102+
if item.check_name(name) {
103+
if let Some(s) = item.value_str() {
104+
return Some(s.to_string())
105+
} else {
106+
sess.span_err(
107+
item.span,
108+
&format!("{} attribute requires a path", item.name()));
109+
return None;
110+
}
111+
}
112+
}
113+
}
114+
}
115+
return None;
116+
};
117+
118+
let print_preflow_to =
119+
attr_meta_name_found(bcx.tcx.sess, attributes, "borrowck_graphviz_preflow");
120+
let print_postflow_to =
121+
attr_meta_name_found(bcx.tcx.sess, attributes, "borrowck_graphviz_postflow");
122+
83123
let mut mbcx = MirBorrowckCtxtPreDataflow {
84124
bcx: bcx,
85125
mir: mir,
86126
node_id: node_id,
87-
attributes: attributes,
127+
print_preflow_to: print_preflow_to,
128+
print_postflow_to: print_postflow_to,
88129
flow_state: DataflowStateBuilder::new(mir, move_data, bd),
89130
};
90131

@@ -99,15 +140,15 @@ pub struct MirBorrowckCtxtPreDataflow<'b, 'a: 'b, 'tcx: 'a, BD>
99140
bcx: &'b mut BorrowckCtxt<'a, 'tcx>,
100141
mir: &'b Mir<'tcx>,
101142
node_id: ast::NodeId,
102-
attributes: &'b [ast::Attribute],
143+
print_preflow_to: Option<String>,
144+
print_postflow_to: Option<String>,
103145
flow_state: DataflowStateBuilder<'a, 'tcx, BD>,
104146
}
105147

106148
pub struct MirBorrowckCtxt<'b, 'a: 'b, 'tcx: 'a> {
107149
bcx: &'b mut BorrowckCtxt<'a, 'tcx>,
108150
mir: &'b Mir<'tcx>,
109151
node_id: ast::NodeId,
110-
attributes: &'b [ast::Attribute],
111152
move_data: MoveData<'tcx>,
112153
flow_inits: DataflowState<MaybeInitializedLvals<'tcx>>,
113154
flow_uninits: DataflowState<MaybeUninitializedLvals<'tcx>>,

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ fn borrowck_fn(this: &mut BorrowckCtxt,
170170
if attributes.iter().any(|item| item.check_name("rustc_mir_borrowck")) {
171171
let mir = this.mir_map.unwrap().map.get(&id).unwrap();
172172
this.with_temp_region_map(id, |this| {
173-
mir::borrowck_mir(this, fk, decl, mir, body, sp, id, attributes)
173+
mir::borrowck_mir(this, fk, decl, mir, body, sp, id, attributes);
174174
});
175175
}
176176

0 commit comments

Comments
 (0)