1use std::fmt::Debug;
14use std::mem;
15use std::path::PathBuf;
16use std::{any::Any, hash::Hash};
17
18use crate::ast::from_rustc;
19use crate::ast::from_rustc::{
20 AttributeKind, DeprecatedSince, Deprecation, Ident, LangItem, OptimizeAttr,
21};
22use crate::ast::*;
23use crate::ids::{Idx, IndexVec};
24use derive_generic_visitor::*;
25
26#[visitable_group(
45 visitor(drive(&VisitAst)),
47 visitor(drive_mut(&mut VisitAstMut)),
48 visitor(drive_two(&two ZipAst)),
49 skip_but_eq(
51 (),
52 Abi,
53 AsmKind,
54 AsmOperandId,
55 AsmOptions,
56 AsmRegister,
57 BranchId,
58 BuiltinImplData,
59 BuiltinPathElem,
60 CallSafety,
61 DeprecatedSince,
62 DropKind,
63 Error,
64 FieldPredecessor,
65 FileName,
66 GlobalKind,
67 ItemOpacity,
68 LangItem,
69 LifetimeMutability,
70 OptimizeAttr,
71 OverflowMode,
72 PathBuf,
73 ReprOptions,
74 RuntimeChecks,
75 String,
76 UnwindTerminateReason,
77 Variance,
78 WithRetag,
79 bool,
80 char,
81 crate::options::Preset,
82 from_rustc::Linkage,
83 i128,
84 std::ops::RangeInclusive<IntegerValue>,
85 u128,
86 u32,
87 u64,
88 u8,
89 ullbc_ast::UnwindKind,
90 usize,
91 ustr::Ustr,
92 ),
93 skip(
95 DeclarationGroup,
96 ImplementsMarkerTraits,
97 PredicateOrigin,
98 TargetInfo,
99 TypeInfo,
100 llbc_ast::BlockId,
101 llbc_ast::StatementId,
102 ),
103 drive(
105 AsmOperand,
106 AsmTemplatePiece,
107 Assert,
108 AssocConstId,
109 AssocItemId,
110 AssocItemNames,
111 AssocTypeId,
112 AttributeKind,
113 BinOp,
114 BinderKind,
115 BorrowKind,
116 BorrowckStatement,
117 BuiltinAdt,
118 BuiltinAssertKind,
119 Byte,
120 Call,
121 CastKind,
122 ClosureInfo,
123 ClosureKind,
124 ConstGenericParam,
125 ConstGenericVarId,
126 ConstProjectionElem,
127 Deprecation,
128 Disambiguator,
129 Discriminator,
130 DynPredicate,
131 Field,
132 FieldId,
133 File,
134 FloatTy,
135 FloatValue,
136 FnOperand,
137 FnPtrKind,
138 GlobalPlaceKind,
139 GlobalProvenance,
140 Ident,
141 InlineAsm,
142 InlineAttr,
143 IntTy,
144 IntegerTy,
145 IntegerValue,
146 ItemByVal,
147 ItemId,
148 LinkedExtern,
149 Loc,
150 Local,
151 LocalId,
152 Locals,
153 MaybeAssocItemId,
154 NullOp,
155 OffsetExpr,
156 OffsetGuarantee,
157 OutlivesPred,
158 PathElem,
159 PlaceKind,
160 PlaceTypeConstraint,
161 PtrMetadata,
162 RawAttribute,
163 RefKind,
164 RegionArgs,
165 RegionId,
166 ScalarTy,
167 SerializedSpan,
168 Size,
169 SpanData,
170 SwitchData,
171 SwitchScrutinee,
172 TraitItemName,
173 TraitMethodId,
174 TranslatedCrate,
175 TypeDeclKind,
176 TypeParam,
177 TypePattern,
178 TypeVarId,
179 UIntTy,
180 UnOp,
181 UnsizingMetadata,
182 VTableDecl,
183 VTableField,
184 Variant,
185 VariantId,
186 VariantLayout,
187 from_rustc::InlineAttr,
188 llbc_ast::ExprBody,
189 llbc_ast::StatementKind,
190 ullbc_ast::BlockData,
191 ullbc_ast::BlockId,
192 ullbc_ast::ExprBody,
193 ullbc_ast::StatementKind,
194 ullbc_ast::TerminatorKind,
195 for<A: AstVisitable, B: AstVisitable, C: AstVisitable> (A, B, C),
196 for<A: AstVisitable, B: AstVisitable> (A, B),
197 for<A: AstVisitable, B: AstVisitable> Result<A, B>,
198 for<I: Idx, T: AstVisitable> IndexMap<I, T>,
199 for<I: Idx, T: AstVisitable> IndexVec<I, T>,
200 for<Id: AstVisitable> DeclRef<Id>,
201 for<T: AstVisitable + HashConsable> HashConsed<T>,
202 for<T: AstVisitable> Box<T>,
203 for<T: AstVisitable> Option<T>,
204 for<T: AstVisitable> Vec<T>,
205 ),
206 override(
209 AbortKind,
210 AggregateKind,
211 AttrInfo,
212 Attribute,
213 Body,
214 ConstantExpr,
215 ConstantExprKind,
216 DeBruijnId,
217 FileId,
218 FnPtr,
219 FunDecl,
220 FunDeclId,
221 FunDeclRef,
222 FunSig,
223 FunSource,
224 GenericArgs,
225 GenericParams,
226 GlobalDecl,
227 GlobalDeclId,
228 GlobalDeclRef,
229 GlobalPlace,
230 GlobalSource,
231 ImplElem,
232 InhabitedPredicate,
233 InhabitedPredicateKind,
234 ItemMeta,
235 Layout,
236 MetadataValue,
237 Name,
238 Operand,
239 Place,
240 PolyTraitRef,
241 ProjectionElem,
242 Region,
243 RegionParam,
244 Rvalue,
245 SizeExpr,
246 SizeExprKind,
247 Span,
248 TargetLayouts,
249 TraitAssocConst,
250 TraitAssocTy,
251 TraitAssocTyImpl,
252 TraitClauseId,
253 TraitDecl,
254 TraitDeclId,
255 TraitDeclRef,
256 TraitDeclSource,
257 TraitImpl,
258 TraitImplId,
259 TraitImplRef,
260 TraitImplSource,
261 TraitMethod,
262 TraitParam,
263 TraitRef,
264 TraitRefContents,
265 TraitRefKind,
266 TraitTypeConstraint,
267 Ty,
268 TyKind,
269 TypeDecl,
270 TypeDeclId,
271 TypeDeclRef,
272 TypeSource,
273 llbc_block: llbc_ast::Block,
274 llbc_statement: llbc_ast::Statement,
275 ullbc_statement: ullbc_ast::Statement,
276 ullbc_terminator: ullbc_ast::Terminator,
277 for<T: TypeLevelVar> DeBruijnVar<T>,
278 for<T: AstVisitable> Binder<T>,
279 for<T: AstVisitable> RegionBinder<T>,
280 for<T: AstVisitable> WithCachedTypeInfo<T>,
281 )
282)]
283pub trait AstVisitable: Any {
284 fn name(&self) -> &'static str {
286 std::any::type_name::<Self>()
287 }
288 fn dyn_visit<T: AstVisitable>(&self, f: impl FnMut(&T))
290 where
291 Self: Sized,
292 {
293 let _ = VisitAst::visit(&mut DynVisitor::new_shared::<T>(f), self);
294 }
295 fn dyn_visit_mut<T: AstVisitable>(&mut self, f: impl FnMut(&mut T))
297 where
298 Self: Sized,
299 {
300 let _ = VisitAstMut::visit(&mut DynVisitor::new_mut::<T>(f), self);
301 }
302}
303
304pub trait MergeValue: Sized {
307 fn merge(&mut self, other: Self);
308}
309impl<T: PartialEq + Debug> MergeValue for T {
310 fn merge(&mut self, other: Self) {
311 assert_eq!(*self, other, "cannot merge two different values of a map");
312 }
313}
314impl MergeValue for TargetLayouts {
315 fn merge(&mut self, other: Self) {
316 other.0.into_iter().for_each(|(k, v)| {
317 assert!(
318 !self.contains_key(&k),
319 "cannot merge overlapping TargetLayouts"
320 );
321 self.insert(k, v);
322 });
323 }
324}
325
326fn insert_or_merge<K: Hash + Eq, T: MergeValue>(map: &mut SeqHashMap<K, T>, k: K, x: T) {
328 use indexmap::map::Entry;
329 match map.entry(k) {
330 Entry::Occupied(mut entry) => entry.get_mut().merge(x),
331 Entry::Vacant(entry) => {
332 entry.insert(x);
333 }
334 }
335}
336
337impl<K: AstVisitable + Hash + Eq, T: AstVisitable + MergeValue> AstVisitable for SeqHashMap<K, T> {
339 fn drive<V: VisitAst>(&self, v: &mut V) -> ControlFlow<V::Break> {
340 for (k, x) in self {
341 v.visit(k)?;
342 v.visit(x)?;
343 }
344 Continue(())
345 }
346 fn drive_mut<V: VisitAstMut>(&mut self, v: &mut V) -> ControlFlow<V::Break> {
347 for (mut k, mut x) in mem::take(self) {
348 v.visit(&mut k)?;
349 v.visit(&mut x)?;
350 insert_or_merge(self, k, x);
351 }
352 Continue(())
353 }
354 fn drive_two<V: ZipAst>(&self, other: &Self, v: &mut V) -> ControlFlow<V::Break> {
355 if self.len() != other.len() {
356 return Break(Default::default());
357 }
358 for ((key, value), (other_key, other_value)) in self.iter().zip(other) {
359 v.visit(key, other_key)?;
360 v.visit(value, other_value)?;
361 }
362 Continue(())
363 }
364}
365impl<K: BodyVisitable + Hash + Eq, T: BodyVisitable + MergeValue> BodyVisitable
366 for SeqHashMap<K, T>
367{
368 fn drive_body<V: VisitBody>(&self, v: &mut V) -> ControlFlow<V::Break> {
369 for (k, x) in self {
370 v.visit(k)?;
371 v.visit(x)?;
372 }
373 Continue(())
374 }
375 fn drive_body_mut<V: VisitBodyMut>(&mut self, v: &mut V) -> ControlFlow<V::Break> {
376 for (mut k, mut x) in mem::take(self) {
377 v.visit(&mut k)?;
378 v.visit(&mut x)?;
379 insert_or_merge(self, k, x);
380 }
381 Continue(())
382 }
383}
384
385#[visitable_group(
398 visitor(drive_body(&VisitBody)),
400 visitor(drive_body_mut(&mut VisitBodyMut)),
401 skip(
403 (),
404 AbortKind,
405 AsmKind,
406 AsmOperandId,
407 AsmOptions,
408 AsmRegister,
409 AsmTemplatePiece,
410 BinOp,
411 BorrowKind,
412 BranchId,
413 BuiltinAdt,
414 BuiltinPathElem,
415 CallSafety,
416 ConstantExpr,
417 DropKind,
418 Error,
419 FieldId,
420 FnPtrKind,
421 FunDeclId,
422 FunDeclRef,
423 GenericArgs,
424 GlobalDeclRef,
425 IntTy,
426 IntegerTy,
427 IntegerValue,
428 LinkedExtern,
429 Name,
430 NullOp,
431 OutlivesPred,
432 PathBuf,
433 PolyTraitRef,
434 RefKind,
435 Region,
436 RegionArgs,
437 RegionId,
438 ScalarTy,
439 Span,
440 String,
441 TraitRef,
442 Ty,
443 TypeDeclId,
444 TypeDeclRef,
445 UIntTy,
446 UnOp,
447 UnwindTerminateReason,
448 Variance,
449 VariantId,
450 WithRetag,
451 bool,
452 llbc_ast::BlockId,
453 llbc_ast::StatementId,
454 u32,
455 ullbc_ast::UnwindKind,
456 usize,
457 ),
458 drive(
460 Assert,
461 BorrowckStatement,
462 BuiltinAssertKind,
463 InlineAsm,
464 PlaceKind,
465 PlaceTypeConstraint,
466 SwitchData,
467 SwitchScrutinee,
468 llbc_ast::StatementKind,
469 ullbc_ast::StatementKind,
470 ullbc_ast::TerminatorKind,
471 for<A: BodyVisitable, B: BodyVisitable, C: BodyVisitable> (A, B, C),
472 for<A: BodyVisitable, B: BodyVisitable> (A, B),
473 for<I: Idx, T: BodyVisitable> IndexMap<I, T>,
474 for<I: Idx, T: BodyVisitable> IndexVec<I, T>,
475 for<T: BodyVisitable, E: BodyVisitable> Result<T, E>,
476 for<T: BodyVisitable> Box<T>,
477 for<T: BodyVisitable> GExprBody<T>,
478 for<T: BodyVisitable> Option<T>,
479 for<T: BodyVisitable> Vec<T>,
480 ),
481 override(
484 AggregateKind,
485 AsmOperand,
486 Body,
487 Call,
488 FnOperand,
489 FnPtr,
490 Local,
491 LocalId,
492 Locals,
493 Operand,
494 Place,
495 ProjectionElem,
496 Rvalue,
497 llbc_block: llbc_ast::Block,
498 llbc_statement: llbc_ast::Statement,
499 ullbc_block: ullbc_ast::BlockData,
500 ullbc_block_id: ullbc_ast::BlockId,
501 ullbc_statement: ullbc_ast::Statement,
502 ullbc_terminator: ullbc_ast::Terminator,
503 )
504)]
505pub trait BodyVisitable: Any {
506 fn dyn_visit_in_body<T: BodyVisitable>(&self, f: impl FnMut(&T))
508 where
509 Self: Sized,
510 {
511 let _ = VisitBody::visit(&mut DynVisitor::new_shared::<T>(f), self);
512 }
513
514 fn dyn_visit_in_body_mut<T: BodyVisitable>(&mut self, f: impl FnMut(&mut T))
516 where
517 Self: Sized,
518 {
519 let _ = VisitBodyMut::visit(&mut DynVisitor::new_mut::<T>(f), self);
520 }
521}
522
523#[derive(Visitor)]
526pub struct DynVisitor<F> {
527 enter: F,
528}
529impl DynVisitor<()> {
530 pub fn new_shared<T: Any>(mut f: impl FnMut(&T)) -> DynVisitor<impl FnMut(&dyn Any)> {
531 let enter = move |x: &dyn Any| {
532 if let Some(x) = x.downcast_ref::<T>() {
533 f(x);
534 }
535 };
536 DynVisitor { enter }
537 }
538 pub fn new_mut<T: Any>(mut f: impl FnMut(&mut T)) -> DynVisitor<impl FnMut(&mut dyn Any)> {
539 let enter = move |x: &mut dyn Any| {
540 if let Some(x) = x.downcast_mut::<T>() {
541 f(x);
542 }
543 };
544 DynVisitor { enter }
545 }
546}
547impl<F> VisitAst for DynVisitor<F>
548where
549 F: FnMut(&dyn Any),
550{
551 fn visit<T: AstVisitable>(&mut self, x: &T) -> ControlFlow<Self::Break> {
552 (self.enter)(x);
553 x.drive(self)?;
554 Continue(())
555 }
556}
557impl<F> VisitAstMut for DynVisitor<F>
558where
559 F: FnMut(&mut dyn Any),
560{
561 fn visit<T: AstVisitable>(&mut self, x: &mut T) -> ControlFlow<Self::Break> {
562 (self.enter)(x);
563 x.drive_mut(self)?;
564 Continue(())
565 }
566}
567impl<F> VisitBody for DynVisitor<F>
568where
569 F: FnMut(&dyn Any),
570{
571 fn visit<T: BodyVisitable>(&mut self, x: &T) -> ControlFlow<Self::Break> {
572 (self.enter)(x);
573 x.drive_body(self)?;
574 Continue(())
575 }
576}
577impl<F> VisitBodyMut for DynVisitor<F>
578where
579 F: FnMut(&mut dyn Any),
580{
581 fn visit<T: BodyVisitable>(&mut self, x: &mut T) -> ControlFlow<Self::Break> {
582 (self.enter)(x);
583 x.drive_body_mut(self)?;
584 Continue(())
585 }
586}
587
588pub use wrappers::*;
589mod wrappers {
590 use std::mem;
604
605 use crate::ast::*;
606 use derive_generic_visitor::*;
607
608 #[repr(transparent)]
614 pub struct DontLeakImplDetails<V>(V);
615
616 impl<V> DontLeakImplDetails<V> {
617 pub fn new(v: &mut V) -> &mut Self {
618 unsafe { std::mem::transmute(v) }
620 }
621 pub fn inner(&mut self) -> &mut V {
622 unsafe { std::mem::transmute(self) }
624 }
625 }
626
627 impl<V: Visitor> Visitor for DontLeakImplDetails<V> {
628 type Break = V::Break;
629 }
630 impl<V: VisitAst> VisitAst for DontLeakImplDetails<V> {
631 fn visit_inner<T>(&mut self, x: &T) -> ControlFlow<Self::Break>
633 where
634 T: AstVisitable,
635 {
636 x.drive(self.inner())
637 }
638 }
639 impl<V: VisitAstMut> VisitAstMut for DontLeakImplDetails<V> {
640 fn visit_inner<T>(&mut self, x: &mut T) -> ControlFlow<Self::Break>
642 where
643 T: AstVisitable,
644 {
645 x.drive_mut(self.inner())
646 }
647 }
648
649 #[repr(transparent)]
660 pub struct VisitWithBinderDepth<V>(V);
661
662 impl<V: VisitorWithBinderDepth> VisitWithBinderDepth<V> {
663 pub fn new(v: &mut V) -> &mut Self {
664 unsafe { std::mem::transmute(v) }
666 }
667 pub fn inner(&mut self) -> &mut V {
668 unsafe { std::mem::transmute(self) }
670 }
671 }
672
673 pub trait VisitorWithBinderDepth {
674 fn binder_depth_mut(&mut self) -> &mut DeBruijnId;
675 }
676
677 impl<V: Visitor> Visitor for VisitWithBinderDepth<V> {
678 type Break = V::Break;
679 }
680 impl<V: VisitAst + VisitorWithBinderDepth> VisitAst for VisitWithBinderDepth<V> {
681 fn visit_inner<T>(&mut self, x: &T) -> ControlFlow<Self::Break>
682 where
683 T: AstVisitable,
684 {
685 x.drive(self.inner())
686 }
687 fn enter_region_binder<T: AstVisitable>(&mut self, _: &RegionBinder<T>) {
688 let binder_depth = self.0.binder_depth_mut();
689 *binder_depth = binder_depth.incr()
690 }
691 fn exit_region_binder<T: AstVisitable>(&mut self, _: &RegionBinder<T>) {
692 let binder_depth = self.0.binder_depth_mut();
693 *binder_depth = binder_depth.decr()
694 }
695 fn enter_binder<T: AstVisitable>(&mut self, _: &Binder<T>) {
696 let binder_depth = self.0.binder_depth_mut();
697 *binder_depth = binder_depth.incr()
698 }
699 fn exit_binder<T: AstVisitable>(&mut self, _: &Binder<T>) {
700 let binder_depth = self.0.binder_depth_mut();
701 *binder_depth = binder_depth.decr()
702 }
703 }
704 impl<V: VisitAstMut + VisitorWithBinderDepth> VisitAstMut for VisitWithBinderDepth<V> {
705 fn visit_inner<T>(&mut self, x: &mut T) -> ControlFlow<Self::Break>
706 where
707 T: AstVisitable,
708 {
709 x.drive_mut(self.inner())
710 }
711 fn enter_region_binder<T: AstVisitable>(&mut self, _: &mut RegionBinder<T>) {
712 let binder_depth = self.0.binder_depth_mut();
713 *binder_depth = binder_depth.incr()
714 }
715 fn exit_region_binder<T: AstVisitable>(&mut self, _: &mut RegionBinder<T>) {
716 let binder_depth = self.0.binder_depth_mut();
717 *binder_depth = binder_depth.decr()
718 }
719 fn enter_binder<T: AstVisitable>(&mut self, _: &mut Binder<T>) {
720 let binder_depth = self.0.binder_depth_mut();
721 *binder_depth = binder_depth.incr()
722 }
723 fn exit_binder<T: AstVisitable>(&mut self, _: &mut Binder<T>) {
724 let binder_depth = self.0.binder_depth_mut();
725 *binder_depth = binder_depth.decr()
726 }
727 }
728
729 pub struct VisitWithVariance<'a, 'ctx, V> {
732 inner: &'a mut V,
733 krate: &'ctx TranslatedCrate,
734 }
735
736 impl<'a, 'ctx, V: VisitorWithVariance> VisitWithVariance<'a, 'ctx, V> {
737 pub fn new(inner: &'a mut V, krate: &'ctx TranslatedCrate) -> Self {
738 Self { inner, krate }
739 }
740 }
741
742 pub trait VisitorWithVariance {
743 fn ambient_variance_mut(&mut self) -> &mut Variance;
744 }
745
746 impl<V: Visitor> Visitor for VisitWithVariance<'_, '_, V> {
747 type Break = V::Break;
748 }
749
750 impl<V: VisitAst + VisitorWithVariance> VisitWithVariance<'_, '_, V> {
751 fn with(
752 &mut self,
753 variance: Variance,
754 f: impl FnOnce(&mut Self) -> ControlFlow<V::Break>,
755 ) -> ControlFlow<V::Break> {
756 let old = *self.inner.ambient_variance_mut();
757 *self.inner.ambient_variance_mut() = Variance::compose(old, variance);
758 let result = f(self);
759 *self.inner.ambient_variance_mut() = old;
760 result
761 }
762
763 fn visit_with<T: AstVisitable>(
764 &mut self,
765 variance: Variance,
766 value: &T,
767 ) -> ControlFlow<V::Break> {
768 self.with(variance, |this| this.visit(value))
769 }
770
771 fn visit_inner_with(&mut self, variance: Variance, ty: &Ty) -> ControlFlow<V::Break> {
772 self.with(variance, |this| this.visit_inner(ty))
773 }
774 }
775
776 impl<V: VisitAst + VisitorWithVariance> VisitAst for VisitWithVariance<'_, '_, V> {
777 fn visit_inner<T: AstVisitable>(&mut self, value: &T) -> ControlFlow<Self::Break> {
778 value.drive(self.inner)
779 }
780
781 fn visit_ty(&mut self, ty: &Ty) -> ControlFlow<Self::Break> {
782 match ty.kind() {
783 TyKind::Ref(region, inner, kind) => {
784 self.visit(region)?;
785 match kind {
786 RefKind::Shared => self.visit(inner)?,
787 RefKind::Mut => self.visit_with(Variance::Invariant, inner)?,
788 }
789 }
790 TyKind::RawPtr(inner, RefKind::Shared) => self.visit(inner)?,
791 TyKind::RawPtr(inner, RefKind::Mut) => {
792 self.visit_with(Variance::Invariant, inner)?;
793 }
794 TyKind::FnPtr(..)
795 | TyKind::Adt(..)
796 | TyKind::Array(..)
797 | TyKind::Slice(..)
798 | TyKind::Pattern(..) => {
799 self.visit_inner(ty)?;
800 }
801 TyKind::TraitType(..)
802 | TyKind::DynTrait(..)
803 | TyKind::FnDef(..)
804 | TyKind::PtrMetadata(..) => {
805 self.visit_inner_with(Variance::Invariant, ty)?;
806 }
807 TyKind::TypeVar(..) | TyKind::Scalar(..) | TyKind::Never | TyKind::Error(..) => {}
808 }
809 Continue(())
810 }
811
812 fn visit_fun_sig(&mut self, sig: &FunSig) -> ControlFlow<Self::Break> {
813 for input in &sig.inputs {
814 self.visit_with(Variance::Contravariant, input)?;
815 }
816 self.visit(&sig.output)?;
817 Continue(())
818 }
819
820 fn visit_type_decl_ref(&mut self, type_ref: &TypeDeclRef) -> ControlFlow<Self::Break> {
821 if let Some(decl) = self.krate.type_decls.get(type_ref.id) {
822 let params = &decl.generics;
823 for (param, region) in params.regions.iter().zip(&type_ref.generics.regions) {
824 self.visit_with(param.variance, region)?;
825 }
826 for (param, ty) in params.types.iter().zip(&type_ref.generics.types) {
827 self.visit_with(param.variance, ty)?;
828 }
829 } else {
830 self.visit_with(Variance::Covariant, &type_ref.generics.regions)?;
831 self.visit_with(Variance::Covariant, &type_ref.generics.types)?;
832 }
833 Continue(())
834 }
835 }
836
837 #[repr(transparent)]
839 pub struct VisitWithItem<V>(V);
840
841 impl<V> VisitWithItem<V> {
842 pub fn new(v: &mut V) -> &mut Self {
843 unsafe { std::mem::transmute(v) }
845 }
846 pub fn inner(&mut self) -> &mut V {
847 unsafe { std::mem::transmute(self) }
849 }
850 }
851
852 pub trait VisitorWithItem: VisitAst {
853 fn enter_item(&mut self, _item: ItemRef<'_>) {}
854 fn exit_item(&mut self, _item: ItemRef<'_>) {}
855 fn visit_item(&mut self, item: ItemRef<'_>) -> ControlFlow<Self::Break> {
856 self.enter_item(item);
857 item.drive(self)?;
858 self.exit_item(item);
859 Continue(())
860 }
861 }
862 pub trait VisitorWithItemMut: VisitAstMut {
863 fn enter_item(&mut self, _item: ItemRefMut<'_>) {}
864 fn exit_item(&mut self, _item: ItemRefMut<'_>) {}
865 fn visit_item(&mut self, mut item: ItemRefMut<'_>) -> ControlFlow<Self::Break> {
866 self.enter_item(item.reborrow());
867 item.drive_mut(self)?;
868 self.exit_item(item);
869 Continue(())
870 }
871 }
872
873 impl<V: Visitor> Visitor for VisitWithItem<V> {
874 type Break = V::Break;
875 }
876 impl<V: VisitAst + VisitorWithItem> VisitAst for VisitWithItem<V> {
877 fn visit_inner<T>(&mut self, x: &T) -> ControlFlow<Self::Break>
878 where
879 T: AstVisitable,
880 {
881 x.drive(self.inner())
882 }
883 fn visit_fun_decl(&mut self, x: &FunDecl) -> ControlFlow<Self::Break> {
884 self.0.visit_item(ItemRef::Fun(x))
885 }
886 fn visit_type_decl(&mut self, x: &TypeDecl) -> ControlFlow<Self::Break> {
887 self.0.visit_item(ItemRef::Type(x))
888 }
889 fn visit_global_decl(&mut self, x: &GlobalDecl) -> ControlFlow<Self::Break> {
890 self.0.visit_item(ItemRef::Global(x))
891 }
892 fn visit_trait_decl(&mut self, x: &TraitDecl) -> ControlFlow<Self::Break> {
893 self.0.visit_item(ItemRef::TraitDecl(x))
894 }
895 fn visit_trait_impl(&mut self, x: &TraitImpl) -> ControlFlow<Self::Break> {
896 self.0.visit_item(ItemRef::TraitImpl(x))
897 }
898 }
899 impl<V: VisitAstMut + VisitorWithItemMut> VisitAstMut for VisitWithItem<V> {
900 fn visit_inner<T>(&mut self, x: &mut T) -> ControlFlow<Self::Break>
901 where
902 T: AstVisitable,
903 {
904 x.drive_mut(self.inner())
905 }
906 fn visit_fun_decl(&mut self, x: &mut FunDecl) -> ControlFlow<Self::Break> {
907 self.0.visit_item(ItemRefMut::Fun(x))
908 }
909 fn visit_type_decl(&mut self, x: &mut TypeDecl) -> ControlFlow<Self::Break> {
910 self.0.visit_item(ItemRefMut::Type(x))
911 }
912 fn visit_global_decl(&mut self, x: &mut GlobalDecl) -> ControlFlow<Self::Break> {
913 self.0.visit_item(ItemRefMut::Global(x))
914 }
915 fn visit_trait_decl(&mut self, x: &mut TraitDecl) -> ControlFlow<Self::Break> {
916 self.0.visit_item(ItemRefMut::TraitDecl(x))
917 }
918 fn visit_trait_impl(&mut self, x: &mut TraitImpl) -> ControlFlow<Self::Break> {
919 self.0.visit_item(ItemRefMut::TraitImpl(x))
920 }
921 }
922
923 #[repr(transparent)]
925 pub struct VisitWithItemRef<V>(V);
926
927 impl<V> VisitWithItemRef<V> {
928 pub fn new(v: &mut V) -> &mut Self {
929 unsafe { std::mem::transmute(v) }
931 }
932 pub fn inner(&mut self) -> &mut V {
933 unsafe { std::mem::transmute(self) }
935 }
936 }
937
938 pub trait VisitorWithItemRef: VisitAst {
939 fn enter_item_ref(&mut self, _item_id: ItemId, _args: &GenericArgs) {}
940 fn exit_item_ref(&mut self, _item_id: ItemId, _args: &GenericArgs) {}
941 fn visit_item_ref(
942 &mut self,
943 item_id: ItemId,
944 args: &GenericArgs,
945 ) -> ControlFlow<Self::Break> {
946 self.enter_item_ref(item_id, args);
947 self.visit_inner(args)?;
948 self.exit_item_ref(item_id, args);
949 Continue(())
950 }
951 }
952 pub trait VisitorWithItemRefMut: VisitAstMut {
953 fn enter_item_ref(&mut self, _item_id: ItemId, _args: &mut GenericArgs) {}
954 fn exit_item_ref(&mut self, _item_id: ItemId, _args: &mut GenericArgs) {}
955 fn visit_item_ref(
956 &mut self,
957 item_id: ItemId,
958 args: &mut GenericArgs,
959 ) -> ControlFlow<Self::Break> {
960 self.enter_item_ref(item_id, args);
961 self.visit_inner(args)?;
962 self.exit_item_ref(item_id, args);
963 Continue(())
964 }
965 }
966
967 impl<V: Visitor> Visitor for VisitWithItemRef<V> {
968 type Break = V::Break;
969 }
970 impl<V: VisitAst + VisitorWithItemRef> VisitAst for VisitWithItemRef<V> {
971 fn visit_inner<T>(&mut self, x: &T) -> ControlFlow<Self::Break>
972 where
973 T: AstVisitable,
974 {
975 x.drive(self.inner())
976 }
977 fn visit_type_decl_ref(&mut self, x: &TypeDeclRef) -> ControlFlow<Self::Break> {
978 self.0.visit_item_ref(ItemId::Type(x.id), &x.generics)
979 }
980 fn visit_fun_decl_ref(&mut self, x: &FunDeclRef) -> ControlFlow<Self::Break> {
981 self.0.visit_item_ref(ItemId::Fun(x.id), &x.generics)
982 }
983 fn visit_global_decl_ref(&mut self, x: &GlobalDeclRef) -> ControlFlow<Self::Break> {
984 self.0.visit_item_ref(ItemId::Global(x.id), &x.generics)
985 }
986 fn visit_trait_decl_ref(&mut self, x: &TraitDeclRef) -> ControlFlow<Self::Break> {
987 self.0.visit_item_ref(ItemId::TraitDecl(x.id), &x.generics)
988 }
989 fn visit_trait_impl_ref(&mut self, x: &TraitImplRef) -> ControlFlow<Self::Break> {
990 self.0.visit_item_ref(ItemId::TraitImpl(x.id), &x.generics)
991 }
992 fn visit_fn_ptr(&mut self, x: &FnPtr) -> ControlFlow<Self::Break> {
993 match x.kind.as_ref() {
994 FnPtrKind::Fun(id) => self.0.visit_item_ref(ItemId::Fun(*id), &x.generics),
995 FnPtrKind::Trait(..) => self.visit_inner(x),
996 }
997 }
998 }
999 impl<V: VisitAstMut + VisitorWithItemRefMut> VisitAstMut for VisitWithItemRef<V> {
1000 fn visit_inner<T>(&mut self, x: &mut T) -> ControlFlow<Self::Break>
1001 where
1002 T: AstVisitable,
1003 {
1004 x.drive_mut(self.inner())
1005 }
1006 fn visit_type_decl_ref(&mut self, x: &mut TypeDeclRef) -> ControlFlow<Self::Break> {
1007 self.0.visit_item_ref(ItemId::Type(x.id), &mut x.generics)
1008 }
1009 fn visit_fun_decl_ref(&mut self, x: &mut FunDeclRef) -> ControlFlow<Self::Break> {
1010 self.0.visit_item_ref(ItemId::Fun(x.id), &mut x.generics)
1011 }
1012 fn visit_global_decl_ref(&mut self, x: &mut GlobalDeclRef) -> ControlFlow<Self::Break> {
1013 self.0.visit_item_ref(ItemId::Global(x.id), &mut x.generics)
1014 }
1015 fn visit_trait_decl_ref(&mut self, x: &mut TraitDeclRef) -> ControlFlow<Self::Break> {
1016 self.0
1017 .visit_item_ref(ItemId::TraitDecl(x.id), &mut x.generics)
1018 }
1019 fn visit_trait_impl_ref(&mut self, x: &mut TraitImplRef) -> ControlFlow<Self::Break> {
1020 self.0
1021 .visit_item_ref(ItemId::TraitImpl(x.id), &mut x.generics)
1022 }
1023 fn visit_fn_ptr(&mut self, x: &mut FnPtr) -> ControlFlow<Self::Break> {
1024 match x.kind.as_ref() {
1025 FnPtrKind::Fun(id) => self.0.visit_item_ref(ItemId::Fun(*id), &mut x.generics),
1026 FnPtrKind::Trait(..) => self.visit_inner(x),
1027 }
1028 }
1029 }
1030
1031 #[repr(transparent)]
1033 pub struct VisitWithBinderStack<V>(V);
1034
1035 impl<V: VisitorWithBinderStack> VisitWithBinderStack<V> {
1036 fn wrap(v: &mut V) -> &mut Self {
1038 unsafe { std::mem::transmute(v) }
1040 }
1041 pub fn new(v: &mut V) -> &mut VisitWithItem<DontLeakImplDetails<Self>> {
1042 VisitWithItem::new(DontLeakImplDetails::new(Self::wrap(v)))
1046 }
1047 pub fn inner(&mut self) -> &mut V {
1048 unsafe { std::mem::transmute(self) }
1050 }
1051 }
1052
1053 pub trait VisitorWithBinderStack {
1054 fn binder_stack_mut(&mut self) -> &mut BindingStack<GenericParams>;
1055 }
1056
1057 impl<V: VisitAst + VisitorWithBinderStack> VisitorWithItem
1058 for DontLeakImplDetails<VisitWithBinderStack<V>>
1059 {
1060 fn enter_item(&mut self, item: ItemRef<'_>) {
1061 self.0
1062 .0
1063 .binder_stack_mut()
1064 .push(item.generic_params().clone());
1065 }
1066 fn exit_item(&mut self, _item: ItemRef<'_>) {
1067 self.0.0.binder_stack_mut().pop();
1068 }
1069 }
1070 impl<V: VisitAstMut + VisitorWithBinderStack> VisitorWithItemMut
1071 for DontLeakImplDetails<VisitWithBinderStack<V>>
1072 {
1073 fn enter_item(&mut self, item: ItemRefMut<'_>) {
1074 self.0
1075 .0
1076 .binder_stack_mut()
1077 .push(item.as_ref().generic_params().clone());
1078 }
1079 fn exit_item(&mut self, _item: ItemRefMut<'_>) {
1080 self.0.0.binder_stack_mut().pop();
1081 }
1082 }
1083
1084 impl<V: Visitor> Visitor for VisitWithBinderStack<V> {
1085 type Break = V::Break;
1086 }
1087 impl<V: VisitAst + VisitorWithBinderStack> VisitAst for VisitWithBinderStack<V> {
1088 fn visit_inner<T>(&mut self, x: &T) -> ControlFlow<Self::Break>
1089 where
1090 T: AstVisitable,
1091 {
1092 x.drive(self.inner())
1093 }
1094 fn visit_binder<T: AstVisitable>(
1095 &mut self,
1096 binder: &Binder<T>,
1097 ) -> ControlFlow<Self::Break> {
1098 self.0.binder_stack_mut().push(binder.params.clone());
1099 let result = self.visit_inner(binder);
1100 self.0.binder_stack_mut().pop();
1101 result
1102 }
1103 fn visit_region_binder<T: AstVisitable>(
1104 &mut self,
1105 binder: &RegionBinder<T>,
1106 ) -> ControlFlow<Self::Break> {
1107 self.0.binder_stack_mut().push(binder.to_params());
1108 let result = self.visit_inner(binder);
1109 self.0.binder_stack_mut().pop();
1110 result
1111 }
1112 }
1113 impl<V: VisitAstMut + VisitorWithBinderStack> VisitAstMut for VisitWithBinderStack<V> {
1114 fn visit_inner<T>(&mut self, x: &mut T) -> ControlFlow<Self::Break>
1115 where
1116 T: AstVisitable,
1117 {
1118 x.drive_mut(self.inner())
1119 }
1120 fn visit_binder<T: AstVisitable>(
1121 &mut self,
1122 binder: &mut Binder<T>,
1123 ) -> ControlFlow<Self::Break> {
1124 self.0.binder_stack_mut().push(binder.params.clone());
1125 let result = self.visit_inner(binder);
1126 self.0.binder_stack_mut().pop();
1127 result
1128 }
1129 fn visit_region_binder<T: AstVisitable>(
1130 &mut self,
1131 binder: &mut RegionBinder<T>,
1132 ) -> ControlFlow<Self::Break> {
1133 self.0.binder_stack_mut().push(binder.to_params());
1134 let result = self.visit_inner(binder);
1135 self.0.binder_stack_mut().pop();
1136 result
1137 }
1138 }
1139
1140 #[repr(transparent)]
1142 pub struct VisitWithSpan<V>(V);
1143
1144 impl<V: VisitorWithSpan> VisitWithSpan<V> {
1145 fn wrap(v: &mut V) -> &mut Self {
1147 unsafe { std::mem::transmute(v) }
1149 }
1150 pub fn new(v: &mut V) -> &mut VisitWithItem<DontLeakImplDetails<Self>> {
1151 VisitWithItem::new(DontLeakImplDetails::new(Self::wrap(v)))
1155 }
1156 pub fn inner(&mut self) -> &mut V {
1157 unsafe { std::mem::transmute(self) }
1159 }
1160 }
1161
1162 pub trait VisitorWithSpan {
1163 fn current_span(&mut self) -> &mut Span;
1164 }
1165
1166 impl<V: VisitAst + VisitorWithSpan> VisitorWithItem for DontLeakImplDetails<VisitWithSpan<V>> {
1167 fn visit_item(&mut self, item: ItemRef<'_>) -> ControlFlow<Self::Break> {
1168 let old_span = mem::replace(self.0.0.current_span(), item.item_meta().span);
1169 item.drive(self)?;
1170 *self.0.0.current_span() = old_span;
1171 Continue(())
1172 }
1173 }
1174 impl<V: VisitAstMut + VisitorWithSpan> VisitorWithItemMut
1175 for DontLeakImplDetails<VisitWithSpan<V>>
1176 {
1177 fn visit_item(&mut self, mut item: ItemRefMut<'_>) -> ControlFlow<Self::Break> {
1178 let span = item.as_ref().item_meta().span;
1179 let old_span = mem::replace(self.0.0.current_span(), span);
1180 item.drive_mut(self)?;
1181 *self.0.0.current_span() = old_span;
1182 Continue(())
1183 }
1184 }
1185
1186 impl<V: Visitor> Visitor for VisitWithSpan<V> {
1187 type Break = V::Break;
1188 }
1189 impl<V: VisitAst + VisitorWithSpan> VisitWithSpan<V> {
1190 fn visit_inner_track_span<T>(&mut self, x: &T, span: Span) -> ControlFlow<V::Break>
1191 where
1192 T: AstVisitable,
1193 T: for<'s> derive_generic_visitor::Drive<'s, AstVisitableWrapper<Self>>,
1194 {
1195 let old_span = mem::replace(self.0.current_span(), span);
1196 self.visit_inner(x)?;
1197 *self.0.current_span() = old_span;
1198 Continue(())
1199 }
1200 }
1201 impl<V: VisitAstMut + VisitorWithSpan> VisitWithSpan<V> {
1202 fn visit_inner_mut_track_span<T>(&mut self, x: &mut T, span: Span) -> ControlFlow<V::Break>
1203 where
1204 T: AstVisitable,
1205 T: for<'s> derive_generic_visitor::DriveMut<'s, AstVisitableWrapper<Self>>,
1206 {
1207 let old_span = mem::replace(self.0.current_span(), span);
1208 self.visit_inner(x)?;
1209 *self.0.current_span() = old_span;
1210 Continue(())
1211 }
1212 }
1213 impl<V: VisitAst + VisitorWithSpan> VisitAst for VisitWithSpan<V> {
1214 fn visit_inner<T>(&mut self, x: &T) -> ControlFlow<Self::Break>
1215 where
1216 T: AstVisitable,
1217 {
1218 x.drive(self.inner())
1219 }
1220 fn visit_trait_param(&mut self, x: &TraitParam) -> ControlFlow<Self::Break> {
1221 match x.span {
1222 Some(span) => self.visit_inner_track_span(x, span),
1223 None => self.visit_inner(x),
1224 }
1225 }
1226 fn visit_ullbc_statement(&mut self, x: &ullbc_ast::Statement) -> ControlFlow<Self::Break> {
1227 self.visit_inner_track_span(x, x.span)
1228 }
1229 fn visit_ullbc_terminator(
1230 &mut self,
1231 x: &ullbc_ast::Terminator,
1232 ) -> ControlFlow<Self::Break> {
1233 self.visit_inner_track_span(x, x.span)
1234 }
1235 fn visit_llbc_statement(&mut self, x: &llbc_ast::Statement) -> ControlFlow<Self::Break> {
1236 self.visit_inner_track_span(x, x.span)
1237 }
1238 fn visit_llbc_block(&mut self, x: &llbc_ast::Block) -> ControlFlow<Self::Break> {
1239 self.visit_inner_track_span(x, x.span)
1240 }
1241 }
1242 impl<V: VisitAstMut + VisitorWithSpan> VisitAstMut for VisitWithSpan<V> {
1243 fn visit_inner<T>(&mut self, x: &mut T) -> ControlFlow<Self::Break>
1244 where
1245 T: AstVisitable,
1246 {
1247 x.drive_mut(self.inner())
1248 }
1249 fn visit_trait_param(&mut self, x: &mut TraitParam) -> ControlFlow<Self::Break> {
1250 match x.span {
1251 Some(span) => self.visit_inner_mut_track_span(x, span),
1252 None => self.visit_inner(x),
1253 }
1254 }
1255 fn visit_ullbc_statement(
1256 &mut self,
1257 x: &mut ullbc_ast::Statement,
1258 ) -> ControlFlow<Self::Break> {
1259 self.visit_inner_mut_track_span(x, x.span)
1260 }
1261 fn visit_ullbc_terminator(
1262 &mut self,
1263 x: &mut ullbc_ast::Terminator,
1264 ) -> ControlFlow<Self::Break> {
1265 self.visit_inner_mut_track_span(x, x.span)
1266 }
1267 fn visit_llbc_statement(
1268 &mut self,
1269 x: &mut llbc_ast::Statement,
1270 ) -> ControlFlow<Self::Break> {
1271 self.visit_inner_mut_track_span(x, x.span)
1272 }
1273 fn visit_llbc_block(&mut self, x: &mut llbc_ast::Block) -> ControlFlow<Self::Break> {
1274 self.visit_inner_mut_track_span(x, x.span)
1275 }
1276 }
1277
1278 impl<V: VisitorWithSpan> VisitorWithSpan for VisitWithBinderStack<V> {
1280 fn current_span(&mut self) -> &mut Span {
1281 self.0.current_span()
1282 }
1283 }
1284 impl<V: VisitorWithSpan> VisitorWithSpan for VisitWithItem<V> {
1285 fn current_span(&mut self) -> &mut Span {
1286 self.0.current_span()
1287 }
1288 }
1289 impl<V: VisitorWithSpan> VisitorWithSpan for DontLeakImplDetails<V> {
1290 fn current_span(&mut self) -> &mut Span {
1291 self.0.current_span()
1292 }
1293 }
1294 impl<V: VisitorWithBinderDepth> VisitorWithBinderDepth for VisitWithItemRef<V> {
1295 fn binder_depth_mut(&mut self) -> &mut DeBruijnId {
1296 self.0.binder_depth_mut()
1297 }
1298 }
1299}