1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987-2020 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)
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. */
30 #include "coretypes.h"
32 #include "stor-layout.h"
35 #include "gcc-rich-location.h"
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. */
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
;
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
)
64 error_not_base_type (base
, type
);
68 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
69 value may not be changed thereafter. */
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
79 #define ERROR_FOR_ASSIGNMENT(LOC, AS, ASM, IN, DE, ARG) \
84 error_at (LOC, AS, ARG); \
87 error_at (LOC, ASM, ARG); \
90 error_at (LOC, IN, ARG); \
93 error_at (LOC, DE, ARG); \
100 /* Handle C++-specific things first. */
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"),
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));
123 readonly_error (loc
, arg
, errstring
);
126 /* Structure that holds information about declarations whose type was
127 incomplete and we could not check whether it was abstract or not. */
129 struct GTY((chain_next ("%h.next"), for_user
)) pending_abstract_type
{
130 /* Declaration which we are checking for abstractness. It is either
131 a DECL node, or an IDENTIFIER_NODE if we do not have a full
132 declaration available. */
135 /* Type which will be checked for abstractness. */
138 /* Kind of use in an unnamed declarator. */
139 enum abstract_class_use use
;
141 /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
142 because DECLs already carry locus information. */
145 /* Link to the next element in list. */
146 struct pending_abstract_type
* next
;
149 struct abstract_type_hasher
: ggc_ptr_hash
<pending_abstract_type
>
151 typedef tree compare_type
;
152 static hashval_t
hash (pending_abstract_type
*);
153 static bool equal (pending_abstract_type
*, tree
);
156 /* Compute the hash value of the node VAL. This function is used by the
157 hash table abstract_pending_vars. */
160 abstract_type_hasher::hash (pending_abstract_type
*pat
)
162 return (hashval_t
) TYPE_UID (pat
->type
);
166 /* Compare node VAL1 with the type VAL2. This function is used by the
167 hash table abstract_pending_vars. */
170 abstract_type_hasher::equal (pending_abstract_type
*pat1
, tree type2
)
172 return (pat1
->type
== type2
);
175 /* Hash table that maintains pending_abstract_type nodes, for which we still
176 need to check for type abstractness. The key of the table is the type
177 of the declaration. */
178 static GTY (()) hash_table
<abstract_type_hasher
> *abstract_pending_vars
= NULL
;
180 static int abstract_virtuals_error_sfinae (tree
, tree
, abstract_class_use
, tsubst_flags_t
);
182 /* This function is called after TYPE is completed, and will check if there
183 are pending declarations for which we still need to verify the abstractness
184 of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
185 turned out to be incomplete. */
188 complete_type_check_abstract (tree type
)
190 struct pending_abstract_type
*pat
;
191 location_t cur_loc
= input_location
;
193 gcc_assert (COMPLETE_TYPE_P (type
));
195 if (!abstract_pending_vars
)
198 /* Retrieve the list of pending declarations for this type. */
199 pending_abstract_type
**slot
200 = abstract_pending_vars
->find_slot_with_hash (type
, TYPE_UID (type
),
207 /* If the type is not abstract, do not do anything. */
208 if (CLASSTYPE_PURE_VIRTUALS (type
))
210 struct pending_abstract_type
*prev
= 0, *next
;
212 /* Reverse the list to emit the errors in top-down order. */
213 for (; pat
; pat
= next
)
221 /* Go through the list, and call abstract_virtuals_error for each
222 element: it will issue a diagnostic if the type is abstract. */
225 gcc_assert (type
== pat
->type
);
227 /* Tweak input_location so that the diagnostic appears at the correct
228 location. Notice that this is only needed if the decl is an
230 input_location
= pat
->locus
;
231 abstract_virtuals_error_sfinae (pat
->decl
, pat
->type
, pat
->use
,
232 tf_warning_or_error
);
237 abstract_pending_vars
->clear_slot (slot
);
239 input_location
= cur_loc
;
243 /* If TYPE has abstract virtual functions, issue an error about trying
244 to create an object of that type. DECL is the object declared, or
245 NULL_TREE if the declaration is unavailable, in which case USE specifies
246 the kind of invalid use. Returns 1 if an error occurred; zero if
250 abstract_virtuals_error_sfinae (tree decl
, tree type
, abstract_class_use use
,
251 tsubst_flags_t complain
)
253 vec
<tree
, va_gc
> *pure
;
255 /* This function applies only to classes. Any other entity can never
257 if (!CLASS_TYPE_P (type
))
259 type
= TYPE_MAIN_VARIANT (type
);
262 /* Instantiation here seems to be required by the standard,
263 but breaks e.g. boost::bind. FIXME! */
264 /* In SFINAE, non-N3276 context, force instantiation. */
265 if (!(complain
& (tf_error
|tf_decltype
)))
266 complete_type (type
);
269 /* If the type is incomplete, we register it within a hash table,
270 so that we can check again once it is completed. This makes sense
271 only for objects for which we have a declaration or at least a
273 if (!COMPLETE_TYPE_P (type
) && (complain
& tf_error
))
275 struct pending_abstract_type
*pat
;
277 gcc_assert (!decl
|| DECL_P (decl
) || identifier_p (decl
));
279 if (!abstract_pending_vars
)
280 abstract_pending_vars
281 = hash_table
<abstract_type_hasher
>::create_ggc (31);
283 pending_abstract_type
**slot
284 = abstract_pending_vars
->find_slot_with_hash (type
, TYPE_UID (type
),
287 pat
= ggc_alloc
<pending_abstract_type
> ();
291 pat
->locus
= ((decl
&& DECL_P (decl
))
292 ? DECL_SOURCE_LOCATION (decl
)
301 if (!TYPE_SIZE (type
))
302 /* TYPE is being defined, and during that time
303 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
306 pure
= CLASSTYPE_PURE_VIRTUALS (type
);
310 if (!(complain
& tf_error
))
313 auto_diagnostic_group d
;
317 error ("cannot declare variable %q+D to be of abstract "
318 "type %qT", decl
, type
);
319 else if (TREE_CODE (decl
) == PARM_DECL
)
321 if (DECL_NAME (decl
))
322 error ("cannot declare parameter %q+D to be of abstract type %qT",
325 error ("cannot declare parameter to be of abstract type %qT",
328 else if (TREE_CODE (decl
) == FIELD_DECL
)
329 error ("cannot declare field %q+D to be of abstract type %qT",
331 else if (TREE_CODE (decl
) == FUNCTION_DECL
332 && TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
333 error ("invalid abstract return type for member function %q+#D", decl
);
334 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
335 error ("invalid abstract return type for function %q+#D", decl
);
336 else if (identifier_p (decl
))
337 /* Here we do not have location information. */
338 error ("invalid abstract type %qT for %qE", type
, decl
);
340 error ("invalid abstract type for %q+D", decl
);
345 error ("creating array of %qT, which is an abstract class type", type
);
348 error ("invalid cast to abstract class type %qT", type
);
351 error ("invalid new-expression of abstract class type %qT", type
);
354 error ("invalid abstract return type %qT", type
);
357 error ("invalid abstract parameter type %qT", type
);
360 error ("expression of abstract class type %qT cannot "
361 "be used in throw-expression", type
);
364 error ("cannot declare %<catch%> parameter to be of abstract "
365 "class type %qT", type
);
368 error ("cannot allocate an object of abstract type %qT", type
);
371 /* Only go through this once. */
377 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
378 " because the following virtual functions are pure within %qT:",
381 FOR_EACH_VEC_ELT (*pure
, ix
, fn
)
382 if (! DECL_CLONED_FUNCTION_P (fn
)
383 || DECL_COMPLETE_DESTRUCTOR_P (fn
))
384 inform (DECL_SOURCE_LOCATION (fn
), " %#qD", fn
);
386 /* Now truncate the vector. This leaves it non-null, so we know
387 there are pure virtuals, but empty so we don't list them out
396 abstract_virtuals_error_sfinae (tree decl
, tree type
, tsubst_flags_t complain
)
398 return abstract_virtuals_error_sfinae (decl
, type
, ACU_UNKNOWN
, complain
);
402 abstract_virtuals_error_sfinae (abstract_class_use use
, tree type
,
403 tsubst_flags_t complain
)
405 return abstract_virtuals_error_sfinae (NULL_TREE
, type
, use
, complain
);
409 /* Wrapper for the above function in the common case of wanting errors. */
412 abstract_virtuals_error (tree decl
, tree type
)
414 return abstract_virtuals_error_sfinae (decl
, type
, tf_warning_or_error
);
418 abstract_virtuals_error (abstract_class_use use
, tree type
)
420 return abstract_virtuals_error_sfinae (use
, type
, tf_warning_or_error
);
423 /* Print an inform about the declaration of the incomplete type TYPE. */
426 cxx_incomplete_type_inform (const_tree type
)
428 if (!TYPE_MAIN_DECL (type
))
431 location_t loc
= DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
));
432 tree ptype
= strip_top_quals (CONST_CAST_TREE (type
));
434 if (current_class_type
435 && TYPE_BEING_DEFINED (current_class_type
)
436 && same_type_p (ptype
, current_class_type
))
437 inform (loc
, "definition of %q#T is not complete until "
438 "the closing brace", ptype
);
439 else if (!TYPE_TEMPLATE_INFO (ptype
))
440 inform (loc
, "forward declaration of %q#T", ptype
);
442 inform (loc
, "declaration of %q#T", ptype
);
445 /* Print an error message for invalid use of an incomplete type.
446 VALUE is the expression that was used (or 0 if that isn't known)
447 and TYPE is the type that was invalid. DIAG_KIND indicates the
448 type of diagnostic (see diagnostic.def). */
451 cxx_incomplete_type_diagnostic (location_t loc
, const_tree value
,
452 const_tree type
, diagnostic_t diag_kind
)
454 bool is_decl
= false, complained
= false;
456 gcc_assert (diag_kind
== DK_WARNING
457 || diag_kind
== DK_PEDWARN
458 || diag_kind
== DK_ERROR
);
460 /* Avoid duplicate error message. */
461 if (TREE_CODE (type
) == ERROR_MARK
)
466 STRIP_ANY_LOCATION_WRAPPER (value
);
469 || TREE_CODE (value
) == PARM_DECL
470 || TREE_CODE (value
) == FIELD_DECL
)
472 complained
= emit_diagnostic (diag_kind
, DECL_SOURCE_LOCATION (value
), 0,
473 "%qD has incomplete type", value
);
478 /* We must print an error message. Be clever about what it says. */
480 switch (TREE_CODE (type
))
486 complained
= emit_diagnostic (diag_kind
, loc
, 0,
487 "invalid use of incomplete type %q#T",
490 cxx_incomplete_type_inform (type
);
494 emit_diagnostic (diag_kind
, loc
, 0,
495 "invalid use of %qT", type
);
499 if (TYPE_DOMAIN (type
))
501 type
= TREE_TYPE (type
);
504 emit_diagnostic (diag_kind
, loc
, 0,
505 "invalid use of array with unspecified bounds");
511 tree member
= TREE_OPERAND (value
, 1);
512 if (is_overloaded_fn (member
))
513 member
= get_first_fn (member
);
515 if (DECL_FUNCTION_MEMBER_P (member
)
516 && ! flag_ms_extensions
)
518 gcc_rich_location
richloc (loc
);
519 /* If "member" has no arguments (other than "this"), then
520 add a fix-it hint. */
521 if (type_num_arguments (TREE_TYPE (member
)) == 1)
522 richloc
.add_fixit_insert_after ("()");
523 emit_diagnostic (diag_kind
, &richloc
, 0,
524 "invalid use of member function %qD "
525 "(did you forget the %<()%> ?)", member
);
528 emit_diagnostic (diag_kind
, loc
, 0,
529 "invalid use of member %qD "
530 "(did you forget the %<&%> ?)", member
);
534 case TEMPLATE_TYPE_PARM
:
537 if (CLASS_PLACEHOLDER_TEMPLATE (type
))
538 emit_diagnostic (diag_kind
, loc
, 0,
539 "invalid use of placeholder %qT", type
);
541 emit_diagnostic (diag_kind
, loc
, 0,
542 "invalid use of %qT", type
);
545 emit_diagnostic (diag_kind
, loc
, 0,
546 "invalid use of template type parameter %qT", type
);
549 case BOUND_TEMPLATE_TEMPLATE_PARM
:
550 emit_diagnostic (diag_kind
, loc
, 0,
551 "invalid use of template template parameter %qT",
555 case TYPE_PACK_EXPANSION
:
556 emit_diagnostic (diag_kind
, loc
, 0,
557 "invalid use of pack expansion %qT", type
);
562 emit_diagnostic (diag_kind
, loc
, 0,
563 "invalid use of dependent type %qT", type
);
567 if (type
== init_list_type_node
)
569 emit_diagnostic (diag_kind
, loc
, 0,
570 "invalid use of brace-enclosed initializer list");
573 gcc_assert (type
== unknown_type_node
);
574 if (value
&& TREE_CODE (value
) == COMPONENT_REF
)
576 else if (value
&& TREE_CODE (value
) == ADDR_EXPR
)
577 emit_diagnostic (diag_kind
, loc
, 0,
578 "address of overloaded function with no contextual "
580 else if (value
&& TREE_CODE (value
) == OVERLOAD
)
581 emit_diagnostic (diag_kind
, loc
, 0,
582 "overloaded function with no contextual type information");
584 emit_diagnostic (diag_kind
, loc
, 0,
585 "insufficient contextual information to determine type");
593 /* Print an error message for invalid use of an incomplete type.
594 VALUE is the expression that was used (or 0 if that isn't known)
595 and TYPE is the type that was invalid. */
598 cxx_incomplete_type_error (location_t loc
, const_tree value
, const_tree type
)
600 cxx_incomplete_type_diagnostic (loc
, value
, type
, DK_ERROR
);
604 /* The recursive part of split_nonconstant_init. DEST is an lvalue
605 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
606 Return true if the whole of the value was initialized by the
607 generated statements. */
610 split_nonconstant_init_1 (tree dest
, tree init
, bool nested
)
612 unsigned HOST_WIDE_INT idx
, tidx
= HOST_WIDE_INT_M1U
;
613 tree field_index
, value
;
614 tree type
= TREE_TYPE (dest
);
615 tree inner_type
= NULL
;
616 bool array_type_p
= false;
617 bool complete_p
= true;
618 HOST_WIDE_INT num_split_elts
= 0;
620 switch (TREE_CODE (type
))
623 inner_type
= TREE_TYPE (type
);
625 if ((TREE_SIDE_EFFECTS (init
)
626 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
627 || vla_type_p (type
))
629 /* For an array, we only need/want a single cleanup region rather
630 than one per element. */
631 tree code
= build_vec_init (dest
, NULL_TREE
, init
, false, 1,
632 tf_warning_or_error
);
635 /* Also clean up the whole array if something later in an enclosing
637 if (tree cleanup
= cxx_maybe_build_cleanup (dest
,
638 tf_warning_or_error
))
639 finish_eh_cleanup (cleanup
);
646 case QUAL_UNION_TYPE
:
647 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init
), idx
,
650 /* The current implementation of this algorithm assumes that
651 the field was set for all the elements. This is usually done
652 by process_init_constructor. */
653 gcc_assert (field_index
);
656 inner_type
= TREE_TYPE (field_index
);
658 if (TREE_CODE (value
) == CONSTRUCTOR
)
663 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
664 NULL_TREE
, NULL_TREE
);
666 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
669 if (!split_nonconstant_init_1 (sub
, value
, true))
673 /* Mark element for removal. */
674 CONSTRUCTOR_ELT (init
, idx
)->index
= NULL_TREE
;
680 else if (!initializer_constant_valid_p (value
, inner_type
))
685 /* Mark element for removal. */
686 CONSTRUCTOR_ELT (init
, idx
)->index
= NULL_TREE
;
690 if (TREE_CODE (field_index
) == RANGE_EXPR
)
692 /* Use build_vec_init to initialize a range. */
693 tree low
= TREE_OPERAND (field_index
, 0);
694 tree hi
= TREE_OPERAND (field_index
, 1);
695 sub
= build4 (ARRAY_REF
, inner_type
, dest
, low
,
696 NULL_TREE
, NULL_TREE
);
697 sub
= cp_build_addr_expr (sub
, tf_warning_or_error
);
698 tree max
= size_binop (MINUS_EXPR
, hi
, low
);
699 code
= build_vec_init (sub
, max
, value
, false, 0,
700 tf_warning_or_error
);
702 if (tree_fits_shwi_p (max
))
703 num_split_elts
+= tree_to_shwi (max
);
708 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
709 NULL_TREE
, NULL_TREE
);
711 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
714 if (unsafe_return_slot_p (sub
))
716 /* We may need to add a copy constructor call if
717 the field has [[no_unique_address]]. */
718 releasing_vec args
= make_tree_vector_single (value
);
719 code
= build_special_member_call
720 (sub
, complete_ctor_identifier
, &args
, inner_type
,
721 LOOKUP_NORMAL
, tf_warning_or_error
);
724 code
= build2 (INIT_EXPR
, inner_type
, sub
, value
);
725 code
= build_stmt (input_location
, EXPR_STMT
, code
);
726 code
= maybe_cleanup_point_expr_void (code
);
729 = cxx_maybe_build_cleanup (sub
, tf_warning_or_error
))
730 finish_eh_cleanup (cleanup
);
736 if (num_split_elts
== 1)
737 CONSTRUCTOR_ELTS (init
)->ordered_remove (tidx
);
738 else if (num_split_elts
> 1)
740 /* Perform the delayed ordered removal of non-constant elements
742 for (idx
= tidx
; idx
< CONSTRUCTOR_NELTS (init
); ++idx
)
743 if (CONSTRUCTOR_ELT (init
, idx
)->index
== NULL_TREE
)
747 *CONSTRUCTOR_ELT (init
, tidx
) = *CONSTRUCTOR_ELT (init
, idx
);
750 vec_safe_truncate (CONSTRUCTOR_ELTS (init
), tidx
);
755 if (!initializer_constant_valid_p (init
, type
))
758 tree cons
= copy_node (init
);
759 CONSTRUCTOR_ELTS (init
) = NULL
;
760 code
= build2 (MODIFY_EXPR
, type
, dest
, cons
);
761 code
= build_stmt (input_location
, EXPR_STMT
, code
);
763 num_split_elts
+= CONSTRUCTOR_NELTS (init
);
771 /* The rest of the initializer is now a constant. */
772 TREE_CONSTANT (init
) = 1;
773 TREE_SIDE_EFFECTS (init
) = 0;
775 /* We didn't split out anything. */
776 if (num_split_elts
== 0)
779 return complete_p
&& complete_ctor_at_level_p (TREE_TYPE (init
),
780 num_split_elts
, inner_type
);
783 /* A subroutine of store_init_value. Splits non-constant static
784 initializer INIT into a constant part and generates code to
785 perform the non-constant part of the initialization to DEST.
786 Returns the code for the runtime init. */
789 split_nonconstant_init (tree dest
, tree init
)
793 if (TREE_CODE (init
) == TARGET_EXPR
)
794 init
= TARGET_EXPR_INITIAL (init
);
795 if (TREE_CODE (init
) == CONSTRUCTOR
)
797 init
= cp_fully_fold_init (init
);
798 code
= push_stmt_list ();
799 if (split_nonconstant_init_1 (dest
, init
, false))
801 code
= pop_stmt_list (code
);
802 if (VAR_P (dest
) && !is_local_temp (dest
))
804 DECL_INITIAL (dest
) = init
;
805 TREE_READONLY (dest
) = 0;
809 tree ie
= build2 (INIT_EXPR
, void_type_node
, dest
, init
);
810 code
= add_stmt_to_compound (ie
, code
);
813 else if (TREE_CODE (init
) == STRING_CST
814 && array_of_runtime_bound_p (TREE_TYPE (dest
)))
815 code
= build_vec_init (dest
, NULL_TREE
, init
, /*value-init*/false,
816 /*from array*/1, tf_warning_or_error
);
818 code
= build2 (INIT_EXPR
, TREE_TYPE (dest
), dest
, init
);
823 /* Perform appropriate conversions on the initial value of a variable,
824 store it in the declaration DECL,
825 and print any error messages that are appropriate.
826 If the init is invalid, store an ERROR_MARK.
828 C++: Note that INIT might be a TREE_LIST, which would mean that it is
829 a base class initializer for some aggregate type, hopefully compatible
830 with DECL. If INIT is a single element, and DECL is an aggregate
831 type, we silently convert INIT into a TREE_LIST, allowing a constructor
834 If INIT is a TREE_LIST and there is no constructor, turn INIT
835 into a CONSTRUCTOR and use standard initialization techniques.
836 Perhaps a warning should be generated?
838 Returns code to be executed if initialization could not be performed
839 for static variable. In that case, caller must emit the code. */
842 store_init_value (tree decl
, tree init
, vec
<tree
, va_gc
>** cleanups
, int flags
)
846 /* If variable's type was invalidly declared, just ignore it. */
848 type
= TREE_TYPE (decl
);
849 if (TREE_CODE (type
) == ERROR_MARK
)
852 if (MAYBE_CLASS_TYPE_P (type
))
854 if (TREE_CODE (init
) == TREE_LIST
)
856 error ("constructor syntax used, but no constructor declared "
857 "for type %qT", type
);
858 init
= build_constructor_from_list (init_list_type_node
, nreverse (init
));
862 /* End of special C++ code. */
864 if (flags
& LOOKUP_ALREADY_DIGESTED
)
868 if (TREE_STATIC (decl
))
869 flags
|= LOOKUP_ALLOW_FLEXARRAY_INIT
;
870 /* Digest the specified initializer into an expression. */
871 value
= digest_init_flags (type
, init
, flags
, tf_warning_or_error
);
874 /* Look for braced array initializers for character arrays and
875 recursively convert them into STRING_CSTs. */
876 value
= braced_lists_to_strings (type
, value
);
878 current_ref_temp_count
= 0;
879 value
= extend_ref_init_temps (decl
, value
, cleanups
);
881 /* In C++11 constant expression is a semantic, not syntactic, property.
882 In C++98, make sure that what we thought was a constant expression at
883 template definition time is still constant and otherwise perform this
884 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
885 if (decl_maybe_constant_var_p (decl
) || TREE_STATIC (decl
))
889 value
= fold_non_dependent_expr (value
, tf_warning_or_error
, true, decl
);
890 if (DECL_DECLARED_CONSTEXPR_P (decl
)
891 || (DECL_IN_AGGR_P (decl
)
892 && DECL_INITIALIZED_IN_CLASS_P (decl
)))
894 /* Diagnose a non-constant initializer for constexpr variable or
895 non-inline in-class-initialized static data member. */
896 if (!require_constant_expression (value
))
897 value
= error_mark_node
;
898 else if (processing_template_decl
)
899 /* In a template we might not have done the necessary
900 transformations to make value actually constant,
901 e.g. extend_ref_init_temps. */
902 value
= maybe_constant_init (value
, decl
, true);
904 value
= cxx_constant_init (value
, decl
);
907 value
= maybe_constant_init (value
, decl
, true);
908 if (TREE_CODE (value
) == CONSTRUCTOR
&& cp_has_mutable_p (type
))
909 /* Poison this CONSTRUCTOR so it can't be copied to another
910 constexpr variable. */
911 CONSTRUCTOR_MUTABLE_POISON (value
) = true;
912 const_init
= (reduced_constant_expression_p (value
)
913 || error_operand_p (value
));
914 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = const_init
;
915 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
916 if (!TYPE_REF_P (type
))
917 TREE_CONSTANT (decl
) = const_init
&& decl_maybe_constant_var_p (decl
);
920 /* [dcl.constinit]/2 "If a variable declared with the constinit
921 specifier has dynamic initialization, the program is
923 if (DECL_DECLARED_CONSTINIT_P (decl
))
925 error_at (location_of (decl
),
926 "%<constinit%> variable %qD does not have a constant "
927 "initializer", decl
);
928 if (require_constant_expression (value
))
929 cxx_constant_init (value
, decl
);
930 value
= error_mark_node
;
936 /* Don't fold initializers of automatic variables in constexpr functions,
937 that might fold away something that needs to be diagnosed at constexpr
939 if (!current_function_decl
940 || !DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
941 || TREE_STATIC (decl
))
942 value
= cp_fully_fold_init (value
);
944 /* Handle aggregate NSDMI in non-constant initializers, too. */
945 value
= replace_placeholders (value
, decl
);
947 /* If the initializer is not a constant, fill in DECL_INITIAL with
948 the bits that are constant, and then return an expression that
949 will perform the dynamic initialization. */
950 if (value
!= error_mark_node
951 && (TREE_SIDE_EFFECTS (value
)
953 || ! reduced_constant_expression_p (value
)))
954 return split_nonconstant_init (decl
, value
);
956 /* DECL may change value; purge caches. */
957 clear_cv_and_fold_caches (TREE_STATIC (decl
));
959 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
960 is an automatic variable, the middle end will turn this into a
961 dynamic initialization later. */
962 DECL_INITIAL (decl
) = value
;
967 /* Give diagnostic about narrowing conversions within { }, or as part of
968 a converted constant expression. If CONST_ONLY, only check
972 check_narrowing (tree type
, tree init
, tsubst_flags_t complain
,
973 bool const_only
/*= false*/)
975 tree ftype
= unlowered_expr_type (init
);
979 if (((!warn_narrowing
|| !(complain
& tf_warning
))
980 && cxx_dialect
== cxx98
)
981 || !ARITHMETIC_TYPE_P (type
)
982 /* Don't emit bogus warnings with e.g. value-dependent trees. */
983 || instantiation_dependent_expression_p (init
))
986 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
987 && TREE_CODE (type
) == COMPLEX_TYPE
)
989 tree elttype
= TREE_TYPE (type
);
990 if (CONSTRUCTOR_NELTS (init
) > 0)
991 ok
&= check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 0)->value
,
993 if (CONSTRUCTOR_NELTS (init
) > 1)
994 ok
&= check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 1)->value
,
999 /* Even non-dependent expressions can still have template
1000 codes like CAST_EXPR, so use *_non_dependent_expr to cope. */
1001 init
= fold_non_dependent_expr (init
, complain
);
1002 if (init
== error_mark_node
)
1005 /* If we were asked to only check constants, return early. */
1006 if (const_only
&& !TREE_CONSTANT (init
))
1009 if (CP_INTEGRAL_TYPE_P (type
)
1010 && TREE_CODE (ftype
) == REAL_TYPE
)
1012 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
1013 && CP_INTEGRAL_TYPE_P (type
))
1015 if (TREE_CODE (ftype
) == ENUMERAL_TYPE
)
1016 /* Check for narrowing based on the values of the enumeration. */
1017 ftype
= ENUM_UNDERLYING_TYPE (ftype
);
1018 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type
),
1019 TYPE_MAX_VALUE (ftype
))
1020 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype
),
1021 TYPE_MIN_VALUE (type
)))
1022 && (TREE_CODE (init
) != INTEGER_CST
1023 || !int_fits_type_p (init
, type
)))
1026 /* [dcl.init.list]#7.2: "from long double to double or float, or from
1027 double to float". */
1028 else if (TREE_CODE (ftype
) == REAL_TYPE
1029 && TREE_CODE (type
) == REAL_TYPE
)
1031 if ((same_type_p (ftype
, long_double_type_node
)
1032 && (same_type_p (type
, double_type_node
)
1033 || same_type_p (type
, float_type_node
)))
1034 || (same_type_p (ftype
, double_type_node
)
1035 && same_type_p (type
, float_type_node
))
1036 || (TYPE_PRECISION (type
) < TYPE_PRECISION (ftype
)))
1038 if (TREE_CODE (init
) == REAL_CST
)
1040 /* Issue 703: Loss of precision is OK as long as the value is
1041 within the representable range of the new type. */
1043 d
= TREE_REAL_CST (init
);
1044 real_convert (&r
, TYPE_MODE (type
), &d
);
1045 if (real_isinf (&r
))
1052 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
1053 && TREE_CODE (type
) == REAL_TYPE
)
1056 if (TREE_CODE (init
) == INTEGER_CST
)
1058 d
= real_value_from_int_cst (0, init
);
1059 if (exact_real_truncate (TYPE_MODE (type
), &d
))
1063 else if (TREE_CODE (type
) == BOOLEAN_TYPE
1064 && (TYPE_PTR_P (ftype
) || TYPE_PTRMEM_P (ftype
)))
1065 /* C++20 P1957R2: converting from a pointer type or a pointer-to-member
1066 type to bool should be considered narrowing. This is a DR so is not
1067 limited to C++20 only. */
1070 bool almost_ok
= ok
;
1071 if (!ok
&& !CONSTANT_CLASS_P (init
) && (complain
& tf_warning_or_error
))
1073 tree folded
= cp_fully_fold (init
);
1074 if (TREE_CONSTANT (folded
) && check_narrowing (type
, folded
, tf_none
))
1080 location_t loc
= cp_expr_loc_or_input_loc (init
);
1081 if (cxx_dialect
== cxx98
)
1083 if (complain
& tf_warning
)
1084 warning_at (loc
, OPT_Wnarrowing
, "narrowing conversion of %qE "
1085 "from %qH to %qI is ill-formed in C++11",
1089 else if (!CONSTANT_CLASS_P (init
))
1091 if (complain
& tf_warning_or_error
)
1093 auto_diagnostic_group d
;
1094 if ((!almost_ok
|| pedantic
)
1095 && pedwarn (loc
, OPT_Wnarrowing
,
1096 "narrowing conversion of %qE from %qH to %qI",
1099 inform (loc
, " the expression has a constant value but is not "
1100 "a C++ constant-expression");
1104 else if (complain
& tf_error
)
1106 int savederrorcount
= errorcount
;
1107 global_dc
->pedantic_errors
= 1;
1108 pedwarn (loc
, OPT_Wnarrowing
,
1109 "narrowing conversion of %qE from %qH to %qI",
1111 if (errorcount
== savederrorcount
)
1113 global_dc
->pedantic_errors
= flag_pedantic_errors
;
1120 /* True iff TYPE is a C++20 "ordinary" character type. */
1123 ordinary_char_type_p (tree type
)
1125 type
= TYPE_MAIN_VARIANT (type
);
1126 return (type
== char_type_node
1127 || type
== signed_char_type_node
1128 || type
== unsigned_char_type_node
);
1131 /* Process the initializer INIT for a variable of type TYPE, emitting
1132 diagnostics for invalid initializers and converting the initializer as
1135 For aggregate types, it assumes that reshape_init has already run, thus the
1136 initializer will have the right shape (brace elision has been undone).
1138 NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR,
1139 2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR. */
1142 digest_init_r (tree type
, tree init
, int nested
, int flags
,
1143 tsubst_flags_t complain
)
1145 enum tree_code code
= TREE_CODE (type
);
1147 if (error_operand_p (init
))
1148 return error_mark_node
;
1152 /* We must strip the outermost array type when completing the type,
1153 because the its bounds might be incomplete at the moment. */
1154 if (!complete_type_or_maybe_complain (code
== ARRAY_TYPE
1155 ? TREE_TYPE (type
) : type
, NULL_TREE
,
1157 return error_mark_node
;
1159 location_t loc
= cp_expr_loc_or_input_loc (init
);
1161 tree stripped_init
= init
;
1163 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
1164 && CONSTRUCTOR_IS_PAREN_INIT (init
))
1165 flags
|= LOOKUP_AGGREGATE_PAREN_INIT
;
1167 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
1168 (g++.old-deja/g++.law/casts2.C). */
1169 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
1170 stripped_init
= TREE_OPERAND (init
, 0);
1172 stripped_init
= tree_strip_any_location_wrapper (stripped_init
);
1174 /* Initialization of an array of chars from a string constant. The initializer
1175 can be optionally enclosed in braces, but reshape_init has already removed
1176 them if they were present. */
1177 if (code
== ARRAY_TYPE
)
1179 if (nested
&& !TYPE_DOMAIN (type
))
1180 /* C++ flexible array members have a null domain. */
1182 if (flags
& LOOKUP_ALLOW_FLEXARRAY_INIT
)
1183 pedwarn (loc
, OPT_Wpedantic
,
1184 "initialization of a flexible array member");
1187 if (complain
& tf_error
)
1188 error_at (loc
, "non-static initialization of"
1189 " a flexible array member");
1190 return error_mark_node
;
1194 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
1195 if (char_type_p (typ1
)
1196 && TREE_CODE (stripped_init
) == STRING_CST
)
1198 tree char_type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init
)));
1199 bool incompat_string_cst
= false;
1201 if (typ1
!= char_type
)
1203 /* The array element type does not match the initializing string
1204 literal element type; this is only allowed when both types are
1205 ordinary character type. There are no string literals of
1206 signed or unsigned char type in the language, but we can get
1207 them internally from converting braced-init-lists to
1209 if (ordinary_char_type_p (typ1
)
1210 && ordinary_char_type_p (char_type
))
1213 incompat_string_cst
= true;
1216 if (incompat_string_cst
)
1218 if (complain
& tf_error
)
1219 error_at (loc
, "cannot initialize array of %qT from "
1220 "a string literal with type array of %qT",
1222 return error_mark_node
;
1225 if (nested
== 2 && !TYPE_DOMAIN (type
))
1227 if (complain
& tf_error
)
1228 error_at (loc
, "initialization of flexible array member "
1229 "in a nested context");
1230 return error_mark_node
;
1233 if (type
!= TREE_TYPE (init
)
1234 && !variably_modified_type_p (type
, NULL_TREE
))
1236 init
= copy_node (init
);
1237 TREE_TYPE (init
) = type
;
1238 /* If we have a location wrapper, then also copy the wrapped
1239 node, and update the copy's type. */
1240 if (location_wrapper_p (init
))
1242 stripped_init
= copy_node (stripped_init
);
1243 TREE_OPERAND (init
, 0) = stripped_init
;
1244 TREE_TYPE (stripped_init
) = type
;
1247 if (TYPE_DOMAIN (type
) && TREE_CONSTANT (TYPE_SIZE (type
)))
1249 /* Not a flexible array member. */
1250 int size
= TREE_INT_CST_LOW (TYPE_SIZE (type
));
1251 size
= (size
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
;
1252 /* In C it is ok to subtract 1 from the length of the string
1253 because it's ok to ignore the terminating null char that is
1254 counted in the length of the constant, but in C++ this would
1256 if (size
< TREE_STRING_LENGTH (stripped_init
))
1258 permerror (loc
, "initializer-string for %qT is too long",
1261 init
= build_string (size
,
1262 TREE_STRING_POINTER (stripped_init
));
1263 TREE_TYPE (init
) = type
;
1270 /* Handle scalar types (including conversions) and references. */
1271 if ((code
!= COMPLEX_TYPE
|| BRACE_ENCLOSED_INITIALIZER_P (stripped_init
))
1272 && (SCALAR_TYPE_P (type
) || code
== REFERENCE_TYPE
))
1274 /* Narrowing is OK when initializing an aggregate from
1275 a parenthesized list. */
1276 if (nested
&& !(flags
& LOOKUP_AGGREGATE_PAREN_INIT
))
1277 flags
|= LOOKUP_NO_NARROWING
;
1278 init
= convert_for_initialization (0, type
, init
, flags
,
1279 ICR_INIT
, NULL_TREE
, 0,
1285 /* Come here only for aggregates: records, arrays, unions, complex numbers
1287 gcc_assert (code
== ARRAY_TYPE
1288 || VECTOR_TYPE_P (type
)
1289 || code
== RECORD_TYPE
1290 || code
== UNION_TYPE
1291 || code
== COMPLEX_TYPE
);
1293 /* "If T is a class type and the initializer list has a single
1294 element of type cv U, where U is T or a class derived from T,
1295 the object is initialized from that element." */
1296 if (cxx_dialect
>= cxx11
1297 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init
)
1298 && CONSTRUCTOR_NELTS (stripped_init
) == 1
1299 && ((CLASS_TYPE_P (type
) && !CLASSTYPE_NON_AGGREGATE (type
))
1300 || VECTOR_TYPE_P (type
)))
1302 tree elt
= CONSTRUCTOR_ELT (stripped_init
, 0)->value
;
1303 if (reference_related_p (type
, TREE_TYPE (elt
)))
1305 /* In C++17, aggregates can have bases, thus participate in
1306 aggregate initialization. In the following case:
1308 struct B { int c; };
1312 there's an extra set of braces, so the D temporary initializes
1313 the first element of d, which is the B base subobject. The base
1314 of type B is copy-initialized from the D temporary, causing
1316 tree field
= next_initializable_field (TYPE_FIELDS (type
));
1317 if (field
&& DECL_FIELD_IS_BASE (field
))
1319 if (warning_at (loc
, 0, "initializing a base class of type %qT "
1320 "results in object slicing", TREE_TYPE (field
)))
1321 inform (loc
, "remove %<{ }%> around initializer");
1323 else if (flag_checking
)
1324 /* We should have fixed this in reshape_init. */
1329 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init
)
1330 && !TYPE_NON_AGGREGATE_CLASS (type
))
1331 return process_init_constructor (type
, stripped_init
, nested
, flags
,
1335 if (COMPOUND_LITERAL_P (stripped_init
) && code
== ARRAY_TYPE
)
1337 if (complain
& tf_error
)
1338 error_at (loc
, "cannot initialize aggregate of type %qT with "
1339 "a compound literal", type
);
1341 return error_mark_node
;
1344 if (code
== ARRAY_TYPE
1345 && !BRACE_ENCLOSED_INITIALIZER_P (stripped_init
))
1347 /* Allow the result of build_array_copy and of
1348 build_value_init_noctor. */
1349 if ((TREE_CODE (stripped_init
) == VEC_INIT_EXPR
1350 || TREE_CODE (stripped_init
) == CONSTRUCTOR
)
1351 && (same_type_ignoring_top_level_qualifiers_p
1352 (type
, TREE_TYPE (init
))))
1355 if (complain
& tf_error
)
1356 error_at (loc
, "array must be initialized with a brace-enclosed"
1358 return error_mark_node
;
1361 return convert_for_initialization (NULL_TREE
, type
, init
,
1363 ICR_INIT
, NULL_TREE
, 0,
1369 digest_init (tree type
, tree init
, tsubst_flags_t complain
)
1371 return digest_init_r (type
, init
, 0, LOOKUP_IMPLICIT
, complain
);
1375 digest_init_flags (tree type
, tree init
, int flags
, tsubst_flags_t complain
)
1377 return digest_init_r (type
, init
, 0, flags
, complain
);
1380 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1382 digest_nsdmi_init (tree decl
, tree init
, tsubst_flags_t complain
)
1384 gcc_assert (TREE_CODE (decl
) == FIELD_DECL
);
1386 tree type
= TREE_TYPE (decl
);
1387 if (DECL_BIT_FIELD_TYPE (decl
))
1388 type
= DECL_BIT_FIELD_TYPE (decl
);
1389 int flags
= LOOKUP_IMPLICIT
;
1390 if (DIRECT_LIST_INIT_P (init
))
1392 flags
= LOOKUP_NORMAL
;
1393 complain
|= tf_no_cleanup
;
1395 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
1396 && CP_AGGREGATE_TYPE_P (type
))
1397 init
= reshape_init (type
, init
, complain
);
1398 init
= digest_init_flags (type
, init
, flags
, complain
);
1402 /* Set of flags used within process_init_constructor to describe the
1404 #define PICFLAG_ERRONEOUS 1
1405 #define PICFLAG_NOT_ALL_CONSTANT 2
1406 #define PICFLAG_NOT_ALL_SIMPLE 4
1407 #define PICFLAG_SIDE_EFFECTS 8
1409 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1413 picflag_from_initializer (tree init
)
1415 if (init
== error_mark_node
)
1416 return PICFLAG_ERRONEOUS
;
1417 else if (!TREE_CONSTANT (init
))
1419 if (TREE_SIDE_EFFECTS (init
))
1420 return PICFLAG_SIDE_EFFECTS
;
1422 return PICFLAG_NOT_ALL_CONSTANT
;
1424 else if (!initializer_constant_valid_p (init
, TREE_TYPE (init
)))
1425 return PICFLAG_NOT_ALL_SIMPLE
;
1429 /* Adjust INIT for going into a CONSTRUCTOR. */
1432 massage_init_elt (tree type
, tree init
, int nested
, int flags
,
1433 tsubst_flags_t complain
)
1435 int new_flags
= LOOKUP_IMPLICIT
;
1436 if (flags
& LOOKUP_ALLOW_FLEXARRAY_INIT
)
1437 new_flags
|= LOOKUP_ALLOW_FLEXARRAY_INIT
;
1438 if (flags
& LOOKUP_AGGREGATE_PAREN_INIT
)
1439 new_flags
|= LOOKUP_AGGREGATE_PAREN_INIT
;
1440 init
= digest_init_r (type
, init
, nested
? 2 : 1, new_flags
, complain
);
1441 /* Strip a simple TARGET_EXPR when we know this is an initializer. */
1442 if (SIMPLE_TARGET_EXPR_P (init
))
1443 init
= TARGET_EXPR_INITIAL (init
);
1444 /* When we defer constant folding within a statement, we may want to
1445 defer this folding as well. */
1446 tree t
= fold_non_dependent_init (init
, complain
);
1447 if (TREE_CONSTANT (t
))
1452 /* Subroutine of process_init_constructor, which will process an initializer
1453 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1454 which describe the initializers. */
1457 process_init_constructor_array (tree type
, tree init
, int nested
, int flags
,
1458 tsubst_flags_t complain
)
1460 unsigned HOST_WIDE_INT i
, len
= 0;
1462 bool unbounded
= false;
1463 constructor_elt
*ce
;
1464 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (init
);
1466 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
1467 || VECTOR_TYPE_P (type
));
1469 if (TREE_CODE (type
) == ARRAY_TYPE
)
1471 /* C++ flexible array members have a null domain. */
1472 tree domain
= TYPE_DOMAIN (type
);
1473 if (domain
&& TREE_CONSTANT (TYPE_MAX_VALUE (domain
)))
1474 len
= wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain
))
1475 - wi::to_offset (TYPE_MIN_VALUE (domain
)) + 1,
1476 TYPE_PRECISION (TREE_TYPE (domain
)),
1477 TYPE_SIGN (TREE_TYPE (domain
))).to_uhwi ();
1479 unbounded
= true; /* Take as many as there are. */
1481 if (nested
== 2 && !domain
&& !vec_safe_is_empty (v
))
1483 if (complain
& tf_error
)
1484 error_at (cp_expr_loc_or_input_loc (init
),
1485 "initialization of flexible array member "
1486 "in a nested context");
1487 return PICFLAG_ERRONEOUS
;
1491 /* Vectors are like simple fixed-size arrays. */
1492 unbounded
= !TYPE_VECTOR_SUBPARTS (type
).is_constant (&len
);
1494 /* There must not be more initializers than needed. */
1495 if (!unbounded
&& vec_safe_length (v
) > len
)
1497 if (complain
& tf_error
)
1498 error ("too many initializers for %qT", type
);
1500 return PICFLAG_ERRONEOUS
;
1503 FOR_EACH_VEC_SAFE_ELT (v
, i
, ce
)
1506 ce
->index
= size_int (i
);
1507 else if (!check_array_designated_initializer (ce
, i
))
1508 ce
->index
= error_mark_node
;
1509 gcc_assert (ce
->value
);
1511 = massage_init_elt (TREE_TYPE (type
), ce
->value
, nested
, flags
,
1515 (ce
->value
== error_mark_node
1516 || (same_type_ignoring_top_level_qualifiers_p
1517 (strip_array_types (TREE_TYPE (type
)),
1518 strip_array_types (TREE_TYPE (ce
->value
)))));
1520 picflags
|= picflag_from_initializer (ce
->value
);
1523 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1524 we must add initializers ourselves. */
1526 for (; i
< len
; ++i
)
1530 if (type_build_ctor_call (TREE_TYPE (type
)))
1532 /* If this type needs constructors run for default-initialization,
1533 we can't rely on the back end to do it for us, so make the
1534 initialization explicit by list-initializing from T{}. */
1535 next
= build_constructor (init_list_type_node
, NULL
);
1536 next
= massage_init_elt (TREE_TYPE (type
), next
, nested
, flags
,
1538 if (initializer_zerop (next
))
1539 /* The default zero-initialization is fine for us; don't
1540 add anything to the CONSTRUCTOR. */
1543 else if (!zero_init_p (TREE_TYPE (type
)))
1544 next
= build_zero_init (TREE_TYPE (type
),
1545 /*nelts=*/NULL_TREE
,
1546 /*static_storage_p=*/false);
1548 /* The default zero-initialization is fine for us; don't
1549 add anything to the CONSTRUCTOR. */
1554 picflags
|= picflag_from_initializer (next
);
1556 && (initializer_constant_valid_p (next
, TREE_TYPE (next
))
1557 == null_pointer_node
))
1559 tree range
= build2 (RANGE_EXPR
, size_type_node
,
1560 build_int_cst (size_type_node
, i
),
1561 build_int_cst (size_type_node
, len
- 1));
1562 CONSTRUCTOR_APPEND_ELT (v
, range
, next
);
1566 CONSTRUCTOR_APPEND_ELT (v
, size_int (i
), next
);
1569 /* Don't bother checking all the other elements. */
1573 CONSTRUCTOR_ELTS (init
) = v
;
1577 /* Subroutine of process_init_constructor, which will process an initializer
1578 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1579 the initializers. */
1582 process_init_constructor_record (tree type
, tree init
, int nested
, int flags
,
1583 tsubst_flags_t complain
)
1585 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1589 gcc_assert (TREE_CODE (type
) == RECORD_TYPE
);
1590 gcc_assert (!CLASSTYPE_VBASECLASSES (type
));
1591 gcc_assert (!TYPE_BINFO (type
)
1592 || cxx_dialect
>= cxx17
1593 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)));
1594 gcc_assert (!TYPE_POLYMORPHIC_P (type
));
1598 unsigned HOST_WIDE_INT idx
= 0;
1599 int designator_skip
= -1;
1600 /* Generally, we will always have an index for each initializer (which is
1601 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1602 reshape_init. So we need to handle both cases. */
1603 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1608 if (TREE_CODE (field
) != FIELD_DECL
1609 || (DECL_ARTIFICIAL (field
)
1610 && !(cxx_dialect
>= cxx17
&& DECL_FIELD_IS_BASE (field
))))
1613 if (DECL_UNNAMED_BIT_FIELD (field
))
1616 /* If this is a bitfield, first convert to the declared type. */
1617 type
= TREE_TYPE (field
);
1618 if (DECL_BIT_FIELD_TYPE (field
))
1619 type
= DECL_BIT_FIELD_TYPE (field
);
1620 if (type
== error_mark_node
)
1621 return PICFLAG_ERRONEOUS
;
1624 if (idx
< CONSTRUCTOR_NELTS (init
))
1626 constructor_elt
*ce
= &(*CONSTRUCTOR_ELTS (init
))[idx
];
1629 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1630 latter case can happen in templates where lookup has to be
1632 gcc_assert (TREE_CODE (ce
->index
) == FIELD_DECL
1633 || identifier_p (ce
->index
));
1634 if (ce
->index
== field
|| ce
->index
== DECL_NAME (field
))
1636 else if (ANON_AGGR_TYPE_P (type
)
1637 && search_anon_aggr (type
,
1638 TREE_CODE (ce
->index
) == FIELD_DECL
1639 ? DECL_NAME (ce
->index
)
1641 /* If the element is an anonymous union object and the
1642 initializer list is a designated-initializer-list, the
1643 anonymous union object is initialized by the
1644 designated-initializer-list { D }, where D is the
1645 designated-initializer-clause naming a member of the
1646 anonymous union object. */
1647 next
= build_constructor_single (init_list_type_node
,
1648 ce
->index
, ce
->value
);
1652 if (designator_skip
== -1)
1653 designator_skip
= 1;
1658 designator_skip
= 0;
1664 gcc_assert (ce
->value
);
1665 next
= massage_init_elt (type
, next
, nested
, flags
, complain
);
1669 if (next
== error_mark_node
)
1670 /* We skip initializers for empty bases/fields, so skipping an invalid
1671 one could make us accept invalid code. */
1672 return PICFLAG_ERRONEOUS
;
1674 /* Already handled above. */;
1675 else if (DECL_INITIAL (field
))
1679 /* We're using an NSDMI past a field with implicit
1680 zero-init. Go back and make it explicit. */
1682 vec_safe_truncate (v
, 0);
1685 /* C++14 aggregate NSDMI. */
1686 next
= get_nsdmi (field
, /*ctor*/false, complain
);
1687 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init
)
1688 && find_placeholders (next
))
1689 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init
) = 1;
1691 else if (type_build_ctor_call (TREE_TYPE (field
)))
1693 /* If this type needs constructors run for
1694 default-initialization, we can't rely on the back end to do it
1695 for us, so build up TARGET_EXPRs. If the type in question is
1696 a class, just build one up; if it's an array, recurse. */
1697 next
= build_constructor (init_list_type_node
, NULL
);
1698 next
= massage_init_elt (TREE_TYPE (field
), next
, nested
, flags
,
1701 /* Warn when some struct elements are implicitly initialized. */
1702 if ((complain
& tf_warning
)
1703 && !EMPTY_CONSTRUCTOR_P (init
))
1704 warning (OPT_Wmissing_field_initializers
,
1705 "missing initializer for member %qD", field
);
1709 const_tree fldtype
= TREE_TYPE (field
);
1710 if (TYPE_REF_P (fldtype
))
1712 if (complain
& tf_error
)
1713 error ("member %qD is uninitialized reference", field
);
1715 return PICFLAG_ERRONEOUS
;
1717 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype
))
1719 if (complain
& tf_error
)
1720 error ("member %qD with uninitialized reference fields", field
);
1722 return PICFLAG_ERRONEOUS
;
1724 /* Do nothing for flexible array members since they need not have any
1725 elements. Don't worry about 'skipped' because a flexarray has to
1726 be the last field. */
1727 else if (TREE_CODE (fldtype
) == ARRAY_TYPE
&& !TYPE_DOMAIN (fldtype
))
1730 /* Warn when some struct elements are implicitly initialized
1732 if ((complain
& tf_warning
)
1733 && !EMPTY_CONSTRUCTOR_P (init
))
1734 warning (OPT_Wmissing_field_initializers
,
1735 "missing initializer for member %qD", field
);
1737 if (!zero_init_p (fldtype
)
1739 next
= build_zero_init (TREE_TYPE (field
), /*nelts=*/NULL_TREE
,
1740 /*static_storage_p=*/false);
1743 /* The default zero-initialization is fine for us; don't
1744 add anything to the CONSTRUCTOR. */
1750 if (DECL_SIZE (field
) && integer_zerop (DECL_SIZE (field
))
1751 && !TREE_SIDE_EFFECTS (next
))
1752 /* Don't add trivial initialization of an empty base/field to the
1753 constructor, as they might not be ordered the way the back-end
1757 /* If this is a bitfield, now convert to the lowered type. */
1758 if (type
!= TREE_TYPE (field
))
1759 next
= cp_convert_and_check (TREE_TYPE (field
), next
, complain
);
1760 picflags
|= picflag_from_initializer (next
);
1761 CONSTRUCTOR_APPEND_ELT (v
, field
, next
);
1764 if (idx
< CONSTRUCTOR_NELTS (init
))
1766 if (complain
& tf_error
)
1768 constructor_elt
*ce
= &(*CONSTRUCTOR_ELTS (init
))[idx
];
1769 /* For better diagnostics, try to find out if it is really
1770 the case of too many initializers or if designators are
1771 in incorrect order. */
1772 if (designator_skip
== 1 && ce
->index
)
1774 gcc_assert (TREE_CODE (ce
->index
) == FIELD_DECL
1775 || identifier_p (ce
->index
));
1776 for (field
= TYPE_FIELDS (type
);
1777 field
; field
= DECL_CHAIN (field
))
1779 if (TREE_CODE (field
) != FIELD_DECL
1780 || (DECL_ARTIFICIAL (field
)
1781 && !(cxx_dialect
>= cxx17
1782 && DECL_FIELD_IS_BASE (field
))))
1785 if (DECL_UNNAMED_BIT_FIELD (field
))
1788 if (ce
->index
== field
|| ce
->index
== DECL_NAME (field
))
1790 if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1793 = search_anon_aggr (TREE_TYPE (field
),
1794 TREE_CODE (ce
->index
) == FIELD_DECL
1795 ? DECL_NAME (ce
->index
)
1806 error ("designator order for field %qD does not match declaration "
1807 "order in %qT", field
, type
);
1809 error ("too many initializers for %qT", type
);
1812 return PICFLAG_ERRONEOUS
;
1815 CONSTRUCTOR_ELTS (init
) = v
;
1819 /* Subroutine of process_init_constructor, which will process a single
1820 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1821 which describe the initializer. */
1824 process_init_constructor_union (tree type
, tree init
, int nested
, int flags
,
1825 tsubst_flags_t complain
)
1827 constructor_elt
*ce
;
1830 /* If the initializer was empty, use the union's NSDMI if it has one.
1831 Otherwise use default zero initialization. */
1832 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
1834 for (tree field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1836 if (TREE_CODE (field
) == FIELD_DECL
1837 && DECL_INITIAL (field
) != NULL_TREE
)
1839 tree val
= get_nsdmi (field
, /*in_ctor=*/false, complain
);
1840 if (!CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init
)
1841 && find_placeholders (val
))
1842 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (init
) = 1;
1843 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init
), field
, val
);
1848 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
1852 len
= CONSTRUCTOR_ELTS (init
)->length ();
1855 if (!(complain
& tf_error
))
1856 return PICFLAG_ERRONEOUS
;
1857 error ("too many initializers for %qT", type
);
1858 CONSTRUCTOR_ELTS (init
)->block_remove (1, len
-1);
1861 ce
= &(*CONSTRUCTOR_ELTS (init
))[0];
1863 /* If this element specifies a field, initialize via that field. */
1866 if (TREE_CODE (ce
->index
) == FIELD_DECL
)
1868 else if (identifier_p (ce
->index
))
1870 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1871 tree name
= ce
->index
;
1873 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1874 if (DECL_NAME (field
) == name
)
1878 if (complain
& tf_error
)
1879 error ("no field %qD found in union being initialized",
1881 ce
->value
= error_mark_node
;
1887 gcc_assert (TREE_CODE (ce
->index
) == INTEGER_CST
1888 || TREE_CODE (ce
->index
) == RANGE_EXPR
);
1889 if (complain
& tf_error
)
1890 error ("index value instead of field name in union initializer");
1891 ce
->value
= error_mark_node
;
1896 /* Find the first named field. ANSI decided in September 1990
1897 that only named fields count here. */
1898 tree field
= TYPE_FIELDS (type
);
1899 while (field
&& (!DECL_NAME (field
) || TREE_CODE (field
) != FIELD_DECL
))
1900 field
= TREE_CHAIN (field
);
1901 if (field
== NULL_TREE
)
1903 if (complain
& tf_error
)
1904 error ("too many initializers for %qT", type
);
1905 ce
->value
= error_mark_node
;
1910 if (ce
->value
&& ce
->value
!= error_mark_node
)
1911 ce
->value
= massage_init_elt (TREE_TYPE (ce
->index
), ce
->value
, nested
,
1914 return picflag_from_initializer (ce
->value
);
1917 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1918 constructor is a brace-enclosed initializer, and will be modified in-place.
1920 Each element is converted to the right type through digest_init, and
1921 missing initializers are added following the language rules (zero-padding,
1924 After the execution, the initializer will have TREE_CONSTANT if all elts are
1925 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1926 constants that the assembler and linker can compute them.
1928 The function returns the initializer itself, or error_mark_node in case
1932 process_init_constructor (tree type
, tree init
, int nested
, int flags
,
1933 tsubst_flags_t complain
)
1937 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init
));
1939 if (TREE_CODE (type
) == ARRAY_TYPE
|| VECTOR_TYPE_P (type
))
1940 picflags
= process_init_constructor_array (type
, init
, nested
, flags
,
1942 else if (TREE_CODE (type
) == RECORD_TYPE
)
1943 picflags
= process_init_constructor_record (type
, init
, nested
, flags
,
1945 else if (TREE_CODE (type
) == UNION_TYPE
)
1946 picflags
= process_init_constructor_union (type
, init
, nested
, flags
,
1951 if (picflags
& PICFLAG_ERRONEOUS
)
1952 return error_mark_node
;
1954 TREE_TYPE (init
) = type
;
1955 if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
) == NULL_TREE
)
1956 cp_complete_array_type (&TREE_TYPE (init
), init
, /*do_default=*/0);
1957 if (picflags
& PICFLAG_SIDE_EFFECTS
)
1959 TREE_CONSTANT (init
) = false;
1960 TREE_SIDE_EFFECTS (init
) = true;
1962 else if (picflags
& PICFLAG_NOT_ALL_CONSTANT
)
1964 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1965 TREE_CONSTANT (init
) = false;
1966 TREE_SIDE_EFFECTS (init
) = false;
1970 TREE_CONSTANT (init
) = 1;
1971 TREE_SIDE_EFFECTS (init
) = false;
1972 if (!(picflags
& PICFLAG_NOT_ALL_SIMPLE
))
1973 TREE_STATIC (init
) = 1;
1978 /* Given a structure or union value DATUM, construct and return
1979 the structure or union component which results from narrowing
1980 that value to the base specified in BASETYPE. For example, given the
1983 class L { int ii; };
1984 class A : L { ... };
1985 class B : L { ... };
1986 class C : A, B { ... };
1994 x.A::ii refers to the ii member of the L part of
1995 the A part of the C object named by X. In this case,
1996 DATUM would be x, and BASETYPE would be A.
1998 I used to think that this was nonconformant, that the standard specified
1999 that first we look up ii in A, then convert x to an L& and pull out the
2000 ii part. But in fact, it does say that we convert x to an A&; A here
2001 is known as the "naming class". (jason 2000-12-19)
2003 BINFO_P points to a variable initialized either to NULL_TREE or to the
2004 binfo for the specific base subobject we want to convert to. */
2007 build_scoped_ref (tree datum
, tree basetype
, tree
* binfo_p
)
2011 if (datum
== error_mark_node
)
2012 return error_mark_node
;
2016 binfo
= lookup_base (TREE_TYPE (datum
), basetype
, ba_check
,
2017 NULL
, tf_warning_or_error
);
2019 if (!binfo
|| binfo
== error_mark_node
)
2021 *binfo_p
= NULL_TREE
;
2023 error_not_base_type (basetype
, TREE_TYPE (datum
));
2024 return error_mark_node
;
2028 return build_base_path (PLUS_EXPR
, datum
, binfo
, 1,
2029 tf_warning_or_error
);
2032 /* Build a reference to an object specified by the C++ `->' operator.
2033 Usually this just involves dereferencing the object, but if the
2034 `->' operator is overloaded, then such overloads must be
2035 performed until an object which does not have the `->' operator
2036 overloaded is found. An error is reported when circular pointer
2037 delegation is detected. */
2040 build_x_arrow (location_t loc
, tree expr
, tsubst_flags_t complain
)
2042 tree orig_expr
= expr
;
2043 tree type
= TREE_TYPE (expr
);
2044 tree last_rval
= NULL_TREE
;
2045 vec
<tree
, va_gc
> *types_memoized
= NULL
;
2047 if (type
== error_mark_node
)
2048 return error_mark_node
;
2050 if (processing_template_decl
)
2052 if (type
&& TYPE_PTR_P (type
)
2053 && !dependent_scope_p (TREE_TYPE (type
)))
2054 /* Pointer to current instantiation, don't treat as dependent. */;
2055 else if (type_dependent_expression_p (expr
))
2056 return build_min_nt_loc (loc
, ARROW_EXPR
, expr
);
2057 expr
= build_non_dependent_expr (expr
);
2060 if (MAYBE_CLASS_TYPE_P (type
))
2062 struct tinst_level
*actual_inst
= current_instantiation ();
2065 while ((expr
= build_new_op (loc
, COMPONENT_REF
,
2066 LOOKUP_NORMAL
, expr
, NULL_TREE
, NULL_TREE
,
2069 if (expr
== error_mark_node
)
2070 return error_mark_node
;
2072 /* This provides a better instantiation backtrace in case of
2074 if (fn
&& DECL_USE_TEMPLATE (fn
))
2075 push_tinst_level_loc (fn
,
2076 (current_instantiation () != actual_inst
)
2077 ? DECL_SOURCE_LOCATION (fn
)
2081 if (vec_member (TREE_TYPE (expr
), types_memoized
))
2083 if (complain
& tf_error
)
2084 error ("circular pointer delegation detected");
2085 return error_mark_node
;
2088 vec_safe_push (types_memoized
, TREE_TYPE (expr
));
2092 while (current_instantiation () != actual_inst
)
2095 if (last_rval
== NULL_TREE
)
2097 if (complain
& tf_error
)
2098 error ("base operand of %<->%> has non-pointer type %qT", type
);
2099 return error_mark_node
;
2102 if (TYPE_REF_P (TREE_TYPE (last_rval
)))
2103 last_rval
= convert_from_reference (last_rval
);
2107 last_rval
= decay_conversion (expr
, complain
);
2108 if (last_rval
== error_mark_node
)
2109 return error_mark_node
;
2112 if (TYPE_PTR_P (TREE_TYPE (last_rval
)))
2114 if (processing_template_decl
)
2116 expr
= build_min (ARROW_EXPR
, TREE_TYPE (TREE_TYPE (last_rval
)),
2118 TREE_SIDE_EFFECTS (expr
) = TREE_SIDE_EFFECTS (last_rval
);
2122 return cp_build_indirect_ref (loc
, last_rval
, RO_ARROW
, complain
);
2125 if (complain
& tf_error
)
2128 error ("result of %<operator->()%> yields non-pointer result");
2130 error ("base operand of %<->%> is not a pointer");
2132 return error_mark_node
;
2135 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
2136 already been checked out to be of aggregate type. */
2139 build_m_component_ref (tree datum
, tree component
, tsubst_flags_t complain
)
2147 datum
= mark_lvalue_use (datum
);
2148 component
= mark_rvalue_use (component
);
2150 if (error_operand_p (datum
) || error_operand_p (component
))
2151 return error_mark_node
;
2153 ptrmem_type
= TREE_TYPE (component
);
2154 if (!TYPE_PTRMEM_P (ptrmem_type
))
2156 if (complain
& tf_error
)
2157 error ("%qE cannot be used as a member pointer, since it is of "
2158 "type %qT", component
, ptrmem_type
);
2159 return error_mark_node
;
2162 objtype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
2163 if (! MAYBE_CLASS_TYPE_P (objtype
))
2165 if (complain
& tf_error
)
2166 error ("cannot apply member pointer %qE to %qE, which is of "
2167 "non-class type %qT", component
, datum
, objtype
);
2168 return error_mark_node
;
2171 type
= TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type
);
2172 ctype
= complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type
));
2174 if (!COMPLETE_TYPE_P (ctype
))
2176 if (!same_type_p (ctype
, objtype
))
2182 binfo
= lookup_base (objtype
, ctype
, ba_check
, NULL
, complain
);
2187 if (complain
& tf_error
)
2188 error ("pointer to member type %qT incompatible with object "
2189 "type %qT", type
, objtype
);
2190 return error_mark_node
;
2192 else if (binfo
== error_mark_node
)
2193 return error_mark_node
;
2196 if (TYPE_PTRDATAMEM_P (ptrmem_type
))
2198 bool is_lval
= real_lvalue_p (datum
);
2201 /* Compute the type of the field, as described in [expr.ref].
2202 There's no such thing as a mutable pointer-to-member, so
2203 things are not as complex as they are for references to
2204 non-static data members. */
2205 type
= cp_build_qualified_type (type
,
2206 (cp_type_quals (type
)
2207 | cp_type_quals (TREE_TYPE (datum
))));
2209 datum
= build_address (datum
);
2211 /* Convert object to the correct base. */
2214 datum
= build_base_path (PLUS_EXPR
, datum
, binfo
, 1, complain
);
2215 if (datum
== error_mark_node
)
2216 return error_mark_node
;
2219 /* Build an expression for "object + offset" where offset is the
2220 value stored in the pointer-to-data-member. */
2221 ptype
= build_pointer_type (type
);
2222 datum
= fold_build_pointer_plus (fold_convert (ptype
, datum
), component
);
2223 datum
= cp_build_fold_indirect_ref (datum
);
2224 if (datum
== error_mark_node
)
2225 return error_mark_node
;
2227 /* If the object expression was an rvalue, return an rvalue. */
2229 datum
= move (datum
);
2234 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
2235 program is ill-formed if the second operand is a pointer to member
2236 function with ref-qualifier & (for C++20: unless its cv-qualifier-seq
2237 is const). In a .* expression whose object expression is an lvalue,
2238 the program is ill-formed if the second operand is a pointer to member
2239 function with ref-qualifier &&. */
2240 if (FUNCTION_REF_QUALIFIED (type
))
2242 bool lval
= lvalue_p (datum
);
2243 if (lval
&& FUNCTION_RVALUE_QUALIFIED (type
))
2245 if (complain
& tf_error
)
2246 error ("pointer-to-member-function type %qT requires an rvalue",
2248 return error_mark_node
;
2250 else if (!lval
&& !FUNCTION_RVALUE_QUALIFIED (type
))
2252 if ((type_memfn_quals (type
)
2253 & (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
))
2256 if (complain
& tf_error
)
2257 error ("pointer-to-member-function type %qT requires "
2258 "an lvalue", ptrmem_type
);
2259 return error_mark_node
;
2261 else if (cxx_dialect
< cxx20
)
2263 if (complain
& tf_warning_or_error
)
2264 pedwarn (input_location
, OPT_Wpedantic
,
2265 "pointer-to-member-function type %qT requires "
2266 "an lvalue before C++20", ptrmem_type
);
2268 return error_mark_node
;
2272 return build2 (OFFSET_REF
, type
, datum
, component
);
2276 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
2279 build_functional_cast_1 (location_t loc
, tree exp
, tree parms
,
2280 tsubst_flags_t complain
)
2282 /* This is either a call to a constructor,
2283 or a C cast in C++'s `functional' notation. */
2285 /* The type to which we are casting. */
2288 if (error_operand_p (exp
) || parms
== error_mark_node
)
2289 return error_mark_node
;
2291 if (TREE_CODE (exp
) == TYPE_DECL
)
2293 type
= TREE_TYPE (exp
);
2295 if (DECL_ARTIFICIAL (exp
))
2296 cp_warn_deprecated_use (type
);
2301 /* We need to check this explicitly, since value-initialization of
2302 arrays is allowed in other situations. */
2303 if (TREE_CODE (type
) == ARRAY_TYPE
)
2305 if (complain
& tf_error
)
2306 error_at (loc
, "functional cast to array type %qT", type
);
2307 return error_mark_node
;
2310 if (tree anode
= type_uses_auto (type
))
2312 if (!CLASS_PLACEHOLDER_TEMPLATE (anode
))
2314 if (complain
& tf_error
)
2315 error_at (loc
, "invalid use of %qT", anode
);
2316 return error_mark_node
;
2320 /* Even if there are no parameters, we might be able to deduce from
2321 default template arguments. Pass TF_NONE so that we don't
2322 generate redundant diagnostics. */
2323 type
= do_auto_deduction (type
, parms
, anode
, tf_none
,
2325 if (type
== error_mark_node
)
2327 if (complain
& tf_error
)
2328 error_at (loc
, "cannot deduce template arguments "
2329 "for %qT from %<()%>", anode
);
2330 return error_mark_node
;
2334 type
= do_auto_deduction (type
, parms
, anode
, complain
,
2338 if (processing_template_decl
)
2342 /* Diagnose this even in a template. We could also try harder
2343 to give all the usual errors when the type and args are
2345 if (TYPE_REF_P (type
) && !parms
)
2347 if (complain
& tf_error
)
2348 error_at (loc
, "invalid value-initialization of reference type");
2349 return error_mark_node
;
2352 t
= build_min (CAST_EXPR
, type
, parms
);
2353 /* We don't know if it will or will not have side effects. */
2354 TREE_SIDE_EFFECTS (t
) = 1;
2358 if (! MAYBE_CLASS_TYPE_P (type
))
2360 if (parms
== NULL_TREE
)
2362 if (VOID_TYPE_P (type
))
2364 return build_value_init (cv_unqualified (type
), complain
);
2367 /* This must build a C cast. */
2368 parms
= build_x_compound_expr_from_list (parms
, ELK_FUNC_CAST
, complain
);
2369 return cp_build_c_cast (loc
, type
, parms
, complain
);
2372 /* Prepare to evaluate as a call to a constructor. If this expression
2373 is actually used, for example,
2375 return X (arg1, arg2, ...);
2377 then the slot being initialized will be filled in. */
2379 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
2380 return error_mark_node
;
2381 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
2382 return error_mark_node
;
2386 If the expression list is a single-expression, the type
2387 conversion is equivalent (in definedness, and if defined in
2388 meaning) to the corresponding cast expression. */
2389 if (parms
&& TREE_CHAIN (parms
) == NULL_TREE
)
2390 return cp_build_c_cast (loc
, type
, TREE_VALUE (parms
), complain
);
2394 The expression T(), where T is a simple-type-specifier for a
2395 non-array complete object type or the (possibly cv-qualified)
2396 void type, creates an rvalue of the specified type, which is
2397 value-initialized. */
2399 if (parms
== NULL_TREE
)
2401 exp
= build_value_init (type
, complain
);
2402 exp
= get_target_expr_sfinae (exp
, complain
);
2406 /* Call the constructor. */
2407 releasing_vec parmvec
;
2408 for (; parms
!= NULL_TREE
; parms
= TREE_CHAIN (parms
))
2409 vec_safe_push (parmvec
, TREE_VALUE (parms
));
2410 exp
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
2411 &parmvec
, type
, LOOKUP_NORMAL
, complain
);
2413 if (exp
== error_mark_node
)
2414 return error_mark_node
;
2416 return build_cplus_new (type
, exp
, complain
);
2420 build_functional_cast (location_t loc
, tree exp
, tree parms
,
2421 tsubst_flags_t complain
)
2423 tree result
= build_functional_cast_1 (loc
, exp
, parms
, complain
);
2424 protected_set_expr_location (result
, loc
);
2429 /* Add new exception specifier SPEC, to the LIST we currently have.
2430 If it's already in LIST then do nothing.
2431 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2432 know what we're doing. */
2435 add_exception_specifier (tree list
, tree spec
, tsubst_flags_t complain
)
2440 diagnostic_t diag_type
= DK_UNSPECIFIED
; /* none */
2442 if (spec
== error_mark_node
)
2445 gcc_assert (spec
&& (!list
|| TREE_VALUE (list
)));
2447 /* [except.spec] 1, type in an exception specifier shall not be
2448 incomplete, or pointer or ref to incomplete other than pointer
2450 is_ptr
= TYPE_PTR_P (core
);
2451 if (is_ptr
|| TYPE_REF_P (core
))
2452 core
= TREE_TYPE (core
);
2455 else if (VOID_TYPE_P (core
))
2457 else if (TREE_CODE (core
) == TEMPLATE_TYPE_PARM
)
2459 else if (processing_template_decl
)
2461 else if (!verify_type_context (input_location
, TCTX_EXCEPTIONS
, core
,
2462 !(complain
& tf_error
)))
2463 return error_mark_node
;
2467 /* 15.4/1 says that types in an exception specifier must be complete,
2468 but it seems more reasonable to only require this on definitions
2469 and calls. So just give a pedwarn at this point; we will give an
2470 error later if we hit one of those two cases. */
2471 if (!COMPLETE_TYPE_P (complete_type (core
)))
2472 diag_type
= DK_PEDWARN
; /* pedwarn */
2479 for (probe
= list
; probe
; probe
= TREE_CHAIN (probe
))
2480 if (same_type_p (TREE_VALUE (probe
), spec
))
2483 list
= tree_cons (NULL_TREE
, spec
, list
);
2486 diag_type
= DK_ERROR
; /* error */
2488 if (diag_type
!= DK_UNSPECIFIED
2489 && (complain
& tf_warning_or_error
))
2490 cxx_incomplete_type_diagnostic (NULL_TREE
, core
, diag_type
);
2495 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2498 nothrow_spec_p_uninst (const_tree spec
)
2500 if (DEFERRED_NOEXCEPT_SPEC_P (spec
))
2502 return nothrow_spec_p (spec
);
2505 /* Combine the two exceptions specifier lists LIST and ADD, and return
2509 merge_exception_specifiers (tree list
, tree add
)
2511 tree noex
, orig_list
;
2513 if (list
== error_mark_node
|| add
== error_mark_node
)
2514 return error_mark_node
;
2516 /* No exception-specifier or noexcept(false) are less strict than
2517 anything else. Prefer the newer variant (LIST). */
2518 if (!list
|| list
== noexcept_false_spec
)
2520 else if (!add
|| add
== noexcept_false_spec
)
2523 /* noexcept(true) and throw() are stricter than anything else.
2524 As above, prefer the more recent one (LIST). */
2525 if (nothrow_spec_p_uninst (add
))
2528 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2529 if (UNEVALUATED_NOEXCEPT_SPEC_P (add
)
2530 && UNEVALUATED_NOEXCEPT_SPEC_P (list
))
2532 /* We should have instantiated other deferred noexcept specs by now. */
2533 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add
));
2535 if (nothrow_spec_p_uninst (list
))
2537 noex
= TREE_PURPOSE (list
);
2538 gcc_checking_assert (!TREE_PURPOSE (add
)
2539 || errorcount
|| !flag_exceptions
2540 || cp_tree_equal (noex
, TREE_PURPOSE (add
)));
2542 /* Combine the dynamic-exception-specifiers, if any. */
2544 for (; add
&& TREE_VALUE (add
); add
= TREE_CHAIN (add
))
2546 tree spec
= TREE_VALUE (add
);
2549 for (probe
= orig_list
; probe
&& TREE_VALUE (probe
);
2550 probe
= TREE_CHAIN (probe
))
2551 if (same_type_p (TREE_VALUE (probe
), spec
))
2555 spec
= build_tree_list (NULL_TREE
, spec
);
2556 TREE_CHAIN (spec
) = list
;
2561 /* Keep the noexcept-specifier at the beginning of the list. */
2562 if (noex
!= TREE_PURPOSE (list
))
2563 list
= tree_cons (noex
, TREE_VALUE (list
), TREE_CHAIN (list
));
2568 /* Subroutine of build_call. Ensure that each of the types in the
2569 exception specification is complete. Technically, 15.4/1 says that
2570 they need to be complete when we see a declaration of the function,
2571 but we should be able to get away with only requiring this when the
2572 function is defined or called. See also add_exception_specifier. */
2575 require_complete_eh_spec_types (tree fntype
, tree decl
)
2578 /* Don't complain about calls to op new. */
2579 if (decl
&& DECL_ARTIFICIAL (decl
))
2581 for (raises
= TYPE_RAISES_EXCEPTIONS (fntype
); raises
;
2582 raises
= TREE_CHAIN (raises
))
2584 tree type
= TREE_VALUE (raises
);
2585 if (type
&& !COMPLETE_TYPE_P (type
))
2589 ("call to function %qD which throws incomplete type %q#T",
2592 error ("call to function which throws incomplete type %q#T",
2599 #include "gt-cp-typeck2.h"