xref: /llvm-project/clang/test/CodeGenObjC/parameterized_classes.m (revision 2eb84c3ae2bf6c918c20cbe1c315e2f4683056f4)
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 ptr @llvm.objc.retainBlock
65  [array addObject: ^ { printMe(name); }];
66  // CHECK-NOT: ret void
67  array[0] = ^ { printMe(name); };
68  // CHECK: call ptr @llvm.objc.retainBlock
69  // CHECK: ret void
70}
71
72// CHECK-LABEL: define internal void @"\01-[Derived setDest:]
73// CHECK: %[[SELFADDR:.*]] = alloca ptr
74// CHECK: %[[AADDR:a.addr]] = alloca ptr
75// CHECK: %[[V2:.*]] = load ptr, ptr %[[AADDR]]
76// CHECK: %[[V3:.*]] = load ptr, ptr %[[SELFADDR]]
77// CHECK: %[[IVAR:.*]] = load i64, ptr @"OBJC_IVAR_$_Base._destination"
78// CHECK: %[[ADDPTR:.*]] = getelementptr inbounds i8, ptr %[[V3]], i64 %[[IVAR]]
79// CHECK: call void @llvm.objc.storeStrong(ptr %[[ADDPTR]], ptr %[[V2]])
80
81@interface Base<DestType> : NSObject {
82  DestType _destination;
83}
84@end
85
86@interface Derived : Base<NSObject *>
87- (void)setDest:(NSObject *)a;
88@end
89
90@implementation Derived
91- (void)setDest:(NSObject *)a {
92  _destination = a;
93}
94@end
95
96// CHECK-LABEL: define internal void @"\01-[C0 foo1]"(
97// CHECK: {{.*}} = alloca
98// CHECK: {{.*}} = alloca
99// CHECK: %[[D:.*]] = alloca ptr
100// CHECK: %[[TEMP:.*]] = alloca ptr
101// CHECK: %[[V4:.*]] = load ptr, ptr %[[D]]
102// CHECK: store ptr %[[V4]], ptr %[[TEMP]]
103// CHECK: call void @objc_msgSend(ptr noundef %{{.*}}, ptr noundef %{{.*}}, ptr noundef %[[TEMP]])
104
105@interface P0<ObjectType> : NSObject
106- (void)m0:(ObjectType *)first;
107@end
108
109@interface C0 : NSObject
110-(void)foo1;
111@end
112
113@implementation C0 {
114  P0<NSString *> *x;
115}
116
117-(void)foo1 {
118  NSString *d;
119  [x m0:&d];
120}
121@end
122