1 /* |
1 /* |
2 * Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
29 import com.sun.tools.javac.jvm.*; |
29 import com.sun.tools.javac.jvm.*; |
30 import com.sun.tools.javac.util.*; |
30 import com.sun.tools.javac.util.*; |
31 |
31 |
32 import com.sun.tools.javac.code.Type.*; |
32 import com.sun.tools.javac.code.Type.*; |
33 |
33 |
34 import static com.sun.tools.javac.code.TypeTags.*; |
34 import static com.sun.tools.javac.code.TypeTag.BOOLEAN; |
|
35 |
35 import static com.sun.tools.javac.jvm.ByteCodes.*; |
36 import static com.sun.tools.javac.jvm.ByteCodes.*; |
36 |
37 |
37 /** Helper class for constant folding, used by the attribution phase. |
38 /** Helper class for constant folding, used by the attribution phase. |
38 * This class is marked strictfp as mandated by JLS 15.4. |
39 * This class is marked strictfp as mandated by JLS 15.4. |
39 * |
40 * |
174 case idiv: |
175 case idiv: |
175 return syms.intType.constType(intValue(l) / intValue(r)); |
176 return syms.intType.constType(intValue(l) / intValue(r)); |
176 case imod: |
177 case imod: |
177 return syms.intType.constType(intValue(l) % intValue(r)); |
178 return syms.intType.constType(intValue(l) % intValue(r)); |
178 case iand: |
179 case iand: |
179 return (left.tag == BOOLEAN |
180 return (left.hasTag(BOOLEAN) |
180 ? syms.booleanType : syms.intType) |
181 ? syms.booleanType : syms.intType) |
181 .constType(intValue(l) & intValue(r)); |
182 .constType(intValue(l) & intValue(r)); |
182 case bool_and: |
183 case bool_and: |
183 return syms.booleanType.constType(b2i((intValue(l) & intValue(r)) != 0)); |
184 return syms.booleanType.constType(b2i((intValue(l) & intValue(r)) != 0)); |
184 case ior: |
185 case ior: |
185 return (left.tag == BOOLEAN |
186 return (left.hasTag(BOOLEAN) |
186 ? syms.booleanType : syms.intType) |
187 ? syms.booleanType : syms.intType) |
187 .constType(intValue(l) | intValue(r)); |
188 .constType(intValue(l) | intValue(r)); |
188 case bool_or: |
189 case bool_or: |
189 return syms.booleanType.constType(b2i((intValue(l) | intValue(r)) != 0)); |
190 return syms.booleanType.constType(b2i((intValue(l) | intValue(r)) != 0)); |
190 case ixor: |
191 case ixor: |
191 return (left.tag == BOOLEAN |
192 return (left.hasTag(BOOLEAN) |
192 ? syms.booleanType : syms.intType) |
193 ? syms.booleanType : syms.intType) |
193 .constType(intValue(l) ^ intValue(r)); |
194 .constType(intValue(l) ^ intValue(r)); |
194 case ishl: case ishll: |
195 case ishl: case ishll: |
195 return syms.intType.constType(intValue(l) << intValue(r)); |
196 return syms.intType.constType(intValue(l) << intValue(r)); |
196 case ishr: case ishrl: |
197 case ishr: case ishrl: |
324 } |
325 } |
325 } |
326 } |
326 |
327 |
327 /** Coerce constant type to target type. |
328 /** Coerce constant type to target type. |
328 * @param etype The source type of the coercion, |
329 * @param etype The source type of the coercion, |
329 * which is assumed to be a constant type compatble with |
330 * which is assumed to be a constant type compatible with |
330 * ttype. |
331 * ttype. |
331 * @param ttype The target type of the coercion. |
332 * @param ttype The target type of the coercion. |
332 */ |
333 */ |
333 Type coerce(Type etype, Type ttype) { |
334 Type coerce(Type etype, Type ttype) { |
334 // WAS if (etype.baseType() == ttype.baseType()) |
335 // WAS if (etype.baseType() == ttype.baseType()) |
335 if (etype.tsym.type == ttype.tsym.type) |
336 if (etype.tsym.type == ttype.tsym.type) |
336 return etype; |
337 return etype; |
337 if (etype.tag <= DOUBLE) { |
338 if (etype.isNumeric()) { |
338 Object n = etype.constValue(); |
339 Object n = etype.constValue(); |
339 switch (ttype.tag) { |
340 switch (ttype.getTag()) { |
340 case BYTE: |
341 case BYTE: |
341 return syms.byteType.constType(0 + (byte)intValue(n)); |
342 return syms.byteType.constType(0 + (byte)intValue(n)); |
342 case CHAR: |
343 case CHAR: |
343 return syms.charType.constType(0 + (char)intValue(n)); |
344 return syms.charType.constType(0 + (char)intValue(n)); |
344 case SHORT: |
345 case SHORT: |