diff options
author | Paul Phillips <paulp@improving.org> | 2011-07-11 20:40:05 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-07-11 20:40:05 +0000 |
commit | e3085dadb3cbb1c416779ddec9d57f9467887a5a (patch) | |
tree | a550aac590d844ac5d101c127faafdcbc5f2fbc0 /src | |
parent | 4977341da76a9ac2bf329ef6f6eba4d824430e97 (diff) | |
download | scala-e3085dadb3cbb1c416779ddec9d57f9467887a5a.tar.gz scala-e3085dadb3cbb1c416779ddec9d57f9467887a5a.tar.bz2 scala-e3085dadb3cbb1c416779ddec9d57f9467887a5a.zip |
Misc smoothing of tree creation, no review.
Diffstat (limited to 'src')
5 files changed, 21 insertions, 31 deletions
diff --git a/src/compiler/scala/reflect/internal/Definitions.scala b/src/compiler/scala/reflect/internal/Definitions.scala index 6d9f8c28e9..71eb471992 100644 --- a/src/compiler/scala/reflect/internal/Definitions.scala +++ b/src/compiler/scala/reflect/internal/Definitions.scala @@ -100,6 +100,7 @@ trait Definitions extends reflect.api.StandardDefinitions { lazy val BooleanClass = valueCache(tpnme.Boolean) def Boolean_and = getMember(BooleanClass, nme.ZAND) def Boolean_or = getMember(BooleanClass, nme.ZOR) + def Boolean_not = getMember(BooleanClass, nme.UNARY_!) def ScalaValueClassesNoUnit = ScalaValueClasses filterNot (_ eq UnitClass) def ScalaValueClasses: List[Symbol] = List( @@ -361,6 +362,7 @@ trait Definitions extends reflect.api.StandardDefinitions { lazy val OptionClass: Symbol = getClass("scala.Option") lazy val SomeClass: Symbol = getClass("scala.Some") lazy val NoneModule: Symbol = getModule("scala.None") + lazy val SomeModule: Symbol = getModule("scala.Some") def isOptionType(tp: Type) = cond(tp.normalize) { case TypeRef(_, OptionClass, List(_)) => true } def isSomeType(tp: Type) = cond(tp.normalize) { case TypeRef(_, SomeClass, List(_)) => true } diff --git a/src/compiler/scala/reflect/internal/Trees.scala b/src/compiler/scala/reflect/internal/Trees.scala index d9b9e10601..70d28d8402 100644 --- a/src/compiler/scala/reflect/internal/Trees.scala +++ b/src/compiler/scala/reflect/internal/Trees.scala @@ -209,6 +209,11 @@ trait Trees extends api.Trees { self: SymbolTable => val superRef: Tree = Select(New(tpt), nme.CONSTRUCTOR) (superRef /: argss) (Apply) } + /** 0-1 argument list new, based on a symbol. + */ + def New(sym: Symbol, args: Tree*): Tree = + if (args.isEmpty) New(TypeTree(sym.tpe)) + else New(TypeTree(sym.tpe), List(args.toList)) def Apply(sym: Symbol, args: Tree*): Tree = Apply(Ident(sym), args.toList) diff --git a/src/compiler/scala/tools/nsc/ast/TreeDSL.scala b/src/compiler/scala/tools/nsc/ast/TreeDSL.scala index 6c35514110..60e761048b 100644 --- a/src/compiler/scala/tools/nsc/ast/TreeDSL.scala +++ b/src/compiler/scala/tools/nsc/ast/TreeDSL.scala @@ -27,12 +27,6 @@ trait TreeDSL { def nullSafe[T](f: Tree => Tree, ifNull: Tree): Tree => Tree = tree => IF (tree MEMBER_== NULL) THEN ifNull ELSE f(tree) - // strip bindings to find what lies beneath - final def unbind(x: Tree): Tree = x match { - case Bind(_, y) => unbind(y) - case y => y - } - def returning[T](x: T)(f: T => Unit): T = util.returning(x)(f) object LIT extends (Any => Literal) { @@ -51,12 +45,9 @@ trait TreeDSL { def UNIT = LIT(()) object WILD { - def apply(tpe: Type = null) = - if (tpe == null) Ident(nme.WILDCARD) - else Ident(nme.WILDCARD) setType tpe - - def unapply(other: Any) = - cond(other) { case Ident(nme.WILDCARD) => true } + def empty = Ident(nme.WILDCARD) + def apply(tpe: Type) = Ident(nme.WILDCARD) setType tpe + def unapply(other: Any) = cond(other) { case Ident(nme.WILDCARD) => true } } def fn(lhs: Tree, op: Name, args: Tree*) = Apply(Select(lhs, op), args.toList) @@ -99,8 +90,8 @@ trait TreeDSL { def INT_== (other: Tree) = fn(target, getMember(IntClass, nme.EQ), other) def INT_!= (other: Tree) = fn(target, getMember(IntClass, nme.NE), other) - def BOOL_&& (other: Tree) = fn(target, getMember(BooleanClass, nme.ZAND), other) - def BOOL_|| (other: Tree) = fn(target, getMember(BooleanClass, nme.ZOR), other) + def BOOL_&& (other: Tree) = fn(target, Boolean_and, other) + def BOOL_|| (other: Tree) = fn(target, Boolean_or, other) /** Apply, Select, Match **/ def APPLY(params: Tree*) = Apply(target, params.toList) @@ -249,7 +240,7 @@ trait TreeDSL { } def CASE(pat: Tree): CaseStart = new CaseStart(pat, EmptyTree) - def DEFAULT: CaseStart = new CaseStart(WILD(), EmptyTree) + def DEFAULT: CaseStart = new CaseStart(WILD.empty, EmptyTree) class SymbolMethods(target: Symbol) { def BIND(body: Tree) = Bind(target, body) @@ -270,10 +261,8 @@ trait TreeDSL { def THROW(sym: Symbol): Throw = Throw(New(TypeTree(sym.tpe), List(Nil))) def THROW(sym: Symbol, msg: Tree): Throw = Throw(New(TypeTree(sym.tpe), List(List(msg.TOSTRING())))) - def NEW(tpe: Tree, args: Tree*) = New(tpe, List(args.toList)) - def NEW(sym: Symbol, args: Tree*) = - if (args.isEmpty) New(TypeTree(sym.tpe)) - else New(TypeTree(sym.tpe), List(args.toList)) + def NEW(tpt: Tree, args: Tree*): Tree = New(tpt, List(args.toList)) + def NEW(sym: Symbol, args: Tree*): Tree = New(sym, args: _*) def DEF(name: Name, tp: Type): DefTreeStart = DEF(name) withType tp def DEF(name: Name): DefTreeStart = new DefTreeStart(name) @@ -302,8 +291,8 @@ trait TreeDSL { def IF(tree: Tree) = new IfStart(tree, EmptyTree) def TRY(tree: Tree) = new TryStart(tree, Nil, EmptyTree) def BLOCK(xs: Tree*) = Block(xs.init.toList, xs.last) - def NOT(tree: Tree) = Select(tree, getMember(BooleanClass, nme.UNARY_!)) - def SOME(xs: Tree*) = Apply(scalaDot(nme.Some), List(makeTupleTerm(xs.toList, true))) + def NOT(tree: Tree) = Select(tree, Boolean_not) + def SOME(xs: Tree*) = Apply(SomeModule, makeTupleTerm(xs.toList, true)) /** Typed trees from symbols. */ def THIS(sym: Symbol) = gen.mkAttributedThis(sym) @@ -311,21 +300,15 @@ trait TreeDSL { def REF(sym: Symbol) = gen.mkAttributedRef(sym) def REF(pre: Type, sym: Symbol) = gen.mkAttributedRef(pre, sym) - /** Some of this is basically verbatim from TreeBuilder, but we do not want - * to get involved with him because he's an untyped only sort. - */ - private def tupleName(count: Int, f: (String) => Name = newTermName(_: String)) = - scalaDot(f("Tuple" + count)) - def makeTupleTerm(trees: List[Tree], flattenUnary: Boolean): Tree = trees match { case Nil => UNIT case List(tree) if flattenUnary => tree - case _ => Apply(tupleName(trees.length), trees) + case _ => Apply(TupleClass(trees.length).companionModule, trees: _*) } def makeTupleType(trees: List[Tree], flattenUnary: Boolean): Tree = trees match { case Nil => gen.scalaUnitConstr case List(tree) if flattenUnary => tree - case _ => AppliedTypeTree(tupleName(trees.length, newTypeName), trees) + case _ => AppliedTypeTree(REF(TupleClass(trees.length)), trees) } /** Implicits - some of these should probably disappear **/ diff --git a/src/compiler/scala/tools/nsc/matching/MatchSupport.scala b/src/compiler/scala/tools/nsc/matching/MatchSupport.scala index 91e139381e..5e46960d04 100644 --- a/src/compiler/scala/tools/nsc/matching/MatchSupport.scala +++ b/src/compiler/scala/tools/nsc/matching/MatchSupport.scala @@ -57,7 +57,7 @@ trait MatchSupport extends ast.TreeDSL { self: ParallelMatching => def symbolToString(s: Symbol): String = s match { case x => x.toString } - def treeToString(t: Tree): String = unbind(t) match { + def treeToString(t: Tree): String = treeInfo.unbind(t) match { case EmptyTree => "?" case WILD() => "_" case Literal(Constant(x)) => "LIT(%s)".format(x) diff --git a/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala b/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala index 0917646f76..2da8992320 100644 --- a/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala +++ b/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala @@ -189,7 +189,7 @@ trait SyntheticMethods extends ast.TreeDSL { def makeTrees(acc: Symbol, cpt: Type): (Tree, Bind) = { val varName = context.unit.freshTermName(acc.name + "$") val isRepeated = isRepeatedParamType(cpt) - val binding = if (isRepeated) Star(WILD()) else WILD() + val binding = if (isRepeated) Star(WILD.empty) else WILD.empty val eqMethod: Tree = if (isRepeated) gen.mkRuntimeCall(nme.sameElements, List(Ident(varName), Ident(acc))) else (Ident(varName) DOT nme.EQ)(Ident(acc)) |