1 /*
   2  * Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.code;
  27 
  28 import java.lang.annotation.Annotation;
  29 import java.util.ArrayDeque;
  30 import java.util.Collections;
  31 import java.util.EnumMap;
  32 import java.util.Map;
  33 import java.util.function.Predicate;
  34 
  35 import javax.lang.model.type.*;
  36 
  37 import com.sun.tools.javac.code.Symbol.*;
  38 import com.sun.tools.javac.code.Type.ClassType.Flavor;
  39 import com.sun.tools.javac.code.TypeMetadata.Entry;
  40 import com.sun.tools.javac.code.Types.TypeMapping;
  41 import com.sun.tools.javac.code.Types.UniqueType;
  42 import com.sun.tools.javac.comp.Infer.IncorporationAction;
  43 import com.sun.tools.javac.jvm.ClassFile;
  44 import com.sun.tools.javac.jvm.PoolConstant;
  45 import com.sun.tools.javac.util.*;
  46 import com.sun.tools.javac.util.DefinedBy.Api;
  47 
  48 import static com.sun.tools.javac.code.BoundKind.*;
  49 import static com.sun.tools.javac.code.Flags.*;
  50 import static com.sun.tools.javac.code.Kinds.Kind.*;
  51 import static com.sun.tools.javac.code.TypeTag.*;
  52 
  53 /** This class represents Java types. The class itself defines the behavior of
  54  *  the following types:
  55  *  <pre>
  56  *  base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
  57  *  type `void' (tag: VOID),
  58  *  the bottom type (tag: BOT),
  59  *  the missing type (tag: NONE).
  60  *  </pre>
  61  *  <p>The behavior of the following types is defined in subclasses, which are
  62  *  all static inner classes of this class:
  63  *  <pre>
  64  *  class types (tag: CLASS, class: ClassType),
  65  *  array types (tag: ARRAY, class: ArrayType),
  66  *  method types (tag: METHOD, class: MethodType),
  67  *  package types (tag: PACKAGE, class: PackageType),
  68  *  type variables (tag: TYPEVAR, class: TypeVar),
  69  *  type arguments (tag: WILDCARD, class: WildcardType),
  70  *  generic method types (tag: FORALL, class: ForAll),
  71  *  the error type (tag: ERROR, class: ErrorType).
  72  *  </pre>
  73  *
  74  *  <p><b>This is NOT part of any supported API.
  75  *  If you write code that depends on this, you do so at your own risk.
  76  *  This code and its internal interfaces are subject to change or
  77  *  deletion without notice.</b>
  78  *
  79  *  @see TypeTag
  80  */
  81 public abstract class Type extends AnnoConstruct implements TypeMirror, PoolConstant {
  82 
  83     /**
  84      * Type metadata,  Should be {@code null} for the default value.
  85      *
  86      * Note: it is an invariant that for any {@code TypeMetadata}
  87      * class, a given {@code Type} may have at most one metadata array
  88      * entry of that class.
  89      */
  90     protected final TypeMetadata metadata;
  91 
  92     public TypeMetadata getMetadata() {
  93         return metadata;
  94     }
  95 
  96     public Entry getMetadataOfKind(final Entry.Kind kind) {
  97         return metadata != null ? metadata.get(kind) : null;
  98     }
  99 
 100     /** Constant type: no type at all. */
 101     public static final JCNoType noType = new JCNoType() {
 102         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 103         public String toString() {
 104             return "none";
 105         }
 106     };
 107 
 108     /** Constant type: special type to be used during recovery of deferred expressions. */
 109     public static final JCNoType recoveryType = new JCNoType(){
 110         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 111         public String toString() {
 112             return "recovery";
 113         }
 114     };
 115 
 116     /** Constant type: special type to be used for marking stuck trees. */
 117     public static final JCNoType stuckType = new JCNoType() {
 118         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 119         public String toString() {
 120             return "stuck";
 121         }
 122     };
 123 
 124     /** If this switch is turned on, the names of type variables
 125      *  and anonymous classes are printed with hashcodes appended.
 126      */
 127     public static boolean moreInfo = false;
 128 
 129     /** The defining class / interface / package / type variable.
 130      */
 131     public TypeSymbol tsym;
 132 
 133     @Override
 134     public int poolTag() {
 135         throw new AssertionError("Invalid pool entry");
 136     }
 137 
 138     @Override
 139     public Object poolKey(Types types) {
 140         return new UniqueType(this, types);
 141     }
 142 
 143     /**
 144      * Checks if the current type tag is equal to the given tag.
 145      * @return true if tag is equal to the current type tag.
 146      */
 147     public boolean hasTag(TypeTag tag) {
 148         return tag == getTag();
 149     }
 150 
 151     /**
 152      * Returns the current type tag.
 153      * @return the value of the current type tag.
 154      */
 155     public abstract TypeTag getTag();
 156 
 157     public boolean isNumeric() {
 158         return false;
 159     }
 160 
 161     public boolean isIntegral() {
 162         return false;
 163     }
 164 
 165     public boolean isPrimitive() {
 166         return false;
 167     }
 168 
 169     public boolean isPrimitiveOrVoid() {
 170         return false;
 171     }
 172 
 173     public boolean isReference() {
 174         return false;
 175     }
 176 
 177     public boolean isNullOrReference() {
 178         return false;
 179     }
 180 
 181     public boolean isPartial() {
 182         return false;
 183     }
 184 
 185     /**
 186      * The constant value of this type, null if this type does not
 187      * have a constant value attribute. Only primitive types and
 188      * strings (ClassType) can have a constant value attribute.
 189      * @return the constant value attribute of this type
 190      */
 191     public Object constValue() {
 192         return null;
 193     }
 194 
 195     /** Is this a constant type whose value is false?
 196      */
 197     public boolean isFalse() {
 198         return false;
 199     }
 200 
 201     /** Is this a constant type whose value is true?
 202      */
 203     public boolean isTrue() {
 204         return false;
 205     }
 206 
 207     /**
 208      * Get the representation of this type used for modelling purposes.
 209      * By default, this is itself. For ErrorType, a different value
 210      * may be provided.
 211      */
 212     public Type getModelType() {
 213         return this;
 214     }
 215 
 216     public static List<Type> getModelTypes(List<Type> ts) {
 217         ListBuffer<Type> lb = new ListBuffer<>();
 218         for (Type t: ts)
 219             lb.append(t.getModelType());
 220         return lb.toList();
 221     }
 222 
 223     /**For ErrorType, returns the original type, otherwise returns the type itself.
 224      */
 225     public Type getOriginalType() {
 226         return this;
 227     }
 228 
 229     public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
 230 
 231     /** Define a type given its tag, type symbol, and type annotations
 232      */
 233 
 234     public Type(TypeSymbol tsym, TypeMetadata metadata) {
 235         Assert.checkNonNull(metadata);
 236         this.tsym = tsym;
 237         this.metadata = metadata;
 238     }
 239 
 240     public boolean isPrimitiveClass() {
 241         return false;
 242     }
 243 
 244     public boolean isValueClass() {
 245         return false;
 246     }
 247 
 248     public boolean isValueInterface() {
 249         return false;
 250     }
 251 
 252     public boolean isIdentityClass() {
 253         return false;
 254     }
 255 
 256     public boolean isIdentityInterface() {
 257         return false;
 258     }
 259 
 260     // Does this type need to be preloaded in the context of the referring class ??
 261     public boolean requiresPreload(Symbol referringClass) {
 262         if (this.tsym == referringClass)
 263             return false; // pointless
 264         if (this.isReferenceProjection())
 265             return true;
 266         return this.isValueClass() && !this.isPrimitiveClass();
 267     }
 268 
 269     /**
 270      * Return the `flavor' associated with a ClassType.
 271      * @see ClassType.Flavor
 272      */
 273     public Flavor getFlavor() {
 274         throw new AssertionError("Unexpected call to getFlavor() on a Type that is not a ClassType: " + this);
 275     }
 276 
 277     /**
 278      * @return true IFF the receiver is a reference projection of a primitive class type and false
 279      * for primitives or plain references
 280      */
 281     public boolean isReferenceProjection() {
 282         return false;
 283     }
 284 
 285     /**
 286      * @return the value projection type IFF the receiver is a reference projection of a primitive class type
 287      * and null otherwise
 288      */
 289     public Type valueProjection() {
 290         return null;
 291     }
 292 
 293     /**
 294      * @return the reference projection type IFF the receiver is a primitive class type
 295      * and null otherwise
 296      */
 297     public Type referenceProjection() {
 298         return null;
 299     }
 300 
 301     /**
 302      * @return the reference projection type IFF the receiver is a primitive class type or self otherwise.
 303      */
 304     public Type referenceProjectionOrSelf() {
 305         Type projection = referenceProjection();
 306         return projection != null ? projection : this;
 307     }
 308 
 309     /**
 310      * A subclass of {@link Types.TypeMapping} which applies a mapping recursively to the subterms
 311      * of a given type expression. This mapping returns the original type is no changes occurred
 312      * when recursively mapping the original type's subterms.
 313      */
 314     public abstract static class StructuralTypeMapping<S> extends Types.TypeMapping<S> {
 315 
 316         @Override
 317         public Type visitClassType(ClassType t, S s) {
 318             Type outer = t.getEnclosingType();
 319             Type outer1 = visit(outer, s);
 320             List<Type> typarams = t.getTypeArguments();
 321             List<Type> typarams1 = visit(typarams, s);
 322             if (outer1 == outer && typarams1 == typarams) return t;
 323             else return new ClassType(outer1, typarams1, t.tsym, t.metadata, t.getFlavor()) {
 324                 @Override
 325                 protected boolean needsStripping() {
 326                     return true;
 327                 }
 328             };
 329         }
 330 
 331         @Override
 332         public Type visitWildcardType(WildcardType wt, S s) {
 333             Type t = wt.type;
 334             if (t != null)
 335                 t = visit(t, s);
 336             if (t == wt.type)
 337                 return wt;
 338             else
 339                 return new WildcardType(t, wt.kind, wt.tsym, wt.bound, wt.metadata) {
 340                     @Override
 341                     protected boolean needsStripping() {
 342                         return true;
 343                     }
 344                 };
 345         }
 346 
 347         @Override
 348         public Type visitArrayType(ArrayType t, S s) {
 349             Type elemtype = t.elemtype;
 350             Type elemtype1 = visit(elemtype, s);
 351             if (elemtype1 == elemtype) return t;
 352             else return new ArrayType(elemtype1, t.tsym, t.metadata) {
 353                 @Override
 354                 protected boolean needsStripping() {
 355                     return true;
 356                 }
 357             };
 358         }
 359 
 360         @Override
 361         public Type visitMethodType(MethodType t, S s) {
 362             List<Type> argtypes = t.argtypes;
 363             Type restype = t.restype;
 364             List<Type> thrown = t.thrown;
 365             List<Type> argtypes1 = visit(argtypes, s);
 366             Type restype1 = visit(restype, s);
 367             List<Type> thrown1 = visit(thrown, s);
 368             if (argtypes1 == argtypes &&
 369                 restype1 == restype &&
 370                 thrown1 == thrown) return t;
 371             else return new MethodType(argtypes1, restype1, thrown1, t.tsym) {
 372                 @Override
 373                 protected boolean needsStripping() {
 374                     return true;
 375                 }
 376             };
 377         }
 378 
 379         @Override
 380         public Type visitForAll(ForAll t, S s) {
 381             return visit(t.qtype, s);
 382         }
 383     }
 384 
 385     /** map a type function over all immediate descendants of this type
 386      */
 387     public <Z> Type map(TypeMapping<Z> mapping, Z arg) {
 388         return mapping.visit(this, arg);
 389     }
 390 
 391     /** map a type function over all immediate descendants of this type (no arg version)
 392      */
 393     public <Z> Type map(TypeMapping<Z> mapping) {
 394         return mapping.visit(this, null);
 395     }
 396 
 397     /** Define a constant type, of the same kind as this type
 398      *  and with given constant value
 399      */
 400     public Type constType(Object constValue) {
 401         throw new AssertionError();
 402     }
 403 
 404     /**
 405      * If this is a constant type, return its underlying type.
 406      * Otherwise, return the type itself.
 407      */
 408     public Type baseType() {
 409         return this;
 410     }
 411 
 412     /**
 413      * Returns the original version of this type, before metadata were added. This routine is meant
 414      * for internal use only (i.e. {@link Type#equalsIgnoreMetadata(Type)}, {@link Type#stripMetadata});
 415      * it should not be used outside this class.
 416      */
 417     protected Type typeNoMetadata() {
 418         return metadata == TypeMetadata.EMPTY ? this : baseType();
 419     }
 420 
 421     /**
 422      * Create a new copy of this type but with the specified TypeMetadata.
 423      */
 424     public abstract Type cloneWithMetadata(TypeMetadata metadata);
 425 
 426     /**
 427      * Does this type require annotation stripping for API clients?
 428      */
 429     protected boolean needsStripping() {
 430         return false;
 431     }
 432 
 433     /**
 434      * Strip all metadata associated with this type - this could return a new clone of the type.
 435      * This routine is only used to present the correct annotated types back to the users when types
 436      * are accessed through compiler APIs; it should not be used anywhere in the compiler internals
 437      * as doing so might result in performance penalties.
 438      */
 439     public Type stripMetadataIfNeeded() {
 440         return needsStripping() ?
 441                 accept(stripMetadata, null) :
 442                 this;
 443     }
 444 
 445     public Type stripMetadata() {
 446         return accept(stripMetadata, null);
 447     }
 448     //where
 449         private static final TypeMapping<Void> stripMetadata = new StructuralTypeMapping<Void>() {
 450             @Override
 451             public Type visitClassType(ClassType t, Void aVoid) {
 452                 return super.visitClassType((ClassType)t.typeNoMetadata(), aVoid);
 453             }
 454 
 455             @Override
 456             public Type visitArrayType(ArrayType t, Void aVoid) {
 457                 return super.visitArrayType((ArrayType)t.typeNoMetadata(), aVoid);
 458             }
 459 
 460             @Override
 461             public Type visitTypeVar(TypeVar t, Void aVoid) {
 462                 return super.visitTypeVar((TypeVar)t.typeNoMetadata(), aVoid);
 463             }
 464 
 465             @Override
 466             public Type visitWildcardType(WildcardType wt, Void aVoid) {
 467                 return super.visitWildcardType((WildcardType)wt.typeNoMetadata(), aVoid);
 468             }
 469         };
 470 
 471     public Type annotatedType(final List<Attribute.TypeCompound> annos) {
 472         final Entry annoMetadata = new TypeMetadata.Annotations(annos);
 473         return cloneWithMetadata(metadata.combine(annoMetadata));
 474     }
 475 
 476     public boolean isAnnotated() {
 477         final TypeMetadata.Annotations metadata =
 478             (TypeMetadata.Annotations)getMetadataOfKind(Entry.Kind.ANNOTATIONS);
 479 
 480         return null != metadata && !metadata.getAnnotations().isEmpty();
 481     }
 482 
 483     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 484     public List<Attribute.TypeCompound> getAnnotationMirrors() {
 485         final TypeMetadata.Annotations metadata =
 486             (TypeMetadata.Annotations)getMetadataOfKind(Entry.Kind.ANNOTATIONS);
 487 
 488         return metadata == null ? List.nil() : metadata.getAnnotations();
 489     }
 490 
 491 
 492     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 493     public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
 494         return null;
 495     }
 496 
 497 
 498     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 499     public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
 500         @SuppressWarnings("unchecked")
 501         A[] tmp = (A[]) java.lang.reflect.Array.newInstance(annotationType, 0);
 502         return tmp;
 503     }
 504 
 505     /** Return the base types of a list of types.
 506      */
 507     public static List<Type> baseTypes(List<Type> ts) {
 508         if (ts.nonEmpty()) {
 509             Type t = ts.head.baseType();
 510             List<Type> baseTypes = baseTypes(ts.tail);
 511             if (t != ts.head || baseTypes != ts.tail)
 512                 return baseTypes.prepend(t);
 513         }
 514         return ts;
 515     }
 516 
 517     protected void appendAnnotationsString(StringBuilder sb,
 518                                          boolean prefix) {
 519         if (isAnnotated()) {
 520             if (prefix) {
 521                 sb.append(" ");
 522             }
 523             sb.append(getAnnotationMirrors());
 524             sb.append(" ");
 525         }
 526     }
 527 
 528     protected void appendAnnotationsString(StringBuilder sb) {
 529         appendAnnotationsString(sb, false);
 530     }
 531 
 532     /** The Java source which this type represents.
 533      */
 534     @DefinedBy(Api.LANGUAGE_MODEL)
 535     public String toString() {
 536         StringBuilder sb = new StringBuilder();
 537         appendAnnotationsString(sb);
 538         if (tsym == null || tsym.name == null) {
 539             sb.append("<none>");
 540         } else {
 541             sb.append(tsym.name.toString());
 542         }
 543         if (moreInfo && hasTag(TYPEVAR)) {
 544             sb.append(hashCode());
 545         }
 546         return sb.toString();
 547     }
 548 
 549     /**
 550      * The Java source which this type list represents.  A List is
 551      * represented as a comma-separated listing of the elements in
 552      * that list.
 553      */
 554     public static String toString(List<Type> ts) {
 555         if (ts.isEmpty()) {
 556             return "";
 557         } else {
 558             StringBuilder buf = new StringBuilder();
 559             buf.append(ts.head.toString());
 560             for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
 561                 buf.append(",").append(l.head.toString());
 562             return buf.toString();
 563         }
 564     }
 565 
 566     /**
 567      * The constant value of this type, converted to String
 568      */
 569     public String stringValue() {
 570         Object cv = Assert.checkNonNull(constValue());
 571         return cv.toString();
 572     }
 573 
 574     /**
 575      * Override this method with care. For most Type instances this should behave as ==.
 576      */
 577     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 578     public boolean equals(Object t) {
 579         return this == t;
 580     }
 581 
 582     public boolean equalsIgnoreMetadata(Type t) {
 583         return typeNoMetadata().equals(t.typeNoMetadata());
 584     }
 585 
 586     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 587     public int hashCode() {
 588         return super.hashCode();
 589     }
 590 
 591     public String argtypes(boolean varargs) {
 592         List<Type> args = getParameterTypes();
 593         if (!varargs) return args.toString();
 594         StringBuilder buf = new StringBuilder();
 595         while (args.tail.nonEmpty()) {
 596             buf.append(args.head);
 597             args = args.tail;
 598             buf.append(',');
 599         }
 600         if (args.head.hasTag(ARRAY)) {
 601             buf.append(((ArrayType)args.head).elemtype);
 602             if (args.head.getAnnotationMirrors().nonEmpty()) {
 603                 buf.append(args.head.getAnnotationMirrors());
 604             }
 605             buf.append("...");
 606         } else {
 607             buf.append(args.head);
 608         }
 609         return buf.toString();
 610     }
 611 
 612     /** Access methods.
 613      */
 614     public List<Type>        getTypeArguments()  { return List.nil(); }
 615     public Type              getEnclosingType()  { return null; }
 616     public List<Type>        getParameterTypes() { return List.nil(); }
 617     public Type              getReturnType()     { return null; }
 618     public Type              getReceiverType()   { return null; }
 619     public List<Type>        getThrownTypes()    { return List.nil(); }
 620     public Type              getUpperBound()     { return null; }
 621     public Type              getLowerBound()     { return null; }
 622 
 623     /** Navigation methods, these will work for classes, type variables,
 624      *  foralls, but will return null for arrays and methods.
 625      */
 626 
 627    /** Return all parameters of this type and all its outer types in order
 628     *  outer (first) to inner (last).
 629     */
 630     public List<Type> allparams() { return List.nil(); }
 631 
 632     /** Does this type contain "error" elements?
 633      */
 634     public boolean isErroneous() {
 635         return false;
 636     }
 637 
 638     public static boolean isErroneous(List<Type> ts) {
 639         for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
 640             if (l.head.isErroneous()) return true;
 641         return false;
 642     }
 643 
 644     /** Is this type parameterized?
 645      *  A class type is parameterized if it has some parameters.
 646      *  An array type is parameterized if its element type is parameterized.
 647      *  All other types are not parameterized.
 648      */
 649     public boolean isParameterized() {
 650         return false;
 651     }
 652 
 653     /** Is this type a raw type?
 654      *  A class type is a raw type if it misses some of its parameters.
 655      *  An array type is a raw type if its element type is raw.
 656      *  All other types are not raw.
 657      *  Type validation will ensure that the only raw types
 658      *  in a program are types that miss all their type variables.
 659      */
 660     public boolean isRaw() {
 661         return false;
 662     }
 663 
 664     /**
 665      * A compound type is a special class type whose supertypes are used to store a list
 666      * of component types. There are two kinds of compound types: (i) intersection types
 667      * {@see IntersectionClassType} and (ii) union types {@see UnionClassType}.
 668      */
 669     public boolean isCompound() {
 670         return false;
 671     }
 672 
 673     public boolean isIntersection() {
 674         return false;
 675     }
 676 
 677     public boolean isUnion() {
 678         return false;
 679     }
 680 
 681     public boolean isInterface() {
 682         return (tsym.flags() & INTERFACE) != 0;
 683     }
 684 
 685     public boolean isFinal() {
 686         return (tsym.flags() & FINAL) != 0;
 687     }
 688 
 689     /**
 690      * Does this type contain occurrences of type t?
 691      */
 692     public boolean contains(Type t) {
 693         return t.equalsIgnoreMetadata(this);
 694     }
 695 
 696     public static boolean contains(List<Type> ts, Type t) {
 697         for (List<Type> l = ts;
 698              l.tail != null /*inlined: l.nonEmpty()*/;
 699              l = l.tail)
 700             if (l.head.contains(t)) return true;
 701         return false;
 702     }
 703 
 704     /** Does this type contain an occurrence of some type in 'ts'?
 705      */
 706     public boolean containsAny(List<Type> ts) {
 707         for (Type t : ts)
 708             if (this.contains(t)) return true;
 709         return false;
 710     }
 711 
 712     public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
 713         for (Type t : ts1)
 714             if (t.containsAny(ts2)) return true;
 715         return false;
 716     }
 717 
 718     public static List<Type> filter(List<Type> ts, Predicate<Type> tf) {
 719         ListBuffer<Type> buf = new ListBuffer<>();
 720         for (Type t : ts) {
 721             if (tf.test(t)) {
 722                 buf.append(t);
 723             }
 724         }
 725         return buf.toList();
 726     }
 727 
 728     public boolean isSuperBound() { return false; }
 729     public boolean isExtendsBound() { return false; }
 730     public boolean isUnbound() { return false; }
 731     public Type withTypeVar(Type t) { return this; }
 732 
 733     /** The underlying method type of this type.
 734      */
 735     public MethodType asMethodType() { throw new AssertionError(); }
 736 
 737     /** Complete loading all classes in this type.
 738      */
 739     public void complete() {}
 740 
 741     public TypeSymbol asElement() {
 742         return tsym;
 743     }
 744 
 745     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 746     public TypeKind getKind() {
 747         return TypeKind.OTHER;
 748     }
 749 
 750     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 751     public <R, P> R accept(TypeVisitor<R, P> v, P p) {
 752         throw new AssertionError();
 753     }
 754 
 755     public static class JCPrimitiveType extends Type
 756             implements javax.lang.model.type.PrimitiveType {
 757 
 758         TypeTag tag;
 759 
 760         public JCPrimitiveType(TypeTag tag, TypeSymbol tsym) {
 761             this(tag, tsym, TypeMetadata.EMPTY);
 762         }
 763 
 764         private JCPrimitiveType(TypeTag tag, TypeSymbol tsym, TypeMetadata metadata) {
 765             super(tsym, metadata);
 766             this.tag = tag;
 767             Assert.check(tag.isPrimitive);
 768         }
 769 
 770         @Override
 771         public JCPrimitiveType cloneWithMetadata(TypeMetadata md) {
 772             return new JCPrimitiveType(tag, tsym, md) {
 773                 @Override
 774                 public Type baseType() { return JCPrimitiveType.this.baseType(); }
 775             };
 776         }
 777 
 778         @Override
 779         public boolean isNumeric() {
 780             return tag != BOOLEAN;
 781         }
 782 
 783         @Override
 784         public boolean isIntegral() {
 785             switch (tag) {
 786                 case CHAR:
 787                 case BYTE:
 788                 case SHORT:
 789                 case INT:
 790                 case LONG:
 791                     return true;
 792                 default:
 793                     return false;
 794             }
 795         }
 796 
 797         @Override
 798         public boolean isPrimitive() {
 799             return true;
 800         }
 801 
 802         @Override
 803         public TypeTag getTag() {
 804             return tag;
 805         }
 806 
 807         @Override
 808         public boolean isPrimitiveOrVoid() {
 809             return true;
 810         }
 811 
 812         /** Define a constant type, of the same kind as this type
 813          *  and with given constant value
 814          */
 815         @Override
 816         public Type constType(Object constValue) {
 817             final Object value = constValue;
 818             return new JCPrimitiveType(tag, tsym, metadata) {
 819                     @Override
 820                     public Object constValue() {
 821                         return value;
 822                     }
 823                     @Override
 824                     public Type baseType() {
 825                         return tsym.type;
 826                     }
 827                 };
 828         }
 829 
 830         /**
 831          * The constant value of this type, converted to String
 832          */
 833         @Override
 834         public String stringValue() {
 835             Object cv = Assert.checkNonNull(constValue());
 836             if (tag == BOOLEAN) {
 837                 return ((Integer) cv).intValue() == 0 ? "false" : "true";
 838             }
 839             else if (tag == CHAR) {
 840                 return String.valueOf((char) ((Integer) cv).intValue());
 841             }
 842             else {
 843                 return cv.toString();
 844             }
 845         }
 846 
 847         /** Is this a constant type whose value is false?
 848          */
 849         @Override
 850         public boolean isFalse() {
 851             return
 852                 tag == BOOLEAN &&
 853                 constValue() != null &&
 854                 ((Integer)constValue()).intValue() == 0;
 855         }
 856 
 857         /** Is this a constant type whose value is true?
 858          */
 859         @Override
 860         public boolean isTrue() {
 861             return
 862                 tag == BOOLEAN &&
 863                 constValue() != null &&
 864                 ((Integer)constValue()).intValue() != 0;
 865         }
 866 
 867         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 868         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
 869             return v.visitPrimitive(this, p);
 870         }
 871 
 872         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 873         public TypeKind getKind() {
 874             switch (tag) {
 875                 case BYTE:      return TypeKind.BYTE;
 876                 case CHAR:      return TypeKind.CHAR;
 877                 case SHORT:     return TypeKind.SHORT;
 878                 case INT:       return TypeKind.INT;
 879                 case LONG:      return TypeKind.LONG;
 880                 case FLOAT:     return TypeKind.FLOAT;
 881                 case DOUBLE:    return TypeKind.DOUBLE;
 882                 case BOOLEAN:   return TypeKind.BOOLEAN;
 883             }
 884             throw new AssertionError();
 885         }
 886 
 887     }
 888 
 889     public static class WildcardType extends Type
 890             implements javax.lang.model.type.WildcardType {
 891 
 892         public Type type;
 893         public BoundKind kind;
 894         public TypeVar bound;
 895 
 896         @Override
 897         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
 898             return v.visitWildcardType(this, s);
 899         }
 900 
 901         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
 902             this(type, kind, tsym, null, TypeMetadata.EMPTY);
 903         }
 904 
 905         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym,
 906                             TypeMetadata metadata) {
 907             this(type, kind, tsym, null, metadata);
 908         }
 909 
 910         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym,
 911                             TypeVar bound) {
 912             this(type, kind, tsym, bound, TypeMetadata.EMPTY);
 913         }
 914 
 915         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym,
 916                             TypeVar bound, TypeMetadata metadata) {
 917             super(tsym, metadata);
 918             this.type = Assert.checkNonNull(type);
 919             this.kind = kind;
 920             this.bound = bound;
 921         }
 922 
 923         @Override
 924         public WildcardType cloneWithMetadata(TypeMetadata md) {
 925             return new WildcardType(type, kind, tsym, bound, md) {
 926                 @Override
 927                 public Type baseType() { return WildcardType.this.baseType(); }
 928             };
 929         }
 930 
 931         @Override
 932         public TypeTag getTag() {
 933             return WILDCARD;
 934         }
 935 
 936         @Override
 937         public boolean contains(Type t) {
 938             return kind != UNBOUND && type.contains(t);
 939         }
 940 
 941         public boolean isSuperBound() {
 942             return kind == SUPER ||
 943                 kind == UNBOUND;
 944         }
 945         public boolean isExtendsBound() {
 946             return kind == EXTENDS ||
 947                 kind == UNBOUND;
 948         }
 949         public boolean isUnbound() {
 950             // is it `?` or `? extends Object`?
 951             return kind == UNBOUND ||
 952                     (kind == EXTENDS && type.tsym.flatName() == type.tsym.name.table.names.java_lang_Object);
 953         }
 954 
 955         @Override
 956         public boolean isReference() {
 957             return true;
 958         }
 959 
 960         @Override
 961         public boolean isNullOrReference() {
 962             return true;
 963         }
 964 
 965         @Override
 966         public Type withTypeVar(Type t) {
 967             //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
 968             if (bound == t)
 969                 return this;
 970             bound = (TypeVar)t;
 971             return this;
 972         }
 973 
 974         boolean isPrintingBound = false;
 975         @DefinedBy(Api.LANGUAGE_MODEL)
 976         public String toString() {
 977             StringBuilder s = new StringBuilder();
 978             appendAnnotationsString(s);
 979             s.append(kind.toString());
 980             if (kind != UNBOUND)
 981                 s.append(type);
 982             if (moreInfo && bound != null && !isPrintingBound)
 983                 try {
 984                     isPrintingBound = true;
 985                     s.append("{:").append(bound.getUpperBound()).append(":}");
 986                 } finally {
 987                     isPrintingBound = false;
 988                 }
 989             return s.toString();
 990         }
 991 
 992         @DefinedBy(Api.LANGUAGE_MODEL)
 993         public Type getExtendsBound() {
 994             if (kind == EXTENDS)
 995                 return type;
 996             else
 997                 return null;
 998         }
 999 
1000         @DefinedBy(Api.LANGUAGE_MODEL)
1001         public Type getSuperBound() {
1002             if (kind == SUPER)
1003                 return type;
1004             else
1005                 return null;
1006         }
1007 
1008         @DefinedBy(Api.LANGUAGE_MODEL)
1009         public TypeKind getKind() {
1010             return TypeKind.WILDCARD;
1011         }
1012 
1013         @DefinedBy(Api.LANGUAGE_MODEL)
1014         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1015             return v.visitWildcard(this, p);
1016         }
1017     }
1018 
1019     public static class ConstantPoolQType implements PoolConstant {
1020 
1021         public final Type type;
1022         final Types types;
1023 
1024         public ConstantPoolQType(Type type, Types types) {
1025             this.type = type;
1026             this.types = types;
1027         }
1028 
1029         @Override
1030         public Object poolKey(Types types) {
1031             return this;
1032         }
1033 
1034         @Override
1035         public int poolTag() {
1036             return ClassFile.CONSTANT_Class;
1037         }
1038 
1039         public int hashCode() {
1040             return types.hashCode(type);
1041         }
1042 
1043         public boolean equals(Object obj) {
1044             return (obj instanceof ConstantPoolQType) &&
1045                     types.isSameType(type, ((ConstantPoolQType)obj).type);
1046         }
1047 
1048         public String toString() {
1049             return type.toString();
1050         }
1051     }
1052 
1053     public static class ClassType extends Type implements DeclaredType, LoadableConstant,
1054                                                           javax.lang.model.type.ErrorType {
1055 
1056         /**
1057          * The 'flavor' of a ClassType indicates its reference/primitive projectionness
1058          * viewed against the default nature of the associated class.
1059          */
1060         public enum Flavor {
1061 
1062             /**
1063              * Classic reference type. Also reference projection type of a reference-favoring aka
1064              * reference-default primitive class type
1065              */
1066             L_TypeOf_L,
1067 
1068             /**
1069              * Reference projection type of a primitive-favoring aka primitive-default
1070              * plain vanilla primitive class type,
1071              */
1072             L_TypeOf_Q,
1073 
1074             /**
1075              * Value projection type of a primitive-favoring aka primitive-default
1076              * plain vanilla primitive class type,
1077              */
1078             Q_TypeOf_Q,
1079 
1080             /**
1081              * Value projection type of a reference-favoring aka
1082              * reference-default primitive class type
1083              */
1084             Q_TypeOf_L,
1085 
1086             /**
1087              * Reference projection type of a class type of an as yet unknown default provenance, 'X' will be
1088              * discovered to be 'L' or 'Q' in "due course" and mutated suitably.
1089              */
1090             L_TypeOf_X,
1091 
1092             /**
1093              * Value projection type of a class type of an as yet unknown default provenance, 'X' will be
1094              * discovered to be 'L' or 'Q' in "due course" and mutated suitably.
1095              */
1096             Q_TypeOf_X,
1097 
1098             /**
1099              *  As yet unknown projection type of an as yet unknown default provenance class.
1100              */
1101             X_Typeof_X,
1102 
1103             /**
1104              *  An error type - we don't care to discriminate them any further.
1105              */
1106              E_Typeof_X;
1107 
1108             // We don't seem to need X_Typeof_L or X_Typeof_Q so far.
1109 
1110             // Transform a larval form into a more evolved form
1111             public Flavor metamorphose(boolean isPrimtiveClass) {
1112 
1113                 switch (this) {
1114 
1115                     case E_Typeof_X:  // stunted form
1116                     case L_TypeOf_L:
1117                     case L_TypeOf_Q:
1118                     case Q_TypeOf_L:
1119                     case Q_TypeOf_Q:
1120                             // These are fully evolved sealed forms or stunted - no futher transformation
1121                             return this;
1122                     case L_TypeOf_X:
1123                             return isPrimtiveClass ? L_TypeOf_Q : L_TypeOf_L;
1124                     case Q_TypeOf_X:
1125                             return isPrimtiveClass ? Q_TypeOf_Q : Q_TypeOf_L;
1126                     case X_Typeof_X:
1127                             return isPrimtiveClass ? Q_TypeOf_Q : L_TypeOf_L;
1128                     default:
1129                             throw new AssertionError("Unexpected class type flavor");
1130                 }
1131             }
1132         }
1133 
1134         /** The enclosing type of this type. If this is the type of an inner
1135          *  class, outer_field refers to the type of its enclosing
1136          *  instance class, in all other cases it refers to noType.
1137          */
1138         private Type outer_field;
1139 
1140         /** The type parameters of this type (to be set once class is loaded).
1141          */
1142         public List<Type> typarams_field;
1143 
1144         /** A cache variable for the type parameters of this type,
1145          *  appended to all parameters of its enclosing class.
1146          *  @see #allparams
1147          */
1148         public List<Type> allparams_field;
1149 
1150         /** The supertype of this class (to be set once class is loaded).
1151          */
1152         public Type supertype_field;
1153 
1154         /** The interfaces of this class (to be set once class is loaded).
1155          */
1156         public List<Type> interfaces_field;
1157 
1158         /** All the interfaces of this class, including missing ones.
1159          */
1160         public List<Type> all_interfaces_field;
1161 
1162         /** The 'other' projection: If 'this' is type of a primitive class, then 'projection' is the
1163          *  reference projection type and vice versa. Lazily initialized, not to be accessed directly.
1164         */
1165         public ClassType projection;
1166 
1167         /** Is this L of default {L, Q, X} or Q of default {L, Q, X} ?
1168          */
1169         public Flavor flavor;
1170 
1171         /*
1172          * Use of this constructor is kinda sorta deprecated, use the other constructor
1173          * that forces the call site to consider and include the class type flavor.
1174          */
1175         public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
1176             this(outer, typarams, tsym, TypeMetadata.EMPTY, Flavor.L_TypeOf_L);
1177         }
1178 
1179         public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym,
1180                          TypeMetadata metadata, Flavor flavor) {
1181             super(tsym, metadata);
1182             this.outer_field = outer;
1183             this.typarams_field = typarams;
1184             this.allparams_field = null;
1185             this.supertype_field = null;
1186             this.interfaces_field = null;
1187             this.flavor = flavor;
1188         }
1189 
1190         public int poolTag() {
1191             return ClassFile.CONSTANT_Class;
1192         }
1193 
1194         @Override
1195         public ClassType cloneWithMetadata(TypeMetadata md) {
1196             return new ClassType(outer_field, typarams_field, tsym, md, flavor) {
1197                 @Override
1198                 public Type baseType() { return ClassType.this.baseType(); }
1199             };
1200         }
1201 
1202         @Override
1203         public TypeTag getTag() {
1204             return CLASS;
1205         }
1206 
1207         @Override
1208         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1209             return v.visitClassType(this, s);
1210         }
1211 
1212         public Type constType(Object constValue) {
1213             final Object value = constValue;
1214             return new ClassType(getEnclosingType(), typarams_field, tsym, metadata, flavor) {
1215                     @Override
1216                     public Object constValue() {
1217                         return value;
1218                     }
1219                     @Override
1220                     public Type baseType() {
1221                         return tsym.type;
1222                     }
1223                 };
1224         }
1225 
1226         /** The Java source which this type represents.
1227          */
1228         @DefinedBy(Api.LANGUAGE_MODEL)
1229         public String toString() {
1230             StringBuilder buf = new StringBuilder();
1231             if (getEnclosingType().hasTag(CLASS) && tsym.owner.kind == TYP) {
1232                 buf.append(getEnclosingType().toString());
1233                 buf.append(".");
1234                 appendAnnotationsString(buf);
1235                 buf.append(className(tsym, false));
1236             } else {
1237                 if (isAnnotated()) {
1238                     if (!tsym.packge().isUnnamed()) {
1239                         buf.append(tsym.packge());
1240                         buf.append(".");
1241                     }
1242                     appendAnnotationsString(buf);
1243                     buf.append(tsym.name);
1244                 } else {
1245                     buf.append(className(tsym, true));
1246                 }
1247             }
1248 
1249             boolean isReferenceProjection;
1250             try {
1251                 isReferenceProjection = isReferenceProjection();
1252             } catch (CompletionFailure cf) {
1253                 isReferenceProjection = false; // handle missing types gracefully.
1254             }
1255             if (isReferenceProjection) {
1256                 buf.append('.');
1257                 buf.append(tsym.name.table.names.ref);
1258             }
1259 
1260             if (getTypeArguments().nonEmpty()) {
1261                 buf.append('<');
1262                 buf.append(getTypeArguments().toString());
1263                 buf.append(">");
1264             }
1265             return buf.toString();
1266         }
1267 //where
1268             private String className(Symbol sym, boolean longform) {
1269                 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
1270                     StringBuilder s = new StringBuilder(supertype_field.toString());
1271                     for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
1272                         s.append("&");
1273                         s.append(is.head.toString());
1274                     }
1275                     return s.toString();
1276                 } else if (sym.name.isEmpty()) {
1277                     String s;
1278                     ClassType norm = (ClassType) tsym.type;
1279                     if (norm == null) {
1280                         s = Log.getLocalizedString("anonymous.class", (Object)null);
1281                     } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
1282                         s = Log.getLocalizedString("anonymous.class",
1283                                                    norm.interfaces_field.head);
1284                     } else {
1285                         s = Log.getLocalizedString("anonymous.class",
1286                                                    norm.supertype_field);
1287                     }
1288                     if (moreInfo)
1289                         s += String.valueOf(sym.hashCode());
1290                     return s;
1291                 }
1292                 String s;
1293                 if (longform) {
1294                     s =  sym.getQualifiedName().toString();
1295                 } else {
1296                     s = sym.name.toString();
1297                 }
1298                 return s;
1299             }
1300 
1301         public Flavor getFlavor() {
1302             return flavor;
1303         }
1304 
1305         @DefinedBy(Api.LANGUAGE_MODEL)
1306         public List<Type> getTypeArguments() {
1307             if (typarams_field == null) {
1308                 complete();
1309                 if (typarams_field == null)
1310                     typarams_field = List.nil();
1311             }
1312             return typarams_field;
1313         }
1314 
1315         public boolean hasErasedSupertypes() {
1316             return isRaw();
1317         }
1318 
1319         @DefinedBy(Api.LANGUAGE_MODEL)
1320         public Type getEnclosingType() {
1321             if (outer_field != null && outer_field.isReferenceProjection()) {
1322                 outer_field = outer_field.valueProjection();
1323             }
1324             return outer_field;
1325         }
1326 
1327         public void setEnclosingType(Type outer) {
1328             outer_field = outer;
1329         }
1330 
1331         public List<Type> allparams() {
1332             if (allparams_field == null) {
1333                 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
1334             }
1335             return allparams_field;
1336         }
1337 
1338         public boolean isErroneous() {
1339             return
1340                 getEnclosingType().isErroneous() ||
1341                 isErroneous(getTypeArguments()) ||
1342                 this != tsym.type && tsym.type.isErroneous();
1343         }
1344 
1345         public boolean isParameterized() {
1346             return allparams().tail != null;
1347             // optimization, was: allparams().nonEmpty();
1348         }
1349 
1350         @Override
1351         public boolean isReference() {
1352             return true;
1353         }
1354 
1355         @Override
1356         public boolean isPrimitiveClass() {
1357             return !isReferenceProjection() && tsym != null && tsym.isPrimitiveClass();
1358         }
1359 
1360         @Override
1361         public boolean isValueClass() {
1362             return !isReferenceProjection() && tsym != null && tsym.isValueClass();
1363         }
1364 
1365         @Override
1366         public boolean isValueInterface() {
1367             return tsym != null && tsym.isValueInterface();
1368         }
1369 
1370         @Override
1371         public boolean isIdentityClass() {
1372             return !isReferenceProjection() && tsym != null && tsym.isIdentityClass();
1373         }
1374 
1375         @Override
1376         public boolean isIdentityInterface() {
1377             return isInterface() && tsym.isIdentityInterface();
1378         }
1379 
1380         @Override
1381         public boolean isReferenceProjection() {
1382             // gaurd against over-eager and/or inopportune completion
1383             if (tsym != null) {
1384                 if (flavor == Flavor.L_TypeOf_X || tsym.isCompleted()) {
1385                     flavor = flavor.metamorphose(tsym.isPrimitiveClass());
1386                 }
1387             }
1388             return flavor == Flavor.L_TypeOf_Q;
1389         }
1390 
1391         @Override
1392         public Type valueProjection() {
1393             if (!isReferenceProjection())
1394                 return null;
1395 
1396             if (projection !=  null)
1397                 return projection;
1398 
1399             projection = new ClassType(outer_field, typarams_field, tsym, getMetadata(), Flavor.Q_TypeOf_Q);
1400             projection.allparams_field = allparams_field;
1401             projection.supertype_field = supertype_field;
1402 
1403             projection.interfaces_field = interfaces_field;
1404             projection.all_interfaces_field = all_interfaces_field;
1405             projection.projection = this;
1406             return projection;
1407         }
1408 
1409         // return the reference projection type preserving parameterizations
1410         @Override
1411         public ClassType referenceProjection() {
1412 
1413             if (!isPrimitiveClass())
1414                 return null;
1415 
1416             if (projection != null)
1417                 return projection;
1418 
1419             projection = new ClassType(outer_field, typarams_field, tsym, getMetadata(), Flavor.L_TypeOf_Q);
1420             projection.allparams_field = allparams_field;
1421             projection.supertype_field = supertype_field;
1422 
1423             projection.interfaces_field = interfaces_field;
1424             projection.all_interfaces_field = all_interfaces_field;
1425             projection.projection = this;
1426             return projection;
1427         }
1428 
1429         @Override
1430         public boolean isNullOrReference() {
1431             return true;
1432         }
1433 
1434         /** A cache for the rank. */
1435         int rank_field = -1;
1436 
1437         /** A class type is raw if it misses some
1438          *  of its type parameter sections.
1439          *  After validation, this is equivalent to:
1440          *  {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); }
1441          */
1442         public boolean isRaw() {
1443             return
1444                 this != tsym.type && // necessary, but not sufficient condition
1445                 tsym.type.allparams().nonEmpty() &&
1446                 allparams().isEmpty();
1447         }
1448 
1449         public boolean contains(Type elem) {
1450             return
1451                 elem.equalsIgnoreMetadata(this)
1452                 || (isParameterized()
1453                     && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
1454                 || (isCompound()
1455                     && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
1456         }
1457 
1458         public void complete() {
1459             tsym.complete();
1460         }
1461 
1462         @DefinedBy(Api.LANGUAGE_MODEL)
1463         public TypeKind getKind() {
1464             tsym.apiComplete();
1465             return tsym.kind == TYP ? TypeKind.DECLARED : TypeKind.ERROR;
1466         }
1467 
1468         @DefinedBy(Api.LANGUAGE_MODEL)
1469         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1470             return v.visitDeclared(this, p);
1471         }
1472     }
1473 
1474     public static class ErasedClassType extends ClassType {
1475         public ErasedClassType(Type outer, TypeSymbol tsym,
1476                                TypeMetadata metadata) {
1477             super(outer, List.nil(), tsym, metadata, tsym.type.getFlavor());
1478         }
1479 
1480         @Override
1481         public boolean hasErasedSupertypes() {
1482             return true;
1483         }
1484     }
1485 
1486     // a clone of a ClassType that knows about the alternatives of a union type.
1487     public static class UnionClassType extends ClassType implements UnionType {
1488         final List<? extends Type> alternatives_field;
1489 
1490         public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
1491             // Presently no way to refer to this type directly, so we
1492             // cannot put annotations directly on it.
1493             super(ct.outer_field, ct.typarams_field, ct.tsym);
1494             allparams_field = ct.allparams_field;
1495             supertype_field = ct.supertype_field;
1496             interfaces_field = ct.interfaces_field;
1497             all_interfaces_field = ct.interfaces_field;
1498             alternatives_field = alternatives;
1499         }
1500 
1501         @Override
1502         public UnionClassType cloneWithMetadata(TypeMetadata md) {
1503             throw new AssertionError("Cannot add metadata to a union type");
1504         }
1505 
1506         public Type getLub() {
1507             return tsym.type;
1508         }
1509 
1510         @DefinedBy(Api.LANGUAGE_MODEL)
1511         public java.util.List<? extends TypeMirror> getAlternatives() {
1512             return Collections.unmodifiableList(alternatives_field);
1513         }
1514 
1515         @Override
1516         public boolean isUnion() {
1517             return true;
1518         }
1519 
1520         @Override
1521         public boolean isCompound() {
1522             return getLub().isCompound();
1523         }
1524 
1525         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1526         public TypeKind getKind() {
1527             return TypeKind.UNION;
1528         }
1529 
1530         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1531         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1532             return v.visitUnion(this, p);
1533         }
1534 
1535         public Iterable<? extends Type> getAlternativeTypes() {
1536             return alternatives_field;
1537         }
1538     }
1539 
1540     // a clone of a ClassType that knows about the bounds of an intersection type.
1541     public static class IntersectionClassType extends ClassType implements IntersectionType {
1542 
1543         public boolean allInterfaces;
1544 
1545         public IntersectionClassType(List<Type> bounds, ClassSymbol csym, boolean allInterfaces) {
1546             // Presently no way to refer to this type directly, so we
1547             // cannot put annotations directly on it.
1548             super(Type.noType, List.nil(), csym);
1549             this.allInterfaces = allInterfaces;
1550             Assert.check((csym.flags() & COMPOUND) != 0);
1551             supertype_field = bounds.head;
1552             interfaces_field = bounds.tail;
1553             Assert.check(!supertype_field.tsym.isCompleted() ||
1554                     !supertype_field.isInterface(), supertype_field);
1555         }
1556 
1557         @Override
1558         public IntersectionClassType cloneWithMetadata(TypeMetadata md) {
1559             throw new AssertionError("Cannot add metadata to an intersection type");
1560         }
1561 
1562         @DefinedBy(Api.LANGUAGE_MODEL)
1563         public java.util.List<? extends TypeMirror> getBounds() {
1564             return Collections.unmodifiableList(getExplicitComponents());
1565         }
1566 
1567         @Override
1568         public boolean isCompound() {
1569             return true;
1570         }
1571 
1572         public List<Type> getComponents() {
1573             return interfaces_field.prepend(supertype_field);
1574         }
1575 
1576         @Override
1577         public boolean isIntersection() {
1578             return true;
1579         }
1580 
1581         public List<Type> getExplicitComponents() {
1582             return allInterfaces ?
1583                     interfaces_field :
1584                     getComponents();
1585         }
1586 
1587         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1588         public TypeKind getKind() {
1589             return TypeKind.INTERSECTION;
1590         }
1591 
1592         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1593         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1594             return v.visitIntersection(this, p);
1595         }
1596     }
1597 
1598     public static class ArrayType extends Type
1599             implements LoadableConstant, javax.lang.model.type.ArrayType {
1600 
1601         public Type elemtype;
1602 
1603         public ArrayType(Type elemtype, TypeSymbol arrayClass) {
1604             this(elemtype, arrayClass, TypeMetadata.EMPTY);
1605         }
1606 
1607         public ArrayType(Type elemtype, TypeSymbol arrayClass,
1608                          TypeMetadata metadata) {
1609             super(arrayClass, metadata);
1610             this.elemtype = elemtype;
1611         }
1612 
1613         public ArrayType(ArrayType that) {
1614             //note: type metadata is deliberately shared here, as we want side-effects from annotation
1615             //processing to flow from original array to the cloned array.
1616             this(that.elemtype, that.tsym, that.getMetadata());
1617         }
1618 
1619         public int poolTag() {
1620             return ClassFile.CONSTANT_Class;
1621         }
1622 
1623         @Override
1624         public ArrayType cloneWithMetadata(TypeMetadata md) {
1625             return new ArrayType(elemtype, tsym, md) {
1626                 @Override
1627                 public Type baseType() { return ArrayType.this.baseType(); }
1628             };
1629         }
1630 
1631         @Override
1632         public TypeTag getTag() {
1633             return ARRAY;
1634         }
1635 
1636         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1637             return v.visitArrayType(this, s);
1638         }
1639 
1640         @DefinedBy(Api.LANGUAGE_MODEL)
1641         public String toString() {
1642             StringBuilder sb = new StringBuilder();
1643 
1644             // First append root component type
1645             Type t = elemtype;
1646             while (t.getKind() == TypeKind.ARRAY)
1647                 t = ((ArrayType) t).getComponentType();
1648             sb.append(t);
1649 
1650             // then append @Anno[] @Anno[] ... @Anno[]
1651             t = this;
1652             do {
1653                 t.appendAnnotationsString(sb, true);
1654                 sb.append("[]");
1655                 t = ((ArrayType) t).getComponentType();
1656             } while (t.getKind() == TypeKind.ARRAY);
1657 
1658             return sb.toString();
1659         }
1660 
1661         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1662         public boolean equals(Object obj) {
1663             return (obj instanceof ArrayType arrayType)
1664                     && (this == arrayType || elemtype.equals(arrayType.elemtype));
1665         }
1666 
1667         @DefinedBy(Api.LANGUAGE_MODEL)
1668         public int hashCode() {
1669             return (ARRAY.ordinal() << 5) + elemtype.hashCode();
1670         }
1671 
1672         public boolean isVarargs() {
1673             return false;
1674         }
1675 
1676         public List<Type> allparams() { return elemtype.allparams(); }
1677 
1678         public boolean isErroneous() {
1679             return elemtype.isErroneous();
1680         }
1681 
1682         public boolean isParameterized() {
1683             return elemtype.isParameterized();
1684         }
1685 
1686         @Override
1687         public boolean isReference() {
1688             return true;
1689         }
1690 
1691         @Override
1692         public boolean isNullOrReference() {
1693             return true;
1694         }
1695 
1696         public boolean isRaw() {
1697             return elemtype.isRaw();
1698         }
1699 
1700         public ArrayType makeVarargs() {
1701             return new ArrayType(elemtype, tsym, metadata) {
1702                 @Override
1703                 public boolean isVarargs() {
1704                     return true;
1705                 }
1706             };
1707         }
1708 
1709         public boolean contains(Type elem) {
1710             return elem.equalsIgnoreMetadata(this) || elemtype.contains(elem);
1711         }
1712 
1713         public void complete() {
1714             elemtype.complete();
1715         }
1716 
1717         @DefinedBy(Api.LANGUAGE_MODEL)
1718         public Type getComponentType() {
1719             return elemtype;
1720         }
1721 
1722         @DefinedBy(Api.LANGUAGE_MODEL)
1723         public TypeKind getKind() {
1724             return TypeKind.ARRAY;
1725         }
1726 
1727         @DefinedBy(Api.LANGUAGE_MODEL)
1728         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1729             return v.visitArray(this, p);
1730         }
1731     }
1732 
1733     public static class MethodType extends Type implements ExecutableType, LoadableConstant {
1734 
1735         public List<Type> argtypes;
1736         public Type restype;
1737         public List<Type> thrown;
1738 
1739         /** The type annotations on the method receiver.
1740          */
1741         public Type recvtype;
1742 
1743         public MethodType(List<Type> argtypes,
1744                           Type restype,
1745                           List<Type> thrown,
1746                           TypeSymbol methodClass) {
1747             // Presently no way to refer to a method type directly, so
1748             // we cannot put type annotations on it.
1749             super(methodClass, TypeMetadata.EMPTY);
1750             this.argtypes = argtypes;
1751             this.restype = restype;
1752             this.thrown = thrown;
1753         }
1754 
1755         @Override
1756         public MethodType cloneWithMetadata(TypeMetadata md) {
1757             throw new AssertionError("Cannot add metadata to a method type");
1758         }
1759 
1760         @Override
1761         public TypeTag getTag() {
1762             return METHOD;
1763         }
1764 
1765         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1766             return v.visitMethodType(this, s);
1767         }
1768 
1769         /** The Java source which this type represents.
1770          *
1771          *  XXX 06/09/99 iris This isn't correct Java syntax, but it probably
1772          *  should be.
1773          */
1774         @DefinedBy(Api.LANGUAGE_MODEL)
1775         public String toString() {
1776             StringBuilder sb = new StringBuilder();
1777             appendAnnotationsString(sb);
1778             sb.append('(');
1779             sb.append(argtypes);
1780             sb.append(')');
1781             sb.append(restype);
1782             return sb.toString();
1783         }
1784 
1785         @DefinedBy(Api.LANGUAGE_MODEL)
1786         public List<Type>        getParameterTypes() { return argtypes; }
1787         @DefinedBy(Api.LANGUAGE_MODEL)
1788         public Type              getReturnType()     { return restype; }
1789         @DefinedBy(Api.LANGUAGE_MODEL)
1790         public Type              getReceiverType()   {
1791             return (recvtype == null) ? Type.noType : recvtype;
1792         }
1793         @DefinedBy(Api.LANGUAGE_MODEL)
1794         public List<Type>        getThrownTypes()    { return thrown; }
1795 
1796         public boolean isErroneous() {
1797             return
1798                 isErroneous(argtypes) ||
1799                 restype != null && restype.isErroneous();
1800         }
1801 
1802         @Override
1803         public int poolTag() {
1804             return ClassFile.CONSTANT_MethodType;
1805         }
1806 
1807         public boolean contains(Type elem) {
1808             return elem.equalsIgnoreMetadata(this) || contains(argtypes, elem) || restype.contains(elem) || contains(thrown, elem);
1809         }
1810 
1811         public MethodType asMethodType() { return this; }
1812 
1813         public void complete() {
1814             for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
1815                 l.head.complete();
1816             restype.complete();
1817             recvtype.complete();
1818             for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
1819                 l.head.complete();
1820         }
1821 
1822         @DefinedBy(Api.LANGUAGE_MODEL)
1823         public List<TypeVar> getTypeVariables() {
1824             return List.nil();
1825         }
1826 
1827         public TypeSymbol asElement() {
1828             return null;
1829         }
1830 
1831         @DefinedBy(Api.LANGUAGE_MODEL)
1832         public TypeKind getKind() {
1833             return TypeKind.EXECUTABLE;
1834         }
1835 
1836         @DefinedBy(Api.LANGUAGE_MODEL)
1837         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1838             return v.visitExecutable(this, p);
1839         }
1840     }
1841 
1842     public static class PackageType extends Type implements NoType {
1843 
1844         PackageType(PackageSymbol tsym) {
1845             // Package types cannot be annotated
1846             super(tsym, TypeMetadata.EMPTY);
1847         }
1848 
1849         @Override
1850         public PackageType cloneWithMetadata(TypeMetadata md) {
1851             throw new AssertionError("Cannot add metadata to a package type");
1852         }
1853 
1854         @Override
1855         public TypeTag getTag() {
1856             return PACKAGE;
1857         }
1858 
1859         @Override
1860         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1861             return v.visitPackageType(this, s);
1862         }
1863 
1864         @DefinedBy(Api.LANGUAGE_MODEL)
1865         public String toString() {
1866             return tsym.getQualifiedName().toString();
1867         }
1868 
1869         @DefinedBy(Api.LANGUAGE_MODEL)
1870         public TypeKind getKind() {
1871             return TypeKind.PACKAGE;
1872         }
1873 
1874         @DefinedBy(Api.LANGUAGE_MODEL)
1875         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1876             return v.visitNoType(this, p);
1877         }
1878     }
1879 
1880     public static class ModuleType extends Type implements NoType {
1881 
1882         ModuleType(ModuleSymbol tsym) {
1883             // Module types cannot be annotated
1884             super(tsym, TypeMetadata.EMPTY);
1885         }
1886 
1887         @Override
1888         public ModuleType cloneWithMetadata(TypeMetadata md) {
1889             throw new AssertionError("Cannot add metadata to a module type");
1890         }
1891 
1892         @Override
1893         public ModuleType annotatedType(List<Attribute.TypeCompound> annos) {
1894             throw new AssertionError("Cannot annotate a module type");
1895         }
1896 
1897         @Override
1898         public TypeTag getTag() {
1899             return TypeTag.MODULE;
1900         }
1901 
1902         @Override
1903         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1904             return v.visitModuleType(this, s);
1905         }
1906 
1907         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1908         public String toString() {
1909             return tsym.getQualifiedName().toString();
1910         }
1911 
1912         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1913         public TypeKind getKind() {
1914             return TypeKind.MODULE;
1915         }
1916 
1917         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1918         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
1919             return v.visitNoType(this, p);
1920         }
1921     }
1922 
1923     public static class TypeVar extends Type implements TypeVariable {
1924 
1925         /** The upper bound of this type variable; set from outside.
1926          *  Must be nonempty once it is set.
1927          *  For a bound, `bound' is the bound type itself.
1928          *  Multiple bounds are expressed as a single class type which has the
1929          *  individual bounds as superclass, respectively interfaces.
1930          *  The class type then has as `tsym' a compiler generated class `c',
1931          *  which has a flag COMPOUND and whose owner is the type variable
1932          *  itself. Furthermore, the erasure_field of the class
1933          *  points to the first class or interface bound.
1934          */
1935         private Type _bound = null;
1936 
1937         /** The lower bound of this type variable.
1938          *  TypeVars don't normally have a lower bound, so it is normally set
1939          *  to syms.botType.
1940          *  Subtypes, such as CapturedType, may provide a different value.
1941          */
1942         public Type lower;
1943 
1944         public TypeVar(Name name, Symbol owner, Type lower) {
1945             super(null, TypeMetadata.EMPTY);
1946             Assert.checkNonNull(lower);
1947             tsym = new TypeVariableSymbol(0, name, this, owner);
1948             this.setUpperBound(null);
1949             this.lower = lower;
1950         }
1951 
1952         public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
1953             this(tsym, bound, lower, TypeMetadata.EMPTY);
1954         }
1955 
1956         public TypeVar(TypeSymbol tsym, Type bound, Type lower,
1957                        TypeMetadata metadata) {
1958             super(tsym, metadata);
1959             Assert.checkNonNull(lower);
1960             this.setUpperBound(bound);
1961             this.lower = lower;
1962         }
1963 
1964         @Override
1965         public TypeVar cloneWithMetadata(TypeMetadata md) {
1966             return new TypeVar(tsym, getUpperBound(), lower, md) {
1967                 @Override
1968                 public Type baseType() { return TypeVar.this.baseType(); }
1969 
1970                 @Override @DefinedBy(Api.LANGUAGE_MODEL)
1971                 public Type getUpperBound() { return TypeVar.this.getUpperBound(); }
1972 
1973                 public void setUpperBound(Type bound) { TypeVar.this.setUpperBound(bound); }
1974             };
1975         }
1976 
1977         @Override
1978         public TypeTag getTag() {
1979             return TYPEVAR;
1980         }
1981 
1982         @Override
1983         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
1984             return v.visitTypeVar(this, s);
1985         }
1986 
1987         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1988         public Type getUpperBound() { return _bound; }
1989 
1990         public void setUpperBound(Type bound) { this._bound = bound; }
1991 
1992         int rank_field = -1;
1993 
1994         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1995         public Type getLowerBound() {
1996             return lower;
1997         }
1998 
1999         @DefinedBy(Api.LANGUAGE_MODEL)
2000         public TypeKind getKind() {
2001             return TypeKind.TYPEVAR;
2002         }
2003 
2004         public boolean isCaptured() {
2005             return false;
2006         }
2007 
2008         @Override
2009         public boolean isReference() {
2010             return true;
2011         }
2012 
2013         @Override
2014         public boolean isNullOrReference() {
2015             return true;
2016         }
2017 
2018         @Override @DefinedBy(Api.LANGUAGE_MODEL)
2019         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
2020             return v.visitTypeVariable(this, p);
2021         }
2022     }
2023 
2024     /** A captured type variable comes from wildcards which can have
2025      *  both upper and lower bound.  CapturedType extends TypeVar with
2026      *  a lower bound.
2027      */
2028     public static class CapturedType extends TypeVar {
2029 
2030         public WildcardType wildcard;
2031 
2032         public CapturedType(Name name,
2033                             Symbol owner,
2034                             Type upper,
2035                             Type lower,
2036                             WildcardType wildcard) {
2037             super(name, owner, lower);
2038             this.lower = Assert.checkNonNull(lower);
2039             this.setUpperBound(upper);
2040             this.wildcard = wildcard;
2041         }
2042 
2043         public CapturedType(TypeSymbol tsym,
2044                             Type bound,
2045                             Type upper,
2046                             Type lower,
2047                             WildcardType wildcard,
2048                             TypeMetadata metadata) {
2049             super(tsym, bound, lower, metadata);
2050             this.wildcard = wildcard;
2051         }
2052 
2053         @Override
2054         public CapturedType cloneWithMetadata(TypeMetadata md) {
2055             return new CapturedType(tsym, getUpperBound(), getUpperBound(), lower, wildcard, md) {
2056                 @Override
2057                 public Type baseType() { return CapturedType.this.baseType(); }
2058 
2059                 @Override @DefinedBy(Api.LANGUAGE_MODEL)
2060                 public Type getUpperBound() { return CapturedType.this.getUpperBound(); }
2061 
2062                 public void setUpperBound(Type bound) { CapturedType.this.setUpperBound(bound); }
2063             };
2064         }
2065 
2066         @Override
2067         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
2068             return v.visitCapturedType(this, s);
2069         }
2070 
2071         @Override
2072         public boolean isCaptured() {
2073             return true;
2074         }
2075 
2076         @Override @DefinedBy(Api.LANGUAGE_MODEL)
2077         public String toString() {
2078             StringBuilder sb = new StringBuilder();
2079             appendAnnotationsString(sb);
2080             sb.append("capture#");
2081             sb.append((hashCode() & 0xFFFFFFFFL) % Printer.PRIME);
2082             sb.append(" of ");
2083             sb.append(wildcard);
2084             return sb.toString();
2085         }
2086     }
2087 
2088     public abstract static class DelegatedType extends Type {
2089         public Type qtype;
2090         public TypeTag tag;
2091 
2092         public DelegatedType(TypeTag tag, Type qtype) {
2093             this(tag, qtype, TypeMetadata.EMPTY);
2094         }
2095 
2096         public DelegatedType(TypeTag tag, Type qtype,
2097                              TypeMetadata metadata) {
2098             super(qtype.tsym, metadata);
2099             this.tag = tag;
2100             this.qtype = qtype;
2101         }
2102 
2103         public TypeTag getTag() { return tag; }
2104         @DefinedBy(Api.LANGUAGE_MODEL)
2105         public String toString() { return qtype.toString(); }
2106         public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
2107         public Type getEnclosingType() { return qtype.getEnclosingType(); }
2108         public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
2109         public Type getReturnType() { return qtype.getReturnType(); }
2110         public Type getReceiverType() { return qtype.getReceiverType(); }
2111         public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
2112         public List<Type> allparams() { return qtype.allparams(); }
2113         public Type getUpperBound() { return qtype.getUpperBound(); }
2114         public boolean isErroneous() { return qtype.isErroneous(); }
2115     }
2116 
2117     /**
2118      * The type of a generic method type. It consists of a method type and
2119      * a list of method type-parameters that are used within the method
2120      * type.
2121      */
2122     public static class ForAll extends DelegatedType implements ExecutableType {
2123         public List<Type> tvars;
2124 
2125         public ForAll(List<Type> tvars, Type qtype) {
2126             super(FORALL, (MethodType)qtype);
2127             this.tvars = tvars;
2128         }
2129 
2130         @Override
2131         public ForAll cloneWithMetadata(TypeMetadata md) {
2132             throw new AssertionError("Cannot add metadata to a forall type");
2133         }
2134 
2135         @Override
2136         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
2137             return v.visitForAll(this, s);
2138         }
2139 
2140         @DefinedBy(Api.LANGUAGE_MODEL)
2141         public String toString() {
2142             StringBuilder sb = new StringBuilder();
2143             appendAnnotationsString(sb);
2144             sb.append('<');
2145             sb.append(tvars);
2146             sb.append('>');
2147             sb.append(qtype);
2148             return sb.toString();
2149         }
2150 
2151         public List<Type> getTypeArguments()   { return tvars; }
2152 
2153         public boolean isErroneous()  {
2154             return qtype.isErroneous();
2155         }
2156 
2157         public boolean contains(Type elem) {
2158             return qtype.contains(elem);
2159         }
2160 
2161         public MethodType asMethodType() {
2162             return (MethodType)qtype;
2163         }
2164 
2165         public void complete() {
2166             for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
2167                 ((TypeVar)l.head).getUpperBound().complete();
2168             }
2169             qtype.complete();
2170         }
2171 
2172         @DefinedBy(Api.LANGUAGE_MODEL)
2173         public List<TypeVar> getTypeVariables() {
2174             return List.convert(TypeVar.class, getTypeArguments());
2175         }
2176 
2177         @DefinedBy(Api.LANGUAGE_MODEL)
2178         public TypeKind getKind() {
2179             return TypeKind.EXECUTABLE;
2180         }
2181 
2182         @DefinedBy(Api.LANGUAGE_MODEL)
2183         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
2184             return v.visitExecutable(this, p);
2185         }
2186     }
2187 
2188     /** A class for inference variables, for use during method/diamond type
2189      *  inference. An inference variable has upper/lower bounds and a set
2190      *  of equality constraints. Such bounds are set during subtyping, type-containment,
2191      *  type-equality checks, when the types being tested contain inference variables.
2192      *  A change listener can be attached to an inference variable, to receive notifications
2193      *  whenever the bounds of an inference variable change.
2194      */
2195     public static class UndetVar extends DelegatedType {
2196 
2197         enum Kind {
2198             NORMAL,
2199             CAPTURED,
2200             THROWS;
2201         }
2202 
2203         /** Inference variable change listener. The listener method is called
2204          *  whenever a change to the inference variable's bounds occurs
2205          */
2206         public interface UndetVarListener {
2207             /** called when some inference variable bounds (of given kinds ibs) change */
2208             void varBoundChanged(UndetVar uv, InferenceBound ib, Type bound, boolean update);
2209             /** called when the inferred type is set on some inference variable */
2210             default void varInstantiated(UndetVar uv) { Assert.error(); }
2211         }
2212 
2213         /**
2214          * Inference variable bound kinds
2215          */
2216         public enum InferenceBound {
2217             /** lower bounds */
2218             LOWER {
2219                 public InferenceBound complement() { return UPPER; }
2220             },
2221             /** equality constraints */
2222             EQ {
2223                 public InferenceBound complement() { return EQ; }
2224             },
2225             /** upper bounds */
2226             UPPER {
2227                 public InferenceBound complement() { return LOWER; }
2228             };
2229 
2230             public abstract InferenceBound complement();
2231 
2232             public boolean lessThan(InferenceBound that) {
2233                 if (that == this) {
2234                     return false;
2235                 } else {
2236                     switch (that) {
2237                         case UPPER: return true;
2238                         case LOWER: return false;
2239                         case EQ: return (this != UPPER);
2240                         default:
2241                             Assert.error("Cannot get here!");
2242                             return false;
2243                     }
2244                 }
2245             }
2246         }
2247 
2248         /** list of incorporation actions (used by the incorporation engine). */
2249         public ArrayDeque<IncorporationAction> incorporationActions = new ArrayDeque<>();
2250 
2251         /** inference variable bounds */
2252         protected Map<InferenceBound, List<Type>> bounds;
2253 
2254         /** inference variable's inferred type (set from Infer.java) */
2255         private Type inst = null;
2256 
2257         /** number of declared (upper) bounds */
2258         public int declaredCount;
2259 
2260         /** inference variable's change listener */
2261         public UndetVarListener listener = null;
2262 
2263         Kind kind;
2264 
2265         @Override
2266         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
2267             return v.visitUndetVar(this, s);
2268         }
2269 
2270         public UndetVar(TypeVar origin, UndetVarListener listener, Types types) {
2271             // This is a synthesized internal type, so we cannot annotate it.
2272             super(UNDETVAR, origin);
2273             this.kind = origin.isCaptured() ?
2274                     Kind.CAPTURED :
2275                     Kind.NORMAL;
2276             this.listener = listener;
2277             bounds = new EnumMap<>(InferenceBound.class);
2278             List<Type> declaredBounds = types.getBounds(origin);
2279             declaredCount = declaredBounds.length();
2280             bounds.put(InferenceBound.UPPER, List.nil());
2281             bounds.put(InferenceBound.LOWER, List.nil());
2282             bounds.put(InferenceBound.EQ, List.nil());
2283             for (Type t : declaredBounds.reverse()) {
2284                 //add bound works in reverse order
2285                 addBound(InferenceBound.UPPER, t, types, true);
2286             }
2287             if (origin.isCaptured() && !origin.lower.hasTag(BOT)) {
2288                 //add lower bound if needed
2289                 addBound(InferenceBound.LOWER, origin.lower, types, true);
2290             }
2291         }
2292 
2293         @DefinedBy(Api.LANGUAGE_MODEL)
2294         public String toString() {
2295             StringBuilder sb = new StringBuilder();
2296             appendAnnotationsString(sb);
2297             if (inst == null) {
2298                 sb.append(qtype);
2299                 sb.append('?');
2300             } else {
2301                 sb.append(inst);
2302             }
2303             return sb.toString();
2304         }
2305 
2306         public String debugString() {
2307             String result = "inference var = " + qtype + "\n";
2308             if (inst != null) {
2309                 result += "inst = " + inst + '\n';
2310             }
2311             for (InferenceBound bound: InferenceBound.values()) {
2312                 List<Type> aboundList = bounds.get(bound);
2313                 if (aboundList != null && aboundList.size() > 0) {
2314                     result += bound + " = " + aboundList + '\n';
2315                 }
2316             }
2317             return result;
2318         }
2319 
2320         public void setThrow() {
2321             if (this.kind == Kind.CAPTURED) {
2322                 //invalid state transition
2323                 throw new IllegalStateException();
2324             }
2325             this.kind = Kind.THROWS;
2326         }
2327 
2328         /**
2329          * Returns a new copy of this undet var.
2330          */
2331         public UndetVar dup(Types types) {
2332             UndetVar uv2 = new UndetVar((TypeVar)qtype, listener, types);
2333             dupTo(uv2, types);
2334             return uv2;
2335         }
2336 
2337         /**
2338          * Dumps the contents of this undet var on another undet var.
2339          */
2340         public void dupTo(UndetVar uv2, Types types) {
2341             uv2.listener = null;
2342             uv2.bounds.clear();
2343             for (InferenceBound ib : InferenceBound.values()) {
2344                 uv2.bounds.put(ib, List.nil());
2345                 for (Type t : getBounds(ib)) {
2346                     uv2.addBound(ib, t, types, true);
2347                 }
2348             }
2349             uv2.inst = inst;
2350             uv2.listener = listener;
2351             uv2.incorporationActions = new ArrayDeque<>();
2352             for (IncorporationAction action : incorporationActions) {
2353                 uv2.incorporationActions.add(action.dup(uv2));
2354             }
2355             uv2.kind = kind;
2356         }
2357 
2358         @Override
2359         public UndetVar cloneWithMetadata(TypeMetadata md) {
2360             throw new AssertionError("Cannot add metadata to an UndetVar type");
2361         }
2362 
2363         @Override
2364         public boolean isPartial() {
2365             return true;
2366         }
2367 
2368         @Override
2369         public Type baseType() {
2370             return (inst == null) ? this : inst.baseType();
2371         }
2372 
2373         public Type getInst() {
2374             return inst;
2375         }
2376 
2377         public void setInst(Type inst) {
2378             this.inst = inst;
2379             if (listener != null) {
2380                 listener.varInstantiated(this);
2381             }
2382         }
2383 
2384         /** get all bounds of a given kind */
2385         public List<Type> getBounds(InferenceBound... ibs) {
2386             ListBuffer<Type> buf = new ListBuffer<>();
2387             for (InferenceBound ib : ibs) {
2388                 buf.appendList(bounds.get(ib));
2389             }
2390             return buf.toList();
2391         }
2392 
2393         /** get the list of declared (upper) bounds */
2394         public List<Type> getDeclaredBounds() {
2395             ListBuffer<Type> buf = new ListBuffer<>();
2396             int count = 0;
2397             for (Type b : getBounds(InferenceBound.UPPER)) {
2398                 if (count++ == declaredCount) break;
2399                 buf.append(b);
2400             }
2401             return buf.toList();
2402         }
2403 
2404         /** internal method used to override an undetvar bounds */
2405         public void setBounds(InferenceBound ib, List<Type> newBounds) {
2406             bounds.put(ib, newBounds);
2407         }
2408 
2409         /** add a bound of a given kind - this might trigger listener notification */
2410         public final void addBound(InferenceBound ib, Type bound, Types types) {
2411             // Per JDK-8075793: in pre-8 sources, follow legacy javac behavior
2412             // when capture variables are inferred as bounds: for lower bounds,
2413             // map to the capture variable's upper bound; for upper bounds,
2414             // if the capture variable has a lower bound, map to that type
2415             if (types.mapCapturesToBounds) {
2416                 switch (ib) {
2417                     case LOWER:
2418                         bound = types.cvarUpperBound(bound);
2419                         break;
2420                     case UPPER:
2421                         Type altBound = types.cvarLowerBound(bound);
2422                         if (!altBound.hasTag(TypeTag.BOT)) bound = altBound;
2423                         break;
2424                 }
2425             }
2426             addBound(ib, bound, types, false);
2427         }
2428 
2429         @SuppressWarnings("fallthrough")
2430         private void addBound(InferenceBound ib, Type bound, Types types, boolean update) {
2431             if (kind == Kind.CAPTURED && !update) {
2432                 //Captured inference variables bounds must not be updated during incorporation,
2433                 //except when some inference variable (beta) has been instantiated in the
2434                 //right-hand-side of a 'C<alpha> = capture(C<? extends/super beta>) constraint.
2435                 if (bound.hasTag(UNDETVAR) && !((UndetVar)bound).isCaptured()) {
2436                     //If the new incoming bound is itself a (regular) inference variable,
2437                     //then we are allowed to propagate this inference variable bounds to it.
2438                     ((UndetVar)bound).addBound(ib.complement(), this, types, false);
2439                 }
2440             } else {
2441                 Type bound2 = bound.map(toTypeVarMap).baseType();
2442                 List<Type> prevBounds = bounds.get(ib);
2443                 if (bound == qtype) return;
2444                 for (Type b : prevBounds) {
2445                     //check for redundancy - do not add same bound twice
2446                     if (types.isSameType(b, bound2)) return;
2447                 }
2448                 bounds.put(ib, prevBounds.prepend(bound2));
2449                 notifyBoundChange(ib, bound2, false);
2450             }
2451         }
2452         //where
2453             TypeMapping<Void> toTypeVarMap = new StructuralTypeMapping<Void>() {
2454                 @Override
2455                 public Type visitUndetVar(UndetVar uv, Void _unused) {
2456                     return uv.inst != null ? uv.inst : uv.qtype;
2457                 }
2458             };
2459 
2460         /** replace types in all bounds - this might trigger listener notification */
2461         public void substBounds(List<Type> from, List<Type> to, Types types) {
2462             final ListBuffer<Pair<InferenceBound, Type>>  boundsChanged = new ListBuffer<>();
2463             UndetVarListener prevListener = listener;
2464             try {
2465                 //setup new listener for keeping track of changed bounds
2466                 listener = (uv, ib, t, _ignored) -> {
2467                     Assert.check(uv == UndetVar.this);
2468                     boundsChanged.add(new Pair<>(ib, t));
2469                 };
2470                 for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) {
2471                     InferenceBound ib = _entry.getKey();
2472                     List<Type> prevBounds = _entry.getValue();
2473                     ListBuffer<Type> newBounds = new ListBuffer<>();
2474                     ListBuffer<Type> deps = new ListBuffer<>();
2475                     //step 1 - re-add bounds that are not dependent on ivars
2476                     for (Type t : prevBounds) {
2477                         if (!t.containsAny(from)) {
2478                             newBounds.append(t);
2479                         } else {
2480                             deps.append(t);
2481                         }
2482                     }
2483                     //step 2 - replace bounds
2484                     bounds.put(ib, newBounds.toList());
2485                     //step 3 - for each dependency, add new replaced bound
2486                     for (Type dep : deps) {
2487                         addBound(ib, types.subst(dep, from, to), types, true);
2488                     }
2489                 }
2490             } finally {
2491                 listener = prevListener;
2492                 for (Pair<InferenceBound, Type> boundUpdate : boundsChanged) {
2493                     notifyBoundChange(boundUpdate.fst, boundUpdate.snd, true);
2494                 }
2495             }
2496         }
2497 
2498         private void notifyBoundChange(InferenceBound ib, Type bound, boolean update) {
2499             if (listener != null) {
2500                 listener.varBoundChanged(this, ib, bound, update);
2501             }
2502         }
2503 
2504         public final boolean isCaptured() {
2505             return kind == Kind.CAPTURED;
2506         }
2507 
2508         public final boolean isThrows() {
2509             return kind == Kind.THROWS;
2510         }
2511     }
2512 
2513     /** Represents NONE.
2514      */
2515     public static class JCNoType extends Type implements NoType {
2516         public JCNoType() {
2517             // Need to use List.nil(), because JCNoType constructor
2518             // gets called in static initializers in Type, where
2519             // noAnnotations is also defined.
2520             super(null, TypeMetadata.EMPTY);
2521         }
2522 
2523         @Override
2524         public JCNoType cloneWithMetadata(TypeMetadata md) {
2525             throw new AssertionError("Cannot add metadata to a JCNoType");
2526         }
2527 
2528         @Override
2529         public TypeTag getTag() {
2530             return NONE;
2531         }
2532 
2533         @Override @DefinedBy(Api.LANGUAGE_MODEL)
2534         public TypeKind getKind() {
2535             return TypeKind.NONE;
2536         }
2537 
2538         @Override @DefinedBy(Api.LANGUAGE_MODEL)
2539         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
2540             return v.visitNoType(this, p);
2541         }
2542 
2543         @Override
2544         public boolean isCompound() { return false; }
2545     }
2546 
2547     /** Represents VOID.
2548      */
2549     public static class JCVoidType extends Type implements NoType {
2550 
2551         public JCVoidType() {
2552             // Void cannot be annotated
2553             super(null, TypeMetadata.EMPTY);
2554         }
2555 
2556         @Override
2557         public JCVoidType cloneWithMetadata(TypeMetadata md) {
2558             throw new AssertionError("Cannot add metadata to a void type");
2559         }
2560 
2561         @Override
2562         public TypeTag getTag() {
2563             return VOID;
2564         }
2565 
2566         @Override @DefinedBy(Api.LANGUAGE_MODEL)
2567         public TypeKind getKind() {
2568             return TypeKind.VOID;
2569         }
2570 
2571         @Override
2572         public boolean isCompound() { return false; }
2573 
2574         @Override @DefinedBy(Api.LANGUAGE_MODEL)
2575         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
2576             return v.visitNoType(this, p);
2577         }
2578 
2579         @Override
2580         public boolean isPrimitiveOrVoid() {
2581             return true;
2582         }
2583     }
2584 
2585     static class BottomType extends Type implements NullType {
2586         public BottomType() {
2587             // Bottom is a synthesized internal type, so it cannot be annotated
2588             super(null, TypeMetadata.EMPTY);
2589         }
2590 
2591         @Override
2592         public BottomType cloneWithMetadata(TypeMetadata md) {
2593             throw new AssertionError("Cannot add metadata to a bottom type");
2594         }
2595 
2596         @Override
2597         public TypeTag getTag() {
2598             return BOT;
2599         }
2600 
2601         @Override @DefinedBy(Api.LANGUAGE_MODEL)
2602         public TypeKind getKind() {
2603             return TypeKind.NULL;
2604         }
2605 
2606         @Override
2607         public boolean isCompound() { return false; }
2608 
2609         @Override @DefinedBy(Api.LANGUAGE_MODEL)
2610         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
2611             return v.visitNull(this, p);
2612         }
2613 
2614         @Override
2615         public Type constType(Object value) {
2616             return this;
2617         }
2618 
2619         @Override
2620         public String stringValue() {
2621             return "null";
2622         }
2623 
2624         @Override
2625         public boolean isNullOrReference() {
2626             return true;
2627         }
2628 
2629     }
2630 
2631     public static class ErrorType extends ClassType
2632             implements javax.lang.model.type.ErrorType {
2633 
2634         private Type originalType = null;
2635 
2636         public ErrorType(ClassSymbol c, Type originalType) {
2637             this(originalType, c);
2638             c.type = this;
2639             c.kind = ERR;
2640             c.members_field = new Scope.ErrorScope(c);
2641         }
2642 
2643         public ErrorType(Type originalType, TypeSymbol tsym) {
2644             super(noType, List.nil(), tsym, TypeMetadata.EMPTY, Flavor.E_Typeof_X);
2645             this.originalType = (originalType == null ? noType : originalType);
2646         }
2647 
2648         private ErrorType(Type originalType, TypeSymbol tsym,
2649                           TypeMetadata metadata, Flavor flavor) {
2650             super(noType, List.nil(), null, metadata, flavor);
2651             this.tsym = tsym;
2652             this.originalType = (originalType == null ? noType : originalType);
2653         }
2654 
2655         @Override
2656         public ErrorType cloneWithMetadata(TypeMetadata md) {
2657             return new ErrorType(originalType, tsym, md, getFlavor()) {
2658                 @Override
2659                 public Type baseType() { return ErrorType.this.baseType(); }
2660             };
2661         }
2662 
2663         @Override
2664         public TypeTag getTag() {
2665             return ERROR;
2666         }
2667 
2668         @Override
2669         public boolean isPartial() {
2670             return true;
2671         }
2672 
2673         @Override
2674         public boolean isReference() {
2675             return true;
2676         }
2677 
2678         @Override
2679         public boolean isNullOrReference() {
2680             return true;
2681         }
2682 
2683         public ErrorType(Name name, TypeSymbol container, Type originalType) {
2684             this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
2685         }
2686 
2687         @Override
2688         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
2689             return v.visitErrorType(this, s);
2690         }
2691 
2692         public Type constType(Object constValue) { return this; }
2693         @DefinedBy(Api.LANGUAGE_MODEL)
2694         public Type getEnclosingType()           { return Type.noType; }
2695         public Type getReturnType()              { return this; }
2696         public Type asSub(Symbol sym)            { return this; }
2697 
2698         public boolean isGenType(Type t)         { return true; }
2699         public boolean isErroneous()             { return true; }
2700         public boolean isCompound()              { return false; }
2701         public boolean isInterface()             { return false; }
2702 
2703         public List<Type> allparams()            { return List.nil(); }
2704         @DefinedBy(Api.LANGUAGE_MODEL)
2705         public List<Type> getTypeArguments()     { return List.nil(); }
2706 
2707         @DefinedBy(Api.LANGUAGE_MODEL)
2708         public TypeKind getKind() {
2709             return TypeKind.ERROR;
2710         }
2711 
2712         public Type getOriginalType() {
2713             return originalType;
2714         }
2715 
2716         @DefinedBy(Api.LANGUAGE_MODEL)
2717         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
2718             return v.visitError(this, p);
2719         }
2720     }
2721 
2722     public static class UnknownType extends Type {
2723 
2724         public UnknownType() {
2725             // Unknown is a synthesized internal type, so it cannot be
2726             // annotated.
2727             super(null, TypeMetadata.EMPTY);
2728         }
2729 
2730         @Override
2731         public UnknownType cloneWithMetadata(TypeMetadata md) {
2732             throw new AssertionError("Cannot add metadata to an unknown type");
2733         }
2734 
2735         @Override
2736         public TypeTag getTag() {
2737             return UNKNOWN;
2738         }
2739 
2740         @Override @DefinedBy(Api.LANGUAGE_MODEL)
2741         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
2742             return v.visitUnknown(this, p);
2743         }
2744 
2745         @Override
2746         public boolean isPartial() {
2747             return true;
2748         }
2749     }
2750 
2751     /**
2752      * A visitor for types.  A visitor is used to implement operations
2753      * (or relations) on types.  Most common operations on types are
2754      * binary relations and this interface is designed for binary
2755      * relations, that is, operations of the form
2756      * Type&nbsp;&times;&nbsp;S&nbsp;&rarr;&nbsp;R.
2757      * <!-- In plain text: Type x S -> R -->
2758      *
2759      * @param <R> the return type of the operation implemented by this
2760      * visitor; use Void if no return type is needed.
2761      * @param <S> the type of the second argument (the first being the
2762      * type itself) of the operation implemented by this visitor; use
2763      * Void if a second argument is not needed.
2764      */
2765     public interface Visitor<R,S> {
2766         R visitClassType(ClassType t, S s);
2767         R visitWildcardType(WildcardType t, S s);
2768         R visitArrayType(ArrayType t, S s);
2769         R visitMethodType(MethodType t, S s);
2770         R visitPackageType(PackageType t, S s);
2771         R visitModuleType(ModuleType t, S s);
2772         R visitTypeVar(TypeVar t, S s);
2773         R visitCapturedType(CapturedType t, S s);
2774         R visitForAll(ForAll t, S s);
2775         R visitUndetVar(UndetVar t, S s);
2776         R visitErrorType(ErrorType t, S s);
2777         R visitType(Type t, S s);
2778     }
2779 }