Skip to content

Commit

Permalink
Add support for companion in MacroAnnotations
Browse files Browse the repository at this point in the history
  • Loading branch information
hamzaremmal committed Apr 22, 2024
1 parent 54d67e0 commit 2cbc56f
Show file tree
Hide file tree
Showing 74 changed files with 637 additions and 347 deletions.
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/CompilationUnit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import config.{SourceVersion, Feature}
import StdNames.nme
import scala.annotation.internal.sharable
import scala.util.control.NoStackTrace
import transform.MacroAnnotations
import transform.MacroAnnotations.isMacroAnnotation

class CompilationUnit protected (val source: SourceFile, val info: CompilationUnitInfo | Null) {

Expand Down Expand Up @@ -193,7 +193,7 @@ object CompilationUnit {
case _ =>
case _ =>
for annot <- tree.symbol.annotations do
if MacroAnnotations.isMacroAnnotation(annot) then
if annot.isMacroAnnotation then
ctx.compilationUnit.hasMacroAnnotations = true
traverseChildren(tree)
}
Expand Down
80 changes: 80 additions & 0 deletions compiler/src/dotty/tools/dotc/ast/TreeMapWithTrackedStats.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package dotty.tools.dotc
package ast

import tpd.*
import core.Contexts.*
import core.Symbols.*
import util.Property

import scala.collection.mutable

abstract class TreeMapWithTrackedStats extends TreeMapWithImplicits:

import TreeMapWithTrackedStats.*

// It is safe to assume that the companion of a tree is in the same scope
// Therefore, when expanding MacroAnnotations, we will only keep track of
// the trees in the same scope as the current transformed tree

protected final def getTracked(sym: Symbol)(using Context): Option[MemberDef] =
for trees <- ctx.property(TrackedTrees)
tree <- trees.get(sym)
yield tree

protected final def updateTracked(tree: Tree)(using Context): Tree =
tree match
case tree: MemberDef =>
trackedTrees.update(tree.symbol, tree)
tree
case _ => tree
end updateTracked

private final def withUpdatedTree(stats: List[Tree])(using Context) =
val trackedTrees = TreeMapWithTrackedStats.trackedTrees
stats.mapConserve:
case tree: MemberDef if trackedTrees.contains(tree.symbol) =>
trackedTrees(tree.symbol)
case stat => stat

override def transform(tree: Tree)(using Context): Tree =
tree match
case PackageDef(_, stats) =>
// Step I: Collect and memoize all the stats
inContext(trackedTreesCtx(stats)):
// Step II: Transform the tree
val result = super.transform(tree)
// Step III: Reconcile between the symbols in syms and the tree
(result: @unchecked) match
case pkg@PackageDef(pid, stats) =>
cpy.PackageDef(pkg)(pid = pid, stats = withUpdatedTree(stats))
case block: Block =>
// Step I: Collect all the member definitions in the block
inContext(trackedTreesCtx(block.stats)):
// Step II: Transform the tree
val result = super.transform(tree)
// Step III: Reconcile between the symbols in syms and the tree
(result: @unchecked) match
case b@Block(stats, expr) =>
cpy.Block(b)(expr = expr, stats = withUpdatedTree(stats))
case TypeDef(_, impl: Template) =>
// Step I: Collect and memoize all the stats
inContext(trackedTreesCtx(impl.body)):
// Step II: Transform the tree
val result = super.transform(tree)
// Step III: Reconcile between the symbols in syms and the tree
(result : @unchecked) match
case tree@TypeDef(name, impl: Template) =>
cpy.TypeDef(tree)(rhs = cpy.Template(impl)(body = withUpdatedTree(impl.body)))
case _ => super.transform(tree)

end TreeMapWithTrackedStats

object TreeMapWithTrackedStats:
private val TrackedTrees = new Property.Key[mutable.Map[Symbol, tpd.MemberDef]]

private def trackedTrees(using Context): mutable.Map[Symbol, MemberDef] =
ctx.property(TrackedTrees).get

private def trackedTreesCtx(stats: List[Tree])(using Context): Context =
val treesToTrack = stats.collect { case m: MemberDef => (m.symbol, m) }
ctx.fresh.setProperty(TrackedTrees, mutable.Map(treesToTrack*))
89 changes: 58 additions & 31 deletions compiler/src/dotty/tools/dotc/transform/Inlining.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package dotty.tools.dotc
package transform

import ast.tpd
import ast.Trees.*
import ast.TreeMapWithTrackedStats
import core.*
import Flags.*
import Decorators.*
import Contexts.*
import Symbols.*
import Decorators.*
import config.Printers.inlining
import DenotTransformers.IdentityDenotTransformer
import MacroAnnotations.hasMacroAnnotation
import inlines.Inlines
import quoted.*
import staging.StagingLevel
import util.Property

import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.ast.Trees.*
import dotty.tools.dotc.quoted.*
import dotty.tools.dotc.inlines.Inlines
import dotty.tools.dotc.ast.TreeMapWithImplicits
import dotty.tools.dotc.core.DenotTransformers.IdentityDenotTransformer
import dotty.tools.dotc.staging.StagingLevel

import scala.collection.mutable.ListBuffer
import scala.collection.mutable

/** Inlines all calls to inline methods that are not in an inline method or a quote */
class Inlining extends MacroTransform, IdentityDenotTransformer {
Expand Down Expand Up @@ -56,38 +60,21 @@ class Inlining extends MacroTransform, IdentityDenotTransformer {

def newTransformer(using Context): Transformer = new Transformer {
override def transform(tree: tpd.Tree)(using Context): tpd.Tree =
new InliningTreeMap().transform(tree)
InliningTreeMap().transform(tree)
}

private class InliningTreeMap extends TreeMapWithImplicits {
private class InliningTreeMap extends TreeMapWithTrackedStats {

/** List of top level classes added by macro annotation in a package object.
* These are added to the PackageDef that owns this particular package object.
*/
private val newTopClasses = MutableSymbolMap[ListBuffer[Tree]]()
private val newTopClasses = MutableSymbolMap[mutable.ListBuffer[Tree]]()

override def transform(tree: Tree)(using Context): Tree = {
tree match
case tree: MemberDef =>
if tree.symbol.is(Inline) then tree
else if tree.symbol.is(Param) then super.transform(tree)
else if
!tree.symbol.isPrimaryConstructor
&& StagingLevel.level == 0
&& MacroAnnotations.hasMacroAnnotation(tree.symbol)
then
val trees = (new MacroAnnotations(self)).expandAnnotations(tree)
val trees1 = trees.map(super.transform)

// Find classes added to the top level from a package object
val (topClasses, trees2) =
if ctx.owner.isPackageObject then trees1.partition(_.symbol.owner == ctx.owner.owner)
else (Nil, trees1)
if topClasses.nonEmpty then
newTopClasses.getOrElseUpdate(ctx.owner.owner, new ListBuffer) ++= topClasses

flatTree(trees2)
else super.transform(tree)
// Fetch the latest tracked tree (It might have already been processed by its companion)
transformMemberDef(getTracked(tree.symbol).getOrElse(tree))
case _: Typed | _: Block =>
super.transform(tree)
case _: PackageDef =>
Expand All @@ -113,7 +100,47 @@ class Inlining extends MacroTransform, IdentityDenotTransformer {
else Inlines.inlineCall(tree1)
else super.transform(tree)
}

private def transformMemberDef(tree: MemberDef)(using Context) : Tree =
if tree.symbol.is(Inline) then tree
else if tree.symbol.is(Param) then
super.transform(tree)
else if
!tree.symbol.isPrimaryConstructor
&& StagingLevel.level == 0
&& tree.symbol.hasMacroAnnotation
then
// Fetch the companion's tree
val companionSym =
if tree.symbol.is(ModuleClass) then tree.symbol.companionClass
else if tree.symbol.is(ModuleVal) then NoSymbol
else tree.symbol.companionModule.moduleClass

// Expand and process MacroAnnotations
val (trees, companion) = MacroAnnotations.expandAnnotations(tree, getTracked(companionSym))

// Enter the new symbols & Update the tracked trees
(companion.toList ::: trees).foreach: tree =>
MacroAnnotations.enterMissingSymbols(tree, self)
updateTracked(tree)

// Perform inlining on the expansion of the annotations
val trees1 = trees.map(super.transform)
trees1.foreach(updateTracked)

// Find classes added to the top level from a package object
val (topClasses, trees2) =
if ctx.owner.isPackageObject then trees1.partition(_.symbol.owner == ctx.owner.owner)
else (Nil, trees1)
if topClasses.nonEmpty then
newTopClasses.getOrElseUpdate(ctx.owner.owner, new mutable.ListBuffer) ++= topClasses
flatTree(trees2)
else
updateTracked(super.transform(tree))
end transformMemberDef

}

}

object Inlining:
Expand Down
Loading

0 comments on commit 2cbc56f

Please sign in to comment.