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