7015104: use new subtype of TypeSymbol for type parameters
Reviewed-by: jjg, mcimadamore
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Symbol.java Thu Apr 11 19:15:56 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Symbol.java Fri Apr 12 12:05:04 2013 +0200
@@ -496,10 +496,11 @@
return List.nil();
}
- public List<TypeSymbol> getTypeParameters() {
- ListBuffer<TypeSymbol> l = ListBuffer.lb();
+ public List<TypeVariableSymbol> getTypeParameters() {
+ ListBuffer<TypeVariableSymbol> l = ListBuffer.lb();
for (Type t : type.getTypeArguments()) {
- l.append(t.tsym);
+ Assert.check(t.tsym.getKind() == ElementKind.TYPE_PARAMETER);
+ l.append((TypeVariableSymbol)t.tsym);
}
return l.toList();
}
@@ -546,19 +547,12 @@
}
}
- /** A class for type symbols. Type variables are represented by instances
- * of this class, classes and packages by instances of subclasses.
+ /** A base class for Symbols representing types.
*/
- public static class TypeSymbol
- extends Symbol implements TypeParameterElement {
- // Implements TypeParameterElement because type parameters don't
- // have their own TypeSymbol subclass.
- // TODO: type parameters should have their own TypeSymbol subclass
-
- public TypeSymbol(long flags, Name name, Type type, Symbol owner) {
- super(TYP, flags, name, type, owner);
+ public static abstract class TypeSymbol extends Symbol {
+ public TypeSymbol(int kind, long flags, Name name, Type type, Symbol owner) {
+ super(kind, flags, name, type, owner);
}
-
/** form a fully qualified name from a name and an owner
*/
static public Name formFullName(Name name, Symbol owner) {
@@ -610,11 +604,7 @@
return this.type.hasTag(TYPEVAR);
}
- // For type params; overridden in subclasses.
- public ElementKind getKind() {
- return ElementKind.TYPE_PARAMETER;
- }
-
+ @Override
public java.util.List<Symbol> getEnclosedElements() {
List<Symbol> list = List.nil();
if (kind == TYP && type.hasTag(TYPEVAR)) {
@@ -627,21 +617,29 @@
return list;
}
- // For type params.
- // Perhaps not needed if getEnclosingElement can be spec'ed
- // to do the same thing.
- // TODO: getGenericElement() might not be needed
- public Symbol getGenericElement() {
- return owner;
+ @Override
+ public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
+ return v.visitTypeSymbol(this, p);
+ }
+ }
+
+ /**
+ * Type variables are represented by instances of this class.
+ */
+ public static class TypeVariableSymbol
+ extends TypeSymbol implements TypeParameterElement {
+
+ public TypeVariableSymbol(long flags, Name name, Type type, Symbol owner) {
+ super(TYP, flags, name, type, owner);
}
- public <R, P> R accept(ElementVisitor<R, P> v, P p) {
- Assert.check(type.hasTag(TYPEVAR)); // else override will be invoked
- return v.visitTypeParameter(this, p);
+ public ElementKind getKind() {
+ return ElementKind.TYPE_PARAMETER;
}
- public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
- return v.visitTypeSymbol(this, p);
+ @Override
+ public Symbol getGenericElement() {
+ return owner;
}
public List<Type> getBounds() {
@@ -658,6 +656,11 @@
return ct.interfaces_field;
}
}
+
+ @Override
+ public <R, P> R accept(ElementVisitor<R, P> v, P p) {
+ return v.visitTypeParameter(this, p);
+ }
}
/** A class for package symbols
@@ -670,8 +673,7 @@
public ClassSymbol package_info; // see bug 6443073
public PackageSymbol(Name name, Type type, Symbol owner) {
- super(0, name, type, owner);
- this.kind = PCK;
+ super(PCK, 0, name, type, owner);
this.members_field = null;
this.fullname = formFullName(name, owner);
}
@@ -783,7 +785,7 @@
public Pool pool;
public ClassSymbol(long flags, Name name, Type type, Symbol owner) {
- super(flags, name, type, owner);
+ super(TYP, flags, name, type, owner);
this.members_field = null;
this.fullname = formFullName(name, owner);
this.flatname = formFlatName(name, owner);
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Symtab.java Thu Apr 11 19:15:56 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Symtab.java Fri Apr 12 12:05:04 2013 +0200
@@ -404,12 +404,11 @@
return messages.getLocalizedString("compiler.misc.unnamed.package");
}
};
- noSymbol = new TypeSymbol(0, names.empty, Type.noType, rootPackage) {
+ noSymbol = new TypeSymbol(Kinds.NIL, 0, names.empty, Type.noType, rootPackage) {
public <R, P> R accept(ElementVisitor<R, P> v, P p) {
return v.visitUnknown(this, p);
}
};
- noSymbol.kind = Kinds.NIL;
// create the error symbols
errSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.any, null, rootPackage);
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Type.java Thu Apr 11 19:15:56 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Type.java Fri Apr 12 12:05:04 2013 +0200
@@ -1145,7 +1145,7 @@
public TypeVar(Name name, Symbol owner, Type lower) {
super(TYPEVAR, null);
- tsym = new TypeSymbol(0, name, this, owner);
+ tsym = new TypeVariableSymbol(0, name, this, owner);
this.lower = lower;
}
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Infer.java Thu Apr 11 19:15:56 2013 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Infer.java Fri Apr 12 12:05:04 2013 +0200
@@ -262,7 +262,7 @@
UndetVar uv = (UndetVar)inferenceContext.asFree(t);
List<Type> upperBounds = uv.getBounds(InferenceBound.UPPER);
if (Type.containsAny(upperBounds, vars)) {
- TypeSymbol fresh_tvar = new TypeSymbol(Flags.SYNTHETIC, uv.qtype.tsym.name, null, uv.qtype.tsym.owner);
+ TypeSymbol fresh_tvar = new TypeVariableSymbol(Flags.SYNTHETIC, uv.qtype.tsym.name, null, uv.qtype.tsym.owner);
fresh_tvar.type = new TypeVar(fresh_tvar, types.makeCompoundType(uv.getBounds(InferenceBound.UPPER)), null);
todo.append(uv);
uv.inst = fresh_tvar.type;
--- a/langtools/test/tools/javac/scope/7017664/CompoundScopeTest.java Thu Apr 11 19:15:56 2013 -0700
+++ b/langtools/test/tools/javac/scope/7017664/CompoundScopeTest.java Fri Apr 12 12:05:04 2013 +0200
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -147,7 +147,7 @@
Scope createScope(int nelems) {
Scope s = new Scope(symtab.noSymbol);
for (int i = 0 ; i < nelems ; i++) {
- Symbol sym = new TypeSymbol(0, names.fromString("s" + i), null, null);
+ Symbol sym = new TypeVariableSymbol(0, names.fromString("s" + i), null, null);
s.enter(sym);
elems = elems.prepend(sym);
List<Symbol> shadowed = shadowedMap.get(sym.name);
--- a/langtools/test/tools/javac/types/TypeHarness.java Thu Apr 11 19:15:56 2013 -0700
+++ b/langtools/test/tools/javac/types/TypeHarness.java Fri Apr 12 12:05:04 2013 +0200
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -309,7 +309,7 @@
}
public TypeVar TypeVariable(Type bound) {
- TypeSymbol tvsym = new TypeSymbol(0, syntheticName(), null, predef.noSymbol);
+ TypeSymbol tvsym = new TypeVariableSymbol(0, syntheticName(), null, predef.noSymbol);
tvsym.type = new TypeVar(tvsym, bound, null);
return (TypeVar)tvsym.type;
}