Skip to content

Commit 4421092

Browse files
committed
Refactor SourceFile operations
Deemphasize AbstractFile, drop TreeIds.
1 parent d1008fc commit 4421092

File tree

19 files changed

+142
-177
lines changed

19 files changed

+142
-177
lines changed

compiler/src/dotty/tools/dotc/ast/Positioned.scala

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ package dotc
33
package ast
44

55
import util.Spans._
6-
import util.{SourceFile, SourcePosition}
6+
import util.{SourceFile, NoSource, SourcePosition}
77
import core.Contexts.{Context, SourceInfo}
88
import core.Decorators._
99
import core.Flags.{JavaDefined, Extension}
1010
import core.StdNames.nme
11-
import io.AbstractFile
1211
import annotation.transientParam
1312
import annotation.internal.sharable
1413

@@ -28,12 +27,11 @@ abstract class Positioned(implicit @transientParam src: SourceInfo) extends Prod
2827
/** item's id */
2928
def uniqueId: Int = myUniqueId
3029

31-
protected def srcfile: AbstractFile = TreeIds.fileOfId(uniqueId)
32-
def source(implicit ctx: Context): SourceFile = ctx.getSource(srcfile)
30+
def source: SourceFile = SourceFile.fromId(uniqueId)
3331
def sourcePos(implicit ctx: Context): SourcePosition = source.atSpan(span)
3432

35-
setId(TreeIds.nextIdFor(initialFile(src)))
36-
setPos(initialSpan(), srcfile)
33+
setId(initialSource(src).nextId)
34+
setPos(initialSpan(), source)
3735

3836
protected def setId(id: Int): Unit = {
3937
myUniqueId = id
@@ -43,7 +41,7 @@ abstract class Positioned(implicit @transientParam src: SourceInfo) extends Prod
4341
/** Destructively update `mySpan` to given span and potentially update `id` so that
4442
* it refers to `file`. Also, set any missing positions in children.
4543
*/
46-
protected def setPos(span: Span, file: AbstractFile): Unit = {
44+
protected def setPos(span: Span, file: SourceFile): Unit = {
4745
setOnePos(span, file)
4846
if (span.exists) setChildPositions(span.toSynthetic, file)
4947
}
@@ -56,34 +54,34 @@ abstract class Positioned(implicit @transientParam src: SourceInfo) extends Prod
5654
def withSpan(span: Span): this.type = {
5755
val ownSpan = this.span
5856
val newpd: this.type =
59-
if (span == ownSpan || ownSpan.isSynthetic) this else cloneIn(srcfile)
60-
newpd.setPos(span, srcfile)
57+
if (span == ownSpan || ownSpan.isSynthetic) this else cloneIn(source)
58+
newpd.setPos(span, source)
6159
newpd
6260
}
6361

6462
def withPosOf(posd: Positioned): this.type = {
6563
val ownSpan = this.span
6664
val newpd: this.type =
67-
if (posd.srcfile == srcfile && posd.span == ownSpan || ownSpan.isSynthetic) this
68-
else cloneIn(posd.srcfile)
69-
newpd.setPos(posd.span, posd.srcfile)
65+
if ((posd.source `eq` source) && posd.span == ownSpan || ownSpan.isSynthetic) this
66+
else cloneIn(posd.source)
67+
newpd.setPos(posd.span, posd.source)
7068
newpd
7169
}
7270

7371
def withSourcePos(sourcePos: SourcePosition): this.type = {
7472
val ownSpan = this.span
7573
val newpd: this.type =
76-
if (sourcePos.source.file == srcfile && sourcePos.span == ownSpan || ownSpan.isSynthetic) this
77-
else cloneIn(sourcePos.source.file)
78-
newpd.setPos(sourcePos.span, sourcePos.source.file)
74+
if ((sourcePos.source `eq` source) && sourcePos.span == ownSpan || ownSpan.isSynthetic) this
75+
else cloneIn(sourcePos.source)
76+
newpd.setPos(sourcePos.span, sourcePos.source)
7977
newpd
8078
}
8179

8280
/** Set span of this tree only, without updating children spans.
8381
* Called from Unpickler when entering positions.
8482
*/
85-
private[dotc] def setOnePos(span: Span, file: AbstractFile = this.srcfile): Unit = {
86-
if (file != this.srcfile) setId(TreeIds.nextIdFor(file))
83+
private[dotc] def setOnePos(span: Span, file: SourceFile = this.source): Unit = {
84+
if (file `ne` this.source) setId(file.nextId)
8785
mySpan = span
8886
}
8987

@@ -98,7 +96,7 @@ abstract class Positioned(implicit @transientParam src: SourceInfo) extends Prod
9896
* But since mutual tail recursion is not supported in Scala, we express it instead
9997
* as a while loop with a termination by return in the middle.
10098
*/
101-
private def setChildPositions(span: Span, file: AbstractFile): Unit = {
99+
private def setChildPositions(span: Span, file: SourceFile): Unit = {
102100
var n = productArity // subnodes are analyzed right to left
103101
var elems: List[Any] = Nil // children in lists still to be considered, from right to left
104102
var end = span.end // the last defined offset, fill in spans up to this offset
@@ -150,39 +148,39 @@ abstract class Positioned(implicit @transientParam src: SourceInfo) extends Prod
150148
}
151149

152150
/** Clone this node but assign it a fresh id which marks it as a node in `file`. */
153-
protected def cloneIn(file: AbstractFile): this.type = {
151+
protected def cloneIn(file: SourceFile): this.type = {
154152
val newpd: this.type = clone.asInstanceOf[this.type]
155-
newpd.setId(TreeIds.nextIdFor(file))
153+
newpd.setId(file.nextId)
156154
newpd
157155
}
158156

159157
/** The source of the first element of this node that has a position */
160-
def elemsFile: AbstractFile = {
161-
def firstFile(x: Any): AbstractFile = x match {
158+
def elemsSource: SourceFile = {
159+
def firstSource(x: Any): SourceFile = x match {
162160
case x: Positioned if x.span.exists =>
163-
x.srcfile
161+
x.source
164162
case x1 :: xs1 =>
165-
val f = firstFile(x1)
166-
if (f != null) f else firstFile(xs1)
163+
val f = firstSource(x1)
164+
if (f.exists) f else firstSource(xs1)
167165
case _ =>
168-
null
166+
NoSource
169167
}
170-
def firstElemFile(n: Int): AbstractFile =
171-
if (n == productArity) null
168+
def firstElemSource(n: Int): SourceFile =
169+
if (n == productArity) NoSource
172170
else {
173-
val f = firstFile(productElement(n))
174-
if (f != null) f else firstElemFile(n + 1)
171+
val f = firstSource(productElement(n))
172+
if (f.exists) f else firstElemSource(n + 1)
175173
}
176-
firstElemFile(0)
174+
firstElemSource(0)
177175
}
178176

179177
/** The source file to use for the `id` of this node. This is
180-
* `elemsFile` if it exists, or the source file indicated by
178+
* `elemsSource` if it exists, or the source file indicated by
181179
* `src` otherwise
182180
*/
183-
private def initialFile(src: SourceInfo): AbstractFile = {
184-
val f = elemsFile
185-
if (f != null) f else src.srcfile
181+
private def initialSource(src: SourceInfo): SourceFile = {
182+
val f = elemsSource
183+
if (f.exists) f else src.source
186184
}
187185

188186
/** The initial, synthetic span. This is usually the union of all positioned children's spans.
@@ -219,7 +217,7 @@ abstract class Positioned(implicit @transientParam src: SourceInfo) extends Prod
219217
span.toSynthetic
220218
}
221219

222-
private def sameSource(that: Positioned) = srcfile == that.srcfile
220+
private def sameSource(that: Positioned) = source `eq` that.source
223221

224222
def contains(that: Positioned): Boolean = {
225223
def isParent(x: Any): Boolean = x match {

compiler/src/dotty/tools/dotc/ast/TreeIds.scala

Lines changed: 0 additions & 53 deletions
This file was deleted.

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import printing.Printer
1111
import printing.Texts.Text
1212
import util.{Stats, Attachment, Property, SourceFile, SourcePosition}
1313
import config.Config
14-
import io.AbstractFile
1514
import annotation.internal.sharable
1615
import annotation.unchecked.uncheckedVariance
1716
import annotation.transientParam
@@ -134,7 +133,7 @@ object Trees {
134133
val tree =
135134
(if (myTpe == null ||
136135
(myTpe.asInstanceOf[AnyRef] eq tpe.asInstanceOf[AnyRef])) this
137-
else cloneIn(srcfile)).asInstanceOf[Tree[Type]]
136+
else cloneIn(source)).asInstanceOf[Tree[Type]]
138137
tree overwriteType tpe
139138
tree.asInstanceOf[ThisTree[Type]]
140139
}
@@ -325,7 +324,7 @@ object Trees {
325324
def rawComment: Option[Comment] = getAttachment(DocComment)
326325

327326
def withMods(mods: untpd.Modifiers): ThisTree[Untyped] = {
328-
val tree = if (myMods == null || (myMods == mods)) this else cloneIn(srcfile)
327+
val tree = if (myMods == null || (myMods == mods)) this else cloneIn(source)
329328
tree.setMods(mods)
330329
tree.asInstanceOf[ThisTree[Untyped]]
331330
}
@@ -777,7 +776,7 @@ object Trees {
777776
trait WithoutTypeOrPos[-T >: Untyped] extends Tree[T] {
778777
override def withTypeUnchecked(tpe: Type): ThisTree[Type] = this.asInstanceOf[ThisTree[Type]]
779778
override def span: Span = NoSpan
780-
override def setPos(span: Span, file: AbstractFile): Unit = {}
779+
override def setPos(span: Span, src: SourceFile): Unit = {}
781780
}
782781

783782
/** Temporary class that results from translation of ModuleDefs
@@ -795,7 +794,7 @@ object Trees {
795794
if (trees eq newTrees)
796795
this
797796
else
798-
Thicket[T](newTrees)(SourceInfo(srcfile)).asInstanceOf[this.type]
797+
Thicket[T](newTrees)(SourceInfo(source)).asInstanceOf[this.type]
799798
}
800799

801800
override def foreachInThicket(op: Tree[T] => Unit): Unit =

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,10 +1134,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
11341134
/** The source file where the symbol of the `inline` method referred to by `call`
11351135
* is defined
11361136
*/
1137-
def sourceFile(call: Tree)(implicit ctx: Context): SourceFile = {
1138-
val file = call.symbol.sourceFile
1139-
if (file != null && file.exists) ctx.getSource(file) else NoSource
1140-
}
1137+
def sourceFile(call: Tree)(implicit ctx: Context): SourceFile = call.symbol.source
11411138

11421139
/** Desugar identifier into a select node. Return the tree itself if not possible */
11431140
def desugarIdent(tree: Ident)(implicit ctx: Context): Tree = {

0 commit comments

Comments
 (0)