提交 cb629213 编写于 作者: J jfranck

7015104: use new subtype of TypeSymbol for type parameters

Reviewed-by: jjg, mcimadamore
上级 7881ecdb
......@@ -496,10 +496,11 @@ public abstract class Symbol implements Element {
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 @@ public abstract class Symbol implements Element {
}
}
/** 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 @@ public abstract class Symbol implements Element {
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 @@ public abstract class Symbol implements Element {
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);
}
}
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);
/**
* 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(Symbol.Visitor<R, P> v, P p) {
return v.visitTypeSymbol(this, p);
public ElementKind getKind() {
return ElementKind.TYPE_PARAMETER;
}
@Override
public Symbol getGenericElement() {
return owner;
}
public List<Type> getBounds() {
......@@ -658,6 +656,11 @@ public abstract class Symbol implements Element {
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 abstract class Symbol implements Element {
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 abstract class Symbol implements Element {
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);
......
......@@ -404,12 +404,11 @@ public class Symtab {
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);
......
......@@ -1145,7 +1145,7 @@ public class Type implements PrimitiveType {
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;
}
......
......@@ -262,7 +262,7 @@ public class Infer {
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;
......
/*
* 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 @@ public class CompoundScopeTest {
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);
......
/*
* 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 class TypeHarness {
}
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;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册