|
| 1 | +//===--- AvailabilityConstraint.cpp - Swift Availability Constraints ------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 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 | +#include "swift/AST/AvailabilityConstraint.h" |
| 14 | +#include "swift/AST/ASTContext.h" |
| 15 | +#include "swift/AST/AvailabilityContext.h" |
| 16 | +#include "swift/AST/AvailabilityInference.h" |
| 17 | +#include "swift/AST/Decl.h" |
| 18 | + |
| 19 | +using namespace swift; |
| 20 | + |
| 21 | +PlatformKind AvailabilityConstraint::getPlatform() const { |
| 22 | + return getAttr().getPlatform(); |
| 23 | +} |
| 24 | + |
| 25 | +std::optional<AvailabilityRange> |
| 26 | +AvailabilityConstraint::getRequiredNewerAvailabilityRange( |
| 27 | + ASTContext &ctx) const { |
| 28 | + switch (getKind()) { |
| 29 | + case Kind::AlwaysUnavailable: |
| 30 | + case Kind::RequiresVersion: |
| 31 | + case Kind::Obsoleted: |
| 32 | + return std::nullopt; |
| 33 | + case Kind::IntroducedInNewerVersion: |
| 34 | + return getAttr().getIntroducedRange(ctx); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +bool AvailabilityConstraint::isConditionallySatisfiable() const { |
| 39 | + switch (getKind()) { |
| 40 | + case Kind::AlwaysUnavailable: |
| 41 | + case Kind::RequiresVersion: |
| 42 | + case Kind::Obsoleted: |
| 43 | + return false; |
| 44 | + case Kind::IntroducedInNewerVersion: |
| 45 | + return true; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +bool AvailabilityConstraint::isActiveForRuntimeQueries(ASTContext &ctx) const { |
| 50 | + if (getAttr().getPlatform() == PlatformKind::none) |
| 51 | + return true; |
| 52 | + |
| 53 | + return swift::isPlatformActive(getAttr().getPlatform(), ctx.LangOpts, |
| 54 | + /*forTargetVariant=*/false, |
| 55 | + /*forRuntimeQuery=*/true); |
| 56 | +} |
| 57 | + |
| 58 | +static bool |
| 59 | +isInsideCompatibleUnavailableDeclaration(const Decl *decl, |
| 60 | + const SemanticAvailableAttr &attr, |
| 61 | + const AvailabilityContext &context) { |
| 62 | + if (!context.isUnavailable()) |
| 63 | + return false; |
| 64 | + |
| 65 | + if (!attr.isUnconditionallyUnavailable()) |
| 66 | + return false; |
| 67 | + |
| 68 | + // Refuse calling universally unavailable functions from unavailable code, |
| 69 | + // but allow the use of types. |
| 70 | + auto domain = attr.getDomain(); |
| 71 | + if (!isa<TypeDecl>(decl) && !isa<ExtensionDecl>(decl)) { |
| 72 | + if (domain.isUniversal() || domain.isSwiftLanguage()) |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + return context.containsUnavailableDomain(domain); |
| 77 | +} |
| 78 | + |
| 79 | +std::optional<AvailabilityConstraint> |
| 80 | +swift::getAvailabilityConstraintForAttr(const Decl *decl, |
| 81 | + const SemanticAvailableAttr &attr, |
| 82 | + const AvailabilityContext &context) { |
| 83 | + if (isInsideCompatibleUnavailableDeclaration(decl, attr, context)) |
| 84 | + return std::nullopt; |
| 85 | + |
| 86 | + if (attr.isUnconditionallyUnavailable()) |
| 87 | + return AvailabilityConstraint::forAlwaysUnavailable(attr); |
| 88 | + |
| 89 | + auto &ctx = decl->getASTContext(); |
| 90 | + auto deploymentVersion = attr.getActiveVersion(ctx); |
| 91 | + auto deploymentRange = |
| 92 | + AvailabilityRange(VersionRange::allGTE(deploymentVersion)); |
| 93 | + std::optional<llvm::VersionTuple> obsoletedVersion = attr.getObsoleted(); |
| 94 | + |
| 95 | + { |
| 96 | + StringRef obsoletedPlatform; |
| 97 | + llvm::VersionTuple remappedObsoletedVersion; |
| 98 | + if (AvailabilityInference::updateObsoletedPlatformForFallback( |
| 99 | + attr, ctx, obsoletedPlatform, remappedObsoletedVersion)) |
| 100 | + obsoletedVersion = remappedObsoletedVersion; |
| 101 | + } |
| 102 | + |
| 103 | + if (obsoletedVersion && *obsoletedVersion <= deploymentVersion) |
| 104 | + return AvailabilityConstraint::forObsoleted(attr); |
| 105 | + |
| 106 | + AvailabilityRange introducedRange = attr.getIntroducedRange(ctx); |
| 107 | + |
| 108 | + // FIXME: [availability] Expand this to cover custom versioned domains |
| 109 | + if (attr.isPlatformSpecific()) { |
| 110 | + if (!context.getPlatformRange().isContainedIn(introducedRange)) |
| 111 | + return AvailabilityConstraint::forIntroducedInNewerVersion(attr); |
| 112 | + } else if (!deploymentRange.isContainedIn(introducedRange)) { |
| 113 | + return AvailabilityConstraint::forRequiresVersion(attr); |
| 114 | + } |
| 115 | + |
| 116 | + return std::nullopt; |
| 117 | +} |
| 118 | + |
| 119 | +/// Returns the most specific platform domain from the availability attributes |
| 120 | +/// attached to \p decl or `std::nullopt` if there are none. Platform specific |
| 121 | +/// `@available` attributes for other platforms should be ignored. For example, |
| 122 | +/// if a declaration has attributes for both iOS and macCatalyst, only the |
| 123 | +/// macCatalyst attributes take effect when compiling for a macCatalyst target. |
| 124 | +static std::optional<AvailabilityDomain> |
| 125 | +activePlatformDomainForDecl(const Decl *decl) { |
| 126 | + std::optional<AvailabilityDomain> activeDomain; |
| 127 | + for (auto attr : |
| 128 | + decl->getSemanticAvailableAttrs(/*includingInactive=*/false)) { |
| 129 | + auto domain = attr.getDomain(); |
| 130 | + if (!domain.isPlatform()) |
| 131 | + continue; |
| 132 | + |
| 133 | + if (activeDomain && domain.contains(*activeDomain)) |
| 134 | + continue; |
| 135 | + |
| 136 | + activeDomain.emplace(domain); |
| 137 | + } |
| 138 | + |
| 139 | + return activeDomain; |
| 140 | +} |
| 141 | + |
| 142 | +static void |
| 143 | +getAvailabilityConstraintsForDecl(DeclAvailabilityConstraints &constraints, |
| 144 | + const Decl *decl, |
| 145 | + const AvailabilityContext &context) { |
| 146 | + auto activePlatformDomain = activePlatformDomainForDecl(decl); |
| 147 | + |
| 148 | + for (auto attr : |
| 149 | + decl->getSemanticAvailableAttrs(/*includingInactive=*/false)) { |
| 150 | + auto domain = attr.getDomain(); |
| 151 | + if (domain.isPlatform() && activePlatformDomain && |
| 152 | + !activePlatformDomain->contains(domain)) |
| 153 | + continue; |
| 154 | + |
| 155 | + if (auto constraint = |
| 156 | + swift::getAvailabilityConstraintForAttr(decl, attr, context)) |
| 157 | + constraints.addConstraint(*constraint); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +DeclAvailabilityConstraints |
| 162 | +swift::getAvailabilityConstraintsForDecl(const Decl *decl, |
| 163 | + const AvailabilityContext &context) { |
| 164 | + DeclAvailabilityConstraints constraints; |
| 165 | + |
| 166 | + // Generic parameters are always available. |
| 167 | + if (isa<GenericTypeParamDecl>(decl)) |
| 168 | + return constraints; |
| 169 | + |
| 170 | + decl = abstractSyntaxDeclForAvailableAttribute(decl); |
| 171 | + |
| 172 | + getAvailabilityConstraintsForDecl(constraints, decl, context); |
| 173 | + |
| 174 | + // If decl is an extension member, query the attributes of the extension, too. |
| 175 | + // |
| 176 | + // Skip decls imported from Clang, though, as they could be associated to the |
| 177 | + // wrong extension and inherit unavailability incorrectly. ClangImporter |
| 178 | + // associates Objective-C protocol members to the first category where the |
| 179 | + // protocol is directly or indirectly adopted, no matter its availability |
| 180 | + // and the availability of other categories. rdar://problem/53956555 |
| 181 | + if (decl->getClangNode()) |
| 182 | + return constraints; |
| 183 | + |
| 184 | + auto parent = AvailabilityInference::parentDeclForInferredAvailability(decl); |
| 185 | + if (auto extension = dyn_cast_or_null<ExtensionDecl>(parent)) |
| 186 | + getAvailabilityConstraintsForDecl(constraints, extension, context); |
| 187 | + |
| 188 | + return constraints; |
| 189 | +} |
0 commit comments