Skip to content

Commit

Permalink
Merge pull request #1421 from AndrejMitrovic/Fix9237
Browse files Browse the repository at this point in the history
Issue 9237 - Add isPOD trait
  • Loading branch information
WalterBright committed Dec 29, 2012
2 parents e32197e + d53a006 commit dad4ab1
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/idgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ Msgtable msgtable[] =
{ "isArithmetic" },
{ "isAssociativeArray" },
{ "isFinalClass" },
{ "isPOD" },
{ "isFloating" },
{ "isIntegral" },
{ "isScalar" },
Expand Down
22 changes: 22 additions & 0 deletions src/traits.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ Expression *TraitsExp::semantic(Scope *sc)
{
ISTYPE(t->toBasetype()->ty == Tclass && ((TypeClass *)t->toBasetype())->sym->storage_class & STCfinal)
}
else if (ident == Id::isPOD)
{
if (dim != 1)
goto Ldimerror;
Object *o = (*args)[0];
Type *t = isType(o);
StructDeclaration *sd;
if (!t)
{
error("type expected as second argument of __traits %s instead of %s", ident->toChars(), o->toChars());
goto Lfalse;
}
if (t->toBasetype()->ty == Tstruct
&& ((sd = (StructDeclaration *)(((TypeStruct *)t->toBasetype())->sym)) != NULL))
{
if (sd->isPOD())
goto Ltrue;
else
goto Lfalse;
}
goto Ltrue;
}
else if (ident == Id::isAbstractFunction)
{
FuncDeclaration *f;
Expand Down
64 changes: 63 additions & 1 deletion test/runnable/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ string toString23(E)(E value) if (is(E == enum)) {
return null;
}

enum OddWord { acini, alembicated, prolegomena, aprosexia }
enum OddWord { acini, alembicated, prolegomena, aprosexia }

void test23()
{
Expand Down Expand Up @@ -894,6 +894,68 @@ struct S9091

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

struct CtorS_9237 { this(int x) { } }
struct DtorS_9237 { ~this() { } }
struct PostblitS_9237 { this(this) { } }

struct NonPOD1_9237
{
CtorS_9237 field; // nonPOD -> ng
}

struct NonPOD2_9237
{
CtorS_9237[2] field; // static array of nonPOD -> ng
}

struct POD1_9237
{
CtorS_9237* field; // pointer to nonPOD -> ok
}

struct POD2_9237
{
CtorS_9237[] field; // dynamic array of nonPOD -> ok
}

struct POD3_9237
{
int x = 123;
}

class C_9273 { }

void test9237()
{
int x;
struct NS_9237 // acceses .outer -> nested
{
void foo() { x++; }
}

struct NonNS_9237 { } // doesn't access .outer -> non-nested
static struct StatNS_9237 { } // can't access .outer -> non-nested

static assert(!__traits(isPOD, NS_9237));
static assert(__traits(isPOD, NonNS_9237));
static assert(__traits(isPOD, StatNS_9237));
static assert(!__traits(isPOD, CtorS_9237));
static assert(!__traits(isPOD, DtorS_9237));
static assert(!__traits(isPOD, PostblitS_9237));
static assert(!__traits(isPOD, NonPOD1_9237));
static assert(!__traits(isPOD, NonPOD2_9237));
static assert(__traits(isPOD, POD1_9237));
static assert(__traits(isPOD, POD2_9237));
static assert(__traits(isPOD, POD3_9237));

// non-structs are POD types
static assert(__traits(isPOD, C_9273));
static assert(__traits(isPOD, int));
static assert(__traits(isPOD, int*));
static assert(__traits(isPOD, int[]));
static assert(!__traits(compiles, __traits(isPOD, 123) ));
}

int main()
{
test1();
Expand Down

0 comments on commit dad4ab1

Please sign in to comment.