Skip to content

Commit 0a94d6e

Browse files
committed
Remove code duplication between Namer, ClassfileParser and UnPickler
1 parent f40c498 commit 0a94d6e

File tree

5 files changed

+21
-36
lines changed

5 files changed

+21
-36
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ trait Symbols { this: Context =>
161161
owner.thisType, modcls, parents, decls, TermRef.withSymAndName(owner.thisType, module, name)),
162162
privateWithin, coord, assocFile)
163163

164+
def synthesizeCompanionMethod(name: TermName, ret: SymDenotation, owner: SymDenotation)(implicit ctx: Context) = {
165+
if(owner.exists && ret.exists) ctx.newSymbol(
166+
owner = owner.symbol,
167+
name = name,
168+
flags = Flags.Synthetic | Flags.Private,
169+
info = ExprType(ret.typeRef))
170+
else NoSymbol
171+
}
172+
164173
/** Create a package symbol with associated package class
165174
* from its non-info fields and a lazy type for loading the package's members.
166175
*/

src/dotty/tools/dotc/core/pickling/ClassfileParser.scala

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,14 @@ class ClassfileParser(
130130
for (i <- 0 until in.nextChar) parseMember(method = true)
131131
classInfo = parseAttributes(classRoot.symbol, classInfo)
132132
if (isAnnotation) addAnnotationConstructor(classInfo)
133-
if (classRoot.exists) syntecizeCompanionMethod(nme.COMPANION_CLASS_METHOD, classRoot, moduleRoot)
133+
val companionClassMethod = ctx.synthesizeCompanionMethod(nme.COMPANION_CLASS_METHOD, classRoot, moduleRoot)
134+
if (companionClassMethod.exists) companionClassMethod.entered
135+
134136

135137
setClassInfo(classRoot, classInfo)
136138
setClassInfo(moduleRoot, staticInfo)
137139
}
138140

139-
def syntecizeCompanionMethod(name: TermName, ret: SymDenotation, owner: SymDenotation)(implicit ctx: Context) = {
140-
if(owner.exists) ctx.newSymbol(
141-
owner = owner.symbol,
142-
name = name,
143-
flags = Flags.Synthetic | Flags.Private,
144-
info = ExprType(ret.typeRef)).entered
145-
}
146141

147142
/** Add type parameters of enclosing classes */
148143
def addEnclosingTParams()(implicit ctx: Context): Unit = {

src/dotty/tools/dotc/core/pickling/UnPickler.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,9 @@ object UnPickler {
120120
if (!(denot.flagsUNSAFE is JavaModule)) ensureConstructor(denot.symbol.asClass, decls)
121121
if (denot.flagsUNSAFE is Module) {
122122
val scalacCompanion = denot.classSymbol.scalacLinkedClass
123-
if (scalacCompanion.exists)
124-
ctx.newSymbol(
125-
owner = denot.classSymbol,
126-
name = nme.COMPANION_CLASS_METHOD,
127-
flags = Flags.Synthetic | Flags.Private,
128-
info = ExprType(scalacCompanion.typeRef)).entered
123+
val companionClassMethod = ctx.synthesizeCompanionMethod(nme.COMPANION_CLASS_METHOD, scalacCompanion, denot.classSymbol)
124+
if (companionClassMethod.exists)
125+
companionClassMethod.entered
129126
}
130127

131128
denot.info = ClassInfo(denot.owner.thisType, denot.classSymbol, parentRefs, decls, ost)

src/dotty/tools/dotc/transform/FirstTransform.scala

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,8 @@ class FirstTransform extends MiniPhaseTransform with IdentityDenotTransformer wi
7878
defn.ObjectClass.typeRef :: Nil, Scopes.newScope)
7979
val mc = modul.moduleClass
8080
if (ctx.owner.isClass) modul.enteredAfter(thisTransformer)
81-
ctx.newSymbol(
82-
owner = mc,
83-
name = nme.COMPANION_CLASS_METHOD,
84-
flags = Flags.Synthetic | Flags.Private,
85-
info = ExprType(forClass.typeRef)).enteredAfter(thisTransformer)
86-
ctx.newSymbol(
87-
owner = forClass,
88-
name = nme.COMPANION_MODULE_METHOD,
89-
flags = Flags.Synthetic | Flags.Private,
90-
info = ExprType(mc.typeRef)).enteredAfter(thisTransformer)
81+
ctx.synthesizeCompanionMethod(nme.COMPANION_CLASS_METHOD, forClass, mc).enteredAfter(thisTransformer)
82+
ctx.synthesizeCompanionMethod(nme.COMPANION_MODULE_METHOD, mc, forClass).enteredAfter(thisTransformer)
9183
ModuleDef(modul, Nil)
9284
}
9385

src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -432,18 +432,10 @@ class Namer { typer: Typer =>
432432
}
433433

434434
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
435+
val claz = ctx.denotNamed(classTree.name.encode).symbol
436+
val modl = ctx.denotNamed(moduleTree.name.encode).symbol
437+
ctx.synthesizeCompanionMethod(nme.COMPANION_CLASS_METHOD, claz, modl).entered
438+
ctx.synthesizeCompanionMethod(nme.COMPANION_MODULE_METHOD, modl, claz).entered
447439
}
448440

449441
def createCompanionLinks(implicit ctx: Context): Unit = {

0 commit comments

Comments
 (0)