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"
39 process_init_constructor (tree type
, tree init
, tsubst_flags_t complain
);
42 /* Print an error message stemming from an attempt to use
43 BASETYPE as a base class for TYPE. */
46 error_not_base_type (tree basetype
, tree type
)
48 if (TREE_CODE (basetype
) == FUNCTION_DECL
)
49 basetype
= DECL_CONTEXT (basetype
);
50 error ("type %qT is not a base type for type %qT", basetype
, type
);
51 return error_mark_node
;
55 binfo_or_else (tree base
, tree type
)
57 tree binfo
= lookup_base (type
, base
, ba_unique
,
58 NULL
, tf_warning_or_error
);
60 if (binfo
== error_mark_node
)
63 error_not_base_type (base
, type
);
67 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
68 value may not be changed thereafter. */
71 cxx_readonly_error (tree arg
, enum lvalue_use errstring
)
74 /* This macro is used to emit diagnostics to ensure that all format
75 strings are complete sentences, visible to gettext and checked at
78 #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG) \
99 /* Handle C++-specific things first. */
102 && DECL_LANG_SPECIFIC (arg
)
103 && DECL_IN_AGGR_P (arg
)
104 && !TREE_STATIC (arg
))
105 ERROR_FOR_ASSIGNMENT (G_("assignment of "
106 "constant field %qD"),
107 G_("constant field %qD "
108 "used as %<asm%> output"),
110 "constant field %qD"),
112 "constant field %qD"),
114 else if (INDIRECT_REF_P (arg
)
115 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 0))) == REFERENCE_TYPE
116 && (VAR_P (TREE_OPERAND (arg
, 0))
117 || TREE_CODE (TREE_OPERAND (arg
, 0)) == PARM_DECL
))
118 ERROR_FOR_ASSIGNMENT (G_("assignment of "
119 "read-only reference %qD"),
120 G_("read-only reference %qD "
121 "used as %<asm%> output"),
123 "read-only reference %qD"),
125 "read-only reference %qD"),
126 TREE_OPERAND (arg
, 0));
128 readonly_error (arg
, errstring
);
132 /* Structure that holds information about declarations whose type was
133 incomplete and we could not check whether it was abstract or not. */
135 struct GTY((chain_next ("%h.next"))) pending_abstract_type
{
136 /* Declaration which we are checking for abstractness. It is either
137 a DECL node, or an IDENTIFIER_NODE if we do not have a full
138 declaration available. */
141 /* Type which will be checked for abstractness. */
144 /* Kind of use in an unnamed declarator. */
145 abstract_class_use use
;
147 /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
148 because DECLs already carry locus information. */
151 /* Link to the next element in list. */
152 struct pending_abstract_type
* next
;
156 /* Compute the hash value of the node VAL. This function is used by the
157 hash table abstract_pending_vars. */
160 pat_calc_hash (const void* val
)
162 const struct pending_abstract_type
*pat
=
163 (const struct pending_abstract_type
*) val
;
164 return (hashval_t
) TYPE_UID (pat
->type
);
168 /* Compare node VAL1 with the type VAL2. This function is used by the
169 hash table abstract_pending_vars. */
172 pat_compare (const void* val1
, const void* val2
)
174 const struct pending_abstract_type
*const pat1
=
175 (const struct pending_abstract_type
*) val1
;
176 const_tree
const type2
= (const_tree
)val2
;
178 return (pat1
->type
== type2
);
181 /* Hash table that maintains pending_abstract_type nodes, for which we still
182 need to check for type abstractness. The key of the table is the type
183 of the declaration. */
184 static GTY ((param_is (struct pending_abstract_type
)))
185 htab_t abstract_pending_vars
= NULL
;
187 static int abstract_virtuals_error_sfinae (tree
, tree
, abstract_class_use
, tsubst_flags_t
);
189 /* This function is called after TYPE is completed, and will check if there
190 are pending declarations for which we still need to verify the abstractness
191 of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
192 turned out to be incomplete. */
195 complete_type_check_abstract (tree type
)
198 struct pending_abstract_type
*pat
;
199 location_t cur_loc
= input_location
;
201 gcc_assert (COMPLETE_TYPE_P (type
));
203 if (!abstract_pending_vars
)
206 /* Retrieve the list of pending declarations for this type. */
207 slot
= htab_find_slot_with_hash (abstract_pending_vars
, type
,
208 (hashval_t
)TYPE_UID (type
), NO_INSERT
);
211 pat
= (struct pending_abstract_type
*)*slot
;
214 /* If the type is not abstract, do not do anything. */
215 if (CLASSTYPE_PURE_VIRTUALS (type
))
217 struct pending_abstract_type
*prev
= 0, *next
;
219 /* Reverse the list to emit the errors in top-down order. */
220 for (; pat
; pat
= next
)
228 /* Go through the list, and call abstract_virtuals_error for each
229 element: it will issue a diagnostic if the type is abstract. */
232 gcc_assert (type
== pat
->type
);
234 /* Tweak input_location so that the diagnostic appears at the correct
235 location. Notice that this is only needed if the decl is an
237 input_location
= pat
->locus
;
238 abstract_virtuals_error_sfinae (pat
->decl
, pat
->type
, pat
->use
,
239 tf_warning_or_error
);
244 htab_clear_slot (abstract_pending_vars
, slot
);
246 input_location
= cur_loc
;
250 /* If TYPE has abstract virtual functions, issue an error about trying
251 to create an object of that type. DECL is the object declared, or
252 NULL_TREE if the declaration is unavailable, in which case USE specifies
253 the kind of invalid use. Returns 1 if an error occurred; zero if
257 abstract_virtuals_error_sfinae (tree decl
, tree type
, abstract_class_use use
,
258 tsubst_flags_t complain
)
260 vec
<tree
, va_gc
> *pure
;
262 /* This function applies only to classes. Any other entity can never
264 if (!CLASS_TYPE_P (type
))
266 type
= TYPE_MAIN_VARIANT (type
);
269 /* Instantiation here seems to be required by the standard,
270 but breaks e.g. boost::bind. FIXME! */
271 /* In SFINAE, non-N3276 context, force instantiation. */
272 if (!(complain
& (tf_error
|tf_decltype
)))
273 complete_type (type
);
276 /* If the type is incomplete, we register it within a hash table,
277 so that we can check again once it is completed. This makes sense
278 only for objects for which we have a declaration or at least a
280 if (!COMPLETE_TYPE_P (type
) && (complain
& tf_error
))
283 struct pending_abstract_type
*pat
;
285 gcc_assert (!decl
|| DECL_P (decl
) || identifier_p (decl
));
287 if (!abstract_pending_vars
)
288 abstract_pending_vars
= htab_create_ggc (31, &pat_calc_hash
,
291 slot
= htab_find_slot_with_hash (abstract_pending_vars
, type
,
292 (hashval_t
)TYPE_UID (type
), INSERT
);
294 pat
= ggc_alloc_pending_abstract_type ();
298 pat
->locus
= ((decl
&& DECL_P (decl
))
299 ? DECL_SOURCE_LOCATION (decl
)
302 pat
->next
= (struct pending_abstract_type
*) *slot
;
308 if (!TYPE_SIZE (type
))
309 /* TYPE is being defined, and during that time
310 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
313 pure
= CLASSTYPE_PURE_VIRTUALS (type
);
317 if (!(complain
& tf_error
))
323 error ("cannot declare variable %q+D to be of abstract "
324 "type %qT", decl
, type
);
325 else if (TREE_CODE (decl
) == PARM_DECL
)
327 if (DECL_NAME (decl
))
328 error ("cannot declare parameter %q+D to be of abstract type %qT",
331 error ("cannot declare parameter to be of abstract type %qT",
334 else if (TREE_CODE (decl
) == FIELD_DECL
)
335 error ("cannot declare field %q+D to be of abstract type %qT",
337 else if (TREE_CODE (decl
) == FUNCTION_DECL
338 && TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
339 error ("invalid abstract return type for member function %q+#D", decl
);
340 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
341 error ("invalid abstract return type for function %q+#D", decl
);
342 else if (identifier_p (decl
))
343 /* Here we do not have location information. */
344 error ("invalid abstract type %qT for %qE", type
, decl
);
346 error ("invalid abstract type for %q+D", decl
);
351 error ("creating array of %qT, which is an abstract class type", type
);
354 error ("invalid cast to abstract class type %qT", type
);
357 error ("invalid new-expression of abstract class type %qT", type
);
360 error ("invalid abstract return type %qT", type
);
363 error ("invalid abstract parameter type %qT", type
);
366 error ("expression of abstract class type %qT cannot "
367 "be used in throw-expression", type
);
370 error ("cannot declare catch parameter to be of abstract "
371 "class type %qT", type
);
374 error ("cannot allocate an object of abstract type %qT", type
);
377 /* Only go through this once. */
383 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
384 " because the following virtual functions are pure within %qT:",
387 FOR_EACH_VEC_ELT (*pure
, ix
, fn
)
388 if (! DECL_CLONED_FUNCTION_P (fn
)
389 || DECL_COMPLETE_DESTRUCTOR_P (fn
))
390 inform (input_location
, "\t%+#D", fn
);
392 /* Now truncate the vector. This leaves it non-null, so we know
393 there are pure virtuals, but empty so we don't list them out
402 abstract_virtuals_error_sfinae (tree decl
, tree type
, tsubst_flags_t complain
)
404 return abstract_virtuals_error_sfinae (decl
, type
, ACU_UNKNOWN
, complain
);
408 abstract_virtuals_error_sfinae (abstract_class_use use
, tree type
,
409 tsubst_flags_t complain
)
411 return abstract_virtuals_error_sfinae (NULL_TREE
, type
, use
, complain
);
415 /* Wrapper for the above function in the common case of wanting errors. */
418 abstract_virtuals_error (tree decl
, tree type
)
420 return abstract_virtuals_error_sfinae (decl
, type
, tf_warning_or_error
);
424 abstract_virtuals_error (abstract_class_use use
, tree type
)
426 return abstract_virtuals_error_sfinae (use
, type
, tf_warning_or_error
);
429 /* Print an error message for invalid use of an incomplete type.
430 VALUE is the expression that was used (or 0 if that isn't known)
431 and TYPE is the type that was invalid. DIAG_KIND indicates the
432 type of diagnostic (see diagnostic.def). */
435 cxx_incomplete_type_diagnostic (const_tree value
, const_tree type
,
436 diagnostic_t diag_kind
)
440 gcc_assert (diag_kind
== DK_WARNING
441 || diag_kind
== DK_PEDWARN
442 || diag_kind
== DK_ERROR
);
444 /* Avoid duplicate error message. */
445 if (TREE_CODE (type
) == ERROR_MARK
)
448 if (value
!= 0 && (VAR_P (value
)
449 || TREE_CODE (value
) == PARM_DECL
450 || TREE_CODE (value
) == FIELD_DECL
))
452 emit_diagnostic (diag_kind
, input_location
, 0,
453 "%q+D has incomplete type", value
);
457 /* We must print an error message. Be clever about what it says. */
459 switch (TREE_CODE (type
))
465 emit_diagnostic (diag_kind
, input_location
, 0,
466 "invalid use of incomplete type %q#T", type
);
467 if (!TYPE_TEMPLATE_INFO (type
))
468 emit_diagnostic (diag_kind
, input_location
, 0,
469 "forward declaration of %q+#T", type
);
471 emit_diagnostic (diag_kind
, input_location
, 0,
472 "declaration of %q+#T", type
);
476 emit_diagnostic (diag_kind
, input_location
, 0,
477 "invalid use of %qT", type
);
481 if (TYPE_DOMAIN (type
))
483 type
= TREE_TYPE (type
);
486 emit_diagnostic (diag_kind
, input_location
, 0,
487 "invalid use of array with unspecified bounds");
493 tree member
= TREE_OPERAND (value
, 1);
494 if (is_overloaded_fn (member
))
495 member
= get_first_fn (member
);
496 if (DECL_FUNCTION_MEMBER_P (member
)
497 && ! flag_ms_extensions
)
498 emit_diagnostic (diag_kind
, input_location
, 0,
499 "invalid use of member function "
500 "(did you forget the %<()%> ?)");
502 emit_diagnostic (diag_kind
, input_location
, 0,
503 "invalid use of member "
504 "(did you forget the %<&%> ?)");
508 case TEMPLATE_TYPE_PARM
:
510 emit_diagnostic (diag_kind
, input_location
, 0,
511 "invalid use of %<auto%>");
513 emit_diagnostic (diag_kind
, input_location
, 0,
514 "invalid use of template type parameter %qT", type
);
517 case BOUND_TEMPLATE_TEMPLATE_PARM
:
518 emit_diagnostic (diag_kind
, input_location
, 0,
519 "invalid use of template template parameter %qT",
524 emit_diagnostic (diag_kind
, input_location
, 0,
525 "invalid use of dependent type %qT", type
);
529 if (type
== init_list_type_node
)
531 emit_diagnostic (diag_kind
, input_location
, 0,
532 "invalid use of brace-enclosed initializer list");
535 gcc_assert (type
== unknown_type_node
);
536 if (value
&& TREE_CODE (value
) == COMPONENT_REF
)
538 else if (value
&& TREE_CODE (value
) == ADDR_EXPR
)
539 emit_diagnostic (diag_kind
, input_location
, 0,
540 "address of overloaded function with no contextual "
542 else if (value
&& TREE_CODE (value
) == OVERLOAD
)
543 emit_diagnostic (diag_kind
, input_location
, 0,
544 "overloaded function with no contextual type information");
546 emit_diagnostic (diag_kind
, input_location
, 0,
547 "insufficient contextual information to determine type");
555 /* Backward-compatibility interface to incomplete_type_diagnostic;
556 required by ../tree.c. */
557 #undef cxx_incomplete_type_error
559 cxx_incomplete_type_error (const_tree value
, const_tree type
)
561 cxx_incomplete_type_diagnostic (value
, type
, DK_ERROR
);
565 /* The recursive part of split_nonconstant_init. DEST is an lvalue
566 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
567 Return true if the whole of the value was initialized by the
568 generated statements. */
571 split_nonconstant_init_1 (tree dest
, tree init
)
573 unsigned HOST_WIDE_INT idx
;
574 tree field_index
, value
;
575 tree type
= TREE_TYPE (dest
);
576 tree inner_type
= NULL
;
577 bool array_type_p
= false;
578 bool complete_p
= true;
579 HOST_WIDE_INT num_split_elts
= 0;
581 switch (TREE_CODE (type
))
584 inner_type
= TREE_TYPE (type
);
590 case QUAL_UNION_TYPE
:
591 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init
), idx
,
594 /* The current implementation of this algorithm assumes that
595 the field was set for all the elements. This is usually done
596 by process_init_constructor. */
597 gcc_assert (field_index
);
600 inner_type
= TREE_TYPE (field_index
);
602 if (TREE_CODE (value
) == CONSTRUCTOR
)
607 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
608 NULL_TREE
, NULL_TREE
);
610 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
613 if (!split_nonconstant_init_1 (sub
, value
))
617 else if (!initializer_constant_valid_p (value
, inner_type
))
622 /* FIXME: Ordered removal is O(1) so the whole function is
623 worst-case quadratic. This could be fixed using an aside
624 bitmap to record which elements must be removed and remove
625 them all at the same time. Or by merging
626 split_non_constant_init into process_init_constructor_array,
627 that is separating constants from non-constants while building
629 CONSTRUCTOR_ELTS (init
)->ordered_remove (idx
);
633 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
634 NULL_TREE
, NULL_TREE
);
636 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
639 code
= build2 (INIT_EXPR
, inner_type
, sub
, value
);
640 code
= build_stmt (input_location
, EXPR_STMT
, code
);
641 code
= maybe_cleanup_point_expr_void (code
);
643 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (inner_type
))
645 code
= (build_special_member_call
646 (sub
, complete_dtor_identifier
, NULL
, inner_type
,
647 LOOKUP_NORMAL
, tf_warning_or_error
));
648 finish_eh_cleanup (code
);
657 if (!initializer_constant_valid_p (init
, type
))
660 tree cons
= copy_node (init
);
661 CONSTRUCTOR_ELTS (init
) = NULL
;
662 code
= build2 (MODIFY_EXPR
, type
, dest
, cons
);
663 code
= build_stmt (input_location
, EXPR_STMT
, code
);
665 num_split_elts
+= CONSTRUCTOR_NELTS (init
);
673 /* The rest of the initializer is now a constant. */
674 TREE_CONSTANT (init
) = 1;
675 return complete_p
&& complete_ctor_at_level_p (TREE_TYPE (init
),
676 num_split_elts
, inner_type
);
679 /* A subroutine of store_init_value. Splits non-constant static
680 initializer INIT into a constant part and generates code to
681 perform the non-constant part of the initialization to DEST.
682 Returns the code for the runtime init. */
685 split_nonconstant_init (tree dest
, tree init
)
689 if (TREE_CODE (init
) == CONSTRUCTOR
)
691 code
= push_stmt_list ();
692 if (split_nonconstant_init_1 (dest
, init
))
694 code
= pop_stmt_list (code
);
695 DECL_INITIAL (dest
) = init
;
696 TREE_READONLY (dest
) = 0;
699 code
= build2 (INIT_EXPR
, TREE_TYPE (dest
), dest
, init
);
704 /* Perform appropriate conversions on the initial value of a variable,
705 store it in the declaration DECL,
706 and print any error messages that are appropriate.
707 If the init is invalid, store an ERROR_MARK.
709 C++: Note that INIT might be a TREE_LIST, which would mean that it is
710 a base class initializer for some aggregate type, hopefully compatible
711 with DECL. If INIT is a single element, and DECL is an aggregate
712 type, we silently convert INIT into a TREE_LIST, allowing a constructor
715 If INIT is a TREE_LIST and there is no constructor, turn INIT
716 into a CONSTRUCTOR and use standard initialization techniques.
717 Perhaps a warning should be generated?
719 Returns code to be executed if initialization could not be performed
720 for static variable. In that case, caller must emit the code. */
723 store_init_value (tree decl
, tree init
, vec
<tree
, va_gc
>** cleanups
, int flags
)
727 /* If variable's type was invalidly declared, just ignore it. */
729 type
= TREE_TYPE (decl
);
730 if (TREE_CODE (type
) == ERROR_MARK
)
733 if (MAYBE_CLASS_TYPE_P (type
))
735 if (TREE_CODE (init
) == TREE_LIST
)
737 error ("constructor syntax used, but no constructor declared "
738 "for type %qT", type
);
739 init
= build_constructor_from_list (init_list_type_node
, nreverse (init
));
742 else if (TREE_CODE (init
) == TREE_LIST
743 && TREE_TYPE (init
) != unknown_type_node
)
745 gcc_assert (TREE_CODE (decl
) != RESULT_DECL
);
747 if (TREE_CODE (init
) == TREE_LIST
748 && TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
750 error ("cannot initialize arrays using this syntax");
754 /* We get here with code like `int a (2);' */
755 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
756 tf_warning_or_error
);
759 /* End of special C++ code. */
761 if (flags
& LOOKUP_ALREADY_DIGESTED
)
764 /* Digest the specified initializer into an expression. */
765 value
= digest_init_flags (type
, init
, flags
);
767 value
= extend_ref_init_temps (decl
, value
, cleanups
);
769 /* In C++0x constant expression is a semantic, not syntactic, property.
770 In C++98, make sure that what we thought was a constant expression at
771 template definition time is still constant and otherwise perform this
772 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
773 if (decl_maybe_constant_var_p (decl
) || TREE_STATIC (decl
))
776 value
= fold_non_dependent_expr (value
);
777 value
= maybe_constant_init (value
);
778 if (DECL_DECLARED_CONSTEXPR_P (decl
))
780 /* Diagnose a non-constant initializer for constexpr. */
781 if (processing_template_decl
782 && !require_potential_constant_expression (value
))
783 value
= error_mark_node
;
785 value
= cxx_constant_value (value
);
787 const_init
= (reduced_constant_expression_p (value
)
788 || error_operand_p (value
));
789 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = const_init
;
790 TREE_CONSTANT (decl
) = const_init
&& decl_maybe_constant_var_p (decl
);
793 /* If the initializer is not a constant, fill in DECL_INITIAL with
794 the bits that are constant, and then return an expression that
795 will perform the dynamic initialization. */
796 if (value
!= error_mark_node
797 && (TREE_SIDE_EFFECTS (value
)
798 || array_of_runtime_bound_p (type
)
799 || ! reduced_constant_expression_p (value
)))
801 if (TREE_CODE (type
) == ARRAY_TYPE
802 && (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type
))
803 || array_of_runtime_bound_p (type
)))
804 /* For an array, we only need/want a single cleanup region rather
805 than one per element. */
806 return build_vec_init (decl
, NULL_TREE
, value
, false, 1,
807 tf_warning_or_error
);
809 return split_nonconstant_init (decl
, value
);
811 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
812 is an automatic variable, the middle end will turn this into a
813 dynamic initialization later. */
814 DECL_INITIAL (decl
) = value
;
819 /* Give errors about narrowing conversions within { }. */
822 check_narrowing (tree type
, tree init
)
824 tree ftype
= unlowered_expr_type (init
);
828 if (!warn_narrowing
|| !ARITHMETIC_TYPE_P (type
))
831 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
832 && TREE_CODE (type
) == COMPLEX_TYPE
)
834 tree elttype
= TREE_TYPE (type
);
835 check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 0)->value
);
836 if (CONSTRUCTOR_NELTS (init
) > 1)
837 check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 1)->value
);
841 init
= maybe_constant_value (init
);
843 if (TREE_CODE (type
) == INTEGER_TYPE
844 && TREE_CODE (ftype
) == REAL_TYPE
)
846 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
847 && CP_INTEGRAL_TYPE_P (type
))
849 if (TREE_CODE (ftype
) == ENUMERAL_TYPE
)
850 /* Check for narrowing based on the values of the enumeration. */
851 ftype
= ENUM_UNDERLYING_TYPE (ftype
);
852 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type
),
853 TYPE_MAX_VALUE (ftype
))
854 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype
),
855 TYPE_MIN_VALUE (type
)))
856 && (TREE_CODE (init
) != INTEGER_CST
857 || !int_fits_type_p (init
, type
)))
860 else if (TREE_CODE (ftype
) == REAL_TYPE
861 && TREE_CODE (type
) == REAL_TYPE
)
863 if (TYPE_PRECISION (type
) < TYPE_PRECISION (ftype
))
865 if (TREE_CODE (init
) == REAL_CST
)
867 /* Issue 703: Loss of precision is OK as long as the value is
868 within the representable range of the new type. */
870 d
= TREE_REAL_CST (init
);
871 real_convert (&r
, TYPE_MODE (type
), &d
);
879 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
880 && TREE_CODE (type
) == REAL_TYPE
)
883 if (TREE_CODE (init
) == INTEGER_CST
)
885 d
= real_value_from_int_cst (0, init
);
886 if (exact_real_truncate (TYPE_MODE (type
), &d
))
893 if (cxx_dialect
>= cxx11
)
894 pedwarn (EXPR_LOC_OR_HERE (init
), OPT_Wnarrowing
,
895 "narrowing conversion of %qE from %qT to %qT inside { }",
898 warning_at (EXPR_LOC_OR_HERE (init
), OPT_Wnarrowing
,
899 "narrowing conversion of %qE from %qT to %qT inside { } "
900 "is ill-formed in C++11", init
, ftype
, type
);
904 /* Process the initializer INIT for a variable of type TYPE, emitting
905 diagnostics for invalid initializers and converting the initializer as
908 For aggregate types, it assumes that reshape_init has already run, thus the
909 initializer will have the right shape (brace elision has been undone).
911 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
914 digest_init_r (tree type
, tree init
, bool nested
, int flags
,
915 tsubst_flags_t complain
)
917 enum tree_code code
= TREE_CODE (type
);
919 if (error_operand_p (init
))
920 return error_mark_node
;
924 /* We must strip the outermost array type when completing the type,
925 because the its bounds might be incomplete at the moment. */
926 if (!complete_type_or_maybe_complain (TREE_CODE (type
) == ARRAY_TYPE
927 ? TREE_TYPE (type
) : type
, NULL_TREE
,
929 return error_mark_node
;
931 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
932 (g++.old-deja/g++.law/casts2.C). */
933 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
934 init
= TREE_OPERAND (init
, 0);
936 /* Initialization of an array of chars from a string constant. The initializer
937 can be optionally enclosed in braces, but reshape_init has already removed
938 them if they were present. */
939 if (code
== ARRAY_TYPE
)
941 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
942 if (char_type_p (typ1
)
944 && TREE_CODE (init
) == STRING_CST
)
946 tree char_type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init
)));
948 if (TYPE_PRECISION (typ1
) == BITS_PER_UNIT
)
950 if (char_type
!= char_type_node
)
952 if (complain
& tf_error
)
953 error ("char-array initialized from wide string");
954 return error_mark_node
;
959 if (char_type
== char_type_node
)
961 if (complain
& tf_error
)
962 error ("int-array initialized from non-wide string");
963 return error_mark_node
;
965 else if (char_type
!= typ1
)
967 if (complain
& tf_error
)
968 error ("int-array initialized from incompatible "
970 return error_mark_node
;
974 if (type
!= TREE_TYPE (init
))
976 init
= copy_node (init
);
977 TREE_TYPE (init
) = type
;
979 if (TYPE_DOMAIN (type
) != 0 && TREE_CONSTANT (TYPE_SIZE (type
)))
981 int size
= TREE_INT_CST_LOW (TYPE_SIZE (type
));
982 size
= (size
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
;
983 /* In C it is ok to subtract 1 from the length of the string
984 because it's ok to ignore the terminating null char that is
985 counted in the length of the constant, but in C++ this would
987 if (size
< TREE_STRING_LENGTH (init
))
988 permerror (input_location
, "initializer-string for array "
989 "of chars is too long");
995 /* Handle scalar types (including conversions) and references. */
996 if ((TREE_CODE (type
) != COMPLEX_TYPE
997 || BRACE_ENCLOSED_INITIALIZER_P (init
))
998 && (SCALAR_TYPE_P (type
) || code
== REFERENCE_TYPE
))
1003 check_narrowing (type
, init
);
1004 init
= convert_for_initialization (0, type
, init
, flags
,
1005 ICR_INIT
, NULL_TREE
, 0,
1009 /* Skip any conversions since we'll be outputting the underlying
1011 while (CONVERT_EXPR_P (*exp
)
1012 || TREE_CODE (*exp
) == NON_LVALUE_EXPR
)
1013 exp
= &TREE_OPERAND (*exp
, 0);
1015 *exp
= cplus_expand_constant (*exp
);
1020 /* Come here only for aggregates: records, arrays, unions, complex numbers
1022 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
1023 || TREE_CODE (type
) == VECTOR_TYPE
1024 || TREE_CODE (type
) == RECORD_TYPE
1025 || TREE_CODE (type
) == UNION_TYPE
1026 || TREE_CODE (type
) == COMPLEX_TYPE
);
1028 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
1029 && !TYPE_NON_AGGREGATE_CLASS (type
))
1030 return process_init_constructor (type
, init
, complain
);
1033 if (COMPOUND_LITERAL_P (init
) && TREE_CODE (type
) == ARRAY_TYPE
)
1035 if (complain
& tf_error
)
1036 error ("cannot initialize aggregate of type %qT with "
1037 "a compound literal", type
);
1039 return error_mark_node
;
1042 if (TREE_CODE (type
) == ARRAY_TYPE
1043 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
1045 /* Allow the result of build_array_copy and of
1046 build_value_init_noctor. */
1047 if ((TREE_CODE (init
) == VEC_INIT_EXPR
1048 || TREE_CODE (init
) == CONSTRUCTOR
)
1049 && (same_type_ignoring_top_level_qualifiers_p
1050 (type
, TREE_TYPE (init
))))
1053 if (complain
& tf_error
)
1054 error ("array must be initialized with a brace-enclosed"
1056 return error_mark_node
;
1059 return convert_for_initialization (NULL_TREE
, type
, init
,
1061 ICR_INIT
, NULL_TREE
, 0,
1067 digest_init (tree type
, tree init
, tsubst_flags_t complain
)
1069 return digest_init_r (type
, init
, false, LOOKUP_IMPLICIT
, complain
);
1073 digest_init_flags (tree type
, tree init
, int flags
)
1075 return digest_init_r (type
, init
, false, flags
, tf_warning_or_error
);
1078 /* Set of flags used within process_init_constructor to describe the
1080 #define PICFLAG_ERRONEOUS 1
1081 #define PICFLAG_NOT_ALL_CONSTANT 2
1082 #define PICFLAG_NOT_ALL_SIMPLE 4
1084 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1088 picflag_from_initializer (tree init
)
1090 if (init
== error_mark_node
)
1091 return PICFLAG_ERRONEOUS
;
1092 else if (!TREE_CONSTANT (init
))
1093 return PICFLAG_NOT_ALL_CONSTANT
;
1094 else if (!initializer_constant_valid_p (init
, TREE_TYPE (init
)))
1095 return PICFLAG_NOT_ALL_SIMPLE
;
1099 /* Subroutine of process_init_constructor, which will process an initializer
1100 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1101 which describe the initializers. */
1104 process_init_constructor_array (tree type
, tree init
,
1105 tsubst_flags_t complain
)
1107 unsigned HOST_WIDE_INT i
, len
= 0;
1109 bool unbounded
= false;
1110 constructor_elt
*ce
;
1111 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (init
);
1113 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
1114 || TREE_CODE (type
) == VECTOR_TYPE
);
1116 if (TREE_CODE (type
) == ARRAY_TYPE
)
1118 tree domain
= TYPE_DOMAIN (type
);
1119 if (domain
&& TREE_CONSTANT (TYPE_MAX_VALUE (domain
)))
1120 len
= (tree_to_double_int (TYPE_MAX_VALUE (domain
))
1121 - tree_to_double_int (TYPE_MIN_VALUE (domain
))
1123 .ext (TYPE_PRECISION (TREE_TYPE (domain
)),
1124 TYPE_UNSIGNED (TREE_TYPE (domain
)))
1127 unbounded
= true; /* Take as many as there are. */
1130 /* Vectors are like simple fixed-size arrays. */
1131 len
= TYPE_VECTOR_SUBPARTS (type
);
1133 /* There must not be more initializers than needed. */
1134 if (!unbounded
&& vec_safe_length (v
) > len
)
1136 if (complain
& tf_error
)
1137 error ("too many initializers for %qT", type
);
1139 return PICFLAG_ERRONEOUS
;
1142 FOR_EACH_VEC_SAFE_ELT (v
, i
, ce
)
1146 gcc_assert (TREE_CODE (ce
->index
) == INTEGER_CST
);
1147 if (compare_tree_int (ce
->index
, i
) != 0)
1149 ce
->value
= error_mark_node
;
1150 sorry ("non-trivial designated initializers not supported");
1154 ce
->index
= size_int (i
);
1155 gcc_assert (ce
->value
);
1156 ce
->value
= digest_init_r (TREE_TYPE (type
), ce
->value
, true,
1157 LOOKUP_IMPLICIT
, complain
);
1159 if (ce
->value
!= error_mark_node
)
1160 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1161 (TREE_TYPE (type
), TREE_TYPE (ce
->value
)));
1163 flags
|= picflag_from_initializer (ce
->value
);
1166 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1167 we must add initializers ourselves. */
1169 for (; i
< len
; ++i
)
1173 if (type_build_ctor_call (TREE_TYPE (type
)))
1175 /* If this type needs constructors run for default-initialization,
1176 we can't rely on the back end to do it for us, so make the
1177 initialization explicit by list-initializing from {}. */
1178 next
= build_constructor (init_list_type_node
, NULL
);
1179 next
= digest_init (TREE_TYPE (type
), next
, complain
);
1181 else if (!zero_init_p (TREE_TYPE (type
)))
1182 next
= build_zero_init (TREE_TYPE (type
),
1183 /*nelts=*/NULL_TREE
,
1184 /*static_storage_p=*/false);
1186 /* The default zero-initialization is fine for us; don't
1187 add anything to the CONSTRUCTOR. */
1190 flags
|= picflag_from_initializer (next
);
1191 CONSTRUCTOR_APPEND_ELT (v
, size_int (i
), next
);
1194 CONSTRUCTOR_ELTS (init
) = v
;
1198 /* Subroutine of process_init_constructor, which will process an initializer
1199 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1200 the initializers. */
1203 process_init_constructor_record (tree type
, tree init
,
1204 tsubst_flags_t complain
)
1206 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1209 unsigned HOST_WIDE_INT idx
= 0;
1211 gcc_assert (TREE_CODE (type
) == RECORD_TYPE
);
1212 gcc_assert (!CLASSTYPE_VBASECLASSES (type
));
1213 gcc_assert (!TYPE_BINFO (type
)
1214 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)));
1215 gcc_assert (!TYPE_POLYMORPHIC_P (type
));
1217 /* Generally, we will always have an index for each initializer (which is
1218 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1219 reshape_init. So we need to handle both cases. */
1220 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1225 if (!DECL_NAME (field
) && DECL_C_BIT_FIELD (field
))
1227 flags
|= picflag_from_initializer (integer_zero_node
);
1228 CONSTRUCTOR_APPEND_ELT (v
, field
, integer_zero_node
);
1232 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
1235 /* If this is a bitfield, first convert to the declared type. */
1236 type
= TREE_TYPE (field
);
1237 if (DECL_BIT_FIELD_TYPE (field
))
1238 type
= DECL_BIT_FIELD_TYPE (field
);
1239 if (type
== error_mark_node
)
1240 return PICFLAG_ERRONEOUS
;
1242 if (idx
< vec_safe_length (CONSTRUCTOR_ELTS (init
)))
1244 constructor_elt
*ce
= &(*CONSTRUCTOR_ELTS (init
))[idx
];
1247 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1248 latter case can happen in templates where lookup has to be
1250 gcc_assert (TREE_CODE (ce
->index
) == FIELD_DECL
1251 || identifier_p (ce
->index
));
1252 if (ce
->index
!= field
1253 && ce
->index
!= DECL_NAME (field
))
1255 ce
->value
= error_mark_node
;
1256 sorry ("non-trivial designated initializers not supported");
1260 gcc_assert (ce
->value
);
1261 next
= digest_init_r (type
, ce
->value
, true,
1262 LOOKUP_IMPLICIT
, complain
);
1265 else if (type_build_ctor_call (TREE_TYPE (field
)))
1267 /* If this type needs constructors run for
1268 default-initialization, we can't rely on the back end to do it
1269 for us, so build up TARGET_EXPRs. If the type in question is
1270 a class, just build one up; if it's an array, recurse. */
1271 next
= build_constructor (init_list_type_node
, NULL
);
1272 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (field
)))
1274 next
= finish_compound_literal (TREE_TYPE (field
), next
,
1276 /* direct-initialize the target. No temporary is going
1278 if (TREE_CODE (next
) == TARGET_EXPR
)
1279 TARGET_EXPR_DIRECT_INIT_P (next
) = true;
1282 next
= digest_init_r (TREE_TYPE (field
), next
, true,
1283 LOOKUP_IMPLICIT
, complain
);
1285 /* Warn when some struct elements are implicitly initialized. */
1286 warning (OPT_Wmissing_field_initializers
,
1287 "missing initializer for member %qD", field
);
1291 if (TREE_READONLY (field
))
1293 if (complain
& tf_error
)
1294 error ("uninitialized const member %qD", field
);
1296 return PICFLAG_ERRONEOUS
;
1298 else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field
)))
1300 if (complain
& tf_error
)
1301 error ("member %qD with uninitialized const fields", field
);
1303 return PICFLAG_ERRONEOUS
;
1305 else if (TREE_CODE (TREE_TYPE (field
)) == REFERENCE_TYPE
)
1307 if (complain
& tf_error
)
1308 error ("member %qD is uninitialized reference", field
);
1310 return PICFLAG_ERRONEOUS
;
1313 /* Warn when some struct elements are implicitly initialized
1315 warning (OPT_Wmissing_field_initializers
,
1316 "missing initializer for member %qD", field
);
1318 if (!zero_init_p (TREE_TYPE (field
)))
1319 next
= build_zero_init (TREE_TYPE (field
), /*nelts=*/NULL_TREE
,
1320 /*static_storage_p=*/false);
1322 /* The default zero-initialization is fine for us; don't
1323 add anything to the CONSTRUCTOR. */
1327 /* If this is a bitfield, now convert to the lowered type. */
1328 if (type
!= TREE_TYPE (field
))
1329 next
= cp_convert_and_check (TREE_TYPE (field
), next
, complain
);
1330 flags
|= picflag_from_initializer (next
);
1331 CONSTRUCTOR_APPEND_ELT (v
, field
, next
);
1334 if (idx
< vec_safe_length (CONSTRUCTOR_ELTS (init
)))
1336 if (complain
& tf_error
)
1337 error ("too many initializers for %qT", type
);
1339 return PICFLAG_ERRONEOUS
;
1342 CONSTRUCTOR_ELTS (init
) = v
;
1346 /* Subroutine of process_init_constructor, which will process a single
1347 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1348 which describe the initializer. */
1351 process_init_constructor_union (tree type
, tree init
,
1352 tsubst_flags_t complain
)
1354 constructor_elt
*ce
;
1357 /* If the initializer was empty, use default zero initialization. */
1358 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
1361 len
= CONSTRUCTOR_ELTS (init
)->length ();
1364 if (!(complain
& tf_error
))
1365 return PICFLAG_ERRONEOUS
;
1366 error ("too many initializers for %qT", type
);
1367 CONSTRUCTOR_ELTS (init
)->block_remove (1, len
-1);
1370 ce
= &(*CONSTRUCTOR_ELTS (init
))[0];
1372 /* If this element specifies a field, initialize via that field. */
1375 if (TREE_CODE (ce
->index
) == FIELD_DECL
)
1377 else if (identifier_p (ce
->index
))
1379 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1380 tree name
= ce
->index
;
1382 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1383 if (DECL_NAME (field
) == name
)
1387 if (complain
& tf_error
)
1388 error ("no field %qD found in union being initialized",
1390 ce
->value
= error_mark_node
;
1396 gcc_assert (TREE_CODE (ce
->index
) == INTEGER_CST
1397 || TREE_CODE (ce
->index
) == RANGE_EXPR
);
1398 if (complain
& tf_error
)
1399 error ("index value instead of field name in union initializer");
1400 ce
->value
= error_mark_node
;
1405 /* Find the first named field. ANSI decided in September 1990
1406 that only named fields count here. */
1407 tree field
= TYPE_FIELDS (type
);
1408 while (field
&& (!DECL_NAME (field
) || TREE_CODE (field
) != FIELD_DECL
))
1409 field
= TREE_CHAIN (field
);
1410 if (field
== NULL_TREE
)
1412 if (complain
& tf_error
)
1413 error ("too many initializers for %qT", type
);
1414 ce
->value
= error_mark_node
;
1419 if (ce
->value
&& ce
->value
!= error_mark_node
)
1420 ce
->value
= digest_init_r (TREE_TYPE (ce
->index
), ce
->value
,
1421 true, LOOKUP_IMPLICIT
, complain
);
1423 return picflag_from_initializer (ce
->value
);
1426 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1427 constructor is a brace-enclosed initializer, and will be modified in-place.
1429 Each element is converted to the right type through digest_init, and
1430 missing initializers are added following the language rules (zero-padding,
1433 After the execution, the initializer will have TREE_CONSTANT if all elts are
1434 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1435 constants that the assembler and linker can compute them.
1437 The function returns the initializer itself, or error_mark_node in case
1441 process_init_constructor (tree type
, tree init
, tsubst_flags_t complain
)
1445 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init
));
1447 if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == VECTOR_TYPE
)
1448 flags
= process_init_constructor_array (type
, init
, complain
);
1449 else if (TREE_CODE (type
) == RECORD_TYPE
)
1450 flags
= process_init_constructor_record (type
, init
, complain
);
1451 else if (TREE_CODE (type
) == UNION_TYPE
)
1452 flags
= process_init_constructor_union (type
, init
, complain
);
1456 if (flags
& PICFLAG_ERRONEOUS
)
1457 return error_mark_node
;
1459 TREE_TYPE (init
) = type
;
1460 if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
) == NULL_TREE
)
1461 cp_complete_array_type (&TREE_TYPE (init
), init
, /*do_default=*/0);
1462 if (flags
& PICFLAG_NOT_ALL_CONSTANT
)
1463 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1464 TREE_CONSTANT (init
) = false;
1467 TREE_CONSTANT (init
) = 1;
1468 if (!(flags
& PICFLAG_NOT_ALL_SIMPLE
))
1469 TREE_STATIC (init
) = 1;
1474 /* Given a structure or union value DATUM, construct and return
1475 the structure or union component which results from narrowing
1476 that value to the base specified in BASETYPE. For example, given the
1479 class L { int ii; };
1480 class A : L { ... };
1481 class B : L { ... };
1482 class C : A, B { ... };
1490 x.A::ii refers to the ii member of the L part of
1491 the A part of the C object named by X. In this case,
1492 DATUM would be x, and BASETYPE would be A.
1494 I used to think that this was nonconformant, that the standard specified
1495 that first we look up ii in A, then convert x to an L& and pull out the
1496 ii part. But in fact, it does say that we convert x to an A&; A here
1497 is known as the "naming class". (jason 2000-12-19)
1499 BINFO_P points to a variable initialized either to NULL_TREE or to the
1500 binfo for the specific base subobject we want to convert to. */
1503 build_scoped_ref (tree datum
, tree basetype
, tree
* binfo_p
)
1507 if (datum
== error_mark_node
)
1508 return error_mark_node
;
1512 binfo
= lookup_base (TREE_TYPE (datum
), basetype
, ba_check
,
1513 NULL
, tf_warning_or_error
);
1515 if (!binfo
|| binfo
== error_mark_node
)
1517 *binfo_p
= NULL_TREE
;
1519 error_not_base_type (basetype
, TREE_TYPE (datum
));
1520 return error_mark_node
;
1524 return build_base_path (PLUS_EXPR
, datum
, binfo
, 1,
1525 tf_warning_or_error
);
1528 /* Build a reference to an object specified by the C++ `->' operator.
1529 Usually this just involves dereferencing the object, but if the
1530 `->' operator is overloaded, then such overloads must be
1531 performed until an object which does not have the `->' operator
1532 overloaded is found. An error is reported when circular pointer
1533 delegation is detected. */
1536 build_x_arrow (location_t loc
, tree expr
, tsubst_flags_t complain
)
1538 tree orig_expr
= expr
;
1539 tree type
= TREE_TYPE (expr
);
1540 tree last_rval
= NULL_TREE
;
1541 vec
<tree
, va_gc
> *types_memoized
= NULL
;
1543 if (type
== error_mark_node
)
1544 return error_mark_node
;
1546 if (processing_template_decl
)
1548 if (type_dependent_expression_p (expr
))
1549 return build_min_nt_loc (loc
, ARROW_EXPR
, expr
);
1550 expr
= build_non_dependent_expr (expr
);
1553 if (MAYBE_CLASS_TYPE_P (type
))
1555 struct tinst_level
*actual_inst
= current_instantiation ();
1558 while ((expr
= build_new_op (loc
, COMPONENT_REF
,
1559 LOOKUP_NORMAL
, expr
, NULL_TREE
, NULL_TREE
,
1562 if (expr
== error_mark_node
)
1563 return error_mark_node
;
1565 if (fn
&& DECL_USE_TEMPLATE (fn
))
1566 push_tinst_level (fn
);
1569 if (vec_member (TREE_TYPE (expr
), types_memoized
))
1571 if (complain
& tf_error
)
1572 error ("circular pointer delegation detected");
1573 return error_mark_node
;
1576 vec_safe_push (types_memoized
, TREE_TYPE (expr
));
1580 while (current_instantiation () != actual_inst
)
1583 if (last_rval
== NULL_TREE
)
1585 if (complain
& tf_error
)
1586 error ("base operand of %<->%> has non-pointer type %qT", type
);
1587 return error_mark_node
;
1590 if (TREE_CODE (TREE_TYPE (last_rval
)) == REFERENCE_TYPE
)
1591 last_rval
= convert_from_reference (last_rval
);
1594 last_rval
= decay_conversion (expr
, complain
);
1596 if (TYPE_PTR_P (TREE_TYPE (last_rval
)))
1598 if (processing_template_decl
)
1600 expr
= build_min (ARROW_EXPR
, TREE_TYPE (TREE_TYPE (last_rval
)),
1602 TREE_SIDE_EFFECTS (expr
) = TREE_SIDE_EFFECTS (last_rval
);
1606 return cp_build_indirect_ref (last_rval
, RO_NULL
, complain
);
1609 if (complain
& tf_error
)
1612 error ("result of %<operator->()%> yields non-pointer result");
1614 error ("base operand of %<->%> is not a pointer");
1616 return error_mark_node
;
1619 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1620 already been checked out to be of aggregate type. */
1623 build_m_component_ref (tree datum
, tree component
, tsubst_flags_t complain
)
1631 if (error_operand_p (datum
) || error_operand_p (component
))
1632 return error_mark_node
;
1634 datum
= mark_lvalue_use (datum
);
1635 component
= mark_rvalue_use (component
);
1637 ptrmem_type
= TREE_TYPE (component
);
1638 if (!TYPE_PTRMEM_P (ptrmem_type
))
1640 if (complain
& tf_error
)
1641 error ("%qE cannot be used as a member pointer, since it is of "
1642 "type %qT", component
, ptrmem_type
);
1643 return error_mark_node
;
1646 objtype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
1647 if (! MAYBE_CLASS_TYPE_P (objtype
))
1649 if (complain
& tf_error
)
1650 error ("cannot apply member pointer %qE to %qE, which is of "
1651 "non-class type %qT", component
, datum
, objtype
);
1652 return error_mark_node
;
1655 type
= TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type
);
1656 ctype
= complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type
));
1658 if (!COMPLETE_TYPE_P (ctype
))
1660 if (!same_type_p (ctype
, objtype
))
1666 binfo
= lookup_base (objtype
, ctype
, ba_check
, NULL
, complain
);
1671 if (complain
& tf_error
)
1672 error ("pointer to member type %qT incompatible with object "
1673 "type %qT", type
, objtype
);
1674 return error_mark_node
;
1676 else if (binfo
== error_mark_node
)
1677 return error_mark_node
;
1680 if (TYPE_PTRDATAMEM_P (ptrmem_type
))
1682 cp_lvalue_kind kind
= lvalue_kind (datum
);
1685 /* Compute the type of the field, as described in [expr.ref].
1686 There's no such thing as a mutable pointer-to-member, so
1687 things are not as complex as they are for references to
1688 non-static data members. */
1689 type
= cp_build_qualified_type (type
,
1690 (cp_type_quals (type
)
1691 | cp_type_quals (TREE_TYPE (datum
))));
1693 datum
= build_address (datum
);
1695 /* Convert object to the correct base. */
1698 datum
= build_base_path (PLUS_EXPR
, datum
, binfo
, 1, complain
);
1699 if (datum
== error_mark_node
)
1700 return error_mark_node
;
1703 /* Build an expression for "object + offset" where offset is the
1704 value stored in the pointer-to-data-member. */
1705 ptype
= build_pointer_type (type
);
1706 datum
= fold_build_pointer_plus (fold_convert (ptype
, datum
), component
);
1707 datum
= cp_build_indirect_ref (datum
, RO_NULL
, complain
);
1708 if (datum
== error_mark_node
)
1709 return error_mark_node
;
1711 /* If the object expression was an rvalue, return an rvalue. */
1712 if (kind
& clk_class
)
1713 datum
= rvalue (datum
);
1714 else if (kind
& clk_rvalueref
)
1715 datum
= move (datum
);
1720 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
1721 program is ill-formed if the second operand is a pointer to member
1722 function with ref-qualifier &. In a .* expression whose object
1723 expression is an lvalue, the program is ill-formed if the second
1724 operand is a pointer to member function with ref-qualifier &&. */
1725 if (FUNCTION_REF_QUALIFIED (type
))
1727 bool lval
= real_lvalue_p (datum
);
1728 if (lval
&& FUNCTION_RVALUE_QUALIFIED (type
))
1730 if (complain
& tf_error
)
1731 error ("pointer-to-member-function type %qT requires an rvalue",
1733 return error_mark_node
;
1735 else if (!lval
&& !FUNCTION_RVALUE_QUALIFIED (type
))
1737 if (complain
& tf_error
)
1738 error ("pointer-to-member-function type %qT requires an lvalue",
1740 return error_mark_node
;
1743 return build2 (OFFSET_REF
, type
, datum
, component
);
1747 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1750 build_functional_cast (tree exp
, tree parms
, tsubst_flags_t complain
)
1752 /* This is either a call to a constructor,
1753 or a C cast in C++'s `functional' notation. */
1755 /* The type to which we are casting. */
1757 vec
<tree
, va_gc
> *parmvec
;
1759 if (exp
== error_mark_node
|| parms
== error_mark_node
)
1760 return error_mark_node
;
1762 if (TREE_CODE (exp
) == TYPE_DECL
)
1763 type
= TREE_TYPE (exp
);
1767 /* We need to check this explicitly, since value-initialization of
1768 arrays is allowed in other situations. */
1769 if (TREE_CODE (type
) == ARRAY_TYPE
)
1771 if (complain
& tf_error
)
1772 error ("functional cast to array type %qT", type
);
1773 return error_mark_node
;
1776 if (type_uses_auto (type
))
1778 if (complain
& tf_error
)
1779 error ("invalid use of %<auto%>");
1780 return error_mark_node
;
1783 if (processing_template_decl
)
1787 /* Diagnose this even in a template. We could also try harder
1788 to give all the usual errors when the type and args are
1790 if (TREE_CODE (type
) == REFERENCE_TYPE
&& !parms
)
1792 if (complain
& tf_error
)
1793 error ("invalid value-initialization of reference type");
1794 return error_mark_node
;
1797 t
= build_min (CAST_EXPR
, type
, parms
);
1798 /* We don't know if it will or will not have side effects. */
1799 TREE_SIDE_EFFECTS (t
) = 1;
1803 if (! MAYBE_CLASS_TYPE_P (type
))
1805 if (parms
== NULL_TREE
)
1807 if (VOID_TYPE_P (type
))
1808 return void_zero_node
;
1809 return build_value_init (cv_unqualified (type
), complain
);
1812 /* This must build a C cast. */
1813 parms
= build_x_compound_expr_from_list (parms
, ELK_FUNC_CAST
, complain
);
1814 return cp_build_c_cast (type
, parms
, complain
);
1817 /* Prepare to evaluate as a call to a constructor. If this expression
1818 is actually used, for example,
1820 return X (arg1, arg2, ...);
1822 then the slot being initialized will be filled in. */
1824 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
1825 return error_mark_node
;
1826 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
1827 return error_mark_node
;
1831 If the expression list is a single-expression, the type
1832 conversion is equivalent (in definedness, and if defined in
1833 meaning) to the corresponding cast expression. */
1834 if (parms
&& TREE_CHAIN (parms
) == NULL_TREE
)
1835 return cp_build_c_cast (type
, TREE_VALUE (parms
), complain
);
1839 The expression T(), where T is a simple-type-specifier for a
1840 non-array complete object type or the (possibly cv-qualified)
1841 void type, creates an rvalue of the specified type, which is
1842 value-initialized. */
1844 if (parms
== NULL_TREE
)
1846 exp
= build_value_init (type
, complain
);
1847 exp
= get_target_expr_sfinae (exp
, complain
);
1851 /* Call the constructor. */
1852 parmvec
= make_tree_vector ();
1853 for (; parms
!= NULL_TREE
; parms
= TREE_CHAIN (parms
))
1854 vec_safe_push (parmvec
, TREE_VALUE (parms
));
1855 exp
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
1856 &parmvec
, type
, LOOKUP_NORMAL
, complain
);
1857 release_tree_vector (parmvec
);
1859 if (exp
== error_mark_node
)
1860 return error_mark_node
;
1862 return build_cplus_new (type
, exp
, complain
);
1866 /* Add new exception specifier SPEC, to the LIST we currently have.
1867 If it's already in LIST then do nothing.
1868 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1869 know what we're doing. */
1872 add_exception_specifier (tree list
, tree spec
, int complain
)
1877 diagnostic_t diag_type
= DK_UNSPECIFIED
; /* none */
1879 if (spec
== error_mark_node
)
1882 gcc_assert (spec
&& (!list
|| TREE_VALUE (list
)));
1884 /* [except.spec] 1, type in an exception specifier shall not be
1885 incomplete, or pointer or ref to incomplete other than pointer
1887 is_ptr
= TYPE_PTR_P (core
);
1888 if (is_ptr
|| TREE_CODE (core
) == REFERENCE_TYPE
)
1889 core
= TREE_TYPE (core
);
1892 else if (VOID_TYPE_P (core
))
1894 else if (TREE_CODE (core
) == TEMPLATE_TYPE_PARM
)
1896 else if (processing_template_decl
)
1901 /* 15.4/1 says that types in an exception specifier must be complete,
1902 but it seems more reasonable to only require this on definitions
1903 and calls. So just give a pedwarn at this point; we will give an
1904 error later if we hit one of those two cases. */
1905 if (!COMPLETE_TYPE_P (complete_type (core
)))
1906 diag_type
= DK_PEDWARN
; /* pedwarn */
1913 for (probe
= list
; probe
; probe
= TREE_CHAIN (probe
))
1914 if (same_type_p (TREE_VALUE (probe
), spec
))
1917 list
= tree_cons (NULL_TREE
, spec
, list
);
1920 diag_type
= DK_ERROR
; /* error */
1922 if (diag_type
!= DK_UNSPECIFIED
1923 && (complain
& tf_warning_or_error
))
1924 cxx_incomplete_type_diagnostic (NULL_TREE
, core
, diag_type
);
1929 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
1932 nothrow_spec_p_uninst (const_tree spec
)
1934 if (DEFERRED_NOEXCEPT_SPEC_P (spec
))
1936 return nothrow_spec_p (spec
);
1939 /* Combine the two exceptions specifier lists LIST and ADD, and return
1940 their union. If FN is non-null, it's the source of ADD. */
1943 merge_exception_specifiers (tree list
, tree add
, tree fn
)
1945 tree noex
, orig_list
;
1947 /* No exception-specifier or noexcept(false) are less strict than
1948 anything else. Prefer the newer variant (LIST). */
1949 if (!list
|| list
== noexcept_false_spec
)
1951 else if (!add
|| add
== noexcept_false_spec
)
1954 /* noexcept(true) and throw() are stricter than anything else.
1955 As above, prefer the more recent one (LIST). */
1956 if (nothrow_spec_p_uninst (add
))
1959 noex
= TREE_PURPOSE (list
);
1960 if (DEFERRED_NOEXCEPT_SPEC_P (add
))
1962 /* If ADD is a deferred noexcept, we must have been called from
1963 process_subob_fn. For implicitly declared functions, we build up
1964 a list of functions to consider at instantiation time. */
1965 if (noex
&& operand_equal_p (noex
, boolean_true_node
, 0))
1967 gcc_assert (fn
&& (!noex
|| is_overloaded_fn (noex
)));
1968 noex
= build_overload (fn
, noex
);
1970 else if (nothrow_spec_p_uninst (list
))
1973 gcc_checking_assert (!TREE_PURPOSE (add
)
1974 || cp_tree_equal (noex
, TREE_PURPOSE (add
)));
1976 /* Combine the dynamic-exception-specifiers, if any. */
1978 for (; add
&& TREE_VALUE (add
); add
= TREE_CHAIN (add
))
1980 tree spec
= TREE_VALUE (add
);
1983 for (probe
= orig_list
; probe
&& TREE_VALUE (probe
);
1984 probe
= TREE_CHAIN (probe
))
1985 if (same_type_p (TREE_VALUE (probe
), spec
))
1989 spec
= build_tree_list (NULL_TREE
, spec
);
1990 TREE_CHAIN (spec
) = list
;
1995 /* Keep the noexcept-specifier at the beginning of the list. */
1996 if (noex
!= TREE_PURPOSE (list
))
1997 list
= tree_cons (noex
, TREE_VALUE (list
), TREE_CHAIN (list
));
2002 /* Subroutine of build_call. Ensure that each of the types in the
2003 exception specification is complete. Technically, 15.4/1 says that
2004 they need to be complete when we see a declaration of the function,
2005 but we should be able to get away with only requiring this when the
2006 function is defined or called. See also add_exception_specifier. */
2009 require_complete_eh_spec_types (tree fntype
, tree decl
)
2012 /* Don't complain about calls to op new. */
2013 if (decl
&& DECL_ARTIFICIAL (decl
))
2015 for (raises
= TYPE_RAISES_EXCEPTIONS (fntype
); raises
;
2016 raises
= TREE_CHAIN (raises
))
2018 tree type
= TREE_VALUE (raises
);
2019 if (type
&& !COMPLETE_TYPE_P (type
))
2023 ("call to function %qD which throws incomplete type %q#T",
2026 error ("call to function which throws incomplete type %q#T",
2033 #include "gt-cp-typeck2.h"