Skip to content

Commit

Permalink
Support multi-dimensional opIndex
Browse files Browse the repository at this point in the history
If it fails, fall back to opSlice for backward compatibility.
  • Loading branch information
9rnsr committed Mar 16, 2014
1 parent 6124e8d commit 1f308ef
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
47 changes: 29 additions & 18 deletions src/opover.c
Expand Up @@ -417,28 +417,13 @@ Expression *op_overload(Expression *e, Scope *sc)
AggregateDeclaration *ad = isAggregate(ae->e1->type);
if (ad)
{
#if 1
if (ae->arguments->dim == 0)
{
// a[]
SliceExp *se = new SliceExp(ae->loc, ae->e1, NULL, NULL);
result = se->semantic(sc);
return;
}
if (ae->arguments->dim == 1 && (*ae->arguments)[0]->op == TOKinterval)
{
// a[lwr..upr]
IntervalExp *ie = (IntervalExp *)(*ae->arguments)[0];
SliceExp *se = new SliceExp(ae->loc, ae->e1, ie->lwr, ie->upr);
result = se->semantic(sc);
return;
}
#endif
Dsymbol *fd = search_function(ad, opId(ae));
if (fd)
{
// Deal with $
Expression *ex = resolveOpDollar(sc, ae);
if (!ex)
goto Lfallback;
if (ex->op == TOKerror)
{
result = ex;
Expand All @@ -450,10 +435,18 @@ Expression *op_overload(Expression *e, Scope *sc)
*/
result = new DotIdExp(ae->loc, ae->e1, fd->ident);
result = new CallExp(ae->loc, result, ae->arguments);
result = result->semantic(sc);
if (ae->arguments->dim == 0)
result = result->trySemantic(sc);
else
result = result->semantic(sc);
if (!result)
goto Lfallback;
return;
}

if (ae->e1->op == TOKtype && ae->arguments->dim < 2)
goto Lfallback;

// Didn't find it. Forward to aliasthis
if (ad->aliasthis && ae->e1->type != ae->att1)
{
Expand All @@ -467,6 +460,24 @@ Expression *op_overload(Expression *e, Scope *sc)
ue->att1 = ae->e1->type;
ue->e1 = e1;
result = ue->trySemantic(sc);
if (result)
return;
}

Lfallback:
if (ae->arguments->dim == 0)
{
// a[]
SliceExp *se = new SliceExp(ae->loc, ae->e1, NULL, NULL);
result = se->semantic(sc);
return;
}
if (ae->arguments->dim == 1 && (*ae->arguments)[0]->op == TOKinterval)
{
// a[lwr..upr]
IntervalExp *ie = (IntervalExp *)(*ae->arguments)[0];
SliceExp *se = new SliceExp(ae->loc, ae->e1, ie->lwr, ie->upr);
result = se->semantic(sc);
return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/runnable/aliasthis.d
Expand Up @@ -1268,11 +1268,11 @@ void test8735()
// 9709 case
alias A = Tuple9709!(1,int,"foo");
A a;
static assert(A[0] == 1);
//static assert(A[0] == 1);
static assert(a[0] == 1);
//static assert(is(A[1] == int));
//static assert(is(a[1] == int));
static assert(A[2] == "foo");
//static assert(A[2] == "foo");
static assert(a[2] == "foo");
}

Expand Down
15 changes: 13 additions & 2 deletions test/runnable/opover2.d
Expand Up @@ -879,10 +879,10 @@ int test6798a()
}
}
S1 s1;
//assert( s1[] == tuple(" []"));
assert( s1[] == tuple(" []"));
assert( s1[10] == tuple(" []", 10));
assert( s1[10, 20] == tuple(" []", 10, 20));
//assert( s1[10..20] == tuple(" []", [0, 10, 20]));
assert( s1[10..20] == tuple(" []", [0, 10, 20]));
//assert(+s1[] == tuple("+[]"));
assert(-s1[10] == tuple("-[]", 10));
assert(*s1[10, 20] == tuple("*[]", 10, 20));
Expand Down Expand Up @@ -977,12 +977,23 @@ int test6798b()
return 0;
}

int test6798c()
{
alias T = Tuple6798!(int, int);
auto n = T[].init;
static assert(is(typeof(n[0]) == Tuple6798!(int, int)));

return 0;
}

void test6798()
{
static assert(test6798a() == 0); // CTFE check
test6798a();
static assert(test6798b() == 0);
test6798b();
static assert(test6798c() == 0);
test6798c();
}

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

0 comments on commit 1f308ef

Please sign in to comment.