|
| 1 | +# Swift Type Representation In C++ |
| 2 | + |
| 3 | +This document describes in details how Swift types are represented in C++. |
| 4 | +It also covers related topics, like debug info representation for Swift types in |
| 5 | +C++. |
| 6 | + |
| 7 | +## Type Categories |
| 8 | + |
| 9 | +### Value Types |
| 10 | + |
| 11 | +1) Primitive Swift types like `Int`, `Float` , `OpaquePointer`, `UnsafePointer<int>?` are mapped to primitive C++ types. `int` , `float`, `void *`, int `* _Nullable` . |
| 12 | + |
| 13 | +* Debug info: Does C++ debug info suffices? |
| 14 | + |
| 15 | +2) Non-resilient fixed-layout Swift value type, e.g. `String` is mapped to a C++ class that stores the value in opaque buffer inline, e.g.: |
| 16 | + |
| 17 | +```c++ |
| 18 | +class swift::String { |
| 19 | + ... |
| 20 | + alignas(8) char buffer[24]; // Swift value is stored here. |
| 21 | +} |
| 22 | +``` |
| 23 | +
|
| 24 | +* Debug info: ... |
| 25 | +
|
| 26 | +
|
| 27 | +3) Resilient (or opaque layout) inline-allocated Swift value type small enough to fit into inline buffer. e.g `URL` is mapped to a C++ class that stores the value in opaque buffer inline, e.g.: |
| 28 | +
|
| 29 | +```c++ |
| 30 | +class Foundation::URL { |
| 31 | + ... |
| 32 | + union { |
| 33 | + alignas(8) char buffer[8]; // Swift value is stored here. |
| 34 | + void *resilientPtr; |
| 35 | + }; |
| 36 | +}; |
| 37 | +``` |
| 38 | + |
| 39 | +* Debug info: ... |
| 40 | + |
| 41 | + |
| 42 | +4) Resilient (or opaque layout) boxed Swift value , e.g. `SHA256` is mapped to a C++ class that stores the value boxed up on the heap, e.g.: |
| 43 | + |
| 44 | +```c++ |
| 45 | +class CryptoKit::SHA256 { |
| 46 | + ... |
| 47 | + union { |
| 48 | + alignas(8) char buffer[8]; |
| 49 | + void *resilientPtr; // Swift value is stored on the heap pointed by this pointer. |
| 50 | + }; |
| 51 | +}; |
| 52 | +``` |
| 53 | +
|
| 54 | +* Debug info: ... |
| 55 | +
|
| 56 | +
|
| 57 | +5) Generic non-resilient fixed-layout Swift value type, e.g. `Array<Int>`, `String?`, is mapped to a C++ class that stores the value in opaque buffer inline, e.g.: |
| 58 | +
|
| 59 | +```c++ |
| 60 | +class swift::Array<swift::Int> { |
| 61 | + ... |
| 62 | + alignas(8) char buffer[8]; // Swift value is stored here. |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +* Debug info: ... |
| 67 | + |
| 68 | + |
| 69 | +6) Generic opaque-layout / resilient / opaque-layout template type params Swift value type, e.g. SHA256`?`, is mapped to a C++ class that stores the value boxed up on the heap, e.g.: |
| 70 | + |
| 71 | +```c++ |
| 72 | +class swift::Optional<CryptoKit::SHA256> { |
| 73 | + ... |
| 74 | + union { |
| 75 | + alignas(8) char buffer[8]; |
| 76 | + void *resilientPtr; // Swift value is stored on the heap pointed by this pointer. |
| 77 | + }; |
| 78 | +} |
| 79 | +``` |
| 80 | +
|
| 81 | +* Debug info: ... |
| 82 | +
|
| 83 | +### Class Types |
| 84 | +
|
| 85 | +Class type is mapped to a C++ class that has a pointer to the underlying Swift instance in the base class: |
| 86 | +
|
| 87 | +```c++ |
| 88 | +class BaseClass { |
| 89 | +private: |
| 90 | + void *_opaquePointer; // Swift class instance pointer is stored here. |
| 91 | +}; |
| 92 | +class Vehicle: public BaseClass { |
| 93 | +public: |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +* Debug info: ... |
| 98 | + |
| 99 | +### Existential Types |
| 100 | + |
| 101 | +Error type is mapped to a specific C++ `swift::Error` class that stores the pointer to the error: |
| 102 | + |
| 103 | +```c++ |
| 104 | +class Error { |
| 105 | +private: |
| 106 | + void *_opaquePointer; // Swift error instance pointer is stored here.: |
| 107 | +}; |
| 108 | +``` |
| 109 | +
|
| 110 | +* Debug info: ... |
| 111 | +
|
| 112 | +
|
| 113 | +Existential type. e.g. `any Hashable` maps to a C++ class that stores the opaque existential value (`swift::any<swift::Hashable>`): |
| 114 | +
|
| 115 | +```c++ |
| 116 | +class swift::OpaqueExistential { |
| 117 | + // approximate layout. |
| 118 | + alignas(8) char buffer[8*5]; // opaque existential is stored here (inline or boxed by Swift) |
| 119 | +}; |
| 120 | +class swift::any<swift::Hashable>: public swift::OpaqueExistential { |
| 121 | +}; |
| 122 | +``` |
| 123 | + |
| 124 | +* Debug info: ... |
0 commit comments