blob: ef68b94e7284170fbe5262937cef93556bd57e5a [file] [log] [blame]
John McCalld1e40d52011-10-02 01:16:38 +00001// RUN: %clang_cc1 -fobjc-runtime-has-weak -fsyntax-only -fobjc-arc -verify -fblocks %s
John McCallf85e1932011-06-15 23:02:42 +00002
3@interface A
4@end
5
Douglas Gregorb2513022012-07-18 00:14:59 +00006@class NSString;
7
John McCallf85e1932011-06-15 23:02:42 +00008template<typename T, typename U>
9struct is_same {
10 static const bool value = false;
11};
12
13template<typename T>
14struct is_same<T, T> {
15 static const bool value = true;
16};
17
18// Instantiation for reference/pointer types that will get lifetime
19// adjustments.
20template<typename T>
21struct X0 {
Douglas Gregore559ca12011-06-17 22:11:49 +000022 typedef T* pointer; // okay: ends up being strong.
23 typedef T& reference; // okay: ends up being strong
John McCallf85e1932011-06-15 23:02:42 +000024};
25
26void test_X0() {
Douglas Gregore559ca12011-06-17 22:11:49 +000027 X0<id> x0id;
28 X0<A*> x0a;
John McCallf85e1932011-06-15 23:02:42 +000029 X0<__strong A*> x0sa;
30
31 id __strong *ptr;
32 id __strong val;
33 X0<__strong id>::pointer &ptr_ref = ptr;
34 X0<__strong id>::reference ref = val;
35}
36
Douglas Gregore559ca12011-06-17 22:11:49 +000037int check_infer_strong[is_same<id, __strong id>::value? 1 : -1];
38
John McCallf85e1932011-06-15 23:02:42 +000039// Check template argument deduction (e.g., for specialization) using
40// lifetime qualifiers.
41template<typename T>
42struct is_pointer_strong {
43 static const bool value = false;
44};
45
46template<typename T>
47struct is_pointer_strong<__strong T*> {
48 static const bool value = true;
49};
50
51int check_ptr_strong1[is_pointer_strong<__strong id*>::value? 1 : -1];
52int check_ptr_strong2[is_pointer_strong<__weak id*>::value? -1 : 1];
53int check_ptr_strong3[is_pointer_strong<__autoreleasing id*>::value? -1 : 1];
54int check_ptr_strong4[is_pointer_strong<__unsafe_unretained id*>::value? -1 : 1];
55int check_ptr_strong5[is_pointer_strong<id>::value? -1 : 1];
56
57// Check substitution into lifetime-qualified dependent types.
58template<typename T>
59struct make_strong_pointer {
60 typedef __strong T *type;
61};
62
Douglas Gregore559ca12011-06-17 22:11:49 +000063template<typename T>
64struct make_strong_pointer<__weak T> {
65 typedef __strong T *type;
66};
67
68template<typename T>
69struct make_strong_pointer<__autoreleasing T> {
70 typedef __strong T *type;
71};
72
73template<typename T>
74struct make_strong_pointer<__unsafe_unretained T> {
75 typedef __strong T *type;
76};
77
John McCallf85e1932011-06-15 23:02:42 +000078// Adding qualifiers
79int check_make_strong1[is_same<make_strong_pointer<id>::type, __strong id *>::value ? 1 : -1];
80int check_make_strong2[is_same<make_strong_pointer<A*>::type, A* __strong *>::value ? 1 : -1];
81
82// Adding redundant qualifiers
83int check_make_strong3[is_same<make_strong_pointer<__strong id>::type, __strong id *>::value ? 1 : -1];
84int check_make_strong4[is_same<make_strong_pointer<__strong A*>::type, A* __strong *>::value ? 1 : -1];
85
86// Adding nonsensical qualifiers.
87int check_make_strong5[is_same<make_strong_pointer<int>::type, int *>::value ? 1 : -1];
Douglas Gregore559ca12011-06-17 22:11:49 +000088int check_make_strong6[is_same<make_strong_pointer<__weak id>::type, __strong id *>::value ? 1 : -1];
89
90template<typename T>
91struct make_weak {
92 typedef __weak T type;
93};
94
95int check_make_weak0[is_same<make_weak<id>::type, __weak id>::value? 1 : -1];
96int check_make_weak1[is_same<make_weak<__strong id>::type, __weak id>::value? 1 : -1];
97int check_make_weak2[is_same<make_weak<__autoreleasing id>::type, __weak id>::value? 1 : -1];
98
Douglas Gregor4020cae2011-06-17 23:16:24 +000099template<typename T>
100struct make_weak_fail {
101 typedef T T_type;
John McCalld85bf9d2012-02-08 00:46:41 +0000102 typedef __weak T_type type; // expected-error{{the type 'T_type' (aka '__weak id') is already explicitly ownership-qualified}} \
103 // expected-error{{the type 'T_type' (aka '__strong id') is already explicitly ownership-qualified}}
Douglas Gregor4020cae2011-06-17 23:16:24 +0000104};
105
106int check_make_weak_fail0[is_same<make_weak_fail<__weak id>::type, __weak id>::value? 1 : -1]; // expected-note{{in instantiation of template class 'make_weak_fail<__weak id>' requested here}}
107
108int check_make_weak_fail1[is_same<make_weak_fail<id>::type, __weak id>::value? -1 : 1]; // expected-note{{in instantiation of template class 'make_weak_fail<id>' requested here}}
John McCallf85e1932011-06-15 23:02:42 +0000109
110// Check template argument deduction from function templates.
111template<typename T> struct identity { };
112
113template<typename T> identity<T> accept_strong_ptr(__strong T*);
114template<typename T> identity<T> accept_strong_ref(__strong T&);
115
116template<typename T> identity<T> accept_any_ptr(T*);
117template<typename T> identity<T> accept_any_ref(T&);
118
119void test_func_deduction_id() {
120 __strong id *sip;
121 __weak id *wip;
122 __autoreleasing id *aip;
123 __unsafe_unretained id *uip;
124
125 identity<id> res1 = accept_strong_ptr(sip);
126 identity<__strong id> res2 = accept_any_ptr(sip);
127
128 __strong id si;
129 __weak id wi;
130 __autoreleasing id ai;
131 __unsafe_unretained id ui;
132 identity<id> res3 = accept_strong_ref(si);
133 identity<__strong id> res4 = accept_any_ref(si);
134 identity<__weak id> res5 = accept_any_ref(wi);
135 identity<__autoreleasing id> res6 = accept_any_ref(ai);
136 identity<__unsafe_unretained id> res7 = accept_any_ref(ui);
137}
138
139void test_func_deduction_A() {
140 __strong A * *sip;
141 __weak A * *wip;
142 __autoreleasing A * *aip;
143 __unsafe_unretained A * *uip;
144
145 identity<A *> res1 = accept_strong_ptr(sip);
146 identity<__strong A *> res2 = accept_any_ptr(sip);
147
148 __strong A * si;
149 __weak A * wi;
150 __autoreleasing A * ai;
151 __unsafe_unretained A * ui;
152 identity<A *> res3 = accept_strong_ref(si);
153 identity<__strong A *> res4 = accept_any_ref(si);
154 identity<__weak A *> res5 = accept_any_ref(wi);
155 identity<__autoreleasing A *> res6 = accept_any_ref(ai);
156 identity<__unsafe_unretained A *> res7 = accept_any_ref(ui);
157}
158
159// Test partial ordering (qualified vs. non-qualified).
160template<typename T>
161struct classify_pointer_pointer {
162 static const unsigned value = 0;
163};
164
165template<typename T>
166struct classify_pointer_pointer<T*> {
167 static const unsigned value = 1;
168};
169
170template<typename T>
171struct classify_pointer_pointer<__strong T*> {
172 static const unsigned value = 2;
173};
174
175template<typename T>
176struct classify_pointer_pointer<__weak T*> {
177 static const unsigned value = 3;
178};
179
180template<typename T>
181struct classify_pointer_pointer<T&> {
182 static const unsigned value = 4;
183};
184
185template<typename T>
186struct classify_pointer_pointer<__strong T&> {
187 static const unsigned value = 5;
188};
189
190template<typename T>
191struct classify_pointer_pointer<__weak T&> {
192 static const unsigned value = 6;
193};
194
195int classify_ptr1[classify_pointer_pointer<int>::value == 0? 1 : -1];
196int classify_ptr2[classify_pointer_pointer<int *>::value == 1? 1 : -1];
197int classify_ptr3[classify_pointer_pointer<id __strong *>::value == 2? 1 : -1];
198int classify_ptr4[classify_pointer_pointer<id __weak *>::value == 3? 1 : -1];
199int classify_ptr5[classify_pointer_pointer<int&>::value == 4? 1 : -1];
200int classify_ptr6[classify_pointer_pointer<id __strong&>::value == 5? 1 : -1];
201int classify_ptr7[classify_pointer_pointer<id __weak&>::value == 6? 1 : -1];
202int classify_ptr8[classify_pointer_pointer<id __autoreleasing&>::value == 4? 1 : -1];
203int classify_ptr9[classify_pointer_pointer<id __unsafe_unretained&>::value == 4? 1 : -1];
204int classify_ptr10[classify_pointer_pointer<id __autoreleasing *>::value == 1? 1 : -1];
205int classify_ptr11[classify_pointer_pointer<id __unsafe_unretained *>::value == 1? 1 : -1];
206int classify_ptr12[classify_pointer_pointer<int *>::value == 1? 1 : -1];
207int classify_ptr13[classify_pointer_pointer<A * __strong *>::value == 2? 1 : -1];
208int classify_ptr14[classify_pointer_pointer<A * __weak *>::value == 3? 1 : -1];
209int classify_ptr15[classify_pointer_pointer<int&>::value == 4? 1 : -1];
210int classify_ptr16[classify_pointer_pointer<A * __strong&>::value == 5? 1 : -1];
211int classify_ptr17[classify_pointer_pointer<A * __weak&>::value == 6? 1 : -1];
212int classify_ptr18[classify_pointer_pointer<A * __autoreleasing&>::value == 4? 1 : -1];
213int classify_ptr19[classify_pointer_pointer<A * __unsafe_unretained&>::value == 4? 1 : -1];
214int classify_ptr20[classify_pointer_pointer<A * __autoreleasing *>::value == 1? 1 : -1];
215int classify_ptr21[classify_pointer_pointer<A * __unsafe_unretained *>::value == 1? 1 : -1];
216
217template<typename T> int& qual_vs_unqual_ptr(__strong T*);
218template<typename T> double& qual_vs_unqual_ptr(__weak T*);
219template<typename T> float& qual_vs_unqual_ptr(T*);
220template<typename T> int& qual_vs_unqual_ref(__strong T&);
221template<typename T> double& qual_vs_unqual_ref(__weak T&);
222template<typename T> float& qual_vs_unqual_ref(T&);
223
224void test_qual_vs_unqual_id() {
225 __strong id *sip;
226 __weak id *wip;
227 __autoreleasing id *aip;
228 __unsafe_unretained id *uip;
229
230 int &ir1 = qual_vs_unqual_ptr(sip);
231 double &dr1 = qual_vs_unqual_ptr(wip);
232 float &fr1 = qual_vs_unqual_ptr(aip);
233 float &fr2 = qual_vs_unqual_ptr(uip);
234
235 int &ir2 = qual_vs_unqual_ref(*sip);
236 double &dr2 = qual_vs_unqual_ref(*wip);
237 float &fr3 = qual_vs_unqual_ref(*aip);
238 float &fr4 = qual_vs_unqual_ref(*uip);
239}
240
241void test_qual_vs_unqual_a() {
242 __strong A * *sap;
243 __weak A * *wap;
244 __autoreleasing A * *aap;
245 __unsafe_unretained A * *uap;
246
247 int &ir1 = qual_vs_unqual_ptr(sap);
248 double &dr1 = qual_vs_unqual_ptr(wap);
249 float &fr1 = qual_vs_unqual_ptr(aap);
250 float &fr2 = qual_vs_unqual_ptr(uap);
251
252 int &ir2 = qual_vs_unqual_ref(*sap);
253 double &dr2 = qual_vs_unqual_ref(*wap);
254 float &fr3 = qual_vs_unqual_ref(*aap);
255 float &fr4 = qual_vs_unqual_ref(*uap);
256}
Douglas Gregorda8b2492011-07-26 14:53:44 +0000257
258namespace rdar9828157 {
259 // Template argument deduction involving lifetime qualifiers and
260 // non-lifetime types.
261 class A { };
262
263 template<typename T> float& f(T&);
264 template<typename T> int& f(__strong T&);
265 template<typename T> double& f(__weak T&);
266
267 void test_f(A* ap) {
268 float &fr = (f)(ap);
269 }
270}
Douglas Gregorb2513022012-07-18 00:14:59 +0000271
272namespace rdar10862386 {
273 // More deduction with lifetime qualifiers.
274 template <typename T>
275 int testing(const T &) {
276 return 1;
277 }
278
279 void test() {
280 testing(1);
281 testing("hi");
282 testing<NSString *>(@"hi");
283 testing(@"hi");
284 }
285}
Douglas Gregor02dd7982013-01-17 23:36:45 +0000286
287namespace rdar12367446 {
288 template <class T> class A;
289 template <class R> class A<R()> {};
290
291 void test() {
292 A<id()> value;
293 }
294}