|
17 | 17 | #ifndef SWIFT_AST_AVAILABILITY_H
|
18 | 18 | #define SWIFT_AST_AVAILABILITY_H
|
19 | 19 |
|
| 20 | +#include "swift/AST/PlatformKind.h" |
20 | 21 | #include "swift/AST/Type.h"
|
21 | 22 | #include "swift/Basic/LLVM.h"
|
| 23 | +#include "llvm/ADT/FoldingSet.h" |
22 | 24 | #include "llvm/Support/VersionTuple.h"
|
23 | 25 | #include <optional>
|
24 | 26 |
|
@@ -174,6 +176,8 @@ class VersionRange {
|
174 | 176 | return VersionRange(EndPoint);
|
175 | 177 | }
|
176 | 178 |
|
| 179 | + void Profile(llvm::FoldingSetNodeID &ID) const; |
| 180 | + |
177 | 181 | private:
|
178 | 182 | VersionRange(const llvm::VersionTuple &LowerEndpoint) {
|
179 | 183 | setLowerEndpoint(LowerEndpoint);
|
@@ -331,6 +335,80 @@ class AvailabilityRange {
|
331 | 335 | }
|
332 | 336 | };
|
333 | 337 |
|
| 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 | + /// Sets `Range` to `other` if `other` is more restrictive. Returns true if |
| 349 | + /// any property changed as a result of adding this constraint. |
| 350 | + bool constrainRange(const AvailabilityRange &other) { |
| 351 | + if (!other.isContainedIn(Range)) |
| 352 | + return false; |
| 353 | + |
| 354 | + Range = other; |
| 355 | + return true; |
| 356 | + } |
| 357 | + |
| 358 | + /// Sets `Range` to the platform introduction range of `decl` if that range |
| 359 | + /// is more restrictive. Returns true if |
| 360 | + /// any property changed as a result of adding this constraint. |
| 361 | + bool constrainRange(const Decl *decl); |
| 362 | + |
| 363 | + void Profile(llvm::FoldingSetNodeID &ID) const { |
| 364 | + Range.getRawVersionRange().Profile(ID); |
| 365 | + } |
| 366 | + }; |
| 367 | + PlatformInfo PlatformAvailability; |
| 368 | + |
| 369 | + AvailabilityContext(const PlatformInfo &platformInfo) |
| 370 | + : PlatformAvailability(platformInfo){}; |
| 371 | + |
| 372 | + static const AvailabilityContext *get(const PlatformInfo &platformInfo, |
| 373 | + ASTContext &ctx); |
| 374 | + |
| 375 | +public: |
| 376 | + /// Retrieves the default `AvailabilityContext`, which is maximally available. |
| 377 | + /// The platform availability range will be set to the deployment target (or |
| 378 | + /// minimum inlining target when applicable). |
| 379 | + static const AvailabilityContext *getDefault(ASTContext &ctx); |
| 380 | + |
| 381 | + /// Retrieves a uniqued `AvailabilityContext` with the given platform |
| 382 | + /// availability parameters. |
| 383 | + static const AvailabilityContext * |
| 384 | + get(const AvailabilityRange &platformAvailability, ASTContext &ctx) { |
| 385 | + PlatformInfo platformInfo{ |
| 386 | + .Range = platformAvailability, |
| 387 | + }; |
| 388 | + return get(platformInfo, ctx); |
| 389 | + } |
| 390 | + |
| 391 | + /// Returns the range of platform versions which may execute code in the |
| 392 | + /// availability context, starting at its introduction version. |
| 393 | + AvailabilityRange getPlatformRange() const { |
| 394 | + return PlatformAvailability.Range; |
| 395 | + } |
| 396 | + |
| 397 | + /// Returns the unique context that is the result of constraining the current |
| 398 | + /// context's platform availability range with `platformRange`. |
| 399 | + const AvailabilityContext * |
| 400 | + constrainWithPlatformRange(const AvailabilityRange &platformRange, |
| 401 | + ASTContext &ctx) const; |
| 402 | + |
| 403 | + /// Returns the unique context that is the result of constraining the current |
| 404 | + /// context both with the availability attributes of `decl` and with |
| 405 | + /// `platformRange`. |
| 406 | + const AvailabilityContext *constrainWithDeclAndPlatformRange( |
| 407 | + Decl *decl, const AvailabilityRange &platformRange) const; |
| 408 | + |
| 409 | + void Profile(llvm::FoldingSetNodeID &ID) const; |
| 410 | +}; |
| 411 | + |
334 | 412 | class AvailabilityInference {
|
335 | 413 | public:
|
336 | 414 | /// Returns the decl that should be considered the parent decl of the given
|
|
0 commit comments