Skip to content

Commit 035bee6

Browse files
authored
Merge pull request swiftlang#60466 from hyp/eng/opaquelayoutdocs++
[interop][SwiftToCxx] update swift type representation docs with diff…
2 parents 2baa1e4 + 74d47a1 commit 035bee6

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

docs/CppInteroperability/SwiftTypeRepresentationInC++.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ C++.
88

99
### Value Types
1010

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`.
1212

1313
* Debug info: Does C++ debug info suffices?
1414

@@ -29,13 +29,23 @@ class swift::String {
2929
```c++
3030
class Foundation::URL {
3131
...
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.
3634
};
3735
```
3836

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+
3949
* Debug info: ...
4050
4151
@@ -44,10 +54,8 @@ class Foundation::URL {
4454
```c++
4555
class CryptoKit::SHA256 {
4656
...
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];
5159
};
5260
```
5361

@@ -66,15 +74,13 @@ class swift::Array<swift::Int> {
6674
* Debug info: ...
6775
6876
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.:
7078
7179
```c++
7280
class swift::Optional<CryptoKit::SHA256> {
7381
...
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.
83+
alignas(N) char buffer[M];
7884
}
7985
```
8086

0 commit comments

Comments
 (0)