Skip to content

Commit 54cad86

Browse files
committed
added system property to control if-then-else auto retrun type codegen
1 parent 9cdd950 commit 54cad86

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/common/IfThenElse.scala

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -384,17 +384,28 @@ trait CGenIfThenElse extends CGenEffect with BaseGenIfThenElse {
384384
emitBlock(b)
385385
stream.println("}")
386386
case _ =>
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("};")
387+
if (cppIfElseAutoRet == "true") {
388+
val ten = quote(sym) + "True"
389+
val fen = quote(sym) + "False"
390+
def emitCondFun[T: Manifest](fname: String, block: Block[T]) {
391+
stream.println("auto " + fname + " = [&]() {");
392+
emitBlock(block)
393+
stream.println("return " + quote(getBlockResult(block)) + ";")
394+
stream.println("};")
395+
}
396+
emitCondFun(ten, a)
397+
emitCondFun(fen, b)
398+
stream.println("auto " + quote(sym) + " = " + quote(c) + " ? " + ten + "() : " + fen + "();")
399+
} else {
400+
stream.println("%s %s;".format(remap(sym.tp),quote(sym)))
401+
stream.println("if (" + quote(c) + ") {")
402+
emitBlock(a)
403+
stream.println("%s = %s;".format(quote(sym),quote(getBlockResult(a))))
404+
stream.println("} else {")
405+
emitBlock(b)
406+
stream.println("%s = %s;".format(quote(sym),quote(getBlockResult(b))))
407+
stream.println("}")
394408
}
395-
emitCondFun(ten, a)
396-
emitCondFun(fen, b)
397-
stream.println("auto " + quote(sym) + " = " + quote(c) + " ? " + ten + "() : " + fen + "();")
398409
}
399410
/*
400411
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
@@ -11,4 +11,7 @@ trait Config {
1111

1212
// explicit return type of lambda functions (allows recursive functions but is less generic)
1313
val cppExplicitFunRet = System.getProperty("lms.cpp.explicitFunRet","true")
14+
15+
// auto return value of if-else expressions (allows type deduction on if-then-else expressions)
16+
val cppIfElseAutoRet = System.getProperty("lms.cpp.ifElseAutoRet","false")
1417
}

0 commit comments

Comments
 (0)