Skip to content

Commit 08f5b6b

Browse files
committed
Fix scala#1209: Skip redundant superclasses\supertraits.
1 parent 6ca5b9d commit 08f5b6b

File tree

6 files changed

+98
-4
lines changed

6 files changed

+98
-4
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package dotty.tools.backend.jvm
2+
3+
import java.io.{File => JFile}
4+
5+
import dotty.tools.dotc.ast.tpd
6+
import dotty.tools.dotc.ast.tpd._
7+
import dotty.tools.dotc.ast.Trees
8+
import dotty.tools.dotc.core.Contexts.Context
9+
import dotty.tools.dotc.core.Decorators._
10+
import dotty.tools.dotc.core.Names.TypeName
11+
import dotty.tools.dotc.core.Symbols._
12+
import dotty.tools.dotc.core._
13+
import dotty.tools.dotc.transform.TreeTransforms.{MiniPhaseTransform, TransformerInfo}
14+
15+
import scala.collection.mutable
16+
import scala.collection.mutable.ArrayBuffer
17+
18+
class CollectSuperCalls extends MiniPhaseTransform {
19+
20+
def phaseName: String = "collectSuperCalls"
21+
22+
override def transformSuper(tree: Super)(implicit ctx: Context, info: TransformerInfo): Tree = {
23+
tree match {
24+
case Trees.Super(qual: This, mix) if mix.nonEmpty =>
25+
registerSuperCall(qual.symbol.asClass.classSymbol, mix)
26+
case _ =>
27+
}
28+
super.transformSuper(tree)
29+
}
30+
31+
private def registerSuperCall(sym: Symbol, calls: TypeName)(implicit ctx: Context) = {
32+
ctx.genBCodePhase match {
33+
case genBCodePhase: GenBCode =>
34+
genBCodePhase.registerSuperCall(sym, calls)
35+
case _ =>
36+
}
37+
}
38+
}

src/dotty/tools/backend/jvm/DottyBackendInterface.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ import NameOps._
3434
import StdNames.nme
3535
import NameOps._
3636
import dotty.tools.dotc.core
37+
import dotty.tools.dotc.core.Names.TypeName
3738

38-
class DottyBackendInterface(outputDirectory: AbstractFile)(implicit ctx: Context) extends BackendInterface{
39+
import scala.annotation.tailrec
40+
41+
class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Map[Symbol, Set[TypeName]])(implicit ctx: Context) extends BackendInterface{
3942
type Symbol = Symbols.Symbol
4043
type Type = Types.Type
4144
type Tree = tpd.Tree
@@ -740,7 +743,12 @@ class DottyBackendInterface(outputDirectory: AbstractFile)(implicit ctx: Context
740743
* All interfaces implemented by a class, except for those inherited through the superclass.
741744
*
742745
*/
743-
def superInterfaces: List[Symbol] = decorateSymbol(sym).directlyInheritedTraits
746+
def superInterfaces: List[Symbol] = {
747+
val directlyInheritedTraits = decorateSymbol(sym).directlyInheritedTraits
748+
val allBaseClasses = directlyInheritedTraits.iterator.flatMap(_.symbol.asClass.baseClasses.drop(1)).toSet
749+
val superCalls = superCallsMap.getOrElse(sym, Set.empty)
750+
directlyInheritedTraits.filter(t => !allBaseClasses(t) || superCalls(t.name))
751+
}
744752

745753
/**
746754
* True for module classes of package level objects. The backend will generate a mirror class for

src/dotty/tools/backend/jvm/GenBCode.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,27 @@ import tpd._
3434
import StdNames._
3535
import scala.reflect.io.{Directory, PlainDirectory, AbstractFile}
3636

37+
import dotty.tools.dotc.core.Names.TypeName
38+
3739
import scala.tools.nsc.backend.jvm.opt.LocalOpt
3840

3941
class GenBCode extends Phase {
4042
def phaseName: String = "genBCode"
4143
private val entryPoints = new mutable.HashSet[Symbol]()
4244
def registerEntryPoint(sym: Symbol) = entryPoints += sym
4345

46+
private val superCallsMap = new mutable.HashMap[Symbol, Set[TypeName]]()
47+
def registerSuperCall(sym: Symbol, calls: TypeName) = {
48+
val old = superCallsMap.getOrElse(sym, Set.empty)
49+
superCallsMap.put(sym, old + calls)
50+
}
51+
4452
def outputDir(implicit ctx: Context): AbstractFile =
4553
new PlainDirectory(new Directory(new JFile(ctx.settings.d.value)))
4654

4755
def run(implicit ctx: Context): Unit = {
48-
new GenBCodePipeline(entryPoints.toList, new DottyBackendInterface(outputDir)(ctx))(ctx).run(ctx.compilationUnit.tpdTree)
56+
new GenBCodePipeline(entryPoints.toList,
57+
new DottyBackendInterface(outputDir, superCallsMap.toMap)(ctx))(ctx).run(ctx.compilationUnit.tpdTree)
4958
entryPoints.clear()
5059
}
5160
}

src/dotty/tools/dotc/Compiler.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import transform.TreeTransforms.{TreeTransform, TreeTransformer}
1515
import core.DenotTransformers.DenotTransformer
1616
import core.Denotations.SingleDenotation
1717

18-
import dotty.tools.backend.jvm.{LabelDefs, GenBCode}
18+
import dotty.tools.backend.jvm.{LabelDefs, GenBCode, CollectSuperCalls}
1919
import dotty.tools.backend.sjs.GenSJSIR
2020

2121
/** The central class of the dotc compiler. The job of a compiler is to create
@@ -94,6 +94,7 @@ class Compiler {
9494
new CollectEntryPoints, // Find classes with main methods
9595
new MoveStatics, // Move static methods to companion classes
9696
new LabelDefs), // Converts calls to labels to jumps
97+
List(new CollectSuperCalls), // TODO
9798
List(new GenSJSIR), // Generate .js code
9899
List(new GenBCode) // Generate JVM bytecode
99100
)

tests/run/redundantParents.check

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
C1 super class: class java.lang.Object
2+
C1 interfaces: List(interface T2)
3+
C2 super class: class C1
4+
C2 interfaces: List(interface T4, interface T5)
5+
C3 super class: class C1
6+
C3 interfaces: List(interface T5)
7+
C4 super class: class C2
8+
C4 interfaces: List()

tests/run/redundantParents.scala

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
trait T1
3+
trait T2 extends T1
4+
trait T3 extends T2
5+
trait T4 extends T3
6+
7+
trait T5
8+
9+
class C1 extends T2
10+
class C2 extends C1 with T4 with T5 with T1 with T2
11+
class C3 extends C1 with T5
12+
class C4 extends C2 with T5
13+
14+
object Test {
15+
def main(args: Array[String]): Unit = {
16+
val c1 = (new C1).getClass
17+
val c2 = (new C2).getClass
18+
val c3 = (new C3).getClass
19+
val c4 = (new C4).getClass
20+
21+
println("C1 super class: " + c1.getSuperclass)
22+
println("C1 interfaces: " + c1.getInterfaces.toList)
23+
println("C2 super class: " + c2.getSuperclass)
24+
println("C2 interfaces: " + c2.getInterfaces.toList)
25+
println("C3 super class: " + c3.getSuperclass)
26+
println("C3 interfaces: " + c3.getInterfaces.toList)
27+
println("C4 super class: " + c4.getSuperclass)
28+
println("C4 interfaces: " + c4.getInterfaces.toList)
29+
}
30+
}

0 commit comments

Comments
 (0)