KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > ast > IntLiteral


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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);//used for ++ and --
24

25     static final Constant FORMAT_ERROR = DoubleConstant.fromValue(1.0/0.0); // NaN;
26
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     //special optimized constructor : the cst is the argument
35

36     //value that should not be used
37
// tokens = null ;
38
// sourceStart = 0;
39
// sourceEnd = 0;
40
super(null,0,0);
41     constant = IntConstant.fromValue(intValue);
42     value = intValue;
43     
44 }
45 public void computeConstant() {
46     //a special constant is use for the potential Integer.MAX_VALUE+1
47
//which is legal if used with a - as prefix....cool....
48
//notice that Integer.MIN_VALUE == -2147483648
49

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 ; //a long in order to be positive !
57
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++; //jump over redondant zero
66
if (j == length)
67             { //watch for 000000000000000000
68
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 /*constant stays null*/ ;}}
77     else
78     { //-----------regular case : radix = 10-----------
79
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 /*constant stays null*/ ; }}
85
86     constant = IntConstant.fromValue(value = (int)computedValue);
87         
88 }
89 /**
90  * Code generation for int literal
91  *
92  * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
93  * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
94  * @param valueRequired boolean
95  */

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     //a special autorized int literral is 2147483648
108
//which is ONE over the limit. This special case
109
//only is used in combinaison with - to denote
110
//the minimal value of int -2147483648
111

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     // the format may be incorrect while the scanner could detect
127
// such an error only on painfull tests...easier and faster here
128

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 JavaDoc printExpression(int indent, StringBuffer JavaDoc output){
139
140     if (source == null) {
141     /* special optimized IntLiteral that are created by the compiler */
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