Clang Project

clang_source_code/test/SemaCXX/vector.cpp
1// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -verify %s
2// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -verify -std=c++98 %s
3// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -verify -std=c++11 %s
4
5typedef char char16 __attribute__ ((__vector_size__ (16)));
6typedef long long longlong16 __attribute__ ((__vector_size__ (16)));
7typedef char char16_e __attribute__ ((__ext_vector_type__ (16)));
8typedef long long longlong16_e __attribute__ ((__ext_vector_type__ (2)));
9
10// Test overloading and function calls with vector types.
11void f0(char16);
12
13void f0_test(char16 c16, longlong16 ll16, char16_e c16e, longlong16_e ll16e) {
14  f0(c16);
15  f0(ll16);
16  f0(c16e);
17  f0(ll16e);
18}
19
20int &f1(char16);
21float &f1(longlong16);
22
23void f1_test(char16 c16, longlong16 ll16, char16_e c16e, longlong16_e ll16e) {
24  int &ir1 = f1(c16);
25  float &fr1 = f1(ll16);
26  int &ir2 = f1(c16e);
27  float &fr2 = f1(ll16e);
28}
29
30void f2(char16_e); // expected-note{{no known conversion from 'longlong16_e' (vector of 2 'long long' values) to 'char16_e' (vector of 16 'char' values) for 1st argument}} \
31       // expected-note{{candidate function not viable: no known conversion from 'convertible_to<longlong16_e>' to 'char16_e' (vector of 16 'char' values) for 1st argument}}
32
33void f2_test(char16 c16, longlong16 ll16, char16_e c16e, longlong16_e ll16e) {
34  f2(c16);
35  f2(ll16);
36  f2(c16e);
37  f2(ll16e); // expected-error{{no matching function}}
38  f2('a');
39  f2(17);
40}
41
42// Test the conditional operator with vector types.
43void conditional(bool Cond, char16 c16, longlong16 ll16, char16_e c16e,
44                 longlong16_e ll16e) {
45  // Conditional operators with the same type.
46  __typeof__(Cond? c16 : c16) *c16p1 = &c16;
47  __typeof__(Cond? ll16 : ll16) *ll16p1 = &ll16;
48  __typeof__(Cond? c16e : c16e) *c16ep1 = &c16e;
49  __typeof__(Cond? ll16e : ll16e) *ll16ep1 = &ll16e;
50
51  // Conditional operators with similar types.
52  __typeof__(Cond? c16 : c16e) *c16ep2 = &c16e;
53  __typeof__(Cond? c16e : c16) *c16ep3 = &c16e;
54  __typeof__(Cond? ll16 : ll16e) *ll16ep2 = &ll16e;
55  __typeof__(Cond? ll16e : ll16) *ll16ep3 = &ll16e;
56
57  // Conditional operators with compatible types under -flax-vector-conversions (default)
58  (void)(Cond? c16 : ll16);
59  (void)(Cond? ll16e : c16e);
60  (void)(Cond? ll16e : c16);
61}
62
63// Test C++ cast'ing of vector types.
64void casts(longlong16 ll16, longlong16_e ll16e) {
65  // C-style casts.
66  (void)(char16)ll16;
67  (void)(char16_e)ll16;
68  (void)(longlong16)ll16;
69  (void)(longlong16_e)ll16;
70  (void)(char16)ll16e;
71  (void)(char16_e)ll16e;
72  (void)(longlong16)ll16e;
73  (void)(longlong16_e)ll16e;
74
75  // Function-style casts.
76  (void)char16(ll16);
77  (void)char16_e(ll16);
78  (void)longlong16(ll16);
79  (void)longlong16_e(ll16);
80  (void)char16(ll16e);
81  (void)char16_e(ll16e);
82  (void)longlong16(ll16e);
83  (void)longlong16_e(ll16e);
84
85  // static_cast
86  (void)static_cast<char16>(ll16);
87  (void)static_cast<char16_e>(ll16);
88  (void)static_cast<longlong16>(ll16);
89  (void)static_cast<longlong16_e>(ll16);
90  (void)static_cast<char16>(ll16e);
91  (void)static_cast<char16_e>(ll16e); // expected-error{{static_cast from 'longlong16_e' (vector of 2 'long long' values) to 'char16_e' (vector of 16 'char' values) is not allowed}}
92  (void)static_cast<longlong16>(ll16e);
93  (void)static_cast<longlong16_e>(ll16e);
94
95  // reinterpret_cast
96  (void)reinterpret_cast<char16>(ll16);
97  (void)reinterpret_cast<char16_e>(ll16);
98  (void)reinterpret_cast<longlong16>(ll16);
99  (void)reinterpret_cast<longlong16_e>(ll16);
100  (void)reinterpret_cast<char16>(ll16e);
101  (void)reinterpret_cast<char16_e>(ll16e);
102  (void)reinterpret_cast<longlong16>(ll16e);
103  (void)reinterpret_cast<longlong16_e>(ll16e);
104}
105
106template<typename T>
107struct convertible_to { // expected-note 3 {{candidate function (the implicit copy assignment operator) not viable}}
108#if __cplusplus >= 201103L // C++11 or later
109// expected-note@-2 3 {{candidate function (the implicit move assignment operator) not viable}}
110#endif
111  operator T() const;
112};
113
114void test_implicit_conversions(bool Cond, char16 c16, longlong16 ll16,
115                               char16_e c16e, longlong16_e ll16e,
116                               convertible_to<char16> to_c16,
117                               convertible_to<longlong16> to_ll16,
118                               convertible_to<char16_e> to_c16e,
119                               convertible_to<longlong16_e> to_ll16e,
120                               convertible_to<char16&> rto_c16,
121                               convertible_to<char16_e&> rto_c16e) {
122  f0(to_c16);
123  f0(to_ll16);
124  f0(to_c16e);
125  f0(to_ll16e);
126  f2(to_c16);
127  f2(to_ll16);
128  f2(to_c16e);
129  f2(to_ll16e); // expected-error{{no matching function}}
130
131  (void)(c16 == c16e);
132  (void)(c16 == to_c16);
133  (void)+to_c16;
134  (void)-to_c16;
135  (void)~to_c16;
136  (void)(to_c16 == to_c16e);
137  (void)(to_c16 != to_c16e);
138  (void)(to_c16 <  to_c16e);
139  (void)(to_c16 <= to_c16e);
140  (void)(to_c16 >  to_c16e);
141  (void)(to_c16 >= to_c16e);
142  (void)(to_c16 + to_c16);
143  (void)(to_c16 - to_c16);
144  (void)(to_c16 * to_c16);
145  (void)(to_c16 / to_c16);
146  (void)(rto_c16 = to_c16); // expected-error{{no viable overloaded '='}}
147  (void)(rto_c16 += to_c16);
148  (void)(rto_c16 -= to_c16);
149  (void)(rto_c16 *= to_c16);
150  (void)(rto_c16 /= to_c16);
151
152  (void)+to_c16e;
153  (void)-to_c16e;
154  (void)~to_c16e;
155  (void)(to_c16e == to_c16e);
156  (void)(to_c16e != to_c16e);
157  (void)(to_c16e <  to_c16e);
158  (void)(to_c16e <= to_c16e);
159  (void)(to_c16e >  to_c16e);
160  (void)(to_c16e >= to_c16e);
161  (void)(to_c16e + to_c16);
162  (void)(to_c16e - to_c16);
163  (void)(to_c16e * to_c16);
164  (void)(to_c16e / to_c16);
165  (void)(rto_c16e = to_c16); // expected-error{{no viable overloaded '='}}
166  (void)(rto_c16e += to_c16);
167  (void)(rto_c16e -= to_c16);
168  (void)(rto_c16e *= to_c16);
169  (void)(rto_c16e /= to_c16);
170
171  (void)+to_c16;
172  (void)-to_c16;
173  (void)~to_c16;
174  (void)(to_c16 == to_c16e);
175  (void)(to_c16 != to_c16e);
176  (void)(to_c16 <  to_c16e);
177  (void)(to_c16 <= to_c16e);
178  (void)(to_c16 >  to_c16e);
179  (void)(to_c16 >= to_c16e);
180  (void)(to_c16 + to_c16e);
181  (void)(to_c16 - to_c16e);
182  (void)(to_c16 * to_c16e);
183  (void)(to_c16 / to_c16e);
184  (void)(rto_c16 = c16e); // expected-error{{no viable overloaded '='}}
185  (void)(rto_c16 += to_c16e);
186  (void)(rto_c16 -= to_c16e);
187  (void)(rto_c16 *= to_c16e);
188  (void)(rto_c16 /= to_c16e);
189
190  (void)(Cond? to_c16 : to_c16e);
191  (void)(Cond? to_ll16e : to_ll16);
192
193  // These 2 are convertible with -flax-vector-conversions (default)
194  (void)(Cond? to_c16 : to_ll16);
195  (void)(Cond? to_c16e : to_ll16e);
196}
197
198typedef float fltx2 __attribute__((__vector_size__(8)));
199typedef float fltx4 __attribute__((__vector_size__(16)));
200typedef double dblx2 __attribute__((__vector_size__(16)));
201typedef double dblx4 __attribute__((__vector_size__(32)));
202
203void accept_fltx2(fltx2); // expected-note{{candidate function not viable: no known conversion from 'double' to 'fltx2' (vector of 2 'float' values) for 1st argument}}
204void accept_fltx4(fltx4);
205void accept_dblx2(dblx2);
206void accept_dblx4(dblx4);
207void accept_bool(bool); // expected-note{{candidate function not viable: no known conversion from 'fltx2' (vector of 2 'float' values) to 'bool' for 1st argument}}
208
209void test(fltx2 fltx2_val, fltx4 fltx4_val, dblx2 dblx2_val, dblx4 dblx4_val) {
210  // Exact matches
211  accept_fltx2(fltx2_val);
212  accept_fltx4(fltx4_val);
213  accept_dblx2(dblx2_val);
214  accept_dblx4(dblx4_val);
215
216  // Same-size conversions
217  // FIXME: G++ rejects these conversions, we accept them. Revisit this!
218  accept_fltx4(dblx2_val);
219  accept_dblx2(fltx4_val);
220
221  // Conversion to bool.
222  accept_bool(fltx2_val); // expected-error{{no matching function for call to 'accept_bool'}}
223
224  // Scalar-to-vector conversions.
225  accept_fltx2(1.0); // expected-error{{no matching function for call to 'accept_fltx2'}}
226}
227
228typedef int intx4 __attribute__((__vector_size__(16)));
229typedef int inte4 __attribute__((__ext_vector_type__(4)));
230typedef int flte4 __attribute__((__ext_vector_type__(4)));
231
232void test_mixed_vector_types(fltx4 f, intx4 n, flte4 g, flte4 m) {
233  (void)(f == g);
234  (void)(g != f);
235  (void)(f <= g);
236  (void)(g >= f);
237  (void)(f < g);
238  (void)(g > f);
239
240  (void)(+g);
241  (void)(-g);
242
243  (void)(f + g);
244  (void)(f - g);
245  (void)(f * g);
246  (void)(f / g);
247  (void)(f = g);
248  (void)(f += g);
249  (void)(f -= g);
250  (void)(f *= g);
251  (void)(f /= g);
252
253
254  (void)(n == m);
255  (void)(m != n);
256  (void)(n <= m);
257  (void)(m >= n);
258  (void)(n < m);
259  (void)(m > n);
260
261  (void)(+m);
262  (void)(-m);
263  (void)(~m);
264
265  (void)(n + m);
266  (void)(n - m);
267  (void)(n * m);
268  (void)(n / m);
269  (void)(n % m);
270  (void)(n = m);
271  (void)(n += m);
272  (void)(n -= m);
273  (void)(n *= m);
274  (void)(n /= m);
275}
276
277template<typename T> void test_pseudo_dtor_tmpl(T *ptr) {
278  ptr->~T();
279  (*ptr).~T();
280}
281
282void test_pseudo_dtor(fltx4 *f) {
283  f->~fltx4();
284  (*f).~fltx4();
285  test_pseudo_dtor_tmpl(f);
286}
287
288// PR16204
289typedef __attribute__((ext_vector_type(4))) int vi4;
290const int &reference_to_vec_element = vi4(1).x;
291
292// PR12649
293typedef bool bad __attribute__((__vector_size__(16)));  // expected-error {{invalid vector element type 'bool'}}
294
295namespace Templates {
296template <typename Elt, unsigned Size>
297struct TemplateVectorType {
298  typedef Elt __attribute__((__vector_size__(Size))) type;
299};
300
301template <int N, typename T>
302struct PR15730 {
303  typedef T __attribute__((vector_size(N * sizeof(T)))) type;
304  typedef T __attribute__((vector_size(8192))) type2;
305  typedef T __attribute__((vector_size(3))) type3;
306};
307
308void Init() {
309  const TemplateVectorType<float, 32>::type Works = {};
310  const TemplateVectorType<int, 32>::type Works2 = {};
311  // expected-error@298 {{invalid vector element type 'bool'}}
312  // expected-note@+1 {{in instantiation of template class 'Templates::TemplateVectorType<bool, 32>' requested here}}
313  const TemplateVectorType<bool, 32>::type NoBool;
314  // expected-error@298 {{invalid vector element type 'int __attribute__((ext_vector_type(4)))' (vector of 4 'int' values)}}
315  // expected-note@+1 {{in instantiation of template class 'Templates::TemplateVectorType<int __attribute__((ext_vector_type(4))), 32>' requested here}}
316  const TemplateVectorType<vi4, 32>::type NoComplex;
317  // expected-error@298 {{vector size not an integral multiple of component size}}
318  // expected-note@+1 {{in instantiation of template class 'Templates::TemplateVectorType<int, 33>' requested here}}
319  const TemplateVectorType<int, 33>::type BadSize;
320  // expected-error@298 {{vector size too large}}
321  // expected-note@+1 {{in instantiation of template class 'Templates::TemplateVectorType<int, 8192>' requested here}}
322  const TemplateVectorType<int, 8192>::type TooLarge;
323  // expected-error@298 {{zero vector size}}
324  // expected-note@+1 {{in instantiation of template class 'Templates::TemplateVectorType<int, 0>' requested here}}
325  const TemplateVectorType<int, 0>::type Zero;
326
327  // expected-error@304 {{vector size too large}}
328  // expected-error@305 {{vector size not an integral multiple of component size}}
329  // expected-note@+1 {{in instantiation of template class 'Templates::PR15730<8, int>' requested here}}
330  const PR15730<8, int>::type PR15730_1 = {};
331  // expected-error@304 {{vector size too large}}
332  // expected-note@+1 {{in instantiation of template class 'Templates::PR15730<8, char>' requested here}}
333  const PR15730<8, char>::type2 PR15730_2 = {};
334}
335
336} // namespace Templates
337