aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/core/Transformers.scala
blob: 9b8bb65a49c5d2835ecfa843224ccb8f0816245d (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
package dotty.tools.dotc
package core

import Periods._
import SymDenotations._
import Contexts._
import Types._
import Denotations._
import java.lang.AssertionError
import dotty.tools.dotc.util.DotClass

object Transformers {

  trait TransformerBase { self: ContextBase =>
    val infoTransformers = new TransformerGroup
  }

  /** A transformer group contains a sequence of transformers,
   *  ordered by the phase where they apply. Transformers are added
   *  to a group via `install`.
   *
   *  There are two transformerGroups in a context base:
   *  symTransformers and refTransformers. symTransformers translate
   *  full symbol denotations, refTransformers translate only symbol references
   *  of type Unique/JointRefDenotation.
   */
  class TransformerGroup {

    private val nxTransformer =
      Array.fill[Transformer](MaxPossiblePhaseId + 1)(NoTransformer)

    def nextTransformer(pid: PhaseId) = nxTransformer(pid)

    def install(pid: PhaseId, transFn: TransformerGroup => Transformer): Unit =
      if ((pid > NoPhaseId) && (nxTransformer(pid).phaseId > pid)) {
        val trans = transFn(this)
        trans._phaseId = pid
        nxTransformer(pid) = transFn(this)
        install(pid - 1, transFn)
      }

    /** A sentinel transformer object */
    object NoTransformer extends Transformer(this) {
      _phaseId = MaxPossiblePhaseId + 1
      override def lastPhaseId = phaseId - 1 // TODO JZ Probably off-by-N error here. MO: Don't think so: we want empty validity period.
      def transform(ref: SingleDenotation)(implicit ctx: Context): SingleDenotation =
        unsupported("transform")
    }
  }

  /** A transformer transforms denotations at a given phase */
  abstract class Transformer(group: TransformerGroup) extends DotClass {

    private[Transformers] var _phaseId: PhaseId = _

    /** The phase at the start of which the denotations are transformed */
    def phaseId: PhaseId = _phaseId

    /** The last phase during which the transformed denotations are valid */
    def lastPhaseId = group.nextTransformer(phaseId).phaseId - 1

    /** The validity period of the transformer in the given context */
    def validFor(implicit ctx: Context): Period =
      Period(ctx.runId, phaseId, lastPhaseId)

    /** The transformation method */
    def transform(ref: SingleDenotation)(implicit ctx: Context): SingleDenotation
  }
}