1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
//===--- PlatformKind.cpp - Swift Language Platform Kinds -----------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements the platform kinds for API availability.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/PlatformKind.h"
#include "swift/Basic/LangOptions.h"
#include "swift/Basic/Platform.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/ErrorHandling.h"
using namespace swift;
StringRef swift::platformString(PlatformKind platform) {
switch (platform) {
case PlatformKind::none:
return "*";
#define AVAILABILITY_PLATFORM(X, PrettyName) \
case PlatformKind::X: \
return #X;
#include "swift/AST/PlatformKinds.def"
}
llvm_unreachable("bad PlatformKind");
}
StringRef swift::prettyPlatformString(PlatformKind platform) {
switch (platform) {
case PlatformKind::none:
return "*";
#define AVAILABILITY_PLATFORM(X, PrettyName) \
case PlatformKind::X: \
return PrettyName;
#include "swift/AST/PlatformKinds.def"
}
llvm_unreachable("bad PlatformKind");
}
std::optional<PlatformKind> swift::platformFromString(StringRef Name) {
if (Name == "*")
return PlatformKind::none;
return llvm::StringSwitch<std::optional<PlatformKind>>(Name)
#define AVAILABILITY_PLATFORM(X, PrettyName) .Case(#X, PlatformKind::X)
#include "swift/AST/PlatformKinds.def"
.Case("OSX", PlatformKind::macOS)
.Case("OSXApplicationExtension", PlatformKind::macOSApplicationExtension)
.Default(std::optional<PlatformKind>());
}
std::optional<PlatformKind> swift::platformFromUnsigned(unsigned value) {
PlatformKind platform = PlatformKind(value);
switch (platform) {
case PlatformKind::none:
#define AVAILABILITY_PLATFORM(X, PrettyName) case PlatformKind::X:
#include "swift/AST/PlatformKinds.def"
return platform;
}
return std::nullopt;
}
std::optional<StringRef>
swift::closestCorrectedPlatformString(StringRef candidate) {
auto lowerCasedCandidate = candidate.lower();
auto lowerCasedCandidateRef = StringRef(lowerCasedCandidate);
auto minDistance = std::numeric_limits<unsigned int>::max();
std::optional<StringRef> result = std::nullopt;
#define AVAILABILITY_PLATFORM(X, PrettyName) \
{ \
auto platform = StringRef(#X); \
auto distance = lowerCasedCandidateRef.edit_distance(platform.lower()); \
if (distance == 0) { \
return platform; \
} \
if (distance < minDistance) { \
minDistance = distance; \
result = platform; \
} \
}
#include "swift/AST/PlatformKinds.def"
// If the most similar platform distance is greater than this threshold,
// it's not similar enough to be suggested as correction.
const unsigned int distanceThreshold = 5;
return (minDistance < distanceThreshold) ? result : std::nullopt;
}
std::optional<PlatformKind>
swift::basePlatformForExtensionPlatform(PlatformKind Platform) {
switch (Platform) {
case PlatformKind::macOSApplicationExtension:
return PlatformKind::macOS;
case PlatformKind::iOSApplicationExtension:
return PlatformKind::iOS;
case PlatformKind::macCatalystApplicationExtension:
return PlatformKind::macCatalyst;
case PlatformKind::tvOSApplicationExtension:
return PlatformKind::tvOS;
case PlatformKind::watchOSApplicationExtension:
return PlatformKind::watchOS;
case PlatformKind::visionOSApplicationExtension:
return PlatformKind::visionOS;
case PlatformKind::macOS:
case PlatformKind::iOS:
case PlatformKind::macCatalyst:
case PlatformKind::tvOS:
case PlatformKind::watchOS:
case PlatformKind::visionOS:
case PlatformKind::OpenBSD:
case PlatformKind::Windows:
case PlatformKind::none:
return std::nullopt;
}
llvm_unreachable("bad PlatformKind");
}
static bool isPlatformActiveForTarget(PlatformKind Platform,
const llvm::Triple &Target,
bool EnableAppExtensionRestrictions,
bool ForRuntimeQuery) {
if (Platform == PlatformKind::none)
return true;
if (!EnableAppExtensionRestrictions &&
isApplicationExtensionPlatform(Platform))
return false;
// FIXME: This is an awful way to get the current OS.
switch (Platform) {
case PlatformKind::macOS:
case PlatformKind::macOSApplicationExtension:
return Target.isMacOSX();
case PlatformKind::iOS:
case PlatformKind::iOSApplicationExtension:
if (!ForRuntimeQuery && Target.isXROS()) {
return true;
}
return Target.isiOS() && !Target.isTvOS();
case PlatformKind::macCatalyst:
case PlatformKind::macCatalystApplicationExtension:
return tripleIsMacCatalystEnvironment(Target);
case PlatformKind::tvOS:
case PlatformKind::tvOSApplicationExtension:
return Target.isTvOS();
case PlatformKind::watchOS:
case PlatformKind::watchOSApplicationExtension:
return Target.isWatchOS();
case PlatformKind::visionOS:
case PlatformKind::visionOSApplicationExtension:
return Target.isXROS();
case PlatformKind::OpenBSD:
return Target.isOSOpenBSD();
case PlatformKind::Windows:
return Target.isOSWindows();
case PlatformKind::none:
llvm_unreachable("handled above");
}
llvm_unreachable("bad PlatformKind");
}
bool swift::isPlatformActive(PlatformKind Platform, const LangOptions &LangOpts,
bool ForTargetVariant, bool ForRuntimeQuery) {
if (ForTargetVariant) {
assert(LangOpts.TargetVariant && "Must have target variant triple");
return isPlatformActiveForTarget(Platform, *LangOpts.TargetVariant,
LangOpts.EnableAppExtensionRestrictions,
ForRuntimeQuery);
}
return isPlatformActiveForTarget(Platform, LangOpts.Target,
LangOpts.EnableAppExtensionRestrictions, ForRuntimeQuery);
}
PlatformKind swift::targetPlatform(const LangOptions &LangOpts) {
if (LangOpts.Target.isMacOSX()) {
return (LangOpts.EnableAppExtensionRestrictions
? PlatformKind::macOSApplicationExtension
: PlatformKind::macOS);
}
if (LangOpts.Target.isTvOS()) {
return (LangOpts.EnableAppExtensionRestrictions
? PlatformKind::tvOSApplicationExtension
: PlatformKind::tvOS);
}
if (LangOpts.Target.isWatchOS()) {
return (LangOpts.EnableAppExtensionRestrictions
? PlatformKind::watchOSApplicationExtension
: PlatformKind::watchOS);
}
if (LangOpts.Target.isiOS()) {
if (tripleIsMacCatalystEnvironment(LangOpts.Target))
return (LangOpts.EnableAppExtensionRestrictions
? PlatformKind::macCatalystApplicationExtension
: PlatformKind::macCatalyst);
return (LangOpts.EnableAppExtensionRestrictions
? PlatformKind::iOSApplicationExtension
: PlatformKind::iOS);
}
if (LangOpts.Target.isXROS()) {
return (LangOpts.EnableAppExtensionRestrictions
? PlatformKind::visionOSApplicationExtension
: PlatformKind::visionOS);
}
return PlatformKind::none;
}
bool swift::inheritsAvailabilityFromPlatform(PlatformKind Child,
PlatformKind Parent) {
if (auto ChildPlatformBase = basePlatformForExtensionPlatform(Child)) {
if (Parent == ChildPlatformBase)
return true;
}
if (Child == PlatformKind::macCatalyst && Parent == PlatformKind::iOS)
return true;
if (Child == PlatformKind::macCatalystApplicationExtension) {
if (Parent == PlatformKind::iOS ||
Parent == PlatformKind::iOSApplicationExtension) {
return true;
}
}
if (Child == PlatformKind::visionOS && Parent == PlatformKind::iOS)
return true;
if (Child == PlatformKind::visionOSApplicationExtension) {
if (Parent == PlatformKind::iOS ||
Parent == PlatformKind::iOSApplicationExtension) {
return true;
}
}
return false;
}
llvm::VersionTuple swift::canonicalizePlatformVersion(
PlatformKind platform, const llvm::VersionTuple &version) {
// Canonicalize macOS version for macOS Big Sur to treat
// 10.16 as 11.0.
if (platform == PlatformKind::macOS ||
platform == PlatformKind::macOSApplicationExtension) {
return llvm::Triple::getCanonicalVersionForOS(llvm::Triple::MacOSX,
version);
}
return version;
}
|