Skip to content

Commit 13a05d5

Browse files
committed
#353 use methods to find companion class
1 parent c679218 commit 13a05d5

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,13 @@ object desugar {
330330
.withMods(synthetic))
331331
.withPos(cdef.pos).toList
332332

333+
def companionClassGeterMethod =
334+
DefDef(nme.COMPANION_CLASS_METHOD, Nil, Nil, Ident(name),
335+
/*Select(Select(
336+
Select(Ident(nme.ROOTPKG), nme.scala_),
337+
nme.Predef), nme.???)*/ Ident(nme.???))
338+
.withMods(synthetic | Private)
339+
333340
// The companion object defifinitions, if a companion is needed, Nil otherwise.
334341
// companion definitions include:
335342
// 1. If class is a case class case class C[Ts](p1: T1, ..., pN: TN)(moreParams):
@@ -359,11 +366,15 @@ object desugar {
359366
DefDef(nme.unapply, derivedTparams, (unapplyParam :: Nil) :: Nil, TypeTree(), unapplyRHS)
360367
.withMods(synthetic)
361368
}
362-
companionDefs(parent, applyMeths ::: unapplyMeth :: defaultGetters)
369+
companionDefs(parent, companionClassGeterMethod :: applyMeths ::: unapplyMeth :: defaultGetters)
370+
}
371+
else {
372+
val methods = if (!(mods is Synthetic | Module)) companionClassGeterMethod :: defaultGetters else defaultGetters
373+
374+
if(methods.nonEmpty)
375+
companionDefs(anyRef, methods)
376+
else Nil
363377
}
364-
else if (defaultGetters.nonEmpty)
365-
companionDefs(anyRef, defaultGetters)
366-
else Nil
367378

368379
// For an implicit class C[Ts](p11: T11, ..., p1N: T1N) ... (pM1: TM1, .., pMN: TMN), the method
369380
// synthetic implicit C[Ts](p11: T11, ..., p1N: T1N) ... (pM1: TM1, ..., pMN: TMN): C[Ts] =
@@ -409,6 +420,8 @@ object desugar {
409420
flatTree(cdef1 :: companions ::: implicitWrappers)
410421
}
411422

423+
val AccessOrSynthetic = AccessFlags | Synthetic
424+
412425
/** Expand
413426
*
414427
* object name extends parents { self => body }
@@ -436,7 +449,7 @@ object desugar {
436449
.withPos(tmpl.self.pos orElse tmpl.pos.startPos)
437450
val clsTmpl = cpy.Template(tmpl)(self = clsSelf, body = tmpl.body)
438451
val cls = TypeDef(clsName, clsTmpl)
439-
.withMods(mods.toTypeFlags & AccessFlags | ModuleClassCreationFlags)
452+
.withMods(mods.toTypeFlags & AccessOrSynthetic | ModuleClassCreationFlags)
440453
Thicket(modul, classDef(cls))
441454
}
442455
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ object StdNames {
8383
final val HASHkw: N = kw("#")
8484
final val ATkw: N = kw("@")
8585

86-
val ANON_CLASS: N = "$anon"
87-
val ANON_FUN: N = "$anonfun"
86+
val ANON_CLASS: N = "$anon"
87+
val ANON_FUN: N = "$anonfun"
8888
val BITMAP_PREFIX: N = "bitmap$"
8989
val BITMAP_NORMAL: N = BITMAP_PREFIX // initialization bitmap for public/protected lazy vals
9090
val BITMAP_TRANSIENT: N = BITMAP_PREFIX + "trans$" // initialization bitmap for transient lazy vals
@@ -117,13 +117,15 @@ object StdNames {
117117
val PROTECTED_PREFIX: N = "protected$"
118118
val PROTECTED_SET_PREFIX: N = PROTECTED_PREFIX + "set"
119119
val ROOT: N = "<root>"
120-
val SHADOWED: N = "(shadowed)" // tag to be used until we have proper name kinds
120+
val SHADOWED: N = "(shadowed)" // tag to be used until we have proper name kinds
121121
val SINGLETON_SUFFIX: N = ".type"
122122
val SPECIALIZED_SUFFIX: N = "$sp"
123123
val SUPER_PREFIX: N = "super$"
124124
val WHILE_PREFIX: N = "while$"
125125
val DEFAULT_EXCEPTION_NAME: N = "ex$"
126126
val INITIALIZER_PREFIX: N = "initial$"
127+
val COMPANION_MODULE_METHOD: N = "companion$module"
128+
val COMPANION_CLASS_METHOD: N = "compaion$class"
127129

128130
// value types (and AnyRef) are all used as terms as well
129131
// as (at least) arguments to the @specialize annotation.

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -773,11 +773,31 @@ object SymDenotations {
773773
companionNamed(effectiveName.moduleClassName).sourceModule
774774

775775
/** The class with the same (type-) name as this module or module class,
776-
* and which is also defined in the same scope and compilation unit.
777-
* NoSymbol if this class does not exist.
778-
*/
779-
final def companionClass(implicit ctx: Context): Symbol =
780-
companionNamed(effectiveName.toTypeName)
776+
* and which is also defined in the same scope and compilation unit.
777+
* NoSymbol if this class does not exist.
778+
*/
779+
final def companionClass(implicit ctx: Context): Symbol = {
780+
val companionMethod = info.decls.denotsNamed(nme.COMPANION_CLASS_METHOD, selectPrivate).first
781+
782+
if (companionMethod.exists)
783+
companionMethod.info.resultType.classSymbol
784+
else {
785+
/*
786+
val scalac = companionNamed(effectiveName.toTypeName)
787+
788+
if (scalac.exists) {
789+
println(s"scalac returned companion class for $this to be $scalac")
790+
}
791+
*/
792+
NoSymbol
793+
}
794+
}
795+
796+
final def scalacLinkedClass(implicit ctx: Context): Symbol =
797+
if (this is ModuleClass) companionNamed(effectiveName.toTypeName)
798+
else if (this.isClass) companionModule.moduleClass
799+
else NoSymbol
800+
781801

782802
/** Find companion class symbol with given name, or NoSymbol if none exists.
783803
* Three alternative strategies:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class ClassfileLoader(val classfile: AbstractFile) extends SymbolLoader {
229229
def description = "class file "+ classfile.toString
230230

231231
def rootDenots(rootDenot: ClassDenotation)(implicit ctx: Context): (ClassDenotation, ClassDenotation) = {
232-
val linkedDenot = rootDenot.linkedClass.denot match {
232+
val linkedDenot = rootDenot.scalacLinkedClass.denot match {
233233
case d: ClassDenotation => d
234234
case d =>
235235
// this can happen if the companion if shadowed by a val or type

0 commit comments

Comments
 (0)