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

Thu, 09 Oct 2008 16:21:04 +0100

author
mcimadamore
date
Thu, 09 Oct 2008 16:21:04 +0100
changeset 138
d766e40e49d6
parent 136
8eafba4f61be
child 302
18e0269f25e3
permissions
-rw-r--r--

6586091: javac crashes with StackOverflowError
Summary: Types.adapt should avoid infinite loops by exploiting a local cache
Reviewed-by: jjg

     1 /*
     2  * Copyright 1999-2008 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.javac.code;
    28 import java.util.EnumSet;
    29 import java.util.Locale;
    31 import com.sun.tools.javac.api.Formattable;
    32 import com.sun.tools.javac.api.Messages;
    34 import static com.sun.tools.javac.code.TypeTags.*;
    35 import static com.sun.tools.javac.code.Flags.*;
    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 API supported by Sun Microsystems.  If
    42  *  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 {
    48     private Kinds() {} // uninstantiable
    50     /** The empty set of kinds.
    51      */
    52     public final static int NIL = 0;
    54     /** The kind of package symbols.
    55      */
    56     public final static int PCK = 1 << 0;
    58     /** The kind of type symbols (classes, interfaces and type variables).
    59      */
    60     public final static int TYP = 1 << 1;
    62     /** The kind of variable symbols.
    63      */
    64     public final static int VAR = 1 << 2;
    66     /** The kind of values (variables or non-variable expressions), includes VAR.
    67      */
    68     public final static int VAL = (1 << 3) | VAR;
    70     /** The kind of methods.
    71      */
    72     public final static int MTH = 1 << 4;
    74     /** The error kind, which includes all other kinds.
    75      */
    76     public final static int ERR = (1 << 5) - 1;
    78     /** The set of all kinds.
    79      */
    80     public final static int AllKinds = ERR;
    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
    94     public enum KindName implements Formattable {
    95         ANNOTATION("kindname.interface"),
    96         CONSTRUCTOR("kindname.constructor"),
    97         INTERFACE("kindname.interface"),
    98         STATIC("kindname.static"),
    99         TYPEVAR("kindname.type.variable"),
   100         BOUND("kindname.type.variable.bound"),
   101         VAR("kindname.variable"),
   102         VAL("kindname.value"),
   103         METHOD("kindname.method"),
   104         CLASS("kindname.class"),
   105         PACKAGE("kindname.package");
   107         private String name;
   109         KindName(String name) {
   110             this.name = name;
   111         }
   113         public String toString() {
   114             return name;
   115         }
   117         public String getKind() {
   118             return "Kindname";
   119         }
   121         public String toString(Locale locale, Messages messages) {
   122             String s = toString();
   123             return messages.getLocalizedString(locale, "compiler.misc." + s);
   124         }
   125     }
   127     /** A KindName representing a given symbol kind
   128      */
   129     public static KindName kindName(int kind) {
   130         switch (kind) {
   131         case PCK: return KindName.PACKAGE;
   132         case TYP: return KindName.CLASS;
   133         case VAR: return KindName.VAR;
   134         case VAL: return KindName.VAL;
   135         case MTH: return KindName.METHOD;
   136             default : throw new AssertionError("Unexpected kind: "+kind);
   137         }
   138     }
   140     /** A KindName representing a given symbol
   141      */
   142     public static KindName kindName(Symbol sym) {
   143         switch (sym.getKind()) {
   144         case PACKAGE:
   145             return KindName.PACKAGE;
   147         case ENUM:
   148         case ANNOTATION_TYPE:
   149         case INTERFACE:
   150         case CLASS:
   151             return KindName.CLASS;
   153         case TYPE_PARAMETER:
   154             return KindName.TYPEVAR;
   156         case ENUM_CONSTANT:
   157         case FIELD:
   158         case PARAMETER:
   159         case LOCAL_VARIABLE:
   160         case EXCEPTION_PARAMETER:
   161             return KindName.VAR;
   163         case METHOD:
   164         case CONSTRUCTOR:
   165         case STATIC_INIT:
   166         case INSTANCE_INIT:
   167             return KindName.METHOD;
   169         default:
   170             if (sym.kind == VAL)
   171                 // I don't think this can happen but it can't harm
   172                 // playing it safe --ahe
   173                 return KindName.VAL;
   174             else
   175                 throw new AssertionError("Unexpected kind: "+sym.getKind());
   176         }
   177     }
   179     /** A set of KindName(s) representing a set of symbol's kinds.
   180      */
   181     public static EnumSet<KindName> kindNames(int kind) {
   182         EnumSet<KindName> kinds = EnumSet.noneOf(KindName.class);
   183         if ((kind & VAL) != 0)
   184             kinds.add(((kind & VAL) == VAR) ? KindName.VAR : KindName.VAL);
   185         if ((kind & MTH) != 0) kinds.add(KindName.METHOD);
   186         if ((kind & TYP) != 0) kinds.add(KindName.CLASS);
   187         if ((kind & PCK) != 0) kinds.add(KindName.PACKAGE);
   188         return kinds;
   189     }
   191     /** A KindName representing the kind of a given class/interface type.
   192      */
   193     public static KindName typeKindName(Type t) {
   194         if (t.tag == TYPEVAR ||
   195             t.tag == CLASS && (t.tsym.flags() & COMPOUND) != 0)
   196             return KindName.BOUND;
   197         else if (t.tag == PACKAGE)
   198             return KindName.PACKAGE;
   199         else if ((t.tsym.flags_field & ANNOTATION) != 0)
   200             return KindName.ANNOTATION;
   201         else if ((t.tsym.flags_field & INTERFACE) != 0)
   202             return KindName.INTERFACE;
   203         else
   204             return KindName.CLASS;
   205     }
   207     /** A KindName representing the kind of a a missing symbol, given an
   208      *  error kind.
   209      * */
   210     public static KindName absentKind(int kind) {
   211         switch (kind) {
   212         case ABSENT_VAR:
   213             return KindName.VAR;
   214         case WRONG_MTHS: case WRONG_MTH: case ABSENT_MTH:
   215             return KindName.METHOD;
   216         case ABSENT_TYP:
   217             return KindName.CLASS;
   218         default:
   219             throw new AssertionError("Unexpected kind: "+kind);
   220         }
   221     }
   222 }

mercurial