OSDN Git Service

* objc.dg/stabs-1.m (dg-final): Change regexp pattern for hppa SOM.
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / objc.dg / encode-2.m
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 #include <objc/objc.h>
20 #include <objc/Object.h>
21
22 #ifdef __NEXT_RUNTIME__
23 #define METHOD Method
24 #define OBJC_GETCLASS objc_getClass
25 #define CLASS_GETINSTANCEMETHOD class_getInstanceMethod
26 #else
27 #include <objc/objc-api.h>
28 #define METHOD Method_t
29 #define OBJC_GETCLASS objc_get_class
30 #define CLASS_GETINSTANCEMETHOD class_get_instance_method
31 #endif
32
33 extern int sscanf(const char *str, const char *format, ...);
34 extern void abort(void);
35 #define CHECK_IF(expr) if(!(expr)) abort()
36
37 @interface Foo: Object
38 typedef struct { float x, y; } XXPoint;
39 typedef struct { float width, height; } XXSize;
40 typedef struct _XXRect { XXPoint origin; XXSize size; } XXRect;
41 -(id)setRect:(XXRect)r withInt:(int)i;
42 -(void) char:(signed char)c float:(float)f double:(double)d long:(long)l;
43 @end
44
45 XXRect my_rect;
46 unsigned offs1, offs2, offs3, offs4, offs5, offs6, offs7;
47
48 @implementation Foo
49 -(id)setRect:(XXRect)r withInt:(int)i {
50   unsigned offs = sizeof(self);
51   CHECK_IF(offs == offs3);
52   offs += sizeof(_cmd);
53   CHECK_IF(offs == offs4);
54   offs += sizeof(r);
55   CHECK_IF(offs == offs5);
56   offs += sizeof(i); 
57   CHECK_IF(offs == offs1); 
58   return nil; 
59 }
60 -(void) char:(signed char)c float:(float)f double:(double)d long:(long)l {
61   unsigned offs = sizeof(self);
62   CHECK_IF(offs == offs3);
63   offs += sizeof(_cmd);
64   CHECK_IF(offs == offs4);
65   offs += sizeof((int)c);
66   CHECK_IF(offs == offs5);
67   offs += sizeof(f);
68   CHECK_IF(offs == offs6);
69   offs += sizeof(d);
70   CHECK_IF(offs == offs7);
71   offs += sizeof(l);
72   CHECK_IF(offs == offs1);
73 }
74 @end
75
76
77 int main(void) {
78   Foo *foo = [[Foo alloc] init];
79   Class fooClass = OBJC_GETCLASS("Foo");
80   METHOD meth;
81   const char *string;
82
83   meth = CLASS_GETINSTANCEMETHOD(fooClass, @selector(setRect:withInt:));
84   offs2 = 9999;
85   sscanf(meth->method_types, "@%u@%u:%u{_XXRect={?=ff}{?=ff}}%ui%u", &offs1, &offs2, &offs3,
86       &offs4, &offs5);
87   CHECK_IF(!offs2);
88   [foo setRect:my_rect withInt:123];
89
90   meth = CLASS_GETINSTANCEMETHOD(fooClass, @selector(char:float:double:long:));
91   offs2 = 9999;
92   if (sizeof (long) == 8)
93     string = "v%u@%u:%uc%uf%ud%uq%u";
94   else
95     string = "v%u@%u:%uc%uf%ud%ul%u";
96   sscanf(meth->method_types, string, &offs1, &offs2, &offs3,  
97          &offs4, &offs5, &offs6, &offs7);
98   CHECK_IF(!offs2);
99   [foo char:'c' float:2.3 double:3.5 long:2345L];
100
101   return 0;
102 }