diff options
author | Paul Phillips <paulp@improving.org> | 2009-10-12 01:10:50 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-10-12 01:10:50 +0000 |
commit | 89a03016abe7e23295fa9cef2f8217cc41f2c194 (patch) | |
tree | 098c1d302c83abaea67d93ceaaece616cc79345e /src/compiler | |
parent | 31c726aa43bcc97c154313e2754e5bbe5922fa41 (diff) | |
download | scala-89a03016abe7e23295fa9cef2f8217cc41f2c194.tar.gz scala-89a03016abe7e23295fa9cef2f8217cc41f2c194.tar.bz2 scala-89a03016abe7e23295fa9cef2f8217cc41f2c194.zip |
Renamed definedVars to deepBoundVariables.
tracing code for the long haul.
Diffstat (limited to 'src/compiler')
7 files changed, 68 insertions, 45 deletions
diff --git a/src/compiler/scala/tools/nsc/matching/MatchSupport.scala b/src/compiler/scala/tools/nsc/matching/MatchSupport.scala index a768f675d5..3ed106cb40 100644 --- a/src/compiler/scala/tools/nsc/matching/MatchSupport.scala +++ b/src/compiler/scala/tools/nsc/matching/MatchSupport.scala @@ -59,7 +59,6 @@ trait MatchSupport extends ast.TreeDSL case WILD() => "_" case Literal(Constant(x)) => "LIT(%s)".format(x) case Apply(fn, args) => "%s(%s)".format(treeToString(fn), args map treeToString mkString ",") - case x: TypeTree => "TT(%s)".format(symbolToString(x.symbol)) case Typed(expr, tpt) => "%s: %s".format(treeToString(expr), treeToString(tpt)) case x => x.toString + " (" + x.getClass + ")" } @@ -75,31 +74,44 @@ trait MatchSupport extends ast.TreeDSL // pretty print for debugging def pp(x: Any): String = pp(x, false) def pp(x: Any, newlines: Boolean): String = { + val stripStrings = List("""java\.lang\.""", """\$iw\.""") + def clean(s: String): String = - s . replaceAll("""java\.lang\.""", "") - . replaceAll("""\$iw\.""", "") - - val elems: List[Any] = x match { - case x: String => return x - case xs: List[_] => xs - case x: Tuple2[_,_] => return pp(x._1) + " -> " + pp(x._2) - case x => return pp(x.toString) - } - def pplist(xs: List[Any]): String = { - val xs2 = xs map pp + stripStrings.foldLeft(s)((s, x) => s.replaceAll(x, "")) + + def pplist(xs: List[Any]): String = + if (newlines) (xs map (" " + _ + "\n")).mkString("\n", "", "") + else xs.mkString("(", ", ", ")") + + pp(x match { + case s: String => return clean(s) + case x: Tree => treeToCompactString(x) + case xs: List[_] => pplist(xs map pp) + case x: Tuple2[_,_] => "%s -> %s".format(pp(x._1), pp(x._2)) + case x => x.toString + }) + } - if (newlines) (xs2 map (" " + _ + "\n")).mkString("\n", "", "") - else xs2.mkString("(", ", ", ")") - } + object compactTreePrinter extends CompactTreePrinter - clean(pplist(elems)) + def treeToCompactString(t: Tree): String = { + val buffer = new StringWriter() + val printer = compactTreePrinter.create(new PrintWriter(buffer)) + printer.print(t) + printer.flush() + buffer.toString } def ifDebug(body: => Unit): Unit = { if (settings.debug.value) body } def DBG(msg: => String): Unit = { ifDebug(println(msg)) } // @elidable(elidable.FINE) - def TRACE(f: String, xs: Any*): Unit = { if (trace) println(pp(if (xs.isEmpty) f else f.format(xs : _*))) } + def TRACE(f: String, xs: Any*): Unit = { + if (trace) { + val msg = if (xs.isEmpty) f else f.format(xs map pp: _*) + println(msg) + } + } def tracing2[T](x: T)(category: String, xs: String*) = { val preamble = "[" + """%10s""".format(category) + "] " @@ -110,11 +122,14 @@ trait MatchSupport extends ast.TreeDSL } def tracing[T](s: String, x: T): T = { - TRACE("[" + """%10s""".format(s) + "] " + x.toString) + val format = "[" + """%10s""".format(s) + "] %s" + TRACE(format, x) x } - def traceCategory(cat: String, f: String, xs: Any*) = - TRACE("[" + """%10s""".format(cat) + "] " + f, xs: _*) + def traceCategory(cat: String, f: String, xs: Any*) = { + val format = "[" + """%10s""".format(cat) + "] " + f + TRACE(format, xs: _*) + } def indent(s: Any) = s.toString() split "\n" map (" " + _) mkString "\n" def indentAll(s: Seq[Any]) = s map (" " + _.toString() + "\n") mkString diff --git a/src/compiler/scala/tools/nsc/matching/MatrixAdditions.scala b/src/compiler/scala/tools/nsc/matching/MatrixAdditions.scala index c27c713a47..e542e1ab11 100644 --- a/src/compiler/scala/tools/nsc/matching/MatrixAdditions.scala +++ b/src/compiler/scala/tools/nsc/matching/MatrixAdditions.scala @@ -28,9 +28,10 @@ trait MatrixAdditions extends ast.TreeDSL def squeezedBlockPVs(pvs: List[PatternVar], exp: Tree): Tree = squeezedBlock(pvs map (_.valDef), exp) - def squeezedBlock(vds: List[Tree], exp: Tree): Tree = + def squeezedBlock(vds: List[Tree], exp: Tree): Tree = tracing("squeezed", if (settings_squeeze) Block(Nil, squeezedBlock1(vds, exp)) else Block(vds, exp) + ) private def squeezedBlock1(vds: List[Tree], exp: Tree): Tree = { class RefTraverser(sym: Symbol) extends Traverser { diff --git a/src/compiler/scala/tools/nsc/matching/ParallelMatching.scala b/src/compiler/scala/tools/nsc/matching/ParallelMatching.scala index c7c40a69ab..44c5b4dbb5 100644 --- a/src/compiler/scala/tools/nsc/matching/ParallelMatching.scala +++ b/src/compiler/scala/tools/nsc/matching/ParallelMatching.scala @@ -315,7 +315,7 @@ trait ParallelMatching extends ast.TreeDSL // varMap is a list from each literal to a list of the defined vars. lazy val (literalMap, varMap) = { val tags = literals map (_.intValue) - val varMap = tags zip (literals map (_.definedVars)) + val varMap = tags zip (literals map (_.deepBoundVariables)) val litMap = tags.zipWithIndex.reverse.foldLeft(IntMap.empty[List[Int]]) { // we reverse before the fold so the list can be built with :: @@ -649,8 +649,9 @@ trait ParallelMatching extends ast.TreeDSL /*** States, Rows, Etc. ***/ case class Row(pats: List[Pattern], subst: Bindings, guard: Guard, bx: Int) { + private def substpp = if (subst.get().isEmpty) "" else "(free = %s)".format(pp(subst.get())) if (pats exists (p => !p.isDefault)) - traceCategory("Row", "%s", pp(pats)) + traceCategory("Row", "%s%s", pats, substpp) /** Extracts the 'i'th pattern. */ def extractColumn(i: Int) = { @@ -668,8 +669,8 @@ trait ParallelMatching extends ast.TreeDSL def insert(hs: List[Pattern]) = copy(pats = hs ::: pats) // prepends supplied pattern def rebind(b: Bindings) = copy(subst = b) // substitutes for bindings - def insert2(hs: List[Pattern], vs: Iterable[Symbol], tvar: Symbol) = // prepends and prepends - copy(pats = hs ::: pats, subst = subst.add(vs, tvar)) + def insert2(hs: List[Pattern], vs: Iterable[Symbol], tvar: Symbol) = + tracing("insert2", copy(pats = hs ::: pats, subst = subst.add(vs, tvar))) // returns this rows with alternatives expanded def expandAlternatives(classifyPat: (Pattern, Int) => Pattern): List[Row] = { @@ -851,7 +852,7 @@ trait ParallelMatching extends ast.TreeDSL case WILD() => emptyTrees(roots.length) }) - row -> FinalState(index, body, pattern.definedVars) + row -> FinalState(index, body, pattern.deepBoundVariables) }) ) diff --git a/src/compiler/scala/tools/nsc/matching/PatternBindings.scala b/src/compiler/scala/tools/nsc/matching/PatternBindings.scala index 563f9e64a2..7132b5d883 100644 --- a/src/compiler/scala/tools/nsc/matching/PatternBindings.scala +++ b/src/compiler/scala/tools/nsc/matching/PatternBindings.scala @@ -51,10 +51,21 @@ trait PatternBindings extends ast.TreeDSL def subpatternsForVars: List[Pattern] = Nil // This is what calls subpatternsForVars. - def definedVars: List[Symbol] = - (boundVariables ::: (subpatternsForVars flatMap (_.definedVars))).reverse // XXX reverse? + // XXX reverse? + def deepBoundVariables: List[Symbol] = deepstrip(boundTree) + // (boundVariables ::: otherBoundVariables).reverse - lazy val boundVariables = strip(boundTree) + private def shallowBoundVariables = strip(boundTree) + private def otherBoundVariables = subpatternsForVars flatMap (_.deepBoundVariables) + lazy val boundVariables = { + val res = shallowBoundVariables + val deep = deepBoundVariables + + if (res.size != deep.size) + TRACE("deep variable list %s is larger than bound %s", deep, res) + + res + } // XXX only a var for short-term experimentation. private var _boundTree: Bind = null @@ -69,8 +80,8 @@ trait PatternBindings extends ast.TreeDSL // This takes the given tree and creates a new pattern // using the same bindings. def rebindTo(t: Tree): Pattern = { - if (boundVariables.size < definedVars.size) - TRACE("In %s, boundVariables = %s but definedVars = %s", this, boundVariables, definedVars) + if (boundVariables.size < deepBoundVariables.size) + TRACE("ALERT: rebinding %s is losing %s", this, otherBoundVariables) Pattern(wrapBindings(boundVariables, t)) } @@ -105,6 +116,8 @@ trait PatternBindings extends ast.TreeDSL case b @ Bind(_, pat) => b.symbol :: strip(pat) case _ => Nil } + private def deepstrip(t: Tree): List[Symbol] = + t filter { case _: Bind => true ; case _ => false } map (_.symbol) } case class Binding(pvar: Symbol, tvar: Symbol) { diff --git a/src/compiler/scala/tools/nsc/matching/Patterns.scala b/src/compiler/scala/tools/nsc/matching/Patterns.scala index 11a2745728..54733e2eea 100644 --- a/src/compiler/scala/tools/nsc/matching/Patterns.scala +++ b/src/compiler/scala/tools/nsc/matching/Patterns.scala @@ -165,7 +165,7 @@ trait Patterns extends ast.TreeDSL { val listRef = typeRef(pre, ListClass, List(tpe)) def fold(x: Tree, xs: Tree) = unbind(x) match { - case _: Star => Pattern(x) rebindTo WILD(x.tpe) boundTree // this is using boundVariables instead of definedVars + case _: Star => Pattern(x) rebindTo WILD(x.tpe) boundTree // this is using boundVariables instead of deepBoundVariables case _ => val dummyMethod = new TermSymbol(NoSymbol, NoPosition, "matching$dummy") val consType = MethodType(dummyMethod newSyntheticValueParams List(tpe, listRef), consRef) @@ -317,6 +317,7 @@ trait Patterns extends ast.TreeDSL { def unapply(other: Any): Option[(Tree, List[Symbol])] = other match { case x: Tree => unapply(Pattern(x)) case x: Pattern => Some((x.tree, x.boundVariables)) + // case x: Pattern => Some((x.tree, x.deepBoundVariables)) case _ => None } } diff --git a/src/compiler/scala/tools/nsc/matching/TransMatcher.scala b/src/compiler/scala/tools/nsc/matching/TransMatcher.scala index 88db07518d..4244b13f6a 100644 --- a/src/compiler/scala/tools/nsc/matching/TransMatcher.scala +++ b/src/compiler/scala/tools/nsc/matching/TransMatcher.scala @@ -25,7 +25,7 @@ trait TransMatcher extends ast.TreeDSL { import analyzer.Typer import definitions._ import CODE._ - import Debug.tracing + import Debug.{ TRACE, tracing } // cunit is set to the current unit in ExplicitOuter's transformUnit, // and nulled out afterward to avoid leaking. @@ -84,17 +84,9 @@ trait TransMatcher extends ast.TreeDSL { // redundancy check matrix.targets filter (_.isNotReached) foreach (cs => cunit.error(cs.body.pos, "unreachable code")) // optimize performs squeezing and resets any remaining TRANS_FLAGs - matrix optimize dfatree - } - - object compactTreePrinters extends CompactTreePrinter - - private def toCompactString(t: Tree): String = { - val buffer = new StringWriter() - val printer = compactTreePrinters.create(new PrintWriter(buffer)) - printer.print(t) - printer.flush() - buffer.toString + val res = matrix optimize dfatree + TRACE("handlePattern(%s, ...) = %s", selector, res) + res } } diff --git a/src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala b/src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala index 9aeebb2360..075f7069c8 100644 --- a/src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala +++ b/src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala @@ -361,7 +361,7 @@ abstract class ExplicitOuter extends InfoTransform val gdcall = if (guard == EmptyTree) EmptyTree else { - val vs = Pattern(p).definedVars + val vs = Pattern(p).deepBoundVariables val guardDef = makeGuardDef(vs, guard) nguard += transform(guardDef) // building up list of guards |