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

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