OSDN Git Service

* obj-c++.dg/bitfield-[1-5].mm: New.
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / obj-c++.dg / encode-4.mm
1 /* Test Objective-C method encodings. */
2
3 /* The _encoded_ parameter offsets for Objective-C methods are 
4    computed inductively as follows:
5     - The first paramter (self) has offset 0;
6     - The k-th parameter (k > 1) has offset equal to the
7       sum of:
8         - the offset of the k-1-st paramter
9         - the (void *)-promoted size of the k-1-st parameter.
10
11    Note that the encoded offsets need not correspond
12    to the actual placement of parameters (relative to 'self')
13    on the stack!  Your target's ABI may have very different
14    opinions on the matter.  */
15
16 /* Contributed by Ziemowit Laski <zlaski@apple.com>.  */
17 /* { dg-do run } */
18
19
20 #include <objc/objc.h>
21 #include <objc/Object.h>
22
23 #ifdef __NEXT_RUNTIME__
24 #define METHOD Method
25 #define OBJC_GETCLASS objc_getClass
26 #define CLASS_GETINSTANCEMETHOD class_getInstanceMethod
27 #else
28 #include <objc/objc-api.h>
29 #define METHOD Method_t
30 #define OBJC_GETCLASS objc_get_class
31 #define CLASS_GETINSTANCEMETHOD class_get_instance_method
32 #endif
33
34 extern "C" {
35   extern int sscanf(const char *str, const char *format, ...);
36   extern void abort(void);
37 }
38 #define CHECK_IF(expr) if(!(expr)) abort()
39
40 @interface Foo: Object
41 typedef struct { float x, y; } XXPoint;
42 typedef struct { float width, height; } XXSize;
43 typedef struct _XXRect { XXPoint origin; XXSize size; } XXRect;
44 -(id)setRect:(XXRect)r withInt:(int)i;
45 -(void) char:(signed char)c float:(float)f double:(double)d long:(long)l;
46 @end
47
48 XXRect my_rect;
49 unsigned offs1, offs2, offs3, offs4, offs5, offs6, offs7;
50
51 @implementation Foo
52 -(id)setRect:(XXRect)r withInt:(int)i {
53   unsigned offs = sizeof(self);
54   CHECK_IF(offs == offs3);
55   offs += sizeof(_cmd);
56   CHECK_IF(offs == offs4);
57   offs += sizeof(r);
58   CHECK_IF(offs == offs5);
59   offs += sizeof(i); 
60   CHECK_IF(offs == offs1); 
61   return nil; 
62 }
63 -(void) char:(signed char)c float:(float)f double:(double)d long:(long)l {
64   unsigned offs = sizeof(self);
65   CHECK_IF(offs == offs3);
66   offs += sizeof(_cmd);
67   CHECK_IF(offs == offs4);
68   offs += sizeof((int)c);
69   CHECK_IF(offs == offs5);
70   offs += sizeof(f);
71   CHECK_IF(offs == offs6);
72   offs += sizeof(d);
73   CHECK_IF(offs == offs7);
74   offs += sizeof(l);
75   CHECK_IF(offs == offs1);
76 }
77 @end
78
79
80 int main(void) {
81   Foo *foo = [[Foo alloc] init];
82   Class fooClass = OBJC_GETCLASS("Foo");
83   METHOD meth;
84   const char *string;
85
86   meth = CLASS_GETINSTANCEMETHOD(fooClass, @selector(setRect:withInt:));
87   offs2 = 9999;
88   sscanf(meth->method_types, "@%u@%u:%u{_XXRect={?=ff}{?=ff}}%ui%u", &offs1, &offs2, &offs3,
89       &offs4, &offs5);
90   CHECK_IF(!offs2);
91   [foo setRect:my_rect withInt:123];
92
93   meth = CLASS_GETINSTANCEMETHOD(fooClass, @selector(char:float:double:long:));
94   offs2 = 9999;
95   if (sizeof (long) == 8)
96     string = "v%u@%u:%uc%uf%ud%uq%u";
97   else
98     string = "v%u@%u:%uc%uf%ud%ul%u";
99   sscanf(meth->method_types, string, &offs1, &offs2, &offs3,  
100          &offs4, &offs5, &offs6, &offs7);
101   CHECK_IF(!offs2);
102   [foo char:'c' float:2.3 double:3.5 long:2345L];
103
104   return 0;
105 }