From c1e263bebf7e73239e7faa3e48d75a0a7df45d76 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 4 Mar 2016 12:07:59 +0100 Subject: Avoid setupMethod in Driver Driver should not know that patch functionality exists. Instead, introduce settings that can introduce their own stateful values. --- src/dotty/tools/dotc/Driver.scala | 1 - src/dotty/tools/dotc/config/ScalaSettings.scala | 6 ++++-- src/dotty/tools/dotc/config/Settings.scala | 9 ++++++++- src/dotty/tools/dotc/rewrite/Patches.scala | 22 ++++++---------------- 4 files changed, 18 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/dotty/tools/dotc/Driver.scala b/src/dotty/tools/dotc/Driver.scala index fd82934ec..a989ad218 100644 --- a/src/dotty/tools/dotc/Driver.scala +++ b/src/dotty/tools/dotc/Driver.scala @@ -44,7 +44,6 @@ abstract class Driver extends DotClass { val ctx = rootCtx.fresh val summary = CompilerCommand.distill(args)(ctx) ctx.setSettings(summary.sstate) - Patches.setup(ctx) val fileNames = CompilerCommand.checkUsage(summary, sourcesRequired)(ctx) (fileNames, ctx) } diff --git a/src/dotty/tools/dotc/config/ScalaSettings.scala b/src/dotty/tools/dotc/config/ScalaSettings.scala index 17da1f8d1..cdbf6ec50 100644 --- a/src/dotty/tools/dotc/config/ScalaSettings.scala +++ b/src/dotty/tools/dotc/config/ScalaSettings.scala @@ -1,6 +1,8 @@ -package dotty.tools.dotc.config +package dotty.tools.dotc +package config import PathResolver.Defaults +import rewrite.Patches class ScalaSettings extends Settings.SettingGroup { @@ -48,7 +50,7 @@ class ScalaSettings extends Settings.SettingGroup { val d = StringSetting("-d", "directory|jar", "destination for generated classfiles.", ".") val nospecialization = BooleanSetting("-no-specialization", "Ignore @specialize annotations.") val language = MultiStringSetting("-language", "feature", "Enable one or more language features.") - val rewrite = BooleanSetting("-rewrite", "When used in conjunction with -language:Scala2 rewrites sources to migrate to new syntax") + val rewrite = OptionSetting[Patches.PatchedFiles]("-rewrite", "When used in conjunction with -language:Scala2 rewrites sources to migrate to new syntax") /** -X "Advanced" settings */ diff --git a/src/dotty/tools/dotc/config/Settings.scala b/src/dotty/tools/dotc/config/Settings.scala index 73bb056aa..eddeb83ab 100644 --- a/src/dotty/tools/dotc/config/Settings.scala +++ b/src/dotty/tools/dotc/config/Settings.scala @@ -19,6 +19,7 @@ object Settings { val StringTag = ClassTag(classOf[String]) val ListTag = ClassTag(classOf[List[_]]) val VersionTag = ClassTag(classOf[ScalaVersion]) + val OptionTag = ClassTag(classOf[Option[_]]) class SettingsState(initialValues: Seq[Any]) { private var values = ArrayBuffer(initialValues: _*) @@ -55,7 +56,8 @@ object Settings { choices: Seq[T] = Nil, prefix: String = "", aliases: List[String] = Nil, - depends: List[(Setting[_], Any)] = Nil)(private[Settings] val idx: Int) { + depends: List[(Setting[_], Any)] = Nil, + propertyClass: Option[Class[_]] = None)(private[Settings] val idx: Int) { def withAbbreviation(abbrv: String): Setting[T] = copy(aliases = aliases :+ abbrv)(idx) @@ -112,6 +114,8 @@ object Settings { def doSet(argRest: String) = ((implicitly[ClassTag[T]], args): @unchecked) match { case (BooleanTag, _) => update(true, args) + case (OptionTag, _) => + update(Some(propertyClass.get.newInstance), args) case (ListTag, _) => if (argRest.isEmpty) missingArg else update((argRest split ",").toList, args) @@ -255,5 +259,8 @@ object Settings { def VersionSetting(name: String, descr: String, default: ScalaVersion = NoScalaVersion): Setting[ScalaVersion] = publish(Setting(name, descr, default)) + + def OptionSetting[T: ClassTag](name: String, descr: String): Setting[Option[T]] = + publish(Setting(name, descr, None, propertyClass = Some(implicitly[ClassTag[T]].runtimeClass))) } } diff --git a/src/dotty/tools/dotc/rewrite/Patches.scala b/src/dotty/tools/dotc/rewrite/Patches.scala index 85532dad9..7513ec7fa 100644 --- a/src/dotty/tools/dotc/rewrite/Patches.scala +++ b/src/dotty/tools/dotc/rewrite/Patches.scala @@ -8,26 +8,17 @@ import collection.mutable object Patches { - private val PropertyTag = "rewrite-patches" - private case class Patch(pos: Position, replacement: String) { def delta = replacement.length - (pos.end - pos.start) } - private class PatchedFiles extends mutable.HashMap[SourceFile, Patches] - - /** If `-rewrite` is set, enable patches in `ctx`. This means - * setting up a property in `ctx` that allows to record patches. - */ - def setup(ctx: FreshContext): Unit = - if (ctx.settings.rewrite.value(ctx)) - ctx.setProperty(PropertyTag, new PatchedFiles) + class PatchedFiles extends mutable.HashMap[SourceFile, Patches] - /** If patches are enabled in `ctx`, record a patch that replaces the range + /** If -rewrite is set, record a patch that replaces the range * given by `pos` in `source` by `replacement` */ def patch(source: SourceFile, pos: Position, replacement: String)(implicit ctx: Context): Unit = - ctx.moreProperties.get(PropertyTag) match { + ctx.settings.rewrite.value match { case Some(pfs: PatchedFiles) => pfs.get(source) match { case Some(ps) => @@ -39,11 +30,10 @@ object Patches { case _ => } - /** If patches are enabled in `ctx`, apply all patches - * and overwrite patched source files + /** If -rewrite is set, apply all patches and overwrite patched source files. */ def writeBack()(implicit ctx: Context) = - ctx.moreProperties.get(PropertyTag) match { + ctx.settings.rewrite.value match { case Some(pfs: PatchedFiles) => for (source <- pfs.keys) { ctx.println(s"[patched file ${source.file.path}]") @@ -53,7 +43,7 @@ object Patches { } } -private class Patches(source: SourceFile) { +class Patches(source: SourceFile) { import Patches._ private val pbuf = new mutable.ListBuffer[Patch]() -- cgit v1.2.3