1// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fblocks -fobjc-arc -emit-llvm -o - %s | FileCheck %s
2
3// Parameterized classes have no effect on code generation; this test
4// mainly verifies that CodeGen doesn't assert when substituted types
5// in uses of methods don't line up exactly with the parameterized
6// types in the method declarations due to type erasure. "Not crash"
7// is the only interesting criteria here.
8
9@protocol NSObject
10@end
11
12@protocol NSCopying
13@end
14
15__attribute__((objc_root_class))
16@interface NSObject <NSObject>
17@end
18
19@interface NSString : NSObject <NSCopying>
20@end
21
22@interface NSMutableArray<T> : NSObject <NSCopying>
23@property (copy,nonatomic) T firstObject;
24- (void)addObject:(T)object;
25- (void)sortWithFunction:(int (*)(T, T))function;
26- (void)getObjects:(T __strong *)objects length:(unsigned*)length;
27- (T)objectAtIndexedSubscript:(unsigned)index;
28- (void)setObject:(T)object atIndexedSubscript:(unsigned)index;
29@end
30
31NSString *getFirstObjectProp(NSMutableArray<NSString *> *array) {
32  return array.firstObject;
33}
34
35NSString *getFirstObjectMethod(NSMutableArray<NSString *> *array) {
36  return [array firstObject];
37}
38
39void addObject(NSMutableArray<NSString *> *array, NSString *obj) {
40  [array addObject: obj];
41}
42
43int compareStrings(NSString *x, NSString *y) { return 0; }
44int compareBlocks(NSString * (^x)(NSString *),
45                  NSString * (^y)(NSString *)) { return 0; }
46
47void sortTest(NSMutableArray<NSString *> *array,
48              NSMutableArray<NSString * (^)(NSString *)> *array2) {
49  [array sortWithFunction: &compareStrings];
50  [array2 sortWithFunction: &compareBlocks];
51}
52
53void getObjectsTest(NSMutableArray<NSString *> *array) {
54  NSString * __strong *objects;
55  unsigned length;
56  [array getObjects: objects length: &length];
57}
58
59void printMe(NSString *name) { }
60
61// CHECK-LABEL: define{{.*}} void @blockTest
62void blockTest(NSMutableArray<void (^)(void)> *array, NSString *name) {
63  // CHECK-NOT: ret void
64  // CHECK: call i8* @llvm.objc.retainBlock
65  [array addObject: ^ { printMe(name); }];
66  // CHECK-NOT: ret void
67  array[0] = ^ { printMe(name); };
68  // CHECK: call i8* @llvm.objc.retainBlock
69  // CHECK: ret void
70}
71
72// CHECK-LABEL: define internal void @"\01-[Derived setDest:]
73// CHECK: %[[SELFADDR:.*]] = alloca %[[SELFTY:.*]]*
74// CHECK: %[[AADDR:.*]] = alloca %[[IVARTY:.*]]*
75// CHECK: %[[V2:.*]] = load %[[IVARTY]]*, %[[IVARTY]]** %[[AADDR]]
76// CHECK: %[[V3:.*]] = load %[[SELFTY]]*, %[[SELFTY]]** %[[SELFADDR]]
77// CHECK: %[[IVAR:.*]] = load i64, i64* @"OBJC_IVAR_$_Base._destination"
78// CHECK: %[[V4:.*]] = bitcast %[[SELFTY]]* %[[V3]] to i8*
79// CHECK: %[[ADDPTR:.*]] = getelementptr inbounds i8, i8* %[[V4]], i64 %[[IVAR]]
80// CHECK: %[[V5:.*]] = bitcast i8* %[[ADDPTR]] to %[[IVARTY]]**
81// CHECK: %[[V6:.*]] = bitcast %[[IVARTY]]** %[[V5]] to i8**
82// CHECK: %[[V7:.*]] = bitcast %[[IVARTY]]* %[[V2]] to i8*
83// CHECK: call void @llvm.objc.storeStrong(i8** %[[V6]], i8* %[[V7]])
84
85@interface Base<DestType> : NSObject {
86  DestType _destination;
87}
88@end
89
90@interface Derived : Base<NSObject *>
91- (void)setDest:(NSObject *)a;
92@end
93
94@implementation Derived
95- (void)setDest:(NSObject *)a {
96  _destination = a;
97}
98@end
99
100// CHECK-LABEL: define internal void @"\01-[C0 foo1]"(
101// CHECK: {{.*}} = alloca
102// CHECK: {{.*}} = alloca
103// CHECK: %[[D:.*]] = alloca %[[TY:.*]]*
104// CHECK: %[[TEMP:.*]] = alloca %[[TY]]*
105// CHECK: %[[V4:.*]] = load %[[TY]]*, %[[TY]]** %[[D]]
106// CHECK: store %[[TY]]* %[[V4]], %[[TY]]** %[[TEMP]]
107// CHECK: %[[V7:.*]] = bitcast %[[TY]]** %[[TEMP]] to i8**
108// CHECK: call void bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to void (i8*, i8*, i8**)*)(i8* %{{.*}}, i8* %{{.*}}, i8** %[[V7]])
109
110@interface P0<ObjectType> : NSObject
111- (void)m0:(ObjectType *)first;
112@end
113
114@interface C0 : NSObject
115-(void)foo1;
116@end
117
118@implementation C0 {
119  P0<NSString *> *x;
120}
121
122-(void)foo1 {
123  NSString *d;
124  [x m0:&d];
125}
126@end
127