21
21
#include " swift/AST/Availability.h"
22
22
#include " swift/AST/PlatformKind.h"
23
23
#include " swift/Basic/LLVM.h"
24
- #include " llvm/ADT/FoldingSet.h"
25
24
#include < optional>
26
25
27
26
namespace swift {
@@ -33,123 +32,70 @@ class Decl;
33
32
// / specific scope, such as within a declaration or at a particular source
34
33
// / location in a function body. This context is sufficient to determine whether
35
34
// / 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);
35
+ class AvailabilityContext {
36
+ public:
37
+ class Storage ;
38
+
39
+ private:
40
+ struct PlatformInfo ;
41
+
42
+ // / A non-null pointer to uniqued storage for this availability context.
43
+ const Storage *Info;
44
+
45
+ AvailabilityContext (const Storage *info) : Info(info) { assert (info); };
95
46
96
47
public:
48
+ AvailabilityContext (const AvailabilityContext &other) : Info(other.Info){};
49
+
97
50
// / Retrieves the default `AvailabilityContext`, which is maximally available.
98
51
// / The platform availability range will be set to the deployment target (or
99
52
// / minimum inlining target when applicable).
100
- static const AvailabilityContext * getDefault (ASTContext &ctx);
53
+ static AvailabilityContext getDefault (ASTContext &ctx);
101
54
102
55
// / Retrieves a uniqued `AvailabilityContext` with the given platform
103
56
// / availability parameters.
104
- static const AvailabilityContext *
57
+ static AvailabilityContext
105
58
get (const AvailabilityRange &platformAvailability,
106
59
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
- }
60
+ ASTContext &ctx);
115
61
116
62
// / Returns the range of platform versions which may execute code in the
117
63
// / availability context, starting at its introduction version.
118
- AvailabilityRange getPlatformRange () const {
119
- return PlatformAvailability.Range ;
120
- }
64
+ AvailabilityRange getPlatformRange () const ;
121
65
122
66
// / When the context is unavailable on the current platform this returns the
123
67
// / broadest `PlatformKind` for which the context is unavailable. Otherwise,
124
68
// / returns `nullopt`.
125
- std::optional<PlatformKind> getUnavailablePlatformKind () const {
126
- if (PlatformAvailability.IsUnavailable )
127
- return PlatformAvailability.UnavailablePlatform ;
128
- return std::nullopt;
129
- }
69
+ std::optional<PlatformKind> getUnavailablePlatformKind () const ;
130
70
131
71
// / Returns true if this context is deprecated on the current platform.
132
- bool isDeprecated () const { return PlatformAvailability. IsDeprecated ; }
72
+ bool isDeprecated () const ;
133
73
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 ;
74
+ // / Constrain the platform availability range with `platformRange`.
75
+ void constrainWithPlatformRange (const AvailabilityRange &platformRange,
76
+ ASTContext &ctx);
139
77
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 ;
78
+ // / Constrain the platform availability range with both the availability
79
+ // / attributes of `decl` and with `platformRange`.
80
+ void
81
+ constrainWithDeclAndPlatformRange (Decl *decl,
82
+ const AvailabilityRange &platformRange);
145
83
146
84
// / Returns true if `other` is as available or is more available.
147
- bool isContainedIn (const AvailabilityContext *other) const ;
85
+ bool isContainedIn (const AvailabilityContext other) const ;
86
+
87
+ friend bool operator ==(const AvailabilityContext &lhs,
88
+ const AvailabilityContext &rhs) {
89
+ return lhs.Info == rhs.Info ;
90
+ }
91
+
92
+ friend bool operator !=(const AvailabilityContext &lhs,
93
+ const AvailabilityContext &rhs) {
94
+ return lhs.Info != rhs.Info ;
95
+ }
148
96
149
97
void print (llvm::raw_ostream &os) const ;
150
98
SWIFT_DEBUG_DUMP;
151
-
152
- void Profile (llvm::FoldingSetNodeID &ID) const ;
153
99
};
154
100
155
101
} // end namespace swift
0 commit comments