Debian Codegraph @

gcc/cp/typeck2.cc

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