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

Wed, 02 Mar 2011 21:13:55 -0800

author
jjg
date
Wed, 02 Mar 2011 21:13:55 -0800
changeset 904
4baab658f357
parent 880
0c24826853b2
child 988
7ae6c0fd479b
permissions
-rw-r--r--

6639645: Modeling type implementing missing interfaces
Reviewed-by: darcy, mcimadamore

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

mercurial