You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IntConst.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later.
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist.compiler.ast;
  16. import javassist.compiler.CompileError;
  17. import javassist.compiler.TokenId;
  18. /**
  19. * Integer constant.
  20. */
  21. public class IntConst extends ASTree {
  22. protected long value;
  23. protected int type;
  24. public IntConst(long v, int tokenId) { value = v; type = tokenId; }
  25. public long get() { return value; }
  26. public void set(long v) { value = v; }
  27. /* Returns IntConstant, CharConstant, or LongConstant.
  28. */
  29. public int getType() { return type; }
  30. public String toString() { return Long.toString(value); }
  31. public void accept(Visitor v) throws CompileError {
  32. v.atIntConst(this);
  33. }
  34. public ASTree compute(int op, ASTree right) {
  35. if (right instanceof IntConst)
  36. return compute0(op, (IntConst)right);
  37. else if (right instanceof DoubleConst)
  38. return compute0(op, (DoubleConst)right);
  39. else
  40. return null;
  41. }
  42. private IntConst compute0(int op, IntConst right) {
  43. int type1 = this.type;
  44. int type2 = right.type;
  45. int newType;
  46. if (type1 == TokenId.LongConstant || type2 == TokenId.LongConstant)
  47. newType = TokenId.LongConstant;
  48. else if (type1 == TokenId.CharConstant
  49. && type2 == TokenId.CharConstant)
  50. newType = TokenId.CharConstant;
  51. else
  52. newType = TokenId.IntConstant;
  53. long value1 = this.value;
  54. long value2 = right.value;
  55. long newValue;
  56. switch (op) {
  57. case '+' :
  58. newValue = value1 + value2;
  59. break;
  60. case '-' :
  61. newValue = value1 - value2;
  62. break;
  63. case '*' :
  64. newValue = value1 * value2;
  65. break;
  66. case '/' :
  67. newValue = value1 / value2;
  68. break;
  69. case '%' :
  70. newValue = value1 % value2;
  71. break;
  72. case '|' :
  73. newValue = value1 | value2;
  74. break;
  75. case '^' :
  76. newValue = value1 ^ value2;
  77. break;
  78. case '&' :
  79. newValue = value1 & value2;
  80. break;
  81. case TokenId.LSHIFT :
  82. newValue = value << (int)value2;
  83. newType = type1;
  84. break;
  85. case TokenId.RSHIFT :
  86. newValue = value >> (int)value2;
  87. newType = type1;
  88. break;
  89. case TokenId.ARSHIFT :
  90. newValue = value >>> (int)value2;
  91. newType = type1;
  92. break;
  93. default :
  94. return null;
  95. }
  96. return new IntConst(newValue, newType);
  97. }
  98. private DoubleConst compute0(int op, DoubleConst right) {
  99. double value1 = (double)this.value;
  100. double value2 = right.value;
  101. double newValue;
  102. switch (op) {
  103. case '+' :
  104. newValue = value1 + value2;
  105. break;
  106. case '-' :
  107. newValue = value1 - value2;
  108. break;
  109. case '*' :
  110. newValue = value1 * value2;
  111. break;
  112. case '/' :
  113. newValue = value1 / value2;
  114. break;
  115. case '%' :
  116. newValue = value1 % value2;
  117. break;
  118. default :
  119. return null;
  120. }
  121. return new DoubleConst(newValue, right.type);
  122. }
  123. }