1- // ===--- TypeRefinementContext .h - Swift Refinement Context ----- *- C++ -*-===//
1+ // ===--- AvailabilityScope .h - Swift Availability Scopes ----*- C++ ---- -*-===//
22//
33// This source file is part of the Swift.org open source project
44//
1010//
1111// ===----------------------------------------------------------------------===//
1212//
13- // This file defines the TypeRefinementContext class. A TypeRefinementContext
14- // is the semantic construct that refines a type within its lexical scope.
13+ // This file defines the AvailabilityScope class. An AvailabilityScope
14+ // is the semantic construct that refines a source range with constraints
15+ // declared using @available and if #available.
1516//
1617// ===----------------------------------------------------------------------===//
1718
18- #ifndef SWIFT_TYPEREFINEMENTCONTEXT_H
19- #define SWIFT_TYPEREFINEMENTCONTEXT_H
19+ #ifndef SWIFT_AVAILABILITYSCOPE_H
20+ #define SWIFT_AVAILABILITYSCOPE_H
2021
2122#include " swift/AST/Availability.h"
2223#include " swift/AST/AvailabilityContext.h"
3031#include " llvm/Support/ErrorHandling.h"
3132
3233namespace swift {
33- class BraceStmt ;
34- class Decl ;
35- class IfStmt ;
36- class GuardStmt ;
37- class SourceFile ;
38- class Stmt ;
39- class Expr ;
40- class StmtConditionElement ;
41-
42- // / Represents a lexical context in which types are refined. For now,
43- // / types are refined solely for API availability checking, based on
44- // / the operating system versions that the refined context may execute
45- // / upon.
46- // /
47- // / These refinement contexts form a lexical tree parallel to the AST but much
48- // / more sparse: we only introduce refinement contexts when there is something
49- // / to refine.
50- class TypeRefinementContext : public ASTAllocated <TypeRefinementContext> {
34+ class BraceStmt ;
35+ class Decl ;
36+ class IfStmt ;
37+ class GuardStmt ;
38+ class SourceFile ;
39+ class Stmt ;
40+ class Expr ;
41+ class StmtConditionElement ;
42+
43+ // / Represents a lexical context in which availability is refined. These scopes
44+ // / form a lexical tree parallel to the AST but much more sparse: we only
45+ // / introduce availability scopes when there is something to refine.
46+ class AvailabilityScope : public ASTAllocated <AvailabilityScope> {
5147
5248public:
53- // / Describes the reason a type refinement context was introduced.
49+ // / Describes the reason an availability scope was introduced.
5450 enum class Reason {
55- // / The root refinement context .
51+ // / The root availability scope .
5652 Root,
5753
5854 // / The context was introduced by a declaration with an explicit
@@ -98,9 +94,9 @@ class TypeRefinementContext : public ASTAllocated<TypeRefinementContext> {
9894 };
9995
10096private:
101- friend class ExpandChildTypeRefinementContextsRequest ;
97+ friend class ExpandChildAvailabilityScopesRequest ;
10298
103- // / Represents the AST node that introduced a refinement context .
99+ // / Represents the AST node that introduced an availability scope .
104100 class IntroNode {
105101 Reason IntroReason;
106102 union {
@@ -118,9 +114,10 @@ class TypeRefinementContext : public ASTAllocated<TypeRefinementContext> {
118114 : IntroReason(introReason), D(D) {
119115 (void )getAsDecl (); // check that assertion succeeds
120116 }
121- IntroNode (IfStmt *IS, bool IsThen) :
122- IntroReason (IsThen ? Reason::IfStmtThenBranch : Reason::IfStmtElseBranch),
123- IS (IS) {}
117+ IntroNode (IfStmt *IS, bool IsThen)
118+ : IntroReason(IsThen ? Reason::IfStmtThenBranch
119+ : Reason::IfStmtElseBranch),
120+ IS (IS) {}
124121 IntroNode (PoundAvailableInfo *PAI)
125122 : IntroReason(Reason::ConditionFollowingAvailabilityQuery), PAI(PAI) {}
126123 IntroNode (GuardStmt *GS, bool IsFallthrough)
@@ -174,69 +171,68 @@ class TypeRefinementContext : public ASTAllocated<TypeRefinementContext> {
174171 // / root context.
175172 const AvailabilityContext AvailabilityInfo;
176173
177- std::vector<TypeRefinementContext *> Children;
174+ std::vector<AvailabilityScope *> Children;
178175
179176 struct {
180177 // / Whether this node has child nodes that have not yet been expanded.
181178 unsigned needsExpansion : 1 ;
182179 } LazyInfo = {};
183180
184- void verify (const TypeRefinementContext *parent, ASTContext &ctx) const ;
181+ void verify (const AvailabilityScope *parent, ASTContext &ctx) const ;
185182
186- TypeRefinementContext (ASTContext &Ctx, IntroNode Node,
187- TypeRefinementContext *Parent, SourceRange SrcRange,
188- const AvailabilityContext Info);
183+ AvailabilityScope (ASTContext &Ctx, IntroNode Node, AvailabilityScope *Parent,
184+ SourceRange SrcRange, const AvailabilityContext Info);
189185
190186public:
191- // / Create the root refinement context for the given SourceFile.
192- static TypeRefinementContext *
193- createForSourceFile (SourceFile *SF, const AvailabilityContext Info);
194-
195- // / Create a refinement context for the given declaration.
196- static TypeRefinementContext *createForDecl (ASTContext &Ctx, Decl *D,
197- TypeRefinementContext *Parent,
198- const AvailabilityContext Info,
199- SourceRange SrcRange);
200-
201- // / Create a refinement context for the given declaration.
202- static TypeRefinementContext *
203- createForDeclImplicit (ASTContext &Ctx, Decl *D, TypeRefinementContext *Parent,
187+ // / Create the root availability scope for the given SourceFile.
188+ static AvailabilityScope * createForSourceFile (SourceFile *SF,
189+ const AvailabilityContext Info);
190+
191+ // / Create an availability scope for the given declaration.
192+ static AvailabilityScope *createForDecl (ASTContext &Ctx, Decl *D,
193+ AvailabilityScope *Parent,
194+ const AvailabilityContext Info,
195+ SourceRange SrcRange);
196+
197+ // / Create an availability scope for the given declaration.
198+ static AvailabilityScope *
199+ createForDeclImplicit (ASTContext &Ctx, Decl *D, AvailabilityScope *Parent,
204200 const AvailabilityContext Info, SourceRange SrcRange);
205201
206- // / Create a refinement context for the Then branch of the given IfStmt.
207- static TypeRefinementContext *
208- createForIfStmtThen (ASTContext &Ctx, IfStmt *S, TypeRefinementContext *Parent,
209- const AvailabilityContext Info);
202+ // / Create an availability scope for the Then branch of the given IfStmt.
203+ static AvailabilityScope * createForIfStmtThen (ASTContext &Ctx, IfStmt *S,
204+ AvailabilityScope *Parent,
205+ const AvailabilityContext Info);
210206
211- // / Create a refinement context for the Else branch of the given IfStmt.
212- static TypeRefinementContext *
213- createForIfStmtElse (ASTContext &Ctx, IfStmt *S, TypeRefinementContext *Parent,
214- const AvailabilityContext Info);
207+ // / Create an availability scope for the Else branch of the given IfStmt.
208+ static AvailabilityScope * createForIfStmtElse (ASTContext &Ctx, IfStmt *S,
209+ AvailabilityScope *Parent,
210+ const AvailabilityContext Info);
215211
216- // / Create a refinement context for the true-branch control flow to
212+ // / Create an availability scope for the true-branch control flow to
217213 // / further StmtConditionElements following a #available() query in
218214 // / a StmtCondition.
219- static TypeRefinementContext *
215+ static AvailabilityScope *
220216 createForConditionFollowingQuery (ASTContext &Ctx, PoundAvailableInfo *PAI,
221217 const StmtConditionElement &LastElement,
222- TypeRefinementContext *Parent,
218+ AvailabilityScope *Parent,
223219 const AvailabilityContext Info);
224220
225- // / Create a refinement context for the fallthrough of a GuardStmt.
226- static TypeRefinementContext *createForGuardStmtFallthrough (
221+ // / Create an availability scope for the fallthrough of a GuardStmt.
222+ static AvailabilityScope *createForGuardStmtFallthrough (
227223 ASTContext &Ctx, GuardStmt *RS, BraceStmt *ContainingBraceStmt,
228- TypeRefinementContext *Parent, const AvailabilityContext Info);
224+ AvailabilityScope *Parent, const AvailabilityContext Info);
229225
230- // / Create a refinement context for the else branch of a GuardStmt.
231- static TypeRefinementContext *
226+ // / Create an availability scope for the else branch of a GuardStmt.
227+ static AvailabilityScope *
232228 createForGuardStmtElse (ASTContext &Ctx, GuardStmt *RS,
233- TypeRefinementContext *Parent,
229+ AvailabilityScope *Parent,
234230 const AvailabilityContext Info);
235231
236- // / Create a refinement context for the body of a WhileStmt.
237- static TypeRefinementContext *
232+ // / Create an availability scope for the body of a WhileStmt.
233+ static AvailabilityScope *
238234 createForWhileStmtBody (ASTContext &Ctx, WhileStmt *WS,
239- TypeRefinementContext *Parent,
235+ AvailabilityScope *Parent,
240236 const AvailabilityContext Info);
241237
242238 Decl *getDeclOrNull () const {
@@ -246,23 +242,23 @@ class TypeRefinementContext : public ASTAllocated<TypeRefinementContext> {
246242 return nullptr ;
247243 }
248244
249- // / Returns the reason this context was introduced.
245+ // / Returns the reason this scope was introduced.
250246 Reason getReason () const ;
251-
252- // / Returns the AST node that introduced this refinement context . Note that
253- // / this node may be different than the refined range. For example, a
254- // / refinement context covering an IfStmt Then branch will have the
247+
248+ // / Returns the AST node that introduced this availability scope . Note that
249+ // / this node may be different than the refined range. For example, an
250+ // / availability scope covering an IfStmt Then branch will have the
255251 // / IfStmt as the introduction node (and its reason as IfStmtThenBranch)
256252 // / but its source range will cover the Then branch.
257253 IntroNode getIntroductionNode () const { return Node; }
258-
259- // / Returns the location of the node that introduced this refinement context
254+
255+ // / Returns the location of the node that introduced this availability scope
260256 // / or an invalid location if the context reflects the minimum deployment
261- // target.
257+ // / target.
262258 SourceLoc getIntroductionLoc () const ;
263259
264260 // / Returns the source range covering a _single_ decl-attribute or statement
265- // / condition that introduced the refinement context for a given platform
261+ // / condition that introduced the availability scope for a given platform
266262 // / version; if zero or multiple such responsible attributes or statements
267263 // / exist, returns an invalid SourceRange.
268264 SourceRange
@@ -274,27 +270,26 @@ class TypeRefinementContext : public ASTAllocated<TypeRefinementContext> {
274270 // / source, if applicable. Otherwise, returns null.
275271 std::optional<const AvailabilityRange> getExplicitAvailabilityRange () const ;
276272
277- // / Returns the source range on which this context refines types .
273+ // / Returns the source range this scope represents .
278274 SourceRange getSourceRange () const { return SrcRange; }
279275
280- // / Returns the availability context of code contained in this context .
276+ // / Returns the availability context of code contained in this scope .
281277 const AvailabilityContext getAvailabilityContext () const {
282278 return AvailabilityInfo;
283279 }
284280
285281 // / Returns the platform version range that can be assumed present at run
286- // / time when running code contained in this context .
282+ // / time when running code contained in this scope .
287283 const AvailabilityRange getPlatformAvailabilityRange () const {
288284 return AvailabilityInfo.getPlatformRange ();
289285 }
290286
291- // / Adds a child refinement context .
292- void addChild (TypeRefinementContext *Child, ASTContext &Ctx);
287+ // / Adds a child availability scope .
288+ void addChild (AvailabilityScope *Child, ASTContext &Ctx);
293289
294- // / Returns the innermost TypeRefinementContext descendant of this context
290+ // / Returns the innermost AvailabilityScope descendant of this scope
295291 // / for the given source location.
296- TypeRefinementContext *findMostRefinedSubContext (SourceLoc Loc,
297- ASTContext &Ctx);
292+ AvailabilityScope *findMostRefinedSubContext (SourceLoc Loc, ASTContext &Ctx);
298293
299294 bool getNeedsExpansion () const { return LazyInfo.needsExpansion ; }
300295
@@ -309,15 +304,14 @@ class TypeRefinementContext : public ASTAllocated<TypeRefinementContext> {
309304 SWIFT_DEBUG_DUMPER (dump(SourceManager &SrcMgr));
310305 void dump (raw_ostream &OS, SourceManager &SrcMgr) const ;
311306 void print (raw_ostream &OS, SourceManager &SrcMgr, unsigned Indent = 0 ) const ;
312-
307+
313308 static StringRef getReasonName (Reason R);
314309};
315310
316- void simple_display (llvm::raw_ostream &out,
317- const TypeRefinementContext *trc);
311+ void simple_display (llvm::raw_ostream &out, const AvailabilityScope *scope);
318312
319- inline SourceLoc extractNearestSourceLoc (const TypeRefinementContext *TRC ) {
320- return TRC ->getIntroductionLoc ();
313+ inline SourceLoc extractNearestSourceLoc (const AvailabilityScope *scope ) {
314+ return scope ->getIntroductionLoc ();
321315}
322316
323317} // end namespace swift
0 commit comments