11use 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 ;
55use crate :: mir:: { BasicBlock , BasicBlockData , Body , LocalDecls , Location , Successors } ;
66use rustc_data_structures:: graph:: { self , GraphPredecessors , GraphSuccessors } ;
77use rustc_data_structures:: graph:: dominators:: { dominators, Dominators } ;
@@ -14,23 +14,23 @@ pub struct Cache {
1414 predecessors : Option < IndexVec < BasicBlock , Vec < BasicBlock > > > ,
1515}
1616
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+ }
3434
3535macro_rules! get_predecessors {
3636 ( mut $self: ident, $block: expr, $body: expr) => {
@@ -98,13 +98,13 @@ impl Cache {
9898
9999 #[ inline]
100100 /// 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 > > {
102102 self . ensure_predecessors ( body) ;
103103 self . predecessors . as_ref ( ) . unwrap ( )
104104 }
105105
106106 #[ inline]
107- pub fn predecessors_for ( & mut self , bb : BasicBlock , body : & Body < ' _ > ) -> & [ BasicBlock ] {
107+ fn predecessors_for ( & mut self , bb : BasicBlock , body : & Body < ' _ > ) -> & [ BasicBlock ] {
108108 & self . predecessors ( body) [ bb]
109109 }
110110
@@ -136,55 +136,58 @@ impl Cache {
136136 }
137137}
138138
139- pub struct BodyCache < T > {
139+ #[ derive( Clone , Debug , RustcEncodable , RustcDecodable ) ]
140+ pub struct BodyCache < ' tcx > {
140141 cache : Cache ,
141- body : T ,
142+ body : Body < ' tcx > ,
142143}
143144
144- impl < T > BodyCache < T > {
145- pub fn new ( body : T ) -> Self {
145+ impl BodyCache < ' tcx > {
146+ pub fn new ( body : Body < ' tcx > ) -> Self {
146147 Self {
147148 cache : Cache :: new ( ) ,
148- body
149+ body,
149150 }
150151 }
151152}
152153
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 ) ;
157157 }
158158
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 )
162161 }
163162
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( ) , "" ) ;
167165 ReadOnlyBodyCache {
168- cache : self . cache ,
169- body : self . body ,
166+ cache : & self . cache ,
167+ body : & self . body ,
170168 }
171169 }
172170
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
176173 }
177- }
178174
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+ }
181178
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 )
184187 }
185188}
186189
187- impl < ' a , ' tcx > Index < BasicBlock > for BodyCache < & ' a Body < ' tcx > > {
190+ impl < ' tcx > Index < BasicBlock > for BodyCache < ' tcx > {
188191 type Output = BasicBlockData < ' tcx > ;
189192
190193 #[ inline]
@@ -193,69 +196,29 @@ impl<'a, 'tcx> Index<BasicBlock> for BodyCache<&'a Body<'tcx>> {
193196 }
194197}
195198
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]
224202 }
225203}
226204
227- impl < ' a , ' tcx > Deref for BodyCache < & ' a mut Body < ' tcx > > {
205+ impl < ' tcx > Deref for BodyCache < ' tcx > {
228206 type Target = Body < ' tcx > ;
229207
230208 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
247210 }
248211}
249212
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
254216 }
255217}
256218
219+ #[ derive( Copy , Clone , Debug ) ]
257220pub struct ReadOnlyBodyCache < ' a , ' tcx > {
258- cache : Cache ,
221+ cache : & ' a Cache ,
259222 body : & ' a Body < ' tcx > ,
260223}
261224
@@ -289,13 +252,6 @@ impl ReadOnlyBodyCache<'a, 'tcx> {
289252 pub fn dominators ( & self ) -> Dominators < BasicBlock > {
290253 dominators ( self )
291254 }
292-
293- pub fn to_owned ( self ) -> BodyCache < & ' a Body < ' tcx > > {
294- BodyCache {
295- cache : self . cache ,
296- body : self . body ,
297- }
298- }
299255}
300256
301257impl graph:: DirectedGraph for ReadOnlyBodyCache < ' a , ' tcx > {
@@ -358,4 +314,20 @@ impl Index<BasicBlock> for ReadOnlyBodyCache<'a, 'tcx> {
358314 fn index ( & self , index : BasicBlock ) -> & BasicBlockData < ' tcx > {
359315 & self . body [ index]
360316 }
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