Skip to content

Commit b9fa2c9

Browse files
committed
Getting rid of separate classes for TermSymbols and TypeSymbols
Distinction is instead carried by type field ThisName.
1 parent d7e2832 commit b9fa2c9

File tree

3 files changed

+33
-61
lines changed

3 files changed

+33
-61
lines changed

src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Definitions(implicit ctx: Context) {
2222

2323
lazy val RootClass: ClassSymbol = ctx.newLazyPackageSymbols(
2424
NoSymbol, nme.ROOT, ctx.rootLoader)._2
25-
lazy val RootPackage: TermSymbol = ctx.newTermSymbol(
25+
lazy val RootPackage: TermSymbol = ctx.newSymbol(
2626
NoSymbol, nme.ROOTPKG, PackageCreationFlags, TypeRef(NoPrefix, RootClass))
2727

2828
lazy val ScalaPackageVal = requiredPackage("scala")
@@ -31,8 +31,8 @@ class Definitions(implicit ctx: Context) {
3131
lazy val JavaLangPackageVal = requiredPackage("java.lang")
3232

3333
lazy val ObjectClass = requiredClass("java.lang.Object")
34-
lazy val AnyRefAlias: TypeSymbol = ctx.newAliasTypeSymbol(
35-
ScalaPackageClass, tpnme.AnyRef, ObjectClass.typeConstructor).entered
34+
lazy val AnyRefAlias: TypeSymbol = ctx.newSymbol(
35+
ScalaPackageClass, tpnme.AnyRef, EmptyFlags, TypeAlias(ObjectClass.typeConstructor)).entered
3636
lazy val AnyClass: ClassSymbol = ctx.newClassSymbol(
3737
ScalaPackageClass, tpnme.Any, Abstract, Nil).entered
3838
lazy val AnyValClass: ClassSymbol = requiredClass("scala.AnyVal")

src/dotty/tools/dotc/core/Symbols.scala

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ import io.AbstractFile
1818

1919
trait Symbols { this: Context =>
2020

21-
def newLazyTermSymbol(owner: Symbol, name: TermName, initFlags: FlagSet, completer: SymCompleter) =
22-
new TermSymbol(new LazySymDenotation(_, owner, name, initFlags, completer))
23-
24-
def newLazyTypeSymbol(owner: Symbol, name: TypeName, initFlags: FlagSet, completer: SymCompleter) =
25-
new TypeSymbol(new LazySymDenotation(_, owner, name, initFlags, completer))
21+
def newLazySymbol[N <: Name](owner: Symbol, name: N, initFlags: FlagSet, completer: SymCompleter) =
22+
new Symbol(new LazySymDenotation(_, owner, name, initFlags, completer)) {
23+
type ThisName = N
24+
}
2625

2726
def newLazyClassSymbol(owner: Symbol, name: TypeName, initFlags: FlagSet, completer: ClassCompleter, assocFile: AbstractFile = null) =
2827
new ClassSymbol(new LazyClassDenotation(_, owner, name, initFlags, completer, assocFile)(this))
@@ -33,7 +32,7 @@ trait Symbols { this: Context =>
3332
completer: ClassCompleter,
3433
assocFile: AbstractFile = null)
3534
= {
36-
val module = newLazyTermSymbol(
35+
val module = newLazySymbol(
3736
owner, name, flags | ModuleCreationFlags, new ModuleCompleter(condensed))
3837
val modcls = newLazyClassSymbol(
3938
owner, name.toTypeName, flags | ModuleClassCreationFlags, completer, assocFile)
@@ -47,30 +46,10 @@ trait Symbols { this: Context =>
4746
def newLazyPackageSymbols(owner: Symbol, name: TermName, completer: ClassCompleter) =
4847
newLazyModuleSymbols(owner, name, PackageCreationFlags, completer)
4948

50-
def newSymbol(owner: Symbol, name: Name, flags: FlagSet, info: Type, privateWithin: Symbol = NoSymbol): Symbol =
51-
if (name.isTermName) newTermSymbol(owner, name.asTermName, flags, info, privateWithin)
52-
else newTypeSymbol(owner, name.asTypeName, flags, info, privateWithin)
53-
54-
def newTermSymbol(
55-
owner: Symbol,
56-
name: TermName,
57-
flags: FlagSet,
58-
info: Type,
59-
privateWithin: Symbol = NoSymbol)
60-
=
61-
new TermSymbol(new CompleteSymDenotation(_, owner, name, flags, privateWithin, info))
62-
63-
def newTypeSymbol(
64-
owner: Symbol,
65-
name: TypeName,
66-
flags: FlagSet,
67-
info: Type,
68-
privateWithin: Symbol = NoSymbol)
69-
=
70-
new TypeSymbol(new CompleteSymDenotation(_, owner, name, flags, privateWithin, info))
71-
72-
def newAliasTypeSymbol(owner: Symbol, name: TypeName, alias: Type, flags: FlagSet = EmptyFlags, privateWithin: Symbol = NoSymbol) =
73-
newTypeSymbol(owner, name, flags, TypeBounds(alias, alias), privateWithin)
49+
def newSymbol[N <: Name](owner: Symbol, name: N, flags: FlagSet, info: Type, privateWithin: Symbol = NoSymbol) =
50+
new Symbol(new CompleteSymDenotation(_, owner, name, flags, privateWithin, info)) {
51+
type ThisName = N
52+
}
7453

7554
def newClassSymbol(
7655
owner: Symbol,
@@ -95,7 +74,7 @@ trait Symbols { this: Context =>
9574
decls: Scope = newScope,
9675
assocFile: AbstractFile = null)(implicit ctx: Context)
9776
= {
98-
val module = newLazyTermSymbol(owner, name, flags | ModuleCreationFlags, new ModuleCompleter(condensed))
77+
val module = newLazySymbol(owner, name, flags | ModuleCreationFlags, new ModuleCompleter(condensed))
9978
val modcls = newClassSymbol(
10079
owner, name.toTypeName, classFlags | ModuleClassCreationFlags, parents, privateWithin,
10180
optSelfType = TermRef(owner.thisType, module),
@@ -115,7 +94,7 @@ trait Symbols { this: Context =>
11594
def newStubSymbol(owner: Symbol, name: Name)(implicit ctx: Context): Symbol = {
11695
def stub[Denot <: SymDenotation] = new StubCompleter[Denot](ctx.condensed)
11796
name match {
118-
case name: TermName => ctx.newLazyTermSymbol(owner, name, EmptyFlags, stub)
97+
case name: TermName => ctx.newLazySymbol(owner, name, EmptyFlags, stub)
11998
case name: TypeName => ctx.newLazyClassSymbol(owner, name, EmptyFlags, stub)
12099
}
121100
}
@@ -125,7 +104,9 @@ object Symbols {
125104

126105
/** A Symbol represents a Scala definition/declaration or a package.
127106
*/
128-
abstract class Symbol(denotf: Symbol => SymDenotation) extends DotClass {
107+
class Symbol(denotf: Symbol => SymDenotation) extends DotClass {
108+
109+
type ThisName <: Name
129110

130111
/** Is symbol different from NoSymbol? */
131112
def exists = true
@@ -195,7 +176,7 @@ object Symbols {
195176
final def owner(implicit ctx: Context): Symbol = denot.owner
196177

197178
/** The current name of this symbol */
198-
final def name(implicit ctx: Context): Name = denot.name
179+
final def name(implicit ctx: Context): ThisName = denot.name.asInstanceOf[ThisName]
199180

200181
/** The current type info of this symbol */
201182
final def info(implicit ctx: Context): Type = denot.info
@@ -393,12 +374,6 @@ object Symbols {
393374

394375
//def isMethod(implicit ctx: Context): Boolean = denot.isMethod
395376

396-
}
397-
398-
class TermSymbol(denotf: Symbol => SymDenotation) extends Symbol(denotf)
399-
400-
class TypeSymbol(denotf: Symbol => SymDenotation) extends Symbol(denotf) {
401-
402377
/** The type representing the type constructor for this type symbol */
403378
def typeConstructor(implicit ctx: Context): TypeRef = denot.typeConstructor
404379

@@ -408,7 +383,13 @@ object Symbols {
408383
def variance(implicit ctx: Context): Int = denot.variance
409384
}
410385

411-
class ClassSymbol(denotf: ClassSymbol => ClassDenotation) extends TypeSymbol(s => denotf(s.asClass)) {
386+
type TermSymbol = Symbol { type ThisName = TermName }
387+
type TypeSymbol = Symbol { type ThisName = TypeName }
388+
389+
class ClassSymbol(denotf: ClassSymbol => ClassDenotation) extends Symbol(s => denotf(s.asClass)) {
390+
391+
type ThisName = TypeName
392+
412393
private var superIdHint: Int = -1
413394

414395
final def classDenot(implicit ctx: Context): ClassDenotation =
@@ -448,28 +429,19 @@ object Symbols {
448429
}
449430
}
450431

451-
trait ErrorSymbol {
452-
val underlying: Symbol
453-
def getMsg: String
454-
}
455-
456-
class ErrorTypeSymbol(val underlying: Symbol, msg: => String)(implicit ctx: Context) extends TypeSymbol(sym => underlying.denot) with ErrorSymbol {
457-
def getMsg = msg
458-
}
459-
460-
class ErrorTermSymbol(val underlying: Symbol, msg: => String)(implicit ctx: Context) extends TermSymbol(sym => underlying.denot) with ErrorSymbol {
461-
def getMsg = msg
432+
class ErrorSymbol(val underlying: Symbol, msg: => String)(implicit ctx: Context) extends Symbol(sym => underlying.denot) {
433+
type ThisName = underlying.ThisName
462434
}
463435

464436
object NoSymbol extends Symbol(sym => NoDenotation) {
465437
override def exists = false
466438
}
467439

468-
implicit class Copier(sym: Symbol)(implicit ctx: Context) {
440+
implicit class Copier[N <: Name](sym: Symbol { type ThisName = N })(implicit ctx: Context) {
469441
/** Copy a symbol, overriding selective fields */
470442
def copy(
471443
owner: Symbol = sym.owner,
472-
name: Name = sym.name,
444+
name: N = sym.name,
473445
flags: FlagSet = sym.flags,
474446
privateWithin: Symbol = sym.privateWithin,
475447
info: Type = sym.info): Symbol =

src/dotty/tools/dotc/core/pickling/UnPickler.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ abstract class UnPickler {
323323
name1 = name1.expandedName(owner)
324324
flags1 |= ProtectedLocal
325325
}
326-
ctx.newLazyTypeSymbol(owner, name1, flags1, completeSym(tag))
326+
ctx.newLazySymbol(owner, name1, flags1, completeSym(tag))
327327
case CLASSsym =>
328328
if (isClassRoot) completeRoot(classRoot)
329329
else if (isModuleRoot) completeRoot(moduleRoot)
@@ -333,10 +333,10 @@ abstract class UnPickler {
333333
if (isModuleRoot) {
334334
moduleRoot.denot.asInstanceOf[LazyClassDenotation].flags = flags
335335
moduleRoot
336-
} else ctx.newTermSymbol(owner, name.asTermName, flags, info)
336+
} else ctx.newSymbol(owner, name.asTermName, flags, info)
337337
case VALsym =>
338338
if (isModuleRoot) { assert(false); NoSymbol }
339-
else ctx.newLazyTermSymbol(owner, name.asTermName, flags, completeSym(tag))
339+
else ctx.newLazySymbol(owner, name.asTermName, flags, completeSym(tag))
340340

341341
case _ =>
342342
errorBadSignature("bad symbol tag: " + tag)
@@ -998,7 +998,7 @@ abstract class UnPickler {
998998
for ((name, tpe) <- refinements) denot.decls.enter {
999999
val formal = cls.info.member(name).symbol
10001000
val bounds = tpe.toAlias(formal)
1001-
ctx.newTypeSymbol(cls, name, formal.flags & RetainedTypeArgFlags, bounds)
1001+
ctx.newSymbol(cls, name, formal.flags & RetainedTypeArgFlags, bounds)
10021002
}
10031003
} catch {
10041004
case e: MissingRequirementError => throw toTypeError(e)

0 commit comments

Comments
 (0)