aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/gcc/testsuite/objc/execute/bf-common.h
blob: 43b0d32b7d6a9509a6928af48955282e7367e0b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <stdio.h>
#include <stdlib.h>

#include "../../objc-obj-c++-shared/runtime.h"

/* The following header, together with the implementation included below,
   emulate functionality provided by the GNU runtime but not available from
   the NeXT runtime.  */
#include "../../objc-obj-c++-shared/objc-test-suite-next-encode-assist.h"

#if defined(__NEXT_RUNTIME__) && !defined(NEXT_OBJC_USE_NEW_INTERFACE)
void print_ivars (Class class)
{
  struct objc_ivar_list* ivars = class->ivars;
  int i;

  for (i = 0; i < ivars->ivar_count; i++) {
    struct objc_ivar *ivar = &(ivars->ivar_list[i]);
    printf ("ivar '%s', type '%s', offset %d\n",
	    ivar->ivar_name, ivar->ivar_type, ivar->ivar_offset);
  }
}

void compare_structures (Class class, const char* type)
{
  struct objc_struct_layout layout;
  struct objc_ivar_list* ivars = class->ivars;
  int i = 0;
  int position;

  objc_layout_structure (type, &layout);

  while (objc_layout_structure_next_member (&layout))
    {
      struct objc_ivar *ivar;
      const char *ivar_type;

      if (i > ivars->ivar_count)
        {
          printf ("too many ivars in type %s, layout = %s\n",
                  type, layout.type);
          exit (1);
        }

      ivar = &(ivars->ivar_list[i]);
      objc_layout_structure_get_info (&layout, &position, NULL, &ivar_type);
      printf ("real ivar '%s' offset %d\n",
              ivar->ivar_name, ivar->ivar_offset);
      printf ("computed type '%s' offset %d\n", ivar_type, position);
      if (position != ivar->ivar_offset)
        {
          printf ("offset %d and computed position %d don't match on ivar '%s'"
                  " (i = %d)\n",
                  ivar->ivar_offset, position, ivar->ivar_name, i);
          exit (1);
        }
      i++;
    }
  
  printf ("%d ivars checked\n", i);
}
#else
void print_ivars (Class class)
{
  unsigned int count, i;
  Ivar *list = class_copyIvarList (class, &count);

  for (i = 0; i < count; i++) {
    printf ("ivar '%s', type '%s', offset %ud\n",
	    ivar_getName (list[i]),
	    ivar_getTypeEncoding (list[i]),
	    (unsigned int)ivar_getOffset (list[i]));
  }
}

void compare_structures (Class class, const char* type)
{
  struct objc_struct_layout layout;
  unsigned int count;
  Ivar *list = class_copyIvarList (class, &count);
  int i = 0;
  int position;

  objc_layout_structure (type, &layout);

  while (objc_layout_structure_next_member (&layout))
    {
      const char *ivar_type;

      if (i > count)
        {
          printf ("too many ivars in type %s, layout = %s\n",
                  type, layout.type);
          exit (1);
        }

      objc_layout_structure_get_info (&layout, &position, NULL, &ivar_type);
      printf ("real ivar '%s' offset %ud\n",
              ivar_getName (list[i]), (unsigned int)ivar_getOffset (list[i]));
      printf ("computed type '%s' offset %d\n", ivar_type, position);
      if ((unsigned int)position != (unsigned int)ivar_getOffset (list[i]))
        {
          printf ("offset %ud and computed position %d don't match on ivar '%s'"
                  " (i = %d)\n",
                  (unsigned int)ivar_getOffset (list[i]), position, ivar_getName (list[i]), i);
          exit (1);
        }
      i++;
    }
  
  printf ("%d ivars checked\n", i);
}
#endif

int main ()
{
  struct class_vars
    {
      @defs (MyObject);
    };
  int size1, size2;
  Class class = objc_getClass ("MyObject");
  printf ("type = %s\n", @encode (struct class_vars));
  print_ivars (class);

  compare_structures (class, @encode(struct class_vars));
  if ((size1 = objc_sizeof_type (@encode(struct class_vars)))
      != (size2 = sizeof (struct class_vars)))
    {
      printf ("sizes don't match (computed %d, exact %d)\n", size1, size2);
      abort ();
    }
  
  return 0;
}
#include "../../objc-obj-c++-shared/objc-test-suite-next-encode-assist-impl.h"