I think the main reason is so that multiple COM interfaces can be implemented on the same Swift type, which isn't possible with single inheritance from a class. C++ relies on multiple inheritance to do this, but protocol import for pure virtual classes is a more conservative solution. My thinking is that although C++ interop is a major goal for the language, the LSG might be hesitant to allow full multiple inheritance in Swift (though correct me if I'm wrong about that). It is quite uncommon for multiple inheritance to be a strict requirement in C++ outside of interfaces, so I think this achieves most of what we want.
It's worth noting here that, as pitched, I believe this is intended to work for 32-bit programs as well as 64-bit programs, hence the calling convention being an issue still.
I don't disagree that it would be useful if the C++ interop code imported virtual base classes as protocols, though I think it's slightly trickier to do that overall, since whether a virtual base class is being used as an interface type or not is at least in part up to the programmer (it probably is if it's entirely pure, but maybe some methods aren't pure and instead have a default implementation of something, or maybe there are non-virtual methods, and so on…). It doesn't sound like a trivial problem trying to identify exactly which things should be protocols and which should be classes. @egor.zhdan might have further comments about that also.
The point though is that most of the code changes in the compiler are things you would ideally want for C++ interop also; the previous objection that there are too many compiler changes and it could all be done in a library has, I think, missed that point slightly.
You can look at this two different ways, I think:
On the one hand, Swift doesn't support multiple inheritance, but you definitely want to be able to have types conform to multiple protocols (or implement multiple interfaces, in COM speak).
On the other, Swift protocols are the logical equivalent of interface types from other languages, or indeed from COM — they specify an interface without specifying the implementation or any details of data storage.
The calling convention still passes this through a register on 64-bit platforms, so it is implicit. As @al45tair mentioned, that there are still ABI differences. DirectX actually has instances of this happening - the C++ ABI is not COM compatible, and a missed C ABI conformance does break interoperability.
FWIW, this is primarily what the Sema related changes are shaped after - validating the inheritance chain. There is some minor other items (e.g. parameters must be C ABI compatible for COM method, COM runtime module is complete, and with the Microsoft model, the IID/CLSID rules are uphelp properly).
It will work on 32-bit programs since __stdcall is included in the code blocks I have shown.
win32metadata explicitly flags these exact methods in DirectX, and I would generate different code for them that matches the headers in the Windows SDK. My point is that whatever I do will match what Microsoft does in its headers which are used in C and C++ development.
What classes are interfaces is indeed based on programmer intent, so I think a Clang attribute applied to the C++ class/struct giving this information would be most appropriate to avoid determining this based on heuristics. Once this attribute is applied, the compiler can check whether it contains any requirements that aren't representable in a protocol and emit an error if that check fails.
I agree the compiler changes are quite similar between a C++ interop strategy and a COM interop strategy, but I was mainly advocating for less compiler work in relation to WinMD reading and projection of C, COM, and WinRT APIs in the compiler, which would be a larger scope increase. That is the part I think can all be done in a library. Expanding C++ interop is primarily a way to generalise these compiler changes so they aren't specific to COM and Windows and (from a design and separation of concerns perspective) focus on ABI primitives for projection generators instead of supporting arbitrary COM interface declarations in Swift, which I have reasoned would not gain much adoption. The lack of those relatively minor other items (such as the COM runtime module in the toolchain) in a C++ interop strategy is not because they pose a major implementation risk but rather to pursue a compiler focusing on generalised language semantics that benefit as many people as possible and keeping platform-specific details (COM) out of the toolchain. It also saves us from solving the same problems again later for C++ interop.
Sure, I'm not saying that the interfaces cannot be interoperable with C++. Microsoft has spent a good deal of effort to try to make that work, but even then, it does break down in edge cases, e.g. -fsigned-char (the default on Windows) is technically relying on UB for C++ as char and signed char are technically distinct in C++ mode.
The point is, COM is most correctly represented in C's model, and we should be using C interop for this. Once you do that, you need interfaces, i.e. protocol types, and it is simply reality that the compiler needs to step in and help. Having a small surface that the compiler supports for this is not a bad thing. It does increase the barrier to entry to implement that, but that isn't insurmountable, and in fact, we have nearly a working implementation (at least conceptually with a PoC branch).
The char edge case is relevant to C++ function overload resolution and name mangling, but COM interfaces do not link methods by mangled C++ symbol names, they use fixed vtable indices. Furthermore, I don't rely on ambiguous char types as win32metadata resolves primitives to explicit fixed-width types (Int8 / int8_t, UInt8 / uint8_t, UInt16 / wchar_t), and there is zero undefined behavior when passing fixed-width integers across a virtual method boundary.
Virtually all of Microsoft's own internal code, games, and native Windows apps consume COM via C++ (using WRL, WIL, or C++/WinRT). If consuming COM in C++ were fundamentally broken in MSVC or Clang, Windows itself wouldn't compile.
Those DirectX edge case methods do need some C-style code generation, but I can easily emit that inside the same header with other C++ declarations. Solely using C interop throws away Clang's built-in knowledge of C++ vtables and inheritance for the rest of the 99% of Windows APIs, and has to reconstruct it specifically for COM. The ability to leverage both C and C++ interop in a projection generator is a strength, not a weakness. Clang attributes I have discussed for C++ interop such as SWIFT_SHARED_REFERENCE are also usable in pure C as it accepts C functions for the reference count operations, so most of my codegen will remain the same.
The compiler support is indeed a good thing, which is why I think we should generalise it for C++ interop on all platforms.
Whether char is signed is implementation defined behavior across both C and C++. Neither language guarantees either representation, and signed char and unsigned char are separate types in both. It's technically not undefined behavior to rely on char being signed, it's just a portability hazard.
Since we can't add the Clang attribute to Microsoft's headers, that means using API Notes (which, as I pointed out, could be generated from the winmd data; they don't need to be manually maintained).
That isn't part of this pitch, to be clear.
I happen to think it would be better for all kinds of reasons, and we already have the tools (API Notes and Swift overlays) to fix any problems with imported data, but it's not part of this vision.
I still think it's useful to have explicit support for COM per se on Windows. In order to make C++ interop a properly viable route for COM, we would need to:
Add annotations to identify interface types.
Add attributes to tell the compiler the reference counting rules for those interface types (COM reference counts interfaces, not objects; calling AddRef on one interface pointer does not necessarily increment the reference count for any other interface pointers you may have even if they were vended by the same object).
Add a way to map the various different COM-specific attributes to an IID property (there are C macros in at least two flavours, plus __declspec(uuid("{...}"), and these are COM specific).
Add a way to get C++ interop to generate appropriately laid-out vtables for COM.
Add a way to indicate to C++ interop that, because it's a COM object, it should be stdcall and not thiscall.
Add a way to tell the compiler to generate a QueryInterface function for you.
Somehow maintain API Notes to attach all of these attributes to definitions we pick up from header files.
There are a lot of COM-specific things in that list, and nothing about COM requires C++. Moreover, in an ideal world you should be able to use COM from Swift code without activating C++ interop. (Turning on C++ interop for a module means that any users of types or functions from that module also have to enable C++ interop; if we can do without it, that would be better.)
That's the viral aspect that Saleem also previously mentioned, and I find this invasive nature of Swift's C++ interop to be a bit of a pain in practice.
In my mixed C/C++/Swift Windows (CMake-based) project, I had to introduce a separate Swift static library target for some Windows COM interop code that required the C view of COM interfaces. Not a huge problem, but not nice either.
FWIW, I'm still a bit unsure about this. I see @al45tair's point about it, but I also agree with your point here (and is where I originally started from). I am concerned about the amount of specialised knowledge that would put into the compiler. This of course becomes less of a concern with the recent Swift in Swift effort (swift-winmd is written as a Swift module that we could pull in), but I'm not entirely sold on that belonging in the compiler as of yet.
I am going to be unavailable for a while, but I figured that I would drop a copy of my latest revision of the vision document to allow people to mull over it. I hope that the new version is clearer for people to follow. It is larger than what discourse allows to post, so I am sharing it as a gist:
I generate my own headers rather than annotating the Windows headers with API Notes for a few reasons:
The Windows headers are hostile to modularity and pollute the global namespace. Including a single Windows header in C and C++ often pulls in the entire Windows API surface from transitive includes, which makes IDE completions useless. Un-projected symbols and symbols that the projection does not need also would still be visible to the user if I was annotating the original Windows headers. And it is not easy to programmatically identify all of the unwanted symbols. Windows API projections for other languages generate code for what they need instead of trying to fix the Windows headers.
I want to make some other changes to the headers such as adding strongly typed handles (based on win32metadata) instead of aliases for void pointers.
I would like to split everything into organised modules (Networking, Graphics, Win32Graphics, etc.) like windows-rs which is not possible with API Notes.
There are many API refinements I would like to do that can't be expressed in API Notes, for example moving out pointers into the return type like windows-rs. I need to generate Swift wrappers for these.
We could add more features to API Notes to address this, but I think some of these refinements are too specific and complex to make sense as simple annotations. This is my most important feedback: whatever solution we go with, I hope the above refinements are possible.
This is true, and I think it would be useful for users of C++ interop as well.
Clang attributes for foreign reference counting handle that in my COMTest demo, in a similar way to the wil::com_ptr smart pointer from the Windows Implementation Libraries for C++ but without a smart pointer type.
The projection generator knows the UUIDs so I place them in an IID property on the Swift side instead of relying on the C macros or MSVC extensions (__declspec(uuid("{...}") and __uuidof).
C++ interop already does that because it uses the MSVC C++ ABI which the COM vtable layout was designed to match from the start. The COMTest demo shows this.
My headers include __stdcall on all the COM interface methods so this already works. Even if I used Microsoft's original Windows headers without modifications or annotations, they still include __stdcall and it would still work.
I don't need the compiler to generate a QueryInterface function for me, I wrote it myself in Swift. It has the signature queryInterface<T>(...) throws(COMError) -> T and can be seen in the COMTest demo here.
I do not use API Notes so this would not be needed in my current approach.
I've addressed the COM-specific points in my own code, so that just leaves additions to C++ interop which are widely useful and benefit all platforms. Nothing about COM requires C++, but it is based on the C++ object model, so we already have support for the core ABI for free. Relying on C++ would not be ideal if it were important for Swift users to define arbitrary COM interfaces in Swift, but I have reasoned this is not a common use case and consuming Windows APIs is the primary use case. For this, projection generators can write all the C++ boilerplate for us and hide it behind idiomatic Swift. And the compiler focuses on generalised language semantics that benefit as many users as possible.
The limitation that all C and C++ code must be built in the same translation unit in either C or C++ mode is a current pain point that other build systems are not subject to. The point I made earlier about this is that I think it would be best to solve this limitation, rather than sidestepping it by introducing an entirely new object model to the language, especially when that new object model is based on an ABI that is already supported.
Other languages like C# and Rust do not have interop for COM or C++ and simply go through C interop, which they still have managed to wrap and make idiomatic (with support from C#/WinRT, C#/Win32, and windows-rs). .NET Framework had COM interop support but modern .NET moved away from it in favour of user-space projections. I use C++ interop in Swift because it is the way that primitives I need are currently exposed, but the path taken by C# and Rust is also totally viable. There is a smaller set of language changes that would be needed to match Rust, including exposing foreign reference counting to Swift (or a copy hook like Clone in Rust or copy constructors in C++), providing calling convention attributes like Rust, and addressing some limitations on Swift protocols that prevent zero cost connection to COM vtables like the traits in windows-rs. That would save all of the C++ interop changes I have suggested. So most languages rely on C, C++ relies on C++, and Swift has support for both; I think it should be possible to achieve COM support without special integration for COM specifically. C#, C++ and Rust cover pretty much all of the modern usage of COM today.