Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -fsyntax-only -pedantic -verify %s |
Douglas Gregor | 6fc17ff | 2008-10-29 15:10:40 +0000 | [diff] [blame] | 2 | int* f(int) { return 0; } |
| 3 | float* f(float) { return 0; } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4 | void f(); |
| 5 | |
| 6 | void test_f(int iv, float fv) { |
| 7 | float* fp = f(fv); |
| 8 | int* ip = f(iv); |
| 9 | } |
| 10 | |
| 11 | int* g(int, float, int); // expected-note {{ candidate function }} |
| 12 | float* g(int, int, int); // expected-note {{ candidate function }} |
| 13 | double* g(int, float, float); // expected-note {{ candidate function }} |
| 14 | char* g(int, float, ...); // expected-note {{ candidate function }} |
| 15 | void g(); |
| 16 | |
| 17 | void test_g(int iv, float fv) { |
| 18 | int* ip1 = g(iv, fv, 0); |
| 19 | float* fp1 = g(iv, iv, 0); |
| 20 | double* dp1 = g(iv, fv, fv); |
| 21 | char* cp1 = g(0, 0); |
| 22 | char* cp2 = g(0, 0, 0, iv, fv); |
| 23 | |
| 24 | double* dp2 = g(0, fv, 1.5); // expected-error {{ call to 'g' is ambiguous; candidates are: }} |
| 25 | } |
| 26 | |
| 27 | double* h(double f); |
| 28 | int* h(int); |
| 29 | |
| 30 | void test_h(float fv, unsigned char cv) { |
| 31 | double* dp = h(fv); |
| 32 | int* ip = h(cv); |
| 33 | } |
| 34 | |
| 35 | int* i(int); |
| 36 | double* i(long); |
| 37 | |
| 38 | void test_i(short sv, int iv, long lv, unsigned char ucv) { |
| 39 | int* ip1 = i(sv); |
| 40 | int* ip2 = i(iv); |
| 41 | int* ip3 = i(ucv); |
| 42 | double* dp1 = i(lv); |
| 43 | } |
| 44 | |
| 45 | int* j(void*); |
| 46 | double* j(bool); |
| 47 | |
| 48 | void test_j(int* ip) { |
| 49 | int* ip1 = j(ip); |
| 50 | } |
| 51 | |
| 52 | int* k(char*); |
| 53 | double* k(bool); |
| 54 | |
| 55 | void test_k() { |
| 56 | int* ip1 = k("foo"); |
| 57 | double* dp1 = k(L"foo"); |
| 58 | } |
| 59 | |
| 60 | int* l(wchar_t*); |
| 61 | double* l(bool); |
| 62 | |
| 63 | void test_l() { |
| 64 | int* ip1 = l(L"foo"); |
| 65 | double* dp1 = l("foo"); |
| 66 | } |
| 67 | |
| 68 | int* m(const char*); |
| 69 | double* m(char*); |
| 70 | |
| 71 | void test_m() { |
| 72 | int* ip = m("foo"); |
| 73 | } |
| 74 | |
| 75 | int* n(char*); |
| 76 | double* n(void*); |
Douglas Gregor | 0f669f5 | 2008-11-26 23:33:36 +0000 | [diff] [blame] | 77 | class E; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 78 | |
Douglas Gregor | 0f669f5 | 2008-11-26 23:33:36 +0000 | [diff] [blame] | 79 | void test_n(E* e) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 80 | char ca[7]; |
| 81 | int* ip1 = n(ca); |
| 82 | int* ip2 = n("foo"); |
| 83 | |
| 84 | float fa[7]; |
| 85 | double* dp1 = n(fa); |
Douglas Gregor | 0f669f5 | 2008-11-26 23:33:36 +0000 | [diff] [blame] | 86 | |
| 87 | double* dp2 = n(e); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | enum PromotesToInt { |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 91 | PromotesToIntValue = -1 |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | enum PromotesToUnsignedInt { |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 95 | PromotesToUnsignedIntValue = 1u |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | int* o(int); |
| 99 | double* o(unsigned int); |
| 100 | float* o(long); |
| 101 | |
| 102 | void test_o() { |
| 103 | int* ip1 = o(PromotesToIntValue); |
| 104 | double* dp1 = o(PromotesToUnsignedIntValue); |
| 105 | } |
| 106 | |
| 107 | int* p(int); |
| 108 | double* p(double); |
| 109 | |
| 110 | void test_p() { |
| 111 | int* ip = p((short)1); |
| 112 | double* dp = p(1.0f); |
| 113 | } |
| 114 | |
| 115 | struct Bits { |
| 116 | signed short int_bitfield : 5; |
| 117 | unsigned int uint_bitfield : 8; |
| 118 | }; |
| 119 | |
| 120 | int* bitfields(int, int); |
| 121 | float* bitfields(unsigned int, int); |
| 122 | |
| 123 | void test_bitfield(Bits bits, int x) { |
| 124 | int* ip = bitfields(bits.int_bitfield, 0); |
| 125 | float* fp = bitfields(bits.uint_bitfield, 0u); |
| 126 | } |
| 127 | |
| 128 | int* multiparm(long, int, long); // expected-note {{ candidate function }} |
| 129 | float* multiparm(int, int, int); // expected-note {{ candidate function }} |
| 130 | double* multiparm(int, int, short); // expected-note {{ candidate function }} |
| 131 | |
| 132 | void test_multiparm(long lv, short sv, int iv) { |
| 133 | int* ip1 = multiparm(lv, iv, lv); |
| 134 | int* ip2 = multiparm(lv, sv, lv); |
| 135 | float* fp1 = multiparm(iv, iv, iv); |
| 136 | float* fp2 = multiparm(sv, iv, iv); |
| 137 | double* dp1 = multiparm(sv, sv, sv); |
| 138 | double* dp2 = multiparm(iv, sv, sv); |
| 139 | multiparm(sv, sv, lv); // expected-error {{ call to 'multiparm' is ambiguous; candidates are: }} |
| 140 | } |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 141 | |
| 142 | // Test overloading based on qualification vs. no qualification |
| 143 | // conversion. |
| 144 | int* quals1(int const * p); |
| 145 | char* quals1(int * p); |
| 146 | |
| 147 | int* quals2(int const * const * pp); |
| 148 | char* quals2(int * * pp); |
| 149 | |
| 150 | int* quals3(int const * * const * ppp); |
| 151 | char* quals3(int *** ppp); |
| 152 | |
| 153 | void test_quals(int * p, int * * pp, int * * * ppp) { |
| 154 | char* q1 = quals1(p); |
| 155 | char* q2 = quals2(pp); |
| 156 | char* q3 = quals3(ppp); |
| 157 | } |
| 158 | |
| 159 | // Test overloading based on qualification ranking (C++ 13.3.2)p3. |
| 160 | int* quals_rank1(int const * p); |
| 161 | float* quals_rank1(int const volatile *p); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 162 | char* quals_rank1(char*); |
| 163 | double* quals_rank1(const char*); |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 164 | |
| 165 | int* quals_rank2(int const * const * pp); |
| 166 | float* quals_rank2(int * const * pp); |
| 167 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 168 | void quals_rank3(int const * const * const volatile * p); // expected-note{{candidate function}} |
| 169 | void quals_rank3(int const * const volatile * const * p); // expected-note{{candidate function}} |
| 170 | |
| 171 | void quals_rank3(int const *); // expected-note{{candidate function}} |
| 172 | void quals_rank3(int volatile *); // expected-note{{candidate function}} |
| 173 | |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 174 | void test_quals_ranking(int * p, int volatile *pq, int * * pp, int * * * ppp) { |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 175 | int* q1 = quals_rank1(p); |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 176 | float* q2 = quals_rank1(pq); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 177 | double* q3 = quals_rank1("string literal"); |
| 178 | char a[17]; |
| 179 | const char* ap = a; |
| 180 | char* q4 = quals_rank1(a); |
| 181 | double* q5 = quals_rank1(ap); |
| 182 | |
| 183 | float* q6 = quals_rank2(pp); |
| 184 | |
| 185 | quals_rank3(ppp); // expected-error {{call to 'quals_rank3' is ambiguous; candidates are:}} |
| 186 | |
| 187 | quals_rank3(p); // expected-error {{call to 'quals_rank3' is ambiguous; candidates are:}} |
| 188 | quals_rank3(pq); |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 189 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 190 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 191 | // Test overloading based on derived-to-base conversions |
| 192 | class A { }; |
| 193 | class B : public A { }; |
| 194 | class C : public B { }; |
| 195 | class D : public C { }; |
| 196 | |
| 197 | int* derived1(A*); |
| 198 | char* derived1(const A*); |
| 199 | float* derived1(void*); |
| 200 | |
| 201 | int* derived2(A*); |
| 202 | float* derived2(B*); |
| 203 | |
| 204 | int* derived3(A*); |
| 205 | float* derived3(const B*); |
| 206 | char* derived3(C*); |
| 207 | |
| 208 | void test_derived(B* b, B const* bc, C* c, const C* cc, void* v, D* d) { |
| 209 | int* d1 = derived1(b); |
| 210 | char* d2 = derived1(bc); |
| 211 | int* d3 = derived1(c); |
| 212 | char* d4 = derived1(cc); |
| 213 | float* d5 = derived1(v); |
| 214 | |
| 215 | float* d6 = derived2(b); |
| 216 | float* d7 = derived2(c); |
| 217 | |
| 218 | char* d8 = derived3(d); |
| 219 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 220 | |
| 221 | // Test overloading of references. |
| 222 | // (FIXME: tests binding to determine candidate sets, not overload |
| 223 | // resolution per se). |
| 224 | int* intref(int&); |
| 225 | float* intref(const int&); |
| 226 | |
| 227 | void intref_test() { |
| 228 | float* ir1 = intref(5); |
| 229 | float* ir2 = intref(5.5); |
| 230 | } |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 231 | |
| 232 | // Test reference binding vs. standard conversions. |
| 233 | int& bind_vs_conv(const double&); |
| 234 | float& bind_vs_conv(int); |
| 235 | |
| 236 | void bind_vs_conv_test() |
| 237 | { |
| 238 | int& i1 = bind_vs_conv(1.0f); |
| 239 | float& f1 = bind_vs_conv((short)1); |
| 240 | } |
| 241 | |
| 242 | // Test that cv-qualifiers get subsumed in the reference binding. |
| 243 | struct X { }; |
| 244 | struct Y { }; |
| 245 | struct Z : X, Y { }; |
| 246 | |
| 247 | int& cvqual_subsume(X&); // expected-note{{candidate function}} |
| 248 | float& cvqual_subsume(const Y&); // expected-note{{candidate function}} |
| 249 | |
| 250 | int& cvqual_subsume2(const X&); |
| 251 | float& cvqual_subsume2(const volatile Y&); |
| 252 | |
| 253 | Z get_Z(); |
| 254 | |
| 255 | void cvqual_subsume_test(Z z) { |
| 256 | cvqual_subsume(z); // expected-error{{call to 'cvqual_subsume' is ambiguous; candidates are:}} |
| 257 | int& x = cvqual_subsume2(get_Z()); // okay: only binds to the first one |
| 258 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 259 | |
| 260 | // Test overloading with cv-qualification differences in reference |
| 261 | // binding. |
| 262 | int& cvqual_diff(X&); |
| 263 | float& cvqual_diff(const X&); |
| 264 | |
| 265 | void cvqual_diff_test(X x, Z z) { |
| 266 | int& i1 = cvqual_diff(x); |
| 267 | int& i2 = cvqual_diff(z); |
| 268 | } |
| 269 | |
| 270 | // Test overloading with derived-to-base differences in reference |
| 271 | // binding. |
| 272 | struct Z2 : Z { }; |
| 273 | |
| 274 | int& db_rebind(X&); |
| 275 | long& db_rebind(Y&); |
| 276 | float& db_rebind(Z&); |
| 277 | |
| 278 | void db_rebind_test(Z2 z2) { |
| 279 | float& f1 = db_rebind(z2); |
| 280 | } |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 281 | |
| 282 | class string { }; |
| 283 | class opt : public string { }; |
| 284 | |
| 285 | struct SR { |
| 286 | SR(const string&); |
| 287 | }; |
| 288 | |
| 289 | void f(SR) { } |
| 290 | |
| 291 | void g(opt o) { |
| 292 | f(o); |
| 293 | } |