Skip to content

Commit 0730209

Browse files
committed
AST: Move AvailabilityContext into its own header and cpp file.
1 parent 4cb783f commit 0730209

7 files changed

+306
-251
lines changed

include/swift/AST/Availability.h

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -335,129 +335,6 @@ class AvailabilityRange {
335335
}
336336
};
337337

338-
/// An `AvailabilityContext` summarizes the availability constraints for a
339-
/// specific scope, such as within a declaration or at a particular source
340-
/// location in a function body. This context is sufficient to determine whether
341-
/// a declaration is available or not in that scope.
342-
class AvailabilityContext : public llvm::FoldingSetNode {
343-
/// Summarizes platform specific availability constraints.
344-
struct PlatformInfo {
345-
/// The introduction version.
346-
AvailabilityRange Range;
347-
348-
/// When `IsUnavailable` is true, this value stores the broadest platform
349-
/// kind for which the context is unavailable.
350-
PlatformKind UnavailablePlatform;
351-
352-
/// Whether or not the context is considered unavailable on the current
353-
/// platform.
354-
unsigned IsUnavailable : 1;
355-
356-
/// Whether or not the context is considered deprecated on the current
357-
/// platform.
358-
unsigned IsDeprecated : 1;
359-
360-
/// Sets `Range` to `other` if `other` is more restrictive. Returns true if
361-
/// any property changed as a result of adding this constraint.
362-
bool constrainRange(const AvailabilityRange &other) {
363-
if (!other.isContainedIn(Range))
364-
return false;
365-
366-
Range = other;
367-
return true;
368-
}
369-
370-
/// Sets `Range` to the platform introduction range of `decl` if that range
371-
/// is more restrictive. Returns true if
372-
/// any property changed as a result of adding this constraint.
373-
bool constrainRange(const Decl *decl);
374-
375-
/// Updates `UnavailablePlatform` and `IsUnavailable` to reflect the status
376-
/// of `decl` if its platform unavailability is more restrictive. Returns
377-
/// true if any property changed as a result of adding this constraint.
378-
bool constrainUnavailability(const Decl *decl);
379-
380-
/// If `decl` is deprecated, sets `IsDeprecated` to true. Returns true if
381-
/// any property changed as a result of adding this constraint.
382-
bool constrainDeprecated(const Decl *decl);
383-
384-
/// Returns true if `other` is as available or is more available.
385-
bool isContainedIn(const PlatformInfo &other) const;
386-
387-
void Profile(llvm::FoldingSetNodeID &ID) const {
388-
Range.getRawVersionRange().Profile(ID);
389-
ID.AddBoolean(IsUnavailable);
390-
ID.AddInteger(static_cast<uint8_t>(UnavailablePlatform));
391-
ID.AddBoolean(IsDeprecated);
392-
}
393-
};
394-
PlatformInfo PlatformAvailability;
395-
396-
AvailabilityContext(const PlatformInfo &platformInfo)
397-
: PlatformAvailability(platformInfo){};
398-
399-
static const AvailabilityContext *get(const PlatformInfo &platformInfo,
400-
ASTContext &ctx);
401-
402-
public:
403-
/// Retrieves the default `AvailabilityContext`, which is maximally available.
404-
/// The platform availability range will be set to the deployment target (or
405-
/// minimum inlining target when applicable).
406-
static const AvailabilityContext *getDefault(ASTContext &ctx);
407-
408-
/// Retrieves a uniqued `AvailabilityContext` with the given platform
409-
/// availability parameters.
410-
static const AvailabilityContext *
411-
get(const AvailabilityRange &platformAvailability,
412-
std::optional<PlatformKind> unavailablePlatform, bool deprecated,
413-
ASTContext &ctx) {
414-
PlatformInfo platformInfo{platformAvailability,
415-
unavailablePlatform.has_value()
416-
? *unavailablePlatform
417-
: PlatformKind::none,
418-
unavailablePlatform.has_value(), deprecated};
419-
return get(platformInfo, ctx);
420-
}
421-
422-
/// Returns the range of platform versions which may execute code in the
423-
/// availability context, starting at its introduction version.
424-
AvailabilityRange getPlatformRange() const {
425-
return PlatformAvailability.Range;
426-
}
427-
428-
/// When the context is unavailable on the current platform this returns the
429-
/// broadest `PlatformKind` for which the context is unavailable. Otherwise,
430-
/// returns `nullopt`.
431-
std::optional<PlatformKind> getUnavailablePlatformKind() const {
432-
if (PlatformAvailability.IsUnavailable)
433-
return PlatformAvailability.UnavailablePlatform;
434-
return std::nullopt;
435-
}
436-
437-
/// Returns true if this context is deprecated on the current platform.
438-
bool isDeprecated() const { return PlatformAvailability.IsDeprecated; }
439-
440-
/// Returns the unique context that is the result of constraining the current
441-
/// context's platform availability range with `platformRange`.
442-
const AvailabilityContext *
443-
constrainWithPlatformRange(const AvailabilityRange &platformRange,
444-
ASTContext &ctx) const;
445-
446-
/// Returns the unique context that is the result of constraining the current
447-
/// context both with the availability attributes of `decl` and with
448-
/// `platformRange`.
449-
const AvailabilityContext *constrainWithDeclAndPlatformRange(
450-
Decl *decl, const AvailabilityRange &platformRange) const;
451-
452-
/// Returns true if `other` is as available or is more available.
453-
bool isContainedIn(const AvailabilityContext *other) const;
454-
455-
void print(llvm::raw_ostream &os) const;
456-
SWIFT_DEBUG_DUMP;
457-
458-
void Profile(llvm::FoldingSetNodeID &ID) const;
459-
};
460-
461338
class AvailabilityInference {
462339
public:
463340
/// Returns the decl that should be considered the parent decl of the given
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
//===--- AvailabilityContext.h - Swift Availability Structures --*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines the AvailabilityContext data structure, which summarizes
14+
// availability constraints for a specific scope.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef SWIFT_AST_AVAILABILITY_CONTEXT_H
19+
#define SWIFT_AST_AVAILABILITY_CONTEXT_H
20+
21+
#include "swift/AST/Availability.h"
22+
#include "swift/AST/PlatformKind.h"
23+
#include "swift/Basic/LLVM.h"
24+
#include "llvm/ADT/FoldingSet.h"
25+
#include <optional>
26+
27+
namespace swift {
28+
class ASTContext;
29+
class AvailableAttr;
30+
class Decl;
31+
32+
/// An `AvailabilityContext` summarizes the availability constraints for a
33+
/// specific scope, such as within a declaration or at a particular source
34+
/// location in a function body. This context is sufficient to determine whether
35+
/// a declaration is available or not in that scope.
36+
class AvailabilityContext : public llvm::FoldingSetNode {
37+
/// Summarizes platform specific availability constraints.
38+
struct PlatformInfo {
39+
/// The introduction version.
40+
AvailabilityRange Range;
41+
42+
/// When `IsUnavailable` is true, this value stores the broadest platform
43+
/// kind for which the context is unavailable.
44+
PlatformKind UnavailablePlatform;
45+
46+
/// Whether or not the context is considered unavailable on the current
47+
/// platform.
48+
unsigned IsUnavailable : 1;
49+
50+
/// Whether or not the context is considered deprecated on the current
51+
/// platform.
52+
unsigned IsDeprecated : 1;
53+
54+
/// Sets `Range` to `other` if `other` is more restrictive. Returns true if
55+
/// any property changed as a result of adding this constraint.
56+
bool constrainRange(const AvailabilityRange &other) {
57+
if (!other.isContainedIn(Range))
58+
return false;
59+
60+
Range = other;
61+
return true;
62+
}
63+
64+
/// Sets `Range` to the platform introduction range of `decl` if that range
65+
/// is more restrictive. Returns true if
66+
/// any property changed as a result of adding this constraint.
67+
bool constrainRange(const Decl *decl);
68+
69+
/// Updates `UnavailablePlatform` and `IsUnavailable` to reflect the status
70+
/// of `decl` if its platform unavailability is more restrictive. Returns
71+
/// true if any property changed as a result of adding this constraint.
72+
bool constrainUnavailability(const Decl *decl);
73+
74+
/// If `decl` is deprecated, sets `IsDeprecated` to true. Returns true if
75+
/// any property changed as a result of adding this constraint.
76+
bool constrainDeprecated(const Decl *decl);
77+
78+
/// Returns true if `other` is as available or is more available.
79+
bool isContainedIn(const PlatformInfo &other) const;
80+
81+
void Profile(llvm::FoldingSetNodeID &ID) const {
82+
Range.getRawVersionRange().Profile(ID);
83+
ID.AddBoolean(IsUnavailable);
84+
ID.AddInteger(static_cast<uint8_t>(UnavailablePlatform));
85+
ID.AddBoolean(IsDeprecated);
86+
}
87+
};
88+
PlatformInfo PlatformAvailability;
89+
90+
AvailabilityContext(const PlatformInfo &platformInfo)
91+
: PlatformAvailability(platformInfo){};
92+
93+
static const AvailabilityContext *get(const PlatformInfo &platformInfo,
94+
ASTContext &ctx);
95+
96+
public:
97+
/// Retrieves the default `AvailabilityContext`, which is maximally available.
98+
/// The platform availability range will be set to the deployment target (or
99+
/// minimum inlining target when applicable).
100+
static const AvailabilityContext *getDefault(ASTContext &ctx);
101+
102+
/// Retrieves a uniqued `AvailabilityContext` with the given platform
103+
/// availability parameters.
104+
static const AvailabilityContext *
105+
get(const AvailabilityRange &platformAvailability,
106+
std::optional<PlatformKind> unavailablePlatform, bool deprecated,
107+
ASTContext &ctx) {
108+
PlatformInfo platformInfo{platformAvailability,
109+
unavailablePlatform.has_value()
110+
? *unavailablePlatform
111+
: PlatformKind::none,
112+
unavailablePlatform.has_value(), deprecated};
113+
return get(platformInfo, ctx);
114+
}
115+
116+
/// Returns the range of platform versions which may execute code in the
117+
/// availability context, starting at its introduction version.
118+
AvailabilityRange getPlatformRange() const {
119+
return PlatformAvailability.Range;
120+
}
121+
122+
/// When the context is unavailable on the current platform this returns the
123+
/// broadest `PlatformKind` for which the context is unavailable. Otherwise,
124+
/// returns `nullopt`.
125+
std::optional<PlatformKind> getUnavailablePlatformKind() const {
126+
if (PlatformAvailability.IsUnavailable)
127+
return PlatformAvailability.UnavailablePlatform;
128+
return std::nullopt;
129+
}
130+
131+
/// Returns true if this context is deprecated on the current platform.
132+
bool isDeprecated() const { return PlatformAvailability.IsDeprecated; }
133+
134+
/// Returns the unique context that is the result of constraining the current
135+
/// context's platform availability range with `platformRange`.
136+
const AvailabilityContext *
137+
constrainWithPlatformRange(const AvailabilityRange &platformRange,
138+
ASTContext &ctx) const;
139+
140+
/// Returns the unique context that is the result of constraining the current
141+
/// context both with the availability attributes of `decl` and with
142+
/// `platformRange`.
143+
const AvailabilityContext *constrainWithDeclAndPlatformRange(
144+
Decl *decl, const AvailabilityRange &platformRange) const;
145+
146+
/// Returns true if `other` is as available or is more available.
147+
bool isContainedIn(const AvailabilityContext *other) const;
148+
149+
void print(llvm::raw_ostream &os) const;
150+
SWIFT_DEBUG_DUMP;
151+
152+
void Profile(llvm::FoldingSetNodeID &ID) const;
153+
};
154+
155+
} // end namespace swift
156+
157+
#endif

include/swift/AST/TypeRefinementContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define SWIFT_TYPEREFINEMENTCONTEXT_H
2020

2121
#include "swift/AST/Availability.h"
22+
#include "swift/AST/AvailabilityContext.h"
2223
#include "swift/AST/Identifier.h"
2324
#include "swift/AST/Stmt.h" // for PoundAvailableInfo
2425
#include "swift/Basic/Debug.h"

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "ForeignRepresentationInfo.h"
2020
#include "SubstitutionMapStorage.h"
2121
#include "swift/ABI/MetadataValues.h"
22+
#include "swift/AST/AvailabilityContext.h"
2223
#include "swift/AST/ClangModuleLoader.h"
2324
#include "swift/AST/ConcreteDeclRef.h"
2425
#include "swift/AST/ConformanceLookup.h"

0 commit comments

Comments
 (0)