libstdc++: Ensure std::variant relops convert to bool [PR115145]
[official-gcc.git] / gcc / cp / typeck2.cc
blob06bad4d3303f86aa2d27785abadb495dc2c7bcb3
1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987-2024 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
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.
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.
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/>. */
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. */
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"
38 static tree
39 process_init_constructor (tree type, tree init, int nested, int flags,
40 tsubst_flags_t complain);
43 /* Print an error message stemming from an attempt to use
44 BASETYPE as a base class for TYPE. */
46 tree
47 error_not_base_type (tree basetype, tree type)
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;
55 tree
56 binfo_or_else (tree base, tree type)
58 tree binfo = lookup_base (type, base, ba_unique,
59 NULL, tf_warning_or_error);
61 if (binfo == error_mark_node)
62 return NULL_TREE;
63 else if (!binfo)
64 error_not_base_type (base, type);
65 return binfo;
68 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
69 value may not be changed thereafter. */
71 void
72 cxx_readonly_error (location_t loc, tree arg, enum lvalue_use errstring)
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. */
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)
100 /* Handle C++-specific things first. */
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);
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. */
132 static int
133 abstract_virtuals_error (tree decl, tree type, abstract_class_use use,
134 tsubst_flags_t complain)
136 vec<tree, va_gc> *pure;
138 if (TREE_CODE (type) == ARRAY_TYPE)
140 decl = NULL_TREE;
141 use = ACU_ARRAY;
142 type = strip_array_types (type);
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);
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
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;
164 pure = CLASSTYPE_PURE_VIRTUALS (type);
165 if (!pure)
166 return 0;
168 if (!(complain & tf_error))
169 return 1;
171 auto_diagnostic_group d;
172 if (decl)
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)
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);
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);
200 else switch (use)
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);
229 /* Only go through this once. */
230 if (pure->length ())
232 unsigned ix;
233 tree fn;
235 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
236 " because the following virtual functions are pure within %qT:",
237 type);
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);
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);
250 return 1;
254 abstract_virtuals_error (tree decl, tree type,
255 tsubst_flags_t complain /* = tf_warning_or_error */)
257 return abstract_virtuals_error (decl, type, ACU_UNKNOWN, complain);
261 abstract_virtuals_error (abstract_class_use use, tree type,
262 tsubst_flags_t complain /* = tf_warning_or_error */)
264 return abstract_virtuals_error (NULL_TREE, type, use, complain);
268 /* Print an inform about the declaration of the incomplete type TYPE. */
270 void
271 cxx_incomplete_type_inform (const_tree type)
273 if (!TYPE_MAIN_DECL (type))
274 return;
276 location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type));
277 tree ptype = strip_top_quals (CONST_CAST_TREE (type));
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);
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). */
295 bool
296 cxx_incomplete_type_diagnostic (location_t loc, const_tree value,
297 const_tree type, diagnostic_t diag_kind)
299 bool is_decl = false, complained = false;
301 /* Avoid duplicate error message. */
302 if (TREE_CODE (type) == ERROR_MARK)
303 return false;
305 if (value)
307 STRIP_ANY_LOCATION_WRAPPER (value);
309 if (VAR_P (value)
310 || TREE_CODE (value) == PARM_DECL
311 || TREE_CODE (value) == FIELD_DECL)
313 complained = emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (value), 0,
314 "%qD has incomplete type", value);
315 is_decl = true;
318 retry:
319 /* We must print an error message. Be clever about what it says. */
321 switch (TREE_CODE (type))
323 case RECORD_TYPE:
324 case UNION_TYPE:
325 case ENUMERAL_TYPE:
326 if (!is_decl)
327 complained = emit_diagnostic (diag_kind, loc, 0,
328 "invalid use of incomplete type %q#T",
329 type);
330 if (complained)
331 cxx_incomplete_type_inform (type);
332 break;
334 case VOID_TYPE:
335 complained = emit_diagnostic (diag_kind, loc, 0,
336 "invalid use of %qT", type);
337 break;
339 case ARRAY_TYPE:
340 if (TYPE_DOMAIN (type))
342 type = TREE_TYPE (type);
343 goto retry;
345 complained = emit_diagnostic (diag_kind, loc, 0,
346 "invalid use of array with unspecified bounds");
347 break;
349 case OFFSET_TYPE:
350 bad_member:
352 tree member = TREE_OPERAND (value, 1);
353 if (is_overloaded_fn (member) && !flag_ms_extensions)
355 gcc_rich_location richloc (loc);
356 /* If "member" has no arguments (other than "this"), then
357 add a fix-it hint. */
358 member = MAYBE_BASELINK_FUNCTIONS (member);
359 if (TREE_CODE (member) == FUNCTION_DECL
360 && DECL_OBJECT_MEMBER_FUNCTION_P (member)
361 && type_num_arguments (TREE_TYPE (member)) == 1)
362 richloc.add_fixit_insert_after ("()");
363 complained = emit_diagnostic (diag_kind, &richloc, 0,
364 "invalid use of member function %qD "
365 "(did you forget the %<()%> ?)", member);
367 else
368 complained = emit_diagnostic (diag_kind, loc, 0,
369 "invalid use of member %qD "
370 "(did you forget the %<&%> ?)", member);
372 break;
374 case TEMPLATE_TYPE_PARM:
375 if (is_auto (type))
377 if (CLASS_PLACEHOLDER_TEMPLATE (type))
378 complained = emit_diagnostic (diag_kind, loc, 0,
379 "invalid use of placeholder %qT", type);
380 else
381 complained = emit_diagnostic (diag_kind, loc, 0,
382 "invalid use of %qT", type);
384 else
385 complained = emit_diagnostic (diag_kind, loc, 0,
386 "invalid use of template type parameter %qT", type);
387 break;
389 case BOUND_TEMPLATE_TEMPLATE_PARM:
390 complained = emit_diagnostic (diag_kind, loc, 0,
391 "invalid use of template template parameter %qT",
392 TYPE_NAME (type));
393 break;
395 case TYPE_PACK_EXPANSION:
396 complained = emit_diagnostic (diag_kind, loc, 0,
397 "invalid use of pack expansion %qT", type);
398 break;
400 case TYPENAME_TYPE:
401 case DECLTYPE_TYPE:
402 complained = emit_diagnostic (diag_kind, loc, 0,
403 "invalid use of dependent type %qT", type);
404 break;
406 case LANG_TYPE:
407 if (type == init_list_type_node)
409 complained = emit_diagnostic (diag_kind, loc, 0,
410 "invalid use of brace-enclosed initializer list");
411 break;
413 gcc_assert (type == unknown_type_node);
414 if (value && TREE_CODE (value) == COMPONENT_REF)
415 goto bad_member;
416 else if (value && TREE_CODE (value) == ADDR_EXPR)
417 complained = emit_diagnostic (diag_kind, loc, 0,
418 "address of overloaded function with no contextual "
419 "type information");
420 else if (value && TREE_CODE (value) == OVERLOAD)
421 complained = emit_diagnostic (diag_kind, loc, 0,
422 "overloaded function with no contextual type information");
423 else
424 complained = emit_diagnostic (diag_kind, loc, 0,
425 "insufficient contextual information to determine type");
426 break;
428 default:
429 gcc_unreachable ();
432 return complained;
435 /* Print an error message for invalid use of an incomplete type.
436 VALUE is the expression that was used (or 0 if that isn't known)
437 and TYPE is the type that was invalid. */
439 void
440 cxx_incomplete_type_error (location_t loc, const_tree value, const_tree type)
442 cxx_incomplete_type_diagnostic (loc, value, type, DK_ERROR);
446 /* We've just initialized subobject SUB; also insert a TARGET_EXPR with an
447 EH-only cleanup for SUB. Because of EH region nesting issues, we need to
448 make the cleanup conditional on a flag that we will clear once the object is
449 fully initialized, so push a new flag onto FLAGS. */
451 static void
452 maybe_push_temp_cleanup (tree sub, vec<tree,va_gc> **flags)
454 if (!flag_exceptions)
455 return;
456 if (tree cleanup
457 = cxx_maybe_build_cleanup (sub, tf_warning_or_error))
459 tree tx = get_target_expr (boolean_true_node);
460 tree flag = TARGET_EXPR_SLOT (tx);
461 CLEANUP_EH_ONLY (tx) = true;
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);
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. */
472 tree
473 build_disable_temp_cleanup (tree f)
475 tree d = f;
476 tree i = boolean_false_node;
477 if (TREE_CODE (f) == TREE_LIST)
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);
485 return build2 (MODIFY_EXPR, TREE_TYPE (d), d, i);
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. */
493 static bool
494 split_nonconstant_init_1 (tree dest, tree init, bool last,
495 vec<tree,va_gc> **flags)
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;
506 switch (TREE_CODE (type))
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))
515 if (!TYPE_DOMAIN (type)
516 && TREE_CODE (init) == CONSTRUCTOR
517 && CONSTRUCTOR_NELTS (init))
519 /* Flexible array. */
520 cp_complete_array_type (&type, init, /*default*/true);
521 dest = build1 (VIEW_CONVERT_EXPR, type, dest);
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;
531 /* FALLTHRU */
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)
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);
544 if (!array_type_p)
545 inner_type = TREE_TYPE (field_index);
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);
555 bool elt_last = last && idx == CONSTRUCTOR_NELTS (init) - 1;
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));
561 if (TREE_CODE (value) == CONSTRUCTOR)
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
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++;
588 else if (tree vi = get_vec_init_expr (value))
590 add_stmt (expand_vec_init_expr (sub, vi, tf_warning_or_error,
591 flags));
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++;
600 else if (!initializer_constant_valid_p (value, inner_type))
602 tree code;
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))
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))
618 tree pcref = build3 (COMPONENT_REF, ptype, dest, prev,
619 NULL_TREE);
620 maybe_push_temp_cleanup (pcref, flags);
624 /* Mark element for removal. */
625 CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE;
626 if (idx < tidx)
627 tidx = idx;
629 if (TREE_CODE (field_index) == RANGE_EXPR)
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);
644 else
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))
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;
657 tree name = (DECL_FIELD_IS_BASE (field_index)
658 ? base_ctor_identifier
659 : complete_ctor_identifier);
660 releasing_vec args = make_tree_vector_single (value);
661 code = build_special_member_call
662 (sub, name, &args, inner_type,
663 LOOKUP_NORMAL, tf_warning_or_error);
665 else
667 build_init:
668 code = cp_build_init_expr (sub, value);
670 code = build_stmt (input_location, EXPR_STMT, code);
671 add_stmt (code);
672 if (!elt_last)
673 maybe_push_temp_cleanup (sub, flags);
676 last_split_elt = field_index;
677 num_split_elts++;
680 if (num_split_elts == 1)
681 CONSTRUCTOR_ELTS (init)->ordered_remove (tidx);
682 else if (num_split_elts > 1)
684 /* Perform the delayed ordered removal of non-constant elements
685 we split out. */
686 for (idx = tidx; idx < CONSTRUCTOR_NELTS (init); ++idx)
687 if (CONSTRUCTOR_ELT (init, idx)->index == NULL_TREE)
689 else
691 *CONSTRUCTOR_ELT (init, tidx) = *CONSTRUCTOR_ELT (init, idx);
692 ++tidx;
694 vec_safe_truncate (CONSTRUCTOR_ELTS (init), tidx);
696 break;
698 case VECTOR_TYPE:
699 if (!initializer_constant_valid_p (init, type))
701 tree code;
702 tree cons = copy_node (init);
703 CONSTRUCTOR_ELTS (init) = NULL;
704 code = build2 (MODIFY_EXPR, type, dest, cons);
705 code = build_stmt (input_location, EXPR_STMT, code);
706 add_stmt (code);
707 num_split_elts += CONSTRUCTOR_NELTS (init);
709 break;
711 default:
712 gcc_unreachable ();
715 /* The rest of the initializer is now a constant. */
716 TREE_CONSTANT (init) = 1;
717 TREE_SIDE_EFFECTS (init) = 0;
719 /* We didn't split out anything. */
720 if (num_split_elts == 0)
721 return false;
723 return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
724 num_split_elts, inner_type);
727 /* A subroutine of store_init_value. Splits non-constant static
728 initializer INIT into a constant part and generates code to
729 perform the non-constant part of the initialization to DEST.
730 Returns the code for the runtime init. */
732 tree
733 split_nonconstant_init (tree dest, tree init)
735 tree code;
737 if (TREE_CODE (init) == TARGET_EXPR)
738 init = TARGET_EXPR_INITIAL (init);
739 if (TREE_CODE (init) == CONSTRUCTOR)
741 /* Subobject initializers are not full-expressions. */
742 auto fe = (make_temp_override
743 (current_stmt_tree ()->stmts_are_full_exprs_p, 0));
745 init = cp_fully_fold_init (init);
746 code = push_stmt_list ();
748 /* If the complete object is an array, build_vec_init's cleanup is
749 enough. Otherwise, collect flags for disabling subobject
750 cleanups once the complete object is fully constructed. */
751 vec<tree, va_gc> *flags = nullptr;
752 if (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE)
753 flags = make_tree_vector ();
755 if (split_nonconstant_init_1 (dest, init, true, &flags))
756 init = NULL_TREE;
758 for (tree f : flags)
759 add_stmt (build_disable_temp_cleanup (f));
760 release_tree_vector (flags);
762 code = pop_stmt_list (code);
763 if (VAR_P (dest) && !is_local_temp (dest))
765 DECL_INITIAL (dest) = init;
766 TREE_READONLY (dest) = 0;
768 else if (init)
770 tree ie = cp_build_init_expr (dest, init);
771 code = add_stmt_to_compound (ie, code);
774 else if (TREE_CODE (init) == STRING_CST
775 && array_of_runtime_bound_p (TREE_TYPE (dest)))
776 code = build_vec_init (dest, NULL_TREE, init, /*value-init*/false,
777 /*from array*/1, tf_warning_or_error);
778 else
779 code = cp_build_init_expr (dest, init);
781 return code;
784 /* T is the initializer of a constexpr variable. Set CONSTRUCTOR_MUTABLE_POISON
785 for any CONSTRUCTOR within T that contains (directly or indirectly) a mutable
786 member, thereby poisoning it so it can't be copied to another a constexpr
787 variable or read during constexpr evaluation. */
789 static void
790 poison_mutable_constructors (tree t)
792 if (TREE_CODE (t) != CONSTRUCTOR)
793 return;
795 if (cp_has_mutable_p (TREE_TYPE (t)))
797 CONSTRUCTOR_MUTABLE_POISON (t) = true;
799 if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (t))
800 for (const constructor_elt &ce : *elts)
801 poison_mutable_constructors (ce.value);
805 /* Perform appropriate conversions on the initial value of a variable,
806 store it in the declaration DECL,
807 and print any error messages that are appropriate.
808 If the init is invalid, store an ERROR_MARK.
810 C++: Note that INIT might be a TREE_LIST, which would mean that it is
811 a base class initializer for some aggregate type, hopefully compatible
812 with DECL. If INIT is a single element, and DECL is an aggregate
813 type, we silently convert INIT into a TREE_LIST, allowing a constructor
814 to be called.
816 If INIT is a TREE_LIST and there is no constructor, turn INIT
817 into a CONSTRUCTOR and use standard initialization techniques.
818 Perhaps a warning should be generated?
820 Returns code to be executed if initialization could not be performed
821 for static variable. In that case, caller must emit the code. */
823 tree
824 store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
826 tree value, type;
828 /* If variable's type was invalidly declared, just ignore it. */
830 type = TREE_TYPE (decl);
831 if (TREE_CODE (type) == ERROR_MARK)
832 return NULL_TREE;
834 if (MAYBE_CLASS_TYPE_P (type))
836 if (TREE_CODE (init) == TREE_LIST)
838 error ("constructor syntax used, but no constructor declared "
839 "for type %qT", type);
840 init = build_constructor_from_list (init_list_type_node, nreverse (init));
844 /* End of special C++ code. */
846 if (flags & LOOKUP_ALREADY_DIGESTED)
847 value = init;
848 else
850 if (TREE_STATIC (decl))
851 flags |= LOOKUP_ALLOW_FLEXARRAY_INIT;
852 /* Digest the specified initializer into an expression. */
853 value = digest_init_flags (type, init, flags, tf_warning_or_error);
856 /* Look for braced array initializers for character arrays and
857 recursively convert them into STRING_CSTs. */
858 value = braced_lists_to_strings (type, value);
860 current_ref_temp_count = 0;
861 value = extend_ref_init_temps (decl, value, cleanups);
863 /* In C++11 constant expression is a semantic, not syntactic, property.
864 In C++98, make sure that what we thought was a constant expression at
865 template definition time is still constant and otherwise perform this
866 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
867 if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
869 bool const_init;
870 tree oldval = value;
871 if (DECL_DECLARED_CONSTEXPR_P (decl)
872 || DECL_DECLARED_CONSTINIT_P (decl)
873 || (DECL_IN_AGGR_P (decl)
874 && DECL_INITIALIZED_IN_CLASS_P (decl)))
876 value = fold_non_dependent_expr (value, tf_warning_or_error,
877 /*manifestly_const_eval=*/true,
878 decl);
879 if (value == error_mark_node)
881 /* Diagnose a non-constant initializer for constexpr variable or
882 non-inline in-class-initialized static data member. */
883 else if (!is_constant_expression (value))
885 /* Maybe we want to give this message for constexpr variables as
886 well, but that will mean a lot of testsuite adjustment. */
887 if (DECL_DECLARED_CONSTINIT_P (decl))
888 error_at (location_of (decl),
889 "%<constinit%> variable %qD does not have a "
890 "constant initializer", decl);
891 require_constant_expression (value);
892 value = error_mark_node;
894 else
896 value = maybe_constant_init (value, decl, true);
898 /* In a template we might not have done the necessary
899 transformations to make value actually constant,
900 e.g. extend_ref_init_temps. */
901 if (!processing_template_decl
902 && !TREE_CONSTANT (value))
904 if (DECL_DECLARED_CONSTINIT_P (decl))
905 error_at (location_of (decl),
906 "%<constinit%> variable %qD does not have a "
907 "constant initializer", decl);
908 value = cxx_constant_init (value, decl);
912 else
913 value = fold_non_dependent_init (value, tf_warning_or_error,
914 /*manifestly_const_eval=*/true, decl);
915 poison_mutable_constructors (value);
916 const_init = (reduced_constant_expression_p (value)
917 || error_operand_p (value));
918 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
919 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
920 if (!TYPE_REF_P (type))
921 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
922 if (!const_init)
923 value = oldval;
925 /* Don't fold initializers of automatic variables in constexpr functions,
926 that might fold away something that needs to be diagnosed at constexpr
927 evaluation time. */
928 if (!current_function_decl
929 || !DECL_DECLARED_CONSTEXPR_P (current_function_decl)
930 || TREE_STATIC (decl))
931 value = cp_fully_fold_init (value);
933 /* Handle aggregate NSDMI in non-constant initializers, too. */
934 value = replace_placeholders (value, decl);
936 /* A COMPOUND_LITERAL_P CONSTRUCTOR is the syntactic form; by the time we get
937 here it should have been digested into an actual value for the type. */
938 gcc_checking_assert (TREE_CODE (value) != CONSTRUCTOR
939 || processing_template_decl
940 || VECTOR_TYPE_P (type)
941 || !TREE_HAS_CONSTRUCTOR (value));
943 /* If the initializer is not a constant, fill in DECL_INITIAL with
944 the bits that are constant, and then return an expression that
945 will perform the dynamic initialization. */
946 if (value != error_mark_node
947 && !processing_template_decl
948 && (TREE_SIDE_EFFECTS (value)
949 || vla_type_p (type)
950 || ! reduced_constant_expression_p (value)))
951 return split_nonconstant_init (decl, value);
953 /* DECL may change value; purge caches. */
954 clear_cv_and_fold_caches ();
956 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
957 is an automatic variable, the middle end will turn this into a
958 dynamic initialization later. */
959 DECL_INITIAL (decl) = value;
960 return NULL_TREE;
964 /* Give diagnostic about narrowing conversions within { }, or as part of
965 a converted constant expression. If CONST_ONLY, only check
966 constants. */
968 bool
969 check_narrowing (tree type, tree init, tsubst_flags_t complain,
970 bool const_only/*= false*/)
972 tree ftype = unlowered_expr_type (init);
973 bool ok = true;
974 REAL_VALUE_TYPE d;
976 if (((!warn_narrowing || !(complain & tf_warning))
977 && cxx_dialect == cxx98)
978 || !ARITHMETIC_TYPE_P (type)
979 /* Don't emit bogus warnings with e.g. value-dependent trees. */
980 || instantiation_dependent_expression_p (init))
981 return ok;
983 if (BRACE_ENCLOSED_INITIALIZER_P (init)
984 && TREE_CODE (type) == COMPLEX_TYPE)
986 tree elttype = TREE_TYPE (type);
987 if (CONSTRUCTOR_NELTS (init) > 0)
988 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value,
989 complain);
990 if (CONSTRUCTOR_NELTS (init) > 1)
991 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value,
992 complain);
993 return ok;
996 /* Even non-dependent expressions can still have template
997 codes like CAST_EXPR, so use *_non_dependent_expr to cope. */
998 init = fold_non_dependent_expr (init, complain, /*manifest*/true);
999 if (init == error_mark_node)
1000 return ok;
1002 /* If we were asked to only check constants, return early. */
1003 if (const_only && !TREE_CONSTANT (init))
1004 return ok;
1006 if (CP_INTEGRAL_TYPE_P (type)
1007 && SCALAR_FLOAT_TYPE_P (ftype))
1008 ok = false;
1009 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
1010 && CP_INTEGRAL_TYPE_P (type))
1012 if (TREE_CODE (ftype) == ENUMERAL_TYPE)
1013 /* Check for narrowing based on the values of the enumeration. */
1014 ftype = ENUM_UNDERLYING_TYPE (ftype);
1015 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
1016 TYPE_MAX_VALUE (ftype))
1017 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
1018 TYPE_MIN_VALUE (type)))
1019 && (TREE_CODE (init) != INTEGER_CST
1020 || !int_fits_type_p (init, type)))
1021 ok = false;
1023 /* [dcl.init.list]#7.2: "from long double to double or float, or from
1024 double to float". */
1025 else if (SCALAR_FLOAT_TYPE_P (ftype)
1026 && SCALAR_FLOAT_TYPE_P (type))
1028 if ((extended_float_type_p (ftype) || extended_float_type_p (type))
1029 ? /* "from a floating-point type T to another floating-point type
1030 whose floating-point conversion rank is neither greater than
1031 nor equal to that of T".
1032 So, it is ok if
1033 cp_compare_floating_point_conversion_ranks (ftype, type)
1034 returns -2 (type has greater conversion rank than ftype)
1035 or [-1..1] (type has equal conversion rank as ftype, possibly
1036 different subrank. Only do this if at least one of the
1037 types is extended floating-point type, otherwise keep doing
1038 what we did before (for the sake of non-standard
1039 backend types). */
1040 cp_compare_floating_point_conversion_ranks (ftype, type) >= 2
1041 : ((same_type_p (ftype, long_double_type_node)
1042 && (same_type_p (type, double_type_node)
1043 || same_type_p (type, float_type_node)))
1044 || (same_type_p (ftype, double_type_node)
1045 && same_type_p (type, float_type_node))
1046 || (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))))
1048 if (TREE_CODE (init) == REAL_CST)
1050 /* Issue 703: Loss of precision is OK as long as the value is
1051 within the representable range of the new type. */
1052 REAL_VALUE_TYPE r;
1053 d = TREE_REAL_CST (init);
1054 real_convert (&r, TYPE_MODE (type), &d);
1055 if (real_isinf (&r))
1056 ok = false;
1058 else
1059 ok = false;
1062 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
1063 && SCALAR_FLOAT_TYPE_P (type))
1065 ok = false;
1066 if (TREE_CODE (init) == INTEGER_CST)
1068 d = real_value_from_int_cst (0, init);
1069 if (exact_real_truncate (TYPE_MODE (type), &d))
1070 ok = true;
1073 else if (TREE_CODE (type) == BOOLEAN_TYPE
1074 && (TYPE_PTR_P (ftype) || TYPE_PTRMEM_P (ftype)))
1075 /* C++20 P1957R2: converting from a pointer type or a pointer-to-member
1076 type to bool should be considered narrowing. This is a DR so is not
1077 limited to C++20 only. */
1078 ok = false;
1080 bool almost_ok = ok;
1081 if (!ok && !CONSTANT_CLASS_P (init) && (complain & tf_warning_or_error))
1083 tree folded = cp_fully_fold (init);
1084 if (TREE_CONSTANT (folded) && check_narrowing (type, folded, tf_none))
1085 almost_ok = true;
1088 if (!ok)
1090 location_t loc = cp_expr_loc_or_input_loc (init);
1091 if (cxx_dialect == cxx98)
1093 if (complain & tf_warning)
1094 warning_at (loc, OPT_Wnarrowing, "narrowing conversion of %qE "
1095 "from %qH to %qI is ill-formed in C++11",
1096 init, ftype, type);
1097 ok = true;
1099 else if (!CONSTANT_CLASS_P (init))
1101 if (complain & tf_warning_or_error)
1103 auto_diagnostic_group d;
1104 if ((!almost_ok || pedantic)
1105 && pedwarn (loc, OPT_Wnarrowing,
1106 "narrowing conversion of %qE from %qH to %qI",
1107 init, ftype, type)
1108 && almost_ok)
1109 inform (loc, " the expression has a constant value but is not "
1110 "a C++ constant-expression");
1111 ok = true;
1114 else if (complain & tf_error)
1116 int savederrorcount = errorcount;
1117 permerror_opt (loc, OPT_Wnarrowing,
1118 "narrowing conversion of %qE from %qH to %qI",
1119 init, ftype, type);
1120 if (errorcount == savederrorcount)
1121 ok = true;
1125 return ok;
1128 /* True iff TYPE is a C++20 "ordinary" character type. */
1130 bool
1131 ordinary_char_type_p (tree type)
1133 type = TYPE_MAIN_VARIANT (type);
1134 return (type == char_type_node
1135 || type == signed_char_type_node
1136 || type == unsigned_char_type_node);
1139 /* True iff the string literal INIT has a type suitable for initializing array
1140 TYPE. */
1142 bool
1143 array_string_literal_compatible_p (tree type, tree init)
1145 tree to_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1146 tree from_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
1148 if (to_char_type == from_char_type)
1149 return true;
1150 /* The array element type does not match the initializing string
1151 literal element type; this is only allowed when both types are
1152 ordinary character type. There are no string literals of
1153 signed or unsigned char type in the language, but we can get
1154 them internally from converting braced-init-lists to
1155 STRING_CST. */
1156 if (ordinary_char_type_p (to_char_type)
1157 && ordinary_char_type_p (from_char_type))
1158 return true;
1160 /* P2513 (C++20/C++23): "an array of char or unsigned char may
1161 be initialized by a UTF-8 string literal, or by such a string
1162 literal enclosed in braces." */
1163 if (from_char_type == char8_type_node
1164 && (to_char_type == char_type_node
1165 || to_char_type == unsigned_char_type_node))
1166 return true;
1168 return false;
1171 /* Process the initializer INIT for a variable of type TYPE, emitting
1172 diagnostics for invalid initializers and converting the initializer as
1173 appropriate.
1175 For aggregate types, it assumes that reshape_init has already run, thus the
1176 initializer will have the right shape (brace elision has been undone).
1178 NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR,
1179 2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR. */
1181 static tree
1182 digest_init_r (tree type, tree init, int nested, int flags,
1183 tsubst_flags_t complain)
1185 enum tree_code code = TREE_CODE (type);
1187 if (error_operand_p (init))
1188 return error_mark_node;
1190 gcc_assert (init);
1192 /* We must strip the outermost array type when completing the type,
1193 because the its bounds might be incomplete at the moment. */
1194 if (!complete_type_or_maybe_complain (code == ARRAY_TYPE
1195 ? TREE_TYPE (type) : type, NULL_TREE,
1196 complain))
1197 return error_mark_node;
1199 location_t loc = cp_expr_loc_or_input_loc (init);
1201 tree stripped_init = init;
1203 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1204 && CONSTRUCTOR_IS_PAREN_INIT (init))
1205 flags |= LOOKUP_AGGREGATE_PAREN_INIT;
1207 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
1208 (g++.old-deja/g++.law/casts2.C). */
1209 if (TREE_CODE (init) == NON_LVALUE_EXPR)
1210 stripped_init = TREE_OPERAND (init, 0);
1212 stripped_init = tree_strip_any_location_wrapper (stripped_init);
1214 /* Initialization of an array of chars from a string constant. The initializer
1215 can be optionally enclosed in braces, but reshape_init has already removed
1216 them if they were present. */
1217 if (code == ARRAY_TYPE)
1219 if (nested && !TYPE_DOMAIN (type))
1220 /* C++ flexible array members have a null domain. */
1222 if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT)
1223 pedwarn (loc, OPT_Wpedantic,
1224 "initialization of a flexible array member");
1225 else
1227 if (complain & tf_error)
1228 error_at (loc, "non-static initialization of"
1229 " a flexible array member");
1230 return error_mark_node;
1234 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1235 if (char_type_p (typ1)
1236 && TREE_CODE (stripped_init) == STRING_CST)
1238 if (!array_string_literal_compatible_p (type, init))
1240 if (complain & tf_error)
1241 error_at (loc, "cannot initialize array of %qT from "
1242 "a string literal with type array of %qT",
1243 typ1,
1244 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init))));
1245 return error_mark_node;
1248 if (nested == 2 && !TYPE_DOMAIN (type))
1250 if (complain & tf_error)
1251 error_at (loc, "initialization of flexible array member "
1252 "in a nested context");
1253 return error_mark_node;
1256 if (type != TREE_TYPE (init)
1257 && !variably_modified_type_p (type, NULL_TREE))
1259 init = copy_node (init);
1260 TREE_TYPE (init) = type;
1261 /* If we have a location wrapper, then also copy the wrapped
1262 node, and update the copy's type. */
1263 if (location_wrapper_p (init))
1265 stripped_init = copy_node (stripped_init);
1266 TREE_OPERAND (init, 0) = stripped_init;
1267 TREE_TYPE (stripped_init) = type;
1270 if (TYPE_DOMAIN (type) && TREE_CONSTANT (TYPE_SIZE (type)))
1272 /* Not a flexible array member. */
1273 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1274 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1275 /* In C it is ok to subtract 1 from the length of the string
1276 because it's ok to ignore the terminating null char that is
1277 counted in the length of the constant, but in C++ this would
1278 be invalid. */
1279 if (size < TREE_STRING_LENGTH (stripped_init))
1281 permerror (loc, "initializer-string for %qT is too long",
1282 type);
1284 init = build_string (size,
1285 TREE_STRING_POINTER (stripped_init));
1286 TREE_TYPE (init) = type;
1289 return init;
1293 /* Handle scalar types (including conversions) and references. */
1294 if ((code != COMPLEX_TYPE || BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
1295 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1297 /* Narrowing is OK when initializing an aggregate from
1298 a parenthesized list. */
1299 if (nested && !(flags & LOOKUP_AGGREGATE_PAREN_INIT))
1300 flags |= LOOKUP_NO_NARROWING;
1301 init = convert_for_initialization (0, type, init, flags,
1302 ICR_INIT, NULL_TREE, 0,
1303 complain);
1305 return init;
1308 /* Come here only for aggregates: records, arrays, unions, complex numbers
1309 and vectors. */
1310 gcc_assert (code == ARRAY_TYPE
1311 || VECTOR_TYPE_P (type)
1312 || code == RECORD_TYPE
1313 || code == UNION_TYPE
1314 || code == OPAQUE_TYPE
1315 || code == COMPLEX_TYPE);
1317 /* "If T is a class type and the initializer list has a single
1318 element of type cv U, where U is T or a class derived from T,
1319 the object is initialized from that element." */
1320 if (cxx_dialect >= cxx11
1321 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
1322 && !CONSTRUCTOR_IS_DESIGNATED_INIT (stripped_init)
1323 && CONSTRUCTOR_NELTS (stripped_init) == 1
1324 && ((CLASS_TYPE_P (type) && !CLASSTYPE_NON_AGGREGATE (type))
1325 || VECTOR_TYPE_P (type)))
1327 tree elt = CONSTRUCTOR_ELT (stripped_init, 0)->value;
1328 if (reference_related_p (type, TREE_TYPE (elt)))
1330 /* In C++17, aggregates can have bases, thus participate in
1331 aggregate initialization. In the following case:
1333 struct B { int c; };
1334 struct D : B { };
1335 D d{{D{{42}}}};
1337 there's an extra set of braces, so the D temporary initializes
1338 the first element of d, which is the B base subobject. The base
1339 of type B is copy-initialized from the D temporary, causing
1340 object slicing. */
1341 tree field = next_aggregate_field (TYPE_FIELDS (type));
1342 if (field && DECL_FIELD_IS_BASE (field))
1344 if (warning_at (loc, 0, "initializing a base class of type %qT "
1345 "results in object slicing", TREE_TYPE (field)))
1346 inform (loc, "remove %<{ }%> around initializer");
1348 else if (flag_checking)
1349 /* We should have fixed this in reshape_init. */
1350 gcc_unreachable ();
1354 if (SIMPLE_TARGET_EXPR_P (stripped_init))
1355 stripped_init = TARGET_EXPR_INITIAL (stripped_init);
1357 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
1358 && !TYPE_NON_AGGREGATE_CLASS (type))
1359 return process_init_constructor (type, stripped_init, nested, flags,
1360 complain);
1361 else
1363 if (COMPOUND_LITERAL_P (stripped_init) && code == ARRAY_TYPE)
1365 if (complain & tf_error)
1366 error_at (loc, "cannot initialize aggregate of type %qT with "
1367 "a compound literal", type);
1369 return error_mark_node;
1372 if (code == ARRAY_TYPE
1373 && !BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
1375 /* Allow the result of build_array_copy and of
1376 build_value_init_noctor. */
1377 if ((TREE_CODE (stripped_init) == VEC_INIT_EXPR
1378 || TREE_CODE (stripped_init) == CONSTRUCTOR)
1379 && (same_type_ignoring_top_level_qualifiers_p
1380 (type, TREE_TYPE (init))))
1381 return init;
1383 if (complain & tf_error)
1384 error_at (loc, "array must be initialized with a brace-enclosed"
1385 " initializer");
1386 return error_mark_node;
1389 return convert_for_initialization (NULL_TREE, type, init,
1390 flags,
1391 ICR_INIT, NULL_TREE, 0,
1392 complain);
1396 tree
1397 digest_init (tree type, tree init, tsubst_flags_t complain)
1399 return digest_init_r (type, init, 0, LOOKUP_IMPLICIT, complain);
1402 tree
1403 digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain)
1405 return digest_init_r (type, init, 0, flags, complain);
1408 /* Callback to replace PLACEHOLDER_EXPRs in a TARGET_EXPR (which isn't used
1409 in the context of guaranteed copy elision). */
1411 static tree
1412 replace_placeholders_for_class_temp_r (tree *tp, int *, void *data)
1414 tree t = *tp;
1415 auto pset = static_cast<hash_set<tree> *>(data);
1417 /* We're looking for a TARGET_EXPR nested in the whole expression. */
1418 if (TREE_CODE (t) == TARGET_EXPR
1419 /* That serves as temporary materialization, not an initializer. */
1420 && !TARGET_EXPR_ELIDING_P (t)
1421 && !pset->add (t))
1423 tree init = TARGET_EXPR_INITIAL (t);
1424 while (TREE_CODE (init) == COMPOUND_EXPR)
1425 init = TREE_OPERAND (init, 1);
1426 if (TREE_CODE (init) == CONSTRUCTOR
1427 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init))
1429 tree obj = TARGET_EXPR_SLOT (t);
1430 replace_placeholders (init, obj);
1431 /* We should have dealt with all PLACEHOLDER_EXPRs. */
1432 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = false;
1433 gcc_checking_assert (!find_placeholders (init));
1436 /* TARGET_EXPRs initializing function arguments are not marked as eliding,
1437 even though gimplify_arg drops them on the floor. Don't go replacing
1438 placeholders in them. */
1439 else if (TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == AGGR_INIT_EXPR)
1440 for (int i = 0; i < call_expr_nargs (t); ++i)
1442 tree arg = get_nth_callarg (t, i);
1443 if (TREE_CODE (arg) == TARGET_EXPR && !TARGET_EXPR_ELIDING_P (arg))
1444 pset->add (arg);
1447 return NULL_TREE;
1450 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1451 tree
1452 digest_nsdmi_init (tree decl, tree init, tsubst_flags_t complain)
1454 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1456 tree type = TREE_TYPE (decl);
1457 if (DECL_BIT_FIELD_TYPE (decl))
1458 type = DECL_BIT_FIELD_TYPE (decl);
1459 int flags = LOOKUP_IMPLICIT;
1460 if (DIRECT_LIST_INIT_P (init))
1462 flags = LOOKUP_NORMAL;
1463 complain |= tf_no_cleanup;
1465 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1466 && CP_AGGREGATE_TYPE_P (type))
1467 init = reshape_init (type, init, complain);
1468 init = digest_init_flags (type, init, flags, complain);
1469 set_target_expr_eliding (init);
1471 /* We may have temporary materialization in a NSDMI, if the initializer
1472 has something like A{} in it. Digesting the {} could have introduced
1473 a PLACEHOLDER_EXPR referring to A. Now that we've got a TARGET_EXPR,
1474 we have an object we can refer to. The reason we bother doing this
1475 here is for code like
1477 struct A {
1478 int x;
1479 int y = x;
1482 struct B {
1483 int x = 0;
1484 int y = A{x}.y; // #1
1487 where in #1 we don't want to end up with two PLACEHOLDER_EXPRs for
1488 different types on the same level in a {} when lookup_placeholder
1489 wouldn't find a named object for the PLACEHOLDER_EXPR for A. Note,
1490 temporary materialization does not occur when initializing an object
1491 from a prvalue of the same type, therefore we must not replace the
1492 placeholder with a temporary object so that it can be elided. */
1493 hash_set<tree> pset;
1494 cp_walk_tree (&init, replace_placeholders_for_class_temp_r, &pset, nullptr);
1496 return init;
1499 /* Set of flags used within process_init_constructor to describe the
1500 initializers. */
1501 #define PICFLAG_ERRONEOUS 1
1502 #define PICFLAG_NOT_ALL_CONSTANT 2
1503 #define PICFLAG_NOT_ALL_SIMPLE 4
1504 #define PICFLAG_SIDE_EFFECTS 8
1505 #define PICFLAG_VEC_INIT 16
1507 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1508 describe it. */
1510 static int
1511 picflag_from_initializer (tree init)
1513 if (init == error_mark_node)
1514 return PICFLAG_ERRONEOUS;
1515 else if (!TREE_CONSTANT (init))
1517 if (TREE_SIDE_EFFECTS (init))
1518 return PICFLAG_SIDE_EFFECTS;
1519 else
1520 return PICFLAG_NOT_ALL_CONSTANT;
1522 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1523 return PICFLAG_NOT_ALL_SIMPLE;
1524 return 0;
1527 /* Adjust INIT for going into a CONSTRUCTOR. */
1529 static tree
1530 massage_init_elt (tree type, tree init, int nested, int flags,
1531 tsubst_flags_t complain)
1533 int new_flags = LOOKUP_IMPLICIT;
1534 if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT)
1535 new_flags |= LOOKUP_ALLOW_FLEXARRAY_INIT;
1536 if (flags & LOOKUP_AGGREGATE_PAREN_INIT)
1537 new_flags |= LOOKUP_AGGREGATE_PAREN_INIT;
1538 init = digest_init_r (type, init, nested ? 2 : 1, new_flags, complain);
1539 /* When we defer constant folding within a statement, we may want to
1540 defer this folding as well. Don't call this on CONSTRUCTORs because
1541 their elements have already been folded, and we must avoid folding
1542 the result of get_nsdmi. */
1543 if (TREE_CODE (init) != CONSTRUCTOR)
1545 tree t = fold_non_dependent_init (init, complain);
1546 if (TREE_CONSTANT (t))
1547 init = t;
1548 set_target_expr_eliding (init);
1550 return init;
1553 /* Subroutine of process_init_constructor, which will process an initializer
1554 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1555 which describe the initializers. */
1557 static int
1558 process_init_constructor_array (tree type, tree init, int nested, int flags,
1559 tsubst_flags_t complain)
1561 unsigned HOST_WIDE_INT i, len = 0;
1562 int picflags = 0;
1563 bool unbounded = false;
1564 constructor_elt *ce;
1565 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1567 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1568 || VECTOR_TYPE_P (type));
1570 if (TREE_CODE (type) == ARRAY_TYPE)
1572 /* C++ flexible array members have a null domain. */
1573 tree domain = TYPE_DOMAIN (type);
1574 if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1575 len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
1576 - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
1577 TYPE_PRECISION (TREE_TYPE (domain)),
1578 TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
1579 else
1580 unbounded = true; /* Take as many as there are. */
1582 if (nested == 2 && !domain && !vec_safe_is_empty (v))
1584 if (complain & tf_error)
1585 error_at (cp_expr_loc_or_input_loc (init),
1586 "initialization of flexible array member "
1587 "in a nested context");
1588 return PICFLAG_ERRONEOUS;
1591 else
1592 /* Vectors are like simple fixed-size arrays. */
1593 unbounded = !TYPE_VECTOR_SUBPARTS (type).is_constant (&len);
1595 /* There must not be more initializers than needed. */
1596 if (!unbounded && vec_safe_length (v) > len)
1598 if (complain & tf_error)
1599 error ("too many initializers for %qT", type);
1600 else
1601 return PICFLAG_ERRONEOUS;
1604 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1606 if (!ce->index)
1607 ce->index = size_int (i);
1608 else if (!check_array_designated_initializer (ce, i))
1609 ce->index = error_mark_node;
1610 gcc_assert (ce->value);
1611 ce->value
1612 = massage_init_elt (TREE_TYPE (type), ce->value, nested, flags,
1613 complain);
1615 gcc_checking_assert
1616 (ce->value == error_mark_node
1617 || (same_type_ignoring_top_level_qualifiers_p
1618 (strip_array_types (TREE_TYPE (type)),
1619 strip_array_types (TREE_TYPE (ce->value)))));
1621 picflags |= picflag_from_initializer (ce->value);
1622 /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer
1623 CONSTRUCTOR. */
1624 if (TREE_CODE (ce->value) == CONSTRUCTOR
1625 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value))
1627 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1628 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value) = 0;
1632 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1633 we must add initializers ourselves. */
1634 if (!unbounded)
1635 for (; i < len; ++i)
1637 tree next;
1639 if (type_build_ctor_call (TREE_TYPE (type)))
1641 /* If this type needs constructors run for default-initialization,
1642 we can't rely on the back end to do it for us, so make the
1643 initialization explicit by list-initializing from T{}. */
1644 next = build_constructor (init_list_type_node, NULL);
1645 next = massage_init_elt (TREE_TYPE (type), next, nested, flags,
1646 complain);
1647 if (initializer_zerop (next))
1648 /* The default zero-initialization is fine for us; don't
1649 add anything to the CONSTRUCTOR. */
1650 next = NULL_TREE;
1652 else if (!zero_init_p (TREE_TYPE (type)))
1653 next = build_zero_init (TREE_TYPE (type),
1654 /*nelts=*/NULL_TREE,
1655 /*static_storage_p=*/false);
1656 else
1657 /* The default zero-initialization is fine for us; don't
1658 add anything to the CONSTRUCTOR. */
1659 next = NULL_TREE;
1661 if (next)
1663 if (next != error_mark_node
1664 && (initializer_constant_valid_p (next, TREE_TYPE (next))
1665 != null_pointer_node))
1667 /* Use VEC_INIT_EXPR for non-constant initialization of
1668 trailing elements with no explicit initializers. */
1669 picflags |= PICFLAG_VEC_INIT;
1670 break;
1673 picflags |= picflag_from_initializer (next);
1674 /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer
1675 CONSTRUCTOR. */
1676 if (TREE_CODE (next) == CONSTRUCTOR
1677 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next))
1679 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1680 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next) = 0;
1682 if (len > i+1)
1684 tree range = build2 (RANGE_EXPR, size_type_node,
1685 build_int_cst (size_type_node, i),
1686 build_int_cst (size_type_node, len - 1));
1687 CONSTRUCTOR_APPEND_ELT (v, range, next);
1688 break;
1690 else
1691 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1693 else
1694 /* Don't bother checking all the other elements. */
1695 break;
1698 CONSTRUCTOR_ELTS (init) = v;
1699 return picflags;
1702 /* Subroutine of process_init_constructor, which will process an initializer
1703 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1704 the initializers. */
1706 static int
1707 process_init_constructor_record (tree type, tree init, int nested, int flags,
1708 tsubst_flags_t complain)
1710 vec<constructor_elt, va_gc> *v = NULL;
1711 tree field;
1712 int skipped = 0;
1714 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1715 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1716 gcc_assert (!TYPE_BINFO (type)
1717 || cxx_dialect >= cxx17
1718 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1719 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1721 restart:
1722 int picflags = 0;
1723 unsigned HOST_WIDE_INT idx = 0;
1724 int designator_skip = -1;
1725 /* Generally, we will always have an index for each initializer (which is
1726 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1727 reshape_init. So we need to handle both cases. */
1728 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1730 tree next;
1732 if (TREE_CODE (field) != FIELD_DECL
1733 || (DECL_ARTIFICIAL (field)
1734 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))))
1735 continue;
1737 if (DECL_UNNAMED_BIT_FIELD (field))
1738 continue;
1740 /* If this is a bitfield, first convert to the declared type. */
1741 tree fldtype = TREE_TYPE (field);
1742 if (DECL_BIT_FIELD_TYPE (field))
1743 fldtype = DECL_BIT_FIELD_TYPE (field);
1744 if (fldtype == error_mark_node)
1745 return PICFLAG_ERRONEOUS;
1747 next = NULL_TREE;
1748 if (idx < CONSTRUCTOR_NELTS (init))
1750 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1751 if (ce->index)
1753 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1754 latter case can happen in templates where lookup has to be
1755 deferred. */
1756 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1757 || identifier_p (ce->index));
1758 if (ce->index == field || ce->index == DECL_NAME (field))
1759 next = ce->value;
1760 else
1762 ce = NULL;
1763 if (designator_skip == -1)
1764 designator_skip = 1;
1767 else
1769 designator_skip = 0;
1770 next = ce->value;
1773 if (ce)
1775 gcc_assert (ce->value);
1776 next = massage_init_elt (fldtype, next, nested, flags, complain);
1777 /* We can't actually elide the temporary when initializing a
1778 potentially-overlapping field from a function that returns by
1779 value. */
1780 if (ce->index
1781 && TREE_CODE (next) == TARGET_EXPR
1782 && unsafe_copy_elision_p (ce->index, next))
1783 TARGET_EXPR_ELIDING_P (next) = false;
1784 ++idx;
1787 if (next == error_mark_node)
1788 /* We skip initializers for empty bases/fields, so skipping an invalid
1789 one could make us accept invalid code. */
1790 return PICFLAG_ERRONEOUS;
1791 else if (next)
1792 /* Already handled above. */;
1793 else if (DECL_INITIAL (field))
1795 if (skipped > 0)
1797 /* We're using an NSDMI past a field with implicit
1798 zero-init. Go back and make it explicit. */
1799 skipped = -1;
1800 vec_safe_truncate (v, 0);
1801 goto restart;
1803 /* C++14 aggregate NSDMI. */
1804 next = get_nsdmi (field, /*ctor*/false, complain);
1805 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
1806 && find_placeholders (next))
1807 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1809 else if (type_build_ctor_call (fldtype))
1811 /* If this type needs constructors run for
1812 default-initialization, we can't rely on the back end to do it
1813 for us, so build up TARGET_EXPRs. If the type in question is
1814 a class, just build one up; if it's an array, recurse. */
1815 next = build_constructor (init_list_type_node, NULL);
1816 next = massage_init_elt (fldtype, next, nested, flags, complain);
1817 if (TREE_CODE (next) == TARGET_EXPR
1818 && unsafe_copy_elision_p (field, next))
1819 TARGET_EXPR_ELIDING_P (next) = false;
1821 /* Warn when some struct elements are implicitly initialized. */
1822 if ((complain & tf_warning)
1823 && !cp_unevaluated_operand
1824 && !EMPTY_CONSTRUCTOR_P (init))
1825 warning (OPT_Wmissing_field_initializers,
1826 "missing initializer for member %qD", field);
1828 else
1830 if (TYPE_REF_P (fldtype))
1832 if (complain & tf_error)
1833 error ("member %qD is uninitialized reference", field);
1834 else
1835 return PICFLAG_ERRONEOUS;
1837 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype))
1839 if (complain & tf_error)
1840 error ("member %qD with uninitialized reference fields", field);
1841 else
1842 return PICFLAG_ERRONEOUS;
1844 /* Do nothing for flexible array members since they need not have any
1845 elements. Don't worry about 'skipped' because a flexarray has to
1846 be the last field. */
1847 else if (TREE_CODE (fldtype) == ARRAY_TYPE && !TYPE_DOMAIN (fldtype))
1848 continue;
1850 /* Warn when some struct elements are implicitly initialized
1851 to zero. */
1852 if ((complain & tf_warning)
1853 && !cp_unevaluated_operand
1854 && !EMPTY_CONSTRUCTOR_P (init)
1855 && !is_really_empty_class (fldtype, /*ignore_vptr*/false))
1856 warning (OPT_Wmissing_field_initializers,
1857 "missing initializer for member %qD", field);
1859 if (!zero_init_p (fldtype) || skipped < 0)
1861 if (TYPE_REF_P (fldtype))
1862 next = build_zero_cst (fldtype);
1863 else
1864 next = build_zero_init (fldtype, /*nelts=*/NULL_TREE,
1865 /*static_storage_p=*/false);
1867 else
1869 /* The default zero-initialization is fine for us; don't
1870 add anything to the CONSTRUCTOR. */
1871 skipped = 1;
1872 continue;
1876 if (is_empty_field (field)
1877 && !TREE_SIDE_EFFECTS (next))
1878 /* Don't add trivial initialization of an empty base/field to the
1879 constructor, as they might not be ordered the way the back-end
1880 expects. */
1881 continue;
1883 /* If this is a bitfield, now convert to the lowered type. */
1884 if (fldtype != TREE_TYPE (field))
1885 next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1886 picflags |= picflag_from_initializer (next);
1887 /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer CONSTRUCTOR. */
1888 if (TREE_CODE (next) == CONSTRUCTOR
1889 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next))
1891 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1892 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next) = 0;
1894 CONSTRUCTOR_APPEND_ELT (v, field, next);
1897 if (idx < CONSTRUCTOR_NELTS (init))
1899 if (complain & tf_error)
1901 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1902 /* For better diagnostics, try to find out if it is really
1903 the case of too many initializers or if designators are
1904 in incorrect order. */
1905 if (designator_skip == 1 && ce->index)
1907 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1908 || identifier_p (ce->index));
1909 for (field = TYPE_FIELDS (type);
1910 field; field = DECL_CHAIN (field))
1912 if (TREE_CODE (field) != FIELD_DECL
1913 || (DECL_ARTIFICIAL (field)
1914 && !(cxx_dialect >= cxx17
1915 && DECL_FIELD_IS_BASE (field))))
1916 continue;
1918 if (DECL_UNNAMED_BIT_FIELD (field))
1919 continue;
1921 if (ce->index == field || ce->index == DECL_NAME (field))
1922 break;
1925 if (field)
1926 error ("designator order for field %qD does not match declaration "
1927 "order in %qT", field, type);
1928 else
1929 error ("too many initializers for %qT", type);
1931 else
1932 return PICFLAG_ERRONEOUS;
1935 CONSTRUCTOR_ELTS (init) = v;
1936 return picflags;
1939 /* Subroutine of process_init_constructor, which will process a single
1940 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1941 which describe the initializer. */
1943 static int
1944 process_init_constructor_union (tree type, tree init, int nested, int flags,
1945 tsubst_flags_t complain)
1947 constructor_elt *ce;
1948 int len;
1950 /* If the initializer was empty, use the union's NSDMI if it has one.
1951 Otherwise use default zero initialization. */
1952 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1954 for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1956 if (TREE_CODE (field) == FIELD_DECL
1957 && DECL_INITIAL (field) != NULL_TREE)
1959 tree val = get_nsdmi (field, /*in_ctor=*/false, complain);
1960 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
1961 && find_placeholders (val))
1962 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1963 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init), field, val);
1964 break;
1968 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1969 return 0;
1972 len = CONSTRUCTOR_ELTS (init)->length ();
1973 if (len > 1)
1975 if (!(complain & tf_error))
1976 return PICFLAG_ERRONEOUS;
1977 error ("too many initializers for %qT", type);
1978 CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1981 ce = &(*CONSTRUCTOR_ELTS (init))[0];
1983 /* If this element specifies a field, initialize via that field. */
1984 if (ce->index)
1986 if (TREE_CODE (ce->index) == FIELD_DECL)
1988 else if (identifier_p (ce->index))
1990 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1991 tree name = ce->index;
1992 tree field;
1993 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1994 if (DECL_NAME (field) == name)
1995 break;
1996 if (!field)
1998 if (complain & tf_error)
1999 error ("no field %qD found in union being initialized",
2000 field);
2001 ce->value = error_mark_node;
2003 ce->index = field;
2005 else
2007 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
2008 || TREE_CODE (ce->index) == RANGE_EXPR);
2009 if (complain & tf_error)
2010 error ("index value instead of field name in union initializer");
2011 ce->value = error_mark_node;
2014 else
2016 /* Find the first named field. ANSI decided in September 1990
2017 that only named fields count here. */
2018 tree field = TYPE_FIELDS (type);
2019 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
2020 field = TREE_CHAIN (field);
2021 if (field == NULL_TREE)
2023 if (complain & tf_error)
2024 error ("too many initializers for %qT", type);
2025 ce->value = error_mark_node;
2027 ce->index = field;
2030 if (ce->value && ce->value != error_mark_node)
2031 ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, nested,
2032 flags, complain);
2034 /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer CONSTRUCTOR. */
2035 if (ce->value
2036 && TREE_CODE (ce->value) == CONSTRUCTOR
2037 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value))
2039 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
2040 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value) = 0;
2042 return picflag_from_initializer (ce->value);
2045 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
2046 constructor is a brace-enclosed initializer, and will be modified in-place.
2048 Each element is converted to the right type through digest_init, and
2049 missing initializers are added following the language rules (zero-padding,
2050 etc.).
2052 After the execution, the initializer will have TREE_CONSTANT if all elts are
2053 constant, and TREE_STATIC set if, in addition, all elts are simple enough
2054 constants that the assembler and linker can compute them.
2056 The function returns the initializer itself, or error_mark_node in case
2057 of error. */
2059 static tree
2060 process_init_constructor (tree type, tree init, int nested, int flags,
2061 tsubst_flags_t complain)
2063 int picflags;
2065 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
2067 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
2068 picflags = process_init_constructor_array (type, init, nested, flags,
2069 complain);
2070 else if (TREE_CODE (type) == RECORD_TYPE)
2071 picflags = process_init_constructor_record (type, init, nested, flags,
2072 complain);
2073 else if (TREE_CODE (type) == UNION_TYPE)
2074 picflags = process_init_constructor_union (type, init, nested, flags,
2075 complain);
2076 else
2077 gcc_unreachable ();
2079 if (picflags & PICFLAG_ERRONEOUS)
2080 return error_mark_node;
2082 TREE_TYPE (init) = type;
2083 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
2084 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
2085 if (picflags & PICFLAG_SIDE_EFFECTS)
2087 TREE_CONSTANT (init) = false;
2088 TREE_SIDE_EFFECTS (init) = true;
2090 else if (picflags & PICFLAG_NOT_ALL_CONSTANT)
2092 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
2093 TREE_CONSTANT (init) = false;
2094 TREE_SIDE_EFFECTS (init) = false;
2096 else
2098 TREE_CONSTANT (init) = 1;
2099 TREE_SIDE_EFFECTS (init) = false;
2100 if (!(picflags & PICFLAG_NOT_ALL_SIMPLE))
2101 TREE_STATIC (init) = 1;
2103 if (picflags & PICFLAG_VEC_INIT)
2105 /* Defer default-initialization of array elements with no corresponding
2106 initializer-clause until later so we can use a loop. */
2107 TREE_TYPE (init) = init_list_type_node;
2108 init = build_vec_init_expr (type, init, complain);
2109 init = get_target_expr (init);
2111 return init;
2114 /* Given a structure or union value DATUM, construct and return
2115 the structure or union component which results from narrowing
2116 that value to the base specified in BASETYPE. For example, given the
2117 hierarchy
2119 class L { int ii; };
2120 class A : L { ... };
2121 class B : L { ... };
2122 class C : A, B { ... };
2124 and the declaration
2126 C x;
2128 then the expression
2130 x.A::ii refers to the ii member of the L part of
2131 the A part of the C object named by X. In this case,
2132 DATUM would be x, and BASETYPE would be A.
2134 I used to think that this was nonconformant, that the standard specified
2135 that first we look up ii in A, then convert x to an L& and pull out the
2136 ii part. But in fact, it does say that we convert x to an A&; A here
2137 is known as the "naming class". (jason 2000-12-19)
2139 BINFO_P points to a variable initialized either to NULL_TREE or to the
2140 binfo for the specific base subobject we want to convert to. */
2142 tree
2143 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
2145 tree binfo;
2147 if (datum == error_mark_node)
2148 return error_mark_node;
2149 if (*binfo_p)
2150 binfo = *binfo_p;
2151 else
2152 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
2153 NULL, tf_warning_or_error);
2155 if (!binfo || binfo == error_mark_node)
2157 *binfo_p = NULL_TREE;
2158 if (!binfo)
2159 error_not_base_type (basetype, TREE_TYPE (datum));
2160 return error_mark_node;
2163 *binfo_p = binfo;
2164 return build_base_path (PLUS_EXPR, datum, binfo, 1,
2165 tf_warning_or_error);
2168 /* Build a reference to an object specified by the C++ `->' operator.
2169 Usually this just involves dereferencing the object, but if the
2170 `->' operator is overloaded, then such overloads must be
2171 performed until an object which does not have the `->' operator
2172 overloaded is found. An error is reported when circular pointer
2173 delegation is detected. */
2175 tree
2176 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
2178 tree orig_expr = expr;
2179 tree type = TREE_TYPE (expr);
2180 tree last_rval = NULL_TREE;
2181 vec<tree, va_gc> *types_memoized = NULL;
2183 if (type == error_mark_node)
2184 return error_mark_node;
2186 if (processing_template_decl)
2188 tree ttype = NULL_TREE;
2189 if (type && TYPE_PTR_P (type))
2190 ttype = TREE_TYPE (type);
2191 if (ttype && !dependent_scope_p (ttype))
2192 /* Pointer to current instantiation, don't treat as dependent. */;
2193 else if (type_dependent_expression_p (expr))
2195 expr = build_min_nt_loc (loc, ARROW_EXPR, expr);
2196 TREE_TYPE (expr) = ttype;
2197 return expr;
2201 if (MAYBE_CLASS_TYPE_P (type))
2203 struct tinst_level *actual_inst = current_instantiation ();
2204 tree fn = NULL;
2206 while ((expr = build_new_op (loc, COMPONENT_REF,
2207 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
2208 NULL_TREE, &fn, complain)))
2210 if (expr == error_mark_node)
2211 return error_mark_node;
2213 /* This provides a better instantiation backtrace in case of
2214 error. */
2215 if (fn && DECL_USE_TEMPLATE (fn))
2216 push_tinst_level_loc (fn,
2217 (current_instantiation () != actual_inst)
2218 ? DECL_SOURCE_LOCATION (fn)
2219 : input_location);
2220 fn = NULL;
2222 if (vec_member (TREE_TYPE (expr), types_memoized))
2224 if (complain & tf_error)
2225 error ("circular pointer delegation detected");
2226 return error_mark_node;
2229 vec_safe_push (types_memoized, TREE_TYPE (expr));
2230 last_rval = expr;
2233 while (current_instantiation () != actual_inst)
2234 pop_tinst_level ();
2236 if (last_rval == NULL_TREE)
2238 if (complain & tf_error)
2239 error ("base operand of %<->%> has non-pointer type %qT", type);
2240 return error_mark_node;
2243 if (TYPE_REF_P (TREE_TYPE (last_rval)))
2244 last_rval = convert_from_reference (last_rval);
2246 else
2248 last_rval = decay_conversion (expr, complain);
2249 if (last_rval == error_mark_node)
2250 return error_mark_node;
2253 if (TYPE_PTR_P (TREE_TYPE (last_rval)))
2255 if (processing_template_decl)
2257 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
2258 orig_expr);
2259 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
2260 return expr;
2263 return cp_build_indirect_ref (loc, last_rval, RO_ARROW, complain);
2266 if (complain & tf_error)
2268 if (types_memoized)
2269 error ("result of %<operator->()%> yields non-pointer result");
2270 else
2271 error ("base operand of %<->%> is not a pointer");
2273 return error_mark_node;
2276 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
2277 already been checked out to be of aggregate type. */
2279 tree
2280 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
2282 tree ptrmem_type;
2283 tree objtype;
2284 tree type;
2285 tree binfo;
2286 tree ctype;
2288 datum = mark_lvalue_use (datum);
2289 component = mark_rvalue_use (component);
2291 if (error_operand_p (datum) || error_operand_p (component))
2292 return error_mark_node;
2294 ptrmem_type = TREE_TYPE (component);
2295 if (!TYPE_PTRMEM_P (ptrmem_type))
2297 if (complain & tf_error)
2298 error ("%qE cannot be used as a member pointer, since it is of "
2299 "type %qT", component, ptrmem_type);
2300 return error_mark_node;
2303 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2304 if (! MAYBE_CLASS_TYPE_P (objtype))
2306 if (complain & tf_error)
2307 error ("cannot apply member pointer %qE to %qE, which is of "
2308 "non-class type %qT", component, datum, objtype);
2309 return error_mark_node;
2312 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
2313 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
2315 if (!COMPLETE_TYPE_P (ctype))
2317 if (!same_type_p (ctype, objtype))
2318 goto mismatch;
2319 binfo = NULL;
2321 else
2323 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
2325 if (!binfo)
2327 mismatch:
2328 if (complain & tf_error)
2329 error ("pointer to member type %qT incompatible with object "
2330 "type %qT", type, objtype);
2331 return error_mark_node;
2333 else if (binfo == error_mark_node)
2334 return error_mark_node;
2337 if (TYPE_PTRDATAMEM_P (ptrmem_type))
2339 bool is_lval = real_lvalue_p (datum);
2340 tree ptype;
2342 /* Compute the type of the field, as described in [expr.ref].
2343 There's no such thing as a mutable pointer-to-member, so
2344 things are not as complex as they are for references to
2345 non-static data members. */
2346 type = cp_build_qualified_type (type,
2347 (cp_type_quals (type)
2348 | cp_type_quals (TREE_TYPE (datum))));
2350 datum = build_address (datum);
2352 /* Convert object to the correct base. */
2353 if (binfo)
2355 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
2356 if (datum == error_mark_node)
2357 return error_mark_node;
2360 /* Build an expression for "object + offset" where offset is the
2361 value stored in the pointer-to-data-member. */
2362 ptype = build_pointer_type (type);
2363 datum = convert (ptype, datum);
2364 if (!processing_template_decl)
2365 datum = build2 (POINTER_PLUS_EXPR, ptype,
2366 datum, convert_to_ptrofftype (component));
2367 datum = cp_fully_fold (datum);
2368 datum = cp_build_fold_indirect_ref (datum);
2369 if (datum == error_mark_node)
2370 return error_mark_node;
2372 /* If the object expression was an rvalue, return an rvalue. */
2373 if (!is_lval)
2374 datum = move (datum);
2375 return datum;
2377 else
2379 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
2380 program is ill-formed if the second operand is a pointer to member
2381 function with ref-qualifier & (for C++20: unless its cv-qualifier-seq
2382 is const). In a .* expression whose object expression is an lvalue,
2383 the program is ill-formed if the second operand is a pointer to member
2384 function with ref-qualifier &&. */
2385 if (FUNCTION_REF_QUALIFIED (type))
2387 bool lval = lvalue_p (datum);
2388 if (lval && FUNCTION_RVALUE_QUALIFIED (type))
2390 if (complain & tf_error)
2391 error ("pointer-to-member-function type %qT requires an rvalue",
2392 ptrmem_type);
2393 return error_mark_node;
2395 else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
2397 if ((type_memfn_quals (type)
2398 & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))
2399 != TYPE_QUAL_CONST)
2401 if (complain & tf_error)
2402 error ("pointer-to-member-function type %qT requires "
2403 "an lvalue", ptrmem_type);
2404 return error_mark_node;
2406 else if (cxx_dialect < cxx20)
2408 if (complain & tf_warning_or_error)
2409 pedwarn (input_location, OPT_Wpedantic,
2410 "pointer-to-member-function type %qT requires "
2411 "an lvalue before C++20", ptrmem_type);
2412 else
2413 return error_mark_node;
2417 return build2 (OFFSET_REF, type, datum, component);
2421 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
2423 static tree
2424 build_functional_cast_1 (location_t loc, tree exp, tree parms,
2425 tsubst_flags_t complain)
2427 /* This is either a call to a constructor,
2428 or a C cast in C++'s `functional' notation. */
2430 /* The type to which we are casting. */
2431 tree type;
2433 if (error_operand_p (exp) || parms == error_mark_node)
2434 return error_mark_node;
2436 if (TREE_CODE (exp) == TYPE_DECL)
2438 type = TREE_TYPE (exp);
2440 if (DECL_ARTIFICIAL (exp))
2441 cp_handle_deprecated_or_unavailable (type);
2443 else
2444 type = exp;
2446 /* We need to check this explicitly, since value-initialization of
2447 arrays is allowed in other situations. */
2448 if (TREE_CODE (type) == ARRAY_TYPE)
2450 if (complain & tf_error)
2451 error_at (loc, "functional cast to array type %qT", type);
2452 return error_mark_node;
2455 if (tree anode = type_uses_auto (type))
2457 tree init;
2458 if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2459 init = parms;
2460 /* C++23 auto(x). */
2461 else if (!AUTO_IS_DECLTYPE (anode)
2462 && list_length (parms) == 1)
2464 init = TREE_VALUE (parms);
2465 if (is_constrained_auto (anode))
2467 if (complain & tf_error)
2468 error_at (loc, "%<auto(x)%> cannot be constrained");
2469 return error_mark_node;
2471 else if (cxx_dialect < cxx23)
2472 pedwarn (loc, OPT_Wc__23_extensions,
2473 "%<auto(x)%> only available with "
2474 "%<-std=c++2b%> or %<-std=gnu++2b%>");
2476 else
2478 if (complain & tf_error)
2479 error_at (loc, "invalid use of %qT", anode);
2480 return error_mark_node;
2482 type = do_auto_deduction (type, init, anode, complain,
2483 adc_variable_type);
2484 if (type == error_mark_node)
2485 return error_mark_node;
2488 if (processing_template_decl)
2490 tree t;
2492 /* Diagnose this even in a template. We could also try harder
2493 to give all the usual errors when the type and args are
2494 non-dependent... */
2495 if (TYPE_REF_P (type) && !parms)
2497 if (complain & tf_error)
2498 error_at (loc, "invalid value-initialization of reference type");
2499 return error_mark_node;
2502 t = build_min (CAST_EXPR, type, parms);
2503 /* We don't know if it will or will not have side effects. */
2504 TREE_SIDE_EFFECTS (t) = 1;
2505 return t;
2508 if (! MAYBE_CLASS_TYPE_P (type))
2510 if (parms == NULL_TREE)
2512 if (VOID_TYPE_P (type))
2513 return void_node;
2514 return build_value_init (cv_unqualified (type), complain);
2517 /* This must build a C cast. */
2518 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
2519 return cp_build_c_cast (loc, type, parms, complain);
2522 /* Prepare to evaluate as a call to a constructor. If this expression
2523 is actually used, for example,
2525 return X (arg1, arg2, ...);
2527 then the slot being initialized will be filled in. */
2529 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
2530 return error_mark_node;
2531 if (abstract_virtuals_error (ACU_CAST, type, complain))
2532 return error_mark_node;
2534 /* [expr.type.conv]
2536 If the expression list is a single-expression, the type
2537 conversion is equivalent (in definedness, and if defined in
2538 meaning) to the corresponding cast expression. */
2539 if (parms && TREE_CHAIN (parms) == NULL_TREE)
2540 return cp_build_c_cast (loc, type, TREE_VALUE (parms), complain);
2542 /* [expr.type.conv]
2544 The expression T(), where T is a simple-type-specifier for a
2545 non-array complete object type or the (possibly cv-qualified)
2546 void type, creates an rvalue of the specified type, which is
2547 value-initialized. */
2549 if (parms == NULL_TREE)
2551 exp = build_value_init (type, complain);
2552 exp = get_target_expr (exp, complain);
2553 return exp;
2556 /* Call the constructor. */
2557 releasing_vec parmvec;
2558 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
2559 vec_safe_push (parmvec, TREE_VALUE (parms));
2560 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2561 &parmvec, type, LOOKUP_NORMAL, complain);
2563 if (exp == error_mark_node)
2564 return error_mark_node;
2566 return build_cplus_new (type, exp, complain);
2569 tree
2570 build_functional_cast (location_t loc, tree exp, tree parms,
2571 tsubst_flags_t complain)
2573 tree result = build_functional_cast_1 (loc, exp, parms, complain);
2574 protected_set_expr_location (result, loc);
2575 return result;
2579 /* Add new exception specifier SPEC, to the LIST we currently have.
2580 If it's already in LIST then do nothing.
2581 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2582 know what we're doing. */
2584 tree
2585 add_exception_specifier (tree list, tree spec, tsubst_flags_t complain)
2587 bool ok;
2588 tree core = spec;
2589 bool is_ptr;
2590 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
2592 if (spec == error_mark_node)
2593 return list;
2595 gcc_assert (spec && (!list || TREE_VALUE (list)));
2597 /* [except.spec] 1, type in an exception specifier shall not be
2598 incomplete, or pointer or ref to incomplete other than pointer
2599 to cv void. */
2600 is_ptr = TYPE_PTR_P (core);
2601 if (is_ptr || TYPE_REF_P (core))
2602 core = TREE_TYPE (core);
2603 if (complain < 0)
2604 ok = true;
2605 else if (VOID_TYPE_P (core))
2606 ok = is_ptr;
2607 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
2608 ok = true;
2609 else if (processing_template_decl)
2610 ok = true;
2611 else if (!verify_type_context (input_location, TCTX_EXCEPTIONS, core,
2612 !(complain & tf_error)))
2613 return error_mark_node;
2614 else
2616 ok = true;
2617 /* 15.4/1 says that types in an exception specifier must be complete,
2618 but it seems more reasonable to only require this on definitions
2619 and calls. So just give a pedwarn at this point; we will give an
2620 error later if we hit one of those two cases. */
2621 if (!COMPLETE_TYPE_P (complete_type (core)))
2622 diag_type = DK_PEDWARN; /* pedwarn */
2625 if (ok)
2627 tree probe;
2629 for (probe = list; probe; probe = TREE_CHAIN (probe))
2630 if (same_type_p (TREE_VALUE (probe), spec))
2631 break;
2632 if (!probe)
2633 list = tree_cons (NULL_TREE, spec, list);
2635 else
2636 diag_type = DK_ERROR; /* error */
2638 if (diag_type != DK_UNSPECIFIED
2639 && (complain & tf_warning_or_error))
2640 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
2642 return list;
2645 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2647 static bool
2648 nothrow_spec_p_uninst (const_tree spec)
2650 if (DEFERRED_NOEXCEPT_SPEC_P (spec))
2651 return false;
2652 return nothrow_spec_p (spec);
2655 /* Combine the two exceptions specifier lists LIST and ADD, and return
2656 their union. */
2658 tree
2659 merge_exception_specifiers (tree list, tree add)
2661 tree noex, orig_list;
2663 if (list == error_mark_node || add == error_mark_node)
2664 return error_mark_node;
2666 /* No exception-specifier or noexcept(false) are less strict than
2667 anything else. Prefer the newer variant (LIST). */
2668 if (!list || list == noexcept_false_spec)
2669 return list;
2670 else if (!add || add == noexcept_false_spec)
2671 return add;
2673 /* noexcept(true) and throw() are stricter than anything else.
2674 As above, prefer the more recent one (LIST). */
2675 if (nothrow_spec_p_uninst (add))
2676 return list;
2678 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2679 if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
2680 && UNEVALUATED_NOEXCEPT_SPEC_P (list))
2681 return list;
2682 /* We should have instantiated other deferred noexcept specs by now. */
2683 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
2685 if (nothrow_spec_p_uninst (list))
2686 return add;
2687 noex = TREE_PURPOSE (list);
2688 gcc_checking_assert (!TREE_PURPOSE (add)
2689 || errorcount || !flag_exceptions
2690 || cp_tree_equal (noex, TREE_PURPOSE (add)));
2692 /* Combine the dynamic-exception-specifiers, if any. */
2693 orig_list = list;
2694 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
2696 tree spec = TREE_VALUE (add);
2697 tree probe;
2699 for (probe = orig_list; probe && TREE_VALUE (probe);
2700 probe = TREE_CHAIN (probe))
2701 if (same_type_p (TREE_VALUE (probe), spec))
2702 break;
2703 if (!probe)
2705 spec = build_tree_list (NULL_TREE, spec);
2706 TREE_CHAIN (spec) = list;
2707 list = spec;
2711 /* Keep the noexcept-specifier at the beginning of the list. */
2712 if (noex != TREE_PURPOSE (list))
2713 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2715 return list;
2718 /* Subroutine of build_call. Ensure that each of the types in the
2719 exception specification is complete. Technically, 15.4/1 says that
2720 they need to be complete when we see a declaration of the function,
2721 but we should be able to get away with only requiring this when the
2722 function is defined or called. See also add_exception_specifier. */
2724 void
2725 require_complete_eh_spec_types (tree fntype, tree decl)
2727 tree raises;
2728 /* Don't complain about calls to op new. */
2729 if (decl && DECL_ARTIFICIAL (decl))
2730 return;
2731 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2732 raises = TREE_CHAIN (raises))
2734 tree type = TREE_VALUE (raises);
2735 if (type && !COMPLETE_TYPE_P (type))
2737 if (decl)
2738 error
2739 ("call to function %qD which throws incomplete type %q#T",
2740 decl, type);
2741 else
2742 error ("call to function which throws incomplete type %q#T",
2743 decl);
2748 /* Record that any TARGET_EXPR in T are going to be elided in
2749 cp_gimplify_init_expr (or sooner). */
2751 void
2752 set_target_expr_eliding (tree t)
2754 if (!t)
2755 return;
2756 switch (TREE_CODE (t))
2758 case TARGET_EXPR:
2759 TARGET_EXPR_ELIDING_P (t) = true;
2760 break;
2761 case COMPOUND_EXPR:
2762 set_target_expr_eliding (TREE_OPERAND (t, 1));
2763 break;
2764 case COND_EXPR:
2765 set_target_expr_eliding (TREE_OPERAND (t, 1));
2766 set_target_expr_eliding (TREE_OPERAND (t, 2));
2767 break;
2769 default:
2770 break;
2774 /* Call the above in the process of building an INIT_EXPR. */
2776 tree
2777 cp_build_init_expr (location_t loc, tree target, tree init)
2779 set_target_expr_eliding (init);
2780 tree ie = build2_loc (loc, INIT_EXPR, TREE_TYPE (target),
2781 target, init);
2782 TREE_SIDE_EFFECTS (ie) = true;
2783 return ie;