summaryrefslogtreecommitdiff
path: root/plugin/src
diff options
context:
space:
mode:
authorJack Koenig2022-01-10 16:32:51 -0800
committerGitHub2022-01-10 16:32:51 -0800
commit2b48fd15a7711dcd44334fbbc538667a102a581a (patch)
tree4b4766347c3943d65c13e5de2d139b14821eec61 /plugin/src
parent92e77a97af986629766ac9038f0ebc8ab9a48fa1 (diff)
parentbff8dc0738adafa1176f6959a33ad86f6373c558 (diff)
Merge pull request #2246 from chipsalliance/scalafmt
Add scalafmt configuration and apply it.
Diffstat (limited to 'plugin/src')
-rw-r--r--plugin/src/main/scala/chisel3/internal/plugin/BundleComponent.scala9
-rw-r--r--plugin/src/main/scala/chisel3/internal/plugin/ChiselComponent.scala63
-rw-r--r--plugin/src/main/scala/chisel3/internal/plugin/ChiselPlugin.scala8
3 files changed, 42 insertions, 38 deletions
diff --git a/plugin/src/main/scala/chisel3/internal/plugin/BundleComponent.scala b/plugin/src/main/scala/chisel3/internal/plugin/BundleComponent.scala
index e56380a1..2d3a2cae 100644
--- a/plugin/src/main/scala/chisel3/internal/plugin/BundleComponent.scala
+++ b/plugin/src/main/scala/chisel3/internal/plugin/BundleComponent.scala
@@ -70,11 +70,9 @@ private[plugin] class BundleComponent(val global: Global, arguments: ChiselPlugi
(primaryConstructor, paramAccessors.toList)
}
-
override def transform(tree: Tree): Tree = tree match {
case bundle: ClassDef if isBundle(bundle.symbol) && !bundle.mods.hasFlag(Flag.ABSTRACT) =>
-
// ==================== Generate _cloneTypeImpl ====================
val (con, params) = getConstructorAndParams(bundle.impl.body)
if (con.isEmpty) {
@@ -99,13 +97,14 @@ private[plugin] class BundleComponent(val global: Global, arguments: ChiselPlugi
if (isData(vp.symbol)) cloneTypeFull(select) else select
})
- val tparamList = bundle.tparams.map{ t => Ident(t.symbol) }
- val ttpe = if(tparamList.nonEmpty) AppliedTypeTree(Ident(bundle.symbol), tparamList) else Ident(bundle.symbol)
+ val tparamList = bundle.tparams.map { t => Ident(t.symbol) }
+ val ttpe = if (tparamList.nonEmpty) AppliedTypeTree(Ident(bundle.symbol), tparamList) else Ident(bundle.symbol)
val newUntyped = New(ttpe, conArgs)
val neww = localTyper.typed(newUntyped)
// Create the symbol for the method and have it be associated with the Bundle class
- val cloneTypeSym = bundle.symbol.newMethod(TermName("_cloneTypeImpl"), bundle.symbol.pos.focus, Flag.OVERRIDE | Flag.PROTECTED)
+ val cloneTypeSym =
+ bundle.symbol.newMethod(TermName("_cloneTypeImpl"), bundle.symbol.pos.focus, Flag.OVERRIDE | Flag.PROTECTED)
// Handwritten cloneTypes don't have the Method flag set, unclear if it matters
cloneTypeSym.resetFlag(Flags.METHOD)
// Need to set the type to chisel3.Bundle for the override to work
diff --git a/plugin/src/main/scala/chisel3/internal/plugin/ChiselComponent.scala b/plugin/src/main/scala/chisel3/internal/plugin/ChiselComponent.scala
index cee11df5..eced652b 100644
--- a/plugin/src/main/scala/chisel3/internal/plugin/ChiselComponent.scala
+++ b/plugin/src/main/scala/chisel3/internal/plugin/ChiselComponent.scala
@@ -11,7 +11,9 @@ import scala.tools.nsc.transform.TypingTransformers
// The component of the chisel plugin. Not sure exactly what the difference is between
// a Plugin and a PluginComponent.
-class ChiselComponent(val global: Global, arguments: ChiselPluginArguments) extends PluginComponent with TypingTransformers {
+class ChiselComponent(val global: Global, arguments: ChiselPluginArguments)
+ extends PluginComponent
+ with TypingTransformers {
import global._
val runsAfter: List[String] = List[String]("typer")
val phaseName: String = "chiselcomponent"
@@ -25,8 +27,7 @@ class ChiselComponent(val global: Global, arguments: ChiselPluginArguments) exte
}
}
- class MyTypingTransformer(unit: CompilationUnit)
- extends TypingTransformer(unit) {
+ class MyTypingTransformer(unit: CompilationUnit) extends TypingTransformer(unit) {
private def shouldMatchGen(bases: Tree*): Type => Boolean = {
val cache = mutable.HashMap.empty[Type, Boolean]
@@ -51,7 +52,7 @@ class ChiselComponent(val global: Global, arguments: ChiselPluginArguments) exte
recShouldMatch(s.typeArgs.head, seen + s)
} else {
// This is the standard inheritance hierarchy, Scalac catches loops here
- s.parents.exists( p => recShouldMatch(p, seen) )
+ s.parents.exists(p => recShouldMatch(p, seen))
}
}
@@ -62,24 +63,25 @@ class ChiselComponent(val global: Global, arguments: ChiselPluginArguments) exte
// Return function so that it captures the cache
{ q: Type =>
- cache.getOrElseUpdate(q, {
- // First check if a match, then check early exit, then recurse
- if(terminate(q)){
- true
- } else if(earlyExit(q)) {
- false
- } else {
- recShouldMatch(q, Set.empty)
+ cache.getOrElseUpdate(
+ q, {
+ // First check if a match, then check early exit, then recurse
+ if (terminate(q)) {
+ true
+ } else if (earlyExit(q)) {
+ false
+ } else {
+ recShouldMatch(q, Set.empty)
+ }
}
- })
+ )
}
}
-
- private val shouldMatchData : Type => Boolean = shouldMatchGen(tq"chisel3.Data")
- private val shouldMatchDataOrMem : Type => Boolean = shouldMatchGen(tq"chisel3.Data", tq"chisel3.MemBase[_]")
- private val shouldMatchModule : Type => Boolean = shouldMatchGen(tq"chisel3.experimental.BaseModule")
- private val shouldMatchInstance : Type => Boolean = shouldMatchGen(tq"chisel3.experimental.hierarchy.Instance[_]")
+ private val shouldMatchData: Type => Boolean = shouldMatchGen(tq"chisel3.Data")
+ private val shouldMatchDataOrMem: Type => Boolean = shouldMatchGen(tq"chisel3.Data", tq"chisel3.MemBase[_]")
+ private val shouldMatchModule: Type => Boolean = shouldMatchGen(tq"chisel3.experimental.BaseModule")
+ private val shouldMatchInstance: Type => Boolean = shouldMatchGen(tq"chisel3.experimental.hierarchy.Instance[_]")
// Given a type tree, infer the type and return it
private def inferType(t: Tree): Type = localTyper.typed(t, nsc.Mode.TYPEmode).tpe
@@ -97,13 +99,13 @@ class ChiselComponent(val global: Global, arguments: ChiselPluginArguments) exte
Flags.CASEACCESSOR,
Flags.PARAMACCESSOR
)
- badFlags.forall{ x => !mods.hasFlag(x)}
+ badFlags.forall { x => !mods.hasFlag(x) }
}
// Ensure expression isn't null, as you can't call `null.autoName("myname")`
val isNull = dd.rhs match {
case Literal(Constant(null)) => true
- case _ => false
+ case _ => false
}
okFlags(dd.mods) && !isNull && dd.rhs != EmptyTree
@@ -130,7 +132,7 @@ class ChiselComponent(val global: Global, arguments: ChiselPluginArguments) exte
// Ensure expression isn't null, as you can't call `null.autoName("myname")`
val isNull = dd.rhs match {
case Literal(Constant(null)) => true
- case _ => false
+ case _ => false
}
val tpe = inferType(dd.tpt)
definitions.isTupleType(tpe) && okFlags(dd.mods) && !isNull && dd.rhs != EmptyTree
@@ -139,7 +141,7 @@ class ChiselComponent(val global: Global, arguments: ChiselPluginArguments) exte
private def findUnapplyNames(tree: Tree): Option[List[String]] = {
val applyArgs: Option[List[Tree]] = tree match {
case Match(_, List(CaseDef(_, _, Apply(_, args)))) => Some(args)
- case _ => None
+ case _ => None
}
applyArgs.flatMap { args =>
var ok = true
@@ -147,7 +149,7 @@ class ChiselComponent(val global: Global, arguments: ChiselPluginArguments) exte
args.foreach {
case Ident(TermName(name)) => result += name
// Anything unexpected and we abort
- case _ => ok = false
+ case _ => ok = false
}
if (ok) Some(result.toList) else None
}
@@ -169,9 +171,9 @@ class ChiselComponent(val global: Global, arguments: ChiselPluginArguments) exte
// If a Data and in a Bundle, just get the name but not a prefix
if (shouldMatchData(tpe) && inBundle(dd)) {
val str = stringFromTermName(name)
- val newRHS = transform(rhs) // chisel3.internal.plugin.autoNameRecursively
+ val newRHS = transform(rhs) // chisel3.internal.plugin.autoNameRecursively
val named = q"chisel3.internal.plugin.autoNameRecursively($str)($newRHS)"
- treeCopy.ValDef(dd, mods, name, tpt, localTyper typed named)
+ treeCopy.ValDef(dd, mods, name, tpt, localTyper.typed(named))
}
// If a Data or a Memory, get the name and a prefix
else if (shouldMatchDataOrMem(tpe)) {
@@ -179,18 +181,18 @@ class ChiselComponent(val global: Global, arguments: ChiselPluginArguments) exte
val newRHS = transform(rhs)
val prefixed = q"chisel3.experimental.prefix.apply[$tpt](name=$str)(f=$newRHS)"
val named = q"chisel3.internal.plugin.autoNameRecursively($str)($prefixed)"
- treeCopy.ValDef(dd, mods, name, tpt, localTyper typed named)
+ treeCopy.ValDef(dd, mods, name, tpt, localTyper.typed(named))
// If an instance, just get a name but no prefix
} else if (shouldMatchModule(tpe)) {
val str = stringFromTermName(name)
val newRHS = transform(rhs)
val named = q"chisel3.internal.plugin.autoNameRecursively($str)($newRHS)"
- treeCopy.ValDef(dd, mods, name, tpt, localTyper typed named)
+ treeCopy.ValDef(dd, mods, name, tpt, localTyper.typed(named))
} else if (shouldMatchInstance(tpe)) {
val str = stringFromTermName(name)
val newRHS = transform(rhs)
val named = q"chisel3.internal.plugin.autoNameRecursively($str)($newRHS)"
- treeCopy.ValDef(dd, mods, name, tpt, localTyper typed named)
+ treeCopy.ValDef(dd, mods, name, tpt, localTyper.typed(named))
} else {
// Otherwise, continue
super.transform(tree)
@@ -202,9 +204,10 @@ class ChiselComponent(val global: Global, arguments: ChiselPluginArguments) exte
if (fieldsOfInterest.reduce(_ || _)) {
findUnapplyNames(rhs) match {
case Some(names) =>
- val onames: List[Option[String]] = fieldsOfInterest.zip(names).map { case (ok, name) => if (ok) Some(name) else None }
+ val onames: List[Option[String]] =
+ fieldsOfInterest.zip(names).map { case (ok, name) => if (ok) Some(name) else None }
val named = q"chisel3.internal.plugin.autoNameRecursivelyProduct($onames)($rhs)"
- treeCopy.ValDef(dd, mods, name, tpt, localTyper typed named)
+ treeCopy.ValDef(dd, mods, name, tpt, localTyper.typed(named))
case None => // It's not clear how this could happen but we don't want to crash
super.transform(tree)
}
diff --git a/plugin/src/main/scala/chisel3/internal/plugin/ChiselPlugin.scala b/plugin/src/main/scala/chisel3/internal/plugin/ChiselPlugin.scala
index eadb76b6..bd02d50c 100644
--- a/plugin/src/main/scala/chisel3/internal/plugin/ChiselPlugin.scala
+++ b/plugin/src/main/scala/chisel3/internal/plugin/ChiselPlugin.scala
@@ -21,7 +21,11 @@ object ChiselPlugin {
val name = "chiselplugin"
// Also logs why the compoennt was not run
- private[plugin] def runComponent(global: Global, arguments: ChiselPluginArguments)(unit: global.CompilationUnit): Boolean = {
+ private[plugin] def runComponent(
+ global: Global,
+ arguments: ChiselPluginArguments
+ )(unit: global.CompilationUnit
+ ): Boolean = {
// This plugin doesn't work on Scala 2.11 nor Scala 3. Rather than complicate the sbt build flow,
// instead we just check the version and if its an early Scala version, the plugin does nothing
val scalaVersion = scala.util.Properties.versionNumberString.split('.')
@@ -70,6 +74,4 @@ class ChiselPlugin(val global: Global) extends Plugin {
true
}
-
}
-