@@ -471,15 +471,6 @@ object SymDenotations {
471471 final def isAnonymousModuleVal (implicit ctx : Context ): Boolean =
472472 this .symbol.is(ModuleVal ) && (initial.name startsWith str.ANON_CLASS )
473473
474- /** Is this a companion class method or companion object method?
475- * These methods are generated by Symbols#synthesizeCompanionMethod
476- * and used in SymDenotations#companionClass and
477- * SymDenotations#companionModule .
478- */
479- final def isCompanionMethod (implicit ctx : Context ): Boolean =
480- name.toTermName == nme.COMPANION_CLASS_METHOD ||
481- name.toTermName == nme.COMPANION_MODULE_METHOD
482-
483474 /** Is this a synthetic method that represents conversions between representations of a value class
484475 * These methods are generated in ExtensionMethods
485476 * and used in ElimErasedValueType.
@@ -526,6 +517,11 @@ object SymDenotations {
526517 /** Is this symbol an abstract type or type parameter? */
527518 final def isAbstractOrParamType (implicit ctx : Context ): Boolean = this is DeferredOrTypeParam
528519
520+ /** Can this symbol have a companion module?
521+ * This is the case if it is a class or an opaque type alias.
522+ */
523+ final def canHaveCompanion (implicit ctx : Context ) = isClass
524+
529525 /** Is this the denotation of a self symbol of some class?
530526 * This is the case if one of two conditions holds:
531527 * 1. It is the symbol referred to in the selfInfo part of the ClassInfo
@@ -597,12 +593,9 @@ object SymDenotations {
597593 /** Is this a "real" method? A real method is a method which is:
598594 * - not an accessor
599595 * - not an anonymous function
600- * - not a companion method
601596 */
602597 final def isRealMethod (implicit ctx : Context ): Boolean =
603- this .is(Method , butNot = Accessor ) &&
604- ! isAnonymousFunction &&
605- ! isCompanionMethod
598+ this .is(Method , butNot = Accessor ) && ! isAnonymousFunction
606599
607600 /** Is this a getter? */
608601 final def isGetter (implicit ctx : Context ): Boolean =
@@ -948,34 +941,32 @@ object SymDenotations {
948941 final def enclosingPackageClass (implicit ctx : Context ): Symbol =
949942 if (this is PackageClass ) symbol else owner.enclosingPackageClass
950943
944+ /** Register target as a companion; overridden in ClassDenotation */
945+ def registerCompanion (target : Symbol )(implicit ctx : Context ) = ()
946+
947+ /** The registered companion; overridden in ClassDenotation */
948+ def registeredCompanion (implicit ctx : Context ): Symbol = NoSymbol
949+ def registeredCompanion_= (c : Symbol ): Unit = ()
950+
951951 /** The module object with the same (term-) name as this class or module class,
952952 * and which is also defined in the same scope and compilation unit.
953953 * NoSymbol if this module does not exist.
954954 */
955- final def companionModule (implicit ctx : Context ): Symbol = {
956- if (this .flagsUNSAFE is Flags .Module ) this .sourceModule
957- else {
958- val companionMethod = info.decls.denotsNamed(nme.COMPANION_MODULE_METHOD , selectPrivate).first
959- if (companionMethod.exists)
960- companionMethod.info.resultType.classSymbol.sourceModule
961- else
962- NoSymbol
963- }
964- }
955+ final def companionModule (implicit ctx : Context ): Symbol =
956+ if (is(Module )) sourceModule
957+ else registeredCompanion.sourceModule
958+
959+ private def companionType (implicit ctx : Context ): Symbol =
960+ if (is(Package )) NoSymbol
961+ else if (is(ModuleVal )) moduleClass.denot.companionType
962+ else registeredCompanion
965963
966964 /** The class with the same (type-) name as this module or module class,
967- * and which is also defined in the same scope and compilation unit.
968- * NoSymbol if this class does not exist.
969- */
965+ * and which is also defined in the same scope and compilation unit.
966+ * NoSymbol if this class does not exist.
967+ */
970968 final def companionClass (implicit ctx : Context ): Symbol =
971- if (is(Package )) NoSymbol
972- else {
973- val companionMethod = info.decls.denotsNamed(nme.COMPANION_CLASS_METHOD , selectPrivate).first
974- if (companionMethod.exists)
975- companionMethod.info.resultType.classSymbol
976- else
977- NoSymbol
978- }
969+ companionType.suchThat(_.isClass).symbol
979970
980971 final def scalacLinkedClass (implicit ctx : Context ): Symbol =
981972 if (this is ModuleClass ) companionNamed(effectiveName.toTypeName)
@@ -1238,6 +1229,7 @@ object SymDenotations {
12381229 val annotations1 = if (annotations != null ) annotations else this .annotations
12391230 val d = ctx.SymDenotation (symbol, owner, name, initFlags1, info1, privateWithin1)
12401231 d.annotations = annotations1
1232+ d.registeredCompanion = registeredCompanion
12411233 d
12421234 }
12431235
@@ -1831,6 +1823,16 @@ object SymDenotations {
18311823 .copyCaches(this , phase.next)
18321824 .installAfter(phase)
18331825 }
1826+
1827+ private [this ] var myCompanion : Symbol = NoSymbol
1828+
1829+ /** Register companion class */
1830+ override def registerCompanion (companion : Symbol )(implicit ctx : Context ) =
1831+ if (companion.canHaveCompanion && ! unforcedIsAbsent && ! companion.unforcedIsAbsent)
1832+ myCompanion = companion
1833+
1834+ override def registeredCompanion (implicit ctx : Context ) = { ensureCompleted(); myCompanion }
1835+ override def registeredCompanion_= (c : Symbol ) = { myCompanion = c }
18341836 }
18351837
18361838 /** The denotation of a package class.
0 commit comments