Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 6508 - alias this doesn't work with AssignExp rhs #322

Merged
merged 2 commits into from Aug 25, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
117 changes: 117 additions & 0 deletions src/expression.c
Expand Up @@ -330,6 +330,77 @@ void expandTuples(Expressions *exps)
}
}

/****************************************
* Expand alias this tuples.
*/

TupleDeclaration *isAliasThisTuple(Expression *e)
{
if (e->type)
{
Type *t = e->type->toBasetype();
AggregateDeclaration *ad;
if (t->ty == Tstruct)
{
ad = ((TypeStruct *)t)->sym;
goto L1;
}
else if (t->ty == Tclass)
{
ad = ((TypeClass *)t)->sym;
L1:
Dsymbol *s = ad->aliasthis;
if (s && s->isVarDeclaration())
{
TupleDeclaration *td = s->isVarDeclaration()->toAlias()->isTupleDeclaration();
if (td && td->isexp)
return td;
}
}
}
return NULL;
}

int expandAliasThisTuples(Expressions *exps, int starti)
{
if (!exps || exps->dim == 0)
return -1;

for (size_t u = starti; u < exps->dim; u++)
{
Expression *exp = exps->tdata()[u];
TupleDeclaration *td = isAliasThisTuple(exp);
if (td)
{
exps->remove(u);
for (size_t i = 0; i<td->objects->dim; ++i)
{
Expression *e = isExpression(td->objects->tdata()[i]);
assert(e);
assert(e->op == TOKdsymbol);
DsymbolExp *se = (DsymbolExp *)e;
Declaration *d = se->s->isDeclaration();
assert(d);
e = new DotVarExp(exp->loc, exp, d);
assert(d->type);
e->type = d->type;
exps->insert(u + i, e);
}
#if 0
printf("expansion ->\n");
for (size_t i = 0; i<exps->dim; ++i)
{
Expression *e = exps->tdata()[i];
printf("\texps[%d] e = %s %s\n", i, Token::tochars[e->op], e->toChars());
}
#endif
return u;
}
}

return -1;
}

Expressions *arrayExpressionToCommonType(Scope *sc, Expressions *exps, Type **pt)
{
#if DMDV1
Expand Down Expand Up @@ -9440,6 +9511,7 @@ Expression *AssignExp::semantic(Scope *sc)

/* Rewrite tuple assignment as a tuple of assignments.
*/
Ltupleassign:
if (e1->op == TOKtuple && e2->op == TOKtuple)
{ TupleExp *tup1 = (TupleExp *)e1;
TupleExp *tup2 = (TupleExp *)e2;
Expand All @@ -9464,6 +9536,51 @@ Expression *AssignExp::semantic(Scope *sc)
}
}

if (e1->op == TOKtuple)
{
if (TupleDeclaration *td = isAliasThisTuple(e2))
{
assert(e1->type->ty == Ttuple);
TypeTuple *tt = (TypeTuple *)e1->type;

Identifier *id = Lexer::uniqueId("__tup");
VarDeclaration *v = new VarDeclaration(e2->loc, NULL, id, new ExpInitializer(e2->loc, e2));
v->storage_class = STCctfe | STCref | STCforeach;
Expression *ve = new VarExp(e2->loc, v);
ve->type = e2->type;

Expressions *iexps = new Expressions();
iexps->push(ve);

for (size_t u = 0; u < iexps->dim ; u++)
{
Lexpand:
Expression *e = iexps->tdata()[u];

Parameter *arg = Parameter::getNth(tt->arguments, u);
//printf("[%d] iexps->dim = %d, ", u, iexps->dim);
//printf("e = (%s %s, %s), ", Token::tochars[e->op], e->toChars(), e->type->toChars());
//printf("arg = (%s, %s)\n", arg->toChars(), arg->type->toChars());

if (!e->type->implicitConvTo(arg->type))
{
// expand initializer to tuple
if (expandAliasThisTuples(iexps, u) != -1)
goto Lexpand;

goto Lnomatch;
}
}
iexps->tdata()[0] = new CommaExp(loc, new DeclarationExp(e2->loc, v), iexps->tdata()[0]);
e2 = new TupleExp(e2->loc, iexps);
e2 = e2->semantic(sc);
goto Ltupleassign;

Lnomatch:
;
}
}

// Determine if this is an initialization of a reference
int refinit = 0;
if (op == TOKconstruct && e1->op == TOKvar)
Expand Down
2 changes: 2 additions & 0 deletions src/expression.h
Expand Up @@ -69,6 +69,8 @@ void inferApplyArgTypes(enum TOK op, Parameters *arguments, Expression *aggr);
void argExpTypesToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs);
void argsToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs);
void expandTuples(Expressions *exps);
TupleDeclaration *isAliasThisTuple(Expression *e);
int expandAliasThisTuples(Expressions *exps, int starti = 0);
FuncDeclaration *hasThis(Scope *sc);
Expression *fromConstInitializer(int result, Expression *e);
int arrayExpressionCanThrow(Expressions *exps, bool mustNotThrow);
Expand Down
54 changes: 39 additions & 15 deletions test/runnable/aliasthis.d
@@ -1,5 +1,29 @@

import std.c.stdio;
extern (C) int printf(const(char*) fmt, ...);

struct Tup(T...)
{
T field;
alias field this;

bool opEquals()(auto ref Tup rhs) const
{
foreach (i, _; T)
if (field[i] != rhs.field[i])
return false;
return true;
}
}

Tup!T tup(T...)(T fields)
{
return typeof(return)(fields);
}

template Seq(T...)
{
alias T Seq;
}

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

Expand Down Expand Up @@ -64,15 +88,9 @@ void test2()

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

struct MyTuple(T...)
{
T value;
alias value this;
}

void test3()
{
MyTuple!(int, double) t;
Tup!(int, double) t;
t[0] = 1;
t[1] = 1.1;
assert(t[0] == 1);
Expand All @@ -82,17 +100,11 @@ void test3()

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

struct Tuple4(U...)
{
U field;
alias field this;
}

struct Iter
{
bool empty() { return true; }
void popFront() { }
ref Tuple4!(int, int) front() { return *new Tuple4!(int, int); }
ref Tup!(int, int) front() { return *new Tup!(int, int); }
ref Iter opSlice() { return this; }
}

Expand Down Expand Up @@ -172,6 +184,17 @@ void test6() {
assert(b.val == 2);
}

/**********************************************/
// 6508

void test6508()
{
int x, y;
Seq!(x, y) = tup(10, 20);
assert(x == 10);
assert(y == 20);
}

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

int main()
Expand All @@ -184,6 +207,7 @@ int main()
test4773();
test5188();
test6();
test6508();

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