@@ -61,12 +61,6 @@ template <typename Rangeable>
61
61
static SourceRange getRangeableSourceRange (const Rangeable *const p) {
62
62
return p->getSourceRange ();
63
63
}
64
- static SourceRange getRangeableSourceRange (const SpecializeAttr *a) {
65
- return a->getRange ();
66
- }
67
- static SourceRange getRangeableSourceRange (const DifferentiableAttr *a) {
68
- return a->getRange ();
69
- }
70
64
static SourceRange getRangeableSourceRange (const ASTNode n) {
71
65
return n.getSourceRange ();
72
66
}
@@ -404,27 +398,6 @@ class ScopeCreator final {
404
398
expr->walk (ClosureFinder (*this , parent));
405
399
}
406
400
407
- private:
408
- // A safe way to discover this, without creating a circular request.
409
- // Cannot call getAttachedPropertyWrappers.
410
- static bool hasAttachedPropertyWrapper (VarDecl *vd) {
411
- return AttachedPropertyWrapperScope::getSourceRangeOfVarDecl (vd).isValid ();
412
- }
413
-
414
- public:
415
- // / If the pattern has an attached property wrapper, create a scope for it
416
- // / so it can be looked up.
417
-
418
- void
419
- addAnyAttachedPropertyWrappersToScopeTree (PatternBindingDecl *patternBinding,
420
- ASTScopeImpl *parent) {
421
- patternBinding->getPattern (0 )->forEachVariable ([&](VarDecl *vd) {
422
- if (hasAttachedPropertyWrapper (vd))
423
- constructExpandAndInsertUncheckable<AttachedPropertyWrapperScope>(
424
- parent, vd);
425
- });
426
- }
427
-
428
401
public:
429
402
// / Create the matryoshka nested generic param scopes (if any)
430
403
// / that are subscopes of the receiver. Return
@@ -447,34 +420,8 @@ class ScopeCreator final {
447
420
addChildrenForAllLocalizableAccessorsInSourceOrder (AbstractStorageDecl *asd,
448
421
ASTScopeImpl *parent);
449
422
450
- void
451
- forEachSpecializeAttrInSourceOrder (Decl *declBeingSpecialized,
452
- function_ref<void (SpecializeAttr *)> fn) {
453
- std::vector<SpecializeAttr *> sortedSpecializeAttrs;
454
- for (auto *attr : declBeingSpecialized->getAttrs ()) {
455
- if (auto *specializeAttr = dyn_cast<SpecializeAttr>(attr))
456
- sortedSpecializeAttrs.push_back (specializeAttr);
457
- }
458
- // TODO: rm extra copy
459
- for (auto *specializeAttr : sortBySourceRange (sortedSpecializeAttrs))
460
- fn (specializeAttr);
461
- }
462
-
463
- void forEachDifferentiableAttrInSourceOrder (
464
- Decl *decl, function_ref<void (DifferentiableAttr *)> fn) {
465
- std::vector<DifferentiableAttr *> sortedDifferentiableAttrs;
466
- for (auto *attr : decl->getAttrs ())
467
- if (auto *diffAttr = dyn_cast<DifferentiableAttr>(attr))
468
- // NOTE(TF-835): Skipping implicit `@differentiable` attributes is
469
- // necessary to avoid verification failure in
470
- // `ASTScopeImpl::verifyThatChildrenAreContainedWithin`.
471
- // Perhaps this check may no longer be necessary after TF-835: robust
472
- // `@derivative` attribute lowering.
473
- if (!diffAttr->isImplicit ())
474
- sortedDifferentiableAttrs.push_back (diffAttr);
475
- for (auto *diffAttr : sortBySourceRange (sortedDifferentiableAttrs))
476
- fn (diffAttr);
477
- }
423
+ void addChildrenForKnownAttributes (ValueDecl *decl,
424
+ ASTScopeImpl *parent);
478
425
479
426
public:
480
427
@@ -796,8 +743,8 @@ class NodeAdder
796
743
visitPatternBindingDecl (PatternBindingDecl *patternBinding,
797
744
ASTScopeImpl *parentScope,
798
745
ScopeCreator &scopeCreator) {
799
- scopeCreator. addAnyAttachedPropertyWrappersToScopeTree ( patternBinding,
800
- parentScope);
746
+ if ( auto *var = patternBinding-> getSingleVar ())
747
+ scopeCreator. addChildrenForKnownAttributes (var, parentScope);
801
748
802
749
const bool isInTypeDecl = parentScope->isATypeDeclScope ();
803
750
@@ -881,28 +828,51 @@ ScopeCreator::addToScopeTreeAndReturnInsertionPoint(ASTNode n,
881
828
882
829
void ScopeCreator::addChildrenForAllLocalizableAccessorsInSourceOrder (
883
830
AbstractStorageDecl *asd, ASTScopeImpl *parent) {
884
- // Accessors are always nested within their abstract storage
885
- // declaration. The nesting may not be immediate, because subscripts may
886
- // have intervening scopes for generics.
887
-
888
- // Create scopes for `@differentiable` attributes.
889
- forEachDifferentiableAttrInSourceOrder (
890
- asd, [&](DifferentiableAttr *diffAttr) {
891
- ifUniqueConstructExpandAndInsert<DifferentiableAttributeScope>(
892
- parent, diffAttr, asd);
893
- });
894
-
895
- AbstractStorageDecl *enclosingAbstractStorageDecl =
896
- parent->getEnclosingAbstractStorageDecl ().get ();
897
-
898
831
asd->visitParsedAccessors ([&](AccessorDecl *ad) {
899
- assert (enclosingAbstractStorageDecl == ad->getStorage ());
900
- (void ) enclosingAbstractStorageDecl;
901
-
832
+ assert (asd == ad->getStorage ());
902
833
this ->addToScopeTree (ad, parent);
903
834
});
904
835
}
905
836
837
+ void ScopeCreator::addChildrenForKnownAttributes (ValueDecl *decl,
838
+ ASTScopeImpl *parent) {
839
+ SmallVector<DeclAttribute *, 2 > relevantAttrs;
840
+
841
+ for (auto *attr : decl->getAttrs ()) {
842
+ if (isa<DifferentiableAttr>(attr)) {
843
+ if (!attr->isImplicit ())
844
+ relevantAttrs.push_back (attr);
845
+ }
846
+
847
+ if (isa<SpecializeAttr>(attr))
848
+ relevantAttrs.push_back (attr);
849
+
850
+ if (isa<CustomAttr>(attr))
851
+ relevantAttrs.push_back (attr);
852
+ }
853
+
854
+ // Decl::getAttrs() is a linked list with head insertion, so the
855
+ // attributes are in reverse source order.
856
+ std::reverse (relevantAttrs.begin (), relevantAttrs.end ());
857
+
858
+ for (auto *attr : relevantAttrs) {
859
+ if (auto *diffAttr = dyn_cast<DifferentiableAttr>(attr)) {
860
+ ifUniqueConstructExpandAndInsert<DifferentiableAttributeScope>(
861
+ parent, diffAttr, decl);
862
+ } else if (auto *specAttr = dyn_cast<SpecializeAttr>(attr)) {
863
+ if (auto *afd = dyn_cast<AbstractFunctionDecl>(decl)) {
864
+ ifUniqueConstructExpandAndInsert<SpecializeAttributeScope>(
865
+ parent, specAttr, afd);
866
+ }
867
+ } else if (auto *customAttr = dyn_cast<CustomAttr>(attr)) {
868
+ if (auto *vd = dyn_cast<VarDecl>(decl)) {
869
+ ifUniqueConstructExpandAndInsert<AttachedPropertyWrapperScope>(
870
+ parent, customAttr, vd);
871
+ }
872
+ }
873
+ }
874
+ }
875
+
906
876
#pragma mark creation helpers
907
877
908
878
void ASTScopeImpl::addChild (ASTScopeImpl *child, ASTContext &ctx) {
@@ -1172,19 +1142,7 @@ TopLevelCodeScope::expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &
1172
1142
1173
1143
void AbstractFunctionDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint (
1174
1144
ScopeCreator &scopeCreator) {
1175
- // Create scopes for specialize attributes
1176
- scopeCreator.forEachSpecializeAttrInSourceOrder (
1177
- decl, [&](SpecializeAttr *specializeAttr) {
1178
- scopeCreator.ifUniqueConstructExpandAndInsert <SpecializeAttributeScope>(
1179
- this , specializeAttr, decl);
1180
- });
1181
- // Create scopes for `@differentiable` attributes.
1182
- scopeCreator.forEachDifferentiableAttrInSourceOrder (
1183
- decl, [&](DifferentiableAttr *diffAttr) {
1184
- scopeCreator
1185
- .ifUniqueConstructExpandAndInsert <DifferentiableAttributeScope>(
1186
- this , diffAttr, decl);
1187
- });
1145
+ scopeCreator.addChildrenForKnownAttributes (decl, this );
1188
1146
1189
1147
// Create scopes for generic and ordinary parameters.
1190
1148
// For a subscript declaration, the generic and ordinary parameters are in an
@@ -1330,13 +1288,12 @@ void VarDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
1330
1288
1331
1289
void SubscriptDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint (
1332
1290
ScopeCreator &scopeCreator) {
1333
- auto *sub = decl;
1291
+ scopeCreator. addChildrenForKnownAttributes ( decl, this ) ;
1334
1292
auto *leaf = scopeCreator.addNestedGenericParamScopesToTree (
1335
- sub, sub->getGenericParams (), this );
1336
- auto *params =
1337
- scopeCreator.constructExpandAndInsertUncheckable <ParameterListScope>(
1338
- leaf, sub->getIndices (), sub->getAccessor (AccessorKind::Get));
1339
- scopeCreator.addChildrenForAllLocalizableAccessorsInSourceOrder (sub, params);
1293
+ decl, decl->getGenericParams (), this );
1294
+ scopeCreator.constructExpandAndInsertUncheckable <ParameterListScope>(
1295
+ leaf, decl->getIndices (), decl->getAccessor (AccessorKind::Get));
1296
+ scopeCreator.addChildrenForAllLocalizableAccessorsInSourceOrder (decl, leaf);
1340
1297
}
1341
1298
1342
1299
void CaptureListScope::expandAScopeThatDoesNotCreateANewInsertionPoint (
@@ -1364,10 +1321,8 @@ void DefaultArgumentInitializerScope::
1364
1321
void AttachedPropertyWrapperScope::
1365
1322
expandAScopeThatDoesNotCreateANewInsertionPoint (
1366
1323
ScopeCreator &scopeCreator) {
1367
- for (auto *attr : decl->getAttrs ().getAttributes <CustomAttr>()) {
1368
- if (auto *expr = attr->getArg ())
1324
+ if (auto *expr = attr->getArg ())
1369
1325
scopeCreator.addToScopeTree (expr, this );
1370
- }
1371
1326
}
1372
1327
1373
1328
#pragma mark expandScope
@@ -1595,6 +1550,7 @@ GET_REFERRENT(CaptureListScope, getExpr())
1595
1550
GET_REFERRENT(ClosureParametersScope, getExpr())
1596
1551
GET_REFERRENT(SpecializeAttributeScope, specializeAttr)
1597
1552
GET_REFERRENT(DifferentiableAttributeScope, differentiableAttr)
1553
+ GET_REFERRENT(AttachedPropertyWrapperScope, attr)
1598
1554
GET_REFERRENT(GenericTypeOrExtensionScope, portion->getReferrentOfScope (this ));
1599
1555
1600
1556
const Decl *
0 commit comments