@@ -1514,18 +1514,60 @@ Macro-expansion expressions have the following form:
1514
1514
< #macro name#> (< #macro argument 1 #> , < #macro argument 2 #> )
1515
1515
```
1516
1516
1517
- A macro- expansion expression omits the parentheses
1517
+ A macro- expansion expression omits the parentheses after the macro's name
1518
1518
if the macro doesn't take any arguments.
1519
1519
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.
1522
1522
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 .
1525
1525
1526
1526
[`file ()`]: https: // developer.apple.com/documentation/swift/file()
1527
1527
[`line ()`]: https: // developer.apple.com/documentation/swift/line()
1528
1528
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
+
1529
1571
> Grammar of a macro- expansion expression:
1530
1572
>
1531
1573
> * macro- expansion- expression* → ** `#`** * identifier* * generic- argument- clause* _ ? _ * function- call- argument- clause* _ ? _ * trailing- closures* _ ? _
0 commit comments