Skip to content
This repository
Browse code

Renamed methods to be less ambiguous in intent.

isNamed    => isNamedArg
isIdentity => allArgsArePositional
nameOf     => nameOfNamedArg

Moved mkNamedArg into TreeGen.
  • Loading branch information...
commit 5878099cbb3ca0b0224c9931d3510c7234e7c686 1 parent e626ecd
Paul Phillips authored January 14, 2013
11  src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
@@ -1584,14 +1584,9 @@ self =>
1584 1584
      *  }}}
1585 1585
      */
1586 1586
     def argumentExprs(): List[Tree] = {
1587  
-      def args(): List[Tree] = commaSeparated {
1588  
-        val maybeNamed = isIdent
1589  
-        expr() match {
1590  
-          case a @ Assign(id, rhs) if maybeNamed =>
1591  
-            atPos(a.pos) { AssignOrNamedArg(id, rhs) }
1592  
-          case e => e
1593  
-        }
1594  
-      }
  1587
+      def args(): List[Tree] = commaSeparated(
  1588
+        if (isIdent) treeInfo.assignmentToMaybeNamedArg(expr()) else expr()
  1589
+      )
1595 1590
       in.token match {
1596 1591
         case LBRACE   => List(blockExpr())
1597 1592
         case LPAREN   => inParens(if (in.token == RPAREN) Nil else args())
9  src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala
@@ -171,15 +171,10 @@ abstract class TreeBuilder {
171 171
 
172 172
   /** Create tree representing (unencoded) binary operation expression or pattern. */
173 173
   def makeBinop(isExpr: Boolean, left: Tree, op: TermName, right: Tree, opPos: Position): Tree = {
174  
-    def mkNamed(args: List[Tree]) =
175  
-      if (isExpr) args map {
176  
-        case a @ Assign(id @ Ident(name), rhs) =>
177  
-          atPos(a.pos) { AssignOrNamedArg(id, rhs) }
178  
-        case e => e
179  
-      } else args
  174
+    def mkNamed(args: List[Tree]) = if (isExpr) args map treeInfo.assignmentToMaybeNamedArg else args
180 175
     val arguments = right match {
181 176
       case Parens(args) => mkNamed(args)
182  
-      case _ => List(right)
  177
+      case _            => List(right)
183 178
     }
184 179
     if (isExpr) {
185 180
       if (treeInfo.isLeftAssoc(op)) {
2  src/compiler/scala/tools/nsc/typechecker/Infer.scala
@@ -866,7 +866,7 @@ trait Infer extends Checkable {
866 866
               val (argtpes1, argPos, namesOK) = checkNames(argtpes0, params)
867 867
               // when using named application, the vararg param has to be specified exactly once
868 868
               (    namesOK
869  
-                && (isIdentity(argPos) || sameLength(formals, params))
  869
+                && (allArgsArePositional(argPos) || sameLength(formals, params))
870 870
                 && typesCompatible(reorderArgs(argtpes1, argPos)) // nb. arguments and names are OK, check if types are compatible
871 871
               )
872 872
             }
14  src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala
@@ -41,11 +41,11 @@ trait NamesDefaults { self: Analyzer =>
41 41
     blockTyper: Typer
42 42
   ) { }
43 43
 
44  
-  def nameOf(arg: Tree) = arg match {
45  
-    case AssignOrNamedArg(Ident(name), rhs) => Some(name)
46  
-    case _ => None
  44
+  private def nameOfNamedArg(arg: Tree) = Some(arg) collect { case AssignOrNamedArg(Ident(name), _) => name }
  45
+  def isNamedArg(arg: Tree) = arg match {
  46
+    case AssignOrNamedArg(Ident(_), _) => true
  47
+    case _                             => false
47 48
   }
48  
-  def isNamed(arg: Tree) = nameOf(arg).isDefined
49 49
 
50 50
   /** @param pos maps indices from old to new */
51 51
   def reorderArgs[T: ClassTag](args: List[T], pos: Int => Int): List[T] = {
@@ -55,13 +55,13 @@ trait NamesDefaults { self: Analyzer =>
55 55
   }
56 56
 
57 57
   /** @param pos maps indices from new to old (!) */
58  
-  def reorderArgsInv[T: ClassTag](args: List[T], pos: Int => Int): List[T] = {
  58
+  private def reorderArgsInv[T: ClassTag](args: List[T], pos: Int => Int): List[T] = {
59 59
     val argsArray = args.toArray
60 60
     (argsArray.indices map (i => argsArray(pos(i)))).toList
61 61
   }
62 62
 
63 63
   /** returns `true` if every element is equal to its index */
64  
-  def isIdentity(a: Array[Int]) = (0 until a.length).forall(i => a(i) == i)
  64
+  def allArgsArePositional(a: Array[Int]) = (0 until a.length).forall(i => a(i) == i)
65 65
 
66 66
   /**
67 67
    * Transform a function application into a Block, and assigns typer.context
@@ -359,7 +359,7 @@ trait NamesDefaults { self: Analyzer =>
359 359
     }
360 360
   }
361 361
 
362  
-  def missingParams[T](args: List[T], params: List[Symbol], argName: T => Option[Name] = nameOf _): (List[Symbol], Boolean) = {
  362
+  def missingParams[T](args: List[T], params: List[Symbol], argName: T => Option[Name] = nameOfNamedArg _): (List[Symbol], Boolean) = {
363 363
     val namedArgs = args.dropWhile(arg => {
364 364
       val n = argName(arg)
365 365
       n.isEmpty || params.forall(p => p.name != n.get)
32  src/compiler/scala/tools/nsc/typechecker/Typers.scala
@@ -117,10 +117,6 @@ trait Typers extends Adaptations with Tags {
117 117
       }
118 118
     }
119 119
 
120  
-    private def mkNamedArg(tree: Tree, name: Name): Tree = {
121  
-      atPos(tree.pos)(new AssignOrNamedArg(Ident(name), tree))
122  
-    }
123  
-
124 120
     /** Find implicit arguments and pass them to given tree.
125 121
      */
126 122
     def applyImplicitArgs(fun: Tree): Tree = fun.tpe match {
@@ -130,9 +126,7 @@ trait Typers extends Adaptations with Tags {
130 126
         // paramFailed cannot be initialized with params.exists(_.tpe.isError) because that would
131 127
         // hide some valid errors for params preceding the erroneous one.
132 128
         var paramFailed = false
133  
-
134  
-        def mkPositionalArg(argTree: Tree, paramName: Name) = argTree
135  
-        var mkArg: (Tree, Name) => Tree = mkPositionalArg
  129
+        var mkArg: (Name, Tree) => Tree = (_, tree) => tree
136 130
 
137 131
         // DEPMETTODO: instantiate type vars that depend on earlier implicit args (see adapt (4.1))
138 132
         //
@@ -147,9 +141,9 @@ trait Typers extends Adaptations with Tags {
147 141
           argResultsBuff += res
148 142
 
149 143
           if (res.isSuccess) {
150  
-            argBuff += mkArg(res.tree, param.name)
  144
+            argBuff += mkArg(param.name, res.tree)
151 145
           } else {
152  
-            mkArg = mkNamedArg // don't pass the default argument (if any) here, but start emitting named arguments for the following args
  146
+            mkArg = gen.mkNamedArg // don't pass the default argument (if any) here, but start emitting named arguments for the following args
153 147
             if (!param.hasDefault && !paramFailed) {
154 148
               context.errBuffer.find(_.kind == ErrorKinds.Divergent) match {
155 149
                 case Some(divergentImplicit) =>
@@ -3104,10 +3098,10 @@ trait Typers extends Adaptations with Tags {
3104 3098
               val (namelessArgs, argPos) = removeNames(Typer.this)(args, params)
3105 3099
               if (namelessArgs exists (_.isErroneous)) {
3106 3100
                 duplErrTree
3107  
-              } else if (!isIdentity(argPos) && !sameLength(formals, params))
3108  
-                // !isIdentity indicates that named arguments are used to re-order arguments
  3101
+              } else if (!allArgsArePositional(argPos) && !sameLength(formals, params))
  3102
+                // !allArgsArePositional indicates that named arguments are used to re-order arguments
3109 3103
                 duplErrorTree(MultipleVarargError(tree))
3110  
-              else if (isIdentity(argPos) && !isNamedApplyBlock(fun)) {
  3104
+              else if (allArgsArePositional(argPos) && !isNamedApplyBlock(fun)) {
3111 3105
                 // if there's no re-ordering, and fun is not transformed, no need to transform
3112 3106
                 // more than an optimization, e.g. important in "synchronized { x = update-x }"
3113 3107
                 checkNotMacro()
@@ -3157,7 +3151,7 @@ trait Typers extends Adaptations with Tags {
3157 3151
           }
3158 3152
 
3159 3153
           if (!sameLength(formals, args) ||   // wrong nb of arguments
3160  
-              (args exists isNamed) ||        // uses a named argument
  3154
+              (args exists isNamedArg) ||     // uses a named argument
3161 3155
               isNamedApplyBlock(fun)) {       // fun was transformed to a named apply block =>
3162 3156
                                               // integrate this application into the block
3163 3157
             if (dyna.isApplyDynamicNamed(fun)) dyna.typedNamedApply(tree, fun, args, mode, pt)
@@ -3464,7 +3458,7 @@ trait Typers extends Adaptations with Tags {
3464 3458
       }
3465 3459
 
3466 3460
       // begin typedAnnotation
3467  
-      val treeInfo.Applied(fun0, targs, argss) = treeInfo.dissectApplied(ann)
  3461
+      val treeInfo.Applied(fun0, targs, argss) = ann
3468 3462
       val typedFun0 = typed(fun0, mode.forFunMode, WildcardType)
3469 3463
       val typedFunPart = (
3470 3464
         // If there are dummy type arguments in typeFun part, it suggests we
@@ -3475,7 +3469,7 @@ trait Typers extends Adaptations with Tags {
3475 3469
         else
3476 3470
           typedFun0
3477 3471
       )
3478  
-      val typedFun @ Select(New(annTpt), _) = treeInfo.dissectApplied(typedFunPart).core
  3472
+      val treeInfo.Applied(typedFun @ Select(New(annTpt), _), _, _) = typedFunPart
3479 3473
       val annType = annTpt.tpe
3480 3474
 
3481 3475
       val res = if (typedFun.isErroneous) ErroneousAnnotation
@@ -3489,15 +3483,15 @@ trait Typers extends Adaptations with Tags {
3489 3483
           } else if (argss.length > 1) {
3490 3484
             reportAnnotationError(MultipleArgumentListForAnnotationError(ann))
3491 3485
           } else {
  3486
+            val args = argss match {
  3487
+              case (arg :: Nil) :: Nil if !isNamedArg(arg) => gen.mkNamedArg(nme.value, arg) :: Nil
  3488
+              case args :: Nil                             => args
  3489
+            }
3492 3490
             val annScope = annType.decls
3493 3491
                 .filter(sym => sym.isMethod && !sym.isConstructor && sym.isJavaDefined)
3494 3492
             val names = new scala.collection.mutable.HashSet[Symbol]
3495 3493
             names ++= (if (isJava) annScope.iterator
3496 3494
                        else typedFun.tpe.params.iterator)
3497  
-            val args = argss match {
3498  
-              case (arg :: Nil) :: Nil if !isNamed(arg) => mkNamedArg(arg, nme.value) :: Nil
3499  
-              case args :: Nil                          => args
3500  
-            }
3501 3495
 
3502 3496
             val nvPairs = args map {
3503 3497
               case arg @ AssignOrNamedArg(Ident(name), rhs) =>
4  src/reflect/scala/reflect/internal/TreeGen.scala
@@ -271,6 +271,10 @@ abstract class TreeGen extends macros.TreeBuilder {
271 271
     case _            => Constant(null)
272 272
   }
273 273
 
  274
+  /** Wrap an expression in a named argument. */
  275
+  def mkNamedArg(name: Name, tree: Tree): Tree = mkNamedArg(Ident(name), tree)
  276
+  def mkNamedArg(lhs: Tree, rhs: Tree): Tree = atPos(rhs.pos)(AssignOrNamedArg(lhs, rhs))
  277
+
274 278
   /** Builds a tuple */
275 279
   def mkTuple(elems: List[Tree]): Tree =
276 280
     if (elems.isEmpty) Literal(Constant())
8  src/reflect/scala/reflect/internal/TreeInfo.scala
@@ -370,6 +370,14 @@ abstract class TreeInfo {
370 370
     case _                                                          => false
371 371
   }
372 372
 
  373
+  /** Translates an Assign(_, _) node to AssignOrNamedArg(_, _) if
  374
+   *  the lhs is a simple ident. Otherwise returns unchanged.
  375
+   */
  376
+  def assignmentToMaybeNamedArg(tree: Tree) = tree match {
  377
+    case t @ Assign(id: Ident, rhs) => atPos(t.pos)(AssignOrNamedArg(id, rhs))
  378
+    case t                          => t
  379
+  }
  380
+
373 381
   /** Is name a left-associative operator? */
374 382
   def isLeftAssoc(operator: Name) = operator.nonEmpty && (operator.endChar != ':')
375 383
 

0 notes on commit 5878099

Please sign in to comment.
Something went wrong with that request. Please try again.