|
| 1 | +# Dart Model |
| 2 | + |
| 3 | +Author: David Morgan |
| 4 | + |
| 5 | +Status: **Work In Progress** |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +A package for describing Dart code, augmentations to it, and queries about it. |
| 10 | + |
| 11 | +Motivated by its use in the |
| 12 | +[macros feature](https://github.com/dart-lang/language/blob/main/working/macros/feature-specification.md) |
| 13 | +but not tied to it. |
| 14 | + |
| 15 | +## Concepts |
| 16 | + |
| 17 | +```mermaid |
| 18 | +--- |
| 19 | +title: package:dart_model |
| 20 | +--- |
| 21 | +flowchart TD |
| 22 | + Model:::model |
| 23 | + Query:::query |
| 24 | + Delta:::delta |
| 25 | + Augmentation:::augmentation |
| 26 | + Service:::service |
| 27 | +
|
| 28 | + classDef model fill:#ffd6a5,stroke-width:0px |
| 29 | + classDef query fill:#fdffb6,stroke-width:0px |
| 30 | + classDef delta fill:#caffbf,stroke-width:0px |
| 31 | + classDef augmentation fill:#9bf6ff,stroke-width:0px |
| 32 | + classDef service fill:#bdb2ff,stroke-width:0px |
| 33 | +``` |
| 34 | + |
| 35 | +### Data Types |
| 36 | + |
| 37 | +`Model`, `Query`, `Delta` and `Augmentation` are serializable data types, see details [below](#serialization-and-schema). |
| 38 | + |
| 39 | +### Model |
| 40 | + |
| 41 | +A description of a corpus of Dart code with the following properties: |
| 42 | + |
| 43 | +- It is a **partial** model. |
| 44 | + - It can omit files, parts of files, and attributes of parts of files, depending on what was requested, what was known about the corpus when the request was made, and considerations about what a particular viewer of the code is _allowed_ to see. |
| 45 | +- It is **sufficient input for codegen or macros**. |
| 46 | + - Anything that is needed to know what to generate, for example type hierarchies, is modeled. |
| 47 | + |
| 48 | +```mermaid |
| 49 | +flowchart LR |
| 50 | + Model:::model --> Generator:::generator --> Augmentation:::augmentation |
| 51 | +
|
| 52 | + classDef model fill:#ffd6a5,stroke-width:0px |
| 53 | + classDef generator fill:#fff,stroke:#000 |
| 54 | + classDef augmentation fill:#9bf6ff,stroke-width:0px |
| 55 | + ``` |
| 56 | + |
| 57 | +### Query |
| 58 | + |
| 59 | +A query for information about a corpus of Dart code with the following properties: |
| 60 | + |
| 61 | +- It **can be applied to a `Model`** to extract another, smaller or identical, `Model`. |
| 62 | + - For example a `Query` for a particular package URI extracts the part of the `Model` corresponding to that URI. |
| 63 | +- It is **designed for codegen or macros** in the sense that it should be possible in just one or a small number of queries to obtain all the information a particular generation needs. |
| 64 | + - An example `Query` might read in pseudocode: give me class `Foo`, its public fields, then all types mentioned in those field types and their metadata. |
| 65 | + |
| 66 | +```mermaid |
| 67 | +flowchart LR |
| 68 | + Model:::model --> Query:::query --> m2[Smaller Model]:::model |
| 69 | +
|
| 70 | +
|
| 71 | + classDef model fill:#ffd6a5,stroke-width:0px |
| 72 | + classDef query fill:#fdffb6,stroke-width:0px |
| 73 | +``` |
| 74 | + |
| 75 | +### Delta |
| 76 | + |
| 77 | +The delta between two `Model` instances. |
| 78 | + |
| 79 | +- Given `Model` instances `previous` and `current`, the `Delta` can be computed. |
| 80 | + - Then: applying the `Delta` to `previous` makes it equal to `current`. |
| 81 | + |
| 82 | +```mermaid |
| 83 | +flowchart LR |
| 84 | + m1[Current Model]:::model -- "minus" --- |
| 85 | + m2[Previous Model]:::model -- "gives" --- |
| 86 | + Delta:::delta |
| 87 | +
|
| 88 | + classDef model fill:#ffd6a5,stroke-width:0px |
| 89 | + classDef delta fill:#caffbf,stroke-width:0px |
| 90 | + linkStyle 0,1 stroke:#fff,font-size:20px |
| 91 | +``` |
| 92 | +```mermaid |
| 93 | +flowchart LR |
| 94 | + m1[Previous Model]:::model -- "plus" --- |
| 95 | + Delta:::delta -- "gives" --- |
| 96 | + m2[Current Model]:::model |
| 97 | +
|
| 98 | + classDef model fill:#ffd6a5,stroke-width:0px |
| 99 | + classDef delta fill:#caffbf,stroke-width:0px |
| 100 | + linkStyle 0,1 stroke:#fff,font-size:20px |
| 101 | +``` |
| 102 | + |
| 103 | +### Augmentation |
| 104 | + |
| 105 | +The output of a generator or macro: a set of incremental changes that can be applied to a program, with the following properties: |
| 106 | + |
| 107 | +- It is **designed for the generator and the host** in the sense that it is both convenient to build and convenient to apply; for example, declarations and definitions might be separated out as they have different types of consequences when applied to a program. |
| 108 | + |
| 109 | +### Service |
| 110 | + |
| 111 | +An endpoint that can accept a `Query` instance and return a `Model`. |
| 112 | + |
| 113 | +`package:dart_model` provides a reference `Service` implementation that is backed by a `Model`. More interesting `Service` implementations for generators and macros will be backed by the analyzer or the CFE. |
| 114 | + |
| 115 | +## Serialization and Schema |
| 116 | + |
| 117 | +`Model`, `Query`, `Delta` and `Augmentation` are each described by a JSON schema published in the same package, as a plain text file and also embedded in Dart code for use by tools. |
| 118 | + |
| 119 | +Each schema is available in two versions: a "strict" schema that only matches what the current package produces, and a "loose" schema that allows for future changes. |
| 120 | + |
| 121 | +Schemas are versioned using semver. Schemas are updated in place for compatible changes, and as new files for major version (incompatible) releases. |
| 122 | + |
| 123 | +## Performance |
| 124 | + |
| 125 | +Data types can be serialized to and from JSON per their schemas. |
| 126 | + |
| 127 | +A binary serialization format is available for performance. When this is used, values are accumulated directly into a binary buffer, so there is no "serialize" step. The received of the value can use values directly from the buffer, so there is no "deserialize" step. |
| 128 | + |
| 129 | +## Macro <-> Host Communication |
| 130 | + |
| 131 | +`package:dart_model` types can be embedded in communication between macro and host, but a higher level protocol on top that is out of scope here. |
0 commit comments