|
| 1 | +package dotty.tools.dotc.printing |
| 2 | + |
| 3 | +import dotty.tools.dotc.ast.Trees.{Closure, DefDef, Untyped, ValDef} |
| 4 | +import dotty.tools.dotc.ast.untpd.{PackageDef, Template, TypeDef} |
| 5 | +import dotty.tools.dotc.ast.{Trees, untpd} |
| 6 | +import dotty.tools.dotc.printing.Texts._ |
| 7 | +import dotty.tools.dotc.core.Contexts._ |
| 8 | +import dotty.tools.dotc.core.Flags._ |
| 9 | +import dotty.tools.dotc.core.Symbols._ |
| 10 | + |
| 11 | +import scala.language.implicitConversions |
| 12 | + |
| 13 | +class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) { |
| 14 | + |
| 15 | + override protected def modText(mods: untpd.Modifiers, kw: String): Text = { // DD |
| 16 | + val suppressKw = if (enclDefIsClass) mods is ParamAndLocal else mods is Param |
| 17 | + var flagMask = |
| 18 | + if (ctx.settings.YdebugFlags.value) AnyFlags |
| 19 | + else if (suppressKw) PrintableFlags &~ Private |
| 20 | + else PrintableFlags |
| 21 | + if (homogenizedView && mods.flags.isTypeFlags) flagMask &~= Implicit // drop implicit from classes |
| 22 | + val flags = mods.flags & flagMask |
| 23 | + val flagsText = if (flags.isEmpty) "" else keywordStr((mods.flags & flagMask).toString) |
| 24 | + val annotations = mods.annotations.filter(_.tpe != defn.SourceFileAnnotType) |
| 25 | + Text(annotations.map(annotText), " ") ~~ flagsText ~~ (Str(kw) provided !suppressKw) |
| 26 | + } |
| 27 | + |
| 28 | + override protected def blockText[T >: Untyped](trees: List[Trees.Tree[T]]): Text = { |
| 29 | + super.blockText(trees.filterNot(_.isInstanceOf[Closure[_]])) |
| 30 | + } |
| 31 | + |
| 32 | + override protected def packageDefText(tree: PackageDef): Text = { |
| 33 | + val stats = tree.stats.filter { |
| 34 | + case vdef: ValDef[_] => !vdef.symbol.is(Module) |
| 35 | + case _ => true |
| 36 | + } |
| 37 | + val statsText = stats match { |
| 38 | + case (pdef: PackageDef) :: Nil => toText(pdef) |
| 39 | + case _ => toTextGlobal(stats, "\n") |
| 40 | + } |
| 41 | + val bodyText = |
| 42 | + if (currentPrecedence == TopLevelPrec) "\n" ~ statsText else " {" ~ statsText ~ "}" |
| 43 | + keywordStr("package ") ~ toTextPackageId(tree.pid) ~ bodyText |
| 44 | + } |
| 45 | + |
| 46 | + override protected def templateText(tree: TypeDef, impl: Template): Text = { |
| 47 | + val decl = |
| 48 | + if (!tree.mods.is(Module)) modText(tree.mods, keywordStr(if ((tree).mods is Trait) "trait" else "class")) |
| 49 | + else modText(tree.mods &~ (Final | Module), keywordStr("object")) |
| 50 | + decl ~~ typeText(nameIdText(tree)) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~ "" |
| 51 | + } |
| 52 | + |
| 53 | + override protected def defDefToText[T >: Untyped](tree: DefDef[T]): Text = { |
| 54 | + import untpd.{modsDeco => _, _} |
| 55 | + dclTextOr(tree) { |
| 56 | + val printLambda = tree.symbol.isAnonymousFunction |
| 57 | + val prefix = modText(tree.mods, keywordStr("def")) ~~ valDefText(nameIdText(tree)) provided (!printLambda) |
| 58 | + withEnclosingDef(tree) { |
| 59 | + addVparamssText(prefix ~ tparamsText(tree.tparams), tree.vparamss) ~ optAscription(tree.tpt).provided(!printLambda) ~ |
| 60 | + optText(tree.rhs)((if (printLambda) " => " else " = ") ~ _) |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments