diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2013-09-24 05:57:42 -0700 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2013-09-24 05:57:42 -0700 |
commit | c9fdcec4577932d001a865a7f6606c6e59f58cd3 (patch) | |
tree | e98628afaa1086c3cb8c99af3921f19b31fb5f1b /src/reflect | |
parent | c2e3b0656bb449e95a529e266294aeabb9cbef3f (diff) | |
parent | efd64ae594e286d2141d2cb99ec25d97de60a414 (diff) | |
download | scala-c9fdcec4577932d001a865a7f6606c6e59f58cd3.tar.gz scala-c9fdcec4577932d001a865a7f6606c6e59f58cd3.tar.bz2 scala-c9fdcec4577932d001a865a7f6606c6e59f58cd3.zip |
Merge pull request #2980 from huitseeker/revert-2957
Revert #2957
Diffstat (limited to 'src/reflect')
-rw-r--r-- | src/reflect/scala/reflect/internal/TreeGen.scala | 67 | ||||
-rw-r--r-- | src/reflect/scala/reflect/internal/TreeInfo.scala | 7 | ||||
-rw-r--r-- | src/reflect/scala/reflect/internal/util/Position.scala | 5 |
3 files changed, 41 insertions, 38 deletions
diff --git a/src/reflect/scala/reflect/internal/TreeGen.scala b/src/reflect/scala/reflect/internal/TreeGen.scala index a9f2807e9f..c8ae4df13d 100644 --- a/src/reflect/scala/reflect/internal/TreeGen.scala +++ b/src/reflect/scala/reflect/internal/TreeGen.scala @@ -280,7 +280,7 @@ abstract class TreeGen extends macros.TreeBuilder { /** Builds a tuple */ def mkTuple(elems: List[Tree]): Tree = - if (elems.isEmpty) mkUnit() + if (elems.isEmpty) Literal(Constant(())) else Apply( Select(mkAttributedRef(TupleClass(elems.length).caseModule), nme.apply), elems) @@ -326,7 +326,8 @@ abstract class TreeGen extends macros.TreeBuilder { * body * } */ - def mkTemplate(parents: List[Tree], self: ValDef, constrMods: Modifiers, vparamss: List[List[ValDef]], body: List[Tree], superPos: Position = NoPosition): Template = { + def mkTemplate(parents: List[Tree], self: ValDef, constrMods: Modifiers, + vparamss: List[List[ValDef]], body: List[Tree], superPos: Position = NoPosition): Template = { /* Add constructor to template */ // create parameters for <init> as synthetic trees. @@ -397,42 +398,52 @@ abstract class TreeGen extends macros.TreeBuilder { * @param npos the position of the new * @param cpos the position of the anonymous class starting with parents */ - def mkNew(parents: List[Tree], self: ValDef, stats: List[Tree], npos: Position, cpos: Position): Tree = { - def enclosingPos = wrappingPos(cpos, parents ::: List(self) ::: stats) - def upos = cpos union npos - def anonTemplate = atPos(cpos)(mkTemplate(parents, self, NoMods, ListOfNil, stats, cpos)) - def anonClass = atPos(anonTemplate.pos.makeTransparent)(ClassDef(Modifiers(FINAL), tpnme.ANON_CLASS_NAME, Nil, anonTemplate)) - def anonNew = atPos(npos)(New(Ident(tpnme.ANON_CLASS_NAME) setPos cpos.focus, Nil)) - - // `Parsers.template` no longer differentiates tpts and their argss - // e.g. `C()` will be represented as a single tree Apply(Ident(C), Nil) - // instead of parents = Ident(C), argss = Nil as before - // this change works great for things that are actually templates - // but in this degenerate case we need to perform postprocessing - parents match { - case Nil => mkNew(List(scalaAnyRefConstr), self, stats, npos, cpos) - case treeInfo.AppliedArgs(callee, argss) :: Nil if stats.isEmpty => atPos(upos)(New(callee, argss)) - case _ => atPos(upos)(mkBlock(anonClass :: anonNew :: Nil)) + def mkNew(parents: List[Tree], self: ValDef, stats: List[Tree], + npos: Position, cpos: Position): Tree = + if (parents.isEmpty) + mkNew(List(scalaAnyRefConstr), self, stats, npos, cpos) + else if (parents.tail.isEmpty && stats.isEmpty) { + // `Parsers.template` no longer differentiates tpts and their argss + // e.g. `C()` will be represented as a single tree Apply(Ident(C), Nil) + // instead of parents = Ident(C), argss = Nil as before + // this change works great for things that are actually templates + // but in this degenerate case we need to perform postprocessing + val app = treeInfo.dissectApplied(parents.head) + atPos(npos union cpos) { New(app.callee, app.argss) } + } else { + val x = tpnme.ANON_CLASS_NAME + atPos(npos union cpos) { + Block( + List( + atPos(cpos) { + ClassDef( + Modifiers(FINAL), x, Nil, + mkTemplate(parents, self, NoMods, ListOfNil, stats, cpos.focus)) + }), + atPos(npos) { + New( + Ident(x) setPos npos.focus, + Nil) + } + ) + } } - } /** Create a tree representing the function type (argtpes) => restpe */ def mkFunctionTypeTree(argtpes: List[Tree], restpe: Tree): Tree = AppliedTypeTree(rootScalaDot(newTypeName("Function" + argtpes.length)), argtpes ::: List(restpe)) - def mkUnit() = Literal(Constant(())) - /** Create block of statements `stats` */ - def mkBlock(stats: List[Tree]): Tree = stats match { - case stats if stats.isEmpty || !stats.last.isTerm => mkBlock(stats :+ mkUnit()) - case stat :: Nil => stat - case stats => Block(stats.init, stats.last) - } + def mkBlock(stats: List[Tree]): Tree = + if (stats.isEmpty) Literal(Constant(())) + else if (!stats.last.isTerm) Block(stats, Literal(Constant(()))) + else if (stats.length == 1) stats.head + else Block(stats.init, stats.last) def mkTreeOrBlock(stats: List[Tree]) = stats match { - case Nil => EmptyTree + case Nil => EmptyTree case head :: Nil => head - case _ => gen.mkBlock(stats) + case _ => gen.mkBlock(stats) } /** Create a tree representing an assignment <lhs = rhs> */ diff --git a/src/reflect/scala/reflect/internal/TreeInfo.scala b/src/reflect/scala/reflect/internal/TreeInfo.scala index ab7506e657..2f70b1950d 100644 --- a/src/reflect/scala/reflect/internal/TreeInfo.scala +++ b/src/reflect/scala/reflect/internal/TreeInfo.scala @@ -773,13 +773,6 @@ abstract class TreeInfo { unapply(dissectApplied(tree)) } - object AppliedArgs { - def unapply(tree: Tree): Some[(Tree, List[List[Tree]])] = tree match { - case Apply(AppliedArgs(fn, argss), args) => Some((fn, argss :+ args)) - case _ => Some((tree, Nil)) - } - } - /** Locates the synthetic Apply node corresponding to an extractor's call to * unapply (unwrapping nested Applies) and returns the fun part of that Apply. */ diff --git a/src/reflect/scala/reflect/internal/util/Position.scala b/src/reflect/scala/reflect/internal/util/Position.scala index d62ab40a9d..8e7df28167 100644 --- a/src/reflect/scala/reflect/internal/util/Position.scala +++ b/src/reflect/scala/reflect/internal/util/Position.scala @@ -197,11 +197,10 @@ private[util] trait InternalPositionImpl { } def showDebug: String = toString def show = ( - if (isOpaqueRange && start != point) s"[$point/$start:$end]" - else if (isOpaqueRange) s"[$start:$end]" + if (isOpaqueRange) s"[$start:$end]" else if (isTransparent) s"<$start:$end>" else if (isDefined) s"[$point]" - else "[X]" + else "[NoPosition]" ) private def asOffset(point: Int): Position = Position.offset(source, point) |