added -J option to preserve unmodified files in preexisting jarfile
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / ast / FloatLiteral.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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
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.codegen.CodeStream;
15 import org.eclipse.jdt.internal.compiler.impl.Constant;
16 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
17 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
18 import org.eclipse.jdt.internal.compiler.util.FloatUtil;
19
20 public class FloatLiteral extends NumberLiteral {
21         float value;
22         final static float Float_MIN_VALUE = Float.intBitsToFloat(1); // work-around VAJ problem 1F6IGUU
23         public FloatLiteral(char[] token, int s, int e) {
24                 super(token, s, e);
25         }
26         public void computeConstant() {
27                 Float computedValue;
28                 try {
29                         computedValue = Float.valueOf(String.valueOf(source));
30                 } catch (NumberFormatException e) {
31                         // hex floating point literal
32                         // being rejected by 1.4 libraries where Float.valueOf(...) doesn't handle hex decimal floats
33                         try {
34                                 float v = FloatUtil.valueOfHexFloatLiteral(source);
35                                 if (v == Float.POSITIVE_INFINITY) {
36                                         // error: the number is too large to represent
37                                         return;
38                                 }
39                                 if (Float.isNaN(v)) {
40                                         // error: the number is too small to represent
41                                         return;
42                                 }
43                                 value = v;
44                                 constant = Constant.fromValue(v);
45                         } catch (NumberFormatException e1) {
46                                 // if the computation of the constant fails
47                         }
48                         return;
49                 }
50
51                 final float floatValue = computedValue.floatValue();
52                 if (floatValue > Float.MAX_VALUE) {
53                         // error: the number is too large to represent
54                         return;
55                 }
56                 if (floatValue < Float.MIN_VALUE) {
57                         // see 1F6IGUU
58                         // a true 0 only has '0' and '.' in mantissa
59                         // 1.0e-5000d is non-zero, but underflows to 0
60                         boolean isHexaDecimal = false;
61                         label : for (int i = 0; i < source.length; i++) { //it is welled formated so just test against '0' and potential . D d  
62                                 switch (source[i]) {
63                                         case '0' :
64                                         case '.' :
65                                                 break;
66                                         case 'x' :
67                                         case 'X' :
68                                                 isHexaDecimal = true;
69                                                 break;
70                                         case 'e' :
71                                         case 'E' :
72                                         case 'f' :
73                                         case 'F' :
74                                         case 'd' :
75                                         case 'D' :
76                                                 if (isHexaDecimal) {
77                                                         return;
78                                                 }
79                                                 // starting the exponent - mantissa is all zero
80                                                 // no exponent - mantissa is all zero
81                                                 break label;
82                                         case 'p' :
83                                         case 'P' :
84                                                 break label;
85                                         default :
86                                                 // error: the number is too small to represent
87                                                 return;
88                                 }
89                         }
90                 }
91                 value = floatValue;
92                 constant = Constant.fromValue(value);
93         }
94         /**
95          * Code generation for float literal
96          *
97          * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
98          * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
99          * @param valueRequired boolean
100          */
101         public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
102                 int pc = codeStream.position;
103                 if (valueRequired) {
104                         codeStream.generateConstant(constant, implicitConversion);
105                 }
106                 codeStream.recordPositionsFrom(pc, this.sourceStart);
107         }
108         public TypeBinding literalType(BlockScope scope) {
109                 return FloatBinding;
110         }
111         public void traverse(ASTVisitor visitor, BlockScope blockScope) {
112                 visitor.visit(this, blockScope);
113                 visitor.endVisit(this, blockScope);
114         }
115 }