Skip to content

Commit d89fa53

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 a660b14 commit d89fa53

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
@@ -67,7 +67,10 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
6767

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

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

7275
override def nameString(name: Name): String =
7376
if (ctx.settings.YdebugNames.value) name.debugString else name.toString
@@ -435,7 +438,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
435438
case tree @ TypeDef(name, rhs) =>
436439
def typeDefText(tparamsText: => Text, rhsText: => Text) =
437440
dclTextOr(tree) {
438-
modText(tree.mods, tree.symbol, keywordStr("type")) ~~ (varianceText(tree.mods) ~ typeText(nameIdText(tree))) ~
441+
modText(tree.mods, tree.symbol, keywordStr("type"), isType = true) ~~
442+
(varianceText(tree.mods) ~ typeText(nameIdText(tree))) ~
439443
withEnclosingDef(tree) { tparamsText ~ rhsText }
440444
}
441445
def recur(rhs: Tree, tparamsTxt: => Text): Text = rhs match {
@@ -474,7 +478,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
474478
else toText(t)
475479
case tree @ ModuleDef(name, impl) =>
476480
withEnclosingDef(tree) {
477-
modText(tree.mods, NoSymbol, keywordStr("object")) ~~ nameIdText(tree) ~ toTextTemplate(impl)
481+
modText(tree.mods, NoSymbol, keywordStr("object"), isType = false) ~~
482+
nameIdText(tree) ~ toTextTemplate(impl)
478483
}
479484
case SymbolLit(str) =>
480485
"'" + str
@@ -529,8 +534,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
529534
t ~ cxBoundToText(cxb)
530535
}
531536
case PatDef(mods, pats, tpt, rhs) =>
532-
modText(mods, NoSymbol, keywordStr("val")) ~~ toText(pats, ", ") ~ optAscription(tpt) ~
533-
optText(rhs)(" = " ~ _)
537+
modText(mods, NoSymbol, keywordStr("val"), isType = false) ~~
538+
toText(pats, ", ") ~ optAscription(tpt) ~ optText(rhs)(" = " ~ _)
534539
case ParsedTry(expr, handler, finalizer) =>
535540
changePrec(GlobalPrec) {
536541
keywordStr("try ") ~ toText(expr) ~ " " ~ keywordStr("catch") ~ " {" ~ toText(handler) ~ "}" ~ optText(finalizer)(keywordStr(" finally ") ~ _)
@@ -653,7 +658,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
653658
protected def valDefToText[T >: Untyped](tree: ValDef[T]): Text = {
654659
import untpd.{modsDeco => _}
655660
dclTextOr(tree) {
656-
modText(tree.mods, tree.symbol, keywordStr(if (tree.mods is Mutable) "var" else "val")) ~~
661+
modText(tree.mods, tree.symbol, keywordStr(if (tree.mods is Mutable) "var" else "val"), isType = false) ~~
657662
valDefText(nameIdText(tree)) ~ optAscription(tree.tpt) ~
658663
withEnclosingDef(tree) { optText(tree.rhs)(" = " ~ _) }
659664
}
@@ -662,7 +667,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
662667
protected def defDefToText[T >: Untyped](tree: DefDef[T]): Text = {
663668
import untpd.{modsDeco => _}
664669
dclTextOr(tree) {
665-
val prefix = modText(tree.mods, tree.symbol, keywordStr("def")) ~~ valDefText(nameIdText(tree))
670+
val prefix = modText(tree.mods, tree.symbol, keywordStr("def"), isType = false) ~~ valDefText(nameIdText(tree))
666671
withEnclosingDef(tree) {
667672
addVparamssText(prefix ~ tparamsText(tree.tparams), tree.vparamss) ~ optAscription(tree.tpt) ~
668673
optText(tree.rhs)(" = " ~ _)
@@ -677,7 +682,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
677682
val prefix: Text =
678683
if (vparamss.isEmpty || primaryConstrs.nonEmpty) tparamsTxt
679684
else {
680-
var modsText = modText(constr.mods, constr.symbol, "")
685+
var modsText = modText(constr.mods, constr.symbol, "", isType = false)
681686
if (!modsText.isEmpty) modsText = " " ~ modsText
682687
if (constr.mods.hasAnnotations && !constr.mods.hasFlags) modsText = modsText ~~ " this"
683688
withEnclosingDef(constr) { addVparamssText(tparamsTxt ~~ modsText, vparamss) }
@@ -704,7 +709,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
704709
}
705710

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

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

730-
protected def modText(mods: untpd.Modifiers, sym: Symbol, kw: String, suppress: FlagSet = EmptyFlags): Text = { // DD
735+
protected def modText(mods: untpd.Modifiers, sym: Symbol, kw: String, isType: Boolean): Text = { // DD
731736
val suppressKw = if (enclDefIsClass) mods is ParamAndLocal else mods is Param
732737
var flagMask =
733738
if (ctx.settings.YdebugFlags.value) AnyFlags
734-
else if (suppressKw) PrintableFlags &~ Private &~ suppress
735-
else PrintableFlags &~ suppress
739+
else if (suppressKw) PrintableFlags(isType) &~ Private
740+
else PrintableFlags(isType)
736741
if (homogenizedView && mods.flags.isTypeFlags) flagMask &~= Implicit // drop implicit from classes
737742
val flags = (if (sym.exists) sym.flags else (mods.flags)) & flagMask
738743
val flagsText = if (flags.isEmpty) "" else keywordStr(flags.toString)
@@ -807,7 +812,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
807812
else {
808813
var flags = sym.flagsUNSAFE
809814
if (flags is TypeParam) flags = flags &~ Protected
810-
Text((flags & PrintableFlags).flagStrings map (flag => stringToText(keywordStr(flag))), " ")
815+
Text((flags & PrintableFlags(sym.isType)).flagStrings map (flag => stringToText(keywordStr(flag))), " ")
811816
}
812817

813818
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)