Skip to content

Commit

Permalink
Trying to move some of the work to the previous phase.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjrd committed Apr 27, 2020
1 parent 1559c98 commit 4370981
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class Compiler {
new ArrayApply, // Optimize `scala.Array.apply([....])` and `scala.Array.apply(..., [....])` into `[...]`
new ElimPolyFunction, // Rewrite PolyFunction subclasses to FunctionN subclasses
new TailRec, // Rewrite tail recursion to loops
new PrepareMixin,
new Mixin, // Expand trait fields and trait initializers
new LazyVals, // Expand lazy vals
new Memoize, // Add private fields to getters and setters
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/transform/Constructors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ class Constructors extends MiniPhase with IdentityDenotTransformer { thisPhase =
val setter =
if (sym.setter.exists) sym.setter
else sym.accessorNamed(Mixin.traitSetterName(sym.asTerm))
assert(setter.exists, sym.fullName)
constrStats += Apply(ref(setter), intoConstr(stat.rhs, sym).withSpan(stat.span) :: Nil)
clsStats += cpy.DefDef(stat)(rhs = EmptyTree)
case DefDef(nme.CONSTRUCTOR, _, ((outerParam @ ValDef(nme.OUTER, _, _)) :: _) :: Nil, _, _) =>
Expand Down
15 changes: 11 additions & 4 deletions compiler/src/dotty/tools/dotc/transform/Mixin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,18 @@ class Mixin extends MiniPhase with SymTransformer { thisPhase =>
override def relaxedTypingInGroup: Boolean = true
// Because it changes number of parameters in trait initializers

override def runsAfter: Set[String] = Set(Erasure.name)
override def runsAfter: Set[String] = Set(Erasure.name, PrepareMixin.name)

override def changesMembers: Boolean = true // the phase adds implementions of mixin accessors

override def transformSym(sym: SymDenotation)(implicit ctx: Context): SymDenotation =
if (sym.is(Accessor, butNot = Deferred) && sym.owner.is(Trait)) {
/*if (sym.is(Accessor, butNot = Deferred) && sym.owner.is(Trait)) {
val sym1 =
if (sym.is(Lazy)) sym
else sym.copySymDenotation(initFlags = sym.flags &~ ParamAccessor | Deferred)
sym1.ensureNotPrivate
}
else if (sym.isConstructor && sym.owner.is(Trait))
else*/ if (sym.isConstructor && sym.owner.is(Trait))
sym.copySymDenotation(
name = nme.TRAIT_CONSTRUCTOR,
info = MethodType(Nil, sym.info.resultType))
Expand All @@ -139,8 +139,10 @@ class Mixin extends MiniPhase with SymTransformer { thisPhase =>
val decls1 = classInfo.decls.cloneScope
var modified: Boolean = false
for (getter <- classInfo.decls)
//System.err.println(s"trait getter ${getter.fullName}")
if needsTraitSetter(getter) then
val setter = makeTraitSetter(getter.asTerm)
//System.err.println(s"create trait setter ${setter.fullName}")
decls1.enter(setter)
modified = true
if modified then
Expand Down Expand Up @@ -168,9 +170,14 @@ class Mixin extends MiniPhase with SymTransformer { thisPhase =>
}.asTerm

private def wasOneOf(sym: Symbol, flags: FlagSet)(implicit ctx: Context): Boolean =
ctx.atPhase(thisPhase) { sym.isOneOf(flags) }
ctx.atPhase(thisPhase.prev) { sym.isOneOf(flags) }

private def needsTraitSetter(sym: Symbol)(implicit ctx: Context): Boolean =
/*System.err.println(" " + sym.isGetter)
if sym.isGetter then
System.err.println(" " + !wasOneOf(sym, DeferredOrLazy | ParamAccessor))
System.err.println(" " + !sym.setter.exists)
System.err.println(" " + !sym.info.resultType.isInstanceOf[ConstantType])*/
sym.isGetter && !wasOneOf(sym, DeferredOrLazy | ParamAccessor) && !sym.setter.exists
&& !sym.info.resultType.isInstanceOf[ConstantType]

Expand Down
43 changes: 43 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/PrepareMixin.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dotty.tools.dotc
package transform

import core._
import MegaPhase._
import Contexts.Context
import Flags._
import SymUtils._
import Symbols._
import SymDenotations._
import Types._
import Decorators._
import DenotTransformers._
import StdNames._
import Names._
import NameKinds._
import NameOps._
import ast.Trees._
import collection.mutable

object PrepareMixin {
val name: String = "prepMixin"
}

class PrepareMixin extends MiniPhase with SymTransformer { thisPhase =>
import ast.tpd._

override def phaseName: String = PrepareMixin.name

override def runsAfter: Set[String] = Set(Erasure.name)

override def changesMembers: Boolean = true // the phase adds implementions of mixin accessors

override def transformSym(sym: SymDenotation)(implicit ctx: Context): SymDenotation =
if (sym.is(Accessor, butNot = Deferred) && sym.owner.is(Trait))
val sym1 =
if (sym.is(Lazy)) sym
else sym.copySymDenotation(initFlags = sym.flags &~ ParamAccessor | Deferred)
sym1.ensureNotPrivate
else
sym
end transformSym
}

0 comments on commit 4370981

Please sign in to comment.