1 /*
   2  * Copyright (c) 1999, 2014, 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.code;
  27 
  28 import com.sun.source.tree.Tree.Kind;
  29 
  30 import javax.lang.model.type.TypeKind;
  31 
  32 import static com.sun.tools.javac.code.TypeTag.NumericClasses.*;
  33 
  34 /** An interface for type tag values, which distinguish between different
  35  *  sorts of types.
  36  *
  37  *  <p><b>This is NOT part of any supported API.
  38  *  If you write code that depends on this, you do so at your own risk.
  39  *  This code and its internal interfaces are subject to change or
  40  *  deletion without notice.</b>
  41  */
  42 public enum TypeTag {
  43     /** The tag of the basic type `byte'.
  44      */
  45     BYTE(BYTE_CLASS, BYTE_SUPERCLASSES, true, byte.class),
  46 
  47     /** The tag of the basic type `char'.
  48      */
  49     CHAR(CHAR_CLASS, CHAR_SUPERCLASSES, true, char.class),
  50 
  51     /** The tag of the basic type `short'.
  52      */
  53     SHORT(SHORT_CLASS, SHORT_SUPERCLASSES, true, short.class),
  54 
  55     /** The tag of the basic type `long'.
  56      */
  57     LONG(LONG_CLASS, LONG_SUPERCLASSES, true, long.class),
  58 
  59     /** The tag of the basic type `float'.
  60      */
  61     FLOAT(FLOAT_CLASS, FLOAT_SUPERCLASSES, true, float.class),
  62     /** The tag of the basic type `int'.
  63      */
  64     INT(INT_CLASS, INT_SUPERCLASSES, true, int.class),
  65     /** The tag of the basic type `double'.
  66      */
  67     DOUBLE(DOUBLE_CLASS, DOUBLE_CLASS, true, double.class),
  68     /** The tag of the basic type `boolean'.
  69      */
  70     BOOLEAN(0, 0, true, boolean.class),
  71 
  72     /** The tag of the type `void'.
  73      */
  74     VOID(0, 0, false, void.class),
  75 
  76     /** The tag of all class and interface types.
  77      */
  78     CLASS,
  79 
  80     /** The tag of all array types.
  81      */
  82     ARRAY,
  83 
  84     /** The tag of all (monomorphic) method types.
  85      */
  86     METHOD,
  87 
  88     /** The tag of all package "types".
  89      */
  90     PACKAGE,
  91 
  92     /** The tag of all module "types".
  93      */
  94     MODULE,
  95 
  96     /** The tag of all (source-level) type variables.
  97      */
  98     TYPEVAR,
  99 
 100     /** The tag of all type arguments.
 101      */
 102     WILDCARD,
 103 
 104     /** The tag of all polymorphic (method-) types.
 105      */
 106     FORALL,
 107 
 108     /** The tag of deferred expression types in method context
 109      */
 110     DEFERRED,
 111 
 112     /** The tag of the bottom type {@code <null>}.
 113      */
 114     BOT,
 115 
 116     /** The tag of a missing type.
 117      */
 118     NONE,
 119 
 120     /** The tag of the error type.
 121      */
 122     ERROR,
 123 
 124     /** The tag of an unknown type
 125      */
 126     UNKNOWN,
 127 
 128     /** The tag of all instantiatable type variables.
 129      */
 130     UNDETVAR,
 131 
 132     /** Pseudo-types, these are special tags
 133      */
 134     UNINITIALIZED_THIS,
 135 
 136     UNINITIALIZED_OBJECT;
 137 
 138     final int superClasses;
 139     final int numericClass;
 140     final boolean isPrimitive;
 141     public final Class<?> theClass;
 142 
 143     private TypeTag() {
 144         this(0, 0, false, null);
 145     }
 146 
 147     private TypeTag(int numericClass, int superClasses, boolean isPrimitive, Class<?> theClass) {
 148         this.superClasses = superClasses;
 149         this.numericClass = numericClass;
 150         this.isPrimitive = isPrimitive;
 151         this.theClass = theClass;
 152     }
 153 
 154     public static class NumericClasses {
 155         public static final int BYTE_CLASS = 1;
 156         public static final int CHAR_CLASS = 2;
 157         public static final int SHORT_CLASS = 4;
 158         public static final int INT_CLASS = 8;
 159         public static final int LONG_CLASS = 16;
 160         public static final int FLOAT_CLASS = 32;
 161         public static final int DOUBLE_CLASS = 64;
 162 
 163         static final int BYTE_SUPERCLASSES = BYTE_CLASS | SHORT_CLASS | INT_CLASS |
 164                 LONG_CLASS | FLOAT_CLASS | DOUBLE_CLASS;
 165 
 166         static final int CHAR_SUPERCLASSES = CHAR_CLASS | INT_CLASS |
 167                 LONG_CLASS | FLOAT_CLASS | DOUBLE_CLASS;
 168 
 169         static final int SHORT_SUPERCLASSES = SHORT_CLASS | INT_CLASS |
 170                 LONG_CLASS | FLOAT_CLASS | DOUBLE_CLASS;
 171 
 172         static final int INT_SUPERCLASSES = INT_CLASS | LONG_CLASS | FLOAT_CLASS | DOUBLE_CLASS;
 173 
 174         static final int LONG_SUPERCLASSES = LONG_CLASS | FLOAT_CLASS | DOUBLE_CLASS;
 175 
 176         static final int FLOAT_SUPERCLASSES = FLOAT_CLASS | DOUBLE_CLASS;
 177     }
 178 
 179     public boolean isStrictSubRangeOf(TypeTag tag) {
 180         /*  Please don't change the implementation of this method to call method
 181          *  isSubRangeOf. Both methods are called from hotspot code, the current
 182          *  implementation is better performance-wise than the commented modification.
 183          */
 184         return (this.superClasses & tag.numericClass) != 0 && this != tag;
 185     }
 186 
 187     public boolean isSubRangeOf(TypeTag tag) {
 188         return (this.superClasses & tag.numericClass) != 0;
 189     }
 190 
 191     /** Returns the number of type tags.
 192      */
 193     public static int getTypeTagCount() {
 194         // last two tags are not included in the total as long as they are pseudo-types
 195         return (UNDETVAR.ordinal() + 1);
 196     }
 197 
 198     public Kind getKindLiteral() {
 199         switch (this) {
 200         case INT:
 201             return Kind.INT_LITERAL;
 202         case LONG:
 203             return Kind.LONG_LITERAL;
 204         case FLOAT:
 205             return Kind.FLOAT_LITERAL;
 206         case DOUBLE:
 207             return Kind.DOUBLE_LITERAL;
 208         case BOOLEAN:
 209             return Kind.BOOLEAN_LITERAL;
 210         case CHAR:
 211             return Kind.CHAR_LITERAL;
 212         case CLASS:
 213             return Kind.STRING_LITERAL;
 214         case BOT:
 215             return Kind.NULL_LITERAL;
 216         default:
 217             throw new AssertionError("unknown literal kind " + this);
 218         }
 219     }
 220 
 221     public TypeKind getPrimitiveTypeKind() {
 222         switch (this) {
 223         case BOOLEAN:
 224             return TypeKind.BOOLEAN;
 225         case BYTE:
 226             return TypeKind.BYTE;
 227         case SHORT:
 228             return TypeKind.SHORT;
 229         case INT:
 230             return TypeKind.INT;
 231         case LONG:
 232             return TypeKind.LONG;
 233         case CHAR:
 234             return TypeKind.CHAR;
 235         case FLOAT:
 236             return TypeKind.FLOAT;
 237         case DOUBLE:
 238             return TypeKind.DOUBLE;
 239         case VOID:
 240             return TypeKind.VOID;
 241         default:
 242             throw new AssertionError("unknown primitive type " + this);
 243         }
 244     }
 245 
 246     /** Returns true if the given value is within the allowed range for this type. */
 247     public boolean checkRange(int value) {
 248         switch (this) {
 249             case BOOLEAN:
 250                 return 0 <= value && value <= 1;
 251             case BYTE:
 252                 return Byte.MIN_VALUE <= value && value <= Byte.MAX_VALUE;
 253             case CHAR:
 254                 return Character.MIN_VALUE <= value && value <= Character.MAX_VALUE;
 255             case SHORT:
 256                 return Short.MIN_VALUE <= value && value <= Short.MAX_VALUE;
 257             case INT:
 258                 return true;
 259             default:
 260                 throw new AssertionError();
 261         }
 262     }
 263 }