Skip to content

Commit 588c0b2

Browse files
authored
Merge pull request swiftlang#22324 from devincoughlin/factor-out-active-target-helper
2 parents c6ee1b3 + da57ab3 commit 588c0b2

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

lib/AST/PlatformKind.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,35 +56,43 @@ Optional<PlatformKind> swift::platformFromString(StringRef Name) {
5656
.Default(Optional<PlatformKind>());
5757
}
5858

59-
bool swift::isPlatformActive(PlatformKind Platform, LangOptions &LangOpts) {
59+
static bool isPlatformActiveForTarget(PlatformKind Platform,
60+
const llvm::Triple &Target,
61+
bool EnableAppExtensionRestrictions) {
6062
if (Platform == PlatformKind::none)
6163
return true;
6264

6365
if (Platform == PlatformKind::OSXApplicationExtension ||
6466
Platform == PlatformKind::iOSApplicationExtension)
65-
if (!LangOpts.EnableAppExtensionRestrictions)
67+
if (!EnableAppExtensionRestrictions)
6668
return false;
6769

6870
// FIXME: This is an awful way to get the current OS.
6971
switch (Platform) {
7072
case PlatformKind::OSX:
7173
case PlatformKind::OSXApplicationExtension:
72-
return LangOpts.Target.isMacOSX();
74+
return Target.isMacOSX();
7375
case PlatformKind::iOS:
7476
case PlatformKind::iOSApplicationExtension:
75-
return LangOpts.Target.isiOS() && !LangOpts.Target.isTvOS();
77+
return Target.isiOS() && !Target.isTvOS();
7678
case PlatformKind::tvOS:
7779
case PlatformKind::tvOSApplicationExtension:
78-
return LangOpts.Target.isTvOS();
80+
return Target.isTvOS();
7981
case PlatformKind::watchOS:
8082
case PlatformKind::watchOSApplicationExtension:
81-
return LangOpts.Target.isWatchOS();
83+
return Target.isWatchOS();
8284
case PlatformKind::none:
8385
llvm_unreachable("handled above");
8486
}
8587
llvm_unreachable("bad PlatformKind");
8688
}
8789

90+
bool swift::isPlatformActive(PlatformKind Platform, LangOptions &LangOpts) {
91+
llvm::Triple TT = LangOpts.Target;
92+
return isPlatformActiveForTarget(Platform, TT,
93+
LangOpts.EnableAppExtensionRestrictions);
94+
}
95+
8896
PlatformKind swift::targetPlatform(LangOptions &LangOpts) {
8997
if (LangOpts.Target.isMacOSX()) {
9098
return (LangOpts.EnableAppExtensionRestrictions

lib/SILGen/SILGenDecl.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,25 +1194,38 @@ void SILGenFunction::visitVarDecl(VarDecl *D) {
11941194
// We handle emitting the variable storage when we see the pattern binding.
11951195
}
11961196

1197-
/// Emit a check that returns 1 if the running OS version is in
1198-
/// the specified version range and 0 otherwise. The returned SILValue
1199-
/// (which has type Builtin.Int1) represents the result of this check.
1200-
SILValue SILGenFunction::emitOSVersionRangeCheck(SILLocation loc,
1201-
const VersionRange &range) {
1202-
// Emit constants for the checked version range.
1203-
llvm::VersionTuple Vers = range.getLowerEndpoint();
1197+
/// Emit literals for the major, minor, and subminor components of the version
1198+
/// and return a tuple of SILValues for them.
1199+
static std::tuple<SILValue, SILValue, SILValue>
1200+
emitVersionLiterals(SILLocation loc, SILGenBuilder &B, ASTContext &ctx,
1201+
llvm::VersionTuple Vers) {
12041202
unsigned major = Vers.getMajor();
12051203
unsigned minor =
12061204
(Vers.getMinor().hasValue() ? Vers.getMinor().getValue() : 0);
12071205
unsigned subminor =
12081206
(Vers.getSubminor().hasValue() ? Vers.getSubminor().getValue() : 0);
12091207

1210-
SILType wordType = SILType::getBuiltinWordType(getASTContext());
1208+
SILType wordType = SILType::getBuiltinWordType(ctx);
12111209

12121210
SILValue majorValue = B.createIntegerLiteral(loc, wordType, major);
12131211
SILValue minorValue = B.createIntegerLiteral(loc, wordType, minor);
12141212
SILValue subminorValue = B.createIntegerLiteral(loc, wordType, subminor);
12151213

1214+
return std::make_tuple(majorValue, minorValue, subminorValue);
1215+
}
1216+
1217+
/// Emit a check that returns 1 if the running OS version is in
1218+
/// the specified version range and 0 otherwise. The returned SILValue
1219+
/// (which has type Builtin.Int1) represents the result of this check.
1220+
SILValue SILGenFunction::emitOSVersionRangeCheck(SILLocation loc,
1221+
const VersionRange &range) {
1222+
// Emit constants for the checked version range.
1223+
SILValue majorValue;
1224+
SILValue minorValue;
1225+
SILValue subminorValue;
1226+
std::tie(majorValue, minorValue, subminorValue) =
1227+
emitVersionLiterals(loc, B, getASTContext(), range.getLowerEndpoint());
1228+
12161229
// Emit call to _stdlib_isOSVersionAtLeast(major, minor, patch)
12171230
FuncDecl *versionQueryDecl =
12181231
getASTContext().getIsOSVersionAtLeastDecl();

0 commit comments

Comments
 (0)