Skip to content

Commit 1e6effd

Browse files
authored
Merge pull request #77563 from tshortli/rename-type-refinement-context
AST: Rename TypeRefinementContext to AvailabilityScope
2 parents ec086e1 + 67c5927 commit 1e6effd

27 files changed

+475
-492
lines changed

include/swift/AST/TypeRefinementContext.h renamed to include/swift/AST/AvailabilityScope.h

Lines changed: 84 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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
//
@@ -10,13 +10,14 @@
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"
@@ -30,29 +31,24 @@
3031
#include "llvm/Support/ErrorHandling.h"
3132

3233
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> {
5147

5248
public:
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

10096
private:
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

190186
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,
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

include/swift/AST/Module.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ namespace clang {
5151
namespace swift {
5252
enum class ArtificialMainKind : uint8_t;
5353
class ASTContext;
54+
class ASTScope;
5455
class ASTWalker;
56+
class AvailabilityScope;
5557
class BraceStmt;
5658
class Decl;
5759
class DeclAttribute;
@@ -74,15 +76,13 @@ namespace swift {
7476
class ProtocolConformance;
7577
class ProtocolDecl;
7678
struct PrintOptions;
79+
class SourceLookupCache;
7780
class Token;
7881
class TupleType;
7982
class Type;
80-
class TypeRefinementContext;
8183
class ValueDecl;
8284
class VarDecl;
8385
class VisibleDeclConsumer;
84-
class ASTScope;
85-
class SourceLookupCache;
8686

8787
namespace ast_scope {
8888
class ASTSourceFileScope;

include/swift/AST/SourceFile.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ class SourceFile final : public FileUnit {
137137
/// same module.
138138
mutable Identifier PrivateDiscriminator;
139139

140-
/// The root TypeRefinementContext for this SourceFile.
140+
/// The root AvailabilityScope for this SourceFile.
141141
///
142142
/// This is set during type checking.
143-
TypeRefinementContext *TRC = nullptr;
143+
AvailabilityScope *RootAvailabilityScope = nullptr;
144144

145145
/// Either the class marked \@NS/UIApplicationMain or the synthesized FuncDecl
146146
/// that calls main on the type marked @main.
@@ -742,14 +742,14 @@ class SourceFile final : public FileUnit {
742742
return ImportedUnderlyingModule->findUnderlyingClangModule();
743743
}
744744

745-
/// Get the root refinement context for the file. The root context may be
746-
/// null if the context hierarchy has not been built yet. Use
747-
/// TypeChecker::getOrBuildTypeRefinementContext() to get a built
748-
/// root of the hierarchy.
749-
TypeRefinementContext *getTypeRefinementContext() const;
745+
/// Get the root availability scope for the file. The root scope may be
746+
/// null if the scope tree has not been built yet. Use
747+
/// TypeChecker::getOrBuildAvailabilityScope() to get a built
748+
/// root of the tree.
749+
AvailabilityScope *getAvailabilityScope() const;
750750

751-
/// Set the root refinement context for the file.
752-
void setTypeRefinementContext(TypeRefinementContext *TRC);
751+
/// Set the root availability scope for the file.
752+
void setAvailabilityScope(AvailabilityScope *scope);
753753

754754
/// Whether this file can compute an interface hash.
755755
bool hasInterfaceHash() const {

include/swift/AST/TypeCheckRequests.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ namespace swift {
4444
class AbstractStorageDecl;
4545
class AccessorDecl;
4646
enum class AccessorKind;
47+
class AvailabilityScope;
4748
class BreakStmt;
4849
class ContextualPattern;
4950
class ContinueStmt;
@@ -68,7 +69,6 @@ class TypeAliasDecl;
6869
class TypeLoc;
6970
class Witness;
7071
class TypeResolution;
71-
class TypeRefinementContext;
7272
struct TypeWitnessAndDecl;
7373
class ValueDecl;
7474
enum class OpaqueReadOwnership: uint8_t;
@@ -4869,10 +4869,10 @@ class InitAccessorReferencedVariablesRequest
48694869
bool isCached() const { return true; }
48704870
};
48714871

4872-
/// Expand the children of the given type refinement context.
4873-
class ExpandChildTypeRefinementContextsRequest
4874-
: public SimpleRequest<ExpandChildTypeRefinementContextsRequest,
4875-
evaluator::SideEffect(TypeRefinementContext *),
4872+
/// Expand the children of the given type availability scope.
4873+
class ExpandChildAvailabilityScopesRequest
4874+
: public SimpleRequest<ExpandChildAvailabilityScopesRequest,
4875+
evaluator::SideEffect(AvailabilityScope *),
48764876
RequestFlags::SeparatelyCached> {
48774877
public:
48784878
using SimpleRequest::SimpleRequest;
@@ -4881,7 +4881,7 @@ class ExpandChildTypeRefinementContextsRequest
48814881
friend SimpleRequest;
48824882

48834883
evaluator::SideEffect evaluate(Evaluator &evaluator,
4884-
TypeRefinementContext *parentTRC) const;
4884+
AvailabilityScope *parentScope) const;
48854885

48864886
public:
48874887
// Separate caching.

0 commit comments

Comments
 (0)