|
| 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 transform(tree: MemberDef)(using Context): List[DefTree] = |
| 31 | + if !hasMacro(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 hasMacro(sym: Symbol)(using Context): Boolean = |
| 119 | + sym.getAnnotation(defn.TastyAnnotationClass).isDefined |
0 commit comments