@@ -19,31 +19,34 @@ import java.io.{ PrintWriter }
1919/** A base class for things that have positions (currently: modifiers and trees)
2020 */
2121abstract class Positioned (implicit @ constructorOnly src : SourceFile ) extends SrcPos , Product , Cloneable {
22+ import Positioned .{ids , nextId , debugId }
2223
2324 private var myUniqueId : Int = _
2425 private var mySpan : Span = _
2526
26- /** A unique identifier. Among other things, used for determining the source file
27- * component of the position .
27+ /** A unique identifier in case -Yshow-tree-ids, or -Ydebug-tree-with-id
28+ * is set, -1 otherwise .
2829 */
29- def uniqueId : Int = myUniqueId
30+ def uniqueId : Int =
31+ if ids != null && ids.containsKey(this ) then ids.get(this ) else - 1
3032
31- def uniqueId_= (id : Int ): Unit = {
32- def printTrace () = {
33- println(s " Debug tree (id= ${Positioned .debugId}) creation \n $this\n " )
34- Reporter .displayPrompt(Console .in, new PrintWriter (Console .err, true ))
35- }
36- if (Positioned .debugId == id) printTrace()
37- myUniqueId = id
38- }
33+ private def allocateId () =
34+ if ids != null then
35+ val ownId = nextId
36+ nextId += 1
37+ ids.put(this , ownId)
38+ if ownId == debugId then
39+ println(s " Debug tree (id= $debugId) creation \n $this\n " )
40+ Reporter .displayPrompt(Console .in, new PrintWriter (Console .err, true ))
41+
42+ allocateId()
3943
4044 /** The span part of the item's position */
4145 def span : Span = mySpan
4246
4347 def span_= (span : Span ): Unit =
4448 mySpan = span
4549
46- uniqueId = src.nextId
4750 span = envelope(src)
4851
4952 val source : SourceFile = src
@@ -125,7 +128,7 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Src
125128 /** Clone this node but assign it a fresh id which marks it as a node in `file`. */
126129 def cloneIn (src : SourceFile ): this .type = {
127130 val newpd : this .type = clone.asInstanceOf [this .type ]
128- newpd.uniqueId = src.nextId
131+ newpd.allocateId()
129132 // assert(newpd.uniqueId != 2208, s"source = $this, ${this.uniqueId}, ${this.span}")
130133 newpd
131134 }
@@ -237,8 +240,14 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Src
237240}
238241
239242object Positioned {
240- @ sharable private [Positioned ] var debugId = Int .MinValue
243+ @ sharable private var debugId = Int .MinValue
244+ @ sharable private var ids : java.util.WeakHashMap [Positioned , Int ] = null
245+ @ sharable private var nextId : Int = 0
241246
242- def updateDebugPos (using Context ): Unit =
247+ def init (using Context ): Unit =
243248 debugId = ctx.settings.YdebugTreeWithId .value
249+ if ctx.settings.YshowTreeIds .value
250+ || debugId != ctx.settings.YdebugTreeWithId .default
251+ then
252+ ids = java.util.WeakHashMap ()
244253}
0 commit comments