diff options
Diffstat (limited to 'src/dotty/tools')
-rw-r--r-- | src/dotty/tools/dotc/ast/Desugar.scala | 15 | ||||
-rw-r--r-- | src/dotty/tools/dotc/typer/Namer.scala | 50 |
2 files changed, 48 insertions, 17 deletions
diff --git a/src/dotty/tools/dotc/ast/Desugar.scala b/src/dotty/tools/dotc/ast/Desugar.scala index cc94e9b27..961675fed 100644 --- a/src/dotty/tools/dotc/ast/Desugar.scala +++ b/src/dotty/tools/dotc/ast/Desugar.scala @@ -330,13 +330,6 @@ object desugar { .withMods(synthetic)) .withPos(cdef.pos).toList - def companionClassGeterMethod = - DefDef(nme.COMPANION_CLASS_METHOD, Nil, Nil, Ident(name), - /*Select(Select( - Select(Ident(nme.ROOTPKG), nme.scala_), - nme.Predef), nme.???)*/ Ident(nme.???)) - .withMods(synthetic | Private) - // The companion object defifinitions, if a companion is needed, Nil otherwise. // companion definitions include: // 1. If class is a case class case class C[Ts](p1: T1, ..., pN: TN)(moreParams): @@ -366,13 +359,11 @@ object desugar { DefDef(nme.unapply, derivedTparams, (unapplyParam :: Nil) :: Nil, TypeTree(), unapplyRHS) .withMods(synthetic) } - companionDefs(parent, companionClassGeterMethod :: applyMeths ::: unapplyMeth :: defaultGetters) + companionDefs(parent, applyMeths ::: unapplyMeth :: defaultGetters) } else { - val methods = if (!(mods is Synthetic | Module)) companionClassGeterMethod :: defaultGetters else defaultGetters - - if(methods.nonEmpty) - companionDefs(anyRef, methods) + if (defaultGetters.nonEmpty) + companionDefs(anyRef, defaultGetters) else Nil } diff --git a/src/dotty/tools/dotc/typer/Namer.scala b/src/dotty/tools/dotc/typer/Namer.scala index 357860290..414f6e517 100644 --- a/src/dotty/tools/dotc/typer/Namer.scala +++ b/src/dotty/tools/dotc/typer/Namer.scala @@ -395,32 +395,72 @@ class Namer { typer: Typer => /** Create top-level symbols for statements and enter them into symbol table */ def index(stats: List[Tree])(implicit ctx: Context): Context = { + val classDef = mutable.Map[TypeName, TypeDef]() + val moduleDef = mutable.Map[TypeName, TypeDef]() + /** Merge the definitions of a synthetic companion generated by a case class * and the real companion, if both exist. */ def mergeCompanionDefs() = { - val classDef = mutable.Map[TypeName, TypeDef]() for (cdef @ TypeDef(name, _) <- stats) - if (cdef.isClassDef) classDef(name) = cdef - for (mdef @ ModuleDef(name, _) <- stats) + if (cdef.isClassDef) { + classDef(name) = cdef + cdef.attachmentOrElse(ExpandedTree, cdef) match { + case Thicket(cls :: mval :: (mcls @ TypeDef(_, _: Template)) :: crest) => + moduleDef(name) = mcls + case _ => + } + } + for (mdef @ ModuleDef(name, _) <- stats) { + val typName = name.toTypeName + val Thicket(vdef :: (mcls @ TypeDef(_, impl: Template)) :: Nil) = mdef.attachment(ExpandedTree) + moduleDef(typName) = mcls classDef get name.toTypeName match { case Some(cdef) => - val Thicket(vdef :: (mcls @ TypeDef(_, impl: Template)) :: Nil) = mdef.attachment(ExpandedTree) cdef.attachmentOrElse(ExpandedTree, cdef) match { case Thicket(cls :: mval :: TypeDef(_, compimpl: Template) :: crest) => val mcls1 = cpy.TypeDef(mcls)( rhs = cpy.Template(impl)(body = compimpl.body ++ impl.body)) mdef.putAttachment(ExpandedTree, Thicket(vdef :: mcls1 :: Nil)) + moduleDef(typName) = mcls1 cdef.putAttachment(ExpandedTree, Thicket(cls :: crest)) case _ => } case none => } + } + } + + def createLinks(classTree: TypeDef, moduleTree: TypeDef)(implicit ctx: Context) = { + val claz = ctx.denotNamed(classTree.name.encode) + val modl = ctx.denotNamed(moduleTree.name.encode) + ctx.newSymbol( + owner = modl.symbol, + name = nme.COMPANION_CLASS_METHOD, + flags = Flags.Synthetic | Flags.Private, + info = ExprType(claz.symbol.typeRef)).entered + ctx.newSymbol( + owner = claz.symbol, + name = nme.COMPANION_MODULE_METHOD, + flags = Flags.Synthetic | Flags.Private, + info = ExprType(modl.symbol.typeRef)).entered + } + + def createCompanionLinks(implicit ctx: Context): Unit = { + for (cdef @ TypeDef(name, _) <- classDef.values) { + moduleDef.getOrElse(name, EmptyTree) match { + case t: TypeDef => + createLinks(cdef, t) + case EmptyTree => + } + } } stats foreach expand mergeCompanionDefs() - (ctx /: stats) ((ctx, stat) => indexExpanded(stat)(ctx)) + val ctxWithStats = (ctx /: stats) ((ctx, stat) => indexExpanded(stat)(ctx)) + createCompanionLinks(ctxWithStats) + ctxWithStats } /** The completer of a symbol defined by a member def or import (except ClassSymbols) */ |