PR tree-optimization/81303
[official-gcc.git] / gcc / testsuite / obj-c++.dg / template-3.mm
blobc564733cf7405c6ad8383792be28587285416248
1 /* Test for passing arguments to ObjC methods in the context of template
2    expansion.  */
3 /* 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 #include "../objc-obj-c++-shared/TestsuiteObject.m"
8 #include <stdlib.h>
10 #define CHECK_IF(expr) if(!(expr)) abort()
12 @interface ObjCClass : TestsuiteObject
14 @public
15   int info;
17 -(id) init;
18 -(id) initWithInformation: (int) whatInfo;
19 -(id) initWithInformation: (int) whatInfo andInfo: (int) info2;
20 @end
22 void foo(int info) {
23    ObjCClass *mObj1 = [[ObjCClass alloc] init];
24    ObjCClass *mObj2 = [[ObjCClass alloc] initWithInformation: info];
25    ObjCClass *mObj3 = [[ObjCClass alloc] initWithInformation: info andInfo: 39];
27    CHECK_IF(mObj1->info == 666);
28    CHECK_IF(mObj2->info == info);
29    CHECK_IF(mObj3->info == info + 39);
32 template <class WrappedObjCClass>
33 class ObjCObjectWrapper
35     public:
36         ObjCObjectWrapper(int info);
37         WrappedObjCClass *mObj1, *mObj2, *mObj3;
40 template <class WrappedObjCClass>
41 ObjCObjectWrapper<WrappedObjCClass>::ObjCObjectWrapper(int info)
43     mObj1 = [[WrappedObjCClass alloc] init];
44     mObj2 = [[WrappedObjCClass alloc] initWithInformation: info];
45     mObj3 = [[WrappedObjCClass alloc] initWithInformation: info andInfo: 67];
48 @implementation ObjCClass
49 -(id) init {
50   return [self initWithInformation:666];
52 -(id) initWithInformation: (int) whatInfo {
53   [super init];
54   info = whatInfo;
55   return self;
57 -(id) initWithInformation: (int) whatInfo andInfo: (int) info2 {
58   [super init];
59   info = whatInfo + info2;
60   return self;
62 @end
64 ObjCObjectWrapper<ObjCClass> staticInstance(42); 
66 int main(void) {
67   ObjCObjectWrapper<ObjCClass> stackInstance(47);
69   foo(89);
70   
71   CHECK_IF(staticInstance.mObj1->info == 666);
72   CHECK_IF(staticInstance.mObj2->info == 42);
73   CHECK_IF(staticInstance.mObj3->info == 42 + 67);
74   
75   CHECK_IF(stackInstance.mObj1->info == 666);
76   CHECK_IF(stackInstance.mObj2->info == 47);
77   CHECK_IF(stackInstance.mObj3->info == 47 + 67);
78   
79   return 0;