Skip to content

Simplify Tasty Trees #4277

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 5 commits into from
Apr 10, 2018
Merged
Changes from 1 commit
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
110 changes: 52 additions & 58 deletions tests/pos/TastyADT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ object tasty {
case DefaultGetter(methodName: TermName, idx: String) // s"$methodName${"$default$"}${idx+1}"
case Variant(underlying: TermName, covariant: Boolean) // s"${if (covariant) "+" else "-"}$underlying"
case SuperAccessor(underlying: TermName) // s"${"super$"}$underlying"
case ProtectedAccessor(underlying: TermName) // s"${"protectded$"}$underlying"
case ProtectecSetter(underlying: TermName) // s"${"protectded$set"}$underlying"
case ProtectedAccessor(underlying: TermName) // s"${"protected$"}$underlying"
case ProtectedSetter(underlying: TermName) // s"${"protected$set"}$underlying"
case ObjectClass(underlying: TermName) // s"$underlying${"$"}"

case Expanded(prefix: TermName, selector: String) // s"$prefix${"$$"}$name" , used only for symbols coming from Scala 2
Expand All @@ -34,8 +34,8 @@ object tasty {

// ------ Statements ---------------------------------

trait TopLevelStatement extends Positioned
trait Statement extends TopLevelStatement
sealed trait TopLevelStatement extends Positioned
sealed trait Statement extends TopLevelStatement

case class Package(pkg: Term, body: List[TopLevelStatement]) extends TopLevelStatement

Expand All @@ -51,22 +51,17 @@ object tasty {

// ------ Definitions ---------------------------------

class Symbol {
def owner: Symbol = ???
def definition: Definition = ???
trait Definition extends Statement {
def name: Name
def owner: Definition
}
object NoSymbol extends Symbol

trait Definition {
def sym: Symbol = ???
}

case class ValDef(name: TermName, tpt: Term, rhs: Term | Empty, mods: List[Modifier]) extends Definition
case class ValDef(name: TermName, tpt: Term, rhs: Option[Term], mods: List[Modifier]) extends Definition
case class DefDef(name: TermName, typeParams: List[TypeDef], paramss: List[List[ValDef]],
returnTpt: Term, rhs: Term | Empty, mods: List[Modifier]) extends Definition
returnTpt: Term, rhs: Option[Term], mods: List[Modifier]) extends Definition
case class TypeDef(name: TypeName, rhs: Term, mods: List[Modifier]) extends Definition
case class ClassDef(name: TypeName, constructor: DefDef, parents: List[Term],
self: ValDef | Empty, body: List[Statement], mods: List[Modifier]) extends Definition
self: Option[ValDef], body: List[Statement], mods: List[Modifier]) extends Definition

// ------ Terms ---------------------------------

Expand All @@ -76,39 +71,39 @@ object tasty {
case Ident(name: TermName, override val tpe: Type)
case Select(prefix: Term, name: PossiblySignedName)
case Literal(value: Constant)
case This(id: Id | Empty)
case New(tpt: Term)
case This(id: Option[Id])
case New(tpt: TypeTree)
case NamedArg(name: TermName, arg: Term)
case Apply(fn: Term, args: List[Term])
case TypeApply(fn: Term, args: List[Term])
case Super(thiz: Term, mixin: Id | Empty)
case Typed(expr: Term, tpt: Term)
case TypeApply(fn: Term, args: List[TypeTree])
case Super(thiz: Term, mixin: Option[Id])
case Typed(expr: Term, tpt: TypeTree)
case Assign(lhs: Term, rhs: Term)
case Block(stats: List[Statement], expr: Term)
case Inlined(call: Term, bindings: List[Definition], expr: Term)
case Lambda(method: Term, tpt: Term | Empty)
case Lambda(method: Term, tpt: Option[TypeTree])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is tpt optional here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the doc is wrong. https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala#L1022-L1025

The docs should be updated to

LAMBDA         Length meth_Term target_Type?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #4285 to fix this

case If(cond: Term, thenPart: Term, elsePart: Term)
case Match(scrutinee: Term, cases: List[CaseDef])
case Try(body: Term, catches: List[CaseDef], finalizer: Term | Empty)
case Try(body: Term, catches: List[CaseDef], finalizer: Option[Term])
case Return(expr: Term)
case Repeated(args: List[Term])
case SelectOuter(from: Term, levels: Int, target: Type) // can be generated by inlining
case Tpt(underlying: TypeTerm | Empty)
}

/** Trees denoting types */
enum TypeTerm extends Positioned {
enum TypeTree extends Positioned {
def tpe: Type = ???
case Synthetic()
case Ident(name: TypeName, override val tpe: Type)
case Select(prefix: Term, name: TypeName)
case Singleton(ref: Term)
case Refined(underlying: TypeTerm, refinements: List[Definition])
case Applied(tycon: TypeTerm, args: List[TypeTerm])
case TypeBounds(loBound: TypeTerm, hiBound: TypeTerm)
case Annotated(tpt: TypeTerm, annotation: Term)
case And(left: TypeTerm, right: TypeTerm)
case Or(left: TypeTerm, right: TypeTerm)
case ByName(tpt: TypeTerm)
case Refined(underlying: TypeTree, refinements: List[Definition])
case Applied(tycon: TypeTree, args: List[TypeTree])
case TypeBounds(loBound: TypeTree, hiBound: TypeTree)
case Annotated(tpt: TypeTree, annotation: Term)
case And(left: TypeTree, right: TypeTree)
case Or(left: TypeTree, right: TypeTree)
case ByName(tpt: TypeTree)
}

/** Trees denoting patterns */
Expand All @@ -118,22 +113,22 @@ object tasty {
case Bind(name: TermName, pat: Pattern)
case Unapply(unapply: Term, implicits: List[Term], pats: List[Pattern])
case Alternative(pats: List[Pattern])
case TypeTest(tpt: Term)
case TypeTest(tpt: TypeTree)
case Wildcard()
}

case class CaseDef(pat: Pattern, guard: Term | Empty, rhs: Term) extends Positioned
case class CaseDef(pat: Pattern, guard: Option[Term], rhs: Term) extends Positioned

// ------ Types ---------------------------------

sealed trait Type

object Type {
private val PlaceHolder = SymRef(NoSymbol, Empty)
private val PlaceHolder = ConstantType(Constant.Unit)

case class ConstantType(value: Constant) extends Type
case class SymRef(sym: Symbol, qualifier: Type | Empty = Empty) extends Type
case class NameRef(name: Name, qualifier: Type | Empty = Empty) extends Type // Empty means: select from _root_
case class SymRef(sym: Definition, qualifier: Type | NoPrefix = NoPrefix) extends Type
case class NameRef(name: Name, qualifier: Type | NoPrefix = NoPrefix) extends Type // NoPrefix means: select from _root_
case class SuperType(thistp: Type, underlying: Type) extends Type
case class Refinement(underlying: Type, name: Name, tpe: Type) extends Type
case class AppliedType(tycon: Type, args: List[Type | TypeBounds]) extends Type
Expand Down Expand Up @@ -202,6 +197,8 @@ object tasty {
object ErasedImplicitMethodType extends SpecializedMethodTypeCompanion

case class TypeBounds(loBound: Type, hiBound: Type)
case class NoPrefix()
object NoPrefix extends NoPrefix
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why both a class and an object for NoPrefix?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To allow NoPrefix to be used in | types and be a singleton at the same time.

}

// ------ Modifiers ---------------------------------
Expand Down Expand Up @@ -232,35 +229,32 @@ object tasty {

// ------ Constants ---------------------------------

enum Constant(value: Any) {
case Unit extends Constant(())
case False extends Constant(false)
case True extends Constant(true)
case Null extends Constant(null)
case Byte(value: scala.Byte) extends Constant(value)
case Short(value: scala.Short) extends Constant(value)
case Char(value: scala.Char) extends Constant(value)
case Int(value: scala.Int) extends Constant(value)
case Long(value: scala.Long) extends Constant(value)
case Float(value: scala.Float) extends Constant(value)
case Double(value: scala.Double) extends Constant(value)
case String(value: java.lang.String) extends Constant(value)
case Class(value: Type) extends Constant(value)
case Enum(value: Type) extends Constant(value)
enum Constant(val value: Any) {
case Unit extends Constant(())
case False extends Constant(false)
case True extends Constant(true)
case Null extends Constant(null)
case Byte(v: scala.Byte) extends Constant(v)
case Short(v: scala.Short) extends Constant(v)
case Char(v: scala.Char) extends Constant(v)
case Int(v: scala.Int) extends Constant(v)
case Long(v: scala.Long) extends Constant(v)
case Float(v: scala.Float) extends Constant(v)
case Double(v: scala.Double) extends Constant(v)
case String(v: java.lang.String) extends Constant(v)
case Class(v: Type) extends Constant(v)
case Enum(v: Type) extends Constant(v)
}

sealed class Empty()
object Empty extends Empty
}

object Test {
import tasty._
import Type._

def show(tp: Type) = tp match {
case ConstantType(value) => ???
case SymRef(sym, Empty) => ???
case SymRef(sym, qual) => ???
def show(tp: Type): String = tp match {
case ConstantType(c) => c.value.toString
case SymRef(sym, NoPrefix) => ???
case SymRef(sym, NoPrefix) => ???
case NameRef(name: Name, qualifier) => ???
case SuperType(thistp: Type, underlying: Type) => ???
case Refinement(underlying: Type, name: Name, tpe: Type) => ???
Expand Down