aoqi@0: /* aoqi@0: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.javac.code; aoqi@0: aoqi@0: import java.util.EnumSet; aoqi@0: import java.util.Locale; aoqi@0: aoqi@0: import com.sun.source.tree.MemberReferenceTree; aoqi@0: import com.sun.tools.javac.api.Formattable; aoqi@0: import com.sun.tools.javac.api.Messages; aoqi@0: import static com.sun.tools.javac.code.Flags.*; aoqi@0: import static com.sun.tools.javac.code.TypeTag.CLASS; aoqi@0: import static com.sun.tools.javac.code.TypeTag.PACKAGE; aoqi@0: import static com.sun.tools.javac.code.TypeTag.TYPEVAR; aoqi@0: aoqi@0: /** Internal symbol kinds, which distinguish between elements of aoqi@0: * different subclasses of Symbol. Symbol kinds are organized so they can be aoqi@0: * or'ed to sets. aoqi@0: * aoqi@0: *

This is NOT part of any supported API. aoqi@0: * If you write code that depends on this, you do so at your own risk. aoqi@0: * This code and its internal interfaces are subject to change or aoqi@0: * deletion without notice. aoqi@0: */ aoqi@0: public class Kinds { aoqi@0: aoqi@0: private Kinds() {} // uninstantiable aoqi@0: aoqi@0: /** The empty set of kinds. aoqi@0: */ aoqi@0: public final static int NIL = 0; aoqi@0: aoqi@0: /** The kind of package symbols. aoqi@0: */ aoqi@0: public final static int PCK = 1 << 0; aoqi@0: aoqi@0: /** The kind of type symbols (classes, interfaces and type variables). aoqi@0: */ aoqi@0: public final static int TYP = 1 << 1; aoqi@0: aoqi@0: /** The kind of variable symbols. aoqi@0: */ aoqi@0: public final static int VAR = 1 << 2; aoqi@0: aoqi@0: /** The kind of values (variables or non-variable expressions), includes VAR. aoqi@0: */ aoqi@0: public final static int VAL = (1 << 3) | VAR; aoqi@0: aoqi@0: /** The kind of methods. aoqi@0: */ aoqi@0: public final static int MTH = 1 << 4; aoqi@0: aoqi@0: /** Poly kind, for deferred types. aoqi@0: */ aoqi@0: public final static int POLY = 1 << 5; aoqi@0: aoqi@0: /** The error kind, which includes all other kinds. aoqi@0: */ aoqi@0: public final static int ERR = (1 << 6) - 1; aoqi@0: aoqi@0: /** The set of all kinds. aoqi@0: */ aoqi@0: public final static int AllKinds = ERR; aoqi@0: aoqi@0: /** Kinds for erroneous symbols that complement the above aoqi@0: */ aoqi@0: public static final int ERRONEOUS = 1 << 7; aoqi@0: public static final int AMBIGUOUS = ERRONEOUS + 1; // ambiguous reference aoqi@0: public static final int HIDDEN = ERRONEOUS + 2; // hidden method or field aoqi@0: public static final int STATICERR = ERRONEOUS + 3; // nonstatic member from static context aoqi@0: public static final int MISSING_ENCL = ERRONEOUS + 4; // missing enclosing class aoqi@0: public static final int ABSENT_VAR = ERRONEOUS + 5; // missing variable aoqi@0: public static final int WRONG_MTHS = ERRONEOUS + 6; // methods with wrong arguments aoqi@0: public static final int WRONG_MTH = ERRONEOUS + 7; // one method with wrong arguments aoqi@0: public static final int ABSENT_MTH = ERRONEOUS + 8; // missing method aoqi@0: public static final int ABSENT_TYP = ERRONEOUS + 9; // missing type aoqi@0: public static final int WRONG_STATICNESS = ERRONEOUS + 10; // wrong staticness for method references aoqi@0: aoqi@0: public enum KindName implements Formattable { aoqi@0: ANNOTATION("kindname.annotation"), aoqi@0: CONSTRUCTOR("kindname.constructor"), aoqi@0: INTERFACE("kindname.interface"), aoqi@0: ENUM("kindname.enum"), aoqi@0: STATIC("kindname.static"), aoqi@0: TYPEVAR("kindname.type.variable"), aoqi@0: BOUND("kindname.type.variable.bound"), aoqi@0: VAR("kindname.variable"), aoqi@0: VAL("kindname.value"), aoqi@0: METHOD("kindname.method"), aoqi@0: CLASS("kindname.class"), aoqi@0: STATIC_INIT("kindname.static.init"), aoqi@0: INSTANCE_INIT("kindname.instance.init"), aoqi@0: PACKAGE("kindname.package"); aoqi@0: aoqi@0: private final String name; aoqi@0: aoqi@0: KindName(String name) { aoqi@0: this.name = name; aoqi@0: } aoqi@0: aoqi@0: public String toString() { aoqi@0: return name; aoqi@0: } aoqi@0: aoqi@0: public String getKind() { aoqi@0: return "Kindname"; aoqi@0: } aoqi@0: aoqi@0: public String toString(Locale locale, Messages messages) { aoqi@0: String s = toString(); aoqi@0: return messages.getLocalizedString(locale, "compiler.misc." + s); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** A KindName representing a given symbol kind aoqi@0: */ aoqi@0: public static KindName kindName(int kind) { aoqi@0: switch (kind) { aoqi@0: case PCK: return KindName.PACKAGE; aoqi@0: case TYP: return KindName.CLASS; aoqi@0: case VAR: return KindName.VAR; aoqi@0: case VAL: return KindName.VAL; aoqi@0: case MTH: return KindName.METHOD; aoqi@0: default : throw new AssertionError("Unexpected kind: "+kind); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public static KindName kindName(MemberReferenceTree.ReferenceMode mode) { aoqi@0: switch (mode) { aoqi@0: case INVOKE: return KindName.METHOD; aoqi@0: case NEW: return KindName.CONSTRUCTOR; aoqi@0: default : throw new AssertionError("Unexpected mode: "+ mode); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** A KindName representing a given symbol aoqi@0: */ aoqi@0: public static KindName kindName(Symbol sym) { aoqi@0: switch (sym.getKind()) { aoqi@0: case PACKAGE: aoqi@0: return KindName.PACKAGE; aoqi@0: aoqi@0: case ENUM: aoqi@0: return KindName.ENUM; aoqi@0: aoqi@0: case ANNOTATION_TYPE: aoqi@0: case CLASS: aoqi@0: return KindName.CLASS; aoqi@0: aoqi@0: case INTERFACE: aoqi@0: return KindName.INTERFACE; aoqi@0: aoqi@0: case TYPE_PARAMETER: aoqi@0: return KindName.TYPEVAR; aoqi@0: aoqi@0: case ENUM_CONSTANT: aoqi@0: case FIELD: aoqi@0: case PARAMETER: aoqi@0: case LOCAL_VARIABLE: aoqi@0: case EXCEPTION_PARAMETER: aoqi@0: case RESOURCE_VARIABLE: aoqi@0: return KindName.VAR; aoqi@0: aoqi@0: case CONSTRUCTOR: aoqi@0: return KindName.CONSTRUCTOR; aoqi@0: aoqi@0: case METHOD: aoqi@0: return KindName.METHOD; aoqi@0: case STATIC_INIT: aoqi@0: return KindName.STATIC_INIT; aoqi@0: case INSTANCE_INIT: aoqi@0: return KindName.INSTANCE_INIT; aoqi@0: aoqi@0: default: aoqi@0: if (sym.kind == VAL) aoqi@0: // I don't think this can happen but it can't harm aoqi@0: // playing it safe --ahe aoqi@0: return KindName.VAL; aoqi@0: else aoqi@0: throw new AssertionError("Unexpected kind: "+sym.getKind()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** A set of KindName(s) representing a set of symbol's kinds. aoqi@0: */ aoqi@0: public static EnumSet kindNames(int kind) { aoqi@0: EnumSet kinds = EnumSet.noneOf(KindName.class); aoqi@0: if ((kind & VAL) != 0) aoqi@0: kinds.add(((kind & VAL) == VAR) ? KindName.VAR : KindName.VAL); aoqi@0: if ((kind & MTH) != 0) kinds.add(KindName.METHOD); aoqi@0: if ((kind & TYP) != 0) kinds.add(KindName.CLASS); aoqi@0: if ((kind & PCK) != 0) kinds.add(KindName.PACKAGE); aoqi@0: return kinds; aoqi@0: } aoqi@0: aoqi@0: /** A KindName representing the kind of a given class/interface type. aoqi@0: */ aoqi@0: public static KindName typeKindName(Type t) { aoqi@0: if (t.hasTag(TYPEVAR) || aoqi@0: t.hasTag(CLASS) && (t.tsym.flags() & COMPOUND) != 0) aoqi@0: return KindName.BOUND; aoqi@0: else if (t.hasTag(PACKAGE)) aoqi@0: return KindName.PACKAGE; aoqi@0: else if ((t.tsym.flags_field & ANNOTATION) != 0) aoqi@0: return KindName.ANNOTATION; aoqi@0: else if ((t.tsym.flags_field & INTERFACE) != 0) aoqi@0: return KindName.INTERFACE; aoqi@0: else aoqi@0: return KindName.CLASS; aoqi@0: } aoqi@0: aoqi@0: /** A KindName representing the kind of a missing symbol, given an aoqi@0: * error kind. aoqi@0: * */ aoqi@0: public static KindName absentKind(int kind) { aoqi@0: switch (kind) { aoqi@0: case ABSENT_VAR: aoqi@0: return KindName.VAR; aoqi@0: case WRONG_MTHS: case WRONG_MTH: case ABSENT_MTH: case WRONG_STATICNESS: aoqi@0: return KindName.METHOD; aoqi@0: case ABSENT_TYP: aoqi@0: return KindName.CLASS; aoqi@0: default: aoqi@0: throw new AssertionError("Unexpected kind: "+kind); aoqi@0: } aoqi@0: } aoqi@0: }