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
)
779 || DECL_IN_AGGR_P (decl
))
781 /* Diagnose a non-constant initializer for constexpr. */
782 if (processing_template_decl
783 && !require_potential_constant_expression (value
))
784 value
= error_mark_node
;
786 value
= cxx_constant_value (value
);
788 const_init
= (reduced_constant_expression_p (value
)
789 || error_operand_p (value
));
790 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = const_init
;
791 TREE_CONSTANT (decl
) = const_init
&& decl_maybe_constant_var_p (decl
);
794 /* If the initializer is not a constant, fill in DECL_INITIAL with
795 the bits that are constant, and then return an expression that
796 will perform the dynamic initialization. */
797 if (value
!= error_mark_node
798 && (TREE_SIDE_EFFECTS (value
)
799 || array_of_runtime_bound_p (type
)
800 || ! reduced_constant_expression_p (value
)))
802 if (TREE_CODE (type
) == ARRAY_TYPE
803 && (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type
))
804 || array_of_runtime_bound_p (type
)))
805 /* For an array, we only need/want a single cleanup region rather
806 than one per element. */
807 return build_vec_init (decl
, NULL_TREE
, value
, false, 1,
808 tf_warning_or_error
);
810 return split_nonconstant_init (decl
, value
);
812 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
813 is an automatic variable, the middle end will turn this into a
814 dynamic initialization later. */
815 DECL_INITIAL (decl
) = value
;
820 /* Give errors about narrowing conversions within { }. */
823 check_narrowing (tree type
, tree init
)
825 tree ftype
= unlowered_expr_type (init
);
829 if (!warn_narrowing
|| !ARITHMETIC_TYPE_P (type
))
832 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
833 && TREE_CODE (type
) == COMPLEX_TYPE
)
835 tree elttype
= TREE_TYPE (type
);
836 check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 0)->value
);
837 if (CONSTRUCTOR_NELTS (init
) > 1)
838 check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 1)->value
);
842 init
= maybe_constant_value (init
);
844 if (TREE_CODE (type
) == INTEGER_TYPE
845 && TREE_CODE (ftype
) == REAL_TYPE
)
847 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
848 && CP_INTEGRAL_TYPE_P (type
))
850 if (TREE_CODE (ftype
) == ENUMERAL_TYPE
)
851 /* Check for narrowing based on the values of the enumeration. */
852 ftype
= ENUM_UNDERLYING_TYPE (ftype
);
853 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type
),
854 TYPE_MAX_VALUE (ftype
))
855 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype
),
856 TYPE_MIN_VALUE (type
)))
857 && (TREE_CODE (init
) != INTEGER_CST
858 || !int_fits_type_p (init
, type
)))
861 else if (TREE_CODE (ftype
) == REAL_TYPE
862 && TREE_CODE (type
) == REAL_TYPE
)
864 if (TYPE_PRECISION (type
) < TYPE_PRECISION (ftype
))
866 if (TREE_CODE (init
) == REAL_CST
)
868 /* Issue 703: Loss of precision is OK as long as the value is
869 within the representable range of the new type. */
871 d
= TREE_REAL_CST (init
);
872 real_convert (&r
, TYPE_MODE (type
), &d
);
880 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
881 && TREE_CODE (type
) == REAL_TYPE
)
884 if (TREE_CODE (init
) == INTEGER_CST
)
886 d
= real_value_from_int_cst (0, init
);
887 if (exact_real_truncate (TYPE_MODE (type
), &d
))
894 if (cxx_dialect
>= cxx11
)
895 pedwarn (EXPR_LOC_OR_HERE (init
), OPT_Wnarrowing
,
896 "narrowing conversion of %qE from %qT to %qT inside { }",
899 warning_at (EXPR_LOC_OR_HERE (init
), OPT_Wnarrowing
,
900 "narrowing conversion of %qE from %qT to %qT inside { } "
901 "is ill-formed in C++11", init
, ftype
, type
);
905 /* Process the initializer INIT for a variable of type TYPE, emitting
906 diagnostics for invalid initializers and converting the initializer as
909 For aggregate types, it assumes that reshape_init has already run, thus the
910 initializer will have the right shape (brace elision has been undone).
912 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
915 digest_init_r (tree type
, tree init
, bool nested
, int flags
,
916 tsubst_flags_t complain
)
918 enum tree_code code
= TREE_CODE (type
);
920 if (error_operand_p (init
))
921 return error_mark_node
;
925 /* We must strip the outermost array type when completing the type,
926 because the its bounds might be incomplete at the moment. */
927 if (!complete_type_or_maybe_complain (TREE_CODE (type
) == ARRAY_TYPE
928 ? TREE_TYPE (type
) : type
, NULL_TREE
,
930 return error_mark_node
;
932 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
933 (g++.old-deja/g++.law/casts2.C). */
934 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
935 init
= TREE_OPERAND (init
, 0);
937 /* Initialization of an array of chars from a string constant. The initializer
938 can be optionally enclosed in braces, but reshape_init has already removed
939 them if they were present. */
940 if (code
== ARRAY_TYPE
)
942 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
943 if (char_type_p (typ1
)
945 && TREE_CODE (init
) == STRING_CST
)
947 tree char_type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init
)));
949 if (TYPE_PRECISION (typ1
) == BITS_PER_UNIT
)
951 if (char_type
!= char_type_node
)
953 if (complain
& tf_error
)
954 error ("char-array initialized from wide string");
955 return error_mark_node
;
960 if (char_type
== char_type_node
)
962 if (complain
& tf_error
)
963 error ("int-array initialized from non-wide string");
964 return error_mark_node
;
966 else if (char_type
!= typ1
)
968 if (complain
& tf_error
)
969 error ("int-array initialized from incompatible "
971 return error_mark_node
;
975 if (type
!= TREE_TYPE (init
))
977 init
= copy_node (init
);
978 TREE_TYPE (init
) = type
;
980 if (TYPE_DOMAIN (type
) != 0 && TREE_CONSTANT (TYPE_SIZE (type
)))
982 int size
= TREE_INT_CST_LOW (TYPE_SIZE (type
));
983 size
= (size
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
;
984 /* In C it is ok to subtract 1 from the length of the string
985 because it's ok to ignore the terminating null char that is
986 counted in the length of the constant, but in C++ this would
988 if (size
< TREE_STRING_LENGTH (init
))
989 permerror (input_location
, "initializer-string for array "
990 "of chars is too long");
996 /* Handle scalar types (including conversions) and references. */
997 if ((TREE_CODE (type
) != COMPLEX_TYPE
998 || BRACE_ENCLOSED_INITIALIZER_P (init
))
999 && (SCALAR_TYPE_P (type
) || code
== REFERENCE_TYPE
))
1004 check_narrowing (type
, init
);
1005 init
= convert_for_initialization (0, type
, init
, flags
,
1006 ICR_INIT
, NULL_TREE
, 0,
1010 /* Skip any conversions since we'll be outputting the underlying
1012 while (CONVERT_EXPR_P (*exp
)
1013 || TREE_CODE (*exp
) == NON_LVALUE_EXPR
)
1014 exp
= &TREE_OPERAND (*exp
, 0);
1016 *exp
= cplus_expand_constant (*exp
);
1021 /* Come here only for aggregates: records, arrays, unions, complex numbers
1023 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
1024 || TREE_CODE (type
) == VECTOR_TYPE
1025 || TREE_CODE (type
) == RECORD_TYPE
1026 || TREE_CODE (type
) == UNION_TYPE
1027 || TREE_CODE (type
) == COMPLEX_TYPE
);
1029 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
1030 && !TYPE_NON_AGGREGATE_CLASS (type
))
1031 return process_init_constructor (type
, init
, complain
);
1034 if (COMPOUND_LITERAL_P (init
) && TREE_CODE (type
) == ARRAY_TYPE
)
1036 if (complain
& tf_error
)
1037 error ("cannot initialize aggregate of type %qT with "
1038 "a compound literal", type
);
1040 return error_mark_node
;
1043 if (TREE_CODE (type
) == ARRAY_TYPE
1044 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
1046 /* Allow the result of build_array_copy and of
1047 build_value_init_noctor. */
1048 if ((TREE_CODE (init
) == VEC_INIT_EXPR
1049 || TREE_CODE (init
) == CONSTRUCTOR
)
1050 && (same_type_ignoring_top_level_qualifiers_p
1051 (type
, TREE_TYPE (init
))))
1054 if (complain
& tf_error
)
1055 error ("array must be initialized with a brace-enclosed"
1057 return error_mark_node
;
1060 return convert_for_initialization (NULL_TREE
, type
, init
,
1062 ICR_INIT
, NULL_TREE
, 0,
1068 digest_init (tree type
, tree init
, tsubst_flags_t complain
)
1070 return digest_init_r (type
, init
, false, LOOKUP_IMPLICIT
, complain
);
1074 digest_init_flags (tree type
, tree init
, int flags
)
1076 return digest_init_r (type
, init
, false, flags
, tf_warning_or_error
);
1079 /* Set of flags used within process_init_constructor to describe the
1081 #define PICFLAG_ERRONEOUS 1
1082 #define PICFLAG_NOT_ALL_CONSTANT 2
1083 #define PICFLAG_NOT_ALL_SIMPLE 4
1085 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1089 picflag_from_initializer (tree init
)
1091 if (init
== error_mark_node
)
1092 return PICFLAG_ERRONEOUS
;
1093 else if (!TREE_CONSTANT (init
))
1094 return PICFLAG_NOT_ALL_CONSTANT
;
1095 else if (!initializer_constant_valid_p (init
, TREE_TYPE (init
)))
1096 return PICFLAG_NOT_ALL_SIMPLE
;
1100 /* Subroutine of process_init_constructor, which will process an initializer
1101 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1102 which describe the initializers. */
1105 process_init_constructor_array (tree type
, tree init
,
1106 tsubst_flags_t complain
)
1108 unsigned HOST_WIDE_INT i
, len
= 0;
1110 bool unbounded
= false;
1111 constructor_elt
*ce
;
1112 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (init
);
1114 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
1115 || TREE_CODE (type
) == VECTOR_TYPE
);
1117 if (TREE_CODE (type
) == ARRAY_TYPE
)
1119 tree domain
= TYPE_DOMAIN (type
);
1120 if (domain
&& TREE_CONSTANT (TYPE_MAX_VALUE (domain
)))
1121 len
= (tree_to_double_int (TYPE_MAX_VALUE (domain
))
1122 - tree_to_double_int (TYPE_MIN_VALUE (domain
))
1124 .ext (TYPE_PRECISION (TREE_TYPE (domain
)),
1125 TYPE_UNSIGNED (TREE_TYPE (domain
)))
1128 unbounded
= true; /* Take as many as there are. */
1131 /* Vectors are like simple fixed-size arrays. */
1132 len
= TYPE_VECTOR_SUBPARTS (type
);
1134 /* There must not be more initializers than needed. */
1135 if (!unbounded
&& vec_safe_length (v
) > len
)
1137 if (complain
& tf_error
)
1138 error ("too many initializers for %qT", type
);
1140 return PICFLAG_ERRONEOUS
;
1143 FOR_EACH_VEC_SAFE_ELT (v
, i
, ce
)
1147 gcc_assert (TREE_CODE (ce
->index
) == INTEGER_CST
);
1148 if (compare_tree_int (ce
->index
, i
) != 0)
1150 ce
->value
= error_mark_node
;
1151 sorry ("non-trivial designated initializers not supported");
1155 ce
->index
= size_int (i
);
1156 gcc_assert (ce
->value
);
1157 ce
->value
= digest_init_r (TREE_TYPE (type
), ce
->value
, true,
1158 LOOKUP_IMPLICIT
, complain
);
1160 if (ce
->value
!= error_mark_node
)
1161 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1162 (TREE_TYPE (type
), TREE_TYPE (ce
->value
)));
1164 flags
|= picflag_from_initializer (ce
->value
);
1167 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1168 we must add initializers ourselves. */
1170 for (; i
< len
; ++i
)
1174 if (type_build_ctor_call (TREE_TYPE (type
)))
1176 /* If this type needs constructors run for default-initialization,
1177 we can't rely on the back end to do it for us, so make the
1178 initialization explicit by list-initializing from {}. */
1179 next
= build_constructor (init_list_type_node
, NULL
);
1180 next
= digest_init (TREE_TYPE (type
), next
, complain
);
1182 else if (!zero_init_p (TREE_TYPE (type
)))
1183 next
= build_zero_init (TREE_TYPE (type
),
1184 /*nelts=*/NULL_TREE
,
1185 /*static_storage_p=*/false);
1187 /* The default zero-initialization is fine for us; don't
1188 add anything to the CONSTRUCTOR. */
1191 flags
|= picflag_from_initializer (next
);
1192 CONSTRUCTOR_APPEND_ELT (v
, size_int (i
), next
);
1195 CONSTRUCTOR_ELTS (init
) = v
;
1199 /* Subroutine of process_init_constructor, which will process an initializer
1200 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1201 the initializers. */
1204 process_init_constructor_record (tree type
, tree init
,
1205 tsubst_flags_t complain
)
1207 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1210 unsigned HOST_WIDE_INT idx
= 0;
1212 gcc_assert (TREE_CODE (type
) == RECORD_TYPE
);
1213 gcc_assert (!CLASSTYPE_VBASECLASSES (type
));
1214 gcc_assert (!TYPE_BINFO (type
)
1215 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)));
1216 gcc_assert (!TYPE_POLYMORPHIC_P (type
));
1218 /* Generally, we will always have an index for each initializer (which is
1219 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1220 reshape_init. So we need to handle both cases. */
1221 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1226 if (!DECL_NAME (field
) && DECL_C_BIT_FIELD (field
))
1228 flags
|= picflag_from_initializer (integer_zero_node
);
1229 CONSTRUCTOR_APPEND_ELT (v
, field
, integer_zero_node
);
1233 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
1236 /* If this is a bitfield, first convert to the declared type. */
1237 type
= TREE_TYPE (field
);
1238 if (DECL_BIT_FIELD_TYPE (field
))
1239 type
= DECL_BIT_FIELD_TYPE (field
);
1240 if (type
== error_mark_node
)
1241 return PICFLAG_ERRONEOUS
;
1243 if (idx
< vec_safe_length (CONSTRUCTOR_ELTS (init
)))
1245 constructor_elt
*ce
= &(*CONSTRUCTOR_ELTS (init
))[idx
];
1248 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1249 latter case can happen in templates where lookup has to be
1251 gcc_assert (TREE_CODE (ce
->index
) == FIELD_DECL
1252 || identifier_p (ce
->index
));
1253 if (ce
->index
!= field
1254 && ce
->index
!= DECL_NAME (field
))
1256 ce
->value
= error_mark_node
;
1257 sorry ("non-trivial designated initializers not supported");
1261 gcc_assert (ce
->value
);
1262 next
= digest_init_r (type
, ce
->value
, true,
1263 LOOKUP_IMPLICIT
, complain
);
1266 else if (type_build_ctor_call (TREE_TYPE (field
)))
1268 /* If this type needs constructors run for
1269 default-initialization, we can't rely on the back end to do it
1270 for us, so build up TARGET_EXPRs. If the type in question is
1271 a class, just build one up; if it's an array, recurse. */
1272 next
= build_constructor (init_list_type_node
, NULL
);
1273 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (field
)))
1275 next
= finish_compound_literal (TREE_TYPE (field
), next
,
1277 /* direct-initialize the target. No temporary is going
1279 if (TREE_CODE (next
) == TARGET_EXPR
)
1280 TARGET_EXPR_DIRECT_INIT_P (next
) = true;
1283 next
= digest_init_r (TREE_TYPE (field
), next
, true,
1284 LOOKUP_IMPLICIT
, complain
);
1286 /* Warn when some struct elements are implicitly initialized. */
1287 warning (OPT_Wmissing_field_initializers
,
1288 "missing initializer for member %qD", field
);
1292 if (TREE_READONLY (field
))
1294 if (complain
& tf_error
)
1295 error ("uninitialized const member %qD", field
);
1297 return PICFLAG_ERRONEOUS
;
1299 else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field
)))
1301 if (complain
& tf_error
)
1302 error ("member %qD with uninitialized const fields", field
);
1304 return PICFLAG_ERRONEOUS
;
1306 else if (TREE_CODE (TREE_TYPE (field
)) == REFERENCE_TYPE
)
1308 if (complain
& tf_error
)
1309 error ("member %qD is uninitialized reference", field
);
1311 return PICFLAG_ERRONEOUS
;
1314 /* Warn when some struct elements are implicitly initialized
1316 warning (OPT_Wmissing_field_initializers
,
1317 "missing initializer for member %qD", field
);
1319 if (!zero_init_p (TREE_TYPE (field
)))
1320 next
= build_zero_init (TREE_TYPE (field
), /*nelts=*/NULL_TREE
,
1321 /*static_storage_p=*/false);
1323 /* The default zero-initialization is fine for us; don't
1324 add anything to the CONSTRUCTOR. */
1328 /* If this is a bitfield, now convert to the lowered type. */
1329 if (type
!= TREE_TYPE (field
))
1330 next
= cp_convert_and_check (TREE_TYPE (field
), next
, complain
);
1331 flags
|= picflag_from_initializer (next
);
1332 CONSTRUCTOR_APPEND_ELT (v
, field
, next
);
1335 if (idx
< vec_safe_length (CONSTRUCTOR_ELTS (init
)))
1337 if (complain
& tf_error
)
1338 error ("too many initializers for %qT", type
);
1340 return PICFLAG_ERRONEOUS
;
1343 CONSTRUCTOR_ELTS (init
) = v
;
1347 /* Subroutine of process_init_constructor, which will process a single
1348 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1349 which describe the initializer. */
1352 process_init_constructor_union (tree type
, tree init
,
1353 tsubst_flags_t complain
)
1355 constructor_elt
*ce
;
1358 /* If the initializer was empty, use default zero initialization. */
1359 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
1362 len
= CONSTRUCTOR_ELTS (init
)->length ();
1365 if (!(complain
& tf_error
))
1366 return PICFLAG_ERRONEOUS
;
1367 error ("too many initializers for %qT", type
);
1368 CONSTRUCTOR_ELTS (init
)->block_remove (1, len
-1);
1371 ce
= &(*CONSTRUCTOR_ELTS (init
))[0];
1373 /* If this element specifies a field, initialize via that field. */
1376 if (TREE_CODE (ce
->index
) == FIELD_DECL
)
1378 else if (identifier_p (ce
->index
))
1380 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1381 tree name
= ce
->index
;
1383 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1384 if (DECL_NAME (field
) == name
)
1388 if (complain
& tf_error
)
1389 error ("no field %qD found in union being initialized",
1391 ce
->value
= error_mark_node
;
1397 gcc_assert (TREE_CODE (ce
->index
) == INTEGER_CST
1398 || TREE_CODE (ce
->index
) == RANGE_EXPR
);
1399 if (complain
& tf_error
)
1400 error ("index value instead of field name in union initializer");
1401 ce
->value
= error_mark_node
;
1406 /* Find the first named field. ANSI decided in September 1990
1407 that only named fields count here. */
1408 tree field
= TYPE_FIELDS (type
);
1409 while (field
&& (!DECL_NAME (field
) || TREE_CODE (field
) != FIELD_DECL
))
1410 field
= TREE_CHAIN (field
);
1411 if (field
== NULL_TREE
)
1413 if (complain
& tf_error
)
1414 error ("too many initializers for %qT", type
);
1415 ce
->value
= error_mark_node
;
1420 if (ce
->value
&& ce
->value
!= error_mark_node
)
1421 ce
->value
= digest_init_r (TREE_TYPE (ce
->index
), ce
->value
,
1422 true, LOOKUP_IMPLICIT
, complain
);
1424 return picflag_from_initializer (ce
->value
);
1427 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1428 constructor is a brace-enclosed initializer, and will be modified in-place.
1430 Each element is converted to the right type through digest_init, and
1431 missing initializers are added following the language rules (zero-padding,
1434 After the execution, the initializer will have TREE_CONSTANT if all elts are
1435 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1436 constants that the assembler and linker can compute them.
1438 The function returns the initializer itself, or error_mark_node in case
1442 process_init_constructor (tree type
, tree init
, tsubst_flags_t complain
)
1446 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init
));
1448 if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == VECTOR_TYPE
)
1449 flags
= process_init_constructor_array (type
, init
, complain
);
1450 else if (TREE_CODE (type
) == RECORD_TYPE
)
1451 flags
= process_init_constructor_record (type
, init
, complain
);
1452 else if (TREE_CODE (type
) == UNION_TYPE
)
1453 flags
= process_init_constructor_union (type
, init
, complain
);
1457 if (flags
& PICFLAG_ERRONEOUS
)
1458 return error_mark_node
;
1460 TREE_TYPE (init
) = type
;
1461 if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
) == NULL_TREE
)
1462 cp_complete_array_type (&TREE_TYPE (init
), init
, /*do_default=*/0);
1463 if (flags
& PICFLAG_NOT_ALL_CONSTANT
)
1464 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1465 TREE_CONSTANT (init
) = false;
1468 TREE_CONSTANT (init
) = 1;
1469 if (!(flags
& PICFLAG_NOT_ALL_SIMPLE
))
1470 TREE_STATIC (init
) = 1;
1475 /* Given a structure or union value DATUM, construct and return
1476 the structure or union component which results from narrowing
1477 that value to the base specified in BASETYPE. For example, given the
1480 class L { int ii; };
1481 class A : L { ... };
1482 class B : L { ... };
1483 class C : A, B { ... };
1491 x.A::ii refers to the ii member of the L part of
1492 the A part of the C object named by X. In this case,
1493 DATUM would be x, and BASETYPE would be A.
1495 I used to think that this was nonconformant, that the standard specified
1496 that first we look up ii in A, then convert x to an L& and pull out the
1497 ii part. But in fact, it does say that we convert x to an A&; A here
1498 is known as the "naming class". (jason 2000-12-19)
1500 BINFO_P points to a variable initialized either to NULL_TREE or to the
1501 binfo for the specific base subobject we want to convert to. */
1504 build_scoped_ref (tree datum
, tree basetype
, tree
* binfo_p
)
1508 if (datum
== error_mark_node
)
1509 return error_mark_node
;
1513 binfo
= lookup_base (TREE_TYPE (datum
), basetype
, ba_check
,
1514 NULL
, tf_warning_or_error
);
1516 if (!binfo
|| binfo
== error_mark_node
)
1518 *binfo_p
= NULL_TREE
;
1520 error_not_base_type (basetype
, TREE_TYPE (datum
));
1521 return error_mark_node
;
1525 return build_base_path (PLUS_EXPR
, datum
, binfo
, 1,
1526 tf_warning_or_error
);
1529 /* Build a reference to an object specified by the C++ `->' operator.
1530 Usually this just involves dereferencing the object, but if the
1531 `->' operator is overloaded, then such overloads must be
1532 performed until an object which does not have the `->' operator
1533 overloaded is found. An error is reported when circular pointer
1534 delegation is detected. */
1537 build_x_arrow (location_t loc
, tree expr
, tsubst_flags_t complain
)
1539 tree orig_expr
= expr
;
1540 tree type
= TREE_TYPE (expr
);
1541 tree last_rval
= NULL_TREE
;
1542 vec
<tree
, va_gc
> *types_memoized
= NULL
;
1544 if (type
== error_mark_node
)
1545 return error_mark_node
;
1547 if (processing_template_decl
)
1549 if (type_dependent_expression_p (expr
))
1550 return build_min_nt_loc (loc
, ARROW_EXPR
, expr
);
1551 expr
= build_non_dependent_expr (expr
);
1554 if (MAYBE_CLASS_TYPE_P (type
))
1556 struct tinst_level
*actual_inst
= current_instantiation ();
1559 while ((expr
= build_new_op (loc
, COMPONENT_REF
,
1560 LOOKUP_NORMAL
, expr
, NULL_TREE
, NULL_TREE
,
1563 if (expr
== error_mark_node
)
1564 return error_mark_node
;
1566 if (fn
&& DECL_USE_TEMPLATE (fn
))
1567 push_tinst_level (fn
);
1570 if (vec_member (TREE_TYPE (expr
), types_memoized
))
1572 if (complain
& tf_error
)
1573 error ("circular pointer delegation detected");
1574 return error_mark_node
;
1577 vec_safe_push (types_memoized
, TREE_TYPE (expr
));
1581 while (current_instantiation () != actual_inst
)
1584 if (last_rval
== NULL_TREE
)
1586 if (complain
& tf_error
)
1587 error ("base operand of %<->%> has non-pointer type %qT", type
);
1588 return error_mark_node
;
1591 if (TREE_CODE (TREE_TYPE (last_rval
)) == REFERENCE_TYPE
)
1592 last_rval
= convert_from_reference (last_rval
);
1595 last_rval
= decay_conversion (expr
, complain
);
1597 if (TYPE_PTR_P (TREE_TYPE (last_rval
)))
1599 if (processing_template_decl
)
1601 expr
= build_min (ARROW_EXPR
, TREE_TYPE (TREE_TYPE (last_rval
)),
1603 TREE_SIDE_EFFECTS (expr
) = TREE_SIDE_EFFECTS (last_rval
);
1607 return cp_build_indirect_ref (last_rval
, RO_NULL
, complain
);
1610 if (complain
& tf_error
)
1613 error ("result of %<operator->()%> yields non-pointer result");
1615 error ("base operand of %<->%> is not a pointer");
1617 return error_mark_node
;
1620 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1621 already been checked out to be of aggregate type. */
1624 build_m_component_ref (tree datum
, tree component
, tsubst_flags_t complain
)
1632 if (error_operand_p (datum
) || error_operand_p (component
))
1633 return error_mark_node
;
1635 datum
= mark_lvalue_use (datum
);
1636 component
= mark_rvalue_use (component
);
1638 ptrmem_type
= TREE_TYPE (component
);
1639 if (!TYPE_PTRMEM_P (ptrmem_type
))
1641 if (complain
& tf_error
)
1642 error ("%qE cannot be used as a member pointer, since it is of "
1643 "type %qT", component
, ptrmem_type
);
1644 return error_mark_node
;
1647 objtype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
1648 if (! MAYBE_CLASS_TYPE_P (objtype
))
1650 if (complain
& tf_error
)
1651 error ("cannot apply member pointer %qE to %qE, which is of "
1652 "non-class type %qT", component
, datum
, objtype
);
1653 return error_mark_node
;
1656 type
= TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type
);
1657 ctype
= complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type
));
1659 if (!COMPLETE_TYPE_P (ctype
))
1661 if (!same_type_p (ctype
, objtype
))
1667 binfo
= lookup_base (objtype
, ctype
, ba_check
, NULL
, complain
);
1672 if (complain
& tf_error
)
1673 error ("pointer to member type %qT incompatible with object "
1674 "type %qT", type
, objtype
);
1675 return error_mark_node
;
1677 else if (binfo
== error_mark_node
)
1678 return error_mark_node
;
1681 if (TYPE_PTRDATAMEM_P (ptrmem_type
))
1683 cp_lvalue_kind kind
= lvalue_kind (datum
);
1686 /* Compute the type of the field, as described in [expr.ref].
1687 There's no such thing as a mutable pointer-to-member, so
1688 things are not as complex as they are for references to
1689 non-static data members. */
1690 type
= cp_build_qualified_type (type
,
1691 (cp_type_quals (type
)
1692 | cp_type_quals (TREE_TYPE (datum
))));
1694 datum
= build_address (datum
);
1696 /* Convert object to the correct base. */
1699 datum
= build_base_path (PLUS_EXPR
, datum
, binfo
, 1, complain
);
1700 if (datum
== error_mark_node
)
1701 return error_mark_node
;
1704 /* Build an expression for "object + offset" where offset is the
1705 value stored in the pointer-to-data-member. */
1706 ptype
= build_pointer_type (type
);
1707 datum
= fold_build_pointer_plus (fold_convert (ptype
, datum
), component
);
1708 datum
= cp_build_indirect_ref (datum
, RO_NULL
, complain
);
1709 if (datum
== error_mark_node
)
1710 return error_mark_node
;
1712 /* If the object expression was an rvalue, return an rvalue. */
1713 if (kind
& clk_class
)
1714 datum
= rvalue (datum
);
1715 else if (kind
& clk_rvalueref
)
1716 datum
= move (datum
);
1721 /* 5.5/6: In a .* expression whose object expression is an rvalue, the
1722 program is ill-formed if the second operand is a pointer to member
1723 function with ref-qualifier &. In a .* expression whose object
1724 expression is an lvalue, the program is ill-formed if the second
1725 operand is a pointer to member function with ref-qualifier &&. */
1726 if (FUNCTION_REF_QUALIFIED (type
))
1728 bool lval
= real_lvalue_p (datum
);
1729 if (lval
&& FUNCTION_RVALUE_QUALIFIED (type
))
1731 if (complain
& tf_error
)
1732 error ("pointer-to-member-function type %qT requires an rvalue",
1734 return error_mark_node
;
1736 else if (!lval
&& !FUNCTION_RVALUE_QUALIFIED (type
))
1738 if (complain
& tf_error
)
1739 error ("pointer-to-member-function type %qT requires an lvalue",
1741 return error_mark_node
;
1744 return build2 (OFFSET_REF
, type
, datum
, component
);
1748 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1751 build_functional_cast (tree exp
, tree parms
, tsubst_flags_t complain
)
1753 /* This is either a call to a constructor,
1754 or a C cast in C++'s `functional' notation. */
1756 /* The type to which we are casting. */
1758 vec
<tree
, va_gc
> *parmvec
;
1760 if (exp
== error_mark_node
|| parms
== error_mark_node
)
1761 return error_mark_node
;
1763 if (TREE_CODE (exp
) == TYPE_DECL
)
1765 type
= TREE_TYPE (exp
);
1767 if (complain
& tf_warning
1768 && TREE_DEPRECATED (type
)
1769 && DECL_ARTIFICIAL (exp
))
1770 warn_deprecated_use (type
, NULL_TREE
);
1775 /* We need to check this explicitly, since value-initialization of
1776 arrays is allowed in other situations. */
1777 if (TREE_CODE (type
) == ARRAY_TYPE
)
1779 if (complain
& tf_error
)
1780 error ("functional cast to array type %qT", type
);
1781 return error_mark_node
;
1784 if (type_uses_auto (type
))
1786 if (complain
& tf_error
)
1787 error ("invalid use of %<auto%>");
1788 return error_mark_node
;
1791 if (processing_template_decl
)
1795 /* Diagnose this even in a template. We could also try harder
1796 to give all the usual errors when the type and args are
1798 if (TREE_CODE (type
) == REFERENCE_TYPE
&& !parms
)
1800 if (complain
& tf_error
)
1801 error ("invalid value-initialization of reference type");
1802 return error_mark_node
;
1805 t
= build_min (CAST_EXPR
, type
, parms
);
1806 /* We don't know if it will or will not have side effects. */
1807 TREE_SIDE_EFFECTS (t
) = 1;
1811 if (! MAYBE_CLASS_TYPE_P (type
))
1813 if (parms
== NULL_TREE
)
1815 if (VOID_TYPE_P (type
))
1816 return void_zero_node
;
1817 return build_value_init (cv_unqualified (type
), complain
);
1820 /* This must build a C cast. */
1821 parms
= build_x_compound_expr_from_list (parms
, ELK_FUNC_CAST
, complain
);
1822 return cp_build_c_cast (type
, parms
, complain
);
1825 /* Prepare to evaluate as a call to a constructor. If this expression
1826 is actually used, for example,
1828 return X (arg1, arg2, ...);
1830 then the slot being initialized will be filled in. */
1832 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
1833 return error_mark_node
;
1834 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
1835 return error_mark_node
;
1839 If the expression list is a single-expression, the type
1840 conversion is equivalent (in definedness, and if defined in
1841 meaning) to the corresponding cast expression. */
1842 if (parms
&& TREE_CHAIN (parms
) == NULL_TREE
)
1843 return cp_build_c_cast (type
, TREE_VALUE (parms
), complain
);
1847 The expression T(), where T is a simple-type-specifier for a
1848 non-array complete object type or the (possibly cv-qualified)
1849 void type, creates an rvalue of the specified type, which is
1850 value-initialized. */
1852 if (parms
== NULL_TREE
)
1854 exp
= build_value_init (type
, complain
);
1855 exp
= get_target_expr_sfinae (exp
, complain
);
1859 /* Call the constructor. */
1860 parmvec
= make_tree_vector ();
1861 for (; parms
!= NULL_TREE
; parms
= TREE_CHAIN (parms
))
1862 vec_safe_push (parmvec
, TREE_VALUE (parms
));
1863 exp
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
1864 &parmvec
, type
, LOOKUP_NORMAL
, complain
);
1865 release_tree_vector (parmvec
);
1867 if (exp
== error_mark_node
)
1868 return error_mark_node
;
1870 return build_cplus_new (type
, exp
, complain
);
1874 /* Add new exception specifier SPEC, to the LIST we currently have.
1875 If it's already in LIST then do nothing.
1876 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1877 know what we're doing. */
1880 add_exception_specifier (tree list
, tree spec
, int complain
)
1885 diagnostic_t diag_type
= DK_UNSPECIFIED
; /* none */
1887 if (spec
== error_mark_node
)
1890 gcc_assert (spec
&& (!list
|| TREE_VALUE (list
)));
1892 /* [except.spec] 1, type in an exception specifier shall not be
1893 incomplete, or pointer or ref to incomplete other than pointer
1895 is_ptr
= TYPE_PTR_P (core
);
1896 if (is_ptr
|| TREE_CODE (core
) == REFERENCE_TYPE
)
1897 core
= TREE_TYPE (core
);
1900 else if (VOID_TYPE_P (core
))
1902 else if (TREE_CODE (core
) == TEMPLATE_TYPE_PARM
)
1904 else if (processing_template_decl
)
1909 /* 15.4/1 says that types in an exception specifier must be complete,
1910 but it seems more reasonable to only require this on definitions
1911 and calls. So just give a pedwarn at this point; we will give an
1912 error later if we hit one of those two cases. */
1913 if (!COMPLETE_TYPE_P (complete_type (core
)))
1914 diag_type
= DK_PEDWARN
; /* pedwarn */
1921 for (probe
= list
; probe
; probe
= TREE_CHAIN (probe
))
1922 if (same_type_p (TREE_VALUE (probe
), spec
))
1925 list
= tree_cons (NULL_TREE
, spec
, list
);
1928 diag_type
= DK_ERROR
; /* error */
1930 if (diag_type
!= DK_UNSPECIFIED
1931 && (complain
& tf_warning_or_error
))
1932 cxx_incomplete_type_diagnostic (NULL_TREE
, core
, diag_type
);
1937 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
1940 nothrow_spec_p_uninst (const_tree spec
)
1942 if (DEFERRED_NOEXCEPT_SPEC_P (spec
))
1944 return nothrow_spec_p (spec
);
1947 /* Combine the two exceptions specifier lists LIST and ADD, and return
1948 their union. If FN is non-null, it's the source of ADD. */
1951 merge_exception_specifiers (tree list
, tree add
, tree fn
)
1953 tree noex
, orig_list
;
1955 /* No exception-specifier or noexcept(false) are less strict than
1956 anything else. Prefer the newer variant (LIST). */
1957 if (!list
|| list
== noexcept_false_spec
)
1959 else if (!add
|| add
== noexcept_false_spec
)
1962 /* noexcept(true) and throw() are stricter than anything else.
1963 As above, prefer the more recent one (LIST). */
1964 if (nothrow_spec_p_uninst (add
))
1967 noex
= TREE_PURPOSE (list
);
1968 if (DEFERRED_NOEXCEPT_SPEC_P (add
))
1970 /* If ADD is a deferred noexcept, we must have been called from
1971 process_subob_fn. For implicitly declared functions, we build up
1972 a list of functions to consider at instantiation time. */
1973 if (noex
&& operand_equal_p (noex
, boolean_true_node
, 0))
1975 gcc_assert (fn
&& (!noex
|| is_overloaded_fn (noex
)));
1976 noex
= build_overload (fn
, noex
);
1978 else if (nothrow_spec_p_uninst (list
))
1981 gcc_checking_assert (!TREE_PURPOSE (add
)
1982 || cp_tree_equal (noex
, TREE_PURPOSE (add
)));
1984 /* Combine the dynamic-exception-specifiers, if any. */
1986 for (; add
&& TREE_VALUE (add
); add
= TREE_CHAIN (add
))
1988 tree spec
= TREE_VALUE (add
);
1991 for (probe
= orig_list
; probe
&& TREE_VALUE (probe
);
1992 probe
= TREE_CHAIN (probe
))
1993 if (same_type_p (TREE_VALUE (probe
), spec
))
1997 spec
= build_tree_list (NULL_TREE
, spec
);
1998 TREE_CHAIN (spec
) = list
;
2003 /* Keep the noexcept-specifier at the beginning of the list. */
2004 if (noex
!= TREE_PURPOSE (list
))
2005 list
= tree_cons (noex
, TREE_VALUE (list
), TREE_CHAIN (list
));
2010 /* Subroutine of build_call. Ensure that each of the types in the
2011 exception specification is complete. Technically, 15.4/1 says that
2012 they need to be complete when we see a declaration of the function,
2013 but we should be able to get away with only requiring this when the
2014 function is defined or called. See also add_exception_specifier. */
2017 require_complete_eh_spec_types (tree fntype
, tree decl
)
2020 /* Don't complain about calls to op new. */
2021 if (decl
&& DECL_ARTIFICIAL (decl
))
2023 for (raises
= TYPE_RAISES_EXCEPTIONS (fntype
); raises
;
2024 raises
= TREE_CHAIN (raises
))
2026 tree type
= TREE_VALUE (raises
);
2027 if (type
&& !COMPLETE_TYPE_P (type
))
2031 ("call to function %qD which throws incomplete type %q#T",
2034 error ("call to function which throws incomplete type %q#T",
2041 #include "gt-cp-typeck2.h"