Skip to content

Commit a60ffb9

Browse files
authored
Merge pull request swiftlang#30196 from slavapestov/abi-check-cleanup
AST: Centralize ABI-related deployment target checks
2 parents ef149d5 + 3904fe8 commit a60ffb9

File tree

7 files changed

+60
-62
lines changed

7 files changed

+60
-62
lines changed

include/swift/AST/ASTContext.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,10 +624,27 @@ class ASTContext final {
624624
void addDestructorCleanup(T &object) {
625625
addCleanup([&object]{ object.~T(); });
626626
}
627+
628+
/// Get the runtime availability of the class metadata update callback
629+
/// mechanism for the target platform.
630+
AvailabilityContext getObjCMetadataUpdateCallbackAvailability();
631+
632+
/// Get the runtime availability of the objc_getClass() hook for the target
633+
/// platform.
634+
AvailabilityContext getObjCGetClassHookAvailability();
627635

628-
/// Get the runtime availability of the opaque types language feature for the target platform.
636+
/// Get the runtime availability of features introduced in the Swift 5.0
637+
/// compiler for the target platform.
638+
AvailabilityContext getSwift50Availability();
639+
640+
/// Get the runtime availability of the opaque types language feature for the
641+
/// target platform.
629642
AvailabilityContext getOpaqueTypeAvailability();
630643

644+
/// Get the runtime availability of the objc_loadClassref() entry point for
645+
/// the target platform.
646+
AvailabilityContext getObjCClassStubsAvailability();
647+
631648
/// Get the runtime availability of features introduced in the Swift 5.1
632649
/// compiler for the target platform.
633650
AvailabilityContext getSwift51Availability();

include/swift/Basic/LangOptions.h

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -390,33 +390,6 @@ namespace swift {
390390
return EffectiveLanguageVersion.isVersionAtLeast(major, minor);
391391
}
392392

393-
// The following deployment targets ship an Objective-C runtime supporting
394-
// the class metadata update callback mechanism:
395-
//
396-
// - macOS 10.14.4
397-
// - iOS 12.2
398-
// - tvOS 12.2
399-
// - watchOS 5.2
400-
bool doesTargetSupportObjCMetadataUpdateCallback() const;
401-
402-
// The following deployment targets ship an Objective-C runtime supporting
403-
// the objc_getClass() hook:
404-
//
405-
// - macOS 10.14.4
406-
// - iOS 12.2
407-
// - tvOS 12.2
408-
// - watchOS 5.2
409-
bool doesTargetSupportObjCGetClassHook() const;
410-
411-
// The following deployment targets ship an Objective-C runtime supporting
412-
// the objc_loadClassref() entry point:
413-
//
414-
// - macOS 10.15
415-
// - iOS 13
416-
// - tvOS 13
417-
// - watchOS 6
418-
bool doesTargetSupportObjCClassStubs() const;
419-
420393
/// Returns true if the given platform condition argument represents
421394
/// a supported target operating system.
422395
///

lib/AST/Availability.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,39 @@ AvailabilityContext AvailabilityInference::inferForType(Type t) {
217217
return walker.AvailabilityInfo;
218218
}
219219

220+
AvailabilityContext ASTContext::getObjCMetadataUpdateCallbackAvailability() {
221+
return getSwift50Availability();
222+
}
223+
224+
AvailabilityContext ASTContext::getObjCGetClassHookAvailability() {
225+
return getSwift50Availability();
226+
}
227+
228+
AvailabilityContext ASTContext::getSwift50Availability() {
229+
auto target = LangOpts.Target;
230+
231+
if (target.isMacOSX()) {
232+
return AvailabilityContext(
233+
VersionRange::allGTE(llvm::VersionTuple(10,14,4)));
234+
} else if (target.isiOS()) {
235+
return AvailabilityContext(
236+
VersionRange::allGTE(llvm::VersionTuple(12,2)));
237+
} else if (target.isWatchOS()) {
238+
return AvailabilityContext(
239+
VersionRange::allGTE(llvm::VersionTuple(5,2)));
240+
} else {
241+
return AvailabilityContext::alwaysAvailable();
242+
}
243+
}
244+
220245
AvailabilityContext ASTContext::getOpaqueTypeAvailability() {
221246
return getSwift51Availability();
222247
}
223248

249+
AvailabilityContext ASTContext::getObjCClassStubsAvailability() {
250+
return getSwift51Availability();
251+
}
252+
224253
AvailabilityContext ASTContext::getSwift51Availability() {
225254
auto target = LangOpts.Target;
226255

lib/Basic/LangOptions.cpp

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -359,34 +359,4 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
359359
// in the common case.
360360

361361
return { false, false };
362-
}
363-
364-
bool LangOptions::doesTargetSupportObjCMetadataUpdateCallback() const {
365-
if (Target.isMacOSX())
366-
return !Target.isMacOSXVersionLT(10, 14, 4);
367-
if (Target.isiOS()) // also returns true on tvOS
368-
return !Target.isOSVersionLT(12, 2);
369-
if (Target.isWatchOS())
370-
return !Target.isOSVersionLT(5, 2);
371-
372-
// Don't assert if we're running on a non-Apple platform; we still
373-
// want to allow running tests that -enable-objc-interop.
374-
return false;
375-
}
376-
377-
bool LangOptions::doesTargetSupportObjCGetClassHook() const {
378-
return doesTargetSupportObjCMetadataUpdateCallback();
379-
}
380-
381-
bool LangOptions::doesTargetSupportObjCClassStubs() const {
382-
if (Target.isMacOSX())
383-
return !Target.isMacOSXVersionLT(10, 15);
384-
if (Target.isiOS()) // also returns true on tvOS
385-
return !Target.isOSVersionLT(13);
386-
if (Target.isWatchOS())
387-
return !Target.isOSVersionLT(6);
388-
389-
// Don't assert if we're running on a non-Apple platform; we still
390-
// want to allow running tests that -enable-objc-interop.
391-
return false;
392-
}
362+
}

lib/IRGen/GenClass.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2347,7 +2347,10 @@ IRGenModule::getClassMetadataStrategy(const ClassDecl *theClass) {
23472347

23482348
// If the Objective-C runtime is new enough, we can just use the update
23492349
// pattern unconditionally.
2350-
if (Context.LangOpts.doesTargetSupportObjCMetadataUpdateCallback())
2350+
auto deploymentAvailability =
2351+
AvailabilityContext::forDeploymentTarget(Context);
2352+
if (deploymentAvailability.isContainedIn(
2353+
Context.getObjCMetadataUpdateCallbackAvailability()))
23512354
return ClassMetadataStrategy::Update;
23522355

23532356
// Otherwise, check if we have legacy type info for backward deployment.

lib/IRGen/GenType.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1294,8 +1294,11 @@ TypeConverter::TypeConverter(IRGenModule &IGM)
12941294
// Whether the Objective-C runtime is guaranteed to invoke the class
12951295
// metadata update callback when realizing a Swift class referenced from
12961296
// Objective-C.
1297+
auto deploymentAvailability =
1298+
AvailabilityContext::forDeploymentTarget(IGM.Context);
12971299
bool supportsObjCMetadataUpdateCallback =
1298-
IGM.Context.LangOpts.doesTargetSupportObjCMetadataUpdateCallback();
1300+
deploymentAvailability.isContainedIn(
1301+
IGM.Context.getObjCMetadataUpdateCallbackAvailability());
12991302

13001303
// If our deployment target allows us to rely on the metadata update
13011304
// callback being called, we don't have to emit a legacy layout for a

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4896,7 +4896,10 @@ static void inferStaticInitializeObjCMetadata(ClassDecl *classDecl) {
48964896
// If the class does not have a custom @objc name and the deployment target
48974897
// supports the objc_getClass() hook, the workaround is unnecessary.
48984898
ASTContext &ctx = classDecl->getASTContext();
4899-
if (ctx.LangOpts.doesTargetSupportObjCGetClassHook() &&
4899+
auto deploymentAvailability =
4900+
AvailabilityContext::forDeploymentTarget(ctx);
4901+
if (deploymentAvailability.isContainedIn(
4902+
ctx.getObjCGetClassHookAvailability()) &&
49004903
!hasExplicitObjCName(classDecl))
49014904
return;
49024905

0 commit comments

Comments
 (0)