]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/visit.rs
Rollup merge of #56149 - ariasuni:improve-amctime-doc, r=TimNN
[rust.git] / src / librustc / mir / visit.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use hir::def_id::DefId;
12 use ty::subst::Substs;
13 use ty::{ClosureSubsts, GeneratorSubsts, Region, Ty};
14 use mir::*;
15 use syntax_pos::Span;
16
17 // # The MIR Visitor
18 //
19 // ## Overview
20 //
21 // There are two visitors, one for immutable and one for mutable references,
22 // but both are generated by the following macro. The code is written according
23 // to the following conventions:
24 //
25 // - introduce a `visit_foo` and a `super_foo` method for every MIR type
26 // - `visit_foo`, by default, calls `super_foo`
27 // - `super_foo`, by default, destructures the `foo` and calls `visit_foo`
28 //
29 // This allows you as a user to override `visit_foo` for types are
30 // interested in, and invoke (within that method) call
31 // `self.super_foo` to get the default behavior. Just as in an OO
32 // language, you should never call `super` methods ordinarily except
33 // in that circumstance.
34 //
35 // For the most part, we do not destructure things external to the
36 // MIR, e.g. types, spans, etc, but simply visit them and stop. This
37 // avoids duplication with other visitors like `TypeFoldable`.
38 //
39 // ## Updating
40 //
41 // The code is written in a very deliberate style intended to minimize
42 // the chance of things being overlooked. You'll notice that we always
43 // use pattern matching to reference fields and we ensure that all
44 // matches are exhaustive.
45 //
46 // For example, the `super_basic_block_data` method begins like this:
47 //
48 // ```rust
49 // fn super_basic_block_data(&mut self,
50 //                           block: BasicBlock,
51 //                           data: & $($mutability)* BasicBlockData<'tcx>) {
52 //     let BasicBlockData {
53 //         ref $($mutability)* statements,
54 //         ref $($mutability)* terminator,
55 //         is_cleanup: _
56 //     } = *data;
57 //
58 //     for statement in statements {
59 //         self.visit_statement(block, statement);
60 //     }
61 //
62 //     ...
63 // }
64 // ```
65 //
66 // Here we used `let BasicBlockData { <fields> } = *data` deliberately,
67 // rather than writing `data.statements` in the body. This is because if one
68 // adds a new field to `BasicBlockData`, one will be forced to revise this code,
69 // and hence one will (hopefully) invoke the correct visit methods (if any).
70 //
71 // For this to work, ALL MATCHES MUST BE EXHAUSTIVE IN FIELDS AND VARIANTS.
72 // That means you never write `..` to skip over fields, nor do you write `_`
73 // to skip over variants in a `match`.
74 //
75 // The only place that `_` is acceptable is to match a field (or
76 // variant argument) that does not require visiting, as in
77 // `is_cleanup` above.
78
79 macro_rules! make_mir_visitor {
80     ($visitor_trait_name:ident, $($mutability:ident)*) => {
81         pub trait $visitor_trait_name<'tcx> {
82             // Override these, and call `self.super_xxx` to revert back to the
83             // default behavior.
84
85             fn visit_mir(&mut self, mir: & $($mutability)* Mir<'tcx>) {
86                 self.super_mir(mir);
87             }
88
89             fn visit_basic_block_data(&mut self,
90                                       block: BasicBlock,
91                                       data: & $($mutability)* BasicBlockData<'tcx>) {
92                 self.super_basic_block_data(block, data);
93             }
94
95             fn visit_source_scope_data(&mut self,
96                                            scope_data: & $($mutability)* SourceScopeData) {
97                 self.super_source_scope_data(scope_data);
98             }
99
100             fn visit_statement(&mut self,
101                                block: BasicBlock,
102                                statement: & $($mutability)* Statement<'tcx>,
103                                location: Location) {
104                 self.super_statement(block, statement, location);
105             }
106
107             fn visit_assign(&mut self,
108                             block: BasicBlock,
109                             place: & $($mutability)* Place<'tcx>,
110                             rvalue: & $($mutability)* Rvalue<'tcx>,
111                             location: Location) {
112                 self.super_assign(block, place, rvalue, location);
113             }
114
115             fn visit_terminator(&mut self,
116                                 block: BasicBlock,
117                                 terminator: & $($mutability)* Terminator<'tcx>,
118                                 location: Location) {
119                 self.super_terminator(block, terminator, location);
120             }
121
122             fn visit_terminator_kind(&mut self,
123                                      block: BasicBlock,
124                                      kind: & $($mutability)* TerminatorKind<'tcx>,
125                                      location: Location) {
126                 self.super_terminator_kind(block, kind, location);
127             }
128
129             fn visit_assert_message(&mut self,
130                                     msg: & $($mutability)* AssertMessage<'tcx>,
131                                     location: Location) {
132                 self.super_assert_message(msg, location);
133             }
134
135             fn visit_rvalue(&mut self,
136                             rvalue: & $($mutability)* Rvalue<'tcx>,
137                             location: Location) {
138                 self.super_rvalue(rvalue, location);
139             }
140
141             fn visit_operand(&mut self,
142                              operand: & $($mutability)* Operand<'tcx>,
143                              location: Location) {
144                 self.super_operand(operand, location);
145             }
146
147             fn visit_ascribe_user_ty(&mut self,
148                                      place: & $($mutability)* Place<'tcx>,
149                                      variance: & $($mutability)* ty::Variance,
150                                      user_ty: & $($mutability)* UserTypeProjection<'tcx>,
151                                      location: Location) {
152                 self.super_ascribe_user_ty(place, variance, user_ty, location);
153             }
154
155             fn visit_retag(&mut self,
156                            fn_entry: & $($mutability)* bool,
157                            place: & $($mutability)* Place<'tcx>,
158                            location: Location) {
159                 self.super_retag(fn_entry, place, location);
160             }
161
162             fn visit_place(&mut self,
163                             place: & $($mutability)* Place<'tcx>,
164                             context: PlaceContext<'tcx>,
165                             location: Location) {
166                 self.super_place(place, context, location);
167             }
168
169             fn visit_static(&mut self,
170                             static_: & $($mutability)* Static<'tcx>,
171                             context: PlaceContext<'tcx>,
172                             location: Location) {
173                 self.super_static(static_, context, location);
174             }
175
176             fn visit_projection(&mut self,
177                                 place: & $($mutability)* PlaceProjection<'tcx>,
178                                 context: PlaceContext<'tcx>,
179                                 location: Location) {
180                 self.super_projection(place, context, location);
181             }
182
183             fn visit_projection_elem(&mut self,
184                                      place: & $($mutability)* PlaceElem<'tcx>,
185                                      location: Location) {
186                 self.super_projection_elem(place, location);
187             }
188
189             fn visit_branch(&mut self,
190                             source: BasicBlock,
191                             target: BasicBlock) {
192                 self.super_branch(source, target);
193             }
194
195             fn visit_constant(&mut self,
196                               constant: & $($mutability)* Constant<'tcx>,
197                               location: Location) {
198                 self.super_constant(constant, location);
199             }
200
201             fn visit_def_id(&mut self,
202                             def_id: & $($mutability)* DefId,
203                             _: Location) {
204                 self.super_def_id(def_id);
205             }
206
207             fn visit_span(&mut self,
208                           span: & $($mutability)* Span) {
209                 self.super_span(span);
210             }
211
212             fn visit_source_info(&mut self,
213                                  source_info: & $($mutability)* SourceInfo) {
214                 self.super_source_info(source_info);
215             }
216
217             fn visit_ty(&mut self,
218                         ty: & $($mutability)* Ty<'tcx>,
219                         _: TyContext) {
220                 self.super_ty(ty);
221             }
222
223             fn visit_user_type_projection(
224                 &mut self,
225                 ty: & $($mutability)* UserTypeProjection<'tcx>,
226             ) {
227                 self.super_user_type_projection(ty);
228             }
229
230             fn visit_user_type_annotation(
231                 &mut self,
232                 ty: & $($mutability)* UserTypeAnnotation<'tcx>,
233             ) {
234                 self.super_user_type_annotation(ty);
235             }
236
237             fn visit_region(&mut self,
238                             region: & $($mutability)* ty::Region<'tcx>,
239                             _: Location) {
240                 self.super_region(region);
241             }
242
243             fn visit_const(&mut self,
244                            constant: & $($mutability)* &'tcx ty::Const<'tcx>,
245                            _: Location) {
246                 self.super_const(constant);
247             }
248
249             fn visit_substs(&mut self,
250                             substs: & $($mutability)* &'tcx Substs<'tcx>,
251                             _: Location) {
252                 self.super_substs(substs);
253             }
254
255             fn visit_closure_substs(&mut self,
256                                     substs: & $($mutability)* ClosureSubsts<'tcx>,
257                                     _: Location) {
258                 self.super_closure_substs(substs);
259             }
260
261             fn visit_generator_substs(&mut self,
262                                       substs: & $($mutability)* GeneratorSubsts<'tcx>,
263                                     _: Location) {
264                 self.super_generator_substs(substs);
265             }
266
267             fn visit_local_decl(&mut self,
268                                 local: Local,
269                                 local_decl: & $($mutability)* LocalDecl<'tcx>) {
270                 self.super_local_decl(local, local_decl);
271             }
272
273             fn visit_local(&mut self,
274                             _local: & $($mutability)* Local,
275                             _context: PlaceContext<'tcx>,
276                             _location: Location) {
277             }
278
279             fn visit_source_scope(&mut self,
280                                       scope: & $($mutability)* SourceScope) {
281                 self.super_source_scope(scope);
282             }
283
284             // The `super_xxx` methods comprise the default behavior and are
285             // not meant to be overridden.
286
287             fn super_mir(&mut self,
288                          mir: & $($mutability)* Mir<'tcx>) {
289                 if let Some(yield_ty) = &$($mutability)* mir.yield_ty {
290                     self.visit_ty(yield_ty, TyContext::YieldTy(SourceInfo {
291                         span: mir.span,
292                         scope: OUTERMOST_SOURCE_SCOPE,
293                     }));
294                 }
295
296                 // for best performance, we want to use an iterator rather
297                 // than a for-loop, to avoid calling Mir::invalidate for
298                 // each basic block.
299                 macro_rules! basic_blocks {
300                     (mut) => (mir.basic_blocks_mut().iter_enumerated_mut());
301                     () => (mir.basic_blocks().iter_enumerated());
302                 };
303                 for (bb, data) in basic_blocks!($($mutability)*) {
304                     self.visit_basic_block_data(bb, data);
305                 }
306
307                 for scope in &$($mutability)* mir.source_scopes {
308                     self.visit_source_scope_data(scope);
309                 }
310
311                 self.visit_ty(&$($mutability)* mir.return_ty(), TyContext::ReturnTy(SourceInfo {
312                     span: mir.span,
313                     scope: OUTERMOST_SOURCE_SCOPE,
314                 }));
315
316                 for local in mir.local_decls.indices() {
317                     self.visit_local_decl(local, & $($mutability)* mir.local_decls[local]);
318                 }
319
320                 self.visit_span(&$($mutability)* mir.span);
321             }
322
323             fn super_basic_block_data(&mut self,
324                                       block: BasicBlock,
325                                       data: & $($mutability)* BasicBlockData<'tcx>) {
326                 let BasicBlockData {
327                     ref $($mutability)* statements,
328                     ref $($mutability)* terminator,
329                     is_cleanup: _
330                 } = *data;
331
332                 let mut index = 0;
333                 for statement in statements {
334                     let location = Location { block: block, statement_index: index };
335                     self.visit_statement(block, statement, location);
336                     index += 1;
337                 }
338
339                 if let Some(ref $($mutability)* terminator) = *terminator {
340                     let location = Location { block: block, statement_index: index };
341                     self.visit_terminator(block, terminator, location);
342                 }
343             }
344
345             fn super_source_scope_data(&mut self,
346                                            scope_data: & $($mutability)* SourceScopeData) {
347                 let SourceScopeData {
348                     ref $($mutability)* span,
349                     ref $($mutability)* parent_scope,
350                 } = *scope_data;
351
352                 self.visit_span(span);
353                 if let Some(ref $($mutability)* parent_scope) = *parent_scope {
354                     self.visit_source_scope(parent_scope);
355                 }
356             }
357
358             fn super_statement(&mut self,
359                                block: BasicBlock,
360                                statement: & $($mutability)* Statement<'tcx>,
361                                location: Location) {
362                 let Statement {
363                     ref $($mutability)* source_info,
364                     ref $($mutability)* kind,
365                 } = *statement;
366
367                 self.visit_source_info(source_info);
368                 match *kind {
369                     StatementKind::Assign(ref $($mutability)* place,
370                                           ref $($mutability)* rvalue) => {
371                         self.visit_assign(block, place, rvalue, location);
372                     }
373                     StatementKind::FakeRead(_, ref $($mutability)* place) => {
374                         self.visit_place(
375                             place,
376                             PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
377                             location
378                         );
379                     }
380                     StatementKind::SetDiscriminant{ ref $($mutability)* place, .. } => {
381                         self.visit_place(
382                             place,
383                             PlaceContext::MutatingUse(MutatingUseContext::Store),
384                             location
385                         );
386                     }
387                     StatementKind::EscapeToRaw(ref $($mutability)* op) => {
388                         self.visit_operand(op, location);
389                     }
390                     StatementKind::StorageLive(ref $($mutability)* local) => {
391                         self.visit_local(
392                             local,
393                             PlaceContext::NonUse(NonUseContext::StorageLive),
394                             location
395                         );
396                     }
397                     StatementKind::StorageDead(ref $($mutability)* local) => {
398                         self.visit_local(
399                             local,
400                             PlaceContext::NonUse(NonUseContext::StorageDead),
401                             location
402                         );
403                     }
404                     StatementKind::InlineAsm { ref $($mutability)* outputs,
405                                                ref $($mutability)* inputs,
406                                                asm: _ } => {
407                         for output in & $($mutability)* outputs[..] {
408                             self.visit_place(
409                                 output,
410                                 PlaceContext::MutatingUse(MutatingUseContext::AsmOutput),
411                                 location
412                             );
413                         }
414                         for (span, input) in & $($mutability)* inputs[..] {
415                             self.visit_span(span);
416                             self.visit_operand(input, location);
417                         }
418                     }
419                     StatementKind::Retag { ref $($mutability)* fn_entry,
420                                            ref $($mutability)* place } => {
421                         self.visit_retag(fn_entry, place, location);
422                     }
423                     StatementKind::AscribeUserType(
424                         ref $($mutability)* place,
425                         ref $($mutability)* variance,
426                         ref $($mutability)* user_ty,
427                     ) => {
428                         self.visit_ascribe_user_ty(place, variance, user_ty, location);
429                     }
430                     StatementKind::Nop => {}
431                 }
432             }
433
434             fn super_assign(&mut self,
435                             _block: BasicBlock,
436                             place: &$($mutability)* Place<'tcx>,
437                             rvalue: &$($mutability)* Rvalue<'tcx>,
438                             location: Location) {
439                 self.visit_place(
440                     place,
441                     PlaceContext::MutatingUse(MutatingUseContext::Store),
442                     location
443                 );
444                 self.visit_rvalue(rvalue, location);
445             }
446
447             fn super_terminator(&mut self,
448                                 block: BasicBlock,
449                                 terminator: &$($mutability)* Terminator<'tcx>,
450                                 location: Location) {
451                 let Terminator {
452                     ref $($mutability)* source_info,
453                     ref $($mutability)* kind,
454                 } = *terminator;
455
456                 self.visit_source_info(source_info);
457                 self.visit_terminator_kind(block, kind, location);
458             }
459
460             fn super_terminator_kind(&mut self,
461                                      block: BasicBlock,
462                                      kind: & $($mutability)* TerminatorKind<'tcx>,
463                                      source_location: Location) {
464                 match *kind {
465                     TerminatorKind::Goto { target } => {
466                         self.visit_branch(block, target);
467                     }
468
469                     TerminatorKind::SwitchInt { ref $($mutability)* discr,
470                                                 ref $($mutability)* switch_ty,
471                                                 values: _,
472                                                 ref targets } => {
473                         self.visit_operand(discr, source_location);
474                         self.visit_ty(switch_ty, TyContext::Location(source_location));
475                         for &target in targets {
476                             self.visit_branch(block, target);
477                         }
478                     }
479
480                     TerminatorKind::Resume |
481                     TerminatorKind::Abort |
482                     TerminatorKind::Return |
483                     TerminatorKind::GeneratorDrop |
484                     TerminatorKind::Unreachable => {
485                     }
486
487                     TerminatorKind::Drop { ref $($mutability)* location,
488                                            target,
489                                            unwind } => {
490                         self.visit_place(
491                             location,
492                             PlaceContext::MutatingUse(MutatingUseContext::Drop),
493                             source_location
494                         );
495                         self.visit_branch(block, target);
496                         unwind.map(|t| self.visit_branch(block, t));
497                     }
498
499                     TerminatorKind::DropAndReplace { ref $($mutability)* location,
500                                                      ref $($mutability)* value,
501                                                      target,
502                                                      unwind } => {
503                         self.visit_place(
504                             location,
505                             PlaceContext::MutatingUse(MutatingUseContext::Drop),
506                             source_location
507                         );
508                         self.visit_operand(value, source_location);
509                         self.visit_branch(block, target);
510                         unwind.map(|t| self.visit_branch(block, t));
511                     }
512
513                     TerminatorKind::Call { ref $($mutability)* func,
514                                            ref $($mutability)* args,
515                                            ref $($mutability)* destination,
516                                            cleanup,
517                                            from_hir_call: _, } => {
518                         self.visit_operand(func, source_location);
519                         for arg in args {
520                             self.visit_operand(arg, source_location);
521                         }
522                         if let Some((ref $($mutability)* destination, target)) = *destination {
523                             self.visit_place(
524                                 destination,
525                                 PlaceContext::MutatingUse(MutatingUseContext::Call),
526                                 source_location
527                             );
528                             self.visit_branch(block, target);
529                         }
530                         cleanup.map(|t| self.visit_branch(block, t));
531                     }
532
533                     TerminatorKind::Assert { ref $($mutability)* cond,
534                                              expected: _,
535                                              ref $($mutability)* msg,
536                                              target,
537                                              cleanup } => {
538                         self.visit_operand(cond, source_location);
539                         self.visit_assert_message(msg, source_location);
540                         self.visit_branch(block, target);
541                         cleanup.map(|t| self.visit_branch(block, t));
542                     }
543
544                     TerminatorKind::Yield { ref $($mutability)* value,
545                                               resume,
546                                               drop } => {
547                         self.visit_operand(value, source_location);
548                         self.visit_branch(block, resume);
549                         drop.map(|t| self.visit_branch(block, t));
550                     }
551
552                     TerminatorKind::FalseEdges { real_target, ref imaginary_targets} => {
553                         self.visit_branch(block, real_target);
554                         for target in imaginary_targets {
555                             self.visit_branch(block, *target);
556                         }
557                     }
558
559                     TerminatorKind::FalseUnwind { real_target, unwind } => {
560                         self.visit_branch(block, real_target);
561                         if let Some(unwind) = unwind {
562                             self.visit_branch(block, unwind);
563                         }
564                     }
565                 }
566             }
567
568             fn super_assert_message(&mut self,
569                                     msg: & $($mutability)* AssertMessage<'tcx>,
570                                     location: Location) {
571                 use mir::interpret::EvalErrorKind::*;
572                 if let BoundsCheck {
573                         ref $($mutability)* len,
574                         ref $($mutability)* index
575                     } = *msg {
576                     self.visit_operand(len, location);
577                     self.visit_operand(index, location);
578                 }
579             }
580
581             fn super_rvalue(&mut self,
582                             rvalue: & $($mutability)* Rvalue<'tcx>,
583                             location: Location) {
584                 match *rvalue {
585                     Rvalue::Use(ref $($mutability)* operand) => {
586                         self.visit_operand(operand, location);
587                     }
588
589                     Rvalue::Repeat(ref $($mutability)* value, _) => {
590                         self.visit_operand(value, location);
591                     }
592
593                     Rvalue::Ref(ref $($mutability)* r, bk, ref $($mutability)* path) => {
594                         self.visit_region(r, location);
595                         let ctx = match bk {
596                             BorrowKind::Shared => PlaceContext::NonMutatingUse(
597                                 NonMutatingUseContext::SharedBorrow(*r)
598                             ),
599                             BorrowKind::Shallow => PlaceContext::NonMutatingUse(
600                                 NonMutatingUseContext::ShallowBorrow(*r)
601                             ),
602                             BorrowKind::Unique => PlaceContext::NonMutatingUse(
603                                 NonMutatingUseContext::UniqueBorrow(*r)
604                             ),
605                             BorrowKind::Mut { .. } =>
606                                 PlaceContext::MutatingUse(MutatingUseContext::Borrow(*r)),
607                         };
608                         self.visit_place(path, ctx, location);
609                     }
610
611                     Rvalue::Len(ref $($mutability)* path) => {
612                         self.visit_place(
613                             path,
614                             PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
615                             location
616                         );
617                     }
618
619                     Rvalue::Cast(_cast_kind,
620                                  ref $($mutability)* operand,
621                                  ref $($mutability)* ty) => {
622                         self.visit_operand(operand, location);
623                         self.visit_ty(ty, TyContext::Location(location));
624                     }
625
626                     Rvalue::BinaryOp(_bin_op,
627                                      ref $($mutability)* lhs,
628                                      ref $($mutability)* rhs) |
629                     Rvalue::CheckedBinaryOp(_bin_op,
630                                      ref $($mutability)* lhs,
631                                      ref $($mutability)* rhs) => {
632                         self.visit_operand(lhs, location);
633                         self.visit_operand(rhs, location);
634                     }
635
636                     Rvalue::UnaryOp(_un_op, ref $($mutability)* op) => {
637                         self.visit_operand(op, location);
638                     }
639
640                     Rvalue::Discriminant(ref $($mutability)* place) => {
641                         self.visit_place(
642                             place,
643                             PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
644                             location
645                         );
646                     }
647
648                     Rvalue::NullaryOp(_op, ref $($mutability)* ty) => {
649                         self.visit_ty(ty, TyContext::Location(location));
650                     }
651
652                     Rvalue::Aggregate(ref $($mutability)* kind,
653                                       ref $($mutability)* operands) => {
654                         let kind = &$($mutability)* **kind;
655                         match *kind {
656                             AggregateKind::Array(ref $($mutability)* ty) => {
657                                 self.visit_ty(ty, TyContext::Location(location));
658                             }
659                             AggregateKind::Tuple => {
660                             }
661                             AggregateKind::Adt(_adt_def,
662                                                _variant_index,
663                                                ref $($mutability)* substs,
664                                                _user_substs,
665                                                _active_field_index) => {
666                                 self.visit_substs(substs, location);
667                             }
668                             AggregateKind::Closure(ref $($mutability)* def_id,
669                                                    ref $($mutability)* closure_substs) => {
670                                 self.visit_def_id(def_id, location);
671                                 self.visit_closure_substs(closure_substs, location);
672                             }
673                             AggregateKind::Generator(ref $($mutability)* def_id,
674                                                      ref $($mutability)* generator_substs,
675                                                      _movability) => {
676                                 self.visit_def_id(def_id, location);
677                                 self.visit_generator_substs(generator_substs, location);
678                             }
679                         }
680
681                         for operand in operands {
682                             self.visit_operand(operand, location);
683                         }
684                     }
685                 }
686             }
687
688             fn super_operand(&mut self,
689                              operand: & $($mutability)* Operand<'tcx>,
690                              location: Location) {
691                 match *operand {
692                     Operand::Copy(ref $($mutability)* place) => {
693                         self.visit_place(
694                             place,
695                             PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
696                             location
697                         );
698                     }
699                     Operand::Move(ref $($mutability)* place) => {
700                         self.visit_place(
701                             place,
702                             PlaceContext::NonMutatingUse(NonMutatingUseContext::Move),
703                             location
704                         );
705                     }
706                     Operand::Constant(ref $($mutability)* constant) => {
707                         self.visit_constant(constant, location);
708                     }
709                 }
710             }
711
712             fn super_ascribe_user_ty(&mut self,
713                                      place: & $($mutability)* Place<'tcx>,
714                                      _variance: & $($mutability)* ty::Variance,
715                                      user_ty: & $($mutability)* UserTypeProjection<'tcx>,
716                                      location: Location) {
717                 self.visit_place(
718                     place,
719                     PlaceContext::NonUse(NonUseContext::AscribeUserTy),
720                     location
721                 );
722                 self.visit_user_type_projection(user_ty);
723             }
724
725             fn super_retag(&mut self,
726                            _fn_entry: & $($mutability)* bool,
727                            place: & $($mutability)* Place<'tcx>,
728                            location: Location) {
729                 self.visit_place(
730                     place,
731                     PlaceContext::MutatingUse(MutatingUseContext::Retag),
732                     location,
733                 );
734             }
735
736             fn super_place(&mut self,
737                             place: & $($mutability)* Place<'tcx>,
738                             context: PlaceContext<'tcx>,
739                             location: Location) {
740                 match *place {
741                     Place::Local(ref $($mutability)* local) => {
742                         self.visit_local(local, context, location);
743                     }
744                     Place::Static(ref $($mutability)* static_) => {
745                         self.visit_static(static_, context, location);
746                     }
747                     Place::Promoted(ref $($mutability)* promoted) => {
748                         self.visit_ty(& $($mutability)* promoted.1, TyContext::Location(location));
749                     },
750                     Place::Projection(ref $($mutability)* proj) => {
751                         self.visit_projection(proj, context, location);
752                     }
753                 }
754             }
755
756             fn super_static(&mut self,
757                             static_: & $($mutability)* Static<'tcx>,
758                             _context: PlaceContext<'tcx>,
759                             location: Location) {
760                 let Static {
761                     ref $($mutability)* def_id,
762                     ref $($mutability)* ty,
763                 } = *static_;
764                 self.visit_def_id(def_id, location);
765                 self.visit_ty(ty, TyContext::Location(location));
766             }
767
768             fn super_projection(&mut self,
769                                 proj: & $($mutability)* PlaceProjection<'tcx>,
770                                 context: PlaceContext<'tcx>,
771                                 location: Location) {
772                 let Projection {
773                     ref $($mutability)* base,
774                     ref $($mutability)* elem,
775                 } = *proj;
776                 let context = if context.is_mutating_use() {
777                     PlaceContext::MutatingUse(MutatingUseContext::Projection)
778                 } else {
779                     PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
780                 };
781                 self.visit_place(base, context, location);
782                 self.visit_projection_elem(elem, location);
783             }
784
785             fn super_projection_elem(&mut self,
786                                      proj: & $($mutability)* PlaceElem<'tcx>,
787                                      location: Location) {
788                 match *proj {
789                     ProjectionElem::Deref => {
790                     }
791                     ProjectionElem::Subslice { from: _, to: _ } => {
792                     }
793                     ProjectionElem::Field(_field, ref $($mutability)* ty) => {
794                         self.visit_ty(ty, TyContext::Location(location));
795                     }
796                     ProjectionElem::Index(ref $($mutability)* local) => {
797                         self.visit_local(
798                             local,
799                             PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
800                             location
801                         );
802                     }
803                     ProjectionElem::ConstantIndex { offset: _,
804                                                     min_length: _,
805                                                     from_end: _ } => {
806                     }
807                     ProjectionElem::Downcast(_adt_def, _variant_index) => {
808                     }
809                 }
810             }
811
812             fn super_local_decl(&mut self,
813                                 local: Local,
814                                 local_decl: & $($mutability)* LocalDecl<'tcx>) {
815                 let LocalDecl {
816                     mutability: _,
817                     ref $($mutability)* ty,
818                     ref $($mutability)* user_ty,
819                     name: _,
820                     ref $($mutability)* source_info,
821                     ref $($mutability)* visibility_scope,
822                     internal: _,
823                     is_user_variable: _,
824                     is_block_tail: _,
825                 } = *local_decl;
826
827                 self.visit_ty(ty, TyContext::LocalDecl {
828                     local,
829                     source_info: *source_info,
830                 });
831                 for (user_ty, _) in & $($mutability)* user_ty.contents {
832                     self.visit_user_type_projection(user_ty);
833                 }
834                 self.visit_source_info(source_info);
835                 self.visit_source_scope(visibility_scope);
836             }
837
838             fn super_source_scope(&mut self,
839                                       _scope: & $($mutability)* SourceScope) {
840             }
841
842             fn super_branch(&mut self,
843                             _source: BasicBlock,
844                             _target: BasicBlock) {
845             }
846
847             fn super_constant(&mut self,
848                               constant: & $($mutability)* Constant<'tcx>,
849                               location: Location) {
850                 let Constant {
851                     ref $($mutability)* span,
852                     ref $($mutability)* ty,
853                     ref $($mutability)* user_ty,
854                     ref $($mutability)* literal,
855                 } = *constant;
856
857                 self.visit_span(span);
858                 self.visit_ty(ty, TyContext::Location(location));
859                 drop(user_ty); // no visit method for this
860                 self.visit_const(literal, location);
861             }
862
863             fn super_def_id(&mut self, _def_id: & $($mutability)* DefId) {
864             }
865
866             fn super_span(&mut self, _span: & $($mutability)* Span) {
867             }
868
869             fn super_source_info(&mut self, source_info: & $($mutability)* SourceInfo) {
870                 let SourceInfo {
871                     ref $($mutability)* span,
872                     ref $($mutability)* scope,
873                 } = *source_info;
874
875                 self.visit_span(span);
876                 self.visit_source_scope(scope);
877             }
878
879             fn super_user_type_projection(
880                 &mut self,
881                 ty: & $($mutability)* UserTypeProjection<'tcx>,
882             ) {
883                 let UserTypeProjection {
884                     ref $($mutability)* base,
885                     projs: _, // Note: Does not visit projection elems!
886                 } = *ty;
887                 self.visit_user_type_annotation(base);
888             }
889
890             fn super_user_type_annotation(
891                 &mut self,
892                 _ty: & $($mutability)* UserTypeAnnotation<'tcx>,
893             ) {
894             }
895
896             fn super_ty(&mut self, _ty: & $($mutability)* Ty<'tcx>) {
897             }
898
899             fn super_region(&mut self, _region: & $($mutability)* ty::Region<'tcx>) {
900             }
901
902             fn super_const(&mut self, _const: & $($mutability)* &'tcx ty::Const<'tcx>) {
903             }
904
905             fn super_substs(&mut self, _substs: & $($mutability)* &'tcx Substs<'tcx>) {
906             }
907
908             fn super_generator_substs(&mut self,
909                                       _substs: & $($mutability)* GeneratorSubsts<'tcx>) {
910             }
911
912             fn super_closure_substs(&mut self,
913                                     _substs: & $($mutability)* ClosureSubsts<'tcx>) {
914             }
915
916             // Convenience methods
917
918             fn visit_location(&mut self, mir: & $($mutability)* Mir<'tcx>, location: Location) {
919                 let basic_block = & $($mutability)* mir[location.block];
920                 if basic_block.statements.len() == location.statement_index {
921                     if let Some(ref $($mutability)* terminator) = basic_block.terminator {
922                         self.visit_terminator(location.block, terminator, location)
923                     }
924                 } else {
925                     let statement = & $($mutability)*
926                         basic_block.statements[location.statement_index];
927                     self.visit_statement(location.block, statement, location)
928                 }
929             }
930         }
931     }
932 }
933
934 make_mir_visitor!(Visitor,);
935 make_mir_visitor!(MutVisitor,mut);
936
937 pub trait MirVisitable<'tcx> {
938     fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>);
939 }
940
941 impl<'tcx> MirVisitable<'tcx> for Statement<'tcx> {
942     fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>)
943     {
944         visitor.visit_statement(location.block, self, location)
945     }
946 }
947
948 impl<'tcx> MirVisitable<'tcx> for Terminator<'tcx> {
949     fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>)
950     {
951         visitor.visit_terminator(location.block, self, location)
952     }
953 }
954
955 impl<'tcx> MirVisitable<'tcx> for Option<Terminator<'tcx>> {
956     fn apply(&self, location: Location, visitor: &mut dyn Visitor<'tcx>)
957     {
958         visitor.visit_terminator(location.block, self.as_ref().unwrap(), location)
959     }
960 }
961
962 /// Extra information passed to `visit_ty` and friends to give context
963 /// about where the type etc appears.
964 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
965 pub enum TyContext {
966     LocalDecl {
967         /// The index of the local variable we are visiting.
968         local: Local,
969
970         /// The source location where this local variable was declared.
971         source_info: SourceInfo,
972     },
973
974     /// The return type of the function.
975     ReturnTy(SourceInfo),
976
977     YieldTy(SourceInfo),
978
979     /// A type found at some location.
980     Location(Location),
981 }
982
983 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
984 pub enum NonMutatingUseContext<'tcx> {
985     /// Being inspected in some way, like loading a len.
986     Inspect,
987     /// Consumed as part of an operand.
988     Copy,
989     /// Consumed as part of an operand.
990     Move,
991     /// Shared borrow.
992     SharedBorrow(Region<'tcx>),
993     /// Shallow borrow.
994     ShallowBorrow(Region<'tcx>),
995     /// Unique borrow.
996     UniqueBorrow(Region<'tcx>),
997     /// Used as base for another place, e.g. `x` in `x.y`. Will not mutate the place.
998     /// For example, the projection `x.y` is not marked as a mutation in these cases:
999     ///
1000     ///     z = x.y;
1001     ///     f(&x.y);
1002     ///
1003     Projection,
1004 }
1005
1006 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1007 pub enum MutatingUseContext<'tcx> {
1008     /// Appears as LHS of an assignment.
1009     Store,
1010     /// Can often be treated as a `Store`, but needs to be separate because
1011     /// ASM is allowed to read outputs as well, so a `Store`-`AsmOutput` sequence
1012     /// cannot be simplified the way a `Store`-`Store` can be.
1013     AsmOutput,
1014     /// Destination of a call.
1015     Call,
1016     /// Being dropped.
1017     Drop,
1018     /// Mutable borrow.
1019     Borrow(Region<'tcx>),
1020     /// Used as base for another place, e.g. `x` in `x.y`. Could potentially mutate the place.
1021     /// For example, the projection `x.y` is marked as a mutation in these cases:
1022     ///
1023     ///     x.y = ...;
1024     ///     f(&mut x.y);
1025     ///
1026     Projection,
1027     /// Retagging, a "Stacked Borrows" shadow state operation
1028     Retag,
1029 }
1030
1031 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1032 pub enum NonUseContext {
1033     /// Starting a storage live range.
1034     StorageLive,
1035     /// Ending a storage live range.
1036     StorageDead,
1037     /// User type annotation assertions for NLL.
1038     AscribeUserTy,
1039 }
1040
1041 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
1042 pub enum PlaceContext<'tcx> {
1043     NonMutatingUse(NonMutatingUseContext<'tcx>),
1044     MutatingUse(MutatingUseContext<'tcx>),
1045     NonUse(NonUseContext),
1046 }
1047
1048 impl<'tcx> PlaceContext<'tcx> {
1049     /// Returns `true` if this place context represents a drop.
1050     pub fn is_drop(&self) -> bool {
1051         match *self {
1052             PlaceContext::MutatingUse(MutatingUseContext::Drop) => true,
1053             _ => false,
1054         }
1055     }
1056
1057     /// Returns `true` if this place context represents a borrow.
1058     pub fn is_borrow(&self) -> bool {
1059         match *self {
1060             PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow(..)) |
1061             PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow(..)) |
1062             PlaceContext::NonMutatingUse(NonMutatingUseContext::UniqueBorrow(..)) |
1063             PlaceContext::MutatingUse(MutatingUseContext::Borrow(..)) => true,
1064             _ => false,
1065         }
1066     }
1067
1068     /// Returns `true` if this place context represents a storage live or storage dead marker.
1069     pub fn is_storage_marker(&self) -> bool {
1070         match *self {
1071             PlaceContext::NonUse(NonUseContext::StorageLive) |
1072             PlaceContext::NonUse(NonUseContext::StorageDead) => true,
1073             _ => false,
1074         }
1075     }
1076
1077     /// Returns `true` if this place context represents a storage live marker.
1078     pub fn is_storage_live_marker(&self) -> bool {
1079         match *self {
1080             PlaceContext::NonUse(NonUseContext::StorageLive) => true,
1081             _ => false,
1082         }
1083     }
1084
1085     /// Returns `true` if this place context represents a storage dead marker.
1086     pub fn is_storage_dead_marker(&self) -> bool {
1087         match *self {
1088             PlaceContext::NonUse(NonUseContext::StorageDead) => true,
1089             _ => false,
1090         }
1091     }
1092
1093     /// Returns `true` if this place context represents a use that potentially changes the value.
1094     pub fn is_mutating_use(&self) -> bool {
1095         match *self {
1096             PlaceContext::MutatingUse(..) => true,
1097             _ => false,
1098         }
1099     }
1100
1101     /// Returns `true` if this place context represents a use that does not change the value.
1102     pub fn is_nonmutating_use(&self) -> bool {
1103         match *self {
1104             PlaceContext::NonMutatingUse(..) => true,
1105             _ => false,
1106         }
1107     }
1108
1109     /// Returns `true` if this place context represents a use.
1110     pub fn is_use(&self) -> bool {
1111         match *self {
1112             PlaceContext::NonUse(..) => false,
1113             _ => true,
1114         }
1115     }
1116
1117     /// Returns `true` if this place context represents an assignment statement.
1118     pub fn is_place_assignment(&self) -> bool {
1119         match *self {
1120             PlaceContext::MutatingUse(MutatingUseContext::Store) |
1121             PlaceContext::MutatingUse(MutatingUseContext::Call) |
1122             PlaceContext::MutatingUse(MutatingUseContext::AsmOutput) => true,
1123             _ => false,
1124         }
1125     }
1126 }