Tue, 13 Dec 2011 11:21:28 -0800
7120736: refactor javac option handling
Reviewed-by: mcimadamore
1 /*
2 * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.tools.javac.jvm;
28 import java.util.*;
30 import com.sun.tools.javac.code.Flags;
31 import com.sun.tools.javac.code.Symbol;
32 import com.sun.tools.javac.util.*;
34 import static com.sun.tools.javac.main.Option.*;
36 /** The classfile version target.
37 *
38 * <p><b>This is NOT part of any supported API.
39 * If you write code that depends on this, you do so at your own risk.
40 * This code and its internal interfaces are subject to change or
41 * deletion without notice.</b>
42 */
43 public enum Target {
44 JDK1_1("1.1", 45, 3),
45 JDK1_2("1.2", 46, 0),
46 JDK1_3("1.3", 47, 0),
48 /** J2SE1.4 = Merlin. */
49 JDK1_4("1.4", 48, 0),
51 /** Support for the JSR14 prototype compiler (targeting 1.4 VMs
52 * augmented with a few support classes). This is a transitional
53 * option that will not be supported in the product. */
54 JSR14("jsr14", 48, 0),
56 /** The following are undocumented transitional targets that we
57 * had used to test VM fixes in update releases. We do not
58 * promise to retain support for them. */
59 JDK1_4_1("1.4.1", 48, 0),
60 JDK1_4_2("1.4.2", 48, 0),
62 /** Tiger. */
63 JDK1_5("1.5", 49, 0),
65 /** JDK 6. */
66 JDK1_6("1.6", 50, 0),
68 /** JDK 7. */
69 JDK1_7("1.7", 51, 0),
71 /** JDK 8. */ // For now, a clone of 7
72 JDK1_8("1.8", 51, 0);
74 private static final Context.Key<Target> targetKey =
75 new Context.Key<Target>();
77 public static Target instance(Context context) {
78 Target instance = context.get(targetKey);
79 if (instance == null) {
80 Options options = Options.instance(context);
81 String targetString = options.get(TARGET);
82 if (targetString != null) instance = lookup(targetString);
83 if (instance == null) instance = DEFAULT;
84 context.put(targetKey, instance);
85 }
86 return instance;
87 }
89 private static Target MIN;
90 public static Target MIN() { return MIN; }
92 private static Target MAX;
93 public static Target MAX() { return MAX; }
95 private static Map<String,Target> tab = new HashMap<String,Target>();
96 static {
97 for (Target t : values()) {
98 if (MIN == null) MIN = t;
99 MAX = t;
100 tab.put(t.name, t);
101 }
102 tab.put("5", JDK1_5);
103 tab.put("6", JDK1_6);
104 tab.put("7", JDK1_7);
105 tab.put("8", JDK1_8);
106 }
108 public final String name;
109 public final int majorVersion;
110 public final int minorVersion;
111 private Target(String name, int majorVersion, int minorVersion) {
112 this.name = name;
113 this.majorVersion = majorVersion;
114 this.minorVersion = minorVersion;
115 }
117 public static final Target DEFAULT = JDK1_8;
119 public static Target lookup(String name) {
120 return tab.get(name);
121 }
123 /** In -target 1.1 and earlier, the compiler is required to emit
124 * synthetic method definitions in abstract classes for interface
125 * methods that are not overridden. We call them "Miranda" methods.
126 */
127 public boolean requiresIproxy() {
128 return compareTo(JDK1_1) <= 0;
129 }
131 /** Beginning in 1.4, we take advantage of the possibility of emitting
132 * code to initialize fields before calling the superclass constructor.
133 * This is allowed by the VM spec, but the verifier refused to allow
134 * it until 1.4. This is necesary to translate some code involving
135 * inner classes. See, for example, 4030374.
136 */
137 public boolean initializeFieldsBeforeSuper() {
138 return compareTo(JDK1_4) >= 0;
139 }
141 /** Beginning with -target 1.2 we obey the JLS rules for binary
142 * compatibility, emitting as the qualifying type of a reference
143 * to a method or field the type of the qualifier. In earlier
144 * targets we use as the qualifying type the class in which the
145 * member was found. The following methods named
146 * *binaryCompatibility() indicate places where we vary from this
147 * general rule. */
148 public boolean obeyBinaryCompatibility() {
149 return compareTo(JDK1_2) >= 0;
150 }
152 /** Starting in 1.5, the compiler uses an array type as
153 * the qualifier for method calls (such as clone) where required by
154 * the language and VM spec. Earlier versions of the compiler
155 * qualified them by Object.
156 */
157 public boolean arrayBinaryCompatibility() {
158 return compareTo(JDK1_5) >= 0;
159 }
161 /** Beginning after 1.2, we follow the binary compatibility rules for
162 * interface fields. The 1.2 VMs had bugs handling interface fields
163 * when compiled using binary compatibility (see 4400598), so this is
164 * an accommodation to them.
165 */
166 public boolean interfaceFieldsBinaryCompatibility() {
167 return compareTo(JDK1_2) > 0;
168 }
170 /** Beginning in -target 1.5, we follow the binary compatibility
171 * rules for interface methods that redefine Object methods.
172 * Earlier VMs had bugs handling such methods compiled using binary
173 * compatibility (see 4392595, 4398791, 4392595, 4400415).
174 * The VMs were fixed during or soon after 1.4. See 4392595.
175 */
176 public boolean interfaceObjectOverridesBinaryCompatibility() {
177 return compareTo(JDK1_5) >= 0;
178 }
180 /** Beginning in -target 1.4.2, we make synthetic variables
181 * package-private instead of private. This is to prevent the
182 * necessity of access methods, which effectively relax the
183 * protection of the field but bloat the class files and affect
184 * execution.
185 */
186 public boolean usePrivateSyntheticFields() {
187 return compareTo(JDK1_4_2) < 0;
188 }
190 /** Sometimes we need to create a field to cache a value like a
191 * class literal of the assertions flag. In -target 1.4.2 and
192 * later we create a new synthetic class for this instead of
193 * using the outermost class. See 4401576.
194 */
195 public boolean useInnerCacheClass() {
196 return compareTo(JDK1_4_2) >= 0;
197 }
199 /** Return true if cldc-style stack maps need to be generated. */
200 public boolean generateCLDCStackmap() {
201 return false;
202 }
204 /** Beginning in -target 6, we generate stackmap attribute in
205 * compact format. */
206 public boolean generateStackMapTable() {
207 return compareTo(JDK1_6) >= 0;
208 }
210 /** Beginning in -target 6, package-info classes are marked synthetic.
211 */
212 public boolean isPackageInfoSynthetic() {
213 return compareTo(JDK1_6) >= 0;
214 }
216 /** Do we generate "empty" stackmap slots after double and long?
217 */
218 public boolean generateEmptyAfterBig() {
219 return false;
220 }
222 /** Beginning in 1.5, we have an unsynchronized version of
223 * StringBuffer called StringBuilder that can be used by the
224 * compiler for string concatenation.
225 */
226 public boolean useStringBuilder() {
227 return compareTo(JDK1_5) >= 0;
228 }
230 /** Beginning in 1.5, we have flag bits we can use instead of
231 * marker attributes.
232 */
233 public boolean useSyntheticFlag() {
234 return compareTo(JDK1_5) >= 0;
235 }
236 public boolean useEnumFlag() {
237 return compareTo(JDK1_5) >= 0;
238 }
239 public boolean useAnnotationFlag() {
240 return compareTo(JDK1_5) >= 0;
241 }
242 public boolean useVarargsFlag() {
243 return compareTo(JDK1_5) >= 0;
244 }
245 public boolean useBridgeFlag() {
246 return compareTo(JDK1_5) >= 0;
247 }
249 /** Return the character to be used in constructing synthetic
250 * identifiers, where not specified by the JLS.
251 */
252 public char syntheticNameChar() {
253 return '$';
254 }
256 /** Does the VM have direct support for class literals?
257 */
258 public boolean hasClassLiterals() {
259 return compareTo(JDK1_5) >= 0;
260 }
262 /** Does the VM support an invokedynamic instruction?
263 */
264 public boolean hasInvokedynamic() {
265 return compareTo(JDK1_7) >= 0;
266 }
268 /** Does the VM support polymorphic method handle invocation?
269 * Affects the linkage information output to the classfile.
270 * An alias for {@code hasInvokedynamic}, since all the JSR 292 features appear together.
271 */
272 public boolean hasMethodHandles() {
273 return hasInvokedynamic();
274 }
276 /** Although we may not have support for class literals, should we
277 * avoid initializing the class that the literal refers to?
278 * See 4468823
279 */
280 public boolean classLiteralsNoInit() {
281 return compareTo(JDK1_4_2) >= 0;
282 }
284 /** Although we may not have support for class literals, when we
285 * throw a NoClassDefFoundError, should we initialize its cause?
286 */
287 public boolean hasInitCause() {
288 return compareTo(JDK1_4) >= 0;
289 }
291 /** For bootstrapping, we use J2SE1.4's wrapper class constructors
292 * to implement boxing.
293 */
294 public boolean boxWithConstructors() {
295 return compareTo(JDK1_5) < 0;
296 }
298 /** For bootstrapping, we use J2SE1.4's java.util.Collection
299 * instead of java.lang.Iterable.
300 */
301 public boolean hasIterable() {
302 return compareTo(JDK1_5) >= 0;
303 }
305 /** For bootstrapping javac only, we do without java.lang.Enum if
306 * necessary.
307 */
308 public boolean compilerBootstrap(Symbol c) {
309 return
310 this == JSR14 &&
311 (c.flags() & Flags.ENUM) != 0 &&
312 c.flatName().toString().startsWith("com.sun.tools.")
313 // && !Target.class.getSuperclass().getName().equals("java.lang.Enum")
314 ;
315 }
317 /** In J2SE1.5.0, we introduced the "EnclosingMethod" attribute
318 * for improved reflection support.
319 */
320 public boolean hasEnclosingMethodAttribute() {
321 return compareTo(JDK1_5) >= 0 || this == JSR14;
322 }
323 }