src/share/classes/com/sun/tools/javac/code/Symbol.java

changeset 1374
c002fdee76fd
parent 1357
c75be5bc5283
child 1393
d7d932236fee
equal deleted inserted replaced
1373:4a1c57a1c410 1374:c002fdee76fd
40 import com.sun.tools.javac.tree.JCTree; 40 import com.sun.tools.javac.tree.JCTree;
41 import com.sun.tools.javac.util.*; 41 import com.sun.tools.javac.util.*;
42 import com.sun.tools.javac.util.Name; 42 import com.sun.tools.javac.util.Name;
43 import static com.sun.tools.javac.code.Flags.*; 43 import static com.sun.tools.javac.code.Flags.*;
44 import static com.sun.tools.javac.code.Kinds.*; 44 import static com.sun.tools.javac.code.Kinds.*;
45 import static com.sun.tools.javac.code.TypeTags.*; 45 import static com.sun.tools.javac.code.TypeTag.CLASS;
46 import static com.sun.tools.javac.code.TypeTag.FORALL;
47 import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
46 48
47 /** Root class for Java symbols. It contains subclasses 49 /** Root class for Java symbols. It contains subclasses
48 * for specific sorts of symbols, such as variables, methods and operators, 50 * for specific sorts of symbols, such as variables, methods and operators,
49 * types, packages. Each subclass is represented as a static inner class 51 * types, packages. Each subclass is represented as a static inner class
50 * inside Symbol. 52 * inside Symbol.
159 161
160 public Symbol location(Type site, Types types) { 162 public Symbol location(Type site, Types types) {
161 if (owner.name == null || owner.name.isEmpty()) { 163 if (owner.name == null || owner.name.isEmpty()) {
162 return location(); 164 return location();
163 } 165 }
164 if (owner.type.tag == CLASS) { 166 if (owner.type.hasTag(CLASS)) {
165 Type ownertype = types.asOuterSuper(site, owner); 167 Type ownertype = types.asOuterSuper(site, owner);
166 if (ownertype != null) return ownertype.tsym; 168 if (ownertype != null) return ownertype.tsym;
167 } 169 }
168 return owner; 170 return owner;
169 } 171 }
254 } 256 }
255 257
256 /** A class is an inner class if it it has an enclosing instance class. 258 /** A class is an inner class if it it has an enclosing instance class.
257 */ 259 */
258 public boolean isInner() { 260 public boolean isInner() {
259 return type.getEnclosingType().tag == CLASS; 261 return type.getEnclosingType().hasTag(CLASS);
260 } 262 }
261 263
262 /** An inner class has an outer instance if it is not an interface 264 /** An inner class has an outer instance if it is not an interface
263 * it has an enclosing instance class which might be referenced from the class. 265 * it has an enclosing instance class which might be referenced from the class.
264 * Nested classes can see instance members of their enclosing class. 266 * Nested classes can see instance members of their enclosing class.
267 * 269 *
268 * @see #isInner 270 * @see #isInner
269 */ 271 */
270 public boolean hasOuterInstance() { 272 public boolean hasOuterInstance() {
271 return 273 return
272 type.getEnclosingType().tag == CLASS && (flags() & (INTERFACE | NOOUTERTHIS)) == 0; 274 type.getEnclosingType().hasTag(CLASS) && (flags() & (INTERFACE | NOOUTERTHIS)) == 0;
273 } 275 }
274 276
275 /** The closest enclosing class of this symbol's declaration. 277 /** The closest enclosing class of this symbol's declaration.
276 */ 278 */
277 public ClassSymbol enclClass() { 279 public ClassSymbol enclClass() {
278 Symbol c = this; 280 Symbol c = this;
279 while (c != null && 281 while (c != null &&
280 ((c.kind & TYP) == 0 || c.type.tag != CLASS)) { 282 ((c.kind & TYP) == 0 || !c.type.hasTag(CLASS))) {
281 c = c.owner; 283 c = c.owner;
282 } 284 }
283 return (ClassSymbol)c; 285 return (ClassSymbol)c;
284 } 286 }
285 287
344 types.isSubSignature(e.sym.type, type))) 346 types.isSubSignature(e.sym.type, type)))
345 return true; 347 return true;
346 e = e.next(); 348 e = e.next();
347 } 349 }
348 Type superType = types.supertype(clazz.type); 350 Type superType = types.supertype(clazz.type);
349 if (superType.tag != TypeTags.CLASS) return false; 351 if (!superType.hasTag(CLASS)) return false;
350 clazz = (ClassSymbol)superType.tsym; 352 clazz = (ClassSymbol)superType.tsym;
351 } 353 }
352 } 354 }
353 355
354 /** Is this symbol inherited into a given class? 356 /** Is this symbol inherited into a given class?
371 case 0: 373 case 0:
372 PackageSymbol thisPackage = this.packge(); 374 PackageSymbol thisPackage = this.packge();
373 for (Symbol sup = clazz; 375 for (Symbol sup = clazz;
374 sup != null && sup != this.owner; 376 sup != null && sup != this.owner;
375 sup = types.supertype(sup.type).tsym) { 377 sup = types.supertype(sup.type).tsym) {
376 while (sup.type.tag == TYPEVAR) 378 while (sup.type.hasTag(TYPEVAR))
377 sup = sup.type.getUpperBound().tsym; 379 sup = sup.type.getUpperBound().tsym;
378 if (sup.type.isErroneous()) 380 if (sup.type.isErroneous())
379 return true; // error recovery 381 return true; // error recovery
380 if ((sup.flags() & COMPOUND) != 0) 382 if ((sup.flags() & COMPOUND) != 0)
381 continue; 383 continue;
518 */ 520 */
519 static public Name formFullName(Name name, Symbol owner) { 521 static public Name formFullName(Name name, Symbol owner) {
520 if (owner == null) return name; 522 if (owner == null) return name;
521 if (((owner.kind != ERR)) && 523 if (((owner.kind != ERR)) &&
522 ((owner.kind & (VAR | MTH)) != 0 524 ((owner.kind & (VAR | MTH)) != 0
523 || (owner.kind == TYP && owner.type.tag == TYPEVAR) 525 || (owner.kind == TYP && owner.type.hasTag(TYPEVAR))
524 )) return name; 526 )) return name;
525 Name prefix = owner.getQualifiedName(); 527 Name prefix = owner.getQualifiedName();
526 if (prefix == null || prefix == prefix.table.names.empty) 528 if (prefix == null || prefix == prefix.table.names.empty)
527 return name; 529 return name;
528 else return prefix.append('.', name); 530 else return prefix.append('.', name);
532 * converting to flat representation 534 * converting to flat representation
533 */ 535 */
534 static public Name formFlatName(Name name, Symbol owner) { 536 static public Name formFlatName(Name name, Symbol owner) {
535 if (owner == null || 537 if (owner == null ||
536 (owner.kind & (VAR | MTH)) != 0 538 (owner.kind & (VAR | MTH)) != 0
537 || (owner.kind == TYP && owner.type.tag == TYPEVAR) 539 || (owner.kind == TYP && owner.type.hasTag(TYPEVAR))
538 ) return name; 540 ) return name;
539 char sep = owner.kind == TYP ? '$' : '.'; 541 char sep = owner.kind == TYP ? '$' : '.';
540 Name prefix = owner.flatName(); 542 Name prefix = owner.flatName();
541 if (prefix == null || prefix == prefix.table.names.empty) 543 if (prefix == null || prefix == prefix.table.names.empty)
542 return name; 544 return name;
551 */ 553 */
552 public final boolean precedes(TypeSymbol that, Types types) { 554 public final boolean precedes(TypeSymbol that, Types types) {
553 if (this == that) 555 if (this == that)
554 return false; 556 return false;
555 if (this.type.tag == that.type.tag) { 557 if (this.type.tag == that.type.tag) {
556 if (this.type.tag == CLASS) { 558 if (this.type.hasTag(CLASS)) {
557 return 559 return
558 types.rank(that.type) < types.rank(this.type) || 560 types.rank(that.type) < types.rank(this.type) ||
559 types.rank(that.type) == types.rank(this.type) && 561 types.rank(that.type) == types.rank(this.type) &&
560 that.getQualifiedName().compareTo(this.getQualifiedName()) < 0; 562 that.getQualifiedName().compareTo(this.getQualifiedName()) < 0;
561 } else if (this.type.tag == TYPEVAR) { 563 } else if (this.type.hasTag(TYPEVAR)) {
562 return types.isSubtype(this.type, that.type); 564 return types.isSubtype(this.type, that.type);
563 } 565 }
564 } 566 }
565 return this.type.tag == TYPEVAR; 567 return this.type.hasTag(TYPEVAR);
566 } 568 }
567 569
568 // For type params; overridden in subclasses. 570 // For type params; overridden in subclasses.
569 public ElementKind getKind() { 571 public ElementKind getKind() {
570 return ElementKind.TYPE_PARAMETER; 572 return ElementKind.TYPE_PARAMETER;
571 } 573 }
572 574
573 public java.util.List<Symbol> getEnclosedElements() { 575 public java.util.List<Symbol> getEnclosedElements() {
574 List<Symbol> list = List.nil(); 576 List<Symbol> list = List.nil();
575 if (kind == TYP && type.tag == TYPEVAR) { 577 if (kind == TYP && type.hasTag(TYPEVAR)) {
576 return list; 578 return list;
577 } 579 }
578 for (Scope.Entry e = members().elems; e != null; e = e.sibling) { 580 for (Scope.Entry e = members().elems; e != null; e = e.sibling) {
579 if (e.sym != null && (e.sym.flags() & SYNTHETIC) == 0 && e.sym.owner == this) 581 if (e.sym != null && (e.sym.flags() & SYNTHETIC) == 0 && e.sym.owner == this)
580 list = list.prepend(e.sym); 582 list = list.prepend(e.sym);
589 public Symbol getGenericElement() { 591 public Symbol getGenericElement() {
590 return owner; 592 return owner;
591 } 593 }
592 594
593 public <R, P> R accept(ElementVisitor<R, P> v, P p) { 595 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
594 Assert.check(type.tag == TYPEVAR); // else override will be invoked 596 Assert.check(type.hasTag(TYPEVAR)); // else override will be invoked
595 return v.visitTypeParameter(this, p); 597 return v.visitTypeParameter(this, p);
596 } 598 }
597 599
598 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) { 600 public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
599 return v.visitTypeSymbol(this, p); 601 return v.visitTypeSymbol(this, p);
796 798
797 public boolean isSubClass(Symbol base, Types types) { 799 public boolean isSubClass(Symbol base, Types types) {
798 if (this == base) { 800 if (this == base) {
799 return true; 801 return true;
800 } else if ((base.flags() & INTERFACE) != 0) { 802 } else if ((base.flags() & INTERFACE) != 0) {
801 for (Type t = type; t.tag == CLASS; t = types.supertype(t)) 803 for (Type t = type; t.hasTag(CLASS); t = types.supertype(t))
802 for (List<Type> is = types.interfaces(t); 804 for (List<Type> is = types.interfaces(t);
803 is.nonEmpty(); 805 is.nonEmpty();
804 is = is.tail) 806 is = is.tail)
805 if (is.head.tsym.isSubClass(base, types)) return true; 807 if (is.head.tsym.isSubClass(base, types)) return true;
806 } else { 808 } else {
807 for (Type t = type; t.tag == CLASS; t = types.supertype(t)) 809 for (Type t = type; t.hasTag(CLASS); t = types.supertype(t))
808 if (t.tsym == base) return true; 810 if (t.tsym == base) return true;
809 } 811 }
810 return false; 812 return false;
811 } 813 }
812 814
1046 1048
1047 /** Construct a method symbol, given its flags, name, type and owner. 1049 /** Construct a method symbol, given its flags, name, type and owner.
1048 */ 1050 */
1049 public MethodSymbol(long flags, Name name, Type type, Symbol owner) { 1051 public MethodSymbol(long flags, Name name, Type type, Symbol owner) {
1050 super(MTH, flags, name, type, owner); 1052 super(MTH, flags, name, type, owner);
1051 if (owner.type.tag == TYPEVAR) Assert.error(owner + "." + name); 1053 if (owner.type.hasTag(TYPEVAR)) Assert.error(owner + "." + name);
1052 } 1054 }
1053 1055
1054 /** Clone this symbol with new owner. 1056 /** Clone this symbol with new owner.
1055 */ 1057 */
1056 public MethodSymbol clone(Symbol newOwner) { 1058 public MethodSymbol clone(Symbol newOwner) {
1072 } else { 1074 } else {
1073 String s = (name == name.table.names.init) 1075 String s = (name == name.table.names.init)
1074 ? owner.name.toString() 1076 ? owner.name.toString()
1075 : name.toString(); 1077 : name.toString();
1076 if (type != null) { 1078 if (type != null) {
1077 if (type.tag == FORALL) 1079 if (type.hasTag(FORALL))
1078 s = "<" + ((ForAll)type).getTypeArguments() + ">" + s; 1080 s = "<" + ((ForAll)type).getTypeArguments() + ">" + s;
1079 s += "(" + type.argtypes((flags() & VARARGS) != 0) + ")"; 1081 s += "(" + type.argtypes((flags() & VARARGS) != 0) + ")";
1080 } 1082 }
1081 return s; 1083 return s;
1082 } 1084 }

mercurial