Skip to content

Commit 9cdd950

Browse files
committed
Lambda functiona return value parametrized with auto and C++14 in-then-else codegen to support type deduction on if-then-else return type
1 parent ef6a27f commit 9cdd950

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

src/common/Functions.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ trait CGenFunctions extends CGenEffect with BaseGenFunctions {
350350
override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
351351
case e@Lambda(fun, x, y) =>
352352
val retType = remap(getBlockResult(y).tp)
353-
stream.println("function<"+retType+"("+
354-
remap(x.tp)+")> "+quote(sym)+
353+
val retTp = if (cppExplicitFunRet == "true") "function<"+retType+"("+remap(x.tp)+")>" else "auto"
354+
stream.println(retTp+" "+quote(sym)+
355355
" = [&]("+remap(x.tp)+" "+quote(x)+") {")
356356
emitBlock(y)
357357
val z = getBlockResult(y)

src/common/IfThenElse.scala

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -384,14 +384,17 @@ trait CGenIfThenElse extends CGenEffect with BaseGenIfThenElse {
384384
emitBlock(b)
385385
stream.println("}")
386386
case _ =>
387-
stream.println("%s %s;".format(remap(sym.tp),quote(sym)))
388-
stream.println("if (" + quote(c) + ") {")
389-
emitBlock(a)
390-
stream.println("%s = %s;".format(quote(sym),quote(getBlockResult(a))))
391-
stream.println("} else {")
392-
emitBlock(b)
393-
stream.println("%s = %s;".format(quote(sym),quote(getBlockResult(b))))
394-
stream.println("}")
387+
val ten = quote(sym) + "True"
388+
val fen = quote(sym) + "False"
389+
def emitCondFun[T: Manifest](fname: String, block: Block[T]) {
390+
stream.println("auto " + fname + " = [&]() {");
391+
emitBlock(block)
392+
stream.println("return " + quote(getBlockResult(block)) + ";")
393+
stream.println("};")
394+
}
395+
emitCondFun(ten, a)
396+
emitCondFun(fen, b)
397+
stream.println("auto " + quote(sym) + " = " + quote(c) + " ? " + ten + "() : " + fen + "();")
395398
}
396399
/*
397400
val booll = remap(sym.tp).equals("void")

src/internal/Config.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ trait Config {
88

99
// memory management type for C++ target (refcnt or gc)
1010
val cppMemMgr = System.getProperty("lms.cpp.memmgr","malloc")
11+
12+
// explicit return type of lambda functions (allows recursive functions but is less generic)
13+
val cppExplicitFunRet = System.getProperty("lms.cpp.explicitFunRet","true")
1114
}

0 commit comments

Comments
 (0)