Skip to content

Commit 3a158c0

Browse files
committed
Roll SyntheticMethods into PostTyper
1 parent 7e60221 commit 3a158c0

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

src/dotty/tools/dotc/Compiler.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Contexts._
66
import Periods._
77
import Symbols._
88
import Scopes._
9-
import typer.{FrontEnd, Typer, Mode, ImportInfo, RefChecks, InstChecks}
9+
import typer.{FrontEnd, Typer, Mode, ImportInfo, RefChecks}
1010
import reporting.ConsoleReporter
1111
import dotty.tools.dotc.core.Phases.Phase
1212
import dotty.tools.dotc.transform._
@@ -39,9 +39,8 @@ class Compiler {
3939
List(
4040
List(new FrontEnd),
4141
List(new PostTyper),
42-
List(new FirstTransform,
43-
new SyntheticMethods),
44-
List(new Pickler), // Pickler needs to come last in a group since it should not pickle trees generated later
42+
List(new FirstTransform),
43+
List(new Pickler),
4544
List(new RefChecks,
4645
new ElimRepeated,
4746
new NormalizeFlags,

src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import Symbols._, TypeUtils._
2323
* field (corresponding = super class field is initialized with subclass field)
2424
* (@see ForwardParamAccessors)
2525
*
26-
* (3) Check that `New` nodes can be instantiated, and that annotations are valid
26+
* (3) Add synthetic methods (@see SyntheticMethods)
27+
*
28+
* (4) Check that `New` nodes can be instantiated, and that annotations are valid
2729
*
2830
* The reason for making this a macro transform is that some functions (in particular
2931
* super and protected accessors and instantiation checks) are naturally top-down and
@@ -46,7 +48,8 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisTran
4648

4749
val superAcc = new SuperAccessors(thisTransformer)
4850
val paramFwd = new ParamForwarding(thisTransformer)
49-
51+
val synthMth = new SyntheticMethods(thisTransformer)
52+
5053
/** Check that `tp` refers to a nonAbstract class
5154
* and that the instance conforms to the self type of the created class.
5255
*/
@@ -99,9 +102,10 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisTran
99102
val saved = parentNews
100103
parentNews ++= impl.parents.flatMap(newPart)
101104
try
102-
paramFwd.forwardParamAccessors(
103-
superAcc.wrapTemplate(impl)(
104-
super.transform(_).asInstanceOf[Template]))
105+
synthMth.addSyntheticMethods(
106+
paramFwd.forwardParamAccessors(
107+
superAcc.wrapTemplate(impl)(
108+
super.transform(_).asInstanceOf[Template])))
105109
finally parentNews = saved
106110
case tree @ TypeApply(sel: Select, args) =>
107111
val args1 = transform(args)

src/dotty/tools/dotc/transform/SyntheticMethods.scala

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,20 @@ import scala.language.postfixOps
3131
* def equals(other: Any): Boolean
3232
* def hashCode(): Int
3333
*/
34-
class SyntheticMethods extends MiniPhaseTransform with IdentityDenotTransformer { thisTransformer =>
34+
class SyntheticMethods(thisTransformer: DenotTransformer) {
3535
import ast.tpd._
3636

37-
override def phaseName = "synthetics"
38-
39-
private var valueSymbols: List[Symbol] = _
40-
private var caseSymbols: List[Symbol] = _
41-
42-
override def prepareForUnit(tree: Tree)(implicit ctx: Context) = {
43-
valueSymbols = List(defn.Any_hashCode, defn.Any_equals)
44-
caseSymbols = valueSymbols ++ List(defn.Any_toString, defn.Product_canEqual, defn.Product_productArity)
45-
this
46-
}
37+
private var myValueSymbols: List[Symbol] = Nil
38+
private var myCaseSymbols: List[Symbol] = Nil
39+
40+
private def initSymbols(implicit ctx: Context) =
41+
if (myValueSymbols.isEmpty) {
42+
myValueSymbols = List(defn.Any_hashCode, defn.Any_equals)
43+
myCaseSymbols = myValueSymbols ++ List(defn.Any_toString, defn.Product_canEqual, defn.Product_productArity)
44+
}
45+
46+
def valueSymbols(implicit ctx: Context) = { initSymbols; myValueSymbols }
47+
def caseSymbols(implicit ctx: Context) = { initSymbols; myCaseSymbols }
4748

4849
/** The synthetic methods of the case or value class `clazz`.
4950
*/
@@ -185,10 +186,9 @@ class SyntheticMethods extends MiniPhaseTransform with IdentityDenotTransformer
185186
symbolsToSynthesize flatMap syntheticDefIfMissing
186187
}
187188

188-
override def transformTemplate(impl: Template)(implicit ctx: Context, info: TransformerInfo) =
189+
def addSyntheticMethods(impl: Template)(implicit ctx: Context) =
189190
if (ctx.owner.is(Case) || isDerivedValueClass(ctx.owner))
190-
cpy.Template(impl)(
191-
body = impl.body ++ syntheticMethods(ctx.owner.asClass))
191+
cpy.Template(impl)(body = impl.body ++ syntheticMethods(ctx.owner.asClass))
192192
else
193193
impl
194194
}

0 commit comments

Comments
 (0)