Skip to content

Commit bf5eb48

Browse files
authored
Expand reference for macros. (#181)
Fixes: rdar://111068729
2 parents 8c644d4 + e9c8c64 commit bf5eb48

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

TSPL.docc/ReferenceManual/Attributes.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ and how it applies to a particular declaration.
2424
These *attribute arguments* are enclosed in parentheses,
2525
and their format is defined by the attribute they belong to.
2626

27+
Attached macros and property wrappers also use attribute syntax.
28+
For information about how macros expand,
29+
see <doc:Expressions#Macro-Expansion-Expression>.
30+
For information about property wrappers,
31+
see <doc:Attributes#propertyWrapper>.
32+
2733
## Declaration Attributes
2834

2935
You can apply a declaration attribute to declarations only.

TSPL.docc/ReferenceManual/Declarations.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3327,15 +3327,19 @@ macro <#name#> = <#macro implementation#>
33273327

33283328
The *macro implementation* is another macro,
33293329
and indicates the location of the code that performs this macro's expansion.
3330+
The code that performs macro expansion is a separate Swift program,
3331+
that uses the [SwiftSyntax][] module to interact with Swift code.
33303332
Call the `externalMacro(module:type:)` macro from the Swift standard library,
33313333
passing in the name of a type that contains the macro's implementation,
33323334
and the name of the module that contains that type.
33333335

3336+
[SwiftSyntax]: http://github.com/apple/swift-syntax/
3337+
33343338
Macros can be overloaded,
33353339
following the same model used by functions.
33363340
A macro declaration appears only at file scope.
33373341

3338-
For more information, see <doc:Macros>.
3342+
For an overview of macros in Swift, see <doc:Macros>.
33393343

33403344
> Grammar of a macro declaration:
33413345
>
@@ -3345,10 +3349,6 @@ For more information, see <doc:Macros>.
33453349
> *macro-function-signature-result***`->`** *type* \
33463350
> *macro-definition***`=`** *expression*
33473351
3348-
<!--
3349-
TODO TR: Confirm that the 'where' clause goes after the equals sign.
3350-
-->
3351-
33523352
## Operator Declaration
33533353

33543354
An *operator declaration* introduces a new infix, prefix,

TSPL.docc/ReferenceManual/Expressions.md

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,18 +1514,60 @@ Macro-expansion expressions have the following form:
15141514
<#macro name#>(<#macro argument 1#>, <#macro argument 2#>)
15151515
```
15161516

1517-
A macro-expansion expression omits the parentheses
1517+
A macro-expansion expression omits the parentheses after the macro's name
15181518
if the macro doesn't take any arguments.
15191519

1520-
A macro expression can't appear as the default value for a parameter,
1521-
except for the [`file()`][] and [`line()`][] macros from the Swift standard library.
1520+
A macro-expansion expression can't appear as the default value for a parameter,
1521+
except the [`file()`][] and [`line()`][] macros from the Swift standard library.
15221522
When used as the default value of a function or method parameter,
1523-
These macros' value is determined
1524-
when the default value expression is evaluated at the call site.
1523+
these macros are evaluated using the source code location of the call site,
1524+
not the location where they appear in a function definition.
15251525

15261526
[`file()`]: https://developer.apple.com/documentation/swift/file()
15271527
[`line()`]: https://developer.apple.com/documentation/swift/line()
15281528

1529+
You use macro expressions to call freestanding macros.
1530+
To call an attached macro,
1531+
use the custom attribute syntax described in <doc:Attributes>.
1532+
Both freestanding and attached macros expand as follows:
1533+
1534+
1. Swift parses the source code
1535+
to produce an abstract syntax tree (AST).
1536+
1537+
2. The macro implementation receives AST nodes as its input
1538+
and performs the transformations needed by that macro.
1539+
1540+
3. The transformed AST nodes that the macro implementation produced
1541+
are added to the original AST.
1542+
1543+
The expansion of each macro is independent and self-contained.
1544+
However, as a performance optimization,
1545+
Swift might start an external process that implements the macro
1546+
and reuse the same process to expand multiple macros.
1547+
When you implement a macro,
1548+
that code must not depend on what macros your code previously expanded,
1549+
or on any other external state like the current time.
1550+
1551+
For nested macros and attached macros that have multiple roles,
1552+
the expansion process repeats.
1553+
Nested macro-expansion expressions expand from the outside in.
1554+
For example, in the code below
1555+
`outerMacro(_:)` expands first and the unexpanded call to `innerMacro(_:)`
1556+
appears in the abstract syntax tree
1557+
that `outerMacro(_:)` receives as its input.
1558+
1559+
```swift
1560+
#outerMacro(12, #innerMacro(34), "some text")
1561+
```
1562+
1563+
An attached macro that has multiple roles expands once for each role.
1564+
Each expansion receives the same, original, AST as its input.
1565+
Swift forms the overall expansion
1566+
by collecting all of the generated AST nodes
1567+
and putting them in their corresponding places in the AST.
1568+
1569+
For an overview of macros in Swift, see <doc:Macros>.
1570+
15291571
> Grammar of a macro-expansion expression:
15301572
>
15311573
> *macro-expansion-expression* **`#`** *identifier* *generic-argument-clause*_?_ *function-call-argument-clause*_?_ *trailing-closures*_?_

0 commit comments

Comments
 (0)