Line data Source code
1 : /* Report error messages, build initializers, and perform
2 : some front-end optimizations for C++ compiler.
3 : Copyright (C) 1987-2026 Free Software Foundation, Inc.
4 : Hacked by Michael Tiemann (tiemann@cygnus.com)
5 :
6 : This file is part of GCC.
7 :
8 : GCC is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3, or (at your option)
11 : any later version.
12 :
13 : GCC is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with GCC; see the file COPYING3. If not see
20 : <http://www.gnu.org/licenses/>. */
21 :
22 :
23 : /* This file is part of the C++ front end.
24 : It contains routines to build C++ expressions given their operands,
25 : including computing the types of the result, C and C++ specific error
26 : checks, and some optimization. */
27 :
28 : #include "config.h"
29 : #include "system.h"
30 : #include "coretypes.h"
31 : #include "cp-tree.h"
32 : #include "stor-layout.h"
33 : #include "varasm.h"
34 : #include "intl.h"
35 : #include "gcc-rich-location.h"
36 : #include "target.h"
37 :
38 : static tree
39 : process_init_constructor (tree type, tree init, int nested, int flags,
40 : tsubst_flags_t complain);
41 :
42 :
43 : /* Print an error message stemming from an attempt to use
44 : BASETYPE as a base class for TYPE. */
45 :
46 : tree
47 29 : error_not_base_type (tree basetype, tree type)
48 : {
49 29 : if (TREE_CODE (basetype) == FUNCTION_DECL)
50 0 : basetype = DECL_CONTEXT (basetype);
51 29 : error ("type %qT is not a base type for type %qT", basetype, type);
52 29 : return error_mark_node;
53 : }
54 :
55 : tree
56 1115 : binfo_or_else (tree base, tree type)
57 : {
58 1115 : tree binfo = lookup_base (type, base, ba_unique,
59 : NULL, tf_warning_or_error);
60 :
61 1115 : if (binfo == error_mark_node)
62 : return NULL_TREE;
63 1115 : else if (!binfo)
64 0 : error_not_base_type (base, type);
65 : return binfo;
66 : }
67 :
68 : /* According to ARM $7.1.6, "A `const' object may be initialized, but its
69 : value may not be changed thereafter. */
70 :
71 : void
72 259 : cxx_readonly_error (location_t loc, tree arg, enum lvalue_use errstring)
73 : {
74 :
75 : /* This macro is used to emit diagnostics to ensure that all format
76 : strings are complete sentences, visible to gettext and checked at
77 : compile time. */
78 :
79 : #define ERROR_FOR_ASSIGNMENT(LOC, AS, ASM, IN, DE, ARG) \
80 : do { \
81 : switch (errstring) \
82 : { \
83 : case lv_assign: \
84 : error_at (LOC, AS, ARG); \
85 : break; \
86 : case lv_asm: \
87 : error_at (LOC, ASM, ARG); \
88 : break; \
89 : case lv_increment: \
90 : error_at (LOC, IN, ARG); \
91 : break; \
92 : case lv_decrement: \
93 : error_at (LOC, DE, ARG); \
94 : break; \
95 : default: \
96 : gcc_unreachable (); \
97 : } \
98 : } while (0)
99 :
100 : /* Handle C++-specific things first. */
101 :
102 259 : if (VAR_P (arg)
103 15 : && DECL_LANG_SPECIFIC (arg)
104 0 : && DECL_IN_AGGR_P (arg)
105 259 : && !TREE_STATIC (arg))
106 0 : ERROR_FOR_ASSIGNMENT (loc,
107 : G_("assignment of constant field %qD"),
108 : G_("constant field %qD used as %<asm%> output"),
109 : G_("increment of constant field %qD"),
110 : G_("decrement of constant field %qD"),
111 : arg);
112 259 : else if (INDIRECT_REF_P (arg)
113 38 : && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
114 291 : && (VAR_P (TREE_OPERAND (arg, 0))
115 19 : || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
116 17 : ERROR_FOR_ASSIGNMENT (loc,
117 : G_("assignment of read-only reference %qD"),
118 : G_("read-only reference %qD used as %<asm%> output"),
119 : G_("increment of read-only reference %qD"),
120 : G_("decrement of read-only reference %qD"),
121 : TREE_OPERAND (arg, 0));
122 242 : else if (is_stub_object (arg))
123 : {
124 7 : gcc_assert (errstring == lv_assign);
125 7 : error_at (loc, "assignment to read-only type %qT", TREE_TYPE (arg));
126 : }
127 : else
128 235 : readonly_error (loc, arg, errstring);
129 259 : }
130 :
131 : /* If TYPE has abstract virtual functions, issue an error about trying
132 : to create an object of that type. DECL is the object declared, or
133 : NULL_TREE if the declaration is unavailable, in which case USE specifies
134 : the kind of invalid use. Returns 1 if an error occurred; zero if
135 : all was well. */
136 :
137 : static int
138 409229411 : abstract_virtuals_error (tree decl, tree type, abstract_class_use use,
139 : tsubst_flags_t complain)
140 : {
141 409229411 : vec<tree, va_gc> *pure;
142 :
143 409229411 : if (TREE_CODE (type) == ARRAY_TYPE)
144 : {
145 964854 : decl = NULL_TREE;
146 964854 : use = ACU_ARRAY;
147 964854 : type = strip_array_types (type);
148 : }
149 :
150 : /* This function applies only to classes. Any other entity can never
151 : be abstract. */
152 409229411 : if (!CLASS_TYPE_P (type))
153 : return 0;
154 :
155 78222032 : if (ANON_AGGR_TYPE_P (type))
156 : {
157 : /* [class.union.anon]/1: Each object of such an unnamed type shall be
158 : such an unnamed object. */
159 420 : auto_diagnostic_group d;
160 420 : location_t aloc
161 420 : = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TYPE_MAIN_VARIANT (type)));
162 420 : if (decl
163 366 : && VAR_P (decl)
164 358 : && DECL_NAME (decl) == NULL_TREE
165 755 : && ANON_UNION_TYPE_P (type))
166 : /* Unnamed variables are ok, those are assumed to be the variable
167 : created for namespace scope anonymous union. For temporaries
168 : even when in the end they create VAR_DECLs with NULL DECL_NAME,
169 : this function is called first with !decl and so invalid code
170 : can be diagnosed below at that point. */;
171 85 : else if (!(complain & tf_error))
172 : return 1;
173 85 : else if (ANON_UNION_TYPE_P (type))
174 : {
175 51 : if (!decl)
176 32 : switch (use)
177 : {
178 20 : default:
179 20 : error ("temporary object with anonymous union type %qT", type);
180 20 : break;
181 4 : case ACU_CATCH:
182 4 : error ("%<catch%> parameter with anonymous union type %qT",
183 : type);
184 4 : break;
185 4 : case ACU_THROW:
186 4 : error ("%<throw%> operand has anonymous union type %qT", type);
187 4 : break;
188 4 : case ACU_ARRAY:
189 4 : error ("object with array of anonymous union type %qT", type);
190 4 : break;
191 : }
192 19 : else if (VAR_P (decl))
193 15 : error_at (location_of (decl),
194 : "declaration of variable %qD with anonymous union type "
195 : "%qT", decl, type);
196 4 : else if (TREE_CODE (decl) == PARM_DECL && DECL_NAME (decl))
197 2 : error_at (location_of (decl),
198 : "declaration of parameter %qD with anonymous union type "
199 : "%qT", decl, type);
200 2 : else if (TREE_CODE (decl) == PARM_DECL)
201 2 : error_at (location_of (decl),
202 : "declaration of a parameter with anonymous union type "
203 : "%qT", type);
204 51 : inform (aloc, "anonymous union declared here");
205 51 : if (decl)
206 19 : TREE_TYPE (decl) = error_mark_node;
207 51 : return 1;
208 : }
209 : else
210 : {
211 34 : if (!decl)
212 22 : switch (use)
213 : {
214 14 : default:
215 14 : error ("temporary object with anonymous struct type %qT",
216 : type);
217 14 : break;
218 4 : case ACU_CATCH:
219 4 : error ("%<catch%> parameter with anonymous struct type %qT",
220 : type);
221 4 : break;
222 2 : case ACU_THROW:
223 2 : error ("%<throw%> operand has anonymous struct type %qT",
224 : type);
225 2 : break;
226 2 : case ACU_ARRAY:
227 2 : error ("object with array of anonymous struct type %qT", type);
228 2 : break;
229 : }
230 12 : else if (VAR_P (decl))
231 8 : error_at (location_of (decl),
232 : "declaration of variable %qD with anonymous struct type "
233 : "%qT", decl, type);
234 4 : else if (TREE_CODE (decl) == PARM_DECL && DECL_NAME (decl))
235 2 : error_at (location_of (decl),
236 : "declaration of parameter %qD with anonymous struct type "
237 : "%qT", decl, type);
238 2 : else if (TREE_CODE (decl) == PARM_DECL)
239 2 : error_at (location_of (decl),
240 : "declaration of a parameter with anonymous struct type "
241 : "%qT", type);
242 34 : inform (aloc, "anonymous struct declared here");
243 34 : if (decl)
244 12 : TREE_TYPE (decl) = error_mark_node;
245 34 : return 1;
246 : }
247 420 : }
248 :
249 78221947 : type = TYPE_MAIN_VARIANT (type);
250 :
251 : #if 0
252 : /* Instantiation here seems to be required by the standard,
253 : but breaks e.g. boost::bind. FIXME! */
254 : /* In SFINAE, non-N3276 context, force instantiation. */
255 : if (!(complain & (tf_error|tf_decltype)))
256 : complete_type (type);
257 : #endif
258 :
259 78221947 : if (!TYPE_SIZE (type))
260 : /* TYPE is being defined, and during that time
261 : CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
262 : return 0;
263 :
264 78221884 : pure = CLASSTYPE_PURE_VIRTUALS (type);
265 78221884 : if (!pure)
266 : return 0;
267 :
268 233 : if (!(complain & tf_error))
269 : return 1;
270 :
271 111 : auto_diagnostic_group d;
272 111 : if (decl)
273 : {
274 54 : if (VAR_P (decl))
275 9 : error ("cannot declare variable %q+D to be of abstract "
276 : "type %qT", decl, type);
277 45 : else if (TREE_CODE (decl) == PARM_DECL)
278 : {
279 27 : if (DECL_NAME (decl))
280 24 : error ("cannot declare parameter %q+D to be of abstract type %qT",
281 : decl, type);
282 : else
283 3 : error ("cannot declare parameter to be of abstract type %qT",
284 : type);
285 : }
286 18 : else if (TREE_CODE (decl) == FIELD_DECL)
287 15 : error ("cannot declare field %q+D to be of abstract type %qT",
288 : decl, type);
289 3 : else if (TREE_CODE (decl) == FUNCTION_DECL
290 3 : && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
291 0 : error ("invalid abstract return type for member function %q+#D", decl);
292 3 : else if (TREE_CODE (decl) == FUNCTION_DECL)
293 3 : error ("invalid abstract return type for function %q+#D", decl);
294 0 : else if (identifier_p (decl))
295 : /* Here we do not have location information. */
296 0 : error ("invalid abstract type %qT for %qE", type, decl);
297 : else
298 0 : error ("invalid abstract type for %q+D", decl);
299 : }
300 57 : else switch (use)
301 : {
302 12 : case ACU_ARRAY:
303 12 : error ("creating array of %qT, which is an abstract class type", type);
304 12 : break;
305 9 : case ACU_CAST:
306 9 : error ("invalid cast to abstract class type %qT", type);
307 9 : break;
308 0 : case ACU_NEW:
309 0 : error ("invalid new-expression of abstract class type %qT", type);
310 0 : break;
311 0 : case ACU_RETURN:
312 0 : error ("invalid abstract return type %qT", type);
313 0 : break;
314 0 : case ACU_PARM:
315 0 : error ("invalid abstract parameter type %qT", type);
316 0 : break;
317 6 : case ACU_THROW:
318 6 : error ("expression of abstract class type %qT cannot "
319 : "be used in throw-expression", type);
320 6 : break;
321 3 : case ACU_CATCH:
322 3 : error ("cannot declare %<catch%> parameter to be of abstract "
323 : "class type %qT", type);
324 3 : break;
325 27 : default:
326 27 : error ("cannot construct an object of abstract type %qT", type);
327 : }
328 :
329 : /* Only go through this once. */
330 111 : if (pure->length ())
331 : {
332 48 : unsigned ix;
333 48 : tree fn;
334 :
335 48 : inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
336 : "because the following virtual functions are pure within %qT:",
337 : type);
338 :
339 48 : auto_diagnostic_nesting_level adnl;
340 144 : FOR_EACH_VEC_ELT (*pure, ix, fn)
341 96 : if (! DECL_CLONED_FUNCTION_P (fn)
342 48 : || DECL_COMPLETE_DESTRUCTOR_P (fn))
343 48 : inform (DECL_SOURCE_LOCATION (fn), "%#qD", fn);
344 :
345 : /* Now truncate the vector. This leaves it non-null, so we know
346 : there are pure virtuals, but empty so we don't list them out
347 : again. */
348 48 : pure->truncate (0);
349 48 : }
350 :
351 111 : return 1;
352 : }
353 :
354 : int
355 352507244 : abstract_virtuals_error (tree decl, tree type,
356 : tsubst_flags_t complain /* = tf_warning_or_error */)
357 : {
358 352507244 : return abstract_virtuals_error (decl, type, ACU_UNKNOWN, complain);
359 : }
360 :
361 : int
362 56722167 : abstract_virtuals_error (abstract_class_use use, tree type,
363 : tsubst_flags_t complain /* = tf_warning_or_error */)
364 : {
365 56722167 : return abstract_virtuals_error (NULL_TREE, type, use, complain);
366 : }
367 :
368 :
369 : /* Print an inform about the declaration of the incomplete type TYPE. */
370 :
371 : void
372 1225 : cxx_incomplete_type_inform (const_tree type)
373 : {
374 1225 : if (!TYPE_MAIN_DECL (type))
375 : return;
376 :
377 1135 : location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type));
378 1135 : tree ptype = strip_top_quals (const_cast<tree> (type));
379 :
380 : /* When defining a template, current_class_type will be the pattern on
381 : the template definition, while non-self-reference usages of this
382 : template will be an instantiation; we should pull out the pattern to
383 : compare against. And for partial specs we should use the loc of the
384 : partial spec rather than the primary template. */
385 1135 : tree ttype = NULL_TREE;
386 1135 : tree tinfo = TYPE_TEMPLATE_INFO (ptype);
387 1135 : if (tinfo)
388 : {
389 303 : tree tmpl = TI_TEMPLATE (tinfo);
390 303 : if (PRIMARY_TEMPLATE_P (tmpl) && TI_PARTIAL_INFO (tinfo))
391 : {
392 37 : tree partial = TI_TEMPLATE (TI_PARTIAL_INFO (tinfo));
393 37 : loc = DECL_SOURCE_LOCATION (partial);
394 37 : ttype = TREE_TYPE (partial);
395 : }
396 : else
397 266 : ttype = TREE_TYPE (tmpl);
398 : }
399 :
400 1135 : if (current_class_type
401 279 : && TYPE_BEING_DEFINED (current_class_type)
402 1366 : && (same_type_p (ptype, current_class_type)
403 135 : || (ttype && same_type_p (ttype, current_class_type))))
404 106 : inform (loc, "definition of %q#T is not complete until "
405 : "the closing brace", ptype);
406 : else
407 : {
408 1029 : if (!tinfo)
409 787 : inform (loc, "forward declaration of %q#T", ptype);
410 : else
411 242 : inform (loc, "declaration of %q#T", ptype);
412 :
413 : /* If there's a similar-looking complete type attached
414 : to a different module, point at that as a suggestion. */
415 1029 : if (modules_p () && TYPE_NAMESPACE_SCOPE_P (ptype))
416 : {
417 24 : tree result = lookup_qualified_name (CP_TYPE_CONTEXT (ptype),
418 24 : TYPE_IDENTIFIER (ptype),
419 : LOOK_want::TYPE);
420 24 : if (TREE_CODE (result) == TREE_LIST)
421 72 : for (; result; result = TREE_CHAIN (result))
422 : {
423 48 : tree cand = TREE_VALUE (result);
424 :
425 : /* Typedefs are not likely intended to correspond. */
426 48 : if (is_typedef_decl (STRIP_TEMPLATE (cand))
427 42 : || DECL_ALIAS_TEMPLATE_P (cand))
428 6 : continue;
429 :
430 : /* Only look at templates if type was a template. */
431 42 : if ((tinfo != nullptr) != (TREE_CODE (cand) == TEMPLATE_DECL))
432 3 : continue;
433 :
434 : /* If we're looking for a template specialisation,
435 : only consider matching specialisations. */
436 39 : if (tinfo)
437 : {
438 27 : tree t = lookup_template_class (cand, TI_ARGS (tinfo),
439 : NULL_TREE, NULL_TREE,
440 : tf_none);
441 27 : if (t == error_mark_node
442 27 : || !CLASS_TYPE_P (t)
443 54 : || TYPE_BEING_DEFINED (t))
444 0 : continue;
445 :
446 27 : if (CLASSTYPE_TEMPLATE_INSTANTIATION (t))
447 : {
448 : /* An uninstantiated template: check if there is a
449 : pattern that could be used. We don't want to
450 : call instantiate_class_template as that could
451 : cause further errors; this is just a hint. */
452 21 : tree part = most_specialized_partial_spec (t, tf_none);
453 27 : cand = (part ? TI_TEMPLATE (part)
454 15 : : CLASSTYPE_TI_TEMPLATE (t));
455 : }
456 : else
457 6 : cand = TYPE_NAME (t);
458 : }
459 :
460 39 : if (!COMPLETE_TYPE_P (TREE_TYPE (cand)))
461 24 : continue;
462 :
463 15 : inform (DECL_SOURCE_LOCATION (cand),
464 : "%q#T has a definition but does not correspond with "
465 : "%q#T because it is attached to a different module",
466 15 : TREE_TYPE (cand), ptype);
467 : }
468 : }
469 : }
470 : }
471 :
472 : /* Print an error message for invalid use of an incomplete type.
473 : VALUE is the expression that was used (or 0 if that isn't known)
474 : and TYPE is the type that was invalid. DIAG_KIND indicates the
475 : type of diagnostic (see diagnostics/kinds.def). */
476 :
477 : bool
478 1226 : cxx_incomplete_type_diagnostic (location_t loc, const_tree value,
479 : const_tree type,
480 : enum diagnostics::kind diag_kind)
481 : {
482 1226 : bool is_decl = false, complained = false;
483 :
484 : /* Avoid duplicate error message. */
485 1226 : if (TREE_CODE (type) == ERROR_MARK)
486 : return false;
487 :
488 1226 : auto_diagnostic_group d;
489 1226 : if (value)
490 : {
491 416 : STRIP_ANY_LOCATION_WRAPPER (value);
492 :
493 416 : if (VAR_P (value)
494 371 : || TREE_CODE (value) == PARM_DECL
495 326 : || TREE_CODE (value) == FIELD_DECL)
496 : {
497 121 : complained = emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (value), 0,
498 : "%qD has incomplete type", value);
499 121 : is_decl = true;
500 : }
501 : }
502 1226 : retry:
503 : /* We must print an error message. Be clever about what it says. */
504 :
505 1306 : switch (TREE_CODE (type))
506 : {
507 960 : case RECORD_TYPE:
508 960 : case UNION_TYPE:
509 960 : case ENUMERAL_TYPE:
510 960 : if (!is_decl)
511 862 : complained = emit_diagnostic (diag_kind, loc, 0,
512 : "invalid use of incomplete type %q#T",
513 : type);
514 960 : if (complained)
515 957 : cxx_incomplete_type_inform (type);
516 : break;
517 :
518 12 : case VOID_TYPE:
519 12 : complained = emit_diagnostic (diag_kind, loc, 0,
520 : "invalid use of %qT", type);
521 12 : break;
522 :
523 134 : case ARRAY_TYPE:
524 134 : if (TYPE_DOMAIN (type))
525 : {
526 80 : type = TREE_TYPE (type);
527 80 : goto retry;
528 : }
529 54 : complained = emit_diagnostic (diag_kind, loc, 0,
530 : "invalid use of array with unspecified bounds");
531 54 : break;
532 :
533 72 : case OFFSET_TYPE:
534 72 : bad_member:
535 72 : {
536 72 : tree member = TREE_OPERAND (value, 1);
537 72 : if (is_overloaded_fn (member) && !flag_ms_extensions)
538 : {
539 69 : gcc_rich_location richloc (loc);
540 : /* If "member" has no arguments (other than "this"), then
541 : add a fix-it hint. */
542 69 : member = MAYBE_BASELINK_FUNCTIONS (member);
543 69 : if (TREE_CODE (member) == FUNCTION_DECL
544 57 : && DECL_OBJECT_MEMBER_FUNCTION_P (member)
545 126 : && type_num_arguments (TREE_TYPE (member)) == 1)
546 54 : richloc.add_fixit_insert_after ("()");
547 69 : complained = emit_diagnostic (diag_kind, &richloc, 0,
548 : "invalid use of member function %qD "
549 : "(did you forget the %<()%> ?)", member);
550 69 : }
551 : else
552 3 : complained = emit_diagnostic (diag_kind, loc, 0,
553 : "invalid use of member %qD "
554 : "(did you forget the %<&%> ?)", member);
555 : }
556 : break;
557 :
558 31 : case TEMPLATE_TYPE_PARM:
559 31 : if (is_auto (type))
560 : {
561 19 : if (CLASS_PLACEHOLDER_TEMPLATE (type))
562 3 : complained = emit_diagnostic (diag_kind, loc, 0,
563 : "invalid use of placeholder %qT", type);
564 : else
565 16 : complained = emit_diagnostic (diag_kind, loc, 0,
566 : "invalid use of %qT", type);
567 : }
568 : else
569 12 : complained = emit_diagnostic (diag_kind, loc, 0,
570 : "invalid use of template type parameter %qT", type);
571 : break;
572 :
573 3 : case BOUND_TEMPLATE_TEMPLATE_PARM:
574 3 : complained = emit_diagnostic (diag_kind, loc, 0,
575 : "invalid use of template template parameter %qT",
576 3 : TYPE_NAME (type));
577 3 : break;
578 :
579 0 : case TYPE_PACK_EXPANSION:
580 0 : complained = emit_diagnostic (diag_kind, loc, 0,
581 : "invalid use of pack expansion %qT", type);
582 0 : break;
583 :
584 16 : case TYPENAME_TYPE:
585 16 : case DECLTYPE_TYPE:
586 16 : complained = emit_diagnostic (diag_kind, loc, 0,
587 : "invalid use of dependent type %qT", type);
588 16 : break;
589 :
590 150 : case LANG_TYPE:
591 150 : if (type == init_list_type_node)
592 : {
593 3 : complained = emit_diagnostic (diag_kind, loc, 0,
594 : "invalid use of brace-enclosed initializer list");
595 3 : break;
596 : }
597 147 : gcc_assert (type == unknown_type_node);
598 147 : if (value && TREE_CODE (value) == COMPONENT_REF)
599 72 : goto bad_member;
600 75 : else if (value && TREE_CODE (value) == ADDR_EXPR)
601 27 : complained = emit_diagnostic (diag_kind, loc, 0,
602 : "address of overloaded function with no contextual "
603 : "type information");
604 48 : else if (value && TREE_CODE (value) == OVERLOAD)
605 42 : complained = emit_diagnostic (diag_kind, loc, 0,
606 : "overloaded function with no contextual type information");
607 : else
608 6 : complained = emit_diagnostic (diag_kind, loc, 0,
609 : "insufficient contextual information to determine type");
610 : break;
611 :
612 0 : default:
613 0 : gcc_unreachable ();
614 : }
615 :
616 1226 : return complained;
617 1226 : }
618 :
619 : /* Print an error message for invalid use of an incomplete type.
620 : VALUE is the expression that was used (or 0 if that isn't known)
621 : and TYPE is the type that was invalid. */
622 :
623 : void
624 58 : cxx_incomplete_type_error (location_t loc, const_tree value, const_tree type)
625 : {
626 58 : cxx_incomplete_type_diagnostic (loc, value, type, diagnostics::kind::error);
627 58 : }
628 :
629 :
630 : /* We've just initialized subobject SUB; also insert a TARGET_EXPR with an
631 : EH-only cleanup for SUB. Because of EH region nesting issues, we need to
632 : make the cleanup conditional on a flag that we will clear once the object is
633 : fully initialized, so push a new flag onto FLAGS. */
634 :
635 : static void
636 327393 : maybe_push_temp_cleanup (tree sub, vec<tree,va_gc> **flags)
637 : {
638 327393 : if (!flag_exceptions)
639 : return;
640 651914 : if (tree cleanup
641 325957 : = cxx_maybe_build_cleanup (sub, tf_warning_or_error))
642 : {
643 213 : tree tx = get_internal_target_expr (boolean_true_node);
644 213 : tree flag = TARGET_EXPR_SLOT (tx);
645 213 : TARGET_EXPR_CLEANUP (tx) = build3 (COND_EXPR, void_type_node,
646 : flag, cleanup, void_node);
647 213 : add_stmt (tx);
648 213 : vec_safe_push (*flags, flag);
649 : }
650 : }
651 :
652 : /* F is something added to a cleanup flags vec by maybe_push_temp_cleanup or
653 : build_vec_init. Return the code to disable the cleanup it controls. */
654 :
655 : tree
656 1202 : build_disable_temp_cleanup (tree f)
657 : {
658 1202 : tree d = f;
659 1202 : tree i = boolean_false_node;
660 1202 : if (TREE_CODE (f) == TREE_LIST)
661 : {
662 : /* To disable a build_vec_init cleanup, set
663 : iterator = maxindex. */
664 989 : d = TREE_PURPOSE (f);
665 989 : i = TREE_VALUE (f);
666 989 : ggc_free (f);
667 : }
668 1202 : return build2 (MODIFY_EXPR, TREE_TYPE (d), d, i);
669 : }
670 :
671 : /* The recursive part of split_nonconstant_init. DEST is an lvalue
672 : expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
673 : Return true if the whole of the value was initialized by the
674 : generated statements or modifying DECL_INITIAL. */
675 :
676 : static bool
677 343843 : split_nonconstant_init_1 (tree dest, tree init, bool last,
678 : vec<tree,va_gc> **flags)
679 : {
680 343843 : unsigned HOST_WIDE_INT idx, tidx = HOST_WIDE_INT_M1U;
681 343843 : tree field_index, value;
682 343843 : tree type = TREE_TYPE (dest);
683 343843 : tree inner_type = NULL;
684 343843 : bool array_type_p = false;
685 343843 : bool complete_p = true;
686 343843 : HOST_WIDE_INT num_split_elts = 0;
687 343843 : tree last_split_elt = NULL_TREE;
688 :
689 343843 : switch (TREE_CODE (type))
690 : {
691 6344 : case ARRAY_TYPE:
692 6344 : inner_type = TREE_TYPE (type);
693 6344 : array_type_p = true;
694 6344 : if ((TREE_SIDE_EFFECTS (init)
695 2295 : && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
696 7943 : || vla_type_p (type))
697 : {
698 764 : if (!TYPE_DOMAIN (type)
699 3 : && TREE_CODE (init) == CONSTRUCTOR
700 767 : && CONSTRUCTOR_NELTS (init))
701 : {
702 : /* Flexible array. */
703 3 : cp_complete_array_type (&type, init, /*default*/true);
704 3 : dest = build1 (VIEW_CONVERT_EXPR, type, dest);
705 : }
706 :
707 : /* For an array, we only need/want a single cleanup region rather
708 : than one per element. build_vec_init will handle it. */
709 764 : tree code = build_vec_init (dest, NULL_TREE, init, false, 1,
710 : tf_warning_or_error, flags);
711 764 : add_stmt (code);
712 764 : return true;
713 : }
714 : /* FALLTHRU */
715 :
716 341791 : case RECORD_TYPE:
717 341791 : case UNION_TYPE:
718 341791 : case QUAL_UNION_TYPE:
719 981375 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
720 : field_index, value)
721 : {
722 : /* The current implementation of this algorithm assumes that
723 : the field was set for all the elements. This is usually done
724 : by process_init_constructor. */
725 639584 : gcc_assert (field_index);
726 :
727 639584 : if (!array_type_p)
728 625801 : inner_type = TREE_TYPE (field_index);
729 :
730 639584 : tree sub;
731 639584 : if (array_type_p)
732 13783 : sub = build4 (ARRAY_REF, inner_type, dest, field_index,
733 : NULL_TREE, NULL_TREE);
734 : else
735 625801 : sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
736 : NULL_TREE);
737 :
738 1915476 : bool elt_last = last && idx == CONSTRUCTOR_NELTS (init) - 1;
739 :
740 : /* We need to see sub-array TARGET_EXPR before cp_fold_r so we can
741 : handle cleanup flags properly. */
742 639584 : gcc_checking_assert (!target_expr_needs_replace (value));
743 :
744 639584 : if (TREE_CODE (value) == CONSTRUCTOR)
745 : {
746 2804 : if (!split_nonconstant_init_1 (sub, value, elt_last, flags)
747 : /* For flexible array member with initializer we
748 : can't remove the initializer, because only the
749 : initializer determines how many elements the
750 : flexible array member has. */
751 2804 : || (!array_type_p
752 1158 : && TREE_CODE (inner_type) == ARRAY_TYPE
753 1037 : && TYPE_DOMAIN (inner_type) == NULL
754 49 : && TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
755 49 : && COMPLETE_TYPE_P (TREE_TYPE (value))
756 49 : && !integer_zerop (TYPE_SIZE (TREE_TYPE (value)))
757 49 : && elt_last
758 49 : && TYPE_HAS_TRIVIAL_DESTRUCTOR
759 : (strip_array_types (inner_type))))
760 : complete_p = false;
761 : else
762 : {
763 : /* Mark element for removal. */
764 1262 : last_split_elt = field_index;
765 1262 : CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE;
766 1262 : if (idx < tidx)
767 : tidx = idx;
768 1262 : num_split_elts++;
769 : }
770 : }
771 636780 : else if (tree vi = get_vec_init_expr (value))
772 : {
773 31 : add_stmt (expand_vec_init_expr (sub, vi, tf_warning_or_error,
774 : flags));
775 :
776 : /* Mark element for removal. */
777 31 : last_split_elt = field_index;
778 31 : CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE;
779 31 : if (idx < tidx)
780 : tidx = idx;
781 31 : num_split_elts++;
782 : }
783 636749 : else if (!initializer_constant_valid_p (value, inner_type))
784 : {
785 518904 : tree code;
786 :
787 : /* Push cleanups for any preceding members with constant
788 : initialization. */
789 518904 : if (CLASS_TYPE_P (type))
790 508562 : for (tree prev = (last_split_elt ?
791 209720 : DECL_CHAIN (last_split_elt)
792 508562 : : TYPE_FIELDS (type));
793 1876 : ; prev = DECL_CHAIN (prev))
794 : {
795 510438 : prev = next_aggregate_field (prev);
796 510438 : if (prev == field_index)
797 : break;
798 1876 : tree ptype = TREE_TYPE (prev);
799 1876 : if (TYPE_P (ptype) && type_build_dtor_call (ptype))
800 : {
801 43 : tree pcref = build3 (COMPONENT_REF, ptype, dest, prev,
802 : NULL_TREE);
803 43 : maybe_push_temp_cleanup (pcref, flags);
804 : }
805 1876 : }
806 :
807 : /* Mark element for removal. */
808 518904 : CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE;
809 518904 : if (idx < tidx)
810 : tidx = idx;
811 :
812 518904 : if (TREE_CODE (field_index) == RANGE_EXPR)
813 : {
814 : /* Use build_vec_init to initialize a range. */
815 0 : tree low = TREE_OPERAND (field_index, 0);
816 0 : tree hi = TREE_OPERAND (field_index, 1);
817 0 : sub = build4 (ARRAY_REF, inner_type, dest, low,
818 : NULL_TREE, NULL_TREE);
819 0 : sub = cp_build_addr_expr (sub, tf_warning_or_error);
820 0 : tree max = size_binop (MINUS_EXPR, hi, low);
821 0 : code = build_vec_init (sub, max, value, false, 0,
822 : tf_warning_or_error);
823 0 : add_stmt (code);
824 0 : if (tree_fits_shwi_p (max))
825 0 : num_split_elts += tree_to_shwi (max);
826 : }
827 : else
828 : {
829 : /* We may need to add a copy constructor call if
830 : the field has [[no_unique_address]]. */
831 518904 : if (unsafe_return_slot_p (sub))
832 : {
833 : /* But not if the initializer is an implicit ctor call
834 : we just built in digest_init. */
835 497 : if (TREE_CODE (value) == TARGET_EXPR
836 497 : && TARGET_EXPR_LIST_INIT_P (value)
837 520 : && make_safe_copy_elision (sub, value))
838 23 : goto build_init;
839 :
840 474 : if (TREE_CODE (value) == TARGET_EXPR)
841 : /* We have to add this constructor, so we will not
842 : elide. */
843 474 : TARGET_EXPR_ELIDING_P (value) = false;
844 :
845 474 : tree name = (DECL_FIELD_IS_BASE (field_index)
846 474 : ? base_ctor_identifier
847 474 : : complete_ctor_identifier);
848 474 : releasing_vec args = make_tree_vector_single (value);
849 474 : code = build_special_member_call
850 474 : (sub, name, &args, inner_type,
851 : LOOKUP_NORMAL, tf_warning_or_error);
852 474 : }
853 : else
854 : {
855 518407 : build_init:
856 518430 : code = cp_build_init_expr (sub, value);
857 : }
858 518904 : code = build_stmt (input_location, EXPR_STMT, code);
859 518904 : add_stmt (code);
860 518904 : if (!elt_last)
861 327350 : maybe_push_temp_cleanup (sub, flags);
862 : }
863 :
864 518904 : last_split_elt = field_index;
865 518904 : num_split_elts++;
866 : }
867 : }
868 341791 : if (num_split_elts == 1)
869 189713 : CONSTRUCTOR_ELTS (init)->ordered_remove (tidx);
870 152078 : else if (num_split_elts > 1)
871 : {
872 : /* Perform the delayed ordered removal of non-constant elements
873 : we split out. */
874 445825 : for (idx = tidx; idx < CONSTRUCTOR_NELTS (init); ++idx)
875 330827 : if (CONSTRUCTOR_ELT (init, idx)->index == NULL_TREE)
876 : ;
877 : else
878 : {
879 343 : *CONSTRUCTOR_ELT (init, tidx) = *CONSTRUCTOR_ELT (init, idx);
880 343 : ++tidx;
881 : }
882 114998 : vec_safe_truncate (CONSTRUCTOR_ELTS (init), tidx);
883 : }
884 : break;
885 :
886 1288 : case VECTOR_TYPE:
887 1288 : if (!initializer_constant_valid_p (init, type))
888 : {
889 1265 : tree code;
890 1265 : tree cons = copy_node (init);
891 1265 : CONSTRUCTOR_ELTS (init) = NULL;
892 1265 : code = build2 (MODIFY_EXPR, type, dest, cons);
893 1265 : code = build_stmt (input_location, EXPR_STMT, code);
894 1265 : add_stmt (code);
895 1265 : num_split_elts += CONSTRUCTOR_NELTS (init);
896 : }
897 : break;
898 :
899 0 : default:
900 0 : gcc_unreachable ();
901 : }
902 :
903 : /* The rest of the initializer is now a constant. */
904 343079 : TREE_CONSTANT (init) = 1;
905 343079 : TREE_SIDE_EFFECTS (init) = 0;
906 :
907 : /* We didn't split out anything. */
908 343079 : if (num_split_elts == 0)
909 : return false;
910 :
911 304711 : return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
912 : num_split_elts, inner_type);
913 : }
914 :
915 : /* A subroutine of store_init_value. Splits non-constant static
916 : initializer INIT into a constant part and generates code to
917 : perform the non-constant part of the initialization to DEST.
918 : Returns the code for the runtime init. */
919 :
920 : tree
921 14768664 : split_nonconstant_init (tree dest, tree init)
922 : {
923 14768664 : tree code;
924 :
925 14768664 : if (TREE_CODE (init) == TARGET_EXPR)
926 754425 : init = TARGET_EXPR_INITIAL (init);
927 14768664 : if (TREE_CODE (init) == CONSTRUCTOR)
928 : {
929 : /* Subobject initializers are not full-expressions. */
930 341039 : auto fe = (make_temp_override
931 341039 : (current_stmt_tree ()->stmts_are_full_exprs_p, 0));
932 :
933 341039 : init = cp_fully_fold_init (init);
934 341039 : code = push_stmt_list ();
935 :
936 : /* If the complete object is an array, build_vec_init's cleanup is
937 : enough. Otherwise, collect flags for disabling subobject
938 : cleanups once the complete object is fully constructed. */
939 341039 : vec<tree, va_gc> *flags = nullptr;
940 341039 : if (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE)
941 336661 : flags = make_tree_vector ();
942 :
943 : /* We are about to call split_nonconstant_init_1 which might
944 : set DECL_INITIAL, so make sure we aren't overwriting an
945 : existing initializer. Also, if we split out everything,
946 : we clear INIT so won't set DECL_INITIAL below. Make
947 : sure it's null so that we're not forgetting to clear it. */
948 341039 : gcc_assert (!(VAR_P (dest) && DECL_INITIAL (dest)));
949 :
950 341039 : if (split_nonconstant_init_1 (dest, init, true, &flags))
951 191362 : init = NULL_TREE;
952 :
953 1016631 : for (tree f : flags)
954 928 : finish_expr_stmt (build_disable_temp_cleanup (f));
955 341039 : release_tree_vector (flags);
956 :
957 341039 : code = pop_stmt_list (code);
958 341039 : if (VAR_P (dest) && !is_local_temp (dest))
959 : {
960 : /* If we are initializing an array, split_nonconstant_init_1
961 : might've delegated to build_vec_init in which case it always
962 : returns true so we clear INIT. But if we're initializing
963 : a static array, build_vec_init can put constant initializers
964 : into DECL_INITIAL. Clearing it would mean losing some of the
965 : initializers as in c++/126335. */
966 339253 : if (init)
967 149298 : DECL_INITIAL (dest) = init;
968 339253 : if (TREE_SIDE_EFFECTS (code))
969 303596 : TREE_READONLY (dest) = 0;
970 : }
971 1786 : else if (init)
972 : {
973 379 : tree ie = cp_build_init_expr (dest, init);
974 379 : code = add_stmt_to_compound (ie, code);
975 : }
976 341039 : }
977 14427625 : else if (TREE_CODE (init) == STRING_CST
978 14427625 : && array_of_runtime_bound_p (TREE_TYPE (dest)))
979 25 : code = build_vec_init (dest, NULL_TREE, init, /*value-init*/false,
980 : /*from array*/1, tf_warning_or_error);
981 : else
982 14427600 : code = cp_build_init_expr (dest, init);
983 :
984 14768664 : return code;
985 : }
986 :
987 : /* T is the initializer of a constexpr variable. Set CONSTRUCTOR_MUTABLE_POISON
988 : for any CONSTRUCTOR within T that contains (directly or indirectly) a mutable
989 : member, thereby poisoning it so it can't be copied to another a constexpr
990 : variable or read during constexpr evaluation. */
991 :
992 : static void
993 36680532 : poison_mutable_constructors (tree t)
994 : {
995 36680532 : if (TREE_CODE (t) != CONSTRUCTOR)
996 : return;
997 :
998 2681495 : if (cp_has_mutable_p (TREE_TYPE (t)))
999 : {
1000 160 : CONSTRUCTOR_MUTABLE_POISON (t) = true;
1001 :
1002 160 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (t))
1003 352 : for (const constructor_elt &ce : *elts)
1004 198 : poison_mutable_constructors (ce.value);
1005 : }
1006 : }
1007 :
1008 : /* Perform appropriate conversions on the initial value of a variable,
1009 : store it in the declaration DECL,
1010 : and print any error messages that are appropriate.
1011 : If the init is invalid, store an ERROR_MARK.
1012 :
1013 : C++: Note that INIT might be a TREE_LIST, which would mean that it is
1014 : a base class initializer for some aggregate type, hopefully compatible
1015 : with DECL. If INIT is a single element, and DECL is an aggregate
1016 : type, we silently convert INIT into a TREE_LIST, allowing a constructor
1017 : to be called.
1018 :
1019 : If INIT is a TREE_LIST and there is no constructor, turn INIT
1020 : into a CONSTRUCTOR and use standard initialization techniques.
1021 : Perhaps a warning should be generated?
1022 :
1023 : Returns code to be executed if initialization could not be performed
1024 : for static variable. In that case, caller must emit the code. */
1025 :
1026 : tree
1027 53338081 : store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
1028 : {
1029 53338081 : tree value, type;
1030 :
1031 : /* If variable's type was invalidly declared, just ignore it. */
1032 :
1033 53338081 : type = TREE_TYPE (decl);
1034 53338081 : if (TREE_CODE (type) == ERROR_MARK)
1035 : return NULL_TREE;
1036 :
1037 4297486 : if (MAYBE_CLASS_TYPE_P (type))
1038 : {
1039 4267052 : if (TREE_CODE (init) == TREE_LIST)
1040 : {
1041 0 : error ("constructor syntax used, but no constructor declared "
1042 : "for type %qT", type);
1043 0 : init = build_constructor_from_list (init_list_type_node, nreverse (init));
1044 : }
1045 : }
1046 :
1047 : /* End of special C++ code. */
1048 :
1049 53338081 : if (flags & LOOKUP_ALREADY_DIGESTED)
1050 : value = init;
1051 : else
1052 : {
1053 50866643 : if (TREE_STATIC (decl))
1054 31436150 : flags |= LOOKUP_ALLOW_FLEXARRAY_INIT;
1055 : /* Digest the specified initializer into an expression. */
1056 50866643 : value = digest_init_flags (type, init, flags, tf_warning_or_error);
1057 : }
1058 :
1059 : /* Look for braced array initializers for character arrays and
1060 : recursively convert them into STRING_CSTs. */
1061 53338081 : value = braced_lists_to_strings (type, value);
1062 :
1063 53338081 : current_ref_temp_count = 0;
1064 53338081 : value = extend_ref_init_temps (decl, value, cleanups);
1065 :
1066 : /* In C++11 constant expression is a semantic, not syntactic, property.
1067 : In C++98, make sure that what we thought was a constant expression at
1068 : template definition time is still constant and otherwise perform this
1069 : as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
1070 53338081 : if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
1071 : {
1072 36683031 : bool const_init;
1073 36683031 : tree oldval = value;
1074 36683031 : if (DECL_DECLARED_CONSTEXPR_P (decl)
1075 6545533 : || DECL_DECLARED_CONSTINIT_P (decl)
1076 43228157 : || (DECL_IN_AGGR_P (decl)
1077 1286133 : && DECL_INITIALIZED_IN_CLASS_P (decl)))
1078 : {
1079 31423904 : value = fold_non_dependent_expr (value, tf_warning_or_error,
1080 : /*manifestly_const_eval=*/true,
1081 : decl);
1082 31421207 : if (value == error_mark_node)
1083 : ;
1084 : /* Diagnose a non-constant initializer for constexpr variable or
1085 : non-inline in-class-initialized static data member. */
1086 31420941 : else if (!is_constant_expression (value))
1087 : {
1088 : /* Maybe we want to give this message for constexpr variables as
1089 : well, but that will mean a lot of testsuite adjustment. */
1090 267 : if (DECL_DECLARED_CONSTINIT_P (decl))
1091 88 : error_at (location_of (decl),
1092 : "%<constinit%> variable %qD does not have a "
1093 : "constant initializer", decl);
1094 267 : require_constant_expression (value);
1095 267 : value = error_mark_node;
1096 : }
1097 : else
1098 : {
1099 31420674 : value = maybe_constant_init (value, decl, true);
1100 :
1101 : /* In a template we might not have done the necessary
1102 : transformations to make value actually constant,
1103 : e.g. extend_ref_init_temps. */
1104 31420674 : if (!processing_template_decl
1105 31420674 : && !TREE_CONSTANT (value))
1106 : {
1107 2108 : if (DECL_DECLARED_CONSTINIT_P (decl))
1108 41 : error_at (location_of (decl),
1109 : "%<constinit%> variable %qD does not have a "
1110 : "constant initializer", decl);
1111 2108 : value = cxx_constant_init (value, decl);
1112 : }
1113 : }
1114 : }
1115 : else
1116 5259127 : value = fold_non_dependent_init (value, tf_warning_or_error,
1117 : /*manifestly_const_eval=*/true, decl);
1118 36680334 : poison_mutable_constructors (value);
1119 36680334 : const_init = (reduced_constant_expression_p (value)
1120 36680334 : || error_operand_p (value));
1121 36680334 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
1122 : /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
1123 36680334 : if (!TYPE_REF_P (type))
1124 39764877 : TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
1125 36680334 : if (!const_init)
1126 19442256 : value = oldval;
1127 : }
1128 : /* Don't fold initializers of automatic variables in constexpr functions,
1129 : that might fold away something that needs to be diagnosed at constexpr
1130 : evaluation time. */
1131 53335384 : if (!current_function_decl
1132 21533518 : || !DECL_DECLARED_CONSTEXPR_P (current_function_decl)
1133 59712208 : || TREE_STATIC (decl))
1134 46964116 : value = cp_fully_fold_init (value);
1135 :
1136 : /* Handle aggregate NSDMI in non-constant initializers, too. */
1137 53335384 : value = replace_placeholders (value, decl);
1138 :
1139 : /* A COMPOUND_LITERAL_P CONSTRUCTOR is the syntactic form; by the time we get
1140 : here it should have been digested into an actual value for the type. */
1141 53335384 : gcc_checking_assert (TREE_CODE (value) != CONSTRUCTOR
1142 : || processing_template_decl
1143 : || VECTOR_TYPE_P (type)
1144 : || !TREE_HAS_CONSTRUCTOR (value));
1145 :
1146 : /* If the initializer is not a constant, fill in DECL_INITIAL with
1147 : the bits that are constant, and then return an expression that
1148 : will perform the dynamic initialization. */
1149 53335384 : if (value != error_mark_node
1150 53329801 : && !processing_template_decl
1151 104222735 : && (TREE_SIDE_EFFECTS (value)
1152 43975267 : || vla_type_p (type)
1153 43975174 : || ! reduced_constant_expression_p (value)))
1154 14759384 : return split_nonconstant_init (decl, value);
1155 :
1156 : /* DECL may change value; purge caches. */
1157 38576000 : clear_cv_and_fold_caches ();
1158 :
1159 : /* If the value is a constant, just put it in DECL_INITIAL. If DECL
1160 : is an automatic variable, the middle end will turn this into a
1161 : dynamic initialization later. */
1162 38576000 : DECL_INITIAL (decl) = value;
1163 38576000 : return NULL_TREE;
1164 : }
1165 :
1166 :
1167 : /* Give diagnostic about narrowing conversions within { }, or as part of
1168 : a converted constant expression. If CONST_ONLY, only check
1169 : constants. */
1170 :
1171 : bool
1172 56536680 : check_narrowing (tree type, tree init, tsubst_flags_t complain,
1173 : bool const_only/*= false*/)
1174 : {
1175 56536680 : tree ftype = unlowered_expr_type (init);
1176 56536680 : bool ok = true;
1177 56536680 : REAL_VALUE_TYPE d;
1178 :
1179 56488189 : if (((!warn_narrowing || !(complain & tf_warning))
1180 4196297 : && cxx_dialect == cxx98)
1181 56489094 : || !ARITHMETIC_TYPE_P (type)
1182 : /* Don't emit bogus warnings with e.g. value-dependent trees. */
1183 112792533 : || instantiation_dependent_expression_p (init))
1184 437010 : return ok;
1185 :
1186 18 : if (BRACE_ENCLOSED_INITIALIZER_P (init)
1187 56099670 : && TREE_CODE (type) == COMPLEX_TYPE)
1188 : {
1189 0 : tree elttype = TREE_TYPE (type);
1190 0 : if (CONSTRUCTOR_NELTS (init) > 0)
1191 0 : ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value,
1192 : complain);
1193 0 : if (CONSTRUCTOR_NELTS (init) > 1)
1194 0 : ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value,
1195 : complain);
1196 0 : return ok;
1197 : }
1198 :
1199 : /* Even non-dependent expressions can still have template
1200 : codes like CAST_EXPR, so use *_non_dependent_expr to cope. */
1201 56099670 : init = fold_non_dependent_expr (init, complain, /*manifest*/true);
1202 56099670 : if (init == error_mark_node)
1203 : return ok;
1204 :
1205 : /* If we were asked to only check constants, return early. */
1206 56099667 : if (const_only && !TREE_CONSTANT (init))
1207 : return ok;
1208 :
1209 56098159 : if (CP_INTEGRAL_TYPE_P (type)
1210 56083676 : && SCALAR_FLOAT_TYPE_P (ftype))
1211 : ok = false;
1212 56097939 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
1213 55875698 : && CP_INTEGRAL_TYPE_P (type))
1214 : {
1215 55869719 : if (TREE_CODE (ftype) == ENUMERAL_TYPE)
1216 : /* Check for narrowing based on the values of the enumeration. */
1217 313849 : ftype = ENUM_UNDERLYING_TYPE (ftype);
1218 : /* Undo convert_bitfield_to_declared_type (STRIP_NOPS isn't enough). */
1219 55869719 : tree op = init;
1220 55874602 : while (CONVERT_EXPR_P (op))
1221 4883 : op = TREE_OPERAND (op, 0);
1222 : /* Core 2627 says that we shouldn't warn when "the source is a bit-field
1223 : whose width w is less than that of its type (or, for an enumeration
1224 : type, its underlying type) and the target type can represent all the
1225 : values of a hypothetical extended integer type with width w and with
1226 : the same signedness as the original type". */
1227 55869719 : if (is_bitfield_expr_with_lowered_type (op)
1228 55869719 : && TYPE_PRECISION (TREE_TYPE (op)) < TYPE_PRECISION (ftype))
1229 70 : ftype = TREE_TYPE (op);
1230 55869719 : if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
1231 55869719 : TYPE_MAX_VALUE (ftype))
1232 55662210 : || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
1233 55662210 : TYPE_MIN_VALUE (type)))
1234 109123305 : && (TREE_CODE (init) != INTEGER_CST
1235 53460979 : || !int_fits_type_p (init, type)))
1236 : ok = false;
1237 : }
1238 : /* [dcl.init.list]#7.2: "from long double to double or float, or from
1239 : double to float". */
1240 228220 : else if (SCALAR_FLOAT_TYPE_P (ftype)
1241 8504 : && SCALAR_FLOAT_TYPE_P (type))
1242 : {
1243 17053 : if ((extended_float_type_p (ftype) || extended_float_type_p (type))
1244 8495 : ? /* "from a floating-point type T to another floating-point type
1245 : whose floating-point conversion rank is neither greater than
1246 : nor equal to that of T".
1247 : So, it is ok if
1248 : cp_compare_floating_point_conversion_ranks (ftype, type)
1249 : returns -2 (type has greater conversion rank than ftype)
1250 : or [-1..1] (type has equal conversion rank as ftype, possibly
1251 : different subrank. Only do this if at least one of the
1252 : types is extended floating-point type, otherwise keep doing
1253 : what we did before (for the sake of non-standard
1254 : backend types). */
1255 458 : cp_compare_floating_point_conversion_ranks (ftype, type) >= 2
1256 8037 : : ((same_type_p (ftype, long_double_type_node)
1257 79 : && (same_type_p (type, double_type_node)
1258 67 : || same_type_p (type, float_type_node)))
1259 8022 : || (same_type_p (ftype, double_type_node)
1260 7499 : && same_type_p (type, float_type_node))
1261 10197 : || (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))))
1262 : {
1263 5957 : if (TREE_CODE (init) == REAL_CST)
1264 : {
1265 : /* Issue 703: Loss of precision is OK as long as the value is
1266 : within the representable range of the new type. */
1267 5852 : REAL_VALUE_TYPE r;
1268 5852 : d = TREE_REAL_CST (init);
1269 5852 : real_convert (&r, TYPE_MODE (type), &d);
1270 5852 : if (real_isinf (&r))
1271 0 : ok = false;
1272 : }
1273 : else
1274 : ok = false;
1275 : }
1276 : }
1277 219725 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
1278 5979 : && SCALAR_FLOAT_TYPE_P (type))
1279 : {
1280 5944 : ok = false;
1281 5944 : if (TREE_CODE (init) == INTEGER_CST)
1282 : {
1283 5906 : d = real_value_from_int_cst (0, init);
1284 5906 : if (exact_real_truncate (TYPE_MODE (type), &d))
1285 : ok = true;
1286 : }
1287 : }
1288 213781 : else if (TREE_CODE (type) == BOOLEAN_TYPE
1289 213781 : && (TYPE_PTR_P (ftype) || TYPE_PTRMEM_P (ftype)))
1290 : /* C++20 P1957R2: converting from a pointer type or a pointer-to-member
1291 : type to bool should be considered narrowing. This is a DR so is not
1292 : limited to C++20 only. */
1293 : ok = false;
1294 :
1295 6829 : bool almost_ok = ok;
1296 6829 : if (!ok && !CONSTANT_CLASS_P (init) && (complain & tf_warning_or_error))
1297 : {
1298 161 : tree folded = cp_fully_fold (init);
1299 161 : if (TREE_CONSTANT (folded) && check_narrowing (type, folded, tf_none))
1300 : almost_ok = true;
1301 : }
1302 :
1303 977 : if (!ok)
1304 : {
1305 977 : location_t loc = cp_expr_loc_or_input_loc (init);
1306 977 : if (cxx_dialect == cxx98)
1307 : {
1308 1 : if (complain & tf_warning)
1309 1 : warning_at (loc, OPT_Wnarrowing, "narrowing conversion of %qE "
1310 : "from %qH to %qI is ill-formed in C++11",
1311 : init, ftype, type);
1312 : ok = true;
1313 : }
1314 976 : else if (!CONSTANT_CLASS_P (init))
1315 : {
1316 315 : if (complain & tf_warning_or_error)
1317 : {
1318 161 : auto_diagnostic_group d;
1319 4 : if ((!almost_ok || pedantic)
1320 304 : && pedwarn (loc, OPT_Wnarrowing,
1321 : "narrowing conversion of %qE from %qH to %qI",
1322 : init, ftype, type)
1323 306 : && almost_ok)
1324 2 : inform (loc, " the expression has a constant value but is not "
1325 : "a C++ constant-expression");
1326 161 : ok = true;
1327 161 : }
1328 : }
1329 661 : else if (complain & tf_error)
1330 : {
1331 647 : int savederrorcount = errorcount;
1332 647 : permerror_opt (loc, OPT_Wnarrowing,
1333 : "narrowing conversion of %qE from %qH to %qI",
1334 : init, ftype, type);
1335 647 : if (errorcount == savederrorcount)
1336 56535980 : ok = true;
1337 : }
1338 : }
1339 :
1340 : return ok;
1341 : }
1342 :
1343 : /* True iff TYPE is a C++20 "ordinary" character type. */
1344 :
1345 : bool
1346 42855 : ordinary_char_type_p (tree type)
1347 : {
1348 42855 : type = TYPE_MAIN_VARIANT (type);
1349 42855 : return (type == char_type_node
1350 21486 : || type == signed_char_type_node
1351 64319 : || type == unsigned_char_type_node);
1352 : }
1353 :
1354 : /* True iff the string literal INIT has a type suitable for initializing array
1355 : TYPE. */
1356 :
1357 : bool
1358 639406 : array_string_literal_compatible_p (tree type, tree init)
1359 : {
1360 639406 : tree to_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1361 639406 : tree from_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
1362 :
1363 639406 : if (to_char_type == from_char_type)
1364 : return true;
1365 : /* The array element type does not match the initializing string
1366 : literal element type; this is only allowed when both types are
1367 : ordinary character type. There are no string literals of
1368 : signed or unsigned char type in the language, but we can get
1369 : them internally from converting braced-init-lists to
1370 : STRING_CST. */
1371 21472 : if (ordinary_char_type_p (to_char_type)
1372 21472 : && ordinary_char_type_p (from_char_type))
1373 : return true;
1374 :
1375 : /* P2513 (C++20/C++23): "an array of char or unsigned char may
1376 : be initialized by a UTF-8 string literal, or by such a string
1377 : literal enclosed in braces." */
1378 190 : if (from_char_type == char8_type_node
1379 98 : && (to_char_type == char_type_node
1380 32 : || to_char_type == unsigned_char_type_node))
1381 : return true;
1382 :
1383 : return false;
1384 : }
1385 :
1386 : /* Process the initializer INIT for a variable of type TYPE, emitting
1387 : diagnostics for invalid initializers and converting the initializer as
1388 : appropriate.
1389 :
1390 : For aggregate types, it assumes that reshape_init has already run, thus the
1391 : initializer will have the right shape (brace elision has been undone).
1392 :
1393 : NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR,
1394 : 2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR. */
1395 :
1396 : static tree
1397 115273857 : digest_init_r (tree type, tree init, int nested, int flags,
1398 : tsubst_flags_t complain)
1399 : {
1400 115273857 : enum tree_code code = TREE_CODE (type);
1401 :
1402 115273857 : if (error_operand_p (init))
1403 2137 : return error_mark_node;
1404 :
1405 115271720 : gcc_assert (init);
1406 :
1407 : /* We must strip the outermost array type when completing the type,
1408 : because the its bounds might be incomplete at the moment. */
1409 116307738 : if (!complete_type_or_maybe_complain (code == ARRAY_TYPE
1410 1036018 : ? TREE_TYPE (type) : type, NULL_TREE,
1411 : complain))
1412 32 : return error_mark_node;
1413 :
1414 115271688 : location_t loc = cp_expr_loc_or_input_loc (init);
1415 :
1416 115271688 : tree stripped_init = init;
1417 :
1418 11194278 : if (BRACE_ENCLOSED_INITIALIZER_P (init)
1419 126462054 : && CONSTRUCTOR_IS_PAREN_INIT (init))
1420 1617 : flags |= LOOKUP_AGGREGATE_PAREN_INIT;
1421 :
1422 : /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
1423 : (g++.old-deja/g++.law/casts2.C). */
1424 115271688 : if (TREE_CODE (init) == NON_LVALUE_EXPR)
1425 32067336 : stripped_init = TREE_OPERAND (init, 0);
1426 :
1427 115271688 : stripped_init = tree_strip_any_location_wrapper (stripped_init);
1428 :
1429 : /* Initialization of an array of chars from a string constant. The initializer
1430 : can be optionally enclosed in braces, but reshape_init has already removed
1431 : them if they were present. */
1432 115271688 : if (code == ARRAY_TYPE)
1433 : {
1434 1157025 : if (nested && !TYPE_DOMAIN (type))
1435 : /* C++ flexible array members have a null domain. */
1436 : {
1437 461 : if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT)
1438 410 : pedwarn (loc, OPT_Wpedantic,
1439 : "initialization of a flexible array member");
1440 : else
1441 : {
1442 51 : if (complain & tf_error)
1443 51 : error_at (loc, "non-static initialization of"
1444 : " a flexible array member");
1445 51 : return error_mark_node;
1446 : }
1447 : }
1448 :
1449 1035967 : tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1450 1035967 : if (char_type_p (typ1)
1451 1035967 : && TREE_CODE (stripped_init) == STRING_CST)
1452 : {
1453 639361 : if (!array_string_literal_compatible_p (type, init))
1454 : {
1455 111 : if (complain & tf_error)
1456 111 : error_at (loc, "cannot initialize array of %qT from "
1457 : "a string literal with type array of %qT",
1458 : typ1,
1459 111 : TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init))));
1460 111 : return error_mark_node;
1461 : }
1462 :
1463 639635 : if (nested == 2 && !TYPE_DOMAIN (type))
1464 : {
1465 24 : if (complain & tf_error)
1466 24 : error_at (loc, "initialization of flexible array member "
1467 : "in a nested context");
1468 24 : return error_mark_node;
1469 : }
1470 :
1471 639226 : if (type != TREE_TYPE (init)
1472 639226 : && !variably_modified_type_p (type, NULL_TREE))
1473 : {
1474 23547 : init = copy_node (init);
1475 23547 : TREE_TYPE (init) = type;
1476 : /* If we have a location wrapper, then also copy the wrapped
1477 : node, and update the copy's type. */
1478 23547 : if (location_wrapper_p (init))
1479 : {
1480 22618 : stripped_init = copy_node (stripped_init);
1481 22618 : TREE_OPERAND (init, 0) = stripped_init;
1482 22618 : TREE_TYPE (stripped_init) = type;
1483 : }
1484 : }
1485 639226 : if (TYPE_DOMAIN (type) && TREE_CONSTANT (TYPE_SIZE (type)))
1486 : {
1487 : /* Not a flexible array member. */
1488 639156 : int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1489 639156 : size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1490 : /* In C it is ok to subtract 1 from the length of the string
1491 : because it's ok to ignore the terminating null char that is
1492 : counted in the length of the constant, but in C++ this would
1493 : be invalid. */
1494 639156 : if (size < TREE_STRING_LENGTH (stripped_init))
1495 : {
1496 159 : permerror (loc, "initializer-string for %qT is too long",
1497 : type);
1498 :
1499 159 : init = build_string (size,
1500 159 : TREE_STRING_POINTER (stripped_init));
1501 159 : TREE_TYPE (init) = type;
1502 : }
1503 : }
1504 639226 : return init;
1505 : }
1506 : }
1507 :
1508 : /* Handle scalar types (including conversions) and references. */
1509 1214 : if ((code != COMPLEX_TYPE || BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
1510 114632321 : && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1511 : {
1512 : /* Narrowing is OK when initializing an aggregate from
1513 : a parenthesized list. */
1514 103650444 : if (nested && !(flags & LOOKUP_AGGREGATE_PAREN_INIT))
1515 54728987 : flags |= LOOKUP_NO_NARROWING;
1516 103650444 : if (TREE_CODE (init) == RAW_DATA_CST && !TYPE_UNSIGNED (type))
1517 : {
1518 52 : tree ret = init;
1519 52 : if ((flags & LOOKUP_NO_NARROWING) || warn_conversion)
1520 226202 : for (unsigned int i = 0;
1521 226254 : i < (unsigned) RAW_DATA_LENGTH (init); ++i)
1522 226202 : if (RAW_DATA_SCHAR_ELT (init, i) < 0)
1523 : {
1524 60 : if ((flags & LOOKUP_NO_NARROWING))
1525 : {
1526 60 : tree elt
1527 60 : = build_int_cst (integer_type_node,
1528 60 : RAW_DATA_UCHAR_ELT (init, i));
1529 60 : if (!check_narrowing (type, elt, complain, false))
1530 : {
1531 30 : if (!(complain & tf_warning_or_error))
1532 0 : ret = error_mark_node;
1533 30 : continue;
1534 : }
1535 : }
1536 30 : if (warn_conversion)
1537 60 : warning (OPT_Wconversion,
1538 : "conversion from %qT to %qT changes value from "
1539 : "%qd to %qd",
1540 : integer_type_node, type,
1541 30 : RAW_DATA_UCHAR_ELT (init, i),
1542 30 : RAW_DATA_SCHAR_ELT (init, i));
1543 : }
1544 52 : return ret;
1545 : }
1546 103650392 : init = convert_for_initialization (0, type, init, flags,
1547 : ICR_INIT, NULL_TREE, 0,
1548 : complain);
1549 :
1550 103650392 : return init;
1551 : }
1552 :
1553 : /* Come here only for aggregates: records, arrays, unions, complex numbers
1554 : and vectors. */
1555 10981832 : gcc_assert (code == ARRAY_TYPE
1556 : || VECTOR_TYPE_P (type)
1557 : || code == RECORD_TYPE
1558 : || code == UNION_TYPE
1559 : || code == OPAQUE_TYPE
1560 : || code == COMPLEX_TYPE);
1561 :
1562 : /* "If T is a class type and the initializer list has a single
1563 : element of type cv U, where U is T or a class derived from T,
1564 : the object is initialized from that element." */
1565 10981832 : if (cxx_dialect >= cxx11
1566 10943719 : && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
1567 10614079 : && !CONSTRUCTOR_IS_DESIGNATED_INIT (stripped_init)
1568 10261956 : && CONSTRUCTOR_NELTS (stripped_init) == 1
1569 11547931 : && ((CLASS_TYPE_P (type) && !CLASSTYPE_NON_AGGREGATE (type))
1570 83483 : || VECTOR_TYPE_P (type)))
1571 : {
1572 482999 : tree elt = CONSTRUCTOR_ELT (stripped_init, 0)->value;
1573 482999 : if (reference_related_p (type, TREE_TYPE (elt)))
1574 : {
1575 : /* In C++17, aggregates can have bases, thus participate in
1576 : aggregate initialization. In the following case:
1577 :
1578 : struct B { int c; };
1579 : struct D : B { };
1580 : D d{{D{{42}}}};
1581 :
1582 : there's an extra set of braces, so the D temporary initializes
1583 : the first element of d, which is the B base subobject. The base
1584 : of type B is copy-initialized from the D temporary, causing
1585 : object slicing. */
1586 30 : tree field = next_aggregate_field (TYPE_FIELDS (type));
1587 60 : if (field && DECL_FIELD_IS_BASE (field))
1588 : {
1589 30 : auto_diagnostic_group d;
1590 30 : if (warning_at (loc, 0, "initializing a base class of type %qT "
1591 30 : "results in object slicing", TREE_TYPE (field)))
1592 30 : inform (loc, "remove %<{ }%> around initializer");
1593 30 : }
1594 0 : else if (flag_checking)
1595 : /* We should have fixed this in reshape_init. */
1596 0 : gcc_unreachable ();
1597 : }
1598 : }
1599 :
1600 10981832 : if (SIMPLE_TARGET_EXPR_P (stripped_init))
1601 18538 : stripped_init = TARGET_EXPR_INITIAL (stripped_init);
1602 :
1603 10637008 : if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
1604 21612902 : && !TYPE_NON_AGGREGATE_CLASS (type))
1605 10267795 : return process_init_constructor (type, stripped_init, nested, flags,
1606 10267795 : complain);
1607 : else
1608 : {
1609 714037 : if (COMPOUND_LITERAL_P (stripped_init) && code == ARRAY_TYPE)
1610 : {
1611 0 : if (complain & tf_error)
1612 0 : error_at (loc, "cannot initialize aggregate of type %qT with "
1613 : "a compound literal", type);
1614 :
1615 0 : return error_mark_node;
1616 : }
1617 :
1618 714037 : if (code == ARRAY_TYPE
1619 714037 : && !BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
1620 : {
1621 : /* Allow the result of build_array_copy and of
1622 : build_value_init_noctor. */
1623 212 : if ((TREE_CODE (stripped_init) == VEC_INIT_EXPR
1624 193 : || TREE_CODE (stripped_init) == CONSTRUCTOR)
1625 380 : && (same_type_ignoring_top_level_qualifiers_p
1626 168 : (type, TREE_TYPE (init))))
1627 : return init;
1628 :
1629 53 : if (complain & tf_error)
1630 53 : error_at (loc, "array must be initialized with a brace-enclosed"
1631 : " initializer");
1632 53 : return error_mark_node;
1633 : }
1634 :
1635 713825 : return convert_for_initialization (NULL_TREE, type, init,
1636 : flags,
1637 : ICR_INIT, NULL_TREE, 0,
1638 713825 : complain);
1639 : }
1640 : }
1641 :
1642 : tree
1643 1393296 : digest_init (tree type, tree init, tsubst_flags_t complain)
1644 : {
1645 1393296 : return digest_init_r (type, init, 0, LOOKUP_IMPLICIT, complain);
1646 : }
1647 :
1648 : tree
1649 56940716 : digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain)
1650 : {
1651 56940716 : return digest_init_r (type, init, 0, flags, complain);
1652 : }
1653 :
1654 : /* Callback to replace PLACEHOLDER_EXPRs in a TARGET_EXPR (which isn't used
1655 : in the context of guaranteed copy elision). */
1656 :
1657 : static tree
1658 3103653 : replace_placeholders_for_class_temp_r (tree *tp, int *, void *)
1659 : {
1660 3103653 : tree t = *tp;
1661 :
1662 : /* We're looking for a TARGET_EXPR nested in the whole expression. */
1663 3103653 : if (TREE_CODE (t) == TARGET_EXPR
1664 : /* That serves as temporary materialization, not an initializer. */
1665 3103653 : && !TARGET_EXPR_ELIDING_P (t))
1666 : {
1667 1707 : tree init = TARGET_EXPR_INITIAL (t);
1668 1710 : while (TREE_CODE (init) == COMPOUND_EXPR)
1669 3 : init = TREE_OPERAND (init, 1);
1670 1707 : if (TREE_CODE (init) == CONSTRUCTOR
1671 1707 : && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init))
1672 : {
1673 129 : tree obj = TARGET_EXPR_SLOT (t);
1674 129 : replace_placeholders (init, obj);
1675 : /* We should have dealt with all PLACEHOLDER_EXPRs. */
1676 129 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = false;
1677 129 : gcc_checking_assert (!find_placeholders (init));
1678 : }
1679 : }
1680 :
1681 3103653 : return NULL_TREE;
1682 : }
1683 :
1684 : /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1685 : tree
1686 731612 : digest_nsdmi_init (tree decl, tree init, tsubst_flags_t complain)
1687 : {
1688 731612 : gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1689 :
1690 731612 : tree type = TREE_TYPE (decl);
1691 731612 : if (DECL_BIT_FIELD_TYPE (decl))
1692 150 : type = DECL_BIT_FIELD_TYPE (decl);
1693 731612 : int flags = LOOKUP_IMPLICIT;
1694 731612 : if (DIRECT_LIST_INIT_P (init))
1695 : {
1696 91251 : flags = LOOKUP_NORMAL;
1697 91251 : complain |= tf_no_cleanup;
1698 : }
1699 231260 : if (BRACE_ENCLOSED_INITIALIZER_P (init)
1700 962872 : && CP_AGGREGATE_TYPE_P (type))
1701 128758 : init = reshape_init (type, init, complain);
1702 731612 : init = digest_init_flags (type, init, flags, complain);
1703 :
1704 : /* Fold away any non-ODR used constants so that we don't need to
1705 : stream them in modules. */
1706 731612 : init = cp_fold_non_odr_use (init, /*rval=*/!TYPE_REF_P (type));
1707 :
1708 731612 : set_target_expr_eliding (init);
1709 :
1710 : /* We may have temporary materialization in a NSDMI, if the initializer
1711 : has something like A{} in it. Digesting the {} could have introduced
1712 : a PLACEHOLDER_EXPR referring to A. Now that we've got a TARGET_EXPR,
1713 : we have an object we can refer to. The reason we bother doing this
1714 : here is for code like
1715 :
1716 : struct A {
1717 : int x;
1718 : int y = x;
1719 : };
1720 :
1721 : struct B {
1722 : int x = 0;
1723 : int y = A{x}.y; // #1
1724 : };
1725 :
1726 : where in #1 we don't want to end up with two PLACEHOLDER_EXPRs for
1727 : different types on the same level in a {} when lookup_placeholder
1728 : wouldn't find a named object for the PLACEHOLDER_EXPR for A. Note,
1729 : temporary materialization does not occur when initializing an object
1730 : from a prvalue of the same type, therefore we must not replace the
1731 : placeholder with a temporary object so that it can be elided. */
1732 731612 : cp_walk_tree_without_duplicates (&init, replace_placeholders_for_class_temp_r,
1733 : nullptr);
1734 :
1735 731612 : return init;
1736 : }
1737 :
1738 : /* Set of flags used within process_init_constructor to describe the
1739 : initializers. */
1740 : #define PICFLAG_ERRONEOUS 1
1741 : #define PICFLAG_NOT_ALL_CONSTANT 2
1742 : #define PICFLAG_NOT_ALL_SIMPLE 4
1743 : #define PICFLAG_SIDE_EFFECTS 8
1744 : #define PICFLAG_VEC_INIT 16
1745 :
1746 : /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1747 : describe it. */
1748 :
1749 : static int
1750 57469294 : picflag_from_initializer (tree init)
1751 : {
1752 57469294 : if (init == error_mark_node)
1753 : return PICFLAG_ERRONEOUS;
1754 57468811 : else if (!TREE_CONSTANT (init))
1755 : {
1756 1575178 : if (TREE_SIDE_EFFECTS (init))
1757 : return PICFLAG_SIDE_EFFECTS;
1758 : else
1759 1329798 : return PICFLAG_NOT_ALL_CONSTANT;
1760 : }
1761 55893633 : else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1762 108212 : return PICFLAG_NOT_ALL_SIMPLE;
1763 : return 0;
1764 : }
1765 :
1766 : /* Adjust INIT for going into a CONSTRUCTOR. */
1767 :
1768 : static tree
1769 56939845 : massage_init_elt (tree type, tree init, int nested, int flags,
1770 : tsubst_flags_t complain)
1771 : {
1772 56939845 : int new_flags = LOOKUP_IMPLICIT;
1773 56939845 : if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT)
1774 52624654 : new_flags |= LOOKUP_ALLOW_FLEXARRAY_INIT;
1775 56939845 : if (flags & LOOKUP_AGGREGATE_PAREN_INIT)
1776 2617 : new_flags |= LOOKUP_AGGREGATE_PAREN_INIT;
1777 109779753 : init = digest_init_r (type, init, nested ? 2 : 1, new_flags, complain);
1778 : /* When we defer constant folding within a statement, we may want to
1779 : defer this folding as well. Don't call this on CONSTRUCTORs in
1780 : a template because their elements have already been folded, and
1781 : we must avoid folding the result of get_nsdmi. */
1782 56939845 : if (!(processing_template_decl && TREE_CODE (init) == CONSTRUCTOR))
1783 : {
1784 56939600 : tree t = fold_non_dependent_init (init, complain);
1785 56939600 : if (TREE_CONSTANT (t))
1786 55385630 : init = t;
1787 56939600 : set_target_expr_eliding (init);
1788 : }
1789 56939845 : return init;
1790 : }
1791 :
1792 : /* Subroutine of process_init_constructor, which will process an initializer
1793 : INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1794 : which describe the initializers. */
1795 :
1796 : static int
1797 446480 : process_init_constructor_array (tree type, tree init, int nested, int flags,
1798 : tsubst_flags_t complain)
1799 : {
1800 446480 : unsigned HOST_WIDE_INT i, j, len = 0;
1801 446480 : int picflags = 0;
1802 446480 : bool unbounded = false;
1803 446480 : constructor_elt *ce;
1804 446480 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1805 :
1806 446480 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1807 : || VECTOR_TYPE_P (type));
1808 :
1809 446480 : if (TREE_CODE (type) == ARRAY_TYPE)
1810 : {
1811 : /* C++ flexible array members have a null domain. */
1812 396394 : tree domain = TYPE_DOMAIN (type);
1813 396394 : if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1814 791870 : len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
1815 791870 : - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
1816 395935 : TYPE_PRECISION (TREE_TYPE (domain)),
1817 791870 : TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
1818 : else
1819 : unbounded = true; /* Take as many as there are. */
1820 :
1821 396394 : if (nested == 2 && !domain && !vec_safe_is_empty (v))
1822 : {
1823 69 : if (complain & tf_error)
1824 138 : error_at (cp_expr_loc_or_input_loc (init),
1825 : "initialization of flexible array member "
1826 : "in a nested context");
1827 69 : return PICFLAG_ERRONEOUS;
1828 : }
1829 : }
1830 : else
1831 : /* Vectors are like simple fixed-size arrays. */
1832 50086 : unbounded = !TYPE_VECTOR_SUBPARTS (type).is_constant (&len);
1833 :
1834 : /* There must not be more initializers than needed. */
1835 446411 : if (!unbounded && vec_safe_length (v) > len)
1836 : {
1837 14 : if (complain & tf_error)
1838 14 : error ("too many initializers for %qT", type);
1839 : else
1840 : return PICFLAG_ERRONEOUS;
1841 : }
1842 :
1843 446411 : j = 0;
1844 50729652 : FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1845 : {
1846 50283241 : if (!ce->index)
1847 634 : ce->index = size_int (j);
1848 50282607 : else if (!check_array_designated_initializer (ce, j))
1849 0 : ce->index = error_mark_node;
1850 50283241 : gcc_assert (ce->value);
1851 50283241 : ce->value
1852 50283241 : = massage_init_elt (TREE_TYPE (type), ce->value, nested, flags,
1853 : complain);
1854 :
1855 50283241 : gcc_checking_assert
1856 : (ce->value == error_mark_node
1857 : || (same_type_ignoring_top_level_qualifiers_p
1858 : (strip_array_types (TREE_TYPE (type)),
1859 : strip_array_types (TREE_TYPE (ce->value)))));
1860 :
1861 50283241 : picflags |= picflag_from_initializer (ce->value);
1862 : /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer
1863 : CONSTRUCTOR. */
1864 50283241 : if (TREE_CODE (ce->value) == CONSTRUCTOR
1865 50283241 : && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value))
1866 : {
1867 17 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1868 17 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value) = 0;
1869 : }
1870 50283241 : if (TREE_CODE (ce->value) == RAW_DATA_CST)
1871 304 : j += RAW_DATA_LENGTH (ce->value);
1872 : else
1873 50282937 : ++j;
1874 : }
1875 :
1876 : /* No more initializers. If the array is unbounded, we are done. Otherwise,
1877 : we must add initializers ourselves. */
1878 446411 : if (!unbounded)
1879 446114 : for (; i < len; ++i)
1880 : {
1881 37611 : tree next;
1882 :
1883 37611 : if (type_build_ctor_call (TREE_TYPE (type)))
1884 : {
1885 : /* If this type needs constructors run for default-initialization,
1886 : we can't rely on the back end to do it for us, so make the
1887 : initialization explicit by list-initializing from T{}. */
1888 591 : next = build_constructor (init_list_type_node, NULL);
1889 591 : next = massage_init_elt (TREE_TYPE (type), next, nested, flags,
1890 : complain);
1891 591 : if (initializer_zerop (next))
1892 : /* The default zero-initialization is fine for us; don't
1893 : add anything to the CONSTRUCTOR. */
1894 : next = NULL_TREE;
1895 : }
1896 37020 : else if (!zero_init_p (TREE_TYPE (type)))
1897 147 : next = build_zero_init (TREE_TYPE (type),
1898 : /*nelts=*/NULL_TREE,
1899 : /*static_storage_p=*/false);
1900 : else
1901 : /* The default zero-initialization is fine for us; don't
1902 : add anything to the CONSTRUCTOR. */
1903 : next = NULL_TREE;
1904 :
1905 421 : if (next)
1906 : {
1907 421 : if (next != error_mark_node
1908 421 : && (initializer_constant_valid_p (next, TREE_TYPE (next))
1909 409 : != null_pointer_node))
1910 : {
1911 : /* Use VEC_INIT_EXPR for non-constant initialization of
1912 : trailing elements with no explicit initializers. */
1913 221 : picflags |= PICFLAG_VEC_INIT;
1914 221 : break;
1915 : }
1916 :
1917 200 : picflags |= picflag_from_initializer (next);
1918 : /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer
1919 : CONSTRUCTOR. */
1920 200 : if (TREE_CODE (next) == CONSTRUCTOR
1921 200 : && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next))
1922 : {
1923 0 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1924 0 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next) = 0;
1925 : }
1926 200 : if (len > i+1)
1927 : {
1928 107 : tree range = build2 (RANGE_EXPR, size_type_node,
1929 107 : build_int_cst (size_type_node, i),
1930 107 : build_int_cst (size_type_node, len - 1));
1931 107 : CONSTRUCTOR_APPEND_ELT (v, range, next);
1932 107 : break;
1933 : }
1934 : else
1935 93 : CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1936 : }
1937 : else
1938 : /* Don't bother checking all the other elements. */
1939 : break;
1940 : }
1941 :
1942 446411 : CONSTRUCTOR_ELTS (init) = v;
1943 446411 : return picflags;
1944 : }
1945 :
1946 : /* Subroutine of process_init_constructor, which will process an initializer
1947 : INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1948 : the initializers. */
1949 :
1950 : static int
1951 9705128 : process_init_constructor_record (tree type, tree init, int nested, int flags,
1952 : tsubst_flags_t complain)
1953 : {
1954 9705128 : vec<constructor_elt, va_gc> *v = NULL;
1955 9705128 : tree field;
1956 9705128 : int skipped = 0;
1957 :
1958 9705128 : gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1959 9705128 : gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1960 9705128 : gcc_assert (!TYPE_BINFO (type)
1961 : || cxx_dialect >= cxx17
1962 : || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1963 9705128 : gcc_assert (!TYPE_POLYMORPHIC_P (type));
1964 :
1965 9705128 : restart:
1966 9731533 : int picflags = 0;
1967 9731533 : unsigned HOST_WIDE_INT idx = 0;
1968 9731533 : int designator_skip = -1;
1969 : /* Generally, we will always have an index for each initializer (which is
1970 : a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1971 : reshape_init. So we need to handle both cases. */
1972 94105232 : for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1973 : {
1974 84400730 : tree next;
1975 :
1976 160759624 : if (TREE_CODE (field) != FIELD_DECL
1977 84400730 : || (DECL_ARTIFICIAL (field)
1978 718414 : && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))))
1979 76358894 : continue;
1980 :
1981 8041836 : if (DECL_UNNAMED_BIT_FIELD (field))
1982 152 : continue;
1983 :
1984 : /* If this is a bitfield, first convert to the declared type. */
1985 8041684 : tree fldtype = TREE_TYPE (field);
1986 8041684 : if (DECL_BIT_FIELD_TYPE (field))
1987 265697 : fldtype = DECL_BIT_FIELD_TYPE (field);
1988 8041684 : if (fldtype == error_mark_node)
1989 : return PICFLAG_ERRONEOUS;
1990 :
1991 8041667 : next = NULL_TREE;
1992 8041667 : if (idx < CONSTRUCTOR_NELTS (init))
1993 : {
1994 6520244 : constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1995 6520244 : if (ce->index)
1996 : {
1997 : /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1998 : latter case can happen in templates where lookup has to be
1999 : deferred. */
2000 6518297 : gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
2001 : || identifier_p (ce->index));
2002 6518297 : if (ce->index == field || ce->index == DECL_NAME (field))
2003 6517827 : next = ce->value;
2004 : else
2005 : {
2006 470 : ce = NULL;
2007 470 : if (designator_skip == -1)
2008 : designator_skip = 1;
2009 : }
2010 : }
2011 : else
2012 : {
2013 1947 : designator_skip = 0;
2014 1947 : next = ce->value;
2015 : }
2016 :
2017 6519774 : if (ce)
2018 : {
2019 6519774 : gcc_assert (ce->value);
2020 6519774 : next = massage_init_elt (fldtype, next, nested, flags, complain);
2021 6519774 : ++idx;
2022 : }
2023 : }
2024 8041667 : if (next == error_mark_node)
2025 : /* We skip initializers for empty bases/fields, so skipping an invalid
2026 : one could make us accept invalid code. */
2027 : return PICFLAG_ERRONEOUS;
2028 8041058 : else if (next)
2029 : /* Already handled above. */;
2030 1521893 : else if (DECL_INITIAL (field))
2031 : {
2032 262718 : if (skipped > 0)
2033 : {
2034 : /* We're using an NSDMI past a field with implicit
2035 : zero-init. Go back and make it explicit. */
2036 26405 : skipped = -1;
2037 26405 : vec_safe_truncate (v, 0);
2038 26405 : goto restart;
2039 : }
2040 : /* C++14 aggregate NSDMI. */
2041 236313 : next = get_nsdmi (field, /*ctor*/false, complain);
2042 236313 : if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
2043 236313 : && find_placeholders (next))
2044 962 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
2045 : }
2046 1259175 : else if (type_build_ctor_call (fldtype))
2047 : {
2048 : /* If this type needs constructors run for
2049 : default-initialization, we can't rely on the back end to do it
2050 : for us, so build up TARGET_EXPRs. If the type in question is
2051 : a class, just build one up; if it's an array, recurse. */
2052 29585 : next = build_constructor (init_list_type_node, NULL);
2053 29585 : next = massage_init_elt (fldtype, next, nested, flags, complain);
2054 29585 : if (TREE_CODE (next) == TARGET_EXPR
2055 29585 : && unsafe_copy_elision_p (field, next))
2056 0 : TARGET_EXPR_ELIDING_P (next) = false;
2057 :
2058 : /* Warn when some struct elements are implicitly initialized. */
2059 29585 : if ((complain & tf_warning)
2060 11764 : && !cp_unevaluated_operand
2061 41324 : && !EMPTY_CONSTRUCTOR_P (init))
2062 102 : warning (OPT_Wmissing_field_initializers,
2063 : "missing initializer for member %qD", field);
2064 : }
2065 : else
2066 : {
2067 1229590 : if (TYPE_REF_P (fldtype))
2068 : {
2069 22 : if (complain & tf_error)
2070 22 : error ("member %qD is uninitialized reference", field);
2071 : else
2072 : return PICFLAG_ERRONEOUS;
2073 : }
2074 1229568 : else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype))
2075 : {
2076 1 : if (complain & tf_error)
2077 1 : error ("member %qD with uninitialized reference fields", field);
2078 : else
2079 : return PICFLAG_ERRONEOUS;
2080 : }
2081 : /* Do nothing for flexible array members since they need not have any
2082 : elements. Don't worry about 'skipped' because a flexarray has to
2083 : be the last field. */
2084 1229567 : else if (TREE_CODE (fldtype) == ARRAY_TYPE && !TYPE_DOMAIN (fldtype))
2085 103 : continue;
2086 :
2087 : /* Warn when some struct elements are implicitly initialized
2088 : to zero. */
2089 1229487 : if ((complain & tf_warning)
2090 1206055 : && !cp_unevaluated_operand
2091 1205533 : && !EMPTY_CONSTRUCTOR_P (init)
2092 1230650 : && !is_really_empty_class (fldtype, /*ignore_vptr*/false))
2093 1064 : warning (OPT_Wmissing_field_initializers,
2094 : "missing initializer for member %qD", field);
2095 :
2096 1229487 : if (!zero_init_p (fldtype) || skipped < 0)
2097 : {
2098 342278 : if (TYPE_REF_P (fldtype))
2099 3 : next = build_zero_cst (fldtype);
2100 : else
2101 342275 : next = build_zero_init (fldtype, /*nelts=*/NULL_TREE,
2102 : /*static_storage_p=*/false);
2103 : }
2104 : else
2105 : {
2106 : /* The default zero-initialization is fine for us; don't
2107 : add anything to the CONSTRUCTOR. */
2108 887209 : skipped = 1;
2109 887209 : continue;
2110 : }
2111 : }
2112 :
2113 : /* We can't actually elide the temporary when initializing a
2114 : potentially-overlapping field from a function that returns by
2115 : value. */
2116 7127341 : if (TREE_CODE (next) == TARGET_EXPR
2117 7127341 : && unsafe_copy_elision_p (field, next))
2118 25 : TARGET_EXPR_ELIDING_P (next) = false;
2119 :
2120 7127341 : if (is_empty_field (field)
2121 7127341 : && !TREE_SIDE_EFFECTS (next))
2122 : /* Don't add trivial initialization of an empty base/field to the
2123 : constructor, as they might not be ordered the way the back-end
2124 : expects. */
2125 48144 : continue;
2126 :
2127 : /* If this is a bitfield, now convert to the lowered type. */
2128 7079197 : if (fldtype != TREE_TYPE (field))
2129 264057 : next = cp_convert_and_check (TREE_TYPE (field), next, complain);
2130 7079197 : picflags |= picflag_from_initializer (next);
2131 : /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer CONSTRUCTOR. */
2132 7079197 : if (TREE_CODE (next) == CONSTRUCTOR
2133 7079197 : && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next))
2134 : {
2135 81 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
2136 81 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next) = 0;
2137 : }
2138 91452896 : CONSTRUCTOR_APPEND_ELT (v, field, next);
2139 : }
2140 :
2141 9704502 : if (idx < CONSTRUCTOR_NELTS (init))
2142 : {
2143 183 : if (complain & tf_error)
2144 : {
2145 26 : constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
2146 : /* For better diagnostics, try to find out if it is really
2147 : the case of too many initializers or if designators are
2148 : in incorrect order. */
2149 26 : if (designator_skip == 1 && ce->index)
2150 : {
2151 15 : gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
2152 : || identifier_p (ce->index));
2153 15 : for (field = TYPE_FIELDS (type);
2154 54 : field; field = DECL_CHAIN (field))
2155 : {
2156 90 : if (TREE_CODE (field) != FIELD_DECL
2157 54 : || (DECL_ARTIFICIAL (field)
2158 0 : && !(cxx_dialect >= cxx17
2159 0 : && DECL_FIELD_IS_BASE (field))))
2160 36 : continue;
2161 :
2162 18 : if (DECL_UNNAMED_BIT_FIELD (field))
2163 0 : continue;
2164 :
2165 18 : if (ce->index == field || ce->index == DECL_NAME (field))
2166 : break;
2167 : }
2168 : }
2169 26 : if (field)
2170 15 : error ("designator order for field %qD does not match declaration "
2171 : "order in %qT", field, type);
2172 : else
2173 11 : error ("too many initializers for %qT", type);
2174 : }
2175 : else
2176 : return PICFLAG_ERRONEOUS;
2177 : }
2178 :
2179 9704345 : CONSTRUCTOR_ELTS (init) = v;
2180 9704345 : return picflags;
2181 : }
2182 :
2183 : /* Subroutine of process_init_constructor, which will process a single
2184 : initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
2185 : which describe the initializer. */
2186 :
2187 : static int
2188 116187 : process_init_constructor_union (tree type, tree init, int nested, int flags,
2189 : tsubst_flags_t complain)
2190 : {
2191 116187 : constructor_elt *ce;
2192 116187 : int len;
2193 :
2194 : /* If the initializer was empty, use the union's NSDMI if it has one.
2195 : Otherwise use default zero initialization. */
2196 116187 : if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
2197 : {
2198 104892 : for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2199 : {
2200 95367 : if (TREE_CODE (field) == FIELD_DECL
2201 95367 : && DECL_INITIAL (field) != NULL_TREE)
2202 : {
2203 25 : tree val = get_nsdmi (field, /*in_ctor=*/false, complain);
2204 25 : if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
2205 25 : && find_placeholders (val))
2206 19 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
2207 25 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init), field, val);
2208 25 : break;
2209 : }
2210 : }
2211 :
2212 9550 : if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
2213 : return 0;
2214 : }
2215 :
2216 106662 : len = CONSTRUCTOR_ELTS (init)->length ();
2217 106662 : if (len > 1)
2218 : {
2219 9 : if (!(complain & tf_error))
2220 : return PICFLAG_ERRONEOUS;
2221 3 : error ("too many initializers for %qT", type);
2222 3 : CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
2223 : }
2224 :
2225 106656 : ce = &(*CONSTRUCTOR_ELTS (init))[0];
2226 :
2227 : /* If this element specifies a field, initialize via that field. */
2228 106656 : if (ce->index)
2229 : {
2230 106637 : if (TREE_CODE (ce->index) == FIELD_DECL)
2231 : ;
2232 0 : else if (identifier_p (ce->index))
2233 : {
2234 : /* This can happen within a cast, see g++.dg/opt/cse2.C. */
2235 0 : tree name = ce->index;
2236 0 : tree field;
2237 0 : for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2238 0 : if (DECL_NAME (field) == name)
2239 : break;
2240 0 : if (!field)
2241 : {
2242 0 : if (complain & tf_error)
2243 0 : error ("no field %qD found in union being initialized",
2244 : field);
2245 0 : ce->value = error_mark_node;
2246 : }
2247 0 : ce->index = field;
2248 : }
2249 : else
2250 : {
2251 0 : gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
2252 : || TREE_CODE (ce->index) == RANGE_EXPR);
2253 0 : if (complain & tf_error)
2254 0 : error ("index value instead of field name in union initializer");
2255 0 : ce->value = error_mark_node;
2256 : }
2257 : }
2258 : else
2259 : {
2260 : /* Find the first named field. ANSI decided in September 1990
2261 : that only named fields count here. */
2262 19 : tree field = TYPE_FIELDS (type);
2263 228 : while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
2264 209 : field = TREE_CHAIN (field);
2265 19 : if (field == NULL_TREE)
2266 : {
2267 2 : if (complain & tf_error)
2268 2 : error ("too many initializers for %qT", type);
2269 2 : ce->value = error_mark_node;
2270 : }
2271 19 : ce->index = field;
2272 : }
2273 :
2274 106656 : if (ce->value && ce->value != error_mark_node)
2275 106654 : ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, nested,
2276 : flags, complain);
2277 :
2278 : /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer CONSTRUCTOR. */
2279 106656 : if (ce->value
2280 106656 : && TREE_CODE (ce->value) == CONSTRUCTOR
2281 173611 : && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value))
2282 : {
2283 0 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
2284 0 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value) = 0;
2285 : }
2286 106656 : return picflag_from_initializer (ce->value);
2287 : }
2288 :
2289 : /* Process INIT, a constructor for a variable of aggregate type TYPE. The
2290 : constructor is a brace-enclosed initializer, and will be modified in-place.
2291 :
2292 : Each element is converted to the right type through digest_init, and
2293 : missing initializers are added following the language rules (zero-padding,
2294 : etc.).
2295 :
2296 : After the execution, the initializer will have TREE_CONSTANT if all elts are
2297 : constant, and TREE_STATIC set if, in addition, all elts are simple enough
2298 : constants that the assembler and linker can compute them.
2299 :
2300 : The function returns the initializer itself, or error_mark_node in case
2301 : of error. */
2302 :
2303 : static tree
2304 10267795 : process_init_constructor (tree type, tree init, int nested, int flags,
2305 : tsubst_flags_t complain)
2306 : {
2307 10267795 : int picflags;
2308 :
2309 10267795 : gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
2310 :
2311 10267795 : if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
2312 446480 : picflags = process_init_constructor_array (type, init, nested, flags,
2313 : complain);
2314 9821315 : else if (TREE_CODE (type) == RECORD_TYPE)
2315 9705128 : picflags = process_init_constructor_record (type, init, nested, flags,
2316 : complain);
2317 116187 : else if (TREE_CODE (type) == UNION_TYPE)
2318 116187 : picflags = process_init_constructor_union (type, init, nested, flags,
2319 : complain);
2320 : else
2321 0 : gcc_unreachable ();
2322 :
2323 10267795 : if (picflags & PICFLAG_ERRONEOUS)
2324 1277 : return error_mark_node;
2325 :
2326 10266518 : TREE_TYPE (init) = type;
2327 10266518 : if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
2328 325 : cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
2329 10266518 : if (picflags & PICFLAG_SIDE_EFFECTS)
2330 : {
2331 198526 : TREE_CONSTANT (init) = false;
2332 198526 : TREE_SIDE_EFFECTS (init) = true;
2333 : }
2334 10067992 : else if (picflags & PICFLAG_NOT_ALL_CONSTANT)
2335 : {
2336 : /* Make sure TREE_CONSTANT isn't set from build_constructor. */
2337 845894 : TREE_CONSTANT (init) = false;
2338 845894 : TREE_SIDE_EFFECTS (init) = false;
2339 : }
2340 : else
2341 : {
2342 9222098 : TREE_CONSTANT (init) = 1;
2343 9222098 : TREE_SIDE_EFFECTS (init) = false;
2344 9222098 : if (!(picflags & PICFLAG_NOT_ALL_SIMPLE))
2345 9190959 : TREE_STATIC (init) = 1;
2346 : }
2347 10266518 : if (picflags & PICFLAG_VEC_INIT)
2348 : {
2349 : /* Defer default-initialization of array elements with no corresponding
2350 : initializer-clause until later so we can use a loop. */
2351 221 : TREE_TYPE (init) = init_list_type_node;
2352 221 : init = build_vec_init_expr (type, init, complain);
2353 221 : init = get_target_expr (init);
2354 : }
2355 : return init;
2356 : }
2357 :
2358 : /* Given a structure or union value DATUM, construct and return
2359 : the structure or union component which results from narrowing
2360 : that value to the base specified in BASETYPE. For example, given the
2361 : hierarchy
2362 :
2363 : class L { int ii; };
2364 : class A : L { ... };
2365 : class B : L { ... };
2366 : class C : A, B { ... };
2367 :
2368 : and the declaration
2369 :
2370 : C x;
2371 :
2372 : then the expression
2373 :
2374 : x.A::ii refers to the ii member of the L part of
2375 : the A part of the C object named by X. In this case,
2376 : DATUM would be x, and BASETYPE would be A.
2377 :
2378 : I used to think that this was nonconformant, that the standard specified
2379 : that first we look up ii in A, then convert x to an L& and pull out the
2380 : ii part. But in fact, it does say that we convert x to an A&; A here
2381 : is known as the "naming class". (jason 2000-12-19)
2382 :
2383 : BINFO_P points to a variable initialized either to NULL_TREE or to the
2384 : binfo for the specific base subobject we want to convert to. */
2385 :
2386 : tree
2387 2021 : build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
2388 : {
2389 2021 : tree binfo;
2390 :
2391 2021 : if (datum == error_mark_node)
2392 : return error_mark_node;
2393 2021 : if (*binfo_p)
2394 : binfo = *binfo_p;
2395 : else
2396 2021 : binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
2397 : NULL, tf_warning_or_error);
2398 :
2399 2021 : if (!binfo || binfo == error_mark_node)
2400 : {
2401 6 : *binfo_p = NULL_TREE;
2402 6 : if (!binfo)
2403 0 : error_not_base_type (basetype, TREE_TYPE (datum));
2404 6 : return error_mark_node;
2405 : }
2406 :
2407 2015 : *binfo_p = binfo;
2408 2015 : return build_base_path (PLUS_EXPR, datum, binfo, 1,
2409 2015 : tf_warning_or_error);
2410 : }
2411 :
2412 : /* Build a reference to an object specified by the C++ `->' operator.
2413 : Usually this just involves dereferencing the object, but if the
2414 : `->' operator is overloaded, then such overloads must be
2415 : performed until an object which does not have the `->' operator
2416 : overloaded is found. An error is reported when circular pointer
2417 : delegation is detected. */
2418 :
2419 : tree
2420 38795741 : build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
2421 : {
2422 38795741 : tree orig_expr = expr;
2423 38795741 : tree type = TREE_TYPE (expr);
2424 38795741 : tree last_rval = NULL_TREE;
2425 38795741 : vec<tree, va_gc> *types_memoized = NULL;
2426 :
2427 38795741 : if (type == error_mark_node)
2428 : return error_mark_node;
2429 :
2430 38795687 : if (processing_template_decl)
2431 : {
2432 33877092 : tree ttype = NULL_TREE;
2433 33877092 : if (type && TYPE_PTR_P (type))
2434 27809487 : ttype = TREE_TYPE (type);
2435 27809487 : if (ttype && !dependent_scope_p (ttype))
2436 : /* Pointer to current instantiation, don't treat as dependent. */;
2437 10188391 : else if (type_dependent_expression_p (expr))
2438 : {
2439 10094055 : expr = build_min_nt_loc (loc, ARROW_EXPR, expr);
2440 10094055 : TREE_TYPE (expr) = ttype;
2441 10094055 : return expr;
2442 : }
2443 : }
2444 :
2445 28701632 : if (MAYBE_CLASS_TYPE_P (type))
2446 : {
2447 251370 : struct tinst_level *actual_inst = current_instantiation ();
2448 251370 : tree fn = NULL;
2449 :
2450 505560 : while ((expr = build_new_op (loc, COMPONENT_REF,
2451 : LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
2452 : NULL_TREE, &fn, complain)))
2453 : {
2454 254214 : if (expr == error_mark_node)
2455 32 : return error_mark_node;
2456 :
2457 : /* This provides a better instantiation backtrace in case of
2458 : error. */
2459 254190 : if (fn && DECL_USE_TEMPLATE (fn))
2460 154546 : push_tinst_level_loc (fn,
2461 151696 : (current_instantiation () != actual_inst)
2462 2850 : ? DECL_SOURCE_LOCATION (fn)
2463 : : input_location);
2464 254190 : fn = NULL;
2465 :
2466 254190 : if (vec_member (TREE_TYPE (expr), types_memoized))
2467 : {
2468 0 : if (complain & tf_error)
2469 0 : error ("circular pointer delegation detected");
2470 0 : return error_mark_node;
2471 : }
2472 :
2473 254190 : vec_safe_push (types_memoized, TREE_TYPE (expr));
2474 254190 : last_rval = expr;
2475 : }
2476 :
2477 400339 : while (current_instantiation () != actual_inst)
2478 148996 : pop_tinst_level ();
2479 :
2480 251343 : if (last_rval == NULL_TREE)
2481 : {
2482 8 : if (complain & tf_error)
2483 8 : error ("base operand of %<->%> has non-pointer type %qT", type);
2484 8 : return error_mark_node;
2485 : }
2486 :
2487 251335 : if (TYPE_REF_P (TREE_TYPE (last_rval)))
2488 0 : last_rval = convert_from_reference (last_rval);
2489 : }
2490 : else
2491 : {
2492 28450262 : last_rval = decay_conversion (expr, complain);
2493 28450262 : if (last_rval == error_mark_node)
2494 : return error_mark_node;
2495 : }
2496 :
2497 28701594 : if (TYPE_PTR_P (TREE_TYPE (last_rval)))
2498 : {
2499 28701591 : if (processing_template_decl)
2500 : {
2501 23783034 : expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
2502 : orig_expr);
2503 23783034 : TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
2504 23783034 : return expr;
2505 : }
2506 :
2507 4918557 : return cp_build_indirect_ref (loc, last_rval, RO_ARROW, complain);
2508 : }
2509 :
2510 3 : if (complain & tf_error)
2511 : {
2512 3 : if (types_memoized)
2513 0 : error ("result of %<operator->()%> yields non-pointer result");
2514 : else
2515 3 : error ("base operand of %<->%> is not a pointer");
2516 : }
2517 3 : return error_mark_node;
2518 : }
2519 :
2520 : /* Return an expression for "DATUM .* COMPONENT". DATUM has not
2521 : already been checked out to be of aggregate type. */
2522 :
2523 : tree
2524 106875 : build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
2525 : {
2526 106875 : tree ptrmem_type;
2527 106875 : tree objtype;
2528 106875 : tree type;
2529 106875 : tree binfo;
2530 106875 : tree ctype;
2531 :
2532 106875 : datum = mark_lvalue_use (datum);
2533 106875 : component = mark_rvalue_use (component);
2534 :
2535 106875 : if (error_operand_p (datum) || error_operand_p (component))
2536 59 : return error_mark_node;
2537 :
2538 106816 : ptrmem_type = TREE_TYPE (component);
2539 106816 : if (!TYPE_PTRMEM_P (ptrmem_type))
2540 : {
2541 6 : if (complain & tf_error)
2542 3 : error ("%qE cannot be used as a member pointer, since it is of "
2543 : "type %qT", component, ptrmem_type);
2544 6 : return error_mark_node;
2545 : }
2546 :
2547 106810 : objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2548 106810 : if (! MAYBE_CLASS_TYPE_P (objtype))
2549 : {
2550 28 : if (complain & tf_error)
2551 0 : error ("cannot apply member pointer %qE to %qE, which is of "
2552 : "non-class type %qT", component, datum, objtype);
2553 28 : return error_mark_node;
2554 : }
2555 :
2556 106782 : type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
2557 106782 : ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
2558 :
2559 106782 : if (!COMPLETE_TYPE_P (ctype))
2560 : {
2561 83 : if (!same_type_p (ctype, objtype))
2562 0 : goto mismatch;
2563 : binfo = NULL;
2564 : }
2565 : else
2566 : {
2567 106699 : binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
2568 :
2569 106699 : if (!binfo)
2570 : {
2571 36 : mismatch:
2572 36 : if (complain & tf_error)
2573 : {
2574 6 : if (COMPLETE_TYPE_P (objtype))
2575 3 : error ("pointer to member type %qT incompatible "
2576 : "with object type %qT because %qT is not "
2577 : "derived from %qT", ptrmem_type, objtype,
2578 : objtype, ctype);
2579 : else
2580 3 : error ("pointer to member type %qT incompatible with "
2581 : "incomplete object type %qT", ptrmem_type, objtype);
2582 : }
2583 36 : return error_mark_node;
2584 : }
2585 106663 : else if (binfo == error_mark_node)
2586 : return error_mark_node;
2587 : }
2588 :
2589 106737 : if (TYPE_PTRDATAMEM_P (ptrmem_type))
2590 : {
2591 1360 : bool is_lval = real_lvalue_p (datum);
2592 1360 : tree ptype;
2593 :
2594 : /* Compute the type of the field, as described in [expr.ref].
2595 : There's no such thing as a mutable pointer-to-member, so
2596 : things are not as complex as they are for references to
2597 : non-static data members. */
2598 1360 : type = cp_build_qualified_type (type,
2599 1360 : (cp_type_quals (type)
2600 1360 : | cp_type_quals (TREE_TYPE (datum))));
2601 :
2602 1360 : datum = cp_build_addr_expr (datum, complain);
2603 :
2604 : /* Convert object to the correct base. */
2605 1360 : if (binfo)
2606 : {
2607 1333 : datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
2608 1333 : if (datum == error_mark_node)
2609 : return error_mark_node;
2610 : }
2611 :
2612 : /* Build an expression for "object + offset" where offset is the
2613 : value stored in the pointer-to-data-member. */
2614 1360 : ptype = build_pointer_type (type);
2615 1360 : datum = convert (ptype, datum);
2616 1360 : if (!processing_template_decl)
2617 1339 : datum = build2 (POINTER_PLUS_EXPR, ptype,
2618 : datum, convert_to_ptrofftype (component));
2619 1360 : datum = cp_fully_fold (datum);
2620 1360 : datum = cp_build_fold_indirect_ref (datum);
2621 1360 : if (datum == error_mark_node)
2622 : return error_mark_node;
2623 :
2624 : /* If the object expression was an rvalue, return an rvalue. */
2625 1360 : if (!is_lval)
2626 157 : datum = move (datum);
2627 1360 : return datum;
2628 : }
2629 : else
2630 : {
2631 : /* 5.5/6: In a .* expression whose object expression is an rvalue, the
2632 : program is ill-formed if the second operand is a pointer to member
2633 : function with ref-qualifier & (for C++20: unless its cv-qualifier-seq
2634 : is const). In a .* expression whose object expression is an lvalue,
2635 : the program is ill-formed if the second operand is a pointer to member
2636 : function with ref-qualifier &&. */
2637 105377 : if (FUNCTION_REF_QUALIFIED (type))
2638 : {
2639 136 : bool lval = lvalue_p (datum);
2640 136 : if (lval && FUNCTION_RVALUE_QUALIFIED (type))
2641 : {
2642 15 : if (complain & tf_error)
2643 6 : error ("pointer-to-member-function type %qT requires an rvalue",
2644 : ptrmem_type);
2645 15 : return error_mark_node;
2646 : }
2647 121 : else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
2648 : {
2649 33 : if ((type_memfn_quals (type)
2650 33 : & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))
2651 : != TYPE_QUAL_CONST)
2652 : {
2653 27 : if (complain & tf_error)
2654 24 : error ("pointer-to-member-function type %qT requires "
2655 : "an lvalue", ptrmem_type);
2656 27 : return error_mark_node;
2657 : }
2658 6 : else if (cxx_dialect < cxx20)
2659 : {
2660 3 : if (complain & tf_warning_or_error)
2661 3 : pedwarn (input_location, OPT_Wpedantic,
2662 : "pointer-to-member-function type %qT requires "
2663 : "an lvalue before C++20", ptrmem_type);
2664 : else
2665 0 : return error_mark_node;
2666 : }
2667 : }
2668 : }
2669 105335 : return build2 (OFFSET_REF, type, datum, component);
2670 : }
2671 : }
2672 :
2673 : /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
2674 :
2675 : static tree
2676 76082043 : build_functional_cast_1 (location_t loc, tree exp, tree parms,
2677 : tsubst_flags_t complain)
2678 : {
2679 : /* This is either a call to a constructor,
2680 : or a C cast in C++'s `functional' notation. */
2681 :
2682 : /* The type to which we are casting. */
2683 76082043 : tree type;
2684 :
2685 76082043 : if (error_operand_p (exp) || parms == error_mark_node)
2686 42315 : return error_mark_node;
2687 :
2688 76039728 : if (TREE_CODE (exp) == TYPE_DECL)
2689 : {
2690 44651380 : type = TREE_TYPE (exp);
2691 :
2692 44651380 : if (DECL_ARTIFICIAL (exp))
2693 28563180 : cp_handle_deprecated_or_unavailable (type);
2694 : }
2695 : else
2696 : type = exp;
2697 :
2698 : /* We need to check this explicitly, since value-initialization of
2699 : arrays is allowed in other situations. */
2700 76039728 : if (TREE_CODE (type) == ARRAY_TYPE)
2701 : {
2702 12 : if (complain & tf_error)
2703 6 : error_at (loc, "functional cast to array type %qT", type);
2704 12 : return error_mark_node;
2705 : }
2706 :
2707 76039716 : if (tree anode = type_uses_auto (type))
2708 : {
2709 663854 : tree init;
2710 663854 : if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2711 : init = parms;
2712 : /* C++23 auto(x). */
2713 200122 : else if (!AUTO_IS_DECLTYPE (anode)
2714 200122 : && list_length (parms) == 1)
2715 : {
2716 200107 : init = TREE_VALUE (parms);
2717 200107 : if (is_constrained_auto (anode))
2718 : {
2719 3 : if (complain & tf_error)
2720 3 : error_at (loc, "%<auto(x)%> cannot be constrained");
2721 3 : return error_mark_node;
2722 : }
2723 200104 : else if (cxx_dialect < cxx23)
2724 : {
2725 25 : if ((complain & tf_warning_or_error) == 0)
2726 2 : return error_mark_node;
2727 23 : pedwarn (loc, OPT_Wc__23_extensions,
2728 : "%<auto(x)%> only available with "
2729 : "%<-std=c++23%> or %<-std=gnu++23%>");
2730 : }
2731 : }
2732 : else
2733 : {
2734 15 : if (complain & tf_error)
2735 15 : error_at (loc, "invalid use of %qT", anode);
2736 15 : return error_mark_node;
2737 : }
2738 663834 : type = do_auto_deduction (type, init, anode, complain,
2739 : adc_variable_type);
2740 663834 : if (type == error_mark_node)
2741 : return error_mark_node;
2742 : }
2743 :
2744 76039542 : if (processing_template_decl)
2745 : {
2746 42534744 : tree t;
2747 :
2748 : /* Diagnose this even in a template. We could also try harder
2749 : to give all the usual errors when the type and args are
2750 : non-dependent... */
2751 42534744 : if (TYPE_REF_P (type) && !parms)
2752 : {
2753 3 : if (complain & tf_error)
2754 3 : error_at (loc, "invalid value-initialization of reference type");
2755 3 : return error_mark_node;
2756 : }
2757 :
2758 42534741 : t = build_min (CAST_EXPR, type, parms);
2759 : /* We don't know if it will or will not have side effects. */
2760 42534741 : TREE_SIDE_EFFECTS (t) = 1;
2761 42534741 : return convert_from_reference (t);
2762 : }
2763 :
2764 33504798 : if (! MAYBE_CLASS_TYPE_P (type))
2765 : {
2766 29232401 : if (parms == NULL_TREE)
2767 : {
2768 755301 : if (VOID_TYPE_P (type))
2769 4325 : return void_node;
2770 750976 : return build_value_init (cv_unqualified (type), complain);
2771 : }
2772 :
2773 : /* This must build a C cast. */
2774 28477100 : parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
2775 28477100 : return cp_build_c_cast (loc, type, parms, complain);
2776 : }
2777 :
2778 : /* Prepare to evaluate as a call to a constructor. If this expression
2779 : is actually used, for example,
2780 :
2781 : return X (arg1, arg2, ...);
2782 :
2783 : then the slot being initialized will be filled in. */
2784 :
2785 4272397 : if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
2786 18 : return error_mark_node;
2787 4272376 : if (abstract_virtuals_error (ACU_CAST, type, complain))
2788 21 : return error_mark_node;
2789 :
2790 : /* [expr.type.conv]
2791 :
2792 : If the expression list is a single-expression, the type
2793 : conversion is equivalent (in definedness, and if defined in
2794 : meaning) to the corresponding cast expression. */
2795 4272355 : if (parms && TREE_CHAIN (parms) == NULL_TREE)
2796 2795336 : return cp_build_c_cast (loc, type, TREE_VALUE (parms), complain);
2797 :
2798 : /* [expr.type.conv]
2799 :
2800 : The expression T(), where T is a simple-type-specifier for a
2801 : non-array complete object type or the (possibly cv-qualified)
2802 : void type, creates an rvalue of the specified type, which is
2803 : value-initialized. */
2804 :
2805 1477019 : if (parms == NULL_TREE)
2806 : {
2807 1002097 : exp = build_value_init (type, complain);
2808 1002097 : exp = get_target_expr (exp, complain);
2809 1002097 : return exp;
2810 : }
2811 :
2812 : /* Call the constructor. */
2813 474922 : releasing_vec parmvec;
2814 1496443 : for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
2815 1021521 : vec_safe_push (parmvec, TREE_VALUE (parms));
2816 474922 : exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2817 : &parmvec, type, LOOKUP_NORMAL, complain);
2818 :
2819 474922 : if (exp == error_mark_node)
2820 : return error_mark_node;
2821 :
2822 474876 : return build_cplus_new (type, exp, complain);
2823 474922 : }
2824 :
2825 : tree
2826 76082043 : build_functional_cast (location_t loc, tree exp, tree parms,
2827 : tsubst_flags_t complain)
2828 : {
2829 76082043 : tree result = build_functional_cast_1 (loc, exp, parms, complain);
2830 76082040 : protected_set_expr_location (result, loc);
2831 76082040 : return result;
2832 : }
2833 :
2834 :
2835 : /* Add new exception specifier SPEC, to the LIST we currently have.
2836 : If it's already in LIST then do nothing.
2837 : Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2838 : know what we're doing. */
2839 :
2840 : tree
2841 15550 : add_exception_specifier (tree list, tree spec, tsubst_flags_t complain)
2842 : {
2843 15550 : bool ok;
2844 15550 : tree core = spec;
2845 15550 : bool is_ptr;
2846 15550 : enum diagnostics::kind diag_type = diagnostics::kind::unspecified; /* none */
2847 :
2848 15550 : if (spec == error_mark_node)
2849 : return list;
2850 :
2851 15575 : gcc_assert (spec && (!list || TREE_VALUE (list)));
2852 :
2853 : /* [except.spec] 1, type in an exception specifier shall not be
2854 : incomplete, or pointer or ref to incomplete other than pointer
2855 : to cv void. */
2856 15536 : is_ptr = TYPE_PTR_P (core);
2857 15536 : if (is_ptr || TYPE_REF_P (core))
2858 19 : core = TREE_TYPE (core);
2859 15536 : if (complain < 0)
2860 : ok = true;
2861 1316 : else if (VOID_TYPE_P (core))
2862 : ok = is_ptr;
2863 1308 : else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
2864 : ok = true;
2865 1298 : else if (processing_template_decl)
2866 : ok = true;
2867 1261 : else if (!verify_type_context (input_location, TCTX_EXCEPTIONS, core,
2868 : !(complain & tf_error)))
2869 0 : return error_mark_node;
2870 : else
2871 : {
2872 1261 : ok = true;
2873 : /* 15.4/1 says that types in an exception specifier must be complete,
2874 : but it seems more reasonable to only require this on definitions
2875 : and calls. So just give a pedwarn at this point; we will give an
2876 : error later if we hit one of those two cases. */
2877 1261 : if (!COMPLETE_TYPE_P (complete_type (core)))
2878 15536 : diag_type = diagnostics::kind::pedwarn; /* pedwarn */
2879 : }
2880 :
2881 15536 : if (ok)
2882 : {
2883 : tree probe;
2884 :
2885 15575 : for (probe = list; probe; probe = TREE_CHAIN (probe))
2886 47 : if (same_type_p (TREE_VALUE (probe), spec))
2887 : break;
2888 15530 : if (!probe)
2889 15528 : list = tree_cons (NULL_TREE, spec, list);
2890 : }
2891 : else
2892 : diag_type = diagnostics::kind::error; /* error */
2893 :
2894 15530 : if (diag_type != diagnostics::kind::unspecified
2895 12 : && (complain & tf_warning_or_error))
2896 10 : cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
2897 :
2898 : return list;
2899 : }
2900 :
2901 : /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2902 :
2903 : static bool
2904 4600077 : nothrow_spec_p_uninst (const_tree spec)
2905 : {
2906 9200154 : if (DEFERRED_NOEXCEPT_SPEC_P (spec))
2907 : return false;
2908 4600075 : return nothrow_spec_p (spec);
2909 : }
2910 :
2911 : /* Combine the two exceptions specifier lists LIST and ADD, and return
2912 : their union. */
2913 :
2914 : tree
2915 12419831 : merge_exception_specifiers (tree list, tree add)
2916 : {
2917 12419831 : tree noex, orig_list;
2918 :
2919 12419831 : if (list == error_mark_node || add == error_mark_node)
2920 : return error_mark_node;
2921 :
2922 : /* No exception-specifier or noexcept(false) are less strict than
2923 : anything else. Prefer the newer variant (LIST). */
2924 12419831 : if (!list || list == noexcept_false_spec)
2925 : return list;
2926 4685727 : else if (!add || add == noexcept_false_spec)
2927 : return add;
2928 :
2929 : /* noexcept(true) and throw() are stricter than anything else.
2930 : As above, prefer the more recent one (LIST). */
2931 4561969 : if (nothrow_spec_p_uninst (add))
2932 : return list;
2933 :
2934 : /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2935 38110 : if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
2936 2 : && UNEVALUATED_NOEXCEPT_SPEC_P (list))
2937 : return list;
2938 : /* We should have instantiated other deferred noexcept specs by now. */
2939 38108 : gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
2940 :
2941 38108 : if (nothrow_spec_p_uninst (list))
2942 : return add;
2943 38105 : noex = TREE_PURPOSE (list);
2944 38105 : gcc_checking_assert (!TREE_PURPOSE (add)
2945 : || errorcount || !flag_exceptions
2946 : || cp_tree_equal (noex, TREE_PURPOSE (add)));
2947 :
2948 : /* Combine the dynamic-exception-specifiers, if any. */
2949 : orig_list = list;
2950 76245 : for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
2951 : {
2952 40 : tree spec = TREE_VALUE (add);
2953 : tree probe;
2954 :
2955 74 : for (probe = orig_list; probe && TREE_VALUE (probe);
2956 12 : probe = TREE_CHAIN (probe))
2957 34 : if (same_type_p (TREE_VALUE (probe), spec))
2958 : break;
2959 28 : if (!probe)
2960 : {
2961 6 : spec = build_tree_list (NULL_TREE, spec);
2962 6 : TREE_CHAIN (spec) = list;
2963 6 : list = spec;
2964 : }
2965 : }
2966 :
2967 : /* Keep the noexcept-specifier at the beginning of the list. */
2968 38105 : if (noex != TREE_PURPOSE (list))
2969 0 : list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2970 :
2971 : return list;
2972 : }
2973 :
2974 : /* Subroutine of build_call. Ensure that each of the types in the
2975 : exception specification is complete. Technically, 15.4/1 says that
2976 : they need to be complete when we see a declaration of the function,
2977 : but we should be able to get away with only requiring this when the
2978 : function is defined or called. See also add_exception_specifier. */
2979 :
2980 : void
2981 168112658 : require_complete_eh_spec_types (tree fntype, tree decl)
2982 : {
2983 168112658 : tree raises;
2984 : /* Don't complain about calls to op new. */
2985 168112658 : if (decl && DECL_ARTIFICIAL (decl))
2986 : return;
2987 244944515 : for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2988 88170888 : raises = TREE_CHAIN (raises))
2989 : {
2990 88170888 : tree type = TREE_VALUE (raises);
2991 88170888 : if (type && !COMPLETE_TYPE_P (type))
2992 : {
2993 1 : if (decl)
2994 1 : error
2995 1 : ("call to function %qD which throws incomplete type %q#T",
2996 : decl, type);
2997 : else
2998 0 : error ("call to function which throws incomplete type %q#T",
2999 : decl);
3000 : }
3001 : }
3002 : }
3003 :
3004 : /* Record that any TARGET_EXPR in T are going to be elided in
3005 : cp_gimplify_init_expr (or sooner). */
3006 :
3007 : void
3008 173105836 : set_target_expr_eliding (tree t)
3009 : {
3010 175870618 : if (!t)
3011 : return;
3012 175080833 : switch (TREE_CODE (t))
3013 : {
3014 13287656 : case TARGET_EXPR:
3015 13287656 : TARGET_EXPR_ELIDING_P (t) = true;
3016 13287656 : break;
3017 1637399 : case COMPOUND_EXPR:
3018 1637399 : set_target_expr_eliding (TREE_OPERAND (t, 1));
3019 1637399 : break;
3020 1127383 : case COND_EXPR:
3021 1127383 : set_target_expr_eliding (TREE_OPERAND (t, 1));
3022 1127383 : set_target_expr_eliding (TREE_OPERAND (t, 2));
3023 1127383 : break;
3024 :
3025 : default:
3026 : break;
3027 : }
3028 : }
3029 :
3030 : /* Call the above in the process of building an INIT_EXPR. */
3031 :
3032 : tree
3033 65285463 : cp_build_init_expr (location_t loc, tree target, tree init)
3034 : {
3035 65285463 : set_target_expr_eliding (init);
3036 65285463 : tree ie = build2_loc (loc, INIT_EXPR, TREE_TYPE (target),
3037 : target, init);
3038 65285463 : TREE_SIDE_EFFECTS (ie) = true;
3039 65285463 : return ie;
3040 : }
|