Skip to content

Commit

Permalink
Fix more regression cases with CondExp and CommaExp
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Nov 20, 2015
1 parent 30695b6 commit 4e6a915
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
52 changes: 47 additions & 5 deletions src/inline.d
Expand Up @@ -1202,17 +1202,56 @@ public:
if (!s.exp)
return;

if (s.exp.op == TOKcall)
Statement inlineScanExpAsStatement(ref Expression exp)
{
/* If there's a TOKcall at the top, then it may fail to inline
* as an Expression. Try to inline as a Statement instead.
*/
visitCallExp(cast(CallExp)s.exp, null, true);
return;
if (exp.op == TOKcall)
{
visitCallExp(cast(CallExp)exp, null, true);
if (eresult)
exp = eresult;
auto s = sresult;
sresult = null;
eresult = null;
return s;
}

/* If there's a CondExp or CommaExp at the top, then its
* sub-expressions may be inlined as statements.
*/
if (exp.op == TOKquestion)
{
auto e = cast(CondExp)exp;
inlineScan(e.econd);
auto s1 = inlineScanExpAsStatement(e.e1);
auto s2 = inlineScanExpAsStatement(e.e2);
if (!s1 && !s2)
return null;
auto ifbody = !s1 ? new ExpStatement(e.e1.loc, e.e1) : s1;
auto elsebody = !s2 ? new ExpStatement(e.e2.loc, e.e2) : s2;
return new IfStatement(exp.loc, null, e.econd, ifbody, elsebody);
}
if (exp.op == TOKcomma)
{
auto e = cast(CommaExp)exp;
auto s1 = inlineScanExpAsStatement(e.e1);
auto s2 = inlineScanExpAsStatement(e.e2);
if (!s1 && !s2)
return null;
auto a = new Statements();
a.push(!s1 ? new ExpStatement(e.e1.loc, e.e1) : s1);
a.push(!s2 ? new ExpStatement(e.e2.loc, e.e2) : s2);
return new CompoundStatement(exp.loc, a);
}

// inline as an expression
inlineScan(exp);
return null;
}

// inline as an expression
inlineScan(s.exp);
sresult = inlineScanExpAsStatement(s.exp);
}

override void visit(CompoundStatement s)
Expand Down Expand Up @@ -1491,6 +1530,9 @@ public:
* e = the function call
* eret = if !null, then this is the lvalue of the nrvo function result
* asStatements = if inline as statements rather than as an Expression
* Returns:
* this.eresult if asStatements == false
* this.sresult if asStatements == true
*/
void visitCallExp(CallExp e, Expression eret, bool asStatements)
{
Expand Down
20 changes: 20 additions & 0 deletions test/runnable/inline.d
Expand Up @@ -936,14 +936,34 @@ struct S15296
}
}

pragma(inline, true)
static void voidCall15296()
{
for (size_t w = 0; w < 3; w++) { ++x15296; }
}

void test15296()
{
bool cond = true;

S15296 s;

// CallExp at the top of ExpStatement
x15296 = 0;
s.foo(0, 0);
assert(x15296 == 2);

// CondExp at the top of ExpStatement
x15296 = 0;
(cond ? s.foo(0, 0) : voidCall15296());
assert(x15296 == 2);
(cond ? voidCall15296() : s.foo(0, 0));
assert(x15296 == 2 + 3);

// CommaExp at the top of ExpStatement
x15296 = 0;
(s.foo(0, 0), voidCall15296());
assert(x15296 == 3 + 2);
}

/**********************************/
Expand Down

0 comments on commit 4e6a915

Please sign in to comment.