Skip to content

Commit 98e81cd

Browse files
[IRGen] De-duplicate implementations of minimum OS versions supporting mangling.
This makes it easier to add handling for new types with special mangling, as only one place needs to be changed instead of two.
1 parent d181336 commit 98e81cd

File tree

5 files changed

+40
-22
lines changed

5 files changed

+40
-22
lines changed

include/swift/AST/ASTContext.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,11 @@ class ASTContext final {
784784
/// compiler for the target platform.
785785
AvailabilityContext getSwift56Availability();
786786

787+
// Note: Update this function if you add a new getSwiftXYAvailability above.
788+
/// Get the runtime availability for a particular version of Swift (5.0+).
789+
AvailabilityContext
790+
getSwift5PlusAvailability(llvm::VersionTuple swiftVersion);
791+
787792
/// Get the runtime availability of features that have been introduced in the
788793
/// Swift compiler for future versions of the target platform.
789794
AvailabilityContext getSwiftFutureAvailability();

lib/AST/Availability.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,21 @@ AvailabilityContext ASTContext::getSwiftFutureAvailability() {
457457
return AvailabilityContext::alwaysAvailable();
458458
}
459459
}
460+
461+
AvailabilityContext
462+
ASTContext::getSwift5PlusAvailability(llvm::VersionTuple swiftVersion) {
463+
if (swiftVersion.getMajor() == 5) {
464+
switch (*swiftVersion.getMinor()) {
465+
case 0: return getSwift50Availability();
466+
case 1: return getSwift51Availability();
467+
case 2: return getSwift52Availability();
468+
case 3: return getSwift53Availability();
469+
case 4: return getSwift54Availability();
470+
case 5: return getSwift55Availability();
471+
case 6: return getSwift56Availability();
472+
default: break;
473+
}
474+
}
475+
llvm::report_fatal_error("Missing call to getSwiftXYAvailability for Swift " +
476+
swiftVersion.getAsString());
477+
}

lib/IRGen/GenReflection.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,8 @@ class PrintMetadataSource
172172
}
173173
};
174174

175-
// Return the minimum Swift runtime version that supports demangling a given
176-
// type.
177-
static llvm::VersionTuple
178-
getRuntimeVersionThatSupportsDemanglingType(IRGenModule &IGM,
179-
CanType type) {
175+
Optional<llvm::VersionTuple>
176+
swift::irgen::getRuntimeVersionThatSupportsDemanglingType(CanType type) {
180177
// Associated types of opaque types weren't mangled in a usable form by the
181178
// Swift 5.1 runtime, so we needed to add a new mangling in 5.2.
182179
if (type->hasOpaqueArchetype()) {
@@ -194,8 +191,7 @@ getRuntimeVersionThatSupportsDemanglingType(IRGenModule &IGM,
194191
// guards, so we don't need to limit availability of mangled names
195192
// involving them.
196193
}
197-
198-
return llvm::VersionTuple(5, 0);
194+
return None;
199195
}
200196

201197
// Produce a fallback mangled type name that uses an open-coded callback
@@ -289,9 +285,11 @@ getTypeRefImpl(IRGenModule &IGM,
289285
// symbolic reference with a callback function.
290286
if (auto runtimeCompatVersion = getSwiftRuntimeCompatibilityVersionForTarget
291287
(IGM.Context.LangOpts.Target)) {
292-
if (*runtimeCompatVersion <
293-
getRuntimeVersionThatSupportsDemanglingType(IGM, type)) {
294-
return getTypeRefByFunction(IGM, sig, type);
288+
if (auto minimumSupportedRuntimeVersion =
289+
getRuntimeVersionThatSupportsDemanglingType(type)) {
290+
if (*runtimeCompatVersion < *minimumSupportedRuntimeVersion) {
291+
return getTypeRefByFunction(IGM, sig, type);
292+
}
295293
}
296294
}
297295

lib/IRGen/IRGenMangler.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,11 @@ class IRGenMangler : public Mangle::ASTMangler {
650650
}
651651
};
652652

653+
/// Does this type require a special minimum Swift runtime version which
654+
/// supports demangling it?
655+
Optional<llvm::VersionTuple>
656+
getRuntimeVersionThatSupportsDemanglingType(CanType type);
657+
653658
} // end namespace irgen
654659
} // end namespace swift
655660

lib/IRGen/MetadataRequest.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,18 +2487,10 @@ static bool shouldAccessByMangledName(IRGenModule &IGM, CanType type) {
24872487
}
24882488

24892489
// The Swift 5.1 runtime fails to demangle associated types of opaque types.
2490-
if (!IGM.getAvailabilityContext().isContainedIn(IGM.Context.getSwift52Availability())) {
2491-
auto hasNestedOpaqueArchetype = type.findIf([](CanType sub) -> bool {
2492-
if (auto archetype = dyn_cast<NestedArchetypeType>(sub)) {
2493-
if (isa<OpaqueTypeArchetypeType>(archetype->getRoot())) {
2494-
return true;
2495-
}
2496-
}
2497-
return false;
2498-
});
2499-
2500-
if (hasNestedOpaqueArchetype)
2501-
return false;
2490+
if (auto minimumSwiftVersion =
2491+
getRuntimeVersionThatSupportsDemanglingType(type)) {
2492+
return IGM.getAvailabilityContext().isContainedIn(
2493+
IGM.Context.getSwift5PlusAvailability(*minimumSwiftVersion));
25022494
}
25032495

25042496
return true;

0 commit comments

Comments
 (0)