companion objects of case classes know their name. review by extempore. close #3579.

git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@22420 5e8d7ff9-d8ef-0310-90f0-a4852d11357a
This commit is contained in:
rytz 2010-06-29 14:19:42 +00:00
parent 17d9f2a0e2
commit 7359e0bd65
1 changed files with 12 additions and 5 deletions

View File

@ -130,21 +130,28 @@ trait Unapplies extends ast.TreeDSL
}
}
/** The module corresponding to a case class; without any member definitions
/** The module corresponding to a case class; overrides toString to show the module's name
*/
def caseModuleDef(cdef: ClassDef): ModuleDef = {
def inheritFromFun = !(cdef.mods hasFlag ABSTRACT) && cdef.tparams.isEmpty && constrParamss(cdef).length == 1
def createFun = gen.scalaFunctionConstr(constrParamss(cdef).head map (_.tpt), toIdent(cdef), abstractFun = true)
def parents = if (inheritFromFun) List(createFun) else Nil
companionModuleDef(cdef, parents ::: List(gen.scalaScalaObjectConstr))
def toString = DefDef(
Modifiers(OVERRIDE | FINAL),
nme.toString_,
Nil,
List(Nil),
TypeTree(),
Literal(Constant(cdef.name.decode)))
companionModuleDef(cdef, parents ::: List(gen.scalaScalaObjectConstr), List(toString))
}
def companionModuleDef(cdef: ClassDef, parents: List[Tree]): ModuleDef = atPos(cdef.pos.focus) {
def companionModuleDef(cdef: ClassDef, parents: List[Tree], body: List[Tree] = Nil): ModuleDef = atPos(cdef.pos.focus) {
ModuleDef(
Modifiers(cdef.mods.flags & AccessFlags | SYNTHETIC, cdef.mods.privateWithin),
cdef.name.toTermName,
Template(parents, emptyValDef, NoMods, Nil, List(Nil), Nil, cdef.impl.pos.focus))
Template(parents, emptyValDef, NoMods, Nil, List(Nil), body, cdef.impl.pos.focus))
}
private val caseMods = Modifiers(SYNTHETIC | CASE)