Relating C++ Interoperability to Former Objective-C interoperability - (Providing Swift overlays for C++ standard library types GSoC)

Hello my name is Isabell and I'm interested in getting involved with a C++ interoperability project through GSoC-2022, specifically Providing Swift overlays for C++ standard library types. I've never really done work in interoperability so I've been trying to learn more about it. It was suggested to me that in order to understand a lot of the underpinnings of the C++ interoperability that are taking place in the interoperability projects, such as Providing Swift overlays for C++ standard library types, that I should learn more about the interoperability between Objective-C and Swift currently. Where should I start when it comes to finding material on understanding the interoperability that is already taking place between Objective-C and Swift?

In terms of the project Providing Swift overlays for C++ standard library types, what would be the natural progression of this type of interoperability project?

I feel like I have a solid understanding of C++, I'm not an expert by any means but I feel like I understand it well enough to expand on anything that I may need to know for this type of project. What fundamental knowledge is needed to really grasp this project from the C++ side of things?

I have also been in the process of learning more about Swift from the practical side. I've never coded in Swift before. What should I be focusing on in terms of the Swift side of things?

@egor.zhdan @Alex_L

3 Likes

Hi Isabell, thanks for reaching out! Glad to see you're interested in the project.

I think this is really good advice, a lot of the interoperability logic is indeed shared between Swift/Obj-C and Swift/C++. The compiler component that handles this is called ClangImporter and you can find its sources here: swift/lib/ClangImporter at main · apple/swift · GitHub.
I would say ImportDecl.cpp is a good starting point if you would like to learn more about the interoperability logic. It contains a bunch of Visit*** methods that generate the Swift declarations that represent underlying ObjC/C++ declarations.

Here are some more resources to learn more about Swift/C++ interop:

Here are some steps to create an overlay module for the C++ stdlib:

  • Extending the CMake build scripts for the Swift compiler to incorporate the overlay module. That module would need to be built & shipped with Swift toolchains. This might take some time, but I can provide help with that.
  • Implementing the Swift extensions for C++ types. Here you would need to consider possible usages of C++ APIs from Swift, and think about making the bridging ergonomic, correct & fast. We've mentioned some C++ types in the project description that would benefit from being extended in Swift (std::string, std::map, std::set, std::vector), but this list is not exclusive, and if you have other types in mind, that would be very interesting (and it would make sense to mention such types in the proposal).
  • An interesting aspect of this is that different platforms use different C++ standard libraries (libc++ on Apple platforms, libstdc++ on Linux, Microsoft standard library on Windows). We are focusing on libc++ for now, and we don't necessarily require the participant to make the overlay work for non-Apple platforms, although we are eventually going to support those as well, and if some of that work can be done as part of this project, that would be a plus.
  • Writing tests for the new functionality to make sure it doesn't regress in the future.

We don't require advanced C++ knowledge. Having some understanding of how common C++ standard library types are used and where to look for documentation should be enough for this project.

Once you feel comfortable reading Swift code, I would recommend to take a look at the Swift standard library types like Array & Dictionary and the protocols they implement (e.g. BidirectionalCollection, Sequence, etc). As a part of this project, you would need to implement conformances to some of these protocols for C++ standard library types, so it would be helpful to have a basic knowledge of these protocols and the features that they enable (e.g. iteration over a collection with a for item in colection {...} loop).

If you have any other questions, feel free to ask!

hello, I am umesh from 1 year of collage in computer science and I know c++ and I am searching for a project to work in can I work in this project.
And I can learn whatever the project need to finish it, it would be very helpful if you can guide me throw what I need to do.

And I can do any project related programing.

Thank you for the response @egor.zhdan I will take a closer look at the links you posted and start to get more familiar with dictionaries and arrays in the Swift language.

Real quick question @egor.zhdan @Alex_L. I found this other resource which discusses a lot of the primitive type conversion between C API's and Swift (swift/HowSwiftImportsCAPIs.md at main · apple/swift · GitHub).

Is the outcome of this project based on expanding this document with the data structures discussed in the project description for Providing Swift overlays for C++ standard library types?

That document mostly describes how the Swift compiler maps various language constructs between C and Swift. For this project, you likely wouldn't need to do any significant modifications to that logic, instead, you would create a new Swift overlay module that imports the C++ stdlib module, and adds some conformances to C++ types, e.g.

// Swift overlay:

import std // Clang module with the C++ stdlib

extension std.string : Collection { ... }
extension std.vector : ... { ... }

This would mean that when the developer types import std in a Swift source file, the Swift compiler would automatically import the overlay module along with the C++ stdlib.

Notice that we don't want to import std.string as Swift's String (std.string allows invalid UTF8, String doesn't), or std.vector as Swift's Array (that would decrease performance by doing extra copies), so the type conversion logic won't necessarily have to change.

1 Like

Thank you for the response @egor.zhdan. So this would mean that we would be working on creating the syntax for bridging to data structures with the Swift values being discussed in the document? The conformance in the case would be the ability for a library such as std.string to be used with different data types in Swift? (I'm not quite sure if I'm thinking about this correctly.)

Not quite: the Swift compiler is already able to import C++ classes like std::vector or std::string, so the following code compiles (with a very recent version of Swift):

// swift -enable-experimental-cxx-interop foo.swift
import std

let s = std.vector<CInt>()
s.push_back(1)
s.push_back(2)
s.push_back(3)

for i in 0..<s.size() {
    print(s[i])
}

The problem with the current way of bridging C++ stdlib types is that it is not very ergonomic in Swift. For example. a developer might want to iterate over a std::vector with a for-in loop, or print a std::string to the console. These features are already implemented for the Swift standard library types, so you wouldn't need to add new syntax to the Swift language, or change the existing syntax. The goal of this project would be to bring the support for these existing Swift language features to C++ standard library types which don't currently support these features.

By conformance I meant "struct X implements the requirements of protocol P", in this case, std.string conformance to Swift's Collection protocol, which would, for example, make this code valid:

func foo(x: std.string) {
  for char in x { // <-- iterating over a std.string with a for-in loop
    // ...
  }
}

Swift allows doing many of these usability improvements by adding extensions to existing types, without having to modify the sources of the types or alter the syntax of the language.

@egor.zhdan The overlay module would be the Swift module that provides all the extensions you mentioned in the second point? Which means this project will mostly (if not completely) be written in Swift since the part that bridges types is already done?

Yes, this is correct, the overlay module would be implemented in Swift, similarly to the existing overlays for Glibc on Linux and CRT on Windows.

We don't expect a lot of C++ coding to be required for this project, although there might be a need to read some C++ sources of the ClangImporter component of the Swift compiler (see the links in my message earlier in this thread) to better understand the logic of bridging different C++ language constructs into Swift.

Okay thank you @egor.zhdan this makes sense. Would a good place to look for the protocols you are looking to create C++ conformances fall under the Conforms to section in Swift documentations?

Yes, that is one way to learn more about the Swift standard library protocols. Another way would be to try using the stdlib collection types in a Swift playground in Xcode: you can access the same documentation for the stdlib types & the protocols they conform to in the Quick Help view in Xcode.