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