1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987-2016 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
, tsubst_flags_t complain
);
40 /* Print an error message stemming from an attempt to use
41 BASETYPE as a base class for TYPE. */
44 error_not_base_type (tree basetype
, tree type
)
46 if (TREE_CODE (basetype
) == FUNCTION_DECL
)
47 basetype
= DECL_CONTEXT (basetype
);
48 error ("type %qT is not a base type for type %qT", basetype
, type
);
49 return error_mark_node
;
53 binfo_or_else (tree base
, tree type
)
55 tree binfo
= lookup_base (type
, base
, ba_unique
,
56 NULL
, tf_warning_or_error
);
58 if (binfo
== error_mark_node
)
61 error_not_base_type (base
, type
);
65 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
66 value may not be changed thereafter. */
69 cxx_readonly_error (tree arg
, enum lvalue_use errstring
)
72 /* This macro is used to emit diagnostics to ensure that all format
73 strings are complete sentences, visible to gettext and checked at
76 #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG) \
97 /* Handle C++-specific things first. */
100 && DECL_LANG_SPECIFIC (arg
)
101 && DECL_IN_AGGR_P (arg
)
102 && !TREE_STATIC (arg
))
103 ERROR_FOR_ASSIGNMENT (G_("assignment of "
104 "constant field %qD"),
105 G_("constant field %qD "
106 "used as %<asm%> output"),
108 "constant field %qD"),
110 "constant field %qD"),
112 else if (INDIRECT_REF_P (arg
)
113 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 0))) == REFERENCE_TYPE
114 && (VAR_P (TREE_OPERAND (arg
, 0))
115 || TREE_CODE (TREE_OPERAND (arg
, 0)) == PARM_DECL
))
116 ERROR_FOR_ASSIGNMENT (G_("assignment of "
117 "read-only reference %qD"),
118 G_("read-only reference %qD "
119 "used as %<asm%> output"),
121 "read-only reference %qD"),
123 "read-only reference %qD"),
124 TREE_OPERAND (arg
, 0));
126 readonly_error (input_location
, arg
, errstring
);
130 /* Structure that holds information about declarations whose type was
131 incomplete and we could not check whether it was abstract or not. */
133 struct GTY((chain_next ("%h.next"), for_user
)) pending_abstract_type
{
134 /* Declaration which we are checking for abstractness. It is either
135 a DECL node, or an IDENTIFIER_NODE if we do not have a full
136 declaration available. */
139 /* Type which will be checked for abstractness. */
142 /* Kind of use in an unnamed declarator. */
143 enum abstract_class_use use
;
145 /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
146 because DECLs already carry locus information. */
149 /* Link to the next element in list. */
150 struct pending_abstract_type
* next
;
153 struct abstract_type_hasher
: ggc_ptr_hash
<pending_abstract_type
>
155 typedef tree compare_type
;
156 static hashval_t
hash (pending_abstract_type
*);
157 static bool equal (pending_abstract_type
*, tree
);
160 /* Compute the hash value of the node VAL. This function is used by the
161 hash table abstract_pending_vars. */
164 abstract_type_hasher::hash (pending_abstract_type
*pat
)
166 return (hashval_t
) TYPE_UID (pat
->type
);
170 /* Compare node VAL1 with the type VAL2. This function is used by the
171 hash table abstract_pending_vars. */
174 abstract_type_hasher::equal (pending_abstract_type
*pat1
, tree type2
)
176 return (pat1
->type
== type2
);
179 /* Hash table that maintains pending_abstract_type nodes, for which we still
180 need to check for type abstractness. The key of the table is the type
181 of the declaration. */
182 static GTY (()) hash_table
<abstract_type_hasher
> *abstract_pending_vars
= NULL
;
184 static int abstract_virtuals_error_sfinae (tree
, tree
, abstract_class_use
, tsubst_flags_t
);
186 /* This function is called after TYPE is completed, and will check if there
187 are pending declarations for which we still need to verify the abstractness
188 of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
189 turned out to be incomplete. */
192 complete_type_check_abstract (tree type
)
194 struct pending_abstract_type
*pat
;
195 location_t cur_loc
= input_location
;
197 gcc_assert (COMPLETE_TYPE_P (type
));
199 if (!abstract_pending_vars
)
202 /* Retrieve the list of pending declarations for this type. */
203 pending_abstract_type
**slot
204 = abstract_pending_vars
->find_slot_with_hash (type
, TYPE_UID (type
),
211 /* If the type is not abstract, do not do anything. */
212 if (CLASSTYPE_PURE_VIRTUALS (type
))
214 struct pending_abstract_type
*prev
= 0, *next
;
216 /* Reverse the list to emit the errors in top-down order. */
217 for (; pat
; pat
= next
)
225 /* Go through the list, and call abstract_virtuals_error for each
226 element: it will issue a diagnostic if the type is abstract. */
229 gcc_assert (type
== pat
->type
);
231 /* Tweak input_location so that the diagnostic appears at the correct
232 location. Notice that this is only needed if the decl is an
234 input_location
= pat
->locus
;
235 abstract_virtuals_error_sfinae (pat
->decl
, pat
->type
, pat
->use
,
236 tf_warning_or_error
);
241 abstract_pending_vars
->clear_slot (slot
);
243 input_location
= cur_loc
;
247 /* If TYPE has abstract virtual functions, issue an error about trying
248 to create an object of that type. DECL is the object declared, or
249 NULL_TREE if the declaration is unavailable, in which case USE specifies
250 the kind of invalid use. Returns 1 if an error occurred; zero if
254 abstract_virtuals_error_sfinae (tree decl
, tree type
, abstract_class_use use
,
255 tsubst_flags_t complain
)
257 vec
<tree
, va_gc
> *pure
;
259 /* This function applies only to classes. Any other entity can never
261 if (!CLASS_TYPE_P (type
))
263 type
= TYPE_MAIN_VARIANT (type
);
266 /* Instantiation here seems to be required by the standard,
267 but breaks e.g. boost::bind. FIXME! */
268 /* In SFINAE, non-N3276 context, force instantiation. */
269 if (!(complain
& (tf_error
|tf_decltype
)))
270 complete_type (type
);
273 /* If the type is incomplete, we register it within a hash table,
274 so that we can check again once it is completed. This makes sense
275 only for objects for which we have a declaration or at least a
277 if (!COMPLETE_TYPE_P (type
) && (complain
& tf_error
))
279 struct pending_abstract_type
*pat
;
281 gcc_assert (!decl
|| DECL_P (decl
) || identifier_p (decl
));
283 if (!abstract_pending_vars
)
284 abstract_pending_vars
285 = hash_table
<abstract_type_hasher
>::create_ggc (31);
287 pending_abstract_type
**slot
288 = abstract_pending_vars
->find_slot_with_hash (type
, TYPE_UID (type
),
291 pat
= ggc_alloc
<pending_abstract_type
> ();
295 pat
->locus
= ((decl
&& DECL_P (decl
))
296 ? DECL_SOURCE_LOCATION (decl
)
305 if (!TYPE_SIZE (type
))
306 /* TYPE is being defined, and during that time
307 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
310 pure
= CLASSTYPE_PURE_VIRTUALS (type
);
314 if (!(complain
& tf_error
))
320 error ("cannot declare variable %q+D to be of abstract "
321 "type %qT", decl
, type
);
322 else if (TREE_CODE (decl
) == PARM_DECL
)
324 if (DECL_NAME (decl
))
325 error ("cannot declare parameter %q+D to be of abstract type %qT",
328 error ("cannot declare parameter to be of abstract type %qT",
331 else if (TREE_CODE (decl
) == FIELD_DECL
)
332 error ("cannot declare field %q+D to be of abstract type %qT",
334 else if (TREE_CODE (decl
) == FUNCTION_DECL
335 && TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
336 error ("invalid abstract return type for member function %q+#D", decl
);
337 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
338 error ("invalid abstract return type for function %q+#D", decl
);
339 else if (identifier_p (decl
))
340 /* Here we do not have location information. */
341 error ("invalid abstract type %qT for %qE", type
, decl
);
343 error ("invalid abstract type for %q+D", decl
);
348 error ("creating array of %qT, which is an abstract class type", type
);
351 error ("invalid cast to abstract class type %qT", type
);
354 error ("invalid new-expression of abstract class type %qT", type
);
357 error ("invalid abstract return type %qT", type
);
360 error ("invalid abstract parameter type %qT", type
);
363 error ("expression of abstract class type %qT cannot "
364 "be used in throw-expression", type
);
367 error ("cannot declare catch parameter to be of abstract "
368 "class type %qT", type
);
371 error ("cannot allocate an object of abstract type %qT", type
);
374 /* Only go through this once. */
380 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
381 " because the following virtual functions are pure within %qT:",
384 FOR_EACH_VEC_ELT (*pure
, ix
, fn
)
385 if (! DECL_CLONED_FUNCTION_P (fn
)
386 || DECL_COMPLETE_DESTRUCTOR_P (fn
))
387 inform (DECL_SOURCE_LOCATION (fn
), "\t%#D", fn
);
389 /* Now truncate the vector. This leaves it non-null, so we know
390 there are pure virtuals, but empty so we don't list them out
399 abstract_virtuals_error_sfinae (tree decl
, tree type
, tsubst_flags_t complain
)
401 return abstract_virtuals_error_sfinae (decl
, type
, ACU_UNKNOWN
, complain
);
405 abstract_virtuals_error_sfinae (abstract_class_use use
, tree type
,
406 tsubst_flags_t complain
)
408 return abstract_virtuals_error_sfinae (NULL_TREE
, type
, use
, complain
);
412 /* Wrapper for the above function in the common case of wanting errors. */
415 abstract_virtuals_error (tree decl
, tree type
)
417 return abstract_virtuals_error_sfinae (decl
, type
, tf_warning_or_error
);
421 abstract_virtuals_error (abstract_class_use use
, tree type
)
423 return abstract_virtuals_error_sfinae (use
, type
, tf_warning_or_error
);
426 /* Print an inform about the declaration of the incomplete type TYPE. */
429 cxx_incomplete_type_inform (const_tree type
)
431 if (!TYPE_MAIN_DECL (type
))
434 location_t loc
= DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
));
435 tree ptype
= strip_top_quals (CONST_CAST_TREE (type
));
437 if (current_class_type
438 && TYPE_BEING_DEFINED (current_class_type
)
439 && same_type_p (ptype
, current_class_type
))
440 inform (loc
, "definition of %q#T is not complete until "
441 "the closing brace", ptype
);
442 else if (!TYPE_TEMPLATE_INFO (ptype
))
443 inform (loc
, "forward declaration of %q#T", ptype
);
445 inform (loc
, "declaration of %q#T", ptype
);
448 /* Print an error message for invalid use of an incomplete type.
449 VALUE is the expression that was used (or 0 if that isn't known)
450 and TYPE is the type that was invalid. DIAG_KIND indicates the
451 type of diagnostic (see diagnostic.def). */
454 cxx_incomplete_type_diagnostic (location_t loc
, const_tree value
,
455 const_tree type
, diagnostic_t diag_kind
)
457 bool is_decl
= false, complained
= false;
459 gcc_assert (diag_kind
== DK_WARNING
460 || diag_kind
== DK_PEDWARN
461 || diag_kind
== DK_ERROR
);
463 /* Avoid duplicate error message. */
464 if (TREE_CODE (type
) == ERROR_MARK
)
467 if (value
!= 0 && (VAR_P (value
)
468 || TREE_CODE (value
) == PARM_DECL
469 || TREE_CODE (value
) == FIELD_DECL
))
471 complained
= emit_diagnostic (diag_kind
, DECL_SOURCE_LOCATION (value
), 0,
472 "%qD has incomplete type", value
);
476 /* We must print an error message. Be clever about what it says. */
478 switch (TREE_CODE (type
))
484 complained
= emit_diagnostic (diag_kind
, loc
, 0,
485 "invalid use of incomplete type %q#T",
488 cxx_incomplete_type_inform (type
);
492 emit_diagnostic (diag_kind
, loc
, 0,
493 "invalid use of %qT", type
);
497 if (TYPE_DOMAIN (type
))
499 type
= TREE_TYPE (type
);
502 emit_diagnostic (diag_kind
, loc
, 0,
503 "invalid use of array with unspecified bounds");
509 tree member
= TREE_OPERAND (value
, 1);
510 if (is_overloaded_fn (member
))
511 member
= get_first_fn (member
);
512 if (DECL_FUNCTION_MEMBER_P (member
)
513 && ! flag_ms_extensions
)
514 emit_diagnostic (diag_kind
, loc
, 0,
515 "invalid use of member function %qD "
516 "(did you forget the %<()%> ?)", member
);
518 emit_diagnostic (diag_kind
, loc
, 0,
519 "invalid use of member %qD "
520 "(did you forget the %<&%> ?)", member
);
524 case TEMPLATE_TYPE_PARM
:
526 emit_diagnostic (diag_kind
, loc
, 0,
527 "invalid use of %<auto%>");
529 emit_diagnostic (diag_kind
, loc
, 0,
530 "invalid use of template type parameter %qT", type
);
533 case BOUND_TEMPLATE_TEMPLATE_PARM
:
534 emit_diagnostic (diag_kind
, loc
, 0,
535 "invalid use of template template parameter %qT",
540 emit_diagnostic (diag_kind
, loc
, 0,
541 "invalid use of dependent type %qT", type
);
545 if (type
== init_list_type_node
)
547 emit_diagnostic (diag_kind
, loc
, 0,
548 "invalid use of brace-enclosed initializer list");
551 gcc_assert (type
== unknown_type_node
);
552 if (value
&& TREE_CODE (value
) == COMPONENT_REF
)
554 else if (value
&& TREE_CODE (value
) == ADDR_EXPR
)
555 emit_diagnostic (diag_kind
, loc
, 0,
556 "address of overloaded function with no contextual "
558 else if (value
&& TREE_CODE (value
) == OVERLOAD
)
559 emit_diagnostic (diag_kind
, loc
, 0,
560 "overloaded function with no contextual type information");
562 emit_diagnostic (diag_kind
, loc
, 0,
563 "insufficient contextual information to determine type");
571 /* Print an error message for invalid use of an incomplete type.
572 VALUE is the expression that was used (or 0 if that isn't known)
573 and TYPE is the type that was invalid. */
576 cxx_incomplete_type_error (location_t loc
, const_tree value
, const_tree type
)
578 cxx_incomplete_type_diagnostic (loc
, value
, type
, DK_ERROR
);
582 /* The recursive part of split_nonconstant_init. DEST is an lvalue
583 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
584 Return true if the whole of the value was initialized by the
585 generated statements. */
588 split_nonconstant_init_1 (tree dest
, tree init
)
590 unsigned HOST_WIDE_INT idx
;
591 tree field_index
, value
;
592 tree type
= TREE_TYPE (dest
);
593 tree inner_type
= NULL
;
594 bool array_type_p
= false;
595 bool complete_p
= true;
596 HOST_WIDE_INT num_split_elts
= 0;
598 switch (TREE_CODE (type
))
601 inner_type
= TREE_TYPE (type
);
603 if ((TREE_SIDE_EFFECTS (init
)
604 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
605 || array_of_runtime_bound_p (type
))
607 /* For an array, we only need/want a single cleanup region rather
608 than one per element. */
609 tree code
= build_vec_init (dest
, NULL_TREE
, init
, false, 1,
610 tf_warning_or_error
);
618 case QUAL_UNION_TYPE
:
619 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init
), idx
,
622 /* The current implementation of this algorithm assumes that
623 the field was set for all the elements. This is usually done
624 by process_init_constructor. */
625 gcc_assert (field_index
);
628 inner_type
= TREE_TYPE (field_index
);
630 if (TREE_CODE (value
) == CONSTRUCTOR
)
635 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
636 NULL_TREE
, NULL_TREE
);
638 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
641 if (!split_nonconstant_init_1 (sub
, value
))
644 CONSTRUCTOR_ELTS (init
)->ordered_remove (idx
--);
647 else if (!initializer_constant_valid_p (value
, inner_type
))
652 /* FIXME: Ordered removal is O(1) so the whole function is
653 worst-case quadratic. This could be fixed using an aside
654 bitmap to record which elements must be removed and remove
655 them all at the same time. Or by merging
656 split_non_constant_init into process_init_constructor_array,
657 that is separating constants from non-constants while building
659 CONSTRUCTOR_ELTS (init
)->ordered_remove (idx
);
662 if (TREE_CODE (field_index
) == RANGE_EXPR
)
664 /* Use build_vec_init to initialize a range. */
665 tree low
= TREE_OPERAND (field_index
, 0);
666 tree hi
= TREE_OPERAND (field_index
, 1);
667 sub
= build4 (ARRAY_REF
, inner_type
, dest
, low
,
668 NULL_TREE
, NULL_TREE
);
669 sub
= cp_build_addr_expr (sub
, tf_warning_or_error
);
670 tree max
= size_binop (MINUS_EXPR
, hi
, low
);
671 code
= build_vec_init (sub
, max
, value
, false, 0,
672 tf_warning_or_error
);
674 if (tree_fits_shwi_p (max
))
675 num_split_elts
+= tree_to_shwi (max
);
680 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
681 NULL_TREE
, NULL_TREE
);
683 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
686 code
= build2 (INIT_EXPR
, inner_type
, sub
, value
);
687 code
= build_stmt (input_location
, EXPR_STMT
, code
);
688 code
= maybe_cleanup_point_expr_void (code
);
691 = cxx_maybe_build_cleanup (sub
, tf_warning_or_error
))
692 finish_eh_cleanup (cleanup
);
701 if (!initializer_constant_valid_p (init
, type
))
704 tree cons
= copy_node (init
);
705 CONSTRUCTOR_ELTS (init
) = NULL
;
706 code
= build2 (MODIFY_EXPR
, type
, dest
, cons
);
707 code
= build_stmt (input_location
, EXPR_STMT
, code
);
709 num_split_elts
+= CONSTRUCTOR_NELTS (init
);
717 /* The rest of the initializer is now a constant. */
718 TREE_CONSTANT (init
) = 1;
719 return complete_p
&& complete_ctor_at_level_p (TREE_TYPE (init
),
720 num_split_elts
, inner_type
);
723 /* A subroutine of store_init_value. Splits non-constant static
724 initializer INIT into a constant part and generates code to
725 perform the non-constant part of the initialization to DEST.
726 Returns the code for the runtime init. */
729 split_nonconstant_init (tree dest
, tree init
)
733 if (TREE_CODE (init
) == TARGET_EXPR
)
734 init
= TARGET_EXPR_INITIAL (init
);
735 if (TREE_CODE (init
) == CONSTRUCTOR
)
737 init
= cp_fully_fold (init
);
738 code
= push_stmt_list ();
739 if (split_nonconstant_init_1 (dest
, init
))
741 code
= pop_stmt_list (code
);
742 DECL_INITIAL (dest
) = init
;
743 TREE_READONLY (dest
) = 0;
746 code
= build2 (INIT_EXPR
, TREE_TYPE (dest
), dest
, init
);
751 /* Perform appropriate conversions on the initial value of a variable,
752 store it in the declaration DECL,
753 and print any error messages that are appropriate.
754 If the init is invalid, store an ERROR_MARK.
756 C++: Note that INIT might be a TREE_LIST, which would mean that it is
757 a base class initializer for some aggregate type, hopefully compatible
758 with DECL. If INIT is a single element, and DECL is an aggregate
759 type, we silently convert INIT into a TREE_LIST, allowing a constructor
762 If INIT is a TREE_LIST and there is no constructor, turn INIT
763 into a CONSTRUCTOR and use standard initialization techniques.
764 Perhaps a warning should be generated?
766 Returns code to be executed if initialization could not be performed
767 for static variable. In that case, caller must emit the code. */
770 store_init_value (tree decl
, tree init
, vec
<tree
, va_gc
>** cleanups
, int flags
)
774 /* If variable's type was invalidly declared, just ignore it. */
776 type
= TREE_TYPE (decl
);
777 if (TREE_CODE (type
) == ERROR_MARK
)
780 if (MAYBE_CLASS_TYPE_P (type
))
782 if (TREE_CODE (init
) == TREE_LIST
)
784 error ("constructor syntax used, but no constructor declared "
785 "for type %qT", type
);
786 init
= build_constructor_from_list (init_list_type_node
, nreverse (init
));
790 /* End of special C++ code. */
792 if (flags
& LOOKUP_ALREADY_DIGESTED
)
795 /* Digest the specified initializer into an expression. */
796 value
= digest_init_flags (type
, init
, flags
);
798 value
= extend_ref_init_temps (decl
, value
, cleanups
);
800 /* In C++11 constant expression is a semantic, not syntactic, property.
801 In C++98, make sure that what we thought was a constant expression at
802 template definition time is still constant and otherwise perform this
803 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
804 if (decl_maybe_constant_var_p (decl
) || TREE_STATIC (decl
))
807 value
= instantiate_non_dependent_expr (value
);
808 if (DECL_DECLARED_CONSTEXPR_P (decl
)
809 || DECL_IN_AGGR_P (decl
))
811 /* Diagnose a non-constant initializer for constexpr. */
812 if (processing_template_decl
813 && !require_potential_constant_expression (value
))
814 value
= error_mark_node
;
816 value
= cxx_constant_value (value
, decl
);
818 value
= maybe_constant_init (value
, decl
);
819 if (TREE_CODE (value
) == CONSTRUCTOR
&& cp_has_mutable_p (type
))
820 /* Poison this CONSTRUCTOR so it can't be copied to another
821 constexpr variable. */
822 CONSTRUCTOR_MUTABLE_POISON (value
) = true;
823 const_init
= (reduced_constant_expression_p (value
)
824 || error_operand_p (value
));
825 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = const_init
;
826 TREE_CONSTANT (decl
) = const_init
&& decl_maybe_constant_var_p (decl
);
828 value
= cp_fully_fold (value
);
830 if (cxx_dialect
>= cxx14
&& CLASS_TYPE_P (strip_array_types (type
)))
831 /* Handle aggregate NSDMI in non-constant initializers, too. */
832 value
= replace_placeholders (value
, decl
);
834 /* DECL may change value; purge caches. */
835 clear_cv_and_fold_caches ();
837 /* If the initializer is not a constant, fill in DECL_INITIAL with
838 the bits that are constant, and then return an expression that
839 will perform the dynamic initialization. */
840 if (value
!= error_mark_node
841 && (TREE_SIDE_EFFECTS (value
)
842 || array_of_runtime_bound_p (type
)
843 || ! reduced_constant_expression_p (value
)))
844 return split_nonconstant_init (decl
, value
);
845 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
846 is an automatic variable, the middle end will turn this into a
847 dynamic initialization later. */
848 DECL_INITIAL (decl
) = value
;
853 /* Give diagnostic about narrowing conversions within { }. */
856 check_narrowing (tree type
, tree init
, tsubst_flags_t complain
)
858 tree ftype
= unlowered_expr_type (init
);
862 if (((!warn_narrowing
|| !(complain
& tf_warning
))
863 && cxx_dialect
== cxx98
)
864 || !ARITHMETIC_TYPE_P (type
))
867 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
868 && TREE_CODE (type
) == COMPLEX_TYPE
)
870 tree elttype
= TREE_TYPE (type
);
871 if (CONSTRUCTOR_NELTS (init
) > 0)
872 ok
&= check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 0)->value
,
874 if (CONSTRUCTOR_NELTS (init
) > 1)
875 ok
&= check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 1)->value
,
880 init
= fold_non_dependent_expr (init
);
882 if (TREE_CODE (type
) == INTEGER_TYPE
883 && TREE_CODE (ftype
) == REAL_TYPE
)
885 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
886 && CP_INTEGRAL_TYPE_P (type
))
888 if (TREE_CODE (ftype
) == ENUMERAL_TYPE
)
889 /* Check for narrowing based on the values of the enumeration. */
890 ftype
= ENUM_UNDERLYING_TYPE (ftype
);
891 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type
),
892 TYPE_MAX_VALUE (ftype
))
893 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype
),
894 TYPE_MIN_VALUE (type
)))
895 && (TREE_CODE (init
) != INTEGER_CST
896 || !int_fits_type_p (init
, type
)))
899 else if (TREE_CODE (ftype
) == REAL_TYPE
900 && TREE_CODE (type
) == REAL_TYPE
)
902 if (TYPE_PRECISION (type
) < TYPE_PRECISION (ftype
))
904 if (TREE_CODE (init
) == REAL_CST
)
906 /* Issue 703: Loss of precision is OK as long as the value is
907 within the representable range of the new type. */
909 d
= TREE_REAL_CST (init
);
910 real_convert (&r
, TYPE_MODE (type
), &d
);
918 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
919 && TREE_CODE (type
) == REAL_TYPE
)
922 if (TREE_CODE (init
) == INTEGER_CST
)
924 d
= real_value_from_int_cst (0, init
);
925 if (exact_real_truncate (TYPE_MODE (type
), &d
))
931 if (!ok
&& !CONSTANT_CLASS_P (init
) && (complain
& tf_warning_or_error
))
933 tree folded
= cp_fully_fold (init
);
934 if (TREE_CONSTANT (folded
) && check_narrowing (type
, folded
, tf_none
))
940 location_t loc
= EXPR_LOC_OR_LOC (init
, input_location
);
941 if (cxx_dialect
== cxx98
)
943 if (complain
& tf_warning
)
944 warning_at (loc
, OPT_Wnarrowing
, "narrowing conversion of %qE "
945 "from %qT to %qT inside { } is ill-formed in C++11",
949 else if (!CONSTANT_CLASS_P (init
))
951 if (complain
& tf_warning_or_error
)
953 if ((!almost_ok
|| pedantic
)
954 && pedwarn (loc
, OPT_Wnarrowing
,
955 "narrowing conversion of %qE "
956 "from %qT to %qT inside { }",
959 inform (loc
, " the expression has a constant value but is not "
960 "a C++ constant-expression");
964 else if (complain
& tf_error
)
966 int savederrorcount
= errorcount
;
967 global_dc
->pedantic_errors
= 1;
968 pedwarn (loc
, OPT_Wnarrowing
,
969 "narrowing conversion of %qE from %qT to %qT "
970 "inside { }", init
, ftype
, type
);
971 if (errorcount
== savederrorcount
)
973 global_dc
->pedantic_errors
= flag_pedantic_errors
;
980 /* Process the initializer INIT for a variable of type TYPE, emitting
981 diagnostics for invalid initializers and converting the initializer as
984 For aggregate types, it assumes that reshape_init has already run, thus the
985 initializer will have the right shape (brace elision has been undone).
987 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
990 digest_init_r (tree type
, tree init
, bool nested
, int flags
,
991 tsubst_flags_t complain
)
993 enum tree_code code
= TREE_CODE (type
);
995 if (error_operand_p (init
))
996 return error_mark_node
;
1000 /* We must strip the outermost array type when completing the type,
1001 because the its bounds might be incomplete at the moment. */
1002 if (!complete_type_or_maybe_complain (TREE_CODE (type
) == ARRAY_TYPE
1003 ? TREE_TYPE (type
) : type
, NULL_TREE
,
1005 return error_mark_node
;
1007 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
1008 (g++.old-deja/g++.law/casts2.C). */
1009 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
1010 init
= TREE_OPERAND (init
, 0);
1012 location_t loc
= EXPR_LOC_OR_LOC (init
, input_location
);
1014 /* Initialization of an array of chars from a string constant. The initializer
1015 can be optionally enclosed in braces, but reshape_init has already removed
1016 them if they were present. */
1017 if (code
== ARRAY_TYPE
)
1019 if (nested
&& !TYPE_DOMAIN (type
))
1021 /* C++ flexible array members have a null domain. */
1022 pedwarn (loc
, OPT_Wpedantic
,
1023 "initialization of a flexible array member");
1026 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
1027 if (char_type_p (typ1
)
1029 && TREE_CODE (init
) == STRING_CST
)
1031 tree char_type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init
)));
1033 if (TYPE_PRECISION (typ1
) == BITS_PER_UNIT
)
1035 if (char_type
!= char_type_node
)
1037 if (complain
& tf_error
)
1038 error_at (loc
, "char-array initialized from wide string");
1039 return error_mark_node
;
1044 if (char_type
== char_type_node
)
1046 if (complain
& tf_error
)
1048 "int-array initialized from non-wide string");
1049 return error_mark_node
;
1051 else if (char_type
!= typ1
)
1053 if (complain
& tf_error
)
1054 error_at (loc
, "int-array initialized from incompatible "
1056 return error_mark_node
;
1060 if (type
!= TREE_TYPE (init
))
1062 init
= copy_node (init
);
1063 TREE_TYPE (init
) = type
;
1065 if (TYPE_DOMAIN (type
) && TREE_CONSTANT (TYPE_SIZE (type
)))
1067 /* Not a flexible array member. */
1068 int size
= TREE_INT_CST_LOW (TYPE_SIZE (type
));
1069 size
= (size
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
;
1070 /* In C it is ok to subtract 1 from the length of the string
1071 because it's ok to ignore the terminating null char that is
1072 counted in the length of the constant, but in C++ this would
1074 if (size
< TREE_STRING_LENGTH (init
))
1075 permerror (loc
, "initializer-string for array "
1076 "of chars is too long");
1082 /* Handle scalar types (including conversions) and references. */
1083 if ((TREE_CODE (type
) != COMPLEX_TYPE
1084 || BRACE_ENCLOSED_INITIALIZER_P (init
))
1085 && (SCALAR_TYPE_P (type
) || code
== REFERENCE_TYPE
))
1088 flags
|= LOOKUP_NO_NARROWING
;
1089 init
= convert_for_initialization (0, type
, init
, flags
,
1090 ICR_INIT
, NULL_TREE
, 0,
1096 /* Come here only for aggregates: records, arrays, unions, complex numbers
1098 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
1099 || VECTOR_TYPE_P (type
)
1100 || TREE_CODE (type
) == RECORD_TYPE
1101 || TREE_CODE (type
) == UNION_TYPE
1102 || TREE_CODE (type
) == COMPLEX_TYPE
);
1104 /* "If T is a class type and the initializer list has a single
1105 element of type cv U, where U is T or a class derived from T,
1106 the object is initialized from that element." */
1108 && cxx_dialect
>= cxx11
1109 && BRACE_ENCLOSED_INITIALIZER_P (init
)
1110 && CONSTRUCTOR_NELTS (init
) == 1
1111 && ((CLASS_TYPE_P (type
) && !CLASSTYPE_NON_AGGREGATE (type
))
1112 || VECTOR_TYPE_P (type
)))
1114 tree elt
= CONSTRUCTOR_ELT (init
, 0)->value
;
1115 if (reference_related_p (type
, TREE_TYPE (elt
)))
1116 /* We should have fixed this in reshape_init. */
1120 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
1121 && !TYPE_NON_AGGREGATE_CLASS (type
))
1122 return process_init_constructor (type
, init
, complain
);
1125 if (COMPOUND_LITERAL_P (init
) && TREE_CODE (type
) == ARRAY_TYPE
)
1127 if (complain
& tf_error
)
1128 error_at (loc
, "cannot initialize aggregate of type %qT with "
1129 "a compound literal", type
);
1131 return error_mark_node
;
1134 if (TREE_CODE (type
) == ARRAY_TYPE
1135 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
1137 /* Allow the result of build_array_copy and of
1138 build_value_init_noctor. */
1139 if ((TREE_CODE (init
) == VEC_INIT_EXPR
1140 || TREE_CODE (init
) == CONSTRUCTOR
)
1141 && (same_type_ignoring_top_level_qualifiers_p
1142 (type
, TREE_TYPE (init
))))
1145 if (complain
& tf_error
)
1146 error_at (loc
, "array must be initialized with a brace-enclosed"
1148 return error_mark_node
;
1151 return convert_for_initialization (NULL_TREE
, type
, init
,
1153 ICR_INIT
, NULL_TREE
, 0,
1159 digest_init (tree type
, tree init
, tsubst_flags_t complain
)
1161 return digest_init_r (type
, init
, false, LOOKUP_IMPLICIT
, complain
);
1165 digest_init_flags (tree type
, tree init
, int flags
)
1167 return digest_init_r (type
, init
, false, flags
, tf_warning_or_error
);
1170 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1172 digest_nsdmi_init (tree decl
, tree init
)
1174 gcc_assert (TREE_CODE (decl
) == FIELD_DECL
);
1176 tree type
= TREE_TYPE (decl
);
1177 int flags
= LOOKUP_IMPLICIT
;
1178 if (DIRECT_LIST_INIT_P (init
))
1179 flags
= LOOKUP_NORMAL
;
1180 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
1181 && CP_AGGREGATE_TYPE_P (type
))
1182 init
= reshape_init (type
, init
, tf_warning_or_error
);
1183 init
= digest_init_flags (type
, init
, flags
);
1184 if (TREE_CODE (init
) == TARGET_EXPR
)
1185 /* This represents the whole initialization. */
1186 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
1190 /* Set of flags used within process_init_constructor to describe the
1192 #define PICFLAG_ERRONEOUS 1
1193 #define PICFLAG_NOT_ALL_CONSTANT 2
1194 #define PICFLAG_NOT_ALL_SIMPLE 4
1195 #define PICFLAG_SIDE_EFFECTS 8
1197 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1201 picflag_from_initializer (tree init
)
1203 if (init
== error_mark_node
)
1204 return PICFLAG_ERRONEOUS
;
1205 else if (!TREE_CONSTANT (init
))
1207 if (TREE_SIDE_EFFECTS (init
))
1208 return PICFLAG_SIDE_EFFECTS
;
1210 return PICFLAG_NOT_ALL_CONSTANT
;
1212 else if (!initializer_constant_valid_p (init
, TREE_TYPE (init
)))
1213 return PICFLAG_NOT_ALL_SIMPLE
;
1217 /* Adjust INIT for going into a CONSTRUCTOR. */
1220 massage_init_elt (tree type
, tree init
, tsubst_flags_t complain
)
1222 init
= digest_init_r (type
, init
, true, LOOKUP_IMPLICIT
, complain
);
1223 /* Strip a simple TARGET_EXPR when we know this is an initializer. */
1224 if (SIMPLE_TARGET_EXPR_P (init
))
1225 init
= TARGET_EXPR_INITIAL (init
);
1226 /* When we defer constant folding within a statement, we may want to
1227 defer this folding as well. */
1228 tree t
= fold_non_dependent_expr (init
);
1229 t
= maybe_constant_init (t
);
1230 if (TREE_CONSTANT (t
))
1235 /* Subroutine of process_init_constructor, which will process an initializer
1236 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1237 which describe the initializers. */
1240 process_init_constructor_array (tree type
, tree init
,
1241 tsubst_flags_t complain
)
1243 unsigned HOST_WIDE_INT i
, len
= 0;
1245 bool unbounded
= false;
1246 constructor_elt
*ce
;
1247 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (init
);
1249 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
1250 || VECTOR_TYPE_P (type
));
1252 if (TREE_CODE (type
) == ARRAY_TYPE
)
1254 /* C++ flexible array members have a null domain. */
1255 tree domain
= TYPE_DOMAIN (type
);
1256 if (domain
&& TREE_CONSTANT (TYPE_MAX_VALUE (domain
)))
1257 len
= wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain
))
1258 - wi::to_offset (TYPE_MIN_VALUE (domain
)) + 1,
1259 TYPE_PRECISION (TREE_TYPE (domain
)),
1260 TYPE_SIGN (TREE_TYPE (domain
))).to_uhwi ();
1262 unbounded
= true; /* Take as many as there are. */
1265 /* Vectors are like simple fixed-size arrays. */
1266 len
= TYPE_VECTOR_SUBPARTS (type
);
1268 /* There must not be more initializers than needed. */
1269 if (!unbounded
&& vec_safe_length (v
) > len
)
1271 if (complain
& tf_error
)
1272 error ("too many initializers for %qT", type
);
1274 return PICFLAG_ERRONEOUS
;
1277 FOR_EACH_VEC_SAFE_ELT (v
, i
, ce
)
1281 gcc_assert (TREE_CODE (ce
->index
) == INTEGER_CST
);
1282 if (compare_tree_int (ce
->index
, i
) != 0)
1284 ce
->value
= error_mark_node
;
1285 sorry ("non-trivial designated initializers not supported");
1289 ce
->index
= size_int (i
);
1290 gcc_assert (ce
->value
);
1291 ce
->value
= massage_init_elt (TREE_TYPE (type
), ce
->value
, complain
);
1293 if (ce
->value
!= error_mark_node
)
1294 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1295 (TREE_TYPE (type
), TREE_TYPE (ce
->value
)));
1297 flags
|= picflag_from_initializer (ce
->value
);
1300 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1301 we must add initializers ourselves. */
1303 for (; i
< len
; ++i
)
1307 if (type_build_ctor_call (TREE_TYPE (type
)))
1309 /* If this type needs constructors run for default-initialization,
1310 we can't rely on the back end to do it for us, so make the
1311 initialization explicit by list-initializing from T{}. */
1312 next
= build_constructor (init_list_type_node
, NULL
);
1313 next
= massage_init_elt (TREE_TYPE (type
), next
, complain
);
1314 if (initializer_zerop (next
))
1315 /* The default zero-initialization is fine for us; don't
1316 add anything to the CONSTRUCTOR. */
1319 else if (!zero_init_p (TREE_TYPE (type
)))
1320 next
= build_zero_init (TREE_TYPE (type
),
1321 /*nelts=*/NULL_TREE
,
1322 /*static_storage_p=*/false);
1324 /* The default zero-initialization is fine for us; don't
1325 add anything to the CONSTRUCTOR. */
1330 flags
|= picflag_from_initializer (next
);
1331 CONSTRUCTOR_APPEND_ELT (v
, size_int (i
), next
);
1335 CONSTRUCTOR_ELTS (init
) = v
;
1339 /* Subroutine of process_init_constructor, which will process an initializer
1340 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1341 the initializers. */
1344 process_init_constructor_record (tree type
, tree init
,
1345 tsubst_flags_t complain
)
1347 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1351 gcc_assert (TREE_CODE (type
) == RECORD_TYPE
);
1352 gcc_assert (!CLASSTYPE_VBASECLASSES (type
));
1353 gcc_assert (!TYPE_BINFO (type
)
1354 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)));
1355 gcc_assert (!TYPE_POLYMORPHIC_P (type
));
1359 unsigned HOST_WIDE_INT idx
= 0;
1360 /* Generally, we will always have an index for each initializer (which is
1361 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1362 reshape_init. So we need to handle both cases. */
1363 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1368 if (!DECL_NAME (field
) && DECL_C_BIT_FIELD (field
))
1371 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
1374 /* If this is a bitfield, first convert to the declared type. */
1375 type
= TREE_TYPE (field
);
1376 if (DECL_BIT_FIELD_TYPE (field
))
1377 type
= DECL_BIT_FIELD_TYPE (field
);
1378 if (type
== error_mark_node
)
1379 return PICFLAG_ERRONEOUS
;
1381 if (idx
< vec_safe_length (CONSTRUCTOR_ELTS (init
)))
1383 constructor_elt
*ce
= &(*CONSTRUCTOR_ELTS (init
))[idx
];
1386 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1387 latter case can happen in templates where lookup has to be
1389 gcc_assert (TREE_CODE (ce
->index
) == FIELD_DECL
1390 || identifier_p (ce
->index
));
1391 if (ce
->index
!= field
1392 && ce
->index
!= DECL_NAME (field
))
1394 ce
->value
= error_mark_node
;
1395 sorry ("non-trivial designated initializers not supported");
1399 gcc_assert (ce
->value
);
1400 next
= massage_init_elt (type
, ce
->value
, complain
);
1403 else if (DECL_INITIAL (field
))
1407 /* We're using an NSDMI past a field with implicit
1408 zero-init. Go back and make it explicit. */
1410 vec_safe_truncate (v
, 0);
1413 /* C++14 aggregate NSDMI. */
1414 next
= get_nsdmi (field
, /*ctor*/false);
1416 else if (type_build_ctor_call (TREE_TYPE (field
)))
1418 /* If this type needs constructors run for
1419 default-initialization, we can't rely on the back end to do it
1420 for us, so build up TARGET_EXPRs. If the type in question is
1421 a class, just build one up; if it's an array, recurse. */
1422 next
= build_constructor (init_list_type_node
, NULL
);
1423 next
= massage_init_elt (TREE_TYPE (field
), next
, complain
);
1425 /* Warn when some struct elements are implicitly initialized. */
1426 if ((complain
& tf_warning
)
1427 && !EMPTY_CONSTRUCTOR_P (init
))
1428 warning (OPT_Wmissing_field_initializers
,
1429 "missing initializer for member %qD", field
);
1433 const_tree fldtype
= TREE_TYPE (field
);
1434 if (TREE_CODE (fldtype
) == REFERENCE_TYPE
)
1436 if (complain
& tf_error
)
1437 error ("member %qD is uninitialized reference", field
);
1439 return PICFLAG_ERRONEOUS
;
1441 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (fldtype
))
1443 if (complain
& tf_error
)
1444 error ("member %qD with uninitialized reference fields", field
);
1446 return PICFLAG_ERRONEOUS
;
1449 /* Warn when some struct elements are implicitly initialized
1450 to zero. However, avoid issuing the warning for flexible
1451 array members since they need not have any elements. */
1452 if ((TREE_CODE (fldtype
) != ARRAY_TYPE
|| TYPE_DOMAIN (fldtype
))
1453 && (complain
& tf_warning
)
1454 && !EMPTY_CONSTRUCTOR_P (init
))
1455 warning (OPT_Wmissing_field_initializers
,
1456 "missing initializer for member %qD", field
);
1458 if (!zero_init_p (fldtype
)
1460 next
= build_zero_init (TREE_TYPE (field
), /*nelts=*/NULL_TREE
,
1461 /*static_storage_p=*/false);
1464 /* The default zero-initialization is fine for us; don't
1465 add anything to the CONSTRUCTOR. */
1471 /* If this is a bitfield, now convert to the lowered type. */
1472 if (type
!= TREE_TYPE (field
))
1473 next
= cp_convert_and_check (TREE_TYPE (field
), next
, complain
);
1474 flags
|= picflag_from_initializer (next
);
1475 CONSTRUCTOR_APPEND_ELT (v
, field
, next
);
1478 if (idx
< vec_safe_length (CONSTRUCTOR_ELTS (init
)))
1480 if (complain
& tf_error
)
1481 error ("too many initializers for %qT", type
);
1483 return PICFLAG_ERRONEOUS
;
1486 CONSTRUCTOR_ELTS (init
) = v
;
1490 /* Subroutine of process_init_constructor, which will process a single
1491 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1492 which describe the initializer. */
1495 process_init_constructor_union (tree type
, tree init
,
1496 tsubst_flags_t complain
)
1498 constructor_elt
*ce
;
1501 /* If the initializer was empty, use the union's NSDMI if it has one.
1502 Otherwise use default zero initialization. */
1503 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
1505 for (tree field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1507 if (DECL_INITIAL (field
))
1509 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init
),
1511 get_nsdmi (field
, /*in_ctor=*/false));
1516 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
1520 len
= CONSTRUCTOR_ELTS (init
)->length ();
1523 if (!(complain
& tf_error
))
1524 return PICFLAG_ERRONEOUS
;
1525 error ("too many initializers for %qT", type
);
1526 CONSTRUCTOR_ELTS (init
)->block_remove (1, len
-1);
1529 ce
= &(*CONSTRUCTOR_ELTS (init
))[0];
1531 /* If this element specifies a field, initialize via that field. */
1534 if (TREE_CODE (ce
->index
) == FIELD_DECL
)
1536 else if (identifier_p (ce
->index
))
1538 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1539 tree name
= ce
->index
;
1541 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1542 if (DECL_NAME (field
) == name
)
1546 if (complain
& tf_error
)
1547 error ("no field %qD found in union being initialized",
1549 ce
->value
= error_mark_node
;
1555 gcc_assert (TREE_CODE (ce
->index
) == INTEGER_CST
1556 || TREE_CODE (ce
->index
) == RANGE_EXPR
);
1557 if (complain
& tf_error
)
1558 error ("index value instead of field name in union initializer");
1559 ce
->value
= error_mark_node
;
1564 /* Find the first named field. ANSI decided in September 1990
1565 that only named fields count here. */
1566 tree field
= TYPE_FIELDS (type
);
1567 while (field
&& (!DECL_NAME (field
) || TREE_CODE (field
) != FIELD_DECL
))
1568 field
= TREE_CHAIN (field
);
1569 if (field
== NULL_TREE
)
1571 if (complain
& tf_error
)
1572 error ("too many initializers for %qT", type
);
1573 ce
->value
= error_mark_node
;
1578 if (ce
->value
&& ce
->value
!= error_mark_node
)
1579 ce
->value
= massage_init_elt (TREE_TYPE (ce
->index
), ce
->value
, complain
);
1581 return picflag_from_initializer (ce
->value
);
1584 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1585 constructor is a brace-enclosed initializer, and will be modified in-place.
1587 Each element is converted to the right type through digest_init, and
1588 missing initializers are added following the language rules (zero-padding,
1591 After the execution, the initializer will have TREE_CONSTANT if all elts are
1592 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1593 constants that the assembler and linker can compute them.
1595 The function returns the initializer itself, or error_mark_node in case
1599 process_init_constructor (tree type
, tree init
, tsubst_flags_t complain
)
1603 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init
));
1605 if (TREE_CODE (type
) == ARRAY_TYPE
|| VECTOR_TYPE_P (type
))
1606 flags
= process_init_constructor_array (type
, init
, complain
);
1607 else if (TREE_CODE (type
) == RECORD_TYPE
)
1608 flags
= process_init_constructor_record (type
, init
, complain
);
1609 else if (TREE_CODE (type
) == UNION_TYPE
)
1610 flags
= process_init_constructor_union (type
, init
, complain
);
1614 if (flags
& PICFLAG_ERRONEOUS
)
1615 return error_mark_node
;
1617 TREE_TYPE (init
) = type
;
1618 if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
) == NULL_TREE
)
1619 cp_complete_array_type (&TREE_TYPE (init
), init
, /*do_default=*/0);
1620 if (flags
& PICFLAG_SIDE_EFFECTS
)
1622 TREE_CONSTANT (init
) = false;
1623 TREE_SIDE_EFFECTS (init
) = true;
1625 else if (flags
& PICFLAG_NOT_ALL_CONSTANT
)
1626 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1627 TREE_CONSTANT (init
) = false;
1630 TREE_CONSTANT (init
) = 1;
1631 if (!(flags
& PICFLAG_NOT_ALL_SIMPLE
))
1632 TREE_STATIC (init
) = 1;
1637 /* Given a structure or union value DATUM, construct and return
1638 the structure or union component which results from narrowing
1639 that value to the base specified in BASETYPE. For example, given the
1642 class L { int ii; };
1643 class A : L { ... };
1644 class B : L { ... };
1645 class C : A, B { ... };
1653 x.A::ii refers to the ii member of the L part of
1654 the A part of the C object named by X. In this case,
1655 DATUM would be x, and BASETYPE would be A.
1657 I used to think that this was nonconformant, that the standard specified
1658 that first we look up ii in A, then convert x to an L& and pull out the
1659 ii part. But in fact, it does say that we convert x to an A&; A here
1660 is known as the "naming class". (jason 2000-12-19)
1662 BINFO_P points to a variable initialized either to NULL_TREE or to the
1663 binfo for the specific base subobject we want to convert to. */
1666 build_scoped_ref (tree datum
, tree basetype
, tree
* binfo_p
)
1670 if (datum
== error_mark_node
)
1671 return error_mark_node
;
1675 binfo
= lookup_base (TREE_TYPE (datum
), basetype
, ba_check
,
1676 NULL
, tf_warning_or_error
);
1678 if (!binfo
|| binfo
== error_mark_node
)
1680 *binfo_p
= NULL_TREE
;
1682 error_not_base_type (basetype
, TREE_TYPE (datum
));
1683 return error_mark_node
;
1687 return build_base_path (PLUS_EXPR
, datum
, binfo
, 1,
1688 tf_warning_or_error
);
1691 /* Build a reference to an object specified by the C++ `->' operator.
1692 Usually this just involves dereferencing the object, but if the
1693 `->' operator is overloaded, then such overloads must be
1694 performed until an object which does not have the `->' operator
1695 overloaded is found. An error is reported when circular pointer
1696 delegation is detected. */
1699 build_x_arrow (location_t loc
, tree expr
, tsubst_flags_t complain
)
1701 tree orig_expr
= expr
;
1702 tree type
= TREE_TYPE (expr
);
1703 tree last_rval
= NULL_TREE
;
1704 vec
<tree
, va_gc
> *types_memoized
= NULL
;
1706 if (type
== error_mark_node
)
1707 return error_mark_node
;
1709 if (processing_template_decl
)
1711 if (type
&& TREE_CODE (type
) == POINTER_TYPE
1712 && !dependent_scope_p (TREE_TYPE (type
)))
1713 /* Pointer to current instantiation, don't treat as dependent. */;
1714 else if (type_dependent_expression_p (expr
))
1715 return build_min_nt_loc (loc
, ARROW_EXPR
, expr
);
1716 expr
= build_non_dependent_expr (expr
);
1719 if (MAYBE_CLASS_TYPE_P (type
))
1721 struct tinst_level
*actual_inst
= current_instantiation ();
1724 while ((expr
= build_new_op (loc
, COMPONENT_REF
,
1725 LOOKUP_NORMAL
, expr
, NULL_TREE
, NULL_TREE
,
1728 if (expr
== error_mark_node
)
1729 return error_mark_node
;
1731 /* This provides a better instantiation backtrace in case of
1733 if (fn
&& DECL_USE_TEMPLATE (fn
))
1734 push_tinst_level_loc (fn
,
1735 (current_instantiation () != actual_inst
)
1736 ? DECL_SOURCE_LOCATION (fn
)
1740 if (vec_member (TREE_TYPE (expr
), types_memoized
))
1742 if (complain
& tf_error
)
1743 error ("circular pointer delegation detected");
1744 return error_mark_node
;
1747 vec_safe_push (types_memoized
, TREE_TYPE (expr
));
1751 while (current_instantiation () != actual_inst
)
1754 if (last_rval
== NULL_TREE
)
1756 if (complain
& tf_error
)
1757 error ("base operand of %<->%> has non-pointer type %qT", type
);
1758 return error_mark_node
;
1761 if (TREE_CODE (TREE_TYPE (last_rval
)) == REFERENCE_TYPE
)
1762 last_rval
= convert_from_reference (last_rval
);
1765 last_rval
= decay_conversion (expr
, complain
);
1767 if (TYPE_PTR_P (TREE_TYPE (last_rval
)))
1769 if (processing_template_decl
)
1771 expr
= build_min (ARROW_EXPR
, TREE_TYPE (TREE_TYPE (last_rval
)),
1773 TREE_SIDE_EFFECTS (expr
) = TREE_SIDE_EFFECTS (last_rval
);
1777 return cp_build_indirect_ref (last_rval
, RO_NULL
, complain
);
1780 if (complain
& tf_error
)
1783 error ("result of %<operator->()%> yields non-pointer result");
1785 error ("base operand of %<->%> is not a pointer");
1787 return error_mark_node
;
1790 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1791 already been checked out to be of aggregate type. */
1794 build_m_component_ref (tree datum
, tree component
, tsubst_flags_t complain
)
1802 if (error_operand_p (datum
) || error_operand_p (component
))
1803 return error_mark_node
;
1805 datum
= mark_lvalue_use (datum
);
1806 component
= mark_rvalue_use (component
);
1808 ptrmem_type
= TREE_TYPE (component
);
1809 if (!TYPE_PTRMEM_P (ptrmem_type
))
1811 if (complain
& tf_error
)
1812 error ("%qE cannot be used as a member pointer, since it is of "
1813 "type %qT", component
, ptrmem_type
);
1814 return error_mark_node
;
1817 objtype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
1818 if (! MAYBE_CLASS_TYPE_P (objtype
))
1820 if (complain
& tf_error
)
1821 error ("cannot apply member pointer %qE to %qE, which is of "
1822 "non-class type %qT", component
, datum
, objtype
);
1823 return error_mark_node
;
1826 type
= TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type
);
1827 ctype
= complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type
));
1829 if (!COMPLETE_TYPE_P (ctype
))
1831 if (!same_type_p (ctype
, objtype
))
1837 binfo
= lookup_base (objtype
, ctype
, ba_check
, NULL
, complain
);
1842 if (complain
& tf_error
)
1843 error ("pointer to member type %qT incompatible with object "
1844 "type %qT", type
, objtype
);
1845 return error_mark_node
;
1847 else if (binfo
== error_mark_node
)
1848 return error_mark_node
;
1851 if (TYPE_PTRDATAMEM_P (ptrmem_type
))
1853 cp_lvalue_kind kind
= lvalue_kind (datum
);
1856 /* Compute the type of the field, as described in [expr.ref].
1857 There's no such thing as a mutable pointer-to-member, so
1858 things are not as complex as they are for references to
1859 non-static data members. */
1860 type
= cp_build_qualified_type (type
,
1861 (cp_type_quals (type
)
1862 | cp_type_quals (TREE_TYPE (datum
))));
1864 datum
= build_address (datum
);
1866 /* Convert object to the correct base. */
1869 datum
= build_base_path (PLUS_EXPR
, datum
, binfo
, 1, complain
);
1870 if (datum
== error_mark_node
)
1871 return error_mark_node
;
1874 /* Build an expression for "object + offset" where offset is the
1875 value stored in the pointer-to-data-member. */
1876 ptype
= build_pointer_type (type
);
1877 datum
= fold_build_pointer_plus (fold_convert (ptype
, datum
), component
);
1878 datum
= cp_build_indirect_ref (datum
, RO_NULL
, complain
);
1879 if (datum
== error_mark_node
)
1880 return error_mark_node
;
1882 /* If the object expression was an rvalue, return an rvalue. */
1883 if (kind
& clk_class
)
1884 datum
= rvalue (datum
);
1885 else if (kind
& clk_rvalueref
)
1886 datum
= move (datum
);
1891 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
1892 program is ill-formed if the second operand is a pointer to member
1893 function with ref-qualifier &. In a .* expression whose object
1894 expression is an lvalue, the program is ill-formed if the second
1895 operand is a pointer to member function with ref-qualifier &&. */
1896 if (FUNCTION_REF_QUALIFIED (type
))
1898 bool lval
= real_lvalue_p (datum
);
1899 if (lval
&& FUNCTION_RVALUE_QUALIFIED (type
))
1901 if (complain
& tf_error
)
1902 error ("pointer-to-member-function type %qT requires an rvalue",
1904 return error_mark_node
;
1906 else if (!lval
&& !FUNCTION_RVALUE_QUALIFIED (type
))
1908 if (complain
& tf_error
)
1909 error ("pointer-to-member-function type %qT requires an lvalue",
1911 return error_mark_node
;
1914 return build2 (OFFSET_REF
, type
, datum
, component
);
1918 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1921 build_functional_cast (tree exp
, tree parms
, tsubst_flags_t complain
)
1923 /* This is either a call to a constructor,
1924 or a C cast in C++'s `functional' notation. */
1926 /* The type to which we are casting. */
1928 vec
<tree
, va_gc
> *parmvec
;
1930 if (error_operand_p (exp
) || parms
== error_mark_node
)
1931 return error_mark_node
;
1933 if (TREE_CODE (exp
) == TYPE_DECL
)
1935 type
= TREE_TYPE (exp
);
1937 if (complain
& tf_warning
1938 && TREE_DEPRECATED (type
)
1939 && DECL_ARTIFICIAL (exp
))
1940 warn_deprecated_use (type
, NULL_TREE
);
1945 /* We need to check this explicitly, since value-initialization of
1946 arrays is allowed in other situations. */
1947 if (TREE_CODE (type
) == ARRAY_TYPE
)
1949 if (complain
& tf_error
)
1950 error ("functional cast to array type %qT", type
);
1951 return error_mark_node
;
1954 if (type_uses_auto (type
))
1956 if (complain
& tf_error
)
1957 error ("invalid use of %<auto%>");
1958 return error_mark_node
;
1961 if (processing_template_decl
)
1965 /* Diagnose this even in a template. We could also try harder
1966 to give all the usual errors when the type and args are
1968 if (TREE_CODE (type
) == REFERENCE_TYPE
&& !parms
)
1970 if (complain
& tf_error
)
1971 error ("invalid value-initialization of reference type");
1972 return error_mark_node
;
1975 t
= build_min (CAST_EXPR
, type
, parms
);
1976 /* We don't know if it will or will not have side effects. */
1977 TREE_SIDE_EFFECTS (t
) = 1;
1981 if (! MAYBE_CLASS_TYPE_P (type
))
1983 if (parms
== NULL_TREE
)
1985 if (VOID_TYPE_P (type
))
1987 return build_value_init (cv_unqualified (type
), complain
);
1990 /* This must build a C cast. */
1991 parms
= build_x_compound_expr_from_list (parms
, ELK_FUNC_CAST
, complain
);
1992 return cp_build_c_cast (type
, parms
, complain
);
1995 /* Prepare to evaluate as a call to a constructor. If this expression
1996 is actually used, for example,
1998 return X (arg1, arg2, ...);
2000 then the slot being initialized will be filled in. */
2002 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
2003 return error_mark_node
;
2004 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
2005 return error_mark_node
;
2009 If the expression list is a single-expression, the type
2010 conversion is equivalent (in definedness, and if defined in
2011 meaning) to the corresponding cast expression. */
2012 if (parms
&& TREE_CHAIN (parms
) == NULL_TREE
)
2013 return cp_build_c_cast (type
, TREE_VALUE (parms
), complain
);
2017 The expression T(), where T is a simple-type-specifier for a
2018 non-array complete object type or the (possibly cv-qualified)
2019 void type, creates an rvalue of the specified type, which is
2020 value-initialized. */
2022 if (parms
== NULL_TREE
)
2024 exp
= build_value_init (type
, complain
);
2025 exp
= get_target_expr_sfinae (exp
, complain
);
2029 /* Call the constructor. */
2030 parmvec
= make_tree_vector ();
2031 for (; parms
!= NULL_TREE
; parms
= TREE_CHAIN (parms
))
2032 vec_safe_push (parmvec
, TREE_VALUE (parms
));
2033 exp
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
2034 &parmvec
, type
, LOOKUP_NORMAL
, complain
);
2035 release_tree_vector (parmvec
);
2037 if (exp
== error_mark_node
)
2038 return error_mark_node
;
2040 return build_cplus_new (type
, exp
, complain
);
2044 /* Add new exception specifier SPEC, to the LIST we currently have.
2045 If it's already in LIST then do nothing.
2046 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2047 know what we're doing. */
2050 add_exception_specifier (tree list
, tree spec
, int complain
)
2055 diagnostic_t diag_type
= DK_UNSPECIFIED
; /* none */
2057 if (spec
== error_mark_node
)
2060 gcc_assert (spec
&& (!list
|| TREE_VALUE (list
)));
2062 /* [except.spec] 1, type in an exception specifier shall not be
2063 incomplete, or pointer or ref to incomplete other than pointer
2065 is_ptr
= TYPE_PTR_P (core
);
2066 if (is_ptr
|| TREE_CODE (core
) == REFERENCE_TYPE
)
2067 core
= TREE_TYPE (core
);
2070 else if (VOID_TYPE_P (core
))
2072 else if (TREE_CODE (core
) == TEMPLATE_TYPE_PARM
)
2074 else if (processing_template_decl
)
2079 /* 15.4/1 says that types in an exception specifier must be complete,
2080 but it seems more reasonable to only require this on definitions
2081 and calls. So just give a pedwarn at this point; we will give an
2082 error later if we hit one of those two cases. */
2083 if (!COMPLETE_TYPE_P (complete_type (core
)))
2084 diag_type
= DK_PEDWARN
; /* pedwarn */
2091 for (probe
= list
; probe
; probe
= TREE_CHAIN (probe
))
2092 if (same_type_p (TREE_VALUE (probe
), spec
))
2095 list
= tree_cons (NULL_TREE
, spec
, list
);
2098 diag_type
= DK_ERROR
; /* error */
2100 if (diag_type
!= DK_UNSPECIFIED
2101 && (complain
& tf_warning_or_error
))
2102 cxx_incomplete_type_diagnostic (NULL_TREE
, core
, diag_type
);
2107 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2110 nothrow_spec_p_uninst (const_tree spec
)
2112 if (DEFERRED_NOEXCEPT_SPEC_P (spec
))
2114 return nothrow_spec_p (spec
);
2117 /* Combine the two exceptions specifier lists LIST and ADD, and return
2121 merge_exception_specifiers (tree list
, tree add
)
2123 tree noex
, orig_list
;
2125 /* No exception-specifier or noexcept(false) are less strict than
2126 anything else. Prefer the newer variant (LIST). */
2127 if (!list
|| list
== noexcept_false_spec
)
2129 else if (!add
|| add
== noexcept_false_spec
)
2132 /* noexcept(true) and throw() are stricter than anything else.
2133 As above, prefer the more recent one (LIST). */
2134 if (nothrow_spec_p_uninst (add
))
2137 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2138 if (UNEVALUATED_NOEXCEPT_SPEC_P (add
)
2139 && UNEVALUATED_NOEXCEPT_SPEC_P (list
))
2141 /* We should have instantiated other deferred noexcept specs by now. */
2142 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add
));
2144 if (nothrow_spec_p_uninst (list
))
2146 noex
= TREE_PURPOSE (list
);
2147 gcc_checking_assert (!TREE_PURPOSE (add
)
2148 || errorcount
|| !flag_exceptions
2149 || cp_tree_equal (noex
, TREE_PURPOSE (add
)));
2151 /* Combine the dynamic-exception-specifiers, if any. */
2153 for (; add
&& TREE_VALUE (add
); add
= TREE_CHAIN (add
))
2155 tree spec
= TREE_VALUE (add
);
2158 for (probe
= orig_list
; probe
&& TREE_VALUE (probe
);
2159 probe
= TREE_CHAIN (probe
))
2160 if (same_type_p (TREE_VALUE (probe
), spec
))
2164 spec
= build_tree_list (NULL_TREE
, spec
);
2165 TREE_CHAIN (spec
) = list
;
2170 /* Keep the noexcept-specifier at the beginning of the list. */
2171 if (noex
!= TREE_PURPOSE (list
))
2172 list
= tree_cons (noex
, TREE_VALUE (list
), TREE_CHAIN (list
));
2177 /* Subroutine of build_call. Ensure that each of the types in the
2178 exception specification is complete. Technically, 15.4/1 says that
2179 they need to be complete when we see a declaration of the function,
2180 but we should be able to get away with only requiring this when the
2181 function is defined or called. See also add_exception_specifier. */
2184 require_complete_eh_spec_types (tree fntype
, tree decl
)
2187 /* Don't complain about calls to op new. */
2188 if (decl
&& DECL_ARTIFICIAL (decl
))
2190 for (raises
= TYPE_RAISES_EXCEPTIONS (fntype
); raises
;
2191 raises
= TREE_CHAIN (raises
))
2193 tree type
= TREE_VALUE (raises
);
2194 if (type
&& !COMPLETE_TYPE_P (type
))
2198 ("call to function %qD which throws incomplete type %q#T",
2201 error ("call to function which throws incomplete type %q#T",
2208 #include "gt-cp-typeck2.h"