Tue, 28 Dec 2010 15:52:36 -0800
6962318: Update copyright year
Reviewed-by: xdono
duke@1 | 1 | /* |
ohair@240 | 2 | * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@158 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@158 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@158 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@158 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@158 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | /* |
duke@1 | 26 | * COMPONENT_NAME: idl.parser |
duke@1 | 27 | * |
duke@1 | 28 | * ORIGINS: 27 |
duke@1 | 29 | * |
duke@1 | 30 | * Licensed Materials - Property of IBM |
duke@1 | 31 | * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999 |
duke@1 | 32 | * RMI-IIOP v1.0 |
duke@1 | 33 | * |
duke@1 | 34 | */ |
duke@1 | 35 | |
duke@1 | 36 | package com.sun.tools.corba.se.idl.constExpr; |
duke@1 | 37 | |
duke@1 | 38 | // NOTES: |
duke@1 | 39 | |
duke@1 | 40 | import java.math.BigInteger; |
duke@1 | 41 | |
duke@1 | 42 | public abstract class Expression |
duke@1 | 43 | { |
duke@1 | 44 | /** |
duke@1 | 45 | * Compute the value of this expression. |
duke@1 | 46 | **/ |
duke@1 | 47 | public abstract Object evaluate () throws EvaluationException; |
duke@1 | 48 | |
duke@1 | 49 | /** |
duke@1 | 50 | * Set the value of this expression. |
duke@1 | 51 | **/ |
duke@1 | 52 | public void value (Object value) |
duke@1 | 53 | { |
duke@1 | 54 | _value = value; |
duke@1 | 55 | } |
duke@1 | 56 | /** |
duke@1 | 57 | * Get the value of this expression. |
duke@1 | 58 | **/ |
duke@1 | 59 | public Object value () |
duke@1 | 60 | { |
duke@1 | 61 | return _value; |
duke@1 | 62 | } |
duke@1 | 63 | |
duke@1 | 64 | /** |
duke@1 | 65 | * Set the representation of this expression. |
duke@1 | 66 | **/ |
duke@1 | 67 | public void rep (String rep) |
duke@1 | 68 | { |
duke@1 | 69 | _rep = rep; |
duke@1 | 70 | } |
duke@1 | 71 | /** |
duke@1 | 72 | * Get the representation of this expression. |
duke@1 | 73 | **/ |
duke@1 | 74 | public String rep () |
duke@1 | 75 | { |
duke@1 | 76 | return _rep; |
duke@1 | 77 | } |
duke@1 | 78 | |
duke@1 | 79 | /** |
duke@1 | 80 | * Set the target type of this expression. |
duke@1 | 81 | **/ |
duke@1 | 82 | public void type (String type) |
duke@1 | 83 | { |
duke@1 | 84 | _type = type; |
duke@1 | 85 | } |
duke@1 | 86 | /** |
duke@1 | 87 | * Get the target type of this expression. |
duke@1 | 88 | **/ |
duke@1 | 89 | public String type () |
duke@1 | 90 | { |
duke@1 | 91 | return _type; |
duke@1 | 92 | } |
duke@1 | 93 | |
duke@1 | 94 | /** |
duke@1 | 95 | * Return the default computation type for the given target type. |
duke@1 | 96 | **/ |
duke@1 | 97 | protected static String defaultType (String targetType) |
duke@1 | 98 | { |
duke@1 | 99 | return (targetType == null) ? new String ("") : targetType; |
duke@1 | 100 | } // defaultType |
duke@1 | 101 | |
duke@1 | 102 | // BigInteger is a multi-precision number whose representation contains |
duke@1 | 103 | // a signum (sign-number = 1, -1) and a magnitude. To support "long long", |
duke@1 | 104 | // all integer expressions are now performed over BigInteger and stored as |
duke@1 | 105 | // such. During the evaluation of an integer expression, the signum of its |
duke@1 | 106 | // value may toggle, which may cause the value of an expression to conflict |
duke@1 | 107 | // with its target type: [Case 1] If the resulting value is negative |
duke@1 | 108 | // (signum=-1) and the target type is unsigned; or [Case 2] if the resulting |
duke@1 | 109 | // value is positive (signum=1) and greater than 2**(target-type-length - 1), |
duke@1 | 110 | // and the target type is signed, then the resulting value will be out of |
duke@1 | 111 | // range. However, this value is correct and must be coerced to the target |
duke@1 | 112 | // type. E.G., After appying "not" to a BigInteger, the result is |
duke@1 | 113 | // a BigInteger that represents its 2's-complement (~5 => -6 in a byte-space). |
duke@1 | 114 | // In this example, the signum toggles and the magnatude is 6. If the target |
duke@1 | 115 | // type of this value were unsigned short, it must be coerced to a positive |
duke@1 | 116 | // number whose bits truly represent -6 in 2's-complement (250 in a byte-space). |
duke@1 | 117 | // |
duke@1 | 118 | // Also, floating types may now be intialized with any integer expression. |
duke@1 | 119 | // The result must be coerced to Double. |
duke@1 | 120 | // |
duke@1 | 121 | // Use the following routines to coerce this expression's value to its |
duke@1 | 122 | // "target" type. |
duke@1 | 123 | |
duke@1 | 124 | /** |
duke@1 | 125 | * Coerces a number to the target type of this expression. |
andrew@135 | 126 | * @param obj The number to coerce. |
duke@1 | 127 | * @return the value of number coerced to the (target) type of |
duke@1 | 128 | * this expression. |
duke@1 | 129 | **/ |
duke@1 | 130 | public Object coerceToTarget (Object obj) |
duke@1 | 131 | { |
duke@1 | 132 | if (obj instanceof BigInteger) |
duke@1 | 133 | { |
duke@1 | 134 | if (type ().indexOf ("unsigned") >= 0) |
duke@1 | 135 | return toUnsignedTarget ((BigInteger)obj); |
duke@1 | 136 | else |
duke@1 | 137 | return toSignedTarget ((BigInteger)obj); |
duke@1 | 138 | } |
duke@1 | 139 | return obj; |
duke@1 | 140 | } // coerceToTarget |
duke@1 | 141 | |
duke@1 | 142 | /** |
duke@1 | 143 | * Coerces an integral value (BigInteger) to its corresponding unsigned |
duke@1 | 144 | * representation, if the target type of this expression is unsigned. |
andrew@135 | 145 | * @param b The BigInteger to be coerced. |
duke@1 | 146 | * @return the value of an integral type coerced to its corresponding |
duke@1 | 147 | * unsigned integral type, if the target type of this expression is |
duke@1 | 148 | * unsigned. |
duke@1 | 149 | **/ |
duke@1 | 150 | protected BigInteger toUnsignedTarget (BigInteger b) |
duke@1 | 151 | { |
duke@1 | 152 | if (type ().equals ("unsigned short")) // target type of this expression |
duke@1 | 153 | { |
duke@1 | 154 | if (b != null && b.compareTo (zero) < 0) // error if value < min = -(2**(l-1)). |
duke@1 | 155 | return b.add (twoPow16); |
duke@1 | 156 | } |
duke@1 | 157 | else if (type ().equals ("unsigned long")) |
duke@1 | 158 | { |
duke@1 | 159 | if (b != null && b.compareTo (zero) < 0) |
duke@1 | 160 | return b.add (twoPow32); |
duke@1 | 161 | } |
duke@1 | 162 | else if (type ().equals ("unsigned long long")) |
duke@1 | 163 | { |
duke@1 | 164 | if (b != null && b.compareTo (zero) < 0) |
duke@1 | 165 | return b.add (twoPow64); |
duke@1 | 166 | } |
duke@1 | 167 | return b; |
duke@1 | 168 | } // toUnsignedTarget |
duke@1 | 169 | |
duke@1 | 170 | /** |
duke@1 | 171 | * Coerces an integral value (BigInteger) to its corresponding signed |
duke@1 | 172 | * representation, if the target type of this expression is signed. |
andrew@135 | 173 | * @param b The BigInteger to be coerced. |
duke@1 | 174 | * @return the value of an integral type coerced to its corresponding |
duke@1 | 175 | * signed integral type, if the target type of this expression is |
duke@1 | 176 | * signed. |
duke@1 | 177 | **/ |
duke@1 | 178 | protected BigInteger toSignedTarget (BigInteger b) |
duke@1 | 179 | { |
duke@1 | 180 | if (type ().equals ("short")) |
duke@1 | 181 | { |
duke@1 | 182 | if (b != null && b.compareTo (sMax) > 0) |
duke@1 | 183 | return b.subtract (twoPow16); |
duke@1 | 184 | } |
duke@1 | 185 | else if (type ().equals ("long")) |
duke@1 | 186 | { |
duke@1 | 187 | if (b != null && b.compareTo (lMax) > 0) |
duke@1 | 188 | return b.subtract (twoPow32); |
duke@1 | 189 | } |
duke@1 | 190 | else if (type ().equals ("long long")) |
duke@1 | 191 | { |
duke@1 | 192 | if (b != null && b.compareTo (llMax) > 0) |
duke@1 | 193 | return b.subtract (twoPow64); |
duke@1 | 194 | } |
duke@1 | 195 | return b; |
duke@1 | 196 | } // toSignedTarget |
duke@1 | 197 | |
duke@1 | 198 | /** |
duke@1 | 199 | * Return the unsigned value of a BigInteger. |
duke@1 | 200 | **/ |
duke@1 | 201 | protected BigInteger toUnsigned (BigInteger b) |
duke@1 | 202 | { |
duke@1 | 203 | if (b != null && b.signum () == -1) |
duke@1 | 204 | if (type ().equals ("short")) |
duke@1 | 205 | return b.add (twoPow16); |
duke@1 | 206 | else if (type ().equals ("long")) |
duke@1 | 207 | return b.add (twoPow32); |
duke@1 | 208 | else if (type ().equals ("long long")) |
duke@1 | 209 | return b.add (twoPow64); |
duke@1 | 210 | return b; |
duke@1 | 211 | } |
duke@1 | 212 | |
duke@1 | 213 | // Integral-type boundaries. |
duke@1 | 214 | |
duke@1 | 215 | public static final BigInteger negOne = BigInteger.valueOf (-1); |
duke@1 | 216 | public static final BigInteger zero = BigInteger.valueOf (0); |
duke@1 | 217 | public static final BigInteger one = BigInteger.valueOf (1); |
duke@1 | 218 | public static final BigInteger two = BigInteger.valueOf (2); |
duke@1 | 219 | |
duke@1 | 220 | public static final BigInteger twoPow15 = two.pow (15); |
duke@1 | 221 | public static final BigInteger twoPow16 = two.pow (16); |
duke@1 | 222 | public static final BigInteger twoPow31 = two.pow (31); |
duke@1 | 223 | public static final BigInteger twoPow32 = two.pow (32); |
duke@1 | 224 | public static final BigInteger twoPow63 = two.pow (63); |
duke@1 | 225 | public static final BigInteger twoPow64 = two.pow (64); |
duke@1 | 226 | |
duke@1 | 227 | public static final BigInteger sMax = BigInteger.valueOf (Short.MAX_VALUE); |
duke@1 | 228 | public static final BigInteger sMin = BigInteger.valueOf (Short.MAX_VALUE); |
duke@1 | 229 | |
duke@1 | 230 | public static final BigInteger usMax = sMax.multiply (two).add (one); |
duke@1 | 231 | public static final BigInteger usMin = zero; |
duke@1 | 232 | |
duke@1 | 233 | public static final BigInteger lMax = BigInteger.valueOf (Integer.MAX_VALUE); |
duke@1 | 234 | public static final BigInteger lMin = BigInteger.valueOf (Integer.MAX_VALUE); |
duke@1 | 235 | |
duke@1 | 236 | public static final BigInteger ulMax = lMax.multiply (two).add (one); |
duke@1 | 237 | public static final BigInteger ulMin = zero; |
duke@1 | 238 | |
duke@1 | 239 | public static final BigInteger llMax = BigInteger.valueOf (Long.MAX_VALUE); |
duke@1 | 240 | public static final BigInteger llMin = BigInteger.valueOf (Long.MIN_VALUE); |
duke@1 | 241 | |
duke@1 | 242 | public static final BigInteger ullMax = llMax.multiply (two).add (one); |
duke@1 | 243 | public static final BigInteger ullMin = zero; |
duke@1 | 244 | |
duke@1 | 245 | /** |
duke@1 | 246 | * Value of this expression: Boolean, Char, Byte, BigInteger, Double, |
duke@1 | 247 | * String, Expression, ConstEntry. |
duke@1 | 248 | **/ |
duke@1 | 249 | private Object _value = null; |
duke@1 | 250 | /** |
duke@1 | 251 | * String representation of this expression. |
duke@1 | 252 | **/ |
duke@1 | 253 | private String _rep = null; |
duke@1 | 254 | /** |
duke@1 | 255 | * Computation type of this (sub)expression = Target type for now. |
duke@1 | 256 | **/ |
duke@1 | 257 | private String _type = null; |
duke@1 | 258 | } // abstract class Expression |