Skip to content

Commit 6b7a1a7

Browse files
committed
Print types and term flags separately
Distinguish what is printed by previous knowledge whether the symbol is a term or a type.
1 parent 60a14d4 commit 6b7a1a7

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

compiler/src/dotty/tools/dotc/core/Flags.scala

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,15 +435,20 @@ object Flags {
435435
// --------- Combined Flag Sets and Conjunctions ----------------------
436436

437437
/** Flags representing source modifiers */
438-
final val SourceModifierFlags: FlagSet =
439-
commonFlags(Private, Protected, Abstract, Final, Inline,
440-
Sealed, Case, Implicit, Override, AbsOverride, Lazy, JavaStatic, Erased)
438+
private val CommonSourceModifierFlags: FlagSet =
439+
commonFlags(Private, Protected, Final, Case, Implicit, Override, JavaStatic)
440+
441+
final val TypeSourceModifierFlags: FlagSet =
442+
CommonSourceModifierFlags.toTypeFlags | Abstract | Sealed
443+
444+
final val TermSourceModifierFlags: FlagSet =
445+
CommonSourceModifierFlags.toTermFlags | Inline | AbsOverride | Lazy | Erased
441446

442447
/** Flags representing modifiers that can appear in trees */
443448
final val ModifierFlags: FlagSet =
444-
SourceModifierFlags | Module | Param | Synthetic | Package | Local |
445-
commonFlags(Mutable)
446-
// | Trait is subsumed by commonFlags(Lazy) from SourceModifierFlags
449+
TypeSourceModifierFlags.toCommonFlags |
450+
TermSourceModifierFlags.toCommonFlags |
451+
commonFlags(Module, Param, Synthetic, Package, Local, Mutable, Trait)
447452

448453
assert(ModifierFlags.isTermFlags && ModifierFlags.isTypeFlags)
449454

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import dotty.tools.dotc.core.StdNames._
1414
class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
1515

1616
override protected def filterModTextAnnots(annots: List[untpd.Tree]): List[untpd.Tree] =
17-
annots.filter(_.tpe != defn.SourceFileAnnotType)
17+
super.filterModTextAnnots(annots).filter(_.tpe != defn.SourceFileAnnotType)
1818

1919
override protected def blockToText[T >: Untyped](block: Block[T]): Text =
2020
block match {
@@ -48,8 +48,8 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
4848

4949
override protected def templateText(tree: TypeDef, impl: Template): Text = {
5050
val decl =
51-
if (!tree.mods.is(Module)) modText(tree.mods, tree.symbol, keywordStr(if ((tree).mods is Trait) "trait" else "class"))
52-
else modText(tree.mods, tree.symbol, keywordStr("object"), suppress = Final | Module)
51+
if (!tree.mods.is(Module)) modText(tree.mods, tree.symbol, keywordStr(if ((tree).mods is Trait) "trait" else "class"), isType = true)
52+
else modText(tree.mods, tree.symbol, keywordStr("object"), isType = false)
5353
decl ~~ typeText(nameIdText(tree)) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~ ""
5454
}
5555

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
6868

6969
override protected def recursionLimitExceeded(): Unit = {}
7070

71-
protected val PrintableFlags: FlagSet = (SourceModifierFlags | Module | Local).toCommonFlags
71+
protected def PrintableFlags(isType: Boolean): FlagSet = {
72+
if (isType) TypeSourceModifierFlags | Module | Local
73+
else TermSourceModifierFlags | Module | Local
74+
}.toCommonFlags
7275

7376
override def nameString(name: Name): String =
7477
if (ctx.settings.YdebugNames.value) name.debugString else name.toString
@@ -440,7 +443,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
440443
case tree @ TypeDef(name, rhs) =>
441444
def typeDefText(tparamsText: => Text, rhsText: => Text) =
442445
dclTextOr(tree) {
443-
modText(tree.mods, tree.symbol, keywordStr("type")) ~~ (varianceText(tree.mods) ~ typeText(nameIdText(tree))) ~
446+
modText(tree.mods, tree.symbol, keywordStr("type"), isType = true) ~~
447+
(varianceText(tree.mods) ~ typeText(nameIdText(tree))) ~
444448
withEnclosingDef(tree) { tparamsText ~ rhsText }
445449
}
446450
def recur(rhs: Tree, tparamsTxt: => Text): Text = rhs match {
@@ -479,7 +483,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
479483
else toText(t)
480484
case tree @ ModuleDef(name, impl) =>
481485
withEnclosingDef(tree) {
482-
modText(tree.mods, NoSymbol, keywordStr("object")) ~~ nameIdText(tree) ~ toTextTemplate(impl)
486+
modText(tree.mods, NoSymbol, keywordStr("object"), isType = false) ~~
487+
nameIdText(tree) ~ toTextTemplate(impl)
483488
}
484489
case SymbolLit(str) =>
485490
"'" + str
@@ -534,8 +539,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
534539
t ~ cxBoundToText(cxb)
535540
}
536541
case PatDef(mods, pats, tpt, rhs) =>
537-
modText(mods, NoSymbol, keywordStr("val")) ~~ toText(pats, ", ") ~ optAscription(tpt) ~
538-
optText(rhs)(" = " ~ _)
542+
modText(mods, NoSymbol, keywordStr("val"), isType = false) ~~
543+
toText(pats, ", ") ~ optAscription(tpt) ~ optText(rhs)(" = " ~ _)
539544
case ParsedTry(expr, handler, finalizer) =>
540545
changePrec(GlobalPrec) {
541546
keywordStr("try ") ~ toText(expr) ~ " " ~ keywordStr("catch") ~ " {" ~ toText(handler) ~ "}" ~ optText(finalizer)(keywordStr(" finally ") ~ _)
@@ -658,7 +663,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
658663
protected def valDefToText[T >: Untyped](tree: ValDef[T]): Text = {
659664
import untpd.{modsDeco => _}
660665
dclTextOr(tree) {
661-
modText(tree.mods, tree.symbol, keywordStr(if (tree.mods is Mutable) "var" else "val")) ~~
666+
modText(tree.mods, tree.symbol, keywordStr(if (tree.mods is Mutable) "var" else "val"), isType = false) ~~
662667
valDefText(nameIdText(tree)) ~ optAscription(tree.tpt) ~
663668
withEnclosingDef(tree) { optText(tree.rhs)(" = " ~ _) }
664669
}
@@ -667,7 +672,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
667672
protected def defDefToText[T >: Untyped](tree: DefDef[T]): Text = {
668673
import untpd.{modsDeco => _}
669674
dclTextOr(tree) {
670-
val prefix = modText(tree.mods, tree.symbol, keywordStr("def")) ~~ valDefText(nameIdText(tree))
675+
val prefix = modText(tree.mods, tree.symbol, keywordStr("def"), isType = false) ~~ valDefText(nameIdText(tree))
671676
withEnclosingDef(tree) {
672677
addVparamssText(prefix ~ tparamsText(tree.tparams), tree.vparamss) ~ optAscription(tree.tpt) ~
673678
optText(tree.rhs)(" = " ~ _)
@@ -682,7 +687,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
682687
val prefix: Text =
683688
if (vparamss.isEmpty || primaryConstrs.nonEmpty) tparamsTxt
684689
else {
685-
var modsText = modText(constr.mods, constr.symbol, "")
690+
var modsText = modText(constr.mods, constr.symbol, "", isType = false)
686691
if (!modsText.isEmpty) modsText = " " ~ modsText
687692
if (constr.mods.hasAnnotations && !constr.mods.hasFlags) modsText = modsText ~~ " this"
688693
withEnclosingDef(constr) { addVparamssText(tparamsTxt ~~ modsText, vparamss) }
@@ -709,7 +714,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
709714
}
710715

711716
protected def templateText(tree: TypeDef, impl: Template): Text = {
712-
val decl = modText(tree.mods, tree.symbol, keywordStr(if ((tree).mods is Trait) "trait" else "class"))
717+
val decl = modText(tree.mods, tree.symbol, keywordStr(if ((tree).mods is Trait) "trait" else "class"), isType = true)
713718
decl ~~ typeText(nameIdText(tree)) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~
714719
(if (tree.hasType && ctx.settings.verbose.value) i"[decls = ${tree.symbol.info.decls}]" else "")
715720
}
@@ -732,12 +737,12 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
732737

733738
protected def annotText(tree: untpd.Tree): Text = "@" ~ constrText(tree) // DD
734739

735-
protected def modText(mods: untpd.Modifiers, sym: Symbol, kw: String, suppress: FlagSet = EmptyFlags): Text = { // DD
740+
protected def modText(mods: untpd.Modifiers, sym: Symbol, kw: String, isType: Boolean): Text = { // DD
736741
val suppressKw = if (enclDefIsClass) mods is ParamAndLocal else mods is Param
737742
var flagMask =
738743
if (ctx.settings.YdebugFlags.value) AnyFlags
739-
else if (suppressKw) PrintableFlags &~ Private &~ suppress
740-
else PrintableFlags &~ suppress
744+
else if (suppressKw) PrintableFlags(isType) &~ Private
745+
else PrintableFlags(isType)
741746
if (homogenizedView && mods.flags.isTypeFlags) flagMask &~= Implicit // drop implicit from classes
742747
val flags = (if (sym.exists) sym.flags else (mods.flags)) & flagMask
743748
val flagsText = if (flags.isEmpty) "" else keywordStr(flags.toString)
@@ -811,7 +816,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
811816
else {
812817
var flags = sym.flagsUNSAFE
813818
if (flags is TypeParam) flags = flags &~ Protected
814-
Text((flags & PrintableFlags).flagStrings map (flag => stringToText(keywordStr(flag))), " ")
819+
Text((flags & PrintableFlags(sym.isType)).flagStrings map (flag => stringToText(keywordStr(flag))), " ")
815820
}
816821

817822
override def toText(denot: Denotation): Text = denot match {

doc-tool/src/dotty/tools/dottydoc/model/factories.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ object factories {
2020
type TypeTree = dotty.tools.dotc.ast.Trees.Tree[Type]
2121

2222
def flags(t: Tree)(implicit ctx: Context): List[String] =
23-
(t.symbol.flags & SourceModifierFlags)
23+
(t.symbol.flags & (if (t.symbol.isType) TypeSourceModifierFlags else TermSourceModifierFlags))
2424
.flagStrings.toList
2525
.filter(_ != "<trait>")
2626
.filter(_ != "interface")

0 commit comments

Comments
 (0)