1
1
use rustc_index:: vec:: IndexVec ;
2
- // use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3
- // use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
4
- // use crate::ich::StableHashingContext;
2
+ use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
3
+ use rustc_serialize:: { Encodable , Encoder , Decodable , Decoder } ;
4
+ use crate :: ich:: StableHashingContext ;
5
5
use crate :: mir:: { BasicBlock , BasicBlockData , Body , LocalDecls , Location , Successors } ;
6
6
use rustc_data_structures:: graph:: { self , GraphPredecessors , GraphSuccessors } ;
7
7
use rustc_data_structures:: graph:: dominators:: { dominators, Dominators } ;
@@ -14,23 +14,23 @@ pub struct Cache {
14
14
predecessors : Option < IndexVec < BasicBlock , Vec < BasicBlock > > > ,
15
15
}
16
16
17
- // impl<'tcx, T> rustc_serialize::Encodable for Cache<'tcx, T> {
18
- // fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
19
- // Encodable::encode(&(), s)
20
- // }
21
- // }
22
- //
23
- // impl<'tcx, T> rustc_serialize::Decodable for Cache<'tcx, T> {
24
- // fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
25
- // Decodable::decode(d).map(|_v: ()| Self::new())
26
- // }
27
- // }
28
- //
29
- // impl<'a, 'tcx, T > HashStable<StableHashingContext<'a>> for Cache<'tcx, T> {
30
- // fn hash_stable(&self, _: &mut StableHashingContext<'a>, _: &mut StableHasher) {
31
- // // Do nothing.
32
- // }
33
- // }
17
+ impl rustc_serialize:: Encodable for Cache {
18
+ fn encode < S : Encoder > ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
19
+ Encodable :: encode ( & ( ) , s)
20
+ }
21
+ }
22
+
23
+ impl rustc_serialize:: Decodable for Cache {
24
+ fn decode < D : Decoder > ( d : & mut D ) -> Result < Self , D :: Error > {
25
+ Decodable :: decode ( d) . map ( |_v : ( ) | Self :: new ( ) )
26
+ }
27
+ }
28
+
29
+ impl < ' a > HashStable < StableHashingContext < ' a > > for Cache {
30
+ fn hash_stable ( & self , _: & mut StableHashingContext < ' a > , _: & mut StableHasher ) {
31
+ // Do nothing.
32
+ }
33
+ }
34
34
35
35
macro_rules! get_predecessors {
36
36
( mut $self: ident, $block: expr, $body: expr) => {
@@ -98,13 +98,13 @@ impl Cache {
98
98
99
99
#[ inline]
100
100
/// This will recompute the predecessors cache if it is not available
101
- pub fn predecessors ( & mut self , body : & Body < ' _ > ) -> & IndexVec < BasicBlock , Vec < BasicBlock > > {
101
+ fn predecessors ( & mut self , body : & Body < ' _ > ) -> & IndexVec < BasicBlock , Vec < BasicBlock > > {
102
102
self . ensure_predecessors ( body) ;
103
103
self . predecessors . as_ref ( ) . unwrap ( )
104
104
}
105
105
106
106
#[ inline]
107
- pub fn predecessors_for ( & mut self , bb : BasicBlock , body : & Body < ' _ > ) -> & [ BasicBlock ] {
107
+ fn predecessors_for ( & mut self , bb : BasicBlock , body : & Body < ' _ > ) -> & [ BasicBlock ] {
108
108
& self . predecessors ( body) [ bb]
109
109
}
110
110
@@ -136,55 +136,58 @@ impl Cache {
136
136
}
137
137
}
138
138
139
- pub struct BodyCache < T > {
139
+ #[ derive( Clone , Debug , RustcEncodable , RustcDecodable ) ]
140
+ pub struct BodyCache < ' tcx > {
140
141
cache : Cache ,
141
- body : T ,
142
+ body : Body < ' tcx > ,
142
143
}
143
144
144
- impl < T > BodyCache < T > {
145
- pub fn new ( body : T ) -> Self {
145
+ impl BodyCache < ' tcx > {
146
+ pub fn new ( body : Body < ' tcx > ) -> Self {
146
147
Self {
147
148
cache : Cache :: new ( ) ,
148
- body
149
+ body,
149
150
}
150
151
}
151
152
}
152
153
153
- impl < ' a , ' tcx > BodyCache < & ' a Body < ' tcx > > {
154
- #[ inline]
155
- pub fn predecessors_for ( & mut self , bb : BasicBlock ) -> & [ BasicBlock ] {
156
- self . cache . predecessors_for ( bb, self . body )
154
+ impl BodyCache < ' tcx > {
155
+ pub fn ensure_predecessors ( & mut self ) {
156
+ self . cache . ensure_predecessors ( & self . body ) ;
157
157
}
158
158
159
- #[ inline]
160
- pub fn body ( & self ) -> & ' a Body < ' tcx > {
161
- self . body
159
+ pub fn predecessors ( & mut self ) -> & IndexVec < BasicBlock , Vec < BasicBlock > > {
160
+ self . cache . predecessors ( & self . body )
162
161
}
163
162
164
- #[ inline]
165
- pub fn read_only ( mut self ) -> ReadOnlyBodyCache < ' a , ' tcx > {
166
- self . cache . ensure_predecessors ( self . body ) ;
163
+ pub fn read_only ( & self ) -> ReadOnlyBodyCache < ' _ , ' _ > {
164
+ assert ! ( self . cache. predecessors. is_some( ) , "" ) ;
167
165
ReadOnlyBodyCache {
168
- cache : self . cache ,
169
- body : self . body ,
166
+ cache : & self . cache ,
167
+ body : & self . body ,
170
168
}
171
169
}
172
170
173
- #[ inline]
174
- pub fn basic_blocks ( & self ) -> & IndexVec < BasicBlock , BasicBlockData < ' tcx > > {
175
- & self . body . basic_blocks
171
+ pub fn body ( & self ) -> & Body < ' tcx > {
172
+ & self . body
176
173
}
177
- }
178
174
179
- impl < ' a , ' tcx > Deref for BodyCache < & ' a Body < ' tcx > > {
180
- type Target = Body < ' tcx > ;
175
+ pub fn body_mut ( & mut self ) -> & mut Body < ' tcx > {
176
+ & mut self . body
177
+ }
181
178
182
- fn deref ( & self ) -> & Self :: Target {
183
- self . body
179
+ pub fn basic_blocks_mut ( & mut self ) -> & mut IndexVec < BasicBlock , BasicBlockData < ' tcx > > {
180
+ self . cache . basic_blocks_mut ( & mut self . body )
181
+ }
182
+
183
+ pub fn basic_blocks_and_local_decls_mut (
184
+ & mut self
185
+ ) -> ( & mut IndexVec < BasicBlock , BasicBlockData < ' tcx > > , & mut LocalDecls < ' tcx > ) {
186
+ self . cache . basic_blocks_and_local_decls_mut ( & mut self . body )
184
187
}
185
188
}
186
189
187
- impl < ' a , ' tcx > Index < BasicBlock > for BodyCache < & ' a Body < ' tcx > > {
190
+ impl < ' tcx > Index < BasicBlock > for BodyCache < ' tcx > {
188
191
type Output = BasicBlockData < ' tcx > ;
189
192
190
193
#[ inline]
@@ -193,69 +196,29 @@ impl<'a, 'tcx> Index<BasicBlock> for BodyCache<&'a Body<'tcx>> {
193
196
}
194
197
}
195
198
196
- impl < ' a , ' tcx > BodyCache < & ' a mut Body < ' tcx > > {
197
- #[ inline]
198
- pub fn body ( & self ) -> & Body < ' tcx > {
199
- self . body
200
- }
201
-
202
- #[ inline]
203
- pub fn body_mut ( & mut self ) -> & mut Body < ' tcx > {
204
- self . body
205
- }
206
-
207
- #[ inline]
208
- pub fn read_only ( mut self ) -> ReadOnlyBodyCache < ' a , ' tcx > {
209
- self . cache . ensure_predecessors ( self . body ) ;
210
- ReadOnlyBodyCache {
211
- cache : self . cache ,
212
- body : self . body ,
213
- }
214
- }
215
-
216
- #[ inline]
217
- pub fn basic_blocks ( & self ) -> & IndexVec < BasicBlock , BasicBlockData < ' tcx > > {
218
- & self . body . basic_blocks
219
- }
220
-
221
- #[ inline]
222
- pub fn basic_blocks_mut ( & mut self ) -> & mut IndexVec < BasicBlock , BasicBlockData < ' tcx > > {
223
- self . cache . basic_blocks_mut ( & mut self . body )
199
+ impl < ' tcx > IndexMut < BasicBlock > for BodyCache < ' tcx > {
200
+ fn index_mut ( & mut self , index : BasicBlock ) -> & mut Self :: Output {
201
+ & mut self . basic_blocks_mut ( ) [ index]
224
202
}
225
203
}
226
204
227
- impl < ' a , ' tcx > Deref for BodyCache < & ' a mut Body < ' tcx > > {
205
+ impl < ' tcx > Deref for BodyCache < ' tcx > {
228
206
type Target = Body < ' tcx > ;
229
207
230
208
fn deref ( & self ) -> & Self :: Target {
231
- self . body
232
- }
233
- }
234
-
235
- impl < ' a , ' tcx > DerefMut for BodyCache < & ' a mut Body < ' tcx > > {
236
- fn deref_mut ( & mut self ) -> & mut Body < ' tcx > {
237
- self . body
238
- }
239
- }
240
-
241
- impl < ' a , ' tcx > Index < BasicBlock > for BodyCache < & ' a mut Body < ' tcx > > {
242
- type Output = BasicBlockData < ' tcx > ;
243
-
244
- #[ inline]
245
- fn index ( & self , index : BasicBlock ) -> & BasicBlockData < ' tcx > {
246
- & self . body [ index]
209
+ & self . body
247
210
}
248
211
}
249
212
250
- impl < ' a , ' tcx > IndexMut < BasicBlock > for BodyCache < & ' a mut Body < ' tcx > > {
251
- fn index_mut ( & mut self , index : BasicBlock ) -> & mut Self :: Output {
252
- self . cache . invalidate_predecessors ( ) ;
253
- & mut self . body . basic_blocks [ index]
213
+ impl < ' tcx > DerefMut for BodyCache < ' tcx > {
214
+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
215
+ & mut self . body
254
216
}
255
217
}
256
218
219
+ #[ derive( Copy , Clone , Debug ) ]
257
220
pub struct ReadOnlyBodyCache < ' a , ' tcx > {
258
- cache : Cache ,
221
+ cache : & ' a Cache ,
259
222
body : & ' a Body < ' tcx > ,
260
223
}
261
224
@@ -289,13 +252,6 @@ impl ReadOnlyBodyCache<'a, 'tcx> {
289
252
pub fn dominators ( & self ) -> Dominators < BasicBlock > {
290
253
dominators ( self )
291
254
}
292
-
293
- pub fn to_owned ( self ) -> BodyCache < & ' a Body < ' tcx > > {
294
- BodyCache {
295
- cache : self . cache ,
296
- body : self . body ,
297
- }
298
- }
299
255
}
300
256
301
257
impl graph:: DirectedGraph for ReadOnlyBodyCache < ' a , ' tcx > {
@@ -358,4 +314,20 @@ impl Index<BasicBlock> for ReadOnlyBodyCache<'a, 'tcx> {
358
314
fn index ( & self , index : BasicBlock ) -> & BasicBlockData < ' tcx > {
359
315
& self . body [ index]
360
316
}
361
- }
317
+ }
318
+
319
+ CloneTypeFoldableAndLiftImpls ! {
320
+ Cache ,
321
+ }
322
+
323
+ impl_stable_hash_for ! ( struct BodyCache <' tcx> {
324
+ cache,
325
+ body,
326
+ } ) ;
327
+
328
+ BraceStructTypeFoldableImpl ! {
329
+ impl <' tcx> TypeFoldable <' tcx> for BodyCache <' tcx> {
330
+ cache,
331
+ body
332
+ }
333
+ }
0 commit comments