Skip to content

Commit 3301cf4

Browse files
committed
Move more logic from RefinedPrinter to DecompilerPrinter
RefinedPrinter cannot make use of symbols in order to be able to print after all phases (e.g. Parser).
1 parent 6971c0d commit 3301cf4

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dotty.tools.dotc.printing
22

33
import dotty.tools.dotc.ast.Trees._
4-
import dotty.tools.dotc.ast.untpd.{PackageDef, Template, TypeDef}
4+
import dotty.tools.dotc.ast.untpd.{Tree, PackageDef, Template, TypeDef}
55
import dotty.tools.dotc.ast.{Trees, untpd}
66
import dotty.tools.dotc.printing.Texts._
77
import dotty.tools.dotc.core.Contexts._
@@ -54,16 +54,22 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
5454
decl ~~ typeText(nameIdText(tree)) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~ ""
5555
}
5656

57-
override protected def selfToText(impl: Template): Text =
58-
super.selfToText(impl).provided(!impl.constr.symbol.owner.is(Module))
59-
6057
override protected def toTextTemplate(impl: Template, ofNew: Boolean = false): Text = {
61-
def isSyntheticParent(sym: Symbol): Boolean = {
58+
def isSynthetic(parent: Tree): Boolean = {
59+
val sym = parent.symbol
6260
sym.maybeOwner == defn.ObjectClass ||
6361
(sym == defn.ProductClass && impl.symbol.owner.is(Case))
6462
}
65-
val impl1 = impl.copy(parents = impl.parents.filterNot(p => isSyntheticParent(p.symbol)))
66-
super.toTextTemplate(impl1, ofNew)
63+
val parents = impl.parents.filterNot(isSynthetic)
64+
65+
// We don't print self type and constructor for objects
66+
val isObject = impl.constr.symbol.owner.is(Module)
67+
if (isObject) {
68+
val parentsText = keywordText(" extends") ~~ Text(parents.map(constrText), keywordStr(" with "))
69+
val bodyText = " {" ~~ toTextGlobal(impl.body, "\n") ~ "}"
70+
parentsText.provided(parents.nonEmpty) ~ bodyText
71+
}
72+
else super.toTextTemplate(impl.copy(parents = parents), ofNew)
6773
}
6874

6975
override protected def typeApplyText[T >: Untyped](tree: TypeApply[T]): Text = {

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -640,16 +640,18 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
640640
val tparamsTxt = withEnclosingDef(constr) { tparamsText(tparams) }
641641
val primaryConstrs = if (constr.rhs.isEmpty) Nil else constr :: Nil
642642
val prefix: Text =
643-
if (constr.symbol.owner.is(Module)) ""
644-
else if (vparamss.isEmpty || primaryConstrs.nonEmpty) tparamsTxt
643+
if (vparamss.isEmpty || primaryConstrs.nonEmpty) tparamsTxt
645644
else {
646645
var modsText = modText(constr.mods, "")
647646
if (!modsText.isEmpty) modsText = " " ~ modsText
648647
if (constr.mods.hasAnnotations && !constr.mods.hasFlags) modsText = modsText ~~ " this"
649648
withEnclosingDef(constr) { addVparamssText(tparamsTxt ~~ modsText, vparamss) }
650649
}
651650
val parentsText = Text(parents map constrText, keywordStr(" with "))
652-
val selfText = selfToText(impl)
651+
val selfText = {
652+
val selfName = if (self.name == nme.WILDCARD) keywordStr("this") else self.name.toString
653+
(selfName ~ optText(self.tpt)(": " ~ _) ~ " =>").close
654+
}.provided(!self.isEmpty)
653655
val body = if (ctx.settings.YtestPickler.value) {
654656
// Pickling/unpickling reorders the body members, so we need to homogenize
655657
val (params, rest) = impl.body partition {
@@ -661,17 +663,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
661663
params ::: rest
662664
} else impl.body
663665

664-
val bodyText =
665-
(if (constr.symbol.owner.is(Module)) " {" else "{") ~~
666-
selfText ~~ toTextGlobal(primaryConstrs ::: body, "\n") ~ "}"
667-
668-
prefix ~ (keywordText(" extends") provided (!ofNew && parents.nonEmpty)) ~~ parentsText ~~ bodyText
669-
}
666+
val bodyText = " {" ~~ selfText ~~ toTextGlobal(primaryConstrs ::: body, "\n") ~ "}"
670667

671-
protected def selfToText(impl: Template): Text = {
672-
val self = impl.self
673-
val selfName = if (self.name == nme.WILDCARD) keywordStr("this") else self.name.toString
674-
(selfName ~ optText(self.tpt)(": " ~ _) ~ " =>").close provided (!self.isEmpty)
668+
prefix ~ keywordText(" extends").provided(!ofNew && parents.nonEmpty) ~~ parentsText ~ bodyText
675669
}
676670

677671
protected def templateText(tree: TypeDef, impl: Template): Text = {

0 commit comments

Comments
 (0)