Skip to content

Move all macro related classes to dotty.tools.dotc.quoted.[reflect] #9625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ import scala.annotation.{switch, tailrec}
import scala.collection.mutable.ListBuffer
import scala.collection.mutable
import config.Printers.pickling
import core.quoted.PickledQuotes
import dotty.tools.dotc.quoted.QuoteContext
import quoted.PickledQuotes

import dotty.tools.tasty.TastyFormat._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import scala.io.Codec
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Phases.Phase
import dotty.tools.dotc.core.tasty.TastyPrinter
import dotty.tools.dotc.tastyreflect.ReflectionImpl
import dotty.tools.dotc.quoted.QuoteContextImpl
import dotty.tools.io.File

/** Phase that prints the trees in all loaded compilation units.
Expand Down Expand Up @@ -43,7 +43,7 @@ class DecompilationPrinter extends Phase {
else {
val unitFile = unit.source.toString.replace("\\", "/").replace(".class", ".tasty")
out.println(s"/** Decompiled from $unitFile */")
out.println(ReflectionImpl.showTree(unit.tpdTree))
out.println(QuoteContextImpl.showTree(unit.tpdTree))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core._
import dotty.tools.dotc.core.tasty.TastyHTMLPrinter
import dotty.tools.dotc.reporting._
import dotty.tools.dotc.tastyreflect.ReflectionImpl
import dotty.tools.dotc.quoted.QuoteContextImpl

/**
* Decompiler to be used with IDEs
Expand Down Expand Up @@ -34,7 +34,7 @@ class IDEDecompilerDriver(val settings: List[String]) extends dotc.Driver {
run.printSummary()
val unit = ctx.run.units.head

val decompiled = ReflectionImpl.showTree(unit.tpdTree)
val decompiled = QuoteContextImpl.showTree(unit.tpdTree)
val tree = new TastyHTMLPrinter(unit.pickled.head._2()).printContents()

reporter.removeBufferedMessages.foreach(message => System.err.println(message))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dotty.tools.dotc.tastyreflect
package dotty.tools.dotc.quoted

import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.core._
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dotty.tools.dotc.core.quoted
package dotty.tools.dotc.quoted

import dotty.tools.dotc.ast.Trees._
import dotty.tools.dotc.ast.{TreeTypeMap, tpd}
Expand All @@ -15,14 +15,13 @@ import dotty.tools.dotc.core.tasty.TreePickler.Hole
import dotty.tools.dotc.core.tasty.{ PositionPickler, TastyPickler, TastyPrinter }
import dotty.tools.dotc.core.tasty.DottyUnpickler
import dotty.tools.dotc.core.tasty.TreeUnpickler.UnpickleMode
import dotty.tools.dotc.quoted.QuoteContext
import dotty.tools.dotc.tastyreflect.ReflectionImpl

import dotty.tools.tasty.TastyString

import scala.reflect.ClassTag

import scala.internal.quoted.Unpickler._
import scala.quoted.QuoteContext

object PickledQuotes {
import tpd._
Expand All @@ -39,14 +38,14 @@ object PickledQuotes {
/** Transform the expression into its fully spliced Tree */
def quotedExprToTree[T](expr: quoted.Expr[T])(using Context): Tree = {
val expr1 = expr.asInstanceOf[scala.internal.quoted.Expr[Tree]]
QuoteContext.checkScopeId(expr1.scopeId)
QuoteContextImpl.checkScopeId(expr1.scopeId)
healOwner(expr1.tree)
}

/** Transform the expression into its fully spliced TypeTree */
def quotedTypeToTree(tpe: quoted.Type[?])(using Context): Tree = {
val tpe1 = tpe.asInstanceOf[scala.internal.quoted.Type[Tree]]
QuoteContext.checkScopeId(tpe1.scopeId)
QuoteContextImpl.checkScopeId(tpe1.scopeId)
healOwner(tpe1.typeTree)
}

Expand Down Expand Up @@ -76,12 +75,12 @@ object PickledQuotes {
override def transform(tree: tpd.Tree)(using Context): tpd.Tree = tree match {
case Hole(isTerm, idx, args) =>
val reifiedArgs = args.map { arg =>
if (arg.isTerm) (using qctx: scala.quoted.QuoteContext) => new scala.internal.quoted.Expr(arg, QuoteContext.scopeId)
else new scala.internal.quoted.Type(arg, QuoteContext.scopeId)
if (arg.isTerm) (using qctx: QuoteContext) => new scala.internal.quoted.Expr(arg, QuoteContextImpl.scopeId)
else new scala.internal.quoted.Type(arg, QuoteContextImpl.scopeId)
}
if isTerm then
val splice1 = splices(idx).asInstanceOf[Seq[Any] => scala.quoted.QuoteContext ?=> quoted.Expr[?]]
val quotedExpr = splice1(reifiedArgs)(using dotty.tools.dotc.quoted.QuoteContext())
val splice1 = splices(idx).asInstanceOf[Seq[Any] => QuoteContext ?=> quoted.Expr[?]]
val quotedExpr = splice1(reifiedArgs)(using dotty.tools.dotc.quoted.QuoteContextImpl())
val filled = PickledQuotes.quotedExprToTree(quotedExpr)

// We need to make sure a hole is created with the source file of the surrounding context, even if
Expand Down
23 changes: 0 additions & 23 deletions compiler/src/dotty/tools/dotc/quoted/QuoteContext.scala

This file was deleted.

43 changes: 43 additions & 0 deletions compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dotty.tools.dotc.quoted

import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.quoted.reflect._

import scala.quoted.QuoteContext
import scala.quoted.show.SyntaxHighlight

object QuoteContextImpl {

type ScopeId = Int

def apply()(using Context): QuoteContext =
new QuoteContextImpl(ctx)

def showTree(tree: tpd.Tree)(using Context): String = {
val qctx = QuoteContextImpl()(using MacroExpansion.context(tree))
val syntaxHighlight =
if (ctx.settings.color.value == "always") SyntaxHighlight.ANSI
else SyntaxHighlight.plain
show(using qctx)(tree.asInstanceOf[qctx.tasty.Tree], syntaxHighlight)(using ctx.asInstanceOf[qctx.tasty.Context])
}

private def show(using qctx: QuoteContext)(tree: qctx.tasty.Tree, syntaxHighlight: SyntaxHighlight)(using qctx.tasty.Context) =
tree.showWith(syntaxHighlight)

private[dotty] def checkScopeId(id: ScopeId)(using Context): Unit =
if (id != scopeId)
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")

// TODO Explore more fine grained scope ids.
// This id can only differentiate scope extrusion from one compiler instance to another.
private[dotty] def scopeId(using Context): ScopeId =
ctx.outersIterator.toList.last.hashCode()

}

class QuoteContextImpl private (ctx: Context) extends QuoteContext {
// NOTE: The tasty class should only mixin the compiler interface and the reflection interface.
// We should not implement methods here, all should be implemented by `ReflectionCompilerInterface`
val tasty = new ReflectionCompilerInterface(ctx) with scala.tasty.Reflection
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package dotty.tools.dotc.tastyreflect
package dotty.tools.dotc.quoted
package reflect

import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.ast.untpd
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dotty.tools.dotc
package tastyreflect
package quoted
package reflect

import dotty.tools.dotc.ast.Trees._
import dotty.tools.dotc.ast.{TreeTypeMap, Trees, tpd, untpd}
Expand All @@ -8,11 +9,11 @@ import dotty.tools.dotc.core._
import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.StdNames._
import dotty.tools.dotc.core.quoted.PickledQuotes
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.Decorators._
import dotty.tools.dotc.core.Types.SingletonType
import dotty.tools.dotc.tastyreflect.FromSymbol.{definitionFromSym, packageDefFromSym}
import dotty.tools.dotc.quoted._
import dotty.tools.dotc.quoted.reflect.FromSymbol.{definitionFromSym, packageDefFromSym}
import dotty.tools.dotc.typer.Implicits.{AmbiguousImplicits, DivergingImplicit, NoMatchingImplicits, SearchFailure, SearchFailureType}
import dotty.tools.dotc.util.{SourceFile, SourcePosition, Spans}

Expand All @@ -30,7 +31,7 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte
private given core.Contexts.Context = rootContext

def rootPosition: util.SourcePosition =
tastyreflect.MacroExpansion.position.getOrElse(SourcePosition(rootContext.source, Spans.NoSpan))
MacroExpansion.position.getOrElse(SourcePosition(rootContext.source, Spans.NoSpan))


//////////////////////
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package dotty.tools.dotc
package dotty.tools.dotc.quoted

import dotty.tools.dotc.ast.Trees.{Tree, Untyped}
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Symbols.Symbol
import dotty.tools.dotc.core.Types.Type
import dotty.tools.dotc.util.SourceFile
import dotty.tools.dotc.core.SymDenotations.SymDenotation
import scala.annotation.constructorOnly
import util.SourceFile

package object tastyreflect {
package object reflect {

type PackageDefinition = PackageDefinitionImpl[Type]

/** Represents the symbol of a definition in tree form */
case class PackageDefinitionImpl[-T >: Untyped] private[tastyreflect] (sym: Symbol)(implicit @constructorOnly src: SourceFile) extends Tree[T] {
case class PackageDefinitionImpl[-T >: Untyped] private[reflect] (sym: Symbol)(implicit @constructorOnly src: SourceFile) extends Tree[T] {
type ThisTree[-T >: Untyped] = PackageDefinitionImpl[T]

override def denot(using Context): SymDenotation = sym.denot
Expand Down
27 changes: 0 additions & 27 deletions compiler/src/dotty/tools/dotc/tastyreflect/ReflectionImpl.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import dotty.tools.dotc.core.Constants._
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Decorators._
import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.quoted._
import dotty.tools.dotc.core.NameKinds._
import dotty.tools.dotc.core.StagingContext._
import dotty.tools.dotc.core.StdNames._
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.Types._
import dotty.tools.dotc.quoted._
import dotty.tools.dotc.util.SrcPos
import dotty.tools.dotc.util.Spans._
import dotty.tools.dotc.transform.SymUtils._
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import scala.collection.mutable
import dotty.tools.dotc.core.Annotations._
import dotty.tools.dotc.core.Names._
import dotty.tools.dotc.core.StdNames._
import dotty.tools.dotc.core.quoted._
import dotty.tools.dotc.quoted._
import dotty.tools.dotc.transform.TreeMapWithStages._
import dotty.tools.dotc.typer.Inliner

Expand Down
13 changes: 6 additions & 7 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.NameKinds.FlatName
import dotty.tools.dotc.core.Names.{Name, TermName}
import dotty.tools.dotc.core.StdNames._
import dotty.tools.dotc.core.quoted._
import dotty.tools.dotc.core.Types._
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.Denotations.staticRef
import dotty.tools.dotc.core.{NameKinds, TypeErasure}
import dotty.tools.dotc.core.Constants.Constant
import dotty.tools.dotc.tastyreflect.ReflectionImpl

import scala.util.control.NonFatal
import dotty.tools.dotc.util.SrcPos
import dotty.tools.repl.AbstractFileClassLoader

import scala.reflect.ClassTag

import dotty.tools.dotc.quoted.QuoteContext
import dotty.tools.dotc.quoted._
import scala.quoted.QuoteContext

/** Utility class to splice quoted expressions */
object Splicer {
Expand All @@ -50,8 +49,8 @@ object Splicer {
val interpreter = new Interpreter(pos, classLoader)

// Some parts of the macro are evaluated during the unpickling performed in quotedExprToTree
val interpretedExpr = interpreter.interpret[scala.quoted.QuoteContext => scala.quoted.Expr[Any]](tree)
val interpretedTree = interpretedExpr.fold(tree)(macroClosure => PickledQuotes.quotedExprToTree(macroClosure(QuoteContext())))
val interpretedExpr = interpreter.interpret[QuoteContext => scala.quoted.Expr[Any]](tree)
val interpretedTree = interpretedExpr.fold(tree)(macroClosure => PickledQuotes.quotedExprToTree(macroClosure(QuoteContextImpl())))

checkEscapedVariables(interpretedTree, macroOwner)
} finally {
Expand Down Expand Up @@ -304,10 +303,10 @@ object Splicer {
}

private def interpretQuote(tree: Tree)(implicit env: Env): Object =
new scala.internal.quoted.Expr(Inlined(EmptyTree, Nil, PickledQuotes.healOwner(tree)).withSpan(tree.span), QuoteContext.scopeId)
new scala.internal.quoted.Expr(Inlined(EmptyTree, Nil, PickledQuotes.healOwner(tree)).withSpan(tree.span), QuoteContextImpl.scopeId)

private def interpretTypeQuote(tree: Tree)(implicit env: Env): Object =
new scala.internal.quoted.Type(PickledQuotes.healOwner(tree), QuoteContext.scopeId)
new scala.internal.quoted.Type(PickledQuotes.healOwner(tree), QuoteContextImpl.scopeId)

private def interpretLiteral(value: Any)(implicit env: Env): Object =
value.asInstanceOf[Object]
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/Staging.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Phases._
import dotty.tools.dotc.core.Decorators._
import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.quoted._
import dotty.tools.dotc.core.NameKinds._
import dotty.tools.dotc.core.StagingContext._
import dotty.tools.dotc.core.StdNames._
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.tasty.TreePickler.Hole
import dotty.tools.dotc.core.Types._
import dotty.tools.dotc.quoted._
import dotty.tools.dotc.util.{SourceFile, SrcPos}
import dotty.tools.dotc.transform.SymUtils._
import dotty.tools.dotc.transform.TreeMapWithStages._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import dotty.tools.dotc.config.Printers.staging
import dotty.tools.dotc.core.Constants._
import dotty.tools.dotc.core.Decorators._
import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.quoted._
import dotty.tools.dotc.core.NameKinds._
import dotty.tools.dotc.core.Types._
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.StagingContext._
import dotty.tools.dotc.core.StdNames._
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.tasty.TreePickler.Hole
import dotty.tools.dotc.quoted._
import dotty.tools.dotc.util.Spans._
import dotty.tools.dotc.util.Property
import dotty.tools.dotc.transform.SymUtils._
Expand Down
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import SymDenotations.SymDenotation
import Inferencing.isFullyDefined
import config.Printers.inlining
import ErrorReporting.errorTree
import dotty.tools.dotc.tastyreflect.ReflectionImpl
import dotty.tools.dotc.util.{SimpleIdentityMap, SimpleIdentitySet, SourceFile, SourcePosition, SrcPos}
import dotty.tools.dotc.parsing.Parsers.Parser
import Nullables.{given _}
Expand Down Expand Up @@ -1410,7 +1409,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
if suspendable then
ctx.compilationUnit.suspend() // this throws a SuspendException

val evaluatedSplice = inContext(tastyreflect.MacroExpansion.context(inlinedFrom)) {
val evaluatedSplice = inContext(quoted.MacroExpansion.context(inlinedFrom)) {
Splicer.splice(body, inlinedFrom.srcPos, MacroClassLoader.fromContext)
}
val inlinedNormailizer = new TreeMap {
Expand Down
Loading