1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2019 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization. */
29 #include "coretypes.h"
32 #include "stor-layout.h"
36 #include "c-family/c-objc.h"
37 #include "c-family/c-ubsan.h"
39 #include "gcc-rich-location.h"
40 #include "stringpool.h"
45 static tree
cp_build_addr_expr_strict (tree
, tsubst_flags_t
);
46 static tree
cp_build_function_call (tree
, tree
, tsubst_flags_t
);
47 static tree
pfn_from_ptrmemfunc (tree
);
48 static tree
delta_from_ptrmemfunc (tree
);
49 static tree
convert_for_assignment (tree
, tree
, impl_conv_rhs
, tree
, int,
51 static tree
cp_pointer_int_sum (location_t
, enum tree_code
, tree
, tree
,
53 static tree
rationalize_conditional_expr (enum tree_code
, tree
,
55 static bool comp_ptr_ttypes_real (tree
, tree
, int);
56 static bool comp_except_types (tree
, tree
, bool);
57 static bool comp_array_types (const_tree
, const_tree
, compare_bounds_t
, bool);
58 static tree
pointer_diff (location_t
, tree
, tree
, tree
, tsubst_flags_t
, tree
*);
59 static tree
get_delta_difference (tree
, tree
, bool, bool, tsubst_flags_t
);
60 static void casts_away_constness_r (tree
*, tree
*, tsubst_flags_t
);
61 static bool casts_away_constness (tree
, tree
, tsubst_flags_t
);
62 static bool maybe_warn_about_returning_address_of_local (tree
);
63 static tree
lookup_destructor (tree
, tree
, tree
, tsubst_flags_t
);
64 static void error_args_num (location_t
, tree
, bool);
65 static int convert_arguments (tree
, vec
<tree
, va_gc
> **, tree
, int,
67 static bool is_std_move_p (tree
);
68 static bool is_std_forward_p (tree
);
70 /* Do `exp = require_complete_type (exp);' to make sure exp
71 does not have an incomplete type. (That includes void types.)
72 Returns error_mark_node if the VALUE does not have
73 complete type when this function returns. */
76 require_complete_type_sfinae (tree value
, tsubst_flags_t complain
)
80 if (processing_template_decl
|| value
== error_mark_node
)
83 if (TREE_CODE (value
) == OVERLOAD
)
84 type
= unknown_type_node
;
86 type
= TREE_TYPE (value
);
88 if (type
== error_mark_node
)
89 return error_mark_node
;
91 /* First, detect a valid value with a complete type. */
92 if (COMPLETE_TYPE_P (type
))
95 if (complete_type_or_maybe_complain (type
, value
, complain
))
98 return error_mark_node
;
102 require_complete_type (tree value
)
104 return require_complete_type_sfinae (value
, tf_warning_or_error
);
107 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
108 a template instantiation, do the instantiation. Returns TYPE,
109 whether or not it could be completed, unless something goes
110 horribly wrong, in which case the error_mark_node is returned. */
113 complete_type (tree type
)
115 if (type
== NULL_TREE
)
116 /* Rather than crash, we return something sure to cause an error
118 return error_mark_node
;
120 if (type
== error_mark_node
|| COMPLETE_TYPE_P (type
))
122 else if (TREE_CODE (type
) == ARRAY_TYPE
)
124 tree t
= complete_type (TREE_TYPE (type
));
125 unsigned int needs_constructing
, has_nontrivial_dtor
;
126 if (COMPLETE_TYPE_P (t
) && !dependent_type_p (type
))
129 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
131 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
132 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
134 TYPE_NEEDS_CONSTRUCTING (t
) = needs_constructing
;
135 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = has_nontrivial_dtor
;
138 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
139 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
144 /* Like complete_type, but issue an error if the TYPE cannot be completed.
145 VALUE is used for informative diagnostics.
146 Returns NULL_TREE if the type cannot be made complete. */
149 complete_type_or_maybe_complain (tree type
, tree value
, tsubst_flags_t complain
)
151 type
= complete_type (type
);
152 if (type
== error_mark_node
)
153 /* We already issued an error. */
155 else if (!COMPLETE_TYPE_P (type
))
157 if (complain
& tf_error
)
158 cxx_incomplete_type_diagnostic (value
, type
, DK_ERROR
);
166 complete_type_or_else (tree type
, tree value
)
168 return complete_type_or_maybe_complain (type
, value
, tf_warning_or_error
);
172 /* Return the common type of two parameter lists.
173 We assume that comptypes has already been done and returned 1;
174 if that isn't so, this may crash.
176 As an optimization, free the space we allocate if the parameter
177 lists are already common. */
180 commonparms (tree p1
, tree p2
)
182 tree oldargs
= p1
, newargs
, n
;
186 len
= list_length (p1
);
187 newargs
= tree_last (p1
);
189 if (newargs
== void_list_node
)
198 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
203 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
205 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
207 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
210 else if (! TREE_PURPOSE (p1
))
212 if (TREE_PURPOSE (p2
))
214 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
220 if (simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)) != 1)
222 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
224 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
227 TREE_VALUE (n
) = merge_types (TREE_VALUE (p1
), TREE_VALUE (p2
));
230 TREE_VALUE (n
) = TREE_VALUE (p1
);
238 /* Given a type, perhaps copied for a typedef,
239 find the "original" version of it. */
241 original_type (tree t
)
243 int quals
= cp_type_quals (t
);
244 while (t
!= error_mark_node
245 && TYPE_NAME (t
) != NULL_TREE
)
247 tree x
= TYPE_NAME (t
);
248 if (TREE_CODE (x
) != TYPE_DECL
)
250 x
= DECL_ORIGINAL_TYPE (x
);
255 return cp_build_qualified_type (t
, quals
);
258 /* Return the common type for two arithmetic types T1 and T2 under the
259 usual arithmetic conversions. The default conversions have already
260 been applied, and enumerated types converted to their compatible
264 cp_common_type (tree t1
, tree t2
)
266 enum tree_code code1
= TREE_CODE (t1
);
267 enum tree_code code2
= TREE_CODE (t2
);
272 /* In what follows, we slightly generalize the rules given in [expr] so
273 as to deal with `long long' and `complex'. First, merge the
275 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
277 if (SCOPED_ENUM_P (t1
) || SCOPED_ENUM_P (t2
))
279 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
280 return build_type_attribute_variant (t1
, attributes
);
285 /* FIXME: Attributes. */
286 gcc_assert (ARITHMETIC_TYPE_P (t1
)
287 || VECTOR_TYPE_P (t1
)
288 || UNSCOPED_ENUM_P (t1
));
289 gcc_assert (ARITHMETIC_TYPE_P (t2
)
290 || VECTOR_TYPE_P (t2
)
291 || UNSCOPED_ENUM_P (t2
));
293 /* If one type is complex, form the common type of the non-complex
294 components, then make that complex. Use T1 or T2 if it is the
296 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
298 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
299 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
301 = type_after_usual_arithmetic_conversions (subtype1
, subtype2
);
303 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
304 return build_type_attribute_variant (t1
, attributes
);
305 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
306 return build_type_attribute_variant (t2
, attributes
);
308 return build_type_attribute_variant (build_complex_type (subtype
),
312 if (code1
== VECTOR_TYPE
)
314 /* When we get here we should have two vectors of the same size.
315 Just prefer the unsigned one if present. */
316 if (TYPE_UNSIGNED (t1
))
317 return build_type_attribute_variant (t1
, attributes
);
319 return build_type_attribute_variant (t2
, attributes
);
322 /* If only one is real, use it as the result. */
323 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
324 return build_type_attribute_variant (t1
, attributes
);
325 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
326 return build_type_attribute_variant (t2
, attributes
);
328 /* Both real or both integers; use the one with greater precision. */
329 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
330 return build_type_attribute_variant (t1
, attributes
);
331 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
332 return build_type_attribute_variant (t2
, attributes
);
334 /* The types are the same; no need to do anything fancy. */
335 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
336 return build_type_attribute_variant (t1
, attributes
);
338 if (code1
!= REAL_TYPE
)
340 /* If one is unsigned long long, then convert the other to unsigned
342 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_unsigned_type_node
)
343 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_unsigned_type_node
))
344 return build_type_attribute_variant (long_long_unsigned_type_node
,
346 /* If one is a long long, and the other is an unsigned long, and
347 long long can represent all the values of an unsigned long, then
348 convert to a long long. Otherwise, convert to an unsigned long
349 long. Otherwise, if either operand is long long, convert the
352 Since we're here, we know the TYPE_PRECISION is the same;
353 therefore converting to long long cannot represent all the values
354 of an unsigned long, so we choose unsigned long long in that
356 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_integer_type_node
)
357 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_integer_type_node
))
359 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
360 ? long_long_unsigned_type_node
361 : long_long_integer_type_node
);
362 return build_type_attribute_variant (t
, attributes
);
365 /* Go through the same procedure, but for longs. */
366 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_unsigned_type_node
)
367 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_unsigned_type_node
))
368 return build_type_attribute_variant (long_unsigned_type_node
,
370 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_integer_type_node
)
371 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_integer_type_node
))
373 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
374 ? long_unsigned_type_node
: long_integer_type_node
);
375 return build_type_attribute_variant (t
, attributes
);
378 /* For __intN types, either the type is __int128 (and is lower
379 priority than the types checked above, but higher than other
380 128-bit types) or it's known to not be the same size as other
381 types (enforced in toplev.c). Prefer the unsigned type. */
382 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
384 if (int_n_enabled_p
[i
]
385 && (same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].signed_type
)
386 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].signed_type
)
387 || same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].unsigned_type
)
388 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].unsigned_type
)))
390 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
391 ? int_n_trees
[i
].unsigned_type
392 : int_n_trees
[i
].signed_type
);
393 return build_type_attribute_variant (t
, attributes
);
397 /* Otherwise prefer the unsigned one. */
398 if (TYPE_UNSIGNED (t1
))
399 return build_type_attribute_variant (t1
, attributes
);
401 return build_type_attribute_variant (t2
, attributes
);
405 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_double_type_node
)
406 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_double_type_node
))
407 return build_type_attribute_variant (long_double_type_node
,
409 if (same_type_p (TYPE_MAIN_VARIANT (t1
), double_type_node
)
410 || same_type_p (TYPE_MAIN_VARIANT (t2
), double_type_node
))
411 return build_type_attribute_variant (double_type_node
,
413 if (same_type_p (TYPE_MAIN_VARIANT (t1
), float_type_node
)
414 || same_type_p (TYPE_MAIN_VARIANT (t2
), float_type_node
))
415 return build_type_attribute_variant (float_type_node
,
418 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
419 the standard C++ floating-point types. Logic earlier in this
420 function has already eliminated the possibility that
421 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
422 compelling reason to choose one or the other. */
423 return build_type_attribute_variant (t1
, attributes
);
427 /* T1 and T2 are arithmetic or enumeration types. Return the type
428 that will result from the "usual arithmetic conversions" on T1 and
429 T2 as described in [expr]. */
432 type_after_usual_arithmetic_conversions (tree t1
, tree t2
)
434 gcc_assert (ARITHMETIC_TYPE_P (t1
)
435 || VECTOR_TYPE_P (t1
)
436 || UNSCOPED_ENUM_P (t1
));
437 gcc_assert (ARITHMETIC_TYPE_P (t2
)
438 || VECTOR_TYPE_P (t2
)
439 || UNSCOPED_ENUM_P (t2
));
441 /* Perform the integral promotions. We do not promote real types here. */
442 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1
)
443 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2
))
445 t1
= type_promotes_to (t1
);
446 t2
= type_promotes_to (t2
);
449 return cp_common_type (t1
, t2
);
453 composite_pointer_error (diagnostic_t kind
, tree t1
, tree t2
,
454 composite_pointer_operation operation
)
459 emit_diagnostic (kind
, input_location
, 0,
460 "comparison between "
461 "distinct pointer types %qT and %qT lacks a cast",
465 emit_diagnostic (kind
, input_location
, 0,
466 "conversion between "
467 "distinct pointer types %qT and %qT lacks a cast",
470 case CPO_CONDITIONAL_EXPR
:
471 emit_diagnostic (kind
, input_location
, 0,
472 "conditional expression between "
473 "distinct pointer types %qT and %qT lacks a cast",
481 /* Subroutine of composite_pointer_type to implement the recursive
482 case. See that function for documentation of the parameters. */
485 composite_pointer_type_r (tree t1
, tree t2
,
486 composite_pointer_operation operation
,
487 tsubst_flags_t complain
)
494 /* Determine the types pointed to by T1 and T2. */
497 pointee1
= TREE_TYPE (t1
);
498 pointee2
= TREE_TYPE (t2
);
502 pointee1
= TYPE_PTRMEM_POINTED_TO_TYPE (t1
);
503 pointee2
= TYPE_PTRMEM_POINTED_TO_TYPE (t2
);
508 Otherwise, the composite pointer type is a pointer type
509 similar (_conv.qual_) to the type of one of the operands,
510 with a cv-qualification signature (_conv.qual_) that is the
511 union of the cv-qualification signatures of the operand
513 if (same_type_ignoring_top_level_qualifiers_p (pointee1
, pointee2
))
514 result_type
= pointee1
;
515 else if ((TYPE_PTR_P (pointee1
) && TYPE_PTR_P (pointee2
))
516 || (TYPE_PTRMEM_P (pointee1
) && TYPE_PTRMEM_P (pointee2
)))
518 result_type
= composite_pointer_type_r (pointee1
, pointee2
, operation
,
520 if (result_type
== error_mark_node
)
521 return error_mark_node
;
525 if (complain
& tf_error
)
526 composite_pointer_error (DK_PERMERROR
, t1
, t2
, operation
);
528 return error_mark_node
;
529 result_type
= void_type_node
;
531 result_type
= cp_build_qualified_type (result_type
,
532 (cp_type_quals (pointee1
)
533 | cp_type_quals (pointee2
)));
534 /* If the original types were pointers to members, so is the
536 if (TYPE_PTRMEM_P (t1
))
538 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
539 TYPE_PTRMEM_CLASS_TYPE (t2
)))
541 if (complain
& tf_error
)
542 composite_pointer_error (DK_PERMERROR
, t1
, t2
, operation
);
544 return error_mark_node
;
546 result_type
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
550 result_type
= build_pointer_type (result_type
);
552 /* Merge the attributes. */
553 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
554 return build_type_attribute_variant (result_type
, attributes
);
557 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
558 ARG1 and ARG2 are the values with those types. The OPERATION is to
559 describe the operation between the pointer types,
560 in case an error occurs.
562 This routine also implements the computation of a common type for
563 pointers-to-members as per [expr.eq]. */
566 composite_pointer_type (tree t1
, tree t2
, tree arg1
, tree arg2
,
567 composite_pointer_operation operation
,
568 tsubst_flags_t complain
)
575 If one operand is a null pointer constant, the composite pointer
576 type is the type of the other operand. */
577 if (null_ptr_cst_p (arg1
))
579 if (null_ptr_cst_p (arg2
))
586 If one of the operands has type "pointer to cv1 void*", then
587 the other has type "pointer to cv2T", and the composite pointer
588 type is "pointer to cv12 void", where cv12 is the union of cv1
591 If either type is a pointer to void, make sure it is T1. */
592 if (TYPE_PTR_P (t2
) && VOID_TYPE_P (TREE_TYPE (t2
)))
595 /* Now, if T1 is a pointer to void, merge the qualifiers. */
596 if (TYPE_PTR_P (t1
) && VOID_TYPE_P (TREE_TYPE (t1
)))
601 if (TYPE_PTRFN_P (t2
))
603 if (complain
& tf_error
)
608 pedwarn (input_location
, OPT_Wpedantic
,
609 "ISO C++ forbids comparison between pointer "
610 "of type %<void *%> and pointer-to-function");
613 pedwarn (input_location
, OPT_Wpedantic
,
614 "ISO C++ forbids conversion between pointer "
615 "of type %<void *%> and pointer-to-function");
617 case CPO_CONDITIONAL_EXPR
:
618 pedwarn (input_location
, OPT_Wpedantic
,
619 "ISO C++ forbids conditional expression between "
620 "pointer of type %<void *%> and "
621 "pointer-to-function");
628 return error_mark_node
;
631 = cp_build_qualified_type (void_type_node
,
632 (cp_type_quals (TREE_TYPE (t1
))
633 | cp_type_quals (TREE_TYPE (t2
))));
634 result_type
= build_pointer_type (result_type
);
635 /* Merge the attributes. */
636 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
637 return build_type_attribute_variant (result_type
, attributes
);
640 if (c_dialect_objc () && TYPE_PTR_P (t1
)
643 if (objc_have_common_type (t1
, t2
, -3, NULL_TREE
))
644 return objc_common_type (t1
, t2
);
647 /* if T1 or T2 is "pointer to noexcept function" and the other type is
648 "pointer to function", where the function types are otherwise the same,
649 "pointer to function" */
650 if (fnptr_conv_p (t1
, t2
))
652 if (fnptr_conv_p (t2
, t1
))
655 /* [expr.eq] permits the application of a pointer conversion to
656 bring the pointers to a common type. */
657 if (TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
)
658 && CLASS_TYPE_P (TREE_TYPE (t1
))
659 && CLASS_TYPE_P (TREE_TYPE (t2
))
660 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1
),
663 class1
= TREE_TYPE (t1
);
664 class2
= TREE_TYPE (t2
);
666 if (DERIVED_FROM_P (class1
, class2
))
667 t2
= (build_pointer_type
668 (cp_build_qualified_type (class1
, cp_type_quals (class2
))));
669 else if (DERIVED_FROM_P (class2
, class1
))
670 t1
= (build_pointer_type
671 (cp_build_qualified_type (class2
, cp_type_quals (class1
))));
674 if (complain
& tf_error
)
675 composite_pointer_error (DK_ERROR
, t1
, t2
, operation
);
676 return error_mark_node
;
679 /* [expr.eq] permits the application of a pointer-to-member
680 conversion to change the class type of one of the types. */
681 else if (TYPE_PTRMEM_P (t1
)
682 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
683 TYPE_PTRMEM_CLASS_TYPE (t2
)))
685 class1
= TYPE_PTRMEM_CLASS_TYPE (t1
);
686 class2
= TYPE_PTRMEM_CLASS_TYPE (t2
);
688 if (DERIVED_FROM_P (class1
, class2
))
689 t1
= build_ptrmem_type (class2
, TYPE_PTRMEM_POINTED_TO_TYPE (t1
));
690 else if (DERIVED_FROM_P (class2
, class1
))
691 t2
= build_ptrmem_type (class1
, TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
694 if (complain
& tf_error
)
698 error ("comparison between distinct "
699 "pointer-to-member types %qT and %qT lacks a cast",
703 error ("conversion between distinct "
704 "pointer-to-member types %qT and %qT lacks a cast",
707 case CPO_CONDITIONAL_EXPR
:
708 error ("conditional expression between distinct "
709 "pointer-to-member types %qT and %qT lacks a cast",
715 return error_mark_node
;
719 return composite_pointer_type_r (t1
, t2
, operation
, complain
);
722 /* Return the merged type of two types.
723 We assume that comptypes has already been done and returned 1;
724 if that isn't so, this may crash.
726 This just combines attributes and default arguments; any other
727 differences would cause the two types to compare unalike. */
730 merge_types (tree t1
, tree t2
)
732 enum tree_code code1
;
733 enum tree_code code2
;
736 /* Save time if the two types are the same. */
739 if (original_type (t1
) == original_type (t2
))
742 /* If one type is nonsense, use the other. */
743 if (t1
== error_mark_node
)
745 if (t2
== error_mark_node
)
748 /* Handle merging an auto redeclaration with a previous deduced
753 /* Merge the attributes. */
754 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
756 if (TYPE_PTRMEMFUNC_P (t1
))
757 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
758 if (TYPE_PTRMEMFUNC_P (t2
))
759 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
761 code1
= TREE_CODE (t1
);
762 code2
= TREE_CODE (t2
);
765 gcc_assert (code1
== TYPENAME_TYPE
|| code2
== TYPENAME_TYPE
);
766 if (code1
== TYPENAME_TYPE
)
768 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
769 code1
= TREE_CODE (t1
);
773 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
774 code2
= TREE_CODE (t2
);
782 /* For two pointers, do this recursively on the target type. */
784 tree target
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
785 int quals
= cp_type_quals (t1
);
787 if (code1
== POINTER_TYPE
)
789 t1
= build_pointer_type (target
);
790 if (TREE_CODE (target
) == METHOD_TYPE
)
791 t1
= build_ptrmemfunc_type (t1
);
794 t1
= cp_build_reference_type (target
, TYPE_REF_IS_RVALUE (t1
));
795 t1
= build_type_attribute_variant (t1
, attributes
);
796 t1
= cp_build_qualified_type (t1
, quals
);
805 quals
= cp_type_quals (t1
);
806 pointee
= merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1
),
807 TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
808 t1
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
810 t1
= cp_build_qualified_type (t1
, quals
);
816 tree elt
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
817 /* Save space: see if the result is identical to one of the args. */
818 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
819 return build_type_attribute_variant (t1
, attributes
);
820 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
821 return build_type_attribute_variant (t2
, attributes
);
822 /* Merge the element types, and have a size if either arg has one. */
823 t1
= build_cplus_array_type
824 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
829 /* Function types: prefer the one that specified arg types.
830 If both do, merge the arg types. Also merge the return types. */
832 tree valtype
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
833 tree p1
= TYPE_ARG_TYPES (t1
);
834 tree p2
= TYPE_ARG_TYPES (t2
);
837 /* Save space: see if the result is identical to one of the args. */
838 if (valtype
== TREE_TYPE (t1
) && ! p2
)
839 return cp_build_type_attribute_variant (t1
, attributes
);
840 if (valtype
== TREE_TYPE (t2
) && ! p1
)
841 return cp_build_type_attribute_variant (t2
, attributes
);
843 /* Simple way if one arg fails to specify argument types. */
844 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
846 else if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
849 parms
= commonparms (p1
, p2
);
851 cp_cv_quals quals
= type_memfn_quals (t1
);
852 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
853 gcc_assert (quals
== type_memfn_quals (t2
));
854 gcc_assert (rqual
== type_memfn_rqual (t2
));
856 tree rval
= build_function_type (valtype
, parms
);
857 rval
= apply_memfn_quals (rval
, quals
);
858 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
859 TYPE_RAISES_EXCEPTIONS (t2
));
860 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
861 t1
= build_cp_fntype_variant (rval
, rqual
, raises
, late_return_type_p
);
867 /* Get this value the long way, since TYPE_METHOD_BASETYPE
868 is just the main variant of this. */
869 tree basetype
= class_of_this_parm (t2
);
870 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
871 TYPE_RAISES_EXCEPTIONS (t2
));
872 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
874 bool late_return_type_1_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
876 /* If this was a member function type, get back to the
877 original type of type member function (i.e., without
878 the class instance variable up front. */
879 t1
= build_function_type (TREE_TYPE (t1
),
880 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
881 t2
= build_function_type (TREE_TYPE (t2
),
882 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
883 t3
= merge_types (t1
, t2
);
884 t3
= build_method_type_directly (basetype
, TREE_TYPE (t3
),
885 TYPE_ARG_TYPES (t3
));
886 t1
= build_cp_fntype_variant (t3
, rqual
, raises
, late_return_type_1_p
);
891 /* There is no need to merge attributes into a TYPENAME_TYPE.
892 When the type is instantiated it will have whatever
893 attributes result from the instantiation. */
897 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
899 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
904 return cp_build_type_attribute_variant (t1
, attributes
);
907 /* Return the ARRAY_TYPE type without its domain. */
910 strip_array_domain (tree type
)
913 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
914 if (TYPE_DOMAIN (type
) == NULL_TREE
)
916 t2
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
917 return cp_build_type_attribute_variant (t2
, TYPE_ATTRIBUTES (type
));
920 /* Wrapper around cp_common_type that is used by c-common.c and other
921 front end optimizations that remove promotions.
923 Return the common type for two arithmetic types T1 and T2 under the
924 usual arithmetic conversions. The default conversions have already
925 been applied, and enumerated types converted to their compatible
929 common_type (tree t1
, tree t2
)
931 /* If one type is nonsense, use the other */
932 if (t1
== error_mark_node
)
934 if (t2
== error_mark_node
)
937 return cp_common_type (t1
, t2
);
940 /* Return the common type of two pointer types T1 and T2. This is the
941 type for the result of most arithmetic operations if the operands
942 have the given two types.
944 We assume that comp_target_types has already been done and returned
945 nonzero; if that isn't so, this may crash. */
948 common_pointer_type (tree t1
, tree t2
)
950 gcc_assert ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
951 || (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
952 || (TYPE_PTRMEMFUNC_P (t1
) && TYPE_PTRMEMFUNC_P (t2
)));
954 return composite_pointer_type (t1
, t2
, error_mark_node
, error_mark_node
,
955 CPO_CONVERSION
, tf_warning_or_error
);
958 /* Compare two exception specifier types for exactness or subsetness, if
959 allowed. Returns false for mismatch, true for match (same, or
962 [except.spec] "If a class X ... objects of class X or any class publicly
963 and unambiguously derived from X. Similarly, if a pointer type Y * ...
964 exceptions of type Y * or that are pointers to any type publicly and
965 unambiguously derived from Y. Otherwise a function only allows exceptions
966 that have the same type ..."
967 This does not mention cv qualifiers and is different to what throw
968 [except.throw] and catch [except.catch] will do. They will ignore the
969 top level cv qualifiers, and allow qualifiers in the pointer to class
972 We implement the letter of the standard. */
975 comp_except_types (tree a
, tree b
, bool exact
)
977 if (same_type_p (a
, b
))
981 if (cp_type_quals (a
) || cp_type_quals (b
))
984 if (TYPE_PTR_P (a
) && TYPE_PTR_P (b
))
988 if (cp_type_quals (a
) || cp_type_quals (b
))
992 if (TREE_CODE (a
) != RECORD_TYPE
993 || TREE_CODE (b
) != RECORD_TYPE
)
996 if (publicly_uniquely_derived_p (a
, b
))
1002 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1003 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1004 If EXACT is ce_type, the C++17 type compatibility rules apply.
1005 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1006 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1007 are unordered, but we've already filtered out duplicates. Most lists will
1008 be in order, we should try to make use of that. */
1011 comp_except_specs (const_tree t1
, const_tree t2
, int exact
)
1020 /* First handle noexcept. */
1021 if (exact
< ce_exact
)
1023 if (exact
== ce_type
1024 && (canonical_eh_spec (CONST_CAST_TREE (t1
))
1025 == canonical_eh_spec (CONST_CAST_TREE (t2
))))
1028 /* noexcept(false) is compatible with no exception-specification,
1029 and less strict than any spec. */
1030 if (t1
== noexcept_false_spec
)
1031 return t2
== NULL_TREE
|| exact
== ce_derived
;
1032 /* Even a derived noexcept(false) is compatible with no
1033 exception-specification. */
1034 if (t2
== noexcept_false_spec
)
1035 return t1
== NULL_TREE
;
1037 /* Otherwise, if we aren't looking for an exact match, noexcept is
1038 equivalent to throw(). */
1039 if (t1
== noexcept_true_spec
)
1040 t1
= empty_except_spec
;
1041 if (t2
== noexcept_true_spec
)
1042 t2
= empty_except_spec
;
1045 /* If any noexcept is left, it is only comparable to itself;
1046 either we're looking for an exact match or we're redeclaring a
1047 template with dependent noexcept. */
1048 if ((t1
&& TREE_PURPOSE (t1
))
1049 || (t2
&& TREE_PURPOSE (t2
)))
1051 && cp_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)));
1053 if (t1
== NULL_TREE
) /* T1 is ... */
1054 return t2
== NULL_TREE
|| exact
== ce_derived
;
1055 if (!TREE_VALUE (t1
)) /* t1 is EMPTY */
1056 return t2
!= NULL_TREE
&& !TREE_VALUE (t2
);
1057 if (t2
== NULL_TREE
) /* T2 is ... */
1059 if (TREE_VALUE (t1
) && !TREE_VALUE (t2
)) /* T2 is EMPTY, T1 is not */
1060 return exact
== ce_derived
;
1062 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1063 Count how many we find, to determine exactness. For exact matching and
1064 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1066 for (base
= t1
; t2
!= NULL_TREE
; t2
= TREE_CHAIN (t2
))
1068 for (probe
= base
; probe
!= NULL_TREE
; probe
= TREE_CHAIN (probe
))
1070 tree a
= TREE_VALUE (probe
);
1071 tree b
= TREE_VALUE (t2
);
1073 if (comp_except_types (a
, b
, exact
))
1075 if (probe
== base
&& exact
> ce_derived
)
1076 base
= TREE_CHAIN (probe
);
1081 if (probe
== NULL_TREE
)
1084 return exact
== ce_derived
|| base
== NULL_TREE
|| length
== list_length (t1
);
1087 /* Compare the array types T1 and T2. CB says how we should behave when
1088 comparing array bounds: bounds_none doesn't allow dimensionless arrays,
1089 bounds_either says than any array can be [], bounds_first means that
1090 onlt T1 can be an array with unknown bounds. STRICT is true if
1091 qualifiers must match when comparing the types of the array elements. */
1094 comp_array_types (const_tree t1
, const_tree t2
, compare_bounds_t cb
,
1104 /* The type of the array elements must be the same. */
1106 ? !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
))
1107 : !similar_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1110 d1
= TYPE_DOMAIN (t1
);
1111 d2
= TYPE_DOMAIN (t2
);
1116 /* If one of the arrays is dimensionless, and the other has a
1117 dimension, they are of different types. However, it is valid to
1125 declarations for an array object can specify
1126 array types that differ by the presence or absence of a major
1127 array bound (_dcl.array_). */
1129 return cb
>= bounds_either
;
1131 return cb
== bounds_either
;
1133 /* Check that the dimensions are the same. */
1135 if (!cp_tree_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
)))
1137 max1
= TYPE_MAX_VALUE (d1
);
1138 max2
= TYPE_MAX_VALUE (d2
);
1140 if (!cp_tree_equal (max1
, max2
))
1146 /* Compare the relative position of T1 and T2 into their respective
1147 template parameter list.
1148 T1 and T2 must be template parameter types.
1149 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1152 comp_template_parms_position (tree t1
, tree t2
)
1154 tree index1
, index2
;
1155 gcc_assert (t1
&& t2
1156 && TREE_CODE (t1
) == TREE_CODE (t2
)
1157 && (TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
1158 || TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
1159 || TREE_CODE (t1
) == TEMPLATE_TYPE_PARM
));
1161 index1
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1
));
1162 index2
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2
));
1164 /* Then compare their relative position. */
1165 if (TEMPLATE_PARM_IDX (index1
) != TEMPLATE_PARM_IDX (index2
)
1166 || TEMPLATE_PARM_LEVEL (index1
) != TEMPLATE_PARM_LEVEL (index2
)
1167 || (TEMPLATE_PARM_PARAMETER_PACK (index1
)
1168 != TEMPLATE_PARM_PARAMETER_PACK (index2
)))
1171 /* In C++14 we can end up comparing 'auto' to a normal template
1172 parameter. Don't confuse them. */
1173 if (cxx_dialect
>= cxx14
&& (is_auto (t1
) || is_auto (t2
)))
1174 return TYPE_IDENTIFIER (t1
) == TYPE_IDENTIFIER (t2
);
1179 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1182 cxx_safe_arg_type_equiv_p (tree t1
, tree t2
)
1184 t1
= TYPE_MAIN_VARIANT (t1
);
1185 t2
= TYPE_MAIN_VARIANT (t2
);
1191 /* The signedness of the parameter matters only when an integral
1192 type smaller than int is promoted to int, otherwise only the
1193 precision of the parameter matters.
1194 This check should make sure that the callee does not see
1195 undefined values in argument registers. */
1196 if (INTEGRAL_TYPE_P (t1
)
1197 && INTEGRAL_TYPE_P (t2
)
1198 && TYPE_PRECISION (t1
) == TYPE_PRECISION (t2
)
1199 && (TYPE_UNSIGNED (t1
) == TYPE_UNSIGNED (t2
)
1200 || !targetm
.calls
.promote_prototypes (NULL_TREE
)
1201 || TYPE_PRECISION (t1
) >= TYPE_PRECISION (integer_type_node
)))
1204 return same_type_p (t1
, t2
);
1207 /* Check if a type cast between two function types can be considered safe. */
1210 cxx_safe_function_type_cast_p (tree t1
, tree t2
)
1212 if (TREE_TYPE (t1
) == void_type_node
&&
1213 TYPE_ARG_TYPES (t1
) == void_list_node
)
1216 if (TREE_TYPE (t2
) == void_type_node
&&
1217 TYPE_ARG_TYPES (t2
) == void_list_node
)
1220 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1223 for (t1
= TYPE_ARG_TYPES (t1
), t2
= TYPE_ARG_TYPES (t2
);
1225 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1226 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1232 /* Subroutine in comptypes. */
1235 structural_comptypes (tree t1
, tree t2
, int strict
)
1240 /* Suppress errors caused by previously reported errors. */
1241 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1244 gcc_assert (TYPE_P (t1
) && TYPE_P (t2
));
1246 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1247 current instantiation. */
1248 if (TREE_CODE (t1
) == TYPENAME_TYPE
)
1249 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
1251 if (TREE_CODE (t2
) == TYPENAME_TYPE
)
1252 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
1254 if (TYPE_PTRMEMFUNC_P (t1
))
1255 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
1256 if (TYPE_PTRMEMFUNC_P (t2
))
1257 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
1259 /* Different classes of types can't be compatible. */
1260 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1263 /* Qualifiers must match. For array types, we will check when we
1264 recur on the array element types. */
1265 if (TREE_CODE (t1
) != ARRAY_TYPE
1266 && cp_type_quals (t1
) != cp_type_quals (t2
))
1268 if (TREE_CODE (t1
) == FUNCTION_TYPE
1269 && type_memfn_quals (t1
) != type_memfn_quals (t2
))
1271 /* Need to check this before TYPE_MAIN_VARIANT.
1272 FIXME function qualifiers should really change the main variant. */
1273 if (FUNC_OR_METHOD_TYPE_P (t1
))
1275 if (type_memfn_rqual (t1
) != type_memfn_rqual (t2
))
1277 if (flag_noexcept_type
1278 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1
),
1279 TYPE_RAISES_EXCEPTIONS (t2
),
1284 /* Allow for two different type nodes which have essentially the same
1285 definition. Note that we already checked for equality of the type
1286 qualifiers (just above). */
1288 if (TREE_CODE (t1
) != ARRAY_TYPE
1289 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1293 /* Compare the types. Break out if they could be the same. */
1294 switch (TREE_CODE (t1
))
1298 /* All void and bool types are the same. */
1302 case FIXED_POINT_TYPE
:
1304 /* With these nodes, we can't determine type equivalence by
1305 looking at what is stored in the nodes themselves, because
1306 two nodes might have different TYPE_MAIN_VARIANTs but still
1307 represent the same type. For example, wchar_t and int could
1308 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1309 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1310 and are distinct types. On the other hand, int and the
1313 typedef int INT __attribute((may_alias));
1315 have identical properties, different TYPE_MAIN_VARIANTs, but
1316 represent the same type. The canonical type system keeps
1317 track of equivalence in this case, so we fall back on it. */
1318 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1320 case TEMPLATE_TEMPLATE_PARM
:
1321 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1322 if (!comp_template_parms_position (t1
, t2
))
1324 if (!comp_template_parms
1325 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1
)),
1326 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2
))))
1328 if (TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
)
1330 /* Don't check inheritance. */
1331 strict
= COMPARE_STRICT
;
1336 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
1337 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
1338 || TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1339 && comp_template_args (TYPE_TI_ARGS (t1
), TYPE_TI_ARGS (t2
)))
1342 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
1344 else if ((strict
& COMPARE_DERIVED
) && DERIVED_FROM_P (t2
, t1
))
1350 if (!comptypes (TYPE_OFFSET_BASETYPE (t1
), TYPE_OFFSET_BASETYPE (t2
),
1351 strict
& ~COMPARE_REDECLARATION
))
1353 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1357 case REFERENCE_TYPE
:
1358 if (TYPE_REF_IS_RVALUE (t1
) != TYPE_REF_IS_RVALUE (t2
))
1360 /* fall through to checks for pointer types */
1364 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1365 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1371 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1373 if (!compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)))
1378 /* Target types must match incl. qualifiers. */
1379 if (!comp_array_types (t1
, t2
, ((strict
& COMPARE_REDECLARATION
)
1380 ? bounds_either
: bounds_none
),
1385 case TEMPLATE_TYPE_PARM
:
1386 /* If T1 and T2 don't have the same relative position in their
1387 template parameters set, they can't be equal. */
1388 if (!comp_template_parms_position (t1
, t2
))
1390 /* If T1 and T2 don't represent the same class template deduction,
1391 they aren't equal. */
1392 if (CLASS_PLACEHOLDER_TEMPLATE (t1
)
1393 != CLASS_PLACEHOLDER_TEMPLATE (t2
))
1395 /* Constrained 'auto's are distinct from parms that don't have the same
1397 if (!equivalent_placeholder_constraints (t1
, t2
))
1402 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1
),
1403 TYPENAME_TYPE_FULLNAME (t2
)))
1405 /* Qualifiers don't matter on scopes. */
1406 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1
),
1411 case UNBOUND_CLASS_TEMPLATE
:
1412 if (!cp_tree_equal (TYPE_IDENTIFIER (t1
), TYPE_IDENTIFIER (t2
)))
1414 if (!same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
)))
1419 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1424 if (maybe_ne (TYPE_VECTOR_SUBPARTS (t1
), TYPE_VECTOR_SUBPARTS (t2
))
1425 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1429 case TYPE_PACK_EXPANSION
:
1430 return (same_type_p (PACK_EXPANSION_PATTERN (t1
),
1431 PACK_EXPANSION_PATTERN (t2
))
1432 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1
),
1433 PACK_EXPANSION_EXTRA_ARGS (t2
)));
1436 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1
)
1437 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2
)
1438 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1
)
1439 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2
))
1440 || (DECLTYPE_FOR_LAMBDA_PROXY (t1
)
1441 != DECLTYPE_FOR_LAMBDA_PROXY (t2
))
1442 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1
),
1443 DECLTYPE_TYPE_EXPR (t2
)))
1447 case UNDERLYING_TYPE
:
1448 return same_type_p (UNDERLYING_TYPE_TYPE (t1
),
1449 UNDERLYING_TYPE_TYPE (t2
));
1455 /* Don't treat an alias template specialization with dependent
1456 arguments as equivalent to its underlying type when used as a
1457 template argument; we need them to be distinct so that we
1458 substitute into the specialization arguments at instantiation
1459 time. And aliases can't be equivalent without being ==, so
1460 we don't need to look any deeper. */
1461 if (comparing_specializations
1462 && (dependent_alias_template_spec_p (t1
)
1463 || dependent_alias_template_spec_p (t2
)))
1466 /* If we get here, we know that from a target independent POV the
1467 types are the same. Make sure the target attributes are also
1469 return comp_type_attributes (t1
, t2
);
1472 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1473 is a bitwise-or of the COMPARE_* flags. */
1476 comptypes (tree t1
, tree t2
, int strict
)
1478 if (strict
== COMPARE_STRICT
&& comparing_specializations
1479 && (t1
!= TYPE_CANONICAL (t1
) || t2
!= TYPE_CANONICAL (t2
)))
1480 /* If comparing_specializations, treat dependent aliases as distinct. */
1481 strict
= COMPARE_STRUCTURAL
;
1482 if (strict
== COMPARE_STRICT
)
1487 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1490 if (TYPE_STRUCTURAL_EQUALITY_P (t1
) || TYPE_STRUCTURAL_EQUALITY_P (t2
))
1491 /* At least one of the types requires structural equality, so
1492 perform a deep check. */
1493 return structural_comptypes (t1
, t2
, strict
);
1495 if (flag_checking
&& USE_CANONICAL_TYPES
)
1497 bool result
= structural_comptypes (t1
, t2
, strict
);
1499 if (result
&& TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1500 /* The two types are structurally equivalent, but their
1501 canonical types were different. This is a failure of the
1502 canonical type propagation code.*/
1504 ("canonical types differ for identical types %qT and %qT",
1506 else if (!result
&& TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
))
1507 /* Two types are structurally different, but the canonical
1508 types are the same. This means we were over-eager in
1509 assigning canonical types. */
1511 ("same canonical type node for different types %qT and %qT",
1516 if (!flag_checking
&& USE_CANONICAL_TYPES
)
1517 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1519 return structural_comptypes (t1
, t2
, strict
);
1521 else if (strict
== COMPARE_STRUCTURAL
)
1522 return structural_comptypes (t1
, t2
, COMPARE_STRICT
);
1524 return structural_comptypes (t1
, t2
, strict
);
1527 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1528 top-level qualifiers. */
1531 same_type_ignoring_top_level_qualifiers_p (tree type1
, tree type2
)
1533 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1538 type1
= cp_build_qualified_type (type1
, TYPE_UNQUALIFIED
);
1539 type2
= cp_build_qualified_type (type2
, TYPE_UNQUALIFIED
);
1540 return same_type_p (type1
, type2
);
1543 /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1546 similar_type_p (tree type1
, tree type2
)
1548 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1551 /* Informally, two types are similar if, ignoring top-level cv-qualification:
1552 * they are the same type; or
1553 * they are both pointers, and the pointed-to types are similar; or
1554 * they are both pointers to member of the same class, and the types of
1555 the pointed-to members are similar; or
1556 * they are both arrays of the same size or both arrays of unknown bound,
1557 and the array element types are similar. */
1559 if (same_type_ignoring_top_level_qualifiers_p (type1
, type2
))
1562 if ((TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
1563 || (TYPE_PTRDATAMEM_P (type1
) && TYPE_PTRDATAMEM_P (type2
))
1564 || (TREE_CODE (type1
) == ARRAY_TYPE
&& TREE_CODE (type2
) == ARRAY_TYPE
))
1565 return comp_ptr_ttypes_const (type1
, type2
, bounds_either
);
1570 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1573 at_least_as_qualified_p (const_tree type1
, const_tree type2
)
1575 int q1
= cp_type_quals (type1
);
1576 int q2
= cp_type_quals (type2
);
1578 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1579 return (q1
& q2
) == q2
;
1582 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1583 more cv-qualified that TYPE1, and 0 otherwise. */
1586 comp_cv_qualification (int q1
, int q2
)
1591 if ((q1
& q2
) == q2
)
1593 else if ((q1
& q2
) == q1
)
1600 comp_cv_qualification (const_tree type1
, const_tree type2
)
1602 int q1
= cp_type_quals (type1
);
1603 int q2
= cp_type_quals (type2
);
1604 return comp_cv_qualification (q1
, q2
);
1607 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1608 subset of the cv-qualification signature of TYPE2, and the types
1609 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1612 comp_cv_qual_signature (tree type1
, tree type2
)
1614 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1616 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1622 /* Subroutines of `comptypes'. */
1624 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1625 equivalent in the sense that functions with those parameter types
1626 can have equivalent types. The two lists must be equivalent,
1627 element by element. */
1630 compparms (const_tree parms1
, const_tree parms2
)
1634 /* An unspecified parmlist matches any specified parmlist
1635 whose argument types don't need default promotions. */
1637 for (t1
= parms1
, t2
= parms2
;
1639 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1641 /* If one parmlist is shorter than the other,
1642 they fail to match. */
1645 if (!same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1652 /* Process a sizeof or alignof expression where the operand is a
1653 type. STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
1654 or GNU (preferred alignment) semantics; it is ignored if op is
1658 cxx_sizeof_or_alignof_type (tree type
, enum tree_code op
, bool std_alignof
,
1661 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
1662 if (type
== error_mark_node
)
1663 return error_mark_node
;
1665 type
= non_reference (type
);
1666 if (TREE_CODE (type
) == METHOD_TYPE
)
1670 pedwarn (input_location
, OPT_Wpointer_arith
,
1671 "invalid application of %qs to a member function",
1672 OVL_OP_INFO (false, op
)->name
);
1673 return size_one_node
;
1676 return error_mark_node
;
1679 bool dependent_p
= dependent_type_p (type
);
1681 complete_type (type
);
1683 /* VLA types will have a non-constant size. In the body of an
1684 uninstantiated template, we don't need to try to compute the
1685 value, because the sizeof expression is not an integral
1686 constant expression in that case. And, if we do try to
1687 compute the value, we'll likely end up with SAVE_EXPRs, which
1688 the template substitution machinery does not expect to see. */
1689 || (processing_template_decl
1690 && COMPLETE_TYPE_P (type
)
1691 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
))
1693 tree value
= build_min (op
, size_type_node
, type
);
1694 TREE_READONLY (value
) = 1;
1695 if (op
== ALIGNOF_EXPR
&& std_alignof
)
1696 ALIGNOF_EXPR_STD_P (value
) = true;
1700 return c_sizeof_or_alignof_type (input_location
, complete_type (type
),
1701 op
== SIZEOF_EXPR
, std_alignof
,
1705 /* Return the size of the type, without producing any warnings for
1706 types whose size cannot be taken. This routine should be used only
1707 in some other routine that has already produced a diagnostic about
1708 using the size of such a type. */
1710 cxx_sizeof_nowarn (tree type
)
1712 if (TREE_CODE (type
) == FUNCTION_TYPE
1713 || VOID_TYPE_P (type
)
1714 || TREE_CODE (type
) == ERROR_MARK
)
1715 return size_one_node
;
1716 else if (!COMPLETE_TYPE_P (type
))
1717 return size_zero_node
;
1719 return cxx_sizeof_or_alignof_type (type
, SIZEOF_EXPR
, false, false);
1722 /* Process a sizeof expression where the operand is an expression. */
1725 cxx_sizeof_expr (tree e
, tsubst_flags_t complain
)
1727 if (e
== error_mark_node
)
1728 return error_mark_node
;
1730 if (instantiation_dependent_uneval_expression_p (e
))
1732 e
= build_min (SIZEOF_EXPR
, size_type_node
, e
);
1733 TREE_SIDE_EFFECTS (e
) = 0;
1734 TREE_READONLY (e
) = 1;
1739 STRIP_ANY_LOCATION_WRAPPER (e
);
1741 /* To get the size of a static data member declared as an array of
1742 unknown bound, we need to instantiate it. */
1744 && VAR_HAD_UNKNOWN_BOUND (e
)
1745 && DECL_TEMPLATE_INSTANTIATION (e
))
1746 instantiate_decl (e
, /*defer_ok*/true, /*expl_inst_mem*/false);
1748 if (TREE_CODE (e
) == PARM_DECL
1749 && DECL_ARRAY_PARAMETER_P (e
)
1750 && (complain
& tf_warning
))
1752 auto_diagnostic_group d
;
1753 if (warning (OPT_Wsizeof_array_argument
, "%<sizeof%> on array function "
1754 "parameter %qE will return size of %qT", e
, TREE_TYPE (e
)))
1755 inform (DECL_SOURCE_LOCATION (e
), "declared here");
1758 e
= mark_type_use (e
);
1762 if (complain
& tf_error
)
1763 error ("invalid application of %<sizeof%> to a bit-field");
1765 return error_mark_node
;
1768 else if (is_overloaded_fn (e
))
1770 if (complain
& tf_error
)
1771 permerror (input_location
, "ISO C++ forbids applying %<sizeof%> to an expression of "
1774 return error_mark_node
;
1777 else if (type_unknown_p (e
))
1779 if (complain
& tf_error
)
1780 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1782 return error_mark_node
;
1788 return cxx_sizeof_or_alignof_type (e
, SIZEOF_EXPR
, false, complain
& tf_error
);
1791 /* Implement the __alignof keyword: Return the minimum required
1792 alignment of E, measured in bytes. For VAR_DECL's and
1793 FIELD_DECL's return DECL_ALIGN (which can be set from an
1794 "aligned" __attribute__ specification). */
1797 cxx_alignof_expr (tree e
, tsubst_flags_t complain
)
1801 if (e
== error_mark_node
)
1802 return error_mark_node
;
1804 if (processing_template_decl
)
1806 e
= build_min (ALIGNOF_EXPR
, size_type_node
, e
);
1807 TREE_SIDE_EFFECTS (e
) = 0;
1808 TREE_READONLY (e
) = 1;
1813 STRIP_ANY_LOCATION_WRAPPER (e
);
1815 e
= mark_type_use (e
);
1818 t
= size_int (DECL_ALIGN_UNIT (e
));
1819 else if (bitfield_p (e
))
1821 if (complain
& tf_error
)
1822 error ("invalid application of %<__alignof%> to a bit-field");
1824 return error_mark_node
;
1827 else if (TREE_CODE (e
) == COMPONENT_REF
1828 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
)
1829 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e
, 1)));
1830 else if (is_overloaded_fn (e
))
1832 if (complain
& tf_error
)
1833 permerror (input_location
, "ISO C++ forbids applying %<__alignof%> to an expression of "
1836 return error_mark_node
;
1837 if (TREE_CODE (e
) == FUNCTION_DECL
)
1838 t
= size_int (DECL_ALIGN_UNIT (e
));
1842 else if (type_unknown_p (e
))
1844 if (complain
& tf_error
)
1845 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1847 return error_mark_node
;
1851 return cxx_sizeof_or_alignof_type (TREE_TYPE (e
), ALIGNOF_EXPR
, false,
1852 complain
& tf_error
);
1854 return fold_convert (size_type_node
, t
);
1857 /* Process a sizeof or alignof expression E with code OP where the operand
1858 is an expression. */
1861 cxx_sizeof_or_alignof_expr (tree e
, enum tree_code op
, bool complain
)
1863 if (op
== SIZEOF_EXPR
)
1864 return cxx_sizeof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1866 return cxx_alignof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1869 /* Build a representation of an expression 'alignas(E).' Return the
1870 folded integer value of E if it is an integral constant expression
1871 that resolves to a valid alignment. If E depends on a template
1872 parameter, return a syntactic representation tree of kind
1873 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1874 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1877 cxx_alignas_expr (tree e
)
1879 if (e
== NULL_TREE
|| e
== error_mark_node
1880 || (!TYPE_P (e
) && !require_potential_rvalue_constant_expression (e
)))
1886 When the alignment-specifier is of the form
1887 alignas(type-id ), it shall have the same effect as
1888 alignas(alignof(type-id )). */
1890 return cxx_sizeof_or_alignof_type (e
, ALIGNOF_EXPR
, true, false);
1892 /* If we reach this point, it means the alignas expression if of
1893 the form "alignas(assignment-expression)", so we should follow
1894 what is stated by [dcl.align]/2. */
1896 if (value_dependent_expression_p (e
))
1897 /* Leave value-dependent expression alone for now. */
1900 e
= instantiate_non_dependent_expr (e
);
1901 e
= mark_rvalue_use (e
);
1903 /* [dcl.align]/2 says:
1905 the assignment-expression shall be an integral constant
1908 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e
)))
1910 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e
));
1911 return error_mark_node
;
1914 return cxx_constant_value (e
);
1918 /* EXPR is being used in a context that is not a function call.
1923 The expression can be used only as the left-hand operand of a
1924 member function call.
1926 [expr.mptr.operator]
1928 If the result of .* or ->* is a function, then that result can be
1929 used only as the operand for the function call operator ().
1931 by issuing an error message if appropriate. Returns true iff EXPR
1932 violates these rules. */
1935 invalid_nonstatic_memfn_p (location_t loc
, tree expr
, tsubst_flags_t complain
)
1937 if (expr
== NULL_TREE
)
1939 /* Don't enforce this in MS mode. */
1940 if (flag_ms_extensions
)
1942 if (is_overloaded_fn (expr
) && !really_overloaded_fn (expr
))
1943 expr
= get_first_fn (expr
);
1944 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr
))
1946 if (complain
& tf_error
)
1950 error_at (loc
, "invalid use of non-static member function %qD",
1952 inform (DECL_SOURCE_LOCATION (expr
), "declared here");
1955 error_at (loc
, "invalid use of non-static member function of "
1956 "type %qT", TREE_TYPE (expr
));
1963 /* If EXP is a reference to a bitfield, and the type of EXP does not
1964 match the declared type of the bitfield, return the declared type
1965 of the bitfield. Otherwise, return NULL_TREE. */
1968 is_bitfield_expr_with_lowered_type (const_tree exp
)
1970 switch (TREE_CODE (exp
))
1973 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1)
1974 ? TREE_OPERAND (exp
, 1)
1975 : TREE_OPERAND (exp
, 0)))
1977 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 2));
1980 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1));
1984 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1990 field
= TREE_OPERAND (exp
, 1);
1991 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_BIT_FIELD_TYPE (field
))
1993 if (same_type_ignoring_top_level_qualifiers_p
1994 (TREE_TYPE (exp
), DECL_BIT_FIELD_TYPE (field
)))
1996 return DECL_BIT_FIELD_TYPE (field
);
2000 if (DECL_HAS_VALUE_EXPR_P (exp
))
2001 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2002 (CONST_CAST_TREE (exp
)));
2005 case VIEW_CONVERT_EXPR
:
2006 if (location_wrapper_p (exp
))
2007 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
2016 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2017 bitfield with a lowered type, the type of EXP is returned, rather
2021 unlowered_expr_type (const_tree exp
)
2024 tree etype
= TREE_TYPE (exp
);
2026 type
= is_bitfield_expr_with_lowered_type (exp
);
2028 type
= cp_build_qualified_type (type
, cp_type_quals (etype
));
2035 /* Perform the conversions in [expr] that apply when an lvalue appears
2036 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2037 function-to-pointer conversions. In addition, bitfield references are
2038 converted to their declared types. Note that this function does not perform
2039 the lvalue-to-rvalue conversion for class types. If you need that conversion
2040 for class types, then you probably need to use force_rvalue.
2042 Although the returned value is being used as an rvalue, this
2043 function does not wrap the returned expression in a
2044 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2045 that the return value is no longer an lvalue. */
2048 decay_conversion (tree exp
,
2049 tsubst_flags_t complain
,
2050 bool reject_builtin
/* = true */)
2053 enum tree_code code
;
2054 location_t loc
= cp_expr_loc_or_input_loc (exp
);
2056 type
= TREE_TYPE (exp
);
2057 if (type
== error_mark_node
)
2058 return error_mark_node
;
2060 exp
= resolve_nondeduced_context_or_error (exp
, complain
);
2062 code
= TREE_CODE (type
);
2064 if (error_operand_p (exp
))
2065 return error_mark_node
;
2067 if (NULLPTR_TYPE_P (type
) && !TREE_SIDE_EFFECTS (exp
))
2069 mark_rvalue_use (exp
, loc
, reject_builtin
);
2070 return nullptr_node
;
2073 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2074 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2075 if (code
== VOID_TYPE
)
2077 if (complain
& tf_error
)
2078 error_at (loc
, "void value not ignored as it ought to be");
2079 return error_mark_node
;
2081 if (invalid_nonstatic_memfn_p (loc
, exp
, complain
))
2082 return error_mark_node
;
2083 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
2085 exp
= mark_lvalue_use (exp
);
2086 if (reject_builtin
&& reject_gcc_builtin (exp
, loc
))
2087 return error_mark_node
;
2088 return cp_build_addr_expr (exp
, complain
);
2090 if (code
== ARRAY_TYPE
)
2095 exp
= mark_lvalue_use (exp
);
2097 if (INDIRECT_REF_P (exp
))
2098 return build_nop (build_pointer_type (TREE_TYPE (type
)),
2099 TREE_OPERAND (exp
, 0));
2101 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
2103 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1), complain
);
2104 if (op1
== error_mark_node
)
2105 return error_mark_node
;
2106 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
2107 TREE_OPERAND (exp
, 0), op1
);
2110 if (!obvalue_p (exp
)
2111 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
2113 if (complain
& tf_error
)
2114 error_at (loc
, "invalid use of non-lvalue array");
2115 return error_mark_node
;
2118 /* Don't let an array compound literal decay to a pointer. It can
2119 still be used to initialize an array or bind to a reference. */
2120 if (TREE_CODE (exp
) == TARGET_EXPR
)
2122 if (complain
& tf_error
)
2123 error_at (loc
, "taking address of temporary array");
2124 return error_mark_node
;
2127 ptrtype
= build_pointer_type (TREE_TYPE (type
));
2131 if (!cxx_mark_addressable (exp
))
2132 return error_mark_node
;
2133 adr
= build_nop (ptrtype
, build_address (exp
));
2136 /* This way is better for a COMPONENT_REF since it can
2137 simplify the offset for a component. */
2138 adr
= cp_build_addr_expr (exp
, complain
);
2139 return cp_convert (ptrtype
, adr
, complain
);
2142 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2143 exp
= mark_rvalue_use (exp
, loc
, reject_builtin
);
2145 /* If a bitfield is used in a context where integral promotion
2146 applies, then the caller is expected to have used
2147 default_conversion. That function promotes bitfields correctly
2148 before calling this function. At this point, if we have a
2149 bitfield referenced, we may assume that is not subject to
2150 promotion, and that, therefore, the type of the resulting rvalue
2151 is the declared type of the bitfield. */
2152 exp
= convert_bitfield_to_declared_type (exp
);
2154 /* We do not call rvalue() here because we do not want to wrap EXP
2155 in a NON_LVALUE_EXPR. */
2159 Non-class rvalues always have cv-unqualified types. */
2160 type
= TREE_TYPE (exp
);
2161 if (!CLASS_TYPE_P (type
) && cv_qualified_p (type
))
2162 exp
= build_nop (cv_unqualified (type
), exp
);
2164 if (!complete_type_or_maybe_complain (type
, exp
, complain
))
2165 return error_mark_node
;
2170 /* Perform preparatory conversions, as part of the "usual arithmetic
2171 conversions". In particular, as per [expr]:
2173 Whenever an lvalue expression appears as an operand of an
2174 operator that expects the rvalue for that operand, the
2175 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2176 standard conversions are applied to convert the expression to an
2179 In addition, we perform integral promotions here, as those are
2180 applied to both operands to a binary operator before determining
2181 what additional conversions should apply. */
2184 cp_default_conversion (tree exp
, tsubst_flags_t complain
)
2186 /* Check for target-specific promotions. */
2187 tree promoted_type
= targetm
.promoted_type (TREE_TYPE (exp
));
2189 exp
= cp_convert (promoted_type
, exp
, complain
);
2190 /* Perform the integral promotions first so that bitfield
2191 expressions (which may promote to "int", even if the bitfield is
2192 declared "unsigned") are promoted correctly. */
2193 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp
)))
2194 exp
= cp_perform_integral_promotions (exp
, complain
);
2195 /* Perform the other conversions. */
2196 exp
= decay_conversion (exp
, complain
);
2204 default_conversion (tree exp
)
2206 return cp_default_conversion (exp
, tf_warning_or_error
);
2209 /* EXPR is an expression with an integral or enumeration type.
2210 Perform the integral promotions in [conv.prom], and return the
2214 cp_perform_integral_promotions (tree expr
, tsubst_flags_t complain
)
2219 expr
= mark_rvalue_use (expr
);
2220 if (error_operand_p (expr
))
2221 return error_mark_node
;
2223 type
= TREE_TYPE (expr
);
2227 A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2228 of type int if int can represent all the values of the bit-field;
2229 otherwise, it can be converted to unsigned int if unsigned int can
2230 represent all the values of the bit-field. If the bit-field is larger yet,
2231 no integral promotion applies to it. If the bit-field has an enumerated
2232 type, it is treated as any other value of that type for promotion
2234 tree bitfield_type
= is_bitfield_expr_with_lowered_type (expr
);
2236 && (TREE_CODE (bitfield_type
) == ENUMERAL_TYPE
2237 || TYPE_PRECISION (type
) > TYPE_PRECISION (integer_type_node
)))
2238 type
= bitfield_type
;
2240 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
2241 /* Scoped enums don't promote. */
2242 if (SCOPED_ENUM_P (type
))
2244 promoted_type
= type_promotes_to (type
);
2245 if (type
!= promoted_type
)
2246 expr
= cp_convert (promoted_type
, expr
, complain
);
2247 else if (bitfield_type
&& bitfield_type
!= type
)
2248 /* Prevent decay_conversion from converting to bitfield_type. */
2249 expr
= build_nop (type
, expr
);
2256 perform_integral_promotions (tree expr
)
2258 return cp_perform_integral_promotions (expr
, tf_warning_or_error
);
2261 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2262 decay_conversion to one. */
2265 string_conv_p (const_tree totype
, const_tree exp
, int warn
)
2269 if (!TYPE_PTR_P (totype
))
2272 t
= TREE_TYPE (totype
);
2273 if (!same_type_p (t
, char_type_node
)
2274 && !same_type_p (t
, char8_type_node
)
2275 && !same_type_p (t
, char16_type_node
)
2276 && !same_type_p (t
, char32_type_node
)
2277 && !same_type_p (t
, wchar_type_node
))
2280 location_t loc
= EXPR_LOC_OR_LOC (exp
, input_location
);
2282 STRIP_ANY_LOCATION_WRAPPER (exp
);
2284 if (TREE_CODE (exp
) == STRING_CST
)
2286 /* Make sure that we don't try to convert between char and wide chars. */
2287 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
2292 /* Is this a string constant which has decayed to 'const char *'? */
2293 t
= build_pointer_type (cp_build_qualified_type (t
, TYPE_QUAL_CONST
));
2294 if (!same_type_p (TREE_TYPE (exp
), t
))
2297 if (TREE_CODE (exp
) != ADDR_EXPR
2298 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
2303 if (cxx_dialect
>= cxx11
)
2304 pedwarn (loc
, OPT_Wwrite_strings
,
2305 "ISO C++ forbids converting a string constant to %qT",
2308 warning_at (loc
, OPT_Wwrite_strings
,
2309 "deprecated conversion from string constant to %qT",
2316 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2317 can, for example, use as an lvalue. This code used to be in
2318 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2319 expressions, where we're dealing with aggregates. But now it's again only
2320 called from unary_complex_lvalue. The case (in particular) that led to
2321 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2325 rationalize_conditional_expr (enum tree_code code
, tree t
,
2326 tsubst_flags_t complain
)
2328 location_t loc
= cp_expr_loc_or_input_loc (t
);
2330 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2331 the first operand is always the one to be used if both operands
2332 are equal, so we know what conditional expression this used to be. */
2333 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
2335 tree op0
= TREE_OPERAND (t
, 0);
2336 tree op1
= TREE_OPERAND (t
, 1);
2338 /* The following code is incorrect if either operand side-effects. */
2339 gcc_assert (!TREE_SIDE_EFFECTS (op0
)
2340 && !TREE_SIDE_EFFECTS (op1
));
2342 build_conditional_expr (loc
,
2343 build_x_binary_op (loc
,
2344 (TREE_CODE (t
) == MIN_EXPR
2345 ? LE_EXPR
: GE_EXPR
),
2346 op0
, TREE_CODE (op0
),
2347 op1
, TREE_CODE (op1
),
2350 cp_build_unary_op (code
, op0
, false, complain
),
2351 cp_build_unary_op (code
, op1
, false, complain
),
2355 tree op1
= TREE_OPERAND (t
, 1);
2356 if (TREE_CODE (op1
) != THROW_EXPR
)
2357 op1
= cp_build_unary_op (code
, op1
, false, complain
);
2358 tree op2
= TREE_OPERAND (t
, 2);
2359 if (TREE_CODE (op2
) != THROW_EXPR
)
2360 op2
= cp_build_unary_op (code
, op2
, false, complain
);
2363 build_conditional_expr (loc
, TREE_OPERAND (t
, 0), op1
, op2
, complain
);
2366 /* Given the TYPE of an anonymous union field inside T, return the
2367 FIELD_DECL for the field. If not found return NULL_TREE. Because
2368 anonymous unions can nest, we must also search all anonymous unions
2369 that are directly reachable. */
2372 lookup_anon_field (tree t
, tree type
)
2376 t
= TYPE_MAIN_VARIANT (t
);
2378 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
2380 if (TREE_STATIC (field
))
2382 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
2385 /* If we find it directly, return the field. */
2386 if (DECL_NAME (field
) == NULL_TREE
2387 && type
== TYPE_MAIN_VARIANT (TREE_TYPE (field
)))
2392 /* Otherwise, it could be nested, search harder. */
2393 if (DECL_NAME (field
) == NULL_TREE
2394 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2396 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
2404 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2405 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2406 non-NULL, it indicates the path to the base used to name MEMBER.
2407 If PRESERVE_REFERENCE is true, the expression returned will have
2408 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2409 returned will have the type referred to by the reference.
2411 This function does not perform access control; that is either done
2412 earlier by the parser when the name of MEMBER is resolved to MEMBER
2413 itself, or later when overload resolution selects one of the
2414 functions indicated by MEMBER. */
2417 build_class_member_access_expr (cp_expr object
, tree member
,
2418 tree access_path
, bool preserve_reference
,
2419 tsubst_flags_t complain
)
2423 tree result
= NULL_TREE
;
2424 tree using_decl
= NULL_TREE
;
2426 if (error_operand_p (object
) || error_operand_p (member
))
2427 return error_mark_node
;
2429 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2433 The type of the first expression shall be "class object" (of a
2435 object_type
= TREE_TYPE (object
);
2436 if (!currently_open_class (object_type
)
2437 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2438 return error_mark_node
;
2439 if (!CLASS_TYPE_P (object_type
))
2441 if (complain
& tf_error
)
2443 if (INDIRECT_TYPE_P (object_type
)
2444 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2445 error ("request for member %qD in %qE, which is of pointer "
2446 "type %qT (maybe you meant to use %<->%> ?)",
2447 member
, object
.get_value (), object_type
);
2449 error ("request for member %qD in %qE, which is of non-class "
2450 "type %qT", member
, object
.get_value (), object_type
);
2452 return error_mark_node
;
2455 /* The standard does not seem to actually say that MEMBER must be a
2456 member of OBJECT_TYPE. However, that is clearly what is
2458 if (DECL_P (member
))
2460 member_scope
= DECL_CLASS_CONTEXT (member
);
2461 if (!mark_used (member
, complain
) && !(complain
& tf_error
))
2462 return error_mark_node
;
2463 if (TREE_DEPRECATED (member
))
2464 warn_deprecated_use (member
, NULL_TREE
);
2467 member_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (member
));
2468 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2469 presently be the anonymous union. Go outwards until we find a
2470 type related to OBJECT_TYPE. */
2471 while ((ANON_AGGR_TYPE_P (member_scope
) || UNSCOPED_ENUM_P (member_scope
))
2472 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
2474 member_scope
= TYPE_CONTEXT (member_scope
);
2475 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
2477 if (complain
& tf_error
)
2479 if (TREE_CODE (member
) == FIELD_DECL
)
2480 error ("invalid use of nonstatic data member %qE", member
);
2482 error ("%qD is not a member of %qT", member
, object_type
);
2484 return error_mark_node
;
2487 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2488 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2489 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2491 tree temp
= unary_complex_lvalue (ADDR_EXPR
, object
);
2494 temp
= cp_build_fold_indirect_ref (temp
);
2495 if (xvalue_p (object
) && !xvalue_p (temp
))
2496 /* Preserve xvalue kind. */
2502 /* In [expr.ref], there is an explicit list of the valid choices for
2503 MEMBER. We check for each of those cases here. */
2506 /* A static data member. */
2508 mark_exp_read (object
);
2510 if (tree wrap
= maybe_get_tls_wrapper_call (result
))
2511 /* Replace an evaluated use of the thread_local variable with
2512 a call to its wrapper. */
2515 /* If OBJECT has side-effects, they are supposed to occur. */
2516 if (TREE_SIDE_EFFECTS (object
))
2517 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
2519 else if (TREE_CODE (member
) == FIELD_DECL
)
2521 /* A non-static data member. */
2526 if (INDIRECT_REF_P (object
))
2528 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object
, 0)));
2530 null_object_p
= false;
2532 /* Convert OBJECT to the type of MEMBER. */
2533 if (!same_type_p (TYPE_MAIN_VARIANT (object_type
),
2534 TYPE_MAIN_VARIANT (member_scope
)))
2539 /* We didn't complain above about a currently open class, but now we
2540 must: we don't know how to refer to a base member before layout is
2541 complete. But still don't complain in a template. */
2542 if (!cp_unevaluated_operand
2543 && !dependent_type_p (object_type
)
2544 && !complete_type_or_maybe_complain (object_type
, object
,
2546 return error_mark_node
;
2548 binfo
= lookup_base (access_path
? access_path
: object_type
,
2549 member_scope
, ba_unique
, &kind
, complain
);
2550 if (binfo
== error_mark_node
)
2551 return error_mark_node
;
2553 /* It is invalid to try to get to a virtual base of a
2554 NULL object. The most common cause is invalid use of
2556 if (null_object_p
&& kind
== bk_via_virtual
)
2558 if (complain
& tf_error
)
2560 error ("invalid access to non-static data member %qD in "
2561 "virtual base of NULL object", member
);
2563 return error_mark_node
;
2566 /* Convert to the base. */
2567 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
2568 /*nonnull=*/1, complain
);
2569 /* If we found the base successfully then we should be able
2570 to convert to it successfully. */
2571 gcc_assert (object
!= error_mark_node
);
2574 /* If MEMBER is from an anonymous aggregate, we have converted
2575 OBJECT so that it refers to the class containing the
2576 anonymous union. Generate a reference to the anonymous union
2577 itself, and recur to find MEMBER. */
2578 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
))
2579 /* When this code is called from build_field_call, the
2580 object already has the type of the anonymous union.
2581 That is because the COMPONENT_REF was already
2582 constructed, and was then disassembled before calling
2583 build_field_call. After the function-call code is
2584 cleaned up, this waste can be eliminated. */
2585 && (!same_type_ignoring_top_level_qualifiers_p
2586 (TREE_TYPE (object
), DECL_CONTEXT (member
))))
2588 tree anonymous_union
;
2590 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
2591 DECL_CONTEXT (member
));
2592 object
= build_class_member_access_expr (object
,
2594 /*access_path=*/NULL_TREE
,
2599 /* Compute the type of the field, as described in [expr.ref]. */
2600 type_quals
= TYPE_UNQUALIFIED
;
2601 member_type
= TREE_TYPE (member
);
2602 if (!TYPE_REF_P (member_type
))
2604 type_quals
= (cp_type_quals (member_type
)
2605 | cp_type_quals (object_type
));
2607 /* A field is const (volatile) if the enclosing object, or the
2608 field itself, is const (volatile). But, a mutable field is
2609 not const, even within a const object. */
2610 if (DECL_MUTABLE_P (member
))
2611 type_quals
&= ~TYPE_QUAL_CONST
;
2612 member_type
= cp_build_qualified_type (member_type
, type_quals
);
2615 result
= build3_loc (input_location
, COMPONENT_REF
, member_type
,
2616 object
, member
, NULL_TREE
);
2618 /* Mark the expression const or volatile, as appropriate. Even
2619 though we've dealt with the type above, we still have to mark the
2620 expression itself. */
2621 if (type_quals
& TYPE_QUAL_CONST
)
2622 TREE_READONLY (result
) = 1;
2623 if (type_quals
& TYPE_QUAL_VOLATILE
)
2624 TREE_THIS_VOLATILE (result
) = 1;
2626 else if (BASELINK_P (member
))
2628 /* The member is a (possibly overloaded) member function. */
2632 /* If the MEMBER is exactly one static member function, then we
2633 know the type of the expression. Otherwise, we must wait
2634 until overload resolution has been performed. */
2635 functions
= BASELINK_FUNCTIONS (member
);
2636 if (TREE_CODE (functions
) == FUNCTION_DECL
2637 && DECL_STATIC_FUNCTION_P (functions
))
2638 type
= TREE_TYPE (functions
);
2640 type
= unknown_type_node
;
2641 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2642 base. That will happen when the function is called. */
2643 result
= build3_loc (input_location
, COMPONENT_REF
, type
, object
, member
,
2646 else if (TREE_CODE (member
) == CONST_DECL
)
2648 /* The member is an enumerator. */
2650 /* If OBJECT has side-effects, they are supposed to occur. */
2651 if (TREE_SIDE_EFFECTS (object
))
2652 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
2655 else if ((using_decl
= strip_using_decl (member
)) != member
)
2656 result
= build_class_member_access_expr (object
,
2658 access_path
, preserve_reference
,
2662 if (complain
& tf_error
)
2663 error ("invalid use of %qD", member
);
2664 return error_mark_node
;
2667 if (!preserve_reference
)
2670 If E2 is declared to have type "reference to T", then ... the
2671 type of E1.E2 is T. */
2672 result
= convert_from_reference (result
);
2677 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2678 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2681 lookup_destructor (tree object
, tree scope
, tree dtor_name
,
2682 tsubst_flags_t complain
)
2684 tree object_type
= TREE_TYPE (object
);
2685 tree dtor_type
= TREE_OPERAND (dtor_name
, 0);
2688 /* We've already complained about this destructor. */
2689 if (dtor_type
== error_mark_node
)
2690 return error_mark_node
;
2692 if (scope
&& !check_dtor_name (scope
, dtor_type
))
2694 if (complain
& tf_error
)
2695 error ("qualified type %qT does not match destructor name ~%qT",
2697 return error_mark_node
;
2699 if (is_auto (dtor_type
))
2700 dtor_type
= object_type
;
2701 else if (identifier_p (dtor_type
))
2703 /* In a template, names we can't find a match for are still accepted
2704 destructor names, and we check them here. */
2705 if (check_dtor_name (object_type
, dtor_type
))
2706 dtor_type
= object_type
;
2709 if (complain
& tf_error
)
2710 error ("object type %qT does not match destructor name ~%qT",
2711 object_type
, dtor_type
);
2712 return error_mark_node
;
2716 else if (!DERIVED_FROM_P (dtor_type
, TYPE_MAIN_VARIANT (object_type
)))
2718 if (complain
& tf_error
)
2719 error ("the type being destroyed is %qT, but the destructor "
2720 "refers to %qT", TYPE_MAIN_VARIANT (object_type
), dtor_type
);
2721 return error_mark_node
;
2723 expr
= lookup_member (dtor_type
, complete_dtor_identifier
,
2724 /*protect=*/1, /*want_type=*/false,
2725 tf_warning_or_error
);
2728 if (complain
& tf_error
)
2729 cxx_incomplete_type_error (dtor_name
, dtor_type
);
2730 return error_mark_node
;
2732 expr
= (adjust_result_of_qualified_name_lookup
2733 (expr
, dtor_type
, object_type
));
2734 if (scope
== NULL_TREE
)
2735 /* We need to call adjust_result_of_qualified_name_lookup in case the
2736 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2737 that we still get virtual function binding. */
2738 BASELINK_QUALIFIED_P (expr
) = false;
2742 /* An expression of the form "A::template B" has been resolved to
2743 DECL. Issue a diagnostic if B is not a template or template
2747 check_template_keyword (tree decl
)
2749 /* The standard says:
2753 If a name prefixed by the keyword template is not a member
2754 template, the program is ill-formed.
2756 DR 228 removed the restriction that the template be a member
2759 DR 96, if accepted would add the further restriction that explicit
2760 template arguments must be provided if the template keyword is
2761 used, but, as of 2005-10-16, that DR is still in "drafting". If
2762 this DR is accepted, then the semantic checks here can be
2763 simplified, as the entity named must in fact be a template
2764 specialization, rather than, as at present, a set of overloaded
2765 functions containing at least one template function. */
2766 if (TREE_CODE (decl
) != TEMPLATE_DECL
2767 && TREE_CODE (decl
) != TEMPLATE_ID_EXPR
)
2771 if (DECL_USE_TEMPLATE (decl
)
2772 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2775 permerror (input_location
, "%qD is not a template", decl
);
2777 else if (!is_overloaded_fn (decl
))
2778 permerror (input_location
, "%qD is not a template", decl
);
2783 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
2784 !found
&& iter
; ++iter
)
2787 if (TREE_CODE (fn
) == TEMPLATE_DECL
2788 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
2789 || (TREE_CODE (fn
) == FUNCTION_DECL
2790 && DECL_USE_TEMPLATE (fn
)
2791 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn
))))
2795 permerror (input_location
, "%qD is not a template", decl
);
2800 /* Record that an access failure occurred on BASETYPE_PATH attempting
2801 to access DECL, where DIAG_DECL should be used for diagnostics. */
2804 access_failure_info::record_access_failure (tree basetype_path
,
2805 tree decl
, tree diag_decl
)
2807 m_was_inaccessible
= true;
2808 m_basetype_path
= basetype_path
;
2810 m_diag_decl
= diag_decl
;
2813 /* If an access failure was recorded, then attempt to locate an
2814 accessor function for the pertinent field.
2815 Otherwise, return NULL_TREE. */
2818 access_failure_info::get_any_accessor (bool const_p
) const
2820 if (!was_inaccessible_p ())
2824 = locate_field_accessor (m_basetype_path
, m_diag_decl
, const_p
);
2828 /* The accessor must itself be accessible for it to be a reasonable
2830 if (!accessible_p (m_basetype_path
, accessor
, true))
2836 /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
2837 replacing the primary location in RICHLOC with "accessor()". */
2840 access_failure_info::add_fixit_hint (rich_location
*richloc
,
2844 pp_printf (&pp
, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor_decl
)));
2845 richloc
->add_fixit_replace (pp_formatted_text (&pp
));
2848 /* If an access failure was recorded, then attempt to locate an
2849 accessor function for the pertinent field, and if one is
2850 available, add a note and fix-it hint suggesting using it. */
2853 access_failure_info::maybe_suggest_accessor (bool const_p
) const
2855 tree accessor
= get_any_accessor (const_p
);
2856 if (accessor
== NULL_TREE
)
2858 rich_location
richloc (line_table
, input_location
);
2859 add_fixit_hint (&richloc
, accessor
);
2860 inform (&richloc
, "field %q#D can be accessed via %q#D",
2861 m_diag_decl
, accessor
);
2864 /* Subroutine of finish_class_member_access_expr.
2865 Issue an error about NAME not being a member of ACCESS_PATH (or
2866 OBJECT_TYPE), potentially providing a fix-it hint for misspelled
2870 complain_about_unrecognized_member (tree access_path
, tree name
,
2873 /* Attempt to provide a hint about misspelled names. */
2874 tree guessed_id
= lookup_member_fuzzy (access_path
, name
,
2875 /*want_type=*/false);
2876 if (guessed_id
== NULL_TREE
)
2879 error ("%q#T has no member named %qE",
2880 TREE_CODE (access_path
) == TREE_BINFO
2881 ? TREE_TYPE (access_path
) : object_type
, name
);
2885 location_t bogus_component_loc
= input_location
;
2886 gcc_rich_location
rich_loc (bogus_component_loc
);
2888 /* Check that the guessed name is accessible along access_path. */
2889 access_failure_info afi
;
2890 lookup_member (access_path
, guessed_id
, /*protect=*/1,
2891 /*want_type=*/false, /*complain=*/false,
2893 if (afi
.was_inaccessible_p ())
2895 tree accessor
= afi
.get_any_accessor (TYPE_READONLY (object_type
));
2898 /* The guessed name isn't directly accessible, but can be accessed
2899 via an accessor member function. */
2900 afi
.add_fixit_hint (&rich_loc
, accessor
);
2901 error_at (&rich_loc
,
2902 "%q#T has no member named %qE;"
2903 " did you mean %q#D? (accessible via %q#D)",
2904 TREE_CODE (access_path
) == TREE_BINFO
2905 ? TREE_TYPE (access_path
) : object_type
,
2906 name
, afi
.get_diag_decl (), accessor
);
2910 /* The guessed name isn't directly accessible, and no accessor
2911 member function could be found. */
2912 error_at (&rich_loc
,
2913 "%q#T has no member named %qE;"
2914 " did you mean %q#D? (not accessible from this context)",
2915 TREE_CODE (access_path
) == TREE_BINFO
2916 ? TREE_TYPE (access_path
) : object_type
,
2917 name
, afi
.get_diag_decl ());
2918 complain_about_access (afi
.get_decl (), afi
.get_diag_decl (), false);
2923 /* The guessed name is directly accessible; suggest it. */
2924 rich_loc
.add_fixit_misspelled_id (bogus_component_loc
,
2926 error_at (&rich_loc
,
2927 "%q#T has no member named %qE;"
2928 " did you mean %qE?",
2929 TREE_CODE (access_path
) == TREE_BINFO
2930 ? TREE_TYPE (access_path
) : object_type
,
2935 /* This function is called by the parser to process a class member
2936 access expression of the form OBJECT.NAME. NAME is a node used by
2937 the parser to represent a name; it is not yet a DECL. It may,
2938 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2939 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2940 there is no reason to do the lookup twice, so the parser keeps the
2941 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2942 be a template via the use of the "A::template B" syntax. */
2945 finish_class_member_access_expr (cp_expr object
, tree name
, bool template_p
,
2946 tsubst_flags_t complain
)
2951 tree access_path
= NULL_TREE
;
2952 tree orig_object
= object
;
2953 tree orig_name
= name
;
2955 if (object
== error_mark_node
|| name
== error_mark_node
)
2956 return error_mark_node
;
2958 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2959 if (!objc_is_public (object
, name
))
2960 return error_mark_node
;
2962 object_type
= TREE_TYPE (object
);
2964 if (processing_template_decl
)
2966 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2967 type_dependent_object_expression_p (object
)
2968 /* If NAME is "f<args>", where either 'f' or 'args' is
2969 dependent, then the expression is dependent. */
2970 || (TREE_CODE (name
) == TEMPLATE_ID_EXPR
2971 && dependent_template_id_p (TREE_OPERAND (name
, 0),
2972 TREE_OPERAND (name
, 1)))
2973 /* If NAME is "T::X" where "T" is dependent, then the
2974 expression is dependent. */
2975 || (TREE_CODE (name
) == SCOPE_REF
2976 && TYPE_P (TREE_OPERAND (name
, 0))
2977 && dependent_scope_p (TREE_OPERAND (name
, 0)))
2978 /* If NAME is operator T where "T" is dependent, we can't
2979 lookup until we instantiate the T. */
2980 || (TREE_CODE (name
) == IDENTIFIER_NODE
2981 && IDENTIFIER_CONV_OP_P (name
)
2982 && dependent_type_p (TREE_TYPE (name
))))
2985 return build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
,
2986 orig_object
, orig_name
, NULL_TREE
);
2988 object
= build_non_dependent_expr (object
);
2990 else if (c_dialect_objc ()
2991 && identifier_p (name
)
2992 && (expr
= objc_maybe_build_component_ref (object
, name
)))
2997 The type of the first expression shall be "class object" (of a
2999 if (!currently_open_class (object_type
)
3000 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
3001 return error_mark_node
;
3002 if (!CLASS_TYPE_P (object_type
))
3004 if (complain
& tf_error
)
3006 if (INDIRECT_TYPE_P (object_type
)
3007 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
3008 error ("request for member %qD in %qE, which is of pointer "
3009 "type %qT (maybe you meant to use %<->%> ?)",
3010 name
, object
.get_value (), object_type
);
3012 error ("request for member %qD in %qE, which is of non-class "
3013 "type %qT", name
, object
.get_value (), object_type
);
3015 return error_mark_node
;
3018 if (BASELINK_P (name
))
3019 /* A member function that has already been looked up. */
3023 bool is_template_id
= false;
3024 tree template_args
= NULL_TREE
;
3025 tree scope
= NULL_TREE
;
3027 access_path
= object_type
;
3029 if (TREE_CODE (name
) == SCOPE_REF
)
3031 /* A qualified name. The qualifying class or namespace `S'
3032 has already been looked up; it is either a TYPE or a
3034 scope
= TREE_OPERAND (name
, 0);
3035 name
= TREE_OPERAND (name
, 1);
3037 /* If SCOPE is a namespace, then the qualified name does not
3038 name a member of OBJECT_TYPE. */
3039 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
3041 if (complain
& tf_error
)
3042 error ("%<%D::%D%> is not a member of %qT",
3043 scope
, name
, object_type
);
3044 return error_mark_node
;
3048 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
3050 is_template_id
= true;
3051 template_args
= TREE_OPERAND (name
, 1);
3052 name
= TREE_OPERAND (name
, 0);
3054 if (!identifier_p (name
))
3055 name
= OVL_NAME (name
);
3060 if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
3062 gcc_assert (!is_template_id
);
3063 /* Looking up a member enumerator (c++/56793). */
3064 if (!TYPE_CLASS_SCOPE_P (scope
)
3065 || !DERIVED_FROM_P (TYPE_CONTEXT (scope
), object_type
))
3067 if (complain
& tf_error
)
3068 error ("%<%D::%D%> is not a member of %qT",
3069 scope
, name
, object_type
);
3070 return error_mark_node
;
3072 tree val
= lookup_enumerator (scope
, name
);
3075 if (complain
& tf_error
)
3076 error ("%qD is not a member of %qD",
3078 return error_mark_node
;
3081 if (TREE_SIDE_EFFECTS (object
))
3082 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
), object
, val
);
3086 gcc_assert (CLASS_TYPE_P (scope
));
3087 gcc_assert (identifier_p (name
) || TREE_CODE (name
) == BIT_NOT_EXPR
);
3089 if (constructor_name_p (name
, scope
))
3091 if (complain
& tf_error
)
3092 error ("cannot call constructor %<%T::%D%> directly",
3094 return error_mark_node
;
3097 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3098 access_path
= lookup_base (object_type
, scope
, ba_check
,
3100 if (access_path
== error_mark_node
)
3101 return error_mark_node
;
3104 if (any_dependent_bases_p (object_type
))
3106 if (complain
& tf_error
)
3107 error ("%qT is not a base of %qT", scope
, object_type
);
3108 return error_mark_node
;
3112 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
3114 if (dependent_type_p (object_type
))
3115 /* The destructor isn't declared yet. */
3117 member
= lookup_destructor (object
, scope
, name
, complain
);
3121 /* Look up the member. */
3122 access_failure_info afi
;
3123 member
= lookup_member (access_path
, name
, /*protect=*/1,
3124 /*want_type=*/false, complain
,
3126 afi
.maybe_suggest_accessor (TYPE_READONLY (object_type
));
3127 if (member
== NULL_TREE
)
3129 if (dependent_type_p (object_type
))
3130 /* Try again at instantiation time. */
3132 if (complain
& tf_error
)
3133 complain_about_unrecognized_member (access_path
, name
,
3135 return error_mark_node
;
3137 if (member
== error_mark_node
)
3138 return error_mark_node
;
3140 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member
)))
3141 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3142 wrong, so don't use it. */
3144 if (TREE_CODE (member
) == USING_DECL
&& DECL_DEPENDENT_P (member
))
3150 tree templ
= member
;
3152 if (BASELINK_P (templ
))
3153 member
= lookup_template_function (templ
, template_args
);
3154 else if (variable_template_p (templ
))
3155 member
= (lookup_and_finish_template_variable
3156 (templ
, template_args
, complain
));
3159 if (complain
& tf_error
)
3160 error ("%qD is not a member template function", name
);
3161 return error_mark_node
;
3166 if (TREE_DEPRECATED (member
))
3167 warn_deprecated_use (member
, NULL_TREE
);
3170 check_template_keyword (member
);
3172 expr
= build_class_member_access_expr (object
, member
, access_path
,
3173 /*preserve_reference=*/false,
3175 if (processing_template_decl
&& expr
!= error_mark_node
)
3177 if (BASELINK_P (member
))
3179 if (TREE_CODE (orig_name
) == SCOPE_REF
)
3180 BASELINK_QUALIFIED_P (member
) = 1;
3183 return build_min_non_dep (COMPONENT_REF
, expr
,
3184 orig_object
, orig_name
,
3191 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3195 build_simple_component_ref (tree object
, tree member
)
3197 tree type
= cp_build_qualified_type (TREE_TYPE (member
),
3198 cp_type_quals (TREE_TYPE (object
)));
3199 return build3_loc (input_location
,
3200 COMPONENT_REF
, type
,
3201 object
, member
, NULL_TREE
);
3204 /* Return an expression for the MEMBER_NAME field in the internal
3205 representation of PTRMEM, a pointer-to-member function. (Each
3206 pointer-to-member function type gets its own RECORD_TYPE so it is
3207 more convenient to access the fields by name than by FIELD_DECL.)
3208 This routine converts the NAME to a FIELD_DECL and then creates the
3209 node for the complete expression. */
3212 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
3217 if (TREE_CODE (ptrmem
) == CONSTRUCTOR
)
3221 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ptrmem
),
3223 if (index
&& DECL_P (index
) && DECL_NAME (index
) == member_name
)
3228 /* This code is a stripped down version of
3229 build_class_member_access_expr. It does not work to use that
3230 routine directly because it expects the object to be of class
3232 ptrmem_type
= TREE_TYPE (ptrmem
);
3233 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
));
3234 for (member
= TYPE_FIELDS (ptrmem_type
); member
;
3235 member
= DECL_CHAIN (member
))
3236 if (DECL_NAME (member
) == member_name
)
3238 tree res
= build_simple_component_ref (ptrmem
, member
);
3240 TREE_NO_WARNING (res
) = 1;
3244 /* Given an expression PTR for a pointer, return an expression
3245 for the value pointed to.
3246 ERRORSTRING is the name of the operator to appear in error messages.
3248 This function may need to overload OPERATOR_FNNAME.
3249 Must also handle REFERENCE_TYPEs for C++. */
3252 build_x_indirect_ref (location_t loc
, tree expr
, ref_operator errorstring
,
3253 tsubst_flags_t complain
)
3255 tree orig_expr
= expr
;
3257 tree overload
= NULL_TREE
;
3259 if (processing_template_decl
)
3261 /* Retain the type if we know the operand is a pointer. */
3262 if (TREE_TYPE (expr
) && INDIRECT_TYPE_P (TREE_TYPE (expr
)))
3263 return build_min (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
3264 if (type_dependent_expression_p (expr
))
3265 return build_min_nt_loc (loc
, INDIRECT_REF
, expr
);
3266 expr
= build_non_dependent_expr (expr
);
3269 rval
= build_new_op (loc
, INDIRECT_REF
, LOOKUP_NORMAL
, expr
,
3270 NULL_TREE
, NULL_TREE
, &overload
, complain
);
3272 rval
= cp_build_indirect_ref (expr
, errorstring
, complain
);
3274 if (processing_template_decl
&& rval
!= error_mark_node
)
3276 if (overload
!= NULL_TREE
)
3277 return (build_min_non_dep_op_overload
3278 (INDIRECT_REF
, rval
, overload
, orig_expr
));
3280 return build_min_non_dep (INDIRECT_REF
, rval
, orig_expr
);
3286 /* The implementation of the above, and of indirection implied by other
3287 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3290 cp_build_indirect_ref_1 (tree ptr
, ref_operator errorstring
,
3291 tsubst_flags_t complain
, bool do_fold
)
3295 /* RO_NULL should only be used with the folding entry points below, not
3296 cp_build_indirect_ref. */
3297 gcc_checking_assert (errorstring
!= RO_NULL
|| do_fold
);
3299 if (ptr
== current_class_ptr
3300 || (TREE_CODE (ptr
) == NOP_EXPR
3301 && TREE_OPERAND (ptr
, 0) == current_class_ptr
3302 && (same_type_ignoring_top_level_qualifiers_p
3303 (TREE_TYPE (ptr
), TREE_TYPE (current_class_ptr
)))))
3304 return current_class_ref
;
3306 pointer
= (TYPE_REF_P (TREE_TYPE (ptr
))
3307 ? ptr
: decay_conversion (ptr
, complain
));
3308 if (pointer
== error_mark_node
)
3309 return error_mark_node
;
3311 type
= TREE_TYPE (pointer
);
3313 if (INDIRECT_TYPE_P (type
))
3317 If the type of the expression is "pointer to T," the type
3318 of the result is "T." */
3319 tree t
= TREE_TYPE (type
);
3321 if ((CONVERT_EXPR_P (ptr
)
3322 || TREE_CODE (ptr
) == VIEW_CONVERT_EXPR
)
3323 && (!CLASS_TYPE_P (t
) || !CLASSTYPE_EMPTY_P (t
)))
3325 /* If a warning is issued, mark it to avoid duplicates from
3326 the backend. This only needs to be done at
3327 warn_strict_aliasing > 2. */
3328 if (warn_strict_aliasing
> 2)
3329 if (strict_aliasing_warning (EXPR_LOCATION (ptr
),
3330 type
, TREE_OPERAND (ptr
, 0)))
3331 TREE_NO_WARNING (ptr
) = 1;
3334 if (VOID_TYPE_P (t
))
3336 /* A pointer to incomplete type (other than cv void) can be
3337 dereferenced [expr.unary.op]/1 */
3338 if (complain
& tf_error
)
3339 error ("%qT is not a pointer-to-object type", type
);
3340 return error_mark_node
;
3342 else if (do_fold
&& TREE_CODE (pointer
) == ADDR_EXPR
3343 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
3344 /* The POINTER was something like `&x'. We simplify `*&x' to
3346 return TREE_OPERAND (pointer
, 0);
3349 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
3351 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3352 so that we get the proper error message if the result is used
3353 to assign to. Also, &* is supposed to be a no-op. */
3354 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
3355 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
3356 TREE_SIDE_EFFECTS (ref
)
3357 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
));
3361 else if (!(complain
& tf_error
))
3362 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3364 /* `pointer' won't be an error_mark_node if we were given a
3365 pointer to member, so it's cool to check for this here. */
3366 else if (TYPE_PTRMEM_P (type
))
3367 switch (errorstring
)
3369 case RO_ARRAY_INDEXING
:
3370 error ("invalid use of array indexing on pointer to member");
3373 error ("invalid use of unary %<*%> on pointer to member");
3375 case RO_IMPLICIT_CONVERSION
:
3376 error ("invalid use of implicit conversion on pointer to member");
3379 error ("left hand operand of %<->*%> must be a pointer to class, "
3380 "but is a pointer to member of type %qT", type
);
3385 else if (pointer
!= error_mark_node
)
3386 invalid_indirection_error (input_location
, type
, errorstring
);
3388 return error_mark_node
;
3391 /* Entry point used by c-common, which expects folding. */
3394 build_indirect_ref (location_t
/*loc*/,
3395 tree ptr
, ref_operator errorstring
)
3397 return cp_build_indirect_ref_1 (ptr
, errorstring
, tf_warning_or_error
, true);
3400 /* Entry point used by internal indirection needs that don't correspond to any
3401 syntactic construct. */
3404 cp_build_fold_indirect_ref (tree pointer
)
3406 return cp_build_indirect_ref_1 (pointer
, RO_NULL
, tf_warning_or_error
, true);
3409 /* Entry point used by indirection needs that correspond to some syntactic
3413 cp_build_indirect_ref (tree ptr
, ref_operator errorstring
,
3414 tsubst_flags_t complain
)
3416 return cp_build_indirect_ref_1 (ptr
, errorstring
, complain
, false);
3419 /* This handles expressions of the form "a[i]", which denotes
3422 This is logically equivalent in C to *(a+i), but we may do it differently.
3423 If A is a variable or a member, we generate a primitive ARRAY_REF.
3424 This avoids forcing the array out of registers, and can work on
3425 arrays that are not lvalues (for example, members of structures returned
3428 If INDEX is of some user-defined type, it must be converted to
3429 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3430 will inherit the type of the array, which will be some pointer type.
3432 LOC is the location to use in building the array reference. */
3435 cp_build_array_ref (location_t loc
, tree array
, tree idx
,
3436 tsubst_flags_t complain
)
3442 if (complain
& tf_error
)
3443 error_at (loc
, "subscript missing in array reference");
3444 return error_mark_node
;
3447 if (TREE_TYPE (array
) == error_mark_node
3448 || TREE_TYPE (idx
) == error_mark_node
)
3449 return error_mark_node
;
3451 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3453 switch (TREE_CODE (array
))
3457 tree value
= cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3459 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3460 TREE_OPERAND (array
, 0), value
);
3461 SET_EXPR_LOCATION (ret
, loc
);
3466 ret
= build_conditional_expr
3467 (loc
, TREE_OPERAND (array
, 0),
3468 cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3470 cp_build_array_ref (loc
, TREE_OPERAND (array
, 2), idx
,
3473 protected_set_expr_location (ret
, loc
);
3480 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, idx
);
3482 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
3486 warn_array_subscript_with_type_char (loc
, idx
);
3488 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx
)))
3490 if (complain
& tf_error
)
3491 error_at (loc
, "array subscript is not an integer");
3492 return error_mark_node
;
3495 /* Apply integral promotions *after* noticing character types.
3496 (It is unclear why we do these promotions -- the standard
3497 does not say that we should. In fact, the natural thing would
3498 seem to be to convert IDX to ptrdiff_t; we're performing
3499 pointer arithmetic.) */
3500 idx
= cp_perform_integral_promotions (idx
, complain
);
3502 idx
= maybe_constant_value (idx
);
3504 /* An array that is indexed by a non-constant
3505 cannot be stored in a register; we must be able to do
3506 address arithmetic on its address.
3507 Likewise an array of elements of variable size. */
3508 if (TREE_CODE (idx
) != INTEGER_CST
3509 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
3510 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
3513 if (!cxx_mark_addressable (array
, true))
3514 return error_mark_node
;
3517 /* An array that is indexed by a constant value which is not within
3518 the array bounds cannot be stored in a register either; because we
3519 would get a crash in store_bit_field/extract_bit_field when trying
3520 to access a non-existent part of the register. */
3521 if (TREE_CODE (idx
) == INTEGER_CST
3522 && TYPE_DOMAIN (TREE_TYPE (array
))
3523 && ! int_fits_type_p (idx
, TYPE_DOMAIN (TREE_TYPE (array
))))
3525 if (!cxx_mark_addressable (array
))
3526 return error_mark_node
;
3529 /* Note in C++ it is valid to subscript a `register' array, since
3530 it is valid to take the address of something with that
3531 storage specification. */
3535 while (TREE_CODE (foo
) == COMPONENT_REF
)
3536 foo
= TREE_OPERAND (foo
, 0);
3537 if (VAR_P (foo
) && DECL_REGISTER (foo
)
3538 && (complain
& tf_warning
))
3539 warning_at (loc
, OPT_Wextra
,
3540 "subscripting array declared %<register%>");
3543 type
= TREE_TYPE (TREE_TYPE (array
));
3544 rval
= build4 (ARRAY_REF
, type
, array
, idx
, NULL_TREE
, NULL_TREE
);
3545 /* Array ref is const/volatile if the array elements are
3546 or if the array is.. */
3547 TREE_READONLY (rval
)
3548 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
3549 TREE_SIDE_EFFECTS (rval
)
3550 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
3551 TREE_THIS_VOLATILE (rval
)
3552 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
3553 ret
= require_complete_type_sfinae (rval
, complain
);
3554 protected_set_expr_location (ret
, loc
);
3556 ret
= non_lvalue_loc (loc
, ret
);
3561 tree ar
= cp_default_conversion (array
, complain
);
3562 tree ind
= cp_default_conversion (idx
, complain
);
3563 tree first
= NULL_TREE
;
3565 if (flag_strong_eval_order
== 2 && TREE_SIDE_EFFECTS (ind
))
3566 ar
= first
= save_expr (ar
);
3568 /* Put the integer in IND to simplify error checking. */
3569 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
3570 std::swap (ar
, ind
);
3572 if (ar
== error_mark_node
|| ind
== error_mark_node
)
3573 return error_mark_node
;
3575 if (!TYPE_PTR_P (TREE_TYPE (ar
)))
3577 if (complain
& tf_error
)
3578 error_at (loc
, "subscripted value is neither array nor pointer");
3579 return error_mark_node
;
3581 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
3583 if (complain
& tf_error
)
3584 error_at (loc
, "array subscript is not an integer");
3585 return error_mark_node
;
3588 warn_array_subscript_with_type_char (loc
, idx
);
3590 ret
= cp_build_binary_op (input_location
, PLUS_EXPR
, ar
, ind
, complain
);
3592 ret
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (ret
), first
, ret
);
3593 ret
= cp_build_indirect_ref (ret
, RO_ARRAY_INDEXING
, complain
);
3594 protected_set_expr_location (ret
, loc
);
3596 ret
= non_lvalue_loc (loc
, ret
);
3601 /* Entry point for Obj-C++. */
3604 build_array_ref (location_t loc
, tree array
, tree idx
)
3606 return cp_build_array_ref (loc
, array
, idx
, tf_warning_or_error
);
3609 /* Resolve a pointer to member function. INSTANCE is the object
3610 instance to use, if the member points to a virtual member.
3612 This used to avoid checking for virtual functions if basetype
3613 has no virtual functions, according to an earlier ANSI draft.
3614 With the final ISO C++ rules, such an optimization is
3615 incorrect: A pointer to a derived member can be static_cast
3616 to pointer-to-base-member, as long as the dynamic object
3617 later has the right member. So now we only do this optimization
3618 when we know the dynamic type of the object. */
3621 get_member_function_from_ptrfunc (tree
*instance_ptrptr
, tree function
,
3622 tsubst_flags_t complain
)
3624 if (TREE_CODE (function
) == OFFSET_REF
)
3625 function
= TREE_OPERAND (function
, 1);
3627 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
3629 tree idx
, delta
, e1
, e2
, e3
, vtbl
;
3631 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
3632 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
3634 tree instance_ptr
= *instance_ptrptr
;
3635 tree instance_save_expr
= 0;
3636 if (instance_ptr
== error_mark_node
)
3638 if (TREE_CODE (function
) == PTRMEM_CST
)
3640 /* Extracting the function address from a pmf is only
3641 allowed with -Wno-pmf-conversions. It only works for
3643 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
), complain
);
3644 e1
= convert (fntype
, e1
);
3649 if (complain
& tf_error
)
3650 error ("object missing in use of %qE", function
);
3651 return error_mark_node
;
3655 /* True if we know that the dynamic type of the object doesn't have
3656 virtual functions, so we can assume the PFN field is a pointer. */
3657 nonvirtual
= (COMPLETE_TYPE_P (basetype
)
3658 && !TYPE_POLYMORPHIC_P (basetype
)
3659 && resolves_to_fixed_type_p (instance_ptr
, 0));
3661 /* If we don't really have an object (i.e. in an ill-formed
3662 conversion from PMF to pointer), we can't resolve virtual
3663 functions anyway. */
3664 if (!nonvirtual
&& is_dummy_object (instance_ptr
))
3667 if (TREE_SIDE_EFFECTS (instance_ptr
))
3668 instance_ptr
= instance_save_expr
= save_expr (instance_ptr
);
3670 if (TREE_SIDE_EFFECTS (function
))
3671 function
= save_expr (function
);
3673 /* Start by extracting all the information from the PMF itself. */
3674 e3
= pfn_from_ptrmemfunc (function
);
3675 delta
= delta_from_ptrmemfunc (function
);
3676 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
3677 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
3679 int flag_sanitize_save
;
3680 case ptrmemfunc_vbit_in_pfn
:
3681 e1
= cp_build_binary_op (input_location
,
3682 BIT_AND_EXPR
, idx
, integer_one_node
,
3684 idx
= cp_build_binary_op (input_location
,
3685 MINUS_EXPR
, idx
, integer_one_node
,
3687 if (idx
== error_mark_node
)
3688 return error_mark_node
;
3691 case ptrmemfunc_vbit_in_delta
:
3692 e1
= cp_build_binary_op (input_location
,
3693 BIT_AND_EXPR
, delta
, integer_one_node
,
3695 /* Don't instrument the RSHIFT_EXPR we're about to create because
3696 we're going to use DELTA number of times, and that wouldn't play
3697 well with SAVE_EXPRs therein. */
3698 flag_sanitize_save
= flag_sanitize
;
3700 delta
= cp_build_binary_op (input_location
,
3701 RSHIFT_EXPR
, delta
, integer_one_node
,
3703 flag_sanitize
= flag_sanitize_save
;
3704 if (delta
== error_mark_node
)
3705 return error_mark_node
;
3712 if (e1
== error_mark_node
)
3713 return error_mark_node
;
3715 /* Convert down to the right base before using the instance. A
3716 special case is that in a pointer to member of class C, C may
3717 be incomplete. In that case, the function will of course be
3718 a member of C, and no conversion is required. In fact,
3719 lookup_base will fail in that case, because incomplete
3720 classes do not have BINFOs. */
3721 if (!same_type_ignoring_top_level_qualifiers_p
3722 (basetype
, TREE_TYPE (TREE_TYPE (instance_ptr
))))
3724 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
3725 basetype
, ba_check
, NULL
, complain
);
3726 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
,
3728 if (instance_ptr
== error_mark_node
)
3729 return error_mark_node
;
3731 /* ...and then the delta in the PMF. */
3732 instance_ptr
= fold_build_pointer_plus (instance_ptr
, delta
);
3734 /* Hand back the adjusted 'this' argument to our caller. */
3735 *instance_ptrptr
= instance_ptr
;
3738 /* Now just return the pointer. */
3741 /* Next extract the vtable pointer from the object. */
3742 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
3744 vtbl
= cp_build_fold_indirect_ref (vtbl
);
3745 if (vtbl
== error_mark_node
)
3746 return error_mark_node
;
3748 /* Finally, extract the function pointer from the vtable. */
3749 e2
= fold_build_pointer_plus_loc (input_location
, vtbl
, idx
);
3750 e2
= cp_build_fold_indirect_ref (e2
);
3751 if (e2
== error_mark_node
)
3752 return error_mark_node
;
3753 TREE_CONSTANT (e2
) = 1;
3755 /* When using function descriptors, the address of the
3756 vtable entry is treated as a function pointer. */
3757 if (TARGET_VTABLE_USES_DESCRIPTORS
)
3758 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
3759 cp_build_addr_expr (e2
, complain
));
3761 e2
= fold_convert (TREE_TYPE (e3
), e2
);
3762 e1
= build_conditional_expr (input_location
, e1
, e2
, e3
, complain
);
3763 if (e1
== error_mark_node
)
3764 return error_mark_node
;
3766 /* Make sure this doesn't get evaluated first inside one of the
3767 branches of the COND_EXPR. */
3768 if (instance_save_expr
)
3769 e1
= build2 (COMPOUND_EXPR
, TREE_TYPE (e1
),
3770 instance_save_expr
, e1
);
3777 /* Used by the C-common bits. */
3779 build_function_call (location_t
/*loc*/,
3780 tree function
, tree params
)
3782 return cp_build_function_call (function
, params
, tf_warning_or_error
);
3785 /* Used by the C-common bits. */
3787 build_function_call_vec (location_t
/*loc*/, vec
<location_t
> /*arg_loc*/,
3788 tree function
, vec
<tree
, va_gc
> *params
,
3789 vec
<tree
, va_gc
> * /*origtypes*/, tree orig_function
)
3791 vec
<tree
, va_gc
> *orig_params
= params
;
3792 tree ret
= cp_build_function_call_vec (function
, ¶ms
,
3793 tf_warning_or_error
, orig_function
);
3795 /* cp_build_function_call_vec can reallocate PARAMS by adding
3796 default arguments. That should never happen here. Verify
3798 gcc_assert (params
== orig_params
);
3803 /* Build a function call using a tree list of arguments. */
3806 cp_build_function_call (tree function
, tree params
, tsubst_flags_t complain
)
3811 for (; params
!= NULL_TREE
; params
= TREE_CHAIN (params
))
3812 vec_safe_push (vec
, TREE_VALUE (params
));
3813 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3817 /* Build a function call using varargs. */
3820 cp_build_function_call_nary (tree function
, tsubst_flags_t complain
, ...)
3826 va_start (args
, complain
);
3827 for (t
= va_arg (args
, tree
); t
!= NULL_TREE
; t
= va_arg (args
, tree
))
3828 vec_safe_push (vec
, t
);
3830 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3834 /* Build a function call using a vector of arguments.
3835 If FUNCTION is the result of resolving an overloaded target built-in,
3836 ORIG_FNDECL is the original function decl, otherwise it is null.
3837 PARAMS may be NULL if there are no parameters. This changes the
3838 contents of PARAMS. */
3841 cp_build_function_call_vec (tree function
, vec
<tree
, va_gc
> **params
,
3842 tsubst_flags_t complain
, tree orig_fndecl
)
3844 tree fntype
, fndecl
;
3846 tree original
= function
;
3850 vec
<tree
, va_gc
> *allocated
= NULL
;
3853 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3854 expressions, like those used for ObjC messenger dispatches. */
3855 if (params
!= NULL
&& !vec_safe_is_empty (*params
))
3856 function
= objc_rewrite_function_call (function
, (**params
)[0]);
3858 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3859 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3860 if (TREE_CODE (function
) == NOP_EXPR
3861 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
3862 function
= TREE_OPERAND (function
, 0);
3864 if (TREE_CODE (function
) == FUNCTION_DECL
)
3866 /* If the function is a non-template member function
3867 or a non-template friend, then we need to check the
3870 Note that if overload resolution failed with a single
3871 candidate this function will be used to explicitly diagnose
3872 the failure for the single call expression. The check is
3873 technically redundant since we also would have failed in
3874 add_function_candidate. */
3876 && (complain
& tf_error
)
3877 && !constraints_satisfied_p (function
))
3879 auto_diagnostic_group d
;
3880 error ("cannot call function %qD", function
);
3881 location_t loc
= DECL_SOURCE_LOCATION (function
);
3882 diagnose_constraints (loc
, function
, NULL_TREE
);
3883 return error_mark_node
;
3886 if (!mark_used (function
, complain
))
3887 return error_mark_node
;
3890 /* Convert anything with function type to a pointer-to-function. */
3891 if (DECL_MAIN_P (function
))
3893 if (complain
& tf_error
)
3894 pedwarn (input_location
, OPT_Wpedantic
,
3895 "ISO C++ forbids calling %<::main%> from within program");
3897 return error_mark_node
;
3899 function
= build_addr_func (function
, complain
);
3905 function
= build_addr_func (function
, complain
);
3908 if (function
== error_mark_node
)
3909 return error_mark_node
;
3911 fntype
= TREE_TYPE (function
);
3913 if (TYPE_PTRMEMFUNC_P (fntype
))
3915 if (complain
& tf_error
)
3916 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3917 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3918 original
, original
);
3919 return error_mark_node
;
3922 is_method
= (TYPE_PTR_P (fntype
)
3923 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
3925 if (!(TYPE_PTRFN_P (fntype
)
3927 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
3929 if (complain
& tf_error
)
3931 if (!flag_diagnostics_show_caret
)
3932 error_at (input_location
,
3933 "%qE cannot be used as a function", original
);
3934 else if (DECL_P (original
))
3935 error_at (input_location
,
3936 "%qD cannot be used as a function", original
);
3938 error_at (input_location
,
3939 "expression cannot be used as a function");
3942 return error_mark_node
;
3945 /* fntype now gets the type of function pointed to. */
3946 fntype
= TREE_TYPE (fntype
);
3947 parm_types
= TYPE_ARG_TYPES (fntype
);
3951 allocated
= make_tree_vector ();
3952 params
= &allocated
;
3955 nargs
= convert_arguments (parm_types
, params
, fndecl
, LOOKUP_NORMAL
,
3958 return error_mark_node
;
3960 argarray
= (*params
)->address ();
3962 /* Check for errors in format strings and inappropriately
3964 bool warned_p
= check_function_arguments (input_location
, fndecl
, fntype
,
3965 nargs
, argarray
, NULL
);
3967 ret
= build_cxx_call (function
, nargs
, argarray
, complain
, orig_fndecl
);
3971 tree c
= extract_call_expr (ret
);
3972 if (TREE_CODE (c
) == CALL_EXPR
)
3973 TREE_NO_WARNING (c
) = 1;
3976 if (allocated
!= NULL
)
3977 release_tree_vector (allocated
);
3982 /* Subroutine of convert_arguments.
3983 Print an error message about a wrong number of arguments. */
3986 error_args_num (location_t loc
, tree fndecl
, bool too_many_p
)
3990 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
3992 if (DECL_NAME (fndecl
) == NULL_TREE
3993 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
3996 ? G_("too many arguments to constructor %q#D")
3997 : G_("too few arguments to constructor %q#D"),
4002 ? G_("too many arguments to member function %q#D")
4003 : G_("too few arguments to member function %q#D"),
4009 ? G_("too many arguments to function %q#D")
4010 : G_("too few arguments to function %q#D"),
4012 if (!DECL_IS_BUILTIN (fndecl
))
4013 inform (DECL_SOURCE_LOCATION (fndecl
), "declared here");
4017 if (c_dialect_objc () && objc_message_selector ())
4020 ? G_("too many arguments to method %q#D")
4021 : G_("too few arguments to method %q#D"),
4022 objc_message_selector ());
4024 error_at (loc
, too_many_p
? G_("too many arguments to function")
4025 : G_("too few arguments to function"));
4029 /* Convert the actual parameter expressions in the list VALUES to the
4030 types in the list TYPELIST. The converted expressions are stored
4031 back in the VALUES vector.
4032 If parmdecls is exhausted, or when an element has NULL as its type,
4033 perform the default conversions.
4035 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4037 This is also where warnings about wrong number of args are generated.
4039 Returns the actual number of arguments processed (which might be less
4040 than the length of the vector), or -1 on error.
4042 In C++, unspecified trailing parameters can be filled in with their
4043 default arguments, if such were specified. Do so here. */
4046 convert_arguments (tree typelist
, vec
<tree
, va_gc
> **values
, tree fndecl
,
4047 int flags
, tsubst_flags_t complain
)
4052 /* Argument passing is always copy-initialization. */
4053 flags
|= LOOKUP_ONLYCONVERTING
;
4055 for (i
= 0, typetail
= typelist
;
4056 i
< vec_safe_length (*values
);
4059 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
4060 tree val
= (**values
)[i
];
4062 if (val
== error_mark_node
|| type
== error_mark_node
)
4065 if (type
== void_type_node
)
4067 if (complain
& tf_error
)
4069 error_args_num (input_location
, fndecl
, /*too_many_p=*/true);
4076 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4077 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4078 if (TREE_CODE (val
) == NOP_EXPR
4079 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
4080 && (type
== 0 || !TYPE_REF_P (type
)))
4081 val
= TREE_OPERAND (val
, 0);
4083 if (type
== 0 || !TYPE_REF_P (type
))
4085 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
4086 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val
)))
4087 val
= decay_conversion (val
, complain
);
4090 if (val
== error_mark_node
)
4095 /* Formal parm type is specified by a function prototype. */
4098 if (!COMPLETE_TYPE_P (complete_type (type
)))
4100 if (complain
& tf_error
)
4102 location_t loc
= EXPR_LOC_OR_LOC (val
, input_location
);
4105 auto_diagnostic_group d
;
4107 "parameter %P of %qD has incomplete type %qT",
4109 inform (get_fndecl_argument_location (fndecl
, i
),
4113 error_at (loc
, "parameter %P has incomplete type %qT", i
,
4116 parmval
= error_mark_node
;
4120 parmval
= convert_for_initialization
4121 (NULL_TREE
, type
, val
, flags
,
4122 ICR_ARGPASS
, fndecl
, i
, complain
);
4123 parmval
= convert_for_arg_passing (type
, parmval
, complain
);
4126 if (parmval
== error_mark_node
)
4129 (**values
)[i
] = parmval
;
4133 if (fndecl
&& magic_varargs_p (fndecl
))
4134 /* Don't do ellipsis conversion for __built_in_constant_p
4135 as this will result in spurious errors for non-trivial
4137 val
= require_complete_type_sfinae (val
, complain
);
4139 val
= convert_arg_to_ellipsis (val
, complain
);
4141 (**values
)[i
] = val
;
4145 typetail
= TREE_CHAIN (typetail
);
4148 if (typetail
!= 0 && typetail
!= void_list_node
)
4150 /* See if there are default arguments that can be used. Because
4151 we hold default arguments in the FUNCTION_TYPE (which is so
4152 wrong), we can see default parameters here from deduced
4153 contexts (and via typeof) for indirect function calls.
4154 Fortunately we know whether we have a function decl to
4155 provide default arguments in a language conformant
4157 if (fndecl
&& TREE_PURPOSE (typetail
)
4158 && TREE_CODE (TREE_PURPOSE (typetail
)) != DEFERRED_PARSE
)
4160 for (; typetail
!= void_list_node
; ++i
)
4162 /* After DR777, with explicit template args we can end up with a
4163 default argument followed by no default argument. */
4164 if (!TREE_PURPOSE (typetail
))
4167 = convert_default_arg (TREE_VALUE (typetail
),
4168 TREE_PURPOSE (typetail
),
4169 fndecl
, i
, complain
);
4171 if (parmval
== error_mark_node
)
4174 vec_safe_push (*values
, parmval
);
4175 typetail
= TREE_CHAIN (typetail
);
4176 /* ends with `...'. */
4177 if (typetail
== NULL_TREE
)
4182 if (typetail
&& typetail
!= void_list_node
)
4184 if (complain
& tf_error
)
4185 error_args_num (input_location
, fndecl
, /*too_many_p=*/false);
4193 /* Build a binary-operation expression, after performing default
4194 conversions on the operands. CODE is the kind of expression to
4195 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4196 are the tree codes which correspond to ARG1 and ARG2 when issuing
4197 warnings about possibly misplaced parentheses. They may differ
4198 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4199 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4200 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4201 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4202 ARG2_CODE as ERROR_MARK. */
4205 build_x_binary_op (const op_location_t
&loc
, enum tree_code code
, tree arg1
,
4206 enum tree_code arg1_code
, tree arg2
,
4207 enum tree_code arg2_code
, tree
*overload_p
,
4208 tsubst_flags_t complain
)
4213 tree overload
= NULL_TREE
;
4218 if (processing_template_decl
)
4220 if (type_dependent_expression_p (arg1
)
4221 || type_dependent_expression_p (arg2
))
4223 expr
= build_min_nt_loc (loc
, code
, arg1
, arg2
);
4224 maybe_save_operator_binding (expr
);
4227 arg1
= build_non_dependent_expr (arg1
);
4228 arg2
= build_non_dependent_expr (arg2
);
4231 if (code
== DOTSTAR_EXPR
)
4232 expr
= build_m_component_ref (arg1
, arg2
, complain
);
4234 expr
= build_new_op (loc
, code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
,
4235 &overload
, complain
);
4237 if (overload_p
!= NULL
)
4238 *overload_p
= overload
;
4240 /* Check for cases such as x+y<<z which users are likely to
4241 misinterpret. But don't warn about obj << x + y, since that is a
4242 common idiom for I/O. */
4243 if (warn_parentheses
4244 && (complain
& tf_warning
)
4245 && !processing_template_decl
4246 && !error_operand_p (arg1
)
4247 && !error_operand_p (arg2
)
4248 && (code
!= LSHIFT_EXPR
4249 || !CLASS_TYPE_P (TREE_TYPE (arg1
))))
4250 warn_about_parentheses (loc
, code
, arg1_code
, orig_arg1
,
4251 arg2_code
, orig_arg2
);
4253 if (processing_template_decl
&& expr
!= error_mark_node
)
4255 if (overload
!= NULL_TREE
)
4256 return (build_min_non_dep_op_overload
4257 (code
, expr
, overload
, orig_arg1
, orig_arg2
));
4259 return build_min_non_dep (code
, expr
, orig_arg1
, orig_arg2
);
4265 /* Build and return an ARRAY_REF expression. */
4268 build_x_array_ref (location_t loc
, tree arg1
, tree arg2
,
4269 tsubst_flags_t complain
)
4271 tree orig_arg1
= arg1
;
4272 tree orig_arg2
= arg2
;
4274 tree overload
= NULL_TREE
;
4276 if (processing_template_decl
)
4278 if (type_dependent_expression_p (arg1
)
4279 || type_dependent_expression_p (arg2
))
4280 return build_min_nt_loc (loc
, ARRAY_REF
, arg1
, arg2
,
4281 NULL_TREE
, NULL_TREE
);
4282 arg1
= build_non_dependent_expr (arg1
);
4283 arg2
= build_non_dependent_expr (arg2
);
4286 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, arg1
, arg2
,
4287 NULL_TREE
, &overload
, complain
);
4289 if (processing_template_decl
&& expr
!= error_mark_node
)
4291 if (overload
!= NULL_TREE
)
4292 return (build_min_non_dep_op_overload
4293 (ARRAY_REF
, expr
, overload
, orig_arg1
, orig_arg2
));
4295 return build_min_non_dep (ARRAY_REF
, expr
, orig_arg1
, orig_arg2
,
4296 NULL_TREE
, NULL_TREE
);
4301 /* Return whether OP is an expression of enum type cast to integer
4302 type. In C++ even unsigned enum types are cast to signed integer
4303 types. We do not want to issue warnings about comparisons between
4304 signed and unsigned types when one of the types is an enum type.
4305 Those warnings are always false positives in practice. */
4308 enum_cast_to_int (tree op
)
4310 if (CONVERT_EXPR_P (op
)
4311 && TREE_TYPE (op
) == integer_type_node
4312 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == ENUMERAL_TYPE
4313 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op
, 0))))
4316 /* The cast may have been pushed into a COND_EXPR. */
4317 if (TREE_CODE (op
) == COND_EXPR
)
4318 return (enum_cast_to_int (TREE_OPERAND (op
, 1))
4319 || enum_cast_to_int (TREE_OPERAND (op
, 2)));
4324 /* For the c-common bits. */
4326 build_binary_op (location_t location
, enum tree_code code
, tree op0
, tree op1
,
4329 return cp_build_binary_op (location
, code
, op0
, op1
, tf_warning_or_error
);
4332 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4333 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4336 build_vec_cmp (tree_code code
, tree type
,
4337 tree arg0
, tree arg1
)
4339 tree zero_vec
= build_zero_cst (type
);
4340 tree minus_one_vec
= build_minus_one_cst (type
);
4341 tree cmp_type
= build_same_sized_truth_vector_type(type
);
4342 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
4343 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
4346 /* Possibly warn about an address never being NULL. */
4349 warn_for_null_address (location_t location
, tree op
, tsubst_flags_t complain
)
4352 || (complain
& tf_warning
) == 0
4353 || c_inhibit_evaluation_warnings
!= 0
4354 || TREE_NO_WARNING (op
))
4357 tree cop
= fold_for_warn (op
);
4359 if (TREE_CODE (cop
) == ADDR_EXPR
4360 && decl_with_nonnull_addr_p (TREE_OPERAND (cop
, 0))
4361 && !TREE_NO_WARNING (cop
))
4362 warning_at (location
, OPT_Waddress
, "the address of %qD will never "
4363 "be NULL", TREE_OPERAND (cop
, 0));
4365 if (CONVERT_EXPR_P (op
)
4366 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op
, 0))))
4369 STRIP_NOPS (inner_op
);
4371 if (DECL_P (inner_op
))
4372 warning_at (location
, OPT_Waddress
,
4373 "the compiler can assume that the address of "
4374 "%qD will never be NULL", inner_op
);
4378 /* Build a binary-operation expression without default conversions.
4379 CODE is the kind of expression to build.
4380 LOCATION is the location_t of the operator in the source code.
4381 This function differs from `build' in several ways:
4382 the data type of the result is computed and recorded in it,
4383 warnings are generated if arg data types are invalid,
4384 special handling for addition and subtraction of pointers is known,
4385 and some optimization is done (operations on narrow ints
4386 are done in the narrower type when that gives the same result).
4387 Constant folding is also done before the result is returned.
4389 Note that the operands will never have enumeral types
4390 because either they have just had the default conversions performed
4391 or they have both just been converted to some other type in which
4392 the arithmetic is to be done.
4394 C++: must do special pointer arithmetic when implementing
4395 multiple inheritance, and deal with pointer to member functions. */
4398 cp_build_binary_op (const op_location_t
&location
,
4399 enum tree_code code
, tree orig_op0
, tree orig_op1
,
4400 tsubst_flags_t complain
)
4403 enum tree_code code0
, code1
;
4405 const char *invalid_op_diag
;
4407 /* Expression code to give to the expression when it is built.
4408 Normally this is CODE, which is what the caller asked for,
4409 but in some special cases we change it. */
4410 enum tree_code resultcode
= code
;
4412 /* Data type in which the computation is to be performed.
4413 In the simplest cases this is the common type of the arguments. */
4414 tree result_type
= NULL_TREE
;
4416 /* Nonzero means operands have already been type-converted
4417 in whatever way is necessary.
4418 Zero means they need to be converted to RESULT_TYPE. */
4421 /* Nonzero means create the expression with this type, rather than
4423 tree build_type
= 0;
4425 /* Nonzero means after finally constructing the expression
4426 convert it to this type. */
4427 tree final_type
= 0;
4429 tree result
, result_ovl
;
4431 /* Nonzero if this is an operation like MIN or MAX which can
4432 safely be computed in short if both args are promoted shorts.
4433 Also implies COMMON.
4434 -1 indicates a bitwise operation; this makes a difference
4435 in the exact conditions for when it is safe to do the operation
4436 in a narrower mode. */
4439 /* Nonzero if this is a comparison operation;
4440 if both args are promoted shorts, compare the original shorts.
4441 Also implies COMMON. */
4442 int short_compare
= 0;
4444 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4447 /* True if both operands have arithmetic type. */
4448 bool arithmetic_types_p
;
4450 /* Remember whether we're doing / or %. */
4451 bool doing_div_or_mod
= false;
4453 /* Remember whether we're doing << or >>. */
4454 bool doing_shift
= false;
4456 /* Tree holding instrumentation expression. */
4457 tree instrument_expr
= NULL_TREE
;
4459 /* Apply default conversions. */
4460 op0
= resolve_nondeduced_context (orig_op0
, complain
);
4461 op1
= resolve_nondeduced_context (orig_op1
, complain
);
4463 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
4464 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
4465 || code
== TRUTH_XOR_EXPR
)
4467 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
4468 op0
= decay_conversion (op0
, complain
);
4469 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
4470 op1
= decay_conversion (op1
, complain
);
4474 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
4475 op0
= cp_default_conversion (op0
, complain
);
4476 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
4477 op1
= cp_default_conversion (op1
, complain
);
4480 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4481 STRIP_TYPE_NOPS (op0
);
4482 STRIP_TYPE_NOPS (op1
);
4484 /* DTRT if one side is an overloaded function, but complain about it. */
4485 if (type_unknown_p (op0
))
4487 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
4488 if (t
!= error_mark_node
)
4490 if (complain
& tf_error
)
4491 permerror (location
,
4492 "assuming cast to type %qT from overloaded function",
4497 if (type_unknown_p (op1
))
4499 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
4500 if (t
!= error_mark_node
)
4502 if (complain
& tf_error
)
4503 permerror (location
,
4504 "assuming cast to type %qT from overloaded function",
4510 type0
= TREE_TYPE (op0
);
4511 type1
= TREE_TYPE (op1
);
4513 /* The expression codes of the data types of the arguments tell us
4514 whether the arguments are integers, floating, pointers, etc. */
4515 code0
= TREE_CODE (type0
);
4516 code1
= TREE_CODE (type1
);
4518 /* If an error was already reported for one of the arguments,
4519 avoid reporting another error. */
4520 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
4521 return error_mark_node
;
4523 if ((invalid_op_diag
4524 = targetm
.invalid_binary_op (code
, type0
, type1
)))
4526 if (complain
& tf_error
)
4527 error (invalid_op_diag
);
4528 return error_mark_node
;
4531 /* Issue warnings about peculiar, but valid, uses of NULL. */
4532 if ((null_node_p (orig_op0
) || null_node_p (orig_op1
))
4533 /* It's reasonable to use pointer values as operands of &&
4534 and ||, so NULL is no exception. */
4535 && code
!= TRUTH_ANDIF_EXPR
&& code
!= TRUTH_ORIF_EXPR
4536 && ( /* Both are NULL (or 0) and the operation was not a
4537 comparison or a pointer subtraction. */
4538 (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)
4539 && code
!= EQ_EXPR
&& code
!= NE_EXPR
&& code
!= MINUS_EXPR
)
4540 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4541 || (!null_ptr_cst_p (orig_op0
)
4542 && !TYPE_PTR_OR_PTRMEM_P (type0
))
4543 || (!null_ptr_cst_p (orig_op1
)
4544 && !TYPE_PTR_OR_PTRMEM_P (type1
)))
4545 && (complain
& tf_warning
))
4548 expansion_point_location_if_in_system_header (input_location
);
4550 warning_at (loc
, OPT_Wpointer_arith
, "NULL used in arithmetic");
4553 /* In case when one of the operands of the binary operation is
4554 a vector and another is a scalar -- convert scalar to vector. */
4555 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
4557 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
4558 complain
& tf_error
);
4560 switch (convert_flag
)
4563 return error_mark_node
;
4566 op0
= convert (TREE_TYPE (type1
), op0
);
4567 op0
= save_expr (op0
);
4568 op0
= build_vector_from_val (type1
, op0
);
4569 type0
= TREE_TYPE (op0
);
4570 code0
= TREE_CODE (type0
);
4576 op1
= convert (TREE_TYPE (type0
), op1
);
4577 op1
= save_expr (op1
);
4578 op1
= build_vector_from_val (type0
, op1
);
4579 type1
= TREE_TYPE (op1
);
4580 code1
= TREE_CODE (type1
);
4592 /* Subtraction of two similar pointers.
4593 We must subtract them as integers, then divide by object size. */
4594 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
4595 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4598 result
= pointer_diff (location
, op0
, op1
,
4599 common_pointer_type (type0
, type1
), complain
,
4601 if (instrument_expr
!= NULL
)
4602 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
4603 instrument_expr
, result
);
4607 /* In all other cases except pointer - int, the usual arithmetic
4609 else if (!(code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
))
4614 /* The pointer - int case is just like pointer + int; fall
4618 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
4619 && (code0
== INTEGER_TYPE
|| code1
== INTEGER_TYPE
))
4623 ptr_operand
= ((code0
== POINTER_TYPE
) ? op0
: op1
);
4624 int_operand
= ((code0
== INTEGER_TYPE
) ? op0
: op1
);
4625 if (processing_template_decl
)
4627 result_type
= TREE_TYPE (ptr_operand
);
4630 return cp_pointer_int_sum (location
, code
,
4642 case TRUNC_DIV_EXPR
:
4644 case FLOOR_DIV_EXPR
:
4645 case ROUND_DIV_EXPR
:
4646 case EXACT_DIV_EXPR
:
4647 if (TREE_CODE (op0
) == SIZEOF_EXPR
&& TREE_CODE (op1
) == SIZEOF_EXPR
)
4649 tree type0
= TREE_OPERAND (op0
, 0);
4650 tree type1
= TREE_OPERAND (op1
, 0);
4651 tree first_arg
= type0
;
4652 if (!TYPE_P (type0
))
4653 type0
= TREE_TYPE (type0
);
4654 if (!TYPE_P (type1
))
4655 type1
= TREE_TYPE (type1
);
4656 if (INDIRECT_TYPE_P (type0
) && same_type_p (TREE_TYPE (type0
), type1
))
4658 STRIP_ANY_LOCATION_WRAPPER (first_arg
);
4659 if (!(TREE_CODE (first_arg
) == PARM_DECL
4660 && DECL_ARRAY_PARAMETER_P (first_arg
)
4661 && warn_sizeof_array_argument
)
4662 && (complain
& tf_warning
))
4664 auto_diagnostic_group d
;
4665 if (warning_at (location
, OPT_Wsizeof_pointer_div
,
4666 "division %<sizeof (%T) / sizeof (%T)%> does "
4667 "not compute the number of array elements",
4669 if (DECL_P (first_arg
))
4670 inform (DECL_SOURCE_LOCATION (first_arg
),
4671 "first %<sizeof%> operand was declared here");
4676 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4677 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
4678 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4679 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
4681 enum tree_code tcode0
= code0
, tcode1
= code1
;
4682 doing_div_or_mod
= true;
4683 warn_for_div_by_zero (location
, fold_for_warn (op1
));
4685 if (tcode0
== COMPLEX_TYPE
|| tcode0
== VECTOR_TYPE
)
4686 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
4687 if (tcode1
== COMPLEX_TYPE
|| tcode1
== VECTOR_TYPE
)
4688 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
4690 if (!(tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
))
4691 resultcode
= RDIV_EXPR
;
4694 /* When dividing two signed integers, we have to promote to int.
4695 unless we divide by a constant != -1. Note that default
4696 conversion will have been performed on the operands at this
4697 point, so we have to dig out the original type to find out if
4699 tree stripped_op1
= tree_strip_any_location_wrapper (op1
);
4700 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4701 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4702 || (TREE_CODE (stripped_op1
) == INTEGER_CST
4703 && ! integer_all_onesp (stripped_op1
)));
4713 if ((code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4714 || (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4715 && !VECTOR_FLOAT_TYPE_P (type0
)
4716 && !VECTOR_FLOAT_TYPE_P (type1
)))
4720 case TRUNC_MOD_EXPR
:
4721 case FLOOR_MOD_EXPR
:
4722 doing_div_or_mod
= true;
4723 warn_for_div_by_zero (location
, fold_for_warn (op1
));
4725 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4726 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4727 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
4729 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4731 /* Although it would be tempting to shorten always here, that loses
4732 on some targets, since the modulo instruction is undefined if the
4733 quotient can't be represented in the computation mode. We shorten
4734 only if unsigned or if dividing by something we know != -1. */
4735 tree stripped_op1
= tree_strip_any_location_wrapper (op1
);
4736 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4737 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4738 || (TREE_CODE (stripped_op1
) == INTEGER_CST
4739 && ! integer_all_onesp (stripped_op1
)));
4744 case TRUTH_ANDIF_EXPR
:
4745 case TRUTH_ORIF_EXPR
:
4746 case TRUTH_AND_EXPR
:
4748 if (!VECTOR_TYPE_P (type0
) && VECTOR_TYPE_P (type1
))
4750 if (!COMPARISON_CLASS_P (op1
))
4751 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4752 build_zero_cst (type1
), complain
);
4753 if (code
== TRUTH_ANDIF_EXPR
)
4755 tree z
= build_zero_cst (TREE_TYPE (op1
));
4756 return build_conditional_expr (location
, op0
, op1
, z
, complain
);
4758 else if (code
== TRUTH_ORIF_EXPR
)
4760 tree m1
= build_all_ones_cst (TREE_TYPE (op1
));
4761 return build_conditional_expr (location
, op0
, m1
, op1
, complain
);
4766 if (VECTOR_TYPE_P (type0
))
4768 if (!COMPARISON_CLASS_P (op0
))
4769 op0
= cp_build_binary_op (EXPR_LOCATION (op0
), NE_EXPR
, op0
,
4770 build_zero_cst (type0
), complain
);
4771 if (!VECTOR_TYPE_P (type1
))
4773 tree m1
= build_all_ones_cst (TREE_TYPE (op0
));
4774 tree z
= build_zero_cst (TREE_TYPE (op0
));
4775 op1
= build_conditional_expr (location
, op1
, m1
, z
, complain
);
4777 else if (!COMPARISON_CLASS_P (op1
))
4778 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4779 build_zero_cst (type1
), complain
);
4781 if (code
== TRUTH_ANDIF_EXPR
)
4782 code
= BIT_AND_EXPR
;
4783 else if (code
== TRUTH_ORIF_EXPR
)
4784 code
= BIT_IOR_EXPR
;
4788 return cp_build_binary_op (location
, code
, op0
, op1
, complain
);
4791 result_type
= boolean_type_node
;
4794 /* Shift operations: result has same type as first operand;
4795 always convert second operand to int.
4796 Also set SHORT_SHIFT if shifting rightward. */
4799 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4800 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4802 result_type
= type0
;
4805 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4806 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4807 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4808 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
4809 TYPE_VECTOR_SUBPARTS (type1
)))
4811 result_type
= type0
;
4814 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4816 tree const_op1
= fold_for_warn (op1
);
4817 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4819 result_type
= type0
;
4821 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4823 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4825 if ((complain
& tf_warning
)
4826 && c_inhibit_evaluation_warnings
== 0)
4827 warning_at (location
, OPT_Wshift_count_negative
,
4828 "right shift count is negative");
4832 if (compare_tree_int (const_op1
, TYPE_PRECISION (type0
)) >= 0
4833 && (complain
& tf_warning
)
4834 && c_inhibit_evaluation_warnings
== 0)
4835 warning_at (location
, OPT_Wshift_count_overflow
,
4836 "right shift count >= width of type");
4839 /* Avoid converting op1 to result_type later. */
4845 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4846 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4848 result_type
= type0
;
4851 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4852 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4853 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4854 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
4855 TYPE_VECTOR_SUBPARTS (type1
)))
4857 result_type
= type0
;
4860 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4862 tree const_op0
= fold_for_warn (op0
);
4863 if (TREE_CODE (const_op0
) != INTEGER_CST
)
4865 tree const_op1
= fold_for_warn (op1
);
4866 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4868 result_type
= type0
;
4870 if (TREE_CODE (const_op0
) == INTEGER_CST
4871 && tree_int_cst_sgn (const_op0
) < 0
4872 && (complain
& tf_warning
)
4873 && c_inhibit_evaluation_warnings
== 0)
4874 warning_at (location
, OPT_Wshift_negative_value
,
4875 "left shift of negative value");
4876 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4878 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4880 if ((complain
& tf_warning
)
4881 && c_inhibit_evaluation_warnings
== 0)
4882 warning_at (location
, OPT_Wshift_count_negative
,
4883 "left shift count is negative");
4885 else if (compare_tree_int (const_op1
,
4886 TYPE_PRECISION (type0
)) >= 0)
4888 if ((complain
& tf_warning
)
4889 && c_inhibit_evaluation_warnings
== 0)
4890 warning_at (location
, OPT_Wshift_count_overflow
,
4891 "left shift count >= width of type");
4893 else if (TREE_CODE (const_op0
) == INTEGER_CST
4894 && (complain
& tf_warning
))
4895 maybe_warn_shift_overflow (location
, const_op0
, const_op1
);
4897 /* Avoid converting op1 to result_type later. */
4904 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4905 goto vector_compare
;
4906 if ((complain
& tf_warning
)
4907 && c_inhibit_evaluation_warnings
== 0
4908 && (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
)))
4909 warning_at (location
, OPT_Wfloat_equal
,
4910 "comparing floating-point with %<==%> "
4911 "or %<!=%> is unsafe");
4912 if (complain
& tf_warning
)
4914 tree stripped_orig_op0
= tree_strip_any_location_wrapper (orig_op0
);
4915 tree stripped_orig_op1
= tree_strip_any_location_wrapper (orig_op1
);
4916 if ((TREE_CODE (stripped_orig_op0
) == STRING_CST
4917 && !integer_zerop (cp_fully_fold (op1
)))
4918 || (TREE_CODE (stripped_orig_op1
) == STRING_CST
4919 && !integer_zerop (cp_fully_fold (op0
))))
4920 warning_at (location
, OPT_Waddress
,
4921 "comparison with string literal results in "
4922 "unspecified behavior");
4925 build_type
= boolean_type_node
;
4926 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4927 || code0
== COMPLEX_TYPE
|| code0
== ENUMERAL_TYPE
)
4928 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4929 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
))
4931 else if (((code0
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type0
))
4932 && null_ptr_cst_p (orig_op1
))
4933 /* Handle, eg, (void*)0 (c++/43906), and more. */
4934 || (code0
== POINTER_TYPE
4935 && TYPE_PTR_P (type1
) && integer_zerop (op1
)))
4937 if (TYPE_PTR_P (type1
))
4938 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4939 CPO_COMPARISON
, complain
);
4941 result_type
= type0
;
4943 if (char_type_p (TREE_TYPE (orig_op1
)))
4945 auto_diagnostic_group d
;
4946 if (warning_at (location
, OPT_Wpointer_compare
,
4947 "comparison between pointer and zero character "
4950 "did you mean to dereference the pointer?");
4952 warn_for_null_address (location
, op0
, complain
);
4954 else if (((code1
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type1
))
4955 && null_ptr_cst_p (orig_op0
))
4956 /* Handle, eg, (void*)0 (c++/43906), and more. */
4957 || (code1
== POINTER_TYPE
4958 && TYPE_PTR_P (type0
) && integer_zerop (op0
)))
4960 if (TYPE_PTR_P (type0
))
4961 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4962 CPO_COMPARISON
, complain
);
4964 result_type
= type1
;
4966 if (char_type_p (TREE_TYPE (orig_op0
)))
4968 auto_diagnostic_group d
;
4969 if (warning_at (location
, OPT_Wpointer_compare
,
4970 "comparison between pointer and zero character "
4973 "did you mean to dereference the pointer?");
4975 warn_for_null_address (location
, op1
, complain
);
4977 else if ((code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4978 || (TYPE_PTRDATAMEM_P (type0
) && TYPE_PTRDATAMEM_P (type1
)))
4979 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4980 CPO_COMPARISON
, complain
);
4981 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
4982 /* One of the operands must be of nullptr_t type. */
4983 result_type
= TREE_TYPE (nullptr_node
);
4984 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4986 result_type
= type0
;
4987 if (complain
& tf_error
)
4988 permerror (location
, "ISO C++ forbids comparison between "
4989 "pointer and integer");
4991 return error_mark_node
;
4993 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4995 result_type
= type1
;
4996 if (complain
& tf_error
)
4997 permerror (location
, "ISO C++ forbids comparison between "
4998 "pointer and integer");
5000 return error_mark_node
;
5002 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (orig_op1
))
5004 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5005 == ptrmemfunc_vbit_in_delta
)
5007 tree pfn0
, delta0
, e1
, e2
;
5009 if (TREE_SIDE_EFFECTS (op0
))
5010 op0
= cp_save_expr (op0
);
5012 pfn0
= pfn_from_ptrmemfunc (op0
);
5013 delta0
= delta_from_ptrmemfunc (op0
);
5014 e1
= cp_build_binary_op (location
,
5017 build_zero_cst (TREE_TYPE (pfn0
)),
5019 e2
= cp_build_binary_op (location
,
5025 if (complain
& tf_warning
)
5026 maybe_warn_zero_as_null_pointer_constant (op1
, input_location
);
5028 e2
= cp_build_binary_op (location
,
5029 EQ_EXPR
, e2
, integer_zero_node
,
5031 op0
= cp_build_binary_op (location
,
5032 TRUTH_ANDIF_EXPR
, e1
, e2
,
5034 op1
= cp_convert (TREE_TYPE (op0
), integer_one_node
, complain
);
5038 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
5039 op1
= cp_convert (TREE_TYPE (op0
), op1
, complain
);
5041 result_type
= TREE_TYPE (op0
);
5043 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (orig_op0
))
5044 return cp_build_binary_op (location
, code
, op1
, op0
, complain
);
5045 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
))
5048 /* E will be the final comparison. */
5050 /* E1 and E2 are for scratch. */
5058 type
= composite_pointer_type (type0
, type1
, op0
, op1
,
5059 CPO_COMPARISON
, complain
);
5061 if (!same_type_p (TREE_TYPE (op0
), type
))
5062 op0
= cp_convert_and_check (type
, op0
, complain
);
5063 if (!same_type_p (TREE_TYPE (op1
), type
))
5064 op1
= cp_convert_and_check (type
, op1
, complain
);
5066 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5067 return error_mark_node
;
5069 if (TREE_SIDE_EFFECTS (op0
))
5070 op0
= save_expr (op0
);
5071 if (TREE_SIDE_EFFECTS (op1
))
5072 op1
= save_expr (op1
);
5074 pfn0
= pfn_from_ptrmemfunc (op0
);
5075 pfn0
= cp_fully_fold (pfn0
);
5076 /* Avoid -Waddress warnings (c++/64877). */
5077 if (TREE_CODE (pfn0
) == ADDR_EXPR
)
5078 TREE_NO_WARNING (pfn0
) = 1;
5079 pfn1
= pfn_from_ptrmemfunc (op1
);
5080 pfn1
= cp_fully_fold (pfn1
);
5081 delta0
= delta_from_ptrmemfunc (op0
);
5082 delta1
= delta_from_ptrmemfunc (op1
);
5083 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5084 == ptrmemfunc_vbit_in_delta
)
5089 && ((op0.delta == op1.delta)
5090 || (!op0.pfn && op0.delta & 1 == 0
5091 && op1.delta & 1 == 0))
5093 The reason for the `!op0.pfn' bit is that a NULL
5094 pointer-to-member is any member with a zero PFN and
5095 LSB of the DELTA field is 0. */
5097 e1
= cp_build_binary_op (location
, BIT_AND_EXPR
,
5101 e1
= cp_build_binary_op (location
,
5102 EQ_EXPR
, e1
, integer_zero_node
,
5104 e2
= cp_build_binary_op (location
, BIT_AND_EXPR
,
5108 e2
= cp_build_binary_op (location
,
5109 EQ_EXPR
, e2
, integer_zero_node
,
5111 e1
= cp_build_binary_op (location
,
5112 TRUTH_ANDIF_EXPR
, e2
, e1
,
5114 e2
= cp_build_binary_op (location
, EQ_EXPR
,
5116 build_zero_cst (TREE_TYPE (pfn0
)),
5118 e2
= cp_build_binary_op (location
,
5119 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
5120 e1
= cp_build_binary_op (location
,
5121 EQ_EXPR
, delta0
, delta1
, complain
);
5122 e1
= cp_build_binary_op (location
,
5123 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
5130 && (!op0.pfn || op0.delta == op1.delta))
5132 The reason for the `!op0.pfn' bit is that a NULL
5133 pointer-to-member is any member with a zero PFN; the
5134 DELTA field is unspecified. */
5136 e1
= cp_build_binary_op (location
,
5137 EQ_EXPR
, delta0
, delta1
, complain
);
5138 e2
= cp_build_binary_op (location
,
5141 build_zero_cst (TREE_TYPE (pfn0
)),
5143 e1
= cp_build_binary_op (location
,
5144 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
5146 e2
= build2 (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
5147 e
= cp_build_binary_op (location
,
5148 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
5149 if (code
== EQ_EXPR
)
5151 return cp_build_binary_op (location
,
5152 EQ_EXPR
, e
, integer_zero_node
, complain
);
5156 gcc_assert (!TYPE_PTRMEMFUNC_P (type0
)
5157 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
),
5159 gcc_assert (!TYPE_PTRMEMFUNC_P (type1
)
5160 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
),
5168 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
5169 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
5171 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5172 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
5173 CPO_COMPARISON
, complain
);
5180 if (TREE_CODE (orig_op0
) == STRING_CST
5181 || TREE_CODE (orig_op1
) == STRING_CST
)
5183 if (complain
& tf_warning
)
5184 warning_at (location
, OPT_Waddress
,
5185 "comparison with string literal results "
5186 "in unspecified behavior");
5189 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
5193 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
5195 && !vector_types_compatible_elements_p (type0
, type1
))
5197 if (complain
& tf_error
)
5199 error_at (location
, "comparing vectors with different "
5201 inform (location
, "operand types are %qT and %qT",
5204 return error_mark_node
;
5207 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0
),
5208 TYPE_VECTOR_SUBPARTS (type1
)))
5210 if (complain
& tf_error
)
5212 error_at (location
, "comparing vectors with different "
5213 "number of elements");
5214 inform (location
, "operand types are %qT and %qT",
5217 return error_mark_node
;
5220 /* It's not precisely specified how the usual arithmetic
5221 conversions apply to the vector types. Here, we use
5222 the unsigned type if one of the operands is signed and
5223 the other one is unsigned. */
5224 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
5226 if (!TYPE_UNSIGNED (type0
))
5227 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
5229 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
5230 warning_at (location
, OPT_Wsign_compare
, "comparison between "
5231 "types %qT and %qT", type0
, type1
);
5234 /* Always construct signed integer vector type. */
5235 intt
= c_common_type_for_size
5236 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0
))), 0);
5239 if (complain
& tf_error
)
5240 error_at (location
, "could not find an integer type "
5241 "of the same size as %qT", TREE_TYPE (type0
));
5242 return error_mark_node
;
5244 result_type
= build_opaque_vector_type (intt
,
5245 TYPE_VECTOR_SUBPARTS (type0
));
5246 return build_vec_cmp (resultcode
, result_type
, op0
, op1
);
5248 build_type
= boolean_type_node
;
5249 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
5250 || code0
== ENUMERAL_TYPE
)
5251 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5252 || code1
== ENUMERAL_TYPE
))
5254 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5255 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
5256 CPO_COMPARISON
, complain
);
5257 else if (code0
== POINTER_TYPE
&& null_ptr_cst_p (orig_op1
))
5259 result_type
= type0
;
5260 if (extra_warnings
&& (complain
& tf_warning
))
5261 warning_at (location
, OPT_Wextra
,
5262 "ordered comparison of pointer with integer zero");
5264 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (orig_op0
))
5266 result_type
= type1
;
5267 if (extra_warnings
&& (complain
& tf_warning
))
5268 warning_at (location
, OPT_Wextra
,
5269 "ordered comparison of pointer with integer zero");
5271 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
5272 /* One of the operands must be of nullptr_t type. */
5273 result_type
= TREE_TYPE (nullptr_node
);
5274 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5276 result_type
= type0
;
5277 if (complain
& tf_error
)
5278 permerror (location
, "ISO C++ forbids comparison between "
5279 "pointer and integer");
5281 return error_mark_node
;
5283 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
5285 result_type
= type1
;
5286 if (complain
& tf_error
)
5287 permerror (location
, "ISO C++ forbids comparison between "
5288 "pointer and integer");
5290 return error_mark_node
;
5293 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
5294 && !processing_template_decl
5295 && sanitize_flags_p (SANITIZE_POINTER_COMPARE
))
5297 op0
= save_expr (op0
);
5298 op1
= save_expr (op1
);
5300 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE
);
5301 instrument_expr
= build_call_expr_loc (location
, tt
, 2, op0
, op1
);
5306 case UNORDERED_EXPR
:
5313 build_type
= integer_type_node
;
5314 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
5316 if (complain
& tf_error
)
5317 error ("unordered comparison on non-floating-point argument");
5318 return error_mark_node
;
5327 if (((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
5328 || code0
== ENUMERAL_TYPE
)
5329 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5330 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
)))
5331 arithmetic_types_p
= 1;
5334 arithmetic_types_p
= 0;
5335 /* Vector arithmetic is only allowed when both sides are vectors. */
5336 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
5338 if (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
5339 || !vector_types_compatible_elements_p (type0
, type1
))
5341 if (complain
& tf_error
)
5343 /* "location" already embeds the locations of the
5344 operands, so we don't need to add them separately
5346 rich_location
richloc (line_table
, location
);
5347 binary_op_error (&richloc
, code
, type0
, type1
);
5349 return error_mark_node
;
5351 arithmetic_types_p
= 1;
5354 /* Determine the RESULT_TYPE, if it is not already known. */
5356 && arithmetic_types_p
5357 && (shorten
|| common
|| short_compare
))
5359 result_type
= cp_common_type (type0
, type1
);
5360 if (complain
& tf_warning
)
5361 do_warn_double_promotion (result_type
, type0
, type1
,
5362 "implicit conversion from %qH to %qI "
5363 "to match other operand of binary "
5370 if (complain
& tf_error
)
5372 binary_op_rich_location
richloc (location
,
5373 orig_op0
, orig_op1
, true);
5375 "invalid operands of types %qT and %qT to binary %qO",
5376 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
5378 return error_mark_node
;
5381 /* If we're in a template, the only thing we need to know is the
5383 if (processing_template_decl
)
5385 /* Since the middle-end checks the type when doing a build2, we
5386 need to build the tree in pieces. This built tree will never
5387 get out of the front-end as we replace it when instantiating
5389 tree tmp
= build2 (resultcode
,
5390 build_type
? build_type
: result_type
,
5392 TREE_OPERAND (tmp
, 0) = op0
;
5396 /* Remember the original type; RESULT_TYPE might be changed later on
5397 by shorten_binary_op. */
5398 tree orig_type
= result_type
;
5400 if (arithmetic_types_p
)
5402 bool first_complex
= (code0
== COMPLEX_TYPE
);
5403 bool second_complex
= (code1
== COMPLEX_TYPE
);
5404 int none_complex
= (!first_complex
&& !second_complex
);
5406 /* Adapted from patch for c/24581. */
5407 if (first_complex
!= second_complex
5408 && (code
== PLUS_EXPR
5409 || code
== MINUS_EXPR
5410 || code
== MULT_EXPR
5411 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
5412 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
5413 && flag_signed_zeros
)
5415 /* An operation on mixed real/complex operands must be
5416 handled specially, but the language-independent code can
5417 more easily optimize the plain complex arithmetic if
5418 -fno-signed-zeros. */
5419 tree real_type
= TREE_TYPE (result_type
);
5423 if (TREE_TYPE (op0
) != result_type
)
5424 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5425 if (TREE_TYPE (op1
) != real_type
)
5426 op1
= cp_convert_and_check (real_type
, op1
, complain
);
5430 if (TREE_TYPE (op0
) != real_type
)
5431 op0
= cp_convert_and_check (real_type
, op0
, complain
);
5432 if (TREE_TYPE (op1
) != result_type
)
5433 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5435 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
5436 return error_mark_node
;
5439 op0
= save_expr (op0
);
5440 real
= cp_build_unary_op (REALPART_EXPR
, op0
, true, complain
);
5441 imag
= cp_build_unary_op (IMAGPART_EXPR
, op0
, true, complain
);
5445 case TRUNC_DIV_EXPR
:
5446 op1
= save_expr (op1
);
5447 imag
= build2 (resultcode
, real_type
, imag
, op1
);
5451 real
= build2 (resultcode
, real_type
, real
, op1
);
5459 op1
= save_expr (op1
);
5460 real
= cp_build_unary_op (REALPART_EXPR
, op1
, true, complain
);
5461 imag
= cp_build_unary_op (IMAGPART_EXPR
, op1
, true, complain
);
5465 op0
= save_expr (op0
);
5466 imag
= build2 (resultcode
, real_type
, op0
, imag
);
5469 real
= build2 (resultcode
, real_type
, op0
, real
);
5472 real
= build2 (resultcode
, real_type
, op0
, real
);
5473 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
5479 result
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
5483 /* For certain operations (which identify themselves by shorten != 0)
5484 if both args were extended from the same smaller type,
5485 do the arithmetic in that type and then extend.
5487 shorten !=0 and !=1 indicates a bitwise operation.
5488 For them, this optimization is safe only if
5489 both args are zero-extended or both are sign-extended.
5490 Otherwise, we might change the result.
5491 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5492 but calculated in (unsigned short) it would be (unsigned short)-1. */
5494 if (shorten
&& none_complex
)
5496 final_type
= result_type
;
5497 result_type
= shorten_binary_op (result_type
, op0
, op1
,
5501 /* Comparison operations are shortened too but differently.
5502 They identify themselves by setting short_compare = 1. */
5506 /* We call shorten_compare only for diagnostics. */
5507 tree xop0
= fold_simple (op0
);
5508 tree xop1
= fold_simple (op1
);
5509 tree xresult_type
= result_type
;
5510 enum tree_code xresultcode
= resultcode
;
5511 shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
5515 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
5516 && warn_sign_compare
5517 /* Do not warn until the template is instantiated; we cannot
5518 bound the ranges of the arguments until that point. */
5519 && !processing_template_decl
5520 && (complain
& tf_warning
)
5521 && c_inhibit_evaluation_warnings
== 0
5522 /* Even unsigned enum types promote to signed int. We don't
5523 want to issue -Wsign-compare warnings for this case. */
5524 && !enum_cast_to_int (orig_op0
)
5525 && !enum_cast_to_int (orig_op1
))
5527 warn_for_sign_compare (location
, orig_op0
, orig_op1
, op0
, op1
,
5528 result_type
, resultcode
);
5532 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5533 Then the expression will be built.
5534 It will be given type FINAL_TYPE if that is nonzero;
5535 otherwise, it will be given type RESULT_TYPE. */
5538 warning_sentinel
w (warn_sign_conversion
, short_compare
);
5539 if (!same_type_p (TREE_TYPE (op0
), result_type
))
5540 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5541 if (!same_type_p (TREE_TYPE (op1
), result_type
))
5542 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5544 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5545 return error_mark_node
;
5548 if (build_type
== NULL_TREE
)
5549 build_type
= result_type
;
5552 && flag_strong_eval_order
== 2
5553 && TREE_SIDE_EFFECTS (op1
)
5554 && !processing_template_decl
)
5556 /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
5557 op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
5558 op0
= cp_save_expr (op0
);
5559 instrument_expr
= op0
;
5562 if (sanitize_flags_p ((SANITIZE_SHIFT
5563 | SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
))
5564 && current_function_decl
!= NULL_TREE
5565 && !processing_template_decl
5566 && (doing_div_or_mod
|| doing_shift
))
5568 /* OP0 and/or OP1 might have side-effects. */
5569 op0
= cp_save_expr (op0
);
5570 op1
= cp_save_expr (op1
);
5571 op0
= fold_non_dependent_expr (op0
, complain
);
5572 op1
= fold_non_dependent_expr (op1
, complain
);
5573 tree instrument_expr1
= NULL_TREE
;
5574 if (doing_div_or_mod
5575 && sanitize_flags_p (SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
))
5577 /* For diagnostics we want to use the promoted types without
5578 shorten_binary_op. So convert the arguments to the
5579 original result_type. */
5582 if (TREE_TYPE (cop0
) != orig_type
)
5583 cop0
= cp_convert (orig_type
, op0
, complain
);
5584 if (TREE_TYPE (cop1
) != orig_type
)
5585 cop1
= cp_convert (orig_type
, op1
, complain
);
5586 instrument_expr1
= ubsan_instrument_division (location
, cop0
, cop1
);
5588 else if (doing_shift
&& sanitize_flags_p (SANITIZE_SHIFT
))
5589 instrument_expr1
= ubsan_instrument_shift (location
, code
, op0
, op1
);
5590 if (instrument_expr
!= NULL
)
5591 instrument_expr
= add_stmt_to_compound (instrument_expr
,
5594 instrument_expr
= instrument_expr1
;
5597 result
= build2_loc (location
, resultcode
, build_type
, op0
, op1
);
5598 if (final_type
!= 0)
5599 result
= cp_convert (final_type
, result
, complain
);
5601 if (instrument_expr
!= NULL
)
5602 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
5603 instrument_expr
, result
);
5605 if (!processing_template_decl
)
5607 op0
= cp_fully_fold (op0
);
5608 /* Only consider the second argument if the first isn't overflowed. */
5609 if (!CONSTANT_CLASS_P (op0
) || TREE_OVERFLOW_P (op0
))
5611 op1
= cp_fully_fold (op1
);
5612 if (!CONSTANT_CLASS_P (op1
) || TREE_OVERFLOW_P (op1
))
5615 else if (!CONSTANT_CLASS_P (op0
) || !CONSTANT_CLASS_P (op1
)
5616 || TREE_OVERFLOW_P (op0
) || TREE_OVERFLOW_P (op1
))
5619 result_ovl
= fold_build2 (resultcode
, build_type
, op0
, op1
);
5620 if (TREE_OVERFLOW_P (result_ovl
))
5621 overflow_warning (location
, result_ovl
);
5626 /* Build a VEC_PERM_EXPR.
5627 This is a simple wrapper for c_build_vec_perm_expr. */
5629 build_x_vec_perm_expr (location_t loc
,
5630 tree arg0
, tree arg1
, tree arg2
,
5631 tsubst_flags_t complain
)
5633 tree orig_arg0
= arg0
;
5634 tree orig_arg1
= arg1
;
5635 tree orig_arg2
= arg2
;
5636 if (processing_template_decl
)
5638 if (type_dependent_expression_p (arg0
)
5639 || type_dependent_expression_p (arg1
)
5640 || type_dependent_expression_p (arg2
))
5641 return build_min_nt_loc (loc
, VEC_PERM_EXPR
, arg0
, arg1
, arg2
);
5642 arg0
= build_non_dependent_expr (arg0
);
5644 arg1
= build_non_dependent_expr (arg1
);
5645 arg2
= build_non_dependent_expr (arg2
);
5647 tree exp
= c_build_vec_perm_expr (loc
, arg0
, arg1
, arg2
, complain
& tf_error
);
5648 if (processing_template_decl
&& exp
!= error_mark_node
)
5649 return build_min_non_dep (VEC_PERM_EXPR
, exp
, orig_arg0
,
5650 orig_arg1
, orig_arg2
);
5654 /* Return a tree for the sum or difference (RESULTCODE says which)
5655 of pointer PTROP and integer INTOP. */
5658 cp_pointer_int_sum (location_t loc
, enum tree_code resultcode
, tree ptrop
,
5659 tree intop
, tsubst_flags_t complain
)
5661 tree res_type
= TREE_TYPE (ptrop
);
5663 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5664 in certain circumstance (when it's valid to do so). So we need
5665 to make sure it's complete. We don't need to check here, if we
5666 can actually complete it at all, as those checks will be done in
5667 pointer_int_sum() anyway. */
5668 complete_type (TREE_TYPE (res_type
));
5670 return pointer_int_sum (loc
, resultcode
, ptrop
,
5671 intop
, complain
& tf_warning_or_error
);
5674 /* Return a tree for the difference of pointers OP0 and OP1.
5675 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
5676 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5679 pointer_diff (location_t loc
, tree op0
, tree op1
, tree ptrtype
,
5680 tsubst_flags_t complain
, tree
*instrument_expr
)
5682 tree result
, inttype
;
5683 tree restype
= ptrdiff_type_node
;
5684 tree target_type
= TREE_TYPE (ptrtype
);
5686 if (!complete_type_or_else (target_type
, NULL_TREE
))
5687 return error_mark_node
;
5689 if (VOID_TYPE_P (target_type
))
5691 if (complain
& tf_error
)
5692 permerror (loc
, "ISO C++ forbids using pointer of "
5693 "type %<void *%> in subtraction");
5695 return error_mark_node
;
5697 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
5699 if (complain
& tf_error
)
5700 permerror (loc
, "ISO C++ forbids using pointer to "
5701 "a function in subtraction");
5703 return error_mark_node
;
5705 if (TREE_CODE (target_type
) == METHOD_TYPE
)
5707 if (complain
& tf_error
)
5708 permerror (loc
, "ISO C++ forbids using pointer to "
5709 "a method in subtraction");
5711 return error_mark_node
;
5714 /* Determine integer type result of the subtraction. This will usually
5715 be the same as the result type (ptrdiff_t), but may need to be a wider
5716 type if pointers for the address space are wider than ptrdiff_t. */
5717 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
5718 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
5722 if (!processing_template_decl
5723 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT
))
5725 op0
= save_expr (op0
);
5726 op1
= save_expr (op1
);
5728 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT
);
5729 *instrument_expr
= build_call_expr_loc (loc
, tt
, 2, op0
, op1
);
5732 /* First do the subtraction, then build the divide operator
5733 and only convert at the very end.
5734 Do not do default conversions in case restype is a short type. */
5736 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5737 pointers. If some platform cannot provide that, or has a larger
5738 ptrdiff_type to support differences larger than half the address
5739 space, cast the pointers to some larger integer type and do the
5740 computations in that type. */
5741 if (TYPE_PRECISION (inttype
) > TYPE_PRECISION (TREE_TYPE (op0
)))
5742 op0
= cp_build_binary_op (loc
,
5744 cp_convert (inttype
, op0
, complain
),
5745 cp_convert (inttype
, op1
, complain
),
5748 op0
= build2_loc (loc
, POINTER_DIFF_EXPR
, inttype
, op0
, op1
);
5750 /* This generates an error if op1 is a pointer to an incomplete type. */
5751 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
5753 if (complain
& tf_error
)
5754 error_at (loc
, "invalid use of a pointer to an incomplete type in "
5755 "pointer arithmetic");
5757 return error_mark_node
;
5760 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1
)))
5762 if (complain
& tf_error
)
5763 error_at (loc
, "arithmetic on pointer to an empty aggregate");
5765 return error_mark_node
;
5768 op1
= (TYPE_PTROB_P (ptrtype
)
5769 ? size_in_bytes_loc (loc
, target_type
)
5770 : integer_one_node
);
5772 /* Do the division. */
5774 result
= build2_loc (loc
, EXACT_DIV_EXPR
, inttype
, op0
,
5775 cp_convert (inttype
, op1
, complain
));
5776 return cp_convert (restype
, result
, complain
);
5779 /* Construct and perhaps optimize a tree representation
5780 for a unary operation. CODE, a tree_code, specifies the operation
5781 and XARG is the operand. */
5784 build_x_unary_op (location_t loc
, enum tree_code code
, cp_expr xarg
,
5785 tsubst_flags_t complain
)
5787 tree orig_expr
= xarg
;
5790 tree overload
= NULL_TREE
;
5792 if (processing_template_decl
)
5794 if (type_dependent_expression_p (xarg
))
5796 tree e
= build_min_nt_loc (loc
, code
, xarg
.get_value (), NULL_TREE
);
5797 maybe_save_operator_binding (e
);
5801 xarg
= build_non_dependent_expr (xarg
);
5806 /* [expr.unary.op] says:
5808 The address of an object of incomplete type can be taken.
5810 (And is just the ordinary address operator, not an overloaded
5811 "operator &".) However, if the type is a template
5812 specialization, we must complete the type at this point so that
5813 an overloaded "operator &" will be available if required. */
5814 if (code
== ADDR_EXPR
5815 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
5816 && ((CLASS_TYPE_P (TREE_TYPE (xarg
))
5817 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg
))))
5818 || (TREE_CODE (xarg
) == OFFSET_REF
)))
5819 /* Don't look for a function. */;
5821 exp
= build_new_op (loc
, code
, LOOKUP_NORMAL
, xarg
, NULL_TREE
,
5822 NULL_TREE
, &overload
, complain
);
5824 if (!exp
&& code
== ADDR_EXPR
)
5826 if (is_overloaded_fn (xarg
))
5828 tree fn
= get_first_fn (xarg
);
5829 if (DECL_CONSTRUCTOR_P (fn
) || DECL_DESTRUCTOR_P (fn
))
5831 if (complain
& tf_error
)
5832 error (DECL_CONSTRUCTOR_P (fn
)
5833 ? G_("taking address of constructor %qD")
5834 : G_("taking address of destructor %qD"),
5836 return error_mark_node
;
5840 /* A pointer to member-function can be formed only by saying
5842 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
5843 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
5845 if (TREE_CODE (xarg
) != OFFSET_REF
5846 || !TYPE_P (TREE_OPERAND (xarg
, 0)))
5848 if (complain
& tf_error
)
5850 error ("invalid use of %qE to form a "
5851 "pointer-to-member-function", xarg
.get_value ());
5852 if (TREE_CODE (xarg
) != OFFSET_REF
)
5853 inform (input_location
, " a qualified-id is required");
5855 return error_mark_node
;
5859 if (complain
& tf_error
)
5860 error ("parentheses around %qE cannot be used to form a"
5861 " pointer-to-member-function",
5864 return error_mark_node
;
5865 PTRMEM_OK_P (xarg
) = 1;
5869 if (TREE_CODE (xarg
) == OFFSET_REF
)
5871 ptrmem
= PTRMEM_OK_P (xarg
);
5873 if (!ptrmem
&& !flag_ms_extensions
5874 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
5876 /* A single non-static member, make sure we don't allow a
5877 pointer-to-member. */
5878 xarg
= build2 (OFFSET_REF
, TREE_TYPE (xarg
),
5879 TREE_OPERAND (xarg
, 0),
5880 ovl_make (TREE_OPERAND (xarg
, 1)));
5881 PTRMEM_OK_P (xarg
) = ptrmem
;
5885 exp
= cp_build_addr_expr_strict (xarg
, complain
);
5888 if (processing_template_decl
&& exp
!= error_mark_node
)
5890 if (overload
!= NULL_TREE
)
5891 return (build_min_non_dep_op_overload
5892 (code
, exp
, overload
, orig_expr
, integer_zero_node
));
5894 exp
= build_min_non_dep (code
, exp
, orig_expr
,
5895 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE
);
5897 if (TREE_CODE (exp
) == ADDR_EXPR
)
5898 PTRMEM_OK_P (exp
) = ptrmem
;
5902 /* Construct and perhaps optimize a tree representation
5903 for __builtin_addressof operation. ARG specifies the operand. */
5906 cp_build_addressof (location_t loc
, tree arg
, tsubst_flags_t complain
)
5908 tree orig_expr
= arg
;
5910 if (processing_template_decl
)
5912 if (type_dependent_expression_p (arg
))
5913 return build_min_nt_loc (loc
, ADDRESSOF_EXPR
, arg
, NULL_TREE
);
5915 arg
= build_non_dependent_expr (arg
);
5918 tree exp
= cp_build_addr_expr_strict (arg
, complain
);
5920 if (processing_template_decl
&& exp
!= error_mark_node
)
5921 exp
= build_min_non_dep (ADDRESSOF_EXPR
, exp
, orig_expr
, NULL_TREE
);
5925 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5926 constants, where a null value is represented by an INTEGER_CST of
5930 cp_truthvalue_conversion (tree expr
)
5932 tree type
= TREE_TYPE (expr
);
5933 if (TYPE_PTR_OR_PTRMEM_P (type
)
5934 /* Avoid ICE on invalid use of non-static member function. */
5935 || TREE_CODE (expr
) == FUNCTION_DECL
)
5936 return build_binary_op (input_location
, NE_EXPR
, expr
, nullptr_node
, true);
5938 return c_common_truthvalue_conversion (input_location
, expr
);
5941 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
5942 is a low-level function; most callers should use maybe_convert_cond. */
5945 condition_conversion (tree expr
)
5948 t
= perform_implicit_conversion_flags (boolean_type_node
, expr
,
5949 tf_warning_or_error
, LOOKUP_NORMAL
);
5950 if (!processing_template_decl
)
5951 t
= fold_build_cleanup_point_expr (boolean_type_node
, t
);
5955 /* Returns the address of T. This function will fold away
5956 ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
5957 most places should use cp_build_addr_expr instead. */
5960 build_address (tree t
)
5962 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
5963 return error_mark_node
;
5964 gcc_checking_assert (TREE_CODE (t
) != CONSTRUCTOR
5965 || processing_template_decl
);
5966 t
= build_fold_addr_expr_loc (EXPR_LOCATION (t
), t
);
5967 if (TREE_CODE (t
) != ADDR_EXPR
)
5972 /* Return a NOP_EXPR converting EXPR to TYPE. */
5975 build_nop (tree type
, tree expr
)
5977 if (type
== error_mark_node
|| error_operand_p (expr
))
5979 return build1_loc (EXPR_LOCATION (expr
), NOP_EXPR
, type
, expr
);
5982 /* Take the address of ARG, whatever that means under C++ semantics.
5983 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5984 and class rvalues as well.
5986 Nothing should call this function directly; instead, callers should use
5987 cp_build_addr_expr or cp_build_addr_expr_strict. */
5990 cp_build_addr_expr_1 (tree arg
, bool strict_lvalue
, tsubst_flags_t complain
)
5995 if (!arg
|| error_operand_p (arg
))
5996 return error_mark_node
;
5998 arg
= mark_lvalue_use (arg
);
5999 if (error_operand_p (arg
))
6000 return error_mark_node
;
6002 argtype
= lvalue_type (arg
);
6004 gcc_assert (!(identifier_p (arg
) && IDENTIFIER_ANY_OP_P (arg
)));
6006 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
6007 && !really_overloaded_fn (arg
))
6009 /* They're trying to take the address of a unique non-static
6010 member function. This is ill-formed (except in MS-land),
6011 but let's try to DTRT.
6012 Note: We only handle unique functions here because we don't
6013 want to complain if there's a static overload; non-unique
6014 cases will be handled by instantiate_type. But we need to
6015 handle this case here to allow casts on the resulting PMF.
6016 We could defer this in non-MS mode, but it's easier to give
6017 a useful error here. */
6019 /* Inside constant member functions, the `this' pointer
6020 contains an extra const qualifier. TYPE_MAIN_VARIANT
6021 is used here to remove this const from the diagnostics
6022 and the created OFFSET_REF. */
6023 tree base
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg
, 0)));
6024 tree fn
= get_first_fn (TREE_OPERAND (arg
, 1));
6025 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
6026 return error_mark_node
;
6028 if (! flag_ms_extensions
)
6030 tree name
= DECL_NAME (fn
);
6031 if (!(complain
& tf_error
))
6032 return error_mark_node
;
6033 else if (current_class_type
6034 && TREE_OPERAND (arg
, 0) == current_class_ref
)
6035 /* An expression like &memfn. */
6036 permerror (input_location
, "ISO C++ forbids taking the address of an unqualified"
6037 " or parenthesized non-static member function to form"
6038 " a pointer to member function. Say %<&%T::%D%>",
6041 permerror (input_location
, "ISO C++ forbids taking the address of a bound member"
6042 " function to form a pointer to member function."
6046 arg
= build_offset_ref (base
, fn
, /*address_p=*/true, complain
);
6049 /* Uninstantiated types are all functions. Taking the
6050 address of a function is a no-op, so just return the
6052 if (type_unknown_p (arg
))
6053 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
6055 if (TREE_CODE (arg
) == OFFSET_REF
)
6056 /* We want a pointer to member; bypass all the code for actually taking
6057 the address of something. */
6060 /* Anything not already handled and not a true memory reference
6062 if (!FUNC_OR_METHOD_TYPE_P (argtype
))
6064 cp_lvalue_kind kind
= lvalue_kind (arg
);
6065 if (kind
== clk_none
)
6067 if (complain
& tf_error
)
6068 lvalue_error (input_location
, lv_addressof
);
6069 return error_mark_node
;
6071 if (strict_lvalue
&& (kind
& (clk_rvalueref
|clk_class
)))
6073 if (!(complain
& tf_error
))
6074 return error_mark_node
;
6075 /* Make this a permerror because we used to accept it. */
6076 permerror (input_location
, "taking address of rvalue");
6080 if (TYPE_REF_P (argtype
))
6082 tree type
= build_pointer_type (TREE_TYPE (argtype
));
6083 arg
= build1 (CONVERT_EXPR
, type
, arg
);
6086 else if (pedantic
&& DECL_MAIN_P (arg
))
6089 /* Apparently a lot of autoconf scripts for C++ packages do this,
6090 so only complain if -Wpedantic. */
6091 if (complain
& (flag_pedantic_errors
? tf_error
: tf_warning
))
6092 pedwarn (input_location
, OPT_Wpedantic
,
6093 "ISO C++ forbids taking address of function %<::main%>");
6094 else if (flag_pedantic_errors
)
6095 return error_mark_node
;
6098 /* Let &* cancel out to simplify resulting code. */
6099 if (INDIRECT_REF_P (arg
))
6101 arg
= TREE_OPERAND (arg
, 0);
6102 if (TYPE_REF_P (TREE_TYPE (arg
)))
6104 tree type
= build_pointer_type (TREE_TYPE (TREE_TYPE (arg
)));
6105 arg
= build1 (CONVERT_EXPR
, type
, arg
);
6108 /* Don't let this be an lvalue. */
6113 /* Handle complex lvalues (when permitted)
6114 by reduction to simpler cases. */
6115 val
= unary_complex_lvalue (ADDR_EXPR
, arg
);
6119 switch (TREE_CODE (arg
))
6123 case FIX_TRUNC_EXPR
:
6124 /* We should have handled this above in the lvalue_kind check. */
6129 arg
= BASELINK_FUNCTIONS (arg
);
6133 arg
= OVL_FIRST (arg
);
6138 /* Turn a reference to a non-static data member into a
6139 pointer-to-member. */
6144 gcc_assert (PTRMEM_OK_P (arg
));
6146 t
= TREE_OPERAND (arg
, 1);
6147 if (TYPE_REF_P (TREE_TYPE (t
)))
6149 if (complain
& tf_error
)
6150 error ("cannot create pointer to reference member %qD", t
);
6151 return error_mark_node
;
6154 type
= build_ptrmem_type (context_for_name_lookup (t
),
6156 t
= make_ptrmem_cst (type
, TREE_OPERAND (arg
, 1));
6164 if (argtype
!= error_mark_node
)
6165 argtype
= build_pointer_type (argtype
);
6167 if (bitfield_p (arg
))
6169 if (complain
& tf_error
)
6170 error ("attempt to take address of bit-field");
6171 return error_mark_node
;
6174 /* In a template, we are processing a non-dependent expression
6175 so we can just form an ADDR_EXPR with the correct type. */
6176 if (processing_template_decl
|| TREE_CODE (arg
) != COMPONENT_REF
)
6178 tree stripped_arg
= tree_strip_any_location_wrapper (arg
);
6179 if (TREE_CODE (stripped_arg
) == FUNCTION_DECL
6180 && !mark_used (stripped_arg
, complain
) && !(complain
& tf_error
))
6181 return error_mark_node
;
6182 val
= build_address (arg
);
6183 if (TREE_CODE (arg
) == OFFSET_REF
)
6184 PTRMEM_OK_P (val
) = PTRMEM_OK_P (arg
);
6186 else if (BASELINK_P (TREE_OPERAND (arg
, 1)))
6188 tree fn
= BASELINK_FUNCTIONS (TREE_OPERAND (arg
, 1));
6190 /* We can only get here with a single static member
6192 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
6193 && DECL_STATIC_FUNCTION_P (fn
));
6194 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
6195 return error_mark_node
;
6196 val
= build_address (fn
);
6197 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
6198 /* Do not lose object's side effects. */
6199 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
),
6200 TREE_OPERAND (arg
, 0), val
);
6204 tree object
= TREE_OPERAND (arg
, 0);
6205 tree field
= TREE_OPERAND (arg
, 1);
6206 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6207 (TREE_TYPE (object
), decl_type_context (field
)));
6208 val
= build_address (arg
);
6211 if (TYPE_PTR_P (argtype
)
6212 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
6214 build_ptrmemfunc_type (argtype
);
6215 val
= build_ptrmemfunc (argtype
, val
, 0,
6223 /* Take the address of ARG if it has one, even if it's an rvalue. */
6226 cp_build_addr_expr (tree arg
, tsubst_flags_t complain
)
6228 return cp_build_addr_expr_1 (arg
, 0, complain
);
6231 /* Take the address of ARG, but only if it's an lvalue. */
6234 cp_build_addr_expr_strict (tree arg
, tsubst_flags_t complain
)
6236 return cp_build_addr_expr_1 (arg
, 1, complain
);
6239 /* C++: Must handle pointers to members.
6241 Perhaps type instantiation should be extended to handle conversion
6242 from aggregates to types we don't yet know we want? (Or are those
6243 cases typically errors which should be reported?)
6245 NOCONVERT suppresses the default promotions (such as from short to int). */
6248 cp_build_unary_op (enum tree_code code
, tree xarg
, bool noconvert
,
6249 tsubst_flags_t complain
)
6251 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
6253 location_t location
= cp_expr_loc_or_input_loc (arg
);
6255 const char *errstring
= NULL
;
6257 const char *invalid_op_diag
;
6259 if (!arg
|| error_operand_p (arg
))
6260 return error_mark_node
;
6262 arg
= resolve_nondeduced_context (arg
, complain
);
6264 if ((invalid_op_diag
6265 = targetm
.invalid_unary_op ((code
== UNARY_PLUS_EXPR
6270 if (complain
& tf_error
)
6271 error (invalid_op_diag
);
6272 return error_mark_node
;
6277 case UNARY_PLUS_EXPR
:
6280 int flags
= WANT_ARITH
| WANT_ENUM
;
6281 /* Unary plus (but not unary minus) is allowed on pointers. */
6282 if (code
== UNARY_PLUS_EXPR
)
6283 flags
|= WANT_POINTER
;
6284 arg
= build_expr_type_conversion (flags
, arg
, true);
6286 errstring
= (code
== NEGATE_EXPR
6287 ? _("wrong type argument to unary minus")
6288 : _("wrong type argument to unary plus"));
6291 if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
6292 arg
= cp_perform_integral_promotions (arg
, complain
);
6294 /* Make sure the result is not an lvalue: a unary plus or minus
6295 expression is always a rvalue. */
6302 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
6307 arg
= cp_default_conversion (arg
, complain
);
6308 if (arg
== error_mark_node
)
6309 return error_mark_node
;
6312 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
6313 | WANT_VECTOR_OR_COMPLEX
,
6315 errstring
= _("wrong type argument to bit-complement");
6316 else if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
6318 /* Warn if the expression has boolean value. */
6319 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
6320 && (complain
& tf_warning
)
6321 && warning_at (location
, OPT_Wbool_operation
,
6322 "%<~%> on an expression of type %<bool%>"))
6323 inform (location
, "did you mean to use logical not (%<!%>)?");
6324 arg
= cp_perform_integral_promotions (arg
, complain
);
6326 else if (!noconvert
&& VECTOR_TYPE_P (TREE_TYPE (arg
)))
6327 arg
= mark_rvalue_use (arg
);
6331 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
6332 errstring
= _("wrong type argument to abs");
6333 else if (!noconvert
)
6335 arg
= cp_default_conversion (arg
, complain
);
6336 if (arg
== error_mark_node
)
6337 return error_mark_node
;
6342 /* Conjugating a real value is a no-op, but allow it anyway. */
6343 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
6344 errstring
= _("wrong type argument to conjugation");
6345 else if (!noconvert
)
6347 arg
= cp_default_conversion (arg
, complain
);
6348 if (arg
== error_mark_node
)
6349 return error_mark_node
;
6353 case TRUTH_NOT_EXPR
:
6354 if (VECTOR_TYPE_P (TREE_TYPE (arg
)))
6355 return cp_build_binary_op (input_location
, EQ_EXPR
, arg
,
6356 build_zero_cst (TREE_TYPE (arg
)), complain
);
6357 arg
= perform_implicit_conversion (boolean_type_node
, arg
,
6359 val
= invert_truthvalue_loc (input_location
, arg
);
6360 if (arg
!= error_mark_node
)
6362 errstring
= _("in argument to unary !");
6370 arg
= build_real_imag_expr (input_location
, code
, arg
);
6373 case PREINCREMENT_EXPR
:
6374 case POSTINCREMENT_EXPR
:
6375 case PREDECREMENT_EXPR
:
6376 case POSTDECREMENT_EXPR
:
6377 /* Handle complex lvalues (when permitted)
6378 by reduction to simpler cases. */
6380 val
= unary_complex_lvalue (code
, arg
);
6384 arg
= mark_lvalue_use (arg
);
6386 /* Increment or decrement the real part of the value,
6387 and don't change the imaginary part. */
6388 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
6392 arg
= cp_stabilize_reference (arg
);
6393 real
= cp_build_unary_op (REALPART_EXPR
, arg
, true, complain
);
6394 imag
= cp_build_unary_op (IMAGPART_EXPR
, arg
, true, complain
);
6395 real
= cp_build_unary_op (code
, real
, true, complain
);
6396 if (real
== error_mark_node
|| imag
== error_mark_node
)
6397 return error_mark_node
;
6398 return build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
6402 /* Report invalid types. */
6404 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
6407 if (code
== PREINCREMENT_EXPR
)
6408 errstring
= _("no pre-increment operator for type");
6409 else if (code
== POSTINCREMENT_EXPR
)
6410 errstring
= _("no post-increment operator for type");
6411 else if (code
== PREDECREMENT_EXPR
)
6412 errstring
= _("no pre-decrement operator for type");
6414 errstring
= _("no post-decrement operator for type");
6417 else if (arg
== error_mark_node
)
6418 return error_mark_node
;
6420 /* Report something read-only. */
6422 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
6423 || TREE_READONLY (arg
))
6425 if (complain
& tf_error
)
6426 cxx_readonly_error (location
, arg
,
6427 ((code
== PREINCREMENT_EXPR
6428 || code
== POSTINCREMENT_EXPR
)
6429 ? lv_increment
: lv_decrement
));
6431 return error_mark_node
;
6436 tree declared_type
= unlowered_expr_type (arg
);
6438 argtype
= TREE_TYPE (arg
);
6440 /* ARM $5.2.5 last annotation says this should be forbidden. */
6441 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
6443 if (complain
& tf_error
)
6444 permerror (input_location
, (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
6445 ? G_("ISO C++ forbids incrementing an enum")
6446 : G_("ISO C++ forbids decrementing an enum"));
6448 return error_mark_node
;
6451 /* Compute the increment. */
6453 if (TYPE_PTR_P (argtype
))
6455 tree type
= complete_type (TREE_TYPE (argtype
));
6457 if (!COMPLETE_OR_VOID_TYPE_P (type
))
6459 if (complain
& tf_error
)
6460 error (((code
== PREINCREMENT_EXPR
6461 || code
== POSTINCREMENT_EXPR
))
6462 ? G_("cannot increment a pointer to incomplete type %qT")
6463 : G_("cannot decrement a pointer to incomplete type %qT"),
6464 TREE_TYPE (argtype
));
6466 return error_mark_node
;
6468 else if (!TYPE_PTROB_P (argtype
))
6470 if (complain
& tf_error
)
6471 pedwarn (input_location
, OPT_Wpointer_arith
,
6472 (code
== PREINCREMENT_EXPR
6473 || code
== POSTINCREMENT_EXPR
)
6474 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6475 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6478 return error_mark_node
;
6481 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
6484 inc
= VECTOR_TYPE_P (argtype
)
6485 ? build_one_cst (argtype
)
6488 inc
= cp_convert (argtype
, inc
, complain
);
6490 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6491 need to ask Objective-C to build the increment or decrement
6492 expression for it. */
6493 if (objc_is_property_ref (arg
))
6494 return objc_build_incr_expr_for_property_ref (input_location
, code
,
6497 /* Complain about anything else that is not a true lvalue. */
6498 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
6499 || code
== POSTINCREMENT_EXPR
)
6500 ? lv_increment
: lv_decrement
),
6502 return error_mark_node
;
6504 /* [depr.volatile.type] "Postfix ++ and -- expressions and
6505 prefix ++ and -- expressions of volatile-qualified arithmetic
6506 and pointer types are deprecated." */
6507 if (TREE_THIS_VOLATILE (arg
) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg
)))
6508 warning_at (location
, OPT_Wvolatile
,
6509 "%qs expression of %<volatile%>-qualified type is "
6511 ((code
== PREINCREMENT_EXPR
6512 || code
== POSTINCREMENT_EXPR
)
6515 /* Forbid using -- or ++ in C++17 on `bool'. */
6516 if (TREE_CODE (declared_type
) == BOOLEAN_TYPE
)
6518 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
6520 if (complain
& tf_error
)
6521 error ("use of an operand of type %qT in %<operator--%> "
6522 "is forbidden", boolean_type_node
);
6523 return error_mark_node
;
6527 if (cxx_dialect
>= cxx17
)
6529 if (complain
& tf_error
)
6530 error ("use of an operand of type %qT in "
6531 "%<operator++%> is forbidden in C++17",
6533 return error_mark_node
;
6535 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6537 warning (OPT_Wdeprecated
, "use of an operand of type %qT "
6538 "in %<operator++%> is deprecated",
6541 val
= boolean_increment (code
, arg
);
6543 else if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
6544 /* An rvalue has no cv-qualifiers. */
6545 val
= build2 (code
, cv_unqualified (TREE_TYPE (arg
)), arg
, inc
);
6547 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
6549 TREE_SIDE_EFFECTS (val
) = 1;
6554 /* Note that this operation never does default_conversion
6555 regardless of NOCONVERT. */
6556 return cp_build_addr_expr (arg
, complain
);
6565 argtype
= TREE_TYPE (arg
);
6566 return build1 (code
, argtype
, arg
);
6569 if (complain
& tf_error
)
6570 error ("%s", errstring
);
6571 return error_mark_node
;
6574 /* Hook for the c-common bits that build a unary op. */
6576 build_unary_op (location_t
/*location*/,
6577 enum tree_code code
, tree xarg
, bool noconvert
)
6579 return cp_build_unary_op (code
, xarg
, noconvert
, tf_warning_or_error
);
6582 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
6583 so that it is a valid lvalue even for GENERIC by replacing
6584 (lhs = rhs) with ((lhs = rhs), lhs)
6585 (--lhs) with ((--lhs), lhs)
6586 (++lhs) with ((++lhs), lhs)
6587 and if lhs has side-effects, calling cp_stabilize_reference on it, so
6588 that it can be evaluated multiple times. */
6591 genericize_compound_lvalue (tree lvalue
)
6593 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue
, 0)))
6594 lvalue
= build2 (TREE_CODE (lvalue
), TREE_TYPE (lvalue
),
6595 cp_stabilize_reference (TREE_OPERAND (lvalue
, 0)),
6596 TREE_OPERAND (lvalue
, 1));
6597 return build2 (COMPOUND_EXPR
, TREE_TYPE (TREE_OPERAND (lvalue
, 0)),
6598 lvalue
, TREE_OPERAND (lvalue
, 0));
6601 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6602 for certain kinds of expressions which are not really lvalues
6603 but which we can accept as lvalues.
6605 If ARG is not a kind of expression we can handle, return
6609 unary_complex_lvalue (enum tree_code code
, tree arg
)
6611 /* Inside a template, making these kinds of adjustments is
6612 pointless; we are only concerned with the type of the
6614 if (processing_template_decl
)
6617 /* Handle (a, b) used as an "lvalue". */
6618 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
6620 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 1), false,
6621 tf_warning_or_error
);
6622 return build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6623 TREE_OPERAND (arg
, 0), real_result
);
6626 /* Handle (a ? b : c) used as an "lvalue". */
6627 if (TREE_CODE (arg
) == COND_EXPR
6628 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
6629 return rationalize_conditional_expr (code
, arg
, tf_warning_or_error
);
6631 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6632 if (TREE_CODE (arg
) == MODIFY_EXPR
6633 || TREE_CODE (arg
) == PREINCREMENT_EXPR
6634 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
6635 return unary_complex_lvalue (code
, genericize_compound_lvalue (arg
));
6637 if (code
!= ADDR_EXPR
)
6640 /* Handle (a = b) used as an "lvalue" for `&'. */
6641 if (TREE_CODE (arg
) == MODIFY_EXPR
6642 || TREE_CODE (arg
) == INIT_EXPR
)
6644 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 0), false,
6645 tf_warning_or_error
);
6646 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6648 TREE_NO_WARNING (arg
) = 1;
6652 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg
))
6653 || TREE_CODE (arg
) == OFFSET_REF
)
6656 /* We permit compiler to make function calls returning
6657 objects of aggregate type look like lvalues. */
6661 if (TREE_CODE (targ
) == SAVE_EXPR
)
6662 targ
= TREE_OPERAND (targ
, 0);
6664 if (TREE_CODE (targ
) == CALL_EXPR
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (targ
)))
6666 if (TREE_CODE (arg
) == SAVE_EXPR
)
6669 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, tf_warning_or_error
);
6670 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
6673 if (TREE_CODE (arg
) == SAVE_EXPR
&& INDIRECT_REF_P (targ
))
6674 return build3 (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
6675 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
6678 /* Don't let anything else be handled specially. */
6682 /* Mark EXP saying that we need to be able to take the
6683 address of it; it should not be allocated in a register.
6684 Value is true if successful. ARRAY_REF_P is true if this
6685 is for ARRAY_REF construction - in that case we don't want
6686 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6687 it is fine to use ARRAY_REFs for vector subscripts on vector
6690 C++: we do not allow `current_class_ptr' to be addressable. */
6693 cxx_mark_addressable (tree exp
, bool array_ref_p
)
6698 switch (TREE_CODE (x
))
6700 case VIEW_CONVERT_EXPR
:
6702 && TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
6703 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x
, 0))))
6711 x
= TREE_OPERAND (x
, 0);
6715 if (x
== current_class_ptr
)
6717 error ("cannot take the address of %<this%>, which is an rvalue expression");
6718 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later. */
6724 /* Caller should not be trying to mark initialized
6725 constant fields addressable. */
6726 gcc_assert (DECL_LANG_SPECIFIC (x
) == 0
6727 || DECL_IN_AGGR_P (x
) == 0
6729 || DECL_EXTERNAL (x
));
6733 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
6734 && !DECL_ARTIFICIAL (x
))
6736 if (VAR_P (x
) && DECL_HARD_REGISTER (x
))
6739 ("address of explicit register variable %qD requested", x
);
6742 else if (extra_warnings
)
6744 (OPT_Wextra
, "address requested for %qD, which is declared %<register%>", x
);
6746 TREE_ADDRESSABLE (x
) = 1;
6751 TREE_ADDRESSABLE (x
) = 1;
6755 TREE_ADDRESSABLE (x
) = 1;
6759 TREE_ADDRESSABLE (x
) = 1;
6760 cxx_mark_addressable (TREE_OPERAND (x
, 0));
6768 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6771 build_x_conditional_expr (location_t loc
, tree ifexp
, tree op1
, tree op2
,
6772 tsubst_flags_t complain
)
6774 tree orig_ifexp
= ifexp
;
6775 tree orig_op1
= op1
;
6776 tree orig_op2
= op2
;
6779 if (processing_template_decl
)
6781 /* The standard says that the expression is type-dependent if
6782 IFEXP is type-dependent, even though the eventual type of the
6783 expression doesn't dependent on IFEXP. */
6784 if (type_dependent_expression_p (ifexp
)
6785 /* As a GNU extension, the middle operand may be omitted. */
6786 || (op1
&& type_dependent_expression_p (op1
))
6787 || type_dependent_expression_p (op2
))
6788 return build_min_nt_loc (loc
, COND_EXPR
, ifexp
, op1
, op2
);
6789 ifexp
= build_non_dependent_expr (ifexp
);
6791 op1
= build_non_dependent_expr (op1
);
6792 op2
= build_non_dependent_expr (op2
);
6795 expr
= build_conditional_expr (loc
, ifexp
, op1
, op2
, complain
);
6796 if (processing_template_decl
&& expr
!= error_mark_node
)
6798 tree min
= build_min_non_dep (COND_EXPR
, expr
,
6799 orig_ifexp
, orig_op1
, orig_op2
);
6800 expr
= convert_from_reference (min
);
6805 /* Given a list of expressions, return a compound expression
6806 that performs them all and returns the value of the last of them. */
6809 build_x_compound_expr_from_list (tree list
, expr_list_kind exp
,
6810 tsubst_flags_t complain
)
6812 tree expr
= TREE_VALUE (list
);
6814 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
6815 && !CONSTRUCTOR_IS_DIRECT_INIT (expr
))
6817 if (complain
& tf_error
)
6818 pedwarn (cp_expr_loc_or_input_loc (expr
), 0,
6819 "list-initializer for non-class type must not "
6820 "be parenthesized");
6822 return error_mark_node
;
6825 if (TREE_CHAIN (list
))
6827 if (complain
& tf_error
)
6831 permerror (input_location
, "expression list treated as compound "
6832 "expression in initializer");
6835 permerror (input_location
, "expression list treated as compound "
6836 "expression in mem-initializer");
6839 permerror (input_location
, "expression list treated as compound "
6840 "expression in functional cast");
6846 return error_mark_node
;
6848 for (list
= TREE_CHAIN (list
); list
; list
= TREE_CHAIN (list
))
6849 expr
= build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list
)),
6850 expr
, TREE_VALUE (list
), complain
);
6856 /* Like build_x_compound_expr_from_list, but using a VEC. */
6859 build_x_compound_expr_from_vec (vec
<tree
, va_gc
> *vec
, const char *msg
,
6860 tsubst_flags_t complain
)
6862 if (vec_safe_is_empty (vec
))
6864 else if (vec
->length () == 1)
6874 if (complain
& tf_error
)
6875 permerror (input_location
,
6876 "%s expression list treated as compound expression",
6879 return error_mark_node
;
6883 for (ix
= 1; vec
->iterate (ix
, &t
); ++ix
)
6884 expr
= build_x_compound_expr (EXPR_LOCATION (t
), expr
,
6891 /* Handle overloading of the ',' operator when needed. */
6894 build_x_compound_expr (location_t loc
, tree op1
, tree op2
,
6895 tsubst_flags_t complain
)
6898 tree orig_op1
= op1
;
6899 tree orig_op2
= op2
;
6900 tree overload
= NULL_TREE
;
6902 if (processing_template_decl
)
6904 if (type_dependent_expression_p (op1
)
6905 || type_dependent_expression_p (op2
))
6906 return build_min_nt_loc (loc
, COMPOUND_EXPR
, op1
, op2
);
6907 op1
= build_non_dependent_expr (op1
);
6908 op2
= build_non_dependent_expr (op2
);
6911 result
= build_new_op (loc
, COMPOUND_EXPR
, LOOKUP_NORMAL
, op1
, op2
,
6912 NULL_TREE
, &overload
, complain
);
6914 result
= cp_build_compound_expr (op1
, op2
, complain
);
6916 if (processing_template_decl
&& result
!= error_mark_node
)
6918 if (overload
!= NULL_TREE
)
6919 return (build_min_non_dep_op_overload
6920 (COMPOUND_EXPR
, result
, overload
, orig_op1
, orig_op2
));
6922 return build_min_non_dep (COMPOUND_EXPR
, result
, orig_op1
, orig_op2
);
6928 /* Like cp_build_compound_expr, but for the c-common bits. */
6931 build_compound_expr (location_t
/*loc*/, tree lhs
, tree rhs
)
6933 return cp_build_compound_expr (lhs
, rhs
, tf_warning_or_error
);
6936 /* Build a compound expression. */
6939 cp_build_compound_expr (tree lhs
, tree rhs
, tsubst_flags_t complain
)
6941 lhs
= convert_to_void (lhs
, ICV_LEFT_OF_COMMA
, complain
);
6943 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
6944 return error_mark_node
;
6946 if (TREE_CODE (rhs
) == TARGET_EXPR
)
6948 /* If the rhs is a TARGET_EXPR, then build the compound
6949 expression inside the target_expr's initializer. This
6950 helps the compiler to eliminate unnecessary temporaries. */
6951 tree init
= TREE_OPERAND (rhs
, 1);
6953 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), lhs
, init
);
6954 TREE_OPERAND (rhs
, 1) = init
;
6959 if (type_unknown_p (rhs
))
6961 if (complain
& tf_error
)
6962 error ("no context to resolve type of %qE", rhs
);
6963 return error_mark_node
;
6966 return build2 (COMPOUND_EXPR
, TREE_TYPE (rhs
), lhs
, rhs
);
6969 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6970 casts away constness. CAST gives the type of cast. Returns true
6971 if the cast is ill-formed, false if it is well-formed.
6973 ??? This function warns for casting away any qualifier not just
6974 const. We would like to specify exactly what qualifiers are casted
6979 check_for_casting_away_constness (tree src_type
, tree dest_type
,
6980 enum tree_code cast
, tsubst_flags_t complain
)
6982 /* C-style casts are allowed to cast away constness. With
6983 WARN_CAST_QUAL, we still want to issue a warning. */
6984 if (cast
== CAST_EXPR
&& !warn_cast_qual
)
6987 if (!casts_away_constness (src_type
, dest_type
, complain
))
6993 if (complain
& tf_warning
)
6994 warning (OPT_Wcast_qual
,
6995 "cast from type %qT to type %qT casts away qualifiers",
6996 src_type
, dest_type
);
6999 case STATIC_CAST_EXPR
:
7000 if (complain
& tf_error
)
7001 error ("%<static_cast%> from type %qT to type %qT casts away "
7003 src_type
, dest_type
);
7006 case REINTERPRET_CAST_EXPR
:
7007 if (complain
& tf_error
)
7008 error ("%<reinterpret_cast%> from type %qT to type %qT casts away "
7010 src_type
, dest_type
);
7018 /* Warns if the cast from expression EXPR to type TYPE is useless. */
7020 maybe_warn_about_useless_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7022 if (warn_useless_cast
7023 && complain
& tf_warning
)
7025 if ((TYPE_REF_P (type
)
7026 && (TYPE_REF_IS_RVALUE (type
)
7027 ? xvalue_p (expr
) : lvalue_p (expr
))
7028 && same_type_p (TREE_TYPE (expr
), TREE_TYPE (type
)))
7029 || same_type_p (TREE_TYPE (expr
), type
))
7030 warning (OPT_Wuseless_cast
, "useless cast to type %q#T", type
);
7034 /* Warns if the cast ignores cv-qualifiers on TYPE. */
7036 maybe_warn_about_cast_ignoring_quals (tree type
, tsubst_flags_t complain
)
7038 if (warn_ignored_qualifiers
7039 && complain
& tf_warning
7040 && !CLASS_TYPE_P (type
)
7041 && (cp_type_quals (type
) & (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)))
7043 warning (OPT_Wignored_qualifiers
, "type qualifiers ignored on cast "
7048 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
7049 (another pointer-to-member type in the same hierarchy) and return
7050 the converted expression. If ALLOW_INVERSE_P is permitted, a
7051 pointer-to-derived may be converted to pointer-to-base; otherwise,
7052 only the other direction is permitted. If C_CAST_P is true, this
7053 conversion is taking place as part of a C-style cast. */
7056 convert_ptrmem (tree type
, tree expr
, bool allow_inverse_p
,
7057 bool c_cast_p
, tsubst_flags_t complain
)
7059 if (same_type_p (type
, TREE_TYPE (expr
)))
7062 if (TYPE_PTRDATAMEM_P (type
))
7064 tree obase
= TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr
));
7065 tree nbase
= TYPE_PTRMEM_CLASS_TYPE (type
);
7066 tree delta
= (get_delta_difference
7068 allow_inverse_p
, c_cast_p
, complain
));
7070 if (delta
== error_mark_node
)
7071 return error_mark_node
;
7073 if (!same_type_p (obase
, nbase
))
7075 if (TREE_CODE (expr
) == PTRMEM_CST
)
7076 expr
= cplus_expand_constant (expr
);
7078 tree cond
= cp_build_binary_op (input_location
, EQ_EXPR
, expr
,
7079 build_int_cst (TREE_TYPE (expr
), -1),
7081 tree op1
= build_nop (ptrdiff_type_node
, expr
);
7082 tree op2
= cp_build_binary_op (input_location
, PLUS_EXPR
, op1
, delta
,
7085 expr
= fold_build3_loc (input_location
,
7086 COND_EXPR
, ptrdiff_type_node
, cond
, op1
, op2
);
7089 return build_nop (type
, expr
);
7092 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
,
7093 allow_inverse_p
, c_cast_p
, complain
);
7096 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
7097 this static_cast is being attempted as one of the possible casts
7098 allowed by a C-style cast. (In that case, accessibility of base
7099 classes is not considered, and it is OK to cast away
7100 constness.) Return the result of the cast. *VALID_P is set to
7101 indicate whether or not the cast was valid. */
7104 build_static_cast_1 (tree type
, tree expr
, bool c_cast_p
,
7105 bool *valid_p
, tsubst_flags_t complain
)
7111 /* Assume the cast is valid. */
7114 intype
= unlowered_expr_type (expr
);
7116 /* Save casted types in the function's used types hash table. */
7117 used_types_insert (type
);
7119 /* A prvalue of non-class type is cv-unqualified. */
7120 if (!CLASS_TYPE_P (type
))
7121 type
= cv_unqualified (type
);
7123 /* [expr.static.cast]
7125 An lvalue of type "cv1 B", where B is a class type, can be cast
7126 to type "reference to cv2 D", where D is a class derived (clause
7127 _class.derived_) from B, if a valid standard conversion from
7128 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
7129 same cv-qualification as, or greater cv-qualification than, cv1,
7130 and B is not a virtual base class of D. */
7131 /* We check this case before checking the validity of "TYPE t =
7132 EXPR;" below because for this case:
7135 struct D : public B { D(const B&); };
7137 void f() { static_cast<const D&>(b); }
7139 we want to avoid constructing a new D. The standard is not
7140 completely clear about this issue, but our interpretation is
7141 consistent with other compilers. */
7142 if (TYPE_REF_P (type
)
7143 && CLASS_TYPE_P (TREE_TYPE (type
))
7144 && CLASS_TYPE_P (intype
)
7145 && (TYPE_REF_IS_RVALUE (type
) || lvalue_p (expr
))
7146 && DERIVED_FROM_P (intype
, TREE_TYPE (type
))
7147 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype
)),
7148 build_pointer_type (TYPE_MAIN_VARIANT
7149 (TREE_TYPE (type
))),
7152 || at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
7156 if (processing_template_decl
)
7159 /* There is a standard conversion from "D*" to "B*" even if "B"
7160 is ambiguous or inaccessible. If this is really a
7161 static_cast, then we check both for inaccessibility and
7162 ambiguity. However, if this is a static_cast being performed
7163 because the user wrote a C-style cast, then accessibility is
7165 base
= lookup_base (TREE_TYPE (type
), intype
,
7166 c_cast_p
? ba_unique
: ba_check
,
7168 expr
= cp_build_addr_expr (expr
, complain
);
7170 if (sanitize_flags_p (SANITIZE_VPTR
))
7173 = cp_ubsan_maybe_instrument_downcast (input_location
, type
,
7179 /* Convert from "B*" to "D*". This function will check that "B"
7180 is not a virtual base of "D". Even if we don't have a guarantee
7181 that expr is NULL, if the static_cast is to a reference type,
7182 it is UB if it would be NULL, so omit the non-NULL check. */
7183 expr
= build_base_path (MINUS_EXPR
, expr
, base
,
7184 /*nonnull=*/flag_delete_null_pointer_checks
,
7187 /* Convert the pointer to a reference -- but then remember that
7188 there are no expressions with reference type in C++.
7190 We call rvalue so that there's an actual tree code
7191 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
7192 is a variable with the same type, the conversion would get folded
7193 away, leaving just the variable and causing lvalue_kind to give
7194 the wrong answer. */
7195 expr
= cp_fold_convert (type
, expr
);
7197 /* When -fsanitize=null, make sure to diagnose reference binding to
7198 NULL even when the reference is converted to pointer later on. */
7199 if (sanitize_flags_p (SANITIZE_NULL
)
7200 && TREE_CODE (expr
) == COND_EXPR
7201 && TREE_OPERAND (expr
, 2)
7202 && TREE_CODE (TREE_OPERAND (expr
, 2)) == INTEGER_CST
7203 && TREE_TYPE (TREE_OPERAND (expr
, 2)) == type
)
7204 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr
, 2));
7206 return convert_from_reference (rvalue (expr
));
7209 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
7210 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
7211 if (TYPE_REF_P (type
)
7212 && TYPE_REF_IS_RVALUE (type
)
7213 && (clk
= real_lvalue_p (expr
))
7214 && reference_related_p (TREE_TYPE (type
), intype
)
7215 && (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
7217 if (processing_template_decl
)
7219 if (clk
== clk_ordinary
)
7221 /* Handle the (non-bit-field) lvalue case here by casting to
7222 lvalue reference and then changing it to an rvalue reference.
7223 Casting an xvalue to rvalue reference will be handled by the
7225 tree lref
= cp_build_reference_type (TREE_TYPE (type
), false);
7226 result
= (perform_direct_initialization_if_possible
7227 (lref
, expr
, c_cast_p
, complain
));
7228 result
= build1 (NON_LVALUE_EXPR
, type
, result
);
7229 return convert_from_reference (result
);
7232 /* For a bit-field or packed field, bind to a temporary. */
7233 expr
= rvalue (expr
);
7236 /* Resolve overloaded address here rather than once in
7237 implicit_conversion and again in the inverse code below. */
7238 if (TYPE_PTRMEMFUNC_P (type
) && type_unknown_p (expr
))
7240 expr
= instantiate_type (type
, expr
, complain
);
7241 intype
= TREE_TYPE (expr
);
7244 /* [expr.static.cast]
7246 Any expression can be explicitly converted to type cv void. */
7247 if (VOID_TYPE_P (type
))
7248 return convert_to_void (expr
, ICV_CAST
, complain
);
7251 An abstract class shall not be used ... as the type of an explicit
7253 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
7254 return error_mark_node
;
7256 /* [expr.static.cast]
7258 An expression e can be explicitly converted to a type T using a
7259 static_cast of the form static_cast<T>(e) if the declaration T
7260 t(e);" is well-formed, for some invented temporary variable
7262 result
= perform_direct_initialization_if_possible (type
, expr
,
7263 c_cast_p
, complain
);
7266 if (processing_template_decl
)
7269 result
= convert_from_reference (result
);
7271 /* [expr.static.cast]
7273 If T is a reference type, the result is an lvalue; otherwise,
7274 the result is an rvalue. */
7275 if (!TYPE_REF_P (type
))
7277 result
= rvalue (result
);
7279 if (result
== expr
&& SCALAR_TYPE_P (type
))
7280 /* Leave some record of the cast. */
7281 result
= build_nop (type
, expr
);
7286 /* [expr.static.cast]
7288 The inverse of any standard conversion sequence (clause _conv_),
7289 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
7290 (_conv.array_), function-to-pointer (_conv.func_), and boolean
7291 (_conv.bool_) conversions, can be performed explicitly using
7292 static_cast subject to the restriction that the explicit
7293 conversion does not cast away constness (_expr.const.cast_), and
7294 the following additional rules for specific cases: */
7295 /* For reference, the conversions not excluded are: integral
7296 promotions, floating-point promotion, integral conversions,
7297 floating-point conversions, floating-integral conversions,
7298 pointer conversions, and pointer to member conversions. */
7301 A value of integral _or enumeration_ type can be explicitly
7302 converted to an enumeration type. */
7303 /* The effect of all that is that any conversion between any two
7304 types which are integral, floating, or enumeration types can be
7306 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7307 || SCALAR_FLOAT_TYPE_P (type
))
7308 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
7309 || SCALAR_FLOAT_TYPE_P (intype
)))
7311 if (processing_template_decl
)
7313 return ocp_convert (type
, expr
, CONV_C_CAST
, LOOKUP_NORMAL
, complain
);
7316 if (TYPE_PTR_P (type
) && TYPE_PTR_P (intype
)
7317 && CLASS_TYPE_P (TREE_TYPE (type
))
7318 && CLASS_TYPE_P (TREE_TYPE (intype
))
7319 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7320 (TREE_TYPE (intype
))),
7321 build_pointer_type (TYPE_MAIN_VARIANT
7322 (TREE_TYPE (type
))),
7327 if (processing_template_decl
)
7331 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
7333 return error_mark_node
;
7334 base
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
7335 c_cast_p
? ba_unique
: ba_check
,
7337 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
7340 if (sanitize_flags_p (SANITIZE_VPTR
))
7343 = cp_ubsan_maybe_instrument_downcast (input_location
, type
,
7349 return cp_fold_convert (type
, expr
);
7352 if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
7353 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
7360 c1
= TYPE_PTRMEM_CLASS_TYPE (intype
);
7361 c2
= TYPE_PTRMEM_CLASS_TYPE (type
);
7363 if (TYPE_PTRDATAMEM_P (type
))
7365 t1
= (build_ptrmem_type
7367 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype
))));
7368 t2
= (build_ptrmem_type
7370 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
7377 if (can_convert (t1
, t2
, complain
) || can_convert (t2
, t1
, complain
))
7380 && check_for_casting_away_constness (intype
, type
,
7383 return error_mark_node
;
7384 if (processing_template_decl
)
7386 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/1,
7387 c_cast_p
, complain
);
7391 /* [expr.static.cast]
7393 An rvalue of type "pointer to cv void" can be explicitly
7394 converted to a pointer to object type. A value of type pointer
7395 to object converted to "pointer to cv void" and back to the
7396 original pointer type will have its original value. */
7397 if (TYPE_PTR_P (intype
)
7398 && VOID_TYPE_P (TREE_TYPE (intype
))
7399 && TYPE_PTROB_P (type
))
7402 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
7404 return error_mark_node
;
7405 if (processing_template_decl
)
7407 return build_nop (type
, expr
);
7411 return error_mark_node
;
7414 /* Return an expression representing static_cast<TYPE>(EXPR). */
7417 build_static_cast (tree type
, tree oexpr
, tsubst_flags_t complain
)
7423 if (type
== error_mark_node
|| expr
== error_mark_node
)
7424 return error_mark_node
;
7426 bool dependent
= (dependent_type_p (type
)
7427 || type_dependent_expression_p (expr
));
7431 expr
= build_min (STATIC_CAST_EXPR
, type
, oexpr
);
7432 /* We don't know if it will or will not have side effects. */
7433 TREE_SIDE_EFFECTS (expr
) = 1;
7434 return convert_from_reference (expr
);
7436 else if (processing_template_decl
)
7437 expr
= build_non_dependent_expr (expr
);
7439 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7440 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7441 if (!TYPE_REF_P (type
)
7442 && TREE_CODE (expr
) == NOP_EXPR
7443 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7444 expr
= TREE_OPERAND (expr
, 0);
7446 result
= build_static_cast_1 (type
, expr
, /*c_cast_p=*/false, &valid_p
,
7450 if (result
!= error_mark_node
)
7452 maybe_warn_about_useless_cast (type
, expr
, complain
);
7453 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7455 if (processing_template_decl
)
7460 if (complain
& tf_error
)
7462 error ("invalid %<static_cast%> from type %qT to type %qT",
7463 TREE_TYPE (expr
), type
);
7464 if ((TYPE_PTR_P (type
) || TYPE_REF_P (type
))
7465 && CLASS_TYPE_P (TREE_TYPE (type
))
7466 && !COMPLETE_TYPE_P (TREE_TYPE (type
)))
7467 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type
))),
7468 "class type %qT is incomplete", TREE_TYPE (type
));
7469 tree expr_type
= TREE_TYPE (expr
);
7470 if (TYPE_PTR_P (expr_type
))
7471 expr_type
= TREE_TYPE (expr_type
);
7472 if (CLASS_TYPE_P (expr_type
) && !COMPLETE_TYPE_P (expr_type
))
7473 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type
)),
7474 "class type %qT is incomplete", expr_type
);
7476 return error_mark_node
;
7479 /* EXPR is an expression with member function or pointer-to-member
7480 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7481 not permitted by ISO C++, but we accept it in some modes. If we
7482 are not in one of those modes, issue a diagnostic. Return the
7483 converted expression. */
7486 convert_member_func_to_ptr (tree type
, tree expr
, tsubst_flags_t complain
)
7491 intype
= TREE_TYPE (expr
);
7492 gcc_assert (TYPE_PTRMEMFUNC_P (intype
)
7493 || TREE_CODE (intype
) == METHOD_TYPE
);
7495 if (!(complain
& tf_warning_or_error
))
7496 return error_mark_node
;
7498 if (pedantic
|| warn_pmf2ptr
)
7499 pedwarn (input_location
, pedantic
? OPT_Wpedantic
: OPT_Wpmf_conversions
,
7500 "converting from %qH to %qI", intype
, type
);
7502 if (TREE_CODE (intype
) == METHOD_TYPE
)
7503 expr
= build_addr_func (expr
, complain
);
7504 else if (TREE_CODE (expr
) == PTRMEM_CST
)
7505 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
7508 decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
), 0);
7509 decl
= build_address (decl
);
7510 expr
= get_member_function_from_ptrfunc (&decl
, expr
, complain
);
7513 if (expr
== error_mark_node
)
7514 return error_mark_node
;
7516 return build_nop (type
, expr
);
7519 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
7520 constexpr evaluation knows to reject it. */
7523 build_nop_reinterpret (tree type
, tree expr
)
7525 tree ret
= build_nop (type
, expr
);
7527 REINTERPRET_CAST_P (ret
) = true;
7531 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7532 If C_CAST_P is true, this reinterpret cast is being done as part of
7533 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7534 indicate whether or not reinterpret_cast was valid. */
7537 build_reinterpret_cast_1 (tree type
, tree expr
, bool c_cast_p
,
7538 bool *valid_p
, tsubst_flags_t complain
)
7542 /* Assume the cast is invalid. */
7546 if (type
== error_mark_node
|| error_operand_p (expr
))
7547 return error_mark_node
;
7549 intype
= TREE_TYPE (expr
);
7551 /* Save casted types in the function's used types hash table. */
7552 used_types_insert (type
);
7554 /* A prvalue of non-class type is cv-unqualified. */
7555 if (!CLASS_TYPE_P (type
))
7556 type
= cv_unqualified (type
);
7558 /* [expr.reinterpret.cast]
7559 A glvalue expression of type T1 can be cast to the type
7560 "reference to T2" if an expression of type "pointer to T1" can be
7561 explicitly converted to the type "pointer to T2" using a
7562 reinterpret_cast. */
7563 if (TYPE_REF_P (type
))
7565 if (TYPE_REF_IS_RVALUE (type
) && !VOID_TYPE_P (intype
))
7567 if (!obvalue_p (expr
))
7568 /* Perform the temporary materialization conversion. */
7569 expr
= get_target_expr_sfinae (expr
, complain
);
7571 else if (!lvalue_p (expr
))
7573 if (complain
& tf_error
)
7574 error ("invalid cast of an rvalue expression of type "
7577 return error_mark_node
;
7580 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7581 "B" are related class types; the reinterpret_cast does not
7582 adjust the pointer. */
7583 if (TYPE_PTR_P (intype
)
7584 && (complain
& tf_warning
)
7585 && (comptypes (TREE_TYPE (intype
), TREE_TYPE (type
),
7586 COMPARE_BASE
| COMPARE_DERIVED
)))
7587 warning (0, "casting %qT to %qT does not dereference pointer",
7590 expr
= cp_build_addr_expr (expr
, complain
);
7592 if (warn_strict_aliasing
> 2)
7593 strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
7595 if (expr
!= error_mark_node
)
7596 expr
= build_reinterpret_cast_1
7597 (build_pointer_type (TREE_TYPE (type
)), expr
, c_cast_p
,
7599 if (expr
!= error_mark_node
)
7600 /* cp_build_indirect_ref isn't right for rvalue refs. */
7601 expr
= convert_from_reference (fold_convert (type
, expr
));
7605 /* As a G++ extension, we consider conversions from member
7606 functions, and pointers to member functions to
7607 pointer-to-function and pointer-to-void types. If
7608 -Wno-pmf-conversions has not been specified,
7609 convert_member_func_to_ptr will issue an error message. */
7610 if ((TYPE_PTRMEMFUNC_P (intype
)
7611 || TREE_CODE (intype
) == METHOD_TYPE
)
7612 && TYPE_PTR_P (type
)
7613 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
7614 || VOID_TYPE_P (TREE_TYPE (type
))))
7615 return convert_member_func_to_ptr (type
, expr
, complain
);
7617 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7618 array-to-pointer, and function-to-pointer conversions are
7620 expr
= decay_conversion (expr
, complain
);
7622 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7623 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7624 if (TREE_CODE (expr
) == NOP_EXPR
7625 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7626 expr
= TREE_OPERAND (expr
, 0);
7628 if (error_operand_p (expr
))
7629 return error_mark_node
;
7631 intype
= TREE_TYPE (expr
);
7633 /* [expr.reinterpret.cast]
7634 A pointer can be converted to any integral type large enough to
7635 hold it. ... A value of type std::nullptr_t can be converted to
7636 an integral type; the conversion has the same meaning and
7637 validity as a conversion of (void*)0 to the integral type. */
7638 if (CP_INTEGRAL_TYPE_P (type
)
7639 && (TYPE_PTR_P (intype
) || NULLPTR_TYPE_P (intype
)))
7641 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
7643 if (complain
& tf_error
)
7644 permerror (input_location
, "cast from %qH to %qI loses precision",
7647 return error_mark_node
;
7649 if (NULLPTR_TYPE_P (intype
))
7650 return build_int_cst (type
, 0);
7652 /* [expr.reinterpret.cast]
7653 A value of integral or enumeration type can be explicitly
7654 converted to a pointer. */
7655 else if (TYPE_PTR_P (type
) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype
))
7658 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7659 || TYPE_PTR_OR_PTRMEM_P (type
))
7660 && same_type_p (type
, intype
))
7662 return rvalue (expr
);
7663 else if (TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
7665 if ((complain
& tf_warning
)
7666 && !cxx_safe_function_type_cast_p (TREE_TYPE (type
),
7667 TREE_TYPE (intype
)))
7668 warning (OPT_Wcast_function_type
,
7669 "cast between incompatible function types"
7670 " from %qH to %qI", intype
, type
);
7671 return build_nop_reinterpret (type
, expr
);
7673 else if (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
))
7675 if ((complain
& tf_warning
)
7676 && !cxx_safe_function_type_cast_p
7677 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type
)),
7678 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype
))))
7679 warning (OPT_Wcast_function_type
,
7680 "cast between incompatible pointer to member types"
7681 " from %qH to %qI", intype
, type
);
7682 return build_nop_reinterpret (type
, expr
);
7684 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
7685 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
7688 && check_for_casting_away_constness (intype
, type
,
7689 REINTERPRET_CAST_EXPR
,
7691 return error_mark_node
;
7692 /* Warn about possible alignment problems. */
7693 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
7694 && (complain
& tf_warning
)
7695 && !VOID_TYPE_P (type
)
7696 && TREE_CODE (TREE_TYPE (intype
)) != FUNCTION_TYPE
7697 && COMPLETE_TYPE_P (TREE_TYPE (type
))
7698 && COMPLETE_TYPE_P (TREE_TYPE (intype
))
7699 && min_align_of_type (TREE_TYPE (type
))
7700 > min_align_of_type (TREE_TYPE (intype
)))
7701 warning (OPT_Wcast_align
, "cast from %qH to %qI "
7702 "increases required alignment of target type", intype
, type
);
7704 if (warn_strict_aliasing
<= 2)
7705 /* strict_aliasing_warning STRIP_NOPs its expr. */
7706 strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
7708 return build_nop_reinterpret (type
, expr
);
7710 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
7711 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
7713 if (complain
& tf_warning
)
7714 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7715 object pointer type or vice versa is conditionally-supported." */
7716 warning (OPT_Wconditionally_supported
,
7717 "casting between pointer-to-function and pointer-to-object "
7718 "is conditionally-supported");
7719 return build_nop_reinterpret (type
, expr
);
7721 else if (VECTOR_TYPE_P (type
))
7722 return convert_to_vector (type
, expr
);
7723 else if (VECTOR_TYPE_P (intype
)
7724 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
7725 return convert_to_integer_nofold (type
, expr
);
7730 if (complain
& tf_error
)
7731 error ("invalid cast from type %qT to type %qT", intype
, type
);
7732 return error_mark_node
;
7735 expr
= cp_convert (type
, expr
, complain
);
7736 if (TREE_CODE (expr
) == NOP_EXPR
)
7737 /* Mark any nop_expr that created as a reintepret_cast. */
7738 REINTERPRET_CAST_P (expr
) = true;
7743 build_reinterpret_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7747 if (type
== error_mark_node
|| expr
== error_mark_node
)
7748 return error_mark_node
;
7750 if (processing_template_decl
)
7752 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
7754 if (!TREE_SIDE_EFFECTS (t
)
7755 && type_dependent_expression_p (expr
))
7756 /* There might turn out to be side effects inside expr. */
7757 TREE_SIDE_EFFECTS (t
) = 1;
7758 return convert_from_reference (t
);
7761 r
= build_reinterpret_cast_1 (type
, expr
, /*c_cast_p=*/false,
7762 /*valid_p=*/NULL
, complain
);
7763 if (r
!= error_mark_node
)
7765 maybe_warn_about_useless_cast (type
, expr
, complain
);
7766 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7771 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7772 return an appropriate expression. Otherwise, return
7773 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7774 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7775 performing a C-style cast, its value upon return will indicate
7776 whether or not the conversion succeeded. */
7779 build_const_cast_1 (tree dst_type
, tree expr
, tsubst_flags_t complain
,
7783 tree reference_type
;
7785 /* Callers are responsible for handling error_mark_node as a
7786 destination type. */
7787 gcc_assert (dst_type
!= error_mark_node
);
7788 /* In a template, callers should be building syntactic
7789 representations of casts, not using this machinery. */
7790 gcc_assert (!processing_template_decl
);
7792 /* Assume the conversion is invalid. */
7796 if (!INDIRECT_TYPE_P (dst_type
) && !TYPE_PTRDATAMEM_P (dst_type
))
7798 if (complain
& tf_error
)
7799 error ("invalid use of %<const_cast%> with type %qT, "
7800 "which is not a pointer, "
7801 "reference, nor a pointer-to-data-member type", dst_type
);
7802 return error_mark_node
;
7805 if (TREE_CODE (TREE_TYPE (dst_type
)) == FUNCTION_TYPE
)
7807 if (complain
& tf_error
)
7808 error ("invalid use of %<const_cast%> with type %qT, "
7809 "which is a pointer or reference to a function type",
7811 return error_mark_node
;
7814 /* A prvalue of non-class type is cv-unqualified. */
7815 dst_type
= cv_unqualified (dst_type
);
7817 /* Save casted types in the function's used types hash table. */
7818 used_types_insert (dst_type
);
7820 src_type
= TREE_TYPE (expr
);
7821 /* Expressions do not really have reference types. */
7822 if (TYPE_REF_P (src_type
))
7823 src_type
= TREE_TYPE (src_type
);
7825 /* [expr.const.cast]
7827 For two object types T1 and T2, if a pointer to T1 can be explicitly
7828 converted to the type "pointer to T2" using a const_cast, then the
7829 following conversions can also be made:
7831 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7832 type T2 using the cast const_cast<T2&>;
7834 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7835 type T2 using the cast const_cast<T2&&>; and
7837 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7838 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7840 if (TYPE_REF_P (dst_type
))
7842 reference_type
= dst_type
;
7843 if (!TYPE_REF_IS_RVALUE (dst_type
)
7849 if (complain
& tf_error
)
7850 error ("invalid %<const_cast%> of an rvalue of type %qT "
7852 src_type
, dst_type
);
7853 return error_mark_node
;
7855 dst_type
= build_pointer_type (TREE_TYPE (dst_type
));
7856 src_type
= build_pointer_type (src_type
);
7860 reference_type
= NULL_TREE
;
7861 /* If the destination type is not a reference type, the
7862 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7863 conversions are performed. */
7864 src_type
= type_decays_to (src_type
);
7865 if (src_type
== error_mark_node
)
7866 return error_mark_node
;
7869 if (TYPE_PTR_P (src_type
) || TYPE_PTRDATAMEM_P (src_type
))
7871 if (comp_ptr_ttypes_const (dst_type
, src_type
, bounds_none
))
7876 /* This cast is actually a C-style cast. Issue a warning if
7877 the user is making a potentially unsafe cast. */
7878 check_for_casting_away_constness (src_type
, dst_type
,
7879 CAST_EXPR
, complain
);
7880 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
7881 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
7882 && (complain
& tf_warning
)
7883 && min_align_of_type (TREE_TYPE (dst_type
))
7884 > min_align_of_type (TREE_TYPE (src_type
)))
7885 warning (OPT_Wcast_align
, "cast from %qH to %qI "
7886 "increases required alignment of target type",
7887 src_type
, dst_type
);
7891 expr
= cp_build_addr_expr (expr
, complain
);
7892 if (expr
== error_mark_node
)
7893 return error_mark_node
;
7894 expr
= build_nop (reference_type
, expr
);
7895 return convert_from_reference (expr
);
7899 expr
= decay_conversion (expr
, complain
);
7900 if (expr
== error_mark_node
)
7901 return error_mark_node
;
7903 /* build_c_cast puts on a NOP_EXPR to make the result not an
7904 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7905 non-lvalue context. */
7906 if (TREE_CODE (expr
) == NOP_EXPR
7907 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7908 expr
= TREE_OPERAND (expr
, 0);
7909 return build_nop (dst_type
, expr
);
7913 && !at_least_as_qualified_p (TREE_TYPE (dst_type
),
7914 TREE_TYPE (src_type
)))
7915 check_for_casting_away_constness (src_type
, dst_type
, CAST_EXPR
,
7919 if (complain
& tf_error
)
7920 error ("invalid %<const_cast%> from type %qT to type %qT",
7921 src_type
, dst_type
);
7922 return error_mark_node
;
7926 build_const_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7930 if (type
== error_mark_node
|| error_operand_p (expr
))
7931 return error_mark_node
;
7933 if (processing_template_decl
)
7935 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
7937 if (!TREE_SIDE_EFFECTS (t
)
7938 && type_dependent_expression_p (expr
))
7939 /* There might turn out to be side effects inside expr. */
7940 TREE_SIDE_EFFECTS (t
) = 1;
7941 return convert_from_reference (t
);
7944 r
= build_const_cast_1 (type
, expr
, complain
, /*valid_p=*/NULL
);
7945 if (r
!= error_mark_node
)
7947 maybe_warn_about_useless_cast (type
, expr
, complain
);
7948 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7953 /* Like cp_build_c_cast, but for the c-common bits. */
7956 build_c_cast (location_t
/*loc*/, tree type
, tree expr
)
7958 return cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7961 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7962 preserve location information even for tree nodes that don't
7966 build_c_cast (location_t loc
, tree type
, cp_expr expr
)
7968 cp_expr result
= cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7969 result
.set_location (loc
);
7973 /* Build an expression representing an explicit C-style cast to type
7974 TYPE of expression EXPR. */
7977 cp_build_c_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7983 if (type
== error_mark_node
|| error_operand_p (expr
))
7984 return error_mark_node
;
7986 if (processing_template_decl
)
7988 tree t
= build_min (CAST_EXPR
, type
,
7989 tree_cons (NULL_TREE
, value
, NULL_TREE
));
7990 /* We don't know if it will or will not have side effects. */
7991 TREE_SIDE_EFFECTS (t
) = 1;
7992 return convert_from_reference (t
);
7995 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7996 'Class') should always be retained, because this information aids
7997 in method lookup. */
7998 if (objc_is_object_ptr (type
)
7999 && objc_is_object_ptr (TREE_TYPE (expr
)))
8000 return build_nop (type
, expr
);
8002 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8003 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8004 if (!TYPE_REF_P (type
)
8005 && TREE_CODE (value
) == NOP_EXPR
8006 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
8007 value
= TREE_OPERAND (value
, 0);
8009 if (TREE_CODE (type
) == ARRAY_TYPE
)
8011 /* Allow casting from T1* to T2[] because Cfront allows it.
8012 NIHCL uses it. It is not valid ISO C++ however. */
8013 if (TYPE_PTR_P (TREE_TYPE (expr
)))
8015 if (complain
& tf_error
)
8016 permerror (input_location
, "ISO C++ forbids casting to an array type %qT", type
);
8018 return error_mark_node
;
8019 type
= build_pointer_type (TREE_TYPE (type
));
8023 if (complain
& tf_error
)
8024 error ("ISO C++ forbids casting to an array type %qT", type
);
8025 return error_mark_node
;
8029 if (FUNC_OR_METHOD_TYPE_P (type
))
8031 if (complain
& tf_error
)
8032 error ("invalid cast to function type %qT", type
);
8033 return error_mark_node
;
8036 if (TYPE_PTR_P (type
)
8037 && TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
8038 /* Casting to an integer of smaller size is an error detected elsewhere. */
8039 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (value
))
8040 /* Don't warn about converting any constant. */
8041 && !TREE_CONSTANT (value
))
8042 warning_at (input_location
, OPT_Wint_to_pointer_cast
,
8043 "cast to pointer from integer of different size");
8045 /* A C-style cast can be a const_cast. */
8046 result
= build_const_cast_1 (type
, value
, complain
& tf_warning
,
8050 if (result
!= error_mark_node
)
8052 maybe_warn_about_useless_cast (type
, value
, complain
);
8053 maybe_warn_about_cast_ignoring_quals (type
, complain
);
8058 /* Or a static cast. */
8059 result
= build_static_cast_1 (type
, value
, /*c_cast_p=*/true,
8060 &valid_p
, complain
);
8061 /* Or a reinterpret_cast. */
8063 result
= build_reinterpret_cast_1 (type
, value
, /*c_cast_p=*/true,
8064 &valid_p
, complain
);
8065 /* The static_cast or reinterpret_cast may be followed by a
8068 /* A valid cast may result in errors if, for example, a
8069 conversion to an ambiguous base class is required. */
8070 && !error_operand_p (result
))
8074 maybe_warn_about_useless_cast (type
, value
, complain
);
8075 maybe_warn_about_cast_ignoring_quals (type
, complain
);
8077 /* Non-class rvalues always have cv-unqualified type. */
8078 if (!CLASS_TYPE_P (type
))
8079 type
= TYPE_MAIN_VARIANT (type
);
8080 result_type
= TREE_TYPE (result
);
8081 if (!CLASS_TYPE_P (result_type
) && !TYPE_REF_P (type
))
8082 result_type
= TYPE_MAIN_VARIANT (result_type
);
8083 /* If the type of RESULT does not match TYPE, perform a
8084 const_cast to make it match. If the static_cast or
8085 reinterpret_cast succeeded, we will differ by at most
8086 cv-qualification, so the follow-on const_cast is guaranteed
8088 if (!same_type_p (non_reference (type
), non_reference (result_type
)))
8090 result
= build_const_cast_1 (type
, result
, false, &valid_p
);
8091 gcc_assert (valid_p
);
8096 return error_mark_node
;
8099 /* For use from the C common bits. */
8101 build_modify_expr (location_t location
,
8102 tree lhs
, tree
/*lhs_origtype*/,
8103 enum tree_code modifycode
,
8104 location_t
/*rhs_location*/, tree rhs
,
8105 tree
/*rhs_origtype*/)
8107 return cp_build_modify_expr (location
, lhs
, modifycode
, rhs
,
8108 tf_warning_or_error
);
8111 /* Build an assignment expression of lvalue LHS from value RHS.
8112 MODIFYCODE is the code for a binary operator that we use
8113 to combine the old value of LHS with RHS to get the new value.
8114 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
8116 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
8119 cp_build_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
8120 tree rhs
, tsubst_flags_t complain
)
8122 lhs
= mark_lvalue_use_nonread (lhs
);
8124 tree result
= NULL_TREE
;
8126 tree lhstype
= TREE_TYPE (lhs
);
8128 tree olhstype
= lhstype
;
8129 bool plain_assign
= (modifycode
== NOP_EXPR
);
8130 bool compound_side_effects_p
= false;
8131 tree preeval
= NULL_TREE
;
8133 /* Avoid duplicate error messages from operands that had errors. */
8134 if (error_operand_p (lhs
) || error_operand_p (rhs
))
8135 return error_mark_node
;
8137 while (TREE_CODE (lhs
) == COMPOUND_EXPR
)
8139 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
8140 compound_side_effects_p
= true;
8141 lhs
= TREE_OPERAND (lhs
, 1);
8144 /* Handle control structure constructs used as "lvalues". Note that we
8145 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
8146 switch (TREE_CODE (lhs
))
8148 /* Handle --foo = 5; as these are valid constructs in C++. */
8149 case PREDECREMENT_EXPR
:
8150 case PREINCREMENT_EXPR
:
8151 if (compound_side_effects_p
)
8152 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
8153 lhs
= genericize_compound_lvalue (lhs
);
8155 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
8156 and looked through the COMPOUND_EXPRs, readd them now around
8157 the resulting lhs. */
8158 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
8160 lhs
= build2 (COMPOUND_EXPR
, lhstype
, TREE_OPERAND (olhs
, 0), lhs
);
8161 tree
*ptr
= &TREE_OPERAND (lhs
, 1);
8162 for (olhs
= TREE_OPERAND (olhs
, 1);
8163 TREE_CODE (olhs
) == COMPOUND_EXPR
;
8164 olhs
= TREE_OPERAND (olhs
, 1))
8166 *ptr
= build2 (COMPOUND_EXPR
, lhstype
,
8167 TREE_OPERAND (olhs
, 0), *ptr
);
8168 ptr
= &TREE_OPERAND (*ptr
, 1);
8174 if (compound_side_effects_p
)
8175 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
8176 lhs
= genericize_compound_lvalue (lhs
);
8177 goto maybe_add_compound
;
8181 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
8182 when neither operand has side-effects. */
8183 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8184 return error_mark_node
;
8186 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0))
8187 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 1)));
8189 lhs
= build3 (COND_EXPR
, TREE_TYPE (lhs
),
8190 build2 (TREE_CODE (lhs
) == MIN_EXPR
? LE_EXPR
: GE_EXPR
,
8192 TREE_OPERAND (lhs
, 0),
8193 TREE_OPERAND (lhs
, 1)),
8194 TREE_OPERAND (lhs
, 0),
8195 TREE_OPERAND (lhs
, 1));
8198 /* Handle (a ? b : c) used as an "lvalue". */
8201 /* Produce (a ? (b = rhs) : (c = rhs))
8202 except that the RHS goes through a save-expr
8203 so the code to compute it is only emitted once. */
8204 if (VOID_TYPE_P (TREE_TYPE (rhs
)))
8206 if (complain
& tf_error
)
8207 error ("void value not ignored as it ought to be");
8208 return error_mark_node
;
8211 rhs
= stabilize_expr (rhs
, &preeval
);
8213 /* Check this here to avoid odd errors when trying to convert
8214 a throw to the type of the COND_EXPR. */
8215 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8216 return error_mark_node
;
8218 tree op1
= TREE_OPERAND (lhs
, 1);
8219 if (TREE_CODE (op1
) != THROW_EXPR
)
8220 op1
= cp_build_modify_expr (loc
, op1
, modifycode
, rhs
, complain
);
8221 /* When sanitizing undefined behavior, even when rhs doesn't need
8222 stabilization at this point, the sanitization might add extra
8223 SAVE_EXPRs in there and so make sure there is no tree sharing
8224 in the rhs, otherwise those SAVE_EXPRs will have initialization
8225 only in one of the two branches. */
8226 if (sanitize_flags_p (SANITIZE_UNDEFINED
8227 | SANITIZE_UNDEFINED_NONDEFAULT
))
8228 rhs
= unshare_expr (rhs
);
8229 tree op2
= TREE_OPERAND (lhs
, 2);
8230 if (TREE_CODE (op2
) != THROW_EXPR
)
8231 op2
= cp_build_modify_expr (loc
, op2
, modifycode
, rhs
, complain
);
8232 tree cond
= build_conditional_expr (input_location
,
8233 TREE_OPERAND (lhs
, 0), op1
, op2
,
8236 if (cond
== error_mark_node
)
8238 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
8239 and looked through the COMPOUND_EXPRs, readd them now around
8240 the resulting cond before adding the preevaluated rhs. */
8241 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
8243 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
8244 TREE_OPERAND (olhs
, 0), cond
);
8245 tree
*ptr
= &TREE_OPERAND (cond
, 1);
8246 for (olhs
= TREE_OPERAND (olhs
, 1);
8247 TREE_CODE (olhs
) == COMPOUND_EXPR
;
8248 olhs
= TREE_OPERAND (olhs
, 1))
8250 *ptr
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
8251 TREE_OPERAND (olhs
, 0), *ptr
);
8252 ptr
= &TREE_OPERAND (*ptr
, 1);
8255 /* Make sure the code to compute the rhs comes out
8256 before the split. */
8266 if (modifycode
== INIT_EXPR
)
8268 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
8269 /* Do the default thing. */;
8270 else if (TREE_CODE (rhs
) == CONSTRUCTOR
)
8272 /* Compound literal. */
8273 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
8274 /* Call convert to generate an error; see PR 11063. */
8275 rhs
= convert (lhstype
, rhs
);
8276 result
= build2 (INIT_EXPR
, lhstype
, lhs
, rhs
);
8277 TREE_SIDE_EFFECTS (result
) = 1;
8280 else if (! MAYBE_CLASS_TYPE_P (lhstype
))
8281 /* Do the default thing. */;
8284 releasing_vec rhs_vec
= make_tree_vector_single (rhs
);
8285 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
8286 &rhs_vec
, lhstype
, LOOKUP_NORMAL
,
8288 if (result
== NULL_TREE
)
8289 return error_mark_node
;
8295 lhs
= require_complete_type_sfinae (lhs
, complain
);
8296 if (lhs
== error_mark_node
)
8297 return error_mark_node
;
8299 if (modifycode
== NOP_EXPR
)
8301 if (c_dialect_objc ())
8303 result
= objc_maybe_build_modify_expr (lhs
, rhs
);
8308 /* `operator=' is not an inheritable operator. */
8309 if (! MAYBE_CLASS_TYPE_P (lhstype
))
8310 /* Do the default thing. */;
8313 result
= build_new_op (input_location
, MODIFY_EXPR
,
8314 LOOKUP_NORMAL
, lhs
, rhs
,
8315 make_node (NOP_EXPR
), /*overload=*/NULL
,
8317 if (result
== NULL_TREE
)
8318 return error_mark_node
;
8325 tree init
= NULL_TREE
;
8327 /* A binary op has been requested. Combine the old LHS
8328 value with the RHS producing the value we should actually
8329 store into the LHS. */
8330 gcc_assert (!((TYPE_REF_P (lhstype
)
8331 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype
)))
8332 || MAYBE_CLASS_TYPE_P (lhstype
)));
8334 /* An expression of the form E1 op= E2. [expr.ass] says:
8335 "Such expressions are deprecated if E1 has volatile-qualified
8336 type." We warn here rather than in cp_genericize_r because
8337 for compound assignments we are supposed to warn even if the
8338 assignment is a discarded-value expression. */
8339 if (TREE_THIS_VOLATILE (lhs
) || CP_TYPE_VOLATILE_P (lhstype
))
8340 warning_at (loc
, OPT_Wvolatile
,
8341 "compound assignment with %<volatile%>-qualified left "
8342 "operand is deprecated");
8343 /* Preevaluate the RHS to make sure its evaluation is complete
8344 before the lvalue-to-rvalue conversion of the LHS:
8346 [expr.ass] With respect to an indeterminately-sequenced
8347 function call, the operation of a compound assignment is a
8348 single evaluation. [ Note: Therefore, a function call shall
8349 not intervene between the lvalue-to-rvalue conversion and the
8350 side effect associated with any single compound assignment
8351 operator. -- end note ] */
8352 lhs
= cp_stabilize_reference (lhs
);
8353 rhs
= decay_conversion (rhs
, complain
);
8354 if (rhs
== error_mark_node
)
8355 return error_mark_node
;
8356 rhs
= stabilize_expr (rhs
, &init
);
8357 newrhs
= cp_build_binary_op (loc
, modifycode
, lhs
, rhs
, complain
);
8358 if (newrhs
== error_mark_node
)
8360 if (complain
& tf_error
)
8361 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode
,
8362 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
8363 return error_mark_node
;
8367 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), init
, newrhs
);
8369 /* Now it looks like a plain assignment. */
8370 modifycode
= NOP_EXPR
;
8371 if (c_dialect_objc ())
8373 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
8378 gcc_assert (!TYPE_REF_P (lhstype
));
8379 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs
)));
8382 /* The left-hand side must be an lvalue. */
8383 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8384 return error_mark_node
;
8386 /* Warn about modifying something that is `const'. Don't warn if
8387 this is initialization. */
8388 if (modifycode
!= INIT_EXPR
8389 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
8390 /* Functions are not modifiable, even though they are
8392 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs
))
8393 /* If it's an aggregate and any field is const, then it is
8394 effectively const. */
8395 || (CLASS_TYPE_P (lhstype
)
8396 && C_TYPE_FIELDS_READONLY (lhstype
))))
8398 if (complain
& tf_error
)
8399 cxx_readonly_error (loc
, lhs
, lv_assign
);
8400 return error_mark_node
;
8403 /* If storing into a structure or union member, it may have been given a
8404 lowered bitfield type. We need to convert to the declared type first,
8405 so retrieve it now. */
8407 olhstype
= unlowered_expr_type (lhs
);
8409 /* Convert new value to destination type. */
8411 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
8415 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs
))
8417 if (modifycode
!= INIT_EXPR
)
8419 if (complain
& tf_error
)
8420 error ("assigning to an array from an initializer list");
8421 return error_mark_node
;
8423 if (check_array_initializer (lhs
, lhstype
, newrhs
))
8424 return error_mark_node
;
8425 newrhs
= digest_init (lhstype
, newrhs
, complain
);
8426 if (newrhs
== error_mark_node
)
8427 return error_mark_node
;
8430 /* C++11 8.5/17: "If the destination type is an array of characters,
8431 an array of char16_t, an array of char32_t, or an array of wchar_t,
8432 and the initializer is a string literal...". */
8433 else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs
))
8435 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype
)))
8436 && modifycode
== INIT_EXPR
)
8438 newrhs
= digest_init (lhstype
, newrhs
, complain
);
8439 if (newrhs
== error_mark_node
)
8440 return error_mark_node
;
8443 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
8444 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))))
8446 if (complain
& tf_error
)
8447 error ("incompatible types in assignment of %qT to %qT",
8448 TREE_TYPE (rhs
), lhstype
);
8449 return error_mark_node
;
8452 /* Allow array assignment in compiler-generated code. */
8453 else if (!current_function_decl
8454 || !DECL_DEFAULTED_FN (current_function_decl
))
8456 /* This routine is used for both initialization and assignment.
8457 Make sure the diagnostic message differentiates the context. */
8458 if (complain
& tf_error
)
8460 if (modifycode
== INIT_EXPR
)
8461 error ("array used as initializer");
8463 error ("invalid array assignment");
8465 return error_mark_node
;
8468 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
8469 ? 1 + (modifycode
!= INIT_EXPR
): 0;
8470 result
= build_vec_init (lhs
, NULL_TREE
, newrhs
,
8471 /*explicit_value_init_p=*/false,
8472 from_array
, complain
);
8476 if (modifycode
== INIT_EXPR
)
8477 /* Calls with INIT_EXPR are all direct-initialization, so don't set
8478 LOOKUP_ONLYCONVERTING. */
8479 newrhs
= convert_for_initialization (lhs
, olhstype
, newrhs
, LOOKUP_NORMAL
,
8480 ICR_INIT
, NULL_TREE
, 0,
8483 newrhs
= convert_for_assignment (olhstype
, newrhs
, ICR_ASSIGN
,
8484 NULL_TREE
, 0, complain
, LOOKUP_IMPLICIT
);
8486 if (!same_type_p (lhstype
, olhstype
))
8487 newrhs
= cp_convert_and_check (lhstype
, newrhs
, complain
);
8489 if (modifycode
!= INIT_EXPR
)
8491 if (TREE_CODE (newrhs
) == CALL_EXPR
8492 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
8493 newrhs
= build_cplus_new (lhstype
, newrhs
, complain
);
8495 /* Can't initialize directly from a TARGET_EXPR, since that would
8496 cause the lhs to be constructed twice, and possibly result in
8497 accidental self-initialization. So we force the TARGET_EXPR to be
8498 expanded without a target. */
8499 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
8500 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
8501 TREE_OPERAND (newrhs
, 0));
8504 if (newrhs
== error_mark_node
)
8505 return error_mark_node
;
8507 if (c_dialect_objc () && flag_objc_gc
)
8509 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
8515 result
= build2_loc (loc
, modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
8516 lhstype
, lhs
, newrhs
);
8518 TREE_SIDE_EFFECTS (result
) = 1;
8520 TREE_NO_WARNING (result
) = 1;
8524 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), preeval
, result
);
8529 build_x_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
8530 tree rhs
, tsubst_flags_t complain
)
8532 tree orig_lhs
= lhs
;
8533 tree orig_rhs
= rhs
;
8534 tree overload
= NULL_TREE
;
8535 tree op
= build_nt (modifycode
, NULL_TREE
, NULL_TREE
);
8537 if (processing_template_decl
)
8539 if (modifycode
== NOP_EXPR
8540 || type_dependent_expression_p (lhs
)
8541 || type_dependent_expression_p (rhs
))
8542 return build_min_nt_loc (loc
, MODOP_EXPR
, lhs
,
8543 build_min_nt_loc (loc
, modifycode
, NULL_TREE
,
8546 lhs
= build_non_dependent_expr (lhs
);
8547 rhs
= build_non_dependent_expr (rhs
);
8550 if (modifycode
!= NOP_EXPR
)
8552 tree rval
= build_new_op (loc
, MODIFY_EXPR
, LOOKUP_NORMAL
,
8553 lhs
, rhs
, op
, &overload
, complain
);
8556 if (rval
== error_mark_node
)
8558 TREE_NO_WARNING (rval
) = 1;
8559 if (processing_template_decl
)
8561 if (overload
!= NULL_TREE
)
8562 return (build_min_non_dep_op_overload
8563 (MODIFY_EXPR
, rval
, overload
, orig_lhs
, orig_rhs
));
8565 return (build_min_non_dep
8566 (MODOP_EXPR
, rval
, orig_lhs
, op
, orig_rhs
));
8571 return cp_build_modify_expr (loc
, lhs
, modifycode
, rhs
, complain
);
8574 /* Helper function for get_delta_difference which assumes FROM is a base
8575 class of TO. Returns a delta for the conversion of pointer-to-member
8576 of FROM to pointer-to-member of TO. If the conversion is invalid and
8577 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8578 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8579 If C_CAST_P is true, this conversion is taking place as part of a
8583 get_delta_difference_1 (tree from
, tree to
, bool c_cast_p
,
8584 tsubst_flags_t complain
)
8589 binfo
= lookup_base (to
, from
, c_cast_p
? ba_unique
: ba_check
,
8592 if (binfo
== error_mark_node
)
8594 if (!(complain
& tf_error
))
8595 return error_mark_node
;
8597 error (" in pointer to member function conversion");
8598 return size_zero_node
;
8602 if (kind
!= bk_via_virtual
)
8603 return BINFO_OFFSET (binfo
);
8605 /* FROM is a virtual base class of TO. Issue an error or warning
8606 depending on whether or not this is a reinterpret cast. */
8608 if (!(complain
& tf_error
))
8609 return error_mark_node
;
8611 error ("pointer to member conversion via virtual base %qT",
8612 BINFO_TYPE (binfo_from_vbase (binfo
)));
8614 return size_zero_node
;
8621 /* Get difference in deltas for different pointer to member function
8622 types. If the conversion is invalid and tf_error is not set in
8623 COMPLAIN, returns error_mark_node, otherwise returns an integer
8624 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8625 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8626 conversions as well. If C_CAST_P is true this conversion is taking
8627 place as part of a C-style cast.
8629 Note that the naming of FROM and TO is kind of backwards; the return
8630 value is what we add to a TO in order to get a FROM. They are named
8631 this way because we call this function to find out how to convert from
8632 a pointer to member of FROM to a pointer to member of TO. */
8635 get_delta_difference (tree from
, tree to
,
8636 bool allow_inverse_p
,
8637 bool c_cast_p
, tsubst_flags_t complain
)
8641 if (same_type_ignoring_top_level_qualifiers_p (from
, to
))
8642 /* Pointer to member of incomplete class is permitted*/
8643 result
= size_zero_node
;
8645 result
= get_delta_difference_1 (from
, to
, c_cast_p
, complain
);
8647 if (result
== error_mark_node
)
8648 return error_mark_node
;
8652 if (!allow_inverse_p
)
8654 if (!(complain
& tf_error
))
8655 return error_mark_node
;
8657 error_not_base_type (from
, to
);
8658 error (" in pointer to member conversion");
8659 result
= size_zero_node
;
8663 result
= get_delta_difference_1 (to
, from
, c_cast_p
, complain
);
8665 if (result
== error_mark_node
)
8666 return error_mark_node
;
8669 result
= size_diffop_loc (input_location
,
8670 size_zero_node
, result
);
8673 if (!(complain
& tf_error
))
8674 return error_mark_node
;
8676 error_not_base_type (from
, to
);
8677 error (" in pointer to member conversion");
8678 result
= size_zero_node
;
8683 return convert_to_integer (ptrdiff_type_node
, result
);
8686 /* Return a constructor for the pointer-to-member-function TYPE using
8687 the other components as specified. */
8690 build_ptrmemfunc1 (tree type
, tree delta
, tree pfn
)
8695 vec
<constructor_elt
, va_gc
> *v
;
8697 /* Pull the FIELD_DECLs out of the type. */
8698 pfn_field
= TYPE_FIELDS (type
);
8699 delta_field
= DECL_CHAIN (pfn_field
);
8701 /* Make sure DELTA has the type we want. */
8702 delta
= convert_and_check (input_location
, delta_type_node
, delta
);
8704 /* Convert to the correct target type if necessary. */
8705 pfn
= fold_convert (TREE_TYPE (pfn_field
), pfn
);
8707 /* Finish creating the initializer. */
8709 CONSTRUCTOR_APPEND_ELT(v
, pfn_field
, pfn
);
8710 CONSTRUCTOR_APPEND_ELT(v
, delta_field
, delta
);
8711 u
= build_constructor (type
, v
);
8712 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) & TREE_CONSTANT (delta
);
8713 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
8714 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
8716 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
8721 /* Build a constructor for a pointer to member function. It can be
8722 used to initialize global variables, local variable, or used
8723 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8726 If FORCE is nonzero, then force this conversion, even if
8727 we would rather not do it. Usually set when using an explicit
8728 cast. A C-style cast is being processed iff C_CAST_P is true.
8730 Return error_mark_node, if something goes wrong. */
8733 build_ptrmemfunc (tree type
, tree pfn
, int force
, bool c_cast_p
,
8734 tsubst_flags_t complain
)
8740 if (error_operand_p (pfn
))
8741 return error_mark_node
;
8743 pfn_type
= TREE_TYPE (pfn
);
8744 to_type
= build_ptrmemfunc_type (type
);
8746 /* Handle multiple conversions of pointer to member functions. */
8747 if (TYPE_PTRMEMFUNC_P (pfn_type
))
8749 tree delta
= NULL_TREE
;
8750 tree npfn
= NULL_TREE
;
8754 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
,
8755 LOOKUP_NORMAL
, complain
))
8757 if (complain
& tf_error
)
8758 error ("invalid conversion to type %qT from type %qT",
8761 return error_mark_node
;
8764 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
8765 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
8767 c_cast_p
, complain
);
8768 if (n
== error_mark_node
)
8769 return error_mark_node
;
8771 /* We don't have to do any conversion to convert a
8772 pointer-to-member to its own type. But, we don't want to
8773 just return a PTRMEM_CST if there's an explicit cast; that
8774 cast should make the expression an invalid template argument. */
8775 if (TREE_CODE (pfn
) != PTRMEM_CST
)
8777 if (same_type_p (to_type
, pfn_type
))
8779 else if (integer_zerop (n
) && TREE_CODE (pfn
) != CONSTRUCTOR
)
8780 return build_reinterpret_cast (to_type
, pfn
,
8784 if (TREE_SIDE_EFFECTS (pfn
))
8785 pfn
= save_expr (pfn
);
8787 /* Obtain the function pointer and the current DELTA. */
8788 if (TREE_CODE (pfn
) == PTRMEM_CST
)
8789 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
8792 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
8793 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
8796 /* Just adjust the DELTA field. */
8797 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8798 (TREE_TYPE (delta
), ptrdiff_type_node
));
8799 if (!integer_zerop (n
))
8801 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
8802 n
= cp_build_binary_op (input_location
,
8803 LSHIFT_EXPR
, n
, integer_one_node
,
8805 delta
= cp_build_binary_op (input_location
,
8806 PLUS_EXPR
, delta
, n
, complain
);
8808 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
8811 /* Handle null pointer to member function conversions. */
8812 if (null_ptr_cst_p (pfn
))
8814 pfn
= cp_build_c_cast (type
, pfn
, complain
);
8815 return build_ptrmemfunc1 (to_type
,
8820 if (type_unknown_p (pfn
))
8821 return instantiate_type (type
, pfn
, complain
);
8823 fn
= TREE_OPERAND (pfn
, 0);
8824 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
8825 /* In a template, we will have preserved the
8827 || (processing_template_decl
&& TREE_CODE (fn
) == OFFSET_REF
));
8828 return make_ptrmem_cst (to_type
, fn
);
8831 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8834 ??? There is no consistency as to the types returned for the above
8835 values. Some code acts as if it were a sizetype and some as if it were
8836 integer_type_node. */
8839 expand_ptrmemfunc_cst (tree cst
, tree
*delta
, tree
*pfn
)
8841 tree type
= TREE_TYPE (cst
);
8842 tree fn
= PTRMEM_CST_MEMBER (cst
);
8843 tree ptr_class
, fn_class
;
8845 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
8847 /* The class that the function belongs to. */
8848 fn_class
= DECL_CONTEXT (fn
);
8850 /* The class that we're creating a pointer to member of. */
8851 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
8853 /* First, calculate the adjustment to the function's class. */
8854 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0,
8855 /*c_cast_p=*/0, tf_warning_or_error
);
8857 if (!DECL_VIRTUAL_P (fn
))
8858 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
),
8859 build_addr_func (fn
, tf_warning_or_error
));
8862 /* If we're dealing with a virtual function, we have to adjust 'this'
8863 again, to point to the base which provides the vtable entry for
8864 fn; the call will do the opposite adjustment. */
8865 tree orig_class
= DECL_CONTEXT (fn
);
8866 tree binfo
= binfo_or_else (orig_class
, fn_class
);
8867 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
8868 *delta
, BINFO_OFFSET (binfo
));
8870 /* We set PFN to the vtable offset at which the function can be
8871 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8872 case delta is shifted left, and then incremented). */
8873 *pfn
= DECL_VINDEX (fn
);
8874 *pfn
= fold_build2 (MULT_EXPR
, integer_type_node
, *pfn
,
8875 TYPE_SIZE_UNIT (vtable_entry_type
));
8877 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
8879 case ptrmemfunc_vbit_in_pfn
:
8880 *pfn
= fold_build2 (PLUS_EXPR
, integer_type_node
, *pfn
,
8884 case ptrmemfunc_vbit_in_delta
:
8885 *delta
= fold_build2 (LSHIFT_EXPR
, TREE_TYPE (*delta
),
8886 *delta
, integer_one_node
);
8887 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
8888 *delta
, integer_one_node
);
8895 *pfn
= fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), *pfn
);
8899 /* Return an expression for PFN from the pointer-to-member function
8903 pfn_from_ptrmemfunc (tree t
)
8905 if (TREE_CODE (t
) == PTRMEM_CST
)
8910 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
8915 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
8918 /* Return an expression for DELTA from the pointer-to-member function
8922 delta_from_ptrmemfunc (tree t
)
8924 if (TREE_CODE (t
) == PTRMEM_CST
)
8929 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
8934 return build_ptrmemfunc_access_expr (t
, delta_identifier
);
8937 /* Convert value RHS to type TYPE as preparation for an assignment to
8938 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8939 implicit conversion is. If FNDECL is non-NULL, we are doing the
8940 conversion in order to pass the PARMNUMth argument of FNDECL.
8941 If FNDECL is NULL, we are doing the conversion in function pointer
8942 argument passing, conversion in initialization, etc. */
8945 convert_for_assignment (tree type
, tree rhs
,
8946 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
8947 tsubst_flags_t complain
, int flags
)
8950 enum tree_code coder
;
8952 location_t rhs_loc
= EXPR_LOC_OR_LOC (rhs
, input_location
);
8953 bool has_loc
= EXPR_LOCATION (rhs
) != UNKNOWN_LOCATION
;
8954 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
8955 but preserve location wrappers. */
8956 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
8957 && !location_wrapper_p (rhs
))
8958 rhs
= TREE_OPERAND (rhs
, 0);
8960 /* Handle [dcl.init.list] direct-list-initialization from
8961 single element of enumeration with a fixed underlying type. */
8962 if (is_direct_enum_init (type
, rhs
))
8964 tree elt
= CONSTRUCTOR_ELT (rhs
, 0)->value
;
8965 if (check_narrowing (ENUM_UNDERLYING_TYPE (type
), elt
, complain
))
8967 warning_sentinel
w (warn_useless_cast
);
8968 warning_sentinel
w2 (warn_ignored_qualifiers
);
8969 rhs
= cp_build_c_cast (type
, elt
, complain
);
8972 rhs
= error_mark_node
;
8975 rhstype
= TREE_TYPE (rhs
);
8976 coder
= TREE_CODE (rhstype
);
8978 if (VECTOR_TYPE_P (type
) && coder
== VECTOR_TYPE
8979 && vector_types_convertible_p (type
, rhstype
, true))
8981 rhs
= mark_rvalue_use (rhs
);
8982 return convert (type
, rhs
);
8985 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
8986 return error_mark_node
;
8987 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
8988 return error_mark_node
;
8990 /* The RHS of an assignment cannot have void type. */
8991 if (coder
== VOID_TYPE
)
8993 if (complain
& tf_error
)
8994 error_at (rhs_loc
, "void value not ignored as it ought to be");
8995 return error_mark_node
;
8998 if (c_dialect_objc ())
9002 tree rname
= fndecl
;
9013 selector
= objc_message_selector ();
9015 if (selector
&& parmno
> 1)
9022 if (objc_compare_types (type
, rhstype
, parmno
, rname
))
9024 rhs
= mark_rvalue_use (rhs
);
9025 return convert (type
, rhs
);
9031 The expression is implicitly converted (clause _conv_) to the
9032 cv-unqualified type of the left operand.
9034 We allow bad conversions here because by the time we get to this point
9035 we are committed to doing the conversion. If we end up doing a bad
9036 conversion, convert_like will complain. */
9037 if (!can_convert_arg_bad (type
, rhstype
, rhs
, flags
, complain
))
9039 /* When -Wno-pmf-conversions is use, we just silently allow
9040 conversions from pointers-to-members to plain pointers. If
9041 the conversion doesn't work, cp_convert will complain. */
9043 && TYPE_PTR_P (type
)
9044 && TYPE_PTRMEMFUNC_P (rhstype
))
9045 rhs
= cp_convert (strip_top_quals (type
), rhs
, complain
);
9048 if (complain
& tf_error
)
9050 /* If the right-hand side has unknown type, then it is an
9051 overloaded function. Call instantiate_type to get error
9053 if (rhstype
== unknown_type_node
)
9055 tree r
= instantiate_type (type
, rhs
, tf_warning_or_error
);
9056 /* -fpermissive might allow this; recurse. */
9058 return convert_for_assignment (type
, r
, errtype
, fndecl
,
9059 parmnum
, complain
, flags
);
9062 complain_about_bad_argument (rhs_loc
,
9067 range_label_for_type_mismatch
label (rhstype
, type
);
9068 gcc_rich_location
richloc (rhs_loc
, has_loc
? &label
: NULL
);
9071 case ICR_DEFAULT_ARGUMENT
:
9073 "cannot convert %qH to %qI in default argument",
9078 "cannot convert %qH to %qI in argument passing",
9081 case ICR_CONVERTING
:
9082 error_at (&richloc
, "cannot convert %qH to %qI",
9087 "cannot convert %qH to %qI in initialization",
9091 error_at (&richloc
, "cannot convert %qH to %qI in return",
9096 "cannot convert %qH to %qI in assignment",
9103 if (TYPE_PTR_P (rhstype
)
9104 && TYPE_PTR_P (type
)
9105 && CLASS_TYPE_P (TREE_TYPE (rhstype
))
9106 && CLASS_TYPE_P (TREE_TYPE (type
))
9107 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype
)))
9108 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
9109 (TREE_TYPE (rhstype
))),
9110 "class type %qT is incomplete", TREE_TYPE (rhstype
));
9112 return error_mark_node
;
9115 if (warn_suggest_attribute_format
)
9117 const enum tree_code codel
= TREE_CODE (type
);
9118 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
9120 && check_missing_format_attribute (type
, rhstype
)
9121 && (complain
& tf_warning
))
9125 case ICR_DEFAULT_ARGUMENT
:
9127 warning (OPT_Wsuggest_attribute_format
,
9128 "parameter %qP of %qD might be a candidate "
9129 "for a format attribute", parmnum
, fndecl
);
9131 warning (OPT_Wsuggest_attribute_format
,
9132 "parameter might be a candidate "
9133 "for a format attribute");
9135 case ICR_CONVERTING
:
9136 warning (OPT_Wsuggest_attribute_format
,
9137 "target of conversion might be a candidate "
9138 "for a format attribute");
9141 warning (OPT_Wsuggest_attribute_format
,
9142 "target of initialization might be a candidate "
9143 "for a format attribute");
9146 warning (OPT_Wsuggest_attribute_format
,
9147 "return type might be a candidate "
9148 "for a format attribute");
9151 warning (OPT_Wsuggest_attribute_format
,
9152 "left-hand side of assignment might be a candidate "
9153 "for a format attribute");
9160 /* If -Wparentheses, warn about a = b = c when a has type bool and b
9162 if (warn_parentheses
9163 && TREE_CODE (type
) == BOOLEAN_TYPE
9164 && TREE_CODE (rhs
) == MODIFY_EXPR
9165 && !TREE_NO_WARNING (rhs
)
9166 && TREE_CODE (TREE_TYPE (rhs
)) != BOOLEAN_TYPE
9167 && (complain
& tf_warning
)
9168 && warning_at (rhs_loc
, OPT_Wparentheses
,
9169 "suggest parentheses around assignment used as "
9171 TREE_NO_WARNING (rhs
) = 1;
9173 if (complain
& tf_warning
)
9174 warn_for_address_or_pointer_of_packed_member (type
, rhs
);
9176 return perform_implicit_conversion_flags (strip_top_quals (type
), rhs
,
9180 /* Convert RHS to be of type TYPE.
9181 If EXP is nonzero, it is the target of the initialization.
9182 ERRTYPE indicates what kind of error the implicit conversion is.
9184 Two major differences between the behavior of
9185 `convert_for_assignment' and `convert_for_initialization'
9186 are that references are bashed in the former, while
9187 copied in the latter, and aggregates are assigned in
9188 the former (operator=) while initialized in the
9191 If using constructor make sure no conversion operator exists, if one does
9192 exist, an ambiguity exists. */
9195 convert_for_initialization (tree exp
, tree type
, tree rhs
, int flags
,
9196 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
9197 tsubst_flags_t complain
)
9199 enum tree_code codel
= TREE_CODE (type
);
9201 enum tree_code coder
;
9203 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9204 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
9205 if (TREE_CODE (rhs
) == NOP_EXPR
9206 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
9207 && codel
!= REFERENCE_TYPE
)
9208 rhs
= TREE_OPERAND (rhs
, 0);
9210 if (type
== error_mark_node
9211 || rhs
== error_mark_node
9212 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
9213 return error_mark_node
;
9215 if (MAYBE_CLASS_TYPE_P (non_reference (type
)))
9217 else if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
9218 && TREE_CODE (type
) != ARRAY_TYPE
9219 && (!TYPE_REF_P (type
)
9220 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
9221 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
9222 && !TYPE_REFFN_P (type
))
9223 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
9224 rhs
= decay_conversion (rhs
, complain
);
9226 rhstype
= TREE_TYPE (rhs
);
9227 coder
= TREE_CODE (rhstype
);
9229 if (coder
== ERROR_MARK
)
9230 return error_mark_node
;
9232 /* We accept references to incomplete types, so we can
9233 return here before checking if RHS is of complete type. */
9235 if (codel
== REFERENCE_TYPE
)
9237 auto_diagnostic_group d
;
9238 /* This should eventually happen in convert_arguments. */
9239 int savew
= 0, savee
= 0;
9242 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
9243 rhs
= initialize_reference (type
, rhs
, flags
, complain
);
9246 && (warningcount
+ werrorcount
> savew
|| errorcount
> savee
))
9247 inform (get_fndecl_argument_location (fndecl
, parmnum
),
9248 "in passing argument %P of %qD", parmnum
, fndecl
);
9253 exp
= require_complete_type_sfinae (exp
, complain
);
9254 if (exp
== error_mark_node
)
9255 return error_mark_node
;
9257 type
= complete_type (type
);
9259 if (DIRECT_INIT_EXPR_P (type
, rhs
))
9260 /* Don't try to do copy-initialization if we already have
9261 direct-initialization. */
9264 if (MAYBE_CLASS_TYPE_P (type
))
9265 return perform_implicit_conversion_flags (type
, rhs
, complain
, flags
);
9267 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
,
9271 /* If RETVAL is the address of, or a reference to, a local variable or
9272 temporary give an appropriate warning and return true. */
9275 maybe_warn_about_returning_address_of_local (tree retval
)
9277 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
9278 tree whats_returned
= fold_for_warn (retval
);
9279 location_t loc
= cp_expr_loc_or_input_loc (retval
);
9283 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
9284 whats_returned
= TREE_OPERAND (whats_returned
, 1);
9285 else if (CONVERT_EXPR_P (whats_returned
)
9286 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
)
9287 whats_returned
= TREE_OPERAND (whats_returned
, 0);
9292 if (TREE_CODE (whats_returned
) == TARGET_EXPR
9293 && is_std_init_list (TREE_TYPE (whats_returned
)))
9295 tree init
= TARGET_EXPR_INITIAL (whats_returned
);
9296 if (TREE_CODE (init
) == CONSTRUCTOR
)
9297 /* Pull out the array address. */
9298 whats_returned
= CONSTRUCTOR_ELT (init
, 0)->value
;
9299 else if (TREE_CODE (init
) == INDIRECT_REF
)
9300 /* The source of a trivial copy looks like *(T*)&var. */
9301 whats_returned
= TREE_OPERAND (init
, 0);
9304 STRIP_NOPS (whats_returned
);
9307 /* As a special case, we handle a call to std::move or std::forward. */
9308 if (TREE_CODE (whats_returned
) == CALL_EXPR
9309 && (is_std_move_p (whats_returned
)
9310 || is_std_forward_p (whats_returned
)))
9312 tree arg
= CALL_EXPR_ARG (whats_returned
, 0);
9313 return maybe_warn_about_returning_address_of_local (arg
);
9316 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
9318 whats_returned
= TREE_OPERAND (whats_returned
, 0);
9320 while (TREE_CODE (whats_returned
) == COMPONENT_REF
9321 || TREE_CODE (whats_returned
) == ARRAY_REF
)
9322 whats_returned
= TREE_OPERAND (whats_returned
, 0);
9324 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
9325 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
9327 if (TYPE_REF_P (valtype
))
9328 warning_at (loc
, OPT_Wreturn_local_addr
,
9329 "returning reference to temporary");
9330 else if (is_std_init_list (valtype
))
9331 warning_at (loc
, OPT_Winit_list_lifetime
,
9332 "returning temporary %<initializer_list%> does not extend "
9333 "the lifetime of the underlying array");
9337 STRIP_ANY_LOCATION_WRAPPER (whats_returned
);
9339 if (DECL_P (whats_returned
)
9340 && DECL_NAME (whats_returned
)
9341 && DECL_FUNCTION_SCOPE_P (whats_returned
)
9342 && !is_capture_proxy (whats_returned
)
9343 && !(TREE_STATIC (whats_returned
)
9344 || TREE_PUBLIC (whats_returned
)))
9346 if (VAR_P (whats_returned
)
9347 && DECL_DECOMPOSITION_P (whats_returned
)
9348 && DECL_DECOMP_BASE (whats_returned
)
9349 && DECL_HAS_VALUE_EXPR_P (whats_returned
))
9351 /* When returning address of a structured binding, if the structured
9352 binding is not a reference, continue normally, if it is a
9353 reference, recurse on the initializer of the structured
9355 tree base
= DECL_DECOMP_BASE (whats_returned
);
9356 if (TYPE_REF_P (TREE_TYPE (base
)))
9358 tree init
= DECL_INITIAL (base
);
9359 return maybe_warn_about_returning_address_of_local (init
);
9363 auto_diagnostic_group d
;
9364 if (TYPE_REF_P (valtype
))
9365 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
9366 "reference to local variable %qD returned",
9368 else if (is_std_init_list (valtype
))
9369 w
= warning_at (loc
, OPT_Winit_list_lifetime
,
9370 "returning local %<initializer_list%> variable %qD "
9371 "does not extend the lifetime of the underlying array",
9373 else if (POINTER_TYPE_P (valtype
)
9374 && TREE_CODE (whats_returned
) == LABEL_DECL
)
9375 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
9376 "address of label %qD returned",
9378 else if (POINTER_TYPE_P (valtype
))
9379 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
9380 "address of local variable %qD returned",
9383 inform (DECL_SOURCE_LOCATION (whats_returned
),
9391 /* Returns true if DECL is in the std namespace. */
9394 decl_in_std_namespace_p (tree decl
)
9396 return (decl
!= NULL_TREE
9397 && DECL_NAMESPACE_STD_P (decl_namespace_context (decl
)));
9400 /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
9403 is_std_forward_p (tree fn
)
9405 /* std::forward only takes one argument. */
9406 if (call_expr_nargs (fn
) != 1)
9409 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
9410 if (!decl_in_std_namespace_p (fndecl
))
9413 tree name
= DECL_NAME (fndecl
);
9414 return name
&& id_equal (name
, "forward");
9417 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
9420 is_std_move_p (tree fn
)
9422 /* std::move only takes one argument. */
9423 if (call_expr_nargs (fn
) != 1)
9426 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
9427 if (!decl_in_std_namespace_p (fndecl
))
9430 tree name
= DECL_NAME (fndecl
);
9431 return name
&& id_equal (name
, "move");
9434 /* Returns true if RETVAL is a good candidate for the NRVO as per
9435 [class.copy.elision]. FUNCTYPE is the type the function is declared
9439 can_do_nrvo_p (tree retval
, tree functype
)
9441 if (functype
== error_mark_node
)
9444 STRIP_ANY_LOCATION_WRAPPER (retval
);
9445 tree result
= DECL_RESULT (current_function_decl
);
9446 return (retval
!= NULL_TREE
9447 && !processing_template_decl
9448 /* Must be a local, automatic variable. */
9450 && DECL_CONTEXT (retval
) == current_function_decl
9451 && !TREE_STATIC (retval
)
9452 /* And not a lambda or anonymous union proxy. */
9453 && !DECL_HAS_VALUE_EXPR_P (retval
)
9454 && (DECL_ALIGN (retval
) <= DECL_ALIGN (result
))
9455 /* The cv-unqualified type of the returned value must be the
9456 same as the cv-unqualified return type of the
9458 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval
))),
9459 (TYPE_MAIN_VARIANT (functype
)))
9460 /* And the returned value must be non-volatile. */
9461 && !TYPE_VOLATILE (TREE_TYPE (retval
)));
9464 /* Returns true if we should treat RETVAL, an expression being returned,
9465 as if it were designated by an rvalue. See [class.copy.elision].
9466 PARM_P is true if a function parameter is OK in this context. */
9469 treat_lvalue_as_rvalue_p (tree retval
, bool parm_ok
)
9471 STRIP_ANY_LOCATION_WRAPPER (retval
);
9472 return ((cxx_dialect
!= cxx98
)
9473 && ((VAR_P (retval
) && !DECL_HAS_VALUE_EXPR_P (retval
))
9474 || (parm_ok
&& TREE_CODE (retval
) == PARM_DECL
))
9475 && DECL_CONTEXT (retval
) == current_function_decl
9476 && !TREE_STATIC (retval
));
9479 /* Warn about wrong usage of std::move in a return statement. RETVAL
9480 is the expression we are returning; FUNCTYPE is the type the function
9481 is declared to return. */
9484 maybe_warn_pessimizing_move (tree retval
, tree functype
)
9486 if (!(warn_pessimizing_move
|| warn_redundant_move
))
9489 location_t loc
= cp_expr_loc_or_input_loc (retval
);
9491 /* C++98 doesn't know move. */
9492 if (cxx_dialect
< cxx11
)
9495 /* Wait until instantiation time, since we can't gauge if we should do
9496 the NRVO until then. */
9497 if (processing_template_decl
)
9500 /* This is only interesting for class types. */
9501 if (!CLASS_TYPE_P (functype
))
9504 /* We're looking for *std::move<T&> ((T &) &arg). */
9505 if (REFERENCE_REF_P (retval
)
9506 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
9508 tree fn
= TREE_OPERAND (retval
, 0);
9509 if (is_std_move_p (fn
))
9511 tree arg
= CALL_EXPR_ARG (fn
, 0);
9512 if (TREE_CODE (arg
) != NOP_EXPR
)
9514 arg
= TREE_OPERAND (arg
, 0);
9515 if (TREE_CODE (arg
) != ADDR_EXPR
)
9517 arg
= TREE_OPERAND (arg
, 0);
9518 arg
= convert_from_reference (arg
);
9519 /* Warn if we could do copy elision were it not for the move. */
9520 if (can_do_nrvo_p (arg
, functype
))
9522 auto_diagnostic_group d
;
9523 if (warning_at (loc
, OPT_Wpessimizing_move
,
9524 "moving a local object in a return statement "
9525 "prevents copy elision"))
9526 inform (loc
, "remove %<std::move%> call");
9528 /* Warn if the move is redundant. It is redundant when we would
9529 do maybe-rvalue overload resolution even without std::move. */
9530 else if (warn_redundant_move
9531 && treat_lvalue_as_rvalue_p (arg
, /*parm_ok*/true))
9533 /* Make sure that the overload resolution would actually succeed
9534 if we removed the std::move call. */
9535 tree t
= convert_for_initialization (NULL_TREE
, functype
,
9538 | LOOKUP_ONLYCONVERTING
9539 | LOOKUP_PREFER_RVALUE
),
9540 ICR_RETURN
, NULL_TREE
, 0,
9542 /* If this worked, implicit rvalue would work, so the call to
9543 std::move is redundant. */
9544 if (t
!= error_mark_node
)
9546 auto_diagnostic_group d
;
9547 if (warning_at (loc
, OPT_Wredundant_move
,
9548 "redundant move in return statement"))
9549 inform (loc
, "remove %<std::move%> call");
9556 /* Check that returning RETVAL from the current function is valid.
9557 Return an expression explicitly showing all conversions required to
9558 change RETVAL into the function return type, and to assign it to
9559 the DECL_RESULT for the function. Set *NO_WARNING to true if
9560 code reaches end of non-void function warning shouldn't be issued
9561 on this RETURN_EXPR. */
9564 check_return_expr (tree retval
, bool *no_warning
)
9567 /* The type actually returned by the function. */
9569 /* The type the function is declared to return, or void if
9570 the declared type is incomplete. */
9572 int fn_returns_value_p
;
9574 *no_warning
= false;
9576 /* A `volatile' function is one that isn't supposed to return, ever.
9577 (This is a G++ extension, used to get better code for functions
9578 that call the `volatile' function.) */
9579 if (TREE_THIS_VOLATILE (current_function_decl
))
9580 warning (0, "function declared %<noreturn%> has a %<return%> statement");
9582 /* Check for various simple errors. */
9583 if (DECL_DESTRUCTOR_P (current_function_decl
))
9586 error ("returning a value from a destructor");
9589 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
9591 if (in_function_try_handler
)
9592 /* If a return statement appears in a handler of the
9593 function-try-block of a constructor, the program is ill-formed. */
9594 error ("cannot return from a handler of a function-try-block of a constructor");
9596 /* You can't return a value from a constructor. */
9597 error ("returning a value from a constructor");
9601 const tree saved_retval
= retval
;
9603 if (processing_template_decl
)
9605 current_function_returns_value
= 1;
9607 if (check_for_bare_parameter_packs (retval
))
9608 return error_mark_node
;
9610 /* If one of the types might be void, we can't tell whether we're
9611 returning a value. */
9612 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl
)))
9613 && !FNDECL_USED_AUTO (current_function_decl
))
9614 || (retval
!= NULL_TREE
9615 && (TREE_TYPE (retval
) == NULL_TREE
9616 || WILDCARD_TYPE_P (TREE_TYPE (retval
)))))
9620 functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
9622 /* Deduce auto return type from a return statement. */
9623 if (FNDECL_USED_AUTO (current_function_decl
))
9625 tree pattern
= DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
);
9629 if (!retval
&& !is_auto (pattern
))
9631 /* Give a helpful error message. */
9632 error ("return-statement with no value, in function returning %qT",
9634 inform (input_location
, "only plain %<auto%> return type can be "
9635 "deduced to %<void%>");
9636 type
= error_mark_node
;
9638 else if (retval
&& BRACE_ENCLOSED_INITIALIZER_P (retval
))
9640 error ("returning initializer list");
9641 type
= error_mark_node
;
9647 auto_node
= type_uses_auto (pattern
);
9648 type
= do_auto_deduction (pattern
, retval
, auto_node
,
9649 tf_warning_or_error
, adc_return_type
);
9652 if (type
== error_mark_node
)
9654 else if (functype
== pattern
)
9655 apply_deduced_return_type (current_function_decl
, type
);
9656 else if (!same_type_p (type
, functype
))
9658 if (LAMBDA_FUNCTION_P (current_function_decl
))
9659 error ("inconsistent types %qT and %qT deduced for "
9660 "lambda return type", functype
, type
);
9662 error ("inconsistent deduction for auto return type: "
9663 "%qT and then %qT", functype
, type
);
9668 result
= DECL_RESULT (current_function_decl
);
9669 valtype
= TREE_TYPE (result
);
9670 gcc_assert (valtype
!= NULL_TREE
);
9671 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
9673 /* Check for a return statement with no return value in a function
9674 that's supposed to return a value. */
9675 if (!retval
&& fn_returns_value_p
)
9677 if (functype
!= error_mark_node
)
9678 permerror (input_location
, "return-statement with no value, in "
9679 "function returning %qT", valtype
);
9680 /* Remember that this function did return. */
9681 current_function_returns_value
= 1;
9682 /* And signal caller that TREE_NO_WARNING should be set on the
9683 RETURN_EXPR to avoid control reaches end of non-void function
9684 warnings in tree-cfg.c. */
9687 /* Check for a return statement with a value in a function that
9688 isn't supposed to return a value. */
9689 else if (retval
&& !fn_returns_value_p
)
9691 if (VOID_TYPE_P (TREE_TYPE (retval
)))
9692 /* You can return a `void' value from a function of `void'
9693 type. In that case, we have to evaluate the expression for
9694 its side-effects. */
9695 finish_expr_stmt (retval
);
9697 permerror (input_location
,
9698 "return-statement with a value, in function "
9699 "returning %qT", valtype
);
9700 current_function_returns_null
= 1;
9702 /* There's really no value to return, after all. */
9706 /* Remember that this function can sometimes return without a
9708 current_function_returns_null
= 1;
9710 /* Remember that this function did return a value. */
9711 current_function_returns_value
= 1;
9713 /* Check for erroneous operands -- but after giving ourselves a
9714 chance to provide an error about returning a value from a void
9716 if (error_operand_p (retval
))
9718 current_function_return_value
= error_mark_node
;
9719 return error_mark_node
;
9722 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
9723 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl
))
9724 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
9726 && retval
&& null_ptr_cst_p (retval
))
9727 warning (0, "%<operator new%> must not return NULL unless it is "
9728 "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
9730 /* Effective C++ rule 15. See also start_function. */
9732 && DECL_NAME (current_function_decl
) == assign_op_identifier
9733 && !type_dependent_expression_p (retval
))
9737 /* The function return type must be a reference to the current
9739 if (TYPE_REF_P (valtype
)
9740 && same_type_ignoring_top_level_qualifiers_p
9741 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
)))
9743 /* Returning '*this' is obviously OK. */
9744 if (retval
== current_class_ref
)
9746 /* If we are calling a function whose return type is the same of
9747 the current class reference, it is ok. */
9748 else if (INDIRECT_REF_P (retval
)
9749 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
9754 warning (OPT_Weffc__
, "%<operator=%> should return a reference to %<*this%>");
9757 if (dependent_type_p (functype
)
9758 || type_dependent_expression_p (retval
))
9761 /* We should not have changed the return value. */
9762 gcc_assert (retval
== saved_retval
);
9766 /* The fabled Named Return Value optimization, as per [class.copy]/15:
9768 [...] For a function with a class return type, if the expression
9769 in the return statement is the name of a local object, and the cv-
9770 unqualified type of the local object is the same as the function
9771 return type, an implementation is permitted to omit creating the tem-
9772 porary object to hold the function return value [...]
9774 So, if this is a value-returning function that always returns the same
9775 local variable, remember it.
9777 It might be nice to be more flexible, and choose the first suitable
9778 variable even if the function sometimes returns something else, but
9779 then we run the risk of clobbering the variable we chose if the other
9780 returned expression uses the chosen variable somehow. And people expect
9781 this restriction, anyway. (jason 2000-11-19)
9783 See finish_function and finalize_nrv for the rest of this optimization. */
9785 STRIP_ANY_LOCATION_WRAPPER (retval
);
9787 bool named_return_value_okay_p
= can_do_nrvo_p (retval
, functype
);
9788 if (fn_returns_value_p
&& flag_elide_constructors
)
9790 if (named_return_value_okay_p
9791 && (current_function_return_value
== NULL_TREE
9792 || current_function_return_value
== retval
))
9793 current_function_return_value
= retval
;
9795 current_function_return_value
= error_mark_node
;
9798 /* We don't need to do any conversions when there's nothing being
9803 if (!named_return_value_okay_p
)
9804 maybe_warn_pessimizing_move (retval
, functype
);
9806 /* Do any required conversions. */
9807 if (retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
9808 /* No conversions are required. */
9812 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
9814 /* The functype's return type will have been set to void, if it
9815 was an incomplete type. Just treat this as 'return;' */
9816 if (VOID_TYPE_P (functype
))
9817 return error_mark_node
;
9819 /* If we had an id-expression obfuscated by force_paren_expr, we need
9820 to undo it so we can try to treat it as an rvalue below. */
9821 retval
= maybe_undo_parenthesized_ref (retval
);
9823 if (processing_template_decl
)
9824 retval
= build_non_dependent_expr (retval
);
9826 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9827 treated as an rvalue for the purposes of overload resolution to
9828 favor move constructors over copy constructors.
9830 Note that these conditions are similar to, but not as strict as,
9831 the conditions for the named return value optimization. */
9832 bool converted
= false;
9833 if (treat_lvalue_as_rvalue_p (retval
, /*parm_ok*/true)
9834 /* This is only interesting for class type. */
9835 && CLASS_TYPE_P (functype
))
9837 tree moved
= move (retval
);
9838 moved
= convert_for_initialization
9839 (NULL_TREE
, functype
, moved
, flags
|LOOKUP_PREFER_RVALUE
,
9840 ICR_RETURN
, NULL_TREE
, 0, tf_none
);
9841 if (moved
!= error_mark_node
)
9848 /* The call in a (lambda) thunk needs no conversions. */
9849 if (TREE_CODE (retval
) == CALL_EXPR
9850 && CALL_FROM_THUNK_P (retval
))
9853 /* First convert the value to the function's return type, then
9854 to the type of return value's location to handle the
9855 case that functype is smaller than the valtype. */
9857 retval
= convert_for_initialization
9858 (NULL_TREE
, functype
, retval
, flags
, ICR_RETURN
, NULL_TREE
, 0,
9859 tf_warning_or_error
);
9860 retval
= convert (valtype
, retval
);
9862 /* If the conversion failed, treat this just like `return;'. */
9863 if (retval
== error_mark_node
)
9865 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9866 else if (! cfun
->returns_struct
9867 && TREE_CODE (retval
) == TARGET_EXPR
9868 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
9869 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
9870 TREE_OPERAND (retval
, 0));
9871 else if (!processing_template_decl
9872 && maybe_warn_about_returning_address_of_local (retval
)
9873 && INDIRECT_TYPE_P (valtype
))
9874 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
9875 build_zero_cst (TREE_TYPE (retval
)));
9878 if (processing_template_decl
)
9879 return saved_retval
;
9881 /* Actually copy the value returned into the appropriate location. */
9882 if (retval
&& retval
!= result
)
9883 retval
= build2 (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
9889 /* Returns nonzero if the pointer-type FROM can be converted to the
9890 pointer-type TO via a qualification conversion. If CONSTP is -1,
9891 then we return nonzero if the pointers are similar, and the
9892 cv-qualification signature of FROM is a proper subset of that of TO.
9894 If CONSTP is positive, then all outer pointers have been
9898 comp_ptr_ttypes_real (tree to
, tree from
, int constp
)
9900 bool to_more_cv_qualified
= false;
9901 bool is_opaque_pointer
= false;
9903 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9905 if (TREE_CODE (to
) != TREE_CODE (from
))
9908 if (TREE_CODE (from
) == OFFSET_TYPE
9909 && !same_type_p (TYPE_OFFSET_BASETYPE (from
),
9910 TYPE_OFFSET_BASETYPE (to
)))
9913 /* Const and volatile mean something different for function and
9914 array types, so the usual checks are not appropriate. We'll
9915 check the array type elements in further iterations. */
9916 if (!FUNC_OR_METHOD_TYPE_P (to
) && TREE_CODE (to
) != ARRAY_TYPE
)
9918 if (!at_least_as_qualified_p (to
, from
))
9921 if (!at_least_as_qualified_p (from
, to
))
9925 to_more_cv_qualified
= true;
9929 constp
&= TYPE_READONLY (to
);
9932 if (VECTOR_TYPE_P (to
))
9933 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
9935 /* P0388R4 allows a conversion from int[N] to int[] but not the
9936 other way round. When both arrays have bounds but they do
9937 not match, then no conversion is possible. */
9938 if (TREE_CODE (to
) == ARRAY_TYPE
9939 && !comp_array_types (to
, from
, bounds_first
, /*strict=*/false))
9942 if (!TYPE_PTR_P (to
)
9943 && !TYPE_PTRDATAMEM_P (to
)
9944 /* CWG 330 says we need to look through arrays. */
9945 && TREE_CODE (to
) != ARRAY_TYPE
)
9946 return ((constp
>= 0 || to_more_cv_qualified
)
9947 && (is_opaque_pointer
9948 || same_type_ignoring_top_level_qualifiers_p (to
, from
)));
9952 /* When comparing, say, char ** to char const **, this function takes
9953 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9954 types to this function. */
9957 comp_ptr_ttypes (tree to
, tree from
)
9959 return comp_ptr_ttypes_real (to
, from
, 1);
9962 /* Returns true iff FNTYPE is a non-class type that involves
9963 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9964 if a parameter type is ill-formed. */
9967 error_type_p (const_tree type
)
9971 switch (TREE_CODE (type
))
9977 case REFERENCE_TYPE
:
9979 return error_type_p (TREE_TYPE (type
));
9983 if (error_type_p (TREE_TYPE (type
)))
9985 for (t
= TYPE_ARG_TYPES (type
); t
; t
= TREE_CHAIN (t
))
9986 if (error_type_p (TREE_VALUE (t
)))
9991 if (TYPE_PTRMEMFUNC_P (type
))
9992 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type
));
10000 /* Returns true if to and from are (possibly multi-level) pointers to the same
10001 type or inheritance-related types, regardless of cv-quals. */
10004 ptr_reasonably_similar (const_tree to
, const_tree from
)
10006 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
10008 /* Any target type is similar enough to void. */
10009 if (VOID_TYPE_P (to
))
10010 return !error_type_p (from
);
10011 if (VOID_TYPE_P (from
))
10012 return !error_type_p (to
);
10014 if (TREE_CODE (to
) != TREE_CODE (from
))
10017 if (TREE_CODE (from
) == OFFSET_TYPE
10018 && comptypes (TYPE_OFFSET_BASETYPE (to
),
10019 TYPE_OFFSET_BASETYPE (from
),
10020 COMPARE_BASE
| COMPARE_DERIVED
))
10023 if (VECTOR_TYPE_P (to
)
10024 && vector_types_convertible_p (to
, from
, false))
10027 if (TREE_CODE (to
) == INTEGER_TYPE
10028 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
10031 if (TREE_CODE (to
) == FUNCTION_TYPE
)
10032 return !error_type_p (to
) && !error_type_p (from
);
10034 if (!TYPE_PTR_P (to
))
10036 /* When either type is incomplete avoid DERIVED_FROM_P,
10037 which may call complete_type (c++/57942). */
10038 bool b
= !COMPLETE_TYPE_P (to
) || !COMPLETE_TYPE_P (from
);
10040 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
10041 b
? COMPARE_STRICT
: COMPARE_BASE
| COMPARE_DERIVED
);
10046 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
10047 pointer-to-member types) are the same, ignoring cv-qualification at
10048 all levels. CB says how we should behave when comparing array bounds. */
10051 comp_ptr_ttypes_const (tree to
, tree from
, compare_bounds_t cb
)
10053 bool is_opaque_pointer
= false;
10055 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
10057 if (TREE_CODE (to
) != TREE_CODE (from
))
10060 if (TREE_CODE (from
) == OFFSET_TYPE
10061 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
10062 TYPE_OFFSET_BASETYPE (to
)))
10065 if (VECTOR_TYPE_P (to
))
10066 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
10068 if (TREE_CODE (to
) == ARRAY_TYPE
10069 /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
10071 && !comp_array_types (to
, from
, cb
, /*strict=*/false))
10074 /* CWG 330 says we need to look through arrays. */
10075 if (!TYPE_PTR_P (to
) && TREE_CODE (to
) != ARRAY_TYPE
)
10076 return (is_opaque_pointer
10077 || same_type_ignoring_top_level_qualifiers_p (to
, from
));
10081 /* Returns the type qualifiers for this type, including the qualifiers on the
10082 elements for an array type. */
10085 cp_type_quals (const_tree type
)
10088 /* This CONST_CAST is okay because strip_array_types returns its
10089 argument unmodified and we assign it to a const_tree. */
10090 type
= strip_array_types (CONST_CAST_TREE (type
));
10091 if (type
== error_mark_node
10092 /* Quals on a FUNCTION_TYPE are memfn quals. */
10093 || TREE_CODE (type
) == FUNCTION_TYPE
)
10094 return TYPE_UNQUALIFIED
;
10095 quals
= TYPE_QUALS (type
);
10096 /* METHOD and REFERENCE_TYPEs should never have quals. */
10097 gcc_assert ((TREE_CODE (type
) != METHOD_TYPE
10098 && !TYPE_REF_P (type
))
10099 || ((quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
))
10100 == TYPE_UNQUALIFIED
));
10104 /* Returns the function-ref-qualifier for TYPE */
10107 type_memfn_rqual (const_tree type
)
10109 gcc_assert (FUNC_OR_METHOD_TYPE_P (type
));
10111 if (!FUNCTION_REF_QUALIFIED (type
))
10112 return REF_QUAL_NONE
;
10113 else if (FUNCTION_RVALUE_QUALIFIED (type
))
10114 return REF_QUAL_RVALUE
;
10116 return REF_QUAL_LVALUE
;
10119 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
10123 type_memfn_quals (const_tree type
)
10125 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10126 return TYPE_QUALS (type
);
10127 else if (TREE_CODE (type
) == METHOD_TYPE
)
10128 return cp_type_quals (class_of_this_parm (type
));
10130 gcc_unreachable ();
10133 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
10134 MEMFN_QUALS and its ref-qualifier to RQUAL. */
10137 apply_memfn_quals (tree type
, cp_cv_quals memfn_quals
, cp_ref_qualifier rqual
)
10139 /* Could handle METHOD_TYPE here if necessary. */
10140 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
10141 if (TYPE_QUALS (type
) == memfn_quals
10142 && type_memfn_rqual (type
) == rqual
)
10145 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
10147 tree result
= build_qualified_type (type
, memfn_quals
);
10148 return build_ref_qualified_type (result
, rqual
);
10151 /* Returns nonzero if TYPE is const or volatile. */
10154 cv_qualified_p (const_tree type
)
10156 int quals
= cp_type_quals (type
);
10157 return (quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)) != 0;
10160 /* Returns nonzero if the TYPE contains a mutable member. */
10163 cp_has_mutable_p (const_tree type
)
10165 /* This CONST_CAST is okay because strip_array_types returns its
10166 argument unmodified and we assign it to a const_tree. */
10167 type
= strip_array_types (CONST_CAST_TREE(type
));
10169 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
10172 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
10173 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
10174 approximation. In particular, consider:
10177 struct S { int i; };
10178 const S s = { f(); }
10180 Here, we will make "s" as TREE_READONLY (because it is declared
10181 "const") -- only to reverse ourselves upon seeing that the
10182 initializer is non-constant. */
10185 cp_apply_type_quals_to_decl (int type_quals
, tree decl
)
10187 tree type
= TREE_TYPE (decl
);
10189 if (type
== error_mark_node
)
10192 if (TREE_CODE (decl
) == TYPE_DECL
)
10195 gcc_assert (!(TREE_CODE (type
) == FUNCTION_TYPE
10196 && type_quals
!= TYPE_UNQUALIFIED
));
10198 /* Avoid setting TREE_READONLY incorrectly. */
10199 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
10200 constructor can produce constant init, so rely on cp_finish_decl to
10201 clear TREE_READONLY if the variable has non-constant init. */
10203 /* If the type has (or might have) a mutable component, that component
10204 might be modified. */
10205 if (TYPE_HAS_MUTABLE_P (type
) || !COMPLETE_TYPE_P (type
))
10206 type_quals
&= ~TYPE_QUAL_CONST
;
10208 c_apply_type_quals_to_decl (type_quals
, decl
);
10211 /* Subroutine of casts_away_constness. Make T1 and T2 point at
10212 exemplar types such that casting T1 to T2 is casting away constness
10213 if and only if there is no implicit conversion from T1 to T2. */
10216 casts_away_constness_r (tree
*t1
, tree
*t2
, tsubst_flags_t complain
)
10221 /* [expr.const.cast]
10223 For multi-level pointer to members and multi-level mixed pointers
10224 and pointers to members (conv.qual), the "member" aspect of a
10225 pointer to member level is ignored when determining if a const
10226 cv-qualifier has been cast away. */
10227 /* [expr.const.cast]
10229 For two pointer types:
10231 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
10232 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
10235 casting from X1 to X2 casts away constness if, for a non-pointer
10236 type T there does not exist an implicit conversion (clause
10239 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
10243 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
10244 if ((!TYPE_PTR_P (*t1
) && !TYPE_PTRDATAMEM_P (*t1
))
10245 || (!TYPE_PTR_P (*t2
) && !TYPE_PTRDATAMEM_P (*t2
)))
10247 *t1
= cp_build_qualified_type (void_type_node
,
10248 cp_type_quals (*t1
));
10249 *t2
= cp_build_qualified_type (void_type_node
,
10250 cp_type_quals (*t2
));
10254 quals1
= cp_type_quals (*t1
);
10255 quals2
= cp_type_quals (*t2
);
10257 if (TYPE_PTRDATAMEM_P (*t1
))
10258 *t1
= TYPE_PTRMEM_POINTED_TO_TYPE (*t1
);
10260 *t1
= TREE_TYPE (*t1
);
10261 if (TYPE_PTRDATAMEM_P (*t2
))
10262 *t2
= TYPE_PTRMEM_POINTED_TO_TYPE (*t2
);
10264 *t2
= TREE_TYPE (*t2
);
10266 casts_away_constness_r (t1
, t2
, complain
);
10267 *t1
= build_pointer_type (*t1
);
10268 *t2
= build_pointer_type (*t2
);
10269 *t1
= cp_build_qualified_type (*t1
, quals1
);
10270 *t2
= cp_build_qualified_type (*t2
, quals2
);
10273 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
10276 ??? This function returns non-zero if casting away qualifiers not
10277 just const. We would like to return to the caller exactly which
10278 qualifiers are casted away to give more accurate diagnostics.
10282 casts_away_constness (tree t1
, tree t2
, tsubst_flags_t complain
)
10284 if (TYPE_REF_P (t2
))
10286 /* [expr.const.cast]
10288 Casting from an lvalue of type T1 to an lvalue of type T2
10289 using a reference cast casts away constness if a cast from an
10290 rvalue of type "pointer to T1" to the type "pointer to T2"
10291 casts away constness. */
10292 t1
= (TYPE_REF_P (t1
) ? TREE_TYPE (t1
) : t1
);
10293 return casts_away_constness (build_pointer_type (t1
),
10294 build_pointer_type (TREE_TYPE (t2
)),
10298 if (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
10299 /* [expr.const.cast]
10301 Casting from an rvalue of type "pointer to data member of X
10302 of type T1" to the type "pointer to data member of Y of type
10303 T2" casts away constness if a cast from an rvalue of type
10304 "pointer to T1" to the type "pointer to T2" casts away
10306 return casts_away_constness
10307 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1
)),
10308 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2
)),
10311 /* Casting away constness is only something that makes sense for
10312 pointer or reference types. */
10313 if (!TYPE_PTR_P (t1
) || !TYPE_PTR_P (t2
))
10316 /* Top-level qualifiers don't matter. */
10317 t1
= TYPE_MAIN_VARIANT (t1
);
10318 t2
= TYPE_MAIN_VARIANT (t2
);
10319 casts_away_constness_r (&t1
, &t2
, complain
);
10320 if (!can_convert (t2
, t1
, complain
))
10326 /* If T is a REFERENCE_TYPE return the type to which T refers.
10327 Otherwise, return T itself. */
10330 non_reference (tree t
)
10332 if (t
&& TYPE_REF_P (t
))
10338 /* Return nonzero if REF is an lvalue valid for this language;
10339 otherwise, print an error message and return zero. USE says
10340 how the lvalue is being used and so selects the error message. */
10343 lvalue_or_else (tree ref
, enum lvalue_use use
, tsubst_flags_t complain
)
10345 cp_lvalue_kind kind
= lvalue_kind (ref
);
10347 if (kind
== clk_none
)
10349 if (complain
& tf_error
)
10350 lvalue_error (input_location
, use
);
10353 else if (kind
& (clk_rvalueref
|clk_class
))
10355 if (!(complain
& tf_error
))
10357 /* Make this a permerror because we used to accept it. */
10358 permerror (input_location
, "using rvalue as lvalue");
10363 /* Return true if a user-defined literal operator is a raw operator. */
10366 check_raw_literal_operator (const_tree decl
)
10368 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
10371 bool maybe_raw_p
= false;
10373 /* Count the number and type of arguments and check for ellipsis. */
10374 for (argtype
= argtypes
, arity
= 0;
10375 argtype
&& argtype
!= void_list_node
;
10376 ++arity
, argtype
= TREE_CHAIN (argtype
))
10378 tree t
= TREE_VALUE (argtype
);
10380 if (same_type_p (t
, const_string_type_node
))
10381 maybe_raw_p
= true;
10384 return false; /* Found ellipsis. */
10386 if (!maybe_raw_p
|| arity
!= 1)
10393 /* Return true if a user-defined literal operator has one of the allowed
10397 check_literal_operator_args (const_tree decl
,
10398 bool *long_long_unsigned_p
, bool *long_double_p
)
10400 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
10402 *long_long_unsigned_p
= false;
10403 *long_double_p
= false;
10404 if (processing_template_decl
|| processing_specialization
)
10405 return argtypes
== void_list_node
;
10412 /* Count the number and type of arguments and check for ellipsis. */
10413 for (argtype
= argtypes
, arity
= 0;
10414 argtype
&& argtype
!= void_list_node
;
10415 argtype
= TREE_CHAIN (argtype
))
10417 tree t
= TREE_VALUE (argtype
);
10420 if (TYPE_PTR_P (t
))
10422 bool maybe_raw_p
= false;
10424 if (cp_type_quals (t
) != TYPE_QUAL_CONST
)
10426 t
= TYPE_MAIN_VARIANT (t
);
10427 if ((maybe_raw_p
= same_type_p (t
, char_type_node
))
10428 || same_type_p (t
, wchar_type_node
)
10429 || same_type_p (t
, char8_type_node
)
10430 || same_type_p (t
, char16_type_node
)
10431 || same_type_p (t
, char32_type_node
))
10433 argtype
= TREE_CHAIN (argtype
);
10436 t
= TREE_VALUE (argtype
);
10437 if (maybe_raw_p
&& argtype
== void_list_node
)
10439 else if (same_type_p (t
, size_type_node
))
10448 else if (same_type_p (t
, long_long_unsigned_type_node
))
10451 *long_long_unsigned_p
= true;
10453 else if (same_type_p (t
, long_double_type_node
))
10456 *long_double_p
= true;
10458 else if (same_type_p (t
, char_type_node
))
10460 else if (same_type_p (t
, wchar_type_node
))
10462 else if (same_type_p (t
, char8_type_node
))
10464 else if (same_type_p (t
, char16_type_node
))
10466 else if (same_type_p (t
, char32_type_node
))
10472 return false; /* Found ellipsis. */
10474 if (arity
!= max_arity
)
10481 /* Always returns false since unlike C90, C++ has no concept of implicit
10482 function declarations. */
10485 c_decl_implicit (const_tree
)