Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/AST/KnownProtocols.def
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ PROTOCOL(DistributedTargetInvocationDecoder)
PROTOCOL(DistributedTargetInvocationResultHandler)

// C++ Standard Library Overlay:
PROTOCOL(CxxConvertibleToCollection)
PROTOCOL(CxxRandomAccessCollection)
PROTOCOL(CxxSequence)
PROTOCOL(UnsafeCxxInputIterator)
Expand Down
1 change: 1 addition & 0 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,7 @@ ProtocolDecl *ASTContext::getProtocol(KnownProtocolKind kind) const {
case KnownProtocolKind::DistributedTargetInvocationResultHandler:
M = getLoadedModule(Id_Distributed);
break;
case KnownProtocolKind::CxxConvertibleToCollection:
case KnownProtocolKind::CxxRandomAccessCollection:
case KnownProtocolKind::CxxSequence:
case KnownProtocolKind::UnsafeCxxInputIterator:
Expand Down
93 changes: 55 additions & 38 deletions lib/ClangImporter/ClangDerivedConformances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ void swift::conformToCxxSequenceIfNeeded(
ctx.getProtocol(KnownProtocolKind::UnsafeCxxInputIterator);
ProtocolDecl *cxxSequenceProto =
ctx.getProtocol(KnownProtocolKind::CxxSequence);
ProtocolDecl *cxxConvertibleProto =
ctx.getProtocol(KnownProtocolKind::CxxConvertibleToCollection);
// If the Cxx module is missing, or does not include one of the necessary
// protocols, bail.
if (!cxxIteratorProto || !cxxSequenceProto)
Expand Down Expand Up @@ -389,47 +391,62 @@ void swift::conformToCxxSequenceIfNeeded(

// Try to conform to CxxRandomAccessCollection if possible.

auto cxxRAIteratorProto =
ctx.getProtocol(KnownProtocolKind::UnsafeCxxRandomAccessIterator);
if (!cxxRAIteratorProto ||
!ctx.getProtocol(KnownProtocolKind::CxxRandomAccessCollection))
return;

// Check if `begin()` and `end()` are non-mutating.
if (begin->isMutating() || end->isMutating())
return;
auto tryToConformToRandomAccessCollection = [&]() -> bool {
auto cxxRAIteratorProto =
ctx.getProtocol(KnownProtocolKind::UnsafeCxxRandomAccessIterator);
if (!cxxRAIteratorProto ||
!ctx.getProtocol(KnownProtocolKind::CxxRandomAccessCollection))
return false;

// Check if RawIterator conforms to UnsafeCxxRandomAccessIterator.
auto rawIteratorRAConformanceRef =
decl->getModuleContext()->lookupConformance(rawIteratorTy,
cxxRAIteratorProto);
if (!isConcreteAndValid(rawIteratorRAConformanceRef, module))
return;
// Check if `begin()` and `end()` are non-mutating.
if (begin->isMutating() || end->isMutating())
return false;

// CxxRandomAccessCollection always uses Int as an Index.
auto indexTy = ctx.getIntType();
// Check if RawIterator conforms to UnsafeCxxRandomAccessIterator.
auto rawIteratorRAConformanceRef =
decl->getModuleContext()->lookupConformance(rawIteratorTy,
cxxRAIteratorProto);
if (!isConcreteAndValid(rawIteratorRAConformanceRef, module))
return false;

auto sliceTy = ctx.getSliceType();
sliceTy = sliceTy.subst(
[&](SubstitutableType *dependentType) {
if (dependentType->isEqual(cxxSequenceSelfTy))
return declSelfTy;
return Type(dependentType);
},
LookUpConformanceInModule(module));
// CxxRandomAccessCollection always uses Int as an Index.
auto indexTy = ctx.getIntType();

auto sliceTy = ctx.getSliceType();
sliceTy = sliceTy.subst(
[&](SubstitutableType *dependentType) {
if (dependentType->isEqual(cxxSequenceSelfTy))
return declSelfTy;
return Type(dependentType);
},
LookUpConformanceInModule(module));

auto indicesTy = ctx.getRangeType();
indicesTy = indicesTy.subst(
[&](SubstitutableType *dependentType) {
if (dependentType->isEqual(cxxSequenceSelfTy))
return indexTy;
return Type(dependentType);
},
LookUpConformanceInModule(module));

impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Index"), indexTy);
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Indices"), indicesTy);
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("SubSequence"),
sliceTy);
impl.addSynthesizedProtocolAttrs(
decl, {KnownProtocolKind::CxxRandomAccessCollection});
return true;
};

auto indicesTy = ctx.getRangeType();
indicesTy = indicesTy.subst(
[&](SubstitutableType *dependentType) {
if (dependentType->isEqual(cxxSequenceSelfTy))
return indexTy;
return Type(dependentType);
},
LookUpConformanceInModule(module));
bool conformedToRAC = tryToConformToRandomAccessCollection();

impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Index"), indexTy);
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Indices"), indicesTy);
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("SubSequence"), sliceTy);
impl.addSynthesizedProtocolAttrs(
decl, {KnownProtocolKind::CxxRandomAccessCollection});
// If the collection does not support random access, let's still allow the
// developer to explicitly convert a C++ sequence to a Swift Array (making a
// copy of the sequence's elements) by conforming the type to
// CxxCollectionConvertible. This enables an overload of Array.init declared
// in the Cxx module.
if (!conformedToRAC && cxxConvertibleProto)
impl.addSynthesizedProtocolAttrs(
decl, {KnownProtocolKind::CxxConvertibleToCollection});
}
1 change: 1 addition & 0 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5884,6 +5884,7 @@ SpecialProtocol irgen::getSpecialProtocolID(ProtocolDecl *P) {
case KnownProtocolKind::DistributedTargetInvocationEncoder:
case KnownProtocolKind::DistributedTargetInvocationDecoder:
case KnownProtocolKind::DistributedTargetInvocationResultHandler:
case KnownProtocolKind::CxxConvertibleToCollection:
case KnownProtocolKind::CxxRandomAccessCollection:
case KnownProtocolKind::CxxSequence:
case KnownProtocolKind::UnsafeCxxInputIterator:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import Cxx

var CxxSequenceTestSuite = TestSuite("CxxConvertibleToCollection")

extension SimpleSequence: CxxConvertibleToCollection {}

CxxSequenceTestSuite.test("SimpleSequence to Swift.Array") {
let seq = SimpleSequence()
let array = Array(seq)
Expand All @@ -23,8 +21,6 @@ CxxSequenceTestSuite.test("SimpleSequence to Swift.Set") {
expectEqual(Set([1, 2, 3, 4] as [Int32]), set)
}

extension SimpleEmptySequence: CxxConvertibleToCollection {}

CxxSequenceTestSuite.test("SimpleEmptySequence to Swift.Array") {
let seq = SimpleEmptySequence()
let array = Array(seq)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
// RUN: %target-swift-ide-test -print-module -module-to-print=CustomSequence -source-filename=x -I %S/Inputs -enable-experimental-cxx-interop -module-cache-path %t | %FileCheck %s

// CHECK: struct SimpleSequence {
// CHECK: struct SimpleSequence : CxxConvertibleToCollection {
// CHECK: typealias Element = ConstIterator.Pointee
// CHECK: typealias Iterator = CxxIterator<SimpleSequence>
// CHECK: typealias RawIterator = ConstIterator
// CHECK: }

// CHECK: struct SimpleSequenceWithOutOfLineEqualEqual {
// CHECK: struct SimpleSequenceWithOutOfLineEqualEqual : CxxConvertibleToCollection {
// CHECK: typealias Element = ConstIteratorOutOfLineEq.Pointee
// CHECK: typealias Iterator = CxxIterator<SimpleSequenceWithOutOfLineEqualEqual>
// CHECK: typealias RawIterator = ConstIteratorOutOfLineEq
// CHECK: }

// CHECK: struct SimpleArrayWrapperNullableIterators {
// CHECK: struct SimpleArrayWrapperNullableIterators : CxxConvertibleToCollection {
// CHECK: typealias Element = Optional<UnsafePointer<Int32>>.Pointee
// CHECK: typealias Iterator = CxxIterator<SimpleArrayWrapperNullableIterators>
// CHECK: typealias RawIterator = UnsafePointer<Int32>?
// CHECK: }

// CHECK: struct SimpleEmptySequence {
// CHECK: struct SimpleEmptySequence : CxxConvertibleToCollection {
// CHECK: typealias Element = Optional<UnsafePointer<Int32>>.Pointee
// CHECK: typealias Iterator = CxxIterator<SimpleEmptySequence>
// CHECK: typealias RawIterator = UnsafePointer<Int32>?
// CHECK: }

// CHECK: struct HasMutatingBeginEnd {
// CHECK: struct HasMutatingBeginEnd : CxxConvertibleToCollection {
// CHECK: typealias Element = ConstIterator.Pointee
// CHECK: typealias Iterator = CxxIterator<HasMutatingBeginEnd>
// CHECK: typealias RawIterator = ConstIterator
Expand Down