Skip to content

Instantly share code, notes, and snippets.

@DougGregor
Created July 18, 2019 17:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DougGregor/8c23737226f500a73bd14909449046b5 to your computer and use it in GitHub Desktop.
Save DougGregor/8c23737226f500a73bd14909449046b5 to your computer and use it in GitHub Desktop.
First steps for Swift/C++ interoperability

First steps for Swift/C++ Interoperability

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:

  1. (DONE) Add a flag -enable-cxx-interop to the frontend, and have it enable (Objective-)C++ mode in the Clang importer.
  2. 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 of extern "C" annotations throughout it's headers---those fixes can be committed to the testsuite behind appropriate #ifdefs.
  3. Import C++ namespaces as Swift enums that have no cases (so-called "uninhabited enums") so the C++ lexical structure is represented in Swift
  4. Import C++ operators as Swift functions with the same operator name.
  5. (IN PROGRESS: apple/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
  6. Import C++ constructors as Swift initializers, so we can create instances of C++ types in Swift
  7. 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
  8. 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.
  9. 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 the this pointer.
  10. Import polymorphic C++ classes as Swift "foreign" classes, import the destructor as deinit, add support for calling virtual C++ methods, and having reference counting for these foreign classes be a no-op; introduce some explicit delete operation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment