Skip to content

Commit 5850cfd

Browse files
Xazax-hunGabor Horvath
andauthored
[cxx-interop] Document the calling conventions for C++ Shared Reference Types (#748)
This is interesting for reverse interop where the Swift functions take or return foreign reference types. Co-authored-by: Gabor Horvath <[email protected]>
1 parent 467b772 commit 5850cfd

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

documentation/cxx-interop/index.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,23 @@ object.doSomething()
12491249
// `object` will be released here.
12501250
```
12511251

1252+
#### Exposing C++ Shared Reference Types back from Swift
1253+
1254+
C++ can call into Swift APIs that take or return C++ Shared Reference Types. Objects of these types are always created on the C++ side,
1255+
but their references can be passed back and forth between Swift and C++. This section explains the conventions of incrementing and decrementing
1256+
the reference counts when passing such references across the language boundaries. Consider the following Swift APIs:
1257+
1258+
```swift
1259+
public func takeSharedObject(_ x : SharedObject) { ... }
1260+
1261+
public func returnSharedObject() -> SharedObject { ... }
1262+
```
1263+
1264+
In case of the `takeSharedObject` function, the compiler will automatically insert calls to retain and release for `x` as necessary to satisfy the semantics of
1265+
owned/guaranteed calling conventions. The C++ callers must guarantee that `x` is alive for the duration of the call.
1266+
Note that functions returning a shared reference type such as `returnSharedObject` transfer the ownership to the caller.
1267+
The C++ caller of this function is responsible for releasing the object.
1268+
12521269
### Unsafe Reference Types
12531270

12541271
The `SWIFT_UNSAFE_REFERENCE` annotation macro has the same effect as `SWIFT_IMMORTAL_REFERENCE`

0 commit comments

Comments
 (0)