Debian Codegraph @

gcc/testsuite/g++.dg/cpp1z/constexpr-asm-5.C

raw
1 // PR c++/118277
2 // { dg-do compile { target c++11 } }
3 // { dg-options "" }
4 // Override any default-'-fno-exceptions':
5 // { dg-additional-options "-fexceptions -fno-implicit-constexpr" }
6
7 struct A {};
8 struct B { int size; };
9 struct C { constexpr int size () const { return 0; } };
10 struct D { constexpr int size () const { return 0; } int data; };
11 struct E { int size = 0;
12 constexpr const char *data () const { return ""; } };
13 struct F { constexpr const char *size () const { return ""; }
14 constexpr const char *data () const { return ""; } };
15 struct G { constexpr long size () const { return 0; }
16 constexpr float data () const { return 0.0f; } };
17 struct H { short size () const { return 0; }
18 constexpr const char *data () const { return ""; } };
19 struct I { constexpr signed char size () const { return 0; }
20 const char *data () const { return ""; } };
21 struct J { constexpr int size () const { return j ? throw 1 : 0; } // { dg-error "expression '<throw-expression>' is not a constant expression" }
22 constexpr const char *data () const { return ""; };
23 constexpr J (int x) : j (x) {}
24 int j; };
25 struct K { constexpr operator int () { return 4; } };
26 struct L { constexpr operator const char * () { return "test"; } };
27 struct M { constexpr K size () const { return {}; }
28 constexpr L data () const { return {}; } };
29 #if __cpp_constexpr_dynamic_alloc >= 201907L
30 struct N { constexpr int size () const { return 3; }
31 constexpr const char *data () const { return new char[3] { 'b', 'a', 'd' }; } }; // { dg-error "'\\\* N\\\(\\\).N::data\\\(\\\)' is not a constant expression because allocated storage has not been deallocated" "" { target c++20 } }
32 #endif
33 constexpr const char a[] = { 't', 'e', 's', 't' };
34 struct O { constexpr int size () const { return 4; }
35 constexpr const char *data () const { return a; } };
36 struct P { constexpr int size () const { return 4 - p; }
37 constexpr const char *data () const { return &a[p]; }
38 constexpr P (int x) : p (x) {}
39 int p; };
40 struct Q { constexpr int size () const { return 4 - q; }
41 constexpr const char *data () const { return &"test"[q]; }
42 constexpr Q (int x) : q (x) {}
43 int q; };
44 struct R { constexpr int size () const { return 4 - r; }
45 constexpr const char *d () const { return "test"; }
46 constexpr const char *data () const { return d () + r; }
47 constexpr R (int x) : r (x) {}
48 int r; };
49 struct S { constexpr float size (float) const { return 42.0f; }
50 constexpr int size (void * = nullptr) const { return 4; }
51 constexpr double data (double) const { return 42.0; }
52 constexpr const char *data (int = 0) const { return "test"; } };
53 using size_t = decltype (sizeof (0));
54 struct string_view {
55 size_t s;
56 const char *d;
57 constexpr string_view () : s (0), d (nullptr) {}
58 constexpr string_view (const char *p) : s (__builtin_strlen (p)), d (p) {}
59 constexpr string_view (size_t l, const char *p) : s (l), d (p) {}
60 constexpr size_t size () const noexcept { return s; }
61 constexpr const char *data () const noexcept { return d; }
62 };
63 template <typename T, size_t N>
64 struct array {
65 constexpr size_t size () const { return N; }
66 constexpr const T *data () const { return a; }
67 const T a[N];
68 };
69 struct U { constexpr operator const char * () const { return u; }
70 char u[5] = "test"; };
71 #if __cplusplus >= 201402L
72 struct V { constexpr auto size () const { return K {}; }
73 constexpr auto data () const { return U {}; } };
74 #endif
75 struct W { constexpr int size (int) const { return 4; }
76 constexpr const char *data () const { return "test"; } };
77 struct X { constexpr int size () const { return 4; }
78 constexpr const char *data (int) const { return "test"; } };
79 struct Y { constexpr int size () { return 4; }
80 constexpr const char *data (int) { return "test"; } };
81 #if __cpp_concepts >= 201907L
82 struct Z { constexpr int size (auto...) const { return 4; }
83 constexpr const char *data (auto...) const { return "test"; } };
84 #endif
85
86 void
87 foo ()
88 {
89 int v1, v2;
90 asm ("");
91 asm (("")); // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" }
92 // { dg-error "request for member 'size' in '\\\(\\\"\\\"\\\)', which is of non-class type 'const char \\\[1\\\]'" "" { target *-*-* } .-1 }
93 asm (("" + 0)); // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" }
94 // { dg-error "request for member 'size' in '\\\(const char\\\*\\\)\\\"\\\"', which is of non-class type 'const char\\\*'" "" { target *-*-* } .-1 }
95 asm ((0) ::); // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" }
96 // { dg-error "request for member 'size' in '0', which is of non-class type 'int'" "" { target *-*-* } .-1 }
97 asm ("" : (A {}) (v1)); // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" }
98 // { dg-error "'struct A' has no member named 'size'" "" { target *-*-* } .-1 }
99 asm ("" : : (B {}) (1)); // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" }
100 // { dg-error "'struct B' has no member named 'data'" "" { target *-*-* } .-1 }
101 asm ("" ::: (C {})); // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" }
102 // { dg-error "'struct C' has no member named 'data'" "" { target *-*-* } .-1 }
103 asm ((D {})); // { dg-error "'D\\\(\\\).D::data' cannot be used as a function" }
104 asm ("" : (E {}) (v1)); // { dg-error "'E\\\{0\\\}.E::size' cannot be used as a function" }
105 __asm ("" :: (F {}) (1)); // { dg-error "constexpr string 'size\\\(\\\)' must be implicitly convertible to 'std::size_t'" }
106 // { dg-error "could not convert 'F\\\(\\\).F::size\\\(\\\)' from 'const char\\\*' to '\[^']*'" "" { target *-*-* } .-1 }
107 // { dg-error "conversion from 'const char\\\*' to '\[^']*' in a converted constant expression" "" { target *-*-* } .-2 }
108 asm ("" : : : (G {})); // { dg-error "constexpr string 'data\\\(\\\)' must be implicitly convertible to 'const char\\\*'" }
109 // { dg-error "could not convert 'G\\\(\\\).G::data\\\(\\\)' from 'float' to 'const char\\\*'" "" { target *-*-* } .-1 }
110 asm ("" : "=r" (v1), (H {}) (v2)); // { dg-error "call to non-'constexpr' function 'short int H::size\\\(\\\) const'" }
111 // { dg-error "constexpr string 'size\\\(\\\)' must be a constant expression" "" { target *-*-* } .-1 }
112 asm ((I {})); // { dg-error "call to non-'constexpr' function 'const char\\\* I::data\\\(\\\) const'" }
113 // { dg-error "constexpr string 'data\\\(\\\)' must be a core constant expression" "" { target *-*-* } .-1 }
114
115 asm ((J (0)));
116 asm ("" :: (J (1)) (1)); // { dg-error "constexpr string 'size\\\(\\\)' must be a constant expression" }
117 asm ((M {}));
118 #if __cpp_constexpr_dynamic_alloc >= 201907L
119 asm ((N {})); // { dg-error "constexpr string 'data\\\(\\\)\\\[0\\\]' must be a constant expression" "" { target c++20 } }
120 #endif
121 asm ((O {}));
122 asm ((P (0)));
123 asm ((P (2)));
124 asm ((Q (0)));
125 asm ((Q (1)));
126 asm ((R (0)));
127 asm ((R (2)));
128 asm ((S {}));
129
130 asm ((string_view {}));
131 asm ((string_view ("test")));
132 asm ((string_view ("א")));
133 asm ((string_view (0, nullptr)));
134 asm ((string_view (4, "testwithextrachars")));
135 asm ((string_view (42, "test"))); // { dg-error "array subscript value '41' is outside the bounds of array type 'const char \\\[5\\\]'" }
136 // { dg-error "constexpr string 'data\\\(\\\)\\\[41\\\]' must be a constant expression" "" { target *-*-* } .-1 }
137
138 asm ((array<char, 2> { 'O', 'K' }));
139 asm ((array<wchar_t, 2> { L'O', L'K' })); // { dg-error "constexpr string 'data\\\(\\\)' must be implicitly convertible to 'const char\\\*'" }
140 // { dg-error "could not convert 'array<wchar_t, 2>{const wchar_t \\\[2\\\]{\[0-9]+, \[0-9]+}}.array<wchar_t, 2>::data\\\(\\\)' from 'const wchar_t\\\*' to 'const char\\\*'" "" { target *-*-* } .-1 }
141 asm ((array<char, 4> { 't', 'e', 's', 't' }));
142 {
143 constexpr auto a = array<char, 4> { 't', 'e', 's', 't' };
144 asm ((a));
145 }
146
147 #if __cplusplus >= 201402L
148 asm ((V {}));
149 #endif
150 asm ((W {})); // { dg-error "no matching function for call to 'W::size\\\(\\\)'" }
151 asm ((X {})); // { dg-error "no matching function for call to 'X::data\\\(\\\)'" }
152 asm ((Y {})); // { dg-error "no matching function for call to 'Y::data\\\(\\\)'" }
153 #if __cpp_concepts >= 201907L
154 asm ((Z {}) :::);
155 #endif
156 }
157
158 template <typename TT>
159 void
160 bar ()
161 {
162 int v1, v2;
163 asm ("");
164 asm (("")); // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" }
165 // { dg-error "request for member 'size' in '\\\(\\\"\\\"\\\)', which is of non-class type 'const char \\\[1\\\]'" "" { target *-*-* } .-1 }
166 asm (("" + 0)); // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" }
167 // { dg-error "request for member 'size' in '\\\(const char\\\*\\\)\\\"\\\"', which is of non-class type 'const char\\\*'" "" { target *-*-* } .-1 }
168 asm ((0) ::); // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" }
169 // { dg-error "request for member 'size' in '0', which is of non-class type 'int'" "" { target *-*-* } .-1 }
170 asm ("" : (A {}) (v1)); // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" }
171 // { dg-error "'struct A' has no member named 'size'" "" { target *-*-* } .-1 }
172 asm ("" : : (B {}) (1)); // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" }
173 // { dg-error "'struct B' has no member named 'data'" "" { target *-*-* } .-1 }
174 asm ("" ::: (C {})); // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" }
175 // { dg-error "'struct C' has no member named 'data'" "" { target *-*-* } .-1 }
176 asm ((D {})); // { dg-error "'D\\\(\\\).D::data' cannot be used as a function" }
177 asm ("" : (E {}) (v1)); // { dg-error "'E\\\{0\\\}.E::size' cannot be used as a function" }
178 __asm ("" :: (F {}) (1)); // { dg-error "constexpr string 'size\\\(\\\)' must be implicitly convertible to 'std::size_t'" }
179 // { dg-error "could not convert 'F\\\(\\\).F::size\\\(\\\)' from 'const char\\\*' to '\[^']*'" "" { target *-*-* } .-1 }
180 // { dg-error "conversion from 'const char\\\*' to '\[^']*' in a converted constant expression" "" { target *-*-* } .-2 }
181 asm ("" : : : (G {})); // { dg-error "constexpr string 'data\\\(\\\)' must be implicitly convertible to 'const char\\\*'" }
182 // { dg-error "could not convert 'G\\\(\\\).G::data\\\(\\\)' from 'float' to 'const char\\\*'" "" { target *-*-* } .-1 }
183 asm ("" : "=r" (v1), (H {}) (v2)); // { dg-error "call to non-'constexpr' function 'short int H::size\\\(\\\) const'" }
184 // { dg-error "constexpr string 'size\\\(\\\)' must be a constant expression" "" { target *-*-* } .-1 }
185 asm ((I {})); // { dg-error "call to non-'constexpr' function 'const char\\\* I::data\\\(\\\) const'" }
186 // { dg-error "constexpr string 'data\\\(\\\)' must be a core constant expression" "" { target *-*-* } .-1 }
187
188 asm ((J (0)));
189 asm ("" :: (J (1)) (1)); // { dg-error "constexpr string 'size\\\(\\\)' must be a constant expression" }
190 asm ((M {}));
191 #if __cpp_constexpr_dynamic_alloc >= 201907L
192 asm ((N {})); // { dg-error "constexpr string 'data\\\(\\\)\\\[0\\\]' must be a constant expression" "" { target c++20 } }
193 #endif
194 asm ((O {}));
195 asm ((P (0)));
196 asm ((P (2)));
197 asm ((Q (0)));
198 asm ((Q (1)));
199 asm ((R (0)));
200 asm ((R (2)));
201 asm ((S {}));
202
203 asm ((string_view {}));
204 asm ((string_view ("test")));
205 asm ((string_view ("א")));
206 asm ((string_view (0, nullptr)));
207 asm ((string_view (4, "testwithextrachars")));
208 asm ((string_view (42, "test"))); // { dg-error "array subscript value '41' is outside the bounds of array type 'const char \\\[5\\\]'" }
209 // { dg-error "constexpr string 'data\\\(\\\)\\\[41\\\]' must be a constant expression" "" { target *-*-* } .-1 }
210
211 asm ((array<char, 2> { 'O', 'K' }));
212 asm ((array<wchar_t, 2> { L'O', L'K' })); // { dg-error "constexpr string 'data\\\(\\\)' must be implicitly convertible to 'const char\\\*'" }
213 // { dg-error "could not convert 'array<wchar_t, 2>{const wchar_t \\\[2\\\]{\[0-9]+, \[0-9]+}}.array<wchar_t, 2>::data\\\(\\\)' from 'const wchar_t\\\*' to 'const char\\\*'" "" { target *-*-* } .-1 }
214 asm ((array<char, 4> { 't', 'e', 's', 't' }));
215 {
216 constexpr auto a = array<char, 4> { 't', 'e', 's', 't' };
217 asm ((a));
218 }
219
220 #if __cplusplus >= 201402L
221 asm ((V {}));
222 #endif
223 asm ((W {})); // { dg-error "no matching function for call to 'W::size\\\(\\\)'" }
224 asm ((X {})); // { dg-error "no matching function for call to 'X::data\\\(\\\)'" }
225 asm ((Y {})); // { dg-error "no matching function for call to 'Y::data\\\(\\\)'" }
226 #if __cpp_concepts >= 201907L
227 asm ((Z {}) :::);
228 #endif
229 }
230
231 void
232 baz ()
233 {
234 bar<int> ();
235 }
236
237 namespace NN
238 {
239 template <typename T>
240 struct A {
241 constexpr int size () const = delete;
242 constexpr const char *data () const { return "test"; } };
243 #if __cpp_concepts >= 201907L
244 template <typename T>
245 struct B {
246 constexpr int size () const { return 4; }
247 constexpr const char *data () const requires false { return "test"; } };
248 #endif
249 class C {
250 constexpr int size () const = delete;
251 constexpr const char *data () const { return "test"; } };
252 #if __cplusplus >= 201402L
253 struct D {
254 constexpr int size () { return 4; }
255 constexpr int size () const { return 3; }
256 constexpr const char *data () { return "test"; }
257 constexpr const char *data () const { return "ehlo"; } };
258 #endif
259 struct E {
260 constexpr int size () const { return 4; }
261 constexpr const char *data () const { return "test"; } };
262 constexpr E operator ""_myd (const char *, size_t) { return E {}; }
263 constexpr E operator + (const char *, const E &) { return E {}; }
264 struct H {
265 static constexpr int size () { return 7; }
266 static constexpr const char *data () { return "message"; } };
267 struct I {
268 static constexpr int size () { return 0; }
269 static constexpr const char *data () { return nullptr; } };
270 #if __cplusplus >= 201402L
271 struct J {
272 static constexpr int size () { return 0; }
273 static constexpr const char *data (int x = 0) { if (x) return nullptr; else throw 1; } }; // { dg-error "expression '<throw-expression>' is not a constant expression" "" { target c++14 } }
274 #endif
275 #if __cpp_if_consteval >= 202106L
276 struct K {
277 static constexpr int size () { if consteval { return 4; } else { throw 1; } }
278 static constexpr const char *data () { return "test"; }
279 };
280 struct L {
281 static constexpr int size () { return 4; }
282 static constexpr const char *data () { if consteval { return "test"; } else { throw 1; } }
283 };
284 struct M {
285 static constexpr int size () { if consteval { throw 1; } else { return 4; } } // { dg-error "expression '<throw-expression>' is not a constant expression" "" { target c++23 } }
286 static constexpr const char *data () { return "test"; }
287 };
288 struct N {
289 static constexpr int size () { return 4; }
290 static constexpr const char *data () { if consteval { throw 1; } else { return "test"; } } // { dg-error "expression '<throw-expression>' is not a constant expression" "" { target c++23 } }
291 };
292 #endif
293 struct O { constexpr int operator () () const { return 12; } };
294 struct P { constexpr const char *operator () () const { return "another test"; } };
295 struct Q { O size; P data; };
296 constexpr int get_size () { return 16; }
297 constexpr const char *get_data () { return "yet another test"; }
298 struct R { int (*size) () = NN::get_size;
299 const char *(*data) () = NN::get_data; };
300
301 void
302 bar ()
303 {
304 asm ((A<int> {})); // { dg-error "use of deleted function 'constexpr int NN::A<T>::size\\\(\\\) const \\\[with T = int\\\]'" }
305 #if __cpp_concepts >= 201907L
306 asm ((B<short> {})); // { dg-error "no matching function for call to 'NN::B<short int>::data\\\(\\\)'" "" { target c++20 } }
307 #endif
308 asm ((C {})); // { dg-error "use of deleted function 'constexpr int NN::C::size\\\(\\\) const'" }
309 // { dg-error "'constexpr const char\\\* NN::C::data\\\(\\\) const' is private within this context" "" { target *-*-* } .-1 }
310 #if __cplusplus >= 201402L
311 asm ((D {}));
312 #endif
313 asm (("foo"_myd));
314 asm (("foo" + E {}));
315 asm ((H {}));
316 asm ((I {}));
317 #if __cplusplus >= 201402L
318 asm ((J {})); // { dg-error "constexpr string 'data\\\(\\\)' must be a core constant expression" "" { target c++14 } }
319 #endif
320 #if __cpp_if_consteval >= 202106L
321 asm ((K {}));
322 asm ((L {}));
323 asm ((M {})); // { dg-error "constexpr string 'size\\\(\\\)' must be a constant expression" "" { target c++23 } }
324 asm ((N {})); // { dg-error "constexpr string 'data\\\(\\\)\\\[0\\\]' must be a constant expression" "" { target c++23 } }
325 #endif
326 asm ((Q {}));
327 asm ((R {}));
328 }
329
330 template <typename TT, typename UU, typename VV, typename WW>
331 void
332 baz ()
333 {
334 asm ((A<VV> {})); // { dg-error "use of deleted function 'constexpr int NN::A<T>::size\\\(\\\) const \\\[with T = int\\\]'" }
335 #if __cpp_concepts >= 201907L
336 asm ((B<WW> {})); // { dg-error "no matching function for call to 'NN::B<short int>::data\\\(\\\)'" "" { target c++20 } }
337 #endif
338 asm ((C {})); // { dg-error "use of deleted function 'constexpr int NN::C::size\\\(\\\) const'" }
339 // { dg-error "'constexpr const char\\\* NN::C::data\\\(\\\) const' is private within this context" "" { target *-*-* } .-1 }
340 #if __cplusplus >= 201402L
341 asm ((D {}));
342 #endif
343 asm (("foo"_myd));
344 asm (("foo" + E {}));
345 asm ((H {}));
346 asm ((I {}));
347 #if __cplusplus >= 201402L
348 asm ((J {})); // { dg-error "constexpr string 'data\\\(\\\)' must be a core constant expression" "" { target c++14 } }
349 #endif
350 #if __cpp_if_consteval >= 202106L
351 asm ((K {}));
352 asm ((L {}));
353 asm ((M {})); // { dg-error "constexpr string 'size\\\(\\\)' must be a constant expression" "" { target c++23 } }
354 asm ((N {})); // { dg-error "constexpr string 'data\\\(\\\)\\\[0\\\]' must be a constant expression" "" { target c++23 } }
355 #endif
356 asm ((Q {}));
357 asm ((R {}));
358 asm ((TT {}));
359 asm ((UU {})); // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" }
360 // { dg-error "request for member 'size' in '0', which is of non-class type 'long int'" "" { target *-*-* } .-1 }
361 }
362 void
363 qux ()
364 {
365 baz<E, long, int, short> ();
366 }
367 }