From 91a11635ebce7c462364cc8d067d49db2497d3e8 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 13 May 2005 15:29:45 +0000 Subject: a --- sources/scala/tools/nsc/Global.scala | 11 +++-- sources/scala/tools/nsc/ast/TreeCheckers.scala | 21 --------- sources/scala/tools/nsc/typechecker/Contexts.scala | 8 +++- .../scala/tools/nsc/typechecker/TreeCheckers.scala | 54 ++++++++++++++++++++++ sources/scala/tools/nsc/typechecker/Typers.scala | 2 +- 5 files changed, 67 insertions(+), 29 deletions(-) delete mode 100755 sources/scala/tools/nsc/ast/TreeCheckers.scala create mode 100644 sources/scala/tools/nsc/typechecker/TreeCheckers.scala diff --git a/sources/scala/tools/nsc/Global.scala b/sources/scala/tools/nsc/Global.scala index 64eaba7d11..318f34b9b0 100755 --- a/sources/scala/tools/nsc/Global.scala +++ b/sources/scala/tools/nsc/Global.scala @@ -17,7 +17,7 @@ import ast._; import ast.parser._; import typechecker._; -class Global(val settings: Settings, val reporter: Reporter) extends SymbolTable with Trees with TreeCheckers with CompilationUnits { +class Global(val settings: Settings, val reporter: Reporter) extends SymbolTable with Trees with CompilationUnits { // sub-components -------------------------------------------------- @@ -42,6 +42,10 @@ class Global(val settings: Settings, val reporter: Reporter) extends SymbolTable val global: Global.this.type = Global.this } + object checker extends TreeCheckers { + val global: Global.this.type = Global.this + } + val copy = new LazyTreeCopier(); type AttrInfo = Pair[Type, List[Any]]; @@ -189,10 +193,10 @@ class Global(val settings: Settings, val reporter: Reporter) extends SymbolTable val startTime = System.currentTimeMillis(); phase.run; if (settings.print contains phase.name) treePrinter.printAll(); - if (settings.check contains phase.name) checkTrees; informTime(phase.description, startTime); } phase = if (settings.stop contains phase.name) terminalPhase else phase.next; + if (settings.check contains phase.name) checker.checkTrees; } if (settings.Xshowcls.value != "") showDef(newTermName(settings.Xshowcls.value), false); if (settings.Xshowobj.value != "") showDef(newTermName(settings.Xshowobj.value), true); @@ -234,9 +238,6 @@ class Global(val settings: Settings, val reporter: Reporter) extends SymbolTable case ex: IOException => error(ex.getMessage()); } - def checkTrees: unit = - for (val unit <- units) treeChecker.traverse(unit.body); - def showDef(name: Name, module: boolean): unit = { def getSym(name: Name, module: boolean): Symbol = { var i = name.length - 1; diff --git a/sources/scala/tools/nsc/ast/TreeCheckers.scala b/sources/scala/tools/nsc/ast/TreeCheckers.scala deleted file mode 100755 index be6e2a2b4c..0000000000 --- a/sources/scala/tools/nsc/ast/TreeCheckers.scala +++ /dev/null @@ -1,21 +0,0 @@ -/* NSC -- new scala compiler - * Copyright 2005 LAMP/EPFL - * @author Martin Odersky - */ -// $Id$ -package scala.tools.nsc.ast; - -import scala.tools.util.Position; - -abstract class TreeCheckers: Global { - - object treeChecker extends Traverser { - override def traverse(tree: Tree): unit = { - if (tree.pos == Position.NOPOS) - throw new FatalError("tree without position: " + tree) - else if (tree.tpe == null && phase.id >= typeCheckPhase.id) - throw new FatalError("tree without type: " + tree) - } - } -} - diff --git a/sources/scala/tools/nsc/typechecker/Contexts.scala b/sources/scala/tools/nsc/typechecker/Contexts.scala index 4d8edafab6..2ab5457fa9 100755 --- a/sources/scala/tools/nsc/typechecker/Contexts.scala +++ b/sources/scala/tools/nsc/typechecker/Contexts.scala @@ -54,6 +54,7 @@ class Contexts: Analyzer { var reportAmbiguousErrors = false; var reportGeneralErrors = false; + var checking = false; def undetparams = _undetparams; def undetparams_=(ps: List[Symbol]) = { @@ -76,6 +77,7 @@ class Contexts: Analyzer { c.imports = imports; c.reportAmbiguousErrors = this.reportAmbiguousErrors; c.reportGeneralErrors = this.reportGeneralErrors; + c.checking = this.checking; c.outer = this; c } @@ -110,8 +112,10 @@ class Contexts: Analyzer { } def error(pos: int, msg: String): unit = - if (reportGeneralErrors) unit.error(pos, msg) - else throw new TypeError(msg); + if (reportGeneralErrors) + unit.error(pos, if (checking) "**** ERROR DURING INTERNAL CHECKING ****\n" + msg else msg) + else + throw new TypeError(msg); def ambiguousError(pos: int, pre: Type, sym1: Symbol, sym2: Symbol, rest: String): unit = { val msg = diff --git a/sources/scala/tools/nsc/typechecker/TreeCheckers.scala b/sources/scala/tools/nsc/typechecker/TreeCheckers.scala new file mode 100644 index 0000000000..fe56b35ea3 --- /dev/null +++ b/sources/scala/tools/nsc/typechecker/TreeCheckers.scala @@ -0,0 +1,54 @@ +/* NSC -- new scala compiler + * Copyright 2005 LAMP/EPFL + * @author Martin Odersky + */ +// $Id$ +package scala.tools.nsc.typechecker; + +import scala.tools.util.Position; + +abstract class TreeCheckers extends Analyzer { + + import global._; + + def checkTrees: unit = { + System.out.println("[consistency check at start of phase " + phase + "]"); + for (val unit <- units) check(unit); + } + + def check(unit: CompilationUnit): unit = { + val curPrompt = reporter.prompt(); + reporter.prompt(true); + val context = startContext.make(unit); + context.checking = true; + new TreeChecker(context).transformExpr(unit.body); + reporter.prompt(curPrompt); + } + + override def newTyper(context: Context): Typer = new TreeChecker(context); + + class TreeChecker(context0: Context) extends Typer(context0) { + + import infer._; + + override def transform(tree: Tree, mode: int, pt: Type): Tree = { + System.out.println("**** checking " + tree);//debug + if (tree.pos == Position.NOPOS) + error(tree.pos, "tree without position: " + tree) + else if (phase.id > typeCheckPhase.id) + if (tree.tpe == null) + error(tree.pos, "tree without type: " + tree); + else { + val oldtpe = tree.tpe; + tree.tpe = null; + val newtree = super.transform(tree, mode, pt); + if (newtree ne tree) + error(tree.pos, "trees differ\n old: " + tree + "\n new: " + newtree); + else if (!(oldtpe =:= newtree.tpe)) + error(tree.pos, "types differ\n old: " + oldtpe + "\n new: " + newtree.tpe); + tree.tpe = oldtpe + } + tree + } + } +} diff --git a/sources/scala/tools/nsc/typechecker/Typers.scala b/sources/scala/tools/nsc/typechecker/Typers.scala index 5cd6d9b473..685e6837f2 100755 --- a/sources/scala/tools/nsc/typechecker/Typers.scala +++ b/sources/scala/tools/nsc/typechecker/Typers.scala @@ -27,7 +27,7 @@ abstract class Typers: Analyzer { unit.body = newTyper(startContext.make(unit)).transformExpr(unit.body) } - def newTyper(context: Context) = new Typer(context); + def newTyper(context: Context): Typer = new Typer(context); class Typer(context0: Context) { import context0.unit; -- cgit v1.2.3