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