gcc/c-family/
[official-gcc.git] / gcc / testsuite / obj-c++.dg / encode-6.mm
blob9a1e65f92766ae50d422b759bbe2b6690b63e1bf
1 /* Encoding tests for ObjC class layouts.  */
2 /* Contributed by Ziemowit Laski <zlaski@apple.com>.  */
3 /* { dg-options "" } */
4 /* { dg-do run } */
5 /* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
6 #include "../objc-obj-c++-shared/TestsuiteObject.m"
7 #include "../objc-obj-c++-shared/runtime.h"
9 #include <stdlib.h>
10 #include <string.h>
12 #define CHECK_IF(expr) if(!(expr)) abort()
14 @class Int1, Int2;
15 struct Nested;
17 struct Innermost {
18   unsigned char a, b;
19   struct Nested *encl;
22 struct Nested {
23   float a, b;
24   Int1 *next;
25   struct Innermost innermost;
28 @interface Int1: TestsuiteObject {
29   signed char a, b;
30   Int2 *int2;
31   struct Nested nested;
33 @end
35 @interface Int2: Int1 {
36   struct Innermost *innermost;
37   Int1 *base;
39 @end
41 @implementation Int1
42 @end
44 @implementation Int2
45 @end
47 #if defined(__NEXT_RUNTIME__) && !defined(NEXT_OBJC_USE_NEW_INTERFACE)
48 struct objc_ivar *ivar;
49 #else
50 Ivar *ivar;
51 #endif
53 static void check_ivar(const char *name, const char *type) {
54 #if defined(__NEXT_RUNTIME__) && !defined(NEXT_OBJC_USE_NEW_INTERFACE)
55   CHECK_IF(!strcmp(ivar->ivar_name, name));
56   CHECK_IF(!strcmp(ivar->ivar_type, type));
57 #else
58   CHECK_IF(!strcmp(ivar_getName(*ivar), name));
59   CHECK_IF(!strcmp(ivar_getTypeEncoding(*ivar), type));
60 #endif
61   ivar++;
64 int main(void) {
65 #if defined(__NEXT_RUNTIME__) && !defined(NEXT_OBJC_USE_NEW_INTERFACE)
66   ivar = ((Class)objc_getClass("Int1"))->ivars->ivar_list;
67 #else
68   ivar = class_copyIvarList ((Class)objc_getClass("Int1"), NULL);
69 #endif
70   check_ivar("a", "c");
71   check_ivar("b", "c");
72   check_ivar("int2", "@\"Int2\"");
73   check_ivar("nested", 
74     "{Nested=\"a\"f\"b\"f\"next\"@\"Int1\"\"innermost\"{Innermost=\"a\"C\"b\"C\"encl\"^{Nested}}}");
75     
76 #if defined(__NEXT_RUNTIME__) && !defined(NEXT_OBJC_USE_NEW_INTERFACE)
77   ivar = ((Class)objc_getClass("Int2"))->ivars->ivar_list;
78 #else
79   ivar = class_copyIvarList ((Class)objc_getClass("Int2"), NULL);
80 #endif
81   check_ivar("innermost", "^{Innermost=CC^{Nested}}");
82   check_ivar("base", "@\"Int1\"");
83   
84   return 0;