Splitting scalac plugin into multiple files
Asked Answered
B

3

6

I'd like to split my scalac plugin into multiple files. This sounds easy but I haven't managed to pull it off due to path-dependent type issues stemming from the import global._ line.

Here's Lex Spoon's sample plugin:

package localhost

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

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

  val name = "divbyzero"
  val description = "checks for division by zero"
  val components = List[PluginComponent](Component)

  private object Component extends PluginComponent {
    val global: DivByZero.this.global.type = DivByZero.this.global
    val runsAfter = "refchecks"
    // Using the Scala Compiler 2.8.x the runsAfter should be written as below
    // val runsAfter = List[String]("refchecks");
    val phaseName = DivByZero.this.name
    def newPhase(_prev: Phase) = new DivByZeroPhase(_prev)    

    class DivByZeroPhase(prev: Phase) extends StdPhase(prev) {
      override def name = DivByZero.this.name
      def apply(unit: CompilationUnit) {
        for ( tree @ Apply(Select(rcvr, nme.DIV), List(Literal(Constant(0)))) <- unit.body;
             if rcvr.tpe <:< definitions.IntClass.tpe) 
          {
            unit.error(tree.pos, "definitely division by zero")
          }
      }
    }
  }
}

How can I put Component and DivByZeroPhase in their own files without having the import global._ in scope?

Bushwhack answered 12/4, 2011 at 0:40 Comment(0)
D
4

Here's a really old project where I've done the same thing:

https://github.com/jsuereth/osgi-scalac-plugin/blob/master/src/main/scala/scala/osgi/compiler/OsgiPlugin.scala

If you don't need to pass path-dependent types from the global, don't worry about trying to keep the "this.global" portions of it relevant.

Drogheda answered 12/4, 2011 at 1:0 Comment(0)
H
3

In the Scala Refactoring library, I solved it by having a trait CompilerAccess:

trait CompilerAccess { 
  val global: tools.nsc.Global
}

Now all the other traits that need to access global just declare CompilerAccess as a dependency:

trait TreeTraverser {
  this: CompilerAccess =>
  import global._

  ...
}

and then there's a class that mixes in all these traits and provides an instance of global:

trait SomeRefactoring extends TreeTraverser with OtherTrait with MoreTraits {
  val global = //wherever you get your global from
}

This scheme worked quite well for me.

Halland answered 12/4, 2011 at 8:42 Comment(0)
F
2

You can create a separate class for your component and pass global in:

class TemplateComponent(val global: Global) extends PluginComponent {

  import global._

  val runsAfter = List[String]("refchecks")

  val phaseName = "plugintemplate"

  def newPhase(prev: Phase) = new StdPhase(prev) {

    override def name = phaseName

    def apply(unit:CompilationUnit) = {
    }    
  }
}
Frederiksberg answered 12/4, 2011 at 1:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.