Skip to content

Commit ef164a8

Browse files
committed
Document @PointerBounds and PointerParam
1 parent 57ef618 commit ef164a8

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

stdlib/public/PointerBounds/PointerBounds.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,46 @@
11
import Swift
22

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.
38
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.
414
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.
520
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`.
625
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.
730
case nonescaping(pointer: Int)
831
}
932

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.
1044
@attached(peer, names: overloaded)
1145
public macro PointerBounds(_ paramInfo: PointerParam...) =
1246
#externalMacro(module: "SwiftMacros", type: "PointerBoundsMacro")

0 commit comments

Comments
 (0)