From 6ee244bc45b828b138aeaaf91e0005f4f4a50ae8 Mon Sep 17 00:00:00 2001 From: Andrej Mitrovic Date: Mon, 10 Dec 2012 18:34:15 +0100 Subject: [PATCH] Fixes Issue 9136 - Add isNested trait for aggregates and functions. --- src/idgen.c | 1 + src/traits.c | 28 ++++++++++++++++++++++++++++ test/runnable/traits.d | 30 ++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/src/idgen.c b/src/idgen.c index 7b9896a4e6e7..c0c07d12e1d1 100644 --- a/src/idgen.c +++ b/src/idgen.c @@ -307,6 +307,7 @@ Msgtable msgtable[] = { "isAssociativeArray" }, { "isFinalClass" }, { "isPOD" }, + { "isNested" }, { "isFloating" }, { "isIntegral" }, { "isScalar" }, diff --git a/src/traits.c b/src/traits.c index 7115819526ea..4a8c821edfc2 100644 --- a/src/traits.c +++ b/src/traits.c @@ -168,6 +168,34 @@ Expression *TraitsExp::semantic(Scope *sc) } goto Ltrue; } + else if (ident == Id::isNested) + { + if (dim != 1) + goto Ldimerror; + Object *o = (*args)[0]; + Dsymbol *s = getDsymbol(o); + AggregateDeclaration *a; + FuncDeclaration *f; + + if (!s) { } + else if ((a = s->isAggregateDeclaration()) != NULL) + { + if (a->isnested) + goto Ltrue; + else + goto Lfalse; + } + else if ((f = s->isFuncDeclaration()) != NULL) + { + if (f->isNested()) + goto Ltrue; + else + goto Lfalse; + } + + error("aggregate or function expected instead of '%s'", o->toChars()); + goto Lfalse; + } else if (ident == Id::isAbstractFunction) { FuncDeclaration *f; diff --git a/test/runnable/traits.d b/test/runnable/traits.d index ccf5f0adb69d..9712b37fcbdf 100644 --- a/test/runnable/traits.d +++ b/test/runnable/traits.d @@ -1043,6 +1043,35 @@ void test9552() /*************************************************************/ +void test9136() +{ + int x; + struct S1 { void f() { x++; } } + struct U1 { void f() { x++; } } + static struct S2 { } + static struct S3 { S1 s; } + static struct U2 { } + void f1() { x++; } + static void f2() { } + + static assert(__traits(isNested, S1)); + static assert(__traits(isNested, U1)); + static assert(!__traits(isNested, S2)); + static assert(!__traits(isNested, S3)); + static assert(!__traits(isNested, U2)); + static assert(!__traits(compiles, __traits(isNested, int) )); + static assert(!__traits(compiles, __traits(isNested, f1, f2) )); + static assert(__traits(isNested, f1)); + static assert(!__traits(isNested, f2)); + + static class A { static class SC { } class NC { } } + static assert(!__traits(isNested, A)); + static assert(!__traits(isNested, A.SC)); + static assert(__traits(isNested, A.NC)); +} + +/********************************************************/ + int main() { test1(); @@ -1075,6 +1104,7 @@ int main() test5978(); test7408(); test9552(); + test9136(); writeln("Success"); return 0;