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