Skip to content

Commit 178402e

Browse files
committed
Extract inner classes from FromTasty
1 parent 6889d44 commit 178402e

File tree

5 files changed

+113
-94
lines changed

5 files changed

+113
-94
lines changed

compiler/src/dotty/tools/dotc/FromTasty.scala

Lines changed: 3 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,10 @@
22
* Copyright 2005-2015 LAMP/EPFL
33
* @author Martin Odersky
44
*/
5-
package dotty.tools
6-
package dotc
5+
package dotty.tools.dotc
76

8-
import core._
9-
import Contexts._
10-
import Symbols._
11-
import SymDenotations._
12-
import typer.FrontEnd
13-
import Phases.Phase
14-
import util._
15-
import Decorators._
16-
import dotty.tools.dotc.ast.tpd
17-
import dotty.tools.dotc.core._
18-
import dotty.tools.dotc.core.Names._
19-
import dotty.tools.dotc.core.NameOps._
20-
import dotty.tools.dotc.transform.Pickler
7+
import dotty.tools.dotc.core.Contexts._
8+
import dotty.tools.dotc.fromtasty.TASTYCompiler
219

2210
/** Compiler for TASTY files.
2311
* Usage:
@@ -32,83 +20,4 @@ import dotty.tools.dotc.transform.Pickler
3220
*/
3321
object FromTasty extends Driver {
3422
override def newCompiler(implicit ctx: Context): Compiler = new TASTYCompiler
35-
36-
class TASTYCompiler extends Compiler {
37-
38-
override def phases: List[List[Phase]] = {
39-
val backendPhases = super.phases.dropWhile {
40-
case List(_: Pickler) => false
41-
case _ => true
42-
}.tail
43-
List(new ReadTastyTreesFromClasses) :: backendPhases
44-
}
45-
46-
override def newRun(implicit ctx: Context): Run = {
47-
reset()
48-
new TASTYRun(this, ctx)
49-
}
50-
}
51-
52-
class TASTYRun(comp: Compiler, ictx: Context) extends Run(comp, ictx) {
53-
override def compile(classNames: List[String]) = {
54-
units = classNames.map(new TASTYCompilationUnit(_))
55-
compileUnits()
56-
}
57-
}
58-
59-
class TASTYCompilationUnit(val className: String) extends CompilationUnit(NoSource) {
60-
override def toString = s"class file $className"
61-
}
62-
63-
class ReadTastyTreesFromClasses extends FrontEnd {
64-
65-
override def isTyper = false
66-
67-
override def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] =
68-
units.flatMap(readTASTY)
69-
70-
def readTASTY(unit: CompilationUnit)(implicit ctx: Context): Option[CompilationUnit] = unit match {
71-
case unit: TASTYCompilationUnit =>
72-
assert(ctx.settings.YretainTrees.value)
73-
val className = unit.className.toTypeName
74-
def compilationUnit(className: TypeName): Option[CompilationUnit] = {
75-
tree(className).flatMap { case (clsd, unpickled) =>
76-
if (unpickled.isEmpty) None
77-
else Some(CompilationUnit.mkCompilationUnit(clsd, unpickled, forceTrees = true))
78-
}
79-
}
80-
// The TASTY section in a/b/C.class may either contain a class a.b.C, an object a.b.C, or both.
81-
// We first try to load the class and fallback to loading the object if the class doesn't exist.
82-
// Note that if both the class and the object are present, then loading the class will also load
83-
// the object, this is why we use orElse here, otherwise we could load the object twice and
84-
// create ambiguities!
85-
compilationUnit(className).orElse(compilationUnit(className.moduleClassName))
86-
}
87-
88-
private def tree(className: TypeName)(implicit ctx: Context): Option[(ClassDenotation, tpd.Tree)] = {
89-
val clsd = ctx.base.staticRef(className)
90-
ctx.base.staticRef(className) match {
91-
case clsd: ClassDenotation =>
92-
def cannotUnpickle(reason: String) =
93-
ctx.error(s"class $className cannot be unpickled because $reason")
94-
def tryToLoad = clsd.infoOrCompleter match {
95-
case info: ClassfileLoader =>
96-
info.load(clsd)
97-
Option(clsd.symbol.asClass.tree).orElse {
98-
cannotUnpickle(s"its class file ${info.classfile} does not have a TASTY attribute")
99-
None
100-
}
101-
102-
case info =>
103-
cannotUnpickle(s"its info of type ${info.getClass} is not a ClassfileLoader")
104-
None
105-
}
106-
Option(clsd.symbol.asClass.tree).orElse(tryToLoad).map(tree => (clsd, tree))
107-
108-
case _ =>
109-
ctx.error(s"class not found: $className")
110-
None
111-
}
112-
}
113-
}
11423
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package dotty.tools
2+
package dotc
3+
package fromtasty
4+
5+
import dotty.tools.dotc.ast.tpd
6+
import dotty.tools.dotc.core.Contexts.Context
7+
import dotty.tools.dotc.core.Decorators._
8+
import dotty.tools.dotc.core.Names._
9+
import dotty.tools.dotc.core.NameOps._
10+
import dotty.tools.dotc.core.SymDenotations.ClassDenotation
11+
import dotty.tools.dotc.core._
12+
import dotty.tools.dotc.typer.FrontEnd
13+
14+
class ReadTastyTreesFromClasses extends FrontEnd {
15+
16+
override def isTyper = false
17+
18+
override def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] =
19+
units.flatMap(readTASTY)
20+
21+
def readTASTY(unit: CompilationUnit)(implicit ctx: Context): Option[CompilationUnit] = unit match {
22+
case unit: TASTYCompilationUnit =>
23+
assert(ctx.settings.YretainTrees.value)
24+
val className = unit.className.toTypeName
25+
def compilationUnit(className: TypeName): Option[CompilationUnit] = {
26+
tree(className).flatMap {
27+
case (clsd, unpickled) =>
28+
if (unpickled.isEmpty) None
29+
else Some(CompilationUnit.mkCompilationUnit(clsd, unpickled, forceTrees = true))
30+
31+
}
32+
}
33+
// The TASTY section in a/b/C.class may either contain a class a.b.C, an object a.b.C, or both.
34+
// We first try to load the class and fallback to loading the object if the class doesn't exist.
35+
// Note that if both the class and the object are present, then loading the class will also load
36+
// the object, this is why we use orElse here, otherwise we could load the object twice and
37+
// create ambiguities!
38+
compilationUnit(className).orElse(compilationUnit(className.moduleClassName))
39+
}
40+
41+
private def tree(className: TypeName)(implicit ctx: Context): Option[(ClassDenotation, tpd.Tree)] = {
42+
val clsd = ctx.base.staticRef(className)
43+
ctx.base.staticRef(className) match {
44+
case clsd: ClassDenotation =>
45+
def cannotUnpickle(reason: String) =
46+
ctx.error(s"class $className cannot be unpickled because $reason")
47+
def tryToLoad = clsd.infoOrCompleter match {
48+
case info: ClassfileLoader =>
49+
info.load(clsd)
50+
Option(clsd.symbol.asClass.tree).orElse {
51+
cannotUnpickle(s"its class file ${info.classfile} does not have a TASTY attribute")
52+
None
53+
}
54+
55+
case info =>
56+
cannotUnpickle(s"its info of type ${info.getClass} is not a ClassfileLoader")
57+
None
58+
}
59+
Option(clsd.symbol.asClass.tree).orElse(tryToLoad).map(tree => (clsd, tree))
60+
61+
case _ =>
62+
ctx.error(s"class not found: $className")
63+
None
64+
}
65+
}
66+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package dotty.tools.dotc.fromtasty
2+
3+
import dotty.tools.dotc.CompilationUnit
4+
import dotty.tools.dotc.util.NoSource
5+
6+
class TASTYCompilationUnit(val className: String) extends CompilationUnit(NoSource) {
7+
override def toString = s"class file $className"
8+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package dotty.tools
2+
package dotc
3+
package fromtasty
4+
5+
import core._
6+
import Contexts._
7+
import Phases.Phase
8+
import dotty.tools.dotc.transform.Pickler
9+
10+
class TASTYCompiler extends Compiler {
11+
12+
override def phases: List[List[Phase]] = {
13+
val backendPhases = super.phases.dropWhile {
14+
case List(_: Pickler) => false
15+
case _ => true
16+
}.tail
17+
List(new ReadTastyTreesFromClasses) :: backendPhases
18+
}
19+
20+
override def newRun(implicit ctx: Context): Run = {
21+
reset()
22+
new TASTYRun(this, ctx)
23+
}
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dotty.tools
2+
package dotc
3+
package fromtasty
4+
5+
import core.Contexts._
6+
7+
class TASTYRun(comp: Compiler, ictx: Context) extends Run(comp, ictx) {
8+
override def compile(classNames: List[String]) = {
9+
units = classNames.map(new TASTYCompilationUnit(_))
10+
compileUnits()
11+
}
12+
}

0 commit comments

Comments
 (0)