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

Tue, 09 Oct 2012 19:10:00 -0700

author
jjg
date
Tue, 09 Oct 2012 19:10:00 -0700
changeset 1357
c75be5bc5283
parent 1348
573ceb23beeb
child 1374
c002fdee76fd
permissions
-rw-r--r--

8000663: clean up langtools imports
Reviewed-by: darcy

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

mercurial