From b9fa2c9f9f4d23ab7c9935a37496f4ac2bdc5fe9 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 6 Feb 2013 22:59:05 +0100 Subject: Getting rid of separate classes for TermSymbols and TypeSymbols Distinction is instead carried by type field ThisName. --- src/dotty/tools/dotc/core/Definitions.scala | 6 +- src/dotty/tools/dotc/core/Symbols.scala | 80 +++++++--------------- src/dotty/tools/dotc/core/pickling/UnPickler.scala | 8 +-- 3 files changed, 33 insertions(+), 61 deletions(-) diff --git a/src/dotty/tools/dotc/core/Definitions.scala b/src/dotty/tools/dotc/core/Definitions.scala index 35527573f..fc77c8384 100644 --- a/src/dotty/tools/dotc/core/Definitions.scala +++ b/src/dotty/tools/dotc/core/Definitions.scala @@ -22,7 +22,7 @@ class Definitions(implicit ctx: Context) { lazy val RootClass: ClassSymbol = ctx.newLazyPackageSymbols( NoSymbol, nme.ROOT, ctx.rootLoader)._2 - lazy val RootPackage: TermSymbol = ctx.newTermSymbol( + lazy val RootPackage: TermSymbol = ctx.newSymbol( NoSymbol, nme.ROOTPKG, PackageCreationFlags, TypeRef(NoPrefix, RootClass)) lazy val ScalaPackageVal = requiredPackage("scala") @@ -31,8 +31,8 @@ class Definitions(implicit ctx: Context) { lazy val JavaLangPackageVal = requiredPackage("java.lang") lazy val ObjectClass = requiredClass("java.lang.Object") - lazy val AnyRefAlias: TypeSymbol = ctx.newAliasTypeSymbol( - ScalaPackageClass, tpnme.AnyRef, ObjectClass.typeConstructor).entered + lazy val AnyRefAlias: TypeSymbol = ctx.newSymbol( + ScalaPackageClass, tpnme.AnyRef, EmptyFlags, TypeAlias(ObjectClass.typeConstructor)).entered lazy val AnyClass: ClassSymbol = ctx.newClassSymbol( ScalaPackageClass, tpnme.Any, Abstract, Nil).entered lazy val AnyValClass: ClassSymbol = requiredClass("scala.AnyVal") diff --git a/src/dotty/tools/dotc/core/Symbols.scala b/src/dotty/tools/dotc/core/Symbols.scala index 6c267da62..86cb70d34 100644 --- a/src/dotty/tools/dotc/core/Symbols.scala +++ b/src/dotty/tools/dotc/core/Symbols.scala @@ -18,11 +18,10 @@ import io.AbstractFile trait Symbols { this: Context => - def newLazyTermSymbol(owner: Symbol, name: TermName, initFlags: FlagSet, completer: SymCompleter) = - new TermSymbol(new LazySymDenotation(_, owner, name, initFlags, completer)) - - def newLazyTypeSymbol(owner: Symbol, name: TypeName, initFlags: FlagSet, completer: SymCompleter) = - new TypeSymbol(new LazySymDenotation(_, owner, name, initFlags, completer)) + def newLazySymbol[N <: Name](owner: Symbol, name: N, initFlags: FlagSet, completer: SymCompleter) = + new Symbol(new LazySymDenotation(_, owner, name, initFlags, completer)) { + type ThisName = N + } def newLazyClassSymbol(owner: Symbol, name: TypeName, initFlags: FlagSet, completer: ClassCompleter, assocFile: AbstractFile = null) = new ClassSymbol(new LazyClassDenotation(_, owner, name, initFlags, completer, assocFile)(this)) @@ -33,7 +32,7 @@ trait Symbols { this: Context => completer: ClassCompleter, assocFile: AbstractFile = null) = { - val module = newLazyTermSymbol( + val module = newLazySymbol( owner, name, flags | ModuleCreationFlags, new ModuleCompleter(condensed)) val modcls = newLazyClassSymbol( owner, name.toTypeName, flags | ModuleClassCreationFlags, completer, assocFile) @@ -47,30 +46,10 @@ trait Symbols { this: Context => def newLazyPackageSymbols(owner: Symbol, name: TermName, completer: ClassCompleter) = newLazyModuleSymbols(owner, name, PackageCreationFlags, completer) - def newSymbol(owner: Symbol, name: Name, flags: FlagSet, info: Type, privateWithin: Symbol = NoSymbol): Symbol = - if (name.isTermName) newTermSymbol(owner, name.asTermName, flags, info, privateWithin) - else newTypeSymbol(owner, name.asTypeName, flags, info, privateWithin) - - def newTermSymbol( - owner: Symbol, - name: TermName, - flags: FlagSet, - info: Type, - privateWithin: Symbol = NoSymbol) - = - new TermSymbol(new CompleteSymDenotation(_, owner, name, flags, privateWithin, info)) - - def newTypeSymbol( - owner: Symbol, - name: TypeName, - flags: FlagSet, - info: Type, - privateWithin: Symbol = NoSymbol) - = - new TypeSymbol(new CompleteSymDenotation(_, owner, name, flags, privateWithin, info)) - - def newAliasTypeSymbol(owner: Symbol, name: TypeName, alias: Type, flags: FlagSet = EmptyFlags, privateWithin: Symbol = NoSymbol) = - newTypeSymbol(owner, name, flags, TypeBounds(alias, alias), privateWithin) + def newSymbol[N <: Name](owner: Symbol, name: N, flags: FlagSet, info: Type, privateWithin: Symbol = NoSymbol) = + new Symbol(new CompleteSymDenotation(_, owner, name, flags, privateWithin, info)) { + type ThisName = N + } def newClassSymbol( owner: Symbol, @@ -95,7 +74,7 @@ trait Symbols { this: Context => decls: Scope = newScope, assocFile: AbstractFile = null)(implicit ctx: Context) = { - val module = newLazyTermSymbol(owner, name, flags | ModuleCreationFlags, new ModuleCompleter(condensed)) + val module = newLazySymbol(owner, name, flags | ModuleCreationFlags, new ModuleCompleter(condensed)) val modcls = newClassSymbol( owner, name.toTypeName, classFlags | ModuleClassCreationFlags, parents, privateWithin, optSelfType = TermRef(owner.thisType, module), @@ -115,7 +94,7 @@ trait Symbols { this: Context => def newStubSymbol(owner: Symbol, name: Name)(implicit ctx: Context): Symbol = { def stub[Denot <: SymDenotation] = new StubCompleter[Denot](ctx.condensed) name match { - case name: TermName => ctx.newLazyTermSymbol(owner, name, EmptyFlags, stub) + case name: TermName => ctx.newLazySymbol(owner, name, EmptyFlags, stub) case name: TypeName => ctx.newLazyClassSymbol(owner, name, EmptyFlags, stub) } } @@ -125,7 +104,9 @@ object Symbols { /** A Symbol represents a Scala definition/declaration or a package. */ - abstract class Symbol(denotf: Symbol => SymDenotation) extends DotClass { + class Symbol(denotf: Symbol => SymDenotation) extends DotClass { + + type ThisName <: Name /** Is symbol different from NoSymbol? */ def exists = true @@ -195,7 +176,7 @@ object Symbols { final def owner(implicit ctx: Context): Symbol = denot.owner /** The current name of this symbol */ - final def name(implicit ctx: Context): Name = denot.name + final def name(implicit ctx: Context): ThisName = denot.name.asInstanceOf[ThisName] /** The current type info of this symbol */ final def info(implicit ctx: Context): Type = denot.info @@ -393,12 +374,6 @@ object Symbols { //def isMethod(implicit ctx: Context): Boolean = denot.isMethod - } - - class TermSymbol(denotf: Symbol => SymDenotation) extends Symbol(denotf) - - class TypeSymbol(denotf: Symbol => SymDenotation) extends Symbol(denotf) { - /** The type representing the type constructor for this type symbol */ def typeConstructor(implicit ctx: Context): TypeRef = denot.typeConstructor @@ -408,7 +383,13 @@ object Symbols { def variance(implicit ctx: Context): Int = denot.variance } - class ClassSymbol(denotf: ClassSymbol => ClassDenotation) extends TypeSymbol(s => denotf(s.asClass)) { + type TermSymbol = Symbol { type ThisName = TermName } + type TypeSymbol = Symbol { type ThisName = TypeName } + + class ClassSymbol(denotf: ClassSymbol => ClassDenotation) extends Symbol(s => denotf(s.asClass)) { + + type ThisName = TypeName + private var superIdHint: Int = -1 final def classDenot(implicit ctx: Context): ClassDenotation = @@ -448,28 +429,19 @@ object Symbols { } } - trait ErrorSymbol { - val underlying: Symbol - def getMsg: String - } - - class ErrorTypeSymbol(val underlying: Symbol, msg: => String)(implicit ctx: Context) extends TypeSymbol(sym => underlying.denot) with ErrorSymbol { - def getMsg = msg - } - - class ErrorTermSymbol(val underlying: Symbol, msg: => String)(implicit ctx: Context) extends TermSymbol(sym => underlying.denot) with ErrorSymbol { - def getMsg = msg + class ErrorSymbol(val underlying: Symbol, msg: => String)(implicit ctx: Context) extends Symbol(sym => underlying.denot) { + type ThisName = underlying.ThisName } object NoSymbol extends Symbol(sym => NoDenotation) { override def exists = false } - implicit class Copier(sym: Symbol)(implicit ctx: Context) { + implicit class Copier[N <: Name](sym: Symbol { type ThisName = N })(implicit ctx: Context) { /** Copy a symbol, overriding selective fields */ def copy( owner: Symbol = sym.owner, - name: Name = sym.name, + name: N = sym.name, flags: FlagSet = sym.flags, privateWithin: Symbol = sym.privateWithin, info: Type = sym.info): Symbol = diff --git a/src/dotty/tools/dotc/core/pickling/UnPickler.scala b/src/dotty/tools/dotc/core/pickling/UnPickler.scala index 2142df937..b3c0a88f9 100644 --- a/src/dotty/tools/dotc/core/pickling/UnPickler.scala +++ b/src/dotty/tools/dotc/core/pickling/UnPickler.scala @@ -323,7 +323,7 @@ abstract class UnPickler { name1 = name1.expandedName(owner) flags1 |= ProtectedLocal } - ctx.newLazyTypeSymbol(owner, name1, flags1, completeSym(tag)) + ctx.newLazySymbol(owner, name1, flags1, completeSym(tag)) case CLASSsym => if (isClassRoot) completeRoot(classRoot) else if (isModuleRoot) completeRoot(moduleRoot) @@ -333,10 +333,10 @@ abstract class UnPickler { if (isModuleRoot) { moduleRoot.denot.asInstanceOf[LazyClassDenotation].flags = flags moduleRoot - } else ctx.newTermSymbol(owner, name.asTermName, flags, info) + } else ctx.newSymbol(owner, name.asTermName, flags, info) case VALsym => if (isModuleRoot) { assert(false); NoSymbol } - else ctx.newLazyTermSymbol(owner, name.asTermName, flags, completeSym(tag)) + else ctx.newLazySymbol(owner, name.asTermName, flags, completeSym(tag)) case _ => errorBadSignature("bad symbol tag: " + tag) @@ -998,7 +998,7 @@ abstract class UnPickler { for ((name, tpe) <- refinements) denot.decls.enter { val formal = cls.info.member(name).symbol val bounds = tpe.toAlias(formal) - ctx.newTypeSymbol(cls, name, formal.flags & RetainedTypeArgFlags, bounds) + ctx.newSymbol(cls, name, formal.flags & RetainedTypeArgFlags, bounds) } } catch { case e: MissingRequirementError => throw toTypeError(e) -- cgit v1.2.3