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