Skip to content
Permalink
Browse files

Cleanups in Treecheckers.

  • Loading branch information
paulp committed May 23, 2012
1 parent 2ed32de commit 0d7952f90fc11f4dc055a2658b16b7183f1d6683
Showing with 33 additions and 34 deletions.
  1. +33 −34 src/compiler/scala/tools/nsc/typechecker/TreeCheckers.scala
@@ -14,22 +14,7 @@ import util.returning
abstract class TreeCheckers extends Analyzer {
import global._

private val everything = ListBuffer[(Phase, Map[Tree, (Symbol, Type)])]()
private val currentTrees = mutable.Map[Tree, (Symbol, Type)]()
private val tpeOfTree = mutable.HashMap[Tree, Type]()

if (settings.debug.value) {
sys addShutdownHook {
for ((ph, map) <- everything.toList) {
println("\n>>>> " + ph + "\n")
for ((tree, (sym, tpe)) <- map.toList.sortBy(_._1.summaryString)) {
println("%20s %20s %s".format(sym, tpe, ("" + tree) take 50))
}
}
}
}

private def classstr(x: AnyRef) = (x.getClass.getName split """\\.|\\$""").last
private def classstr(x: AnyRef) = x.getClass.getName split """\\.|\\$""" last;
private def typestr(x: Type) = " (tpe = " + x + ")"
private def treestr(t: Tree) = t + " [" + classstr(t) + "]" + typestr(t.tpe)
private def ownerstr(s: Symbol) = "'" + s + "'" + s.locationString
@@ -50,14 +35,13 @@ abstract class TreeCheckers extends Analyzer {
object SymbolTracker extends Traverser {
type PhaseMap = mutable.HashMap[Symbol, List[Tree]]

val defSyms = mutable.HashMap[Symbol, List[DefTree]]() withDefaultValue Nil
val newSyms = mutable.HashSet[Symbol]()
val maps = ListBuffer[(Phase, PhaseMap)]()
val movedMsgs = ListBuffer[String]()

def prev = maps.init.last._2
def latest = maps.last._2
def sortedNewSyms = newSyms.toList.distinct sortBy (_.name)
val defSyms = mutable.HashMap[Symbol, List[DefTree]]()
val newSyms = mutable.HashSet[Symbol]()
val movedMsgs = new ListBuffer[String]
def sortedNewSyms = newSyms.toList.distinct sortBy (_.name.toString)

def inPrev(sym: Symbol) = {
(maps.size >= 2) && (prev contains sym)
@@ -108,28 +92,27 @@ abstract class TreeCheckers extends Analyzer {
if (maps.isEmpty || maps.last._1 != ph)
maps += ((ph, new PhaseMap))

currentTrees.clear()
traverse(unit.body)
everything += ((ph, currentTrees.toMap))

reportChanges()
}
override def traverse(tree: Tree): Unit = {
val sym = tree.symbol
currentTrees(tree) = ((sym, tree.tpe))

if (sym != null && sym != NoSymbol) {
record(sym, tree)
tree match {
case x: DefTree => defSyms(sym) :+= x
case _ => ()
case x: DefTree =>
if (defSyms contains sym) defSyms(sym) = defSyms(sym) :+ x
else defSyms(sym) = List(x)
case _ => ()
}
}

super.traverse(tree)
}
}

lazy val tpeOfTree = mutable.HashMap[Tree, Type]()

def posstr(p: Position) =
try p.source.path + ":" + p.line
catch { case _: UnsupportedOperationException => p.toString }
@@ -144,9 +127,20 @@ abstract class TreeCheckers extends Analyzer {
def assertFn(cond: Boolean, msg: => Any) =
if (!cond) errorFn(msg)

private def wrap[T](msg: => Any)(body: => Unit) {
try body
catch { case x =>
Console.println("Caught " + x)
Console.println(msg)
x.printStackTrace
}
}

def checkTrees() {
informFn("[consistency check at the beginning of phase " + phase + "]")
currentRun.units foreach check
if (settings.verbose.value)
Console.println("[consistency check at the beginning of phase " + phase + "]")

currentRun.units foreach (x => wrap(x)(check(x)))
}

def printingTypings[T](body: => T): T = {
@@ -168,7 +162,7 @@ abstract class TreeCheckers extends Analyzer {
informProgress("checking "+unit)
val context = rootContext(unit)
context.checking = true
tpeOfTree.clear()
tpeOfTree.clear
SymbolTracker.check(phase, unit)
val checker = new TreeChecker(context)
runWithUnit(unit) {
@@ -215,11 +209,11 @@ abstract class TreeCheckers extends Analyzer {
tree.tpe = null
saved
})
super.typed(tree, mode, pt) match {
wrap(tree)(super.typed(tree, mode, pt) match {
case _: Literal => ()
case x if x ne tree => treesDiffer(tree, x)
case _ => ()
}
})
case _ => ()
}

@@ -286,7 +280,12 @@ abstract class TreeCheckers extends Analyzer {
if (sym.owner != currentOwner) {
val expected = currentOwner.ownerChain find (x => cond(x)) getOrElse fail("DefTree can't find owner: ")
if (sym.owner != expected)
fail("Expected owner %s (out of %s), found %s: ".format(expected, currentOwner.ownerChain, sym.owner))
fail("""|
| currentOwner chain: %s
| symbol chain: %s""".stripMargin.format(
currentOwner.ownerChain take 3 mkString " -> ",
sym.ownerChain mkString " -> ")
)
}
}
}

0 comments on commit 0d7952f

Please sign in to comment.
You can’t perform that action at this time.