src/share/classes/com/sun/tools/javac/code/Kinds.java

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 3938
93012e2a5d1d
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe

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

mercurial