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

Fri, 30 Nov 2012 15:14:48 +0000

author
mcimadamore
date
Fri, 30 Nov 2012 15:14:48 +0000
changeset 1436
f6f1fd261f57
parent 1374
c002fdee76fd
child 1452
de1ec6fc93fe
permissions
-rw-r--r--

8002099: Add support for intersection types in cast expression
Summary: Add parser and type-checking support for intersection types in cast expressions
Reviewed-by: jjg

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 */
duke@1 305 public boolean equals(Object t) {
duke@1 306 return super.equals(t);
duke@1 307 }
duke@1 308
duke@1 309 public int hashCode() {
duke@1 310 return super.hashCode();
duke@1 311 }
duke@1 312
duke@1 313 /** Is this a constant type whose value is false?
duke@1 314 */
duke@1 315 public boolean isFalse() {
duke@1 316 return
duke@1 317 tag == BOOLEAN &&
duke@1 318 constValue() != null &&
duke@1 319 ((Integer)constValue()).intValue() == 0;
duke@1 320 }
duke@1 321
duke@1 322 /** Is this a constant type whose value is true?
duke@1 323 */
duke@1 324 public boolean isTrue() {
duke@1 325 return
duke@1 326 tag == BOOLEAN &&
duke@1 327 constValue() != null &&
duke@1 328 ((Integer)constValue()).intValue() != 0;
duke@1 329 }
duke@1 330
duke@1 331 public String argtypes(boolean varargs) {
duke@1 332 List<Type> args = getParameterTypes();
duke@1 333 if (!varargs) return args.toString();
jjg@789 334 StringBuilder buf = new StringBuilder();
duke@1 335 while (args.tail.nonEmpty()) {
duke@1 336 buf.append(args.head);
duke@1 337 args = args.tail;
duke@1 338 buf.append(',');
duke@1 339 }
duke@1 340 if (args.head.tag == ARRAY) {
duke@1 341 buf.append(((ArrayType)args.head).elemtype);
duke@1 342 buf.append("...");
duke@1 343 } else {
duke@1 344 buf.append(args.head);
duke@1 345 }
duke@1 346 return buf.toString();
duke@1 347 }
duke@1 348
duke@1 349 /** Access methods.
duke@1 350 */
duke@1 351 public List<Type> getTypeArguments() { return List.nil(); }
duke@1 352 public Type getEnclosingType() { return null; }
duke@1 353 public List<Type> getParameterTypes() { return List.nil(); }
duke@1 354 public Type getReturnType() { return null; }
duke@1 355 public List<Type> getThrownTypes() { return List.nil(); }
duke@1 356 public Type getUpperBound() { return null; }
duke@1 357 public Type getLowerBound() { return null; }
duke@1 358
duke@1 359 /** Navigation methods, these will work for classes, type variables,
duke@1 360 * foralls, but will return null for arrays and methods.
duke@1 361 */
duke@1 362
duke@1 363 /** Return all parameters of this type and all its outer types in order
duke@1 364 * outer (first) to inner (last).
duke@1 365 */
duke@1 366 public List<Type> allparams() { return List.nil(); }
duke@1 367
duke@1 368 /** Does this type contain "error" elements?
duke@1 369 */
duke@1 370 public boolean isErroneous() {
duke@1 371 return false;
duke@1 372 }
duke@1 373
duke@1 374 public static boolean isErroneous(List<Type> ts) {
duke@1 375 for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
duke@1 376 if (l.head.isErroneous()) return true;
duke@1 377 return false;
duke@1 378 }
duke@1 379
duke@1 380 /** Is this type parameterized?
duke@1 381 * A class type is parameterized if it has some parameters.
duke@1 382 * An array type is parameterized if its element type is parameterized.
duke@1 383 * All other types are not parameterized.
duke@1 384 */
duke@1 385 public boolean isParameterized() {
duke@1 386 return false;
duke@1 387 }
duke@1 388
duke@1 389 /** Is this type a raw type?
duke@1 390 * A class type is a raw type if it misses some of its parameters.
duke@1 391 * An array type is a raw type if its element type is raw.
duke@1 392 * All other types are not raw.
duke@1 393 * Type validation will ensure that the only raw types
duke@1 394 * in a program are types that miss all their type variables.
duke@1 395 */
duke@1 396 public boolean isRaw() {
duke@1 397 return false;
duke@1 398 }
duke@1 399
duke@1 400 public boolean isCompound() {
duke@1 401 return tsym.completer == null
duke@1 402 // Compound types can't have a completer. Calling
duke@1 403 // flags() will complete the symbol causing the
duke@1 404 // compiler to load classes unnecessarily. This led
duke@1 405 // to regression 6180021.
duke@1 406 && (tsym.flags() & COMPOUND) != 0;
duke@1 407 }
duke@1 408
duke@1 409 public boolean isInterface() {
duke@1 410 return (tsym.flags() & INTERFACE) != 0;
duke@1 411 }
duke@1 412
mcimadamore@640 413 public boolean isFinal() {
mcimadamore@640 414 return (tsym.flags() & FINAL) != 0;
mcimadamore@640 415 }
mcimadamore@640 416
duke@1 417 /**
duke@1 418 * Does this type contain occurrences of type t?
duke@1 419 */
duke@1 420 public boolean contains(Type t) {
duke@1 421 return t == this;
duke@1 422 }
duke@1 423
duke@1 424 public static boolean contains(List<Type> ts, Type t) {
duke@1 425 for (List<Type> l = ts;
duke@1 426 l.tail != null /*inlined: l.nonEmpty()*/;
duke@1 427 l = l.tail)
duke@1 428 if (l.head.contains(t)) return true;
duke@1 429 return false;
duke@1 430 }
duke@1 431
mcimadamore@635 432 /** Does this type contain an occurrence of some type in 'ts'?
duke@1 433 */
mcimadamore@635 434 public boolean containsAny(List<Type> ts) {
mcimadamore@635 435 for (Type t : ts)
mcimadamore@635 436 if (this.contains(t)) return true;
mcimadamore@635 437 return false;
mcimadamore@635 438 }
mcimadamore@635 439
mcimadamore@635 440 public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
mcimadamore@635 441 for (Type t : ts1)
mcimadamore@635 442 if (t.containsAny(ts2)) return true;
duke@1 443 return false;
duke@1 444 }
duke@1 445
mcimadamore@828 446 public static List<Type> filter(List<Type> ts, Filter<Type> tf) {
mcimadamore@828 447 ListBuffer<Type> buf = ListBuffer.lb();
mcimadamore@828 448 for (Type t : ts) {
mcimadamore@828 449 if (tf.accepts(t)) {
mcimadamore@828 450 buf.append(t);
mcimadamore@828 451 }
mcimadamore@828 452 }
mcimadamore@828 453 return buf.toList();
mcimadamore@828 454 }
mcimadamore@828 455
duke@1 456 public boolean isSuperBound() { return false; }
duke@1 457 public boolean isExtendsBound() { return false; }
duke@1 458 public boolean isUnbound() { return false; }
duke@1 459 public Type withTypeVar(Type t) { return this; }
duke@1 460
duke@1 461 /** The underlying method type of this type.
duke@1 462 */
duke@1 463 public MethodType asMethodType() { throw new AssertionError(); }
duke@1 464
duke@1 465 /** Complete loading all classes in this type.
duke@1 466 */
duke@1 467 public void complete() {}
duke@1 468
duke@1 469 public TypeSymbol asElement() {
duke@1 470 return tsym;
duke@1 471 }
duke@1 472
duke@1 473 public TypeKind getKind() {
duke@1 474 switch (tag) {
duke@1 475 case BYTE: return TypeKind.BYTE;
duke@1 476 case CHAR: return TypeKind.CHAR;
duke@1 477 case SHORT: return TypeKind.SHORT;
duke@1 478 case INT: return TypeKind.INT;
duke@1 479 case LONG: return TypeKind.LONG;
duke@1 480 case FLOAT: return TypeKind.FLOAT;
duke@1 481 case DOUBLE: return TypeKind.DOUBLE;
duke@1 482 case BOOLEAN: return TypeKind.BOOLEAN;
duke@1 483 case VOID: return TypeKind.VOID;
duke@1 484 case BOT: return TypeKind.NULL;
duke@1 485 case NONE: return TypeKind.NONE;
duke@1 486 default: return TypeKind.OTHER;
duke@1 487 }
duke@1 488 }
duke@1 489
duke@1 490 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 491 if (isPrimitive())
duke@1 492 return v.visitPrimitive(this, p);
duke@1 493 else
duke@1 494 throw new AssertionError();
duke@1 495 }
duke@1 496
duke@1 497 public static class WildcardType extends Type
duke@1 498 implements javax.lang.model.type.WildcardType {
duke@1 499
duke@1 500 public Type type;
duke@1 501 public BoundKind kind;
duke@1 502 public TypeVar bound;
duke@1 503
duke@1 504 @Override
duke@1 505 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 506 return v.visitWildcardType(this, s);
duke@1 507 }
duke@1 508
duke@1 509 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
duke@1 510 super(WILDCARD, tsym);
jjg@816 511 this.type = Assert.checkNonNull(type);
duke@1 512 this.kind = kind;
duke@1 513 }
duke@1 514 public WildcardType(WildcardType t, TypeVar bound) {
duke@1 515 this(t.type, t.kind, t.tsym, bound);
duke@1 516 }
duke@1 517
duke@1 518 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
duke@1 519 this(type, kind, tsym);
duke@1 520 this.bound = bound;
duke@1 521 }
duke@1 522
mcimadamore@635 523 public boolean contains(Type t) {
mcimadamore@635 524 return kind != UNBOUND && type.contains(t);
mcimadamore@635 525 }
mcimadamore@635 526
duke@1 527 public boolean isSuperBound() {
duke@1 528 return kind == SUPER ||
duke@1 529 kind == UNBOUND;
duke@1 530 }
duke@1 531 public boolean isExtendsBound() {
duke@1 532 return kind == EXTENDS ||
duke@1 533 kind == UNBOUND;
duke@1 534 }
duke@1 535 public boolean isUnbound() {
duke@1 536 return kind == UNBOUND;
duke@1 537 }
duke@1 538
duke@1 539 public Type withTypeVar(Type t) {
duke@1 540 //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
duke@1 541 if (bound == t)
duke@1 542 return this;
duke@1 543 bound = (TypeVar)t;
duke@1 544 return this;
duke@1 545 }
duke@1 546
duke@1 547 boolean isPrintingBound = false;
duke@1 548 public String toString() {
jjg@904 549 StringBuilder s = new StringBuilder();
duke@1 550 s.append(kind.toString());
duke@1 551 if (kind != UNBOUND)
duke@1 552 s.append(type);
duke@1 553 if (moreInfo && bound != null && !isPrintingBound)
duke@1 554 try {
duke@1 555 isPrintingBound = true;
duke@1 556 s.append("{:").append(bound.bound).append(":}");
duke@1 557 } finally {
duke@1 558 isPrintingBound = false;
duke@1 559 }
duke@1 560 return s.toString();
duke@1 561 }
duke@1 562
duke@1 563 public Type map(Mapping f) {
duke@1 564 //- System.err.println(" (" + this + ").map(" + f + ")");//DEBUG
duke@1 565 Type t = type;
duke@1 566 if (t != null)
duke@1 567 t = f.apply(t);
duke@1 568 if (t == type)
duke@1 569 return this;
duke@1 570 else
duke@1 571 return new WildcardType(t, kind, tsym, bound);
duke@1 572 }
duke@1 573
duke@1 574 public Type getExtendsBound() {
duke@1 575 if (kind == EXTENDS)
duke@1 576 return type;
duke@1 577 else
duke@1 578 return null;
duke@1 579 }
duke@1 580
duke@1 581 public Type getSuperBound() {
duke@1 582 if (kind == SUPER)
duke@1 583 return type;
duke@1 584 else
duke@1 585 return null;
duke@1 586 }
duke@1 587
duke@1 588 public TypeKind getKind() {
duke@1 589 return TypeKind.WILDCARD;
duke@1 590 }
duke@1 591
duke@1 592 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 593 return v.visitWildcard(this, p);
duke@1 594 }
duke@1 595 }
duke@1 596
duke@1 597 public static class ClassType extends Type implements DeclaredType {
duke@1 598
duke@1 599 /** The enclosing type of this type. If this is the type of an inner
duke@1 600 * class, outer_field refers to the type of its enclosing
duke@1 601 * instance class, in all other cases it referes to noType.
duke@1 602 */
duke@1 603 private Type outer_field;
duke@1 604
duke@1 605 /** The type parameters of this type (to be set once class is loaded).
duke@1 606 */
duke@1 607 public List<Type> typarams_field;
duke@1 608
duke@1 609 /** A cache variable for the type parameters of this type,
duke@1 610 * appended to all parameters of its enclosing class.
duke@1 611 * @see #allparams
duke@1 612 */
duke@1 613 public List<Type> allparams_field;
duke@1 614
duke@1 615 /** The supertype of this class (to be set once class is loaded).
duke@1 616 */
duke@1 617 public Type supertype_field;
duke@1 618
duke@1 619 /** The interfaces of this class (to be set once class is loaded).
duke@1 620 */
duke@1 621 public List<Type> interfaces_field;
duke@1 622
jjg@904 623 /** All the interfaces of this class, including missing ones.
jjg@904 624 */
jjg@904 625 public List<Type> all_interfaces_field;
jjg@904 626
duke@1 627 public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
duke@1 628 super(CLASS, tsym);
duke@1 629 this.outer_field = outer;
duke@1 630 this.typarams_field = typarams;
duke@1 631 this.allparams_field = null;
duke@1 632 this.supertype_field = null;
duke@1 633 this.interfaces_field = null;
duke@1 634 /*
duke@1 635 // this can happen during error recovery
duke@1 636 assert
duke@1 637 outer.isParameterized() ?
duke@1 638 typarams.length() == tsym.type.typarams().length() :
duke@1 639 outer.isRaw() ?
duke@1 640 typarams.length() == 0 :
duke@1 641 true;
duke@1 642 */
duke@1 643 }
duke@1 644
duke@1 645 @Override
duke@1 646 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 647 return v.visitClassType(this, s);
duke@1 648 }
duke@1 649
duke@1 650 public Type constType(Object constValue) {
duke@1 651 final Object value = constValue;
duke@1 652 return new ClassType(getEnclosingType(), typarams_field, tsym) {
duke@1 653 @Override
duke@1 654 public Object constValue() {
duke@1 655 return value;
duke@1 656 }
duke@1 657 @Override
duke@1 658 public Type baseType() {
duke@1 659 return tsym.type;
duke@1 660 }
duke@1 661 };
duke@1 662 }
duke@1 663
duke@1 664 /** The Java source which this type represents.
duke@1 665 */
duke@1 666 public String toString() {
jjg@904 667 StringBuilder buf = new StringBuilder();
duke@1 668 if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {
duke@1 669 buf.append(getEnclosingType().toString());
duke@1 670 buf.append(".");
duke@1 671 buf.append(className(tsym, false));
duke@1 672 } else {
duke@1 673 buf.append(className(tsym, true));
duke@1 674 }
duke@1 675 if (getTypeArguments().nonEmpty()) {
duke@1 676 buf.append('<');
duke@1 677 buf.append(getTypeArguments().toString());
duke@1 678 buf.append(">");
duke@1 679 }
duke@1 680 return buf.toString();
duke@1 681 }
duke@1 682 //where
duke@1 683 private String className(Symbol sym, boolean longform) {
jjg@113 684 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
jjg@904 685 StringBuilder s = new StringBuilder(supertype_field.toString());
duke@1 686 for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
duke@1 687 s.append("&");
duke@1 688 s.append(is.head.toString());
duke@1 689 }
duke@1 690 return s.toString();
jjg@113 691 } else if (sym.name.isEmpty()) {
duke@1 692 String s;
duke@1 693 ClassType norm = (ClassType) tsym.type;
duke@1 694 if (norm == null) {
duke@1 695 s = Log.getLocalizedString("anonymous.class", (Object)null);
duke@1 696 } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
duke@1 697 s = Log.getLocalizedString("anonymous.class",
duke@1 698 norm.interfaces_field.head);
duke@1 699 } else {
duke@1 700 s = Log.getLocalizedString("anonymous.class",
duke@1 701 norm.supertype_field);
duke@1 702 }
duke@1 703 if (moreInfo)
duke@1 704 s += String.valueOf(sym.hashCode());
duke@1 705 return s;
duke@1 706 } else if (longform) {
duke@1 707 return sym.getQualifiedName().toString();
duke@1 708 } else {
duke@1 709 return sym.name.toString();
duke@1 710 }
duke@1 711 }
duke@1 712
duke@1 713 public List<Type> getTypeArguments() {
duke@1 714 if (typarams_field == null) {
duke@1 715 complete();
duke@1 716 if (typarams_field == null)
duke@1 717 typarams_field = List.nil();
duke@1 718 }
duke@1 719 return typarams_field;
duke@1 720 }
duke@1 721
mcimadamore@30 722 public boolean hasErasedSupertypes() {
mcimadamore@30 723 return isRaw();
mcimadamore@30 724 }
mcimadamore@30 725
duke@1 726 public Type getEnclosingType() {
duke@1 727 return outer_field;
duke@1 728 }
duke@1 729
duke@1 730 public void setEnclosingType(Type outer) {
duke@1 731 outer_field = outer;
duke@1 732 }
duke@1 733
duke@1 734 public List<Type> allparams() {
duke@1 735 if (allparams_field == null) {
duke@1 736 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
duke@1 737 }
duke@1 738 return allparams_field;
duke@1 739 }
duke@1 740
duke@1 741 public boolean isErroneous() {
duke@1 742 return
duke@1 743 getEnclosingType().isErroneous() ||
duke@1 744 isErroneous(getTypeArguments()) ||
duke@1 745 this != tsym.type && tsym.type.isErroneous();
duke@1 746 }
duke@1 747
duke@1 748 public boolean isParameterized() {
duke@1 749 return allparams().tail != null;
duke@1 750 // optimization, was: allparams().nonEmpty();
duke@1 751 }
duke@1 752
duke@1 753 /** A cache for the rank. */
duke@1 754 int rank_field = -1;
duke@1 755
duke@1 756 /** A class type is raw if it misses some
duke@1 757 * of its type parameter sections.
duke@1 758 * After validation, this is equivalent to:
jjg@1326 759 * {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); }
duke@1 760 */
duke@1 761 public boolean isRaw() {
duke@1 762 return
duke@1 763 this != tsym.type && // necessary, but not sufficient condition
duke@1 764 tsym.type.allparams().nonEmpty() &&
duke@1 765 allparams().isEmpty();
duke@1 766 }
duke@1 767
duke@1 768 public Type map(Mapping f) {
duke@1 769 Type outer = getEnclosingType();
duke@1 770 Type outer1 = f.apply(outer);
duke@1 771 List<Type> typarams = getTypeArguments();
duke@1 772 List<Type> typarams1 = map(typarams, f);
duke@1 773 if (outer1 == outer && typarams1 == typarams) return this;
duke@1 774 else return new ClassType(outer1, typarams1, tsym);
duke@1 775 }
duke@1 776
duke@1 777 public boolean contains(Type elem) {
duke@1 778 return
duke@1 779 elem == this
duke@1 780 || (isParameterized()
mcimadamore@635 781 && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
mcimadamore@635 782 || (isCompound()
mcimadamore@635 783 && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
duke@1 784 }
duke@1 785
duke@1 786 public void complete() {
duke@1 787 if (tsym.completer != null) tsym.complete();
duke@1 788 }
duke@1 789
duke@1 790 public TypeKind getKind() {
duke@1 791 return TypeKind.DECLARED;
duke@1 792 }
duke@1 793
duke@1 794 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 795 return v.visitDeclared(this, p);
duke@1 796 }
duke@1 797 }
duke@1 798
mcimadamore@30 799 public static class ErasedClassType extends ClassType {
mcimadamore@30 800 public ErasedClassType(Type outer, TypeSymbol tsym) {
mcimadamore@30 801 super(outer, List.<Type>nil(), tsym);
mcimadamore@30 802 }
mcimadamore@30 803
mcimadamore@30 804 @Override
mcimadamore@30 805 public boolean hasErasedSupertypes() {
mcimadamore@30 806 return true;
mcimadamore@30 807 }
mcimadamore@30 808 }
mcimadamore@30 809
jjg@988 810 // a clone of a ClassType that knows about the alternatives of a union type.
jjg@988 811 public static class UnionClassType extends ClassType implements UnionType {
jjg@988 812 final List<? extends Type> alternatives_field;
jjg@988 813
jjg@988 814 public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
jjg@988 815 super(ct.outer_field, ct.typarams_field, ct.tsym);
jjg@988 816 allparams_field = ct.allparams_field;
jjg@988 817 supertype_field = ct.supertype_field;
jjg@988 818 interfaces_field = ct.interfaces_field;
jjg@988 819 all_interfaces_field = ct.interfaces_field;
jjg@988 820 alternatives_field = alternatives;
jjg@988 821 }
jjg@988 822
jjg@988 823 public Type getLub() {
jjg@988 824 return tsym.type;
jjg@988 825 }
jjg@988 826
jjg@988 827 public java.util.List<? extends TypeMirror> getAlternatives() {
jjg@988 828 return Collections.unmodifiableList(alternatives_field);
jjg@988 829 }
jjg@988 830
jjg@988 831 @Override
jjg@988 832 public TypeKind getKind() {
jjg@988 833 return TypeKind.UNION;
jjg@988 834 }
jjg@988 835
jjg@988 836 @Override
jjg@988 837 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
jjg@988 838 return v.visitUnion(this, p);
jjg@988 839 }
jjg@988 840 }
jjg@988 841
mcimadamore@1436 842 // a clone of a ClassType that knows about the bounds of an intersection type.
mcimadamore@1436 843 public static class IntersectionClassType extends ClassType implements IntersectionType {
mcimadamore@1436 844
mcimadamore@1436 845 public boolean allInterfaces;
mcimadamore@1436 846
mcimadamore@1436 847 public enum IntersectionKind {
mcimadamore@1436 848 EXPLICIT,
mcimadamore@1436 849 IMPLICT;
mcimadamore@1436 850 }
mcimadamore@1436 851
mcimadamore@1436 852 public IntersectionKind intersectionKind;
mcimadamore@1436 853
mcimadamore@1436 854 public IntersectionClassType(List<Type> bounds, ClassSymbol csym, boolean allInterfaces) {
mcimadamore@1436 855 super(Type.noType, List.<Type>nil(), csym);
mcimadamore@1436 856 this.allInterfaces = allInterfaces;
mcimadamore@1436 857 Assert.check((csym.flags() & COMPOUND) != 0);
mcimadamore@1436 858 supertype_field = bounds.head;
mcimadamore@1436 859 interfaces_field = bounds.tail;
mcimadamore@1436 860 Assert.check(supertype_field.tsym.completer != null ||
mcimadamore@1436 861 !supertype_field.isInterface(), supertype_field);
mcimadamore@1436 862 }
mcimadamore@1436 863
mcimadamore@1436 864 public java.util.List<? extends TypeMirror> getBounds() {
mcimadamore@1436 865 return Collections.unmodifiableList(getComponents());
mcimadamore@1436 866 }
mcimadamore@1436 867
mcimadamore@1436 868 public List<Type> getComponents() {
mcimadamore@1436 869 return interfaces_field.prepend(supertype_field);
mcimadamore@1436 870 }
mcimadamore@1436 871
mcimadamore@1436 872 @Override
mcimadamore@1436 873 public TypeKind getKind() {
mcimadamore@1436 874 return TypeKind.INTERSECTION;
mcimadamore@1436 875 }
mcimadamore@1436 876
mcimadamore@1436 877 @Override
mcimadamore@1436 878 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
mcimadamore@1436 879 return intersectionKind == IntersectionKind.EXPLICIT ?
mcimadamore@1436 880 v.visitIntersection(this, p) :
mcimadamore@1436 881 v.visitDeclared(this, p);
mcimadamore@1436 882 }
mcimadamore@1436 883 }
mcimadamore@1436 884
duke@1 885 public static class ArrayType extends Type
duke@1 886 implements javax.lang.model.type.ArrayType {
duke@1 887
duke@1 888 public Type elemtype;
duke@1 889
duke@1 890 public ArrayType(Type elemtype, TypeSymbol arrayClass) {
duke@1 891 super(ARRAY, arrayClass);
duke@1 892 this.elemtype = elemtype;
duke@1 893 }
duke@1 894
duke@1 895 @Override
duke@1 896 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 897 return v.visitArrayType(this, s);
duke@1 898 }
duke@1 899
duke@1 900 public String toString() {
duke@1 901 return elemtype + "[]";
duke@1 902 }
duke@1 903
duke@1 904 public boolean equals(Object obj) {
duke@1 905 return
duke@1 906 this == obj ||
duke@1 907 (obj instanceof ArrayType &&
duke@1 908 this.elemtype.equals(((ArrayType)obj).elemtype));
duke@1 909 }
duke@1 910
duke@1 911 public int hashCode() {
jjg@1374 912 return (ARRAY.ordinal() << 5) + elemtype.hashCode();
duke@1 913 }
duke@1 914
mcimadamore@795 915 public boolean isVarargs() {
mcimadamore@795 916 return false;
mcimadamore@795 917 }
mcimadamore@795 918
duke@1 919 public List<Type> allparams() { return elemtype.allparams(); }
duke@1 920
duke@1 921 public boolean isErroneous() {
duke@1 922 return elemtype.isErroneous();
duke@1 923 }
duke@1 924
duke@1 925 public boolean isParameterized() {
duke@1 926 return elemtype.isParameterized();
duke@1 927 }
duke@1 928
duke@1 929 public boolean isRaw() {
duke@1 930 return elemtype.isRaw();
duke@1 931 }
duke@1 932
mcimadamore@795 933 public ArrayType makeVarargs() {
mcimadamore@795 934 return new ArrayType(elemtype, tsym) {
mcimadamore@795 935 @Override
mcimadamore@795 936 public boolean isVarargs() {
mcimadamore@795 937 return true;
mcimadamore@795 938 }
mcimadamore@795 939 };
mcimadamore@795 940 }
mcimadamore@795 941
duke@1 942 public Type map(Mapping f) {
duke@1 943 Type elemtype1 = f.apply(elemtype);
duke@1 944 if (elemtype1 == elemtype) return this;
duke@1 945 else return new ArrayType(elemtype1, tsym);
duke@1 946 }
duke@1 947
duke@1 948 public boolean contains(Type elem) {
duke@1 949 return elem == this || elemtype.contains(elem);
duke@1 950 }
duke@1 951
duke@1 952 public void complete() {
duke@1 953 elemtype.complete();
duke@1 954 }
duke@1 955
duke@1 956 public Type getComponentType() {
duke@1 957 return elemtype;
duke@1 958 }
duke@1 959
duke@1 960 public TypeKind getKind() {
duke@1 961 return TypeKind.ARRAY;
duke@1 962 }
duke@1 963
duke@1 964 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 965 return v.visitArray(this, p);
duke@1 966 }
duke@1 967 }
duke@1 968
dlsmith@880 969 public static class MethodType extends Type implements ExecutableType {
duke@1 970
duke@1 971 public List<Type> argtypes;
duke@1 972 public Type restype;
duke@1 973 public List<Type> thrown;
duke@1 974
duke@1 975 public MethodType(List<Type> argtypes,
duke@1 976 Type restype,
duke@1 977 List<Type> thrown,
duke@1 978 TypeSymbol methodClass) {
duke@1 979 super(METHOD, methodClass);
duke@1 980 this.argtypes = argtypes;
duke@1 981 this.restype = restype;
duke@1 982 this.thrown = thrown;
duke@1 983 }
duke@1 984
duke@1 985 @Override
duke@1 986 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 987 return v.visitMethodType(this, s);
duke@1 988 }
duke@1 989
duke@1 990 /** The Java source which this type represents.
duke@1 991 *
duke@1 992 * XXX 06/09/99 iris This isn't correct Java syntax, but it probably
duke@1 993 * should be.
duke@1 994 */
duke@1 995 public String toString() {
duke@1 996 return "(" + argtypes + ")" + restype;
duke@1 997 }
duke@1 998
duke@1 999 public boolean equals(Object obj) {
duke@1 1000 if (this == obj)
duke@1 1001 return true;
duke@1 1002 if (!(obj instanceof MethodType))
duke@1 1003 return false;
duke@1 1004 MethodType m = (MethodType)obj;
duke@1 1005 List<Type> args1 = argtypes;
duke@1 1006 List<Type> args2 = m.argtypes;
duke@1 1007 while (!args1.isEmpty() && !args2.isEmpty()) {
duke@1 1008 if (!args1.head.equals(args2.head))
duke@1 1009 return false;
duke@1 1010 args1 = args1.tail;
duke@1 1011 args2 = args2.tail;
duke@1 1012 }
duke@1 1013 if (!args1.isEmpty() || !args2.isEmpty())
duke@1 1014 return false;
duke@1 1015 return restype.equals(m.restype);
duke@1 1016 }
duke@1 1017
duke@1 1018 public int hashCode() {
jjg@1374 1019 int h = METHOD.ordinal();
duke@1 1020 for (List<Type> thisargs = this.argtypes;
duke@1 1021 thisargs.tail != null; /*inlined: thisargs.nonEmpty()*/
duke@1 1022 thisargs = thisargs.tail)
duke@1 1023 h = (h << 5) + thisargs.head.hashCode();
duke@1 1024 return (h << 5) + this.restype.hashCode();
duke@1 1025 }
duke@1 1026
duke@1 1027 public List<Type> getParameterTypes() { return argtypes; }
duke@1 1028 public Type getReturnType() { return restype; }
duke@1 1029 public List<Type> getThrownTypes() { return thrown; }
duke@1 1030
duke@1 1031 public boolean isErroneous() {
duke@1 1032 return
duke@1 1033 isErroneous(argtypes) ||
duke@1 1034 restype != null && restype.isErroneous();
duke@1 1035 }
duke@1 1036
duke@1 1037 public Type map(Mapping f) {
duke@1 1038 List<Type> argtypes1 = map(argtypes, f);
duke@1 1039 Type restype1 = f.apply(restype);
duke@1 1040 List<Type> thrown1 = map(thrown, f);
duke@1 1041 if (argtypes1 == argtypes &&
duke@1 1042 restype1 == restype &&
duke@1 1043 thrown1 == thrown) return this;
duke@1 1044 else return new MethodType(argtypes1, restype1, thrown1, tsym);
duke@1 1045 }
duke@1 1046
duke@1 1047 public boolean contains(Type elem) {
duke@1 1048 return elem == this || contains(argtypes, elem) || restype.contains(elem);
duke@1 1049 }
duke@1 1050
duke@1 1051 public MethodType asMethodType() { return this; }
duke@1 1052
duke@1 1053 public void complete() {
duke@1 1054 for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
duke@1 1055 l.head.complete();
duke@1 1056 restype.complete();
duke@1 1057 for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
duke@1 1058 l.head.complete();
duke@1 1059 }
duke@1 1060
duke@1 1061 public List<TypeVar> getTypeVariables() {
duke@1 1062 return List.nil();
duke@1 1063 }
duke@1 1064
duke@1 1065 public TypeSymbol asElement() {
duke@1 1066 return null;
duke@1 1067 }
duke@1 1068
duke@1 1069 public TypeKind getKind() {
duke@1 1070 return TypeKind.EXECUTABLE;
duke@1 1071 }
duke@1 1072
duke@1 1073 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1074 return v.visitExecutable(this, p);
duke@1 1075 }
duke@1 1076 }
duke@1 1077
duke@1 1078 public static class PackageType extends Type implements NoType {
duke@1 1079
duke@1 1080 PackageType(TypeSymbol tsym) {
duke@1 1081 super(PACKAGE, tsym);
duke@1 1082 }
duke@1 1083
duke@1 1084 @Override
duke@1 1085 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1086 return v.visitPackageType(this, s);
duke@1 1087 }
duke@1 1088
duke@1 1089 public String toString() {
duke@1 1090 return tsym.getQualifiedName().toString();
duke@1 1091 }
duke@1 1092
duke@1 1093 public TypeKind getKind() {
duke@1 1094 return TypeKind.PACKAGE;
duke@1 1095 }
duke@1 1096
duke@1 1097 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1098 return v.visitNoType(this, p);
duke@1 1099 }
duke@1 1100 }
duke@1 1101
duke@1 1102 public static class TypeVar extends Type implements TypeVariable {
duke@1 1103
jjg@789 1104 /** The upper bound of this type variable; set from outside.
duke@1 1105 * Must be nonempty once it is set.
duke@1 1106 * For a bound, `bound' is the bound type itself.
duke@1 1107 * Multiple bounds are expressed as a single class type which has the
duke@1 1108 * individual bounds as superclass, respectively interfaces.
duke@1 1109 * The class type then has as `tsym' a compiler generated class `c',
duke@1 1110 * which has a flag COMPOUND and whose owner is the type variable
duke@1 1111 * itself. Furthermore, the erasure_field of the class
duke@1 1112 * points to the first class or interface bound.
duke@1 1113 */
duke@1 1114 public Type bound = null;
jjg@789 1115
jjg@789 1116 /** The lower bound of this type variable.
jjg@789 1117 * TypeVars don't normally have a lower bound, so it is normally set
jjg@789 1118 * to syms.botType.
jjg@789 1119 * Subtypes, such as CapturedType, may provide a different value.
jjg@789 1120 */
duke@1 1121 public Type lower;
duke@1 1122
duke@1 1123 public TypeVar(Name name, Symbol owner, Type lower) {
duke@1 1124 super(TYPEVAR, null);
duke@1 1125 tsym = new TypeSymbol(0, name, this, owner);
duke@1 1126 this.lower = lower;
duke@1 1127 }
duke@1 1128
duke@1 1129 public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
duke@1 1130 super(TYPEVAR, tsym);
duke@1 1131 this.bound = bound;
duke@1 1132 this.lower = lower;
duke@1 1133 }
duke@1 1134
duke@1 1135 @Override
duke@1 1136 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1137 return v.visitTypeVar(this, s);
duke@1 1138 }
duke@1 1139
jjg@789 1140 @Override
duke@1 1141 public Type getUpperBound() { return bound; }
duke@1 1142
duke@1 1143 int rank_field = -1;
duke@1 1144
jjg@789 1145 @Override
duke@1 1146 public Type getLowerBound() {
duke@1 1147 return lower;
duke@1 1148 }
duke@1 1149
duke@1 1150 public TypeKind getKind() {
duke@1 1151 return TypeKind.TYPEVAR;
duke@1 1152 }
duke@1 1153
mcimadamore@79 1154 public boolean isCaptured() {
mcimadamore@79 1155 return false;
mcimadamore@79 1156 }
mcimadamore@79 1157
duke@1 1158 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1159 return v.visitTypeVariable(this, p);
duke@1 1160 }
duke@1 1161 }
duke@1 1162
duke@1 1163 /** A captured type variable comes from wildcards which can have
duke@1 1164 * both upper and lower bound. CapturedType extends TypeVar with
duke@1 1165 * a lower bound.
duke@1 1166 */
duke@1 1167 public static class CapturedType extends TypeVar {
duke@1 1168
duke@1 1169 public WildcardType wildcard;
duke@1 1170
duke@1 1171 public CapturedType(Name name,
duke@1 1172 Symbol owner,
duke@1 1173 Type upper,
duke@1 1174 Type lower,
duke@1 1175 WildcardType wildcard) {
duke@1 1176 super(name, owner, lower);
jjg@816 1177 this.lower = Assert.checkNonNull(lower);
duke@1 1178 this.bound = upper;
duke@1 1179 this.wildcard = wildcard;
duke@1 1180 }
duke@1 1181
duke@1 1182 @Override
duke@1 1183 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1184 return v.visitCapturedType(this, s);
duke@1 1185 }
duke@1 1186
duke@1 1187 @Override
mcimadamore@79 1188 public boolean isCaptured() {
mcimadamore@79 1189 return true;
mcimadamore@79 1190 }
mcimadamore@79 1191
mcimadamore@79 1192 @Override
duke@1 1193 public String toString() {
duke@1 1194 return "capture#"
mcimadamore@288 1195 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
duke@1 1196 + " of "
duke@1 1197 + wildcard;
duke@1 1198 }
duke@1 1199 }
duke@1 1200
duke@1 1201 public static abstract class DelegatedType extends Type {
duke@1 1202 public Type qtype;
jjg@1374 1203 public DelegatedType(TypeTag tag, Type qtype) {
duke@1 1204 super(tag, qtype.tsym);
duke@1 1205 this.qtype = qtype;
duke@1 1206 }
duke@1 1207 public String toString() { return qtype.toString(); }
duke@1 1208 public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
duke@1 1209 public Type getEnclosingType() { return qtype.getEnclosingType(); }
duke@1 1210 public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
duke@1 1211 public Type getReturnType() { return qtype.getReturnType(); }
duke@1 1212 public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
duke@1 1213 public List<Type> allparams() { return qtype.allparams(); }
duke@1 1214 public Type getUpperBound() { return qtype.getUpperBound(); }
duke@1 1215 public boolean isErroneous() { return qtype.isErroneous(); }
duke@1 1216 }
duke@1 1217
mcimadamore@1268 1218 /**
mcimadamore@1268 1219 * The type of a generic method type. It consists of a method type and
mcimadamore@1268 1220 * a list of method type-parameters that are used within the method
mcimadamore@1268 1221 * type.
mcimadamore@1268 1222 */
dlsmith@880 1223 public static class ForAll extends DelegatedType implements ExecutableType {
duke@1 1224 public List<Type> tvars;
duke@1 1225
duke@1 1226 public ForAll(List<Type> tvars, Type qtype) {
mcimadamore@1268 1227 super(FORALL, (MethodType)qtype);
duke@1 1228 this.tvars = tvars;
duke@1 1229 }
duke@1 1230
duke@1 1231 @Override
duke@1 1232 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1233 return v.visitForAll(this, s);
duke@1 1234 }
duke@1 1235
duke@1 1236 public String toString() {
duke@1 1237 return "<" + tvars + ">" + qtype;
duke@1 1238 }
duke@1 1239
duke@1 1240 public List<Type> getTypeArguments() { return tvars; }
duke@1 1241
duke@1 1242 public boolean isErroneous() {
duke@1 1243 return qtype.isErroneous();
duke@1 1244 }
duke@1 1245
duke@1 1246 public Type map(Mapping f) {
duke@1 1247 return f.apply(qtype);
duke@1 1248 }
duke@1 1249
duke@1 1250 public boolean contains(Type elem) {
duke@1 1251 return qtype.contains(elem);
duke@1 1252 }
duke@1 1253
duke@1 1254 public MethodType asMethodType() {
mcimadamore@1268 1255 return (MethodType)qtype;
duke@1 1256 }
duke@1 1257
duke@1 1258 public void complete() {
duke@1 1259 for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
duke@1 1260 ((TypeVar)l.head).bound.complete();
duke@1 1261 }
duke@1 1262 qtype.complete();
duke@1 1263 }
duke@1 1264
duke@1 1265 public List<TypeVar> getTypeVariables() {
duke@1 1266 return List.convert(TypeVar.class, getTypeArguments());
duke@1 1267 }
duke@1 1268
duke@1 1269 public TypeKind getKind() {
duke@1 1270 return TypeKind.EXECUTABLE;
duke@1 1271 }
duke@1 1272
duke@1 1273 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1274 return v.visitExecutable(this, p);
duke@1 1275 }
duke@1 1276 }
duke@1 1277
mcimadamore@1338 1278 /** A class for inference variables, for use during method/diamond type
mcimadamore@1338 1279 * inference. An inference variable has upper/lower bounds and a set
mcimadamore@1338 1280 * of equality constraints. Such bounds are set during subtyping, type-containment,
mcimadamore@1338 1281 * type-equality checks, when the types being tested contain inference variables.
mcimadamore@1338 1282 * A change listener can be attached to an inference variable, to receive notifications
mcimadamore@1338 1283 * whenever the bounds of an inference variable change.
duke@1 1284 */
duke@1 1285 public static class UndetVar extends DelegatedType {
mcimadamore@1338 1286
mcimadamore@1338 1287 /** Inference variable change listener. The listener method is called
mcimadamore@1338 1288 * whenever a change to the inference variable's bounds occurs
mcimadamore@1338 1289 */
mcimadamore@1338 1290 public interface UndetVarListener {
mcimadamore@1338 1291 /** called when some inference variable bounds (of given kinds ibs) change */
mcimadamore@1338 1292 void varChanged(UndetVar uv, Set<InferenceBound> ibs);
mcimadamore@1338 1293 }
mcimadamore@1338 1294
mcimadamore@1338 1295 /**
mcimadamore@1338 1296 * Inference variable bound kinds
mcimadamore@1338 1297 */
mcimadamore@1338 1298 public enum InferenceBound {
mcimadamore@1338 1299 /** upper bounds */
mcimadamore@1338 1300 UPPER,
mcimadamore@1338 1301 /** lower bounds */
mcimadamore@1338 1302 LOWER,
mcimadamore@1338 1303 /** equality constraints */
mcimadamore@1338 1304 EQ;
mcimadamore@1338 1305 }
mcimadamore@1338 1306
mcimadamore@1338 1307 /** inference variable bounds */
mcimadamore@1338 1308 private Map<InferenceBound, List<Type>> bounds;
mcimadamore@1338 1309
mcimadamore@1338 1310 /** inference variable's inferred type (set from Infer.java) */
duke@1 1311 public Type inst = null;
duke@1 1312
mcimadamore@1338 1313 /** inference variable's change listener */
mcimadamore@1338 1314 public UndetVarListener listener = null;
mcimadamore@1338 1315
duke@1 1316 @Override
duke@1 1317 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1318 return v.visitUndetVar(this, s);
duke@1 1319 }
duke@1 1320
mcimadamore@1338 1321 public UndetVar(TypeVar origin, Types types) {
mcimadamore@1348 1322 this(origin, types, true);
mcimadamore@1348 1323 }
mcimadamore@1348 1324
mcimadamore@1348 1325 public UndetVar(TypeVar origin, Types types, boolean includeBounds) {
duke@1 1326 super(UNDETVAR, origin);
mcimadamore@1338 1327 bounds = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
mcimadamore@1348 1328 bounds.put(InferenceBound.UPPER, includeBounds ? types.getBounds(origin) : List.<Type>nil());
mcimadamore@1338 1329 bounds.put(InferenceBound.LOWER, List.<Type>nil());
mcimadamore@1338 1330 bounds.put(InferenceBound.EQ, List.<Type>nil());
duke@1 1331 }
duke@1 1332
duke@1 1333 public String toString() {
duke@1 1334 if (inst != null) return inst.toString();
duke@1 1335 else return qtype + "?";
duke@1 1336 }
duke@1 1337
duke@1 1338 public Type baseType() {
duke@1 1339 if (inst != null) return inst.baseType();
duke@1 1340 else return this;
duke@1 1341 }
mcimadamore@1338 1342
mcimadamore@1338 1343 /** get all bounds of a given kind */
mcimadamore@1338 1344 public List<Type> getBounds(InferenceBound ib) {
mcimadamore@1338 1345 return bounds.get(ib);
mcimadamore@1338 1346 }
mcimadamore@1338 1347
mcimadamore@1338 1348 /** add a bound of a given kind - this might trigger listener notification */
mcimadamore@1338 1349 public void addBound(InferenceBound ib, Type bound, Types types) {
mcimadamore@1338 1350 List<Type> prevBounds = bounds.get(ib);
mcimadamore@1338 1351 for (Type b : prevBounds) {
mcimadamore@1338 1352 if (types.isSameType(b, bound)) {
mcimadamore@1338 1353 return;
mcimadamore@1338 1354 }
mcimadamore@1338 1355 }
mcimadamore@1338 1356 bounds.put(ib, prevBounds.prepend(bound));
mcimadamore@1338 1357 notifyChange(EnumSet.of(ib));
mcimadamore@1338 1358 }
mcimadamore@1338 1359
mcimadamore@1338 1360 /** replace types in all bounds - this might trigger listener notification */
mcimadamore@1338 1361 public void substBounds(List<Type> from, List<Type> to, Types types) {
mcimadamore@1338 1362 EnumSet<InferenceBound> changed = EnumSet.noneOf(InferenceBound.class);
mcimadamore@1338 1363 Map<InferenceBound, List<Type>> bounds2 = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
mcimadamore@1338 1364 for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) {
mcimadamore@1338 1365 InferenceBound ib = _entry.getKey();
mcimadamore@1338 1366 List<Type> prevBounds = _entry.getValue();
mcimadamore@1338 1367 List<Type> newBounds = types.subst(prevBounds, from, to);
mcimadamore@1338 1368 bounds2.put(ib, newBounds);
mcimadamore@1338 1369 if (prevBounds != newBounds) {
mcimadamore@1338 1370 changed.add(ib);
mcimadamore@1338 1371 }
mcimadamore@1338 1372 }
mcimadamore@1338 1373 if (!changed.isEmpty()) {
mcimadamore@1338 1374 bounds = bounds2;
mcimadamore@1338 1375 notifyChange(changed);
mcimadamore@1338 1376 }
mcimadamore@1338 1377 }
mcimadamore@1338 1378
mcimadamore@1338 1379 private void notifyChange(EnumSet<InferenceBound> ibs) {
mcimadamore@1338 1380 if (listener != null) {
mcimadamore@1338 1381 listener.varChanged(this, ibs);
mcimadamore@1338 1382 }
mcimadamore@1338 1383 }
duke@1 1384 }
duke@1 1385
duke@1 1386 /** Represents VOID or NONE.
duke@1 1387 */
duke@1 1388 static class JCNoType extends Type implements NoType {
jjg@1374 1389 public JCNoType(TypeTag tag) {
duke@1 1390 super(tag, null);
duke@1 1391 }
duke@1 1392
duke@1 1393 @Override
duke@1 1394 public TypeKind getKind() {
duke@1 1395 switch (tag) {
duke@1 1396 case VOID: return TypeKind.VOID;
duke@1 1397 case NONE: return TypeKind.NONE;
duke@1 1398 default:
duke@1 1399 throw new AssertionError("Unexpected tag: " + tag);
duke@1 1400 }
duke@1 1401 }
duke@1 1402
duke@1 1403 @Override
duke@1 1404 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1405 return v.visitNoType(this, p);
duke@1 1406 }
duke@1 1407 }
duke@1 1408
duke@1 1409 static class BottomType extends Type implements NullType {
duke@1 1410 public BottomType() {
jjg@1374 1411 super(BOT, null);
duke@1 1412 }
duke@1 1413
duke@1 1414 @Override
duke@1 1415 public TypeKind getKind() {
duke@1 1416 return TypeKind.NULL;
duke@1 1417 }
duke@1 1418
duke@1 1419 @Override
duke@1 1420 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1421 return v.visitNull(this, p);
duke@1 1422 }
duke@1 1423
duke@1 1424 @Override
duke@1 1425 public Type constType(Object value) {
duke@1 1426 return this;
duke@1 1427 }
duke@1 1428
duke@1 1429 @Override
duke@1 1430 public String stringValue() {
duke@1 1431 return "null";
duke@1 1432 }
duke@1 1433 }
duke@1 1434
duke@1 1435 public static class ErrorType extends ClassType
duke@1 1436 implements javax.lang.model.type.ErrorType {
duke@1 1437
jjg@110 1438 private Type originalType = null;
jjg@110 1439
jjg@110 1440 public ErrorType(Type originalType, TypeSymbol tsym) {
duke@1 1441 super(noType, List.<Type>nil(), null);
duke@1 1442 tag = ERROR;
jjg@110 1443 this.tsym = tsym;
jjg@110 1444 this.originalType = (originalType == null ? noType : originalType);
duke@1 1445 }
duke@1 1446
jjg@110 1447 public ErrorType(ClassSymbol c, Type originalType) {
jjg@110 1448 this(originalType, c);
duke@1 1449 c.type = this;
duke@1 1450 c.kind = ERR;
duke@1 1451 c.members_field = new Scope.ErrorScope(c);
duke@1 1452 }
duke@1 1453
jjg@110 1454 public ErrorType(Name name, TypeSymbol container, Type originalType) {
jjg@110 1455 this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
duke@1 1456 }
duke@1 1457
duke@1 1458 @Override
duke@1 1459 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1460 return v.visitErrorType(this, s);
duke@1 1461 }
duke@1 1462
duke@1 1463 public Type constType(Object constValue) { return this; }
duke@1 1464 public Type getEnclosingType() { return this; }
duke@1 1465 public Type getReturnType() { return this; }
duke@1 1466 public Type asSub(Symbol sym) { return this; }
duke@1 1467 public Type map(Mapping f) { return this; }
duke@1 1468
duke@1 1469 public boolean isGenType(Type t) { return true; }
duke@1 1470 public boolean isErroneous() { return true; }
duke@1 1471 public boolean isCompound() { return false; }
duke@1 1472 public boolean isInterface() { return false; }
duke@1 1473
duke@1 1474 public List<Type> allparams() { return List.nil(); }
duke@1 1475 public List<Type> getTypeArguments() { return List.nil(); }
duke@1 1476
duke@1 1477 public TypeKind getKind() {
duke@1 1478 return TypeKind.ERROR;
duke@1 1479 }
duke@1 1480
jjg@110 1481 public Type getOriginalType() {
jjg@110 1482 return originalType;
jjg@110 1483 }
jjg@110 1484
duke@1 1485 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1486 return v.visitError(this, p);
duke@1 1487 }
duke@1 1488 }
duke@1 1489
duke@1 1490 /**
duke@1 1491 * A visitor for types. A visitor is used to implement operations
duke@1 1492 * (or relations) on types. Most common operations on types are
duke@1 1493 * binary relations and this interface is designed for binary
duke@1 1494 * relations, that is, operations on the form
duke@1 1495 * Type&nbsp;&times;&nbsp;S&nbsp;&rarr;&nbsp;R.
duke@1 1496 * <!-- In plain text: Type x S -> R -->
duke@1 1497 *
duke@1 1498 * @param <R> the return type of the operation implemented by this
duke@1 1499 * visitor; use Void if no return type is needed.
duke@1 1500 * @param <S> the type of the second argument (the first being the
duke@1 1501 * type itself) of the operation implemented by this visitor; use
duke@1 1502 * Void if a second argument is not needed.
duke@1 1503 */
duke@1 1504 public interface Visitor<R,S> {
duke@1 1505 R visitClassType(ClassType t, S s);
duke@1 1506 R visitWildcardType(WildcardType t, S s);
duke@1 1507 R visitArrayType(ArrayType t, S s);
duke@1 1508 R visitMethodType(MethodType t, S s);
duke@1 1509 R visitPackageType(PackageType t, S s);
duke@1 1510 R visitTypeVar(TypeVar t, S s);
duke@1 1511 R visitCapturedType(CapturedType t, S s);
duke@1 1512 R visitForAll(ForAll t, S s);
duke@1 1513 R visitUndetVar(UndetVar t, S s);
duke@1 1514 R visitErrorType(ErrorType t, S s);
duke@1 1515 R visitType(Type t, S s);
duke@1 1516 }
duke@1 1517 }

mercurial