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.phpdt.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;
19 public class IntLiteral extends NumberLiteral {
22 public static final IntLiteral One = new IntLiteral(new char[] { '1' }, 0,
23 0, 1);// used for ++ and --
25 static final Constant FORMAT_ERROR = new DoubleConstant(1.0 / 0.0); // NaN;
27 public IntLiteral(char[] token, int s, int e) {
31 public IntLiteral(char[] token, int s, int e, int value) {
36 public IntLiteral(int intValue) {
37 // special optimized constructor : the cst is the argument
39 // value that should not be used
44 constant = Constant.fromValue(intValue);
49 public void computeConstant() {
50 // a special constant is use for the potential Integer.MAX_VALUE+1
51 // which is legal if used with a - as prefix....cool....
52 // notice that Integer.MIN_VALUE == -2147483648
54 long MAX = Integer.MAX_VALUE;
56 constant = Constant.One;
60 int length = source.length;
61 long computedValue = 0L;
62 if (source[0] == '0') {
63 MAX = 0xFFFFFFFFL; // a long in order to be positive !
65 constant = Constant.fromValue(0);
68 final int shift, radix;
70 if ((source[1] == 'x') | (source[1] == 'X')) {
79 while (source[j] == '0') {
80 j++; // jump over redondant zero
81 if (j == length) { // watch for 000000000000000000 :-(
82 constant = Constant.fromValue(value = (int) computedValue);
89 if ((digitValue = Character.digit(source[j++], radix)) < 0) {
90 constant = FORMAT_ERROR;
93 computedValue = (computedValue << shift) | digitValue;
94 if (computedValue > MAX)
95 return /* constant stays null */;
97 } else { // -----------regular case : radix = 10-----------
98 for (int i = 0; i < length; i++) {
100 if ((digitValue = Character.digit(source[i], 10)) < 0) {
101 constant = FORMAT_ERROR;
104 computedValue = 10 * computedValue + digitValue;
105 if (computedValue > MAX)
106 return /* constant stays null */;
110 constant = Constant.fromValue(value = (int) computedValue);
115 * Code generation for int literal
117 * @param currentScope
118 * net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
120 * net.sourceforge.phpdt.internal.compiler.codegen.CodeStream
121 * @param valueRequired
124 // public void generateCode(BlockScope currentScope, CodeStream codeStream,
125 // boolean valueRequired) {
126 // int pc = codeStream.position;
127 // if (valueRequired)
128 // if ((implicitConversion >> 4) == T_int)
129 // codeStream.generateInlinedValue(value);
131 // codeStream.generateConstant(constant, implicitConversion);
132 // codeStream.recordPositionsFrom(pc, this.sourceStart);
134 public TypeBinding literalType(BlockScope scope) {
138 public final boolean mayRepresentMIN_VALUE() {
139 // a special autorized int literral is 2147483648
140 // which is ONE over the limit. This special case
141 // only is used in combinaison with - to denote
142 // the minimal value of int -2147483648
144 return ((source.length == 10) && (source[0] == '2')
145 && (source[1] == '1') && (source[2] == '4')
146 && (source[3] == '7') && (source[4] == '4')
147 && (source[5] == '8') && (source[6] == '3')
148 && (source[7] == '6') && (source[8] == '4') && (source[9] == '8'));
151 public TypeBinding resolveType(BlockScope scope) {
152 // the format may be incorrect while the scanner could detect
153 // such an error only on painfull tests...easier and faster here
155 TypeBinding tb = super.resolveType(scope);
156 if (constant == FORMAT_ERROR) {
157 constant = NotAConstant;
158 scope.problemReporter().constantOutOfFormat(this);
159 this.resolvedType = null;
165 public String toStringExpression() {
168 /* special optimized IntLiteral that are created by the compiler */
169 return String.valueOf(value);
171 return super.toStringExpression();
174 public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope scope) {
175 visitor.visit(this, scope);
176 visitor.endVisit(this, scope);