Fri, 12 Apr 2013 12:05:04 +0200
7015104: use new subtype of TypeSymbol for type parameters
Reviewed-by: jjg, mcimadamore
1.1 --- a/src/share/classes/com/sun/tools/javac/code/Symbol.java Thu Apr 11 19:15:56 2013 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Symbol.java Fri Apr 12 12:05:04 2013 +0200 1.3 @@ -496,10 +496,11 @@ 1.4 return List.nil(); 1.5 } 1.6 1.7 - public List<TypeSymbol> getTypeParameters() { 1.8 - ListBuffer<TypeSymbol> l = ListBuffer.lb(); 1.9 + public List<TypeVariableSymbol> getTypeParameters() { 1.10 + ListBuffer<TypeVariableSymbol> l = ListBuffer.lb(); 1.11 for (Type t : type.getTypeArguments()) { 1.12 - l.append(t.tsym); 1.13 + Assert.check(t.tsym.getKind() == ElementKind.TYPE_PARAMETER); 1.14 + l.append((TypeVariableSymbol)t.tsym); 1.15 } 1.16 return l.toList(); 1.17 } 1.18 @@ -546,19 +547,12 @@ 1.19 } 1.20 } 1.21 1.22 - /** A class for type symbols. Type variables are represented by instances 1.23 - * of this class, classes and packages by instances of subclasses. 1.24 + /** A base class for Symbols representing types. 1.25 */ 1.26 - public static class TypeSymbol 1.27 - extends Symbol implements TypeParameterElement { 1.28 - // Implements TypeParameterElement because type parameters don't 1.29 - // have their own TypeSymbol subclass. 1.30 - // TODO: type parameters should have their own TypeSymbol subclass 1.31 - 1.32 - public TypeSymbol(long flags, Name name, Type type, Symbol owner) { 1.33 - super(TYP, flags, name, type, owner); 1.34 + public static abstract class TypeSymbol extends Symbol { 1.35 + public TypeSymbol(int kind, long flags, Name name, Type type, Symbol owner) { 1.36 + super(kind, flags, name, type, owner); 1.37 } 1.38 - 1.39 /** form a fully qualified name from a name and an owner 1.40 */ 1.41 static public Name formFullName(Name name, Symbol owner) { 1.42 @@ -610,11 +604,7 @@ 1.43 return this.type.hasTag(TYPEVAR); 1.44 } 1.45 1.46 - // For type params; overridden in subclasses. 1.47 - public ElementKind getKind() { 1.48 - return ElementKind.TYPE_PARAMETER; 1.49 - } 1.50 - 1.51 + @Override 1.52 public java.util.List<Symbol> getEnclosedElements() { 1.53 List<Symbol> list = List.nil(); 1.54 if (kind == TYP && type.hasTag(TYPEVAR)) { 1.55 @@ -627,23 +617,31 @@ 1.56 return list; 1.57 } 1.58 1.59 - // For type params. 1.60 - // Perhaps not needed if getEnclosingElement can be spec'ed 1.61 - // to do the same thing. 1.62 - // TODO: getGenericElement() might not be needed 1.63 + @Override 1.64 + public <R, P> R accept(Symbol.Visitor<R, P> v, P p) { 1.65 + return v.visitTypeSymbol(this, p); 1.66 + } 1.67 + } 1.68 + 1.69 + /** 1.70 + * Type variables are represented by instances of this class. 1.71 + */ 1.72 + public static class TypeVariableSymbol 1.73 + extends TypeSymbol implements TypeParameterElement { 1.74 + 1.75 + public TypeVariableSymbol(long flags, Name name, Type type, Symbol owner) { 1.76 + super(TYP, flags, name, type, owner); 1.77 + } 1.78 + 1.79 + public ElementKind getKind() { 1.80 + return ElementKind.TYPE_PARAMETER; 1.81 + } 1.82 + 1.83 + @Override 1.84 public Symbol getGenericElement() { 1.85 return owner; 1.86 } 1.87 1.88 - public <R, P> R accept(ElementVisitor<R, P> v, P p) { 1.89 - Assert.check(type.hasTag(TYPEVAR)); // else override will be invoked 1.90 - return v.visitTypeParameter(this, p); 1.91 - } 1.92 - 1.93 - public <R, P> R accept(Symbol.Visitor<R, P> v, P p) { 1.94 - return v.visitTypeSymbol(this, p); 1.95 - } 1.96 - 1.97 public List<Type> getBounds() { 1.98 TypeVar t = (TypeVar)type; 1.99 Type bound = t.getUpperBound(); 1.100 @@ -658,6 +656,11 @@ 1.101 return ct.interfaces_field; 1.102 } 1.103 } 1.104 + 1.105 + @Override 1.106 + public <R, P> R accept(ElementVisitor<R, P> v, P p) { 1.107 + return v.visitTypeParameter(this, p); 1.108 + } 1.109 } 1.110 1.111 /** A class for package symbols 1.112 @@ -670,8 +673,7 @@ 1.113 public ClassSymbol package_info; // see bug 6443073 1.114 1.115 public PackageSymbol(Name name, Type type, Symbol owner) { 1.116 - super(0, name, type, owner); 1.117 - this.kind = PCK; 1.118 + super(PCK, 0, name, type, owner); 1.119 this.members_field = null; 1.120 this.fullname = formFullName(name, owner); 1.121 } 1.122 @@ -783,7 +785,7 @@ 1.123 public Pool pool; 1.124 1.125 public ClassSymbol(long flags, Name name, Type type, Symbol owner) { 1.126 - super(flags, name, type, owner); 1.127 + super(TYP, flags, name, type, owner); 1.128 this.members_field = null; 1.129 this.fullname = formFullName(name, owner); 1.130 this.flatname = formFlatName(name, owner);
2.1 --- a/src/share/classes/com/sun/tools/javac/code/Symtab.java Thu Apr 11 19:15:56 2013 -0700 2.2 +++ b/src/share/classes/com/sun/tools/javac/code/Symtab.java Fri Apr 12 12:05:04 2013 +0200 2.3 @@ -404,12 +404,11 @@ 2.4 return messages.getLocalizedString("compiler.misc.unnamed.package"); 2.5 } 2.6 }; 2.7 - noSymbol = new TypeSymbol(0, names.empty, Type.noType, rootPackage) { 2.8 + noSymbol = new TypeSymbol(Kinds.NIL, 0, names.empty, Type.noType, rootPackage) { 2.9 public <R, P> R accept(ElementVisitor<R, P> v, P p) { 2.10 return v.visitUnknown(this, p); 2.11 } 2.12 }; 2.13 - noSymbol.kind = Kinds.NIL; 2.14 2.15 // create the error symbols 2.16 errSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.any, null, rootPackage);
3.1 --- a/src/share/classes/com/sun/tools/javac/code/Type.java Thu Apr 11 19:15:56 2013 -0700 3.2 +++ b/src/share/classes/com/sun/tools/javac/code/Type.java Fri Apr 12 12:05:04 2013 +0200 3.3 @@ -1145,7 +1145,7 @@ 3.4 3.5 public TypeVar(Name name, Symbol owner, Type lower) { 3.6 super(TYPEVAR, null); 3.7 - tsym = new TypeSymbol(0, name, this, owner); 3.8 + tsym = new TypeVariableSymbol(0, name, this, owner); 3.9 this.lower = lower; 3.10 } 3.11
4.1 --- a/src/share/classes/com/sun/tools/javac/comp/Infer.java Thu Apr 11 19:15:56 2013 -0700 4.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Infer.java Fri Apr 12 12:05:04 2013 +0200 4.3 @@ -262,7 +262,7 @@ 4.4 UndetVar uv = (UndetVar)inferenceContext.asFree(t); 4.5 List<Type> upperBounds = uv.getBounds(InferenceBound.UPPER); 4.6 if (Type.containsAny(upperBounds, vars)) { 4.7 - TypeSymbol fresh_tvar = new TypeSymbol(Flags.SYNTHETIC, uv.qtype.tsym.name, null, uv.qtype.tsym.owner); 4.8 + TypeSymbol fresh_tvar = new TypeVariableSymbol(Flags.SYNTHETIC, uv.qtype.tsym.name, null, uv.qtype.tsym.owner); 4.9 fresh_tvar.type = new TypeVar(fresh_tvar, types.makeCompoundType(uv.getBounds(InferenceBound.UPPER)), null); 4.10 todo.append(uv); 4.11 uv.inst = fresh_tvar.type;
5.1 --- a/test/tools/javac/scope/7017664/CompoundScopeTest.java Thu Apr 11 19:15:56 2013 -0700 5.2 +++ b/test/tools/javac/scope/7017664/CompoundScopeTest.java Fri Apr 12 12:05:04 2013 +0200 5.3 @@ -1,5 +1,5 @@ 5.4 /* 5.5 - * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 5.6 + * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. 5.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.8 * 5.9 * This code is free software; you can redistribute it and/or modify it 5.10 @@ -147,7 +147,7 @@ 5.11 Scope createScope(int nelems) { 5.12 Scope s = new Scope(symtab.noSymbol); 5.13 for (int i = 0 ; i < nelems ; i++) { 5.14 - Symbol sym = new TypeSymbol(0, names.fromString("s" + i), null, null); 5.15 + Symbol sym = new TypeVariableSymbol(0, names.fromString("s" + i), null, null); 5.16 s.enter(sym); 5.17 elems = elems.prepend(sym); 5.18 List<Symbol> shadowed = shadowedMap.get(sym.name);
6.1 --- a/test/tools/javac/types/TypeHarness.java Thu Apr 11 19:15:56 2013 -0700 6.2 +++ b/test/tools/javac/types/TypeHarness.java Fri Apr 12 12:05:04 2013 +0200 6.3 @@ -1,5 +1,5 @@ 6.4 /* 6.5 - * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. 6.6 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 6.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.8 * 6.9 * This code is free software; you can redistribute it and/or modify it 6.10 @@ -309,7 +309,7 @@ 6.11 } 6.12 6.13 public TypeVar TypeVariable(Type bound) { 6.14 - TypeSymbol tvsym = new TypeSymbol(0, syntheticName(), null, predef.noSymbol); 6.15 + TypeSymbol tvsym = new TypeVariableSymbol(0, syntheticName(), null, predef.noSymbol); 6.16 tvsym.type = new TypeVar(tvsym, bound, null); 6.17 return (TypeVar)tvsym.type; 6.18 }