Skip to content

Commit 401fae7

Browse files
committed
Some progress in printing things.
1 parent 831abc1 commit 401fae7

File tree

5 files changed

+113
-26
lines changed

5 files changed

+113
-26
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,15 @@ object Contexts {
4848
_typeComparer
4949
}
5050

51-
private[this] var _printer: Context => Printer = _
52-
protected def printer_=(printer: Context => Printer) = _printer = printer
53-
def printer: Context => Printer = _printer
51+
private[this] var _plainPrinter: Context => Printer = _
52+
protected def plainPrinter_=(plainPrinter: Context => Printer) = _plainPrinter = plainPrinter
53+
def plainPrinter: Context => Printer = _plainPrinter
54+
55+
private[this] var _refinedPrinter: Context => Printer = _
56+
protected def refinedPrinter_=(refinedPrinter: Context => Printer) = _refinedPrinter = refinedPrinter
57+
def refinedPrinter: Context => Printer = _refinedPrinter
58+
59+
def printer = if (base.settings.debug.value) plainPrinter else refinedPrinter
5460

5561
private[this] var _owner: Symbol = _
5662
protected def owner_=(owner: Symbol) = _owner = owner
@@ -75,7 +81,8 @@ object Contexts {
7581
if (_condensed == null)
7682
_condensed = base.initialCtx.fresh
7783
.withPeriod(period)
78-
.withPrinter(printer)
84+
.withPlainPrinter(plainPrinter)
85+
.withRefinedPrinter(refinedPrinter)
7986
.withSettings(sstate)
8087
_condensed
8188
}
@@ -94,7 +101,8 @@ object Contexts {
94101
def withPeriod(period: Period): this.type = { this.period = period; this }
95102
def withPhase(pid: PhaseId): this.type = withPeriod(Period(runId, pid))
96103
def withConstraints(constraints: Constraints): this.type = { this.constraints = constraints; this }
97-
def withPrinter(printer: Context => Printer): this.type = { this.printer = printer; this }
104+
def withPlainPrinter(printer: Context => Printer): this.type = { this.plainPrinter = printer; this }
105+
def withRefinedPrinter(printer: Context => Printer): this.type = { this.refinedPrinter = printer; this }
98106
def withOwner(owner: Symbol): this.type = { this.owner = owner; this }
99107
def withSettings(sstate: SettingsState): this.type = { this.sstate = sstate; this }
100108
def withDiagnostics(diagnostics: Option[StringBuilder]): this.type = { this.diagnostics = diagnostics; this }
@@ -104,7 +112,8 @@ object Contexts {
104112
underlying = NoContext
105113
period = Nowhere
106114
constraints = Map()
107-
printer = new StdPrinter()(_)
115+
plainPrinter = new PlainPrinter(_)
116+
refinedPrinter = new RefinedPrinter(_)
108117
owner = NoSymbol
109118
}
110119

@@ -134,6 +143,12 @@ object Contexts {
134143
class ContextState {
135144

136145
// Symbols state
146+
147+
/** A counter for unique ids */
148+
private[core] var _nextId = 0
149+
150+
def nextId = { _nextId += 1; _nextId }
151+
137152
/** A map from a superclass id to the class that has it */
138153
private[core] var classOfId = new Array[ClassSymbol](InitialSuperIdsSize)
139154

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ object Flags {
316316
/** Symbol is a method which should be marked ACC_SYNCHRONIZED */
317317
final val Synchronized = termFlag(???, "<synchronized>")
318318

319+
/** Symbol's name is expanded */
320+
final val ExpandedName = commonFlag(???, "<expandedname>")
321+
319322
/** Symbol should be ignored when typechecking; will be marked ACC_SYNTHETIC in bytecode */
320323
final val Artifact = commonFlag(???, "<artifact>")
321324

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

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

4-
import Types._, Symbols._, Contexts._, Scopes._, Names._
4+
import Types._, Symbols._, Contexts._, Scopes._, Names._, NameOps._, Flags._
55

66
trait Printers { this: Context =>
77

@@ -40,10 +40,19 @@ object Printers {
4040
def show(sc: Scope): String
4141
def show(syms: List[Symbol], sep: String): String
4242
def showNameDetailed(name: Name): String
43+
def showFullName(sym: Symbol): String
44+
45+
/** String representation of symbol's simple name.
46+
* If !settings.debug translates expansions of operators back to operator symbol.
47+
* E.g. $eq => =.
48+
* If settings.uniqid, adds id.
49+
* If settings.Yshowsymkinds, adds abbreviated symbol kind.
50+
*/
51+
def showName(sym: Symbol): String
4352
}
4453

45-
class StdPrinter(implicit ctx: Context) extends Printer {
46-
54+
class PlainPrinter(_ctx: Context) extends Printer {
55+
protected[this] implicit val ctx = _ctx
4756
def controlled(op: => String): String =
4857
if (ctx.showRecursions < maxShowRecursions)
4958
try {
@@ -53,22 +62,25 @@ object Printers {
5362
ctx.showRecursions -= 1
5463
}
5564
else {
56-
if (ctx.debug) {
57-
ctx.warning("Exceeded recursion depth attempting to print type.")
58-
(new Throwable).printStackTrace
59-
}
65+
recursionLimitExceeeded()
6066
"..."
6167
}
6268

63-
def show(tp: Type): String = controlled {
64-
tp match {
65-
case TermRef(pre, name) =>
66-
??? // showPrefix(pre) + show(name)
69+
protected def recursionLimitExceeeded() = {
70+
ctx.warning("Exceeded recursion depth attempting to print type.")
71+
(new Throwable).printStackTrace
72+
}
6773

74+
protected def showSimpleName(sym: Symbol) = sym.originalName.decode
6875

76+
def showName(sym: Symbol): String =
77+
showSimpleName(sym) + (if (ctx.settings.uniqid.value) "#" + sym.id else "")
6978

70-
}
71-
}
79+
def showFullName(sym: Symbol): String =
80+
if (sym.isRoot || sym == NoSymbol || sym.owner.isEffectiveRoot)
81+
showName(sym)
82+
else
83+
showFullName(sym.effectiveOwner.enclosingClass) + "." + showName(sym)
7284

7385
protected def objectPrefix = "object "
7486
protected def packagePrefix = "package "
@@ -80,28 +92,51 @@ object Printers {
8092
(defn.UnqualifiedOwners contains sym) || isEmptyPrefix(sym)
8193

8294
protected def isEmptyPrefix(sym: Symbol) =
83-
sym.isEffectiveRoot || sym.isAnonymousClass || ??? // nme.isReplWrapperName(sym.name)
84-
95+
sym.isEffectiveRoot || sym.isAnonymousClass || sym.name.isReplWrapperName
8596

8697
def showPrefix(tp: Type): String = controlled {
8798
tp match {
99+
case RefinedThis(_) =>
100+
"this."
88101
case ThisType(cls) =>
89-
if (ctx.debug) showName(cls) + ".this."
90-
else if (cls.isAnonymousClass) "this."
91-
else ???
102+
showName(cls) + ".this."
103+
case SuperType(thistpe, _) =>
104+
showPrefix(thistpe).replaceAll("""\bthis\.$""", "super.")
105+
case tp @ TermRef(pre, name) =>
106+
val sym = tp.symbol
107+
if (!sym.exists) showPrefix(pre) + name + "."
108+
else showPrefix(pre) + showName(sym) + "."
109+
case MethodParam(mt, idx) =>
110+
mt.paramNames(idx) + "."
92111
case NoPrefix =>
93112
""
113+
case ConstantType(value) =>
114+
"(" + value + ")."
94115
case _ =>
95116
trimPrefix(show(tp)) + "#"
117+
}
118+
}
119+
120+
def show(tp: Type): String = controlled {
121+
tp match {
122+
case tp: SingletonType =>
123+
val str = showPrefix(tp)
124+
if (str.endsWith(".")) str + "type"
125+
else showFullName(tp.underlying.typeSymbol.skipPackageObject) + ".type"
126+
case
127+
TermRef(pre, name) =>
128+
??? // showPrefix(pre) + show(name)
129+
130+
96131

97132
}
98133
}
99134

135+
100136
def show(sym: Symbol): String = controlled {
101137

102138
???
103139
}
104-
def showName(sym: Symbol): String = ???
105140
def showLocated(sym: Symbol): String = ???
106141
def showDef(sym: Symbol): String = ???
107142
def show(sc: Scope): String =
@@ -112,9 +147,29 @@ object Printers {
112147

113148
def showNameDetailed(name: Name): String =
114149
(if (name.isTypeName) "type " else "term ") + name
150+
}
151+
152+
class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
153+
override protected def recursionLimitExceeeded() = {}
115154

155+
override protected def showSimpleName(sym: Symbol) = sym.name.toString
156+
157+
override def showPrefix(tp: Type): String = controlled {
158+
tp match {
159+
case ThisType(cls) =>
160+
if (cls.isAnonymousClass) return "this."
161+
if (isOmittablePrefix(cls)) return ""
162+
if (cls.isModuleClass) return showFullName(cls) + "."
163+
case tp @ TermRef(pre, name) =>
164+
val sym = tp.symbol
165+
if (sym is PackageObject) return showPrefix(pre)
166+
if (isOmittablePrefix(sym)) return ""
167+
case _ =>
168+
}
169+
super.showPrefix(tp)
170+
}
116171
}
117172

118-
final val maxShowRecursions = 50
173+
final val maxShowRecursions = 100
119174

120175
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ object SymDenotations {
7878
/** is this denotation a method? */
7979
//def isMethod: Boolean = false
8080

81+
def originalName =
82+
if (flags is ExpandedName) initial.asSymDenotation.name else name
83+
8184
def isSubClass(cls: Symbol)(implicit ctx: Context) = false
8285

8386
def isNonBottomSubClass(cls: Symbol)(implicit ctx: Context) = false

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ object Symbols {
145145
denot
146146
}
147147

148+
private[this] var _id: Int = _
149+
150+
/** The unique id of this symbol */
151+
def id(implicit ctx: Context) = {
152+
if (_id == 0) _id = ctx.nextId
153+
_id
154+
}
155+
148156
/** Subclass tests and casts */
149157
final def isTerm: Boolean = isInstanceOf[TermSymbol]
150158
final def isType: Boolean = isInstanceOf[TypeSymbol]
@@ -201,6 +209,9 @@ object Symbols {
201209
/** The current annotations of this symbol */
202210
final def annotations(implicit ctx: Context): List[Annotation] = denot.annotations
203211

212+
/** The name with which this symbol was created */
213+
def originalName(implicit ctx: Context) = denot.originalName
214+
204215
/** Set given flags(s) of this symbol */
205216
def setFlag(flags: FlagSet)(implicit ctx: Context): Unit = denot.setFlag(flags)
206217

0 commit comments

Comments
 (0)