Don't warn when alignment of global common data exceeds maximum alignment.
[official-gcc.git] / gcc / cp / typeck2.c
blobdcfdff2f90553b01ea1835a0e73271636edb4ca7
1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987-2021 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_sfinae (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_sfinae (tree decl, tree type, tsubst_flags_t complain)
256 return abstract_virtuals_error_sfinae (decl, type, ACU_UNKNOWN, complain);
260 abstract_virtuals_error_sfinae (abstract_class_use use, tree type,
261 tsubst_flags_t complain)
263 return abstract_virtuals_error_sfinae (NULL_TREE, type, use, complain);
267 /* Wrapper for the above function in the common case of wanting errors. */
270 abstract_virtuals_error (tree decl, tree type)
272 return abstract_virtuals_error_sfinae (decl, type, tf_warning_or_error);
276 abstract_virtuals_error (abstract_class_use use, tree type)
278 return abstract_virtuals_error_sfinae (use, type, tf_warning_or_error);
281 /* Print an inform about the declaration of the incomplete type TYPE. */
283 void
284 cxx_incomplete_type_inform (const_tree type)
286 if (!TYPE_MAIN_DECL (type))
287 return;
289 location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type));
290 tree ptype = strip_top_quals (CONST_CAST_TREE (type));
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);
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). */
308 void
309 cxx_incomplete_type_diagnostic (location_t loc, const_tree value,
310 const_tree type, diagnostic_t diag_kind)
312 bool is_decl = false, complained = false;
314 gcc_assert (diag_kind == DK_WARNING
315 || diag_kind == DK_PEDWARN
316 || diag_kind == DK_ERROR);
318 /* Avoid duplicate error message. */
319 if (TREE_CODE (type) == ERROR_MARK)
320 return;
322 if (value)
324 STRIP_ANY_LOCATION_WRAPPER (value);
326 if (VAR_P (value)
327 || TREE_CODE (value) == PARM_DECL
328 || TREE_CODE (value) == FIELD_DECL)
330 complained = emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (value), 0,
331 "%qD has incomplete type", value);
332 is_decl = true;
335 retry:
336 /* We must print an error message. Be clever about what it says. */
338 switch (TREE_CODE (type))
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;
351 case VOID_TYPE:
352 emit_diagnostic (diag_kind, loc, 0,
353 "invalid use of %qT", type);
354 break;
356 case ARRAY_TYPE:
357 if (TYPE_DOMAIN (type))
359 type = TREE_TYPE (type);
360 goto retry;
362 emit_diagnostic (diag_kind, loc, 0,
363 "invalid use of array with unspecified bounds");
364 break;
366 case OFFSET_TYPE:
367 bad_member:
369 tree member = TREE_OPERAND (value, 1);
370 if (is_overloaded_fn (member))
371 member = get_first_fn (member);
373 if (DECL_FUNCTION_MEMBER_P (member)
374 && ! flag_ms_extensions)
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);
385 else
386 emit_diagnostic (diag_kind, loc, 0,
387 "invalid use of member %qD "
388 "(did you forget the %<&%> ?)", member);
390 break;
392 case TEMPLATE_TYPE_PARM:
393 if (is_auto (type))
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);
402 else
403 emit_diagnostic (diag_kind, loc, 0,
404 "invalid use of template type parameter %qT", type);
405 break;
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;
413 case TYPE_PACK_EXPANSION:
414 emit_diagnostic (diag_kind, loc, 0,
415 "invalid use of pack expansion %qT", type);
416 break;
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;
424 case LANG_TYPE:
425 if (type == init_list_type_node)
427 emit_diagnostic (diag_kind, loc, 0,
428 "invalid use of brace-enclosed initializer list");
429 break;
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;
446 default:
447 gcc_unreachable ();
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. */
455 void
456 cxx_incomplete_type_error (location_t loc, const_tree value, const_tree type)
458 cxx_incomplete_type_diagnostic (loc, value, type, DK_ERROR);
462 /* The recursive part of split_nonconstant_init. DEST is an lvalue
463 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
464 Return true if the whole of the value was initialized by the
465 generated statements. */
467 static bool
468 split_nonconstant_init_1 (tree dest, tree init, bool nested)
470 unsigned HOST_WIDE_INT idx, tidx = HOST_WIDE_INT_M1U;
471 tree field_index, value;
472 tree type = TREE_TYPE (dest);
473 tree inner_type = NULL;
474 bool array_type_p = false;
475 bool complete_p = true;
476 HOST_WIDE_INT num_split_elts = 0;
478 switch (TREE_CODE (type))
480 case ARRAY_TYPE:
481 inner_type = TREE_TYPE (type);
482 array_type_p = true;
483 if ((TREE_SIDE_EFFECTS (init)
484 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
485 || vla_type_p (type))
487 /* For an array, we only need/want a single cleanup region rather
488 than one per element. */
489 tree code = build_vec_init (dest, NULL_TREE, init, false, 1,
490 tf_warning_or_error);
491 add_stmt (code);
492 if (nested)
493 /* Also clean up the whole array if something later in an enclosing
494 init-list throws. */
495 if (tree cleanup = cxx_maybe_build_cleanup (dest,
496 tf_warning_or_error))
497 finish_eh_cleanup (cleanup);
498 return true;
500 /* FALLTHRU */
502 case RECORD_TYPE:
503 case UNION_TYPE:
504 case QUAL_UNION_TYPE:
505 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
506 field_index, value)
508 /* The current implementation of this algorithm assumes that
509 the field was set for all the elements. This is usually done
510 by process_init_constructor. */
511 gcc_assert (field_index);
513 if (!array_type_p)
514 inner_type = TREE_TYPE (field_index);
516 if (TREE_CODE (value) == CONSTRUCTOR)
518 tree sub;
520 if (array_type_p)
521 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
522 NULL_TREE, NULL_TREE);
523 else
524 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
525 NULL_TREE);
527 if (!split_nonconstant_init_1 (sub, value, true))
528 complete_p = false;
529 else
531 /* Mark element for removal. */
532 CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE;
533 if (idx < tidx)
534 tidx = idx;
535 num_split_elts++;
538 else if (!initializer_constant_valid_p (value, inner_type))
540 tree code;
541 tree sub;
543 /* Mark element for removal. */
544 CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE;
545 if (idx < tidx)
546 tidx = idx;
548 if (TREE_CODE (field_index) == RANGE_EXPR)
550 /* Use build_vec_init to initialize a range. */
551 tree low = TREE_OPERAND (field_index, 0);
552 tree hi = TREE_OPERAND (field_index, 1);
553 sub = build4 (ARRAY_REF, inner_type, dest, low,
554 NULL_TREE, NULL_TREE);
555 sub = cp_build_addr_expr (sub, tf_warning_or_error);
556 tree max = size_binop (MINUS_EXPR, hi, low);
557 code = build_vec_init (sub, max, value, false, 0,
558 tf_warning_or_error);
559 add_stmt (code);
560 if (tree_fits_shwi_p (max))
561 num_split_elts += tree_to_shwi (max);
563 else
565 if (array_type_p)
566 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
567 NULL_TREE, NULL_TREE);
568 else
569 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
570 NULL_TREE);
572 /* We may need to add a copy constructor call if
573 the field has [[no_unique_address]]. */
574 if (unsafe_return_slot_p (sub))
576 /* But not if the initializer is an implicit ctor call
577 we just built in digest_init. */
578 if (TREE_CODE (value) == TARGET_EXPR
579 && TARGET_EXPR_LIST_INIT_P (value)
580 && make_safe_copy_elision (sub, value))
581 goto build_init;
583 tree name = (DECL_FIELD_IS_BASE (field_index)
584 ? base_ctor_identifier
585 : complete_ctor_identifier);
586 releasing_vec args = make_tree_vector_single (value);
587 code = build_special_member_call
588 (sub, name, &args, inner_type,
589 LOOKUP_NORMAL, tf_warning_or_error);
591 else
593 build_init:
594 code = build2 (INIT_EXPR, inner_type, sub, value);
596 code = build_stmt (input_location, EXPR_STMT, code);
597 code = maybe_cleanup_point_expr_void (code);
598 add_stmt (code);
599 if (tree cleanup
600 = cxx_maybe_build_cleanup (sub, tf_warning_or_error))
601 finish_eh_cleanup (cleanup);
604 num_split_elts++;
607 if (num_split_elts == 1)
608 CONSTRUCTOR_ELTS (init)->ordered_remove (tidx);
609 else if (num_split_elts > 1)
611 /* Perform the delayed ordered removal of non-constant elements
612 we split out. */
613 for (idx = tidx; idx < CONSTRUCTOR_NELTS (init); ++idx)
614 if (CONSTRUCTOR_ELT (init, idx)->index == NULL_TREE)
616 else
618 *CONSTRUCTOR_ELT (init, tidx) = *CONSTRUCTOR_ELT (init, idx);
619 ++tidx;
621 vec_safe_truncate (CONSTRUCTOR_ELTS (init), tidx);
623 break;
625 case VECTOR_TYPE:
626 if (!initializer_constant_valid_p (init, type))
628 tree code;
629 tree cons = copy_node (init);
630 CONSTRUCTOR_ELTS (init) = NULL;
631 code = build2 (MODIFY_EXPR, type, dest, cons);
632 code = build_stmt (input_location, EXPR_STMT, code);
633 add_stmt (code);
634 num_split_elts += CONSTRUCTOR_NELTS (init);
636 break;
638 default:
639 gcc_unreachable ();
642 /* The rest of the initializer is now a constant. */
643 TREE_CONSTANT (init) = 1;
644 TREE_SIDE_EFFECTS (init) = 0;
646 /* We didn't split out anything. */
647 if (num_split_elts == 0)
648 return false;
650 return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
651 num_split_elts, inner_type);
654 /* A subroutine of store_init_value. Splits non-constant static
655 initializer INIT into a constant part and generates code to
656 perform the non-constant part of the initialization to DEST.
657 Returns the code for the runtime init. */
659 tree
660 split_nonconstant_init (tree dest, tree init)
662 tree code;
664 if (TREE_CODE (init) == TARGET_EXPR)
665 init = TARGET_EXPR_INITIAL (init);
666 if (TREE_CODE (init) == CONSTRUCTOR)
668 init = cp_fully_fold_init (init);
669 code = push_stmt_list ();
670 if (split_nonconstant_init_1 (dest, init, false))
671 init = NULL_TREE;
672 code = pop_stmt_list (code);
673 if (VAR_P (dest) && !is_local_temp (dest))
675 DECL_INITIAL (dest) = init;
676 TREE_READONLY (dest) = 0;
678 else if (init)
680 tree ie = build2 (INIT_EXPR, void_type_node, dest, init);
681 code = add_stmt_to_compound (ie, code);
684 else if (TREE_CODE (init) == STRING_CST
685 && array_of_runtime_bound_p (TREE_TYPE (dest)))
686 code = build_vec_init (dest, NULL_TREE, init, /*value-init*/false,
687 /*from array*/1, tf_warning_or_error);
688 else
689 code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
691 return code;
694 /* Perform appropriate conversions on the initial value of a variable,
695 store it in the declaration DECL,
696 and print any error messages that are appropriate.
697 If the init is invalid, store an ERROR_MARK.
699 C++: Note that INIT might be a TREE_LIST, which would mean that it is
700 a base class initializer for some aggregate type, hopefully compatible
701 with DECL. If INIT is a single element, and DECL is an aggregate
702 type, we silently convert INIT into a TREE_LIST, allowing a constructor
703 to be called.
705 If INIT is a TREE_LIST and there is no constructor, turn INIT
706 into a CONSTRUCTOR and use standard initialization techniques.
707 Perhaps a warning should be generated?
709 Returns code to be executed if initialization could not be performed
710 for static variable. In that case, caller must emit the code. */
712 tree
713 store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
715 tree value, type;
717 /* If variable's type was invalidly declared, just ignore it. */
719 type = TREE_TYPE (decl);
720 if (TREE_CODE (type) == ERROR_MARK)
721 return NULL_TREE;
723 if (MAYBE_CLASS_TYPE_P (type))
725 if (TREE_CODE (init) == TREE_LIST)
727 error ("constructor syntax used, but no constructor declared "
728 "for type %qT", type);
729 init = build_constructor_from_list (init_list_type_node, nreverse (init));
733 /* End of special C++ code. */
735 if (flags & LOOKUP_ALREADY_DIGESTED)
736 value = init;
737 else
739 if (TREE_STATIC (decl))
740 flags |= LOOKUP_ALLOW_FLEXARRAY_INIT;
741 /* Digest the specified initializer into an expression. */
742 value = digest_init_flags (type, init, flags, tf_warning_or_error);
745 /* Look for braced array initializers for character arrays and
746 recursively convert them into STRING_CSTs. */
747 value = braced_lists_to_strings (type, value);
749 current_ref_temp_count = 0;
750 value = extend_ref_init_temps (decl, value, cleanups);
752 /* In C++11 constant expression is a semantic, not syntactic, property.
753 In C++98, make sure that what we thought was a constant expression at
754 template definition time is still constant and otherwise perform this
755 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
756 if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
758 bool const_init;
759 tree oldval = value;
760 if (DECL_DECLARED_CONSTEXPR_P (decl)
761 || (DECL_IN_AGGR_P (decl)
762 && DECL_INITIALIZED_IN_CLASS_P (decl)))
764 value = fold_non_dependent_expr (value, tf_warning_or_error,
765 /*manifestly_const_eval=*/true,
766 decl);
767 /* Diagnose a non-constant initializer for constexpr variable or
768 non-inline in-class-initialized static data member. */
769 if (!require_constant_expression (value))
770 value = error_mark_node;
771 else if (processing_template_decl)
772 /* In a template we might not have done the necessary
773 transformations to make value actually constant,
774 e.g. extend_ref_init_temps. */
775 value = maybe_constant_init (value, decl, true);
776 else
777 value = cxx_constant_init (value, decl);
779 else
780 value = fold_non_dependent_init (value, tf_warning_or_error,
781 /*manifestly_const_eval=*/true, decl);
782 if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type))
783 /* Poison this CONSTRUCTOR so it can't be copied to another
784 constexpr variable. */
785 CONSTRUCTOR_MUTABLE_POISON (value) = true;
786 const_init = (reduced_constant_expression_p (value)
787 || error_operand_p (value));
788 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
789 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
790 if (!TYPE_REF_P (type))
791 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
792 if (!const_init)
794 /* [dcl.constinit]/2 "If a variable declared with the constinit
795 specifier has dynamic initialization, the program is
796 ill-formed." */
797 if (DECL_DECLARED_CONSTINIT_P (decl))
799 error_at (location_of (decl),
800 "%<constinit%> variable %qD does not have a constant "
801 "initializer", decl);
802 if (require_constant_expression (value))
803 cxx_constant_init (value, decl);
804 value = error_mark_node;
806 else
807 value = oldval;
810 /* Don't fold initializers of automatic variables in constexpr functions,
811 that might fold away something that needs to be diagnosed at constexpr
812 evaluation time. */
813 if (!current_function_decl
814 || !DECL_DECLARED_CONSTEXPR_P (current_function_decl)
815 || TREE_STATIC (decl))
816 value = cp_fully_fold_init (value);
818 /* Handle aggregate NSDMI in non-constant initializers, too. */
819 value = replace_placeholders (value, decl);
821 /* A COMPOUND_LITERAL_P CONSTRUCTOR is the syntactic form; by the time we get
822 here it should have been digested into an actual value for the type. */
823 gcc_checking_assert (TREE_CODE (value) != CONSTRUCTOR
824 || processing_template_decl
825 || !TREE_HAS_CONSTRUCTOR (value));
827 /* If the initializer is not a constant, fill in DECL_INITIAL with
828 the bits that are constant, and then return an expression that
829 will perform the dynamic initialization. */
830 if (value != error_mark_node
831 && !processing_template_decl
832 && (TREE_SIDE_EFFECTS (value)
833 || vla_type_p (type)
834 || ! reduced_constant_expression_p (value)))
835 return split_nonconstant_init (decl, value);
837 /* DECL may change value; purge caches. */
838 clear_cv_and_fold_caches ();
840 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
841 is an automatic variable, the middle end will turn this into a
842 dynamic initialization later. */
843 DECL_INITIAL (decl) = value;
844 return NULL_TREE;
848 /* Give diagnostic about narrowing conversions within { }, or as part of
849 a converted constant expression. If CONST_ONLY, only check
850 constants. */
852 bool
853 check_narrowing (tree type, tree init, tsubst_flags_t complain,
854 bool const_only/*= false*/)
856 tree ftype = unlowered_expr_type (init);
857 bool ok = true;
858 REAL_VALUE_TYPE d;
860 if (((!warn_narrowing || !(complain & tf_warning))
861 && cxx_dialect == cxx98)
862 || !ARITHMETIC_TYPE_P (type)
863 /* Don't emit bogus warnings with e.g. value-dependent trees. */
864 || instantiation_dependent_expression_p (init))
865 return ok;
867 if (BRACE_ENCLOSED_INITIALIZER_P (init)
868 && TREE_CODE (type) == COMPLEX_TYPE)
870 tree elttype = TREE_TYPE (type);
871 if (CONSTRUCTOR_NELTS (init) > 0)
872 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value,
873 complain);
874 if (CONSTRUCTOR_NELTS (init) > 1)
875 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value,
876 complain);
877 return ok;
880 /* Even non-dependent expressions can still have template
881 codes like CAST_EXPR, so use *_non_dependent_expr to cope. */
882 init = fold_non_dependent_expr (init, complain);
883 if (init == error_mark_node)
884 return ok;
886 /* If we were asked to only check constants, return early. */
887 if (const_only && !TREE_CONSTANT (init))
888 return ok;
890 if (CP_INTEGRAL_TYPE_P (type)
891 && TREE_CODE (ftype) == REAL_TYPE)
892 ok = false;
893 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
894 && CP_INTEGRAL_TYPE_P (type))
896 if (TREE_CODE (ftype) == ENUMERAL_TYPE)
897 /* Check for narrowing based on the values of the enumeration. */
898 ftype = ENUM_UNDERLYING_TYPE (ftype);
899 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
900 TYPE_MAX_VALUE (ftype))
901 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
902 TYPE_MIN_VALUE (type)))
903 && (TREE_CODE (init) != INTEGER_CST
904 || !int_fits_type_p (init, type)))
905 ok = false;
907 /* [dcl.init.list]#7.2: "from long double to double or float, or from
908 double to float". */
909 else if (TREE_CODE (ftype) == REAL_TYPE
910 && TREE_CODE (type) == REAL_TYPE)
912 if ((same_type_p (ftype, long_double_type_node)
913 && (same_type_p (type, double_type_node)
914 || same_type_p (type, float_type_node)))
915 || (same_type_p (ftype, double_type_node)
916 && same_type_p (type, float_type_node))
917 || (TYPE_PRECISION (type) < TYPE_PRECISION (ftype)))
919 if (TREE_CODE (init) == REAL_CST)
921 /* Issue 703: Loss of precision is OK as long as the value is
922 within the representable range of the new type. */
923 REAL_VALUE_TYPE r;
924 d = TREE_REAL_CST (init);
925 real_convert (&r, TYPE_MODE (type), &d);
926 if (real_isinf (&r))
927 ok = false;
929 else
930 ok = false;
933 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
934 && TREE_CODE (type) == REAL_TYPE)
936 ok = false;
937 if (TREE_CODE (init) == INTEGER_CST)
939 d = real_value_from_int_cst (0, init);
940 if (exact_real_truncate (TYPE_MODE (type), &d))
941 ok = true;
944 else if (TREE_CODE (type) == BOOLEAN_TYPE
945 && (TYPE_PTR_P (ftype) || TYPE_PTRMEM_P (ftype)))
946 /* C++20 P1957R2: converting from a pointer type or a pointer-to-member
947 type to bool should be considered narrowing. This is a DR so is not
948 limited to C++20 only. */
949 ok = false;
951 bool almost_ok = ok;
952 if (!ok && !CONSTANT_CLASS_P (init) && (complain & tf_warning_or_error))
954 tree folded = cp_fully_fold (init);
955 if (TREE_CONSTANT (folded) && check_narrowing (type, folded, tf_none))
956 almost_ok = true;
959 if (!ok)
961 location_t loc = cp_expr_loc_or_input_loc (init);
962 if (cxx_dialect == cxx98)
964 if (complain & tf_warning)
965 warning_at (loc, OPT_Wnarrowing, "narrowing conversion of %qE "
966 "from %qH to %qI is ill-formed in C++11",
967 init, ftype, type);
968 ok = true;
970 else if (!CONSTANT_CLASS_P (init))
972 if (complain & tf_warning_or_error)
974 auto_diagnostic_group d;
975 if ((!almost_ok || pedantic)
976 && pedwarn (loc, OPT_Wnarrowing,
977 "narrowing conversion of %qE from %qH to %qI",
978 init, ftype, type)
979 && almost_ok)
980 inform (loc, " the expression has a constant value but is not "
981 "a C++ constant-expression");
982 ok = true;
985 else if (complain & tf_error)
987 int savederrorcount = errorcount;
988 global_dc->pedantic_errors = 1;
989 auto s = make_temp_override (global_dc->dc_warn_system_headers, true);
990 pedwarn (loc, OPT_Wnarrowing,
991 "narrowing conversion of %qE from %qH to %qI",
992 init, ftype, type);
993 if (errorcount == savederrorcount)
994 ok = true;
995 global_dc->pedantic_errors = flag_pedantic_errors;
999 return ok;
1002 /* True iff TYPE is a C++20 "ordinary" character type. */
1004 bool
1005 ordinary_char_type_p (tree type)
1007 type = TYPE_MAIN_VARIANT (type);
1008 return (type == char_type_node
1009 || type == signed_char_type_node
1010 || type == unsigned_char_type_node);
1013 /* True iff the string literal INIT has a type suitable for initializing array
1014 TYPE. */
1016 bool
1017 array_string_literal_compatible_p (tree type, tree init)
1019 tree to_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1020 tree from_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
1022 if (to_char_type == from_char_type)
1023 return true;
1024 /* The array element type does not match the initializing string
1025 literal element type; this is only allowed when both types are
1026 ordinary character type. There are no string literals of
1027 signed or unsigned char type in the language, but we can get
1028 them internally from converting braced-init-lists to
1029 STRING_CST. */
1030 if (ordinary_char_type_p (to_char_type)
1031 && ordinary_char_type_p (from_char_type))
1032 return true;
1033 return false;
1036 /* Process the initializer INIT for a variable of type TYPE, emitting
1037 diagnostics for invalid initializers and converting the initializer as
1038 appropriate.
1040 For aggregate types, it assumes that reshape_init has already run, thus the
1041 initializer will have the right shape (brace elision has been undone).
1043 NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR,
1044 2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR. */
1046 static tree
1047 digest_init_r (tree type, tree init, int nested, int flags,
1048 tsubst_flags_t complain)
1050 enum tree_code code = TREE_CODE (type);
1052 if (error_operand_p (init))
1053 return error_mark_node;
1055 gcc_assert (init);
1057 /* We must strip the outermost array type when completing the type,
1058 because the its bounds might be incomplete at the moment. */
1059 if (!complete_type_or_maybe_complain (code == ARRAY_TYPE
1060 ? TREE_TYPE (type) : type, NULL_TREE,
1061 complain))
1062 return error_mark_node;
1064 location_t loc = cp_expr_loc_or_input_loc (init);
1066 tree stripped_init = init;
1068 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1069 && CONSTRUCTOR_IS_PAREN_INIT (init))
1070 flags |= LOOKUP_AGGREGATE_PAREN_INIT;
1072 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
1073 (g++.old-deja/g++.law/casts2.C). */
1074 if (TREE_CODE (init) == NON_LVALUE_EXPR)
1075 stripped_init = TREE_OPERAND (init, 0);
1077 stripped_init = tree_strip_any_location_wrapper (stripped_init);
1079 /* Initialization of an array of chars from a string constant. The initializer
1080 can be optionally enclosed in braces, but reshape_init has already removed
1081 them if they were present. */
1082 if (code == ARRAY_TYPE)
1084 if (nested && !TYPE_DOMAIN (type))
1085 /* C++ flexible array members have a null domain. */
1087 if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT)
1088 pedwarn (loc, OPT_Wpedantic,
1089 "initialization of a flexible array member");
1090 else
1092 if (complain & tf_error)
1093 error_at (loc, "non-static initialization of"
1094 " a flexible array member");
1095 return error_mark_node;
1099 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1100 if (char_type_p (typ1)
1101 && TREE_CODE (stripped_init) == STRING_CST)
1103 if (!array_string_literal_compatible_p (type, init))
1105 if (complain & tf_error)
1106 error_at (loc, "cannot initialize array of %qT from "
1107 "a string literal with type array of %qT",
1108 typ1,
1109 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init))));
1110 return error_mark_node;
1113 if (nested == 2 && !TYPE_DOMAIN (type))
1115 if (complain & tf_error)
1116 error_at (loc, "initialization of flexible array member "
1117 "in a nested context");
1118 return error_mark_node;
1121 if (type != TREE_TYPE (init)
1122 && !variably_modified_type_p (type, NULL_TREE))
1124 init = copy_node (init);
1125 TREE_TYPE (init) = type;
1126 /* If we have a location wrapper, then also copy the wrapped
1127 node, and update the copy's type. */
1128 if (location_wrapper_p (init))
1130 stripped_init = copy_node (stripped_init);
1131 TREE_OPERAND (init, 0) = stripped_init;
1132 TREE_TYPE (stripped_init) = type;
1135 if (TYPE_DOMAIN (type) && TREE_CONSTANT (TYPE_SIZE (type)))
1137 /* Not a flexible array member. */
1138 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1139 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1140 /* In C it is ok to subtract 1 from the length of the string
1141 because it's ok to ignore the terminating null char that is
1142 counted in the length of the constant, but in C++ this would
1143 be invalid. */
1144 if (size < TREE_STRING_LENGTH (stripped_init))
1146 permerror (loc, "initializer-string for %qT is too long",
1147 type);
1149 init = build_string (size,
1150 TREE_STRING_POINTER (stripped_init));
1151 TREE_TYPE (init) = type;
1154 return init;
1158 /* Handle scalar types (including conversions) and references. */
1159 if ((code != COMPLEX_TYPE || BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
1160 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1162 /* Narrowing is OK when initializing an aggregate from
1163 a parenthesized list. */
1164 if (nested && !(flags & LOOKUP_AGGREGATE_PAREN_INIT))
1165 flags |= LOOKUP_NO_NARROWING;
1166 init = convert_for_initialization (0, type, init, flags,
1167 ICR_INIT, NULL_TREE, 0,
1168 complain);
1170 return init;
1173 /* Come here only for aggregates: records, arrays, unions, complex numbers
1174 and vectors. */
1175 gcc_assert (code == ARRAY_TYPE
1176 || VECTOR_TYPE_P (type)
1177 || code == RECORD_TYPE
1178 || code == UNION_TYPE
1179 || code == OPAQUE_TYPE
1180 || code == COMPLEX_TYPE);
1182 /* "If T is a class type and the initializer list has a single
1183 element of type cv U, where U is T or a class derived from T,
1184 the object is initialized from that element." */
1185 if (cxx_dialect >= cxx11
1186 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
1187 && !CONSTRUCTOR_IS_DESIGNATED_INIT (stripped_init)
1188 && CONSTRUCTOR_NELTS (stripped_init) == 1
1189 && ((CLASS_TYPE_P (type) && !CLASSTYPE_NON_AGGREGATE (type))
1190 || VECTOR_TYPE_P (type)))
1192 tree elt = CONSTRUCTOR_ELT (stripped_init, 0)->value;
1193 if (reference_related_p (type, TREE_TYPE (elt)))
1195 /* In C++17, aggregates can have bases, thus participate in
1196 aggregate initialization. In the following case:
1198 struct B { int c; };
1199 struct D : B { };
1200 D d{{D{{42}}}};
1202 there's an extra set of braces, so the D temporary initializes
1203 the first element of d, which is the B base subobject. The base
1204 of type B is copy-initialized from the D temporary, causing
1205 object slicing. */
1206 tree field = next_initializable_field (TYPE_FIELDS (type));
1207 if (field && DECL_FIELD_IS_BASE (field))
1209 if (warning_at (loc, 0, "initializing a base class of type %qT "
1210 "results in object slicing", TREE_TYPE (field)))
1211 inform (loc, "remove %<{ }%> around initializer");
1213 else if (flag_checking)
1214 /* We should have fixed this in reshape_init. */
1215 gcc_unreachable ();
1219 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
1220 && !TYPE_NON_AGGREGATE_CLASS (type))
1221 return process_init_constructor (type, stripped_init, nested, flags,
1222 complain);
1223 else
1225 if (COMPOUND_LITERAL_P (stripped_init) && code == ARRAY_TYPE)
1227 if (complain & tf_error)
1228 error_at (loc, "cannot initialize aggregate of type %qT with "
1229 "a compound literal", type);
1231 return error_mark_node;
1234 if (code == ARRAY_TYPE
1235 && !BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
1237 /* Allow the result of build_array_copy and of
1238 build_value_init_noctor. */
1239 if ((TREE_CODE (stripped_init) == VEC_INIT_EXPR
1240 || TREE_CODE (stripped_init) == CONSTRUCTOR)
1241 && (same_type_ignoring_top_level_qualifiers_p
1242 (type, TREE_TYPE (init))))
1243 return init;
1245 if (complain & tf_error)
1246 error_at (loc, "array must be initialized with a brace-enclosed"
1247 " initializer");
1248 return error_mark_node;
1251 return convert_for_initialization (NULL_TREE, type, init,
1252 flags,
1253 ICR_INIT, NULL_TREE, 0,
1254 complain);
1258 tree
1259 digest_init (tree type, tree init, tsubst_flags_t complain)
1261 return digest_init_r (type, init, 0, LOOKUP_IMPLICIT, complain);
1264 tree
1265 digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain)
1267 return digest_init_r (type, init, 0, flags, complain);
1270 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1271 tree
1272 digest_nsdmi_init (tree decl, tree init, tsubst_flags_t complain)
1274 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1276 tree type = TREE_TYPE (decl);
1277 if (DECL_BIT_FIELD_TYPE (decl))
1278 type = DECL_BIT_FIELD_TYPE (decl);
1279 int flags = LOOKUP_IMPLICIT;
1280 if (DIRECT_LIST_INIT_P (init))
1282 flags = LOOKUP_NORMAL;
1283 complain |= tf_no_cleanup;
1285 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1286 && CP_AGGREGATE_TYPE_P (type))
1287 init = reshape_init (type, init, complain);
1288 init = digest_init_flags (type, init, flags, complain);
1289 return init;
1292 /* Set of flags used within process_init_constructor to describe the
1293 initializers. */
1294 #define PICFLAG_ERRONEOUS 1
1295 #define PICFLAG_NOT_ALL_CONSTANT 2
1296 #define PICFLAG_NOT_ALL_SIMPLE 4
1297 #define PICFLAG_SIDE_EFFECTS 8
1299 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1300 describe it. */
1302 static int
1303 picflag_from_initializer (tree init)
1305 if (init == error_mark_node)
1306 return PICFLAG_ERRONEOUS;
1307 else if (!TREE_CONSTANT (init))
1309 if (TREE_SIDE_EFFECTS (init))
1310 return PICFLAG_SIDE_EFFECTS;
1311 else
1312 return PICFLAG_NOT_ALL_CONSTANT;
1314 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1315 return PICFLAG_NOT_ALL_SIMPLE;
1316 return 0;
1319 /* Adjust INIT for going into a CONSTRUCTOR. */
1321 static tree
1322 massage_init_elt (tree type, tree init, int nested, int flags,
1323 tsubst_flags_t complain)
1325 int new_flags = LOOKUP_IMPLICIT;
1326 if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT)
1327 new_flags |= LOOKUP_ALLOW_FLEXARRAY_INIT;
1328 if (flags & LOOKUP_AGGREGATE_PAREN_INIT)
1329 new_flags |= LOOKUP_AGGREGATE_PAREN_INIT;
1330 init = digest_init_r (type, init, nested ? 2 : 1, new_flags, complain);
1331 /* When we defer constant folding within a statement, we may want to
1332 defer this folding as well. */
1333 tree t = fold_non_dependent_init (init, complain);
1334 if (TREE_CONSTANT (t))
1335 init = t;
1336 return init;
1339 /* Subroutine of process_init_constructor, which will process an initializer
1340 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1341 which describe the initializers. */
1343 static int
1344 process_init_constructor_array (tree type, tree init, int nested, int flags,
1345 tsubst_flags_t complain)
1347 unsigned HOST_WIDE_INT i, len = 0;
1348 int picflags = 0;
1349 bool unbounded = false;
1350 constructor_elt *ce;
1351 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1353 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1354 || VECTOR_TYPE_P (type));
1356 if (TREE_CODE (type) == ARRAY_TYPE)
1358 /* C++ flexible array members have a null domain. */
1359 tree domain = TYPE_DOMAIN (type);
1360 if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1361 len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
1362 - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
1363 TYPE_PRECISION (TREE_TYPE (domain)),
1364 TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
1365 else
1366 unbounded = true; /* Take as many as there are. */
1368 if (nested == 2 && !domain && !vec_safe_is_empty (v))
1370 if (complain & tf_error)
1371 error_at (cp_expr_loc_or_input_loc (init),
1372 "initialization of flexible array member "
1373 "in a nested context");
1374 return PICFLAG_ERRONEOUS;
1377 else
1378 /* Vectors are like simple fixed-size arrays. */
1379 unbounded = !TYPE_VECTOR_SUBPARTS (type).is_constant (&len);
1381 /* There must not be more initializers than needed. */
1382 if (!unbounded && vec_safe_length (v) > len)
1384 if (complain & tf_error)
1385 error ("too many initializers for %qT", type);
1386 else
1387 return PICFLAG_ERRONEOUS;
1390 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1392 if (!ce->index)
1393 ce->index = size_int (i);
1394 else if (!check_array_designated_initializer (ce, i))
1395 ce->index = error_mark_node;
1396 gcc_assert (ce->value);
1397 ce->value
1398 = massage_init_elt (TREE_TYPE (type), ce->value, nested, flags,
1399 complain);
1401 gcc_checking_assert
1402 (ce->value == error_mark_node
1403 || (same_type_ignoring_top_level_qualifiers_p
1404 (strip_array_types (TREE_TYPE (type)),
1405 strip_array_types (TREE_TYPE (ce->value)))));
1407 picflags |= picflag_from_initializer (ce->value);
1410 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1411 we must add initializers ourselves. */
1412 if (!unbounded)
1413 for (; i < len; ++i)
1415 tree next;
1417 if (type_build_ctor_call (TREE_TYPE (type)))
1419 /* If this type needs constructors run for default-initialization,
1420 we can't rely on the back end to do it for us, so make the
1421 initialization explicit by list-initializing from T{}. */
1422 next = build_constructor (init_list_type_node, NULL);
1423 next = massage_init_elt (TREE_TYPE (type), next, nested, flags,
1424 complain);
1425 if (initializer_zerop (next))
1426 /* The default zero-initialization is fine for us; don't
1427 add anything to the CONSTRUCTOR. */
1428 next = NULL_TREE;
1430 else if (!zero_init_p (TREE_TYPE (type)))
1431 next = build_zero_init (TREE_TYPE (type),
1432 /*nelts=*/NULL_TREE,
1433 /*static_storage_p=*/false);
1434 else
1435 /* The default zero-initialization is fine for us; don't
1436 add anything to the CONSTRUCTOR. */
1437 next = NULL_TREE;
1439 if (next)
1441 picflags |= picflag_from_initializer (next);
1442 if (len > i+1
1443 && (initializer_constant_valid_p (next, TREE_TYPE (next))
1444 == null_pointer_node))
1446 tree range = build2 (RANGE_EXPR, size_type_node,
1447 build_int_cst (size_type_node, i),
1448 build_int_cst (size_type_node, len - 1));
1449 CONSTRUCTOR_APPEND_ELT (v, range, next);
1450 break;
1452 else
1453 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1455 else
1456 /* Don't bother checking all the other elements. */
1457 break;
1460 CONSTRUCTOR_ELTS (init) = v;
1461 return picflags;
1464 /* Subroutine of process_init_constructor, which will process an initializer
1465 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1466 the initializers. */
1468 static int
1469 process_init_constructor_record (tree type, tree init, int nested, int flags,
1470 tsubst_flags_t complain)
1472 vec<constructor_elt, va_gc> *v = NULL;
1473 tree field;
1474 int skipped = 0;
1476 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1477 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1478 gcc_assert (!TYPE_BINFO (type)
1479 || cxx_dialect >= cxx17
1480 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1481 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1483 restart:
1484 int picflags = 0;
1485 unsigned HOST_WIDE_INT idx = 0;
1486 int designator_skip = -1;
1487 /* Generally, we will always have an index for each initializer (which is
1488 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1489 reshape_init. So we need to handle both cases. */
1490 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1492 tree next;
1494 if (TREE_CODE (field) != FIELD_DECL
1495 || (DECL_ARTIFICIAL (field)
1496 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))))
1497 continue;
1499 if (DECL_UNNAMED_BIT_FIELD (field))
1500 continue;
1502 /* If this is a bitfield, first convert to the declared type. */
1503 tree fldtype = TREE_TYPE (field);
1504 if (DECL_BIT_FIELD_TYPE (field))
1505 fldtype = DECL_BIT_FIELD_TYPE (field);
1506 if (fldtype == error_mark_node)
1507 return PICFLAG_ERRONEOUS;
1509 next = NULL_TREE;
1510 if (idx < CONSTRUCTOR_NELTS (init))
1512 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1513 if (ce->index)
1515 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1516 latter case can happen in templates where lookup has to be
1517 deferred. */
1518 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1519 || identifier_p (ce->index));
1520 if (ce->index == field || ce->index == DECL_NAME (field))
1521 next = ce->value;
1522 else
1524 ce = NULL;
1525 if (designator_skip == -1)
1526 designator_skip = 1;
1529 else
1531 designator_skip = 0;
1532 next = ce->value;
1535 if (ce)
1537 gcc_assert (ce->value);
1538 next = massage_init_elt (fldtype, next, nested, flags, complain);
1539 ++idx;
1542 if (next == error_mark_node)
1543 /* We skip initializers for empty bases/fields, so skipping an invalid
1544 one could make us accept invalid code. */
1545 return PICFLAG_ERRONEOUS;
1546 else if (next)
1547 /* Already handled above. */;
1548 else if (DECL_INITIAL (field))
1550 if (skipped > 0)
1552 /* We're using an NSDMI past a field with implicit
1553 zero-init. Go back and make it explicit. */
1554 skipped = -1;
1555 vec_safe_truncate (v, 0);
1556 goto restart;
1558 /* C++14 aggregate NSDMI. */
1559 next = get_nsdmi (field, /*ctor*/false, complain);
1560 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
1561 && find_placeholders (next))
1562 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1564 else if (type_build_ctor_call (fldtype))
1566 /* If this type needs constructors run for
1567 default-initialization, we can't rely on the back end to do it
1568 for us, so build up TARGET_EXPRs. If the type in question is
1569 a class, just build one up; if it's an array, recurse. */
1570 next = build_constructor (init_list_type_node, NULL);
1571 next = massage_init_elt (fldtype, next, nested, flags, complain);
1573 /* Warn when some struct elements are implicitly initialized. */
1574 if ((complain & tf_warning)
1575 && !cp_unevaluated_operand
1576 && !EMPTY_CONSTRUCTOR_P (init))
1577 warning (OPT_Wmissing_field_initializers,
1578 "missing initializer for member %qD", field);
1580 else
1582 if (TYPE_REF_P (fldtype))
1584 if (complain & tf_error)
1585 error ("member %qD is uninitialized reference", field);
1586 else
1587 return PICFLAG_ERRONEOUS;
1589 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype))
1591 if (complain & tf_error)
1592 error ("member %qD with uninitialized reference fields", field);
1593 else
1594 return PICFLAG_ERRONEOUS;
1596 /* Do nothing for flexible array members since they need not have any
1597 elements. Don't worry about 'skipped' because a flexarray has to
1598 be the last field. */
1599 else if (TREE_CODE (fldtype) == ARRAY_TYPE && !TYPE_DOMAIN (fldtype))
1600 continue;
1602 /* Warn when some struct elements are implicitly initialized
1603 to zero. */
1604 if ((complain & tf_warning)
1605 && !cp_unevaluated_operand
1606 && !EMPTY_CONSTRUCTOR_P (init))
1607 warning (OPT_Wmissing_field_initializers,
1608 "missing initializer for member %qD", field);
1610 if (!zero_init_p (fldtype) || skipped < 0)
1612 if (TYPE_REF_P (fldtype))
1613 next = build_zero_cst (fldtype);
1614 else
1615 next = build_zero_init (fldtype, /*nelts=*/NULL_TREE,
1616 /*static_storage_p=*/false);
1618 else
1620 /* The default zero-initialization is fine for us; don't
1621 add anything to the CONSTRUCTOR. */
1622 skipped = 1;
1623 continue;
1627 if (is_empty_field (field)
1628 && !TREE_SIDE_EFFECTS (next))
1629 /* Don't add trivial initialization of an empty base/field to the
1630 constructor, as they might not be ordered the way the back-end
1631 expects. */
1632 continue;
1634 /* If this is a bitfield, now convert to the lowered type. */
1635 if (fldtype != TREE_TYPE (field))
1636 next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1637 picflags |= picflag_from_initializer (next);
1638 CONSTRUCTOR_APPEND_ELT (v, field, next);
1641 if (idx < CONSTRUCTOR_NELTS (init))
1643 if (complain & tf_error)
1645 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1646 /* For better diagnostics, try to find out if it is really
1647 the case of too many initializers or if designators are
1648 in incorrect order. */
1649 if (designator_skip == 1 && ce->index)
1651 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1652 || identifier_p (ce->index));
1653 for (field = TYPE_FIELDS (type);
1654 field; field = DECL_CHAIN (field))
1656 if (TREE_CODE (field) != FIELD_DECL
1657 || (DECL_ARTIFICIAL (field)
1658 && !(cxx_dialect >= cxx17
1659 && DECL_FIELD_IS_BASE (field))))
1660 continue;
1662 if (DECL_UNNAMED_BIT_FIELD (field))
1663 continue;
1665 if (ce->index == field || ce->index == DECL_NAME (field))
1666 break;
1669 if (field)
1670 error ("designator order for field %qD does not match declaration "
1671 "order in %qT", field, type);
1672 else
1673 error ("too many initializers for %qT", type);
1675 else
1676 return PICFLAG_ERRONEOUS;
1679 CONSTRUCTOR_ELTS (init) = v;
1680 return picflags;
1683 /* Subroutine of process_init_constructor, which will process a single
1684 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1685 which describe the initializer. */
1687 static int
1688 process_init_constructor_union (tree type, tree init, int nested, int flags,
1689 tsubst_flags_t complain)
1691 constructor_elt *ce;
1692 int len;
1694 /* If the initializer was empty, use the union's NSDMI if it has one.
1695 Otherwise use default zero initialization. */
1696 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1698 for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1700 if (TREE_CODE (field) == FIELD_DECL
1701 && DECL_INITIAL (field) != NULL_TREE)
1703 tree val = get_nsdmi (field, /*in_ctor=*/false, complain);
1704 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
1705 && find_placeholders (val))
1706 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1707 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init), field, val);
1708 break;
1712 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1713 return 0;
1716 len = CONSTRUCTOR_ELTS (init)->length ();
1717 if (len > 1)
1719 if (!(complain & tf_error))
1720 return PICFLAG_ERRONEOUS;
1721 error ("too many initializers for %qT", type);
1722 CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1725 ce = &(*CONSTRUCTOR_ELTS (init))[0];
1727 /* If this element specifies a field, initialize via that field. */
1728 if (ce->index)
1730 if (TREE_CODE (ce->index) == FIELD_DECL)
1732 else if (identifier_p (ce->index))
1734 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1735 tree name = ce->index;
1736 tree field;
1737 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1738 if (DECL_NAME (field) == name)
1739 break;
1740 if (!field)
1742 if (complain & tf_error)
1743 error ("no field %qD found in union being initialized",
1744 field);
1745 ce->value = error_mark_node;
1747 ce->index = field;
1749 else
1751 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1752 || TREE_CODE (ce->index) == RANGE_EXPR);
1753 if (complain & tf_error)
1754 error ("index value instead of field name in union initializer");
1755 ce->value = error_mark_node;
1758 else
1760 /* Find the first named field. ANSI decided in September 1990
1761 that only named fields count here. */
1762 tree field = TYPE_FIELDS (type);
1763 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1764 field = TREE_CHAIN (field);
1765 if (field == NULL_TREE)
1767 if (complain & tf_error)
1768 error ("too many initializers for %qT", type);
1769 ce->value = error_mark_node;
1771 ce->index = field;
1774 if (ce->value && ce->value != error_mark_node)
1775 ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, nested,
1776 flags, complain);
1778 return picflag_from_initializer (ce->value);
1781 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1782 constructor is a brace-enclosed initializer, and will be modified in-place.
1784 Each element is converted to the right type through digest_init, and
1785 missing initializers are added following the language rules (zero-padding,
1786 etc.).
1788 After the execution, the initializer will have TREE_CONSTANT if all elts are
1789 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1790 constants that the assembler and linker can compute them.
1792 The function returns the initializer itself, or error_mark_node in case
1793 of error. */
1795 static tree
1796 process_init_constructor (tree type, tree init, int nested, int flags,
1797 tsubst_flags_t complain)
1799 int picflags;
1801 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1803 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
1804 picflags = process_init_constructor_array (type, init, nested, flags,
1805 complain);
1806 else if (TREE_CODE (type) == RECORD_TYPE)
1807 picflags = process_init_constructor_record (type, init, nested, flags,
1808 complain);
1809 else if (TREE_CODE (type) == UNION_TYPE)
1810 picflags = process_init_constructor_union (type, init, nested, flags,
1811 complain);
1812 else
1813 gcc_unreachable ();
1815 if (picflags & PICFLAG_ERRONEOUS)
1816 return error_mark_node;
1818 TREE_TYPE (init) = type;
1819 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1820 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1821 if (picflags & PICFLAG_SIDE_EFFECTS)
1823 TREE_CONSTANT (init) = false;
1824 TREE_SIDE_EFFECTS (init) = true;
1826 else if (picflags & PICFLAG_NOT_ALL_CONSTANT)
1828 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1829 TREE_CONSTANT (init) = false;
1830 TREE_SIDE_EFFECTS (init) = false;
1832 else
1834 TREE_CONSTANT (init) = 1;
1835 TREE_SIDE_EFFECTS (init) = false;
1836 if (!(picflags & PICFLAG_NOT_ALL_SIMPLE))
1837 TREE_STATIC (init) = 1;
1839 return init;
1842 /* Given a structure or union value DATUM, construct and return
1843 the structure or union component which results from narrowing
1844 that value to the base specified in BASETYPE. For example, given the
1845 hierarchy
1847 class L { int ii; };
1848 class A : L { ... };
1849 class B : L { ... };
1850 class C : A, B { ... };
1852 and the declaration
1854 C x;
1856 then the expression
1858 x.A::ii refers to the ii member of the L part of
1859 the A part of the C object named by X. In this case,
1860 DATUM would be x, and BASETYPE would be A.
1862 I used to think that this was nonconformant, that the standard specified
1863 that first we look up ii in A, then convert x to an L& and pull out the
1864 ii part. But in fact, it does say that we convert x to an A&; A here
1865 is known as the "naming class". (jason 2000-12-19)
1867 BINFO_P points to a variable initialized either to NULL_TREE or to the
1868 binfo for the specific base subobject we want to convert to. */
1870 tree
1871 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1873 tree binfo;
1875 if (datum == error_mark_node)
1876 return error_mark_node;
1877 if (*binfo_p)
1878 binfo = *binfo_p;
1879 else
1880 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
1881 NULL, tf_warning_or_error);
1883 if (!binfo || binfo == error_mark_node)
1885 *binfo_p = NULL_TREE;
1886 if (!binfo)
1887 error_not_base_type (basetype, TREE_TYPE (datum));
1888 return error_mark_node;
1891 *binfo_p = binfo;
1892 return build_base_path (PLUS_EXPR, datum, binfo, 1,
1893 tf_warning_or_error);
1896 /* Build a reference to an object specified by the C++ `->' operator.
1897 Usually this just involves dereferencing the object, but if the
1898 `->' operator is overloaded, then such overloads must be
1899 performed until an object which does not have the `->' operator
1900 overloaded is found. An error is reported when circular pointer
1901 delegation is detected. */
1903 tree
1904 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
1906 tree orig_expr = expr;
1907 tree type = TREE_TYPE (expr);
1908 tree last_rval = NULL_TREE;
1909 vec<tree, va_gc> *types_memoized = NULL;
1911 if (type == error_mark_node)
1912 return error_mark_node;
1914 if (processing_template_decl)
1916 if (type && TYPE_PTR_P (type)
1917 && !dependent_scope_p (TREE_TYPE (type)))
1918 /* Pointer to current instantiation, don't treat as dependent. */;
1919 else if (type_dependent_expression_p (expr))
1920 return build_min_nt_loc (loc, ARROW_EXPR, expr);
1921 expr = build_non_dependent_expr (expr);
1924 if (MAYBE_CLASS_TYPE_P (type))
1926 struct tinst_level *actual_inst = current_instantiation ();
1927 tree fn = NULL;
1929 while ((expr = build_new_op (loc, COMPONENT_REF,
1930 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
1931 &fn, complain)))
1933 if (expr == error_mark_node)
1934 return error_mark_node;
1936 /* This provides a better instantiation backtrace in case of
1937 error. */
1938 if (fn && DECL_USE_TEMPLATE (fn))
1939 push_tinst_level_loc (fn,
1940 (current_instantiation () != actual_inst)
1941 ? DECL_SOURCE_LOCATION (fn)
1942 : input_location);
1943 fn = NULL;
1945 if (vec_member (TREE_TYPE (expr), types_memoized))
1947 if (complain & tf_error)
1948 error ("circular pointer delegation detected");
1949 return error_mark_node;
1952 vec_safe_push (types_memoized, TREE_TYPE (expr));
1953 last_rval = expr;
1956 while (current_instantiation () != actual_inst)
1957 pop_tinst_level ();
1959 if (last_rval == NULL_TREE)
1961 if (complain & tf_error)
1962 error ("base operand of %<->%> has non-pointer type %qT", type);
1963 return error_mark_node;
1966 if (TYPE_REF_P (TREE_TYPE (last_rval)))
1967 last_rval = convert_from_reference (last_rval);
1969 else
1971 last_rval = decay_conversion (expr, complain);
1972 if (last_rval == error_mark_node)
1973 return error_mark_node;
1976 if (TYPE_PTR_P (TREE_TYPE (last_rval)))
1978 if (processing_template_decl)
1980 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
1981 orig_expr);
1982 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
1983 return expr;
1986 return cp_build_indirect_ref (loc, last_rval, RO_ARROW, complain);
1989 if (complain & tf_error)
1991 if (types_memoized)
1992 error ("result of %<operator->()%> yields non-pointer result");
1993 else
1994 error ("base operand of %<->%> is not a pointer");
1996 return error_mark_node;
1999 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
2000 already been checked out to be of aggregate type. */
2002 tree
2003 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
2005 tree ptrmem_type;
2006 tree objtype;
2007 tree type;
2008 tree binfo;
2009 tree ctype;
2011 datum = mark_lvalue_use (datum);
2012 component = mark_rvalue_use (component);
2014 if (error_operand_p (datum) || error_operand_p (component))
2015 return error_mark_node;
2017 ptrmem_type = TREE_TYPE (component);
2018 if (!TYPE_PTRMEM_P (ptrmem_type))
2020 if (complain & tf_error)
2021 error ("%qE cannot be used as a member pointer, since it is of "
2022 "type %qT", component, ptrmem_type);
2023 return error_mark_node;
2026 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2027 if (! MAYBE_CLASS_TYPE_P (objtype))
2029 if (complain & tf_error)
2030 error ("cannot apply member pointer %qE to %qE, which is of "
2031 "non-class type %qT", component, datum, objtype);
2032 return error_mark_node;
2035 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
2036 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
2038 if (!COMPLETE_TYPE_P (ctype))
2040 if (!same_type_p (ctype, objtype))
2041 goto mismatch;
2042 binfo = NULL;
2044 else
2046 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
2048 if (!binfo)
2050 mismatch:
2051 if (complain & tf_error)
2052 error ("pointer to member type %qT incompatible with object "
2053 "type %qT", type, objtype);
2054 return error_mark_node;
2056 else if (binfo == error_mark_node)
2057 return error_mark_node;
2060 if (TYPE_PTRDATAMEM_P (ptrmem_type))
2062 bool is_lval = real_lvalue_p (datum);
2063 tree ptype;
2065 /* Compute the type of the field, as described in [expr.ref].
2066 There's no such thing as a mutable pointer-to-member, so
2067 things are not as complex as they are for references to
2068 non-static data members. */
2069 type = cp_build_qualified_type (type,
2070 (cp_type_quals (type)
2071 | cp_type_quals (TREE_TYPE (datum))));
2073 datum = build_address (datum);
2075 /* Convert object to the correct base. */
2076 if (binfo)
2078 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
2079 if (datum == error_mark_node)
2080 return error_mark_node;
2083 /* Build an expression for "object + offset" where offset is the
2084 value stored in the pointer-to-data-member. */
2085 ptype = build_pointer_type (type);
2086 datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
2087 datum = cp_build_fold_indirect_ref (datum);
2088 if (datum == error_mark_node)
2089 return error_mark_node;
2091 /* If the object expression was an rvalue, return an rvalue. */
2092 if (!is_lval)
2093 datum = move (datum);
2094 return datum;
2096 else
2098 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
2099 program is ill-formed if the second operand is a pointer to member
2100 function with ref-qualifier & (for C++20: unless its cv-qualifier-seq
2101 is const). In a .* expression whose object expression is an lvalue,
2102 the program is ill-formed if the second operand is a pointer to member
2103 function with ref-qualifier &&. */
2104 if (FUNCTION_REF_QUALIFIED (type))
2106 bool lval = lvalue_p (datum);
2107 if (lval && FUNCTION_RVALUE_QUALIFIED (type))
2109 if (complain & tf_error)
2110 error ("pointer-to-member-function type %qT requires an rvalue",
2111 ptrmem_type);
2112 return error_mark_node;
2114 else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
2116 if ((type_memfn_quals (type)
2117 & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))
2118 != TYPE_QUAL_CONST)
2120 if (complain & tf_error)
2121 error ("pointer-to-member-function type %qT requires "
2122 "an lvalue", ptrmem_type);
2123 return error_mark_node;
2125 else if (cxx_dialect < cxx20)
2127 if (complain & tf_warning_or_error)
2128 pedwarn (input_location, OPT_Wpedantic,
2129 "pointer-to-member-function type %qT requires "
2130 "an lvalue before C++20", ptrmem_type);
2131 else
2132 return error_mark_node;
2136 return build2 (OFFSET_REF, type, datum, component);
2140 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
2142 static tree
2143 build_functional_cast_1 (location_t loc, tree exp, tree parms,
2144 tsubst_flags_t complain)
2146 /* This is either a call to a constructor,
2147 or a C cast in C++'s `functional' notation. */
2149 /* The type to which we are casting. */
2150 tree type;
2152 if (error_operand_p (exp) || parms == error_mark_node)
2153 return error_mark_node;
2155 if (TREE_CODE (exp) == TYPE_DECL)
2157 type = TREE_TYPE (exp);
2159 if (DECL_ARTIFICIAL (exp))
2160 cp_warn_deprecated_use (type);
2162 else
2163 type = exp;
2165 /* We need to check this explicitly, since value-initialization of
2166 arrays is allowed in other situations. */
2167 if (TREE_CODE (type) == ARRAY_TYPE)
2169 if (complain & tf_error)
2170 error_at (loc, "functional cast to array type %qT", type);
2171 return error_mark_node;
2174 if (tree anode = type_uses_auto (type))
2176 if (!CLASS_PLACEHOLDER_TEMPLATE (anode))
2178 if (complain & tf_error)
2179 error_at (loc, "invalid use of %qT", anode);
2180 return error_mark_node;
2182 else
2184 type = do_auto_deduction (type, parms, anode, complain,
2185 adc_variable_type);
2186 if (type == error_mark_node)
2187 return error_mark_node;
2191 if (processing_template_decl)
2193 tree t;
2195 /* Diagnose this even in a template. We could also try harder
2196 to give all the usual errors when the type and args are
2197 non-dependent... */
2198 if (TYPE_REF_P (type) && !parms)
2200 if (complain & tf_error)
2201 error_at (loc, "invalid value-initialization of reference type");
2202 return error_mark_node;
2205 t = build_min (CAST_EXPR, type, parms);
2206 /* We don't know if it will or will not have side effects. */
2207 TREE_SIDE_EFFECTS (t) = 1;
2208 return t;
2211 if (! MAYBE_CLASS_TYPE_P (type))
2213 if (parms == NULL_TREE)
2215 if (VOID_TYPE_P (type))
2216 return void_node;
2217 return build_value_init (cv_unqualified (type), complain);
2220 /* This must build a C cast. */
2221 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
2222 return cp_build_c_cast (loc, type, parms, complain);
2225 /* Prepare to evaluate as a call to a constructor. If this expression
2226 is actually used, for example,
2228 return X (arg1, arg2, ...);
2230 then the slot being initialized will be filled in. */
2232 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
2233 return error_mark_node;
2234 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
2235 return error_mark_node;
2237 /* [expr.type.conv]
2239 If the expression list is a single-expression, the type
2240 conversion is equivalent (in definedness, and if defined in
2241 meaning) to the corresponding cast expression. */
2242 if (parms && TREE_CHAIN (parms) == NULL_TREE)
2243 return cp_build_c_cast (loc, type, TREE_VALUE (parms), complain);
2245 /* [expr.type.conv]
2247 The expression T(), where T is a simple-type-specifier for a
2248 non-array complete object type or the (possibly cv-qualified)
2249 void type, creates an rvalue of the specified type, which is
2250 value-initialized. */
2252 if (parms == NULL_TREE)
2254 exp = build_value_init (type, complain);
2255 exp = get_target_expr_sfinae (exp, complain);
2256 return exp;
2259 /* Call the constructor. */
2260 releasing_vec parmvec;
2261 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
2262 vec_safe_push (parmvec, TREE_VALUE (parms));
2263 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2264 &parmvec, type, LOOKUP_NORMAL, complain);
2266 if (exp == error_mark_node)
2267 return error_mark_node;
2269 return build_cplus_new (type, exp, complain);
2272 tree
2273 build_functional_cast (location_t loc, tree exp, tree parms,
2274 tsubst_flags_t complain)
2276 tree result = build_functional_cast_1 (loc, exp, parms, complain);
2277 protected_set_expr_location (result, loc);
2278 return result;
2282 /* Add new exception specifier SPEC, to the LIST we currently have.
2283 If it's already in LIST then do nothing.
2284 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2285 know what we're doing. */
2287 tree
2288 add_exception_specifier (tree list, tree spec, tsubst_flags_t complain)
2290 bool ok;
2291 tree core = spec;
2292 bool is_ptr;
2293 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
2295 if (spec == error_mark_node)
2296 return list;
2298 gcc_assert (spec && (!list || TREE_VALUE (list)));
2300 /* [except.spec] 1, type in an exception specifier shall not be
2301 incomplete, or pointer or ref to incomplete other than pointer
2302 to cv void. */
2303 is_ptr = TYPE_PTR_P (core);
2304 if (is_ptr || TYPE_REF_P (core))
2305 core = TREE_TYPE (core);
2306 if (complain < 0)
2307 ok = true;
2308 else if (VOID_TYPE_P (core))
2309 ok = is_ptr;
2310 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
2311 ok = true;
2312 else if (processing_template_decl)
2313 ok = true;
2314 else if (!verify_type_context (input_location, TCTX_EXCEPTIONS, core,
2315 !(complain & tf_error)))
2316 return error_mark_node;
2317 else
2319 ok = true;
2320 /* 15.4/1 says that types in an exception specifier must be complete,
2321 but it seems more reasonable to only require this on definitions
2322 and calls. So just give a pedwarn at this point; we will give an
2323 error later if we hit one of those two cases. */
2324 if (!COMPLETE_TYPE_P (complete_type (core)))
2325 diag_type = DK_PEDWARN; /* pedwarn */
2328 if (ok)
2330 tree probe;
2332 for (probe = list; probe; probe = TREE_CHAIN (probe))
2333 if (same_type_p (TREE_VALUE (probe), spec))
2334 break;
2335 if (!probe)
2336 list = tree_cons (NULL_TREE, spec, list);
2338 else
2339 diag_type = DK_ERROR; /* error */
2341 if (diag_type != DK_UNSPECIFIED
2342 && (complain & tf_warning_or_error))
2343 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
2345 return list;
2348 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2350 static bool
2351 nothrow_spec_p_uninst (const_tree spec)
2353 if (DEFERRED_NOEXCEPT_SPEC_P (spec))
2354 return false;
2355 return nothrow_spec_p (spec);
2358 /* Combine the two exceptions specifier lists LIST and ADD, and return
2359 their union. */
2361 tree
2362 merge_exception_specifiers (tree list, tree add)
2364 tree noex, orig_list;
2366 if (list == error_mark_node || add == error_mark_node)
2367 return error_mark_node;
2369 /* No exception-specifier or noexcept(false) are less strict than
2370 anything else. Prefer the newer variant (LIST). */
2371 if (!list || list == noexcept_false_spec)
2372 return list;
2373 else if (!add || add == noexcept_false_spec)
2374 return add;
2376 /* noexcept(true) and throw() are stricter than anything else.
2377 As above, prefer the more recent one (LIST). */
2378 if (nothrow_spec_p_uninst (add))
2379 return list;
2381 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2382 if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
2383 && UNEVALUATED_NOEXCEPT_SPEC_P (list))
2384 return list;
2385 /* We should have instantiated other deferred noexcept specs by now. */
2386 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
2388 if (nothrow_spec_p_uninst (list))
2389 return add;
2390 noex = TREE_PURPOSE (list);
2391 gcc_checking_assert (!TREE_PURPOSE (add)
2392 || errorcount || !flag_exceptions
2393 || cp_tree_equal (noex, TREE_PURPOSE (add)));
2395 /* Combine the dynamic-exception-specifiers, if any. */
2396 orig_list = list;
2397 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
2399 tree spec = TREE_VALUE (add);
2400 tree probe;
2402 for (probe = orig_list; probe && TREE_VALUE (probe);
2403 probe = TREE_CHAIN (probe))
2404 if (same_type_p (TREE_VALUE (probe), spec))
2405 break;
2406 if (!probe)
2408 spec = build_tree_list (NULL_TREE, spec);
2409 TREE_CHAIN (spec) = list;
2410 list = spec;
2414 /* Keep the noexcept-specifier at the beginning of the list. */
2415 if (noex != TREE_PURPOSE (list))
2416 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2418 return list;
2421 /* Subroutine of build_call. Ensure that each of the types in the
2422 exception specification is complete. Technically, 15.4/1 says that
2423 they need to be complete when we see a declaration of the function,
2424 but we should be able to get away with only requiring this when the
2425 function is defined or called. See also add_exception_specifier. */
2427 void
2428 require_complete_eh_spec_types (tree fntype, tree decl)
2430 tree raises;
2431 /* Don't complain about calls to op new. */
2432 if (decl && DECL_ARTIFICIAL (decl))
2433 return;
2434 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2435 raises = TREE_CHAIN (raises))
2437 tree type = TREE_VALUE (raises);
2438 if (type && !COMPLETE_TYPE_P (type))
2440 if (decl)
2441 error
2442 ("call to function %qD which throws incomplete type %q#T",
2443 decl, type);
2444 else
2445 error ("call to function which throws incomplete type %q#T",
2446 decl);