Skip to content

Commit

Permalink
fix Issue 5749 - argument evaluation order of chained function from r…
Browse files Browse the repository at this point in the history
…ight
  • Loading branch information
9rnsr committed Dec 31, 2013
1 parent 78331fc commit 7a31af4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/e2ir.c
Expand Up @@ -3646,8 +3646,34 @@ elem *CallExp::toElem(IRState *irs)
{ StructLiteralExp *sle = (StructLiteralExp *)dve->e1;
sle->sinit = NULL; // don't modify initializer
}

ec = dve->e1->toElem(irs);
ectype = dve->e1->type->toBasetype();

if (arguments && arguments->dim && ec->Eoper != OPvar)
{
if (ec->Eoper == OPind && el_sideeffect(ec->E1))
{
/* Rewrite (*exp)(arguments) as:
* tmp = exp, (*tmp)(arguments)
*/
elem *ec1 = ec->E1;
Symbol *stmp = symbol_genauto(type_fake(ec1->Ety));
eeq = el_bin(OPeq, ec->Ety, el_var(stmp), ec1);
ec->E1 = el_var(stmp);
}
else if (tybasic(ec->Ety) != TYnptr)
{
/* Rewrite (exp)(arguments) as:
* tmp=&exp, (*tmp)(arguments)
*/
ec = addressElem(ec, ectype);

Symbol *stmp = symbol_genauto(type_fake(ec->Ety));
eeq = el_bin(OPeq, ec->Ety, el_var(stmp), ec);
ec = el_una(OPind, ectype->totym(), el_var(stmp));
}
}
}
else if (e1->op == TOKvar)
{
Expand Down Expand Up @@ -3693,7 +3719,8 @@ elem *CallExp::toElem(IRState *irs)
* we need to solve this generally.
*/
if (ec->Eoper == OPind && el_sideeffect(ec->E1))
{ /* Rewrite (*exp)(arguments) as:
{
/* Rewrite (*exp)(arguments) as:
* tmp=exp, (*tmp)(arguments)
*/
elem *ec1 = ec->E1;
Expand All @@ -3702,7 +3729,8 @@ elem *CallExp::toElem(IRState *irs)
ec->E1 = el_var(stmp);
}
else if (tybasic(ec->Ety) == TYdelegate && el_sideeffect(ec))
{ /* Rewrite (exp)(arguments) as:
{
/* Rewrite (exp)(arguments) as:
* tmp=exp, (tmp)(arguments)
*/
Symbol *stmp = symbol_genauto(type_fake(ec->Ety));
Expand Down
49 changes: 49 additions & 0 deletions test/runnable/xtest46.d
Expand Up @@ -5928,6 +5928,54 @@ void test8395()
assert(cs.v == 14);
}

/***************************************************/
// 5749

void test5749()
{
static struct A
{
A foo(int x, int i)
{
//printf("this = %p, %d: i=%d\n", &this, x, i);
assert(i == x);
return this;
}
A bar(int x, ref int i)
{
//printf("this = %p, %d: i=%d\n", &this, x, i);
assert(i == x);
return this;
}
}

static int inc1(ref int i) { return ++i; }
static ref int inc2(ref int i) { return ++i; }

int i;
A a;
//printf("&a = %p\n", &a);

i = 0; a.foo(1, ++i).foo(2, ++i); // OK <-- 2 1
i = 0; a.bar(1, ++i).bar(2, ++i); // OK <-- 2 2
i = 0; a.foo(1, inc1(i)).foo(2, inc1(i)); // OK <-- 2 1
i = 0; a.bar(1, inc2(i)).bar(2, inc2(i)); // OK <-- 2 2
//printf("\n");

A getVal() { static A a; return a; }
i = 0; getVal().foo(1, ++i).foo(2, ++i); // OK <-- 2 1
i = 0; getVal().bar(1, ++i).bar(2, ++i); // OK <-- 2 2
i = 0; getVal().foo(1, inc1(i)).foo(2, inc1(i)); // OK <-- 2 1
i = 0; getVal().bar(1, inc2(i)).bar(2, inc2(i)); // OK <-- 2 2
//printf("\n");

ref A getRef() { static A a; return a; }
i = 0; getRef().foo(1, ++i).foo(2, ++i); // OK <-- 2 1
i = 0; getRef().bar(1, ++i).bar(2, ++i); // OK <-- 2 2
i = 0; getRef().foo(1, inc1(i)).foo(2, inc1(i)); // OK <-- 2 1
i = 0; getRef().bar(1, inc2(i)).bar(2, inc2(i)); // OK <-- 2 2
}

/***************************************************/
// 8396

Expand Down Expand Up @@ -7086,6 +7134,7 @@ int main()
test159();
test8283();
test8395();
test5749();
test8396();
test160();
test8665();
Expand Down

0 comments on commit 7a31af4

Please sign in to comment.