1 /* Test Objective-C method encodings. */
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
8 - the offset of the k-1-st paramter
9 - the (void *)-promoted size of the k-1-st parameter.
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. */
16 /* Contributed by Ziemowit Laski <zlaski@apple.com>. */
18 /* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
19 #include "../objc-obj-c++-shared/TestsuiteObject.m"
20 #include "../objc-obj-c++-shared/runtime.h"
25 #define CHECK_IF(expr) if(!(expr)) abort()
27 @interface Foo: TestsuiteObject
28 typedef struct { float x, y; } XXPoint;
29 typedef struct { float width, height; } XXSize;
30 typedef struct _XXRect { XXPoint origin; XXSize size; } XXRect;
31 -(id)setRect:(XXRect)r withInt:(int)i;
32 -(void) char:(signed char)c float:(float)f double:(double)d long:(long)l;
36 unsigned offs1, offs2, offs3, offs4, offs5, offs6, offs7;
39 -(id)setRect:(XXRect)r withInt:(int)i {
40 unsigned offs = sizeof(self);
41 CHECK_IF(offs == offs3);
43 CHECK_IF(offs == offs4);
45 CHECK_IF(offs == offs5);
47 CHECK_IF(offs == offs1);
50 -(void) char:(signed char)c float:(float)f double:(double)d long:(long)l {
51 unsigned offs = sizeof(self);
52 CHECK_IF(offs == offs3);
54 CHECK_IF(offs == offs4);
55 offs += sizeof((int)c);
56 CHECK_IF(offs == offs5);
58 CHECK_IF(offs == offs6);
60 CHECK_IF(offs == offs7);
62 CHECK_IF(offs == offs1);
68 Foo *foo = [[Foo alloc] init];
69 Class fooClass = objc_getClass("Foo");
73 meth = class_getInstanceMethod(fooClass, @selector(setRect:withInt:));
75 sscanf(method_getTypeEncoding(meth), "@%u@%u:%u{_XXRect={?=ff}{?=ff}}%ui%u", &offs1, &offs2, &offs3,
78 [foo setRect:my_rect withInt:123];
80 meth = class_getInstanceMethod(fooClass, @selector(char:float:double:long:));
82 if (sizeof (long) == 8)
83 string = "v%u@%u:%uc%uf%ud%uq%u";
85 string = "v%u@%u:%uc%uf%ud%ul%u";
86 sscanf(method_getTypeEncoding(meth), string, &offs1, &offs2, &offs3,
87 &offs4, &offs5, &offs6, &offs7);
89 [foo char:'c' float:2.3 double:3.5 long:2345L];