38
38
#include " llvm/ADT/STLExtras.h"
39
39
#include " llvm/ADT/SmallVector.h"
40
40
41
- // / In case there's a bug in the ASTScope lookup system, suggest that the user
42
- // / try disabling it.
43
- // / \p message must be a string literal
44
- #define ASTScopeAssert (predicate, message ) \
45
- assert ((predicate) && message \
46
- " Try compiling with '-disable-astscope-lookup'.")
47
-
48
- #define ASTScope_unreachable (message ) \
49
- llvm_unreachable (message " Try compiling with '-disable-astscope-lookup'." )
50
-
51
41
namespace swift {
52
42
53
43
#pragma mark Forward-references
@@ -135,10 +125,10 @@ class ASTScopeImpl {
135
125
// / Must clear source range change whenever this changes
136
126
Children storedChildren;
137
127
138
- bool wasExpanded = false ;
139
-
140
- // / For use-before-def, ASTAncestor scopes may be added to a BraceStmt .
141
- unsigned astAncestorScopeCount = 0 ;
128
+ // / Because expansion returns an insertion point,
129
+ // / if a scope is reexpanded, the children added NOT by expansion must be
130
+ // / rescued and reused .
131
+ unsigned childrenCountWhenLastExpanded = 0 ;
142
132
143
133
// / Can clear storedChildren, so must remember this
144
134
bool haveAddedCleanup = false ;
@@ -172,7 +162,7 @@ class ASTScopeImpl {
172
162
void *operator new (size_t bytes, const ASTContext &ctx,
173
163
unsigned alignment = alignof (ASTScopeImpl));
174
164
void *operator new (size_t Bytes, void *Mem) {
175
- ASTScopeAssert (Mem, " Allocation failed " );
165
+ assert (Mem);
176
166
return Mem;
177
167
}
178
168
@@ -190,13 +180,12 @@ class ASTScopeImpl {
190
180
191
181
public: // for addReusedBodyScopes
192
182
void addChild (ASTScopeImpl *child, ASTContext &);
193
- std::vector<ASTScopeImpl *> rescueASTAncestorScopesForReuseFromMe ( );
183
+ std::vector<ASTScopeImpl *> rescueYoungestChildren ( unsigned count );
194
184
195
185
// / When reexpanding, do we always create a new body?
196
- virtual NullablePtr<ASTScopeImpl> getParentOfASTAncestorScopesToBeRescued ();
197
- std::vector<ASTScopeImpl *>
198
- rescueASTAncestorScopesForReuseFromMeOrDescendants ();
199
- void replaceASTAncestorScopes (ArrayRef<ASTScopeImpl *>);
186
+ virtual NullablePtr<ASTScopeImpl> getParentOfRescuedScopes ();
187
+ std::vector<ASTScopeImpl *> rescueScopesToReuse ();
188
+ void addReusedScopes (ArrayRef<ASTScopeImpl *>);
200
189
201
190
private:
202
191
void removeChildren ();
@@ -207,8 +196,6 @@ class ASTScopeImpl {
207
196
208
197
public:
209
198
void preOrderDo (function_ref<void (ASTScopeImpl *)>);
210
- // / Like preorderDo but without myself.
211
- void preOrderChildrenDo (function_ref<void (ASTScopeImpl *)>);
212
199
void postOrderDo (function_ref<void (ASTScopeImpl *)>);
213
200
214
201
#pragma mark - source ranges
@@ -232,12 +219,6 @@ class ASTScopeImpl {
232
219
bool doesRangeMatch (unsigned start, unsigned end, StringRef file = " " ,
233
220
StringRef className = " " );
234
221
235
- unsigned countDescendants () const ;
236
-
237
- // / Make sure that when the argument is executed, there are as many
238
- // / descendants after as before.
239
- void assertThatTreeDoesNotShrink (function_ref<void ()>);
240
-
241
222
private:
242
223
SourceRange computeSourceRangeOfScope (bool omitAssertions = false ) const ;
243
224
SourceRange
@@ -272,8 +253,6 @@ class ASTScopeImpl {
272
253
void ensureSourceRangesAreCorrectWhenAddingDescendants (function_ref<void ()>);
273
254
274
255
public: // public for debugging
275
- // / Returns source range of this node alone, without factoring in any
276
- // / children.
277
256
virtual SourceRange
278
257
getSourceRangeOfThisASTNode (bool omitAssertions = false ) const = 0 ;
279
258
@@ -335,17 +314,10 @@ class ASTScopeImpl {
335
314
// / Return the scope into which to place subsequent decls
336
315
ASTScopeImpl *expandAndBeCurrent (ScopeCreator &);
337
316
338
- unsigned getASTAncestorScopeCount () const { return astAncestorScopeCount; }
339
- bool getWasExpanded () const { return wasExpanded; }
340
-
341
317
protected:
342
- void resetASTAncestorScopeCount () { astAncestorScopeCount = 0 ; }
343
- void increaseASTAncestorScopeCount (unsigned c) { astAncestorScopeCount += c; }
344
- void setWasExpanded () { wasExpanded = true ; }
345
318
virtual ASTScopeImpl *expandSpecifically (ScopeCreator &) = 0;
346
319
virtual void beCurrent ();
347
- bool isCurrent () const ;
348
- virtual bool isCurrentIfWasExpanded () const ;
320
+ virtual bool isCurrent () const ;
349
321
350
322
private:
351
323
// / Compare the pre-expasion range with the post-expansion range and return
@@ -356,8 +328,6 @@ class ASTScopeImpl {
356
328
// / Some scopes can be expanded lazily.
357
329
// / Such scopes must: not change their source ranges after expansion, and
358
330
// / their expansion must return an insertion point outside themselves.
359
- // / After a node is expanded, its source range (getSourceRangeofThisASTNode
360
- // / union children's ranges) must be same as this.
361
331
virtual NullablePtr<ASTScopeImpl> insertionPointForDeferredExpansion ();
362
332
virtual SourceRange sourceRangeForDeferredExpansion () const ;
363
333
@@ -386,6 +356,9 @@ class ASTScopeImpl {
386
356
387
357
virtual ScopeCreator &getScopeCreator ();
388
358
359
+ protected:
360
+ void setChildrenCountWhenLastExpanded ();
361
+
389
362
#pragma mark - - creation queries
390
363
public:
391
364
virtual bool isThisAnAbstractStorageDecl () const { return false ; }
@@ -533,7 +506,6 @@ class ASTSourceFileScope final : public ASTScopeImpl {
533
506
// / The number of \c Decls in the \c SourceFile that were already seen.
534
507
// / Since parsing can be interleaved with type-checking, on every
535
508
// / lookup, look at creating scopes for any \c Decls beyond this number.
536
- // / rdar://55562483 Unify with numberOfChildrenWhenLastExpanded
537
509
int numberOfDeclsAlreadySeen = 0 ;
538
510
539
511
ASTSourceFileScope (SourceFile *SF, ScopeCreator *scopeCreator);
@@ -549,9 +521,7 @@ class ASTSourceFileScope final : public ASTScopeImpl {
549
521
NullablePtr<DeclContext> getDeclContext () const override ;
550
522
551
523
void addNewDeclsToScopeTree ();
552
- void buildFullyExpandedTree ();
553
- void
554
- buildEnoughOfTreeForTopLevelExpressionsButDontRequestGenericsOrExtendedNominals ();
524
+ void buildScopeTreeEagerly ();
555
525
556
526
const SourceFile *getSourceFile () const override ;
557
527
NullablePtr<const void > addressForPrinting () const override { return SF; }
@@ -582,7 +552,7 @@ class Portion {
582
552
void *operator new (size_t bytes, const ASTContext &ctx,
583
553
unsigned alignment = alignof (ASTScopeImpl));
584
554
void *operator new (size_t Bytes, void *Mem) {
585
- ASTScopeAssert (Mem, " Allocation failed " );
555
+ assert (Mem);
586
556
return Mem;
587
557
}
588
558
@@ -605,12 +575,12 @@ class Portion {
605
575
virtual const Decl *
606
576
getReferrentOfScope (const GenericTypeOrExtensionScope *s) const ;
607
577
608
- virtual void beCurrent (IterableTypeScope *) const = 0 ;
609
- virtual bool isCurrentIfWasExpanded (const IterableTypeScope *) const = 0 ;
578
+ virtual void beCurrent (IterableTypeScope *) const ;
579
+ virtual bool isCurrent (const IterableTypeScope *) const ;
610
580
virtual NullablePtr<ASTScopeImpl>
611
- insertionPointForDeferredExpansion (IterableTypeScope *) const = 0 ;
581
+ insertionPointForDeferredExpansion (IterableTypeScope *) const ;
612
582
virtual SourceRange
613
- sourceRangeForDeferredExpansion (const IterableTypeScope *) const = 0 ;
583
+ sourceRangeForDeferredExpansion (const IterableTypeScope *) const ;
614
584
};
615
585
616
586
// For the whole Decl scope of a GenericType or an Extension
@@ -631,24 +601,6 @@ class Portion {
631
601
632
602
const Decl *
633
603
getReferrentOfScope (const GenericTypeOrExtensionScope *s) const override ;
634
-
635
- // / Make whole portion lazy to avoid circularity in lookup of generic
636
- // / parameters of extensions. When \c bindExtension is called, it needs to
637
- // / unqualifed-lookup the type being extended. That causes an \c
638
- // / ExtensionScope
639
- // / (\c GenericTypeOrExtensionWholePortion) to be built.
640
- // / The building process needs the generic parameters, but that results in a
641
- // / request for the extended nominal type of the \c ExtensionDecl, which is
642
- // / an endless recursion. Although we only need to make \c ExtensionScope
643
- // / lazy, might as well do it for all \c IterableTypeScopes.
644
-
645
- void beCurrent (IterableTypeScope *) const override ;
646
- bool isCurrentIfWasExpanded (const IterableTypeScope *) const override ;
647
-
648
- NullablePtr<ASTScopeImpl>
649
- insertionPointForDeferredExpansion (IterableTypeScope *) const override ;
650
- SourceRange
651
- sourceRangeForDeferredExpansion (const IterableTypeScope *) const override ;
652
604
};
653
605
654
606
// / GenericTypeOrExtension = GenericType or Extension
@@ -687,14 +639,6 @@ class GenericTypeOrExtensionWherePortion final
687
639
688
640
SourceRange getChildlessSourceRangeOf (const GenericTypeOrExtensionScope *,
689
641
bool omitAssertions) const override ;
690
-
691
- void beCurrent (IterableTypeScope *) const override ;
692
- bool isCurrentIfWasExpanded (const IterableTypeScope *) const override ;
693
-
694
- NullablePtr<ASTScopeImpl>
695
- insertionPointForDeferredExpansion (IterableTypeScope *) const override ;
696
- SourceRange
697
- sourceRangeForDeferredExpansion (const IterableTypeScope *) const override ;
698
642
};
699
643
700
644
// / Behavior specific to representing the Body of a NominalTypeDecl or
@@ -711,7 +655,7 @@ class IterableTypeBodyPortion final
711
655
bool omitAssertions) const override ;
712
656
713
657
void beCurrent (IterableTypeScope *) const override ;
714
- bool isCurrentIfWasExpanded (const IterableTypeScope *) const override ;
658
+ bool isCurrent (const IterableTypeScope *) const override ;
715
659
NullablePtr<ASTScopeImpl>
716
660
insertionPointForDeferredExpansion (IterableTypeScope *) const override ;
717
661
SourceRange
@@ -750,17 +694,6 @@ class GenericTypeOrExtensionScope : public ASTScopeImpl {
750
694
SourceRange
751
695
getSourceRangeOfThisASTNode (bool omitAssertions = false ) const override ;
752
696
753
- // / \c tryBindExtension needs to get the extended nominal, and the DeclContext
754
- // / is the parent of the \c ExtensionDecl. If the \c SourceRange of an \c
755
- // / ExtensionScope were to start where the \c ExtensionDecl says, the lookup
756
- // / source locaiton would fall within the \c ExtensionScope. This inclusion
757
- // / would cause the lazy \c ExtensionScope to be expanded which would ask for
758
- // / its generic parameters in order to create those sub-scopes. That request
759
- // / would cause a cycle because it would ask for the extended nominal. So,
760
- // / move the source range of an \c ExtensionScope *past* the extended nominal
761
- // / type, which is not in-scope there anyway.
762
- virtual SourceRange moveStartPastExtendedNominal (SourceRange) const = 0;
763
-
764
697
virtual GenericContext *getGenericContext () const = 0;
765
698
std::string getClassName () const override ;
766
699
virtual std::string declKindName () const = 0;
@@ -799,8 +732,6 @@ class GenericTypeScope : public GenericTypeOrExtensionScope {
799
732
public:
800
733
GenericTypeScope (const Portion *p) : GenericTypeOrExtensionScope(p) {}
801
734
virtual ~GenericTypeScope () {}
802
- SourceRange moveStartPastExtendedNominal (SourceRange) const override ;
803
-
804
735
protected:
805
736
NullablePtr<const GenericParamList> genericParams () const override ;
806
737
};
@@ -822,11 +753,9 @@ class IterableTypeScope : public GenericTypeScope {
822
753
823
754
protected:
824
755
void beCurrent () override ;
825
- bool isCurrentIfWasExpanded () const override ;
756
+ bool isCurrent () const override ;
826
757
827
758
public:
828
- void makeWholeCurrent ();
829
- bool isWholeCurrent () const ;
830
759
void makeBodyCurrent ();
831
760
bool isBodyCurrent () const ;
832
761
NullablePtr<ASTScopeImpl> insertionPointForDeferredExpansion () override ;
@@ -875,7 +804,6 @@ class ExtensionScope final : public IterableTypeScope {
875
804
NullablePtr<NominalTypeDecl> getCorrespondingNominalTypeDecl () const override ;
876
805
std::string declKindName () const override { return " Extension" ; }
877
806
SourceRange getBraces () const override ;
878
- SourceRange moveStartPastExtendedNominal (SourceRange) const override ;
879
807
ASTScopeImpl *createTrailingWhereClauseScope (ASTScopeImpl *parent,
880
808
ScopeCreator &) override ;
881
809
void createBodyScope (ASTScopeImpl *leaf, ScopeCreator &) override ;
@@ -1056,7 +984,7 @@ class AbstractFunctionBodyScope : public ASTScopeImpl {
1056
984
protected:
1057
985
ASTScopeImpl *expandSpecifically (ScopeCreator &scopeCreator) override ;
1058
986
void beCurrent () override ;
1059
- bool isCurrentIfWasExpanded () const override ;
987
+ bool isCurrent () const override ;
1060
988
1061
989
private:
1062
990
void expandAScopeThatDoesNotCreateANewInsertionPoint (ScopeCreator &);
@@ -1072,7 +1000,7 @@ class AbstractFunctionBodyScope : public ASTScopeImpl {
1072
1000
Decl *getDecl () const { return decl; }
1073
1001
static bool isAMethod (const AbstractFunctionDecl *);
1074
1002
1075
- NullablePtr<ASTScopeImpl> getParentOfASTAncestorScopesToBeRescued () override ;
1003
+ NullablePtr<ASTScopeImpl> getParentOfRescuedScopes () override ;
1076
1004
1077
1005
protected:
1078
1006
bool lookupLocalsOrMembers (ArrayRef<const ASTScopeImpl *>,
@@ -1148,14 +1076,11 @@ class AttachedPropertyWrapperScope final : public ASTScopeImpl {
1148
1076
// / false positives, that that doesn't hurt anything. However, the result of
1149
1077
// / the conservative source range computation doesn't seem to be stable. So
1150
1078
// / keep the original here, and use it for source range queries.
1151
- // / rdar://55263708
1152
-
1153
1079
const SourceRange sourceRangeWhenCreated;
1154
1080
1155
1081
AttachedPropertyWrapperScope (VarDecl *e)
1156
1082
: decl(e), sourceRangeWhenCreated(getSourceRangeOfVarDecl(e)) {
1157
- ASTScopeAssert (sourceRangeWhenCreated.isValid (),
1158
- " VarDecls must have ranges to be looked-up" );
1083
+ assert (sourceRangeWhenCreated.isValid ());
1159
1084
}
1160
1085
virtual ~AttachedPropertyWrapperScope () {}
1161
1086
@@ -1232,7 +1157,7 @@ class PatternEntryDeclScope final : public AbstractPatternEntryScope {
1232
1157
protected:
1233
1158
ASTScopeImpl *expandSpecifically (ScopeCreator &scopeCreator) override ;
1234
1159
void beCurrent () override ;
1235
- bool isCurrentIfWasExpanded () const override ;
1160
+ bool isCurrent () const override ;
1236
1161
1237
1162
private:
1238
1163
AnnotatedInsertionPoint
@@ -1406,7 +1331,7 @@ class WholeClosureScope final : public AbstractClosureScope {
1406
1331
protected:
1407
1332
ASTScopeImpl *expandSpecifically (ScopeCreator &scopeCreator) override ;
1408
1333
void beCurrent () override ;
1409
- bool isCurrentIfWasExpanded () const override ;
1334
+ bool isCurrent () const override ;
1410
1335
1411
1336
private:
1412
1337
void expandAScopeThatDoesNotCreateANewInsertionPoint (ScopeCreator &);
@@ -1477,7 +1402,7 @@ class TopLevelCodeScope final : public ASTScopeImpl {
1477
1402
protected:
1478
1403
ASTScopeImpl *expandSpecifically (ScopeCreator &scopeCreator) override ;
1479
1404
void beCurrent () override ;
1480
- bool isCurrentIfWasExpanded () const override ;
1405
+ bool isCurrent () const override ;
1481
1406
1482
1407
private:
1483
1408
AnnotatedInsertionPoint
@@ -1495,7 +1420,7 @@ class TopLevelCodeScope final : public ASTScopeImpl {
1495
1420
Decl *getDecl () const { return decl; }
1496
1421
NullablePtr<const void > getReferrent () const override ;
1497
1422
1498
- NullablePtr<ASTScopeImpl> getParentOfASTAncestorScopesToBeRescued () override ;
1423
+ NullablePtr<ASTScopeImpl> getParentOfRescuedScopes () override ;
1499
1424
};
1500
1425
1501
1426
// / The \c _@specialize attribute.
0 commit comments