Skip to content

Commit 0cb6eb3

Browse files
author
Oron Port
committed
derive DFApp arg type names from values and just ignore non-supported typed arguments instead of forbidding them
1 parent 0d13e7b commit 0cb6eb3

File tree

4 files changed

+32
-38
lines changed

4 files changed

+32
-38
lines changed

lib/src/main/scala/dfhdl/app/DFApp.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ trait DFApp:
4141
_topScalaPath: String,
4242
top: dfhdl.top,
4343
argNames: List[String],
44-
argTypes: List[String],
4544
argValues: List[Any],
4645
argDescs: List[String]
4746
): Unit =
@@ -52,7 +51,7 @@ trait DFApp:
5251
printerOptions = top.printerOptions
5352
linterOptions = top.linterOptions
5453
appOptions = top.appOptions
55-
designArgs = DesignArgs(argNames, argTypes, argValues, argDescs)
54+
designArgs = DesignArgs(argNames, argValues, argDescs)
5655
end setInitials
5756
final protected def setDsn(d: => core.Design): Unit = dsn = () => d
5857
private def elaborate: core.Design =

lib/src/main/scala/dfhdl/app/DesignArgs.scala

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
package dfhdl.app
22
import dfhdl.*
3+
import dfhdl.compiler.ir
34
import core.{DFValAny, asValAny}
45
import scala.collection.immutable.ListMap
56

6-
case class DesignArg(name: String, typeName: String, value: Any, desc: String)(using DFC):
7+
case class DesignArg(name: String, value: Any, desc: String)(using DFC):
8+
val typeName =
9+
value match
10+
case _: String => "String"
11+
case _: Int => "Int"
12+
case _: Double => "Double"
13+
case _: Boolean => "Boolean"
14+
case _: BigInt => "Int"
15+
case dfConst: DFValAny =>
16+
dfConst.asIR.dfType match
17+
case ir.DFBit | ir.DFBool => "Boolean"
18+
case ir.DFInt32 => "Int"
19+
case _ => ""
20+
case _ => ""
721
def getScalaValue: Any =
822
val data = value match
923
case dfConst: DFValAny =>
@@ -29,13 +43,12 @@ object DesignArgs:
2943
def empty: DesignArgs = ListMap.empty
3044
def apply(
3145
argNames: List[String],
32-
argTypes: List[String],
3346
argValues: List[Any],
3447
argDescs: List[String]
3548
): DesignArgs =
3649
ListMap.from(
37-
argNames.lazyZip(argTypes).lazyZip(argValues).lazyZip(argDescs).map(
38-
(name, typeName, value, desc) => name -> DesignArg(name, typeName, value, desc)
50+
argNames.lazyZip(argValues).lazyZip(argDescs).map((name, value, desc) =>
51+
name -> DesignArg(name, value, desc)
3952
)
4053
)
4154
def apply(iter: Iterable[(String, DesignArg)]): DesignArgs = ListMap.from(iter)

lib/src/main/scala/dfhdl/app/ParsedCommandLine.scala

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,21 +136,22 @@ class ParsedCommandLine(
136136
exitHandler = code => exitCodeOption = Some(code)
137137
private lazy val designArgOptionGroup = group("Design arguments:")
138138
private val designArgOptions =
139-
for (designArg <- designArgs.values)
140-
yield
139+
designArgs.view.values.collect {
140+
case designArg if designArg.typeName.nonEmpty =>
141141
val conv = designArg.typeName match
142-
case "String" => summon[ValueConverter[String]]
143-
case "Int" => summon[ValueConverter[Int]]
144-
case "Double" => summon[ValueConverter[Double]]
145-
case "Boolean" | "Bit" => summon[ValueConverter[Boolean]]
146-
case _ => ???
147-
opt[Any](
142+
case "String" => summon[ValueConverter[String]]
143+
case "Int" => summon[ValueConverter[Int]]
144+
case "Double" => summon[ValueConverter[Double]]
145+
case "Boolean" => summon[ValueConverter[Boolean]]
146+
designArg.name -> opt[Any](
148147
name = designArg.name, descr = designArg.desc, argName = designArg.typeName,
149148
default = Some(designArg.getScalaValue), noshort = true, group = designArgOptionGroup
150149
)(conv.asInstanceOf[ValueConverter[Any]])
151-
lazy val updatedDesignArgs: DesignArgs = DesignArgs(designArgs.lazyZip(designArgOptions).map {
152-
case ((argName, designArg), opt) =>
153-
(argName, designArg.updateScalaValue(opt.toOption.get))
150+
}.toMap
151+
lazy val updatedDesignArgs: DesignArgs = DesignArgs(designArgs.map { case (argName, designArg) =>
152+
designArgOptions.get(argName) match
153+
case None => argName -> designArg
154+
case Some(opt) => argName -> designArg.updateScalaValue(opt.toOption.get)
154155
})
155156

156157
addSubcommand(Mode.elaborate)

plugin/src/main/scala/plugin/TopAnnotPhase.scala

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,6 @@ class TopAnnotPhase(setting: Setting) extends CommonPhase:
7979
val paramVDs =
8080
template.constr.paramss.flatten.collect { case vd: ValDef => vd }
8181
val dsnArgNames = mkList(paramVDs.map(vd => Literal(Constant(vd.name.toString))))
82-
extension (vd: Type)
83-
def argType: Option[String] =
84-
if (vd <:< defn.IntType || vd <:< dfConstInt32Tpe) Some("Int")
85-
else if (vd <:< defn.StringType) Some("String")
86-
else if (vd <:< defn.DoubleType) Some("Double")
87-
else if (vd <:< defn.BooleanType || vd <:< dfConstBoolTpe) Some("Boolean")
88-
else if (vd <:< dfConstBitTpe) Some("Bit")
89-
else None
90-
91-
val dsnArgTypes = mkList(paramVDs.map { vd =>
92-
vd.tpt.tpe.argType match
93-
case Some(value) => Literal(Constant(value))
94-
case _ =>
95-
report.error(
96-
"Unsupported argument's type for top-level design with a default app entry point.\nEither use a supported type or disable the app entry point generation with `@top(false)`.",
97-
vd.srcPos
98-
)
99-
EmptyTree
100-
})
10182
val defaultMap = mutable.Map.empty[Int, Tree]
10283
rest match
10384
case (_: ValDef) :: (compSym @ TypeDef(_, compTemplate: Template)) :: _
@@ -125,8 +106,8 @@ class TopAnnotPhase(setting: Setting) extends CommonPhase:
125106
mkList(paramVDs.map(vd => Literal(Constant(vd.symbol.docString.getOrElse("")))))
126107
val setInitials = This(moduleCls).select("setInitials".toTermName).appliedToArgs(
127108
List(
128-
designNameTree, topScalaPathTree, topAnnotTree, dsnArgNames, dsnArgTypes,
129-
dsnArgValues, dsnArgDescs
109+
designNameTree, topScalaPathTree, topAnnotTree, dsnArgNames, dsnArgValues,
110+
dsnArgDescs
130111
)
131112
)
132113
val dsnInstArgs = paramVDs.map(vd =>

0 commit comments

Comments
 (0)