forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASTWalker.cpp
1914 lines (1613 loc) · 48.3 KB
/
ASTWalker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===--- ASTWalker.cpp - AST Traversal ------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements a recursive traversal of every node in an AST.
//
// It's important to update this traversal whenever the AST is
// changed, whether by adding a new node class or adding a new child
// to an existing node. Many walker implementations rely on being
// invoked with every node in the AST.
//
// Please follow these general rules when implementing traversal for
// a node:
//
// - Every node should be walked. If a node has both syntactic and
// semantic components, you should make sure you visit every node
// in both.
//
// - Nodes should only be walked once. So if a node has both
// syntactic and semantic components, but the type-checker builds
// the semantic components directly on top of the syntactic
// components, walking the semantic components will be sufficient
// to visit all the nodes in both.
//
// - Explicitly-written nodes should be walked in left-to-right
// syntactic order. The ordering of implicit nodes isn't
// particularly important.
//
// Note that semantic components will generally preserve the
// syntactic order of their children because doing something else
// could illegally change order of evaluation. This is why, for
// example, shuffling a TupleExpr creates a DestructureTupleExpr
// instead of just making a new TupleExpr with the elements in
// different order.
//
// - Sub-expressions and sub-statements should be replaceable.
// It's reasonable to expect that the replacement won't be
// completely unrelated to the original, but try to avoid making
// assumptions about the exact representation type. For example,
// assuming that a child expression is literally a TupleExpr may
// only be a reasonable assumption in an unchecked parse tree.
//
// - Avoid relying on the AST being type-checked or even
// well-formed during traversal.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/ASTWalker.h"
#include "swift/AST/ASTVisitor.h"
#include "swift/AST/GenericParamList.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/PrettyStackTrace.h"
using namespace swift;
void ASTWalker::anchor() {}
namespace {
/// Traversal - This class implements a simple expression/statement
/// recursive traverser which queries a user-provided walker class
/// on every node in an AST.
class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
/*Decl*/ bool,
Pattern *, /*TypeRepr*/ bool>
{
friend class ASTVisitor<Traversal, Expr*, Stmt*, bool, Pattern*, bool>;
typedef ASTVisitor<Traversal, Expr*, Stmt*, bool, Pattern*, bool> inherited;
ASTWalker &Walker;
/// RAII object that sets the parent of the walk context
/// appropriately.
class SetParentRAII {
ASTWalker &Walker;
decltype(ASTWalker::Parent) PriorParent;
public:
template<typename T>
SetParentRAII(ASTWalker &walker, T *newParent)
: Walker(walker), PriorParent(walker.Parent) {
walker.Parent = newParent;
}
~SetParentRAII() {
Walker.Parent = PriorParent;
}
};
Expr *visit(Expr *E) {
SetParentRAII SetParent(Walker, E);
return inherited::visit(E);
}
Stmt *visit(Stmt *S) {
SetParentRAII SetParent(Walker, S);
return inherited::visit(S);
}
Pattern *visit(Pattern *P) {
SetParentRAII SetParent(Walker, P);
return inherited::visit(P);
}
bool visit(Decl *D) {
SetParentRAII SetParent(Walker, D);
return inherited::visit(D);
}
bool visit(TypeRepr *T) {
SetParentRAII SetParent(Walker, T);
return inherited::visit(T);
}
bool visit(ParameterList *PL) {
return inherited::visit(PL);
}
//===--------------------------------------------------------------------===//
// Decls
//===--------------------------------------------------------------------===//
bool visitGenericParamListIfNeeded(GenericContext *GC) {
// Must check this first in case extensions have not been bound yet
if (Walker.shouldWalkIntoGenericParams()) {
if (auto *params = GC->getParsedGenericParams()) {
doIt(params);
}
return true;
}
return false;
}
bool visitTrailingRequirements(GenericContext *GC) {
if (const auto Where = GC->getTrailingWhereClause()) {
for (auto &Req: Where->getRequirements())
if (doIt(Req))
return true;
}
return false;
}
bool visitImportDecl(ImportDecl *ID) {
return false;
}
bool visitExtensionDecl(ExtensionDecl *ED) {
if (auto *typeRepr = ED->getExtendedTypeRepr())
if (doIt(typeRepr))
return true;
for (auto &Inherit : ED->getInherited()) {
if (auto *const TyR = Inherit.getTypeRepr())
if (doIt(TyR))
return true;
}
if (visitTrailingRequirements(ED))
return true;
for (Decl *M : ED->getMembers()) {
if (doIt(M))
return true;
if (Walker.shouldWalkAccessorsTheOldWay()) {
// Pretend that accessors share a parent with the storage.
//
// FIXME: Update existing ASTWalkers to deal with accessors appearing as
// children of the storage instead.
if (auto *ASD = dyn_cast<AbstractStorageDecl>(M)) {
for (auto AD : ASD->getAllAccessors()) {
if (doIt(AD))
return true;
}
}
}
}
return false;
}
bool visitPatternBindingDecl(PatternBindingDecl *PBD) {
bool isPropertyWrapperBackingProperty = false;
if (auto singleVar = PBD->getSingleVar()) {
isPropertyWrapperBackingProperty =
singleVar->getOriginalWrappedProperty() != nullptr;
}
for (auto idx : range(PBD->getNumPatternEntries())) {
if (Pattern *Pat = doIt(PBD->getPattern(idx)))
PBD->setPattern(idx, Pat, PBD->getInitContext(idx));
else
return true;
if (PBD->getInit(idx) &&
!isPropertyWrapperBackingProperty &&
(!PBD->isInitializerSubsumed(idx) ||
Walker.shouldWalkIntoLazyInitializers())) {
#ifndef NDEBUG
PrettyStackTraceDecl debugStack("walking into initializer for", PBD);
#endif
if (Expr *E2 = doIt(PBD->getInit(idx)))
PBD->setInit(idx, E2);
else
return true;
}
}
return false;
}
bool visitEnumCaseDecl(EnumCaseDecl *ECD) {
// We'll visit the EnumElementDecls separately.
return false;
}
bool visitTopLevelCodeDecl(TopLevelCodeDecl *TLCD) {
if (BraceStmt *S = cast_or_null<BraceStmt>(doIt(TLCD->getBody()))) {
TLCD->setBody(S);
return false;
}
return true;
}
bool visitIfConfigDecl(IfConfigDecl *ICD) {
// By default, just visit the elements that are actually
// injected into the enclosing context.
return false;
}
bool visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD) {
// By default, ignore #error/#warning.
return false;
}
bool visitOperatorDecl(OperatorDecl *OD) {
return false;
}
bool visitPrecedenceGroupDecl(PrecedenceGroupDecl *PGD) {
return false;
}
bool visitTypeAliasDecl(TypeAliasDecl *TAD) {
bool WalkGenerics = visitGenericParamListIfNeeded(TAD);
if (auto typerepr = TAD->getUnderlyingTypeRepr())
if (doIt(typerepr))
return true;
return WalkGenerics && visitTrailingRequirements(TAD);
}
bool visitOpaqueTypeDecl(OpaqueTypeDecl *OTD) {
if (Walker.shouldWalkIntoGenericParams() && OTD->getGenericParams()) {
if (doIt(OTD->getGenericParams()))
return true;
}
return false;
}
bool visitAbstractTypeParamDecl(AbstractTypeParamDecl *TPD) {
for (const auto &Inherit: TPD->getInherited()) {
if (auto *const TyR = Inherit.getTypeRepr())
if (doIt(TyR))
return true;
}
if (const auto ATD = dyn_cast<AssociatedTypeDecl>(TPD)) {
if (const auto DefaultTy = ATD->getDefaultDefinitionTypeRepr())
if (doIt(DefaultTy))
return true;
if (auto *WhereClause = ATD->getTrailingWhereClause()) {
for (auto &Req: WhereClause->getRequirements()) {
if (doIt(Req))
return true;
}
}
}
return false;
}
bool visitNominalTypeDecl(NominalTypeDecl *NTD) {
bool WalkGenerics = visitGenericParamListIfNeeded(NTD);
for (const auto &Inherit : NTD->getInherited()) {
if (auto *const TyR = Inherit.getTypeRepr())
if (doIt(Inherit.getTypeRepr()))
return true;
}
// Visit requirements
if (WalkGenerics && visitTrailingRequirements(NTD))
return true;
for (Decl *Member : NTD->getMembers()) {
if (doIt(Member))
return true;
if (Walker.shouldWalkAccessorsTheOldWay()) {
// Pretend that accessors share a parent with the storage.
//
// FIXME: Update existing ASTWalkers to deal with accessors appearing as
// children of the storage instead.
if (auto *ASD = dyn_cast<AbstractStorageDecl>(Member)) {
for (auto AD : ASD->getAllAccessors()) {
if (doIt(AD))
return true;
}
}
}
}
return false;
}
bool visitModuleDecl(ModuleDecl *MD) {
// TODO: should we recurse within the module?
return false;
}
bool visitVarDecl(VarDecl *VD) {
if (!Walker.shouldWalkAccessorsTheOldWay()) {
for (auto *AD : VD->getAllAccessors())
if (doIt(AD))
return true;
}
return false;
}
bool visitParamDecl(ParamDecl *P) {
// Don't walk into the type if the decl is implicit, or if the type is
// implicit.
if (!P->isImplicit()) {
if (auto *repr = P->getTypeRepr()) {
if (doIt(repr)) {
return true;
}
}
}
if (auto *E = P->getStructuralDefaultExpr()) {
auto res = doIt(E);
if (!res) return true;
P->setDefaultExpr(res, /*isTypeChecked*/ (bool)res->getType());
}
if (!Walker.shouldWalkAccessorsTheOldWay()) {
for (auto *AD : P->getAllAccessors())
if (doIt(AD))
return true;
}
return false;
}
bool visitSubscriptDecl(SubscriptDecl *SD) {
bool WalkGenerics = visitGenericParamListIfNeeded(SD);
visit(SD->getIndices());
if (auto *const TyR = SD->getElementTypeRepr())
if (doIt(TyR))
return true;
// Visit trailing requirements
if (WalkGenerics && visitTrailingRequirements(SD))
return true;
if (!Walker.shouldWalkAccessorsTheOldWay()) {
for (auto *AD : SD->getAllAccessors())
if (doIt(AD))
return true;
}
return false;
}
bool visitMissingMemberDecl(MissingMemberDecl *MMD) {
return false;
}
bool visitAbstractFunctionDecl(AbstractFunctionDecl *AFD) {
#ifndef NDEBUG
PrettyStackTraceDecl debugStack("walking into body of", AFD);
#endif
bool WalkGenerics =
// accessor generics are visited from the storage decl
!isa<AccessorDecl>(AFD) && visitGenericParamListIfNeeded(AFD);
if (auto *PD = AFD->getImplicitSelfDecl(/*createIfNeeded=*/false))
visit(PD);
visit(AFD->getParameters());
if (auto *FD = dyn_cast<FuncDecl>(AFD)) {
if (!isa<AccessorDecl>(FD))
if (auto *const TyR = FD->getResultTypeRepr())
if (doIt(TyR))
return true;
}
// Visit trailing requirements
if (WalkGenerics && visitTrailingRequirements(AFD))
return true;
if (AFD->getBody(/*canSynthesize=*/false)) {
AbstractFunctionDecl::BodyKind PreservedKind = AFD->getBodyKind();
if (BraceStmt *S = cast_or_null<BraceStmt>(doIt(AFD->getBody())))
AFD->setBody(S, PreservedKind);
else
return true;
}
if (auto ctor = dyn_cast<ConstructorDecl>(AFD)) {
if (auto superInit = ctor->getSuperInitCall()) {
if ((superInit = doIt(superInit)))
ctor->setSuperInitCall(superInit);
else
return true;
}
}
return false;
}
bool visitEnumElementDecl(EnumElementDecl *ED) {
if (auto *PL = ED->getParameterList()) {
visit(PL);
}
if (auto *rawLiteralExpr = ED->getRawValueUnchecked()) {
if (Expr *newRawExpr = doIt(rawLiteralExpr)) {
auto *newLiteralRawExpr = cast<LiteralExpr>(newRawExpr);
ED->setRawValueExpr(newLiteralRawExpr);
} else {
return true;
}
}
return false;
}
//===--------------------------------------------------------------------===//
// Exprs
//===--------------------------------------------------------------------===//
// A macro for handling the "semantic expressions" that are common
// on sugared expression nodes like string interpolation. The
// semantic expression is set up by type-checking to include all the
// other children as sub-expressions, so if it exists, we should
// just bypass the rest of the visitation.
#define HANDLE_SEMANTIC_EXPR(NODE) \
do { \
if (Expr *_semanticExpr = NODE->getSemanticExpr()) { \
if ((_semanticExpr = doIt(_semanticExpr))) { \
NODE->setSemanticExpr(_semanticExpr); \
} else { \
return nullptr; \
} \
return NODE; \
} \
} while (false)
Expr *visitErrorExpr(ErrorExpr *E) { return E; }
Expr *visitCodeCompletionExpr(CodeCompletionExpr *E) {
if (Expr *baseExpr = E->getBase()) {
Expr *newBaseExpr = doIt(baseExpr);
if (!newBaseExpr)
return nullptr;
E->setBase(newBaseExpr);
}
return E;
}
Expr *visitLiteralExpr(LiteralExpr *E) { return E; }
Expr *visitDiscardAssignmentExpr(DiscardAssignmentExpr *E) { return E; }
Expr *visitTypeExpr(TypeExpr *E) {
if (!E->isImplicit())
if (auto *typerepr = E->getTypeRepr())
if (doIt(typerepr))
return nullptr;
return E;
}
Expr *visitSuperRefExpr(SuperRefExpr *E) { return E; }
Expr *visitOtherConstructorDeclRefExpr(OtherConstructorDeclRefExpr *E) {
return E;
}
Expr *visitOverloadedDeclRefExpr(OverloadedDeclRefExpr *E) { return E; }
Expr *visitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *E) { return E; }
Expr *visitUnresolvedMemberExpr(UnresolvedMemberExpr *E) { return E; }
Expr *visitOpaqueValueExpr(OpaqueValueExpr *E) { return E; }
Expr *visitPropertyWrapperValuePlaceholderExpr(
PropertyWrapperValuePlaceholderExpr *E) {
if (E->getOpaqueValuePlaceholder()) {
if (auto *placeholder = doIt(E->getOpaqueValuePlaceholder()))
E->setOpaqueValuePlaceholder(dyn_cast<OpaqueValueExpr>(placeholder));
else
return nullptr;
}
if (Walker.shouldWalkIntoPropertyWrapperPlaceholderValue()) {
if (E->getOriginalWrappedValue()) {
if (auto *newValue = doIt(E->getOriginalWrappedValue()))
E->setOriginalWrappedValue(newValue);
else
return nullptr;
}
}
return E;
}
Expr *visitAppliedPropertyWrapperExpr(AppliedPropertyWrapperExpr *E) {
if (auto *newValue = doIt(E->getValue())) {
E->setValue(newValue);
} else {
return nullptr;
}
return E;
}
Expr *visitDefaultArgumentExpr(DefaultArgumentExpr *E) { return E; }
Expr *visitInterpolatedStringLiteralExpr(InterpolatedStringLiteralExpr *E) {
if (auto oldAppendingExpr = E->getAppendingExpr()) {
if (auto appendingExpr = doIt(oldAppendingExpr))
E->setAppendingExpr(dyn_cast<TapExpr>(appendingExpr));
else
return nullptr;
}
return E;
}
Expr *visitObjectLiteralExpr(ObjectLiteralExpr *E) {
if (Expr *arg = E->getArg()) {
if (Expr *arg2 = doIt(arg)) {
E->setArg(arg2);
} else {
return nullptr;
}
}
return E;
}
Expr *visitCollectionExpr(CollectionExpr *E) {
for (auto &elt : E->getElements())
if (Expr *Sub = doIt(elt))
elt = Sub;
else
return nullptr;
return E;
}
Expr *visitDeclRefExpr(DeclRefExpr *E) {
return E;
}
Expr *visitMemberRefExpr(MemberRefExpr *E) {
if (Expr *Base = doIt(E->getBase())) {
E->setBase(Base);
return E;
}
return nullptr;
}
Expr *visitDynamicMemberRefExpr(DynamicMemberRefExpr *E) {
if (Expr *Base = doIt(E->getBase())) {
E->setBase(Base);
return E;
}
return nullptr;
}
Expr *visitAnyTryExpr(AnyTryExpr *E) {
if (Expr *subExpr = doIt(E->getSubExpr())) {
E->setSubExpr(subExpr);
return E;
}
return nullptr;
}
Expr *visitIdentityExpr(IdentityExpr *E) {
if (Expr *subExpr = doIt(E->getSubExpr())) {
E->setSubExpr(subExpr);
return E;
}
return nullptr;
}
Expr *visitTupleExpr(TupleExpr *E) {
for (unsigned i = 0, e = E->getNumElements(); i != e; ++i)
if (E->getElement(i)) {
if (Expr *Elt = doIt(E->getElement(i)))
E->setElement(i, Elt);
else
return nullptr;
}
return E;
}
Expr *visitSubscriptExpr(SubscriptExpr *E) {
if (Expr *Base = doIt(E->getBase()))
E->setBase(Base);
else
return nullptr;
if (Expr *Index = doIt(E->getIndex()))
E->setIndex(Index);
else
return nullptr;
return E;
}
Expr *visitKeyPathApplicationExpr(KeyPathApplicationExpr *E) {
if (Expr *Base = doIt(E->getBase()))
E->setBase(Base);
else
return nullptr;
if (Expr *KeyPath = doIt(E->getKeyPath()))
E->setKeyPath(KeyPath);
else
return nullptr;
return E;
}
Expr *visitDynamicSubscriptExpr(DynamicSubscriptExpr *E) {
if (Expr *Base = doIt(E->getBase()))
E->setBase(Base);
else
return nullptr;
if (Expr *Index = doIt(E->getIndex()))
E->setIndex(Index);
else
return nullptr;
return E;
}
Expr *visitUnresolvedDotExpr(UnresolvedDotExpr *E) {
if (!E->getBase())
return E;
if (Expr *E2 = doIt(E->getBase())) {
E->setBase(E2);
return E;
}
return nullptr;
}
Expr *visitUnresolvedSpecializeExpr(UnresolvedSpecializeExpr *E) {
if (!E->getSubExpr())
return E;
if (Expr *Sub = doIt(E->getSubExpr()))
E->setSubExpr(Sub);
else
return nullptr;
for (auto &TyLoc : E->getUnresolvedParams()) {
if (doIt(TyLoc))
return nullptr;
}
return E;
}
Expr *visitTupleElementExpr(TupleElementExpr *E) {
if (Expr *E2 = doIt(E->getBase())) {
E->setBase(E2);
return E;
}
return nullptr;
}
Expr *visitImplicitConversionExpr(ImplicitConversionExpr *E) {
if (Expr *E2 = doIt(E->getSubExpr())) {
E->setSubExpr(E2);
return E;
}
return nullptr;
}
Expr *visitCollectionUpcastConversionExpr(CollectionUpcastConversionExpr *E) {
if (Expr *E2 = doIt(E->getSubExpr())) {
E->setSubExpr(E2);
} else {
return nullptr;
}
if (auto &keyConv = E->getKeyConversion()) {
auto kConv = keyConv.Conversion;
if (!kConv) {
return nullptr;
} else if (Expr *E2 = doIt(kConv)) {
E->setKeyConversion({keyConv.OrigValue, E2});
} else {
return nullptr;
}
}
if (auto &valueConv = E->getValueConversion()) {
auto vConv = valueConv.Conversion;
if (!vConv) {
return nullptr;
} else if (Expr *E2 = doIt(vConv)) {
E->setValueConversion({valueConv.OrigValue, E2});
} else {
return nullptr;
}
}
return E;
}
Expr *visitDestructureTupleExpr(DestructureTupleExpr *E) {
if (auto *src = doIt(E->getSubExpr())) {
E->setSubExpr(src);
} else {
return nullptr;
}
if (auto *dst = doIt(E->getResultExpr())) {
E->setResultExpr(dst);
} else {
return nullptr;
}
return E;
}
Expr *visitTryExpr(TryExpr *E) {
if (Expr *E2 = doIt(E->getSubExpr())) {
E->setSubExpr(E2);
return E;
}
return nullptr;
}
Expr *visitForceTryExpr(ForceTryExpr *E) {
if (Expr *E2 = doIt(E->getSubExpr())) {
E->setSubExpr(E2);
return E;
}
return nullptr;
}
Expr *visitOptionalTryExpr(OptionalTryExpr *E) {
if (Expr *E2 = doIt(E->getSubExpr())) {
E->setSubExpr(E2);
return E;
}
return nullptr;
}
Expr *visitInOutExpr(InOutExpr *E) {
if (Expr *E2 = doIt(E->getSubExpr())) {
E->setSubExpr(E2);
return E;
}
return nullptr;
}
Expr *visitVarargExpansionExpr(VarargExpansionExpr *E) {
if (Expr *E2 = doIt(E->getSubExpr())) {
E->setSubExpr(E2);
return E;
}
return nullptr;
}
Expr *visitSequenceExpr(SequenceExpr *E) {
for (unsigned i = 0, e = E->getNumElements(); i != e; ++i)
if (Expr *Elt = doIt(E->getElement(i)))
E->setElement(i, Elt);
else
return nullptr;
return E;
}
Expr *visitDynamicTypeExpr(DynamicTypeExpr *E) {
Expr *base = E->getBase();
if ((base = doIt(base)))
E->setBase(base);
else
return nullptr;
return E;
}
Expr *visitCaptureListExpr(CaptureListExpr *expr) {
for (auto c : expr->getCaptureList()) {
if (Walker.shouldWalkCaptureInitializerExpressions()) {
for (auto entryIdx : range(c.PBD->getNumPatternEntries())) {
if (auto newInit = doIt(c.PBD->getInit(entryIdx)))
c.PBD->setInit(entryIdx, newInit);
else
return nullptr;
}
} else {
if (doIt(c.PBD))
return nullptr;
}
}
ClosureExpr *body = expr->getClosureBody();
if ((body = cast_or_null<ClosureExpr>(doIt(body))))
expr->setClosureBody(body);
else
return nullptr;
return expr;
}
Expr *visitClosureExpr(ClosureExpr *expr) {
visit(expr->getParameters());
if (expr->hasExplicitResultType()) {
if (doIt(expr->getExplicitResultTypeRepr()))
return nullptr;
}
// If the closure was separately type checked and we don't want to
// visit separately-checked closure bodies, bail out now.
if (expr->isSeparatelyTypeChecked() &&
!Walker.shouldWalkIntoSeparatelyCheckedClosure(expr))
return expr;
// Handle other closures.
if (BraceStmt *body = cast_or_null<BraceStmt>(doIt(expr->getBody()))) {
expr->setBody(body, expr->hasSingleExpressionBody());
return expr;
}
return nullptr;
}
Expr *visitAutoClosureExpr(AutoClosureExpr *E) {
if (Expr *E2 = doIt(E->getSingleExpressionBody())) {
E->setBody(E2);
return E;
}
return nullptr;
}
Expr *visitApplyExpr(ApplyExpr *E) {
if (E->getFn()) {
Expr *E2 = doIt(E->getFn());
if (E2 == nullptr) return nullptr;
E->setFn(E2);
}
if (E->getArg()) {
Expr *E2 = doIt(E->getArg());
if (E2 == nullptr) return nullptr;
// Protect against setting a non-tuple argument expression for a binop,
// which may occur as a result of error recovery.
// E.g., "print(Array<Int)"
if (!isa<BinaryExpr>(E) || isa<TupleExpr>(E2))
E->setArg(E2);
}
return E;
}
Expr *visitSelfApplyExpr(SelfApplyExpr *E) {
if (E->getBase()) {
Expr *E2 = doIt(E->getBase());
if (E2 == nullptr) return nullptr;
E->setBase(E2);
}
if (E->getFn()) {
Expr *E2 = doIt(E->getFn());
if (E2 == nullptr) return nullptr;
E->setFn(E2);
}
return E;
}
Expr *visitDotSyntaxBaseIgnoredExpr(DotSyntaxBaseIgnoredExpr *E) {
Expr *E2 = doIt(E->getLHS());
if (E2 == nullptr) return nullptr;
E->setLHS(E2);
E2 = doIt(E->getRHS());
if (E2 == nullptr) return nullptr;
E->setRHS(E2);
return E;
}
Expr *visitExplicitCastExpr(ExplicitCastExpr *E) {
if (Expr *Sub = E->getSubExpr()) {
Sub = doIt(Sub);
if (!Sub) return nullptr;
E->setSubExpr(Sub);
}
if (auto *const tyRepr = E->getCastTypeRepr())
if (doIt(tyRepr))
return nullptr;
return E;
}
Expr *visitArrowExpr(ArrowExpr *E) {
if (Expr *Args = E->getArgsExpr()) {
Args = doIt(Args);
if (!Args) return nullptr;
E->setArgsExpr(Args);
}
if (Expr *Result = E->getResultExpr()) {
Result = doIt(Result);
if (!Result) return nullptr;
E->setResultExpr(Result);
}
return E;
}
Expr *visitRebindSelfInConstructorExpr(RebindSelfInConstructorExpr *E) {
Expr *Sub = doIt(E->getSubExpr());
if (!Sub) return nullptr;
E->setSubExpr(Sub);
return E;
}
Expr *visitAssignExpr(AssignExpr *AE) {
if (Expr *Dest = AE->getDest()) {
if (!(Dest = doIt(Dest)))
return nullptr;
AE->setDest(Dest);
}
if (Expr *Src = AE->getSrc()) {
if (!(Src = doIt(AE->getSrc())))
return nullptr;
AE->setSrc(Src);
}
return AE;
}
Expr *visitEnumIsCaseExpr(EnumIsCaseExpr *E) {
if (Expr *Sub = E->getSubExpr()) {
if (!(Sub = doIt(Sub)))
return nullptr;
E->setSubExpr(Sub);
}
if (auto *typerepr = E->getCaseTypeRepr())
if (doIt(typerepr))
return nullptr;
return E;
}
Expr *visitIfExpr(IfExpr *E) {
if (Expr *Cond = E->getCondExpr()) {
Cond = doIt(Cond);
if (!Cond) return nullptr;
E->setCondExpr(Cond);
}
Expr *Then = doIt(E->getThenExpr());
if (!Then) return nullptr;
E->setThenExpr(Then);
if (Expr *Else = E->getElseExpr()) {
Else = doIt(Else);
if (!Else) return nullptr;
E->setElseExpr(Else);
}
return E;
}
Expr *visitUnresolvedPatternExpr(UnresolvedPatternExpr *E) {
Pattern *sub = doIt(E->getSubPattern());
if (!sub) return nullptr;
E->setSubPattern(sub);
return E;
}
Expr *visitBindOptionalExpr(BindOptionalExpr *E) {
Expr *sub = doIt(E->getSubExpr());
if (!sub) return nullptr;
E->setSubExpr(sub);
return E;
}
Expr *visitOptionalEvaluationExpr(OptionalEvaluationExpr *E) {