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