-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathAvailabilityScopeBuilder.cpp
1168 lines (994 loc) · 45 KB
/
AvailabilityScopeBuilder.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
//===--- AvailabilityScopeBuilder.cpp - Swift Availability Scope Builder --===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 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 the AvailabilityScope::buildForSourceFile() function.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/AvailabilityScope.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/AvailabilityInference.h"
#include "swift/AST/AvailabilitySpec.h"
#include "swift/AST/Decl.h"
#include "swift/AST/DeclExportabilityVisitor.h"
#include "swift/AST/DiagnosticsParse.h"
#include "swift/AST/DiagnosticsSema.h"
#include "swift/AST/PrettyStackTrace.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/Parse/Lexer.h"
using namespace swift;
/// Returns true if there is any availability attribute on the declaration
/// that is active.
// FIXME: [availability] De-duplicate this with TypeCheckAvailability.cpp.
static bool hasActiveAvailableAttribute(const Decl *decl, ASTContext &ctx) {
decl = decl->getAbstractSyntaxDeclForAttributes();
for (auto attr : decl->getSemanticAvailableAttrs()) {
if (attr.isActive(ctx))
return true;
}
return false;
}
static bool computeContainedByDeploymentTarget(AvailabilityScope *scope,
ASTContext &ctx) {
return scope->getPlatformAvailabilityRange().isContainedIn(
AvailabilityRange::forDeploymentTarget(ctx));
}
namespace {
/// A class that walks the AST to build the availability scope tree.
class AvailabilityScopeBuilder : private ASTWalker {
ASTContext &Context;
/// Represents an entry in a stack of active availability scopes. The stack is
/// used to facilitate building the availability scope tree structure. A new
/// scope is pushed onto this stack before visiting children whenever the
/// current AST node requires a new context and the scope is then popped
/// post-visitation.
struct ContextInfo {
AvailabilityScope *Scope;
/// The AST node. This node can be null (ParentTy()),
/// indicating that custom logic elsewhere will handle removing
/// the context when needed.
ParentTy ScopeNode;
bool ContainedByDeploymentTarget;
};
std::vector<ContextInfo> ContextStack;
llvm::SmallVector<const Decl *, 4> ConcreteDeclStack;
/// Represents an entry in a stack of pending decl body availability scopes.
/// Scopes in this stack should be pushed onto \p ContextStack when
/// \p BodyStmt is encountered.
struct DeclBodyContextInfo {
Decl *Decl;
llvm::DenseMap<ASTNode, AvailabilityScope *> BodyScopes;
};
std::vector<DeclBodyContextInfo> DeclBodyContextStack;
std::vector<const DeclContext *> DeclContextStack;
AvailabilityScope *getCurrentScope() { return ContextStack.back().Scope; }
const DeclContext *getCurrentDeclContext() const {
assert(!DeclContextStack.empty());
return DeclContextStack.back();
}
bool isCurrentScopeContainedByDeploymentTarget() {
return ContextStack.back().ContainedByDeploymentTarget;
}
const AvailabilityContext constrainCurrentAvailabilityWithPlatformRange(
const AvailabilityRange &platformRange) {
auto availability = getCurrentScope()->getAvailabilityContext();
availability.constrainWithPlatformRange(platformRange, Context);
return availability;
}
void pushContext(AvailabilityScope *scope, ParentTy popAfterNode) {
ContextInfo info;
info.Scope = scope;
info.ScopeNode = popAfterNode;
if (!ContextStack.empty() && isCurrentScopeContainedByDeploymentTarget()) {
assert(computeContainedByDeploymentTarget(scope, Context) &&
"incorrectly skipping computeContainedByDeploymentTarget()");
info.ContainedByDeploymentTarget = true;
} else {
info.ContainedByDeploymentTarget =
computeContainedByDeploymentTarget(scope, Context);
}
ContextStack.push_back(info);
}
void pushDeclBodyContext(
Decl *decl, llvm::SmallVector<std::pair<ASTNode, AvailabilityScope *>, 4>
nodesAndScopes) {
DeclBodyContextInfo info;
info.Decl = decl;
for (auto nodeAndScope : nodesAndScopes) {
info.BodyScopes.insert(nodeAndScope);
}
DeclBodyContextStack.push_back(info);
}
const char *stackTraceAction() const {
return "building availabilty scope for";
}
friend class swift::ExpandChildAvailabilityScopesRequest;
public:
AvailabilityScopeBuilder(AvailabilityScope *scope, ASTContext &ctx)
: Context(ctx) {
assert(scope);
pushContext(scope, ParentTy());
DeclContextStack.push_back(scope->getIntroductionNode().getDeclContext());
}
void build(Decl *decl) {
PrettyStackTraceDecl trace(stackTraceAction(), decl);
unsigned stackHeight = ContextStack.size();
decl->walk(*this);
assert(ContextStack.size() == stackHeight);
(void)stackHeight;
}
void build(Stmt *stmt) {
PrettyStackTraceStmt trace(Context, stackTraceAction(), stmt);
unsigned stackHeight = ContextStack.size();
stmt->walk(*this);
assert(ContextStack.size() == stackHeight);
(void)stackHeight;
}
void build(Expr *expr) {
PrettyStackTraceExpr trace(Context, stackTraceAction(), expr);
unsigned stackHeight = ContextStack.size();
expr->walk(*this);
assert(ContextStack.size() == stackHeight);
(void)stackHeight;
}
private:
MacroWalking getMacroWalkingBehavior() const override {
// Expansion buffers will have their type availability scopes built lazily.
return MacroWalking::Arguments;
}
SequenceWalking getSequenceWalkingBehavior() const override {
// Since availability scopes may be built at arbitrary times, the builder
// may encounter ASTs where SequenceExprs still exist and have not been
// folded, or it may encounter folded SequenceExprs that have not been
// removed from the AST. When folded exprs are encountered, its important
// to avoid walking into the same AST nodes twice.
return SequenceWalking::OnlyWalkFirstOperatorWhenFolded;
}
/// Check whether this declaration is within a macro expansion buffer that
/// will have its own availability scope that will be lazily expanded.
bool isDeclInMacroExpansion(Decl *decl) const override {
// If it's not in a macro expansion relative to its context, it's not
// considered to be in a macro expansion.
if (!decl->isInMacroExpansionInContext())
return false;
auto parentModule = decl->getDeclContext()->getParentModule();
auto *declFile =
parentModule->getSourceFileContainingLocation(decl->getLoc());
if (!declFile)
return false;
// Look for a parent context that implies that we are producing an
// availability scope for this expansion.
for (auto iter = ContextStack.rbegin(), endIter = ContextStack.rend();
iter != endIter; ++iter) {
const auto &context = *iter;
if (auto scope = context.Scope) {
// If the context is the same source file, don't treat it as an
// expansion.
auto introNode = scope->getIntroductionNode();
switch (scope->getReason()) {
case AvailabilityScope::Reason::Root:
if (auto contextFile = introNode.getAsSourceFile())
if (declFile == contextFile)
return false;
break;
case AvailabilityScope::Reason::Decl:
case AvailabilityScope::Reason::DeclImplicit:
// If the context is a declaration, check whether the declaration
// is in the same source file as this declaration.
if (auto contextDecl = introNode.getAsDecl()) {
if (decl == contextDecl)
return false;
auto contextModule =
contextDecl->getDeclContext()->getParentModule();
SourceLoc contextDeclLoc = contextDecl->getLoc();
auto contextDeclFile =
contextModule->getSourceFileContainingLocation(contextDeclLoc);
if (declFile == contextDeclFile)
return false;
}
break;
case AvailabilityScope::Reason::IfStmtThenBranch:
case AvailabilityScope::Reason::IfStmtElseBranch:
case AvailabilityScope::Reason::ConditionFollowingAvailabilityQuery:
case AvailabilityScope::Reason::GuardStmtFallthrough:
case AvailabilityScope::Reason::GuardStmtElseBranch:
case AvailabilityScope::Reason::WhileStmtBody:
// Nothing to check here.
break;
}
}
}
return true;
}
bool shouldSkipDecl(Decl *decl) const {
// Only visit a node that has a corresponding concrete syntax node if we are
// already walking that concrete syntax node.
auto *concreteDecl = decl->getConcreteSyntaxDeclForAttributes();
if (concreteDecl != decl) {
if (ConcreteDeclStack.empty() || ConcreteDeclStack.back() != concreteDecl)
return true;
}
return false;
}
PreWalkAction walkToDeclPre(Decl *decl) override {
PrettyStackTraceDecl trace(stackTraceAction(), decl);
// Implicit decls don't have source locations so they cannot have a scope.
// However, some implicit nodes contain non-implicit nodes (e.g. defer
// blocks) so we must continue through them.
if (!decl->isImplicit()) {
if (shouldSkipDecl(decl))
return Action::SkipNode();
// The AST of this decl may not be ready to traverse yet if it hasn't been
// full typechecked. If that's the case, we leave a placeholder node in
// the tree to indicate that the subtree should be expanded lazily when it
// needs to be traversed.
if (buildLazyContextForDecl(decl))
return Action::SkipNode();
// Adds in a scope that covers the entire declaration.
if (auto declScope = getNewContextForSignatureOfDecl(decl)) {
pushContext(declScope, decl);
}
// Create scopes that cover only the body of the declaration.
buildContextsForBodyOfDecl(decl);
}
if (auto *declContext = dyn_cast<DeclContext>(decl)) {
DeclContextStack.push_back(declContext);
}
// If this decl is the concrete syntax decl for some abstract syntax decl,
// push it onto the stack so that the abstract syntax decls may be visited.
auto *abstractDecl = decl->getAbstractSyntaxDeclForAttributes();
if (abstractDecl != decl) {
ConcreteDeclStack.push_back(decl);
}
return Action::Continue();
}
PostWalkAction walkToDeclPost(Decl *decl) override {
if (!ConcreteDeclStack.empty() && ConcreteDeclStack.back() == decl) {
ConcreteDeclStack.pop_back();
}
if (auto *declContext = dyn_cast<DeclContext>(decl)) {
assert(DeclContextStack.back() == declContext);
DeclContextStack.pop_back();
}
while (ContextStack.back().ScopeNode.getAsDecl() == decl) {
ContextStack.pop_back();
}
while (!DeclBodyContextStack.empty() &&
DeclBodyContextStack.back().Decl == decl) {
// All pending body scopes should have been consumed.
assert(DeclBodyContextStack.back().BodyScopes.empty());
DeclBodyContextStack.pop_back();
}
return Action::Continue();
}
bool shouldBuildLazyContextForDecl(Decl *decl) {
// Skip functions that have unparsed bodies on an initial descent to avoid
// eagerly parsing bodies unnecessarily.
if (auto *afd = dyn_cast<AbstractFunctionDecl>(decl)) {
if (afd->hasBody() && !afd->isBodySkipped() &&
!afd->getBody(/*canSynthesize=*/false))
return true;
}
// Pattern binding declarations may have attached property wrappers that
// get expanded from macros attached to the parent declaration. We must
// not eagerly expand the attached property wrappers to avoid request
// cycles.
if (isa<PatternBindingDecl>(decl))
return true;
if (isa<ExtensionDecl>(decl))
return true;
return false;
}
/// For declarations that were previously skipped prepare the AST before
/// building out scopes.
void prepareDeclForLazyExpansion(Decl *decl) {
if (auto afd = dyn_cast<AbstractFunctionDecl>(decl))
(void)afd->getBody(/*canSynthesize=*/true);
}
/// Constructs a placeholder scope that should be expanded later. This is
/// useful for postponing unnecessary work (and request triggers) when
/// initally building out the scope subtree under a declaration. Lazy nodes
/// constructed here will be expanded by ExpandChildAvailabilityScopesRequest.
/// Returns true if a node was created.
bool buildLazyContextForDecl(Decl *decl) {
// Check whether the current scope is already a lazy placeholder. If it is,
// we should try to expand it rather than creating a new placeholder.
auto currentScope = getCurrentScope();
if (currentScope->getNeedsExpansion() &&
currentScope->getDeclOrNull() == decl)
return false;
if (!shouldBuildLazyContextForDecl(decl))
return false;
// If we've made it this far then we've identified a declaration that
// requires lazy expansion later.
auto lazyScope = AvailabilityScope::createForDeclImplicit(
Context, decl, currentScope, currentScope->getAvailabilityContext(),
refinementSourceRangeForDecl(decl));
lazyScope->setNeedsExpansion(true);
return true;
}
/// Returns a new context to be introduced for the declaration, or nullptr
/// if no new context should be introduced.
AvailabilityScope *getNewContextForSignatureOfDecl(Decl *decl) {
if (!isa<ValueDecl>(decl) && !isa<EnumCaseDecl>(decl) &&
!isa<ExtensionDecl>(decl) && !isa<MacroExpansionDecl>(decl) &&
!isa<PatternBindingDecl>(decl))
return nullptr;
// Only introduce for an AbstractStorageDecl if it is not local. We
// introduce for the non-local case because these may have getters and
// setters (and these may be synthesized, so they might not even exist yet).
if (isa<AbstractStorageDecl>(decl) &&
decl->getDeclContext()->isLocalContext())
return nullptr;
// Don't introduce for abstract syntax nodes that have separate concrete
// syntax nodes. The scope will be introduced for the concrete node instead.
if (decl->getConcreteSyntaxDeclForAttributes() != decl)
return nullptr;
// Declarations with explicit availability attributes always get a scope.
if (hasActiveAvailableAttribute(decl, Context)) {
return AvailabilityScope::createForDecl(
Context, decl, getCurrentScope(),
getEffectiveAvailabilityForDeclSignature(decl),
refinementSourceRangeForDecl(decl));
}
// Declarations without explicit availability attributes get a scope if they
// are effectively less available than the surrounding context. For example,
// an internal property in a public struct can be effectively less available
// than the containing struct decl because the internal property will only
// be accessed by code running at the deployment target or later.
auto currentAvailability = getCurrentScope()->getAvailabilityContext();
auto effectiveAvailability = getEffectiveAvailabilityForDeclSignature(decl);
if (currentAvailability != effectiveAvailability)
return AvailabilityScope::createForDeclImplicit(
Context, decl, getCurrentScope(), effectiveAvailability,
refinementSourceRangeForDecl(decl));
return nullptr;
}
const AvailabilityContext
getEffectiveAvailabilityForDeclSignature(const Decl *decl) {
auto effectiveIntroduction = AvailabilityRange::alwaysAvailable();
// Availability attributes are found abstract syntax decls.
decl = decl->getAbstractSyntaxDeclForAttributes();
// As a special case, extension decls are treated as effectively as
// available as the nominal type they extend, up to the deployment target.
// This rule is a convenience for library authors who have written
// extensions without specifying availabilty on the extension itself.
if (auto *extension = dyn_cast<ExtensionDecl>(decl)) {
auto extendedType = extension->getExtendedType();
if (extendedType && !hasActiveAvailableAttribute(decl, Context)) {
effectiveIntroduction.intersectWith(
swift::AvailabilityInference::inferForType(extendedType));
// We want to require availability to be specified on extensions of
// types that would be potentially unavailable to the module containing
// the extension, so limit the effective availability to the deployment
// target.
effectiveIntroduction.unionWith(
AvailabilityRange::forDeploymentTarget(Context));
}
}
if (shouldConstrainSignatureToDeploymentTarget(decl))
effectiveIntroduction.intersectWith(
AvailabilityRange::forDeploymentTarget(Context));
auto availability = getCurrentScope()->getAvailabilityContext();
availability.constrainWithDeclAndPlatformRange(decl, effectiveIntroduction);
return availability;
}
/// Checks whether the entire declaration, including its signature, should be
/// constrained to the deployment target. Generally public API declarations
/// are not constrained since they appear in the interface of the module and
/// may be consumed by clients with lower deployment targets, but there are
/// some exceptions.
bool shouldConstrainSignatureToDeploymentTarget(const Decl *decl) {
if (isCurrentScopeContainedByDeploymentTarget())
return false;
// A declaration inside of a local context always inherits the availability
// of the parent.
if (decl->getDeclContext()->isLocalContext())
return false;
// As a convenience, explicitly unavailable decls are constrained to the
// deployment target. There's not much benefit to checking these decls at a
// lower availability version floor since they can't be invoked by clients.
if (getCurrentScope()->getAvailabilityContext().isUnavailable() ||
decl->isUnavailable())
return true;
// To remain compatible with a lot of existing SPIs that are declared
// without availability attributes, constrain them to the deployment target
// too.
if (decl->isSPI())
return true;
return !isExported(decl);
}
/// Returns the source range which should be refined by declaration. This
/// provides a convenient place to specify the refined range when it is
/// different than the declaration's source range.
SourceRange refinementSourceRangeForDecl(Decl *decl) {
// We require a valid range in order to be able to query for the scope
// corresponding to a given SourceLoc.
// If this assert fires, it means we have probably synthesized an implicit
// declaration without location information. The appropriate fix is
// probably to gin up a source range for the declaration when synthesizing
// it.
assert(decl->getSourceRange().isValid());
auto &ctx = decl->getASTContext();
SourceRange range;
if (auto *storageDecl = dyn_cast<AbstractStorageDecl>(decl)) {
// Use the declaration's availability for the context when checking
// the bodies of its accessors.
range = storageDecl->getSourceRange();
// HACK: For synthesized trivial accessors we may have not a valid
// location for the end of the braces, so in that case we will fall back
// to using the range for the storage declaration. The right fix here is
// to update AbstractStorageDecl::addTrivialAccessors() to take brace
// locations and have callers of that method provide appropriate source
// locations.
SourceRange bracesRange = storageDecl->getBracesRange();
if (bracesRange.isValid()) {
range.widen(bracesRange);
}
} else {
range = decl->getSourceRangeIncludingAttrs();
}
range.End = Lexer::getLocForEndOfToken(ctx.SourceMgr, range.End);
return range;
}
/// Enumerate the AST nodes and their corresponding source ranges for
/// the body (or bodies) of the given declaration.
void enumerateBodyRanges(
Decl *decl,
llvm::function_ref<void(Decl *decl, ASTNode body, SourceRange)>
acceptBody) {
// Top level code always uses the deployment target.
if (auto tlcd = dyn_cast<TopLevelCodeDecl>(decl)) {
if (auto bodyStmt = tlcd->getBody()) {
acceptBody(tlcd, bodyStmt, refinementSourceRangeForDecl(tlcd));
}
return;
}
// For functions, provide the body source range.
if (auto afd = dyn_cast<AbstractFunctionDecl>(decl)) {
if (!afd->isImplicit()) {
if (auto body = afd->getBody(/*canSynthesize=*/false)) {
acceptBody(afd, body, afd->getBodySourceRange());
}
}
return;
}
// Pattern binding declarations have initial values that are their
// bodies.
if (auto *pbd = dyn_cast<PatternBindingDecl>(decl)) {
for (unsigned index : range(pbd->getNumPatternEntries())) {
auto var = pbd->getAnchoringVarDecl(index);
if (!var)
continue;
auto *initExpr = pbd->getInit(index);
if (initExpr && !initExpr->isImplicit()) {
assert(initExpr->getSourceRange().isValid());
// Create a scope for the init written in the source.
acceptBody(var, initExpr, initExpr->getSourceRange());
}
}
return;
}
}
/// Creates an implicit decl scope specifying the deployment target for
/// `range` in `decl`.
AvailabilityScope *
createImplicitDeclContextForDeploymentTarget(Decl *decl, SourceRange range) {
auto availability = constrainCurrentAvailabilityWithPlatformRange(
AvailabilityRange::forDeploymentTarget(Context));
return AvailabilityScope::createForDeclImplicit(
Context, decl, getCurrentScope(), availability, range);
}
/// Determine whether the body of the given declaration has
/// deployment-target availability.
static bool bodyIsDeploymentTarget(Decl *decl) {
if (auto afd = dyn_cast<AbstractFunctionDecl>(decl)) {
return afd->getResilienceExpansion() != ResilienceExpansion::Minimal;
}
if (auto var = dyn_cast<VarDecl>(decl)) {
// Var decls may have associated pattern binding decls or property
// wrappers with init expressions. Those expressions need to be
// constrained to the deployment target unless they are exposed to
// clients.
return var->hasInitialValue() && !var->isInitExposedToClients();
}
return true;
}
void buildContextsForBodyOfDecl(Decl *decl) {
// Are we already constrained by the deployment target and the declaration
// doesn't explicitly allow unsafe constructs in its definition, adding
// new contexts won't change availability.
if (isCurrentScopeContainedByDeploymentTarget())
return;
// Enumerate all of the body scopes to apply availability.
llvm::SmallVector<std::pair<ASTNode, AvailabilityScope *>, 4>
nodesAndScopes;
enumerateBodyRanges(decl, [&](Decl *decl, ASTNode body, SourceRange range) {
auto availability = getCurrentScope()->getAvailabilityContext();
// Apply deployment-target availability if appropriate for this body.
if (!isCurrentScopeContainedByDeploymentTarget() &&
bodyIsDeploymentTarget(decl)) {
availability.constrainWithPlatformRange(
AvailabilityRange::forDeploymentTarget(Context), Context);
}
nodesAndScopes.push_back(
{body, AvailabilityScope::createForDeclImplicit(
Context, decl, getCurrentScope(), availability, range)});
});
if (nodesAndScopes.size() > 0)
pushDeclBodyContext(decl, nodesAndScopes);
if (!isCurrentScopeContainedByDeploymentTarget()) {
// Pattern binding declarations can have children corresponding to
// property wrappers, which we handle separately.
if (auto *pbd = dyn_cast<PatternBindingDecl>(decl)) {
// Ideally any init expression would be returned by `getInit()` above.
// However, for property wrappers it doesn't get populated until
// typechecking completes (which is too late). Instead, we find the
// the property wrapper attribute and use its source range to create a
// scope for the initializer expression.
//
// FIXME: Since we don't have an expression here, we can't build out its
// scope. If the Expr that will eventually be created contains a closure
// expression, then it might have AST nodes that need to be refined. For
// example, property wrapper initializers that takes block arguments
// are not handled correctly because of this (rdar://77841331).
if (auto firstVar = pbd->getAnchoringVarDecl(0)) {
if (firstVar->hasInitialValue() &&
!firstVar->isInitExposedToClients()) {
for (auto *wrapper : firstVar->getAttachedPropertyWrappers()) {
createImplicitDeclContextForDeploymentTarget(firstVar,
wrapper->getRange());
}
}
}
}
}
}
PreWalkResult<Stmt *> walkToStmtPre(Stmt *stmt) override {
PrettyStackTraceStmt trace(Context, stackTraceAction(), stmt);
if (consumeDeclBodyContextIfNecessary(stmt)) {
return Action::Continue(stmt);
}
if (auto *ifStmt = dyn_cast<IfStmt>(stmt)) {
buildIfStmtRefinementContext(ifStmt);
return Action::SkipNode(stmt);
}
if (auto *guardStmt = dyn_cast<GuardStmt>(stmt)) {
buildGuardStmtRefinementContext(guardStmt);
return Action::SkipNode(stmt);
}
if (auto *whileStmt = dyn_cast<WhileStmt>(stmt)) {
buildWhileStmtRefinementContext(whileStmt);
return Action::SkipNode(stmt);
}
return Action::Continue(stmt);
}
PostWalkResult<Stmt *> walkToStmtPost(Stmt *stmt) override {
// If we have multiple guard statements in the same block
// then we may have multiple availability scopes to pop
// after walking that block.
while (!ContextStack.empty() &&
ContextStack.back().ScopeNode.getAsStmt() == stmt) {
ContextStack.pop_back();
}
return Action::Continue(stmt);
}
/// Attempts to consume a scope from the `BodyScopes` of the top of
/// `DeclBodyContextStack`. Returns \p true if a scope was pushed.
template <typename T>
bool consumeDeclBodyContextIfNecessary(T body) {
if (DeclBodyContextStack.empty())
return false;
auto &info = DeclBodyContextStack.back();
auto iter = info.BodyScopes.find(body);
if (iter == info.BodyScopes.end())
return false;
pushContext(iter->getSecond(), body);
info.BodyScopes.erase(iter);
return true;
}
/// Builds the availability scope hierarchy for the IfStmt if the guard
/// introduces a new scope for the Then branch.
/// There is no need for the caller to explicitly traverse the children
/// of this node.
void buildIfStmtRefinementContext(IfStmt *ifStmt) {
std::optional<AvailabilityRange> thenRange;
std::optional<AvailabilityRange> elseRange;
std::tie(thenRange, elseRange) =
buildStmtConditionRefinementContext(ifStmt->getCond());
if (thenRange.has_value()) {
// Create a new context for the Then branch and traverse it in that new
// context.
auto availabilityContext =
constrainCurrentAvailabilityWithPlatformRange(thenRange.value());
auto *thenScope = AvailabilityScope::createForIfStmtThen(
Context, ifStmt, getCurrentDeclContext(), getCurrentScope(),
availabilityContext);
AvailabilityScopeBuilder(thenScope, Context).build(ifStmt->getThenStmt());
} else {
build(ifStmt->getThenStmt());
}
Stmt *elseStmt = ifStmt->getElseStmt();
if (!elseStmt)
return;
// Refine the else branch if we're given a version range for that branch.
// For now, if present, this will only be the empty range, indicating
// that the branch is dead. We use it to suppress potential unavailability
// and deprecation diagnostics on code that definitely will not run with
// the current platform and minimum deployment target.
// If we add a more precise version range lattice (i.e., one that can
// support "<") we should create non-empty contexts for the Else branch.
if (elseRange.has_value()) {
// Create a new context for the Then branch and traverse it in that new
// context.
auto availabilityContext =
constrainCurrentAvailabilityWithPlatformRange(elseRange.value());
auto *elseScope = AvailabilityScope::createForIfStmtElse(
Context, ifStmt, getCurrentDeclContext(), getCurrentScope(),
availabilityContext);
AvailabilityScopeBuilder(elseScope, Context).build(elseStmt);
} else {
build(ifStmt->getElseStmt());
}
}
/// Builds the availability scopes for the WhileStmt if the guard
/// introduces a new availability scope for the body branch.
/// There is no need for the caller to explicitly traverse the children
/// of this node.
void buildWhileStmtRefinementContext(WhileStmt *whileStmt) {
std::optional<AvailabilityRange> bodyRange =
buildStmtConditionRefinementContext(whileStmt->getCond()).first;
if (bodyRange.has_value()) {
// Create a new context for the body and traverse it in the new
// context.
auto availabilityContext =
constrainCurrentAvailabilityWithPlatformRange(bodyRange.value());
auto *bodyScope = AvailabilityScope::createForWhileStmtBody(
Context, whileStmt, getCurrentDeclContext(), getCurrentScope(),
availabilityContext);
AvailabilityScopeBuilder(bodyScope, Context).build(whileStmt->getBody());
} else {
build(whileStmt->getBody());
}
}
/// Builds the availability scopes for the GuardStmt and pushes
/// the fallthrough scope onto the scope stack so that subsequent
/// AST elements in the same scope are analyzed in the context of the
/// fallthrough scope.
void buildGuardStmtRefinementContext(GuardStmt *guardStmt) {
// 'guard' statements fall through if all of the guard conditions are true,
// so we refine the range after the require until the end of the enclosing
// block:
//
// if ... {
// guard available(...) else { return } <-- Refined range starts here
// ...
// } <-- Refined range ends here
//
// This is slightly tricky because, unlike our other control constructs,
// the refined region is not lexically contained inside the construct
// introducing the availability scope.
std::optional<AvailabilityRange> fallthroughRange;
std::optional<AvailabilityRange> elseRange;
std::tie(fallthroughRange, elseRange) =
buildStmtConditionRefinementContext(guardStmt->getCond());
if (Stmt *elseBody = guardStmt->getBody()) {
if (elseRange.has_value()) {
auto availabilityContext =
constrainCurrentAvailabilityWithPlatformRange(elseRange.value());
auto *trueScope = AvailabilityScope::createForGuardStmtElse(
Context, guardStmt, getCurrentDeclContext(), getCurrentScope(),
availabilityContext);
AvailabilityScopeBuilder(trueScope, Context).build(elseBody);
} else {
build(elseBody);
}
}
auto *parentBrace = dyn_cast<BraceStmt>(Parent.getAsStmt());
assert(parentBrace && "Expected parent of GuardStmt to be BraceStmt");
if (!fallthroughRange.has_value())
return;
// Create a new context for the fallthrough.
auto fallthroughAvailability =
constrainCurrentAvailabilityWithPlatformRange(fallthroughRange.value());
auto *fallthroughScope = AvailabilityScope::createForGuardStmtFallthrough(
Context, guardStmt, parentBrace, getCurrentDeclContext(),
getCurrentScope(), fallthroughAvailability);
pushContext(fallthroughScope, parentBrace);
}
/// Build the availability scopes for a StmtCondition and return a pair of
/// optional version ranges, the first for the true branch and the second
/// for the false branch. A value of `nullopt` for a given branch indicates
/// that the branch does not introduce a new scope.
std::pair<std::optional<AvailabilityRange>, std::optional<AvailabilityRange>>
buildStmtConditionRefinementContext(StmtCondition cond) {
if (Context.LangOpts.DisableAvailabilityChecking)
return {};
// Any availability scopes introduced in the statement condition will end
// at the end of the last condition element.
StmtConditionElement lastElement = cond.back();
// Keep track of how many nested availability scopes we have pushed on
// the scope stack so we can pop them when we're done building the scope
// for the StmtCondition.
unsigned nestedCount = 0;
// Tracks the potential version range when the condition is false.
auto falseFlow = AvailabilityRange::neverAvailable();
AvailabilityScope *startingScope = getCurrentScope();
// Tracks if we're refining for availability or unavailability.
std::optional<bool> isUnavailability = std::nullopt;
for (StmtConditionElement element : cond) {
auto *currentScope = getCurrentScope();
auto currentInfo = currentScope->getPlatformAvailabilityRange();
// If the element is not a condition, walk it in the current scope.
if (element.getKind() != StmtConditionElement::CK_Availability) {
// Assume any condition element that is not a #available() can
// potentially be false, so conservatively combine the version
// range of the current context with the accumulated false flow
// of all other conjuncts.
falseFlow.unionWith(currentInfo);
element.walk(*this);
continue;
}
// #available query: introduce a new availability scope for the statement
// condition elements following it.
auto *query = element.getAvailability();
if (isUnavailability == std::nullopt) {
isUnavailability = query->isUnavailability();
} else if (isUnavailability != query->isUnavailability()) {
// Mixing availability with unavailability in the same statement will
// cause the false flow's version range to be ambiguous. Report it.
//
// Technically we can support this by not refining ambiguous flows,
// but there are currently no legitimate cases where one would have
// to mix availability with unavailability.
Context.Diags.diagnose(query->getLoc(),
diag::availability_cannot_be_mixed);
break;
}
// If this query expression has no queries, we will not introduce a new
// availability scope. We do not diagnose here: a diagnostic will already
// have been emitted by the parser.
// For #unavailable, empty queries are valid as wildcards are implied.
if (!query->isUnavailability() && query->getQueries().empty())
continue;
auto spec = bestActiveSpecForQuery(query);
if (!spec) {
// We couldn't find an appropriate spec for the current platform,
// so rather than refining, emit a diagnostic and just use the current
// scope.
Context.Diags.diagnose(
query->getLoc(), diag::availability_query_required_for_platform,
platformString(targetPlatform(Context.LangOpts)));
continue;
}
AvailabilityRange newConstraint = contextForSpec(*spec, false);
query->setAvailableRange(
contextForSpec(*spec, true).getRawVersionRange());
// When compiling zippered for macCatalyst, we need to collect both
// a macOS version (the target version) and an iOS/macCatalyst version
// (the target-variant). These versions will both be passed to a runtime
// entrypoint that will check either the macOS version or the iOS
// version depending on the kind of process this code is loaded into.
if (Context.LangOpts.TargetVariant) {
auto variantSpec =
bestActiveSpecForQuery(query, /*ForTargetVariant*/ true);
if (variantSpec) {
VersionRange variantRange =
contextForSpec(*variantSpec, true).getRawVersionRange();
query->setVariantAvailableRange(variantRange);
}
}
if (spec->isWildcard()) {
// The wildcard spec '*' represents the minimum deployment target, so
// there is no need to create an availability scope for this query.
// Further, we won't diagnose for useless #available() conditions
// where * matched on this platform -- presumably those conditions are
// needed for some other platform.
continue;
}
// If the explicitly-specified (via #availability) version range for the
// current scope is completely contained in the range for the spec, then
// a version query can never be false, so the spec is useless.
// If so, report this.
auto explicitRange = currentScope->getExplicitAvailabilityRange();
if (explicitRange && explicitRange->isContainedIn(newConstraint)) {
// Unavailability scopes are always "useless" from a symbol
// availability point of view, so only useless availability specs are
// reported.
if (isUnavailability.value()) {
continue;
}
DiagnosticEngine &diags = Context.Diags;
if (currentScope->getReason() != AvailabilityScope::Reason::Root) {
PlatformKind bestPlatform = targetPlatform(Context.LangOpts);
auto Domain = spec->getDomain();
// If possible, try to report the diagnostic in terms for the
// platform the user uttered in the '#available()'. For a platform
// that inherits availability from another platform it may be
// different from the platform specified in the target triple.
if (Domain.getPlatformKind() != PlatformKind::none)
bestPlatform = Domain.getPlatformKind();
diags.diagnose(query->getLoc(),
diag::availability_query_useless_enclosing_scope,
platformString(bestPlatform));
diags.diagnose(currentScope->getIntroductionLoc(),
diag::availability_query_useless_enclosing_scope_here);
}
}
if (currentInfo.isContainedIn(newConstraint)) {
// No need to actually create the availability scope if we know it is
// useless.
continue;
}
// If the #available() is not useless then there is potential false flow,
// so join the false flow with the potential versions of the current
// context.
// We could be more precise here if we enriched the lattice to include
// ranges of the form [x, y).
falseFlow.unionWith(currentInfo);
auto constrainedAvailability =
constrainCurrentAvailabilityWithPlatformRange(newConstraint);
auto *scope = AvailabilityScope::createForConditionFollowingQuery(
Context, query, lastElement, getCurrentDeclContext(), currentScope,
constrainedAvailability);
pushContext(scope, ParentTy());
++nestedCount;
}
std::optional<AvailabilityRange> falseRefinement = std::nullopt;
// The version range for the false branch should never have any versions
// that weren't possible when the condition started evaluating.
assert(
falseFlow.isContainedIn(startingScope->getPlatformAvailabilityRange()));
// If the starting version range is not completely contained in the
// false flow version range then it must be the case that false flow range
// is strictly smaller than the starting range (because the false flow
// range *is* contained in the starting range), so we should introduce a