Skip to content

Commit 3ff58dc

Browse files
committed
TASTy macro annotation (part 1)
Add basic support for TASTy annotations. * Introduce experimental `scala.annotations.TastyAnnotation` * Macro annotations can analyze or modify definitions * Macro annotation can add definition around the annotated definition * Added members are not visible while typing * Added members are not visible to other macro annotations * Added definition must have the same owner * Implement macro annotation expansion * Implemented at Inlining phase * Can use macro annotations in staged expressions (expanded when at stage 0) * Can use staged expression to implement macro annotations * Can insert calls to inline methods in macro annotations * Current limitations (to be loosened) * Can only be used on `def`, `val`, `lazy val` and `var` * Can only add `def`, `val`, `lazy val` and `var` definitions Based on: * #15626 * https://infoscience.epfl.ch/record/294615?ln=en
1 parent 1f451ec commit 3ff58dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+974
-19
lines changed

compiler/src/dotty/tools/dotc/CompilationUnit.scala

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotc
33

44
import core._
55
import Contexts._
6-
import SymDenotations.ClassDenotation
6+
import SymDenotations.{ClassDenotation, NoDenotation}
77
import Symbols._
88
import util.{FreshNameCreator, SourceFile, NoSource}
99
import util.Spans.Span
@@ -45,6 +45,8 @@ class CompilationUnit protected (val source: SourceFile) {
4545
*/
4646
var needsInlining: Boolean = false
4747

48+
var hasTastyAnnotations: Boolean = false
49+
4850
/** Set to `true` if inliner added anonymous mirrors that need to be completed */
4951
var needsMirrorSupport: Boolean = false
5052

@@ -119,6 +121,7 @@ object CompilationUnit {
119121
force.traverse(unit1.tpdTree)
120122
unit1.needsStaging = force.containsQuote
121123
unit1.needsInlining = force.containsInline
124+
unit1.hasTastyAnnotations = force.containsTastyAnnotation
122125
}
123126
unit1
124127
}
@@ -147,6 +150,7 @@ object CompilationUnit {
147150
var containsQuote = false
148151
var containsInline = false
149152
var containsCaptureChecking = false
153+
var containsTastyAnnotation = false
150154
def traverse(tree: Tree)(using Context): Unit = {
151155
if (tree.symbol.isQuote)
152156
containsQuote = true
@@ -160,6 +164,9 @@ object CompilationUnit {
160164
Feature.handleGlobalLanguageImport(prefix, imported)
161165
case _ =>
162166
case _ =>
167+
for annot <- tree.symbol.annotations do
168+
if annot.tree.symbol.denot != NoDenotation && annot.tree.symbol.owner.derivesFrom(defn.TastyAnnotationClass) then
169+
ctx.compilationUnit.hasTastyAnnotations = true
163170
traverseChildren(tree)
164171
}
165172
}

compiler/src/dotty/tools/dotc/config/Printers.scala

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ object Printers {
3232
val init = noPrinter
3333
val inlining = noPrinter
3434
val interactiv = noPrinter
35+
val macroAnnot = noPrinter
3536
val matchTypes = noPrinter
3637
val nullables = noPrinter
3738
val overload = noPrinter

compiler/src/dotty/tools/dotc/core/Definitions.scala

+2
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,8 @@ class Definitions {
895895
@tu lazy val QuotedTypeModule: Symbol = QuotedTypeClass.companionModule
896896
@tu lazy val QuotedTypeModule_of: Symbol = QuotedTypeModule.requiredMethod("of")
897897

898+
@tu lazy val TastyAnnotationClass: ClassSymbol = requiredClass("scala.annotation.TastyAnnotation")
899+
898900
@tu lazy val CanEqualClass: ClassSymbol = getClassIfDefined("scala.Eql").orElse(requiredClass("scala.CanEqual")).asClass
899901
def CanEqual_canEqualAny(using Context): TermSymbol =
900902
val methodName = if CanEqualClass.name == tpnme.Eql then nme.eqlAny else nme.canEqualAny

compiler/src/dotty/tools/dotc/quoted/Interpreter.scala

+13-14
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import dotty.tools.dotc.reporting.Message
3232
import dotty.tools.repl.AbstractFileClassLoader
3333

3434
/** Tree interpreter for metaprogramming constructs */
35-
abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context):
35+
class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context):
3636
import Interpreter._
3737
import tpd._
3838

@@ -65,7 +65,7 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
6565

6666
// TODO disallow interpreted method calls as arguments
6767
case Call(fn, args) =>
68-
if (fn.symbol.isConstructor && fn.symbol.owner.owner.is(Package))
68+
if (fn.symbol.isConstructor)
6969
interpretNew(fn.symbol, args.flatten.map(interpretTree))
7070
else if (fn.symbol.is(Module))
7171
interpretModuleAccess(fn.symbol)
@@ -185,7 +185,8 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
185185
loadModule(fn.moduleClass)
186186

187187
private def interpretNew(fn: Symbol, args: => List[Object])(implicit env: Env): Object = {
188-
val clazz = loadClass(fn.owner.fullName.toString)
188+
val className = fn.owner.fullName.toString.replaceAll("\\$\\.", "\\$")
189+
val clazz = loadClass(className)
189190
val constr = clazz.getConstructor(paramsSig(fn): _*)
190191
constr.newInstance(args: _*).asInstanceOf[Object]
191192
}
@@ -218,10 +219,6 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
218219
private def loadClass(name: String): Class[?] =
219220
try classLoader.loadClass(name)
220221
catch {
221-
case _: ClassNotFoundException if ctx.compilationUnit.isSuspendable =>
222-
if (ctx.settings.XprintSuspension.value)
223-
report.echo(i"suspension triggered by a dependency on $name", pos)
224-
ctx.compilationUnit.suspend()
225222
case MissingClassDefinedInCurrentRun(sym) if ctx.compilationUnit.isSuspendable =>
226223
if (ctx.settings.XprintSuspension.value)
227224
report.echo(i"suspension triggered by a dependency on $sym", pos)
@@ -276,13 +273,15 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
276273
}
277274

278275
private object MissingClassDefinedInCurrentRun {
279-
def unapply(targetException: NoClassDefFoundError)(using Context): Option[Symbol] = {
280-
val className = targetException.getMessage
281-
if (className eq null) None
282-
else {
283-
val sym = staticRef(className.toTypeName).symbol
284-
if (sym.isDefinedInCurrentRun) Some(sym) else None
285-
}
276+
def unapply(targetException: Throwable)(using Context): Option[Symbol] = {
277+
targetException match
278+
case _: NoClassDefFoundError | _: ClassNotFoundException =>
279+
val className = targetException.getMessage
280+
if className eq null then None
281+
else
282+
val sym = staticRef(className.toTypeName).symbol
283+
if (sym.isDefinedInCurrentRun) Some(sym) else None
284+
case _ => None
286285
}
287286
}
288287

compiler/src/dotty/tools/dotc/transform/Inlining.scala

+16-4
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import Contexts._
77
import Symbols._
88
import SymUtils._
99
import dotty.tools.dotc.ast.tpd
10-
10+
import dotty.tools.dotc.ast.Trees._
11+
import dotty.tools.dotc.quoted._
1112
import dotty.tools.dotc.core.StagingContext._
1213
import dotty.tools.dotc.inlines.Inlines
1314
import dotty.tools.dotc.ast.TreeMapWithImplicits
15+
import dotty.tools.dotc.core.DenotTransformers.IdentityDenotTransformer
1416

1517

1618
/** Inlines all calls to inline methods that are not in an inline method or a quote */
17-
class Inlining extends MacroTransform {
19+
class Inlining extends MacroTransform with IdentityDenotTransformer {
1820
import tpd._
1921

2022
override def phaseName: String = Inlining.name
@@ -23,8 +25,10 @@ class Inlining extends MacroTransform {
2325

2426
override def allowsImplicitSearch: Boolean = true
2527

28+
override def changesMembers: Boolean = true
29+
2630
override def run(using Context): Unit =
27-
if ctx.compilationUnit.needsInlining then
31+
if ctx.compilationUnit.needsInlining || ctx.compilationUnit.hasTastyAnnotations then
2832
try super.run
2933
catch case _: CompilationUnit.SuspendException => ()
3034

@@ -59,8 +63,16 @@ class Inlining extends MacroTransform {
5963
private class InliningTreeMap extends TreeMapWithImplicits {
6064
override def transform(tree: Tree)(using Context): Tree = {
6165
tree match
62-
case tree: DefTree =>
66+
case tree: MemberDef =>
6367
if tree.symbol.is(Inline) then tree
68+
else if tree.symbol.is(Param) then super.transform(tree)
69+
else if
70+
!tree.symbol.isPrimaryConstructor
71+
&& StagingContext.level == 0
72+
&& TastyAnnotations.hasTastyAnnotation(tree.symbol)
73+
then
74+
val trees = new TastyAnnotations(Inlining.this).expandAnnotations(tree)
75+
flatTree(trees.map(super.transform))
6476
else super.transform(tree)
6577
case _: Typed | _: Block =>
6678
super.transform(tree)

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

+14
Original file line numberDiff line numberDiff line change
@@ -375,21 +375,25 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
375375
)
376376
}
377377
case tree: ValDef =>
378+
registerIfHasTastyAnnotations(tree)
378379
checkErasedDef(tree)
379380
val tree1 = cpy.ValDef(tree)(rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
380381
if tree1.removeAttachment(desugar.UntupledParam).isDefined then
381382
checkStableSelection(tree.rhs)
382383
processValOrDefDef(super.transform(tree1))
383384
case tree: DefDef =>
385+
registerIfHasTastyAnnotations(tree)
384386
checkErasedDef(tree)
385387
annotateContextResults(tree)
386388
val tree1 = cpy.DefDef(tree)(rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
387389
processValOrDefDef(superAcc.wrapDefDef(tree1)(super.transform(tree1).asInstanceOf[DefDef]))
388390
case tree: TypeDef =>
391+
registerIfHasTastyAnnotations(tree)
389392
val sym = tree.symbol
390393
if (sym.isClass)
391394
VarianceChecker.check(tree)
392395
annotateExperimental(sym)
396+
checkTastyAnnotation(sym)
393397
tree.rhs match
394398
case impl: Template =>
395399
for parent <- impl.parents do
@@ -483,6 +487,16 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
483487
private def normalizeErasedRhs(rhs: Tree, sym: Symbol)(using Context) =
484488
if (sym.isEffectivelyErased) dropInlines.transform(rhs) else rhs
485489

490+
/** Check if the definition has macro annotation and sets `compilationUnit.hasTastyAnnotations` if needed. */
491+
private def registerIfHasTastyAnnotations(tree: DefTree)(using Context) =
492+
if !Inlines.inInlineMethod && TastyAnnotations.hasTastyAnnotation(tree.symbol) then
493+
ctx.compilationUnit.hasTastyAnnotations = true
494+
495+
/** Check macro annotations implementations */
496+
private def checkTastyAnnotation(sym: Symbol)(using Context) =
497+
if sym.derivesFrom(defn.TastyAnnotationClass) && !sym.isStatic then
498+
report.error("classes that extend TastyAnnotation must not be inner/local classes", sym.srcPos)
499+
486500
private def checkErasedDef(tree: ValOrDefDef)(using Context): Unit =
487501
if tree.symbol.is(Erased, butNot = Macro) then
488502
val tpe = tree.rhs.tpe
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package dotty.tools.dotc
2+
package transform
3+
4+
import scala.language.unsafeNulls
5+
6+
import dotty.tools.dotc.ast.tpd
7+
import dotty.tools.dotc.ast.Trees.*
8+
import dotty.tools.dotc.config.Printers.{macroAnnot => debug}
9+
import dotty.tools.dotc.core.Annotations.*
10+
import dotty.tools.dotc.core.Contexts.*
11+
import dotty.tools.dotc.core.Decorators.*
12+
import dotty.tools.dotc.core.DenotTransformers.DenotTransformer
13+
import dotty.tools.dotc.core.Flags.*
14+
import dotty.tools.dotc.core.MacroClassLoader
15+
import dotty.tools.dotc.core.Symbols.*
16+
import dotty.tools.dotc.core.SymDenotations.NoDenotation
17+
import dotty.tools.dotc.quoted.*
18+
import dotty.tools.dotc.util.SrcPos
19+
import scala.quoted.runtime.impl.{QuotesImpl, SpliceScope}
20+
21+
import scala.quoted.Quotes
22+
23+
class TastyAnnotations(thisPhase: DenotTransformer):
24+
import tpd.*
25+
import TastyAnnotations.*
26+
27+
/** Expands every TASTy annotation that is on this tree.
28+
* Returns a list with transformed definition and any added definitions.
29+
*/
30+
def expandAnnotations(tree: MemberDef)(using Context): List[DefTree] =
31+
if !hasTastyAnnotation(tree.symbol) then
32+
List(tree)
33+
else if tree.symbol.is(Module) then
34+
if tree.symbol.isClass then // error only reported on module class
35+
report.error("TASTy annotations are not supported on object", tree)
36+
List(tree)
37+
else if tree.symbol.isClass then
38+
report.error("TASTy annotations are not supported on class", tree)
39+
List(tree)
40+
else if tree.symbol.isType then
41+
report.error("TASTy annotations are not supported on type", tree)
42+
List(tree)
43+
else
44+
debug.println(i"Expanding TASTy annotations of:\n$tree")
45+
46+
val macroInterpreter = new Interpreter(tree.srcPos, MacroClassLoader.fromContext)
47+
48+
val allTrees = List.newBuilder[DefTree]
49+
var insertedAfter: List[List[DefTree]] = Nil
50+
51+
// Apply all TASTy annotation to `tree` and collect new definitions in order
52+
val transformedTree: DefTree = tree.symbol.annotations.foldLeft(tree) { (tree, annot) =>
53+
if isTastyAnnotation(annot) then
54+
debug.println(i"Expanding TASTy annotation: ${annot}")
55+
56+
// Interpret call to `new myAnnot(..).transform(using <Quotes>)(<tree>)`
57+
val transformedTrees = callMacro(macroInterpreter, tree, annot)
58+
transformedTrees.span(_.symbol != tree.symbol) match
59+
case (prefixed, newTree :: suffixed) =>
60+
allTrees ++= prefixed
61+
insertedAfter = suffixed :: insertedAfter
62+
prefixed.foreach(checkAndEnter(_, tree.symbol, annot))
63+
suffixed.foreach(checkAndEnter(_, tree.symbol, annot))
64+
newTree
65+
case (Nil, Nil) =>
66+
report.error(i"Unexpected `Nil` returned by `(${annot.tree}).transform(..)` during macro expansion", annot.tree.srcPos)
67+
tree
68+
case (_, Nil) =>
69+
report.error(i"Transformed tree for ${tree} was not return by `(${annot.tree}).transform(..)` during macro expansion", annot.tree.srcPos)
70+
tree
71+
else
72+
tree
73+
}
74+
75+
allTrees += transformedTree
76+
insertedAfter.foreach(allTrees.++=)
77+
78+
val result = allTrees.result()
79+
debug.println(result.map(_.show).mkString("expanded to:\n", "\n", ""))
80+
result
81+
82+
/** Interpret the code `new annot(..).transform(using <Quotes(ctx)>)(<tree>)` */
83+
private def callMacro(interpreter: Interpreter, tree: MemberDef, annot: Annotation)(using Context): List[MemberDef] =
84+
// TODO: Remove when scala.annaotaion.TastyAnnotation is no longer experimental
85+
import scala.reflect.Selectable.reflectiveSelectable
86+
type TastyAnnotation = {
87+
def transform(using Quotes)(tree: Object/*Erased type of quotes.refelct.Definition*/): List[MemberDef /*quotes.refelct.Definition known to be MemberDef in QuotesImpl*/]
88+
}
89+
90+
// Interpret TASTy annotation instantiation `new myAnnot(..)`
91+
val annotInstance = interpreter.interpret[TastyAnnotation](annot.tree).get
92+
// TODO: Remove when scala.annaotaion.TastyAnnotation is no longer experimental
93+
assert(annotInstance.getClass.getClassLoader.loadClass("scala.annotation.TastyAnnotation").isInstance(annotInstance))
94+
95+
val quotes = QuotesImpl()(using SpliceScope.contextWithNewSpliceScope(tree.symbol.sourcePos)(using MacroExpansion.context(tree)).withOwner(tree.symbol))
96+
annotInstance.transform(using quotes)(tree.asInstanceOf[quotes.reflect.Definition])
97+
98+
/** Check that this tree can be added by the TASTy annotation and enter it if needed */
99+
private def checkAndEnter(newTree: Tree, annotated: Symbol, annot: Annotation)(using Context) =
100+
val sym = newTree.symbol
101+
if sym.isClass then
102+
report.error("Generating classes is not supported", annot.tree)
103+
else if sym.isType then
104+
report.error("Generating type is not supported", annot.tree)
105+
else if sym.owner != annotated.owner then
106+
report.error(i"TASTy annotation $annot added $sym with an inconsistent owner. Expected it to be owned by ${annotated.owner} but was owned by ${sym.owner}.", annot.tree)
107+
else
108+
sym.enteredAfter(thisPhase)
109+
110+
object TastyAnnotations:
111+
112+
/** Is this an annotation that implements `scala.annation.TastyAnnotation` */
113+
def isTastyAnnotation(annot: Annotation)(using Context): Boolean =
114+
val sym = annot.tree.symbol
115+
sym.denot != NoDenotation && sym.owner.derivesFrom(defn.TastyAnnotationClass)
116+
117+
/** Is this symbol annotated with an annotation that implements `scala.annation.TastyAnnotation` */
118+
def hasTastyAnnotation(sym: Symbol)(using Context): Boolean =
119+
sym.getAnnotation(defn.TastyAnnotationClass).isDefined

compiler/src/dotty/tools/dotc/transform/YCheckPositions.scala

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class YCheckPositions extends Phase {
6161

6262
private def isMacro(call: Tree)(using Context) =
6363
call.symbol.is(Macro) ||
64+
(call.symbol.isClass && call.tpe.derivesFrom(defn.TastyAnnotationClass)) ||
6465
// The call of a macro after typer is encoded as a Select while other inlines are Ident
6566
// TODO remove this distinction once Inline nodes of expanded macros can be trusted (also in Inliner.inlineCallTrace)
6667
(!(ctx.phase <= postTyperPhase) && call.isInstanceOf[Select])

0 commit comments

Comments
 (0)