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. */
101 if (TREE_CODE (arg
) == VAR_DECL
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 (TREE_CODE (arg
) == INDIRECT_REF
115 && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg
, 0))) == REFERENCE_TYPE
116 && (TREE_CODE (TREE_OPERAND (arg
, 0)) == VAR_DECL
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
);
268 /* In SFINAE context, force instantiation. */
269 if (!(complain
& tf_error
))
270 complete_type (type
);
272 /* If the type is incomplete, we register it within a hash table,
273 so that we can check again once it is completed. This makes sense
274 only for objects for which we have a declaration or at least a
276 if (!COMPLETE_TYPE_P (type
))
279 struct pending_abstract_type
*pat
;
281 gcc_assert (!decl
|| DECL_P (decl
)
282 || TREE_CODE (decl
) == IDENTIFIER_NODE
);
284 if (!abstract_pending_vars
)
285 abstract_pending_vars
= htab_create_ggc (31, &pat_calc_hash
,
288 slot
= htab_find_slot_with_hash (abstract_pending_vars
, type
,
289 (hashval_t
)TYPE_UID (type
), INSERT
);
291 pat
= ggc_alloc_pending_abstract_type ();
295 pat
->locus
= ((decl
&& DECL_P (decl
))
296 ? DECL_SOURCE_LOCATION (decl
)
299 pat
->next
= (struct pending_abstract_type
*) *slot
;
305 if (!TYPE_SIZE (type
))
306 /* TYPE is being defined, and during that time
307 CLASSTYPE_PURE_VIRTUALS holds the inline friends. */
310 pure
= CLASSTYPE_PURE_VIRTUALS (type
);
314 if (!(complain
& tf_error
))
319 if (TREE_CODE (decl
) == VAR_DECL
)
320 error ("cannot declare variable %q+D to be of abstract "
321 "type %qT", decl
, type
);
322 else if (TREE_CODE (decl
) == PARM_DECL
)
324 if (DECL_NAME (decl
))
325 error ("cannot declare parameter %q+D to be of abstract type %qT",
328 error ("cannot declare parameter to be of abstract type %qT",
331 else if (TREE_CODE (decl
) == FIELD_DECL
)
332 error ("cannot declare field %q+D to be of abstract type %qT",
334 else if (TREE_CODE (decl
) == FUNCTION_DECL
335 && TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
336 error ("invalid abstract return type for member function %q+#D", decl
);
337 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
338 error ("invalid abstract return type for function %q+#D", decl
);
339 else if (TREE_CODE (decl
) == IDENTIFIER_NODE
)
340 /* Here we do not have location information. */
341 error ("invalid abstract type %qT for %qE", type
, decl
);
343 error ("invalid abstract type for %q+D", decl
);
348 error ("creating array of %qT, which is an abstract class type", type
);
351 error ("invalid cast to abstract class type %qT", type
);
354 error ("invalid new-expression of abstract class type %qT", type
);
357 error ("invalid abstract return type %qT", type
);
360 error ("invalid abstract parameter type %qT", type
);
363 error ("expression of abstract class type %qT cannot "
364 "be used in throw-expression", type
);
367 error ("cannot declare catch parameter to be of abstract "
368 "class type %qT", type
);
371 error ("cannot allocate an object of abstract type %qT", type
);
374 /* Only go through this once. */
380 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
381 " because the following virtual functions are pure within %qT:",
384 FOR_EACH_VEC_ELT (*pure
, ix
, fn
)
385 if (! DECL_CLONED_FUNCTION_P (fn
)
386 || DECL_COMPLETE_DESTRUCTOR_P (fn
))
387 inform (input_location
, "\t%+#D", fn
);
389 /* Now truncate the vector. This leaves it non-null, so we know
390 there are pure virtuals, but empty so we don't list them out
399 abstract_virtuals_error_sfinae (tree decl
, tree type
, tsubst_flags_t complain
)
401 return abstract_virtuals_error_sfinae (decl
, type
, ACU_UNKNOWN
, complain
);
405 abstract_virtuals_error_sfinae (abstract_class_use use
, tree type
,
406 tsubst_flags_t complain
)
408 return abstract_virtuals_error_sfinae (NULL_TREE
, type
, use
, complain
);
412 /* Wrapper for the above function in the common case of wanting errors. */
415 abstract_virtuals_error (tree decl
, tree type
)
417 return abstract_virtuals_error_sfinae (decl
, type
, tf_warning_or_error
);
421 abstract_virtuals_error (abstract_class_use use
, tree type
)
423 return abstract_virtuals_error_sfinae (use
, type
, tf_warning_or_error
);
426 /* Print an error message for invalid use of an incomplete type.
427 VALUE is the expression that was used (or 0 if that isn't known)
428 and TYPE is the type that was invalid. DIAG_KIND indicates the
429 type of diagnostic (see diagnostic.def). */
432 cxx_incomplete_type_diagnostic (const_tree value
, const_tree type
,
433 diagnostic_t diag_kind
)
437 gcc_assert (diag_kind
== DK_WARNING
438 || diag_kind
== DK_PEDWARN
439 || diag_kind
== DK_ERROR
);
441 /* Avoid duplicate error message. */
442 if (TREE_CODE (type
) == ERROR_MARK
)
445 if (value
!= 0 && (TREE_CODE (value
) == VAR_DECL
446 || TREE_CODE (value
) == PARM_DECL
447 || TREE_CODE (value
) == FIELD_DECL
))
449 emit_diagnostic (diag_kind
, input_location
, 0,
450 "%q+D has incomplete type", value
);
454 /* We must print an error message. Be clever about what it says. */
456 switch (TREE_CODE (type
))
462 emit_diagnostic (diag_kind
, input_location
, 0,
463 "invalid use of incomplete type %q#T", type
);
464 if (!TYPE_TEMPLATE_INFO (type
))
465 emit_diagnostic (diag_kind
, input_location
, 0,
466 "forward declaration of %q+#T", type
);
468 emit_diagnostic (diag_kind
, input_location
, 0,
469 "declaration of %q+#T", type
);
473 emit_diagnostic (diag_kind
, input_location
, 0,
474 "invalid use of %qT", type
);
478 if (TYPE_DOMAIN (type
))
480 type
= TREE_TYPE (type
);
483 emit_diagnostic (diag_kind
, input_location
, 0,
484 "invalid use of array with unspecified bounds");
490 tree member
= TREE_OPERAND (value
, 1);
491 if (is_overloaded_fn (member
))
492 member
= get_first_fn (member
);
493 if (DECL_FUNCTION_MEMBER_P (member
)
494 && ! flag_ms_extensions
)
495 emit_diagnostic (diag_kind
, input_location
, 0,
496 "invalid use of member function "
497 "(did you forget the %<()%> ?)");
499 emit_diagnostic (diag_kind
, input_location
, 0,
500 "invalid use of member "
501 "(did you forget the %<&%> ?)");
505 case TEMPLATE_TYPE_PARM
:
507 emit_diagnostic (diag_kind
, input_location
, 0,
508 "invalid use of %<auto%>");
510 emit_diagnostic (diag_kind
, input_location
, 0,
511 "invalid use of template type parameter %qT", type
);
514 case BOUND_TEMPLATE_TEMPLATE_PARM
:
515 emit_diagnostic (diag_kind
, input_location
, 0,
516 "invalid use of template template parameter %qT",
521 emit_diagnostic (diag_kind
, input_location
, 0,
522 "invalid use of dependent type %qT", type
);
526 if (type
== init_list_type_node
)
528 emit_diagnostic (diag_kind
, input_location
, 0,
529 "invalid use of brace-enclosed initializer list");
532 gcc_assert (type
== unknown_type_node
);
533 if (value
&& TREE_CODE (value
) == COMPONENT_REF
)
535 else if (value
&& TREE_CODE (value
) == ADDR_EXPR
)
536 emit_diagnostic (diag_kind
, input_location
, 0,
537 "address of overloaded function with no contextual "
539 else if (value
&& TREE_CODE (value
) == OVERLOAD
)
540 emit_diagnostic (diag_kind
, input_location
, 0,
541 "overloaded function with no contextual type information");
543 emit_diagnostic (diag_kind
, input_location
, 0,
544 "insufficient contextual information to determine type");
552 /* Backward-compatibility interface to incomplete_type_diagnostic;
553 required by ../tree.c. */
554 #undef cxx_incomplete_type_error
556 cxx_incomplete_type_error (const_tree value
, const_tree type
)
558 cxx_incomplete_type_diagnostic (value
, type
, DK_ERROR
);
562 /* The recursive part of split_nonconstant_init. DEST is an lvalue
563 expression to which INIT should be assigned. INIT is a CONSTRUCTOR.
564 Return true if the whole of the value was initialized by the
565 generated statements. */
568 split_nonconstant_init_1 (tree dest
, tree init
)
570 unsigned HOST_WIDE_INT idx
;
571 tree field_index
, value
;
572 tree type
= TREE_TYPE (dest
);
573 tree inner_type
= NULL
;
574 bool array_type_p
= false;
575 bool complete_p
= true;
576 HOST_WIDE_INT num_split_elts
= 0;
578 switch (TREE_CODE (type
))
581 inner_type
= TREE_TYPE (type
);
587 case QUAL_UNION_TYPE
:
588 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init
), idx
,
591 /* The current implementation of this algorithm assumes that
592 the field was set for all the elements. This is usually done
593 by process_init_constructor. */
594 gcc_assert (field_index
);
597 inner_type
= TREE_TYPE (field_index
);
599 if (TREE_CODE (value
) == CONSTRUCTOR
)
604 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
605 NULL_TREE
, NULL_TREE
);
607 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
610 if (!split_nonconstant_init_1 (sub
, value
))
614 else if (!initializer_constant_valid_p (value
, inner_type
))
619 /* FIXME: Ordered removal is O(1) so the whole function is
620 worst-case quadratic. This could be fixed using an aside
621 bitmap to record which elements must be removed and remove
622 them all at the same time. Or by merging
623 split_non_constant_init into process_init_constructor_array,
624 that is separating constants from non-constants while building
626 CONSTRUCTOR_ELTS (init
)->ordered_remove (idx
);
630 sub
= build4 (ARRAY_REF
, inner_type
, dest
, field_index
,
631 NULL_TREE
, NULL_TREE
);
633 sub
= build3 (COMPONENT_REF
, inner_type
, dest
, field_index
,
636 code
= build2 (INIT_EXPR
, inner_type
, sub
, value
);
637 code
= build_stmt (input_location
, EXPR_STMT
, code
);
638 code
= maybe_cleanup_point_expr_void (code
);
640 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (inner_type
))
642 code
= (build_special_member_call
643 (sub
, complete_dtor_identifier
, NULL
, inner_type
,
644 LOOKUP_NORMAL
, tf_warning_or_error
));
645 finish_eh_cleanup (code
);
654 if (!initializer_constant_valid_p (init
, type
))
657 tree cons
= copy_node (init
);
658 CONSTRUCTOR_ELTS (init
) = NULL
;
659 code
= build2 (MODIFY_EXPR
, type
, dest
, cons
);
660 code
= build_stmt (input_location
, EXPR_STMT
, code
);
662 num_split_elts
+= CONSTRUCTOR_NELTS (init
);
670 /* The rest of the initializer is now a constant. */
671 TREE_CONSTANT (init
) = 1;
672 return complete_p
&& complete_ctor_at_level_p (TREE_TYPE (init
),
673 num_split_elts
, inner_type
);
676 /* A subroutine of store_init_value. Splits non-constant static
677 initializer INIT into a constant part and generates code to
678 perform the non-constant part of the initialization to DEST.
679 Returns the code for the runtime init. */
682 split_nonconstant_init (tree dest
, tree init
)
686 if (TREE_CODE (init
) == CONSTRUCTOR
)
688 code
= push_stmt_list ();
689 if (split_nonconstant_init_1 (dest
, init
))
691 code
= pop_stmt_list (code
);
692 DECL_INITIAL (dest
) = init
;
693 TREE_READONLY (dest
) = 0;
696 code
= build2 (INIT_EXPR
, TREE_TYPE (dest
), dest
, init
);
701 /* Perform appropriate conversions on the initial value of a variable,
702 store it in the declaration DECL,
703 and print any error messages that are appropriate.
704 If the init is invalid, store an ERROR_MARK.
706 C++: Note that INIT might be a TREE_LIST, which would mean that it is
707 a base class initializer for some aggregate type, hopefully compatible
708 with DECL. If INIT is a single element, and DECL is an aggregate
709 type, we silently convert INIT into a TREE_LIST, allowing a constructor
712 If INIT is a TREE_LIST and there is no constructor, turn INIT
713 into a CONSTRUCTOR and use standard initialization techniques.
714 Perhaps a warning should be generated?
716 Returns code to be executed if initialization could not be performed
717 for static variable. In that case, caller must emit the code. */
720 store_init_value (tree decl
, tree init
, vec
<tree
, va_gc
>** cleanups
, int flags
)
724 /* If variable's type was invalidly declared, just ignore it. */
726 type
= TREE_TYPE (decl
);
727 if (TREE_CODE (type
) == ERROR_MARK
)
730 if (MAYBE_CLASS_TYPE_P (type
))
732 if (TREE_CODE (init
) == TREE_LIST
)
734 error ("constructor syntax used, but no constructor declared "
735 "for type %qT", type
);
736 init
= build_constructor_from_list (init_list_type_node
, nreverse (init
));
739 else if (TREE_CODE (init
) == TREE_LIST
740 && TREE_TYPE (init
) != unknown_type_node
)
742 gcc_assert (TREE_CODE (decl
) != RESULT_DECL
);
744 if (TREE_CODE (init
) == TREE_LIST
745 && TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
747 error ("cannot initialize arrays using this syntax");
751 /* We get here with code like `int a (2);' */
752 init
= build_x_compound_expr_from_list (init
, ELK_INIT
,
753 tf_warning_or_error
);
756 /* End of special C++ code. */
758 if (flags
& LOOKUP_ALREADY_DIGESTED
)
761 /* Digest the specified initializer into an expression. */
762 value
= digest_init_flags (type
, init
, flags
);
764 value
= extend_ref_init_temps (decl
, value
, cleanups
);
766 /* In C++0x constant expression is a semantic, not syntactic, property.
767 In C++98, make sure that what we thought was a constant expression at
768 template definition time is still constant and otherwise perform this
769 as optimization, e.g. to fold SIZEOF_EXPRs in the initializer. */
770 if (decl_maybe_constant_var_p (decl
) || TREE_STATIC (decl
))
773 value
= fold_non_dependent_expr (value
);
774 value
= maybe_constant_init (value
);
775 if (DECL_DECLARED_CONSTEXPR_P (decl
))
777 /* Diagnose a non-constant initializer for constexpr. */
778 if (processing_template_decl
779 && !require_potential_constant_expression (value
))
780 value
= error_mark_node
;
782 value
= cxx_constant_value (value
);
784 const_init
= (reduced_constant_expression_p (value
)
785 || error_operand_p (value
));
786 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
) = const_init
;
787 TREE_CONSTANT (decl
) = const_init
&& decl_maybe_constant_var_p (decl
);
790 /* If the initializer is not a constant, fill in DECL_INITIAL with
791 the bits that are constant, and then return an expression that
792 will perform the dynamic initialization. */
793 if (value
!= error_mark_node
794 && (TREE_SIDE_EFFECTS (value
)
795 || ! initializer_constant_valid_p (value
, TREE_TYPE (value
))))
797 if (TREE_CODE (type
) == ARRAY_TYPE
798 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (type
)))
799 /* For an array, we only need/want a single cleanup region rather
800 than one per element. */
801 return build_vec_init (decl
, NULL_TREE
, value
, false, 1,
802 tf_warning_or_error
);
804 return split_nonconstant_init (decl
, value
);
806 /* If the value is a constant, just put it in DECL_INITIAL. If DECL
807 is an automatic variable, the middle end will turn this into a
808 dynamic initialization later. */
809 DECL_INITIAL (decl
) = value
;
814 /* Give errors about narrowing conversions within { }. */
817 check_narrowing (tree type
, tree init
)
819 tree ftype
= unlowered_expr_type (init
);
823 if (!warn_narrowing
|| !ARITHMETIC_TYPE_P (type
))
826 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
827 && TREE_CODE (type
) == COMPLEX_TYPE
)
829 tree elttype
= TREE_TYPE (type
);
830 check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 0)->value
);
831 if (CONSTRUCTOR_NELTS (init
) > 1)
832 check_narrowing (elttype
, CONSTRUCTOR_ELT (init
, 1)->value
);
836 init
= maybe_constant_value (init
);
838 if (TREE_CODE (type
) == INTEGER_TYPE
839 && TREE_CODE (ftype
) == REAL_TYPE
)
841 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
842 && CP_INTEGRAL_TYPE_P (type
))
844 if (TREE_CODE (ftype
) == ENUMERAL_TYPE
)
845 /* Check for narrowing based on the values of the enumeration. */
846 ftype
= ENUM_UNDERLYING_TYPE (ftype
);
847 if ((tree_int_cst_lt (TYPE_MAX_VALUE (type
),
848 TYPE_MAX_VALUE (ftype
))
849 || tree_int_cst_lt (TYPE_MIN_VALUE (ftype
),
850 TYPE_MIN_VALUE (type
)))
851 && (TREE_CODE (init
) != INTEGER_CST
852 || !int_fits_type_p (init
, type
)))
855 else if (TREE_CODE (ftype
) == REAL_TYPE
856 && TREE_CODE (type
) == REAL_TYPE
)
858 if (TYPE_PRECISION (type
) < TYPE_PRECISION (ftype
))
860 if (TREE_CODE (init
) == REAL_CST
)
862 /* Issue 703: Loss of precision is OK as long as the value is
863 within the representable range of the new type. */
865 d
= TREE_REAL_CST (init
);
866 real_convert (&r
, TYPE_MODE (type
), &d
);
874 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype
)
875 && TREE_CODE (type
) == REAL_TYPE
)
878 if (TREE_CODE (init
) == INTEGER_CST
)
880 d
= real_value_from_int_cst (0, init
);
881 if (exact_real_truncate (TYPE_MODE (type
), &d
))
888 if (cxx_dialect
>= cxx0x
)
889 pedwarn (EXPR_LOC_OR_HERE (init
), OPT_Wnarrowing
,
890 "narrowing conversion of %qE from %qT to %qT inside { }",
893 warning_at (EXPR_LOC_OR_HERE (init
), OPT_Wnarrowing
,
894 "narrowing conversion of %qE from %qT to %qT inside { } "
895 "is ill-formed in C++11", init
, ftype
, type
);
899 /* Process the initializer INIT for a variable of type TYPE, emitting
900 diagnostics for invalid initializers and converting the initializer as
903 For aggregate types, it assumes that reshape_init has already run, thus the
904 initializer will have the right shape (brace elision has been undone).
906 NESTED is true iff we are being called for an element of a CONSTRUCTOR. */
909 digest_init_r (tree type
, tree init
, bool nested
, int flags
,
910 tsubst_flags_t complain
)
912 enum tree_code code
= TREE_CODE (type
);
914 if (error_operand_p (init
))
915 return error_mark_node
;
919 /* We must strip the outermost array type when completing the type,
920 because the its bounds might be incomplete at the moment. */
921 if (!complete_type_or_maybe_complain (TREE_CODE (type
) == ARRAY_TYPE
922 ? TREE_TYPE (type
) : type
, NULL_TREE
,
924 return error_mark_node
;
926 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
927 (g++.old-deja/g++.law/casts2.C). */
928 if (TREE_CODE (init
) == NON_LVALUE_EXPR
)
929 init
= TREE_OPERAND (init
, 0);
931 /* Initialization of an array of chars from a string constant. The initializer
932 can be optionally enclosed in braces, but reshape_init has already removed
933 them if they were present. */
934 if (code
== ARRAY_TYPE
)
936 tree typ1
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
937 if (char_type_p (typ1
)
939 && TREE_CODE (init
) == STRING_CST
)
941 tree char_type
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init
)));
943 if (TYPE_PRECISION (typ1
) == BITS_PER_UNIT
)
945 if (char_type
!= char_type_node
)
947 if (complain
& tf_error
)
948 error ("char-array initialized from wide string");
949 return error_mark_node
;
954 if (char_type
== char_type_node
)
956 if (complain
& tf_error
)
957 error ("int-array initialized from non-wide string");
958 return error_mark_node
;
960 else if (char_type
!= typ1
)
962 if (complain
& tf_error
)
963 error ("int-array initialized from incompatible "
965 return error_mark_node
;
969 if (type
!= TREE_TYPE (init
))
971 init
= copy_node (init
);
972 TREE_TYPE (init
) = type
;
974 if (TYPE_DOMAIN (type
) != 0 && TREE_CONSTANT (TYPE_SIZE (type
)))
976 int size
= TREE_INT_CST_LOW (TYPE_SIZE (type
));
977 size
= (size
+ BITS_PER_UNIT
- 1) / BITS_PER_UNIT
;
978 /* In C it is ok to subtract 1 from the length of the string
979 because it's ok to ignore the terminating null char that is
980 counted in the length of the constant, but in C++ this would
982 if (size
< TREE_STRING_LENGTH (init
))
983 permerror (input_location
, "initializer-string for array "
984 "of chars is too long");
990 /* Handle scalar types (including conversions) and references. */
991 if ((TREE_CODE (type
) != COMPLEX_TYPE
992 || BRACE_ENCLOSED_INITIALIZER_P (init
))
993 && (SCALAR_TYPE_P (type
) || code
== REFERENCE_TYPE
))
998 check_narrowing (type
, init
);
999 init
= convert_for_initialization (0, type
, init
, flags
,
1000 ICR_INIT
, NULL_TREE
, 0,
1004 /* Skip any conversions since we'll be outputting the underlying
1006 while (CONVERT_EXPR_P (*exp
)
1007 || TREE_CODE (*exp
) == NON_LVALUE_EXPR
)
1008 exp
= &TREE_OPERAND (*exp
, 0);
1010 *exp
= cplus_expand_constant (*exp
);
1015 /* Come here only for aggregates: records, arrays, unions, complex numbers
1017 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
1018 || TREE_CODE (type
) == VECTOR_TYPE
1019 || TREE_CODE (type
) == RECORD_TYPE
1020 || TREE_CODE (type
) == UNION_TYPE
1021 || TREE_CODE (type
) == COMPLEX_TYPE
);
1023 if (BRACE_ENCLOSED_INITIALIZER_P (init
)
1024 && !TYPE_NON_AGGREGATE_CLASS (type
))
1025 return process_init_constructor (type
, init
, complain
);
1028 if (COMPOUND_LITERAL_P (init
) && TREE_CODE (type
) == ARRAY_TYPE
)
1030 if (complain
& tf_error
)
1031 error ("cannot initialize aggregate of type %qT with "
1032 "a compound literal", type
);
1034 return error_mark_node
;
1037 if (TREE_CODE (type
) == ARRAY_TYPE
1038 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
1040 /* Allow the result of build_array_copy and of
1041 build_value_init_noctor. */
1042 if ((TREE_CODE (init
) == VEC_INIT_EXPR
1043 || TREE_CODE (init
) == CONSTRUCTOR
)
1044 && (same_type_ignoring_top_level_qualifiers_p
1045 (type
, TREE_TYPE (init
))))
1048 if (complain
& tf_error
)
1049 error ("array must be initialized with a brace-enclosed"
1051 return error_mark_node
;
1054 return convert_for_initialization (NULL_TREE
, type
, init
,
1056 ICR_INIT
, NULL_TREE
, 0,
1062 digest_init (tree type
, tree init
, tsubst_flags_t complain
)
1064 return digest_init_r (type
, init
, false, LOOKUP_IMPLICIT
, complain
);
1068 digest_init_flags (tree type
, tree init
, int flags
)
1070 return digest_init_r (type
, init
, false, flags
, tf_warning_or_error
);
1073 /* Set of flags used within process_init_constructor to describe the
1075 #define PICFLAG_ERRONEOUS 1
1076 #define PICFLAG_NOT_ALL_CONSTANT 2
1077 #define PICFLAG_NOT_ALL_SIMPLE 4
1079 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1083 picflag_from_initializer (tree init
)
1085 if (init
== error_mark_node
)
1086 return PICFLAG_ERRONEOUS
;
1087 else if (!TREE_CONSTANT (init
))
1088 return PICFLAG_NOT_ALL_CONSTANT
;
1089 else if (!initializer_constant_valid_p (init
, TREE_TYPE (init
)))
1090 return PICFLAG_NOT_ALL_SIMPLE
;
1094 /* Subroutine of process_init_constructor, which will process an initializer
1095 INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1096 which describe the initializers. */
1099 process_init_constructor_array (tree type
, tree init
,
1100 tsubst_flags_t complain
)
1102 unsigned HOST_WIDE_INT i
, len
= 0;
1104 bool unbounded
= false;
1105 constructor_elt
*ce
;
1106 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (init
);
1108 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
1109 || TREE_CODE (type
) == VECTOR_TYPE
);
1111 if (TREE_CODE (type
) == ARRAY_TYPE
)
1113 tree domain
= TYPE_DOMAIN (type
);
1115 len
= (tree_to_double_int (TYPE_MAX_VALUE (domain
))
1116 - tree_to_double_int (TYPE_MIN_VALUE (domain
))
1118 .ext (TYPE_PRECISION (TREE_TYPE (domain
)),
1119 TYPE_UNSIGNED (TREE_TYPE (domain
)))
1122 unbounded
= true; /* Take as many as there are. */
1125 /* Vectors are like simple fixed-size arrays. */
1126 len
= TYPE_VECTOR_SUBPARTS (type
);
1128 /* There must not be more initializers than needed. */
1129 if (!unbounded
&& vec_safe_length (v
) > len
)
1131 if (complain
& tf_error
)
1132 error ("too many initializers for %qT", type
);
1134 return PICFLAG_ERRONEOUS
;
1137 FOR_EACH_VEC_SAFE_ELT (v
, i
, ce
)
1141 gcc_assert (TREE_CODE (ce
->index
) == INTEGER_CST
);
1142 if (compare_tree_int (ce
->index
, i
) != 0)
1144 ce
->value
= error_mark_node
;
1145 sorry ("non-trivial designated initializers not supported");
1149 ce
->index
= size_int (i
);
1150 gcc_assert (ce
->value
);
1151 ce
->value
= digest_init_r (TREE_TYPE (type
), ce
->value
, true,
1152 LOOKUP_IMPLICIT
, complain
);
1154 if (ce
->value
!= error_mark_node
)
1155 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1156 (TREE_TYPE (type
), TREE_TYPE (ce
->value
)));
1158 flags
|= picflag_from_initializer (ce
->value
);
1161 /* No more initializers. If the array is unbounded, we are done. Otherwise,
1162 we must add initializers ourselves. */
1164 for (; i
< len
; ++i
)
1168 if (type_build_ctor_call (TREE_TYPE (type
)))
1170 /* If this type needs constructors run for default-initialization,
1171 we can't rely on the back end to do it for us, so make the
1172 initialization explicit by list-initializing from {}. */
1173 next
= build_constructor (init_list_type_node
, NULL
);
1174 next
= digest_init (TREE_TYPE (type
), next
, complain
);
1176 else if (!zero_init_p (TREE_TYPE (type
)))
1177 next
= build_zero_init (TREE_TYPE (type
),
1178 /*nelts=*/NULL_TREE
,
1179 /*static_storage_p=*/false);
1181 /* The default zero-initialization is fine for us; don't
1182 add anything to the CONSTRUCTOR. */
1185 flags
|= picflag_from_initializer (next
);
1186 CONSTRUCTOR_APPEND_ELT (v
, size_int (i
), next
);
1189 CONSTRUCTOR_ELTS (init
) = v
;
1193 /* Subroutine of process_init_constructor, which will process an initializer
1194 INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1195 the initializers. */
1198 process_init_constructor_record (tree type
, tree init
,
1199 tsubst_flags_t complain
)
1201 vec
<constructor_elt
, va_gc
> *v
= NULL
;
1204 unsigned HOST_WIDE_INT idx
= 0;
1206 gcc_assert (TREE_CODE (type
) == RECORD_TYPE
);
1207 gcc_assert (!CLASSTYPE_VBASECLASSES (type
));
1208 gcc_assert (!TYPE_BINFO (type
)
1209 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)));
1210 gcc_assert (!TYPE_POLYMORPHIC_P (type
));
1212 /* Generally, we will always have an index for each initializer (which is
1213 a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1214 reshape_init. So we need to handle both cases. */
1215 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
1220 if (!DECL_NAME (field
) && DECL_C_BIT_FIELD (field
))
1222 flags
|= picflag_from_initializer (integer_zero_node
);
1223 CONSTRUCTOR_APPEND_ELT (v
, field
, integer_zero_node
);
1227 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
1230 /* If this is a bitfield, first convert to the declared type. */
1231 type
= TREE_TYPE (field
);
1232 if (DECL_BIT_FIELD_TYPE (field
))
1233 type
= DECL_BIT_FIELD_TYPE (field
);
1235 if (idx
< vec_safe_length (CONSTRUCTOR_ELTS (init
)))
1237 constructor_elt
*ce
= &(*CONSTRUCTOR_ELTS (init
))[idx
];
1240 /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1241 latter case can happen in templates where lookup has to be
1243 gcc_assert (TREE_CODE (ce
->index
) == FIELD_DECL
1244 || TREE_CODE (ce
->index
) == IDENTIFIER_NODE
);
1245 if (ce
->index
!= field
1246 && ce
->index
!= DECL_NAME (field
))
1248 ce
->value
= error_mark_node
;
1249 sorry ("non-trivial designated initializers not supported");
1253 gcc_assert (ce
->value
);
1254 next
= digest_init_r (type
, ce
->value
, true,
1255 LOOKUP_IMPLICIT
, complain
);
1258 else if (type_build_ctor_call (TREE_TYPE (field
)))
1260 /* If this type needs constructors run for
1261 default-initialization, we can't rely on the back end to do it
1262 for us, so build up TARGET_EXPRs. If the type in question is
1263 a class, just build one up; if it's an array, recurse. */
1264 next
= build_constructor (init_list_type_node
, NULL
);
1265 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (field
)))
1267 next
= finish_compound_literal (TREE_TYPE (field
), next
,
1269 /* direct-initialize the target. No temporary is going
1271 if (TREE_CODE (next
) == TARGET_EXPR
)
1272 TARGET_EXPR_DIRECT_INIT_P (next
) = true;
1275 next
= digest_init_r (TREE_TYPE (field
), next
, true,
1276 LOOKUP_IMPLICIT
, complain
);
1278 /* Warn when some struct elements are implicitly initialized. */
1279 warning (OPT_Wmissing_field_initializers
,
1280 "missing initializer for member %qD", field
);
1284 if (TREE_READONLY (field
))
1286 if (complain
& tf_error
)
1287 error ("uninitialized const member %qD", field
);
1289 return PICFLAG_ERRONEOUS
;
1291 else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field
)))
1293 if (complain
& tf_error
)
1294 error ("member %qD with uninitialized const fields", field
);
1296 return PICFLAG_ERRONEOUS
;
1298 else if (TREE_CODE (TREE_TYPE (field
)) == REFERENCE_TYPE
)
1300 if (complain
& tf_error
)
1301 error ("member %qD is uninitialized reference", field
);
1303 return PICFLAG_ERRONEOUS
;
1306 /* Warn when some struct elements are implicitly initialized
1308 warning (OPT_Wmissing_field_initializers
,
1309 "missing initializer for member %qD", field
);
1311 if (!zero_init_p (TREE_TYPE (field
)))
1312 next
= build_zero_init (TREE_TYPE (field
), /*nelts=*/NULL_TREE
,
1313 /*static_storage_p=*/false);
1315 /* The default zero-initialization is fine for us; don't
1316 add anything to the CONSTRUCTOR. */
1320 /* If this is a bitfield, now convert to the lowered type. */
1321 if (type
!= TREE_TYPE (field
))
1322 next
= cp_convert_and_check (TREE_TYPE (field
), next
, complain
);
1323 flags
|= picflag_from_initializer (next
);
1324 CONSTRUCTOR_APPEND_ELT (v
, field
, next
);
1327 if (idx
< vec_safe_length (CONSTRUCTOR_ELTS (init
)))
1329 if (complain
& tf_error
)
1330 error ("too many initializers for %qT", type
);
1332 return PICFLAG_ERRONEOUS
;
1335 CONSTRUCTOR_ELTS (init
) = v
;
1339 /* Subroutine of process_init_constructor, which will process a single
1340 initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1341 which describe the initializer. */
1344 process_init_constructor_union (tree type
, tree init
,
1345 tsubst_flags_t complain
)
1347 constructor_elt
*ce
;
1350 /* If the initializer was empty, use default zero initialization. */
1351 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init
)))
1354 len
= CONSTRUCTOR_ELTS (init
)->length ();
1357 if (!(complain
& tf_error
))
1358 return PICFLAG_ERRONEOUS
;
1359 error ("too many initializers for %qT", type
);
1360 CONSTRUCTOR_ELTS (init
)->block_remove (1, len
-1);
1363 ce
= &(*CONSTRUCTOR_ELTS (init
))[0];
1365 /* If this element specifies a field, initialize via that field. */
1368 if (TREE_CODE (ce
->index
) == FIELD_DECL
)
1370 else if (TREE_CODE (ce
->index
) == IDENTIFIER_NODE
)
1372 /* This can happen within a cast, see g++.dg/opt/cse2.C. */
1373 tree name
= ce
->index
;
1375 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1376 if (DECL_NAME (field
) == name
)
1380 if (complain
& tf_error
)
1381 error ("no field %qD found in union being initialized",
1383 ce
->value
= error_mark_node
;
1389 gcc_assert (TREE_CODE (ce
->index
) == INTEGER_CST
1390 || TREE_CODE (ce
->index
) == RANGE_EXPR
);
1391 if (complain
& tf_error
)
1392 error ("index value instead of field name in union initializer");
1393 ce
->value
= error_mark_node
;
1398 /* Find the first named field. ANSI decided in September 1990
1399 that only named fields count here. */
1400 tree field
= TYPE_FIELDS (type
);
1401 while (field
&& (!DECL_NAME (field
) || TREE_CODE (field
) != FIELD_DECL
))
1402 field
= TREE_CHAIN (field
);
1403 if (field
== NULL_TREE
)
1405 if (complain
& tf_error
)
1406 error ("too many initializers for %qT", type
);
1407 ce
->value
= error_mark_node
;
1412 if (ce
->value
&& ce
->value
!= error_mark_node
)
1413 ce
->value
= digest_init_r (TREE_TYPE (ce
->index
), ce
->value
,
1414 true, LOOKUP_IMPLICIT
, complain
);
1416 return picflag_from_initializer (ce
->value
);
1419 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1420 constructor is a brace-enclosed initializer, and will be modified in-place.
1422 Each element is converted to the right type through digest_init, and
1423 missing initializers are added following the language rules (zero-padding,
1426 After the execution, the initializer will have TREE_CONSTANT if all elts are
1427 constant, and TREE_STATIC set if, in addition, all elts are simple enough
1428 constants that the assembler and linker can compute them.
1430 The function returns the initializer itself, or error_mark_node in case
1434 process_init_constructor (tree type
, tree init
, tsubst_flags_t complain
)
1438 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init
));
1440 if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == VECTOR_TYPE
)
1441 flags
= process_init_constructor_array (type
, init
, complain
);
1442 else if (TREE_CODE (type
) == RECORD_TYPE
)
1443 flags
= process_init_constructor_record (type
, init
, complain
);
1444 else if (TREE_CODE (type
) == UNION_TYPE
)
1445 flags
= process_init_constructor_union (type
, init
, complain
);
1449 if (flags
& PICFLAG_ERRONEOUS
)
1450 return error_mark_node
;
1452 TREE_TYPE (init
) = type
;
1453 if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
) == NULL_TREE
)
1454 cp_complete_array_type (&TREE_TYPE (init
), init
, /*do_default=*/0);
1455 if (flags
& PICFLAG_NOT_ALL_CONSTANT
)
1456 /* Make sure TREE_CONSTANT isn't set from build_constructor. */
1457 TREE_CONSTANT (init
) = false;
1460 TREE_CONSTANT (init
) = 1;
1461 if (!(flags
& PICFLAG_NOT_ALL_SIMPLE
))
1462 TREE_STATIC (init
) = 1;
1467 /* Given a structure or union value DATUM, construct and return
1468 the structure or union component which results from narrowing
1469 that value to the base specified in BASETYPE. For example, given the
1472 class L { int ii; };
1473 class A : L { ... };
1474 class B : L { ... };
1475 class C : A, B { ... };
1483 x.A::ii refers to the ii member of the L part of
1484 the A part of the C object named by X. In this case,
1485 DATUM would be x, and BASETYPE would be A.
1487 I used to think that this was nonconformant, that the standard specified
1488 that first we look up ii in A, then convert x to an L& and pull out the
1489 ii part. But in fact, it does say that we convert x to an A&; A here
1490 is known as the "naming class". (jason 2000-12-19)
1492 BINFO_P points to a variable initialized either to NULL_TREE or to the
1493 binfo for the specific base subobject we want to convert to. */
1496 build_scoped_ref (tree datum
, tree basetype
, tree
* binfo_p
)
1500 if (datum
== error_mark_node
)
1501 return error_mark_node
;
1505 binfo
= lookup_base (TREE_TYPE (datum
), basetype
, ba_check
,
1506 NULL
, tf_warning_or_error
);
1508 if (!binfo
|| binfo
== error_mark_node
)
1510 *binfo_p
= NULL_TREE
;
1512 error_not_base_type (basetype
, TREE_TYPE (datum
));
1513 return error_mark_node
;
1517 return build_base_path (PLUS_EXPR
, datum
, binfo
, 1,
1518 tf_warning_or_error
);
1521 /* Build a reference to an object specified by the C++ `->' operator.
1522 Usually this just involves dereferencing the object, but if the
1523 `->' operator is overloaded, then such overloads must be
1524 performed until an object which does not have the `->' operator
1525 overloaded is found. An error is reported when circular pointer
1526 delegation is detected. */
1529 build_x_arrow (location_t loc
, tree expr
, tsubst_flags_t complain
)
1531 tree orig_expr
= expr
;
1532 tree type
= TREE_TYPE (expr
);
1533 tree last_rval
= NULL_TREE
;
1534 vec
<tree
, va_gc
> *types_memoized
= NULL
;
1536 if (type
== error_mark_node
)
1537 return error_mark_node
;
1539 if (processing_template_decl
)
1541 if (type_dependent_expression_p (expr
))
1542 return build_min_nt_loc (loc
, ARROW_EXPR
, expr
);
1543 expr
= build_non_dependent_expr (expr
);
1546 if (MAYBE_CLASS_TYPE_P (type
))
1548 struct tinst_level
*actual_inst
= current_instantiation ();
1551 while ((expr
= build_new_op (loc
, COMPONENT_REF
,
1552 LOOKUP_NORMAL
, expr
, NULL_TREE
, NULL_TREE
,
1555 if (expr
== error_mark_node
)
1556 return error_mark_node
;
1558 if (fn
&& DECL_USE_TEMPLATE (fn
))
1559 push_tinst_level (fn
);
1562 if (vec_member (TREE_TYPE (expr
), types_memoized
))
1564 if (complain
& tf_error
)
1565 error ("circular pointer delegation detected");
1566 return error_mark_node
;
1569 vec_safe_push (types_memoized
, TREE_TYPE (expr
));
1573 while (current_instantiation () != actual_inst
)
1576 if (last_rval
== NULL_TREE
)
1578 if (complain
& tf_error
)
1579 error ("base operand of %<->%> has non-pointer type %qT", type
);
1580 return error_mark_node
;
1583 if (TREE_CODE (TREE_TYPE (last_rval
)) == REFERENCE_TYPE
)
1584 last_rval
= convert_from_reference (last_rval
);
1587 last_rval
= decay_conversion (expr
, complain
);
1589 if (TREE_CODE (TREE_TYPE (last_rval
)) == POINTER_TYPE
)
1591 if (processing_template_decl
)
1593 expr
= build_min (ARROW_EXPR
, TREE_TYPE (TREE_TYPE (last_rval
)),
1595 TREE_SIDE_EFFECTS (expr
) = TREE_SIDE_EFFECTS (last_rval
);
1599 return cp_build_indirect_ref (last_rval
, RO_NULL
, complain
);
1602 if (complain
& tf_error
)
1605 error ("result of %<operator->()%> yields non-pointer result");
1607 error ("base operand of %<->%> is not a pointer");
1609 return error_mark_node
;
1612 /* Return an expression for "DATUM .* COMPONENT". DATUM has not
1613 already been checked out to be of aggregate type. */
1616 build_m_component_ref (tree datum
, tree component
, tsubst_flags_t complain
)
1624 if (error_operand_p (datum
) || error_operand_p (component
))
1625 return error_mark_node
;
1627 datum
= mark_lvalue_use (datum
);
1628 component
= mark_rvalue_use (component
);
1630 ptrmem_type
= TREE_TYPE (component
);
1631 if (!TYPE_PTRMEM_P (ptrmem_type
))
1633 if (complain
& tf_error
)
1634 error ("%qE cannot be used as a member pointer, since it is of "
1635 "type %qT", component
, ptrmem_type
);
1636 return error_mark_node
;
1639 objtype
= TYPE_MAIN_VARIANT (TREE_TYPE (datum
));
1640 if (! MAYBE_CLASS_TYPE_P (objtype
))
1642 if (complain
& tf_error
)
1643 error ("cannot apply member pointer %qE to %qE, which is of "
1644 "non-class type %qT", component
, datum
, objtype
);
1645 return error_mark_node
;
1648 type
= TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type
);
1649 ctype
= complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type
));
1651 if (!COMPLETE_TYPE_P (ctype
))
1653 if (!same_type_p (ctype
, objtype
))
1659 binfo
= lookup_base (objtype
, ctype
, ba_check
, NULL
, complain
);
1664 if (complain
& tf_error
)
1665 error ("pointer to member type %qT incompatible with object "
1666 "type %qT", type
, objtype
);
1667 return error_mark_node
;
1669 else if (binfo
== error_mark_node
)
1670 return error_mark_node
;
1673 if (TYPE_PTRDATAMEM_P (ptrmem_type
))
1675 bool is_lval
= real_lvalue_p (datum
);
1678 /* Compute the type of the field, as described in [expr.ref].
1679 There's no such thing as a mutable pointer-to-member, so
1680 things are not as complex as they are for references to
1681 non-static data members. */
1682 type
= cp_build_qualified_type (type
,
1683 (cp_type_quals (type
)
1684 | cp_type_quals (TREE_TYPE (datum
))));
1686 datum
= build_address (datum
);
1688 /* Convert object to the correct base. */
1691 datum
= build_base_path (PLUS_EXPR
, datum
, binfo
, 1, complain
);
1692 if (datum
== error_mark_node
)
1693 return error_mark_node
;
1696 /* Build an expression for "object + offset" where offset is the
1697 value stored in the pointer-to-data-member. */
1698 ptype
= build_pointer_type (type
);
1699 datum
= fold_build_pointer_plus (fold_convert (ptype
, datum
), component
);
1700 datum
= cp_build_indirect_ref (datum
, RO_NULL
, complain
);
1701 if (datum
== error_mark_node
)
1702 return error_mark_node
;
1704 /* If the object expression was an rvalue, return an rvalue. */
1706 datum
= move (datum
);
1710 return build2 (OFFSET_REF
, type
, datum
, component
);
1713 /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */
1716 build_functional_cast (tree exp
, tree parms
, tsubst_flags_t complain
)
1718 /* This is either a call to a constructor,
1719 or a C cast in C++'s `functional' notation. */
1721 /* The type to which we are casting. */
1723 vec
<tree
, va_gc
> *parmvec
;
1725 if (exp
== error_mark_node
|| parms
== error_mark_node
)
1726 return error_mark_node
;
1728 if (TREE_CODE (exp
) == TYPE_DECL
)
1729 type
= TREE_TYPE (exp
);
1733 /* We need to check this explicitly, since value-initialization of
1734 arrays is allowed in other situations. */
1735 if (TREE_CODE (type
) == ARRAY_TYPE
)
1737 if (complain
& tf_error
)
1738 error ("functional cast to array type %qT", type
);
1739 return error_mark_node
;
1742 if (type_uses_auto (type
))
1744 if (complain
& tf_error
)
1745 error ("invalid use of %<auto%>");
1746 return error_mark_node
;
1749 if (processing_template_decl
)
1753 /* Diagnose this even in a template. We could also try harder
1754 to give all the usual errors when the type and args are
1756 if (TREE_CODE (type
) == REFERENCE_TYPE
&& !parms
)
1758 if (complain
& tf_error
)
1759 error ("invalid value-initialization of reference type");
1760 return error_mark_node
;
1763 t
= build_min (CAST_EXPR
, type
, parms
);
1764 /* We don't know if it will or will not have side effects. */
1765 TREE_SIDE_EFFECTS (t
) = 1;
1769 if (! MAYBE_CLASS_TYPE_P (type
))
1771 if (parms
== NULL_TREE
)
1773 if (VOID_TYPE_P (type
))
1774 return void_zero_node
;
1775 return build_value_init (cv_unqualified (type
), complain
);
1778 /* This must build a C cast. */
1779 parms
= build_x_compound_expr_from_list (parms
, ELK_FUNC_CAST
, complain
);
1780 return cp_build_c_cast (type
, parms
, complain
);
1783 /* Prepare to evaluate as a call to a constructor. If this expression
1784 is actually used, for example,
1786 return X (arg1, arg2, ...);
1788 then the slot being initialized will be filled in. */
1790 if (!complete_type_or_maybe_complain (type
, NULL_TREE
, complain
))
1791 return error_mark_node
;
1792 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
1793 return error_mark_node
;
1797 If the expression list is a single-expression, the type
1798 conversion is equivalent (in definedness, and if defined in
1799 meaning) to the corresponding cast expression. */
1800 if (parms
&& TREE_CHAIN (parms
) == NULL_TREE
)
1801 return cp_build_c_cast (type
, TREE_VALUE (parms
), complain
);
1805 The expression T(), where T is a simple-type-specifier for a
1806 non-array complete object type or the (possibly cv-qualified)
1807 void type, creates an rvalue of the specified type, which is
1808 value-initialized. */
1810 if (parms
== NULL_TREE
)
1812 exp
= build_value_init (type
, complain
);
1813 exp
= get_target_expr_sfinae (exp
, complain
);
1817 /* Call the constructor. */
1818 parmvec
= make_tree_vector ();
1819 for (; parms
!= NULL_TREE
; parms
= TREE_CHAIN (parms
))
1820 vec_safe_push (parmvec
, TREE_VALUE (parms
));
1821 exp
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
1822 &parmvec
, type
, LOOKUP_NORMAL
, complain
);
1823 release_tree_vector (parmvec
);
1825 if (exp
== error_mark_node
)
1826 return error_mark_node
;
1828 return build_cplus_new (type
, exp
, complain
);
1832 /* Add new exception specifier SPEC, to the LIST we currently have.
1833 If it's already in LIST then do nothing.
1834 Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1835 know what we're doing. */
1838 add_exception_specifier (tree list
, tree spec
, int complain
)
1843 diagnostic_t diag_type
= DK_UNSPECIFIED
; /* none */
1845 if (spec
== error_mark_node
)
1848 gcc_assert (spec
&& (!list
|| TREE_VALUE (list
)));
1850 /* [except.spec] 1, type in an exception specifier shall not be
1851 incomplete, or pointer or ref to incomplete other than pointer
1853 is_ptr
= TREE_CODE (core
) == POINTER_TYPE
;
1854 if (is_ptr
|| TREE_CODE (core
) == REFERENCE_TYPE
)
1855 core
= TREE_TYPE (core
);
1858 else if (VOID_TYPE_P (core
))
1860 else if (TREE_CODE (core
) == TEMPLATE_TYPE_PARM
)
1862 else if (processing_template_decl
)
1867 /* 15.4/1 says that types in an exception specifier must be complete,
1868 but it seems more reasonable to only require this on definitions
1869 and calls. So just give a pedwarn at this point; we will give an
1870 error later if we hit one of those two cases. */
1871 if (!COMPLETE_TYPE_P (complete_type (core
)))
1872 diag_type
= DK_PEDWARN
; /* pedwarn */
1879 for (probe
= list
; probe
; probe
= TREE_CHAIN (probe
))
1880 if (same_type_p (TREE_VALUE (probe
), spec
))
1883 list
= tree_cons (NULL_TREE
, spec
, list
);
1886 diag_type
= DK_ERROR
; /* error */
1888 if (diag_type
!= DK_UNSPECIFIED
1889 && (complain
& tf_warning_or_error
))
1890 cxx_incomplete_type_diagnostic (NULL_TREE
, core
, diag_type
);
1895 /* Like nothrow_spec_p, but don't abort on deferred noexcept. */
1898 nothrow_spec_p_uninst (const_tree spec
)
1900 if (DEFERRED_NOEXCEPT_SPEC_P (spec
))
1902 return nothrow_spec_p (spec
);
1905 /* Combine the two exceptions specifier lists LIST and ADD, and return
1906 their union. If FN is non-null, it's the source of ADD. */
1909 merge_exception_specifiers (tree list
, tree add
, tree fn
)
1911 tree noex
, orig_list
;
1913 /* No exception-specifier or noexcept(false) are less strict than
1914 anything else. Prefer the newer variant (LIST). */
1915 if (!list
|| list
== noexcept_false_spec
)
1917 else if (!add
|| add
== noexcept_false_spec
)
1920 /* noexcept(true) and throw() are stricter than anything else.
1921 As above, prefer the more recent one (LIST). */
1922 if (nothrow_spec_p_uninst (add
))
1925 noex
= TREE_PURPOSE (list
);
1926 if (DEFERRED_NOEXCEPT_SPEC_P (add
))
1928 /* If ADD is a deferred noexcept, we must have been called from
1929 process_subob_fn. For implicitly declared functions, we build up
1930 a list of functions to consider at instantiation time. */
1931 if (noex
&& operand_equal_p (noex
, boolean_true_node
, 0))
1933 gcc_assert (fn
&& (!noex
|| is_overloaded_fn (noex
)));
1934 noex
= build_overload (fn
, noex
);
1936 else if (nothrow_spec_p_uninst (list
))
1939 gcc_checking_assert (!TREE_PURPOSE (add
)
1940 || cp_tree_equal (noex
, TREE_PURPOSE (add
)));
1942 /* Combine the dynamic-exception-specifiers, if any. */
1944 for (; add
&& TREE_VALUE (add
); add
= TREE_CHAIN (add
))
1946 tree spec
= TREE_VALUE (add
);
1949 for (probe
= orig_list
; probe
&& TREE_VALUE (probe
);
1950 probe
= TREE_CHAIN (probe
))
1951 if (same_type_p (TREE_VALUE (probe
), spec
))
1955 spec
= build_tree_list (NULL_TREE
, spec
);
1956 TREE_CHAIN (spec
) = list
;
1961 /* Keep the noexcept-specifier at the beginning of the list. */
1962 if (noex
!= TREE_PURPOSE (list
))
1963 list
= tree_cons (noex
, TREE_VALUE (list
), TREE_CHAIN (list
));
1968 /* Subroutine of build_call. Ensure that each of the types in the
1969 exception specification is complete. Technically, 15.4/1 says that
1970 they need to be complete when we see a declaration of the function,
1971 but we should be able to get away with only requiring this when the
1972 function is defined or called. See also add_exception_specifier. */
1975 require_complete_eh_spec_types (tree fntype
, tree decl
)
1978 /* Don't complain about calls to op new. */
1979 if (decl
&& DECL_ARTIFICIAL (decl
))
1981 for (raises
= TYPE_RAISES_EXCEPTIONS (fntype
); raises
;
1982 raises
= TREE_CHAIN (raises
))
1984 tree type
= TREE_VALUE (raises
);
1985 if (type
&& !COMPLETE_TYPE_P (type
))
1989 ("call to function %qD which throws incomplete type %q#T",
1992 error ("call to function which throws incomplete type %q#T",
1999 #include "gt-cp-typeck2.h"