1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpeclipse.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
14 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
15 import net.sourceforge.phpdt.internal.compiler.impl.DoubleConstant;
16 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
17 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
20 public class IntLiteral extends NumberLiteral {
23 public static final IntLiteral
24 One = new IntLiteral(new char[]{'1'},0,0,1);//used for ++ and --
26 static final Constant FORMAT_ERROR = new DoubleConstant(1.0/0.0); // NaN;
27 public IntLiteral(char[] token, int s, int e) {
30 public IntLiteral(char[] token, int s,int e, int value) {
34 public IntLiteral(int intValue) {
35 //special optimized constructor : the cst is the argument
37 //value that should not be used
42 constant = Constant.fromValue(intValue);
46 public void computeConstant() {
47 //a special constant is use for the potential Integer.MAX_VALUE+1
48 //which is legal if used with a - as prefix....cool....
49 //notice that Integer.MIN_VALUE == -2147483648
51 long MAX = Integer.MAX_VALUE;
52 if (this == One) { constant = Constant.One; return ;}
54 int length = source.length;
55 long computedValue = 0L;
57 { MAX = 0xFFFFFFFFL ; //a long in order to be positive !
58 if (length == 1) { constant = Constant.fromValue(0); return ;}
59 final int shift,radix;
61 if ( (source[1] == 'x') | (source[1] == 'X') )
62 { shift = 4 ; j = 2; radix = 16;}
64 { shift = 3 ; j = 1; radix = 8;}
65 while (source[j]=='0')
66 { j++; //jump over redondant zero
68 { //watch for 000000000000000000 :-(
69 constant = Constant.fromValue(value = (int)computedValue);
74 if ((digitValue = Character.digit(source[j++],radix)) < 0 )
75 { constant = FORMAT_ERROR; return ;}
76 computedValue = (computedValue<<shift) | digitValue ;
77 if (computedValue > MAX) return /*constant stays null*/ ;}}
79 { //-----------regular case : radix = 10-----------
80 for (int i = 0 ; i < length;i++)
82 if ((digitValue = Character.digit(source[i],10)) < 0 )
83 { constant = FORMAT_ERROR; return ;}
84 computedValue = 10*computedValue + digitValue;
85 if (computedValue > MAX) return /*constant stays null*/ ; }}
87 constant = Constant.fromValue(value = (int)computedValue);
91 * Code generation for int literal
93 * @param currentScope net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
94 * @param codeStream net.sourceforge.phpdt.internal.compiler.codegen.CodeStream
95 * @param valueRequired boolean
97 //public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
98 // int pc = codeStream.position;
100 // if ((implicitConversion >> 4) == T_int)
101 // codeStream.generateInlinedValue(value);
103 // codeStream.generateConstant(constant, implicitConversion);
104 // codeStream.recordPositionsFrom(pc, this.sourceStart);
106 public TypeBinding literalType(BlockScope scope) {
109 public final boolean mayRepresentMIN_VALUE(){
110 //a special autorized int literral is 2147483648
111 //which is ONE over the limit. This special case
112 //only is used in combinaison with - to denote
113 //the minimal value of int -2147483648
115 return ((source.length == 10) &&
116 (source[0] == '2') &&
117 (source[1] == '1') &&
118 (source[2] == '4') &&
119 (source[3] == '7') &&
120 (source[4] == '4') &&
121 (source[5] == '8') &&
122 (source[6] == '3') &&
123 (source[7] == '6') &&
124 (source[8] == '4') &&
125 (source[9] == '8'));}
126 public TypeBinding resolveType(BlockScope scope) {
127 // the format may be incorrect while the scanner could detect
128 // such an error only on painfull tests...easier and faster here
130 TypeBinding tb = super.resolveType(scope);
131 if (constant == FORMAT_ERROR) {
132 constant = NotAConstant;
133 scope.problemReporter().constantOutOfFormat(this);
134 this.resolvedType = null;
139 public String toStringExpression(){
142 /* special optimized IntLiteral that are created by the compiler */
143 return String.valueOf(value);
145 return super.toStringExpression();}
146 public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
147 visitor.visit(this, scope);
148 visitor.endVisit(this, scope);