summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/transform/PostErasure.scala
blob: 2a86d711f1e72ca202fd5edce76f33d8ad5c92bb (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
/* NSC -- new Scala compiler
 * Copyright 2005-2013 LAMP/EPFL
 * @author Martin odersky
 */
package scala.tools.nsc
package transform

/** This phase maps ErasedValueTypes to the underlying unboxed representation and
 *  performs peephole optimizations.
 */
trait PostErasure extends InfoTransform with TypingTransformers {
  val global: Global

  import global._
  import treeInfo._

  val phaseName: String = "posterasure"

  def newTransformer(unit: CompilationUnit): Transformer = new PostErasureTransformer(unit)
  override def changesBaseClasses = false

  object elimErasedValueType extends TypeMap {
    def apply(tp: Type) = tp match {
      case ConstantType(Constant(tp: Type)) => ConstantType(Constant(apply(tp)))
      case ErasedValueType(tref)            => enteringErasure(erasure.erasedValueClassArg(tref))
      case _                                => mapOver(tp)
    }
  }

  def transformInfo(sym: Symbol, tp: Type) = elimErasedValueType(tp)

  class PostErasureTransformer(unit: CompilationUnit) extends TypingTransformer(unit) {
    override def transform(tree: Tree) = {
      def finish(res: Tree) = logResult(s"Posterasure reduction\n  Old: $tree\n  New")(res)

      /** We use the name of the operation being performed and not the symbol
       *  itself because the symbol hails from the boxed class, and this transformation
       *  exists to operate directly on the values. So we are for instance looking
       *  up == on an lhs of type Int, whereas the symbol which has been passed in
       *  is from java.lang.Integer.
       */
      def binop(lhs: Tree, op: Symbol, rhs: Tree) =
        finish(localTyper typed (Apply(Select(lhs, op.name) setPos tree.pos, rhs :: Nil) setPos tree.pos))

      super.transform(tree) setType elimErasedValueType(tree.tpe) match {
        case AsInstanceOf(v, tpe) if v.tpe <:< tpe => finish(v)          // x.asInstanceOf[X]       ==> x
        case ValueClass.BoxAndUnbox(v)             => finish(v)          // (new B(v)).unbox        ==> v
        case ValueClass.BoxAndCompare(v1, op, v2)  => binop(v1, op, v2)  // new B(v1) == new B(v2)  ==> v1 == v2
        case tree                                  => tree
      }
    }
  }
}