summaryrefslogtreecommitdiff
path: root/gcc/testsuite/obj-c++.dg/template-3.mm
blob: a3b96b32d7eabdc8fb91feeda9d0211d70112fd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Test for passing arguments to ObjC methods in the context of template
   expansion.  */
/* Contributed by Ziemowit Laski  <zlaski@apple.com>.  */

/* { dg-do run } */
/* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
#include "../objc-obj-c++-shared/Object1.h"
#include <stdlib.h>

#define CHECK_IF(expr) if(!(expr)) abort()

@interface ObjCClass : Object
{
@public
  int info;
}
-(id) init;
-(id) initWithInformation: (int) whatInfo;
-(id) initWithInformation: (int) whatInfo andInfo: (int) info2;
@end

void foo(int info) {
   ObjCClass *mObj1 = [[ObjCClass alloc] init];
   ObjCClass *mObj2 = [[ObjCClass alloc] initWithInformation: info];
   ObjCClass *mObj3 = [[ObjCClass alloc] initWithInformation: info andInfo: 39];

   CHECK_IF(mObj1->info == 666);
   CHECK_IF(mObj2->info == info);
   CHECK_IF(mObj3->info == info + 39);
}

template <class WrappedObjCClass>
class ObjCObjectWrapper
{
    public:
        ObjCObjectWrapper(int info);
        WrappedObjCClass *mObj1, *mObj2, *mObj3;
};

template <class WrappedObjCClass>
ObjCObjectWrapper<WrappedObjCClass>::ObjCObjectWrapper(int info)
{
    mObj1 = [[WrappedObjCClass alloc] init];
    mObj2 = [[WrappedObjCClass alloc] initWithInformation: info];
    mObj3 = [[WrappedObjCClass alloc] initWithInformation: info andInfo: 67];
}

@implementation ObjCClass
-(id) init {
  return [self initWithInformation:666];
}
-(id) initWithInformation: (int) whatInfo {
  [super init];
  info = whatInfo;
  return self;
}
-(id) initWithInformation: (int) whatInfo andInfo: (int) info2 {
  [super init];
  info = whatInfo + info2;
  return self;
}
@end

ObjCObjectWrapper<ObjCClass> staticInstance(42); 

int main(void) {
  ObjCObjectWrapper<ObjCClass> stackInstance(47);

  foo(89);
  
  CHECK_IF(staticInstance.mObj1->info == 666);
  CHECK_IF(staticInstance.mObj2->info == 42);
  CHECK_IF(staticInstance.mObj3->info == 42 + 67);
  
  CHECK_IF(stackInstance.mObj1->info == 666);
  CHECK_IF(stackInstance.mObj2->info == 47);
  CHECK_IF(stackInstance.mObj3->info == 47 + 67);
  
  return 0;
}
#include "../objc-obj-c++-shared/Object1-implementation.h"