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

Sat, 15 Dec 2012 13:54:51 +0000

author
vromero
date
Sat, 15 Dec 2012 13:54:51 +0000
changeset 1452
de1ec6fc93fe
parent 1436
f6f1fd261f57
child 1521
71f35e4b93a5
permissions
-rw-r--r--

8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
Reviewed-by: jjg, mcimadamore

duke@1 1 /*
jjh@1305 2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.code;
duke@1 27
jjg@988 28 import java.util.Collections;
mcimadamore@1338 29 import java.util.EnumMap;
mcimadamore@1338 30 import java.util.EnumSet;
mcimadamore@1338 31 import java.util.Map;
mcimadamore@1338 32 import java.util.Set;
mcimadamore@1342 33
duke@1 34 import javax.lang.model.type.*;
duke@1 35
jjg@1357 36 import com.sun.tools.javac.code.Symbol.*;
jjg@1357 37 import com.sun.tools.javac.util.*;
mcimadamore@1342 38 import static com.sun.tools.javac.code.BoundKind.*;
duke@1 39 import static com.sun.tools.javac.code.Flags.*;
duke@1 40 import static com.sun.tools.javac.code.Kinds.*;
jjg@1374 41 import static com.sun.tools.javac.code.TypeTag.*;
duke@1 42
duke@1 43 /** This class represents Java types. The class itself defines the behavior of
duke@1 44 * the following types:
duke@1 45 * <pre>
duke@1 46 * base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
duke@1 47 * type `void' (tag: VOID),
duke@1 48 * the bottom type (tag: BOT),
duke@1 49 * the missing type (tag: NONE).
duke@1 50 * </pre>
duke@1 51 * <p>The behavior of the following types is defined in subclasses, which are
duke@1 52 * all static inner classes of this class:
duke@1 53 * <pre>
duke@1 54 * class types (tag: CLASS, class: ClassType),
duke@1 55 * array types (tag: ARRAY, class: ArrayType),
duke@1 56 * method types (tag: METHOD, class: MethodType),
duke@1 57 * package types (tag: PACKAGE, class: PackageType),
duke@1 58 * type variables (tag: TYPEVAR, class: TypeVar),
duke@1 59 * type arguments (tag: WILDCARD, class: WildcardType),
mcimadamore@1268 60 * generic method types (tag: FORALL, class: ForAll),
duke@1 61 * the error type (tag: ERROR, class: ErrorType).
duke@1 62 * </pre>
duke@1 63 *
jjg@581 64 * <p><b>This is NOT part of any supported API.
jjg@581 65 * If you write code that depends on this, you do so at your own risk.
duke@1 66 * This code and its internal interfaces are subject to change or
duke@1 67 * deletion without notice.</b>
duke@1 68 *
jjg@1374 69 * @see TypeTag
duke@1 70 */
duke@1 71 public class Type implements PrimitiveType {
duke@1 72
duke@1 73 /** Constant type: no type at all. */
duke@1 74 public static final JCNoType noType = new JCNoType(NONE);
duke@1 75
mcimadamore@1347 76 /** Constant type: special type to be used during recovery of deferred expressions. */
mcimadamore@1347 77 public static final JCNoType recoveryType = new JCNoType(NONE);
mcimadamore@1347 78
duke@1 79 /** If this switch is turned on, the names of type variables
duke@1 80 * and anonymous classes are printed with hashcodes appended.
duke@1 81 */
duke@1 82 public static boolean moreInfo = false;
duke@1 83
duke@1 84 /** The tag of this type.
duke@1 85 *
jjg@1374 86 * @see TypeTag
duke@1 87 */
jjg@1374 88 protected TypeTag tag;
duke@1 89
duke@1 90 /** The defining class / interface / package / type variable
duke@1 91 */
duke@1 92 public TypeSymbol tsym;
duke@1 93
duke@1 94 /**
jjg@1374 95 * Checks if the current type tag is equal to the given tag.
jjg@1374 96 * @return true if tag is equal to the current type tag.
jjg@1374 97 */
jjg@1374 98 public boolean hasTag(TypeTag tag) {
jjg@1374 99 return this.tag == tag;
jjg@1374 100 }
jjg@1374 101
jjg@1374 102 /**
jjg@1374 103 * Returns the current type tag.
jjg@1374 104 * @return the value of the current type tag.
jjg@1374 105 */
jjg@1374 106 public TypeTag getTag() {
jjg@1374 107 return tag;
jjg@1374 108 }
jjg@1374 109
jjg@1374 110 public boolean isNumeric() {
jjg@1374 111 switch (tag) {
jjg@1374 112 case BYTE: case CHAR:
jjg@1374 113 case SHORT:
jjg@1374 114 case INT: case LONG:
jjg@1374 115 case FLOAT: case DOUBLE:
jjg@1374 116 return true;
jjg@1374 117 default:
jjg@1374 118 return false;
jjg@1374 119 }
jjg@1374 120 }
jjg@1374 121
jjg@1374 122 public boolean isPrimitive() {
jjg@1374 123 return (isNumeric() || tag == BOOLEAN);
jjg@1374 124 }
jjg@1374 125
jjg@1374 126 public boolean isPrimitiveOrVoid() {
jjg@1374 127 return (isPrimitive() || tag == VOID);
jjg@1374 128 }
jjg@1374 129
jjg@1374 130 public boolean isReference() {
jjg@1374 131 switch (tag) {
jjg@1374 132 case CLASS:
jjg@1374 133 case ARRAY:
jjg@1374 134 case TYPEVAR:
jjg@1374 135 case WILDCARD:
jjg@1374 136 case ERROR:
jjg@1374 137 return true;
jjg@1374 138 default:
jjg@1374 139 return false;
jjg@1374 140 }
jjg@1374 141 }
jjg@1374 142
jjg@1374 143 public boolean isNullOrReference() {
jjg@1374 144 return (tag == BOT || isReference());
jjg@1374 145 }
jjg@1374 146
jjg@1374 147 public boolean isPartial() {
jjg@1374 148 switch(tag) {
jjg@1374 149 case ERROR: case UNKNOWN: case UNDETVAR:
jjg@1374 150 return true;
jjg@1374 151 default:
jjg@1374 152 return false;
jjg@1374 153 }
jjg@1374 154 }
jjg@1374 155
jjg@1374 156 /**
duke@1 157 * The constant value of this type, null if this type does not
duke@1 158 * have a constant value attribute. Only primitive types and
duke@1 159 * strings (ClassType) can have a constant value attribute.
duke@1 160 * @return the constant value attribute of this type
duke@1 161 */
duke@1 162 public Object constValue() {
duke@1 163 return null;
duke@1 164 }
duke@1 165
jjg@904 166 /**
jjg@904 167 * Get the representation of this type used for modelling purposes.
jjg@904 168 * By default, this is itself. For ErrorType, a different value
jjg@904 169 * may be provided,
jjg@904 170 */
jjg@904 171 public Type getModelType() {
jjg@904 172 return this;
jjg@904 173 }
jjg@904 174
jjg@904 175 public static List<Type> getModelTypes(List<Type> ts) {
jjg@904 176 ListBuffer<Type> lb = new ListBuffer<Type>();
jjg@904 177 for (Type t: ts)
jjg@904 178 lb.append(t.getModelType());
jjg@904 179 return lb.toList();
jjg@904 180 }
jjg@904 181
duke@1 182 public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
duke@1 183
duke@1 184 /** Define a type given its tag and type symbol
duke@1 185 */
jjg@1374 186 public Type(TypeTag tag, TypeSymbol tsym) {
duke@1 187 this.tag = tag;
duke@1 188 this.tsym = tsym;
duke@1 189 }
duke@1 190
duke@1 191 /** An abstract class for mappings from types to types
duke@1 192 */
duke@1 193 public static abstract class Mapping {
duke@1 194 private String name;
duke@1 195 public Mapping(String name) {
duke@1 196 this.name = name;
duke@1 197 }
duke@1 198 public abstract Type apply(Type t);
duke@1 199 public String toString() {
duke@1 200 return name;
duke@1 201 }
duke@1 202 }
duke@1 203
duke@1 204 /** map a type function over all immediate descendants of this type
duke@1 205 */
duke@1 206 public Type map(Mapping f) {
duke@1 207 return this;
duke@1 208 }
duke@1 209
duke@1 210 /** map a type function over a list of types
duke@1 211 */
duke@1 212 public static List<Type> map(List<Type> ts, Mapping f) {
duke@1 213 if (ts.nonEmpty()) {
duke@1 214 List<Type> tail1 = map(ts.tail, f);
duke@1 215 Type t = f.apply(ts.head);
duke@1 216 if (tail1 != ts.tail || t != ts.head)
duke@1 217 return tail1.prepend(t);
duke@1 218 }
duke@1 219 return ts;
duke@1 220 }
duke@1 221
duke@1 222 /** Define a constant type, of the same kind as this type
duke@1 223 * and with given constant value
duke@1 224 */
duke@1 225 public Type constType(Object constValue) {
duke@1 226 final Object value = constValue;
jjg@1374 227 Assert.check(isPrimitive());
duke@1 228 return new Type(tag, tsym) {
duke@1 229 @Override
duke@1 230 public Object constValue() {
duke@1 231 return value;
duke@1 232 }
duke@1 233 @Override
duke@1 234 public Type baseType() {
duke@1 235 return tsym.type;
duke@1 236 }
duke@1 237 };
duke@1 238 }
duke@1 239
duke@1 240 /**
duke@1 241 * If this is a constant type, return its underlying type.
duke@1 242 * Otherwise, return the type itself.
duke@1 243 */
duke@1 244 public Type baseType() {
duke@1 245 return this;
duke@1 246 }
duke@1 247
duke@1 248 /** Return the base types of a list of types.
duke@1 249 */
duke@1 250 public static List<Type> baseTypes(List<Type> ts) {
duke@1 251 if (ts.nonEmpty()) {
duke@1 252 Type t = ts.head.baseType();
duke@1 253 List<Type> baseTypes = baseTypes(ts.tail);
duke@1 254 if (t != ts.head || baseTypes != ts.tail)
duke@1 255 return baseTypes.prepend(t);
duke@1 256 }
duke@1 257 return ts;
duke@1 258 }
duke@1 259
duke@1 260 /** The Java source which this type represents.
duke@1 261 */
duke@1 262 public String toString() {
duke@1 263 String s = (tsym == null || tsym.name == null)
duke@1 264 ? "<none>"
duke@1 265 : tsym.name.toString();
duke@1 266 if (moreInfo && tag == TYPEVAR) s = s + hashCode();
duke@1 267 return s;
duke@1 268 }
duke@1 269
duke@1 270 /**
duke@1 271 * The Java source which this type list represents. A List is
duke@1 272 * represented as a comma-spearated listing of the elements in
duke@1 273 * that list.
duke@1 274 */
duke@1 275 public static String toString(List<Type> ts) {
duke@1 276 if (ts.isEmpty()) {
duke@1 277 return "";
duke@1 278 } else {
jjg@904 279 StringBuilder buf = new StringBuilder();
duke@1 280 buf.append(ts.head.toString());
duke@1 281 for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
duke@1 282 buf.append(",").append(l.head.toString());
duke@1 283 return buf.toString();
duke@1 284 }
duke@1 285 }
duke@1 286
duke@1 287 /**
duke@1 288 * The constant value of this type, converted to String
duke@1 289 */
duke@1 290 public String stringValue() {
jjg@816 291 Object cv = Assert.checkNonNull(constValue());
duke@1 292 if (tag == BOOLEAN)
jjg@816 293 return ((Integer) cv).intValue() == 0 ? "false" : "true";
duke@1 294 else if (tag == CHAR)
jjg@816 295 return String.valueOf((char) ((Integer) cv).intValue());
duke@1 296 else
jjg@816 297 return cv.toString();
duke@1 298 }
duke@1 299
duke@1 300 /**
duke@1 301 * This method is analogous to isSameType, but weaker, since we
duke@1 302 * never complete classes. Where isSameType would complete a
duke@1 303 * class, equals assumes that the two types are different.
duke@1 304 */
vromero@1452 305 @Override
duke@1 306 public boolean equals(Object t) {
duke@1 307 return super.equals(t);
duke@1 308 }
duke@1 309
vromero@1452 310 @Override
duke@1 311 public int hashCode() {
duke@1 312 return super.hashCode();
duke@1 313 }
duke@1 314
duke@1 315 /** Is this a constant type whose value is false?
duke@1 316 */
duke@1 317 public boolean isFalse() {
duke@1 318 return
duke@1 319 tag == BOOLEAN &&
duke@1 320 constValue() != null &&
duke@1 321 ((Integer)constValue()).intValue() == 0;
duke@1 322 }
duke@1 323
duke@1 324 /** Is this a constant type whose value is true?
duke@1 325 */
duke@1 326 public boolean isTrue() {
duke@1 327 return
duke@1 328 tag == BOOLEAN &&
duke@1 329 constValue() != null &&
duke@1 330 ((Integer)constValue()).intValue() != 0;
duke@1 331 }
duke@1 332
duke@1 333 public String argtypes(boolean varargs) {
duke@1 334 List<Type> args = getParameterTypes();
duke@1 335 if (!varargs) return args.toString();
jjg@789 336 StringBuilder buf = new StringBuilder();
duke@1 337 while (args.tail.nonEmpty()) {
duke@1 338 buf.append(args.head);
duke@1 339 args = args.tail;
duke@1 340 buf.append(',');
duke@1 341 }
duke@1 342 if (args.head.tag == ARRAY) {
duke@1 343 buf.append(((ArrayType)args.head).elemtype);
duke@1 344 buf.append("...");
duke@1 345 } else {
duke@1 346 buf.append(args.head);
duke@1 347 }
duke@1 348 return buf.toString();
duke@1 349 }
duke@1 350
duke@1 351 /** Access methods.
duke@1 352 */
duke@1 353 public List<Type> getTypeArguments() { return List.nil(); }
duke@1 354 public Type getEnclosingType() { return null; }
duke@1 355 public List<Type> getParameterTypes() { return List.nil(); }
duke@1 356 public Type getReturnType() { return null; }
duke@1 357 public List<Type> getThrownTypes() { return List.nil(); }
duke@1 358 public Type getUpperBound() { return null; }
duke@1 359 public Type getLowerBound() { return null; }
duke@1 360
duke@1 361 /** Navigation methods, these will work for classes, type variables,
duke@1 362 * foralls, but will return null for arrays and methods.
duke@1 363 */
duke@1 364
duke@1 365 /** Return all parameters of this type and all its outer types in order
duke@1 366 * outer (first) to inner (last).
duke@1 367 */
duke@1 368 public List<Type> allparams() { return List.nil(); }
duke@1 369
duke@1 370 /** Does this type contain "error" elements?
duke@1 371 */
duke@1 372 public boolean isErroneous() {
duke@1 373 return false;
duke@1 374 }
duke@1 375
duke@1 376 public static boolean isErroneous(List<Type> ts) {
duke@1 377 for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
duke@1 378 if (l.head.isErroneous()) return true;
duke@1 379 return false;
duke@1 380 }
duke@1 381
duke@1 382 /** Is this type parameterized?
duke@1 383 * A class type is parameterized if it has some parameters.
duke@1 384 * An array type is parameterized if its element type is parameterized.
duke@1 385 * All other types are not parameterized.
duke@1 386 */
duke@1 387 public boolean isParameterized() {
duke@1 388 return false;
duke@1 389 }
duke@1 390
duke@1 391 /** Is this type a raw type?
duke@1 392 * A class type is a raw type if it misses some of its parameters.
duke@1 393 * An array type is a raw type if its element type is raw.
duke@1 394 * All other types are not raw.
duke@1 395 * Type validation will ensure that the only raw types
duke@1 396 * in a program are types that miss all their type variables.
duke@1 397 */
duke@1 398 public boolean isRaw() {
duke@1 399 return false;
duke@1 400 }
duke@1 401
duke@1 402 public boolean isCompound() {
duke@1 403 return tsym.completer == null
duke@1 404 // Compound types can't have a completer. Calling
duke@1 405 // flags() will complete the symbol causing the
duke@1 406 // compiler to load classes unnecessarily. This led
duke@1 407 // to regression 6180021.
duke@1 408 && (tsym.flags() & COMPOUND) != 0;
duke@1 409 }
duke@1 410
duke@1 411 public boolean isInterface() {
duke@1 412 return (tsym.flags() & INTERFACE) != 0;
duke@1 413 }
duke@1 414
mcimadamore@640 415 public boolean isFinal() {
mcimadamore@640 416 return (tsym.flags() & FINAL) != 0;
mcimadamore@640 417 }
mcimadamore@640 418
duke@1 419 /**
duke@1 420 * Does this type contain occurrences of type t?
duke@1 421 */
duke@1 422 public boolean contains(Type t) {
duke@1 423 return t == this;
duke@1 424 }
duke@1 425
duke@1 426 public static boolean contains(List<Type> ts, Type t) {
duke@1 427 for (List<Type> l = ts;
duke@1 428 l.tail != null /*inlined: l.nonEmpty()*/;
duke@1 429 l = l.tail)
duke@1 430 if (l.head.contains(t)) return true;
duke@1 431 return false;
duke@1 432 }
duke@1 433
mcimadamore@635 434 /** Does this type contain an occurrence of some type in 'ts'?
duke@1 435 */
mcimadamore@635 436 public boolean containsAny(List<Type> ts) {
mcimadamore@635 437 for (Type t : ts)
mcimadamore@635 438 if (this.contains(t)) return true;
mcimadamore@635 439 return false;
mcimadamore@635 440 }
mcimadamore@635 441
mcimadamore@635 442 public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
mcimadamore@635 443 for (Type t : ts1)
mcimadamore@635 444 if (t.containsAny(ts2)) return true;
duke@1 445 return false;
duke@1 446 }
duke@1 447
mcimadamore@828 448 public static List<Type> filter(List<Type> ts, Filter<Type> tf) {
mcimadamore@828 449 ListBuffer<Type> buf = ListBuffer.lb();
mcimadamore@828 450 for (Type t : ts) {
mcimadamore@828 451 if (tf.accepts(t)) {
mcimadamore@828 452 buf.append(t);
mcimadamore@828 453 }
mcimadamore@828 454 }
mcimadamore@828 455 return buf.toList();
mcimadamore@828 456 }
mcimadamore@828 457
duke@1 458 public boolean isSuperBound() { return false; }
duke@1 459 public boolean isExtendsBound() { return false; }
duke@1 460 public boolean isUnbound() { return false; }
duke@1 461 public Type withTypeVar(Type t) { return this; }
duke@1 462
duke@1 463 /** The underlying method type of this type.
duke@1 464 */
duke@1 465 public MethodType asMethodType() { throw new AssertionError(); }
duke@1 466
duke@1 467 /** Complete loading all classes in this type.
duke@1 468 */
duke@1 469 public void complete() {}
duke@1 470
duke@1 471 public TypeSymbol asElement() {
duke@1 472 return tsym;
duke@1 473 }
duke@1 474
duke@1 475 public TypeKind getKind() {
duke@1 476 switch (tag) {
duke@1 477 case BYTE: return TypeKind.BYTE;
duke@1 478 case CHAR: return TypeKind.CHAR;
duke@1 479 case SHORT: return TypeKind.SHORT;
duke@1 480 case INT: return TypeKind.INT;
duke@1 481 case LONG: return TypeKind.LONG;
duke@1 482 case FLOAT: return TypeKind.FLOAT;
duke@1 483 case DOUBLE: return TypeKind.DOUBLE;
duke@1 484 case BOOLEAN: return TypeKind.BOOLEAN;
duke@1 485 case VOID: return TypeKind.VOID;
duke@1 486 case BOT: return TypeKind.NULL;
duke@1 487 case NONE: return TypeKind.NONE;
duke@1 488 default: return TypeKind.OTHER;
duke@1 489 }
duke@1 490 }
duke@1 491
duke@1 492 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 493 if (isPrimitive())
duke@1 494 return v.visitPrimitive(this, p);
duke@1 495 else
duke@1 496 throw new AssertionError();
duke@1 497 }
duke@1 498
duke@1 499 public static class WildcardType extends Type
duke@1 500 implements javax.lang.model.type.WildcardType {
duke@1 501
duke@1 502 public Type type;
duke@1 503 public BoundKind kind;
duke@1 504 public TypeVar bound;
duke@1 505
duke@1 506 @Override
duke@1 507 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 508 return v.visitWildcardType(this, s);
duke@1 509 }
duke@1 510
duke@1 511 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
duke@1 512 super(WILDCARD, tsym);
jjg@816 513 this.type = Assert.checkNonNull(type);
duke@1 514 this.kind = kind;
duke@1 515 }
duke@1 516 public WildcardType(WildcardType t, TypeVar bound) {
duke@1 517 this(t.type, t.kind, t.tsym, bound);
duke@1 518 }
duke@1 519
duke@1 520 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
duke@1 521 this(type, kind, tsym);
duke@1 522 this.bound = bound;
duke@1 523 }
duke@1 524
mcimadamore@635 525 public boolean contains(Type t) {
mcimadamore@635 526 return kind != UNBOUND && type.contains(t);
mcimadamore@635 527 }
mcimadamore@635 528
duke@1 529 public boolean isSuperBound() {
duke@1 530 return kind == SUPER ||
duke@1 531 kind == UNBOUND;
duke@1 532 }
duke@1 533 public boolean isExtendsBound() {
duke@1 534 return kind == EXTENDS ||
duke@1 535 kind == UNBOUND;
duke@1 536 }
duke@1 537 public boolean isUnbound() {
duke@1 538 return kind == UNBOUND;
duke@1 539 }
duke@1 540
duke@1 541 public Type withTypeVar(Type t) {
duke@1 542 //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
duke@1 543 if (bound == t)
duke@1 544 return this;
duke@1 545 bound = (TypeVar)t;
duke@1 546 return this;
duke@1 547 }
duke@1 548
duke@1 549 boolean isPrintingBound = false;
duke@1 550 public String toString() {
jjg@904 551 StringBuilder s = new StringBuilder();
duke@1 552 s.append(kind.toString());
duke@1 553 if (kind != UNBOUND)
duke@1 554 s.append(type);
duke@1 555 if (moreInfo && bound != null && !isPrintingBound)
duke@1 556 try {
duke@1 557 isPrintingBound = true;
duke@1 558 s.append("{:").append(bound.bound).append(":}");
duke@1 559 } finally {
duke@1 560 isPrintingBound = false;
duke@1 561 }
duke@1 562 return s.toString();
duke@1 563 }
duke@1 564
duke@1 565 public Type map(Mapping f) {
duke@1 566 //- System.err.println(" (" + this + ").map(" + f + ")");//DEBUG
duke@1 567 Type t = type;
duke@1 568 if (t != null)
duke@1 569 t = f.apply(t);
duke@1 570 if (t == type)
duke@1 571 return this;
duke@1 572 else
duke@1 573 return new WildcardType(t, kind, tsym, bound);
duke@1 574 }
duke@1 575
duke@1 576 public Type getExtendsBound() {
duke@1 577 if (kind == EXTENDS)
duke@1 578 return type;
duke@1 579 else
duke@1 580 return null;
duke@1 581 }
duke@1 582
duke@1 583 public Type getSuperBound() {
duke@1 584 if (kind == SUPER)
duke@1 585 return type;
duke@1 586 else
duke@1 587 return null;
duke@1 588 }
duke@1 589
duke@1 590 public TypeKind getKind() {
duke@1 591 return TypeKind.WILDCARD;
duke@1 592 }
duke@1 593
duke@1 594 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 595 return v.visitWildcard(this, p);
duke@1 596 }
duke@1 597 }
duke@1 598
duke@1 599 public static class ClassType extends Type implements DeclaredType {
duke@1 600
duke@1 601 /** The enclosing type of this type. If this is the type of an inner
duke@1 602 * class, outer_field refers to the type of its enclosing
duke@1 603 * instance class, in all other cases it referes to noType.
duke@1 604 */
duke@1 605 private Type outer_field;
duke@1 606
duke@1 607 /** The type parameters of this type (to be set once class is loaded).
duke@1 608 */
duke@1 609 public List<Type> typarams_field;
duke@1 610
duke@1 611 /** A cache variable for the type parameters of this type,
duke@1 612 * appended to all parameters of its enclosing class.
duke@1 613 * @see #allparams
duke@1 614 */
duke@1 615 public List<Type> allparams_field;
duke@1 616
duke@1 617 /** The supertype of this class (to be set once class is loaded).
duke@1 618 */
duke@1 619 public Type supertype_field;
duke@1 620
duke@1 621 /** The interfaces of this class (to be set once class is loaded).
duke@1 622 */
duke@1 623 public List<Type> interfaces_field;
duke@1 624
jjg@904 625 /** All the interfaces of this class, including missing ones.
jjg@904 626 */
jjg@904 627 public List<Type> all_interfaces_field;
jjg@904 628
duke@1 629 public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
duke@1 630 super(CLASS, tsym);
duke@1 631 this.outer_field = outer;
duke@1 632 this.typarams_field = typarams;
duke@1 633 this.allparams_field = null;
duke@1 634 this.supertype_field = null;
duke@1 635 this.interfaces_field = null;
duke@1 636 /*
duke@1 637 // this can happen during error recovery
duke@1 638 assert
duke@1 639 outer.isParameterized() ?
duke@1 640 typarams.length() == tsym.type.typarams().length() :
duke@1 641 outer.isRaw() ?
duke@1 642 typarams.length() == 0 :
duke@1 643 true;
duke@1 644 */
duke@1 645 }
duke@1 646
duke@1 647 @Override
duke@1 648 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 649 return v.visitClassType(this, s);
duke@1 650 }
duke@1 651
duke@1 652 public Type constType(Object constValue) {
duke@1 653 final Object value = constValue;
duke@1 654 return new ClassType(getEnclosingType(), typarams_field, tsym) {
duke@1 655 @Override
duke@1 656 public Object constValue() {
duke@1 657 return value;
duke@1 658 }
duke@1 659 @Override
duke@1 660 public Type baseType() {
duke@1 661 return tsym.type;
duke@1 662 }
duke@1 663 };
duke@1 664 }
duke@1 665
duke@1 666 /** The Java source which this type represents.
duke@1 667 */
duke@1 668 public String toString() {
jjg@904 669 StringBuilder buf = new StringBuilder();
duke@1 670 if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {
duke@1 671 buf.append(getEnclosingType().toString());
duke@1 672 buf.append(".");
duke@1 673 buf.append(className(tsym, false));
duke@1 674 } else {
duke@1 675 buf.append(className(tsym, true));
duke@1 676 }
duke@1 677 if (getTypeArguments().nonEmpty()) {
duke@1 678 buf.append('<');
duke@1 679 buf.append(getTypeArguments().toString());
duke@1 680 buf.append(">");
duke@1 681 }
duke@1 682 return buf.toString();
duke@1 683 }
duke@1 684 //where
duke@1 685 private String className(Symbol sym, boolean longform) {
jjg@113 686 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
jjg@904 687 StringBuilder s = new StringBuilder(supertype_field.toString());
duke@1 688 for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
duke@1 689 s.append("&");
duke@1 690 s.append(is.head.toString());
duke@1 691 }
duke@1 692 return s.toString();
jjg@113 693 } else if (sym.name.isEmpty()) {
duke@1 694 String s;
duke@1 695 ClassType norm = (ClassType) tsym.type;
duke@1 696 if (norm == null) {
duke@1 697 s = Log.getLocalizedString("anonymous.class", (Object)null);
duke@1 698 } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
duke@1 699 s = Log.getLocalizedString("anonymous.class",
duke@1 700 norm.interfaces_field.head);
duke@1 701 } else {
duke@1 702 s = Log.getLocalizedString("anonymous.class",
duke@1 703 norm.supertype_field);
duke@1 704 }
duke@1 705 if (moreInfo)
duke@1 706 s += String.valueOf(sym.hashCode());
duke@1 707 return s;
duke@1 708 } else if (longform) {
duke@1 709 return sym.getQualifiedName().toString();
duke@1 710 } else {
duke@1 711 return sym.name.toString();
duke@1 712 }
duke@1 713 }
duke@1 714
duke@1 715 public List<Type> getTypeArguments() {
duke@1 716 if (typarams_field == null) {
duke@1 717 complete();
duke@1 718 if (typarams_field == null)
duke@1 719 typarams_field = List.nil();
duke@1 720 }
duke@1 721 return typarams_field;
duke@1 722 }
duke@1 723
mcimadamore@30 724 public boolean hasErasedSupertypes() {
mcimadamore@30 725 return isRaw();
mcimadamore@30 726 }
mcimadamore@30 727
duke@1 728 public Type getEnclosingType() {
duke@1 729 return outer_field;
duke@1 730 }
duke@1 731
duke@1 732 public void setEnclosingType(Type outer) {
duke@1 733 outer_field = outer;
duke@1 734 }
duke@1 735
duke@1 736 public List<Type> allparams() {
duke@1 737 if (allparams_field == null) {
duke@1 738 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
duke@1 739 }
duke@1 740 return allparams_field;
duke@1 741 }
duke@1 742
duke@1 743 public boolean isErroneous() {
duke@1 744 return
duke@1 745 getEnclosingType().isErroneous() ||
duke@1 746 isErroneous(getTypeArguments()) ||
duke@1 747 this != tsym.type && tsym.type.isErroneous();
duke@1 748 }
duke@1 749
duke@1 750 public boolean isParameterized() {
duke@1 751 return allparams().tail != null;
duke@1 752 // optimization, was: allparams().nonEmpty();
duke@1 753 }
duke@1 754
duke@1 755 /** A cache for the rank. */
duke@1 756 int rank_field = -1;
duke@1 757
duke@1 758 /** A class type is raw if it misses some
duke@1 759 * of its type parameter sections.
duke@1 760 * After validation, this is equivalent to:
jjg@1326 761 * {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); }
duke@1 762 */
duke@1 763 public boolean isRaw() {
duke@1 764 return
duke@1 765 this != tsym.type && // necessary, but not sufficient condition
duke@1 766 tsym.type.allparams().nonEmpty() &&
duke@1 767 allparams().isEmpty();
duke@1 768 }
duke@1 769
duke@1 770 public Type map(Mapping f) {
duke@1 771 Type outer = getEnclosingType();
duke@1 772 Type outer1 = f.apply(outer);
duke@1 773 List<Type> typarams = getTypeArguments();
duke@1 774 List<Type> typarams1 = map(typarams, f);
duke@1 775 if (outer1 == outer && typarams1 == typarams) return this;
duke@1 776 else return new ClassType(outer1, typarams1, tsym);
duke@1 777 }
duke@1 778
duke@1 779 public boolean contains(Type elem) {
duke@1 780 return
duke@1 781 elem == this
duke@1 782 || (isParameterized()
mcimadamore@635 783 && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
mcimadamore@635 784 || (isCompound()
mcimadamore@635 785 && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
duke@1 786 }
duke@1 787
duke@1 788 public void complete() {
duke@1 789 if (tsym.completer != null) tsym.complete();
duke@1 790 }
duke@1 791
duke@1 792 public TypeKind getKind() {
duke@1 793 return TypeKind.DECLARED;
duke@1 794 }
duke@1 795
duke@1 796 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 797 return v.visitDeclared(this, p);
duke@1 798 }
duke@1 799 }
duke@1 800
mcimadamore@30 801 public static class ErasedClassType extends ClassType {
mcimadamore@30 802 public ErasedClassType(Type outer, TypeSymbol tsym) {
mcimadamore@30 803 super(outer, List.<Type>nil(), tsym);
mcimadamore@30 804 }
mcimadamore@30 805
mcimadamore@30 806 @Override
mcimadamore@30 807 public boolean hasErasedSupertypes() {
mcimadamore@30 808 return true;
mcimadamore@30 809 }
mcimadamore@30 810 }
mcimadamore@30 811
jjg@988 812 // a clone of a ClassType that knows about the alternatives of a union type.
jjg@988 813 public static class UnionClassType extends ClassType implements UnionType {
jjg@988 814 final List<? extends Type> alternatives_field;
jjg@988 815
jjg@988 816 public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
jjg@988 817 super(ct.outer_field, ct.typarams_field, ct.tsym);
jjg@988 818 allparams_field = ct.allparams_field;
jjg@988 819 supertype_field = ct.supertype_field;
jjg@988 820 interfaces_field = ct.interfaces_field;
jjg@988 821 all_interfaces_field = ct.interfaces_field;
jjg@988 822 alternatives_field = alternatives;
jjg@988 823 }
jjg@988 824
jjg@988 825 public Type getLub() {
jjg@988 826 return tsym.type;
jjg@988 827 }
jjg@988 828
jjg@988 829 public java.util.List<? extends TypeMirror> getAlternatives() {
jjg@988 830 return Collections.unmodifiableList(alternatives_field);
jjg@988 831 }
jjg@988 832
jjg@988 833 @Override
jjg@988 834 public TypeKind getKind() {
jjg@988 835 return TypeKind.UNION;
jjg@988 836 }
jjg@988 837
jjg@988 838 @Override
jjg@988 839 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
jjg@988 840 return v.visitUnion(this, p);
jjg@988 841 }
jjg@988 842 }
jjg@988 843
mcimadamore@1436 844 // a clone of a ClassType that knows about the bounds of an intersection type.
mcimadamore@1436 845 public static class IntersectionClassType extends ClassType implements IntersectionType {
mcimadamore@1436 846
mcimadamore@1436 847 public boolean allInterfaces;
mcimadamore@1436 848
mcimadamore@1436 849 public enum IntersectionKind {
mcimadamore@1436 850 EXPLICIT,
mcimadamore@1436 851 IMPLICT;
mcimadamore@1436 852 }
mcimadamore@1436 853
mcimadamore@1436 854 public IntersectionKind intersectionKind;
mcimadamore@1436 855
mcimadamore@1436 856 public IntersectionClassType(List<Type> bounds, ClassSymbol csym, boolean allInterfaces) {
mcimadamore@1436 857 super(Type.noType, List.<Type>nil(), csym);
mcimadamore@1436 858 this.allInterfaces = allInterfaces;
mcimadamore@1436 859 Assert.check((csym.flags() & COMPOUND) != 0);
mcimadamore@1436 860 supertype_field = bounds.head;
mcimadamore@1436 861 interfaces_field = bounds.tail;
mcimadamore@1436 862 Assert.check(supertype_field.tsym.completer != null ||
mcimadamore@1436 863 !supertype_field.isInterface(), supertype_field);
mcimadamore@1436 864 }
mcimadamore@1436 865
mcimadamore@1436 866 public java.util.List<? extends TypeMirror> getBounds() {
mcimadamore@1436 867 return Collections.unmodifiableList(getComponents());
mcimadamore@1436 868 }
mcimadamore@1436 869
mcimadamore@1436 870 public List<Type> getComponents() {
mcimadamore@1436 871 return interfaces_field.prepend(supertype_field);
mcimadamore@1436 872 }
mcimadamore@1436 873
mcimadamore@1436 874 @Override
mcimadamore@1436 875 public TypeKind getKind() {
mcimadamore@1436 876 return TypeKind.INTERSECTION;
mcimadamore@1436 877 }
mcimadamore@1436 878
mcimadamore@1436 879 @Override
mcimadamore@1436 880 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
mcimadamore@1436 881 return intersectionKind == IntersectionKind.EXPLICIT ?
mcimadamore@1436 882 v.visitIntersection(this, p) :
mcimadamore@1436 883 v.visitDeclared(this, p);
mcimadamore@1436 884 }
mcimadamore@1436 885 }
mcimadamore@1436 886
duke@1 887 public static class ArrayType extends Type
duke@1 888 implements javax.lang.model.type.ArrayType {
duke@1 889
duke@1 890 public Type elemtype;
duke@1 891
duke@1 892 public ArrayType(Type elemtype, TypeSymbol arrayClass) {
duke@1 893 super(ARRAY, arrayClass);
duke@1 894 this.elemtype = elemtype;
duke@1 895 }
duke@1 896
duke@1 897 @Override
duke@1 898 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 899 return v.visitArrayType(this, s);
duke@1 900 }
duke@1 901
duke@1 902 public String toString() {
duke@1 903 return elemtype + "[]";
duke@1 904 }
duke@1 905
duke@1 906 public boolean equals(Object obj) {
duke@1 907 return
duke@1 908 this == obj ||
duke@1 909 (obj instanceof ArrayType &&
duke@1 910 this.elemtype.equals(((ArrayType)obj).elemtype));
duke@1 911 }
duke@1 912
duke@1 913 public int hashCode() {
jjg@1374 914 return (ARRAY.ordinal() << 5) + elemtype.hashCode();
duke@1 915 }
duke@1 916
mcimadamore@795 917 public boolean isVarargs() {
mcimadamore@795 918 return false;
mcimadamore@795 919 }
mcimadamore@795 920
duke@1 921 public List<Type> allparams() { return elemtype.allparams(); }
duke@1 922
duke@1 923 public boolean isErroneous() {
duke@1 924 return elemtype.isErroneous();
duke@1 925 }
duke@1 926
duke@1 927 public boolean isParameterized() {
duke@1 928 return elemtype.isParameterized();
duke@1 929 }
duke@1 930
duke@1 931 public boolean isRaw() {
duke@1 932 return elemtype.isRaw();
duke@1 933 }
duke@1 934
mcimadamore@795 935 public ArrayType makeVarargs() {
mcimadamore@795 936 return new ArrayType(elemtype, tsym) {
mcimadamore@795 937 @Override
mcimadamore@795 938 public boolean isVarargs() {
mcimadamore@795 939 return true;
mcimadamore@795 940 }
mcimadamore@795 941 };
mcimadamore@795 942 }
mcimadamore@795 943
duke@1 944 public Type map(Mapping f) {
duke@1 945 Type elemtype1 = f.apply(elemtype);
duke@1 946 if (elemtype1 == elemtype) return this;
duke@1 947 else return new ArrayType(elemtype1, tsym);
duke@1 948 }
duke@1 949
duke@1 950 public boolean contains(Type elem) {
duke@1 951 return elem == this || elemtype.contains(elem);
duke@1 952 }
duke@1 953
duke@1 954 public void complete() {
duke@1 955 elemtype.complete();
duke@1 956 }
duke@1 957
duke@1 958 public Type getComponentType() {
duke@1 959 return elemtype;
duke@1 960 }
duke@1 961
duke@1 962 public TypeKind getKind() {
duke@1 963 return TypeKind.ARRAY;
duke@1 964 }
duke@1 965
duke@1 966 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 967 return v.visitArray(this, p);
duke@1 968 }
duke@1 969 }
duke@1 970
dlsmith@880 971 public static class MethodType extends Type implements ExecutableType {
duke@1 972
duke@1 973 public List<Type> argtypes;
duke@1 974 public Type restype;
duke@1 975 public List<Type> thrown;
duke@1 976
duke@1 977 public MethodType(List<Type> argtypes,
duke@1 978 Type restype,
duke@1 979 List<Type> thrown,
duke@1 980 TypeSymbol methodClass) {
duke@1 981 super(METHOD, methodClass);
duke@1 982 this.argtypes = argtypes;
duke@1 983 this.restype = restype;
duke@1 984 this.thrown = thrown;
duke@1 985 }
duke@1 986
duke@1 987 @Override
duke@1 988 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 989 return v.visitMethodType(this, s);
duke@1 990 }
duke@1 991
duke@1 992 /** The Java source which this type represents.
duke@1 993 *
duke@1 994 * XXX 06/09/99 iris This isn't correct Java syntax, but it probably
duke@1 995 * should be.
duke@1 996 */
duke@1 997 public String toString() {
duke@1 998 return "(" + argtypes + ")" + restype;
duke@1 999 }
duke@1 1000
duke@1 1001 public List<Type> getParameterTypes() { return argtypes; }
duke@1 1002 public Type getReturnType() { return restype; }
duke@1 1003 public List<Type> getThrownTypes() { return thrown; }
duke@1 1004
duke@1 1005 public boolean isErroneous() {
duke@1 1006 return
duke@1 1007 isErroneous(argtypes) ||
duke@1 1008 restype != null && restype.isErroneous();
duke@1 1009 }
duke@1 1010
duke@1 1011 public Type map(Mapping f) {
duke@1 1012 List<Type> argtypes1 = map(argtypes, f);
duke@1 1013 Type restype1 = f.apply(restype);
duke@1 1014 List<Type> thrown1 = map(thrown, f);
duke@1 1015 if (argtypes1 == argtypes &&
duke@1 1016 restype1 == restype &&
duke@1 1017 thrown1 == thrown) return this;
duke@1 1018 else return new MethodType(argtypes1, restype1, thrown1, tsym);
duke@1 1019 }
duke@1 1020
duke@1 1021 public boolean contains(Type elem) {
duke@1 1022 return elem == this || contains(argtypes, elem) || restype.contains(elem);
duke@1 1023 }
duke@1 1024
duke@1 1025 public MethodType asMethodType() { return this; }
duke@1 1026
duke@1 1027 public void complete() {
duke@1 1028 for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
duke@1 1029 l.head.complete();
duke@1 1030 restype.complete();
duke@1 1031 for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
duke@1 1032 l.head.complete();
duke@1 1033 }
duke@1 1034
duke@1 1035 public List<TypeVar> getTypeVariables() {
duke@1 1036 return List.nil();
duke@1 1037 }
duke@1 1038
duke@1 1039 public TypeSymbol asElement() {
duke@1 1040 return null;
duke@1 1041 }
duke@1 1042
duke@1 1043 public TypeKind getKind() {
duke@1 1044 return TypeKind.EXECUTABLE;
duke@1 1045 }
duke@1 1046
duke@1 1047 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1048 return v.visitExecutable(this, p);
duke@1 1049 }
duke@1 1050 }
duke@1 1051
duke@1 1052 public static class PackageType extends Type implements NoType {
duke@1 1053
duke@1 1054 PackageType(TypeSymbol tsym) {
duke@1 1055 super(PACKAGE, tsym);
duke@1 1056 }
duke@1 1057
duke@1 1058 @Override
duke@1 1059 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1060 return v.visitPackageType(this, s);
duke@1 1061 }
duke@1 1062
duke@1 1063 public String toString() {
duke@1 1064 return tsym.getQualifiedName().toString();
duke@1 1065 }
duke@1 1066
duke@1 1067 public TypeKind getKind() {
duke@1 1068 return TypeKind.PACKAGE;
duke@1 1069 }
duke@1 1070
duke@1 1071 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1072 return v.visitNoType(this, p);
duke@1 1073 }
duke@1 1074 }
duke@1 1075
duke@1 1076 public static class TypeVar extends Type implements TypeVariable {
duke@1 1077
jjg@789 1078 /** The upper bound of this type variable; set from outside.
duke@1 1079 * Must be nonempty once it is set.
duke@1 1080 * For a bound, `bound' is the bound type itself.
duke@1 1081 * Multiple bounds are expressed as a single class type which has the
duke@1 1082 * individual bounds as superclass, respectively interfaces.
duke@1 1083 * The class type then has as `tsym' a compiler generated class `c',
duke@1 1084 * which has a flag COMPOUND and whose owner is the type variable
duke@1 1085 * itself. Furthermore, the erasure_field of the class
duke@1 1086 * points to the first class or interface bound.
duke@1 1087 */
duke@1 1088 public Type bound = null;
jjg@789 1089
jjg@789 1090 /** The lower bound of this type variable.
jjg@789 1091 * TypeVars don't normally have a lower bound, so it is normally set
jjg@789 1092 * to syms.botType.
jjg@789 1093 * Subtypes, such as CapturedType, may provide a different value.
jjg@789 1094 */
duke@1 1095 public Type lower;
duke@1 1096
duke@1 1097 public TypeVar(Name name, Symbol owner, Type lower) {
duke@1 1098 super(TYPEVAR, null);
duke@1 1099 tsym = new TypeSymbol(0, name, this, owner);
duke@1 1100 this.lower = lower;
duke@1 1101 }
duke@1 1102
duke@1 1103 public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
duke@1 1104 super(TYPEVAR, tsym);
duke@1 1105 this.bound = bound;
duke@1 1106 this.lower = lower;
duke@1 1107 }
duke@1 1108
duke@1 1109 @Override
duke@1 1110 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1111 return v.visitTypeVar(this, s);
duke@1 1112 }
duke@1 1113
jjg@789 1114 @Override
duke@1 1115 public Type getUpperBound() { return bound; }
duke@1 1116
duke@1 1117 int rank_field = -1;
duke@1 1118
jjg@789 1119 @Override
duke@1 1120 public Type getLowerBound() {
duke@1 1121 return lower;
duke@1 1122 }
duke@1 1123
duke@1 1124 public TypeKind getKind() {
duke@1 1125 return TypeKind.TYPEVAR;
duke@1 1126 }
duke@1 1127
mcimadamore@79 1128 public boolean isCaptured() {
mcimadamore@79 1129 return false;
mcimadamore@79 1130 }
mcimadamore@79 1131
duke@1 1132 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1133 return v.visitTypeVariable(this, p);
duke@1 1134 }
duke@1 1135 }
duke@1 1136
duke@1 1137 /** A captured type variable comes from wildcards which can have
duke@1 1138 * both upper and lower bound. CapturedType extends TypeVar with
duke@1 1139 * a lower bound.
duke@1 1140 */
duke@1 1141 public static class CapturedType extends TypeVar {
duke@1 1142
duke@1 1143 public WildcardType wildcard;
duke@1 1144
duke@1 1145 public CapturedType(Name name,
duke@1 1146 Symbol owner,
duke@1 1147 Type upper,
duke@1 1148 Type lower,
duke@1 1149 WildcardType wildcard) {
duke@1 1150 super(name, owner, lower);
jjg@816 1151 this.lower = Assert.checkNonNull(lower);
duke@1 1152 this.bound = upper;
duke@1 1153 this.wildcard = wildcard;
duke@1 1154 }
duke@1 1155
duke@1 1156 @Override
duke@1 1157 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1158 return v.visitCapturedType(this, s);
duke@1 1159 }
duke@1 1160
duke@1 1161 @Override
mcimadamore@79 1162 public boolean isCaptured() {
mcimadamore@79 1163 return true;
mcimadamore@79 1164 }
mcimadamore@79 1165
mcimadamore@79 1166 @Override
duke@1 1167 public String toString() {
duke@1 1168 return "capture#"
mcimadamore@288 1169 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
duke@1 1170 + " of "
duke@1 1171 + wildcard;
duke@1 1172 }
duke@1 1173 }
duke@1 1174
duke@1 1175 public static abstract class DelegatedType extends Type {
duke@1 1176 public Type qtype;
jjg@1374 1177 public DelegatedType(TypeTag tag, Type qtype) {
duke@1 1178 super(tag, qtype.tsym);
duke@1 1179 this.qtype = qtype;
duke@1 1180 }
duke@1 1181 public String toString() { return qtype.toString(); }
duke@1 1182 public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
duke@1 1183 public Type getEnclosingType() { return qtype.getEnclosingType(); }
duke@1 1184 public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
duke@1 1185 public Type getReturnType() { return qtype.getReturnType(); }
duke@1 1186 public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
duke@1 1187 public List<Type> allparams() { return qtype.allparams(); }
duke@1 1188 public Type getUpperBound() { return qtype.getUpperBound(); }
duke@1 1189 public boolean isErroneous() { return qtype.isErroneous(); }
duke@1 1190 }
duke@1 1191
mcimadamore@1268 1192 /**
mcimadamore@1268 1193 * The type of a generic method type. It consists of a method type and
mcimadamore@1268 1194 * a list of method type-parameters that are used within the method
mcimadamore@1268 1195 * type.
mcimadamore@1268 1196 */
dlsmith@880 1197 public static class ForAll extends DelegatedType implements ExecutableType {
duke@1 1198 public List<Type> tvars;
duke@1 1199
duke@1 1200 public ForAll(List<Type> tvars, Type qtype) {
mcimadamore@1268 1201 super(FORALL, (MethodType)qtype);
duke@1 1202 this.tvars = tvars;
duke@1 1203 }
duke@1 1204
duke@1 1205 @Override
duke@1 1206 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1207 return v.visitForAll(this, s);
duke@1 1208 }
duke@1 1209
duke@1 1210 public String toString() {
duke@1 1211 return "<" + tvars + ">" + qtype;
duke@1 1212 }
duke@1 1213
duke@1 1214 public List<Type> getTypeArguments() { return tvars; }
duke@1 1215
duke@1 1216 public boolean isErroneous() {
duke@1 1217 return qtype.isErroneous();
duke@1 1218 }
duke@1 1219
duke@1 1220 public Type map(Mapping f) {
duke@1 1221 return f.apply(qtype);
duke@1 1222 }
duke@1 1223
duke@1 1224 public boolean contains(Type elem) {
duke@1 1225 return qtype.contains(elem);
duke@1 1226 }
duke@1 1227
duke@1 1228 public MethodType asMethodType() {
mcimadamore@1268 1229 return (MethodType)qtype;
duke@1 1230 }
duke@1 1231
duke@1 1232 public void complete() {
duke@1 1233 for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
duke@1 1234 ((TypeVar)l.head).bound.complete();
duke@1 1235 }
duke@1 1236 qtype.complete();
duke@1 1237 }
duke@1 1238
duke@1 1239 public List<TypeVar> getTypeVariables() {
duke@1 1240 return List.convert(TypeVar.class, getTypeArguments());
duke@1 1241 }
duke@1 1242
duke@1 1243 public TypeKind getKind() {
duke@1 1244 return TypeKind.EXECUTABLE;
duke@1 1245 }
duke@1 1246
duke@1 1247 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1248 return v.visitExecutable(this, p);
duke@1 1249 }
duke@1 1250 }
duke@1 1251
mcimadamore@1338 1252 /** A class for inference variables, for use during method/diamond type
mcimadamore@1338 1253 * inference. An inference variable has upper/lower bounds and a set
mcimadamore@1338 1254 * of equality constraints. Such bounds are set during subtyping, type-containment,
mcimadamore@1338 1255 * type-equality checks, when the types being tested contain inference variables.
mcimadamore@1338 1256 * A change listener can be attached to an inference variable, to receive notifications
mcimadamore@1338 1257 * whenever the bounds of an inference variable change.
duke@1 1258 */
duke@1 1259 public static class UndetVar extends DelegatedType {
mcimadamore@1338 1260
mcimadamore@1338 1261 /** Inference variable change listener. The listener method is called
mcimadamore@1338 1262 * whenever a change to the inference variable's bounds occurs
mcimadamore@1338 1263 */
mcimadamore@1338 1264 public interface UndetVarListener {
mcimadamore@1338 1265 /** called when some inference variable bounds (of given kinds ibs) change */
mcimadamore@1338 1266 void varChanged(UndetVar uv, Set<InferenceBound> ibs);
mcimadamore@1338 1267 }
mcimadamore@1338 1268
mcimadamore@1338 1269 /**
mcimadamore@1338 1270 * Inference variable bound kinds
mcimadamore@1338 1271 */
mcimadamore@1338 1272 public enum InferenceBound {
mcimadamore@1338 1273 /** upper bounds */
mcimadamore@1338 1274 UPPER,
mcimadamore@1338 1275 /** lower bounds */
mcimadamore@1338 1276 LOWER,
mcimadamore@1338 1277 /** equality constraints */
mcimadamore@1338 1278 EQ;
mcimadamore@1338 1279 }
mcimadamore@1338 1280
mcimadamore@1338 1281 /** inference variable bounds */
mcimadamore@1338 1282 private Map<InferenceBound, List<Type>> bounds;
mcimadamore@1338 1283
mcimadamore@1338 1284 /** inference variable's inferred type (set from Infer.java) */
duke@1 1285 public Type inst = null;
duke@1 1286
mcimadamore@1338 1287 /** inference variable's change listener */
mcimadamore@1338 1288 public UndetVarListener listener = null;
mcimadamore@1338 1289
duke@1 1290 @Override
duke@1 1291 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1292 return v.visitUndetVar(this, s);
duke@1 1293 }
duke@1 1294
mcimadamore@1338 1295 public UndetVar(TypeVar origin, Types types) {
mcimadamore@1348 1296 this(origin, types, true);
mcimadamore@1348 1297 }
mcimadamore@1348 1298
mcimadamore@1348 1299 public UndetVar(TypeVar origin, Types types, boolean includeBounds) {
duke@1 1300 super(UNDETVAR, origin);
mcimadamore@1338 1301 bounds = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
mcimadamore@1348 1302 bounds.put(InferenceBound.UPPER, includeBounds ? types.getBounds(origin) : List.<Type>nil());
mcimadamore@1338 1303 bounds.put(InferenceBound.LOWER, List.<Type>nil());
mcimadamore@1338 1304 bounds.put(InferenceBound.EQ, List.<Type>nil());
duke@1 1305 }
duke@1 1306
duke@1 1307 public String toString() {
duke@1 1308 if (inst != null) return inst.toString();
duke@1 1309 else return qtype + "?";
duke@1 1310 }
duke@1 1311
duke@1 1312 public Type baseType() {
duke@1 1313 if (inst != null) return inst.baseType();
duke@1 1314 else return this;
duke@1 1315 }
mcimadamore@1338 1316
mcimadamore@1338 1317 /** get all bounds of a given kind */
mcimadamore@1338 1318 public List<Type> getBounds(InferenceBound ib) {
mcimadamore@1338 1319 return bounds.get(ib);
mcimadamore@1338 1320 }
mcimadamore@1338 1321
mcimadamore@1338 1322 /** add a bound of a given kind - this might trigger listener notification */
mcimadamore@1338 1323 public void addBound(InferenceBound ib, Type bound, Types types) {
mcimadamore@1338 1324 List<Type> prevBounds = bounds.get(ib);
mcimadamore@1338 1325 for (Type b : prevBounds) {
mcimadamore@1338 1326 if (types.isSameType(b, bound)) {
mcimadamore@1338 1327 return;
mcimadamore@1338 1328 }
mcimadamore@1338 1329 }
mcimadamore@1338 1330 bounds.put(ib, prevBounds.prepend(bound));
mcimadamore@1338 1331 notifyChange(EnumSet.of(ib));
mcimadamore@1338 1332 }
mcimadamore@1338 1333
mcimadamore@1338 1334 /** replace types in all bounds - this might trigger listener notification */
mcimadamore@1338 1335 public void substBounds(List<Type> from, List<Type> to, Types types) {
mcimadamore@1338 1336 EnumSet<InferenceBound> changed = EnumSet.noneOf(InferenceBound.class);
mcimadamore@1338 1337 Map<InferenceBound, List<Type>> bounds2 = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
mcimadamore@1338 1338 for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) {
mcimadamore@1338 1339 InferenceBound ib = _entry.getKey();
mcimadamore@1338 1340 List<Type> prevBounds = _entry.getValue();
mcimadamore@1338 1341 List<Type> newBounds = types.subst(prevBounds, from, to);
mcimadamore@1338 1342 bounds2.put(ib, newBounds);
mcimadamore@1338 1343 if (prevBounds != newBounds) {
mcimadamore@1338 1344 changed.add(ib);
mcimadamore@1338 1345 }
mcimadamore@1338 1346 }
mcimadamore@1338 1347 if (!changed.isEmpty()) {
mcimadamore@1338 1348 bounds = bounds2;
mcimadamore@1338 1349 notifyChange(changed);
mcimadamore@1338 1350 }
mcimadamore@1338 1351 }
mcimadamore@1338 1352
mcimadamore@1338 1353 private void notifyChange(EnumSet<InferenceBound> ibs) {
mcimadamore@1338 1354 if (listener != null) {
mcimadamore@1338 1355 listener.varChanged(this, ibs);
mcimadamore@1338 1356 }
mcimadamore@1338 1357 }
duke@1 1358 }
duke@1 1359
duke@1 1360 /** Represents VOID or NONE.
duke@1 1361 */
duke@1 1362 static class JCNoType extends Type implements NoType {
jjg@1374 1363 public JCNoType(TypeTag tag) {
duke@1 1364 super(tag, null);
duke@1 1365 }
duke@1 1366
duke@1 1367 @Override
duke@1 1368 public TypeKind getKind() {
duke@1 1369 switch (tag) {
duke@1 1370 case VOID: return TypeKind.VOID;
duke@1 1371 case NONE: return TypeKind.NONE;
duke@1 1372 default:
duke@1 1373 throw new AssertionError("Unexpected tag: " + tag);
duke@1 1374 }
duke@1 1375 }
duke@1 1376
duke@1 1377 @Override
duke@1 1378 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1379 return v.visitNoType(this, p);
duke@1 1380 }
duke@1 1381 }
duke@1 1382
duke@1 1383 static class BottomType extends Type implements NullType {
duke@1 1384 public BottomType() {
jjg@1374 1385 super(BOT, null);
duke@1 1386 }
duke@1 1387
duke@1 1388 @Override
duke@1 1389 public TypeKind getKind() {
duke@1 1390 return TypeKind.NULL;
duke@1 1391 }
duke@1 1392
duke@1 1393 @Override
duke@1 1394 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1395 return v.visitNull(this, p);
duke@1 1396 }
duke@1 1397
duke@1 1398 @Override
duke@1 1399 public Type constType(Object value) {
duke@1 1400 return this;
duke@1 1401 }
duke@1 1402
duke@1 1403 @Override
duke@1 1404 public String stringValue() {
duke@1 1405 return "null";
duke@1 1406 }
duke@1 1407 }
duke@1 1408
duke@1 1409 public static class ErrorType extends ClassType
duke@1 1410 implements javax.lang.model.type.ErrorType {
duke@1 1411
jjg@110 1412 private Type originalType = null;
jjg@110 1413
jjg@110 1414 public ErrorType(Type originalType, TypeSymbol tsym) {
duke@1 1415 super(noType, List.<Type>nil(), null);
duke@1 1416 tag = ERROR;
jjg@110 1417 this.tsym = tsym;
jjg@110 1418 this.originalType = (originalType == null ? noType : originalType);
duke@1 1419 }
duke@1 1420
jjg@110 1421 public ErrorType(ClassSymbol c, Type originalType) {
jjg@110 1422 this(originalType, c);
duke@1 1423 c.type = this;
duke@1 1424 c.kind = ERR;
duke@1 1425 c.members_field = new Scope.ErrorScope(c);
duke@1 1426 }
duke@1 1427
jjg@110 1428 public ErrorType(Name name, TypeSymbol container, Type originalType) {
jjg@110 1429 this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
duke@1 1430 }
duke@1 1431
duke@1 1432 @Override
duke@1 1433 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1434 return v.visitErrorType(this, s);
duke@1 1435 }
duke@1 1436
duke@1 1437 public Type constType(Object constValue) { return this; }
duke@1 1438 public Type getEnclosingType() { return this; }
duke@1 1439 public Type getReturnType() { return this; }
duke@1 1440 public Type asSub(Symbol sym) { return this; }
duke@1 1441 public Type map(Mapping f) { return this; }
duke@1 1442
duke@1 1443 public boolean isGenType(Type t) { return true; }
duke@1 1444 public boolean isErroneous() { return true; }
duke@1 1445 public boolean isCompound() { return false; }
duke@1 1446 public boolean isInterface() { return false; }
duke@1 1447
duke@1 1448 public List<Type> allparams() { return List.nil(); }
duke@1 1449 public List<Type> getTypeArguments() { return List.nil(); }
duke@1 1450
duke@1 1451 public TypeKind getKind() {
duke@1 1452 return TypeKind.ERROR;
duke@1 1453 }
duke@1 1454
jjg@110 1455 public Type getOriginalType() {
jjg@110 1456 return originalType;
jjg@110 1457 }
jjg@110 1458
duke@1 1459 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1460 return v.visitError(this, p);
duke@1 1461 }
duke@1 1462 }
duke@1 1463
duke@1 1464 /**
duke@1 1465 * A visitor for types. A visitor is used to implement operations
duke@1 1466 * (or relations) on types. Most common operations on types are
duke@1 1467 * binary relations and this interface is designed for binary
duke@1 1468 * relations, that is, operations on the form
duke@1 1469 * Type&nbsp;&times;&nbsp;S&nbsp;&rarr;&nbsp;R.
duke@1 1470 * <!-- In plain text: Type x S -> R -->
duke@1 1471 *
duke@1 1472 * @param <R> the return type of the operation implemented by this
duke@1 1473 * visitor; use Void if no return type is needed.
duke@1 1474 * @param <S> the type of the second argument (the first being the
duke@1 1475 * type itself) of the operation implemented by this visitor; use
duke@1 1476 * Void if a second argument is not needed.
duke@1 1477 */
duke@1 1478 public interface Visitor<R,S> {
duke@1 1479 R visitClassType(ClassType t, S s);
duke@1 1480 R visitWildcardType(WildcardType t, S s);
duke@1 1481 R visitArrayType(ArrayType t, S s);
duke@1 1482 R visitMethodType(MethodType t, S s);
duke@1 1483 R visitPackageType(PackageType t, S s);
duke@1 1484 R visitTypeVar(TypeVar t, S s);
duke@1 1485 R visitCapturedType(CapturedType t, S s);
duke@1 1486 R visitForAll(ForAll t, S s);
duke@1 1487 R visitUndetVar(UndetVar t, S s);
duke@1 1488 R visitErrorType(ErrorType t, S s);
duke@1 1489 R visitType(Type t, S s);
duke@1 1490 }
duke@1 1491 }

mercurial