< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Operators.java

Print this page




 529 
 530         BinaryStringOperator(Tag tag) {
 531             super(tag);
 532         }
 533 
 534         @Override
 535         public OperatorSymbol resolve(Type arg1, Type arg2) {
 536             return doLookup(stringPromotion(arg1), stringPromotion(arg2));
 537         }
 538 
 539         @Override
 540         public boolean test(Type arg1, Type arg2) {
 541             boolean hasStringOp = types.isSameType(arg1, syms.stringType) ||
 542                     types.isSameType(arg2, syms.stringType);
 543             boolean hasVoidOp = arg1.hasTag(TypeTag.VOID) || arg2.hasTag(TypeTag.VOID);
 544             return hasStringOp && !hasVoidOp;
 545         }
 546 
 547         /**
 548          * This routine applies following mappings:
 549          * - if input type is primitive, apply numeric promotion
 550          * - if input type is either 'void', 'null' or 'String' leave it untouched
 551          * - otherwise return 'Object'
 552          */
 553         private Type stringPromotion(Type t) {
 554             if (t.isPrimitive()) {
 555                 return unaryPromotion(t);
 556             } else if (t.hasTag(TypeTag.VOID) || t.hasTag(TypeTag.BOT) ||
 557                     types.isSameType(t, syms.stringType)) {
 558                 return t;


 559             } else if (t.hasTag(TypeTag.TYPEVAR)) {
 560                 return stringPromotion(t.getUpperBound());
 561             } else {
 562                 return syms.objectType;
 563             }
 564         }
 565     }
 566 
 567     /**
 568      * Class representing shift operator helper that operates on integral operand types (either boxed
 569      * or unboxed). Operator lookup is performed after applying unary numeric promotion to each input type.
 570      */
 571     class BinaryShiftOperator extends BinaryOperatorHelper {
 572 
 573         BinaryShiftOperator(Tag tag) {
 574             super(tag);
 575         }
 576 
 577         @Override
 578         public OperatorSymbol resolve(Type arg1, Type arg2) {


 679                         .addUnaryOperator(LONG, LONG, lsub)
 680                         .addUnaryOperator(INT, INT, isub)
 681                         .addUnaryOperator(CHAR, CHAR, isub)
 682                         .addUnaryOperator(SHORT, SHORT, isub)
 683                         .addUnaryOperator(BYTE, BYTE, isub),
 684                 new UnaryBooleanOperator(Tag.NOT)
 685                         .addUnaryOperator(BOOLEAN, BOOLEAN, bool_not),
 686                 new UnaryReferenceOperator(Tag.NULLCHK)
 687                         .addUnaryOperator(OBJECT, OBJECT, nullchk));
 688     }
 689 
 690     /**
 691      * Initialize all binary operators.
 692      */
 693     private void initBinaryOperators() {
 694         initOperators(binaryOperators,
 695             new BinaryStringOperator(Tag.PLUS)
 696                     .addBinaryOperator(STRING, OBJECT, STRING, string_add)
 697                     .addBinaryOperator(OBJECT, STRING, STRING, string_add)
 698                     .addBinaryOperator(STRING, STRING, STRING, string_add)

 699                     .addBinaryOperator(STRING, INT, STRING, string_add)
 700                     .addBinaryOperator(STRING, LONG, STRING, string_add)
 701                     .addBinaryOperator(STRING, FLOAT, STRING, string_add)
 702                     .addBinaryOperator(STRING, DOUBLE, STRING, string_add)
 703                     .addBinaryOperator(STRING, BOOLEAN, STRING, string_add)
 704                     .addBinaryOperator(STRING, BOT, STRING, string_add)

 705                     .addBinaryOperator(INT, STRING, STRING, string_add)
 706                     .addBinaryOperator(LONG, STRING, STRING, string_add)
 707                     .addBinaryOperator(FLOAT, STRING, STRING, string_add)
 708                     .addBinaryOperator(DOUBLE, STRING, STRING, string_add)
 709                     .addBinaryOperator(BOOLEAN, STRING, STRING, string_add)
 710                     .addBinaryOperator(BOT, STRING, STRING, string_add),
 711             new BinaryNumericOperator(Tag.PLUS)
 712                     .addBinaryOperator(DOUBLE, DOUBLE, DOUBLE, dadd)
 713                     .addBinaryOperator(FLOAT, FLOAT, FLOAT, fadd)
 714                     .addBinaryOperator(LONG, LONG, LONG, ladd)
 715                     .addBinaryOperator(INT, INT, INT, iadd),
 716             new BinaryNumericOperator(Tag.MINUS)
 717                     .addBinaryOperator(DOUBLE, DOUBLE, DOUBLE, dsub)
 718                     .addBinaryOperator(FLOAT, FLOAT, FLOAT, fsub)
 719                     .addBinaryOperator(LONG, LONG, LONG, lsub)
 720                     .addBinaryOperator(INT, INT, INT, isub),
 721             new BinaryNumericOperator(Tag.MUL)
 722                     .addBinaryOperator(DOUBLE, DOUBLE, DOUBLE, dmul)
 723                     .addBinaryOperator(FLOAT, FLOAT, FLOAT, fmul)
 724                     .addBinaryOperator(LONG, LONG, LONG, lmul)




 529 
 530         BinaryStringOperator(Tag tag) {
 531             super(tag);
 532         }
 533 
 534         @Override
 535         public OperatorSymbol resolve(Type arg1, Type arg2) {
 536             return doLookup(stringPromotion(arg1), stringPromotion(arg2));
 537         }
 538 
 539         @Override
 540         public boolean test(Type arg1, Type arg2) {
 541             boolean hasStringOp = types.isSameType(arg1, syms.stringType) ||
 542                     types.isSameType(arg2, syms.stringType);
 543             boolean hasVoidOp = arg1.hasTag(TypeTag.VOID) || arg2.hasTag(TypeTag.VOID);
 544             return hasStringOp && !hasVoidOp;
 545         }
 546 
 547         /**
 548          * This routine applies following mappings:
 549          * - if input type is either 'char', 'void', 'null' or 'String' leave it untouched
 550          * - otherwise, if input type is primitive, apply numeric promotion
 551          * - otherwise return 'Object'
 552          */
 553         private Type stringPromotion(Type t) {
 554             if (t.hasTag(TypeTag.CHAR) || t.hasTag(TypeTag.VOID) || t.hasTag(TypeTag.BOT) ||


 555                     types.isSameType(t, syms.stringType)) {
 556                 return t;
 557             } else if (t.isPrimitive()) {
 558                 return unaryPromotion(t);
 559             } else if (t.hasTag(TypeTag.TYPEVAR)) {
 560                 return stringPromotion(t.getUpperBound());
 561             } else {
 562                 return syms.objectType;
 563             }
 564         }
 565     }
 566 
 567     /**
 568      * Class representing shift operator helper that operates on integral operand types (either boxed
 569      * or unboxed). Operator lookup is performed after applying unary numeric promotion to each input type.
 570      */
 571     class BinaryShiftOperator extends BinaryOperatorHelper {
 572 
 573         BinaryShiftOperator(Tag tag) {
 574             super(tag);
 575         }
 576 
 577         @Override
 578         public OperatorSymbol resolve(Type arg1, Type arg2) {


 679                         .addUnaryOperator(LONG, LONG, lsub)
 680                         .addUnaryOperator(INT, INT, isub)
 681                         .addUnaryOperator(CHAR, CHAR, isub)
 682                         .addUnaryOperator(SHORT, SHORT, isub)
 683                         .addUnaryOperator(BYTE, BYTE, isub),
 684                 new UnaryBooleanOperator(Tag.NOT)
 685                         .addUnaryOperator(BOOLEAN, BOOLEAN, bool_not),
 686                 new UnaryReferenceOperator(Tag.NULLCHK)
 687                         .addUnaryOperator(OBJECT, OBJECT, nullchk));
 688     }
 689 
 690     /**
 691      * Initialize all binary operators.
 692      */
 693     private void initBinaryOperators() {
 694         initOperators(binaryOperators,
 695             new BinaryStringOperator(Tag.PLUS)
 696                     .addBinaryOperator(STRING, OBJECT, STRING, string_add)
 697                     .addBinaryOperator(OBJECT, STRING, STRING, string_add)
 698                     .addBinaryOperator(STRING, STRING, STRING, string_add)
 699                     .addBinaryOperator(STRING, CHAR, STRING, string_add)
 700                     .addBinaryOperator(STRING, INT, STRING, string_add)
 701                     .addBinaryOperator(STRING, LONG, STRING, string_add)
 702                     .addBinaryOperator(STRING, FLOAT, STRING, string_add)
 703                     .addBinaryOperator(STRING, DOUBLE, STRING, string_add)
 704                     .addBinaryOperator(STRING, BOOLEAN, STRING, string_add)
 705                     .addBinaryOperator(STRING, BOT, STRING, string_add)
 706                     .addBinaryOperator(CHAR, STRING, STRING, string_add)
 707                     .addBinaryOperator(INT, STRING, STRING, string_add)
 708                     .addBinaryOperator(LONG, STRING, STRING, string_add)
 709                     .addBinaryOperator(FLOAT, STRING, STRING, string_add)
 710                     .addBinaryOperator(DOUBLE, STRING, STRING, string_add)
 711                     .addBinaryOperator(BOOLEAN, STRING, STRING, string_add)
 712                     .addBinaryOperator(BOT, STRING, STRING, string_add),
 713             new BinaryNumericOperator(Tag.PLUS)
 714                     .addBinaryOperator(DOUBLE, DOUBLE, DOUBLE, dadd)
 715                     .addBinaryOperator(FLOAT, FLOAT, FLOAT, fadd)
 716                     .addBinaryOperator(LONG, LONG, LONG, ladd)
 717                     .addBinaryOperator(INT, INT, INT, iadd),
 718             new BinaryNumericOperator(Tag.MINUS)
 719                     .addBinaryOperator(DOUBLE, DOUBLE, DOUBLE, dsub)
 720                     .addBinaryOperator(FLOAT, FLOAT, FLOAT, fsub)
 721                     .addBinaryOperator(LONG, LONG, LONG, lsub)
 722                     .addBinaryOperator(INT, INT, INT, isub),
 723             new BinaryNumericOperator(Tag.MUL)
 724                     .addBinaryOperator(DOUBLE, DOUBLE, DOUBLE, dmul)
 725                     .addBinaryOperator(FLOAT, FLOAT, FLOAT, fmul)
 726                     .addBinaryOperator(LONG, LONG, LONG, lmul)


< prev index next >