|
| 1 | +# Macro Host Notes |
| 2 | + |
| 3 | +Notes about macro host (analyzer, CFE) implementations. |
| 4 | + |
| 5 | +Some related discussion |
| 6 | +[on issue 55784](https://github.com/dart-lang/sdk/issues/55784). |
| 7 | + |
| 8 | +## Analyzer |
| 9 | + |
| 10 | +1. Build library cycles. Use imports and exports to form library cycles. Sort |
| 11 | + them topologically. |
| 12 | +2. With all dependency library cycles loaded (which means that we can ask them |
| 13 | + for elements with all types ready), take the next library cycle. |
| 14 | +3. Parse all user-written Dart files, create `LibraryBuilder` objects. |
| 15 | +4. Build `Element`s for all declarations - classes, methods, etc. Just |
| 16 | + elements, types are not known yet. |
| 17 | +5. Create the `LibraryMacroApplier` instance, fill `LibraryBuilder`s with |
| 18 | + `_MacroApplication`s. The order of the macro applications is defined by the |
| 19 | + specification. |
| 20 | +6. Run the types phase. Iterate over `LibraryBuilder`s, and run the types phase |
| 21 | + for macro applications. We get Dart code as output. Add library |
| 22 | + augmentations from these Dart code strings. Build elements for these library |
| 23 | + augmentations, just like (4). Look for more macro applications, just like |
| 24 | + (5). |
| 25 | +7. Build export scopes for `LibraryBuilder`s. This includes any user-written |
| 26 | + declarations, handling re-exports. |
| 27 | +8. Resolve `TypeAnnotation`s in all `LibraryBuilder`s. |
| 28 | +9. Run the declarations phase. Like (6), but not only build elements, and find |
| 29 | + new macro applications, but also resolve type annotations like (8). If new |
| 30 | + declarations are added, rebuild export scopes. |
| 31 | +10. Run many other element linking steps - build synthetic constructors, enum |
| 32 | + constants, resolve constants, resolve metadata, perform type inference, etc. |
| 33 | +11. Run the definitions phase. Like (9), still produce new library |
| 34 | + augmentations. |
| 35 | +12. Dispose all macro applications, so free resources in the remote isolate. |
| 36 | +13. Merge all macro results into single Dart code. Discard all transitory |
| 37 | + library augmentations, add the new, final one. Do not create new elements, |
| 38 | + not like (6) or (9), instead merge `ClassElement`s, update offsets, etc. |
| 39 | + These elements have everything ready in them - types, resolution for |
| 40 | + constants, etc. We don’t want to redo this work. And we cannot create new |
| 41 | + elements - there are already references to these elements in other parts of |
| 42 | + the element model. |
| 43 | + |
| 44 | +## CFE |
| 45 | + |
| 46 | +TODO |
0 commit comments