summaryrefslogtreecommitdiff
path: root/src/continuations/plugin/scala/tools/selectivecps/SelectiveCPSPlugin.scala
blob: eb18f0374864c2f1d38cb9bc343a0174034ad504 (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
// $Id$

package scala.tools.selectivecps

import scala.tools.nsc
import scala.tools.nsc.typechecker._
import nsc.Global
import nsc.Phase
import nsc.plugins.Plugin
import nsc.plugins.PluginComponent

class SelectiveCPSPlugin(val global: Global) extends Plugin {
  import global._

  val name = "continuations"
  val description = "applies selective cps conversion"

  val anfPhase = new SelectiveANFTransform() {
    val global = SelectiveCPSPlugin.this.global
    val runsAfter = List("pickler")
  }

  val cpsPhase = new SelectiveCPSTransform() {
    val global = SelectiveCPSPlugin.this.global
    val runsAfter = List("selectiveanf")
    override val runsBefore = List("uncurry")
  }

  val components = List[PluginComponent](anfPhase, cpsPhase)

  val checker = new CPSAnnotationChecker {
    val global: SelectiveCPSPlugin.this.global.type = SelectiveCPSPlugin.this.global
  }
  global.addAnnotationChecker(checker.checker)

  global.log("instantiated cps plugin: " + this)

  def setEnabled(flag: Boolean) = {
    checker.cpsEnabled = flag
    anfPhase.cpsEnabled = flag
    cpsPhase.cpsEnabled = flag
  }

  // TODO: require -enabled command-line flag
  override def processOptions(options: List[String], error: String => Unit) = {
    var enabled = true
    options foreach {
      case "enable"    => enabled = true
      case "disable"   => enabled = false
      case option      => error("Option not understood: "+option)
    }
    setEnabled(enabled)
  }

  override val optionsHelp: Option[String] = {
    Some("  -P:continuations:disable        Disable continuations plugin")
  }
}