aarch64: Use dup and zip1 for interleaving elements in vector initializer.
[official-gcc.git] / gcc / cp / typeck2.cc
blob26444720cb817b4b7a5d7d28767b457835123602
1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987-2022 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 void
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 gcc_assert (diag_kind == DK_WARNING
302 || diag_kind == DK_PEDWARN
303 || diag_kind == DK_ERROR);
305 /* Avoid duplicate error message. */
306 if (TREE_CODE (type) == ERROR_MARK)
307 return;
309 if (value)
311 STRIP_ANY_LOCATION_WRAPPER (value);
313 if (VAR_P (value)
314 || TREE_CODE (value) == PARM_DECL
315 || TREE_CODE (value) == FIELD_DECL)
317 complained = emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (value), 0,
318 "%qD has incomplete type", value);
319 is_decl = true;
322 retry:
323 /* We must print an error message. Be clever about what it says. */
325 switch (TREE_CODE (type))
327 case RECORD_TYPE:
328 case UNION_TYPE:
329 case ENUMERAL_TYPE:
330 if (!is_decl)
331 complained = emit_diagnostic (diag_kind, loc, 0,
332 "invalid use of incomplete type %q#T",
333 type);
334 if (complained)
335 cxx_incomplete_type_inform (type);
336 break;
338 case VOID_TYPE:
339 emit_diagnostic (diag_kind, loc, 0,
340 "invalid use of %qT", type);
341 break;
343 case ARRAY_TYPE:
344 if (TYPE_DOMAIN (type))
346 type = TREE_TYPE (type);
347 goto retry;
349 emit_diagnostic (diag_kind, loc, 0,
350 "invalid use of array with unspecified bounds");
351 break;
353 case OFFSET_TYPE:
354 bad_member:
356 tree member = TREE_OPERAND (value, 1);
357 if (is_overloaded_fn (member))
358 member = get_first_fn (member);
360 if (DECL_FUNCTION_MEMBER_P (member)
361 && ! flag_ms_extensions)
363 gcc_rich_location richloc (loc);
364 /* If "member" has no arguments (other than "this"), then
365 add a fix-it hint. */
366 if (type_num_arguments (TREE_TYPE (member)) == 1)
367 richloc.add_fixit_insert_after ("()");
368 emit_diagnostic (diag_kind, &richloc, 0,
369 "invalid use of member function %qD "
370 "(did you forget the %<()%> ?)", member);
372 else
373 emit_diagnostic (diag_kind, loc, 0,
374 "invalid use of member %qD "
375 "(did you forget the %<&%> ?)", member);
377 break;
379 case TEMPLATE_TYPE_PARM:
380 if (is_auto (type))
382 if (CLASS_PLACEHOLDER_TEMPLATE (type))
383 emit_diagnostic (diag_kind, loc, 0,
384 "invalid use of placeholder %qT", type);
385 else
386 emit_diagnostic (diag_kind, loc, 0,
387 "invalid use of %qT", type);
389 else
390 emit_diagnostic (diag_kind, loc, 0,
391 "invalid use of template type parameter %qT", type);
392 break;
394 case BOUND_TEMPLATE_TEMPLATE_PARM:
395 emit_diagnostic (diag_kind, loc, 0,
396 "invalid use of template template parameter %qT",
397 TYPE_NAME (type));
398 break;
400 case TYPE_PACK_EXPANSION:
401 emit_diagnostic (diag_kind, loc, 0,
402 "invalid use of pack expansion %qT", type);
403 break;
405 case TYPENAME_TYPE:
406 case DECLTYPE_TYPE:
407 emit_diagnostic (diag_kind, loc, 0,
408 "invalid use of dependent type %qT", type);
409 break;
411 case LANG_TYPE:
412 if (type == init_list_type_node)
414 emit_diagnostic (diag_kind, loc, 0,
415 "invalid use of brace-enclosed initializer list");
416 break;
418 gcc_assert (type == unknown_type_node);
419 if (value && TREE_CODE (value) == COMPONENT_REF)
420 goto bad_member;
421 else if (value && TREE_CODE (value) == ADDR_EXPR)
422 emit_diagnostic (diag_kind, loc, 0,
423 "address of overloaded function with no contextual "
424 "type information");
425 else if (value && TREE_CODE (value) == OVERLOAD)
426 emit_diagnostic (diag_kind, loc, 0,
427 "overloaded function with no contextual type information");
428 else
429 emit_diagnostic (diag_kind, loc, 0,
430 "insufficient contextual information to determine type");
431 break;
433 default:
434 gcc_unreachable ();
438 /* Print an error message for invalid use of an incomplete type.
439 VALUE is the expression that was used (or 0 if that isn't known)
440 and TYPE is the type that was invalid. */
442 void
443 cxx_incomplete_type_error (location_t loc, const_tree value, const_tree type)
445 cxx_incomplete_type_diagnostic (loc, value, type, DK_ERROR);
449 /* We've just initialized subobject SUB; also insert a TARGET_EXPR with an
450 EH-only cleanup for SUB. Because of EH region nesting issues, we need to
451 make the cleanup conditional on a flag that we will clear once the object is
452 fully initialized, so push a new flag onto FLAGS. */
454 static void
455 maybe_push_temp_cleanup (tree sub, vec<tree,va_gc> **flags)
457 if (!flag_exceptions)
458 return;
459 if (tree cleanup
460 = cxx_maybe_build_cleanup (sub, tf_warning_or_error))
462 tree tx = get_target_expr (boolean_true_node);
463 tree flag = TARGET_EXPR_SLOT (tx);
464 CLEANUP_EH_ONLY (tx) = true;
465 TARGET_EXPR_CLEANUP (tx) = build3 (COND_EXPR, void_type_node,
466 flag, cleanup, void_node);
467 add_stmt (tx);
468 vec_safe_push (*flags, flag);
472 /* The recursive part of split_nonconstant_init. DEST is an lvalue
473 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
474 Return true if the whole of the value was initialized by the
475 generated statements. */
477 static bool
478 split_nonconstant_init_1 (tree dest, tree init, bool last,
479 vec<tree,va_gc> **flags)
481 unsigned HOST_WIDE_INT idx, tidx = HOST_WIDE_INT_M1U;
482 tree field_index, value;
483 tree type = TREE_TYPE (dest);
484 tree inner_type = NULL;
485 bool array_type_p = false;
486 bool complete_p = true;
487 HOST_WIDE_INT num_split_elts = 0;
488 tree last_split_elt = NULL_TREE;
490 switch (TREE_CODE (type))
492 case ARRAY_TYPE:
493 inner_type = TREE_TYPE (type);
494 array_type_p = true;
495 if ((TREE_SIDE_EFFECTS (init)
496 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
497 || vla_type_p (type))
499 if (!TYPE_DOMAIN (type)
500 && TREE_CODE (init) == CONSTRUCTOR
501 && CONSTRUCTOR_NELTS (init))
503 /* Flexible array. */
504 cp_complete_array_type (&type, init, /*default*/true);
505 dest = build1 (VIEW_CONVERT_EXPR, type, dest);
508 /* For an array, we only need/want a single cleanup region rather
509 than one per element. build_vec_init will handle it. */
510 tree code = build_vec_init (dest, NULL_TREE, init, false, 1,
511 tf_warning_or_error, flags);
512 add_stmt (code);
513 return true;
515 /* FALLTHRU */
517 case RECORD_TYPE:
518 case UNION_TYPE:
519 case QUAL_UNION_TYPE:
520 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
521 field_index, value)
523 /* The current implementation of this algorithm assumes that
524 the field was set for all the elements. This is usually done
525 by process_init_constructor. */
526 gcc_assert (field_index);
528 if (!array_type_p)
529 inner_type = TREE_TYPE (field_index);
531 tree sub;
532 if (array_type_p)
533 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
534 NULL_TREE, NULL_TREE);
535 else
536 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
537 NULL_TREE);
539 bool elt_last = last && idx == CONSTRUCTOR_NELTS (init) - 1;
541 /* We need to see sub-array TARGET_EXPR before cp_fold_r so we can
542 handle cleanup flags properly. */
543 gcc_checking_assert (!target_expr_needs_replace (value));
545 if (TREE_CODE (value) == CONSTRUCTOR)
547 if (!split_nonconstant_init_1 (sub, value, elt_last, flags)
548 /* For flexible array member with initializer we
549 can't remove the initializer, because only the
550 initializer determines how many elements the
551 flexible array member has. */
552 || (!array_type_p
553 && TREE_CODE (inner_type) == ARRAY_TYPE
554 && TYPE_DOMAIN (inner_type) == NULL
555 && TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
556 && COMPLETE_TYPE_P (TREE_TYPE (value))
557 && !integer_zerop (TYPE_SIZE (TREE_TYPE (value)))
558 && elt_last
559 && TYPE_HAS_TRIVIAL_DESTRUCTOR
560 (strip_array_types (inner_type))))
561 complete_p = false;
562 else
564 /* Mark element for removal. */
565 last_split_elt = field_index;
566 CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE;
567 if (idx < tidx)
568 tidx = idx;
569 num_split_elts++;
572 else if (tree vi = get_vec_init_expr (value))
574 add_stmt (expand_vec_init_expr (sub, vi, tf_warning_or_error,
575 flags));
577 /* Mark element for removal. */
578 last_split_elt = field_index;
579 CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE;
580 if (idx < tidx)
581 tidx = idx;
582 num_split_elts++;
584 else if (!initializer_constant_valid_p (value, inner_type))
586 tree code;
588 /* Push cleanups for any preceding members with constant
589 initialization. */
590 if (CLASS_TYPE_P (type))
591 for (tree prev = (last_split_elt ?
592 DECL_CHAIN (last_split_elt)
593 : TYPE_FIELDS (type));
594 ; prev = DECL_CHAIN (prev))
596 prev = next_aggregate_field (prev);
597 if (prev == field_index)
598 break;
599 tree ptype = TREE_TYPE (prev);
600 if (TYPE_P (ptype) && type_build_dtor_call (ptype))
602 tree pcref = build3 (COMPONENT_REF, ptype, dest, prev,
603 NULL_TREE);
604 maybe_push_temp_cleanup (pcref, flags);
608 /* Mark element for removal. */
609 CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE;
610 if (idx < tidx)
611 tidx = idx;
613 if (TREE_CODE (field_index) == RANGE_EXPR)
615 /* Use build_vec_init to initialize a range. */
616 tree low = TREE_OPERAND (field_index, 0);
617 tree hi = TREE_OPERAND (field_index, 1);
618 sub = build4 (ARRAY_REF, inner_type, dest, low,
619 NULL_TREE, NULL_TREE);
620 sub = cp_build_addr_expr (sub, tf_warning_or_error);
621 tree max = size_binop (MINUS_EXPR, hi, low);
622 code = build_vec_init (sub, max, value, false, 0,
623 tf_warning_or_error);
624 add_stmt (code);
625 if (tree_fits_shwi_p (max))
626 num_split_elts += tree_to_shwi (max);
628 else
630 /* We may need to add a copy constructor call if
631 the field has [[no_unique_address]]. */
632 if (unsafe_return_slot_p (sub))
634 /* But not if the initializer is an implicit ctor call
635 we just built in digest_init. */
636 if (TREE_CODE (value) == TARGET_EXPR
637 && TARGET_EXPR_LIST_INIT_P (value)
638 && make_safe_copy_elision (sub, value))
639 goto build_init;
641 tree name = (DECL_FIELD_IS_BASE (field_index)
642 ? base_ctor_identifier
643 : complete_ctor_identifier);
644 releasing_vec args = make_tree_vector_single (value);
645 code = build_special_member_call
646 (sub, name, &args, inner_type,
647 LOOKUP_NORMAL, tf_warning_or_error);
649 else
651 build_init:
652 code = cp_build_init_expr (sub, value);
654 code = build_stmt (input_location, EXPR_STMT, code);
655 add_stmt (code);
656 if (!elt_last)
657 maybe_push_temp_cleanup (sub, flags);
660 last_split_elt = field_index;
661 num_split_elts++;
664 if (num_split_elts == 1)
665 CONSTRUCTOR_ELTS (init)->ordered_remove (tidx);
666 else if (num_split_elts > 1)
668 /* Perform the delayed ordered removal of non-constant elements
669 we split out. */
670 for (idx = tidx; idx < CONSTRUCTOR_NELTS (init); ++idx)
671 if (CONSTRUCTOR_ELT (init, idx)->index == NULL_TREE)
673 else
675 *CONSTRUCTOR_ELT (init, tidx) = *CONSTRUCTOR_ELT (init, idx);
676 ++tidx;
678 vec_safe_truncate (CONSTRUCTOR_ELTS (init), tidx);
680 break;
682 case VECTOR_TYPE:
683 if (!initializer_constant_valid_p (init, type))
685 tree code;
686 tree cons = copy_node (init);
687 CONSTRUCTOR_ELTS (init) = NULL;
688 code = build2 (MODIFY_EXPR, type, dest, cons);
689 code = build_stmt (input_location, EXPR_STMT, code);
690 add_stmt (code);
691 num_split_elts += CONSTRUCTOR_NELTS (init);
693 break;
695 default:
696 gcc_unreachable ();
699 /* The rest of the initializer is now a constant. */
700 TREE_CONSTANT (init) = 1;
701 TREE_SIDE_EFFECTS (init) = 0;
703 /* We didn't split out anything. */
704 if (num_split_elts == 0)
705 return false;
707 return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
708 num_split_elts, inner_type);
711 /* A subroutine of store_init_value. Splits non-constant static
712 initializer INIT into a constant part and generates code to
713 perform the non-constant part of the initialization to DEST.
714 Returns the code for the runtime init. */
716 tree
717 split_nonconstant_init (tree dest, tree init)
719 tree code;
721 if (TREE_CODE (init) == TARGET_EXPR)
722 init = TARGET_EXPR_INITIAL (init);
723 if (TREE_CODE (init) == CONSTRUCTOR)
725 /* Subobject initializers are not full-expressions. */
726 auto fe = (make_temp_override
727 (current_stmt_tree ()->stmts_are_full_exprs_p, 0));
729 init = cp_fully_fold_init (init);
730 code = push_stmt_list ();
732 /* If the complete object is an array, build_vec_init's cleanup is
733 enough. Otherwise, collect flags for disabling subobject
734 cleanups once the complete object is fully constructed. */
735 vec<tree, va_gc> *flags = nullptr;
736 if (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE)
737 flags = make_tree_vector ();
739 if (split_nonconstant_init_1 (dest, init, true, &flags))
740 init = NULL_TREE;
742 for (tree f : flags)
744 /* See maybe_push_temp_cleanup. */
745 tree d = f;
746 tree i = boolean_false_node;
747 if (TREE_CODE (f) == TREE_LIST)
749 /* To disable a build_vec_init cleanup, set
750 iterator = maxindex. */
751 d = TREE_PURPOSE (f);
752 i = TREE_VALUE (f);
753 ggc_free (f);
755 add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (d), d, i));
757 release_tree_vector (flags);
759 code = pop_stmt_list (code);
760 if (VAR_P (dest) && !is_local_temp (dest))
762 DECL_INITIAL (dest) = init;
763 TREE_READONLY (dest) = 0;
765 else if (init)
767 tree ie = cp_build_init_expr (dest, init);
768 code = add_stmt_to_compound (ie, code);
771 else if (TREE_CODE (init) == STRING_CST
772 && array_of_runtime_bound_p (TREE_TYPE (dest)))
773 code = build_vec_init (dest, NULL_TREE, init, /*value-init*/false,
774 /*from array*/1, tf_warning_or_error);
775 else
776 code = cp_build_init_expr (dest, init);
778 return code;
781 /* Perform appropriate conversions on the initial value of a variable,
782 store it in the declaration DECL,
783 and print any error messages that are appropriate.
784 If the init is invalid, store an ERROR_MARK.
786 C++: Note that INIT might be a TREE_LIST, which would mean that it is
787 a base class initializer for some aggregate type, hopefully compatible
788 with DECL. If INIT is a single element, and DECL is an aggregate
789 type, we silently convert INIT into a TREE_LIST, allowing a constructor
790 to be called.
792 If INIT is a TREE_LIST and there is no constructor, turn INIT
793 into a CONSTRUCTOR and use standard initialization techniques.
794 Perhaps a warning should be generated?
796 Returns code to be executed if initialization could not be performed
797 for static variable. In that case, caller must emit the code. */
799 tree
800 store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
802 tree value, type;
804 /* If variable's type was invalidly declared, just ignore it. */
806 type = TREE_TYPE (decl);
807 if (TREE_CODE (type) == ERROR_MARK)
808 return NULL_TREE;
810 if (MAYBE_CLASS_TYPE_P (type))
812 if (TREE_CODE (init) == TREE_LIST)
814 error ("constructor syntax used, but no constructor declared "
815 "for type %qT", type);
816 init = build_constructor_from_list (init_list_type_node, nreverse (init));
820 /* End of special C++ code. */
822 if (flags & LOOKUP_ALREADY_DIGESTED)
823 value = init;
824 else
826 if (TREE_STATIC (decl))
827 flags |= LOOKUP_ALLOW_FLEXARRAY_INIT;
828 /* Digest the specified initializer into an expression. */
829 value = digest_init_flags (type, init, flags, tf_warning_or_error);
832 /* Look for braced array initializers for character arrays and
833 recursively convert them into STRING_CSTs. */
834 value = braced_lists_to_strings (type, value);
836 current_ref_temp_count = 0;
837 value = extend_ref_init_temps (decl, value, cleanups);
839 /* In C++11 constant expression is a semantic, not syntactic, property.
840 In C++98, make sure that what we thought was a constant expression at
841 template definition time is still constant and otherwise perform this
842 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
843 if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
845 bool const_init;
846 tree oldval = value;
847 if (DECL_DECLARED_CONSTEXPR_P (decl)
848 || (DECL_IN_AGGR_P (decl)
849 && DECL_INITIALIZED_IN_CLASS_P (decl)))
851 value = fold_non_dependent_expr (value, tf_warning_or_error,
852 /*manifestly_const_eval=*/true,
853 decl);
854 /* Diagnose a non-constant initializer for constexpr variable or
855 non-inline in-class-initialized static data member. */
856 if (!require_constant_expression (value))
857 value = error_mark_node;
858 else if (processing_template_decl)
859 /* In a template we might not have done the necessary
860 transformations to make value actually constant,
861 e.g. extend_ref_init_temps. */
862 value = maybe_constant_init (value, decl, true);
863 else
864 value = cxx_constant_init (value, decl);
866 else
867 value = fold_non_dependent_init (value, tf_warning_or_error,
868 /*manifestly_const_eval=*/true, decl);
869 if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type))
870 /* Poison this CONSTRUCTOR so it can't be copied to another
871 constexpr variable. */
872 CONSTRUCTOR_MUTABLE_POISON (value) = true;
873 const_init = (reduced_constant_expression_p (value)
874 || error_operand_p (value));
875 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
876 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
877 if (!TYPE_REF_P (type))
878 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
879 if (!const_init)
881 /* [dcl.constinit]/2 "If a variable declared with the constinit
882 specifier has dynamic initialization, the program is
883 ill-formed." */
884 if (DECL_DECLARED_CONSTINIT_P (decl))
886 error_at (location_of (decl),
887 "%<constinit%> variable %qD does not have a constant "
888 "initializer", decl);
889 if (require_constant_expression (value))
890 cxx_constant_init (value, decl);
891 value = error_mark_node;
893 else
894 value = oldval;
897 /* Don't fold initializers of automatic variables in constexpr functions,
898 that might fold away something that needs to be diagnosed at constexpr
899 evaluation time. */
900 if (!current_function_decl
901 || !DECL_DECLARED_CONSTEXPR_P (current_function_decl)
902 || TREE_STATIC (decl))
903 value = cp_fully_fold_init (value);
905 /* Handle aggregate NSDMI in non-constant initializers, too. */
906 value = replace_placeholders (value, decl);
908 /* A COMPOUND_LITERAL_P CONSTRUCTOR is the syntactic form; by the time we get
909 here it should have been digested into an actual value for the type. */
910 gcc_checking_assert (TREE_CODE (value) != CONSTRUCTOR
911 || processing_template_decl
912 || TREE_CODE (type) == VECTOR_TYPE
913 || !TREE_HAS_CONSTRUCTOR (value));
915 /* If the initializer is not a constant, fill in DECL_INITIAL with
916 the bits that are constant, and then return an expression that
917 will perform the dynamic initialization. */
918 if (value != error_mark_node
919 && !processing_template_decl
920 && (TREE_SIDE_EFFECTS (value)
921 || vla_type_p (type)
922 || ! reduced_constant_expression_p (value)))
923 return split_nonconstant_init (decl, value);
925 /* DECL may change value; purge caches. */
926 clear_cv_and_fold_caches ();
928 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
929 is an automatic variable, the middle end will turn this into a
930 dynamic initialization later. */
931 DECL_INITIAL (decl) = value;
932 return NULL_TREE;
936 /* Give diagnostic about narrowing conversions within { }, or as part of
937 a converted constant expression. If CONST_ONLY, only check
938 constants. */
940 bool
941 check_narrowing (tree type, tree init, tsubst_flags_t complain,
942 bool const_only/*= false*/)
944 tree ftype = unlowered_expr_type (init);
945 bool ok = true;
946 REAL_VALUE_TYPE d;
948 if (((!warn_narrowing || !(complain & tf_warning))
949 && cxx_dialect == cxx98)
950 || !ARITHMETIC_TYPE_P (type)
951 /* Don't emit bogus warnings with e.g. value-dependent trees. */
952 || instantiation_dependent_expression_p (init))
953 return ok;
955 if (BRACE_ENCLOSED_INITIALIZER_P (init)
956 && TREE_CODE (type) == COMPLEX_TYPE)
958 tree elttype = TREE_TYPE (type);
959 if (CONSTRUCTOR_NELTS (init) > 0)
960 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value,
961 complain);
962 if (CONSTRUCTOR_NELTS (init) > 1)
963 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value,
964 complain);
965 return ok;
968 /* Even non-dependent expressions can still have template
969 codes like CAST_EXPR, so use *_non_dependent_expr to cope. */
970 init = fold_non_dependent_expr (init, complain, /*manifest*/true);
971 if (init == error_mark_node)
972 return ok;
974 /* If we were asked to only check constants, return early. */
975 if (const_only && !TREE_CONSTANT (init))
976 return ok;
978 if (CP_INTEGRAL_TYPE_P (type)
979 && TREE_CODE (ftype) == REAL_TYPE)
980 ok = false;
981 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
982 && CP_INTEGRAL_TYPE_P (type))
984 if (TREE_CODE (ftype) == ENUMERAL_TYPE)
985 /* Check for narrowing based on the values of the enumeration. */
986 ftype = ENUM_UNDERLYING_TYPE (ftype);
987 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
988 TYPE_MAX_VALUE (ftype))
989 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
990 TYPE_MIN_VALUE (type)))
991 && (TREE_CODE (init) != INTEGER_CST
992 || !int_fits_type_p (init, type)))
993 ok = false;
995 /* [dcl.init.list]#7.2: "from long double to double or float, or from
996 double to float". */
997 else if (TREE_CODE (ftype) == REAL_TYPE
998 && TREE_CODE (type) == REAL_TYPE)
1000 if ((extended_float_type_p (ftype) || extended_float_type_p (type))
1001 ? /* "from a floating-point type T to another floating-point type
1002 whose floating-point conversion rank is neither greater than
1003 nor equal to that of T".
1004 So, it is ok if
1005 cp_compare_floating_point_conversion_ranks (ftype, type)
1006 returns -2 (type has greater conversion rank than ftype)
1007 or [-1..1] (type has equal conversion rank as ftype, possibly
1008 different subrank. Only do this if at least one of the
1009 types is extended floating-point type, otherwise keep doing
1010 what we did before (for the sake of non-standard
1011 backend types). */
1012 cp_compare_floating_point_conversion_ranks (ftype, type) >= 2
1013 : ((same_type_p (ftype, long_double_type_node)
1014 && (same_type_p (type, double_type_node)
1015 || same_type_p (type, float_type_node)))
1016 || (same_type_p (ftype, double_type_node)
1017 && same_type_p (type, float_type_node))
1018 || (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))))
1020 if (TREE_CODE (init) == REAL_CST)
1022 /* Issue 703: Loss of precision is OK as long as the value is
1023 within the representable range of the new type. */
1024 REAL_VALUE_TYPE r;
1025 d = TREE_REAL_CST (init);
1026 real_convert (&r, TYPE_MODE (type), &d);
1027 if (real_isinf (&r))
1028 ok = false;
1030 else
1031 ok = false;
1034 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
1035 && TREE_CODE (type) == REAL_TYPE)
1037 ok = false;
1038 if (TREE_CODE (init) == INTEGER_CST)
1040 d = real_value_from_int_cst (0, init);
1041 if (exact_real_truncate (TYPE_MODE (type), &d))
1042 ok = true;
1045 else if (TREE_CODE (type) == BOOLEAN_TYPE
1046 && (TYPE_PTR_P (ftype) || TYPE_PTRMEM_P (ftype)))
1047 /* C++20 P1957R2: converting from a pointer type or a pointer-to-member
1048 type to bool should be considered narrowing. This is a DR so is not
1049 limited to C++20 only. */
1050 ok = false;
1052 bool almost_ok = ok;
1053 if (!ok && !CONSTANT_CLASS_P (init) && (complain & tf_warning_or_error))
1055 tree folded = cp_fully_fold (init);
1056 if (TREE_CONSTANT (folded) && check_narrowing (type, folded, tf_none))
1057 almost_ok = true;
1060 if (!ok)
1062 location_t loc = cp_expr_loc_or_input_loc (init);
1063 if (cxx_dialect == cxx98)
1065 if (complain & tf_warning)
1066 warning_at (loc, OPT_Wnarrowing, "narrowing conversion of %qE "
1067 "from %qH to %qI is ill-formed in C++11",
1068 init, ftype, type);
1069 ok = true;
1071 else if (!CONSTANT_CLASS_P (init))
1073 if (complain & tf_warning_or_error)
1075 auto_diagnostic_group d;
1076 if ((!almost_ok || pedantic)
1077 && pedwarn (loc, OPT_Wnarrowing,
1078 "narrowing conversion of %qE from %qH to %qI",
1079 init, ftype, type)
1080 && almost_ok)
1081 inform (loc, " the expression has a constant value but is not "
1082 "a C++ constant-expression");
1083 ok = true;
1086 else if (complain & tf_error)
1088 int savederrorcount = errorcount;
1089 global_dc->pedantic_errors = 1;
1090 auto s = make_temp_override (global_dc->dc_warn_system_headers, true);
1091 pedwarn (loc, OPT_Wnarrowing,
1092 "narrowing conversion of %qE from %qH to %qI",
1093 init, ftype, type);
1094 if (errorcount == savederrorcount)
1095 ok = true;
1096 global_dc->pedantic_errors = flag_pedantic_errors;
1100 return ok;
1103 /* True iff TYPE is a C++20 "ordinary" character type. */
1105 bool
1106 ordinary_char_type_p (tree type)
1108 type = TYPE_MAIN_VARIANT (type);
1109 return (type == char_type_node
1110 || type == signed_char_type_node
1111 || type == unsigned_char_type_node);
1114 /* True iff the string literal INIT has a type suitable for initializing array
1115 TYPE. */
1117 bool
1118 array_string_literal_compatible_p (tree type, tree init)
1120 tree to_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1121 tree from_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
1123 if (to_char_type == from_char_type)
1124 return true;
1125 /* The array element type does not match the initializing string
1126 literal element type; this is only allowed when both types are
1127 ordinary character type. There are no string literals of
1128 signed or unsigned char type in the language, but we can get
1129 them internally from converting braced-init-lists to
1130 STRING_CST. */
1131 if (ordinary_char_type_p (to_char_type)
1132 && ordinary_char_type_p (from_char_type))
1133 return true;
1135 /* P2513 (C++20/C++23): "an array of char or unsigned char may
1136 be initialized by a UTF-8 string literal, or by such a string
1137 literal enclosed in braces." */
1138 if (from_char_type == char8_type_node
1139 && (to_char_type == char_type_node
1140 || to_char_type == unsigned_char_type_node))
1141 return true;
1143 return false;
1146 /* Process the initializer INIT for a variable of type TYPE, emitting
1147 diagnostics for invalid initializers and converting the initializer as
1148 appropriate.
1150 For aggregate types, it assumes that reshape_init has already run, thus the
1151 initializer will have the right shape (brace elision has been undone).
1153 NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR,
1154 2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR. */
1156 static tree
1157 digest_init_r (tree type, tree init, int nested, int flags,
1158 tsubst_flags_t complain)
1160 enum tree_code code = TREE_CODE (type);
1162 if (error_operand_p (init))
1163 return error_mark_node;
1165 gcc_assert (init);
1167 /* We must strip the outermost array type when completing the type,
1168 because the its bounds might be incomplete at the moment. */
1169 if (!complete_type_or_maybe_complain (code == ARRAY_TYPE
1170 ? TREE_TYPE (type) : type, NULL_TREE,
1171 complain))
1172 return error_mark_node;
1174 location_t loc = cp_expr_loc_or_input_loc (init);
1176 tree stripped_init = init;
1178 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1179 && CONSTRUCTOR_IS_PAREN_INIT (init))
1180 flags |= LOOKUP_AGGREGATE_PAREN_INIT;
1182 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
1183 (g++.old-deja/g++.law/casts2.C). */
1184 if (TREE_CODE (init) == NON_LVALUE_EXPR)
1185 stripped_init = TREE_OPERAND (init, 0);
1187 stripped_init = tree_strip_any_location_wrapper (stripped_init);
1189 /* Initialization of an array of chars from a string constant. The initializer
1190 can be optionally enclosed in braces, but reshape_init has already removed
1191 them if they were present. */
1192 if (code == ARRAY_TYPE)
1194 if (nested && !TYPE_DOMAIN (type))
1195 /* C++ flexible array members have a null domain. */
1197 if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT)
1198 pedwarn (loc, OPT_Wpedantic,
1199 "initialization of a flexible array member");
1200 else
1202 if (complain & tf_error)
1203 error_at (loc, "non-static initialization of"
1204 " a flexible array member");
1205 return error_mark_node;
1209 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1210 if (char_type_p (typ1)
1211 && TREE_CODE (stripped_init) == STRING_CST)
1213 if (!array_string_literal_compatible_p (type, init))
1215 if (complain & tf_error)
1216 error_at (loc, "cannot initialize array of %qT from "
1217 "a string literal with type array of %qT",
1218 typ1,
1219 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init))));
1220 return error_mark_node;
1223 if (nested == 2 && !TYPE_DOMAIN (type))
1225 if (complain & tf_error)
1226 error_at (loc, "initialization of flexible array member "
1227 "in a nested context");
1228 return error_mark_node;
1231 if (type != TREE_TYPE (init)
1232 && !variably_modified_type_p (type, NULL_TREE))
1234 init = copy_node (init);
1235 TREE_TYPE (init) = type;
1236 /* If we have a location wrapper, then also copy the wrapped
1237 node, and update the copy's type. */
1238 if (location_wrapper_p (init))
1240 stripped_init = copy_node (stripped_init);
1241 TREE_OPERAND (init, 0) = stripped_init;
1242 TREE_TYPE (stripped_init) = type;
1245 if (TYPE_DOMAIN (type) && TREE_CONSTANT (TYPE_SIZE (type)))
1247 /* Not a flexible array member. */
1248 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1249 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1250 /* In C it is ok to subtract 1 from the length of the string
1251 because it's ok to ignore the terminating null char that is
1252 counted in the length of the constant, but in C++ this would
1253 be invalid. */
1254 if (size < TREE_STRING_LENGTH (stripped_init))
1256 permerror (loc, "initializer-string for %qT is too long",
1257 type);
1259 init = build_string (size,
1260 TREE_STRING_POINTER (stripped_init));
1261 TREE_TYPE (init) = type;
1264 return init;
1268 /* Handle scalar types (including conversions) and references. */
1269 if ((code != COMPLEX_TYPE || BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
1270 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1272 /* Narrowing is OK when initializing an aggregate from
1273 a parenthesized list. */
1274 if (nested && !(flags & LOOKUP_AGGREGATE_PAREN_INIT))
1275 flags |= LOOKUP_NO_NARROWING;
1276 init = convert_for_initialization (0, type, init, flags,
1277 ICR_INIT, NULL_TREE, 0,
1278 complain);
1280 return init;
1283 /* Come here only for aggregates: records, arrays, unions, complex numbers
1284 and vectors. */
1285 gcc_assert (code == ARRAY_TYPE
1286 || VECTOR_TYPE_P (type)
1287 || code == RECORD_TYPE
1288 || code == UNION_TYPE
1289 || code == OPAQUE_TYPE
1290 || code == COMPLEX_TYPE);
1292 /* "If T is a class type and the initializer list has a single
1293 element of type cv U, where U is T or a class derived from T,
1294 the object is initialized from that element." */
1295 if (cxx_dialect >= cxx11
1296 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
1297 && !CONSTRUCTOR_IS_DESIGNATED_INIT (stripped_init)
1298 && CONSTRUCTOR_NELTS (stripped_init) == 1
1299 && ((CLASS_TYPE_P (type) && !CLASSTYPE_NON_AGGREGATE (type))
1300 || VECTOR_TYPE_P (type)))
1302 tree elt = CONSTRUCTOR_ELT (stripped_init, 0)->value;
1303 if (reference_related_p (type, TREE_TYPE (elt)))
1305 /* In C++17, aggregates can have bases, thus participate in
1306 aggregate initialization. In the following case:
1308 struct B { int c; };
1309 struct D : B { };
1310 D d{{D{{42}}}};
1312 there's an extra set of braces, so the D temporary initializes
1313 the first element of d, which is the B base subobject. The base
1314 of type B is copy-initialized from the D temporary, causing
1315 object slicing. */
1316 tree field = next_aggregate_field (TYPE_FIELDS (type));
1317 if (field && DECL_FIELD_IS_BASE (field))
1319 if (warning_at (loc, 0, "initializing a base class of type %qT "
1320 "results in object slicing", TREE_TYPE (field)))
1321 inform (loc, "remove %<{ }%> around initializer");
1323 else if (flag_checking)
1324 /* We should have fixed this in reshape_init. */
1325 gcc_unreachable ();
1329 if (SIMPLE_TARGET_EXPR_P (stripped_init))
1330 stripped_init = TARGET_EXPR_INITIAL (stripped_init);
1332 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
1333 && !TYPE_NON_AGGREGATE_CLASS (type))
1334 return process_init_constructor (type, stripped_init, nested, flags,
1335 complain);
1336 else
1338 if (COMPOUND_LITERAL_P (stripped_init) && code == ARRAY_TYPE)
1340 if (complain & tf_error)
1341 error_at (loc, "cannot initialize aggregate of type %qT with "
1342 "a compound literal", type);
1344 return error_mark_node;
1347 if (code == ARRAY_TYPE
1348 && !BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
1350 /* Allow the result of build_array_copy and of
1351 build_value_init_noctor. */
1352 if ((TREE_CODE (stripped_init) == VEC_INIT_EXPR
1353 || TREE_CODE (stripped_init) == CONSTRUCTOR)
1354 && (same_type_ignoring_top_level_qualifiers_p
1355 (type, TREE_TYPE (init))))
1356 return init;
1358 if (complain & tf_error)
1359 error_at (loc, "array must be initialized with a brace-enclosed"
1360 " initializer");
1361 return error_mark_node;
1364 return convert_for_initialization (NULL_TREE, type, init,
1365 flags,
1366 ICR_INIT, NULL_TREE, 0,
1367 complain);
1371 tree
1372 digest_init (tree type, tree init, tsubst_flags_t complain)
1374 return digest_init_r (type, init, 0, LOOKUP_IMPLICIT, complain);
1377 tree
1378 digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain)
1380 return digest_init_r (type, init, 0, flags, complain);
1383 /* Return true if SUBOB initializes the same object as FULL_EXPR.
1384 For instance:
1386 A a = A{}; // initializer
1387 A a = (A{}); // initializer
1388 A a = (1, A{}); // initializer
1389 A a = true ? A{} : A{}; // initializer
1390 auto x = A{}.x; // temporary materialization
1391 auto x = foo(A{}); // temporary materialization
1393 FULL_EXPR is the whole expression, SUBOB is its TARGET_EXPR subobject. */
1395 static bool
1396 potential_prvalue_result_of (tree subob, tree full_expr)
1398 if (subob == full_expr)
1399 return true;
1400 else if (TREE_CODE (full_expr) == TARGET_EXPR)
1402 tree init = TARGET_EXPR_INITIAL (full_expr);
1403 if (TREE_CODE (init) == COND_EXPR)
1404 return (potential_prvalue_result_of (subob, TREE_OPERAND (init, 1))
1405 || potential_prvalue_result_of (subob, TREE_OPERAND (init, 2)));
1406 else if (TREE_CODE (init) == COMPOUND_EXPR)
1407 return potential_prvalue_result_of (subob, TREE_OPERAND (init, 1));
1408 /* ??? I don't know if this can be hit. */
1409 else if (TREE_CODE (init) == PAREN_EXPR)
1411 gcc_checking_assert (false);
1412 return potential_prvalue_result_of (subob, TREE_OPERAND (init, 0));
1415 return false;
1418 /* Callback to replace PLACEHOLDER_EXPRs in a TARGET_EXPR (which isn't used
1419 in the context of guaranteed copy elision). */
1421 static tree
1422 replace_placeholders_for_class_temp_r (tree *tp, int *, void *data)
1424 tree t = *tp;
1425 tree full_expr = *static_cast<tree *>(data);
1427 /* We're looking for a TARGET_EXPR nested in the whole expression. */
1428 if (TREE_CODE (t) == TARGET_EXPR
1429 && !potential_prvalue_result_of (t, full_expr))
1431 tree init = TARGET_EXPR_INITIAL (t);
1432 while (TREE_CODE (init) == COMPOUND_EXPR)
1433 init = TREE_OPERAND (init, 1);
1434 if (TREE_CODE (init) == CONSTRUCTOR
1435 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init))
1437 tree obj = TARGET_EXPR_SLOT (t);
1438 replace_placeholders (init, obj);
1439 /* We should have dealt with all PLACEHOLDER_EXPRs. */
1440 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = false;
1441 gcc_checking_assert (!find_placeholders (init));
1445 return NULL_TREE;
1448 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1449 tree
1450 digest_nsdmi_init (tree decl, tree init, tsubst_flags_t complain)
1452 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1454 tree type = TREE_TYPE (decl);
1455 if (DECL_BIT_FIELD_TYPE (decl))
1456 type = DECL_BIT_FIELD_TYPE (decl);
1457 int flags = LOOKUP_IMPLICIT;
1458 if (DIRECT_LIST_INIT_P (init))
1460 flags = LOOKUP_NORMAL;
1461 complain |= tf_no_cleanup;
1463 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1464 && CP_AGGREGATE_TYPE_P (type))
1465 init = reshape_init (type, init, complain);
1466 init = digest_init_flags (type, init, flags, complain);
1467 set_target_expr_eliding (init);
1469 /* We may have temporary materialization in a NSDMI, if the initializer
1470 has something like A{} in it. Digesting the {} could have introduced
1471 a PLACEHOLDER_EXPR referring to A. Now that we've got a TARGET_EXPR,
1472 we have an object we can refer to. The reason we bother doing this
1473 here is for code like
1475 struct A {
1476 int x;
1477 int y = x;
1480 struct B {
1481 int x = 0;
1482 int y = A{x}.y; // #1
1485 where in #1 we don't want to end up with two PLACEHOLDER_EXPRs for
1486 different types on the same level in a {} when lookup_placeholder
1487 wouldn't find a named object for the PLACEHOLDER_EXPR for A. Note,
1488 temporary materialization does not occur when initializing an object
1489 from a prvalue of the same type, therefore we must not replace the
1490 placeholder with a temporary object so that it can be elided. */
1491 cp_walk_tree (&init, replace_placeholders_for_class_temp_r, &init,
1492 nullptr);
1494 return init;
1497 /* Set of flags used within process_init_constructor to describe the
1498 initializers. */
1499 #define PICFLAG_ERRONEOUS 1
1500 #define PICFLAG_NOT_ALL_CONSTANT 2
1501 #define PICFLAG_NOT_ALL_SIMPLE 4
1502 #define PICFLAG_SIDE_EFFECTS 8
1503 #define PICFLAG_VEC_INIT 16
1505 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1506 describe it. */
1508 static int
1509 picflag_from_initializer (tree init)
1511 if (init == error_mark_node)
1512 return PICFLAG_ERRONEOUS;
1513 else if (!TREE_CONSTANT (init))
1515 if (TREE_SIDE_EFFECTS (init))
1516 return PICFLAG_SIDE_EFFECTS;
1517 else
1518 return PICFLAG_NOT_ALL_CONSTANT;
1520 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1521 return PICFLAG_NOT_ALL_SIMPLE;
1522 return 0;
1525 /* Adjust INIT for going into a CONSTRUCTOR. */
1527 static tree
1528 massage_init_elt (tree type, tree init, int nested, int flags,
1529 tsubst_flags_t complain)
1531 int new_flags = LOOKUP_IMPLICIT;
1532 if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT)
1533 new_flags |= LOOKUP_ALLOW_FLEXARRAY_INIT;
1534 if (flags & LOOKUP_AGGREGATE_PAREN_INIT)
1535 new_flags |= LOOKUP_AGGREGATE_PAREN_INIT;
1536 init = digest_init_r (type, init, nested ? 2 : 1, new_flags, complain);
1537 /* When we defer constant folding within a statement, we may want to
1538 defer this folding as well. Don't call this on CONSTRUCTORs because
1539 their elements have already been folded, and we must avoid folding
1540 the result of get_nsdmi. */
1541 if (TREE_CODE (init) != CONSTRUCTOR)
1543 tree t = fold_non_dependent_init (init, complain);
1544 if (TREE_CONSTANT (t))
1545 init = t;
1546 set_target_expr_eliding (init);
1548 return init;
1551 /* Subroutine of process_init_constructor, which will process an initializer
1552 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1553 which describe the initializers. */
1555 static int
1556 process_init_constructor_array (tree type, tree init, int nested, int flags,
1557 tsubst_flags_t complain)
1559 unsigned HOST_WIDE_INT i, len = 0;
1560 int picflags = 0;
1561 bool unbounded = false;
1562 constructor_elt *ce;
1563 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1565 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1566 || VECTOR_TYPE_P (type));
1568 if (TREE_CODE (type) == ARRAY_TYPE)
1570 /* C++ flexible array members have a null domain. */
1571 tree domain = TYPE_DOMAIN (type);
1572 if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1573 len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
1574 - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
1575 TYPE_PRECISION (TREE_TYPE (domain)),
1576 TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
1577 else
1578 unbounded = true; /* Take as many as there are. */
1580 if (nested == 2 && !domain && !vec_safe_is_empty (v))
1582 if (complain & tf_error)
1583 error_at (cp_expr_loc_or_input_loc (init),
1584 "initialization of flexible array member "
1585 "in a nested context");
1586 return PICFLAG_ERRONEOUS;
1589 else
1590 /* Vectors are like simple fixed-size arrays. */
1591 unbounded = !TYPE_VECTOR_SUBPARTS (type).is_constant (&len);
1593 /* There must not be more initializers than needed. */
1594 if (!unbounded && vec_safe_length (v) > len)
1596 if (complain & tf_error)
1597 error ("too many initializers for %qT", type);
1598 else
1599 return PICFLAG_ERRONEOUS;
1602 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1604 if (!ce->index)
1605 ce->index = size_int (i);
1606 else if (!check_array_designated_initializer (ce, i))
1607 ce->index = error_mark_node;
1608 gcc_assert (ce->value);
1609 ce->value
1610 = massage_init_elt (TREE_TYPE (type), ce->value, nested, flags,
1611 complain);
1613 gcc_checking_assert
1614 (ce->value == error_mark_node
1615 || (same_type_ignoring_top_level_qualifiers_p
1616 (strip_array_types (TREE_TYPE (type)),
1617 strip_array_types (TREE_TYPE (ce->value)))));
1619 picflags |= picflag_from_initializer (ce->value);
1620 /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer
1621 CONSTRUCTOR. */
1622 if (TREE_CODE (ce->value) == CONSTRUCTOR
1623 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value))
1625 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1626 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value) = 0;
1630 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1631 we must add initializers ourselves. */
1632 if (!unbounded)
1633 for (; i < len; ++i)
1635 tree next;
1637 if (type_build_ctor_call (TREE_TYPE (type)))
1639 /* If this type needs constructors run for default-initialization,
1640 we can't rely on the back end to do it for us, so make the
1641 initialization explicit by list-initializing from T{}. */
1642 next = build_constructor (init_list_type_node, NULL);
1643 next = massage_init_elt (TREE_TYPE (type), next, nested, flags,
1644 complain);
1645 if (initializer_zerop (next))
1646 /* The default zero-initialization is fine for us; don't
1647 add anything to the CONSTRUCTOR. */
1648 next = NULL_TREE;
1650 else if (!zero_init_p (TREE_TYPE (type)))
1651 next = build_zero_init (TREE_TYPE (type),
1652 /*nelts=*/NULL_TREE,
1653 /*static_storage_p=*/false);
1654 else
1655 /* The default zero-initialization is fine for us; don't
1656 add anything to the CONSTRUCTOR. */
1657 next = NULL_TREE;
1659 if (next)
1661 if (next != error_mark_node
1662 && ! seen_error () // Improves error-recovery on anew5.C.
1663 && (initializer_constant_valid_p (next, TREE_TYPE (next))
1664 != null_pointer_node))
1666 /* Use VEC_INIT_EXPR for non-constant initialization of
1667 trailing elements with no explicit initializers. */
1668 picflags |= PICFLAG_VEC_INIT;
1669 break;
1672 picflags |= picflag_from_initializer (next);
1673 /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer
1674 CONSTRUCTOR. */
1675 if (TREE_CODE (next) == CONSTRUCTOR
1676 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next))
1678 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1679 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next) = 0;
1681 if (len > i+1)
1683 tree range = build2 (RANGE_EXPR, size_type_node,
1684 build_int_cst (size_type_node, i),
1685 build_int_cst (size_type_node, len - 1));
1686 CONSTRUCTOR_APPEND_ELT (v, range, next);
1687 break;
1689 else
1690 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1692 else
1693 /* Don't bother checking all the other elements. */
1694 break;
1697 CONSTRUCTOR_ELTS (init) = v;
1698 return picflags;
1701 /* Subroutine of process_init_constructor, which will process an initializer
1702 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1703 the initializers. */
1705 static int
1706 process_init_constructor_record (tree type, tree init, int nested, int flags,
1707 tsubst_flags_t complain)
1709 vec<constructor_elt, va_gc> *v = NULL;
1710 tree field;
1711 int skipped = 0;
1713 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1714 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1715 gcc_assert (!TYPE_BINFO (type)
1716 || cxx_dialect >= cxx17
1717 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1718 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1720 restart:
1721 int picflags = 0;
1722 unsigned HOST_WIDE_INT idx = 0;
1723 int designator_skip = -1;
1724 /* Generally, we will always have an index for each initializer (which is
1725 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1726 reshape_init. So we need to handle both cases. */
1727 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1729 tree next;
1731 if (TREE_CODE (field) != FIELD_DECL
1732 || (DECL_ARTIFICIAL (field)
1733 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))))
1734 continue;
1736 if (DECL_UNNAMED_BIT_FIELD (field))
1737 continue;
1739 /* If this is a bitfield, first convert to the declared type. */
1740 tree fldtype = TREE_TYPE (field);
1741 if (DECL_BIT_FIELD_TYPE (field))
1742 fldtype = DECL_BIT_FIELD_TYPE (field);
1743 if (fldtype == error_mark_node)
1744 return PICFLAG_ERRONEOUS;
1746 next = NULL_TREE;
1747 if (idx < CONSTRUCTOR_NELTS (init))
1749 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1750 if (ce->index)
1752 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1753 latter case can happen in templates where lookup has to be
1754 deferred. */
1755 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1756 || identifier_p (ce->index));
1757 if (ce->index == field || ce->index == DECL_NAME (field))
1758 next = ce->value;
1759 else
1761 ce = NULL;
1762 if (designator_skip == -1)
1763 designator_skip = 1;
1766 else
1768 designator_skip = 0;
1769 next = ce->value;
1772 if (ce)
1774 gcc_assert (ce->value);
1775 next = massage_init_elt (fldtype, next, nested, flags, complain);
1776 /* We can't actually elide the temporary when initializing a
1777 potentially-overlapping field from a function that returns by
1778 value. */
1779 if (ce->index
1780 && TREE_CODE (next) == TARGET_EXPR
1781 && unsafe_copy_elision_p (ce->index, next))
1782 TARGET_EXPR_ELIDING_P (next) = false;
1783 ++idx;
1786 if (next == error_mark_node)
1787 /* We skip initializers for empty bases/fields, so skipping an invalid
1788 one could make us accept invalid code. */
1789 return PICFLAG_ERRONEOUS;
1790 else if (next)
1791 /* Already handled above. */;
1792 else if (DECL_INITIAL (field))
1794 if (skipped > 0)
1796 /* We're using an NSDMI past a field with implicit
1797 zero-init. Go back and make it explicit. */
1798 skipped = -1;
1799 vec_safe_truncate (v, 0);
1800 goto restart;
1802 /* C++14 aggregate NSDMI. */
1803 next = get_nsdmi (field, /*ctor*/false, complain);
1804 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
1805 && find_placeholders (next))
1806 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1808 else if (type_build_ctor_call (fldtype))
1810 /* If this type needs constructors run for
1811 default-initialization, we can't rely on the back end to do it
1812 for us, so build up TARGET_EXPRs. If the type in question is
1813 a class, just build one up; if it's an array, recurse. */
1814 next = build_constructor (init_list_type_node, NULL);
1815 next = massage_init_elt (fldtype, next, nested, flags, complain);
1816 if (TREE_CODE (next) == TARGET_EXPR
1817 && unsafe_copy_elision_p (field, next))
1818 TARGET_EXPR_ELIDING_P (next) = false;
1820 /* Warn when some struct elements are implicitly initialized. */
1821 if ((complain & tf_warning)
1822 && !cp_unevaluated_operand
1823 && !EMPTY_CONSTRUCTOR_P (init))
1824 warning (OPT_Wmissing_field_initializers,
1825 "missing initializer for member %qD", field);
1827 else
1829 if (TYPE_REF_P (fldtype))
1831 if (complain & tf_error)
1832 error ("member %qD is uninitialized reference", field);
1833 else
1834 return PICFLAG_ERRONEOUS;
1836 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype))
1838 if (complain & tf_error)
1839 error ("member %qD with uninitialized reference fields", field);
1840 else
1841 return PICFLAG_ERRONEOUS;
1843 /* Do nothing for flexible array members since they need not have any
1844 elements. Don't worry about 'skipped' because a flexarray has to
1845 be the last field. */
1846 else if (TREE_CODE (fldtype) == ARRAY_TYPE && !TYPE_DOMAIN (fldtype))
1847 continue;
1849 /* Warn when some struct elements are implicitly initialized
1850 to zero. */
1851 if ((complain & tf_warning)
1852 && !cp_unevaluated_operand
1853 && !EMPTY_CONSTRUCTOR_P (init))
1854 warning (OPT_Wmissing_field_initializers,
1855 "missing initializer for member %qD", field);
1857 if (!zero_init_p (fldtype) || skipped < 0)
1859 if (TYPE_REF_P (fldtype))
1860 next = build_zero_cst (fldtype);
1861 else
1862 next = build_zero_init (fldtype, /*nelts=*/NULL_TREE,
1863 /*static_storage_p=*/false);
1865 else
1867 /* The default zero-initialization is fine for us; don't
1868 add anything to the CONSTRUCTOR. */
1869 skipped = 1;
1870 continue;
1874 if (is_empty_field (field)
1875 && !TREE_SIDE_EFFECTS (next))
1876 /* Don't add trivial initialization of an empty base/field to the
1877 constructor, as they might not be ordered the way the back-end
1878 expects. */
1879 continue;
1881 /* If this is a bitfield, now convert to the lowered type. */
1882 if (fldtype != TREE_TYPE (field))
1883 next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1884 picflags |= picflag_from_initializer (next);
1885 /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer CONSTRUCTOR. */
1886 if (TREE_CODE (next) == CONSTRUCTOR
1887 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next))
1889 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1890 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next) = 0;
1892 CONSTRUCTOR_APPEND_ELT (v, field, next);
1895 if (idx < CONSTRUCTOR_NELTS (init))
1897 if (complain & tf_error)
1899 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1900 /* For better diagnostics, try to find out if it is really
1901 the case of too many initializers or if designators are
1902 in incorrect order. */
1903 if (designator_skip == 1 && ce->index)
1905 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1906 || identifier_p (ce->index));
1907 for (field = TYPE_FIELDS (type);
1908 field; field = DECL_CHAIN (field))
1910 if (TREE_CODE (field) != FIELD_DECL
1911 || (DECL_ARTIFICIAL (field)
1912 && !(cxx_dialect >= cxx17
1913 && DECL_FIELD_IS_BASE (field))))
1914 continue;
1916 if (DECL_UNNAMED_BIT_FIELD (field))
1917 continue;
1919 if (ce->index == field || ce->index == DECL_NAME (field))
1920 break;
1923 if (field)
1924 error ("designator order for field %qD does not match declaration "
1925 "order in %qT", field, type);
1926 else
1927 error ("too many initializers for %qT", type);
1929 else
1930 return PICFLAG_ERRONEOUS;
1933 CONSTRUCTOR_ELTS (init) = v;
1934 return picflags;
1937 /* Subroutine of process_init_constructor, which will process a single
1938 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1939 which describe the initializer. */
1941 static int
1942 process_init_constructor_union (tree type, tree init, int nested, int flags,
1943 tsubst_flags_t complain)
1945 constructor_elt *ce;
1946 int len;
1948 /* If the initializer was empty, use the union's NSDMI if it has one.
1949 Otherwise use default zero initialization. */
1950 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1952 for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1954 if (TREE_CODE (field) == FIELD_DECL
1955 && DECL_INITIAL (field) != NULL_TREE)
1957 tree val = get_nsdmi (field, /*in_ctor=*/false, complain);
1958 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
1959 && find_placeholders (val))
1960 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1961 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init), field, val);
1962 break;
1966 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1967 return 0;
1970 len = CONSTRUCTOR_ELTS (init)->length ();
1971 if (len > 1)
1973 if (!(complain & tf_error))
1974 return PICFLAG_ERRONEOUS;
1975 error ("too many initializers for %qT", type);
1976 CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1979 ce = &(*CONSTRUCTOR_ELTS (init))[0];
1981 /* If this element specifies a field, initialize via that field. */
1982 if (ce->index)
1984 if (TREE_CODE (ce->index) == FIELD_DECL)
1986 else if (identifier_p (ce->index))
1988 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1989 tree name = ce->index;
1990 tree field;
1991 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1992 if (DECL_NAME (field) == name)
1993 break;
1994 if (!field)
1996 if (complain & tf_error)
1997 error ("no field %qD found in union being initialized",
1998 field);
1999 ce->value = error_mark_node;
2001 ce->index = field;
2003 else
2005 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
2006 || TREE_CODE (ce->index) == RANGE_EXPR);
2007 if (complain & tf_error)
2008 error ("index value instead of field name in union initializer");
2009 ce->value = error_mark_node;
2012 else
2014 /* Find the first named field. ANSI decided in September 1990
2015 that only named fields count here. */
2016 tree field = TYPE_FIELDS (type);
2017 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
2018 field = TREE_CHAIN (field);
2019 if (field == NULL_TREE)
2021 if (complain & tf_error)
2022 error ("too many initializers for %qT", type);
2023 ce->value = error_mark_node;
2025 ce->index = field;
2028 if (ce->value && ce->value != error_mark_node)
2029 ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, nested,
2030 flags, complain);
2032 /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer CONSTRUCTOR. */
2033 if (ce->value
2034 && TREE_CODE (ce->value) == CONSTRUCTOR
2035 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value))
2037 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
2038 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value) = 0;
2040 return picflag_from_initializer (ce->value);
2043 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
2044 constructor is a brace-enclosed initializer, and will be modified in-place.
2046 Each element is converted to the right type through digest_init, and
2047 missing initializers are added following the language rules (zero-padding,
2048 etc.).
2050 After the execution, the initializer will have TREE_CONSTANT if all elts are
2051 constant, and TREE_STATIC set if, in addition, all elts are simple enough
2052 constants that the assembler and linker can compute them.
2054 The function returns the initializer itself, or error_mark_node in case
2055 of error. */
2057 static tree
2058 process_init_constructor (tree type, tree init, int nested, int flags,
2059 tsubst_flags_t complain)
2061 int picflags;
2063 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
2065 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
2066 picflags = process_init_constructor_array (type, init, nested, flags,
2067 complain);
2068 else if (TREE_CODE (type) == RECORD_TYPE)
2069 picflags = process_init_constructor_record (type, init, nested, flags,
2070 complain);
2071 else if (TREE_CODE (type) == UNION_TYPE)
2072 picflags = process_init_constructor_union (type, init, nested, flags,
2073 complain);
2074 else
2075 gcc_unreachable ();
2077 if (picflags & PICFLAG_ERRONEOUS)
2078 return error_mark_node;
2080 TREE_TYPE (init) = type;
2081 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
2082 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
2083 if (picflags & PICFLAG_SIDE_EFFECTS)
2085 TREE_CONSTANT (init) = false;
2086 TREE_SIDE_EFFECTS (init) = true;
2088 else if (picflags & PICFLAG_NOT_ALL_CONSTANT)
2090 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
2091 TREE_CONSTANT (init) = false;
2092 TREE_SIDE_EFFECTS (init) = false;
2094 else
2096 TREE_CONSTANT (init) = 1;
2097 TREE_SIDE_EFFECTS (init) = false;
2098 if (!(picflags & PICFLAG_NOT_ALL_SIMPLE))
2099 TREE_STATIC (init) = 1;
2101 if (picflags & PICFLAG_VEC_INIT)
2103 /* Defer default-initialization of array elements with no corresponding
2104 initializer-clause until later so we can use a loop. */
2105 TREE_TYPE (init) = init_list_type_node;
2106 init = build_vec_init_expr (type, init, complain);
2107 init = get_target_expr (init);
2109 return init;
2112 /* Given a structure or union value DATUM, construct and return
2113 the structure or union component which results from narrowing
2114 that value to the base specified in BASETYPE. For example, given the
2115 hierarchy
2117 class L { int ii; };
2118 class A : L { ... };
2119 class B : L { ... };
2120 class C : A, B { ... };
2122 and the declaration
2124 C x;
2126 then the expression
2128 x.A::ii refers to the ii member of the L part of
2129 the A part of the C object named by X. In this case,
2130 DATUM would be x, and BASETYPE would be A.
2132 I used to think that this was nonconformant, that the standard specified
2133 that first we look up ii in A, then convert x to an L& and pull out the
2134 ii part. But in fact, it does say that we convert x to an A&; A here
2135 is known as the "naming class". (jason 2000-12-19)
2137 BINFO_P points to a variable initialized either to NULL_TREE or to the
2138 binfo for the specific base subobject we want to convert to. */
2140 tree
2141 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
2143 tree binfo;
2145 if (datum == error_mark_node)
2146 return error_mark_node;
2147 if (*binfo_p)
2148 binfo = *binfo_p;
2149 else
2150 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
2151 NULL, tf_warning_or_error);
2153 if (!binfo || binfo == error_mark_node)
2155 *binfo_p = NULL_TREE;
2156 if (!binfo)
2157 error_not_base_type (basetype, TREE_TYPE (datum));
2158 return error_mark_node;
2161 *binfo_p = binfo;
2162 return build_base_path (PLUS_EXPR, datum, binfo, 1,
2163 tf_warning_or_error);
2166 /* Build a reference to an object specified by the C++ `->' operator.
2167 Usually this just involves dereferencing the object, but if the
2168 `->' operator is overloaded, then such overloads must be
2169 performed until an object which does not have the `->' operator
2170 overloaded is found. An error is reported when circular pointer
2171 delegation is detected. */
2173 tree
2174 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
2176 tree orig_expr = expr;
2177 tree type = TREE_TYPE (expr);
2178 tree last_rval = NULL_TREE;
2179 vec<tree, va_gc> *types_memoized = NULL;
2181 if (type == error_mark_node)
2182 return error_mark_node;
2184 if (processing_template_decl)
2186 tree ttype = NULL_TREE;
2187 if (type && TYPE_PTR_P (type))
2188 ttype = TREE_TYPE (type);
2189 if (ttype && !dependent_scope_p (ttype))
2190 /* Pointer to current instantiation, don't treat as dependent. */;
2191 else if (type_dependent_expression_p (expr))
2193 expr = build_min_nt_loc (loc, ARROW_EXPR, expr);
2194 TREE_TYPE (expr) = ttype;
2195 return expr;
2197 expr = build_non_dependent_expr (expr);
2200 if (MAYBE_CLASS_TYPE_P (type))
2202 struct tinst_level *actual_inst = current_instantiation ();
2203 tree fn = NULL;
2205 while ((expr = build_new_op (loc, COMPONENT_REF,
2206 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
2207 NULL_TREE, &fn, complain)))
2209 if (expr == error_mark_node)
2210 return error_mark_node;
2212 /* This provides a better instantiation backtrace in case of
2213 error. */
2214 if (fn && DECL_USE_TEMPLATE (fn))
2215 push_tinst_level_loc (fn,
2216 (current_instantiation () != actual_inst)
2217 ? DECL_SOURCE_LOCATION (fn)
2218 : input_location);
2219 fn = NULL;
2221 if (vec_member (TREE_TYPE (expr), types_memoized))
2223 if (complain & tf_error)
2224 error ("circular pointer delegation detected");
2225 return error_mark_node;
2228 vec_safe_push (types_memoized, TREE_TYPE (expr));
2229 last_rval = expr;
2232 while (current_instantiation () != actual_inst)
2233 pop_tinst_level ();
2235 if (last_rval == NULL_TREE)
2237 if (complain & tf_error)
2238 error ("base operand of %<->%> has non-pointer type %qT", type);
2239 return error_mark_node;
2242 if (TYPE_REF_P (TREE_TYPE (last_rval)))
2243 last_rval = convert_from_reference (last_rval);
2245 else
2247 last_rval = decay_conversion (expr, complain);
2248 if (last_rval == error_mark_node)
2249 return error_mark_node;
2252 if (TYPE_PTR_P (TREE_TYPE (last_rval)))
2254 if (processing_template_decl)
2256 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
2257 orig_expr);
2258 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
2259 return expr;
2262 return cp_build_indirect_ref (loc, last_rval, RO_ARROW, complain);
2265 if (complain & tf_error)
2267 if (types_memoized)
2268 error ("result of %<operator->()%> yields non-pointer result");
2269 else
2270 error ("base operand of %<->%> is not a pointer");
2272 return error_mark_node;
2275 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
2276 already been checked out to be of aggregate type. */
2278 tree
2279 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
2281 tree ptrmem_type;
2282 tree objtype;
2283 tree type;
2284 tree binfo;
2285 tree ctype;
2287 datum = mark_lvalue_use (datum);
2288 component = mark_rvalue_use (component);
2290 if (error_operand_p (datum) || error_operand_p (component))
2291 return error_mark_node;
2293 ptrmem_type = TREE_TYPE (component);
2294 if (!TYPE_PTRMEM_P (ptrmem_type))
2296 if (complain & tf_error)
2297 error ("%qE cannot be used as a member pointer, since it is of "
2298 "type %qT", component, ptrmem_type);
2299 return error_mark_node;
2302 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2303 if (! MAYBE_CLASS_TYPE_P (objtype))
2305 if (complain & tf_error)
2306 error ("cannot apply member pointer %qE to %qE, which is of "
2307 "non-class type %qT", component, datum, objtype);
2308 return error_mark_node;
2311 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
2312 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
2314 if (!COMPLETE_TYPE_P (ctype))
2316 if (!same_type_p (ctype, objtype))
2317 goto mismatch;
2318 binfo = NULL;
2320 else
2322 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
2324 if (!binfo)
2326 mismatch:
2327 if (complain & tf_error)
2328 error ("pointer to member type %qT incompatible with object "
2329 "type %qT", type, objtype);
2330 return error_mark_node;
2332 else if (binfo == error_mark_node)
2333 return error_mark_node;
2336 if (TYPE_PTRDATAMEM_P (ptrmem_type))
2338 bool is_lval = real_lvalue_p (datum);
2339 tree ptype;
2341 /* Compute the type of the field, as described in [expr.ref].
2342 There's no such thing as a mutable pointer-to-member, so
2343 things are not as complex as they are for references to
2344 non-static data members. */
2345 type = cp_build_qualified_type (type,
2346 (cp_type_quals (type)
2347 | cp_type_quals (TREE_TYPE (datum))));
2349 datum = build_address (datum);
2351 /* Convert object to the correct base. */
2352 if (binfo)
2354 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
2355 if (datum == error_mark_node)
2356 return error_mark_node;
2359 /* Build an expression for "object + offset" where offset is the
2360 value stored in the pointer-to-data-member. */
2361 ptype = build_pointer_type (type);
2362 datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
2363 datum = cp_build_fold_indirect_ref (datum);
2364 if (datum == error_mark_node)
2365 return error_mark_node;
2367 /* If the object expression was an rvalue, return an rvalue. */
2368 if (!is_lval)
2369 datum = move (datum);
2370 return datum;
2372 else
2374 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
2375 program is ill-formed if the second operand is a pointer to member
2376 function with ref-qualifier & (for C++20: unless its cv-qualifier-seq
2377 is const). In a .* expression whose object expression is an lvalue,
2378 the program is ill-formed if the second operand is a pointer to member
2379 function with ref-qualifier &&. */
2380 if (FUNCTION_REF_QUALIFIED (type))
2382 bool lval = lvalue_p (datum);
2383 if (lval && FUNCTION_RVALUE_QUALIFIED (type))
2385 if (complain & tf_error)
2386 error ("pointer-to-member-function type %qT requires an rvalue",
2387 ptrmem_type);
2388 return error_mark_node;
2390 else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
2392 if ((type_memfn_quals (type)
2393 & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))
2394 != TYPE_QUAL_CONST)
2396 if (complain & tf_error)
2397 error ("pointer-to-member-function type %qT requires "
2398 "an lvalue", ptrmem_type);
2399 return error_mark_node;
2401 else if (cxx_dialect < cxx20)
2403 if (complain & tf_warning_or_error)
2404 pedwarn (input_location, OPT_Wpedantic,
2405 "pointer-to-member-function type %qT requires "
2406 "an lvalue before C++20", ptrmem_type);
2407 else
2408 return error_mark_node;
2412 return build2 (OFFSET_REF, type, datum, component);
2416 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
2418 static tree
2419 build_functional_cast_1 (location_t loc, tree exp, tree parms,
2420 tsubst_flags_t complain)
2422 /* This is either a call to a constructor,
2423 or a C cast in C++'s `functional' notation. */
2425 /* The type to which we are casting. */
2426 tree type;
2428 if (error_operand_p (exp) || parms == error_mark_node)
2429 return error_mark_node;
2431 if (TREE_CODE (exp) == TYPE_DECL)
2433 type = TREE_TYPE (exp);
2435 if (DECL_ARTIFICIAL (exp))
2436 cp_handle_deprecated_or_unavailable (type);
2438 else
2439 type = exp;
2441 /* We need to check this explicitly, since value-initialization of
2442 arrays is allowed in other situations. */
2443 if (TREE_CODE (type) == ARRAY_TYPE)
2445 if (complain & tf_error)
2446 error_at (loc, "functional cast to array type %qT", type);
2447 return error_mark_node;
2450 if (tree anode = type_uses_auto (type))
2452 tree init;
2453 if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2454 init = parms;
2455 /* C++23 auto(x). */
2456 else if (!AUTO_IS_DECLTYPE (anode)
2457 && list_length (parms) == 1)
2459 init = TREE_VALUE (parms);
2460 if (is_constrained_auto (anode))
2462 if (complain & tf_error)
2463 error_at (loc, "%<auto(x)%> cannot be constrained");
2464 return error_mark_node;
2466 else if (cxx_dialect < cxx23)
2467 pedwarn (loc, OPT_Wc__23_extensions,
2468 "%<auto(x)%> only available with "
2469 "%<-std=c++2b%> or %<-std=gnu++2b%>");
2471 else
2473 if (complain & tf_error)
2474 error_at (loc, "invalid use of %qT", anode);
2475 return error_mark_node;
2477 type = do_auto_deduction (type, init, anode, complain,
2478 adc_variable_type);
2479 if (type == error_mark_node)
2480 return error_mark_node;
2483 if (processing_template_decl)
2485 tree t;
2487 /* Diagnose this even in a template. We could also try harder
2488 to give all the usual errors when the type and args are
2489 non-dependent... */
2490 if (TYPE_REF_P (type) && !parms)
2492 if (complain & tf_error)
2493 error_at (loc, "invalid value-initialization of reference type");
2494 return error_mark_node;
2497 t = build_min (CAST_EXPR, type, parms);
2498 /* We don't know if it will or will not have side effects. */
2499 TREE_SIDE_EFFECTS (t) = 1;
2500 return t;
2503 if (! MAYBE_CLASS_TYPE_P (type))
2505 if (parms == NULL_TREE)
2507 if (VOID_TYPE_P (type))
2508 return void_node;
2509 return build_value_init (cv_unqualified (type), complain);
2512 /* This must build a C cast. */
2513 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
2514 return cp_build_c_cast (loc, type, parms, complain);
2517 /* Prepare to evaluate as a call to a constructor. If this expression
2518 is actually used, for example,
2520 return X (arg1, arg2, ...);
2522 then the slot being initialized will be filled in. */
2524 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
2525 return error_mark_node;
2526 if (abstract_virtuals_error (ACU_CAST, type, complain))
2527 return error_mark_node;
2529 /* [expr.type.conv]
2531 If the expression list is a single-expression, the type
2532 conversion is equivalent (in definedness, and if defined in
2533 meaning) to the corresponding cast expression. */
2534 if (parms && TREE_CHAIN (parms) == NULL_TREE)
2535 return cp_build_c_cast (loc, type, TREE_VALUE (parms), complain);
2537 /* [expr.type.conv]
2539 The expression T(), where T is a simple-type-specifier for a
2540 non-array complete object type or the (possibly cv-qualified)
2541 void type, creates an rvalue of the specified type, which is
2542 value-initialized. */
2544 if (parms == NULL_TREE)
2546 exp = build_value_init (type, complain);
2547 exp = get_target_expr (exp, complain);
2548 return exp;
2551 /* Call the constructor. */
2552 releasing_vec parmvec;
2553 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
2554 vec_safe_push (parmvec, TREE_VALUE (parms));
2555 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2556 &parmvec, type, LOOKUP_NORMAL, complain);
2558 if (exp == error_mark_node)
2559 return error_mark_node;
2561 return build_cplus_new (type, exp, complain);
2564 tree
2565 build_functional_cast (location_t loc, tree exp, tree parms,
2566 tsubst_flags_t complain)
2568 tree result = build_functional_cast_1 (loc, exp, parms, complain);
2569 protected_set_expr_location (result, loc);
2570 return result;
2574 /* Add new exception specifier SPEC, to the LIST we currently have.
2575 If it's already in LIST then do nothing.
2576 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2577 know what we're doing. */
2579 tree
2580 add_exception_specifier (tree list, tree spec, tsubst_flags_t complain)
2582 bool ok;
2583 tree core = spec;
2584 bool is_ptr;
2585 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
2587 if (spec == error_mark_node)
2588 return list;
2590 gcc_assert (spec && (!list || TREE_VALUE (list)));
2592 /* [except.spec] 1, type in an exception specifier shall not be
2593 incomplete, or pointer or ref to incomplete other than pointer
2594 to cv void. */
2595 is_ptr = TYPE_PTR_P (core);
2596 if (is_ptr || TYPE_REF_P (core))
2597 core = TREE_TYPE (core);
2598 if (complain < 0)
2599 ok = true;
2600 else if (VOID_TYPE_P (core))
2601 ok = is_ptr;
2602 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
2603 ok = true;
2604 else if (processing_template_decl)
2605 ok = true;
2606 else if (!verify_type_context (input_location, TCTX_EXCEPTIONS, core,
2607 !(complain & tf_error)))
2608 return error_mark_node;
2609 else
2611 ok = true;
2612 /* 15.4/1 says that types in an exception specifier must be complete,
2613 but it seems more reasonable to only require this on definitions
2614 and calls. So just give a pedwarn at this point; we will give an
2615 error later if we hit one of those two cases. */
2616 if (!COMPLETE_TYPE_P (complete_type (core)))
2617 diag_type = DK_PEDWARN; /* pedwarn */
2620 if (ok)
2622 tree probe;
2624 for (probe = list; probe; probe = TREE_CHAIN (probe))
2625 if (same_type_p (TREE_VALUE (probe), spec))
2626 break;
2627 if (!probe)
2628 list = tree_cons (NULL_TREE, spec, list);
2630 else
2631 diag_type = DK_ERROR; /* error */
2633 if (diag_type != DK_UNSPECIFIED
2634 && (complain & tf_warning_or_error))
2635 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
2637 return list;
2640 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2642 static bool
2643 nothrow_spec_p_uninst (const_tree spec)
2645 if (DEFERRED_NOEXCEPT_SPEC_P (spec))
2646 return false;
2647 return nothrow_spec_p (spec);
2650 /* Combine the two exceptions specifier lists LIST and ADD, and return
2651 their union. */
2653 tree
2654 merge_exception_specifiers (tree list, tree add)
2656 tree noex, orig_list;
2658 if (list == error_mark_node || add == error_mark_node)
2659 return error_mark_node;
2661 /* No exception-specifier or noexcept(false) are less strict than
2662 anything else. Prefer the newer variant (LIST). */
2663 if (!list || list == noexcept_false_spec)
2664 return list;
2665 else if (!add || add == noexcept_false_spec)
2666 return add;
2668 /* noexcept(true) and throw() are stricter than anything else.
2669 As above, prefer the more recent one (LIST). */
2670 if (nothrow_spec_p_uninst (add))
2671 return list;
2673 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2674 if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
2675 && UNEVALUATED_NOEXCEPT_SPEC_P (list))
2676 return list;
2677 /* We should have instantiated other deferred noexcept specs by now. */
2678 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
2680 if (nothrow_spec_p_uninst (list))
2681 return add;
2682 noex = TREE_PURPOSE (list);
2683 gcc_checking_assert (!TREE_PURPOSE (add)
2684 || errorcount || !flag_exceptions
2685 || cp_tree_equal (noex, TREE_PURPOSE (add)));
2687 /* Combine the dynamic-exception-specifiers, if any. */
2688 orig_list = list;
2689 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
2691 tree spec = TREE_VALUE (add);
2692 tree probe;
2694 for (probe = orig_list; probe && TREE_VALUE (probe);
2695 probe = TREE_CHAIN (probe))
2696 if (same_type_p (TREE_VALUE (probe), spec))
2697 break;
2698 if (!probe)
2700 spec = build_tree_list (NULL_TREE, spec);
2701 TREE_CHAIN (spec) = list;
2702 list = spec;
2706 /* Keep the noexcept-specifier at the beginning of the list. */
2707 if (noex != TREE_PURPOSE (list))
2708 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2710 return list;
2713 /* Subroutine of build_call. Ensure that each of the types in the
2714 exception specification is complete. Technically, 15.4/1 says that
2715 they need to be complete when we see a declaration of the function,
2716 but we should be able to get away with only requiring this when the
2717 function is defined or called. See also add_exception_specifier. */
2719 void
2720 require_complete_eh_spec_types (tree fntype, tree decl)
2722 tree raises;
2723 /* Don't complain about calls to op new. */
2724 if (decl && DECL_ARTIFICIAL (decl))
2725 return;
2726 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2727 raises = TREE_CHAIN (raises))
2729 tree type = TREE_VALUE (raises);
2730 if (type && !COMPLETE_TYPE_P (type))
2732 if (decl)
2733 error
2734 ("call to function %qD which throws incomplete type %q#T",
2735 decl, type);
2736 else
2737 error ("call to function which throws incomplete type %q#T",
2738 decl);
2743 /* Record that any TARGET_EXPR in T are going to be elided in
2744 cp_gimplify_init_expr (or sooner). */
2746 void
2747 set_target_expr_eliding (tree t)
2749 if (!t)
2750 return;
2751 switch (TREE_CODE (t))
2753 case TARGET_EXPR:
2754 TARGET_EXPR_ELIDING_P (t) = true;
2755 break;
2756 case COMPOUND_EXPR:
2757 set_target_expr_eliding (TREE_OPERAND (t, 1));
2758 break;
2759 case COND_EXPR:
2760 set_target_expr_eliding (TREE_OPERAND (t, 1));
2761 set_target_expr_eliding (TREE_OPERAND (t, 2));
2762 break;
2764 default:
2765 break;
2769 /* Call the above in the process of building an INIT_EXPR. */
2771 tree
2772 cp_build_init_expr (location_t loc, tree target, tree init)
2774 set_target_expr_eliding (init);
2775 tree ie = build2_loc (loc, INIT_EXPR, TREE_TYPE (target),
2776 target, init);
2777 TREE_SIDE_EFFECTS (ie) = true;
2778 return ie;