Misc smoothing of tree creation, no review.

git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@25267 5e8d7ff9-d8ef-0310-90f0-a4852d11357a
This commit is contained in:
extempore 2011-07-11 20:40:05 +00:00
parent 3190280d30
commit da85e3d5e5
5 changed files with 23 additions and 33 deletions

View File

@ -100,6 +100,7 @@ trait Definitions extends reflect.api.StandardDefinitions {
lazy val BooleanClass = valueCache(tpnme.Boolean) lazy val BooleanClass = valueCache(tpnme.Boolean)
def Boolean_and = getMember(BooleanClass, nme.ZAND) def Boolean_and = getMember(BooleanClass, nme.ZAND)
def Boolean_or = getMember(BooleanClass, nme.ZOR) def Boolean_or = getMember(BooleanClass, nme.ZOR)
def Boolean_not = getMember(BooleanClass, nme.UNARY_!)
def ScalaValueClassesNoUnit = ScalaValueClasses filterNot (_ eq UnitClass) def ScalaValueClassesNoUnit = ScalaValueClasses filterNot (_ eq UnitClass)
def ScalaValueClasses: List[Symbol] = List( def ScalaValueClasses: List[Symbol] = List(
@ -361,6 +362,7 @@ trait Definitions extends reflect.api.StandardDefinitions {
lazy val OptionClass: Symbol = getClass("scala.Option") lazy val OptionClass: Symbol = getClass("scala.Option")
lazy val SomeClass: Symbol = getClass("scala.Some") lazy val SomeClass: Symbol = getClass("scala.Some")
lazy val NoneModule: Symbol = getModule("scala.None") 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 isOptionType(tp: Type) = cond(tp.normalize) { case TypeRef(_, OptionClass, List(_)) => true }
def isSomeType(tp: Type) = cond(tp.normalize) { case TypeRef(_, SomeClass, List(_)) => true } def isSomeType(tp: Type) = cond(tp.normalize) { case TypeRef(_, SomeClass, List(_)) => true }

View File

@ -209,6 +209,11 @@ trait Trees extends api.Trees { self: SymbolTable =>
val superRef: Tree = Select(New(tpt), nme.CONSTRUCTOR) val superRef: Tree = Select(New(tpt), nme.CONSTRUCTOR)
(superRef /: argss) (Apply) (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 = def Apply(sym: Symbol, args: Tree*): Tree =
Apply(Ident(sym), args.toList) Apply(Ident(sym), args.toList)

View File

@ -26,12 +26,6 @@ trait TreeDSL {
// Add a null check to a Tree => Tree function // Add a null check to a Tree => Tree function
def nullSafe[T](f: Tree => Tree, ifNull: Tree): Tree => Tree = def nullSafe[T](f: Tree => Tree, ifNull: Tree): Tree => Tree =
tree => IF (tree MEMBER_== NULL) THEN ifNull ELSE f(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) def returning[T](x: T)(f: T => Unit): T = util.returning(x)(f)
@ -51,14 +45,11 @@ trait TreeDSL {
def UNIT = LIT(()) def UNIT = LIT(())
object WILD { object WILD {
def apply(tpe: Type = null) = def empty = Ident(nme.WILDCARD)
if (tpe == null) Ident(nme.WILDCARD) def apply(tpe: Type) = Ident(nme.WILDCARD) setType tpe
else Ident(nme.WILDCARD) setType tpe def unapply(other: Any) = cond(other) { case Ident(nme.WILDCARD) => true }
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) def fn(lhs: Tree, op: Name, args: Tree*) = Apply(Select(lhs, op), args.toList)
def fn(lhs: Tree, op: Symbol, args: Tree*) = Apply(Select(lhs, op), args.toList) def fn(lhs: Tree, op: Symbol, args: Tree*) = Apply(Select(lhs, op), args.toList)
@ -99,9 +90,9 @@ trait TreeDSL {
def INT_== (other: Tree) = fn(target, getMember(IntClass, nme.EQ), other) def INT_== (other: Tree) = fn(target, getMember(IntClass, nme.EQ), other)
def INT_!= (other: Tree) = fn(target, getMember(IntClass, nme.NE), 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, Boolean_and, other)
def BOOL_|| (other: Tree) = fn(target, getMember(BooleanClass, nme.ZOR), other) def BOOL_|| (other: Tree) = fn(target, Boolean_or, other)
/** Apply, Select, Match **/ /** Apply, Select, Match **/
def APPLY(params: Tree*) = Apply(target, params.toList) def APPLY(params: Tree*) = Apply(target, params.toList)
def APPLY(params: List[Tree]) = Apply(target, params) def APPLY(params: List[Tree]) = Apply(target, params)
@ -249,7 +240,7 @@ trait TreeDSL {
} }
def CASE(pat: Tree): CaseStart = new CaseStart(pat, EmptyTree) 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) { class SymbolMethods(target: Symbol) {
def BIND(body: Tree) = Bind(target, body) 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): 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 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(tpt: Tree, args: Tree*): Tree = New(tpt, List(args.toList))
def NEW(sym: Symbol, args: Tree*) = def NEW(sym: Symbol, args: Tree*): Tree = New(sym, args: _*)
if (args.isEmpty) New(TypeTree(sym.tpe))
else New(TypeTree(sym.tpe), List(args.toList))
def DEF(name: Name, tp: Type): DefTreeStart = DEF(name) withType tp def DEF(name: Name, tp: Type): DefTreeStart = DEF(name) withType tp
def DEF(name: Name): DefTreeStart = new DefTreeStart(name) def DEF(name: Name): DefTreeStart = new DefTreeStart(name)
@ -302,8 +291,8 @@ trait TreeDSL {
def IF(tree: Tree) = new IfStart(tree, EmptyTree) def IF(tree: Tree) = new IfStart(tree, EmptyTree)
def TRY(tree: Tree) = new TryStart(tree, Nil, EmptyTree) def TRY(tree: Tree) = new TryStart(tree, Nil, EmptyTree)
def BLOCK(xs: Tree*) = Block(xs.init.toList, xs.last) def BLOCK(xs: Tree*) = Block(xs.init.toList, xs.last)
def NOT(tree: Tree) = Select(tree, getMember(BooleanClass, nme.UNARY_!)) def NOT(tree: Tree) = Select(tree, Boolean_not)
def SOME(xs: Tree*) = Apply(scalaDot(nme.Some), List(makeTupleTerm(xs.toList, true))) def SOME(xs: Tree*) = Apply(SomeModule, makeTupleTerm(xs.toList, true))
/** Typed trees from symbols. */ /** Typed trees from symbols. */
def THIS(sym: Symbol) = gen.mkAttributedThis(sym) def THIS(sym: Symbol) = gen.mkAttributedThis(sym)
@ -311,21 +300,15 @@ trait TreeDSL {
def REF(sym: Symbol) = gen.mkAttributedRef(sym) def REF(sym: Symbol) = gen.mkAttributedRef(sym)
def REF(pre: Type, sym: Symbol) = gen.mkAttributedRef(pre, 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 { def makeTupleTerm(trees: List[Tree], flattenUnary: Boolean): Tree = trees match {
case Nil => UNIT case Nil => UNIT
case List(tree) if flattenUnary => tree 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 { def makeTupleType(trees: List[Tree], flattenUnary: Boolean): Tree = trees match {
case Nil => gen.scalaUnitConstr case Nil => gen.scalaUnitConstr
case List(tree) if flattenUnary => tree 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 **/ /** Implicits - some of these should probably disappear **/

View File

@ -57,7 +57,7 @@ trait MatchSupport extends ast.TreeDSL { self: ParallelMatching =>
def symbolToString(s: Symbol): String = s match { def symbolToString(s: Symbol): String = s match {
case x => x.toString case x => x.toString
} }
def treeToString(t: Tree): String = unbind(t) match { def treeToString(t: Tree): String = treeInfo.unbind(t) match {
case EmptyTree => "?" case EmptyTree => "?"
case WILD() => "_" case WILD() => "_"
case Literal(Constant(x)) => "LIT(%s)".format(x) case Literal(Constant(x)) => "LIT(%s)".format(x)

View File

@ -189,7 +189,7 @@ trait SyntheticMethods extends ast.TreeDSL {
def makeTrees(acc: Symbol, cpt: Type): (Tree, Bind) = { def makeTrees(acc: Symbol, cpt: Type): (Tree, Bind) = {
val varName = context.unit.freshTermName(acc.name + "$") val varName = context.unit.freshTermName(acc.name + "$")
val isRepeated = isRepeatedParamType(cpt) 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 = val eqMethod: Tree =
if (isRepeated) gen.mkRuntimeCall(nme.sameElements, List(Ident(varName), Ident(acc))) if (isRepeated) gen.mkRuntimeCall(nme.sameElements, List(Ident(varName), Ident(acc)))
else (Ident(varName) DOT nme.EQ)(Ident(acc)) else (Ident(varName) DOT nme.EQ)(Ident(acc))