Skip to content

Commit 9d444ae

Browse files
authored
[docs] stdlib prog-man: Document @_silgen_name use (#12125)
Add a description of `@_silgen_name` to the standard library programmer's manual.
1 parent 901cbde commit 9d444ae

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

docs/StandardLibraryProgrammersManual.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ TODO: Should this subsume or link to [AccessControlInStdlib.rst](https://github.
2727
1. Resilience, ABI stability, `@_inlineable`, `@_versioned`, etc
2828
1. Strings and ICU
2929
1. Lifetimes
30-
1. withExtendedLifetime, withUnsafe...,
30+
1. withExtendedLifetime, withUnsafe...,
31+
1. Shims and stubs
3132
1. Coding Standards
3233
1. High level concerns
3334
1. Best practices
3435
1. Formatting
3536
1. Internals
3637
1. `@inline(__always)` and `@inline(never)`
3738
1. `@semantics(...)`
38-
1. `@_silgen_name`
3939
1. Builtins
4040
1. Builtin.addressof, _isUnique, _isUniqueOrPinned, etc
4141
1. Dirty hacks
@@ -125,6 +125,35 @@ Use of this attribute imposes limitations on what can be in the body. For more d
125125

126126
This is currently used in the standard library as an additional annotation applied to @objc protocols signifying that any objects which conform to this protocol are not tagged. This means that (on Darwin platforms) such references, unlike AnyObject, have spare bits available from things like restricted memory spaces or alignment.
127127

128+
#### `@_silgen_name`
129+
130+
This attribute specifies the name that a declaration will have at link time. It is used for two purposes, the second of which is currently considered bad practice and should be replaced with shims:
131+
132+
1. To specify the symbol name of a Swift function so that it can be called from Swift-aware C. Such functions have bodies.
133+
2. To provide a Swift declaration which really represents a C declaration. Such functions do not have bodies.
134+
135+
##### Using `@_silgen_name` to call Swift from Swift-aware C
136+
137+
Rather than hard-code Swift mangling into C code, `@_silgen_name` is used to provide a stable and known symbol name for linking. Note that C code still must understand and use the Swift calling convention (available in swift-clang) for such Swift functions (if they use Swift's CC). Example:
138+
139+
```swift
140+
@_silgen_name("_swift_stdlib_destroyTLS")
141+
internal func _destroyTLS(_ ptr: UnsafeMutableRawPointer?) {
142+
// ... implementation ...
143+
}
144+
```
145+
146+
```C++
147+
__attribute__((__swiftcall__)) extern "C" void _swift_stdlib_destroyTLS(void *);
148+
149+
// ... C code can now call _swift_stdlib_destroyTLS on a void * ...
150+
```
151+
152+
##### Using `@_silgen_name` to call C from Swift
153+
154+
The standard library cannot import the Darwin module (much less an ICU module), yet it needs access to these C functions that it otherwise wouldn't have a decl for. For that, we use shims. But, `@_silgen_name` can also be used on a body-less Swift function declaration to denote that it's an external C function whose symbol will be available at link time, even if not available at compile time. This usage is discouraged.
155+
156+
128157
### Internal structures
129158
130159
#### `_FixedArray`

0 commit comments

Comments
 (0)