From 66036d3d4f16e21ce468f357406b0d68fb2b2501 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Wed, 10 Jun 2009 18:00:02 +0000 Subject: Minor cleanups while I evaluate rewriting the p... Minor cleanups while I evaluate rewriting the pattern matcher. --- .../tools/nsc/matching/ParallelMatching.scala | 4 +- .../scala/tools/nsc/matching/TransMatcher.scala | 6 -- src/compiler/scala/tools/nsc/symtab/Scopes.scala | 11 ++- .../scala/tools/nsc/transform/ExplicitOuter.scala | 104 ++++++++------------- 4 files changed, 53 insertions(+), 72 deletions(-) diff --git a/src/compiler/scala/tools/nsc/matching/ParallelMatching.scala b/src/compiler/scala/tools/nsc/matching/ParallelMatching.scala index b00ea9d68c..258057f4e7 100644 --- a/src/compiler/scala/tools/nsc/matching/ParallelMatching.scala +++ b/src/compiler/scala/tools/nsc/matching/ParallelMatching.scala @@ -988,8 +988,8 @@ trait ParallelMatching { final def condition(tpe: Type, scrutTree: Tree)(implicit typer : Typer): Tree = { assert((tpe ne NoType) && (scrutTree.tpe ne NoType)) - lazy val equalsRef = Equals(gen.mkAttributedRef(tpe.termSymbol), scrutTree) - lazy val isInst = gen.mkIsInstanceOf(scrutTree, tpe) + def equalsRef = Equals(gen.mkAttributedRef(tpe.termSymbol), scrutTree) + def isInst = gen.mkIsInstanceOf(scrutTree, tpe) tpe match { case _: SingletonType if !tpe.isInstanceOf[ConstantType] => diff --git a/src/compiler/scala/tools/nsc/matching/TransMatcher.scala b/src/compiler/scala/tools/nsc/matching/TransMatcher.scala index b7b16fc906..97aa3fbada 100644 --- a/src/compiler/scala/tools/nsc/matching/TransMatcher.scala +++ b/src/compiler/scala/tools/nsc/matching/TransMatcher.scala @@ -22,8 +22,6 @@ trait TransMatcher { var cunit: CompilationUnit = _ // memory leak? var resultType: Type = _ - // cache these - //final val settings_debug = settings.debug.value final val settings_squeeze = settings.Xsqueeze.value == "on" // check special case Seq(p1,...,pk,_*) @@ -47,10 +45,6 @@ trait TransMatcher { handleOuter: Tree => Tree) (implicit typer: Typer): Tree = { - //DBG("****") - //DBG("**** initalize, selector = "+selector+" selector.tpe = "+selector.tpe) - //DBG("**** doCheckExhaustive == "+doCheckExhaustive) - implicit val theOwner = owner implicit val rep = new RepFactory(handleOuter) val flags = if (doCheckExhaustive) Nil else List(Flags.TRANS_FLAG) diff --git a/src/compiler/scala/tools/nsc/symtab/Scopes.scala b/src/compiler/scala/tools/nsc/symtab/Scopes.scala index 2793a11b20..a82d86c066 100644 --- a/src/compiler/scala/tools/nsc/symtab/Scopes.scala +++ b/src/compiler/scala/tools/nsc/symtab/Scopes.scala @@ -12,12 +12,21 @@ trait Scopes { class ScopeEntry(val sym: Symbol, val owner: Scope) { /** the next entry in the hash bucket */ - var tail: ScopeEntry = _ + var tail: ScopeEntry = null /** the next entry in this scope */ var next: ScopeEntry = null + def iterator: Iterator[ScopeEntry] = new Iterator[ScopeEntry] { + private var buf: ScopeEntry = ScopeEntry.this + def hasNext = buf != null + def next = { + val res = buf + buf = buf.next + res + } + } override def hashCode(): Int = sym.name.start override def toString(): String = sym.toString() } diff --git a/src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala b/src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala index 402df5c48d..9a63c1d753 100644 --- a/src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala +++ b/src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala @@ -48,19 +48,19 @@ abstract class ExplicitOuter extends InfoTransform with TransMatcher with Patter } private def outerField(clazz: Symbol): Symbol = { - val result = clazz.info.member(nme.getterToLocal(nme.OUTER)) - if (result == NoSymbol) - assert(false, "no outer field in "+clazz+clazz.info.decls+" at "+phase) + val result = clazz.info.member(nme getterToLocal nme.OUTER) + assert(result != NoSymbol, "no outer field in "+clazz+clazz.info.decls+" at "+phase) + result } def outerAccessor(clazz: Symbol): Symbol = { - val firstTry = clazz.info.decl(clazz.expandedName(nme.OUTER)) + val firstTry = clazz.info.decl(clazz expandedName nme.OUTER) if (firstTry != NoSymbol && firstTry.outerSource == clazz) firstTry else { - var e = clazz.info.decls.elems - while ((e ne null) && e.sym.outerSource != clazz) e = e.next - if (e ne null) e.sym else NoSymbol + val e = clazz.info.decls.elems + if (e == null) NoSymbol + else (e.iterator find (_.sym.outerSource == clazz) map (_.sym)) getOrElse NoSymbol } } @@ -223,14 +223,8 @@ abstract class ExplicitOuter extends InfoTransform with TransMatcher with Patter case _ => } super.transform(tree) - } catch {//debug - case ex: Throwable => - Console.println("exception when transforming " + tree) - //Console.println(ex.getMessage) - //Console.println(ex.printStackTrace()) - //System.exit(-1); - throw ex - } finally { + } + finally { outerParam = savedOuterParam } } @@ -410,71 +404,58 @@ abstract class ExplicitOuter extends InfoTransform with TransMatcher with Patter super.transform(treeCopy.Apply(tree, sel, outerVal :: args)) case mch @ Match(selector, cases) => // <----- transmatch hook - val tid = if (settings.debug.value) { - val q = unit.fresh.newName(mch.pos, "tidmark") - Console.println("transforming patmat with tidmark "+q+" ncases = "+cases.length) - q - } else null - - var nselector = transform(selector) - def makeGuardDef(vs:List[Symbol], guard:Tree) = { + def makeGuardDef(vs: List[Symbol], guard: Tree) = { import symtab.Flags._ val gdname = cunit.fresh.newName(guard.pos, "gd") - val fmls = new ListBuffer[Type] - val method = currentOwner.newMethod(mch.pos, gdname) setFlag (SYNTHETIC) - var vs1 = vs; while (vs1 ne Nil) { - fmls += vs1.head.tpe - vs1 = vs1.tail - } - val tpe = new MethodType(method.newSyntheticValueParams(fmls.toList), - definitions.BooleanClass.tpe) + val method = currentOwner.newMethod(mch.pos, gdname) setFlag SYNTHETIC + val fmls = vs map (_.tpe) + val tpe = new MethodType(method newSyntheticValueParams fmls, BooleanClass.tpe) method setInfo tpe - localTyper. - typed { - DefDef(method, - { new ChangeOwnerTraverser(currentOwner, method).traverse(guard); - new TreeSymSubstituter(vs, method.paramss.head).traverse(guard);guard})} + + localTyper.typed( + DefDef(method, { + new ChangeOwnerTraverser(currentOwner, method) traverse guard + new TreeSymSubstituter(vs, method.paramss.head) traverse guard + guard + }) + ) } val nguard = new ListBuffer[Tree] - val ncases = new ListBuffer[CaseDef] - var cs = cases; while (cs ne Nil) { - cs.head match { - case CaseDef(p,EmptyTree,b) => - ncases += CaseDef(transform(p), EmptyTree, transform(b)) - case CaseDef(p, guard, b) => - val vs = definedVars(p) - val guardDef = makeGuardDef(vs, guard) - nguard += transform(guardDef) - val gdcall = localTyper.typed{Apply(Ident(guardDef.symbol),vs.map {Ident(_)})} - ncases += CaseDef(transform(p), gdcall, transform(b)) + val ncases = + for (CaseDef(p, guard, b) <- cases) yield { + val gdcall = + if (guard == EmptyTree) EmptyTree + else { + val vs = definedVars(p) + val guardDef = makeGuardDef(vs, guard) + nguard += transform(guardDef) // building up list of guards + + localTyper.typed(Apply(Ident(guardDef.symbol), vs map Ident)) + } + CaseDef(transform(p), gdcall, transform(b)) } - cs = cs.tail - } - - var checkExhaustive = true - var requireSwitch = false def isUncheckedAnnotation(tpe: Type) = tpe hasAnnotation UncheckedClass def isSwitchAnnotation(tpe: Type) = tpe hasAnnotation SwitchClass - nselector match { + val (checkExhaustive, requireSwitch) = nselector match { case Typed(nselector1, tpt) => - if (isUncheckedAnnotation(tpt.tpe)) { + val unchecked = isUncheckedAnnotation(tpt.tpe) + if (unchecked) nselector = nselector1 - checkExhaustive = false - } - if (isSwitchAnnotation(tpt.tpe)) - requireSwitch = true - case _ => + + (!unchecked, isSwitchAnnotation(tpt.tpe)) + case _ => + (true, false) } ExplicitOuter.this.resultType = tree.tpe val t = atPos(tree.pos) { - val t_untyped = handlePattern(nselector, ncases.toList, checkExhaustive, currentOwner, transform)(localTyper) + val t_untyped = handlePattern(nselector, ncases, checkExhaustive, currentOwner, transform)(localTyper) /* if @switch annotation is present, verify the resulting tree is a Match */ if (requireSwitch) t_untyped match { case Block(_, Match(_, _)) => // ok @@ -485,9 +466,6 @@ abstract class ExplicitOuter extends InfoTransform with TransMatcher with Patter localTyper.typed(t_untyped, resultType) } - if (settings.debug.value) - Console.println("finished translation of " + tid) - if (nguard.isEmpty) t else Block(nguard.toList, t) setType t.tpe -- cgit v1.2.3