Skip to content

Commit

Permalink
[Refactoring] Remove unnecessary isVarDeclaration() call
Browse files Browse the repository at this point in the history
`AggregateDeclaration::fields` is typed as an array of `VarDeclaration`. Therefore, assignment its elements to `Dsymbol *` + invocation of `isVarDeclaration` are merely redundant operations.
  • Loading branch information
9rnsr committed Dec 10, 2013
1 parent 6a65ae7 commit 6164bbd
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 94 deletions.
6 changes: 4 additions & 2 deletions src/argtypes.c
Expand Up @@ -321,7 +321,8 @@ TypeTuple *TypeStruct::toArgTypes()
#if 1
t1 = NULL;
for (size_t i = 0; i < sym->fields.dim; i++)
{ VarDeclaration *f = sym->fields[i];
{
VarDeclaration *f = sym->fields[i];
//printf("f->type = %s\n", f->type->toChars());

TypeTuple *tup = f->type->toArgTypes();
Expand Down Expand Up @@ -401,7 +402,8 @@ TypeTuple *TypeStruct::toArgTypes()
}
#else
if (sym->fields.dim == 1)
{ VarDeclaration *f = sym->fields[0];
{
VarDeclaration *f = sym->fields[0];
//printf("f->type = %s\n", f->type->toChars());
TypeTuple *tup = f->type->toArgTypes();
if (tup)
Expand Down
8 changes: 4 additions & 4 deletions src/cast.c
Expand Up @@ -934,8 +934,8 @@ MATCH NewExp::implicitConvTo(Type *t)
goto Lnomatch;

for (size_t i = 0; i < cd->fields.dim; i++)
{ Dsymbol *sm = cd->fields[i];
Declaration *d = sm->isDeclaration();
{
Declaration *d = cd->fields[i];
if (d->storage_class & STCref || d->hasPointers())
goto Lnomatch;
}
Expand Down Expand Up @@ -984,8 +984,8 @@ MATCH NewExp::implicitConvTo(Type *t)
goto Lnomatch;

for (size_t i = 0; i < sd->fields.dim; i++)
{ Dsymbol *sm = sd->fields[i];
Declaration *d = sm->isDeclaration();
{
Declaration *d = sd->fields[i];
if (d->storage_class & STCref || d->hasPointers())
goto Lnomatch;
}
Expand Down
6 changes: 3 additions & 3 deletions src/class.c
Expand Up @@ -658,8 +658,8 @@ void ClassDeclaration::semantic(Scope *sc)
// Unwind what we did, and defer it for later
for (size_t i = 0; i < fields.dim; i++)
{
if (VarDeclaration *v = fields[i])
v->offset = 0;
VarDeclaration *v = fields[i];
v->offset = 0;
}
fields.setDim(0);
structsize = 0;
Expand Down Expand Up @@ -693,7 +693,7 @@ void ClassDeclaration::semantic(Scope *sc)
// A class object is always created by constructor, so this check is legitimate.
for (size_t i = 0; i < fields.dim; i++)
{
VarDeclaration *v = fields[i]->isVarDeclaration();
VarDeclaration *v = fields[i];
if (v->storage_class & STCnodefaultctor)
::error(v->loc, "field %s must be initialized in constructor", v->toChars());
}
Expand Down
24 changes: 6 additions & 18 deletions src/clone.c
Expand Up @@ -131,9 +131,7 @@ int StructDeclaration::needOpAssign()
*/
for (size_t i = 0; i < fields.dim; i++)
{
Dsymbol *s = fields[i];
VarDeclaration *v = s->isVarDeclaration();
assert(v && v->isField());
VarDeclaration *v = fields[i];
if (v->storage_class & STCref)
continue;
Type *tv = v->type->baseElemOf();
Expand Down Expand Up @@ -207,9 +205,7 @@ FuncDeclaration *StructDeclaration::buildOpAssign(Scope *sc)
{
for (size_t i = 0; i < fields.dim; i++)
{
Dsymbol *s = fields[i];
VarDeclaration *v = s->isVarDeclaration();
assert(v && v->isField());
VarDeclaration *v = fields[i];
if (v->storage_class & STCref)
continue;
Type *tv = v->type->baseElemOf();
Expand Down Expand Up @@ -277,9 +273,7 @@ FuncDeclaration *StructDeclaration::buildOpAssign(Scope *sc)
//printf("\tmemberwise copy\n");
for (size_t i = 0; i < fields.dim; i++)
{
Dsymbol *s = fields[i];
VarDeclaration *v = s->isVarDeclaration();
assert(v && v->isField());
VarDeclaration *v = fields[i];
// this.v = s.v;
AssignExp *ec = new AssignExp(loc,
new DotVarExp(loc, new ThisExp(loc), v, 0),
Expand Down Expand Up @@ -365,9 +359,7 @@ int StructDeclaration::needOpEquals()
*/
for (size_t i = 0; i < fields.dim; i++)
{
Dsymbol *s = fields[i];
VarDeclaration *v = s->isVarDeclaration();
assert(v && v->isField());
VarDeclaration *v = fields[i];
if (v->storage_class & STCref)
continue;
Type *tv = v->type->toBasetype();
Expand Down Expand Up @@ -774,9 +766,7 @@ FuncDeclaration *StructDeclaration::buildPostBlit(Scope *sc)
Expression *e = NULL;
for (size_t i = 0; i < fields.dim; i++)
{
Dsymbol *s = fields[i];
VarDeclaration *v = s->isVarDeclaration();
assert(v && v->isField());
VarDeclaration *v = fields[i];
if (v->storage_class & STCref)
continue;
Type *tv = v->type->toBasetype();
Expand Down Expand Up @@ -887,9 +877,7 @@ FuncDeclaration *AggregateDeclaration::buildDtor(Scope *sc)
Expression *e = NULL;
for (size_t i = 0; i < fields.dim; i++)
{
Dsymbol *s = fields[i];
VarDeclaration *v = s->isVarDeclaration();
assert(v && v->isField());
VarDeclaration *v = fields[i];
if (v->storage_class & STCref)
continue;
Type *tv = v->type->toBasetype();
Expand Down
6 changes: 2 additions & 4 deletions src/constfold.c
Expand Up @@ -1183,10 +1183,8 @@ Expression *Cast(Type *type, Type *to, Expression *e1)
assert(sd);
Expressions *elements = new Expressions;
for (size_t i = 0; i < sd->fields.dim; i++)
{ Dsymbol *s = sd->fields[i];
VarDeclaration *v = s->isVarDeclaration();
assert(v);

{
VarDeclaration *v = sd->fields[i];
Expression *exp = new IntegerExp(0);
exp = Cast(v->type, v->type, exp);
if (exp == EXP_CANT_INTERPRET)
Expand Down
51 changes: 28 additions & 23 deletions src/ctfeexpr.c
Expand Up @@ -55,7 +55,8 @@ VarDeclaration *ClassReferenceExp::getFieldAt(unsigned index)
ClassDeclaration *cd = originalClass();
unsigned fieldsSoFar = 0;
while (index - fieldsSoFar >= cd->fields.dim)
{ fieldsSoFar += cd->fields.dim;
{
fieldsSoFar += cd->fields.dim;
cd = cd->baseClass;
}
return cd->fields[index - fieldsSoFar];
Expand All @@ -67,15 +68,17 @@ int ClassReferenceExp::getFieldIndex(Type *fieldtype, unsigned fieldoffset)
ClassDeclaration *cd = originalClass();
unsigned fieldsSoFar = 0;
for (size_t j = 0; j < value->elements->dim; j++)
{ while (j - fieldsSoFar >= cd->fields.dim)
{ fieldsSoFar += cd->fields.dim;
{
while (j - fieldsSoFar >= cd->fields.dim)
{
fieldsSoFar += cd->fields.dim;
cd = cd->baseClass;
}
Dsymbol *s = cd->fields[j - fieldsSoFar];
VarDeclaration *v2 = s->isVarDeclaration();
VarDeclaration *v2 = cd->fields[j - fieldsSoFar];
if (fieldoffset == v2->offset &&
fieldtype->size() == v2->type->size())
{ return (int)(value->elements->dim - fieldsSoFar - cd->fields.dim + (j-fieldsSoFar));
{
return (int)(value->elements->dim - fieldsSoFar - cd->fields.dim + (j-fieldsSoFar));
}
}
return -1;
Expand All @@ -88,14 +91,16 @@ int ClassReferenceExp::findFieldIndexByName(VarDeclaration *v)
ClassDeclaration *cd = originalClass();
size_t fieldsSoFar = 0;
for (size_t j = 0; j < value->elements->dim; j++)
{ while (j - fieldsSoFar >= cd->fields.dim)
{ fieldsSoFar += cd->fields.dim;
{
while (j - fieldsSoFar >= cd->fields.dim)
{
fieldsSoFar += cd->fields.dim;
cd = cd->baseClass;
}
Dsymbol *s = cd->fields[j - fieldsSoFar];
VarDeclaration *v2 = s->isVarDeclaration();
VarDeclaration *v2 = cd->fields[j - fieldsSoFar];
if (v == v2)
{ return (int)(value->elements->dim - fieldsSoFar - cd->fields.dim + (j-fieldsSoFar));
{
return (int)(value->elements->dim - fieldsSoFar - cd->fields.dim + (j-fieldsSoFar));
}
}
return -1;
Expand Down Expand Up @@ -283,9 +288,7 @@ Expression *copyLiteral(Expression *e)
Expression *m = (*oldelems)[i];
// We need the struct definition to detect block assignment
AggregateDeclaration *sd = se->sd;
Dsymbol *s = sd->fields[i];
VarDeclaration *v = s->isVarDeclaration();
assert(v);
VarDeclaration *v = sd->fields[i];
// If it is a void assignment, use the default initializer
if (!m)
m = v->type->voidInitLiteral(v);
Expand Down Expand Up @@ -2191,38 +2194,40 @@ void showCtfeExpr(Expression *e, int level)
size_t fieldsSoFar = 0;
for (size_t i = 0; i < elements->dim; i++)
{ Expression *z = NULL;
Dsymbol *s = NULL;
VarDeclaration *v = NULL;
if (i > 15) {
printf("...(total %d elements)\n", (int)elements->dim);
return;
}
if (sd)
{ s = sd->fields[i];
{
v = sd->fields[i];
z = (*elements)[i];
}
else if (cd)
{ while (i - fieldsSoFar >= cd->fields.dim)
{ fieldsSoFar += cd->fields.dim;
{
while (i - fieldsSoFar >= cd->fields.dim)
{
fieldsSoFar += cd->fields.dim;
cd = cd->baseClass;
for (int j = level; j>0; --j) printf(" ");
printf(" BASE CLASS: %s\n", cd->toChars());
}
s = cd->fields[i - fieldsSoFar];
v = cd->fields[i - fieldsSoFar];
assert((elements->dim + i) >= (fieldsSoFar + cd->fields.dim));
size_t indx = (elements->dim - fieldsSoFar)- cd->fields.dim + i;
assert(indx < elements->dim);
z = (*elements)[indx];
}
if (!z) {
if (!z)
{
for (int j = level; j>0; --j) printf(" ");
printf(" void\n");
continue;
}

if (s)
if (v)
{
VarDeclaration *v = s->isVarDeclaration();
assert(v);
// If it is a void assignment, use the default initializer
if ((v->type->ty != z->type->ty) && v->type->ty == Tsarray)
{
Expand Down
3 changes: 2 additions & 1 deletion src/declaration.c
Expand Up @@ -45,7 +45,8 @@ void checkFrameAccess(Loc loc, Scope *sc, AggregateDeclaration *ad)
{
// Is it better moving this check to AggregateDeclaration:semantic?
for (size_t i = 0; i < ad->fields.dim; i++)
{ VarDeclaration *vd = ad->fields[i]->isVarDeclaration();
{
VarDeclaration *vd = ad->fields[i];
if (vd)
if (AggregateDeclaration *ad2 = isAggregate(vd->type))
if (ad2->isStructDeclaration())
Expand Down
12 changes: 3 additions & 9 deletions src/e2ir.c
Expand Up @@ -5128,9 +5128,7 @@ elem *StructLiteralExp::toElem(IRState *irs)
size_t offset = 0;
for (size_t i = 0; i < sd->fields.dim; i++)
{
Dsymbol *s = sd->fields[i];
VarDeclaration *v = s->isVarDeclaration();
assert(v);
VarDeclaration *v = sd->fields[i];

e = el_combine(e, fillHole(stmp, &offset, v->offset, sd->structsize));
size_t vend = v->offset + v->type->size();
Expand All @@ -5150,9 +5148,7 @@ elem *StructLiteralExp::toElem(IRState *irs)
if (!el)
continue;

Dsymbol *s = sd->fields[i];
VarDeclaration *v = s->isVarDeclaration();
assert(v);
VarDeclaration *v = sd->fields[i];
assert(!v->isThisDeclaration() || el->op == TOKnull);

elem *e1;
Expand Down Expand Up @@ -5209,9 +5205,7 @@ elem *StructLiteralExp::toElem(IRState *irs)
{
// Initialize the hidden 'this' pointer
assert(sd->fields.dim);
Dsymbol *s = sd->fields[sd->fields.dim - 1];
ThisDeclaration *v = s->isThisDeclaration();
assert(v);
ThisDeclaration *v = sd->fields[sd->fields.dim - 1]->isThisDeclaration();

elem *e1;
if (tybasic(stmp->Stype->Tty) == TYnptr)
Expand Down
4 changes: 1 addition & 3 deletions src/expression.c
Expand Up @@ -4813,9 +4813,7 @@ int StructLiteralExp::getFieldIndex(Type *type, unsigned offset)
{
for (size_t i = 0; i < sd->fields.dim; i++)
{
Dsymbol *s = sd->fields[i];
VarDeclaration *v = s->isVarDeclaration();
assert(v);
VarDeclaration *v = sd->fields[i];

if (offset == v->offset &&
type->size() == v->type->size())
Expand Down
4 changes: 2 additions & 2 deletions src/func.c
Expand Up @@ -1354,8 +1354,8 @@ void FuncDeclaration::semantic3(Scope *sc)
if (!(sc2->callSuper & CSXthis_ctor))
{
for (size_t i = 0; i < ad2->fields.dim; i++)
{ VarDeclaration *v = ad2->fields[i];

{
VarDeclaration *v = ad2->fields[i];
if (v->ctorinit == 0)
{
/* Current bugs in the flow analysis:
Expand Down
4 changes: 1 addition & 3 deletions src/interpret.c
Expand Up @@ -2942,9 +2942,7 @@ Expression *NewExp::interpret(InterState *istate, CtfeGoal goal)
fieldsSoFar -= c->fields.dim;
for (size_t i = 0; i < c->fields.dim; i++)
{
Dsymbol *s = c->fields[i];
VarDeclaration *v = s->isVarDeclaration();
assert(v);
VarDeclaration *v = c->fields[i];
Expression *m;
if (v->init)
{
Expand Down
8 changes: 3 additions & 5 deletions src/mtype.c
Expand Up @@ -8368,9 +8368,8 @@ bool TypeStruct::needsNested()

for (size_t i = 0; i < sym->fields.dim; i++)
{
Dsymbol *s = sym->fields[i];
VarDeclaration *vd = s->isVarDeclaration();
if (vd && !vd->isDataseg() && vd->type->needsNested())
VarDeclaration *v = sym->fields[i];
if (!v->isDataseg() && v->type->needsNested())
return true;
}
return false;
Expand Down Expand Up @@ -8419,8 +8418,7 @@ int TypeStruct::hasPointers()
sym->size(Loc()); // give error for forward references
for (size_t i = 0; i < s->fields.dim; i++)
{
Dsymbol *sm = s->fields[i];
Declaration *d = sm->isDeclaration();
Declaration *d = s->fields[i];
if (d->storage_class & STCref || d->hasPointers())
return true;
}
Expand Down

0 comments on commit 6164bbd

Please sign in to comment.