|
1 | 1 | import Swift
|
2 | 2 |
|
| 3 | +/// Different ways to annotate pointer parameters using the `@PointerBounds` macro. |
| 4 | +/// All indices into parameter lists start at 1. Indices __must__ be integer literals, and strings |
| 5 | +/// __must__ be string literals, because their contents are parsed by the `@PointerBounds` macro. |
| 6 | +/// Only 1 instance of `countedBy`, `sizedBy` or `endedBy` can refer to each pointer index, however |
| 7 | +/// `nonescaping` is orthogonal to the rest and can (and should) overlap with other annotations. |
3 | 8 | public enum PointerParam {
|
| 9 | + /// Corresponds to the C `__counted_by(count)` attribute. |
| 10 | + /// Parameter pointer: index of pointer in function parameter list. Must be of type |
| 11 | + /// `Unsafe[Mutable]Pointer<T>[?]`, i.e. not an `UnsafeRawPointer`. |
| 12 | + /// Parameter count: string containing valid Swift syntax containing the number of elements in |
| 13 | + /// the buffer. |
4 | 14 | case countedBy(pointer: Int, count: String)
|
| 15 | + /// Corresponds to the C `__sized_by(size)` attribute. |
| 16 | + /// Parameter pointer: index of pointer in function parameter list. Must be of type |
| 17 | + /// `Unsafe[Mutable]RawPointer[?]`, i.e. not an `UnsafePointer<T>`. |
| 18 | + /// Parameter count: string containing valid Swift syntax containing the size of the buffer, |
| 19 | + /// in bytes. |
5 | 20 | case sizedBy(pointer: Int, size: String)
|
| 21 | + /// Corresponds to the C `__ended_by(end)` attribute. |
| 22 | + /// Parameter start: index of pointer in function parameter list. |
| 23 | + /// Parameter end: index of pointer in function parameter list, pointing one past the end of |
| 24 | + /// the same buffer as `start`. |
6 | 25 | case endedBy(start: Int, end: Int)
|
| 26 | + /// Corresponds to the C `noescape` attribute. Allows generated wrapper to use `Span`-types |
| 27 | + /// instead of `UnsafeBuffer`-types, because it is known that the function doesn't capture the |
| 28 | + /// object past the lifetime of the function. |
| 29 | + /// Parameter pointer: index of pointer in function parameter list. |
7 | 30 | case nonescaping(pointer: Int)
|
8 | 31 | }
|
9 | 32 |
|
| 33 | +/// Generates a bounds safe wrapper for function with Unsafe[Mutable][Raw]Pointer[?] arguments. |
| 34 | +/// Intended to be automatically attached to function declarations imported by ClangImporter. |
| 35 | +/// The wrapper function will replace Unsafe[Mutable][Raw]Pointer[?] parameters with |
| 36 | +/// [Mutable][Raw]Span[?] or Unsafe[Mutable][Raw]BufferPointer[?] if they have bounds information |
| 37 | +/// attached. Where possible "count" parameters will be elided from the wrapper signature, instead |
| 38 | +/// fetching the count from the buffer pointer. In these cases the bounds check is also skipped. |
| 39 | +/// |
| 40 | +/// Currently not supported: return pointers, nested pointers, pointee "count" parameters, endedBy. |
| 41 | +/// |
| 42 | +/// Parameter paramInfo: information about how the function uses the pointer passed to it. The |
| 43 | +/// safety of the generated wrapper function depends on this info being extensive and accurate. |
10 | 44 | @attached(peer, names: overloaded)
|
11 | 45 | public macro PointerBounds(_ paramInfo: PointerParam...) =
|
12 | 46 | #externalMacro(module: "SwiftMacros", type: "PointerBoundsMacro")
|
|
0 commit comments