1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987-2015 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"
35 #include "double-int.h"
42 #include "stor-layout.h"
47 #include "diagnostic-core.h"
51 process_init_constructor (tree type
, tree init
, tsubst_flags_t complain
);
54 /* Print an error message stemming from an attempt to use
55 BASETYPE as a base class for TYPE. */
58 error_not_base_type (tree basetype
, tree type
)
60 if (TREE_CODE (basetype
) == FUNCTION_DECL
)
61 basetype
= DECL_CONTEXT (basetype
);
62 error ("type %qT is not a base type for type %qT", basetype
, type
);
63 return error_mark_node
;
67 binfo_or_else (tree base
, tree type
)
69 tree binfo
= lookup_base (type
, base
, ba_unique
,
70 NULL
, tf_warning_or_error
);
72 if (binfo
== error_mark_node
)
75 error_not_base_type (base
, type
);
79 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
80 value may not be changed thereafter. */
83 cxx_readonly_error (tree arg
, enum lvalue_use errstring
)
86 /* This macro is used to emit diagnostics to ensure that all format
87 strings are complete sentences, visible to gettext and checked at
90 #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG) \
107 gcc_unreachable (); \
111 /* Handle C++-specific things first. */
114 && DECL_LANG_SPECIFIC (arg
)
115 && DECL_IN_AGGR_P (arg
)
116 && !TREE_STATIC (arg
))
117 ERROR_FOR_ASSIGNMENT (G_("assignment of "
118 "constant field %qD"),
119 G_("constant field %qD "
120 "used as %<asm%> output"),
122 "constant field %qD"),
124 "constant field %qD"),
126 else if (INDIRECT_REF_P (arg
)
127 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 0))) == REFERENCE_TYPE
128 && (VAR_P (TREE_OPERAND (arg
, 0))
129 || TREE_CODE (TREE_OPERAND (arg
, 0)) == PARM_DECL
))
130 ERROR_FOR_ASSIGNMENT (G_("assignment of "
131 "read-only reference %qD"),
132 G_("read-only reference %qD "
133 "used as %<asm%> output"),
135 "read-only reference %qD"),
137 "read-only reference %qD"),
138 TREE_OPERAND (arg
, 0));
140 readonly_error (input_location
, arg
, errstring
);
144 /* Structure that holds information about declarations whose type was
145 incomplete and we could not check whether it was abstract or not. */
147 struct GTY((chain_next ("%h.next"), for_user
)) pending_abstract_type
{
148 /* Declaration which we are checking for abstractness. It is either
149 a DECL node, or an IDENTIFIER_NODE if we do not have a full
150 declaration available. */
153 /* Type which will be checked for abstractness. */
156 /* Kind of use in an unnamed declarator. */
157 abstract_class_use use
;
159 /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
160 because DECLs already carry locus information. */
163 /* Link to the next element in list. */
164 struct pending_abstract_type
* next
;
167 struct abstract_type_hasher
: ggc_hasher
<pending_abstract_type
*>
169 typedef tree compare_type
;
170 static hashval_t
hash (pending_abstract_type
*);
171 static bool equal (pending_abstract_type
*, tree
);
174 /* Compute the hash value of the node VAL. This function is used by the
175 hash table abstract_pending_vars. */
178 abstract_type_hasher::hash (pending_abstract_type
*pat
)
180 return (hashval_t
) TYPE_UID (pat
->type
);
184 /* Compare node VAL1 with the type VAL2. This function is used by the
185 hash table abstract_pending_vars. */
188 abstract_type_hasher::equal (pending_abstract_type
*pat1
, tree type2
)
190 return (pat1
->type
== type2
);
193 /* Hash table that maintains pending_abstract_type nodes, for which we still
194 need to check for type abstractness. The key of the table is the type
195 of the declaration. */
196 static GTY (()) hash_table
<abstract_type_hasher
> *abstract_pending_vars
= NULL
;
198 static int abstract_virtuals_error_sfinae (tree
, tree
, abstract_class_use
, tsubst_flags_t
);
200 /* This function is called after TYPE is completed, and will check if there
201 are pending declarations for which we still need to verify the abstractness
202 of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
203 turned out to be incomplete. */
206 complete_type_check_abstract (tree type
)
208 struct pending_abstract_type
*pat
;
209 location_t cur_loc
= input_location
;
211 gcc_assert (COMPLETE_TYPE_P (type
));
213 if (!abstract_pending_vars
)
216 /* Retrieve the list of pending declarations for this type. */
217 pending_abstract_type
**slot
218 = abstract_pending_vars
->find_slot_with_hash (type
, TYPE_UID (type
),
225 /* If the type is not abstract, do not do anything. */
226 if (CLASSTYPE_PURE_VIRTUALS (type
))
228 struct pending_abstract_type
*prev
= 0, *next
;
230 /* Reverse the list to emit the errors in top-down order. */
231 for (; pat
; pat
= next
)
239 /* Go through the list, and call abstract_virtuals_error for each
240 element: it will issue a diagnostic if the type is abstract. */
243 gcc_assert (type
== pat
->type
);
245 /* Tweak input_location so that the diagnostic appears at the correct
246 location. Notice that this is only needed if the decl is an
248 input_location
= pat
->locus
;
249 abstract_virtuals_error_sfinae (pat
->decl
, pat
->type
, pat
->use
,
250 tf_warning_or_error
);
255 abstract_pending_vars
->clear_slot (slot
);
257 input_location
= cur_loc
;
261 /* If TYPE has abstract virtual functions, issue an error about trying
262 to create an object of that type. DECL is the object declared, or
263 NULL_TREE if the declaration is unavailable, in which case USE specifies
264 the kind of invalid use. Returns 1 if an error occurred; zero if
268 abstract_virtuals_error_sfinae (tree decl
, tree type
, abstract_class_use use
,
269 tsubst_flags_t complain
)
271 vec
<tree
, va_gc
> *pure
;
273 /* This function applies only to classes. Any other entity can never
275 if (!CLASS_TYPE_P (type
))
277 type
= TYPE_MAIN_VARIANT (type
);
280 /* Instantiation here seems to be required by the standard,
281 but breaks e.g. boost::bind. FIXME! */
282 /* In SFINAE, non-N3276 context, force instantiation. */
283 if (!(complain
& (tf_error
|tf_decltype
)))
284 complete_type (type
);
287 /* If the type is incomplete, we register it within a hash table,
288 so that we can check again once it is completed. This makes sense
289 only for objects for which we have a declaration or at least a
291 if (!COMPLETE_TYPE_P (type
) && (complain
& tf_error
))
293 struct pending_abstract_type
*pat
;
295 gcc_assert (!decl
|| DECL_P (decl
) || identifier_p (decl
));
297 if (!abstract_pending_vars
)
298 abstract_pending_vars
299 = hash_table
<abstract_type_hasher
>::create_ggc (31);
301 pending_abstract_type
**slot
302 = abstract_pending_vars
->find_slot_with_hash (type
, TYPE_UID (type
),
305 pat
= ggc_alloc
<pending_abstract_type
> ();
309 pat
->locus
= ((decl
&& DECL_P (decl
))
310 ? DECL_SOURCE_LOCATION (decl
)
319 if (!TYPE_SIZE (type
))
320 /* TYPE is being defined, and during that time
321 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
324 pure
= CLASSTYPE_PURE_VIRTUALS (type
);
328 if (!(complain
& tf_error
))
334 error ("cannot declare variable %q+D to be of abstract "
335 "type %qT", decl
, type
);
336 else if (TREE_CODE (decl
) == PARM_DECL
)
338 if (DECL_NAME (decl
))
339 error ("cannot declare parameter %q+D to be of abstract type %qT",
342 error ("cannot declare parameter to be of abstract type %qT",
345 else if (TREE_CODE (decl
) == FIELD_DECL
)
346 error ("cannot declare field %q+D to be of abstract type %qT",
348 else if (TREE_CODE (decl
) == FUNCTION_DECL
349 && TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
350 error ("invalid abstract return type for member function %q+#D", decl
);
351 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
352 error ("invalid abstract return type for function %q+#D", decl
);
353 else if (identifier_p (decl
))
354 /* Here we do not have location information. */
355 error ("invalid abstract type %qT for %qE", type
, decl
);
357 error ("invalid abstract type for %q+D", decl
);
362 error ("creating array of %qT, which is an abstract class type", type
);
365 error ("invalid cast to abstract class type %qT", type
);
368 error ("invalid new-expression of abstract class type %qT", type
);
371 error ("invalid abstract return type %qT", type
);
374 error ("invalid abstract parameter type %qT", type
);
377 error ("expression of abstract class type %qT cannot "
378 "be used in throw-expression", type
);
381 error ("cannot declare catch parameter to be of abstract "
382 "class type %qT", type
);
385 error ("cannot allocate an object of abstract type %qT", type
);
388 /* Only go through this once. */
394 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
395 " because the following virtual functions are pure within %qT:",
398 FOR_EACH_VEC_ELT (*pure
, ix
, fn
)
399 if (! DECL_CLONED_FUNCTION_P (fn
)
400 || DECL_COMPLETE_DESTRUCTOR_P (fn
))
401 inform (input_location
, "\t%+#D", fn
);
403 /* Now truncate the vector. This leaves it non-null, so we know
404 there are pure virtuals, but empty so we don't list them out
413 abstract_virtuals_error_sfinae (tree decl
, tree type
, tsubst_flags_t complain
)
415 return abstract_virtuals_error_sfinae (decl
, type
, ACU_UNKNOWN
, complain
);
419 abstract_virtuals_error_sfinae (abstract_class_use use
, tree type
,
420 tsubst_flags_t complain
)
422 return abstract_virtuals_error_sfinae (NULL_TREE
, type
, use
, complain
);
426 /* Wrapper for the above function in the common case of wanting errors. */
429 abstract_virtuals_error (tree decl
, tree type
)
431 return abstract_virtuals_error_sfinae (decl
, type
, tf_warning_or_error
);
435 abstract_virtuals_error (abstract_class_use use
, tree type
)
437 return abstract_virtuals_error_sfinae (use
, type
, tf_warning_or_error
);
440 /* Print an inform about the declaration of the incomplete type TYPE. */
443 cxx_incomplete_type_inform (const_tree type
)
445 if (!TYPE_MAIN_DECL (type
))
448 location_t loc
= DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
));
449 tree ptype
= strip_top_quals (CONST_CAST_TREE (type
));
451 if (current_class_type
452 && TYPE_BEING_DEFINED (current_class_type
)
453 && same_type_p (ptype
, current_class_type
))
454 inform (loc
, "definition of %q#T is not complete until "
455 "the closing brace", ptype
);
456 else if (!TYPE_TEMPLATE_INFO (ptype
))
457 inform (loc
, "forward declaration of %q#T", ptype
);
459 inform (loc
, "declaration of %q#T", ptype
);
462 /* Print an error message for invalid use of an incomplete type.
463 VALUE is the expression that was used (or 0 if that isn't known)
464 and TYPE is the type that was invalid. DIAG_KIND indicates the
465 type of diagnostic (see diagnostic.def). */
468 cxx_incomplete_type_diagnostic (const_tree value
, const_tree type
,
469 diagnostic_t diag_kind
)
471 bool is_decl
= false, complained
= false;
473 gcc_assert (diag_kind
== DK_WARNING
474 || diag_kind
== DK_PEDWARN
475 || diag_kind
== DK_ERROR
);
477 /* Avoid duplicate error message. */
478 if (TREE_CODE (type
) == ERROR_MARK
)
481 if (value
!= 0 && (VAR_P (value
)
482 || TREE_CODE (value
) == PARM_DECL
483 || TREE_CODE (value
) == FIELD_DECL
))
485 complained
= emit_diagnostic (diag_kind
, input_location
, 0,
486 "%q+D has incomplete type", value
);
490 /* We must print an error message. Be clever about what it says. */
492 switch (TREE_CODE (type
))
498 complained
= emit_diagnostic (diag_kind
, input_location
, 0,
499 "invalid use of incomplete type %q#T",
502 cxx_incomplete_type_inform (type
);
506 emit_diagnostic (diag_kind
, input_location
, 0,
507 "invalid use of %qT", type
);
511 if (TYPE_DOMAIN (type
))
513 type
= TREE_TYPE (type
);
516 emit_diagnostic (diag_kind
, input_location
, 0,
517 "invalid use of array with unspecified bounds");
523 tree member
= TREE_OPERAND (value
, 1);
524 if (is_overloaded_fn (member
))
525 member
= get_first_fn (member
);
526 if (DECL_FUNCTION_MEMBER_P (member
)
527 && ! flag_ms_extensions
)
528 emit_diagnostic (diag_kind
, input_location
, 0,
529 "invalid use of member function "
530 "(did you forget the %<()%> ?)");
532 emit_diagnostic (diag_kind
, input_location
, 0,
533 "invalid use of member "
534 "(did you forget the %<&%> ?)");
538 case TEMPLATE_TYPE_PARM
:
540 emit_diagnostic (diag_kind
, input_location
, 0,
541 "invalid use of %<auto%>");
543 emit_diagnostic (diag_kind
, input_location
, 0,
544 "invalid use of template type parameter %qT", type
);
547 case BOUND_TEMPLATE_TEMPLATE_PARM
:
548 emit_diagnostic (diag_kind
, input_location
, 0,
549 "invalid use of template template parameter %qT",
554 emit_diagnostic (diag_kind
, input_location
, 0,
555 "invalid use of dependent type %qT", type
);
559 if (type
== init_list_type_node
)
561 emit_diagnostic (diag_kind
, input_location
, 0,
562 "invalid use of brace-enclosed initializer list");
565 gcc_assert (type
== unknown_type_node
);
566 if (value
&& TREE_CODE (value
) == COMPONENT_REF
)
568 else if (value
&& TREE_CODE (value
) == ADDR_EXPR
)
569 emit_diagnostic (diag_kind
, input_location
, 0,
570 "address of overloaded function with no contextual "
572 else if (value
&& TREE_CODE (value
) == OVERLOAD
)
573 emit_diagnostic (diag_kind
, input_location
, 0,
574 "overloaded function with no contextual type information");
576 emit_diagnostic (diag_kind
, input_location
, 0,
577 "insufficient contextual information to determine type");
585 /* Backward-compatibility interface to incomplete_type_diagnostic;
586 required by ../tree.c. */
587 #undef cxx_incomplete_type_error
589 cxx_incomplete_type_error (const_tree value
, const_tree type
)
591 cxx_incomplete_type_diagnostic (value
, type
, DK_ERROR
);
595 /* The recursive part of split_nonconstant_init. DEST is an lvalue
596 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
597 Return true if the whole of the value was initialized by the
598 generated statements. */
601 split_nonconstant_init_1 (tree dest
, tree init
)
603 unsigned HOST_WIDE_INT idx
;
604 tree field_index
, value
;
605 tree type
= TREE_TYPE (dest
);
606 tree inner_type
= NULL
;
607 bool array_type_p
= false;
608 bool complete_p
= true;
609 HOST_WIDE_INT num_split_elts
= 0;
611 switch (TREE_CODE (type
))
614 inner_type
= TREE_TYPE (type
);
616 if ((TREE_SIDE_EFFECTS (init
)
617 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
618 || array_of_runtime_bound_p (type
))
620 /* For an array, we only need/want a single cleanup region rather
621 than one per element. */
622 tree code
= build_vec_init (dest
, NULL_TREE
, init
, false, 1,
623 tf_warning_or_error
);
631 case QUAL_UNION_TYPE
:
632 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init
), idx
,
635 /* The current implementation of this algorithm assumes that
636 the field was set for all the elements. This is usually done
637 by process_init_constructor. */
638 gcc_assert (field_index
);
641 inner_type
= TREE_TYPE (field_index
);
643 if (TREE_CODE (value
) == CONSTRUCTOR
)
648 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
649 NULL_TREE
, NULL_TREE
);
651 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
654 if (!split_nonconstant_init_1 (sub
, value
))
658 else if (!initializer_constant_valid_p (value
, inner_type
))
663 /* FIXME: Ordered removal is O(1) so the whole function is
664 worst-case quadratic. This could be fixed using an aside
665 bitmap to record which elements must be removed and remove
666 them all at the same time. Or by merging
667 split_non_constant_init into process_init_constructor_array,
668 that is separating constants from non-constants while building
670 CONSTRUCTOR_ELTS (init
)->ordered_remove (idx
);
673 if (TREE_CODE (field_index
) == RANGE_EXPR
)
675 /* Use build_vec_init to initialize a range. */
676 tree low
= TREE_OPERAND (field_index
, 0);
677 tree hi
= TREE_OPERAND (field_index
, 1);
678 sub
= build4 (ARRAY_REF
, inner_type
, dest
, low
,
679 NULL_TREE
, NULL_TREE
);
680 sub
= cp_build_addr_expr (sub
, tf_warning_or_error
);
681 tree max
= size_binop (MINUS_EXPR
, hi
, low
);
682 code
= build_vec_init (sub
, max
, value
, false, 0,
683 tf_warning_or_error
);
685 if (tree_fits_shwi_p (max
))
686 num_split_elts
+= tree_to_shwi (max
);
691 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
692 NULL_TREE
, NULL_TREE
);
694 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
697 code
= build2 (INIT_EXPR
, inner_type
, sub
, value
);
698 code
= build_stmt (input_location
, EXPR_STMT
, code
);
699 code
= maybe_cleanup_point_expr_void (code
);
701 if (type_build_dtor_call (inner_type
))
703 code
= (build_special_member_call
704 (sub
, complete_dtor_identifier
, NULL
, inner_type
,
705 LOOKUP_NORMAL
, tf_warning_or_error
));
706 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (inner_type
))
707 finish_eh_cleanup (code
);
717 if (!initializer_constant_valid_p (init
, type
))
720 tree cons
= copy_node (init
);
721 CONSTRUCTOR_ELTS (init
) = NULL
;
722 code
= build2 (MODIFY_EXPR
, type
, dest
, cons
);
723 code
= build_stmt (input_location
, EXPR_STMT
, code
);
725 num_split_elts
+= CONSTRUCTOR_NELTS (init
);
733 /* The rest of the initializer is now a constant. */
734 TREE_CONSTANT (init
) = 1;
735 return complete_p
&& complete_ctor_at_level_p (TREE_TYPE (init
),
736 num_split_elts
, inner_type
);
739 /* A subroutine of store_init_value. Splits non-constant static
740 initializer INIT into a constant part and generates code to
741 perform the non-constant part of the initialization to DEST.
742 Returns the code for the runtime init. */
745 split_nonconstant_init (tree dest
, tree init
)
749 if (TREE_CODE (init
) == TARGET_EXPR
)
750 init
= TARGET_EXPR_INITIAL (init
);
751 if (TREE_CODE (init
) == CONSTRUCTOR
)
753 code
= push_stmt_list ();
754 if (split_nonconstant_init_1 (dest
, init
))
756 code
= pop_stmt_list (code
);
757 DECL_INITIAL (dest
) = init
;
758 TREE_READONLY (dest
) = 0;
761 code
= build2 (INIT_EXPR
, TREE_TYPE (dest
), dest
, init
);
766 /* Perform appropriate conversions on the initial value of a variable,
767 store it in the declaration DECL,
768 and print any error messages that are appropriate.
769 If the init is invalid, store an ERROR_MARK.
771 C++: Note that INIT might be a TREE_LIST, which would mean that it is
772 a base class initializer for some aggregate type, hopefully compatible
773 with DECL. If INIT is a single element, and DECL is an aggregate
774 type, we silently convert INIT into a TREE_LIST, allowing a constructor
777 If INIT is a TREE_LIST and there is no constructor, turn INIT
778 into a CONSTRUCTOR and use standard initialization techniques.
779 Perhaps a warning should be generated?
781 Returns code to be executed if initialization could not be performed
782 for static variable. In that case, caller must emit the code. */
785 store_init_value (tree decl
, tree init
, vec
<tree
, va_gc
>** cleanups
, int flags
)
789 /* If variable's type was invalidly declared, just ignore it. */
791 type
= TREE_TYPE (decl
);
792 if (TREE_CODE (type
) == ERROR_MARK
)
795 if (MAYBE_CLASS_TYPE_P (type
))
797 if (TREE_CODE (init
) == TREE_LIST
)
799 error ("constructor syntax used, but no constructor declared "
800 "for type %qT", type
);
801 init
= build_constructor_from_list (init_list_type_node
, nreverse (init
));
805 /* End of special C++ code. */
807 if (flags
& LOOKUP_ALREADY_DIGESTED
)
810 /* Digest the specified initializer into an expression. */
811 value
= digest_init_flags (type
, init
, flags
);
813 value
= extend_ref_init_temps (decl
, value
, cleanups
);
815 /* In C++11 constant expression is a semantic, not syntactic, property.
816 In C++98, make sure that what we thought was a constant expression at
817 template definition time is still constant and otherwise perform this
818 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
819 if (decl_maybe_constant_var_p (decl
) || TREE_STATIC (decl
))
822 value
= instantiate_non_dependent_expr (value
);
823 if (DECL_DECLARED_CONSTEXPR_P (decl
)
824 || DECL_IN_AGGR_P (decl
))
826 /* Diagnose a non-constant initializer for constexpr. */
827 if (processing_template_decl
828 && !require_potential_constant_expression (value
))
829 value
= error_mark_node
;
831 value
= cxx_constant_value (value
, decl
);
833 value
= maybe_constant_init (value
, decl
);
834 if (TREE_CODE (value
) == CONSTRUCTOR
&& cp_has_mutable_p (type
))
835 /* Poison this CONSTRUCTOR so it can't be copied to another
836 constexpr variable. */
837 CONSTRUCTOR_MUTABLE_POISON (value
) = true;
838 const_init
= (reduced_constant_expression_p (value
)
839 || error_operand_p (value
));
840 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = const_init
;
841 TREE_CONSTANT (decl
) = const_init
&& decl_maybe_constant_var_p (decl
);
844 if (cxx_dialect
>= cxx14
)
845 /* Handle aggregate NSDMI in non-constant initializers, too. */
846 value
= replace_placeholders (value
, decl
);
848 /* If the initializer is not a constant, fill in DECL_INITIAL with
849 the bits that are constant, and then return an expression that
850 will perform the dynamic initialization. */
851 if (value
!= error_mark_node
852 && (TREE_SIDE_EFFECTS (value
)
853 || array_of_runtime_bound_p (type
)
854 || ! reduced_constant_expression_p (value
)))
855 return split_nonconstant_init (decl
, value
);
856 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
857 is an automatic variable, the middle end will turn this into a
858 dynamic initialization later. */
859 DECL_INITIAL (decl
) = value
;
864 /* Give diagnostic about narrowing conversions within { }. */
867 check_narrowing (tree type
, tree init
, tsubst_flags_t complain
)
869 tree ftype
= unlowered_expr_type (init
);
873 if (((!warn_narrowing
|| !(complain
& tf_warning
))
874 && cxx_dialect
== cxx98
)
875 || !ARITHMETIC_TYPE_P (type
))
878 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
879 && TREE_CODE (type
) == COMPLEX_TYPE
)
881 tree elttype
= TREE_TYPE (type
);
882 if (CONSTRUCTOR_NELTS (init
) > 0)
883 ok
&= check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 0)->value
,
885 if (CONSTRUCTOR_NELTS (init
) > 1)
886 ok
&= check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 1)->value
,
891 init
= fold_non_dependent_expr (init
);
893 if (TREE_CODE (type
) == INTEGER_TYPE
894 && TREE_CODE (ftype
) == REAL_TYPE
)
896 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
897 && CP_INTEGRAL_TYPE_P (type
))
899 if (TREE_CODE (ftype
) == ENUMERAL_TYPE
)
900 /* Check for narrowing based on the values of the enumeration. */
901 ftype
= ENUM_UNDERLYING_TYPE (ftype
);
902 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type
),
903 TYPE_MAX_VALUE (ftype
))
904 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype
),
905 TYPE_MIN_VALUE (type
)))
906 && (TREE_CODE (init
) != INTEGER_CST
907 || !int_fits_type_p (init
, type
)))
910 else if (TREE_CODE (ftype
) == REAL_TYPE
911 && TREE_CODE (type
) == REAL_TYPE
)
913 if (TYPE_PRECISION (type
) < TYPE_PRECISION (ftype
))
915 if (TREE_CODE (init
) == REAL_CST
)
917 /* Issue 703: Loss of precision is OK as long as the value is
918 within the representable range of the new type. */
920 d
= TREE_REAL_CST (init
);
921 real_convert (&r
, TYPE_MODE (type
), &d
);
929 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
930 && TREE_CODE (type
) == REAL_TYPE
)
933 if (TREE_CODE (init
) == INTEGER_CST
)
935 d
= real_value_from_int_cst (0, init
);
936 if (exact_real_truncate (TYPE_MODE (type
), &d
))
943 if (cxx_dialect
== cxx98
)
944 warning_at (EXPR_LOC_OR_LOC (init
, input_location
), OPT_Wnarrowing
,
945 "narrowing conversion of %qE from %qT to %qT inside { } "
946 "is ill-formed in C++11", init
, ftype
, type
);
947 else if (!TREE_CONSTANT (init
))
949 if (complain
& tf_warning_or_error
)
951 pedwarn (EXPR_LOC_OR_LOC (init
, input_location
), OPT_Wnarrowing
,
952 "narrowing conversion of %qE from %qT to %qT inside { }",
957 else if (complain
& tf_error
)
958 error_at (EXPR_LOC_OR_LOC (init
, input_location
),
959 "narrowing conversion of %qE from %qT to %qT inside { }",
963 return cxx_dialect
== cxx98
|| ok
;
966 /* Process the initializer INIT for a variable of type TYPE, emitting
967 diagnostics for invalid initializers and converting the initializer as
970 For aggregate types, it assumes that reshape_init has already run, thus the
971 initializer will have the right shape (brace elision has been undone).
973 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
976 digest_init_r (tree type
, tree init
, bool nested
, int flags
,
977 tsubst_flags_t complain
)
979 enum tree_code code
= TREE_CODE (type
);
981 if (error_operand_p (init
))
982 return error_mark_node
;
986 /* We must strip the outermost array type when completing the type,
987 because the its bounds might be incomplete at the moment. */
988 if (!complete_type_or_maybe_complain (TREE_CODE (type
) == ARRAY_TYPE
989 ? TREE_TYPE (type
) : type
, NULL_TREE
,
991 return error_mark_node
;
993 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
994 (g++.old-deja/g++.law/casts2.C). */
995 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
996 init
= TREE_OPERAND (init
, 0);
998 /* Initialization of an array of chars from a string constant. The initializer
999 can be optionally enclosed in braces, but reshape_init has already removed
1000 them if they were present. */
1001 if (code
== ARRAY_TYPE
)
1003 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
1004 if (char_type_p (typ1
)
1006 && TREE_CODE (init
) == STRING_CST
)
1008 tree char_type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init
)));
1010 if (TYPE_PRECISION (typ1
) == BITS_PER_UNIT
)
1012 if (char_type
!= char_type_node
)
1014 if (complain
& tf_error
)
1015 error ("char-array initialized from wide string");
1016 return error_mark_node
;
1021 if (char_type
== char_type_node
)
1023 if (complain
& tf_error
)
1024 error ("int-array initialized from non-wide string");
1025 return error_mark_node
;
1027 else if (char_type
!= typ1
)
1029 if (complain
& tf_error
)
1030 error ("int-array initialized from incompatible "
1032 return error_mark_node
;
1036 if (type
!= TREE_TYPE (init
))
1038 init
= copy_node (init
);
1039 TREE_TYPE (init
) = type
;
1041 if (TYPE_DOMAIN (type
) != 0 && TREE_CONSTANT (TYPE_SIZE (type
)))
1043 int size
= TREE_INT_CST_LOW (TYPE_SIZE (type
));
1044 size
= (size
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
;
1045 /* In C it is ok to subtract 1 from the length of the string
1046 because it's ok to ignore the terminating null char that is
1047 counted in the length of the constant, but in C++ this would
1049 if (size
< TREE_STRING_LENGTH (init
))
1050 permerror (input_location
, "initializer-string for array "
1051 "of chars is too long");
1057 /* Handle scalar types (including conversions) and references. */
1058 if ((TREE_CODE (type
) != COMPLEX_TYPE
1059 || BRACE_ENCLOSED_INITIALIZER_P (init
))
1060 && (SCALAR_TYPE_P (type
) || code
== REFERENCE_TYPE
))
1065 flags
|= LOOKUP_NO_NARROWING
;
1066 init
= convert_for_initialization (0, type
, init
, flags
,
1067 ICR_INIT
, NULL_TREE
, 0,
1071 /* Skip any conversions since we'll be outputting the underlying
1073 while (CONVERT_EXPR_P (*exp
)
1074 || TREE_CODE (*exp
) == NON_LVALUE_EXPR
)
1075 exp
= &TREE_OPERAND (*exp
, 0);
1077 *exp
= cplus_expand_constant (*exp
);
1082 /* Come here only for aggregates: records, arrays, unions, complex numbers
1084 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
1085 || TREE_CODE (type
) == VECTOR_TYPE
1086 || TREE_CODE (type
) == RECORD_TYPE
1087 || TREE_CODE (type
) == UNION_TYPE
1088 || TREE_CODE (type
) == COMPLEX_TYPE
);
1090 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
1091 && !TYPE_NON_AGGREGATE_CLASS (type
))
1092 return process_init_constructor (type
, init
, complain
);
1095 if (COMPOUND_LITERAL_P (init
) && TREE_CODE (type
) == ARRAY_TYPE
)
1097 if (complain
& tf_error
)
1098 error ("cannot initialize aggregate of type %qT with "
1099 "a compound literal", type
);
1101 return error_mark_node
;
1104 if (TREE_CODE (type
) == ARRAY_TYPE
1105 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
1107 /* Allow the result of build_array_copy and of
1108 build_value_init_noctor. */
1109 if ((TREE_CODE (init
) == VEC_INIT_EXPR
1110 || TREE_CODE (init
) == CONSTRUCTOR
)
1111 && (same_type_ignoring_top_level_qualifiers_p
1112 (type
, TREE_TYPE (init
))))
1115 if (complain
& tf_error
)
1116 error ("array must be initialized with a brace-enclosed"
1118 return error_mark_node
;
1121 return convert_for_initialization (NULL_TREE
, type
, init
,
1123 ICR_INIT
, NULL_TREE
, 0,
1129 digest_init (tree type
, tree init
, tsubst_flags_t complain
)
1131 return digest_init_r (type
, init
, false, LOOKUP_IMPLICIT
, complain
);
1135 digest_init_flags (tree type
, tree init
, int flags
)
1137 return digest_init_r (type
, init
, false, flags
, tf_warning_or_error
);
1140 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
1142 digest_nsdmi_init (tree decl
, tree init
)
1144 gcc_assert (TREE_CODE (decl
) == FIELD_DECL
);
1146 int flags
= LOOKUP_IMPLICIT
;
1147 if (DIRECT_LIST_INIT_P (init
))
1148 flags
= LOOKUP_NORMAL
;
1149 init
= digest_init_flags (TREE_TYPE (decl
), init
, flags
);
1150 if (TREE_CODE (init
) == TARGET_EXPR
)
1151 /* This represents the whole initialization. */
1152 TARGET_EXPR_DIRECT_INIT_P (init
) = true;
1156 /* Set of flags used within process_init_constructor to describe the
1158 #define PICFLAG_ERRONEOUS 1
1159 #define PICFLAG_NOT_ALL_CONSTANT 2
1160 #define PICFLAG_NOT_ALL_SIMPLE 4
1161 #define PICFLAG_SIDE_EFFECTS 8
1163 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1167 picflag_from_initializer (tree init
)
1169 if (init
== error_mark_node
)
1170 return PICFLAG_ERRONEOUS
;
1171 else if (!TREE_CONSTANT (init
))
1173 if (TREE_SIDE_EFFECTS (init
))
1174 return PICFLAG_SIDE_EFFECTS
;
1176 return PICFLAG_NOT_ALL_CONSTANT
;
1178 else if (!initializer_constant_valid_p (init
, TREE_TYPE (init
)))
1179 return PICFLAG_NOT_ALL_SIMPLE
;
1183 /* Adjust INIT for going into a CONSTRUCTOR. */
1186 massage_init_elt (tree type
, tree init
, tsubst_flags_t complain
)
1188 init
= digest_init_r (type
, init
, true, LOOKUP_IMPLICIT
, complain
);
1189 /* Strip a simple TARGET_EXPR when we know this is an initializer. */
1190 if (TREE_CODE (init
) == TARGET_EXPR
1191 && !VOID_TYPE_P (TREE_TYPE (TARGET_EXPR_INITIAL (init
))))
1192 init
= TARGET_EXPR_INITIAL (init
);
1193 /* When we defer constant folding within a statement, we may want to
1194 defer this folding as well. */
1195 tree t
= fold_non_dependent_expr (init
);
1196 t
= maybe_constant_init (t
);
1197 if (TREE_CONSTANT (t
))
1202 /* Subroutine of process_init_constructor, which will process an initializer
1203 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1204 which describe the initializers. */
1207 process_init_constructor_array (tree type
, tree init
,
1208 tsubst_flags_t complain
)
1210 unsigned HOST_WIDE_INT i
, len
= 0;
1212 bool unbounded
= false;
1213 constructor_elt
*ce
;
1214 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (init
);
1216 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
1217 || TREE_CODE (type
) == VECTOR_TYPE
);
1219 if (TREE_CODE (type
) == ARRAY_TYPE
)
1221 tree domain
= TYPE_DOMAIN (type
);
1222 if (domain
&& TREE_CONSTANT (TYPE_MAX_VALUE (domain
)))
1223 len
= wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain
))
1224 - wi::to_offset (TYPE_MIN_VALUE (domain
)) + 1,
1225 TYPE_PRECISION (TREE_TYPE (domain
)),
1226 TYPE_SIGN (TREE_TYPE (domain
))).to_uhwi ();
1228 unbounded
= true; /* Take as many as there are. */
1231 /* Vectors are like simple fixed-size arrays. */
1232 len
= TYPE_VECTOR_SUBPARTS (type
);
1234 /* There must not be more initializers than needed. */
1235 if (!unbounded
&& vec_safe_length (v
) > len
)
1237 if (complain
& tf_error
)
1238 error ("too many initializers for %qT", type
);
1240 return PICFLAG_ERRONEOUS
;
1243 FOR_EACH_VEC_SAFE_ELT (v
, i
, ce
)
1247 gcc_assert (TREE_CODE (ce
->index
) == INTEGER_CST
);
1248 if (compare_tree_int (ce
->index
, i
) != 0)
1250 ce
->value
= error_mark_node
;
1251 sorry ("non-trivial designated initializers not supported");
1255 ce
->index
= size_int (i
);
1256 gcc_assert (ce
->value
);
1257 ce
->value
= massage_init_elt (TREE_TYPE (type
), ce
->value
, complain
);
1259 if (ce
->value
!= error_mark_node
)
1260 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1261 (TREE_TYPE (type
), TREE_TYPE (ce
->value
)));
1263 flags
|= picflag_from_initializer (ce
->value
);
1266 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1267 we must add initializers ourselves. */
1269 for (; i
< len
; ++i
)
1273 if (type_build_ctor_call (TREE_TYPE (type
)))
1275 /* If this type needs constructors run for default-initialization,
1276 we can't rely on the back end to do it for us, so make the
1277 initialization explicit by list-initializing from T{}. */
1278 next
= build_constructor (init_list_type_node
, NULL
);
1279 CONSTRUCTOR_IS_DIRECT_INIT (next
) = true;
1280 next
= massage_init_elt (TREE_TYPE (type
), next
, complain
);
1281 if (initializer_zerop (next
))
1282 /* The default zero-initialization is fine for us; don't
1283 add anything to the CONSTRUCTOR. */
1286 else if (!zero_init_p (TREE_TYPE (type
)))
1287 next
= build_zero_init (TREE_TYPE (type
),
1288 /*nelts=*/NULL_TREE
,
1289 /*static_storage_p=*/false);
1291 /* The default zero-initialization is fine for us; don't
1292 add anything to the CONSTRUCTOR. */
1297 flags
|= picflag_from_initializer (next
);
1298 CONSTRUCTOR_APPEND_ELT (v
, size_int (i
), next
);
1302 CONSTRUCTOR_ELTS (init
) = v
;
1306 /* Subroutine of process_init_constructor, which will process an initializer
1307 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1308 the initializers. */
1311 process_init_constructor_record (tree type
, tree init
,
1312 tsubst_flags_t complain
)
1314 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1318 gcc_assert (TREE_CODE (type
) == RECORD_TYPE
);
1319 gcc_assert (!CLASSTYPE_VBASECLASSES (type
));
1320 gcc_assert (!TYPE_BINFO (type
)
1321 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)));
1322 gcc_assert (!TYPE_POLYMORPHIC_P (type
));
1326 unsigned HOST_WIDE_INT idx
= 0;
1327 /* Generally, we will always have an index for each initializer (which is
1328 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1329 reshape_init. So we need to handle both cases. */
1330 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1335 if (!DECL_NAME (field
) && DECL_C_BIT_FIELD (field
))
1338 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
1341 /* If this is a bitfield, first convert to the declared type. */
1342 type
= TREE_TYPE (field
);
1343 if (DECL_BIT_FIELD_TYPE (field
))
1344 type
= DECL_BIT_FIELD_TYPE (field
);
1345 if (type
== error_mark_node
)
1346 return PICFLAG_ERRONEOUS
;
1348 if (idx
< vec_safe_length (CONSTRUCTOR_ELTS (init
)))
1350 constructor_elt
*ce
= &(*CONSTRUCTOR_ELTS (init
))[idx
];
1353 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1354 latter case can happen in templates where lookup has to be
1356 gcc_assert (TREE_CODE (ce
->index
) == FIELD_DECL
1357 || identifier_p (ce
->index
));
1358 if (ce
->index
!= field
1359 && ce
->index
!= DECL_NAME (field
))
1361 ce
->value
= error_mark_node
;
1362 sorry ("non-trivial designated initializers not supported");
1366 gcc_assert (ce
->value
);
1367 next
= massage_init_elt (type
, ce
->value
, complain
);
1370 else if (DECL_INITIAL (field
))
1374 /* We're using an NSDMI past a field with implicit
1375 zero-init. Go back and make it explicit. */
1377 vec_safe_truncate (v
, 0);
1380 /* C++14 aggregate NSDMI. */
1381 next
= get_nsdmi (field
, /*ctor*/false);
1383 else if (type_build_ctor_call (TREE_TYPE (field
)))
1385 /* If this type needs constructors run for
1386 default-initialization, we can't rely on the back end to do it
1387 for us, so build up TARGET_EXPRs. If the type in question is
1388 a class, just build one up; if it's an array, recurse. */
1389 next
= build_constructor (init_list_type_node
, NULL
);
1390 /* Call this direct-initialization pending DR 1518 resolution so
1391 that explicit default ctors don't break valid C++03 code. */
1392 CONSTRUCTOR_IS_DIRECT_INIT (next
) = true;
1393 next
= massage_init_elt (TREE_TYPE (field
), next
, complain
);
1395 /* Warn when some struct elements are implicitly initialized. */
1396 if ((complain
& tf_warning
)
1397 && !EMPTY_CONSTRUCTOR_P (init
))
1398 warning (OPT_Wmissing_field_initializers
,
1399 "missing initializer for member %qD", field
);
1403 if (TREE_CODE (TREE_TYPE (field
)) == REFERENCE_TYPE
)
1405 if (complain
& tf_error
)
1406 error ("member %qD is uninitialized reference", field
);
1408 return PICFLAG_ERRONEOUS
;
1410 else if (CLASSTYPE_REF_FIELDS_NEED_INIT (TREE_TYPE (field
)))
1412 if (complain
& tf_error
)
1413 error ("member %qD with uninitialized reference fields", field
);
1415 return PICFLAG_ERRONEOUS
;
1418 /* Warn when some struct elements are implicitly initialized
1420 if ((complain
& tf_warning
)
1421 && !EMPTY_CONSTRUCTOR_P (init
))
1422 warning (OPT_Wmissing_field_initializers
,
1423 "missing initializer for member %qD", field
);
1425 if (!zero_init_p (TREE_TYPE (field
))
1427 next
= build_zero_init (TREE_TYPE (field
), /*nelts=*/NULL_TREE
,
1428 /*static_storage_p=*/false);
1431 /* The default zero-initialization is fine for us; don't
1432 add anything to the CONSTRUCTOR. */
1438 /* If this is a bitfield, now convert to the lowered type. */
1439 if (type
!= TREE_TYPE (field
))
1440 next
= cp_convert_and_check (TREE_TYPE (field
), next
, complain
);
1441 flags
|= picflag_from_initializer (next
);
1442 CONSTRUCTOR_APPEND_ELT (v
, field
, next
);
1445 if (idx
< vec_safe_length (CONSTRUCTOR_ELTS (init
)))
1447 if (complain
& tf_error
)
1448 error ("too many initializers for %qT", type
);
1450 return PICFLAG_ERRONEOUS
;
1453 CONSTRUCTOR_ELTS (init
) = v
;
1457 /* Subroutine of process_init_constructor, which will process a single
1458 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1459 which describe the initializer. */
1462 process_init_constructor_union (tree type
, tree init
,
1463 tsubst_flags_t complain
)
1465 constructor_elt
*ce
;
1468 /* If the initializer was empty, use default zero initialization. */
1469 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
1472 len
= CONSTRUCTOR_ELTS (init
)->length ();
1475 if (!(complain
& tf_error
))
1476 return PICFLAG_ERRONEOUS
;
1477 error ("too many initializers for %qT", type
);
1478 CONSTRUCTOR_ELTS (init
)->block_remove (1, len
-1);
1481 ce
= &(*CONSTRUCTOR_ELTS (init
))[0];
1483 /* If this element specifies a field, initialize via that field. */
1486 if (TREE_CODE (ce
->index
) == FIELD_DECL
)
1488 else if (identifier_p (ce
->index
))
1490 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1491 tree name
= ce
->index
;
1493 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1494 if (DECL_NAME (field
) == name
)
1498 if (complain
& tf_error
)
1499 error ("no field %qD found in union being initialized",
1501 ce
->value
= error_mark_node
;
1507 gcc_assert (TREE_CODE (ce
->index
) == INTEGER_CST
1508 || TREE_CODE (ce
->index
) == RANGE_EXPR
);
1509 if (complain
& tf_error
)
1510 error ("index value instead of field name in union initializer");
1511 ce
->value
= error_mark_node
;
1516 /* Find the first named field. ANSI decided in September 1990
1517 that only named fields count here. */
1518 tree field
= TYPE_FIELDS (type
);
1519 while (field
&& (!DECL_NAME (field
) || TREE_CODE (field
) != FIELD_DECL
))
1520 field
= TREE_CHAIN (field
);
1521 if (field
== NULL_TREE
)
1523 if (complain
& tf_error
)
1524 error ("too many initializers for %qT", type
);
1525 ce
->value
= error_mark_node
;
1530 if (ce
->value
&& ce
->value
!= error_mark_node
)
1531 ce
->value
= massage_init_elt (TREE_TYPE (ce
->index
), ce
->value
, complain
);
1533 return picflag_from_initializer (ce
->value
);
1536 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1537 constructor is a brace-enclosed initializer, and will be modified in-place.
1539 Each element is converted to the right type through digest_init, and
1540 missing initializers are added following the language rules (zero-padding,
1543 After the execution, the initializer will have TREE_CONSTANT if all elts are
1544 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1545 constants that the assembler and linker can compute them.
1547 The function returns the initializer itself, or error_mark_node in case
1551 process_init_constructor (tree type
, tree init
, tsubst_flags_t complain
)
1555 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init
));
1557 if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == VECTOR_TYPE
)
1558 flags
= process_init_constructor_array (type
, init
, complain
);
1559 else if (TREE_CODE (type
) == RECORD_TYPE
)
1560 flags
= process_init_constructor_record (type
, init
, complain
);
1561 else if (TREE_CODE (type
) == UNION_TYPE
)
1562 flags
= process_init_constructor_union (type
, init
, complain
);
1566 if (flags
& PICFLAG_ERRONEOUS
)
1567 return error_mark_node
;
1569 TREE_TYPE (init
) = type
;
1570 if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
) == NULL_TREE
)
1571 cp_complete_array_type (&TREE_TYPE (init
), init
, /*do_default=*/0);
1572 if (flags
& PICFLAG_SIDE_EFFECTS
)
1574 TREE_CONSTANT (init
) = false;
1575 TREE_SIDE_EFFECTS (init
) = true;
1577 else if (flags
& PICFLAG_NOT_ALL_CONSTANT
)
1578 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1579 TREE_CONSTANT (init
) = false;
1582 TREE_CONSTANT (init
) = 1;
1583 if (!(flags
& PICFLAG_NOT_ALL_SIMPLE
))
1584 TREE_STATIC (init
) = 1;
1589 /* Given a structure or union value DATUM, construct and return
1590 the structure or union component which results from narrowing
1591 that value to the base specified in BASETYPE. For example, given the
1594 class L { int ii; };
1595 class A : L { ... };
1596 class B : L { ... };
1597 class C : A, B { ... };
1605 x.A::ii refers to the ii member of the L part of
1606 the A part of the C object named by X. In this case,
1607 DATUM would be x, and BASETYPE would be A.
1609 I used to think that this was nonconformant, that the standard specified
1610 that first we look up ii in A, then convert x to an L& and pull out the
1611 ii part. But in fact, it does say that we convert x to an A&; A here
1612 is known as the "naming class". (jason 2000-12-19)
1614 BINFO_P points to a variable initialized either to NULL_TREE or to the
1615 binfo for the specific base subobject we want to convert to. */
1618 build_scoped_ref (tree datum
, tree basetype
, tree
* binfo_p
)
1622 if (datum
== error_mark_node
)
1623 return error_mark_node
;
1627 binfo
= lookup_base (TREE_TYPE (datum
), basetype
, ba_check
,
1628 NULL
, tf_warning_or_error
);
1630 if (!binfo
|| binfo
== error_mark_node
)
1632 *binfo_p
= NULL_TREE
;
1634 error_not_base_type (basetype
, TREE_TYPE (datum
));
1635 return error_mark_node
;
1639 return build_base_path (PLUS_EXPR
, datum
, binfo
, 1,
1640 tf_warning_or_error
);
1643 /* Build a reference to an object specified by the C++ `->' operator.
1644 Usually this just involves dereferencing the object, but if the
1645 `->' operator is overloaded, then such overloads must be
1646 performed until an object which does not have the `->' operator
1647 overloaded is found. An error is reported when circular pointer
1648 delegation is detected. */
1651 build_x_arrow (location_t loc
, tree expr
, tsubst_flags_t complain
)
1653 tree orig_expr
= expr
;
1654 tree type
= TREE_TYPE (expr
);
1655 tree last_rval
= NULL_TREE
;
1656 vec
<tree
, va_gc
> *types_memoized
= NULL
;
1658 if (type
== error_mark_node
)
1659 return error_mark_node
;
1661 if (processing_template_decl
)
1663 if (type_dependent_expression_p (expr
))
1664 return build_min_nt_loc (loc
, ARROW_EXPR
, expr
);
1665 expr
= build_non_dependent_expr (expr
);
1668 if (MAYBE_CLASS_TYPE_P (type
))
1670 struct tinst_level
*actual_inst
= current_instantiation ();
1673 while ((expr
= build_new_op (loc
, COMPONENT_REF
,
1674 LOOKUP_NORMAL
, expr
, NULL_TREE
, NULL_TREE
,
1677 if (expr
== error_mark_node
)
1678 return error_mark_node
;
1680 /* This provides a better instantiation backtrace in case of
1682 if (fn
&& DECL_USE_TEMPLATE (fn
))
1683 push_tinst_level_loc (fn
,
1684 (current_instantiation () != actual_inst
)
1685 ? DECL_SOURCE_LOCATION (fn
)
1689 if (vec_member (TREE_TYPE (expr
), types_memoized
))
1691 if (complain
& tf_error
)
1692 error ("circular pointer delegation detected");
1693 return error_mark_node
;
1696 vec_safe_push (types_memoized
, TREE_TYPE (expr
));
1700 while (current_instantiation () != actual_inst
)
1703 if (last_rval
== NULL_TREE
)
1705 if (complain
& tf_error
)
1706 error ("base operand of %<->%> has non-pointer type %qT", type
);
1707 return error_mark_node
;
1710 if (TREE_CODE (TREE_TYPE (last_rval
)) == REFERENCE_TYPE
)
1711 last_rval
= convert_from_reference (last_rval
);
1714 last_rval
= decay_conversion (expr
, complain
);
1716 if (TYPE_PTR_P (TREE_TYPE (last_rval
)))
1718 if (processing_template_decl
)
1720 expr
= build_min (ARROW_EXPR
, TREE_TYPE (TREE_TYPE (last_rval
)),
1722 TREE_SIDE_EFFECTS (expr
) = TREE_SIDE_EFFECTS (last_rval
);
1726 return cp_build_indirect_ref (last_rval
, RO_NULL
, complain
);
1729 if (complain
& tf_error
)
1732 error ("result of %<operator->()%> yields non-pointer result");
1734 error ("base operand of %<->%> is not a pointer");
1736 return error_mark_node
;
1739 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1740 already been checked out to be of aggregate type. */
1743 build_m_component_ref (tree datum
, tree component
, tsubst_flags_t complain
)
1751 if (error_operand_p (datum
) || error_operand_p (component
))
1752 return error_mark_node
;
1754 datum
= mark_lvalue_use (datum
);
1755 component
= mark_rvalue_use (component
);
1757 ptrmem_type
= TREE_TYPE (component
);
1758 if (!TYPE_PTRMEM_P (ptrmem_type
))
1760 if (complain
& tf_error
)
1761 error ("%qE cannot be used as a member pointer, since it is of "
1762 "type %qT", component
, ptrmem_type
);
1763 return error_mark_node
;
1766 objtype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
1767 if (! MAYBE_CLASS_TYPE_P (objtype
))
1769 if (complain
& tf_error
)
1770 error ("cannot apply member pointer %qE to %qE, which is of "
1771 "non-class type %qT", component
, datum
, objtype
);
1772 return error_mark_node
;
1775 type
= TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type
);
1776 ctype
= complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type
));
1778 if (!COMPLETE_TYPE_P (ctype
))
1780 if (!same_type_p (ctype
, objtype
))
1786 binfo
= lookup_base (objtype
, ctype
, ba_check
, NULL
, complain
);
1791 if (complain
& tf_error
)
1792 error ("pointer to member type %qT incompatible with object "
1793 "type %qT", type
, objtype
);
1794 return error_mark_node
;
1796 else if (binfo
== error_mark_node
)
1797 return error_mark_node
;
1800 if (TYPE_PTRDATAMEM_P (ptrmem_type
))
1802 cp_lvalue_kind kind
= lvalue_kind (datum
);
1805 /* Compute the type of the field, as described in [expr.ref].
1806 There's no such thing as a mutable pointer-to-member, so
1807 things are not as complex as they are for references to
1808 non-static data members. */
1809 type
= cp_build_qualified_type (type
,
1810 (cp_type_quals (type
)
1811 | cp_type_quals (TREE_TYPE (datum
))));
1813 datum
= build_address (datum
);
1815 /* Convert object to the correct base. */
1818 datum
= build_base_path (PLUS_EXPR
, datum
, binfo
, 1, complain
);
1819 if (datum
== error_mark_node
)
1820 return error_mark_node
;
1823 /* Build an expression for "object + offset" where offset is the
1824 value stored in the pointer-to-data-member. */
1825 ptype
= build_pointer_type (type
);
1826 datum
= fold_build_pointer_plus (fold_convert (ptype
, datum
), component
);
1827 datum
= cp_build_indirect_ref (datum
, RO_NULL
, complain
);
1828 if (datum
== error_mark_node
)
1829 return error_mark_node
;
1831 /* If the object expression was an rvalue, return an rvalue. */
1832 if (kind
& clk_class
)
1833 datum
= rvalue (datum
);
1834 else if (kind
& clk_rvalueref
)
1835 datum
= move (datum
);
1840 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
1841 program is ill-formed if the second operand is a pointer to member
1842 function with ref-qualifier &. In a .* expression whose object
1843 expression is an lvalue, the program is ill-formed if the second
1844 operand is a pointer to member function with ref-qualifier &&. */
1845 if (FUNCTION_REF_QUALIFIED (type
))
1847 bool lval
= real_lvalue_p (datum
);
1848 if (lval
&& FUNCTION_RVALUE_QUALIFIED (type
))
1850 if (complain
& tf_error
)
1851 error ("pointer-to-member-function type %qT requires an rvalue",
1853 return error_mark_node
;
1855 else if (!lval
&& !FUNCTION_RVALUE_QUALIFIED (type
))
1857 if (complain
& tf_error
)
1858 error ("pointer-to-member-function type %qT requires an lvalue",
1860 return error_mark_node
;
1863 return build2 (OFFSET_REF
, type
, datum
, component
);
1867 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1870 build_functional_cast (tree exp
, tree parms
, tsubst_flags_t complain
)
1872 /* This is either a call to a constructor,
1873 or a C cast in C++'s `functional' notation. */
1875 /* The type to which we are casting. */
1877 vec
<tree
, va_gc
> *parmvec
;
1879 if (error_operand_p (exp
) || parms
== error_mark_node
)
1880 return error_mark_node
;
1882 if (TREE_CODE (exp
) == TYPE_DECL
)
1884 type
= TREE_TYPE (exp
);
1886 if (complain
& tf_warning
1887 && TREE_DEPRECATED (type
)
1888 && DECL_ARTIFICIAL (exp
))
1889 warn_deprecated_use (type
, NULL_TREE
);
1894 /* We need to check this explicitly, since value-initialization of
1895 arrays is allowed in other situations. */
1896 if (TREE_CODE (type
) == ARRAY_TYPE
)
1898 if (complain
& tf_error
)
1899 error ("functional cast to array type %qT", type
);
1900 return error_mark_node
;
1903 if (type_uses_auto (type
))
1905 if (complain
& tf_error
)
1906 error ("invalid use of %<auto%>");
1907 return error_mark_node
;
1910 if (processing_template_decl
)
1914 /* Diagnose this even in a template. We could also try harder
1915 to give all the usual errors when the type and args are
1917 if (TREE_CODE (type
) == REFERENCE_TYPE
&& !parms
)
1919 if (complain
& tf_error
)
1920 error ("invalid value-initialization of reference type");
1921 return error_mark_node
;
1924 t
= build_min (CAST_EXPR
, type
, parms
);
1925 /* We don't know if it will or will not have side effects. */
1926 TREE_SIDE_EFFECTS (t
) = 1;
1930 if (! MAYBE_CLASS_TYPE_P (type
))
1932 if (parms
== NULL_TREE
)
1934 if (VOID_TYPE_P (type
))
1936 return build_value_init (cv_unqualified (type
), complain
);
1939 /* This must build a C cast. */
1940 parms
= build_x_compound_expr_from_list (parms
, ELK_FUNC_CAST
, complain
);
1941 return cp_build_c_cast (type
, parms
, complain
);
1944 /* Prepare to evaluate as a call to a constructor. If this expression
1945 is actually used, for example,
1947 return X (arg1, arg2, ...);
1949 then the slot being initialized will be filled in. */
1951 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
1952 return error_mark_node
;
1953 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
1954 return error_mark_node
;
1958 If the expression list is a single-expression, the type
1959 conversion is equivalent (in definedness, and if defined in
1960 meaning) to the corresponding cast expression. */
1961 if (parms
&& TREE_CHAIN (parms
) == NULL_TREE
)
1962 return cp_build_c_cast (type
, TREE_VALUE (parms
), complain
);
1966 The expression T(), where T is a simple-type-specifier for a
1967 non-array complete object type or the (possibly cv-qualified)
1968 void type, creates an rvalue of the specified type, which is
1969 value-initialized. */
1971 if (parms
== NULL_TREE
)
1973 exp
= build_value_init (type
, complain
);
1974 exp
= get_target_expr_sfinae (exp
, complain
);
1978 /* Call the constructor. */
1979 parmvec
= make_tree_vector ();
1980 for (; parms
!= NULL_TREE
; parms
= TREE_CHAIN (parms
))
1981 vec_safe_push (parmvec
, TREE_VALUE (parms
));
1982 exp
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
1983 &parmvec
, type
, LOOKUP_NORMAL
, complain
);
1984 release_tree_vector (parmvec
);
1986 if (exp
== error_mark_node
)
1987 return error_mark_node
;
1989 return build_cplus_new (type
, exp
, complain
);
1993 /* Add new exception specifier SPEC, to the LIST we currently have.
1994 If it's already in LIST then do nothing.
1995 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1996 know what we're doing. */
1999 add_exception_specifier (tree list
, tree spec
, int complain
)
2004 diagnostic_t diag_type
= DK_UNSPECIFIED
; /* none */
2006 if (spec
== error_mark_node
)
2009 gcc_assert (spec
&& (!list
|| TREE_VALUE (list
)));
2011 /* [except.spec] 1, type in an exception specifier shall not be
2012 incomplete, or pointer or ref to incomplete other than pointer
2014 is_ptr
= TYPE_PTR_P (core
);
2015 if (is_ptr
|| TREE_CODE (core
) == REFERENCE_TYPE
)
2016 core
= TREE_TYPE (core
);
2019 else if (VOID_TYPE_P (core
))
2021 else if (TREE_CODE (core
) == TEMPLATE_TYPE_PARM
)
2023 else if (processing_template_decl
)
2028 /* 15.4/1 says that types in an exception specifier must be complete,
2029 but it seems more reasonable to only require this on definitions
2030 and calls. So just give a pedwarn at this point; we will give an
2031 error later if we hit one of those two cases. */
2032 if (!COMPLETE_TYPE_P (complete_type (core
)))
2033 diag_type
= DK_PEDWARN
; /* pedwarn */
2040 for (probe
= list
; probe
; probe
= TREE_CHAIN (probe
))
2041 if (same_type_p (TREE_VALUE (probe
), spec
))
2044 list
= tree_cons (NULL_TREE
, spec
, list
);
2047 diag_type
= DK_ERROR
; /* error */
2049 if (diag_type
!= DK_UNSPECIFIED
2050 && (complain
& tf_warning_or_error
))
2051 cxx_incomplete_type_diagnostic (NULL_TREE
, core
, diag_type
);
2056 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
2059 nothrow_spec_p_uninst (const_tree spec
)
2061 if (DEFERRED_NOEXCEPT_SPEC_P (spec
))
2063 return nothrow_spec_p (spec
);
2066 /* Combine the two exceptions specifier lists LIST and ADD, and return
2070 merge_exception_specifiers (tree list
, tree add
)
2072 tree noex
, orig_list
;
2074 /* No exception-specifier or noexcept(false) are less strict than
2075 anything else. Prefer the newer variant (LIST). */
2076 if (!list
|| list
== noexcept_false_spec
)
2078 else if (!add
|| add
== noexcept_false_spec
)
2081 /* noexcept(true) and throw() are stricter than anything else.
2082 As above, prefer the more recent one (LIST). */
2083 if (nothrow_spec_p_uninst (add
))
2086 /* Two implicit noexcept specs (e.g. on a destructor) are equivalent. */
2087 if (UNEVALUATED_NOEXCEPT_SPEC_P (add
)
2088 && UNEVALUATED_NOEXCEPT_SPEC_P (list
))
2090 /* We should have instantiated other deferred noexcept specs by now. */
2091 gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add
));
2093 if (nothrow_spec_p_uninst (list
))
2095 noex
= TREE_PURPOSE (list
);
2096 gcc_checking_assert (!TREE_PURPOSE (add
)
2097 || cp_tree_equal (noex
, TREE_PURPOSE (add
)));
2099 /* Combine the dynamic-exception-specifiers, if any. */
2101 for (; add
&& TREE_VALUE (add
); add
= TREE_CHAIN (add
))
2103 tree spec
= TREE_VALUE (add
);
2106 for (probe
= orig_list
; probe
&& TREE_VALUE (probe
);
2107 probe
= TREE_CHAIN (probe
))
2108 if (same_type_p (TREE_VALUE (probe
), spec
))
2112 spec
= build_tree_list (NULL_TREE
, spec
);
2113 TREE_CHAIN (spec
) = list
;
2118 /* Keep the noexcept-specifier at the beginning of the list. */
2119 if (noex
!= TREE_PURPOSE (list
))
2120 list
= tree_cons (noex
, TREE_VALUE (list
), TREE_CHAIN (list
));
2125 /* Subroutine of build_call. Ensure that each of the types in the
2126 exception specification is complete. Technically, 15.4/1 says that
2127 they need to be complete when we see a declaration of the function,
2128 but we should be able to get away with only requiring this when the
2129 function is defined or called. See also add_exception_specifier. */
2132 require_complete_eh_spec_types (tree fntype
, tree decl
)
2135 /* Don't complain about calls to op new. */
2136 if (decl
&& DECL_ARTIFICIAL (decl
))
2138 for (raises
= TYPE_RAISES_EXCEPTIONS (fntype
); raises
;
2139 raises
= TREE_CHAIN (raises
))
2141 tree type
= TREE_VALUE (raises
);
2142 if (type
&& !COMPLETE_TYPE_P (type
))
2146 ("call to function %qD which throws incomplete type %q#T",
2149 error ("call to function which throws incomplete type %q#T",
2156 #include "gt-cp-typeck2.h"