You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/CppInteroperability/SwiftTypeRepresentationInC++.md
+20-14Lines changed: 20 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ C++.
8
8
9
9
### Value Types
10
10
11
-
1) Primitive Swift types like `Int`, `Float` , `OpaquePointer`, `UnsafePointer<int>?` are mapped to primitive C++ types. `int` , `float`, `void *`, int `* _Nullable`.
11
+
1) Primitive Swift types like `Int`, `Float` , `OpaquePointer`, `UnsafePointer<int>?` are mapped to primitive C++ types. `int` , `float`, `void *`, `int * _Nullable`.
12
12
13
13
* Debug info: Does C++ debug info suffices?
14
14
@@ -29,13 +29,23 @@ class swift::String {
29
29
```c++
30
30
class Foundation::URL {
31
31
...
32
-
union {
33
-
alignas(8) char buffer[8]; // Swift value is stored here.
34
-
void *resilientPtr;
35
-
};
32
+
uintptr_t pointer; // pointer has alignment to compute buffer offset?
33
+
alignas(N) char buffer[M]; // Swift value is stored here.
36
34
};
37
35
```
38
36
37
+
concrete examples:
38
+
39
+
```c++
40
+
// representation for buffer aligned at 8:
41
+
{/*pointer=*/0x3, ....}; // buffer is alignas(2^3 = 8)
42
+
43
+
// representation for buffer aligned at 16:
44
+
{/*pointer=*/0x4, ....}; // buffer is alignas(2^4 = 16)
45
+
46
+
// where pointer < 10 for inline stores.
47
+
```
48
+
39
49
* Debug info: ...
40
50
41
51
@@ -44,10 +54,8 @@ class Foundation::URL {
44
54
```c++
45
55
class CryptoKit::SHA256 {
46
56
...
47
-
union {
48
-
alignas(8) char buffer[8];
49
-
void *resilientPtr; // Swift value is stored on the heap pointed by this pointer.
50
-
};
57
+
uintptr_t pointer; // Swift value is stored on the heap pointed by this pointer.
58
+
alignas(8) char buffer[8];
51
59
};
52
60
```
53
61
@@ -66,15 +74,13 @@ class swift::Array<swift::Int> {
66
74
* Debug info: ...
67
75
68
76
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.:
77
+
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
78
71
79
```c++
72
80
class swift::Optional<CryptoKit::SHA256> {
73
81
...
74
-
union {
75
-
alignas(8) char buffer[8];
76
-
void *resilientPtr; // Swift value is stored on the heap pointed by this pointer.
77
-
};
82
+
uintptr_t pointer; // Swift value is stored on the heap pointed by this pointer.
0 commit comments