Skip to content

Commit 4666fec

Browse files
committed
make the basic_blocks field private
1 parent a47f179 commit 4666fec

File tree

24 files changed

+202
-201
lines changed

24 files changed

+202
-201
lines changed

src/librustc/mir/repr.rs

+30-11
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: IdxVec<BasicBlock, BasicBlockData<'tcx>>,
58+
basic_blocks: IdxVec<BasicBlock, BasicBlockData<'tcx>>,
5959

6060
/// List of lexical scopes; these are referenced by statements and
6161
/// used (eventually) for debuginfo. Indexed by a `ScopeId`.
@@ -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: IdxVec<BasicBlock, BasicBlockData<'tcx>>,
98+
scopes: IdxVec<ScopeId, ScopeData>,
99+
promoted: IdxVec<Promoted, Mir<'tcx>>,
100+
return_ty: FnOutput<'tcx>,
101+
var_decls: IdxVec<Var, VarDecl<'tcx>>,
102+
arg_decls: IdxVec<Arg, ArgDecl<'tcx>>,
103+
temp_decls: IdxVec<Temp, TempDecl<'tcx>>,
104+
upvar_decls: Vec<UpvarDecl>,
105+
span: Span) -> Self
106+
{
107+
Mir {
108+
basic_blocks: basic_blocks,
109+
scopes: 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) -> &IdxVec<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 IdxVec<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/visit.rs

+8-20
Original file line numberDiff line numberDiff line change
@@ -247,42 +247,30 @@ macro_rules! make_mir_visitor {
247247

248248
fn super_mir(&mut self,
249249
mir: & $($mutability)* Mir<'tcx>) {
250-
let Mir {
251-
ref $($mutability)* basic_blocks,
252-
ref $($mutability)* scopes,
253-
promoted: _, // Visited by passes separately.
254-
ref $($mutability)* return_ty,
255-
ref $($mutability)* var_decls,
256-
ref $($mutability)* arg_decls,
257-
ref $($mutability)* temp_decls,
258-
upvar_decls: _,
259-
ref $($mutability)* span,
260-
} = *mir;
261-
262-
for (index, data) in basic_blocks.into_iter().enumerate() {
250+
for index in 0..mir.basic_blocks().len() {
263251
let block = BasicBlock::new(index);
264-
self.visit_basic_block_data(block, data);
252+
self.visit_basic_block_data(block, &$($mutability)* mir[block]);
265253
}
266254

267-
for scope in scopes {
255+
for scope in &$($mutability)* mir.scopes {
268256
self.visit_scope_data(scope);
269257
}
270258

271-
self.visit_fn_output(return_ty);
259+
self.visit_fn_output(&$($mutability)* mir.return_ty);
272260

273-
for var_decl in var_decls {
261+
for var_decl in &$($mutability)* mir.var_decls {
274262
self.visit_var_decl(var_decl);
275263
}
276264

277-
for arg_decl in arg_decls {
265+
for arg_decl in &$($mutability)* mir.arg_decls {
278266
self.visit_arg_decl(arg_decl);
279267
}
280268

281-
for temp_decl in temp_decls {
269+
for temp_decl in &$($mutability)* mir.temp_decls {
282270
self.visit_temp_decl(temp_decl);
283271
}
284272

285-
self.visit_span(span);
273+
self.visit_span(&$($mutability)* mir.span);
286274
}
287275

288276
fn super_basic_block_data(&mut self,

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

+9-6
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

+2-2
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

+4-5
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

+4-6
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

+5-9
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
226226

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

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

@@ -325,7 +323,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
325323
unwind: Option<BasicBlock>)
326324
{
327325
let bb = loc.block;
328-
let data = self.mir.basic_block_data(bb);
326+
let data = &self.mir[bb];
329327
let terminator = data.terminator();
330328

331329
let assign = Statement {
@@ -949,8 +947,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
949947
}
950948

951949
fn drop_flags_for_fn_rets(&mut self) {
952-
for bb in self.mir.all_basic_blocks() {
953-
let data = self.mir.basic_block_data(bb);
950+
for (bb, data) in self.mir.basic_blocks().iter_enumerated() {
954951
if let TerminatorKind::Call {
955952
destination: Some((ref lv, tgt)), cleanup: Some(_), ..
956953
} = data.terminator().kind {
@@ -982,8 +979,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
982979
// drop flags by themselves, to avoid the drop flags being
983980
// clobbered before they are read.
984981

985-
for bb in self.mir.all_basic_blocks() {
986-
let data = self.mir.basic_block_data(bb);
982+
for (bb, data) in self.mir.basic_blocks().iter_enumerated() {
987983
debug!("drop_flags_for_locs({:?})", data);
988984
for i in 0..(data.statements.len()+1) {
989985
debug!("drop_flag_for_locs: stmt {}", i);

src/librustc_borrowck/borrowck/mir/gather_moves.rs

+7-8
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();

src/librustc_borrowck/borrowck/mir/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pub fn borrowck_mir<'a, 'tcx: 'a>(
111111
flow_uninits: flow_uninits,
112112
};
113113

114-
for bb in mir.all_basic_blocks() {
114+
for bb in mir.basic_blocks().indices() {
115115
mbcx.process_basic_block(bb);
116116
}
117117

@@ -180,8 +180,8 @@ pub struct MirBorrowckCtxt<'b, 'a: 'b, 'tcx: 'a> {
180180

181181
impl<'b, 'a: 'b, 'tcx: 'a> MirBorrowckCtxt<'b, 'a, 'tcx> {
182182
fn process_basic_block(&mut self, bb: BasicBlock) {
183-
let &BasicBlockData { ref statements, ref terminator, is_cleanup: _ } =
184-
self.mir.basic_block_data(bb);
183+
let BasicBlockData { ref statements, ref terminator, is_cleanup: _ } =
184+
self.mir[bb];
185185
for stmt in statements {
186186
self.process_statement(bb, stmt);
187187
}
@@ -337,8 +337,8 @@ fn drop_flag_effects_for_location<'a, 'tcx, F>(
337337
|moi| callback(moi, DropFlagState::Absent))
338338
}
339339

340-
let bb = mir.basic_block_data(loc.block);
341-
match bb.statements.get(loc.index) {
340+
let block = &mir[loc.block];
341+
match block.statements.get(loc.index) {
342342
Some(stmt) => match stmt.kind {
343343
repr::StatementKind::Assign(ref lvalue, _) => {
344344
debug!("drop_flag_effects: assignment {:?}", stmt);
@@ -348,8 +348,8 @@ fn drop_flag_effects_for_location<'a, 'tcx, F>(
348348
}
349349
},
350350
None => {
351-
debug!("drop_flag_effects: replace {:?}", bb.terminator());
352-
match bb.terminator().kind {
351+
debug!("drop_flag_effects: replace {:?}", block.terminator());
352+
match block.terminator().kind {
353353
repr::TerminatorKind::DropAndReplace { ref location, .. } => {
354354
on_all_children_bits(tcx, mir, move_data,
355355
move_data.rev_lookup.find(location),

0 commit comments

Comments
 (0)