OSDN Git Service

patch for PR rtl-optimization/25130
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / obj-c++.dg / encode-5.mm
1 /* Method encoding tests for stand-alone @protocol declarations.  */
2 /* Contributed by Ziemowit Laski <zlaski@apple.com>.  */
3 /* { dg-do run } */
4
5 #include <objc/Protocol.h>
6 #ifdef __cplusplus
7 #define ProtoBool bool
8 #else
9 #define ProtoBool _Bool
10 #endif
11
12 #ifndef __NEXT_RUNTIME__
13 #include <objc/objc-api.h>
14 #endif
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
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 = @protocol(Proto);
39 struct objc_method_description *meth;
40 unsigned totsize, offs0, offs1, offs2, offs3, offs4, offs5, offs6, offs7;
41
42 static void scan_initial(const char *pattern) {
43   totsize = offs0 = offs1 = offs2 = offs3 = offs4 = offs5 = offs6 = offs7 = (unsigned)-1;
44   sscanf(meth->types, pattern, &totsize, &offs0, &offs1, &offs2, &offs3,
45       &offs4, &offs5, &offs6, &offs7);
46   CHECK_IF(!offs0 && offs1 == sizeof(id) && offs2 == offs1 + sizeof(SEL) && totsize >= offs2);
47 }
48
49 int main(void) {
50   const char *string;
51
52   meth = [proto descriptionForInstanceMethod: @selector(char:float:double:unsigned:short:long:)];
53   if (sizeof (long) == 8)
54     string = "v%u@%u:%uc%uf%ud%uI%us%uq%u";
55   else
56     string = "v%u@%u:%uc%uf%ud%uI%us%ul%u";
57   scan_initial(string);
58   CHECK_IF(offs3 == offs2 + sizeof(int) && offs4 == offs3 + sizeof(float));
59   CHECK_IF(offs5 == offs4 + sizeof(double) && offs6 == offs5 + sizeof(unsigned));
60   CHECK_IF(offs7 == offs6 + sizeof(int) && totsize == offs7 + sizeof(long));
61   meth = [proto descriptionForInstanceMethod: @selector(setRect:withBool:withInt:)];
62   scan_initial("^v%u@%u:%u{_XXRect={?=ff(__XXAngle=II)}{?=dd}^{_XXRect}}%uB%ui%u");
63   CHECK_IF(offs3 == offs2 + sizeof(XXRect) && offs4 == offs3 + sizeof(int));
64   CHECK_IF(totsize == offs4 + sizeof(int));
65   meth = [proto descriptionForClassMethod: @selector(getEnum:enum:bool:)];
66   scan_initial("^i%u@%u:%u^{?=ff(__XXAngle=II)}%ui%uc%u");
67   CHECK_IF(offs3 == offs2 + sizeof(XXPoint *) && offs4 == offs3 + sizeof(enum Enum));
68   CHECK_IF(totsize == offs4 + sizeof(int));  /* 'ObjCBool' is really 'char' */
69   meth = [proto descriptionForClassMethod: @selector(getBool:)];         
70   scan_initial("^^B%u@%u:%u^*%u");
71   CHECK_IF(totsize == offs2 + sizeof(ObjCBool **));
72   return 0;
73 }