1 /*
2  * Copyright (c) 1999, 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.code;
27 
28 import java.util.EnumSet;
29 import java.util.Locale;
30 
31 import com.sun.source.tree.MemberReferenceTree;
32 import com.sun.tools.javac.api.Formattable;
33 import com.sun.tools.javac.api.Messages;
34 import static com.sun.tools.javac.code.Flags.*;
35 import static com.sun.tools.javac.code.TypeTag.CLASS;
36 import static com.sun.tools.javac.code.TypeTag.PACKAGE;
37 import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
38 
39 /** Internal symbol kinds, which distinguish between elements of
40  *  different subclasses of Symbol. Symbol kinds are organized so they can be
41  *  or'ed to sets.
42  *
43  *  <p><b>This is NOT part of any supported API.
44  *  If you write code that depends on this, you do so at your own risk.
45  *  This code and its internal interfaces are subject to change or
46  *  deletion without notice.</b>
47  */
48 public class Kinds {
49 
Kinds()50     private Kinds() {} // uninstantiable
51 
52     /** The empty set of kinds.
53      */
54     public final static int NIL = 0;
55 
56     /** The kind of package symbols.
57      */
58     public final static int PCK = 1 << 0;
59 
60     /** The kind of type symbols (classes, interfaces and type variables).
61      */
62     public final static int TYP = 1 << 1;
63 
64     /** The kind of variable symbols.
65      */
66     public final static int VAR = 1 << 2;
67 
68     /** The kind of values (variables or non-variable expressions), includes VAR.
69      */
70     public final static int VAL = (1 << 3) | VAR;
71 
72     /** The kind of methods.
73      */
74     public final static int MTH = 1 << 4;
75 
76     /** Poly kind, for deferred types.
77      */
78     public final static int POLY = 1 << 5;
79 
80     /** The error kind, which includes all other kinds.
81      */
82     public final static int ERR = (1 << 6) - 1;
83 
84     /** The set of all kinds.
85      */
86     public final static int AllKinds = ERR;
87 
88     /** Kinds for erroneous symbols that complement the above
89      */
90     public static final int ERRONEOUS           = 1 << 7;
91     public static final int AMBIGUOUS           = ERRONEOUS + 1;  // ambiguous reference
92     public static final int HIDDEN              = ERRONEOUS + 2;  // hidden method or field
93     public static final int STATICERR           = ERRONEOUS + 3;  // nonstatic member from static context
94     public static final int MISSING_ENCL        = ERRONEOUS + 4;  // missing enclosing class
95     public static final int ABSENT_VAR          = ERRONEOUS + 5;  // missing variable
96     public static final int WRONG_MTHS          = ERRONEOUS + 6;  // methods with wrong arguments
97     public static final int WRONG_MTH           = ERRONEOUS + 7;  // one method with wrong arguments
98     public static final int ABSENT_MTH          = ERRONEOUS + 8;  // missing method
99     public static final int ABSENT_TYP          = ERRONEOUS + 9;  // missing type
100     public static final int WRONG_STATICNESS    = ERRONEOUS + 10; // wrong staticness for method references
101 
102     public enum KindName implements Formattable {
103         ANNOTATION("kindname.annotation"),
104         CONSTRUCTOR("kindname.constructor"),
105         INTERFACE("kindname.interface"),
106         ENUM("kindname.enum"),
107         STATIC("kindname.static"),
108         TYPEVAR("kindname.type.variable"),
109         BOUND("kindname.type.variable.bound"),
110         VAR("kindname.variable"),
111         VAL("kindname.value"),
112         METHOD("kindname.method"),
113         CLASS("kindname.class"),
114         STATIC_INIT("kindname.static.init"),
115         INSTANCE_INIT("kindname.instance.init"),
116         PACKAGE("kindname.package");
117 
118         private final String name;
119 
KindName(String name)120         KindName(String name) {
121             this.name = name;
122         }
123 
toString()124         public String toString() {
125             return name;
126         }
127 
getKind()128         public String getKind() {
129             return "Kindname";
130         }
131 
toString(Locale locale, Messages messages)132         public String toString(Locale locale, Messages messages) {
133             String s = toString();
134             return messages.getLocalizedString(locale, "compiler.misc." + s);
135         }
136     }
137 
138     /** A KindName representing a given symbol kind
139      */
kindName(int kind)140     public static KindName kindName(int kind) {
141         switch (kind) {
142         case PCK: return KindName.PACKAGE;
143         case TYP: return KindName.CLASS;
144         case VAR: return KindName.VAR;
145         case VAL: return KindName.VAL;
146         case MTH: return KindName.METHOD;
147             default : throw new AssertionError("Unexpected kind: "+kind);
148         }
149     }
150 
kindName(MemberReferenceTree.ReferenceMode mode)151     public static KindName kindName(MemberReferenceTree.ReferenceMode mode) {
152         switch (mode) {
153             case INVOKE: return KindName.METHOD;
154             case NEW: return KindName.CONSTRUCTOR;
155             default : throw new AssertionError("Unexpected mode: "+ mode);
156         }
157     }
158 
159     /** A KindName representing a given symbol
160      */
kindName(Symbol sym)161     public static KindName kindName(Symbol sym) {
162         switch (sym.getKind()) {
163         case PACKAGE:
164             return KindName.PACKAGE;
165 
166         case ENUM:
167             return KindName.ENUM;
168 
169         case ANNOTATION_TYPE:
170         case CLASS:
171             return KindName.CLASS;
172 
173         case INTERFACE:
174             return KindName.INTERFACE;
175 
176         case TYPE_PARAMETER:
177             return KindName.TYPEVAR;
178 
179         case ENUM_CONSTANT:
180         case FIELD:
181         case PARAMETER:
182         case LOCAL_VARIABLE:
183         case EXCEPTION_PARAMETER:
184         case RESOURCE_VARIABLE:
185             return KindName.VAR;
186 
187         case CONSTRUCTOR:
188             return KindName.CONSTRUCTOR;
189 
190         case METHOD:
191             return KindName.METHOD;
192         case STATIC_INIT:
193             return KindName.STATIC_INIT;
194         case INSTANCE_INIT:
195             return KindName.INSTANCE_INIT;
196 
197         default:
198             if (sym.kind == VAL)
199                 // I don't think this can happen but it can't harm
200                 // playing it safe --ahe
201                 return KindName.VAL;
202             else
203                 throw new AssertionError("Unexpected kind: "+sym.getKind());
204         }
205     }
206 
207     /** A set of KindName(s) representing a set of symbol's kinds.
208      */
kindNames(int kind)209     public static EnumSet<KindName> kindNames(int kind) {
210         EnumSet<KindName> kinds = EnumSet.noneOf(KindName.class);
211         if ((kind & VAL) != 0)
212             kinds.add(((kind & VAL) == VAR) ? KindName.VAR : KindName.VAL);
213         if ((kind & MTH) != 0) kinds.add(KindName.METHOD);
214         if ((kind & TYP) != 0) kinds.add(KindName.CLASS);
215         if ((kind & PCK) != 0) kinds.add(KindName.PACKAGE);
216         return kinds;
217     }
218 
219     /** A KindName representing the kind of a given class/interface type.
220      */
typeKindName(Type t)221     public static KindName typeKindName(Type t) {
222         if (t.hasTag(TYPEVAR) ||
223             t.hasTag(CLASS) && (t.tsym.flags() & COMPOUND) != 0)
224             return KindName.BOUND;
225         else if (t.hasTag(PACKAGE))
226             return KindName.PACKAGE;
227         else if ((t.tsym.flags_field & ANNOTATION) != 0)
228             return KindName.ANNOTATION;
229         else if ((t.tsym.flags_field & INTERFACE) != 0)
230             return KindName.INTERFACE;
231         else
232             return KindName.CLASS;
233     }
234 
235     /** A KindName representing the kind of a missing symbol, given an
236      *  error kind.
237      * */
absentKind(int kind)238     public static KindName absentKind(int kind) {
239         switch (kind) {
240         case ABSENT_VAR:
241             return KindName.VAR;
242         case WRONG_MTHS: case WRONG_MTH: case ABSENT_MTH: case WRONG_STATICNESS:
243             return KindName.METHOD;
244         case ABSENT_TYP:
245             return KindName.CLASS;
246         default:
247             throw new AssertionError("Unexpected kind: "+kind);
248         }
249     }
250 }
251