Skip to content

Commit

Permalink
Merge pull request #203 from tiffany1618/add-default-cpp-params
Browse files Browse the repository at this point in the history
Add default C++ function parameters
  • Loading branch information
bocchino authored Jan 6, 2023
2 parents a6172a4 + dd67c66 commit 9976f75
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
3 changes: 2 additions & 1 deletion compiler/lib/src/main/scala/codegen/CppWriter/CppDoc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ object CppDoc {
case class Param(
t: Type,
name: String,
comment: Option[String] = None
comment: Option[String] = None,
default: Option[String] = None
)
sealed trait SVQualifier
case object NonSV extends SVQualifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ object CppDocHppWriter extends CppDocWriter {
case None => s
}

def addParamDefault(s: String, defaultOpt: Option[String]): String = defaultOpt match {
case Some(default) => s"$s = $default"
case None => s
}

def closeIncludeGuard: List[Line] = lines(
"""|
|#endif"""
Expand All @@ -25,14 +30,17 @@ object CppDocHppWriter extends CppDocWriter {

def paramString(p: CppDoc.Function.Param): String = {
val s1 = CppDocCppWriter.paramString(p)
val s2 = addParamComment(s1, p.comment)
s2
val s2 = addParamDefault(s1, p.default)
val s3 = addParamComment(s2, p.comment)
s3
}

def paramStringComma(p: CppDoc.Function.Param): String = {
val s1 = CppDocCppWriter.paramStringComma(p)
val s2 = addParamComment(s1, p.comment)
s2
val s1 = CppDocCppWriter.paramString(p)
val s2 = addParamDefault(s1, p.default)
val s3 = s"$s2,"
val s4 = addParamComment(s3, p.comment)
s4
}

def writeAccessTag(tag: String): List[Line] = List(
Expand All @@ -43,7 +51,7 @@ object CppDocHppWriter extends CppDocWriter {
def writeParams(prefix: String, params: List[CppDoc.Function.Param]): List[Line] = {
if (params.length == 0) lines(s"$prefix()")
else if (params.length == 1 && params.head.comment.isEmpty)
lines(s"$prefix(" ++ CppDocCppWriter.paramString(params.head) ++ ")")
lines(s"$prefix(" ++ paramString(params.head) ++ ")")
else {
val head :: tail = params.reverse
val paramLines = (writeParam(head) :: tail.map(writeParamComma(_))).reverse
Expand Down
6 changes: 4 additions & 2 deletions compiler/lib/test/codegen/CppWriter/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,14 @@ object Program extends LineUtils {
Function.Param(
Type("const double", None),
"x",
Some("This is parameter x")
Some("This is parameter x"),
Some("0.0")
),
Function.Param(
Type("const int", None),
"y",
Some("This is parameter y")
Some("This is parameter y"),
Some("0")
)
),
retType = Type("void", None),
Expand Down

0 comments on commit 9976f75

Please sign in to comment.