From e62dc2441bb67a18e9d9f3b2b04e42e2f71c4972 Mon Sep 17 00:00:00 2001 From: odersky Date: Fri, 16 Feb 2007 12:02:06 +0000 Subject: [PATCH] fixed unary_ops git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@10005 5e8d7ff9-d8ef-0310-90f0-a4852d11357a --- .../scala/tools/nsc/ast/parser/Parsers.scala | 8 ++-- .../tools/nsc/backend/ScalaPrimitives.scala | 40 ++++--------------- .../scala/tools/nsc/symtab/Definitions.scala | 6 +-- .../scala/tools/nsc/symtab/StdNames.scala | 2 - .../nsc/typechecker/ConstantFolder.scala | 22 +++++----- 5 files changed, 24 insertions(+), 54 deletions(-) diff --git a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala index b71c71502..ebb644f9b 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala @@ -986,16 +986,17 @@ trait Parsers requires SyntaxAnalyzer { /** PrefixExpr ::= [`-' | `+' | `~' | `!' | `&'] SimpleExpr */ - def prefixExpr(): Tree = + def prefixExpr(): Tree = { + def unaryOp(): Name = "unary_" + ident() if (isIdent && in.name == MINUS) { - val name = ident() + val name = unaryOp() in.token match { case INTLIT | LONGLIT | FLOATLIT | DOUBLELIT => literal(false, true) case _ => atPos(in.currentPos) { Select(stripParens(simpleExpr()), name) } } } else if (isIdent && (in.name == PLUS || in.name == TILDE || in.name == BANG)) { val pos = in.currentPos - val name = ident() + val name = unaryOp() atPos(pos) { Select(stripParens(simpleExpr()), name) } } else if (isIdent && in.name == AMP) { val pos = in.currentPos @@ -1011,6 +1012,7 @@ trait Parsers requires SyntaxAnalyzer { } else { simpleExpr() } + } /* SimpleExpr ::= new AnnotType {`(' [Exprs [`,']] `)'} {`with' AnnotType} [TemplateBody] * | BlockExpr diff --git a/src/compiler/scala/tools/nsc/backend/ScalaPrimitives.scala b/src/compiler/scala/tools/nsc/backend/ScalaPrimitives.scala index 8b37ca6bd..ca0359c01 100644 --- a/src/compiler/scala/tools/nsc/backend/ScalaPrimitives.scala +++ b/src/compiler/scala/tools/nsc/backend/ScalaPrimitives.scala @@ -245,12 +245,11 @@ abstract class ScalaPrimitives { addPrimitives(BooleanClass, nme.XOR, XOR) // addPrimitives(BooleanClass, nme.ADD, CONCAT) // unary ! - addPrimitives(BooleanClass, nme.UNARY_!, ZNOT) +// addPrimitives(BooleanClass, nme.UNARY_!, ZNOT) // scala.Byte addPrimitives(ByteClass, nme.EQ, EQ) addPrimitives(ByteClass, nme.NE, NE) - addPrimitives(ByteClass, nme.NOT, NOT) addPrimitives(ByteClass, nme.ADD, ADD) addPrimitives(ByteClass, nme.SUB, SUB) addPrimitives(ByteClass, nme.MUL, MUL) @@ -285,7 +284,6 @@ abstract class ScalaPrimitives { // scala.Short addPrimitives(ShortClass, nme.EQ, EQ) addPrimitives(ShortClass, nme.NE, NE) - addPrimitives(ShortClass, nme.NOT, NOT) addPrimitives(ShortClass, nme.ADD, ADD) addPrimitives(ShortClass, nme.SUB, SUB) addPrimitives(ShortClass, nme.MUL, MUL) @@ -320,7 +318,6 @@ abstract class ScalaPrimitives { // scala.Char addPrimitives(CharClass, nme.EQ, EQ) addPrimitives(CharClass, nme.NE, NE) - addPrimitives(CharClass, nme.NOT, NOT) addPrimitives(CharClass, nme.ADD, ADD) addPrimitives(CharClass, nme.SUB, SUB) addPrimitives(CharClass, nme.MUL, MUL) @@ -354,7 +351,6 @@ abstract class ScalaPrimitives { // scala.Int addPrimitives(IntClass, nme.EQ, EQ) addPrimitives(IntClass, nme.NE, NE) - addPrimitives(IntClass, nme.NOT, NOT) addPrimitives(IntClass, nme.ADD, ADD) addPrimitives(IntClass, nme.SUB, SUB) addPrimitives(IntClass, nme.MUL, MUL) @@ -388,7 +384,6 @@ abstract class ScalaPrimitives { // scala.Long addPrimitives(LongClass, nme.EQ, EQ) addPrimitives(LongClass, nme.NE, NE) - addPrimitives(LongClass, nme.NOT, NOT) addPrimitives(LongClass, nme.ADD, ADD) addPrimitives(LongClass, nme.SUB, SUB) addPrimitives(LongClass, nme.MUL, MUL) @@ -481,34 +476,13 @@ abstract class ScalaPrimitives { def addPrimitives(cls: Symbol, method: Name, code: Int): Unit = { val tpe = cls.info val sym = tpe.member(method) - if (sym != NoSymbol) sym.info match { - case OverloadedType(pre, alternatives) => - log("Adding " + alternatives.length + " overloads for " + sym.fullNameString) - code match { - case SUB => - alternatives foreach ((s) => - if (s.info.paramTypes.length == 0) - addPrimitive(s, NEG) // unary - else - addPrimitive(s, code)) - - case ADD => - alternatives foreach ((s) => - if (s.info.paramTypes.length == 0) - addPrimitive(s, POS) // unary - else if (s.info.paramTypes.head == definitions.StringClass.tpe) - addPrimitive(s, CONCAT) // string concatenation - else - addPrimitive(s, code)) - - case _ => - alternatives foreach ((s) => addPrimitive(s, code)) - } - - case _ => - addPrimitive(sym, code) - } else + if (sym == NoSymbol) inform("Unknown primitive method " + cls + "." + method) + for (val s <- sym.alternatives) + addPrimitive( + s, + if (code == ADD && s.info.paramTypes.head == definitions.StringClass.tpe) CONCAT + else code) } def isCoercion(code: Int): Boolean = (code >= B2B) && (code <= D2D); diff --git a/src/compiler/scala/tools/nsc/symtab/Definitions.scala b/src/compiler/scala/tools/nsc/symtab/Definitions.scala index 6de5557de..da9828266 100644 --- a/src/compiler/scala/tools/nsc/symtab/Definitions.scala +++ b/src/compiler/scala/tools/nsc/symtab/Definitions.scala @@ -51,7 +51,7 @@ trait Definitions requires SymbolTable { // the scala value classes var UnitClass: Symbol = _ var BooleanClass: Symbol = _ - def Boolean_not = getMember(BooleanClass, nme.ZNOT) + def Boolean_not = getMember(BooleanClass, nme.UNARY_!) def Boolean_and = getMember(BooleanClass, nme.ZAND) def Boolean_or = getMember(BooleanClass, nme.ZOR) var ByteClass: Symbol = _ @@ -520,7 +520,6 @@ trait Definitions requires SymbolTable { val stringtype = StringClass.typeConstructor // init scala.Boolean - newParameterlessMethod(BooleanClass, nme.ZNOT, booltype) newParameterlessMethod(BooleanClass, nme.UNARY_!, booltype) newMethod(BooleanClass, nme.EQ, boolparam, booltype) newMethod(BooleanClass, nme.NE, boolparam, booltype) @@ -585,13 +584,10 @@ trait Definitions requires SymbolTable { } // unary operations - newParameterlessMethod(clazz, nme.ADD, restype) - newParameterlessMethod(clazz, nme.SUB, restype) newParameterlessMethod(clazz, nme.UNARY_+, restype) newParameterlessMethod(clazz, nme.UNARY_-, restype) if (isCardinal) { - newParameterlessMethod(clazz, nme.NOT, restype) newParameterlessMethod(clazz, nme.UNARY_~, restype) } diff --git a/src/compiler/scala/tools/nsc/symtab/StdNames.scala b/src/compiler/scala/tools/nsc/symtab/StdNames.scala index 7d124e522..8ed10a75a 100644 --- a/src/compiler/scala/tools/nsc/symtab/StdNames.scala +++ b/src/compiler/scala/tools/nsc/symtab/StdNames.scala @@ -312,10 +312,8 @@ trait StdNames requires SymbolTable { val xml = newTermName("xml") val zip = newTermName("zip") - val ZNOT = encode("!") val ZAND = encode("&&") val ZOR = encode("||") - val NOT = encode("~") val ADD = encode("+") val SUB = encode("-") val MUL = encode("*") diff --git a/src/compiler/scala/tools/nsc/typechecker/ConstantFolder.scala b/src/compiler/scala/tools/nsc/typechecker/ConstantFolder.scala index b7a85d4c5..48dd134f5 100644 --- a/src/compiler/scala/tools/nsc/typechecker/ConstantFolder.scala +++ b/src/compiler/scala/tools/nsc/typechecker/ConstantFolder.scala @@ -50,20 +50,20 @@ abstract class ConstantFolder { } private def foldUnop(op: Name, x: Constant): Constant = Pair(op, x.tag) match { - case Pair(nme.ZNOT, BooleanTag) => Constant(!x.booleanValue) + case Pair(nme.UNARY_!, BooleanTag) => Constant(!x.booleanValue) - case Pair(nme.NOT , IntTag ) => Constant(~x.intValue) - case Pair(nme.NOT , LongTag ) => Constant(~x.longValue) + case Pair(nme.UNARY_~ , IntTag ) => Constant(~x.intValue) + case Pair(nme.UNARY_~ , LongTag ) => Constant(~x.longValue) - case Pair(nme.ADD , IntTag ) => Constant(+x.intValue) - case Pair(nme.ADD , LongTag ) => Constant(+x.longValue) - case Pair(nme.ADD , FloatTag ) => Constant(+x.floatValue) - case Pair(nme.ADD , DoubleTag ) => Constant(+x.doubleValue) + case Pair(nme.UNARY_+ , IntTag ) => Constant(+x.intValue) + case Pair(nme.UNARY_+ , LongTag ) => Constant(+x.longValue) + case Pair(nme.UNARY_+ , FloatTag ) => Constant(+x.floatValue) + case Pair(nme.UNARY_+ , DoubleTag ) => Constant(+x.doubleValue) - case Pair(nme.SUB , IntTag ) => Constant(-x.intValue) - case Pair(nme.SUB , LongTag ) => Constant(-x.longValue) - case Pair(nme.SUB , FloatTag ) => Constant(-x.floatValue) - case Pair(nme.SUB , DoubleTag ) => Constant(-x.doubleValue) + case Pair(nme.UNARY_- , IntTag ) => Constant(-x.intValue) + case Pair(nme.UNARY_- , LongTag ) => Constant(-x.longValue) + case Pair(nme.UNARY_- , FloatTag ) => Constant(-x.floatValue) + case Pair(nme.UNARY_- , DoubleTag ) => Constant(-x.doubleValue) case _ => null }