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

+1-2
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

+2-2
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

+2-2
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

+1-1
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

+8-9
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

-23
This file was deleted.
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

+2-1
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

+5-4
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

+4-4
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

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionImpl.scala

-27
This file was deleted.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import dotty.tools.dotc.core.Constants._
88
import dotty.tools.dotc.core.Contexts._
99
import dotty.tools.dotc.core.Decorators._
1010
import dotty.tools.dotc.core.Flags._
11-
import dotty.tools.dotc.core.quoted._
1211
import dotty.tools.dotc.core.NameKinds._
1312
import dotty.tools.dotc.core.StagingContext._
1413
import dotty.tools.dotc.core.StdNames._
1514
import dotty.tools.dotc.core.Symbols._
1615
import dotty.tools.dotc.core.Types._
16+
import dotty.tools.dotc.quoted._
1717
import dotty.tools.dotc.util.SrcPos
1818
import dotty.tools.dotc.util.Spans._
1919
import dotty.tools.dotc.transform.SymUtils._

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import scala.collection.mutable
2121
import dotty.tools.dotc.core.Annotations._
2222
import dotty.tools.dotc.core.Names._
2323
import dotty.tools.dotc.core.StdNames._
24-
import dotty.tools.dotc.core.quoted._
24+
import dotty.tools.dotc.quoted._
2525
import dotty.tools.dotc.transform.TreeMapWithStages._
2626
import dotty.tools.dotc.typer.Inliner
2727

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,20 @@ import dotty.tools.dotc.core.Flags._
1212
import dotty.tools.dotc.core.NameKinds.FlatName
1313
import dotty.tools.dotc.core.Names.{Name, TermName}
1414
import dotty.tools.dotc.core.StdNames._
15-
import dotty.tools.dotc.core.quoted._
1615
import dotty.tools.dotc.core.Types._
1716
import dotty.tools.dotc.core.Symbols._
1817
import dotty.tools.dotc.core.Denotations.staticRef
1918
import dotty.tools.dotc.core.{NameKinds, TypeErasure}
2019
import dotty.tools.dotc.core.Constants.Constant
21-
import dotty.tools.dotc.tastyreflect.ReflectionImpl
2220

2321
import scala.util.control.NonFatal
2422
import dotty.tools.dotc.util.SrcPos
2523
import dotty.tools.repl.AbstractFileClassLoader
2624

2725
import scala.reflect.ClassTag
2826

29-
import dotty.tools.dotc.quoted.QuoteContext
27+
import dotty.tools.dotc.quoted._
28+
import scala.quoted.QuoteContext
3029

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

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

5655
checkEscapedVariables(interpretedTree, macroOwner)
5756
} finally {
@@ -304,10 +303,10 @@ object Splicer {
304303
}
305304

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

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

312311
private def interpretLiteral(value: Any)(implicit env: Env): Object =
313312
value.asInstanceOf[Object]

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import dotty.tools.dotc.core.Contexts._
88
import dotty.tools.dotc.core.Phases._
99
import dotty.tools.dotc.core.Decorators._
1010
import dotty.tools.dotc.core.Flags._
11-
import dotty.tools.dotc.core.quoted._
1211
import dotty.tools.dotc.core.NameKinds._
1312
import dotty.tools.dotc.core.StagingContext._
1413
import dotty.tools.dotc.core.StdNames._
1514
import dotty.tools.dotc.core.Symbols._
1615
import dotty.tools.dotc.core.tasty.TreePickler.Hole
1716
import dotty.tools.dotc.core.Types._
17+
import dotty.tools.dotc.quoted._
1818
import dotty.tools.dotc.util.{SourceFile, SrcPos}
1919
import dotty.tools.dotc.transform.SymUtils._
2020
import dotty.tools.dotc.transform.TreeMapWithStages._

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import dotty.tools.dotc.config.Printers.staging
77
import dotty.tools.dotc.core.Constants._
88
import dotty.tools.dotc.core.Decorators._
99
import dotty.tools.dotc.core.Flags._
10-
import dotty.tools.dotc.core.quoted._
1110
import dotty.tools.dotc.core.NameKinds._
1211
import dotty.tools.dotc.core.Types._
1312
import dotty.tools.dotc.core.Contexts._
1413
import dotty.tools.dotc.core.StagingContext._
1514
import dotty.tools.dotc.core.StdNames._
1615
import dotty.tools.dotc.core.Symbols._
1716
import dotty.tools.dotc.core.tasty.TreePickler.Hole
17+
import dotty.tools.dotc.quoted._
1818
import dotty.tools.dotc.util.Spans._
1919
import dotty.tools.dotc.util.Property
2020
import dotty.tools.dotc.transform.SymUtils._

compiler/src/dotty/tools/dotc/typer/Inliner.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import SymDenotations.SymDenotation
2121
import Inferencing.isFullyDefined
2222
import config.Printers.inlining
2323
import ErrorReporting.errorTree
24-
import dotty.tools.dotc.tastyreflect.ReflectionImpl
2524
import dotty.tools.dotc.util.{SimpleIdentityMap, SimpleIdentitySet, SourceFile, SourcePosition, SrcPos}
2625
import dotty.tools.dotc.parsing.Parsers.Parser
2726
import Nullables.{given _}
@@ -1410,7 +1409,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
14101409
if suspendable then
14111410
ctx.compilationUnit.suspend() // this throws a SuspendException
14121411

1413-
val evaluatedSplice = inContext(tastyreflect.MacroExpansion.context(inlinedFrom)) {
1412+
val evaluatedSplice = inContext(quoted.MacroExpansion.context(inlinedFrom)) {
14141413
Splicer.splice(body, inlinedFrom.srcPos, MacroClassLoader.fromContext)
14151414
}
14161415
val inlinedNormailizer = new TreeMap {

0 commit comments

Comments
 (0)