Skip to content

Move implementation of Expr/Type to lib #8978

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
10 changes: 5 additions & 5 deletions compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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, TastyTreeExpr, TreeType}
import dotty.tools.dotc.tastyreflect.ReflectionImpl

import dotty.tools.tasty.TastyString

Expand All @@ -38,14 +38,14 @@ object PickledQuotes {

/** Transform the expression into its fully spliced Tree */
def quotedExprToTree[T](expr: quoted.Expr[T])(implicit ctx: Context): Tree = {
val expr1 = expr.asInstanceOf[TastyTreeExpr]
val expr1 = expr.asInstanceOf[scala.internal.quoted.Expr[Tree]]
QuoteContext.checkScopeId(expr1.scopeId)
healOwner(expr1.tree)
}

/** Transform the expression into its fully spliced TypeTree */
def quotedTypeToTree(tpe: quoted.Type[?])(implicit ctx: Context): Tree = {
val tpe1 = tpe.asInstanceOf[TreeType]
val tpe1 = tpe.asInstanceOf[scala.internal.quoted.Type[Tree]]
QuoteContext.checkScopeId(tpe1.scopeId)
healOwner(tpe1.typeTree)
}
Expand Down Expand Up @@ -74,8 +74,8 @@ object PickledQuotes {
override def transform(tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = tree match {
case Hole(isTerm, idx, args) =>
val reifiedArgs = args.map { arg =>
if (arg.isTerm) (using qctx: scala.quoted.QuoteContext) => new TastyTreeExpr(arg, QuoteContext.scopeId)
else new TreeType(arg, QuoteContext.scopeId)
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 isTerm then
val splice1 = splices(idx).asInstanceOf[Seq[Any] => scala.quoted.QuoteContext ?=> quoted.Expr[?]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import dotty.tools.dotc.quoted.QuoteContext
import dotty.tools.tasty.TastyFormat._

import scala.quoted
import dotty.tools.dotc.tastyreflect.{TastyTreeExpr, TreeType}
import scala.annotation.constructorOnly
import scala.annotation.internal.sharable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
// QUOTE UNPICKLING //
//////////////////////

def unpickleExpr(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): scala.quoted.Expr[?] =
new TastyTreeExpr(PickledQuotes.unpickleExpr(repr, args), compilerId)
def unpickleExpr(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): Term =
PickledQuotes.unpickleExpr(repr, args)

def unpickleType(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): scala.quoted.Type[?] =
new TreeType(PickledQuotes.unpickleType(repr, args), compilerId)
def unpickleType(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): TypeTree =
PickledQuotes.unpickleType(repr, args)


/////////////
Expand Down Expand Up @@ -1903,7 +1903,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend

def QuotedExpr_seal(self: Term)(using ctx: Context): Option[scala.quoted.Expr[Any]] = self.tpe.widen match {
case _: Types.MethodType | _: Types.PolyType => None
case _ => Some(new TastyTreeExpr(self, compilerId))
case _ => Some(new scala.internal.quoted.Expr(self, compilerId))
}

/** Checked cast to a `quoted.Expr[U]` */
Expand All @@ -1923,7 +1923,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
/** Convert `Type` to an `quoted.Type[?]` */
def QuotedType_seal(self: Type)(using ctx: Context): scala.quoted.Type[?] = {
val dummySpan = ctx.owner.span // FIXME
new TreeType(tpd.TypeTree(self).withSpan(dummySpan), compilerId)
new scala.internal.quoted.Type(tpd.TypeTree(self).withSpan(dummySpan), compilerId)
}

/////////////////
Expand Down Expand Up @@ -2139,5 +2139,5 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
private def withDefaultPos[T <: Tree](fn: Context ?=> T)(using ctx: Context): T =
fn(using ctx.withSource(rootPosition.source)).withSpan(rootPosition.span)

private def compilerId: Int = rootContext.outersIterator.toList.last.hashCode()
def compilerId: Int = rootContext.outersIterator.toList.last.hashCode()
}
22 changes: 0 additions & 22 deletions compiler/src/dotty/tools/dotc/tastyreflect/TastyTreeExpr.scala

This file was deleted.

16 changes: 0 additions & 16 deletions compiler/src/dotty/tools/dotc/tastyreflect/TreeType.scala

This file was deleted.

6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import dotty.tools.dotc.core.Types._
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.{NameKinds, TypeErasure}
import dotty.tools.dotc.core.Constants.Constant
import dotty.tools.dotc.tastyreflect.{ReflectionImpl, TastyTreeExpr, TreeType}
import dotty.tools.dotc.tastyreflect.ReflectionImpl

import scala.util.control.NonFatal
import dotty.tools.dotc.util.SourcePosition
Expand Down Expand Up @@ -295,10 +295,10 @@ object Splicer {
}

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

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

private def interpretLiteral(value: Any)(implicit env: Env): Object =
value.asInstanceOf[Object]
Expand Down
20 changes: 16 additions & 4 deletions library/src/scala/internal/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ package scala.internal.quoted

import scala.quoted._

/** Quoted expression of type `T`
/** An Expr backed by a tree. Only the current compiler trees are allowed.
*
* Restriction: only the QuoteContext.tasty.internal implementation is allowed to extend this trait.
* Any other implementation will result in an undefined behavior.
* These expressions are used for arguments of macros. They contain and actual tree
* from the program that is being expanded by the macro.
*
* May contain references to code defined outside this Expr instance.
*/
class Expr[+T] extends scala.quoted.Expr[T]
final class Expr[Tree](val tree: Tree, val scopeId: Int) extends scala.quoted.Expr[Any] {
override def equals(that: Any): Boolean = that match {
case that: Expr[_] =>
// Expr are wrappers around trees, therfore they are equals if their trees are equal.
// All scopeId should be equal unless two different runs of the compiler created the trees.
tree == that.tree && scopeId == that.scopeId
case _ => false
}
override def hashCode: Int = tree.hashCode
override def toString: String = "'{ ... }"
}

object Expr {

Expand Down
18 changes: 12 additions & 6 deletions library/src/scala/internal/quoted/Type.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package scala.internal.quoted

import scala.quoted._

/** Quoted type (or kind) `T`
*
* Restriction: only the QuoteContext.tasty.internal implementation is allowed to extend this trait.
* Any other implementation will result in an undefined behavior.
*/
class Type[T <: AnyKind] extends scala.quoted.Type[T]
/** Quoted type (or kind) `T` backed by a tree */
final class Type[Tree](val typeTree: Tree, val scopeId: Int) extends scala.quoted.Type[Any] {
override def equals(that: Any): Boolean = that match {
case that: Type[_] => typeTree ==
// TastyTreeExpr are wrappers around trees, therfore they are equals if their trees are equal.
// All scopeId should be equal unless two different runs of the compiler created the trees.
that.typeTree && scopeId == that.scopeId
case _ => false
}
override def hashCode: Int = typeTree.hashCode
override def toString: String = "'[ ... ]"
}

object Type {

Expand Down
8 changes: 6 additions & 2 deletions library/src/scala/internal/quoted/Unpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ object Unpickler {
* replacing splice nodes with `args`
*/
def unpickleExpr[T](repr: PickledQuote, args: PickledArgs): QuoteContext ?=> Expr[T] =
summon[QuoteContext].tasty.internal.unpickleExpr(repr, args).asInstanceOf[Expr[T]]
val ctx = summon[QuoteContext]
val tree = ctx.tasty.internal.unpickleExpr(repr, args)
new scala.internal.quoted.Expr(tree, ctx.tasty.internal.compilerId).asInstanceOf[Expr[T]]

/** Unpickle `repr` which represents a pickled `Type` tree,
* replacing splice nodes with `args`
*/
def unpickleType[T](repr: PickledQuote, args: PickledArgs): QuoteContext ?=> Type[T] =
summon[QuoteContext].tasty.internal.unpickleType(repr, args).asInstanceOf[Type[T]]
val ctx = summon[QuoteContext]
val tree = ctx.tasty.internal.unpickleType(repr, args)
new scala.internal.quoted.Type(tree, ctx.tasty.internal.compilerId).asInstanceOf[Type[T]]

}
6 changes: 4 additions & 2 deletions library/src/scala/tasty/reflect/CompilerInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ trait CompilerInterface {
/** Unpickle `repr` which represents a pickled `Expr` tree,
* replacing splice nodes with `args`
*/
def unpickleExpr(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): scala.quoted.Expr[Any]
def unpickleExpr(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): Term

/** Unpickle `repr` which represents a pickled `Type` tree,
* replacing splice nodes with `args`
*/
def unpickleType(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): scala.quoted.Type[_]
def unpickleType(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): TypeTree


/////////////
Expand Down Expand Up @@ -1576,4 +1576,6 @@ trait CompilerInterface {

def lambdaExtractor(term: Term, paramTypes: List[Type])(using ctx: Context): Option[List[Term] => Term]

def compilerId: Int

}