Skip to content

Commit

Permalink
Merge pull request #3534 from 9rnsr/refactor_traits
Browse files Browse the repository at this point in the history
Fix warning and small refactoring for __traits(getFunctionAttributes)
  • Loading branch information
AndrejMitrovic committed May 9, 2014
2 parents 540fe41 + 1ec07dd commit 647e7f1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 33 deletions.
8 changes: 3 additions & 5 deletions src/mtype.c
Expand Up @@ -1615,19 +1615,17 @@ char *Type::modToChars()
void* for the work param and a string representation of the attribute. */
int Type::modifiersApply(void *param, int (*fp)(void *, const char *))
{
int res = 0;
static unsigned char modsArr[] = { MODconst, MODimmutable, MODwild, MODshared };

static unsigned modsArr[] = { MODconst, MODimmutable, MODwild, MODshared };
for (size_t idx = 0; idx < 4; ++idx)
{
if (mod & modsArr[idx])
{
if (res = fp(param, MODtoChars(modsArr[idx])))
if (int res = fp(param, MODtoChars(modsArr[idx])))
return res;
}
}

return res;
return 0;
}

/************************************
Expand Down
39 changes: 12 additions & 27 deletions src/traits.c
Expand Up @@ -697,34 +697,23 @@ Expression *semanticTraits(TraitsExp *e, Scope *sc)
Dsymbol *s = getDsymbol(o);
Type *t = isType(o);
TypeFunction *tf = NULL;
FuncDeclaration *fd = NULL;
Type *type = NULL;

if (s)
{
if (FuncDeclaration *sfd = s->isFuncDeclaration())
{
fd = sfd;
type = fd->type;
}
else if (VarDeclaration *vd = s->isVarDeclaration())
type = vd->type;
if (FuncDeclaration *f = s->isFuncDeclaration())
t = f->type;
else if (VarDeclaration *v = s->isVarDeclaration())
t = v->type;
}
else if (t)
if (t)
{
type = t;
if (t->ty == Tfunction)
tf = (TypeFunction *)t;
else if (t->ty == Tdelegate)
tf = (TypeFunction *)t->nextOf();
else if (t->ty == Tpointer && t->nextOf()->ty == Tfunction)
tf = (TypeFunction *)t->nextOf();
}

if (type)
{
if (type->ty == Tfunction)
tf = (TypeFunction *)type;
else if (type->ty == Tdelegate)
tf = (TypeFunction *)type->nextOf();
else if (type->ty == Tpointer && type->nextOf()->ty == Tfunction)
tf = (TypeFunction *)type->nextOf();
}

if (!tf)
{
e->error("first argument is not a function");
Expand All @@ -736,11 +725,7 @@ Expression *semanticTraits(TraitsExp *e, Scope *sc)
PushAttributes pa;
pa.mods = mods;

if (fd)
fd->type->modifiersApply(&pa, &PushAttributes::fp);
else if (tf)
tf->modifiersApply(&pa, &PushAttributes::fp);

tf->modifiersApply(&pa, &PushAttributes::fp);
tf->attributesApply(&pa, &PushAttributes::fp, TRUSTformatSystem);

TupleExp *tup = new TupleExp(e->loc, mods);
Expand Down
1 change: 0 additions & 1 deletion test/runnable/traits.d
Expand Up @@ -1303,7 +1303,6 @@ void test_getUnitTests ()

void test_getFunctionAttributes()
{
enum tupleLength(T...) = T.length;
alias tuple(T...) = T;

struct S
Expand Down

0 comments on commit 647e7f1

Please sign in to comment.