1
- // ===--- TypeRefinementContext .h - Swift Refinement Context ----- *- C++ -*-===//
1
+ // ===--- AvailabilityScope .h - Swift Availability Scopes ----*- C++ ---- -*-===//
2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
10
10
//
11
11
// ===----------------------------------------------------------------------===//
12
12
//
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.
15
16
//
16
17
// ===----------------------------------------------------------------------===//
17
18
18
- #ifndef SWIFT_TYPEREFINEMENTCONTEXT_H
19
- #define SWIFT_TYPEREFINEMENTCONTEXT_H
19
+ #ifndef SWIFT_AVAILABILITYSCOPE_H
20
+ #define SWIFT_AVAILABILITYSCOPE_H
20
21
21
22
#include " swift/AST/Availability.h"
22
23
#include " swift/AST/AvailabilityContext.h"
30
31
#include " llvm/Support/ErrorHandling.h"
31
32
32
33
namespace 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> {
51
47
52
48
public:
53
- // / Describes the reason a type refinement context was introduced.
49
+ // / Describes the reason an availability scope was introduced.
54
50
enum class Reason {
55
- // / The root refinement context .
51
+ // / The root availability scope .
56
52
Root,
57
53
58
54
// / The context was introduced by a declaration with an explicit
@@ -98,9 +94,9 @@ class TypeRefinementContext : public ASTAllocated<TypeRefinementContext> {
98
94
};
99
95
100
96
private:
101
- friend class ExpandChildTypeRefinementContextsRequest ;
97
+ friend class ExpandChildAvailabilityScopesRequest ;
102
98
103
- // / Represents the AST node that introduced a refinement context .
99
+ // / Represents the AST node that introduced an availability scope .
104
100
class IntroNode {
105
101
Reason IntroReason;
106
102
union {
@@ -118,9 +114,10 @@ class TypeRefinementContext : public ASTAllocated<TypeRefinementContext> {
118
114
: IntroReason(introReason), D(D) {
119
115
(void )getAsDecl (); // check that assertion succeeds
120
116
}
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) {}
124
121
IntroNode (PoundAvailableInfo *PAI)
125
122
: IntroReason(Reason::ConditionFollowingAvailabilityQuery), PAI(PAI) {}
126
123
IntroNode (GuardStmt *GS, bool IsFallthrough)
@@ -174,69 +171,68 @@ class TypeRefinementContext : public ASTAllocated<TypeRefinementContext> {
174
171
// / root context.
175
172
const AvailabilityContext AvailabilityInfo;
176
173
177
- std::vector<TypeRefinementContext *> Children;
174
+ std::vector<AvailabilityScope *> Children;
178
175
179
176
struct {
180
177
// / Whether this node has child nodes that have not yet been expanded.
181
178
unsigned needsExpansion : 1 ;
182
179
} LazyInfo = {};
183
180
184
- void verify (const TypeRefinementContext *parent, ASTContext &ctx) const ;
181
+ void verify (const AvailabilityScope *parent, ASTContext &ctx) const ;
185
182
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);
189
185
190
186
public:
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,
204
200
const AvailabilityContext Info, SourceRange SrcRange);
205
201
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);
210
206
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);
215
211
216
- // / Create a refinement context for the true-branch control flow to
212
+ // / Create an availability scope for the true-branch control flow to
217
213
// / further StmtConditionElements following a #available() query in
218
214
// / a StmtCondition.
219
- static TypeRefinementContext *
215
+ static AvailabilityScope *
220
216
createForConditionFollowingQuery (ASTContext &Ctx, PoundAvailableInfo *PAI,
221
217
const StmtConditionElement &LastElement,
222
- TypeRefinementContext *Parent,
218
+ AvailabilityScope *Parent,
223
219
const AvailabilityContext Info);
224
220
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 (
227
223
ASTContext &Ctx, GuardStmt *RS, BraceStmt *ContainingBraceStmt,
228
- TypeRefinementContext *Parent, const AvailabilityContext Info);
224
+ AvailabilityScope *Parent, const AvailabilityContext Info);
229
225
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 *
232
228
createForGuardStmtElse (ASTContext &Ctx, GuardStmt *RS,
233
- TypeRefinementContext *Parent,
229
+ AvailabilityScope *Parent,
234
230
const AvailabilityContext Info);
235
231
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 *
238
234
createForWhileStmtBody (ASTContext &Ctx, WhileStmt *WS,
239
- TypeRefinementContext *Parent,
235
+ AvailabilityScope *Parent,
240
236
const AvailabilityContext Info);
241
237
242
238
Decl *getDeclOrNull () const {
@@ -246,23 +242,23 @@ class TypeRefinementContext : public ASTAllocated<TypeRefinementContext> {
246
242
return nullptr ;
247
243
}
248
244
249
- // / Returns the reason this context was introduced.
245
+ // / Returns the reason this scope was introduced.
250
246
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
255
251
// / IfStmt as the introduction node (and its reason as IfStmtThenBranch)
256
252
// / but its source range will cover the Then branch.
257
253
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
260
256
// / or an invalid location if the context reflects the minimum deployment
261
- // target.
257
+ // / target.
262
258
SourceLoc getIntroductionLoc () const ;
263
259
264
260
// / 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
266
262
// / version; if zero or multiple such responsible attributes or statements
267
263
// / exist, returns an invalid SourceRange.
268
264
SourceRange
@@ -274,27 +270,26 @@ class TypeRefinementContext : public ASTAllocated<TypeRefinementContext> {
274
270
// / source, if applicable. Otherwise, returns null.
275
271
std::optional<const AvailabilityRange> getExplicitAvailabilityRange () const ;
276
272
277
- // / Returns the source range on which this context refines types .
273
+ // / Returns the source range this scope represents .
278
274
SourceRange getSourceRange () const { return SrcRange; }
279
275
280
- // / Returns the availability context of code contained in this context .
276
+ // / Returns the availability context of code contained in this scope .
281
277
const AvailabilityContext getAvailabilityContext () const {
282
278
return AvailabilityInfo;
283
279
}
284
280
285
281
// / 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 .
287
283
const AvailabilityRange getPlatformAvailabilityRange () const {
288
284
return AvailabilityInfo.getPlatformRange ();
289
285
}
290
286
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);
293
289
294
- // / Returns the innermost TypeRefinementContext descendant of this context
290
+ // / Returns the innermost AvailabilityScope descendant of this scope
295
291
// / for the given source location.
296
- TypeRefinementContext *findMostRefinedSubContext (SourceLoc Loc,
297
- ASTContext &Ctx);
292
+ AvailabilityScope *findMostRefinedSubContext (SourceLoc Loc, ASTContext &Ctx);
298
293
299
294
bool getNeedsExpansion () const { return LazyInfo.needsExpansion ; }
300
295
@@ -309,15 +304,14 @@ class TypeRefinementContext : public ASTAllocated<TypeRefinementContext> {
309
304
SWIFT_DEBUG_DUMPER (dump(SourceManager &SrcMgr));
310
305
void dump (raw_ostream &OS, SourceManager &SrcMgr) const ;
311
306
void print (raw_ostream &OS, SourceManager &SrcMgr, unsigned Indent = 0 ) const ;
312
-
307
+
313
308
static StringRef getReasonName (Reason R);
314
309
};
315
310
316
- void simple_display (llvm::raw_ostream &out,
317
- const TypeRefinementContext *trc);
311
+ void simple_display (llvm::raw_ostream &out, const AvailabilityScope *scope);
318
312
319
- inline SourceLoc extractNearestSourceLoc (const TypeRefinementContext *TRC ) {
320
- return TRC ->getIntroductionLoc ();
313
+ inline SourceLoc extractNearestSourceLoc (const AvailabilityScope *scope ) {
314
+ return scope ->getIntroductionLoc ();
321
315
}
322
316
323
317
} // end namespace swift
0 commit comments