Skip to content

Commit c0c3765

Browse files
committed
Remove all case classes from internal representation
1 parent 9dc23b8 commit c0c3765

25 files changed

+481
-425
lines changed

compiler/src/dotty/tools/dotc/tasty/Toolbox.scala

Lines changed: 101 additions & 88 deletions
Large diffs are not rendered by default.

compiler/src/dotty/tools/dotc/tasty/internal/AnnotationModifier.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import scala.tasty.modifiers
99

1010
object AnnotationModifier {
1111

12-
def apply(tree: Annotation)(implicit ctx: Context): modifiers.Modifier = Impl(tree, ctx)
12+
def apply(tree: Annotation)(implicit ctx: Context): modifiers.Modifier = new Impl(tree)
1313

14-
def unapplyAnnotation(mod: modifiers.Modifier): Option[modifiers.Annotation.Data] = mod match {
15-
case Impl(annot, ctx) => Some(Term(annot.tree(ctx))(ctx))
16-
case _ => None
14+
def unapplyAnnotation(arg: Impl): Option[modifiers.Annotation.Data] = {
15+
implicit val ctx: Context = arg.ctx
16+
Some(Term(arg.annot.tree))
1717
}
1818

19-
private case class Impl(annot: Annotation, ctx: Context) extends modifiers.Modifier {
19+
private[tasty] class Impl(val annot: Annotation)(implicit val ctx: Context) extends modifiers.Modifier {
2020

2121
def pos: scala.tasty.Position = new Position(sourcePos(annot.tree(ctx).pos)(ctx))
2222

compiler/src/dotty/tools/dotc/tasty/internal/CaseDef.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import scala.tasty.trees
99

1010
object CaseDef {
1111

12-
def apply(tree: tpd.Tree)(implicit ctx: Context): trees.CaseDef = Impl(tree, ctx)
12+
def apply(tree: tpd.CaseDef)(implicit ctx: Context): trees.CaseDef = new Impl(tree)
1313

14-
def unapplyCaseDef(tree: trees.Tree): Option[trees.CaseDef.Data] = tree match {
15-
case Impl(Trees.CaseDef(pat, guard, body), ctx) =>
16-
Some(Pattern(pat)(ctx), if (guard.isEmpty) None else Some(Term(guard)(ctx)), Term(body)(ctx))
17-
case _ => None
14+
def unapplyCaseDef(arg: Impl): Option[trees.CaseDef.Data] = {
15+
implicit val ctx: Context = arg.ctx
16+
val Trees.CaseDef(pat, guard, body) = arg.tree
17+
Some(Pattern(pat), if (guard.isEmpty) None else Some(Term(guard)), Term(body))
1818
}
1919

20-
private case class Impl(tree: tpd.Tree, ctx: Context) extends trees.CaseDef with Positioned {
20+
private[tasty] class Impl(val tree: tpd.CaseDef)(implicit val ctx: Context) extends trees.CaseDef with Positioned {
2121
override def toString: String = {
2222
import Toolbox.extractor
2323
val trees.CaseDef(pat, guard, body) = this

compiler/src/dotty/tools/dotc/tasty/internal/ClassDef.scala

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,21 @@ import scala.tasty.types
99

1010
object ClassDef {
1111

12-
def apply(tree: tpd.TypeDef)(implicit ctx: Context): trees.ClassDef = Impl(tree, ctx)
13-
14-
def unapplyClassDef(tree: trees.Tree): Option[trees.ClassDef.Data] = tree match {
15-
case Impl(cdef @ Trees.TypeDef(name, impl@Trees.Template(constr, parents, self, _)), ctx) =>
16-
implicit val ctx_ = ctx
17-
if (cdef.symbol.isClass) {
18-
val className = TypeName(name)
19-
val constructor = DefDef(constr)
20-
val classParents = parents.map(p => if (!p.isType) Term(p) else TypeTree(p))
21-
val selfVal = if (self.isEmpty) None else Some(ValDef(self))
22-
val body = impl.body.map(Statement(_))
23-
val mods = Modifiers(cdef)
24-
Some((className, constructor, classParents, selfVal, body, mods))
25-
}
26-
else None
27-
case _ => None
12+
def apply(tree: tpd.TypeDef)(implicit ctx: Context): trees.ClassDef = new Impl(tree)
13+
14+
def unapplyClassDef(arg: Impl): Option[trees.ClassDef.Data] = {
15+
implicit val ctx: Context = arg.ctx
16+
val Trees.TypeDef(name, impl@Trees.Template(constr, parents, self, _)) = arg.tree
17+
val className = TypeName(name)
18+
val constructor = DefDef(constr)
19+
val classParents = parents.map(p => if (!p.isType) Term(p) else TypeTree(p))
20+
val selfVal = if (self.isEmpty) None else Some(ValDef(self))
21+
val body = impl.body.map(Statement(_))
22+
val mods = Modifiers(arg.tree)
23+
Some((className, constructor, classParents, selfVal, body, mods))
2824
}
2925

30-
private case class Impl(tree: tpd.TypeDef, ctx: Context) extends trees.ClassDef with Positioned {
26+
private[tasty] class Impl(val tree: tpd.TypeDef)(implicit val ctx: Context) extends trees.ClassDef with Positioned {
3127

3228
def tpe: types.Type = Type(tree.tpe)(ctx)
3329

compiler/src/dotty/tools/dotc/tasty/internal/Constant.scala

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,49 @@ import scala.tasty.constants
77

88
object Constant {
99

10-
def apply(constant: Constants.Constant): constants.Constant = Impl(constant)
10+
def apply(constant: Constants.Constant): constants.Constant = new Impl(constant)
1111

12-
def unapplyUnit(const: constants.Constant): Boolean = const match {
13-
case Impl(const) => const.tag == Constants.UnitTag
14-
case _ => false
15-
}
12+
def unapplyUnit(arg: Impl): Boolean = arg.const.tag == Constants.UnitTag
1613

17-
def unapplyNull(const: constants.Constant): Boolean = const match {
18-
case Impl(const) => const.tag == Constants.NullTag
19-
case _ => false
20-
}
14+
def unapplyNull(arg: Impl): Boolean = arg.const.tag == Constants.NullTag
2115

22-
def unapplyBoolean(const: constants.Constant): Option[Boolean] = const match {
23-
case Impl(const) if const.tag == Constants.BooleanTag => Some(const.booleanValue)
24-
case _ => None
25-
}
16+
def unapplyBoolean(arg: Impl): Option[Boolean] =
17+
if (arg.const.tag == Constants.BooleanTag) Some(arg.const.booleanValue)
18+
else None
2619

27-
def unapplyByte(const: constants.Constant): Option[Byte] = const match {
28-
case Impl(const) if const.tag == Constants.ByteTag => Some(const.byteValue)
29-
case _ => None
30-
}
20+
def unapplyByte(arg: Impl): Option[Byte] =
21+
if (arg.const.tag == Constants.ByteTag) Some(arg.const.byteValue)
22+
else None
3123

32-
def unapplyChar(const: constants.Constant): Option[Char] = const match {
33-
case Impl(const) if const.tag == Constants.CharTag => Some(const.charValue)
34-
case _ => None
35-
}
24+
def unapplyChar(arg: Impl): Option[Char] =
25+
if (arg.const.tag == Constants.CharTag) Some(arg.const.charValue)
26+
else None
3627

37-
def unapplyShort(const: constants.Constant): Option[Short] = const match {
38-
case Impl(const) if const.tag == Constants.ShortTag => Some(const.shortValue)
39-
case _ => None
40-
}
28+
def unapplyShort(arg: Impl): Option[Short] =
29+
if (arg.const.tag == Constants.ShortTag) Some(arg.const.shortValue)
30+
else None
4131

42-
def unapplyInt(const: constants.Constant): Option[Int] = const match {
43-
case Impl(const) if const.tag == Constants.IntTag => Some(const.intValue)
44-
case _ => None
45-
}
32+
def unapplyInt(arg: Impl): Option[Int] =
33+
if (arg.const.tag == Constants.IntTag) Some(arg.const.intValue)
34+
else None
4635

47-
def unapplyLong(const: constants.Constant): Option[Long] = const match {
48-
case Impl(const) if const.tag == Constants.LongTag => Some(const.longValue)
49-
case _ => None
50-
}
36+
def unapplyLong(arg: Impl): Option[Long] =
37+
if (arg.const.tag == Constants.LongTag) Some(arg.const.longValue)
38+
else None
5139

52-
def unapplyFloat(const: constants.Constant): Option[Float] = const match {
53-
case Impl(const) if const.tag == Constants.FloatTag => Some(const.floatValue)
54-
case _ => None
55-
}
40+
def unapplyFloat(arg: Impl): Option[Float] =
41+
if (arg.const.tag == Constants.FloatTag) Some(arg.const.floatValue)
42+
else None
5643

57-
def unapplyDouble(const: constants.Constant): Option[Double] = const match {
58-
case Impl(const) if const.tag == Constants.DoubleTag => Some(const.doubleValue)
59-
case _ => None
60-
}
44+
def unapplyDouble(arg: Impl): Option[Double] =
45+
if (arg.const.tag == Constants.DoubleTag) Some(arg.const.doubleValue)
46+
else None
6147

62-
def unapplyString(const: constants.Constant): Option[String] = const match {
63-
case Impl(const) if const.tag == Constants.StringTag => Some(const.stringValue)
64-
case _ => None
65-
}
48+
def unapplyString(arg: Impl): Option[String] =
49+
if (arg.const.tag == Constants.StringTag) Some(arg.const.stringValue)
50+
else None
6651

67-
private[tasty] case class Impl(const: Constants.Constant) extends constants.Constant {
52+
private[tasty] class Impl(val const: Constants.Constant) extends constants.Constant {
6853

6954
def value: Any = const.value
7055

compiler/src/dotty/tools/dotc/tasty/internal/DefDef.scala

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@ import scala.tasty.types
99

1010
object DefDef {
1111

12-
def apply(tree: tpd.DefDef)(implicit ctx: Context): trees.DefDef = Impl(tree, ctx)
12+
def apply(tree: tpd.DefDef)(implicit ctx: Context): trees.DefDef = new Impl(tree)
1313

14-
def unapplyDefDef(tree: trees.Tree): Option[trees.DefDef.Data] = tree match {
15-
case Impl(ddef, ctx) =>
16-
implicit val localContext = ctx.withOwner(ddef.symbol(ctx))
17-
Some((TermName(ddef.name), ddef.tparams.map(TypeDef(_)), ddef.vparamss.map(_.map(ValDef(_))), TypeTree(ddef.tpt), if (ddef.rhs.isEmpty) None else Some(Term(ddef.rhs)), Modifiers(ddef)))
18-
case _ => None
14+
def unapplyDefDef(arg: Impl): Option[trees.DefDef.Data] = {
15+
val ddef = arg.tree
16+
implicit val localContext: Context = arg.ctx.withOwner(ddef.symbol(arg.ctx))
17+
Some((TermName(ddef.name), ddef.tparams.map(TypeDef(_)), ddef.vparamss.map(_.map(ValDef(_))), TypeTree(ddef.tpt), if (ddef.rhs.isEmpty) None else Some(Term(ddef.rhs)), Modifiers(ddef)))
1918
}
2019

21-
private case class Impl(tree: tpd.DefDef, ctx: Context) extends trees.DefDef with Positioned {
20+
private[tasty] class Impl(val tree: tpd.DefDef)(implicit val ctx: Context) extends trees.DefDef with Positioned {
2221

2322
def tpe: types.Type = Type(tree.tpe)(ctx)
2423

compiler/src/dotty/tools/dotc/tasty/internal/Import.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import scala.tasty.trees
88

99
object Import {
1010

11-
def apply(tree: tpd.Tree)(implicit ctx: Context): trees.Import = Impl(tree, ctx)
11+
def apply(tree: tpd.Tree)(implicit ctx: Context): trees.Import = new Impl(tree)
1212

13-
def unapplyImport(tree: trees.Tree): Option[trees.Import.Data] = tree match {
14-
case Impl(Trees.Import(expr, selectors), ctx) => Some(Term(expr)(ctx), selectors.map(ImportSelector(_)(ctx)))
15-
case _ => None
13+
def unapplyImport(arg: Impl): Option[trees.Import.Data] = {
14+
implicit val ctx: Context = arg.ctx
15+
val Trees.Import(expr, selectors) = arg.tree
16+
Some(Term(expr), selectors.map(ImportSelector(_)))
1617
}
1718

18-
private case class Impl(tree: tpd.Tree, ctx: Context) extends trees.Import with Positioned {
19+
private[tasty] class Impl(val tree: tpd.Tree)(implicit val ctx: Context) extends trees.Import with Positioned {
1920
override def toString: String = {
2021
import Toolbox.extractor
2122
val trees.Import(pkg, body) = this

compiler/src/dotty/tools/dotc/tasty/internal/ImportSelector.scala

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,30 @@ import scala.tasty.trees
99

1010
object ImportSelector {
1111

12-
def apply(tree: untpd.Tree)(implicit ctx: Context): trees.ImportSelector = Impl(tree, ctx)
12+
def apply(tree: untpd.Tree)(implicit ctx: Context): trees.ImportSelector = new Impl(tree)
1313

14-
def unapplySimpleSelector(tree: trees.ImportSelector): Option[trees.SimpleSelector.Data] = tree match {
15-
case Impl(id@Trees.Ident(_), ctx) => Some(Id(id)(ctx))
14+
def unapplySimpleSelector(arg: Impl): Option[trees.SimpleSelector.Data] = arg.tree match {
15+
case id@Trees.Ident(_) =>
16+
implicit val ctx: Context = arg.ctx
17+
Some(Id(id))
1618
case _ => None
1719
}
1820

19-
def unapplyRenameSelector(tree: trees.ImportSelector): Option[trees.RenameSelector.Data] = tree match {
20-
case Impl(Trees.Thicket((id1@Trees.Ident(_)) :: (id2@Trees.Ident(_)) :: Nil), ctx) if id2.name != nme.WILDCARD => Some(Id(id1)(ctx), Id(id2)(ctx))
21+
def unapplyRenameSelector(arg: Impl): Option[trees.RenameSelector.Data] = arg.tree match {
22+
case Trees.Thicket((id1@Trees.Ident(_)) :: (id2@Trees.Ident(_)) :: Nil) if id2.name != nme.WILDCARD =>
23+
implicit val ctx: Context = arg.ctx
24+
Some(Id(id1), Id(id2))
2125
case _ => None
2226
}
2327

24-
def unapplyOmitSelector(tree: trees.ImportSelector): Option[trees.OmitSelector.Data] = tree match {
25-
case Impl(Trees.Thicket((id@Trees.Ident(_)) :: Trees.Ident(nme.WILDCARD) :: Nil), ctx) => Some(Id(id)(ctx))
28+
def unapplyOmitSelector(arg: Impl): Option[trees.OmitSelector.Data] = arg.tree match {
29+
case Trees.Thicket((id@Trees.Ident(_)) :: Trees.Ident(nme.WILDCARD) :: Nil) =>
30+
implicit val ctx: Context = arg.ctx
31+
Some(Id(id))
2632
case _ => None
2733
}
2834

29-
private case class Impl(tree: untpd.Tree, ctx: Context) extends trees.ImportSelector {
35+
private[tasty] class Impl(val tree: untpd.Tree)(implicit val ctx: Context) extends trees.ImportSelector {
3036
override def toString: String = {
3137
import Toolbox.extractor
3238
this match {

compiler/src/dotty/tools/dotc/tasty/internal/MethodType.scala

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@ object MethodType {
1010

1111
// TODO make sure all extractors are tested
1212

13-
def apply(tpe: Types.MethodType)(implicit ctx: Context): types.MethodType = Impl(tpe, ctx)
14-
15-
def unapplyMethodType(tpe: types.MaybeType): Option[types.MethodType.Data] = tpe match {
16-
case Impl(meth: Types.MethodType, ctx) =>
17-
implicit val ctx_ = ctx
18-
// TODO add companion
19-
Some((meth.paramNames.map(TermName(_)), meth.paramInfos.map(Type(_)), Type(meth.resType)))
20-
case _ => None
13+
def apply(tpe: Types.MethodType)(implicit ctx: Context): types.MethodType = new Impl(tpe)
14+
15+
def unapplyMethodType(arg: Impl): Option[types.MethodType.Data] = {
16+
implicit val ctx: Context = arg.ctx
17+
val meth = arg.meth
18+
Some((meth.paramNames.map(TermName(_)), meth.paramInfos.map(Type(_)), Type(meth.resType)))
2119
}
2220

23-
private case class Impl(meth: Types.MethodType, ctx: Context) extends types.MethodType {
21+
private[tasty] class Impl(val meth: Types.MethodType)(implicit val ctx: Context) extends types.MethodType {
2422

2523
override def isImplicit: Boolean = meth.isImplicitMethod
2624
override def isErased: Boolean = meth.isErasedMethod

compiler/src/dotty/tools/dotc/tasty/internal/PackageDef.scala

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@ object PackageDef {
1010

1111
// TODO make sure all extractors are tested
1212

13-
def apply(tree: tpd.Tree)(implicit ctx: Context): trees.PackageDef = Impl(tree, ctx)
13+
def apply(tree: tpd.PackageDef)(implicit ctx: Context): trees.PackageDef = new Impl(tree)
1414

15-
def unapplyPackageDef(tree: trees.Tree): Option[trees.PackageDef.Data] = tree match {
16-
case Impl(pkgDef@Trees.PackageDef(pkg, body), ctx) =>
17-
val localContext = ctx.withOwner(pkgDef.symbol(ctx))
18-
Some(Term(pkg)(ctx), body.map(TopLevelStatement(_)(localContext)))
19-
case _ => None
15+
def unapplyPackageDef(arg: Impl): Option[trees.PackageDef.Data] = {
16+
implicit val ctx: Context = arg.ctx
17+
val localContext = ctx.withOwner(arg.tree.symbol)
18+
Some(Term(arg.tree.pid), arg.tree.stats.map(TopLevelStatement(_)(localContext)))
2019
}
2120

22-
private case class Impl(tree: tpd.Tree, ctx: Context) extends trees.PackageDef with Positioned {
21+
private[tasty] class Impl(val tree: tpd.PackageDef)(implicit val ctx: Context) extends trees.PackageDef with Positioned {
2322
override def toString: String = {
2423
import Toolbox.extractor
2524
val trees.PackageDef(pkg, body) = this

compiler/src/dotty/tools/dotc/tasty/internal/Pattern.scala

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,48 @@ import scala.tasty.types
1010

1111
object Pattern {
1212

13-
def apply(tree: tpd.Tree)(implicit ctx: Context): trees.Pattern = Impl(tree, ctx)
13+
def apply(tree: tpd.Tree)(implicit ctx: Context): trees.Pattern = new Impl(tree)
1414

15-
def unapplyValue(tree: trees.Tree): Option[trees.Value.Data] = tree match {
16-
case Impl(lit: tpd.Literal, ctx) => Some(Term(lit)(ctx))
17-
case Impl(ident: tpd.Ident, ctx) => Some(Term(ident)(ctx))
18-
case _ => None
15+
def unapplyValue(arg: Impl): Option[trees.Value.Data] = {
16+
implicit val ctx: Context = arg.ctx
17+
arg.tree match {
18+
case lit: tpd.Literal => Some(Term(lit))
19+
case ident: tpd.Ident => Some(Term(ident))
20+
case _ => None
21+
}
1922
}
2023

21-
def unapplyBind(tree: trees.Tree): Option[trees.Bind.Data] = tree match {
22-
case Impl(Trees.Bind(name: Names.TermName, body), ctx) => Some(TermName(name), Pattern(body)(ctx))
24+
def unapplyBind(arg: Impl): Option[trees.Bind.Data] = arg.tree match {
25+
case Trees.Bind(name: Names.TermName, body) =>
26+
implicit val ctx: Context = arg.ctx
27+
Some(TermName(name), Pattern(body))
2328
case _ => None
2429
}
2530

26-
def unapplyUnapply(tree: trees.Tree): Option[trees.Unapply.Data] = tree match {
27-
case Impl(Trees.UnApply(fun, implicits, patterns), ctx) => Some((Term(fun)(ctx), implicits.map(Term(_)(ctx)), patterns.map(Pattern(_)(ctx))))
31+
def unapplyUnapply(arg: Impl): Option[trees.Unapply.Data] = arg.tree match {
32+
case Trees.UnApply(fun, implicits, patterns) =>
33+
implicit val ctx: Context = arg.ctx
34+
Some((Term(fun), implicits.map(Term(_)), patterns.map(Pattern(_))))
2835
case _ => None
2936
}
3037

31-
def unapplyAlternative(tree: trees.Tree): Option[trees.Alternative.Data] = tree match {
32-
case Impl(Trees.Alternative(patterns), ctx) => Some(patterns.map(Pattern(_)(ctx)))
38+
def unapplyAlternative(arg: Impl): Option[trees.Alternative.Data] = arg.tree match {
39+
case Trees.Alternative(patterns) =>
40+
implicit val ctx: Context = arg.ctx
41+
Some(patterns.map(Pattern(_)))
3342
case _ => None
3443
}
3544

36-
def unapplyTypeTest(tree: trees.Tree): Option[trees.TypeTest.Data] = tree match {
37-
case Impl(Trees.Typed(_, tpt), ctx) => Some(TypeTree(tpt)(ctx))
45+
def unapplyTypeTest(arg: Impl): Option[trees.TypeTest.Data] = arg.tree match {
46+
case Trees.Typed(_, tpt) =>
47+
implicit val ctx: Context = arg.ctx
48+
Some(TypeTree(tpt))
3849
case _ => None
3950
}
4051

41-
private case class Impl(tree: tpd.Tree, ctx: Context) extends trees.Pattern with Positioned {
52+
private[tasty] class Impl(val tree: tpd.Tree)(implicit val ctx: Context) extends trees.Pattern with Positioned {
4253

43-
def tpe: types.Type = Type(tree.tpe)(ctx)
54+
def tpe: types.Type = Type(tree.tpe)
4455

4556
override def toString: String = {
4657
import Toolbox.extractor

0 commit comments

Comments
 (0)