1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987-2013 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"
36 #include "diagnostic-core.h"
40 process_init_constructor (tree type
, tree init
, tsubst_flags_t complain
);
43 /* Print an error message stemming from an attempt to use
44 BASETYPE as a base class for TYPE. */
47 error_not_base_type (tree basetype
, tree type
)
49 if (TREE_CODE (basetype
) == FUNCTION_DECL
)
50 basetype
= DECL_CONTEXT (basetype
);
51 error ("type %qT is not a base type for type %qT", basetype
, type
);
52 return error_mark_node
;
56 binfo_or_else (tree base
, tree type
)
58 tree binfo
= lookup_base (type
, base
, ba_unique
,
59 NULL
, tf_warning_or_error
);
61 if (binfo
== error_mark_node
)
64 error_not_base_type (base
, type
);
68 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
69 value may not be changed thereafter. */
72 cxx_readonly_error (tree arg
, enum lvalue_use errstring
)
75 /* This macro is used to emit diagnostics to ensure that all format
76 strings are complete sentences, visible to gettext and checked at
79 #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG) \
100 /* Handle C++-specific things first. */
103 && DECL_LANG_SPECIFIC (arg
)
104 && DECL_IN_AGGR_P (arg
)
105 && !TREE_STATIC (arg
))
106 ERROR_FOR_ASSIGNMENT (G_("assignment of "
107 "constant field %qD"),
108 G_("constant field %qD "
109 "used as %<asm%> output"),
111 "constant field %qD"),
113 "constant field %qD"),
115 else if (INDIRECT_REF_P (arg
)
116 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 0))) == REFERENCE_TYPE
117 && (VAR_P (TREE_OPERAND (arg
, 0))
118 || TREE_CODE (TREE_OPERAND (arg
, 0)) == PARM_DECL
))
119 ERROR_FOR_ASSIGNMENT (G_("assignment of "
120 "read-only reference %qD"),
121 G_("read-only reference %qD "
122 "used as %<asm%> output"),
124 "read-only reference %qD"),
126 "read-only reference %qD"),
127 TREE_OPERAND (arg
, 0));
129 readonly_error (arg
, errstring
);
133 /* Structure that holds information about declarations whose type was
134 incomplete and we could not check whether it was abstract or not. */
136 struct GTY((chain_next ("%h.next"))) pending_abstract_type
{
137 /* Declaration which we are checking for abstractness. It is either
138 a DECL node, or an IDENTIFIER_NODE if we do not have a full
139 declaration available. */
142 /* Type which will be checked for abstractness. */
145 /* Kind of use in an unnamed declarator. */
146 abstract_class_use use
;
148 /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
149 because DECLs already carry locus information. */
152 /* Link to the next element in list. */
153 struct pending_abstract_type
* next
;
157 /* Compute the hash value of the node VAL. This function is used by the
158 hash table abstract_pending_vars. */
161 pat_calc_hash (const void* val
)
163 const struct pending_abstract_type
*pat
=
164 (const struct pending_abstract_type
*) val
;
165 return (hashval_t
) TYPE_UID (pat
->type
);
169 /* Compare node VAL1 with the type VAL2. This function is used by the
170 hash table abstract_pending_vars. */
173 pat_compare (const void* val1
, const void* val2
)
175 const struct pending_abstract_type
*const pat1
=
176 (const struct pending_abstract_type
*) val1
;
177 const_tree
const type2
= (const_tree
)val2
;
179 return (pat1
->type
== type2
);
182 /* Hash table that maintains pending_abstract_type nodes, for which we still
183 need to check for type abstractness. The key of the table is the type
184 of the declaration. */
185 static GTY ((param_is (struct pending_abstract_type
)))
186 htab_t abstract_pending_vars
= NULL
;
188 static int abstract_virtuals_error_sfinae (tree
, tree
, abstract_class_use
, tsubst_flags_t
);
190 /* This function is called after TYPE is completed, and will check if there
191 are pending declarations for which we still need to verify the abstractness
192 of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
193 turned out to be incomplete. */
196 complete_type_check_abstract (tree type
)
199 struct pending_abstract_type
*pat
;
200 location_t cur_loc
= input_location
;
202 gcc_assert (COMPLETE_TYPE_P (type
));
204 if (!abstract_pending_vars
)
207 /* Retrieve the list of pending declarations for this type. */
208 slot
= htab_find_slot_with_hash (abstract_pending_vars
, type
,
209 (hashval_t
)TYPE_UID (type
), NO_INSERT
);
212 pat
= (struct pending_abstract_type
*)*slot
;
215 /* If the type is not abstract, do not do anything. */
216 if (CLASSTYPE_PURE_VIRTUALS (type
))
218 struct pending_abstract_type
*prev
= 0, *next
;
220 /* Reverse the list to emit the errors in top-down order. */
221 for (; pat
; pat
= next
)
229 /* Go through the list, and call abstract_virtuals_error for each
230 element: it will issue a diagnostic if the type is abstract. */
233 gcc_assert (type
== pat
->type
);
235 /* Tweak input_location so that the diagnostic appears at the correct
236 location. Notice that this is only needed if the decl is an
238 input_location
= pat
->locus
;
239 abstract_virtuals_error_sfinae (pat
->decl
, pat
->type
, pat
->use
,
240 tf_warning_or_error
);
245 htab_clear_slot (abstract_pending_vars
, slot
);
247 input_location
= cur_loc
;
251 /* If TYPE has abstract virtual functions, issue an error about trying
252 to create an object of that type. DECL is the object declared, or
253 NULL_TREE if the declaration is unavailable, in which case USE specifies
254 the kind of invalid use. Returns 1 if an error occurred; zero if
258 abstract_virtuals_error_sfinae (tree decl
, tree type
, abstract_class_use use
,
259 tsubst_flags_t complain
)
261 vec
<tree
, va_gc
> *pure
;
263 /* This function applies only to classes. Any other entity can never
265 if (!CLASS_TYPE_P (type
))
267 type
= TYPE_MAIN_VARIANT (type
);
270 /* Instantiation here seems to be required by the standard,
271 but breaks e.g. boost::bind. FIXME! */
272 /* In SFINAE, non-N3276 context, force instantiation. */
273 if (!(complain
& (tf_error
|tf_decltype
)))
274 complete_type (type
);
277 /* If the type is incomplete, we register it within a hash table,
278 so that we can check again once it is completed. This makes sense
279 only for objects for which we have a declaration or at least a
281 if (!COMPLETE_TYPE_P (type
) && (complain
& tf_error
))
284 struct pending_abstract_type
*pat
;
286 gcc_assert (!decl
|| DECL_P (decl
) || identifier_p (decl
));
288 if (!abstract_pending_vars
)
289 abstract_pending_vars
= htab_create_ggc (31, &pat_calc_hash
,
292 slot
= htab_find_slot_with_hash (abstract_pending_vars
, type
,
293 (hashval_t
)TYPE_UID (type
), INSERT
);
295 pat
= ggc_alloc_pending_abstract_type ();
299 pat
->locus
= ((decl
&& DECL_P (decl
))
300 ? DECL_SOURCE_LOCATION (decl
)
303 pat
->next
= (struct pending_abstract_type
*) *slot
;
309 if (!TYPE_SIZE (type
))
310 /* TYPE is being defined, and during that time
311 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
314 pure
= CLASSTYPE_PURE_VIRTUALS (type
);
318 if (!(complain
& tf_error
))
324 error ("cannot declare variable %q+D to be of abstract "
325 "type %qT", decl
, type
);
326 else if (TREE_CODE (decl
) == PARM_DECL
)
328 if (DECL_NAME (decl
))
329 error ("cannot declare parameter %q+D to be of abstract type %qT",
332 error ("cannot declare parameter to be of abstract type %qT",
335 else if (TREE_CODE (decl
) == FIELD_DECL
)
336 error ("cannot declare field %q+D to be of abstract type %qT",
338 else if (TREE_CODE (decl
) == FUNCTION_DECL
339 && TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
340 error ("invalid abstract return type for member function %q+#D", decl
);
341 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
342 error ("invalid abstract return type for function %q+#D", decl
);
343 else if (identifier_p (decl
))
344 /* Here we do not have location information. */
345 error ("invalid abstract type %qT for %qE", type
, decl
);
347 error ("invalid abstract type for %q+D", decl
);
352 error ("creating array of %qT, which is an abstract class type", type
);
355 error ("invalid cast to abstract class type %qT", type
);
358 error ("invalid new-expression of abstract class type %qT", type
);
361 error ("invalid abstract return type %qT", type
);
364 error ("invalid abstract parameter type %qT", type
);
367 error ("expression of abstract class type %qT cannot "
368 "be used in throw-expression", type
);
371 error ("cannot declare catch parameter to be of abstract "
372 "class type %qT", type
);
375 error ("cannot allocate an object of abstract type %qT", type
);
378 /* Only go through this once. */
384 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
385 " because the following virtual functions are pure within %qT:",
388 FOR_EACH_VEC_ELT (*pure
, ix
, fn
)
389 if (! DECL_CLONED_FUNCTION_P (fn
)
390 || DECL_COMPLETE_DESTRUCTOR_P (fn
))
391 inform (input_location
, "\t%+#D", fn
);
393 /* Now truncate the vector. This leaves it non-null, so we know
394 there are pure virtuals, but empty so we don't list them out
403 abstract_virtuals_error_sfinae (tree decl
, tree type
, tsubst_flags_t complain
)
405 return abstract_virtuals_error_sfinae (decl
, type
, ACU_UNKNOWN
, complain
);
409 abstract_virtuals_error_sfinae (abstract_class_use use
, tree type
,
410 tsubst_flags_t complain
)
412 return abstract_virtuals_error_sfinae (NULL_TREE
, type
, use
, complain
);
416 /* Wrapper for the above function in the common case of wanting errors. */
419 abstract_virtuals_error (tree decl
, tree type
)
421 return abstract_virtuals_error_sfinae (decl
, type
, tf_warning_or_error
);
425 abstract_virtuals_error (abstract_class_use use
, tree type
)
427 return abstract_virtuals_error_sfinae (use
, type
, tf_warning_or_error
);
430 /* Print an error message for invalid use of an incomplete type.
431 VALUE is the expression that was used (or 0 if that isn't known)
432 and TYPE is the type that was invalid. DIAG_KIND indicates the
433 type of diagnostic (see diagnostic.def). */
436 cxx_incomplete_type_diagnostic (const_tree value
, const_tree type
,
437 diagnostic_t diag_kind
)
441 gcc_assert (diag_kind
== DK_WARNING
442 || diag_kind
== DK_PEDWARN
443 || diag_kind
== DK_ERROR
);
445 /* Avoid duplicate error message. */
446 if (TREE_CODE (type
) == ERROR_MARK
)
449 if (value
!= 0 && (VAR_P (value
)
450 || TREE_CODE (value
) == PARM_DECL
451 || TREE_CODE (value
) == FIELD_DECL
))
453 emit_diagnostic (diag_kind
, input_location
, 0,
454 "%q+D has incomplete type", value
);
458 /* We must print an error message. Be clever about what it says. */
460 switch (TREE_CODE (type
))
466 emit_diagnostic (diag_kind
, input_location
, 0,
467 "invalid use of incomplete type %q#T", type
);
468 if (!TYPE_TEMPLATE_INFO (type
))
469 emit_diagnostic (diag_kind
, input_location
, 0,
470 "forward declaration of %q+#T", type
);
472 emit_diagnostic (diag_kind
, input_location
, 0,
473 "declaration of %q+#T", type
);
477 emit_diagnostic (diag_kind
, input_location
, 0,
478 "invalid use of %qT", type
);
482 if (TYPE_DOMAIN (type
))
484 type
= TREE_TYPE (type
);
487 emit_diagnostic (diag_kind
, input_location
, 0,
488 "invalid use of array with unspecified bounds");
494 tree member
= TREE_OPERAND (value
, 1);
495 if (is_overloaded_fn (member
))
496 member
= get_first_fn (member
);
497 if (DECL_FUNCTION_MEMBER_P (member
)
498 && ! flag_ms_extensions
)
499 emit_diagnostic (diag_kind
, input_location
, 0,
500 "invalid use of member function "
501 "(did you forget the %<()%> ?)");
503 emit_diagnostic (diag_kind
, input_location
, 0,
504 "invalid use of member "
505 "(did you forget the %<&%> ?)");
509 case TEMPLATE_TYPE_PARM
:
511 emit_diagnostic (diag_kind
, input_location
, 0,
512 "invalid use of %<auto%>");
514 emit_diagnostic (diag_kind
, input_location
, 0,
515 "invalid use of template type parameter %qT", type
);
518 case BOUND_TEMPLATE_TEMPLATE_PARM
:
519 emit_diagnostic (diag_kind
, input_location
, 0,
520 "invalid use of template template parameter %qT",
525 emit_diagnostic (diag_kind
, input_location
, 0,
526 "invalid use of dependent type %qT", type
);
530 if (type
== init_list_type_node
)
532 emit_diagnostic (diag_kind
, input_location
, 0,
533 "invalid use of brace-enclosed initializer list");
536 gcc_assert (type
== unknown_type_node
);
537 if (value
&& TREE_CODE (value
) == COMPONENT_REF
)
539 else if (value
&& TREE_CODE (value
) == ADDR_EXPR
)
540 emit_diagnostic (diag_kind
, input_location
, 0,
541 "address of overloaded function with no contextual "
543 else if (value
&& TREE_CODE (value
) == OVERLOAD
)
544 emit_diagnostic (diag_kind
, input_location
, 0,
545 "overloaded function with no contextual type information");
547 emit_diagnostic (diag_kind
, input_location
, 0,
548 "insufficient contextual information to determine type");
556 /* Backward-compatibility interface to incomplete_type_diagnostic;
557 required by ../tree.c. */
558 #undef cxx_incomplete_type_error
560 cxx_incomplete_type_error (const_tree value
, const_tree type
)
562 cxx_incomplete_type_diagnostic (value
, type
, DK_ERROR
);
566 /* The recursive part of split_nonconstant_init. DEST is an lvalue
567 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
568 Return true if the whole of the value was initialized by the
569 generated statements. */
572 split_nonconstant_init_1 (tree dest
, tree init
)
574 unsigned HOST_WIDE_INT idx
;
575 tree field_index
, value
;
576 tree type
= TREE_TYPE (dest
);
577 tree inner_type
= NULL
;
578 bool array_type_p
= false;
579 bool complete_p
= true;
580 HOST_WIDE_INT num_split_elts
= 0;
582 switch (TREE_CODE (type
))
585 inner_type
= TREE_TYPE (type
);
591 case QUAL_UNION_TYPE
:
592 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init
), idx
,
595 /* The current implementation of this algorithm assumes that
596 the field was set for all the elements. This is usually done
597 by process_init_constructor. */
598 gcc_assert (field_index
);
601 inner_type
= TREE_TYPE (field_index
);
603 if (TREE_CODE (value
) == CONSTRUCTOR
)
608 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
609 NULL_TREE
, NULL_TREE
);
611 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
614 if (!split_nonconstant_init_1 (sub
, value
))
618 else if (!initializer_constant_valid_p (value
, inner_type
))
623 /* FIXME: Ordered removal is O(1) so the whole function is
624 worst-case quadratic. This could be fixed using an aside
625 bitmap to record which elements must be removed and remove
626 them all at the same time. Or by merging
627 split_non_constant_init into process_init_constructor_array,
628 that is separating constants from non-constants while building
630 CONSTRUCTOR_ELTS (init
)->ordered_remove (idx
);
634 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
635 NULL_TREE
, NULL_TREE
);
637 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
640 code
= build2 (INIT_EXPR
, inner_type
, sub
, value
);
641 code
= build_stmt (input_location
, EXPR_STMT
, code
);
642 code
= maybe_cleanup_point_expr_void (code
);
644 if (type_build_dtor_call (inner_type
))
646 code
= (build_special_member_call
647 (sub
, complete_dtor_identifier
, NULL
, inner_type
,
648 LOOKUP_NORMAL
, tf_warning_or_error
));
649 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (inner_type
))
650 finish_eh_cleanup (code
);
659 if (!initializer_constant_valid_p (init
, type
))
662 tree cons
= copy_node (init
);
663 CONSTRUCTOR_ELTS (init
) = NULL
;
664 code
= build2 (MODIFY_EXPR
, type
, dest
, cons
);
665 code
= build_stmt (input_location
, EXPR_STMT
, code
);
667 num_split_elts
+= CONSTRUCTOR_NELTS (init
);
675 /* The rest of the initializer is now a constant. */
676 TREE_CONSTANT (init
) = 1;
677 return complete_p
&& complete_ctor_at_level_p (TREE_TYPE (init
),
678 num_split_elts
, inner_type
);
681 /* A subroutine of store_init_value. Splits non-constant static
682 initializer INIT into a constant part and generates code to
683 perform the non-constant part of the initialization to DEST.
684 Returns the code for the runtime init. */
687 split_nonconstant_init (tree dest
, tree init
)
691 if (TREE_CODE (init
) == CONSTRUCTOR
)
693 code
= push_stmt_list ();
694 if (split_nonconstant_init_1 (dest
, init
))
696 code
= pop_stmt_list (code
);
697 DECL_INITIAL (dest
) = init
;
698 TREE_READONLY (dest
) = 0;
701 code
= build2 (INIT_EXPR
, TREE_TYPE (dest
), dest
, init
);
706 /* Perform appropriate conversions on the initial value of a variable,
707 store it in the declaration DECL,
708 and print any error messages that are appropriate.
709 If the init is invalid, store an ERROR_MARK.
711 C++: Note that INIT might be a TREE_LIST, which would mean that it is
712 a base class initializer for some aggregate type, hopefully compatible
713 with DECL. If INIT is a single element, and DECL is an aggregate
714 type, we silently convert INIT into a TREE_LIST, allowing a constructor
717 If INIT is a TREE_LIST and there is no constructor, turn INIT
718 into a CONSTRUCTOR and use standard initialization techniques.
719 Perhaps a warning should be generated?
721 Returns code to be executed if initialization could not be performed
722 for static variable. In that case, caller must emit the code. */
725 store_init_value (tree decl
, tree init
, vec
<tree
, va_gc
>** cleanups
, int flags
)
729 /* If variable's type was invalidly declared, just ignore it. */
731 type
= TREE_TYPE (decl
);
732 if (TREE_CODE (type
) == ERROR_MARK
)
735 if (MAYBE_CLASS_TYPE_P (type
))
737 if (TREE_CODE (init
) == TREE_LIST
)
739 error ("constructor syntax used, but no constructor declared "
740 "for type %qT", type
);
741 init
= build_constructor_from_list (init_list_type_node
, nreverse (init
));
744 else if (TREE_CODE (init
) == TREE_LIST
745 && TREE_TYPE (init
) != unknown_type_node
)
747 gcc_assert (TREE_CODE (decl
) != RESULT_DECL
);
749 if (TREE_CODE (init
) == TREE_LIST
750 && TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
752 error ("cannot initialize arrays using this syntax");
756 /* We get here with code like `int a (2);' */
757 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
758 tf_warning_or_error
);
761 /* End of special C++ code. */
763 if (flags
& LOOKUP_ALREADY_DIGESTED
)
766 /* Digest the specified initializer into an expression. */
767 value
= digest_init_flags (type
, init
, flags
);
769 value
= extend_ref_init_temps (decl
, value
, cleanups
);
771 /* In C++0x constant expression is a semantic, not syntactic, property.
772 In C++98, make sure that what we thought was a constant expression at
773 template definition time is still constant and otherwise perform this
774 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
775 if (decl_maybe_constant_var_p (decl
) || TREE_STATIC (decl
))
778 value
= fold_non_dependent_expr (value
);
779 if (DECL_DECLARED_CONSTEXPR_P (decl
)
780 || DECL_IN_AGGR_P (decl
))
782 /* Diagnose a non-constant initializer for constexpr. */
783 if (processing_template_decl
784 && !require_potential_constant_expression (value
))
785 value
= error_mark_node
;
787 value
= cxx_constant_value (value
);
789 value
= maybe_constant_init (value
);
790 const_init
= (reduced_constant_expression_p (value
)
791 || error_operand_p (value
));
792 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = const_init
;
793 TREE_CONSTANT (decl
) = const_init
&& decl_maybe_constant_var_p (decl
);
796 /* If the initializer is not a constant, fill in DECL_INITIAL with
797 the bits that are constant, and then return an expression that
798 will perform the dynamic initialization. */
799 if (value
!= error_mark_node
800 && (TREE_SIDE_EFFECTS (value
)
801 || array_of_runtime_bound_p (type
)
802 || ! reduced_constant_expression_p (value
)))
804 if (TREE_CODE (type
) == ARRAY_TYPE
805 && (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type
))
806 || array_of_runtime_bound_p (type
)))
807 /* For an array, we only need/want a single cleanup region rather
808 than one per element. */
809 return build_vec_init (decl
, NULL_TREE
, value
, false, 1,
810 tf_warning_or_error
);
812 return split_nonconstant_init (decl
, value
);
814 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
815 is an automatic variable, the middle end will turn this into a
816 dynamic initialization later. */
817 DECL_INITIAL (decl
) = value
;
822 /* Give errors about narrowing conversions within { }. */
825 check_narrowing (tree type
, tree init
)
827 tree ftype
= unlowered_expr_type (init
);
831 if (!warn_narrowing
|| !ARITHMETIC_TYPE_P (type
))
834 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
835 && TREE_CODE (type
) == COMPLEX_TYPE
)
837 tree elttype
= TREE_TYPE (type
);
838 if (CONSTRUCTOR_NELTS (init
) > 0)
839 check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 0)->value
);
840 if (CONSTRUCTOR_NELTS (init
) > 1)
841 check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 1)->value
);
845 init
= maybe_constant_value (init
);
847 if (TREE_CODE (type
) == INTEGER_TYPE
848 && TREE_CODE (ftype
) == REAL_TYPE
)
850 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
851 && CP_INTEGRAL_TYPE_P (type
))
853 if (TREE_CODE (ftype
) == ENUMERAL_TYPE
)
854 /* Check for narrowing based on the values of the enumeration. */
855 ftype
= ENUM_UNDERLYING_TYPE (ftype
);
856 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type
),
857 TYPE_MAX_VALUE (ftype
))
858 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype
),
859 TYPE_MIN_VALUE (type
)))
860 && (TREE_CODE (init
) != INTEGER_CST
861 || !int_fits_type_p (init
, type
)))
864 else if (TREE_CODE (ftype
) == REAL_TYPE
865 && TREE_CODE (type
) == REAL_TYPE
)
867 if (TYPE_PRECISION (type
) < TYPE_PRECISION (ftype
))
869 if (TREE_CODE (init
) == REAL_CST
)
871 /* Issue 703: Loss of precision is OK as long as the value is
872 within the representable range of the new type. */
874 d
= TREE_REAL_CST (init
);
875 real_convert (&r
, TYPE_MODE (type
), &d
);
883 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
884 && TREE_CODE (type
) == REAL_TYPE
)
887 if (TREE_CODE (init
) == INTEGER_CST
)
889 d
= real_value_from_int_cst (0, init
);
890 if (exact_real_truncate (TYPE_MODE (type
), &d
))
897 if (cxx_dialect
>= cxx11
)
898 pedwarn (EXPR_LOC_OR_HERE (init
), OPT_Wnarrowing
,
899 "narrowing conversion of %qE from %qT to %qT inside { }",
902 warning_at (EXPR_LOC_OR_HERE (init
), OPT_Wnarrowing
,
903 "narrowing conversion of %qE from %qT to %qT inside { } "
904 "is ill-formed in C++11", init
, ftype
, type
);
908 /* Process the initializer INIT for a variable of type TYPE, emitting
909 diagnostics for invalid initializers and converting the initializer as
912 For aggregate types, it assumes that reshape_init has already run, thus the
913 initializer will have the right shape (brace elision has been undone).
915 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
918 digest_init_r (tree type
, tree init
, bool nested
, int flags
,
919 tsubst_flags_t complain
)
921 enum tree_code code
= TREE_CODE (type
);
923 if (error_operand_p (init
))
924 return error_mark_node
;
928 /* We must strip the outermost array type when completing the type,
929 because the its bounds might be incomplete at the moment. */
930 if (!complete_type_or_maybe_complain (TREE_CODE (type
) == ARRAY_TYPE
931 ? TREE_TYPE (type
) : type
, NULL_TREE
,
933 return error_mark_node
;
935 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
936 (g++.old-deja/g++.law/casts2.C). */
937 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
938 init
= TREE_OPERAND (init
, 0);
940 /* Initialization of an array of chars from a string constant. The initializer
941 can be optionally enclosed in braces, but reshape_init has already removed
942 them if they were present. */
943 if (code
== ARRAY_TYPE
)
945 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
946 if (char_type_p (typ1
)
948 && TREE_CODE (init
) == STRING_CST
)
950 tree char_type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init
)));
952 if (TYPE_PRECISION (typ1
) == BITS_PER_UNIT
)
954 if (char_type
!= char_type_node
)
956 if (complain
& tf_error
)
957 error ("char-array initialized from wide string");
958 return error_mark_node
;
963 if (char_type
== char_type_node
)
965 if (complain
& tf_error
)
966 error ("int-array initialized from non-wide string");
967 return error_mark_node
;
969 else if (char_type
!= typ1
)
971 if (complain
& tf_error
)
972 error ("int-array initialized from incompatible "
974 return error_mark_node
;
978 if (type
!= TREE_TYPE (init
))
980 init
= copy_node (init
);
981 TREE_TYPE (init
) = type
;
983 if (TYPE_DOMAIN (type
) != 0 && TREE_CONSTANT (TYPE_SIZE (type
)))
985 int size
= tree_to_hwi (TYPE_SIZE (type
));
986 size
= (size
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
;
987 /* In C it is ok to subtract 1 from the length of the string
988 because it's ok to ignore the terminating null char that is
989 counted in the length of the constant, but in C++ this would
991 if (size
< TREE_STRING_LENGTH (init
))
992 permerror (input_location
, "initializer-string for array "
993 "of chars is too long");
999 /* Handle scalar types (including conversions) and references. */
1000 if ((TREE_CODE (type
) != COMPLEX_TYPE
1001 || BRACE_ENCLOSED_INITIALIZER_P (init
))
1002 && (SCALAR_TYPE_P (type
) || code
== REFERENCE_TYPE
))
1007 check_narrowing (type
, init
);
1008 init
= convert_for_initialization (0, type
, init
, flags
,
1009 ICR_INIT
, NULL_TREE
, 0,
1013 /* Skip any conversions since we'll be outputting the underlying
1015 while (CONVERT_EXPR_P (*exp
)
1016 || TREE_CODE (*exp
) == NON_LVALUE_EXPR
)
1017 exp
= &TREE_OPERAND (*exp
, 0);
1019 *exp
= cplus_expand_constant (*exp
);
1024 /* Come here only for aggregates: records, arrays, unions, complex numbers
1026 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
1027 || TREE_CODE (type
) == VECTOR_TYPE
1028 || TREE_CODE (type
) == RECORD_TYPE
1029 || TREE_CODE (type
) == UNION_TYPE
1030 || TREE_CODE (type
) == COMPLEX_TYPE
);
1032 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
1033 && !TYPE_NON_AGGREGATE_CLASS (type
))
1034 return process_init_constructor (type
, init
, complain
);
1037 if (COMPOUND_LITERAL_P (init
) && TREE_CODE (type
) == ARRAY_TYPE
)
1039 if (complain
& tf_error
)
1040 error ("cannot initialize aggregate of type %qT with "
1041 "a compound literal", type
);
1043 return error_mark_node
;
1046 if (TREE_CODE (type
) == ARRAY_TYPE
1047 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
1049 /* Allow the result of build_array_copy and of
1050 build_value_init_noctor. */
1051 if ((TREE_CODE (init
) == VEC_INIT_EXPR
1052 || TREE_CODE (init
) == CONSTRUCTOR
)
1053 && (same_type_ignoring_top_level_qualifiers_p
1054 (type
, TREE_TYPE (init
))))
1057 if (complain
& tf_error
)
1058 error ("array must be initialized with a brace-enclosed"
1060 return error_mark_node
;
1063 return convert_for_initialization (NULL_TREE
, type
, init
,
1065 ICR_INIT
, NULL_TREE
, 0,
1071 digest_init (tree type
, tree init
, tsubst_flags_t complain
)
1073 return digest_init_r (type
, init
, false, LOOKUP_IMPLICIT
, complain
);
1077 digest_init_flags (tree type
, tree init
, int flags
)
1079 return digest_init_r (type
, init
, false, flags
, tf_warning_or_error
);
1082 /* Set of flags used within process_init_constructor to describe the
1084 #define PICFLAG_ERRONEOUS 1
1085 #define PICFLAG_NOT_ALL_CONSTANT 2
1086 #define PICFLAG_NOT_ALL_SIMPLE 4
1088 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1092 picflag_from_initializer (tree init
)
1094 if (init
== error_mark_node
)
1095 return PICFLAG_ERRONEOUS
;
1096 else if (!TREE_CONSTANT (init
))
1097 return PICFLAG_NOT_ALL_CONSTANT
;
1098 else if (!initializer_constant_valid_p (init
, TREE_TYPE (init
)))
1099 return PICFLAG_NOT_ALL_SIMPLE
;
1103 /* Subroutine of process_init_constructor, which will process an initializer
1104 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1105 which describe the initializers. */
1108 process_init_constructor_array (tree type
, tree init
,
1109 tsubst_flags_t complain
)
1111 unsigned HOST_WIDE_INT i
, len
= 0;
1113 bool unbounded
= false;
1114 constructor_elt
*ce
;
1115 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (init
);
1117 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
1118 || TREE_CODE (type
) == VECTOR_TYPE
);
1120 if (TREE_CODE (type
) == ARRAY_TYPE
)
1122 tree domain
= TYPE_DOMAIN (type
);
1123 if (domain
&& TREE_CONSTANT (TYPE_MAX_VALUE (domain
)))
1124 len
= wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain
))
1125 - wi::to_offset (TYPE_MIN_VALUE (domain
)) + 1,
1126 TYPE_PRECISION (TREE_TYPE (domain
)),
1127 TYPE_SIGN (TREE_TYPE (domain
))).to_uhwi ();
1129 unbounded
= true; /* Take as many as there are. */
1132 /* Vectors are like simple fixed-size arrays. */
1133 len
= TYPE_VECTOR_SUBPARTS (type
);
1135 /* There must not be more initializers than needed. */
1136 if (!unbounded
&& vec_safe_length (v
) > len
)
1138 if (complain
& tf_error
)
1139 error ("too many initializers for %qT", type
);
1141 return PICFLAG_ERRONEOUS
;
1144 FOR_EACH_VEC_SAFE_ELT (v
, i
, ce
)
1148 gcc_assert (TREE_CODE (ce
->index
) == INTEGER_CST
);
1149 if (compare_tree_int (ce
->index
, i
) != 0)
1151 ce
->value
= error_mark_node
;
1152 sorry ("non-trivial designated initializers not supported");
1156 ce
->index
= size_int (i
);
1157 gcc_assert (ce
->value
);
1158 ce
->value
= digest_init_r (TREE_TYPE (type
), ce
->value
, true,
1159 LOOKUP_IMPLICIT
, complain
);
1161 if (ce
->value
!= error_mark_node
)
1162 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1163 (TREE_TYPE (type
), TREE_TYPE (ce
->value
)));
1165 flags
|= picflag_from_initializer (ce
->value
);
1168 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1169 we must add initializers ourselves. */
1171 for (; i
< len
; ++i
)
1175 if (type_build_ctor_call (TREE_TYPE (type
)))
1177 /* If this type needs constructors run for default-initialization,
1178 we can't rely on the back end to do it for us, so make the
1179 initialization explicit by list-initializing from {}. */
1180 next
= build_constructor (init_list_type_node
, NULL
);
1181 next
= digest_init (TREE_TYPE (type
), next
, complain
);
1183 else if (!zero_init_p (TREE_TYPE (type
)))
1184 next
= build_zero_init (TREE_TYPE (type
),
1185 /*nelts=*/NULL_TREE
,
1186 /*static_storage_p=*/false);
1188 /* The default zero-initialization is fine for us; don't
1189 add anything to the CONSTRUCTOR. */
1192 flags
|= picflag_from_initializer (next
);
1193 CONSTRUCTOR_APPEND_ELT (v
, size_int (i
), next
);
1196 CONSTRUCTOR_ELTS (init
) = v
;
1200 /* Subroutine of process_init_constructor, which will process an initializer
1201 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1202 the initializers. */
1205 process_init_constructor_record (tree type
, tree init
,
1206 tsubst_flags_t complain
)
1208 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1211 unsigned HOST_WIDE_INT idx
= 0;
1213 gcc_assert (TREE_CODE (type
) == RECORD_TYPE
);
1214 gcc_assert (!CLASSTYPE_VBASECLASSES (type
));
1215 gcc_assert (!TYPE_BINFO (type
)
1216 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)));
1217 gcc_assert (!TYPE_POLYMORPHIC_P (type
));
1219 /* Generally, we will always have an index for each initializer (which is
1220 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1221 reshape_init. So we need to handle both cases. */
1222 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1227 if (!DECL_NAME (field
) && DECL_C_BIT_FIELD (field
))
1229 flags
|= picflag_from_initializer (integer_zero_node
);
1230 CONSTRUCTOR_APPEND_ELT (v
, field
, integer_zero_node
);
1234 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
1237 /* If this is a bitfield, first convert to the declared type. */
1238 type
= TREE_TYPE (field
);
1239 if (DECL_BIT_FIELD_TYPE (field
))
1240 type
= DECL_BIT_FIELD_TYPE (field
);
1241 if (type
== error_mark_node
)
1242 return PICFLAG_ERRONEOUS
;
1244 if (idx
< vec_safe_length (CONSTRUCTOR_ELTS (init
)))
1246 constructor_elt
*ce
= &(*CONSTRUCTOR_ELTS (init
))[idx
];
1249 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1250 latter case can happen in templates where lookup has to be
1252 gcc_assert (TREE_CODE (ce
->index
) == FIELD_DECL
1253 || identifier_p (ce
->index
));
1254 if (ce
->index
!= field
1255 && ce
->index
!= DECL_NAME (field
))
1257 ce
->value
= error_mark_node
;
1258 sorry ("non-trivial designated initializers not supported");
1262 gcc_assert (ce
->value
);
1263 next
= digest_init_r (type
, ce
->value
, true,
1264 LOOKUP_IMPLICIT
, complain
);
1267 else if (type_build_ctor_call (TREE_TYPE (field
)))
1269 /* If this type needs constructors run for
1270 default-initialization, we can't rely on the back end to do it
1271 for us, so build up TARGET_EXPRs. If the type in question is
1272 a class, just build one up; if it's an array, recurse. */
1273 next
= build_constructor (init_list_type_node
, NULL
);
1274 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (field
)))
1276 next
= finish_compound_literal (TREE_TYPE (field
), next
,
1278 /* direct-initialize the target. No temporary is going
1280 if (TREE_CODE (next
) == TARGET_EXPR
)
1281 TARGET_EXPR_DIRECT_INIT_P (next
) = true;
1284 next
= digest_init_r (TREE_TYPE (field
), next
, true,
1285 LOOKUP_IMPLICIT
, complain
);
1287 /* Warn when some struct elements are implicitly initialized. */
1288 warning (OPT_Wmissing_field_initializers
,
1289 "missing initializer for member %qD", field
);
1293 if (TREE_READONLY (field
))
1295 if (complain
& tf_error
)
1296 error ("uninitialized const member %qD", field
);
1298 return PICFLAG_ERRONEOUS
;
1300 else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field
)))
1302 if (complain
& tf_error
)
1303 error ("member %qD with uninitialized const fields", field
);
1305 return PICFLAG_ERRONEOUS
;
1307 else if (TREE_CODE (TREE_TYPE (field
)) == REFERENCE_TYPE
)
1309 if (complain
& tf_error
)
1310 error ("member %qD is uninitialized reference", field
);
1312 return PICFLAG_ERRONEOUS
;
1315 /* Warn when some struct elements are implicitly initialized
1317 warning (OPT_Wmissing_field_initializers
,
1318 "missing initializer for member %qD", field
);
1320 if (!zero_init_p (TREE_TYPE (field
)))
1321 next
= build_zero_init (TREE_TYPE (field
), /*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. */
1329 /* If this is a bitfield, now convert to the lowered type. */
1330 if (type
!= TREE_TYPE (field
))
1331 next
= cp_convert_and_check (TREE_TYPE (field
), next
, complain
);
1332 flags
|= picflag_from_initializer (next
);
1333 CONSTRUCTOR_APPEND_ELT (v
, field
, next
);
1336 if (idx
< vec_safe_length (CONSTRUCTOR_ELTS (init
)))
1338 if (complain
& tf_error
)
1339 error ("too many initializers for %qT", type
);
1341 return PICFLAG_ERRONEOUS
;
1344 CONSTRUCTOR_ELTS (init
) = v
;
1348 /* Subroutine of process_init_constructor, which will process a single
1349 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1350 which describe the initializer. */
1353 process_init_constructor_union (tree type
, tree init
,
1354 tsubst_flags_t complain
)
1356 constructor_elt
*ce
;
1359 /* If the initializer was empty, use default zero initialization. */
1360 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
1363 len
= CONSTRUCTOR_ELTS (init
)->length ();
1366 if (!(complain
& tf_error
))
1367 return PICFLAG_ERRONEOUS
;
1368 error ("too many initializers for %qT", type
);
1369 CONSTRUCTOR_ELTS (init
)->block_remove (1, len
-1);
1372 ce
= &(*CONSTRUCTOR_ELTS (init
))[0];
1374 /* If this element specifies a field, initialize via that field. */
1377 if (TREE_CODE (ce
->index
) == FIELD_DECL
)
1379 else if (identifier_p (ce
->index
))
1381 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1382 tree name
= ce
->index
;
1384 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1385 if (DECL_NAME (field
) == name
)
1389 if (complain
& tf_error
)
1390 error ("no field %qD found in union being initialized",
1392 ce
->value
= error_mark_node
;
1398 gcc_assert (TREE_CODE (ce
->index
) == INTEGER_CST
1399 || TREE_CODE (ce
->index
) == RANGE_EXPR
);
1400 if (complain
& tf_error
)
1401 error ("index value instead of field name in union initializer");
1402 ce
->value
= error_mark_node
;
1407 /* Find the first named field. ANSI decided in September 1990
1408 that only named fields count here. */
1409 tree field
= TYPE_FIELDS (type
);
1410 while (field
&& (!DECL_NAME (field
) || TREE_CODE (field
) != FIELD_DECL
))
1411 field
= TREE_CHAIN (field
);
1412 if (field
== NULL_TREE
)
1414 if (complain
& tf_error
)
1415 error ("too many initializers for %qT", type
);
1416 ce
->value
= error_mark_node
;
1421 if (ce
->value
&& ce
->value
!= error_mark_node
)
1422 ce
->value
= digest_init_r (TREE_TYPE (ce
->index
), ce
->value
,
1423 true, LOOKUP_IMPLICIT
, complain
);
1425 return picflag_from_initializer (ce
->value
);
1428 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1429 constructor is a brace-enclosed initializer, and will be modified in-place.
1431 Each element is converted to the right type through digest_init, and
1432 missing initializers are added following the language rules (zero-padding,
1435 After the execution, the initializer will have TREE_CONSTANT if all elts are
1436 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1437 constants that the assembler and linker can compute them.
1439 The function returns the initializer itself, or error_mark_node in case
1443 process_init_constructor (tree type
, tree init
, tsubst_flags_t complain
)
1447 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init
));
1449 if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == VECTOR_TYPE
)
1450 flags
= process_init_constructor_array (type
, init
, complain
);
1451 else if (TREE_CODE (type
) == RECORD_TYPE
)
1452 flags
= process_init_constructor_record (type
, init
, complain
);
1453 else if (TREE_CODE (type
) == UNION_TYPE
)
1454 flags
= process_init_constructor_union (type
, init
, complain
);
1458 if (flags
& PICFLAG_ERRONEOUS
)
1459 return error_mark_node
;
1461 TREE_TYPE (init
) = type
;
1462 if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
) == NULL_TREE
)
1463 cp_complete_array_type (&TREE_TYPE (init
), init
, /*do_default=*/0);
1464 if (flags
& PICFLAG_NOT_ALL_CONSTANT
)
1465 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1466 TREE_CONSTANT (init
) = false;
1469 TREE_CONSTANT (init
) = 1;
1470 if (!(flags
& PICFLAG_NOT_ALL_SIMPLE
))
1471 TREE_STATIC (init
) = 1;
1476 /* Given a structure or union value DATUM, construct and return
1477 the structure or union component which results from narrowing
1478 that value to the base specified in BASETYPE. For example, given the
1481 class L { int ii; };
1482 class A : L { ... };
1483 class B : L { ... };
1484 class C : A, B { ... };
1492 x.A::ii refers to the ii member of the L part of
1493 the A part of the C object named by X. In this case,
1494 DATUM would be x, and BASETYPE would be A.
1496 I used to think that this was nonconformant, that the standard specified
1497 that first we look up ii in A, then convert x to an L& and pull out the
1498 ii part. But in fact, it does say that we convert x to an A&; A here
1499 is known as the "naming class". (jason 2000-12-19)
1501 BINFO_P points to a variable initialized either to NULL_TREE or to the
1502 binfo for the specific base subobject we want to convert to. */
1505 build_scoped_ref (tree datum
, tree basetype
, tree
* binfo_p
)
1509 if (datum
== error_mark_node
)
1510 return error_mark_node
;
1514 binfo
= lookup_base (TREE_TYPE (datum
), basetype
, ba_check
,
1515 NULL
, tf_warning_or_error
);
1517 if (!binfo
|| binfo
== error_mark_node
)
1519 *binfo_p
= NULL_TREE
;
1521 error_not_base_type (basetype
, TREE_TYPE (datum
));
1522 return error_mark_node
;
1526 return build_base_path (PLUS_EXPR
, datum
, binfo
, 1,
1527 tf_warning_or_error
);
1530 /* Build a reference to an object specified by the C++ `->' operator.
1531 Usually this just involves dereferencing the object, but if the
1532 `->' operator is overloaded, then such overloads must be
1533 performed until an object which does not have the `->' operator
1534 overloaded is found. An error is reported when circular pointer
1535 delegation is detected. */
1538 build_x_arrow (location_t loc
, tree expr
, tsubst_flags_t complain
)
1540 tree orig_expr
= expr
;
1541 tree type
= TREE_TYPE (expr
);
1542 tree last_rval
= NULL_TREE
;
1543 vec
<tree
, va_gc
> *types_memoized
= NULL
;
1545 if (type
== error_mark_node
)
1546 return error_mark_node
;
1548 if (processing_template_decl
)
1550 if (type_dependent_expression_p (expr
))
1551 return build_min_nt_loc (loc
, ARROW_EXPR
, expr
);
1552 expr
= build_non_dependent_expr (expr
);
1555 if (MAYBE_CLASS_TYPE_P (type
))
1557 struct tinst_level
*actual_inst
= current_instantiation ();
1560 while ((expr
= build_new_op (loc
, COMPONENT_REF
,
1561 LOOKUP_NORMAL
, expr
, NULL_TREE
, NULL_TREE
,
1564 if (expr
== error_mark_node
)
1565 return error_mark_node
;
1567 if (fn
&& DECL_USE_TEMPLATE (fn
))
1568 push_tinst_level (fn
);
1571 if (vec_member (TREE_TYPE (expr
), types_memoized
))
1573 if (complain
& tf_error
)
1574 error ("circular pointer delegation detected");
1575 return error_mark_node
;
1578 vec_safe_push (types_memoized
, TREE_TYPE (expr
));
1582 while (current_instantiation () != actual_inst
)
1585 if (last_rval
== NULL_TREE
)
1587 if (complain
& tf_error
)
1588 error ("base operand of %<->%> has non-pointer type %qT", type
);
1589 return error_mark_node
;
1592 if (TREE_CODE (TREE_TYPE (last_rval
)) == REFERENCE_TYPE
)
1593 last_rval
= convert_from_reference (last_rval
);
1596 last_rval
= decay_conversion (expr
, complain
);
1598 if (TYPE_PTR_P (TREE_TYPE (last_rval
)))
1600 if (processing_template_decl
)
1602 expr
= build_min (ARROW_EXPR
, TREE_TYPE (TREE_TYPE (last_rval
)),
1604 TREE_SIDE_EFFECTS (expr
) = TREE_SIDE_EFFECTS (last_rval
);
1608 return cp_build_indirect_ref (last_rval
, RO_NULL
, complain
);
1611 if (complain
& tf_error
)
1614 error ("result of %<operator->()%> yields non-pointer result");
1616 error ("base operand of %<->%> is not a pointer");
1618 return error_mark_node
;
1621 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1622 already been checked out to be of aggregate type. */
1625 build_m_component_ref (tree datum
, tree component
, tsubst_flags_t complain
)
1633 if (error_operand_p (datum
) || error_operand_p (component
))
1634 return error_mark_node
;
1636 datum
= mark_lvalue_use (datum
);
1637 component
= mark_rvalue_use (component
);
1639 ptrmem_type
= TREE_TYPE (component
);
1640 if (!TYPE_PTRMEM_P (ptrmem_type
))
1642 if (complain
& tf_error
)
1643 error ("%qE cannot be used as a member pointer, since it is of "
1644 "type %qT", component
, ptrmem_type
);
1645 return error_mark_node
;
1648 objtype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
1649 if (! MAYBE_CLASS_TYPE_P (objtype
))
1651 if (complain
& tf_error
)
1652 error ("cannot apply member pointer %qE to %qE, which is of "
1653 "non-class type %qT", component
, datum
, objtype
);
1654 return error_mark_node
;
1657 type
= TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type
);
1658 ctype
= complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type
));
1660 if (!COMPLETE_TYPE_P (ctype
))
1662 if (!same_type_p (ctype
, objtype
))
1668 binfo
= lookup_base (objtype
, ctype
, ba_check
, NULL
, complain
);
1673 if (complain
& tf_error
)
1674 error ("pointer to member type %qT incompatible with object "
1675 "type %qT", type
, objtype
);
1676 return error_mark_node
;
1678 else if (binfo
== error_mark_node
)
1679 return error_mark_node
;
1682 if (TYPE_PTRDATAMEM_P (ptrmem_type
))
1684 cp_lvalue_kind kind
= lvalue_kind (datum
);
1687 /* Compute the type of the field, as described in [expr.ref].
1688 There's no such thing as a mutable pointer-to-member, so
1689 things are not as complex as they are for references to
1690 non-static data members. */
1691 type
= cp_build_qualified_type (type
,
1692 (cp_type_quals (type
)
1693 | cp_type_quals (TREE_TYPE (datum
))));
1695 datum
= build_address (datum
);
1697 /* Convert object to the correct base. */
1700 datum
= build_base_path (PLUS_EXPR
, datum
, binfo
, 1, complain
);
1701 if (datum
== error_mark_node
)
1702 return error_mark_node
;
1705 /* Build an expression for "object + offset" where offset is the
1706 value stored in the pointer-to-data-member. */
1707 ptype
= build_pointer_type (type
);
1708 datum
= fold_build_pointer_plus (fold_convert (ptype
, datum
), component
);
1709 datum
= cp_build_indirect_ref (datum
, RO_NULL
, complain
);
1710 if (datum
== error_mark_node
)
1711 return error_mark_node
;
1713 /* If the object expression was an rvalue, return an rvalue. */
1714 if (kind
& clk_class
)
1715 datum
= rvalue (datum
);
1716 else if (kind
& clk_rvalueref
)
1717 datum
= move (datum
);
1722 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
1723 program is ill-formed if the second operand is a pointer to member
1724 function with ref-qualifier &. In a .* expression whose object
1725 expression is an lvalue, the program is ill-formed if the second
1726 operand is a pointer to member function with ref-qualifier &&. */
1727 if (FUNCTION_REF_QUALIFIED (type
))
1729 bool lval
= real_lvalue_p (datum
);
1730 if (lval
&& FUNCTION_RVALUE_QUALIFIED (type
))
1732 if (complain
& tf_error
)
1733 error ("pointer-to-member-function type %qT requires an rvalue",
1735 return error_mark_node
;
1737 else if (!lval
&& !FUNCTION_RVALUE_QUALIFIED (type
))
1739 if (complain
& tf_error
)
1740 error ("pointer-to-member-function type %qT requires an lvalue",
1742 return error_mark_node
;
1745 return build2 (OFFSET_REF
, type
, datum
, component
);
1749 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1752 build_functional_cast (tree exp
, tree parms
, tsubst_flags_t complain
)
1754 /* This is either a call to a constructor,
1755 or a C cast in C++'s `functional' notation. */
1757 /* The type to which we are casting. */
1759 vec
<tree
, va_gc
> *parmvec
;
1761 if (error_operand_p (exp
) || parms
== error_mark_node
)
1762 return error_mark_node
;
1764 if (TREE_CODE (exp
) == TYPE_DECL
)
1766 type
= TREE_TYPE (exp
);
1768 if (complain
& tf_warning
1769 && TREE_DEPRECATED (type
)
1770 && DECL_ARTIFICIAL (exp
))
1771 warn_deprecated_use (type
, NULL_TREE
);
1776 /* We need to check this explicitly, since value-initialization of
1777 arrays is allowed in other situations. */
1778 if (TREE_CODE (type
) == ARRAY_TYPE
)
1780 if (complain
& tf_error
)
1781 error ("functional cast to array type %qT", type
);
1782 return error_mark_node
;
1785 if (type_uses_auto (type
))
1787 if (complain
& tf_error
)
1788 error ("invalid use of %<auto%>");
1789 return error_mark_node
;
1792 if (processing_template_decl
)
1796 /* Diagnose this even in a template. We could also try harder
1797 to give all the usual errors when the type and args are
1799 if (TREE_CODE (type
) == REFERENCE_TYPE
&& !parms
)
1801 if (complain
& tf_error
)
1802 error ("invalid value-initialization of reference type");
1803 return error_mark_node
;
1806 t
= build_min (CAST_EXPR
, type
, parms
);
1807 /* We don't know if it will or will not have side effects. */
1808 TREE_SIDE_EFFECTS (t
) = 1;
1812 if (! MAYBE_CLASS_TYPE_P (type
))
1814 if (parms
== NULL_TREE
)
1816 if (VOID_TYPE_P (type
))
1817 return void_zero_node
;
1818 return build_value_init (cv_unqualified (type
), complain
);
1821 /* This must build a C cast. */
1822 parms
= build_x_compound_expr_from_list (parms
, ELK_FUNC_CAST
, complain
);
1823 return cp_build_c_cast (type
, parms
, complain
);
1826 /* Prepare to evaluate as a call to a constructor. If this expression
1827 is actually used, for example,
1829 return X (arg1, arg2, ...);
1831 then the slot being initialized will be filled in. */
1833 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
1834 return error_mark_node
;
1835 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
1836 return error_mark_node
;
1840 If the expression list is a single-expression, the type
1841 conversion is equivalent (in definedness, and if defined in
1842 meaning) to the corresponding cast expression. */
1843 if (parms
&& TREE_CHAIN (parms
) == NULL_TREE
)
1844 return cp_build_c_cast (type
, TREE_VALUE (parms
), complain
);
1848 The expression T(), where T is a simple-type-specifier for a
1849 non-array complete object type or the (possibly cv-qualified)
1850 void type, creates an rvalue of the specified type, which is
1851 value-initialized. */
1853 if (parms
== NULL_TREE
)
1855 exp
= build_value_init (type
, complain
);
1856 exp
= get_target_expr_sfinae (exp
, complain
);
1860 /* Call the constructor. */
1861 parmvec
= make_tree_vector ();
1862 for (; parms
!= NULL_TREE
; parms
= TREE_CHAIN (parms
))
1863 vec_safe_push (parmvec
, TREE_VALUE (parms
));
1864 exp
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
1865 &parmvec
, type
, LOOKUP_NORMAL
, complain
);
1866 release_tree_vector (parmvec
);
1868 if (exp
== error_mark_node
)
1869 return error_mark_node
;
1871 return build_cplus_new (type
, exp
, complain
);
1875 /* Add new exception specifier SPEC, to the LIST we currently have.
1876 If it's already in LIST then do nothing.
1877 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1878 know what we're doing. */
1881 add_exception_specifier (tree list
, tree spec
, int complain
)
1886 diagnostic_t diag_type
= DK_UNSPECIFIED
; /* none */
1888 if (spec
== error_mark_node
)
1891 gcc_assert (spec
&& (!list
|| TREE_VALUE (list
)));
1893 /* [except.spec] 1, type in an exception specifier shall not be
1894 incomplete, or pointer or ref to incomplete other than pointer
1896 is_ptr
= TYPE_PTR_P (core
);
1897 if (is_ptr
|| TREE_CODE (core
) == REFERENCE_TYPE
)
1898 core
= TREE_TYPE (core
);
1901 else if (VOID_TYPE_P (core
))
1903 else if (TREE_CODE (core
) == TEMPLATE_TYPE_PARM
)
1905 else if (processing_template_decl
)
1910 /* 15.4/1 says that types in an exception specifier must be complete,
1911 but it seems more reasonable to only require this on definitions
1912 and calls. So just give a pedwarn at this point; we will give an
1913 error later if we hit one of those two cases. */
1914 if (!COMPLETE_TYPE_P (complete_type (core
)))
1915 diag_type
= DK_PEDWARN
; /* pedwarn */
1922 for (probe
= list
; probe
; probe
= TREE_CHAIN (probe
))
1923 if (same_type_p (TREE_VALUE (probe
), spec
))
1926 list
= tree_cons (NULL_TREE
, spec
, list
);
1929 diag_type
= DK_ERROR
; /* error */
1931 if (diag_type
!= DK_UNSPECIFIED
1932 && (complain
& tf_warning_or_error
))
1933 cxx_incomplete_type_diagnostic (NULL_TREE
, core
, diag_type
);
1938 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
1941 nothrow_spec_p_uninst (const_tree spec
)
1943 if (DEFERRED_NOEXCEPT_SPEC_P (spec
))
1945 return nothrow_spec_p (spec
);
1948 /* Combine the two exceptions specifier lists LIST and ADD, and return
1949 their union. If FN is non-null, it's the source of ADD. */
1952 merge_exception_specifiers (tree list
, tree add
, tree fn
)
1954 tree noex
, orig_list
;
1956 /* No exception-specifier or noexcept(false) are less strict than
1957 anything else. Prefer the newer variant (LIST). */
1958 if (!list
|| list
== noexcept_false_spec
)
1960 else if (!add
|| add
== noexcept_false_spec
)
1963 /* noexcept(true) and throw() are stricter than anything else.
1964 As above, prefer the more recent one (LIST). */
1965 if (nothrow_spec_p_uninst (add
))
1968 noex
= TREE_PURPOSE (list
);
1969 if (DEFERRED_NOEXCEPT_SPEC_P (add
))
1971 /* If ADD is a deferred noexcept, we must have been called from
1972 process_subob_fn. For implicitly declared functions, we build up
1973 a list of functions to consider at instantiation time. */
1974 if (noex
&& operand_equal_p (noex
, boolean_true_node
, 0))
1976 gcc_assert (fn
&& (!noex
|| is_overloaded_fn (noex
)));
1977 noex
= build_overload (fn
, noex
);
1979 else if (nothrow_spec_p_uninst (list
))
1982 gcc_checking_assert (!TREE_PURPOSE (add
)
1983 || cp_tree_equal (noex
, TREE_PURPOSE (add
)));
1985 /* Combine the dynamic-exception-specifiers, if any. */
1987 for (; add
&& TREE_VALUE (add
); add
= TREE_CHAIN (add
))
1989 tree spec
= TREE_VALUE (add
);
1992 for (probe
= orig_list
; probe
&& TREE_VALUE (probe
);
1993 probe
= TREE_CHAIN (probe
))
1994 if (same_type_p (TREE_VALUE (probe
), spec
))
1998 spec
= build_tree_list (NULL_TREE
, spec
);
1999 TREE_CHAIN (spec
) = list
;
2004 /* Keep the noexcept-specifier at the beginning of the list. */
2005 if (noex
!= TREE_PURPOSE (list
))
2006 list
= tree_cons (noex
, TREE_VALUE (list
), TREE_CHAIN (list
));
2011 /* Subroutine of build_call. Ensure that each of the types in the
2012 exception specification is complete. Technically, 15.4/1 says that
2013 they need to be complete when we see a declaration of the function,
2014 but we should be able to get away with only requiring this when the
2015 function is defined or called. See also add_exception_specifier. */
2018 require_complete_eh_spec_types (tree fntype
, tree decl
)
2021 /* Don't complain about calls to op new. */
2022 if (decl
&& DECL_ARTIFICIAL (decl
))
2024 for (raises
= TYPE_RAISES_EXCEPTIONS (fntype
); raises
;
2025 raises
= TREE_CHAIN (raises
))
2027 tree type
= TREE_VALUE (raises
);
2028 if (type
&& !COMPLETE_TYPE_P (type
))
2032 ("call to function %qD which throws incomplete type %q#T",
2035 error ("call to function which throws incomplete type %q#T",
2042 #include "gt-cp-typeck2.h"