@@ -51,6 +51,7 @@ class Stmt;
51
51
class StmtConditionElement ;
52
52
class SwitchStmt ;
53
53
class TopLevelCodeDecl ;
54
+ class TypeDecl ;
54
55
class WhileStmt ;
55
56
56
57
// / Describes kind of scope that occurs within the AST.
@@ -61,6 +62,10 @@ enum class ASTScopeKind : uint8_t {
61
62
Preexpanded,
62
63
// / A source file, which is the root of a scope.
63
64
SourceFile,
65
+ // / The declaration of a type.
66
+ TypeDecl,
67
+ // / The generic parameters of an extension declaration.
68
+ ExtensionGenericParams,
64
69
// / The body of a type or extension thereof.
65
70
TypeOrExtensionBody,
66
71
// / The generic parameters of a declaration.
@@ -71,17 +76,18 @@ enum class ASTScopeKind : uint8_t {
71
76
AbstractFunctionParams,
72
77
// / The default argument for a parameter.
73
78
DefaultArgument,
79
+ // / The body of a function.
80
+ AbstractFunctionBody,
74
81
// / A specific pattern binding.
75
82
PatternBinding,
76
83
// / The scope introduced for an initializer of a pattern binding.
77
84
PatternInitializer,
78
- // / The scope introduced by a particular clause in a pattern binding
79
- // / declaration.
85
+ // / The scope following a particular clause in a pattern binding declaration,
86
+ // / which is the outermost scope in which the variables introduced by that
87
+ // / clause will be visible.
80
88
AfterPatternBinding,
81
89
// / The scope introduced by a brace statement.
82
90
BraceStmt,
83
- // / The scope introduced by a local declaration.
84
- LocalDeclaration,
85
91
// / Node describing an "if" statement.
86
92
IfStmt,
87
93
// / The scope introduced by a conditional clause in an if/guard/while
@@ -150,8 +156,27 @@ class ASTScope {
150
156
// / whether the children of this node have already been expanded.
151
157
mutable llvm::PointerIntPair<const ASTScope *, 1 , bool > parentAndExpanded;
152
158
153
- // / The cached source range.
154
- mutable SourceRange CachedSourceRange;
159
+ // / Describes the kind of continuation stored in the continuation field.
160
+ enum class ContinuationKind {
161
+ // / The continuation is historical: if the continuation is non-null, we
162
+ // / preserve it so we know which scope to look at to compute the end of the
163
+ // / source range.
164
+ Historical = 0 ,
165
+ // / The continuation is active.
166
+ Active = 1 ,
167
+ // / The continuation stored in the pointer field is active, and replaced a
168
+ // / \c SourceFile continuation.
169
+ ActiveThenSourceFile = 2 ,
170
+ };
171
+
172
+ // / The scope from which the continuation child nodes will be populated.
173
+ // /
174
+ // / The enumeration bits indicate whether the continuation pointer represents
175
+ // / an active continuation (vs. a historical one) and whether the former
176
+ // / continuation was for a \c SourceFile (which can be stacked behind another
177
+ // / continuation).
178
+ mutable llvm::PointerIntPair<const ASTScope *, 2 , ContinuationKind>
179
+ continuation = { nullptr , ContinuationKind::Historical };
155
180
156
181
// / Union describing the various kinds of AST nodes that can introduce
157
182
// / scopes.
@@ -167,8 +192,15 @@ class ASTScope {
167
192
mutable unsigned nextElement;
168
193
} sourceFile;
169
194
195
+ // / A type declaration, for \c kind == ASTScopeKind::TypeDecl.
196
+ TypeDecl *typeDecl;
197
+
198
+ // / An extension declaration, for
199
+ // / \c kind == ASTScopeKind::ExtensionGenericParams.
200
+ ExtensionDecl *extension;
201
+
170
202
// / An iterable declaration context, which covers nominal type declarations
171
- // / and extensions .
203
+ // / and extension bodies .
172
204
// /
173
205
// / For \c kind == ASTScopeKind::TypeOrExtensionBody.
174
206
IterableDeclContext *iterableDeclContext;
@@ -185,10 +217,11 @@ class ASTScope {
185
217
unsigned index;
186
218
} genericParams;
187
219
188
- // / An abstract function, for \c kind == ASTScopeKind::AbstractFunctionDecl.
220
+ // / An abstract function, for \c kind == ASTScopeKind::AbstractFunctionDecl
221
+ // / or \c kind == ASTScopeKind::AbstractFunctionBody.
189
222
AbstractFunctionDecl *abstractFunction;
190
223
191
- // / An parameter for an abstract function (init/func/deinit).
224
+ // / A parameter for an abstract function (init/func/deinit).
192
225
// /
193
226
// / For \c kind == ASTScopeKind::AbstractFunctionParams.
194
227
struct {
@@ -222,11 +255,6 @@ class ASTScope {
222
255
mutable unsigned nextElement;
223
256
} braceStmt;
224
257
225
- // / The declaration introduced within a local scope.
226
- // /
227
- // / For \c kind == ASTScopeKind::LocalDeclaration.
228
- Decl *localDeclaration;
229
-
230
258
// / The 'if' statement, for \c kind == ASTScopeKind::IfStmt.
231
259
IfStmt *ifStmt;
232
260
@@ -286,6 +314,26 @@ class ASTScope {
286
314
// / Child scopes, sorted by source range.
287
315
mutable SmallVector<ASTScope *, 4 > storedChildren;
288
316
317
+ // / Retrieve the active continuation.
318
+ const ASTScope *getActiveContinuation () const ;
319
+
320
+ // / Retrieve the historical continuation (which might also be active).
321
+ // /
322
+ // / This is the oldest historical continuation, so a \c SourceFile
323
+ // / continuation will be returned even if it's been replaced by a more local
324
+ // / continuation.
325
+ const ASTScope *getHistoricalContinuation () const ;
326
+
327
+ // / Set the active continuation.
328
+ void addActiveContinuation (const ASTScope *newContinuation) const ;
329
+
330
+ // / Remove the active continuation.
331
+ void removeActiveContinuation () const ;
332
+
333
+ // / Clear out the continuation, because it has been stolen been transferred to
334
+ // / a child node.
335
+ void clearActiveContinuation () const ;
336
+
289
337
// / Constructor that only initializes the kind and parent, leaving the
290
338
// / pieces to be initialized by the caller.
291
339
ASTScope (ASTScopeKind kind, const ASTScope *parent)
@@ -300,6 +348,16 @@ class ASTScope {
300
348
// / Constructor that initializes a preexpanded node.
301
349
ASTScope (const ASTScope *parent, ArrayRef<ASTScope *> children);
302
350
351
+ ASTScope (const ASTScope *parent, TypeDecl *typeDecl)
352
+ : ASTScope(ASTScopeKind::TypeDecl, parent) {
353
+ this ->typeDecl = typeDecl;
354
+ }
355
+
356
+ ASTScope (const ASTScope *parent, ExtensionDecl *extension)
357
+ : ASTScope(ASTScopeKind::ExtensionGenericParams, parent) {
358
+ this ->extension = extension;
359
+ }
360
+
303
361
ASTScope (const ASTScope *parent, IterableDeclContext *idc)
304
362
: ASTScope(ASTScopeKind::TypeOrExtensionBody, parent) {
305
363
this ->iterableDeclContext = idc;
@@ -313,8 +371,11 @@ class ASTScope {
313
371
this ->genericParams .index = index;
314
372
}
315
373
316
- ASTScope (const ASTScope *parent, AbstractFunctionDecl *abstractFunction)
317
- : ASTScope(ASTScopeKind::AbstractFunctionDecl, parent) {
374
+ ASTScope (ASTScopeKind kind, const ASTScope *parent,
375
+ AbstractFunctionDecl *abstractFunction)
376
+ : ASTScope(kind, parent) {
377
+ assert (kind == ASTScopeKind::AbstractFunctionDecl ||
378
+ kind == ASTScopeKind::AbstractFunctionBody);
318
379
this ->abstractFunction = abstractFunction;
319
380
}
320
381
@@ -429,7 +490,8 @@ class ASTScope {
429
490
// / Expand the children of this AST scope so they can be queried.
430
491
void expand () const ;
431
492
432
- // / Determine whether the given scope has already been expanded.
493
+ // / Determine whether the given scope has already been completely expanded,
494
+ // / and cannot create any new children.
433
495
bool isExpanded () const ;
434
496
435
497
// / Create a new AST scope if one is needed for the given declaration.
@@ -456,25 +518,28 @@ class ASTScope {
456
518
// / introduced by this AST node.
457
519
static ASTScope *createIfNeeded (const ASTScope *parent, ASTNode node);
458
520
459
- // / Determine whether this is a scope from which we can perform a
460
- // / continuation.
461
- bool isContinuationScope () const ;
521
+ // / Determine whether this scope can steal a continuation from its parent,
522
+ // / because (e.g.) it introduces some name binding that should be visible
523
+ // / in the continuation.
524
+ bool canStealContinuation () const ;
462
525
463
- // / Enumerate the continuation scopes for the given parent,
526
+ // / Enumerate the continuation child scopes for the given scope.
464
527
// /
465
- // / Statements, such as 'guard' and local declarations, introduce scopes
466
- // / that extend to the end of an enclosing brace-stmt. This
467
- // / operation finds each of the "continuation" scopes in the nearest
468
- // / enclosing brace statement.
528
+ // / \param addChild Function that will be invoked to add the continuation
529
+ // / child. This function should return true if the child steals the
530
+ // / continuation, which terminates the enumeration.
469
531
void enumerateContinuationScopes (
470
- llvm::function_ref<void (ASTScope *)> fn ) const ;
532
+ llvm::function_ref<bool (ASTScope *)> addChild ) const ;
471
533
472
534
// / Compute the source range of this scope.
473
535
SourceRange getSourceRangeImpl () const ;
474
536
475
537
// / Retrieve the ASTContext in which this scope exists.
476
538
ASTContext &getASTContext () const ;
477
539
540
+ // / Retrieve the source file scope, which is the root of the tree.
541
+ const ASTScope *getSourceFileScope () const ;
542
+
478
543
// / Retrieve the source file in which this scope exists.
479
544
SourceFile &getSourceFile () const ;
480
545
@@ -497,24 +562,29 @@ class ASTScope {
497
562
498
563
// / Determine the source range covered by this scope.
499
564
SourceRange getSourceRange () const {
500
- if (CachedSourceRange.isInvalid () ||
501
- getKind () == ASTScopeKind::SourceFile) {
502
- CachedSourceRange = getSourceRangeImpl ();
565
+ SourceRange range = getSourceRangeImpl ();
566
+
567
+ // If there was ever a continuation, use it's end range.
568
+ if (auto historical = getHistoricalContinuation ()) {
569
+ if (historical != this )
570
+ range.End = historical->getSourceRange ().End ;
503
571
}
504
- return CachedSourceRange;
572
+
573
+ return range;
505
574
}
506
575
507
- // / Retrieve the local declatation when
508
- // / \c getKind() == ASTScopeKind::LocalDeclaration.
509
- Decl *getLocalDeclaration () const {
510
- assert (getKind () == ASTScopeKind::LocalDeclaration);
511
- return localDeclaration;
576
+ // / Retrieve the type declaration when \c getKind() == ASTScopeKind::TypeDecl.
577
+ TypeDecl *getTypeDecl () const {
578
+ assert (getKind () == ASTScopeKind::TypeDecl);
579
+ return typeDecl;
512
580
}
513
581
514
582
// / Retrieve the abstract function declaration when
515
- // / \c getKind() == ASTScopeKind::AbstractFunctionDecl;
583
+ // / \c getKind() == ASTScopeKind::AbstractFunctionDecl or
584
+ // / \c getKind() == ASTScopeKind::AbstractFunctionBody;
516
585
AbstractFunctionDecl *getAbstractFunctionDecl () const {
517
- assert (getKind () == ASTScopeKind::AbstractFunctionDecl);
586
+ assert (getKind () == ASTScopeKind::AbstractFunctionDecl ||
587
+ getKind () == ASTScopeKind::AbstractFunctionBody);
518
588
return abstractFunction;
519
589
}
520
590
@@ -543,6 +613,18 @@ class ASTScope {
543
613
// / \seealso getDeclContext().
544
614
DeclContext *getInnermostEnclosingDeclContext () const ;
545
615
616
+ // / Retrueve the declarations whose names are directly bound by this scope.
617
+ // /
618
+ // / The declarations bound in this scope aren't available in the immediate
619
+ // / parent of this scope, but will still be visible in child scopes (unless
620
+ // / shadowed there).
621
+ // /
622
+ // / Note that this routine does not produce bindings for anything that can
623
+ // / be found via qualified name lookup in a \c DeclContext, such as nominal
624
+ // / type declarations or extensions thereof, or the source file itself. The
625
+ // / client can perform such lookups using the result of \c getDeclContext().
626
+ SmallVector<ValueDecl *, 4 > getLocalBindings () const ;
627
+
546
628
// / Expand the entire scope map.
547
629
// /
548
630
// / Normally, the scope map will be expanded only as needed by its queries,
0 commit comments