Kinds.java revision 2726:f62d01419621
1/* 2 * Copyright (c) 1999, 2013, 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 26package com.sun.tools.javac.code; 27 28import java.util.EnumSet; 29import java.util.Set; 30import java.util.Locale; 31 32import com.sun.source.tree.MemberReferenceTree; 33import com.sun.tools.javac.api.Formattable; 34import com.sun.tools.javac.api.Messages; 35 36import static com.sun.tools.javac.code.Flags.*; 37import static com.sun.tools.javac.code.TypeTag.CLASS; 38import static com.sun.tools.javac.code.TypeTag.PACKAGE; 39import static com.sun.tools.javac.code.TypeTag.TYPEVAR; 40 41/** Internal symbol kinds, which distinguish between elements of 42 * different subclasses of Symbol. Symbol kinds are organized so they can be 43 * or'ed to sets. 44 * 45 * <p><b>This is NOT part of any supported API. 46 * If you write code that depends on this, you do so at your own risk. 47 * This code and its internal interfaces are subject to change or 48 * deletion without notice.</b> 49 */ 50public class Kinds { 51 52 private Kinds() {} // uninstantiable 53 54 /** 55 * Kind of symbols. 56 * 57 * IMPORTANT: This is an ordered type. The ordering of 58 * declarations in this enum matters. Be careful when changing 59 * it. 60 */ 61 public enum Kind { 62 NIL(Category.BASIC, KindSelector.NIL), 63 PCK(Category.BASIC, KindName.PACKAGE, KindSelector.PCK), 64 TYP(Category.BASIC, KindName.CLASS, KindSelector.TYP), 65 VAR(Category.BASIC, KindName.VAR, KindSelector.VAR), 66 MTH(Category.BASIC, KindName.METHOD, KindSelector.MTH), 67 POLY(Category.BASIC, KindSelector.POLY), 68 ERR(Category.ERROR, KindSelector.ERR), 69 AMBIGUOUS(Category.OVERLOAD), 70 HIDDEN(Category.OVERLOAD), 71 STATICERR(Category.OVERLOAD), 72 MISSING_ENCL(Category.OVERLOAD), 73 ABSENT_VAR(Category.OVERLOAD, KindName.VAR), 74 WRONG_MTHS(Category.OVERLOAD, KindName.METHOD), 75 WRONG_MTH(Category.OVERLOAD, KindName.METHOD), 76 ABSENT_MTH(Category.OVERLOAD, KindName.METHOD), 77 ABSENT_TYP(Category.OVERLOAD, KindName.CLASS), 78 WRONG_STATICNESS(Category.OVERLOAD, KindName.METHOD); 79 80 // There are essentially two "levels" to the Kind datatype. 81 // The first is a totally-ordered set of categories of 82 // solutions. Within each category, we have more 83 // possibilities. 84 private enum Category { 85 BASIC, ERROR, OVERLOAD; 86 } 87 88 private final KindName kindName; 89 private final KindName absentKind; 90 private final KindSelector selector; 91 private final Category category; 92 93 private Kind(Category category) { 94 this(category, null, null, null); 95 } 96 97 private Kind(Category category, 98 KindSelector selector) { 99 this(category, null, null, selector); 100 } 101 102 private Kind(Category category, 103 KindName absentKind) { 104 this(category, null, absentKind, null); 105 } 106 107 private Kind(Category category, 108 KindName kindName, 109 KindSelector selector) { 110 this(category, kindName, null, selector); 111 } 112 113 private Kind(Category category, 114 KindName kindName, 115 KindName absentKind, 116 KindSelector selector) { 117 this.category = category; 118 this.kindName = kindName; 119 this.absentKind = absentKind; 120 this.selector = selector; 121 } 122 123 public KindSelector toSelector() { 124 return selector; 125 } 126 127 public boolean matches(KindSelector kindSelectors) { 128 return selector.contains(kindSelectors); 129 } 130 131 public boolean isOverloadError() { 132 return category == Category.OVERLOAD; 133 } 134 135 public boolean isValid() { 136 return category == Category.BASIC; 137 } 138 139 public boolean betterThan(Kind other) { 140 return ordinal() < other.ordinal(); 141 } 142 143 public KindName kindName() { 144 if (kindName == null) { 145 throw new AssertionError("Unexpected kind: " + this); 146 } else { 147 return kindName; 148 } 149 } 150 151 public KindName absentKind() { 152 if (absentKind == null) { 153 throw new AssertionError("Unexpected kind: " + this); 154 } else { 155 return absentKind; 156 } 157 } 158 } 159 160 public static class KindSelector { 161 162 //basic selectors 163 public static final KindSelector NIL = new KindSelector(0); 164 public static final KindSelector PCK = new KindSelector(0x01); 165 public static final KindSelector TYP = new KindSelector(0x02); 166 public static final KindSelector VAR = new KindSelector(0x04); 167 public static final KindSelector VAL = new KindSelector(0x0c); 168 public static final KindSelector MTH = new KindSelector(0x10); 169 public static final KindSelector ERR = new KindSelector(0x3f); 170 public static final KindSelector POLY = new KindSelector(0x20); 171 public static final KindSelector ASG = new KindSelector(0x44); 172 173 //common derived selectors 174 public static final KindSelector TYP_PCK = of(TYP, PCK); 175 public static final KindSelector VAL_MTH = of(VAL, MTH); 176 public static final KindSelector VAL_POLY = of(VAL, POLY); 177 public static final KindSelector VAL_TYP = of(VAL, TYP); 178 public static final KindSelector VAL_TYP_PCK = of(VAL, TYP, PCK); 179 180 private final byte data; 181 182 private KindSelector(int data) { 183 this.data = (byte) data; 184 } 185 186 public static KindSelector of(KindSelector... kindSelectors) { 187 byte newData = 0; 188 for (KindSelector kindSel : kindSelectors) { 189 newData |= kindSel.data; 190 } 191 return new KindSelector(newData); 192 } 193 194 public boolean subset(KindSelector other) { 195 return (data & ~other.data) == 0; 196 } 197 198 public boolean contains(KindSelector other) { 199 return (data & other.data) != 0; 200 } 201 202 /** A set of KindName(s) representing a set of symbol's kinds. */ 203 public Set<KindName> kindNames() { 204 EnumSet<KindName> kinds = EnumSet.noneOf(KindName.class); 205 if ((data & VAL.data) != 0) { 206 if ((data & VAL.data) == VAR.data) kinds.add(KindName.VAR); 207 else kinds.add(KindName.VAL); 208 } 209 if ((data & MTH.data) != 0) kinds.add(KindName.METHOD); 210 if ((data & TYP.data) != 0) kinds.add(KindName.CLASS); 211 if ((data & PCK.data) != 0) kinds.add(KindName.PACKAGE); 212 return kinds; 213 } 214 } 215 216 public enum KindName implements Formattable { 217 ANNOTATION("kindname.annotation"), 218 CONSTRUCTOR("kindname.constructor"), 219 INTERFACE("kindname.interface"), 220 ENUM("kindname.enum"), 221 STATIC("kindname.static"), 222 TYPEVAR("kindname.type.variable"), 223 BOUND("kindname.type.variable.bound"), 224 VAR("kindname.variable"), 225 VAL("kindname.value"), 226 METHOD("kindname.method"), 227 CLASS("kindname.class"), 228 STATIC_INIT("kindname.static.init"), 229 INSTANCE_INIT("kindname.instance.init"), 230 PACKAGE("kindname.package"); 231 232 private final String name; 233 234 KindName(String name) { 235 this.name = name; 236 } 237 238 public String toString() { 239 return name; 240 } 241 242 public String getKind() { 243 return "Kindname"; 244 } 245 246 public String toString(Locale locale, Messages messages) { 247 String s = toString(); 248 return messages.getLocalizedString(locale, "compiler.misc." + s); 249 } 250 } 251 252 public static KindName kindName(MemberReferenceTree.ReferenceMode mode) { 253 switch (mode) { 254 case INVOKE: return KindName.METHOD; 255 case NEW: return KindName.CONSTRUCTOR; 256 default : throw new AssertionError("Unexpected mode: "+ mode); 257 } 258 } 259 260 /** A KindName representing a given symbol 261 */ 262 public static KindName kindName(Symbol sym) { 263 switch (sym.getKind()) { 264 case PACKAGE: 265 return KindName.PACKAGE; 266 267 case ENUM: 268 return KindName.ENUM; 269 270 case ANNOTATION_TYPE: 271 case CLASS: 272 return KindName.CLASS; 273 274 case INTERFACE: 275 return KindName.INTERFACE; 276 277 case TYPE_PARAMETER: 278 return KindName.TYPEVAR; 279 280 case ENUM_CONSTANT: 281 case FIELD: 282 case PARAMETER: 283 case LOCAL_VARIABLE: 284 case EXCEPTION_PARAMETER: 285 case RESOURCE_VARIABLE: 286 return KindName.VAR; 287 288 case CONSTRUCTOR: 289 return KindName.CONSTRUCTOR; 290 291 case METHOD: 292 return KindName.METHOD; 293 case STATIC_INIT: 294 return KindName.STATIC_INIT; 295 case INSTANCE_INIT: 296 return KindName.INSTANCE_INIT; 297 298 default: 299 throw new AssertionError("Unexpected kind: "+sym.getKind()); 300 } 301 } 302 303 /** A KindName representing the kind of a given class/interface type. 304 */ 305 public static KindName typeKindName(Type t) { 306 if (t.hasTag(TYPEVAR) || 307 t.hasTag(CLASS) && (t.tsym.flags() & COMPOUND) != 0) 308 return KindName.BOUND; 309 else if (t.hasTag(PACKAGE)) 310 return KindName.PACKAGE; 311 else if ((t.tsym.flags_field & ANNOTATION) != 0) 312 return KindName.ANNOTATION; 313 else if ((t.tsym.flags_field & INTERFACE) != 0) 314 return KindName.INTERFACE; 315 else 316 return KindName.CLASS; 317 } 318 319} 320