docs/CppInteroperability/SwiftTypeRepresentationInC++.md
This document describes in details how Swift types are represented in C++. It also covers related topics, like debug info representation for Swift types in C++.
Int, Float , OpaquePointer, UnsafePointer<int>? are mapped to primitive C++ types. int , float, void *, int * _Nullable.String is mapped to a C++ class that stores the value in opaque buffer inline, e.g.:class swift::String {
...
alignas(8) char buffer[24]; // Swift value is stored here.
}
URL is mapped to a C++ class that stores the value in opaque buffer inline, e.g.:class Foundation::URL {
...
uintptr_t pointer; // pointer has alignment to compute buffer offset?
alignas(N) char buffer[M]; // Swift value is stored here.
};
concrete examples:
// representation for buffer aligned at 8:
{/*pointer=*/0x3, ....}; // buffer is alignas(2^3 = 8)
// representation for buffer aligned at 16:
{/*pointer=*/0x4, ....}; // buffer is alignas(2^4 = 16)
// where pointer < 10 for inline stores.
SHA256 is mapped to a C++ class that stores the value boxed up on the heap, e.g.:class CryptoKit::SHA256 {
...
uintptr_t pointer; // Swift value is stored on the heap pointed by this pointer.
alignas(8) char buffer[8];
};
Array<Int>, String?, is mapped to a C++ class that stores the value in opaque buffer inline, e.g.:class swift::Array<swift::Int> {
...
alignas(8) char buffer[8]; // Swift value is stored here.
}
SHA256?, is mapped to a C++ class that stores the value boxed up on the heap, e.g.:class swift::Optional<CryptoKit::SHA256> {
...
uintptr_t pointer; // Swift value is stored on the heap pointed by this pointer.
alignas(N) char buffer[M];
}
Class type is mapped to a C++ class that has a pointer to the underlying Swift instance in the base class:
class BaseClass {
private:
void *_opaquePointer; // Swift class instance pointer is stored here.
};
class Vehicle: public BaseClass {
public:
}
Error type is mapped to a specific C++ swift::Error class that stores the pointer to the error:
class Error {
private:
void *_opaquePointer; // Swift error instance pointer is stored here.:
};
Existential type. e.g. any Hashable maps to a C++ class that stores the opaque existential value (swift::any<swift::Hashable>):
class swift::OpaqueExistential {
// approximate layout.
alignas(8) char buffer[8*5]; // opaque existential is stored here (inline or boxed by Swift)
};
class swift::any<swift::Hashable>: public swift::OpaqueExistential {
};