duke@1: /* duke@1: * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. Sun designates this duke@1: * particular file as subject to the "Classpath" exception as provided duke@1: * by Sun in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or duke@1: * have any questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.code; duke@1: duke@1: import java.util.ArrayList; duke@1: import java.util.Collections; duke@1: import java.util.Set; duke@1: import java.util.concurrent.Callable; duke@1: import javax.lang.model.element.*; duke@1: import javax.lang.model.type.ReferenceType; duke@1: import javax.lang.model.type.TypeMirror; duke@1: import javax.tools.JavaFileObject; duke@1: duke@1: import com.sun.tools.javac.util.*; duke@1: import com.sun.tools.javac.util.Name; duke@1: import com.sun.tools.javac.code.Type.*; duke@1: import com.sun.tools.javac.comp.Attr; duke@1: import com.sun.tools.javac.comp.AttrContext; duke@1: import com.sun.tools.javac.comp.Env; duke@1: import com.sun.tools.javac.jvm.*; duke@1: import com.sun.tools.javac.model.*; duke@1: import com.sun.tools.javac.tree.JCTree; duke@1: duke@1: import static com.sun.tools.javac.code.Flags.*; duke@1: import static com.sun.tools.javac.code.Kinds.*; duke@1: import static com.sun.tools.javac.code.TypeTags.*; duke@1: duke@1: /** Root class for Java symbols. It contains subclasses duke@1: * for specific sorts of symbols, such as variables, methods and operators, duke@1: * types, packages. Each subclass is represented as a static inner class duke@1: * inside Symbol. duke@1: * duke@1: *

This is NOT part of any API supported by Sun Microsystems. If duke@1: * you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. duke@1: */ duke@1: public abstract class Symbol implements Element { duke@1: // public Throwable debug = new Throwable(); duke@1: duke@1: /** The kind of this symbol. duke@1: * @see Kinds duke@1: */ duke@1: public int kind; duke@1: duke@1: /** The flags of this symbol. duke@1: */ duke@1: public long flags_field; duke@1: duke@1: /** An accessor method for the flags of this symbol. duke@1: * Flags of class symbols should be accessed through the accessor duke@1: * method to make sure that the class symbol is loaded. duke@1: */ duke@1: public long flags() { return flags_field; } duke@1: duke@1: /** The attributes of this symbol. duke@1: */ duke@1: public List attributes_field; duke@1: duke@1: /** An accessor method for the attributes of this symbol. duke@1: * Attributes of class symbols should be accessed through the accessor duke@1: * method to make sure that the class symbol is loaded. duke@1: */ duke@1: public List getAnnotationMirrors() { duke@1: assert attributes_field != null; duke@1: return attributes_field; duke@1: } duke@1: duke@1: /** Fetch a particular annotation from a symbol. */ duke@1: public Attribute.Compound attribute(Symbol anno) { duke@1: for (Attribute.Compound a : getAnnotationMirrors()) duke@1: if (a.type.tsym == anno) return a; duke@1: return null; duke@1: } duke@1: duke@1: /** The name of this symbol in Utf8 representation. duke@1: */ duke@1: public Name name; duke@1: duke@1: /** The type of this symbol. duke@1: */ duke@1: public Type type; duke@1: duke@1: /** The owner of this symbol. duke@1: */ duke@1: public Symbol owner; duke@1: duke@1: /** The completer of this symbol. duke@1: */ duke@1: public Completer completer; duke@1: duke@1: /** A cache for the type erasure of this symbol. duke@1: */ duke@1: public Type erasure_field; duke@1: duke@1: /** Construct a symbol with given kind, flags, name, type and owner. duke@1: */ duke@1: public Symbol(int kind, long flags, Name name, Type type, Symbol owner) { duke@1: this.kind = kind; duke@1: this.flags_field = flags; duke@1: this.type = type; duke@1: this.owner = owner; duke@1: this.completer = null; duke@1: this.erasure_field = null; duke@1: this.attributes_field = List.nil(); duke@1: this.name = name; duke@1: } duke@1: duke@1: /** Clone this symbol with new owner. duke@1: * Legal only for fields and methods. duke@1: */ duke@1: public Symbol clone(Symbol newOwner) { duke@1: throw new AssertionError(); duke@1: } duke@1: duke@1: /** The Java source which this symbol represents. duke@1: * A description of this symbol; overrides Object. duke@1: */ duke@1: public String toString() { duke@1: return name.toString(); duke@1: } duke@1: duke@1: /** A Java source description of the location of this symbol; used for duke@1: * error reporting. Use of this method may result in the loss of the duke@1: * symbol's description. duke@1: */ duke@1: public String location() { duke@1: if (owner.name == null || (owner.name.len == 0 && owner.kind != PCK)) { duke@1: return ""; duke@1: } duke@1: return owner.toString(); duke@1: } duke@1: duke@1: public String location(Type site, Types types) { duke@1: if (owner.name == null || owner.name.len == 0) { duke@1: return location(); duke@1: } duke@1: if (owner.type.tag == CLASS) { duke@1: Type ownertype = types.asOuterSuper(site, owner); duke@1: if (ownertype != null) return ownertype.toString(); duke@1: } duke@1: return owner.toString(); duke@1: } duke@1: duke@1: /** The symbol's erased type. duke@1: */ duke@1: public Type erasure(Types types) { duke@1: if (erasure_field == null) duke@1: erasure_field = types.erasure(type); duke@1: return erasure_field; duke@1: } duke@1: duke@1: /** The external type of a symbol. This is the symbol's erased type duke@1: * except for constructors of inner classes which get the enclosing duke@1: * instance class added as first argument. duke@1: */ duke@1: public Type externalType(Types types) { duke@1: Type t = erasure(types); duke@1: if (name == name.table.init && owner.hasOuterInstance()) { duke@1: Type outerThisType = types.erasure(owner.type.getEnclosingType()); duke@1: return new MethodType(t.getParameterTypes().prepend(outerThisType), duke@1: t.getReturnType(), duke@1: t.getThrownTypes(), duke@1: t.tsym); duke@1: } else { duke@1: return t; duke@1: } duke@1: } duke@1: duke@1: public boolean isStatic() { duke@1: return duke@1: (flags() & STATIC) != 0 || duke@1: (owner.flags() & INTERFACE) != 0 && kind != MTH; duke@1: } duke@1: duke@1: public boolean isInterface() { duke@1: return (flags() & INTERFACE) != 0; duke@1: } duke@1: duke@1: /** Is this symbol declared (directly or indirectly) local duke@1: * to a method or variable initializer? duke@1: * Also includes fields of inner classes which are in duke@1: * turn local to a method or variable initializer. duke@1: */ duke@1: public boolean isLocal() { duke@1: return duke@1: (owner.kind & (VAR | MTH)) != 0 || duke@1: (owner.kind == TYP && owner.isLocal()); duke@1: } duke@1: duke@1: /** Is this symbol a constructor? duke@1: */ duke@1: public boolean isConstructor() { duke@1: return name == name.table.init; duke@1: } duke@1: duke@1: /** The fully qualified name of this symbol. duke@1: * This is the same as the symbol's name except for class symbols, duke@1: * which are handled separately. duke@1: */ duke@1: public Name getQualifiedName() { duke@1: return name; duke@1: } duke@1: duke@1: /** The fully qualified name of this symbol after converting to flat duke@1: * representation. This is the same as the symbol's name except for duke@1: * class symbols, which are handled separately. duke@1: */ duke@1: public Name flatName() { duke@1: return getQualifiedName(); duke@1: } duke@1: duke@1: /** If this is a class or package, its members, otherwise null. duke@1: */ duke@1: public Scope members() { duke@1: return null; duke@1: } duke@1: duke@1: /** A class is an inner class if it it has an enclosing instance class. duke@1: */ duke@1: public boolean isInner() { duke@1: return type.getEnclosingType().tag == CLASS; duke@1: } duke@1: duke@1: /** An inner class has an outer instance if it is not an interface duke@1: * it has an enclosing instance class which might be referenced from the class. duke@1: * Nested classes can see instance members of their enclosing class. duke@1: * Their constructors carry an additional this$n parameter, inserted duke@1: * implicitly by the compiler. duke@1: * duke@1: * @see #isInner duke@1: */ duke@1: public boolean hasOuterInstance() { duke@1: return duke@1: type.getEnclosingType().tag == CLASS && (flags() & (INTERFACE | NOOUTERTHIS)) == 0; duke@1: } duke@1: duke@1: /** The closest enclosing class of this symbol's declaration. duke@1: */ duke@1: public ClassSymbol enclClass() { duke@1: Symbol c = this; duke@1: while (c != null && duke@1: ((c.kind & TYP) == 0 || c.type.tag != CLASS)) { duke@1: c = c.owner; duke@1: } duke@1: return (ClassSymbol)c; duke@1: } duke@1: duke@1: /** The outermost class which indirectly owns this symbol. duke@1: */ duke@1: public ClassSymbol outermostClass() { duke@1: Symbol sym = this; duke@1: Symbol prev = null; duke@1: while (sym.kind != PCK) { duke@1: prev = sym; duke@1: sym = sym.owner; duke@1: } duke@1: return (ClassSymbol) prev; duke@1: } duke@1: duke@1: /** The package which indirectly owns this symbol. duke@1: */ duke@1: public PackageSymbol packge() { duke@1: Symbol sym = this; duke@1: while (sym.kind != PCK) { duke@1: sym = sym.owner; duke@1: } duke@1: return (PackageSymbol) sym; duke@1: } duke@1: duke@1: /** Is this symbol a subclass of `base'? Only defined for ClassSymbols. duke@1: */ duke@1: public boolean isSubClass(Symbol base, Types types) { duke@1: throw new AssertionError("isSubClass " + this); duke@1: } duke@1: duke@1: /** Fully check membership: hierarchy, protection, and hiding. duke@1: * Does not exclude methods not inherited due to overriding. duke@1: */ duke@1: public boolean isMemberOf(TypeSymbol clazz, Types types) { duke@1: return duke@1: owner == clazz || duke@1: clazz.isSubClass(owner, types) && duke@1: isInheritedIn(clazz, types) && duke@1: !hiddenIn((ClassSymbol)clazz, types); duke@1: } duke@1: duke@1: /** Is this symbol the same as or enclosed by the given class? */ duke@1: public boolean isEnclosedBy(ClassSymbol clazz) { duke@1: for (Symbol sym = this; sym.kind != PCK; sym = sym.owner) duke@1: if (sym == clazz) return true; duke@1: return false; duke@1: } duke@1: duke@1: /** Check for hiding. Note that this doesn't handle multiple duke@1: * (interface) inheritance. */ duke@1: private boolean hiddenIn(ClassSymbol clazz, Types types) { duke@1: if (kind == MTH && (flags() & STATIC) == 0) return false; duke@1: while (true) { duke@1: if (owner == clazz) return false; duke@1: Scope.Entry e = clazz.members().lookup(name); duke@1: while (e.scope != null) { duke@1: if (e.sym == this) return false; duke@1: if (e.sym.kind == kind && duke@1: (kind != MTH || duke@1: (e.sym.flags() & STATIC) != 0 && duke@1: types.isSubSignature(e.sym.type, type))) duke@1: return true; duke@1: e = e.next(); duke@1: } duke@1: Type superType = types.supertype(clazz.type); duke@1: if (superType.tag != TypeTags.CLASS) return false; duke@1: clazz = (ClassSymbol)superType.tsym; duke@1: } duke@1: } duke@1: duke@1: /** Is this symbol inherited into a given class? duke@1: * PRE: If symbol's owner is a interface, duke@1: * it is already assumed that the interface is a superinterface duke@1: * of given class. duke@1: * @param clazz The class for which we want to establish membership. duke@1: * This must be a subclass of the member's owner. duke@1: */ duke@1: public boolean isInheritedIn(Symbol clazz, Types types) { duke@1: switch ((int)(flags_field & Flags.AccessFlags)) { duke@1: default: // error recovery duke@1: case PUBLIC: duke@1: return true; duke@1: case PRIVATE: duke@1: return this.owner == clazz; duke@1: case PROTECTED: duke@1: // we model interfaces as extending Object duke@1: return (clazz.flags() & INTERFACE) == 0; duke@1: case 0: duke@1: PackageSymbol thisPackage = this.packge(); duke@1: for (Symbol sup = clazz; duke@1: sup != null && sup != this.owner; duke@1: sup = types.supertype(sup.type).tsym) { duke@1: if (sup.type.isErroneous()) duke@1: return true; // error recovery duke@1: if ((sup.flags() & COMPOUND) != 0) duke@1: continue; duke@1: if (sup.packge() != thisPackage) duke@1: return false; duke@1: } duke@1: return (clazz.flags() & INTERFACE) == 0; duke@1: } duke@1: } duke@1: duke@1: /** The (variable or method) symbol seen as a member of given duke@1: * class type`site' (this might change the symbol's type). duke@1: * This is used exclusively for producing diagnostics. duke@1: */ duke@1: public Symbol asMemberOf(Type site, Types types) { duke@1: throw new AssertionError(); duke@1: } duke@1: duke@1: /** Does this method symbol override `other' symbol, when both are seen as duke@1: * members of class `origin'? It is assumed that _other is a member duke@1: * of origin. duke@1: * duke@1: * It is assumed that both symbols have the same name. The static duke@1: * modifier is ignored for this test. duke@1: * duke@1: * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4 duke@1: */ duke@1: public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) { duke@1: return false; duke@1: } duke@1: duke@1: /** Complete the elaboration of this symbol's definition. duke@1: */ duke@1: public void complete() throws CompletionFailure { duke@1: if (completer != null) { duke@1: Completer c = completer; duke@1: completer = null; duke@1: c.complete(this); duke@1: } duke@1: } duke@1: duke@1: /** True if the symbol represents an entity that exists. duke@1: */ duke@1: public boolean exists() { duke@1: return true; duke@1: } duke@1: duke@1: public Type asType() { duke@1: return type; duke@1: } duke@1: duke@1: public Symbol getEnclosingElement() { duke@1: return owner; duke@1: } duke@1: duke@1: public ElementKind getKind() { duke@1: return ElementKind.OTHER; // most unkind duke@1: } duke@1: duke@1: public Set getModifiers() { duke@1: return Flags.asModifierSet(flags()); duke@1: } duke@1: duke@1: public Name getSimpleName() { duke@1: return name; duke@1: } duke@1: duke@1: /** duke@1: * @deprecated this method should never be used by javac internally. duke@1: */ duke@1: @Deprecated duke@1: public A getAnnotation(Class annoType) { duke@1: return JavacElements.getAnnotation(this, annoType); duke@1: } duke@1: duke@1: // TODO: getEnclosedElements should return a javac List, fix in FilteredMemberList duke@1: public java.util.List getEnclosedElements() { duke@1: return List.nil(); duke@1: } duke@1: duke@1: public List getTypeParameters() { duke@1: ListBuffer l = ListBuffer.lb(); duke@1: for (Type t : type.getTypeArguments()) { duke@1: l.append(t.tsym); duke@1: } duke@1: return l.toList(); duke@1: } duke@1: duke@1: public static class DelegatedSymbol extends Symbol { duke@1: protected Symbol other; duke@1: public DelegatedSymbol(Symbol other) { duke@1: super(other.kind, other.flags_field, other.name, other.type, other.owner); duke@1: this.other = other; duke@1: } duke@1: public String toString() { return other.toString(); } duke@1: public String location() { return other.location(); } duke@1: public String location(Type site, Types types) { return other.location(site, types); } duke@1: public Type erasure(Types types) { return other.erasure(types); } duke@1: public Type externalType(Types types) { return other.externalType(types); } duke@1: public boolean isLocal() { return other.isLocal(); } duke@1: public boolean isConstructor() { return other.isConstructor(); } duke@1: public Name getQualifiedName() { return other.getQualifiedName(); } duke@1: public Name flatName() { return other.flatName(); } duke@1: public Scope members() { return other.members(); } duke@1: public boolean isInner() { return other.isInner(); } duke@1: public boolean hasOuterInstance() { return other.hasOuterInstance(); } duke@1: public ClassSymbol enclClass() { return other.enclClass(); } duke@1: public ClassSymbol outermostClass() { return other.outermostClass(); } duke@1: public PackageSymbol packge() { return other.packge(); } duke@1: public boolean isSubClass(Symbol base, Types types) { return other.isSubClass(base, types); } duke@1: public boolean isMemberOf(TypeSymbol clazz, Types types) { return other.isMemberOf(clazz, types); } duke@1: public boolean isEnclosedBy(ClassSymbol clazz) { return other.isEnclosedBy(clazz); } duke@1: public boolean isInheritedIn(Symbol clazz, Types types) { return other.isInheritedIn(clazz, types); } duke@1: public Symbol asMemberOf(Type site, Types types) { return other.asMemberOf(site, types); } duke@1: public void complete() throws CompletionFailure { other.complete(); } duke@1: duke@1: public R accept(ElementVisitor v, P p) { duke@1: return other.accept(v, p); duke@1: } duke@1: } duke@1: duke@1: /** A class for type symbols. Type variables are represented by instances duke@1: * of this class, classes and packages by instances of subclasses. duke@1: */ duke@1: public static class TypeSymbol duke@1: extends Symbol implements TypeParameterElement { duke@1: // Implements TypeParameterElement because type parameters don't duke@1: // have their own TypeSymbol subclass. duke@1: // TODO: type parameters should have their own TypeSymbol subclass duke@1: duke@1: public TypeSymbol(long flags, Name name, Type type, Symbol owner) { duke@1: super(TYP, flags, name, type, owner); duke@1: } duke@1: duke@1: /** form a fully qualified name from a name and an owner duke@1: */ duke@1: static public Name formFullName(Name name, Symbol owner) { duke@1: if (owner == null) return name; duke@1: if (((owner.kind != ERR)) && duke@1: ((owner.kind & (VAR | MTH)) != 0 duke@1: || (owner.kind == TYP && owner.type.tag == TYPEVAR) duke@1: )) return name; duke@1: Name prefix = owner.getQualifiedName(); duke@1: if (prefix == null || prefix == prefix.table.empty) duke@1: return name; duke@1: else return prefix.append('.', name); duke@1: } duke@1: duke@1: /** form a fully qualified name from a name and an owner, after duke@1: * converting to flat representation duke@1: */ duke@1: static public Name formFlatName(Name name, Symbol owner) { duke@1: if (owner == null || duke@1: (owner.kind & (VAR | MTH)) != 0 duke@1: || (owner.kind == TYP && owner.type.tag == TYPEVAR) duke@1: ) return name; duke@1: char sep = owner.kind == TYP ? '$' : '.'; duke@1: Name prefix = owner.flatName(); duke@1: if (prefix == null || prefix == prefix.table.empty) duke@1: return name; duke@1: else return prefix.append(sep, name); duke@1: } duke@1: duke@1: /** duke@1: * A total ordering between type symbols that refines the duke@1: * class inheritance graph. duke@1: * duke@1: * Typevariables always precede other kinds of symbols. duke@1: */ duke@1: public final boolean precedes(TypeSymbol that, Types types) { duke@1: if (this == that) duke@1: return false; duke@1: if (this.type.tag == that.type.tag) { duke@1: if (this.type.tag == CLASS) { duke@1: return duke@1: types.rank(that.type) < types.rank(this.type) || duke@1: types.rank(that.type) == types.rank(this.type) && duke@1: that.getQualifiedName().compareTo(this.getQualifiedName()) < 0; duke@1: } else if (this.type.tag == TYPEVAR) { duke@1: return types.isSubtype(this.type, that.type); duke@1: } duke@1: } duke@1: return this.type.tag == TYPEVAR; duke@1: } duke@1: duke@1: // For type params; overridden in subclasses. duke@1: public ElementKind getKind() { duke@1: return ElementKind.TYPE_PARAMETER; duke@1: } duke@1: duke@1: public java.util.List getEnclosedElements() { duke@1: List list = List.nil(); duke@1: for (Scope.Entry e = members().elems; e != null; e = e.sibling) { duke@1: if (e.sym != null && (e.sym.flags() & SYNTHETIC) == 0 && e.sym.owner == this) duke@1: list = list.prepend(e.sym); duke@1: } duke@1: return list; duke@1: } duke@1: duke@1: // For type params. duke@1: // Perhaps not needed if getEnclosingElement can be spec'ed duke@1: // to do the same thing. duke@1: // TODO: getGenericElement() might not be needed duke@1: public Symbol getGenericElement() { duke@1: return owner; duke@1: } duke@1: duke@1: public R accept(ElementVisitor v, P p) { duke@1: assert type.tag == TYPEVAR; // else override will be invoked duke@1: return v.visitTypeParameter(this, p); duke@1: } duke@1: duke@1: public List getBounds() { duke@1: TypeVar t = (TypeVar)type; duke@1: Type bound = t.getUpperBound(); duke@1: if (!bound.isCompound()) duke@1: return List.of(bound); duke@1: ClassType ct = (ClassType)bound; duke@1: if (!ct.tsym.erasure_field.isInterface()) { duke@1: return ct.interfaces_field.prepend(ct.supertype_field); duke@1: } else { duke@1: // No superclass was given in bounds. duke@1: // In this case, supertype is Object, erasure is first interface. duke@1: return ct.interfaces_field; duke@1: } duke@1: } duke@1: } duke@1: duke@1: /** A class for package symbols duke@1: */ duke@1: public static class PackageSymbol extends TypeSymbol duke@1: implements PackageElement { duke@1: duke@1: public Scope members_field; duke@1: public Name fullname; duke@1: public ClassSymbol package_info; // see bug 6443073 duke@1: duke@1: public PackageSymbol(Name name, Type type, Symbol owner) { duke@1: super(0, name, type, owner); duke@1: this.kind = PCK; duke@1: this.members_field = null; duke@1: this.fullname = formFullName(name, owner); duke@1: } duke@1: duke@1: public PackageSymbol(Name name, Symbol owner) { duke@1: this(name, null, owner); duke@1: this.type = new PackageType(this); duke@1: } duke@1: duke@1: public String toString() { duke@1: return fullname.toString(); duke@1: } duke@1: duke@1: public Name getQualifiedName() { duke@1: return fullname; duke@1: } duke@1: duke@1: public boolean isUnnamed() { duke@1: return name.isEmpty() && owner != null; duke@1: } duke@1: duke@1: public Scope members() { duke@1: if (completer != null) complete(); duke@1: return members_field; duke@1: } duke@1: duke@1: public long flags() { duke@1: if (completer != null) complete(); duke@1: return flags_field; duke@1: } duke@1: duke@1: public List getAnnotationMirrors() { duke@1: if (completer != null) complete(); duke@1: assert attributes_field != null; duke@1: return attributes_field; duke@1: } duke@1: duke@1: /** A package "exists" if a type or package that exists has duke@1: * been seen within it. duke@1: */ duke@1: public boolean exists() { duke@1: return (flags_field & EXISTS) != 0; duke@1: } duke@1: duke@1: public ElementKind getKind() { duke@1: return ElementKind.PACKAGE; duke@1: } duke@1: duke@1: public Symbol getEnclosingElement() { duke@1: return null; duke@1: } duke@1: duke@1: public R accept(ElementVisitor v, P p) { duke@1: return v.visitPackage(this, p); duke@1: } duke@1: } duke@1: duke@1: /** A class for class symbols duke@1: */ duke@1: public static class ClassSymbol extends TypeSymbol implements TypeElement { duke@1: duke@1: /** a scope for all class members; variables, methods and inner classes duke@1: * type parameters are not part of this scope duke@1: */ duke@1: public Scope members_field; duke@1: duke@1: /** the fully qualified name of the class, i.e. pck.outer.inner. duke@1: * null for anonymous classes duke@1: */ duke@1: public Name fullname; duke@1: duke@1: /** the fully qualified name of the class after converting to flat duke@1: * representation, i.e. pck.outer$inner, duke@1: * set externally for local and anonymous classes duke@1: */ duke@1: public Name flatname; duke@1: duke@1: /** the sourcefile where the class came from duke@1: */ duke@1: public JavaFileObject sourcefile; duke@1: duke@1: /** the classfile from where to load this class duke@1: * this will have extension .class or .java duke@1: */ duke@1: public JavaFileObject classfile; duke@1: duke@1: /** the constant pool of the class duke@1: */ duke@1: public Pool pool; duke@1: duke@1: public ClassSymbol(long flags, Name name, Type type, Symbol owner) { duke@1: super(flags, name, type, owner); duke@1: this.members_field = null; duke@1: this.fullname = formFullName(name, owner); duke@1: this.flatname = formFlatName(name, owner); duke@1: this.sourcefile = null; duke@1: this.classfile = null; duke@1: this.pool = null; duke@1: } duke@1: duke@1: public ClassSymbol(long flags, Name name, Symbol owner) { duke@1: this( duke@1: flags, duke@1: name, duke@1: new ClassType(Type.noType, null, null), duke@1: owner); duke@1: this.type.tsym = this; duke@1: } duke@1: duke@1: /** The Java source which this symbol represents. duke@1: */ duke@1: public String toString() { duke@1: return className(); duke@1: } duke@1: duke@1: public long flags() { duke@1: if (completer != null) complete(); duke@1: return flags_field; duke@1: } duke@1: duke@1: public Scope members() { duke@1: if (completer != null) complete(); duke@1: return members_field; duke@1: } duke@1: duke@1: public List getAnnotationMirrors() { duke@1: if (completer != null) complete(); duke@1: assert attributes_field != null; duke@1: return attributes_field; duke@1: } duke@1: duke@1: public Type erasure(Types types) { duke@1: if (erasure_field == null) duke@1: erasure_field = new ClassType(types.erasure(type.getEnclosingType()), duke@1: List.nil(), this); duke@1: return erasure_field; duke@1: } duke@1: duke@1: public String className() { duke@1: if (name.len == 0) duke@1: return duke@1: Log.getLocalizedString("anonymous.class", flatname); duke@1: else duke@1: return fullname.toString(); duke@1: } duke@1: duke@1: public Name getQualifiedName() { duke@1: return fullname; duke@1: } duke@1: duke@1: public Name flatName() { duke@1: return flatname; duke@1: } duke@1: duke@1: public boolean isSubClass(Symbol base, Types types) { duke@1: if (this == base) { duke@1: return true; duke@1: } else if ((base.flags() & INTERFACE) != 0) { duke@1: for (Type t = type; t.tag == CLASS; t = types.supertype(t)) duke@1: for (List is = types.interfaces(t); duke@1: is.nonEmpty(); duke@1: is = is.tail) duke@1: if (is.head.tsym.isSubClass(base, types)) return true; duke@1: } else { duke@1: for (Type t = type; t.tag == CLASS; t = types.supertype(t)) duke@1: if (t.tsym == base) return true; duke@1: } duke@1: return false; duke@1: } duke@1: duke@1: /** Complete the elaboration of this symbol's definition. duke@1: */ duke@1: public void complete() throws CompletionFailure { duke@1: try { duke@1: super.complete(); duke@1: } catch (CompletionFailure ex) { duke@1: // quiet error recovery duke@1: flags_field |= (PUBLIC|STATIC); duke@1: this.type = new ErrorType(this); duke@1: throw ex; duke@1: } duke@1: } duke@1: duke@1: public List getInterfaces() { duke@1: complete(); duke@1: if (type instanceof ClassType) { duke@1: ClassType t = (ClassType)type; duke@1: if (t.interfaces_field == null) // FIXME: shouldn't be null duke@1: t.interfaces_field = List.nil(); duke@1: return t.interfaces_field; duke@1: } else { duke@1: return List.nil(); duke@1: } duke@1: } duke@1: duke@1: public Type getSuperclass() { duke@1: complete(); duke@1: if (type instanceof ClassType) { duke@1: ClassType t = (ClassType)type; duke@1: if (t.supertype_field == null) // FIXME: shouldn't be null duke@1: t.supertype_field = Type.noType; duke@1: // An interface has no superclass; its supertype is Object. duke@1: return t.isInterface() duke@1: ? Type.noType duke@1: : t.supertype_field; duke@1: } else { duke@1: return Type.noType; duke@1: } duke@1: } duke@1: duke@1: public ElementKind getKind() { duke@1: long flags = flags(); duke@1: if ((flags & ANNOTATION) != 0) duke@1: return ElementKind.ANNOTATION_TYPE; duke@1: else if ((flags & INTERFACE) != 0) duke@1: return ElementKind.INTERFACE; duke@1: else if ((flags & ENUM) != 0) duke@1: return ElementKind.ENUM; duke@1: else duke@1: return ElementKind.CLASS; duke@1: } duke@1: duke@1: public NestingKind getNestingKind() { duke@1: complete(); duke@1: if (owner.kind == PCK) duke@1: return NestingKind.TOP_LEVEL; duke@1: else if (name.isEmpty()) duke@1: return NestingKind.ANONYMOUS; duke@1: else if (owner.kind == MTH) duke@1: return NestingKind.LOCAL; duke@1: else duke@1: return NestingKind.MEMBER; duke@1: } duke@1: duke@1: /** duke@1: * @deprecated this method should never be used by javac internally. duke@1: */ duke@1: @Override @Deprecated duke@1: public A getAnnotation(Class annoType) { duke@1: return JavacElements.getAnnotation(this, annoType); duke@1: } duke@1: duke@1: public R accept(ElementVisitor v, P p) { duke@1: return v.visitType(this, p); duke@1: } duke@1: } duke@1: duke@1: duke@1: /** A class for variable symbols duke@1: */ duke@1: public static class VarSymbol extends Symbol implements VariableElement { duke@1: duke@1: /** The variable's declaration position. duke@1: */ duke@1: public int pos = Position.NOPOS; duke@1: duke@1: /** The variable's address. Used for different purposes during duke@1: * flow analysis, translation and code generation. duke@1: * Flow analysis: duke@1: * If this is a blank final or local variable, its sequence number. duke@1: * Translation: duke@1: * If this is a private field, its access number. duke@1: * Code generation: duke@1: * If this is a local variable, its logical slot number. duke@1: */ duke@1: public int adr = -1; duke@1: duke@1: /** Construct a variable symbol, given its flags, name, type and owner. duke@1: */ duke@1: public VarSymbol(long flags, Name name, Type type, Symbol owner) { duke@1: super(VAR, flags, name, type, owner); duke@1: } duke@1: duke@1: /** Clone this symbol with new owner. duke@1: */ duke@1: public VarSymbol clone(Symbol newOwner) { duke@1: VarSymbol v = new VarSymbol(flags_field, name, type, newOwner); duke@1: v.pos = pos; duke@1: v.adr = adr; duke@1: v.data = data; duke@1: // System.out.println("clone " + v + " in " + newOwner);//DEBUG duke@1: return v; duke@1: } duke@1: duke@1: public String toString() { duke@1: return name.toString(); duke@1: } duke@1: duke@1: public Symbol asMemberOf(Type site, Types types) { duke@1: return new VarSymbol(flags_field, name, types.memberType(site, this), owner); duke@1: } duke@1: duke@1: public ElementKind getKind() { duke@1: long flags = flags(); duke@1: if ((flags & PARAMETER) != 0) { duke@1: if (isExceptionParameter()) duke@1: return ElementKind.EXCEPTION_PARAMETER; duke@1: else duke@1: return ElementKind.PARAMETER; duke@1: } else if ((flags & ENUM) != 0) { duke@1: return ElementKind.ENUM_CONSTANT; duke@1: } else if (owner.kind == TYP || owner.kind == ERR) { duke@1: return ElementKind.FIELD; duke@1: } else { duke@1: return ElementKind.LOCAL_VARIABLE; duke@1: } duke@1: } duke@1: duke@1: public R accept(ElementVisitor v, P p) { duke@1: return v.visitVariable(this, p); duke@1: } duke@1: duke@1: public Object getConstantValue() { // Mirror API duke@1: return Constants.decode(getConstValue(), type); duke@1: } duke@1: duke@1: public void setLazyConstValue(final Env env, duke@1: final Log log, duke@1: final Attr attr, duke@1: final JCTree.JCExpression initializer) duke@1: { duke@1: setData(new Callable() { duke@1: public Object call() { duke@1: JavaFileObject source = log.useSource(env.toplevel.sourcefile); duke@1: try { duke@1: // In order to catch self-references, we set duke@1: // the variable's declaration position to duke@1: // maximal possible value, effectively marking duke@1: // the variable as undefined. duke@1: int pos = VarSymbol.this.pos; duke@1: VarSymbol.this.pos = Position.MAXPOS; duke@1: Type itype = attr.attribExpr(initializer, env, type); duke@1: VarSymbol.this.pos = pos; duke@1: if (itype.constValue() != null) duke@1: return attr.coerce(itype, type).constValue(); duke@1: else duke@1: return null; duke@1: } finally { duke@1: log.useSource(source); duke@1: } duke@1: } duke@1: }); duke@1: } duke@1: duke@1: /** duke@1: * The variable's constant value, if this is a constant. duke@1: * Before the constant value is evaluated, it points to an duke@1: * initalizer environment. If this is not a constant, it can duke@1: * be used for other stuff. duke@1: */ duke@1: private Object data; duke@1: duke@1: public boolean isExceptionParameter() { duke@1: return data == ElementKind.EXCEPTION_PARAMETER; duke@1: } duke@1: duke@1: public Object getConstValue() { duke@1: // TODO: Consider if getConstValue and getConstantValue can be collapsed duke@1: if (data == ElementKind.EXCEPTION_PARAMETER) { duke@1: return null; duke@1: } else if (data instanceof Callable) { duke@1: // In this case, this is final a variable, with an as duke@1: // yet unevaluated initializer. duke@1: Callable eval = (Callable)data; duke@1: data = null; // to make sure we don't evaluate this twice. duke@1: try { duke@1: data = eval.call(); duke@1: } catch (Exception ex) { duke@1: throw new AssertionError(ex); duke@1: } duke@1: } duke@1: return data; duke@1: } duke@1: duke@1: public void setData(Object data) { duke@1: assert !(data instanceof Env) : this; duke@1: this.data = data; duke@1: } duke@1: } duke@1: duke@1: /** A class for method symbols. duke@1: */ duke@1: public static class MethodSymbol extends Symbol implements ExecutableElement { duke@1: duke@1: /** The code of the method. */ duke@1: public Code code = null; duke@1: duke@1: /** The parameters of the method. */ duke@1: public List params = null; duke@1: duke@1: /** The names of the parameters */ duke@1: public List savedParameterNames; duke@1: duke@1: /** For an attribute field accessor, its default value if any. duke@1: * The value is null if none appeared in the method duke@1: * declaration. duke@1: */ duke@1: public Attribute defaultValue = null; duke@1: duke@1: /** Construct a method symbol, given its flags, name, type and owner. duke@1: */ duke@1: public MethodSymbol(long flags, Name name, Type type, Symbol owner) { duke@1: super(MTH, flags, name, type, owner); duke@1: assert owner.type.tag != TYPEVAR : owner + "." + name; duke@1: } duke@1: duke@1: /** Clone this symbol with new owner. duke@1: */ duke@1: public MethodSymbol clone(Symbol newOwner) { duke@1: MethodSymbol m = new MethodSymbol(flags_field, name, type, newOwner); duke@1: m.code = code; duke@1: return m; duke@1: } duke@1: duke@1: /** The Java source which this symbol represents. duke@1: */ duke@1: public String toString() { duke@1: if ((flags() & BLOCK) != 0) { duke@1: return owner.name.toString(); duke@1: } else { duke@1: String s = (name == name.table.init) duke@1: ? owner.name.toString() duke@1: : name.toString(); duke@1: if (type != null) { duke@1: if (type.tag == FORALL) duke@1: s = "<" + ((ForAll)type).getTypeArguments() + ">" + s; duke@1: s += "(" + type.argtypes((flags() & VARARGS) != 0) + ")"; duke@1: } duke@1: return s; duke@1: } duke@1: } duke@1: duke@1: /** find a symbol that this (proxy method) symbol implements. duke@1: * @param c The class whose members are searched for duke@1: * implementations duke@1: */ duke@1: public Symbol implemented(TypeSymbol c, Types types) { duke@1: Symbol impl = null; duke@1: for (List is = types.interfaces(c.type); duke@1: impl == null && is.nonEmpty(); duke@1: is = is.tail) { duke@1: TypeSymbol i = is.head.tsym; duke@1: for (Scope.Entry e = i.members().lookup(name); duke@1: impl == null && e.scope != null; duke@1: e = e.next()) { duke@1: if (this.overrides(e.sym, (TypeSymbol)owner, types, true) && duke@1: // FIXME: I suspect the following requires a duke@1: // subst() for a parametric return type. duke@1: types.isSameType(type.getReturnType(), duke@1: types.memberType(owner.type, e.sym).getReturnType())) { duke@1: impl = e.sym; duke@1: } duke@1: if (impl == null) duke@1: impl = implemented(i, types); duke@1: } duke@1: } duke@1: return impl; duke@1: } duke@1: duke@1: /** Will the erasure of this method be considered by the VM to duke@1: * override the erasure of the other when seen from class `origin'? duke@1: */ duke@1: public boolean binaryOverrides(Symbol _other, TypeSymbol origin, Types types) { duke@1: if (isConstructor() || _other.kind != MTH) return false; duke@1: duke@1: if (this == _other) return true; duke@1: MethodSymbol other = (MethodSymbol)_other; duke@1: duke@1: // check for a direct implementation duke@1: if (other.isOverridableIn((TypeSymbol)owner) && duke@1: types.asSuper(owner.type, other.owner) != null && duke@1: types.isSameType(erasure(types), other.erasure(types))) duke@1: return true; duke@1: duke@1: // check for an inherited implementation duke@1: return duke@1: (flags() & ABSTRACT) == 0 && duke@1: other.isOverridableIn(origin) && duke@1: this.isMemberOf(origin, types) && duke@1: types.isSameType(erasure(types), other.erasure(types)); duke@1: } duke@1: duke@1: /** The implementation of this (abstract) symbol in class origin, duke@1: * from the VM's point of view, null if method does not have an duke@1: * implementation in class. duke@1: * @param origin The class of which the implementation is a member. duke@1: */ duke@1: public MethodSymbol binaryImplementation(ClassSymbol origin, Types types) { duke@1: for (TypeSymbol c = origin; c != null; c = types.supertype(c.type).tsym) { duke@1: for (Scope.Entry e = c.members().lookup(name); duke@1: e.scope != null; duke@1: e = e.next()) { duke@1: if (e.sym.kind == MTH && duke@1: ((MethodSymbol)e.sym).binaryOverrides(this, origin, types)) duke@1: return (MethodSymbol)e.sym; duke@1: } duke@1: } duke@1: return null; duke@1: } duke@1: duke@1: /** Does this symbol override `other' symbol, when both are seen as duke@1: * members of class `origin'? It is assumed that _other is a member duke@1: * of origin. duke@1: * duke@1: * It is assumed that both symbols have the same name. The static duke@1: * modifier is ignored for this test. duke@1: * duke@1: * See JLS 8.4.6.1 (without transitivity) and 8.4.6.4 duke@1: */ duke@1: public boolean overrides(Symbol _other, TypeSymbol origin, Types types, boolean checkResult) { duke@1: if (isConstructor() || _other.kind != MTH) return false; duke@1: duke@1: if (this == _other) return true; duke@1: MethodSymbol other = (MethodSymbol)_other; duke@1: duke@1: // check for a direct implementation duke@1: if (other.isOverridableIn((TypeSymbol)owner) && duke@1: types.asSuper(owner.type, other.owner) != null) { duke@1: Type mt = types.memberType(owner.type, this); duke@1: Type ot = types.memberType(owner.type, other); duke@1: if (types.isSubSignature(mt, ot)) { duke@1: if (!checkResult) duke@1: return true; duke@1: if (types.returnTypeSubstitutable(mt, ot)) duke@1: return true; duke@1: } duke@1: } duke@1: duke@1: // check for an inherited implementation duke@1: if ((flags() & ABSTRACT) != 0 || duke@1: (other.flags() & ABSTRACT) == 0 || duke@1: !other.isOverridableIn(origin) || duke@1: !this.isMemberOf(origin, types)) duke@1: return false; duke@1: duke@1: // assert types.asSuper(origin.type, other.owner) != null; duke@1: Type mt = types.memberType(origin.type, this); duke@1: Type ot = types.memberType(origin.type, other); duke@1: return duke@1: types.isSubSignature(mt, ot) && duke@1: (!checkResult || types.resultSubtype(mt, ot, Warner.noWarnings)); duke@1: } duke@1: duke@1: private boolean isOverridableIn(TypeSymbol origin) { duke@1: // JLS3 8.4.6.1 duke@1: switch ((int)(flags_field & Flags.AccessFlags)) { duke@1: case Flags.PRIVATE: duke@1: return false; duke@1: case Flags.PUBLIC: duke@1: return true; duke@1: case Flags.PROTECTED: duke@1: return (origin.flags() & INTERFACE) == 0; duke@1: case 0: duke@1: // for package private: can only override in the same duke@1: // package duke@1: return duke@1: this.packge() == origin.packge() && duke@1: (origin.flags() & INTERFACE) == 0; duke@1: default: duke@1: return false; duke@1: } duke@1: } duke@1: duke@1: /** The implementation of this (abstract) symbol in class origin; duke@1: * null if none exists. Synthetic methods are not considered duke@1: * as possible implementations. duke@1: */ duke@1: public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) { duke@1: for (Type t = origin.type; t.tag == CLASS; t = types.supertype(t)) { duke@1: TypeSymbol c = t.tsym; duke@1: for (Scope.Entry e = c.members().lookup(name); duke@1: e.scope != null; duke@1: e = e.next()) { duke@1: if (e.sym.kind == MTH) { duke@1: MethodSymbol m = (MethodSymbol) e.sym; duke@1: if (m.overrides(this, origin, types, checkResult) && duke@1: (m.flags() & SYNTHETIC) == 0) duke@1: return m; duke@1: } duke@1: } duke@1: } duke@1: // if origin is derived from a raw type, we might have missed duke@1: // an implementation because we do not know enough about instantiations. duke@1: // in this case continue with the supertype as origin. duke@1: if (types.isDerivedRaw(origin.type)) duke@1: return implementation(types.supertype(origin.type).tsym, types, checkResult); duke@1: else duke@1: return null; duke@1: } duke@1: duke@1: public List params() { duke@1: owner.complete(); duke@1: if (params == null) { duke@1: List names = savedParameterNames; duke@1: savedParameterNames = null; duke@1: if (names == null) { duke@1: names = List.nil(); duke@1: int i = 0; duke@1: for (Type t : type.getParameterTypes()) duke@1: names = names.prepend(name.table.fromString("arg" + i++)); duke@1: names = names.reverse(); duke@1: } duke@1: ListBuffer buf = new ListBuffer(); duke@1: for (Type t : type.getParameterTypes()) { duke@1: buf.append(new VarSymbol(PARAMETER, names.head, t, this)); duke@1: names = names.tail; duke@1: } duke@1: params = buf.toList(); duke@1: } duke@1: return params; duke@1: } duke@1: duke@1: public Symbol asMemberOf(Type site, Types types) { duke@1: return new MethodSymbol(flags_field, name, types.memberType(site, this), owner); duke@1: } duke@1: duke@1: public ElementKind getKind() { duke@1: if (name == name.table.init) duke@1: return ElementKind.CONSTRUCTOR; duke@1: else if (name == name.table.clinit) duke@1: return ElementKind.STATIC_INIT; duke@1: else duke@1: return ElementKind.METHOD; duke@1: } duke@1: duke@1: public Attribute getDefaultValue() { duke@1: return defaultValue; duke@1: } duke@1: duke@1: public List getParameters() { duke@1: return params(); duke@1: } duke@1: duke@1: public boolean isVarArgs() { duke@1: return (flags() & VARARGS) != 0; duke@1: } duke@1: duke@1: public R accept(ElementVisitor v, P p) { duke@1: return v.visitExecutable(this, p); duke@1: } duke@1: duke@1: public Type getReturnType() { duke@1: return asType().getReturnType(); duke@1: } duke@1: duke@1: public List getThrownTypes() { duke@1: return asType().getThrownTypes(); duke@1: } duke@1: } duke@1: duke@1: /** A class for predefined operators. duke@1: */ duke@1: public static class OperatorSymbol extends MethodSymbol { duke@1: duke@1: public int opcode; duke@1: duke@1: public OperatorSymbol(Name name, Type type, int opcode, Symbol owner) { duke@1: super(PUBLIC | STATIC, name, type, owner); duke@1: this.opcode = opcode; duke@1: } duke@1: } duke@1: duke@1: /** Symbol completer interface. duke@1: */ duke@1: public static interface Completer { duke@1: void complete(Symbol sym) throws CompletionFailure; duke@1: } duke@1: duke@1: public static class CompletionFailure extends RuntimeException { duke@1: private static final long serialVersionUID = 0; duke@1: public Symbol sym; duke@1: duke@1: /** A localized string describing the failure. duke@1: */ duke@1: public String errmsg; duke@1: duke@1: public CompletionFailure(Symbol sym, String errmsg) { duke@1: this.sym = sym; duke@1: this.errmsg = errmsg; duke@1: // this.printStackTrace();//DEBUG duke@1: } duke@1: duke@1: public String getMessage() { duke@1: return errmsg; duke@1: } duke@1: duke@1: @Override duke@1: public CompletionFailure initCause(Throwable cause) { duke@1: super.initCause(cause); duke@1: return this; duke@1: } duke@1: duke@1: } duke@1: }