summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/matching/PatternBindings.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/compiler/scala/tools/nsc/matching/PatternBindings.scala')
-rw-r--r--src/compiler/scala/tools/nsc/matching/PatternBindings.scala69
1 files changed, 32 insertions, 37 deletions
diff --git a/src/compiler/scala/tools/nsc/matching/PatternBindings.scala b/src/compiler/scala/tools/nsc/matching/PatternBindings.scala
index 575050821a..f791236a10 100644
--- a/src/compiler/scala/tools/nsc/matching/PatternBindings.scala
+++ b/src/compiler/scala/tools/nsc/matching/PatternBindings.scala
@@ -7,6 +7,7 @@ package scala.tools.nsc
package matching
import transform.ExplicitOuter
+import collection.immutable.TreeMap
trait PatternBindings extends ast.TreeDSL
{
@@ -44,55 +45,49 @@ trait PatternBindings extends ast.TreeDSL
}
case class Binding(pvar: Symbol, tvar: Symbol) {
- override def toString() = "%s: %s @ %s: %s".format(pvar.name, pvar.tpe, tvar.name, tvar.tpe)
+ // see bug #1843 for the consequences of not setting info.
+ // there is surely a better way to do this, especially since
+ // this looks to be the only usage of containsTp anywhere
+ // in the compiler, but it suffices for now.
+ if (tvar.info containsTp WildcardType)
+ tvar setInfo pvar.info
+
+ def toIdent =
+ Ident(tvar) setType pvar.tpe
+
+ def castIfNeeded =
+ if (tvar.tpe <:< pvar.tpe) ID(tvar)
+ else ID(tvar) AS_ANY pvar.tpe
}
- case class BindingsInfo(xs: List[BindingInfo]) {
- def idents = xs map (_.ident)
- def vsyms = xs map (_.vsym)
+ case class BindingsInfo(xs: List[Binding]) {
+ def patternVars = xs map (_.pvar)
+ def temporaryVars = xs map (_.tvar)
+ def idents = xs map (_.toIdent)
- def vdefs(implicit context: MatrixContext) =
- xs map (x => context.typedValDef(x.vsym, x.ident))
+ def patternValDefs(implicit context: MatrixContext) =
+ for (b @ Binding(pvar, tvar) <- xs) yield
+ context.typedValDef(pvar, b.toIdent)
}
- case class BindingInfo(vsym: Symbol, ident: Ident)
- case class Bindings(bindings: Binding*) extends Function1[Symbol, Option[Ident]] {
- private def castIfNeeded(pvar: Symbol, tvar: Symbol) =
- if (tvar.tpe <:< pvar.tpe) ID(tvar)
- else ID(tvar) AS_ANY pvar.tpe
+ class Bindings(private val vlist: List[Binding]) extends Function1[Symbol, Option[Ident]] {
+ def this() = this(Nil)
+
+ def vmap(v: Symbol): Option[Binding] = vlist find (_.pvar eq v)
// filters the given list down to those defined in these bindings
- def infoFor(vs: List[Symbol]): BindingsInfo = BindingsInfo(
- for (v <- vs ; substv <- apply(v)) yield
- BindingInfo(v, substv)
- )
+ def infoFor(vs: List[Symbol]) = BindingsInfo(vs map vmap flatten)
+ def infoForAll = BindingsInfo(vlist)
def add(vs: Iterable[Symbol], tvar: Symbol): Bindings = {
- def newBinding(v: Symbol) = {
- // see bug #1843 for the consequences of not setting info.
- // there is surely a better way to do this, especially since
- // this looks to be the only usage of containsTp anywhere
- // in the compiler, but it suffices for now.
- if (tvar.info containsTp WildcardType)
- tvar setInfo v.info
-
- Binding(v, tvar)
- }
- val newBindings = vs.toList map newBinding
- Bindings(newBindings ++ bindings: _*)
+ val newBindings = vs.toList map (v => Binding(v, tvar))
+ new Bindings(newBindings ++ vlist)
}
- def apply(v: Symbol): Option[Ident] =
- bindings find (_.pvar eq v) map (x => Ident(x.tvar) setType v.tpe)
-
- override def toString() =
- if (bindings.isEmpty) "" else bindings.mkString(" Bound(", ", ", ")")
+ def apply(v: Symbol): Option[Ident] = vmap(v) map (_.toIdent)
- /** The corresponding list of value definitions. */
- final def targetParams(implicit typer: analyzer.Typer): List[ValDef] =
- for (Binding(v, t) <- bindings.toList) yield
- VAL(v) === (typer typed castIfNeeded(v, t))
+ override def toString() = " Bound(%s)".format(vlist)
}
- val NoBinding: Bindings = Bindings()
+ val NoBinding: Bindings = new Bindings()
} \ No newline at end of file