18
18
#ifndef SWIFT_AST_AVAILABILITY_DOMAIN_H
19
19
#define SWIFT_AST_AVAILABILITY_DOMAIN_H
20
20
21
+ #include " swift/AST/ASTAllocated.h"
22
+ #include " swift/AST/Identifier.h"
21
23
#include " swift/AST/PlatformKind.h"
22
24
#include " swift/Basic/LLVM.h"
23
25
#include " llvm/ADT/FoldingSet.h"
26
28
27
29
namespace swift {
28
30
class ASTContext ;
31
+ class CustomAvailabilityDomain ;
29
32
class DeclContext ;
33
+ class ModuleDecl ;
30
34
31
35
// / Represents a dimension of availability (e.g. macOS platform or Swift
32
36
// / language mode).
@@ -48,6 +52,10 @@ class AvailabilityDomain final {
48
52
49
53
// / Represents availability for a specific operating system platform.
50
54
Platform,
55
+
56
+ // / Represents availability for an arbitrary domain that is defined at
57
+ // / compile time by a module.
58
+ Custom,
51
59
};
52
60
53
61
private:
@@ -86,13 +94,10 @@ class AvailabilityDomain final {
86
94
PlatformKind getPlatform () { return platform; }
87
95
};
88
96
89
- // / This will eventually be a class storing information about externally
90
- // / defined availability domains.
91
- using ExternalDomain = void ;
92
-
93
97
using InlineDomainPtr =
94
98
llvm::PointerEmbeddedInt<uint32_t , InlineDomain::ReprBits>;
95
- using Storage = llvm::PointerUnion<ExternalDomain *, InlineDomainPtr>;
99
+ using Storage =
100
+ llvm::PointerUnion<CustomAvailabilityDomain *, InlineDomainPtr>;
96
101
Storage storage;
97
102
98
103
AvailabilityDomain (Kind kind)
@@ -103,6 +108,8 @@ class AvailabilityDomain final {
103
108
AvailabilityDomain (PlatformKind platform)
104
109
: storage(InlineDomain(Kind::Platform, platform).asInteger()) {};
105
110
111
+ AvailabilityDomain (CustomAvailabilityDomain *domain) : storage(domain) {};
112
+
106
113
AvailabilityDomain (Storage storage) : storage(storage) {};
107
114
108
115
static AvailabilityDomain fromOpaque (void *opaque) {
@@ -118,6 +125,11 @@ class AvailabilityDomain final {
118
125
: std::nullopt;
119
126
}
120
127
128
+ CustomAvailabilityDomain *getCustomDomain () const {
129
+ assert (isCustom ());
130
+ return storage.get <CustomAvailabilityDomain *>();
131
+ }
132
+
121
133
public:
122
134
AvailabilityDomain () {}
123
135
@@ -141,6 +153,10 @@ class AvailabilityDomain final {
141
153
return AvailabilityDomain (Kind::Embedded);
142
154
}
143
155
156
+ static AvailabilityDomain forCustom (CustomAvailabilityDomain *domain) {
157
+ return AvailabilityDomain (domain);
158
+ }
159
+
144
160
// / Returns the built-in availability domain identified by the given string.
145
161
static std::optional<AvailabilityDomain>
146
162
builtinDomainForString (StringRef string, const DeclContext *declContext);
@@ -149,7 +165,7 @@ class AvailabilityDomain final {
149
165
if (auto inlineDomain = getInlineDomain ())
150
166
return inlineDomain->getKind ();
151
167
152
- llvm_unreachable ( " unimplemented " ) ;
168
+ return Kind::Custom ;
153
169
}
154
170
155
171
bool isUniversal () const { return getKind () == Kind::Universal; }
@@ -164,6 +180,8 @@ class AvailabilityDomain final {
164
180
165
181
bool isEmbedded () const { return getKind () == Kind::Embedded; }
166
182
183
+ bool isCustom () const { return getKind () == Kind::Custom; }
184
+
167
185
// / Returns the platform kind for this domain if applicable.
168
186
PlatformKind getPlatformKind () const {
169
187
if (auto inlineDomain = getInlineDomain ())
@@ -183,6 +201,9 @@ class AvailabilityDomain final {
183
201
// / Returns the string to use when printing an `@available` attribute.
184
202
llvm::StringRef getNameForAttributePrinting () const ;
185
203
204
+ // / Returns the module that the domain belongs to, if it is a custom domain.
205
+ ModuleDecl *getModule () const ;
206
+
186
207
// / Returns true if availability in `other` is a subset of availability in
187
208
// / this domain. The set of all availability domains form a lattice where the
188
209
// / universal domain (`*`) is the bottom element.
@@ -201,6 +222,34 @@ class AvailabilityDomain final {
201
222
}
202
223
};
203
224
225
+ // / Represents an availability domain that has been defined in a module.
226
+ class CustomAvailabilityDomain : public ASTAllocated <CustomAvailabilityDomain> {
227
+ public:
228
+ enum class Kind {
229
+ // / A domain that is known to be enabled at compile time.
230
+ Enabled,
231
+ // / A domain that is known to be disabled at compile time.
232
+ Disabled,
233
+ // / A domain with an enablement state that must be queried at runtime.
234
+ Dynamic,
235
+ };
236
+
237
+ private:
238
+ Identifier name;
239
+ Kind kind;
240
+ ModuleDecl *mod;
241
+
242
+ CustomAvailabilityDomain (Identifier name, ModuleDecl *mod, Kind kind);
243
+
244
+ public:
245
+ static CustomAvailabilityDomain *create (const ASTContext &ctx, StringRef name,
246
+ ModuleDecl *mod, Kind kind);
247
+
248
+ Identifier getName () const { return name; }
249
+ Kind getKind () const { return kind; }
250
+ ModuleDecl *getModule () const { return mod; }
251
+ };
252
+
204
253
} // end namespace swift
205
254
206
255
namespace llvm {
0 commit comments