blob: c62460b05b97ac758b573815a267bb6ea24fd0f9 [file] [log] [blame]
//===--- 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 "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");
}
Optional<PlatformKind> swift::platformFromString(StringRef Name) {
if (Name == "*")
return PlatformKind::none;
return llvm::StringSwitch<Optional<PlatformKind>>(Name)
#define AVAILABILITY_PLATFORM(X, PrettyName) .Case(#X, PlatformKind::X)
#include "swift/AST/PlatformKinds.def"
.Case("macOS", PlatformKind::OSX)
.Case("macOSApplicationExtension", PlatformKind::OSXApplicationExtension)
.Default(Optional<PlatformKind>());
}
static bool isPlatformActiveForTarget(PlatformKind Platform,
const llvm::Triple &Target,
bool EnableAppExtensionRestrictions) {
if (Platform == PlatformKind::none)
return true;
if (Platform == PlatformKind::OSXApplicationExtension ||
Platform == PlatformKind::iOSApplicationExtension)
if (!EnableAppExtensionRestrictions)
return false;
// FIXME: This is an awful way to get the current OS.
switch (Platform) {
case PlatformKind::OSX:
case PlatformKind::OSXApplicationExtension:
return Target.isMacOSX();
case PlatformKind::iOS:
case PlatformKind::iOSApplicationExtension:
return Target.isiOS() && !Target.isTvOS();
case PlatformKind::tvOS:
case PlatformKind::tvOSApplicationExtension:
return Target.isTvOS();
case PlatformKind::watchOS:
case PlatformKind::watchOSApplicationExtension:
return Target.isWatchOS();
case PlatformKind::none:
llvm_unreachable("handled above");
}
llvm_unreachable("bad PlatformKind");
}
bool swift::isPlatformActive(PlatformKind Platform, LangOptions &LangOpts) {
llvm::Triple TT = LangOpts.Target;
return isPlatformActiveForTarget(Platform, TT,
LangOpts.EnableAppExtensionRestrictions);
}
PlatformKind swift::targetPlatform(LangOptions &LangOpts) {
if (LangOpts.Target.isMacOSX()) {
return (LangOpts.EnableAppExtensionRestrictions
? PlatformKind::OSXApplicationExtension
: PlatformKind::OSX);
}
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()) {
return (LangOpts.EnableAppExtensionRestrictions
? PlatformKind::iOSApplicationExtension
: PlatformKind::iOS);
}
return PlatformKind::none;
}