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/Object1.h"
20 #include "../objc-obj-c++-shared/next-mapping.h"
22 #ifdef __NEXT_RUNTIME__
25 #include <objc/objc-api.h>
26 #define METHOD Method_t
27 #define method_get_types(M) (M)->method_types
33 #define CHECK_IF(expr) if(!(expr)) abort()
35 @interface Foo: Object
36 typedef struct { float x, y; } XXPoint;
37 typedef struct { float width, height; } XXSize;
38 typedef struct _XXRect { XXPoint origin; XXSize size; } XXRect;
39 -(id)setRect:(XXRect)r withInt:(int)i;
40 -(void) char:(signed char)c float:(float)f double:(double)d long:(long)l;
44 unsigned offs1, offs2, offs3, offs4, offs5, offs6, offs7;
47 -(id)setRect:(XXRect)r withInt:(int)i {
48 unsigned offs = sizeof(self);
49 CHECK_IF(offs == offs3);
51 CHECK_IF(offs == offs4);
53 CHECK_IF(offs == offs5);
55 CHECK_IF(offs == offs1);
58 -(void) char:(signed char)c float:(float)f double:(double)d long:(long)l {
59 unsigned offs = sizeof(self);
60 CHECK_IF(offs == offs3);
62 CHECK_IF(offs == offs4);
63 offs += sizeof((int)c);
64 CHECK_IF(offs == offs5);
66 CHECK_IF(offs == offs6);
68 CHECK_IF(offs == offs7);
70 CHECK_IF(offs == offs1);
76 Foo *foo = [[Foo alloc] init];
77 Class fooClass = objc_get_class("Foo");
81 meth = class_get_instance_method(fooClass, @selector(setRect:withInt:));
83 sscanf(method_get_types(meth), "@%u@%u:%u{_XXRect={?=ff}{?=ff}}%ui%u", &offs1, &offs2, &offs3,
86 [foo setRect:my_rect withInt:123];
88 meth = class_get_instance_method(fooClass, @selector(char:float:double:long:));
90 if (sizeof (long) == 8)
91 string = "v%u@%u:%uc%uf%ud%uq%u";
93 string = "v%u@%u:%uc%uf%ud%ul%u";
94 sscanf(method_get_types(meth), string, &offs1, &offs2, &offs3,
95 &offs4, &offs5, &offs6, &offs7);
97 [foo char:'c' float:2.3 double:3.5 long:2345L];
101 #include "../objc-obj-c++-shared/Object1-implementation.h"