1
+ use rustc_index:: { Idx , IndexVec } ;
2
+ use rustc_middle:: mir:: * ;
3
+ use rustc_middle:: ty:: Ty ;
4
+ use rustc_span:: Span ;
1
5
use tracing:: debug;
2
6
3
- use crate :: mir:: * ;
4
-
5
7
/// This struct represents a patch to MIR, which can add
6
8
/// new statements and basic blocks and patch over block
7
9
/// terminators.
8
- pub struct MirPatch < ' tcx > {
10
+ pub ( crate ) struct MirPatch < ' tcx > {
9
11
patch_map : IndexVec < BasicBlock , Option < TerminatorKind < ' tcx > > > ,
10
12
new_blocks : Vec < BasicBlockData < ' tcx > > ,
11
13
new_statements : Vec < ( Location , StatementKind < ' tcx > ) > ,
@@ -22,7 +24,7 @@ pub struct MirPatch<'tcx> {
22
24
}
23
25
24
26
impl < ' tcx > MirPatch < ' tcx > {
25
- pub fn new ( body : & Body < ' tcx > ) -> Self {
27
+ pub ( crate ) fn new ( body : & Body < ' tcx > ) -> Self {
26
28
let mut result = MirPatch {
27
29
patch_map : IndexVec :: from_elem ( None , & body. basic_blocks ) ,
28
30
new_blocks : vec ! [ ] ,
@@ -69,7 +71,7 @@ impl<'tcx> MirPatch<'tcx> {
69
71
result
70
72
}
71
73
72
- pub fn resume_block ( & mut self ) -> BasicBlock {
74
+ pub ( crate ) fn resume_block ( & mut self ) -> BasicBlock {
73
75
if let Some ( bb) = self . resume_block {
74
76
return bb;
75
77
}
@@ -86,7 +88,7 @@ impl<'tcx> MirPatch<'tcx> {
86
88
bb
87
89
}
88
90
89
- pub fn unreachable_cleanup_block ( & mut self ) -> BasicBlock {
91
+ pub ( crate ) fn unreachable_cleanup_block ( & mut self ) -> BasicBlock {
90
92
if let Some ( bb) = self . unreachable_cleanup_block {
91
93
return bb;
92
94
}
@@ -103,7 +105,7 @@ impl<'tcx> MirPatch<'tcx> {
103
105
bb
104
106
}
105
107
106
- pub fn unreachable_no_cleanup_block ( & mut self ) -> BasicBlock {
108
+ pub ( crate ) fn unreachable_no_cleanup_block ( & mut self ) -> BasicBlock {
107
109
if let Some ( bb) = self . unreachable_no_cleanup_block {
108
110
return bb;
109
111
}
@@ -120,7 +122,7 @@ impl<'tcx> MirPatch<'tcx> {
120
122
bb
121
123
}
122
124
123
- pub fn terminate_block ( & mut self , reason : UnwindTerminateReason ) -> BasicBlock {
125
+ pub ( crate ) fn terminate_block ( & mut self , reason : UnwindTerminateReason ) -> BasicBlock {
124
126
if let Some ( ( cached_bb, cached_reason) ) = self . terminate_block
125
127
&& reason == cached_reason
126
128
{
@@ -139,19 +141,11 @@ impl<'tcx> MirPatch<'tcx> {
139
141
bb
140
142
}
141
143
142
- pub fn is_patched ( & self , bb : BasicBlock ) -> bool {
144
+ pub ( crate ) fn is_patched ( & self , bb : BasicBlock ) -> bool {
143
145
self . patch_map [ bb] . is_some ( )
144
146
}
145
147
146
- pub fn terminator_loc ( & self , body : & Body < ' tcx > , bb : BasicBlock ) -> Location {
147
- let offset = match bb. index ( ) . checked_sub ( body. basic_blocks . len ( ) ) {
148
- Some ( index) => self . new_blocks [ index] . statements . len ( ) ,
149
- None => body[ bb] . statements . len ( ) ,
150
- } ;
151
- Location { block : bb, statement_index : offset }
152
- }
153
-
154
- pub fn new_local_with_info (
148
+ pub ( crate ) fn new_local_with_info (
155
149
& mut self ,
156
150
ty : Ty < ' tcx > ,
157
151
span : Span ,
@@ -165,37 +159,37 @@ impl<'tcx> MirPatch<'tcx> {
165
159
Local :: new ( index)
166
160
}
167
161
168
- pub fn new_temp ( & mut self , ty : Ty < ' tcx > , span : Span ) -> Local {
162
+ pub ( crate ) fn new_temp ( & mut self , ty : Ty < ' tcx > , span : Span ) -> Local {
169
163
let index = self . next_local ;
170
164
self . next_local += 1 ;
171
165
self . new_locals . push ( LocalDecl :: new ( ty, span) ) ;
172
166
Local :: new ( index)
173
167
}
174
168
175
- pub fn new_block ( & mut self , data : BasicBlockData < ' tcx > ) -> BasicBlock {
169
+ pub ( crate ) fn new_block ( & mut self , data : BasicBlockData < ' tcx > ) -> BasicBlock {
176
170
let block = BasicBlock :: new ( self . patch_map . len ( ) ) ;
177
171
debug ! ( "MirPatch: new_block: {:?}: {:?}" , block, data) ;
178
172
self . new_blocks . push ( data) ;
179
173
self . patch_map . push ( None ) ;
180
174
block
181
175
}
182
176
183
- pub fn patch_terminator ( & mut self , block : BasicBlock , new : TerminatorKind < ' tcx > ) {
177
+ pub ( crate ) fn patch_terminator ( & mut self , block : BasicBlock , new : TerminatorKind < ' tcx > ) {
184
178
assert ! ( self . patch_map[ block] . is_none( ) ) ;
185
179
debug ! ( "MirPatch: patch_terminator({:?}, {:?})" , block, new) ;
186
180
self . patch_map [ block] = Some ( new) ;
187
181
}
188
182
189
- pub fn add_statement ( & mut self , loc : Location , stmt : StatementKind < ' tcx > ) {
183
+ pub ( crate ) fn add_statement ( & mut self , loc : Location , stmt : StatementKind < ' tcx > ) {
190
184
debug ! ( "MirPatch: add_statement({:?}, {:?})" , loc, stmt) ;
191
185
self . new_statements . push ( ( loc, stmt) ) ;
192
186
}
193
187
194
- pub fn add_assign ( & mut self , loc : Location , place : Place < ' tcx > , rv : Rvalue < ' tcx > ) {
188
+ pub ( crate ) fn add_assign ( & mut self , loc : Location , place : Place < ' tcx > , rv : Rvalue < ' tcx > ) {
195
189
self . add_statement ( loc, StatementKind :: Assign ( Box :: new ( ( place, rv) ) ) ) ;
196
190
}
197
191
198
- pub fn apply ( self , body : & mut Body < ' tcx > ) {
192
+ pub ( crate ) fn apply ( self , body : & mut Body < ' tcx > ) {
199
193
debug ! (
200
194
"MirPatch: {:?} new temps, starting from index {}: {:?}" ,
201
195
self . new_locals. len( ) ,
@@ -241,14 +235,14 @@ impl<'tcx> MirPatch<'tcx> {
241
235
}
242
236
}
243
237
244
- pub fn source_info_for_index ( data : & BasicBlockData < ' _ > , loc : Location ) -> SourceInfo {
238
+ fn source_info_for_index ( data : & BasicBlockData < ' _ > , loc : Location ) -> SourceInfo {
245
239
match data. statements . get ( loc. statement_index ) {
246
240
Some ( stmt) => stmt. source_info ,
247
241
None => data. terminator ( ) . source_info ,
248
242
}
249
243
}
250
244
251
- pub fn source_info_for_location ( & self , body : & Body < ' tcx > , loc : Location ) -> SourceInfo {
245
+ pub ( crate ) fn source_info_for_location ( & self , body : & Body < ' tcx > , loc : Location ) -> SourceInfo {
252
246
let data = match loc. block . index ( ) . checked_sub ( body. basic_blocks . len ( ) ) {
253
247
Some ( new) => & self . new_blocks [ new] ,
254
248
None => & body[ loc. block ] ,
0 commit comments