5d6ef577a9094b8e59d5288f29cbed932fe35445
[gcc.git] / gcc / testsuite / objc / execute / bf-common.h
1 #include <stdlib.h>
2 #include "../../objc-obj-c++-shared/next-mapping.h"
3 #ifndef __NEXT_RUNTIME__
4 #include <objc/encoding.h>
5 #endif
6
7 void print_ivars (Class class)
8 {
9 struct objc_ivar_list* ivars = class->ivars;
10 int i;
11
12 for (i = 0; i < ivars->ivar_count; i++) {
13 struct objc_ivar *ivar = &(ivars->ivar_list[i]);
14 printf ("ivar '%s', type '%s', offset %d\n",
15 ivar->ivar_name, ivar->ivar_type, ivar->ivar_offset);
16 }
17 }
18
19 void compare_structures (Class class, const char* type)
20 {
21 struct objc_struct_layout layout;
22 struct objc_ivar_list* ivars = class->ivars;
23 int i = 0;
24 int position;
25
26 objc_layout_structure (type, &layout);
27
28 while (objc_layout_structure_next_member (&layout))
29 {
30 struct objc_ivar *ivar;
31 const char *ivar_type;
32
33 if (i > ivars->ivar_count)
34 {
35 printf ("too many ivars in type %s, layout = %s\n",
36 type, layout.type);
37 exit (1);
38 }
39
40 ivar = &(ivars->ivar_list[i]);
41 objc_layout_structure_get_info (&layout, &position, NULL, &ivar_type);
42 printf ("real ivar '%s' offset %d\n",
43 ivar->ivar_name, ivar->ivar_offset);
44 printf ("computed type '%s' offset %d\n", ivar_type, position);
45 if (position != ivar->ivar_offset)
46 {
47 printf ("offset %d and computed position %d don't match on ivar '%s'"
48 " (i = %d)\n",
49 ivar->ivar_offset, position, ivar->ivar_name, i);
50 exit (1);
51 }
52 i++;
53 }
54
55 printf ("%d ivars checked\n", i);
56 }
57
58 int main ()
59 {
60 struct class_vars
61 {
62 @defs (MyObject);
63 };
64 int size1, size2;
65 Class class = objc_get_class ("MyObject");
66
67 printf ("type = %s\n", @encode (struct class_vars));
68 print_ivars (class);
69
70 compare_structures (class, @encode(struct class_vars));
71 if ((size1 = objc_sizeof_type (@encode(struct class_vars)))
72 != (size2 = sizeof (struct class_vars)))
73 {
74 printf ("sizes don't match (computed %d, exact %d)\n", size1, size2);
75 abort ();
76 }
77
78 exit (0);
79 }