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>. */
20 #include <objc/objc.h>
21 #include <objc/Object.h>
23 #ifdef __NEXT_RUNTIME__
25 #define OBJC_GETCLASS objc_getClass
26 #define CLASS_GETINSTANCEMETHOD class_getInstanceMethod
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
37 #define CHECK_IF(expr) if(!(expr)) abort()
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;
48 unsigned offs1, offs2, offs3, offs4, offs5, offs6, offs7;
51 -(id)setRect:(XXRect)r withInt:(int)i {
52 unsigned offs = sizeof(self);
53 CHECK_IF(offs == offs3);
55 CHECK_IF(offs == offs4);
57 CHECK_IF(offs == offs5);
59 CHECK_IF(offs == offs1);
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);
66 CHECK_IF(offs == offs4);
67 offs += sizeof((int)c);
68 CHECK_IF(offs == offs5);
70 CHECK_IF(offs == offs6);
72 CHECK_IF(offs == offs7);
74 CHECK_IF(offs == offs1);
80 Foo *foo = [[Foo alloc] init];
81 Class fooClass = OBJC_GETCLASS("Foo");
85 meth = CLASS_GETINSTANCEMETHOD(fooClass, @selector(setRect:withInt:));
87 sscanf(meth->method_types, "@%u@%u:%u{_XXRect={?=ff}{?=ff}}%ui%u", &offs1, &offs2, &offs3,
90 [foo setRect:my_rect withInt:123];
92 meth = CLASS_GETINSTANCEMETHOD(fooClass, @selector(char:float:double:long:));
94 if (sizeof (long) == 8)
95 string = "v%u@%u:%uc%uf%ud%uq%u";
97 string = "v%u@%u:%uc%uf%ud%ul%u";
98 sscanf(meth->method_types, string, &offs1, &offs2, &offs3,
99 &offs4, &offs5, &offs6, &offs7);
101 [foo char:'c' float:2.3 double:3.5 long:2345L];