Skip to content

Commit 073633f

Browse files
committed
corrected positioning and non-const confusion
1 parent 21f224f commit 073633f

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

documentation/cxx-interop/index.md

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,33 +1255,10 @@ object.doSomething()
12551255
// `object` will be released here.
12561256
```
12571257

1258-
### Inheritance and Virtual Member Functions
1259-
1260-
Similar to value types, casting an instance of a derived reference type to a
1261-
base reference type, or vice versa, is not yet supported by Swift.
1262-
1263-
If a reference type has virtual methods, you can call those methods from Swift.
1264-
This includes pure virtual methods.
1265-
1266-
#### Exposing C++ Shared Reference Types back from Swift
1267-
1268-
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,
1269-
but their references can be passed back and forth between Swift and C++. This section explains the conventions of incrementing and decrementing
1270-
the reference counts when passing such references across the language boundaries. Consider the following Swift APIs:
1271-
1272-
```swift
1273-
public func takeSharedObject(_ x : SharedObject) { ... }
1274-
1275-
public func returnSharedObject() -> SharedObject { ... }
1276-
```
1277-
1278-
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
1279-
owned/guaranteed calling conventions. The C++ callers must guarantee that `x` is alive for the duration of the call.
1280-
Note that functions returning a shared reference type such as `returnSharedObject` transfer the ownership to the caller.
1281-
The C++ caller of this function is responsible for releasing the object.
1282-
1283-
If a C++ shared reference type is passed as an non-const argument to a C++ API from Swift, the Swift compiler guarantees that the passed value would be alive, and retains the ownership of the value. In other words, the argument is passed as +0 and there is no transfer of ownership.
1258+
### Calling conventions when passing shared reference types from Swift to C++
1259+
If a C++ shared reference type is passed as an argument to a C++ API from Swift, the Swift compiler guarantees that the passed value would be alive, and retains the ownership of the value. In other words, the argument is passed as +0 and there is no transfer of ownership.
12841260
The C++ function is responsible for ensuring that the value pointed to by the parameter is alive during and at the end of the call. The C++ function should not assume it has ownership of the value and should do necessary retain operations if it is needs to take ownership.
1261+
12851262
If the argument is an inout (non-const reference) as shown below:
12861263
```c++
12871264
void takeSharedObjectAsInout(SharedObject *& x) { ... }
@@ -1306,6 +1283,31 @@ void receiveSharedObject(SharedObject *sobj) {
13061283
}
13071284
```
13081285
1286+
### Inheritance and Virtual Member Functions
1287+
1288+
Similar to value types, casting an instance of a derived reference type to a
1289+
base reference type, or vice versa, is not yet supported by Swift.
1290+
1291+
If a reference type has virtual methods, you can call those methods from Swift.
1292+
This includes pure virtual methods.
1293+
1294+
### Exposing C++ Shared Reference Types back from Swift
1295+
1296+
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,
1297+
but their references can be passed back and forth between Swift and C++. This section explains the conventions of incrementing and decrementing
1298+
the reference counts when passing such references across the language boundaries. Consider the following Swift APIs:
1299+
1300+
```swift
1301+
public func takeSharedObject(_ x : SharedObject) { ... }
1302+
1303+
public func returnSharedObject() -> SharedObject { ... }
1304+
```
1305+
1306+
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
1307+
owned/guaranteed calling conventions. The C++ callers must guarantee that `x` is alive for the duration of the call.
1308+
Note that functions returning a shared reference type such as `returnSharedObject` transfer the ownership to the caller.
1309+
The C++ caller of this function is responsible for releasing the object.
1310+
13091311
### Unsafe Reference Types
13101312

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

0 commit comments

Comments
 (0)