From fbb7865e137e83660257fdc79d19d29ff39c775b Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Mon, 20 Feb 2012 20:50:18 -0800 Subject: Eliminating duplication in zero creation. --- src/compiler/scala/reflect/internal/TreeGen.scala | 27 +++++++++++----------- .../scala/tools/nsc/backend/icode/GenICode.scala | 26 +++++++++------------ 2 files changed, 24 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/compiler/scala/reflect/internal/TreeGen.scala b/src/compiler/scala/reflect/internal/TreeGen.scala index e537c6b83f..cc882ad5ed 100644 --- a/src/compiler/scala/reflect/internal/TreeGen.scala +++ b/src/compiler/scala/reflect/internal/TreeGen.scala @@ -250,20 +250,19 @@ abstract class TreeGen { * var x: T = _ * which is appropriate to the given Type. */ - def mkZero(tp: Type): Tree = { - val tree = tp.typeSymbol match { - case UnitClass => Literal(Constant()) - case BooleanClass => Literal(Constant(false)) - case FloatClass => Literal(Constant(0.0f)) - case DoubleClass => Literal(Constant(0.0d)) - case ByteClass => Literal(Constant(0.toByte)) - case ShortClass => Literal(Constant(0.toShort)) - case IntClass => Literal(Constant(0)) - case LongClass => Literal(Constant(0L)) - case CharClass => Literal(Constant(0.toChar)) - case _ => Literal(Constant(null)) - } - tree setType tp + def mkZero(tp: Type): Tree = Literal(mkConstantZero(tp)) setType tp + + def mkConstantZero(tp: Type): Constant = tp.typeSymbol match { + case UnitClass => Constant(()) + case BooleanClass => Constant(false) + case FloatClass => Constant(0.0f) + case DoubleClass => Constant(0.0d) + case ByteClass => Constant(0.toByte) + case ShortClass => Constant(0.toShort) + case IntClass => Constant(0) + case LongClass => Constant(0L) + case CharClass => Constant(0.toChar) + case _ => Constant(null) } def mkZeroContravariantAfterTyper(tp: Type): Tree = { diff --git a/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala b/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala index e1731c5259..dd65e825c3 100644 --- a/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala +++ b/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala @@ -879,23 +879,19 @@ abstract class GenICode extends SubComponent { } else if (sym.elisionLevel.exists (_ < settings.elidebelow.value || settings.noassertions.value)) { // XXX settings.noassertions.value temporarily retained to avoid // breakage until a reasonable interface is settled upon. - debuglog("Eliding call from " + tree.symbol.owner + " to " + sym + " based on its elision threshold of " + sym.elisionLevel.get) - val value = expectedType match { - case UNIT => () - case BOOL => false - case BYTE => 0:Byte - case SHORT => 0:Short - case CHAR => '?' - case INT => 0 - case LONG => 0L - case FLOAT => 0.0f - case DOUBLE => 0.0 - case _ => null + debuglog("Eliding call from " + tree.symbol.owner + " to " + sym + + " based on its elision threshold of " + sym.elisionLevel.get) + if (expectedType.isValueType) { + ctx.bb.emit(CONSTANT(global.gen.mkConstantZero(expectedType.toType)), tree.pos) + generatedType = expectedType + } + else { + ctx.bb.emit(CONSTANT(Constant(null)), tree.pos) + generatedType = NullReference } - ctx.bb.emit(CONSTANT(Constant(value)), tree.pos) - generatedType = if (expectedType.isInstanceOf[ValueTypeKind]) expectedType else NullReference ctx - } else { // normal method call + } + else { // normal method call debuglog("Gen CALL_METHOD with sym: " + sym + " isStaticSymbol: " + sym.isStaticMember); val invokeStyle = if (sym.isStaticMember) -- cgit v1.2.3