Skip to content

Split TASTy reflect interface and implementation #4905

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 2 commits into from
Aug 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
21 changes: 21 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/CaseDefOpsImpl.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dotty.tools.dotc.tastyreflect

import dotty.tools.dotc.ast.tpd

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

def CaseDefDeco(caseDef: CaseDef): CaseDefAPI = new CaseDefAPI {
def pattern(implicit ctx: Context): Pattern = caseDef.pat
def guard(implicit ctx: Context): Option[Term] = optional(caseDef.guard)
def rhs(implicit ctx: Context): Term = caseDef.body
}

object CaseDef extends CaseDefExtractor {
def unapply(x: CaseDef): Option[(Pattern, Option[Term], Term)] = x match {
case x: tpd.CaseDef =>
Some(x.pat, optional(x.guard), x.body)
case _ => None
}
}

}
107 changes: 107 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/ConstantOpsImpl.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package dotty.tools.dotc.tastyreflect

import dotty.tools.dotc.core.Constants

import scala.tasty.util.Show

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

def ConstantDeco(const: Constant): ConstantAPI = new ConstantAPI {
def value: Any = const.value
}

object Constant extends ConstantModule {

object Unit extends UnitExtractor {
def unapply(x: Constant): Boolean = x match {
case x: Constants.Constant => x.tag == Constants.UnitTag
case _ => false
}
}

object Null extends NullExtractor {
def unapply(x: Constant): Boolean = x match {
case x: Constants.Constant => x.tag == Constants.NullTag
case _ => false
}
}

object Boolean extends BooleanExtractor {
def unapply(x: Constant): Option[Boolean] = x match {
case x: Constants.Constant if x.tag == Constants.BooleanTag => Some(x.booleanValue)
case _ => None
}
}

object Byte extends ByteExtractor {
def unapply(x: Constant): Option[Byte] = x match {
case x: Constants.Constant if x.tag == Constants.ByteTag => Some(x.byteValue)
case _ => None
}
}

object Short extends ShortExtractor {
def unapply(x: Constant): Option[Short] = x match {
case x: Constants.Constant if x.tag == Constants.ShortTag => Some(x.shortValue)
case _ => None
}
}

object Char extends CharExtractor {
def unapply(x: Constant): Option[Char] = x match {
case x: Constants.Constant if x.tag == Constants.CharTag => Some(x.charValue)
case _ => None
}
}

object Int extends IntExtractor {
def unapply(x: Constant): Option[Int] = x match {
case x: Constants.Constant if x.tag == Constants.IntTag => Some(x.intValue)
case _ => None
}
}

object Long extends LongExtractor {
def unapply(x: Constant): Option[Long] = x match {
case x: Constants.Constant if x.tag == Constants.LongTag => Some(x.longValue)
case _ => None
}
}

object Float extends FloatExtractor {
def unapply(x: Constant): Option[Float] = x match {
case x: Constants.Constant if x.tag == Constants.FloatTag => Some(x.floatValue)
case _ => None
}
}

object Double extends DoubleExtractor {
def unapply(x: Constant): Option[Double] = x match {
case x: Constants.Constant if x.tag == Constants.DoubleTag => Some(x.doubleValue)
case _ => None
}
}

object String extends StringExtractor {
def unapply(x: Constant): Option[String] = x match {
case x: Constants.Constant if x.tag == Constants.StringTag => Some(x.stringValue)
case _ => None
}
}

object ClassTag extends ClassTagExtractor {
def unapply(x: Constant): Option[Type] = x match {
case x: Constants.Constant if x.tag == Constants.ClazzTag => Some(x.typeValue)
case _ => None
}
}

object Symbol extends SymbolExtractor {
def unapply(x: Constant): Option[scala.Symbol] = x match {
case x: Constants.Constant if x.tag == Constants.ScalaSymbolTag => Some(x.scalaSymbolValue)
case _ => None
}
}
}

}
19 changes: 19 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/ContextOpsImpl.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dotty.tools.dotc.tastyreflect

import dotty.tools.dotc.core.Contexts
import dotty.tools.dotc.tastyreflect.FromSymbol.definitionFromSym
import dotty.tools.dotc.util.{Positions, SourcePosition}

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

val rootContext: Contexts.Context

def ContextDeco(ctx: Context): ContextAPI = new ContextAPI {
def owner: Definition = definitionFromSym(ctx.owner)(ctx)

def source: java.nio.file.Path = ctx.compilationUnit.source.file.jpath
}

def rootPosition: SourcePosition = SourcePosition(rootContext.source, Positions.NoPosition)

}
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/tastyreflect/FlagSet.scala
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.Flags
import dotty.tools.dotc.core.Flags._

class FlagSet(flags: Flags.FlagSet) extends scala.tasty.FlagSet {
class FlagSet(flags: Flags.FlagSet) extends scala.tasty.reflect.FlagSet {

def isProtected: Boolean = flags.is(Protected)
def isAbstract: Boolean = flags.is(Abstract)
Expand Down
10 changes: 10 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/Helpers.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dotty.tools.dotc.tastyreflect

import dotty.tools.dotc.ast.Trees

trait Helpers {

protected final def optional[T <: Trees.Tree[_]](tree: T): Option[tree.type] =
if (tree.isEmpty) None else Some(tree)

}
16 changes: 16 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/IdOpsImpl.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dotty.tools.dotc.tastyreflect

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

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

def IdDeco(id: Id): IdAPI = new IdAPI {
def pos(implicit ctx: Context): Position = id.pos
def name(implicit ctx: Context): String = id.name.toString
}

object Id extends IdExtractor {
def unapply(id: Id): Option[String] = Some(id.name.toString)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 {

object SimpleSelector extends SimpleSelectorExtractor {
def unapply(x: ImportSelector)(implicit ctx: Context): Option[Id] = x match {
case x: untpd.Ident => Some(x)
case _ => None
}
}

object RenameSelector extends RenameSelectorExtractor {
def unapply(x: ImportSelector)(implicit ctx: Context): Option[(Id, Id)] = x match {
case Trees.Thicket((id1: untpd.Ident) :: (id2: untpd.Ident) :: Nil) if id2.name != nme.WILDCARD => Some(id1, id2)
case _ => None
}
}

object OmitSelector extends OmitSelectorExtractor {
def unapply(x: ImportSelector)(implicit ctx: Context): Option[Id] = x match {
case Trees.Thicket((id: untpd.Ident) :: Trees.Ident(nme.WILDCARD) :: Nil) => Some(id)
case _ => None
}
}

}
65 changes: 65 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/PatternOpsImpl.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package dotty.tools.dotc.tastyreflect

import dotty.tools.dotc.ast.{Trees, tpd}
import dotty.tools.dotc.core.Decorators._
import dotty.tools.dotc.core.Types

import scala.tasty.util.Show

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

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

def PatternDeco(pattern: Pattern): PatternAPI = new PatternAPI {
def pos(implicit ctx: Context): Position = pattern.pos
def tpe(implicit ctx: Context): Types.Type = pattern.tpe.stripTypeVar
}

object Pattern extends PatternModule {

object Value extends ValueExtractor {
def unapply(x: Pattern)(implicit ctx: Context): Option[Term] = x match {
case lit: tpd.Literal => Some(lit)
case ref: tpd.RefTree if ref.isTerm => Some(ref)
case ths: tpd.This => Some(ths)
case _ => None
}
}

object Bind extends BindExtractor {
def unapply(x: Pattern)(implicit ctx: Context): Option[(String, Pattern)] = x match {
case x: tpd.Bind if x.name.isTermName => Some(x.name.toString, x.body)
case _ => None
}
}

object Unapply extends UnapplyExtractor {
def unapply(x: Pattern)(implicit ctx: Context): Option[(Term, List[Term], List[Pattern])] = x match {
case Trees.UnApply(fun, implicits, patterns) => Some((fun, implicits, effectivePatterns(patterns)))
case Trees.Typed(Trees.UnApply(fun, implicits, patterns), _) => Some((fun, implicits, effectivePatterns(patterns)))
case _ => None
}
private def effectivePatterns(patterns: List[Pattern]): List[Pattern] = patterns match {
case patterns0 :+ Trees.SeqLiteral(elems, _) => patterns0 ::: elems
case _ => patterns
}
}

object Alternative extends AlternativeExtractor {
def unapply(x: Pattern)(implicit ctx: Context): Option[List[Pattern]] = x match {
case x: tpd.Alternative => Some(x.trees)
case _ => None
}
}

object TypeTest extends TypeTestExtractor {
def unapply(x: Pattern)(implicit ctx: Context): Option[TypeTree] = x match {
case Trees.Typed(Trees.UnApply(_, _, _), _) => None
case Trees.Typed(_, tpt) => Some(tpt)
case _ => None
}
}

}

}
18 changes: 18 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/PositionOpsImpl.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dotty.tools.dotc.tastyreflect

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

def PositionDeco(pos: Position): PositionAPI = new PositionAPI {
def start: Int = pos.start
def end: Int = pos.end

def sourceFile: java.nio.file.Path = pos.source.file.jpath

def startLine: Int = pos.startLine
def endLine: Int = pos.endLine

def startColumn: Int = pos.startColumn
def endColumn: Int = pos.endColumn
}

}
43 changes: 43 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/PrintersImpl.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dotty.tools.dotc.tastyreflect

import dotty.tools.dotc.core.Contexts

import scala.tasty.util.{Show, ShowExtractors, ShowSourceCode}

trait PrintersImpl extends scala.tasty.reflect.Printers with scala.tasty.reflect.TastyCore { tasty: TastyImpl =>

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

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

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

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

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

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

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

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

}
49 changes: 49 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/QuotedOpsImpl.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package dotty.tools.dotc.tastyreflect

import dotty.tools.dotc.core.Contexts
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 {

def QuotedExprDeco[T](x: scala.quoted.Expr[T]): QuotedExprAPI = new QuotedExprAPI {
def toTasty(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 TermToQuoteDeco(term: Term): TermToQuotedAPI = new TermToQuotedAPI {

def toExpr[T: scala.quoted.Type](implicit ctx: Context): scala.quoted.Expr[T] = {
typecheck(ctx)
new scala.quoted.Exprs.TastyTreeExpr(term).asInstanceOf[scala.quoted.Expr[T]]
}

private def typecheck[T: scala.quoted.Type](ctx: Contexts.Context): Unit = {
implicit val ctx0: Contexts.FreshContext = ctx.fresh
ctx0.setTyperState(ctx0.typerState.fresh())
ctx0.typerState.setReporter(new Reporter {
def doReport(m: MessageContainer)(implicit ctx: Contexts.Context): Unit = ()
})
val tp = QuotedTypeDeco(implicitly[scala.quoted.Type[T]]).toTasty
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")
throw new scala.tasty.TastyTypecheckError(
s"""Error during tasty reflection while typing term
|term: ${term.show}
|with expected type: ${tp.tpe.show}
|
| ${stack.takeWhile(filter).mkString("\n ")}
""".stripMargin
)
}
}
}
}
Loading