From 3131c0daad41dea78bede4517669e376c41a325a Mon Sep 17 00:00:00 2001 From: Jack Koenig Date: Mon, 10 Jan 2022 10:39:52 -0800 Subject: Apply scalafmt Command: sbt scalafmtAll --- .../scala/chisel3/internal/InstantiableMacro.scala | 27 +++++++---- .../scala/chisel3/internal/RangeTransform.scala | 8 ++-- .../internal/RuntimeDeprecationTransform.scala | 40 ++++++++++------ .../internal/naming/NamingAnnotations.scala | 53 +++++++++++++--------- .../internal/sourceinfo/SourceInfoTransform.scala | 11 +++-- 5 files changed, 84 insertions(+), 55 deletions(-) (limited to 'macros/src') diff --git a/macros/src/main/scala/chisel3/internal/InstantiableMacro.scala b/macros/src/main/scala/chisel3/internal/InstantiableMacro.scala index 18c6c7aa..d66b51ac 100644 --- a/macros/src/main/scala/chisel3/internal/InstantiableMacro.scala +++ b/macros/src/main/scala/chisel3/internal/InstantiableMacro.scala @@ -6,7 +6,6 @@ import scala.language.experimental.macros import scala.annotation.StaticAnnotation import scala.reflect.macros.whitebox - private[chisel3] object instantiableMacro { def impl(c: whitebox.Context)(annottees: c.Expr[Any]*): c.Expr[Any] = { @@ -38,7 +37,7 @@ private[chisel3] object instantiableMacro { val result = { val (clz, objOpt) = annottees.map(_.tree).toList match { case Seq(c, o) => (c, Some(o)) - case Seq(c) => (c, None) + case Seq(c) => (c, None) } val (newClz, implicitClzs, tpname) = clz match { case q"$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents { $self => ..$stats }" => @@ -46,19 +45,27 @@ private[chisel3] object instantiableMacro { val instname = TypeName(tpname + c.freshName()) val (newStats, extensions) = processBody(stats) val argTParams = tparams.map(_.name) - (q""" $mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents with chisel3.experimental.hierarchy.IsInstantiable { $self => ..$newStats } """, - Seq(q"""implicit class $defname[..$tparams](___module: chisel3.experimental.hierarchy.Definition[$tpname[..$argTParams]]) { ..$extensions }""", - q"""implicit class $instname[..$tparams](___module: chisel3.experimental.hierarchy.Instance[$tpname[..$argTParams]]) { ..$extensions } """), - tpname) + ( + q""" $mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents with chisel3.experimental.hierarchy.IsInstantiable { $self => ..$newStats } """, + Seq( + q"""implicit class $defname[..$tparams](___module: chisel3.experimental.hierarchy.Definition[$tpname[..$argTParams]]) { ..$extensions }""", + q"""implicit class $instname[..$tparams](___module: chisel3.experimental.hierarchy.Instance[$tpname[..$argTParams]]) { ..$extensions } """ + ), + tpname + ) case q"$mods trait $tpname[..$tparams] extends { ..$earlydefns } with ..$parents { $self => ..$stats }" => val defname = TypeName(tpname + c.freshName()) val instname = TypeName(tpname + c.freshName()) val (newStats, extensions) = processBody(stats) val argTParams = tparams.map(_.name) - (q"$mods trait $tpname[..$tparams] extends { ..$earlydefns } with ..$parents with chisel3.experimental.hierarchy.IsInstantiable { $self => ..$newStats }", - Seq(q"""implicit class $defname[..$tparams](___module: chisel3.experimental.hierarchy.Definition[$tpname[..$argTParams]]) { ..$extensions }""", - q"""implicit class $instname[..$tparams](___module: chisel3.experimental.hierarchy.Instance[$tpname[..$argTParams]]) { ..$extensions } """), - tpname) + ( + q"$mods trait $tpname[..$tparams] extends { ..$earlydefns } with ..$parents with chisel3.experimental.hierarchy.IsInstantiable { $self => ..$newStats }", + Seq( + q"""implicit class $defname[..$tparams](___module: chisel3.experimental.hierarchy.Definition[$tpname[..$argTParams]]) { ..$extensions }""", + q"""implicit class $instname[..$tparams](___module: chisel3.experimental.hierarchy.Instance[$tpname[..$argTParams]]) { ..$extensions } """ + ), + tpname + ) } val newObj = objOpt match { case None => q"""object ${tpname.toTermName} { ..$implicitClzs } """ diff --git a/macros/src/main/scala/chisel3/internal/RangeTransform.scala b/macros/src/main/scala/chisel3/internal/RangeTransform.scala index b9a2d2aa..f401241d 100644 --- a/macros/src/main/scala/chisel3/internal/RangeTransform.scala +++ b/macros/src/main/scala/chisel3/internal/RangeTransform.scala @@ -12,8 +12,8 @@ import scala.util.matching.Regex // Workaround for https://github.com/sbt/sbt/issues/3966 object RangeTransform { val UnspecifiedNumber: Regex = """(\?).*""".r - val IntegerNumber: Regex = """(-?\d+).*""".r - val DecimalNumber: Regex = """(-?\d+\.\d+).*""".r + val IntegerNumber: Regex = """(-?\d+).*""".r + val DecimalNumber: Regex = """(-?\d+\.\d+).*""".r } /** Convert the string to IntervalRange, with unknown, open or closed endpoints and a binary point @@ -48,8 +48,8 @@ class RangeTransform(val c: blackbox.Context) { } var nextStringIndex: Int = 1 - var nextArgIndex: Int = 0 - var currString: String = strings.head + var nextArgIndex: Int = 0 + var currString: String = strings.head /** Mutably gets the next numeric value in the range specifier. */ diff --git a/macros/src/main/scala/chisel3/internal/RuntimeDeprecationTransform.scala b/macros/src/main/scala/chisel3/internal/RuntimeDeprecationTransform.scala index e7eb7162..ff41bd30 100644 --- a/macros/src/main/scala/chisel3/internal/RuntimeDeprecationTransform.scala +++ b/macros/src/main/scala/chisel3/internal/RuntimeDeprecationTransform.scala @@ -3,7 +3,7 @@ package chisel3.internal import scala.reflect.macros.whitebox.Context -import scala.annotation.{StaticAnnotation, compileTimeOnly} +import scala.annotation.{compileTimeOnly, StaticAnnotation} import scala.language.experimental.macros // Workaround for https://github.com/sbt/sbt/issues/3966 @@ -14,24 +14,34 @@ class RuntimeDeprecatedTransform(val c: Context) { /** Adds a Builder.deprecated(...) call based on the contents of a plain @deprecated annotation. */ def runtimeDeprecated(annottees: c.Tree*): c.Tree = { - val transformed = annottees.map(annottee => annottee match { - case q"$mods def $tname[..$tparams](...$paramss): $tpt = $expr" => { - val Modifiers(_, _, annotations) = mods - val annotationMessage = annotations.collect { // get all messages from deprecated annotations - case q"new deprecated($desc, $since)" => desc - } match { // ensure there's only one and return it - case msg :: Nil => msg - case _ => c.abort(c.enclosingPosition, s"@chiselRuntimeDeprecated annotion must be used with exactly one @deprecated annotation, got annotations $annotations") - } - val message = s"$tname is deprecated: $annotationMessage" - val transformedExpr = q""" { + val transformed = annottees.map(annottee => + annottee match { + case q"$mods def $tname[..$tparams](...$paramss): $tpt = $expr" => { + val Modifiers(_, _, annotations) = mods + val annotationMessage = annotations.collect { // get all messages from deprecated annotations + case q"new deprecated($desc, $since)" => desc + } match { // ensure there's only one and return it + case msg :: Nil => msg + case _ => + c.abort( + c.enclosingPosition, + s"@chiselRuntimeDeprecated annotion must be used with exactly one @deprecated annotation, got annotations $annotations" + ) + } + val message = s"$tname is deprecated: $annotationMessage" + val transformedExpr = q""" { _root_.chisel3.internal.Builder.deprecated($message) $expr } """ - q"$mods def $tname[..$tparams](...$paramss): $tpt = $transformedExpr" + q"$mods def $tname[..$tparams](...$paramss): $tpt = $transformedExpr" + } + case other => + c.abort( + c.enclosingPosition, + s"@chiselRuntimeDeprecated annotion may only be used on defs, got ${showCode(other)}" + ) } - case other => c.abort(c.enclosingPosition, s"@chiselRuntimeDeprecated annotion may only be used on defs, got ${showCode(other)}") - }) + ) q"..$transformed" } } diff --git a/macros/src/main/scala/chisel3/internal/naming/NamingAnnotations.scala b/macros/src/main/scala/chisel3/internal/naming/NamingAnnotations.scala index 0c400a07..82223d78 100644 --- a/macros/src/main/scala/chisel3/internal/naming/NamingAnnotations.scala +++ b/macros/src/main/scala/chisel3/internal/naming/NamingAnnotations.scala @@ -13,7 +13,7 @@ package chisel3.internal.naming import scala.reflect.macros.whitebox.Context -import scala.annotation.{StaticAnnotation, compileTimeOnly} +import scala.annotation.{compileTimeOnly, StaticAnnotation} import scala.language.experimental.macros // Workaround for https://github.com/sbt/sbt/issues/3966 @@ -79,13 +79,13 @@ class NamingTransforms(val c: Context) { class ClassBodyTransformer(val contextVar: TermName) extends ValNameTransformer { override def transform(tree: Tree): Tree = tree match { case q"$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents { $self => ..$stats }" => - tree // don't recurse into inner classes + tree // don't recurse into inner classes case q"$mods trait $tpname[..$tparams] extends { ..$earlydefns } with ..$parents { $self => ..$stats }" => - tree // don't recurse into inner classes + tree // don't recurse into inner classes case q"$mods def $tname[..$tparams](...$paramss): $tpt = $expr" => { val Modifiers(_, _, annotations) = mods // don't apply naming transform twice - val containsChiselName = annotations.map({q"new chiselName()" equalsStructure _}).fold(false)({_||_}) + val containsChiselName = annotations.map({ q"new chiselName()" equalsStructure _ }).fold(false)({ _ || _ }) // transforming overloaded initializers causes errors, and the transform isn't helpful val isInitializer = tname == TermName("") if (containsChiselName || isInitializer) { @@ -108,7 +108,7 @@ class NamingTransforms(val c: Context) { case q"return $expr" => q"return $globalNamingStack.popReturnContext($expr, $contextVar)" // Do not recurse into methods case q"$mods def $tname[..$tparams](...$paramss): $tpt = $expr" => tree - case other => super.transform(other) + case other => super.transform(other) } } @@ -163,26 +163,35 @@ class NamingTransforms(val c: Context) { def chiselName(annottees: c.Tree*): c.Tree = { var namedElts: Int = 0 - val transformed = annottees.map(annottee => annottee match { - case q"$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents { $self => ..$stats }" => { - val transformedStats = transformClassBody(stats) - namedElts += 1 - q"$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents { $self => ..$transformedStats }" - } - case q"$mods object $tname extends { ..$earlydefns } with ..$parents { $self => ..$body }" => { - annottee // Don't fail noisly when a companion object is passed in with the actual class def - } - // Currently disallow on traits, this won't work well with inheritance. - case q"$mods def $tname[..$tparams](...$paramss): $tpt = $expr" => { - val transformedExpr = transformHierarchicalMethod(expr) - namedElts += 1 - q"$mods def $tname[..$tparams](...$paramss): $tpt = $transformedExpr" + val transformed = annottees.map(annottee => + annottee match { + case q"$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents { $self => ..$stats }" => { + val transformedStats = transformClassBody(stats) + namedElts += 1 + q"$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents { $self => ..$transformedStats }" + } + case q"$mods object $tname extends { ..$earlydefns } with ..$parents { $self => ..$body }" => { + annottee // Don't fail noisly when a companion object is passed in with the actual class def + } + // Currently disallow on traits, this won't work well with inheritance. + case q"$mods def $tname[..$tparams](...$paramss): $tpt = $expr" => { + val transformedExpr = transformHierarchicalMethod(expr) + namedElts += 1 + q"$mods def $tname[..$tparams](...$paramss): $tpt = $transformedExpr" + } + case other => + c.abort( + c.enclosingPosition, + s"@chiselName annotion may only be used on classes and methods, got ${showCode(other)}" + ) } - case other => c.abort(c.enclosingPosition, s"@chiselName annotion may only be used on classes and methods, got ${showCode(other)}") - }) + ) if (namedElts != 1) { - c.abort(c.enclosingPosition, s"@chiselName annotation did not match exactly one valid tree, got:\r\n${annottees.map(tree => showCode(tree)).mkString("\r\n\r\n")}") + c.abort( + c.enclosingPosition, + s"@chiselName annotation did not match exactly one valid tree, got:\r\n${annottees.map(tree => showCode(tree)).mkString("\r\n\r\n")}" + ) } q"..$transformed" diff --git a/macros/src/main/scala/chisel3/internal/sourceinfo/SourceInfoTransform.scala b/macros/src/main/scala/chisel3/internal/sourceinfo/SourceInfoTransform.scala index ac3e236c..01e0acd6 100644 --- a/macros/src/main/scala/chisel3/internal/sourceinfo/SourceInfoTransform.scala +++ b/macros/src/main/scala/chisel3/internal/sourceinfo/SourceInfoTransform.scala @@ -9,7 +9,6 @@ import scala.language.experimental.macros import scala.reflect.macros.blackbox.Context import scala.reflect.macros.whitebox - /** Transforms a function call so that it can both provide implicit-style source information and * have a chained apply call. Without macros, only one is possible, since having a implicit * argument in the definition will cause the compiler to interpret a chained apply as an @@ -65,7 +64,6 @@ class DefinitionWrapTransform(val c: Context) extends SourceInfoTransformMacro { } } - // Workaround for https://github.com/sbt/sbt/issues/3966 object InstanceTransform // Module instantiation transform @@ -138,7 +136,7 @@ class VecTransform(val c: Context) extends SourceInfoTransformMacro { def reduceTree(redOp: c.Tree, layerOp: c.Tree): c.Tree = { q"$thisObj.do_reduceTree($redOp,$layerOp)($implicitSourceInfo, $implicitCompileOptions)" } - def reduceTreeDefault(redOp: c.Tree ): c.Tree = { + def reduceTreeDefault(redOp: c.Tree): c.Tree = { q"$thisObj.do_reduceTree($redOp)($implicitSourceInfo, $implicitCompileOptions)" } } @@ -148,13 +146,17 @@ class VecTransform(val c: Context) extends SourceInfoTransformMacro { */ abstract class AutoSourceTransform extends SourceInfoTransformMacro { import c.universe._ + /** Returns the TermName of the transformed function, which is the applied function name with do_ * prepended. */ def doFuncTerm: TermName = { val funcName = c.macroApplication match { case q"$_.$funcName[..$_](...$_)" => funcName - case _ => throw new Exception(s"Chisel Internal Error: Could not resolve function name from macro application: ${showCode(c.macroApplication)}") + case _ => + throw new Exception( + s"Chisel Internal Error: Could not resolve function name from macro application: ${showCode(c.macroApplication)}" + ) } TermName("do_" + funcName) } @@ -223,6 +225,7 @@ class CompileOptionsTransform(val c: Context) extends AutoSourceTransform { // Workaround for https://github.com/sbt/sbt/issues/3966 object SourceInfoWhiteboxTransform + /** Special whitebox version of the blackbox SourceInfoTransform, used when fun things need to * happen to satisfy the type system while preventing the use of macro overrides. */ -- cgit v1.2.3