OSDN Git Service

patch for PR rtl-optimization/25130
[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 #include <stdio.h>
35 #include <stdlib.h>
36
37 #define CHECK_IF(expr) if(!(expr)) abort()
38
39 @interface Foo: Object
40 typedef struct { float x, y; } XXPoint;
41 typedef struct { float width, height; } XXSize;
42 typedef struct _XXRect { XXPoint origin; XXSize size; } XXRect;
43 -(id)setRect:(XXRect)r withInt:(int)i;
44 -(void) char:(signed char)c float:(float)f double:(double)d long:(long)l;
45 @end
46
47 XXRect my_rect;
48 unsigned offs1, offs2, offs3, offs4, offs5, offs6, offs7;
49
50 @implementation Foo
51 -(id)setRect:(XXRect)r withInt:(int)i {
52   unsigned offs = sizeof(self);
53   CHECK_IF(offs == offs3);
54   offs += sizeof(_cmd);
55   CHECK_IF(offs == offs4);
56   offs += sizeof(r);
57   CHECK_IF(offs == offs5);
58   offs += sizeof(i); 
59   CHECK_IF(offs == offs1); 
60   return nil; 
61 }
62 -(void) char:(signed char)c float:(float)f double:(double)d long:(long)l {
63   unsigned offs = sizeof(self);
64   CHECK_IF(offs == offs3);
65   offs += sizeof(_cmd);
66   CHECK_IF(offs == offs4);
67   offs += sizeof((int)c);
68   CHECK_IF(offs == offs5);
69   offs += sizeof(f);
70   CHECK_IF(offs == offs6);
71   offs += sizeof(d);
72   CHECK_IF(offs == offs7);
73   offs += sizeof(l);
74   CHECK_IF(offs == offs1);
75 }
76 @end
77
78
79 int main(void) {
80   Foo *foo = [[Foo alloc] init];
81   Class fooClass = OBJC_GETCLASS("Foo");
82   METHOD meth;
83   const char *string;
84
85   meth = CLASS_GETINSTANCEMETHOD(fooClass, @selector(setRect:withInt:));
86   offs2 = 9999;
87   sscanf(meth->method_types, "@%u@%u:%u{_XXRect={?=ff}{?=ff}}%ui%u", &offs1, &offs2, &offs3,
88       &offs4, &offs5);
89   CHECK_IF(!offs2);
90   [foo setRect:my_rect withInt:123];
91
92   meth = CLASS_GETINSTANCEMETHOD(fooClass, @selector(char:float:double:long:));
93   offs2 = 9999;
94   if (sizeof (long) == 8)
95     string = "v%u@%u:%uc%uf%ud%uq%u";
96   else
97     string = "v%u@%u:%uc%uf%ud%ul%u";
98   sscanf(meth->method_types, string, &offs1, &offs2, &offs3,  
99          &offs4, &offs5, &offs6, &offs7);
100   CHECK_IF(!offs2);
101   [foo char:'c' float:2.3 double:3.5 long:2345L];
102
103   return 0;
104 }