Skip to content

Commit

Permalink
[Refactoring] ArrayScopeSymbol::search
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Mar 16, 2014
1 parent 6cf3992 commit b12585b
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions src/dsymbol.c
Expand Up @@ -1390,13 +1390,14 @@ Dsymbol *ArrayScopeSymbol::search(Loc loc, Identifier *ident, int flags)
{
//printf("ArrayScopeSymbol::search('%s', flags = %d)\n", ident->toChars(), flags);
if (ident == Id::dollar)
{ VarDeclaration **pvar;
{
VarDeclaration **pvar;
Expression *ce;

L1:

if (td)
{ /* $ gives the number of elements in the tuple
{
/* $ gives the number of elements in the tuple
*/
VarDeclaration *v = new VarDeclaration(loc, Type::tsize_t, Id::dollar, NULL);
Expression *e = new IntegerExp(Loc(), td->objects->dim, Type::tsize_t);
Expand All @@ -1407,7 +1408,8 @@ Dsymbol *ArrayScopeSymbol::search(Loc loc, Identifier *ident, int flags)
}

if (type)
{ /* $ gives the number of type entries in the type tuple
{
/* $ gives the number of type entries in the type tuple
*/
VarDeclaration *v = new VarDeclaration(loc, Type::tsize_t, Id::dollar, NULL);
Expression *e = new IntegerExp(Loc(), type->arguments->dim, Type::tsize_t);
Expand All @@ -1418,34 +1420,36 @@ Dsymbol *ArrayScopeSymbol::search(Loc loc, Identifier *ident, int flags)
}

if (exp->op == TOKindex)
{ /* array[index] where index is some function of $
{
/* array[index] where index is some function of $
*/
IndexExp *ie = (IndexExp *)exp;

pvar = &ie->lengthVar;
ce = ie->e1;
}
else if (exp->op == TOKslice)
{ /* array[lwr .. upr] where lwr or upr is some function of $
{
/* array[lwr .. upr] where lwr or upr is some function of $
*/
SliceExp *se = (SliceExp *)exp;

pvar = &se->lengthVar;
ce = se->e1;
}
else if (exp->op == TOKarray)
{ /* array[e0, e1, e2, e3] where e0, e1, e2 are some function of $
{
/* array[e0, e1, e2, e3] where e0, e1, e2 are some function of $
* $ is a opDollar!(dim)() where dim is the dimension(0,1,2,...)
*/
ArrayExp *ae = (ArrayExp *)exp;

pvar = &ae->lengthVar;
ce = ae->e1;
}
else
{
/* Didn't find $, look in enclosing scope(s).
*/
return NULL;
}

while (ce->op == TOKcomma)
ce = ((CommaExp *)ce)->e2;
Expand All @@ -1458,7 +1462,8 @@ Dsymbol *ArrayScopeSymbol::search(Loc loc, Identifier *ident, int flags)
{
Type *t = ((TypeExp *)ce)->type;
if (t->ty == Ttuple)
{ type = (TypeTuple *)t;
{
type = (TypeTuple *)t;
goto L1;
}
}
Expand All @@ -1467,12 +1472,14 @@ Dsymbol *ArrayScopeSymbol::search(Loc loc, Identifier *ident, int flags)
* multiple times, it gets set only once.
*/
if (!*pvar) // if not already initialized
{ /* Create variable v and set it to the value of $
{
/* Create variable v and set it to the value of $
*/
VarDeclaration *v;
Type *t;
if (ce->op == TOKtuple)
{ /* It is for an expression tuple, so the
{
/* It is for an expression tuple, so the
* length will be a const.
*/
Expression *e = new IntegerExp(Loc(), ((TupleExp *)ce)->exps->dim, Type::tsize_t);
Expand All @@ -1481,18 +1488,10 @@ Dsymbol *ArrayScopeSymbol::search(Loc loc, Identifier *ident, int flags)
}
else if (ce->type && (t = ce->type->toBasetype()) != NULL &&
(t->ty == Tstruct || t->ty == Tclass))
{ // Look for opDollar
{
// Look for opDollar
assert(exp->op == TOKarray || exp->op == TOKslice);
AggregateDeclaration *ad = NULL;

if (t->ty == Tclass)
{
ad = ((TypeClass *)t)->sym;
}
else if (t->ty == Tstruct)
{
ad = ((TypeStruct *)t)->sym;
}
AggregateDeclaration *ad = isAggregate(t);
assert(ad);

Dsymbol *s = ad->search(loc, Id::opDollar);
Expand Down Expand Up @@ -1521,7 +1520,8 @@ Dsymbol *ArrayScopeSymbol::search(Loc loc, Identifier *ident, int flags)
e = new DotTemplateInstanceExp(loc, ce, td->ident, tiargs);
}
else
{ /* opDollar exists, but it's not a template.
{
/* opDollar exists, but it's not a template.
* This is acceptable ONLY for single-dimension indexing.
* Note that it's impossible to have both template & function opDollar,
* because both take no arguments.
Expand All @@ -1545,7 +1545,8 @@ Dsymbol *ArrayScopeSymbol::search(Loc loc, Identifier *ident, int flags)
v->storage_class |= STCtemp;
}
else
{ /* For arrays, $ will either be a compile-time constant
{
/* For arrays, $ will either be a compile-time constant
* (in which case its value in set during constant-folding),
* or a variable (in which case an expression is created in
* toir.c).
Expand Down

0 comments on commit b12585b

Please sign in to comment.