Skip to content

Commit 725e0c8

Browse files
Merge pull request #9625 from dotty-staging/cleanup-dotty.tools.dotc.quoted
Move all macro related classes to dotty.tools.dotc.quoted.[reflect]
2 parents 033c690 + 795dd8b commit 725e0c8

20 files changed

+84
-95
lines changed

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ import scala.annotation.{switch, tailrec}
3737
import scala.collection.mutable.ListBuffer
3838
import scala.collection.mutable
3939
import config.Printers.pickling
40-
import core.quoted.PickledQuotes
41-
import dotty.tools.dotc.quoted.QuoteContext
40+
import quoted.PickledQuotes
4241

4342
import dotty.tools.tasty.TastyFormat._
4443

compiler/src/dotty/tools/dotc/decompiler/DecompilationPrinter.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import scala.io.Codec
88
import dotty.tools.dotc.core.Contexts._
99
import dotty.tools.dotc.core.Phases.Phase
1010
import dotty.tools.dotc.core.tasty.TastyPrinter
11-
import dotty.tools.dotc.tastyreflect.ReflectionImpl
11+
import dotty.tools.dotc.quoted.QuoteContextImpl
1212
import dotty.tools.io.File
1313

1414
/** Phase that prints the trees in all loaded compilation units.
@@ -43,7 +43,7 @@ class DecompilationPrinter extends Phase {
4343
else {
4444
val unitFile = unit.source.toString.replace("\\", "/").replace(".class", ".tasty")
4545
out.println(s"/** Decompiled from $unitFile */")
46-
out.println(ReflectionImpl.showTree(unit.tpdTree))
46+
out.println(QuoteContextImpl.showTree(unit.tpdTree))
4747
}
4848
}
4949
}

compiler/src/dotty/tools/dotc/decompiler/IDEDecompilerDriver.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import dotty.tools.dotc.core.Contexts._
66
import dotty.tools.dotc.core._
77
import dotty.tools.dotc.core.tasty.TastyHTMLPrinter
88
import dotty.tools.dotc.reporting._
9-
import dotty.tools.dotc.tastyreflect.ReflectionImpl
9+
import dotty.tools.dotc.quoted.QuoteContextImpl
1010

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

37-
val decompiled = ReflectionImpl.showTree(unit.tpdTree)
37+
val decompiled = QuoteContextImpl.showTree(unit.tpdTree)
3838
val tree = new TastyHTMLPrinter(unit.pickled.head._2()).printContents()
3939

4040
reporter.removeBufferedMessages.foreach(message => System.err.println(message))

compiler/src/dotty/tools/dotc/tastyreflect/MacroExpansion.scala renamed to compiler/src/dotty/tools/dotc/quoted/MacroExpansion.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dotty.tools.dotc.tastyreflect
1+
package dotty.tools.dotc.quoted
22

33
import dotty.tools.dotc.ast.tpd
44
import dotty.tools.dotc.core._

compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala renamed to compiler/src/dotty/tools/dotc/quoted/PickledQuotes.scala

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dotty.tools.dotc.core.quoted
1+
package dotty.tools.dotc.quoted
22

33
import dotty.tools.dotc.ast.Trees._
44
import dotty.tools.dotc.ast.{TreeTypeMap, tpd}
@@ -15,14 +15,13 @@ import dotty.tools.dotc.core.tasty.TreePickler.Hole
1515
import dotty.tools.dotc.core.tasty.{ PositionPickler, TastyPickler, TastyPrinter }
1616
import dotty.tools.dotc.core.tasty.DottyUnpickler
1717
import dotty.tools.dotc.core.tasty.TreeUnpickler.UnpickleMode
18-
import dotty.tools.dotc.quoted.QuoteContext
19-
import dotty.tools.dotc.tastyreflect.ReflectionImpl
2018

2119
import dotty.tools.tasty.TastyString
2220

2321
import scala.reflect.ClassTag
2422

2523
import scala.internal.quoted.Unpickler._
24+
import scala.quoted.QuoteContext
2625

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

4645
/** Transform the expression into its fully spliced TypeTree */
4746
def quotedTypeToTree(tpe: quoted.Type[?])(using Context): Tree = {
4847
val tpe1 = tpe.asInstanceOf[scala.internal.quoted.Type[Tree]]
49-
QuoteContext.checkScopeId(tpe1.scopeId)
48+
QuoteContextImpl.checkScopeId(tpe1.scopeId)
5049
healOwner(tpe1.typeTree)
5150
}
5251

@@ -76,12 +75,12 @@ object PickledQuotes {
7675
override def transform(tree: tpd.Tree)(using Context): tpd.Tree = tree match {
7776
case Hole(isTerm, idx, args) =>
7877
val reifiedArgs = args.map { arg =>
79-
if (arg.isTerm) (using qctx: scala.quoted.QuoteContext) => new scala.internal.quoted.Expr(arg, QuoteContext.scopeId)
80-
else new scala.internal.quoted.Type(arg, QuoteContext.scopeId)
78+
if (arg.isTerm) (using qctx: QuoteContext) => new scala.internal.quoted.Expr(arg, QuoteContextImpl.scopeId)
79+
else new scala.internal.quoted.Type(arg, QuoteContextImpl.scopeId)
8180
}
8281
if isTerm then
83-
val splice1 = splices(idx).asInstanceOf[Seq[Any] => scala.quoted.QuoteContext ?=> quoted.Expr[?]]
84-
val quotedExpr = splice1(reifiedArgs)(using dotty.tools.dotc.quoted.QuoteContext())
82+
val splice1 = splices(idx).asInstanceOf[Seq[Any] => QuoteContext ?=> quoted.Expr[?]]
83+
val quotedExpr = splice1(reifiedArgs)(using dotty.tools.dotc.quoted.QuoteContextImpl())
8584
val filled = PickledQuotes.quotedExprToTree(quotedExpr)
8685

8786
// We need to make sure a hole is created with the source file of the surrounding context, even if

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

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package dotty.tools.dotc.quoted
2+
3+
import dotty.tools.dotc.ast.tpd
4+
import dotty.tools.dotc.core.Contexts._
5+
import dotty.tools.dotc.quoted.reflect._
6+
7+
import scala.quoted.QuoteContext
8+
import scala.quoted.show.SyntaxHighlight
9+
10+
object QuoteContextImpl {
11+
12+
type ScopeId = Int
13+
14+
def apply()(using Context): QuoteContext =
15+
new QuoteContextImpl(ctx)
16+
17+
def showTree(tree: tpd.Tree)(using Context): String = {
18+
val qctx = QuoteContextImpl()(using MacroExpansion.context(tree))
19+
val syntaxHighlight =
20+
if (ctx.settings.color.value == "always") SyntaxHighlight.ANSI
21+
else SyntaxHighlight.plain
22+
show(using qctx)(tree.asInstanceOf[qctx.tasty.Tree], syntaxHighlight)(using ctx.asInstanceOf[qctx.tasty.Context])
23+
}
24+
25+
private def show(using qctx: QuoteContext)(tree: qctx.tasty.Tree, syntaxHighlight: SyntaxHighlight)(using qctx.tasty.Context) =
26+
tree.showWith(syntaxHighlight)
27+
28+
private[dotty] def checkScopeId(id: ScopeId)(using Context): Unit =
29+
if (id != scopeId)
30+
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
31+
32+
// TODO Explore more fine grained scope ids.
33+
// This id can only differentiate scope extrusion from one compiler instance to another.
34+
private[dotty] def scopeId(using Context): ScopeId =
35+
ctx.outersIterator.toList.last.hashCode()
36+
37+
}
38+
39+
class QuoteContextImpl private (ctx: Context) extends QuoteContext {
40+
// NOTE: The tasty class should only mixin the compiler interface and the reflection interface.
41+
// We should not implement methods here, all should be implemented by `ReflectionCompilerInterface`
42+
val tasty = new ReflectionCompilerInterface(ctx) with scala.tasty.Reflection
43+
}

compiler/src/dotty/tools/dotc/tastyreflect/FromSymbol.scala renamed to compiler/src/dotty/tools/dotc/quoted/reflect/FromSymbol.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package dotty.tools.dotc.tastyreflect
1+
package dotty.tools.dotc.quoted
2+
package reflect
23

34
import dotty.tools.dotc.ast.tpd
45
import dotty.tools.dotc.ast.untpd

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala renamed to compiler/src/dotty/tools/dotc/quoted/reflect/ReflectionCompilerInterface.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dotty.tools.dotc
2-
package tastyreflect
2+
package quoted
3+
package reflect
34

45
import dotty.tools.dotc.ast.Trees._
56
import dotty.tools.dotc.ast.{TreeTypeMap, Trees, tpd, untpd}
@@ -8,11 +9,11 @@ import dotty.tools.dotc.core._
89
import dotty.tools.dotc.core.Flags._
910
import dotty.tools.dotc.core.Contexts._
1011
import dotty.tools.dotc.core.StdNames._
11-
import dotty.tools.dotc.core.quoted.PickledQuotes
1212
import dotty.tools.dotc.core.Symbols._
1313
import dotty.tools.dotc.core.Decorators._
1414
import dotty.tools.dotc.core.Types.SingletonType
15-
import dotty.tools.dotc.tastyreflect.FromSymbol.{definitionFromSym, packageDefFromSym}
15+
import dotty.tools.dotc.quoted._
16+
import dotty.tools.dotc.quoted.reflect.FromSymbol.{definitionFromSym, packageDefFromSym}
1617
import dotty.tools.dotc.typer.Implicits.{AmbiguousImplicits, DivergingImplicit, NoMatchingImplicits, SearchFailure, SearchFailureType}
1718
import dotty.tools.dotc.util.{SourceFile, SourcePosition, Spans}
1819

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

3233
def rootPosition: util.SourcePosition =
33-
tastyreflect.MacroExpansion.position.getOrElse(SourcePosition(rootContext.source, Spans.NoSpan))
34+
MacroExpansion.position.getOrElse(SourcePosition(rootContext.source, Spans.NoSpan))
3435

3536

3637
//////////////////////

compiler/src/dotty/tools/dotc/tastyreflect/package.scala renamed to compiler/src/dotty/tools/dotc/quoted/reflect/package.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
package dotty.tools.dotc
1+
package dotty.tools.dotc.quoted
22

33
import dotty.tools.dotc.ast.Trees.{Tree, Untyped}
44
import dotty.tools.dotc.core.Contexts._
55
import dotty.tools.dotc.core.Symbols.Symbol
66
import dotty.tools.dotc.core.Types.Type
7+
import dotty.tools.dotc.util.SourceFile
78
import dotty.tools.dotc.core.SymDenotations.SymDenotation
89
import scala.annotation.constructorOnly
9-
import util.SourceFile
1010

11-
package object tastyreflect {
11+
package object reflect {
1212

1313
type PackageDefinition = PackageDefinitionImpl[Type]
1414

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

1919
override def denot(using Context): SymDenotation = sym.denot

0 commit comments

Comments
 (0)