aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/transform/TreeChecker.scala
blob: f52c5bc1c94d7facb5757f567258209d21034155 (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
package dotty.tools.dotc
package transform

import TreeTransforms._
import core.DenotTransformers._
import core.Denotations._
import core.SymDenotations._
import core.Contexts._
import core.Symbols._
import core.Types._
import core.Constants._
import core.StdNames._
import core.Decorators._
import core.transform.Erasure.isUnboundedGeneric
import typer._
import typer.ErrorReporting._
import ast.Trees._
import ast.{tpd, untpd}

/** This transform eliminates patterns. Right now it's a dummy.
 *  Awaiting the real pattern matcher.
 */
class TreeChecker {
  import ast.tpd._

  def check(ctx: Context) = {
    println(s"checking ${ctx.compilationUnit} after phase ${ctx.phase.prev}")
    Checker.typedExpr(ctx.compilationUnit.tpdTree)(ctx)
  }

  object Checker extends ReTyper {
    override def typed(tree: untpd.Tree, pt: Type)(implicit ctx: Context) =
      if (tree.isEmpty) tree.asInstanceOf[Tree]
      else {
        assert(tree.hasType, tree.show)
        val tree1 = super.typed(tree, pt)
        def sameType(tp1: Type, tp2: Type) =
          (tp1 eq tp2) || // accept NoType / NoType
          (tp1 =:= tp2)
        def divergenceMsg =
          s"""Types differ
           |Original type : ${tree.typeOpt.show}
           |After checking: ${tree1.tpe.show}
           |Original tree : ${tree.show}
           |After checking: ${tree1.show}
        """.stripMargin
        assert(sameType(tree1.tpe, tree.typeOpt), divergenceMsg)
        tree1
      }

    override def typedIdent(tree: untpd.Ident, pt: Type)(implicit ctx: Context): Tree = {
      assert(tree.isTerm, tree.show)
      super.typedIdent(tree, pt)
    }

    override def typedSelect(tree: untpd.Select, pt: Type)(implicit ctx: Context): Tree = {
      assert(tree.isTerm, tree.show)
      super.typedSelect(tree, pt)
    }
  }
}

object TreeChecker extends TreeChecker