Skip to content

Commit cc25751

Browse files
committed
Macro annotation (part 1)
Add basic support for macro annotations. * Introduce experimental `scala.annotations.MacroAnnotation` * 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: * scala#15626 * https://infoscience.epfl.ch/record/294615?ln=en
1 parent e4326e2 commit cc25751

File tree

61 files changed

+967
-19
lines changed

Some content is hidden

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

61 files changed

+967
-19
lines changed

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

Lines changed: 8 additions & 1 deletion
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 hasMacroAnnotations: 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.hasMacroAnnotations = force.containsMacroAnnotation
122125
}
123126
unit1
124127
}
@@ -147,6 +150,7 @@ object CompilationUnit {
147150
var containsQuote = false
148151
var containsInline = false
149152
var containsCaptureChecking = false
153+
var containsMacroAnnotation = 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.QuotedMacroAnnotationClass) then
169+
ctx.compilationUnit.hasMacroAnnotations = true
163170
traverseChildren(tree)
164171
}
165172
}

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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,9 @@ 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 QuotedMacroAnnotationClass: ClassSymbol = requiredClass("scala.annotation.MacroAnnotation")
899+
@tu lazy val QuotedMacroAnnotation_transform: Symbol = QuotedMacroAnnotationClass.requiredMethod("transform")
900+
898901
@tu lazy val CanEqualClass: ClassSymbol = getClassIfDefined("scala.Eql").orElse(requiredClass("scala.CanEqual")).asClass
899902
def CanEqual_canEqualAny(using Context): TermSymbol =
900903
val methodName = if CanEqualClass.name == tpnme.Eql then nme.eqlAny else nme.canEqualAny

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
6464

6565
// TODO disallow interpreted method calls as arguments
6666
case Call(fn, args) =>
67-
if (fn.symbol.isConstructor && fn.symbol.owner.owner.is(Package))
67+
if (fn.symbol.isConstructor)
6868
interpretNew(fn.symbol, args.flatten.map(interpretTree))
6969
else if (fn.symbol.is(Module))
7070
interpretModuleAccess(fn.symbol)
@@ -174,6 +174,12 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
174174
(args: List[Object]) => stopIfRuntimeException(method.invoke(inst, args: _*), method)
175175
}
176176

177+
protected def interpretedMethodCall(inst: Object, fn: Symbol)(args: Object*)(implicit env: Env): Object = {
178+
val name = fn.name.asTermName
179+
val method = getMethod(inst.getClass, name, paramsSig(fn))
180+
stopIfRuntimeException(method.invoke(inst, args: _*), method)
181+
}
182+
177183
private def interpretedStaticFieldAccess(sym: Symbol)(implicit env: Env): Object = {
178184
val clazz = loadClass(sym.owner.fullName.toString)
179185
val field = clazz.getField(sym.name.toString)
@@ -184,7 +190,8 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
184190
loadModule(fn.moduleClass)
185191

186192
private def interpretNew(fn: Symbol, args: => List[Object])(implicit env: Env): Object = {
187-
val clazz = loadClass(fn.owner.fullName.toString)
193+
val className = fn.owner.fullName.toString.replaceAll("\\$\\.", "\\$")
194+
val clazz = loadClass(className)
188195
val constr = clazz.getConstructor(paramsSig(fn): _*)
189196
constr.newInstance(args: _*).asInstanceOf[Object]
190197
}
@@ -217,10 +224,6 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
217224
private def loadClass(name: String): Class[?] =
218225
try classLoader.loadClass(name)
219226
catch {
220-
case _: ClassNotFoundException if ctx.compilationUnit.isSuspendable =>
221-
if (ctx.settings.XprintSuspension.value)
222-
report.echo(i"suspension triggered by a dependency on $name", pos)
223-
ctx.compilationUnit.suspend()
224227
case MissingClassDefinedInCurrentRun(sym) if ctx.compilationUnit.isSuspendable =>
225228
if (ctx.settings.XprintSuspension.value)
226229
report.echo(i"suspension triggered by a dependency on $sym", pos)
@@ -275,13 +278,15 @@ abstract class Interpreter(pos: SrcPos, classLoader: ClassLoader)(using Context)
275278
}
276279

277280
private object MissingClassDefinedInCurrentRun {
278-
def unapply(targetException: NoClassDefFoundError)(using Context): Option[Symbol] = {
279-
val className = targetException.getMessage
280-
if (className eq null) None
281-
else {
282-
val sym = staticRef(className.toTypeName).symbol
283-
if (sym.isDefinedInCurrentRun) Some(sym) else None
284-
}
281+
def unapply(targetException: Throwable)(using Context): Option[Symbol] = {
282+
targetException match
283+
case _: NoClassDefFoundError | _: ClassNotFoundException =>
284+
val className = targetException.getMessage
285+
if className eq null then None
286+
else
287+
val sym = staticRef(className.toTypeName).symbol
288+
if (sym.isDefinedInCurrentRun) Some(sym) else None
289+
case _ => None
285290
}
286291
}
287292

@@ -344,7 +349,7 @@ end Interpreter
344349

345350
object Interpreter:
346351
/** Exception that stops interpretation if some issue is found */
347-
class StopInterpretation(val msg: String, val pos: SrcPos) extends Exception
352+
class StopInterpretation(val msg: String, val pos: SrcPos) extends Exception(msg)
348353

349354
object Call:
350355
import tpd._

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

Lines changed: 18 additions & 4 deletions
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.hasMacroAnnotations then
2832
try super.run
2933
catch case _: CompilationUnit.SuspendException => ()
3034

@@ -61,7 +65,17 @@ class Inlining extends MacroTransform {
6165
tree match
6266
case tree: DefTree =>
6367
if tree.symbol.is(Inline) then tree
64-
else super.transform(tree)
68+
else
69+
tree match
70+
case _: Bind => super.transform(tree)
71+
case tree if tree.symbol.is(Param) => super.transform(tree)
72+
case tree
73+
if !tree.symbol.isPrimaryConstructor
74+
&& StagingContext.level == 0
75+
&& MacroAnnotations.hasMacro(tree.symbol) =>
76+
val trees = new MacroAnnotations(Inlining.this).transform(tree)
77+
flatTree(trees.map(super.transform))
78+
case tree => super.transform(tree)
6579
case _: Typed | _: Block =>
6680
super.transform(tree)
6781
case _ if Inlines.needsInlining(tree) =>
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
class MacroAnnotations(thisPhase: DenotTransformer):
22+
import tpd.*
23+
import MacroAnnotations.*
24+
25+
/** Expands every macro annotation that is on this tree.
26+
* Returns a list with transformed definition and any added definitions.
27+
*/
28+
def transform(tree: DefTree)(using Context): List[DefTree] =
29+
if !hasMacro(tree.symbol) then
30+
List(tree)
31+
else if tree.symbol.is(Module) then
32+
if tree.symbol.isClass then // error only reported on module class
33+
report.error("Macro annotations are not supported on object", tree)
34+
List(tree)
35+
else if tree.symbol.isClass then
36+
report.error("Macro annotations are not supported on class", tree)
37+
List(tree)
38+
else if tree.symbol.isType then
39+
report.error("Macro annotations are not supported on type", tree)
40+
List(tree)
41+
else
42+
debug.println(i"Expanding macro annotations of:\n$tree")
43+
44+
val macroInterpreter = new InterpreterMacroAnnot(tree.srcPos, MacroClassLoader.fromContext)
45+
46+
val allTrees = List.newBuilder[DefTree]
47+
var insertedAfter: List[List[DefTree]] = Nil
48+
49+
// Apply all macro annotation to `tree` and collect new definitions in order
50+
val transformedTree: DefTree = tree.symbol.annotations.foldLeft(tree) { (tree, annot) =>
51+
if isMacroAnnotation(annot) then
52+
debug.println(i"Expanding macro annotation: ${annot}")
53+
54+
// Interpret call to `new myAnnot(..).transform(using <Quotes>)(<tree>)`
55+
val transformedTrees = callMacro(macroInterpreter, tree, annot)
56+
transformedTrees.span(_.symbol != tree.symbol) match
57+
case (prefixed, newTree :: suffixed) =>
58+
allTrees ++= prefixed
59+
insertedAfter = suffixed :: insertedAfter
60+
prefixed.foreach(checkAndEnter(_, tree.symbol, annot))
61+
suffixed.foreach(checkAndEnter(_, tree.symbol, annot))
62+
newTree
63+
case (Nil, Nil) =>
64+
report.error(i"Unexpected `Nil` returned by `(${annot.tree}).transform(..)` during macro expansion", annot.tree.srcPos)
65+
tree
66+
case (_, Nil) =>
67+
report.error(i"Transformed tree for ${tree} was not return by `(${annot.tree}).transform(..)` during macro expansion", annot.tree.srcPos)
68+
tree
69+
else
70+
tree
71+
}
72+
73+
allTrees += transformedTree
74+
insertedAfter.foreach(allTrees.++=)
75+
76+
val result = allTrees.result()
77+
debug.println(result.map(_.show).mkString("expanded to:\n", "\n", ""))
78+
result
79+
80+
/** Interpret the code `new annot(..).transform(using <Quotes(ctx)>)(<tree>)` */
81+
private def callMacro(interpreter: InterpreterMacroAnnot, tree: Tree, annot: Annotation)(using Context): List[DefTree] =
82+
// Interpret macro annotation instantiation `new myAnnot(..)`
83+
// TODO: Interpret as MacroAnnotation when no longer experimental
84+
val annotInstance = interpreter.interpret[Object/*MacroAnnotation*/](annot.tree).get
85+
assert(annotInstance.getClass.getClassLoader.loadClass("scala.annotation.MacroAnnotation").isInstance(annotInstance))
86+
87+
// Call transform `new annot(..).transform(using <Quotes(ctx)>)(<tree>)`
88+
val quotes = QuotesImpl()(using SpliceScope.contextWithNewSpliceScope(tree.symbol.sourcePos)(using MacroExpansion.context(tree)).withOwner(tree.symbol))
89+
// TODO: Call MacroAnnotation.transform directly when no longer experimental
90+
val transformedTrees = interpreter.interpretedMethodCall(annotInstance, defn.QuotedMacroAnnotation_transform)(quotes, tree)(Map.empty)
91+
.asInstanceOf[List[DefTree]]
92+
assert(transformedTrees.forall(_.isInstanceOf[DefTree]))
93+
transformedTrees
94+
95+
/** Check that this tree can be added by the macro annotation and enter it if needed */
96+
private def checkAndEnter(newTree: Tree, annotated: Symbol, annot: Annotation)(using Context) =
97+
val sym = newTree.symbol
98+
if sym.isClass then
99+
report.error("Generating classes is not supported", annot.tree)
100+
else if sym.isType then
101+
report.error("Generating type is not supported", annot.tree)
102+
else if sym.owner != annotated.owner then
103+
report.error(i"Macro annotation $annot added $sym with an inconsistent owner. Expected it to be owned by ${annotated.owner} but was owned by ${sym.owner}.", annot.tree)
104+
else
105+
sym.enteredAfter(thisPhase)
106+
107+
object MacroAnnotations:
108+
109+
/** Is this an annotation that implements `scala.annation.MacroAnnotation` */
110+
def isMacroAnnotation(annot: Annotation)(using Context): Boolean =
111+
val sym = annot.tree.symbol
112+
sym.denot != NoDenotation && sym.owner.derivesFrom(defn.QuotedMacroAnnotationClass)
113+
114+
/** Is this symbol annotated with an annotation that implements `scala.annation.MacroAnnotation` */
115+
def hasMacro(sym: Symbol)(using Context): Boolean =
116+
sym.getAnnotation(defn.QuotedMacroAnnotationClass).isDefined
117+
118+
// TODO: Remove InterpreterMacroAnnot and use Interpreter directly when InterpreterMacroAnnot is no longer experimental
119+
private[MacroAnnotations] class InterpreterMacroAnnot(pos: SrcPos, classLoader: ClassLoader)(using Context) extends Interpreter(pos, classLoader):
120+
override def interpretedMethodCall(inst: Object, fn: Symbol)(args: Object*)(implicit env: Env): Object =
121+
super.interpretedMethodCall(inst, fn)(args*)

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

Lines changed: 13 additions & 0 deletions
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+
checkForMacrosAnnotations(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+
checkForMacrosAnnotations(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+
checkForMacrosAnnotations(tree)
389392
val sym = tree.symbol
390393
if (sym.isClass)
391394
VarianceChecker.check(tree)
392395
annotateExperimental(sym)
396+
checkMacroAnnotation(sym)
393397
tree.rhs match
394398
case impl: Template =>
395399
for parent <- impl.parents do
@@ -483,6 +487,15 @@ 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+
private def checkForMacrosAnnotations(tree: Tree)(using Context) =
491+
if !ctx.compilationUnit.hasMacroAnnotations then
492+
ctx.compilationUnit.hasMacroAnnotations |=
493+
tree.symbol.annotations.exists(MacroAnnotations.isMacroAnnotation)
494+
495+
private def checkMacroAnnotation(sym: Symbol)(using Context) =
496+
if sym.derivesFrom(defn.QuotedMacroAnnotationClass) && !sym.isStatic then
497+
report.error("Implementation restriction: classes that extend MacroAnnotation must not be inner/local classes", sym.srcPos)
498+
486499
private def checkErasedDef(tree: ValOrDefDef)(using Context): Unit =
487500
if tree.symbol.is(Erased, butNot = Macro) then
488501
val tpe = tree.rhs.tpe

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

Lines changed: 1 addition & 0 deletions
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.QuotedMacroAnnotationClass)) ||
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)