Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1421 from AndrejMitrovic/Fix9237

Issue 9237 - Add isPOD trait
  • Loading branch information...
commit dad4ab12a9fe1f846c70640c35d8293678f77bd1 2 parents e32197e + d53a006
@WalterBright WalterBright authored
Showing with 86 additions and 1 deletion.
  1. +1 −0  src/idgen.c
  2. +22 −0 src/traits.c
  3. +63 −1 test/runnable/traits.d
View
1  src/idgen.c
@@ -308,6 +308,7 @@ Msgtable msgtable[] =
{ "isArithmetic" },
{ "isAssociativeArray" },
{ "isFinalClass" },
+ { "isPOD" },
{ "isFloating" },
{ "isIntegral" },
{ "isScalar" },
View
22 src/traits.c
@@ -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;
View
64 test/runnable/traits.d
@@ -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()
{
@@ -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();
Please sign in to comment.
Something went wrong with that request. Please try again.