2121#include " swift/AST/Availability.h"
2222#include " swift/AST/PlatformKind.h"
2323#include " swift/Basic/LLVM.h"
24- #include " llvm/ADT/FoldingSet.h"
2524#include < optional>
2625
2726namespace swift {
@@ -33,123 +32,70 @@ class Decl;
3332// / specific scope, such as within a declaration or at a particular source
3433// / location in a function body. This context is sufficient to determine whether
3534// / 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); };
9546
9647public:
48+ AvailabilityContext (const AvailabilityContext &other) : Info(other.Info){};
49+
9750 // / Retrieves the default `AvailabilityContext`, which is maximally available.
9851 // / The platform availability range will be set to the deployment target (or
9952 // / minimum inlining target when applicable).
100- static const AvailabilityContext * getDefault (ASTContext &ctx);
53+ static AvailabilityContext getDefault (ASTContext &ctx);
10154
10255 // / Retrieves a uniqued `AvailabilityContext` with the given platform
10356 // / availability parameters.
104- static const AvailabilityContext *
57+ static AvailabilityContext
10558 get (const AvailabilityRange &platformAvailability,
10659 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);
11561
11662 // / Returns the range of platform versions which may execute code in the
11763 // / availability context, starting at its introduction version.
118- AvailabilityRange getPlatformRange () const {
119- return PlatformAvailability.Range ;
120- }
64+ AvailabilityRange getPlatformRange () const ;
12165
12266 // / When the context is unavailable on the current platform this returns the
12367 // / broadest `PlatformKind` for which the context is unavailable. Otherwise,
12468 // / 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 ;
13070
13171 // / Returns true if this context is deprecated on the current platform.
132- bool isDeprecated () const { return PlatformAvailability. IsDeprecated ; }
72+ bool isDeprecated () const ;
13373
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);
13977
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);
14583
14684 // / 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+ }
14896
14997 void print (llvm::raw_ostream &os) const ;
15098 SWIFT_DEBUG_DUMP;
151-
152- void Profile (llvm::FoldingSetNodeID &ID) const ;
15399};
154100
155101} // end namespace swift
0 commit comments