1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2023 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"
38 #include "gcc-rich-location.h"
39 #include "stringpool.h"
44 static tree
cp_build_addr_expr_strict (tree
, tsubst_flags_t
);
45 static tree
cp_build_function_call (tree
, tree
, tsubst_flags_t
);
46 static tree
pfn_from_ptrmemfunc (tree
);
47 static tree
delta_from_ptrmemfunc (tree
);
48 static tree
convert_for_assignment (tree
, tree
, impl_conv_rhs
, tree
, int,
50 static tree
cp_pointer_int_sum (location_t
, enum tree_code
, tree
, tree
,
52 static tree
rationalize_conditional_expr (enum tree_code
, tree
,
54 static bool comp_ptr_ttypes_real (tree
, tree
, int);
55 static bool comp_except_types (tree
, tree
, bool);
56 static bool comp_array_types (const_tree
, const_tree
, compare_bounds_t
, bool);
57 static tree
pointer_diff (location_t
, tree
, tree
, tree
, tsubst_flags_t
, tree
*);
58 static tree
get_delta_difference (tree
, tree
, bool, bool, tsubst_flags_t
);
59 static void casts_away_constness_r (tree
*, tree
*, tsubst_flags_t
);
60 static bool casts_away_constness (tree
, tree
, tsubst_flags_t
);
61 static bool maybe_warn_about_returning_address_of_local (tree
, location_t
= UNKNOWN_LOCATION
);
62 static void error_args_num (location_t
, tree
, bool);
63 static int convert_arguments (tree
, vec
<tree
, va_gc
> **, tree
, int,
65 static bool is_std_move_p (tree
);
66 static bool is_std_forward_p (tree
);
68 /* Do `exp = require_complete_type (exp);' to make sure exp
69 does not have an incomplete type. (That includes void types.)
70 Returns error_mark_node if the VALUE does not have
71 complete type when this function returns. */
74 require_complete_type (tree value
,
75 tsubst_flags_t complain
/* = tf_warning_or_error */)
79 if (processing_template_decl
|| value
== error_mark_node
)
82 if (TREE_CODE (value
) == OVERLOAD
)
83 type
= unknown_type_node
;
85 type
= TREE_TYPE (value
);
87 if (type
== error_mark_node
)
88 return error_mark_node
;
90 /* First, detect a valid value with a complete type. */
91 if (COMPLETE_TYPE_P (type
))
94 if (complete_type_or_maybe_complain (type
, value
, complain
))
97 return error_mark_node
;
100 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
101 a template instantiation, do the instantiation. Returns TYPE,
102 whether or not it could be completed, unless something goes
103 horribly wrong, in which case the error_mark_node is returned. */
106 complete_type (tree type
)
108 if (type
== NULL_TREE
)
109 /* Rather than crash, we return something sure to cause an error
111 return error_mark_node
;
113 if (type
== error_mark_node
|| COMPLETE_TYPE_P (type
))
115 else if (TREE_CODE (type
) == ARRAY_TYPE
)
117 tree t
= complete_type (TREE_TYPE (type
));
118 unsigned int needs_constructing
, has_nontrivial_dtor
;
119 if (COMPLETE_TYPE_P (t
) && !dependent_type_p (type
))
122 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
124 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
125 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
127 TYPE_NEEDS_CONSTRUCTING (t
) = needs_constructing
;
128 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = has_nontrivial_dtor
;
131 else if (CLASS_TYPE_P (type
))
134 /* TYPE could be a class member we've not loaded the definition of. */
135 lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type
)));
137 if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
138 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
);
159 note_failed_type_completion_for_satisfaction (type
);
167 complete_type_or_else (tree type
, tree value
)
169 return complete_type_or_maybe_complain (type
, value
, tf_warning_or_error
);
173 /* Return the common type of two parameter lists.
174 We assume that comptypes has already been done and returned 1;
175 if that isn't so, this may crash.
177 As an optimization, free the space we allocate if the parameter
178 lists are already common. */
181 commonparms (tree p1
, tree p2
)
183 tree oldargs
= p1
, newargs
, n
;
187 len
= list_length (p1
);
188 newargs
= tree_last (p1
);
190 if (newargs
== void_list_node
)
199 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
204 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
206 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
208 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
211 else if (! TREE_PURPOSE (p1
))
213 if (TREE_PURPOSE (p2
))
215 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
221 if (simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)) != 1)
223 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
225 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
228 TREE_VALUE (n
) = merge_types (TREE_VALUE (p1
), TREE_VALUE (p2
));
231 TREE_VALUE (n
) = TREE_VALUE (p1
);
239 /* Given a type, perhaps copied for a typedef,
240 find the "original" version of it. */
242 original_type (tree t
)
244 int quals
= cp_type_quals (t
);
245 while (t
!= error_mark_node
246 && TYPE_NAME (t
) != NULL_TREE
)
248 tree x
= TYPE_NAME (t
);
249 if (TREE_CODE (x
) != TYPE_DECL
)
251 x
= DECL_ORIGINAL_TYPE (x
);
256 return cp_build_qualified_type (t
, quals
);
259 /* Merge the attributes of type OTHER_TYPE into the attributes of type TYPE
260 and return a variant of TYPE with the merged attributes. */
263 merge_type_attributes_from (tree type
, tree other_type
)
265 tree attrs
= targetm
.merge_type_attributes (type
, other_type
);
266 attrs
= restrict_type_identity_attributes_to (attrs
, TYPE_ATTRIBUTES (type
));
267 return cp_build_type_attribute_variant (type
, attrs
);
270 /* Compare floating point conversion ranks and subranks of T1 and T2
271 types. If T1 and T2 have unordered conversion ranks, return 3.
272 If T1 has greater conversion rank than T2, return 2.
273 If T2 has greater conversion rank than T1, return -2.
274 If T1 has equal conversion rank as T2, return -1, 0 or 1 depending
275 on if T1 has smaller, equal or greater conversion subrank than
279 cp_compare_floating_point_conversion_ranks (tree t1
, tree t2
)
281 tree mv1
= TYPE_MAIN_VARIANT (t1
);
282 tree mv2
= TYPE_MAIN_VARIANT (t2
);
289 for (int i
= 0; i
< NUM_FLOATN_NX_TYPES
; ++i
)
291 if (mv1
== FLOATN_NX_TYPE_NODE (i
))
293 if (mv2
== FLOATN_NX_TYPE_NODE (i
))
296 if (mv1
== bfloat16_type_node
)
298 if (mv2
== bfloat16_type_node
)
300 if (extended2
&& !extended1
)
302 int ret
= cp_compare_floating_point_conversion_ranks (t2
, t1
);
303 return ret
== 3 ? 3 : -ret
;
306 const struct real_format
*fmt1
= REAL_MODE_FORMAT (TYPE_MODE (t1
));
307 const struct real_format
*fmt2
= REAL_MODE_FORMAT (TYPE_MODE (t2
));
308 gcc_assert (fmt1
->b
== 2 && fmt2
->b
== 2);
309 /* For {ibm,mips}_extended_format formats, the type has variable
310 precision up to ~2150 bits when the first double is around maximum
311 representable double and second double is subnormal minimum.
312 So, e.g. for __ibm128 vs. std::float128_t, they have unordered
314 int p1
= (MODE_COMPOSITE_P (TYPE_MODE (t1
))
315 ? fmt1
->emax
- fmt1
->emin
+ fmt1
->p
- 1 : fmt1
->p
);
316 int p2
= (MODE_COMPOSITE_P (TYPE_MODE (t2
))
317 ? fmt2
->emax
- fmt2
->emin
+ fmt2
->p
- 1 : fmt2
->p
);
318 /* The rank of a floating point type T is greater than the rank of
319 any floating-point type whose set of values is a proper subset
320 of the set of values of T. */
321 if ((p1
> p2
&& fmt1
->emax
>= fmt2
->emax
)
322 || (p1
== p2
&& fmt1
->emax
> fmt2
->emax
))
324 if ((p1
< p2
&& fmt1
->emax
<= fmt2
->emax
)
325 || (p1
== p2
&& fmt1
->emax
< fmt2
->emax
))
327 if ((p1
> p2
&& fmt1
->emax
< fmt2
->emax
)
328 || (p1
< p2
&& fmt1
->emax
> fmt2
->emax
))
330 if (!extended1
&& !extended2
)
332 /* The rank of long double is greater than the rank of double, which
333 is greater than the rank of float. */
334 if (t1
== long_double_type_node
)
336 else if (t2
== long_double_type_node
)
338 if (t1
== double_type_node
)
340 else if (t2
== double_type_node
)
342 if (t1
== float_type_node
)
344 else if (t2
== float_type_node
)
348 /* Two extended floating-point types with the same set of values have equal
350 if (extended1
&& extended2
)
352 if ((extended1
<= NUM_FLOATN_TYPES
) == (extended2
<= NUM_FLOATN_TYPES
))
354 /* Prefer higher extendedN value. */
355 if (extended1
> extended2
)
357 else if (extended1
< extended2
)
362 else if (extended1
<= NUM_FLOATN_TYPES
)
363 /* Prefer _FloatN type over _FloatMx type. */
365 else if (extended2
<= NUM_FLOATN_TYPES
)
371 /* gcc_assert (extended1 && !extended2); */
374 for (p
= &float_type_node
; p
<= &long_double_type_node
; ++p
)
376 const struct real_format
*fmt3
= REAL_MODE_FORMAT (TYPE_MODE (*p
));
377 gcc_assert (fmt3
->b
== 2);
378 int p3
= (MODE_COMPOSITE_P (TYPE_MODE (*p
))
379 ? fmt3
->emax
- fmt3
->emin
+ fmt3
->p
- 1 : fmt3
->p
);
380 if (p1
== p3
&& fmt1
->emax
== fmt3
->emax
)
383 /* An extended floating-point type with the same set of values
384 as exactly one cv-unqualified standard floating-point type
385 has a rank equal to the rank of that standard floating-point
388 An extended floating-point type with the same set of values
389 as more than one cv-unqualified standard floating-point type
390 has a rank equal to the rank of double.
392 Thus, if the latter is true and t2 is long double, t2
394 if (cnt
> 1 && mv2
== long_double_type_node
)
396 /* Otherwise, they have equal rank, but extended types
397 (other than std::bfloat16_t) have higher subrank.
398 std::bfloat16_t shouldn't have equal rank to any standard
399 floating point type. */
403 /* Return the common type for two arithmetic types T1 and T2 under the
404 usual arithmetic conversions. The default conversions have already
405 been applied, and enumerated types converted to their compatible
409 cp_common_type (tree t1
, tree t2
)
411 enum tree_code code1
= TREE_CODE (t1
);
412 enum tree_code code2
= TREE_CODE (t2
);
417 /* In what follows, we slightly generalize the rules given in [expr] so
418 as to deal with `long long' and `complex'. First, merge the
420 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
422 if (SCOPED_ENUM_P (t1
) || SCOPED_ENUM_P (t2
))
424 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
425 return build_type_attribute_variant (t1
, attributes
);
430 /* FIXME: Attributes. */
431 gcc_assert (ARITHMETIC_TYPE_P (t1
)
432 || VECTOR_TYPE_P (t1
)
433 || UNSCOPED_ENUM_P (t1
));
434 gcc_assert (ARITHMETIC_TYPE_P (t2
)
435 || VECTOR_TYPE_P (t2
)
436 || UNSCOPED_ENUM_P (t2
));
438 /* If one type is complex, form the common type of the non-complex
439 components, then make that complex. Use T1 or T2 if it is the
441 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
443 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
444 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
446 = type_after_usual_arithmetic_conversions (subtype1
, subtype2
);
448 if (subtype
== error_mark_node
)
450 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
451 return build_type_attribute_variant (t1
, attributes
);
452 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
453 return build_type_attribute_variant (t2
, attributes
);
455 return build_type_attribute_variant (build_complex_type (subtype
),
459 if (code1
== VECTOR_TYPE
)
461 /* When we get here we should have two vectors of the same size.
462 Just prefer the unsigned one if present. */
463 if (TYPE_UNSIGNED (t1
))
464 return merge_type_attributes_from (t1
, t2
);
466 return merge_type_attributes_from (t2
, t1
);
469 /* If only one is real, use it as the result. */
470 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
471 return build_type_attribute_variant (t1
, attributes
);
472 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
473 return build_type_attribute_variant (t2
, attributes
);
475 if (code1
== REAL_TYPE
476 && (extended_float_type_p (t1
) || extended_float_type_p (t2
)))
478 tree mv1
= TYPE_MAIN_VARIANT (t1
);
479 tree mv2
= TYPE_MAIN_VARIANT (t2
);
481 return build_type_attribute_variant (t1
, attributes
);
483 int cmpret
= cp_compare_floating_point_conversion_ranks (mv1
, mv2
);
485 return error_mark_node
;
486 else if (cmpret
>= 0)
487 return build_type_attribute_variant (t1
, attributes
);
489 return build_type_attribute_variant (t2
, attributes
);
492 /* Both real or both integers; use the one with greater precision. */
493 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
494 return build_type_attribute_variant (t1
, attributes
);
495 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
496 return build_type_attribute_variant (t2
, attributes
);
498 /* The types are the same; no need to do anything fancy. */
499 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
500 return build_type_attribute_variant (t1
, attributes
);
502 if (code1
!= REAL_TYPE
)
504 /* If one is unsigned long long, then convert the other to unsigned
506 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_unsigned_type_node
)
507 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_unsigned_type_node
))
508 return build_type_attribute_variant (long_long_unsigned_type_node
,
510 /* If one is a long long, and the other is an unsigned long, and
511 long long can represent all the values of an unsigned long, then
512 convert to a long long. Otherwise, convert to an unsigned long
513 long. Otherwise, if either operand is long long, convert the
516 Since we're here, we know the TYPE_PRECISION is the same;
517 therefore converting to long long cannot represent all the values
518 of an unsigned long, so we choose unsigned long long in that
520 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_integer_type_node
)
521 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_integer_type_node
))
523 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
524 ? long_long_unsigned_type_node
525 : long_long_integer_type_node
);
526 return build_type_attribute_variant (t
, attributes
);
529 /* Go through the same procedure, but for longs. */
530 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_unsigned_type_node
)
531 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_unsigned_type_node
))
532 return build_type_attribute_variant (long_unsigned_type_node
,
534 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_integer_type_node
)
535 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_integer_type_node
))
537 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
538 ? long_unsigned_type_node
: long_integer_type_node
);
539 return build_type_attribute_variant (t
, attributes
);
542 /* For __intN types, either the type is __int128 (and is lower
543 priority than the types checked above, but higher than other
544 128-bit types) or it's known to not be the same size as other
545 types (enforced in toplev.cc). Prefer the unsigned type. */
546 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
548 if (int_n_enabled_p
[i
]
549 && (same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].signed_type
)
550 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].signed_type
)
551 || same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].unsigned_type
)
552 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].unsigned_type
)))
554 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
555 ? int_n_trees
[i
].unsigned_type
556 : int_n_trees
[i
].signed_type
);
557 return build_type_attribute_variant (t
, attributes
);
561 /* Otherwise prefer the unsigned one. */
562 if (TYPE_UNSIGNED (t1
))
563 return build_type_attribute_variant (t1
, attributes
);
565 return build_type_attribute_variant (t2
, attributes
);
569 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_double_type_node
)
570 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_double_type_node
))
571 return build_type_attribute_variant (long_double_type_node
,
573 if (same_type_p (TYPE_MAIN_VARIANT (t1
), double_type_node
)
574 || same_type_p (TYPE_MAIN_VARIANT (t2
), double_type_node
))
575 return build_type_attribute_variant (double_type_node
,
577 if (same_type_p (TYPE_MAIN_VARIANT (t1
), float_type_node
)
578 || same_type_p (TYPE_MAIN_VARIANT (t2
), float_type_node
))
579 return build_type_attribute_variant (float_type_node
,
582 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
583 the standard C++ floating-point types. Logic earlier in this
584 function has already eliminated the possibility that
585 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
586 compelling reason to choose one or the other. */
587 return build_type_attribute_variant (t1
, attributes
);
591 /* T1 and T2 are arithmetic or enumeration types. Return the type
592 that will result from the "usual arithmetic conversions" on T1 and
593 T2 as described in [expr]. */
596 type_after_usual_arithmetic_conversions (tree t1
, tree t2
)
598 gcc_assert (ARITHMETIC_TYPE_P (t1
)
599 || VECTOR_TYPE_P (t1
)
600 || UNSCOPED_ENUM_P (t1
));
601 gcc_assert (ARITHMETIC_TYPE_P (t2
)
602 || VECTOR_TYPE_P (t2
)
603 || UNSCOPED_ENUM_P (t2
));
605 /* Perform the integral promotions. We do not promote real types here. */
606 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1
)
607 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2
))
609 t1
= type_promotes_to (t1
);
610 t2
= type_promotes_to (t2
);
613 return cp_common_type (t1
, t2
);
617 composite_pointer_error (const op_location_t
&location
,
618 diagnostic_t kind
, tree t1
, tree t2
,
619 composite_pointer_operation operation
)
624 emit_diagnostic (kind
, location
, 0,
625 "comparison between "
626 "distinct pointer types %qT and %qT lacks a cast",
630 emit_diagnostic (kind
, location
, 0,
631 "conversion between "
632 "distinct pointer types %qT and %qT lacks a cast",
635 case CPO_CONDITIONAL_EXPR
:
636 emit_diagnostic (kind
, location
, 0,
637 "conditional expression between "
638 "distinct pointer types %qT and %qT lacks a cast",
646 /* Subroutine of composite_pointer_type to implement the recursive
647 case. See that function for documentation of the parameters. And ADD_CONST
648 is used to track adding "const" where needed. */
651 composite_pointer_type_r (const op_location_t
&location
,
652 tree t1
, tree t2
, bool *add_const
,
653 composite_pointer_operation operation
,
654 tsubst_flags_t complain
)
661 /* Determine the types pointed to by T1 and T2. */
664 pointee1
= TREE_TYPE (t1
);
665 pointee2
= TREE_TYPE (t2
);
669 pointee1
= TYPE_PTRMEM_POINTED_TO_TYPE (t1
);
670 pointee2
= TYPE_PTRMEM_POINTED_TO_TYPE (t2
);
675 If T1 and T2 are similar types, the result is the cv-combined type of
677 if (same_type_ignoring_top_level_qualifiers_p (pointee1
, pointee2
))
678 result_type
= pointee1
;
679 else if ((TYPE_PTR_P (pointee1
) && TYPE_PTR_P (pointee2
))
680 || (TYPE_PTRMEM_P (pointee1
) && TYPE_PTRMEM_P (pointee2
)))
682 result_type
= composite_pointer_type_r (location
, pointee1
, pointee2
,
683 add_const
, operation
, complain
);
684 if (result_type
== error_mark_node
)
685 return error_mark_node
;
689 if (complain
& tf_error
)
690 composite_pointer_error (location
, DK_PERMERROR
,
693 return error_mark_node
;
694 result_type
= void_type_node
;
696 const int q1
= cp_type_quals (pointee1
);
697 const int q2
= cp_type_quals (pointee2
);
698 const int quals
= q1
| q2
;
699 result_type
= cp_build_qualified_type (result_type
,
702 : TYPE_UNQUALIFIED
)));
703 /* The cv-combined type can add "const" as per [conv.qual]/3.3 (except for
704 the TLQ). The reason is that both T1 and T2 can then be converted to the
705 cv-combined type of T1 and T2. */
706 if (quals
!= q1
|| quals
!= q2
)
708 /* If the original types were pointers to members, so is the
710 if (TYPE_PTRMEM_P (t1
))
712 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
713 TYPE_PTRMEM_CLASS_TYPE (t2
)))
715 if (complain
& tf_error
)
716 composite_pointer_error (location
, DK_PERMERROR
,
719 return error_mark_node
;
721 result_type
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
725 result_type
= build_pointer_type (result_type
);
727 /* Merge the attributes. */
728 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
729 return build_type_attribute_variant (result_type
, attributes
);
732 /* Return the composite pointer type (see [expr.type]) for T1 and T2.
733 ARG1 and ARG2 are the values with those types. The OPERATION is to
734 describe the operation between the pointer types,
735 in case an error occurs.
737 This routine also implements the computation of a common type for
738 pointers-to-members as per [expr.eq]. */
741 composite_pointer_type (const op_location_t
&location
,
742 tree t1
, tree t2
, tree arg1
, tree arg2
,
743 composite_pointer_operation operation
,
744 tsubst_flags_t complain
)
751 If one operand is a null pointer constant, the composite pointer
752 type is the type of the other operand. */
753 if (null_ptr_cst_p (arg1
))
755 if (null_ptr_cst_p (arg2
))
762 If one of the operands has type "pointer to cv1 void", then
763 the other has type "pointer to cv2 T", and the composite pointer
764 type is "pointer to cv12 void", where cv12 is the union of cv1
767 If either type is a pointer to void, make sure it is T1. */
768 if (TYPE_PTR_P (t2
) && VOID_TYPE_P (TREE_TYPE (t2
)))
771 /* Now, if T1 is a pointer to void, merge the qualifiers. */
772 if (TYPE_PTR_P (t1
) && VOID_TYPE_P (TREE_TYPE (t1
)))
777 if (TYPE_PTRFN_P (t2
))
779 if (complain
& tf_error
)
784 pedwarn (location
, OPT_Wpedantic
,
785 "ISO C++ forbids comparison between pointer "
786 "of type %<void *%> and pointer-to-function");
789 pedwarn (location
, OPT_Wpedantic
,
790 "ISO C++ forbids conversion between pointer "
791 "of type %<void *%> and pointer-to-function");
793 case CPO_CONDITIONAL_EXPR
:
794 pedwarn (location
, OPT_Wpedantic
,
795 "ISO C++ forbids conditional expression between "
796 "pointer of type %<void *%> and "
797 "pointer-to-function");
804 return error_mark_node
;
807 = cp_build_qualified_type (void_type_node
,
808 (cp_type_quals (TREE_TYPE (t1
))
809 | cp_type_quals (TREE_TYPE (t2
))));
810 result_type
= build_pointer_type (result_type
);
811 /* Merge the attributes. */
812 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
813 return build_type_attribute_variant (result_type
, attributes
);
816 if (c_dialect_objc () && TYPE_PTR_P (t1
)
819 if (objc_have_common_type (t1
, t2
, -3, NULL_TREE
))
820 return objc_common_type (t1
, t2
);
823 /* if T1 or T2 is "pointer to noexcept function" and the other type is
824 "pointer to function", where the function types are otherwise the same,
825 "pointer to function" */
826 if (fnptr_conv_p (t1
, t2
))
828 if (fnptr_conv_p (t2
, t1
))
831 /* [expr.eq] permits the application of a pointer conversion to
832 bring the pointers to a common type. */
833 if (TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
)
834 && CLASS_TYPE_P (TREE_TYPE (t1
))
835 && CLASS_TYPE_P (TREE_TYPE (t2
))
836 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1
),
839 class1
= TREE_TYPE (t1
);
840 class2
= TREE_TYPE (t2
);
842 if (DERIVED_FROM_P (class1
, class2
))
843 t2
= (build_pointer_type
844 (cp_build_qualified_type (class1
, cp_type_quals (class2
))));
845 else if (DERIVED_FROM_P (class2
, class1
))
846 t1
= (build_pointer_type
847 (cp_build_qualified_type (class2
, cp_type_quals (class1
))));
850 if (complain
& tf_error
)
851 composite_pointer_error (location
, DK_ERROR
, t1
, t2
, operation
);
852 return error_mark_node
;
855 /* [expr.eq] permits the application of a pointer-to-member
856 conversion to change the class type of one of the types. */
857 else if (TYPE_PTRMEM_P (t1
)
858 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
859 TYPE_PTRMEM_CLASS_TYPE (t2
)))
861 class1
= TYPE_PTRMEM_CLASS_TYPE (t1
);
862 class2
= TYPE_PTRMEM_CLASS_TYPE (t2
);
864 if (DERIVED_FROM_P (class1
, class2
))
865 t1
= build_ptrmem_type (class2
, TYPE_PTRMEM_POINTED_TO_TYPE (t1
));
866 else if (DERIVED_FROM_P (class2
, class1
))
867 t2
= build_ptrmem_type (class1
, TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
870 if (complain
& tf_error
)
874 error_at (location
, "comparison between distinct "
875 "pointer-to-member types %qT and %qT lacks a cast",
879 error_at (location
, "conversion between distinct "
880 "pointer-to-member types %qT and %qT lacks a cast",
883 case CPO_CONDITIONAL_EXPR
:
884 error_at (location
, "conditional expression between distinct "
885 "pointer-to-member types %qT and %qT lacks a cast",
891 return error_mark_node
;
895 bool add_const
= false;
896 return composite_pointer_type_r (location
, t1
, t2
, &add_const
, operation
,
900 /* Return the merged type of two types.
901 We assume that comptypes has already been done and returned 1;
902 if that isn't so, this may crash.
904 This just combines attributes and default arguments; any other
905 differences would cause the two types to compare unalike. */
908 merge_types (tree t1
, tree t2
)
910 enum tree_code code1
;
911 enum tree_code code2
;
914 /* Save time if the two types are the same. */
917 if (original_type (t1
) == original_type (t2
))
920 /* If one type is nonsense, use the other. */
921 if (t1
== error_mark_node
)
923 if (t2
== error_mark_node
)
926 /* Handle merging an auto redeclaration with a previous deduced
931 /* Merge the attributes. */
932 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
934 if (TYPE_PTRMEMFUNC_P (t1
))
935 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
936 if (TYPE_PTRMEMFUNC_P (t2
))
937 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
939 code1
= TREE_CODE (t1
);
940 code2
= TREE_CODE (t2
);
943 gcc_assert (code1
== TYPENAME_TYPE
|| code2
== TYPENAME_TYPE
);
944 if (code1
== TYPENAME_TYPE
)
946 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
947 code1
= TREE_CODE (t1
);
951 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
952 code2
= TREE_CODE (t2
);
960 /* For two pointers, do this recursively on the target type. */
962 tree target
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
963 int quals
= cp_type_quals (t1
);
965 if (code1
== POINTER_TYPE
)
967 t1
= build_pointer_type (target
);
968 if (TREE_CODE (target
) == METHOD_TYPE
)
969 t1
= build_ptrmemfunc_type (t1
);
972 t1
= cp_build_reference_type (target
, TYPE_REF_IS_RVALUE (t1
));
973 t1
= build_type_attribute_variant (t1
, attributes
);
974 t1
= cp_build_qualified_type (t1
, quals
);
983 quals
= cp_type_quals (t1
);
984 pointee
= merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1
),
985 TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
986 t1
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
988 t1
= cp_build_qualified_type (t1
, quals
);
994 tree elt
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
995 /* Save space: see if the result is identical to one of the args. */
996 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
997 return build_type_attribute_variant (t1
, attributes
);
998 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
999 return build_type_attribute_variant (t2
, attributes
);
1000 /* Merge the element types, and have a size if either arg has one. */
1001 t1
= build_cplus_array_type
1002 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
1007 /* Function types: prefer the one that specified arg types.
1008 If both do, merge the arg types. Also merge the return types. */
1010 tree valtype
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
1011 tree p1
= TYPE_ARG_TYPES (t1
);
1012 tree p2
= TYPE_ARG_TYPES (t2
);
1015 /* Save space: see if the result is identical to one of the args. */
1016 if (valtype
== TREE_TYPE (t1
) && ! p2
)
1017 return cp_build_type_attribute_variant (t1
, attributes
);
1018 if (valtype
== TREE_TYPE (t2
) && ! p1
)
1019 return cp_build_type_attribute_variant (t2
, attributes
);
1021 /* Simple way if one arg fails to specify argument types. */
1022 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
1024 else if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
1027 parms
= commonparms (p1
, p2
);
1029 cp_cv_quals quals
= type_memfn_quals (t1
);
1030 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
1031 gcc_assert (quals
== type_memfn_quals (t2
));
1032 gcc_assert (rqual
== type_memfn_rqual (t2
));
1034 tree rval
= build_function_type (valtype
, parms
);
1035 rval
= apply_memfn_quals (rval
, quals
);
1036 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
1037 TYPE_RAISES_EXCEPTIONS (t2
));
1038 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
1039 t1
= build_cp_fntype_variant (rval
, rqual
, raises
, late_return_type_p
);
1045 /* Get this value the long way, since TYPE_METHOD_BASETYPE
1046 is just the main variant of this. */
1047 tree basetype
= class_of_this_parm (t2
);
1048 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
1049 TYPE_RAISES_EXCEPTIONS (t2
));
1050 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
1052 bool late_return_type_1_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
1054 /* If this was a member function type, get back to the
1055 original type of type member function (i.e., without
1056 the class instance variable up front. */
1057 t1
= build_function_type (TREE_TYPE (t1
),
1058 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
1059 t2
= build_function_type (TREE_TYPE (t2
),
1060 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
1061 t3
= merge_types (t1
, t2
);
1062 t3
= build_method_type_directly (basetype
, TREE_TYPE (t3
),
1063 TYPE_ARG_TYPES (t3
));
1064 t1
= build_cp_fntype_variant (t3
, rqual
, raises
, late_return_type_1_p
);
1069 /* There is no need to merge attributes into a TYPENAME_TYPE.
1070 When the type is instantiated it will have whatever
1071 attributes result from the instantiation. */
1075 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
1077 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
1082 return cp_build_type_attribute_variant (t1
, attributes
);
1085 /* Return the ARRAY_TYPE type without its domain. */
1088 strip_array_domain (tree type
)
1091 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1092 if (TYPE_DOMAIN (type
) == NULL_TREE
)
1094 t2
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
1095 return cp_build_type_attribute_variant (t2
, TYPE_ATTRIBUTES (type
));
1098 /* Wrapper around cp_common_type that is used by c-common.cc and other
1099 front end optimizations that remove promotions.
1101 Return the common type for two arithmetic types T1 and T2 under the
1102 usual arithmetic conversions. The default conversions have already
1103 been applied, and enumerated types converted to their compatible
1107 common_type (tree t1
, tree t2
)
1109 /* If one type is nonsense, use the other */
1110 if (t1
== error_mark_node
)
1112 if (t2
== error_mark_node
)
1115 return cp_common_type (t1
, t2
);
1118 /* Return the common type of two pointer types T1 and T2. This is the
1119 type for the result of most arithmetic operations if the operands
1120 have the given two types.
1122 We assume that comp_target_types has already been done and returned
1123 nonzero; if that isn't so, this may crash. */
1126 common_pointer_type (tree t1
, tree t2
)
1128 gcc_assert ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
1129 || (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
1130 || (TYPE_PTRMEMFUNC_P (t1
) && TYPE_PTRMEMFUNC_P (t2
)));
1132 return composite_pointer_type (input_location
, t1
, t2
,
1133 error_mark_node
, error_mark_node
,
1134 CPO_CONVERSION
, tf_warning_or_error
);
1137 /* Compare two exception specifier types for exactness or subsetness, if
1138 allowed. Returns false for mismatch, true for match (same, or
1139 derived and !exact).
1141 [except.spec] "If a class X ... objects of class X or any class publicly
1142 and unambiguously derived from X. Similarly, if a pointer type Y * ...
1143 exceptions of type Y * or that are pointers to any type publicly and
1144 unambiguously derived from Y. Otherwise a function only allows exceptions
1145 that have the same type ..."
1146 This does not mention cv qualifiers and is different to what throw
1147 [except.throw] and catch [except.catch] will do. They will ignore the
1148 top level cv qualifiers, and allow qualifiers in the pointer to class
1151 We implement the letter of the standard. */
1154 comp_except_types (tree a
, tree b
, bool exact
)
1156 if (same_type_p (a
, b
))
1160 if (cp_type_quals (a
) || cp_type_quals (b
))
1163 if (TYPE_PTR_P (a
) && TYPE_PTR_P (b
))
1167 if (cp_type_quals (a
) || cp_type_quals (b
))
1171 if (TREE_CODE (a
) != RECORD_TYPE
1172 || TREE_CODE (b
) != RECORD_TYPE
)
1175 if (publicly_uniquely_derived_p (a
, b
))
1181 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1182 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1183 If EXACT is ce_type, the C++17 type compatibility rules apply.
1184 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1185 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1186 are unordered, but we've already filtered out duplicates. Most lists will
1187 be in order, we should try to make use of that. */
1190 comp_except_specs (const_tree t1
, const_tree t2
, int exact
)
1199 /* First handle noexcept. */
1200 if (exact
< ce_exact
)
1202 if (exact
== ce_type
1203 && (canonical_eh_spec (CONST_CAST_TREE (t1
))
1204 == canonical_eh_spec (CONST_CAST_TREE (t2
))))
1207 /* noexcept(false) is compatible with no exception-specification,
1208 and less strict than any spec. */
1209 if (t1
== noexcept_false_spec
)
1210 return t2
== NULL_TREE
|| exact
== ce_derived
;
1211 /* Even a derived noexcept(false) is compatible with no
1212 exception-specification. */
1213 if (t2
== noexcept_false_spec
)
1214 return t1
== NULL_TREE
;
1216 /* Otherwise, if we aren't looking for an exact match, noexcept is
1217 equivalent to throw(). */
1218 if (t1
== noexcept_true_spec
)
1219 t1
= empty_except_spec
;
1220 if (t2
== noexcept_true_spec
)
1221 t2
= empty_except_spec
;
1224 /* If any noexcept is left, it is only comparable to itself;
1225 either we're looking for an exact match or we're redeclaring a
1226 template with dependent noexcept. */
1227 if ((t1
&& TREE_PURPOSE (t1
))
1228 || (t2
&& TREE_PURPOSE (t2
)))
1230 && cp_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)));
1232 if (t1
== NULL_TREE
) /* T1 is ... */
1233 return t2
== NULL_TREE
|| exact
== ce_derived
;
1234 if (!TREE_VALUE (t1
)) /* t1 is EMPTY */
1235 return t2
!= NULL_TREE
&& !TREE_VALUE (t2
);
1236 if (t2
== NULL_TREE
) /* T2 is ... */
1238 if (TREE_VALUE (t1
) && !TREE_VALUE (t2
)) /* T2 is EMPTY, T1 is not */
1239 return exact
== ce_derived
;
1241 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1242 Count how many we find, to determine exactness. For exact matching and
1243 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1245 for (base
= t1
; t2
!= NULL_TREE
; t2
= TREE_CHAIN (t2
))
1247 for (probe
= base
; probe
!= NULL_TREE
; probe
= TREE_CHAIN (probe
))
1249 tree a
= TREE_VALUE (probe
);
1250 tree b
= TREE_VALUE (t2
);
1252 if (comp_except_types (a
, b
, exact
))
1254 if (probe
== base
&& exact
> ce_derived
)
1255 base
= TREE_CHAIN (probe
);
1260 if (probe
== NULL_TREE
)
1263 return exact
== ce_derived
|| base
== NULL_TREE
|| length
== list_length (t1
);
1266 /* Compare the array types T1 and T2. CB says how we should behave when
1267 comparing array bounds: bounds_none doesn't allow dimensionless arrays,
1268 bounds_either says than any array can be [], bounds_first means that
1269 onlt T1 can be an array with unknown bounds. STRICT is true if
1270 qualifiers must match when comparing the types of the array elements. */
1273 comp_array_types (const_tree t1
, const_tree t2
, compare_bounds_t cb
,
1283 /* The type of the array elements must be the same. */
1285 ? !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
))
1286 : !similar_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1289 d1
= TYPE_DOMAIN (t1
);
1290 d2
= TYPE_DOMAIN (t2
);
1295 /* If one of the arrays is dimensionless, and the other has a
1296 dimension, they are of different types. However, it is valid to
1304 declarations for an array object can specify
1305 array types that differ by the presence or absence of a major
1306 array bound (_dcl.array_). */
1308 return cb
>= bounds_either
;
1310 return cb
== bounds_either
;
1312 /* Check that the dimensions are the same. */
1314 if (!cp_tree_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
)))
1316 max1
= TYPE_MAX_VALUE (d1
);
1317 max2
= TYPE_MAX_VALUE (d2
);
1319 if (!cp_tree_equal (max1
, max2
))
1325 /* Compare the relative position of T1 and T2 into their respective
1326 template parameter list.
1327 T1 and T2 must be template parameter types.
1328 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1331 comp_template_parms_position (tree t1
, tree t2
)
1333 tree index1
, index2
;
1334 gcc_assert (t1
&& t2
1335 && TREE_CODE (t1
) == TREE_CODE (t2
)
1336 && (TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
1337 || TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
1338 || TREE_CODE (t1
) == TEMPLATE_TYPE_PARM
));
1340 index1
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1
));
1341 index2
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2
));
1343 /* Then compare their relative position. */
1344 if (TEMPLATE_PARM_IDX (index1
) != TEMPLATE_PARM_IDX (index2
)
1345 || TEMPLATE_PARM_LEVEL (index1
) != TEMPLATE_PARM_LEVEL (index2
)
1346 || (TEMPLATE_PARM_PARAMETER_PACK (index1
)
1347 != TEMPLATE_PARM_PARAMETER_PACK (index2
)))
1350 /* In C++14 we can end up comparing 'auto' to a normal template
1351 parameter. Don't confuse them. */
1352 if (cxx_dialect
>= cxx14
&& (is_auto (t1
) || is_auto (t2
)))
1353 return TYPE_IDENTIFIER (t1
) == TYPE_IDENTIFIER (t2
);
1358 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1361 cxx_safe_arg_type_equiv_p (tree t1
, tree t2
)
1363 t1
= TYPE_MAIN_VARIANT (t1
);
1364 t2
= TYPE_MAIN_VARIANT (t2
);
1370 /* The signedness of the parameter matters only when an integral
1371 type smaller than int is promoted to int, otherwise only the
1372 precision of the parameter matters.
1373 This check should make sure that the callee does not see
1374 undefined values in argument registers. */
1375 if (INTEGRAL_TYPE_P (t1
)
1376 && INTEGRAL_TYPE_P (t2
)
1377 && TYPE_PRECISION (t1
) == TYPE_PRECISION (t2
)
1378 && (TYPE_UNSIGNED (t1
) == TYPE_UNSIGNED (t2
)
1379 || !targetm
.calls
.promote_prototypes (NULL_TREE
)
1380 || TYPE_PRECISION (t1
) >= TYPE_PRECISION (integer_type_node
)))
1383 return same_type_p (t1
, t2
);
1386 /* Check if a type cast between two function types can be considered safe. */
1389 cxx_safe_function_type_cast_p (tree t1
, tree t2
)
1391 if (TREE_TYPE (t1
) == void_type_node
&&
1392 TYPE_ARG_TYPES (t1
) == void_list_node
)
1395 if (TREE_TYPE (t2
) == void_type_node
&&
1396 TYPE_ARG_TYPES (t2
) == void_list_node
)
1399 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1402 for (t1
= TYPE_ARG_TYPES (t1
), t2
= TYPE_ARG_TYPES (t2
);
1404 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1405 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1411 /* Subroutine in comptypes. */
1414 structural_comptypes (tree t1
, tree t2
, int strict
)
1416 /* Both should be types that are not obviously the same. */
1417 gcc_checking_assert (t1
!= t2
&& TYPE_P (t1
) && TYPE_P (t2
));
1419 /* Suppress typename resolution under spec_hasher::equal in place of calling
1420 push_to_top_level there. */
1421 if (!comparing_specializations
)
1423 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1424 current instantiation. */
1425 if (TREE_CODE (t1
) == TYPENAME_TYPE
)
1426 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
1428 if (TREE_CODE (t2
) == TYPENAME_TYPE
)
1429 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
1432 if (TYPE_PTRMEMFUNC_P (t1
))
1433 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
1434 if (TYPE_PTRMEMFUNC_P (t2
))
1435 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
1437 /* Different classes of types can't be compatible. */
1438 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1441 /* Qualifiers must match. For array types, we will check when we
1442 recur on the array element types. */
1443 if (TREE_CODE (t1
) != ARRAY_TYPE
1444 && cp_type_quals (t1
) != cp_type_quals (t2
))
1446 if (TREE_CODE (t1
) == FUNCTION_TYPE
1447 && type_memfn_quals (t1
) != type_memfn_quals (t2
))
1449 /* Need to check this before TYPE_MAIN_VARIANT.
1450 FIXME function qualifiers should really change the main variant. */
1451 if (FUNC_OR_METHOD_TYPE_P (t1
))
1453 if (type_memfn_rqual (t1
) != type_memfn_rqual (t2
))
1455 if (flag_noexcept_type
1456 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1
),
1457 TYPE_RAISES_EXCEPTIONS (t2
),
1462 /* Allow for two different type nodes which have essentially the same
1463 definition. Note that we already checked for equality of the type
1464 qualifiers (just above). */
1465 if (TREE_CODE (t1
) != ARRAY_TYPE
1466 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1469 /* Compare the types. Return false on known not-same. Break on not
1470 known. Never return true from this switch -- you'll break
1471 specialization comparison. */
1472 switch (TREE_CODE (t1
))
1476 /* All void and bool types are the same. */
1481 case FIXED_POINT_TYPE
:
1483 /* With these nodes, we can't determine type equivalence by
1484 looking at what is stored in the nodes themselves, because
1485 two nodes might have different TYPE_MAIN_VARIANTs but still
1486 represent the same type. For example, wchar_t and int could
1487 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1488 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1489 and are distinct types. On the other hand, int and the
1492 typedef int INT __attribute((may_alias));
1494 have identical properties, different TYPE_MAIN_VARIANTs, but
1495 represent the same type. The canonical type system keeps
1496 track of equivalence in this case, so we fall back on it. */
1497 if (TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1500 /* We don't need or want the attribute comparison. */
1503 case TEMPLATE_TEMPLATE_PARM
:
1504 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1505 if (!comp_template_parms_position (t1
, t2
))
1507 if (!comp_template_parms
1508 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1
)),
1509 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2
))))
1511 if (TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
)
1513 /* Don't check inheritance. */
1514 strict
= COMPARE_STRICT
;
1519 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
1520 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
1521 || TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1522 && comp_template_args (TYPE_TI_ARGS (t1
), TYPE_TI_ARGS (t2
)))
1525 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
1527 else if ((strict
& COMPARE_DERIVED
) && DERIVED_FROM_P (t2
, t1
))
1533 if (!comptypes (TYPE_OFFSET_BASETYPE (t1
), TYPE_OFFSET_BASETYPE (t2
),
1534 strict
& ~COMPARE_REDECLARATION
))
1536 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1540 case REFERENCE_TYPE
:
1541 if (TYPE_REF_IS_RVALUE (t1
) != TYPE_REF_IS_RVALUE (t2
))
1543 /* fall through to checks for pointer types */
1547 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1548 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1554 /* Exception specs and memfn_rquals were checked above. */
1555 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1557 if (!compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)))
1562 /* Target types must match incl. qualifiers. */
1563 if (!comp_array_types (t1
, t2
, ((strict
& COMPARE_REDECLARATION
)
1564 ? bounds_either
: bounds_none
),
1569 case TEMPLATE_TYPE_PARM
:
1570 /* If T1 and T2 don't have the same relative position in their
1571 template parameters set, they can't be equal. */
1572 if (!comp_template_parms_position (t1
, t2
))
1574 /* If T1 and T2 don't represent the same class template deduction,
1575 they aren't equal. */
1576 if (CLASS_PLACEHOLDER_TEMPLATE (t1
)
1577 != CLASS_PLACEHOLDER_TEMPLATE (t2
))
1579 /* Constrained 'auto's are distinct from parms that don't have the same
1581 if (!equivalent_placeholder_constraints (t1
, t2
))
1586 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1
),
1587 TYPENAME_TYPE_FULLNAME (t2
)))
1589 /* Qualifiers don't matter on scopes. */
1590 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1
),
1595 case UNBOUND_CLASS_TEMPLATE
:
1596 if (!cp_tree_equal (TYPE_IDENTIFIER (t1
), TYPE_IDENTIFIER (t2
)))
1598 if (!same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
)))
1603 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1608 if (gnu_vector_type_p (t1
) != gnu_vector_type_p (t2
)
1609 || maybe_ne (TYPE_VECTOR_SUBPARTS (t1
), TYPE_VECTOR_SUBPARTS (t2
))
1610 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1614 case TYPE_PACK_EXPANSION
:
1615 return (same_type_p (PACK_EXPANSION_PATTERN (t1
),
1616 PACK_EXPANSION_PATTERN (t2
))
1617 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1
),
1618 PACK_EXPANSION_EXTRA_ARGS (t2
)));
1621 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1
)
1622 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2
))
1624 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1
) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2
))
1626 if (DECLTYPE_FOR_LAMBDA_PROXY (t1
) != DECLTYPE_FOR_LAMBDA_PROXY (t2
))
1628 if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1
), DECLTYPE_TYPE_EXPR (t2
)))
1633 if (TRAIT_TYPE_KIND (t1
) != TRAIT_TYPE_KIND (t2
))
1635 if (!same_type_p (TRAIT_TYPE_TYPE1 (t1
), TRAIT_TYPE_TYPE1 (t2
))
1636 || !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1
), TRAIT_TYPE_TYPE2 (t2
)))
1641 if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1
), TYPEOF_TYPE_EXPR (t2
)))
1649 /* If we get here, we know that from a target independent POV the
1650 types are the same. Make sure the target attributes are also
1652 if (!comp_type_attributes (t1
, t2
))
1656 if (comparing_dependent_aliases
)
1658 /* Don't treat an alias template specialization with dependent
1659 arguments as equivalent to its underlying type when used as a
1660 template argument; we need them to be distinct so that we
1661 substitute into the specialization arguments at instantiation
1662 time. And aliases can't be equivalent without being ==, so
1663 we don't need to look any deeper. */
1664 ++processing_template_decl
;
1665 tree dep1
= dependent_alias_template_spec_p (t1
, nt_transparent
);
1666 tree dep2
= dependent_alias_template_spec_p (t2
, nt_transparent
);
1667 --processing_template_decl
;
1668 if ((dep1
|| dep2
) && dep1
!= dep2
)
1675 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1676 is a bitwise-or of the COMPARE_* flags. */
1679 comptypes (tree t1
, tree t2
, int strict
)
1681 gcc_checking_assert (t1
&& t2
);
1683 /* TYPE_ARGUMENT_PACKS are not really types. */
1684 gcc_checking_assert (TREE_CODE (t1
) != TYPE_ARGUMENT_PACK
1685 && TREE_CODE (t2
) != TYPE_ARGUMENT_PACK
);
1690 /* Suppress errors caused by previously reported errors. */
1691 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1694 if (strict
== COMPARE_STRICT
)
1696 if (TYPE_STRUCTURAL_EQUALITY_P (t1
) || TYPE_STRUCTURAL_EQUALITY_P (t2
))
1697 /* At least one of the types requires structural equality, so
1698 perform a deep check. */
1699 return structural_comptypes (t1
, t2
, strict
);
1701 if (flag_checking
&& param_use_canonical_types
)
1703 bool result
= structural_comptypes (t1
, t2
, strict
);
1705 if (result
&& TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1706 /* The two types are structurally equivalent, but their
1707 canonical types were different. This is a failure of the
1708 canonical type propagation code.*/
1710 ("canonical types differ for identical types %qT and %qT",
1712 else if (!result
&& TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
))
1713 /* Two types are structurally different, but the canonical
1714 types are the same. This means we were over-eager in
1715 assigning canonical types. */
1717 ("same canonical type node for different types %qT and %qT",
1722 if (!flag_checking
&& param_use_canonical_types
)
1723 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1725 return structural_comptypes (t1
, t2
, strict
);
1727 else if (strict
== COMPARE_STRUCTURAL
)
1728 return structural_comptypes (t1
, t2
, COMPARE_STRICT
);
1730 return structural_comptypes (t1
, t2
, strict
);
1733 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1734 top-level qualifiers. */
1737 same_type_ignoring_top_level_qualifiers_p (tree type1
, tree type2
)
1739 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1744 type1
= cp_build_qualified_type (type1
, TYPE_UNQUALIFIED
);
1745 type2
= cp_build_qualified_type (type2
, TYPE_UNQUALIFIED
);
1746 return same_type_p (type1
, type2
);
1749 /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1752 similar_type_p (tree type1
, tree type2
)
1754 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1757 /* Informally, two types are similar if, ignoring top-level cv-qualification:
1758 * they are the same type; or
1759 * they are both pointers, and the pointed-to types are similar; or
1760 * they are both pointers to member of the same class, and the types of
1761 the pointed-to members are similar; or
1762 * they are both arrays of the same size or both arrays of unknown bound,
1763 and the array element types are similar. */
1765 if (same_type_ignoring_top_level_qualifiers_p (type1
, type2
))
1768 if ((TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
1769 || (TYPE_PTRDATAMEM_P (type1
) && TYPE_PTRDATAMEM_P (type2
))
1770 || (TREE_CODE (type1
) == ARRAY_TYPE
&& TREE_CODE (type2
) == ARRAY_TYPE
))
1771 return comp_ptr_ttypes_const (type1
, type2
, bounds_either
);
1776 /* Helper function for layout_compatible_type_p and
1777 is_corresponding_member_aggr. Advance to next members (NULL if
1778 no further ones) and return true if those members are still part of
1779 the common initial sequence. */
1782 next_common_initial_sequence (tree
&memb1
, tree
&memb2
)
1786 if (TREE_CODE (memb1
) != FIELD_DECL
1787 || (DECL_FIELD_IS_BASE (memb1
) && is_empty_field (memb1
)))
1789 memb1
= DECL_CHAIN (memb1
);
1792 if (DECL_FIELD_IS_BASE (memb1
))
1794 memb1
= TYPE_FIELDS (TREE_TYPE (memb1
));
1801 if (TREE_CODE (memb2
) != FIELD_DECL
1802 || (DECL_FIELD_IS_BASE (memb2
) && is_empty_field (memb2
)))
1804 memb2
= DECL_CHAIN (memb2
);
1807 if (DECL_FIELD_IS_BASE (memb2
))
1809 memb2
= TYPE_FIELDS (TREE_TYPE (memb2
));
1814 if (memb1
== NULL_TREE
&& memb2
== NULL_TREE
)
1816 if (memb1
== NULL_TREE
|| memb2
== NULL_TREE
)
1818 if (DECL_BIT_FIELD_TYPE (memb1
))
1820 if (!DECL_BIT_FIELD_TYPE (memb2
))
1822 if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1
),
1823 DECL_BIT_FIELD_TYPE (memb2
)))
1825 if (TYPE_PRECISION (TREE_TYPE (memb1
))
1826 != TYPE_PRECISION (TREE_TYPE (memb2
)))
1829 else if (DECL_BIT_FIELD_TYPE (memb2
))
1831 else if (!layout_compatible_type_p (TREE_TYPE (memb1
), TREE_TYPE (memb2
)))
1833 if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1
)))
1834 != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2
)))
1836 if (DECL_ALIGN (memb1
) != DECL_ALIGN (memb2
))
1838 if (!tree_int_cst_equal (bit_position (memb1
), bit_position (memb2
)))
1843 /* Return true if TYPE1 and TYPE2 are layout-compatible types. */
1846 layout_compatible_type_p (tree type1
, tree type2
)
1848 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1852 if (TREE_CODE (type1
) != TREE_CODE (type2
))
1855 type1
= cp_build_qualified_type (type1
, TYPE_UNQUALIFIED
);
1856 type2
= cp_build_qualified_type (type2
, TYPE_UNQUALIFIED
);
1858 if (TREE_CODE (type1
) == ENUMERAL_TYPE
)
1859 return (tree_int_cst_equal (TYPE_SIZE (type1
), TYPE_SIZE (type2
))
1860 && same_type_p (finish_underlying_type (type1
),
1861 finish_underlying_type (type2
)));
1863 if (CLASS_TYPE_P (type1
)
1864 && std_layout_type_p (type1
)
1865 && std_layout_type_p (type2
)
1866 && tree_int_cst_equal (TYPE_SIZE (type1
), TYPE_SIZE (type2
)))
1868 tree field1
= TYPE_FIELDS (type1
);
1869 tree field2
= TYPE_FIELDS (type2
);
1870 if (TREE_CODE (type1
) == RECORD_TYPE
)
1874 if (!next_common_initial_sequence (field1
, field2
))
1876 if (field1
== NULL_TREE
)
1878 field1
= DECL_CHAIN (field1
);
1879 field2
= DECL_CHAIN (field2
);
1882 /* Otherwise both types must be union types.
1884 "Two standard-layout unions are layout-compatible if they have
1885 the same number of non-static data members and corresponding
1886 non-static data members (in any order) have layout-compatible
1888 but the code anticipates that bitfield vs. non-bitfield,
1889 different bitfield widths or presence/absence of
1890 [[no_unique_address]] should be checked as well. */
1891 auto_vec
<tree
, 16> vec
;
1892 unsigned int count
= 0;
1893 for (; field1
; field1
= DECL_CHAIN (field1
))
1894 if (TREE_CODE (field1
) == FIELD_DECL
)
1896 for (; field2
; field2
= DECL_CHAIN (field2
))
1897 if (TREE_CODE (field2
) == FIELD_DECL
)
1898 vec
.safe_push (field2
);
1899 /* Discussions on core lean towards treating multiple union fields
1900 of the same type as the same field, so this might need changing
1902 if (count
!= vec
.length ())
1904 for (field1
= TYPE_FIELDS (type1
); field1
; field1
= DECL_CHAIN (field1
))
1906 if (TREE_CODE (field1
) != FIELD_DECL
)
1909 tree t1
= DECL_BIT_FIELD_TYPE (field1
);
1910 if (t1
== NULL_TREE
)
1911 t1
= TREE_TYPE (field1
);
1912 FOR_EACH_VEC_ELT (vec
, j
, field2
)
1914 tree t2
= DECL_BIT_FIELD_TYPE (field2
);
1915 if (t2
== NULL_TREE
)
1916 t2
= TREE_TYPE (field2
);
1917 if (DECL_BIT_FIELD_TYPE (field1
))
1919 if (!DECL_BIT_FIELD_TYPE (field2
))
1921 if (TYPE_PRECISION (TREE_TYPE (field1
))
1922 != TYPE_PRECISION (TREE_TYPE (field2
)))
1925 else if (DECL_BIT_FIELD_TYPE (field2
))
1927 if (!layout_compatible_type_p (t1
, t2
))
1929 if ((!lookup_attribute ("no_unique_address",
1930 DECL_ATTRIBUTES (field1
)))
1931 != !lookup_attribute ("no_unique_address",
1932 DECL_ATTRIBUTES (field2
)))
1936 if (j
== vec
.length ())
1938 vec
.unordered_remove (j
);
1943 return same_type_p (type1
, type2
);
1946 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1949 at_least_as_qualified_p (const_tree type1
, const_tree type2
)
1951 int q1
= cp_type_quals (type1
);
1952 int q2
= cp_type_quals (type2
);
1954 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1955 return (q1
& q2
) == q2
;
1958 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1959 more cv-qualified that TYPE1, and 0 otherwise. */
1962 comp_cv_qualification (int q1
, int q2
)
1967 if ((q1
& q2
) == q2
)
1969 else if ((q1
& q2
) == q1
)
1976 comp_cv_qualification (const_tree type1
, const_tree type2
)
1978 int q1
= cp_type_quals (type1
);
1979 int q2
= cp_type_quals (type2
);
1980 return comp_cv_qualification (q1
, q2
);
1983 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1984 subset of the cv-qualification signature of TYPE2, and the types
1985 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1988 comp_cv_qual_signature (tree type1
, tree type2
)
1990 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1992 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1998 /* Subroutines of `comptypes'. */
2000 /* Return true if two parameter type lists PARMS1 and PARMS2 are
2001 equivalent in the sense that functions with those parameter types
2002 can have equivalent types. The two lists must be equivalent,
2003 element by element. */
2006 compparms (const_tree parms1
, const_tree parms2
)
2010 /* An unspecified parmlist matches any specified parmlist
2011 whose argument types don't need default promotions. */
2013 for (t1
= parms1
, t2
= parms2
;
2015 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
2017 /* If one parmlist is shorter than the other,
2018 they fail to match. */
2021 if (!same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
2028 /* Process a sizeof or alignof expression where the operand is a type.
2029 STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
2030 or GNU (preferred alignment) semantics; it is ignored if OP is
2034 cxx_sizeof_or_alignof_type (location_t loc
, tree type
, enum tree_code op
,
2035 bool std_alignof
, bool complain
)
2037 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
2038 if (type
== error_mark_node
)
2039 return error_mark_node
;
2041 type
= non_reference (type
);
2042 if (TREE_CODE (type
) == METHOD_TYPE
)
2046 pedwarn (loc
, OPT_Wpointer_arith
,
2047 "invalid application of %qs to a member function",
2048 OVL_OP_INFO (false, op
)->name
);
2049 return size_one_node
;
2052 return error_mark_node
;
2054 else if (VOID_TYPE_P (type
) && std_alignof
)
2057 error_at (loc
, "invalid application of %qs to a void type",
2058 OVL_OP_INFO (false, op
)->name
);
2059 return error_mark_node
;
2062 bool dependent_p
= dependent_type_p (type
);
2064 complete_type (type
);
2066 /* VLA types will have a non-constant size. In the body of an
2067 uninstantiated template, we don't need to try to compute the
2068 value, because the sizeof expression is not an integral
2069 constant expression in that case. And, if we do try to
2070 compute the value, we'll likely end up with SAVE_EXPRs, which
2071 the template substitution machinery does not expect to see. */
2072 || (processing_template_decl
2073 && COMPLETE_TYPE_P (type
)
2074 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
))
2076 tree value
= build_min (op
, size_type_node
, type
);
2077 TREE_READONLY (value
) = 1;
2078 if (op
== ALIGNOF_EXPR
&& std_alignof
)
2079 ALIGNOF_EXPR_STD_P (value
) = true;
2080 SET_EXPR_LOCATION (value
, loc
);
2084 return c_sizeof_or_alignof_type (loc
, complete_type (type
),
2085 op
== SIZEOF_EXPR
, std_alignof
,
2089 /* Return the size of the type, without producing any warnings for
2090 types whose size cannot be taken. This routine should be used only
2091 in some other routine that has already produced a diagnostic about
2092 using the size of such a type. */
2094 cxx_sizeof_nowarn (tree type
)
2096 if (TREE_CODE (type
) == FUNCTION_TYPE
2097 || VOID_TYPE_P (type
)
2098 || TREE_CODE (type
) == ERROR_MARK
)
2099 return size_one_node
;
2100 else if (!COMPLETE_TYPE_P (type
))
2101 return size_zero_node
;
2103 return cxx_sizeof_or_alignof_type (input_location
, type
,
2104 SIZEOF_EXPR
, false, false);
2107 /* Process a sizeof expression where the operand is an expression. */
2110 cxx_sizeof_expr (location_t loc
, tree e
, tsubst_flags_t complain
)
2112 if (e
== error_mark_node
)
2113 return error_mark_node
;
2115 if (instantiation_dependent_uneval_expression_p (e
))
2117 e
= build_min (SIZEOF_EXPR
, size_type_node
, e
);
2118 TREE_SIDE_EFFECTS (e
) = 0;
2119 TREE_READONLY (e
) = 1;
2120 SET_EXPR_LOCATION (e
, loc
);
2125 location_t e_loc
= cp_expr_loc_or_loc (e
, loc
);
2126 STRIP_ANY_LOCATION_WRAPPER (e
);
2128 /* To get the size of a static data member declared as an array of
2129 unknown bound, we need to instantiate it. */
2131 && VAR_HAD_UNKNOWN_BOUND (e
)
2132 && DECL_TEMPLATE_INSTANTIATION (e
))
2133 instantiate_decl (e
, /*defer_ok*/true, /*expl_inst_mem*/false);
2135 if (TREE_CODE (e
) == PARM_DECL
2136 && DECL_ARRAY_PARAMETER_P (e
)
2137 && (complain
& tf_warning
))
2139 auto_diagnostic_group d
;
2140 if (warning_at (e_loc
, OPT_Wsizeof_array_argument
,
2141 "%<sizeof%> on array function parameter %qE "
2142 "will return size of %qT", e
, TREE_TYPE (e
)))
2143 inform (DECL_SOURCE_LOCATION (e
), "declared here");
2146 e
= mark_type_use (e
);
2150 if (complain
& tf_error
)
2152 "invalid application of %<sizeof%> to a bit-field");
2154 return error_mark_node
;
2157 else if (is_overloaded_fn (e
))
2159 if (complain
& tf_error
)
2160 permerror (e_loc
, "ISO C++ forbids applying %<sizeof%> to "
2161 "an expression of function type");
2163 return error_mark_node
;
2166 else if (type_unknown_p (e
))
2168 if (complain
& tf_error
)
2169 cxx_incomplete_type_error (e_loc
, e
, TREE_TYPE (e
));
2171 return error_mark_node
;
2177 return cxx_sizeof_or_alignof_type (loc
, e
, SIZEOF_EXPR
, false,
2178 complain
& tf_error
);
2181 /* Implement the __alignof keyword: Return the minimum required
2182 alignment of E, measured in bytes. For VAR_DECL's and
2183 FIELD_DECL's return DECL_ALIGN (which can be set from an
2184 "aligned" __attribute__ specification). STD_ALIGNOF acts
2185 like in cxx_sizeof_or_alignof_type. */
2188 cxx_alignof_expr (location_t loc
, tree e
, bool std_alignof
,
2189 tsubst_flags_t complain
)
2193 if (e
== error_mark_node
)
2194 return error_mark_node
;
2196 if (processing_template_decl
)
2198 e
= build_min (ALIGNOF_EXPR
, size_type_node
, e
);
2199 TREE_SIDE_EFFECTS (e
) = 0;
2200 TREE_READONLY (e
) = 1;
2201 SET_EXPR_LOCATION (e
, loc
);
2202 ALIGNOF_EXPR_STD_P (e
) = std_alignof
;
2207 location_t e_loc
= cp_expr_loc_or_loc (e
, loc
);
2208 STRIP_ANY_LOCATION_WRAPPER (e
);
2210 e
= mark_type_use (e
);
2212 if (!verify_type_context (loc
, TCTX_ALIGNOF
, TREE_TYPE (e
),
2213 !(complain
& tf_error
)))
2215 if (!(complain
& tf_error
))
2216 return error_mark_node
;
2220 t
= size_int (DECL_ALIGN_UNIT (e
));
2221 else if (bitfield_p (e
))
2223 if (complain
& tf_error
)
2225 "invalid application of %<__alignof%> to a bit-field");
2227 return error_mark_node
;
2230 else if (TREE_CODE (e
) == COMPONENT_REF
2231 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
)
2232 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e
, 1)));
2233 else if (is_overloaded_fn (e
))
2235 if (complain
& tf_error
)
2236 permerror (e_loc
, "ISO C++ forbids applying %<__alignof%> to "
2237 "an expression of function type");
2239 return error_mark_node
;
2240 if (TREE_CODE (e
) == FUNCTION_DECL
)
2241 t
= size_int (DECL_ALIGN_UNIT (e
));
2245 else if (type_unknown_p (e
))
2247 if (complain
& tf_error
)
2248 cxx_incomplete_type_error (e_loc
, e
, TREE_TYPE (e
));
2250 return error_mark_node
;
2254 return cxx_sizeof_or_alignof_type (loc
, TREE_TYPE (e
),
2255 ALIGNOF_EXPR
, std_alignof
,
2256 complain
& tf_error
);
2258 return fold_convert_loc (loc
, size_type_node
, t
);
2261 /* Process a sizeof or alignof expression E with code OP where the operand
2262 is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2265 cxx_sizeof_or_alignof_expr (location_t loc
, tree e
, enum tree_code op
,
2266 bool std_alignof
, bool complain
)
2268 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
2269 if (op
== SIZEOF_EXPR
)
2270 return cxx_sizeof_expr (loc
, e
, complain
? tf_warning_or_error
: tf_none
);
2272 return cxx_alignof_expr (loc
, e
, std_alignof
,
2273 complain
? tf_warning_or_error
: tf_none
);
2276 /* Build a representation of an expression 'alignas(E).' Return the
2277 folded integer value of E if it is an integral constant expression
2278 that resolves to a valid alignment. If E depends on a template
2279 parameter, return a syntactic representation tree of kind
2280 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2281 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2284 cxx_alignas_expr (tree e
)
2286 if (e
== NULL_TREE
|| e
== error_mark_node
2287 || (!TYPE_P (e
) && !require_potential_rvalue_constant_expression (e
)))
2293 When the alignment-specifier is of the form
2294 alignas(type-id), it shall have the same effect as
2295 alignas(alignof(type-id)). */
2297 return cxx_sizeof_or_alignof_type (input_location
,
2299 /*std_alignof=*/true,
2302 /* If we reach this point, it means the alignas expression if of
2303 the form "alignas(assignment-expression)", so we should follow
2304 what is stated by [dcl.align]/2. */
2306 if (value_dependent_expression_p (e
))
2307 /* Leave value-dependent expression alone for now. */
2310 e
= instantiate_non_dependent_expr (e
);
2311 e
= mark_rvalue_use (e
);
2313 /* [dcl.align]/2 says:
2315 the assignment-expression shall be an integral constant
2318 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e
)))
2320 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e
));
2321 return error_mark_node
;
2324 return cxx_constant_value (e
);
2328 /* EXPR is being used in a context that is not a function call.
2333 The expression can be used only as the left-hand operand of a
2334 member function call.
2336 [expr.mptr.operator]
2338 If the result of .* or ->* is a function, then that result can be
2339 used only as the operand for the function call operator ().
2341 by issuing an error message if appropriate. Returns true iff EXPR
2342 violates these rules. */
2345 invalid_nonstatic_memfn_p (location_t loc
, tree expr
, tsubst_flags_t complain
)
2347 if (expr
== NULL_TREE
)
2349 /* Don't enforce this in MS mode. */
2350 if (flag_ms_extensions
)
2352 if (is_overloaded_fn (expr
) && !really_overloaded_fn (expr
))
2353 expr
= get_first_fn (expr
);
2354 if (TREE_TYPE (expr
)
2355 && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr
))
2357 if (complain
& tf_error
)
2361 error_at (loc
, "invalid use of non-static member function %qD",
2363 inform (DECL_SOURCE_LOCATION (expr
), "declared here");
2366 error_at (loc
, "invalid use of non-static member function of "
2367 "type %qT", TREE_TYPE (expr
));
2374 /* If EXP is a reference to a bit-field, and the type of EXP does not
2375 match the declared type of the bit-field, return the declared type
2376 of the bit-field. Otherwise, return NULL_TREE. */
2379 is_bitfield_expr_with_lowered_type (const_tree exp
)
2381 switch (TREE_CODE (exp
))
2384 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1)
2385 ? TREE_OPERAND (exp
, 1)
2386 : TREE_OPERAND (exp
, 0)))
2388 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 2));
2391 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1));
2395 case UNARY_PLUS_EXPR
:
2396 case PREDECREMENT_EXPR
:
2397 case PREINCREMENT_EXPR
:
2398 case POSTDECREMENT_EXPR
:
2399 case POSTINCREMENT_EXPR
:
2401 case NON_LVALUE_EXPR
:
2403 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
2409 field
= TREE_OPERAND (exp
, 1);
2410 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_BIT_FIELD_TYPE (field
))
2412 if (same_type_ignoring_top_level_qualifiers_p
2413 (TREE_TYPE (exp
), DECL_BIT_FIELD_TYPE (field
)))
2415 return DECL_BIT_FIELD_TYPE (field
);
2419 if (DECL_HAS_VALUE_EXPR_P (exp
))
2420 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2421 (CONST_CAST_TREE (exp
)));
2424 case VIEW_CONVERT_EXPR
:
2425 if (location_wrapper_p (exp
))
2426 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
2435 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2436 bitfield with a lowered type, the type of EXP is returned, rather
2440 unlowered_expr_type (const_tree exp
)
2443 tree etype
= TREE_TYPE (exp
);
2445 type
= is_bitfield_expr_with_lowered_type (exp
);
2447 type
= cp_build_qualified_type (type
, cp_type_quals (etype
));
2454 /* Perform the conversions in [expr] that apply when an lvalue appears
2455 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2456 function-to-pointer conversions. In addition, bitfield references are
2457 converted to their declared types. Note that this function does not perform
2458 the lvalue-to-rvalue conversion for class types. If you need that conversion
2459 for class types, then you probably need to use force_rvalue.
2461 Although the returned value is being used as an rvalue, this
2462 function does not wrap the returned expression in a
2463 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2464 that the return value is no longer an lvalue. */
2467 decay_conversion (tree exp
,
2468 tsubst_flags_t complain
,
2469 bool reject_builtin
/* = true */)
2472 enum tree_code code
;
2473 location_t loc
= cp_expr_loc_or_input_loc (exp
);
2475 type
= TREE_TYPE (exp
);
2476 if (type
== error_mark_node
)
2477 return error_mark_node
;
2479 exp
= resolve_nondeduced_context_or_error (exp
, complain
);
2481 code
= TREE_CODE (type
);
2483 if (error_operand_p (exp
))
2484 return error_mark_node
;
2486 if (NULLPTR_TYPE_P (type
) && !TREE_SIDE_EFFECTS (exp
))
2488 mark_rvalue_use (exp
, loc
, reject_builtin
);
2489 return nullptr_node
;
2492 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2493 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2494 if (code
== VOID_TYPE
)
2496 if (complain
& tf_error
)
2497 error_at (loc
, "void value not ignored as it ought to be");
2498 return error_mark_node
;
2500 if (invalid_nonstatic_memfn_p (loc
, exp
, complain
))
2501 return error_mark_node
;
2502 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
2504 exp
= mark_lvalue_use (exp
);
2505 if (reject_builtin
&& reject_gcc_builtin (exp
, loc
))
2506 return error_mark_node
;
2507 return cp_build_addr_expr (exp
, complain
);
2509 if (code
== ARRAY_TYPE
)
2514 exp
= mark_lvalue_use (exp
);
2516 if (INDIRECT_REF_P (exp
))
2517 return build_nop (build_pointer_type (TREE_TYPE (type
)),
2518 TREE_OPERAND (exp
, 0));
2520 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
2522 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1), complain
);
2523 if (op1
== error_mark_node
)
2524 return error_mark_node
;
2525 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
2526 TREE_OPERAND (exp
, 0), op1
);
2529 if (!obvalue_p (exp
)
2530 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
2532 if (complain
& tf_error
)
2533 error_at (loc
, "invalid use of non-lvalue array");
2534 return error_mark_node
;
2537 /* Don't let an array compound literal decay to a pointer. It can
2538 still be used to initialize an array or bind to a reference. */
2539 if (TREE_CODE (exp
) == TARGET_EXPR
)
2541 if (complain
& tf_error
)
2542 error_at (loc
, "taking address of temporary array");
2543 return error_mark_node
;
2546 ptrtype
= build_pointer_type (TREE_TYPE (type
));
2550 if (!cxx_mark_addressable (exp
))
2551 return error_mark_node
;
2552 adr
= build_nop (ptrtype
, build_address (exp
));
2555 /* This way is better for a COMPONENT_REF since it can
2556 simplify the offset for a component. */
2557 adr
= cp_build_addr_expr (exp
, complain
);
2558 return cp_convert (ptrtype
, adr
, complain
);
2561 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2562 exp
= mark_rvalue_use (exp
, loc
, reject_builtin
);
2564 /* If a bitfield is used in a context where integral promotion
2565 applies, then the caller is expected to have used
2566 default_conversion. That function promotes bitfields correctly
2567 before calling this function. At this point, if we have a
2568 bitfield referenced, we may assume that is not subject to
2569 promotion, and that, therefore, the type of the resulting rvalue
2570 is the declared type of the bitfield. */
2571 exp
= convert_bitfield_to_declared_type (exp
);
2573 /* We do not call rvalue() here because we do not want to wrap EXP
2574 in a NON_LVALUE_EXPR. */
2578 Non-class rvalues always have cv-unqualified types. */
2579 type
= TREE_TYPE (exp
);
2580 if (!CLASS_TYPE_P (type
) && cv_qualified_p (type
))
2581 exp
= build_nop (cv_unqualified (type
), exp
);
2583 if (!complete_type_or_maybe_complain (type
, exp
, complain
))
2584 return error_mark_node
;
2589 /* Perform preparatory conversions, as part of the "usual arithmetic
2590 conversions". In particular, as per [expr]:
2592 Whenever an lvalue expression appears as an operand of an
2593 operator that expects the rvalue for that operand, the
2594 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2595 standard conversions are applied to convert the expression to an
2598 In addition, we perform integral promotions here, as those are
2599 applied to both operands to a binary operator before determining
2600 what additional conversions should apply. */
2603 cp_default_conversion (tree exp
, tsubst_flags_t complain
)
2605 /* Check for target-specific promotions. */
2606 tree promoted_type
= targetm
.promoted_type (TREE_TYPE (exp
));
2608 exp
= cp_convert (promoted_type
, exp
, complain
);
2609 /* Perform the integral promotions first so that bitfield
2610 expressions (which may promote to "int", even if the bitfield is
2611 declared "unsigned") are promoted correctly. */
2612 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp
)))
2613 exp
= cp_perform_integral_promotions (exp
, complain
);
2614 /* Perform the other conversions. */
2615 exp
= decay_conversion (exp
, complain
);
2623 default_conversion (tree exp
)
2625 return cp_default_conversion (exp
, tf_warning_or_error
);
2628 /* EXPR is an expression with an integral or enumeration type.
2629 Perform the integral promotions in [conv.prom], and return the
2633 cp_perform_integral_promotions (tree expr
, tsubst_flags_t complain
)
2638 expr
= mark_rvalue_use (expr
);
2639 if (error_operand_p (expr
))
2640 return error_mark_node
;
2642 type
= TREE_TYPE (expr
);
2646 A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2647 of type int if int can represent all the values of the bit-field;
2648 otherwise, it can be converted to unsigned int if unsigned int can
2649 represent all the values of the bit-field. If the bit-field is larger yet,
2650 no integral promotion applies to it. If the bit-field has an enumerated
2651 type, it is treated as any other value of that type for promotion
2653 tree bitfield_type
= is_bitfield_expr_with_lowered_type (expr
);
2655 && (TREE_CODE (bitfield_type
) == ENUMERAL_TYPE
2656 || TYPE_PRECISION (type
) > TYPE_PRECISION (integer_type_node
)))
2657 type
= bitfield_type
;
2659 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
2660 /* Scoped enums don't promote. */
2661 if (SCOPED_ENUM_P (type
))
2663 promoted_type
= type_promotes_to (type
);
2664 if (type
!= promoted_type
)
2665 expr
= cp_convert (promoted_type
, expr
, complain
);
2666 else if (bitfield_type
&& bitfield_type
!= type
)
2667 /* Prevent decay_conversion from converting to bitfield_type. */
2668 expr
= build_nop (type
, expr
);
2675 perform_integral_promotions (tree expr
)
2677 return cp_perform_integral_promotions (expr
, tf_warning_or_error
);
2680 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2681 decay_conversion to one. */
2684 string_conv_p (const_tree totype
, const_tree exp
, int warn
)
2688 if (!TYPE_PTR_P (totype
))
2691 t
= TREE_TYPE (totype
);
2692 if (!same_type_p (t
, char_type_node
)
2693 && !same_type_p (t
, char8_type_node
)
2694 && !same_type_p (t
, char16_type_node
)
2695 && !same_type_p (t
, char32_type_node
)
2696 && !same_type_p (t
, wchar_type_node
))
2699 location_t loc
= EXPR_LOC_OR_LOC (exp
, input_location
);
2701 STRIP_ANY_LOCATION_WRAPPER (exp
);
2703 if (TREE_CODE (exp
) == STRING_CST
)
2705 /* Make sure that we don't try to convert between char and wide chars. */
2706 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
2711 /* Is this a string constant which has decayed to 'const char *'? */
2712 t
= build_pointer_type (cp_build_qualified_type (t
, TYPE_QUAL_CONST
));
2713 if (!same_type_p (TREE_TYPE (exp
), t
))
2716 if (TREE_CODE (exp
) != ADDR_EXPR
2717 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
2722 if (cxx_dialect
>= cxx11
)
2723 pedwarn (loc
, OPT_Wwrite_strings
,
2724 "ISO C++ forbids converting a string constant to %qT",
2727 warning_at (loc
, OPT_Wwrite_strings
,
2728 "deprecated conversion from string constant to %qT",
2735 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2736 can, for example, use as an lvalue. This code used to be in
2737 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2738 expressions, where we're dealing with aggregates. But now it's again only
2739 called from unary_complex_lvalue. The case (in particular) that led to
2740 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2744 rationalize_conditional_expr (enum tree_code code
, tree t
,
2745 tsubst_flags_t complain
)
2747 location_t loc
= cp_expr_loc_or_input_loc (t
);
2749 /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that
2750 the first operand is always the one to be used if both operands
2751 are equal, so we know what conditional expression this used to be. */
2752 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
2754 tree op0
= TREE_OPERAND (t
, 0);
2755 tree op1
= TREE_OPERAND (t
, 1);
2757 /* The following code is incorrect if either operand side-effects. */
2758 gcc_assert (!TREE_SIDE_EFFECTS (op0
)
2759 && !TREE_SIDE_EFFECTS (op1
));
2761 build_conditional_expr (loc
,
2762 build_x_binary_op (loc
,
2763 (TREE_CODE (t
) == MIN_EXPR
2764 ? LE_EXPR
: GE_EXPR
),
2765 op0
, TREE_CODE (op0
),
2766 op1
, TREE_CODE (op1
),
2770 cp_build_unary_op (code
, op0
, false, complain
),
2771 cp_build_unary_op (code
, op1
, false, complain
),
2775 tree op1
= TREE_OPERAND (t
, 1);
2776 if (TREE_CODE (op1
) != THROW_EXPR
)
2777 op1
= cp_build_unary_op (code
, op1
, false, complain
);
2778 tree op2
= TREE_OPERAND (t
, 2);
2779 if (TREE_CODE (op2
) != THROW_EXPR
)
2780 op2
= cp_build_unary_op (code
, op2
, false, complain
);
2783 build_conditional_expr (loc
, TREE_OPERAND (t
, 0), op1
, op2
, complain
);
2786 /* Given the TYPE of an anonymous union field inside T, return the
2787 FIELD_DECL for the field. If not found return NULL_TREE. Because
2788 anonymous unions can nest, we must also search all anonymous unions
2789 that are directly reachable. */
2792 lookup_anon_field (tree
, tree type
)
2796 type
= TYPE_MAIN_VARIANT (type
);
2797 field
= ANON_AGGR_TYPE_FIELD (type
);
2802 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2803 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2804 non-NULL, it indicates the path to the base used to name MEMBER.
2805 If PRESERVE_REFERENCE is true, the expression returned will have
2806 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2807 returned will have the type referred to by the reference.
2809 This function does not perform access control; that is either done
2810 earlier by the parser when the name of MEMBER is resolved to MEMBER
2811 itself, or later when overload resolution selects one of the
2812 functions indicated by MEMBER. */
2815 build_class_member_access_expr (cp_expr object
, tree member
,
2816 tree access_path
, bool preserve_reference
,
2817 tsubst_flags_t complain
)
2821 tree result
= NULL_TREE
;
2822 tree using_decl
= NULL_TREE
;
2824 if (error_operand_p (object
) || error_operand_p (member
))
2825 return error_mark_node
;
2827 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2831 The type of the first expression shall be "class object" (of a
2833 object_type
= TREE_TYPE (object
);
2834 if (!currently_open_class (object_type
)
2835 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2836 return error_mark_node
;
2837 if (!CLASS_TYPE_P (object_type
))
2839 if (complain
& tf_error
)
2841 if (INDIRECT_TYPE_P (object_type
)
2842 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2843 error ("request for member %qD in %qE, which is of pointer "
2844 "type %qT (maybe you meant to use %<->%> ?)",
2845 member
, object
.get_value (), object_type
);
2847 error ("request for member %qD in %qE, which is of non-class "
2848 "type %qT", member
, object
.get_value (), object_type
);
2850 return error_mark_node
;
2853 /* The standard does not seem to actually say that MEMBER must be a
2854 member of OBJECT_TYPE. However, that is clearly what is
2856 if (DECL_P (member
))
2858 member_scope
= DECL_CLASS_CONTEXT (member
);
2859 if (!mark_used (member
, complain
) && !(complain
& tf_error
))
2860 return error_mark_node
;
2862 if (TREE_UNAVAILABLE (member
))
2863 error_unavailable_use (member
, NULL_TREE
);
2864 else if (TREE_DEPRECATED (member
))
2865 warn_deprecated_use (member
, NULL_TREE
);
2868 member_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (member
));
2869 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2870 presently be the anonymous union. Go outwards until we find a
2871 type related to OBJECT_TYPE. */
2872 while ((ANON_AGGR_TYPE_P (member_scope
) || UNSCOPED_ENUM_P (member_scope
))
2873 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
2875 member_scope
= TYPE_CONTEXT (member_scope
);
2876 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
2878 if (complain
& tf_error
)
2880 if (TREE_CODE (member
) == FIELD_DECL
)
2881 error ("invalid use of non-static data member %qE", member
);
2883 error ("%qD is not a member of %qT", member
, object_type
);
2885 return error_mark_node
;
2888 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2889 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2890 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2891 if (tree temp
= unary_complex_lvalue (ADDR_EXPR
, object
))
2893 temp
= cp_build_fold_indirect_ref (temp
);
2894 if (!lvalue_p (object
) && lvalue_p (temp
))
2895 /* Preserve rvalueness. */
2900 /* In [expr.ref], there is an explicit list of the valid choices for
2901 MEMBER. We check for each of those cases here. */
2904 /* A static data member. */
2906 mark_exp_read (object
);
2908 if (tree wrap
= maybe_get_tls_wrapper_call (result
))
2909 /* Replace an evaluated use of the thread_local variable with
2910 a call to its wrapper. */
2913 /* If OBJECT has side-effects, they are supposed to occur. */
2914 if (TREE_SIDE_EFFECTS (object
))
2915 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
2917 else if (TREE_CODE (member
) == FIELD_DECL
)
2919 /* A non-static data member. */
2924 if (INDIRECT_REF_P (object
))
2926 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object
, 0)));
2928 null_object_p
= false;
2930 /* Convert OBJECT to the type of MEMBER. */
2931 if (!same_type_p (TYPE_MAIN_VARIANT (object_type
),
2932 TYPE_MAIN_VARIANT (member_scope
)))
2937 /* We didn't complain above about a currently open class, but now we
2938 must: we don't know how to refer to a base member before layout is
2939 complete. But still don't complain in a template. */
2940 if (!cp_unevaluated_operand
2941 && !dependent_type_p (object_type
)
2942 && !complete_type_or_maybe_complain (object_type
, object
,
2944 return error_mark_node
;
2946 binfo
= lookup_base (access_path
? access_path
: object_type
,
2947 member_scope
, ba_unique
, &kind
, complain
);
2948 if (binfo
== error_mark_node
)
2949 return error_mark_node
;
2951 /* It is invalid to try to get to a virtual base of a
2952 NULL object. The most common cause is invalid use of
2954 if (null_object_p
&& kind
== bk_via_virtual
)
2956 if (complain
& tf_error
)
2958 error ("invalid access to non-static data member %qD in "
2959 "virtual base of NULL object", member
);
2961 return error_mark_node
;
2964 /* Convert to the base. */
2965 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
2966 /*nonnull=*/1, complain
);
2967 /* If we found the base successfully then we should be able
2968 to convert to it successfully. */
2969 gcc_assert (object
!= error_mark_node
);
2972 /* If MEMBER is from an anonymous aggregate, we have converted
2973 OBJECT so that it refers to the class containing the
2974 anonymous union. Generate a reference to the anonymous union
2975 itself, and recur to find MEMBER. */
2976 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
))
2977 /* When this code is called from build_field_call, the
2978 object already has the type of the anonymous union.
2979 That is because the COMPONENT_REF was already
2980 constructed, and was then disassembled before calling
2981 build_field_call. After the function-call code is
2982 cleaned up, this waste can be eliminated. */
2983 && (!same_type_ignoring_top_level_qualifiers_p
2984 (TREE_TYPE (object
), DECL_CONTEXT (member
))))
2986 tree anonymous_union
;
2988 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
2989 DECL_CONTEXT (member
));
2990 object
= build_class_member_access_expr (object
,
2992 /*access_path=*/NULL_TREE
,
2997 /* Compute the type of the field, as described in [expr.ref]. */
2998 type_quals
= TYPE_UNQUALIFIED
;
2999 member_type
= TREE_TYPE (member
);
3000 if (!TYPE_REF_P (member_type
))
3002 type_quals
= (cp_type_quals (member_type
)
3003 | cp_type_quals (object_type
));
3005 /* A field is const (volatile) if the enclosing object, or the
3006 field itself, is const (volatile). But, a mutable field is
3007 not const, even within a const object. */
3008 if (DECL_MUTABLE_P (member
))
3009 type_quals
&= ~TYPE_QUAL_CONST
;
3010 member_type
= cp_build_qualified_type (member_type
, type_quals
);
3013 result
= build3_loc (input_location
, COMPONENT_REF
, member_type
,
3014 object
, member
, NULL_TREE
);
3016 /* Mark the expression const or volatile, as appropriate. Even
3017 though we've dealt with the type above, we still have to mark the
3018 expression itself. */
3019 if (type_quals
& TYPE_QUAL_CONST
)
3020 TREE_READONLY (result
) = 1;
3021 if (type_quals
& TYPE_QUAL_VOLATILE
)
3022 TREE_THIS_VOLATILE (result
) = 1;
3024 else if (BASELINK_P (member
))
3026 /* The member is a (possibly overloaded) member function. */
3030 /* If the MEMBER is exactly one static member function, then we
3031 know the type of the expression. Otherwise, we must wait
3032 until overload resolution has been performed. */
3033 functions
= BASELINK_FUNCTIONS (member
);
3034 if (TREE_CODE (functions
) == FUNCTION_DECL
3035 && DECL_STATIC_FUNCTION_P (functions
))
3036 type
= TREE_TYPE (functions
);
3038 type
= unknown_type_node
;
3039 /* Note that we do not convert OBJECT to the BASELINK_BINFO
3040 base. That will happen when the function is called. */
3041 result
= build3_loc (input_location
, COMPONENT_REF
, type
, object
, member
,
3044 else if (TREE_CODE (member
) == CONST_DECL
)
3046 /* The member is an enumerator. */
3048 /* If OBJECT has side-effects, they are supposed to occur. */
3049 if (TREE_SIDE_EFFECTS (object
))
3050 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
3053 else if ((using_decl
= strip_using_decl (member
)) != member
)
3054 result
= build_class_member_access_expr (object
,
3056 access_path
, preserve_reference
,
3060 if (complain
& tf_error
)
3061 error ("invalid use of %qD", member
);
3062 return error_mark_node
;
3065 if (!preserve_reference
)
3068 If E2 is declared to have type "reference to T", then ... the
3069 type of E1.E2 is T. */
3070 result
= convert_from_reference (result
);
3075 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
3076 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
3079 lookup_destructor (tree object
, tree scope
, tree dtor_name
,
3080 tsubst_flags_t complain
)
3082 tree object_type
= TREE_TYPE (object
);
3083 tree dtor_type
= TREE_OPERAND (dtor_name
, 0);
3086 /* We've already complained about this destructor. */
3087 if (dtor_type
== error_mark_node
)
3088 return error_mark_node
;
3090 if (scope
&& !check_dtor_name (scope
, dtor_type
))
3092 if (complain
& tf_error
)
3093 error ("qualified type %qT does not match destructor name ~%qT",
3095 return error_mark_node
;
3097 if (is_auto (dtor_type
))
3098 dtor_type
= object_type
;
3099 else if (identifier_p (dtor_type
))
3101 /* In a template, names we can't find a match for are still accepted
3102 destructor names, and we check them here. */
3103 if (check_dtor_name (object_type
, dtor_type
))
3104 dtor_type
= object_type
;
3107 if (complain
& tf_error
)
3108 error ("object type %qT does not match destructor name ~%qT",
3109 object_type
, dtor_type
);
3110 return error_mark_node
;
3114 else if (!DERIVED_FROM_P (dtor_type
, TYPE_MAIN_VARIANT (object_type
)))
3116 if (complain
& tf_error
)
3117 error ("the type being destroyed is %qT, but the destructor "
3118 "refers to %qT", TYPE_MAIN_VARIANT (object_type
), dtor_type
);
3119 return error_mark_node
;
3121 expr
= lookup_member (dtor_type
, complete_dtor_identifier
,
3122 /*protect=*/1, /*want_type=*/false,
3123 tf_warning_or_error
);
3126 if (complain
& tf_error
)
3127 cxx_incomplete_type_error (dtor_name
, dtor_type
);
3128 return error_mark_node
;
3130 expr
= (adjust_result_of_qualified_name_lookup
3131 (expr
, dtor_type
, object_type
));
3132 if (scope
== NULL_TREE
)
3133 /* We need to call adjust_result_of_qualified_name_lookup in case the
3134 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
3135 that we still get virtual function binding. */
3136 BASELINK_QUALIFIED_P (expr
) = false;
3140 /* An expression of the form "A::template B" has been resolved to
3141 DECL. Issue a diagnostic if B is not a template or template
3145 check_template_keyword (tree decl
)
3147 /* The standard says:
3151 If a name prefixed by the keyword template is not a member
3152 template, the program is ill-formed.
3154 DR 228 removed the restriction that the template be a member
3157 DR 96, if accepted would add the further restriction that explicit
3158 template arguments must be provided if the template keyword is
3159 used, but, as of 2005-10-16, that DR is still in "drafting". If
3160 this DR is accepted, then the semantic checks here can be
3161 simplified, as the entity named must in fact be a template
3162 specialization, rather than, as at present, a set of overloaded
3163 functions containing at least one template function. */
3164 if (TREE_CODE (decl
) != TEMPLATE_DECL
3165 && TREE_CODE (decl
) != TEMPLATE_ID_EXPR
)
3169 if (DECL_USE_TEMPLATE (decl
)
3170 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
3173 permerror (input_location
, "%qD is not a template", decl
);
3175 else if (!is_overloaded_fn (decl
))
3176 permerror (input_location
, "%qD is not a template", decl
);
3181 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
3182 !found
&& iter
; ++iter
)
3185 if (TREE_CODE (fn
) == TEMPLATE_DECL
3186 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
3187 || (TREE_CODE (fn
) == FUNCTION_DECL
3188 && DECL_USE_TEMPLATE (fn
)
3189 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn
))))
3193 permerror (input_location
, "%qD is not a template", decl
);
3198 /* Record that an access failure occurred on BASETYPE_PATH attempting
3199 to access DECL, where DIAG_DECL should be used for diagnostics. */
3202 access_failure_info::record_access_failure (tree basetype_path
,
3203 tree decl
, tree diag_decl
)
3205 m_was_inaccessible
= true;
3206 m_basetype_path
= basetype_path
;
3208 m_diag_decl
= diag_decl
;
3211 /* If an access failure was recorded, then attempt to locate an
3212 accessor function for the pertinent field.
3213 Otherwise, return NULL_TREE. */
3216 access_failure_info::get_any_accessor (bool const_p
) const
3218 if (!was_inaccessible_p ())
3222 = locate_field_accessor (m_basetype_path
, m_diag_decl
, const_p
);
3226 /* The accessor must itself be accessible for it to be a reasonable
3228 if (!accessible_p (m_basetype_path
, accessor
, true))
3234 /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3235 replacing the primary location in RICHLOC with "accessor()". */
3238 access_failure_info::add_fixit_hint (rich_location
*richloc
,
3242 pp_string (&pp
, IDENTIFIER_POINTER (DECL_NAME (accessor_decl
)));
3243 pp_string (&pp
, "()");
3244 richloc
->add_fixit_replace (pp_formatted_text (&pp
));
3247 /* If an access failure was recorded, then attempt to locate an
3248 accessor function for the pertinent field, and if one is
3249 available, add a note and fix-it hint suggesting using it. */
3252 access_failure_info::maybe_suggest_accessor (bool const_p
) const
3254 tree accessor
= get_any_accessor (const_p
);
3255 if (accessor
== NULL_TREE
)
3257 rich_location
richloc (line_table
, input_location
);
3258 add_fixit_hint (&richloc
, accessor
);
3259 inform (&richloc
, "field %q#D can be accessed via %q#D",
3260 m_diag_decl
, accessor
);
3263 /* Subroutine of finish_class_member_access_expr.
3264 Issue an error about NAME not being a member of ACCESS_PATH (or
3265 OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3269 complain_about_unrecognized_member (tree access_path
, tree name
,
3272 /* Attempt to provide a hint about misspelled names. */
3273 tree guessed_id
= lookup_member_fuzzy (access_path
, name
,
3274 /*want_type=*/false);
3275 if (guessed_id
== NULL_TREE
)
3278 error ("%q#T has no member named %qE",
3279 TREE_CODE (access_path
) == TREE_BINFO
3280 ? TREE_TYPE (access_path
) : object_type
, name
);
3284 location_t bogus_component_loc
= input_location
;
3285 gcc_rich_location
rich_loc (bogus_component_loc
);
3287 /* Check that the guessed name is accessible along access_path. */
3288 access_failure_info afi
;
3289 lookup_member (access_path
, guessed_id
, /*protect=*/1,
3290 /*want_type=*/false, /*complain=*/false,
3292 if (afi
.was_inaccessible_p ())
3294 tree accessor
= afi
.get_any_accessor (TYPE_READONLY (object_type
));
3297 /* The guessed name isn't directly accessible, but can be accessed
3298 via an accessor member function. */
3299 afi
.add_fixit_hint (&rich_loc
, accessor
);
3300 error_at (&rich_loc
,
3301 "%q#T has no member named %qE;"
3302 " did you mean %q#D? (accessible via %q#D)",
3303 TREE_CODE (access_path
) == TREE_BINFO
3304 ? TREE_TYPE (access_path
) : object_type
,
3305 name
, afi
.get_diag_decl (), accessor
);
3309 /* The guessed name isn't directly accessible, and no accessor
3310 member function could be found. */
3311 error_at (&rich_loc
,
3312 "%q#T has no member named %qE;"
3313 " did you mean %q#D? (not accessible from this context)",
3314 TREE_CODE (access_path
) == TREE_BINFO
3315 ? TREE_TYPE (access_path
) : object_type
,
3316 name
, afi
.get_diag_decl ());
3317 complain_about_access (afi
.get_decl (), afi
.get_diag_decl (),
3318 afi
.get_diag_decl (), false, ak_none
);
3323 /* The guessed name is directly accessible; suggest it. */
3324 rich_loc
.add_fixit_misspelled_id (bogus_component_loc
,
3326 error_at (&rich_loc
,
3327 "%q#T has no member named %qE;"
3328 " did you mean %qE?",
3329 TREE_CODE (access_path
) == TREE_BINFO
3330 ? TREE_TYPE (access_path
) : object_type
,
3335 /* This function is called by the parser to process a class member
3336 access expression of the form OBJECT.NAME. NAME is a node used by
3337 the parser to represent a name; it is not yet a DECL. It may,
3338 however, be a BASELINK where the BASELINK_FUNCTIONS is a
3339 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3340 there is no reason to do the lookup twice, so the parser keeps the
3341 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3342 be a template via the use of the "A::template B" syntax. */
3345 finish_class_member_access_expr (cp_expr object
, tree name
, bool template_p
,
3346 tsubst_flags_t complain
)
3351 tree access_path
= NULL_TREE
;
3352 tree orig_object
= object
;
3353 tree orig_name
= name
;
3355 if (object
== error_mark_node
|| name
== error_mark_node
)
3356 return error_mark_node
;
3358 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3359 if (!objc_is_public (object
, name
))
3360 return error_mark_node
;
3362 object_type
= TREE_TYPE (object
);
3364 if (processing_template_decl
)
3366 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3367 type_dependent_object_expression_p (object
)
3368 /* If NAME is "f<args>", where either 'f' or 'args' is
3369 dependent, then the expression is dependent. */
3370 || (TREE_CODE (name
) == TEMPLATE_ID_EXPR
3371 && dependent_template_id_p (TREE_OPERAND (name
, 0),
3372 TREE_OPERAND (name
, 1)))
3373 /* If NAME is "T::X" where "T" is dependent, then the
3374 expression is dependent. */
3375 || (TREE_CODE (name
) == SCOPE_REF
3376 && TYPE_P (TREE_OPERAND (name
, 0))
3377 && dependent_scope_p (TREE_OPERAND (name
, 0)))
3378 /* If NAME is operator T where "T" is dependent, we can't
3379 lookup until we instantiate the T. */
3380 || (TREE_CODE (name
) == IDENTIFIER_NODE
3381 && IDENTIFIER_CONV_OP_P (name
)
3382 && dependent_type_p (TREE_TYPE (name
))))
3385 return build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
,
3386 orig_object
, orig_name
, NULL_TREE
);
3388 object
= build_non_dependent_expr (object
);
3390 else if (c_dialect_objc ()
3391 && identifier_p (name
)
3392 && (expr
= objc_maybe_build_component_ref (object
, name
)))
3397 The type of the first expression shall be "class object" (of a
3399 if (!currently_open_class (object_type
)
3400 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
3401 return error_mark_node
;
3402 if (!CLASS_TYPE_P (object_type
))
3404 if (complain
& tf_error
)
3406 if (INDIRECT_TYPE_P (object_type
)
3407 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
3408 error ("request for member %qD in %qE, which is of pointer "
3409 "type %qT (maybe you meant to use %<->%> ?)",
3410 name
, object
.get_value (), object_type
);
3412 error ("request for member %qD in %qE, which is of non-class "
3413 "type %qT", name
, object
.get_value (), object_type
);
3415 return error_mark_node
;
3418 if (BASELINK_P (name
))
3419 /* A member function that has already been looked up. */
3423 bool is_template_id
= false;
3424 tree template_args
= NULL_TREE
;
3425 tree scope
= NULL_TREE
;
3427 access_path
= object_type
;
3429 if (TREE_CODE (name
) == SCOPE_REF
)
3431 /* A qualified name. The qualifying class or namespace `S'
3432 has already been looked up; it is either a TYPE or a
3434 scope
= TREE_OPERAND (name
, 0);
3435 name
= TREE_OPERAND (name
, 1);
3437 /* If SCOPE is a namespace, then the qualified name does not
3438 name a member of OBJECT_TYPE. */
3439 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
3441 if (complain
& tf_error
)
3442 error ("%<%D::%D%> is not a member of %qT",
3443 scope
, name
, object_type
);
3444 return error_mark_node
;
3448 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
3450 is_template_id
= true;
3451 template_args
= TREE_OPERAND (name
, 1);
3452 name
= TREE_OPERAND (name
, 0);
3454 if (!identifier_p (name
))
3455 name
= OVL_NAME (name
);
3460 if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
3462 gcc_assert (!is_template_id
);
3463 /* Looking up a member enumerator (c++/56793). */
3464 if (!TYPE_CLASS_SCOPE_P (scope
)
3465 || !DERIVED_FROM_P (TYPE_CONTEXT (scope
), object_type
))
3467 if (complain
& tf_error
)
3468 error ("%<%D::%D%> is not a member of %qT",
3469 scope
, name
, object_type
);
3470 return error_mark_node
;
3472 tree val
= lookup_enumerator (scope
, name
);
3475 if (complain
& tf_error
)
3476 error ("%qD is not a member of %qD",
3478 return error_mark_node
;
3481 if (TREE_SIDE_EFFECTS (object
))
3482 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
), object
, val
);
3486 gcc_assert (CLASS_TYPE_P (scope
));
3487 gcc_assert (identifier_p (name
) || TREE_CODE (name
) == BIT_NOT_EXPR
);
3489 if (constructor_name_p (name
, scope
))
3491 if (complain
& tf_error
)
3492 error ("cannot call constructor %<%T::%D%> directly",
3494 return error_mark_node
;
3497 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3498 access_path
= lookup_base (object_type
, scope
, ba_check
,
3500 if (access_path
== error_mark_node
)
3501 return error_mark_node
;
3504 if (any_dependent_bases_p (object_type
))
3506 if (complain
& tf_error
)
3507 error ("%qT is not a base of %qT", scope
, object_type
);
3508 return error_mark_node
;
3512 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
3514 if (dependent_type_p (object_type
))
3515 /* The destructor isn't declared yet. */
3517 member
= lookup_destructor (object
, scope
, name
, complain
);
3521 /* Look up the member. */
3522 access_failure_info afi
;
3523 if (processing_template_decl
)
3524 /* Even though this class member access expression is at this
3525 point not dependent, the member itself may be dependent, and
3526 we must not potentially push a access check for a dependent
3527 member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3528 ahead of time here; we're going to redo this member lookup at
3529 instantiation time anyway. */
3530 push_deferring_access_checks (dk_no_check
);
3531 member
= lookup_member (access_path
, name
, /*protect=*/1,
3532 /*want_type=*/false, complain
,
3534 if (processing_template_decl
)
3535 pop_deferring_access_checks ();
3536 afi
.maybe_suggest_accessor (TYPE_READONLY (object_type
));
3537 if (member
== NULL_TREE
)
3539 if (dependent_type_p (object_type
))
3540 /* Try again at instantiation time. */
3542 if (complain
& tf_error
)
3543 complain_about_unrecognized_member (access_path
, name
,
3545 return error_mark_node
;
3547 if (member
== error_mark_node
)
3548 return error_mark_node
;
3550 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member
)))
3551 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3552 wrong, so don't use it. */
3554 if (TREE_CODE (member
) == USING_DECL
&& DECL_DEPENDENT_P (member
))
3560 tree templ
= member
;
3562 if (BASELINK_P (templ
))
3563 member
= lookup_template_function (templ
, template_args
);
3564 else if (variable_template_p (templ
))
3565 member
= (lookup_and_finish_template_variable
3566 (templ
, template_args
, complain
));
3569 if (complain
& tf_error
)
3570 error ("%qD is not a member template function", name
);
3571 return error_mark_node
;
3576 if (TREE_UNAVAILABLE (member
))
3577 error_unavailable_use (member
, NULL_TREE
);
3578 else if (TREE_DEPRECATED (member
))
3579 warn_deprecated_use (member
, NULL_TREE
);
3582 check_template_keyword (member
);
3584 expr
= build_class_member_access_expr (object
, member
, access_path
,
3585 /*preserve_reference=*/false,
3587 if (processing_template_decl
&& expr
!= error_mark_node
)
3589 if (BASELINK_P (member
))
3591 if (TREE_CODE (orig_name
) == SCOPE_REF
)
3592 BASELINK_QUALIFIED_P (member
) = 1;
3595 return build_min_non_dep (COMPONENT_REF
, expr
,
3596 orig_object
, orig_name
,
3603 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3607 build_simple_component_ref (tree object
, tree member
)
3609 tree type
= cp_build_qualified_type (TREE_TYPE (member
),
3610 cp_type_quals (TREE_TYPE (object
)));
3611 return build3_loc (input_location
,
3612 COMPONENT_REF
, type
,
3613 object
, member
, NULL_TREE
);
3616 /* Return an expression for the MEMBER_NAME field in the internal
3617 representation of PTRMEM, a pointer-to-member function. (Each
3618 pointer-to-member function type gets its own RECORD_TYPE so it is
3619 more convenient to access the fields by name than by FIELD_DECL.)
3620 This routine converts the NAME to a FIELD_DECL and then creates the
3621 node for the complete expression. */
3624 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
3629 if (TREE_CODE (ptrmem
) == CONSTRUCTOR
)
3631 for (auto &e
: CONSTRUCTOR_ELTS (ptrmem
))
3632 if (e
.index
&& DECL_P (e
.index
) && DECL_NAME (e
.index
) == member_name
)
3637 /* This code is a stripped down version of
3638 build_class_member_access_expr. It does not work to use that
3639 routine directly because it expects the object to be of class
3641 ptrmem_type
= TREE_TYPE (ptrmem
);
3642 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
));
3643 for (member
= TYPE_FIELDS (ptrmem_type
); member
;
3644 member
= DECL_CHAIN (member
))
3645 if (DECL_NAME (member
) == member_name
)
3647 return build_simple_component_ref (ptrmem
, member
);
3650 /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3651 and for any other relevant operator. */
3654 op_unqualified_lookup (tree_code code
, bool is_assign
)
3656 tree lookups
= NULL_TREE
;
3658 if (cxx_dialect
>= cxx20
&& !is_assign
)
3660 if (code
== NE_EXPR
)
3662 /* != can get rewritten in terms of ==. */
3663 tree fnname
= ovl_op_identifier (false, EQ_EXPR
);
3664 if (tree fns
= lookup_name (fnname
, LOOK_where::BLOCK_NAMESPACE
))
3665 lookups
= tree_cons (fnname
, fns
, lookups
);
3667 else if (code
== GT_EXPR
|| code
== LE_EXPR
3668 || code
== LT_EXPR
|| code
== GE_EXPR
)
3670 /* These can get rewritten in terms of <=>. */
3671 tree fnname
= ovl_op_identifier (false, SPACESHIP_EXPR
);
3672 if (tree fns
= lookup_name (fnname
, LOOK_where::BLOCK_NAMESPACE
))
3673 lookups
= tree_cons (fnname
, fns
, lookups
);
3677 tree fnname
= ovl_op_identifier (is_assign
, code
);
3678 if (tree fns
= lookup_name (fnname
, LOOK_where::BLOCK_NAMESPACE
))
3679 lookups
= tree_cons (fnname
, fns
, lookups
);
3684 return build_tree_list (NULL_TREE
, NULL_TREE
);
3687 /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3688 the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3689 name lookup for the given operator. */
3692 build_dependent_operator_type (tree lookups
, tree_code code
, bool is_assign
)
3695 /* We're partially instantiating a dependent operator expression, and
3696 LOOKUPS is the result of phase 1 name lookup that we performed
3697 earlier at template definition time, so just reuse the corresponding
3698 DEPENDENT_OPERATOR_TYPE. */
3699 return TREE_TYPE (lookups
);
3701 /* Otherwise we're processing a dependent operator expression at template
3702 definition time, so perform phase 1 name lookup now. */
3703 lookups
= op_unqualified_lookup (code
, is_assign
);
3705 tree type
= cxx_make_type (DEPENDENT_OPERATOR_TYPE
);
3706 DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type
) = lookups
;
3707 TREE_TYPE (lookups
) = type
;
3711 /* Given an expression PTR for a pointer, return an expression
3712 for the value pointed to.
3713 ERRORSTRING is the name of the operator to appear in error messages.
3715 This function may need to overload OPERATOR_FNNAME.
3716 Must also handle REFERENCE_TYPEs for C++. */
3719 build_x_indirect_ref (location_t loc
, tree expr
, ref_operator errorstring
,
3720 tree lookups
, tsubst_flags_t complain
)
3722 tree orig_expr
= expr
;
3724 tree overload
= NULL_TREE
;
3726 if (processing_template_decl
)
3728 /* Retain the type if we know the operand is a pointer. */
3729 if (TREE_TYPE (expr
) && INDIRECT_TYPE_P (TREE_TYPE (expr
)))
3731 if (expr
== current_class_ptr
3732 || (TREE_CODE (expr
) == NOP_EXPR
3733 && TREE_OPERAND (expr
, 0) == current_class_ptr
3734 && (same_type_ignoring_top_level_qualifiers_p
3735 (TREE_TYPE (expr
), TREE_TYPE (current_class_ptr
)))))
3736 return current_class_ref
;
3737 return build_min (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
3739 if (type_dependent_expression_p (expr
))
3741 expr
= build_min_nt_loc (loc
, INDIRECT_REF
, expr
);
3743 = build_dependent_operator_type (lookups
, INDIRECT_REF
, false);
3746 expr
= build_non_dependent_expr (expr
);
3749 rval
= build_new_op (loc
, INDIRECT_REF
, LOOKUP_NORMAL
, expr
,
3750 NULL_TREE
, NULL_TREE
, lookups
,
3751 &overload
, complain
);
3753 rval
= cp_build_indirect_ref (loc
, expr
, errorstring
, complain
);
3755 if (processing_template_decl
&& rval
!= error_mark_node
)
3757 if (overload
!= NULL_TREE
)
3758 return (build_min_non_dep_op_overload
3759 (INDIRECT_REF
, rval
, overload
, orig_expr
));
3761 return build_min_non_dep (INDIRECT_REF
, rval
, orig_expr
);
3767 /* Like c-family strict_aliasing_warning, but don't warn for dependent
3768 types or expressions. */
3771 cp_strict_aliasing_warning (location_t loc
, tree type
, tree expr
)
3773 if (processing_template_decl
)
3777 if (dependent_type_p (type
) || type_dependent_expression_p (e
))
3780 return strict_aliasing_warning (loc
, type
, expr
);
3783 /* The implementation of the above, and of indirection implied by other
3784 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3787 cp_build_indirect_ref_1 (location_t loc
, tree ptr
, ref_operator errorstring
,
3788 tsubst_flags_t complain
, bool do_fold
)
3792 /* RO_NULL should only be used with the folding entry points below, not
3793 cp_build_indirect_ref. */
3794 gcc_checking_assert (errorstring
!= RO_NULL
|| do_fold
);
3796 if (ptr
== current_class_ptr
3797 || (TREE_CODE (ptr
) == NOP_EXPR
3798 && TREE_OPERAND (ptr
, 0) == current_class_ptr
3799 && (same_type_ignoring_top_level_qualifiers_p
3800 (TREE_TYPE (ptr
), TREE_TYPE (current_class_ptr
)))))
3801 return current_class_ref
;
3803 pointer
= (TYPE_REF_P (TREE_TYPE (ptr
))
3804 ? ptr
: decay_conversion (ptr
, complain
));
3805 if (pointer
== error_mark_node
)
3806 return error_mark_node
;
3808 type
= TREE_TYPE (pointer
);
3810 if (INDIRECT_TYPE_P (type
))
3814 If the type of the expression is "pointer to T," the type
3815 of the result is "T." */
3816 tree t
= TREE_TYPE (type
);
3818 if ((CONVERT_EXPR_P (ptr
)
3819 || TREE_CODE (ptr
) == VIEW_CONVERT_EXPR
)
3820 && (!CLASS_TYPE_P (t
) || !CLASSTYPE_EMPTY_P (t
)))
3822 /* If a warning is issued, mark it to avoid duplicates from
3823 the backend. This only needs to be done at
3824 warn_strict_aliasing > 2. */
3825 if (warn_strict_aliasing
> 2
3826 && cp_strict_aliasing_warning (EXPR_LOCATION (ptr
),
3827 type
, TREE_OPERAND (ptr
, 0)))
3828 suppress_warning (ptr
, OPT_Wstrict_aliasing
);
3831 if (VOID_TYPE_P (t
))
3833 /* A pointer to incomplete type (other than cv void) can be
3834 dereferenced [expr.unary.op]/1 */
3835 if (complain
& tf_error
)
3836 error_at (loc
, "%qT is not a pointer-to-object type", type
);
3837 return error_mark_node
;
3839 else if (do_fold
&& TREE_CODE (pointer
) == ADDR_EXPR
3840 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
3841 /* The POINTER was something like `&x'. We simplify `*&x' to
3843 return TREE_OPERAND (pointer
, 0);
3846 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
3848 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3849 so that we get the proper error message if the result is used
3850 to assign to. Also, &* is supposed to be a no-op. */
3851 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
3852 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
3853 TREE_SIDE_EFFECTS (ref
)
3854 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
));
3858 else if (!(complain
& tf_error
))
3859 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3861 /* `pointer' won't be an error_mark_node if we were given a
3862 pointer to member, so it's cool to check for this here. */
3863 else if (TYPE_PTRMEM_P (type
))
3864 switch (errorstring
)
3866 case RO_ARRAY_INDEXING
:
3868 "invalid use of array indexing on pointer to member");
3871 error_at (loc
, "invalid use of unary %<*%> on pointer to member");
3873 case RO_IMPLICIT_CONVERSION
:
3874 error_at (loc
, "invalid use of implicit conversion on pointer "
3878 error_at (loc
, "left hand operand of %<->*%> must be a pointer to "
3879 "class, but is a pointer to member of type %qT", type
);
3884 else if (pointer
!= error_mark_node
)
3885 invalid_indirection_error (loc
, type
, errorstring
);
3887 return error_mark_node
;
3890 /* Entry point used by c-common, which expects folding. */
3893 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errorstring
)
3895 return cp_build_indirect_ref_1 (loc
, ptr
, errorstring
,
3896 tf_warning_or_error
, true);
3899 /* Entry point used by internal indirection needs that don't correspond to any
3900 syntactic construct. */
3903 cp_build_fold_indirect_ref (tree pointer
)
3905 return cp_build_indirect_ref_1 (input_location
, pointer
, RO_NULL
,
3906 tf_warning_or_error
, true);
3909 /* Entry point used by indirection needs that correspond to some syntactic
3913 cp_build_indirect_ref (location_t loc
, tree ptr
, ref_operator errorstring
,
3914 tsubst_flags_t complain
)
3916 return cp_build_indirect_ref_1 (loc
, ptr
, errorstring
, complain
, false);
3919 /* This handles expressions of the form "a[i]", which denotes
3922 This is logically equivalent in C to *(a+i), but we may do it differently.
3923 If A is a variable or a member, we generate a primitive ARRAY_REF.
3924 This avoids forcing the array out of registers, and can work on
3925 arrays that are not lvalues (for example, members of structures returned
3928 If INDEX is of some user-defined type, it must be converted to
3929 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3930 will inherit the type of the array, which will be some pointer type.
3932 LOC is the location to use in building the array reference. */
3935 cp_build_array_ref (location_t loc
, tree array
, tree idx
,
3936 tsubst_flags_t complain
)
3942 if (complain
& tf_error
)
3943 error_at (loc
, "subscript missing in array reference");
3944 return error_mark_node
;
3947 if (TREE_TYPE (array
) == error_mark_node
3948 || TREE_TYPE (idx
) == error_mark_node
)
3949 return error_mark_node
;
3951 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3953 switch (TREE_CODE (array
))
3957 tree value
= cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3959 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3960 TREE_OPERAND (array
, 0), value
);
3961 SET_EXPR_LOCATION (ret
, loc
);
3966 ret
= build_conditional_expr
3967 (loc
, TREE_OPERAND (array
, 0),
3968 cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3970 cp_build_array_ref (loc
, TREE_OPERAND (array
, 2), idx
,
3973 protected_set_expr_location (ret
, loc
);
3980 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, idx
);
3982 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
3986 warn_array_subscript_with_type_char (loc
, idx
);
3988 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx
)))
3990 if (complain
& tf_error
)
3991 error_at (loc
, "array subscript is not an integer");
3992 return error_mark_node
;
3995 /* Apply integral promotions *after* noticing character types.
3996 (It is unclear why we do these promotions -- the standard
3997 does not say that we should. In fact, the natural thing would
3998 seem to be to convert IDX to ptrdiff_t; we're performing
3999 pointer arithmetic.) */
4000 idx
= cp_perform_integral_promotions (idx
, complain
);
4002 idx
= maybe_fold_non_dependent_expr (idx
, complain
);
4004 /* An array that is indexed by a non-constant
4005 cannot be stored in a register; we must be able to do
4006 address arithmetic on its address.
4007 Likewise an array of elements of variable size. */
4008 if (TREE_CODE (idx
) != INTEGER_CST
4009 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
4010 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
4013 if (!cxx_mark_addressable (array
, true))
4014 return error_mark_node
;
4017 /* An array that is indexed by a constant value which is not within
4018 the array bounds cannot be stored in a register either; because we
4019 would get a crash in store_bit_field/extract_bit_field when trying
4020 to access a non-existent part of the register. */
4021 if (TREE_CODE (idx
) == INTEGER_CST
4022 && TYPE_DOMAIN (TREE_TYPE (array
))
4023 && ! int_fits_type_p (idx
, TYPE_DOMAIN (TREE_TYPE (array
))))
4025 if (!cxx_mark_addressable (array
))
4026 return error_mark_node
;
4029 /* Note in C++ it is valid to subscript a `register' array, since
4030 it is valid to take the address of something with that
4031 storage specification. */
4035 while (TREE_CODE (foo
) == COMPONENT_REF
)
4036 foo
= TREE_OPERAND (foo
, 0);
4037 if (VAR_P (foo
) && DECL_REGISTER (foo
)
4038 && (complain
& tf_warning
))
4039 warning_at (loc
, OPT_Wextra
,
4040 "subscripting array declared %<register%>");
4043 type
= TREE_TYPE (TREE_TYPE (array
));
4044 rval
= build4 (ARRAY_REF
, type
, array
, idx
, NULL_TREE
, NULL_TREE
);
4045 /* Array ref is const/volatile if the array elements are
4046 or if the array is.. */
4047 TREE_READONLY (rval
)
4048 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
4049 TREE_SIDE_EFFECTS (rval
)
4050 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
4051 TREE_THIS_VOLATILE (rval
)
4052 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
4053 ret
= require_complete_type (rval
, complain
);
4054 protected_set_expr_location (ret
, loc
);
4056 ret
= non_lvalue_loc (loc
, ret
);
4061 tree ar
= cp_default_conversion (array
, complain
);
4062 tree ind
= cp_default_conversion (idx
, complain
);
4063 tree first
= NULL_TREE
;
4065 if (flag_strong_eval_order
== 2 && TREE_SIDE_EFFECTS (ind
))
4066 ar
= first
= save_expr (ar
);
4068 /* Put the integer in IND to simplify error checking. */
4069 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
4070 std::swap (ar
, ind
);
4072 if (ar
== error_mark_node
|| ind
== error_mark_node
)
4073 return error_mark_node
;
4075 if (!TYPE_PTR_P (TREE_TYPE (ar
)))
4077 if (complain
& tf_error
)
4078 error_at (loc
, "subscripted value is neither array nor pointer");
4079 return error_mark_node
;
4081 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
4083 if (complain
& tf_error
)
4084 error_at (loc
, "array subscript is not an integer");
4085 return error_mark_node
;
4088 warn_array_subscript_with_type_char (loc
, idx
);
4090 ret
= cp_build_binary_op (input_location
, PLUS_EXPR
, ar
, ind
, complain
);
4092 ret
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (ret
), first
, ret
);
4093 ret
= cp_build_indirect_ref (loc
, ret
, RO_ARRAY_INDEXING
, complain
);
4094 protected_set_expr_location (ret
, loc
);
4096 ret
= non_lvalue_loc (loc
, ret
);
4101 /* Entry point for Obj-C++. */
4104 build_array_ref (location_t loc
, tree array
, tree idx
)
4106 return cp_build_array_ref (loc
, array
, idx
, tf_warning_or_error
);
4109 /* Resolve a pointer to member function. INSTANCE is the object
4110 instance to use, if the member points to a virtual member.
4112 This used to avoid checking for virtual functions if basetype
4113 has no virtual functions, according to an earlier ANSI draft.
4114 With the final ISO C++ rules, such an optimization is
4115 incorrect: A pointer to a derived member can be static_cast
4116 to pointer-to-base-member, as long as the dynamic object
4117 later has the right member. So now we only do this optimization
4118 when we know the dynamic type of the object. */
4121 get_member_function_from_ptrfunc (tree
*instance_ptrptr
, tree function
,
4122 tsubst_flags_t complain
)
4124 if (TREE_CODE (function
) == OFFSET_REF
)
4125 function
= TREE_OPERAND (function
, 1);
4127 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
4129 tree idx
, delta
, e1
, e2
, e3
, vtbl
;
4131 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
4132 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
4134 tree instance_ptr
= *instance_ptrptr
;
4135 tree instance_save_expr
= 0;
4136 if (instance_ptr
== error_mark_node
)
4138 if (TREE_CODE (function
) == PTRMEM_CST
)
4140 /* Extracting the function address from a pmf is only
4141 allowed with -Wno-pmf-conversions. It only works for
4143 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
), complain
);
4144 e1
= convert (fntype
, e1
);
4149 if (complain
& tf_error
)
4150 error ("object missing in use of %qE", function
);
4151 return error_mark_node
;
4155 /* True if we know that the dynamic type of the object doesn't have
4156 virtual functions, so we can assume the PFN field is a pointer. */
4157 nonvirtual
= (COMPLETE_TYPE_P (basetype
)
4158 && !TYPE_POLYMORPHIC_P (basetype
)
4159 && resolves_to_fixed_type_p (instance_ptr
, 0));
4161 /* If we don't really have an object (i.e. in an ill-formed
4162 conversion from PMF to pointer), we can't resolve virtual
4163 functions anyway. */
4164 if (!nonvirtual
&& is_dummy_object (instance_ptr
))
4167 if (TREE_SIDE_EFFECTS (instance_ptr
))
4168 instance_ptr
= instance_save_expr
= save_expr (instance_ptr
);
4170 if (TREE_SIDE_EFFECTS (function
))
4171 function
= save_expr (function
);
4173 /* Start by extracting all the information from the PMF itself. */
4174 e3
= pfn_from_ptrmemfunc (function
);
4175 delta
= delta_from_ptrmemfunc (function
);
4176 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
4177 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
4179 int flag_sanitize_save
;
4180 case ptrmemfunc_vbit_in_pfn
:
4181 e1
= cp_build_binary_op (input_location
,
4182 BIT_AND_EXPR
, idx
, integer_one_node
,
4184 idx
= cp_build_binary_op (input_location
,
4185 MINUS_EXPR
, idx
, integer_one_node
,
4187 if (idx
== error_mark_node
)
4188 return error_mark_node
;
4191 case ptrmemfunc_vbit_in_delta
:
4192 e1
= cp_build_binary_op (input_location
,
4193 BIT_AND_EXPR
, delta
, integer_one_node
,
4195 /* Don't instrument the RSHIFT_EXPR we're about to create because
4196 we're going to use DELTA number of times, and that wouldn't play
4197 well with SAVE_EXPRs therein. */
4198 flag_sanitize_save
= flag_sanitize
;
4200 delta
= cp_build_binary_op (input_location
,
4201 RSHIFT_EXPR
, delta
, integer_one_node
,
4203 flag_sanitize
= flag_sanitize_save
;
4204 if (delta
== error_mark_node
)
4205 return error_mark_node
;
4212 if (e1
== error_mark_node
)
4213 return error_mark_node
;
4215 /* Convert down to the right base before using the instance. A
4216 special case is that in a pointer to member of class C, C may
4217 be incomplete. In that case, the function will of course be
4218 a member of C, and no conversion is required. In fact,
4219 lookup_base will fail in that case, because incomplete
4220 classes do not have BINFOs. */
4221 if (!same_type_ignoring_top_level_qualifiers_p
4222 (basetype
, TREE_TYPE (TREE_TYPE (instance_ptr
))))
4224 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
4225 basetype
, ba_check
, NULL
, complain
);
4226 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
,
4228 if (instance_ptr
== error_mark_node
)
4229 return error_mark_node
;
4231 /* ...and then the delta in the PMF. */
4232 instance_ptr
= fold_build_pointer_plus (instance_ptr
, delta
);
4234 /* Hand back the adjusted 'this' argument to our caller. */
4235 *instance_ptrptr
= instance_ptr
;
4238 /* Now just return the pointer. */
4241 /* Next extract the vtable pointer from the object. */
4242 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
4244 vtbl
= cp_build_fold_indirect_ref (vtbl
);
4245 if (vtbl
== error_mark_node
)
4246 return error_mark_node
;
4248 /* Finally, extract the function pointer from the vtable. */
4249 e2
= fold_build_pointer_plus_loc (input_location
, vtbl
, idx
);
4250 e2
= cp_build_fold_indirect_ref (e2
);
4251 if (e2
== error_mark_node
)
4252 return error_mark_node
;
4253 TREE_CONSTANT (e2
) = 1;
4255 /* When using function descriptors, the address of the
4256 vtable entry is treated as a function pointer. */
4257 if (TARGET_VTABLE_USES_DESCRIPTORS
)
4258 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
4259 cp_build_addr_expr (e2
, complain
));
4261 e2
= fold_convert (TREE_TYPE (e3
), e2
);
4262 e1
= build_conditional_expr (input_location
, e1
, e2
, e3
, complain
);
4263 if (e1
== error_mark_node
)
4264 return error_mark_node
;
4266 /* Make sure this doesn't get evaluated first inside one of the
4267 branches of the COND_EXPR. */
4268 if (instance_save_expr
)
4269 e1
= build2 (COMPOUND_EXPR
, TREE_TYPE (e1
),
4270 instance_save_expr
, e1
);
4277 /* Used by the C-common bits. */
4279 build_function_call (location_t
/*loc*/,
4280 tree function
, tree params
)
4282 return cp_build_function_call (function
, params
, tf_warning_or_error
);
4285 /* Used by the C-common bits. */
4287 build_function_call_vec (location_t
/*loc*/, vec
<location_t
> /*arg_loc*/,
4288 tree function
, vec
<tree
, va_gc
> *params
,
4289 vec
<tree
, va_gc
> * /*origtypes*/, tree orig_function
)
4291 vec
<tree
, va_gc
> *orig_params
= params
;
4292 tree ret
= cp_build_function_call_vec (function
, ¶ms
,
4293 tf_warning_or_error
, orig_function
);
4295 /* cp_build_function_call_vec can reallocate PARAMS by adding
4296 default arguments. That should never happen here. Verify
4298 gcc_assert (params
== orig_params
);
4303 /* Build a function call using a tree list of arguments. */
4306 cp_build_function_call (tree function
, tree params
, tsubst_flags_t complain
)
4311 for (; params
!= NULL_TREE
; params
= TREE_CHAIN (params
))
4312 vec_safe_push (vec
, TREE_VALUE (params
));
4313 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
4317 /* Build a function call using varargs. */
4320 cp_build_function_call_nary (tree function
, tsubst_flags_t complain
, ...)
4326 va_start (args
, complain
);
4327 for (t
= va_arg (args
, tree
); t
!= NULL_TREE
; t
= va_arg (args
, tree
))
4328 vec_safe_push (vec
, t
);
4330 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
4334 /* Build a function call using a vector of arguments.
4335 If FUNCTION is the result of resolving an overloaded target built-in,
4336 ORIG_FNDECL is the original function decl, otherwise it is null.
4337 PARAMS may be NULL if there are no parameters. This changes the
4338 contents of PARAMS. */
4341 cp_build_function_call_vec (tree function
, vec
<tree
, va_gc
> **params
,
4342 tsubst_flags_t complain
, tree orig_fndecl
)
4344 tree fntype
, fndecl
;
4346 tree original
= function
;
4350 vec
<tree
, va_gc
> *allocated
= NULL
;
4353 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4354 expressions, like those used for ObjC messenger dispatches. */
4355 if (params
!= NULL
&& !vec_safe_is_empty (*params
))
4356 function
= objc_rewrite_function_call (function
, (**params
)[0]);
4358 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4359 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4360 if (TREE_CODE (function
) == NOP_EXPR
4361 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
4362 function
= TREE_OPERAND (function
, 0);
4364 if (TREE_CODE (function
) == FUNCTION_DECL
)
4366 if (!mark_used (function
, complain
))
4367 return error_mark_node
;
4370 /* Convert anything with function type to a pointer-to-function. */
4371 if (DECL_MAIN_P (function
))
4373 if (complain
& tf_error
)
4374 pedwarn (input_location
, OPT_Wpedantic
,
4375 "ISO C++ forbids calling %<::main%> from within program");
4377 return error_mark_node
;
4379 function
= build_addr_func (function
, complain
);
4385 function
= build_addr_func (function
, complain
);
4388 if (function
== error_mark_node
)
4389 return error_mark_node
;
4391 fntype
= TREE_TYPE (function
);
4393 if (TYPE_PTRMEMFUNC_P (fntype
))
4395 if (complain
& tf_error
)
4396 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4397 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4398 original
, original
);
4399 return error_mark_node
;
4402 is_method
= (TYPE_PTR_P (fntype
)
4403 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
4405 if (!(TYPE_PTRFN_P (fntype
)
4407 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
4409 if (complain
& tf_error
)
4411 if (!flag_diagnostics_show_caret
)
4412 error_at (input_location
,
4413 "%qE cannot be used as a function", original
);
4414 else if (DECL_P (original
))
4415 error_at (input_location
,
4416 "%qD cannot be used as a function", original
);
4418 error_at (input_location
,
4419 "expression cannot be used as a function");
4422 return error_mark_node
;
4425 /* fntype now gets the type of function pointed to. */
4426 fntype
= TREE_TYPE (fntype
);
4427 parm_types
= TYPE_ARG_TYPES (fntype
);
4431 allocated
= make_tree_vector ();
4432 params
= &allocated
;
4435 nargs
= convert_arguments (parm_types
, params
, fndecl
, LOOKUP_NORMAL
,
4438 return error_mark_node
;
4440 argarray
= (*params
)->address ();
4442 /* Check for errors in format strings and inappropriately
4444 bool warned_p
= check_function_arguments (input_location
, fndecl
, fntype
,
4445 nargs
, argarray
, NULL
);
4447 ret
= build_cxx_call (function
, nargs
, argarray
, complain
, orig_fndecl
);
4451 tree c
= extract_call_expr (ret
);
4452 if (TREE_CODE (c
) == CALL_EXPR
)
4453 suppress_warning (c
, OPT_Wnonnull
);
4456 if (allocated
!= NULL
)
4457 release_tree_vector (allocated
);
4462 /* Subroutine of convert_arguments.
4463 Print an error message about a wrong number of arguments. */
4466 error_args_num (location_t loc
, tree fndecl
, bool too_many_p
)
4470 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
4472 if (DECL_NAME (fndecl
) == NULL_TREE
4473 || (DECL_NAME (fndecl
)
4474 == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl
)))))
4477 ? G_("too many arguments to constructor %q#D")
4478 : G_("too few arguments to constructor %q#D"),
4483 ? G_("too many arguments to member function %q#D")
4484 : G_("too few arguments to member function %q#D"),
4490 ? G_("too many arguments to function %q#D")
4491 : G_("too few arguments to function %q#D"),
4493 if (!DECL_IS_UNDECLARED_BUILTIN (fndecl
))
4494 inform (DECL_SOURCE_LOCATION (fndecl
), "declared here");
4498 if (c_dialect_objc () && objc_message_selector ())
4501 ? G_("too many arguments to method %q#D")
4502 : G_("too few arguments to method %q#D"),
4503 objc_message_selector ());
4505 error_at (loc
, too_many_p
? G_("too many arguments to function")
4506 : G_("too few arguments to function"));
4510 /* Convert the actual parameter expressions in the list VALUES to the
4511 types in the list TYPELIST. The converted expressions are stored
4512 back in the VALUES vector.
4513 If parmdecls is exhausted, or when an element has NULL as its type,
4514 perform the default conversions.
4516 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4518 This is also where warnings about wrong number of args are generated.
4520 Returns the actual number of arguments processed (which might be less
4521 than the length of the vector), or -1 on error.
4523 In C++, unspecified trailing parameters can be filled in with their
4524 default arguments, if such were specified. Do so here. */
4527 convert_arguments (tree typelist
, vec
<tree
, va_gc
> **values
, tree fndecl
,
4528 int flags
, tsubst_flags_t complain
)
4533 /* Argument passing is always copy-initialization. */
4534 flags
|= LOOKUP_ONLYCONVERTING
;
4536 for (i
= 0, typetail
= typelist
;
4537 i
< vec_safe_length (*values
);
4540 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
4541 tree val
= (**values
)[i
];
4543 if (val
== error_mark_node
|| type
== error_mark_node
)
4546 if (type
== void_type_node
)
4548 if (complain
& tf_error
)
4550 error_args_num (input_location
, fndecl
, /*too_many_p=*/true);
4557 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4558 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4559 if (TREE_CODE (val
) == NOP_EXPR
4560 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
4561 && (type
== 0 || !TYPE_REF_P (type
)))
4562 val
= TREE_OPERAND (val
, 0);
4564 if (type
== 0 || !TYPE_REF_P (type
))
4566 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
4567 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val
)))
4568 val
= decay_conversion (val
, complain
);
4571 if (val
== error_mark_node
)
4576 /* Formal parm type is specified by a function prototype. */
4579 if (!COMPLETE_TYPE_P (complete_type (type
)))
4581 if (complain
& tf_error
)
4583 location_t loc
= EXPR_LOC_OR_LOC (val
, input_location
);
4586 auto_diagnostic_group d
;
4588 "parameter %P of %qD has incomplete type %qT",
4590 inform (get_fndecl_argument_location (fndecl
, i
),
4594 error_at (loc
, "parameter %P has incomplete type %qT", i
,
4597 parmval
= error_mark_node
;
4601 parmval
= convert_for_initialization
4602 (NULL_TREE
, type
, val
, flags
,
4603 ICR_ARGPASS
, fndecl
, i
, complain
);
4604 parmval
= convert_for_arg_passing (type
, parmval
, complain
);
4607 if (parmval
== error_mark_node
)
4610 (**values
)[i
] = parmval
;
4614 int magic
= fndecl
? magic_varargs_p (fndecl
) : 0;
4617 /* Don't truncate excess precision to the semantic type. */
4618 if (magic
== 1 && TREE_CODE (val
) == EXCESS_PRECISION_EXPR
)
4619 val
= TREE_OPERAND (val
, 0);
4620 /* Don't do ellipsis conversion for __built_in_constant_p
4621 as this will result in spurious errors for non-trivial
4623 val
= require_complete_type (val
, complain
);
4626 val
= convert_arg_to_ellipsis (val
, complain
);
4628 (**values
)[i
] = val
;
4632 typetail
= TREE_CHAIN (typetail
);
4635 if (typetail
!= 0 && typetail
!= void_list_node
)
4637 /* See if there are default arguments that can be used. Because
4638 we hold default arguments in the FUNCTION_TYPE (which is so
4639 wrong), we can see default parameters here from deduced
4640 contexts (and via typeof) for indirect function calls.
4641 Fortunately we know whether we have a function decl to
4642 provide default arguments in a language conformant
4644 if (fndecl
&& TREE_PURPOSE (typetail
)
4645 && TREE_CODE (TREE_PURPOSE (typetail
)) != DEFERRED_PARSE
)
4647 for (; typetail
!= void_list_node
; ++i
)
4649 /* After DR777, with explicit template args we can end up with a
4650 default argument followed by no default argument. */
4651 if (!TREE_PURPOSE (typetail
))
4654 = convert_default_arg (TREE_VALUE (typetail
),
4655 TREE_PURPOSE (typetail
),
4656 fndecl
, i
, complain
);
4658 if (parmval
== error_mark_node
)
4661 vec_safe_push (*values
, parmval
);
4662 typetail
= TREE_CHAIN (typetail
);
4663 /* ends with `...'. */
4664 if (typetail
== NULL_TREE
)
4669 if (typetail
&& typetail
!= void_list_node
)
4671 if (complain
& tf_error
)
4672 error_args_num (input_location
, fndecl
, /*too_many_p=*/false);
4680 /* Build a binary-operation expression, after performing default
4681 conversions on the operands. CODE is the kind of expression to
4682 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4683 are the tree codes which correspond to ARG1 and ARG2 when issuing
4684 warnings about possibly misplaced parentheses. They may differ
4685 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4686 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4687 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4688 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4689 ARG2_CODE as ERROR_MARK. */
4692 build_x_binary_op (const op_location_t
&loc
, enum tree_code code
, tree arg1
,
4693 enum tree_code arg1_code
, tree arg2
,
4694 enum tree_code arg2_code
, tree lookups
,
4695 tree
*overload_p
, tsubst_flags_t complain
)
4700 tree overload
= NULL_TREE
;
4705 if (processing_template_decl
)
4707 if (type_dependent_expression_p (arg1
)
4708 || type_dependent_expression_p (arg2
))
4710 expr
= build_min_nt_loc (loc
, code
, arg1
, arg2
);
4712 = build_dependent_operator_type (lookups
, code
, false);
4715 arg1
= build_non_dependent_expr (arg1
);
4716 arg2
= build_non_dependent_expr (arg2
);
4719 if (code
== DOTSTAR_EXPR
)
4720 expr
= build_m_component_ref (arg1
, arg2
, complain
);
4722 expr
= build_new_op (loc
, code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
,
4723 lookups
, &overload
, complain
);
4725 if (overload_p
!= NULL
)
4726 *overload_p
= overload
;
4728 /* Check for cases such as x+y<<z which users are likely to
4729 misinterpret. But don't warn about obj << x + y, since that is a
4730 common idiom for I/O. */
4731 if (warn_parentheses
4732 && (complain
& tf_warning
)
4733 && !processing_template_decl
4734 && !error_operand_p (arg1
)
4735 && !error_operand_p (arg2
)
4736 && (code
!= LSHIFT_EXPR
4737 || !CLASS_TYPE_P (TREE_TYPE (arg1
))))
4738 warn_about_parentheses (loc
, code
, arg1_code
, orig_arg1
,
4739 arg2_code
, orig_arg2
);
4741 if (processing_template_decl
&& expr
!= error_mark_node
)
4743 if (overload
!= NULL_TREE
)
4744 return (build_min_non_dep_op_overload
4745 (code
, expr
, overload
, orig_arg1
, orig_arg2
));
4747 return build_min_non_dep (code
, expr
, orig_arg1
, orig_arg2
);
4753 /* Build and return an ARRAY_REF expression. */
4756 build_x_array_ref (location_t loc
, tree arg1
, tree arg2
,
4757 tsubst_flags_t complain
)
4759 tree orig_arg1
= arg1
;
4760 tree orig_arg2
= arg2
;
4762 tree overload
= NULL_TREE
;
4764 if (processing_template_decl
)
4766 if (type_dependent_expression_p (arg1
)
4767 || type_dependent_expression_p (arg2
))
4768 return build_min_nt_loc (loc
, ARRAY_REF
, arg1
, arg2
,
4769 NULL_TREE
, NULL_TREE
);
4770 arg1
= build_non_dependent_expr (arg1
);
4771 arg2
= build_non_dependent_expr (arg2
);
4774 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, arg1
, arg2
,
4775 NULL_TREE
, NULL_TREE
, &overload
, complain
);
4777 if (processing_template_decl
&& expr
!= error_mark_node
)
4779 if (overload
!= NULL_TREE
)
4780 return (build_min_non_dep_op_overload
4781 (ARRAY_REF
, expr
, overload
, orig_arg1
, orig_arg2
));
4783 return build_min_non_dep (ARRAY_REF
, expr
, orig_arg1
, orig_arg2
,
4784 NULL_TREE
, NULL_TREE
);
4789 /* Return whether OP is an expression of enum type cast to integer
4790 type. In C++ even unsigned enum types are cast to signed integer
4791 types. We do not want to issue warnings about comparisons between
4792 signed and unsigned types when one of the types is an enum type.
4793 Those warnings are always false positives in practice. */
4796 enum_cast_to_int (tree op
)
4798 if (CONVERT_EXPR_P (op
)
4799 && TREE_TYPE (op
) == integer_type_node
4800 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == ENUMERAL_TYPE
4801 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op
, 0))))
4804 /* The cast may have been pushed into a COND_EXPR. */
4805 if (TREE_CODE (op
) == COND_EXPR
)
4806 return (enum_cast_to_int (TREE_OPERAND (op
, 1))
4807 || enum_cast_to_int (TREE_OPERAND (op
, 2)));
4812 /* For the c-common bits. */
4814 build_binary_op (location_t location
, enum tree_code code
, tree op0
, tree op1
,
4817 return cp_build_binary_op (location
, code
, op0
, op1
, tf_warning_or_error
);
4820 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4821 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4824 build_vec_cmp (tree_code code
, tree type
,
4825 tree arg0
, tree arg1
)
4827 tree zero_vec
= build_zero_cst (type
);
4828 tree minus_one_vec
= build_minus_one_cst (type
);
4829 tree cmp_type
= truth_type_for (type
);
4830 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
4831 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
4834 /* Possibly warn about an address never being NULL. */
4837 warn_for_null_address (location_t location
, tree op
, tsubst_flags_t complain
)
4839 /* Prevent warnings issued for macro expansion. */
4841 || (complain
& tf_warning
) == 0
4842 || c_inhibit_evaluation_warnings
!= 0
4843 || from_macro_expansion_at (location
)
4844 || warning_suppressed_p (op
, OPT_Waddress
))
4847 if (TREE_CODE (op
) == NON_DEPENDENT_EXPR
)
4848 op
= TREE_OPERAND (op
, 0);
4850 tree cop
= fold_for_warn (op
);
4852 if (TREE_CODE (cop
) == NON_LVALUE_EXPR
)
4853 /* Unwrap the expression for C++ 98. */
4854 cop
= TREE_OPERAND (cop
, 0);
4856 if (TREE_CODE (cop
) == PTRMEM_CST
)
4858 /* The address of a nonstatic data member is never null. */
4859 warning_at (location
, OPT_Waddress
,
4860 "the address %qE will never be NULL",
4865 if (TREE_CODE (cop
) == NOP_EXPR
)
4867 /* Allow casts to intptr_t to suppress the warning. */
4868 tree type
= TREE_TYPE (cop
);
4869 if (TREE_CODE (type
) == INTEGER_TYPE
)
4875 bool warned
= false;
4876 if (TREE_CODE (cop
) == ADDR_EXPR
)
4878 cop
= TREE_OPERAND (cop
, 0);
4880 /* Set to true in the loop below if OP dereferences its operand.
4881 In such a case the ultimate target need not be a decl for
4882 the null [in]equality test to be necessarily constant. */
4885 /* Get the outermost array or object, or member. */
4886 while (handled_component_p (cop
))
4888 if (TREE_CODE (cop
) == COMPONENT_REF
)
4890 /* Get the member (its address is never null). */
4891 cop
= TREE_OPERAND (cop
, 1);
4895 /* Get the outer array/object to refer to in the warning. */
4896 cop
= TREE_OPERAND (cop
, 0);
4900 if ((!deref
&& !decl_with_nonnull_addr_p (cop
))
4901 || from_macro_expansion_at (location
)
4902 || warning_suppressed_p (cop
, OPT_Waddress
))
4905 warned
= warning_at (location
, OPT_Waddress
,
4906 "the address of %qD will never be NULL", cop
);
4909 else if (TREE_CODE (cop
) == POINTER_PLUS_EXPR
)
4911 /* Adding zero to the null pointer is well-defined in C++. When
4912 the offset is unknown (i.e., not a constant) warn anyway since
4913 it's less likely that the pointer operand is null than not. */
4914 tree off
= TREE_OPERAND (cop
, 1);
4915 if (!integer_zerop (off
)
4916 && !warning_suppressed_p (cop
, OPT_Waddress
))
4918 tree base
= TREE_OPERAND (cop
, 0);
4920 if (TYPE_REF_P (TREE_TYPE (base
)))
4921 warning_at (location
, OPT_Waddress
, "the compiler can assume that "
4922 "the address of %qE will never be NULL", base
);
4924 warning_at (location
, OPT_Waddress
, "comparing the result of "
4925 "pointer addition %qE and NULL", cop
);
4929 else if (CONVERT_EXPR_P (op
)
4930 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op
, 0))))
4934 if (TREE_CODE (op
) == COMPONENT_REF
)
4935 op
= TREE_OPERAND (op
, 1);
4938 warned
= warning_at (location
, OPT_Waddress
,
4939 "the compiler can assume that the address of "
4940 "%qD will never be NULL", op
);
4943 if (warned
&& DECL_P (op
))
4944 inform (DECL_SOURCE_LOCATION (op
), "%qD declared here", op
);
4947 /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
4948 the other operand is of a different enumeration type or a floating-point
4949 type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
4950 code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
4951 and LOC is the location for the whole binary expression.
4952 TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
4955 do_warn_enum_conversions (location_t loc
, enum tree_code code
, tree type0
,
4958 if (TREE_CODE (type0
) == ENUMERAL_TYPE
4959 && TREE_CODE (type1
) == ENUMERAL_TYPE
4960 && TYPE_MAIN_VARIANT (type0
) != TYPE_MAIN_VARIANT (type1
))
4962 /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
4963 Otherwise, warn if -Wenum-conversion is on. */
4965 if (warn_deprecated_enum_enum_conv
)
4966 opt
= OPT_Wdeprecated_enum_enum_conversion
;
4967 else if (warn_enum_conversion
)
4968 opt
= OPT_Wenum_conversion
;
4980 /* Comparisons are handled by -Wenum-compare. */
4982 case SPACESHIP_EXPR
:
4983 /* This is invalid, don't warn. */
4988 warning_at (loc
, opt
, "bitwise operation between different "
4989 "enumeration types %qT and %qT is deprecated",
4993 warning_at (loc
, opt
, "arithmetic between different enumeration "
4994 "types %qT and %qT is deprecated", type0
, type1
);
4998 else if ((TREE_CODE (type0
) == ENUMERAL_TYPE
4999 && TREE_CODE (type1
) == REAL_TYPE
)
5000 || (TREE_CODE (type0
) == REAL_TYPE
5001 && TREE_CODE (type1
) == ENUMERAL_TYPE
))
5003 const bool enum_first_p
= TREE_CODE (type0
) == ENUMERAL_TYPE
;
5004 /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
5005 Otherwise, warn if -Wenum-conversion is on. */
5007 if (warn_deprecated_enum_float_conv
)
5008 opt
= OPT_Wdeprecated_enum_float_conversion
;
5009 else if (warn_enum_conversion
)
5010 opt
= OPT_Wenum_conversion
;
5023 warning_at (loc
, opt
, "comparison of enumeration type %qT with "
5024 "floating-point type %qT is deprecated",
5027 warning_at (loc
, opt
, "comparison of floating-point type %qT "
5028 "with enumeration type %qT is deprecated",
5031 case SPACESHIP_EXPR
:
5032 /* This is invalid, don't warn. */
5036 warning_at (loc
, opt
, "arithmetic between enumeration type %qT "
5037 "and floating-point type %qT is deprecated",
5040 warning_at (loc
, opt
, "arithmetic between floating-point type %qT "
5041 "and enumeration type %qT is deprecated",
5048 /* Build a binary-operation expression without default conversions.
5049 CODE is the kind of expression to build.
5050 LOCATION is the location_t of the operator in the source code.
5051 This function differs from `build' in several ways:
5052 the data type of the result is computed and recorded in it,
5053 warnings are generated if arg data types are invalid,
5054 special handling for addition and subtraction of pointers is known,
5055 and some optimization is done (operations on narrow ints
5056 are done in the narrower type when that gives the same result).
5057 Constant folding is also done before the result is returned.
5059 Note that the operands will never have enumeral types
5060 because either they have just had the default conversions performed
5061 or they have both just been converted to some other type in which
5062 the arithmetic is to be done.
5064 C++: must do special pointer arithmetic when implementing
5065 multiple inheritance, and deal with pointer to member functions. */
5068 cp_build_binary_op (const op_location_t
&location
,
5069 enum tree_code code
, tree orig_op0
, tree orig_op1
,
5070 tsubst_flags_t complain
)
5073 enum tree_code code0
, code1
;
5074 tree type0
, type1
, orig_type0
, orig_type1
;
5075 const char *invalid_op_diag
;
5077 /* Expression code to give to the expression when it is built.
5078 Normally this is CODE, which is what the caller asked for,
5079 but in some special cases we change it. */
5080 enum tree_code resultcode
= code
;
5082 /* Data type in which the computation is to be performed.
5083 In the simplest cases this is the common type of the arguments. */
5084 tree result_type
= NULL_TREE
;
5086 /* When the computation is in excess precision, the type of the
5087 final EXCESS_PRECISION_EXPR. */
5088 tree semantic_result_type
= NULL
;
5090 /* Nonzero means operands have already been type-converted
5091 in whatever way is necessary.
5092 Zero means they need to be converted to RESULT_TYPE. */
5095 /* Nonzero means create the expression with this type, rather than
5097 tree build_type
= 0;
5099 /* Nonzero means after finally constructing the expression
5100 convert it to this type. */
5101 tree final_type
= 0;
5105 /* Nonzero if this is an operation like MIN or MAX which can
5106 safely be computed in short if both args are promoted shorts.
5107 Also implies COMMON.
5108 -1 indicates a bitwise operation; this makes a difference
5109 in the exact conditions for when it is safe to do the operation
5110 in a narrower mode. */
5113 /* Nonzero if this is a comparison operation;
5114 if both args are promoted shorts, compare the original shorts.
5115 Also implies COMMON. */
5116 int short_compare
= 0;
5118 /* Nonzero if this is a right-shift operation, which can be computed on the
5119 original short and then promoted if the operand is a promoted short. */
5120 int short_shift
= 0;
5122 /* Nonzero means set RESULT_TYPE to the common type of the args. */
5125 /* True if both operands have arithmetic type. */
5126 bool arithmetic_types_p
;
5128 /* Remember whether we're doing / or %. */
5129 bool doing_div_or_mod
= false;
5131 /* Remember whether we're doing << or >>. */
5132 bool doing_shift
= false;
5134 /* Tree holding instrumentation expression. */
5135 tree instrument_expr
= NULL_TREE
;
5137 /* True means this is an arithmetic operation that may need excess
5139 bool may_need_excess_precision
;
5141 /* Apply default conversions. */
5142 op0
= resolve_nondeduced_context (orig_op0
, complain
);
5143 op1
= resolve_nondeduced_context (orig_op1
, complain
);
5145 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
5146 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
5147 || code
== TRUTH_XOR_EXPR
)
5149 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
5150 op0
= decay_conversion (op0
, complain
);
5151 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
5152 op1
= decay_conversion (op1
, complain
);
5156 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
5157 op0
= cp_default_conversion (op0
, complain
);
5158 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
5159 op1
= cp_default_conversion (op1
, complain
);
5162 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
5163 STRIP_TYPE_NOPS (op0
);
5164 STRIP_TYPE_NOPS (op1
);
5166 /* DTRT if one side is an overloaded function, but complain about it. */
5167 if (type_unknown_p (op0
))
5169 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
5170 if (t
!= error_mark_node
)
5172 if (complain
& tf_error
)
5173 permerror (location
,
5174 "assuming cast to type %qT from overloaded function",
5179 if (type_unknown_p (op1
))
5181 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
5182 if (t
!= error_mark_node
)
5184 if (complain
& tf_error
)
5185 permerror (location
,
5186 "assuming cast to type %qT from overloaded function",
5192 orig_type0
= type0
= TREE_TYPE (op0
);
5193 orig_type1
= type1
= TREE_TYPE (op1
);
5194 tree non_ep_op0
= op0
;
5195 tree non_ep_op1
= op1
;
5197 /* The expression codes of the data types of the arguments tell us
5198 whether the arguments are integers, floating, pointers, etc. */
5199 code0
= TREE_CODE (type0
);
5200 code1
= TREE_CODE (type1
);
5202 /* If an error was already reported for one of the arguments,
5203 avoid reporting another error. */
5204 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
5205 return error_mark_node
;
5207 if ((invalid_op_diag
5208 = targetm
.invalid_binary_op (code
, type0
, type1
)))
5210 if (complain
& tf_error
)
5212 if (code0
== REAL_TYPE
5213 && code1
== REAL_TYPE
5214 && (extended_float_type_p (type0
)
5215 || extended_float_type_p (type1
))
5216 && cp_compare_floating_point_conversion_ranks (type0
,
5219 rich_location
richloc (line_table
, location
);
5220 binary_op_error (&richloc
, code
, type0
, type1
);
5223 error (invalid_op_diag
);
5225 return error_mark_node
;
5233 case TRUNC_DIV_EXPR
:
5235 case FLOOR_DIV_EXPR
:
5236 case ROUND_DIV_EXPR
:
5237 case EXACT_DIV_EXPR
:
5238 may_need_excess_precision
= true;
5246 case SPACESHIP_EXPR
:
5247 /* Excess precision for implicit conversions of integers to
5249 may_need_excess_precision
= (ANY_INTEGRAL_TYPE_P (type0
)
5250 || ANY_INTEGRAL_TYPE_P (type1
));
5253 may_need_excess_precision
= false;
5256 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
5258 op0
= TREE_OPERAND (op0
, 0);
5259 type0
= TREE_TYPE (op0
);
5261 else if (may_need_excess_precision
5262 && (code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
))
5263 if (tree eptype
= excess_precision_type (type0
))
5266 op0
= convert (eptype
, op0
);
5268 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
5270 op1
= TREE_OPERAND (op1
, 0);
5271 type1
= TREE_TYPE (op1
);
5273 else if (may_need_excess_precision
5274 && (code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
))
5275 if (tree eptype
= excess_precision_type (type1
))
5278 op1
= convert (eptype
, op1
);
5281 /* Issue warnings about peculiar, but valid, uses of NULL. */
5282 if ((null_node_p (orig_op0
) || null_node_p (orig_op1
))
5283 /* It's reasonable to use pointer values as operands of &&
5284 and ||, so NULL is no exception. */
5285 && code
!= TRUTH_ANDIF_EXPR
&& code
!= TRUTH_ORIF_EXPR
5286 && ( /* Both are NULL (or 0) and the operation was not a
5287 comparison or a pointer subtraction. */
5288 (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)
5289 && code
!= EQ_EXPR
&& code
!= NE_EXPR
&& code
!= MINUS_EXPR
)
5290 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5291 || (!null_ptr_cst_p (orig_op0
)
5292 && !TYPE_PTR_OR_PTRMEM_P (type0
))
5293 || (!null_ptr_cst_p (orig_op1
)
5294 && !TYPE_PTR_OR_PTRMEM_P (type1
)))
5295 && (complain
& tf_warning
))
5298 expansion_point_location_if_in_system_header (input_location
);
5300 warning_at (loc
, OPT_Wpointer_arith
, "NULL used in arithmetic");
5303 /* In case when one of the operands of the binary operation is
5304 a vector and another is a scalar -- convert scalar to vector. */
5305 if ((gnu_vector_type_p (type0
) && code1
!= VECTOR_TYPE
)
5306 || (gnu_vector_type_p (type1
) && code0
!= VECTOR_TYPE
))
5308 enum stv_conv convert_flag
5309 = scalar_to_vector (location
, code
, non_ep_op0
, non_ep_op1
,
5310 complain
& tf_error
);
5312 switch (convert_flag
)
5315 return error_mark_node
;
5318 op0
= convert (TREE_TYPE (type1
), op0
);
5319 op0
= save_expr (op0
);
5320 op0
= build_vector_from_val (type1
, op0
);
5321 orig_type0
= type0
= TREE_TYPE (op0
);
5322 code0
= TREE_CODE (type0
);
5328 op1
= convert (TREE_TYPE (type0
), op1
);
5329 op1
= save_expr (op1
);
5330 op1
= build_vector_from_val (type0
, op1
);
5331 orig_type1
= type1
= TREE_TYPE (op1
);
5332 code1
= TREE_CODE (type1
);
5344 /* Subtraction of two similar pointers.
5345 We must subtract them as integers, then divide by object size. */
5346 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
5347 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
5350 result
= pointer_diff (location
, op0
, op1
,
5351 common_pointer_type (type0
, type1
), complain
,
5353 if (instrument_expr
!= NULL
)
5354 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
5355 instrument_expr
, result
);
5359 /* In all other cases except pointer - int, the usual arithmetic
5361 else if (!(code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
))
5366 /* The pointer - int case is just like pointer + int; fall
5370 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
5371 && (code0
== INTEGER_TYPE
|| code1
== INTEGER_TYPE
))
5375 ptr_operand
= ((code0
== POINTER_TYPE
) ? op0
: op1
);
5376 int_operand
= ((code0
== INTEGER_TYPE
) ? op0
: op1
);
5377 if (processing_template_decl
)
5379 result_type
= TREE_TYPE (ptr_operand
);
5382 return cp_pointer_int_sum (location
, code
,
5394 case TRUNC_DIV_EXPR
:
5396 case FLOOR_DIV_EXPR
:
5397 case ROUND_DIV_EXPR
:
5398 case EXACT_DIV_EXPR
:
5399 if (TREE_CODE (op0
) == SIZEOF_EXPR
&& TREE_CODE (op1
) == SIZEOF_EXPR
)
5401 tree type0
= TREE_OPERAND (op0
, 0);
5402 tree type1
= TREE_OPERAND (op1
, 0);
5403 tree first_arg
= tree_strip_any_location_wrapper (type0
);
5404 if (!TYPE_P (type0
))
5405 type0
= TREE_TYPE (type0
);
5406 if (!TYPE_P (type1
))
5407 type1
= TREE_TYPE (type1
);
5408 if (INDIRECT_TYPE_P (type0
) && same_type_p (TREE_TYPE (type0
), type1
))
5410 if (!(TREE_CODE (first_arg
) == PARM_DECL
5411 && DECL_ARRAY_PARAMETER_P (first_arg
)
5412 && warn_sizeof_array_argument
)
5413 && (complain
& tf_warning
))
5415 auto_diagnostic_group d
;
5416 if (warning_at (location
, OPT_Wsizeof_pointer_div
,
5417 "division %<sizeof (%T) / sizeof (%T)%> does "
5418 "not compute the number of array elements",
5420 if (DECL_P (first_arg
))
5421 inform (DECL_SOURCE_LOCATION (first_arg
),
5422 "first %<sizeof%> operand was declared here");
5425 else if (TREE_CODE (type0
) == ARRAY_TYPE
5426 && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0
)))
5427 /* Set by finish_parenthesized_expr. */
5428 && !warning_suppressed_p (op1
, OPT_Wsizeof_array_div
)
5429 && (complain
& tf_warning
))
5430 maybe_warn_sizeof_array_div (location
, first_arg
, type0
,
5431 op1
, non_reference (type1
));
5434 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
5435 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
5436 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5437 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
5439 enum tree_code tcode0
= code0
, tcode1
= code1
;
5440 doing_div_or_mod
= true;
5441 warn_for_div_by_zero (location
, fold_for_warn (op1
));
5443 if (tcode0
== COMPLEX_TYPE
|| tcode0
== VECTOR_TYPE
)
5444 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
5445 if (tcode1
== COMPLEX_TYPE
|| tcode1
== VECTOR_TYPE
)
5446 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
5448 if (!(tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
))
5449 resultcode
= RDIV_EXPR
;
5452 /* When dividing two signed integers, we have to promote to int.
5453 unless we divide by a constant != -1. Note that default
5454 conversion will have been performed on the operands at this
5455 point, so we have to dig out the original type to find out if
5457 tree stripped_op1
= tree_strip_any_location_wrapper (op1
);
5458 shorten
= may_shorten_divmod (op0
, stripped_op1
);
5468 if ((code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
5469 || (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
5470 && !VECTOR_FLOAT_TYPE_P (type0
)
5471 && !VECTOR_FLOAT_TYPE_P (type1
)))
5475 case TRUNC_MOD_EXPR
:
5476 case FLOOR_MOD_EXPR
:
5477 doing_div_or_mod
= true;
5478 warn_for_div_by_zero (location
, fold_for_warn (op1
));
5480 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
5481 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
5482 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
5484 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
5486 /* Although it would be tempting to shorten always here, that loses
5487 on some targets, since the modulo instruction is undefined if the
5488 quotient can't be represented in the computation mode. We shorten
5489 only if unsigned or if dividing by something we know != -1. */
5490 tree stripped_op1
= tree_strip_any_location_wrapper (op1
);
5491 shorten
= may_shorten_divmod (op0
, stripped_op1
);
5496 case TRUTH_ANDIF_EXPR
:
5497 case TRUTH_ORIF_EXPR
:
5498 case TRUTH_AND_EXPR
:
5500 if (!VECTOR_TYPE_P (type0
) && gnu_vector_type_p (type1
))
5502 if (!COMPARISON_CLASS_P (op1
))
5503 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
5504 build_zero_cst (type1
), complain
);
5505 if (code
== TRUTH_ANDIF_EXPR
)
5507 tree z
= build_zero_cst (TREE_TYPE (op1
));
5508 return build_conditional_expr (location
, op0
, op1
, z
, complain
);
5510 else if (code
== TRUTH_ORIF_EXPR
)
5512 tree m1
= build_all_ones_cst (TREE_TYPE (op1
));
5513 return build_conditional_expr (location
, op0
, m1
, op1
, complain
);
5518 if (gnu_vector_type_p (type0
)
5519 && (!VECTOR_TYPE_P (type1
) || gnu_vector_type_p (type1
)))
5521 if (!COMPARISON_CLASS_P (op0
))
5522 op0
= cp_build_binary_op (EXPR_LOCATION (op0
), NE_EXPR
, op0
,
5523 build_zero_cst (type0
), complain
);
5524 if (!VECTOR_TYPE_P (type1
))
5526 tree m1
= build_all_ones_cst (TREE_TYPE (op0
));
5527 tree z
= build_zero_cst (TREE_TYPE (op0
));
5528 op1
= build_conditional_expr (location
, op1
, m1
, z
, complain
);
5530 else if (!COMPARISON_CLASS_P (op1
))
5531 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
5532 build_zero_cst (type1
), complain
);
5534 if (code
== TRUTH_ANDIF_EXPR
)
5535 code
= BIT_AND_EXPR
;
5536 else if (code
== TRUTH_ORIF_EXPR
)
5537 code
= BIT_IOR_EXPR
;
5541 return cp_build_binary_op (location
, code
, op0
, op1
, complain
);
5544 result_type
= boolean_type_node
;
5547 /* Shift operations: result has same type as first operand;
5548 always convert second operand to int.
5549 Also set SHORT_SHIFT if shifting rightward. */
5552 if (gnu_vector_type_p (type0
)
5553 && code1
== INTEGER_TYPE
5554 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
5556 result_type
= type0
;
5559 else if (gnu_vector_type_p (type0
)
5560 && gnu_vector_type_p (type1
)
5561 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
5562 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
5563 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
5564 TYPE_VECTOR_SUBPARTS (type1
)))
5566 result_type
= type0
;
5569 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
5571 tree const_op1
= fold_for_warn (op1
);
5572 if (TREE_CODE (const_op1
) != INTEGER_CST
)
5574 result_type
= type0
;
5576 if (TREE_CODE (const_op1
) == INTEGER_CST
)
5578 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
5580 if ((complain
& tf_warning
)
5581 && c_inhibit_evaluation_warnings
== 0)
5582 warning_at (location
, OPT_Wshift_count_negative
,
5583 "right shift count is negative");
5587 if (!integer_zerop (const_op1
))
5590 if (compare_tree_int (const_op1
, TYPE_PRECISION (type0
)) >= 0
5591 && (complain
& tf_warning
)
5592 && c_inhibit_evaluation_warnings
== 0)
5593 warning_at (location
, OPT_Wshift_count_overflow
,
5594 "right shift count >= width of type");
5597 /* Avoid converting op1 to result_type later. */
5603 if (gnu_vector_type_p (type0
)
5604 && code1
== INTEGER_TYPE
5605 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
5607 result_type
= type0
;
5610 else if (gnu_vector_type_p (type0
)
5611 && gnu_vector_type_p (type1
)
5612 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
5613 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
5614 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
5615 TYPE_VECTOR_SUBPARTS (type1
)))
5617 result_type
= type0
;
5620 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
5622 tree const_op0
= fold_for_warn (op0
);
5623 if (TREE_CODE (const_op0
) != INTEGER_CST
)
5625 tree const_op1
= fold_for_warn (op1
);
5626 if (TREE_CODE (const_op1
) != INTEGER_CST
)
5628 result_type
= type0
;
5630 if (TREE_CODE (const_op0
) == INTEGER_CST
5631 && tree_int_cst_sgn (const_op0
) < 0
5632 && !TYPE_OVERFLOW_WRAPS (type0
)
5633 && (complain
& tf_warning
)
5634 && c_inhibit_evaluation_warnings
== 0)
5635 warning_at (location
, OPT_Wshift_negative_value
,
5636 "left shift of negative value");
5637 if (TREE_CODE (const_op1
) == INTEGER_CST
)
5639 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
5641 if ((complain
& tf_warning
)
5642 && c_inhibit_evaluation_warnings
== 0)
5643 warning_at (location
, OPT_Wshift_count_negative
,
5644 "left shift count is negative");
5646 else if (compare_tree_int (const_op1
,
5647 TYPE_PRECISION (type0
)) >= 0)
5649 if ((complain
& tf_warning
)
5650 && c_inhibit_evaluation_warnings
== 0)
5651 warning_at (location
, OPT_Wshift_count_overflow
,
5652 "left shift count >= width of type");
5654 else if (TREE_CODE (const_op0
) == INTEGER_CST
5655 && (complain
& tf_warning
))
5656 maybe_warn_shift_overflow (location
, const_op0
, const_op1
);
5658 /* Avoid converting op1 to result_type later. */
5665 if (gnu_vector_type_p (type0
) && gnu_vector_type_p (type1
))
5666 goto vector_compare
;
5667 if ((complain
& tf_warning
)
5668 && c_inhibit_evaluation_warnings
== 0
5669 && (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
)))
5670 warning_at (location
, OPT_Wfloat_equal
,
5671 "comparing floating-point with %<==%> "
5672 "or %<!=%> is unsafe");
5673 if (complain
& tf_warning
)
5675 tree stripped_orig_op0
= tree_strip_any_location_wrapper (orig_op0
);
5676 tree stripped_orig_op1
= tree_strip_any_location_wrapper (orig_op1
);
5677 if ((TREE_CODE (stripped_orig_op0
) == STRING_CST
5678 && !integer_zerop (cp_fully_fold (op1
)))
5679 || (TREE_CODE (stripped_orig_op1
) == STRING_CST
5680 && !integer_zerop (cp_fully_fold (op0
))))
5681 warning_at (location
, OPT_Waddress
,
5682 "comparison with string literal results in "
5683 "unspecified behavior");
5684 else if (warn_array_compare
5685 && TREE_CODE (TREE_TYPE (orig_op0
)) == ARRAY_TYPE
5686 && TREE_CODE (TREE_TYPE (orig_op1
)) == ARRAY_TYPE
)
5687 do_warn_array_compare (location
, code
, stripped_orig_op0
,
5691 build_type
= boolean_type_node
;
5692 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
5693 || code0
== COMPLEX_TYPE
|| code0
== ENUMERAL_TYPE
)
5694 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5695 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
))
5697 else if (((code0
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type0
))
5698 && null_ptr_cst_p (orig_op1
))
5699 /* Handle, eg, (void*)0 (c++/43906), and more. */
5700 || (code0
== POINTER_TYPE
5701 && TYPE_PTR_P (type1
) && integer_zerop (op1
)))
5703 if (TYPE_PTR_P (type1
))
5704 result_type
= composite_pointer_type (location
,
5705 type0
, type1
, op0
, op1
,
5706 CPO_COMPARISON
, complain
);
5708 result_type
= type0
;
5710 if (char_type_p (TREE_TYPE (orig_op1
)))
5712 auto_diagnostic_group d
;
5713 if (warning_at (location
, OPT_Wpointer_compare
,
5714 "comparison between pointer and zero character "
5717 "did you mean to dereference the pointer?");
5719 warn_for_null_address (location
, op0
, complain
);
5721 else if (((code1
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type1
))
5722 && null_ptr_cst_p (orig_op0
))
5723 /* Handle, eg, (void*)0 (c++/43906), and more. */
5724 || (code1
== POINTER_TYPE
5725 && TYPE_PTR_P (type0
) && integer_zerop (op0
)))
5727 if (TYPE_PTR_P (type0
))
5728 result_type
= composite_pointer_type (location
,
5729 type0
, type1
, op0
, op1
,
5730 CPO_COMPARISON
, complain
);
5732 result_type
= type1
;
5734 if (char_type_p (TREE_TYPE (orig_op0
)))
5736 auto_diagnostic_group d
;
5737 if (warning_at (location
, OPT_Wpointer_compare
,
5738 "comparison between pointer and zero character "
5741 "did you mean to dereference the pointer?");
5743 warn_for_null_address (location
, op1
, complain
);
5745 else if ((code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5746 || (TYPE_PTRDATAMEM_P (type0
) && TYPE_PTRDATAMEM_P (type1
)))
5747 result_type
= composite_pointer_type (location
,
5748 type0
, type1
, op0
, op1
,
5749 CPO_COMPARISON
, complain
);
5750 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
5751 /* One of the operands must be of nullptr_t type. */
5752 result_type
= TREE_TYPE (nullptr_node
);
5753 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5755 result_type
= type0
;
5756 if (complain
& tf_error
)
5757 permerror (location
, "ISO C++ forbids comparison between "
5758 "pointer and integer");
5760 return error_mark_node
;
5762 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
5764 result_type
= type1
;
5765 if (complain
& tf_error
)
5766 permerror (location
, "ISO C++ forbids comparison between "
5767 "pointer and integer");
5769 return error_mark_node
;
5771 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (orig_op1
))
5773 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5774 == ptrmemfunc_vbit_in_delta
)
5776 tree pfn0
, delta0
, e1
, e2
;
5778 if (TREE_SIDE_EFFECTS (op0
))
5779 op0
= cp_save_expr (op0
);
5781 pfn0
= pfn_from_ptrmemfunc (op0
);
5782 delta0
= delta_from_ptrmemfunc (op0
);
5783 e1
= cp_build_binary_op (location
,
5786 build_zero_cst (TREE_TYPE (pfn0
)),
5788 e2
= cp_build_binary_op (location
,
5794 if (complain
& tf_warning
)
5795 maybe_warn_zero_as_null_pointer_constant (op1
, input_location
);
5797 e2
= cp_build_binary_op (location
,
5798 EQ_EXPR
, e2
, integer_zero_node
,
5800 op0
= cp_build_binary_op (location
,
5801 TRUTH_ANDIF_EXPR
, e1
, e2
,
5803 op1
= cp_convert (TREE_TYPE (op0
), integer_one_node
, complain
);
5807 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
5808 op1
= cp_convert (TREE_TYPE (op0
), op1
, complain
);
5810 result_type
= TREE_TYPE (op0
);
5812 warn_for_null_address (location
, orig_op0
, complain
);
5814 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (orig_op0
))
5815 return cp_build_binary_op (location
, code
, op1
, op0
, complain
);
5816 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
))
5819 /* E will be the final comparison. */
5821 /* E1 and E2 are for scratch. */
5829 type
= composite_pointer_type (location
, type0
, type1
, op0
, op1
,
5830 CPO_COMPARISON
, complain
);
5832 if (!same_type_p (TREE_TYPE (op0
), type
))
5833 op0
= cp_convert_and_check (type
, op0
, complain
);
5834 if (!same_type_p (TREE_TYPE (op1
), type
))
5835 op1
= cp_convert_and_check (type
, op1
, complain
);
5837 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5838 return error_mark_node
;
5840 if (TREE_SIDE_EFFECTS (op0
))
5841 op0
= save_expr (op0
);
5842 if (TREE_SIDE_EFFECTS (op1
))
5843 op1
= save_expr (op1
);
5845 pfn0
= pfn_from_ptrmemfunc (op0
);
5846 pfn0
= cp_fully_fold (pfn0
);
5847 /* Avoid -Waddress warnings (c++/64877). */
5848 if (TREE_CODE (pfn0
) == ADDR_EXPR
)
5849 suppress_warning (pfn0
, OPT_Waddress
);
5850 pfn1
= pfn_from_ptrmemfunc (op1
);
5851 pfn1
= cp_fully_fold (pfn1
);
5852 delta0
= delta_from_ptrmemfunc (op0
);
5853 delta1
= delta_from_ptrmemfunc (op1
);
5854 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5855 == ptrmemfunc_vbit_in_delta
)
5860 && ((op0.delta == op1.delta)
5861 || (!op0.pfn && op0.delta & 1 == 0
5862 && op1.delta & 1 == 0))
5864 The reason for the `!op0.pfn' bit is that a NULL
5865 pointer-to-member is any member with a zero PFN and
5866 LSB of the DELTA field is 0. */
5868 e1
= cp_build_binary_op (location
, BIT_AND_EXPR
,
5872 e1
= cp_build_binary_op (location
,
5873 EQ_EXPR
, e1
, integer_zero_node
,
5875 e2
= cp_build_binary_op (location
, BIT_AND_EXPR
,
5879 e2
= cp_build_binary_op (location
,
5880 EQ_EXPR
, e2
, integer_zero_node
,
5882 e1
= cp_build_binary_op (location
,
5883 TRUTH_ANDIF_EXPR
, e2
, e1
,
5885 e2
= cp_build_binary_op (location
, EQ_EXPR
,
5887 build_zero_cst (TREE_TYPE (pfn0
)),
5889 e2
= cp_build_binary_op (location
,
5890 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
5891 e1
= cp_build_binary_op (location
,
5892 EQ_EXPR
, delta0
, delta1
, complain
);
5893 e1
= cp_build_binary_op (location
,
5894 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
5901 && (!op0.pfn || op0.delta == op1.delta))
5903 The reason for the `!op0.pfn' bit is that a NULL
5904 pointer-to-member is any member with a zero PFN; the
5905 DELTA field is unspecified. */
5907 e1
= cp_build_binary_op (location
,
5908 EQ_EXPR
, delta0
, delta1
, complain
);
5909 e2
= cp_build_binary_op (location
,
5912 build_zero_cst (TREE_TYPE (pfn0
)),
5914 e1
= cp_build_binary_op (location
,
5915 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
5917 e2
= build2 (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
5918 e
= cp_build_binary_op (location
,
5919 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
5920 if (code
== EQ_EXPR
)
5922 return cp_build_binary_op (location
,
5923 EQ_EXPR
, e
, integer_zero_node
, complain
);
5927 gcc_assert (!TYPE_PTRMEMFUNC_P (type0
)
5928 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
),
5930 gcc_assert (!TYPE_PTRMEMFUNC_P (type1
)
5931 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
),
5939 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
5940 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
5942 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5943 result_type
= composite_pointer_type (location
,
5944 type0
, type1
, op0
, op1
,
5945 CPO_COMPARISON
, complain
);
5952 case SPACESHIP_EXPR
:
5953 if (TREE_CODE (orig_op0
) == STRING_CST
5954 || TREE_CODE (orig_op1
) == STRING_CST
)
5956 if (complain
& tf_warning
)
5957 warning_at (location
, OPT_Waddress
,
5958 "comparison with string literal results "
5959 "in unspecified behavior");
5961 else if (warn_array_compare
5962 && TREE_CODE (TREE_TYPE (orig_op0
)) == ARRAY_TYPE
5963 && TREE_CODE (TREE_TYPE (orig_op1
)) == ARRAY_TYPE
5964 && code
!= SPACESHIP_EXPR
5965 && (complain
& tf_warning
))
5966 do_warn_array_compare (location
, code
,
5967 tree_strip_any_location_wrapper (orig_op0
),
5968 tree_strip_any_location_wrapper (orig_op1
));
5970 if (gnu_vector_type_p (type0
) && gnu_vector_type_p (type1
))
5974 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
5976 && !vector_types_compatible_elements_p (type0
, type1
))
5978 if (complain
& tf_error
)
5980 error_at (location
, "comparing vectors with different "
5982 inform (location
, "operand types are %qT and %qT",
5985 return error_mark_node
;
5988 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0
),
5989 TYPE_VECTOR_SUBPARTS (type1
)))
5991 if (complain
& tf_error
)
5993 error_at (location
, "comparing vectors with different "
5994 "number of elements");
5995 inform (location
, "operand types are %qT and %qT",
5998 return error_mark_node
;
6001 /* It's not precisely specified how the usual arithmetic
6002 conversions apply to the vector types. Here, we use
6003 the unsigned type if one of the operands is signed and
6004 the other one is unsigned. */
6005 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
6007 if (!TYPE_UNSIGNED (type0
))
6008 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
6010 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
6011 warning_at (location
, OPT_Wsign_compare
, "comparison between "
6012 "types %qT and %qT", type0
, type1
);
6015 if (resultcode
== SPACESHIP_EXPR
)
6017 if (complain
& tf_error
)
6018 sorry_at (location
, "three-way comparison of vectors");
6019 return error_mark_node
;
6022 /* Always construct signed integer vector type. */
6023 intt
= c_common_type_for_size
6024 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0
))), 0);
6027 if (complain
& tf_error
)
6028 error_at (location
, "could not find an integer type "
6029 "of the same size as %qT", TREE_TYPE (type0
));
6030 return error_mark_node
;
6032 result_type
= build_opaque_vector_type (intt
,
6033 TYPE_VECTOR_SUBPARTS (type0
));
6034 return build_vec_cmp (resultcode
, result_type
, op0
, op1
);
6036 build_type
= boolean_type_node
;
6037 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
6038 || code0
== ENUMERAL_TYPE
)
6039 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
6040 || code1
== ENUMERAL_TYPE
))
6042 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
6043 result_type
= composite_pointer_type (location
,
6044 type0
, type1
, op0
, op1
,
6045 CPO_COMPARISON
, complain
);
6046 else if ((code0
== POINTER_TYPE
&& null_ptr_cst_p (orig_op1
))
6047 || (code1
== POINTER_TYPE
&& null_ptr_cst_p (orig_op0
))
6048 || (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)))
6050 /* Core Issue 1512 made this ill-formed. */
6051 if (complain
& tf_error
)
6052 error_at (location
, "ordered comparison of pointer with "
6053 "integer zero (%qT and %qT)", type0
, type1
);
6054 return error_mark_node
;
6056 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
6058 result_type
= type0
;
6059 if (complain
& tf_error
)
6060 permerror (location
, "ISO C++ forbids comparison between "
6061 "pointer and integer");
6063 return error_mark_node
;
6065 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
6067 result_type
= type1
;
6068 if (complain
& tf_error
)
6069 permerror (location
, "ISO C++ forbids comparison between "
6070 "pointer and integer");
6072 return error_mark_node
;
6075 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
6076 && !processing_template_decl
6077 && sanitize_flags_p (SANITIZE_POINTER_COMPARE
))
6079 op0
= save_expr (op0
);
6080 op1
= save_expr (op1
);
6082 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE
);
6083 instrument_expr
= build_call_expr_loc (location
, tt
, 2, op0
, op1
);
6088 case UNORDERED_EXPR
:
6095 build_type
= integer_type_node
;
6096 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
6098 if (complain
& tf_error
)
6099 error ("unordered comparison on non-floating-point argument");
6100 return error_mark_node
;
6109 if (((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
6110 || code0
== ENUMERAL_TYPE
)
6111 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
6112 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
)))
6113 arithmetic_types_p
= 1;
6116 arithmetic_types_p
= 0;
6117 /* Vector arithmetic is only allowed when both sides are vectors. */
6118 if (gnu_vector_type_p (type0
) && gnu_vector_type_p (type1
))
6120 if (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
6121 || !vector_types_compatible_elements_p (type0
, type1
))
6123 if (complain
& tf_error
)
6125 /* "location" already embeds the locations of the
6126 operands, so we don't need to add them separately
6128 rich_location
richloc (line_table
, location
);
6129 binary_op_error (&richloc
, code
, type0
, type1
);
6131 return error_mark_node
;
6133 arithmetic_types_p
= 1;
6136 /* Determine the RESULT_TYPE, if it is not already known. */
6138 && arithmetic_types_p
6139 && (shorten
|| common
|| short_compare
))
6141 result_type
= cp_common_type (type0
, type1
);
6142 if (result_type
== error_mark_node
)
6146 if (TREE_CODE (t1
) == COMPLEX_TYPE
)
6147 t1
= TREE_TYPE (t1
);
6148 if (TREE_CODE (t2
) == COMPLEX_TYPE
)
6149 t2
= TREE_TYPE (t2
);
6150 gcc_checking_assert (TREE_CODE (t1
) == REAL_TYPE
6151 && TREE_CODE (t2
) == REAL_TYPE
6152 && (extended_float_type_p (t1
)
6153 || extended_float_type_p (t2
))
6154 && cp_compare_floating_point_conversion_ranks
6156 if (complain
& tf_error
)
6158 rich_location
richloc (line_table
, location
);
6159 binary_op_error (&richloc
, code
, type0
, type1
);
6161 return error_mark_node
;
6163 if (complain
& tf_warning
)
6165 do_warn_double_promotion (result_type
, type0
, type1
,
6166 "implicit conversion from %qH to %qI "
6167 "to match other operand of binary "
6170 do_warn_enum_conversions (location
, code
, TREE_TYPE (orig_op0
),
6171 TREE_TYPE (orig_op1
));
6174 if (may_need_excess_precision
6175 && (orig_type0
!= type0
|| orig_type1
!= type1
)
6176 && build_type
== NULL_TREE
6179 gcc_assert (common
);
6180 semantic_result_type
= cp_common_type (orig_type0
, orig_type1
);
6181 if (semantic_result_type
== error_mark_node
)
6183 tree t1
= orig_type0
;
6184 tree t2
= orig_type1
;
6185 if (TREE_CODE (t1
) == COMPLEX_TYPE
)
6186 t1
= TREE_TYPE (t1
);
6187 if (TREE_CODE (t2
) == COMPLEX_TYPE
)
6188 t2
= TREE_TYPE (t2
);
6189 gcc_checking_assert (TREE_CODE (t1
) == REAL_TYPE
6190 && TREE_CODE (t2
) == REAL_TYPE
6191 && (extended_float_type_p (t1
)
6192 || extended_float_type_p (t2
))
6193 && cp_compare_floating_point_conversion_ranks
6195 if (complain
& tf_error
)
6197 rich_location
richloc (line_table
, location
);
6198 binary_op_error (&richloc
, code
, type0
, type1
);
6200 return error_mark_node
;
6204 if (code
== SPACESHIP_EXPR
)
6206 iloc_sentinel
s (location
);
6208 tree orig_type0
= TREE_TYPE (orig_op0
);
6209 tree_code orig_code0
= TREE_CODE (orig_type0
);
6210 tree orig_type1
= TREE_TYPE (orig_op1
);
6211 tree_code orig_code1
= TREE_CODE (orig_type1
);
6212 if (!result_type
|| result_type
== error_mark_node
)
6214 result_type
= NULL_TREE
;
6215 else if ((orig_code0
== BOOLEAN_TYPE
) != (orig_code1
== BOOLEAN_TYPE
))
6216 /* "If one of the operands is of type bool and the other is not, the
6217 program is ill-formed." */
6218 result_type
= NULL_TREE
;
6219 else if (code0
== POINTER_TYPE
&& orig_code0
!= POINTER_TYPE
6220 && code1
== POINTER_TYPE
&& orig_code1
!= POINTER_TYPE
)
6221 /* We only do array/function-to-pointer conversion if "at least one of
6222 the operands is of pointer type". */
6223 result_type
= NULL_TREE
;
6224 else if (TYPE_PTRFN_P (result_type
) || NULLPTR_TYPE_P (result_type
))
6225 /* <=> no longer supports equality relations. */
6226 result_type
= NULL_TREE
;
6227 else if (orig_code0
== ENUMERAL_TYPE
&& orig_code1
== ENUMERAL_TYPE
6228 && !(same_type_ignoring_top_level_qualifiers_p
6229 (orig_type0
, orig_type1
)))
6230 /* "If both operands have arithmetic types, or one operand has integral
6231 type and the other operand has unscoped enumeration type, the usual
6232 arithmetic conversions are applied to the operands." So we don't do
6233 arithmetic conversions if the operands both have enumeral type. */
6234 result_type
= NULL_TREE
;
6235 else if ((orig_code0
== ENUMERAL_TYPE
&& orig_code1
== REAL_TYPE
)
6236 || (orig_code0
== REAL_TYPE
&& orig_code1
== ENUMERAL_TYPE
))
6237 /* [depr.arith.conv.enum]: Three-way comparisons between such operands
6238 [where one is of enumeration type and the other is of a different
6239 enumeration type or a floating-point type] are ill-formed. */
6240 result_type
= NULL_TREE
;
6244 build_type
= spaceship_type (result_type
, complain
);
6245 if (build_type
== error_mark_node
)
6246 return error_mark_node
;
6249 if (result_type
&& arithmetic_types_p
)
6251 /* If a narrowing conversion is required, other than from an integral
6252 type to a floating point type, the program is ill-formed. */
6254 if (TREE_CODE (result_type
) == REAL_TYPE
6255 && CP_INTEGRAL_TYPE_P (orig_type0
))
6257 else if (!check_narrowing (result_type
, orig_op0
, complain
))
6259 if (TREE_CODE (result_type
) == REAL_TYPE
6260 && CP_INTEGRAL_TYPE_P (orig_type1
))
6262 else if (!check_narrowing (result_type
, orig_op1
, complain
))
6264 if (!ok
&& !(complain
& tf_error
))
6265 return error_mark_node
;
6271 if (complain
& tf_error
)
6273 binary_op_rich_location
richloc (location
,
6274 orig_op0
, orig_op1
, true);
6276 "invalid operands of types %qT and %qT to binary %qO",
6277 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
6279 return error_mark_node
;
6282 /* If we're in a template, the only thing we need to know is the
6284 if (processing_template_decl
)
6286 /* Since the middle-end checks the type when doing a build2, we
6287 need to build the tree in pieces. This built tree will never
6288 get out of the front-end as we replace it when instantiating
6290 tree tmp
= build2 (resultcode
,
6291 build_type
? build_type
: result_type
,
6293 TREE_OPERAND (tmp
, 0) = op0
;
6294 if (semantic_result_type
)
6295 tmp
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, tmp
);
6299 /* Remember the original type; RESULT_TYPE might be changed later on
6300 by shorten_binary_op. */
6301 tree orig_type
= result_type
;
6303 if (arithmetic_types_p
)
6305 bool first_complex
= (code0
== COMPLEX_TYPE
);
6306 bool second_complex
= (code1
== COMPLEX_TYPE
);
6307 int none_complex
= (!first_complex
&& !second_complex
);
6309 /* Adapted from patch for c/24581. */
6310 if (first_complex
!= second_complex
6311 && (code
== PLUS_EXPR
6312 || code
== MINUS_EXPR
6313 || code
== MULT_EXPR
6314 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
6315 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
6316 && flag_signed_zeros
)
6318 /* An operation on mixed real/complex operands must be
6319 handled specially, but the language-independent code can
6320 more easily optimize the plain complex arithmetic if
6321 -fno-signed-zeros. */
6322 tree real_type
= TREE_TYPE (result_type
);
6326 if (TREE_TYPE (op0
) != result_type
)
6327 op0
= cp_convert_and_check (result_type
, op0
, complain
);
6328 if (TREE_TYPE (op1
) != real_type
)
6329 op1
= cp_convert_and_check (real_type
, op1
, complain
);
6333 if (TREE_TYPE (op0
) != real_type
)
6334 op0
= cp_convert_and_check (real_type
, op0
, complain
);
6335 if (TREE_TYPE (op1
) != result_type
)
6336 op1
= cp_convert_and_check (result_type
, op1
, complain
);
6338 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
6339 return error_mark_node
;
6342 op0
= save_expr (op0
);
6343 real
= cp_build_unary_op (REALPART_EXPR
, op0
, true, complain
);
6344 imag
= cp_build_unary_op (IMAGPART_EXPR
, op0
, true, complain
);
6348 case TRUNC_DIV_EXPR
:
6349 op1
= save_expr (op1
);
6350 imag
= build2 (resultcode
, real_type
, imag
, op1
);
6354 real
= build2 (resultcode
, real_type
, real
, op1
);
6362 op1
= save_expr (op1
);
6363 real
= cp_build_unary_op (REALPART_EXPR
, op1
, true, complain
);
6364 imag
= cp_build_unary_op (IMAGPART_EXPR
, op1
, true, complain
);
6368 op0
= save_expr (op0
);
6369 imag
= build2 (resultcode
, real_type
, op0
, imag
);
6372 real
= build2 (resultcode
, real_type
, op0
, real
);
6375 real
= build2 (resultcode
, real_type
, op0
, real
);
6376 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
6382 result
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
6383 if (semantic_result_type
)
6384 result
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
,
6389 /* For certain operations (which identify themselves by shorten != 0)
6390 if both args were extended from the same smaller type,
6391 do the arithmetic in that type and then extend.
6393 shorten !=0 and !=1 indicates a bitwise operation.
6394 For them, this optimization is safe only if
6395 both args are zero-extended or both are sign-extended.
6396 Otherwise, we might change the result.
6397 E.g., (short)-1 | (unsigned short)-1 is (int)-1
6398 but calculated in (unsigned short) it would be (unsigned short)-1. */
6400 if (shorten
&& none_complex
)
6402 final_type
= result_type
;
6403 result_type
= shorten_binary_op (result_type
, op0
, op1
,
6407 /* Shifts can be shortened if shifting right. */
6412 tree arg0
= get_narrower (op0
, &unsigned_arg
);
6413 /* We're not really warning here but when we set short_shift we
6414 used fold_for_warn to fold the operand. */
6415 tree const_op1
= fold_for_warn (op1
);
6417 final_type
= result_type
;
6419 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
6420 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
6422 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
6423 && tree_int_cst_sgn (const_op1
) > 0
6424 /* We can shorten only if the shift count is less than the
6425 number of bits in the smaller type size. */
6426 && compare_tree_int (const_op1
,
6427 TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
6428 /* We cannot drop an unsigned shift after sign-extension. */
6429 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
6431 /* Do an unsigned shift if the operand was zero-extended. */
6433 = c_common_signed_or_unsigned_type (unsigned_arg
,
6435 /* Convert value-to-be-shifted to that type. */
6436 if (TREE_TYPE (op0
) != result_type
)
6437 op0
= convert (result_type
, op0
);
6442 /* Comparison operations are shortened too but differently.
6443 They identify themselves by setting short_compare = 1. */
6447 /* We call shorten_compare only for diagnostics. */
6448 tree xop0
= fold_simple (op0
);
6449 tree xop1
= fold_simple (op1
);
6450 tree xresult_type
= result_type
;
6451 enum tree_code xresultcode
= resultcode
;
6452 shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
6456 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
6457 && warn_sign_compare
6458 /* Do not warn until the template is instantiated; we cannot
6459 bound the ranges of the arguments until that point. */
6460 && !processing_template_decl
6461 && (complain
& tf_warning
)
6462 && c_inhibit_evaluation_warnings
== 0
6463 /* Even unsigned enum types promote to signed int. We don't
6464 want to issue -Wsign-compare warnings for this case. */
6465 && !enum_cast_to_int (orig_op0
)
6466 && !enum_cast_to_int (orig_op1
))
6468 warn_for_sign_compare (location
, orig_op0
, orig_op1
, op0
, op1
,
6469 result_type
, resultcode
);
6473 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6474 Then the expression will be built.
6475 It will be given type FINAL_TYPE if that is nonzero;
6476 otherwise, it will be given type RESULT_TYPE. */
6479 warning_sentinel
w (warn_sign_conversion
, short_compare
);
6480 if (!same_type_p (TREE_TYPE (op0
), result_type
))
6481 op0
= cp_convert_and_check (result_type
, op0
, complain
);
6482 if (!same_type_p (TREE_TYPE (op1
), result_type
))
6483 op1
= cp_convert_and_check (result_type
, op1
, complain
);
6485 if (op0
== error_mark_node
|| op1
== error_mark_node
)
6486 return error_mark_node
;
6489 if (build_type
== NULL_TREE
)
6490 build_type
= result_type
;
6493 && flag_strong_eval_order
== 2
6494 && TREE_SIDE_EFFECTS (op1
)
6495 && !processing_template_decl
)
6497 /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6498 op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6499 op0
= cp_save_expr (op0
);
6500 instrument_expr
= op0
;
6503 if (sanitize_flags_p ((SANITIZE_SHIFT
6505 | SANITIZE_FLOAT_DIVIDE
6506 | SANITIZE_SI_OVERFLOW
))
6507 && current_function_decl
!= NULL_TREE
6508 && !processing_template_decl
6509 && (doing_div_or_mod
|| doing_shift
))
6511 /* OP0 and/or OP1 might have side-effects. */
6512 op0
= cp_save_expr (op0
);
6513 op1
= cp_save_expr (op1
);
6514 op0
= fold_non_dependent_expr (op0
, complain
);
6515 op1
= fold_non_dependent_expr (op1
, complain
);
6516 tree instrument_expr1
= NULL_TREE
;
6517 if (doing_div_or_mod
6518 && sanitize_flags_p (SANITIZE_DIVIDE
6519 | SANITIZE_FLOAT_DIVIDE
6520 | SANITIZE_SI_OVERFLOW
))
6522 /* For diagnostics we want to use the promoted types without
6523 shorten_binary_op. So convert the arguments to the
6524 original result_type. */
6527 if (TREE_TYPE (cop0
) != orig_type
)
6528 cop0
= cp_convert (orig_type
, op0
, complain
);
6529 if (TREE_TYPE (cop1
) != orig_type
)
6530 cop1
= cp_convert (orig_type
, op1
, complain
);
6531 instrument_expr1
= ubsan_instrument_division (location
, cop0
, cop1
);
6533 else if (doing_shift
&& sanitize_flags_p (SANITIZE_SHIFT
))
6534 instrument_expr1
= ubsan_instrument_shift (location
, code
, op0
, op1
);
6535 if (instrument_expr
!= NULL
)
6536 instrument_expr
= add_stmt_to_compound (instrument_expr
,
6539 instrument_expr
= instrument_expr1
;
6542 result
= build2_loc (location
, resultcode
, build_type
, op0
, op1
);
6543 if (final_type
!= 0)
6544 result
= cp_convert (final_type
, result
, complain
);
6546 if (instrument_expr
!= NULL
)
6547 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
6548 instrument_expr
, result
);
6550 if (resultcode
== SPACESHIP_EXPR
&& !processing_template_decl
)
6551 result
= get_target_expr (result
, complain
);
6553 if (semantic_result_type
)
6554 result
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, result
);
6556 if (!c_inhibit_evaluation_warnings
)
6558 if (!processing_template_decl
)
6560 op0
= cp_fully_fold (op0
);
6561 /* Only consider the second argument if the first isn't overflowed. */
6562 if (!CONSTANT_CLASS_P (op0
) || TREE_OVERFLOW_P (op0
))
6564 op1
= cp_fully_fold (op1
);
6565 if (!CONSTANT_CLASS_P (op1
) || TREE_OVERFLOW_P (op1
))
6568 else if (!CONSTANT_CLASS_P (op0
) || !CONSTANT_CLASS_P (op1
)
6569 || TREE_OVERFLOW_P (op0
) || TREE_OVERFLOW_P (op1
))
6572 tree result_ovl
= fold_build2 (resultcode
, build_type
, op0
, op1
);
6573 if (TREE_OVERFLOW_P (result_ovl
))
6574 overflow_warning (location
, result_ovl
);
6580 /* Build a VEC_PERM_EXPR.
6581 This is a simple wrapper for c_build_vec_perm_expr. */
6583 build_x_vec_perm_expr (location_t loc
,
6584 tree arg0
, tree arg1
, tree arg2
,
6585 tsubst_flags_t complain
)
6587 tree orig_arg0
= arg0
;
6588 tree orig_arg1
= arg1
;
6589 tree orig_arg2
= arg2
;
6590 if (processing_template_decl
)
6592 if (type_dependent_expression_p (arg0
)
6593 || type_dependent_expression_p (arg1
)
6594 || type_dependent_expression_p (arg2
))
6595 return build_min_nt_loc (loc
, VEC_PERM_EXPR
, arg0
, arg1
, arg2
);
6596 arg0
= build_non_dependent_expr (arg0
);
6598 arg1
= build_non_dependent_expr (arg1
);
6599 arg2
= build_non_dependent_expr (arg2
);
6601 tree exp
= c_build_vec_perm_expr (loc
, arg0
, arg1
, arg2
, complain
& tf_error
);
6602 if (processing_template_decl
&& exp
!= error_mark_node
)
6603 return build_min_non_dep (VEC_PERM_EXPR
, exp
, orig_arg0
,
6604 orig_arg1
, orig_arg2
);
6608 /* Build a VEC_PERM_EXPR.
6609 This is a simple wrapper for c_build_shufflevector. */
6611 build_x_shufflevector (location_t loc
, vec
<tree
, va_gc
> *args
,
6612 tsubst_flags_t complain
)
6614 tree arg0
= (*args
)[0];
6615 tree arg1
= (*args
)[1];
6616 if (processing_template_decl
)
6618 for (unsigned i
= 0; i
< args
->length (); ++i
)
6620 ? type_dependent_expression_p ((*args
)[i
])
6621 : instantiation_dependent_expression_p ((*args
)[i
]))
6623 tree exp
= build_min_nt_call_vec (NULL
, args
);
6624 CALL_EXPR_IFN (exp
) = IFN_SHUFFLEVECTOR
;
6627 arg0
= build_non_dependent_expr (arg0
);
6628 arg1
= build_non_dependent_expr (arg1
);
6629 /* ??? Nothing needed for the index arguments? */
6631 auto_vec
<tree
, 16> mask
;
6632 for (unsigned i
= 2; i
< args
->length (); ++i
)
6634 tree idx
= fold_non_dependent_expr ((*args
)[i
], complain
);
6635 mask
.safe_push (idx
);
6637 tree exp
= c_build_shufflevector (loc
, arg0
, arg1
, mask
, complain
& tf_error
);
6638 if (processing_template_decl
&& exp
!= error_mark_node
)
6640 exp
= build_min_non_dep_call_vec (exp
, NULL
, args
);
6641 CALL_EXPR_IFN (exp
) = IFN_SHUFFLEVECTOR
;
6646 /* Return a tree for the sum or difference (RESULTCODE says which)
6647 of pointer PTROP and integer INTOP. */
6650 cp_pointer_int_sum (location_t loc
, enum tree_code resultcode
, tree ptrop
,
6651 tree intop
, tsubst_flags_t complain
)
6653 tree res_type
= TREE_TYPE (ptrop
);
6655 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
6656 in certain circumstance (when it's valid to do so). So we need
6657 to make sure it's complete. We don't need to check here, if we
6658 can actually complete it at all, as those checks will be done in
6659 pointer_int_sum() anyway. */
6660 complete_type (TREE_TYPE (res_type
));
6662 return pointer_int_sum (loc
, resultcode
, ptrop
,
6663 intop
, complain
& tf_warning_or_error
);
6666 /* Return a tree for the difference of pointers OP0 and OP1.
6667 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
6668 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
6671 pointer_diff (location_t loc
, tree op0
, tree op1
, tree ptrtype
,
6672 tsubst_flags_t complain
, tree
*instrument_expr
)
6674 tree result
, inttype
;
6675 tree restype
= ptrdiff_type_node
;
6676 tree target_type
= TREE_TYPE (ptrtype
);
6678 if (!complete_type_or_maybe_complain (target_type
, NULL_TREE
, complain
))
6679 return error_mark_node
;
6681 if (VOID_TYPE_P (target_type
))
6683 if (complain
& tf_error
)
6684 permerror (loc
, "ISO C++ forbids using pointer of "
6685 "type %<void *%> in subtraction");
6687 return error_mark_node
;
6689 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
6691 if (complain
& tf_error
)
6692 permerror (loc
, "ISO C++ forbids using pointer to "
6693 "a function in subtraction");
6695 return error_mark_node
;
6697 if (TREE_CODE (target_type
) == METHOD_TYPE
)
6699 if (complain
& tf_error
)
6700 permerror (loc
, "ISO C++ forbids using pointer to "
6701 "a method in subtraction");
6703 return error_mark_node
;
6705 else if (!verify_type_context (loc
, TCTX_POINTER_ARITH
,
6706 TREE_TYPE (TREE_TYPE (op0
)),
6707 !(complain
& tf_error
))
6708 || !verify_type_context (loc
, TCTX_POINTER_ARITH
,
6709 TREE_TYPE (TREE_TYPE (op1
)),
6710 !(complain
& tf_error
)))
6711 return error_mark_node
;
6713 /* Determine integer type result of the subtraction. This will usually
6714 be the same as the result type (ptrdiff_t), but may need to be a wider
6715 type if pointers for the address space are wider than ptrdiff_t. */
6716 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
6717 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
6721 if (!processing_template_decl
6722 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT
))
6724 op0
= save_expr (op0
);
6725 op1
= save_expr (op1
);
6727 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT
);
6728 *instrument_expr
= build_call_expr_loc (loc
, tt
, 2, op0
, op1
);
6731 /* First do the subtraction, then build the divide operator
6732 and only convert at the very end.
6733 Do not do default conversions in case restype is a short type. */
6735 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
6736 pointers. If some platform cannot provide that, or has a larger
6737 ptrdiff_type to support differences larger than half the address
6738 space, cast the pointers to some larger integer type and do the
6739 computations in that type. */
6740 if (TYPE_PRECISION (inttype
) > TYPE_PRECISION (TREE_TYPE (op0
)))
6741 op0
= cp_build_binary_op (loc
,
6743 cp_convert (inttype
, op0
, complain
),
6744 cp_convert (inttype
, op1
, complain
),
6747 op0
= build2_loc (loc
, POINTER_DIFF_EXPR
, inttype
, op0
, op1
);
6749 /* This generates an error if op1 is a pointer to an incomplete type. */
6750 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
6752 if (complain
& tf_error
)
6753 error_at (loc
, "invalid use of a pointer to an incomplete type in "
6754 "pointer arithmetic");
6756 return error_mark_node
;
6759 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1
)))
6761 if (complain
& tf_error
)
6762 error_at (loc
, "arithmetic on pointer to an empty aggregate");
6764 return error_mark_node
;
6767 op1
= (TYPE_PTROB_P (ptrtype
)
6768 ? size_in_bytes_loc (loc
, target_type
)
6769 : integer_one_node
);
6771 /* Do the division. */
6773 result
= build2_loc (loc
, EXACT_DIV_EXPR
, inttype
, op0
,
6774 cp_convert (inttype
, op1
, complain
));
6775 return cp_convert (restype
, result
, complain
);
6778 /* Construct and perhaps optimize a tree representation
6779 for a unary operation. CODE, a tree_code, specifies the operation
6780 and XARG is the operand. */
6783 build_x_unary_op (location_t loc
, enum tree_code code
, cp_expr xarg
,
6784 tree lookups
, tsubst_flags_t complain
)
6786 tree orig_expr
= xarg
;
6789 tree overload
= NULL_TREE
;
6791 if (processing_template_decl
)
6793 if (type_dependent_expression_p (xarg
))
6795 tree e
= build_min_nt_loc (loc
, code
, xarg
.get_value (), NULL_TREE
);
6796 TREE_TYPE (e
) = build_dependent_operator_type (lookups
, code
, false);
6800 xarg
= build_non_dependent_expr (xarg
);
6805 /* [expr.unary.op] says:
6807 The address of an object of incomplete type can be taken.
6809 (And is just the ordinary address operator, not an overloaded
6810 "operator &".) However, if the type is a template
6811 specialization, we must complete the type at this point so that
6812 an overloaded "operator &" will be available if required. */
6813 if (code
== ADDR_EXPR
6814 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
6815 && ((CLASS_TYPE_P (TREE_TYPE (xarg
))
6816 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg
))))
6817 || (TREE_CODE (xarg
) == OFFSET_REF
)))
6818 /* Don't look for a function. */;
6820 exp
= build_new_op (loc
, code
, LOOKUP_NORMAL
, xarg
, NULL_TREE
,
6821 NULL_TREE
, lookups
, &overload
, complain
);
6823 if (!exp
&& code
== ADDR_EXPR
)
6825 if (is_overloaded_fn (xarg
))
6827 tree fn
= get_first_fn (xarg
);
6828 if (DECL_CONSTRUCTOR_P (fn
) || DECL_DESTRUCTOR_P (fn
))
6830 if (complain
& tf_error
)
6831 error_at (loc
, DECL_CONSTRUCTOR_P (fn
)
6832 ? G_("taking address of constructor %qD")
6833 : G_("taking address of destructor %qD"),
6835 return error_mark_node
;
6839 /* A pointer to member-function can be formed only by saying
6841 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
6842 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
6844 if (TREE_CODE (xarg
) != OFFSET_REF
6845 || !TYPE_P (TREE_OPERAND (xarg
, 0)))
6847 if (complain
& tf_error
)
6849 error_at (loc
, "invalid use of %qE to form a "
6850 "pointer-to-member-function", xarg
.get_value ());
6851 if (TREE_CODE (xarg
) != OFFSET_REF
)
6852 inform (loc
, " a qualified-id is required");
6854 return error_mark_node
;
6858 if (complain
& tf_error
)
6859 error_at (loc
, "parentheses around %qE cannot be used to "
6860 "form a pointer-to-member-function",
6863 return error_mark_node
;
6864 PTRMEM_OK_P (xarg
) = 1;
6868 if (TREE_CODE (xarg
) == OFFSET_REF
)
6870 ptrmem
= PTRMEM_OK_P (xarg
);
6872 if (!ptrmem
&& !flag_ms_extensions
6873 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
6875 /* A single non-static member, make sure we don't allow a
6876 pointer-to-member. */
6877 xarg
= build2 (OFFSET_REF
, TREE_TYPE (xarg
),
6878 TREE_OPERAND (xarg
, 0),
6879 ovl_make (TREE_OPERAND (xarg
, 1)));
6880 PTRMEM_OK_P (xarg
) = ptrmem
;
6884 exp
= cp_build_addr_expr_strict (xarg
, complain
);
6886 if (TREE_CODE (exp
) == PTRMEM_CST
)
6887 PTRMEM_CST_LOCATION (exp
) = loc
;
6889 protected_set_expr_location (exp
, loc
);
6892 if (processing_template_decl
&& exp
!= error_mark_node
)
6894 if (overload
!= NULL_TREE
)
6895 return (build_min_non_dep_op_overload
6896 (code
, exp
, overload
, orig_expr
, integer_zero_node
));
6898 exp
= build_min_non_dep (code
, exp
, orig_expr
,
6899 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE
);
6901 if (TREE_CODE (exp
) == ADDR_EXPR
)
6902 PTRMEM_OK_P (exp
) = ptrmem
;
6906 /* Construct and perhaps optimize a tree representation
6907 for __builtin_addressof operation. ARG specifies the operand. */
6910 cp_build_addressof (location_t loc
, tree arg
, tsubst_flags_t complain
)
6912 tree orig_expr
= arg
;
6914 if (processing_template_decl
)
6916 if (type_dependent_expression_p (arg
))
6917 return build_min_nt_loc (loc
, ADDRESSOF_EXPR
, arg
, NULL_TREE
);
6919 arg
= build_non_dependent_expr (arg
);
6922 tree exp
= cp_build_addr_expr_strict (arg
, complain
);
6924 if (processing_template_decl
&& exp
!= error_mark_node
)
6925 exp
= build_min_non_dep (ADDRESSOF_EXPR
, exp
, orig_expr
, NULL_TREE
);
6929 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
6930 constants, where a null value is represented by an INTEGER_CST of
6934 cp_truthvalue_conversion (tree expr
, tsubst_flags_t complain
)
6936 tree type
= TREE_TYPE (expr
);
6937 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6938 if (TYPE_PTR_OR_PTRMEM_P (type
)
6939 /* Avoid ICE on invalid use of non-static member function. */
6940 || TREE_CODE (expr
) == FUNCTION_DECL
)
6941 return cp_build_binary_op (loc
, NE_EXPR
, expr
, nullptr_node
, complain
);
6943 return c_common_truthvalue_conversion (loc
, expr
);
6946 /* Returns EXPR contextually converted to bool. */
6949 contextual_conv_bool (tree expr
, tsubst_flags_t complain
)
6951 return perform_implicit_conversion_flags (boolean_type_node
, expr
,
6952 complain
, LOOKUP_NORMAL
);
6955 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
6956 is a low-level function; most callers should use maybe_convert_cond. */
6959 condition_conversion (tree expr
)
6961 tree t
= contextual_conv_bool (expr
, tf_warning_or_error
);
6962 if (!processing_template_decl
)
6963 t
= fold_build_cleanup_point_expr (boolean_type_node
, t
);
6967 /* Returns the address of T. This function will fold away
6968 ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
6969 most places should use cp_build_addr_expr instead. */
6972 build_address (tree t
)
6974 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
6975 return error_mark_node
;
6976 gcc_checking_assert (TREE_CODE (t
) != CONSTRUCTOR
6977 || processing_template_decl
);
6978 t
= build_fold_addr_expr_loc (EXPR_LOCATION (t
), t
);
6979 if (TREE_CODE (t
) != ADDR_EXPR
)
6984 /* Return a NOP_EXPR converting EXPR to TYPE. */
6987 build_nop (tree type
, tree expr
)
6989 if (type
== error_mark_node
|| error_operand_p (expr
))
6991 return build1_loc (EXPR_LOCATION (expr
), NOP_EXPR
, type
, expr
);
6994 /* Take the address of ARG, whatever that means under C++ semantics.
6995 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
6996 and class rvalues as well.
6998 Nothing should call this function directly; instead, callers should use
6999 cp_build_addr_expr or cp_build_addr_expr_strict. */
7002 cp_build_addr_expr_1 (tree arg
, bool strict_lvalue
, tsubst_flags_t complain
)
7007 if (!arg
|| error_operand_p (arg
))
7008 return error_mark_node
;
7010 arg
= mark_lvalue_use (arg
);
7011 if (error_operand_p (arg
))
7012 return error_mark_node
;
7014 argtype
= lvalue_type (arg
);
7015 location_t loc
= cp_expr_loc_or_input_loc (arg
);
7017 gcc_assert (!(identifier_p (arg
) && IDENTIFIER_ANY_OP_P (arg
)));
7019 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
7020 && !really_overloaded_fn (arg
))
7022 /* They're trying to take the address of a unique non-static
7023 member function. This is ill-formed (except in MS-land),
7024 but let's try to DTRT.
7025 Note: We only handle unique functions here because we don't
7026 want to complain if there's a static overload; non-unique
7027 cases will be handled by instantiate_type. But we need to
7028 handle this case here to allow casts on the resulting PMF.
7029 We could defer this in non-MS mode, but it's easier to give
7030 a useful error here. */
7032 /* Inside constant member functions, the `this' pointer
7033 contains an extra const qualifier. TYPE_MAIN_VARIANT
7034 is used here to remove this const from the diagnostics
7035 and the created OFFSET_REF. */
7036 tree base
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg
, 0)));
7037 tree fn
= get_first_fn (TREE_OPERAND (arg
, 1));
7038 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
7039 return error_mark_node
;
7041 if (! flag_ms_extensions
)
7043 tree name
= DECL_NAME (fn
);
7044 if (!(complain
& tf_error
))
7045 return error_mark_node
;
7046 else if (current_class_type
7047 && TREE_OPERAND (arg
, 0) == current_class_ref
)
7048 /* An expression like &memfn. */
7050 "ISO C++ forbids taking the address of an unqualified"
7051 " or parenthesized non-static member function to form"
7052 " a pointer to member function. Say %<&%T::%D%>",
7056 "ISO C++ forbids taking the address of a bound member"
7057 " function to form a pointer to member function."
7061 arg
= build_offset_ref (base
, fn
, /*address_p=*/true, complain
);
7064 /* Uninstantiated types are all functions. Taking the
7065 address of a function is a no-op, so just return the
7067 if (type_unknown_p (arg
))
7068 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
7070 if (TREE_CODE (arg
) == OFFSET_REF
)
7071 /* We want a pointer to member; bypass all the code for actually taking
7072 the address of something. */
7075 /* Anything not already handled and not a true memory reference
7077 if (!FUNC_OR_METHOD_TYPE_P (argtype
))
7079 cp_lvalue_kind kind
= lvalue_kind (arg
);
7080 if (kind
== clk_none
)
7082 if (complain
& tf_error
)
7083 lvalue_error (loc
, lv_addressof
);
7084 return error_mark_node
;
7086 if (strict_lvalue
&& (kind
& (clk_rvalueref
|clk_class
)))
7088 if (!(complain
& tf_error
))
7089 return error_mark_node
;
7090 /* Make this a permerror because we used to accept it. */
7091 permerror (loc
, "taking address of rvalue");
7095 if (TYPE_REF_P (argtype
))
7097 tree type
= build_pointer_type (TREE_TYPE (argtype
));
7098 arg
= build1 (CONVERT_EXPR
, type
, arg
);
7101 else if (pedantic
&& DECL_MAIN_P (tree_strip_any_location_wrapper (arg
)))
7104 /* Apparently a lot of autoconf scripts for C++ packages do this,
7105 so only complain if -Wpedantic. */
7106 if (complain
& (flag_pedantic_errors
? tf_error
: tf_warning
))
7107 pedwarn (loc
, OPT_Wpedantic
,
7108 "ISO C++ forbids taking address of function %<::main%>");
7109 else if (flag_pedantic_errors
)
7110 return error_mark_node
;
7113 /* Let &* cancel out to simplify resulting code. */
7114 if (INDIRECT_REF_P (arg
))
7116 arg
= TREE_OPERAND (arg
, 0);
7117 if (TYPE_REF_P (TREE_TYPE (arg
)))
7119 tree type
= build_pointer_type (TREE_TYPE (TREE_TYPE (arg
)));
7120 arg
= build1 (CONVERT_EXPR
, type
, arg
);
7123 /* Don't let this be an lvalue. */
7128 /* Handle complex lvalues (when permitted)
7129 by reduction to simpler cases. */
7130 val
= unary_complex_lvalue (ADDR_EXPR
, arg
);
7134 switch (TREE_CODE (arg
))
7138 case FIX_TRUNC_EXPR
:
7139 /* We should have handled this above in the lvalue_kind check. */
7144 arg
= BASELINK_FUNCTIONS (arg
);
7148 arg
= OVL_FIRST (arg
);
7153 /* Turn a reference to a non-static data member into a
7154 pointer-to-member. */
7159 gcc_assert (PTRMEM_OK_P (arg
));
7161 t
= TREE_OPERAND (arg
, 1);
7162 if (TYPE_REF_P (TREE_TYPE (t
)))
7164 if (complain
& tf_error
)
7166 "cannot create pointer to reference member %qD", t
);
7167 return error_mark_node
;
7170 /* Forming a pointer-to-member is a use of non-pure-virtual fns. */
7171 if (TREE_CODE (t
) == FUNCTION_DECL
7172 && !DECL_PURE_VIRTUAL_P (t
)
7173 && !mark_used (t
, complain
) && !(complain
& tf_error
))
7174 return error_mark_node
;
7176 type
= build_ptrmem_type (context_for_name_lookup (t
),
7178 t
= make_ptrmem_cst (type
, t
);
7186 if (argtype
!= error_mark_node
)
7187 argtype
= build_pointer_type (argtype
);
7189 if (bitfield_p (arg
))
7191 if (complain
& tf_error
)
7192 error_at (loc
, "attempt to take address of bit-field");
7193 return error_mark_node
;
7196 /* In a template, we are processing a non-dependent expression
7197 so we can just form an ADDR_EXPR with the correct type. */
7198 if (processing_template_decl
|| TREE_CODE (arg
) != COMPONENT_REF
)
7200 if (!mark_single_function (arg
, complain
))
7201 return error_mark_node
;
7202 val
= build_address (arg
);
7203 if (TREE_CODE (arg
) == OFFSET_REF
)
7204 PTRMEM_OK_P (val
) = PTRMEM_OK_P (arg
);
7206 else if (BASELINK_P (TREE_OPERAND (arg
, 1)))
7208 tree fn
= BASELINK_FUNCTIONS (TREE_OPERAND (arg
, 1));
7210 /* We can only get here with a single static member
7212 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
7213 && DECL_STATIC_FUNCTION_P (fn
));
7214 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
7215 return error_mark_node
;
7216 val
= build_address (fn
);
7217 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
7218 /* Do not lose object's side effects. */
7219 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
),
7220 TREE_OPERAND (arg
, 0), val
);
7224 tree object
= TREE_OPERAND (arg
, 0);
7225 tree field
= TREE_OPERAND (arg
, 1);
7226 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7227 (TREE_TYPE (object
), decl_type_context (field
)));
7228 val
= build_address (arg
);
7231 if (TYPE_PTR_P (argtype
)
7232 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
7234 build_ptrmemfunc_type (argtype
);
7235 val
= build_ptrmemfunc (argtype
, val
, 0,
7240 /* For addresses of immediate functions ensure we have EXPR_LOCATION
7241 set for possible later diagnostics. */
7242 if (TREE_CODE (val
) == ADDR_EXPR
7243 && TREE_CODE (TREE_OPERAND (val
, 0)) == FUNCTION_DECL
7244 && DECL_IMMEDIATE_FUNCTION_P (TREE_OPERAND (val
, 0)))
7245 SET_EXPR_LOCATION (val
, input_location
);
7250 /* Take the address of ARG if it has one, even if it's an rvalue. */
7253 cp_build_addr_expr (tree arg
, tsubst_flags_t complain
)
7255 return cp_build_addr_expr_1 (arg
, 0, complain
);
7258 /* Take the address of ARG, but only if it's an lvalue. */
7261 cp_build_addr_expr_strict (tree arg
, tsubst_flags_t complain
)
7263 return cp_build_addr_expr_1 (arg
, 1, complain
);
7266 /* C++: Must handle pointers to members.
7268 Perhaps type instantiation should be extended to handle conversion
7269 from aggregates to types we don't yet know we want? (Or are those
7270 cases typically errors which should be reported?)
7272 NOCONVERT suppresses the default promotions (such as from short to int). */
7275 cp_build_unary_op (enum tree_code code
, tree xarg
, bool noconvert
,
7276 tsubst_flags_t complain
)
7278 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
7280 location_t location
= cp_expr_loc_or_input_loc (arg
);
7282 tree eptype
= NULL_TREE
;
7283 const char *errstring
= NULL
;
7285 const char *invalid_op_diag
;
7287 if (!arg
|| error_operand_p (arg
))
7288 return error_mark_node
;
7290 arg
= resolve_nondeduced_context (arg
, complain
);
7292 if ((invalid_op_diag
7293 = targetm
.invalid_unary_op ((code
== UNARY_PLUS_EXPR
7298 if (complain
& tf_error
)
7299 error (invalid_op_diag
);
7300 return error_mark_node
;
7303 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
7305 eptype
= TREE_TYPE (arg
);
7306 arg
= TREE_OPERAND (arg
, 0);
7311 case UNARY_PLUS_EXPR
:
7314 int flags
= WANT_ARITH
| WANT_ENUM
;
7315 /* Unary plus (but not unary minus) is allowed on pointers. */
7316 if (code
== UNARY_PLUS_EXPR
)
7317 flags
|= WANT_POINTER
;
7318 arg
= build_expr_type_conversion (flags
, arg
, true);
7320 errstring
= (code
== NEGATE_EXPR
7321 ? _("wrong type argument to unary minus")
7322 : _("wrong type argument to unary plus"));
7325 if (!noconvert
&& INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg
)))
7326 arg
= cp_perform_integral_promotions (arg
, complain
);
7328 /* Make sure the result is not an lvalue: a unary plus or minus
7329 expression is always a rvalue. */
7336 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
7341 arg
= cp_default_conversion (arg
, complain
);
7342 if (arg
== error_mark_node
)
7343 return error_mark_node
;
7346 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
7347 | WANT_VECTOR_OR_COMPLEX
,
7349 errstring
= _("wrong type argument to bit-complement");
7350 else if (!noconvert
&& INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg
)))
7352 /* Warn if the expression has boolean value. */
7353 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
7354 && (complain
& tf_warning
)
7355 && warning_at (location
, OPT_Wbool_operation
,
7356 "%<~%> on an expression of type %<bool%>"))
7357 inform (location
, "did you mean to use logical not (%<!%>)?");
7358 arg
= cp_perform_integral_promotions (arg
, complain
);
7360 else if (!noconvert
&& VECTOR_TYPE_P (TREE_TYPE (arg
)))
7361 arg
= mark_rvalue_use (arg
);
7365 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
7366 errstring
= _("wrong type argument to abs");
7367 else if (!noconvert
)
7369 arg
= cp_default_conversion (arg
, complain
);
7370 if (arg
== error_mark_node
)
7371 return error_mark_node
;
7376 /* Conjugating a real value is a no-op, but allow it anyway. */
7377 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
7378 errstring
= _("wrong type argument to conjugation");
7379 else if (!noconvert
)
7381 arg
= cp_default_conversion (arg
, complain
);
7382 if (arg
== error_mark_node
)
7383 return error_mark_node
;
7387 case TRUTH_NOT_EXPR
:
7388 if (gnu_vector_type_p (TREE_TYPE (arg
)))
7389 return cp_build_binary_op (input_location
, EQ_EXPR
, arg
,
7390 build_zero_cst (TREE_TYPE (arg
)), complain
);
7391 arg
= perform_implicit_conversion (boolean_type_node
, arg
,
7393 if (arg
!= error_mark_node
)
7395 val
= invert_truthvalue_loc (location
, arg
);
7396 if (obvalue_p (val
))
7397 val
= non_lvalue_loc (location
, val
);
7400 errstring
= _("in argument to unary !");
7408 val
= build_real_imag_expr (input_location
, code
, arg
);
7409 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_EXPR
)
7410 val
= build1_loc (input_location
, EXCESS_PRECISION_EXPR
,
7411 TREE_TYPE (eptype
), val
);
7414 case PREINCREMENT_EXPR
:
7415 case POSTINCREMENT_EXPR
:
7416 case PREDECREMENT_EXPR
:
7417 case POSTDECREMENT_EXPR
:
7418 /* Handle complex lvalues (when permitted)
7419 by reduction to simpler cases. */
7421 val
= unary_complex_lvalue (code
, arg
);
7423 goto return_build_unary_op
;
7425 arg
= mark_lvalue_use (arg
);
7427 /* Increment or decrement the real part of the value,
7428 and don't change the imaginary part. */
7429 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
7433 arg
= cp_stabilize_reference (arg
);
7434 real
= cp_build_unary_op (REALPART_EXPR
, arg
, true, complain
);
7435 imag
= cp_build_unary_op (IMAGPART_EXPR
, arg
, true, complain
);
7436 real
= cp_build_unary_op (code
, real
, true, complain
);
7437 if (real
== error_mark_node
|| imag
== error_mark_node
)
7438 return error_mark_node
;
7439 val
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
), real
, imag
);
7440 goto return_build_unary_op
;
7443 /* Report invalid types. */
7445 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
7448 if (code
== PREINCREMENT_EXPR
)
7449 errstring
= _("no pre-increment operator for type");
7450 else if (code
== POSTINCREMENT_EXPR
)
7451 errstring
= _("no post-increment operator for type");
7452 else if (code
== PREDECREMENT_EXPR
)
7453 errstring
= _("no pre-decrement operator for type");
7455 errstring
= _("no post-decrement operator for type");
7458 else if (arg
== error_mark_node
)
7459 return error_mark_node
;
7461 /* Report something read-only. */
7463 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
7464 || TREE_READONLY (arg
))
7466 if (complain
& tf_error
)
7467 cxx_readonly_error (location
, arg
,
7468 ((code
== PREINCREMENT_EXPR
7469 || code
== POSTINCREMENT_EXPR
)
7470 ? lv_increment
: lv_decrement
));
7472 return error_mark_node
;
7477 tree declared_type
= unlowered_expr_type (arg
);
7479 argtype
= TREE_TYPE (arg
);
7481 /* ARM $5.2.5 last annotation says this should be forbidden. */
7482 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
7484 if (complain
& tf_error
)
7485 permerror (location
, (code
== PREINCREMENT_EXPR
7486 || code
== POSTINCREMENT_EXPR
)
7487 ? G_("ISO C++ forbids incrementing an enum")
7488 : G_("ISO C++ forbids decrementing an enum"));
7490 return error_mark_node
;
7493 /* Compute the increment. */
7495 if (TYPE_PTR_P (argtype
))
7497 tree type
= complete_type (TREE_TYPE (argtype
));
7499 if (!COMPLETE_OR_VOID_TYPE_P (type
))
7501 if (complain
& tf_error
)
7502 error_at (location
, ((code
== PREINCREMENT_EXPR
7503 || code
== POSTINCREMENT_EXPR
))
7504 ? G_("cannot increment a pointer to incomplete "
7506 : G_("cannot decrement a pointer to incomplete "
7508 TREE_TYPE (argtype
));
7510 return error_mark_node
;
7512 else if (!TYPE_PTROB_P (argtype
))
7514 if (complain
& tf_error
)
7515 pedwarn (location
, OPT_Wpointer_arith
,
7516 (code
== PREINCREMENT_EXPR
7517 || code
== POSTINCREMENT_EXPR
)
7518 ? G_("ISO C++ forbids incrementing a pointer "
7520 : G_("ISO C++ forbids decrementing a pointer "
7524 return error_mark_node
;
7526 else if (!verify_type_context (location
, TCTX_POINTER_ARITH
,
7527 TREE_TYPE (argtype
),
7528 !(complain
& tf_error
)))
7529 return error_mark_node
;
7531 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
7534 inc
= VECTOR_TYPE_P (argtype
)
7535 ? build_one_cst (argtype
)
7538 inc
= cp_convert (argtype
, inc
, complain
);
7540 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
7541 need to ask Objective-C to build the increment or decrement
7542 expression for it. */
7543 if (objc_is_property_ref (arg
))
7544 return objc_build_incr_expr_for_property_ref (input_location
, code
,
7547 /* Complain about anything else that is not a true lvalue. */
7548 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
7549 || code
== POSTINCREMENT_EXPR
)
7550 ? lv_increment
: lv_decrement
),
7552 return error_mark_node
;
7554 /* [depr.volatile.type] "Postfix ++ and -- expressions and
7555 prefix ++ and -- expressions of volatile-qualified arithmetic
7556 and pointer types are deprecated." */
7557 if (TREE_THIS_VOLATILE (arg
) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg
)))
7558 warning_at (location
, OPT_Wvolatile
,
7559 "%qs expression of %<volatile%>-qualified type is "
7561 ((code
== PREINCREMENT_EXPR
7562 || code
== POSTINCREMENT_EXPR
)
7565 /* Forbid using -- or ++ in C++17 on `bool'. */
7566 if (TREE_CODE (declared_type
) == BOOLEAN_TYPE
)
7568 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
7570 if (complain
& tf_error
)
7572 "use of an operand of type %qT in %<operator--%> "
7573 "is forbidden", boolean_type_node
);
7574 return error_mark_node
;
7578 if (cxx_dialect
>= cxx17
)
7580 if (complain
& tf_error
)
7582 "use of an operand of type %qT in "
7583 "%<operator++%> is forbidden in C++17",
7585 return error_mark_node
;
7587 /* Otherwise, [depr.incr.bool] says this is deprecated. */
7589 warning_at (location
, OPT_Wdeprecated
,
7590 "use of an operand of type %qT "
7591 "in %<operator++%> is deprecated",
7594 val
= boolean_increment (code
, arg
);
7596 else if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
7597 /* An rvalue has no cv-qualifiers. */
7598 val
= build2 (code
, cv_unqualified (TREE_TYPE (arg
)), arg
, inc
);
7600 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
7602 TREE_SIDE_EFFECTS (val
) = 1;
7603 goto return_build_unary_op
;
7607 /* Note that this operation never does default_conversion
7608 regardless of NOCONVERT. */
7609 return cp_build_addr_expr (arg
, complain
);
7618 argtype
= TREE_TYPE (arg
);
7619 val
= build1 (code
, argtype
, arg
);
7620 return_build_unary_op
:
7622 val
= build1 (EXCESS_PRECISION_EXPR
, eptype
, val
);
7626 if (complain
& tf_error
)
7627 error_at (location
, "%s", errstring
);
7628 return error_mark_node
;
7631 /* Hook for the c-common bits that build a unary op. */
7633 build_unary_op (location_t
/*location*/,
7634 enum tree_code code
, tree xarg
, bool noconvert
)
7636 return cp_build_unary_op (code
, xarg
, noconvert
, tf_warning_or_error
);
7639 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
7640 so that it is a valid lvalue even for GENERIC by replacing
7641 (lhs = rhs) with ((lhs = rhs), lhs)
7642 (--lhs) with ((--lhs), lhs)
7643 (++lhs) with ((++lhs), lhs)
7644 and if lhs has side-effects, calling cp_stabilize_reference on it, so
7645 that it can be evaluated multiple times. */
7648 genericize_compound_lvalue (tree lvalue
)
7650 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue
, 0)))
7651 lvalue
= build2 (TREE_CODE (lvalue
), TREE_TYPE (lvalue
),
7652 cp_stabilize_reference (TREE_OPERAND (lvalue
, 0)),
7653 TREE_OPERAND (lvalue
, 1));
7654 return build2 (COMPOUND_EXPR
, TREE_TYPE (TREE_OPERAND (lvalue
, 0)),
7655 lvalue
, TREE_OPERAND (lvalue
, 0));
7658 /* Apply unary lvalue-demanding operator CODE to the expression ARG
7659 for certain kinds of expressions which are not really lvalues
7660 but which we can accept as lvalues.
7662 If ARG is not a kind of expression we can handle, return
7666 unary_complex_lvalue (enum tree_code code
, tree arg
)
7668 /* Inside a template, making these kinds of adjustments is
7669 pointless; we are only concerned with the type of the
7671 if (processing_template_decl
)
7674 /* Handle (a, b) used as an "lvalue". */
7675 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
7677 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 1), false,
7678 tf_warning_or_error
);
7679 return build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
7680 TREE_OPERAND (arg
, 0), real_result
);
7683 /* Handle (a ? b : c) used as an "lvalue". */
7684 if (TREE_CODE (arg
) == COND_EXPR
7685 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
7686 return rationalize_conditional_expr (code
, arg
, tf_warning_or_error
);
7688 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
7689 if (TREE_CODE (arg
) == MODIFY_EXPR
7690 || TREE_CODE (arg
) == PREINCREMENT_EXPR
7691 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
7692 return unary_complex_lvalue (code
, genericize_compound_lvalue (arg
));
7694 if (code
!= ADDR_EXPR
)
7697 /* Handle (a = b) used as an "lvalue" for `&'. */
7698 if (TREE_CODE (arg
) == MODIFY_EXPR
7699 || TREE_CODE (arg
) == INIT_EXPR
)
7701 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 0), false,
7702 tf_warning_or_error
);
7703 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
7705 suppress_warning (arg
/* What warning? */);
7709 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg
))
7710 || TREE_CODE (arg
) == OFFSET_REF
)
7713 /* We permit compiler to make function calls returning
7714 objects of aggregate type look like lvalues. */
7718 if (TREE_CODE (targ
) == SAVE_EXPR
)
7719 targ
= TREE_OPERAND (targ
, 0);
7721 if (TREE_CODE (targ
) == CALL_EXPR
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (targ
)))
7723 if (TREE_CODE (arg
) == SAVE_EXPR
)
7726 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, tf_warning_or_error
);
7727 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
7730 if (TREE_CODE (arg
) == SAVE_EXPR
&& INDIRECT_REF_P (targ
))
7731 return build3 (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
7732 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
7735 /* Don't let anything else be handled specially. */
7739 /* Mark EXP saying that we need to be able to take the
7740 address of it; it should not be allocated in a register.
7741 Value is true if successful. ARRAY_REF_P is true if this
7742 is for ARRAY_REF construction - in that case we don't want
7743 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
7744 it is fine to use ARRAY_REFs for vector subscripts on vector
7747 C++: we do not allow `current_class_ptr' to be addressable. */
7750 cxx_mark_addressable (tree exp
, bool array_ref_p
)
7755 switch (TREE_CODE (x
))
7757 case VIEW_CONVERT_EXPR
:
7759 && TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
7760 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x
, 0))))
7762 x
= TREE_OPERAND (x
, 0);
7767 error ("attempt to take address of bit-field");
7773 x
= TREE_OPERAND (x
, 0);
7777 if (x
== current_class_ptr
)
7779 error ("cannot take the address of %<this%>, which is an rvalue expression");
7780 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later. */
7786 /* Caller should not be trying to mark initialized
7787 constant fields addressable. */
7788 gcc_assert (DECL_LANG_SPECIFIC (x
) == 0
7789 || DECL_IN_AGGR_P (x
) == 0
7791 || DECL_EXTERNAL (x
));
7795 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
7796 && !DECL_ARTIFICIAL (x
))
7798 if (VAR_P (x
) && DECL_HARD_REGISTER (x
))
7801 ("address of explicit register variable %qD requested", x
);
7804 else if (extra_warnings
)
7806 (OPT_Wextra
, "address requested for %qD, which is declared %<register%>", x
);
7808 TREE_ADDRESSABLE (x
) = 1;
7813 TREE_ADDRESSABLE (x
) = 1;
7817 TREE_ADDRESSABLE (x
) = 1;
7821 TREE_ADDRESSABLE (x
) = 1;
7822 cxx_mark_addressable (TREE_OPERAND (x
, 0));
7830 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
7833 build_x_conditional_expr (location_t loc
, tree ifexp
, tree op1
, tree op2
,
7834 tsubst_flags_t complain
)
7836 tree orig_ifexp
= ifexp
;
7837 tree orig_op1
= op1
;
7838 tree orig_op2
= op2
;
7841 if (processing_template_decl
)
7843 /* The standard says that the expression is type-dependent if
7844 IFEXP is type-dependent, even though the eventual type of the
7845 expression doesn't dependent on IFEXP. */
7846 if (type_dependent_expression_p (ifexp
)
7847 /* As a GNU extension, the middle operand may be omitted. */
7848 || (op1
&& type_dependent_expression_p (op1
))
7849 || type_dependent_expression_p (op2
))
7850 return build_min_nt_loc (loc
, COND_EXPR
, ifexp
, op1
, op2
);
7851 ifexp
= build_non_dependent_expr (ifexp
);
7853 op1
= build_non_dependent_expr (op1
);
7854 op2
= build_non_dependent_expr (op2
);
7857 expr
= build_conditional_expr (loc
, ifexp
, op1
, op2
, complain
);
7858 if (processing_template_decl
&& expr
!= error_mark_node
)
7860 tree min
= build_min_non_dep (COND_EXPR
, expr
,
7861 orig_ifexp
, orig_op1
, orig_op2
);
7862 expr
= convert_from_reference (min
);
7867 /* Given a list of expressions, return a compound expression
7868 that performs them all and returns the value of the last of them. */
7871 build_x_compound_expr_from_list (tree list
, expr_list_kind exp
,
7872 tsubst_flags_t complain
)
7874 tree expr
= TREE_VALUE (list
);
7876 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
7877 && !CONSTRUCTOR_IS_DIRECT_INIT (expr
))
7879 if (complain
& tf_error
)
7880 pedwarn (cp_expr_loc_or_input_loc (expr
), 0,
7881 "list-initializer for non-class type must not "
7882 "be parenthesized");
7884 return error_mark_node
;
7887 if (TREE_CHAIN (list
))
7889 if (complain
& tf_error
)
7893 permerror (input_location
, "expression list treated as compound "
7894 "expression in initializer");
7897 permerror (input_location
, "expression list treated as compound "
7898 "expression in mem-initializer");
7901 permerror (input_location
, "expression list treated as compound "
7902 "expression in functional cast");
7908 return error_mark_node
;
7910 for (list
= TREE_CHAIN (list
); list
; list
= TREE_CHAIN (list
))
7911 expr
= build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list
)),
7912 expr
, TREE_VALUE (list
), NULL_TREE
,
7919 /* Like build_x_compound_expr_from_list, but using a VEC. */
7922 build_x_compound_expr_from_vec (vec
<tree
, va_gc
> *vec
, const char *msg
,
7923 tsubst_flags_t complain
)
7925 if (vec_safe_is_empty (vec
))
7927 else if (vec
->length () == 1)
7937 if (complain
& tf_error
)
7938 permerror (input_location
,
7939 "%s expression list treated as compound expression",
7942 return error_mark_node
;
7946 for (ix
= 1; vec
->iterate (ix
, &t
); ++ix
)
7947 expr
= build_x_compound_expr (EXPR_LOCATION (t
), expr
,
7948 t
, NULL_TREE
, complain
);
7954 /* Handle overloading of the ',' operator when needed. */
7957 build_x_compound_expr (location_t loc
, tree op1
, tree op2
,
7958 tree lookups
, tsubst_flags_t complain
)
7961 tree orig_op1
= op1
;
7962 tree orig_op2
= op2
;
7963 tree overload
= NULL_TREE
;
7965 if (processing_template_decl
)
7967 if (type_dependent_expression_p (op1
)
7968 || type_dependent_expression_p (op2
))
7970 result
= build_min_nt_loc (loc
, COMPOUND_EXPR
, op1
, op2
);
7972 = build_dependent_operator_type (lookups
, COMPOUND_EXPR
, false);
7975 op1
= build_non_dependent_expr (op1
);
7976 op2
= build_non_dependent_expr (op2
);
7979 result
= build_new_op (loc
, COMPOUND_EXPR
, LOOKUP_NORMAL
, op1
, op2
,
7980 NULL_TREE
, lookups
, &overload
, complain
);
7982 result
= cp_build_compound_expr (op1
, op2
, complain
);
7984 if (processing_template_decl
&& result
!= error_mark_node
)
7986 if (overload
!= NULL_TREE
)
7987 return (build_min_non_dep_op_overload
7988 (COMPOUND_EXPR
, result
, overload
, orig_op1
, orig_op2
));
7990 return build_min_non_dep (COMPOUND_EXPR
, result
, orig_op1
, orig_op2
);
7996 /* Like cp_build_compound_expr, but for the c-common bits. */
7999 build_compound_expr (location_t
/*loc*/, tree lhs
, tree rhs
)
8001 return cp_build_compound_expr (lhs
, rhs
, tf_warning_or_error
);
8004 /* Build a compound expression. */
8007 cp_build_compound_expr (tree lhs
, tree rhs
, tsubst_flags_t complain
)
8009 lhs
= convert_to_void (lhs
, ICV_LEFT_OF_COMMA
, complain
);
8011 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
8012 return error_mark_node
;
8014 if (TREE_CODE (lhs
) == EXCESS_PRECISION_EXPR
)
8015 lhs
= TREE_OPERAND (lhs
, 0);
8016 tree eptype
= NULL_TREE
;
8017 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
8019 eptype
= TREE_TYPE (rhs
);
8020 rhs
= TREE_OPERAND (rhs
, 0);
8023 if (TREE_CODE (rhs
) == TARGET_EXPR
)
8025 /* If the rhs is a TARGET_EXPR, then build the compound
8026 expression inside the target_expr's initializer. This
8027 helps the compiler to eliminate unnecessary temporaries. */
8028 tree init
= TREE_OPERAND (rhs
, 1);
8030 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), lhs
, init
);
8031 TREE_OPERAND (rhs
, 1) = init
;
8034 rhs
= build1 (EXCESS_PRECISION_EXPR
, eptype
, rhs
);
8038 if (type_unknown_p (rhs
))
8040 if (complain
& tf_error
)
8041 error_at (cp_expr_loc_or_input_loc (rhs
),
8042 "no context to resolve type of %qE", rhs
);
8043 return error_mark_node
;
8046 tree ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (rhs
), lhs
, rhs
);
8048 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
8052 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
8053 casts away constness. CAST gives the type of cast. Returns true
8054 if the cast is ill-formed, false if it is well-formed.
8056 ??? This function warns for casting away any qualifier not just
8057 const. We would like to specify exactly what qualifiers are casted
8062 check_for_casting_away_constness (location_t loc
, tree src_type
,
8063 tree dest_type
, enum tree_code cast
,
8064 tsubst_flags_t complain
)
8066 /* C-style casts are allowed to cast away constness. With
8067 WARN_CAST_QUAL, we still want to issue a warning. */
8068 if (cast
== CAST_EXPR
&& !warn_cast_qual
)
8071 if (!casts_away_constness (src_type
, dest_type
, complain
))
8077 if (complain
& tf_warning
)
8078 warning_at (loc
, OPT_Wcast_qual
,
8079 "cast from type %qT to type %qT casts away qualifiers",
8080 src_type
, dest_type
);
8083 case STATIC_CAST_EXPR
:
8084 if (complain
& tf_error
)
8085 error_at (loc
, "%<static_cast%> from type %qT to type %qT casts "
8087 src_type
, dest_type
);
8090 case REINTERPRET_CAST_EXPR
:
8091 if (complain
& tf_error
)
8092 error_at (loc
, "%<reinterpret_cast%> from type %qT to type %qT "
8093 "casts away qualifiers",
8094 src_type
, dest_type
);
8102 /* Warns if the cast from expression EXPR to type TYPE is useless. */
8104 maybe_warn_about_useless_cast (location_t loc
, tree type
, tree expr
,
8105 tsubst_flags_t complain
)
8107 if (warn_useless_cast
8108 && complain
& tf_warning
)
8110 if (TYPE_REF_P (type
)
8111 ? ((TYPE_REF_IS_RVALUE (type
)
8112 ? xvalue_p (expr
) : lvalue_p (expr
))
8113 && same_type_p (TREE_TYPE (expr
), TREE_TYPE (type
)))
8114 /* Don't warn when converting a class object to a non-reference type,
8115 because that's a common way to create a temporary. */
8116 : (!glvalue_p (expr
) && same_type_p (TREE_TYPE (expr
), type
)))
8117 warning_at (loc
, OPT_Wuseless_cast
,
8118 "useless cast to type %q#T", type
);
8122 /* Warns if the cast ignores cv-qualifiers on TYPE. */
8124 maybe_warn_about_cast_ignoring_quals (location_t loc
, tree type
,
8125 tsubst_flags_t complain
)
8127 if (warn_ignored_qualifiers
8128 && complain
& tf_warning
8129 && !CLASS_TYPE_P (type
)
8130 && (cp_type_quals (type
) & (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)))
8131 warning_at (loc
, OPT_Wignored_qualifiers
,
8132 "type qualifiers ignored on cast result type");
8135 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
8136 (another pointer-to-member type in the same hierarchy) and return
8137 the converted expression. If ALLOW_INVERSE_P is permitted, a
8138 pointer-to-derived may be converted to pointer-to-base; otherwise,
8139 only the other direction is permitted. If C_CAST_P is true, this
8140 conversion is taking place as part of a C-style cast. */
8143 convert_ptrmem (tree type
, tree expr
, bool allow_inverse_p
,
8144 bool c_cast_p
, tsubst_flags_t complain
)
8146 if (same_type_p (type
, TREE_TYPE (expr
)))
8149 if (TYPE_PTRDATAMEM_P (type
))
8151 tree obase
= TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr
));
8152 tree nbase
= TYPE_PTRMEM_CLASS_TYPE (type
);
8153 tree delta
= (get_delta_difference
8155 allow_inverse_p
, c_cast_p
, complain
));
8157 if (delta
== error_mark_node
)
8158 return error_mark_node
;
8160 if (!same_type_p (obase
, nbase
))
8162 if (TREE_CODE (expr
) == PTRMEM_CST
)
8163 expr
= cplus_expand_constant (expr
);
8165 tree cond
= cp_build_binary_op (input_location
, EQ_EXPR
, expr
,
8166 build_int_cst (TREE_TYPE (expr
), -1),
8168 tree op1
= build_nop (ptrdiff_type_node
, expr
);
8169 tree op2
= cp_build_binary_op (input_location
, PLUS_EXPR
, op1
, delta
,
8172 expr
= fold_build3_loc (input_location
,
8173 COND_EXPR
, ptrdiff_type_node
, cond
, op1
, op2
);
8176 return build_nop (type
, expr
);
8179 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
,
8180 allow_inverse_p
, c_cast_p
, complain
);
8183 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
8184 this static_cast is being attempted as one of the possible casts
8185 allowed by a C-style cast. (In that case, accessibility of base
8186 classes is not considered, and it is OK to cast away
8187 constness.) Return the result of the cast. *VALID_P is set to
8188 indicate whether or not the cast was valid. */
8191 build_static_cast_1 (location_t loc
, tree type
, tree expr
, bool c_cast_p
,
8192 bool *valid_p
, tsubst_flags_t complain
)
8198 /* Assume the cast is valid. */
8201 intype
= unlowered_expr_type (expr
);
8203 /* Save casted types in the function's used types hash table. */
8204 used_types_insert (type
);
8206 /* A prvalue of non-class type is cv-unqualified. */
8207 if (!CLASS_TYPE_P (type
))
8208 type
= cv_unqualified (type
);
8210 /* [expr.static.cast]
8212 An lvalue of type "cv1 B", where B is a class type, can be cast
8213 to type "reference to cv2 D", where D is a class derived (clause
8214 _class.derived_) from B, if a valid standard conversion from
8215 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
8216 same cv-qualification as, or greater cv-qualification than, cv1,
8217 and B is not a virtual base class of D. */
8218 /* We check this case before checking the validity of "TYPE t =
8219 EXPR;" below because for this case:
8222 struct D : public B { D(const B&); };
8224 void f() { static_cast<const D&>(b); }
8226 we want to avoid constructing a new D. The standard is not
8227 completely clear about this issue, but our interpretation is
8228 consistent with other compilers. */
8229 if (TYPE_REF_P (type
)
8230 && CLASS_TYPE_P (TREE_TYPE (type
))
8231 && CLASS_TYPE_P (intype
)
8232 && (TYPE_REF_IS_RVALUE (type
) || lvalue_p (expr
))
8233 && DERIVED_FROM_P (intype
, TREE_TYPE (type
))
8234 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype
)),
8235 build_pointer_type (TYPE_MAIN_VARIANT
8236 (TREE_TYPE (type
))),
8239 || at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
8243 if (processing_template_decl
)
8246 /* There is a standard conversion from "D*" to "B*" even if "B"
8247 is ambiguous or inaccessible. If this is really a
8248 static_cast, then we check both for inaccessibility and
8249 ambiguity. However, if this is a static_cast being performed
8250 because the user wrote a C-style cast, then accessibility is
8252 base
= lookup_base (TREE_TYPE (type
), intype
,
8253 c_cast_p
? ba_unique
: ba_check
,
8255 expr
= cp_build_addr_expr (expr
, complain
);
8257 if (sanitize_flags_p (SANITIZE_VPTR
))
8260 = cp_ubsan_maybe_instrument_downcast (loc
, type
,
8266 /* Convert from "B*" to "D*". This function will check that "B"
8267 is not a virtual base of "D". Even if we don't have a guarantee
8268 that expr is NULL, if the static_cast is to a reference type,
8269 it is UB if it would be NULL, so omit the non-NULL check. */
8270 expr
= build_base_path (MINUS_EXPR
, expr
, base
,
8271 /*nonnull=*/flag_delete_null_pointer_checks
,
8274 /* Convert the pointer to a reference -- but then remember that
8275 there are no expressions with reference type in C++.
8277 We call rvalue so that there's an actual tree code
8278 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
8279 is a variable with the same type, the conversion would get folded
8280 away, leaving just the variable and causing lvalue_kind to give
8281 the wrong answer. */
8282 expr
= cp_fold_convert (type
, expr
);
8284 /* When -fsanitize=null, make sure to diagnose reference binding to
8285 NULL even when the reference is converted to pointer later on. */
8286 if (sanitize_flags_p (SANITIZE_NULL
)
8287 && TREE_CODE (expr
) == COND_EXPR
8288 && TREE_OPERAND (expr
, 2)
8289 && TREE_CODE (TREE_OPERAND (expr
, 2)) == INTEGER_CST
8290 && TREE_TYPE (TREE_OPERAND (expr
, 2)) == type
)
8291 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr
, 2));
8293 return convert_from_reference (rvalue (expr
));
8296 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
8297 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
8298 if (TYPE_REF_P (type
)
8299 && TYPE_REF_IS_RVALUE (type
)
8300 && (clk
= real_lvalue_p (expr
))
8301 && reference_compatible_p (TREE_TYPE (type
), intype
)
8302 && (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
8304 if (processing_template_decl
)
8306 if (clk
== clk_ordinary
)
8308 /* Handle the (non-bit-field) lvalue case here by casting to
8309 lvalue reference and then changing it to an rvalue reference.
8310 Casting an xvalue to rvalue reference will be handled by the
8312 tree lref
= cp_build_reference_type (TREE_TYPE (type
), false);
8313 result
= (perform_direct_initialization_if_possible
8314 (lref
, expr
, c_cast_p
, complain
));
8315 result
= build1 (NON_LVALUE_EXPR
, type
, result
);
8316 return convert_from_reference (result
);
8319 /* For a bit-field or packed field, bind to a temporary. */
8320 expr
= rvalue (expr
);
8323 /* Resolve overloaded address here rather than once in
8324 implicit_conversion and again in the inverse code below. */
8325 if (TYPE_PTRMEMFUNC_P (type
) && type_unknown_p (expr
))
8327 expr
= instantiate_type (type
, expr
, complain
);
8328 intype
= TREE_TYPE (expr
);
8331 /* [expr.static.cast]
8333 Any expression can be explicitly converted to type cv void. */
8334 if (VOID_TYPE_P (type
))
8336 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
8337 expr
= TREE_OPERAND (expr
, 0);
8338 return convert_to_void (expr
, ICV_CAST
, complain
);
8342 An abstract class shall not be used ... as the type of an explicit
8344 if (abstract_virtuals_error (ACU_CAST
, type
, complain
))
8345 return error_mark_node
;
8347 /* [expr.static.cast]
8349 An expression e can be explicitly converted to a type T using a
8350 static_cast of the form static_cast<T>(e) if the declaration T
8351 t(e);" is well-formed, for some invented temporary variable
8353 result
= perform_direct_initialization_if_possible (type
, expr
,
8354 c_cast_p
, complain
);
8355 /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
8356 which initialize the first element of the aggregate. We need to handle
8357 the array case specifically. */
8358 if (result
== NULL_TREE
8359 && cxx_dialect
>= cxx20
8360 && TREE_CODE (type
) == ARRAY_TYPE
)
8362 /* Create { EXPR } and perform direct-initialization from it. */
8363 tree e
= build_constructor_single (init_list_type_node
, NULL_TREE
, expr
);
8364 CONSTRUCTOR_IS_DIRECT_INIT (e
) = true;
8365 CONSTRUCTOR_IS_PAREN_INIT (e
) = true;
8366 result
= perform_direct_initialization_if_possible (type
, e
, c_cast_p
,
8371 if (processing_template_decl
)
8374 result
= convert_from_reference (result
);
8376 /* [expr.static.cast]
8378 If T is a reference type, the result is an lvalue; otherwise,
8379 the result is an rvalue. */
8380 if (!TYPE_REF_P (type
))
8382 result
= rvalue (result
);
8384 if (result
== expr
&& SCALAR_TYPE_P (type
))
8385 /* Leave some record of the cast. */
8386 result
= build_nop (type
, expr
);
8391 /* [expr.static.cast]
8393 The inverse of any standard conversion sequence (clause _conv_),
8394 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
8395 (_conv.array_), function-to-pointer (_conv.func_), and boolean
8396 (_conv.bool_) conversions, can be performed explicitly using
8397 static_cast subject to the restriction that the explicit
8398 conversion does not cast away constness (_expr.const.cast_), and
8399 the following additional rules for specific cases: */
8400 /* For reference, the conversions not excluded are: integral
8401 promotions, floating-point promotion, integral conversions,
8402 floating-point conversions, floating-integral conversions,
8403 pointer conversions, and pointer to member conversions. */
8406 A value of integral _or enumeration_ type can be explicitly
8407 converted to an enumeration type. */
8408 /* The effect of all that is that any conversion between any two
8409 types which are integral, floating, or enumeration types can be
8411 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
8412 || SCALAR_FLOAT_TYPE_P (type
))
8413 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
8414 || SCALAR_FLOAT_TYPE_P (intype
)))
8416 if (processing_template_decl
)
8418 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
8419 expr
= TREE_OPERAND (expr
, 0);
8420 return ocp_convert (type
, expr
, CONV_C_CAST
, LOOKUP_NORMAL
, complain
);
8423 if (TYPE_PTR_P (type
) && TYPE_PTR_P (intype
)
8424 && CLASS_TYPE_P (TREE_TYPE (type
))
8425 && CLASS_TYPE_P (TREE_TYPE (intype
))
8426 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
8427 (TREE_TYPE (intype
))),
8428 build_pointer_type (TYPE_MAIN_VARIANT
8429 (TREE_TYPE (type
))),
8434 if (processing_template_decl
)
8438 && check_for_casting_away_constness (loc
, intype
, type
,
8441 return error_mark_node
;
8442 base
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
8443 c_cast_p
? ba_unique
: ba_check
,
8445 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
8448 if (sanitize_flags_p (SANITIZE_VPTR
))
8451 = cp_ubsan_maybe_instrument_downcast (loc
, type
,
8457 return cp_fold_convert (type
, expr
);
8460 if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
8461 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
8468 c1
= TYPE_PTRMEM_CLASS_TYPE (intype
);
8469 c2
= TYPE_PTRMEM_CLASS_TYPE (type
);
8471 if (TYPE_PTRDATAMEM_P (type
))
8473 t1
= (build_ptrmem_type
8475 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype
))));
8476 t2
= (build_ptrmem_type
8478 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
8485 if (can_convert (t1
, t2
, complain
) || can_convert (t2
, t1
, complain
))
8488 && check_for_casting_away_constness (loc
, intype
, type
,
8491 return error_mark_node
;
8492 if (processing_template_decl
)
8494 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/1,
8495 c_cast_p
, complain
);
8499 /* [expr.static.cast]
8501 An rvalue of type "pointer to cv void" can be explicitly
8502 converted to a pointer to object type. A value of type pointer
8503 to object converted to "pointer to cv void" and back to the
8504 original pointer type will have its original value. */
8505 if (TYPE_PTR_P (intype
)
8506 && VOID_TYPE_P (TREE_TYPE (intype
))
8507 && TYPE_PTROB_P (type
))
8510 && check_for_casting_away_constness (loc
, intype
, type
,
8513 return error_mark_node
;
8514 if (processing_template_decl
)
8516 return build_nop (type
, expr
);
8520 return error_mark_node
;
8523 /* Return an expression representing static_cast<TYPE>(EXPR). */
8526 build_static_cast (location_t loc
, tree type
, tree oexpr
,
8527 tsubst_flags_t complain
)
8533 if (type
== error_mark_node
|| expr
== error_mark_node
)
8534 return error_mark_node
;
8536 bool dependent
= (dependent_type_p (type
)
8537 || type_dependent_expression_p (expr
));
8541 expr
= build_min (STATIC_CAST_EXPR
, type
, oexpr
);
8542 /* We don't know if it will or will not have side effects. */
8543 TREE_SIDE_EFFECTS (expr
) = 1;
8544 result
= convert_from_reference (expr
);
8545 protected_set_expr_location (result
, loc
);
8548 else if (processing_template_decl
)
8549 expr
= build_non_dependent_expr (expr
);
8551 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8552 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8553 if (!TYPE_REF_P (type
)
8554 && TREE_CODE (expr
) == NOP_EXPR
8555 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
8556 expr
= TREE_OPERAND (expr
, 0);
8558 result
= build_static_cast_1 (loc
, type
, expr
, /*c_cast_p=*/false,
8559 &valid_p
, complain
);
8562 if (result
!= error_mark_node
)
8564 maybe_warn_about_useless_cast (loc
, type
, expr
, complain
);
8565 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
8567 if (processing_template_decl
)
8569 protected_set_expr_location (result
, loc
);
8573 if (complain
& tf_error
)
8575 error_at (loc
, "invalid %<static_cast%> from type %qT to type %qT",
8576 TREE_TYPE (expr
), type
);
8577 if ((TYPE_PTR_P (type
) || TYPE_REF_P (type
))
8578 && CLASS_TYPE_P (TREE_TYPE (type
))
8579 && !COMPLETE_TYPE_P (TREE_TYPE (type
)))
8580 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type
))),
8581 "class type %qT is incomplete", TREE_TYPE (type
));
8582 tree expr_type
= TREE_TYPE (expr
);
8583 if (TYPE_PTR_P (expr_type
))
8584 expr_type
= TREE_TYPE (expr_type
);
8585 if (CLASS_TYPE_P (expr_type
) && !COMPLETE_TYPE_P (expr_type
))
8586 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type
)),
8587 "class type %qT is incomplete", expr_type
);
8589 return error_mark_node
;
8592 /* EXPR is an expression with member function or pointer-to-member
8593 function type. TYPE is a pointer type. Converting EXPR to TYPE is
8594 not permitted by ISO C++, but we accept it in some modes. If we
8595 are not in one of those modes, issue a diagnostic. Return the
8596 converted expression. */
8599 convert_member_func_to_ptr (tree type
, tree expr
, tsubst_flags_t complain
)
8604 intype
= TREE_TYPE (expr
);
8605 gcc_assert (TYPE_PTRMEMFUNC_P (intype
)
8606 || TREE_CODE (intype
) == METHOD_TYPE
);
8608 if (!(complain
& tf_warning_or_error
))
8609 return error_mark_node
;
8611 location_t loc
= cp_expr_loc_or_input_loc (expr
);
8613 if (pedantic
|| warn_pmf2ptr
)
8614 pedwarn (loc
, pedantic
? OPT_Wpedantic
: OPT_Wpmf_conversions
,
8615 "converting from %qH to %qI", intype
, type
);
8617 STRIP_ANY_LOCATION_WRAPPER (expr
);
8619 if (TREE_CODE (intype
) == METHOD_TYPE
)
8620 expr
= build_addr_func (expr
, complain
);
8621 else if (TREE_CODE (expr
) == PTRMEM_CST
)
8622 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
8625 decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
), 0);
8626 decl
= build_address (decl
);
8627 expr
= get_member_function_from_ptrfunc (&decl
, expr
, complain
);
8630 if (expr
== error_mark_node
)
8631 return error_mark_node
;
8633 expr
= build_nop (type
, expr
);
8634 SET_EXPR_LOCATION (expr
, loc
);
8638 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
8639 constexpr evaluation knows to reject it. */
8642 build_nop_reinterpret (tree type
, tree expr
)
8644 tree ret
= build_nop (type
, expr
);
8646 REINTERPRET_CAST_P (ret
) = true;
8650 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
8651 If C_CAST_P is true, this reinterpret cast is being done as part of
8652 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
8653 indicate whether or not reinterpret_cast was valid. */
8656 build_reinterpret_cast_1 (location_t loc
, tree type
, tree expr
,
8657 bool c_cast_p
, bool *valid_p
,
8658 tsubst_flags_t complain
)
8662 /* Assume the cast is invalid. */
8666 if (type
== error_mark_node
|| error_operand_p (expr
))
8667 return error_mark_node
;
8669 intype
= TREE_TYPE (expr
);
8671 /* Save casted types in the function's used types hash table. */
8672 used_types_insert (type
);
8674 /* A prvalue of non-class type is cv-unqualified. */
8675 if (!CLASS_TYPE_P (type
))
8676 type
= cv_unqualified (type
);
8678 /* [expr.reinterpret.cast]
8679 A glvalue of type T1, designating an object x, can be cast to the type
8680 "reference to T2" if an expression of type "pointer to T1" can be
8681 explicitly converted to the type "pointer to T2" using a reinterpret_cast.
8682 The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
8683 of type "pointer to T1". No temporary is created, no copy is made, and no
8684 constructors (11.4.4) or conversion functions (11.4.7) are called. */
8685 if (TYPE_REF_P (type
))
8687 if (!glvalue_p (expr
))
8689 if (complain
& tf_error
)
8690 error_at (loc
, "invalid cast of a prvalue expression of type "
8693 return error_mark_node
;
8696 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
8697 "B" are related class types; the reinterpret_cast does not
8698 adjust the pointer. */
8699 if (TYPE_PTR_P (intype
)
8700 && (complain
& tf_warning
)
8701 && (comptypes (TREE_TYPE (intype
), TREE_TYPE (type
),
8702 COMPARE_BASE
| COMPARE_DERIVED
)))
8703 warning_at (loc
, 0, "casting %qT to %qT does not dereference pointer",
8706 expr
= cp_build_addr_expr (expr
, complain
);
8708 if (warn_strict_aliasing
> 2)
8709 cp_strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
8711 if (expr
!= error_mark_node
)
8712 expr
= build_reinterpret_cast_1
8713 (loc
, build_pointer_type (TREE_TYPE (type
)), expr
, c_cast_p
,
8715 if (expr
!= error_mark_node
)
8716 /* cp_build_indirect_ref isn't right for rvalue refs. */
8717 expr
= convert_from_reference (fold_convert (type
, expr
));
8721 /* As a G++ extension, we consider conversions from member
8722 functions, and pointers to member functions to
8723 pointer-to-function and pointer-to-void types. If
8724 -Wno-pmf-conversions has not been specified,
8725 convert_member_func_to_ptr will issue an error message. */
8726 if ((TYPE_PTRMEMFUNC_P (intype
)
8727 || TREE_CODE (intype
) == METHOD_TYPE
)
8728 && TYPE_PTR_P (type
)
8729 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
8730 || VOID_TYPE_P (TREE_TYPE (type
))))
8731 return convert_member_func_to_ptr (type
, expr
, complain
);
8733 /* If the cast is not to a reference type, the lvalue-to-rvalue,
8734 array-to-pointer, and function-to-pointer conversions are
8736 expr
= decay_conversion (expr
, complain
);
8738 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8739 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8740 if (TREE_CODE (expr
) == NOP_EXPR
8741 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
8742 expr
= TREE_OPERAND (expr
, 0);
8744 if (error_operand_p (expr
))
8745 return error_mark_node
;
8747 intype
= TREE_TYPE (expr
);
8749 /* [expr.reinterpret.cast]
8750 A pointer can be converted to any integral type large enough to
8751 hold it. ... A value of type std::nullptr_t can be converted to
8752 an integral type; the conversion has the same meaning and
8753 validity as a conversion of (void*)0 to the integral type. */
8754 if (CP_INTEGRAL_TYPE_P (type
)
8755 && (TYPE_PTR_P (intype
) || NULLPTR_TYPE_P (intype
)))
8757 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
8759 if (complain
& tf_error
)
8760 permerror (loc
, "cast from %qH to %qI loses precision",
8763 return error_mark_node
;
8765 if (NULLPTR_TYPE_P (intype
))
8766 return build_int_cst (type
, 0);
8768 /* [expr.reinterpret.cast]
8769 A value of integral or enumeration type can be explicitly
8770 converted to a pointer. */
8771 else if (TYPE_PTR_P (type
) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype
))
8774 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
8775 || TYPE_PTR_OR_PTRMEM_P (type
))
8776 && same_type_p (type
, intype
))
8778 return rvalue (expr
);
8779 else if (TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
8781 if ((complain
& tf_warning
)
8782 && !cxx_safe_function_type_cast_p (TREE_TYPE (type
),
8783 TREE_TYPE (intype
)))
8784 warning_at (loc
, OPT_Wcast_function_type
,
8785 "cast between incompatible function types"
8786 " from %qH to %qI", intype
, type
);
8787 return build_nop_reinterpret (type
, expr
);
8789 else if (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
))
8791 if ((complain
& tf_warning
)
8792 && !cxx_safe_function_type_cast_p
8793 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type
)),
8794 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype
))))
8795 warning_at (loc
, OPT_Wcast_function_type
,
8796 "cast between incompatible pointer to member types"
8797 " from %qH to %qI", intype
, type
);
8798 return build_nop_reinterpret (type
, expr
);
8800 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
8801 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
8804 && check_for_casting_away_constness (loc
, intype
, type
,
8805 REINTERPRET_CAST_EXPR
,
8807 return error_mark_node
;
8808 /* Warn about possible alignment problems. */
8809 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
8810 && (complain
& tf_warning
)
8811 && !VOID_TYPE_P (type
)
8812 && TREE_CODE (TREE_TYPE (intype
)) != FUNCTION_TYPE
8813 && COMPLETE_TYPE_P (TREE_TYPE (type
))
8814 && COMPLETE_TYPE_P (TREE_TYPE (intype
))
8815 && min_align_of_type (TREE_TYPE (type
))
8816 > min_align_of_type (TREE_TYPE (intype
)))
8817 warning_at (loc
, OPT_Wcast_align
, "cast from %qH to %qI "
8818 "increases required alignment of target type",
8821 if (warn_strict_aliasing
<= 2)
8822 /* strict_aliasing_warning STRIP_NOPs its expr. */
8823 cp_strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
8825 return build_nop_reinterpret (type
, expr
);
8827 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
8828 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
8830 if (complain
& tf_warning
)
8831 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
8832 object pointer type or vice versa is conditionally-supported." */
8833 warning_at (loc
, OPT_Wconditionally_supported
,
8834 "casting between pointer-to-function and "
8835 "pointer-to-object is conditionally-supported");
8836 return build_nop_reinterpret (type
, expr
);
8838 else if (gnu_vector_type_p (type
) && scalarish_type_p (intype
))
8839 return convert_to_vector (type
, rvalue (expr
));
8840 else if (gnu_vector_type_p (intype
)
8841 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
8842 return convert_to_integer_nofold (type
, expr
);
8847 if (complain
& tf_error
)
8848 error_at (loc
, "invalid cast from type %qT to type %qT",
8850 return error_mark_node
;
8853 expr
= cp_convert (type
, expr
, complain
);
8854 if (TREE_CODE (expr
) == NOP_EXPR
)
8855 /* Mark any nop_expr that created as a reintepret_cast. */
8856 REINTERPRET_CAST_P (expr
) = true;
8861 build_reinterpret_cast (location_t loc
, tree type
, tree expr
,
8862 tsubst_flags_t complain
)
8866 if (type
== error_mark_node
|| expr
== error_mark_node
)
8867 return error_mark_node
;
8869 if (processing_template_decl
)
8871 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
8873 if (!TREE_SIDE_EFFECTS (t
)
8874 && type_dependent_expression_p (expr
))
8875 /* There might turn out to be side effects inside expr. */
8876 TREE_SIDE_EFFECTS (t
) = 1;
8877 r
= convert_from_reference (t
);
8878 protected_set_expr_location (r
, loc
);
8882 r
= build_reinterpret_cast_1 (loc
, type
, expr
, /*c_cast_p=*/false,
8883 /*valid_p=*/NULL
, complain
);
8884 if (r
!= error_mark_node
)
8886 maybe_warn_about_useless_cast (loc
, type
, expr
, complain
);
8887 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
8889 protected_set_expr_location (r
, loc
);
8893 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
8894 return an appropriate expression. Otherwise, return
8895 error_mark_node. If the cast is not valid, and COMPLAIN is true,
8896 then a diagnostic will be issued. If VALID_P is non-NULL, we are
8897 performing a C-style cast, its value upon return will indicate
8898 whether or not the conversion succeeded. */
8901 build_const_cast_1 (location_t loc
, tree dst_type
, tree expr
,
8902 tsubst_flags_t complain
, bool *valid_p
)
8905 tree reference_type
;
8907 /* Callers are responsible for handling error_mark_node as a
8908 destination type. */
8909 gcc_assert (dst_type
!= error_mark_node
);
8910 /* In a template, callers should be building syntactic
8911 representations of casts, not using this machinery. */
8912 gcc_assert (!processing_template_decl
);
8914 /* Assume the conversion is invalid. */
8918 if (!INDIRECT_TYPE_P (dst_type
) && !TYPE_PTRDATAMEM_P (dst_type
))
8920 if (complain
& tf_error
)
8921 error_at (loc
, "invalid use of %<const_cast%> with type %qT, "
8922 "which is not a pointer, reference, "
8923 "nor a pointer-to-data-member type", dst_type
);
8924 return error_mark_node
;
8927 if (TREE_CODE (TREE_TYPE (dst_type
)) == FUNCTION_TYPE
)
8929 if (complain
& tf_error
)
8930 error_at (loc
, "invalid use of %<const_cast%> with type %qT, "
8931 "which is a pointer or reference to a function type",
8933 return error_mark_node
;
8936 /* A prvalue of non-class type is cv-unqualified. */
8937 dst_type
= cv_unqualified (dst_type
);
8939 /* Save casted types in the function's used types hash table. */
8940 used_types_insert (dst_type
);
8942 src_type
= TREE_TYPE (expr
);
8943 /* Expressions do not really have reference types. */
8944 if (TYPE_REF_P (src_type
))
8945 src_type
= TREE_TYPE (src_type
);
8947 /* [expr.const.cast]
8949 For two object types T1 and T2, if a pointer to T1 can be explicitly
8950 converted to the type "pointer to T2" using a const_cast, then the
8951 following conversions can also be made:
8953 -- an lvalue of type T1 can be explicitly converted to an lvalue of
8954 type T2 using the cast const_cast<T2&>;
8956 -- a glvalue of type T1 can be explicitly converted to an xvalue of
8957 type T2 using the cast const_cast<T2&&>; and
8959 -- if T1 is a class type, a prvalue of type T1 can be explicitly
8960 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
8962 if (TYPE_REF_P (dst_type
))
8964 reference_type
= dst_type
;
8965 if (!TYPE_REF_IS_RVALUE (dst_type
)
8971 if (complain
& tf_error
)
8972 error_at (loc
, "invalid %<const_cast%> of an rvalue of type %qT "
8974 src_type
, dst_type
);
8975 return error_mark_node
;
8977 dst_type
= build_pointer_type (TREE_TYPE (dst_type
));
8978 src_type
= build_pointer_type (src_type
);
8982 reference_type
= NULL_TREE
;
8983 /* If the destination type is not a reference type, the
8984 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
8985 conversions are performed. */
8986 src_type
= type_decays_to (src_type
);
8987 if (src_type
== error_mark_node
)
8988 return error_mark_node
;
8991 if (TYPE_PTR_P (src_type
) || TYPE_PTRDATAMEM_P (src_type
))
8993 if (comp_ptr_ttypes_const (dst_type
, src_type
, bounds_none
))
8998 /* This cast is actually a C-style cast. Issue a warning if
8999 the user is making a potentially unsafe cast. */
9000 check_for_casting_away_constness (loc
, src_type
, dst_type
,
9001 CAST_EXPR
, complain
);
9002 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
9003 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
9004 && (complain
& tf_warning
)
9005 && min_align_of_type (TREE_TYPE (dst_type
))
9006 > min_align_of_type (TREE_TYPE (src_type
)))
9007 warning_at (loc
, OPT_Wcast_align
, "cast from %qH to %qI "
9008 "increases required alignment of target type",
9009 src_type
, dst_type
);
9013 expr
= cp_build_addr_expr (expr
, complain
);
9014 if (expr
== error_mark_node
)
9015 return error_mark_node
;
9016 expr
= build_nop (reference_type
, expr
);
9017 return convert_from_reference (expr
);
9021 expr
= decay_conversion (expr
, complain
);
9022 if (expr
== error_mark_node
)
9023 return error_mark_node
;
9025 /* build_c_cast puts on a NOP_EXPR to make the result not an
9026 lvalue. Strip such NOP_EXPRs if VALUE is being used in
9027 non-lvalue context. */
9028 if (TREE_CODE (expr
) == NOP_EXPR
9029 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
9030 expr
= TREE_OPERAND (expr
, 0);
9031 return build_nop (dst_type
, expr
);
9035 && !at_least_as_qualified_p (TREE_TYPE (dst_type
),
9036 TREE_TYPE (src_type
)))
9037 check_for_casting_away_constness (loc
, src_type
, dst_type
,
9038 CAST_EXPR
, complain
);
9041 if (complain
& tf_error
)
9042 error_at (loc
, "invalid %<const_cast%> from type %qT to type %qT",
9043 src_type
, dst_type
);
9044 return error_mark_node
;
9048 build_const_cast (location_t loc
, tree type
, tree expr
,
9049 tsubst_flags_t complain
)
9053 if (type
== error_mark_node
|| error_operand_p (expr
))
9054 return error_mark_node
;
9056 if (processing_template_decl
)
9058 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
9060 if (!TREE_SIDE_EFFECTS (t
)
9061 && type_dependent_expression_p (expr
))
9062 /* There might turn out to be side effects inside expr. */
9063 TREE_SIDE_EFFECTS (t
) = 1;
9064 r
= convert_from_reference (t
);
9065 protected_set_expr_location (r
, loc
);
9069 r
= build_const_cast_1 (loc
, type
, expr
, complain
, /*valid_p=*/NULL
);
9070 if (r
!= error_mark_node
)
9072 maybe_warn_about_useless_cast (loc
, type
, expr
, complain
);
9073 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
9075 protected_set_expr_location (r
, loc
);
9079 /* Like cp_build_c_cast, but for the c-common bits. */
9082 build_c_cast (location_t loc
, tree type
, tree expr
)
9084 return cp_build_c_cast (loc
, type
, expr
, tf_warning_or_error
);
9087 /* Like the "build_c_cast" used for c-common, but using cp_expr to
9088 preserve location information even for tree nodes that don't
9092 build_c_cast (location_t loc
, tree type
, cp_expr expr
)
9094 cp_expr result
= cp_build_c_cast (loc
, type
, expr
, tf_warning_or_error
);
9095 result
.set_location (loc
);
9099 /* Build an expression representing an explicit C-style cast to type
9100 TYPE of expression EXPR. */
9103 cp_build_c_cast (location_t loc
, tree type
, tree expr
,
9104 tsubst_flags_t complain
)
9110 if (type
== error_mark_node
|| error_operand_p (expr
))
9111 return error_mark_node
;
9113 if (processing_template_decl
)
9115 tree t
= build_min (CAST_EXPR
, type
,
9116 tree_cons (NULL_TREE
, value
, NULL_TREE
));
9117 /* We don't know if it will or will not have side effects. */
9118 TREE_SIDE_EFFECTS (t
) = 1;
9119 return convert_from_reference (t
);
9122 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
9123 'Class') should always be retained, because this information aids
9124 in method lookup. */
9125 if (objc_is_object_ptr (type
)
9126 && objc_is_object_ptr (TREE_TYPE (expr
)))
9127 return build_nop (type
, expr
);
9129 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9130 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9131 if (!TYPE_REF_P (type
)
9132 && TREE_CODE (value
) == NOP_EXPR
9133 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
9134 value
= TREE_OPERAND (value
, 0);
9136 if (TREE_CODE (type
) == ARRAY_TYPE
)
9138 /* Allow casting from T1* to T2[] because Cfront allows it.
9139 NIHCL uses it. It is not valid ISO C++ however. */
9140 if (TYPE_PTR_P (TREE_TYPE (expr
)))
9142 if (complain
& tf_error
)
9143 permerror (loc
, "ISO C++ forbids casting to an array type %qT",
9146 return error_mark_node
;
9147 type
= build_pointer_type (TREE_TYPE (type
));
9151 if (complain
& tf_error
)
9152 error_at (loc
, "ISO C++ forbids casting to an array type %qT",
9154 return error_mark_node
;
9158 if (FUNC_OR_METHOD_TYPE_P (type
))
9160 if (complain
& tf_error
)
9161 error_at (loc
, "invalid cast to function type %qT", type
);
9162 return error_mark_node
;
9165 if (TYPE_PTR_P (type
)
9166 && TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
9167 /* Casting to an integer of smaller size is an error detected elsewhere. */
9168 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (value
))
9169 /* Don't warn about converting any constant. */
9170 && !TREE_CONSTANT (value
))
9171 warning_at (loc
, OPT_Wint_to_pointer_cast
,
9172 "cast to pointer from integer of different size");
9174 /* A C-style cast can be a const_cast. */
9175 result
= build_const_cast_1 (loc
, type
, value
, complain
& tf_warning
,
9179 if (result
!= error_mark_node
)
9181 maybe_warn_about_useless_cast (loc
, type
, value
, complain
);
9182 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
9187 /* Or a static cast. */
9188 result
= build_static_cast_1 (loc
, type
, value
, /*c_cast_p=*/true,
9189 &valid_p
, complain
);
9190 /* Or a reinterpret_cast. */
9192 result
= build_reinterpret_cast_1 (loc
, type
, value
, /*c_cast_p=*/true,
9193 &valid_p
, complain
);
9194 /* The static_cast or reinterpret_cast may be followed by a
9197 /* A valid cast may result in errors if, for example, a
9198 conversion to an ambiguous base class is required. */
9199 && !error_operand_p (result
))
9203 maybe_warn_about_useless_cast (loc
, type
, value
, complain
);
9204 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
9206 /* Non-class rvalues always have cv-unqualified type. */
9207 if (!CLASS_TYPE_P (type
))
9208 type
= TYPE_MAIN_VARIANT (type
);
9209 result_type
= TREE_TYPE (result
);
9210 if (!CLASS_TYPE_P (result_type
) && !TYPE_REF_P (type
))
9211 result_type
= TYPE_MAIN_VARIANT (result_type
);
9212 /* If the type of RESULT does not match TYPE, perform a
9213 const_cast to make it match. If the static_cast or
9214 reinterpret_cast succeeded, we will differ by at most
9215 cv-qualification, so the follow-on const_cast is guaranteed
9217 if (!same_type_p (non_reference (type
), non_reference (result_type
)))
9219 result
= build_const_cast_1 (loc
, type
, result
, false, &valid_p
);
9220 gcc_assert (valid_p
);
9225 return error_mark_node
;
9228 /* Warn when a value is moved to itself with std::move. LHS is the target,
9229 RHS may be the std::move call, and LOC is the location of the whole
9233 maybe_warn_self_move (location_t loc
, tree lhs
, tree rhs
)
9235 if (!warn_self_move
)
9238 /* C++98 doesn't know move. */
9239 if (cxx_dialect
< cxx11
)
9242 if (processing_template_decl
)
9245 if (!REFERENCE_REF_P (rhs
)
9246 || TREE_CODE (TREE_OPERAND (rhs
, 0)) != CALL_EXPR
)
9248 tree fn
= TREE_OPERAND (rhs
, 0);
9249 if (!is_std_move_p (fn
))
9252 /* Just a little helper to strip * and various NOPs. */
9253 auto extract_op
= [] (tree
&op
) {
9255 while (INDIRECT_REF_P (op
))
9256 op
= TREE_OPERAND (op
, 0);
9257 op
= maybe_undo_parenthesized_ref (op
);
9258 STRIP_ANY_LOCATION_WRAPPER (op
);
9261 tree arg
= CALL_EXPR_ARG (fn
, 0);
9263 if (TREE_CODE (arg
) == ADDR_EXPR
)
9264 arg
= TREE_OPERAND (arg
, 0);
9265 tree type
= TREE_TYPE (lhs
);
9266 tree orig_lhs
= lhs
;
9268 if (cp_tree_equal (lhs
, arg
))
9270 auto_diagnostic_group d
;
9271 if (warning_at (loc
, OPT_Wself_move
,
9272 "moving %qE of type %qT to itself", orig_lhs
, type
))
9273 inform (loc
, "remove %<std::move%> call");
9277 /* For use from the C common bits. */
9279 build_modify_expr (location_t location
,
9280 tree lhs
, tree
/*lhs_origtype*/,
9281 enum tree_code modifycode
,
9282 location_t
/*rhs_location*/, tree rhs
,
9283 tree
/*rhs_origtype*/)
9285 return cp_build_modify_expr (location
, lhs
, modifycode
, rhs
,
9286 tf_warning_or_error
);
9289 /* Build an assignment expression of lvalue LHS from value RHS.
9290 MODIFYCODE is the code for a binary operator that we use
9291 to combine the old value of LHS with RHS to get the new value.
9292 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
9294 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
9297 cp_build_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
9298 tree rhs
, tsubst_flags_t complain
)
9300 lhs
= mark_lvalue_use_nonread (lhs
);
9302 tree result
= NULL_TREE
;
9304 tree lhstype
= TREE_TYPE (lhs
);
9306 tree olhstype
= lhstype
;
9307 bool plain_assign
= (modifycode
== NOP_EXPR
);
9308 bool compound_side_effects_p
= false;
9309 tree preeval
= NULL_TREE
;
9311 /* Avoid duplicate error messages from operands that had errors. */
9312 if (error_operand_p (lhs
) || error_operand_p (rhs
))
9313 return error_mark_node
;
9315 while (TREE_CODE (lhs
) == COMPOUND_EXPR
)
9317 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
9318 compound_side_effects_p
= true;
9319 lhs
= TREE_OPERAND (lhs
, 1);
9322 /* Handle control structure constructs used as "lvalues". Note that we
9323 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
9324 switch (TREE_CODE (lhs
))
9326 /* Handle --foo = 5; as these are valid constructs in C++. */
9327 case PREDECREMENT_EXPR
:
9328 case PREINCREMENT_EXPR
:
9329 if (compound_side_effects_p
)
9330 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
9331 lhs
= genericize_compound_lvalue (lhs
);
9333 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
9334 and looked through the COMPOUND_EXPRs, readd them now around
9335 the resulting lhs. */
9336 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
9338 lhs
= build2 (COMPOUND_EXPR
, lhstype
, TREE_OPERAND (olhs
, 0), lhs
);
9339 tree
*ptr
= &TREE_OPERAND (lhs
, 1);
9340 for (olhs
= TREE_OPERAND (olhs
, 1);
9341 TREE_CODE (olhs
) == COMPOUND_EXPR
;
9342 olhs
= TREE_OPERAND (olhs
, 1))
9344 *ptr
= build2 (COMPOUND_EXPR
, lhstype
,
9345 TREE_OPERAND (olhs
, 0), *ptr
);
9346 ptr
= &TREE_OPERAND (*ptr
, 1);
9352 if (compound_side_effects_p
)
9353 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
9354 lhs
= genericize_compound_lvalue (lhs
);
9355 goto maybe_add_compound
;
9359 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
9360 when neither operand has side-effects. */
9361 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
9362 return error_mark_node
;
9364 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0))
9365 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 1)));
9367 lhs
= build3 (COND_EXPR
, TREE_TYPE (lhs
),
9368 build2 (TREE_CODE (lhs
) == MIN_EXPR
? LE_EXPR
: GE_EXPR
,
9370 TREE_OPERAND (lhs
, 0),
9371 TREE_OPERAND (lhs
, 1)),
9372 TREE_OPERAND (lhs
, 0),
9373 TREE_OPERAND (lhs
, 1));
9376 /* Handle (a ? b : c) used as an "lvalue". */
9379 /* Produce (a ? (b = rhs) : (c = rhs))
9380 except that the RHS goes through a save-expr
9381 so the code to compute it is only emitted once. */
9382 if (VOID_TYPE_P (TREE_TYPE (rhs
)))
9384 if (complain
& tf_error
)
9385 error_at (cp_expr_loc_or_loc (rhs
, loc
),
9386 "void value not ignored as it ought to be");
9387 return error_mark_node
;
9390 rhs
= stabilize_expr (rhs
, &preeval
);
9392 /* Check this here to avoid odd errors when trying to convert
9393 a throw to the type of the COND_EXPR. */
9394 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
9395 return error_mark_node
;
9397 tree op1
= TREE_OPERAND (lhs
, 1);
9398 if (TREE_CODE (op1
) != THROW_EXPR
)
9399 op1
= cp_build_modify_expr (loc
, op1
, modifycode
, rhs
, complain
);
9400 /* When sanitizing undefined behavior, even when rhs doesn't need
9401 stabilization at this point, the sanitization might add extra
9402 SAVE_EXPRs in there and so make sure there is no tree sharing
9403 in the rhs, otherwise those SAVE_EXPRs will have initialization
9404 only in one of the two branches. */
9405 if (sanitize_flags_p (SANITIZE_UNDEFINED
9406 | SANITIZE_UNDEFINED_NONDEFAULT
))
9407 rhs
= unshare_expr (rhs
);
9408 tree op2
= TREE_OPERAND (lhs
, 2);
9409 if (TREE_CODE (op2
) != THROW_EXPR
)
9410 op2
= cp_build_modify_expr (loc
, op2
, modifycode
, rhs
, complain
);
9411 tree cond
= build_conditional_expr (input_location
,
9412 TREE_OPERAND (lhs
, 0), op1
, op2
,
9415 if (cond
== error_mark_node
)
9417 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
9418 and looked through the COMPOUND_EXPRs, readd them now around
9419 the resulting cond before adding the preevaluated rhs. */
9420 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
9422 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
9423 TREE_OPERAND (olhs
, 0), cond
);
9424 tree
*ptr
= &TREE_OPERAND (cond
, 1);
9425 for (olhs
= TREE_OPERAND (olhs
, 1);
9426 TREE_CODE (olhs
) == COMPOUND_EXPR
;
9427 olhs
= TREE_OPERAND (olhs
, 1))
9429 *ptr
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
9430 TREE_OPERAND (olhs
, 0), *ptr
);
9431 ptr
= &TREE_OPERAND (*ptr
, 1);
9434 /* Make sure the code to compute the rhs comes out
9435 before the split. */
9445 if (modifycode
== INIT_EXPR
)
9447 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9448 /* Do the default thing. */;
9449 else if (TREE_CODE (rhs
) == CONSTRUCTOR
)
9451 /* Compound literal. */
9452 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
9453 /* Call convert to generate an error; see PR 11063. */
9454 rhs
= convert (lhstype
, rhs
);
9455 result
= cp_build_init_expr (lhs
, rhs
);
9456 TREE_SIDE_EFFECTS (result
) = 1;
9459 else if (! MAYBE_CLASS_TYPE_P (lhstype
))
9460 /* Do the default thing. */;
9463 releasing_vec rhs_vec
= make_tree_vector_single (rhs
);
9464 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
9465 &rhs_vec
, lhstype
, LOOKUP_NORMAL
,
9467 if (result
== NULL_TREE
)
9468 return error_mark_node
;
9474 lhs
= require_complete_type (lhs
, complain
);
9475 if (lhs
== error_mark_node
)
9476 return error_mark_node
;
9478 if (modifycode
== NOP_EXPR
)
9480 maybe_warn_self_move (loc
, lhs
, rhs
);
9482 if (c_dialect_objc ())
9484 result
= objc_maybe_build_modify_expr (lhs
, rhs
);
9489 /* `operator=' is not an inheritable operator. */
9490 if (! MAYBE_CLASS_TYPE_P (lhstype
))
9491 /* Do the default thing. */;
9494 result
= build_new_op (input_location
, MODIFY_EXPR
,
9495 LOOKUP_NORMAL
, lhs
, rhs
,
9496 make_node (NOP_EXPR
), NULL_TREE
,
9497 /*overload=*/NULL
, complain
);
9498 if (result
== NULL_TREE
)
9499 return error_mark_node
;
9506 tree init
= NULL_TREE
;
9508 /* A binary op has been requested. Combine the old LHS
9509 value with the RHS producing the value we should actually
9510 store into the LHS. */
9511 gcc_assert (!((TYPE_REF_P (lhstype
)
9512 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype
)))
9513 || MAYBE_CLASS_TYPE_P (lhstype
)));
9515 /* Preevaluate the RHS to make sure its evaluation is complete
9516 before the lvalue-to-rvalue conversion of the LHS:
9518 [expr.ass] With respect to an indeterminately-sequenced
9519 function call, the operation of a compound assignment is a
9520 single evaluation. [ Note: Therefore, a function call shall
9521 not intervene between the lvalue-to-rvalue conversion and the
9522 side effect associated with any single compound assignment
9523 operator. -- end note ] */
9524 lhs
= cp_stabilize_reference (lhs
);
9525 rhs
= decay_conversion (rhs
, complain
);
9526 if (rhs
== error_mark_node
)
9527 return error_mark_node
;
9528 rhs
= stabilize_expr (rhs
, &init
);
9529 newrhs
= cp_build_binary_op (loc
, modifycode
, lhs
, rhs
, complain
);
9530 if (newrhs
== error_mark_node
)
9532 if (complain
& tf_error
)
9533 inform (loc
, " in evaluation of %<%Q(%#T, %#T)%>",
9534 modifycode
, TREE_TYPE (lhs
), TREE_TYPE (rhs
));
9535 return error_mark_node
;
9539 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), init
, newrhs
);
9541 /* Now it looks like a plain assignment. */
9542 modifycode
= NOP_EXPR
;
9543 if (c_dialect_objc ())
9545 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
9550 gcc_assert (!TYPE_REF_P (lhstype
));
9551 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs
)));
9554 /* The left-hand side must be an lvalue. */
9555 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
9556 return error_mark_node
;
9558 /* Warn about modifying something that is `const'. Don't warn if
9559 this is initialization. */
9560 if (modifycode
!= INIT_EXPR
9561 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
9562 /* Functions are not modifiable, even though they are
9564 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs
))
9565 /* If it's an aggregate and any field is const, then it is
9566 effectively const. */
9567 || (CLASS_TYPE_P (lhstype
)
9568 && C_TYPE_FIELDS_READONLY (lhstype
))))
9570 if (complain
& tf_error
)
9571 cxx_readonly_error (loc
, lhs
, lv_assign
);
9572 return error_mark_node
;
9575 /* If storing into a structure or union member, it may have been given a
9576 lowered bitfield type. We need to convert to the declared type first,
9577 so retrieve it now. */
9579 olhstype
= unlowered_expr_type (lhs
);
9581 /* Convert new value to destination type. */
9583 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
9587 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs
))
9589 if (modifycode
!= INIT_EXPR
)
9591 if (complain
& tf_error
)
9593 "assigning to an array from an initializer list");
9594 return error_mark_node
;
9596 if (check_array_initializer (lhs
, lhstype
, newrhs
))
9597 return error_mark_node
;
9598 newrhs
= digest_init (lhstype
, newrhs
, complain
);
9599 if (newrhs
== error_mark_node
)
9600 return error_mark_node
;
9603 /* C++11 8.5/17: "If the destination type is an array of characters,
9604 an array of char16_t, an array of char32_t, or an array of wchar_t,
9605 and the initializer is a string literal...". */
9606 else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs
))
9608 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype
)))
9609 && modifycode
== INIT_EXPR
)
9611 newrhs
= digest_init (lhstype
, newrhs
, complain
);
9612 if (newrhs
== error_mark_node
)
9613 return error_mark_node
;
9616 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
9617 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))))
9619 if (complain
& tf_error
)
9620 error_at (loc
, "incompatible types in assignment of %qT to %qT",
9621 TREE_TYPE (rhs
), lhstype
);
9622 return error_mark_node
;
9625 /* Allow array assignment in compiler-generated code. */
9626 else if (!current_function_decl
9627 || !DECL_DEFAULTED_FN (current_function_decl
))
9629 /* This routine is used for both initialization and assignment.
9630 Make sure the diagnostic message differentiates the context. */
9631 if (complain
& tf_error
)
9633 if (modifycode
== INIT_EXPR
)
9634 error_at (loc
, "array used as initializer");
9636 error_at (loc
, "invalid array assignment");
9638 return error_mark_node
;
9641 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
9642 ? 1 + (modifycode
!= INIT_EXPR
): 0;
9643 result
= build_vec_init (lhs
, NULL_TREE
, newrhs
,
9644 /*explicit_value_init_p=*/false,
9645 from_array
, complain
);
9649 if (modifycode
== INIT_EXPR
)
9650 /* Calls with INIT_EXPR are all direct-initialization, so don't set
9651 LOOKUP_ONLYCONVERTING. */
9652 newrhs
= convert_for_initialization (lhs
, olhstype
, newrhs
, LOOKUP_NORMAL
,
9653 ICR_INIT
, NULL_TREE
, 0,
9654 complain
| tf_no_cleanup
);
9656 newrhs
= convert_for_assignment (olhstype
, newrhs
, ICR_ASSIGN
,
9657 NULL_TREE
, 0, complain
, LOOKUP_IMPLICIT
);
9659 if (!same_type_p (lhstype
, olhstype
))
9660 newrhs
= cp_convert_and_check (lhstype
, newrhs
, complain
);
9662 if (modifycode
!= INIT_EXPR
)
9664 if (TREE_CODE (newrhs
) == CALL_EXPR
9665 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
9666 newrhs
= build_cplus_new (lhstype
, newrhs
, complain
);
9668 /* Can't initialize directly from a TARGET_EXPR, since that would
9669 cause the lhs to be constructed twice, and possibly result in
9670 accidental self-initialization. So we force the TARGET_EXPR to be
9671 expanded without a target. */
9672 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
9673 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
9674 TREE_OPERAND (newrhs
, 0));
9677 if (newrhs
== error_mark_node
)
9678 return error_mark_node
;
9680 if (c_dialect_objc () && flag_objc_gc
)
9682 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
9688 result
= build2_loc (loc
, modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
9689 lhstype
, lhs
, newrhs
);
9690 if (modifycode
== INIT_EXPR
)
9691 set_target_expr_eliding (newrhs
);
9693 TREE_SIDE_EFFECTS (result
) = 1;
9695 suppress_warning (result
, OPT_Wparentheses
);
9699 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), preeval
, result
);
9704 build_x_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
9705 tree rhs
, tree lookups
, tsubst_flags_t complain
)
9707 tree orig_lhs
= lhs
;
9708 tree orig_rhs
= rhs
;
9709 tree overload
= NULL_TREE
;
9711 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
9712 return cp_expr (error_mark_node
, loc
);
9714 if (processing_template_decl
)
9716 if (modifycode
== NOP_EXPR
9717 || type_dependent_expression_p (lhs
)
9718 || type_dependent_expression_p (rhs
))
9720 tree op
= build_min_nt_loc (loc
, modifycode
, NULL_TREE
, NULL_TREE
);
9721 tree rval
= build_min_nt_loc (loc
, MODOP_EXPR
, lhs
, op
, rhs
);
9722 if (modifycode
!= NOP_EXPR
)
9724 = build_dependent_operator_type (lookups
, modifycode
, true);
9728 lhs
= build_non_dependent_expr (lhs
);
9729 rhs
= build_non_dependent_expr (rhs
);
9732 if (modifycode
!= NOP_EXPR
)
9734 tree op
= build_nt (modifycode
, NULL_TREE
, NULL_TREE
);
9735 tree rval
= build_new_op (loc
, MODIFY_EXPR
, LOOKUP_NORMAL
,
9736 lhs
, rhs
, op
, lookups
, &overload
, complain
);
9739 if (rval
== error_mark_node
)
9741 suppress_warning (rval
/* What warning? */);
9742 if (processing_template_decl
)
9744 if (overload
!= NULL_TREE
)
9745 return (build_min_non_dep_op_overload
9746 (MODIFY_EXPR
, rval
, overload
, orig_lhs
, orig_rhs
));
9748 return (build_min_non_dep
9749 (MODOP_EXPR
, rval
, orig_lhs
, op
, orig_rhs
));
9754 return cp_build_modify_expr (loc
, lhs
, modifycode
, rhs
, complain
);
9757 /* Helper function for get_delta_difference which assumes FROM is a base
9758 class of TO. Returns a delta for the conversion of pointer-to-member
9759 of FROM to pointer-to-member of TO. If the conversion is invalid and
9760 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
9761 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
9762 If C_CAST_P is true, this conversion is taking place as part of a
9766 get_delta_difference_1 (tree from
, tree to
, bool c_cast_p
,
9767 tsubst_flags_t complain
)
9772 binfo
= lookup_base (to
, from
, c_cast_p
? ba_unique
: ba_check
,
9775 if (binfo
== error_mark_node
)
9777 if (!(complain
& tf_error
))
9778 return error_mark_node
;
9780 inform (input_location
, " in pointer to member function conversion");
9781 return size_zero_node
;
9785 if (kind
!= bk_via_virtual
)
9786 return BINFO_OFFSET (binfo
);
9788 /* FROM is a virtual base class of TO. Issue an error or warning
9789 depending on whether or not this is a reinterpret cast. */
9791 if (!(complain
& tf_error
))
9792 return error_mark_node
;
9794 error ("pointer to member conversion via virtual base %qT",
9795 BINFO_TYPE (binfo_from_vbase (binfo
)));
9797 return size_zero_node
;
9804 /* Get difference in deltas for different pointer to member function
9805 types. If the conversion is invalid and tf_error is not set in
9806 COMPLAIN, returns error_mark_node, otherwise returns an integer
9807 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
9808 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
9809 conversions as well. If C_CAST_P is true this conversion is taking
9810 place as part of a C-style cast.
9812 Note that the naming of FROM and TO is kind of backwards; the return
9813 value is what we add to a TO in order to get a FROM. They are named
9814 this way because we call this function to find out how to convert from
9815 a pointer to member of FROM to a pointer to member of TO. */
9818 get_delta_difference (tree from
, tree to
,
9819 bool allow_inverse_p
,
9820 bool c_cast_p
, tsubst_flags_t complain
)
9824 if (same_type_ignoring_top_level_qualifiers_p (from
, to
))
9825 /* Pointer to member of incomplete class is permitted*/
9826 result
= size_zero_node
;
9828 result
= get_delta_difference_1 (from
, to
, c_cast_p
, complain
);
9830 if (result
== error_mark_node
)
9831 return error_mark_node
;
9835 if (!allow_inverse_p
)
9837 if (!(complain
& tf_error
))
9838 return error_mark_node
;
9840 error_not_base_type (from
, to
);
9841 inform (input_location
, " in pointer to member conversion");
9842 result
= size_zero_node
;
9846 result
= get_delta_difference_1 (to
, from
, c_cast_p
, complain
);
9848 if (result
== error_mark_node
)
9849 return error_mark_node
;
9852 result
= size_diffop_loc (input_location
,
9853 size_zero_node
, result
);
9856 if (!(complain
& tf_error
))
9857 return error_mark_node
;
9859 error_not_base_type (from
, to
);
9860 inform (input_location
, " in pointer to member conversion");
9861 result
= size_zero_node
;
9866 return convert_to_integer (ptrdiff_type_node
, result
);
9869 /* Return a constructor for the pointer-to-member-function TYPE using
9870 the other components as specified. */
9873 build_ptrmemfunc1 (tree type
, tree delta
, tree pfn
)
9878 vec
<constructor_elt
, va_gc
> *v
;
9880 /* Pull the FIELD_DECLs out of the type. */
9881 pfn_field
= TYPE_FIELDS (type
);
9882 delta_field
= DECL_CHAIN (pfn_field
);
9884 /* Make sure DELTA has the type we want. */
9885 delta
= convert_and_check (input_location
, delta_type_node
, delta
);
9887 /* Convert to the correct target type if necessary. */
9888 pfn
= fold_convert (TREE_TYPE (pfn_field
), pfn
);
9890 /* Finish creating the initializer. */
9892 CONSTRUCTOR_APPEND_ELT(v
, pfn_field
, pfn
);
9893 CONSTRUCTOR_APPEND_ELT(v
, delta_field
, delta
);
9894 u
= build_constructor (type
, v
);
9895 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) & TREE_CONSTANT (delta
);
9896 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
9897 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
9899 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
9904 /* Build a constructor for a pointer to member function. It can be
9905 used to initialize global variables, local variable, or used
9906 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
9909 If FORCE is nonzero, then force this conversion, even if
9910 we would rather not do it. Usually set when using an explicit
9911 cast. A C-style cast is being processed iff C_CAST_P is true.
9913 Return error_mark_node, if something goes wrong. */
9916 build_ptrmemfunc (tree type
, tree pfn
, int force
, bool c_cast_p
,
9917 tsubst_flags_t complain
)
9923 if (error_operand_p (pfn
))
9924 return error_mark_node
;
9926 pfn_type
= TREE_TYPE (pfn
);
9927 to_type
= build_ptrmemfunc_type (type
);
9929 /* Handle multiple conversions of pointer to member functions. */
9930 if (TYPE_PTRMEMFUNC_P (pfn_type
))
9932 tree delta
= NULL_TREE
;
9933 tree npfn
= NULL_TREE
;
9937 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
,
9938 LOOKUP_NORMAL
, complain
))
9940 if (complain
& tf_error
)
9941 error ("invalid conversion to type %qT from type %qT",
9944 return error_mark_node
;
9947 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
9948 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
9950 c_cast_p
, complain
);
9951 if (n
== error_mark_node
)
9952 return error_mark_node
;
9954 /* We don't have to do any conversion to convert a
9955 pointer-to-member to its own type. But, we don't want to
9956 just return a PTRMEM_CST if there's an explicit cast; that
9957 cast should make the expression an invalid template argument. */
9958 if (TREE_CODE (pfn
) != PTRMEM_CST
)
9960 if (same_type_p (to_type
, pfn_type
))
9962 else if (integer_zerop (n
) && TREE_CODE (pfn
) != CONSTRUCTOR
)
9963 return build_reinterpret_cast (input_location
, to_type
, pfn
,
9967 if (TREE_SIDE_EFFECTS (pfn
))
9968 pfn
= save_expr (pfn
);
9970 /* Obtain the function pointer and the current DELTA. */
9971 if (TREE_CODE (pfn
) == PTRMEM_CST
)
9972 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
9975 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
9976 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
9979 /* Just adjust the DELTA field. */
9980 gcc_assert (same_type_ignoring_top_level_qualifiers_p
9981 (TREE_TYPE (delta
), ptrdiff_type_node
));
9982 if (!integer_zerop (n
))
9984 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
9985 n
= cp_build_binary_op (input_location
,
9986 LSHIFT_EXPR
, n
, integer_one_node
,
9988 delta
= cp_build_binary_op (input_location
,
9989 PLUS_EXPR
, delta
, n
, complain
);
9991 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
9994 /* Handle null pointer to member function conversions. */
9995 if (null_ptr_cst_p (pfn
))
9997 pfn
= cp_build_c_cast (input_location
,
9998 TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type
),
10000 return build_ptrmemfunc1 (to_type
,
10005 if (type_unknown_p (pfn
))
10006 return instantiate_type (type
, pfn
, complain
);
10008 fn
= TREE_OPERAND (pfn
, 0);
10009 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
10010 /* In a template, we will have preserved the
10012 || (processing_template_decl
&& TREE_CODE (fn
) == OFFSET_REF
));
10013 return make_ptrmem_cst (to_type
, fn
);
10016 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
10019 ??? There is no consistency as to the types returned for the above
10020 values. Some code acts as if it were a sizetype and some as if it were
10021 integer_type_node. */
10024 expand_ptrmemfunc_cst (tree cst
, tree
*delta
, tree
*pfn
)
10026 tree type
= TREE_TYPE (cst
);
10027 tree fn
= PTRMEM_CST_MEMBER (cst
);
10028 tree ptr_class
, fn_class
;
10030 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
10032 /* The class that the function belongs to. */
10033 fn_class
= DECL_CONTEXT (fn
);
10035 /* The class that we're creating a pointer to member of. */
10036 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
10038 /* First, calculate the adjustment to the function's class. */
10039 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0,
10040 /*c_cast_p=*/0, tf_warning_or_error
);
10042 if (!DECL_VIRTUAL_P (fn
))
10044 tree t
= build_addr_func (fn
, tf_warning_or_error
);
10045 if (TREE_CODE (t
) == ADDR_EXPR
)
10046 SET_EXPR_LOCATION (t
, PTRMEM_CST_LOCATION (cst
));
10047 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), t
);
10051 /* If we're dealing with a virtual function, we have to adjust 'this'
10052 again, to point to the base which provides the vtable entry for
10053 fn; the call will do the opposite adjustment. */
10054 tree orig_class
= DECL_CONTEXT (fn
);
10055 tree binfo
= binfo_or_else (orig_class
, fn_class
);
10056 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
10057 *delta
, BINFO_OFFSET (binfo
));
10059 /* We set PFN to the vtable offset at which the function can be
10060 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
10061 case delta is shifted left, and then incremented). */
10062 *pfn
= DECL_VINDEX (fn
);
10063 *pfn
= fold_build2 (MULT_EXPR
, integer_type_node
, *pfn
,
10064 TYPE_SIZE_UNIT (vtable_entry_type
));
10066 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
10068 case ptrmemfunc_vbit_in_pfn
:
10069 *pfn
= fold_build2 (PLUS_EXPR
, integer_type_node
, *pfn
,
10073 case ptrmemfunc_vbit_in_delta
:
10074 *delta
= fold_build2 (LSHIFT_EXPR
, TREE_TYPE (*delta
),
10075 *delta
, integer_one_node
);
10076 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
10077 *delta
, integer_one_node
);
10081 gcc_unreachable ();
10084 *pfn
= fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), *pfn
);
10088 /* Return an expression for PFN from the pointer-to-member function
10092 pfn_from_ptrmemfunc (tree t
)
10094 if (TREE_CODE (t
) == PTRMEM_CST
)
10099 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
10104 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
10107 /* Return an expression for DELTA from the pointer-to-member function
10111 delta_from_ptrmemfunc (tree t
)
10113 if (TREE_CODE (t
) == PTRMEM_CST
)
10118 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
10123 return build_ptrmemfunc_access_expr (t
, delta_identifier
);
10126 /* Convert value RHS to type TYPE as preparation for an assignment to
10127 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
10128 implicit conversion is. If FNDECL is non-NULL, we are doing the
10129 conversion in order to pass the PARMNUMth argument of FNDECL.
10130 If FNDECL is NULL, we are doing the conversion in function pointer
10131 argument passing, conversion in initialization, etc. */
10134 convert_for_assignment (tree type
, tree rhs
,
10135 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
10136 tsubst_flags_t complain
, int flags
)
10139 enum tree_code coder
;
10141 location_t rhs_loc
= cp_expr_loc_or_input_loc (rhs
);
10142 bool has_loc
= EXPR_LOCATION (rhs
) != UNKNOWN_LOCATION
;
10143 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
10144 but preserve location wrappers. */
10145 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
10146 && !location_wrapper_p (rhs
))
10147 rhs
= TREE_OPERAND (rhs
, 0);
10149 /* Handle [dcl.init.list] direct-list-initialization from
10150 single element of enumeration with a fixed underlying type. */
10151 if (is_direct_enum_init (type
, rhs
))
10153 tree elt
= CONSTRUCTOR_ELT (rhs
, 0)->value
;
10154 if (check_narrowing (ENUM_UNDERLYING_TYPE (type
), elt
, complain
))
10156 warning_sentinel
w (warn_useless_cast
);
10157 warning_sentinel
w2 (warn_ignored_qualifiers
);
10158 rhs
= cp_build_c_cast (rhs_loc
, type
, elt
, complain
);
10161 rhs
= error_mark_node
;
10164 rhstype
= TREE_TYPE (rhs
);
10165 coder
= TREE_CODE (rhstype
);
10167 if (VECTOR_TYPE_P (type
) && coder
== VECTOR_TYPE
10168 && vector_types_convertible_p (type
, rhstype
, true))
10170 rhs
= mark_rvalue_use (rhs
);
10171 return convert (type
, rhs
);
10174 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
10175 return error_mark_node
;
10176 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
10177 return error_mark_node
;
10179 /* The RHS of an assignment cannot have void type. */
10180 if (coder
== VOID_TYPE
)
10182 if (complain
& tf_error
)
10183 error_at (rhs_loc
, "void value not ignored as it ought to be");
10184 return error_mark_node
;
10187 if (c_dialect_objc ())
10191 tree rname
= fndecl
;
10202 selector
= objc_message_selector ();
10204 if (selector
&& parmno
> 1)
10211 if (objc_compare_types (type
, rhstype
, parmno
, rname
))
10213 rhs
= mark_rvalue_use (rhs
);
10214 return convert (type
, rhs
);
10220 The expression is implicitly converted (clause _conv_) to the
10221 cv-unqualified type of the left operand.
10223 We allow bad conversions here because by the time we get to this point
10224 we are committed to doing the conversion. If we end up doing a bad
10225 conversion, convert_like will complain. */
10226 if (!can_convert_arg_bad (type
, rhstype
, rhs
, flags
, complain
))
10228 /* When -Wno-pmf-conversions is use, we just silently allow
10229 conversions from pointers-to-members to plain pointers. If
10230 the conversion doesn't work, cp_convert will complain. */
10232 && TYPE_PTR_P (type
)
10233 && TYPE_PTRMEMFUNC_P (rhstype
))
10234 rhs
= cp_convert (strip_top_quals (type
), rhs
, complain
);
10237 if (complain
& tf_error
)
10239 /* If the right-hand side has unknown type, then it is an
10240 overloaded function. Call instantiate_type to get error
10242 if (rhstype
== unknown_type_node
)
10244 tree r
= instantiate_type (type
, rhs
, tf_warning_or_error
);
10245 /* -fpermissive might allow this; recurse. */
10246 if (!seen_error ())
10247 return convert_for_assignment (type
, r
, errtype
, fndecl
,
10248 parmnum
, complain
, flags
);
10251 complain_about_bad_argument (rhs_loc
,
10256 range_label_for_type_mismatch
label (rhstype
, type
);
10257 gcc_rich_location
richloc (rhs_loc
, has_loc
? &label
: NULL
);
10260 case ICR_DEFAULT_ARGUMENT
:
10261 error_at (&richloc
,
10262 "cannot convert %qH to %qI in default argument",
10266 error_at (&richloc
,
10267 "cannot convert %qH to %qI in argument passing",
10270 case ICR_CONVERTING
:
10271 error_at (&richloc
, "cannot convert %qH to %qI",
10275 error_at (&richloc
,
10276 "cannot convert %qH to %qI in initialization",
10280 error_at (&richloc
, "cannot convert %qH to %qI in return",
10284 error_at (&richloc
,
10285 "cannot convert %qH to %qI in assignment",
10292 if (TYPE_PTR_P (rhstype
)
10293 && TYPE_PTR_P (type
)
10294 && CLASS_TYPE_P (TREE_TYPE (rhstype
))
10295 && CLASS_TYPE_P (TREE_TYPE (type
))
10296 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype
)))
10297 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
10298 (TREE_TYPE (rhstype
))),
10299 "class type %qT is incomplete", TREE_TYPE (rhstype
));
10301 return error_mark_node
;
10304 if (warn_suggest_attribute_format
)
10306 const enum tree_code codel
= TREE_CODE (type
);
10307 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
10309 && check_missing_format_attribute (type
, rhstype
)
10310 && (complain
& tf_warning
))
10314 case ICR_DEFAULT_ARGUMENT
:
10316 warning (OPT_Wsuggest_attribute_format
,
10317 "parameter %qP of %qD might be a candidate "
10318 "for a format attribute", parmnum
, fndecl
);
10320 warning (OPT_Wsuggest_attribute_format
,
10321 "parameter might be a candidate "
10322 "for a format attribute");
10324 case ICR_CONVERTING
:
10325 warning (OPT_Wsuggest_attribute_format
,
10326 "target of conversion might be a candidate "
10327 "for a format attribute");
10330 warning (OPT_Wsuggest_attribute_format
,
10331 "target of initialization might be a candidate "
10332 "for a format attribute");
10335 warning (OPT_Wsuggest_attribute_format
,
10336 "return type might be a candidate "
10337 "for a format attribute");
10340 warning (OPT_Wsuggest_attribute_format
,
10341 "left-hand side of assignment might be a candidate "
10342 "for a format attribute");
10349 /* If -Wparentheses, warn about a = b = c when a has type bool and b
10351 if (warn_parentheses
10352 && TREE_CODE (type
) == BOOLEAN_TYPE
10353 && TREE_CODE (rhs
) == MODIFY_EXPR
10354 && !warning_suppressed_p (rhs
, OPT_Wparentheses
)
10355 && TREE_CODE (TREE_TYPE (rhs
)) != BOOLEAN_TYPE
10356 && (complain
& tf_warning
)
10357 && warning_at (rhs_loc
, OPT_Wparentheses
,
10358 "suggest parentheses around assignment used as "
10360 suppress_warning (rhs
, OPT_Wparentheses
);
10362 if (complain
& tf_warning
)
10363 warn_for_address_or_pointer_of_packed_member (type
, rhs
);
10365 return perform_implicit_conversion_flags (strip_top_quals (type
), rhs
,
10369 /* Convert RHS to be of type TYPE.
10370 If EXP is nonzero, it is the target of the initialization.
10371 ERRTYPE indicates what kind of error the implicit conversion is.
10373 Two major differences between the behavior of
10374 `convert_for_assignment' and `convert_for_initialization'
10375 are that references are bashed in the former, while
10376 copied in the latter, and aggregates are assigned in
10377 the former (operator=) while initialized in the
10380 If using constructor make sure no conversion operator exists, if one does
10381 exist, an ambiguity exists. */
10384 convert_for_initialization (tree exp
, tree type
, tree rhs
, int flags
,
10385 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
10386 tsubst_flags_t complain
)
10388 enum tree_code codel
= TREE_CODE (type
);
10390 enum tree_code coder
;
10392 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
10393 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
10394 if (TREE_CODE (rhs
) == NOP_EXPR
10395 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
10396 && codel
!= REFERENCE_TYPE
)
10397 rhs
= TREE_OPERAND (rhs
, 0);
10399 if (type
== error_mark_node
10400 || rhs
== error_mark_node
10401 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
10402 return error_mark_node
;
10404 if (MAYBE_CLASS_TYPE_P (non_reference (type
)))
10406 else if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
10407 && TREE_CODE (type
) != ARRAY_TYPE
10408 && (!TYPE_REF_P (type
)
10409 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
10410 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
10411 && !TYPE_REFFN_P (type
))
10412 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
10413 rhs
= decay_conversion (rhs
, complain
);
10415 rhstype
= TREE_TYPE (rhs
);
10416 coder
= TREE_CODE (rhstype
);
10418 if (coder
== ERROR_MARK
)
10419 return error_mark_node
;
10421 /* We accept references to incomplete types, so we can
10422 return here before checking if RHS is of complete type. */
10424 if (codel
== REFERENCE_TYPE
)
10426 auto_diagnostic_group d
;
10427 /* This should eventually happen in convert_arguments. */
10428 int savew
= 0, savee
= 0;
10431 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
10432 rhs
= initialize_reference (type
, rhs
, flags
, complain
);
10435 && (warningcount
+ werrorcount
> savew
|| errorcount
> savee
))
10436 inform (get_fndecl_argument_location (fndecl
, parmnum
),
10437 "in passing argument %P of %qD", parmnum
, fndecl
);
10442 exp
= require_complete_type (exp
, complain
);
10443 if (exp
== error_mark_node
)
10444 return error_mark_node
;
10446 type
= complete_type (type
);
10448 if (DIRECT_INIT_EXPR_P (type
, rhs
))
10449 /* Don't try to do copy-initialization if we already have
10450 direct-initialization. */
10453 if (MAYBE_CLASS_TYPE_P (type
))
10454 return perform_implicit_conversion_flags (type
, rhs
, complain
, flags
);
10456 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
,
10460 /* If RETVAL is the address of, or a reference to, a local variable or
10461 temporary give an appropriate warning and return true. */
10464 maybe_warn_about_returning_address_of_local (tree retval
, location_t loc
)
10466 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
10467 tree whats_returned
= fold_for_warn (retval
);
10469 loc
= cp_expr_loc_or_input_loc (retval
);
10473 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
10474 whats_returned
= TREE_OPERAND (whats_returned
, 1);
10475 else if (CONVERT_EXPR_P (whats_returned
)
10476 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
)
10477 whats_returned
= TREE_OPERAND (whats_returned
, 0);
10482 if (TREE_CODE (whats_returned
) == TARGET_EXPR
10483 && is_std_init_list (TREE_TYPE (whats_returned
)))
10485 tree init
= TARGET_EXPR_INITIAL (whats_returned
);
10486 if (TREE_CODE (init
) == CONSTRUCTOR
)
10487 /* Pull out the array address. */
10488 whats_returned
= CONSTRUCTOR_ELT (init
, 0)->value
;
10489 else if (TREE_CODE (init
) == INDIRECT_REF
)
10490 /* The source of a trivial copy looks like *(T*)&var. */
10491 whats_returned
= TREE_OPERAND (init
, 0);
10494 STRIP_NOPS (whats_returned
);
10497 /* As a special case, we handle a call to std::move or std::forward. */
10498 if (TREE_CODE (whats_returned
) == CALL_EXPR
10499 && (is_std_move_p (whats_returned
)
10500 || is_std_forward_p (whats_returned
)))
10502 tree arg
= CALL_EXPR_ARG (whats_returned
, 0);
10503 return maybe_warn_about_returning_address_of_local (arg
, loc
);
10506 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
10508 whats_returned
= TREE_OPERAND (whats_returned
, 0);
10510 while (TREE_CODE (whats_returned
) == COMPONENT_REF
10511 || TREE_CODE (whats_returned
) == ARRAY_REF
)
10512 whats_returned
= TREE_OPERAND (whats_returned
, 0);
10514 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
10515 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
10517 if (TYPE_REF_P (valtype
))
10518 warning_at (loc
, OPT_Wreturn_local_addr
,
10519 "returning reference to temporary");
10520 else if (is_std_init_list (valtype
))
10521 warning_at (loc
, OPT_Winit_list_lifetime
,
10522 "returning temporary %<initializer_list%> does not extend "
10523 "the lifetime of the underlying array");
10527 STRIP_ANY_LOCATION_WRAPPER (whats_returned
);
10529 if (DECL_P (whats_returned
)
10530 && DECL_NAME (whats_returned
)
10531 && DECL_FUNCTION_SCOPE_P (whats_returned
)
10532 && !is_capture_proxy (whats_returned
)
10533 && !(TREE_STATIC (whats_returned
)
10534 || TREE_PUBLIC (whats_returned
)))
10536 if (VAR_P (whats_returned
)
10537 && DECL_DECOMPOSITION_P (whats_returned
)
10538 && DECL_DECOMP_BASE (whats_returned
)
10539 && DECL_HAS_VALUE_EXPR_P (whats_returned
))
10541 /* When returning address of a structured binding, if the structured
10542 binding is not a reference, continue normally, if it is a
10543 reference, recurse on the initializer of the structured
10545 tree base
= DECL_DECOMP_BASE (whats_returned
);
10546 if (TYPE_REF_P (TREE_TYPE (base
)))
10548 if (tree init
= DECL_INITIAL (base
))
10549 return maybe_warn_about_returning_address_of_local (init
, loc
);
10555 auto_diagnostic_group d
;
10556 if (TYPE_REF_P (valtype
))
10557 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
10558 "reference to local variable %qD returned",
10560 else if (is_std_init_list (valtype
))
10561 w
= warning_at (loc
, OPT_Winit_list_lifetime
,
10562 "returning local %<initializer_list%> variable %qD "
10563 "does not extend the lifetime of the underlying array",
10565 else if (POINTER_TYPE_P (valtype
)
10566 && TREE_CODE (whats_returned
) == LABEL_DECL
)
10567 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
10568 "address of label %qD returned",
10570 else if (POINTER_TYPE_P (valtype
))
10571 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
10572 "address of local variable %qD returned",
10575 inform (DECL_SOURCE_LOCATION (whats_returned
),
10583 /* Returns true if DECL is in the std namespace. */
10586 decl_in_std_namespace_p (tree decl
)
10590 decl
= decl_namespace_context (decl
);
10591 if (DECL_NAMESPACE_STD_P (decl
))
10593 /* Allow inline namespaces inside of std namespace, e.g. with
10594 --enable-symvers=gnu-versioned-namespace std::forward would be
10595 actually std::_8::forward. */
10596 if (!DECL_NAMESPACE_INLINE_P (decl
))
10598 decl
= CP_DECL_CONTEXT (decl
);
10603 /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
10606 is_std_forward_p (tree fn
)
10608 /* std::forward only takes one argument. */
10609 if (call_expr_nargs (fn
) != 1)
10612 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
10613 if (!decl_in_std_namespace_p (fndecl
))
10616 tree name
= DECL_NAME (fndecl
);
10617 return name
&& id_equal (name
, "forward");
10620 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
10623 is_std_move_p (tree fn
)
10625 /* std::move only takes one argument. */
10626 if (call_expr_nargs (fn
) != 1)
10629 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
10630 if (!decl_in_std_namespace_p (fndecl
))
10633 tree name
= DECL_NAME (fndecl
);
10634 return name
&& id_equal (name
, "move");
10637 /* Returns true if RETVAL is a good candidate for the NRVO as per
10638 [class.copy.elision]. FUNCTYPE is the type the function is declared
10642 can_do_nrvo_p (tree retval
, tree functype
)
10644 if (functype
== error_mark_node
)
10647 STRIP_ANY_LOCATION_WRAPPER (retval
);
10648 tree result
= DECL_RESULT (current_function_decl
);
10649 return (retval
!= NULL_TREE
10650 && !processing_template_decl
10651 /* Must be a local, automatic variable. */
10653 && DECL_CONTEXT (retval
) == current_function_decl
10654 && !TREE_STATIC (retval
)
10655 /* And not a lambda or anonymous union proxy. */
10656 && !DECL_HAS_VALUE_EXPR_P (retval
)
10657 && (DECL_ALIGN (retval
) <= DECL_ALIGN (result
))
10658 /* The cv-unqualified type of the returned value must be the
10659 same as the cv-unqualified return type of the
10661 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval
)),
10662 TYPE_MAIN_VARIANT (functype
))
10663 /* And the returned value must be non-volatile. */
10664 && !TYPE_VOLATILE (TREE_TYPE (retval
)));
10667 /* Like can_do_nrvo_p, but we check if we're trying to move a class
10671 can_elide_copy_prvalue_p (tree retval
, tree functype
)
10673 if (functype
== error_mark_node
)
10676 STRIP_ANY_LOCATION_WRAPPER (retval
);
10677 return (retval
!= NULL_TREE
10678 && !glvalue_p (retval
)
10679 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval
)),
10680 TYPE_MAIN_VARIANT (functype
))
10681 && !TYPE_VOLATILE (TREE_TYPE (retval
)));
10684 /* If we should treat RETVAL, an expression being returned, as if it were
10685 designated by an rvalue, returns it adjusted accordingly; otherwise, returns
10686 NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
10687 context (rather than throw). */
10690 treat_lvalue_as_rvalue_p (tree expr
, bool return_p
)
10692 if (cxx_dialect
== cxx98
)
10695 tree retval
= expr
;
10696 STRIP_ANY_LOCATION_WRAPPER (retval
);
10697 if (REFERENCE_REF_P (retval
))
10698 retval
= TREE_OPERAND (retval
, 0);
10700 /* An implicitly movable entity is a variable of automatic storage duration
10701 that is either a non-volatile object or (C++20) an rvalue reference to a
10702 non-volatile object type. */
10703 if (!(((VAR_P (retval
) && !DECL_HAS_VALUE_EXPR_P (retval
))
10704 || TREE_CODE (retval
) == PARM_DECL
)
10705 && !TREE_STATIC (retval
)
10706 && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval
)))
10707 && (TREE_CODE (TREE_TYPE (retval
)) != REFERENCE_TYPE
10708 || (cxx_dialect
>= cxx20
10709 && TYPE_REF_IS_RVALUE (TREE_TYPE (retval
))))))
10712 /* If the expression in a return or co_return statement is a (possibly
10713 parenthesized) id-expression that names an implicitly movable entity
10714 declared in the body or parameter-declaration-clause of the innermost
10715 enclosing function or lambda-expression, */
10716 if (DECL_CONTEXT (retval
) != current_function_decl
)
10720 expr
= move (expr
);
10721 if (expr
== error_mark_node
)
10723 return set_implicit_rvalue_p (expr
);
10726 /* if the operand of a throw-expression is a (possibly parenthesized)
10727 id-expression that names an implicitly movable entity whose scope does not
10728 extend beyond the compound-statement of the innermost try-block or
10729 function-try-block (if any) whose compound-statement or ctor-initializer
10730 encloses the throw-expression, */
10732 /* C++20 added move on throw of parms. */
10733 if (TREE_CODE (retval
) == PARM_DECL
&& cxx_dialect
< cxx20
)
10736 for (cp_binding_level
*b
= current_binding_level
;
10737 ; b
= b
->level_chain
)
10739 for (tree decl
= b
->names
; decl
; decl
= TREE_CHAIN (decl
))
10740 if (decl
== retval
)
10741 return set_implicit_rvalue_p (move (expr
));
10742 if (b
->kind
== sk_function_parms
|| b
->kind
== sk_try
)
10747 /* Warn about dubious usage of std::move (in a return statement, if RETURN_P
10748 is true). EXPR is the std::move expression; TYPE is the type of the object
10749 being initialized. */
10752 maybe_warn_pessimizing_move (tree expr
, tree type
, bool return_p
)
10754 if (!(warn_pessimizing_move
|| warn_redundant_move
))
10757 const location_t loc
= cp_expr_loc_or_input_loc (expr
);
10759 /* C++98 doesn't know move. */
10760 if (cxx_dialect
< cxx11
)
10763 /* Wait until instantiation time, since we can't gauge if we should do
10764 the NRVO until then. */
10765 if (processing_template_decl
)
10768 /* This is only interesting for class types. */
10769 if (!CLASS_TYPE_P (type
))
10772 bool wrapped_p
= false;
10773 /* A a = std::move (A()); */
10774 if (TREE_CODE (expr
) == TREE_LIST
)
10776 if (list_length (expr
) == 1)
10778 expr
= TREE_VALUE (expr
);
10784 /* A a = {std::move (A())};
10785 A a{std::move (A())}; */
10786 else if (TREE_CODE (expr
) == CONSTRUCTOR
)
10788 if (CONSTRUCTOR_NELTS (expr
) == 1)
10790 expr
= CONSTRUCTOR_ELT (expr
, 0)->value
;
10797 /* First, check if this is a call to std::move. */
10798 if (!REFERENCE_REF_P (expr
)
10799 || TREE_CODE (TREE_OPERAND (expr
, 0)) != CALL_EXPR
)
10801 tree fn
= TREE_OPERAND (expr
, 0);
10802 if (!is_std_move_p (fn
))
10804 tree arg
= CALL_EXPR_ARG (fn
, 0);
10805 if (TREE_CODE (arg
) != NOP_EXPR
)
10807 /* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
10808 and implicitly-movable warnings. */
10809 if (TREE_CODE (TREE_OPERAND (arg
, 0)) == ADDR_EXPR
)
10811 arg
= TREE_OPERAND (arg
, 0);
10812 arg
= TREE_OPERAND (arg
, 0);
10813 arg
= convert_from_reference (arg
);
10814 if (can_elide_copy_prvalue_p (arg
, type
))
10816 auto_diagnostic_group d
;
10817 if (warning_at (loc
, OPT_Wpessimizing_move
,
10818 "moving a temporary object prevents copy elision"))
10819 inform (loc
, "remove %<std::move%> call");
10821 /* The rest of the warnings is only relevant for when we are returning
10822 from a function. */
10827 /* Warn if we could do copy elision were it not for the move. */
10828 if (can_do_nrvo_p (arg
, type
))
10830 auto_diagnostic_group d
;
10831 if (!warning_suppressed_p (expr
, OPT_Wpessimizing_move
)
10832 && warning_at (loc
, OPT_Wpessimizing_move
,
10833 "moving a local object in a return statement "
10834 "prevents copy elision"))
10835 inform (loc
, "remove %<std::move%> call");
10837 /* Warn if the move is redundant. It is redundant when we would
10838 do maybe-rvalue overload resolution even without std::move. */
10839 else if (warn_redundant_move
10840 /* This doesn't apply for return {std::move (t)};. */
10842 && !warning_suppressed_p (expr
, OPT_Wredundant_move
)
10843 && (moved
= treat_lvalue_as_rvalue_p (arg
, /*return*/true)))
10845 /* Make sure that overload resolution would actually succeed
10846 if we removed the std::move call. */
10847 tree t
= convert_for_initialization (NULL_TREE
, type
,
10850 | LOOKUP_ONLYCONVERTING
),
10851 ICR_RETURN
, NULL_TREE
, 0,
10853 /* If this worked, implicit rvalue would work, so the call to
10854 std::move is redundant. */
10855 if (t
!= error_mark_node
)
10857 auto_diagnostic_group d
;
10858 if (warning_at (loc
, OPT_Wredundant_move
,
10859 "redundant move in return statement"))
10860 inform (loc
, "remove %<std::move%> call");
10864 /* Also try to warn about redundant std::move in code such as
10867 return std::move(t);
10869 for which EXPR will be something like
10870 *std::move<const T&> ((const struct T &) (const struct T *) t)
10871 and where the std::move does nothing if T does not have a T(const T&&)
10872 constructor, because the argument is const. It will not use T(T&&)
10873 because that would mean losing the const. */
10874 else if (warn_redundant_move
10875 && !warning_suppressed_p (expr
, OPT_Wredundant_move
)
10876 && TYPE_REF_P (TREE_TYPE (arg
))
10877 && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg
))))
10879 tree rtype
= TREE_TYPE (TREE_TYPE (arg
));
10880 if (!same_type_ignoring_top_level_qualifiers_p (rtype
, type
))
10882 /* Check for the unlikely case there's T(const T&&) (we don't care if
10884 for (tree fn
: ovl_range (CLASSTYPE_CONSTRUCTORS (rtype
)))
10885 if (move_fn_p (fn
))
10887 tree t
= TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn
));
10888 if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t
))))
10891 auto_diagnostic_group d
;
10893 ? warning_at (loc
, OPT_Wredundant_move
,
10894 "redundant move in return statement")
10895 : warning_at (loc
, OPT_Wredundant_move
,
10896 "redundant move in initialization"))
10897 inform (loc
, "remove %<std::move%> call");
10901 /* Check that returning RETVAL from the current function is valid.
10902 Return an expression explicitly showing all conversions required to
10903 change RETVAL into the function return type, and to assign it to
10904 the DECL_RESULT for the function. Set *NO_WARNING to true if
10905 code reaches end of non-void function warning shouldn't be issued
10906 on this RETURN_EXPR. */
10909 check_return_expr (tree retval
, bool *no_warning
)
10912 /* The type actually returned by the function. */
10914 /* The type the function is declared to return, or void if
10915 the declared type is incomplete. */
10917 int fn_returns_value_p
;
10918 location_t loc
= cp_expr_loc_or_input_loc (retval
);
10920 *no_warning
= false;
10922 /* A `volatile' function is one that isn't supposed to return, ever.
10923 (This is a G++ extension, used to get better code for functions
10924 that call the `volatile' function.) */
10925 if (TREE_THIS_VOLATILE (current_function_decl
))
10926 warning (0, "function declared %<noreturn%> has a %<return%> statement");
10928 /* Check for various simple errors. */
10929 if (DECL_DESTRUCTOR_P (current_function_decl
))
10932 error_at (loc
, "returning a value from a destructor");
10934 if (targetm
.cxx
.cdtor_returns_this () && !processing_template_decl
)
10935 retval
= current_class_ptr
;
10939 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
10941 if (in_function_try_handler
)
10942 /* If a return statement appears in a handler of the
10943 function-try-block of a constructor, the program is ill-formed. */
10944 error ("cannot return from a handler of a function-try-block of a constructor");
10946 /* You can't return a value from a constructor. */
10947 error_at (loc
, "returning a value from a constructor");
10949 if (targetm
.cxx
.cdtor_returns_this () && !processing_template_decl
)
10950 retval
= current_class_ptr
;
10955 const tree saved_retval
= retval
;
10957 if (processing_template_decl
)
10959 current_function_returns_value
= 1;
10961 if (check_for_bare_parameter_packs (retval
))
10962 return error_mark_node
;
10964 /* If one of the types might be void, we can't tell whether we're
10965 returning a value. */
10966 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl
)))
10967 && !FNDECL_USED_AUTO (current_function_decl
))
10968 || (retval
!= NULL_TREE
10969 && (TREE_TYPE (retval
) == NULL_TREE
10970 || WILDCARD_TYPE_P (TREE_TYPE (retval
)))))
10974 functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
10976 /* Deduce auto return type from a return statement. */
10977 if (FNDECL_USED_AUTO (current_function_decl
))
10979 tree pattern
= DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
);
10983 if (!retval
&& !is_auto (pattern
))
10985 /* Give a helpful error message. */
10986 error ("return-statement with no value, in function returning %qT",
10988 inform (input_location
, "only plain %<auto%> return type can be "
10989 "deduced to %<void%>");
10990 type
= error_mark_node
;
10992 else if (retval
&& BRACE_ENCLOSED_INITIALIZER_P (retval
))
10994 error ("returning initializer list");
10995 type
= error_mark_node
;
11000 retval
= void_node
;
11001 auto_node
= type_uses_auto (pattern
);
11002 type
= do_auto_deduction (pattern
, retval
, auto_node
,
11003 tf_warning_or_error
, adc_return_type
);
11006 if (type
== error_mark_node
)
11008 else if (functype
== pattern
)
11009 apply_deduced_return_type (current_function_decl
, type
);
11010 else if (!same_type_p (type
, functype
))
11012 if (LAMBDA_FUNCTION_P (current_function_decl
))
11013 error_at (loc
, "inconsistent types %qT and %qT deduced for "
11014 "lambda return type", functype
, type
);
11016 error_at (loc
, "inconsistent deduction for auto return type: "
11017 "%qT and then %qT", functype
, type
);
11022 result
= DECL_RESULT (current_function_decl
);
11023 valtype
= TREE_TYPE (result
);
11024 gcc_assert (valtype
!= NULL_TREE
);
11025 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
11027 /* Check for a return statement with no return value in a function
11028 that's supposed to return a value. */
11029 if (!retval
&& fn_returns_value_p
)
11031 if (functype
!= error_mark_node
)
11032 permerror (input_location
, "return-statement with no value, in "
11033 "function returning %qT", valtype
);
11034 /* Remember that this function did return. */
11035 current_function_returns_value
= 1;
11036 /* And signal caller that TREE_NO_WARNING should be set on the
11037 RETURN_EXPR to avoid control reaches end of non-void function
11038 warnings in tree-cfg.cc. */
11039 *no_warning
= true;
11041 /* Check for a return statement with a value in a function that
11042 isn't supposed to return a value. */
11043 else if (retval
&& !fn_returns_value_p
)
11045 if (VOID_TYPE_P (TREE_TYPE (retval
)))
11046 /* You can return a `void' value from a function of `void'
11047 type. In that case, we have to evaluate the expression for
11048 its side-effects. */
11049 finish_expr_stmt (retval
);
11050 else if (retval
!= error_mark_node
)
11051 permerror (loc
, "return-statement with a value, in function "
11052 "returning %qT", valtype
);
11053 current_function_returns_null
= 1;
11055 /* There's really no value to return, after all. */
11059 /* Remember that this function can sometimes return without a
11061 current_function_returns_null
= 1;
11063 /* Remember that this function did return a value. */
11064 current_function_returns_value
= 1;
11066 /* Check for erroneous operands -- but after giving ourselves a
11067 chance to provide an error about returning a value from a void
11069 if (error_operand_p (retval
))
11071 current_function_return_value
= error_mark_node
;
11072 return error_mark_node
;
11075 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
11076 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl
))
11077 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
11078 && ! flag_check_new
11079 && retval
&& null_ptr_cst_p (retval
))
11080 warning (0, "%<operator new%> must not return NULL unless it is "
11081 "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
11083 /* Effective C++ rule 15. See also start_function. */
11085 && DECL_NAME (current_function_decl
) == assign_op_identifier
11086 && !type_dependent_expression_p (retval
))
11090 /* The function return type must be a reference to the current
11092 if (TYPE_REF_P (valtype
)
11093 && same_type_ignoring_top_level_qualifiers_p
11094 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
)))
11096 /* Returning '*this' is obviously OK. */
11097 if (retval
== current_class_ref
)
11099 /* If we are calling a function whose return type is the same of
11100 the current class reference, it is ok. */
11101 else if (INDIRECT_REF_P (retval
)
11102 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
11107 warning_at (loc
, OPT_Weffc__
,
11108 "%<operator=%> should return a reference to %<*this%>");
11111 if (dependent_type_p (functype
)
11112 || type_dependent_expression_p (retval
))
11115 /* We should not have changed the return value. */
11116 gcc_assert (retval
== saved_retval
);
11117 /* We don't know if this is an lvalue or rvalue use, but
11118 either way we can mark it as read. */
11119 mark_exp_read (retval
);
11123 /* The fabled Named Return Value optimization, as per [class.copy]/15:
11125 [...] For a function with a class return type, if the expression
11126 in the return statement is the name of a local object, and the cv-
11127 unqualified type of the local object is the same as the function
11128 return type, an implementation is permitted to omit creating the tem-
11129 porary object to hold the function return value [...]
11131 So, if this is a value-returning function that always returns the same
11132 local variable, remember it.
11134 It might be nice to be more flexible, and choose the first suitable
11135 variable even if the function sometimes returns something else, but
11136 then we run the risk of clobbering the variable we chose if the other
11137 returned expression uses the chosen variable somehow. And people expect
11138 this restriction, anyway. (jason 2000-11-19)
11140 See finish_function and finalize_nrv for the rest of this optimization. */
11141 tree bare_retval
= NULL_TREE
;
11144 retval
= maybe_undo_parenthesized_ref (retval
);
11145 bare_retval
= tree_strip_any_location_wrapper (retval
);
11148 bool named_return_value_okay_p
= can_do_nrvo_p (bare_retval
, functype
);
11149 if (fn_returns_value_p
&& flag_elide_constructors
)
11151 if (named_return_value_okay_p
11152 && (current_function_return_value
== NULL_TREE
11153 || current_function_return_value
== bare_retval
))
11154 current_function_return_value
= bare_retval
;
11156 current_function_return_value
= error_mark_node
;
11159 /* We don't need to do any conversions when there's nothing being
11164 if (!named_return_value_okay_p
)
11165 maybe_warn_pessimizing_move (retval
, functype
, /*return_p*/true);
11167 /* Do any required conversions. */
11168 if (bare_retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
11169 /* No conversions are required. */
11173 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
11175 /* The functype's return type will have been set to void, if it
11176 was an incomplete type. Just treat this as 'return;' */
11177 if (VOID_TYPE_P (functype
))
11178 return error_mark_node
;
11180 if (processing_template_decl
)
11181 retval
= build_non_dependent_expr (retval
);
11183 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
11184 treated as an rvalue for the purposes of overload resolution to
11185 favor move constructors over copy constructors.
11187 Note that these conditions are similar to, but not as strict as,
11188 the conditions for the named return value optimization. */
11189 bool converted
= false;
11191 /* Until C++23, this was only interesting for class type, but in C++23,
11192 we should do the below when we're converting rom/to a class/reference
11193 (a non-scalar type). */
11194 if ((cxx_dialect
< cxx23
11195 ? CLASS_TYPE_P (functype
)
11196 : !SCALAR_TYPE_P (functype
) || !SCALAR_TYPE_P (TREE_TYPE (retval
)))
11197 && (moved
= treat_lvalue_as_rvalue_p (retval
, /*return*/true)))
11198 /* In C++20 and earlier we treat the return value as an rvalue
11199 that can bind to lvalue refs. In C++23, such an expression is just
11200 an xvalue (see reference_binding). */
11203 /* The call in a (lambda) thunk needs no conversions. */
11204 if (TREE_CODE (retval
) == CALL_EXPR
11205 && call_from_lambda_thunk_p (retval
))
11208 /* First convert the value to the function's return type, then
11209 to the type of return value's location to handle the
11210 case that functype is smaller than the valtype. */
11212 retval
= convert_for_initialization
11213 (NULL_TREE
, functype
, retval
, flags
, ICR_RETURN
, NULL_TREE
, 0,
11214 tf_warning_or_error
);
11215 retval
= convert (valtype
, retval
);
11217 /* If the conversion failed, treat this just like `return;'. */
11218 if (retval
== error_mark_node
)
11220 /* We can't initialize a register from a AGGR_INIT_EXPR. */
11221 else if (! cfun
->returns_struct
11222 && TREE_CODE (retval
) == TARGET_EXPR
11223 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
11224 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
11225 TREE_OPERAND (retval
, 0));
11226 else if (!processing_template_decl
11227 && maybe_warn_about_returning_address_of_local (retval
, loc
)
11228 && INDIRECT_TYPE_P (valtype
))
11229 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
11230 build_zero_cst (TREE_TYPE (retval
)));
11233 if (processing_template_decl
)
11234 return saved_retval
;
11236 /* A naive attempt to reduce the number of -Wdangling-reference false
11237 positives: if we know that this function can return a variable with
11238 static storage duration rather than one of its parameters, suppress
11240 if (warn_dangling_reference
11241 && TYPE_REF_P (functype
)
11243 && VAR_P (bare_retval
)
11244 && TREE_STATIC (bare_retval
))
11245 suppress_warning (current_function_decl
, OPT_Wdangling_reference
);
11247 /* Actually copy the value returned into the appropriate location. */
11248 if (retval
&& retval
!= result
)
11250 /* If there's a postcondition for a scalar return value, wrap
11251 retval in a call to the postcondition function. */
11252 if (tree post
= apply_postcondition_to_return (retval
))
11254 retval
= cp_build_init_expr (result
, retval
);
11257 if (tree set
= maybe_set_retval_sentinel ())
11258 retval
= build2 (COMPOUND_EXPR
, void_type_node
, retval
, set
);
11260 /* If there's a postcondition for an aggregate return value, call the
11261 postcondition function after the return object is initialized. */
11262 if (tree post
= apply_postcondition_to_return (result
))
11263 retval
= build2 (COMPOUND_EXPR
, void_type_node
, retval
, post
);
11269 /* Returns nonzero if the pointer-type FROM can be converted to the
11270 pointer-type TO via a qualification conversion. If CONSTP is -1,
11271 then we return nonzero if the pointers are similar, and the
11272 cv-qualification signature of FROM is a proper subset of that of TO.
11274 If CONSTP is positive, then all outer pointers have been
11275 const-qualified. */
11278 comp_ptr_ttypes_real (tree to
, tree from
, int constp
)
11280 bool to_more_cv_qualified
= false;
11281 bool is_opaque_pointer
= false;
11283 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
11285 if (TREE_CODE (to
) != TREE_CODE (from
))
11288 if (TREE_CODE (from
) == OFFSET_TYPE
11289 && !same_type_p (TYPE_OFFSET_BASETYPE (from
),
11290 TYPE_OFFSET_BASETYPE (to
)))
11293 /* Const and volatile mean something different for function and
11294 array types, so the usual checks are not appropriate. We'll
11295 check the array type elements in further iterations. */
11296 if (!FUNC_OR_METHOD_TYPE_P (to
) && TREE_CODE (to
) != ARRAY_TYPE
)
11298 if (!at_least_as_qualified_p (to
, from
))
11301 if (!at_least_as_qualified_p (from
, to
))
11305 to_more_cv_qualified
= true;
11309 constp
&= TYPE_READONLY (to
);
11312 if (VECTOR_TYPE_P (to
))
11313 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
11315 /* P0388R4 allows a conversion from int[N] to int[] but not the
11316 other way round. When both arrays have bounds but they do
11317 not match, then no conversion is possible. */
11318 if (TREE_CODE (to
) == ARRAY_TYPE
11319 && !comp_array_types (to
, from
, bounds_first
, /*strict=*/false))
11322 if (!TYPE_PTR_P (to
)
11323 && !TYPE_PTRDATAMEM_P (to
)
11324 /* CWG 330 says we need to look through arrays. */
11325 && TREE_CODE (to
) != ARRAY_TYPE
)
11326 return ((constp
>= 0 || to_more_cv_qualified
)
11327 && (is_opaque_pointer
11328 || same_type_ignoring_top_level_qualifiers_p (to
, from
)));
11332 /* When comparing, say, char ** to char const **, this function takes
11333 the 'char *' and 'char const *'. Do not pass non-pointer/reference
11334 types to this function. */
11337 comp_ptr_ttypes (tree to
, tree from
)
11339 return comp_ptr_ttypes_real (to
, from
, 1);
11342 /* Returns true iff FNTYPE is a non-class type that involves
11343 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
11344 if a parameter type is ill-formed. */
11347 error_type_p (const_tree type
)
11351 switch (TREE_CODE (type
))
11357 case REFERENCE_TYPE
:
11359 return error_type_p (TREE_TYPE (type
));
11361 case FUNCTION_TYPE
:
11363 if (error_type_p (TREE_TYPE (type
)))
11365 for (t
= TYPE_ARG_TYPES (type
); t
; t
= TREE_CHAIN (t
))
11366 if (error_type_p (TREE_VALUE (t
)))
11371 if (TYPE_PTRMEMFUNC_P (type
))
11372 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type
));
11380 /* Returns true if to and from are (possibly multi-level) pointers to the same
11381 type or inheritance-related types, regardless of cv-quals. */
11384 ptr_reasonably_similar (const_tree to
, const_tree from
)
11386 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
11388 /* Any target type is similar enough to void. */
11389 if (VOID_TYPE_P (to
))
11390 return !error_type_p (from
);
11391 if (VOID_TYPE_P (from
))
11392 return !error_type_p (to
);
11394 if (TREE_CODE (to
) != TREE_CODE (from
))
11397 if (TREE_CODE (from
) == OFFSET_TYPE
11398 && comptypes (TYPE_OFFSET_BASETYPE (to
),
11399 TYPE_OFFSET_BASETYPE (from
),
11400 COMPARE_BASE
| COMPARE_DERIVED
))
11403 if (VECTOR_TYPE_P (to
)
11404 && vector_types_convertible_p (to
, from
, false))
11407 if (TREE_CODE (to
) == INTEGER_TYPE
11408 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
11411 if (TREE_CODE (to
) == FUNCTION_TYPE
)
11412 return !error_type_p (to
) && !error_type_p (from
);
11414 if (!TYPE_PTR_P (to
))
11416 /* When either type is incomplete avoid DERIVED_FROM_P,
11417 which may call complete_type (c++/57942). */
11418 bool b
= !COMPLETE_TYPE_P (to
) || !COMPLETE_TYPE_P (from
);
11420 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
11421 b
? COMPARE_STRICT
: COMPARE_BASE
| COMPARE_DERIVED
);
11426 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
11427 pointer-to-member types) are the same, ignoring cv-qualification at
11428 all levels. CB says how we should behave when comparing array bounds. */
11431 comp_ptr_ttypes_const (tree to
, tree from
, compare_bounds_t cb
)
11433 bool is_opaque_pointer
= false;
11435 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
11437 if (TREE_CODE (to
) != TREE_CODE (from
))
11440 if (TREE_CODE (from
) == OFFSET_TYPE
11441 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
11442 TYPE_OFFSET_BASETYPE (to
)))
11445 if (VECTOR_TYPE_P (to
))
11446 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
11448 if (TREE_CODE (to
) == ARRAY_TYPE
11449 /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
11451 && !comp_array_types (to
, from
, cb
, /*strict=*/false))
11454 /* CWG 330 says we need to look through arrays. */
11455 if (!TYPE_PTR_P (to
) && TREE_CODE (to
) != ARRAY_TYPE
)
11456 return (is_opaque_pointer
11457 || same_type_ignoring_top_level_qualifiers_p (to
, from
));
11461 /* Returns the type qualifiers for this type, including the qualifiers on the
11462 elements for an array type. */
11465 cp_type_quals (const_tree type
)
11468 /* This CONST_CAST is okay because strip_array_types returns its
11469 argument unmodified and we assign it to a const_tree. */
11470 type
= strip_array_types (CONST_CAST_TREE (type
));
11471 if (type
== error_mark_node
11472 /* Quals on a FUNCTION_TYPE are memfn quals. */
11473 || TREE_CODE (type
) == FUNCTION_TYPE
)
11474 return TYPE_UNQUALIFIED
;
11475 quals
= TYPE_QUALS (type
);
11476 /* METHOD and REFERENCE_TYPEs should never have quals. */
11477 gcc_assert ((TREE_CODE (type
) != METHOD_TYPE
11478 && !TYPE_REF_P (type
))
11479 || ((quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
))
11480 == TYPE_UNQUALIFIED
));
11484 /* Returns the function-ref-qualifier for TYPE */
11487 type_memfn_rqual (const_tree type
)
11489 gcc_assert (FUNC_OR_METHOD_TYPE_P (type
));
11491 if (!FUNCTION_REF_QUALIFIED (type
))
11492 return REF_QUAL_NONE
;
11493 else if (FUNCTION_RVALUE_QUALIFIED (type
))
11494 return REF_QUAL_RVALUE
;
11496 return REF_QUAL_LVALUE
;
11499 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
11503 type_memfn_quals (const_tree type
)
11505 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11506 return TYPE_QUALS (type
);
11507 else if (TREE_CODE (type
) == METHOD_TYPE
)
11508 return cp_type_quals (class_of_this_parm (type
));
11510 gcc_unreachable ();
11513 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
11514 MEMFN_QUALS and its ref-qualifier to RQUAL. */
11517 apply_memfn_quals (tree type
, cp_cv_quals memfn_quals
, cp_ref_qualifier rqual
)
11519 /* Could handle METHOD_TYPE here if necessary. */
11520 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
11521 if (TYPE_QUALS (type
) == memfn_quals
11522 && type_memfn_rqual (type
) == rqual
)
11525 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
11527 tree result
= build_qualified_type (type
, memfn_quals
);
11528 return build_ref_qualified_type (result
, rqual
);
11531 /* Returns nonzero if TYPE is const or volatile. */
11534 cv_qualified_p (const_tree type
)
11536 int quals
= cp_type_quals (type
);
11537 return (quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)) != 0;
11540 /* Returns nonzero if the TYPE contains a mutable member. */
11543 cp_has_mutable_p (const_tree type
)
11545 /* This CONST_CAST is okay because strip_array_types returns its
11546 argument unmodified and we assign it to a const_tree. */
11547 type
= strip_array_types (CONST_CAST_TREE(type
));
11549 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
11552 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
11553 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
11554 approximation. In particular, consider:
11557 struct S { int i; };
11558 const S s = { f(); }
11560 Here, we will make "s" as TREE_READONLY (because it is declared
11561 "const") -- only to reverse ourselves upon seeing that the
11562 initializer is non-constant. */
11565 cp_apply_type_quals_to_decl (int type_quals
, tree decl
)
11567 tree type
= TREE_TYPE (decl
);
11569 if (type
== error_mark_node
)
11572 if (TREE_CODE (decl
) == TYPE_DECL
)
11575 gcc_assert (!(TREE_CODE (type
) == FUNCTION_TYPE
11576 && type_quals
!= TYPE_UNQUALIFIED
));
11578 /* Avoid setting TREE_READONLY incorrectly. */
11579 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
11580 constructor can produce constant init, so rely on cp_finish_decl to
11581 clear TREE_READONLY if the variable has non-constant init. */
11583 /* If the type has (or might have) a mutable component, that component
11584 might be modified. */
11585 if (TYPE_HAS_MUTABLE_P (type
) || !COMPLETE_TYPE_P (type
))
11586 type_quals
&= ~TYPE_QUAL_CONST
;
11588 c_apply_type_quals_to_decl (type_quals
, decl
);
11591 /* Subroutine of casts_away_constness. Make T1 and T2 point at
11592 exemplar types such that casting T1 to T2 is casting away constness
11593 if and only if there is no implicit conversion from T1 to T2. */
11596 casts_away_constness_r (tree
*t1
, tree
*t2
, tsubst_flags_t complain
)
11601 /* [expr.const.cast]
11603 For multi-level pointer to members and multi-level mixed pointers
11604 and pointers to members (conv.qual), the "member" aspect of a
11605 pointer to member level is ignored when determining if a const
11606 cv-qualifier has been cast away. */
11607 /* [expr.const.cast]
11609 For two pointer types:
11611 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
11612 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
11615 casting from X1 to X2 casts away constness if, for a non-pointer
11616 type T there does not exist an implicit conversion (clause
11619 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
11623 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
11624 if ((!TYPE_PTR_P (*t1
) && !TYPE_PTRDATAMEM_P (*t1
))
11625 || (!TYPE_PTR_P (*t2
) && !TYPE_PTRDATAMEM_P (*t2
)))
11627 *t1
= cp_build_qualified_type (void_type_node
,
11628 cp_type_quals (*t1
));
11629 *t2
= cp_build_qualified_type (void_type_node
,
11630 cp_type_quals (*t2
));
11634 quals1
= cp_type_quals (*t1
);
11635 quals2
= cp_type_quals (*t2
);
11637 if (TYPE_PTRDATAMEM_P (*t1
))
11638 *t1
= TYPE_PTRMEM_POINTED_TO_TYPE (*t1
);
11640 *t1
= TREE_TYPE (*t1
);
11641 if (TYPE_PTRDATAMEM_P (*t2
))
11642 *t2
= TYPE_PTRMEM_POINTED_TO_TYPE (*t2
);
11644 *t2
= TREE_TYPE (*t2
);
11646 casts_away_constness_r (t1
, t2
, complain
);
11647 *t1
= build_pointer_type (*t1
);
11648 *t2
= build_pointer_type (*t2
);
11649 *t1
= cp_build_qualified_type (*t1
, quals1
);
11650 *t2
= cp_build_qualified_type (*t2
, quals2
);
11653 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
11656 ??? This function returns non-zero if casting away qualifiers not
11657 just const. We would like to return to the caller exactly which
11658 qualifiers are casted away to give more accurate diagnostics.
11662 casts_away_constness (tree t1
, tree t2
, tsubst_flags_t complain
)
11664 if (TYPE_REF_P (t2
))
11666 /* [expr.const.cast]
11668 Casting from an lvalue of type T1 to an lvalue of type T2
11669 using a reference cast casts away constness if a cast from an
11670 rvalue of type "pointer to T1" to the type "pointer to T2"
11671 casts away constness. */
11672 t1
= (TYPE_REF_P (t1
) ? TREE_TYPE (t1
) : t1
);
11673 return casts_away_constness (build_pointer_type (t1
),
11674 build_pointer_type (TREE_TYPE (t2
)),
11678 if (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
11679 /* [expr.const.cast]
11681 Casting from an rvalue of type "pointer to data member of X
11682 of type T1" to the type "pointer to data member of Y of type
11683 T2" casts away constness if a cast from an rvalue of type
11684 "pointer to T1" to the type "pointer to T2" casts away
11686 return casts_away_constness
11687 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1
)),
11688 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2
)),
11691 /* Casting away constness is only something that makes sense for
11692 pointer or reference types. */
11693 if (!TYPE_PTR_P (t1
) || !TYPE_PTR_P (t2
))
11696 /* Top-level qualifiers don't matter. */
11697 t1
= TYPE_MAIN_VARIANT (t1
);
11698 t2
= TYPE_MAIN_VARIANT (t2
);
11699 casts_away_constness_r (&t1
, &t2
, complain
);
11700 if (!can_convert (t2
, t1
, complain
))
11706 /* If T is a REFERENCE_TYPE return the type to which T refers.
11707 Otherwise, return T itself. */
11710 non_reference (tree t
)
11712 if (t
&& TYPE_REF_P (t
))
11718 /* Return nonzero if REF is an lvalue valid for this language;
11719 otherwise, print an error message and return zero. USE says
11720 how the lvalue is being used and so selects the error message. */
11723 lvalue_or_else (tree ref
, enum lvalue_use use
, tsubst_flags_t complain
)
11725 cp_lvalue_kind kind
= lvalue_kind (ref
);
11727 if (kind
== clk_none
)
11729 if (complain
& tf_error
)
11730 lvalue_error (cp_expr_loc_or_input_loc (ref
), use
);
11733 else if (kind
& (clk_rvalueref
|clk_class
))
11735 if (!(complain
& tf_error
))
11737 /* Make this a permerror because we used to accept it. */
11738 permerror (cp_expr_loc_or_input_loc (ref
),
11739 "using rvalue as lvalue");
11744 /* Return true if a user-defined literal operator is a raw operator. */
11747 check_raw_literal_operator (const_tree decl
)
11749 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
11752 bool maybe_raw_p
= false;
11754 /* Count the number and type of arguments and check for ellipsis. */
11755 for (argtype
= argtypes
, arity
= 0;
11756 argtype
&& argtype
!= void_list_node
;
11757 ++arity
, argtype
= TREE_CHAIN (argtype
))
11759 tree t
= TREE_VALUE (argtype
);
11761 if (same_type_p (t
, const_string_type_node
))
11762 maybe_raw_p
= true;
11765 return false; /* Found ellipsis. */
11767 if (!maybe_raw_p
|| arity
!= 1)
11774 /* Return true if a user-defined literal operator has one of the allowed
11778 check_literal_operator_args (const_tree decl
,
11779 bool *long_long_unsigned_p
, bool *long_double_p
)
11781 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
11783 *long_long_unsigned_p
= false;
11784 *long_double_p
= false;
11785 if (processing_template_decl
|| processing_specialization
)
11786 return argtypes
== void_list_node
;
11793 /* Count the number and type of arguments and check for ellipsis. */
11794 for (argtype
= argtypes
, arity
= 0;
11795 argtype
&& argtype
!= void_list_node
;
11796 argtype
= TREE_CHAIN (argtype
))
11798 tree t
= TREE_VALUE (argtype
);
11801 if (TYPE_PTR_P (t
))
11803 bool maybe_raw_p
= false;
11805 if (cp_type_quals (t
) != TYPE_QUAL_CONST
)
11807 t
= TYPE_MAIN_VARIANT (t
);
11808 if ((maybe_raw_p
= same_type_p (t
, char_type_node
))
11809 || same_type_p (t
, wchar_type_node
)
11810 || same_type_p (t
, char8_type_node
)
11811 || same_type_p (t
, char16_type_node
)
11812 || same_type_p (t
, char32_type_node
))
11814 argtype
= TREE_CHAIN (argtype
);
11817 t
= TREE_VALUE (argtype
);
11818 if (maybe_raw_p
&& argtype
== void_list_node
)
11820 else if (same_type_p (t
, size_type_node
))
11829 else if (same_type_p (t
, long_long_unsigned_type_node
))
11832 *long_long_unsigned_p
= true;
11834 else if (same_type_p (t
, long_double_type_node
))
11837 *long_double_p
= true;
11839 else if (same_type_p (t
, char_type_node
))
11841 else if (same_type_p (t
, wchar_type_node
))
11843 else if (same_type_p (t
, char8_type_node
))
11845 else if (same_type_p (t
, char16_type_node
))
11847 else if (same_type_p (t
, char32_type_node
))
11853 return false; /* Found ellipsis. */
11855 if (arity
!= max_arity
)
11862 /* Always returns false since unlike C90, C++ has no concept of implicit
11863 function declarations. */
11866 c_decl_implicit (const_tree
)