OSDN Git Service

Backported from mainline
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / objc.dg / encode-3.m
1 /* Method encoding tests for stand-alone @protocol declarations.  */
2 /* Contributed by Ziemowit Laski <zlaski@apple.com>.  */
3 /* { dg-do run } */
4 /* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 #include "../objc-obj-c++-shared/runtime.h"
10
11 #ifdef __cplusplus
12 #define ProtoBool bool
13 #else
14 #define ProtoBool _Bool
15 #endif
16
17 extern int sscanf(const char *str, const char *format, ...);
18 extern void abort(void);
19 #define CHECK_IF(expr) if(!(expr)) abort()
20
21 enum Enum {
22   zero, one, two, three
23 };
24 typedef enum Enum Enum;
25 typedef signed char ObjCBool; /* as used by the NeXT runtime */
26
27 @protocol Proto
28 union __XXAngle { unsigned int alpha, beta; };
29 typedef struct { float x, y; union __XXAngle a; } XXPoint;
30 typedef struct { double width, height; } XXSize;
31 typedef struct _XXRect { XXPoint origin; XXSize size; struct _XXRect *next; } XXRect;
32 - (void) char:(signed char)c float:(float)f double:(double)d unsigned:(unsigned)u short:(short)s long:(long)l;
33 - (void *)setRect:(XXRect)r withBool:(ProtoBool)b withInt:(int)i;
34 + (Enum *)getEnum:(XXPoint *)pt enum:(enum Enum)e bool:(ObjCBool)b;
35 + (ProtoBool **)getBool:(ObjCBool **)b;
36 @end
37
38 Protocol *proto;
39 struct objc_method_description *meth;
40 struct objc_method_description meth_object;
41 unsigned totsize, offs0, offs1, offs2, offs3, offs4, offs5, offs6, offs7;
42
43 static void scan_initial(const char *pattern) {
44   totsize = offs0 = offs1 = offs2 = offs3 = offs4 = offs5 = offs6 = offs7 = (unsigned)-1;
45   sscanf(meth->types, pattern, &totsize, &offs0, &offs1, &offs2, &offs3,
46          &offs4, &offs5, &offs6, &offs7);
47   CHECK_IF(!offs0 && offs1 == sizeof(id) && offs2 == offs1 + sizeof(SEL) && totsize >= offs2);
48 }
49
50 int main(void) {
51   const char *string;
52   proto = @protocol(Proto);
53   meth_object = protocol_getMethodDescription (proto,
54                    @selector(char:float:double:unsigned:short:long:), YES, YES);
55   meth = &meth_object;
56   if (sizeof (long) == 8)
57     string = "v%u@%u:%uc%uf%ud%uI%us%uq%u";
58   else
59     string = "v%u@%u:%uc%uf%ud%uI%us%ul%u";
60   scan_initial(string);
61   CHECK_IF(offs3 == offs2 + sizeof(int) && offs4 == offs3 + sizeof(float));
62   CHECK_IF(offs5 == offs4 + sizeof(double) && offs6 == offs5 + sizeof(unsigned));
63   CHECK_IF(offs7 == offs6 + sizeof(int) && totsize == offs7 + sizeof(long));
64   meth_object = protocol_getMethodDescription (proto,
65                   @selector(setRect:withBool:withInt:), YES, YES);
66   meth = &meth_object;
67   scan_initial("^v%u@%u:%u{_XXRect={?=ff(__XXAngle=II)}{?=dd}^{_XXRect}}%uB%ui%u");
68   CHECK_IF(offs3 == offs2 + sizeof(XXRect) && offs4 == offs3 + sizeof(int));
69   CHECK_IF(totsize == offs4 + sizeof(int));
70   meth_object = protocol_getMethodDescription (proto,
71                   @selector(getEnum:enum:bool:), YES, NO);
72   meth = &meth_object; 
73
74   /* Here we have the complication that 'enum Enum' could be encoded
75      as 'i' on __NEXT_RUNTIME_, and (most likely) as 'I' on the GNU
76      runtime.  So we get the @encode(enum Enum), then put it into the
77      string in place of the traditional 'i'.
78   */
79   /* scan_initial("^i%u@%u:%u^{?=ff(__XXAngle=II)}%ui%uc%u"); */
80   {
81     char pattern[1024];
82
83     sprintf (pattern, "^%s%%u@%%u:%%u^{?=ff(__XXAngle=II)}%%u%s%%uc%%u",
84              @encode(enum Enum), @encode(enum Enum));
85     scan_initial(pattern);
86   }
87
88   CHECK_IF(offs3 == offs2 + sizeof(XXPoint *) && offs4 == offs3 + sizeof(enum Enum));
89   CHECK_IF(totsize == offs4 + sizeof(int));  /* 'ObjCBool' is really 'char' */
90   meth_object = protocol_getMethodDescription (proto,
91                   @selector(getBool:), YES, NO);
92   meth = &meth_object;
93   scan_initial("^^B%u@%u:%u^*%u");
94   CHECK_IF(totsize == offs2 + sizeof(ObjCBool **));
95   return 0;
96 }