Skip to content

Commit ae9cf23

Browse files
Merge pull request #11033 from dotty-staging/update-metaprogramming-docs
Update metaprogramming docs
2 parents b97665a + a6fedfa commit ae9cf23

File tree

7 files changed

+45
-51
lines changed

7 files changed

+45
-51
lines changed

docs/blog/_posts/2019-01-21-12th-dotty-milestone-release.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ We also connect the new lower-level reflection layer to the existing principled
9292
- `unseal` that unseals an `Expr[T]` (non traversable code) into a `Term` and
9393
- `seal` that seals back a `Term` into an `Expr[T]`.
9494

95-
Read the [relevant documentation](https://dotty.epfl.ch/docs/reference/metaprogramming/tasty-reflect.html) to learn how to go from quotes and splices to TASTys Reflect trees and back .
95+
Read the [relevant documentation](https://dotty.epfl.ch/docs/reference/metaprogramming/reflection.html) to learn how to go from quotes and splices to TASTys Reflect trees and back .
9696

9797
### Alignments with the Scala Improvement Process
9898

docs/docs/reference/dropped-features/macros.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ The previous, experimental macro system has been dropped.
88
Instead, there is a cleaner, more restricted system based on two complementary concepts: `inline` and `'{ ... }`/`${ ... }` code generation.
99
`'{ ... }` delays the compilation of the code and produces an object containing the code, dually `${ ... }` evaluates an expression which produces code and inserts it in the surrounding `${ ... }`.
1010
In this setting, a definition marked as inlined containing a `${ ... }` is a macro, the code inside the `${ ... }` is executed at compile-time and produces code in the form of `'{ ... }`.
11-
Additionally, the contents of code can be inspected and created with a more complex reflection API (TASTy Reflect) as an extension of `'{ ... }`/`${ ... }` framework.
11+
Additionally, the contents of code can be inspected and created with a more complex reflection API as an extension of `'{ ... }`/`${ ... }` framework.
1212

1313
* `inline` has been [implemented](../metaprogramming/inline.md) in Scala 3.
1414
* Quotes `'{ ... }` and splices `${ ... }` has been [implemented](../metaprogramming/macros.md) in Scala 3.
15-
* [TASTy reflect](../metaprogramming/tasty-reflect.md) provides more complex tree based APIs to inspect or create quoted code.
15+
* [TASTy reflect](../metaprogramming/reflection.md) provides more complex tree based APIs to inspect or create quoted code.

docs/docs/reference/metaprogramming/tasty-reflect.md renamed to docs/docs/reference/metaprogramming/reflection.md

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
---
22
layout: doc-page
3-
title: "TASTy Reflect"
3+
title: "Reflection"
44
---
55

6-
TASTy Reflect enables inspection and construction of Typed Abstract Syntax Trees
6+
Reflection enables inspection and construction of Typed Abstract Syntax Trees
77
(Typed-AST). It may be used on quoted expressions (`quoted.Expr`) and quoted
88
types (`quoted.Type`) from [Macros](./macros.md) or on full TASTy files.
99

1010
If you are writing macros, please first read [Macros](./macros.md).
11-
You may find all you need without using TASTy Reflect.
11+
You may find all you need without using quote reflection.
1212

1313
## API: From quotes and splices to TASTy reflect trees and back
1414

1515
With `quoted.Expr` and `quoted.Type` we can compute code but also analyze code
1616
by inspecting the ASTs. [Macros](./macros.md) provide the guarantee that the
17-
generation of code will be type-correct. Using TASTy Reflect will break these
17+
generation of code will be type-correct. Using quote reflection will break these
1818
guarantees and may fail at macro expansion time, hence additional explicit
1919
checks must be done.
2020

@@ -34,20 +34,20 @@ def natConstImpl(x: Expr[Int])(using Quotes): Expr[Int] =
3434

3535
### Extractors
3636

37-
`import quotes.reflect._` will provide all extractors and methods on TASTy
38-
Reflect trees. For example the `Literal(_)` extractor used below.
37+
`import quotes.reflect._` will provide all extractors and methods on `quotes.reflect.Tree`s.
38+
For example the `Literal(_)` extractor used below.
3939

4040
```scala
4141
def natConstImpl(x: Expr[Int])(using Quotes): Expr[Int] =
4242
import quotes.reflect._
43-
val xTree: Term = x.asTerm
44-
xTree match
43+
val tree: Term = x.asTerm
44+
tree match
4545
case Inlined(_, _, Literal(IntConstant(n))) =>
4646
if n <= 0 then
4747
report.error("Parameter must be natural number")
4848
'{0}
4949
else
50-
xTree.asExprOf[Int]
50+
tree.asExprOf[Int]
5151
case _ =>
5252
report.error("Parameter must be a known constant")
5353
'{0}
@@ -58,9 +58,9 @@ which returns the string representation the structure of the tree. Other printer
5858
can also be found in the `Printer` module.
5959

6060
```scala
61-
xTree.show(using Printer.TreeStructure)
61+
tree.show(using Printer.TreeStructure)
6262
// or
63-
Printer.TreeStructure.show(xTree)
63+
Printer.TreeStructure.show(tree)
6464
```
6565

6666
The methods `quotes.reflect.Term.{asExpr, asExprOf}` provide a way to go back to
@@ -94,40 +94,37 @@ def macroImpl()(quotes: Quotes): Expr[Unit] =
9494

9595
### Tree Utilities
9696

97-
`scala.tasty.reflect` contains three facilities for tree traversal and
97+
`quotes.reflect` contains three facilities for tree traversal and
9898
transformation.
9999

100-
`TreeAccumulator` ties the knot of a traversal. By calling `foldOver(x, tree))`
100+
`TreeAccumulator` ties the knot of a traversal. By calling `foldOver(x, tree)(owner)`
101101
we can dive into the `tree` node and start accumulating values of type `X` (e.g.,
102102
of type `List[Symbol]` if we want to collect symbols). The code below, for
103-
example, collects the pattern variables of a tree.
103+
example, collects the `val` definitions in the tree.
104104

105105
```scala
106106
def collectPatternVariables(tree: Tree)(using ctx: Context): List[Symbol] =
107107
val acc = new TreeAccumulator[List[Symbol]]:
108-
def apply(syms: List[Symbol], tree: Tree)(using ctx: Context): List[Symbol] = tree match
109-
case Bind(_, body) => apply(tree.symbol :: syms, body)
110-
case _ => foldOver(syms, tree)
108+
def foldTree(syms: List[Symbol], tree: Tree)(owner: Symbol): List[Symbol] = tree match
109+
case ValDef(_, _, rhs) =>
110+
val newSyms = tree.symbol :: syms
111+
foldTree(newSyms, body)(tree.symbol)
112+
case _ =>
113+
foldOverTree(syms, tree)(owner)
111114
acc(Nil, tree)
112115
```
113116

114117
A `TreeTraverser` extends a `TreeAccumulator` and performs the same traversal
115-
but without returning any value. Finally a `TreeMap` performs a transformation.
118+
but without returning any value. Finally, a `TreeMap` performs a transformation.
116119

117-
#### Let
120+
#### ValDef.let
118121

119-
`scala.tasty.Reflection` also offers a method `let` that allows us to bind the
120-
`rhs` (right-hand side) to a `val` and use it in `body`. Additionally, `lets`
121-
binds the given `terms` to names and allows to use them in the `body`. Their type
122-
definitions are shown below:
122+
`quotes.reflect.ValDef` also offers a method `let` that allows us to bind the `rhs` (right-hand side) to a `val` and use it in `body`.
123+
Additionally, `lets` binds the given `terms` to names and allows to use them in the `body`.
124+
Their type definitions are shown below:
123125

124126
```scala
125127
def let(rhs: Term)(body: Ident => Term): Term = ...
126128

127129
def lets(terms: List[Term])(body: List[Term] => Term): Term = ...
128130
```
129-
130-
## More Examples
131-
132-
* Start experimenting with TASTy Reflect ([link](https://github.com/nicolasstucki/tasty-reflection-exercise))
133-
(outdated, need update)

docs/docs/reference/metaprogramming/staging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
layout: doc-page
3-
title: "Multi-Stage Programming"
3+
title: "Runtime Multi-Stage Programming"
44
---
55

66
The framework expresses at the same time compile-time metaprogramming and

docs/docs/reference/metaprogramming/tasty-inspect.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,27 @@ through the TASTy reflect API.
1515

1616
## Inspecting TASTy files
1717

18-
To inspect the TASTy Reflect trees of a TASTy file a consumer can be defined in
19-
the following way.
18+
To inspect the trees of a TASTy file a consumer can be defined in the following way.
2019

2120
```scala
22-
import scala.tasty.Reflection
23-
import scala.tasty.file._
21+
import scala.quoted._
22+
import scala.tasty.inspector._
2423

25-
class Consumer extends TastyInspector:
26-
final def apply(reflect: Reflection)(root: reflect.Tree): Unit =
27-
import reflect._
28-
// Do something with the tree
24+
class MyInspector extends TastyInspector:
25+
protected def processCompilationUnit(using Quotes)(tree: quotes.reflect.Tree): Unit =
26+
import quotes.reflect._
27+
// Do something with the tree
2928
```
3029

31-
Then the consumer can be instantiated with the following code to get the tree of
32-
the class `foo.Bar` for a foo in the classpath.
30+
Then the consumer can be instantiated with the following code to get the tree of the `foo/Bar.tasty` file.
3331

3432
```scala
3533
object Test:
36-
def main(args: Array[String]): Unit =
37-
InspectTasty("", List("foo.Bar"), new Consumer)
34+
def main(args: Array[String]): Unit =
35+
new MyInspector().inspectTastyFiles("foo/Bar.tasty")
3836
```
3937

40-
Note that if we need to run the main (in the example below defined in an object called `Test`) after
41-
compilation we need to make the compiler available to the runtime:
38+
Note that if we need to run the main (in the example below defined in an object called `Test`) after compilation we need to make the compiler available to the runtime:
4239

4340
```shell
4441
scalac -d out Test.scala

docs/docs/reference/metaprogramming/toc.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ introduce the following fundamental facilities:
2525
to program code. Together with `inline`, these two abstractions allow
2626
to construct program code programmatically.
2727

28-
3. [Staging](./staging.md) Where macros construct code at _compile-time_,
28+
3. [Runtime Staging](./staging.md) Where macros construct code at _compile-time_,
2929
staging lets programs construct new code at _runtime_. That way,
3030
code generation can depend not only on static data but also on data available at runtime. This splits the evaluation of the program in two or more phases or ...
3131
stages. Consequently, this method of generative programming is called "Multi-Stage Programming". Staging is built on the same foundations as macros. It uses
3232
quotes and splices, but leaves out `inline`.
3333

34-
4. [TASTy Reflection](./tasty-reflect.md) Quotations are a "black-box"
34+
4. [Reflection](./reflection.md) Quotations are a "black-box"
3535
representation of code. They can be parameterized and composed using
3636
splices, but their structure cannot be analyzed from the outside. TASTy
3737
reflection gives a way to analyze code structure by partly revealing the representation type of a piece of code in a standard API. The representation

docs/sidebar.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ sidebar:
8181
url: docs/reference/metaprogramming/inline.html
8282
- title: Macros
8383
url: docs/reference/metaprogramming/macros.html
84-
- title: Staging
84+
- title: Runtime Staging
8585
url: docs/reference/metaprogramming/staging.html
86-
- title: TASTy Reflection
87-
url: docs/reference/metaprogramming/tasty-reflect.html
86+
- title: Reflection
87+
url: docs/reference/metaprogramming/reflection.html
8888
- title: TASTy Inspection
8989
url: docs/reference/metaprogramming/tasty-inspect.html
9090
- title: Other New Features

0 commit comments

Comments
 (0)