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>. */
19 #include <objc/objc.h>
20 #include <objc/Object.h>
22 #ifdef __NEXT_RUNTIME__
24 #define OBJC_GETCLASS objc_getClass
25 #define CLASS_GETINSTANCEMETHOD class_getInstanceMethod
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
33 extern int sscanf(const char *str, const char *format, ...);
34 extern void abort(void);
35 #define CHECK_IF(expr) if(!(expr)) abort()
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;
46 unsigned offs1, offs2, offs3, offs4, offs5, offs6, offs7;
49 -(id)setRect:(XXRect)r withInt:(int)i {
50 unsigned offs = sizeof(self);
51 CHECK_IF(offs == offs3);
53 CHECK_IF(offs == offs4);
55 CHECK_IF(offs == offs5);
57 CHECK_IF(offs == offs1);
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);
64 CHECK_IF(offs == offs4);
65 offs += sizeof((int)c);
66 CHECK_IF(offs == offs5);
68 CHECK_IF(offs == offs6);
70 CHECK_IF(offs == offs7);
72 CHECK_IF(offs == offs1);
78 Foo *foo = [[Foo alloc] init];
79 Class fooClass = OBJC_GETCLASS("Foo");
83 meth = CLASS_GETINSTANCEMETHOD(fooClass, @selector(setRect:withInt:));
85 sscanf(meth->method_types, "@%u@%u:%u{_XXRect={?=ff}{?=ff}}%ui%u", &offs1, &offs2, &offs3,
88 [foo setRect:my_rect withInt:123];
90 meth = CLASS_GETINSTANCEMETHOD(fooClass, @selector(char:float:double:long:));
92 if (sizeof (long) == 8)
93 string = "v%u@%u:%uc%uf%ud%uq%u";
95 string = "v%u@%u:%uc%uf%ud%ul%u";
96 sscanf(meth->method_types, string, &offs1, &offs2, &offs3,
97 &offs4, &offs5, &offs6, &offs7);
99 [foo char:'c' float:2.3 double:3.5 long:2345L];