Skip to content

Refcatorings in TASTy reflection interface #5398

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 6 commits into from
Nov 10, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dotty.tools.dotc.consumetasty

import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Phases.Phase
import dotty.tools.dotc.tastyreflect.TastyImpl
import dotty.tools.dotc.tastyreflect.ReflectionImpl

import scala.tasty.file.TastyConsumer

Expand All @@ -11,8 +11,8 @@ class TastyConsumerPhase(consumer: TastyConsumer) extends Phase {
override def phaseName: String = "tastyConsumer"

override def run(implicit ctx: Context): Unit = {
val tasty = new TastyImpl(ctx)
consumer(tasty)(ctx.compilationUnit.tpdTree)
val reflect = new ReflectionImpl(ctx)
consumer(reflect)(ctx.compilationUnit.tpdTree)
}

}
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -691,11 +691,11 @@ class Definitions {
def Unpickler_liftedExpr: TermSymbol = ctx.requiredMethod("scala.runtime.quoted.Unpickler.liftedExpr")
def Unpickler_unpickleType: TermSymbol = ctx.requiredMethod("scala.runtime.quoted.Unpickler.unpickleType")

lazy val TastyTastyType: TypeRef = ctx.requiredClassRef("scala.tasty.Tasty")
def TastyTastyClass(implicit ctx: Context): ClassSymbol = TastyTastyType.symbol.asClass
lazy val TastyReflectionType: TypeRef = ctx.requiredClassRef("scala.tasty.Reflection")
def TastyReflectionClass(implicit ctx: Context): ClassSymbol = TastyReflectionType.symbol.asClass

lazy val TastyTastyModule: TermSymbol = ctx.requiredModule("scala.tasty.Tasty")
lazy val TastyTasty_macroContext: TermSymbol = TastyTastyModule.requiredMethod("macroContext")
lazy val TastyReflectionModule: TermSymbol = ctx.requiredModule("scala.tasty.Reflection")
lazy val TastyReflection_macroContext: TermSymbol = TastyReflectionModule.requiredMethod("macroContext")

lazy val EqType: TypeRef = ctx.requiredClassRef("scala.Eq")
def EqClass(implicit ctx: Context): ClassSymbol = EqType.symbol.asClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.io.{OutputStream, PrintStream}
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Phases.Phase
import dotty.tools.dotc.core.tasty.TastyPrinter
import dotty.tools.dotc.tastyreflect.TastyImpl
import dotty.tools.dotc.tastyreflect.ReflectionImpl
import dotty.tools.io.File

/** Phase that prints the trees in all loaded compilation units.
Expand Down Expand Up @@ -41,7 +41,7 @@ class DecompilationPrinter extends Phase {
} else {
val unitFile = unit.source.toString.replace("\\", "/").replace(".class", ".tasty")
out.println(s"/** Decompiled from $unitFile */")
out.println(new TastyImpl(ctx).showSourceCode.showTree(unit.tpdTree)(ctx))
out.println(new ReflectionImpl(ctx).showSourceCode.showTree(unit.tpdTree)(ctx))
}
}
}
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dotty.tools.dotc.quoted
import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.Driver
import dotty.tools.dotc.core.Contexts.{Context, ContextBase, FreshContext}
import dotty.tools.dotc.tastyreflect.TastyImpl
import dotty.tools.dotc.tastyreflect.ReflectionImpl
import dotty.tools.io.{AbstractFile, Directory, PlainDirectory, VirtualDirectory}
import dotty.tools.repl.AbstractFileClassLoader

Expand Down Expand Up @@ -47,7 +47,7 @@ class QuoteDriver extends Driver {
val tree1 =
if (ctx.settings.YshowRawQuoteTrees.value) tree
else (new TreeCleaner).transform(tree)
new TastyImpl(ctx).showSourceCode.showTree(tree1)
new ReflectionImpl(ctx).showSourceCode.showTree(tree1)
}
withTree(expr, show, settings)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dotty.tools.dotc.tastyreflect


trait CaseDefOpsImpl extends scala.tasty.reflect.CaseDefOps with TastyCoreImpl with Helpers {
trait CaseDefOpsImpl extends scala.tasty.reflect.CaseDefOps with CoreImpl with Helpers {

def CaseDefDeco(caseDef: CaseDef): CaseDefAPI = new CaseDefAPI {
def pattern(implicit ctx: Context): Pattern = caseDef.pat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dotty.tools.dotc.tastyreflect
import dotty.tools.dotc.core.Constants


trait ConstantOpsImpl extends scala.tasty.reflect.ConstantOps with TastyCoreImpl {
trait ConstantOpsImpl extends scala.tasty.reflect.ConstantOps with CoreImpl {

def ConstantDeco(const: Constant): ConstantAPI = new ConstantAPI {
def value: Any = const.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dotty.tools.dotc.tastyreflect

import dotty.tools.dotc.util.{Positions, SourcePosition}

trait ContextOpsImpl extends scala.tasty.reflect.ContextOps with TastyCoreImpl {
trait ContextOpsImpl extends scala.tasty.reflect.ContextOps with CoreImpl {

val rootContext: Context

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import dotty.tools.dotc.ast.{tpd, untpd}
import dotty.tools.dotc.core.Constants
import dotty.tools.dotc.core.Types

trait TastyCoreImpl extends scala.tasty.reflect.TastyCore {
trait CoreImpl extends scala.tasty.reflect.Core {

type Context = core.Contexts.Context

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/tastyreflect/IdOpsImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dotty.tools.dotc.tastyreflect

import dotty.tools.dotc.core.Decorators._

trait IdOpsImpl extends scala.tasty.reflect.IdOps with TastyCoreImpl {
trait IdOpsImpl extends scala.tasty.reflect.IdOps with CoreImpl {

def IdDeco(id: Id): IdAPI = new IdAPI {
def pos(implicit ctx: Context): Position = id.pos
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dotty.tools.dotc.tastyreflect
import dotty.tools.dotc.ast.{Trees, untpd}
import dotty.tools.dotc.core.StdNames.nme

trait ImportSelectorOpsImpl extends scala.tasty.reflect.ImportSelectorOps with TastyCoreImpl {
trait ImportSelectorOpsImpl extends scala.tasty.reflect.ImportSelectorOps with CoreImpl {

object SimpleSelector extends SimpleSelectorExtractor {
def unapply(x: ImportSelector)(implicit ctx: Context): Option[Id] = x match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dotty.tools.dotc.tastyreflect
import dotty.tools.dotc.ast.{Trees, tpd}
import dotty.tools.dotc.core.Decorators._

trait PatternOpsImpl extends scala.tasty.reflect.PatternOps with TastyCoreImpl {
trait PatternOpsImpl extends scala.tasty.reflect.PatternOps with CoreImpl {

// ----- Patterns -------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dotty.tools.dotc.tastyreflect

trait PositionOpsImpl extends scala.tasty.reflect.PositionOps with TastyCoreImpl {
trait PositionOpsImpl extends scala.tasty.reflect.PositionOps with CoreImpl {

def PositionDeco(pos: Position): PositionAPI = new PositionAPI {
def start: Int = pos.start
Expand Down
29 changes: 17 additions & 12 deletions compiler/src/dotty/tools/dotc/tastyreflect/PrintersImpl.scala
Original file line number Diff line number Diff line change
@@ -1,46 +1,51 @@
package dotty.tools.dotc.tastyreflect

import scala.tasty.util.{Show, ShowExtractors, ShowSourceCode}
trait PrintersImpl extends scala.tasty.reflect.Printers with scala.tasty.reflect.Core { reflect: ReflectionImpl =>

trait PrintersImpl extends scala.tasty.reflect.Printers with scala.tasty.reflect.TastyCore { tasty: TastyImpl =>
def showExtractors: reflect.Printer = new reflect.ExtractorsPrinter

def showExtractors: Show[tasty.type] = new ShowExtractors[tasty.type](this)

def showSourceCode: Show[tasty.type] = new ShowSourceCode[tasty.type](this)
def showSourceCode: reflect.Printer = new reflect.SourceCodePrinter

/** Adds `show` as an extension method of a `Tree` */
def TreeShowDeco(tree: Tree): ShowAPI = new ShowAPI {
def show(implicit ctx: Context, s: Show[tasty.type]): String = s.showTree(tree)
def show(implicit ctx: Context): String = showExtractors.showTree(tree)
def showCode(implicit ctx: Context): String = showSourceCode.showTree(tree)
}

/** Adds `show` as an extension method of a `TypeOrBoundsTree` */
def TypeOrBoundsTreeShowDeco(tpt: TypeOrBoundsTree): ShowAPI = new ShowAPI {
def show(implicit ctx: Context, s: Show[tasty.type]): String = s.showTypeOrBoundsTree(tpt)
def show(implicit ctx: Context): String = showExtractors.showTypeOrBoundsTree(tpt)
def showCode(implicit ctx: Context): String = showSourceCode.showTypeOrBoundsTree(tpt)
}

/** Adds `show` as an extension method of a `TypeOrBounds` */
def TypeOrBoundsShowDeco(tpe: TypeOrBounds): ShowAPI = new ShowAPI {
def show(implicit ctx: Context, s: Show[tasty.type]): String = s.showTypeOrBounds(tpe)
def show(implicit ctx: Context): String = showExtractors.showTypeOrBounds(tpe)
def showCode(implicit ctx: Context): String = showSourceCode.showTypeOrBounds(tpe)
}

/** Adds `show` as an extension method of a `CaseDef` */
def CaseDefShowDeco(caseDef: CaseDef): ShowAPI = new ShowAPI {
def show(implicit ctx: Context, s: Show[tasty.type]): String = s.showCaseDef(caseDef)
def show(implicit ctx: Context): String = showExtractors.showCaseDef(caseDef)
def showCode(implicit ctx: Context): String = showSourceCode.showCaseDef(caseDef)
}

/** Adds `show` as an extension method of a `Pattern` */
def PatternShowDeco(pattern: Pattern): ShowAPI = new ShowAPI {
def show(implicit ctx: Context, s: Show[tasty.type]): String = s.showPattern(pattern)
def show(implicit ctx: Context): String = showExtractors.showPattern(pattern)
def showCode(implicit ctx: Context): String = showSourceCode.showPattern(pattern)
}

/** Adds `show` as an extension method of a `Constant` */
def ConstantShowDeco(const: Constant): ShowAPI = new ShowAPI {
def show(implicit ctx: Context, s: Show[tasty.type]): String = s.showConstant(const)
def show(implicit ctx: Context): String = showExtractors.showConstant(const)
def showCode(implicit ctx: Context): String = showSourceCode.showConstant(const)
}

/** Adds `show` as an extension method of a `Symbol` */
def SymbolShowDeco(symbol: Symbol): ShowAPI = new ShowAPI {
def show(implicit ctx: Context, s: Show[tasty.type]): String = s.showSymbol(symbol)
def show(implicit ctx: Context): String = showExtractors.showSymbol(symbol)
def showCode(implicit ctx: Context): String = showSourceCode.showSymbol(symbol)
}

}
12 changes: 6 additions & 6 deletions compiler/src/dotty/tools/dotc/tastyreflect/QuotedOpsImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import dotty.tools.dotc.core.quoted.PickledQuotes
import dotty.tools.dotc.reporting.Reporter
import dotty.tools.dotc.reporting.diagnostic.MessageContainer

trait QuotedOpsImpl extends scala.tasty.reflect.QuotedOps with TastyCoreImpl {
trait QuotedOpsImpl extends scala.tasty.reflect.QuotedOps with CoreImpl {

def QuotedExprDeco[T](x: scala.quoted.Expr[T]): QuotedExprAPI = new QuotedExprAPI {
def toTasty(implicit ctx: Context): Term = PickledQuotes.quotedExprToTree(x)
def reflect(implicit ctx: Context): Term = PickledQuotes.quotedExprToTree(x)
}

def QuotedTypeDeco[T](x: scala.quoted.Type[T]): QuotedTypeAPI = new QuotedTypeAPI {
def toTasty(implicit ctx: Context): TypeTree = PickledQuotes.quotedTypeToTree(x)
def reflect(implicit ctx: Context): TypeTree = PickledQuotes.quotedTypeToTree(x)
}

def TermToQuoteDeco(term: Term): TermToQuotedAPI = new TermToQuotedAPI {

def toExpr[T: scala.quoted.Type](implicit ctx: Context): scala.quoted.Expr[T] = {
def reify[T: scala.quoted.Type](implicit ctx: Context): scala.quoted.Expr[T] = {
typecheck(ctx)
new scala.quoted.Exprs.TastyTreeExpr(term).asInstanceOf[scala.quoted.Expr[T]]
}
Expand All @@ -28,12 +28,12 @@ trait QuotedOpsImpl extends scala.tasty.reflect.QuotedOps with TastyCoreImpl {
ctx0.typerState.setReporter(new Reporter {
def doReport(m: MessageContainer)(implicit ctx: Context): Unit = ()
})
val tp = QuotedTypeDeco(implicitly[scala.quoted.Type[T]]).toTasty
val tp = QuotedTypeDeco(implicitly[scala.quoted.Type[T]]).reflect
ctx0.typer.typed(term, tp.tpe)
if (ctx0.reporter.hasErrors) {
val stack = new Exception().getStackTrace
def filter(elem: StackTraceElement) =
elem.getClassName.startsWith("dotty.tools.dotc.tasty.TastyImpl") ||
elem.getClassName.startsWith("dotty.tools.dotc.tasty.ReflectionImpl") ||
!elem.getClassName.startsWith("dotty.tools.dotc")
throw new scala.tasty.TastyTypecheckError(
s"""Error during tasty reflection while typing term
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package dotty.tools.dotc.tastyreflect

import dotty.tools.dotc.core._

class TastyImpl(val rootContext: Contexts.Context)
extends scala.tasty.Tasty
with TastyCoreImpl
class ReflectionImpl(val rootContext: Contexts.Context)
extends scala.tasty.Reflection
with CoreImpl
with CaseDefOpsImpl
with ConstantOpsImpl
with ContextOpsImpl
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package dotty.tools.dotc.tastyreflect

trait SettingsOpsImpl extends scala.tasty.reflect.SettingsOps with TastyCoreImpl {
trait SettingsOpsImpl extends scala.tasty.reflect.SettingsOps with scala.tasty.reflect.ContextOps with CoreImpl {

def settings(implicit ctx: Context): Settings = ctx.settings
def settings: Settings = rootContext.settings

def SettingsDeco(settings: Settings): SettingsAPI = new SettingsAPI {
def color(implicit ctx: Context): Boolean = settings.color.value == "always"
def color: Boolean = settings.color.value == "always"
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dotty.tools.dotc.tastyreflect

trait SignatureOpsImpl extends scala.tasty.reflect.SignatureOps with TastyCoreImpl {
trait SignatureOpsImpl extends scala.tasty.reflect.SignatureOps with CoreImpl {

object Signature extends SignatureExtractor {
def unapply(x: Signature)(implicit ctx: Context): Option[(List[String], String)] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import dotty.tools.dotc.core.Symbols._


trait StandardDefinitions extends scala.tasty.reflect.StandardDefinitions {
tasty: TastyImpl =>
tasty: ReflectionImpl =>

private implicit def ctx: Context = rootContext

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package tastyreflect
import dotty.tools.dotc.core.Flags
import dotty.tools.dotc.core.Symbols._

trait SymbolOpsImpl extends scala.tasty.reflect.SymbolOps with TastyCoreImpl {
trait SymbolOpsImpl extends scala.tasty.reflect.SymbolOps with CoreImpl {

def SymbolDeco(symbol: Symbol): SymbolAPI = new SymbolAPI {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import dotty.tools.dotc.core.Decorators._
import dotty.tools.dotc.core._
import dotty.tools.dotc.tastyreflect.FromSymbol.{definitionFromSym, packageDefFromSym}

trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with TastyCoreImpl with Helpers {
trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers {

def TreeDeco(tree: Tree): TreeAPI = new TreeAPI {
def pos(implicit ctx: Context): Position = tree.pos
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dotty.tools.dotc.tastyreflect

import dotty.tools.dotc.core.{Names, Types}

trait TypeOrBoundsOpsImpl extends scala.tasty.reflect.TypeOrBoundsOps with TastyCoreImpl {
trait TypeOrBoundsOpsImpl extends scala.tasty.reflect.TypeOrBoundsOps with CoreImpl {

// ===== Types ====================================================

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import dotty.tools.dotc.core.StdNames.nme
import dotty.tools.dotc.core.Types


trait TypeOrBoundsTreesOpsImpl extends scala.tasty.reflect.TypeOrBoundsTreeOps with TastyCoreImpl {
trait TypeOrBoundsTreesOpsImpl extends scala.tasty.reflect.TypeOrBoundsTreeOps with CoreImpl {

// ----- TypeOrBoundsTree ------------------------------------------------

Expand Down
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 @@ -18,7 +18,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.TastyImpl
import dotty.tools.dotc.tastyreflect.ReflectionImpl

import scala.util.control.NonFatal
import dotty.tools.dotc.util.SourcePosition
Expand Down Expand Up @@ -107,7 +107,7 @@ object Splicer {
args.toSeq

protected def interpretTastyContext()(implicit env: Env): Object = {
new TastyImpl(ctx) {
new ReflectionImpl(ctx) {
override def rootPosition: SourcePosition = pos
}
}
Expand Down Expand Up @@ -307,7 +307,7 @@ object Splicer {
case Literal(Constant(value)) =>
interpretLiteral(value)

case _ if tree.symbol == defn.TastyTasty_macroContext =>
case _ if tree.symbol == defn.TastyReflection_macroContext =>
interpretTastyContext()

case Call(fn, args) =>
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/Staging.scala
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ class Staging extends MacroTransformWithImplicits {
case Some(l) =>
l == level ||
level == -1 && (
sym == defn.TastyTasty_macroContext ||
sym == defn.TastyReflection_macroContext ||
// here we assume that Splicer.canBeSpliced was true before going to level -1,
// this implies that all non-inline arguments are quoted and that the following two cases are checked
// on inline parameters or type parameters.
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ trait Implicits { self: Typer =>
}

def synthesizedTastyContext(formal: Type): Tree =
if (ctx.inInlineMethod || enclosingInlineds.nonEmpty) ref(defn.TastyTasty_macroContext)
if (ctx.inInlineMethod || enclosingInlineds.nonEmpty) ref(defn.TastyReflection_macroContext)
else EmptyTree

/** If `formal` is of the form Eq[T, U], where no `Eq` instance exists for
Expand Down Expand Up @@ -698,7 +698,7 @@ trait Implicits { self: Typer =>
else
trySpecialCase(defn.ClassTagClass, synthesizedClassTag,
trySpecialCase(defn.QuotedTypeClass, synthesizedTypeTag,
trySpecialCase(defn.TastyTastyClass, synthesizedTastyContext,
trySpecialCase(defn.TastyReflectionClass, synthesizedTastyContext,
trySpecialCase(defn.EqClass, synthesizedEq, failed))))
}
}
Expand Down
Loading