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