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