Skip to content

Rename Reflection tasty to reflect #9961

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
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/quoted/Matcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ import scala.internal.tasty.CompilerInterface.quoteContextWithCompilerInterface
*/
object Matcher {

abstract class QuoteMatcher[QCtx <: QuoteContext { val tasty: scala.internal.tasty.CompilerInterface } & Singleton](val qctx: QCtx) {
abstract class QuoteMatcher[QCtx <: QuoteContext { val reflect: scala.internal.tasty.CompilerInterface } & Singleton](val qctx: QCtx) {

// TODO improve performance

// TODO use flag from qctx.tasty. Maybe -debug or add -debug-macros
// TODO use flag from qctx.reflect. Maybe -debug or add -debug-macros
private final val debug = false

import qctx.tasty._
import qctx.reflect._
import Matching._

def patternHoleSymbol: Symbol
Expand Down
30 changes: 15 additions & 15 deletions compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ object QuoteContextImpl {
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])
show(using qctx)(tree.asInstanceOf[qctx.reflect.Tree], syntaxHighlight)(using ctx.asInstanceOf[qctx.reflect.Context])
}

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

private[dotty] def checkScopeId(id: ScopeId)(using Context): Unit =
Expand All @@ -49,7 +49,7 @@ object QuoteContextImpl {

class QuoteContextImpl private (ctx: Context) extends QuoteContext:

object tasty extends scala.tasty.Reflection, scala.internal.tasty.CompilerInterface:
object reflect extends scala.tasty.Reflection, scala.internal.tasty.CompilerInterface:

def rootContext: Context = ctx

Expand All @@ -67,11 +67,11 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
def pos: Position = self.sourcePos
def symbol: Symbol = self.symbol
def showExtractors: String =
new ExtractorsPrinter[tasty.type](tasty).showTree(self)
new ExtractorsPrinter[reflect.type](reflect).showTree(self)
def show: String =
self.showWith(SyntaxHighlight.plain)
def showWith(syntaxHighlight: SyntaxHighlight): String =
new SourceCodePrinter[tasty.type](tasty)(syntaxHighlight).showTree(self)
new SourceCodePrinter[reflect.type](reflect)(syntaxHighlight).showTree(self)
def isExpr: Boolean =
self match
case TermTypeTest(self) =>
Expand Down Expand Up @@ -1584,13 +1584,13 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
object TypeMethodsImpl extends TypeMethods:
extension (self: Type):
def showExtractors: String =
new ExtractorsPrinter[tasty.type](tasty).showType(self)
new ExtractorsPrinter[reflect.type](reflect).showType(self)

def show: String =
self.showWith(SyntaxHighlight.plain)

def showWith(syntaxHighlight: SyntaxHighlight): String =
new SourceCodePrinter[tasty.type](tasty)(syntaxHighlight).showType(self)
new SourceCodePrinter[reflect.type](reflect)(syntaxHighlight).showType(self)

def seal: scala.quoted.Type[_] =
new scala.internal.quoted.Type(Inferred(self), QuoteContextImpl.this.hashCode)
Expand Down Expand Up @@ -2164,11 +2164,11 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
extension (self: Constant):
def value: Any = self.value
def showExtractors: String =
new ExtractorsPrinter[tasty.type](tasty).showConstant(self)
new ExtractorsPrinter[reflect.type](reflect).showConstant(self)
def show: String =
self.showWith(SyntaxHighlight.plain)
def showWith(syntaxHighlight: SyntaxHighlight): String =
new SourceCodePrinter[tasty.type](tasty)(syntaxHighlight).showConstant(self)
new SourceCodePrinter[reflect.type](reflect)(syntaxHighlight).showConstant(self)
end extension
end ConstantMethodsImpl

Expand Down Expand Up @@ -2387,11 +2387,11 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
def children: List[Symbol] = self.denot.children

def showExtractors: String =
new ExtractorsPrinter[tasty.type](tasty).showSymbol(self)
new ExtractorsPrinter[reflect.type](reflect).showSymbol(self)
def show: String =
self.showWith(SyntaxHighlight.plain)
def showWith(syntaxHighlight: SyntaxHighlight): String =
new SourceCodePrinter[tasty.type](tasty)(syntaxHighlight).showSymbol(self)
new SourceCodePrinter[reflect.type](reflect)(syntaxHighlight).showSymbol(self)

end extension

Expand Down Expand Up @@ -2523,11 +2523,11 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
def |(that: Flags): Flags = dotc.core.Flags.extension_|(self)(that)
def &(that: Flags): Flags = dotc.core.Flags.extension_&(self)(that)
def showExtractors: String =
new ExtractorsPrinter[tasty.type](tasty).showFlags(self)
new ExtractorsPrinter[reflect.type](reflect).showFlags(self)
def show: String =
self.showWith(SyntaxHighlight.plain)
def showWith(syntaxHighlight: SyntaxHighlight): String =
new SourceCodePrinter[tasty.type](tasty)(syntaxHighlight).showFlags(self)
new SourceCodePrinter[reflect.type](reflect)(syntaxHighlight).showFlags(self)
end extension
end FlagsMethodsImpl

Expand Down Expand Up @@ -2647,7 +2647,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
ctx1

val qctx1 = dotty.tools.dotc.quoted.QuoteContextImpl()(using ctx1)
.asInstanceOf[QuoteContext { val tasty: QuoteContextImpl.this.tasty.type }]
.asInstanceOf[QuoteContext { val reflect: QuoteContextImpl.this.reflect.type }]

val matcher = new Matcher.QuoteMatcher[qctx1.type](qctx1) {
def patternHoleSymbol: Symbol = dotc.core.Symbols.defn.InternalQuotedPatterns_patternHole
Expand All @@ -2671,7 +2671,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
}
}

end tasty
end reflect

private[this] val hash = QuoteContextImpl.scopeId(using ctx)
override def hashCode: Int = hash
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/reference/contextual/derivation-macro.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ from the signature. The body of the `derived` method is shown below:

```scala
given derived[T: Type](using qctx: QuoteContext) as Expr[Eq[T]] = {
import qctx.tasty._
import qctx.reflect._

val ev: Expr[Mirror.Of[T]] = Expr.summon(using '[Mirror.Of[T]]).get

Expand Down Expand Up @@ -177,7 +177,7 @@ object Eq {
}

given derived[T: Type](using qctx: QuoteContext) as Expr[Eq[T]] = {
import qctx.tasty._
import qctx.reflect._

val ev: Expr[Mirror.Of[T]] = Expr.summon(using '[Mirror.Of[T]]).get

Expand Down
18 changes: 9 additions & 9 deletions docs/docs/reference/metaprogramming/tasty-reflect.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ guarantees and may fail at macro expansion time, hence additional explicit
checks must be done.

To provide reflection capabilities in macros we need to add an implicit
parameter of type `scala.quoted.QuoteContext` and import `tasty._` from it in
parameter of type `scala.quoted.QuoteContext` and import `qctx.reflect._` from it in
the scope where it is used.

```scala
Expand All @@ -29,19 +29,19 @@ import scala.quoted._
inline def natConst(x: => Int): Int = ${natConstImpl('{x})}

def natConstImpl(x: Expr[Int])(using qctx: QuoteContext): Expr[Int] = {
import qctx.tasty._
import qctx.reflect._
...
}
```

### Extractors

`import qctx.tasty._` will provide all extractors and methods on TASTy Reflect
`import qctx.reflect._` will provide all extractors and methods on TASTy Reflect
trees. For example the `Literal(_)` extractor used below.

```scala
def natConstImpl(x: Expr[Int])(using qctx: QuoteContext): Expr[Int] = {
import qctx.tasty._
import qctx.reflect._
val xTree: Term = x.unseal
xTree match {
case Inlined(_, _, Literal(Constant(n: Int))) =>
Expand All @@ -59,9 +59,9 @@ def natConstImpl(x: Expr[Int])(using qctx: QuoteContext): Expr[Int] = {
```

To easily know which extractors are needed, the `showExtractors` method on a
`qctx.tasty.Term` returns the string representation of the extractors.
`qctx.reflect.Term` returns the string representation of the extractors.

The method `qctx.tasty.Term.seal` provides a way to go back to a
The method `qctx.reflect.Term.seal` provides a way to go back to a
`quoted.Expr[Any]`. Note that the type is `Expr[Any]`. Consequently, the type
must be set explicitly with a checked `cast` call. If the type does not conform
to it an exception will be thrown at runtime.
Expand All @@ -77,7 +77,7 @@ operation expression passed while calling the `macro` below.
inline def macro(param: => Boolean): Unit = ${ macroImpl('param) }

def macroImpl(param: Expr[Boolean])(using qctx: QuoteContext): Expr[Unit] = {
import qctx.tasty._
import qctx.reflect._
import util._

param.unseal.underlyingArgument match {
Expand All @@ -91,15 +91,15 @@ macro(this.checkCondition())

### Positions

The tasty context provides a `rootPosition` value. It corresponds to
The `ast` in the context provides a `rootPosition` value. It corresponds to
the expansion site for macros. The macro authors can obtain various information about that
expansion site. The example below shows how we can obtain position information
such as the start line, the end line or even the source code at the expansion
point.

```scala
def macroImpl()(qctx: QuoteContext): Expr[Unit] = {
import qctx.tasty._
import qctx.reflect._
val pos = rootPosition

val path = pos.sourceFile.jpath.toString
Expand Down
10 changes: 5 additions & 5 deletions library/src-bootstrapped/scala/internal/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import scala.internal.tasty.CompilerInterface.quoteContextWithCompilerInterface
case _ => false
}

def unseal(using qctx: QuoteContext): qctx.tasty.Term =
def unseal(using qctx: QuoteContext): qctx.reflect.Term =
if (qctx.hashCode != scopeId)
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
tree.asInstanceOf[qctx.tasty.Term]
tree.asInstanceOf[qctx.reflect.Term]

override def hashCode: Int = tree.hashCode
override def toString: String = "'{ ... }"
Expand Down Expand Up @@ -54,18 +54,18 @@ object Expr {
def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutineeExpr: scala.quoted.Expr[Any])
(using patternExpr: scala.quoted.Expr[Any], qctx: QuoteContext): Option[Tup] = {
val qctx1 = quoteContextWithCompilerInterface(qctx)
qctx1.tasty.termMatch(scrutineeExpr.unseal, patternExpr.unseal).asInstanceOf[Option[Tup]]
qctx1.reflect.termMatch(scrutineeExpr.unseal, patternExpr.unseal).asInstanceOf[Option[Tup]]
}

/** Returns a null expresssion equivalent to `'{null}` */
def `null`: QuoteContext ?=> quoted.Expr[Null] = qctx ?=> {
import qctx.tasty._
import qctx.reflect._
Literal(Constant.Null()).seal.asInstanceOf[quoted.Expr[Null]]
}

/** Returns a unit expresssion equivalent to `'{}` or `'{()}` */
def Unit: QuoteContext ?=> quoted.Expr[Unit] = qctx ?=> {
import qctx.tasty._
import qctx.reflect._
Literal(Constant.Unit()).seal.asInstanceOf[quoted.Expr[Unit]]
}

Expand Down
24 changes: 12 additions & 12 deletions library/src-bootstrapped/scala/internal/quoted/Type.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ final class Type[Tree](val typeTree: Tree, val scopeId: Int) extends scala.quote
}

/** View this expression `quoted.Type[T]` as a `TypeTree` */
def unseal(using qctx: QuoteContext): qctx.tasty.TypeTree =
def unseal(using qctx: QuoteContext): qctx.reflect.TypeTree =
if (qctx.hashCode != scopeId)
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
typeTree.asInstanceOf[qctx.tasty.TypeTree]
typeTree.asInstanceOf[qctx.reflect.TypeTree]

override def hashCode: Int = typeTree.hashCode
override def toString: String = "'[ ... ]"
Expand All @@ -37,37 +37,37 @@ object Type {
def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutineeType: scala.quoted.Type[_])
(using patternType: scala.quoted.Type[_], qctx: QuoteContext): Option[Tup] = {
val qctx1 = quoteContextWithCompilerInterface(qctx)
qctx1.tasty.typeTreeMatch(scrutineeType.unseal, patternType.unseal).asInstanceOf[Option[Tup]]
qctx1.reflect.typeTreeMatch(scrutineeType.unseal, patternType.unseal).asInstanceOf[Option[Tup]]
}


// TODO generalize following optimizations for all classes without parameters

def Unit: QuoteContext ?=> quoted.Type[Unit] =
qctx.tasty.Type.typeConstructorOf(classOf[Unit]).seal.asInstanceOf[quoted.Type[Unit]]
qctx.reflect.Type.typeConstructorOf(classOf[Unit]).seal.asInstanceOf[quoted.Type[Unit]]

def Boolean: QuoteContext ?=> quoted.Type[Boolean] =
qctx.tasty.Type.typeConstructorOf(classOf[Boolean]).seal.asInstanceOf[quoted.Type[Boolean]]
qctx.reflect.Type.typeConstructorOf(classOf[Boolean]).seal.asInstanceOf[quoted.Type[Boolean]]

def Byte: QuoteContext ?=> quoted.Type[Byte] =
qctx.tasty.Type.typeConstructorOf(classOf[Byte]).seal.asInstanceOf[quoted.Type[Byte]]
qctx.reflect.Type.typeConstructorOf(classOf[Byte]).seal.asInstanceOf[quoted.Type[Byte]]

def Char: QuoteContext ?=> quoted.Type[Char] =
qctx.tasty.Type.typeConstructorOf(classOf[Char]).seal.asInstanceOf[quoted.Type[Char]]
qctx.reflect.Type.typeConstructorOf(classOf[Char]).seal.asInstanceOf[quoted.Type[Char]]

def Short: QuoteContext ?=> quoted.Type[Short] =
qctx.tasty.Type.typeConstructorOf(classOf[Short]).seal.asInstanceOf[quoted.Type[Short]]
qctx.reflect.Type.typeConstructorOf(classOf[Short]).seal.asInstanceOf[quoted.Type[Short]]

def Int: QuoteContext ?=> quoted.Type[Int] =
qctx.tasty.Type.typeConstructorOf(classOf[Int]).seal.asInstanceOf[quoted.Type[Int]]
qctx.reflect.Type.typeConstructorOf(classOf[Int]).seal.asInstanceOf[quoted.Type[Int]]

def Long: QuoteContext ?=> quoted.Type[Long] =
qctx.tasty.Type.typeConstructorOf(classOf[Long]).seal.asInstanceOf[quoted.Type[Long]]
qctx.reflect.Type.typeConstructorOf(classOf[Long]).seal.asInstanceOf[quoted.Type[Long]]

def Float: QuoteContext ?=> quoted.Type[Float] =
qctx.tasty.Type.typeConstructorOf(classOf[Float]).seal.asInstanceOf[quoted.Type[Float]]
qctx.reflect.Type.typeConstructorOf(classOf[Float]).seal.asInstanceOf[quoted.Type[Float]]

def Double: QuoteContext ?=> quoted.Type[Double] =
qctx.tasty.Type.typeConstructorOf(classOf[Double]).seal.asInstanceOf[quoted.Type[Double]]
qctx.reflect.Type.typeConstructorOf(classOf[Double]).seal.asInstanceOf[quoted.Type[Double]]

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ object Unpickler {
*/
def unpickleExpr[T](repr: PickledQuote, args: PickledArgs): QuoteContext ?=> Expr[T] =
val qctx = quoteContextWithCompilerInterface(summon[QuoteContext])
val tree = qctx.tasty.unpickleExpr(repr, args)
val tree = qctx.reflect.unpickleExpr(repr, args)
new scala.internal.quoted.Expr(tree, qctx.hashCode).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] =
val qctx = quoteContextWithCompilerInterface(summon[QuoteContext])
val tree = qctx.tasty.unpickleType(repr, args)
val tree = qctx.reflect.unpickleType(repr, args)
new scala.internal.quoted.Type(tree, qctx.hashCode).asInstanceOf[Type[T]]

}
2 changes: 1 addition & 1 deletion library/src-bootstrapped/scala/quoted/Const.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object Const {
* ```
*/
def unapply[T](expr: Expr[T])(using qctx: QuoteContext): Option[T] = {
import qctx.tasty._
import qctx.reflect._
def rec(tree: Term): Option[T] = tree match {
case Literal(c) => Some(c.value.asInstanceOf[T])
case Block(Nil, e) => rec(e)
Expand Down
8 changes: 4 additions & 4 deletions library/src-bootstrapped/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ abstract class Expr[+T] private[scala] {
}

/** View this expression `quoted.Expr[T]` as a `Term` */
def unseal(using qctx: QuoteContext): qctx.tasty.Term
def unseal(using qctx: QuoteContext): qctx.reflect.Term

}

Expand Down Expand Up @@ -80,7 +80,7 @@ object Expr {
* Some bindings may be elided as an early optimization.
*/
def betaReduce[T](expr: Expr[T])(using qctx: QuoteContext): Expr[T] =
qctx.tasty.Term.betaReduce(expr.unseal) match
qctx.reflect.Term.betaReduce(expr.unseal) match
case Some(expr1) => expr1.seal.asInstanceOf[Expr[T]]
case _ => expr

Expand All @@ -89,7 +89,7 @@ object Expr {
* will be equivalent to `'{ $s1; $s2; ...; $e }`.
*/
def block[T](statements: List[Expr[Any]], expr: Expr[T])(using qctx: QuoteContext): Expr[T] = {
import qctx.tasty._
import qctx.reflect._
Block(statements.map(_.unseal), expr.unseal).seal.asInstanceOf[Expr[T]]
}

Expand Down Expand Up @@ -191,7 +191,7 @@ object Expr {
* @param qctx current context
*/
def summon[T](using tpe: Type[T])(using qctx: QuoteContext): Option[Expr[T]] = {
import qctx.tasty._
import qctx.reflect._
Implicits.search(tpe.unseal.tpe) match {
case iss: ImplicitSearchSuccess => Some(iss.tree.seal.asInstanceOf[Expr[T]])
case isf: ImplicitSearchFailure => None
Expand Down
Loading