c++: P2513R4, char8_t Compatibility and Portability Fix [PR106656]
[official-gcc.git] / gcc / cp / typeck2.cc
blob739097a97342e5ad25e9579d79b7eef93cf84767
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 = build2 (INIT_EXPR, inner_type, 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 = build2 (INIT_EXPR, void_type_node, 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 = build2 (INIT_EXPR, TREE_TYPE (dest), 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 ((same_type_p (ftype, long_double_type_node)
1001 && (same_type_p (type, double_type_node)
1002 || same_type_p (type, float_type_node)))
1003 || (same_type_p (ftype, double_type_node)
1004 && same_type_p (type, float_type_node))
1005 || (TYPE_PRECISION (type) < TYPE_PRECISION (ftype)))
1007 if (TREE_CODE (init) == REAL_CST)
1009 /* Issue 703: Loss of precision is OK as long as the value is
1010 within the representable range of the new type. */
1011 REAL_VALUE_TYPE r;
1012 d = TREE_REAL_CST (init);
1013 real_convert (&r, TYPE_MODE (type), &d);
1014 if (real_isinf (&r))
1015 ok = false;
1017 else
1018 ok = false;
1021 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
1022 && TREE_CODE (type) == REAL_TYPE)
1024 ok = false;
1025 if (TREE_CODE (init) == INTEGER_CST)
1027 d = real_value_from_int_cst (0, init);
1028 if (exact_real_truncate (TYPE_MODE (type), &d))
1029 ok = true;
1032 else if (TREE_CODE (type) == BOOLEAN_TYPE
1033 && (TYPE_PTR_P (ftype) || TYPE_PTRMEM_P (ftype)))
1034 /* C++20 P1957R2: converting from a pointer type or a pointer-to-member
1035 type to bool should be considered narrowing. This is a DR so is not
1036 limited to C++20 only. */
1037 ok = false;
1039 bool almost_ok = ok;
1040 if (!ok && !CONSTANT_CLASS_P (init) && (complain & tf_warning_or_error))
1042 tree folded = cp_fully_fold (init);
1043 if (TREE_CONSTANT (folded) && check_narrowing (type, folded, tf_none))
1044 almost_ok = true;
1047 if (!ok)
1049 location_t loc = cp_expr_loc_or_input_loc (init);
1050 if (cxx_dialect == cxx98)
1052 if (complain & tf_warning)
1053 warning_at (loc, OPT_Wnarrowing, "narrowing conversion of %qE "
1054 "from %qH to %qI is ill-formed in C++11",
1055 init, ftype, type);
1056 ok = true;
1058 else if (!CONSTANT_CLASS_P (init))
1060 if (complain & tf_warning_or_error)
1062 auto_diagnostic_group d;
1063 if ((!almost_ok || pedantic)
1064 && pedwarn (loc, OPT_Wnarrowing,
1065 "narrowing conversion of %qE from %qH to %qI",
1066 init, ftype, type)
1067 && almost_ok)
1068 inform (loc, " the expression has a constant value but is not "
1069 "a C++ constant-expression");
1070 ok = true;
1073 else if (complain & tf_error)
1075 int savederrorcount = errorcount;
1076 global_dc->pedantic_errors = 1;
1077 auto s = make_temp_override (global_dc->dc_warn_system_headers, true);
1078 pedwarn (loc, OPT_Wnarrowing,
1079 "narrowing conversion of %qE from %qH to %qI",
1080 init, ftype, type);
1081 if (errorcount == savederrorcount)
1082 ok = true;
1083 global_dc->pedantic_errors = flag_pedantic_errors;
1087 return ok;
1090 /* True iff TYPE is a C++20 "ordinary" character type. */
1092 bool
1093 ordinary_char_type_p (tree type)
1095 type = TYPE_MAIN_VARIANT (type);
1096 return (type == char_type_node
1097 || type == signed_char_type_node
1098 || type == unsigned_char_type_node);
1101 /* True iff the string literal INIT has a type suitable for initializing array
1102 TYPE. */
1104 bool
1105 array_string_literal_compatible_p (tree type, tree init)
1107 tree to_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1108 tree from_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
1110 if (to_char_type == from_char_type)
1111 return true;
1112 /* The array element type does not match the initializing string
1113 literal element type; this is only allowed when both types are
1114 ordinary character type. There are no string literals of
1115 signed or unsigned char type in the language, but we can get
1116 them internally from converting braced-init-lists to
1117 STRING_CST. */
1118 if (ordinary_char_type_p (to_char_type)
1119 && ordinary_char_type_p (from_char_type))
1120 return true;
1122 /* P2513 (C++20/C++23): "an array of char or unsigned char may
1123 be initialized by a UTF-8 string literal, or by such a string
1124 literal enclosed in braces." */
1125 if (from_char_type == char8_type_node
1126 && (to_char_type == char_type_node
1127 || to_char_type == unsigned_char_type_node))
1128 return true;
1130 return false;
1133 /* Process the initializer INIT for a variable of type TYPE, emitting
1134 diagnostics for invalid initializers and converting the initializer as
1135 appropriate.
1137 For aggregate types, it assumes that reshape_init has already run, thus the
1138 initializer will have the right shape (brace elision has been undone).
1140 NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR,
1141 2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR. */
1143 static tree
1144 digest_init_r (tree type, tree init, int nested, int flags,
1145 tsubst_flags_t complain)
1147 enum tree_code code = TREE_CODE (type);
1149 if (error_operand_p (init))
1150 return error_mark_node;
1152 gcc_assert (init);
1154 /* We must strip the outermost array type when completing the type,
1155 because the its bounds might be incomplete at the moment. */
1156 if (!complete_type_or_maybe_complain (code == ARRAY_TYPE
1157 ? TREE_TYPE (type) : type, NULL_TREE,
1158 complain))
1159 return error_mark_node;
1161 location_t loc = cp_expr_loc_or_input_loc (init);
1163 tree stripped_init = init;
1165 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1166 && CONSTRUCTOR_IS_PAREN_INIT (init))
1167 flags |= LOOKUP_AGGREGATE_PAREN_INIT;
1169 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
1170 (g++.old-deja/g++.law/casts2.C). */
1171 if (TREE_CODE (init) == NON_LVALUE_EXPR)
1172 stripped_init = TREE_OPERAND (init, 0);
1174 stripped_init = tree_strip_any_location_wrapper (stripped_init);
1176 /* Initialization of an array of chars from a string constant. The initializer
1177 can be optionally enclosed in braces, but reshape_init has already removed
1178 them if they were present. */
1179 if (code == ARRAY_TYPE)
1181 if (nested && !TYPE_DOMAIN (type))
1182 /* C++ flexible array members have a null domain. */
1184 if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT)
1185 pedwarn (loc, OPT_Wpedantic,
1186 "initialization of a flexible array member");
1187 else
1189 if (complain & tf_error)
1190 error_at (loc, "non-static initialization of"
1191 " a flexible array member");
1192 return error_mark_node;
1196 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1197 if (char_type_p (typ1)
1198 && TREE_CODE (stripped_init) == STRING_CST)
1200 if (!array_string_literal_compatible_p (type, init))
1202 if (complain & tf_error)
1203 error_at (loc, "cannot initialize array of %qT from "
1204 "a string literal with type array of %qT",
1205 typ1,
1206 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init))));
1207 return error_mark_node;
1210 if (nested == 2 && !TYPE_DOMAIN (type))
1212 if (complain & tf_error)
1213 error_at (loc, "initialization of flexible array member "
1214 "in a nested context");
1215 return error_mark_node;
1218 if (type != TREE_TYPE (init)
1219 && !variably_modified_type_p (type, NULL_TREE))
1221 init = copy_node (init);
1222 TREE_TYPE (init) = type;
1223 /* If we have a location wrapper, then also copy the wrapped
1224 node, and update the copy's type. */
1225 if (location_wrapper_p (init))
1227 stripped_init = copy_node (stripped_init);
1228 TREE_OPERAND (init, 0) = stripped_init;
1229 TREE_TYPE (stripped_init) = type;
1232 if (TYPE_DOMAIN (type) && TREE_CONSTANT (TYPE_SIZE (type)))
1234 /* Not a flexible array member. */
1235 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1236 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1237 /* In C it is ok to subtract 1 from the length of the string
1238 because it's ok to ignore the terminating null char that is
1239 counted in the length of the constant, but in C++ this would
1240 be invalid. */
1241 if (size < TREE_STRING_LENGTH (stripped_init))
1243 permerror (loc, "initializer-string for %qT is too long",
1244 type);
1246 init = build_string (size,
1247 TREE_STRING_POINTER (stripped_init));
1248 TREE_TYPE (init) = type;
1251 return init;
1255 /* Handle scalar types (including conversions) and references. */
1256 if ((code != COMPLEX_TYPE || BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
1257 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1259 /* Narrowing is OK when initializing an aggregate from
1260 a parenthesized list. */
1261 if (nested && !(flags & LOOKUP_AGGREGATE_PAREN_INIT))
1262 flags |= LOOKUP_NO_NARROWING;
1263 init = convert_for_initialization (0, type, init, flags,
1264 ICR_INIT, NULL_TREE, 0,
1265 complain);
1267 return init;
1270 /* Come here only for aggregates: records, arrays, unions, complex numbers
1271 and vectors. */
1272 gcc_assert (code == ARRAY_TYPE
1273 || VECTOR_TYPE_P (type)
1274 || code == RECORD_TYPE
1275 || code == UNION_TYPE
1276 || code == OPAQUE_TYPE
1277 || code == COMPLEX_TYPE);
1279 /* "If T is a class type and the initializer list has a single
1280 element of type cv U, where U is T or a class derived from T,
1281 the object is initialized from that element." */
1282 if (cxx_dialect >= cxx11
1283 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
1284 && !CONSTRUCTOR_IS_DESIGNATED_INIT (stripped_init)
1285 && CONSTRUCTOR_NELTS (stripped_init) == 1
1286 && ((CLASS_TYPE_P (type) && !CLASSTYPE_NON_AGGREGATE (type))
1287 || VECTOR_TYPE_P (type)))
1289 tree elt = CONSTRUCTOR_ELT (stripped_init, 0)->value;
1290 if (reference_related_p (type, TREE_TYPE (elt)))
1292 /* In C++17, aggregates can have bases, thus participate in
1293 aggregate initialization. In the following case:
1295 struct B { int c; };
1296 struct D : B { };
1297 D d{{D{{42}}}};
1299 there's an extra set of braces, so the D temporary initializes
1300 the first element of d, which is the B base subobject. The base
1301 of type B is copy-initialized from the D temporary, causing
1302 object slicing. */
1303 tree field = next_aggregate_field (TYPE_FIELDS (type));
1304 if (field && DECL_FIELD_IS_BASE (field))
1306 if (warning_at (loc, 0, "initializing a base class of type %qT "
1307 "results in object slicing", TREE_TYPE (field)))
1308 inform (loc, "remove %<{ }%> around initializer");
1310 else if (flag_checking)
1311 /* We should have fixed this in reshape_init. */
1312 gcc_unreachable ();
1316 if (SIMPLE_TARGET_EXPR_P (stripped_init))
1317 stripped_init = TARGET_EXPR_INITIAL (stripped_init);
1319 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
1320 && !TYPE_NON_AGGREGATE_CLASS (type))
1321 return process_init_constructor (type, stripped_init, nested, flags,
1322 complain);
1323 else
1325 if (COMPOUND_LITERAL_P (stripped_init) && code == ARRAY_TYPE)
1327 if (complain & tf_error)
1328 error_at (loc, "cannot initialize aggregate of type %qT with "
1329 "a compound literal", type);
1331 return error_mark_node;
1334 if (code == ARRAY_TYPE
1335 && !BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
1337 /* Allow the result of build_array_copy and of
1338 build_value_init_noctor. */
1339 if ((TREE_CODE (stripped_init) == VEC_INIT_EXPR
1340 || TREE_CODE (stripped_init) == CONSTRUCTOR)
1341 && (same_type_ignoring_top_level_qualifiers_p
1342 (type, TREE_TYPE (init))))
1343 return init;
1345 if (complain & tf_error)
1346 error_at (loc, "array must be initialized with a brace-enclosed"
1347 " initializer");
1348 return error_mark_node;
1351 return convert_for_initialization (NULL_TREE, type, init,
1352 flags,
1353 ICR_INIT, NULL_TREE, 0,
1354 complain);
1358 tree
1359 digest_init (tree type, tree init, tsubst_flags_t complain)
1361 return digest_init_r (type, init, 0, LOOKUP_IMPLICIT, complain);
1364 tree
1365 digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain)
1367 return digest_init_r (type, init, 0, flags, complain);
1370 /* Return true if SUBOB initializes the same object as FULL_EXPR.
1371 For instance:
1373 A a = A{}; // initializer
1374 A a = (A{}); // initializer
1375 A a = (1, A{}); // initializer
1376 A a = true ? A{} : A{}; // initializer
1377 auto x = A{}.x; // temporary materialization
1378 auto x = foo(A{}); // temporary materialization
1380 FULL_EXPR is the whole expression, SUBOB is its TARGET_EXPR subobject. */
1382 static bool
1383 potential_prvalue_result_of (tree subob, tree full_expr)
1385 if (subob == full_expr)
1386 return true;
1387 else if (TREE_CODE (full_expr) == TARGET_EXPR)
1389 tree init = TARGET_EXPR_INITIAL (full_expr);
1390 if (TREE_CODE (init) == COND_EXPR)
1391 return (potential_prvalue_result_of (subob, TREE_OPERAND (init, 1))
1392 || potential_prvalue_result_of (subob, TREE_OPERAND (init, 2)));
1393 else if (TREE_CODE (init) == COMPOUND_EXPR)
1394 return potential_prvalue_result_of (subob, TREE_OPERAND (init, 1));
1395 /* ??? I don't know if this can be hit. */
1396 else if (TREE_CODE (init) == PAREN_EXPR)
1398 gcc_checking_assert (false);
1399 return potential_prvalue_result_of (subob, TREE_OPERAND (init, 0));
1402 return false;
1405 /* Callback to replace PLACEHOLDER_EXPRs in a TARGET_EXPR (which isn't used
1406 in the context of guaranteed copy elision). */
1408 static tree
1409 replace_placeholders_for_class_temp_r (tree *tp, int *, void *data)
1411 tree t = *tp;
1412 tree full_expr = *static_cast<tree *>(data);
1414 /* We're looking for a TARGET_EXPR nested in the whole expression. */
1415 if (TREE_CODE (t) == TARGET_EXPR
1416 && !potential_prvalue_result_of (t, full_expr))
1418 tree init = TARGET_EXPR_INITIAL (t);
1419 while (TREE_CODE (init) == COMPOUND_EXPR)
1420 init = TREE_OPERAND (init, 1);
1421 if (TREE_CODE (init) == CONSTRUCTOR
1422 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init))
1424 tree obj = TARGET_EXPR_SLOT (t);
1425 replace_placeholders (init, obj);
1426 /* We should have dealt with all PLACEHOLDER_EXPRs. */
1427 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = false;
1428 gcc_checking_assert (!find_placeholders (init));
1432 return NULL_TREE;
1435 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1436 tree
1437 digest_nsdmi_init (tree decl, tree init, tsubst_flags_t complain)
1439 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1441 tree type = TREE_TYPE (decl);
1442 if (DECL_BIT_FIELD_TYPE (decl))
1443 type = DECL_BIT_FIELD_TYPE (decl);
1444 int flags = LOOKUP_IMPLICIT;
1445 if (DIRECT_LIST_INIT_P (init))
1447 flags = LOOKUP_NORMAL;
1448 complain |= tf_no_cleanup;
1450 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1451 && CP_AGGREGATE_TYPE_P (type))
1452 init = reshape_init (type, init, complain);
1453 init = digest_init_flags (type, init, flags, complain);
1455 /* We may have temporary materialization in a NSDMI, if the initializer
1456 has something like A{} in it. Digesting the {} could have introduced
1457 a PLACEHOLDER_EXPR referring to A. Now that we've got a TARGET_EXPR,
1458 we have an object we can refer to. The reason we bother doing this
1459 here is for code like
1461 struct A {
1462 int x;
1463 int y = x;
1466 struct B {
1467 int x = 0;
1468 int y = A{x}.y; // #1
1471 where in #1 we don't want to end up with two PLACEHOLDER_EXPRs for
1472 different types on the same level in a {} when lookup_placeholder
1473 wouldn't find a named object for the PLACEHOLDER_EXPR for A. Note,
1474 temporary materialization does not occur when initializing an object
1475 from a prvalue of the same type, therefore we must not replace the
1476 placeholder with a temporary object so that it can be elided. */
1477 cp_walk_tree (&init, replace_placeholders_for_class_temp_r, &init,
1478 nullptr);
1480 return init;
1483 /* Set of flags used within process_init_constructor to describe the
1484 initializers. */
1485 #define PICFLAG_ERRONEOUS 1
1486 #define PICFLAG_NOT_ALL_CONSTANT 2
1487 #define PICFLAG_NOT_ALL_SIMPLE 4
1488 #define PICFLAG_SIDE_EFFECTS 8
1489 #define PICFLAG_VEC_INIT 16
1491 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1492 describe it. */
1494 static int
1495 picflag_from_initializer (tree init)
1497 if (init == error_mark_node)
1498 return PICFLAG_ERRONEOUS;
1499 else if (!TREE_CONSTANT (init))
1501 if (TREE_SIDE_EFFECTS (init))
1502 return PICFLAG_SIDE_EFFECTS;
1503 else
1504 return PICFLAG_NOT_ALL_CONSTANT;
1506 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1507 return PICFLAG_NOT_ALL_SIMPLE;
1508 return 0;
1511 /* Adjust INIT for going into a CONSTRUCTOR. */
1513 static tree
1514 massage_init_elt (tree type, tree init, int nested, int flags,
1515 tsubst_flags_t complain)
1517 int new_flags = LOOKUP_IMPLICIT;
1518 if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT)
1519 new_flags |= LOOKUP_ALLOW_FLEXARRAY_INIT;
1520 if (flags & LOOKUP_AGGREGATE_PAREN_INIT)
1521 new_flags |= LOOKUP_AGGREGATE_PAREN_INIT;
1522 init = digest_init_r (type, init, nested ? 2 : 1, new_flags, complain);
1523 /* When we defer constant folding within a statement, we may want to
1524 defer this folding as well. Don't call this on CONSTRUCTORs because
1525 their elements have already been folded, and we must avoid folding
1526 the result of get_nsdmi. */
1527 if (TREE_CODE (init) != CONSTRUCTOR)
1529 tree t = fold_non_dependent_init (init, complain);
1530 if (TREE_CONSTANT (t))
1531 init = t;
1533 return init;
1536 /* Subroutine of process_init_constructor, which will process an initializer
1537 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1538 which describe the initializers. */
1540 static int
1541 process_init_constructor_array (tree type, tree init, int nested, int flags,
1542 tsubst_flags_t complain)
1544 unsigned HOST_WIDE_INT i, len = 0;
1545 int picflags = 0;
1546 bool unbounded = false;
1547 constructor_elt *ce;
1548 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1550 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1551 || VECTOR_TYPE_P (type));
1553 if (TREE_CODE (type) == ARRAY_TYPE)
1555 /* C++ flexible array members have a null domain. */
1556 tree domain = TYPE_DOMAIN (type);
1557 if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1558 len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
1559 - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
1560 TYPE_PRECISION (TREE_TYPE (domain)),
1561 TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
1562 else
1563 unbounded = true; /* Take as many as there are. */
1565 if (nested == 2 && !domain && !vec_safe_is_empty (v))
1567 if (complain & tf_error)
1568 error_at (cp_expr_loc_or_input_loc (init),
1569 "initialization of flexible array member "
1570 "in a nested context");
1571 return PICFLAG_ERRONEOUS;
1574 else
1575 /* Vectors are like simple fixed-size arrays. */
1576 unbounded = !TYPE_VECTOR_SUBPARTS (type).is_constant (&len);
1578 /* There must not be more initializers than needed. */
1579 if (!unbounded && vec_safe_length (v) > len)
1581 if (complain & tf_error)
1582 error ("too many initializers for %qT", type);
1583 else
1584 return PICFLAG_ERRONEOUS;
1587 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1589 if (!ce->index)
1590 ce->index = size_int (i);
1591 else if (!check_array_designated_initializer (ce, i))
1592 ce->index = error_mark_node;
1593 gcc_assert (ce->value);
1594 ce->value
1595 = massage_init_elt (TREE_TYPE (type), ce->value, nested, flags,
1596 complain);
1598 gcc_checking_assert
1599 (ce->value == error_mark_node
1600 || (same_type_ignoring_top_level_qualifiers_p
1601 (strip_array_types (TREE_TYPE (type)),
1602 strip_array_types (TREE_TYPE (ce->value)))));
1604 picflags |= picflag_from_initializer (ce->value);
1605 /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer
1606 CONSTRUCTOR. */
1607 if (TREE_CODE (ce->value) == CONSTRUCTOR
1608 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value))
1610 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1611 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value) = 0;
1615 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1616 we must add initializers ourselves. */
1617 if (!unbounded)
1618 for (; i < len; ++i)
1620 tree next;
1622 if (type_build_ctor_call (TREE_TYPE (type)))
1624 /* If this type needs constructors run for default-initialization,
1625 we can't rely on the back end to do it for us, so make the
1626 initialization explicit by list-initializing from T{}. */
1627 next = build_constructor (init_list_type_node, NULL);
1628 next = massage_init_elt (TREE_TYPE (type), next, nested, flags,
1629 complain);
1630 if (initializer_zerop (next))
1631 /* The default zero-initialization is fine for us; don't
1632 add anything to the CONSTRUCTOR. */
1633 next = NULL_TREE;
1635 else if (!zero_init_p (TREE_TYPE (type)))
1636 next = build_zero_init (TREE_TYPE (type),
1637 /*nelts=*/NULL_TREE,
1638 /*static_storage_p=*/false);
1639 else
1640 /* The default zero-initialization is fine for us; don't
1641 add anything to the CONSTRUCTOR. */
1642 next = NULL_TREE;
1644 if (next)
1646 if (next != error_mark_node
1647 && ! seen_error () // Improves error-recovery on anew5.C.
1648 && (initializer_constant_valid_p (next, TREE_TYPE (next))
1649 != null_pointer_node))
1651 /* Use VEC_INIT_EXPR for non-constant initialization of
1652 trailing elements with no explicit initializers. */
1653 picflags |= PICFLAG_VEC_INIT;
1654 break;
1657 picflags |= picflag_from_initializer (next);
1658 /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer
1659 CONSTRUCTOR. */
1660 if (TREE_CODE (next) == CONSTRUCTOR
1661 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next))
1663 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1664 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next) = 0;
1666 if (len > i+1)
1668 tree range = build2 (RANGE_EXPR, size_type_node,
1669 build_int_cst (size_type_node, i),
1670 build_int_cst (size_type_node, len - 1));
1671 CONSTRUCTOR_APPEND_ELT (v, range, next);
1672 break;
1674 else
1675 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1677 else
1678 /* Don't bother checking all the other elements. */
1679 break;
1682 CONSTRUCTOR_ELTS (init) = v;
1683 return picflags;
1686 /* Subroutine of process_init_constructor, which will process an initializer
1687 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1688 the initializers. */
1690 static int
1691 process_init_constructor_record (tree type, tree init, int nested, int flags,
1692 tsubst_flags_t complain)
1694 vec<constructor_elt, va_gc> *v = NULL;
1695 tree field;
1696 int skipped = 0;
1698 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1699 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1700 gcc_assert (!TYPE_BINFO (type)
1701 || cxx_dialect >= cxx17
1702 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1703 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1705 restart:
1706 int picflags = 0;
1707 unsigned HOST_WIDE_INT idx = 0;
1708 int designator_skip = -1;
1709 /* Generally, we will always have an index for each initializer (which is
1710 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1711 reshape_init. So we need to handle both cases. */
1712 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1714 tree next;
1716 if (TREE_CODE (field) != FIELD_DECL
1717 || (DECL_ARTIFICIAL (field)
1718 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))))
1719 continue;
1721 if (DECL_UNNAMED_BIT_FIELD (field))
1722 continue;
1724 /* If this is a bitfield, first convert to the declared type. */
1725 tree fldtype = TREE_TYPE (field);
1726 if (DECL_BIT_FIELD_TYPE (field))
1727 fldtype = DECL_BIT_FIELD_TYPE (field);
1728 if (fldtype == error_mark_node)
1729 return PICFLAG_ERRONEOUS;
1731 next = NULL_TREE;
1732 if (idx < CONSTRUCTOR_NELTS (init))
1734 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1735 if (ce->index)
1737 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1738 latter case can happen in templates where lookup has to be
1739 deferred. */
1740 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1741 || identifier_p (ce->index));
1742 if (ce->index == field || ce->index == DECL_NAME (field))
1743 next = ce->value;
1744 else
1746 ce = NULL;
1747 if (designator_skip == -1)
1748 designator_skip = 1;
1751 else
1753 designator_skip = 0;
1754 next = ce->value;
1757 if (ce)
1759 gcc_assert (ce->value);
1760 next = massage_init_elt (fldtype, next, nested, flags, complain);
1761 ++idx;
1764 if (next == error_mark_node)
1765 /* We skip initializers for empty bases/fields, so skipping an invalid
1766 one could make us accept invalid code. */
1767 return PICFLAG_ERRONEOUS;
1768 else if (next)
1769 /* Already handled above. */;
1770 else if (DECL_INITIAL (field))
1772 if (skipped > 0)
1774 /* We're using an NSDMI past a field with implicit
1775 zero-init. Go back and make it explicit. */
1776 skipped = -1;
1777 vec_safe_truncate (v, 0);
1778 goto restart;
1780 /* C++14 aggregate NSDMI. */
1781 next = get_nsdmi (field, /*ctor*/false, complain);
1782 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
1783 && find_placeholders (next))
1784 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1786 else if (type_build_ctor_call (fldtype))
1788 /* If this type needs constructors run for
1789 default-initialization, we can't rely on the back end to do it
1790 for us, so build up TARGET_EXPRs. If the type in question is
1791 a class, just build one up; if it's an array, recurse. */
1792 next = build_constructor (init_list_type_node, NULL);
1793 next = massage_init_elt (fldtype, next, nested, flags, complain);
1795 /* Warn when some struct elements are implicitly initialized. */
1796 if ((complain & tf_warning)
1797 && !cp_unevaluated_operand
1798 && !EMPTY_CONSTRUCTOR_P (init))
1799 warning (OPT_Wmissing_field_initializers,
1800 "missing initializer for member %qD", field);
1802 else
1804 if (TYPE_REF_P (fldtype))
1806 if (complain & tf_error)
1807 error ("member %qD is uninitialized reference", field);
1808 else
1809 return PICFLAG_ERRONEOUS;
1811 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype))
1813 if (complain & tf_error)
1814 error ("member %qD with uninitialized reference fields", field);
1815 else
1816 return PICFLAG_ERRONEOUS;
1818 /* Do nothing for flexible array members since they need not have any
1819 elements. Don't worry about 'skipped' because a flexarray has to
1820 be the last field. */
1821 else if (TREE_CODE (fldtype) == ARRAY_TYPE && !TYPE_DOMAIN (fldtype))
1822 continue;
1824 /* Warn when some struct elements are implicitly initialized
1825 to zero. */
1826 if ((complain & tf_warning)
1827 && !cp_unevaluated_operand
1828 && !EMPTY_CONSTRUCTOR_P (init))
1829 warning (OPT_Wmissing_field_initializers,
1830 "missing initializer for member %qD", field);
1832 if (!zero_init_p (fldtype) || skipped < 0)
1834 if (TYPE_REF_P (fldtype))
1835 next = build_zero_cst (fldtype);
1836 else
1837 next = build_zero_init (fldtype, /*nelts=*/NULL_TREE,
1838 /*static_storage_p=*/false);
1840 else
1842 /* The default zero-initialization is fine for us; don't
1843 add anything to the CONSTRUCTOR. */
1844 skipped = 1;
1845 continue;
1849 if (is_empty_field (field)
1850 && !TREE_SIDE_EFFECTS (next))
1851 /* Don't add trivial initialization of an empty base/field to the
1852 constructor, as they might not be ordered the way the back-end
1853 expects. */
1854 continue;
1856 /* If this is a bitfield, now convert to the lowered type. */
1857 if (fldtype != TREE_TYPE (field))
1858 next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1859 picflags |= picflag_from_initializer (next);
1860 /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer CONSTRUCTOR. */
1861 if (TREE_CODE (next) == CONSTRUCTOR
1862 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next))
1864 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1865 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (next) = 0;
1867 CONSTRUCTOR_APPEND_ELT (v, field, next);
1870 if (idx < CONSTRUCTOR_NELTS (init))
1872 if (complain & tf_error)
1874 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1875 /* For better diagnostics, try to find out if it is really
1876 the case of too many initializers or if designators are
1877 in incorrect order. */
1878 if (designator_skip == 1 && ce->index)
1880 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1881 || identifier_p (ce->index));
1882 for (field = TYPE_FIELDS (type);
1883 field; field = DECL_CHAIN (field))
1885 if (TREE_CODE (field) != FIELD_DECL
1886 || (DECL_ARTIFICIAL (field)
1887 && !(cxx_dialect >= cxx17
1888 && DECL_FIELD_IS_BASE (field))))
1889 continue;
1891 if (DECL_UNNAMED_BIT_FIELD (field))
1892 continue;
1894 if (ce->index == field || ce->index == DECL_NAME (field))
1895 break;
1898 if (field)
1899 error ("designator order for field %qD does not match declaration "
1900 "order in %qT", field, type);
1901 else
1902 error ("too many initializers for %qT", type);
1904 else
1905 return PICFLAG_ERRONEOUS;
1908 CONSTRUCTOR_ELTS (init) = v;
1909 return picflags;
1912 /* Subroutine of process_init_constructor, which will process a single
1913 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1914 which describe the initializer. */
1916 static int
1917 process_init_constructor_union (tree type, tree init, int nested, int flags,
1918 tsubst_flags_t complain)
1920 constructor_elt *ce;
1921 int len;
1923 /* If the initializer was empty, use the union's NSDMI if it has one.
1924 Otherwise use default zero initialization. */
1925 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1927 for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1929 if (TREE_CODE (field) == FIELD_DECL
1930 && DECL_INITIAL (field) != NULL_TREE)
1932 tree val = get_nsdmi (field, /*in_ctor=*/false, complain);
1933 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
1934 && find_placeholders (val))
1935 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1936 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init), field, val);
1937 break;
1941 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1942 return 0;
1945 len = CONSTRUCTOR_ELTS (init)->length ();
1946 if (len > 1)
1948 if (!(complain & tf_error))
1949 return PICFLAG_ERRONEOUS;
1950 error ("too many initializers for %qT", type);
1951 CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1954 ce = &(*CONSTRUCTOR_ELTS (init))[0];
1956 /* If this element specifies a field, initialize via that field. */
1957 if (ce->index)
1959 if (TREE_CODE (ce->index) == FIELD_DECL)
1961 else if (identifier_p (ce->index))
1963 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1964 tree name = ce->index;
1965 tree field;
1966 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1967 if (DECL_NAME (field) == name)
1968 break;
1969 if (!field)
1971 if (complain & tf_error)
1972 error ("no field %qD found in union being initialized",
1973 field);
1974 ce->value = error_mark_node;
1976 ce->index = field;
1978 else
1980 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1981 || TREE_CODE (ce->index) == RANGE_EXPR);
1982 if (complain & tf_error)
1983 error ("index value instead of field name in union initializer");
1984 ce->value = error_mark_node;
1987 else
1989 /* Find the first named field. ANSI decided in September 1990
1990 that only named fields count here. */
1991 tree field = TYPE_FIELDS (type);
1992 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1993 field = TREE_CHAIN (field);
1994 if (field == NULL_TREE)
1996 if (complain & tf_error)
1997 error ("too many initializers for %qT", type);
1998 ce->value = error_mark_node;
2000 ce->index = field;
2003 if (ce->value && ce->value != error_mark_node)
2004 ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, nested,
2005 flags, complain);
2007 /* Propagate CONSTRUCTOR_PLACEHOLDER_BOUNDARY to outer CONSTRUCTOR. */
2008 if (ce->value
2009 && TREE_CODE (ce->value) == CONSTRUCTOR
2010 && CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value))
2012 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
2013 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ce->value) = 0;
2015 return picflag_from_initializer (ce->value);
2018 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
2019 constructor is a brace-enclosed initializer, and will be modified in-place.
2021 Each element is converted to the right type through digest_init, and
2022 missing initializers are added following the language rules (zero-padding,
2023 etc.).
2025 After the execution, the initializer will have TREE_CONSTANT if all elts are
2026 constant, and TREE_STATIC set if, in addition, all elts are simple enough
2027 constants that the assembler and linker can compute them.
2029 The function returns the initializer itself, or error_mark_node in case
2030 of error. */
2032 static tree
2033 process_init_constructor (tree type, tree init, int nested, int flags,
2034 tsubst_flags_t complain)
2036 int picflags;
2038 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
2040 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
2041 picflags = process_init_constructor_array (type, init, nested, flags,
2042 complain);
2043 else if (TREE_CODE (type) == RECORD_TYPE)
2044 picflags = process_init_constructor_record (type, init, nested, flags,
2045 complain);
2046 else if (TREE_CODE (type) == UNION_TYPE)
2047 picflags = process_init_constructor_union (type, init, nested, flags,
2048 complain);
2049 else
2050 gcc_unreachable ();
2052 if (picflags & PICFLAG_ERRONEOUS)
2053 return error_mark_node;
2055 TREE_TYPE (init) = type;
2056 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
2057 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
2058 if (picflags & PICFLAG_SIDE_EFFECTS)
2060 TREE_CONSTANT (init) = false;
2061 TREE_SIDE_EFFECTS (init) = true;
2063 else if (picflags & PICFLAG_NOT_ALL_CONSTANT)
2065 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
2066 TREE_CONSTANT (init) = false;
2067 TREE_SIDE_EFFECTS (init) = false;
2069 else
2071 TREE_CONSTANT (init) = 1;
2072 TREE_SIDE_EFFECTS (init) = false;
2073 if (!(picflags & PICFLAG_NOT_ALL_SIMPLE))
2074 TREE_STATIC (init) = 1;
2076 if (picflags & PICFLAG_VEC_INIT)
2078 /* Defer default-initialization of array elements with no corresponding
2079 initializer-clause until later so we can use a loop. */
2080 TREE_TYPE (init) = init_list_type_node;
2081 init = build_vec_init_expr (type, init, complain);
2082 init = get_target_expr (init);
2084 return init;
2087 /* Given a structure or union value DATUM, construct and return
2088 the structure or union component which results from narrowing
2089 that value to the base specified in BASETYPE. For example, given the
2090 hierarchy
2092 class L { int ii; };
2093 class A : L { ... };
2094 class B : L { ... };
2095 class C : A, B { ... };
2097 and the declaration
2099 C x;
2101 then the expression
2103 x.A::ii refers to the ii member of the L part of
2104 the A part of the C object named by X. In this case,
2105 DATUM would be x, and BASETYPE would be A.
2107 I used to think that this was nonconformant, that the standard specified
2108 that first we look up ii in A, then convert x to an L& and pull out the
2109 ii part. But in fact, it does say that we convert x to an A&; A here
2110 is known as the "naming class". (jason 2000-12-19)
2112 BINFO_P points to a variable initialized either to NULL_TREE or to the
2113 binfo for the specific base subobject we want to convert to. */
2115 tree
2116 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
2118 tree binfo;
2120 if (datum == error_mark_node)
2121 return error_mark_node;
2122 if (*binfo_p)
2123 binfo = *binfo_p;
2124 else
2125 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
2126 NULL, tf_warning_or_error);
2128 if (!binfo || binfo == error_mark_node)
2130 *binfo_p = NULL_TREE;
2131 if (!binfo)
2132 error_not_base_type (basetype, TREE_TYPE (datum));
2133 return error_mark_node;
2136 *binfo_p = binfo;
2137 return build_base_path (PLUS_EXPR, datum, binfo, 1,
2138 tf_warning_or_error);
2141 /* Build a reference to an object specified by the C++ `->' operator.
2142 Usually this just involves dereferencing the object, but if the
2143 `->' operator is overloaded, then such overloads must be
2144 performed until an object which does not have the `->' operator
2145 overloaded is found. An error is reported when circular pointer
2146 delegation is detected. */
2148 tree
2149 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
2151 tree orig_expr = expr;
2152 tree type = TREE_TYPE (expr);
2153 tree last_rval = NULL_TREE;
2154 vec<tree, va_gc> *types_memoized = NULL;
2156 if (type == error_mark_node)
2157 return error_mark_node;
2159 if (processing_template_decl)
2161 tree ttype = NULL_TREE;
2162 if (type && TYPE_PTR_P (type))
2163 ttype = TREE_TYPE (type);
2164 if (ttype && !dependent_scope_p (ttype))
2165 /* Pointer to current instantiation, don't treat as dependent. */;
2166 else if (type_dependent_expression_p (expr))
2168 expr = build_min_nt_loc (loc, ARROW_EXPR, expr);
2169 TREE_TYPE (expr) = ttype;
2170 return expr;
2172 expr = build_non_dependent_expr (expr);
2175 if (MAYBE_CLASS_TYPE_P (type))
2177 struct tinst_level *actual_inst = current_instantiation ();
2178 tree fn = NULL;
2180 while ((expr = build_new_op (loc, COMPONENT_REF,
2181 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
2182 NULL_TREE, &fn, complain)))
2184 if (expr == error_mark_node)
2185 return error_mark_node;
2187 /* This provides a better instantiation backtrace in case of
2188 error. */
2189 if (fn && DECL_USE_TEMPLATE (fn))
2190 push_tinst_level_loc (fn,
2191 (current_instantiation () != actual_inst)
2192 ? DECL_SOURCE_LOCATION (fn)
2193 : input_location);
2194 fn = NULL;
2196 if (vec_member (TREE_TYPE (expr), types_memoized))
2198 if (complain & tf_error)
2199 error ("circular pointer delegation detected");
2200 return error_mark_node;
2203 vec_safe_push (types_memoized, TREE_TYPE (expr));
2204 last_rval = expr;
2207 while (current_instantiation () != actual_inst)
2208 pop_tinst_level ();
2210 if (last_rval == NULL_TREE)
2212 if (complain & tf_error)
2213 error ("base operand of %<->%> has non-pointer type %qT", type);
2214 return error_mark_node;
2217 if (TYPE_REF_P (TREE_TYPE (last_rval)))
2218 last_rval = convert_from_reference (last_rval);
2220 else
2222 last_rval = decay_conversion (expr, complain);
2223 if (last_rval == error_mark_node)
2224 return error_mark_node;
2227 if (TYPE_PTR_P (TREE_TYPE (last_rval)))
2229 if (processing_template_decl)
2231 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
2232 orig_expr);
2233 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
2234 return expr;
2237 return cp_build_indirect_ref (loc, last_rval, RO_ARROW, complain);
2240 if (complain & tf_error)
2242 if (types_memoized)
2243 error ("result of %<operator->()%> yields non-pointer result");
2244 else
2245 error ("base operand of %<->%> is not a pointer");
2247 return error_mark_node;
2250 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
2251 already been checked out to be of aggregate type. */
2253 tree
2254 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
2256 tree ptrmem_type;
2257 tree objtype;
2258 tree type;
2259 tree binfo;
2260 tree ctype;
2262 datum = mark_lvalue_use (datum);
2263 component = mark_rvalue_use (component);
2265 if (error_operand_p (datum) || error_operand_p (component))
2266 return error_mark_node;
2268 ptrmem_type = TREE_TYPE (component);
2269 if (!TYPE_PTRMEM_P (ptrmem_type))
2271 if (complain & tf_error)
2272 error ("%qE cannot be used as a member pointer, since it is of "
2273 "type %qT", component, ptrmem_type);
2274 return error_mark_node;
2277 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2278 if (! MAYBE_CLASS_TYPE_P (objtype))
2280 if (complain & tf_error)
2281 error ("cannot apply member pointer %qE to %qE, which is of "
2282 "non-class type %qT", component, datum, objtype);
2283 return error_mark_node;
2286 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
2287 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
2289 if (!COMPLETE_TYPE_P (ctype))
2291 if (!same_type_p (ctype, objtype))
2292 goto mismatch;
2293 binfo = NULL;
2295 else
2297 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
2299 if (!binfo)
2301 mismatch:
2302 if (complain & tf_error)
2303 error ("pointer to member type %qT incompatible with object "
2304 "type %qT", type, objtype);
2305 return error_mark_node;
2307 else if (binfo == error_mark_node)
2308 return error_mark_node;
2311 if (TYPE_PTRDATAMEM_P (ptrmem_type))
2313 bool is_lval = real_lvalue_p (datum);
2314 tree ptype;
2316 /* Compute the type of the field, as described in [expr.ref].
2317 There's no such thing as a mutable pointer-to-member, so
2318 things are not as complex as they are for references to
2319 non-static data members. */
2320 type = cp_build_qualified_type (type,
2321 (cp_type_quals (type)
2322 | cp_type_quals (TREE_TYPE (datum))));
2324 datum = build_address (datum);
2326 /* Convert object to the correct base. */
2327 if (binfo)
2329 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
2330 if (datum == error_mark_node)
2331 return error_mark_node;
2334 /* Build an expression for "object + offset" where offset is the
2335 value stored in the pointer-to-data-member. */
2336 ptype = build_pointer_type (type);
2337 datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
2338 datum = cp_build_fold_indirect_ref (datum);
2339 if (datum == error_mark_node)
2340 return error_mark_node;
2342 /* If the object expression was an rvalue, return an rvalue. */
2343 if (!is_lval)
2344 datum = move (datum);
2345 return datum;
2347 else
2349 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
2350 program is ill-formed if the second operand is a pointer to member
2351 function with ref-qualifier & (for C++20: unless its cv-qualifier-seq
2352 is const). In a .* expression whose object expression is an lvalue,
2353 the program is ill-formed if the second operand is a pointer to member
2354 function with ref-qualifier &&. */
2355 if (FUNCTION_REF_QUALIFIED (type))
2357 bool lval = lvalue_p (datum);
2358 if (lval && FUNCTION_RVALUE_QUALIFIED (type))
2360 if (complain & tf_error)
2361 error ("pointer-to-member-function type %qT requires an rvalue",
2362 ptrmem_type);
2363 return error_mark_node;
2365 else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
2367 if ((type_memfn_quals (type)
2368 & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))
2369 != TYPE_QUAL_CONST)
2371 if (complain & tf_error)
2372 error ("pointer-to-member-function type %qT requires "
2373 "an lvalue", ptrmem_type);
2374 return error_mark_node;
2376 else if (cxx_dialect < cxx20)
2378 if (complain & tf_warning_or_error)
2379 pedwarn (input_location, OPT_Wpedantic,
2380 "pointer-to-member-function type %qT requires "
2381 "an lvalue before C++20", ptrmem_type);
2382 else
2383 return error_mark_node;
2387 return build2 (OFFSET_REF, type, datum, component);
2391 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
2393 static tree
2394 build_functional_cast_1 (location_t loc, tree exp, tree parms,
2395 tsubst_flags_t complain)
2397 /* This is either a call to a constructor,
2398 or a C cast in C++'s `functional' notation. */
2400 /* The type to which we are casting. */
2401 tree type;
2403 if (error_operand_p (exp) || parms == error_mark_node)
2404 return error_mark_node;
2406 if (TREE_CODE (exp) == TYPE_DECL)
2408 type = TREE_TYPE (exp);
2410 if (DECL_ARTIFICIAL (exp))
2411 cp_handle_deprecated_or_unavailable (type);
2413 else
2414 type = exp;
2416 /* We need to check this explicitly, since value-initialization of
2417 arrays is allowed in other situations. */
2418 if (TREE_CODE (type) == ARRAY_TYPE)
2420 if (complain & tf_error)
2421 error_at (loc, "functional cast to array type %qT", type);
2422 return error_mark_node;
2425 if (tree anode = type_uses_auto (type))
2427 tree init;
2428 if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2429 init = parms;
2430 /* C++23 auto(x). */
2431 else if (!AUTO_IS_DECLTYPE (anode)
2432 && list_length (parms) == 1)
2434 init = TREE_VALUE (parms);
2435 if (is_constrained_auto (anode))
2437 if (complain & tf_error)
2438 error_at (loc, "%<auto(x)%> cannot be constrained");
2439 return error_mark_node;
2441 else if (cxx_dialect < cxx23)
2442 pedwarn (loc, OPT_Wc__23_extensions,
2443 "%<auto(x)%> only available with "
2444 "%<-std=c++2b%> or %<-std=gnu++2b%>");
2446 else
2448 if (complain & tf_error)
2449 error_at (loc, "invalid use of %qT", anode);
2450 return error_mark_node;
2452 type = do_auto_deduction (type, init, anode, complain,
2453 adc_variable_type);
2454 if (type == error_mark_node)
2455 return error_mark_node;
2458 if (processing_template_decl)
2460 tree t;
2462 /* Diagnose this even in a template. We could also try harder
2463 to give all the usual errors when the type and args are
2464 non-dependent... */
2465 if (TYPE_REF_P (type) && !parms)
2467 if (complain & tf_error)
2468 error_at (loc, "invalid value-initialization of reference type");
2469 return error_mark_node;
2472 t = build_min (CAST_EXPR, type, parms);
2473 /* We don't know if it will or will not have side effects. */
2474 TREE_SIDE_EFFECTS (t) = 1;
2475 return t;
2478 if (! MAYBE_CLASS_TYPE_P (type))
2480 if (parms == NULL_TREE)
2482 if (VOID_TYPE_P (type))
2483 return void_node;
2484 return build_value_init (cv_unqualified (type), complain);
2487 /* This must build a C cast. */
2488 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
2489 return cp_build_c_cast (loc, type, parms, complain);
2492 /* Prepare to evaluate as a call to a constructor. If this expression
2493 is actually used, for example,
2495 return X (arg1, arg2, ...);
2497 then the slot being initialized will be filled in. */
2499 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
2500 return error_mark_node;
2501 if (abstract_virtuals_error (ACU_CAST, type, complain))
2502 return error_mark_node;
2504 /* [expr.type.conv]
2506 If the expression list is a single-expression, the type
2507 conversion is equivalent (in definedness, and if defined in
2508 meaning) to the corresponding cast expression. */
2509 if (parms && TREE_CHAIN (parms) == NULL_TREE)
2510 return cp_build_c_cast (loc, type, TREE_VALUE (parms), complain);
2512 /* [expr.type.conv]
2514 The expression T(), where T is a simple-type-specifier for a
2515 non-array complete object type or the (possibly cv-qualified)
2516 void type, creates an rvalue of the specified type, which is
2517 value-initialized. */
2519 if (parms == NULL_TREE)
2521 exp = build_value_init (type, complain);
2522 exp = get_target_expr (exp, complain);
2523 return exp;
2526 /* Call the constructor. */
2527 releasing_vec parmvec;
2528 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
2529 vec_safe_push (parmvec, TREE_VALUE (parms));
2530 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2531 &parmvec, type, LOOKUP_NORMAL, complain);
2533 if (exp == error_mark_node)
2534 return error_mark_node;
2536 return build_cplus_new (type, exp, complain);
2539 tree
2540 build_functional_cast (location_t loc, tree exp, tree parms,
2541 tsubst_flags_t complain)
2543 tree result = build_functional_cast_1 (loc, exp, parms, complain);
2544 protected_set_expr_location (result, loc);
2545 return result;
2549 /* Add new exception specifier SPEC, to the LIST we currently have.
2550 If it's already in LIST then do nothing.
2551 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2552 know what we're doing. */
2554 tree
2555 add_exception_specifier (tree list, tree spec, tsubst_flags_t complain)
2557 bool ok;
2558 tree core = spec;
2559 bool is_ptr;
2560 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
2562 if (spec == error_mark_node)
2563 return list;
2565 gcc_assert (spec && (!list || TREE_VALUE (list)));
2567 /* [except.spec] 1, type in an exception specifier shall not be
2568 incomplete, or pointer or ref to incomplete other than pointer
2569 to cv void. */
2570 is_ptr = TYPE_PTR_P (core);
2571 if (is_ptr || TYPE_REF_P (core))
2572 core = TREE_TYPE (core);
2573 if (complain < 0)
2574 ok = true;
2575 else if (VOID_TYPE_P (core))
2576 ok = is_ptr;
2577 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
2578 ok = true;
2579 else if (processing_template_decl)
2580 ok = true;
2581 else if (!verify_type_context (input_location, TCTX_EXCEPTIONS, core,
2582 !(complain & tf_error)))
2583 return error_mark_node;
2584 else
2586 ok = true;
2587 /* 15.4/1 says that types in an exception specifier must be complete,
2588 but it seems more reasonable to only require this on definitions
2589 and calls. So just give a pedwarn at this point; we will give an
2590 error later if we hit one of those two cases. */
2591 if (!COMPLETE_TYPE_P (complete_type (core)))
2592 diag_type = DK_PEDWARN; /* pedwarn */
2595 if (ok)
2597 tree probe;
2599 for (probe = list; probe; probe = TREE_CHAIN (probe))
2600 if (same_type_p (TREE_VALUE (probe), spec))
2601 break;
2602 if (!probe)
2603 list = tree_cons (NULL_TREE, spec, list);
2605 else
2606 diag_type = DK_ERROR; /* error */
2608 if (diag_type != DK_UNSPECIFIED
2609 && (complain & tf_warning_or_error))
2610 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
2612 return list;
2615 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2617 static bool
2618 nothrow_spec_p_uninst (const_tree spec)
2620 if (DEFERRED_NOEXCEPT_SPEC_P (spec))
2621 return false;
2622 return nothrow_spec_p (spec);
2625 /* Combine the two exceptions specifier lists LIST and ADD, and return
2626 their union. */
2628 tree
2629 merge_exception_specifiers (tree list, tree add)
2631 tree noex, orig_list;
2633 if (list == error_mark_node || add == error_mark_node)
2634 return error_mark_node;
2636 /* No exception-specifier or noexcept(false) are less strict than
2637 anything else. Prefer the newer variant (LIST). */
2638 if (!list || list == noexcept_false_spec)
2639 return list;
2640 else if (!add || add == noexcept_false_spec)
2641 return add;
2643 /* noexcept(true) and throw() are stricter than anything else.
2644 As above, prefer the more recent one (LIST). */
2645 if (nothrow_spec_p_uninst (add))
2646 return list;
2648 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2649 if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
2650 && UNEVALUATED_NOEXCEPT_SPEC_P (list))
2651 return list;
2652 /* We should have instantiated other deferred noexcept specs by now. */
2653 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
2655 if (nothrow_spec_p_uninst (list))
2656 return add;
2657 noex = TREE_PURPOSE (list);
2658 gcc_checking_assert (!TREE_PURPOSE (add)
2659 || errorcount || !flag_exceptions
2660 || cp_tree_equal (noex, TREE_PURPOSE (add)));
2662 /* Combine the dynamic-exception-specifiers, if any. */
2663 orig_list = list;
2664 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
2666 tree spec = TREE_VALUE (add);
2667 tree probe;
2669 for (probe = orig_list; probe && TREE_VALUE (probe);
2670 probe = TREE_CHAIN (probe))
2671 if (same_type_p (TREE_VALUE (probe), spec))
2672 break;
2673 if (!probe)
2675 spec = build_tree_list (NULL_TREE, spec);
2676 TREE_CHAIN (spec) = list;
2677 list = spec;
2681 /* Keep the noexcept-specifier at the beginning of the list. */
2682 if (noex != TREE_PURPOSE (list))
2683 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2685 return list;
2688 /* Subroutine of build_call. Ensure that each of the types in the
2689 exception specification is complete. Technically, 15.4/1 says that
2690 they need to be complete when we see a declaration of the function,
2691 but we should be able to get away with only requiring this when the
2692 function is defined or called. See also add_exception_specifier. */
2694 void
2695 require_complete_eh_spec_types (tree fntype, tree decl)
2697 tree raises;
2698 /* Don't complain about calls to op new. */
2699 if (decl && DECL_ARTIFICIAL (decl))
2700 return;
2701 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2702 raises = TREE_CHAIN (raises))
2704 tree type = TREE_VALUE (raises);
2705 if (type && !COMPLETE_TYPE_P (type))
2707 if (decl)
2708 error
2709 ("call to function %qD which throws incomplete type %q#T",
2710 decl, type);
2711 else
2712 error ("call to function which throws incomplete type %q#T",
2713 decl);