annotate gcc/testsuite/objc.dg/encode-2.m @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Test Objective-C method encodings. */
kono
parents:
diff changeset
2
kono
parents:
diff changeset
3 /* The _encoded_ parameter offsets for Objective-C methods are
kono
parents:
diff changeset
4 computed inductively as follows:
kono
parents:
diff changeset
5 - The first paramter (self) has offset 0;
kono
parents:
diff changeset
6 - The k-th parameter (k > 1) has offset equal to the
kono
parents:
diff changeset
7 sum of:
kono
parents:
diff changeset
8 - the offset of the k-1-st paramter
kono
parents:
diff changeset
9 - the (void *)-promoted size of the k-1-st parameter.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 Note that the encoded offsets need not correspond
kono
parents:
diff changeset
12 to the actual placement of parameters (relative to 'self')
kono
parents:
diff changeset
13 on the stack! Your target's ABI may have very different
kono
parents:
diff changeset
14 opinions on the matter. */
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 /* Contributed by Ziemowit Laski <zlaski@apple.com>. */
kono
parents:
diff changeset
17 /* { dg-do run } */
kono
parents:
diff changeset
18 /* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 #include "../objc-obj-c++-shared/TestsuiteObject.m"
kono
parents:
diff changeset
21 #include "../objc-obj-c++-shared/runtime.h"
kono
parents:
diff changeset
22
kono
parents:
diff changeset
23 extern int sscanf(const char *str, const char *format, ...);
kono
parents:
diff changeset
24 extern void abort(void);
kono
parents:
diff changeset
25 #define CHECK_IF(expr) if(!(expr)) abort()
kono
parents:
diff changeset
26
kono
parents:
diff changeset
27 @interface Foo: TestsuiteObject
kono
parents:
diff changeset
28 typedef struct { float x, y; } XXPoint;
kono
parents:
diff changeset
29 typedef struct { float width, height; } XXSize;
kono
parents:
diff changeset
30 typedef struct _XXRect { XXPoint origin; XXSize size; } XXRect;
kono
parents:
diff changeset
31 -(id)setRect:(XXRect)r withInt:(int)i;
kono
parents:
diff changeset
32 -(void) char:(signed char)c float:(float)f double:(double)d long:(long)l;
kono
parents:
diff changeset
33 @end
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 XXRect my_rect;
kono
parents:
diff changeset
36 unsigned offs1, offs2, offs3, offs4, offs5, offs6, offs7;
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 @implementation Foo
kono
parents:
diff changeset
39 -(id)setRect:(XXRect)r withInt:(int)i {
kono
parents:
diff changeset
40 unsigned offs = sizeof(self);
kono
parents:
diff changeset
41 CHECK_IF(offs == offs3);
kono
parents:
diff changeset
42 offs += sizeof(_cmd);
kono
parents:
diff changeset
43 CHECK_IF(offs == offs4);
kono
parents:
diff changeset
44 offs += sizeof(r);
kono
parents:
diff changeset
45 CHECK_IF(offs == offs5);
kono
parents:
diff changeset
46 offs += sizeof(i);
kono
parents:
diff changeset
47 CHECK_IF(offs == offs1);
kono
parents:
diff changeset
48 return nil;
kono
parents:
diff changeset
49 }
kono
parents:
diff changeset
50 -(void) char:(signed char)c float:(float)f double:(double)d long:(long)l {
kono
parents:
diff changeset
51 unsigned offs = sizeof(self);
kono
parents:
diff changeset
52 CHECK_IF(offs == offs3);
kono
parents:
diff changeset
53 offs += sizeof(_cmd);
kono
parents:
diff changeset
54 CHECK_IF(offs == offs4);
kono
parents:
diff changeset
55 offs += sizeof((int)c);
kono
parents:
diff changeset
56 CHECK_IF(offs == offs5);
kono
parents:
diff changeset
57 offs += sizeof(f);
kono
parents:
diff changeset
58 CHECK_IF(offs == offs6);
kono
parents:
diff changeset
59 offs += sizeof(d);
kono
parents:
diff changeset
60 CHECK_IF(offs == offs7);
kono
parents:
diff changeset
61 offs += sizeof(l);
kono
parents:
diff changeset
62 CHECK_IF(offs == offs1);
kono
parents:
diff changeset
63 }
kono
parents:
diff changeset
64 @end
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 int main(void) {
kono
parents:
diff changeset
68 Foo *foo = [[Foo alloc] init];
kono
parents:
diff changeset
69 Class fooClass = objc_getClass("Foo");
kono
parents:
diff changeset
70 Method meth;
kono
parents:
diff changeset
71 const char *string;
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 meth = class_getInstanceMethod(fooClass, @selector(setRect:withInt:));
kono
parents:
diff changeset
74 offs2 = 9999;
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76 sscanf(method_getTypeEncoding(meth), "@%u@%u:%u{_XXRect={?=ff}{?=ff}}%ui%u", &offs1, &offs2, &offs3,
kono
parents:
diff changeset
77 &offs4, &offs5);
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 CHECK_IF(!offs2);
kono
parents:
diff changeset
80 [foo setRect:my_rect withInt:123];
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 meth = class_getInstanceMethod(fooClass, @selector(char:float:double:long:));
kono
parents:
diff changeset
83 offs2 = 9999;
kono
parents:
diff changeset
84 if (sizeof (long) == 8)
kono
parents:
diff changeset
85 string = "v%u@%u:%uc%uf%ud%uq%u";
kono
parents:
diff changeset
86 else
kono
parents:
diff changeset
87 string = "v%u@%u:%uc%uf%ud%ul%u";
kono
parents:
diff changeset
88 sscanf(method_getTypeEncoding(meth), string, &offs1, &offs2, &offs3,
kono
parents:
diff changeset
89 &offs4, &offs5, &offs6, &offs7);
kono
parents:
diff changeset
90 CHECK_IF(!offs2);
kono
parents:
diff changeset
91 [foo char:'c' float:2.3 double:3.5 long:2345L];
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 return 0;
kono
parents:
diff changeset
94 }