Skip to content

Commit e3af9fa

Browse files
arielb1Ariel Ben-Yehuda
authored andcommitted
make the basic_blocks field private
1 parent bc1eb67 commit e3af9fa

File tree

24 files changed

+203
-200
lines changed

24 files changed

+203
-200
lines changed

src/librustc/mir/repr.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ macro_rules! newtype_index {
5555
pub struct Mir<'tcx> {
5656
/// List of basic blocks. References to basic block use a newtyped index type `BasicBlock`
5757
/// that indexes into this vector.
58-
pub basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
58+
basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
5959

6060
/// List of visibility (lexical) scopes; these are referenced by statements
6161
/// and used (eventually) for debuginfo. Indexed by a `VisibilityScope`.
@@ -94,18 +94,37 @@ pub struct Mir<'tcx> {
9494
pub const START_BLOCK: BasicBlock = BasicBlock(0);
9595

9696
impl<'tcx> Mir<'tcx> {
97-
pub fn all_basic_blocks(&self) -> Vec<BasicBlock> {
98-
(0..self.basic_blocks.len())
99-
.map(|i| BasicBlock::new(i))
100-
.collect()
97+
pub fn new(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
98+
visibility_scopes: IndexVec<VisibilityScope, VisibilityScopeData>,
99+
promoted: IndexVec<Promoted, Mir<'tcx>>,
100+
return_ty: FnOutput<'tcx>,
101+
var_decls: IndexVec<Var, VarDecl<'tcx>>,
102+
arg_decls: IndexVec<Arg, ArgDecl<'tcx>>,
103+
temp_decls: IndexVec<Temp, TempDecl<'tcx>>,
104+
upvar_decls: Vec<UpvarDecl>,
105+
span: Span) -> Self
106+
{
107+
Mir {
108+
basic_blocks: basic_blocks,
109+
visibility_scopes: visibility_scopes,
110+
promoted: promoted,
111+
return_ty: return_ty,
112+
var_decls: var_decls,
113+
arg_decls: arg_decls,
114+
temp_decls: temp_decls,
115+
upvar_decls: upvar_decls,
116+
span: span
117+
}
101118
}
102119

103-
pub fn basic_block_data(&self, bb: BasicBlock) -> &BasicBlockData<'tcx> {
104-
&self.basic_blocks[bb]
120+
#[inline]
121+
pub fn basic_blocks(&self) -> &IndexVec<BasicBlock, BasicBlockData<'tcx>> {
122+
&self.basic_blocks
105123
}
106124

107-
pub fn basic_block_data_mut(&mut self, bb: BasicBlock) -> &mut BasicBlockData<'tcx> {
108-
&mut self.basic_blocks[bb]
125+
#[inline]
126+
pub fn basic_blocks_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
127+
&mut self.basic_blocks
109128
}
110129
}
111130

@@ -114,14 +133,14 @@ impl<'tcx> Index<BasicBlock> for Mir<'tcx> {
114133

115134
#[inline]
116135
fn index(&self, index: BasicBlock) -> &BasicBlockData<'tcx> {
117-
self.basic_block_data(index)
136+
&self.basic_blocks()[index]
118137
}
119138
}
120139

121140
impl<'tcx> IndexMut<BasicBlock> for Mir<'tcx> {
122141
#[inline]
123142
fn index_mut(&mut self, index: BasicBlock) -> &mut BasicBlockData<'tcx> {
124-
self.basic_block_data_mut(index)
143+
&mut self.basic_blocks_mut()[index]
125144
}
126145
}
127146

src/librustc/mir/traversal.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl<'a, 'tcx> Preorder<'a, 'tcx> {
4545

4646
Preorder {
4747
mir: mir,
48-
visited: BitVector::new(mir.basic_blocks.len()),
48+
visited: BitVector::new(mir.basic_blocks().len()),
4949
worklist: worklist
5050
}
5151
}
@@ -64,7 +64,7 @@ impl<'a, 'tcx> Iterator for Preorder<'a, 'tcx> {
6464
continue;
6565
}
6666

67-
let data = self.mir.basic_block_data(idx);
67+
let data = &self.mir[idx];
6868

6969
if let Some(ref term) = data.terminator {
7070
for &succ in term.successors().iter() {
@@ -107,12 +107,12 @@ impl<'a, 'tcx> Postorder<'a, 'tcx> {
107107
pub fn new(mir: &'a Mir<'tcx>, root: BasicBlock) -> Postorder<'a, 'tcx> {
108108
let mut po = Postorder {
109109
mir: mir,
110-
visited: BitVector::new(mir.basic_blocks.len()),
110+
visited: BitVector::new(mir.basic_blocks().len()),
111111
visit_stack: Vec::new()
112112
};
113113

114114

115-
let data = po.mir.basic_block_data(root);
115+
let data = &po.mir[root];
116116

117117
if let Some(ref term) = data.terminator {
118118
po.visited.insert(root.index());
@@ -186,9 +186,7 @@ impl<'a, 'tcx> Postorder<'a, 'tcx> {
186186
};
187187

188188
if self.visited.insert(bb.index()) {
189-
let data = self.mir.basic_block_data(bb);
190-
191-
if let Some(ref term) = data.terminator {
189+
if let Some(ref term) = self.mir[bb].terminator {
192190
let succs = term.successors().into_owned().into_iter();
193191
self.visit_stack.push((bb, succs));
194192
}
@@ -210,10 +208,7 @@ impl<'a, 'tcx> Iterator for Postorder<'a, 'tcx> {
210208
self.traverse_successor();
211209
}
212210

213-
next.map(|(bb, _)| {
214-
let data = self.mir.basic_block_data(bb);
215-
(bb, data)
216-
})
211+
next.map(|(bb, _)| (bb, &self.mir[bb]))
217212
}
218213
}
219214

@@ -279,9 +274,6 @@ impl<'a, 'tcx> Iterator for ReversePostorder<'a, 'tcx> {
279274
if self.idx == 0 { return None; }
280275
self.idx -= 1;
281276

282-
self.blocks.get(self.idx).map(|&bb| {
283-
let data = self.mir.basic_block_data(bb);
284-
(bb, data)
285-
})
277+
self.blocks.get(self.idx).map(|&bb| (bb, &self.mir[bb]))
286278
}
287279
}

src/librustc/mir/visit.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -252,42 +252,30 @@ macro_rules! make_mir_visitor {
252252

253253
fn super_mir(&mut self,
254254
mir: & $($mutability)* Mir<'tcx>) {
255-
let Mir {
256-
ref $($mutability)* basic_blocks,
257-
ref $($mutability)* visibility_scopes,
258-
promoted: _, // Visited by passes separately.
259-
ref $($mutability)* return_ty,
260-
ref $($mutability)* var_decls,
261-
ref $($mutability)* arg_decls,
262-
ref $($mutability)* temp_decls,
263-
upvar_decls: _,
264-
ref $($mutability)* span,
265-
} = *mir;
266-
267-
for (index, data) in basic_blocks.into_iter().enumerate() {
255+
for index in 0..mir.basic_blocks().len() {
268256
let block = BasicBlock::new(index);
269-
self.visit_basic_block_data(block, data);
257+
self.visit_basic_block_data(block, &$($mutability)* mir[block]);
270258
}
271259

272-
for scope in visibility_scopes {
260+
for scope in &$($mutability)* mir.visibility_scopes {
273261
self.visit_visibility_scope_data(scope);
274262
}
275263

276-
self.visit_fn_output(return_ty);
264+
self.visit_fn_output(&$($mutability)* mir.return_ty);
277265

278-
for var_decl in var_decls {
266+
for var_decl in &$($mutability)* mir.var_decls {
279267
self.visit_var_decl(var_decl);
280268
}
281269

282-
for arg_decl in arg_decls {
270+
for arg_decl in &$($mutability)* mir.arg_decls {
283271
self.visit_arg_decl(arg_decl);
284272
}
285273

286-
for temp_decl in temp_decls {
274+
for temp_decl in &$($mutability)* mir.temp_decls {
287275
self.visit_temp_decl(temp_decl);
288276
}
289277

290-
self.visit_span(span);
278+
self.visit_span(&$($mutability)* mir.span);
291279
}
292280

293281
fn super_basic_block_data(&mut self,

src/librustc_borrowck/borrowck/mir/dataflow/graphviz.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub type Node = BasicBlock;
127127
pub struct Edge { source: BasicBlock, index: usize }
128128

129129
fn outgoing(mir: &Mir, bb: BasicBlock) -> Vec<Edge> {
130-
let succ_len = mir.basic_block_data(bb).terminator().successors().len();
130+
let succ_len = mir[bb].terminator().successors().len();
131131
(0..succ_len).map(|index| Edge { source: bb, index: index}).collect()
132132
}
133133

@@ -313,17 +313,20 @@ impl<'a, 'tcx, MWF, P> dot::GraphWalk<'a> for Graph<'a, 'tcx, MWF, P>
313313
type Node = Node;
314314
type Edge = Edge;
315315
fn nodes(&self) -> dot::Nodes<Node> {
316-
self.mbcx.mir().all_basic_blocks().into_cow()
316+
self.mbcx.mir()
317+
.basic_blocks()
318+
.indices()
319+
.collect::<Vec<_>>()
320+
.into_cow()
317321
}
318322

319323
fn edges(&self) -> dot::Edges<Edge> {
320324
let mir = self.mbcx.mir();
321-
let blocks = mir.all_basic_blocks();
322325
// base initial capacity on assumption every block has at
323326
// least one outgoing edge (Which should be true for all
324327
// blocks but one, the exit-block).
325-
let mut edges = Vec::with_capacity(blocks.len());
326-
for bb in blocks {
328+
let mut edges = Vec::with_capacity(mir.basic_blocks().len());
329+
for bb in mir.basic_blocks().indices() {
327330
let outgoing = outgoing(mir, bb);
328331
edges.extend(outgoing.into_iter());
329332
}
@@ -336,6 +339,6 @@ impl<'a, 'tcx, MWF, P> dot::GraphWalk<'a> for Graph<'a, 'tcx, MWF, P>
336339

337340
fn target(&self, edge: &Edge) -> Node {
338341
let mir = self.mbcx.mir();
339-
mir.basic_block_data(edge.source).terminator().successors()[edge.index]
342+
mir[edge.source].terminator().successors()[edge.index]
340343
}
341344
}

src/librustc_borrowck/borrowck/mir/dataflow/impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ impl<'a, 'tcx> BitDenotation for MovingOutStatements<'a, 'tcx> {
426426
bb: repr::BasicBlock,
427427
idx: usize) {
428428
let (tcx, mir, move_data) = (self.tcx, self.mir, &ctxt.move_data);
429-
let stmt = &mir.basic_block_data(bb).statements[idx];
429+
let stmt = &mir[bb].statements[idx];
430430
let loc_map = &move_data.loc_map;
431431
let path_map = &move_data.path_map;
432432
let rev_lookup = &move_data.rev_lookup;
@@ -466,7 +466,7 @@ impl<'a, 'tcx> BitDenotation for MovingOutStatements<'a, 'tcx> {
466466
statements_len: usize)
467467
{
468468
let (mir, move_data) = (self.mir, &ctxt.move_data);
469-
let term = mir.basic_block_data(bb).terminator.as_ref().unwrap();
469+
let term = mir[bb].terminator();
470470
let loc_map = &move_data.loc_map;
471471
let loc = Location { block: bb, index: statements_len };
472472
debug!("terminator {:?} at loc {:?} moves out of move_indexes {:?}",

src/librustc_borrowck/borrowck/mir/dataflow/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,10 @@ impl<'a, 'tcx: 'a, BD> DataflowAnalysis<'a, 'tcx, BD>
8383
self.flow_state.operator.start_block_effect(&self.ctxt, sets);
8484
}
8585

86-
for bb in self.mir.all_basic_blocks() {
86+
for (bb, data) in self.mir.basic_blocks().iter_enumerated() {
8787
let &repr::BasicBlockData { ref statements,
8888
ref terminator,
89-
is_cleanup: _ } =
90-
self.mir.basic_block_data(bb);
89+
is_cleanup: _ } = data;
9190

9291
let sets = &mut self.flow_state.sets.for_block(bb.index());
9392
for j_stmt in 0..statements.len() {
@@ -114,7 +113,7 @@ impl<'b, 'a: 'b, 'tcx: 'a, BD> PropagationContext<'b, 'a, 'tcx, BD>
114113

115114
fn walk_cfg(&mut self, in_out: &mut IdxSet<BD::Idx>) {
116115
let mir = self.builder.mir;
117-
for (bb_idx, bb_data) in mir.basic_blocks.iter().enumerate() {
116+
for (bb_idx, bb_data) in mir.basic_blocks().iter().enumerate() {
118117
let builder = &mut self.builder;
119118
{
120119
let sets = builder.flow_state.sets.for_block(bb_idx);
@@ -398,7 +397,7 @@ impl<'a, 'tcx: 'a, D> DataflowAnalysis<'a, 'tcx, D>
398397
// (now rounded up to multiple of word size)
399398
let bits_per_block = words_per_block * usize_bits;
400399

401-
let num_blocks = mir.basic_blocks.len();
400+
let num_blocks = mir.basic_blocks().len();
402401
let num_overall = num_blocks * bits_per_block;
403402

404403
let zeroes = Bits::new(IdxSetBuf::new_empty(num_overall));

src/librustc_borrowck/borrowck/mir/dataflow/sanity_check.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ pub fn sanity_check_via_rustc_peek<'a, 'tcx, O>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
5050
// `dataflow::build_sets`. (But note it is doing non-standard
5151
// stuff, so such generalization may not be realistic.)
5252

53-
let blocks = mir.all_basic_blocks();
54-
'next_block: for bb in blocks {
53+
for bb in mir.basic_blocks().indices() {
5554
each_block(tcx, mir, flow_ctxt, results, bb);
5655
}
5756
}
@@ -64,10 +63,9 @@ fn each_block<'a, 'tcx, O>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
6463
O: BitDenotation<Ctxt=MoveDataParamEnv<'tcx>, Idx=MovePathIndex>
6564
{
6665
let move_data = &ctxt.move_data;
67-
let bb_data = mir.basic_block_data(bb);
68-
let &repr::BasicBlockData { ref statements,
69-
ref terminator,
70-
is_cleanup: _ } = bb_data;
66+
let repr::BasicBlockData { ref statements,
67+
ref terminator,
68+
is_cleanup: _ } = mir[bb];
7169

7270
let (args, span) = match is_rustc_peek(tcx, terminator) {
7371
Some(args_and_span) => args_and_span,

src/librustc_borrowck/borrowck/mir/elaborate_drops.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
225225

226226
fn collect_drop_flags(&mut self)
227227
{
228-
for bb in self.mir.all_basic_blocks() {
229-
let data = self.mir.basic_block_data(bb);
228+
for (bb, data) in self.mir.basic_blocks().iter_enumerated() {
230229
let terminator = data.terminator();
231230
let location = match terminator.kind {
232231
TerminatorKind::Drop { ref location, .. } |
@@ -262,8 +261,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
262261

263262
fn elaborate_drops(&mut self)
264263
{
265-
for bb in self.mir.all_basic_blocks() {
266-
let data = self.mir.basic_block_data(bb);
264+
for (bb, data) in self.mir.basic_blocks().iter_enumerated() {
267265
let loc = Location { block: bb, index: data.statements.len() };
268266
let terminator = data.terminator();
269267

@@ -323,7 +321,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
323321
unwind: Option<BasicBlock>)
324322
{
325323
let bb = loc.block;
326-
let data = self.mir.basic_block_data(bb);
324+
let data = &self.mir[bb];
327325
let terminator = data.terminator();
328326

329327
let assign = Statement {
@@ -942,8 +940,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
942940
}
943941

944942
fn drop_flags_for_fn_rets(&mut self) {
945-
for bb in self.mir.all_basic_blocks() {
946-
let data = self.mir.basic_block_data(bb);
943+
for (bb, data) in self.mir.basic_blocks().iter_enumerated() {
947944
if let TerminatorKind::Call {
948945
destination: Some((ref lv, tgt)), cleanup: Some(_), ..
949946
} = data.terminator().kind {
@@ -975,8 +972,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
975972
// drop flags by themselves, to avoid the drop flags being
976973
// clobbered before they are read.
977974

978-
for bb in self.mir.all_basic_blocks() {
979-
let data = self.mir.basic_block_data(bb);
975+
for (bb, data) in self.mir.basic_blocks().iter_enumerated() {
980976
debug!("drop_flags_for_locs({:?})", data);
981977
for i in 0..(data.statements.len()+1) {
982978
debug!("drop_flag_for_locs: stmt {}", i);

src/librustc_borrowck/borrowck/mir/gather_moves.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,9 @@ enum StmtKind {
519519
fn gather_moves<'a, 'tcx>(mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> MoveData<'tcx> {
520520
use self::StmtKind as SK;
521521

522-
let bbs = mir.all_basic_blocks();
523-
let mut moves = Vec::with_capacity(bbs.len());
524-
let mut loc_map: Vec<_> = iter::repeat(Vec::new()).take(bbs.len()).collect();
522+
let bb_count = mir.basic_blocks().len();
523+
let mut moves = vec![];
524+
let mut loc_map: Vec<_> = iter::repeat(Vec::new()).take(bb_count).collect();
525525
let mut path_map = Vec::new();
526526

527527
// this is mutable only because we will move it to and fro' the
@@ -541,22 +541,21 @@ fn gather_moves<'a, 'tcx>(mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> MoveD
541541
assert!(mir.var_decls.len() <= ::std::u32::MAX as usize);
542542
assert!(mir.arg_decls.len() <= ::std::u32::MAX as usize);
543543
assert!(mir.temp_decls.len() <= ::std::u32::MAX as usize);
544-
for (var, _) in mir.var_decls.iter_enumerated() {
544+
for var in mir.var_decls.indices() {
545545
let path_idx = builder.move_path_for(&Lvalue::Var(var));
546546
path_map.fill_to(path_idx.index());
547547
}
548-
for (arg, _) in mir.arg_decls.iter_enumerated() {
548+
for arg in mir.arg_decls.indices() {
549549
let path_idx = builder.move_path_for(&Lvalue::Arg(arg));
550550
path_map.fill_to(path_idx.index());
551551
}
552-
for (temp, _) in mir.temp_decls.iter_enumerated() {
552+
for temp in mir.temp_decls.indices() {
553553
let path_idx = builder.move_path_for(&Lvalue::Temp(temp));
554554
path_map.fill_to(path_idx.index());
555555
}
556556

557-
for bb in bbs {
557+
for (bb, bb_data) in mir.basic_blocks().iter_enumerated() {
558558
let loc_map_bb = &mut loc_map[bb.index()];
559-
let bb_data = mir.basic_block_data(bb);
560559

561560
debug_assert!(loc_map_bb.len() == 0);
562561
let len = bb_data.statements.len();

0 commit comments

Comments
 (0)