@@ -395,32 +395,72 @@ class Namer { typer: Typer =>
395
395
/** Create top-level symbols for statements and enter them into symbol table */
396
396
def index (stats : List [Tree ])(implicit ctx : Context ): Context = {
397
397
398
+ val classDef = mutable.Map [TypeName , TypeDef ]()
399
+ val moduleDef = mutable.Map [TypeName , TypeDef ]()
400
+
398
401
/** Merge the definitions of a synthetic companion generated by a case class
399
402
* and the real companion, if both exist.
400
403
*/
401
404
def mergeCompanionDefs () = {
402
- val classDef = mutable.Map [TypeName , TypeDef ]()
403
405
for (cdef @ TypeDef (name, _) <- stats)
404
- if (cdef.isClassDef) classDef(name) = cdef
405
- for (mdef @ ModuleDef (name, _) <- stats)
406
+ if (cdef.isClassDef) {
407
+ classDef(name) = cdef
408
+ cdef.attachmentOrElse(ExpandedTree , cdef) match {
409
+ case Thicket (cls :: mval :: (mcls @ TypeDef (_, _ : Template )) :: crest) =>
410
+ moduleDef(name) = mcls
411
+ case _ =>
412
+ }
413
+ }
414
+ for (mdef @ ModuleDef (name, _) <- stats) {
415
+ val typName = name.toTypeName
416
+ val Thicket (vdef :: (mcls @ TypeDef (_, impl : Template )) :: Nil ) = mdef.attachment(ExpandedTree )
417
+ moduleDef(typName) = mcls
406
418
classDef get name.toTypeName match {
407
419
case Some (cdef) =>
408
- val Thicket (vdef :: (mcls @ TypeDef (_, impl : Template )) :: Nil ) = mdef.attachment(ExpandedTree )
409
420
cdef.attachmentOrElse(ExpandedTree , cdef) match {
410
421
case Thicket (cls :: mval :: TypeDef (_, compimpl : Template ) :: crest) =>
411
422
val mcls1 = cpy.TypeDef (mcls)(
412
423
rhs = cpy.Template (impl)(body = compimpl.body ++ impl.body))
413
424
mdef.putAttachment(ExpandedTree , Thicket (vdef :: mcls1 :: Nil ))
425
+ moduleDef(typName) = mcls1
414
426
cdef.putAttachment(ExpandedTree , Thicket (cls :: crest))
415
427
case _ =>
416
428
}
417
429
case none =>
418
430
}
431
+ }
432
+ }
433
+
434
+ def createLinks (classTree : TypeDef , moduleTree : TypeDef )(implicit ctx : Context ) = {
435
+ val claz = ctx.denotNamed(classTree.name.encode)
436
+ val modl = ctx.denotNamed(moduleTree.name.encode)
437
+ ctx.newSymbol(
438
+ owner = modl.symbol,
439
+ name = nme.COMPANION_CLASS_METHOD ,
440
+ flags = Flags .Synthetic | Flags .Private ,
441
+ info = ExprType (claz.symbol.typeRef)).entered
442
+ ctx.newSymbol(
443
+ owner = claz.symbol,
444
+ name = nme.COMPANION_MODULE_METHOD ,
445
+ flags = Flags .Synthetic | Flags .Private ,
446
+ info = ExprType (modl.symbol.typeRef)).entered
447
+ }
448
+
449
+ def createCompanionLinks (implicit ctx : Context ): Unit = {
450
+ for (cdef @ TypeDef (name, _) <- classDef.values) {
451
+ moduleDef.getOrElse(name, EmptyTree ) match {
452
+ case t : TypeDef =>
453
+ createLinks(cdef, t)
454
+ case EmptyTree =>
455
+ }
456
+ }
419
457
}
420
458
421
459
stats foreach expand
422
460
mergeCompanionDefs()
423
- (ctx /: stats) ((ctx, stat) => indexExpanded(stat)(ctx))
461
+ val ctxWithStats = (ctx /: stats) ((ctx, stat) => indexExpanded(stat)(ctx))
462
+ createCompanionLinks(ctxWithStats)
463
+ ctxWithStats
424
464
}
425
465
426
466
/** The completer of a symbol defined by a member def or import (except ClassSymbols) */
0 commit comments