summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/matching/TransMatcher.scala
blob: 97dd2b64249282017111b4adfc4cf8ae6a4e2ba6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* NSC -- new Scala compiler
 * Copyright 2005-2007 LAMP/EPFL
 * Copyright 2007 Google Inc. All Rights Reserved.
 * Author: bqe@google.com (Burak Emir)
 */
// $Id$

package scala.tools.nsc.matching

/** Translation of pattern matching
 *
 *  @author Burak Emir
 */
trait TransMatcher { self: transform.ExplicitOuter with PatternNodes with ParallelMatching with CodeFactory =>

  import global._
  import definitions._
  import posAssigner.atPos
  import symtab.Flags
  import typer.typed

  import collection.mutable.ListBuffer

  var cunit: CompilationUnit = _ // memory leak?
  def fresh = cunit.fresh
  var nPatterns = 0
  var resultType: Type = _

  // cache these
  final val settings_debug       = settings.debug.value
  final val settings_squeeze     = settings.Xsqueeze.value == "on"
  final val settings_casetags    = settings.Xcasetags.value == "on"

  final def hasRegularPattern(pats1: List[Tree]): Boolean = {
    var pats = pats1; while(pats ne Nil) {
      if(isRegularPattern(pats.head)) { return true; } else { pats = pats.tail }
    }
    return false
  }

  final def isRegularPattern(pat: Tree): Boolean = {
    pat match {
      case Alternative(trees)    => hasRegularPattern(trees)
      case Star(t)               => true
      case Ident(_)              => false
      case Bind(n, pat1)         => isRegularPattern(pat1)
      case Sequence(trees)       => true // cause there are ArrayValues now
      case ArrayValue(tt, trees) => hasRegularPattern(trees)
      case Apply(fn, trees)      => hasRegularPattern(trees)
      case Literal(_)            => false
      case Select(_, _)          => false
      case Typed(_, _)           => false
      case UnApply(_,trees)      => hasRegularPattern(trees)
    }
  }

  // @todo: this should be isNotRegular :-/ premature opt src of all evil
  // check special case Seq(p1,...,pk,_*) where pi not regular
  protected def isRightIgnoring(p: ArrayValue): Boolean = {
    def isDefaultStar(p: Tree): Boolean = p match {
      case Bind(_, q)                 => isDefaultStar(q)
      case Star(Ident(nme.WILDCARD))  => true
      case _                          => false
    }
    p match {
      case ArrayValue(s, trees) =>
        var ts = trees
	var c: Tree = null
	while ((ts ne Nil) && {c = ts.head; ts = ts.tail; !isRegularPattern(c)}) {}
	(ts eq Nil) && isDefaultStar(c)
    }
  }

  /** a casedef with sequence subpatterns like
   *
   *  case ..x @ ().. => body
   *
   * should be replaced straight away with
   *
   *  case    .. () .. => val x = Nil; body
   */
  def isRegular(pats: List[CaseDef]): (List[CaseDef],Boolean) = {
    var existsReg = false
    var isReg = false
    var nilVars: List[Symbol] = null

    def isRegular1(pat: Tree): Tree = pat match {
      case Alternative(trees) =>
        copy.Alternative(pat, trees map { isRegular1 })

      case Star(t) =>
        isReg = true; copy.Star(pat, isRegular1(t))

      case Ident(_) =>
        pat

      case Bind(id, empt @ Sequence(List())) =>
        nilVars = pat.symbol :: nilVars
        empt

      case Bind(n, pat1) =>
        copy.Bind(pat, n, isRegular1(pat1))

      case Sequence(trees) =>
        isReg = true
        copy.Sequence(pat, trees map { isRegular1 })

      case UnApply(fn, args) => copy.UnApply(pat, fn, args map { isRegular1 })

      case app @ Apply(fn, List(pat2@ ArrayValue( tt, List(b @ Bind(id, Star(wc @ Ident(nme.WILDCARD))))))) if (app.tpe.typeSymbol.flags & Flags.CASE) == 0 =>
        val tpe1:Type = pat2.tpe.widen.baseType( definitions.SeqClass ).typeArgs(0)
	val tpe = appliedType(definitions.SeqClass.typeConstructor, List(tpe1))
	b.symbol.setInfo(tpe)
	b.setType(tpe)
        copy.Bind(b, id, wc)

      case app @ Apply(fn, List(pat2@ ArrayValue( tt, List(b @ Bind(id, Star(wc @ Ident(nme.WILDCARD))))))) =>  // a pattern of the form MyCaseConstructor(foo@_*)
        val tpe1:Type = pat2.tpe.widen.baseType( definitions.SeqClass ).typeArgs(0)
	val tpe = appliedType(definitions.SeqClass.typeConstructor, List(tpe1))
	b.symbol.setInfo(tpe)
	b.setType(tpe)
        copy.Apply(pat, fn, List(copy.Bind(b, id, wc)))

      case av @ ArrayValue(s, trees) =>
        if (isRightIgnoring(av)) pat
        else copy.ArrayValue(pat, s, (trees map { isRegular1 }))

      case Apply(fn, List(Sequence(List()))) =>
        pat

      case Apply(fn, trees) =>
        copy.Apply(pat, fn, (trees map { isRegular1 }))

      case Literal(_) =>
        pat

      case Select(_, _) =>
        pat

      case Typed(_, _) =>
        pat

      case This(_) =>
        val stpe = mkThisType(pat.tpe.typeSymbol)
        Typed(Ident(nme.WILDCARD) setType stpe, TypeTree(stpe))
    }

    var res = new ListBuffer[CaseDef]
    val it = pats.elements; while (it.hasNext) {
      nilVars = Nil
      val cdef = it.next
      val newt = isRegular1(cdef.pat)
      existsReg = existsReg || isReg

      val nbody = if (nilVars.isEmpty) cdef.body else
        atPos(cdef.body.pos)(
          Block(nilVars map {
            x => ValDef(x, Ident(definitions.NilModule))
          }, cdef.body)
        )

      res += copy.CaseDef(cdef, newt, cdef.guard, nbody)
    }
    (res.toList, existsReg)
  }

  /** handles all translation of pattern matching
   */
  def handlePattern(selector: Tree, ocases: List[CaseDef], doCheckExhaustive: Boolean, owner: Symbol, handleOuter: Tree => Tree): Tree = {
    val (cases, containsReg) = isRegular(ocases)
    // @todo: remove unused variables
    if (containsReg) {
      cunit.error(selector.pos, "regular expressions not yet implemented")
      //sel
      EmptyTree
    } else {
      implicit val theOwner = owner
      if (settings_debug) {
        Console.println("****")
        Console.println("**** initalize, selector = "+selector+" selector.tpe = "+selector.tpe)
        Console.println("****    doCheckExhaustive == "+doCheckExhaustive)
      }

      implicit val rep = new RepFactory(handleOuter)
      try {
        val tmps = new ListBuffer[Symbol]
        val vds  = new ListBuffer[Tree]
        var root:Symbol = newVar(selector.pos, selector.tpe)
        if (!doCheckExhaustive)
          root.setFlag(symtab.Flags.TRANS_FLAG)

        var vdef:Tree        = typed{ValDef(root, selector)}
        var theFailTree:Tree = ThrowMatchError(selector.pos, mkIdent(root))

        if (definitions.isTupleType(selector.tpe)) selector match {
          case app @ Apply(fn, args)
          if (fn.symbol eq selector.tpe.decls.lookup(nme.CONSTRUCTOR)) &&
          (cases forall { x => x match {
            case CaseDef(Apply(fn, pargs),_,_) => true ;
            case CaseDef(Ident(nme.WILDCARD),_,_) => true  ;
            case _ => false
          }}) =>
            var i = 0
            var as = args
            while(as ne Nil) {
              val ti = as.head
              val v = newVar(ti.pos, cunit.fresh.newName("tp"), selector.tpe.typeArgs(i))
              if (!doCheckExhaustive)
                v.setFlag(symtab.Flags.TRANS_FLAG)
              vds  += typedValDef(v, ti)
              tmps += v
              i = i + 1
              as = as.tail
            }
          theFailTree = ThrowMatchError(selector.pos, copy.Apply(app, fn, tmps.toList map mkIdent))
          case _ =>
            tmps += root
            vds  += vdef
        } else {
          tmps += root
          vds  += vdef
        }
        val irep = initRep(tmps.toList, cases, rep)

        implicit val fail: Tree = theFailTree

        val mch  = typed{ repToTree(irep)}
        var dfatree = typed{Block(vds.toList, mch)}
        // cannot use squeezedBlock because of side-effects, see t275
        //DEBUG("**** finished\n"+dfatree.toString)
        var bx = 0; var cs = cases; while(cs ne Nil) {
          if (!rep.isReached(bx)) {
            cunit.error(cs.head.asInstanceOf[CaseDef].body.pos, "unreachable code")
          }
          cs = cs.tail
          bx += 1
        }
        dfatree = rep.cleanup(dfatree)
        resetTrav.traverse(dfatree)
        return dfatree
      } catch {
        case e => e.printStackTrace(); throw new FatalError(e.getMessage())
      }
    }
  }

  object resetTrav extends Traverser {
    override def traverse(x: Tree): Unit = x match {
      case vd @ ValDef(_, _, _, _) =>
        if (vd.symbol hasFlag symtab.Flags.SYNTHETIC) {
          vd.symbol resetFlag symtab.Flags.TRANS_FLAG
          vd.symbol resetFlag symtab.Flags.MUTABLE
        }
      case _ =>
        super.traverse(x)
    }
  }
}