Skip to content

Commit 9213ab3

Browse files
committed
Move internal Expr and Type implementations into the compiler
1 parent 7b80d8a commit 9213ab3

File tree

7 files changed

+71
-65
lines changed

7 files changed

+71
-65
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ object PickledQuotes {
3939

4040
/** Transform the expression into its fully spliced Tree */
4141
def quotedExprToTree[T](expr: quoted.Expr[T])(using Context): Tree = {
42-
val expr1 = expr.asInstanceOf[scala.internal.quoted.Expr[Tree]]
42+
val expr1 = expr.asInstanceOf[scala.quoted.internal.Expr[Tree]]
4343
expr1.checkScopeId(QuoteContextImpl.scopeId)
4444
changeOwnerOfTree(expr1.tree, ctx.owner)
4545
}
4646

4747
/** Transform the expression into its fully spliced TypeTree */
4848
def quotedTypeToTree(tpe: quoted.Type[?])(using Context): Tree = {
49-
val tpe1 = tpe.asInstanceOf[scala.internal.quoted.Type[Tree]]
49+
val tpe1 = tpe.asInstanceOf[scala.quoted.internal.Type[Tree]]
5050
tpe1.checkScopeId(QuoteContextImpl.scopeId)
5151
changeOwnerOfTree(tpe1.typeTree, ctx.owner)
5252
}
@@ -75,8 +75,8 @@ object PickledQuotes {
7575
override def transform(tree: tpd.Tree)(using Context): tpd.Tree = tree match {
7676
case Hole(isTerm, idx, args) =>
7777
val reifiedArgs = args.map { arg =>
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)
78+
if (arg.isTerm) (using qctx: QuoteContext) => new scala.quoted.internal.Expr(arg, QuoteContextImpl.scopeId)
79+
else new scala.quoted.internal.Type(arg, QuoteContextImpl.scopeId)
8080
}
8181
if isTerm then
8282
val quotedExpr = pickledQuote.exprSplice(idx)(reifiedArgs)(dotty.tools.dotc.quoted.QuoteContextImpl())

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
7272
case _ => false
7373
def asExpr: scala.quoted.Expr[Any] =
7474
if self.isExpr then
75-
new scala.internal.quoted.Expr(self, QuoteContextImpl.this.hashCode)
75+
new scala.quoted.internal.Expr(self, QuoteContextImpl.this.hashCode)
7676
else self match
7777
case TermTypeTest(self) => throw new Exception("Expected an expression. This is a partially applied Term. Try eta-expanding the term first.")
7878
case _ => throw new Exception("Expected a Term but was: " + self)
@@ -316,11 +316,11 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
316316
object TermMethodsImpl extends TermMethods:
317317
extension (self: Term):
318318
def seal: scala.quoted.Expr[Any] =
319-
if self.isExpr then new scala.internal.quoted.Expr(self, QuoteContextImpl.this.hashCode)
319+
if self.isExpr then new scala.quoted.internal.Expr(self, QuoteContextImpl.this.hashCode)
320320
else throw new Exception("Cannot seal a partially applied Term. Try eta-expanding the term first.")
321321

322322
def sealOpt: Option[scala.quoted.Expr[Any]] =
323-
if self.isExpr then Some(new scala.internal.quoted.Expr(self, QuoteContextImpl.this.hashCode))
323+
if self.isExpr then Some(new scala.quoted.internal.Expr(self, QuoteContextImpl.this.hashCode))
324324
else None
325325

326326
def tpe: TypeRepr = self.tpe
@@ -1003,7 +1003,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
10031003

10041004
object TypeTree extends TypeTreeModule:
10051005
def of[T <: AnyKind](using tp: scala.quoted.Type[T]): TypeTree =
1006-
tp.asInstanceOf[scala.internal.quoted.Type[TypeTree]].typeTree
1006+
tp.asInstanceOf[scala.quoted.internal.Type[TypeTree]].typeTree
10071007
end TypeTree
10081008

10091009
object TypeTreeMethodsImpl extends TypeTreeMethods:
@@ -1572,7 +1572,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
15721572

15731573
object TypeRepr extends TypeReprModule:
15741574
def of[T <: AnyKind](using tp: scala.quoted.Type[T]): TypeRepr =
1575-
tp.asInstanceOf[scala.internal.quoted.Type[TypeTree]].typeTree.tpe
1575+
tp.asInstanceOf[scala.quoted.internal.Type[TypeTree]].typeTree.tpe
15761576
def typeConstructorOf(clazz: Class[?]): TypeRepr =
15771577
if (clazz.isPrimitive)
15781578
if (clazz == classOf[Boolean]) dotc.core.Symbols.defn.BooleanType
@@ -1609,7 +1609,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
16091609
def seal: scala.quoted.Type[_] = self.asType
16101610

16111611
def asType: scala.quoted.Type[?] =
1612-
new scala.internal.quoted.Type(Inferred(self), QuoteContextImpl.this.hashCode)
1612+
new scala.quoted.internal.Type(Inferred(self), QuoteContextImpl.this.hashCode)
16131613

16141614
def =:=(that: TypeRepr): Boolean = self =:= that
16151615
def <:<(that: TypeRepr): Boolean = self <:< that
@@ -2633,11 +2633,11 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
26332633

26342634
def unpickleExpr[T](pickledQuote: PickledQuote): scala.quoted.Expr[T] =
26352635
val tree = PickledQuotes.unpickleTerm(pickledQuote)(using reflect.rootContext)
2636-
new scala.internal.quoted.Expr(tree, hash).asInstanceOf[scala.quoted.Expr[T]]
2636+
new scala.quoted.internal.Expr(tree, hash).asInstanceOf[scala.quoted.Expr[T]]
26372637

26382638
def unpickleType[T <: AnyKind](pickledQuote: PickledQuote): scala.quoted.Type[T] =
26392639
val tree = PickledQuotes.unpickleTypeTree(pickledQuote)(using reflect.rootContext)
2640-
new scala.internal.quoted.Type(tree, hash).asInstanceOf[scala.quoted.Type[T]]
2640+
new scala.quoted.internal.Type(tree, hash).asInstanceOf[scala.quoted.Type[T]]
26412641

26422642
def exprMatch(scrutinee: scala.quoted.Expr[Any], pattern: scala.quoted.Expr[Any]): Option[Tuple] =
26432643
treeMatch(scrutinee.unseal(using this), pattern.unseal(using this))

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,10 @@ object Splicer {
323323
}
324324

325325
private def interpretQuote(tree: Tree)(implicit env: Env): Object =
326-
new scala.internal.quoted.Expr(Inlined(EmptyTree, Nil, QuoteUtils.changeOwnerOfTree(tree, ctx.owner)).withSpan(tree.span), QuoteContextImpl.scopeId)
326+
new scala.quoted.internal.Expr(Inlined(EmptyTree, Nil, QuoteUtils.changeOwnerOfTree(tree, ctx.owner)).withSpan(tree.span), QuoteContextImpl.scopeId)
327327

328328
private def interpretTypeQuote(tree: Tree)(implicit env: Env): Object =
329-
new scala.internal.quoted.Type(QuoteUtils.changeOwnerOfTree(tree, ctx.owner), QuoteContextImpl.scopeId)
329+
new scala.quoted.internal.Type(QuoteUtils.changeOwnerOfTree(tree, ctx.owner), QuoteContextImpl.scopeId)
330330

331331
private def interpretLiteral(value: Any)(implicit env: Env): Object =
332332
value.asInstanceOf[Object]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package scala.quoted.internal
2+
3+
import scala.quoted._
4+
5+
/** An Expr backed by a tree. Only the current compiler trees are allowed.
6+
*
7+
* These expressions are used for arguments of macros. They contain and actual tree
8+
* from the program that is being expanded by the macro.
9+
*
10+
* May contain references to code defined outside this Expr instance.
11+
*/
12+
final class Expr[Tree](val tree: Tree, val scopeId: Int) extends scala.quoted.Expr[Any] {
13+
override def equals(that: Any): Boolean = that match {
14+
case that: Expr[_] =>
15+
// Expr are wrappers around trees, therefore they are equals if their trees are equal.
16+
// All scopeId should be equal unless two different runs of the compiler created the trees.
17+
tree == that.tree && scopeId == that.scopeId
18+
case _ => false
19+
}
20+
21+
def unseal(using qctx: QuoteContext): qctx.reflect.Term =
22+
checkScopeId(qctx.hashCode)
23+
tree.asInstanceOf[qctx.reflect.Term]
24+
25+
def checkScopeId(expectedScopeId: Int): Unit =
26+
if expectedScopeId != scopeId then
27+
throw new Exception("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
28+
29+
override def hashCode: Int = tree.hashCode
30+
override def toString: String = "'{ ... }"
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package scala.quoted.internal
2+
3+
import scala.quoted._
4+
5+
/** Quoted type (or kind) `T` backed by a tree */
6+
final class Type[Tree](val typeTree: Tree, val scopeId: Int) extends scala.quoted.Type[Any] {
7+
override def equals(that: Any): Boolean = that match {
8+
case that: Type[_] => typeTree ==
9+
// TastyTreeExpr are wrappers around trees, therfore they are equals if their trees are equal.
10+
// All scopeId should be equal unless two different runs of the compiler created the trees.
11+
that.typeTree && scopeId == that.scopeId
12+
case _ => false
13+
}
14+
15+
/** View this expression `quoted.Type[T]` as a `TypeTree` */
16+
def unseal(using qctx: QuoteContext): qctx.reflect.TypeTree =
17+
checkScopeId(qctx.hashCode)
18+
typeTree.asInstanceOf[qctx.reflect.TypeTree]
19+
20+
def checkScopeId(expectedScopeId: Int): Unit =
21+
if expectedScopeId != scopeId then
22+
throw new Exception("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
23+
24+
override def hashCode: Int = typeTree.hashCode
25+
override def toString: String = "'[ ... ]"
26+
}

library/src-bootstrapped/scala/internal/quoted/Expr.scala

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,6 @@ package scala.internal.quoted
22

33
import scala.quoted._
44

5-
/** An Expr backed by a tree. Only the current compiler trees are allowed.
6-
*
7-
* These expressions are used for arguments of macros. They contain and actual tree
8-
* from the program that is being expanded by the macro.
9-
*
10-
* May contain references to code defined outside this Expr instance.
11-
*/
12-
final class Expr[Tree](val tree: Tree, val scopeId: Int) extends scala.quoted.Expr[Any] {
13-
override def equals(that: Any): Boolean = that match {
14-
case that: Expr[_] =>
15-
// Expr are wrappers around trees, therefore they are equals if their trees are equal.
16-
// All scopeId should be equal unless two different runs of the compiler created the trees.
17-
tree == that.tree && scopeId == that.scopeId
18-
case _ => false
19-
}
20-
21-
def unseal(using qctx: QuoteContext): qctx.reflect.Term =
22-
checkScopeId(qctx.hashCode)
23-
tree.asInstanceOf[qctx.reflect.Term]
24-
25-
def checkScopeId(expectedScopeId: Int): Unit =
26-
if expectedScopeId != scopeId then
27-
throw new Exception("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
28-
29-
override def hashCode: Int = tree.hashCode
30-
override def toString: String = "'{ ... }"
31-
}
32-
335
object Expr {
346

357
/** Pattern matches an the scrutineeExpr against the patternExpr and returns a tuple

library/src-bootstrapped/scala/internal/quoted/Type.scala

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,6 @@ package scala.internal.quoted
22

33
import scala.quoted._
44

5-
/** Quoted type (or kind) `T` backed by a tree */
6-
final class Type[Tree](val typeTree: Tree, val scopeId: Int) extends scala.quoted.Type[Any] {
7-
override def equals(that: Any): Boolean = that match {
8-
case that: Type[_] => typeTree ==
9-
// TastyTreeExpr are wrappers around trees, therfore they are equals if their trees are equal.
10-
// All scopeId should be equal unless two different runs of the compiler created the trees.
11-
that.typeTree && scopeId == that.scopeId
12-
case _ => false
13-
}
14-
15-
/** View this expression `quoted.Type[T]` as a `TypeTree` */
16-
def unseal(using qctx: QuoteContext): qctx.reflect.TypeTree =
17-
checkScopeId(qctx.hashCode)
18-
typeTree.asInstanceOf[qctx.reflect.TypeTree]
19-
20-
def checkScopeId(expectedScopeId: Int): Unit =
21-
if expectedScopeId != scopeId then
22-
throw new Exception("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
23-
24-
override def hashCode: Int = typeTree.hashCode
25-
override def toString: String = "'[ ... ]"
26-
}
27-
285
object Type {
296

307
/** Pattern matches an the scrutineeType against the patternType and returns a tuple

0 commit comments

Comments
 (0)