Skip to content

Commit

Permalink
Re-allow using delegate.ptr and .funcptr as lvalues
Browse files Browse the repository at this point in the history
  • Loading branch information
yebblies committed May 27, 2014
1 parent 9d48c47 commit 313243c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 25 deletions.
11 changes: 9 additions & 2 deletions src/e2ir.c
Expand Up @@ -4326,16 +4326,23 @@ elem *toElem(Expression *e, IRState *irs)

void visit(DelegatePtrExp *dpe)
{
// *cast(void**)(&dg)
elem *e = dpe->e1->toElem(irs);
e = el_una(I64 ? OP128_64 : OP64_32, totym(dpe->type), e);
Type *tb1 = dpe->e1->type->toBasetype();
e = addressElem(e, tb1);
e = el_una(OPind, totym(dpe->type), e);
el_setLoc(e, dpe->loc);
result = e;
}

void visit(DelegateFuncptrExp *dfpe)
{
// *cast(void**)(&dg + size_t.sizeof)
elem *e = dfpe->e1->toElem(irs);
e = el_una(OPmsw, totym(dfpe->type), e);
Type *tb1 = dfpe->e1->type->toBasetype();
e = addressElem(e, tb1);
e = el_bin(OPadd, TYnptr, e, el_long(TYsize_t, I64 ? 8 : 4));
e = el_una(OPind, totym(dfpe->type), e);
el_setLoc(e, dfpe->loc);
result = e;
}
Expand Down
22 changes: 22 additions & 0 deletions src/expression.c
Expand Up @@ -10227,6 +10227,17 @@ Expression *DelegatePtrExp::semantic(Scope *sc)
return this;
}

int DelegatePtrExp::isLvalue()
{
return e1->isLvalue();
}

Expression *DelegatePtrExp::toLvalue(Scope *sc, Expression *e)
{
e1 = e1->toLvalue(sc, e);
return this;
}

/********************** DelegateFuncptrExp **************************************/

DelegateFuncptrExp::DelegateFuncptrExp(Loc loc, Expression *e1)
Expand All @@ -10251,6 +10262,17 @@ Expression *DelegateFuncptrExp::semantic(Scope *sc)
return this;
}

int DelegateFuncptrExp::isLvalue()
{
return e1->isLvalue();
}

Expression *DelegateFuncptrExp::toLvalue(Scope *sc, Expression *e)
{
e1 = e1->toLvalue(sc, e);
return this;
}

/*********************** ArrayExp *************************************/

// e1 [ i1, i2, i3, ... ]
Expand Down
4 changes: 4 additions & 0 deletions src/expression.h
Expand Up @@ -1103,6 +1103,8 @@ class DelegatePtrExp : public UnaExp
public:
DelegatePtrExp(Loc loc, Expression *e1);
Expression *semantic(Scope *sc);
int isLvalue();
Expression *toLvalue(Scope *sc, Expression *e);
void accept(Visitor *v) { v->visit(this); }
};

Expand All @@ -1111,6 +1113,8 @@ class DelegateFuncptrExp : public UnaExp
public:
DelegateFuncptrExp(Loc loc, Expression *e1);
Expression *semantic(Scope *sc);
int isLvalue();
Expression *toLvalue(Scope *sc, Expression *e);
void accept(Visitor *v) { v->visit(this); }
};

Expand Down
23 changes: 0 additions & 23 deletions test/fail_compilation/faildg.d

This file was deleted.

26 changes: 26 additions & 0 deletions test/runnable/delegate.d
Expand Up @@ -309,6 +309,31 @@ void test2472()

/********************************************************/

void testAssign()
{
static class C
{
int a;
this(int a) { this.a = a; }
int funca() { return a; }
int funcb() { return a + 1; }
}

auto x = new C(5);
auto y = new C(7);

auto dg = &x.funca;
assert(dg() == 5);
dg.funcptr = &C.funcb;
assert(dg() == 6);
dg.ptr = cast(void*)y;
assert(dg() == 8);
dg.funcptr = &C.funca;
assert(dg() == 7);
}

/********************************************************/

int main()
{
test1();
Expand All @@ -325,6 +350,7 @@ int main()
test13();
test2472();
test8257();
testAssign();

printf("Success\n");
return 0;
Expand Down

0 comments on commit 313243c

Please sign in to comment.