Fix typo in t-dimode
[official-gcc.git] / gcc / cp / typeck2.c
blob3fb651a02ba1e2eb2b46b8cddc3331ed40d5c28d
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 if (!TYPE_DOMAIN (type)
488 && TREE_CODE (init) == CONSTRUCTOR
489 && CONSTRUCTOR_NELTS (init))
491 /* Flexible array. */
492 cp_complete_array_type (&type, init, /*default*/true);
493 dest = build1 (VIEW_CONVERT_EXPR, type, dest);
496 /* For an array, we only need/want a single cleanup region rather
497 than one per element. */
498 tree code = build_vec_init (dest, NULL_TREE, init, false, 1,
499 tf_warning_or_error);
500 add_stmt (code);
501 if (nested)
502 /* Also clean up the whole array if something later in an enclosing
503 init-list throws. */
504 if (tree cleanup = cxx_maybe_build_cleanup (dest,
505 tf_warning_or_error))
506 finish_eh_cleanup (cleanup);
507 return true;
509 /* FALLTHRU */
511 case RECORD_TYPE:
512 case UNION_TYPE:
513 case QUAL_UNION_TYPE:
514 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
515 field_index, value)
517 /* The current implementation of this algorithm assumes that
518 the field was set for all the elements. This is usually done
519 by process_init_constructor. */
520 gcc_assert (field_index);
522 if (!array_type_p)
523 inner_type = TREE_TYPE (field_index);
525 if (TREE_CODE (value) == CONSTRUCTOR)
527 tree sub;
529 if (array_type_p)
530 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
531 NULL_TREE, NULL_TREE);
532 else
533 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
534 NULL_TREE);
536 if (!split_nonconstant_init_1 (sub, value, true)
537 /* For flexible array member with initializer we
538 can't remove the initializer, because only the
539 initializer determines how many elements the
540 flexible array member has. */
541 || (!array_type_p
542 && TREE_CODE (inner_type) == ARRAY_TYPE
543 && TYPE_DOMAIN (inner_type) == NULL
544 && TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
545 && COMPLETE_TYPE_P (TREE_TYPE (value))
546 && !integer_zerop (TYPE_SIZE (TREE_TYPE (value)))
547 && idx == CONSTRUCTOR_NELTS (init) - 1
548 && TYPE_HAS_TRIVIAL_DESTRUCTOR
549 (strip_array_types (inner_type))))
550 complete_p = false;
551 else
553 /* Mark element for removal. */
554 CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE;
555 if (idx < tidx)
556 tidx = idx;
557 num_split_elts++;
560 else if (!initializer_constant_valid_p (value, inner_type))
562 tree code;
563 tree sub;
565 /* Mark element for removal. */
566 CONSTRUCTOR_ELT (init, idx)->index = NULL_TREE;
567 if (idx < tidx)
568 tidx = idx;
570 if (TREE_CODE (field_index) == RANGE_EXPR)
572 /* Use build_vec_init to initialize a range. */
573 tree low = TREE_OPERAND (field_index, 0);
574 tree hi = TREE_OPERAND (field_index, 1);
575 sub = build4 (ARRAY_REF, inner_type, dest, low,
576 NULL_TREE, NULL_TREE);
577 sub = cp_build_addr_expr (sub, tf_warning_or_error);
578 tree max = size_binop (MINUS_EXPR, hi, low);
579 code = build_vec_init (sub, max, value, false, 0,
580 tf_warning_or_error);
581 add_stmt (code);
582 if (tree_fits_shwi_p (max))
583 num_split_elts += tree_to_shwi (max);
585 else
587 if (array_type_p)
588 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
589 NULL_TREE, NULL_TREE);
590 else
591 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
592 NULL_TREE);
594 /* We may need to add a copy constructor call if
595 the field has [[no_unique_address]]. */
596 if (unsafe_return_slot_p (sub))
598 /* But not if the initializer is an implicit ctor call
599 we just built in digest_init. */
600 if (TREE_CODE (value) == TARGET_EXPR
601 && TARGET_EXPR_LIST_INIT_P (value)
602 && make_safe_copy_elision (sub, value))
603 goto build_init;
605 tree name = (DECL_FIELD_IS_BASE (field_index)
606 ? base_ctor_identifier
607 : complete_ctor_identifier);
608 releasing_vec args = make_tree_vector_single (value);
609 code = build_special_member_call
610 (sub, name, &args, inner_type,
611 LOOKUP_NORMAL, tf_warning_or_error);
613 else
615 build_init:
616 code = build2 (INIT_EXPR, inner_type, sub, value);
618 code = build_stmt (input_location, EXPR_STMT, code);
619 code = maybe_cleanup_point_expr_void (code);
620 add_stmt (code);
621 if (tree cleanup
622 = cxx_maybe_build_cleanup (sub, tf_warning_or_error))
623 finish_eh_cleanup (cleanup);
626 num_split_elts++;
629 if (num_split_elts == 1)
630 CONSTRUCTOR_ELTS (init)->ordered_remove (tidx);
631 else if (num_split_elts > 1)
633 /* Perform the delayed ordered removal of non-constant elements
634 we split out. */
635 for (idx = tidx; idx < CONSTRUCTOR_NELTS (init); ++idx)
636 if (CONSTRUCTOR_ELT (init, idx)->index == NULL_TREE)
638 else
640 *CONSTRUCTOR_ELT (init, tidx) = *CONSTRUCTOR_ELT (init, idx);
641 ++tidx;
643 vec_safe_truncate (CONSTRUCTOR_ELTS (init), tidx);
645 break;
647 case VECTOR_TYPE:
648 if (!initializer_constant_valid_p (init, type))
650 tree code;
651 tree cons = copy_node (init);
652 CONSTRUCTOR_ELTS (init) = NULL;
653 code = build2 (MODIFY_EXPR, type, dest, cons);
654 code = build_stmt (input_location, EXPR_STMT, code);
655 add_stmt (code);
656 num_split_elts += CONSTRUCTOR_NELTS (init);
658 break;
660 default:
661 gcc_unreachable ();
664 /* The rest of the initializer is now a constant. */
665 TREE_CONSTANT (init) = 1;
666 TREE_SIDE_EFFECTS (init) = 0;
668 /* We didn't split out anything. */
669 if (num_split_elts == 0)
670 return false;
672 return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
673 num_split_elts, inner_type);
676 /* A subroutine of store_init_value. Splits non-constant static
677 initializer INIT into a constant part and generates code to
678 perform the non-constant part of the initialization to DEST.
679 Returns the code for the runtime init. */
681 tree
682 split_nonconstant_init (tree dest, tree init)
684 tree code;
686 if (TREE_CODE (init) == TARGET_EXPR)
687 init = TARGET_EXPR_INITIAL (init);
688 if (TREE_CODE (init) == CONSTRUCTOR)
690 init = cp_fully_fold_init (init);
691 code = push_stmt_list ();
692 if (split_nonconstant_init_1 (dest, init, false))
693 init = NULL_TREE;
694 code = pop_stmt_list (code);
695 if (VAR_P (dest) && !is_local_temp (dest))
697 DECL_INITIAL (dest) = init;
698 TREE_READONLY (dest) = 0;
700 else if (init)
702 tree ie = build2 (INIT_EXPR, void_type_node, dest, init);
703 code = add_stmt_to_compound (ie, code);
706 else if (TREE_CODE (init) == STRING_CST
707 && array_of_runtime_bound_p (TREE_TYPE (dest)))
708 code = build_vec_init (dest, NULL_TREE, init, /*value-init*/false,
709 /*from array*/1, tf_warning_or_error);
710 else
711 code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
713 return code;
716 /* Perform appropriate conversions on the initial value of a variable,
717 store it in the declaration DECL,
718 and print any error messages that are appropriate.
719 If the init is invalid, store an ERROR_MARK.
721 C++: Note that INIT might be a TREE_LIST, which would mean that it is
722 a base class initializer for some aggregate type, hopefully compatible
723 with DECL. If INIT is a single element, and DECL is an aggregate
724 type, we silently convert INIT into a TREE_LIST, allowing a constructor
725 to be called.
727 If INIT is a TREE_LIST and there is no constructor, turn INIT
728 into a CONSTRUCTOR and use standard initialization techniques.
729 Perhaps a warning should be generated?
731 Returns code to be executed if initialization could not be performed
732 for static variable. In that case, caller must emit the code. */
734 tree
735 store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
737 tree value, type;
739 /* If variable's type was invalidly declared, just ignore it. */
741 type = TREE_TYPE (decl);
742 if (TREE_CODE (type) == ERROR_MARK)
743 return NULL_TREE;
745 if (MAYBE_CLASS_TYPE_P (type))
747 if (TREE_CODE (init) == TREE_LIST)
749 error ("constructor syntax used, but no constructor declared "
750 "for type %qT", type);
751 init = build_constructor_from_list (init_list_type_node, nreverse (init));
755 /* End of special C++ code. */
757 if (flags & LOOKUP_ALREADY_DIGESTED)
758 value = init;
759 else
761 if (TREE_STATIC (decl))
762 flags |= LOOKUP_ALLOW_FLEXARRAY_INIT;
763 /* Digest the specified initializer into an expression. */
764 value = digest_init_flags (type, init, flags, tf_warning_or_error);
767 /* Look for braced array initializers for character arrays and
768 recursively convert them into STRING_CSTs. */
769 value = braced_lists_to_strings (type, value);
771 current_ref_temp_count = 0;
772 value = extend_ref_init_temps (decl, value, cleanups);
774 /* In C++11 constant expression is a semantic, not syntactic, property.
775 In C++98, make sure that what we thought was a constant expression at
776 template definition time is still constant and otherwise perform this
777 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
778 if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
780 bool const_init;
781 tree oldval = value;
782 if (DECL_DECLARED_CONSTEXPR_P (decl)
783 || (DECL_IN_AGGR_P (decl)
784 && DECL_INITIALIZED_IN_CLASS_P (decl)))
786 value = fold_non_dependent_expr (value, tf_warning_or_error,
787 /*manifestly_const_eval=*/true,
788 decl);
789 /* Diagnose a non-constant initializer for constexpr variable or
790 non-inline in-class-initialized static data member. */
791 if (!require_constant_expression (value))
792 value = error_mark_node;
793 else if (processing_template_decl)
794 /* In a template we might not have done the necessary
795 transformations to make value actually constant,
796 e.g. extend_ref_init_temps. */
797 value = maybe_constant_init (value, decl, true);
798 else
799 value = cxx_constant_init (value, decl);
801 else
802 value = fold_non_dependent_init (value, tf_warning_or_error,
803 /*manifestly_const_eval=*/true, decl);
804 if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type))
805 /* Poison this CONSTRUCTOR so it can't be copied to another
806 constexpr variable. */
807 CONSTRUCTOR_MUTABLE_POISON (value) = true;
808 const_init = (reduced_constant_expression_p (value)
809 || error_operand_p (value));
810 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
811 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
812 if (!TYPE_REF_P (type))
813 TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
814 if (!const_init)
816 /* [dcl.constinit]/2 "If a variable declared with the constinit
817 specifier has dynamic initialization, the program is
818 ill-formed." */
819 if (DECL_DECLARED_CONSTINIT_P (decl))
821 error_at (location_of (decl),
822 "%<constinit%> variable %qD does not have a constant "
823 "initializer", decl);
824 if (require_constant_expression (value))
825 cxx_constant_init (value, decl);
826 value = error_mark_node;
828 else
829 value = oldval;
832 /* Don't fold initializers of automatic variables in constexpr functions,
833 that might fold away something that needs to be diagnosed at constexpr
834 evaluation time. */
835 if (!current_function_decl
836 || !DECL_DECLARED_CONSTEXPR_P (current_function_decl)
837 || TREE_STATIC (decl))
838 value = cp_fully_fold_init (value);
840 /* Handle aggregate NSDMI in non-constant initializers, too. */
841 value = replace_placeholders (value, decl);
843 /* A COMPOUND_LITERAL_P CONSTRUCTOR is the syntactic form; by the time we get
844 here it should have been digested into an actual value for the type. */
845 gcc_checking_assert (TREE_CODE (value) != CONSTRUCTOR
846 || processing_template_decl
847 || !TREE_HAS_CONSTRUCTOR (value));
849 /* If the initializer is not a constant, fill in DECL_INITIAL with
850 the bits that are constant, and then return an expression that
851 will perform the dynamic initialization. */
852 if (value != error_mark_node
853 && !processing_template_decl
854 && (TREE_SIDE_EFFECTS (value)
855 || vla_type_p (type)
856 || ! reduced_constant_expression_p (value)))
857 return split_nonconstant_init (decl, value);
859 /* DECL may change value; purge caches. */
860 clear_cv_and_fold_caches ();
862 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
863 is an automatic variable, the middle end will turn this into a
864 dynamic initialization later. */
865 DECL_INITIAL (decl) = value;
866 return NULL_TREE;
870 /* Give diagnostic about narrowing conversions within { }, or as part of
871 a converted constant expression. If CONST_ONLY, only check
872 constants. */
874 bool
875 check_narrowing (tree type, tree init, tsubst_flags_t complain,
876 bool const_only/*= false*/)
878 tree ftype = unlowered_expr_type (init);
879 bool ok = true;
880 REAL_VALUE_TYPE d;
882 if (((!warn_narrowing || !(complain & tf_warning))
883 && cxx_dialect == cxx98)
884 || !ARITHMETIC_TYPE_P (type)
885 /* Don't emit bogus warnings with e.g. value-dependent trees. */
886 || instantiation_dependent_expression_p (init))
887 return ok;
889 if (BRACE_ENCLOSED_INITIALIZER_P (init)
890 && TREE_CODE (type) == COMPLEX_TYPE)
892 tree elttype = TREE_TYPE (type);
893 if (CONSTRUCTOR_NELTS (init) > 0)
894 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value,
895 complain);
896 if (CONSTRUCTOR_NELTS (init) > 1)
897 ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value,
898 complain);
899 return ok;
902 /* Even non-dependent expressions can still have template
903 codes like CAST_EXPR, so use *_non_dependent_expr to cope. */
904 init = fold_non_dependent_expr (init, complain, /*manifest*/true);
905 if (init == error_mark_node)
906 return ok;
908 /* If we were asked to only check constants, return early. */
909 if (const_only && !TREE_CONSTANT (init))
910 return ok;
912 if (CP_INTEGRAL_TYPE_P (type)
913 && TREE_CODE (ftype) == REAL_TYPE)
914 ok = false;
915 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
916 && CP_INTEGRAL_TYPE_P (type))
918 if (TREE_CODE (ftype) == ENUMERAL_TYPE)
919 /* Check for narrowing based on the values of the enumeration. */
920 ftype = ENUM_UNDERLYING_TYPE (ftype);
921 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
922 TYPE_MAX_VALUE (ftype))
923 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
924 TYPE_MIN_VALUE (type)))
925 && (TREE_CODE (init) != INTEGER_CST
926 || !int_fits_type_p (init, type)))
927 ok = false;
929 /* [dcl.init.list]#7.2: "from long double to double or float, or from
930 double to float". */
931 else if (TREE_CODE (ftype) == REAL_TYPE
932 && TREE_CODE (type) == REAL_TYPE)
934 if ((same_type_p (ftype, long_double_type_node)
935 && (same_type_p (type, double_type_node)
936 || same_type_p (type, float_type_node)))
937 || (same_type_p (ftype, double_type_node)
938 && same_type_p (type, float_type_node))
939 || (TYPE_PRECISION (type) < TYPE_PRECISION (ftype)))
941 if (TREE_CODE (init) == REAL_CST)
943 /* Issue 703: Loss of precision is OK as long as the value is
944 within the representable range of the new type. */
945 REAL_VALUE_TYPE r;
946 d = TREE_REAL_CST (init);
947 real_convert (&r, TYPE_MODE (type), &d);
948 if (real_isinf (&r))
949 ok = false;
951 else
952 ok = false;
955 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
956 && TREE_CODE (type) == REAL_TYPE)
958 ok = false;
959 if (TREE_CODE (init) == INTEGER_CST)
961 d = real_value_from_int_cst (0, init);
962 if (exact_real_truncate (TYPE_MODE (type), &d))
963 ok = true;
966 else if (TREE_CODE (type) == BOOLEAN_TYPE
967 && (TYPE_PTR_P (ftype) || TYPE_PTRMEM_P (ftype)))
968 /* C++20 P1957R2: converting from a pointer type or a pointer-to-member
969 type to bool should be considered narrowing. This is a DR so is not
970 limited to C++20 only. */
971 ok = false;
973 bool almost_ok = ok;
974 if (!ok && !CONSTANT_CLASS_P (init) && (complain & tf_warning_or_error))
976 tree folded = cp_fully_fold (init);
977 if (TREE_CONSTANT (folded) && check_narrowing (type, folded, tf_none))
978 almost_ok = true;
981 if (!ok)
983 location_t loc = cp_expr_loc_or_input_loc (init);
984 if (cxx_dialect == cxx98)
986 if (complain & tf_warning)
987 warning_at (loc, OPT_Wnarrowing, "narrowing conversion of %qE "
988 "from %qH to %qI is ill-formed in C++11",
989 init, ftype, type);
990 ok = true;
992 else if (!CONSTANT_CLASS_P (init))
994 if (complain & tf_warning_or_error)
996 auto_diagnostic_group d;
997 if ((!almost_ok || pedantic)
998 && pedwarn (loc, OPT_Wnarrowing,
999 "narrowing conversion of %qE from %qH to %qI",
1000 init, ftype, type)
1001 && almost_ok)
1002 inform (loc, " the expression has a constant value but is not "
1003 "a C++ constant-expression");
1004 ok = true;
1007 else if (complain & tf_error)
1009 int savederrorcount = errorcount;
1010 global_dc->pedantic_errors = 1;
1011 auto s = make_temp_override (global_dc->dc_warn_system_headers, true);
1012 pedwarn (loc, OPT_Wnarrowing,
1013 "narrowing conversion of %qE from %qH to %qI",
1014 init, ftype, type);
1015 if (errorcount == savederrorcount)
1016 ok = true;
1017 global_dc->pedantic_errors = flag_pedantic_errors;
1021 return ok;
1024 /* True iff TYPE is a C++20 "ordinary" character type. */
1026 bool
1027 ordinary_char_type_p (tree type)
1029 type = TYPE_MAIN_VARIANT (type);
1030 return (type == char_type_node
1031 || type == signed_char_type_node
1032 || type == unsigned_char_type_node);
1035 /* True iff the string literal INIT has a type suitable for initializing array
1036 TYPE. */
1038 bool
1039 array_string_literal_compatible_p (tree type, tree init)
1041 tree to_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1042 tree from_char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
1044 if (to_char_type == from_char_type)
1045 return true;
1046 /* The array element type does not match the initializing string
1047 literal element type; this is only allowed when both types are
1048 ordinary character type. There are no string literals of
1049 signed or unsigned char type in the language, but we can get
1050 them internally from converting braced-init-lists to
1051 STRING_CST. */
1052 if (ordinary_char_type_p (to_char_type)
1053 && ordinary_char_type_p (from_char_type))
1054 return true;
1055 return false;
1058 /* Process the initializer INIT for a variable of type TYPE, emitting
1059 diagnostics for invalid initializers and converting the initializer as
1060 appropriate.
1062 For aggregate types, it assumes that reshape_init has already run, thus the
1063 initializer will have the right shape (brace elision has been undone).
1065 NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR,
1066 2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR. */
1068 static tree
1069 digest_init_r (tree type, tree init, int nested, int flags,
1070 tsubst_flags_t complain)
1072 enum tree_code code = TREE_CODE (type);
1074 if (error_operand_p (init))
1075 return error_mark_node;
1077 gcc_assert (init);
1079 /* We must strip the outermost array type when completing the type,
1080 because the its bounds might be incomplete at the moment. */
1081 if (!complete_type_or_maybe_complain (code == ARRAY_TYPE
1082 ? TREE_TYPE (type) : type, NULL_TREE,
1083 complain))
1084 return error_mark_node;
1086 location_t loc = cp_expr_loc_or_input_loc (init);
1088 tree stripped_init = init;
1090 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1091 && CONSTRUCTOR_IS_PAREN_INIT (init))
1092 flags |= LOOKUP_AGGREGATE_PAREN_INIT;
1094 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
1095 (g++.old-deja/g++.law/casts2.C). */
1096 if (TREE_CODE (init) == NON_LVALUE_EXPR)
1097 stripped_init = TREE_OPERAND (init, 0);
1099 stripped_init = tree_strip_any_location_wrapper (stripped_init);
1101 /* Initialization of an array of chars from a string constant. The initializer
1102 can be optionally enclosed in braces, but reshape_init has already removed
1103 them if they were present. */
1104 if (code == ARRAY_TYPE)
1106 if (nested && !TYPE_DOMAIN (type))
1107 /* C++ flexible array members have a null domain. */
1109 if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT)
1110 pedwarn (loc, OPT_Wpedantic,
1111 "initialization of a flexible array member");
1112 else
1114 if (complain & tf_error)
1115 error_at (loc, "non-static initialization of"
1116 " a flexible array member");
1117 return error_mark_node;
1121 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1122 if (char_type_p (typ1)
1123 && TREE_CODE (stripped_init) == STRING_CST)
1125 if (!array_string_literal_compatible_p (type, init))
1127 if (complain & tf_error)
1128 error_at (loc, "cannot initialize array of %qT from "
1129 "a string literal with type array of %qT",
1130 typ1,
1131 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init))));
1132 return error_mark_node;
1135 if (nested == 2 && !TYPE_DOMAIN (type))
1137 if (complain & tf_error)
1138 error_at (loc, "initialization of flexible array member "
1139 "in a nested context");
1140 return error_mark_node;
1143 if (type != TREE_TYPE (init)
1144 && !variably_modified_type_p (type, NULL_TREE))
1146 init = copy_node (init);
1147 TREE_TYPE (init) = type;
1148 /* If we have a location wrapper, then also copy the wrapped
1149 node, and update the copy's type. */
1150 if (location_wrapper_p (init))
1152 stripped_init = copy_node (stripped_init);
1153 TREE_OPERAND (init, 0) = stripped_init;
1154 TREE_TYPE (stripped_init) = type;
1157 if (TYPE_DOMAIN (type) && TREE_CONSTANT (TYPE_SIZE (type)))
1159 /* Not a flexible array member. */
1160 int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1161 size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1162 /* In C it is ok to subtract 1 from the length of the string
1163 because it's ok to ignore the terminating null char that is
1164 counted in the length of the constant, but in C++ this would
1165 be invalid. */
1166 if (size < TREE_STRING_LENGTH (stripped_init))
1168 permerror (loc, "initializer-string for %qT is too long",
1169 type);
1171 init = build_string (size,
1172 TREE_STRING_POINTER (stripped_init));
1173 TREE_TYPE (init) = type;
1176 return init;
1180 /* Handle scalar types (including conversions) and references. */
1181 if ((code != COMPLEX_TYPE || BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
1182 && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1184 /* Narrowing is OK when initializing an aggregate from
1185 a parenthesized list. */
1186 if (nested && !(flags & LOOKUP_AGGREGATE_PAREN_INIT))
1187 flags |= LOOKUP_NO_NARROWING;
1188 init = convert_for_initialization (0, type, init, flags,
1189 ICR_INIT, NULL_TREE, 0,
1190 complain);
1192 return init;
1195 /* Come here only for aggregates: records, arrays, unions, complex numbers
1196 and vectors. */
1197 gcc_assert (code == ARRAY_TYPE
1198 || VECTOR_TYPE_P (type)
1199 || code == RECORD_TYPE
1200 || code == UNION_TYPE
1201 || code == OPAQUE_TYPE
1202 || code == COMPLEX_TYPE);
1204 /* "If T is a class type and the initializer list has a single
1205 element of type cv U, where U is T or a class derived from T,
1206 the object is initialized from that element." */
1207 if (cxx_dialect >= cxx11
1208 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
1209 && !CONSTRUCTOR_IS_DESIGNATED_INIT (stripped_init)
1210 && CONSTRUCTOR_NELTS (stripped_init) == 1
1211 && ((CLASS_TYPE_P (type) && !CLASSTYPE_NON_AGGREGATE (type))
1212 || VECTOR_TYPE_P (type)))
1214 tree elt = CONSTRUCTOR_ELT (stripped_init, 0)->value;
1215 if (reference_related_p (type, TREE_TYPE (elt)))
1217 /* In C++17, aggregates can have bases, thus participate in
1218 aggregate initialization. In the following case:
1220 struct B { int c; };
1221 struct D : B { };
1222 D d{{D{{42}}}};
1224 there's an extra set of braces, so the D temporary initializes
1225 the first element of d, which is the B base subobject. The base
1226 of type B is copy-initialized from the D temporary, causing
1227 object slicing. */
1228 tree field = next_initializable_field (TYPE_FIELDS (type));
1229 if (field && DECL_FIELD_IS_BASE (field))
1231 if (warning_at (loc, 0, "initializing a base class of type %qT "
1232 "results in object slicing", TREE_TYPE (field)))
1233 inform (loc, "remove %<{ }%> around initializer");
1235 else if (flag_checking)
1236 /* We should have fixed this in reshape_init. */
1237 gcc_unreachable ();
1241 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init)
1242 && !TYPE_NON_AGGREGATE_CLASS (type))
1243 return process_init_constructor (type, stripped_init, nested, flags,
1244 complain);
1245 else
1247 if (COMPOUND_LITERAL_P (stripped_init) && code == ARRAY_TYPE)
1249 if (complain & tf_error)
1250 error_at (loc, "cannot initialize aggregate of type %qT with "
1251 "a compound literal", type);
1253 return error_mark_node;
1256 if (code == ARRAY_TYPE
1257 && !BRACE_ENCLOSED_INITIALIZER_P (stripped_init))
1259 /* Allow the result of build_array_copy and of
1260 build_value_init_noctor. */
1261 if ((TREE_CODE (stripped_init) == VEC_INIT_EXPR
1262 || TREE_CODE (stripped_init) == CONSTRUCTOR)
1263 && (same_type_ignoring_top_level_qualifiers_p
1264 (type, TREE_TYPE (init))))
1265 return init;
1267 if (complain & tf_error)
1268 error_at (loc, "array must be initialized with a brace-enclosed"
1269 " initializer");
1270 return error_mark_node;
1273 return convert_for_initialization (NULL_TREE, type, init,
1274 flags,
1275 ICR_INIT, NULL_TREE, 0,
1276 complain);
1280 tree
1281 digest_init (tree type, tree init, tsubst_flags_t complain)
1283 return digest_init_r (type, init, 0, LOOKUP_IMPLICIT, complain);
1286 tree
1287 digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain)
1289 return digest_init_r (type, init, 0, flags, complain);
1292 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1293 tree
1294 digest_nsdmi_init (tree decl, tree init, tsubst_flags_t complain)
1296 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1298 tree type = TREE_TYPE (decl);
1299 if (DECL_BIT_FIELD_TYPE (decl))
1300 type = DECL_BIT_FIELD_TYPE (decl);
1301 int flags = LOOKUP_IMPLICIT;
1302 if (DIRECT_LIST_INIT_P (init))
1304 flags = LOOKUP_NORMAL;
1305 complain |= tf_no_cleanup;
1307 if (BRACE_ENCLOSED_INITIALIZER_P (init)
1308 && CP_AGGREGATE_TYPE_P (type))
1309 init = reshape_init (type, init, complain);
1310 init = digest_init_flags (type, init, flags, complain);
1311 return init;
1314 /* Set of flags used within process_init_constructor to describe the
1315 initializers. */
1316 #define PICFLAG_ERRONEOUS 1
1317 #define PICFLAG_NOT_ALL_CONSTANT 2
1318 #define PICFLAG_NOT_ALL_SIMPLE 4
1319 #define PICFLAG_SIDE_EFFECTS 8
1321 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1322 describe it. */
1324 static int
1325 picflag_from_initializer (tree init)
1327 if (init == error_mark_node)
1328 return PICFLAG_ERRONEOUS;
1329 else if (!TREE_CONSTANT (init))
1331 if (TREE_SIDE_EFFECTS (init))
1332 return PICFLAG_SIDE_EFFECTS;
1333 else
1334 return PICFLAG_NOT_ALL_CONSTANT;
1336 else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1337 return PICFLAG_NOT_ALL_SIMPLE;
1338 return 0;
1341 /* Adjust INIT for going into a CONSTRUCTOR. */
1343 static tree
1344 massage_init_elt (tree type, tree init, int nested, int flags,
1345 tsubst_flags_t complain)
1347 int new_flags = LOOKUP_IMPLICIT;
1348 if (flags & LOOKUP_ALLOW_FLEXARRAY_INIT)
1349 new_flags |= LOOKUP_ALLOW_FLEXARRAY_INIT;
1350 if (flags & LOOKUP_AGGREGATE_PAREN_INIT)
1351 new_flags |= LOOKUP_AGGREGATE_PAREN_INIT;
1352 init = digest_init_r (type, init, nested ? 2 : 1, new_flags, complain);
1353 /* When we defer constant folding within a statement, we may want to
1354 defer this folding as well. */
1355 tree t = fold_non_dependent_init (init, complain);
1356 if (TREE_CONSTANT (t))
1357 init = t;
1358 return init;
1361 /* Subroutine of process_init_constructor, which will process an initializer
1362 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1363 which describe the initializers. */
1365 static int
1366 process_init_constructor_array (tree type, tree init, int nested, int flags,
1367 tsubst_flags_t complain)
1369 unsigned HOST_WIDE_INT i, len = 0;
1370 int picflags = 0;
1371 bool unbounded = false;
1372 constructor_elt *ce;
1373 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1375 gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1376 || VECTOR_TYPE_P (type));
1378 if (TREE_CODE (type) == ARRAY_TYPE)
1380 /* C++ flexible array members have a null domain. */
1381 tree domain = TYPE_DOMAIN (type);
1382 if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1383 len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
1384 - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
1385 TYPE_PRECISION (TREE_TYPE (domain)),
1386 TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
1387 else
1388 unbounded = true; /* Take as many as there are. */
1390 if (nested == 2 && !domain && !vec_safe_is_empty (v))
1392 if (complain & tf_error)
1393 error_at (cp_expr_loc_or_input_loc (init),
1394 "initialization of flexible array member "
1395 "in a nested context");
1396 return PICFLAG_ERRONEOUS;
1399 else
1400 /* Vectors are like simple fixed-size arrays. */
1401 unbounded = !TYPE_VECTOR_SUBPARTS (type).is_constant (&len);
1403 /* There must not be more initializers than needed. */
1404 if (!unbounded && vec_safe_length (v) > len)
1406 if (complain & tf_error)
1407 error ("too many initializers for %qT", type);
1408 else
1409 return PICFLAG_ERRONEOUS;
1412 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1414 if (!ce->index)
1415 ce->index = size_int (i);
1416 else if (!check_array_designated_initializer (ce, i))
1417 ce->index = error_mark_node;
1418 gcc_assert (ce->value);
1419 ce->value
1420 = massage_init_elt (TREE_TYPE (type), ce->value, nested, flags,
1421 complain);
1423 gcc_checking_assert
1424 (ce->value == error_mark_node
1425 || (same_type_ignoring_top_level_qualifiers_p
1426 (strip_array_types (TREE_TYPE (type)),
1427 strip_array_types (TREE_TYPE (ce->value)))));
1429 picflags |= picflag_from_initializer (ce->value);
1432 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1433 we must add initializers ourselves. */
1434 if (!unbounded)
1435 for (; i < len; ++i)
1437 tree next;
1439 if (type_build_ctor_call (TREE_TYPE (type)))
1441 /* If this type needs constructors run for default-initialization,
1442 we can't rely on the back end to do it for us, so make the
1443 initialization explicit by list-initializing from T{}. */
1444 next = build_constructor (init_list_type_node, NULL);
1445 next = massage_init_elt (TREE_TYPE (type), next, nested, flags,
1446 complain);
1447 if (initializer_zerop (next))
1448 /* The default zero-initialization is fine for us; don't
1449 add anything to the CONSTRUCTOR. */
1450 next = NULL_TREE;
1452 else if (!zero_init_p (TREE_TYPE (type)))
1453 next = build_zero_init (TREE_TYPE (type),
1454 /*nelts=*/NULL_TREE,
1455 /*static_storage_p=*/false);
1456 else
1457 /* The default zero-initialization is fine for us; don't
1458 add anything to the CONSTRUCTOR. */
1459 next = NULL_TREE;
1461 if (next)
1463 picflags |= picflag_from_initializer (next);
1464 if (len > i+1
1465 && (initializer_constant_valid_p (next, TREE_TYPE (next))
1466 == null_pointer_node))
1468 tree range = build2 (RANGE_EXPR, size_type_node,
1469 build_int_cst (size_type_node, i),
1470 build_int_cst (size_type_node, len - 1));
1471 CONSTRUCTOR_APPEND_ELT (v, range, next);
1472 break;
1474 else
1475 CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1477 else
1478 /* Don't bother checking all the other elements. */
1479 break;
1482 CONSTRUCTOR_ELTS (init) = v;
1483 return picflags;
1486 /* Subroutine of process_init_constructor, which will process an initializer
1487 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1488 the initializers. */
1490 static int
1491 process_init_constructor_record (tree type, tree init, int nested, int flags,
1492 tsubst_flags_t complain)
1494 vec<constructor_elt, va_gc> *v = NULL;
1495 tree field;
1496 int skipped = 0;
1498 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1499 gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1500 gcc_assert (!TYPE_BINFO (type)
1501 || cxx_dialect >= cxx17
1502 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1503 gcc_assert (!TYPE_POLYMORPHIC_P (type));
1505 restart:
1506 int picflags = 0;
1507 unsigned HOST_WIDE_INT idx = 0;
1508 int designator_skip = -1;
1509 /* Generally, we will always have an index for each initializer (which is
1510 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1511 reshape_init. So we need to handle both cases. */
1512 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1514 tree next;
1516 if (TREE_CODE (field) != FIELD_DECL
1517 || (DECL_ARTIFICIAL (field)
1518 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))))
1519 continue;
1521 if (DECL_UNNAMED_BIT_FIELD (field))
1522 continue;
1524 /* If this is a bitfield, first convert to the declared type. */
1525 tree fldtype = TREE_TYPE (field);
1526 if (DECL_BIT_FIELD_TYPE (field))
1527 fldtype = DECL_BIT_FIELD_TYPE (field);
1528 if (fldtype == error_mark_node)
1529 return PICFLAG_ERRONEOUS;
1531 next = NULL_TREE;
1532 if (idx < CONSTRUCTOR_NELTS (init))
1534 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1535 if (ce->index)
1537 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1538 latter case can happen in templates where lookup has to be
1539 deferred. */
1540 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1541 || identifier_p (ce->index));
1542 if (ce->index == field || ce->index == DECL_NAME (field))
1543 next = ce->value;
1544 else
1546 ce = NULL;
1547 if (designator_skip == -1)
1548 designator_skip = 1;
1551 else
1553 designator_skip = 0;
1554 next = ce->value;
1557 if (ce)
1559 gcc_assert (ce->value);
1560 next = massage_init_elt (fldtype, next, nested, flags, complain);
1561 ++idx;
1564 if (next == error_mark_node)
1565 /* We skip initializers for empty bases/fields, so skipping an invalid
1566 one could make us accept invalid code. */
1567 return PICFLAG_ERRONEOUS;
1568 else if (next)
1569 /* Already handled above. */;
1570 else if (DECL_INITIAL (field))
1572 if (skipped > 0)
1574 /* We're using an NSDMI past a field with implicit
1575 zero-init. Go back and make it explicit. */
1576 skipped = -1;
1577 vec_safe_truncate (v, 0);
1578 goto restart;
1580 /* C++14 aggregate NSDMI. */
1581 next = get_nsdmi (field, /*ctor*/false, complain);
1582 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
1583 && find_placeholders (next))
1584 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1586 else if (type_build_ctor_call (fldtype))
1588 /* If this type needs constructors run for
1589 default-initialization, we can't rely on the back end to do it
1590 for us, so build up TARGET_EXPRs. If the type in question is
1591 a class, just build one up; if it's an array, recurse. */
1592 next = build_constructor (init_list_type_node, NULL);
1593 next = massage_init_elt (fldtype, next, nested, flags, complain);
1595 /* Warn when some struct elements are implicitly initialized. */
1596 if ((complain & tf_warning)
1597 && !cp_unevaluated_operand
1598 && !EMPTY_CONSTRUCTOR_P (init))
1599 warning (OPT_Wmissing_field_initializers,
1600 "missing initializer for member %qD", field);
1602 else
1604 if (TYPE_REF_P (fldtype))
1606 if (complain & tf_error)
1607 error ("member %qD is uninitialized reference", field);
1608 else
1609 return PICFLAG_ERRONEOUS;
1611 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype))
1613 if (complain & tf_error)
1614 error ("member %qD with uninitialized reference fields", field);
1615 else
1616 return PICFLAG_ERRONEOUS;
1618 /* Do nothing for flexible array members since they need not have any
1619 elements. Don't worry about 'skipped' because a flexarray has to
1620 be the last field. */
1621 else if (TREE_CODE (fldtype) == ARRAY_TYPE && !TYPE_DOMAIN (fldtype))
1622 continue;
1624 /* Warn when some struct elements are implicitly initialized
1625 to zero. */
1626 if ((complain & tf_warning)
1627 && !cp_unevaluated_operand
1628 && !EMPTY_CONSTRUCTOR_P (init))
1629 warning (OPT_Wmissing_field_initializers,
1630 "missing initializer for member %qD", field);
1632 if (!zero_init_p (fldtype) || skipped < 0)
1634 if (TYPE_REF_P (fldtype))
1635 next = build_zero_cst (fldtype);
1636 else
1637 next = build_zero_init (fldtype, /*nelts=*/NULL_TREE,
1638 /*static_storage_p=*/false);
1640 else
1642 /* The default zero-initialization is fine for us; don't
1643 add anything to the CONSTRUCTOR. */
1644 skipped = 1;
1645 continue;
1649 if (is_empty_field (field)
1650 && !TREE_SIDE_EFFECTS (next))
1651 /* Don't add trivial initialization of an empty base/field to the
1652 constructor, as they might not be ordered the way the back-end
1653 expects. */
1654 continue;
1656 /* If this is a bitfield, now convert to the lowered type. */
1657 if (fldtype != TREE_TYPE (field))
1658 next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1659 picflags |= picflag_from_initializer (next);
1660 CONSTRUCTOR_APPEND_ELT (v, field, next);
1663 if (idx < CONSTRUCTOR_NELTS (init))
1665 if (complain & tf_error)
1667 constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1668 /* For better diagnostics, try to find out if it is really
1669 the case of too many initializers or if designators are
1670 in incorrect order. */
1671 if (designator_skip == 1 && ce->index)
1673 gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1674 || identifier_p (ce->index));
1675 for (field = TYPE_FIELDS (type);
1676 field; field = DECL_CHAIN (field))
1678 if (TREE_CODE (field) != FIELD_DECL
1679 || (DECL_ARTIFICIAL (field)
1680 && !(cxx_dialect >= cxx17
1681 && DECL_FIELD_IS_BASE (field))))
1682 continue;
1684 if (DECL_UNNAMED_BIT_FIELD (field))
1685 continue;
1687 if (ce->index == field || ce->index == DECL_NAME (field))
1688 break;
1691 if (field)
1692 error ("designator order for field %qD does not match declaration "
1693 "order in %qT", field, type);
1694 else
1695 error ("too many initializers for %qT", type);
1697 else
1698 return PICFLAG_ERRONEOUS;
1701 CONSTRUCTOR_ELTS (init) = v;
1702 return picflags;
1705 /* Subroutine of process_init_constructor, which will process a single
1706 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1707 which describe the initializer. */
1709 static int
1710 process_init_constructor_union (tree type, tree init, int nested, int flags,
1711 tsubst_flags_t complain)
1713 constructor_elt *ce;
1714 int len;
1716 /* If the initializer was empty, use the union's NSDMI if it has one.
1717 Otherwise use default zero initialization. */
1718 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1720 for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1722 if (TREE_CODE (field) == FIELD_DECL
1723 && DECL_INITIAL (field) != NULL_TREE)
1725 tree val = get_nsdmi (field, /*in_ctor=*/false, complain);
1726 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init)
1727 && find_placeholders (val))
1728 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init) = 1;
1729 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init), field, val);
1730 break;
1734 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1735 return 0;
1738 len = CONSTRUCTOR_ELTS (init)->length ();
1739 if (len > 1)
1741 if (!(complain & tf_error))
1742 return PICFLAG_ERRONEOUS;
1743 error ("too many initializers for %qT", type);
1744 CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1747 ce = &(*CONSTRUCTOR_ELTS (init))[0];
1749 /* If this element specifies a field, initialize via that field. */
1750 if (ce->index)
1752 if (TREE_CODE (ce->index) == FIELD_DECL)
1754 else if (identifier_p (ce->index))
1756 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1757 tree name = ce->index;
1758 tree field;
1759 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1760 if (DECL_NAME (field) == name)
1761 break;
1762 if (!field)
1764 if (complain & tf_error)
1765 error ("no field %qD found in union being initialized",
1766 field);
1767 ce->value = error_mark_node;
1769 ce->index = field;
1771 else
1773 gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1774 || TREE_CODE (ce->index) == RANGE_EXPR);
1775 if (complain & tf_error)
1776 error ("index value instead of field name in union initializer");
1777 ce->value = error_mark_node;
1780 else
1782 /* Find the first named field. ANSI decided in September 1990
1783 that only named fields count here. */
1784 tree field = TYPE_FIELDS (type);
1785 while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1786 field = TREE_CHAIN (field);
1787 if (field == NULL_TREE)
1789 if (complain & tf_error)
1790 error ("too many initializers for %qT", type);
1791 ce->value = error_mark_node;
1793 ce->index = field;
1796 if (ce->value && ce->value != error_mark_node)
1797 ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, nested,
1798 flags, complain);
1800 return picflag_from_initializer (ce->value);
1803 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1804 constructor is a brace-enclosed initializer, and will be modified in-place.
1806 Each element is converted to the right type through digest_init, and
1807 missing initializers are added following the language rules (zero-padding,
1808 etc.).
1810 After the execution, the initializer will have TREE_CONSTANT if all elts are
1811 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1812 constants that the assembler and linker can compute them.
1814 The function returns the initializer itself, or error_mark_node in case
1815 of error. */
1817 static tree
1818 process_init_constructor (tree type, tree init, int nested, int flags,
1819 tsubst_flags_t complain)
1821 int picflags;
1823 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1825 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type))
1826 picflags = process_init_constructor_array (type, init, nested, flags,
1827 complain);
1828 else if (TREE_CODE (type) == RECORD_TYPE)
1829 picflags = process_init_constructor_record (type, init, nested, flags,
1830 complain);
1831 else if (TREE_CODE (type) == UNION_TYPE)
1832 picflags = process_init_constructor_union (type, init, nested, flags,
1833 complain);
1834 else
1835 gcc_unreachable ();
1837 if (picflags & PICFLAG_ERRONEOUS)
1838 return error_mark_node;
1840 TREE_TYPE (init) = type;
1841 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1842 cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1843 if (picflags & PICFLAG_SIDE_EFFECTS)
1845 TREE_CONSTANT (init) = false;
1846 TREE_SIDE_EFFECTS (init) = true;
1848 else if (picflags & PICFLAG_NOT_ALL_CONSTANT)
1850 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1851 TREE_CONSTANT (init) = false;
1852 TREE_SIDE_EFFECTS (init) = false;
1854 else
1856 TREE_CONSTANT (init) = 1;
1857 TREE_SIDE_EFFECTS (init) = false;
1858 if (!(picflags & PICFLAG_NOT_ALL_SIMPLE))
1859 TREE_STATIC (init) = 1;
1861 return init;
1864 /* Given a structure or union value DATUM, construct and return
1865 the structure or union component which results from narrowing
1866 that value to the base specified in BASETYPE. For example, given the
1867 hierarchy
1869 class L { int ii; };
1870 class A : L { ... };
1871 class B : L { ... };
1872 class C : A, B { ... };
1874 and the declaration
1876 C x;
1878 then the expression
1880 x.A::ii refers to the ii member of the L part of
1881 the A part of the C object named by X. In this case,
1882 DATUM would be x, and BASETYPE would be A.
1884 I used to think that this was nonconformant, that the standard specified
1885 that first we look up ii in A, then convert x to an L& and pull out the
1886 ii part. But in fact, it does say that we convert x to an A&; A here
1887 is known as the "naming class". (jason 2000-12-19)
1889 BINFO_P points to a variable initialized either to NULL_TREE or to the
1890 binfo for the specific base subobject we want to convert to. */
1892 tree
1893 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1895 tree binfo;
1897 if (datum == error_mark_node)
1898 return error_mark_node;
1899 if (*binfo_p)
1900 binfo = *binfo_p;
1901 else
1902 binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
1903 NULL, tf_warning_or_error);
1905 if (!binfo || binfo == error_mark_node)
1907 *binfo_p = NULL_TREE;
1908 if (!binfo)
1909 error_not_base_type (basetype, TREE_TYPE (datum));
1910 return error_mark_node;
1913 *binfo_p = binfo;
1914 return build_base_path (PLUS_EXPR, datum, binfo, 1,
1915 tf_warning_or_error);
1918 /* Build a reference to an object specified by the C++ `->' operator.
1919 Usually this just involves dereferencing the object, but if the
1920 `->' operator is overloaded, then such overloads must be
1921 performed until an object which does not have the `->' operator
1922 overloaded is found. An error is reported when circular pointer
1923 delegation is detected. */
1925 tree
1926 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
1928 tree orig_expr = expr;
1929 tree type = TREE_TYPE (expr);
1930 tree last_rval = NULL_TREE;
1931 vec<tree, va_gc> *types_memoized = NULL;
1933 if (type == error_mark_node)
1934 return error_mark_node;
1936 if (processing_template_decl)
1938 tree ttype = NULL_TREE;
1939 if (type && TYPE_PTR_P (type))
1940 ttype = TREE_TYPE (type);
1941 if (ttype && !dependent_scope_p (ttype))
1942 /* Pointer to current instantiation, don't treat as dependent. */;
1943 else if (type_dependent_expression_p (expr))
1945 expr = build_min_nt_loc (loc, ARROW_EXPR, expr);
1946 TREE_TYPE (expr) = ttype;
1947 return expr;
1949 expr = build_non_dependent_expr (expr);
1952 if (MAYBE_CLASS_TYPE_P (type))
1954 struct tinst_level *actual_inst = current_instantiation ();
1955 tree fn = NULL;
1957 while ((expr = build_new_op (loc, COMPONENT_REF,
1958 LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
1959 &fn, complain)))
1961 if (expr == error_mark_node)
1962 return error_mark_node;
1964 /* This provides a better instantiation backtrace in case of
1965 error. */
1966 if (fn && DECL_USE_TEMPLATE (fn))
1967 push_tinst_level_loc (fn,
1968 (current_instantiation () != actual_inst)
1969 ? DECL_SOURCE_LOCATION (fn)
1970 : input_location);
1971 fn = NULL;
1973 if (vec_member (TREE_TYPE (expr), types_memoized))
1975 if (complain & tf_error)
1976 error ("circular pointer delegation detected");
1977 return error_mark_node;
1980 vec_safe_push (types_memoized, TREE_TYPE (expr));
1981 last_rval = expr;
1984 while (current_instantiation () != actual_inst)
1985 pop_tinst_level ();
1987 if (last_rval == NULL_TREE)
1989 if (complain & tf_error)
1990 error ("base operand of %<->%> has non-pointer type %qT", type);
1991 return error_mark_node;
1994 if (TYPE_REF_P (TREE_TYPE (last_rval)))
1995 last_rval = convert_from_reference (last_rval);
1997 else
1999 last_rval = decay_conversion (expr, complain);
2000 if (last_rval == error_mark_node)
2001 return error_mark_node;
2004 if (TYPE_PTR_P (TREE_TYPE (last_rval)))
2006 if (processing_template_decl)
2008 expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
2009 orig_expr);
2010 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
2011 return expr;
2014 return cp_build_indirect_ref (loc, last_rval, RO_ARROW, complain);
2017 if (complain & tf_error)
2019 if (types_memoized)
2020 error ("result of %<operator->()%> yields non-pointer result");
2021 else
2022 error ("base operand of %<->%> is not a pointer");
2024 return error_mark_node;
2027 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
2028 already been checked out to be of aggregate type. */
2030 tree
2031 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
2033 tree ptrmem_type;
2034 tree objtype;
2035 tree type;
2036 tree binfo;
2037 tree ctype;
2039 datum = mark_lvalue_use (datum);
2040 component = mark_rvalue_use (component);
2042 if (error_operand_p (datum) || error_operand_p (component))
2043 return error_mark_node;
2045 ptrmem_type = TREE_TYPE (component);
2046 if (!TYPE_PTRMEM_P (ptrmem_type))
2048 if (complain & tf_error)
2049 error ("%qE cannot be used as a member pointer, since it is of "
2050 "type %qT", component, ptrmem_type);
2051 return error_mark_node;
2054 objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2055 if (! MAYBE_CLASS_TYPE_P (objtype))
2057 if (complain & tf_error)
2058 error ("cannot apply member pointer %qE to %qE, which is of "
2059 "non-class type %qT", component, datum, objtype);
2060 return error_mark_node;
2063 type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
2064 ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
2066 if (!COMPLETE_TYPE_P (ctype))
2068 if (!same_type_p (ctype, objtype))
2069 goto mismatch;
2070 binfo = NULL;
2072 else
2074 binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
2076 if (!binfo)
2078 mismatch:
2079 if (complain & tf_error)
2080 error ("pointer to member type %qT incompatible with object "
2081 "type %qT", type, objtype);
2082 return error_mark_node;
2084 else if (binfo == error_mark_node)
2085 return error_mark_node;
2088 if (TYPE_PTRDATAMEM_P (ptrmem_type))
2090 bool is_lval = real_lvalue_p (datum);
2091 tree ptype;
2093 /* Compute the type of the field, as described in [expr.ref].
2094 There's no such thing as a mutable pointer-to-member, so
2095 things are not as complex as they are for references to
2096 non-static data members. */
2097 type = cp_build_qualified_type (type,
2098 (cp_type_quals (type)
2099 | cp_type_quals (TREE_TYPE (datum))));
2101 datum = build_address (datum);
2103 /* Convert object to the correct base. */
2104 if (binfo)
2106 datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
2107 if (datum == error_mark_node)
2108 return error_mark_node;
2111 /* Build an expression for "object + offset" where offset is the
2112 value stored in the pointer-to-data-member. */
2113 ptype = build_pointer_type (type);
2114 datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
2115 datum = cp_build_fold_indirect_ref (datum);
2116 if (datum == error_mark_node)
2117 return error_mark_node;
2119 /* If the object expression was an rvalue, return an rvalue. */
2120 if (!is_lval)
2121 datum = move (datum);
2122 return datum;
2124 else
2126 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
2127 program is ill-formed if the second operand is a pointer to member
2128 function with ref-qualifier & (for C++20: unless its cv-qualifier-seq
2129 is const). In a .* expression whose object expression is an lvalue,
2130 the program is ill-formed if the second operand is a pointer to member
2131 function with ref-qualifier &&. */
2132 if (FUNCTION_REF_QUALIFIED (type))
2134 bool lval = lvalue_p (datum);
2135 if (lval && FUNCTION_RVALUE_QUALIFIED (type))
2137 if (complain & tf_error)
2138 error ("pointer-to-member-function type %qT requires an rvalue",
2139 ptrmem_type);
2140 return error_mark_node;
2142 else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
2144 if ((type_memfn_quals (type)
2145 & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))
2146 != TYPE_QUAL_CONST)
2148 if (complain & tf_error)
2149 error ("pointer-to-member-function type %qT requires "
2150 "an lvalue", ptrmem_type);
2151 return error_mark_node;
2153 else if (cxx_dialect < cxx20)
2155 if (complain & tf_warning_or_error)
2156 pedwarn (input_location, OPT_Wpedantic,
2157 "pointer-to-member-function type %qT requires "
2158 "an lvalue before C++20", ptrmem_type);
2159 else
2160 return error_mark_node;
2164 return build2 (OFFSET_REF, type, datum, component);
2168 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
2170 static tree
2171 build_functional_cast_1 (location_t loc, tree exp, tree parms,
2172 tsubst_flags_t complain)
2174 /* This is either a call to a constructor,
2175 or a C cast in C++'s `functional' notation. */
2177 /* The type to which we are casting. */
2178 tree type;
2180 if (error_operand_p (exp) || parms == error_mark_node)
2181 return error_mark_node;
2183 if (TREE_CODE (exp) == TYPE_DECL)
2185 type = TREE_TYPE (exp);
2187 if (DECL_ARTIFICIAL (exp))
2188 cp_handle_deprecated_or_unavailable (type);
2190 else
2191 type = exp;
2193 /* We need to check this explicitly, since value-initialization of
2194 arrays is allowed in other situations. */
2195 if (TREE_CODE (type) == ARRAY_TYPE)
2197 if (complain & tf_error)
2198 error_at (loc, "functional cast to array type %qT", type);
2199 return error_mark_node;
2202 if (tree anode = type_uses_auto (type))
2204 tree init;
2205 if (CLASS_PLACEHOLDER_TEMPLATE (anode))
2206 init = parms;
2207 /* C++23 auto(x). */
2208 else if (!AUTO_IS_DECLTYPE (anode)
2209 && list_length (parms) == 1)
2211 init = TREE_VALUE (parms);
2212 if (cxx_dialect < cxx23)
2213 pedwarn (loc, OPT_Wc__23_extensions,
2214 "%<auto(x)%> only available with "
2215 "%<-std=c++2b%> or %<-std=gnu++2b%>");
2217 else
2219 if (complain & tf_error)
2220 error_at (loc, "invalid use of %qT", anode);
2221 return error_mark_node;
2223 type = do_auto_deduction (type, init, anode, complain,
2224 adc_variable_type);
2225 if (type == error_mark_node)
2226 return error_mark_node;
2229 if (processing_template_decl)
2231 tree t;
2233 /* Diagnose this even in a template. We could also try harder
2234 to give all the usual errors when the type and args are
2235 non-dependent... */
2236 if (TYPE_REF_P (type) && !parms)
2238 if (complain & tf_error)
2239 error_at (loc, "invalid value-initialization of reference type");
2240 return error_mark_node;
2243 t = build_min (CAST_EXPR, type, parms);
2244 /* We don't know if it will or will not have side effects. */
2245 TREE_SIDE_EFFECTS (t) = 1;
2246 return t;
2249 if (! MAYBE_CLASS_TYPE_P (type))
2251 if (parms == NULL_TREE)
2253 if (VOID_TYPE_P (type))
2254 return void_node;
2255 return build_value_init (cv_unqualified (type), complain);
2258 /* This must build a C cast. */
2259 parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
2260 return cp_build_c_cast (loc, type, parms, complain);
2263 /* Prepare to evaluate as a call to a constructor. If this expression
2264 is actually used, for example,
2266 return X (arg1, arg2, ...);
2268 then the slot being initialized will be filled in. */
2270 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
2271 return error_mark_node;
2272 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
2273 return error_mark_node;
2275 /* [expr.type.conv]
2277 If the expression list is a single-expression, the type
2278 conversion is equivalent (in definedness, and if defined in
2279 meaning) to the corresponding cast expression. */
2280 if (parms && TREE_CHAIN (parms) == NULL_TREE)
2281 return cp_build_c_cast (loc, type, TREE_VALUE (parms), complain);
2283 /* [expr.type.conv]
2285 The expression T(), where T is a simple-type-specifier for a
2286 non-array complete object type or the (possibly cv-qualified)
2287 void type, creates an rvalue of the specified type, which is
2288 value-initialized. */
2290 if (parms == NULL_TREE)
2292 exp = build_value_init (type, complain);
2293 exp = get_target_expr_sfinae (exp, complain);
2294 return exp;
2297 /* Call the constructor. */
2298 releasing_vec parmvec;
2299 for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
2300 vec_safe_push (parmvec, TREE_VALUE (parms));
2301 exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2302 &parmvec, type, LOOKUP_NORMAL, complain);
2304 if (exp == error_mark_node)
2305 return error_mark_node;
2307 return build_cplus_new (type, exp, complain);
2310 tree
2311 build_functional_cast (location_t loc, tree exp, tree parms,
2312 tsubst_flags_t complain)
2314 tree result = build_functional_cast_1 (loc, exp, parms, complain);
2315 protected_set_expr_location (result, loc);
2316 return result;
2320 /* Add new exception specifier SPEC, to the LIST we currently have.
2321 If it's already in LIST then do nothing.
2322 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2323 know what we're doing. */
2325 tree
2326 add_exception_specifier (tree list, tree spec, tsubst_flags_t complain)
2328 bool ok;
2329 tree core = spec;
2330 bool is_ptr;
2331 diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
2333 if (spec == error_mark_node)
2334 return list;
2336 gcc_assert (spec && (!list || TREE_VALUE (list)));
2338 /* [except.spec] 1, type in an exception specifier shall not be
2339 incomplete, or pointer or ref to incomplete other than pointer
2340 to cv void. */
2341 is_ptr = TYPE_PTR_P (core);
2342 if (is_ptr || TYPE_REF_P (core))
2343 core = TREE_TYPE (core);
2344 if (complain < 0)
2345 ok = true;
2346 else if (VOID_TYPE_P (core))
2347 ok = is_ptr;
2348 else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
2349 ok = true;
2350 else if (processing_template_decl)
2351 ok = true;
2352 else if (!verify_type_context (input_location, TCTX_EXCEPTIONS, core,
2353 !(complain & tf_error)))
2354 return error_mark_node;
2355 else
2357 ok = true;
2358 /* 15.4/1 says that types in an exception specifier must be complete,
2359 but it seems more reasonable to only require this on definitions
2360 and calls. So just give a pedwarn at this point; we will give an
2361 error later if we hit one of those two cases. */
2362 if (!COMPLETE_TYPE_P (complete_type (core)))
2363 diag_type = DK_PEDWARN; /* pedwarn */
2366 if (ok)
2368 tree probe;
2370 for (probe = list; probe; probe = TREE_CHAIN (probe))
2371 if (same_type_p (TREE_VALUE (probe), spec))
2372 break;
2373 if (!probe)
2374 list = tree_cons (NULL_TREE, spec, list);
2376 else
2377 diag_type = DK_ERROR; /* error */
2379 if (diag_type != DK_UNSPECIFIED
2380 && (complain & tf_warning_or_error))
2381 cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
2383 return list;
2386 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2388 static bool
2389 nothrow_spec_p_uninst (const_tree spec)
2391 if (DEFERRED_NOEXCEPT_SPEC_P (spec))
2392 return false;
2393 return nothrow_spec_p (spec);
2396 /* Combine the two exceptions specifier lists LIST and ADD, and return
2397 their union. */
2399 tree
2400 merge_exception_specifiers (tree list, tree add)
2402 tree noex, orig_list;
2404 if (list == error_mark_node || add == error_mark_node)
2405 return error_mark_node;
2407 /* No exception-specifier or noexcept(false) are less strict than
2408 anything else. Prefer the newer variant (LIST). */
2409 if (!list || list == noexcept_false_spec)
2410 return list;
2411 else if (!add || add == noexcept_false_spec)
2412 return add;
2414 /* noexcept(true) and throw() are stricter than anything else.
2415 As above, prefer the more recent one (LIST). */
2416 if (nothrow_spec_p_uninst (add))
2417 return list;
2419 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2420 if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
2421 && UNEVALUATED_NOEXCEPT_SPEC_P (list))
2422 return list;
2423 /* We should have instantiated other deferred noexcept specs by now. */
2424 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
2426 if (nothrow_spec_p_uninst (list))
2427 return add;
2428 noex = TREE_PURPOSE (list);
2429 gcc_checking_assert (!TREE_PURPOSE (add)
2430 || errorcount || !flag_exceptions
2431 || cp_tree_equal (noex, TREE_PURPOSE (add)));
2433 /* Combine the dynamic-exception-specifiers, if any. */
2434 orig_list = list;
2435 for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
2437 tree spec = TREE_VALUE (add);
2438 tree probe;
2440 for (probe = orig_list; probe && TREE_VALUE (probe);
2441 probe = TREE_CHAIN (probe))
2442 if (same_type_p (TREE_VALUE (probe), spec))
2443 break;
2444 if (!probe)
2446 spec = build_tree_list (NULL_TREE, spec);
2447 TREE_CHAIN (spec) = list;
2448 list = spec;
2452 /* Keep the noexcept-specifier at the beginning of the list. */
2453 if (noex != TREE_PURPOSE (list))
2454 list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2456 return list;
2459 /* Subroutine of build_call. Ensure that each of the types in the
2460 exception specification is complete. Technically, 15.4/1 says that
2461 they need to be complete when we see a declaration of the function,
2462 but we should be able to get away with only requiring this when the
2463 function is defined or called. See also add_exception_specifier. */
2465 void
2466 require_complete_eh_spec_types (tree fntype, tree decl)
2468 tree raises;
2469 /* Don't complain about calls to op new. */
2470 if (decl && DECL_ARTIFICIAL (decl))
2471 return;
2472 for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2473 raises = TREE_CHAIN (raises))
2475 tree type = TREE_VALUE (raises);
2476 if (type && !COMPLETE_TYPE_P (type))
2478 if (decl)
2479 error
2480 ("call to function %qD which throws incomplete type %q#T",
2481 decl, type);
2482 else
2483 error ("call to function which throws incomplete type %q#T",
2484 decl);