Mon, 04 May 2009 21:04:04 -0700
6829189: Java programming with JSR 292 needs language support
Summary: Language changes documented in http://wikis.sun.com/display/mlvm/ProjectCoinProposal
Reviewed-by: jjg, darcy, mcimadamore
duke@1 | 1 | /* |
duke@1 | 2 | * Copyright 2002-2006 Sun Microsystems, Inc. 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 |
duke@1 | 7 | * published by the Free Software Foundation. Sun designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
duke@1 | 9 | * by Sun 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 | * |
duke@1 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@1 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@1 | 23 | * have any questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.jvm; |
duke@1 | 27 | |
duke@1 | 28 | import java.util.*; |
duke@1 | 29 | |
duke@1 | 30 | import com.sun.tools.javac.code.Flags; |
duke@1 | 31 | import com.sun.tools.javac.code.Symbol; |
duke@1 | 32 | import com.sun.tools.javac.util.*; |
duke@1 | 33 | |
duke@1 | 34 | /** The classfile version target. |
duke@1 | 35 | * |
duke@1 | 36 | * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
duke@1 | 37 | * you write code that depends on this, you do so at your own risk. |
duke@1 | 38 | * This code and its internal interfaces are subject to change or |
duke@1 | 39 | * deletion without notice.</b> |
duke@1 | 40 | */ |
duke@1 | 41 | public enum Target { |
duke@1 | 42 | JDK1_1("1.1", 45, 3), |
duke@1 | 43 | JDK1_2("1.2", 46, 0), |
duke@1 | 44 | JDK1_3("1.3", 47, 0), |
duke@1 | 45 | |
duke@1 | 46 | /** J2SE1.4 = Merlin. */ |
duke@1 | 47 | JDK1_4("1.4", 48, 0), |
duke@1 | 48 | |
duke@1 | 49 | /** Support for the JSR14 prototype compiler (targeting 1.4 VMs |
duke@1 | 50 | * augmented with a few support classes). This is a transitional |
duke@1 | 51 | * option that will not be supported in the product. */ |
duke@1 | 52 | JSR14("jsr14", 48, 0), |
duke@1 | 53 | |
duke@1 | 54 | /** The following are undocumented transitional targets that we |
duke@1 | 55 | * had used to test VM fixes in update releases. We do not |
duke@1 | 56 | * promise to retain support for them. */ |
duke@1 | 57 | JDK1_4_1("1.4.1", 48, 0), |
duke@1 | 58 | JDK1_4_2("1.4.2", 48, 0), |
duke@1 | 59 | |
duke@1 | 60 | /** Tiger. */ |
duke@1 | 61 | JDK1_5("1.5", 49, 0), |
duke@1 | 62 | |
duke@1 | 63 | /** JDK 6. */ |
duke@1 | 64 | JDK1_6("1.6", 50, 0), |
duke@1 | 65 | |
duke@1 | 66 | /** JDK 7. */ |
duke@1 | 67 | JDK1_7("1.7", 51, 0); |
duke@1 | 68 | |
duke@1 | 69 | private static final Context.Key<Target> targetKey = |
duke@1 | 70 | new Context.Key<Target>(); |
duke@1 | 71 | |
duke@1 | 72 | public static Target instance(Context context) { |
duke@1 | 73 | Target instance = context.get(targetKey); |
duke@1 | 74 | if (instance == null) { |
duke@1 | 75 | Options options = Options.instance(context); |
duke@1 | 76 | String targetString = options.get("-target"); |
duke@1 | 77 | if (targetString != null) instance = lookup(targetString); |
duke@1 | 78 | if (instance == null) instance = DEFAULT; |
duke@1 | 79 | context.put(targetKey, instance); |
duke@1 | 80 | } |
duke@1 | 81 | return instance; |
duke@1 | 82 | } |
duke@1 | 83 | |
duke@1 | 84 | private static Target MIN; |
duke@1 | 85 | public static Target MIN() { return MIN; } |
duke@1 | 86 | |
duke@1 | 87 | private static Target MAX; |
duke@1 | 88 | public static Target MAX() { return MAX; } |
duke@1 | 89 | |
duke@1 | 90 | private static Map<String,Target> tab = new HashMap<String,Target>(); |
duke@1 | 91 | static { |
duke@1 | 92 | for (Target t : values()) { |
duke@1 | 93 | if (MIN == null) MIN = t; |
duke@1 | 94 | MAX = t; |
duke@1 | 95 | tab.put(t.name, t); |
duke@1 | 96 | } |
duke@1 | 97 | tab.put("5", JDK1_5); |
duke@1 | 98 | tab.put("6", JDK1_6); |
duke@1 | 99 | tab.put("7", JDK1_7); |
duke@1 | 100 | } |
duke@1 | 101 | |
duke@1 | 102 | public final String name; |
duke@1 | 103 | public final int majorVersion; |
duke@1 | 104 | public final int minorVersion; |
duke@1 | 105 | private Target(String name, int majorVersion, int minorVersion) { |
duke@1 | 106 | this.name = name; |
duke@1 | 107 | this.majorVersion = majorVersion; |
duke@1 | 108 | this.minorVersion = minorVersion; |
duke@1 | 109 | } |
duke@1 | 110 | |
duke@1 | 111 | public static final Target DEFAULT = JDK1_6; |
duke@1 | 112 | |
duke@1 | 113 | public static Target lookup(String name) { |
duke@1 | 114 | return tab.get(name); |
duke@1 | 115 | } |
duke@1 | 116 | |
duke@1 | 117 | /** In -target 1.1 and earlier, the compiler is required to emit |
duke@1 | 118 | * synthetic method definitions in abstract classes for interface |
duke@1 | 119 | * methods that are not overridden. We call them "Miranda" methods. |
duke@1 | 120 | */ |
duke@1 | 121 | public boolean requiresIproxy() { |
duke@1 | 122 | return compareTo(JDK1_1) <= 0; |
duke@1 | 123 | } |
duke@1 | 124 | |
duke@1 | 125 | /** Beginning in 1.4, we take advantage of the possibility of emitting |
duke@1 | 126 | * code to initialize fields before calling the superclass constructor. |
duke@1 | 127 | * This is allowed by the VM spec, but the verifier refused to allow |
duke@1 | 128 | * it until 1.4. This is necesary to translate some code involving |
duke@1 | 129 | * inner classes. See, for example, 4030374. |
duke@1 | 130 | */ |
duke@1 | 131 | public boolean initializeFieldsBeforeSuper() { |
duke@1 | 132 | return compareTo(JDK1_4) >= 0; |
duke@1 | 133 | } |
duke@1 | 134 | |
duke@1 | 135 | /** Beginning with -target 1.2 we obey the JLS rules for binary |
duke@1 | 136 | * compatibility, emitting as the qualifying type of a reference |
duke@1 | 137 | * to a method or field the type of the qualifier. In earlier |
duke@1 | 138 | * targets we use as the qualifying type the class in which the |
duke@1 | 139 | * member was found. The following methods named |
duke@1 | 140 | * *binaryCompatibility() indicate places where we vary from this |
duke@1 | 141 | * general rule. */ |
duke@1 | 142 | public boolean obeyBinaryCompatibility() { |
duke@1 | 143 | return compareTo(JDK1_2) >= 0; |
duke@1 | 144 | } |
duke@1 | 145 | |
duke@1 | 146 | /** Starting in 1.5, the compiler uses an array type as |
duke@1 | 147 | * the qualifier for method calls (such as clone) where required by |
duke@1 | 148 | * the language and VM spec. Earlier versions of the compiler |
duke@1 | 149 | * qualified them by Object. |
duke@1 | 150 | */ |
duke@1 | 151 | public boolean arrayBinaryCompatibility() { |
duke@1 | 152 | return compareTo(JDK1_5) >= 0; |
duke@1 | 153 | } |
duke@1 | 154 | |
duke@1 | 155 | /** Beginning after 1.2, we follow the binary compatibility rules for |
duke@1 | 156 | * interface fields. The 1.2 VMs had bugs handling interface fields |
duke@1 | 157 | * when compiled using binary compatibility (see 4400598), so this is |
duke@1 | 158 | * an accommodation to them. |
duke@1 | 159 | */ |
duke@1 | 160 | public boolean interfaceFieldsBinaryCompatibility() { |
duke@1 | 161 | return compareTo(JDK1_2) > 0; |
duke@1 | 162 | } |
duke@1 | 163 | |
duke@1 | 164 | /** Beginning in -target 1.5, we follow the binary compatibility |
duke@1 | 165 | * rules for interface methods that redefine Object methods. |
duke@1 | 166 | * Earlier VMs had bugs handling such methods compiled using binary |
duke@1 | 167 | * compatibility (see 4392595, 4398791, 4392595, 4400415). |
duke@1 | 168 | * The VMs were fixed during or soon after 1.4. See 4392595. |
duke@1 | 169 | */ |
duke@1 | 170 | public boolean interfaceObjectOverridesBinaryCompatibility() { |
duke@1 | 171 | return compareTo(JDK1_5) >= 0; |
duke@1 | 172 | } |
duke@1 | 173 | |
duke@1 | 174 | /** Beginning in -target 1.4.2, we make synthetic variables |
duke@1 | 175 | * package-private instead of private. This is to prevent the |
duke@1 | 176 | * necessity of access methods, which effectively relax the |
duke@1 | 177 | * protection of the field but bloat the class files and affect |
duke@1 | 178 | * execution. |
duke@1 | 179 | */ |
duke@1 | 180 | public boolean usePrivateSyntheticFields() { |
duke@1 | 181 | return compareTo(JDK1_4_2) < 0; |
duke@1 | 182 | } |
duke@1 | 183 | |
duke@1 | 184 | /** Sometimes we need to create a field to cache a value like a |
duke@1 | 185 | * class literal of the assertions flag. In -target 1.4.2 and |
duke@1 | 186 | * later we create a new synthetic class for this instead of |
duke@1 | 187 | * using the outermost class. See 4401576. |
duke@1 | 188 | */ |
duke@1 | 189 | public boolean useInnerCacheClass() { |
duke@1 | 190 | return compareTo(JDK1_4_2) >= 0; |
duke@1 | 191 | } |
duke@1 | 192 | |
duke@1 | 193 | /** Return true if cldc-style stack maps need to be generated. */ |
duke@1 | 194 | public boolean generateCLDCStackmap() { |
duke@1 | 195 | return false; |
duke@1 | 196 | } |
duke@1 | 197 | |
duke@1 | 198 | /** Beginning in -target 6, we generate stackmap attribute in |
duke@1 | 199 | * compact format. */ |
duke@1 | 200 | public boolean generateStackMapTable() { |
duke@1 | 201 | return compareTo(JDK1_6) >= 0; |
duke@1 | 202 | } |
duke@1 | 203 | |
duke@1 | 204 | /** Beginning in -target 6, package-info classes are marked synthetic. |
duke@1 | 205 | */ |
duke@1 | 206 | public boolean isPackageInfoSynthetic() { |
duke@1 | 207 | return compareTo(JDK1_6) >= 0; |
duke@1 | 208 | } |
duke@1 | 209 | |
duke@1 | 210 | /** Do we generate "empty" stackmap slots after double and long? |
duke@1 | 211 | */ |
duke@1 | 212 | public boolean generateEmptyAfterBig() { |
duke@1 | 213 | return false; |
duke@1 | 214 | } |
duke@1 | 215 | |
duke@1 | 216 | /** Beginning in 1.5, we have an unsynchronized version of |
duke@1 | 217 | * StringBuffer called StringBuilder that can be used by the |
duke@1 | 218 | * compiler for string concatenation. |
duke@1 | 219 | */ |
duke@1 | 220 | public boolean useStringBuilder() { |
duke@1 | 221 | return compareTo(JDK1_5) >= 0; |
duke@1 | 222 | } |
duke@1 | 223 | |
duke@1 | 224 | /** Beginning in 1.5, we have flag bits we can use instead of |
duke@1 | 225 | * marker attributes. |
duke@1 | 226 | */ |
duke@1 | 227 | public boolean useSyntheticFlag() { |
duke@1 | 228 | return compareTo(JDK1_5) >= 0; |
duke@1 | 229 | } |
duke@1 | 230 | public boolean useEnumFlag() { |
duke@1 | 231 | return compareTo(JDK1_5) >= 0; |
duke@1 | 232 | } |
duke@1 | 233 | public boolean useAnnotationFlag() { |
duke@1 | 234 | return compareTo(JDK1_5) >= 0; |
duke@1 | 235 | } |
duke@1 | 236 | public boolean useVarargsFlag() { |
duke@1 | 237 | return compareTo(JDK1_5) >= 0; |
duke@1 | 238 | } |
duke@1 | 239 | public boolean useBridgeFlag() { |
duke@1 | 240 | return compareTo(JDK1_5) >= 0; |
duke@1 | 241 | } |
duke@1 | 242 | |
duke@1 | 243 | /** Return the character to be used in constructing synthetic |
duke@1 | 244 | * identifiers, where not specified by the JLS. |
duke@1 | 245 | */ |
duke@1 | 246 | public char syntheticNameChar() { |
duke@1 | 247 | return '$'; |
duke@1 | 248 | } |
duke@1 | 249 | |
duke@1 | 250 | /** Does the VM have direct support for class literals? |
duke@1 | 251 | */ |
duke@1 | 252 | public boolean hasClassLiterals() { |
duke@1 | 253 | return compareTo(JDK1_5) >= 0; |
duke@1 | 254 | } |
duke@1 | 255 | |
jrose@267 | 256 | /** Does the VM support an invokedynamic instruction? |
jrose@267 | 257 | */ |
jrose@267 | 258 | public boolean hasInvokedynamic() { |
jrose@267 | 259 | return compareTo(JDK1_7) >= 0; |
jrose@267 | 260 | } |
jrose@267 | 261 | |
duke@1 | 262 | /** Although we may not have support for class literals, should we |
duke@1 | 263 | * avoid initializing the class that the literal refers to? |
duke@1 | 264 | * See 4468823 |
duke@1 | 265 | */ |
duke@1 | 266 | public boolean classLiteralsNoInit() { |
duke@1 | 267 | return compareTo(JDK1_4_2) >= 0; |
duke@1 | 268 | } |
duke@1 | 269 | |
duke@1 | 270 | /** Although we may not have support for class literals, when we |
duke@1 | 271 | * throw a NoClassDefFoundError, should we initialize its cause? |
duke@1 | 272 | */ |
duke@1 | 273 | public boolean hasInitCause() { |
duke@1 | 274 | return compareTo(JDK1_4) >= 0; |
duke@1 | 275 | } |
duke@1 | 276 | |
duke@1 | 277 | /** For bootstrapping, we use J2SE1.4's wrapper class constructors |
duke@1 | 278 | * to implement boxing. |
duke@1 | 279 | */ |
duke@1 | 280 | public boolean boxWithConstructors() { |
duke@1 | 281 | return compareTo(JDK1_5) < 0; |
duke@1 | 282 | } |
duke@1 | 283 | |
duke@1 | 284 | /** For bootstrapping, we use J2SE1.4's java.util.Collection |
duke@1 | 285 | * instead of java.lang.Iterable. |
duke@1 | 286 | */ |
duke@1 | 287 | public boolean hasIterable() { |
duke@1 | 288 | return compareTo(JDK1_5) >= 0; |
duke@1 | 289 | } |
duke@1 | 290 | |
duke@1 | 291 | /** For bootstrapping javac only, we do without java.lang.Enum if |
duke@1 | 292 | * necessary. |
duke@1 | 293 | */ |
duke@1 | 294 | public boolean compilerBootstrap(Symbol c) { |
duke@1 | 295 | return |
duke@1 | 296 | this == JSR14 && |
duke@1 | 297 | (c.flags() & Flags.ENUM) != 0 && |
duke@1 | 298 | c.flatName().toString().startsWith("com.sun.tools.") |
duke@1 | 299 | // && !Target.class.getSuperclass().getName().equals("java.lang.Enum") |
duke@1 | 300 | ; |
duke@1 | 301 | } |
duke@1 | 302 | |
duke@1 | 303 | /** In J2SE1.5.0, we introduced the "EnclosingMethod" attribute |
duke@1 | 304 | * for improved reflection support. |
duke@1 | 305 | */ |
duke@1 | 306 | public boolean hasEnclosingMethodAttribute() { |
duke@1 | 307 | return compareTo(JDK1_5) >= 0 || this == JSR14; |
duke@1 | 308 | } |
duke@1 | 309 | } |