OSDN Git Service

2010-11-05 Steve Ellcey <sje@cup.hp.com>
[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 /* { dg-additional-sources "../objc-obj-c++-shared/Object1.m" } */
8
9 #include "../objc-obj-c++-shared/Object1.h"
10 #include "../objc-obj-c++-shared/next-mapping.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #ifdef __NEXT_RUNTIME__
14 #include <objc/objc-runtime.h>
15 #define METHOD Method
16 #else
17 #include <objc/objc-api.h>
18 #define METHOD Method_t
19 #define method_get_types(M) (M)->method_types
20 #endif
21
22 extern int sscanf(const char *str, const char *format, ...);
23 extern void abort(void);
24 #define CHECK_IF(expr) if(!(expr)) abort()
25
26 enum Enum { one, two, three, four };
27
28 @interface ArrayTest
29 - (const char *)str:(signed char [])arg1 with:(unsigned char *)arg2 and:(enum Enum[4])en;
30 - (int)meth1:(int [])arg1 with:(int [0])arg2 with:(int [2])arg3;
31 @end
32
33 @implementation ArrayTest
34 - (int)meth1:(int [])arg1 with:(int [0])arg2 with:(int [2])arg3 { return 0; }
35 - (const char *)str:(signed char [])arg1 with:(unsigned char *)arg2 and:(enum Enum[4])en { return "str"; }
36 @end
37
38 Class cls;
39 METHOD meth ;
40
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(method_get_types(meth), 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   cls = objc_get_class("ArrayTest");
52
53   meth = class_get_instance_method(cls, @selector(str:with:and:));
54
55   /* Here we have the complication that 'enum Enum' could be encoded
56      as 'i' on __NEXT_RUNTIME_, and (most likely) as 'I' on the GNU
57      runtime.  So we get the @encode(enum Enum), then put it into the
58      string in place of the traditional 'i'.
59   */
60   /* scan_initial("r*%u@%u:%u*%u*%u[4i]%u"); */
61   {
62     char pattern[1024];
63
64     sprintf (pattern, "r*%%u@%%u:%%u*%%u*%%u[4%s]%%u", @encode(enum Enum));
65     scan_initial(pattern);
66   }
67
68   CHECK_IF(offs3 == offs2 + sizeof(signed char *) && offs4 == offs3 + sizeof(unsigned char *));
69   CHECK_IF(totsize == offs4 + sizeof(enum Enum *));
70   meth = class_get_instance_method(cls, @selector(meth1:with:with:));
71   scan_initial("i%u@%u:%u^i%u[0i]%u[2i]%u");
72   CHECK_IF(offs3 == offs2 + sizeof(int *) && offs4 == offs3 + sizeof(int *));
73   CHECK_IF(totsize == offs4 + sizeof(int *));                                           
74   return 0;
75 }