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.lang.annotation.Inherited;
  30 import java.util.Collections;
  31 import java.util.EnumSet;
  32 import java.util.Map;
  33 import java.util.Set;
  34 import java.util.concurrent.Callable;
  35 import java.util.function.Supplier;
  36 
  37 import javax.lang.model.element.Element;
  38 import javax.lang.model.element.ElementKind;
  39 import javax.lang.model.element.ElementVisitor;
  40 import javax.lang.model.element.ExecutableElement;
  41 import javax.lang.model.element.Modifier;
  42 import javax.lang.model.element.ModuleElement;
  43 import javax.lang.model.element.NestingKind;
  44 import javax.lang.model.element.PackageElement;
  45 import javax.lang.model.element.TypeElement;
  46 import javax.lang.model.element.TypeParameterElement;
  47 import javax.lang.model.element.VariableElement;
  48 import javax.tools.JavaFileManager;
  49 import javax.tools.JavaFileObject;
  50 
  51 import com.sun.tools.javac.code.Kinds.Kind;
  52 import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata;
  53 import com.sun.tools.javac.code.Type.*;
  54 import com.sun.tools.javac.comp.Attr;
  55 import com.sun.tools.javac.comp.AttrContext;
  56 import com.sun.tools.javac.comp.Env;
  57 import com.sun.tools.javac.jvm.*;
  58 import com.sun.tools.javac.jvm.PoolConstant;
  59 import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
  60 import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
  61 import com.sun.tools.javac.tree.JCTree.Tag;
  62 import com.sun.tools.javac.util.*;
  63 import com.sun.tools.javac.util.DefinedBy.Api;
  64 import com.sun.tools.javac.util.Name;
  65 
  66 import static com.sun.tools.javac.code.Flags.*;
  67 import static com.sun.tools.javac.code.Kinds.*;
  68 import static com.sun.tools.javac.code.Kinds.Kind.*;
  69 import com.sun.tools.javac.code.MissingInfoHandler;
  70 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
  71 import com.sun.tools.javac.code.Scope.WriteableScope;
  72 import com.sun.tools.javac.code.Symbol;
  73 import static com.sun.tools.javac.code.Symbol.OperatorSymbol.AccessCode.FIRSTASGOP;
  74 import com.sun.tools.javac.code.Type;
  75 import static com.sun.tools.javac.code.TypeTag.CLASS;
  76 import static com.sun.tools.javac.code.TypeTag.FORALL;
  77 import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
  78 import static com.sun.tools.javac.jvm.ByteCodes.iadd;
  79 import static com.sun.tools.javac.jvm.ByteCodes.ishll;
  80 import static com.sun.tools.javac.jvm.ByteCodes.lushrl;
  81 import static com.sun.tools.javac.jvm.ByteCodes.lxor;
  82 import static com.sun.tools.javac.jvm.ByteCodes.string_add;
  83 import com.sun.tools.javac.util.Name;
  84 
  85 /** Root class for Java symbols. It contains subclasses
  86  *  for specific sorts of symbols, such as variables, methods and operators,
  87  *  types, packages. Each subclass is represented as a static inner class
  88  *  inside Symbol.
  89  *
  90  *  <p><b>This is NOT part of any supported API.
  91  *  If you write code that depends on this, you do so at your own risk.
  92  *  This code and its internal interfaces are subject to change or
  93  *  deletion without notice.</b>
  94  */
  95 public abstract class Symbol extends AnnoConstruct implements PoolConstant, Element {
  96 
  97     /** The kind of this symbol.
  98      *  @see Kinds
  99      */
 100     public Kind kind;
 101 
 102     /** The flags of this symbol.
 103      */
 104     public long flags_field;
 105 
 106     /** An accessor method for the flags of this symbol.
 107      *  Flags of class symbols should be accessed through the accessor
 108      *  method to make sure that the class symbol is loaded.
 109      */
 110     public long flags() { return flags_field; }
 111 
 112     /** The name of this symbol in Utf8 representation.
 113      */
 114     public Name name;
 115 
 116     /** The type of this symbol.
 117      */
 118     public Type type;
 119 
 120     /** The owner of this symbol.
 121      */
 122     public Symbol owner;
 123 
 124     /** The completer of this symbol.
 125      * This should never equal null (NULL_COMPLETER should be used instead).
 126      */
 127     public Completer completer;
 128 
 129     /** A cache for the type erasure of this symbol.
 130      */
 131     public Type erasure_field;
 132 
 133     // <editor-fold defaultstate="collapsed" desc="annotations">
 134 
 135     /** The attributes of this symbol are contained in this
 136      * SymbolMetadata. The SymbolMetadata instance is NOT immutable.
 137      */
 138     protected SymbolMetadata metadata;
 139 
 140 
 141     /** An accessor method for the attributes of this symbol.
 142      *  Attributes of class symbols should be accessed through the accessor
 143      *  method to make sure that the class symbol is loaded.
 144      */
 145     public List<Attribute.Compound> getRawAttributes() {
 146         return (metadata == null)
 147                 ? List.nil()
 148                 : metadata.getDeclarationAttributes();
 149     }
 150 
 151     /** An accessor method for the type attributes of this symbol.
 152      *  Attributes of class symbols should be accessed through the accessor
 153      *  method to make sure that the class symbol is loaded.
 154      */
 155     public List<Attribute.TypeCompound> getRawTypeAttributes() {
 156         return (metadata == null)
 157                 ? List.nil()
 158                 : metadata.getTypeAttributes();
 159     }
 160 
 161     /** Fetch a particular annotation from a symbol. */
 162     public Attribute.Compound attribute(Symbol anno) {
 163         for (Attribute.Compound a : getRawAttributes()) {
 164             if (a.type.tsym == anno) return a;
 165         }
 166         return null;
 167     }
 168 
 169     public boolean annotationsPendingCompletion() {
 170         return metadata == null ? false : metadata.pendingCompletion();
 171     }
 172 
 173     public void appendAttributes(List<Attribute.Compound> l) {
 174         if (l.nonEmpty()) {
 175             initedMetadata().append(l);
 176         }
 177     }
 178 
 179     public void appendClassInitTypeAttributes(List<Attribute.TypeCompound> l) {
 180         if (l.nonEmpty()) {
 181             initedMetadata().appendClassInitTypeAttributes(l);
 182         }
 183     }
 184 
 185     public void appendInitTypeAttributes(List<Attribute.TypeCompound> l) {
 186         if (l.nonEmpty()) {
 187             initedMetadata().appendInitTypeAttributes(l);
 188         }
 189     }
 190 
 191     public void appendUniqueTypeAttributes(List<Attribute.TypeCompound> l) {
 192         if (l.nonEmpty()) {
 193             initedMetadata().appendUniqueTypes(l);
 194         }
 195     }
 196 
 197     public List<Attribute.TypeCompound> getClassInitTypeAttributes() {
 198         return (metadata == null)
 199                 ? List.nil()
 200                 : metadata.getClassInitTypeAttributes();
 201     }
 202 
 203     public List<Attribute.TypeCompound> getInitTypeAttributes() {
 204         return (metadata == null)
 205                 ? List.nil()
 206                 : metadata.getInitTypeAttributes();
 207     }
 208 
 209     public void setInitTypeAttributes(List<Attribute.TypeCompound> l) {
 210         initedMetadata().setInitTypeAttributes(l);
 211     }
 212 
 213     public void setClassInitTypeAttributes(List<Attribute.TypeCompound> l) {
 214         initedMetadata().setClassInitTypeAttributes(l);
 215     }
 216 
 217     public List<Attribute.Compound> getDeclarationAttributes() {
 218         return (metadata == null)
 219                 ? List.nil()
 220                 : metadata.getDeclarationAttributes();
 221     }
 222 
 223     public boolean hasAnnotations() {
 224         return (metadata != null && !metadata.isEmpty());
 225     }
 226 
 227     public boolean hasTypeAnnotations() {
 228         return (metadata != null && !metadata.isTypesEmpty());
 229     }
 230 
 231     public boolean isCompleted() {
 232         return completer.isTerminal();
 233     }
 234 
 235     public void prependAttributes(List<Attribute.Compound> l) {
 236         if (l.nonEmpty()) {
 237             initedMetadata().prepend(l);
 238         }
 239     }
 240 
 241     public void resetAnnotations() {
 242         initedMetadata().reset();
 243     }
 244 
 245     public void setAttributes(Symbol other) {
 246         if (metadata != null || other.metadata != null) {
 247             initedMetadata().setAttributes(other.metadata);
 248         }
 249     }
 250 
 251     public void setDeclarationAttributes(List<Attribute.Compound> a) {
 252         if (metadata != null || a.nonEmpty()) {
 253             initedMetadata().setDeclarationAttributes(a);
 254         }
 255     }
 256 
 257     public void setTypeAttributes(List<Attribute.TypeCompound> a) {
 258         if (metadata != null || a.nonEmpty()) {
 259             if (metadata == null)
 260                 metadata = new SymbolMetadata(this);
 261             metadata.setTypeAttributes(a);
 262         }
 263     }
 264 
 265     private SymbolMetadata initedMetadata() {
 266         if (metadata == null)
 267             metadata = new SymbolMetadata(this);
 268         return metadata;
 269     }
 270 
 271     /** This method is intended for debugging only. */
 272     public SymbolMetadata getMetadata() {
 273         return metadata;
 274     }
 275 
 276     // </editor-fold>
 277 
 278     /** Construct a symbol with given kind, flags, name, type and owner.
 279      */
 280     public Symbol(Kind kind, long flags, Name name, Type type, Symbol owner) {
 281         this.kind = kind;
 282         this.flags_field = flags;
 283         this.type = type;
 284         this.owner = owner;
 285         this.completer = Completer.NULL_COMPLETER;
 286         this.erasure_field = null;
 287         this.name = name;
 288     }
 289 
 290     @Override
 291     public int poolTag() {
 292         throw new AssertionError("Invalid pool entry");
 293     }
 294 
 295     /** Clone this symbol with new owner.
 296      *  Legal only for fields and methods.
 297      */
 298     public Symbol clone(Symbol newOwner) {
 299         throw new AssertionError();
 300     }
 301 
 302     public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
 303         return v.visitSymbol(this, p);
 304     }
 305 
 306     /** The Java source which this symbol represents.
 307      *  A description of this symbol; overrides Object.
 308      */
 309     public String toString() {
 310         return name.toString();
 311     }
 312 
 313     /** A Java source description of the location of this symbol; used for
 314      *  error reporting.
 315      *
 316      * @return null if the symbol is a package or a toplevel class defined in
 317      * the default package; otherwise, the owner symbol is returned
 318      */
 319     public Symbol location() {
 320         if (owner.name == null || (owner.name.isEmpty() &&
 321                                    (owner.flags() & BLOCK) == 0 &&
 322                                    owner.kind != PCK &&
 323                                    owner.kind != TYP)) {
 324             return null;
 325         }
 326         return owner;
 327     }
 328 
 329     public Symbol location(Type site, Types types) {
 330         if (owner.name == null || owner.name.isEmpty()) {
 331             return location();
 332         }
 333         if (owner.type.hasTag(CLASS)) {
 334             Type ownertype = types.asOuterSuper(site, owner);
 335             if (ownertype != null) return ownertype.tsym;
 336         }
 337         return owner;
 338     }
 339 
 340     public Symbol baseSymbol() {
 341         return this;
 342     }
 343 
 344     /** The symbol's erased type.
 345      */
 346     public Type erasure(Types types) {
 347         if (erasure_field == null)
 348             erasure_field = types.erasure(type);
 349         return erasure_field;
 350     }
 351 
 352     /** The external type of a symbol. This is the symbol's erased type
 353      *  except for constructors of inner classes which get the enclosing
 354      *  instance class added as first argument.
 355      */
 356     public Type externalType(Types types) {
 357         Type t = erasure(types);
 358         if (name == name.table.names.init && owner.hasOuterInstance()) {
 359             Type outerThisType = types.erasure(owner.type.getEnclosingType());
 360             return new MethodType(t.getParameterTypes().prepend(outerThisType),
 361                                   t.getReturnType(),
 362                                   t.getThrownTypes(),
 363                                   t.tsym);
 364         } else {
 365             return t;
 366         }
 367     }
 368 
 369     public boolean isDeprecated() {
 370         return (flags_field & DEPRECATED) != 0;
 371     }
 372 
 373     public boolean hasDeprecatedAnnotation() {
 374         return (flags_field & DEPRECATED_ANNOTATION) != 0;
 375     }
 376 
 377     public boolean isDeprecatedForRemoval() {
 378         return (flags_field & DEPRECATED_REMOVAL) != 0;
 379     }
 380 
 381     public boolean isDeprecatableViaAnnotation() {
 382         switch (getKind()) {
 383             case LOCAL_VARIABLE:
 384             case PACKAGE:
 385             case PARAMETER:
 386             case RESOURCE_VARIABLE:
 387             case EXCEPTION_PARAMETER:
 388                 return false;
 389             default:
 390                 return true;
 391         }
 392     }
 393 
 394     public boolean isStatic() {
 395         return
 396             (flags() & STATIC) != 0 ||
 397             (owner.flags() & INTERFACE) != 0 && kind != MTH &&
 398              name != name.table.names._this;
 399     }
 400 
 401     public boolean isInterface() {
 402         return (flags() & INTERFACE) != 0;
 403     }
 404 
 405     public boolean isPrivate() {
 406         return (flags_field & Flags.AccessFlags) == PRIVATE;
 407     }
 408 
 409     public boolean isEnum() {
 410         return (flags() & ENUM) != 0;
 411     }
 412 
 413     /** Is this symbol declared (directly or indirectly) local
 414      *  to a method or variable initializer?
 415      *  Also includes fields of inner classes which are in
 416      *  turn local to a method or variable initializer.
 417      */
 418     public boolean isLocal() {
 419         return
 420             (owner.kind.matches(KindSelector.VAL_MTH) ||
 421              (owner.kind == TYP && owner.isLocal()));
 422     }
 423 
 424     /** Has this symbol an empty name? This includes anonymous
 425      *  inner classes.
 426      */
 427     public boolean isAnonymous() {
 428         return name.isEmpty();
 429     }
 430 
 431     /** Is this symbol a constructor?
 432      */
 433     public boolean isConstructor() {
 434         return name == name.table.names.init;
 435     }
 436 
 437     public boolean isDynamic() {
 438         return false;
 439     }
 440 
 441     /** The fully qualified name of this symbol.
 442      *  This is the same as the symbol's name except for class symbols,
 443      *  which are handled separately.
 444      */
 445     public Name getQualifiedName() {
 446         return name;
 447     }
 448 
 449     /** The fully qualified name of this symbol after converting to flat
 450      *  representation. This is the same as the symbol's name except for
 451      *  class symbols, which are handled separately.
 452      */
 453     public Name flatName() {
 454         return getQualifiedName();
 455     }
 456 
 457     /** If this is a class or package, its members, otherwise null.
 458      */
 459     public WriteableScope members() {
 460         return null;
 461     }
 462 
 463     /** A class is an inner class if it it has an enclosing instance class.
 464      */
 465     public boolean isInner() {
 466         return kind == TYP && type.getEnclosingType().hasTag(CLASS);
 467     }
 468 
 469     /** An inner class has an outer instance if it is not an interface
 470      *  it has an enclosing instance class which might be referenced from the class.
 471      *  Nested classes can see instance members of their enclosing class.
 472      *  Their constructors carry an additional this$n parameter, inserted
 473      *  implicitly by the compiler.
 474      *
 475      *  @see #isInner
 476      */
 477     public boolean hasOuterInstance() {
 478         return
 479             type.getEnclosingType().hasTag(CLASS) && (flags() & (INTERFACE | NOOUTERTHIS)) == 0;
 480     }
 481 
 482     /** The closest enclosing class of this symbol's declaration.
 483      *  Warning: this (misnamed) method returns the receiver itself
 484      *  when the receiver is a class (as opposed to its enclosing
 485      *  class as one may be misled to believe.)
 486      */
 487     public ClassSymbol enclClass() {
 488         Symbol c = this;
 489         while (c != null &&
 490                (!c.kind.matches(KindSelector.TYP) || !c.type.hasTag(CLASS))) {
 491             c = c.owner;
 492         }
 493         return (ClassSymbol)c;
 494     }
 495 
 496     /** The outermost class which indirectly owns this symbol.
 497      */
 498     public ClassSymbol outermostClass() {
 499         Symbol sym = this;
 500         Symbol prev = null;
 501         while (sym.kind != PCK) {
 502             prev = sym;
 503             sym = sym.owner;
 504         }
 505         return (ClassSymbol) prev;
 506     }
 507 
 508     /** The package which indirectly owns this symbol.
 509      */
 510     public PackageSymbol packge() {
 511         Symbol sym = this;
 512         while (sym.kind != PCK) {
 513             sym = sym.owner;
 514         }
 515         return (PackageSymbol) sym;
 516     }
 517 
 518     /** Is this symbol a subclass of `base'? Only defined for ClassSymbols.
 519      */
 520     public boolean isSubClass(Symbol base, Types types) {
 521         throw new AssertionError("isSubClass " + this);
 522     }
 523 
 524     /** Fully check membership: hierarchy, protection, and hiding.
 525      *  Does not exclude methods not inherited due to overriding.
 526      */
 527     public boolean isMemberOf(TypeSymbol clazz, Types types) {
 528         return
 529             owner == clazz ||
 530             clazz.isSubClass(owner, types) &&
 531             isInheritedIn(clazz, types) &&
 532             !hiddenIn((ClassSymbol)clazz, types);
 533     }
 534 
 535     /** Is this symbol the same as or enclosed by the given class? */
 536     public boolean isEnclosedBy(ClassSymbol clazz) {
 537         for (Symbol sym = this; sym.kind != PCK; sym = sym.owner)
 538             if (sym == clazz) return true;
 539         return false;
 540     }
 541 
 542     private boolean hiddenIn(ClassSymbol clazz, Types types) {
 543         Symbol sym = hiddenInInternal(clazz, types);
 544         Assert.check(sym != null, "the result of hiddenInInternal() can't be null");
 545         /* If we find the current symbol then there is no symbol hiding it
 546          */
 547         return sym != this;
 548     }
 549 
 550     /** This method looks in the supertypes graph that has the current class as the
 551      * initial node, till it finds the current symbol or another symbol that hides it.
 552      * If the current class has more than one supertype (extends one class and
 553      * implements one or more interfaces) then null can be returned, meaning that
 554      * a wrong path in the supertypes graph was selected. Null can only be returned
 555      * as a temporary value, as a result of the recursive call.
 556      */
 557     private Symbol hiddenInInternal(ClassSymbol currentClass, Types types) {
 558         if (currentClass == owner) {
 559             return this;
 560         }
 561         for (Symbol sym : currentClass.members().getSymbolsByName(name)) {
 562             if (sym.kind == kind &&
 563                     (kind != MTH ||
 564                     (sym.flags() & STATIC) != 0 &&
 565                     types.isSubSignature(sym.type, type))) {
 566                 return sym;
 567             }
 568         }
 569         Symbol hiddenSym = null;
 570         for (Type st : types.interfaces(currentClass.type)
 571                 .prepend(types.supertype(currentClass.type))) {
 572             if (st != null && (st.hasTag(CLASS))) {
 573                 Symbol sym = hiddenInInternal((ClassSymbol)st.tsym, types);
 574                 if (sym == this) {
 575                     return this;
 576                 } else if (sym != null) {
 577                     hiddenSym = sym;
 578                 }
 579             }
 580         }
 581         return hiddenSym;
 582     }
 583 
 584     /** Is this symbol accessible in a given class?
 585      *  PRE: If symbol's owner is a interface,
 586      *       it is already assumed that the interface is a superinterface
 587      *       the given class.
 588      *  @param clazz  The class for which we want to establish membership.
 589      *                This must be a subclass of the member's owner.
 590      */
 591     public final boolean isAccessibleIn(Symbol clazz, Types types) {
 592         switch ((int)(flags_field & Flags.AccessFlags)) {
 593         default: // error recovery
 594         case PUBLIC:
 595             return true;
 596         case PRIVATE:
 597             return this.owner == clazz;
 598         case PROTECTED:
 599             // we model interfaces as extending Object
 600             return (clazz.flags() & INTERFACE) == 0;
 601         case 0:
 602             PackageSymbol thisPackage = this.packge();
 603             for (Symbol sup = clazz;
 604                  sup != null && sup != this.owner;
 605                  sup = types.supertype(sup.type).tsym) {
 606                 while (sup.type.hasTag(TYPEVAR))
 607                     sup = sup.type.getUpperBound().tsym;
 608                 if (sup.type.isErroneous())
 609                     return true; // error recovery
 610                 if ((sup.flags() & COMPOUND) != 0)
 611                     continue;
 612                 if (sup.packge() != thisPackage)
 613                     return false;
 614             }
 615             return (clazz.flags() & INTERFACE) == 0;
 616         }
 617     }
 618 
 619     /** Is this symbol inherited into a given class?
 620      *  PRE: If symbol's owner is a interface,
 621      *       it is already assumed that the interface is a superinterface
 622      *       of the given class.
 623      *  @param clazz  The class for which we want to establish membership.
 624      *                This must be a subclass of the member's owner.
 625      */
 626     public boolean isInheritedIn(Symbol clazz, Types types) {
 627         return isAccessibleIn(clazz, types);
 628     }
 629 
 630     /** The (variable or method) symbol seen as a member of given
 631      *  class type`site' (this might change the symbol's type).
 632      *  This is used exclusively for producing diagnostics.
 633      */
 634     public Symbol asMemberOf(Type site, Types types) {
 635         throw new AssertionError();
 636     }
 637 
 638     /** Does this method symbol override `other' symbol, when both are seen as
 639      *  members of class `origin'?  It is assumed that _other is a member
 640      *  of origin.
 641      *
 642      *  It is assumed that both symbols have the same name.  The static
 643      *  modifier is ignored for this test.
 644      *
 645      *  See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
 646      */
 647     public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
 648         return false;
 649     }
 650 
 651     /** Complete the elaboration of this symbol's definition.
 652      */
 653     public void complete() throws CompletionFailure {
 654         if (completer != Completer.NULL_COMPLETER) {
 655             Completer c = completer;
 656             completer = Completer.NULL_COMPLETER;
 657             c.complete(this);
 658         }
 659     }
 660 
 661     public void apiComplete() throws CompletionFailure {
 662         try {
 663             complete();
 664         } catch (CompletionFailure cf) {
 665             cf.dcfh.handleAPICompletionFailure(cf);
 666         }
 667     }
 668 
 669     /** True if the symbol represents an entity that exists.
 670      */
 671     public boolean exists() {
 672         return true;
 673     }
 674 
 675     @DefinedBy(Api.LANGUAGE_MODEL)
 676     public Type asType() {
 677         return type;
 678     }
 679 
 680     @DefinedBy(Api.LANGUAGE_MODEL)
 681     public Symbol getEnclosingElement() {
 682         return owner;
 683     }
 684 
 685     @DefinedBy(Api.LANGUAGE_MODEL)
 686     public ElementKind getKind() {
 687         return ElementKind.OTHER;       // most unkind
 688     }
 689 
 690     @DefinedBy(Api.LANGUAGE_MODEL)
 691     public Set<Modifier> getModifiers() {
 692         apiComplete();
 693         return Flags.asModifierSet(flags());
 694     }
 695 
 696     @DefinedBy(Api.LANGUAGE_MODEL)
 697     public Name getSimpleName() {
 698         return name;
 699     }
 700 
 701     /**
 702      * This is the implementation for {@code
 703      * javax.lang.model.element.Element.getAnnotationMirrors()}.
 704      */
 705     @Override @DefinedBy(Api.LANGUAGE_MODEL)
 706     public List<Attribute.Compound> getAnnotationMirrors() {
 707         apiComplete();
 708         return getRawAttributes();
 709     }
 710 
 711 
 712     // TODO: getEnclosedElements should return a javac List, fix in FilteredMemberList
 713     @DefinedBy(Api.LANGUAGE_MODEL)
 714     public java.util.List<Symbol> getEnclosedElements() {
 715         return List.nil();
 716     }
 717 
 718     public List<TypeVariableSymbol> getTypeParameters() {
 719         ListBuffer<TypeVariableSymbol> l = new ListBuffer<>();
 720         for (Type t : type.getTypeArguments()) {
 721             Assert.check(t.tsym.getKind() == ElementKind.TYPE_PARAMETER);
 722             l.append((TypeVariableSymbol)t.tsym);
 723         }
 724         return l.toList();
 725     }
 726 
 727     public static class DelegatedSymbol<T extends Symbol> extends Symbol {
 728         protected T other;
 729         public DelegatedSymbol(T other) {
 730             super(other.kind, other.flags_field, other.name, other.type, other.owner);
 731             this.other = other;
 732         }
 733         public String toString() { return other.toString(); }
 734         public Symbol location() { return other.location(); }
 735         public Symbol location(Type site, Types types) { return other.location(site, types); }
 736         public Symbol baseSymbol() { return other; }
 737         public Type erasure(Types types) { return other.erasure(types); }
 738         public Type externalType(Types types) { return other.externalType(types); }
 739         public boolean isLocal() { return other.isLocal(); }
 740         public boolean isConstructor() { return other.isConstructor(); }
 741         public Name getQualifiedName() { return other.getQualifiedName(); }
 742         public Name flatName() { return other.flatName(); }
 743         public WriteableScope members() { return other.members(); }
 744         public boolean isInner() { return other.isInner(); }
 745         public boolean hasOuterInstance() { return other.hasOuterInstance(); }
 746         public ClassSymbol enclClass() { return other.enclClass(); }
 747         public ClassSymbol outermostClass() { return other.outermostClass(); }
 748         public PackageSymbol packge() { return other.packge(); }
 749         public boolean isSubClass(Symbol base, Types types) { return other.isSubClass(base, types); }
 750         public boolean isMemberOf(TypeSymbol clazz, Types types) { return other.isMemberOf(clazz, types); }
 751         public boolean isEnclosedBy(ClassSymbol clazz) { return other.isEnclosedBy(clazz); }
 752         public boolean isInheritedIn(Symbol clazz, Types types) { return other.isInheritedIn(clazz, types); }
 753         public Symbol asMemberOf(Type site, Types types) { return other.asMemberOf(site, types); }
 754         public void complete() throws CompletionFailure { other.complete(); }
 755 
 756         @DefinedBy(Api.LANGUAGE_MODEL)
 757         public <R, P> R accept(ElementVisitor<R, P> v, P p) {
 758             return other.accept(v, p);
 759         }
 760 
 761         public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
 762             return v.visitSymbol(other, p);
 763         }
 764 
 765         public T getUnderlyingSymbol() {
 766             return other;
 767         }
 768     }
 769 
 770     /** A base class for Symbols representing types.
 771      */
 772     public static abstract class TypeSymbol extends Symbol {
 773         public TypeSymbol(Kind kind, long flags, Name name, Type type, Symbol owner) {
 774             super(kind, flags, name, type, owner);
 775         }
 776         /** form a fully qualified name from a name and an owner
 777          */
 778         static public Name formFullName(Name name, Symbol owner) {
 779             if (owner == null) return name;
 780             if ((owner.kind != ERR) &&
 781                 (owner.kind.matches(KindSelector.VAL_MTH) ||
 782                  (owner.kind == TYP && owner.type.hasTag(TYPEVAR))
 783                  )) return name;
 784             Name prefix = owner.getQualifiedName();
 785             if (prefix == null || prefix == prefix.table.names.empty)
 786                 return name;
 787             else return prefix.append('.', name);
 788         }
 789 
 790         /** form a fully qualified name from a name and an owner, after
 791          *  converting to flat representation
 792          */
 793         static public Name formFlatName(Name name, Symbol owner) {
 794             if (owner == null || owner.kind.matches(KindSelector.VAL_MTH) ||
 795                 (owner.kind == TYP && owner.type.hasTag(TYPEVAR))
 796                 ) return name;
 797             char sep = owner.kind == TYP ? '$' : '.';
 798             Name prefix = owner.flatName();
 799             if (prefix == null || prefix == prefix.table.names.empty)
 800                 return name;
 801             else return prefix.append(sep, name);
 802         }
 803 
 804         /**
 805          * A partial ordering between type symbols that refines the
 806          * class inheritance graph.
 807          *
 808          * Type variables always precede other kinds of symbols.
 809          */
 810         public final boolean precedes(TypeSymbol that, Types types) {
 811             if (this == that)
 812                 return false;
 813             if (type.hasTag(that.type.getTag())) {
 814                 if (type.hasTag(CLASS)) {
 815                     return
 816                         types.rank(that.type) < types.rank(this.type) ||
 817                         types.rank(that.type) == types.rank(this.type) &&
 818                         that.getQualifiedName().compareTo(this.getQualifiedName()) < 0;
 819                 } else if (type.hasTag(TYPEVAR)) {
 820                     return types.isSubtype(this.type, that.type);
 821                 }
 822             }
 823             return type.hasTag(TYPEVAR);
 824         }
 825 
 826         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 827         public java.util.List<Symbol> getEnclosedElements() {
 828             List<Symbol> list = List.nil();
 829             if (kind == TYP && type.hasTag(TYPEVAR)) {
 830                 return list;
 831             }
 832             apiComplete();
 833             for (Symbol sym : members().getSymbols(NON_RECURSIVE)) {
 834                 sym.apiComplete();
 835                 if ((sym.flags() & SYNTHETIC) == 0 && sym.owner == this && sym.kind != ERR) {
 836                     list = list.prepend(sym);
 837                 }
 838             }
 839             return list;
 840         }
 841 
 842         public AnnotationTypeMetadata getAnnotationTypeMetadata() {
 843             Assert.error("Only on ClassSymbol");
 844             return null; //unreachable
 845         }
 846 
 847         public boolean isAnnotationType() { return false; }
 848 
 849         @Override
 850         public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
 851             return v.visitTypeSymbol(this, p);
 852         }
 853     }
 854 
 855     /**
 856      * Type variables are represented by instances of this class.
 857      */
 858     public static class TypeVariableSymbol
 859             extends TypeSymbol implements TypeParameterElement {
 860 
 861         public TypeVariableSymbol(long flags, Name name, Type type, Symbol owner) {
 862             super(TYP, flags, name, type, owner);
 863         }
 864 
 865         @DefinedBy(Api.LANGUAGE_MODEL)
 866         public ElementKind getKind() {
 867             return ElementKind.TYPE_PARAMETER;
 868         }
 869 
 870         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 871         public Symbol getGenericElement() {
 872             return owner;
 873         }
 874 
 875         @DefinedBy(Api.LANGUAGE_MODEL)
 876         public List<Type> getBounds() {
 877             TypeVar t = (TypeVar)type;
 878             Type bound = t.getUpperBound();
 879             if (!bound.isCompound())
 880                 return List.of(bound);
 881             ClassType ct = (ClassType)bound;
 882             if (!ct.tsym.erasure_field.isInterface()) {
 883                 return ct.interfaces_field.prepend(ct.supertype_field);
 884             } else {
 885                 // No superclass was given in bounds.
 886                 // In this case, supertype is Object, erasure is first interface.
 887                 return ct.interfaces_field;
 888             }
 889         }
 890 
 891         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 892         public List<Attribute.Compound> getAnnotationMirrors() {
 893             // Declaration annotations on type variables are stored in type attributes
 894             // on the owner of the TypeVariableSymbol
 895             List<Attribute.TypeCompound> candidates = owner.getRawTypeAttributes();
 896             int index = owner.getTypeParameters().indexOf(this);
 897             List<Attribute.Compound> res = List.nil();
 898             for (Attribute.TypeCompound a : candidates) {
 899                 if (isCurrentSymbolsAnnotation(a, index))
 900                     res = res.prepend(a);
 901             }
 902 
 903             return res.reverse();
 904         }
 905 
 906         // Helper to getAnnotation[s]
 907         @Override
 908         public <A extends Annotation> Attribute.Compound getAttribute(Class<A> annoType) {
 909             String name = annoType.getName();
 910 
 911             // Declaration annotations on type variables are stored in type attributes
 912             // on the owner of the TypeVariableSymbol
 913             List<Attribute.TypeCompound> candidates = owner.getRawTypeAttributes();
 914             int index = owner.getTypeParameters().indexOf(this);
 915             for (Attribute.TypeCompound anno : candidates)
 916                 if (isCurrentSymbolsAnnotation(anno, index) &&
 917                     name.contentEquals(anno.type.tsym.flatName()))
 918                     return anno;
 919 
 920             return null;
 921         }
 922             //where:
 923             boolean isCurrentSymbolsAnnotation(Attribute.TypeCompound anno, int index) {
 924                 return (anno.position.type == TargetType.CLASS_TYPE_PARAMETER ||
 925                         anno.position.type == TargetType.METHOD_TYPE_PARAMETER) &&
 926                         anno.position.parameter_index == index;
 927             }
 928 
 929 
 930         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 931         public <R, P> R accept(ElementVisitor<R, P> v, P p) {
 932             return v.visitTypeParameter(this, p);
 933         }
 934     }
 935     /** A class for module symbols.
 936      */
 937     public static class ModuleSymbol extends TypeSymbol
 938             implements ModuleElement {
 939 
 940         public Name version;
 941         public JavaFileManager.Location sourceLocation;
 942         public JavaFileManager.Location classLocation;
 943         public JavaFileManager.Location patchLocation;
 944         public JavaFileManager.Location patchOutputLocation;
 945 
 946         /** All directives, in natural order. */
 947         public List<com.sun.tools.javac.code.Directive> directives;
 948         public List<com.sun.tools.javac.code.Directive.RequiresDirective> requires;
 949         public List<com.sun.tools.javac.code.Directive.ExportsDirective> exports;
 950         public List<com.sun.tools.javac.code.Directive.OpensDirective> opens;
 951         public List<com.sun.tools.javac.code.Directive.ProvidesDirective> provides;
 952         public List<com.sun.tools.javac.code.Directive.UsesDirective> uses;
 953 
 954         public ClassSymbol module_info;
 955 
 956         public PackageSymbol unnamedPackage;
 957         public Map<Name, PackageSymbol> visiblePackages;
 958         public Set<ModuleSymbol> readModules;
 959         public List<Symbol> enclosedPackages = List.nil();
 960 
 961         public Completer usesProvidesCompleter = Completer.NULL_COMPLETER;
 962         public final Set<ModuleFlags> flags = EnumSet.noneOf(ModuleFlags.class);
 963         public final Set<ModuleResolutionFlags> resolutionFlags = EnumSet.noneOf(ModuleResolutionFlags.class);
 964 
 965         /**
 966          * Create a ModuleSymbol with an associated module-info ClassSymbol.
 967          */
 968         public static ModuleSymbol create(Name name, Name module_info) {
 969             ModuleSymbol msym = new ModuleSymbol(name, null);
 970             ClassSymbol info = new ClassSymbol(Flags.MODULE, module_info, msym);
 971             info.fullname = formFullName(module_info, msym);
 972             info.flatname = info.fullname;
 973             info.members_field = WriteableScope.create(info);
 974             msym.module_info = info;
 975             return msym;
 976         }
 977 
 978         public ModuleSymbol(Name name, Symbol owner) {
 979             super(MDL, 0, name, null, owner);
 980             Assert.checkNonNull(name);
 981             this.type = new ModuleType(this);
 982         }
 983 
 984         @Override
 985         public int poolTag() {
 986             return ClassFile.CONSTANT_Module;
 987         }
 988 
 989         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 990         public Name getSimpleName() {
 991             return Convert.shortName(name);
 992         }
 993 
 994         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 995         public boolean isOpen() {
 996             return flags.contains(ModuleFlags.OPEN);
 997         }
 998 
 999         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1000         public boolean isUnnamed() {
1001             return name.isEmpty() && owner == null;
1002         }
1003 
1004         @Override
1005         public boolean isDeprecated() {
1006             return hasDeprecatedAnnotation();
1007         }
1008 
1009         public boolean isNoModule() {
1010             return false;
1011         }
1012 
1013         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1014         public ElementKind getKind() {
1015             return ElementKind.MODULE;
1016         }
1017 
1018         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1019         public java.util.List<Directive> getDirectives() {
1020             apiComplete();
1021             completeUsesProvides();
1022             return Collections.unmodifiableList(directives);
1023         }
1024 
1025         public void completeUsesProvides() {
1026             if (usesProvidesCompleter != Completer.NULL_COMPLETER) {
1027                 Completer c = usesProvidesCompleter;
1028                 usesProvidesCompleter = Completer.NULL_COMPLETER;
1029                 c.complete(this);
1030             }
1031         }
1032 
1033         @Override
1034         public ClassSymbol outermostClass() {
1035             return null;
1036         }
1037 
1038         @Override
1039         public String toString() {
1040             // TODO: the following strings should be localized
1041             // Do this with custom anon subtypes in Symtab
1042             String n = (name == null) ? "<unknown>"
1043                     : (name.isEmpty()) ? "<unnamed>"
1044                     : String.valueOf(name);
1045             return n;
1046         }
1047 
1048         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1049         public <R, P> R accept(ElementVisitor<R, P> v, P p) {
1050             return v.visitModule(this, p);
1051         }
1052 
1053         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1054         public List<Symbol> getEnclosedElements() {
1055             List<Symbol> list = List.nil();
1056             for (Symbol sym : enclosedPackages) {
1057                 if (sym.members().anyMatch(m -> m.kind == TYP))
1058                     list = list.prepend(sym);
1059             }
1060             return list;
1061         }
1062 
1063         public void reset() {
1064             this.directives = null;
1065             this.requires = null;
1066             this.exports = null;
1067             this.provides = null;
1068             this.uses = null;
1069             this.visiblePackages = null;
1070         }
1071 
1072     }
1073 
1074     public enum ModuleFlags {
1075         OPEN(0x0020),
1076         SYNTHETIC(0x1000),
1077         MANDATED(0x8000);
1078 
1079         public static int value(Set<ModuleFlags> s) {
1080             int v = 0;
1081             for (ModuleFlags f: s)
1082                 v |= f.value;
1083             return v;
1084         }
1085 
1086         private ModuleFlags(int value) {
1087             this.value = value;
1088         }
1089 
1090         public final int value;
1091     }
1092 
1093     public enum ModuleResolutionFlags {
1094         DO_NOT_RESOLVE_BY_DEFAULT(0x0001),
1095         WARN_DEPRECATED(0x0002),
1096         WARN_DEPRECATED_REMOVAL(0x0004),
1097         WARN_INCUBATING(0x0008);
1098 
1099         public static int value(Set<ModuleResolutionFlags> s) {
1100             int v = 0;
1101             for (ModuleResolutionFlags f: s)
1102                 v |= f.value;
1103             return v;
1104         }
1105 
1106         private ModuleResolutionFlags(int value) {
1107             this.value = value;
1108         }
1109 
1110         public final int value;
1111     }
1112 
1113     /** A class for package symbols
1114      */
1115     public static class PackageSymbol extends TypeSymbol
1116         implements PackageElement {
1117 
1118         public WriteableScope members_field;
1119         public Name fullname;
1120         public ClassSymbol package_info; // see bug 6443073
1121         public ModuleSymbol modle;
1122         // the file containing the documentation comments for the package
1123         public JavaFileObject sourcefile;
1124 
1125         public PackageSymbol(Name name, Type type, Symbol owner) {
1126             super(PCK, 0, name, type, owner);
1127             this.members_field = null;
1128             this.fullname = formFullName(name, owner);
1129         }
1130 
1131         public PackageSymbol(Name name, Symbol owner) {
1132             this(name, null, owner);
1133             this.type = new PackageType(this);
1134         }
1135 
1136         public String toString() {
1137             return fullname.toString();
1138         }
1139 
1140         @DefinedBy(Api.LANGUAGE_MODEL)
1141         public Name getQualifiedName() {
1142             return fullname;
1143         }
1144 
1145         @DefinedBy(Api.LANGUAGE_MODEL)
1146         public boolean isUnnamed() {
1147             return name.isEmpty() && owner != null;
1148         }
1149 
1150         public WriteableScope members() {
1151             complete();
1152             return members_field;
1153         }
1154 
1155         @Override
1156         public int poolTag() {
1157             return ClassFile.CONSTANT_Package;
1158         }
1159 
1160         public long flags() {
1161             complete();
1162             return flags_field;
1163         }
1164 
1165         @Override
1166         public List<Attribute.Compound> getRawAttributes() {
1167             complete();
1168             if (package_info != null) {
1169                 package_info.complete();
1170                 mergeAttributes();
1171             }
1172             return super.getRawAttributes();
1173         }
1174 
1175         private void mergeAttributes() {
1176             if (metadata == null &&
1177                 package_info.metadata != null) {
1178                 metadata = new SymbolMetadata(this);
1179                 metadata.setAttributes(package_info.metadata);
1180             }
1181         }
1182 
1183         /** A package "exists" if a type or package that exists has
1184          *  been seen within it.
1185          */
1186         public boolean exists() {
1187             return (flags_field & EXISTS) != 0;
1188         }
1189 
1190         @DefinedBy(Api.LANGUAGE_MODEL)
1191         public ElementKind getKind() {
1192             return ElementKind.PACKAGE;
1193         }
1194 
1195         @DefinedBy(Api.LANGUAGE_MODEL)
1196         public Symbol getEnclosingElement() {
1197             return modle != null && !modle.isNoModule() ? modle : null;
1198         }
1199 
1200         @DefinedBy(Api.LANGUAGE_MODEL)
1201         public <R, P> R accept(ElementVisitor<R, P> v, P p) {
1202             return v.visitPackage(this, p);
1203         }
1204 
1205         public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1206             return v.visitPackageSymbol(this, p);
1207         }
1208 
1209         /**Resets the Symbol into the state good for next round of annotation processing.*/
1210         public void reset() {
1211             metadata = null;
1212         }
1213 
1214     }
1215 
1216     public static class RootPackageSymbol extends PackageSymbol {
1217         public final MissingInfoHandler missingInfoHandler;
1218 
1219         public RootPackageSymbol(Name name, Symbol owner, MissingInfoHandler missingInfoHandler) {
1220             super(name, owner);
1221             this.missingInfoHandler = missingInfoHandler;
1222         }
1223 
1224     }
1225 
1226     /** A class for class symbols
1227      */
1228     public static class ClassSymbol extends TypeSymbol implements TypeElement {
1229 
1230         /** a scope for all class members; variables, methods and inner classes
1231          *  type parameters are not part of this scope
1232          */
1233         public WriteableScope members_field;
1234 
1235         /** the fully qualified name of the class, i.e. pck.outer.inner.
1236          *  null for anonymous classes
1237          */
1238         public Name fullname;
1239 
1240         /** the fully qualified name of the class after converting to flat
1241          *  representation, i.e. pck.outer$inner,
1242          *  set externally for local and anonymous classes
1243          */
1244         public Name flatname;
1245 
1246         /** the sourcefile where the class came from
1247          */
1248         public JavaFileObject sourcefile;
1249 
1250         /** the classfile from where to load this class
1251          *  this will have extension .class or .java
1252          */
1253         public JavaFileObject classfile;
1254 
1255         /** the list of translated local classes (used for generating
1256          * InnerClasses attribute)
1257          */
1258         public List<ClassSymbol> trans_local;
1259 
1260         /** the annotation metadata attached to this class */
1261         private AnnotationTypeMetadata annotationTypeMetadata;
1262 
1263         public ClassSymbol(long flags, Name name, Type type, Symbol owner) {
1264             super(TYP, flags, name, type, owner);
1265             this.members_field = null;
1266             this.fullname = formFullName(name, owner);
1267             this.flatname = formFlatName(name, owner);
1268             this.sourcefile = null;
1269             this.classfile = null;
1270             this.annotationTypeMetadata = AnnotationTypeMetadata.notAnAnnotationType();
1271         }
1272 
1273         public ClassSymbol(long flags, Name name, Symbol owner) {
1274             this(
1275                 flags,
1276                 name,
1277                 new ClassType(Type.noType, null, null),
1278                 owner);
1279             this.type.tsym = this;
1280         }
1281 
1282         /** The Java source which this symbol represents.
1283          */
1284         public String toString() {
1285             return className();
1286         }
1287 
1288         public long flags() {
1289             complete();
1290             return flags_field;
1291         }
1292 
1293         public WriteableScope members() {
1294             complete();
1295             return members_field;
1296         }
1297 
1298         @Override
1299         public List<Attribute.Compound> getRawAttributes() {
1300             complete();
1301             return super.getRawAttributes();
1302         }
1303 
1304         @Override
1305         public List<Attribute.TypeCompound> getRawTypeAttributes() {
1306             complete();
1307             return super.getRawTypeAttributes();
1308         }
1309 
1310         public Type erasure(Types types) {
1311             if (erasure_field == null)
1312                 erasure_field = new ClassType(types.erasure(type.getEnclosingType()),
1313                                               List.nil(), this,
1314                                               type.getMetadata());
1315             return erasure_field;
1316         }
1317 
1318         public String className() {
1319             if (name.isEmpty())
1320                 return
1321                     Log.getLocalizedString("anonymous.class", flatname);
1322             else
1323                 return fullname.toString();
1324         }
1325 
1326         @DefinedBy(Api.LANGUAGE_MODEL)
1327         public Name getQualifiedName() {
1328             return fullname;
1329         }
1330 
1331         public Name flatName() {
1332             return flatname;
1333         }
1334 
1335         public boolean isSubClass(Symbol base, Types types) {
1336             if (this == base) {
1337                 return true;
1338             } else if ((base.flags() & INTERFACE) != 0) {
1339                 for (Type t = type; t.hasTag(CLASS); t = types.supertype(t))
1340                     for (List<Type> is = types.interfaces(t);
1341                          is.nonEmpty();
1342                          is = is.tail)
1343                         if (is.head.tsym.isSubClass(base, types)) return true;
1344             } else {
1345                 for (Type t = type; t.hasTag(CLASS); t = types.supertype(t))
1346                     if (t.tsym == base) return true;
1347             }
1348             return false;
1349         }
1350 
1351         /** Complete the elaboration of this symbol's definition.
1352          */
1353         public void complete() throws CompletionFailure {
1354             Completer origCompleter = completer;
1355             try {
1356                 super.complete();
1357             } catch (CompletionFailure ex) {
1358                 ex.dcfh.classSymbolCompleteFailed(this, origCompleter);
1359                 // quiet error recovery
1360                 flags_field |= (PUBLIC|STATIC);
1361                 this.type = new ErrorType(this, Type.noType);
1362                 throw ex;
1363             }
1364         }
1365 
1366         @DefinedBy(Api.LANGUAGE_MODEL)
1367         public List<Type> getInterfaces() {
1368             apiComplete();
1369             if (type instanceof ClassType) {
1370                 ClassType t = (ClassType)type;
1371                 if (t.interfaces_field == null) // FIXME: shouldn't be null
1372                     t.interfaces_field = List.nil();
1373                 if (t.all_interfaces_field != null)
1374                     return Type.getModelTypes(t.all_interfaces_field);
1375                 return t.interfaces_field;
1376             } else {
1377                 return List.nil();
1378             }
1379         }
1380 
1381         @DefinedBy(Api.LANGUAGE_MODEL)
1382         public Type getSuperclass() {
1383             apiComplete();
1384             if (type instanceof ClassType) {
1385                 ClassType t = (ClassType)type;
1386                 if (t.supertype_field == null) // FIXME: shouldn't be null
1387                     t.supertype_field = Type.noType;
1388                 // An interface has no superclass; its supertype is Object.
1389                 return t.isInterface()
1390                     ? Type.noType
1391                     : t.supertype_field.getModelType();
1392             } else {
1393                 return Type.noType;
1394             }
1395         }
1396 
1397         /**
1398          * Returns the next class to search for inherited annotations or {@code null}
1399          * if the next class can't be found.
1400          */
1401         private ClassSymbol getSuperClassToSearchForAnnotations() {
1402 
1403             Type sup = getSuperclass();
1404 
1405             if (!sup.hasTag(CLASS) || sup.isErroneous())
1406                 return null;
1407 
1408             return (ClassSymbol) sup.tsym;
1409         }
1410 
1411 
1412         @Override
1413         protected <A extends Annotation> A[] getInheritedAnnotations(Class<A> annoType) {
1414 
1415             ClassSymbol sup = getSuperClassToSearchForAnnotations();
1416 
1417             return sup == null ? super.getInheritedAnnotations(annoType)
1418                                : sup.getAnnotationsByType(annoType);
1419         }
1420 
1421 
1422         @DefinedBy(Api.LANGUAGE_MODEL)
1423         public ElementKind getKind() {
1424             apiComplete();
1425             long flags = flags();
1426             if ((flags & ANNOTATION) != 0)
1427                 return ElementKind.ANNOTATION_TYPE;
1428             else if ((flags & INTERFACE) != 0)
1429                 return ElementKind.INTERFACE;
1430             else if ((flags & ENUM) != 0)
1431                 return ElementKind.ENUM;
1432             else
1433                 return ElementKind.CLASS;
1434         }
1435 
1436         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1437         public Set<Modifier> getModifiers() {
1438             apiComplete();
1439             long flags = flags();
1440             return Flags.asModifierSet(flags & ~DEFAULT);
1441         }
1442 
1443         @DefinedBy(Api.LANGUAGE_MODEL)
1444         public NestingKind getNestingKind() {
1445             apiComplete();
1446             if (owner.kind == PCK)
1447                 return NestingKind.TOP_LEVEL;
1448             else if (name.isEmpty())
1449                 return NestingKind.ANONYMOUS;
1450             else if (owner.kind == MTH)
1451                 return NestingKind.LOCAL;
1452             else
1453                 return NestingKind.MEMBER;
1454         }
1455 
1456 
1457         @Override
1458         protected <A extends Annotation> Attribute.Compound getAttribute(final Class<A> annoType) {
1459 
1460             Attribute.Compound attrib = super.getAttribute(annoType);
1461 
1462             boolean inherited = annoType.isAnnotationPresent(Inherited.class);
1463             if (attrib != null || !inherited)
1464                 return attrib;
1465 
1466             // Search supertypes
1467             ClassSymbol superType = getSuperClassToSearchForAnnotations();
1468             return superType == null ? null
1469                                      : superType.getAttribute(annoType);
1470         }
1471 
1472 
1473 
1474 
1475         @DefinedBy(Api.LANGUAGE_MODEL)
1476         public <R, P> R accept(ElementVisitor<R, P> v, P p) {
1477             return v.visitType(this, p);
1478         }
1479 
1480         public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1481             return v.visitClassSymbol(this, p);
1482         }
1483 
1484         public void markAbstractIfNeeded(Types types) {
1485             if (types.enter.getEnv(this) != null &&
1486                 (flags() & ENUM) != 0 && types.supertype(type).tsym == types.syms.enumSym &&
1487                 (flags() & (FINAL | ABSTRACT)) == 0) {
1488                 if (types.firstUnimplementedAbstract(this) != null)
1489                     // add the ABSTRACT flag to an enum
1490                     flags_field |= ABSTRACT;
1491             }
1492         }
1493 
1494         /**Resets the Symbol into the state good for next round of annotation processing.*/
1495         public void reset() {
1496             kind = TYP;
1497             erasure_field = null;
1498             members_field = null;
1499             flags_field = 0;
1500             if (type instanceof ClassType) {
1501                 ClassType t = (ClassType)type;
1502                 t.setEnclosingType(Type.noType);
1503                 t.rank_field = -1;
1504                 t.typarams_field = null;
1505                 t.allparams_field = null;
1506                 t.supertype_field = null;
1507                 t.interfaces_field = null;
1508                 t.all_interfaces_field = null;
1509             }
1510             clearAnnotationMetadata();
1511         }
1512 
1513         public void clearAnnotationMetadata() {
1514             metadata = null;
1515             annotationTypeMetadata = AnnotationTypeMetadata.notAnAnnotationType();
1516         }
1517 
1518         @Override
1519         public AnnotationTypeMetadata getAnnotationTypeMetadata() {
1520             return annotationTypeMetadata;
1521         }
1522 
1523         @Override
1524         public boolean isAnnotationType() {
1525             return (flags_field & Flags.ANNOTATION) != 0;
1526         }
1527 
1528         public void setAnnotationTypeMetadata(AnnotationTypeMetadata a) {
1529             Assert.checkNonNull(a);
1530             Assert.check(!annotationTypeMetadata.isMetadataForAnnotationType());
1531             this.annotationTypeMetadata = a;
1532         }
1533     }
1534 
1535 
1536     /** A class for variable symbols
1537      */
1538     public static class VarSymbol extends Symbol implements VariableElement {
1539 
1540         /** The variable's declaration position.
1541          */
1542         public int pos = Position.NOPOS;
1543 
1544         /** The variable's address. Used for different purposes during
1545          *  flow analysis, translation and code generation.
1546          *  Flow analysis:
1547          *    If this is a blank final or local variable, its sequence number.
1548          *  Translation:
1549          *    If this is a private field, its access number.
1550          *  Code generation:
1551          *    If this is a local variable, its logical slot number.
1552          */
1553         public int adr = -1;
1554 
1555         /** Construct a variable symbol, given its flags, name, type and owner.
1556          */
1557         public VarSymbol(long flags, Name name, Type type, Symbol owner) {
1558             super(VAR, flags, name, type, owner);
1559         }
1560 
1561         @Override
1562         public int poolTag() {
1563             return ClassFile.CONSTANT_Fieldref;
1564         }
1565 
1566         public MethodHandleSymbol asMethodHandle(boolean getter) {
1567             return new MethodHandleSymbol(this, getter);
1568         }
1569 
1570         /** Clone this symbol with new owner.
1571          */
1572         public VarSymbol clone(Symbol newOwner) {
1573             VarSymbol v = new VarSymbol(flags_field, name, type, newOwner) {
1574                 @Override
1575                 public Symbol baseSymbol() {
1576                     return VarSymbol.this;
1577                 }
1578 
1579                 @Override
1580                 public Object poolKey(Types types) {
1581                     return new Pair<>(newOwner, baseSymbol());
1582                 }
1583             };
1584             v.pos = pos;
1585             v.adr = adr;
1586             v.data = data;
1587 //          System.out.println("clone " + v + " in " + newOwner);//DEBUG
1588             return v;
1589         }
1590 
1591         public String toString() {
1592             return name.toString();
1593         }
1594 
1595         public Symbol asMemberOf(Type site, Types types) {
1596             return new VarSymbol(flags_field, name, types.memberType(site, this), owner);
1597         }
1598 
1599         @DefinedBy(Api.LANGUAGE_MODEL)
1600         public ElementKind getKind() {
1601             long flags = flags();
1602             if ((flags & PARAMETER) != 0) {
1603                 if (isExceptionParameter())
1604                     return ElementKind.EXCEPTION_PARAMETER;
1605                 else
1606                     return ElementKind.PARAMETER;
1607             } else if ((flags & ENUM) != 0) {
1608                 return ElementKind.ENUM_CONSTANT;
1609             } else if (owner.kind == TYP || owner.kind == ERR) {
1610                 return ElementKind.FIELD;
1611             } else if (isResourceVariable()) {
1612                 return ElementKind.RESOURCE_VARIABLE;
1613             } else {
1614                 return ElementKind.LOCAL_VARIABLE;
1615             }
1616         }
1617 
1618         @DefinedBy(Api.LANGUAGE_MODEL)
1619         public <R, P> R accept(ElementVisitor<R, P> v, P p) {
1620             return v.visitVariable(this, p);
1621         }
1622 
1623         @DefinedBy(Api.LANGUAGE_MODEL)
1624         public Object getConstantValue() { // Mirror API
1625             return Constants.decode(getConstValue(), type);
1626         }
1627 
1628         public void setLazyConstValue(final Env<AttrContext> env,
1629                                       final Attr attr,
1630                                       final JCVariableDecl variable)
1631         {
1632             setData((Callable<Object>)() -> attr.attribLazyConstantValue(env, variable, type));
1633         }
1634 
1635         /**
1636          * The variable's constant value, if this is a constant.
1637          * Before the constant value is evaluated, it points to an
1638          * initializer environment.  If this is not a constant, it can
1639          * be used for other stuff.
1640          */
1641         private Object data;
1642 
1643         public boolean isExceptionParameter() {
1644             return data == ElementKind.EXCEPTION_PARAMETER;
1645         }
1646 
1647         public boolean isResourceVariable() {
1648             return data == ElementKind.RESOURCE_VARIABLE;
1649         }
1650 
1651         public Object getConstValue() {
1652             // TODO: Consider if getConstValue and getConstantValue can be collapsed
1653             if (data == ElementKind.EXCEPTION_PARAMETER ||
1654                 data == ElementKind.RESOURCE_VARIABLE) {
1655                 return null;
1656             } else if (data instanceof Callable<?>) {
1657                 // In this case, this is a final variable, with an as
1658                 // yet unevaluated initializer.
1659                 Callable<?> eval = (Callable<?>)data;
1660                 data = null; // to make sure we don't evaluate this twice.
1661                 try {
1662                     data = eval.call();
1663                 } catch (Exception ex) {
1664                     throw new AssertionError(ex);
1665                 }
1666             }
1667             return data;
1668         }
1669 
1670         public void setData(Object data) {
1671             Assert.check(!(data instanceof Env<?>), this);
1672             this.data = data;
1673         }
1674 
1675         public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
1676             return v.visitVarSymbol(this, p);
1677         }
1678     }
1679 
1680     public static class ParamSymbol extends VarSymbol {
1681         public ParamSymbol(long flags, Name name, Type type, Symbol owner) {
1682             super(flags, name, type, owner);
1683         }
1684 
1685         @Override
1686         public Name getSimpleName() {
1687             if ((flags_field & NAME_FILLED) == 0) {
1688                 flags_field |= NAME_FILLED;
1689                 Symbol rootPack = this;
1690                 while (rootPack != null && !(rootPack instanceof RootPackageSymbol)) {
1691                     rootPack = rootPack.owner;
1692                 }
1693                 if (rootPack != null) {
1694                     Name inferredName =
1695                             ((RootPackageSymbol) rootPack).missingInfoHandler.getParameterName(this);
1696                     if (inferredName != null) {
1697                         this.name = inferredName;
1698                     }
1699                 }
1700             }
1701             return super.getSimpleName();
1702         }
1703 
1704     }
1705 
1706     /** A class for method symbols.
1707      */
1708     public static class MethodSymbol extends Symbol implements ExecutableElement {
1709 
1710         /** The code of the method. */
1711         public Code code = null;
1712 
1713         /** The extra (synthetic/mandated) parameters of the method. */
1714         public List<VarSymbol> extraParams = List.nil();
1715 
1716         /** The captured local variables in an anonymous class */
1717         public List<VarSymbol> capturedLocals = List.nil();
1718 
1719         /** The parameters of the method. */
1720         public List<VarSymbol> params = null;
1721 
1722         /** For an annotation type element, its default value if any.
1723          *  The value is null if none appeared in the method
1724          *  declaration.
1725          */
1726         public Attribute defaultValue = null;
1727 
1728         /** Construct a method symbol, given its flags, name, type and owner.
1729          */
1730         public MethodSymbol(long flags, Name name, Type type, Symbol owner) {
1731             super(MTH, flags, name, type, owner);
1732             if (owner.type.hasTag(TYPEVAR)) Assert.error(owner + "." + name);
1733         }
1734 
1735         /** Clone this symbol with new owner.
1736          */
1737         public MethodSymbol clone(Symbol newOwner) {
1738             MethodSymbol m = new MethodSymbol(flags_field, name, type, newOwner) {
1739                 @Override
1740                 public Symbol baseSymbol() {
1741                     return MethodSymbol.this;
1742                 }
1743 
1744                 @Override
1745                 public Object poolKey(Types types) {
1746                     return new Pair<>(newOwner, baseSymbol());
1747                 }
1748             };
1749             m.code = code;
1750             return m;
1751         }
1752 
1753         @Override @DefinedBy(Api.LANGUAGE_MODEL)
1754         public Set<Modifier> getModifiers() {
1755             long flags = flags();
1756             return Flags.asModifierSet((flags & DEFAULT) != 0 ? flags & ~ABSTRACT : flags);
1757         }
1758 
1759         /** The Java source which this symbol represents.
1760          */
1761         public String toString() {
1762             if ((flags() & BLOCK) != 0) {
1763                 return owner.name.toString();
1764             } else {
1765                 String s = (name == name.table.names.init)
1766                     ? owner.name.toString()
1767                     : name.toString();
1768                 if (type != null) {
1769                     if (type.hasTag(FORALL))
1770                         s = "<" + ((ForAll)type).getTypeArguments() + ">" + s;
1771                     s += "(" + type.argtypes((flags() & VARARGS) != 0) + ")";
1772                 }
1773                 return s;
1774             }
1775         }
1776 
1777         @Override
1778         public int poolTag() {
1779             return owner.isInterface() ?
1780                     ClassFile.CONSTANT_InterfaceMethodref : ClassFile.CONSTANT_Methodref;
1781         }
1782 
1783         public boolean isHandle() {
1784             return false;
1785         }
1786 
1787 
1788         public MethodHandleSymbol asHandle() {
1789             return new MethodHandleSymbol(this);
1790         }
1791 
1792         /** find a symbol that this (proxy method) symbol implements.
1793          *  @param    c       The class whose members are searched for
1794          *                    implementations
1795          */
1796         public Symbol implemented(TypeSymbol c, Types types) {
1797             Symbol impl = null;
1798             for (List<Type> is = types.interfaces(c.type);
1799                  impl == null && is.nonEmpty();
1800                  is = is.tail) {
1801                 TypeSymbol i = is.head.tsym;
1802                 impl = implementedIn(i, types);
1803                 if (impl == null)
1804                     impl = implemented(i, types);
1805             }
1806             return impl;
1807         }
1808 
1809         public Symbol implementedIn(TypeSymbol c, Types types) {
1810             Symbol impl = null;
1811             for (Symbol sym : c.members().getSymbolsByName(name)) {
1812                 if (this.overrides(sym, (TypeSymbol)owner, types, true) &&
1813                     // FIXME: I suspect the following requires a
1814                     // subst() for a parametric return type.
1815                     types.isSameType(type.getReturnType(),
1816                                      types.memberType(owner.type, sym).getReturnType())) {
1817                     impl = sym;
1818                 }
1819             }
1820             return impl;
1821         }
1822 
1823         /** Will the erasure of this method be considered by the VM to
1824          *  override the erasure of the other when seen from class `origin'?
1825          */
1826         public boolean binaryOverrides(Symbol _other, TypeSymbol origin, Types types) {
1827             if (isConstructor() || _other.kind != MTH) return false;
1828 
1829             if (this == _other) return true;
1830             MethodSymbol other = (MethodSymbol)_other;
1831 
1832             // check for a direct implementation
1833             if (other.isOverridableIn((TypeSymbol)owner) &&
1834                 types.asSuper(owner.type, other.owner) != null &&
1835                 types.isSameType(erasure(types), other.erasure(types)))
1836                 return true;
1837 
1838             // check for an inherited implementation
1839             return
1840                 (flags() & ABSTRACT) == 0 &&
1841                 other.isOverridableIn(origin) &&
1842                 this.isMemberOf(origin, types) &&
1843                 types.isSameType(erasure(types), other.erasure(types));
1844         }
1845 
1846         /** The implementation of this (abstract) symbol in class origin,
1847          *  from the VM's point of view, null if method does not have an
1848          *  implementation in class.
1849          *  @param origin   The class of which the implementation is a member.
1850          */
1851         public MethodSymbol binaryImplementation(ClassSymbol origin, Types types) {
1852             for (TypeSymbol c = origin; c != null; c = types.supertype(c.type).tsym) {
1853                 for (Symbol sym : c.members().getSymbolsByName(name)) {
1854                     if (sym.kind == MTH &&
1855                         ((MethodSymbol)sym).binaryOverrides(this, origin, types))
1856                         return (MethodSymbol)sym;
1857                 }
1858             }
1859             return null;
1860         }
1861 
1862         /** Does this symbol override `other' symbol, when both are seen as
1863          *  members of class `origin'?  It is assumed that _other is a member
1864          *  of origin.
1865          *
1866          *  It is assumed that both symbols have the same name.  The static
1867          *  modifier is ignored for this test.
1868          *
1869          *  A quirk in the works is that if the receiver is a method symbol for
1870          *  an inherited abstract method we answer false summarily all else being
1871          *  immaterial. Abstract "own" methods (i.e `this' is a direct member of
1872          *  origin) don't get rejected as summarily and are put to test against the
1873          *  suitable criteria.
1874          *
1875          *  See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
1876          */
1877         public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) {
1878             return overrides(_other, origin, types, checkResult, true);
1879         }
1880 
1881         /** Does this symbol override `other' symbol, when both are seen as
1882          *  members of class `origin'?  It is assumed that _other is a member
1883          *  of origin.
1884          *
1885          *  Caveat: If `this' is an abstract inherited member of origin, it is
1886          *  deemed to override `other' only when `requireConcreteIfInherited'
1887          *  is false.
1888          *
1889          *  It is assumed that both symbols have the same name.  The static
1890          *  modifier is ignored for this test.
1891          *
1892          *  See JLS 8.4.6.1 (without transitivity) and 8.4.6.4
1893          */
1894         public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult,
1895                                             boolean requireConcreteIfInherited) {
1896             if (isConstructor() || _other.kind != MTH) return false;
1897 
1898             if (this == _other) return true;
1899             MethodSymbol other = (MethodSymbol)_other;
1900 
1901             // check for a direct implementation
1902             if (other.isOverridableIn((TypeSymbol)owner) &&
1903                 types.asSuper(owner.type, other.owner) != null) {
1904                 Type mt = types.memberType(owner.type, this);
1905                 Type ot = types.memberType(owner.type, other);
1906                 if (types.isSubSignature(mt, ot)) {
1907                     if (!checkResult)
1908                         return true;
1909                     if (types.returnTypeSubstitutable(mt, ot))
1910                         return true;
1911                 }
1912             }
1913 
1914             // check for an inherited implementation
1915             if (((flags() & ABSTRACT) != 0 && requireConcreteIfInherited) ||
1916                     ((other.flags() & ABSTRACT) == 0 && (other.flags() & DEFAULT) == 0) ||
1917                     !other.isOverridableIn(origin) ||
1918                     !this.isMemberOf(origin, types))
1919                 return false;
1920 
1921             // assert types.asSuper(origin.type, other.owner) != null;
1922             Type mt = types.memberType(origin.type, this);
1923             Type ot = types.memberType(origin.type, other);
1924             return
1925                 types.isSubSignature(mt, ot) &&
1926                 (!checkResult || types.resultSubtype(mt, ot, types.noWarnings));
1927         }
1928 
1929         private boolean isOverridableIn(TypeSymbol origin) {
1930             // JLS 8.4.6.1
1931             switch ((int)(flags_field & Flags.AccessFlags)) {
1932             case Flags.PRIVATE:
1933                 return false;
1934             case Flags.PUBLIC:
1935                 return !this.owner.isInterface() ||
1936                         (flags_field & STATIC) == 0;
1937             case Flags.PROTECTED:
1938                 return (origin.flags() & INTERFACE) == 0;
1939             case 0:
1940                 // for package private: can only override in the same
1941                 // package
1942                 return
1943                     this.packge() == origin.packge() &&
1944                     (origin.flags() & INTERFACE) == 0;
1945             default:
1946                 return false;
1947             }
1948         }
1949 
1950         @Override
1951         public boolean isInheritedIn(Symbol clazz, Types types) {
1952             switch ((int)(flags_field & Flags.AccessFlags)) {
1953                 case PUBLIC:
1954                     return !this.owner.isInterface() ||
1955                             clazz == owner ||
1956                             (flags_field & STATIC) == 0;
1957                 default:
1958                     return super.isInheritedIn(clazz, types);
1959             }
1960         }
1961 
1962         public boolean isLambdaMethod() {
1963             return (flags() & LAMBDA_METHOD) == LAMBDA_METHOD;
1964         }
1965 
1966         /** override this method to point to the original enclosing method if this method symbol represents a synthetic
1967          *  lambda method
1968          */
1969         public MethodSymbol originalEnclosingMethod() {
1970             return this;
1971         }
1972 
1973         /** The implementation of this (abstract) symbol in class origin;
1974          *  null if none exists. Synthetic methods are not considered
1975          *  as possible implementations.
1976          */
1977         public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
1978             return implementation(origin, types, checkResult, implementation_filter);
1979         }
1980         // where
1981             public static final Filter<Symbol> implementation_filter = s ->
1982                     s.kind == MTH && (s.flags() & SYNTHETIC) == 0;
1983 
1984         public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult, Filter<Symbol> implFilter) {
1985             MethodSymbol res = types.implementation(this, origin, checkResult, implFilter);
1986             if (res != null)
1987                 return res;
1988             // if origin is derived from a raw type, we might have missed
1989             // an implementation because we do not know enough about instantiations.
1990             // in this case continue with the supertype as origin.
1991             if (types.isDerivedRaw(origin.type) && !origin.isInterface())
1992                 return implementation(types.supertype(origin.type).tsym, types, checkResult);
1993             else
1994                 return null;
1995         }
1996 
1997         public List<VarSymbol> params() {
1998             owner.complete();
1999             if (params == null) {
2000                 ListBuffer<VarSymbol> newParams = new ListBuffer<>();
2001                 int i = 0;
2002                 for (Type t : type.getParameterTypes()) {
2003                     Name paramName = name.table.fromString("arg" + i);
2004                     VarSymbol param = new VarSymbol(PARAMETER, paramName, t, this);
2005                     newParams.append(param);
2006                     i++;
2007                 }
2008                 params = newParams.toList();
2009             }
2010             Assert.checkNonNull(params);
2011             return params;
2012         }
2013 
2014         public Symbol asMemberOf(Type site, Types types) {
2015             return new MethodSymbol(flags_field, name, types.memberType(site, this), owner);
2016         }
2017 
2018         @DefinedBy(Api.LANGUAGE_MODEL)
2019         public ElementKind getKind() {
2020             if (name == name.table.names.init)
2021                 return ElementKind.CONSTRUCTOR;
2022             else if (name == name.table.names.clinit)
2023                 return ElementKind.STATIC_INIT;
2024             else if ((flags() & BLOCK) != 0)
2025                 return isStatic() ? ElementKind.STATIC_INIT : ElementKind.INSTANCE_INIT;
2026             else
2027                 return ElementKind.METHOD;
2028         }
2029 
2030         public boolean isStaticOrInstanceInit() {
2031             return getKind() == ElementKind.STATIC_INIT ||
2032                     getKind() == ElementKind.INSTANCE_INIT;
2033         }
2034 
2035         @DefinedBy(Api.LANGUAGE_MODEL)
2036         public Attribute getDefaultValue() {
2037             return defaultValue;
2038         }
2039 
2040         @DefinedBy(Api.LANGUAGE_MODEL)
2041         public List<VarSymbol> getParameters() {
2042             return params();
2043         }
2044 
2045         @DefinedBy(Api.LANGUAGE_MODEL)
2046         public boolean isVarArgs() {
2047             return (flags() & VARARGS) != 0;
2048         }
2049 
2050         @DefinedBy(Api.LANGUAGE_MODEL)
2051         public boolean isDefault() {
2052             return (flags() & DEFAULT) != 0;
2053         }
2054 
2055         @DefinedBy(Api.LANGUAGE_MODEL)
2056         public <R, P> R accept(ElementVisitor<R, P> v, P p) {
2057             return v.visitExecutable(this, p);
2058         }
2059 
2060         public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
2061             return v.visitMethodSymbol(this, p);
2062         }
2063 
2064         @DefinedBy(Api.LANGUAGE_MODEL)
2065         public Type getReceiverType() {
2066             Type result = asType().getReceiverType();
2067             return (result == null) ? Type.noType : result;
2068         }
2069 
2070         @DefinedBy(Api.LANGUAGE_MODEL)
2071         public Type getReturnType() {
2072             return asType().getReturnType();
2073         }
2074 
2075         @DefinedBy(Api.LANGUAGE_MODEL)
2076         public List<Type> getThrownTypes() {
2077             return asType().getThrownTypes();
2078         }
2079     }
2080 
2081     /** A class for invokedynamic method calls.
2082      */
2083     public static class DynamicMethodSymbol extends MethodSymbol implements Dynamic {
2084 
2085         public LoadableConstant[] staticArgs;
2086         public MethodHandleSymbol bsm;
2087 
2088         public DynamicMethodSymbol(Name name, Symbol owner, MethodHandleSymbol bsm, Type type, LoadableConstant[] staticArgs) {
2089             super(0, name, type, owner);
2090             this.bsm = bsm;
2091             this.staticArgs = staticArgs;
2092         }
2093 
2094         @Override
2095         public boolean isDynamic() {
2096             return true;
2097         }
2098 
2099         @Override
2100         public LoadableConstant[] staticArgs() {
2101             return staticArgs;
2102         }
2103 
2104         @Override
2105         public MethodHandleSymbol bootstrapMethod() {
2106             return bsm;
2107         }
2108 
2109         @Override
2110         public int poolTag() {
2111             return ClassFile.CONSTANT_InvokeDynamic;
2112         }
2113 
2114         @Override
2115         public Type dynamicType() {
2116             return type;
2117         }
2118     }
2119 
2120     /** A class for condy.
2121      */
2122     public static class DynamicVarSymbol extends VarSymbol implements Dynamic, LoadableConstant {
2123         public LoadableConstant[] staticArgs;
2124         public MethodHandleSymbol bsm;
2125 
2126         public DynamicVarSymbol(Name name, Symbol owner, MethodHandleSymbol bsm, Type type, LoadableConstant[] staticArgs) {
2127             super(0, name, type, owner);
2128             this.bsm = bsm;
2129             this.staticArgs = staticArgs;
2130         }
2131 
2132         @Override
2133         public boolean isDynamic() {
2134             return true;
2135         }
2136 
2137         @Override
2138         public PoolConstant dynamicType() {
2139             return type;
2140         }
2141 
2142         @Override
2143         public LoadableConstant[] staticArgs() {
2144             return staticArgs;
2145         }
2146 
2147         @Override
2148         public LoadableConstant bootstrapMethod() {
2149             return bsm;
2150         }
2151 
2152         @Override
2153         public int poolTag() {
2154             return ClassFile.CONSTANT_Dynamic;
2155         }
2156     }
2157 
2158     /** A class for method handles.
2159      */
2160     public static class MethodHandleSymbol extends MethodSymbol implements LoadableConstant {
2161 
2162         private Symbol refSym;
2163         private boolean getter;
2164 
2165         public MethodHandleSymbol(Symbol msym) {
2166             this(msym, false);
2167         }
2168 
2169         public MethodHandleSymbol(Symbol msym, boolean getter) {
2170             super(msym.flags_field, msym.name, msym.type, msym.owner);
2171             this.refSym = msym;
2172             this.getter = getter;
2173         }
2174 
2175         /**
2176          * Returns the kind associated with this method handle.
2177          */
2178         public int referenceKind() {
2179             if (refSym.kind == VAR) {
2180                 return getter ?
2181                         refSym.isStatic() ? ClassFile.REF_getStatic : ClassFile.REF_getField :
2182                         refSym.isStatic() ? ClassFile.REF_putStatic : ClassFile.REF_putField;
2183             } else {
2184                 if (refSym.isConstructor()) {
2185                     return ClassFile.REF_newInvokeSpecial;
2186                 } else {
2187                     if (refSym.isStatic()) {
2188                         return ClassFile.REF_invokeStatic;
2189                     } else if ((refSym.flags() & PRIVATE) != 0) {
2190                         return ClassFile.REF_invokeSpecial;
2191                     } else if (refSym.enclClass().isInterface()) {
2192                         return ClassFile.REF_invokeInterface;
2193                     } else {
2194                         return ClassFile.REF_invokeVirtual;
2195                     }
2196                 }
2197             }
2198         }
2199 
2200         @Override
2201         public int poolTag() {
2202             return ClassFile.CONSTANT_MethodHandle;
2203         }
2204 
2205         @Override
2206         public Object poolKey(Types types) {
2207             return new Pair<>(baseSymbol(), referenceKind());
2208         }
2209 
2210         @Override
2211         public MethodHandleSymbol asHandle() {
2212             return this;
2213         }
2214 
2215         @Override
2216         public Symbol baseSymbol() {
2217             return refSym;
2218         }
2219 
2220 
2221         @Override
2222         public boolean isHandle() {
2223             return true;
2224         }
2225     }
2226 
2227     /** A class for predefined operators.
2228      */
2229     public static class OperatorSymbol extends MethodSymbol {
2230 
2231         public int opcode;
2232         private int accessCode = Integer.MIN_VALUE;
2233 
2234         public OperatorSymbol(Name name, Type type, int opcode, Symbol owner) {
2235             super(PUBLIC | STATIC, name, type, owner);
2236             this.opcode = opcode;
2237         }
2238 
2239         @Override
2240         public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
2241             return v.visitOperatorSymbol(this, p);
2242         }
2243 
2244         public int getAccessCode(Tag tag) {
2245             if (accessCode != Integer.MIN_VALUE && !tag.isIncOrDecUnaryOp()) {
2246                 return accessCode;
2247             }
2248             accessCode = AccessCode.from(tag, opcode);
2249             return accessCode;
2250         }
2251 
2252         /** Access codes for dereferencing, assignment,
2253          *  and pre/post increment/decrement.
2254 
2255          *  All access codes for accesses to the current class are even.
2256          *  If a member of the superclass should be accessed instead (because
2257          *  access was via a qualified super), add one to the corresponding code
2258          *  for the current class, making the number odd.
2259          *  This numbering scheme is used by the backend to decide whether
2260          *  to issue an invokevirtual or invokespecial call.
2261          *
2262          *  @see Gen#visitSelect(JCFieldAccess tree)
2263          */
2264         public enum AccessCode {
2265             UNKNOWN(-1, Tag.NO_TAG),
2266             DEREF(0, Tag.NO_TAG),
2267             ASSIGN(2, Tag.ASSIGN),
2268             PREINC(4, Tag.PREINC),
2269             PREDEC(6, Tag.PREDEC),
2270             POSTINC(8, Tag.POSTINC),
2271             POSTDEC(10, Tag.POSTDEC),
2272             FIRSTASGOP(12, Tag.NO_TAG);
2273 
2274             public final int code;
2275             public final Tag tag;
2276             public static final int numberOfAccessCodes = (lushrl - ishll + lxor + 2 - iadd) * 2 + FIRSTASGOP.code + 2;
2277 
2278             AccessCode(int code, Tag tag) {
2279                 this.code = code;
2280                 this.tag = tag;
2281             }
2282 
2283             static public AccessCode getFromCode(int code) {
2284                 for (AccessCode aCodes : AccessCode.values()) {
2285                     if (aCodes.code == code) {
2286                         return aCodes;
2287                     }
2288                 }
2289                 return UNKNOWN;
2290             }
2291 
2292             static int from(Tag tag, int opcode) {
2293                 /** Map bytecode of binary operation to access code of corresponding
2294                 *  assignment operation. This is always an even number.
2295                 */
2296                 switch (tag) {
2297                     case PREINC:
2298                         return AccessCode.PREINC.code;
2299                     case PREDEC:
2300                         return AccessCode.PREDEC.code;
2301                     case POSTINC:
2302                         return AccessCode.POSTINC.code;
2303                     case POSTDEC:
2304                         return AccessCode.POSTDEC.code;
2305                 }
2306                 if (iadd <= opcode && opcode <= lxor) {
2307                     return (opcode - iadd) * 2 + FIRSTASGOP.code;
2308                 } else if (opcode == string_add) {
2309                     return (lxor + 1 - iadd) * 2 + FIRSTASGOP.code;
2310                 } else if (ishll <= opcode && opcode <= lushrl) {
2311                     return (opcode - ishll + lxor + 2 - iadd) * 2 + FIRSTASGOP.code;
2312                 }
2313                 return -1;
2314             }
2315         }
2316     }
2317 
2318     /** Symbol completer interface.
2319      */
2320     public static interface Completer {
2321 
2322         /** Dummy completer to be used when the symbol has been completed or
2323          * does not need completion.
2324          */
2325         public final static Completer NULL_COMPLETER = new Completer() {
2326             public void complete(Symbol sym) { }
2327             public boolean isTerminal() { return true; }
2328         };
2329 
2330         void complete(Symbol sym) throws CompletionFailure;
2331 
2332         /** Returns true if this completer is <em>terminal</em>. A terminal
2333          * completer is used as a place holder when the symbol is completed.
2334          * Calling complete on a terminal completer will not affect the symbol.
2335          *
2336          * The dummy NULL_COMPLETER and the GraphDependencies completer are
2337          * examples of terminal completers.
2338          *
2339          * @return true iff this completer is terminal
2340          */
2341         default boolean isTerminal() {
2342             return false;
2343         }
2344     }
2345 
2346     public static class CompletionFailure extends RuntimeException {
2347         private static final long serialVersionUID = 0;
2348         public final DeferredCompletionFailureHandler dcfh;
2349         public Symbol sym;
2350 
2351         /** A diagnostic object describing the failure
2352          */
2353         private JCDiagnostic diag;
2354 
2355         private Supplier<JCDiagnostic> diagSupplier;
2356 
2357         public CompletionFailure(Symbol sym, Supplier<JCDiagnostic> diagSupplier, DeferredCompletionFailureHandler dcfh) {
2358             this.dcfh = dcfh;
2359             this.sym = sym;
2360             this.diagSupplier = diagSupplier;
2361 //          this.printStackTrace();//DEBUG
2362         }
2363 
2364         public JCDiagnostic getDiagnostic() {
2365             if (diag == null && diagSupplier != null) {
2366                 diag = diagSupplier.get();
2367             }
2368             return diag;
2369         }
2370 
2371         @Override
2372         public String getMessage() {
2373             return getDiagnostic().getMessage(null);
2374         }
2375 
2376         public JCDiagnostic getDetailValue() {
2377             return getDiagnostic();
2378         }
2379 
2380         @Override
2381         public CompletionFailure initCause(Throwable cause) {
2382             super.initCause(cause);
2383             return this;
2384         }
2385 
2386         public void resetDiagnostic(Supplier<JCDiagnostic> diagSupplier) {
2387             this.diagSupplier = diagSupplier;
2388             this.diag = null;
2389         }
2390 
2391     }
2392 
2393     /**
2394      * A visitor for symbols.  A visitor is used to implement operations
2395      * (or relations) on symbols.  Most common operations on types are
2396      * binary relations and this interface is designed for binary
2397      * relations, that is, operations on the form
2398      * Symbol&nbsp;&times;&nbsp;P&nbsp;&rarr;&nbsp;R.
2399      * <!-- In plain text: Type x P -> R -->
2400      *
2401      * @param <R> the return type of the operation implemented by this
2402      * visitor; use Void if no return type is needed.
2403      * @param <P> the type of the second argument (the first being the
2404      * symbol itself) of the operation implemented by this visitor; use
2405      * Void if a second argument is not needed.
2406      */
2407     public interface Visitor<R,P> {
2408         R visitClassSymbol(ClassSymbol s, P arg);
2409         R visitMethodSymbol(MethodSymbol s, P arg);
2410         R visitPackageSymbol(PackageSymbol s, P arg);
2411         R visitOperatorSymbol(OperatorSymbol s, P arg);
2412         R visitVarSymbol(VarSymbol s, P arg);
2413         R visitTypeSymbol(TypeSymbol s, P arg);
2414         R visitSymbol(Symbol s, P arg);
2415     }
2416 }