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