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