OSDN Git Service

Backported from mainline
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / objc.dg / type-size-2.m
1 /* Make sure that array arguments to methods are given the size of pointers.  */
2 /* As in the case of ivars, arrays without size (e.g., 'int []') are
3    encoded as pointers.  */
4 /* Contributed by Ziemowit Laski <zlaski@apple.com>.  */
5 /* { dg-do run } */
6 /* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
7
8 #include "../objc-obj-c++-shared/runtime.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 extern int sscanf(const char *str, const char *format, ...);
13 extern void abort(void);
14 #define CHECK_IF(expr) if(!(expr)) abort()
15
16 enum Enum { one, two, three, four };
17
18 @interface ArrayTest
19 - (const char *)str:(signed char [])arg1 with:(unsigned char *)arg2 and:(enum Enum[4])en;
20 - (int)meth1:(int [])arg1 with:(int [0])arg2 with:(int [2])arg3;
21 @end
22
23 @implementation ArrayTest
24 - (int)meth1:(int [])arg1 with:(int [0])arg2 with:(int [2])arg3 { return 0; }
25 - (const char *)str:(signed char [])arg1 with:(unsigned char *)arg2 and:(enum Enum[4])en { return "str"; }
26 @end
27
28 Class cls;
29 Method meth ;
30
31 unsigned totsize, offs0, offs1, offs2, offs3, offs4, offs5, offs6, offs7;
32
33 static void scan_initial(const char *pattern) {
34   totsize = offs0 = offs1 = offs2 = offs3 = offs4 = offs5 = offs6 = offs7 = (unsigned)-1;
35   sscanf(method_getTypeEncoding(meth), pattern, &totsize, &offs0, &offs1, &offs2, &offs3,
36       &offs4, &offs5, &offs6, &offs7);
37   CHECK_IF(!offs0 && offs1 == sizeof(id) && offs2 == offs1 + sizeof(SEL) && totsize >= offs2);
38 }
39
40 int main(void) {
41   cls = objc_getClass("ArrayTest");
42
43   meth = class_getInstanceMethod(cls, @selector(str:with:and:));
44
45   /* Here we have the complication that 'enum Enum' could be encoded
46      as 'i' on __NEXT_RUNTIME_, and (most likely) as 'I' on the GNU
47      runtime.  So we get the @encode(enum Enum), then put it into the
48      string in place of the traditional 'i'.
49   */
50   /* scan_initial("r*%u@%u:%u*%u*%u[4i]%u"); */
51   {
52     char pattern[1024];
53
54     sprintf (pattern, "r*%%u@%%u:%%u*%%u*%%u[4%s]%%u", @encode(enum Enum));
55     scan_initial(pattern);
56   }
57
58   CHECK_IF(offs3 == offs2 + sizeof(signed char *) && offs4 == offs3 + sizeof(unsigned char *));
59   CHECK_IF(totsize == offs4 + sizeof(enum Enum *));
60   meth = class_getInstanceMethod(cls, @selector(meth1:with:with:));
61   scan_initial("i%u@%u:%u^i%u[0i]%u[2i]%u");
62   CHECK_IF(offs3 == offs2 + sizeof(int *) && offs4 == offs3 + sizeof(int *));
63   CHECK_IF(totsize == offs4 + sizeof(int *));                                           
64   return 0;
65 }