1 /* Report error messages, build initializers, and perform
2 some front-end optimizations for C++ compiler.
3 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
6 Free Software Foundation, Inc.
7 Hacked by Michael Tiemann (tiemann@cygnus.com)
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3, or (at your option)
16 GCC is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* This file is part of the C++ front end.
27 It contains routines to build C++ expressions given their operands,
28 including computing the types of the result, C and C++ specific error
29 checks, and some optimization. */
33 #include "coretypes.h"
39 #include "diagnostic-core.h"
42 process_init_constructor (tree type
, tree init
, tsubst_flags_t complain
);
45 /* Print an error message stemming from an attempt to use
46 BASETYPE as a base class for TYPE. */
49 error_not_base_type (tree basetype
, tree type
)
51 if (TREE_CODE (basetype
) == FUNCTION_DECL
)
52 basetype
= DECL_CONTEXT (basetype
);
53 error ("type %qT is not a base type for type %qT", basetype
, type
);
54 return error_mark_node
;
58 binfo_or_else (tree base
, tree type
)
60 tree binfo
= lookup_base (type
, base
, ba_unique
, NULL
);
62 if (binfo
== error_mark_node
)
65 error_not_base_type (base
, type
);
69 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
70 value may not be changed thereafter. */
73 cxx_readonly_error (tree arg
, enum lvalue_use errstring
)
76 /* This macro is used to emit diagnostics to ensure that all format
77 strings are complete sentences, visible to gettext and checked at
80 #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG) \
101 /* Handle C++-specific things first. */
103 if (TREE_CODE (arg
) == VAR_DECL
104 && DECL_LANG_SPECIFIC (arg
)
105 && DECL_IN_AGGR_P (arg
)
106 && !TREE_STATIC (arg
))
107 ERROR_FOR_ASSIGNMENT (G_("assignment of "
108 "constant field %qD"),
109 G_("constant field %qD "
110 "used as %<asm%> output"),
112 "constant field %qD"),
114 "constant field %qD"),
116 else if (TREE_CODE (arg
) == INDIRECT_REF
117 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 0))) == REFERENCE_TYPE
118 && (TREE_CODE (TREE_OPERAND (arg
, 0)) == VAR_DECL
119 || TREE_CODE (TREE_OPERAND (arg
, 0)) == PARM_DECL
))
120 ERROR_FOR_ASSIGNMENT (G_("assignment of "
121 "read-only reference %qD"),
122 G_("read-only reference %qD "
123 "used as %<asm%> output"),
125 "read-only reference %qD"),
127 "read-only reference %qD"),
128 TREE_OPERAND (arg
, 0));
130 readonly_error (arg
, errstring
);
134 /* Structure that holds information about declarations whose type was
135 incomplete and we could not check whether it was abstract or not. */
137 struct GTY((chain_next ("%h.next"))) pending_abstract_type
{
138 /* Declaration which we are checking for abstractness. It is either
139 a DECL node, or an IDENTIFIER_NODE if we do not have a full
140 declaration available. */
143 /* Type which will be checked for abstractness. */
146 /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
147 because DECLs already carry locus information. */
150 /* Link to the next element in list. */
151 struct pending_abstract_type
* next
;
155 /* Compute the hash value of the node VAL. This function is used by the
156 hash table abstract_pending_vars. */
159 pat_calc_hash (const void* val
)
161 const struct pending_abstract_type
*pat
=
162 (const struct pending_abstract_type
*) val
;
163 return (hashval_t
) TYPE_UID (pat
->type
);
167 /* Compare node VAL1 with the type VAL2. This function is used by the
168 hash table abstract_pending_vars. */
171 pat_compare (const void* val1
, const void* val2
)
173 const struct pending_abstract_type
*const pat1
=
174 (const struct pending_abstract_type
*) val1
;
175 const_tree
const type2
= (const_tree
)val2
;
177 return (pat1
->type
== type2
);
180 /* Hash table that maintains pending_abstract_type nodes, for which we still
181 need to check for type abstractness. The key of the table is the type
182 of the declaration. */
183 static GTY ((param_is (struct pending_abstract_type
)))
184 htab_t abstract_pending_vars
= NULL
;
187 /* This function is called after TYPE is completed, and will check if there
188 are pending declarations for which we still need to verify the abstractness
189 of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
190 turned out to be incomplete. */
193 complete_type_check_abstract (tree type
)
196 struct pending_abstract_type
*pat
;
197 location_t cur_loc
= input_location
;
199 gcc_assert (COMPLETE_TYPE_P (type
));
201 if (!abstract_pending_vars
)
204 /* Retrieve the list of pending declarations for this type. */
205 slot
= htab_find_slot_with_hash (abstract_pending_vars
, type
,
206 (hashval_t
)TYPE_UID (type
), NO_INSERT
);
209 pat
= (struct pending_abstract_type
*)*slot
;
212 /* If the type is not abstract, do not do anything. */
213 if (CLASSTYPE_PURE_VIRTUALS (type
))
215 struct pending_abstract_type
*prev
= 0, *next
;
217 /* Reverse the list to emit the errors in top-down order. */
218 for (; pat
; pat
= next
)
226 /* Go through the list, and call abstract_virtuals_error for each
227 element: it will issue a diagnostic if the type is abstract. */
230 gcc_assert (type
== pat
->type
);
232 /* Tweak input_location so that the diagnostic appears at the correct
233 location. Notice that this is only needed if the decl is an
235 input_location
= pat
->locus
;
236 abstract_virtuals_error (pat
->decl
, pat
->type
);
241 htab_clear_slot (abstract_pending_vars
, slot
);
243 input_location
= cur_loc
;
247 /* If TYPE has abstract virtual functions, issue an error about trying
248 to create an object of that type. DECL is the object declared, or
249 NULL_TREE if the declaration is unavailable. Returns 1 if an error
250 occurred; zero if all was well. */
253 abstract_virtuals_error_sfinae (tree decl
, tree type
, tsubst_flags_t complain
)
257 /* This function applies only to classes. Any other entity can never
259 if (!CLASS_TYPE_P (type
))
261 type
= TYPE_MAIN_VARIANT (type
);
263 /* If the type is incomplete, we register it within a hash table,
264 so that we can check again once it is completed. This makes sense
265 only for objects for which we have a declaration or at least a
267 if (!COMPLETE_TYPE_P (type
))
270 struct pending_abstract_type
*pat
;
272 gcc_assert (!decl
|| DECL_P (decl
)
273 || TREE_CODE (decl
) == IDENTIFIER_NODE
);
275 if (!abstract_pending_vars
)
276 abstract_pending_vars
= htab_create_ggc (31, &pat_calc_hash
,
279 slot
= htab_find_slot_with_hash (abstract_pending_vars
, type
,
280 (hashval_t
)TYPE_UID (type
), INSERT
);
282 pat
= ggc_alloc_pending_abstract_type ();
285 pat
->locus
= ((decl
&& DECL_P (decl
))
286 ? DECL_SOURCE_LOCATION (decl
)
289 pat
->next
= (struct pending_abstract_type
*) *slot
;
295 if (!TYPE_SIZE (type
))
296 /* TYPE is being defined, and during that time
297 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
300 pure
= CLASSTYPE_PURE_VIRTUALS (type
);
304 if (!(complain
& tf_error
))
309 if (TREE_CODE (decl
) == VAR_DECL
)
310 error ("cannot declare variable %q+D to be of abstract "
311 "type %qT", decl
, type
);
312 else if (TREE_CODE (decl
) == PARM_DECL
)
313 error ("cannot declare parameter %q+D to be of abstract type %qT",
315 else if (TREE_CODE (decl
) == FIELD_DECL
)
316 error ("cannot declare field %q+D to be of abstract type %qT",
318 else if (TREE_CODE (decl
) == FUNCTION_DECL
319 && TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
320 error ("invalid abstract return type for member function %q+#D", decl
);
321 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
322 error ("invalid abstract return type for function %q+#D", decl
);
323 else if (TREE_CODE (decl
) == IDENTIFIER_NODE
)
324 /* Here we do not have location information. */
325 error ("invalid abstract type %qT for %qE", type
, decl
);
327 error ("invalid abstract type for %q+D", decl
);
330 error ("cannot allocate an object of abstract type %qT", type
);
332 /* Only go through this once. */
333 if (VEC_length (tree
, pure
))
338 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
339 " because the following virtual functions are pure within %qT:",
342 FOR_EACH_VEC_ELT (tree
, pure
, ix
, fn
)
343 if (! DECL_CLONED_FUNCTION_P (fn
)
344 || DECL_COMPLETE_DESTRUCTOR_P (fn
))
345 inform (input_location
, "\t%+#D", fn
);
347 /* Now truncate the vector. This leaves it non-null, so we know
348 there are pure virtuals, but empty so we don't list them out
350 VEC_truncate (tree
, pure
, 0);
353 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
354 " since type %qT has pure virtual functions",
360 /* Wrapper for the above function in the common case of wanting errors. */
363 abstract_virtuals_error (tree decl
, tree type
)
365 return abstract_virtuals_error_sfinae (decl
, type
, tf_warning_or_error
);
368 /* Print an error message for invalid use of an incomplete type.
369 VALUE is the expression that was used (or 0 if that isn't known)
370 and TYPE is the type that was invalid. DIAG_KIND indicates the
371 type of diagnostic (see diagnostic.def). */
374 cxx_incomplete_type_diagnostic (const_tree value
, const_tree type
,
375 diagnostic_t diag_kind
)
379 gcc_assert (diag_kind
== DK_WARNING
380 || diag_kind
== DK_PEDWARN
381 || diag_kind
== DK_ERROR
);
383 /* Avoid duplicate error message. */
384 if (TREE_CODE (type
) == ERROR_MARK
)
387 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
388 || TREE_CODE (value
) == PARM_DECL
389 || TREE_CODE (value
) == FIELD_DECL
))
391 emit_diagnostic (diag_kind
, input_location
, 0,
392 "%q+D has incomplete type", value
);
396 /* We must print an error message. Be clever about what it says. */
398 switch (TREE_CODE (type
))
404 emit_diagnostic (diag_kind
, input_location
, 0,
405 "invalid use of incomplete type %q#T", type
);
406 if (!TYPE_TEMPLATE_INFO (type
))
407 emit_diagnostic (diag_kind
, input_location
, 0,
408 "forward declaration of %q+#T", type
);
410 emit_diagnostic (diag_kind
, input_location
, 0,
411 "declaration of %q+#T", type
);
415 emit_diagnostic (diag_kind
, input_location
, 0,
416 "invalid use of %qT", type
);
420 if (TYPE_DOMAIN (type
))
422 type
= TREE_TYPE (type
);
425 emit_diagnostic (diag_kind
, input_location
, 0,
426 "invalid use of array with unspecified bounds");
432 tree member
= TREE_OPERAND (value
, 1);
433 if (is_overloaded_fn (member
))
434 member
= get_first_fn (member
);
435 if (DECL_FUNCTION_MEMBER_P (member
)
436 && ! flag_ms_extensions
)
437 emit_diagnostic (diag_kind
, input_location
, 0,
438 "invalid use of member function "
439 "(did you forget the %<()%> ?)");
441 emit_diagnostic (diag_kind
, input_location
, 0,
442 "invalid use of member "
443 "(did you forget the %<&%> ?)");
447 case TEMPLATE_TYPE_PARM
:
449 emit_diagnostic (diag_kind
, input_location
, 0,
450 "invalid use of %<auto%>");
452 emit_diagnostic (diag_kind
, input_location
, 0,
453 "invalid use of template type parameter %qT", type
);
456 case BOUND_TEMPLATE_TEMPLATE_PARM
:
457 emit_diagnostic (diag_kind
, input_location
, 0,
458 "invalid use of template template parameter %qT",
463 emit_diagnostic (diag_kind
, input_location
, 0,
464 "invalid use of dependent type %qT", type
);
468 if (type
== init_list_type_node
)
470 emit_diagnostic (diag_kind
, input_location
, 0,
471 "invalid use of brace-enclosed initializer list");
474 gcc_assert (type
== unknown_type_node
);
475 if (value
&& TREE_CODE (value
) == COMPONENT_REF
)
477 else if (value
&& TREE_CODE (value
) == ADDR_EXPR
)
478 emit_diagnostic (diag_kind
, input_location
, 0,
479 "address of overloaded function with no contextual "
481 else if (value
&& TREE_CODE (value
) == OVERLOAD
)
482 emit_diagnostic (diag_kind
, input_location
, 0,
483 "overloaded function with no contextual type information");
485 emit_diagnostic (diag_kind
, input_location
, 0,
486 "insufficient contextual information to determine type");
494 /* Backward-compatibility interface to incomplete_type_diagnostic;
495 required by ../tree.c. */
496 #undef cxx_incomplete_type_error
498 cxx_incomplete_type_error (const_tree value
, const_tree type
)
500 cxx_incomplete_type_diagnostic (value
, type
, DK_ERROR
);
504 /* The recursive part of split_nonconstant_init. DEST is an lvalue
505 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
506 Return true if the whole of the value was initialized by the
507 generated statements. */
510 split_nonconstant_init_1 (tree dest
, tree init
)
512 unsigned HOST_WIDE_INT idx
;
513 tree field_index
, value
;
514 tree type
= TREE_TYPE (dest
);
515 tree inner_type
= NULL
;
516 bool array_type_p
= false;
517 bool complete_p
= true;
518 HOST_WIDE_INT num_split_elts
= 0;
520 switch (TREE_CODE (type
))
523 inner_type
= TREE_TYPE (type
);
529 case QUAL_UNION_TYPE
:
530 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init
), idx
,
533 /* The current implementation of this algorithm assumes that
534 the field was set for all the elements. This is usually done
535 by process_init_constructor. */
536 gcc_assert (field_index
);
539 inner_type
= TREE_TYPE (field_index
);
541 if (TREE_CODE (value
) == CONSTRUCTOR
)
546 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
547 NULL_TREE
, NULL_TREE
);
549 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
552 if (!split_nonconstant_init_1 (sub
, value
))
556 else if (!initializer_constant_valid_p (value
, inner_type
))
561 /* FIXME: Ordered removal is O(1) so the whole function is
562 worst-case quadratic. This could be fixed using an aside
563 bitmap to record which elements must be removed and remove
564 them all at the same time. Or by merging
565 split_non_constant_init into process_init_constructor_array,
566 that is separating constants from non-constants while building
568 VEC_ordered_remove (constructor_elt
, CONSTRUCTOR_ELTS (init
),
573 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
574 NULL_TREE
, NULL_TREE
);
576 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
579 code
= build2 (INIT_EXPR
, inner_type
, sub
, value
);
580 code
= build_stmt (input_location
, EXPR_STMT
, code
);
581 code
= maybe_cleanup_point_expr_void (code
);
583 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (inner_type
))
585 code
= (build_special_member_call
586 (sub
, complete_dtor_identifier
, NULL
, inner_type
,
587 LOOKUP_NORMAL
, tf_warning_or_error
));
588 finish_eh_cleanup (code
);
597 if (!initializer_constant_valid_p (init
, type
))
600 tree cons
= copy_node (init
);
601 CONSTRUCTOR_ELTS (init
) = NULL
;
602 code
= build2 (MODIFY_EXPR
, type
, dest
, cons
);
603 code
= build_stmt (input_location
, EXPR_STMT
, code
);
605 num_split_elts
+= CONSTRUCTOR_NELTS (init
);
613 /* The rest of the initializer is now a constant. */
614 TREE_CONSTANT (init
) = 1;
615 return complete_p
&& complete_ctor_at_level_p (TREE_TYPE (init
),
616 num_split_elts
, inner_type
);
619 /* A subroutine of store_init_value. Splits non-constant static
620 initializer INIT into a constant part and generates code to
621 perform the non-constant part of the initialization to DEST.
622 Returns the code for the runtime init. */
625 split_nonconstant_init (tree dest
, tree init
)
629 if (TREE_CODE (init
) == CONSTRUCTOR
)
631 code
= push_stmt_list ();
632 if (split_nonconstant_init_1 (dest
, init
))
634 code
= pop_stmt_list (code
);
635 DECL_INITIAL (dest
) = init
;
636 TREE_READONLY (dest
) = 0;
639 code
= build2 (INIT_EXPR
, TREE_TYPE (dest
), dest
, init
);
644 /* Perform appropriate conversions on the initial value of a variable,
645 store it in the declaration DECL,
646 and print any error messages that are appropriate.
647 If the init is invalid, store an ERROR_MARK.
649 C++: Note that INIT might be a TREE_LIST, which would mean that it is
650 a base class initializer for some aggregate type, hopefully compatible
651 with DECL. If INIT is a single element, and DECL is an aggregate
652 type, we silently convert INIT into a TREE_LIST, allowing a constructor
655 If INIT is a TREE_LIST and there is no constructor, turn INIT
656 into a CONSTRUCTOR and use standard initialization techniques.
657 Perhaps a warning should be generated?
659 Returns code to be executed if initialization could not be performed
660 for static variable. In that case, caller must emit the code. */
663 store_init_value (tree decl
, tree init
, VEC(tree
,gc
)** cleanups
, int flags
)
667 /* If variable's type was invalidly declared, just ignore it. */
669 type
= TREE_TYPE (decl
);
670 if (TREE_CODE (type
) == ERROR_MARK
)
673 if (MAYBE_CLASS_TYPE_P (type
))
675 if (TREE_CODE (init
) == TREE_LIST
)
677 error ("constructor syntax used, but no constructor declared "
678 "for type %qT", type
);
679 init
= build_constructor_from_list (init_list_type_node
, nreverse (init
));
682 else if (TREE_CODE (init
) == TREE_LIST
683 && TREE_TYPE (init
) != unknown_type_node
)
685 gcc_assert (TREE_CODE (decl
) != RESULT_DECL
);
687 if (TREE_CODE (init
) == TREE_LIST
688 && TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
690 error ("cannot initialize arrays using this syntax");
694 /* We get here with code like `int a (2);' */
695 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
696 tf_warning_or_error
);
699 /* End of special C++ code. */
701 if (flags
& LOOKUP_ALREADY_DIGESTED
)
704 /* Digest the specified initializer into an expression. */
705 value
= digest_init_flags (type
, init
, flags
);
707 value
= extend_ref_init_temps (decl
, value
, cleanups
);
709 /* In C++0x constant expression is a semantic, not syntactic, property.
710 In C++98, make sure that what we thought was a constant expression at
711 template definition time is still constant. */
712 if ((cxx_dialect
>= cxx0x
713 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
))
714 && (decl_maybe_constant_var_p (decl
)
715 || TREE_STATIC (decl
)))
718 value
= fold_non_dependent_expr (value
);
719 value
= maybe_constant_init (value
);
720 if (DECL_DECLARED_CONSTEXPR_P (decl
))
722 /* Diagnose a non-constant initializer for constexpr. */
723 if (processing_template_decl
724 && !require_potential_constant_expression (value
))
725 value
= error_mark_node
;
727 value
= cxx_constant_value (value
);
729 const_init
= (reduced_constant_expression_p (value
)
730 || error_operand_p (value
));
731 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = const_init
;
732 TREE_CONSTANT (decl
) = const_init
&& decl_maybe_constant_var_p (decl
);
735 /* If the initializer is not a constant, fill in DECL_INITIAL with
736 the bits that are constant, and then return an expression that
737 will perform the dynamic initialization. */
738 if (value
!= error_mark_node
739 && (TREE_SIDE_EFFECTS (value
)
740 || ! initializer_constant_valid_p (value
, TREE_TYPE (value
))))
742 if (TREE_CODE (type
) == ARRAY_TYPE
743 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type
)))
744 /* For an array, we only need/want a single cleanup region rather
745 than one per element. */
746 return build_vec_init (decl
, NULL_TREE
, value
, false, 1,
747 tf_warning_or_error
);
749 return split_nonconstant_init (decl
, value
);
751 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
752 is an automatic variable, the middle end will turn this into a
753 dynamic initialization later. */
754 DECL_INITIAL (decl
) = value
;
759 /* Give errors about narrowing conversions within { }. */
762 check_narrowing (tree type
, tree init
)
764 tree ftype
= unlowered_expr_type (init
);
768 if (!warn_narrowing
|| !ARITHMETIC_TYPE_P (type
))
771 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
772 && TREE_CODE (type
) == COMPLEX_TYPE
)
774 tree elttype
= TREE_TYPE (type
);
775 check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 0)->value
);
776 if (CONSTRUCTOR_NELTS (init
) > 1)
777 check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 1)->value
);
781 init
= maybe_constant_value (init
);
783 if (TREE_CODE (type
) == INTEGER_TYPE
784 && TREE_CODE (ftype
) == REAL_TYPE
)
786 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
787 && CP_INTEGRAL_TYPE_P (type
))
789 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type
),
790 TYPE_MAX_VALUE (ftype
))
791 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype
),
792 TYPE_MIN_VALUE (type
)))
793 && (TREE_CODE (init
) != INTEGER_CST
794 || !int_fits_type_p (init
, type
)))
797 else if (TREE_CODE (ftype
) == REAL_TYPE
798 && TREE_CODE (type
) == REAL_TYPE
)
800 if (TYPE_PRECISION (type
) < TYPE_PRECISION (ftype
))
802 if (TREE_CODE (init
) == REAL_CST
)
804 /* Issue 703: Loss of precision is OK as long as the value is
805 within the representable range of the new type. */
807 d
= TREE_REAL_CST (init
);
808 real_convert (&r
, TYPE_MODE (type
), &d
);
816 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
817 && TREE_CODE (type
) == REAL_TYPE
)
820 if (TREE_CODE (init
) == INTEGER_CST
)
822 d
= real_value_from_int_cst (0, init
);
823 if (exact_real_truncate (TYPE_MODE (type
), &d
))
830 if (cxx_dialect
>= cxx0x
)
831 pedwarn (EXPR_LOC_OR_HERE (init
), OPT_Wnarrowing
,
832 "narrowing conversion of %qE from %qT to %qT inside { }",
835 warning_at (EXPR_LOC_OR_HERE (init
), OPT_Wnarrowing
,
836 "narrowing conversion of %qE from %qT to %qT inside { } "
837 "is ill-formed in C++11", init
, ftype
, type
);
841 /* Process the initializer INIT for a variable of type TYPE, emitting
842 diagnostics for invalid initializers and converting the initializer as
845 For aggregate types, it assumes that reshape_init has already run, thus the
846 initializer will have the right shape (brace elision has been undone).
848 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
851 digest_init_r (tree type
, tree init
, bool nested
, int flags
,
852 tsubst_flags_t complain
)
854 enum tree_code code
= TREE_CODE (type
);
856 if (error_operand_p (init
))
857 return error_mark_node
;
861 /* We must strip the outermost array type when completing the type,
862 because the its bounds might be incomplete at the moment. */
863 if (!complete_type_or_maybe_complain (TREE_CODE (type
) == ARRAY_TYPE
864 ? TREE_TYPE (type
) : type
, NULL_TREE
,
866 return error_mark_node
;
868 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
869 (g++.old-deja/g++.law/casts2.C). */
870 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
871 init
= TREE_OPERAND (init
, 0);
873 /* Initialization of an array of chars from a string constant. The initializer
874 can be optionally enclosed in braces, but reshape_init has already removed
875 them if they were present. */
876 if (code
== ARRAY_TYPE
)
878 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
879 if (char_type_p (typ1
)
881 && TREE_CODE (init
) == STRING_CST
)
883 tree char_type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init
)));
885 if (TYPE_PRECISION (typ1
) == BITS_PER_UNIT
)
887 if (char_type
!= char_type_node
)
889 if (complain
& tf_error
)
890 error ("char-array initialized from wide string");
891 return error_mark_node
;
896 if (char_type
== char_type_node
)
898 if (complain
& tf_error
)
899 error ("int-array initialized from non-wide string");
900 return error_mark_node
;
902 else if (char_type
!= typ1
)
904 if (complain
& tf_error
)
905 error ("int-array initialized from incompatible "
907 return error_mark_node
;
911 if (type
!= TREE_TYPE (init
))
913 init
= copy_node (init
);
914 TREE_TYPE (init
) = type
;
916 if (TYPE_DOMAIN (type
) != 0 && TREE_CONSTANT (TYPE_SIZE (type
)))
918 int size
= TREE_INT_CST_LOW (TYPE_SIZE (type
));
919 size
= (size
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
;
920 /* In C it is ok to subtract 1 from the length of the string
921 because it's ok to ignore the terminating null char that is
922 counted in the length of the constant, but in C++ this would
924 if (size
< TREE_STRING_LENGTH (init
))
925 permerror (input_location
, "initializer-string for array "
926 "of chars is too long");
932 /* Handle scalar types (including conversions) and references. */
933 if ((TREE_CODE (type
) != COMPLEX_TYPE
934 || BRACE_ENCLOSED_INITIALIZER_P (init
))
935 && (SCALAR_TYPE_P (type
) || code
== REFERENCE_TYPE
))
940 check_narrowing (type
, init
);
941 init
= convert_for_initialization (0, type
, init
, flags
,
942 ICR_INIT
, NULL_TREE
, 0,
946 /* Skip any conversions since we'll be outputting the underlying
948 while (CONVERT_EXPR_P (*exp
)
949 || TREE_CODE (*exp
) == NON_LVALUE_EXPR
)
950 exp
= &TREE_OPERAND (*exp
, 0);
952 *exp
= cplus_expand_constant (*exp
);
957 /* Come here only for aggregates: records, arrays, unions, complex numbers
959 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
960 || TREE_CODE (type
) == VECTOR_TYPE
961 || TREE_CODE (type
) == RECORD_TYPE
962 || TREE_CODE (type
) == UNION_TYPE
963 || TREE_CODE (type
) == COMPLEX_TYPE
);
965 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
966 && !TYPE_NON_AGGREGATE_CLASS (type
))
967 return process_init_constructor (type
, init
, complain
);
970 if (COMPOUND_LITERAL_P (init
) && TREE_CODE (type
) == ARRAY_TYPE
)
972 if (complain
& tf_error
)
973 error ("cannot initialize aggregate of type %qT with "
974 "a compound literal", type
);
976 return error_mark_node
;
979 if (TREE_CODE (type
) == ARRAY_TYPE
980 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
982 /* Allow the result of build_array_copy and of
983 build_value_init_noctor. */
984 if ((TREE_CODE (init
) == VEC_INIT_EXPR
985 || TREE_CODE (init
) == CONSTRUCTOR
)
986 && (same_type_ignoring_top_level_qualifiers_p
987 (type
, TREE_TYPE (init
))))
990 if (complain
& tf_error
)
991 error ("array must be initialized with a brace-enclosed"
993 return error_mark_node
;
996 return convert_for_initialization (NULL_TREE
, type
, init
,
998 ICR_INIT
, NULL_TREE
, 0,
1004 digest_init (tree type
, tree init
, tsubst_flags_t complain
)
1006 return digest_init_r (type
, init
, false, LOOKUP_IMPLICIT
, complain
);
1010 digest_init_flags (tree type
, tree init
, int flags
)
1012 return digest_init_r (type
, init
, false, flags
, tf_warning_or_error
);
1015 /* Set of flags used within process_init_constructor to describe the
1017 #define PICFLAG_ERRONEOUS 1
1018 #define PICFLAG_NOT_ALL_CONSTANT 2
1019 #define PICFLAG_NOT_ALL_SIMPLE 4
1021 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1025 picflag_from_initializer (tree init
)
1027 if (init
== error_mark_node
)
1028 return PICFLAG_ERRONEOUS
;
1029 else if (!TREE_CONSTANT (init
))
1030 return PICFLAG_NOT_ALL_CONSTANT
;
1031 else if (!initializer_constant_valid_p (init
, TREE_TYPE (init
)))
1032 return PICFLAG_NOT_ALL_SIMPLE
;
1036 /* Subroutine of process_init_constructor, which will process an initializer
1037 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1038 which describe the initializers. */
1041 process_init_constructor_array (tree type
, tree init
,
1042 tsubst_flags_t complain
)
1044 unsigned HOST_WIDE_INT i
, len
= 0;
1046 bool unbounded
= false;
1047 constructor_elt
*ce
;
1048 VEC(constructor_elt
,gc
) *v
= CONSTRUCTOR_ELTS (init
);
1050 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
1051 || TREE_CODE (type
) == VECTOR_TYPE
);
1053 if (TREE_CODE (type
) == ARRAY_TYPE
)
1055 tree domain
= TYPE_DOMAIN (type
);
1057 len
= double_int_ext
1060 (tree_to_double_int (TYPE_MAX_VALUE (domain
)),
1061 tree_to_double_int (TYPE_MIN_VALUE (domain
))),
1063 TYPE_PRECISION (TREE_TYPE (domain
)),
1064 TYPE_UNSIGNED (TREE_TYPE (domain
))).low
;
1066 unbounded
= true; /* Take as many as there are. */
1069 /* Vectors are like simple fixed-size arrays. */
1070 len
= TYPE_VECTOR_SUBPARTS (type
);
1072 /* There must not be more initializers than needed. */
1073 if (!unbounded
&& VEC_length (constructor_elt
, v
) > len
)
1075 if (complain
& tf_error
)
1076 error ("too many initializers for %qT", type
);
1078 return PICFLAG_ERRONEOUS
;
1081 FOR_EACH_VEC_ELT (constructor_elt
, v
, i
, ce
)
1085 gcc_assert (TREE_CODE (ce
->index
) == INTEGER_CST
);
1086 if (compare_tree_int (ce
->index
, i
) != 0)
1088 ce
->value
= error_mark_node
;
1089 sorry ("non-trivial designated initializers not supported");
1093 ce
->index
= size_int (i
);
1094 gcc_assert (ce
->value
);
1095 ce
->value
= digest_init_r (TREE_TYPE (type
), ce
->value
, true,
1096 LOOKUP_IMPLICIT
, complain
);
1098 if (ce
->value
!= error_mark_node
)
1099 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1100 (TREE_TYPE (type
), TREE_TYPE (ce
->value
)));
1102 flags
|= picflag_from_initializer (ce
->value
);
1105 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1106 we must add initializers ourselves. */
1108 for (; i
< len
; ++i
)
1112 if (type_build_ctor_call (TREE_TYPE (type
)))
1114 /* If this type needs constructors run for default-initialization,
1115 we can't rely on the back end to do it for us, so make the
1116 initialization explicit by list-initializing from {}. */
1117 next
= build_constructor (init_list_type_node
, NULL
);
1118 next
= digest_init (TREE_TYPE (type
), next
, complain
);
1120 else if (!zero_init_p (TREE_TYPE (type
)))
1121 next
= build_zero_init (TREE_TYPE (type
),
1122 /*nelts=*/NULL_TREE
,
1123 /*static_storage_p=*/false);
1125 /* The default zero-initialization is fine for us; don't
1126 add anything to the CONSTRUCTOR. */
1129 flags
|= picflag_from_initializer (next
);
1130 CONSTRUCTOR_APPEND_ELT (v
, size_int (i
), next
);
1133 CONSTRUCTOR_ELTS (init
) = v
;
1137 /* Subroutine of process_init_constructor, which will process an initializer
1138 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1139 the initializers. */
1142 process_init_constructor_record (tree type
, tree init
,
1143 tsubst_flags_t complain
)
1145 VEC(constructor_elt
,gc
) *v
= NULL
;
1148 unsigned HOST_WIDE_INT idx
= 0;
1150 gcc_assert (TREE_CODE (type
) == RECORD_TYPE
);
1151 gcc_assert (!CLASSTYPE_VBASECLASSES (type
));
1152 gcc_assert (!TYPE_BINFO (type
)
1153 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)));
1154 gcc_assert (!TYPE_POLYMORPHIC_P (type
));
1156 /* Generally, we will always have an index for each initializer (which is
1157 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1158 reshape_init. So we need to handle both cases. */
1159 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1164 if (!DECL_NAME (field
) && DECL_C_BIT_FIELD (field
))
1166 flags
|= picflag_from_initializer (integer_zero_node
);
1167 CONSTRUCTOR_APPEND_ELT (v
, field
, integer_zero_node
);
1171 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
1174 /* If this is a bitfield, first convert to the declared type. */
1175 type
= TREE_TYPE (field
);
1176 if (DECL_BIT_FIELD_TYPE (field
))
1177 type
= DECL_BIT_FIELD_TYPE (field
);
1179 if (idx
< VEC_length (constructor_elt
, CONSTRUCTOR_ELTS (init
)))
1181 constructor_elt
*ce
= VEC_index (constructor_elt
,
1182 CONSTRUCTOR_ELTS (init
), idx
);
1185 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1186 latter case can happen in templates where lookup has to be
1188 gcc_assert (TREE_CODE (ce
->index
) == FIELD_DECL
1189 || TREE_CODE (ce
->index
) == IDENTIFIER_NODE
);
1190 if (ce
->index
!= field
1191 && ce
->index
!= DECL_NAME (field
))
1193 ce
->value
= error_mark_node
;
1194 sorry ("non-trivial designated initializers not supported");
1198 gcc_assert (ce
->value
);
1199 next
= digest_init_r (type
, ce
->value
, true,
1200 LOOKUP_IMPLICIT
, complain
);
1203 else if (type_build_ctor_call (TREE_TYPE (field
)))
1205 /* If this type needs constructors run for
1206 default-initialization, we can't rely on the back end to do it
1207 for us, so build up TARGET_EXPRs. If the type in question is
1208 a class, just build one up; if it's an array, recurse. */
1209 next
= build_constructor (init_list_type_node
, NULL
);
1210 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (field
)))
1212 next
= finish_compound_literal (TREE_TYPE (field
), next
,
1214 /* direct-initialize the target. No temporary is going
1216 if (TREE_CODE (next
) == TARGET_EXPR
)
1217 TARGET_EXPR_DIRECT_INIT_P (next
) = true;
1220 next
= digest_init_r (TREE_TYPE (field
), next
, true,
1221 LOOKUP_IMPLICIT
, complain
);
1223 /* Warn when some struct elements are implicitly initialized. */
1224 warning (OPT_Wmissing_field_initializers
,
1225 "missing initializer for member %qD", field
);
1229 if (TREE_READONLY (field
))
1231 if (complain
& tf_error
)
1232 error ("uninitialized const member %qD", field
);
1234 return PICFLAG_ERRONEOUS
;
1236 else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field
)))
1238 if (complain
& tf_error
)
1239 error ("member %qD with uninitialized const fields", field
);
1241 return PICFLAG_ERRONEOUS
;
1243 else if (TREE_CODE (TREE_TYPE (field
)) == REFERENCE_TYPE
)
1245 if (complain
& tf_error
)
1246 error ("member %qD is uninitialized reference", field
);
1248 return PICFLAG_ERRONEOUS
;
1251 /* Warn when some struct elements are implicitly initialized
1253 warning (OPT_Wmissing_field_initializers
,
1254 "missing initializer for member %qD", field
);
1256 if (!zero_init_p (TREE_TYPE (field
)))
1257 next
= build_zero_init (TREE_TYPE (field
), /*nelts=*/NULL_TREE
,
1258 /*static_storage_p=*/false);
1260 /* The default zero-initialization is fine for us; don't
1261 add anything to the CONSTRUCTOR. */
1265 /* If this is a bitfield, now convert to the lowered type. */
1266 if (type
!= TREE_TYPE (field
))
1267 next
= cp_convert_and_check (TREE_TYPE (field
), next
, complain
);
1268 flags
|= picflag_from_initializer (next
);
1269 CONSTRUCTOR_APPEND_ELT (v
, field
, next
);
1272 if (idx
< VEC_length (constructor_elt
, CONSTRUCTOR_ELTS (init
)))
1274 if (complain
& tf_error
)
1275 error ("too many initializers for %qT", type
);
1277 return PICFLAG_ERRONEOUS
;
1280 CONSTRUCTOR_ELTS (init
) = v
;
1284 /* Subroutine of process_init_constructor, which will process a single
1285 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1286 which describe the initializer. */
1289 process_init_constructor_union (tree type
, tree init
,
1290 tsubst_flags_t complain
)
1292 constructor_elt
*ce
;
1295 /* If the initializer was empty, use default zero initialization. */
1296 if (VEC_empty (constructor_elt
, CONSTRUCTOR_ELTS (init
)))
1299 len
= VEC_length (constructor_elt
, CONSTRUCTOR_ELTS (init
));
1302 if (!(complain
& tf_error
))
1303 return PICFLAG_ERRONEOUS
;
1304 error ("too many initializers for %qT", type
);
1305 VEC_block_remove (constructor_elt
, CONSTRUCTOR_ELTS (init
), 1, len
-1);
1308 ce
= VEC_index (constructor_elt
, CONSTRUCTOR_ELTS (init
), 0);
1310 /* If this element specifies a field, initialize via that field. */
1313 if (TREE_CODE (ce
->index
) == FIELD_DECL
)
1315 else if (TREE_CODE (ce
->index
) == IDENTIFIER_NODE
)
1317 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1318 tree name
= ce
->index
;
1320 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1321 if (DECL_NAME (field
) == name
)
1325 if (complain
& tf_error
)
1326 error ("no field %qD found in union being initialized",
1328 ce
->value
= error_mark_node
;
1334 gcc_assert (TREE_CODE (ce
->index
) == INTEGER_CST
1335 || TREE_CODE (ce
->index
) == RANGE_EXPR
);
1336 if (complain
& tf_error
)
1337 error ("index value instead of field name in union initializer");
1338 ce
->value
= error_mark_node
;
1343 /* Find the first named field. ANSI decided in September 1990
1344 that only named fields count here. */
1345 tree field
= TYPE_FIELDS (type
);
1346 while (field
&& (!DECL_NAME (field
) || TREE_CODE (field
) != FIELD_DECL
))
1347 field
= TREE_CHAIN (field
);
1348 if (field
== NULL_TREE
)
1350 if (complain
& tf_error
)
1351 error ("too many initializers for %qT", type
);
1352 ce
->value
= error_mark_node
;
1357 if (ce
->value
&& ce
->value
!= error_mark_node
)
1358 ce
->value
= digest_init_r (TREE_TYPE (ce
->index
), ce
->value
,
1359 true, LOOKUP_IMPLICIT
, complain
);
1361 return picflag_from_initializer (ce
->value
);
1364 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1365 constructor is a brace-enclosed initializer, and will be modified in-place.
1367 Each element is converted to the right type through digest_init, and
1368 missing initializers are added following the language rules (zero-padding,
1371 After the execution, the initializer will have TREE_CONSTANT if all elts are
1372 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1373 constants that the assembler and linker can compute them.
1375 The function returns the initializer itself, or error_mark_node in case
1379 process_init_constructor (tree type
, tree init
, tsubst_flags_t complain
)
1383 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init
));
1385 if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == VECTOR_TYPE
)
1386 flags
= process_init_constructor_array (type
, init
, complain
);
1387 else if (TREE_CODE (type
) == RECORD_TYPE
)
1388 flags
= process_init_constructor_record (type
, init
, complain
);
1389 else if (TREE_CODE (type
) == UNION_TYPE
)
1390 flags
= process_init_constructor_union (type
, init
, complain
);
1394 if (flags
& PICFLAG_ERRONEOUS
)
1395 return error_mark_node
;
1397 TREE_TYPE (init
) = type
;
1398 if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
) == NULL_TREE
)
1399 cp_complete_array_type (&TREE_TYPE (init
), init
, /*do_default=*/0);
1400 if (flags
& PICFLAG_NOT_ALL_CONSTANT
)
1401 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1402 TREE_CONSTANT (init
) = false;
1405 TREE_CONSTANT (init
) = 1;
1406 if (!(flags
& PICFLAG_NOT_ALL_SIMPLE
))
1407 TREE_STATIC (init
) = 1;
1412 /* Given a structure or union value DATUM, construct and return
1413 the structure or union component which results from narrowing
1414 that value to the base specified in BASETYPE. For example, given the
1417 class L { int ii; };
1418 class A : L { ... };
1419 class B : L { ... };
1420 class C : A, B { ... };
1428 x.A::ii refers to the ii member of the L part of
1429 the A part of the C object named by X. In this case,
1430 DATUM would be x, and BASETYPE would be A.
1432 I used to think that this was nonconformant, that the standard specified
1433 that first we look up ii in A, then convert x to an L& and pull out the
1434 ii part. But in fact, it does say that we convert x to an A&; A here
1435 is known as the "naming class". (jason 2000-12-19)
1437 BINFO_P points to a variable initialized either to NULL_TREE or to the
1438 binfo for the specific base subobject we want to convert to. */
1441 build_scoped_ref (tree datum
, tree basetype
, tree
* binfo_p
)
1445 if (datum
== error_mark_node
)
1446 return error_mark_node
;
1450 binfo
= lookup_base (TREE_TYPE (datum
), basetype
, ba_check
, NULL
);
1452 if (!binfo
|| binfo
== error_mark_node
)
1454 *binfo_p
= NULL_TREE
;
1456 error_not_base_type (basetype
, TREE_TYPE (datum
));
1457 return error_mark_node
;
1461 return build_base_path (PLUS_EXPR
, datum
, binfo
, 1,
1462 tf_warning_or_error
);
1465 /* Build a reference to an object specified by the C++ `->' operator.
1466 Usually this just involves dereferencing the object, but if the
1467 `->' operator is overloaded, then such overloads must be
1468 performed until an object which does not have the `->' operator
1469 overloaded is found. An error is reported when circular pointer
1470 delegation is detected. */
1473 build_x_arrow (location_t loc
, tree expr
, tsubst_flags_t complain
)
1475 tree orig_expr
= expr
;
1476 tree type
= TREE_TYPE (expr
);
1477 tree last_rval
= NULL_TREE
;
1478 VEC(tree
,gc
) *types_memoized
= NULL
;
1480 if (type
== error_mark_node
)
1481 return error_mark_node
;
1483 if (processing_template_decl
)
1485 if (type_dependent_expression_p (expr
))
1486 return build_min_nt_loc (loc
, ARROW_EXPR
, expr
);
1487 expr
= build_non_dependent_expr (expr
);
1490 if (MAYBE_CLASS_TYPE_P (type
))
1492 struct tinst_level
*actual_inst
= current_instantiation ();
1495 while ((expr
= build_new_op (loc
, COMPONENT_REF
,
1496 LOOKUP_NORMAL
, expr
, NULL_TREE
, NULL_TREE
,
1499 if (expr
== error_mark_node
)
1500 return error_mark_node
;
1502 if (fn
&& DECL_USE_TEMPLATE (fn
))
1503 push_tinst_level (fn
);
1506 if (vec_member (TREE_TYPE (expr
), types_memoized
))
1508 if (complain
& tf_error
)
1509 error ("circular pointer delegation detected");
1510 return error_mark_node
;
1513 VEC_safe_push (tree
, gc
, types_memoized
, TREE_TYPE (expr
));
1517 while (current_instantiation () != actual_inst
)
1520 if (last_rval
== NULL_TREE
)
1522 if (complain
& tf_error
)
1523 error ("base operand of %<->%> has non-pointer type %qT", type
);
1524 return error_mark_node
;
1527 if (TREE_CODE (TREE_TYPE (last_rval
)) == REFERENCE_TYPE
)
1528 last_rval
= convert_from_reference (last_rval
);
1531 last_rval
= decay_conversion (expr
, complain
);
1533 if (TREE_CODE (TREE_TYPE (last_rval
)) == POINTER_TYPE
)
1535 if (processing_template_decl
)
1537 expr
= build_min (ARROW_EXPR
, TREE_TYPE (TREE_TYPE (last_rval
)),
1539 TREE_SIDE_EFFECTS (expr
) = TREE_SIDE_EFFECTS (last_rval
);
1543 return cp_build_indirect_ref (last_rval
, RO_NULL
, complain
);
1546 if (complain
& tf_error
)
1549 error ("result of %<operator->()%> yields non-pointer result");
1551 error ("base operand of %<->%> is not a pointer");
1553 return error_mark_node
;
1556 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1557 already been checked out to be of aggregate type. */
1560 build_m_component_ref (tree datum
, tree component
, tsubst_flags_t complain
)
1568 if (error_operand_p (datum
) || error_operand_p (component
))
1569 return error_mark_node
;
1571 datum
= mark_lvalue_use (datum
);
1572 component
= mark_rvalue_use (component
);
1574 ptrmem_type
= TREE_TYPE (component
);
1575 if (!TYPE_PTRMEM_P (ptrmem_type
))
1577 if (complain
& tf_error
)
1578 error ("%qE cannot be used as a member pointer, since it is of "
1579 "type %qT", component
, ptrmem_type
);
1580 return error_mark_node
;
1583 objtype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
1584 if (! MAYBE_CLASS_TYPE_P (objtype
))
1586 if (complain
& tf_error
)
1587 error ("cannot apply member pointer %qE to %qE, which is of "
1588 "non-class type %qT", component
, datum
, objtype
);
1589 return error_mark_node
;
1592 type
= TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type
);
1593 ctype
= complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type
));
1595 if (!COMPLETE_TYPE_P (ctype
))
1597 if (!same_type_p (ctype
, objtype
))
1603 binfo
= lookup_base (objtype
, ctype
, ba_check
, NULL
);
1608 if (complain
& tf_error
)
1609 error ("pointer to member type %qT incompatible with object "
1610 "type %qT", type
, objtype
);
1611 return error_mark_node
;
1613 else if (binfo
== error_mark_node
)
1614 return error_mark_node
;
1617 if (TYPE_PTRDATAMEM_P (ptrmem_type
))
1619 bool is_lval
= real_lvalue_p (datum
);
1622 /* Compute the type of the field, as described in [expr.ref].
1623 There's no such thing as a mutable pointer-to-member, so
1624 things are not as complex as they are for references to
1625 non-static data members. */
1626 type
= cp_build_qualified_type (type
,
1627 (cp_type_quals (type
)
1628 | cp_type_quals (TREE_TYPE (datum
))));
1630 datum
= build_address (datum
);
1632 /* Convert object to the correct base. */
1635 datum
= build_base_path (PLUS_EXPR
, datum
, binfo
, 1, complain
);
1636 if (datum
== error_mark_node
)
1637 return error_mark_node
;
1640 /* Build an expression for "object + offset" where offset is the
1641 value stored in the pointer-to-data-member. */
1642 ptype
= build_pointer_type (type
);
1643 datum
= fold_build_pointer_plus (fold_convert (ptype
, datum
), component
);
1644 datum
= cp_build_indirect_ref (datum
, RO_NULL
, complain
);
1645 if (datum
== error_mark_node
)
1646 return error_mark_node
;
1648 /* If the object expression was an rvalue, return an rvalue. */
1650 datum
= move (datum
);
1654 return build2 (OFFSET_REF
, type
, datum
, component
);
1657 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1660 build_functional_cast (tree exp
, tree parms
, tsubst_flags_t complain
)
1662 /* This is either a call to a constructor,
1663 or a C cast in C++'s `functional' notation. */
1665 /* The type to which we are casting. */
1667 VEC(tree
,gc
) *parmvec
;
1669 if (exp
== error_mark_node
|| parms
== error_mark_node
)
1670 return error_mark_node
;
1672 if (TREE_CODE (exp
) == TYPE_DECL
)
1673 type
= TREE_TYPE (exp
);
1677 /* We need to check this explicitly, since value-initialization of
1678 arrays is allowed in other situations. */
1679 if (TREE_CODE (type
) == ARRAY_TYPE
)
1681 if (complain
& tf_error
)
1682 error ("functional cast to array type %qT", type
);
1683 return error_mark_node
;
1686 if (type_uses_auto (type
))
1688 if (complain
& tf_error
)
1689 error ("invalid use of %<auto%>");
1690 return error_mark_node
;
1693 if (processing_template_decl
)
1697 /* Diagnose this even in a template. We could also try harder
1698 to give all the usual errors when the type and args are
1700 if (TREE_CODE (type
) == REFERENCE_TYPE
&& !parms
)
1702 if (complain
& tf_error
)
1703 error ("invalid value-initialization of reference type");
1704 return error_mark_node
;
1707 t
= build_min (CAST_EXPR
, type
, parms
);
1708 /* We don't know if it will or will not have side effects. */
1709 TREE_SIDE_EFFECTS (t
) = 1;
1713 if (! MAYBE_CLASS_TYPE_P (type
))
1715 if (parms
== NULL_TREE
)
1717 if (VOID_TYPE_P (type
))
1718 return void_zero_node
;
1719 return build_value_init (cv_unqualified (type
), complain
);
1722 /* This must build a C cast. */
1723 parms
= build_x_compound_expr_from_list (parms
, ELK_FUNC_CAST
, complain
);
1724 return cp_build_c_cast (type
, parms
, complain
);
1727 /* Prepare to evaluate as a call to a constructor. If this expression
1728 is actually used, for example,
1730 return X (arg1, arg2, ...);
1732 then the slot being initialized will be filled in. */
1734 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
1735 return error_mark_node
;
1736 if (abstract_virtuals_error_sfinae (NULL_TREE
, type
, complain
))
1737 return error_mark_node
;
1741 If the expression list is a single-expression, the type
1742 conversion is equivalent (in definedness, and if defined in
1743 meaning) to the corresponding cast expression. */
1744 if (parms
&& TREE_CHAIN (parms
) == NULL_TREE
)
1745 return cp_build_c_cast (type
, TREE_VALUE (parms
), complain
);
1749 The expression T(), where T is a simple-type-specifier for a
1750 non-array complete object type or the (possibly cv-qualified)
1751 void type, creates an rvalue of the specified type, which is
1752 value-initialized. */
1754 if (parms
== NULL_TREE
)
1756 exp
= build_value_init (type
, complain
);
1757 exp
= get_target_expr_sfinae (exp
, complain
);
1761 /* Call the constructor. */
1762 parmvec
= make_tree_vector ();
1763 for (; parms
!= NULL_TREE
; parms
= TREE_CHAIN (parms
))
1764 VEC_safe_push (tree
, gc
, parmvec
, TREE_VALUE (parms
));
1765 exp
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
1766 &parmvec
, type
, LOOKUP_NORMAL
, complain
);
1767 release_tree_vector (parmvec
);
1769 if (exp
== error_mark_node
)
1770 return error_mark_node
;
1772 return build_cplus_new (type
, exp
, complain
);
1776 /* Add new exception specifier SPEC, to the LIST we currently have.
1777 If it's already in LIST then do nothing.
1778 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1779 know what we're doing. */
1782 add_exception_specifier (tree list
, tree spec
, int complain
)
1787 diagnostic_t diag_type
= DK_UNSPECIFIED
; /* none */
1789 if (spec
== error_mark_node
)
1792 gcc_assert (spec
&& (!list
|| TREE_VALUE (list
)));
1794 /* [except.spec] 1, type in an exception specifier shall not be
1795 incomplete, or pointer or ref to incomplete other than pointer
1797 is_ptr
= TREE_CODE (core
) == POINTER_TYPE
;
1798 if (is_ptr
|| TREE_CODE (core
) == REFERENCE_TYPE
)
1799 core
= TREE_TYPE (core
);
1802 else if (VOID_TYPE_P (core
))
1804 else if (TREE_CODE (core
) == TEMPLATE_TYPE_PARM
)
1806 else if (processing_template_decl
)
1811 /* 15.4/1 says that types in an exception specifier must be complete,
1812 but it seems more reasonable to only require this on definitions
1813 and calls. So just give a pedwarn at this point; we will give an
1814 error later if we hit one of those two cases. */
1815 if (!COMPLETE_TYPE_P (complete_type (core
)))
1816 diag_type
= DK_PEDWARN
; /* pedwarn */
1823 for (probe
= list
; probe
; probe
= TREE_CHAIN (probe
))
1824 if (same_type_p (TREE_VALUE (probe
), spec
))
1827 list
= tree_cons (NULL_TREE
, spec
, list
);
1830 diag_type
= DK_ERROR
; /* error */
1832 if (diag_type
!= DK_UNSPECIFIED
1833 && (complain
& tf_warning_or_error
))
1834 cxx_incomplete_type_diagnostic (NULL_TREE
, core
, diag_type
);
1839 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
1842 nothrow_spec_p_uninst (const_tree spec
)
1844 if (DEFERRED_NOEXCEPT_SPEC_P (spec
))
1846 return nothrow_spec_p (spec
);
1849 /* Combine the two exceptions specifier lists LIST and ADD, and return
1850 their union. If FN is non-null, it's the source of ADD. */
1853 merge_exception_specifiers (tree list
, tree add
, tree fn
)
1855 tree noex
, orig_list
;
1857 /* No exception-specifier or noexcept(false) are less strict than
1858 anything else. Prefer the newer variant (LIST). */
1859 if (!list
|| list
== noexcept_false_spec
)
1861 else if (!add
|| add
== noexcept_false_spec
)
1864 /* noexcept(true) and throw() are stricter than anything else.
1865 As above, prefer the more recent one (LIST). */
1866 if (nothrow_spec_p_uninst (add
))
1869 noex
= TREE_PURPOSE (list
);
1870 if (DEFERRED_NOEXCEPT_SPEC_P (add
))
1872 /* If ADD is a deferred noexcept, we must have been called from
1873 process_subob_fn. For implicitly declared functions, we build up
1874 a list of functions to consider at instantiation time. */
1875 if (noex
== boolean_true_node
)
1877 gcc_assert (fn
&& (!noex
|| is_overloaded_fn (noex
)));
1878 noex
= build_overload (fn
, noex
);
1880 else if (nothrow_spec_p_uninst (list
))
1883 gcc_checking_assert (!TREE_PURPOSE (add
)
1884 || cp_tree_equal (noex
, TREE_PURPOSE (add
)));
1886 /* Combine the dynamic-exception-specifiers, if any. */
1888 for (; add
&& TREE_VALUE (add
); add
= TREE_CHAIN (add
))
1890 tree spec
= TREE_VALUE (add
);
1893 for (probe
= orig_list
; probe
&& TREE_VALUE (probe
);
1894 probe
= TREE_CHAIN (probe
))
1895 if (same_type_p (TREE_VALUE (probe
), spec
))
1899 spec
= build_tree_list (NULL_TREE
, spec
);
1900 TREE_CHAIN (spec
) = list
;
1905 /* Keep the noexcept-specifier at the beginning of the list. */
1906 if (noex
!= TREE_PURPOSE (list
))
1907 list
= tree_cons (noex
, TREE_VALUE (list
), TREE_CHAIN (list
));
1912 /* Subroutine of build_call. Ensure that each of the types in the
1913 exception specification is complete. Technically, 15.4/1 says that
1914 they need to be complete when we see a declaration of the function,
1915 but we should be able to get away with only requiring this when the
1916 function is defined or called. See also add_exception_specifier. */
1919 require_complete_eh_spec_types (tree fntype
, tree decl
)
1922 /* Don't complain about calls to op new. */
1923 if (decl
&& DECL_ARTIFICIAL (decl
))
1925 for (raises
= TYPE_RAISES_EXCEPTIONS (fntype
); raises
;
1926 raises
= TREE_CHAIN (raises
))
1928 tree type
= TREE_VALUE (raises
);
1929 if (type
&& !COMPLETE_TYPE_P (type
))
1933 ("call to function %qD which throws incomplete type %q#T",
1936 error ("call to function which throws incomplete type %q#T",
1943 #include "gt-cp-typeck2.h"