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

Tue, 25 Sep 2012 11:56:46 +0100

author
mcimadamore
date
Tue, 25 Sep 2012 11:56:46 +0100
changeset 1338
ad2ca2a4ab5e
parent 1326
30c36e23f154
child 1342
1a65d6565b45
permissions
-rw-r--r--

7177306: Regression: unchecked method call does not erase return type
Summary: Spurious extra call to Attr.checkMethod when method call is unchecked
Reviewed-by: jjg, dlsmith

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

mercurial