1 /*
   2  * Copyright (c) 2002, 2013, 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  */
  25 
  26 package com.sun.tools.javac.jvm;
  27 
  28 import java.util.*;
  29 
  30 import com.sun.tools.javac.code.Flags;
  31 import com.sun.tools.javac.code.Symbol;
  32 import com.sun.tools.javac.util.*;
  33 
  34 import static com.sun.tools.javac.main.Option.TARGET;
  35 
  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),
  47 
  48     /** J2SE1.4 = Merlin. */
  49     JDK1_4("1.4", 48, 0),
  50 
  51     /** JDK 5, codename Tiger. */
  52     JDK1_5("1.5", 49, 0),
  53 
  54     /** JDK 6. */
  55     JDK1_6("1.6", 50, 0),
  56 
  57     /** JDK 7. */
  58     JDK1_7("1.7", 51, 0),
  59 
  60     /** JDK 8. */
  61     JDK1_8("1.8", 52, 0),
  62 
  63     /** JDK 9, initially an alias for 8. */
  64     JDK1_9("1.9", 52, 0);
  65 
  66     private static final Context.Key<Target> targetKey =
  67         new Context.Key<Target>();
  68 
  69     public static Target instance(Context context) {
  70         Target instance = context.get(targetKey);
  71         if (instance == null) {
  72             Options options = Options.instance(context);
  73             String targetString = options.get(TARGET);
  74             if (targetString != null) instance = lookup(targetString);
  75             if (instance == null) instance = DEFAULT;
  76             context.put(targetKey, instance);
  77         }
  78         return instance;
  79     }
  80 
  81     private static final Target MIN = values()[0];
  82     public static Target MIN() { return MIN; }
  83 
  84     private static final Target MAX = values()[values().length - 1];
  85     public static Target MAX() { return MAX; }
  86 
  87     private static final Map<String,Target> tab = new HashMap<>();
  88     static {
  89         for (Target t : values()) {
  90             tab.put(t.name, t);
  91         }
  92         tab.put("5", JDK1_5);
  93         tab.put("6", JDK1_6);
  94         tab.put("7", JDK1_7);
  95         tab.put("8", JDK1_8);
  96         tab.put("9", JDK1_9);
  97     }
  98 
  99     public final String name;
 100     public final int majorVersion;
 101     public final int minorVersion;
 102     private Target(String name, int majorVersion, int minorVersion) {
 103         this.name = name;
 104         this.majorVersion = majorVersion;
 105         this.minorVersion = minorVersion;
 106     }
 107 
 108     public static final Target DEFAULT = JDK1_9;
 109 
 110     public static Target lookup(String name) {
 111         return tab.get(name);
 112     }
 113 
 114     /** In -target 1.1 and earlier, the compiler is required to emit
 115      *  synthetic method definitions in abstract classes for interface
 116      *  methods that are not overridden.  We call them "Miranda" methods.
 117      */
 118     public boolean requiresIproxy() {
 119         return compareTo(JDK1_1) <= 0;
 120     }
 121 
 122     /** Beginning in 1.4, we take advantage of the possibility of emitting
 123      *  code to initialize fields before calling the superclass constructor.
 124      *  This is allowed by the VM spec, but the verifier refused to allow
 125      *  it until 1.4.  This is necesary to translate some code involving
 126      *  inner classes.  See, for example, 4030374.
 127      */
 128     public boolean initializeFieldsBeforeSuper() {
 129         return compareTo(JDK1_4) >= 0;
 130     }
 131 
 132     /** Beginning with -target 1.2 we obey the JLS rules for binary
 133      *  compatibility, emitting as the qualifying type of a reference
 134      *  to a method or field the type of the qualifier.  In earlier
 135      *  targets we use as the qualifying type the class in which the
 136      *  member was found.  The following methods named
 137      *  *binaryCompatibility() indicate places where we vary from this
 138      *  general rule. */
 139     public boolean obeyBinaryCompatibility() {
 140         return compareTo(JDK1_2) >= 0;
 141     }
 142 
 143     /** Starting in 1.5, the compiler uses an array type as
 144      *  the qualifier for method calls (such as clone) where required by
 145      *  the language and VM spec.  Earlier versions of the compiler
 146      *  qualified them by Object.
 147      */
 148     public boolean arrayBinaryCompatibility() {
 149         return compareTo(JDK1_5) >= 0;
 150     }
 151 
 152     /** Beginning after 1.2, we follow the binary compatibility rules for
 153      *  interface fields.  The 1.2 VMs had bugs handling interface fields
 154      *  when compiled using binary compatibility (see 4400598), so this is
 155      *  an accommodation to them.
 156      */
 157     public boolean interfaceFieldsBinaryCompatibility() {
 158         return compareTo(JDK1_2) > 0;
 159     }
 160 
 161     /** Beginning in -target 1.5, we follow the binary compatibility
 162      *  rules for interface methods that redefine Object methods.
 163      *  Earlier VMs had bugs handling such methods compiled using binary
 164      *  compatibility (see 4392595, 4398791, 4392595, 4400415).
 165      *  The VMs were fixed during or soon after 1.4.  See 4392595.
 166      */
 167     public boolean interfaceObjectOverridesBinaryCompatibility() {
 168         return compareTo(JDK1_5) >= 0;
 169     }
 170 
 171     /** Beginning in -target 1.5, we make synthetic variables
 172      *  package-private instead of private.  This is to prevent the
 173      *  necessity of access methods, which effectively relax the
 174      *  protection of the field but bloat the class files and affect
 175      *  execution.
 176      */
 177     public boolean usePrivateSyntheticFields() {
 178         return compareTo(JDK1_5) < 0;
 179     }
 180 
 181     /** Sometimes we need to create a field to cache a value like a
 182      *  class literal of the assertions flag.  In -target 1.5 and
 183      *  later we create a new synthetic class for this instead of
 184      *  using the outermost class.  See 4401576.
 185      */
 186     public boolean useInnerCacheClass() {
 187         return compareTo(JDK1_5) >= 0;
 188     }
 189 
 190     /** Return true if cldc-style stack maps need to be generated. */
 191     public boolean generateCLDCStackmap() {
 192         return false;
 193     }
 194 
 195     /** Beginning in -target 6, we generate stackmap attribute in
 196      *  compact format. */
 197     public boolean generateStackMapTable() {
 198         return compareTo(JDK1_6) >= 0;
 199     }
 200 
 201     /** Beginning in -target 6, package-info classes are marked synthetic.
 202      */
 203     public boolean isPackageInfoSynthetic() {
 204         return compareTo(JDK1_6) >= 0;
 205     }
 206 
 207     /** Do we generate "empty" stackmap slots after double and long?
 208      */
 209     public boolean generateEmptyAfterBig() {
 210         return false;
 211     }
 212 
 213     /** Beginning in 1.5, we have an unsynchronized version of
 214      *  StringBuffer called StringBuilder that can be used by the
 215      *  compiler for string concatenation.
 216      */
 217     public boolean useStringBuilder() {
 218         return compareTo(JDK1_5) >= 0;
 219     }
 220 
 221     /** Beginning in 1.5, we have flag bits we can use instead of
 222      *  marker attributes.
 223      */
 224     public boolean useSyntheticFlag() {
 225         return compareTo(JDK1_5) >= 0;
 226     }
 227     public boolean useEnumFlag() {
 228         return compareTo(JDK1_5) >= 0;
 229     }
 230     public boolean useAnnotationFlag() {
 231         return compareTo(JDK1_5) >= 0;
 232     }
 233     public boolean useVarargsFlag() {
 234         return compareTo(JDK1_5) >= 0;
 235     }
 236     public boolean useBridgeFlag() {
 237         return compareTo(JDK1_5) >= 0;
 238     }
 239 
 240     /** Return the character to be used in constructing synthetic
 241      *  identifiers, where not specified by the JLS.
 242      */
 243     public char syntheticNameChar() {
 244         return '$';
 245     }
 246 
 247     /** Does the VM have direct support for class literals?
 248      */
 249     public boolean hasClassLiterals() {
 250         return compareTo(JDK1_5) >= 0;
 251     }
 252 
 253     /** Does the VM support an invokedynamic instruction?
 254      */
 255     public boolean hasInvokedynamic() {
 256         return compareTo(JDK1_7) >= 0;
 257     }
 258 
 259     /** Does the VM support polymorphic method handle invocation?
 260      *  Affects the linkage information output to the classfile.
 261      *  An alias for {@code hasInvokedynamic}, since all the JSR 292 features appear together.
 262      */
 263     public boolean hasMethodHandles() {
 264         return hasInvokedynamic();
 265     }
 266 
 267     /** Although we may not have support for class literals, should we
 268      *  avoid initializing the class that the literal refers to?
 269      *  See 4468823
 270      */
 271     public boolean classLiteralsNoInit() {
 272         return compareTo(JDK1_5) >= 0;
 273     }
 274 
 275     /** Although we may not have support for class literals, when we
 276      *  throw a NoClassDefFoundError, should we initialize its cause?
 277      */
 278     public boolean hasInitCause() {
 279         return compareTo(JDK1_4) >= 0;
 280     }
 281 
 282     /** For bootstrapping, we use J2SE1.4's wrapper class constructors
 283      *  to implement boxing.
 284      */
 285     public boolean boxWithConstructors() {
 286         return compareTo(JDK1_5) < 0;
 287     }
 288 
 289     /** For bootstrapping, we use J2SE1.4's java.util.Collection
 290      *  instead of java.lang.Iterable.
 291      */
 292     public boolean hasIterable() {
 293         return compareTo(JDK1_5) >= 0;
 294     }
 295 
 296     /** In J2SE1.5.0, we introduced the "EnclosingMethod" attribute
 297      *  for improved reflection support.
 298      */
 299     public boolean hasEnclosingMethodAttribute() {
 300         return compareTo(JDK1_5) >= 0;
 301     }
 302 }