summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala
blob: 34f832f2a61518dc6d47f31fb2a0336c8bf055f9 (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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/* NSC -- new Scala compiler
 * Copyright 2005-2009 LAMP/EPFL
 * @author  Martin Odersky
 */
// $Id: NamesDefaults.scala 17081 2009-02-10 17:45:38Z rytz $

package scala.tools.nsc.typechecker

import scala.collection.mutable.ListBuffer
  import symtab.Flags._

/**
 *  @author Lukas Rytz
 *  @version 1.0
 */
trait NamesDefaults { self: Analyzer =>

  import global._
  import definitions._

  case class NamedApplyInfo(qual: Option[Tree], targs: List[Tree],
                            vargss: List[List[Tree]], blockTyper: Typer)
  val noApplyInfo = NamedApplyInfo(None, Nil, Nil, null)

  def nameOf(arg: Tree) = arg match {
    case a @ Assign(Ident(name), rhs) if a.namedArg => Some(name)
    case _ => None
  }
  def isNamed(arg: Tree) = nameOf(arg).isDefined


  /** @param pos maps indicies from old to new */
  def reorderArgs[T](args: List[T], pos: Int => Int): List[T] = {
    val res = new Array[T](args.length)
    // (hopefully) faster than zipWithIndex
    (0 /: args) { case (index, arg) => res(pos(index)) = arg; index + 1 }
    res.toList
  }

  /** @param pos maps indicies from new to old (!) */
  def reorderArgsInv[T](args: List[T], pos: Int => Int): List[T] = {
    val argsArray = args.toArray
    val res = new ListBuffer[T]
    for (i <- 0 until argsArray.length)
      res += argsArray(pos(i))
    res.toList
  }

  /** returns `true' if every element is equal to its index */
  def isIdentity(a: Array[Int]) = (0 until a.length).forall(i => a(i) == i)

  /**
   * Transform a function application into a Block, and assigns typer.context
   * .namedApplyBlockInfo to the new block as side-effect. If tree has the form
   *    Apply(fun, args)
   * first the function "fun" (which might be an application itself!) is transformed into a
   * block of the form
   *   {
   *     val qual$1 = qualifier_of_fun
   *     val x$1 = arg_1_of_fun
   *     ...
   *     val x$n = arg_n_of_fun
   *     qual$1.fun[targs](x$1, ...)...(..., x$n)
   *   }
   * then for each argument in args, a value is created and entered into the block. finally
   * the application expression of the block is updated.
   *   {
   *     val qual$1 = ..
   *     ...
   *     val x$n = ...
   *  >  val qual$n+1 = arg(1)
   *  >  ...
   *  >  val qual$n+m = arg(m)
   *  >  qual$1.fun[targs](x$1, ...)...(..., x$n)(x$n+1, ..., x$n+m)
   *   }
   *
   * @param typer the typer calling this method; this method calls
   *    typer.doTypedApply
   * @param mode the mode to use for calling typer.doTypedApply
   * @param pt the expected type for calling typer.doTypedApply
   *
   * @param tree: the function application tree
   * @argPos: a function mapping arguments from their current position to the
   *   position specified by the method type. example:
   *    def foo(a: Int, b: String)
   *    foo(b = "1", a = 2)
   *  calls
   *    transformNamedApplication(Apply(foo, List("1", 2), { 0 => 1, 1 => 0 })
   *
   *  @return the transformed application (a Block) together with the NamedApplyInfo.
   *     if isNamedApplyBlock(tree), returns the existing context.namedApplyBlockInfo
   */
  def transformNamedApplication(typer: Typer, mode: Int, pt: Type)
                               (tree: Tree, argPos: Int => Int): Tree = {
    import typer._
    import typer.infer._
    val context = typer.context
    import context.unit

    /**
     * Transform a function into a block, and assing context.namedApplyBlockInfo to
     * the new block as side-effect.
     * Fun is transformed in the following way:
     *  - Ident(f)                             ==>  Block(Nil, Ident(f))
     *  - Select(qual, f) if (qual is stable)  ==>  Block(Nil, Select(qual, f))
     *  - Select(qual, f) otherwise            ==>  Block(ValDef(qual$1, qual), Select(qual$1, f))
     *  - TypeApply(fun, targs)                ==>  Block(Nil or qual$1, TypeApply(fun, targs))
     *  - Select(New(TypeTree()), <init>)      ==>  Block(Nil, Select(New(TypeTree()), <init>))
     *  - Select(New(Select(qual, typeName)), <init>) if (qual is stable)
     *       ==>  Block(Nil, Select(...))
     *  - Select(New(Select(qual, typeName)), <init>) otherwise
     *       ==>  Block(ValDef(qual$1, qual), Select(New(Select(qual$1, typeName)), <init>))
     */
    def baseFunBlock(baseFun: Tree): Tree = {
      val isConstr = baseFun.symbol.isConstructor
      val blockTyper = newTyper(context.makeNewScope(tree, context.owner)(BlockScopeKind(context.depth)))

      // baseFun1: extract the function from a potential TypeApply
      // funTargs: type arguments on baseFun, used to reconstruct TypeApply in blockWith(Out)Qualifier
      // defaultTargs: type arguments to be used for calling defaultGetters. If the type arguments are given
      //   in the source code, re-use them for default getter. Otherwise infer the default getter's t-args.
      val (baseFun1, funTargs, defaultTargs) = baseFun match {
        case TypeApply(fun, targs) =>
          val targsInSource =
            if (targs.forall(a => context.undetparams contains a.symbol)) Nil
            else targs
          (fun, targs, targsInSource)

        case Select(New(tpt @ TypeTree()), _) if isConstr =>
          val targsInSource = tpt.tpe match {
            case TypeRef(pre, sym, args)
            if (!args.forall(a => context.undetparams contains a.typeSymbol)) =>
              args.map(TypeTree(_))
            case _ => Nil
          }
          (baseFun, Nil, targsInSource)

        case _ => (baseFun, Nil, Nil)
      }

      def blockWithQualifier(qual: Tree, fun: Symbol => Tree, defaultQual: Symbol => Option[Tree]) = {
        val sym = blockTyper.context.owner.newValue(baseFun1.pos,
                                                    unit.fresh.newName(baseFun1.pos, "qual$"))
                            .setInfo(qual.tpe)
        blockTyper.context.scope.enter(sym)
        val vd = atPos(sym.pos) { ValDef(sym, qual).setType(NoType) }
        var baseFunTransformed: Tree = fun(sym)
        if (!funTargs.isEmpty)
          baseFunTransformed = treeCopy.TypeApply(baseFun, baseFunTransformed, funTargs)
        val b = atPos(baseFun1.pos) { Block(List(vd), baseFunTransformed)
                                      .setType(baseFunTransformed.tpe) }
        context.namedApplyBlockInfo =
          Some((b, NamedApplyInfo(defaultQual(sym), defaultTargs, Nil, blockTyper)))
        b
      }

      def blockWithoutQualifier(fun: Tree, defaultQual: Option[Tree]) = {
       val fun1 = if (funTargs.isEmpty) fun
                  else treeCopy.TypeApply(baseFun, fun, funTargs)
        val b = atPos(baseFun.pos) { Block(Nil, fun1).setType(fun1.tpe) }
        context.namedApplyBlockInfo =
          Some((b, NamedApplyInfo(defaultQual, defaultTargs, Nil, blockTyper)))
        b
      }

      baseFun1 match {
        // constructor calls

        case Select(New(TypeTree()), _) if isConstr =>
          blockWithoutQualifier(baseFun1, None)

        case Select(New(Ident(_)), _) if isConstr =>
          blockWithoutQualifier(baseFun1, None)

        case Select(nev @ New(sel @ Select(qual, typeName)), constr) if isConstr =>
          // #2057
          val module = baseFun.symbol.owner.linkedModuleOfClass
          val defaultQual = if (module == NoSymbol) None
                            else Some(gen.mkAttributedSelect(qual.duplicate, module))
          // in `new q.C()', q is always stable
          assert(treeInfo.isPureExpr(qual), qual)
          blockWithoutQualifier(baseFun1, defaultQual)

        // super constructor calls

        case Select(Super(_, _), _) if isConstr =>
          blockWithoutQualifier(baseFun1, None)

        // self constructor calls (in secondary constructors)

        case Select(qual, name) if isConstr =>
          assert(treeInfo.isPureExpr(qual), qual)
          blockWithoutQualifier(baseFun1, None)

        // other method calls

        case Ident(_) =>
          blockWithoutQualifier(baseFun1, None)

        case Select(qual, name) =>
          if (treeInfo.isPureExpr(qual))
            blockWithoutQualifier(baseFun1, Some(qual.duplicate))
          else
            blockWithQualifier(qual,
                               sym => treeCopy.Select(baseFun1, gen.mkAttributedRef(sym), name),
                               sym => Some(gen.mkAttributedRef(sym)))
      }
    }

    /**
     * For each argument (arg: T), create a local value
     *  x$n: T = arg
     *
     * assumes "args" are typed. owner of the definitions in the block is the owner of
     * the block (see typedBlock), but the symbols have to be entered into the block's scope.
     *
     * For by-name parameters, create a value
     *  x$n: () => T = () => arg
     */
    def argValDefs(args: List[Tree], paramTypes: List[Type], blockTyper: Typer): List[ValDef] = {
      val context = blockTyper.context
      val symPs = List.map2(args, paramTypes)((arg, tpe) => {
        val byName = tpe.typeSymbol == ByNameParamClass
        val s = context.owner.newValue(arg.pos, unit.fresh.newName(arg.pos, "x$"))
        val valType = if (byName) functionType(List(), arg.tpe)
                  else arg.tpe
        s.setInfo(valType)
        (context.scope.enter(s), byName)
      })
      List.map2(symPs, args)((symP, arg) => {
        val (sym, byName) = symP
        val body = if (byName) blockTyper.typed(Function(List(), arg))
                   else arg
        ValDef(sym, body).setType(NoType)
      })
    }

    // begin transform
    if (isNamedApplyBlock(tree)) {
      context.namedApplyBlockInfo.get._1
    } else tree match {
      // we know that Apply(Apply(...)) can only be an application of a curried method;
      // for functions, it's transformed to applying the .apply() method already.
      case Apply(fun, namelessArgs) =>
        val transformedFun = transformNamedApplication(typer, mode, pt)(fun, x => x)
        if (transformedFun.isErroneous) setError(tree)
        else {
          assert(isNamedApplyBlock(transformedFun), transformedFun)
          val NamedApplyInfo(qual, targs, vargss, blockTyper) =
            context.namedApplyBlockInfo.get._2
          val existingBlock @ Block(stats, funOnly) = transformedFun

          // type the application without names; put the arguments in definition-site order
          val typedApp = doTypedApply(tree, funOnly, reorderArgs(namelessArgs, argPos), mode, pt)

          if (typedApp.tpe.isError) setError(tree)
          else typedApp match {
            // Extract the typed arguments, restore the call-site evaluation order (using
            // ValDef's in the block), change the arguments to these local values.
            case Apply(expr, typedArgs) =>
              // typedArgs: definition-site order
              val formals = formalTypes(expr.tpe.paramTypes, typedArgs.length, false)
              // valDefs: call-site order
              val valDefs = argValDefs(reorderArgsInv(typedArgs, argPos),
                                       reorderArgsInv(formals, argPos),
                                       blockTyper)
              // refArgs: definition-site order again
              val refArgs = List.map2(reorderArgs(valDefs, argPos), formals)((vDef, tpe) => {
                val ref = gen.mkAttributedRef(vDef.symbol)
                // for by-name parameters, the local value is a nullary function returning the argument
                if (tpe.typeSymbol == ByNameParamClass) Apply(ref, List())
                else ref
              })
              // cannot call blockTyper.typedBlock here, because the method expr might be partially applied only
              val res = blockTyper.doTypedApply(tree, expr, refArgs, mode, pt)
              val block = treeCopy.Block(existingBlock, stats ::: valDefs, res).setType(res.tpe)
              context.namedApplyBlockInfo =
                Some((block, NamedApplyInfo(qual, targs, vargss ::: List(refArgs), blockTyper)))
              block
          }
        }

      // case ApplyDynamic??? case AppliedTypeTree???

      case baseFun => // also treats "case TypeApply(fun, targs)" and "case Select(New(..), <init>)"
        baseFunBlock(baseFun)

    }
  }

  def missingParams[T](args: List[T], params: List[Symbol], argName: T => Option[Name] = nameOf _): (List[Symbol], Boolean) = {
    val namedArgs = args.dropWhile(arg => {
      val n = argName(arg)
      n.isEmpty || params.forall(p => p.name != n.get)
    })
    val namedParams = params.drop(args.length - namedArgs.length)
    // missing: keep those with a name which doesn't exist in namedArgs
    val missingParams = namedParams.filter(p => namedArgs.forall(arg => {
      val n = argName(arg)
      n.isEmpty || n.get != p.name
    }))
    val allPositional = missingParams.length == namedParams.length
    (missingParams, allPositional)
  }

  /**
   * Extend the argument list `givenArgs' with default arguments. Defaults are added
   * as named arguments calling the corresponding default getter.
   *
   * Example: given
   *   def foo(x: Int = 2, y: String = "def")
   *   foo(1)
   * the argument list (y = "lt") is transformed to (y = "lt", x = foo$default$1())
   */
  def addDefaults(givenArgs: List[Tree], qual: Option[Tree], targs: List[Tree],
                  previousArgss: List[List[Tree]], params: List[Symbol], pos: util.Position): (List[Tree], List[Symbol]) = {
    if (givenArgs.length < params.length) {
      val (missing, positional) = missingParams(givenArgs, params)
      if (missing forall (_.hasFlag(DEFAULTPARAM))) {
        val defaultArgs = missing map (p => {
          var default1 = qual match {
            case Some(q) => gen.mkAttributedSelect(q.duplicate, p.defaultGetter)
            case None => gen.mkAttributedRef(p.defaultGetter)
          }
          default1 = if (targs.isEmpty) default1
                     else TypeApply(default1, targs.map(_.duplicate)).setPos(pos)
          val default2 = (default1 /: previousArgss)((tree, args) =>
            Apply(tree, args.map(_.duplicate)).setPos(pos))
          if (positional) default2
          else atPos(pos) { new AssignOrNamedArg(Ident(p.name), default2) }
        })
        (givenArgs ::: defaultArgs, Nil)
      } else (givenArgs, missing filter (! _.hasFlag(DEFAULTPARAM)))
    } else (givenArgs, Nil)
  }

  /**
   * Removes name assignments from args. Additionally, returns an array mapping
   * argument indicies from call-site-order to definition-site-order.
   *
   * Verifies that names are not specified twice, positional args don't appear
   * after named ones.
   */
  def removeNames(typer: Typer)(args: List[Tree], params: List[Symbol]): (List[Tree], Array[Int]) = {
    import typer.infer.errorTree

    // maps indicies from (order written by user) to (order of definition)
    val argPos = (new Array[Int](args.length)) map (x => -1)
    var positionalAllowed = true
    val namelessArgs = for ((arg, index) <- (args.zipWithIndex)) yield arg match {
      case a @ Assign(Ident(name), rhs) if a.namedArg =>
        val pos = params.indexWhere(p => p.name == name && !p.hasFlag(SYNTHETIC))
        if (pos == -1) {
          if (positionalAllowed) {
            argPos(index) = index
            // prevent isNamed from being true when calling doTypedApply recursively,
            // treat the arg as an assignment of type Unit
            Assign(a.lhs, rhs).setPos(arg.pos)
          } else {
            errorTree(arg, "unknown parameter name: "+ name)
          }
        } else if (argPos contains pos) {
          errorTree(arg, "parameter specified twice: "+ name)
        } else {
          // for named arguments, check wether the assignment expression would
          // typecheck. if it does, report an ambiguous error.
          val param = params(pos)
          val paramtpe = params(pos).tpe.cloneInfo(param)
          // replace type parameters by wildcard. in the below example we need to
          // typecheck (x = 1) with wildcard (not T) so that it succeeds.
          //   def f[T](x: T) = x
          //   var x = 0
          //   f(x = 1)   <<  "x = 1" typechecks with expected type WildcardType
          val udp = typer.context.extractUndetparams()
          val subst = new SubstTypeMap(udp, udp map (_ => WildcardType)) {
            override def apply(tp: Type): Type = tp match {
              case TypeRef(_, sym, List(arg)) if (sym == ByNameParamClass) => super.apply(arg)
              case _ => super.apply(tp)
            }
          }
          val res = typer.silent(_.typed(arg, subst(paramtpe))) match {
            case _: TypeError =>
              positionalAllowed = false
              argPos(index) = pos
              rhs
            case t: Tree =>
              errorTree(arg, "reference to "+ name +" is ambiguous; it is both, a parameter\n"+
                             "name of the method and the name of a variable currently in scope.")
          }
          typer.context.undetparams = udp
          res
        }
      case _ =>
        argPos(index) = index
        if (positionalAllowed) arg
        else errorTree(arg, "positional after named argument.")
    }
    (namelessArgs, argPos)
  }
}