summaryrefslogtreecommitdiff
path: root/sources/scalac/symtab
diff options
context:
space:
mode:
Diffstat (limited to 'sources/scalac/symtab')
-rw-r--r--sources/scalac/symtab/Definitions.java14
-rw-r--r--sources/scalac/symtab/EntryTags.java2
-rw-r--r--sources/scalac/symtab/Symbol.java275
-rw-r--r--sources/scalac/symtab/Type.java2
-rw-r--r--sources/scalac/symtab/classfile/Pickle.java2
-rw-r--r--sources/scalac/symtab/classfile/UnPickle.java11
6 files changed, 157 insertions, 149 deletions
diff --git a/sources/scalac/symtab/Definitions.java b/sources/scalac/symtab/Definitions.java
index e8450ef265..fcafabc1aa 100644
--- a/sources/scalac/symtab/Definitions.java
+++ b/sources/scalac/symtab/Definitions.java
@@ -218,12 +218,13 @@ public class Definitions {
PARTIALFUNCTION_CLASS = getClass(Names.scala_PartialFunction);
// the scala.ANYREF class
- ANYREF_CLASS = new TypeSymbol(
- Kinds.ALIAS, Position.NOPOS, Names.AnyRef.toTypeName(),
+ ANYREF_CLASS = new AliasTypeSymbol(
+ Position.NOPOS, Names.AnyRef.toTypeName(),
SCALA_CLASS, Modifiers.JAVA)
.setInfo(JAVA_OBJECT_TYPE);
+ SCALA_CLASS.members().enter(ANYREF_CLASS);
ANYREF_TYPE = ANYREF_CLASS.typeConstructor().unalias();
- SCALA.members().enter(ANYREF_CLASS);
+ ANYREF_CLASS.primaryConstructor().setInfo(Type.MethodType(Symbol.EMPTY_ARRAY, ANYREF_TYPE));
// the scala.OBJECT class
OBJECT_CLASS = getClass(Names.scala_Object);
@@ -284,11 +285,12 @@ public class Definitions {
// add the java.lang.String class to the scala package
JAVA_STRING_CLASS = getClass(Names.java_lang_String);
JAVA_STRING_TYPE = JAVA_STRING_CLASS.typeConstructor();
- STRING_CLASS = new TypeSymbol(
- Kinds.ALIAS, Position.NOPOS, Names.String.toTypeName(), SCALA_CLASS, 0)
+ STRING_CLASS = new AliasTypeSymbol(
+ Position.NOPOS, Names.String.toTypeName(), SCALA_CLASS, 0)
.setInfo(JAVA_STRING_TYPE);
+ SCALA_CLASS.members().enter(STRING_CLASS);
STRING_TYPE = STRING_CLASS.typeConstructor();
- SCALA.members().enter(STRING_CLASS);
+ STRING_CLASS.primaryConstructor().setInfo(Type.MethodType(Symbol.EMPTY_ARRAY, STRING_TYPE));
SEQ_CLASS = getClass(Names.scala_Seq);
diff --git a/sources/scalac/symtab/EntryTags.java b/sources/scalac/symtab/EntryTags.java
index 512027944b..236733b8bc 100644
--- a/sources/scalac/symtab/EntryTags.java
+++ b/sources/scalac/symtab/EntryTags.java
@@ -17,7 +17,7 @@ public interface EntryTags {
* | 2 TYPENAME len_Nat NameInfo
* | 3 NONEsym len_Nat
* | 4 TYPEsym len_Nat SymbolInfo lobound_Ref
- * | 5 ALIASsym len_Nat SymbolInfo
+ * | 5 ALIASsym len_Nat SymbolInfo constrsym_Ref
* | 6 CLASSsym len_Nat SymbolInfo thistype_Ref constrsym_Ref
* | 7 VALsym len_Nat SymbolInfo [classsym_Ref]
* | 8 EXTref len_Nat name_Ref [owner_Ref]
diff --git a/sources/scalac/symtab/Symbol.java b/sources/scalac/symtab/Symbol.java
index 751aa93863..2e3ebbbf90 100644
--- a/sources/scalac/symtab/Symbol.java
+++ b/sources/scalac/symtab/Symbol.java
@@ -145,8 +145,7 @@ public abstract class Symbol implements Modifiers, Kinds {
|| info == Type.ErrorType
|| info instanceof Type.MethodType
|| info instanceof Type.OverloadedType
- || info instanceof Type.PolyType &&
- ((Type.PolyType)info).result instanceof Type.MethodType
+ || info instanceof Type.PolyType
: "illegal type for " + this + ": " + info;
if (infos == TypeIntervalList.EMPTY) {
infos = new TypeIntervalList(TypeIntervalList.EMPTY);
@@ -1125,42 +1124,111 @@ public class TermSymbol extends Symbol {
}
}
-/** A class for (abstract and alias) type symbols. It has ClassSymbol as a subclass.
+/** A base class for all type symbols.
+ * It has AliasTypeSymbol, AbsTypeSymbol, ClassSymbol as subclasses.
*/
-public class TypeSymbol extends Symbol {
+public abstract class TypeSymbol extends Symbol {
/** A cache for closures
*/
private ClosureIntervalList closures = ClosureIntervalList.EMPTY;
+ /** The symbol's type template */
+ private Type template = null;
+
/** A cache for type constructors
*/
private Type tycon = null;
+ /** The primary constructor of this type */
+ private Symbol constructor;
+
/** Constructor */
public TypeSymbol(int kind, int pos, Name name, Symbol owner, int flags) {
super(kind, pos, name, owner, flags);
+ if (kind != TYPE)
+ this.constructor = TermSymbol.newConstructor(this, flags & ~MODUL);
}
- public static TypeSymbol define(
- int pos, Name name, Symbol owner, int flags, Scope scope) {
- Scope.Entry e = scope.lookupEntry(name);
- if (e.owner == scope && e.sym.isExternal() && e.sym.kind == ALIAS) {
- TypeSymbol sym = (TypeSymbol) e.sym;
- sym.update(pos, flags);
- return sym;
- } else {
- return new TypeSymbol(ALIAS, pos, name, owner, flags);
+ protected void update(int pos, int flags) {
+ super.update(pos, flags);
+ this.template = null;
+ }
+
+ /** copy all fields to `sym'
+ */
+ public void copyTo(Symbol sym) {
+ super.copyTo(sym);
+ if (kind != TYPE) {
+ Symbol symconstr = ((TypeSymbol) sym).constructor;
+ constructor.copyTo(symconstr);
+ if (constructor.isInitialized())
+ symconstr.setInfo(fixConstrType(symconstr.type(), sym));
}
}
- /** Return a fresh symbol with the same fields as this one.
+ protected void copyConstructorInfo(TypeSymbol other) {
+ other.primaryConstructor().setInfo(
+ fixConstrType(
+ primaryConstructor().info().cloneType(
+ primaryConstructor(), other.primaryConstructor()),
+ other));
+ Symbol[] alts = allConstructors().alternativeSymbols();
+ for (int i = 1; i < alts.length; i++) {
+ Symbol constr = other.addConstructor();
+ constr.setInfo(
+ fixConstrType(
+ alts[i].info().cloneType(alts[i], constr),
+ other));
+ }
+ }
+
+ private Type fixConstrType(Type type, Symbol clone) {
+ switch (type) {
+ case MethodType(Symbol[] vparams, Type result):
+ result = fixConstrType(result, clone);
+ return new Type.MethodType(vparams, result);
+ case PolyType(Symbol[] tparams, Type result):
+ result = fixConstrType(result, clone);
+ return new Type.PolyType(tparams, result);
+ case TypeRef(Type pre, Symbol sym, Type[] args):
+ assert sym == this : Debug.show(sym) + " != " + Debug.show(this);
+ return new Type.TypeRef(pre, clone, args);
+ case LazyType():
+ return type;
+ default:
+ throw Debug.abort("unexpected constructor type:" + clone + ":" + type);
+ }
+ }
+
+ /** add a constructor
*/
- public Symbol cloneSymbol(Symbol owner) {
- TypeSymbol other = new TypeSymbol(kind, pos, name, owner, flags);
- if (Global.instance.debug) System.out.println("cloning " + this + this.locationString() + " to " + other + " in phase " + Global.instance.currentPhase.name());
- other.setInfo(info());
- return other;
+ public Symbol addConstructor() {
+ Symbol constr = TermSymbol.newConstructor(this, flags & ~MODUL);
+ constructor = constructor.overloadWith(constr);
+ return constr;
+ }
+
+ /** Get primary constructor */
+ public Symbol primaryConstructor() {
+ return (kind == TYPE) ? Symbol.NONE : constructor.firstAlternative();
+ }
+
+ /** Get all constructors */
+ public Symbol allConstructors() {
+ return (kind == TYPE) ? Symbol.NONE : constructor;
+ }
+
+ /** Get type parameters */
+ public Symbol[] typeParams() {
+ return (kind == TYPE) ? Symbol.EMPTY_ARRAY
+ : primaryConstructor().info().typeParams();
+ }
+
+ /** Get value parameters */
+ public Symbol[] valueParams() {
+ return (kind == CLASS) ? primaryConstructor().info().valueParams()
+ : Symbol.EMPTY_ARRAY;
}
/** Get type constructor */
@@ -1172,12 +1240,26 @@ public class TypeSymbol extends Symbol {
public Symbol setOwner(Symbol owner) {
tycon = null;
+ template = null;
return super.setOwner(owner);
}
/** Get type */
public Type type() {
- return typeConstructor();
+ Symbol[] tparams = typeParams();
+ if (template != null) {
+ switch (template) {
+ case TypeRef(_, _, Type[] targs):
+ if (targs.length == tparams.length)
+ return template;
+ }
+ }
+ if (tparams.length == 0)
+ template = typeConstructor();
+ else
+ template = Type.TypeRef(
+ owner().thisType(), this, type(tparams));
+ return template;
}
/** Get type at phase id */
@@ -1185,20 +1267,6 @@ public class TypeSymbol extends Symbol {
return type();
}
- public Symbol[] typeParams() {
- return type().unalias().typeParams();
- }
-
- /** Get all constructors of class */
- public Symbol allConstructors() {
- return type().unalias().symbol().allConstructors();
- }
-
- /** Get primary constructor of class */
- public Symbol primaryConstructor() {
- return type().unalias().symbol().primaryConstructor();
- }
-
public Type[] closure() {
if (kind == ALIAS) return info().symbol().closure();
int id = currentPhaseId();
@@ -1256,7 +1324,7 @@ public class TypeSymbol extends Symbol {
closures.closure = Symbol.type(closureClasses);
//System.out.println("closure(" + this + ") = " + ArrayApply.toString(closures.closure));//DEBUG
adjustType(type());
- //System.out.println("closure(" + this + ") at " + Global.instance.currentPhase.name() + " = " + ArrayApply.toString(closures.closure));//DEBUG
+ //System.out.println("closure(" + this + ") = " + ArrayApply.toString(closures.closure));//DEBUG
}
//where
@@ -1301,6 +1369,36 @@ public class TypeSymbol extends Symbol {
super.reset(completer);
closures = ClosureIntervalList.EMPTY;
tycon = null;
+ template = null;
+ }
+}
+
+public class AliasTypeSymbol extends TypeSymbol {
+
+ /** Constructor */
+ public AliasTypeSymbol(int pos, Name name, Symbol owner, int flags) {
+ super(ALIAS, pos, name, owner, flags);
+ }
+
+ public static AliasTypeSymbol define(
+ int pos, Name name, Symbol owner, int flags, Scope scope) {
+ Scope.Entry e = scope.lookupEntry(name);
+ if (e.owner == scope && e.sym.isExternal() && e.sym.kind == ALIAS) {
+ AliasTypeSymbol sym = (AliasTypeSymbol) e.sym;
+ sym.update(pos, flags);
+ return sym;
+ } else {
+ return new AliasTypeSymbol(pos, name, owner, flags);
+ }
+ }
+
+ /** Return a fresh symbol with the same fields as this one.
+ */
+ public Symbol cloneSymbol(Symbol owner) {
+ AliasTypeSymbol other = new AliasTypeSymbol(pos, name, owner, flags);
+ other.setInfo(info());
+ copyConstructorInfo(other);
+ return other;
}
}
@@ -1329,24 +1427,16 @@ public class AbsTypeSymbol extends TypeSymbol {
*/
public Symbol cloneSymbol(Symbol owner) {
TypeSymbol other = new AbsTypeSymbol(pos, name, owner, flags);
- if (Global.instance.debug) System.out.println("cloning " + this + this.locationString() + " to " + other + " in phase " + Global.instance.currentPhase.name());
other.setInfo(info());
other.setLoBound(loBound());
return other;
}
- public Symbol[] typeParams() {
- return Symbol.EMPTY_ARRAY;
- }
-
- /** Get all constructors of class */
- public Symbol allConstructors() {
- return Symbol.NONE;
- }
-
- /** Get primary constructor of class */
- public Symbol primaryConstructor() {
- return Symbol.NONE;
+ /** copy all fields to `sym'
+ */
+ public void copyTo(Symbol sym) {
+ super.copyTo(sym);
+ ((AbsTypeSymbol) sym).lobound = lobound;
}
public Type loBound() {
@@ -1367,12 +1457,6 @@ public class ClassSymbol extends TypeSymbol {
/** The mangled class name */
private Name mangled;
- /** The symbol's type template */
- private Type template;
-
- /** The primary constructor of this type */
- private Symbol constructor;
-
/** The module belonging to the class. This means:
* For Java classes, its statics parts.
* For module classes, the corresponding module.
@@ -1394,7 +1478,6 @@ public class ClassSymbol extends TypeSymbol {
*/
public ClassSymbol(int pos, Name name, Symbol owner, int flags) {
super(CLASS, pos, name, owner, flags);
- this.constructor = TermSymbol.newConstructor(this, flags & ~MODUL);
this.mangled = name;
}
@@ -1404,7 +1487,6 @@ public class ClassSymbol extends TypeSymbol {
if (e.owner == scope && e.sym.isExternal() && e.sym.kind == CLASS) {
ClassSymbol sym = (ClassSymbol) e.sym;
sym.update(pos, flags);
- sym.template = null;
return sym;
} else {
return new ClassSymbol(pos, name, owner, flags);
@@ -1433,49 +1515,16 @@ public class ClassSymbol extends TypeSymbol {
ClassSymbol other = new ClassSymbol(pos, name, owner, flags);
other.module = module;
other.setInfo(info());
- other.primaryConstructor().setInfo(
- fixConstrType(
- primaryConstructor().info().cloneType(
- primaryConstructor(), other.primaryConstructor()),
- other));
- Symbol[] alts = allConstructors().alternativeSymbols();
- for (int i = 1; i < alts.length; i++) {
- Symbol constr = other.addConstructor();
- constr.setInfo(
- fixConstrType(
- alts[i].info().cloneType(alts[i], constr),
- other));
- }
+ copyConstructorInfo(other);
other.mangled = mangled;
if (thisSym != this) other.setTypeOfThis(typeOfThis());
return other;
}
- private Type fixConstrType(Type type, Symbol clone) {
- switch (type) {
- case MethodType(Symbol[] vparams, Type result):
- result = fixConstrType(result, clone);
- return new Type.MethodType(vparams, result);
- case PolyType(Symbol[] tparams, Type result):
- result = fixConstrType(result, clone);
- return new Type.PolyType(tparams, result);
- case TypeRef(Type pre, Symbol sym, Type[] args):
- assert sym == this : Debug.show(sym) + " != " + Debug.show(this);
- return new Type.TypeRef(pre, clone, args);
- case LazyType():
- return type;
- default:
- throw Debug.abort("unexpected constructor type:" + clone + ":" + type);
- }
- }
/** copy all fields to `sym'
*/
public void copyTo(Symbol sym) {
super.copyTo(sym);
- Symbol symconstr = ((ClassSymbol) sym).constructor;
- constructor.copyTo(symconstr);
- if (constructor.isInitialized())
- symconstr.setInfo(fixConstrType(symconstr.type(), sym));
if (thisSym != this) sym.setTypeOfThis(typeOfThis());
}
@@ -1522,33 +1571,6 @@ public class ClassSymbol extends TypeSymbol {
}
}
- /** Get type parameters */
- public Symbol[] typeParams() {
- return primaryConstructor().info().typeParams();
- }
-
- public Symbol[] valueParams() {
- return primaryConstructor().info().valueParams();
- }
-
- /** Get type */
- public Type type() {
- if (template == null || template.typeArgs().length != typeParams().length) {
- Symbol[] tparams = typeParams();
- if (tparams.length == 0)
- template = typeConstructor();
- else
- template = Type.TypeRef(
- owner().thisType(), this, type(typeParams()));
- }
- return template;
- }
-
- public Symbol setOwner(Symbol owner) {
- template = null;
- return super.setOwner(owner);
- }
-
public Type thisType() {
return thistp;
}
@@ -1563,24 +1585,6 @@ public class ClassSymbol extends TypeSymbol {
return this;
}
- /** add a constructor
- */
- public Symbol addConstructor() {
- Symbol constr = TermSymbol.newConstructor(this, flags & ~MODUL);
- constructor = constructor.overloadWith(constr);
- return constr;
- }
-
- /** Get primary constructor */
- public Symbol primaryConstructor() {
- return constructor.firstAlternative();
- }
-
- /** Get all constructors */
- public Symbol allConstructors() {
- return constructor;
- }
-
/** Return the next enclosing class */
public Symbol enclClass() {
return this;
@@ -1603,7 +1607,6 @@ public class ClassSymbol extends TypeSymbol {
public void reset(Type completer) {
super.reset(completer);
module().reset(completer);
- template = null;
thisSym = this;
}
}
diff --git a/sources/scalac/symtab/Type.java b/sources/scalac/symtab/Type.java
index b23b9448f9..e64848858d 100644
--- a/sources/scalac/symtab/Type.java
+++ b/sources/scalac/symtab/Type.java
@@ -988,7 +988,7 @@ public class Type implements Modifiers, Kinds, TypeTags, EntryTags {
for (int i = 0; i < baseparams.length; i++) {
if (sym == baseparams[i]) return baseargs[i];
}
- //System.out.println(sym + " " + basesym + " " + ArrayApply.toString(baseparams));//DEBUG
+ System.out.println(sym + " " + basesym + " " + ArrayApply.toString(baseparams));//debug
break;
case ErrorType:
return ErrorType;
diff --git a/sources/scalac/symtab/classfile/Pickle.java b/sources/scalac/symtab/classfile/Pickle.java
index ec75241842..58b2888d79 100644
--- a/sources/scalac/symtab/classfile/Pickle.java
+++ b/sources/scalac/symtab/classfile/Pickle.java
@@ -121,6 +121,7 @@ public class Pickle implements Kinds, Modifiers, EntryTags {
putType(sym.loBound());
break;
case ALIAS:
+ putSymbol(sym.allConstructors());
break;
case CLASS:
putType(sym.typeOfThis());
@@ -308,6 +309,7 @@ public class Pickle implements Kinds, Modifiers, EntryTags {
writeRef(sym.loBound());
break;
case ALIAS:
+ writeRef(sym.allConstructors());
break;
case CLASS:
writeRef(sym.typeOfThis());
diff --git a/sources/scalac/symtab/classfile/UnPickle.java b/sources/scalac/symtab/classfile/UnPickle.java
index 07c30a60c4..f7f039c8c8 100644
--- a/sources/scalac/symtab/classfile/UnPickle.java
+++ b/sources/scalac/symtab/classfile/UnPickle.java
@@ -235,9 +235,10 @@ public class UnPickle implements Kinds, Modifiers, EntryTags {
break;
case ALIASsym:
- entries[n] = sym = new TypeSymbol(
- ALIAS, Position.NOPOS, name, owner, flags);
+ entries[n] = sym = new AliasTypeSymbol(
+ Position.NOPOS, name, owner, flags);
sym.setInfo(getType(inforef), Symbol.FIRST_ID);
+ Symbol constr = readSymbolRef();
break;
case CLASSsym:
@@ -262,15 +263,15 @@ public class UnPickle implements Kinds, Modifiers, EntryTags {
case VALsym:
if (bp < end) {
- ClassSymbol clazz = (ClassSymbol) readSymbolRef();
+ Symbol tsym = readSymbolRef();
if (name.isTypeName()) {
- entries[n] = sym = clazz.primaryConstructor();
+ entries[n] = sym = tsym.primaryConstructor();
sym.flags = flags;
} else {
assert (flags & MODUL) != 0 : name;
entries[n] = sym = new TermSymbol(
Position.NOPOS, name, owner, flags)
- .makeModule(clazz);
+ .makeModule((ClassSymbol) tsym);
}
} else {
entries[n] = sym = new TermSymbol(