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