summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/transform/LiftCode.scala
blob: ba90ed87e440c5c1272c8752319048f1bb359412 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* NSC -- new Scala compiler
 * Copyright 2005-2011 LAMP/EPFL
 * @author Gilles Dubochet
 */

package scala.tools.nsc
package transform

import symtab._
import Flags._
import scala.collection.{ mutable, immutable }
import scala.collection.mutable.ListBuffer
import scala.tools.nsc.util.FreshNameCreator

/** Translate expressions of the form reflect.Code.lift(exp)
 *  to the lifted "reflect trees" representation of exp.
 *  Also: mutable variables that are accessed from a local function are wrapped in refs.
 *
 *  @author Gilles Dubochet
 *  @author Martin Odersky
 *  @version 2.10
 */
abstract class LiftCode extends Transform with TypingTransformers {

  import global._                  // the global environment
  import definitions._             // standard classes and methods
  import typer.{typed, atOwner}    // methods to type trees

  val symbols: global.type = global

  /** the following two members override abstract members in Transform */
  val phaseName: String = "liftcode"

  def newTransformer(unit: CompilationUnit): Transformer =
    new Lifter(unit)

  class Lifter(unit: CompilationUnit) extends TypingTransformer(unit) {

    /** Set of mutable local variables that are free in some inner method. */
    private val freeMutableVars: mutable.Set[Symbol] = new mutable.HashSet
    private val converted: mutable.Set[Symbol] = new mutable.HashSet // debug

    override def transformUnit(unit: CompilationUnit) {
      freeMutableVars.clear()
      freeLocalsTraverser(unit.body)
      atPhase(phase.next) {
        super.transformUnit(unit)
      }
      for (v <- freeMutableVars)
        assert(converted contains v, "unconverted: "+v+" in "+v.owner+" in unit "+unit)
    }

    override def transform(tree: Tree): Tree = {
      val sym = tree.symbol
      tree match {
        case Apply(lift, List(tree)) if sym == Code_lift =>
          //printTypings = true //debug
          val result = transform(localTyper.typedPos(tree.pos)(codify(tree)))
          //println("transformed = "+result) //debug
          //printTypings = false //debug
          result
        case ValDef(mods, name, tpt, rhs) if (freeMutableVars(sym)) =>
          val tpt1 = TypeTree(sym.tpe) setPos tpt.pos
          /* Creating a constructor argument if one isn't present. */
          val constructorArg = rhs match {
            case EmptyTree => gen.mkZero(atPhase(phase.prev)(sym.tpe))
            case _ => transform(rhs)
          }
          val rhs1 = typer.typedPos(rhs.pos) {
            /*util.errtrace("lifted rhs for "+tree+" in "+unit)*/ (
            Apply(Select(New(TypeTree(sym.tpe)), nme.CONSTRUCTOR), List(constructorArg)))
          }
          sym resetFlag MUTABLE
          sym removeAnnotation VolatileAttr
          converted += sym
          treeCopy.ValDef(tree, mods &~ MUTABLE, name, tpt1, rhs1)
        case Ident(name) if freeMutableVars(sym) =>
          localTyper.typedPos(tree.pos) {
            /*util.errtrace("lifting ")*/(Select(tree setType sym.tpe, nme.elem))
          }
        case _ =>
          super.transform(tree)
      }
    }

    /** todo: Treat embedded Code blocks by merging them into containing block
     *
     */
    class Reifier() {

      private val boundVars: mutable.Set[Symbol] = mutable.Set()

      // todo: review & rework
      // todo replace className by caseName in CaseClass once we have switched to nsc.
      def className(value: AnyRef): String = value match {
        case _ :: _ => "scala.$colon$colon"
        case MethodType(_, _) => "scala.reflect.runtime.Mirror.MethodType"
        case x: Product => "scala.reflect.runtime.Mirror." + x.productPrefix
        case _ => ""
      }

      // todo: review & rework
      def objectName(value: Any): String = value match {
        case Nil => "scala.collection.immutable.Nil"
        case EmptyTree => "scala.reflect.runtime.Mirror.EmptyTree"
        case NoSymbol => "scala.reflect.runtime.Mirror.NoSymbol"
        case definitions.RootClass => "scala.reflect.runtime.Mirror.definitions.RootClass"
        case NoPrefix => "scala.reflect.runtime.Mirror.NoPrefix"
        case NoType => "scala.reflect.runtime.Mirror.NoType"
        case _ => ""
      }

      /** Reify a free reference. The result will be either a mirror reference
       *  to a global value, or else a mirror Literal.
       */
      def reifyFree(tree: Tree): Tree =
        if (tree.symbol.hasFlag(MODULE) && tree.symbol.isStatic)
          reifiedPath(tree.symbol.fullName)
        else
          Apply(termPath("scala.reflect.runtime.Mirror.freeValue"), List(tree))

      /** Reify an arbitary value */
      def reify(value: Any): Tree = {
        //println("reifing "+value) //debug
        /*util.trace("reified "+value+" --> ")*/ {
          value match {
            case tree: DefTree =>
              boundVars += tree.symbol
              reify1(tree)
            case tree @ This(_) if !(boundVars contains tree.symbol) =>
              reifyFree(tree)
            case tree @ Ident(_) if !(boundVars contains tree.symbol) =>
              reifyFree(tree)
            case tree @ TypeTree() if tree.original != null =>
              reify(tree.original)
            // todo: should we also reify inferred types?
            case _ =>
              reify1(value)
          }
        }
      }

      /** Second part of reify */
      def reify1(value: Any): Tree = {
        def treatProduct(c: Product): Tree = {
          val fullname = objectName(c)
          if (fullname.length != 0)
            termPath(fullname)
          else {
            val fullname = className(c)
            if (fullname.length == 0) abort("don't know how to inject " + value + " of class " + value.getClass)
            val injectedArgs = new ListBuffer[Tree]
            for (i <- 0 until c.productArity)
              injectedArgs += reify(c.productElement(i))
            New(typePath(fullname), List(injectedArgs.toList))
          }
        }

        value match {
          case () => Literal(Constant(()))
          case x: String => Literal(Constant(x))
          case x: Boolean => Literal(Constant(x))
          case x: Byte => Literal(Constant(x))
          case x: Short => Literal(Constant(x))
          case x: Char => Literal(Constant(x))
          case x: Int => Literal(Constant(x))
          case x: Long => Literal(Constant(x))
          case x: Float => Literal(Constant(x))
          case x: Double => Literal(Constant(x))
          case x: Name => reifiedName(x)
          case c: Product => treatProduct(c)
          case _ =>
            abort("don't know how to inject " + value + " of class " + value.getClass)
        }
      }

     /** The reified version of given name */
      def reifiedName(name: Name) = {
        val fn = "scala.reflect.runtime.Mirror.new"+(if (name.isTypeName) "TypeName" else "TermName")
        Apply(termPath(fn), List(Literal(Constant(name.toString))))
      }

      /** A reified identifier with given name */
      def reifiedIdent(name: Name) =
        New(typePath("scala.reflect.runtime.Mirror.Ident"), List(List(reifiedName(name))))

      /** A reified selection over given qualifier and name */
      def reifiedSelect(qual: Tree, name: Name) =
        New(typePath("scala.reflect.runtime.Mirror.Select"), List(List(qual, reifiedName(name))))

      /** A reified path that selects definition with given fully qualified name */
      def reifiedPath(fullname: String): Tree = {
        val parts = fullname split "\\."
        val prefixParts = parts.init
        val lastName = parts.last
        if (prefixParts.isEmpty) reifiedIdent(lastName)
        else {
          val prefixTree = ((reifiedIdent(prefixParts.head): Tree) /: prefixParts.tail)(reifiedSelect(_, _))
          reifiedSelect(prefixTree, lastName)
        }
      }

      /** An (unreified) path that refers to definition with given fully qualified name
       *  @param mkName   Creator for last portion of name (either TermName or TypeName)
       */
      private def path(fullname: String, mkName: String => Name): Tree = {
        val parts = fullname split "\\."
        val prefixParts = parts.init
        val lastName = mkName(parts.last)
        if (prefixParts.isEmpty) Ident(lastName)
        else {
          val prefixTree = ((Ident(prefixParts.head): Tree) /: prefixParts.tail)(Select(_, _))
          Select(prefixTree, lastName)
        }
      }

      /** An (unreified) path that refers to term definition with given fully qualified name */
      def termPath(fullname: String) = path(fullname, newTermName)

      /** An (unreified) path that refers to type definition with given fully qualified name */
      def typePath(fullname: String) = path(fullname, newTypeName)
    }

    def codify(tree: Tree): Tree = /*util.trace("codified " + tree + " -> ")*/ {
      val targetType = definitions.CodeClass.primaryConstructor.info.paramTypes.head
      val arg = gen.mkAsInstanceOf(new Reifier().reify(tree), targetType, wrapInApply = false)
      New(TypeTree(appliedType(definitions.CodeClass.typeConstructor, List(tree.tpe))),
        List(List(arg)))
    }

    /**
     * PP: There is apparently some degree of overlap between the CAPTURED
     *  flag and the role being filled here.  I think this is how this was able
     *  to go for so long looking only at DefDef and Ident nodes, as bugs
     *  would only emerge under more complicated conditions such as #3855.
     *  I'll try to figure it all out, but if someone who already knows the
     *  whole story wants to fill it in, that too would be great.
     */
    private val freeLocalsTraverser = new Traverser {
      var currentMethod: Symbol = NoSymbol
      var maybeEscaping = false

      def withEscaping(body: => Unit) {
        val saved = maybeEscaping
        maybeEscaping = true
        try body
        finally maybeEscaping = saved
      }

      override def traverse(tree: Tree) = tree match {
        case DefDef(_, _, _, _, _, _) =>
          val lastMethod = currentMethod
          currentMethod = tree.symbol
          try super.traverse(tree)
          finally currentMethod = lastMethod
        /** A method call with a by-name parameter represents escape. */
        case Apply(fn, args) if fn.symbol.paramss.nonEmpty =>
          traverse(fn)
          (fn.symbol.paramss.head, args).zipped foreach { (param, arg) =>
            if (param.tpe != null && isByNameParamType(param.tpe))
              withEscaping(traverse(arg))
            else
              traverse(arg)
          }
        /** The rhs of a closure represents escape. */
        case Function(vparams, body) =>
          vparams foreach traverse
          withEscaping(traverse(body))

        /**
         * The appearance of an ident outside the method where it was defined or
         *  anytime maybeEscaping is true implies escape.
         */
        case Ident(_) =>
          val sym = tree.symbol
          if (sym.isVariable && sym.owner.isMethod && (maybeEscaping || sym.owner != currentMethod)) {
            freeMutableVars += sym
            val symTpe = sym.tpe
            val symClass = symTpe.typeSymbol
            atPhase(phase.next) {
              def refType(valueRef: Map[Symbol, Symbol], objectRefClass: Symbol) =
                if (isValueClass(symClass)) valueRef(symClass).tpe
                else appliedType(objectRefClass.typeConstructor, List(symTpe))

              sym updateInfo (
                if (sym.hasAnnotation(VolatileAttr)) refType(volatileRefClass, VolatileObjectRefClass)
                else refType(refClass, ObjectRefClass))
            }
          }
        case _ =>
          super.traverse(tree)
      }
    }
  }
}

// case EmptyTree =>
// case LiftPoint(tree) =>
// case PackageDef(pid, stats) =>
// case ClassDef(mods, name, tparams, impl) =>
// case ValDef(mods, name, tpt, rhs) =>
// case DefDef(mods, name, tparams, vparamss, tpt, rhs) =>
// case TypeDef(mods, name, tparams, rhs) =>
// case LabelDef(name, params, rhs) =>
// case Template(parents, self, body) =>
// case Block(stats, expr) =>
// case ArrayValue(elemtpt, trees) =>
// case Assign(lhs, rhs) =>
// case If(cond, thenp, elsep) =>
// case Match(selector, cases) =>
// case Return(expr) =>
// case Try(block, catches, finalizer) =>
// case Throw(expr) =>
// case New(tpt) =>
// case Typed(expr, tpt) =>
// case TypeApply(fun, args) =>
// case Apply(fun, args) =>
// case Super(qual, mix) =>
// case This(qual) =>
// case Select(qualifier, selector) =>
// case Ident(name) =>
// case Literal(value) =>
// case TypeTree() =>
// /* Pattern matching */
// case CaseDef(pat, guard, body) =>
// case Alternative(trees) =>
// case Star(elem) =>
// case Bind(name, body) =>