Skip to content

Commit c49ad7f

Browse files
committed
split Plugin to ResearchPlugin & StandardPlugin
1 parent 90d707e commit c49ad7f

File tree

6 files changed

+37
-42
lines changed

6 files changed

+37
-42
lines changed

compiler/src/dotty/tools/dotc/plugins/Plugin.scala

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ trait PluginPhase extends MiniPhase {
1616
def runsBefore: Set[Class[_ <: Phase]] = Set.empty
1717
}
1818

19-
trait Plugin {
19+
sealed trait Plugin {
2020
/** The name of this plugin */
2121
def name: String
2222

@@ -28,36 +28,34 @@ trait Plugin {
2828
* Research plugin receives a phase plan and return a new phase plan, while
2929
* non-research plugin returns a list of phases to be inserted.
3030
*/
31-
def research: Boolean = false
31+
def research: Boolean = isInstanceOf[ResearchPlugin]
3232

33+
/** A description of this plugin's options, suitable as a response
34+
* to the -help command-line option. Conventionally, the options
35+
* should be listed with the `-P:plugname:` part included.
36+
*/
37+
val optionsHelp: Option[String] = None
38+
}
3339

40+
trait StandardPlugin extends Plugin {
3441
/** Non-research plugins should override this method to return the phases
3542
*
3643
* @param options: commandline options to the plugin, `-P:plugname:opt1,opt2` becomes List(opt1, opt2)
3744
* @return a list of phases to be added to the phase plan
3845
*/
39-
def init(options: List[String]): List[PluginPhase] = ???
46+
def init(options: List[String]): List[PluginPhase]
47+
}
4048

49+
trait ResearchPlugin extends Plugin {
4150
/** Research plugins should override this method to return the new phase plan
4251
*
4352
* @param options: commandline options to the plugin, `-P:plugname:opt1,opt2` becomes List(opt1, opt2)
4453
* @param plan: the given phase plan
4554
* @return the new phase plan
4655
*/
47-
def init(options: List[String], plan: List[List[Phase]])(implicit ctx: Context): List[List[Phase]] = ???
48-
49-
/** A description of this plugin's options, suitable as a response
50-
* to the -help command-line option. Conventionally, the options
51-
* should be listed with the `-P:plugname:` part included.
52-
*/
53-
val optionsHelp: Option[String] = None
56+
def init(options: List[String], plan: List[List[Phase]])(implicit ctx: Context): List[List[Phase]]
5457
}
5558

56-
/** ...
57-
*
58-
* @author Lex Spoon
59-
* @version 1.0, 2007-5-21
60-
*/
6159
object Plugin {
6260

6361
private val PluginXML = "scalac-plugin.xml"
@@ -72,21 +70,6 @@ object Plugin {
7270
new java.net.URLClassLoader(urls.toArray, compilerLoader)
7371
}
7472

75-
/** Try to load a plugin description from the specified location.
76-
*/
77-
private def loadDescriptionFromJar(jarp: Path): Try[PluginDescription] = {
78-
// XXX Return to this once we have more ARM support
79-
def read(is: InputStream) =
80-
if (is == null) throw new PluginLoadException(jarp.path, s"Missing $PluginXML in $jarp")
81-
else PluginDescription.fromXML(is)
82-
83-
val xmlEntry = new java.util.jar.JarEntry(PluginXML)
84-
Try(read(new Jar(jarp.jpath.toFile).getEntryStream(xmlEntry)))
85-
}
86-
87-
private def loadDescriptionFromFile(f: Path): Try[PluginDescription] =
88-
Try(PluginDescription.fromXML(new java.io.FileInputStream(f.jpath.toFile)))
89-
9073
type AnyClass = Class[_]
9174

9275
/** Use a class loader to load the plugin class.
@@ -115,6 +98,20 @@ object Plugin {
11598
dirs: List[Path],
11699
ignoring: List[String]): List[Try[AnyClass]] =
117100
{
101+
102+
def loadDescriptionFromDir(f: Path): Try[PluginDescription] =
103+
Try(PluginDescription.fromXML(new java.io.FileInputStream((f / PluginXML).jpath.toFile)))
104+
105+
def loadDescriptionFromJar(jarp: Path): Try[PluginDescription] = {
106+
// XXX Return to this once we have more ARM support
107+
def read(is: InputStream) =
108+
if (is == null) throw new PluginLoadException(jarp.path, s"Missing $PluginXML in $jarp")
109+
else PluginDescription.fromXML(is)
110+
111+
val xmlEntry = new java.util.jar.JarEntry(PluginXML)
112+
Try(read(new Jar(jarp.jpath.toFile).getEntryStream(xmlEntry)))
113+
}
114+
118115
// List[(jar, Try(descriptor))] in dir
119116
def scan(d: Directory) =
120117
d.files.toList sortBy (_.name) filter (Jar isJarOrZip _) map (j => (j, loadDescriptionFromJar(j)))
@@ -134,7 +131,7 @@ object Plugin {
134131
def loop(qs: List[Path]): Try[PluginDescription] = qs match {
135132
case Nil => Failure(new MissingPluginException(ps))
136133
case p :: rest =>
137-
if (p.isDirectory) loadDescriptionFromFile(p.toDirectory / PluginXML) orElse loop(rest)
134+
if (p.isDirectory) loadDescriptionFromDir(p.toDirectory) orElse loop(rest)
138135
else if (p.isFile) loadDescriptionFromJar(p.toFile) orElse loop(rest)
139136
else loop(rest)
140137
}

compiler/src/dotty/tools/dotc/plugins/Plugins.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ trait Plugins {
128128
}
129129

130130
// schedule plugins according to ordering constraints
131-
val pluginPhases = plugins.filter(!_.research).flatMap(plug => plug.init(options(plug)))
131+
val pluginPhases = plugins.collect { case p: StandardPlugin => p }.flatMap { plug => plug.init(options(plug)) }
132132
val updatedPlan = Plugins.schedule(plan, pluginPhases)
133133

134134
// add research plugins
135-
plugins.filter(_.research).foldRight(updatedPlan) { (plug, plan) => plug.init(options(plug), plan) }
135+
plugins.collect { case p: ResearchPlugin => p }.foldRight(updatedPlan) { (plug, plan) => plug.init(options(plug), plan) }
136136
}
137137
}
138138

sbt-dotty/sbt-test/sbt-dotty/compiler-plugin/plugin/DivideZero.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dividezero
33
import dotty.tools.dotc._
44
import core._
55
import Contexts.Context
6-
import plugins.{Plugin, PluginPhase}
6+
import plugins._
77
import Phases.Phase
88
import ast.tpd
99
import transform.MegaPhase.MiniPhase
@@ -12,7 +12,7 @@ import Symbols.Symbol
1212
import Constants.Constant
1313
import transform.{LinkAll, Pickler}
1414

15-
class DivideZero extends PluginPhase with Plugin {
15+
class DivideZero extends PluginPhase with StandardPlugin {
1616
val name: String = "divideZero"
1717
override val description: String = "divide zero check"
1818

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=0.13.15
1+
sbt.version=0.13.17

tests/plugins/neg/divideZero-research/plugin_1.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import dotty.tools.dotc._
22
import core._
33
import Contexts.Context
4-
import plugins.Plugin
4+
import plugins._
55
import Phases.Phase
66
import ast.tpd
77
import transform.MegaPhase.MiniPhase
@@ -10,12 +10,10 @@ import Symbols.Symbol
1010
import Constants.Constant
1111
import StdNames._
1212

13-
class DivideZero extends MiniPhase with Plugin {
13+
class DivideZero extends MiniPhase with ResearchPlugin {
1414
val name: String = "divideZero"
1515
override val description: String = "divide zero check"
1616

17-
override val research = true
18-
1917
val phaseName = name
2018

2119
override def init(options: List[String], phases: List[List[Phase]])(implicit ctx: Context): List[List[Phase]] = {

tests/plugins/neg/divideZero/plugin_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import dotty.tools.dotc._
22
import core._
33
import Contexts.Context
4-
import plugins.{Plugin, PluginPhase}
4+
import plugins._
55
import Phases.Phase
66
import ast.tpd
77
import transform.MegaPhase.MiniPhase
@@ -11,7 +11,7 @@ import Constants.Constant
1111
import transform.{LinkAll, Pickler}
1212
import StdNames._
1313

14-
class DivideZero extends PluginPhase with Plugin {
14+
class DivideZero extends PluginPhase with StandardPlugin {
1515
val name: String = "divideZero"
1616
override val description: String = "divide zero check"
1717

0 commit comments

Comments
 (0)