1 11 package org.eclipse.jdt.internal.compiler.ast; 12 13 import org.eclipse.jdt.internal.compiler.ASTVisitor; 14 import org.eclipse.jdt.internal.compiler.impl.*; 15 import org.eclipse.jdt.internal.compiler.codegen.*; 16 import org.eclipse.jdt.internal.compiler.lookup.*; 17 import org.eclipse.jdt.internal.compiler.parser.ScannerHelper; 18 19 public class IntLiteral extends NumberLiteral { 20 public int value; 21 22 public static final IntLiteral 23 One = new IntLiteral(new char[]{'1'},0,0,1); 25 static final Constant FORMAT_ERROR = DoubleConstant.fromValue(1.0/0.0); public IntLiteral(char[] token, int s, int e) { 27 super(token, s,e); 28 } 29 public IntLiteral(char[] token, int s,int e, int value) { 30 this(token, s,e); 31 this.value = value; 32 } 33 public IntLiteral(int intValue) { 34 36 super(null,0,0); 41 constant = IntConstant.fromValue(intValue); 42 value = intValue; 43 44 } 45 public void computeConstant() { 46 50 long MAX = Integer.MAX_VALUE; 51 if (this == One) { constant = IntConstant.fromValue(1); return ;} 52 53 int length = source.length; 54 long computedValue = 0L; 55 if (source[0] == '0') 56 { MAX = 0xFFFFFFFFL ; if (length == 1) { constant = IntConstant.fromValue(0); return ;} 58 final int shift,radix; 59 int j ; 60 if ( (source[1] == 'x') || (source[1] == 'X') ) 61 { shift = 4 ; j = 2; radix = 16;} 62 else 63 { shift = 3 ; j = 1; radix = 8;} 64 while (source[j]=='0') 65 { j++; if (j == length) 67 { constant = IntConstant.fromValue(value = (int)computedValue); 69 return ;}} 70 71 while (j<length) 72 { int digitValue ; 73 if ((digitValue = ScannerHelper.digit(source[j++],radix)) < 0 ) 74 { constant = FORMAT_ERROR; return ;} 75 computedValue = (computedValue<<shift) | digitValue ; 76 if (computedValue > MAX) return ;}} 77 else 78 { for (int i = 0 ; i < length;i++) 80 { int digitValue ; 81 if ((digitValue = ScannerHelper.digit(source[i],10)) < 0 ) 82 { constant = FORMAT_ERROR; return ;} 83 computedValue = 10*computedValue + digitValue; 84 if (computedValue > MAX) return ; }} 85 86 constant = IntConstant.fromValue(value = (int)computedValue); 87 88 } 89 96 public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) { 97 int pc = codeStream.position; 98 if (valueRequired) { 99 codeStream.generateConstant(constant, implicitConversion); 100 } 101 codeStream.recordPositionsFrom(pc, this.sourceStart); 102 } 103 public TypeBinding literalType(BlockScope scope) { 104 return TypeBinding.INT; 105 } 106 public final boolean mayRepresentMIN_VALUE(){ 107 112 return ((source.length == 10) && 113 (source[0] == '2') && 114 (source[1] == '1') && 115 (source[2] == '4') && 116 (source[3] == '7') && 117 (source[4] == '4') && 118 (source[5] == '8') && 119 (source[6] == '3') && 120 (source[7] == '6') && 121 (source[8] == '4') && 122 (source[9] == '8') && 123 (((this.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT) == 0)); 124 } 125 public TypeBinding resolveType(BlockScope scope) { 126 129 TypeBinding tb = super.resolveType(scope); 130 if (constant == FORMAT_ERROR) { 131 constant = Constant.NotAConstant; 132 scope.problemReporter().constantOutOfFormat(this); 133 this.resolvedType = null; 134 return null; 135 } 136 return tb; 137 } 138 public StringBuffer printExpression(int indent, StringBuffer output){ 139 140 if (source == null) { 141 142 return output.append(String.valueOf(value)); 143 } 144 return super.printExpression(indent, output); 145 } 146 147 public void traverse(ASTVisitor visitor, BlockScope scope) { 148 visitor.visit(this, scope); 149 visitor.endVisit(this, scope); 150 } 151 } 152 | Popular Tags |