OSDN Git Service

2010-11-05 Steve Ellcey <sje@cup.hp.com>
[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 "../objc-obj-c++-shared/Protocol1.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #ifdef __cplusplus
11 #define ProtoBool bool
12 #else
13 #define ProtoBool _Bool
14 #endif
15
16 #ifndef __NEXT_RUNTIME__
17 #include <objc/objc-api.h>
18 #endif
19
20 extern int sscanf(const char *str, const char *format, ...);
21 extern void abort(void);
22 #define CHECK_IF(expr) if(!(expr)) abort()
23
24 enum Enum {
25   zero, one, two, three
26 };
27 typedef enum Enum Enum;
28 typedef signed char ObjCBool; /* as used by the NeXT runtime */
29
30 @protocol Proto
31 union __XXAngle { unsigned int alpha, beta; };
32 typedef struct { float x, y; union __XXAngle a; } XXPoint;
33 typedef struct { double width, height; } XXSize;
34 typedef struct _XXRect { XXPoint origin; XXSize size; struct _XXRect *next; } XXRect;
35 - (void) char:(signed char)c float:(float)f double:(double)d unsigned:(unsigned)u short:(short)s long:(long)l;
36 - (void *)setRect:(XXRect)r withBool:(ProtoBool)b withInt:(int)i;
37 + (Enum *)getEnum:(XXPoint *)pt enum:(enum Enum)e bool:(ObjCBool)b;
38 + (ProtoBool **)getBool:(ObjCBool **)b;
39 @end
40
41 Protocol *proto = @protocol(Proto);
42 struct objc_method_description *meth;
43 unsigned totsize, offs0, offs1, offs2, offs3, offs4, offs5, offs6, offs7;
44
45 static void scan_initial(const char *pattern) {
46   totsize = offs0 = offs1 = offs2 = offs3 = offs4 = offs5 = offs6 = offs7 = (unsigned)-1;
47   sscanf(meth->types, pattern, &totsize, &offs0, &offs1, &offs2, &offs3,
48          &offs4, &offs5, &offs6, &offs7);
49   CHECK_IF(!offs0 && offs1 == sizeof(id) && offs2 == offs1 + sizeof(SEL) && totsize >= offs2);
50 }
51
52 int main(void) {
53   const char *string;
54
55   meth = [proto descriptionForInstanceMethod: @selector(char:float:double:unsigned:short:long:)];
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 = [proto descriptionForInstanceMethod: @selector(setRect:withBool:withInt:)];
65   scan_initial("^v%u@%u:%u{_XXRect={?=ff(__XXAngle=II)}{?=dd}^{_XXRect}}%uB%ui%u");
66   CHECK_IF(offs3 == offs2 + sizeof(XXRect) && offs4 == offs3 + sizeof(int));
67   CHECK_IF(totsize == offs4 + sizeof(int));
68   meth = [proto descriptionForClassMethod: @selector(getEnum:enum:bool:)];
69
70   /* Here we have the complication that 'enum Enum' could be encoded
71      as 'i' on __NEXT_RUNTIME_, and (most likely) as 'I' on the GNU
72      runtime.  So we get the @encode(enum Enum), then put it into the
73      string in place of the traditional 'i'.
74   */
75   /* scan_initial("^i%u@%u:%u^{?=ff(__XXAngle=II)}%ui%uc%u"); */
76   {
77     char pattern[1024];
78
79     sprintf (pattern, "^%s%%u@%%u:%%u^{?=ff(__XXAngle=II)}%%u%s%%uc%%u",
80              @encode(enum Enum), @encode(enum Enum));
81     scan_initial(pattern);
82   }
83
84   CHECK_IF(offs3 == offs2 + sizeof(XXPoint *) && offs4 == offs3 + sizeof(enum Enum));
85   CHECK_IF(totsize == offs4 + sizeof(int));  /* 'ObjCBool' is really 'char' */
86   meth = [proto descriptionForClassMethod: @selector(getBool:)];         
87   scan_initial("^^B%u@%u:%u^*%u");
88   CHECK_IF(totsize == offs2 + sizeof(ObjCBool **));
89   return 0;
90 }