|
| 1 | +//===--- AvailabilityConstraint.h - Swift Availability Constraints ------*-===// |
| 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 AvailabilityConstraint class. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#ifndef SWIFT_AST_AVAILABILITY_CONSTRAINT_H |
| 18 | +#define SWIFT_AST_AVAILABILITY_CONSTRAINT_H |
| 19 | + |
| 20 | +#include "swift/AST/Availability.h" |
| 21 | +#include "swift/AST/PlatformKind.h" |
| 22 | +#include "swift/Basic/LLVM.h" |
| 23 | + |
| 24 | +namespace swift { |
| 25 | + |
| 26 | +class ASTContext; |
| 27 | +class AvailableAttr; |
| 28 | + |
| 29 | +/// Represents the reason a declaration could be considered unavailable in a |
| 30 | +/// certain context. |
| 31 | +class AvailabilityConstraint { |
| 32 | +public: |
| 33 | + enum class Kind { |
| 34 | + /// The declaration is referenced in a context in which it is generally |
| 35 | + /// unavailable. For example, a reference to a declaration that is |
| 36 | + /// unavailable on macOS from a context that may execute on macOS has this |
| 37 | + /// constraint. |
| 38 | + AlwaysUnavailable, |
| 39 | + |
| 40 | + /// The declaration is referenced in a context in which it is considered |
| 41 | + /// obsolete. For example, a reference to a declaration that is obsolete in |
| 42 | + /// macOS 13 from a context that may execute on macOS 13 or later has this |
| 43 | + /// constraint. |
| 44 | + Obsoleted, |
| 45 | + |
| 46 | + /// The declaration is only available in a different version. For example, |
| 47 | + /// the declaration might only be introduced in the Swift 6 language mode |
| 48 | + /// while the module is being compiled in the Swift 5 language mode. |
| 49 | + RequiresVersion, |
| 50 | + |
| 51 | + /// The declaration is referenced in a context that does not have an |
| 52 | + /// adequate minimum version constraint. For example, a reference to a |
| 53 | + /// declaration that is introduced in macOS 13 from a context that may |
| 54 | + /// execute on earlier versions of macOS has this constraint. This |
| 55 | + /// kind of constraint can be satisfied by tightening the minimum |
| 56 | + /// version of the context with `if #available(...)` or by adding or |
| 57 | + /// adjusting an `@available` attribute. |
| 58 | + IntroducedInNewerVersion, |
| 59 | + }; |
| 60 | + |
| 61 | +private: |
| 62 | + Kind kind; |
| 63 | + const AvailableAttr *attr; |
| 64 | + |
| 65 | + AvailabilityConstraint(Kind kind, const AvailableAttr *attr) |
| 66 | + : kind(kind), attr(attr) {}; |
| 67 | + |
| 68 | +public: |
| 69 | + static AvailabilityConstraint |
| 70 | + forAlwaysUnavailable(const AvailableAttr *attr) { |
| 71 | + return AvailabilityConstraint(Kind::AlwaysUnavailable, attr); |
| 72 | + } |
| 73 | + |
| 74 | + static AvailabilityConstraint forObsoleted(const AvailableAttr *attr) { |
| 75 | + return AvailabilityConstraint(Kind::Obsoleted, attr); |
| 76 | + } |
| 77 | + |
| 78 | + static AvailabilityConstraint forRequiresVersion(const AvailableAttr *attr) { |
| 79 | + return AvailabilityConstraint(Kind::RequiresVersion, attr); |
| 80 | + } |
| 81 | + |
| 82 | + static AvailabilityConstraint |
| 83 | + forIntroducedInNewerVersion(const AvailableAttr *attr) { |
| 84 | + return AvailabilityConstraint(Kind::IntroducedInNewerVersion, attr); |
| 85 | + } |
| 86 | + |
| 87 | + Kind getKind() const { return kind; } |
| 88 | + const AvailableAttr *getAttr() const { return attr; } |
| 89 | + |
| 90 | + /// Returns the required range for `IntroducedInNewerVersion` requirements, or |
| 91 | + /// `std::nullopt` otherwise. |
| 92 | + std::optional<AvailabilityRange> |
| 93 | + getRequiredNewerAvailabilityRange(ASTContext &ctx) const; |
| 94 | + |
| 95 | + /// Returns true if this unmet requirement can be satisfied by introducing an |
| 96 | + /// `if #available(...)` condition in source. |
| 97 | + bool isConditionallySatisfiable() const; |
| 98 | +}; |
| 99 | + |
| 100 | +} // end namespace swift |
| 101 | + |
| 102 | +#endif |
0 commit comments