summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/interactive/Positions.scala
blob: f30fc83a2b8f4379f7551f21892c9589b832de7c (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package scala.tools.nsc.interactive

import ast.Trees
import scala.tools.nsc.util.{SourceFile, Position, RangePosition, OffsetPosition, NoPosition, WorkScheduler}
import scala.collection.mutable.ListBuffer

/** Handling range positions
 *  atPos, the main method in this trait, will add positions to a tree,
 *  and will ensure the following properties:
 *
 *    1. All nodes between the root of the tree and nodes that already have positions
 *       will be assigned positions.
 *    2. No node which already has a position will be assigned a different range; however
 *       a RangePosition might become a TransparentPosition.
 *    3. The position of each assigned node includes the positions of each of its children.
 *    4. The positions of all solid descendants of children of an assigned node
 *       are mutually non-overlapping.
 *
 * Here, the solid descendant of a node are:
 *
 *   If the node has a TransparentPosition, the solid descendants of all its children
 *   Otherwise, then singleton consisting of the node itself.
 */
trait Positions extends Trees {
self: Global =>

  case class Range(val pos: Position, val tree: Tree) {
    def isFree = tree == EmptyTree
  }

  class TransparentPosition(source0: SourceFile, start: Int, point: Int, end: Int) extends RangePosition(source0, start, point, end) {
    override def toString = "<"+super.toString+">"
  }

  def isTransparent(pos: Position) = pos.isInstanceOf[TransparentPosition]
  def isRange(pos: Position) = pos.isInstanceOf[RangePosition]

  // -------------- ensuring no overlaps -------------------------------

  def solidDescendants(tree: Tree): List[Tree] =
    if (isTransparent(tree.pos)) tree.children flatMap solidDescendants else List(tree)

  /** A free range from `lo` to `hi` */
  private def free(lo: Int, hi: Int): Range =
    Range(new RangePosition(null, lo, lo, hi), EmptyTree)

  /** The maximal free range */
  private val maxFree: Range = free(0, Math.MAX_INT)

  /** A singleton list of a non-empty range from `lo` to `hi`, or else the empty List */
  private def maybeFree(lo: Int, hi: Int) = if (lo < hi) List(free(lo, hi)) else List()

  /** Insert `pos` into ranges `rs` if possible;
   *  otherwise add conflicting trees to `conflicting`.
   */
  private def insert(rs: List[Range], t: Tree, conflicting: ListBuffer[Tree]): List[Range] = rs match {
    case List() =>
      assert(conflicting.nonEmpty)
      rs
    case r :: rs1 =>
      assert(!isTransparent(t.pos))
      if (r.isFree && (r.pos includes t.pos)) {
        maybeFree(t.pos.end, r.pos.end) ::: List(Range(t.pos, t)) ::: maybeFree(r.pos.start, t.pos.start) ::: rs1
      } else {
        if (!r.isFree && (r.pos overlaps t.pos)) conflicting += r.tree
        r :: insert(rs1, t, conflicting)
      }
  }

  /** Replace elem `t` of `ts` by `replacement` list. */
  private def replace(ts: List[Tree], t: Tree, replacement: List[Tree]): List[Tree] =
    if (ts.head == t) replacement ::: ts.tail
    else ts.head :: replace(ts.tail, t, replacement)

  /** Ensure that given list of trees has mutually non-overlapping positions,
   *  by assinging TransparentPositions to some of them, if necessary
   */
  def ensureNonOverlapping(cts: List[Tree]): Unit = {

    /** Do a pass over all child trees `cts`, where `ranges` reflects positions previously
     *  encountered. If there are overlaps, break up one node by making its position a TransparentPosition
     *  and do another pass of `ensureOverlapping`.
     *  @param ranges   The current list of non-overlapping ranges,
     *                  both occupied and free, sorted from later to earlier.
     *                  No TransparentPositions allowed here!
     *  @param trees    The list of trees to insert in ranges.
     */
    def iterate(ranges: List[Range], trees: List[Tree]): Unit = trees match {
      case List() =>
        ;
      case ct :: trees1 =>
        if (isTransparent(ct.pos))
          iterate(ranges, solidDescendants(ct) ::: trees1)
        else if (ct.pos.isDefined) {
          val conflicting = new ListBuffer[Tree]
          val ranges1 = insert(ranges, ct, conflicting)
          if (conflicting.isEmpty) {
            iterate(ranges1, trees1)
          } else {
            val splitNode =
              if (conflicting.size == 1 && (conflicting.head.pos includes ct.pos)) conflicting.head
              else ct
            splitNode setPos new TransparentPosition(splitNode.pos.source.get, splitNode.pos.start, splitNode.pos.point, splitNode.pos.end)
            ensureNonOverlapping(replace(cts, splitNode, solidDescendants(splitNode)))
          }
        }
    }

    iterate(List(maxFree), cts)
  }

  /** Does given list of trees have mutually non-overlapping positions? */
  def findOverlapping(cts: List[Tree]): List[(Tree, Tree)] = {
    var ranges = List(maxFree)
    for (ct <- cts) {
      val conflicting = new ListBuffer[Tree]
      ranges = insert(ranges, ct, conflicting)
      if (conflicting.nonEmpty) return conflicting.toList map (t => (t, ct))
    }
    List()
  }

  // -------------- setting positions -------------------------------

  /** The sorted list of descendant nodes, a long positions */


  /** Set position of all children of a node
   *  @param  pos   A target position.
   *                Uses the point of the position as the point of all positions it assigns.
   *                Uses the start of this position as an Offset position for unpositioed trees
   *                without children.
   *  @param  trees  The children to position
   */
  private def setChildrenPos(pos: Position, trees: List[Tree]) {
    var currentPos = pos
    for (tree <- trees) {
      if (tree != EmptyTree && tree.pos == NoPosition) {
        val children = tree.children
        if (children.isEmpty)
          tree setPos OffsetPosition(pos.source.get, currentPos.start)
        else {
          setChildrenPos(currentPos, children)
          tree setPos new RangePosition(
            pos.source.get, (children map (_.pos.start)).min, pos.point, (children map (_.pos.end)).max)
        }
        currentPos = new RangePosition(pos.source.get, tree.pos.end, pos.point, pos.end)
      }
//      println("set children pos "+pos+" of "+trees)
    }
    ensureNonOverlapping(trees)
  }

  /** Position a tree.
   *  This means: Set position of a node and position all its unpositioned children.
   */
  override def atPos[T <: Tree](pos: Position)(tree: T): T =
    if (isRange(pos)) {
      if (tree != EmptyTree && tree.pos == NoPosition) {
        tree.setPos(pos)
        val children = tree.children
        if (children.nonEmpty) {
          if (children.tail.isEmpty) atPos(pos)(children.head)
          else setChildrenPos(pos, children)
        }
      }
      tree
    } else {
      super.atPos(pos)(tree)
    }

  // ---------------- Validating positions ----------------------------------

  def validatePositions(tree: Tree) {
    def error(msg: String) {
      inform("**** bad positions:")
      inform(msg)
      inform("================= in =================")
      inform(tree.toString)
    }
    def validate(tree: Tree, encltree: Tree) {
      if (encltree.pos.isSynthetic && !tree.pos.isSynthetic)
        error("synthetic "+encltree+" contains nonsynthetic" + tree)
      if (!(encltree.pos includes tree.pos))
        error(encltree+" does not include "+tree)
      for ((t1, t2) <- findOverlapping(tree.children flatMap solidDescendants))
        error("overlapping trees: "+t1+" === and === "+t2)
    }
    validate(tree, tree)
  }

  // ---------------- Locating trees ----------------------------------

  /** A locator for trees with given positions.
   *  Given a position `pos`, locator.apply returns
   *  the smallest tree that encloses `pos`.
   */
  class Locator(pos: Position) extends Traverser {
    var last: Tree = _
    def locateIn(root: Tree): Tree = {
      this.last = EmptyTree
      traverse(root)
      this.last
    }
    override def traverse(t: Tree) {
      if (!t.pos.isSynthetic && (t.pos includes pos)) {
        if (!isTransparent(t.pos)) last = t
        super.traverse(t)
      }
    }
  }
}