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

Fri, 28 Sep 2012 16:56:53 +0100

author
mcimadamore
date
Fri, 28 Sep 2012 16:56:53 +0100
changeset 1342
1a65d6565b45
parent 1338
ad2ca2a4ab5e
child 1347
1408af4cd8b0
permissions
-rw-r--r--

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

mercurial