Skip to content

Commit 780c2e1

Browse files
committed
WIP Add explicit tasty context
1 parent 78178a9 commit 780c2e1

File tree

87 files changed

+1319
-1006
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+1319
-1006
lines changed

compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ object PickledQuotes {
4141
case value: Class[_] => ref(defn.Predef_classOf).appliedToType(classToType(value))
4242
case value=> Literal(Constant(value))
4343
}
44-
case expr: TreeExpr =>
45-
dotty.tools.dotc.tasty.internal.Term.tree(expr.term)
44+
case expr: TreeExpr[Tree, Context] @unchecked => expr.tree
4645
case expr: FunctionAppliedTo[_, _] =>
4746
functionAppliedTo(quotedExprToTree(expr.f), quotedExprToTree(expr.x))
4847
}
@@ -51,7 +50,7 @@ object PickledQuotes {
5150
def quotedTypeToTree(expr: quoted.Type[_])(implicit ctx: Context): Tree = expr match {
5251
case expr: TastyType[_] => unpickleType(expr)
5352
case expr: TaggedType[_] => classTagToTypeTree(expr.ct)
54-
case expr: TreeType => dotty.tools.dotc.tasty.internal.TypeTree.tree(expr.tpe)
53+
case expr: TreeType[TypeTree, Context] @unchecked => expr.typeTree
5554
}
5655

5756
/** Unpickle the tree contained in the TastyExpr */

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,8 +1141,8 @@ class TreeUnpickler(reader: TastyReader,
11411141
val args = until(end)(readTerm())
11421142
val splice = splices(idx)
11431143
def wrap(arg: Tree) =
1144-
if (arg.isTerm) new TreeExpr(dotty.tools.dotc.tasty.internal.Term(arg))
1145-
else new TreeType(dotty.tools.dotc.tasty.internal.TypeTree(arg))
1144+
if (arg.isTerm) new TreeExpr(arg, ctx)
1145+
else new TreeType(arg, ctx)
11461146
val reifiedArgs = args.map(wrap)
11471147
if (isType) {
11481148
val quotedType =

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package dotty.tools.dotc.quoted
22

33
import dotty.tools.dotc.ast.tpd
44
import dotty.tools.dotc.core.Constants._
5+
import dotty.tools.dotc.core.Contexts.Context
56
import dotty.tools.dotc.printing.RefinedPrinter
67
import dotty.tools.dotc.tasty.internal
8+
import dotty.tools.dotc.tasty.internal.TastyContext
79

810
import scala.quoted.{Expr, Type}
911
import scala.quoted.Exprs.{LiftedExpr, TreeExpr}
@@ -44,14 +46,20 @@ object Toolbox {
4446
case _ => new QuoteDriver().show(expr, showSettings)
4547
}
4648

47-
def toTasty(expr: Expr[T]): trees.Term = expr match {
48-
case expr: TreeExpr => expr.term
49-
case _ => new QuoteDriver().withTree(expr, (tree, ctx) => internal.Term(tree)(ctx), Settings.run())
49+
def toTasty(expr: Expr[T]): (trees.Term, scala.tasty.Context) = {
50+
def box(tree: Tree, ctx: Context) = (internal.Term(tree), new internal.TastyContext(ctx))
51+
expr match {
52+
case expr: TreeExpr[Tree, Context] @unchecked => box(expr.tree, expr.ctx)
53+
case _ => new QuoteDriver().withTree(expr, (tree, ctx) => box(tree, ctx), Settings.run())
54+
}
5055
}
5156

52-
def toTasty(tpe: Type[T]): trees.TypeTree = tpe match {
53-
case expr: TreeType => expr.tpe
54-
case _ => new QuoteDriver().withTypeTree(tpe, (tpt, ctx) => internal.TypeTree(tpt)(ctx), Settings.run())
57+
def toTasty(tpe: Type[T]): (trees.TypeTree, scala.tasty.Context) = {
58+
def box(tree: TypTree, ctx: Context) = (internal.TypeTree(tree), new internal.TastyContext(ctx))
59+
tpe match {
60+
case typeTree: TreeType[TypeTree, Context] @unchecked => box(typeTree.typeTree, typeTree.ctx)
61+
case _ => new QuoteDriver().withTypeTree(tpe, (tpt, ctx) => box(tpt, ctx), Settings.run())
62+
}
5563
}
5664
}
5765

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

Lines changed: 0 additions & 151 deletions
This file was deleted.

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,18 @@ package internal
33

44
import dotty.tools.dotc.core.Contexts.Context
55
import dotty.tools.dotc.core.Annotations.Annotation
6-
import dotty.tools.dotc.core.Decorators.sourcePos
76

87
import scala.tasty.modifiers
98

109
object AnnotationModifier {
1110

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

14-
def unapplyAnnotation(arg: Impl): Option[modifiers.Annotation.Data] = {
15-
implicit val ctx: Context = arg.ctx
13+
def unapplyAnnotation(arg: Impl)(implicit ctx: Context): Option[modifiers.Annotation.Data] = {
1614
Some(Term(arg.annot.tree))
1715
}
1816

19-
private[tasty] class Impl(val annot: Annotation)(implicit val ctx: Context) extends modifiers.Modifier {
20-
21-
def pos: scala.tasty.Position = new Position(sourcePos(annot.tree(ctx).pos)(ctx))
22-
23-
override def toString: String = {
24-
import Toolbox.extractor
25-
val modifiers.Annotation(annot) = this
26-
s"Annotation($annot)"
27-
}
17+
private[tasty] class Impl(val annot: Annotation) extends modifiers.Annotation {
18+
override def toString: String = "Annotation"
2819
}
2920
}

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

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

1010
object CaseDef {
1111

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

14-
def unapplyCaseDef(arg: Impl): Option[trees.CaseDef.Data] = {
15-
implicit val ctx: Context = arg.ctx
14+
def unapplyCaseDef(arg: Impl)(implicit ctx: Context): Option[trees.CaseDef.Data] = {
1615
val Trees.CaseDef(pat, guard, body) = arg.tree
1716
Some(Pattern(pat), if (guard.isEmpty) None else Some(Term(guard)), Term(body))
1817
}
1918

20-
private[tasty] class Impl(val tree: tpd.CaseDef)(implicit val ctx: Context) extends trees.CaseDef with Positioned {
21-
override def toString: String = {
22-
import Toolbox.extractor
23-
val trees.CaseDef(pat, guard, body) = this
24-
s"CaseDef($pat, $guard, $body)"
25-
}
19+
private[tasty] class Impl(val tree: tpd.CaseDef) extends trees.CaseDef with Positioned {
20+
override def toString: String = "CaseDef"
2621
}
2722
}

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import scala.tasty.types
1111

1212
object ClassDef {
1313

14-
def apply(tree: tpd.TypeDef)(implicit ctx: Context): trees.ClassDef = new Impl(tree)
14+
def apply(tree: tpd.TypeDef): trees.ClassDef = new Impl(tree)
1515

1616
def apply(sym: ClassSymbol)(implicit ctx: Context): trees.ClassDef = {
1717
def toTree(sym: ClassSymbol): tpd.TypeDef = {
@@ -28,8 +28,7 @@ object ClassDef {
2828
new Impl(toTree(sym))
2929
}
3030

31-
def unapplyClassDef(arg: Impl): Option[trees.ClassDef.Data] = {
32-
implicit val ctx: Context = arg.ctx
31+
def unapplyClassDef(arg: Impl)(implicit ctx: Context): Option[trees.ClassDef.Data] = {
3332
val Trees.TypeDef(name, impl@Trees.Template(constr, parents, self, _)) = arg.tree
3433
val className = TypeName(name)
3534
val constructor = DefDef(constr)
@@ -40,17 +39,16 @@ object ClassDef {
4039
Some((className, constructor, classParents, selfVal, body, mods))
4140
}
4241

43-
private[tasty] class Impl(val tree: tpd.TypeDef)(implicit val ctx: Context) extends trees.ClassDef with Positioned {
42+
private[tasty] class Impl(val tree: tpd.TypeDef) extends trees.ClassDef with Positioned {
4443

45-
def tpe: types.Type = Type(tree.tpe)(ctx)
44+
def tpe: types.Type = Type(tree.tpe)
4645

47-
def owner: trees.Definition = Definition(tree.symbol.owner)
48-
49-
override def toString: String = {
50-
import Toolbox.extractor
51-
val trees.ClassDef(name, constructor, parents, self, body, mods) = this
52-
s"ClassDef($name, $constructor, $parents, $self, $body, $mods)"
46+
def owner(implicit tctx: scala.tasty.Context): trees.Definition = {
47+
implicit val ctx = tctx.asInstanceOf[TastyContext].ctx
48+
Definition(tree.symbol.owner)
5349
}
50+
51+
override def toString: String = "ClassDef"
5452
}
5553

5654
}

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,6 @@ object Constant {
5353

5454
def value: Any = const.value
5555

56-
override def toString: String = {
57-
import Toolbox.extractor
58-
this match {
59-
case constants.Unit() => "Unit()"
60-
case constants.Null() => "Null()"
61-
case constants.Boolean(value) => s"Boolean($value)"
62-
case constants.Byte(value) => s"Byte($value)"
63-
case constants.Short(value) => s"Short($value)"
64-
case constants.Char(value) => s"Char('$value')"
65-
case constants.Int(value) => s"Int($value)"
66-
case constants.Long(value) => s"Long($value)"
67-
case constants.Float(value) => s"Float($value)"
68-
case constants.Double(value) => s"Double($value)"
69-
case constants.String(value) => s"""String("$value")"""
70-
}
71-
}
56+
override def toString: String = "Constant"
7257
}
7358
}

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,25 @@ import scala.tasty.types
1010

1111
object DefDef {
1212

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

1515
def apply(sym: TermSymbol)(implicit ctx: Context): trees.DefDef = new Impl(tpd.DefDef(sym))
1616

17-
def unapplyDefDef(arg: Impl): Option[trees.DefDef.Data] = {
17+
def unapplyDefDef(arg: Impl)(implicit ctx: Context): Option[trees.DefDef.Data] = {
1818
val ddef = arg.tree
19-
implicit val localContext: Context = arg.ctx.withOwner(ddef.symbol(arg.ctx))
2019
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)))
2120
}
2221

23-
private[tasty] class Impl(val tree: tpd.DefDef)(implicit val ctx: Context) extends trees.DefDef with Positioned {
22+
private[tasty] class Impl(val tree: tpd.DefDef) extends trees.DefDef with Positioned {
2423

25-
def tpe: types.Type = Type(tree.tpe)(ctx)
24+
def tpe: types.Type = Type(tree.tpe)
2625

27-
def owner: trees.Definition = Definition(tree.symbol.owner)
28-
29-
override def toString: String = {
30-
import Toolbox.extractor
31-
val trees.DefDef(name, typeParams, paramss, returnTpt, rhs, mods) = this
32-
s"DefDef($name, $typeParams, $paramss, $returnTpt, $rhs, $mods)"
26+
def owner(implicit tctx: scala.tasty.Context): trees.Definition = {
27+
implicit val ctx = tctx.asInstanceOf[TastyContext].ctx
28+
Definition(tree.symbol.owner)
3329
}
30+
31+
override def toString: String = "DefDef"
3432
}
3533

3634
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ object Definition {
2626
}
2727

2828
private[tasty] object NoDefinition extends trees.Definition {
29-
override def owner: trees.Definition = NoDefinition
29+
def owner(implicit tctx: scala.tasty.Context): trees.Definition = NoDefinition
3030

31-
override def pos: scala.tasty.Position = ???
31+
def pos(implicit ctx: scala.tasty.Context): scala.tasty.Position = ???
3232

3333
override def toString: String = "NoDefinition"
3434
}

0 commit comments

Comments
 (0)