提交 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 { ...@@ -496,10 +496,11 @@ public abstract class Symbol implements Element {
return List.nil(); return List.nil();
} }
public List<TypeSymbol> getTypeParameters() { public List<TypeVariableSymbol> getTypeParameters() {
ListBuffer<TypeSymbol> l = ListBuffer.lb(); ListBuffer<TypeVariableSymbol> l = ListBuffer.lb();
for (Type t : type.getTypeArguments()) { 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(); return l.toList();
} }
...@@ -546,19 +547,12 @@ public abstract class Symbol implements Element { ...@@ -546,19 +547,12 @@ public abstract class Symbol implements Element {
} }
} }
/** A class for type symbols. Type variables are represented by instances /** A base class for Symbols representing types.
* of this class, classes and packages by instances of subclasses.
*/ */
public static class TypeSymbol public static abstract class TypeSymbol extends Symbol {
extends Symbol implements TypeParameterElement { public TypeSymbol(int kind, long flags, Name name, Type type, Symbol owner) {
// Implements TypeParameterElement because type parameters don't super(kind, flags, name, type, owner);
// 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);
} }
/** form a fully qualified name from a name and an owner /** form a fully qualified name from a name and an owner
*/ */
static public Name formFullName(Name name, Symbol owner) { static public Name formFullName(Name name, Symbol owner) {
...@@ -610,11 +604,7 @@ public abstract class Symbol implements Element { ...@@ -610,11 +604,7 @@ public abstract class Symbol implements Element {
return this.type.hasTag(TYPEVAR); return this.type.hasTag(TYPEVAR);
} }
// For type params; overridden in subclasses. @Override
public ElementKind getKind() {
return ElementKind.TYPE_PARAMETER;
}
public java.util.List<Symbol> getEnclosedElements() { public java.util.List<Symbol> getEnclosedElements() {
List<Symbol> list = List.nil(); List<Symbol> list = List.nil();
if (kind == TYP && type.hasTag(TYPEVAR)) { if (kind == TYP && type.hasTag(TYPEVAR)) {
...@@ -627,21 +617,29 @@ public abstract class Symbol implements Element { ...@@ -627,21 +617,29 @@ public abstract class Symbol implements Element {
return list; return list;
} }
// For type params. @Override
// Perhaps not needed if getEnclosingElement can be spec'ed public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
// to do the same thing. return v.visitTypeSymbol(this, p);
// TODO: getGenericElement() might not be needed
public Symbol getGenericElement() {
return owner;
} }
}
public <R, P> R accept(ElementVisitor<R, P> v, P p) { /**
Assert.check(type.hasTag(TYPEVAR)); // else override will be invoked * Type variables are represented by instances of this class.
return v.visitTypeParameter(this, p); */
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) { public ElementKind getKind() {
return v.visitTypeSymbol(this, p); return ElementKind.TYPE_PARAMETER;
}
@Override
public Symbol getGenericElement() {
return owner;
} }
public List<Type> getBounds() { public List<Type> getBounds() {
...@@ -658,6 +656,11 @@ public abstract class Symbol implements Element { ...@@ -658,6 +656,11 @@ public abstract class Symbol implements Element {
return ct.interfaces_field; 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 /** A class for package symbols
...@@ -670,8 +673,7 @@ public abstract class Symbol implements Element { ...@@ -670,8 +673,7 @@ public abstract class Symbol implements Element {
public ClassSymbol package_info; // see bug 6443073 public ClassSymbol package_info; // see bug 6443073
public PackageSymbol(Name name, Type type, Symbol owner) { public PackageSymbol(Name name, Type type, Symbol owner) {
super(0, name, type, owner); super(PCK, 0, name, type, owner);
this.kind = PCK;
this.members_field = null; this.members_field = null;
this.fullname = formFullName(name, owner); this.fullname = formFullName(name, owner);
} }
...@@ -783,7 +785,7 @@ public abstract class Symbol implements Element { ...@@ -783,7 +785,7 @@ public abstract class Symbol implements Element {
public Pool pool; public Pool pool;
public ClassSymbol(long flags, Name name, Type type, Symbol owner) { 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.members_field = null;
this.fullname = formFullName(name, owner); this.fullname = formFullName(name, owner);
this.flatname = formFlatName(name, owner); this.flatname = formFlatName(name, owner);
......
...@@ -404,12 +404,11 @@ public class Symtab { ...@@ -404,12 +404,11 @@ public class Symtab {
return messages.getLocalizedString("compiler.misc.unnamed.package"); 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) { public <R, P> R accept(ElementVisitor<R, P> v, P p) {
return v.visitUnknown(this, p); return v.visitUnknown(this, p);
} }
}; };
noSymbol.kind = Kinds.NIL;
// create the error symbols // create the error symbols
errSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.any, null, rootPackage); errSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.any, null, rootPackage);
......
...@@ -1145,7 +1145,7 @@ public class Type implements PrimitiveType { ...@@ -1145,7 +1145,7 @@ public class Type implements PrimitiveType {
public TypeVar(Name name, Symbol owner, Type lower) { public TypeVar(Name name, Symbol owner, Type lower) {
super(TYPEVAR, null); super(TYPEVAR, null);
tsym = new TypeSymbol(0, name, this, owner); tsym = new TypeVariableSymbol(0, name, this, owner);
this.lower = lower; this.lower = lower;
} }
......
...@@ -262,7 +262,7 @@ public class Infer { ...@@ -262,7 +262,7 @@ public class Infer {
UndetVar uv = (UndetVar)inferenceContext.asFree(t); UndetVar uv = (UndetVar)inferenceContext.asFree(t);
List<Type> upperBounds = uv.getBounds(InferenceBound.UPPER); List<Type> upperBounds = uv.getBounds(InferenceBound.UPPER);
if (Type.containsAny(upperBounds, vars)) { 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); fresh_tvar.type = new TypeVar(fresh_tvar, types.makeCompoundType(uv.getBounds(InferenceBound.UPPER)), null);
todo.append(uv); todo.append(uv);
uv.inst = fresh_tvar.type; 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -147,7 +147,7 @@ public class CompoundScopeTest { ...@@ -147,7 +147,7 @@ public class CompoundScopeTest {
Scope createScope(int nelems) { Scope createScope(int nelems) {
Scope s = new Scope(symtab.noSymbol); Scope s = new Scope(symtab.noSymbol);
for (int i = 0 ; i < nelems ; i++) { 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); s.enter(sym);
elems = elems.prepend(sym); elems = elems.prepend(sym);
List<Symbol> shadowed = shadowedMap.get(sym.name); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -309,7 +309,7 @@ public class TypeHarness { ...@@ -309,7 +309,7 @@ public class TypeHarness {
} }
public TypeVar TypeVariable(Type bound) { 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); tvsym.type = new TypeVar(tvsym, bound, null);
return (TypeVar)tvsym.type; return (TypeVar)tvsym.type;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册