1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
package dotty.tools
package dotc
package core
package tasty
import ast._
import ast.Trees._
import ast.Trees.WithLazyField
import TastyFormat._
import core._
import Contexts._, Symbols._, Types._, Names._, Constants._, Decorators._, Annotations._
import collection.mutable
import TastyBuffer._
import util.Positions._
class PositionPickler(pickler: TastyPickler, addrsOfTree: tpd.Tree => List[Addr]) {
val buf = new TastyBuffer(5000)
pickler.newSection("Positions", buf)
import buf._
import ast.tpd._
private val remainingAddrs = new java.util.IdentityHashMap[Tree, Iterator[Addr]]
def nextTreeAddr(tree: Tree): Option[Addr] = remainingAddrs.get(tree) match {
case null =>
addrsOfTree(tree) match {
case Nil =>
None
case addr :: Nil =>
Some(addr)
case addrs =>
remainingAddrs.put(tree, addrs.iterator)
nextTreeAddr(tree)
}
case it: Iterator[_] =>
if (it.hasNext) Some(it.next) else None
}
def header(addrDelta: Int, hasStartDelta: Boolean, hasEndDelta: Boolean) = {
def toInt(b: Boolean) = if (b) 1 else 0
(addrDelta << 2) | (toInt(hasStartDelta) << 1) | toInt(hasEndDelta)
}
def picklePositions(roots: List[Tree])(implicit ctx: Context) = {
var lastIndex = 0
var lastPos = Position(0, 0)
def pickleDeltas(index: Int, pos: Position) = {
val addrDelta = index - lastIndex
val startDelta = pos.start - lastPos.start
val endDelta = pos.end - lastPos.end
buf.writeInt(header(addrDelta, startDelta != 0, endDelta != 0))
if (startDelta != 0) buf.writeInt(startDelta)
if (endDelta != 0) buf.writeInt(endDelta)
lastIndex = index
lastPos = pos
}
def traverse(x: Any): Unit = x match {
case x: Tree @unchecked =>
if (x.pos.exists /*&& x.pos.toSynthetic != x.initialPos.toSynthetic*/) {
nextTreeAddr(x) match {
case Some(addr) =>
//println(i"pickling $x at $addr")
pickleDeltas(addr.index, x.pos)
case _ =>
//println(i"no address for $x")
}
}
//else println(i"skipping $x")
x match {
case x: MemberDef @unchecked => traverse(x.symbol.annotations.map(_.tree))
case _ =>
}
traverse(x.productIterator)
case xs: TraversableOnce[_] =>
xs.foreach(traverse)
case _ =>
}
traverse(roots)
}
}
|