1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987-2018 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"
37 process_init_constructor (tree type
, tree init
, int nested
,
38 tsubst_flags_t complain
);
41 /* Print an error message stemming from an attempt to use
42 BASETYPE as a base class for TYPE. */
45 error_not_base_type (tree basetype
, tree type
)
47 if (TREE_CODE (basetype
) == FUNCTION_DECL
)
48 basetype
= DECL_CONTEXT (basetype
);
49 error ("type %qT is not a base type for type %qT", basetype
, type
);
50 return error_mark_node
;
54 binfo_or_else (tree base
, tree type
)
56 tree binfo
= lookup_base (type
, base
, ba_unique
,
57 NULL
, tf_warning_or_error
);
59 if (binfo
== error_mark_node
)
62 error_not_base_type (base
, type
);
66 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
67 value may not be changed thereafter. */
70 cxx_readonly_error (tree arg
, enum lvalue_use errstring
)
73 /* This macro is used to emit diagnostics to ensure that all format
74 strings are complete sentences, visible to gettext and checked at
77 #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG) \
98 /* Handle C++-specific things first. */
101 && DECL_LANG_SPECIFIC (arg
)
102 && DECL_IN_AGGR_P (arg
)
103 && !TREE_STATIC (arg
))
104 ERROR_FOR_ASSIGNMENT (G_("assignment of "
105 "constant field %qD"),
106 G_("constant field %qD "
107 "used as %<asm%> output"),
109 "constant field %qD"),
111 "constant field %qD"),
113 else if (INDIRECT_REF_P (arg
)
114 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 0))) == REFERENCE_TYPE
115 && (VAR_P (TREE_OPERAND (arg
, 0))
116 || TREE_CODE (TREE_OPERAND (arg
, 0)) == PARM_DECL
))
117 ERROR_FOR_ASSIGNMENT (G_("assignment of "
118 "read-only reference %qD"),
119 G_("read-only reference %qD "
120 "used as %<asm%> output"),
122 "read-only reference %qD"),
124 "read-only reference %qD"),
125 TREE_OPERAND (arg
, 0));
127 readonly_error (input_location
, arg
, errstring
);
131 /* Structure that holds information about declarations whose type was
132 incomplete and we could not check whether it was abstract or not. */
134 struct GTY((chain_next ("%h.next"), for_user
)) pending_abstract_type
{
135 /* Declaration which we are checking for abstractness. It is either
136 a DECL node, or an IDENTIFIER_NODE if we do not have a full
137 declaration available. */
140 /* Type which will be checked for abstractness. */
143 /* Kind of use in an unnamed declarator. */
144 enum abstract_class_use use
;
146 /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
147 because DECLs already carry locus information. */
150 /* Link to the next element in list. */
151 struct pending_abstract_type
* next
;
154 struct abstract_type_hasher
: ggc_ptr_hash
<pending_abstract_type
>
156 typedef tree compare_type
;
157 static hashval_t
hash (pending_abstract_type
*);
158 static bool equal (pending_abstract_type
*, tree
);
161 /* Compute the hash value of the node VAL. This function is used by the
162 hash table abstract_pending_vars. */
165 abstract_type_hasher::hash (pending_abstract_type
*pat
)
167 return (hashval_t
) TYPE_UID (pat
->type
);
171 /* Compare node VAL1 with the type VAL2. This function is used by the
172 hash table abstract_pending_vars. */
175 abstract_type_hasher::equal (pending_abstract_type
*pat1
, tree type2
)
177 return (pat1
->type
== type2
);
180 /* Hash table that maintains pending_abstract_type nodes, for which we still
181 need to check for type abstractness. The key of the table is the type
182 of the declaration. */
183 static GTY (()) hash_table
<abstract_type_hasher
> *abstract_pending_vars
= NULL
;
185 static int abstract_virtuals_error_sfinae (tree
, tree
, abstract_class_use
, tsubst_flags_t
);
187 /* This function is called after TYPE is completed, and will check if there
188 are pending declarations for which we still need to verify the abstractness
189 of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
190 turned out to be incomplete. */
193 complete_type_check_abstract (tree type
)
195 struct pending_abstract_type
*pat
;
196 location_t cur_loc
= input_location
;
198 gcc_assert (COMPLETE_TYPE_P (type
));
200 if (!abstract_pending_vars
)
203 /* Retrieve the list of pending declarations for this type. */
204 pending_abstract_type
**slot
205 = abstract_pending_vars
->find_slot_with_hash (type
, TYPE_UID (type
),
212 /* If the type is not abstract, do not do anything. */
213 if (CLASSTYPE_PURE_VIRTUALS (type
))
215 struct pending_abstract_type
*prev
= 0, *next
;
217 /* Reverse the list to emit the errors in top-down order. */
218 for (; pat
; pat
= next
)
226 /* Go through the list, and call abstract_virtuals_error for each
227 element: it will issue a diagnostic if the type is abstract. */
230 gcc_assert (type
== pat
->type
);
232 /* Tweak input_location so that the diagnostic appears at the correct
233 location. Notice that this is only needed if the decl is an
235 input_location
= pat
->locus
;
236 abstract_virtuals_error_sfinae (pat
->decl
, pat
->type
, pat
->use
,
237 tf_warning_or_error
);
242 abstract_pending_vars
->clear_slot (slot
);
244 input_location
= cur_loc
;
248 /* If TYPE has abstract virtual functions, issue an error about trying
249 to create an object of that type. DECL is the object declared, or
250 NULL_TREE if the declaration is unavailable, in which case USE specifies
251 the kind of invalid use. Returns 1 if an error occurred; zero if
255 abstract_virtuals_error_sfinae (tree decl
, tree type
, abstract_class_use use
,
256 tsubst_flags_t complain
)
258 vec
<tree
, va_gc
> *pure
;
260 /* This function applies only to classes. Any other entity can never
262 if (!CLASS_TYPE_P (type
))
264 type
= TYPE_MAIN_VARIANT (type
);
267 /* Instantiation here seems to be required by the standard,
268 but breaks e.g. boost::bind. FIXME! */
269 /* In SFINAE, non-N3276 context, force instantiation. */
270 if (!(complain
& (tf_error
|tf_decltype
)))
271 complete_type (type
);
274 /* If the type is incomplete, we register it within a hash table,
275 so that we can check again once it is completed. This makes sense
276 only for objects for which we have a declaration or at least a
278 if (!COMPLETE_TYPE_P (type
) && (complain
& tf_error
))
280 struct pending_abstract_type
*pat
;
282 gcc_assert (!decl
|| DECL_P (decl
) || identifier_p (decl
));
284 if (!abstract_pending_vars
)
285 abstract_pending_vars
286 = hash_table
<abstract_type_hasher
>::create_ggc (31);
288 pending_abstract_type
**slot
289 = abstract_pending_vars
->find_slot_with_hash (type
, TYPE_UID (type
),
292 pat
= ggc_alloc
<pending_abstract_type
> ();
296 pat
->locus
= ((decl
&& DECL_P (decl
))
297 ? DECL_SOURCE_LOCATION (decl
)
306 if (!TYPE_SIZE (type
))
307 /* TYPE is being defined, and during that time
308 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
311 pure
= CLASSTYPE_PURE_VIRTUALS (type
);
315 if (!(complain
& tf_error
))
321 error ("cannot declare variable %q+D to be of abstract "
322 "type %qT", decl
, type
);
323 else if (TREE_CODE (decl
) == PARM_DECL
)
325 if (DECL_NAME (decl
))
326 error ("cannot declare parameter %q+D to be of abstract type %qT",
329 error ("cannot declare parameter to be of abstract type %qT",
332 else if (TREE_CODE (decl
) == FIELD_DECL
)
333 error ("cannot declare field %q+D to be of abstract type %qT",
335 else if (TREE_CODE (decl
) == FUNCTION_DECL
336 && TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
337 error ("invalid abstract return type for member function %q+#D", decl
);
338 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
339 error ("invalid abstract return type for function %q+#D", decl
);
340 else if (identifier_p (decl
))
341 /* Here we do not have location information. */
342 error ("invalid abstract type %qT for %qE", type
, decl
);
344 error ("invalid abstract type for %q+D", decl
);
349 error ("creating array of %qT, which is an abstract class type", type
);
352 error ("invalid cast to abstract class type %qT", type
);
355 error ("invalid new-expression of abstract class type %qT", type
);
358 error ("invalid abstract return type %qT", type
);
361 error ("invalid abstract parameter type %qT", type
);
364 error ("expression of abstract class type %qT cannot "
365 "be used in throw-expression", type
);
368 error ("cannot declare catch parameter to be of abstract "
369 "class type %qT", type
);
372 error ("cannot allocate an object of abstract type %qT", type
);
375 /* Only go through this once. */
381 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
382 " because the following virtual functions are pure within %qT:",
385 FOR_EACH_VEC_ELT (*pure
, ix
, fn
)
386 if (! DECL_CLONED_FUNCTION_P (fn
)
387 || DECL_COMPLETE_DESTRUCTOR_P (fn
))
388 inform (DECL_SOURCE_LOCATION (fn
), "\t%#qD", fn
);
390 /* Now truncate the vector. This leaves it non-null, so we know
391 there are pure virtuals, but empty so we don't list them out
400 abstract_virtuals_error_sfinae (tree decl
, tree type
, tsubst_flags_t complain
)
402 return abstract_virtuals_error_sfinae (decl
, type
, ACU_UNKNOWN
, complain
);
406 abstract_virtuals_error_sfinae (abstract_class_use use
, tree type
,
407 tsubst_flags_t complain
)
409 return abstract_virtuals_error_sfinae (NULL_TREE
, type
, use
, complain
);
413 /* Wrapper for the above function in the common case of wanting errors. */
416 abstract_virtuals_error (tree decl
, tree type
)
418 return abstract_virtuals_error_sfinae (decl
, type
, tf_warning_or_error
);
422 abstract_virtuals_error (abstract_class_use use
, tree type
)
424 return abstract_virtuals_error_sfinae (use
, type
, tf_warning_or_error
);
427 /* Print an inform about the declaration of the incomplete type TYPE. */
430 cxx_incomplete_type_inform (const_tree type
)
432 if (!TYPE_MAIN_DECL (type
))
435 location_t loc
= DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
));
436 tree ptype
= strip_top_quals (CONST_CAST_TREE (type
));
438 if (current_class_type
439 && TYPE_BEING_DEFINED (current_class_type
)
440 && same_type_p (ptype
, current_class_type
))
441 inform (loc
, "definition of %q#T is not complete until "
442 "the closing brace", ptype
);
443 else if (!TYPE_TEMPLATE_INFO (ptype
))
444 inform (loc
, "forward declaration of %q#T", ptype
);
446 inform (loc
, "declaration of %q#T", ptype
);
449 /* Print an error message for invalid use of an incomplete type.
450 VALUE is the expression that was used (or 0 if that isn't known)
451 and TYPE is the type that was invalid. DIAG_KIND indicates the
452 type of diagnostic (see diagnostic.def). */
455 cxx_incomplete_type_diagnostic (location_t loc
, const_tree value
,
456 const_tree type
, diagnostic_t diag_kind
)
458 bool is_decl
= false, complained
= false;
460 gcc_assert (diag_kind
== DK_WARNING
461 || diag_kind
== DK_PEDWARN
462 || diag_kind
== DK_ERROR
);
464 /* Avoid duplicate error message. */
465 if (TREE_CODE (type
) == ERROR_MARK
)
468 if (value
!= 0 && (VAR_P (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
);
477 /* We must print an error message. Be clever about what it says. */
479 switch (TREE_CODE (type
))
485 complained
= emit_diagnostic (diag_kind
, loc
, 0,
486 "invalid use of incomplete type %q#T",
489 cxx_incomplete_type_inform (type
);
493 emit_diagnostic (diag_kind
, loc
, 0,
494 "invalid use of %qT", type
);
498 if (TYPE_DOMAIN (type
))
500 type
= TREE_TYPE (type
);
503 emit_diagnostic (diag_kind
, loc
, 0,
504 "invalid use of array with unspecified bounds");
510 tree member
= TREE_OPERAND (value
, 1);
511 if (is_overloaded_fn (member
))
512 member
= get_first_fn (member
);
514 if (DECL_FUNCTION_MEMBER_P (member
)
515 && ! flag_ms_extensions
)
516 emit_diagnostic (diag_kind
, loc
, 0,
517 "invalid use of member function %qD "
518 "(did you forget the %<()%> ?)", member
);
520 emit_diagnostic (diag_kind
, loc
, 0,
521 "invalid use of member %qD "
522 "(did you forget the %<&%> ?)", member
);
526 case TEMPLATE_TYPE_PARM
:
529 if (CLASS_PLACEHOLDER_TEMPLATE (type
))
530 emit_diagnostic (diag_kind
, loc
, 0,
531 "invalid use of placeholder %qT", type
);
533 emit_diagnostic (diag_kind
, loc
, 0,
534 "invalid use of %qT", type
);
537 emit_diagnostic (diag_kind
, loc
, 0,
538 "invalid use of template type parameter %qT", type
);
541 case BOUND_TEMPLATE_TEMPLATE_PARM
:
542 emit_diagnostic (diag_kind
, loc
, 0,
543 "invalid use of template template parameter %qT",
549 emit_diagnostic (diag_kind
, loc
, 0,
550 "invalid use of dependent type %qT", type
);
554 if (type
== init_list_type_node
)
556 emit_diagnostic (diag_kind
, loc
, 0,
557 "invalid use of brace-enclosed initializer list");
560 gcc_assert (type
== unknown_type_node
);
561 if (value
&& TREE_CODE (value
) == COMPONENT_REF
)
563 else if (value
&& TREE_CODE (value
) == ADDR_EXPR
)
564 emit_diagnostic (diag_kind
, loc
, 0,
565 "address of overloaded function with no contextual "
567 else if (value
&& TREE_CODE (value
) == OVERLOAD
)
568 emit_diagnostic (diag_kind
, loc
, 0,
569 "overloaded function with no contextual type information");
571 emit_diagnostic (diag_kind
, loc
, 0,
572 "insufficient contextual information to determine type");
580 /* Print an error message for invalid use of an incomplete type.
581 VALUE is the expression that was used (or 0 if that isn't known)
582 and TYPE is the type that was invalid. */
585 cxx_incomplete_type_error (location_t loc
, const_tree value
, const_tree type
)
587 cxx_incomplete_type_diagnostic (loc
, value
, type
, DK_ERROR
);
591 /* The recursive part of split_nonconstant_init. DEST is an lvalue
592 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
593 Return true if the whole of the value was initialized by the
594 generated statements. */
597 split_nonconstant_init_1 (tree dest
, tree init
)
599 unsigned HOST_WIDE_INT idx
;
600 tree field_index
, value
;
601 tree type
= TREE_TYPE (dest
);
602 tree inner_type
= NULL
;
603 bool array_type_p
= false;
604 bool complete_p
= true;
605 HOST_WIDE_INT num_split_elts
= 0;
607 switch (TREE_CODE (type
))
610 inner_type
= TREE_TYPE (type
);
612 if ((TREE_SIDE_EFFECTS (init
)
613 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
614 || array_of_runtime_bound_p (type
))
616 /* For an array, we only need/want a single cleanup region rather
617 than one per element. */
618 tree code
= build_vec_init (dest
, NULL_TREE
, init
, false, 1,
619 tf_warning_or_error
);
627 case QUAL_UNION_TYPE
:
628 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init
), idx
,
631 /* The current implementation of this algorithm assumes that
632 the field was set for all the elements. This is usually done
633 by process_init_constructor. */
634 gcc_assert (field_index
);
637 inner_type
= TREE_TYPE (field_index
);
639 if (TREE_CODE (value
) == CONSTRUCTOR
)
644 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
645 NULL_TREE
, NULL_TREE
);
647 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
650 if (!split_nonconstant_init_1 (sub
, value
))
653 CONSTRUCTOR_ELTS (init
)->ordered_remove (idx
--);
656 else if (!initializer_constant_valid_p (value
, inner_type
))
661 /* FIXME: Ordered removal is O(1) so the whole function is
662 worst-case quadratic. This could be fixed using an aside
663 bitmap to record which elements must be removed and remove
664 them all at the same time. Or by merging
665 split_non_constant_init into process_init_constructor_array,
666 that is separating constants from non-constants while building
668 CONSTRUCTOR_ELTS (init
)->ordered_remove (idx
);
671 if (TREE_CODE (field_index
) == RANGE_EXPR
)
673 /* Use build_vec_init to initialize a range. */
674 tree low
= TREE_OPERAND (field_index
, 0);
675 tree hi
= TREE_OPERAND (field_index
, 1);
676 sub
= build4 (ARRAY_REF
, inner_type
, dest
, low
,
677 NULL_TREE
, NULL_TREE
);
678 sub
= cp_build_addr_expr (sub
, tf_warning_or_error
);
679 tree max
= size_binop (MINUS_EXPR
, hi
, low
);
680 code
= build_vec_init (sub
, max
, value
, false, 0,
681 tf_warning_or_error
);
683 if (tree_fits_shwi_p (max
))
684 num_split_elts
+= tree_to_shwi (max
);
689 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
690 NULL_TREE
, NULL_TREE
);
692 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
695 code
= build2 (INIT_EXPR
, inner_type
, sub
, value
);
696 code
= build_stmt (input_location
, EXPR_STMT
, code
);
697 code
= maybe_cleanup_point_expr_void (code
);
700 = cxx_maybe_build_cleanup (sub
, tf_warning_or_error
))
701 finish_eh_cleanup (cleanup
);
710 if (!initializer_constant_valid_p (init
, type
))
713 tree cons
= copy_node (init
);
714 CONSTRUCTOR_ELTS (init
) = NULL
;
715 code
= build2 (MODIFY_EXPR
, type
, dest
, cons
);
716 code
= build_stmt (input_location
, EXPR_STMT
, code
);
718 num_split_elts
+= CONSTRUCTOR_NELTS (init
);
726 /* The rest of the initializer is now a constant. */
727 TREE_CONSTANT (init
) = 1;
728 return complete_p
&& complete_ctor_at_level_p (TREE_TYPE (init
),
729 num_split_elts
, inner_type
);
732 /* A subroutine of store_init_value. Splits non-constant static
733 initializer INIT into a constant part and generates code to
734 perform the non-constant part of the initialization to DEST.
735 Returns the code for the runtime init. */
738 split_nonconstant_init (tree dest
, tree init
)
742 if (TREE_CODE (init
) == TARGET_EXPR
)
743 init
= TARGET_EXPR_INITIAL (init
);
744 if (TREE_CODE (init
) == CONSTRUCTOR
)
746 init
= cp_fully_fold (init
);
747 code
= push_stmt_list ();
748 if (split_nonconstant_init_1 (dest
, init
))
750 code
= pop_stmt_list (code
);
751 DECL_INITIAL (dest
) = init
;
752 TREE_READONLY (dest
) = 0;
754 else if (TREE_CODE (init
) == STRING_CST
755 && array_of_runtime_bound_p (TREE_TYPE (dest
)))
756 code
= build_vec_init (dest
, NULL_TREE
, init
, /*value-init*/false,
757 /*from array*/1, tf_warning_or_error
);
759 code
= build2 (INIT_EXPR
, TREE_TYPE (dest
), dest
, init
);
764 /* Perform appropriate conversions on the initial value of a variable,
765 store it in the declaration DECL,
766 and print any error messages that are appropriate.
767 If the init is invalid, store an ERROR_MARK.
769 C++: Note that INIT might be a TREE_LIST, which would mean that it is
770 a base class initializer for some aggregate type, hopefully compatible
771 with DECL. If INIT is a single element, and DECL is an aggregate
772 type, we silently convert INIT into a TREE_LIST, allowing a constructor
775 If INIT is a TREE_LIST and there is no constructor, turn INIT
776 into a CONSTRUCTOR and use standard initialization techniques.
777 Perhaps a warning should be generated?
779 Returns code to be executed if initialization could not be performed
780 for static variable. In that case, caller must emit the code. */
783 store_init_value (tree decl
, tree init
, vec
<tree
, va_gc
>** cleanups
, int flags
)
787 /* If variable's type was invalidly declared, just ignore it. */
789 type
= TREE_TYPE (decl
);
790 if (TREE_CODE (type
) == ERROR_MARK
)
793 if (MAYBE_CLASS_TYPE_P (type
))
795 if (TREE_CODE (init
) == TREE_LIST
)
797 error ("constructor syntax used, but no constructor declared "
798 "for type %qT", type
);
799 init
= build_constructor_from_list (init_list_type_node
, nreverse (init
));
803 /* End of special C++ code. */
805 if (flags
& LOOKUP_ALREADY_DIGESTED
)
808 /* Digest the specified initializer into an expression. */
809 value
= digest_init_flags (type
, init
, flags
, tf_warning_or_error
);
811 value
= extend_ref_init_temps (decl
, value
, cleanups
);
813 /* In C++11 constant expression is a semantic, not syntactic, property.
814 In C++98, make sure that what we thought was a constant expression at
815 template definition time is still constant and otherwise perform this
816 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
817 if (decl_maybe_constant_var_p (decl
) || TREE_STATIC (decl
))
820 value
= instantiate_non_dependent_expr (value
);
821 if (DECL_DECLARED_CONSTEXPR_P (decl
)
822 || (DECL_IN_AGGR_P (decl
) && !DECL_VAR_DECLARED_INLINE_P (decl
)))
824 /* Diagnose a non-constant initializer for constexpr. */
825 if (!require_constant_expression (value
))
826 value
= error_mark_node
;
828 value
= cxx_constant_value (value
, decl
);
830 value
= maybe_constant_init (value
, decl
);
831 if (TREE_CODE (value
) == CONSTRUCTOR
&& cp_has_mutable_p (type
))
832 /* Poison this CONSTRUCTOR so it can't be copied to another
833 constexpr variable. */
834 CONSTRUCTOR_MUTABLE_POISON (value
) = true;
835 const_init
= (reduced_constant_expression_p (value
)
836 || error_operand_p (value
));
837 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = const_init
;
838 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */
839 if (TREE_CODE (type
) != REFERENCE_TYPE
)
840 TREE_CONSTANT (decl
) = const_init
&& decl_maybe_constant_var_p (decl
);
842 value
= cp_fully_fold (value
);
844 /* Handle aggregate NSDMI in non-constant initializers, too. */
845 value
= replace_placeholders (value
, decl
);
847 /* DECL may change value; purge caches. */
848 clear_cv_and_fold_caches ();
850 /* If the initializer is not a constant, fill in DECL_INITIAL with
851 the bits that are constant, and then return an expression that
852 will perform the dynamic initialization. */
853 if (value
!= error_mark_node
854 && (TREE_SIDE_EFFECTS (value
)
855 || array_of_runtime_bound_p (type
)
856 || ! reduced_constant_expression_p (value
)))
857 return split_nonconstant_init (decl
, value
);
858 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
859 is an automatic variable, the middle end will turn this into a
860 dynamic initialization later. */
861 DECL_INITIAL (decl
) = value
;
866 /* Give diagnostic about narrowing conversions within { }. */
869 check_narrowing (tree type
, tree init
, tsubst_flags_t complain
)
871 tree ftype
= unlowered_expr_type (init
);
875 if (((!warn_narrowing
|| !(complain
& tf_warning
))
876 && cxx_dialect
== cxx98
)
877 || !ARITHMETIC_TYPE_P (type
))
880 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
881 && TREE_CODE (type
) == COMPLEX_TYPE
)
883 tree elttype
= TREE_TYPE (type
);
884 if (CONSTRUCTOR_NELTS (init
) > 0)
885 ok
&= check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 0)->value
,
887 if (CONSTRUCTOR_NELTS (init
) > 1)
888 ok
&= check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 1)->value
,
893 init
= fold_non_dependent_expr (init
);
895 if (TREE_CODE (type
) == INTEGER_TYPE
896 && TREE_CODE (ftype
) == REAL_TYPE
)
898 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
899 && CP_INTEGRAL_TYPE_P (type
))
901 if (TREE_CODE (ftype
) == ENUMERAL_TYPE
)
902 /* Check for narrowing based on the values of the enumeration. */
903 ftype
= ENUM_UNDERLYING_TYPE (ftype
);
904 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type
),
905 TYPE_MAX_VALUE (ftype
))
906 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype
),
907 TYPE_MIN_VALUE (type
)))
908 && (TREE_CODE (init
) != INTEGER_CST
909 || !int_fits_type_p (init
, type
)))
912 else if (TREE_CODE (ftype
) == REAL_TYPE
913 && TREE_CODE (type
) == REAL_TYPE
)
915 if (TYPE_PRECISION (type
) < TYPE_PRECISION (ftype
))
917 if (TREE_CODE (init
) == REAL_CST
)
919 /* Issue 703: Loss of precision is OK as long as the value is
920 within the representable range of the new type. */
922 d
= TREE_REAL_CST (init
);
923 real_convert (&r
, TYPE_MODE (type
), &d
);
931 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
932 && TREE_CODE (type
) == REAL_TYPE
)
935 if (TREE_CODE (init
) == INTEGER_CST
)
937 d
= real_value_from_int_cst (0, init
);
938 if (exact_real_truncate (TYPE_MODE (type
), &d
))
944 if (!ok
&& !CONSTANT_CLASS_P (init
) && (complain
& tf_warning_or_error
))
946 tree folded
= cp_fully_fold (init
);
947 if (TREE_CONSTANT (folded
) && check_narrowing (type
, folded
, tf_none
))
953 location_t loc
= EXPR_LOC_OR_LOC (init
, input_location
);
954 if (cxx_dialect
== cxx98
)
956 if (complain
& tf_warning
)
957 warning_at (loc
, OPT_Wnarrowing
, "narrowing conversion of %qE "
958 "from %qH to %qI inside { } is ill-formed in C++11",
962 else if (!CONSTANT_CLASS_P (init
))
964 if (complain
& tf_warning_or_error
)
966 if ((!almost_ok
|| pedantic
)
967 && pedwarn (loc
, OPT_Wnarrowing
,
968 "narrowing conversion of %qE "
969 "from %qH to %qI inside { }",
972 inform (loc
, " the expression has a constant value but is not "
973 "a C++ constant-expression");
977 else if (complain
& tf_error
)
979 int savederrorcount
= errorcount
;
980 global_dc
->pedantic_errors
= 1;
981 pedwarn (loc
, OPT_Wnarrowing
,
982 "narrowing conversion of %qE from %qH to %qI "
983 "inside { }", init
, ftype
, type
);
984 if (errorcount
== savederrorcount
)
986 global_dc
->pedantic_errors
= flag_pedantic_errors
;
993 /* Process the initializer INIT for a variable of type TYPE, emitting
994 diagnostics for invalid initializers and converting the initializer as
997 For aggregate types, it assumes that reshape_init has already run, thus the
998 initializer will have the right shape (brace elision has been undone).
1000 NESTED is non-zero iff we are being called for an element of a CONSTRUCTOR,
1001 2 iff the element of a CONSTRUCTOR is inside another CONSTRUCTOR. */
1004 digest_init_r (tree type
, tree init
, int nested
, int flags
,
1005 tsubst_flags_t complain
)
1007 enum tree_code code
= TREE_CODE (type
);
1009 if (error_operand_p (init
))
1010 return error_mark_node
;
1014 /* We must strip the outermost array type when completing the type,
1015 because the its bounds might be incomplete at the moment. */
1016 if (!complete_type_or_maybe_complain (code
== ARRAY_TYPE
1017 ? TREE_TYPE (type
) : type
, NULL_TREE
,
1019 return error_mark_node
;
1021 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
1022 (g++.old-deja/g++.law/casts2.C). */
1023 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
1024 init
= TREE_OPERAND (init
, 0);
1026 location_t loc
= EXPR_LOC_OR_LOC (init
, input_location
);
1028 /* Initialization of an array of chars from a string constant. The initializer
1029 can be optionally enclosed in braces, but reshape_init has already removed
1030 them if they were present. */
1031 if (code
== ARRAY_TYPE
)
1033 if (nested
&& !TYPE_DOMAIN (type
))
1034 /* C++ flexible array members have a null domain. */
1035 pedwarn (loc
, OPT_Wpedantic
,
1036 "initialization of a flexible array member");
1038 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
1039 if (char_type_p (typ1
)
1041 && TREE_CODE (init
) == STRING_CST
)
1043 tree char_type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init
)));
1045 if (TYPE_PRECISION (typ1
) == BITS_PER_UNIT
)
1047 if (char_type
!= char_type_node
)
1049 if (complain
& tf_error
)
1050 error_at (loc
, "char-array initialized from wide string");
1051 return error_mark_node
;
1056 if (char_type
== char_type_node
)
1058 if (complain
& tf_error
)
1060 "int-array initialized from non-wide string");
1061 return error_mark_node
;
1063 else if (char_type
!= typ1
)
1065 if (complain
& tf_error
)
1066 error_at (loc
, "int-array initialized from incompatible "
1068 return error_mark_node
;
1072 if (nested
== 2 && !TYPE_DOMAIN (type
))
1074 if (complain
& tf_error
)
1075 error_at (loc
, "initialization of flexible array member "
1076 "in a nested context");
1077 return error_mark_node
;
1080 if (type
!= TREE_TYPE (init
)
1081 && !variably_modified_type_p (type
, NULL_TREE
))
1083 init
= copy_node (init
);
1084 TREE_TYPE (init
) = type
;
1086 if (TYPE_DOMAIN (type
) && TREE_CONSTANT (TYPE_SIZE (type
)))
1088 /* Not a flexible array member. */
1089 int size
= TREE_INT_CST_LOW (TYPE_SIZE (type
));
1090 size
= (size
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
;
1091 /* In C it is ok to subtract 1 from the length of the string
1092 because it's ok to ignore the terminating null char that is
1093 counted in the length of the constant, but in C++ this would
1095 if (size
< TREE_STRING_LENGTH (init
))
1096 permerror (loc
, "initializer-string for array "
1097 "of chars is too long");
1103 /* Handle scalar types (including conversions) and references. */
1104 if ((code
!= COMPLEX_TYPE
|| BRACE_ENCLOSED_INITIALIZER_P (init
))
1105 && (SCALAR_TYPE_P (type
) || code
== REFERENCE_TYPE
))
1108 flags
|= LOOKUP_NO_NARROWING
;
1109 init
= convert_for_initialization (0, type
, init
, flags
,
1110 ICR_INIT
, NULL_TREE
, 0,
1116 /* Come here only for aggregates: records, arrays, unions, complex numbers
1118 gcc_assert (code
== ARRAY_TYPE
1119 || VECTOR_TYPE_P (type
)
1120 || code
== RECORD_TYPE
1121 || code
== UNION_TYPE
1122 || code
== COMPLEX_TYPE
);
1124 /* "If T is a class type and the initializer list has a single
1125 element of type cv U, where U is T or a class derived from T,
1126 the object is initialized from that element." */
1128 && cxx_dialect
>= cxx11
1129 && BRACE_ENCLOSED_INITIALIZER_P (init
)
1130 && CONSTRUCTOR_NELTS (init
) == 1
1131 && ((CLASS_TYPE_P (type
) && !CLASSTYPE_NON_AGGREGATE (type
))
1132 || VECTOR_TYPE_P (type
)))
1134 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
1135 if (reference_related_p (type
, TREE_TYPE (elt
)))
1136 /* We should have fixed this in reshape_init. */
1140 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
1141 && !TYPE_NON_AGGREGATE_CLASS (type
))
1142 return process_init_constructor (type
, init
, nested
, complain
);
1145 if (COMPOUND_LITERAL_P (init
) && code
== ARRAY_TYPE
)
1147 if (complain
& tf_error
)
1148 error_at (loc
, "cannot initialize aggregate of type %qT with "
1149 "a compound literal", type
);
1151 return error_mark_node
;
1154 if (code
== ARRAY_TYPE
1155 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
1157 /* Allow the result of build_array_copy and of
1158 build_value_init_noctor. */
1159 if ((TREE_CODE (init
) == VEC_INIT_EXPR
1160 || TREE_CODE (init
) == CONSTRUCTOR
)
1161 && (same_type_ignoring_top_level_qualifiers_p
1162 (type
, TREE_TYPE (init
))))
1165 if (complain
& tf_error
)
1166 error_at (loc
, "array must be initialized with a brace-enclosed"
1168 return error_mark_node
;
1171 return convert_for_initialization (NULL_TREE
, type
, init
,
1173 ICR_INIT
, NULL_TREE
, 0,
1179 digest_init (tree type
, tree init
, tsubst_flags_t complain
)
1181 return digest_init_r (type
, init
, 0, LOOKUP_IMPLICIT
, complain
);
1185 digest_init_flags (tree type
, tree init
, int flags
, tsubst_flags_t complain
)
1187 return digest_init_r (type
, init
, 0, flags
, complain
);
1190 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1192 digest_nsdmi_init (tree decl
, tree init
, tsubst_flags_t complain
)
1194 gcc_assert (TREE_CODE (decl
) == FIELD_DECL
);
1196 tree type
= TREE_TYPE (decl
);
1197 int flags
= LOOKUP_IMPLICIT
;
1198 if (DIRECT_LIST_INIT_P (init
))
1199 flags
= LOOKUP_NORMAL
;
1200 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
1201 && CP_AGGREGATE_TYPE_P (type
))
1202 init
= reshape_init (type
, init
, complain
);
1203 init
= digest_init_flags (type
, init
, flags
, complain
);
1204 if (TREE_CODE (init
) == TARGET_EXPR
)
1205 /* This represents the whole initialization. */
1206 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
1210 /* Set of flags used within process_init_constructor to describe the
1212 #define PICFLAG_ERRONEOUS 1
1213 #define PICFLAG_NOT_ALL_CONSTANT 2
1214 #define PICFLAG_NOT_ALL_SIMPLE 4
1215 #define PICFLAG_SIDE_EFFECTS 8
1217 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1221 picflag_from_initializer (tree init
)
1223 if (init
== error_mark_node
)
1224 return PICFLAG_ERRONEOUS
;
1225 else if (!TREE_CONSTANT (init
))
1227 if (TREE_SIDE_EFFECTS (init
))
1228 return PICFLAG_SIDE_EFFECTS
;
1230 return PICFLAG_NOT_ALL_CONSTANT
;
1232 else if (!initializer_constant_valid_p (init
, TREE_TYPE (init
)))
1233 return PICFLAG_NOT_ALL_SIMPLE
;
1237 /* Adjust INIT for going into a CONSTRUCTOR. */
1240 massage_init_elt (tree type
, tree init
, int nested
, tsubst_flags_t complain
)
1242 init
= digest_init_r (type
, init
, nested
? 2 : 1, LOOKUP_IMPLICIT
, complain
);
1243 /* Strip a simple TARGET_EXPR when we know this is an initializer. */
1244 if (SIMPLE_TARGET_EXPR_P (init
))
1245 init
= TARGET_EXPR_INITIAL (init
);
1246 /* When we defer constant folding within a statement, we may want to
1247 defer this folding as well. */
1248 tree t
= fold_non_dependent_expr (init
);
1249 t
= maybe_constant_init (t
);
1250 if (TREE_CONSTANT (t
))
1255 /* Subroutine of process_init_constructor, which will process an initializer
1256 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1257 which describe the initializers. */
1260 process_init_constructor_array (tree type
, tree init
, int nested
,
1261 tsubst_flags_t complain
)
1263 unsigned HOST_WIDE_INT i
, len
= 0;
1265 bool unbounded
= false;
1266 constructor_elt
*ce
;
1267 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (init
);
1269 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
1270 || VECTOR_TYPE_P (type
));
1272 if (TREE_CODE (type
) == ARRAY_TYPE
)
1274 /* C++ flexible array members have a null domain. */
1275 tree domain
= TYPE_DOMAIN (type
);
1276 if (domain
&& TREE_CONSTANT (TYPE_MAX_VALUE (domain
)))
1277 len
= wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain
))
1278 - wi::to_offset (TYPE_MIN_VALUE (domain
)) + 1,
1279 TYPE_PRECISION (TREE_TYPE (domain
)),
1280 TYPE_SIGN (TREE_TYPE (domain
))).to_uhwi ();
1282 unbounded
= true; /* Take as many as there are. */
1284 if (nested
== 2 && !domain
&& !vec_safe_is_empty (v
))
1286 if (complain
& tf_error
)
1287 error_at (EXPR_LOC_OR_LOC (init
, input_location
),
1288 "initialization of flexible array member "
1289 "in a nested context");
1290 return PICFLAG_ERRONEOUS
;
1294 /* Vectors are like simple fixed-size arrays. */
1295 unbounded
= !TYPE_VECTOR_SUBPARTS (type
).is_constant (&len
);
1297 /* There must not be more initializers than needed. */
1298 if (!unbounded
&& vec_safe_length (v
) > len
)
1300 if (complain
& tf_error
)
1301 error ("too many initializers for %qT", type
);
1303 return PICFLAG_ERRONEOUS
;
1306 FOR_EACH_VEC_SAFE_ELT (v
, i
, ce
)
1309 ce
->index
= size_int (i
);
1310 else if (!check_array_designated_initializer (ce
, i
))
1311 ce
->index
= error_mark_node
;
1312 gcc_assert (ce
->value
);
1314 = massage_init_elt (TREE_TYPE (type
), ce
->value
, nested
, complain
);
1316 if (ce
->value
!= error_mark_node
)
1317 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1318 (TREE_TYPE (type
), TREE_TYPE (ce
->value
)));
1320 flags
|= picflag_from_initializer (ce
->value
);
1323 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1324 we must add initializers ourselves. */
1326 for (; i
< len
; ++i
)
1330 if (type_build_ctor_call (TREE_TYPE (type
)))
1332 /* If this type needs constructors run for default-initialization,
1333 we can't rely on the back end to do it for us, so make the
1334 initialization explicit by list-initializing from T{}. */
1335 next
= build_constructor (init_list_type_node
, NULL
);
1336 next
= massage_init_elt (TREE_TYPE (type
), next
, nested
, complain
);
1337 if (initializer_zerop (next
))
1338 /* The default zero-initialization is fine for us; don't
1339 add anything to the CONSTRUCTOR. */
1342 else if (!zero_init_p (TREE_TYPE (type
)))
1343 next
= build_zero_init (TREE_TYPE (type
),
1344 /*nelts=*/NULL_TREE
,
1345 /*static_storage_p=*/false);
1347 /* The default zero-initialization is fine for us; don't
1348 add anything to the CONSTRUCTOR. */
1353 flags
|= picflag_from_initializer (next
);
1354 CONSTRUCTOR_APPEND_ELT (v
, size_int (i
), next
);
1358 CONSTRUCTOR_ELTS (init
) = v
;
1362 /* Subroutine of process_init_constructor, which will process an initializer
1363 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1364 the initializers. */
1367 process_init_constructor_record (tree type
, tree init
, int nested
,
1368 tsubst_flags_t complain
)
1370 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1374 gcc_assert (TREE_CODE (type
) == RECORD_TYPE
);
1375 gcc_assert (!CLASSTYPE_VBASECLASSES (type
));
1376 gcc_assert (!TYPE_BINFO (type
)
1377 || cxx_dialect
>= cxx17
1378 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)));
1379 gcc_assert (!TYPE_POLYMORPHIC_P (type
));
1383 unsigned HOST_WIDE_INT idx
= 0;
1384 int designator_skip
= -1;
1385 /* Generally, we will always have an index for each initializer (which is
1386 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1387 reshape_init. So we need to handle both cases. */
1388 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1393 if (!DECL_NAME (field
) && DECL_C_BIT_FIELD (field
))
1396 if (TREE_CODE (field
) != FIELD_DECL
1397 || (DECL_ARTIFICIAL (field
)
1398 && !(cxx_dialect
>= cxx17
&& DECL_FIELD_IS_BASE (field
))))
1401 /* If this is a bitfield, first convert to the declared type. */
1402 type
= TREE_TYPE (field
);
1403 if (DECL_BIT_FIELD_TYPE (field
))
1404 type
= DECL_BIT_FIELD_TYPE (field
);
1405 if (type
== error_mark_node
)
1406 return PICFLAG_ERRONEOUS
;
1409 if (idx
< CONSTRUCTOR_NELTS (init
))
1411 constructor_elt
*ce
= &(*CONSTRUCTOR_ELTS (init
))[idx
];
1414 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1415 latter case can happen in templates where lookup has to be
1417 gcc_assert (TREE_CODE (ce
->index
) == FIELD_DECL
1418 || identifier_p (ce
->index
));
1419 if (ce
->index
== field
|| ce
->index
== DECL_NAME (field
))
1421 else if (ANON_AGGR_TYPE_P (type
)
1422 && search_anon_aggr (type
,
1423 TREE_CODE (ce
->index
) == FIELD_DECL
1424 ? DECL_NAME (ce
->index
)
1426 /* If the element is an anonymous union object and the
1427 initializer list is a designated-initializer-list, the
1428 anonymous union object is initialized by the
1429 designated-initializer-list { D }, where D is the
1430 designated-initializer-clause naming a member of the
1431 anonymous union object. */
1432 next
= build_constructor_single (type
, ce
->index
, ce
->value
);
1436 if (designator_skip
== -1)
1437 designator_skip
= 1;
1442 designator_skip
= 0;
1448 gcc_assert (ce
->value
);
1449 next
= massage_init_elt (type
, next
, nested
, complain
);
1454 /* Already handled above. */;
1455 else if (DECL_INITIAL (field
))
1459 /* We're using an NSDMI past a field with implicit
1460 zero-init. Go back and make it explicit. */
1462 vec_safe_truncate (v
, 0);
1465 /* C++14 aggregate NSDMI. */
1466 next
= get_nsdmi (field
, /*ctor*/false, complain
);
1468 else if (type_build_ctor_call (TREE_TYPE (field
)))
1470 /* If this type needs constructors run for
1471 default-initialization, we can't rely on the back end to do it
1472 for us, so build up TARGET_EXPRs. If the type in question is
1473 a class, just build one up; if it's an array, recurse. */
1474 next
= build_constructor (init_list_type_node
, NULL
);
1475 next
= massage_init_elt (TREE_TYPE (field
), next
, nested
, complain
);
1477 /* Warn when some struct elements are implicitly initialized. */
1478 if ((complain
& tf_warning
)
1479 && !EMPTY_CONSTRUCTOR_P (init
))
1480 warning (OPT_Wmissing_field_initializers
,
1481 "missing initializer for member %qD", field
);
1485 const_tree fldtype
= TREE_TYPE (field
);
1486 if (TREE_CODE (fldtype
) == REFERENCE_TYPE
)
1488 if (complain
& tf_error
)
1489 error ("member %qD is uninitialized reference", field
);
1491 return PICFLAG_ERRONEOUS
;
1493 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype
))
1495 if (complain
& tf_error
)
1496 error ("member %qD with uninitialized reference fields", field
);
1498 return PICFLAG_ERRONEOUS
;
1501 /* Warn when some struct elements are implicitly initialized
1502 to zero. However, avoid issuing the warning for flexible
1503 array members since they need not have any elements. */
1504 if ((TREE_CODE (fldtype
) != ARRAY_TYPE
|| TYPE_DOMAIN (fldtype
))
1505 && (complain
& tf_warning
)
1506 && !EMPTY_CONSTRUCTOR_P (init
))
1507 warning (OPT_Wmissing_field_initializers
,
1508 "missing initializer for member %qD", field
);
1510 if (!zero_init_p (fldtype
)
1512 next
= build_zero_init (TREE_TYPE (field
), /*nelts=*/NULL_TREE
,
1513 /*static_storage_p=*/false);
1516 /* The default zero-initialization is fine for us; don't
1517 add anything to the CONSTRUCTOR. */
1523 /* If this is a bitfield, now convert to the lowered type. */
1524 if (type
!= TREE_TYPE (field
))
1525 next
= cp_convert_and_check (TREE_TYPE (field
), next
, complain
);
1526 flags
|= picflag_from_initializer (next
);
1527 CONSTRUCTOR_APPEND_ELT (v
, field
, next
);
1530 if (idx
< CONSTRUCTOR_NELTS (init
))
1532 if (complain
& tf_error
)
1534 constructor_elt
*ce
= &(*CONSTRUCTOR_ELTS (init
))[idx
];
1535 /* For better diagnostics, try to find out if it is really
1536 the case of too many initializers or if designators are
1537 in incorrect order. */
1538 if (designator_skip
== 1 && ce
->index
)
1540 gcc_assert (TREE_CODE (ce
->index
) == FIELD_DECL
1541 || identifier_p (ce
->index
));
1542 for (field
= TYPE_FIELDS (type
);
1543 field
; field
= DECL_CHAIN (field
))
1545 if (!DECL_NAME (field
) && DECL_C_BIT_FIELD (field
))
1547 if (TREE_CODE (field
) != FIELD_DECL
1548 || (DECL_ARTIFICIAL (field
)
1549 && !(cxx_dialect
>= cxx17
1550 && DECL_FIELD_IS_BASE (field
))))
1553 if (ce
->index
== field
|| ce
->index
== DECL_NAME (field
))
1555 if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1558 = search_anon_aggr (TREE_TYPE (field
),
1559 TREE_CODE (ce
->index
) == FIELD_DECL
1560 ? DECL_NAME (ce
->index
)
1571 error ("designator order for field %qD does not match declaration "
1572 "order in %qT", field
, type
);
1574 error ("too many initializers for %qT", type
);
1577 return PICFLAG_ERRONEOUS
;
1580 CONSTRUCTOR_ELTS (init
) = v
;
1584 /* Subroutine of process_init_constructor, which will process a single
1585 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1586 which describe the initializer. */
1589 process_init_constructor_union (tree type
, tree init
, int nested
,
1590 tsubst_flags_t complain
)
1592 constructor_elt
*ce
;
1595 /* If the initializer was empty, use the union's NSDMI if it has one.
1596 Otherwise use default zero initialization. */
1597 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
1599 for (tree field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1601 if (TREE_CODE (field
) == FIELD_DECL
1602 && DECL_INITIAL (field
) != NULL_TREE
)
1604 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init
),
1606 get_nsdmi (field
, /*in_ctor=*/false,
1612 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
1616 len
= CONSTRUCTOR_ELTS (init
)->length ();
1619 if (!(complain
& tf_error
))
1620 return PICFLAG_ERRONEOUS
;
1621 error ("too many initializers for %qT", type
);
1622 CONSTRUCTOR_ELTS (init
)->block_remove (1, len
-1);
1625 ce
= &(*CONSTRUCTOR_ELTS (init
))[0];
1627 /* If this element specifies a field, initialize via that field. */
1630 if (TREE_CODE (ce
->index
) == FIELD_DECL
)
1632 else if (identifier_p (ce
->index
))
1634 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1635 tree name
= ce
->index
;
1637 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1638 if (DECL_NAME (field
) == name
)
1642 if (complain
& tf_error
)
1643 error ("no field %qD found in union being initialized",
1645 ce
->value
= error_mark_node
;
1651 gcc_assert (TREE_CODE (ce
->index
) == INTEGER_CST
1652 || TREE_CODE (ce
->index
) == RANGE_EXPR
);
1653 if (complain
& tf_error
)
1654 error ("index value instead of field name in union initializer");
1655 ce
->value
= error_mark_node
;
1660 /* Find the first named field. ANSI decided in September 1990
1661 that only named fields count here. */
1662 tree field
= TYPE_FIELDS (type
);
1663 while (field
&& (!DECL_NAME (field
) || TREE_CODE (field
) != FIELD_DECL
))
1664 field
= TREE_CHAIN (field
);
1665 if (field
== NULL_TREE
)
1667 if (complain
& tf_error
)
1668 error ("too many initializers for %qT", type
);
1669 ce
->value
= error_mark_node
;
1674 if (ce
->value
&& ce
->value
!= error_mark_node
)
1675 ce
->value
= massage_init_elt (TREE_TYPE (ce
->index
), ce
->value
, nested
,
1678 return picflag_from_initializer (ce
->value
);
1681 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1682 constructor is a brace-enclosed initializer, and will be modified in-place.
1684 Each element is converted to the right type through digest_init, and
1685 missing initializers are added following the language rules (zero-padding,
1688 After the execution, the initializer will have TREE_CONSTANT if all elts are
1689 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1690 constants that the assembler and linker can compute them.
1692 The function returns the initializer itself, or error_mark_node in case
1696 process_init_constructor (tree type
, tree init
, int nested
,
1697 tsubst_flags_t complain
)
1701 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init
));
1703 if (TREE_CODE (type
) == ARRAY_TYPE
|| VECTOR_TYPE_P (type
))
1704 flags
= process_init_constructor_array (type
, init
, nested
, complain
);
1705 else if (TREE_CODE (type
) == RECORD_TYPE
)
1706 flags
= process_init_constructor_record (type
, init
, nested
, complain
);
1707 else if (TREE_CODE (type
) == UNION_TYPE
)
1708 flags
= process_init_constructor_union (type
, init
, nested
, complain
);
1712 if (flags
& PICFLAG_ERRONEOUS
)
1713 return error_mark_node
;
1715 TREE_TYPE (init
) = type
;
1716 if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
) == NULL_TREE
)
1717 cp_complete_array_type (&TREE_TYPE (init
), init
, /*do_default=*/0);
1718 if (flags
& PICFLAG_SIDE_EFFECTS
)
1720 TREE_CONSTANT (init
) = false;
1721 TREE_SIDE_EFFECTS (init
) = true;
1723 else if (flags
& PICFLAG_NOT_ALL_CONSTANT
)
1724 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1725 TREE_CONSTANT (init
) = false;
1728 TREE_CONSTANT (init
) = 1;
1729 if (!(flags
& PICFLAG_NOT_ALL_SIMPLE
))
1730 TREE_STATIC (init
) = 1;
1735 /* Given a structure or union value DATUM, construct and return
1736 the structure or union component which results from narrowing
1737 that value to the base specified in BASETYPE. For example, given the
1740 class L { int ii; };
1741 class A : L { ... };
1742 class B : L { ... };
1743 class C : A, B { ... };
1751 x.A::ii refers to the ii member of the L part of
1752 the A part of the C object named by X. In this case,
1753 DATUM would be x, and BASETYPE would be A.
1755 I used to think that this was nonconformant, that the standard specified
1756 that first we look up ii in A, then convert x to an L& and pull out the
1757 ii part. But in fact, it does say that we convert x to an A&; A here
1758 is known as the "naming class". (jason 2000-12-19)
1760 BINFO_P points to a variable initialized either to NULL_TREE or to the
1761 binfo for the specific base subobject we want to convert to. */
1764 build_scoped_ref (tree datum
, tree basetype
, tree
* binfo_p
)
1768 if (datum
== error_mark_node
)
1769 return error_mark_node
;
1773 binfo
= lookup_base (TREE_TYPE (datum
), basetype
, ba_check
,
1774 NULL
, tf_warning_or_error
);
1776 if (!binfo
|| binfo
== error_mark_node
)
1778 *binfo_p
= NULL_TREE
;
1780 error_not_base_type (basetype
, TREE_TYPE (datum
));
1781 return error_mark_node
;
1785 return build_base_path (PLUS_EXPR
, datum
, binfo
, 1,
1786 tf_warning_or_error
);
1789 /* Build a reference to an object specified by the C++ `->' operator.
1790 Usually this just involves dereferencing the object, but if the
1791 `->' operator is overloaded, then such overloads must be
1792 performed until an object which does not have the `->' operator
1793 overloaded is found. An error is reported when circular pointer
1794 delegation is detected. */
1797 build_x_arrow (location_t loc
, tree expr
, tsubst_flags_t complain
)
1799 tree orig_expr
= expr
;
1800 tree type
= TREE_TYPE (expr
);
1801 tree last_rval
= NULL_TREE
;
1802 vec
<tree
, va_gc
> *types_memoized
= NULL
;
1804 if (type
== error_mark_node
)
1805 return error_mark_node
;
1807 if (processing_template_decl
)
1809 if (type
&& TREE_CODE (type
) == POINTER_TYPE
1810 && !dependent_scope_p (TREE_TYPE (type
)))
1811 /* Pointer to current instantiation, don't treat as dependent. */;
1812 else if (type_dependent_expression_p (expr
))
1813 return build_min_nt_loc (loc
, ARROW_EXPR
, expr
);
1814 expr
= build_non_dependent_expr (expr
);
1817 if (MAYBE_CLASS_TYPE_P (type
))
1819 struct tinst_level
*actual_inst
= current_instantiation ();
1822 while ((expr
= build_new_op (loc
, COMPONENT_REF
,
1823 LOOKUP_NORMAL
, expr
, NULL_TREE
, NULL_TREE
,
1826 if (expr
== error_mark_node
)
1827 return error_mark_node
;
1829 /* This provides a better instantiation backtrace in case of
1831 if (fn
&& DECL_USE_TEMPLATE (fn
))
1832 push_tinst_level_loc (fn
,
1833 (current_instantiation () != actual_inst
)
1834 ? DECL_SOURCE_LOCATION (fn
)
1838 if (vec_member (TREE_TYPE (expr
), types_memoized
))
1840 if (complain
& tf_error
)
1841 error ("circular pointer delegation detected");
1842 return error_mark_node
;
1845 vec_safe_push (types_memoized
, TREE_TYPE (expr
));
1849 while (current_instantiation () != actual_inst
)
1852 if (last_rval
== NULL_TREE
)
1854 if (complain
& tf_error
)
1855 error ("base operand of %<->%> has non-pointer type %qT", type
);
1856 return error_mark_node
;
1859 if (TREE_CODE (TREE_TYPE (last_rval
)) == REFERENCE_TYPE
)
1860 last_rval
= convert_from_reference (last_rval
);
1863 last_rval
= decay_conversion (expr
, complain
);
1865 if (TYPE_PTR_P (TREE_TYPE (last_rval
)))
1867 if (processing_template_decl
)
1869 expr
= build_min (ARROW_EXPR
, TREE_TYPE (TREE_TYPE (last_rval
)),
1871 TREE_SIDE_EFFECTS (expr
) = TREE_SIDE_EFFECTS (last_rval
);
1875 return cp_build_indirect_ref (last_rval
, RO_ARROW
, complain
);
1878 if (complain
& tf_error
)
1881 error ("result of %<operator->()%> yields non-pointer result");
1883 error ("base operand of %<->%> is not a pointer");
1885 return error_mark_node
;
1888 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1889 already been checked out to be of aggregate type. */
1892 build_m_component_ref (tree datum
, tree component
, tsubst_flags_t complain
)
1900 if (error_operand_p (datum
) || error_operand_p (component
))
1901 return error_mark_node
;
1903 datum
= mark_lvalue_use (datum
);
1904 component
= mark_rvalue_use (component
);
1906 ptrmem_type
= TREE_TYPE (component
);
1907 if (!TYPE_PTRMEM_P (ptrmem_type
))
1909 if (complain
& tf_error
)
1910 error ("%qE cannot be used as a member pointer, since it is of "
1911 "type %qT", component
, ptrmem_type
);
1912 return error_mark_node
;
1915 objtype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
1916 if (! MAYBE_CLASS_TYPE_P (objtype
))
1918 if (complain
& tf_error
)
1919 error ("cannot apply member pointer %qE to %qE, which is of "
1920 "non-class type %qT", component
, datum
, objtype
);
1921 return error_mark_node
;
1924 type
= TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type
);
1925 ctype
= complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type
));
1927 if (!COMPLETE_TYPE_P (ctype
))
1929 if (!same_type_p (ctype
, objtype
))
1935 binfo
= lookup_base (objtype
, ctype
, ba_check
, NULL
, complain
);
1940 if (complain
& tf_error
)
1941 error ("pointer to member type %qT incompatible with object "
1942 "type %qT", type
, objtype
);
1943 return error_mark_node
;
1945 else if (binfo
== error_mark_node
)
1946 return error_mark_node
;
1949 if (TYPE_PTRDATAMEM_P (ptrmem_type
))
1951 cp_lvalue_kind kind
= lvalue_kind (datum
);
1954 /* Compute the type of the field, as described in [expr.ref].
1955 There's no such thing as a mutable pointer-to-member, so
1956 things are not as complex as they are for references to
1957 non-static data members. */
1958 type
= cp_build_qualified_type (type
,
1959 (cp_type_quals (type
)
1960 | cp_type_quals (TREE_TYPE (datum
))));
1962 datum
= build_address (datum
);
1964 /* Convert object to the correct base. */
1967 datum
= build_base_path (PLUS_EXPR
, datum
, binfo
, 1, complain
);
1968 if (datum
== error_mark_node
)
1969 return error_mark_node
;
1972 /* Build an expression for "object + offset" where offset is the
1973 value stored in the pointer-to-data-member. */
1974 ptype
= build_pointer_type (type
);
1975 datum
= fold_build_pointer_plus (fold_convert (ptype
, datum
), component
);
1976 datum
= cp_build_fold_indirect_ref (datum
);
1977 if (datum
== error_mark_node
)
1978 return error_mark_node
;
1980 /* If the object expression was an rvalue, return an rvalue. */
1981 if (kind
& clk_class
)
1982 datum
= rvalue (datum
);
1983 else if (kind
& clk_rvalueref
)
1984 datum
= move (datum
);
1989 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
1990 program is ill-formed if the second operand is a pointer to member
1991 function with ref-qualifier & (for C++2A: unless its cv-qualifier-seq
1992 is const). In a .* expression whose object expression is an lvalue,
1993 the program is ill-formed if the second operand is a pointer to member
1994 function with ref-qualifier &&. */
1995 if (FUNCTION_REF_QUALIFIED (type
))
1997 bool lval
= lvalue_p (datum
);
1998 if (lval
&& FUNCTION_RVALUE_QUALIFIED (type
))
2000 if (complain
& tf_error
)
2001 error ("pointer-to-member-function type %qT requires an rvalue",
2003 return error_mark_node
;
2005 else if (!lval
&& !FUNCTION_RVALUE_QUALIFIED (type
))
2007 if ((type_memfn_quals (type
)
2008 & (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
))
2011 if (complain
& tf_error
)
2012 error ("pointer-to-member-function type %qT requires "
2013 "an lvalue", ptrmem_type
);
2014 return error_mark_node
;
2016 else if (cxx_dialect
< cxx2a
)
2018 if (complain
& tf_warning_or_error
)
2019 pedwarn (input_location
, OPT_Wpedantic
,
2020 "pointer-to-member-function type %qT requires "
2021 "an lvalue before C++2a", ptrmem_type
);
2023 return error_mark_node
;
2027 return build2 (OFFSET_REF
, type
, datum
, component
);
2031 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
2034 build_functional_cast (tree exp
, tree parms
, tsubst_flags_t complain
)
2036 /* This is either a call to a constructor,
2037 or a C cast in C++'s `functional' notation. */
2039 /* The type to which we are casting. */
2041 vec
<tree
, va_gc
> *parmvec
;
2043 if (error_operand_p (exp
) || parms
== error_mark_node
)
2044 return error_mark_node
;
2046 if (TREE_CODE (exp
) == TYPE_DECL
)
2048 type
= TREE_TYPE (exp
);
2050 if (complain
& tf_warning
2051 && TREE_DEPRECATED (type
)
2052 && DECL_ARTIFICIAL (exp
))
2053 warn_deprecated_use (type
, NULL_TREE
);
2058 /* We need to check this explicitly, since value-initialization of
2059 arrays is allowed in other situations. */
2060 if (TREE_CODE (type
) == ARRAY_TYPE
)
2062 if (complain
& tf_error
)
2063 error ("functional cast to array type %qT", type
);
2064 return error_mark_node
;
2067 if (tree anode
= type_uses_auto (type
))
2069 if (!CLASS_PLACEHOLDER_TEMPLATE (anode
))
2071 if (complain
& tf_error
)
2072 error ("invalid use of %qT", anode
);
2073 return error_mark_node
;
2077 if (complain
& tf_error
)
2078 error ("cannot deduce template arguments for %qT from ()", anode
);
2079 return error_mark_node
;
2082 type
= do_auto_deduction (type
, parms
, anode
, complain
,
2086 if (processing_template_decl
)
2090 /* Diagnose this even in a template. We could also try harder
2091 to give all the usual errors when the type and args are
2093 if (TREE_CODE (type
) == REFERENCE_TYPE
&& !parms
)
2095 if (complain
& tf_error
)
2096 error ("invalid value-initialization of reference type");
2097 return error_mark_node
;
2100 t
= build_min (CAST_EXPR
, type
, parms
);
2101 /* We don't know if it will or will not have side effects. */
2102 TREE_SIDE_EFFECTS (t
) = 1;
2106 if (! MAYBE_CLASS_TYPE_P (type
))
2108 if (parms
== NULL_TREE
)
2110 if (VOID_TYPE_P (type
))
2112 return build_value_init (cv_unqualified (type
), complain
);
2115 /* This must build a C cast. */
2116 parms
= build_x_compound_expr_from_list (parms
, ELK_FUNC_CAST
, complain
);
2117 return cp_build_c_cast (type
, parms
, complain
);
2120 /* Prepare to evaluate as a call to a constructor. If this expression
2121 is actually used, for example,
2123 return X (arg1, arg2, ...);
2125 then the slot being initialized will be filled in. */
2127 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
2128 return error_mark_node
;
2129 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
2130 return error_mark_node
;
2134 If the expression list is a single-expression, the type
2135 conversion is equivalent (in definedness, and if defined in
2136 meaning) to the corresponding cast expression. */
2137 if (parms
&& TREE_CHAIN (parms
) == NULL_TREE
)
2138 return cp_build_c_cast (type
, TREE_VALUE (parms
), complain
);
2142 The expression T(), where T is a simple-type-specifier for a
2143 non-array complete object type or the (possibly cv-qualified)
2144 void type, creates an rvalue of the specified type, which is
2145 value-initialized. */
2147 if (parms
== NULL_TREE
)
2149 exp
= build_value_init (type
, complain
);
2150 exp
= get_target_expr_sfinae (exp
, complain
);
2154 /* Call the constructor. */
2155 parmvec
= make_tree_vector ();
2156 for (; parms
!= NULL_TREE
; parms
= TREE_CHAIN (parms
))
2157 vec_safe_push (parmvec
, TREE_VALUE (parms
));
2158 exp
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
2159 &parmvec
, type
, LOOKUP_NORMAL
, complain
);
2160 release_tree_vector (parmvec
);
2162 if (exp
== error_mark_node
)
2163 return error_mark_node
;
2165 return build_cplus_new (type
, exp
, complain
);
2169 /* Add new exception specifier SPEC, to the LIST we currently have.
2170 If it's already in LIST then do nothing.
2171 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2172 know what we're doing. */
2175 add_exception_specifier (tree list
, tree spec
, int complain
)
2180 diagnostic_t diag_type
= DK_UNSPECIFIED
; /* none */
2182 if (spec
== error_mark_node
)
2185 gcc_assert (spec
&& (!list
|| TREE_VALUE (list
)));
2187 /* [except.spec] 1, type in an exception specifier shall not be
2188 incomplete, or pointer or ref to incomplete other than pointer
2190 is_ptr
= TYPE_PTR_P (core
);
2191 if (is_ptr
|| TREE_CODE (core
) == REFERENCE_TYPE
)
2192 core
= TREE_TYPE (core
);
2195 else if (VOID_TYPE_P (core
))
2197 else if (TREE_CODE (core
) == TEMPLATE_TYPE_PARM
)
2199 else if (processing_template_decl
)
2204 /* 15.4/1 says that types in an exception specifier must be complete,
2205 but it seems more reasonable to only require this on definitions
2206 and calls. So just give a pedwarn at this point; we will give an
2207 error later if we hit one of those two cases. */
2208 if (!COMPLETE_TYPE_P (complete_type (core
)))
2209 diag_type
= DK_PEDWARN
; /* pedwarn */
2216 for (probe
= list
; probe
; probe
= TREE_CHAIN (probe
))
2217 if (same_type_p (TREE_VALUE (probe
), spec
))
2220 list
= tree_cons (NULL_TREE
, spec
, list
);
2223 diag_type
= DK_ERROR
; /* error */
2225 if (diag_type
!= DK_UNSPECIFIED
2226 && (complain
& tf_warning_or_error
))
2227 cxx_incomplete_type_diagnostic (NULL_TREE
, core
, diag_type
);
2232 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2235 nothrow_spec_p_uninst (const_tree spec
)
2237 if (DEFERRED_NOEXCEPT_SPEC_P (spec
))
2239 return nothrow_spec_p (spec
);
2242 /* Combine the two exceptions specifier lists LIST and ADD, and return
2246 merge_exception_specifiers (tree list
, tree add
)
2248 tree noex
, orig_list
;
2250 /* No exception-specifier or noexcept(false) are less strict than
2251 anything else. Prefer the newer variant (LIST). */
2252 if (!list
|| list
== noexcept_false_spec
)
2254 else if (!add
|| add
== noexcept_false_spec
)
2257 /* noexcept(true) and throw() are stricter than anything else.
2258 As above, prefer the more recent one (LIST). */
2259 if (nothrow_spec_p_uninst (add
))
2262 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2263 if (UNEVALUATED_NOEXCEPT_SPEC_P (add
)
2264 && UNEVALUATED_NOEXCEPT_SPEC_P (list
))
2266 /* We should have instantiated other deferred noexcept specs by now. */
2267 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add
));
2269 if (nothrow_spec_p_uninst (list
))
2271 noex
= TREE_PURPOSE (list
);
2272 gcc_checking_assert (!TREE_PURPOSE (add
)
2273 || errorcount
|| !flag_exceptions
2274 || cp_tree_equal (noex
, TREE_PURPOSE (add
)));
2276 /* Combine the dynamic-exception-specifiers, if any. */
2278 for (; add
&& TREE_VALUE (add
); add
= TREE_CHAIN (add
))
2280 tree spec
= TREE_VALUE (add
);
2283 for (probe
= orig_list
; probe
&& TREE_VALUE (probe
);
2284 probe
= TREE_CHAIN (probe
))
2285 if (same_type_p (TREE_VALUE (probe
), spec
))
2289 spec
= build_tree_list (NULL_TREE
, spec
);
2290 TREE_CHAIN (spec
) = list
;
2295 /* Keep the noexcept-specifier at the beginning of the list. */
2296 if (noex
!= TREE_PURPOSE (list
))
2297 list
= tree_cons (noex
, TREE_VALUE (list
), TREE_CHAIN (list
));
2302 /* Subroutine of build_call. Ensure that each of the types in the
2303 exception specification is complete. Technically, 15.4/1 says that
2304 they need to be complete when we see a declaration of the function,
2305 but we should be able to get away with only requiring this when the
2306 function is defined or called. See also add_exception_specifier. */
2309 require_complete_eh_spec_types (tree fntype
, tree decl
)
2312 /* Don't complain about calls to op new. */
2313 if (decl
&& DECL_ARTIFICIAL (decl
))
2315 for (raises
= TYPE_RAISES_EXCEPTIONS (fntype
); raises
;
2316 raises
= TREE_CHAIN (raises
))
2318 tree type
= TREE_VALUE (raises
);
2319 if (type
&& !COMPLETE_TYPE_P (type
))
2323 ("call to function %qD which throws incomplete type %q#T",
2326 error ("call to function which throws incomplete type %q#T",
2333 #include "gt-cp-typeck2.h"