1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/TypeTag.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,240 @@ 1.4 +/* 1.5 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.javac.code; 1.30 + 1.31 +import com.sun.source.tree.Tree.Kind; 1.32 + 1.33 +import javax.lang.model.type.TypeKind; 1.34 + 1.35 +import static com.sun.tools.javac.code.TypeTag.NumericClasses.*; 1.36 + 1.37 +/** An interface for type tag values, which distinguish between different 1.38 + * sorts of types. 1.39 + * 1.40 + * <p><b>This is NOT part of any supported API. 1.41 + * If you write code that depends on this, you do so at your own risk. 1.42 + * This code and its internal interfaces are subject to change or 1.43 + * deletion without notice.</b> 1.44 + */ 1.45 +public enum TypeTag { 1.46 + /** The tag of the basic type `byte'. 1.47 + */ 1.48 + BYTE(BYTE_CLASS, BYTE_SUPERCLASSES, true), 1.49 + 1.50 + /** The tag of the basic type `char'. 1.51 + */ 1.52 + CHAR(CHAR_CLASS, CHAR_SUPERCLASSES, true), 1.53 + 1.54 + /** The tag of the basic type `short'. 1.55 + */ 1.56 + SHORT(SHORT_CLASS, SHORT_SUPERCLASSES, true), 1.57 + 1.58 + /** The tag of the basic type `long'. 1.59 + */ 1.60 + LONG(LONG_CLASS, LONG_SUPERCLASSES, true), 1.61 + 1.62 + /** The tag of the basic type `float'. 1.63 + */ 1.64 + FLOAT(FLOAT_CLASS, FLOAT_SUPERCLASSES, true), 1.65 + /** The tag of the basic type `int'. 1.66 + */ 1.67 + INT(INT_CLASS, INT_SUPERCLASSES, true), 1.68 + /** The tag of the basic type `double'. 1.69 + */ 1.70 + DOUBLE(DOUBLE_CLASS, DOUBLE_CLASS, true), 1.71 + /** The tag of the basic type `boolean'. 1.72 + */ 1.73 + BOOLEAN(0, 0, true), 1.74 + 1.75 + /** The tag of the type `void'. 1.76 + */ 1.77 + VOID, 1.78 + 1.79 + /** The tag of all class and interface types. 1.80 + */ 1.81 + CLASS, 1.82 + 1.83 + /** The tag of all array types. 1.84 + */ 1.85 + ARRAY, 1.86 + 1.87 + /** The tag of all (monomorphic) method types. 1.88 + */ 1.89 + METHOD, 1.90 + 1.91 + /** The tag of all package "types". 1.92 + */ 1.93 + PACKAGE, 1.94 + 1.95 + /** The tag of all (source-level) type variables. 1.96 + */ 1.97 + TYPEVAR, 1.98 + 1.99 + /** The tag of all type arguments. 1.100 + */ 1.101 + WILDCARD, 1.102 + 1.103 + /** The tag of all polymorphic (method-) types. 1.104 + */ 1.105 + FORALL, 1.106 + 1.107 + /** The tag of deferred expression types in method context 1.108 + */ 1.109 + DEFERRED, 1.110 + 1.111 + /** The tag of the bottom type {@code <null>}. 1.112 + */ 1.113 + BOT, 1.114 + 1.115 + /** The tag of a missing type. 1.116 + */ 1.117 + NONE, 1.118 + 1.119 + /** The tag of the error type. 1.120 + */ 1.121 + ERROR, 1.122 + 1.123 + /** The tag of an unknown type 1.124 + */ 1.125 + UNKNOWN, 1.126 + 1.127 + /** The tag of all instantiatable type variables. 1.128 + */ 1.129 + UNDETVAR, 1.130 + 1.131 + /** Pseudo-types, these are special tags 1.132 + */ 1.133 + UNINITIALIZED_THIS, 1.134 + 1.135 + UNINITIALIZED_OBJECT; 1.136 + 1.137 + final int superClasses; 1.138 + final int numericClass; 1.139 + final boolean isPrimitive; 1.140 + 1.141 + private TypeTag() { 1.142 + this(0, 0, false); 1.143 + } 1.144 + 1.145 + private TypeTag(int numericClass, int superClasses, boolean isPrimitive) { 1.146 + this.superClasses = superClasses; 1.147 + this.numericClass = numericClass; 1.148 + this.isPrimitive = isPrimitive; 1.149 + } 1.150 + 1.151 + public static class NumericClasses { 1.152 + public static final int BYTE_CLASS = 1; 1.153 + public static final int CHAR_CLASS = 2; 1.154 + public static final int SHORT_CLASS = 4; 1.155 + public static final int INT_CLASS = 8; 1.156 + public static final int LONG_CLASS = 16; 1.157 + public static final int FLOAT_CLASS = 32; 1.158 + public static final int DOUBLE_CLASS = 64; 1.159 + 1.160 + static final int BYTE_SUPERCLASSES = BYTE_CLASS | SHORT_CLASS | INT_CLASS | 1.161 + LONG_CLASS | FLOAT_CLASS | DOUBLE_CLASS; 1.162 + 1.163 + static final int CHAR_SUPERCLASSES = CHAR_CLASS | INT_CLASS | 1.164 + LONG_CLASS | FLOAT_CLASS | DOUBLE_CLASS; 1.165 + 1.166 + static final int SHORT_SUPERCLASSES = SHORT_CLASS | INT_CLASS | 1.167 + LONG_CLASS | FLOAT_CLASS | DOUBLE_CLASS; 1.168 + 1.169 + static final int INT_SUPERCLASSES = INT_CLASS | LONG_CLASS | FLOAT_CLASS | DOUBLE_CLASS; 1.170 + 1.171 + static final int LONG_SUPERCLASSES = LONG_CLASS | FLOAT_CLASS | DOUBLE_CLASS; 1.172 + 1.173 + static final int FLOAT_SUPERCLASSES = FLOAT_CLASS | DOUBLE_CLASS; 1.174 + } 1.175 + 1.176 + public boolean isStrictSubRangeOf(TypeTag tag) { 1.177 + /* Please don't change the implementation of this method to call method 1.178 + * isSubRangeOf. Both methods are called from hotspot code, the current 1.179 + * implementation is better performance-wise than the commented modification. 1.180 + */ 1.181 + return (this.superClasses & tag.numericClass) != 0 && this != tag; 1.182 + } 1.183 + 1.184 + public boolean isSubRangeOf(TypeTag tag) { 1.185 + return (this.superClasses & tag.numericClass) != 0; 1.186 + } 1.187 + 1.188 + /** Returns the number of type tags. 1.189 + */ 1.190 + public static int getTypeTagCount() { 1.191 + // last two tags are not included in the total as long as they are pseudo-types 1.192 + return (UNDETVAR.ordinal() + 1); 1.193 + } 1.194 + 1.195 + public Kind getKindLiteral() { 1.196 + switch (this) { 1.197 + case INT: 1.198 + return Kind.INT_LITERAL; 1.199 + case LONG: 1.200 + return Kind.LONG_LITERAL; 1.201 + case FLOAT: 1.202 + return Kind.FLOAT_LITERAL; 1.203 + case DOUBLE: 1.204 + return Kind.DOUBLE_LITERAL; 1.205 + case BOOLEAN: 1.206 + return Kind.BOOLEAN_LITERAL; 1.207 + case CHAR: 1.208 + return Kind.CHAR_LITERAL; 1.209 + case CLASS: 1.210 + return Kind.STRING_LITERAL; 1.211 + case BOT: 1.212 + return Kind.NULL_LITERAL; 1.213 + default: 1.214 + throw new AssertionError("unknown literal kind " + this); 1.215 + } 1.216 + } 1.217 + 1.218 + public TypeKind getPrimitiveTypeKind() { 1.219 + switch (this) { 1.220 + case BOOLEAN: 1.221 + return TypeKind.BOOLEAN; 1.222 + case BYTE: 1.223 + return TypeKind.BYTE; 1.224 + case SHORT: 1.225 + return TypeKind.SHORT; 1.226 + case INT: 1.227 + return TypeKind.INT; 1.228 + case LONG: 1.229 + return TypeKind.LONG; 1.230 + case CHAR: 1.231 + return TypeKind.CHAR; 1.232 + case FLOAT: 1.233 + return TypeKind.FLOAT; 1.234 + case DOUBLE: 1.235 + return TypeKind.DOUBLE; 1.236 + case VOID: 1.237 + return TypeKind.VOID; 1.238 + default: 1.239 + throw new AssertionError("unknown primitive type " + this); 1.240 + } 1.241 + } 1.242 + 1.243 +}