1 // RUN: %clang_cc1 -fobjc-runtime-has-weak -fsyntax-only -fobjc-arc -verify -fblocks %s 2 3 @interface A 4 @end 5 6 template<typename T, typename U> 7 struct is_same { 8 static const bool value = false; 9 }; 10 11 template<typename T> 12 struct is_same<T, T> { 13 static const bool value = true; 14 }; 15 16 // Instantiation for reference/pointer types that will get lifetime 17 // adjustments. 18 template<typename T> 19 struct X0 { 20 typedef T* pointer; // okay: ends up being strong. 21 typedef T& reference; // okay: ends up being strong 22 }; 23 24 void 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 35 int 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. 39 template<typename T> 40 struct is_pointer_strong { 41 static const bool value = false; 42 }; 43 44 template<typename T> 45 struct is_pointer_strong<__strong T*> { 46 static const bool value = true; 47 }; 48 49 int check_ptr_strong1[is_pointer_strong<__strong id*>::value? 1 : -1]; 50 int check_ptr_strong2[is_pointer_strong<__weak id*>::value? -1 : 1]; 51 int check_ptr_strong3[is_pointer_strong<__autoreleasing id*>::value? -1 : 1]; 52 int check_ptr_strong4[is_pointer_strong<__unsafe_unretained id*>::value? -1 : 1]; 53 int check_ptr_strong5[is_pointer_strong<id>::value? -1 : 1]; 54 55 // Check substitution into lifetime-qualified dependent types. 56 template<typename T> 57 struct make_strong_pointer { 58 typedef __strong T *type; 59 }; 60 61 template<typename T> 62 struct make_strong_pointer<__weak T> { 63 typedef __strong T *type; 64 }; 65 66 template<typename T> 67 struct make_strong_pointer<__autoreleasing T> { 68 typedef __strong T *type; 69 }; 70 71 template<typename T> 72 struct make_strong_pointer<__unsafe_unretained T> { 73 typedef __strong T *type; 74 }; 75 76 // Adding qualifiers 77 int check_make_strong1[is_same<make_strong_pointer<id>::type, __strong id *>::value ? 1 : -1]; 78 int check_make_strong2[is_same<make_strong_pointer<A*>::type, A* __strong *>::value ? 1 : -1]; 79 80 // Adding redundant qualifiers 81 int check_make_strong3[is_same<make_strong_pointer<__strong id>::type, __strong id *>::value ? 1 : -1]; 82 int check_make_strong4[is_same<make_strong_pointer<__strong A*>::type, A* __strong *>::value ? 1 : -1]; 83 84 // Adding nonsensical qualifiers. 85 int check_make_strong5[is_same<make_strong_pointer<int>::type, int *>::value ? 1 : -1]; 86 int check_make_strong6[is_same<make_strong_pointer<__weak id>::type, __strong id *>::value ? 1 : -1]; 87 88 template<typename T> 89 struct make_weak { 90 typedef __weak T type; 91 }; 92 93 int check_make_weak0[is_same<make_weak<id>::type, __weak id>::value? 1 : -1]; 94 int check_make_weak1[is_same<make_weak<__strong id>::type, __weak id>::value? 1 : -1]; 95 int check_make_weak2[is_same<make_weak<__autoreleasing id>::type, __weak id>::value? 1 : -1]; 96 97 template<typename T> 98 struct make_weak_fail { 99 typedef T T_type; 100 typedef __weak T_type type; // expected-error{{the type 'T_type' (aka '__weak id') already has retainment attributes set on it}} \ 101 // expected-error{{the type 'T_type' (aka '__strong id') already has retainment attributes set on it}} 102 }; 103 104 int 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 106 int 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. 109 template<typename T> struct identity { }; 110 111 template<typename T> identity<T> accept_strong_ptr(__strong T*); 112 template<typename T> identity<T> accept_strong_ref(__strong T&); 113 114 template<typename T> identity<T> accept_any_ptr(T*); 115 template<typename T> identity<T> accept_any_ref(T&); 116 117 void 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 137 void 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). 158 template<typename T> 159 struct classify_pointer_pointer { 160 static const unsigned value = 0; 161 }; 162 163 template<typename T> 164 struct classify_pointer_pointer<T*> { 165 static const unsigned value = 1; 166 }; 167 168 template<typename T> 169 struct classify_pointer_pointer<__strong T*> { 170 static const unsigned value = 2; 171 }; 172 173 template<typename T> 174 struct classify_pointer_pointer<__weak T*> { 175 static const unsigned value = 3; 176 }; 177 178 template<typename T> 179 struct classify_pointer_pointer<T&> { 180 static const unsigned value = 4; 181 }; 182 183 template<typename T> 184 struct classify_pointer_pointer<__strong T&> { 185 static const unsigned value = 5; 186 }; 187 188 template<typename T> 189 struct classify_pointer_pointer<__weak T&> { 190 static const unsigned value = 6; 191 }; 192 193 int classify_ptr1[classify_pointer_pointer<int>::value == 0? 1 : -1]; 194 int classify_ptr2[classify_pointer_pointer<int *>::value == 1? 1 : -1]; 195 int classify_ptr3[classify_pointer_pointer<id __strong *>::value == 2? 1 : -1]; 196 int classify_ptr4[classify_pointer_pointer<id __weak *>::value == 3? 1 : -1]; 197 int classify_ptr5[classify_pointer_pointer<int&>::value == 4? 1 : -1]; 198 int classify_ptr6[classify_pointer_pointer<id __strong&>::value == 5? 1 : -1]; 199 int classify_ptr7[classify_pointer_pointer<id __weak&>::value == 6? 1 : -1]; 200 int classify_ptr8[classify_pointer_pointer<id __autoreleasing&>::value == 4? 1 : -1]; 201 int classify_ptr9[classify_pointer_pointer<id __unsafe_unretained&>::value == 4? 1 : -1]; 202 int classify_ptr10[classify_pointer_pointer<id __autoreleasing *>::value == 1? 1 : -1]; 203 int classify_ptr11[classify_pointer_pointer<id __unsafe_unretained *>::value == 1? 1 : -1]; 204 int classify_ptr12[classify_pointer_pointer<int *>::value == 1? 1 : -1]; 205 int classify_ptr13[classify_pointer_pointer<A * __strong *>::value == 2? 1 : -1]; 206 int classify_ptr14[classify_pointer_pointer<A * __weak *>::value == 3? 1 : -1]; 207 int classify_ptr15[classify_pointer_pointer<int&>::value == 4? 1 : -1]; 208 int classify_ptr16[classify_pointer_pointer<A * __strong&>::value == 5? 1 : -1]; 209 int classify_ptr17[classify_pointer_pointer<A * __weak&>::value == 6? 1 : -1]; 210 int classify_ptr18[classify_pointer_pointer<A * __autoreleasing&>::value == 4? 1 : -1]; 211 int classify_ptr19[classify_pointer_pointer<A * __unsafe_unretained&>::value == 4? 1 : -1]; 212 int classify_ptr20[classify_pointer_pointer<A * __autoreleasing *>::value == 1? 1 : -1]; 213 int classify_ptr21[classify_pointer_pointer<A * __unsafe_unretained *>::value == 1? 1 : -1]; 214 215 template<typename T> int& qual_vs_unqual_ptr(__strong T*); 216 template<typename T> double& qual_vs_unqual_ptr(__weak T*); 217 template<typename T> float& qual_vs_unqual_ptr(T*); 218 template<typename T> int& qual_vs_unqual_ref(__strong T&); 219 template<typename T> double& qual_vs_unqual_ref(__weak T&); 220 template<typename T> float& qual_vs_unqual_ref(T&); 221 222 void 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 239 void 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 256 namespace 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