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

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1442
fcf89720ae71
child 1850
6debfa63a4a1
permissions
-rw-r--r--

Merge

     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  */
    26 package com.sun.tools.javac.code;
    28 import java.util.EnumSet;
    29 import java.util.Locale;
    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;
    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 {
    50     private Kinds() {} // uninstantiable
    52     /** The empty set of kinds.
    53      */
    54     public final static int NIL = 0;
    56     /** The kind of package symbols.
    57      */
    58     public final static int PCK = 1 << 0;
    60     /** The kind of type symbols (classes, interfaces and type variables).
    61      */
    62     public final static int TYP = 1 << 1;
    64     /** The kind of variable symbols.
    65      */
    66     public final static int VAR = 1 << 2;
    68     /** The kind of values (variables or non-variable expressions), includes VAR.
    69      */
    70     public final static int VAL = (1 << 3) | VAR;
    72     /** The kind of methods.
    73      */
    74     public final static int MTH = 1 << 4;
    76     /** The error kind, which includes all other kinds.
    77      */
    78     public final static int ERR = (1 << 5) - 1;
    80     /** The set of all kinds.
    81      */
    82     public final static int AllKinds = ERR;
    84     /** Kinds for erroneous symbols that complement the above
    85      */
    86     public static final int ERRONEOUS = 1 << 6;
    87     public static final int AMBIGUOUS    = ERRONEOUS+1; // ambiguous reference
    88     public static final int HIDDEN       = ERRONEOUS+2; // hidden method or field
    89     public static final int STATICERR    = ERRONEOUS+3; // nonstatic member from static context
    90     public static final int MISSING_ENCL = ERRONEOUS+4; // missing enclosing class
    91     public static final int ABSENT_VAR   = ERRONEOUS+5; // missing variable
    92     public static final int WRONG_MTHS   = ERRONEOUS+6; // methods with wrong arguments
    93     public static final int WRONG_MTH    = ERRONEOUS+7; // one method with wrong arguments
    94     public static final int ABSENT_MTH   = ERRONEOUS+8; // missing method
    95     public static final int ABSENT_TYP   = ERRONEOUS+9; // missing type
    97     public enum KindName implements Formattable {
    98         ANNOTATION("kindname.annotation"),
    99         CONSTRUCTOR("kindname.constructor"),
   100         INTERFACE("kindname.interface"),
   101         ENUM("kindname.enum"),
   102         STATIC("kindname.static"),
   103         TYPEVAR("kindname.type.variable"),
   104         BOUND("kindname.type.variable.bound"),
   105         VAR("kindname.variable"),
   106         VAL("kindname.value"),
   107         METHOD("kindname.method"),
   108         CLASS("kindname.class"),
   109         STATIC_INIT("kindname.static.init"),
   110         INSTANCE_INIT("kindname.instance.init"),
   111         PACKAGE("kindname.package");
   113         private final String name;
   115         KindName(String name) {
   116             this.name = name;
   117         }
   119         public String toString() {
   120             return name;
   121         }
   123         public String getKind() {
   124             return "Kindname";
   125         }
   127         public String toString(Locale locale, Messages messages) {
   128             String s = toString();
   129             return messages.getLocalizedString(locale, "compiler.misc." + s);
   130         }
   131     }
   133     /** A KindName representing a given symbol kind
   134      */
   135     public static KindName kindName(int kind) {
   136         switch (kind) {
   137         case PCK: return KindName.PACKAGE;
   138         case TYP: return KindName.CLASS;
   139         case VAR: return KindName.VAR;
   140         case VAL: return KindName.VAL;
   141         case MTH: return KindName.METHOD;
   142             default : throw new AssertionError("Unexpected kind: "+kind);
   143         }
   144     }
   146     public static KindName kindName(MemberReferenceTree.ReferenceMode mode) {
   147         switch (mode) {
   148             case INVOKE: return KindName.METHOD;
   149             case NEW: return KindName.CONSTRUCTOR;
   150             default : throw new AssertionError("Unexpected mode: "+ mode);
   151         }
   152     }
   154     /** A KindName representing a given symbol
   155      */
   156     public static KindName kindName(Symbol sym) {
   157         switch (sym.getKind()) {
   158         case PACKAGE:
   159             return KindName.PACKAGE;
   161         case ENUM:
   162             return KindName.ENUM;
   164         case ANNOTATION_TYPE:
   165         case CLASS:
   166             return KindName.CLASS;
   168         case INTERFACE:
   169             return KindName.INTERFACE;
   171         case TYPE_PARAMETER:
   172             return KindName.TYPEVAR;
   174         case ENUM_CONSTANT:
   175         case FIELD:
   176         case PARAMETER:
   177         case LOCAL_VARIABLE:
   178         case EXCEPTION_PARAMETER:
   179         case RESOURCE_VARIABLE:
   180             return KindName.VAR;
   182         case CONSTRUCTOR:
   183             return KindName.CONSTRUCTOR;
   185         case METHOD:
   186             return KindName.METHOD;
   187         case STATIC_INIT:
   188             return KindName.STATIC_INIT;
   189         case INSTANCE_INIT:
   190             return KindName.INSTANCE_INIT;
   192         default:
   193             if (sym.kind == VAL)
   194                 // I don't think this can happen but it can't harm
   195                 // playing it safe --ahe
   196                 return KindName.VAL;
   197             else
   198                 throw new AssertionError("Unexpected kind: "+sym.getKind());
   199         }
   200     }
   202     /** A set of KindName(s) representing a set of symbol's kinds.
   203      */
   204     public static EnumSet<KindName> kindNames(int kind) {
   205         EnumSet<KindName> kinds = EnumSet.noneOf(KindName.class);
   206         if ((kind & VAL) != 0)
   207             kinds.add(((kind & VAL) == VAR) ? KindName.VAR : KindName.VAL);
   208         if ((kind & MTH) != 0) kinds.add(KindName.METHOD);
   209         if ((kind & TYP) != 0) kinds.add(KindName.CLASS);
   210         if ((kind & PCK) != 0) kinds.add(KindName.PACKAGE);
   211         return kinds;
   212     }
   214     /** A KindName representing the kind of a given class/interface type.
   215      */
   216     public static KindName typeKindName(Type t) {
   217         if (t.tag == TYPEVAR ||
   218             t.tag == CLASS && (t.tsym.flags() & COMPOUND) != 0)
   219             return KindName.BOUND;
   220         else if (t.tag == PACKAGE)
   221             return KindName.PACKAGE;
   222         else if ((t.tsym.flags_field & ANNOTATION) != 0)
   223             return KindName.ANNOTATION;
   224         else if ((t.tsym.flags_field & INTERFACE) != 0)
   225             return KindName.INTERFACE;
   226         else
   227             return KindName.CLASS;
   228     }
   230     /** A KindName representing the kind of a a missing symbol, given an
   231      *  error kind.
   232      * */
   233     public static KindName absentKind(int kind) {
   234         switch (kind) {
   235         case ABSENT_VAR:
   236             return KindName.VAR;
   237         case WRONG_MTHS: case WRONG_MTH: case ABSENT_MTH:
   238             return KindName.METHOD;
   239         case ABSENT_TYP:
   240             return KindName.CLASS;
   241         default:
   242             throw new AssertionError("Unexpected kind: "+kind);
   243         }
   244     }
   245 }

mercurial