Skip to content

Commit 2c6d7d7

Browse files
committed
[Runtime] Add entry point to compare type context descriptors.
The new function `swift_compareTypeContextDescriptors` is equivalent to a call through to swift::equalContexts. The implementation it the same as that of swift::equalContexts with the following removals: - Handling of context descriptors of kind other outside of ContextDescriptorKind::Type_First...ContextDescriptorKind::Type_Last. Because the arguments are both TypeContextDescriptors, the kinds are known to fall within that range. - Casting to TypeContextDescriptor. The arguments are already of that type. For now, the new function has "future" availability.
1 parent 81483cc commit 2c6d7d7

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed

include/swift/AST/ASTContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,10 @@ class ASTContext final {
654654
/// metadata.
655655
AvailabilityContext getPrespecializedGenericMetadataAvailability();
656656

657+
/// Get the runtime availability of the swift_compareTypeContextDescriptors
658+
/// for the target platform.
659+
AvailabilityContext getCompareTypeContextDescriptorsAvailability();
660+
657661
/// Get the runtime availability of features introduced in the Swift 5.2
658662
/// compiler for the target platform.
659663
AvailabilityContext getSwift52Availability();

include/swift/Runtime/Metadata.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,20 @@ const
307307
/// the same context.
308308
bool equalContexts(const ContextDescriptor *a, const ContextDescriptor *b);
309309

310+
/// Determines whether two type context descriptors describe the same type
311+
/// context.
312+
///
313+
/// Runtime availability: Swift 5.4.
314+
///
315+
/// \param lhs The first type context descriptor to compare.
316+
/// \param rhs The second type context descriptor to compare.
317+
///
318+
/// \returns true if both describe the same type context, false otherwise.
319+
SWIFT_RUNTIME_EXPORT
320+
SWIFT_CC(swift)
321+
bool swift_compareTypeContextDescriptors(const TypeContextDescriptor *lhs,
322+
const TypeContextDescriptor *rhs);
323+
310324
/// Compute the bounds of class metadata with a resilient superclass.
311325
ClassMetadataBounds getResilientMetadataBounds(
312326
const ClassDescriptor *descriptor);

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,18 @@ FUNCTION(GetForeignTypeMetadata, swift_getForeignTypeMetadata,
616616
ARGS(SizeTy, TypeMetadataPtrTy),
617617
ATTRS(NoUnwind, ReadNone)) // only writes to runtime-private fields
618618

619+
// SWIFT_RUNTIME_EXPORT
620+
// SWIFT_CC(swift)
621+
// bool swift_compareTypeContextDescriptors(const TypeContextDescriptor *lhs,
622+
// const TypeContextDescriptor *rhs);
623+
FUNCTION(CompareTypeContextDescriptors,
624+
swift_compareTypeContextDescriptors, SwiftCC,
625+
CompareTypeContextDescriptorsAvailability,
626+
RETURNS(Int1Ty),
627+
ARGS(TypeContextDescriptorPtrTy,
628+
TypeContextDescriptorPtrTy),
629+
ATTRS(NoUnwind, ReadNone))
630+
619631
// MetadataResponse swift_getSingletonMetadata(MetadataRequest request,
620632
// TypeContextDescriptor *type);
621633
FUNCTION(GetSingletonMetadata, swift_getSingletonMetadata,

lib/AST/Availability.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ AvailabilityContext ASTContext::getPrespecializedGenericMetadataAvailability() {
295295
return getSwift53Availability();
296296
}
297297

298+
AvailabilityContext ASTContext::getCompareTypeContextDescriptorsAvailability() {
299+
return getSwiftFutureAvailability();
300+
}
301+
298302
AvailabilityContext ASTContext::getSwift52Availability() {
299303
auto target = LangOpts.Target;
300304

lib/IRGen/IRGenModule.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,16 @@ namespace RuntimeConstants {
650650
}
651651
return RuntimeAvailability::AlwaysAvailable;
652652
}
653+
654+
RuntimeAvailability
655+
CompareTypeContextDescriptorsAvailability(ASTContext &Context) {
656+
auto featureAvailability =
657+
Context.getCompareTypeContextDescriptorsAvailability();
658+
if (!isDeploymentAvailabilityContainedIn(Context, featureAvailability)) {
659+
return RuntimeAvailability::ConditionallyAvailable;
660+
}
661+
return RuntimeAvailability::AlwaysAvailable;
662+
}
653663
} // namespace RuntimeConstants
654664

655665
// We don't use enough attributes to justify generalizing the

stdlib/public/runtime/Metadata.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,33 @@ bool swift::equalContexts(const ContextDescriptor *a,
19181918
}
19191919
}
19201920

1921+
SWIFT_CC(swift)
1922+
bool swift::swift_compareTypeContextDescriptors(
1923+
const TypeContextDescriptor *a, const TypeContextDescriptor *b) {
1924+
// The implementation is the same as the implementation of
1925+
// swift::equalContexts except that the handling of non-type
1926+
// context descriptors and casts to TypeContextDescriptor are removed.
1927+
1928+
// Fast path: pointer equality.
1929+
if (a == b) return true;
1930+
1931+
// If either context is null, we're done.
1932+
if (a == nullptr || b == nullptr)
1933+
return false;
1934+
1935+
// If either descriptor is known to be unique, we're done.
1936+
if (a->isUnique() || b->isUnique()) return false;
1937+
1938+
// Do the kinds match?
1939+
if (a->getKind() != b->getKind()) return false;
1940+
1941+
// Do the parents match?
1942+
if (!equalContexts(a->Parent.get(), b->Parent.get()))
1943+
return false;
1944+
1945+
return TypeContextIdentity(a) == TypeContextIdentity(b);
1946+
}
1947+
19211948
/***************************************************************************/
19221949
/*** Common value witnesses ************************************************/
19231950
/***************************************************************************/

0 commit comments

Comments
 (0)