1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
14 import net.sourceforge.phpdt.internal.compiler.impl.*;
15 import net.sourceforge.phpdt.internal.compiler.codegen.*;
16 import net.sourceforge.phpdt.internal.compiler.lookup.*;
18 public class IntLiteral extends NumberLiteral {
21 public static final IntLiteral
22 One = new IntLiteral(new char[]{'1'},0,0,1);//used for ++ and --
24 static final Constant FORMAT_ERROR = new DoubleConstant(1.0/0.0); // NaN;
25 public IntLiteral(char[] token, int s, int e) {
28 public IntLiteral(char[] token, int s,int e, int value) {
32 public IntLiteral(int intValue) {
33 //special optimized constructor : the cst is the argument
35 //value that should not be used
40 constant = Constant.fromValue(intValue);
44 public void computeConstant() {
45 //a special constant is use for the potential Integer.MAX_VALUE+1
46 //which is legal if used with a - as prefix....cool....
47 //notice that Integer.MIN_VALUE == -2147483648
49 long MAX = Integer.MAX_VALUE;
50 if (this == One) { constant = Constant.One; return ;}
52 int length = source.length;
53 long computedValue = 0L;
55 { MAX = 0xFFFFFFFFL ; //a long in order to be positive !
56 if (length == 1) { constant = Constant.fromValue(0); return ;}
57 final int shift,radix;
59 if ( (source[1] == 'x') | (source[1] == 'X') )
60 { shift = 4 ; j = 2; radix = 16;}
62 { shift = 3 ; j = 1; radix = 8;}
63 while (source[j]=='0')
64 { j++; //jump over redondant zero
66 { //watch for 000000000000000000 :-(
67 constant = Constant.fromValue(value = (int)computedValue);
72 if ((digitValue = Character.digit(source[j++],radix)) < 0 )
73 { constant = FORMAT_ERROR; return ;}
74 computedValue = (computedValue<<shift) | digitValue ;
75 if (computedValue > MAX) return /*constant stays null*/ ;}}
77 { //-----------regular case : radix = 10-----------
78 for (int i = 0 ; i < length;i++)
80 if ((digitValue = Character.digit(source[i],10)) < 0 )
81 { constant = FORMAT_ERROR; return ;}
82 computedValue = 10*computedValue + digitValue;
83 if (computedValue > MAX) return /*constant stays null*/ ; }}
85 constant = Constant.fromValue(value = (int)computedValue);
89 * Code generation for int literal
91 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
92 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
93 * @param valueRequired boolean
95 public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
96 int pc = codeStream.position;
98 if ((implicitConversion >> 4) == T_int)
99 codeStream.generateInlinedValue(value);
101 codeStream.generateConstant(constant, implicitConversion);
102 codeStream.recordPositionsFrom(pc, this.sourceStart);
104 public TypeBinding literalType(BlockScope scope) {
107 public final boolean mayRepresentMIN_VALUE(){
108 //a special autorized int literral is 2147483648
109 //which is ONE over the limit. This special case
110 //only is used in combinaison with - to denote
111 //the minimal value of int -2147483648
113 return ((source.length == 10) &&
114 (source[0] == '2') &&
115 (source[1] == '1') &&
116 (source[2] == '4') &&
117 (source[3] == '7') &&
118 (source[4] == '4') &&
119 (source[5] == '8') &&
120 (source[6] == '3') &&
121 (source[7] == '6') &&
122 (source[8] == '4') &&
123 (source[9] == '8'));}
124 public TypeBinding resolveType(BlockScope scope) {
125 // the format may be incorrect while the scanner could detect
126 // such an error only on painfull tests...easier and faster here
128 TypeBinding tb = super.resolveType(scope);
129 if (constant == FORMAT_ERROR) {
130 constant = NotAConstant;
131 scope.problemReporter().constantOutOfFormat(this);
136 public String toStringExpression(){
139 /* special optimized IntLiteral that are created by the compiler */
140 return String.valueOf(value);
142 return super.toStringExpression();}
143 public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
144 visitor.visit(this, scope);
145 visitor.endVisit(this, scope);