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

Fri, 05 Oct 2012 14:35:24 +0100

author
mcimadamore
date
Fri, 05 Oct 2012 14:35:24 +0100
changeset 1348
573ceb23beeb
parent 1347
1408af4cd8b0
child 1357
c75be5bc5283
permissions
-rw-r--r--

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

mercurial