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

Thu, 04 Aug 2016 23:36:47 -0700

author
asaha
date
Thu, 04 Aug 2016 23:36:47 -0700
changeset 3270
8a30511b2ea4
parent 2904
14891e981af0
child 3295
859dc787b52b
permissions
-rw-r--r--

8162511: 8u111 L10n resource file updates
Summary: 8u111 L10n resource file updates
Reviewed-by: coffeys
Contributed-by: li.jiang@oracle.com

duke@1 1 /*
vromero@2302 2 * Copyright (c) 1999, 2014, 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@1645 28 import java.lang.annotation.Annotation;
jjg@988 29 import java.util.Collections;
mcimadamore@1338 30 import java.util.EnumMap;
mcimadamore@1338 31 import java.util.EnumSet;
mcimadamore@1338 32 import java.util.Map;
mcimadamore@1338 33 import java.util.Set;
mcimadamore@1342 34
duke@1 35 import javax.lang.model.type.*;
duke@1 36
jjg@1357 37 import com.sun.tools.javac.code.Symbol.*;
jjg@1357 38 import com.sun.tools.javac.util.*;
mcimadamore@1342 39 import static com.sun.tools.javac.code.BoundKind.*;
duke@1 40 import static com.sun.tools.javac.code.Flags.*;
duke@1 41 import static com.sun.tools.javac.code.Kinds.*;
jjg@1374 42 import static com.sun.tools.javac.code.TypeTag.*;
duke@1 43
duke@1 44 /** This class represents Java types. The class itself defines the behavior of
duke@1 45 * the following types:
duke@1 46 * <pre>
duke@1 47 * base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
duke@1 48 * type `void' (tag: VOID),
duke@1 49 * the bottom type (tag: BOT),
duke@1 50 * the missing type (tag: NONE).
duke@1 51 * </pre>
duke@1 52 * <p>The behavior of the following types is defined in subclasses, which are
duke@1 53 * all static inner classes of this class:
duke@1 54 * <pre>
duke@1 55 * class types (tag: CLASS, class: ClassType),
duke@1 56 * array types (tag: ARRAY, class: ArrayType),
duke@1 57 * method types (tag: METHOD, class: MethodType),
duke@1 58 * package types (tag: PACKAGE, class: PackageType),
duke@1 59 * type variables (tag: TYPEVAR, class: TypeVar),
duke@1 60 * type arguments (tag: WILDCARD, class: WildcardType),
mcimadamore@1268 61 * generic method types (tag: FORALL, class: ForAll),
duke@1 62 * the error type (tag: ERROR, class: ErrorType).
duke@1 63 * </pre>
duke@1 64 *
jjg@581 65 * <p><b>This is NOT part of any supported API.
jjg@581 66 * If you write code that depends on this, you do so at your own risk.
duke@1 67 * This code and its internal interfaces are subject to change or
duke@1 68 * deletion without notice.</b>
duke@1 69 *
jjg@1374 70 * @see TypeTag
duke@1 71 */
alundblad@2100 72 public abstract class Type extends AnnoConstruct implements TypeMirror {
duke@1 73
duke@1 74 /** Constant type: no type at all. */
vromero@2412 75 public static final JCNoType noType = new JCNoType() {
vromero@2412 76 @Override
vromero@2412 77 public String toString() {
vromero@2412 78 return "none";
vromero@2412 79 }
vromero@2412 80 };
duke@1 81
mcimadamore@1347 82 /** Constant type: special type to be used during recovery of deferred expressions. */
vromero@2412 83 public static final JCNoType recoveryType = new JCNoType(){
vromero@2412 84 @Override
vromero@2412 85 public String toString() {
vromero@2412 86 return "recovery";
vromero@2412 87 }
vromero@2412 88 };
mcimadamore@1347 89
mcimadamore@1889 90 /** Constant type: special type to be used for marking stuck trees. */
vromero@2412 91 public static final JCNoType stuckType = new JCNoType() {
vromero@2412 92 @Override
vromero@2412 93 public String toString() {
vromero@2412 94 return "stuck";
vromero@2412 95 }
vromero@2412 96 };
mcimadamore@1889 97
duke@1 98 /** If this switch is turned on, the names of type variables
duke@1 99 * and anonymous classes are printed with hashcodes appended.
duke@1 100 */
duke@1 101 public static boolean moreInfo = false;
duke@1 102
jjg@1521 103 /** The defining class / interface / package / type variable.
duke@1 104 */
duke@1 105 public TypeSymbol tsym;
duke@1 106
duke@1 107 /**
jjg@1374 108 * Checks if the current type tag is equal to the given tag.
jjg@1374 109 * @return true if tag is equal to the current type tag.
jjg@1374 110 */
jjg@1374 111 public boolean hasTag(TypeTag tag) {
vromero@1853 112 return tag == getTag();
jjg@1374 113 }
jjg@1374 114
jjg@1374 115 /**
jjg@1374 116 * Returns the current type tag.
jjg@1374 117 * @return the value of the current type tag.
jjg@1374 118 */
vromero@1853 119 public abstract TypeTag getTag();
jjg@1374 120
jjg@1374 121 public boolean isNumeric() {
vromero@1853 122 return false;
jjg@1374 123 }
jjg@1374 124
jjg@1374 125 public boolean isPrimitive() {
vromero@1853 126 return false;
jjg@1374 127 }
jjg@1374 128
jjg@1374 129 public boolean isPrimitiveOrVoid() {
vromero@1853 130 return false;
jjg@1374 131 }
jjg@1374 132
jjg@1374 133 public boolean isReference() {
vromero@1853 134 return false;
jjg@1374 135 }
jjg@1374 136
jjg@1374 137 public boolean isNullOrReference() {
vromero@1853 138 return false;
jjg@1374 139 }
jjg@1374 140
jjg@1374 141 public boolean isPartial() {
vromero@1853 142 return false;
jjg@1374 143 }
jjg@1374 144
jjg@1374 145 /**
duke@1 146 * The constant value of this type, null if this type does not
duke@1 147 * have a constant value attribute. Only primitive types and
duke@1 148 * strings (ClassType) can have a constant value attribute.
duke@1 149 * @return the constant value attribute of this type
duke@1 150 */
duke@1 151 public Object constValue() {
duke@1 152 return null;
duke@1 153 }
duke@1 154
vromero@1853 155 /** Is this a constant type whose value is false?
vromero@1853 156 */
vromero@1853 157 public boolean isFalse() {
vromero@1853 158 return false;
vromero@1853 159 }
vromero@1853 160
vromero@1853 161 /** Is this a constant type whose value is true?
vromero@1853 162 */
vromero@1853 163 public boolean isTrue() {
vromero@1853 164 return false;
vromero@1853 165 }
vromero@1853 166
jjg@904 167 /**
jjg@904 168 * Get the representation of this type used for modelling purposes.
jjg@904 169 * By default, this is itself. For ErrorType, a different value
jjg@1521 170 * may be provided.
jjg@904 171 */
jjg@904 172 public Type getModelType() {
jjg@904 173 return this;
jjg@904 174 }
jjg@904 175
jjg@904 176 public static List<Type> getModelTypes(List<Type> ts) {
vromero@1853 177 ListBuffer<Type> lb = new ListBuffer<>();
jjg@904 178 for (Type t: ts)
jjg@904 179 lb.append(t.getModelType());
jjg@904 180 return lb.toList();
jjg@904 181 }
jjg@904 182
jlahoda@2108 183 /**For ErrorType, returns the original type, otherwise returns the type itself.
jlahoda@2108 184 */
jlahoda@2108 185 public Type getOriginalType() {
jlahoda@2108 186 return this;
jlahoda@2108 187 }
jlahoda@2108 188
duke@1 189 public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
duke@1 190
duke@1 191 /** Define a type given its tag and type symbol
duke@1 192 */
vromero@1853 193 public Type(TypeSymbol tsym) {
duke@1 194 this.tsym = tsym;
duke@1 195 }
duke@1 196
duke@1 197 /** An abstract class for mappings from types to types
duke@1 198 */
duke@1 199 public static abstract class Mapping {
duke@1 200 private String name;
duke@1 201 public Mapping(String name) {
duke@1 202 this.name = name;
duke@1 203 }
duke@1 204 public abstract Type apply(Type t);
duke@1 205 public String toString() {
duke@1 206 return name;
duke@1 207 }
duke@1 208 }
duke@1 209
duke@1 210 /** map a type function over all immediate descendants of this type
duke@1 211 */
duke@1 212 public Type map(Mapping f) {
duke@1 213 return this;
duke@1 214 }
duke@1 215
duke@1 216 /** map a type function over a list of types
duke@1 217 */
duke@1 218 public static List<Type> map(List<Type> ts, Mapping f) {
duke@1 219 if (ts.nonEmpty()) {
duke@1 220 List<Type> tail1 = map(ts.tail, f);
duke@1 221 Type t = f.apply(ts.head);
duke@1 222 if (tail1 != ts.tail || t != ts.head)
duke@1 223 return tail1.prepend(t);
duke@1 224 }
duke@1 225 return ts;
duke@1 226 }
duke@1 227
duke@1 228 /** Define a constant type, of the same kind as this type
duke@1 229 * and with given constant value
duke@1 230 */
duke@1 231 public Type constType(Object constValue) {
vromero@1853 232 throw new AssertionError();
duke@1 233 }
duke@1 234
duke@1 235 /**
duke@1 236 * If this is a constant type, return its underlying type.
duke@1 237 * Otherwise, return the type itself.
duke@1 238 */
duke@1 239 public Type baseType() {
duke@1 240 return this;
duke@1 241 }
duke@1 242
jjg@2134 243 public Type annotatedType(List<Attribute.TypeCompound> annos) {
jjg@2134 244 return new AnnotatedType(annos, this);
jjg@2134 245 }
jjg@2134 246
jjg@1644 247 public boolean isAnnotated() {
jjg@1644 248 return false;
jjg@1644 249 }
jjg@1644 250
jjg@1521 251 /**
jjg@1521 252 * If this is an annotated type, return the underlying type.
jjg@1521 253 * Otherwise, return the type itself.
jjg@1521 254 */
jjg@1521 255 public Type unannotatedType() {
jjg@1521 256 return this;
jjg@1521 257 }
jjg@1521 258
jjg@1645 259 @Override
alundblad@2100 260 public List<Attribute.TypeCompound> getAnnotationMirrors() {
jjg@1645 261 return List.nil();
jjg@1645 262 }
jjg@1645 263
alundblad@2100 264
jjg@1645 265 @Override
jjg@1645 266 public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
jjg@1645 267 return null;
jjg@1645 268 }
jjg@1645 269
alundblad@2100 270
jjg@1645 271 @Override
jjg@1645 272 public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
jjg@1645 273 @SuppressWarnings("unchecked")
jjg@1645 274 A[] tmp = (A[]) java.lang.reflect.Array.newInstance(annotationType, 0);
jjg@1645 275 return tmp;
jjg@1645 276 }
jjg@1645 277
duke@1 278 /** Return the base types of a list of types.
duke@1 279 */
duke@1 280 public static List<Type> baseTypes(List<Type> ts) {
duke@1 281 if (ts.nonEmpty()) {
duke@1 282 Type t = ts.head.baseType();
duke@1 283 List<Type> baseTypes = baseTypes(ts.tail);
duke@1 284 if (t != ts.head || baseTypes != ts.tail)
duke@1 285 return baseTypes.prepend(t);
duke@1 286 }
duke@1 287 return ts;
duke@1 288 }
duke@1 289
duke@1 290 /** The Java source which this type represents.
duke@1 291 */
duke@1 292 public String toString() {
duke@1 293 String s = (tsym == null || tsym.name == null)
duke@1 294 ? "<none>"
duke@1 295 : tsym.name.toString();
vromero@1853 296 if (moreInfo && hasTag(TYPEVAR)) {
vromero@1853 297 s = s + hashCode();
vromero@1853 298 }
duke@1 299 return s;
duke@1 300 }
duke@1 301
duke@1 302 /**
duke@1 303 * The Java source which this type list represents. A List is
duke@1 304 * represented as a comma-spearated listing of the elements in
duke@1 305 * that list.
duke@1 306 */
duke@1 307 public static String toString(List<Type> ts) {
duke@1 308 if (ts.isEmpty()) {
duke@1 309 return "";
duke@1 310 } else {
jjg@904 311 StringBuilder buf = new StringBuilder();
duke@1 312 buf.append(ts.head.toString());
duke@1 313 for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
duke@1 314 buf.append(",").append(l.head.toString());
duke@1 315 return buf.toString();
duke@1 316 }
duke@1 317 }
duke@1 318
duke@1 319 /**
duke@1 320 * The constant value of this type, converted to String
duke@1 321 */
duke@1 322 public String stringValue() {
jjg@816 323 Object cv = Assert.checkNonNull(constValue());
vromero@1853 324 return cv.toString();
duke@1 325 }
duke@1 326
duke@1 327 /**
duke@1 328 * This method is analogous to isSameType, but weaker, since we
duke@1 329 * never complete classes. Where isSameType would complete a
duke@1 330 * class, equals assumes that the two types are different.
duke@1 331 */
vromero@1452 332 @Override
duke@1 333 public boolean equals(Object t) {
duke@1 334 return super.equals(t);
duke@1 335 }
duke@1 336
vromero@1452 337 @Override
duke@1 338 public int hashCode() {
duke@1 339 return super.hashCode();
duke@1 340 }
duke@1 341
duke@1 342 public String argtypes(boolean varargs) {
duke@1 343 List<Type> args = getParameterTypes();
duke@1 344 if (!varargs) return args.toString();
jjg@789 345 StringBuilder buf = new StringBuilder();
duke@1 346 while (args.tail.nonEmpty()) {
duke@1 347 buf.append(args.head);
duke@1 348 args = args.tail;
duke@1 349 buf.append(',');
duke@1 350 }
vromero@1853 351 if (args.head.unannotatedType().hasTag(ARRAY)) {
jjg@1521 352 buf.append(((ArrayType)args.head.unannotatedType()).elemtype);
jjg@1645 353 if (args.head.getAnnotationMirrors().nonEmpty()) {
jjg@1645 354 buf.append(args.head.getAnnotationMirrors());
jjg@1521 355 }
duke@1 356 buf.append("...");
duke@1 357 } else {
duke@1 358 buf.append(args.head);
duke@1 359 }
duke@1 360 return buf.toString();
duke@1 361 }
duke@1 362
duke@1 363 /** Access methods.
duke@1 364 */
duke@1 365 public List<Type> getTypeArguments() { return List.nil(); }
jjg@1521 366 public Type getEnclosingType() { return null; }
duke@1 367 public List<Type> getParameterTypes() { return List.nil(); }
duke@1 368 public Type getReturnType() { return null; }
jjg@1521 369 public Type getReceiverType() { return null; }
duke@1 370 public List<Type> getThrownTypes() { return List.nil(); }
duke@1 371 public Type getUpperBound() { return null; }
duke@1 372 public Type getLowerBound() { return null; }
duke@1 373
duke@1 374 /** Navigation methods, these will work for classes, type variables,
duke@1 375 * foralls, but will return null for arrays and methods.
duke@1 376 */
duke@1 377
duke@1 378 /** Return all parameters of this type and all its outer types in order
duke@1 379 * outer (first) to inner (last).
duke@1 380 */
duke@1 381 public List<Type> allparams() { return List.nil(); }
duke@1 382
duke@1 383 /** Does this type contain "error" elements?
duke@1 384 */
duke@1 385 public boolean isErroneous() {
duke@1 386 return false;
duke@1 387 }
duke@1 388
duke@1 389 public static boolean isErroneous(List<Type> ts) {
duke@1 390 for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
duke@1 391 if (l.head.isErroneous()) return true;
duke@1 392 return false;
duke@1 393 }
duke@1 394
duke@1 395 /** Is this type parameterized?
duke@1 396 * A class type is parameterized if it has some parameters.
duke@1 397 * An array type is parameterized if its element type is parameterized.
duke@1 398 * All other types are not parameterized.
duke@1 399 */
duke@1 400 public boolean isParameterized() {
duke@1 401 return false;
duke@1 402 }
duke@1 403
duke@1 404 /** Is this type a raw type?
duke@1 405 * A class type is a raw type if it misses some of its parameters.
duke@1 406 * An array type is a raw type if its element type is raw.
duke@1 407 * All other types are not raw.
duke@1 408 * Type validation will ensure that the only raw types
duke@1 409 * in a program are types that miss all their type variables.
duke@1 410 */
duke@1 411 public boolean isRaw() {
duke@1 412 return false;
duke@1 413 }
duke@1 414
duke@1 415 public boolean isCompound() {
duke@1 416 return tsym.completer == null
duke@1 417 // Compound types can't have a completer. Calling
duke@1 418 // flags() will complete the symbol causing the
duke@1 419 // compiler to load classes unnecessarily. This led
duke@1 420 // to regression 6180021.
duke@1 421 && (tsym.flags() & COMPOUND) != 0;
duke@1 422 }
duke@1 423
robm@2904 424 public boolean isIntersection() {
robm@2904 425 return false;
robm@2904 426 }
robm@2904 427
robm@2904 428 public boolean isUnion() {
robm@2904 429 return false;
robm@2904 430 }
robm@2904 431
duke@1 432 public boolean isInterface() {
duke@1 433 return (tsym.flags() & INTERFACE) != 0;
duke@1 434 }
duke@1 435
mcimadamore@640 436 public boolean isFinal() {
mcimadamore@640 437 return (tsym.flags() & FINAL) != 0;
mcimadamore@640 438 }
mcimadamore@640 439
duke@1 440 /**
duke@1 441 * Does this type contain occurrences of type t?
duke@1 442 */
duke@1 443 public boolean contains(Type t) {
duke@1 444 return t == this;
duke@1 445 }
duke@1 446
duke@1 447 public static boolean contains(List<Type> ts, Type t) {
duke@1 448 for (List<Type> l = ts;
duke@1 449 l.tail != null /*inlined: l.nonEmpty()*/;
duke@1 450 l = l.tail)
duke@1 451 if (l.head.contains(t)) return true;
duke@1 452 return false;
duke@1 453 }
duke@1 454
mcimadamore@635 455 /** Does this type contain an occurrence of some type in 'ts'?
duke@1 456 */
mcimadamore@635 457 public boolean containsAny(List<Type> ts) {
mcimadamore@635 458 for (Type t : ts)
mcimadamore@635 459 if (this.contains(t)) return true;
mcimadamore@635 460 return false;
mcimadamore@635 461 }
mcimadamore@635 462
mcimadamore@635 463 public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
mcimadamore@635 464 for (Type t : ts1)
mcimadamore@635 465 if (t.containsAny(ts2)) return true;
duke@1 466 return false;
duke@1 467 }
duke@1 468
mcimadamore@828 469 public static List<Type> filter(List<Type> ts, Filter<Type> tf) {
alundblad@2047 470 ListBuffer<Type> buf = new ListBuffer<>();
mcimadamore@828 471 for (Type t : ts) {
mcimadamore@828 472 if (tf.accepts(t)) {
mcimadamore@828 473 buf.append(t);
mcimadamore@828 474 }
mcimadamore@828 475 }
mcimadamore@828 476 return buf.toList();
mcimadamore@828 477 }
mcimadamore@828 478
duke@1 479 public boolean isSuperBound() { return false; }
duke@1 480 public boolean isExtendsBound() { return false; }
duke@1 481 public boolean isUnbound() { return false; }
duke@1 482 public Type withTypeVar(Type t) { return this; }
duke@1 483
duke@1 484 /** The underlying method type of this type.
duke@1 485 */
duke@1 486 public MethodType asMethodType() { throw new AssertionError(); }
duke@1 487
duke@1 488 /** Complete loading all classes in this type.
duke@1 489 */
duke@1 490 public void complete() {}
duke@1 491
duke@1 492 public TypeSymbol asElement() {
duke@1 493 return tsym;
duke@1 494 }
duke@1 495
vromero@1853 496 @Override
duke@1 497 public TypeKind getKind() {
vromero@1853 498 return TypeKind.OTHER;
duke@1 499 }
duke@1 500
vromero@1853 501 @Override
duke@1 502 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
vromero@1853 503 throw new AssertionError();
vromero@1853 504 }
vromero@1853 505
vromero@1853 506 public static class JCPrimitiveType extends Type
vromero@1853 507 implements javax.lang.model.type.PrimitiveType {
vromero@1853 508
vromero@1853 509 TypeTag tag;
vromero@1853 510
vromero@1853 511 public JCPrimitiveType(TypeTag tag, TypeSymbol tsym) {
vromero@1853 512 super(tsym);
vromero@1853 513 this.tag = tag;
vromero@1853 514 Assert.check(tag.isPrimitive);
vromero@1853 515 }
vromero@1853 516
vromero@1853 517 @Override
vromero@1853 518 public boolean isNumeric() {
vromero@1853 519 return tag != BOOLEAN;
vromero@1853 520 }
vromero@1853 521
vromero@1853 522 @Override
vromero@1853 523 public boolean isPrimitive() {
vromero@1853 524 return true;
vromero@1853 525 }
vromero@1853 526
vromero@1853 527 @Override
vromero@1853 528 public TypeTag getTag() {
vromero@1853 529 return tag;
vromero@1853 530 }
vromero@1853 531
vromero@1853 532 @Override
vromero@1853 533 public boolean isPrimitiveOrVoid() {
vromero@1853 534 return true;
vromero@1853 535 }
vromero@1853 536
vromero@1853 537 /** Define a constant type, of the same kind as this type
vromero@1853 538 * and with given constant value
vromero@1853 539 */
vromero@1853 540 @Override
vromero@1853 541 public Type constType(Object constValue) {
vromero@1853 542 final Object value = constValue;
vromero@1853 543 return new JCPrimitiveType(tag, tsym) {
vromero@1853 544 @Override
vromero@1853 545 public Object constValue() {
vromero@1853 546 return value;
vromero@1853 547 }
vromero@1853 548 @Override
vromero@1853 549 public Type baseType() {
vromero@1853 550 return tsym.type;
vromero@1853 551 }
vromero@1853 552 };
vromero@1853 553 }
vromero@1853 554
vromero@1853 555 /**
vromero@1853 556 * The constant value of this type, converted to String
vromero@1853 557 */
vromero@1853 558 @Override
vromero@1853 559 public String stringValue() {
vromero@1853 560 Object cv = Assert.checkNonNull(constValue());
vromero@1853 561 if (tag == BOOLEAN) {
vromero@1853 562 return ((Integer) cv).intValue() == 0 ? "false" : "true";
vromero@1853 563 }
vromero@1853 564 else if (tag == CHAR) {
vromero@1853 565 return String.valueOf((char) ((Integer) cv).intValue());
vromero@1853 566 }
vromero@1853 567 else {
vromero@1853 568 return cv.toString();
vromero@1853 569 }
vromero@1853 570 }
vromero@1853 571
vromero@1853 572 /** Is this a constant type whose value is false?
vromero@1853 573 */
vromero@1853 574 @Override
vromero@1853 575 public boolean isFalse() {
vromero@1853 576 return
vromero@1853 577 tag == BOOLEAN &&
vromero@1853 578 constValue() != null &&
vromero@1853 579 ((Integer)constValue()).intValue() == 0;
vromero@1853 580 }
vromero@1853 581
vromero@1853 582 /** Is this a constant type whose value is true?
vromero@1853 583 */
vromero@1853 584 @Override
vromero@1853 585 public boolean isTrue() {
vromero@1853 586 return
vromero@1853 587 tag == BOOLEAN &&
vromero@1853 588 constValue() != null &&
vromero@1853 589 ((Integer)constValue()).intValue() != 0;
vromero@1853 590 }
vromero@1853 591
vromero@1853 592 @Override
vromero@1853 593 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 594 return v.visitPrimitive(this, p);
vromero@1853 595 }
vromero@1853 596
vromero@1853 597 @Override
vromero@1853 598 public TypeKind getKind() {
vromero@1853 599 switch (tag) {
vromero@1853 600 case BYTE: return TypeKind.BYTE;
vromero@1853 601 case CHAR: return TypeKind.CHAR;
vromero@1853 602 case SHORT: return TypeKind.SHORT;
vromero@1853 603 case INT: return TypeKind.INT;
vromero@1853 604 case LONG: return TypeKind.LONG;
vromero@1853 605 case FLOAT: return TypeKind.FLOAT;
vromero@1853 606 case DOUBLE: return TypeKind.DOUBLE;
vromero@1853 607 case BOOLEAN: return TypeKind.BOOLEAN;
vromero@1853 608 }
duke@1 609 throw new AssertionError();
vromero@1853 610 }
vromero@1853 611
duke@1 612 }
duke@1 613
duke@1 614 public static class WildcardType extends Type
duke@1 615 implements javax.lang.model.type.WildcardType {
duke@1 616
duke@1 617 public Type type;
duke@1 618 public BoundKind kind;
duke@1 619 public TypeVar bound;
duke@1 620
duke@1 621 @Override
duke@1 622 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 623 return v.visitWildcardType(this, s);
duke@1 624 }
duke@1 625
duke@1 626 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
vromero@1853 627 super(tsym);
jjg@816 628 this.type = Assert.checkNonNull(type);
duke@1 629 this.kind = kind;
duke@1 630 }
duke@1 631 public WildcardType(WildcardType t, TypeVar bound) {
duke@1 632 this(t.type, t.kind, t.tsym, bound);
duke@1 633 }
duke@1 634
duke@1 635 public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
duke@1 636 this(type, kind, tsym);
duke@1 637 this.bound = bound;
duke@1 638 }
duke@1 639
vromero@1853 640 @Override
vromero@1853 641 public TypeTag getTag() {
vromero@1853 642 return WILDCARD;
vromero@1853 643 }
vromero@1853 644
vromero@1853 645 @Override
mcimadamore@635 646 public boolean contains(Type t) {
mcimadamore@635 647 return kind != UNBOUND && type.contains(t);
mcimadamore@635 648 }
mcimadamore@635 649
duke@1 650 public boolean isSuperBound() {
duke@1 651 return kind == SUPER ||
duke@1 652 kind == UNBOUND;
duke@1 653 }
duke@1 654 public boolean isExtendsBound() {
duke@1 655 return kind == EXTENDS ||
duke@1 656 kind == UNBOUND;
duke@1 657 }
duke@1 658 public boolean isUnbound() {
duke@1 659 return kind == UNBOUND;
duke@1 660 }
duke@1 661
vromero@1853 662 @Override
vromero@1853 663 public boolean isReference() {
vromero@1853 664 return true;
vromero@1853 665 }
vromero@1853 666
vromero@1853 667 @Override
vromero@1853 668 public boolean isNullOrReference() {
vromero@1853 669 return true;
vromero@1853 670 }
vromero@1853 671
vromero@1853 672 @Override
duke@1 673 public Type withTypeVar(Type t) {
duke@1 674 //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
duke@1 675 if (bound == t)
duke@1 676 return this;
duke@1 677 bound = (TypeVar)t;
duke@1 678 return this;
duke@1 679 }
duke@1 680
duke@1 681 boolean isPrintingBound = false;
duke@1 682 public String toString() {
jjg@904 683 StringBuilder s = new StringBuilder();
duke@1 684 s.append(kind.toString());
duke@1 685 if (kind != UNBOUND)
duke@1 686 s.append(type);
duke@1 687 if (moreInfo && bound != null && !isPrintingBound)
duke@1 688 try {
duke@1 689 isPrintingBound = true;
duke@1 690 s.append("{:").append(bound.bound).append(":}");
duke@1 691 } finally {
duke@1 692 isPrintingBound = false;
duke@1 693 }
duke@1 694 return s.toString();
duke@1 695 }
duke@1 696
duke@1 697 public Type map(Mapping f) {
duke@1 698 //- System.err.println(" (" + this + ").map(" + f + ")");//DEBUG
duke@1 699 Type t = type;
duke@1 700 if (t != null)
duke@1 701 t = f.apply(t);
duke@1 702 if (t == type)
duke@1 703 return this;
duke@1 704 else
duke@1 705 return new WildcardType(t, kind, tsym, bound);
duke@1 706 }
duke@1 707
duke@1 708 public Type getExtendsBound() {
duke@1 709 if (kind == EXTENDS)
duke@1 710 return type;
duke@1 711 else
duke@1 712 return null;
duke@1 713 }
duke@1 714
duke@1 715 public Type getSuperBound() {
duke@1 716 if (kind == SUPER)
duke@1 717 return type;
duke@1 718 else
duke@1 719 return null;
duke@1 720 }
duke@1 721
duke@1 722 public TypeKind getKind() {
duke@1 723 return TypeKind.WILDCARD;
duke@1 724 }
duke@1 725
duke@1 726 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 727 return v.visitWildcard(this, p);
duke@1 728 }
duke@1 729 }
duke@1 730
duke@1 731 public static class ClassType extends Type implements DeclaredType {
duke@1 732
duke@1 733 /** The enclosing type of this type. If this is the type of an inner
duke@1 734 * class, outer_field refers to the type of its enclosing
jjg@1521 735 * instance class, in all other cases it refers to noType.
duke@1 736 */
duke@1 737 private Type outer_field;
duke@1 738
duke@1 739 /** The type parameters of this type (to be set once class is loaded).
duke@1 740 */
duke@1 741 public List<Type> typarams_field;
duke@1 742
duke@1 743 /** A cache variable for the type parameters of this type,
duke@1 744 * appended to all parameters of its enclosing class.
duke@1 745 * @see #allparams
duke@1 746 */
duke@1 747 public List<Type> allparams_field;
duke@1 748
duke@1 749 /** The supertype of this class (to be set once class is loaded).
duke@1 750 */
duke@1 751 public Type supertype_field;
duke@1 752
duke@1 753 /** The interfaces of this class (to be set once class is loaded).
duke@1 754 */
duke@1 755 public List<Type> interfaces_field;
duke@1 756
jjg@904 757 /** All the interfaces of this class, including missing ones.
jjg@904 758 */
jjg@904 759 public List<Type> all_interfaces_field;
jjg@904 760
duke@1 761 public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
vromero@1853 762 super(tsym);
duke@1 763 this.outer_field = outer;
duke@1 764 this.typarams_field = typarams;
duke@1 765 this.allparams_field = null;
duke@1 766 this.supertype_field = null;
duke@1 767 this.interfaces_field = null;
duke@1 768 /*
duke@1 769 // this can happen during error recovery
duke@1 770 assert
duke@1 771 outer.isParameterized() ?
duke@1 772 typarams.length() == tsym.type.typarams().length() :
duke@1 773 outer.isRaw() ?
duke@1 774 typarams.length() == 0 :
duke@1 775 true;
duke@1 776 */
duke@1 777 }
duke@1 778
duke@1 779 @Override
vromero@1853 780 public TypeTag getTag() {
vromero@1853 781 return CLASS;
vromero@1853 782 }
vromero@1853 783
vromero@1853 784 @Override
duke@1 785 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 786 return v.visitClassType(this, s);
duke@1 787 }
duke@1 788
duke@1 789 public Type constType(Object constValue) {
duke@1 790 final Object value = constValue;
duke@1 791 return new ClassType(getEnclosingType(), typarams_field, tsym) {
duke@1 792 @Override
duke@1 793 public Object constValue() {
duke@1 794 return value;
duke@1 795 }
duke@1 796 @Override
duke@1 797 public Type baseType() {
duke@1 798 return tsym.type;
duke@1 799 }
duke@1 800 };
duke@1 801 }
duke@1 802
duke@1 803 /** The Java source which this type represents.
duke@1 804 */
duke@1 805 public String toString() {
jjg@904 806 StringBuilder buf = new StringBuilder();
vromero@1853 807 if (getEnclosingType().hasTag(CLASS) && tsym.owner.kind == TYP) {
duke@1 808 buf.append(getEnclosingType().toString());
duke@1 809 buf.append(".");
duke@1 810 buf.append(className(tsym, false));
duke@1 811 } else {
duke@1 812 buf.append(className(tsym, true));
duke@1 813 }
duke@1 814 if (getTypeArguments().nonEmpty()) {
duke@1 815 buf.append('<');
duke@1 816 buf.append(getTypeArguments().toString());
duke@1 817 buf.append(">");
duke@1 818 }
duke@1 819 return buf.toString();
duke@1 820 }
duke@1 821 //where
duke@1 822 private String className(Symbol sym, boolean longform) {
jjg@113 823 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
jjg@904 824 StringBuilder s = new StringBuilder(supertype_field.toString());
duke@1 825 for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
duke@1 826 s.append("&");
duke@1 827 s.append(is.head.toString());
duke@1 828 }
duke@1 829 return s.toString();
jjg@113 830 } else if (sym.name.isEmpty()) {
duke@1 831 String s;
jjg@1755 832 ClassType norm = (ClassType) tsym.type.unannotatedType();
duke@1 833 if (norm == null) {
duke@1 834 s = Log.getLocalizedString("anonymous.class", (Object)null);
duke@1 835 } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
duke@1 836 s = Log.getLocalizedString("anonymous.class",
duke@1 837 norm.interfaces_field.head);
duke@1 838 } else {
duke@1 839 s = Log.getLocalizedString("anonymous.class",
duke@1 840 norm.supertype_field);
duke@1 841 }
duke@1 842 if (moreInfo)
duke@1 843 s += String.valueOf(sym.hashCode());
duke@1 844 return s;
duke@1 845 } else if (longform) {
duke@1 846 return sym.getQualifiedName().toString();
duke@1 847 } else {
duke@1 848 return sym.name.toString();
duke@1 849 }
duke@1 850 }
duke@1 851
duke@1 852 public List<Type> getTypeArguments() {
duke@1 853 if (typarams_field == null) {
duke@1 854 complete();
duke@1 855 if (typarams_field == null)
duke@1 856 typarams_field = List.nil();
duke@1 857 }
duke@1 858 return typarams_field;
duke@1 859 }
duke@1 860
mcimadamore@30 861 public boolean hasErasedSupertypes() {
mcimadamore@30 862 return isRaw();
mcimadamore@30 863 }
mcimadamore@30 864
duke@1 865 public Type getEnclosingType() {
duke@1 866 return outer_field;
duke@1 867 }
duke@1 868
duke@1 869 public void setEnclosingType(Type outer) {
duke@1 870 outer_field = outer;
duke@1 871 }
duke@1 872
duke@1 873 public List<Type> allparams() {
duke@1 874 if (allparams_field == null) {
duke@1 875 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
duke@1 876 }
duke@1 877 return allparams_field;
duke@1 878 }
duke@1 879
duke@1 880 public boolean isErroneous() {
duke@1 881 return
duke@1 882 getEnclosingType().isErroneous() ||
duke@1 883 isErroneous(getTypeArguments()) ||
jjg@1755 884 this != tsym.type.unannotatedType() && tsym.type.isErroneous();
duke@1 885 }
duke@1 886
duke@1 887 public boolean isParameterized() {
duke@1 888 return allparams().tail != null;
duke@1 889 // optimization, was: allparams().nonEmpty();
duke@1 890 }
duke@1 891
vromero@1853 892 @Override
vromero@1853 893 public boolean isReference() {
vromero@1853 894 return true;
vromero@1853 895 }
vromero@1853 896
vromero@1853 897 @Override
vromero@1853 898 public boolean isNullOrReference() {
vromero@1853 899 return true;
vromero@1853 900 }
vromero@1853 901
duke@1 902 /** A cache for the rank. */
duke@1 903 int rank_field = -1;
duke@1 904
duke@1 905 /** A class type is raw if it misses some
duke@1 906 * of its type parameter sections.
duke@1 907 * After validation, this is equivalent to:
jjg@1326 908 * {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); }
duke@1 909 */
duke@1 910 public boolean isRaw() {
duke@1 911 return
duke@1 912 this != tsym.type && // necessary, but not sufficient condition
duke@1 913 tsym.type.allparams().nonEmpty() &&
duke@1 914 allparams().isEmpty();
duke@1 915 }
duke@1 916
duke@1 917 public Type map(Mapping f) {
duke@1 918 Type outer = getEnclosingType();
duke@1 919 Type outer1 = f.apply(outer);
duke@1 920 List<Type> typarams = getTypeArguments();
duke@1 921 List<Type> typarams1 = map(typarams, f);
duke@1 922 if (outer1 == outer && typarams1 == typarams) return this;
duke@1 923 else return new ClassType(outer1, typarams1, tsym);
duke@1 924 }
duke@1 925
duke@1 926 public boolean contains(Type elem) {
duke@1 927 return
duke@1 928 elem == this
duke@1 929 || (isParameterized()
mcimadamore@635 930 && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
mcimadamore@635 931 || (isCompound()
mcimadamore@635 932 && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
duke@1 933 }
duke@1 934
duke@1 935 public void complete() {
duke@1 936 if (tsym.completer != null) tsym.complete();
duke@1 937 }
duke@1 938
duke@1 939 public TypeKind getKind() {
duke@1 940 return TypeKind.DECLARED;
duke@1 941 }
duke@1 942
duke@1 943 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 944 return v.visitDeclared(this, p);
duke@1 945 }
duke@1 946 }
duke@1 947
mcimadamore@30 948 public static class ErasedClassType extends ClassType {
mcimadamore@30 949 public ErasedClassType(Type outer, TypeSymbol tsym) {
mcimadamore@30 950 super(outer, List.<Type>nil(), tsym);
mcimadamore@30 951 }
mcimadamore@30 952
mcimadamore@30 953 @Override
mcimadamore@30 954 public boolean hasErasedSupertypes() {
mcimadamore@30 955 return true;
mcimadamore@30 956 }
mcimadamore@30 957 }
mcimadamore@30 958
jjg@988 959 // a clone of a ClassType that knows about the alternatives of a union type.
jjg@988 960 public static class UnionClassType extends ClassType implements UnionType {
jjg@988 961 final List<? extends Type> alternatives_field;
jjg@988 962
jjg@988 963 public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
jjg@988 964 super(ct.outer_field, ct.typarams_field, ct.tsym);
jjg@988 965 allparams_field = ct.allparams_field;
jjg@988 966 supertype_field = ct.supertype_field;
jjg@988 967 interfaces_field = ct.interfaces_field;
jjg@988 968 all_interfaces_field = ct.interfaces_field;
jjg@988 969 alternatives_field = alternatives;
jjg@988 970 }
jjg@988 971
jjg@988 972 public Type getLub() {
jjg@988 973 return tsym.type;
jjg@988 974 }
jjg@988 975
jjg@988 976 public java.util.List<? extends TypeMirror> getAlternatives() {
jjg@988 977 return Collections.unmodifiableList(alternatives_field);
jjg@988 978 }
jjg@988 979
jjg@988 980 @Override
robm@2904 981 public boolean isUnion() {
robm@2904 982 return true;
robm@2904 983 }
robm@2904 984
robm@2904 985 @Override
jjg@988 986 public TypeKind getKind() {
jjg@988 987 return TypeKind.UNION;
jjg@988 988 }
jjg@988 989
jjg@988 990 @Override
jjg@988 991 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
jjg@988 992 return v.visitUnion(this, p);
jjg@988 993 }
jjg@988 994 }
jjg@988 995
mcimadamore@1436 996 // a clone of a ClassType that knows about the bounds of an intersection type.
mcimadamore@1436 997 public static class IntersectionClassType extends ClassType implements IntersectionType {
mcimadamore@1436 998
mcimadamore@1436 999 public boolean allInterfaces;
mcimadamore@1436 1000
mcimadamore@1436 1001 public IntersectionClassType(List<Type> bounds, ClassSymbol csym, boolean allInterfaces) {
mcimadamore@1436 1002 super(Type.noType, List.<Type>nil(), csym);
mcimadamore@1436 1003 this.allInterfaces = allInterfaces;
mcimadamore@1436 1004 Assert.check((csym.flags() & COMPOUND) != 0);
mcimadamore@1436 1005 supertype_field = bounds.head;
mcimadamore@1436 1006 interfaces_field = bounds.tail;
mcimadamore@1436 1007 Assert.check(supertype_field.tsym.completer != null ||
mcimadamore@1436 1008 !supertype_field.isInterface(), supertype_field);
mcimadamore@1436 1009 }
mcimadamore@1436 1010
mcimadamore@1436 1011 public java.util.List<? extends TypeMirror> getBounds() {
emc@2050 1012 return Collections.unmodifiableList(getExplicitComponents());
mcimadamore@1436 1013 }
mcimadamore@1436 1014
mcimadamore@1436 1015 public List<Type> getComponents() {
mcimadamore@1436 1016 return interfaces_field.prepend(supertype_field);
mcimadamore@1436 1017 }
mcimadamore@1436 1018
robm@2904 1019 @Override
robm@2904 1020 public boolean isIntersection() {
robm@2904 1021 return true;
robm@2904 1022 }
robm@2904 1023
mcimadamore@1678 1024 public List<Type> getExplicitComponents() {
mcimadamore@1678 1025 return allInterfaces ?
mcimadamore@1678 1026 interfaces_field :
mcimadamore@1678 1027 getComponents();
mcimadamore@1678 1028 }
mcimadamore@1678 1029
mcimadamore@1436 1030 @Override
mcimadamore@1436 1031 public TypeKind getKind() {
mcimadamore@1436 1032 return TypeKind.INTERSECTION;
mcimadamore@1436 1033 }
mcimadamore@1436 1034
mcimadamore@1436 1035 @Override
mcimadamore@1436 1036 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
jlahoda@2200 1037 return v.visitIntersection(this, p);
mcimadamore@1436 1038 }
mcimadamore@1436 1039 }
mcimadamore@1436 1040
duke@1 1041 public static class ArrayType extends Type
duke@1 1042 implements javax.lang.model.type.ArrayType {
duke@1 1043
duke@1 1044 public Type elemtype;
duke@1 1045
duke@1 1046 public ArrayType(Type elemtype, TypeSymbol arrayClass) {
vromero@1853 1047 super(arrayClass);
duke@1 1048 this.elemtype = elemtype;
duke@1 1049 }
duke@1 1050
duke@1 1051 @Override
vromero@1853 1052 public TypeTag getTag() {
vromero@1853 1053 return ARRAY;
vromero@1853 1054 }
vromero@1853 1055
duke@1 1056 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1057 return v.visitArrayType(this, s);
duke@1 1058 }
duke@1 1059
duke@1 1060 public String toString() {
duke@1 1061 return elemtype + "[]";
duke@1 1062 }
duke@1 1063
duke@1 1064 public boolean equals(Object obj) {
duke@1 1065 return
duke@1 1066 this == obj ||
duke@1 1067 (obj instanceof ArrayType &&
duke@1 1068 this.elemtype.equals(((ArrayType)obj).elemtype));
duke@1 1069 }
duke@1 1070
duke@1 1071 public int hashCode() {
jjg@1374 1072 return (ARRAY.ordinal() << 5) + elemtype.hashCode();
duke@1 1073 }
duke@1 1074
mcimadamore@795 1075 public boolean isVarargs() {
mcimadamore@795 1076 return false;
mcimadamore@795 1077 }
mcimadamore@795 1078
duke@1 1079 public List<Type> allparams() { return elemtype.allparams(); }
duke@1 1080
duke@1 1081 public boolean isErroneous() {
duke@1 1082 return elemtype.isErroneous();
duke@1 1083 }
duke@1 1084
duke@1 1085 public boolean isParameterized() {
duke@1 1086 return elemtype.isParameterized();
duke@1 1087 }
duke@1 1088
vromero@1853 1089 @Override
vromero@1853 1090 public boolean isReference() {
vromero@1853 1091 return true;
vromero@1853 1092 }
vromero@1853 1093
vromero@1853 1094 @Override
vromero@1853 1095 public boolean isNullOrReference() {
vromero@1853 1096 return true;
vromero@1853 1097 }
vromero@1853 1098
duke@1 1099 public boolean isRaw() {
duke@1 1100 return elemtype.isRaw();
duke@1 1101 }
duke@1 1102
mcimadamore@795 1103 public ArrayType makeVarargs() {
mcimadamore@795 1104 return new ArrayType(elemtype, tsym) {
mcimadamore@795 1105 @Override
mcimadamore@795 1106 public boolean isVarargs() {
mcimadamore@795 1107 return true;
mcimadamore@795 1108 }
mcimadamore@795 1109 };
mcimadamore@795 1110 }
mcimadamore@795 1111
duke@1 1112 public Type map(Mapping f) {
duke@1 1113 Type elemtype1 = f.apply(elemtype);
duke@1 1114 if (elemtype1 == elemtype) return this;
duke@1 1115 else return new ArrayType(elemtype1, tsym);
duke@1 1116 }
duke@1 1117
duke@1 1118 public boolean contains(Type elem) {
duke@1 1119 return elem == this || elemtype.contains(elem);
duke@1 1120 }
duke@1 1121
duke@1 1122 public void complete() {
duke@1 1123 elemtype.complete();
duke@1 1124 }
duke@1 1125
duke@1 1126 public Type getComponentType() {
duke@1 1127 return elemtype;
duke@1 1128 }
duke@1 1129
duke@1 1130 public TypeKind getKind() {
duke@1 1131 return TypeKind.ARRAY;
duke@1 1132 }
duke@1 1133
duke@1 1134 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1135 return v.visitArray(this, p);
duke@1 1136 }
duke@1 1137 }
duke@1 1138
dlsmith@880 1139 public static class MethodType extends Type implements ExecutableType {
duke@1 1140
duke@1 1141 public List<Type> argtypes;
duke@1 1142 public Type restype;
duke@1 1143 public List<Type> thrown;
duke@1 1144
jjg@1521 1145 /** The type annotations on the method receiver.
jjg@1521 1146 */
jjg@1521 1147 public Type recvtype;
jjg@1521 1148
duke@1 1149 public MethodType(List<Type> argtypes,
duke@1 1150 Type restype,
duke@1 1151 List<Type> thrown,
duke@1 1152 TypeSymbol methodClass) {
vromero@1853 1153 super(methodClass);
duke@1 1154 this.argtypes = argtypes;
duke@1 1155 this.restype = restype;
duke@1 1156 this.thrown = thrown;
duke@1 1157 }
duke@1 1158
duke@1 1159 @Override
vromero@1853 1160 public TypeTag getTag() {
vromero@1853 1161 return METHOD;
vromero@1853 1162 }
vromero@1853 1163
duke@1 1164 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1165 return v.visitMethodType(this, s);
duke@1 1166 }
duke@1 1167
duke@1 1168 /** The Java source which this type represents.
duke@1 1169 *
duke@1 1170 * XXX 06/09/99 iris This isn't correct Java syntax, but it probably
duke@1 1171 * should be.
duke@1 1172 */
duke@1 1173 public String toString() {
duke@1 1174 return "(" + argtypes + ")" + restype;
duke@1 1175 }
duke@1 1176
duke@1 1177 public List<Type> getParameterTypes() { return argtypes; }
duke@1 1178 public Type getReturnType() { return restype; }
jjg@1521 1179 public Type getReceiverType() { return recvtype; }
duke@1 1180 public List<Type> getThrownTypes() { return thrown; }
duke@1 1181
duke@1 1182 public boolean isErroneous() {
duke@1 1183 return
duke@1 1184 isErroneous(argtypes) ||
duke@1 1185 restype != null && restype.isErroneous();
duke@1 1186 }
duke@1 1187
duke@1 1188 public Type map(Mapping f) {
duke@1 1189 List<Type> argtypes1 = map(argtypes, f);
duke@1 1190 Type restype1 = f.apply(restype);
duke@1 1191 List<Type> thrown1 = map(thrown, f);
duke@1 1192 if (argtypes1 == argtypes &&
duke@1 1193 restype1 == restype &&
duke@1 1194 thrown1 == thrown) return this;
duke@1 1195 else return new MethodType(argtypes1, restype1, thrown1, tsym);
duke@1 1196 }
duke@1 1197
duke@1 1198 public boolean contains(Type elem) {
jlahoda@1955 1199 return elem == this || contains(argtypes, elem) || restype.contains(elem) || contains(thrown, elem);
duke@1 1200 }
duke@1 1201
duke@1 1202 public MethodType asMethodType() { return this; }
duke@1 1203
duke@1 1204 public void complete() {
duke@1 1205 for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
duke@1 1206 l.head.complete();
duke@1 1207 restype.complete();
jjg@1521 1208 recvtype.complete();
duke@1 1209 for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
duke@1 1210 l.head.complete();
duke@1 1211 }
duke@1 1212
duke@1 1213 public List<TypeVar> getTypeVariables() {
duke@1 1214 return List.nil();
duke@1 1215 }
duke@1 1216
duke@1 1217 public TypeSymbol asElement() {
duke@1 1218 return null;
duke@1 1219 }
duke@1 1220
duke@1 1221 public TypeKind getKind() {
duke@1 1222 return TypeKind.EXECUTABLE;
duke@1 1223 }
duke@1 1224
duke@1 1225 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1226 return v.visitExecutable(this, p);
duke@1 1227 }
duke@1 1228 }
duke@1 1229
duke@1 1230 public static class PackageType extends Type implements NoType {
duke@1 1231
duke@1 1232 PackageType(TypeSymbol tsym) {
vromero@1853 1233 super(tsym);
vromero@1853 1234 }
vromero@1853 1235
vromero@1853 1236 @Override
vromero@1853 1237 public TypeTag getTag() {
vromero@1853 1238 return PACKAGE;
duke@1 1239 }
duke@1 1240
duke@1 1241 @Override
duke@1 1242 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1243 return v.visitPackageType(this, s);
duke@1 1244 }
duke@1 1245
duke@1 1246 public String toString() {
duke@1 1247 return tsym.getQualifiedName().toString();
duke@1 1248 }
duke@1 1249
duke@1 1250 public TypeKind getKind() {
duke@1 1251 return TypeKind.PACKAGE;
duke@1 1252 }
duke@1 1253
duke@1 1254 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1255 return v.visitNoType(this, p);
duke@1 1256 }
duke@1 1257 }
duke@1 1258
duke@1 1259 public static class TypeVar extends Type implements TypeVariable {
duke@1 1260
jjg@789 1261 /** The upper bound of this type variable; set from outside.
duke@1 1262 * Must be nonempty once it is set.
duke@1 1263 * For a bound, `bound' is the bound type itself.
duke@1 1264 * Multiple bounds are expressed as a single class type which has the
duke@1 1265 * individual bounds as superclass, respectively interfaces.
duke@1 1266 * The class type then has as `tsym' a compiler generated class `c',
duke@1 1267 * which has a flag COMPOUND and whose owner is the type variable
duke@1 1268 * itself. Furthermore, the erasure_field of the class
duke@1 1269 * points to the first class or interface bound.
duke@1 1270 */
duke@1 1271 public Type bound = null;
jjg@789 1272
jjg@789 1273 /** The lower bound of this type variable.
jjg@789 1274 * TypeVars don't normally have a lower bound, so it is normally set
jjg@789 1275 * to syms.botType.
jjg@789 1276 * Subtypes, such as CapturedType, may provide a different value.
jjg@789 1277 */
duke@1 1278 public Type lower;
duke@1 1279
duke@1 1280 public TypeVar(Name name, Symbol owner, Type lower) {
vromero@1853 1281 super(null);
jfranck@1689 1282 tsym = new TypeVariableSymbol(0, name, this, owner);
duke@1 1283 this.lower = lower;
duke@1 1284 }
duke@1 1285
duke@1 1286 public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
vromero@1853 1287 super(tsym);
duke@1 1288 this.bound = bound;
duke@1 1289 this.lower = lower;
duke@1 1290 }
duke@1 1291
duke@1 1292 @Override
vromero@1853 1293 public TypeTag getTag() {
vromero@1853 1294 return TYPEVAR;
vromero@1853 1295 }
vromero@1853 1296
vromero@1853 1297 @Override
duke@1 1298 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1299 return v.visitTypeVar(this, s);
duke@1 1300 }
duke@1 1301
jjg@789 1302 @Override
jjg@1521 1303 public Type getUpperBound() {
vromero@1853 1304 if ((bound == null || bound.hasTag(NONE)) && this != tsym.type) {
jjg@1521 1305 bound = tsym.type.getUpperBound();
vromero@1853 1306 }
jjg@1521 1307 return bound;
jjg@1521 1308 }
duke@1 1309
duke@1 1310 int rank_field = -1;
duke@1 1311
jjg@789 1312 @Override
duke@1 1313 public Type getLowerBound() {
duke@1 1314 return lower;
duke@1 1315 }
duke@1 1316
duke@1 1317 public TypeKind getKind() {
duke@1 1318 return TypeKind.TYPEVAR;
duke@1 1319 }
duke@1 1320
mcimadamore@79 1321 public boolean isCaptured() {
mcimadamore@79 1322 return false;
mcimadamore@79 1323 }
mcimadamore@79 1324
vromero@1853 1325 @Override
vromero@1853 1326 public boolean isReference() {
vromero@1853 1327 return true;
vromero@1853 1328 }
vromero@1853 1329
vromero@1853 1330 @Override
vromero@1853 1331 public boolean isNullOrReference() {
vromero@1853 1332 return true;
vromero@1853 1333 }
vromero@1853 1334
vromero@1853 1335 @Override
duke@1 1336 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1337 return v.visitTypeVariable(this, p);
duke@1 1338 }
duke@1 1339 }
duke@1 1340
duke@1 1341 /** A captured type variable comes from wildcards which can have
duke@1 1342 * both upper and lower bound. CapturedType extends TypeVar with
duke@1 1343 * a lower bound.
duke@1 1344 */
duke@1 1345 public static class CapturedType extends TypeVar {
duke@1 1346
duke@1 1347 public WildcardType wildcard;
duke@1 1348
duke@1 1349 public CapturedType(Name name,
duke@1 1350 Symbol owner,
duke@1 1351 Type upper,
duke@1 1352 Type lower,
duke@1 1353 WildcardType wildcard) {
duke@1 1354 super(name, owner, lower);
jjg@816 1355 this.lower = Assert.checkNonNull(lower);
duke@1 1356 this.bound = upper;
duke@1 1357 this.wildcard = wildcard;
duke@1 1358 }
duke@1 1359
duke@1 1360 @Override
duke@1 1361 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1362 return v.visitCapturedType(this, s);
duke@1 1363 }
duke@1 1364
duke@1 1365 @Override
mcimadamore@79 1366 public boolean isCaptured() {
mcimadamore@79 1367 return true;
mcimadamore@79 1368 }
mcimadamore@79 1369
mcimadamore@79 1370 @Override
duke@1 1371 public String toString() {
duke@1 1372 return "capture#"
mcimadamore@288 1373 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
duke@1 1374 + " of "
duke@1 1375 + wildcard;
duke@1 1376 }
duke@1 1377 }
duke@1 1378
duke@1 1379 public static abstract class DelegatedType extends Type {
duke@1 1380 public Type qtype;
vromero@1853 1381 public TypeTag tag;
jjg@1374 1382 public DelegatedType(TypeTag tag, Type qtype) {
vromero@1853 1383 super(qtype.tsym);
vromero@1853 1384 this.tag = tag;
duke@1 1385 this.qtype = qtype;
duke@1 1386 }
vromero@1853 1387 public TypeTag getTag() { return tag; }
duke@1 1388 public String toString() { return qtype.toString(); }
duke@1 1389 public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
duke@1 1390 public Type getEnclosingType() { return qtype.getEnclosingType(); }
duke@1 1391 public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
duke@1 1392 public Type getReturnType() { return qtype.getReturnType(); }
jjg@1521 1393 public Type getReceiverType() { return qtype.getReceiverType(); }
duke@1 1394 public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
duke@1 1395 public List<Type> allparams() { return qtype.allparams(); }
duke@1 1396 public Type getUpperBound() { return qtype.getUpperBound(); }
duke@1 1397 public boolean isErroneous() { return qtype.isErroneous(); }
duke@1 1398 }
duke@1 1399
mcimadamore@1268 1400 /**
mcimadamore@1268 1401 * The type of a generic method type. It consists of a method type and
mcimadamore@1268 1402 * a list of method type-parameters that are used within the method
mcimadamore@1268 1403 * type.
mcimadamore@1268 1404 */
dlsmith@880 1405 public static class ForAll extends DelegatedType implements ExecutableType {
duke@1 1406 public List<Type> tvars;
duke@1 1407
duke@1 1408 public ForAll(List<Type> tvars, Type qtype) {
mcimadamore@1268 1409 super(FORALL, (MethodType)qtype);
duke@1 1410 this.tvars = tvars;
duke@1 1411 }
duke@1 1412
duke@1 1413 @Override
duke@1 1414 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1415 return v.visitForAll(this, s);
duke@1 1416 }
duke@1 1417
duke@1 1418 public String toString() {
duke@1 1419 return "<" + tvars + ">" + qtype;
duke@1 1420 }
duke@1 1421
duke@1 1422 public List<Type> getTypeArguments() { return tvars; }
duke@1 1423
duke@1 1424 public boolean isErroneous() {
duke@1 1425 return qtype.isErroneous();
duke@1 1426 }
duke@1 1427
duke@1 1428 public Type map(Mapping f) {
duke@1 1429 return f.apply(qtype);
duke@1 1430 }
duke@1 1431
duke@1 1432 public boolean contains(Type elem) {
duke@1 1433 return qtype.contains(elem);
duke@1 1434 }
duke@1 1435
duke@1 1436 public MethodType asMethodType() {
mcimadamore@1268 1437 return (MethodType)qtype;
duke@1 1438 }
duke@1 1439
duke@1 1440 public void complete() {
duke@1 1441 for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
duke@1 1442 ((TypeVar)l.head).bound.complete();
duke@1 1443 }
duke@1 1444 qtype.complete();
duke@1 1445 }
duke@1 1446
duke@1 1447 public List<TypeVar> getTypeVariables() {
duke@1 1448 return List.convert(TypeVar.class, getTypeArguments());
duke@1 1449 }
duke@1 1450
duke@1 1451 public TypeKind getKind() {
duke@1 1452 return TypeKind.EXECUTABLE;
duke@1 1453 }
duke@1 1454
duke@1 1455 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1456 return v.visitExecutable(this, p);
duke@1 1457 }
duke@1 1458 }
duke@1 1459
mcimadamore@1338 1460 /** A class for inference variables, for use during method/diamond type
mcimadamore@1338 1461 * inference. An inference variable has upper/lower bounds and a set
mcimadamore@1338 1462 * of equality constraints. Such bounds are set during subtyping, type-containment,
mcimadamore@1338 1463 * type-equality checks, when the types being tested contain inference variables.
mcimadamore@1338 1464 * A change listener can be attached to an inference variable, to receive notifications
mcimadamore@1338 1465 * whenever the bounds of an inference variable change.
duke@1 1466 */
duke@1 1467 public static class UndetVar extends DelegatedType {
mcimadamore@1338 1468
mcimadamore@1338 1469 /** Inference variable change listener. The listener method is called
mcimadamore@1338 1470 * whenever a change to the inference variable's bounds occurs
mcimadamore@1338 1471 */
mcimadamore@1338 1472 public interface UndetVarListener {
mcimadamore@1338 1473 /** called when some inference variable bounds (of given kinds ibs) change */
mcimadamore@1338 1474 void varChanged(UndetVar uv, Set<InferenceBound> ibs);
mcimadamore@1338 1475 }
mcimadamore@1338 1476
mcimadamore@1338 1477 /**
mcimadamore@1338 1478 * Inference variable bound kinds
mcimadamore@1338 1479 */
mcimadamore@1338 1480 public enum InferenceBound {
dlsmith@2384 1481 UPPER {
dlsmith@2384 1482 public InferenceBound complement() { return LOWER; }
dlsmith@2384 1483 },
dlsmith@2384 1484 /** lower bounds */
dlsmith@2384 1485 LOWER {
dlsmith@2384 1486 public InferenceBound complement() { return UPPER; }
dlsmith@2384 1487 },
dlsmith@2384 1488 /** equality constraints */
dlsmith@2384 1489 EQ {
dlsmith@2384 1490 public InferenceBound complement() { return EQ; }
dlsmith@2384 1491 };
dlsmith@2384 1492
dlsmith@2384 1493 public abstract InferenceBound complement();
mcimadamore@1338 1494 }
mcimadamore@1338 1495
mcimadamore@1338 1496 /** inference variable bounds */
mcimadamore@1898 1497 protected Map<InferenceBound, List<Type>> bounds;
mcimadamore@1338 1498
mcimadamore@1338 1499 /** inference variable's inferred type (set from Infer.java) */
duke@1 1500 public Type inst = null;
duke@1 1501
mcimadamore@1550 1502 /** number of declared (upper) bounds */
mcimadamore@1550 1503 public int declaredCount;
mcimadamore@1550 1504
mcimadamore@1338 1505 /** inference variable's change listener */
mcimadamore@1338 1506 public UndetVarListener listener = null;
mcimadamore@1338 1507
duke@1 1508 @Override
duke@1 1509 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1510 return v.visitUndetVar(this, s);
duke@1 1511 }
duke@1 1512
mcimadamore@1338 1513 public UndetVar(TypeVar origin, Types types) {
duke@1 1514 super(UNDETVAR, origin);
mcimadamore@1338 1515 bounds = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
mcimadamore@1550 1516 List<Type> declaredBounds = types.getBounds(origin);
mcimadamore@1550 1517 declaredCount = declaredBounds.length();
mcimadamore@1550 1518 bounds.put(InferenceBound.UPPER, declaredBounds);
mcimadamore@1338 1519 bounds.put(InferenceBound.LOWER, List.<Type>nil());
mcimadamore@1338 1520 bounds.put(InferenceBound.EQ, List.<Type>nil());
duke@1 1521 }
duke@1 1522
duke@1 1523 public String toString() {
vromero@2302 1524 return (inst == null) ? qtype + "?" : inst.toString();
vromero@2302 1525 }
vromero@2302 1526
vromero@2302 1527 public String debugString() {
vromero@2302 1528 String result = "inference var = " + qtype + "\n";
vromero@2302 1529 if (inst != null) {
vromero@2302 1530 result += "inst = " + inst + '\n';
vromero@2302 1531 }
vromero@2302 1532 for (InferenceBound bound: InferenceBound.values()) {
vromero@2302 1533 List<Type> aboundList = bounds.get(bound);
vromero@2302 1534 if (aboundList.size() > 0) {
vromero@2302 1535 result += bound + " = " + aboundList + '\n';
vromero@2302 1536 }
vromero@2302 1537 }
vromero@2302 1538 return result;
duke@1 1539 }
duke@1 1540
vromero@1853 1541 @Override
vromero@1853 1542 public boolean isPartial() {
vromero@1853 1543 return true;
vromero@1853 1544 }
vromero@1853 1545
vromero@1853 1546 @Override
duke@1 1547 public Type baseType() {
vromero@2302 1548 return (inst == null) ? this : inst.baseType();
duke@1 1549 }
mcimadamore@1338 1550
mcimadamore@1338 1551 /** get all bounds of a given kind */
mcimadamore@1550 1552 public List<Type> getBounds(InferenceBound... ibs) {
alundblad@2047 1553 ListBuffer<Type> buf = new ListBuffer<>();
mcimadamore@1550 1554 for (InferenceBound ib : ibs) {
mcimadamore@1550 1555 buf.appendList(bounds.get(ib));
mcimadamore@1550 1556 }
mcimadamore@1550 1557 return buf.toList();
mcimadamore@1550 1558 }
mcimadamore@1550 1559
mcimadamore@1550 1560 /** get the list of declared (upper) bounds */
mcimadamore@1550 1561 public List<Type> getDeclaredBounds() {
alundblad@2047 1562 ListBuffer<Type> buf = new ListBuffer<>();
mcimadamore@1550 1563 int count = 0;
mcimadamore@1550 1564 for (Type b : getBounds(InferenceBound.UPPER)) {
mcimadamore@1550 1565 if (count++ == declaredCount) break;
mcimadamore@1550 1566 buf.append(b);
mcimadamore@1550 1567 }
mcimadamore@1550 1568 return buf.toList();
mcimadamore@1338 1569 }
mcimadamore@1338 1570
mcimadamore@1891 1571 /** internal method used to override an undetvar bounds */
mcimadamore@1891 1572 public void setBounds(InferenceBound ib, List<Type> newBounds) {
mcimadamore@1891 1573 bounds.put(ib, newBounds);
mcimadamore@1891 1574 }
mcimadamore@1891 1575
mcimadamore@1338 1576 /** add a bound of a given kind - this might trigger listener notification */
mcimadamore@1898 1577 public final void addBound(InferenceBound ib, Type bound, Types types) {
mcimadamore@1898 1578 addBound(ib, bound, types, false);
mcimadamore@1898 1579 }
mcimadamore@1898 1580
mcimadamore@1898 1581 protected void addBound(InferenceBound ib, Type bound, Types types, boolean update) {
mcimadamore@1946 1582 Type bound2 = toTypeVarMap.apply(bound).baseType();
mcimadamore@1338 1583 List<Type> prevBounds = bounds.get(ib);
mcimadamore@1338 1584 for (Type b : prevBounds) {
mcimadamore@1550 1585 //check for redundancy - use strict version of isSameType on tvars
mcimadamore@1550 1586 //(as the standard version will lead to false positives w.r.t. clones ivars)
mcimadamore@1628 1587 if (types.isSameType(b, bound2, true) || bound == qtype) return;
mcimadamore@1338 1588 }
mcimadamore@1550 1589 bounds.put(ib, prevBounds.prepend(bound2));
mcimadamore@1338 1590 notifyChange(EnumSet.of(ib));
mcimadamore@1338 1591 }
mcimadamore@1550 1592 //where
mcimadamore@1905 1593 Type.Mapping toTypeVarMap = new Mapping("toTypeVarMap") {
mcimadamore@1550 1594 @Override
mcimadamore@1550 1595 public Type apply(Type t) {
mcimadamore@1550 1596 if (t.hasTag(UNDETVAR)) {
mcimadamore@1550 1597 UndetVar uv = (UndetVar)t;
mcimadamore@1891 1598 return uv.inst != null ? uv.inst : uv.qtype;
mcimadamore@1550 1599 } else {
mcimadamore@1550 1600 return t.map(this);
mcimadamore@1550 1601 }
mcimadamore@1550 1602 }
mcimadamore@1550 1603 };
mcimadamore@1338 1604
mcimadamore@1338 1605 /** replace types in all bounds - this might trigger listener notification */
mcimadamore@1338 1606 public void substBounds(List<Type> from, List<Type> to, Types types) {
mcimadamore@1550 1607 List<Type> instVars = from.diff(to);
mcimadamore@1550 1608 //if set of instantiated ivars is empty, there's nothing to do!
mcimadamore@1550 1609 if (instVars.isEmpty()) return;
mcimadamore@1550 1610 final EnumSet<InferenceBound> boundsChanged = EnumSet.noneOf(InferenceBound.class);
mcimadamore@1550 1611 UndetVarListener prevListener = listener;
mcimadamore@1550 1612 try {
mcimadamore@1550 1613 //setup new listener for keeping track of changed bounds
mcimadamore@1550 1614 listener = new UndetVarListener() {
mcimadamore@1550 1615 public void varChanged(UndetVar uv, Set<InferenceBound> ibs) {
mcimadamore@1550 1616 boundsChanged.addAll(ibs);
mcimadamore@1550 1617 }
mcimadamore@1550 1618 };
mcimadamore@1550 1619 for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) {
mcimadamore@1550 1620 InferenceBound ib = _entry.getKey();
mcimadamore@1550 1621 List<Type> prevBounds = _entry.getValue();
alundblad@2047 1622 ListBuffer<Type> newBounds = new ListBuffer<>();
alundblad@2047 1623 ListBuffer<Type> deps = new ListBuffer<>();
mcimadamore@1550 1624 //step 1 - re-add bounds that are not dependent on ivars
mcimadamore@1550 1625 for (Type t : prevBounds) {
mcimadamore@1550 1626 if (!t.containsAny(instVars)) {
mcimadamore@1550 1627 newBounds.append(t);
mcimadamore@1550 1628 } else {
mcimadamore@1550 1629 deps.append(t);
mcimadamore@1550 1630 }
mcimadamore@1550 1631 }
mcimadamore@1550 1632 //step 2 - replace bounds
mcimadamore@1550 1633 bounds.put(ib, newBounds.toList());
mcimadamore@1550 1634 //step 3 - for each dependency, add new replaced bound
mcimadamore@1550 1635 for (Type dep : deps) {
mcimadamore@1898 1636 addBound(ib, types.subst(dep, from, to), types, true);
mcimadamore@1550 1637 }
mcimadamore@1338 1638 }
mcimadamore@1550 1639 } finally {
mcimadamore@1550 1640 listener = prevListener;
mcimadamore@1550 1641 if (!boundsChanged.isEmpty()) {
mcimadamore@1550 1642 notifyChange(boundsChanged);
mcimadamore@1550 1643 }
mcimadamore@1338 1644 }
mcimadamore@1338 1645 }
mcimadamore@1338 1646
mcimadamore@1338 1647 private void notifyChange(EnumSet<InferenceBound> ibs) {
mcimadamore@1338 1648 if (listener != null) {
mcimadamore@1338 1649 listener.varChanged(this, ibs);
mcimadamore@1338 1650 }
mcimadamore@1338 1651 }
mcimadamore@1898 1652
mcimadamore@1898 1653 public boolean isCaptured() {
mcimadamore@1898 1654 return false;
mcimadamore@1898 1655 }
mcimadamore@1898 1656 }
mcimadamore@1898 1657
mcimadamore@1898 1658 /**
mcimadamore@1898 1659 * This class is used to represent synthetic captured inference variables
mcimadamore@1898 1660 * that can be generated during nested generic method calls. The only difference
mcimadamore@1898 1661 * between these inference variables and ordinary ones is that captured inference
mcimadamore@1898 1662 * variables cannot get new bounds through incorporation.
mcimadamore@1898 1663 */
mcimadamore@1898 1664 public static class CapturedUndetVar extends UndetVar {
mcimadamore@1898 1665
mcimadamore@1898 1666 public CapturedUndetVar(CapturedType origin, Types types) {
mcimadamore@1898 1667 super(origin, types);
mcimadamore@1898 1668 if (!origin.lower.hasTag(BOT)) {
mcimadamore@1898 1669 bounds.put(InferenceBound.LOWER, List.of(origin.lower));
mcimadamore@1898 1670 }
mcimadamore@1898 1671 }
mcimadamore@1898 1672
mcimadamore@1898 1673 @Override
mcimadamore@1898 1674 public void addBound(InferenceBound ib, Type bound, Types types, boolean update) {
mcimadamore@1898 1675 if (update) {
mcimadamore@1898 1676 //only change bounds if request comes from substBounds
mcimadamore@1898 1677 super.addBound(ib, bound, types, update);
mcimadamore@1898 1678 }
mcimadamore@1898 1679 }
mcimadamore@1898 1680
mcimadamore@1898 1681 @Override
mcimadamore@1898 1682 public boolean isCaptured() {
mcimadamore@1898 1683 return true;
mcimadamore@1898 1684 }
duke@1 1685 }
duke@1 1686
vromero@1853 1687 /** Represents NONE.
duke@1 1688 */
vromero@1853 1689 public static class JCNoType extends Type implements NoType {
vromero@1853 1690 public JCNoType() {
vromero@1853 1691 super(null);
vromero@1853 1692 }
vromero@1853 1693
vromero@1853 1694 @Override
vromero@1853 1695 public TypeTag getTag() {
vromero@1853 1696 return NONE;
duke@1 1697 }
duke@1 1698
duke@1 1699 @Override
duke@1 1700 public TypeKind getKind() {
vromero@1853 1701 return TypeKind.NONE;
duke@1 1702 }
duke@1 1703
duke@1 1704 @Override
duke@1 1705 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1706 return v.visitNoType(this, p);
duke@1 1707 }
emc@2077 1708
emc@2077 1709 @Override
emc@2077 1710 public boolean isCompound() { return false; }
duke@1 1711 }
duke@1 1712
vromero@1853 1713 /** Represents VOID.
vromero@1853 1714 */
vromero@1853 1715 public static class JCVoidType extends Type implements NoType {
vromero@1853 1716
vromero@1853 1717 public JCVoidType() {
vromero@1853 1718 super(null);
vromero@1853 1719 }
vromero@1853 1720
vromero@1853 1721 @Override
vromero@1853 1722 public TypeTag getTag() {
vromero@1853 1723 return VOID;
vromero@1853 1724 }
vromero@1853 1725
vromero@1853 1726 @Override
vromero@1853 1727 public TypeKind getKind() {
vromero@1853 1728 return TypeKind.VOID;
vromero@1853 1729 }
vromero@1853 1730
vromero@1853 1731 @Override
emc@2077 1732 public boolean isCompound() { return false; }
emc@2077 1733
emc@2077 1734 @Override
vromero@1853 1735 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
vromero@1853 1736 return v.visitNoType(this, p);
vromero@1853 1737 }
vromero@1853 1738
vromero@1853 1739 @Override
vromero@1853 1740 public boolean isPrimitiveOrVoid() {
vromero@1853 1741 return true;
vromero@1853 1742 }
vromero@1853 1743 }
vromero@1853 1744
duke@1 1745 static class BottomType extends Type implements NullType {
duke@1 1746 public BottomType() {
vromero@1853 1747 super(null);
vromero@1853 1748 }
vromero@1853 1749
vromero@1853 1750 @Override
vromero@1853 1751 public TypeTag getTag() {
vromero@1853 1752 return BOT;
duke@1 1753 }
duke@1 1754
duke@1 1755 @Override
duke@1 1756 public TypeKind getKind() {
duke@1 1757 return TypeKind.NULL;
duke@1 1758 }
duke@1 1759
duke@1 1760 @Override
emc@2077 1761 public boolean isCompound() { return false; }
emc@2077 1762
emc@2077 1763 @Override
duke@1 1764 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1765 return v.visitNull(this, p);
duke@1 1766 }
duke@1 1767
duke@1 1768 @Override
duke@1 1769 public Type constType(Object value) {
duke@1 1770 return this;
duke@1 1771 }
duke@1 1772
duke@1 1773 @Override
duke@1 1774 public String stringValue() {
duke@1 1775 return "null";
duke@1 1776 }
vromero@1853 1777
vromero@1853 1778 @Override
vromero@1853 1779 public boolean isNullOrReference() {
vromero@1853 1780 return true;
vromero@1853 1781 }
vromero@1853 1782
duke@1 1783 }
duke@1 1784
duke@1 1785 public static class ErrorType extends ClassType
duke@1 1786 implements javax.lang.model.type.ErrorType {
duke@1 1787
jjg@110 1788 private Type originalType = null;
jjg@110 1789
jjg@110 1790 public ErrorType(Type originalType, TypeSymbol tsym) {
duke@1 1791 super(noType, List.<Type>nil(), null);
jjg@110 1792 this.tsym = tsym;
jjg@110 1793 this.originalType = (originalType == null ? noType : originalType);
duke@1 1794 }
duke@1 1795
jjg@110 1796 public ErrorType(ClassSymbol c, Type originalType) {
jjg@110 1797 this(originalType, c);
duke@1 1798 c.type = this;
duke@1 1799 c.kind = ERR;
duke@1 1800 c.members_field = new Scope.ErrorScope(c);
duke@1 1801 }
duke@1 1802
vromero@1853 1803 @Override
vromero@1853 1804 public TypeTag getTag() {
vromero@1853 1805 return ERROR;
vromero@1853 1806 }
vromero@1853 1807
vromero@1853 1808 @Override
vromero@1853 1809 public boolean isPartial() {
vromero@1853 1810 return true;
vromero@1853 1811 }
vromero@1853 1812
vromero@1853 1813 @Override
vromero@1853 1814 public boolean isReference() {
vromero@1853 1815 return true;
vromero@1853 1816 }
vromero@1853 1817
vromero@1853 1818 @Override
vromero@1853 1819 public boolean isNullOrReference() {
vromero@1853 1820 return true;
vromero@1853 1821 }
vromero@1853 1822
jjg@110 1823 public ErrorType(Name name, TypeSymbol container, Type originalType) {
jjg@110 1824 this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
duke@1 1825 }
duke@1 1826
duke@1 1827 @Override
duke@1 1828 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
duke@1 1829 return v.visitErrorType(this, s);
duke@1 1830 }
duke@1 1831
duke@1 1832 public Type constType(Object constValue) { return this; }
jjg@1521 1833 public Type getEnclosingType() { return this; }
duke@1 1834 public Type getReturnType() { return this; }
duke@1 1835 public Type asSub(Symbol sym) { return this; }
duke@1 1836 public Type map(Mapping f) { return this; }
duke@1 1837
duke@1 1838 public boolean isGenType(Type t) { return true; }
duke@1 1839 public boolean isErroneous() { return true; }
duke@1 1840 public boolean isCompound() { return false; }
duke@1 1841 public boolean isInterface() { return false; }
duke@1 1842
duke@1 1843 public List<Type> allparams() { return List.nil(); }
duke@1 1844 public List<Type> getTypeArguments() { return List.nil(); }
duke@1 1845
duke@1 1846 public TypeKind getKind() {
duke@1 1847 return TypeKind.ERROR;
duke@1 1848 }
duke@1 1849
jjg@110 1850 public Type getOriginalType() {
jjg@110 1851 return originalType;
jjg@110 1852 }
jjg@110 1853
duke@1 1854 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
duke@1 1855 return v.visitError(this, p);
duke@1 1856 }
duke@1 1857 }
duke@1 1858
jjg@1521 1859 public static class AnnotatedType extends Type
jjg@1644 1860 implements
jjg@1644 1861 javax.lang.model.type.ArrayType,
jjg@1644 1862 javax.lang.model.type.DeclaredType,
jjg@1644 1863 javax.lang.model.type.PrimitiveType,
jjg@1644 1864 javax.lang.model.type.TypeVariable,
jjg@1644 1865 javax.lang.model.type.WildcardType {
jjg@1521 1866 /** The type annotations on this type.
jjg@1521 1867 */
jjg@2134 1868 private List<Attribute.TypeCompound> typeAnnotations;
jjg@1521 1869
jjg@1521 1870 /** The underlying type that is annotated.
jjg@1521 1871 */
jjg@2134 1872 private Type underlyingType;
jjg@1521 1873
jjg@2134 1874 protected AnnotatedType(List<Attribute.TypeCompound> typeAnnotations,
jjg@1521 1875 Type underlyingType) {
vromero@1853 1876 super(underlyingType.tsym);
jjg@1521 1877 this.typeAnnotations = typeAnnotations;
jjg@1521 1878 this.underlyingType = underlyingType;
jjg@2134 1879 Assert.check(typeAnnotations != null && typeAnnotations.nonEmpty(),
jjg@2134 1880 "Can't create AnnotatedType without annotations: " + underlyingType);
jjg@1644 1881 Assert.check(!underlyingType.isAnnotated(),
jjg@1521 1882 "Can't annotate already annotated type: " + underlyingType +
jjg@1521 1883 "; adding: " + typeAnnotations);
jjg@1521 1884 }
jjg@1521 1885
jjg@1521 1886 @Override
vromero@1853 1887 public TypeTag getTag() {
vromero@1853 1888 return underlyingType.getTag();
vromero@1853 1889 }
vromero@1853 1890
vromero@1853 1891 @Override
jjg@1644 1892 public boolean isAnnotated() {
jjg@1644 1893 return true;
jjg@1644 1894 }
jjg@1644 1895
jjg@1644 1896 @Override
alundblad@2100 1897 public List<Attribute.TypeCompound> getAnnotationMirrors() {
jjg@1645 1898 return typeAnnotations;
jjg@1521 1899 }
jjg@1521 1900
jjg@1645 1901
jjg@1645 1902 @Override
jjg@1645 1903 public TypeKind getKind() {
jjg@1645 1904 return underlyingType.getKind();
jjg@1521 1905 }
jjg@1521 1906
jjg@1521 1907 @Override
jjg@1521 1908 public Type unannotatedType() {
jjg@1521 1909 return underlyingType;
jjg@1521 1910 }
jjg@1521 1911
jjg@1521 1912 @Override
jjg@1521 1913 public <R,S> R accept(Type.Visitor<R,S> v, S s) {
jjg@1521 1914 return v.visitAnnotatedType(this, s);
jjg@1521 1915 }
jjg@1521 1916
jjg@1521 1917 @Override
jjg@1521 1918 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
jjg@1644 1919 return underlyingType.accept(v, p);
jjg@1521 1920 }
jjg@1521 1921
jjg@1521 1922 @Override
jjg@1521 1923 public Type map(Mapping f) {
jjg@1521 1924 underlyingType.map(f);
jjg@1521 1925 return this;
jjg@1521 1926 }
jjg@1521 1927
jjg@1521 1928 @Override
jjg@1521 1929 public Type constType(Object constValue) { return underlyingType.constType(constValue); }
jjg@1521 1930 @Override
jjg@1521 1931 public Type getEnclosingType() { return underlyingType.getEnclosingType(); }
jjg@1521 1932
jjg@1521 1933 @Override
jjg@1521 1934 public Type getReturnType() { return underlyingType.getReturnType(); }
jjg@1521 1935 @Override
jjg@1521 1936 public List<Type> getTypeArguments() { return underlyingType.getTypeArguments(); }
jjg@1521 1937 @Override
jjg@1521 1938 public List<Type> getParameterTypes() { return underlyingType.getParameterTypes(); }
jjg@1521 1939 @Override
jjg@1521 1940 public Type getReceiverType() { return underlyingType.getReceiverType(); }
jjg@1521 1941 @Override
jjg@1521 1942 public List<Type> getThrownTypes() { return underlyingType.getThrownTypes(); }
jjg@1521 1943 @Override
jjg@1521 1944 public Type getUpperBound() { return underlyingType.getUpperBound(); }
jjg@1521 1945 @Override
jjg@1521 1946 public Type getLowerBound() { return underlyingType.getLowerBound(); }
jjg@1521 1947
jjg@1521 1948 @Override
jjg@1521 1949 public boolean isErroneous() { return underlyingType.isErroneous(); }
jjg@1521 1950 @Override
jjg@1521 1951 public boolean isCompound() { return underlyingType.isCompound(); }
jjg@1521 1952 @Override
jjg@1521 1953 public boolean isInterface() { return underlyingType.isInterface(); }
jjg@1521 1954 @Override
jjg@1521 1955 public List<Type> allparams() { return underlyingType.allparams(); }
jjg@1521 1956 @Override
vromero@1853 1957 public boolean isPrimitive() { return underlyingType.isPrimitive(); }
vromero@1853 1958 @Override
vromero@1853 1959 public boolean isPrimitiveOrVoid() { return underlyingType.isPrimitiveOrVoid(); }
vromero@1853 1960 @Override
jjg@1521 1961 public boolean isNumeric() { return underlyingType.isNumeric(); }
jjg@1521 1962 @Override
jjg@1521 1963 public boolean isReference() { return underlyingType.isReference(); }
jjg@1521 1964 @Override
vromero@1853 1965 public boolean isNullOrReference() { return underlyingType.isNullOrReference(); }
vromero@1853 1966 @Override
vromero@1853 1967 public boolean isPartial() { return underlyingType.isPartial(); }
vromero@1853 1968 @Override
jjg@1521 1969 public boolean isParameterized() { return underlyingType.isParameterized(); }
jjg@1521 1970 @Override
jjg@1521 1971 public boolean isRaw() { return underlyingType.isRaw(); }
jjg@1521 1972 @Override
jjg@1521 1973 public boolean isFinal() { return underlyingType.isFinal(); }
jjg@1521 1974 @Override
jjg@1521 1975 public boolean isSuperBound() { return underlyingType.isSuperBound(); }
jjg@1521 1976 @Override
jjg@1521 1977 public boolean isExtendsBound() { return underlyingType.isExtendsBound(); }
jjg@1521 1978 @Override
jjg@1521 1979 public boolean isUnbound() { return underlyingType.isUnbound(); }
jjg@1521 1980
jjg@1521 1981 @Override
jjg@1521 1982 public String toString() {
jjg@1755 1983 // This method is only used for internal debugging output.
jjg@1755 1984 // See
jjg@1755 1985 // com.sun.tools.javac.code.Printer.visitAnnotatedType(AnnotatedType, Locale)
jjg@1755 1986 // for the user-visible logic.
jjg@1521 1987 if (typeAnnotations != null &&
jjg@1521 1988 !typeAnnotations.isEmpty()) {
jjg@1521 1989 return "(" + typeAnnotations.toString() + " :: " + underlyingType.toString() + ")";
jjg@1521 1990 } else {
jjg@1521 1991 return "({} :: " + underlyingType.toString() +")";
jjg@1521 1992 }
jjg@1521 1993 }
jjg@1521 1994
jjg@1521 1995 @Override
jjg@1521 1996 public boolean contains(Type t) { return underlyingType.contains(t); }
jjg@1521 1997
jjg@1521 1998 @Override
jjg@1755 1999 public Type withTypeVar(Type t) {
jjg@1755 2000 // Don't create a new AnnotatedType, as 'this' will
jjg@1755 2001 // get its annotations set later.
jjg@1755 2002 underlyingType = underlyingType.withTypeVar(t);
jjg@1755 2003 return this;
jjg@1755 2004 }
jjg@1521 2005
jjg@1521 2006 // TODO: attach annotations?
jjg@1521 2007 @Override
jjg@1521 2008 public TypeSymbol asElement() { return underlyingType.asElement(); }
jjg@1521 2009
jjg@1521 2010 // TODO: attach annotations?
jjg@1521 2011 @Override
jjg@1521 2012 public MethodType asMethodType() { return underlyingType.asMethodType(); }
jjg@1521 2013
jjg@1521 2014 @Override
jjg@1521 2015 public void complete() { underlyingType.complete(); }
jjg@1521 2016
jjg@1521 2017 @Override
jjg@1521 2018 public TypeMirror getComponentType() { return ((ArrayType)underlyingType).getComponentType(); }
jjg@1521 2019
jjg@1521 2020 // The result is an ArrayType, but only in the model sense, not the Type sense.
jjg@2134 2021 public Type makeVarargs() {
jjg@2134 2022 return ((ArrayType) underlyingType).makeVarargs().annotatedType(typeAnnotations);
jjg@1521 2023 }
jjg@1521 2024
jjg@1521 2025 @Override
jjg@1521 2026 public TypeMirror getExtendsBound() { return ((WildcardType)underlyingType).getExtendsBound(); }
jjg@1521 2027 @Override
jjg@1521 2028 public TypeMirror getSuperBound() { return ((WildcardType)underlyingType).getSuperBound(); }
jjg@1521 2029 }
jjg@1521 2030
vromero@1853 2031 public static class UnknownType extends Type {
vromero@1853 2032
vromero@1853 2033 public UnknownType() {
vromero@1853 2034 super(null);
vromero@1853 2035 }
vromero@1853 2036
vromero@1853 2037 @Override
vromero@1853 2038 public TypeTag getTag() {
vromero@1853 2039 return UNKNOWN;
vromero@1853 2040 }
vromero@1853 2041
vromero@1853 2042 @Override
vromero@1853 2043 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
vromero@1853 2044 return v.visitUnknown(this, p);
vromero@1853 2045 }
vromero@1853 2046
vromero@1853 2047 @Override
vromero@1853 2048 public boolean isPartial() {
vromero@1853 2049 return true;
vromero@1853 2050 }
vromero@1853 2051 }
vromero@1853 2052
duke@1 2053 /**
duke@1 2054 * A visitor for types. A visitor is used to implement operations
duke@1 2055 * (or relations) on types. Most common operations on types are
duke@1 2056 * binary relations and this interface is designed for binary
jjg@1521 2057 * relations, that is, operations of the form
duke@1 2058 * Type&nbsp;&times;&nbsp;S&nbsp;&rarr;&nbsp;R.
duke@1 2059 * <!-- In plain text: Type x S -> R -->
duke@1 2060 *
duke@1 2061 * @param <R> the return type of the operation implemented by this
duke@1 2062 * visitor; use Void if no return type is needed.
duke@1 2063 * @param <S> the type of the second argument (the first being the
duke@1 2064 * type itself) of the operation implemented by this visitor; use
duke@1 2065 * Void if a second argument is not needed.
duke@1 2066 */
duke@1 2067 public interface Visitor<R,S> {
duke@1 2068 R visitClassType(ClassType t, S s);
duke@1 2069 R visitWildcardType(WildcardType t, S s);
duke@1 2070 R visitArrayType(ArrayType t, S s);
duke@1 2071 R visitMethodType(MethodType t, S s);
duke@1 2072 R visitPackageType(PackageType t, S s);
duke@1 2073 R visitTypeVar(TypeVar t, S s);
duke@1 2074 R visitCapturedType(CapturedType t, S s);
duke@1 2075 R visitForAll(ForAll t, S s);
duke@1 2076 R visitUndetVar(UndetVar t, S s);
duke@1 2077 R visitErrorType(ErrorType t, S s);
jjg@1521 2078 R visitAnnotatedType(AnnotatedType t, S s);
duke@1 2079 R visitType(Type t, S s);
duke@1 2080 }
duke@1 2081 }

mercurial