Just a quick brain dump of "first steps" to move Swift/C++ interoperability forward. While there are many big and interesting design questions for a good interoperability story, there are also a large number of implementation tasks that can proceed independently of those design discussions (and often in parallel). This list focuses on those implementation tasks:
- (DONE) Add a flag
-enable-cxx-interop
to the frontend, and have it enable (Objective-)C++ mode in the Clang importer. - Add a lit configuration flag to enable
-enable-cxx-interop
for the full Swift testsuite and fix all of the issues that prevent (Objective-)C code from being imported correctly when it's compiled as C++. The testsuite will likely need a lot ofextern "C"
annotations throughout it's headers---those fixes can be committed to the testsuite behind appropriate #ifdefs. - Import C++ namespaces as Swift enums that have no cases (so-called "uninhabited enums") so the C++ lexical structure is represented in Swift
- Import C++ operators as Swift functions with the same operator name.
- (IN PROGRESS: swiftlang/swift#26047) Import C++ member functions as member functions in Swift. For non-static member functions, you'll need to support C++'s member function calling convention
- Import C++ constructors as Swift initializers, so we can create instances of C++ types in Swift
- Import C++ reference parameters appropriately for Swift, separating "const &" parameters (which should be passed as indirect but are otherwise immutable) and "&" parameters (which should appear as
inout
in Swift). Some discussion of this at https://forums.swift.org/t/c-interop/25567 - Put the C++ copy constructor, copy assignment operator, move constructor, move assignment operator, and destructor into the value witness table for a C++ type. If any are non-trivial, treat the C++ type as "address only" in SIL. This should make non-POD C++ types provide proper copying/moving/destruction semantics.
- Introduce a mechanism to cope with C++ inheritance of value types, e.g., mirroring the visible members from inherited classes on each Swift
struct
type and having IR generation perform the appropriate adjustment to thethis
pointer. - Import polymorphic C++ classes as Swift "foreign" classes, import the destructor as
deinit
, add support for callingvirtual
C++ methods, and having reference counting for these foreign classes be a no-op; introduce some explicitdelete
operation.