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

Save This Page
Home » openjdk-7 » com.sun.tools » javac » code » [javadoc | source]