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

Thu, 28 Apr 2011 15:05:36 -0700

author
jjg
date
Thu, 28 Apr 2011 15:05:36 -0700
changeset 988
7ae6c0fd479b
parent 904
4baab658f357
child 1251
6f0ed5a89c25
permissions
-rw-r--r--

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

mercurial