Skip to content

Commit 7eaf4b5

Browse files
varungandhi-appleDougGregor
authored andcommitted
[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 44d42d8 commit 7eaf4b5

File tree

5 files changed

+39
-21
lines changed

5 files changed

+39
-21
lines changed

include/swift/AST/ASTContext.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,11 @@ class ASTContext final {
753753
/// compiler for the target platform.
754754
AvailabilityContext getSwift55Availability();
755755

756+
// Note: Update this function if you add a new getSwiftXYAvailability above.
757+
/// Get the runtime availability for a particular version of Swift (5.0+).
758+
AvailabilityContext
759+
getSwift5PlusAvailability(llvm::VersionTuple swiftVersion);
760+
756761
/// Get the runtime availability of features that have been introduced in the
757762
/// Swift compiler for future versions of the target platform.
758763
AvailabilityContext getSwiftFutureAvailability();

lib/AST/Availability.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,3 +450,20 @@ AvailabilityContext ASTContext::getSwiftFutureAvailability() {
450450
return AvailabilityContext::alwaysAvailable();
451451
}
452452
}
453+
454+
AvailabilityContext
455+
ASTContext::getSwift5PlusAvailability(llvm::VersionTuple swiftVersion) {
456+
if (swiftVersion.getMajor() == 5) {
457+
switch (*swiftVersion.getMinor()) {
458+
case 0: return getSwift50Availability();
459+
case 1: return getSwift51Availability();
460+
case 2: return getSwift52Availability();
461+
case 3: return getSwift53Availability();
462+
case 4: return getSwift54Availability();
463+
case 5: return getSwift55Availability();
464+
default: break;
465+
}
466+
}
467+
llvm::report_fatal_error("Missing call to getSwiftXYAvailability for Swift " +
468+
swiftVersion.getAsString());
469+
}

lib/IRGen/GenReflection.cpp

Lines changed: 8 additions & 9 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()) {
@@ -205,7 +202,7 @@ getRuntimeVersionThatSupportsDemanglingType(IRGenModule &IGM,
205202
return llvm::VersionTuple(5, 5);
206203
}
207204

208-
return llvm::VersionTuple(5, 0);
205+
return None;
209206
}
210207

211208
// Produce a fallback mangled type name that uses an open-coded callback
@@ -302,9 +299,11 @@ getTypeRefImpl(IRGenModule &IGM,
302299
// symbolic reference with a callback function.
303300
if (auto runtimeCompatVersion = getSwiftRuntimeCompatibilityVersionForTarget
304301
(IGM.Context.LangOpts.Target)) {
305-
if (*runtimeCompatVersion <
306-
getRuntimeVersionThatSupportsDemanglingType(IGM, type)) {
307-
return getTypeRefByFunction(IGM, sig, type);
302+
if (auto minimumSupportedRuntimeVersion =
303+
getRuntimeVersionThatSupportsDemanglingType(type)) {
304+
if (*runtimeCompatVersion < *minimumSupportedRuntimeVersion) {
305+
return getTypeRefByFunction(IGM, sig, type);
306+
}
308307
}
309308
}
310309

lib/IRGen/IRGenMangler.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,11 @@ class IRGenMangler : public Mangle::ASTMangler {
641641
}
642642
};
643643

644+
/// Does this type require a special minimum Swift runtime version which
645+
/// supports demangling it?
646+
Optional<llvm::VersionTuple>
647+
getRuntimeVersionThatSupportsDemanglingType(CanType type);
648+
644649
} // end namespace irgen
645650
} // end namespace swift
646651

lib/IRGen/MetadataRequest.cpp

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

24982498
// The Swift 5.1 runtime fails to demangle associated types of opaque types.
2499-
if (!IGM.getAvailabilityContext().isContainedIn(IGM.Context.getSwift52Availability())) {
2500-
auto hasNestedOpaqueArchetype = type.findIf([](CanType sub) -> bool {
2501-
if (auto archetype = dyn_cast<NestedArchetypeType>(sub)) {
2502-
if (isa<OpaqueTypeArchetypeType>(archetype->getRoot())) {
2503-
return true;
2504-
}
2505-
}
2506-
return false;
2507-
});
2508-
2509-
if (hasNestedOpaqueArchetype)
2510-
return false;
2499+
if (auto minimumSwiftVersion =
2500+
getRuntimeVersionThatSupportsDemanglingType(type)) {
2501+
return IGM.getAvailabilityContext().isContainedIn(
2502+
IGM.Context.getSwift5PlusAvailability(*minimumSwiftVersion));
25112503
}
25122504

25132505
return true;

0 commit comments

Comments
 (0)