|
1 /* |
|
2 * Copyright (c) 1999, 2012, 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 /** An interface for type tag values, which distinguish between different |
|
33 * sorts of types. |
|
34 * |
|
35 * <p><b>This is NOT part of any supported API. |
|
36 * If you write code that depends on this, you do so at your own risk. |
|
37 * This code and its internal interfaces are subject to change or |
|
38 * deletion without notice.</b> |
|
39 */ |
|
40 public enum TypeTag { |
|
41 /** The tag of the basic type `byte'. |
|
42 */ |
|
43 BYTE(1), |
|
44 |
|
45 /** The tag of the basic type `char'. |
|
46 */ |
|
47 CHAR(2), |
|
48 |
|
49 /** The tag of the basic type `short'. |
|
50 */ |
|
51 SHORT(3), |
|
52 |
|
53 /** The tag of the basic type `int'. |
|
54 */ |
|
55 INT(4), |
|
56 |
|
57 /** The tag of the basic type `long'. |
|
58 */ |
|
59 LONG(5), |
|
60 |
|
61 /** The tag of the basic type `float'. |
|
62 */ |
|
63 FLOAT(6), |
|
64 |
|
65 /** The tag of the basic type `double'. |
|
66 */ |
|
67 DOUBLE(7), |
|
68 |
|
69 /** The tag of the basic type `boolean'. |
|
70 */ |
|
71 BOOLEAN, |
|
72 |
|
73 /** The tag of the type `void'. |
|
74 */ |
|
75 VOID, |
|
76 |
|
77 /** The tag of all class and interface types. |
|
78 */ |
|
79 CLASS, |
|
80 |
|
81 /** The tag of all array types. |
|
82 */ |
|
83 ARRAY, |
|
84 |
|
85 /** The tag of all (monomorphic) method types. |
|
86 */ |
|
87 METHOD, |
|
88 |
|
89 /** The tag of all package "types". |
|
90 */ |
|
91 PACKAGE, |
|
92 |
|
93 /** The tag of all (source-level) type variables. |
|
94 */ |
|
95 TYPEVAR, |
|
96 |
|
97 /** The tag of all type arguments. |
|
98 */ |
|
99 WILDCARD, |
|
100 |
|
101 /** The tag of all polymorphic (method-) types. |
|
102 */ |
|
103 FORALL, |
|
104 |
|
105 /** The tag of deferred expression types in method context |
|
106 */ |
|
107 DEFERRED, |
|
108 |
|
109 /** The tag of the bottom type <null>. |
|
110 */ |
|
111 BOT, |
|
112 |
|
113 /** The tag of a missing type. |
|
114 */ |
|
115 NONE, |
|
116 |
|
117 /** The tag of the error type. |
|
118 */ |
|
119 ERROR, |
|
120 |
|
121 /** The tag of an unknown type |
|
122 */ |
|
123 UNKNOWN, |
|
124 |
|
125 /** The tag of all instantiatable type variables. |
|
126 */ |
|
127 UNDETVAR, |
|
128 |
|
129 /** Pseudo-types, these are special tags |
|
130 */ |
|
131 UNINITIALIZED_THIS, |
|
132 |
|
133 UNINITIALIZED_OBJECT; |
|
134 |
|
135 /** This field will only be used for tags related with numeric types for |
|
136 * optimization reasons. |
|
137 */ |
|
138 private int order = 0; |
|
139 |
|
140 private TypeTag() {} |
|
141 |
|
142 private TypeTag(int order) { |
|
143 this.order = order; |
|
144 } |
|
145 |
|
146 private static final int MIN_NUMERIC_TAG_ORDER = 1; |
|
147 private static final int MAX_NUMERIC_TAG_ORDER = 7; |
|
148 |
|
149 /** Returns the number of type tags. |
|
150 */ |
|
151 public static int getTypeTagCount() { |
|
152 // last two tags are not included in the total as long as they are pseudo-types |
|
153 return (UNDETVAR.ordinal() + 1); |
|
154 } |
|
155 |
|
156 public boolean isSubRangeOf(TypeTag range) { |
|
157 return (this == range) || isStrictSubRangeOf(range); |
|
158 } |
|
159 |
|
160 public boolean isStrictSubRangeOf(TypeTag range) { |
|
161 if (this.order >= MIN_NUMERIC_TAG_ORDER && this.order <= MAX_NUMERIC_TAG_ORDER && |
|
162 range.order >= MIN_NUMERIC_TAG_ORDER && this.order <= MAX_NUMERIC_TAG_ORDER) { |
|
163 if (this == range) |
|
164 return false; |
|
165 switch (this) { |
|
166 case BYTE: |
|
167 return true; |
|
168 case CHAR: case SHORT: case INT: |
|
169 case LONG: case FLOAT: |
|
170 return this.order < range.order && range.order <= MAX_NUMERIC_TAG_ORDER; |
|
171 default: |
|
172 return false; |
|
173 } |
|
174 } |
|
175 else |
|
176 return false; |
|
177 } |
|
178 |
|
179 public Kind getKindLiteral() { |
|
180 switch (this) { |
|
181 case INT: |
|
182 return Kind.INT_LITERAL; |
|
183 case LONG: |
|
184 return Kind.LONG_LITERAL; |
|
185 case FLOAT: |
|
186 return Kind.FLOAT_LITERAL; |
|
187 case DOUBLE: |
|
188 return Kind.DOUBLE_LITERAL; |
|
189 case BOOLEAN: |
|
190 return Kind.BOOLEAN_LITERAL; |
|
191 case CHAR: |
|
192 return Kind.CHAR_LITERAL; |
|
193 case CLASS: |
|
194 return Kind.STRING_LITERAL; |
|
195 case BOT: |
|
196 return Kind.NULL_LITERAL; |
|
197 default: |
|
198 throw new AssertionError("unknown literal kind " + this); |
|
199 } |
|
200 } |
|
201 |
|
202 public TypeKind getPrimitiveTypeKind() { |
|
203 switch (this) { |
|
204 case BOOLEAN: |
|
205 return TypeKind.BOOLEAN; |
|
206 case BYTE: |
|
207 return TypeKind.BYTE; |
|
208 case SHORT: |
|
209 return TypeKind.SHORT; |
|
210 case INT: |
|
211 return TypeKind.INT; |
|
212 case LONG: |
|
213 return TypeKind.LONG; |
|
214 case CHAR: |
|
215 return TypeKind.CHAR; |
|
216 case FLOAT: |
|
217 return TypeKind.FLOAT; |
|
218 case DOUBLE: |
|
219 return TypeKind.DOUBLE; |
|
220 case VOID: |
|
221 return TypeKind.VOID; |
|
222 default: |
|
223 throw new AssertionError("unknown primitive type " + this); |
|
224 } |
|
225 } |
|
226 } |