1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2022 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 (extended2
&& !extended1
)
298 int ret
= cp_compare_floating_point_conversion_ranks (t2
, t1
);
299 return ret
== 3 ? 3 : -ret
;
302 const struct real_format
*fmt1
= REAL_MODE_FORMAT (TYPE_MODE (t1
));
303 const struct real_format
*fmt2
= REAL_MODE_FORMAT (TYPE_MODE (t2
));
304 gcc_assert (fmt1
->b
== 2 && fmt2
->b
== 2);
305 /* For {ibm,mips}_extended_format formats, the type has variable
306 precision up to ~2150 bits when the first double is around maximum
307 representable double and second double is subnormal minimum.
308 So, e.g. for __ibm128 vs. std::float128_t, they have unordered
310 int p1
= (MODE_COMPOSITE_P (TYPE_MODE (t1
))
311 ? fmt1
->emax
- fmt1
->emin
+ fmt1
->p
- 1 : fmt1
->p
);
312 int p2
= (MODE_COMPOSITE_P (TYPE_MODE (t2
))
313 ? fmt2
->emax
- fmt2
->emin
+ fmt2
->p
- 1 : fmt2
->p
);
314 /* The rank of a floating point type T is greater than the rank of
315 any floating-point type whose set of values is a proper subset
316 of the set of values of T. */
317 if ((p1
> p2
&& fmt1
->emax
>= fmt2
->emax
)
318 || (p1
== p2
&& fmt1
->emax
> fmt2
->emax
))
320 if ((p1
< p2
&& fmt1
->emax
<= fmt2
->emax
)
321 || (p1
== p2
&& fmt1
->emax
< fmt2
->emax
))
323 if ((p1
> p2
&& fmt1
->emax
< fmt2
->emax
)
324 || (p1
< p2
&& fmt1
->emax
> fmt2
->emax
))
326 if (!extended1
&& !extended2
)
328 /* The rank of long double is greater than the rank of double, which
329 is greater than the rank of float. */
330 if (t1
== long_double_type_node
)
332 else if (t2
== long_double_type_node
)
334 if (t1
== double_type_node
)
336 else if (t2
== double_type_node
)
338 if (t1
== float_type_node
)
340 else if (t2
== float_type_node
)
344 /* Two extended floating-point types with the same set of values have equal
346 if (extended1
&& extended2
)
348 if ((extended1
<= NUM_FLOATN_TYPES
) == (extended2
<= NUM_FLOATN_TYPES
))
350 /* Prefer higher extendedN value. */
351 if (extended1
> extended2
)
353 else if (extended1
< extended2
)
358 else if (extended1
<= NUM_FLOATN_TYPES
)
359 /* Prefer _FloatN type over _FloatMx type. */
361 else if (extended2
<= NUM_FLOATN_TYPES
)
367 /* gcc_assert (extended1 && !extended2); */
370 for (p
= &float_type_node
; p
<= &long_double_type_node
; ++p
)
372 const struct real_format
*fmt3
= REAL_MODE_FORMAT (TYPE_MODE (*p
));
373 gcc_assert (fmt3
->b
== 2);
374 int p3
= (MODE_COMPOSITE_P (TYPE_MODE (*p
))
375 ? fmt3
->emax
- fmt3
->emin
+ fmt3
->p
- 1 : fmt3
->p
);
376 if (p1
== p3
&& fmt1
->emax
== fmt3
->emax
)
379 /* An extended floating-point type with the same set of values
380 as exactly one cv-unqualified standard floating-point type
381 has a rank equal to the rank of that standard floating-point
384 An extended floating-point type with the same set of values
385 as more than one cv-unqualified standard floating-point type
386 has a rank equal to the rank of double.
388 Thus, if the latter is true and t2 is long double, t2
390 if (cnt
> 1 && mv2
== long_double_type_node
)
392 /* Otherwise, they have equal rank, but extended types
393 (other than std::bfloat16_t) have higher subrank. */
397 /* Return the common type for two arithmetic types T1 and T2 under the
398 usual arithmetic conversions. The default conversions have already
399 been applied, and enumerated types converted to their compatible
403 cp_common_type (tree t1
, tree t2
)
405 enum tree_code code1
= TREE_CODE (t1
);
406 enum tree_code code2
= TREE_CODE (t2
);
411 /* In what follows, we slightly generalize the rules given in [expr] so
412 as to deal with `long long' and `complex'. First, merge the
414 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
416 if (SCOPED_ENUM_P (t1
) || SCOPED_ENUM_P (t2
))
418 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
419 return build_type_attribute_variant (t1
, attributes
);
424 /* FIXME: Attributes. */
425 gcc_assert (ARITHMETIC_TYPE_P (t1
)
426 || VECTOR_TYPE_P (t1
)
427 || UNSCOPED_ENUM_P (t1
));
428 gcc_assert (ARITHMETIC_TYPE_P (t2
)
429 || VECTOR_TYPE_P (t2
)
430 || UNSCOPED_ENUM_P (t2
));
432 /* If one type is complex, form the common type of the non-complex
433 components, then make that complex. Use T1 or T2 if it is the
435 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
437 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
438 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
440 = type_after_usual_arithmetic_conversions (subtype1
, subtype2
);
442 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
443 return build_type_attribute_variant (t1
, attributes
);
444 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
445 return build_type_attribute_variant (t2
, attributes
);
447 return build_type_attribute_variant (build_complex_type (subtype
),
451 if (code1
== VECTOR_TYPE
)
453 /* When we get here we should have two vectors of the same size.
454 Just prefer the unsigned one if present. */
455 if (TYPE_UNSIGNED (t1
))
456 return merge_type_attributes_from (t1
, t2
);
458 return merge_type_attributes_from (t2
, t1
);
461 /* If only one is real, use it as the result. */
462 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
463 return build_type_attribute_variant (t1
, attributes
);
464 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
465 return build_type_attribute_variant (t2
, attributes
);
467 if (code1
== REAL_TYPE
468 && (extended_float_type_p (t1
) || extended_float_type_p (t2
)))
470 tree mv1
= TYPE_MAIN_VARIANT (t1
);
471 tree mv2
= TYPE_MAIN_VARIANT (t2
);
473 return build_type_attribute_variant (t1
, attributes
);
475 int cmpret
= cp_compare_floating_point_conversion_ranks (mv1
, mv2
);
477 return error_mark_node
;
478 else if (cmpret
>= 0)
479 return build_type_attribute_variant (t1
, attributes
);
481 return build_type_attribute_variant (t2
, attributes
);
484 /* Both real or both integers; use the one with greater precision. */
485 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
486 return build_type_attribute_variant (t1
, attributes
);
487 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
488 return build_type_attribute_variant (t2
, attributes
);
490 /* The types are the same; no need to do anything fancy. */
491 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
492 return build_type_attribute_variant (t1
, attributes
);
494 if (code1
!= REAL_TYPE
)
496 /* If one is unsigned long long, then convert the other to unsigned
498 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_unsigned_type_node
)
499 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_unsigned_type_node
))
500 return build_type_attribute_variant (long_long_unsigned_type_node
,
502 /* If one is a long long, and the other is an unsigned long, and
503 long long can represent all the values of an unsigned long, then
504 convert to a long long. Otherwise, convert to an unsigned long
505 long. Otherwise, if either operand is long long, convert the
508 Since we're here, we know the TYPE_PRECISION is the same;
509 therefore converting to long long cannot represent all the values
510 of an unsigned long, so we choose unsigned long long in that
512 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_integer_type_node
)
513 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_integer_type_node
))
515 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
516 ? long_long_unsigned_type_node
517 : long_long_integer_type_node
);
518 return build_type_attribute_variant (t
, attributes
);
521 /* Go through the same procedure, but for longs. */
522 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_unsigned_type_node
)
523 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_unsigned_type_node
))
524 return build_type_attribute_variant (long_unsigned_type_node
,
526 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_integer_type_node
)
527 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_integer_type_node
))
529 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
530 ? long_unsigned_type_node
: long_integer_type_node
);
531 return build_type_attribute_variant (t
, attributes
);
534 /* For __intN types, either the type is __int128 (and is lower
535 priority than the types checked above, but higher than other
536 128-bit types) or it's known to not be the same size as other
537 types (enforced in toplev.cc). Prefer the unsigned type. */
538 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
540 if (int_n_enabled_p
[i
]
541 && (same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].signed_type
)
542 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].signed_type
)
543 || same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].unsigned_type
)
544 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].unsigned_type
)))
546 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
547 ? int_n_trees
[i
].unsigned_type
548 : int_n_trees
[i
].signed_type
);
549 return build_type_attribute_variant (t
, attributes
);
553 /* Otherwise prefer the unsigned one. */
554 if (TYPE_UNSIGNED (t1
))
555 return build_type_attribute_variant (t1
, attributes
);
557 return build_type_attribute_variant (t2
, attributes
);
561 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_double_type_node
)
562 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_double_type_node
))
563 return build_type_attribute_variant (long_double_type_node
,
565 if (same_type_p (TYPE_MAIN_VARIANT (t1
), double_type_node
)
566 || same_type_p (TYPE_MAIN_VARIANT (t2
), double_type_node
))
567 return build_type_attribute_variant (double_type_node
,
569 if (same_type_p (TYPE_MAIN_VARIANT (t1
), float_type_node
)
570 || same_type_p (TYPE_MAIN_VARIANT (t2
), float_type_node
))
571 return build_type_attribute_variant (float_type_node
,
574 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
575 the standard C++ floating-point types. Logic earlier in this
576 function has already eliminated the possibility that
577 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
578 compelling reason to choose one or the other. */
579 return build_type_attribute_variant (t1
, attributes
);
583 /* T1 and T2 are arithmetic or enumeration types. Return the type
584 that will result from the "usual arithmetic conversions" on T1 and
585 T2 as described in [expr]. */
588 type_after_usual_arithmetic_conversions (tree t1
, tree t2
)
590 gcc_assert (ARITHMETIC_TYPE_P (t1
)
591 || VECTOR_TYPE_P (t1
)
592 || UNSCOPED_ENUM_P (t1
));
593 gcc_assert (ARITHMETIC_TYPE_P (t2
)
594 || VECTOR_TYPE_P (t2
)
595 || UNSCOPED_ENUM_P (t2
));
597 /* Perform the integral promotions. We do not promote real types here. */
598 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1
)
599 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2
))
601 t1
= type_promotes_to (t1
);
602 t2
= type_promotes_to (t2
);
605 return cp_common_type (t1
, t2
);
609 composite_pointer_error (const op_location_t
&location
,
610 diagnostic_t kind
, tree t1
, tree t2
,
611 composite_pointer_operation operation
)
616 emit_diagnostic (kind
, location
, 0,
617 "comparison between "
618 "distinct pointer types %qT and %qT lacks a cast",
622 emit_diagnostic (kind
, location
, 0,
623 "conversion between "
624 "distinct pointer types %qT and %qT lacks a cast",
627 case CPO_CONDITIONAL_EXPR
:
628 emit_diagnostic (kind
, location
, 0,
629 "conditional expression between "
630 "distinct pointer types %qT and %qT lacks a cast",
638 /* Subroutine of composite_pointer_type to implement the recursive
639 case. See that function for documentation of the parameters. And ADD_CONST
640 is used to track adding "const" where needed. */
643 composite_pointer_type_r (const op_location_t
&location
,
644 tree t1
, tree t2
, bool *add_const
,
645 composite_pointer_operation operation
,
646 tsubst_flags_t complain
)
653 /* Determine the types pointed to by T1 and T2. */
656 pointee1
= TREE_TYPE (t1
);
657 pointee2
= TREE_TYPE (t2
);
661 pointee1
= TYPE_PTRMEM_POINTED_TO_TYPE (t1
);
662 pointee2
= TYPE_PTRMEM_POINTED_TO_TYPE (t2
);
667 If T1 and T2 are similar types, the result is the cv-combined type of
669 if (same_type_ignoring_top_level_qualifiers_p (pointee1
, pointee2
))
670 result_type
= pointee1
;
671 else if ((TYPE_PTR_P (pointee1
) && TYPE_PTR_P (pointee2
))
672 || (TYPE_PTRMEM_P (pointee1
) && TYPE_PTRMEM_P (pointee2
)))
674 result_type
= composite_pointer_type_r (location
, pointee1
, pointee2
,
675 add_const
, operation
, complain
);
676 if (result_type
== error_mark_node
)
677 return error_mark_node
;
681 if (complain
& tf_error
)
682 composite_pointer_error (location
, DK_PERMERROR
,
685 return error_mark_node
;
686 result_type
= void_type_node
;
688 const int q1
= cp_type_quals (pointee1
);
689 const int q2
= cp_type_quals (pointee2
);
690 const int quals
= q1
| q2
;
691 result_type
= cp_build_qualified_type (result_type
,
694 : TYPE_UNQUALIFIED
)));
695 /* The cv-combined type can add "const" as per [conv.qual]/3.3 (except for
696 the TLQ). The reason is that both T1 and T2 can then be converted to the
697 cv-combined type of T1 and T2. */
698 if (quals
!= q1
|| quals
!= q2
)
700 /* If the original types were pointers to members, so is the
702 if (TYPE_PTRMEM_P (t1
))
704 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
705 TYPE_PTRMEM_CLASS_TYPE (t2
)))
707 if (complain
& tf_error
)
708 composite_pointer_error (location
, DK_PERMERROR
,
711 return error_mark_node
;
713 result_type
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
717 result_type
= build_pointer_type (result_type
);
719 /* Merge the attributes. */
720 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
721 return build_type_attribute_variant (result_type
, attributes
);
724 /* Return the composite pointer type (see [expr.type]) for T1 and T2.
725 ARG1 and ARG2 are the values with those types. The OPERATION is to
726 describe the operation between the pointer types,
727 in case an error occurs.
729 This routine also implements the computation of a common type for
730 pointers-to-members as per [expr.eq]. */
733 composite_pointer_type (const op_location_t
&location
,
734 tree t1
, tree t2
, tree arg1
, tree arg2
,
735 composite_pointer_operation operation
,
736 tsubst_flags_t complain
)
743 If one operand is a null pointer constant, the composite pointer
744 type is the type of the other operand. */
745 if (null_ptr_cst_p (arg1
))
747 if (null_ptr_cst_p (arg2
))
754 If one of the operands has type "pointer to cv1 void", then
755 the other has type "pointer to cv2 T", and the composite pointer
756 type is "pointer to cv12 void", where cv12 is the union of cv1
759 If either type is a pointer to void, make sure it is T1. */
760 if (TYPE_PTR_P (t2
) && VOID_TYPE_P (TREE_TYPE (t2
)))
763 /* Now, if T1 is a pointer to void, merge the qualifiers. */
764 if (TYPE_PTR_P (t1
) && VOID_TYPE_P (TREE_TYPE (t1
)))
769 if (TYPE_PTRFN_P (t2
))
771 if (complain
& tf_error
)
776 pedwarn (location
, OPT_Wpedantic
,
777 "ISO C++ forbids comparison between pointer "
778 "of type %<void *%> and pointer-to-function");
781 pedwarn (location
, OPT_Wpedantic
,
782 "ISO C++ forbids conversion between pointer "
783 "of type %<void *%> and pointer-to-function");
785 case CPO_CONDITIONAL_EXPR
:
786 pedwarn (location
, OPT_Wpedantic
,
787 "ISO C++ forbids conditional expression between "
788 "pointer of type %<void *%> and "
789 "pointer-to-function");
796 return error_mark_node
;
799 = cp_build_qualified_type (void_type_node
,
800 (cp_type_quals (TREE_TYPE (t1
))
801 | cp_type_quals (TREE_TYPE (t2
))));
802 result_type
= build_pointer_type (result_type
);
803 /* Merge the attributes. */
804 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
805 return build_type_attribute_variant (result_type
, attributes
);
808 if (c_dialect_objc () && TYPE_PTR_P (t1
)
811 if (objc_have_common_type (t1
, t2
, -3, NULL_TREE
))
812 return objc_common_type (t1
, t2
);
815 /* if T1 or T2 is "pointer to noexcept function" and the other type is
816 "pointer to function", where the function types are otherwise the same,
817 "pointer to function" */
818 if (fnptr_conv_p (t1
, t2
))
820 if (fnptr_conv_p (t2
, t1
))
823 /* [expr.eq] permits the application of a pointer conversion to
824 bring the pointers to a common type. */
825 if (TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
)
826 && CLASS_TYPE_P (TREE_TYPE (t1
))
827 && CLASS_TYPE_P (TREE_TYPE (t2
))
828 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1
),
831 class1
= TREE_TYPE (t1
);
832 class2
= TREE_TYPE (t2
);
834 if (DERIVED_FROM_P (class1
, class2
))
835 t2
= (build_pointer_type
836 (cp_build_qualified_type (class1
, cp_type_quals (class2
))));
837 else if (DERIVED_FROM_P (class2
, class1
))
838 t1
= (build_pointer_type
839 (cp_build_qualified_type (class2
, cp_type_quals (class1
))));
842 if (complain
& tf_error
)
843 composite_pointer_error (location
, DK_ERROR
, t1
, t2
, operation
);
844 return error_mark_node
;
847 /* [expr.eq] permits the application of a pointer-to-member
848 conversion to change the class type of one of the types. */
849 else if (TYPE_PTRMEM_P (t1
)
850 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
851 TYPE_PTRMEM_CLASS_TYPE (t2
)))
853 class1
= TYPE_PTRMEM_CLASS_TYPE (t1
);
854 class2
= TYPE_PTRMEM_CLASS_TYPE (t2
);
856 if (DERIVED_FROM_P (class1
, class2
))
857 t1
= build_ptrmem_type (class2
, TYPE_PTRMEM_POINTED_TO_TYPE (t1
));
858 else if (DERIVED_FROM_P (class2
, class1
))
859 t2
= build_ptrmem_type (class1
, TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
862 if (complain
& tf_error
)
866 error_at (location
, "comparison between distinct "
867 "pointer-to-member types %qT and %qT lacks a cast",
871 error_at (location
, "conversion between distinct "
872 "pointer-to-member types %qT and %qT lacks a cast",
875 case CPO_CONDITIONAL_EXPR
:
876 error_at (location
, "conditional expression between distinct "
877 "pointer-to-member types %qT and %qT lacks a cast",
883 return error_mark_node
;
887 bool add_const
= false;
888 return composite_pointer_type_r (location
, t1
, t2
, &add_const
, operation
,
892 /* Return the merged type of two types.
893 We assume that comptypes has already been done and returned 1;
894 if that isn't so, this may crash.
896 This just combines attributes and default arguments; any other
897 differences would cause the two types to compare unalike. */
900 merge_types (tree t1
, tree t2
)
902 enum tree_code code1
;
903 enum tree_code code2
;
906 /* Save time if the two types are the same. */
909 if (original_type (t1
) == original_type (t2
))
912 /* If one type is nonsense, use the other. */
913 if (t1
== error_mark_node
)
915 if (t2
== error_mark_node
)
918 /* Handle merging an auto redeclaration with a previous deduced
923 /* Merge the attributes. */
924 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
926 if (TYPE_PTRMEMFUNC_P (t1
))
927 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
928 if (TYPE_PTRMEMFUNC_P (t2
))
929 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
931 code1
= TREE_CODE (t1
);
932 code2
= TREE_CODE (t2
);
935 gcc_assert (code1
== TYPENAME_TYPE
|| code2
== TYPENAME_TYPE
);
936 if (code1
== TYPENAME_TYPE
)
938 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
939 code1
= TREE_CODE (t1
);
943 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
944 code2
= TREE_CODE (t2
);
952 /* For two pointers, do this recursively on the target type. */
954 tree target
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
955 int quals
= cp_type_quals (t1
);
957 if (code1
== POINTER_TYPE
)
959 t1
= build_pointer_type (target
);
960 if (TREE_CODE (target
) == METHOD_TYPE
)
961 t1
= build_ptrmemfunc_type (t1
);
964 t1
= cp_build_reference_type (target
, TYPE_REF_IS_RVALUE (t1
));
965 t1
= build_type_attribute_variant (t1
, attributes
);
966 t1
= cp_build_qualified_type (t1
, quals
);
975 quals
= cp_type_quals (t1
);
976 pointee
= merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1
),
977 TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
978 t1
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
980 t1
= cp_build_qualified_type (t1
, quals
);
986 tree elt
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
987 /* Save space: see if the result is identical to one of the args. */
988 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
989 return build_type_attribute_variant (t1
, attributes
);
990 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
991 return build_type_attribute_variant (t2
, attributes
);
992 /* Merge the element types, and have a size if either arg has one. */
993 t1
= build_cplus_array_type
994 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
999 /* Function types: prefer the one that specified arg types.
1000 If both do, merge the arg types. Also merge the return types. */
1002 tree valtype
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
1003 tree p1
= TYPE_ARG_TYPES (t1
);
1004 tree p2
= TYPE_ARG_TYPES (t2
);
1007 /* Save space: see if the result is identical to one of the args. */
1008 if (valtype
== TREE_TYPE (t1
) && ! p2
)
1009 return cp_build_type_attribute_variant (t1
, attributes
);
1010 if (valtype
== TREE_TYPE (t2
) && ! p1
)
1011 return cp_build_type_attribute_variant (t2
, attributes
);
1013 /* Simple way if one arg fails to specify argument types. */
1014 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
1016 else if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
1019 parms
= commonparms (p1
, p2
);
1021 cp_cv_quals quals
= type_memfn_quals (t1
);
1022 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
1023 gcc_assert (quals
== type_memfn_quals (t2
));
1024 gcc_assert (rqual
== type_memfn_rqual (t2
));
1026 tree rval
= build_function_type (valtype
, parms
);
1027 rval
= apply_memfn_quals (rval
, quals
);
1028 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
1029 TYPE_RAISES_EXCEPTIONS (t2
));
1030 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
1031 t1
= build_cp_fntype_variant (rval
, rqual
, raises
, late_return_type_p
);
1037 /* Get this value the long way, since TYPE_METHOD_BASETYPE
1038 is just the main variant of this. */
1039 tree basetype
= class_of_this_parm (t2
);
1040 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
1041 TYPE_RAISES_EXCEPTIONS (t2
));
1042 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
1044 bool late_return_type_1_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
1046 /* If this was a member function type, get back to the
1047 original type of type member function (i.e., without
1048 the class instance variable up front. */
1049 t1
= build_function_type (TREE_TYPE (t1
),
1050 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
1051 t2
= build_function_type (TREE_TYPE (t2
),
1052 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
1053 t3
= merge_types (t1
, t2
);
1054 t3
= build_method_type_directly (basetype
, TREE_TYPE (t3
),
1055 TYPE_ARG_TYPES (t3
));
1056 t1
= build_cp_fntype_variant (t3
, rqual
, raises
, late_return_type_1_p
);
1061 /* There is no need to merge attributes into a TYPENAME_TYPE.
1062 When the type is instantiated it will have whatever
1063 attributes result from the instantiation. */
1067 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
1069 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
1074 return cp_build_type_attribute_variant (t1
, attributes
);
1077 /* Return the ARRAY_TYPE type without its domain. */
1080 strip_array_domain (tree type
)
1083 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1084 if (TYPE_DOMAIN (type
) == NULL_TREE
)
1086 t2
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
1087 return cp_build_type_attribute_variant (t2
, TYPE_ATTRIBUTES (type
));
1090 /* Wrapper around cp_common_type that is used by c-common.cc and other
1091 front end optimizations that remove promotions.
1093 Return the common type for two arithmetic types T1 and T2 under the
1094 usual arithmetic conversions. The default conversions have already
1095 been applied, and enumerated types converted to their compatible
1099 common_type (tree t1
, tree t2
)
1101 /* If one type is nonsense, use the other */
1102 if (t1
== error_mark_node
)
1104 if (t2
== error_mark_node
)
1107 return cp_common_type (t1
, t2
);
1110 /* Return the common type of two pointer types T1 and T2. This is the
1111 type for the result of most arithmetic operations if the operands
1112 have the given two types.
1114 We assume that comp_target_types has already been done and returned
1115 nonzero; if that isn't so, this may crash. */
1118 common_pointer_type (tree t1
, tree t2
)
1120 gcc_assert ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
1121 || (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
1122 || (TYPE_PTRMEMFUNC_P (t1
) && TYPE_PTRMEMFUNC_P (t2
)));
1124 return composite_pointer_type (input_location
, t1
, t2
,
1125 error_mark_node
, error_mark_node
,
1126 CPO_CONVERSION
, tf_warning_or_error
);
1129 /* Compare two exception specifier types for exactness or subsetness, if
1130 allowed. Returns false for mismatch, true for match (same, or
1131 derived and !exact).
1133 [except.spec] "If a class X ... objects of class X or any class publicly
1134 and unambiguously derived from X. Similarly, if a pointer type Y * ...
1135 exceptions of type Y * or that are pointers to any type publicly and
1136 unambiguously derived from Y. Otherwise a function only allows exceptions
1137 that have the same type ..."
1138 This does not mention cv qualifiers and is different to what throw
1139 [except.throw] and catch [except.catch] will do. They will ignore the
1140 top level cv qualifiers, and allow qualifiers in the pointer to class
1143 We implement the letter of the standard. */
1146 comp_except_types (tree a
, tree b
, bool exact
)
1148 if (same_type_p (a
, b
))
1152 if (cp_type_quals (a
) || cp_type_quals (b
))
1155 if (TYPE_PTR_P (a
) && TYPE_PTR_P (b
))
1159 if (cp_type_quals (a
) || cp_type_quals (b
))
1163 if (TREE_CODE (a
) != RECORD_TYPE
1164 || TREE_CODE (b
) != RECORD_TYPE
)
1167 if (publicly_uniquely_derived_p (a
, b
))
1173 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1174 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1175 If EXACT is ce_type, the C++17 type compatibility rules apply.
1176 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1177 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1178 are unordered, but we've already filtered out duplicates. Most lists will
1179 be in order, we should try to make use of that. */
1182 comp_except_specs (const_tree t1
, const_tree t2
, int exact
)
1191 /* First handle noexcept. */
1192 if (exact
< ce_exact
)
1194 if (exact
== ce_type
1195 && (canonical_eh_spec (CONST_CAST_TREE (t1
))
1196 == canonical_eh_spec (CONST_CAST_TREE (t2
))))
1199 /* noexcept(false) is compatible with no exception-specification,
1200 and less strict than any spec. */
1201 if (t1
== noexcept_false_spec
)
1202 return t2
== NULL_TREE
|| exact
== ce_derived
;
1203 /* Even a derived noexcept(false) is compatible with no
1204 exception-specification. */
1205 if (t2
== noexcept_false_spec
)
1206 return t1
== NULL_TREE
;
1208 /* Otherwise, if we aren't looking for an exact match, noexcept is
1209 equivalent to throw(). */
1210 if (t1
== noexcept_true_spec
)
1211 t1
= empty_except_spec
;
1212 if (t2
== noexcept_true_spec
)
1213 t2
= empty_except_spec
;
1216 /* If any noexcept is left, it is only comparable to itself;
1217 either we're looking for an exact match or we're redeclaring a
1218 template with dependent noexcept. */
1219 if ((t1
&& TREE_PURPOSE (t1
))
1220 || (t2
&& TREE_PURPOSE (t2
)))
1222 && cp_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)));
1224 if (t1
== NULL_TREE
) /* T1 is ... */
1225 return t2
== NULL_TREE
|| exact
== ce_derived
;
1226 if (!TREE_VALUE (t1
)) /* t1 is EMPTY */
1227 return t2
!= NULL_TREE
&& !TREE_VALUE (t2
);
1228 if (t2
== NULL_TREE
) /* T2 is ... */
1230 if (TREE_VALUE (t1
) && !TREE_VALUE (t2
)) /* T2 is EMPTY, T1 is not */
1231 return exact
== ce_derived
;
1233 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1234 Count how many we find, to determine exactness. For exact matching and
1235 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1237 for (base
= t1
; t2
!= NULL_TREE
; t2
= TREE_CHAIN (t2
))
1239 for (probe
= base
; probe
!= NULL_TREE
; probe
= TREE_CHAIN (probe
))
1241 tree a
= TREE_VALUE (probe
);
1242 tree b
= TREE_VALUE (t2
);
1244 if (comp_except_types (a
, b
, exact
))
1246 if (probe
== base
&& exact
> ce_derived
)
1247 base
= TREE_CHAIN (probe
);
1252 if (probe
== NULL_TREE
)
1255 return exact
== ce_derived
|| base
== NULL_TREE
|| length
== list_length (t1
);
1258 /* Compare the array types T1 and T2. CB says how we should behave when
1259 comparing array bounds: bounds_none doesn't allow dimensionless arrays,
1260 bounds_either says than any array can be [], bounds_first means that
1261 onlt T1 can be an array with unknown bounds. STRICT is true if
1262 qualifiers must match when comparing the types of the array elements. */
1265 comp_array_types (const_tree t1
, const_tree t2
, compare_bounds_t cb
,
1275 /* The type of the array elements must be the same. */
1277 ? !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
))
1278 : !similar_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1281 d1
= TYPE_DOMAIN (t1
);
1282 d2
= TYPE_DOMAIN (t2
);
1287 /* If one of the arrays is dimensionless, and the other has a
1288 dimension, they are of different types. However, it is valid to
1296 declarations for an array object can specify
1297 array types that differ by the presence or absence of a major
1298 array bound (_dcl.array_). */
1300 return cb
>= bounds_either
;
1302 return cb
== bounds_either
;
1304 /* Check that the dimensions are the same. */
1306 if (!cp_tree_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
)))
1308 max1
= TYPE_MAX_VALUE (d1
);
1309 max2
= TYPE_MAX_VALUE (d2
);
1311 if (!cp_tree_equal (max1
, max2
))
1317 /* Compare the relative position of T1 and T2 into their respective
1318 template parameter list.
1319 T1 and T2 must be template parameter types.
1320 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1323 comp_template_parms_position (tree t1
, tree t2
)
1325 tree index1
, index2
;
1326 gcc_assert (t1
&& t2
1327 && TREE_CODE (t1
) == TREE_CODE (t2
)
1328 && (TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
1329 || TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
1330 || TREE_CODE (t1
) == TEMPLATE_TYPE_PARM
));
1332 index1
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1
));
1333 index2
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2
));
1335 /* Then compare their relative position. */
1336 if (TEMPLATE_PARM_IDX (index1
) != TEMPLATE_PARM_IDX (index2
)
1337 || TEMPLATE_PARM_LEVEL (index1
) != TEMPLATE_PARM_LEVEL (index2
)
1338 || (TEMPLATE_PARM_PARAMETER_PACK (index1
)
1339 != TEMPLATE_PARM_PARAMETER_PACK (index2
)))
1342 /* In C++14 we can end up comparing 'auto' to a normal template
1343 parameter. Don't confuse them. */
1344 if (cxx_dialect
>= cxx14
&& (is_auto (t1
) || is_auto (t2
)))
1345 return TYPE_IDENTIFIER (t1
) == TYPE_IDENTIFIER (t2
);
1350 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1353 cxx_safe_arg_type_equiv_p (tree t1
, tree t2
)
1355 t1
= TYPE_MAIN_VARIANT (t1
);
1356 t2
= TYPE_MAIN_VARIANT (t2
);
1362 /* The signedness of the parameter matters only when an integral
1363 type smaller than int is promoted to int, otherwise only the
1364 precision of the parameter matters.
1365 This check should make sure that the callee does not see
1366 undefined values in argument registers. */
1367 if (INTEGRAL_TYPE_P (t1
)
1368 && INTEGRAL_TYPE_P (t2
)
1369 && TYPE_PRECISION (t1
) == TYPE_PRECISION (t2
)
1370 && (TYPE_UNSIGNED (t1
) == TYPE_UNSIGNED (t2
)
1371 || !targetm
.calls
.promote_prototypes (NULL_TREE
)
1372 || TYPE_PRECISION (t1
) >= TYPE_PRECISION (integer_type_node
)))
1375 return same_type_p (t1
, t2
);
1378 /* Check if a type cast between two function types can be considered safe. */
1381 cxx_safe_function_type_cast_p (tree t1
, tree t2
)
1383 if (TREE_TYPE (t1
) == void_type_node
&&
1384 TYPE_ARG_TYPES (t1
) == void_list_node
)
1387 if (TREE_TYPE (t2
) == void_type_node
&&
1388 TYPE_ARG_TYPES (t2
) == void_list_node
)
1391 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1394 for (t1
= TYPE_ARG_TYPES (t1
), t2
= TYPE_ARG_TYPES (t2
);
1396 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1397 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1403 /* Subroutine in comptypes. */
1406 structural_comptypes (tree t1
, tree t2
, int strict
)
1408 /* Both should be types that are not obviously the same. */
1409 gcc_checking_assert (t1
!= t2
&& TYPE_P (t1
) && TYPE_P (t2
));
1411 /* Suppress typename resolution under spec_hasher::equal in place of calling
1412 push_to_top_level there. */
1413 if (!comparing_specializations
)
1415 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1416 current instantiation. */
1417 if (TREE_CODE (t1
) == TYPENAME_TYPE
)
1418 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
1420 if (TREE_CODE (t2
) == TYPENAME_TYPE
)
1421 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
1424 if (TYPE_PTRMEMFUNC_P (t1
))
1425 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
1426 if (TYPE_PTRMEMFUNC_P (t2
))
1427 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
1429 /* Different classes of types can't be compatible. */
1430 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1433 /* Qualifiers must match. For array types, we will check when we
1434 recur on the array element types. */
1435 if (TREE_CODE (t1
) != ARRAY_TYPE
1436 && cp_type_quals (t1
) != cp_type_quals (t2
))
1438 if (TREE_CODE (t1
) == FUNCTION_TYPE
1439 && type_memfn_quals (t1
) != type_memfn_quals (t2
))
1441 /* Need to check this before TYPE_MAIN_VARIANT.
1442 FIXME function qualifiers should really change the main variant. */
1443 if (FUNC_OR_METHOD_TYPE_P (t1
))
1445 if (type_memfn_rqual (t1
) != type_memfn_rqual (t2
))
1447 if (flag_noexcept_type
1448 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1
),
1449 TYPE_RAISES_EXCEPTIONS (t2
),
1454 /* Allow for two different type nodes which have essentially the same
1455 definition. Note that we already checked for equality of the type
1456 qualifiers (just above). */
1457 if (TREE_CODE (t1
) != ARRAY_TYPE
1458 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1461 /* Compare the types. Return false on known not-same. Break on not
1462 known. Never return true from this switch -- you'll break
1463 specialization comparison. */
1464 switch (TREE_CODE (t1
))
1468 /* All void and bool types are the same. */
1473 case FIXED_POINT_TYPE
:
1475 /* With these nodes, we can't determine type equivalence by
1476 looking at what is stored in the nodes themselves, because
1477 two nodes might have different TYPE_MAIN_VARIANTs but still
1478 represent the same type. For example, wchar_t and int could
1479 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1480 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1481 and are distinct types. On the other hand, int and the
1484 typedef int INT __attribute((may_alias));
1486 have identical properties, different TYPE_MAIN_VARIANTs, but
1487 represent the same type. The canonical type system keeps
1488 track of equivalence in this case, so we fall back on it. */
1489 if (TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1492 /* We don't need or want the attribute comparison. */
1495 case TEMPLATE_TEMPLATE_PARM
:
1496 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1497 if (!comp_template_parms_position (t1
, t2
))
1499 if (!comp_template_parms
1500 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1
)),
1501 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2
))))
1503 if (TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
)
1505 /* Don't check inheritance. */
1506 strict
= COMPARE_STRICT
;
1511 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
1512 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
1513 || TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1514 && comp_template_args (TYPE_TI_ARGS (t1
), TYPE_TI_ARGS (t2
)))
1517 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
1519 else if ((strict
& COMPARE_DERIVED
) && DERIVED_FROM_P (t2
, t1
))
1525 if (!comptypes (TYPE_OFFSET_BASETYPE (t1
), TYPE_OFFSET_BASETYPE (t2
),
1526 strict
& ~COMPARE_REDECLARATION
))
1528 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1532 case REFERENCE_TYPE
:
1533 if (TYPE_REF_IS_RVALUE (t1
) != TYPE_REF_IS_RVALUE (t2
))
1535 /* fall through to checks for pointer types */
1539 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1540 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1546 /* Exception specs and memfn_rquals were checked above. */
1547 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1549 if (!compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)))
1554 /* Target types must match incl. qualifiers. */
1555 if (!comp_array_types (t1
, t2
, ((strict
& COMPARE_REDECLARATION
)
1556 ? bounds_either
: bounds_none
),
1561 case TEMPLATE_TYPE_PARM
:
1562 /* If T1 and T2 don't have the same relative position in their
1563 template parameters set, they can't be equal. */
1564 if (!comp_template_parms_position (t1
, t2
))
1566 /* If T1 and T2 don't represent the same class template deduction,
1567 they aren't equal. */
1568 if (CLASS_PLACEHOLDER_TEMPLATE (t1
)
1569 != CLASS_PLACEHOLDER_TEMPLATE (t2
))
1571 /* Constrained 'auto's are distinct from parms that don't have the same
1573 if (!equivalent_placeholder_constraints (t1
, t2
))
1578 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1
),
1579 TYPENAME_TYPE_FULLNAME (t2
)))
1581 /* Qualifiers don't matter on scopes. */
1582 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1
),
1587 case UNBOUND_CLASS_TEMPLATE
:
1588 if (!cp_tree_equal (TYPE_IDENTIFIER (t1
), TYPE_IDENTIFIER (t2
)))
1590 if (!same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
)))
1595 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1600 if (gnu_vector_type_p (t1
) != gnu_vector_type_p (t2
)
1601 || maybe_ne (TYPE_VECTOR_SUBPARTS (t1
), TYPE_VECTOR_SUBPARTS (t2
))
1602 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1606 case TYPE_PACK_EXPANSION
:
1607 return (same_type_p (PACK_EXPANSION_PATTERN (t1
),
1608 PACK_EXPANSION_PATTERN (t2
))
1609 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1
),
1610 PACK_EXPANSION_EXTRA_ARGS (t2
)));
1613 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1
)
1614 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2
))
1616 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1
) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2
))
1618 if (DECLTYPE_FOR_LAMBDA_PROXY (t1
) != DECLTYPE_FOR_LAMBDA_PROXY (t2
))
1620 if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1
), DECLTYPE_TYPE_EXPR (t2
)))
1625 if (TRAIT_TYPE_KIND (t1
) != TRAIT_TYPE_KIND (t2
))
1627 if (!same_type_p (TRAIT_TYPE_TYPE1 (t1
), TRAIT_TYPE_TYPE1 (t2
))
1628 || !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1
), TRAIT_TYPE_TYPE2 (t2
)))
1633 if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1
), TYPEOF_TYPE_EXPR (t2
)))
1641 /* If we get here, we know that from a target independent POV the
1642 types are the same. Make sure the target attributes are also
1644 if (!comp_type_attributes (t1
, t2
))
1648 if (comparing_dependent_aliases
)
1650 /* Don't treat an alias template specialization with dependent
1651 arguments as equivalent to its underlying type when used as a
1652 template argument; we need them to be distinct so that we
1653 substitute into the specialization arguments at instantiation
1654 time. And aliases can't be equivalent without being ==, so
1655 we don't need to look any deeper. */
1656 ++processing_template_decl
;
1657 tree dep1
= dependent_alias_template_spec_p (t1
, nt_transparent
);
1658 tree dep2
= dependent_alias_template_spec_p (t2
, nt_transparent
);
1659 --processing_template_decl
;
1660 if ((dep1
|| dep2
) && dep1
!= dep2
)
1667 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1668 is a bitwise-or of the COMPARE_* flags. */
1671 comptypes (tree t1
, tree t2
, int strict
)
1673 gcc_checking_assert (t1
&& t2
);
1675 /* TYPE_ARGUMENT_PACKS are not really types. */
1676 gcc_checking_assert (TREE_CODE (t1
) != TYPE_ARGUMENT_PACK
1677 && TREE_CODE (t2
) != TYPE_ARGUMENT_PACK
);
1682 /* Suppress errors caused by previously reported errors. */
1683 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1686 if (strict
== COMPARE_STRICT
)
1688 if (TYPE_STRUCTURAL_EQUALITY_P (t1
) || TYPE_STRUCTURAL_EQUALITY_P (t2
))
1689 /* At least one of the types requires structural equality, so
1690 perform a deep check. */
1691 return structural_comptypes (t1
, t2
, strict
);
1693 if (flag_checking
&& param_use_canonical_types
)
1695 bool result
= structural_comptypes (t1
, t2
, strict
);
1697 if (result
&& TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1698 /* The two types are structurally equivalent, but their
1699 canonical types were different. This is a failure of the
1700 canonical type propagation code.*/
1702 ("canonical types differ for identical types %qT and %qT",
1704 else if (!result
&& TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
))
1705 /* Two types are structurally different, but the canonical
1706 types are the same. This means we were over-eager in
1707 assigning canonical types. */
1709 ("same canonical type node for different types %qT and %qT",
1714 if (!flag_checking
&& param_use_canonical_types
)
1715 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1717 return structural_comptypes (t1
, t2
, strict
);
1719 else if (strict
== COMPARE_STRUCTURAL
)
1720 return structural_comptypes (t1
, t2
, COMPARE_STRICT
);
1722 return structural_comptypes (t1
, t2
, strict
);
1725 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1726 top-level qualifiers. */
1729 same_type_ignoring_top_level_qualifiers_p (tree type1
, tree type2
)
1731 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1736 type1
= cp_build_qualified_type (type1
, TYPE_UNQUALIFIED
);
1737 type2
= cp_build_qualified_type (type2
, TYPE_UNQUALIFIED
);
1738 return same_type_p (type1
, type2
);
1741 /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1744 similar_type_p (tree type1
, tree type2
)
1746 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1749 /* Informally, two types are similar if, ignoring top-level cv-qualification:
1750 * they are the same type; or
1751 * they are both pointers, and the pointed-to types are similar; or
1752 * they are both pointers to member of the same class, and the types of
1753 the pointed-to members are similar; or
1754 * they are both arrays of the same size or both arrays of unknown bound,
1755 and the array element types are similar. */
1757 if (same_type_ignoring_top_level_qualifiers_p (type1
, type2
))
1760 if ((TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
1761 || (TYPE_PTRDATAMEM_P (type1
) && TYPE_PTRDATAMEM_P (type2
))
1762 || (TREE_CODE (type1
) == ARRAY_TYPE
&& TREE_CODE (type2
) == ARRAY_TYPE
))
1763 return comp_ptr_ttypes_const (type1
, type2
, bounds_either
);
1768 /* Helper function for layout_compatible_type_p and
1769 is_corresponding_member_aggr. Advance to next members (NULL if
1770 no further ones) and return true if those members are still part of
1771 the common initial sequence. */
1774 next_common_initial_seqence (tree
&memb1
, tree
&memb2
)
1778 if (TREE_CODE (memb1
) != FIELD_DECL
1779 || (DECL_FIELD_IS_BASE (memb1
) && is_empty_field (memb1
)))
1781 memb1
= DECL_CHAIN (memb1
);
1784 if (DECL_FIELD_IS_BASE (memb1
))
1786 memb1
= TYPE_FIELDS (TREE_TYPE (memb1
));
1793 if (TREE_CODE (memb2
) != FIELD_DECL
1794 || (DECL_FIELD_IS_BASE (memb2
) && is_empty_field (memb2
)))
1796 memb2
= DECL_CHAIN (memb2
);
1799 if (DECL_FIELD_IS_BASE (memb2
))
1801 memb2
= TYPE_FIELDS (TREE_TYPE (memb2
));
1806 if (memb1
== NULL_TREE
&& memb2
== NULL_TREE
)
1808 if (memb1
== NULL_TREE
|| memb2
== NULL_TREE
)
1810 if (DECL_BIT_FIELD_TYPE (memb1
))
1812 if (!DECL_BIT_FIELD_TYPE (memb2
))
1814 if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1
),
1815 DECL_BIT_FIELD_TYPE (memb2
)))
1817 if (TYPE_PRECISION (TREE_TYPE (memb1
))
1818 != TYPE_PRECISION (TREE_TYPE (memb2
)))
1821 else if (DECL_BIT_FIELD_TYPE (memb2
))
1823 else if (!layout_compatible_type_p (TREE_TYPE (memb1
), TREE_TYPE (memb2
)))
1825 if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1
)))
1826 != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2
)))
1828 if (!tree_int_cst_equal (bit_position (memb1
), bit_position (memb2
)))
1833 /* Return true if TYPE1 and TYPE2 are layout-compatible types. */
1836 layout_compatible_type_p (tree type1
, tree type2
)
1838 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1842 if (TREE_CODE (type1
) != TREE_CODE (type2
))
1845 type1
= cp_build_qualified_type (type1
, TYPE_UNQUALIFIED
);
1846 type2
= cp_build_qualified_type (type2
, TYPE_UNQUALIFIED
);
1848 if (TREE_CODE (type1
) == ENUMERAL_TYPE
)
1849 return (TYPE_ALIGN (type1
) == TYPE_ALIGN (type2
)
1850 && tree_int_cst_equal (TYPE_SIZE (type1
), TYPE_SIZE (type2
))
1851 && same_type_p (finish_underlying_type (type1
),
1852 finish_underlying_type (type2
)));
1854 if (CLASS_TYPE_P (type1
)
1855 && std_layout_type_p (type1
)
1856 && std_layout_type_p (type2
)
1857 && TYPE_ALIGN (type1
) == TYPE_ALIGN (type2
)
1858 && tree_int_cst_equal (TYPE_SIZE (type1
), TYPE_SIZE (type2
)))
1860 tree field1
= TYPE_FIELDS (type1
);
1861 tree field2
= TYPE_FIELDS (type2
);
1862 if (TREE_CODE (type1
) == RECORD_TYPE
)
1866 if (!next_common_initial_seqence (field1
, field2
))
1868 if (field1
== NULL_TREE
)
1870 field1
= DECL_CHAIN (field1
);
1871 field2
= DECL_CHAIN (field2
);
1874 /* Otherwise both types must be union types.
1876 "Two standard-layout unions are layout-compatible if they have
1877 the same number of non-static data members and corresponding
1878 non-static data members (in any order) have layout-compatible
1880 but the code anticipates that bitfield vs. non-bitfield,
1881 different bitfield widths or presence/absence of
1882 [[no_unique_address]] should be checked as well. */
1883 auto_vec
<tree
, 16> vec
;
1884 unsigned int count
= 0;
1885 for (; field1
; field1
= DECL_CHAIN (field1
))
1886 if (TREE_CODE (field1
) == FIELD_DECL
)
1888 for (; field2
; field2
= DECL_CHAIN (field2
))
1889 if (TREE_CODE (field2
) == FIELD_DECL
)
1890 vec
.safe_push (field2
);
1891 /* Discussions on core lean towards treating multiple union fields
1892 of the same type as the same field, so this might need changing
1894 if (count
!= vec
.length ())
1896 for (field1
= TYPE_FIELDS (type1
); field1
; field1
= DECL_CHAIN (field1
))
1898 if (TREE_CODE (field1
) != FIELD_DECL
)
1901 tree t1
= DECL_BIT_FIELD_TYPE (field1
);
1902 if (t1
== NULL_TREE
)
1903 t1
= TREE_TYPE (field1
);
1904 FOR_EACH_VEC_ELT (vec
, j
, field2
)
1906 tree t2
= DECL_BIT_FIELD_TYPE (field2
);
1907 if (t2
== NULL_TREE
)
1908 t2
= TREE_TYPE (field2
);
1909 if (DECL_BIT_FIELD_TYPE (field1
))
1911 if (!DECL_BIT_FIELD_TYPE (field2
))
1913 if (TYPE_PRECISION (TREE_TYPE (field1
))
1914 != TYPE_PRECISION (TREE_TYPE (field2
)))
1917 else if (DECL_BIT_FIELD_TYPE (field2
))
1919 if (!layout_compatible_type_p (t1
, t2
))
1921 if ((!lookup_attribute ("no_unique_address",
1922 DECL_ATTRIBUTES (field1
)))
1923 != !lookup_attribute ("no_unique_address",
1924 DECL_ATTRIBUTES (field2
)))
1928 if (j
== vec
.length ())
1930 vec
.unordered_remove (j
);
1935 return same_type_p (type1
, type2
);
1938 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1941 at_least_as_qualified_p (const_tree type1
, const_tree type2
)
1943 int q1
= cp_type_quals (type1
);
1944 int q2
= cp_type_quals (type2
);
1946 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1947 return (q1
& q2
) == q2
;
1950 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1951 more cv-qualified that TYPE1, and 0 otherwise. */
1954 comp_cv_qualification (int q1
, int q2
)
1959 if ((q1
& q2
) == q2
)
1961 else if ((q1
& q2
) == q1
)
1968 comp_cv_qualification (const_tree type1
, const_tree type2
)
1970 int q1
= cp_type_quals (type1
);
1971 int q2
= cp_type_quals (type2
);
1972 return comp_cv_qualification (q1
, q2
);
1975 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1976 subset of the cv-qualification signature of TYPE2, and the types
1977 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1980 comp_cv_qual_signature (tree type1
, tree type2
)
1982 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1984 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1990 /* Subroutines of `comptypes'. */
1992 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1993 equivalent in the sense that functions with those parameter types
1994 can have equivalent types. The two lists must be equivalent,
1995 element by element. */
1998 compparms (const_tree parms1
, const_tree parms2
)
2002 /* An unspecified parmlist matches any specified parmlist
2003 whose argument types don't need default promotions. */
2005 for (t1
= parms1
, t2
= parms2
;
2007 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
2009 /* If one parmlist is shorter than the other,
2010 they fail to match. */
2013 if (!same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
2020 /* Process a sizeof or alignof expression where the operand is a type.
2021 STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
2022 or GNU (preferred alignment) semantics; it is ignored if OP is
2026 cxx_sizeof_or_alignof_type (location_t loc
, tree type
, enum tree_code op
,
2027 bool std_alignof
, bool complain
)
2029 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
2030 if (type
== error_mark_node
)
2031 return error_mark_node
;
2033 type
= non_reference (type
);
2034 if (TREE_CODE (type
) == METHOD_TYPE
)
2038 pedwarn (loc
, OPT_Wpointer_arith
,
2039 "invalid application of %qs to a member function",
2040 OVL_OP_INFO (false, op
)->name
);
2041 return size_one_node
;
2044 return error_mark_node
;
2046 else if (VOID_TYPE_P (type
) && std_alignof
)
2049 error_at (loc
, "invalid application of %qs to a void type",
2050 OVL_OP_INFO (false, op
)->name
);
2051 return error_mark_node
;
2054 bool dependent_p
= dependent_type_p (type
);
2056 complete_type (type
);
2058 /* VLA types will have a non-constant size. In the body of an
2059 uninstantiated template, we don't need to try to compute the
2060 value, because the sizeof expression is not an integral
2061 constant expression in that case. And, if we do try to
2062 compute the value, we'll likely end up with SAVE_EXPRs, which
2063 the template substitution machinery does not expect to see. */
2064 || (processing_template_decl
2065 && COMPLETE_TYPE_P (type
)
2066 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
))
2068 tree value
= build_min (op
, size_type_node
, type
);
2069 TREE_READONLY (value
) = 1;
2070 if (op
== ALIGNOF_EXPR
&& std_alignof
)
2071 ALIGNOF_EXPR_STD_P (value
) = true;
2072 SET_EXPR_LOCATION (value
, loc
);
2076 return c_sizeof_or_alignof_type (loc
, complete_type (type
),
2077 op
== SIZEOF_EXPR
, std_alignof
,
2081 /* Return the size of the type, without producing any warnings for
2082 types whose size cannot be taken. This routine should be used only
2083 in some other routine that has already produced a diagnostic about
2084 using the size of such a type. */
2086 cxx_sizeof_nowarn (tree type
)
2088 if (TREE_CODE (type
) == FUNCTION_TYPE
2089 || VOID_TYPE_P (type
)
2090 || TREE_CODE (type
) == ERROR_MARK
)
2091 return size_one_node
;
2092 else if (!COMPLETE_TYPE_P (type
))
2093 return size_zero_node
;
2095 return cxx_sizeof_or_alignof_type (input_location
, type
,
2096 SIZEOF_EXPR
, false, false);
2099 /* Process a sizeof expression where the operand is an expression. */
2102 cxx_sizeof_expr (location_t loc
, tree e
, tsubst_flags_t complain
)
2104 if (e
== error_mark_node
)
2105 return error_mark_node
;
2107 if (instantiation_dependent_uneval_expression_p (e
))
2109 e
= build_min (SIZEOF_EXPR
, size_type_node
, e
);
2110 TREE_SIDE_EFFECTS (e
) = 0;
2111 TREE_READONLY (e
) = 1;
2112 SET_EXPR_LOCATION (e
, loc
);
2117 location_t e_loc
= cp_expr_loc_or_loc (e
, loc
);
2118 STRIP_ANY_LOCATION_WRAPPER (e
);
2120 /* To get the size of a static data member declared as an array of
2121 unknown bound, we need to instantiate it. */
2123 && VAR_HAD_UNKNOWN_BOUND (e
)
2124 && DECL_TEMPLATE_INSTANTIATION (e
))
2125 instantiate_decl (e
, /*defer_ok*/true, /*expl_inst_mem*/false);
2127 if (TREE_CODE (e
) == PARM_DECL
2128 && DECL_ARRAY_PARAMETER_P (e
)
2129 && (complain
& tf_warning
))
2131 auto_diagnostic_group d
;
2132 if (warning_at (e_loc
, OPT_Wsizeof_array_argument
,
2133 "%<sizeof%> on array function parameter %qE "
2134 "will return size of %qT", e
, TREE_TYPE (e
)))
2135 inform (DECL_SOURCE_LOCATION (e
), "declared here");
2138 e
= mark_type_use (e
);
2142 if (complain
& tf_error
)
2144 "invalid application of %<sizeof%> to a bit-field");
2146 return error_mark_node
;
2149 else if (is_overloaded_fn (e
))
2151 if (complain
& tf_error
)
2152 permerror (e_loc
, "ISO C++ forbids applying %<sizeof%> to "
2153 "an expression of function type");
2155 return error_mark_node
;
2158 else if (type_unknown_p (e
))
2160 if (complain
& tf_error
)
2161 cxx_incomplete_type_error (e_loc
, e
, TREE_TYPE (e
));
2163 return error_mark_node
;
2169 return cxx_sizeof_or_alignof_type (loc
, e
, SIZEOF_EXPR
, false,
2170 complain
& tf_error
);
2173 /* Implement the __alignof keyword: Return the minimum required
2174 alignment of E, measured in bytes. For VAR_DECL's and
2175 FIELD_DECL's return DECL_ALIGN (which can be set from an
2176 "aligned" __attribute__ specification). STD_ALIGNOF acts
2177 like in cxx_sizeof_or_alignof_type. */
2180 cxx_alignof_expr (location_t loc
, tree e
, bool std_alignof
,
2181 tsubst_flags_t complain
)
2185 if (e
== error_mark_node
)
2186 return error_mark_node
;
2188 if (processing_template_decl
)
2190 e
= build_min (ALIGNOF_EXPR
, size_type_node
, e
);
2191 TREE_SIDE_EFFECTS (e
) = 0;
2192 TREE_READONLY (e
) = 1;
2193 SET_EXPR_LOCATION (e
, loc
);
2194 ALIGNOF_EXPR_STD_P (e
) = std_alignof
;
2199 location_t e_loc
= cp_expr_loc_or_loc (e
, loc
);
2200 STRIP_ANY_LOCATION_WRAPPER (e
);
2202 e
= mark_type_use (e
);
2204 if (!verify_type_context (loc
, TCTX_ALIGNOF
, TREE_TYPE (e
),
2205 !(complain
& tf_error
)))
2207 if (!(complain
& tf_error
))
2208 return error_mark_node
;
2212 t
= size_int (DECL_ALIGN_UNIT (e
));
2213 else if (bitfield_p (e
))
2215 if (complain
& tf_error
)
2217 "invalid application of %<__alignof%> to a bit-field");
2219 return error_mark_node
;
2222 else if (TREE_CODE (e
) == COMPONENT_REF
2223 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
)
2224 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e
, 1)));
2225 else if (is_overloaded_fn (e
))
2227 if (complain
& tf_error
)
2228 permerror (e_loc
, "ISO C++ forbids applying %<__alignof%> to "
2229 "an expression of function type");
2231 return error_mark_node
;
2232 if (TREE_CODE (e
) == FUNCTION_DECL
)
2233 t
= size_int (DECL_ALIGN_UNIT (e
));
2237 else if (type_unknown_p (e
))
2239 if (complain
& tf_error
)
2240 cxx_incomplete_type_error (e_loc
, e
, TREE_TYPE (e
));
2242 return error_mark_node
;
2246 return cxx_sizeof_or_alignof_type (loc
, TREE_TYPE (e
),
2247 ALIGNOF_EXPR
, std_alignof
,
2248 complain
& tf_error
);
2250 return fold_convert_loc (loc
, size_type_node
, t
);
2253 /* Process a sizeof or alignof expression E with code OP where the operand
2254 is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2257 cxx_sizeof_or_alignof_expr (location_t loc
, tree e
, enum tree_code op
,
2258 bool std_alignof
, bool complain
)
2260 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
2261 if (op
== SIZEOF_EXPR
)
2262 return cxx_sizeof_expr (loc
, e
, complain
? tf_warning_or_error
: tf_none
);
2264 return cxx_alignof_expr (loc
, e
, std_alignof
,
2265 complain
? tf_warning_or_error
: tf_none
);
2268 /* Build a representation of an expression 'alignas(E).' Return the
2269 folded integer value of E if it is an integral constant expression
2270 that resolves to a valid alignment. If E depends on a template
2271 parameter, return a syntactic representation tree of kind
2272 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2273 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2276 cxx_alignas_expr (tree e
)
2278 if (e
== NULL_TREE
|| e
== error_mark_node
2279 || (!TYPE_P (e
) && !require_potential_rvalue_constant_expression (e
)))
2285 When the alignment-specifier is of the form
2286 alignas(type-id), it shall have the same effect as
2287 alignas(alignof(type-id)). */
2289 return cxx_sizeof_or_alignof_type (input_location
,
2291 /*std_alignof=*/true,
2294 /* If we reach this point, it means the alignas expression if of
2295 the form "alignas(assignment-expression)", so we should follow
2296 what is stated by [dcl.align]/2. */
2298 if (value_dependent_expression_p (e
))
2299 /* Leave value-dependent expression alone for now. */
2302 e
= instantiate_non_dependent_expr (e
);
2303 e
= mark_rvalue_use (e
);
2305 /* [dcl.align]/2 says:
2307 the assignment-expression shall be an integral constant
2310 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e
)))
2312 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e
));
2313 return error_mark_node
;
2316 return cxx_constant_value (e
);
2320 /* EXPR is being used in a context that is not a function call.
2325 The expression can be used only as the left-hand operand of a
2326 member function call.
2328 [expr.mptr.operator]
2330 If the result of .* or ->* is a function, then that result can be
2331 used only as the operand for the function call operator ().
2333 by issuing an error message if appropriate. Returns true iff EXPR
2334 violates these rules. */
2337 invalid_nonstatic_memfn_p (location_t loc
, tree expr
, tsubst_flags_t complain
)
2339 if (expr
== NULL_TREE
)
2341 /* Don't enforce this in MS mode. */
2342 if (flag_ms_extensions
)
2344 if (is_overloaded_fn (expr
) && !really_overloaded_fn (expr
))
2345 expr
= get_first_fn (expr
);
2346 if (TREE_TYPE (expr
)
2347 && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr
))
2349 if (complain
& tf_error
)
2353 error_at (loc
, "invalid use of non-static member function %qD",
2355 inform (DECL_SOURCE_LOCATION (expr
), "declared here");
2358 error_at (loc
, "invalid use of non-static member function of "
2359 "type %qT", TREE_TYPE (expr
));
2366 /* If EXP is a reference to a bit-field, and the type of EXP does not
2367 match the declared type of the bit-field, return the declared type
2368 of the bit-field. Otherwise, return NULL_TREE. */
2371 is_bitfield_expr_with_lowered_type (const_tree exp
)
2373 switch (TREE_CODE (exp
))
2376 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1)
2377 ? TREE_OPERAND (exp
, 1)
2378 : TREE_OPERAND (exp
, 0)))
2380 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 2));
2383 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1));
2387 case UNARY_PLUS_EXPR
:
2388 case PREDECREMENT_EXPR
:
2389 case PREINCREMENT_EXPR
:
2390 case POSTDECREMENT_EXPR
:
2391 case POSTINCREMENT_EXPR
:
2393 case NON_LVALUE_EXPR
:
2395 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
2401 field
= TREE_OPERAND (exp
, 1);
2402 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_BIT_FIELD_TYPE (field
))
2404 if (same_type_ignoring_top_level_qualifiers_p
2405 (TREE_TYPE (exp
), DECL_BIT_FIELD_TYPE (field
)))
2407 return DECL_BIT_FIELD_TYPE (field
);
2411 if (DECL_HAS_VALUE_EXPR_P (exp
))
2412 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2413 (CONST_CAST_TREE (exp
)));
2416 case VIEW_CONVERT_EXPR
:
2417 if (location_wrapper_p (exp
))
2418 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
2427 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2428 bitfield with a lowered type, the type of EXP is returned, rather
2432 unlowered_expr_type (const_tree exp
)
2435 tree etype
= TREE_TYPE (exp
);
2437 type
= is_bitfield_expr_with_lowered_type (exp
);
2439 type
= cp_build_qualified_type (type
, cp_type_quals (etype
));
2446 /* Perform the conversions in [expr] that apply when an lvalue appears
2447 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2448 function-to-pointer conversions. In addition, bitfield references are
2449 converted to their declared types. Note that this function does not perform
2450 the lvalue-to-rvalue conversion for class types. If you need that conversion
2451 for class types, then you probably need to use force_rvalue.
2453 Although the returned value is being used as an rvalue, this
2454 function does not wrap the returned expression in a
2455 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2456 that the return value is no longer an lvalue. */
2459 decay_conversion (tree exp
,
2460 tsubst_flags_t complain
,
2461 bool reject_builtin
/* = true */)
2464 enum tree_code code
;
2465 location_t loc
= cp_expr_loc_or_input_loc (exp
);
2467 type
= TREE_TYPE (exp
);
2468 if (type
== error_mark_node
)
2469 return error_mark_node
;
2471 exp
= resolve_nondeduced_context_or_error (exp
, complain
);
2473 code
= TREE_CODE (type
);
2475 if (error_operand_p (exp
))
2476 return error_mark_node
;
2478 if (NULLPTR_TYPE_P (type
) && !TREE_SIDE_EFFECTS (exp
))
2480 mark_rvalue_use (exp
, loc
, reject_builtin
);
2481 return nullptr_node
;
2484 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2485 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2486 if (code
== VOID_TYPE
)
2488 if (complain
& tf_error
)
2489 error_at (loc
, "void value not ignored as it ought to be");
2490 return error_mark_node
;
2492 if (invalid_nonstatic_memfn_p (loc
, exp
, complain
))
2493 return error_mark_node
;
2494 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
2496 exp
= mark_lvalue_use (exp
);
2497 if (reject_builtin
&& reject_gcc_builtin (exp
, loc
))
2498 return error_mark_node
;
2499 return cp_build_addr_expr (exp
, complain
);
2501 if (code
== ARRAY_TYPE
)
2506 exp
= mark_lvalue_use (exp
);
2508 if (INDIRECT_REF_P (exp
))
2509 return build_nop (build_pointer_type (TREE_TYPE (type
)),
2510 TREE_OPERAND (exp
, 0));
2512 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
2514 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1), complain
);
2515 if (op1
== error_mark_node
)
2516 return error_mark_node
;
2517 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
2518 TREE_OPERAND (exp
, 0), op1
);
2521 if (!obvalue_p (exp
)
2522 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
2524 if (complain
& tf_error
)
2525 error_at (loc
, "invalid use of non-lvalue array");
2526 return error_mark_node
;
2529 /* Don't let an array compound literal decay to a pointer. It can
2530 still be used to initialize an array or bind to a reference. */
2531 if (TREE_CODE (exp
) == TARGET_EXPR
)
2533 if (complain
& tf_error
)
2534 error_at (loc
, "taking address of temporary array");
2535 return error_mark_node
;
2538 ptrtype
= build_pointer_type (TREE_TYPE (type
));
2542 if (!cxx_mark_addressable (exp
))
2543 return error_mark_node
;
2544 adr
= build_nop (ptrtype
, build_address (exp
));
2547 /* This way is better for a COMPONENT_REF since it can
2548 simplify the offset for a component. */
2549 adr
= cp_build_addr_expr (exp
, complain
);
2550 return cp_convert (ptrtype
, adr
, complain
);
2553 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2554 exp
= mark_rvalue_use (exp
, loc
, reject_builtin
);
2556 /* If a bitfield is used in a context where integral promotion
2557 applies, then the caller is expected to have used
2558 default_conversion. That function promotes bitfields correctly
2559 before calling this function. At this point, if we have a
2560 bitfield referenced, we may assume that is not subject to
2561 promotion, and that, therefore, the type of the resulting rvalue
2562 is the declared type of the bitfield. */
2563 exp
= convert_bitfield_to_declared_type (exp
);
2565 /* We do not call rvalue() here because we do not want to wrap EXP
2566 in a NON_LVALUE_EXPR. */
2570 Non-class rvalues always have cv-unqualified types. */
2571 type
= TREE_TYPE (exp
);
2572 if (!CLASS_TYPE_P (type
) && cv_qualified_p (type
))
2573 exp
= build_nop (cv_unqualified (type
), exp
);
2575 if (!complete_type_or_maybe_complain (type
, exp
, complain
))
2576 return error_mark_node
;
2581 /* Perform preparatory conversions, as part of the "usual arithmetic
2582 conversions". In particular, as per [expr]:
2584 Whenever an lvalue expression appears as an operand of an
2585 operator that expects the rvalue for that operand, the
2586 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2587 standard conversions are applied to convert the expression to an
2590 In addition, we perform integral promotions here, as those are
2591 applied to both operands to a binary operator before determining
2592 what additional conversions should apply. */
2595 cp_default_conversion (tree exp
, tsubst_flags_t complain
)
2597 /* Check for target-specific promotions. */
2598 tree promoted_type
= targetm
.promoted_type (TREE_TYPE (exp
));
2600 exp
= cp_convert (promoted_type
, exp
, complain
);
2601 /* Perform the integral promotions first so that bitfield
2602 expressions (which may promote to "int", even if the bitfield is
2603 declared "unsigned") are promoted correctly. */
2604 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp
)))
2605 exp
= cp_perform_integral_promotions (exp
, complain
);
2606 /* Perform the other conversions. */
2607 exp
= decay_conversion (exp
, complain
);
2615 default_conversion (tree exp
)
2617 return cp_default_conversion (exp
, tf_warning_or_error
);
2620 /* EXPR is an expression with an integral or enumeration type.
2621 Perform the integral promotions in [conv.prom], and return the
2625 cp_perform_integral_promotions (tree expr
, tsubst_flags_t complain
)
2630 expr
= mark_rvalue_use (expr
);
2631 if (error_operand_p (expr
))
2632 return error_mark_node
;
2634 type
= TREE_TYPE (expr
);
2638 A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2639 of type int if int can represent all the values of the bit-field;
2640 otherwise, it can be converted to unsigned int if unsigned int can
2641 represent all the values of the bit-field. If the bit-field is larger yet,
2642 no integral promotion applies to it. If the bit-field has an enumerated
2643 type, it is treated as any other value of that type for promotion
2645 tree bitfield_type
= is_bitfield_expr_with_lowered_type (expr
);
2647 && (TREE_CODE (bitfield_type
) == ENUMERAL_TYPE
2648 || TYPE_PRECISION (type
) > TYPE_PRECISION (integer_type_node
)))
2649 type
= bitfield_type
;
2651 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
2652 /* Scoped enums don't promote. */
2653 if (SCOPED_ENUM_P (type
))
2655 promoted_type
= type_promotes_to (type
);
2656 if (type
!= promoted_type
)
2657 expr
= cp_convert (promoted_type
, expr
, complain
);
2658 else if (bitfield_type
&& bitfield_type
!= type
)
2659 /* Prevent decay_conversion from converting to bitfield_type. */
2660 expr
= build_nop (type
, expr
);
2667 perform_integral_promotions (tree expr
)
2669 return cp_perform_integral_promotions (expr
, tf_warning_or_error
);
2672 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2673 decay_conversion to one. */
2676 string_conv_p (const_tree totype
, const_tree exp
, int warn
)
2680 if (!TYPE_PTR_P (totype
))
2683 t
= TREE_TYPE (totype
);
2684 if (!same_type_p (t
, char_type_node
)
2685 && !same_type_p (t
, char8_type_node
)
2686 && !same_type_p (t
, char16_type_node
)
2687 && !same_type_p (t
, char32_type_node
)
2688 && !same_type_p (t
, wchar_type_node
))
2691 location_t loc
= EXPR_LOC_OR_LOC (exp
, input_location
);
2693 STRIP_ANY_LOCATION_WRAPPER (exp
);
2695 if (TREE_CODE (exp
) == STRING_CST
)
2697 /* Make sure that we don't try to convert between char and wide chars. */
2698 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
2703 /* Is this a string constant which has decayed to 'const char *'? */
2704 t
= build_pointer_type (cp_build_qualified_type (t
, TYPE_QUAL_CONST
));
2705 if (!same_type_p (TREE_TYPE (exp
), t
))
2708 if (TREE_CODE (exp
) != ADDR_EXPR
2709 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
2714 if (cxx_dialect
>= cxx11
)
2715 pedwarn (loc
, OPT_Wwrite_strings
,
2716 "ISO C++ forbids converting a string constant to %qT",
2719 warning_at (loc
, OPT_Wwrite_strings
,
2720 "deprecated conversion from string constant to %qT",
2727 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2728 can, for example, use as an lvalue. This code used to be in
2729 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2730 expressions, where we're dealing with aggregates. But now it's again only
2731 called from unary_complex_lvalue. The case (in particular) that led to
2732 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2736 rationalize_conditional_expr (enum tree_code code
, tree t
,
2737 tsubst_flags_t complain
)
2739 location_t loc
= cp_expr_loc_or_input_loc (t
);
2741 /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that
2742 the first operand is always the one to be used if both operands
2743 are equal, so we know what conditional expression this used to be. */
2744 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
2746 tree op0
= TREE_OPERAND (t
, 0);
2747 tree op1
= TREE_OPERAND (t
, 1);
2749 /* The following code is incorrect if either operand side-effects. */
2750 gcc_assert (!TREE_SIDE_EFFECTS (op0
)
2751 && !TREE_SIDE_EFFECTS (op1
));
2753 build_conditional_expr (loc
,
2754 build_x_binary_op (loc
,
2755 (TREE_CODE (t
) == MIN_EXPR
2756 ? LE_EXPR
: GE_EXPR
),
2757 op0
, TREE_CODE (op0
),
2758 op1
, TREE_CODE (op1
),
2762 cp_build_unary_op (code
, op0
, false, complain
),
2763 cp_build_unary_op (code
, op1
, false, complain
),
2767 tree op1
= TREE_OPERAND (t
, 1);
2768 if (TREE_CODE (op1
) != THROW_EXPR
)
2769 op1
= cp_build_unary_op (code
, op1
, false, complain
);
2770 tree op2
= TREE_OPERAND (t
, 2);
2771 if (TREE_CODE (op2
) != THROW_EXPR
)
2772 op2
= cp_build_unary_op (code
, op2
, false, complain
);
2775 build_conditional_expr (loc
, TREE_OPERAND (t
, 0), op1
, op2
, complain
);
2778 /* Given the TYPE of an anonymous union field inside T, return the
2779 FIELD_DECL for the field. If not found return NULL_TREE. Because
2780 anonymous unions can nest, we must also search all anonymous unions
2781 that are directly reachable. */
2784 lookup_anon_field (tree
, tree type
)
2788 type
= TYPE_MAIN_VARIANT (type
);
2789 field
= ANON_AGGR_TYPE_FIELD (type
);
2794 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2795 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2796 non-NULL, it indicates the path to the base used to name MEMBER.
2797 If PRESERVE_REFERENCE is true, the expression returned will have
2798 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2799 returned will have the type referred to by the reference.
2801 This function does not perform access control; that is either done
2802 earlier by the parser when the name of MEMBER is resolved to MEMBER
2803 itself, or later when overload resolution selects one of the
2804 functions indicated by MEMBER. */
2807 build_class_member_access_expr (cp_expr object
, tree member
,
2808 tree access_path
, bool preserve_reference
,
2809 tsubst_flags_t complain
)
2813 tree result
= NULL_TREE
;
2814 tree using_decl
= NULL_TREE
;
2816 if (error_operand_p (object
) || error_operand_p (member
))
2817 return error_mark_node
;
2819 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2823 The type of the first expression shall be "class object" (of a
2825 object_type
= TREE_TYPE (object
);
2826 if (!currently_open_class (object_type
)
2827 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2828 return error_mark_node
;
2829 if (!CLASS_TYPE_P (object_type
))
2831 if (complain
& tf_error
)
2833 if (INDIRECT_TYPE_P (object_type
)
2834 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2835 error ("request for member %qD in %qE, which is of pointer "
2836 "type %qT (maybe you meant to use %<->%> ?)",
2837 member
, object
.get_value (), object_type
);
2839 error ("request for member %qD in %qE, which is of non-class "
2840 "type %qT", member
, object
.get_value (), object_type
);
2842 return error_mark_node
;
2845 /* The standard does not seem to actually say that MEMBER must be a
2846 member of OBJECT_TYPE. However, that is clearly what is
2848 if (DECL_P (member
))
2850 member_scope
= DECL_CLASS_CONTEXT (member
);
2851 if (!mark_used (member
, complain
) && !(complain
& tf_error
))
2852 return error_mark_node
;
2854 if (TREE_UNAVAILABLE (member
))
2855 error_unavailable_use (member
, NULL_TREE
);
2856 else if (TREE_DEPRECATED (member
))
2857 warn_deprecated_use (member
, NULL_TREE
);
2860 member_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (member
));
2861 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2862 presently be the anonymous union. Go outwards until we find a
2863 type related to OBJECT_TYPE. */
2864 while ((ANON_AGGR_TYPE_P (member_scope
) || UNSCOPED_ENUM_P (member_scope
))
2865 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
2867 member_scope
= TYPE_CONTEXT (member_scope
);
2868 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
2870 if (complain
& tf_error
)
2872 if (TREE_CODE (member
) == FIELD_DECL
)
2873 error ("invalid use of non-static data member %qE", member
);
2875 error ("%qD is not a member of %qT", member
, object_type
);
2877 return error_mark_node
;
2880 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2881 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2882 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2883 if (tree temp
= unary_complex_lvalue (ADDR_EXPR
, object
))
2885 temp
= cp_build_fold_indirect_ref (temp
);
2886 if (!lvalue_p (object
) && lvalue_p (temp
))
2887 /* Preserve rvalueness. */
2892 /* In [expr.ref], there is an explicit list of the valid choices for
2893 MEMBER. We check for each of those cases here. */
2896 /* A static data member. */
2898 mark_exp_read (object
);
2900 if (tree wrap
= maybe_get_tls_wrapper_call (result
))
2901 /* Replace an evaluated use of the thread_local variable with
2902 a call to its wrapper. */
2905 /* If OBJECT has side-effects, they are supposed to occur. */
2906 if (TREE_SIDE_EFFECTS (object
))
2907 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
2909 else if (TREE_CODE (member
) == FIELD_DECL
)
2911 /* A non-static data member. */
2916 if (INDIRECT_REF_P (object
))
2918 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object
, 0)));
2920 null_object_p
= false;
2922 /* Convert OBJECT to the type of MEMBER. */
2923 if (!same_type_p (TYPE_MAIN_VARIANT (object_type
),
2924 TYPE_MAIN_VARIANT (member_scope
)))
2929 /* We didn't complain above about a currently open class, but now we
2930 must: we don't know how to refer to a base member before layout is
2931 complete. But still don't complain in a template. */
2932 if (!cp_unevaluated_operand
2933 && !dependent_type_p (object_type
)
2934 && !complete_type_or_maybe_complain (object_type
, object
,
2936 return error_mark_node
;
2938 binfo
= lookup_base (access_path
? access_path
: object_type
,
2939 member_scope
, ba_unique
, &kind
, complain
);
2940 if (binfo
== error_mark_node
)
2941 return error_mark_node
;
2943 /* It is invalid to try to get to a virtual base of a
2944 NULL object. The most common cause is invalid use of
2946 if (null_object_p
&& kind
== bk_via_virtual
)
2948 if (complain
& tf_error
)
2950 error ("invalid access to non-static data member %qD in "
2951 "virtual base of NULL object", member
);
2953 return error_mark_node
;
2956 /* Convert to the base. */
2957 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
2958 /*nonnull=*/1, complain
);
2959 /* If we found the base successfully then we should be able
2960 to convert to it successfully. */
2961 gcc_assert (object
!= error_mark_node
);
2964 /* If MEMBER is from an anonymous aggregate, we have converted
2965 OBJECT so that it refers to the class containing the
2966 anonymous union. Generate a reference to the anonymous union
2967 itself, and recur to find MEMBER. */
2968 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
))
2969 /* When this code is called from build_field_call, the
2970 object already has the type of the anonymous union.
2971 That is because the COMPONENT_REF was already
2972 constructed, and was then disassembled before calling
2973 build_field_call. After the function-call code is
2974 cleaned up, this waste can be eliminated. */
2975 && (!same_type_ignoring_top_level_qualifiers_p
2976 (TREE_TYPE (object
), DECL_CONTEXT (member
))))
2978 tree anonymous_union
;
2980 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
2981 DECL_CONTEXT (member
));
2982 object
= build_class_member_access_expr (object
,
2984 /*access_path=*/NULL_TREE
,
2989 /* Compute the type of the field, as described in [expr.ref]. */
2990 type_quals
= TYPE_UNQUALIFIED
;
2991 member_type
= TREE_TYPE (member
);
2992 if (!TYPE_REF_P (member_type
))
2994 type_quals
= (cp_type_quals (member_type
)
2995 | cp_type_quals (object_type
));
2997 /* A field is const (volatile) if the enclosing object, or the
2998 field itself, is const (volatile). But, a mutable field is
2999 not const, even within a const object. */
3000 if (DECL_MUTABLE_P (member
))
3001 type_quals
&= ~TYPE_QUAL_CONST
;
3002 member_type
= cp_build_qualified_type (member_type
, type_quals
);
3005 result
= build3_loc (input_location
, COMPONENT_REF
, member_type
,
3006 object
, member
, NULL_TREE
);
3008 /* Mark the expression const or volatile, as appropriate. Even
3009 though we've dealt with the type above, we still have to mark the
3010 expression itself. */
3011 if (type_quals
& TYPE_QUAL_CONST
)
3012 TREE_READONLY (result
) = 1;
3013 if (type_quals
& TYPE_QUAL_VOLATILE
)
3014 TREE_THIS_VOLATILE (result
) = 1;
3016 else if (BASELINK_P (member
))
3018 /* The member is a (possibly overloaded) member function. */
3022 /* If the MEMBER is exactly one static member function, then we
3023 know the type of the expression. Otherwise, we must wait
3024 until overload resolution has been performed. */
3025 functions
= BASELINK_FUNCTIONS (member
);
3026 if (TREE_CODE (functions
) == FUNCTION_DECL
3027 && DECL_STATIC_FUNCTION_P (functions
))
3028 type
= TREE_TYPE (functions
);
3030 type
= unknown_type_node
;
3031 /* Note that we do not convert OBJECT to the BASELINK_BINFO
3032 base. That will happen when the function is called. */
3033 result
= build3_loc (input_location
, COMPONENT_REF
, type
, object
, member
,
3036 else if (TREE_CODE (member
) == CONST_DECL
)
3038 /* The member is an enumerator. */
3040 /* If OBJECT has side-effects, they are supposed to occur. */
3041 if (TREE_SIDE_EFFECTS (object
))
3042 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
3045 else if ((using_decl
= strip_using_decl (member
)) != member
)
3046 result
= build_class_member_access_expr (object
,
3048 access_path
, preserve_reference
,
3052 if (complain
& tf_error
)
3053 error ("invalid use of %qD", member
);
3054 return error_mark_node
;
3057 if (!preserve_reference
)
3060 If E2 is declared to have type "reference to T", then ... the
3061 type of E1.E2 is T. */
3062 result
= convert_from_reference (result
);
3067 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
3068 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
3071 lookup_destructor (tree object
, tree scope
, tree dtor_name
,
3072 tsubst_flags_t complain
)
3074 tree object_type
= TREE_TYPE (object
);
3075 tree dtor_type
= TREE_OPERAND (dtor_name
, 0);
3078 /* We've already complained about this destructor. */
3079 if (dtor_type
== error_mark_node
)
3080 return error_mark_node
;
3082 if (scope
&& !check_dtor_name (scope
, dtor_type
))
3084 if (complain
& tf_error
)
3085 error ("qualified type %qT does not match destructor name ~%qT",
3087 return error_mark_node
;
3089 if (is_auto (dtor_type
))
3090 dtor_type
= object_type
;
3091 else if (identifier_p (dtor_type
))
3093 /* In a template, names we can't find a match for are still accepted
3094 destructor names, and we check them here. */
3095 if (check_dtor_name (object_type
, dtor_type
))
3096 dtor_type
= object_type
;
3099 if (complain
& tf_error
)
3100 error ("object type %qT does not match destructor name ~%qT",
3101 object_type
, dtor_type
);
3102 return error_mark_node
;
3106 else if (!DERIVED_FROM_P (dtor_type
, TYPE_MAIN_VARIANT (object_type
)))
3108 if (complain
& tf_error
)
3109 error ("the type being destroyed is %qT, but the destructor "
3110 "refers to %qT", TYPE_MAIN_VARIANT (object_type
), dtor_type
);
3111 return error_mark_node
;
3113 expr
= lookup_member (dtor_type
, complete_dtor_identifier
,
3114 /*protect=*/1, /*want_type=*/false,
3115 tf_warning_or_error
);
3118 if (complain
& tf_error
)
3119 cxx_incomplete_type_error (dtor_name
, dtor_type
);
3120 return error_mark_node
;
3122 expr
= (adjust_result_of_qualified_name_lookup
3123 (expr
, dtor_type
, object_type
));
3124 if (scope
== NULL_TREE
)
3125 /* We need to call adjust_result_of_qualified_name_lookup in case the
3126 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
3127 that we still get virtual function binding. */
3128 BASELINK_QUALIFIED_P (expr
) = false;
3132 /* An expression of the form "A::template B" has been resolved to
3133 DECL. Issue a diagnostic if B is not a template or template
3137 check_template_keyword (tree decl
)
3139 /* The standard says:
3143 If a name prefixed by the keyword template is not a member
3144 template, the program is ill-formed.
3146 DR 228 removed the restriction that the template be a member
3149 DR 96, if accepted would add the further restriction that explicit
3150 template arguments must be provided if the template keyword is
3151 used, but, as of 2005-10-16, that DR is still in "drafting". If
3152 this DR is accepted, then the semantic checks here can be
3153 simplified, as the entity named must in fact be a template
3154 specialization, rather than, as at present, a set of overloaded
3155 functions containing at least one template function. */
3156 if (TREE_CODE (decl
) != TEMPLATE_DECL
3157 && TREE_CODE (decl
) != TEMPLATE_ID_EXPR
)
3161 if (DECL_USE_TEMPLATE (decl
)
3162 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
3165 permerror (input_location
, "%qD is not a template", decl
);
3167 else if (!is_overloaded_fn (decl
))
3168 permerror (input_location
, "%qD is not a template", decl
);
3173 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
3174 !found
&& iter
; ++iter
)
3177 if (TREE_CODE (fn
) == TEMPLATE_DECL
3178 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
3179 || (TREE_CODE (fn
) == FUNCTION_DECL
3180 && DECL_USE_TEMPLATE (fn
)
3181 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn
))))
3185 permerror (input_location
, "%qD is not a template", decl
);
3190 /* Record that an access failure occurred on BASETYPE_PATH attempting
3191 to access DECL, where DIAG_DECL should be used for diagnostics. */
3194 access_failure_info::record_access_failure (tree basetype_path
,
3195 tree decl
, tree diag_decl
)
3197 m_was_inaccessible
= true;
3198 m_basetype_path
= basetype_path
;
3200 m_diag_decl
= diag_decl
;
3203 /* If an access failure was recorded, then attempt to locate an
3204 accessor function for the pertinent field.
3205 Otherwise, return NULL_TREE. */
3208 access_failure_info::get_any_accessor (bool const_p
) const
3210 if (!was_inaccessible_p ())
3214 = locate_field_accessor (m_basetype_path
, m_diag_decl
, const_p
);
3218 /* The accessor must itself be accessible for it to be a reasonable
3220 if (!accessible_p (m_basetype_path
, accessor
, true))
3226 /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3227 replacing the primary location in RICHLOC with "accessor()". */
3230 access_failure_info::add_fixit_hint (rich_location
*richloc
,
3234 pp_string (&pp
, IDENTIFIER_POINTER (DECL_NAME (accessor_decl
)));
3235 pp_string (&pp
, "()");
3236 richloc
->add_fixit_replace (pp_formatted_text (&pp
));
3239 /* If an access failure was recorded, then attempt to locate an
3240 accessor function for the pertinent field, and if one is
3241 available, add a note and fix-it hint suggesting using it. */
3244 access_failure_info::maybe_suggest_accessor (bool const_p
) const
3246 tree accessor
= get_any_accessor (const_p
);
3247 if (accessor
== NULL_TREE
)
3249 rich_location
richloc (line_table
, input_location
);
3250 add_fixit_hint (&richloc
, accessor
);
3251 inform (&richloc
, "field %q#D can be accessed via %q#D",
3252 m_diag_decl
, accessor
);
3255 /* Subroutine of finish_class_member_access_expr.
3256 Issue an error about NAME not being a member of ACCESS_PATH (or
3257 OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3261 complain_about_unrecognized_member (tree access_path
, tree name
,
3264 /* Attempt to provide a hint about misspelled names. */
3265 tree guessed_id
= lookup_member_fuzzy (access_path
, name
,
3266 /*want_type=*/false);
3267 if (guessed_id
== NULL_TREE
)
3270 error ("%q#T has no member named %qE",
3271 TREE_CODE (access_path
) == TREE_BINFO
3272 ? TREE_TYPE (access_path
) : object_type
, name
);
3276 location_t bogus_component_loc
= input_location
;
3277 gcc_rich_location
rich_loc (bogus_component_loc
);
3279 /* Check that the guessed name is accessible along access_path. */
3280 access_failure_info afi
;
3281 lookup_member (access_path
, guessed_id
, /*protect=*/1,
3282 /*want_type=*/false, /*complain=*/false,
3284 if (afi
.was_inaccessible_p ())
3286 tree accessor
= afi
.get_any_accessor (TYPE_READONLY (object_type
));
3289 /* The guessed name isn't directly accessible, but can be accessed
3290 via an accessor member function. */
3291 afi
.add_fixit_hint (&rich_loc
, accessor
);
3292 error_at (&rich_loc
,
3293 "%q#T has no member named %qE;"
3294 " did you mean %q#D? (accessible via %q#D)",
3295 TREE_CODE (access_path
) == TREE_BINFO
3296 ? TREE_TYPE (access_path
) : object_type
,
3297 name
, afi
.get_diag_decl (), accessor
);
3301 /* The guessed name isn't directly accessible, and no accessor
3302 member function could be found. */
3303 error_at (&rich_loc
,
3304 "%q#T has no member named %qE;"
3305 " did you mean %q#D? (not accessible from this context)",
3306 TREE_CODE (access_path
) == TREE_BINFO
3307 ? TREE_TYPE (access_path
) : object_type
,
3308 name
, afi
.get_diag_decl ());
3309 complain_about_access (afi
.get_decl (), afi
.get_diag_decl (),
3310 afi
.get_diag_decl (), false, ak_none
);
3315 /* The guessed name is directly accessible; suggest it. */
3316 rich_loc
.add_fixit_misspelled_id (bogus_component_loc
,
3318 error_at (&rich_loc
,
3319 "%q#T has no member named %qE;"
3320 " did you mean %qE?",
3321 TREE_CODE (access_path
) == TREE_BINFO
3322 ? TREE_TYPE (access_path
) : object_type
,
3327 /* This function is called by the parser to process a class member
3328 access expression of the form OBJECT.NAME. NAME is a node used by
3329 the parser to represent a name; it is not yet a DECL. It may,
3330 however, be a BASELINK where the BASELINK_FUNCTIONS is a
3331 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3332 there is no reason to do the lookup twice, so the parser keeps the
3333 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3334 be a template via the use of the "A::template B" syntax. */
3337 finish_class_member_access_expr (cp_expr object
, tree name
, bool template_p
,
3338 tsubst_flags_t complain
)
3343 tree access_path
= NULL_TREE
;
3344 tree orig_object
= object
;
3345 tree orig_name
= name
;
3347 if (object
== error_mark_node
|| name
== error_mark_node
)
3348 return error_mark_node
;
3350 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3351 if (!objc_is_public (object
, name
))
3352 return error_mark_node
;
3354 object_type
= TREE_TYPE (object
);
3356 if (processing_template_decl
)
3358 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3359 type_dependent_object_expression_p (object
)
3360 /* If NAME is "f<args>", where either 'f' or 'args' is
3361 dependent, then the expression is dependent. */
3362 || (TREE_CODE (name
) == TEMPLATE_ID_EXPR
3363 && dependent_template_id_p (TREE_OPERAND (name
, 0),
3364 TREE_OPERAND (name
, 1)))
3365 /* If NAME is "T::X" where "T" is dependent, then the
3366 expression is dependent. */
3367 || (TREE_CODE (name
) == SCOPE_REF
3368 && TYPE_P (TREE_OPERAND (name
, 0))
3369 && dependent_scope_p (TREE_OPERAND (name
, 0)))
3370 /* If NAME is operator T where "T" is dependent, we can't
3371 lookup until we instantiate the T. */
3372 || (TREE_CODE (name
) == IDENTIFIER_NODE
3373 && IDENTIFIER_CONV_OP_P (name
)
3374 && dependent_type_p (TREE_TYPE (name
))))
3377 return build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
,
3378 orig_object
, orig_name
, NULL_TREE
);
3380 object
= build_non_dependent_expr (object
);
3382 else if (c_dialect_objc ()
3383 && identifier_p (name
)
3384 && (expr
= objc_maybe_build_component_ref (object
, name
)))
3389 The type of the first expression shall be "class object" (of a
3391 if (!currently_open_class (object_type
)
3392 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
3393 return error_mark_node
;
3394 if (!CLASS_TYPE_P (object_type
))
3396 if (complain
& tf_error
)
3398 if (INDIRECT_TYPE_P (object_type
)
3399 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
3400 error ("request for member %qD in %qE, which is of pointer "
3401 "type %qT (maybe you meant to use %<->%> ?)",
3402 name
, object
.get_value (), object_type
);
3404 error ("request for member %qD in %qE, which is of non-class "
3405 "type %qT", name
, object
.get_value (), object_type
);
3407 return error_mark_node
;
3410 if (BASELINK_P (name
))
3411 /* A member function that has already been looked up. */
3415 bool is_template_id
= false;
3416 tree template_args
= NULL_TREE
;
3417 tree scope
= NULL_TREE
;
3419 access_path
= object_type
;
3421 if (TREE_CODE (name
) == SCOPE_REF
)
3423 /* A qualified name. The qualifying class or namespace `S'
3424 has already been looked up; it is either a TYPE or a
3426 scope
= TREE_OPERAND (name
, 0);
3427 name
= TREE_OPERAND (name
, 1);
3429 /* If SCOPE is a namespace, then the qualified name does not
3430 name a member of OBJECT_TYPE. */
3431 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
3433 if (complain
& tf_error
)
3434 error ("%<%D::%D%> is not a member of %qT",
3435 scope
, name
, object_type
);
3436 return error_mark_node
;
3440 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
3442 is_template_id
= true;
3443 template_args
= TREE_OPERAND (name
, 1);
3444 name
= TREE_OPERAND (name
, 0);
3446 if (!identifier_p (name
))
3447 name
= OVL_NAME (name
);
3452 if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
3454 gcc_assert (!is_template_id
);
3455 /* Looking up a member enumerator (c++/56793). */
3456 if (!TYPE_CLASS_SCOPE_P (scope
)
3457 || !DERIVED_FROM_P (TYPE_CONTEXT (scope
), object_type
))
3459 if (complain
& tf_error
)
3460 error ("%<%D::%D%> is not a member of %qT",
3461 scope
, name
, object_type
);
3462 return error_mark_node
;
3464 tree val
= lookup_enumerator (scope
, name
);
3467 if (complain
& tf_error
)
3468 error ("%qD is not a member of %qD",
3470 return error_mark_node
;
3473 if (TREE_SIDE_EFFECTS (object
))
3474 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
), object
, val
);
3478 gcc_assert (CLASS_TYPE_P (scope
));
3479 gcc_assert (identifier_p (name
) || TREE_CODE (name
) == BIT_NOT_EXPR
);
3481 if (constructor_name_p (name
, scope
))
3483 if (complain
& tf_error
)
3484 error ("cannot call constructor %<%T::%D%> directly",
3486 return error_mark_node
;
3489 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3490 access_path
= lookup_base (object_type
, scope
, ba_check
,
3492 if (access_path
== error_mark_node
)
3493 return error_mark_node
;
3496 if (any_dependent_bases_p (object_type
))
3498 if (complain
& tf_error
)
3499 error ("%qT is not a base of %qT", scope
, object_type
);
3500 return error_mark_node
;
3504 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
3506 if (dependent_type_p (object_type
))
3507 /* The destructor isn't declared yet. */
3509 member
= lookup_destructor (object
, scope
, name
, complain
);
3513 /* Look up the member. */
3514 access_failure_info afi
;
3515 if (processing_template_decl
)
3516 /* Even though this class member access expression is at this
3517 point not dependent, the member itself may be dependent, and
3518 we must not potentially push a access check for a dependent
3519 member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3520 ahead of time here; we're going to redo this member lookup at
3521 instantiation time anyway. */
3522 push_deferring_access_checks (dk_no_check
);
3523 member
= lookup_member (access_path
, name
, /*protect=*/1,
3524 /*want_type=*/false, complain
,
3526 if (processing_template_decl
)
3527 pop_deferring_access_checks ();
3528 afi
.maybe_suggest_accessor (TYPE_READONLY (object_type
));
3529 if (member
== NULL_TREE
)
3531 if (dependent_type_p (object_type
))
3532 /* Try again at instantiation time. */
3534 if (complain
& tf_error
)
3535 complain_about_unrecognized_member (access_path
, name
,
3537 return error_mark_node
;
3539 if (member
== error_mark_node
)
3540 return error_mark_node
;
3542 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member
)))
3543 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3544 wrong, so don't use it. */
3546 if (TREE_CODE (member
) == USING_DECL
&& DECL_DEPENDENT_P (member
))
3552 tree templ
= member
;
3554 if (BASELINK_P (templ
))
3555 member
= lookup_template_function (templ
, template_args
);
3556 else if (variable_template_p (templ
))
3557 member
= (lookup_and_finish_template_variable
3558 (templ
, template_args
, complain
));
3561 if (complain
& tf_error
)
3562 error ("%qD is not a member template function", name
);
3563 return error_mark_node
;
3568 if (TREE_UNAVAILABLE (member
))
3569 error_unavailable_use (member
, NULL_TREE
);
3570 else if (TREE_DEPRECATED (member
))
3571 warn_deprecated_use (member
, NULL_TREE
);
3574 check_template_keyword (member
);
3576 expr
= build_class_member_access_expr (object
, member
, access_path
,
3577 /*preserve_reference=*/false,
3579 if (processing_template_decl
&& expr
!= error_mark_node
)
3581 if (BASELINK_P (member
))
3583 if (TREE_CODE (orig_name
) == SCOPE_REF
)
3584 BASELINK_QUALIFIED_P (member
) = 1;
3587 return build_min_non_dep (COMPONENT_REF
, expr
,
3588 orig_object
, orig_name
,
3595 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3599 build_simple_component_ref (tree object
, tree member
)
3601 tree type
= cp_build_qualified_type (TREE_TYPE (member
),
3602 cp_type_quals (TREE_TYPE (object
)));
3603 return build3_loc (input_location
,
3604 COMPONENT_REF
, type
,
3605 object
, member
, NULL_TREE
);
3608 /* Return an expression for the MEMBER_NAME field in the internal
3609 representation of PTRMEM, a pointer-to-member function. (Each
3610 pointer-to-member function type gets its own RECORD_TYPE so it is
3611 more convenient to access the fields by name than by FIELD_DECL.)
3612 This routine converts the NAME to a FIELD_DECL and then creates the
3613 node for the complete expression. */
3616 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
3621 if (TREE_CODE (ptrmem
) == CONSTRUCTOR
)
3623 for (auto &e
: CONSTRUCTOR_ELTS (ptrmem
))
3624 if (e
.index
&& DECL_P (e
.index
) && DECL_NAME (e
.index
) == member_name
)
3629 /* This code is a stripped down version of
3630 build_class_member_access_expr. It does not work to use that
3631 routine directly because it expects the object to be of class
3633 ptrmem_type
= TREE_TYPE (ptrmem
);
3634 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
));
3635 for (member
= TYPE_FIELDS (ptrmem_type
); member
;
3636 member
= DECL_CHAIN (member
))
3637 if (DECL_NAME (member
) == member_name
)
3639 return build_simple_component_ref (ptrmem
, member
);
3642 /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3643 and for any other relevant operator. */
3646 op_unqualified_lookup (tree_code code
, bool is_assign
)
3648 tree lookups
= NULL_TREE
;
3650 if (cxx_dialect
>= cxx20
&& !is_assign
)
3652 if (code
== NE_EXPR
)
3654 /* != can get rewritten in terms of ==. */
3655 tree fnname
= ovl_op_identifier (false, EQ_EXPR
);
3656 if (tree fns
= lookup_name (fnname
, LOOK_where::BLOCK_NAMESPACE
))
3657 lookups
= tree_cons (fnname
, fns
, lookups
);
3659 else if (code
== GT_EXPR
|| code
== LE_EXPR
3660 || code
== LT_EXPR
|| code
== GE_EXPR
)
3662 /* These can get rewritten in terms of <=>. */
3663 tree fnname
= ovl_op_identifier (false, SPACESHIP_EXPR
);
3664 if (tree fns
= lookup_name (fnname
, LOOK_where::BLOCK_NAMESPACE
))
3665 lookups
= tree_cons (fnname
, fns
, lookups
);
3669 tree fnname
= ovl_op_identifier (is_assign
, code
);
3670 if (tree fns
= lookup_name (fnname
, LOOK_where::BLOCK_NAMESPACE
))
3671 lookups
= tree_cons (fnname
, fns
, lookups
);
3676 return build_tree_list (NULL_TREE
, NULL_TREE
);
3679 /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3680 the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3681 name lookup for the given operator. */
3684 build_dependent_operator_type (tree lookups
, tree_code code
, bool is_assign
)
3687 /* We're partially instantiating a dependent operator expression, and
3688 LOOKUPS is the result of phase 1 name lookup that we performed
3689 earlier at template definition time, so just reuse the corresponding
3690 DEPENDENT_OPERATOR_TYPE. */
3691 return TREE_TYPE (lookups
);
3693 /* Otherwise we're processing a dependent operator expression at template
3694 definition time, so perform phase 1 name lookup now. */
3695 lookups
= op_unqualified_lookup (code
, is_assign
);
3697 tree type
= cxx_make_type (DEPENDENT_OPERATOR_TYPE
);
3698 DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type
) = lookups
;
3699 TREE_TYPE (lookups
) = type
;
3703 /* Given an expression PTR for a pointer, return an expression
3704 for the value pointed to.
3705 ERRORSTRING is the name of the operator to appear in error messages.
3707 This function may need to overload OPERATOR_FNNAME.
3708 Must also handle REFERENCE_TYPEs for C++. */
3711 build_x_indirect_ref (location_t loc
, tree expr
, ref_operator errorstring
,
3712 tree lookups
, tsubst_flags_t complain
)
3714 tree orig_expr
= expr
;
3716 tree overload
= NULL_TREE
;
3718 if (processing_template_decl
)
3720 /* Retain the type if we know the operand is a pointer. */
3721 if (TREE_TYPE (expr
) && INDIRECT_TYPE_P (TREE_TYPE (expr
)))
3723 if (expr
== current_class_ptr
3724 || (TREE_CODE (expr
) == NOP_EXPR
3725 && TREE_OPERAND (expr
, 0) == current_class_ptr
3726 && (same_type_ignoring_top_level_qualifiers_p
3727 (TREE_TYPE (expr
), TREE_TYPE (current_class_ptr
)))))
3728 return current_class_ref
;
3729 return build_min (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
3731 if (type_dependent_expression_p (expr
))
3733 expr
= build_min_nt_loc (loc
, INDIRECT_REF
, expr
);
3735 = build_dependent_operator_type (lookups
, INDIRECT_REF
, false);
3738 expr
= build_non_dependent_expr (expr
);
3741 rval
= build_new_op (loc
, INDIRECT_REF
, LOOKUP_NORMAL
, expr
,
3742 NULL_TREE
, NULL_TREE
, lookups
,
3743 &overload
, complain
);
3745 rval
= cp_build_indirect_ref (loc
, expr
, errorstring
, complain
);
3747 if (processing_template_decl
&& rval
!= error_mark_node
)
3749 if (overload
!= NULL_TREE
)
3750 return (build_min_non_dep_op_overload
3751 (INDIRECT_REF
, rval
, overload
, orig_expr
));
3753 return build_min_non_dep (INDIRECT_REF
, rval
, orig_expr
);
3759 /* Like c-family strict_aliasing_warning, but don't warn for dependent
3760 types or expressions. */
3763 cp_strict_aliasing_warning (location_t loc
, tree type
, tree expr
)
3765 if (processing_template_decl
)
3769 if (dependent_type_p (type
) || type_dependent_expression_p (e
))
3772 return strict_aliasing_warning (loc
, type
, expr
);
3775 /* The implementation of the above, and of indirection implied by other
3776 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3779 cp_build_indirect_ref_1 (location_t loc
, tree ptr
, ref_operator errorstring
,
3780 tsubst_flags_t complain
, bool do_fold
)
3784 /* RO_NULL should only be used with the folding entry points below, not
3785 cp_build_indirect_ref. */
3786 gcc_checking_assert (errorstring
!= RO_NULL
|| do_fold
);
3788 if (ptr
== current_class_ptr
3789 || (TREE_CODE (ptr
) == NOP_EXPR
3790 && TREE_OPERAND (ptr
, 0) == current_class_ptr
3791 && (same_type_ignoring_top_level_qualifiers_p
3792 (TREE_TYPE (ptr
), TREE_TYPE (current_class_ptr
)))))
3793 return current_class_ref
;
3795 pointer
= (TYPE_REF_P (TREE_TYPE (ptr
))
3796 ? ptr
: decay_conversion (ptr
, complain
));
3797 if (pointer
== error_mark_node
)
3798 return error_mark_node
;
3800 type
= TREE_TYPE (pointer
);
3802 if (INDIRECT_TYPE_P (type
))
3806 If the type of the expression is "pointer to T," the type
3807 of the result is "T." */
3808 tree t
= TREE_TYPE (type
);
3810 if ((CONVERT_EXPR_P (ptr
)
3811 || TREE_CODE (ptr
) == VIEW_CONVERT_EXPR
)
3812 && (!CLASS_TYPE_P (t
) || !CLASSTYPE_EMPTY_P (t
)))
3814 /* If a warning is issued, mark it to avoid duplicates from
3815 the backend. This only needs to be done at
3816 warn_strict_aliasing > 2. */
3817 if (warn_strict_aliasing
> 2
3818 && cp_strict_aliasing_warning (EXPR_LOCATION (ptr
),
3819 type
, TREE_OPERAND (ptr
, 0)))
3820 suppress_warning (ptr
, OPT_Wstrict_aliasing
);
3823 if (VOID_TYPE_P (t
))
3825 /* A pointer to incomplete type (other than cv void) can be
3826 dereferenced [expr.unary.op]/1 */
3827 if (complain
& tf_error
)
3828 error_at (loc
, "%qT is not a pointer-to-object type", type
);
3829 return error_mark_node
;
3831 else if (do_fold
&& TREE_CODE (pointer
) == ADDR_EXPR
3832 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
3833 /* The POINTER was something like `&x'. We simplify `*&x' to
3835 return TREE_OPERAND (pointer
, 0);
3838 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
3840 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3841 so that we get the proper error message if the result is used
3842 to assign to. Also, &* is supposed to be a no-op. */
3843 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
3844 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
3845 TREE_SIDE_EFFECTS (ref
)
3846 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
));
3850 else if (!(complain
& tf_error
))
3851 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3853 /* `pointer' won't be an error_mark_node if we were given a
3854 pointer to member, so it's cool to check for this here. */
3855 else if (TYPE_PTRMEM_P (type
))
3856 switch (errorstring
)
3858 case RO_ARRAY_INDEXING
:
3860 "invalid use of array indexing on pointer to member");
3863 error_at (loc
, "invalid use of unary %<*%> on pointer to member");
3865 case RO_IMPLICIT_CONVERSION
:
3866 error_at (loc
, "invalid use of implicit conversion on pointer "
3870 error_at (loc
, "left hand operand of %<->*%> must be a pointer to "
3871 "class, but is a pointer to member of type %qT", type
);
3876 else if (pointer
!= error_mark_node
)
3877 invalid_indirection_error (loc
, type
, errorstring
);
3879 return error_mark_node
;
3882 /* Entry point used by c-common, which expects folding. */
3885 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errorstring
)
3887 return cp_build_indirect_ref_1 (loc
, ptr
, errorstring
,
3888 tf_warning_or_error
, true);
3891 /* Entry point used by internal indirection needs that don't correspond to any
3892 syntactic construct. */
3895 cp_build_fold_indirect_ref (tree pointer
)
3897 return cp_build_indirect_ref_1 (input_location
, pointer
, RO_NULL
,
3898 tf_warning_or_error
, true);
3901 /* Entry point used by indirection needs that correspond to some syntactic
3905 cp_build_indirect_ref (location_t loc
, tree ptr
, ref_operator errorstring
,
3906 tsubst_flags_t complain
)
3908 return cp_build_indirect_ref_1 (loc
, ptr
, errorstring
, complain
, false);
3911 /* This handles expressions of the form "a[i]", which denotes
3914 This is logically equivalent in C to *(a+i), but we may do it differently.
3915 If A is a variable or a member, we generate a primitive ARRAY_REF.
3916 This avoids forcing the array out of registers, and can work on
3917 arrays that are not lvalues (for example, members of structures returned
3920 If INDEX is of some user-defined type, it must be converted to
3921 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3922 will inherit the type of the array, which will be some pointer type.
3924 LOC is the location to use in building the array reference. */
3927 cp_build_array_ref (location_t loc
, tree array
, tree idx
,
3928 tsubst_flags_t complain
)
3934 if (complain
& tf_error
)
3935 error_at (loc
, "subscript missing in array reference");
3936 return error_mark_node
;
3939 if (TREE_TYPE (array
) == error_mark_node
3940 || TREE_TYPE (idx
) == error_mark_node
)
3941 return error_mark_node
;
3943 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3945 switch (TREE_CODE (array
))
3949 tree value
= cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3951 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3952 TREE_OPERAND (array
, 0), value
);
3953 SET_EXPR_LOCATION (ret
, loc
);
3958 ret
= build_conditional_expr
3959 (loc
, TREE_OPERAND (array
, 0),
3960 cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3962 cp_build_array_ref (loc
, TREE_OPERAND (array
, 2), idx
,
3965 protected_set_expr_location (ret
, loc
);
3972 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, idx
);
3974 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
3978 warn_array_subscript_with_type_char (loc
, idx
);
3980 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx
)))
3982 if (complain
& tf_error
)
3983 error_at (loc
, "array subscript is not an integer");
3984 return error_mark_node
;
3987 /* Apply integral promotions *after* noticing character types.
3988 (It is unclear why we do these promotions -- the standard
3989 does not say that we should. In fact, the natural thing would
3990 seem to be to convert IDX to ptrdiff_t; we're performing
3991 pointer arithmetic.) */
3992 idx
= cp_perform_integral_promotions (idx
, complain
);
3994 idx
= maybe_fold_non_dependent_expr (idx
, complain
);
3996 /* An array that is indexed by a non-constant
3997 cannot be stored in a register; we must be able to do
3998 address arithmetic on its address.
3999 Likewise an array of elements of variable size. */
4000 if (TREE_CODE (idx
) != INTEGER_CST
4001 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
4002 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
4005 if (!cxx_mark_addressable (array
, true))
4006 return error_mark_node
;
4009 /* An array that is indexed by a constant value which is not within
4010 the array bounds cannot be stored in a register either; because we
4011 would get a crash in store_bit_field/extract_bit_field when trying
4012 to access a non-existent part of the register. */
4013 if (TREE_CODE (idx
) == INTEGER_CST
4014 && TYPE_DOMAIN (TREE_TYPE (array
))
4015 && ! int_fits_type_p (idx
, TYPE_DOMAIN (TREE_TYPE (array
))))
4017 if (!cxx_mark_addressable (array
))
4018 return error_mark_node
;
4021 /* Note in C++ it is valid to subscript a `register' array, since
4022 it is valid to take the address of something with that
4023 storage specification. */
4027 while (TREE_CODE (foo
) == COMPONENT_REF
)
4028 foo
= TREE_OPERAND (foo
, 0);
4029 if (VAR_P (foo
) && DECL_REGISTER (foo
)
4030 && (complain
& tf_warning
))
4031 warning_at (loc
, OPT_Wextra
,
4032 "subscripting array declared %<register%>");
4035 type
= TREE_TYPE (TREE_TYPE (array
));
4036 rval
= build4 (ARRAY_REF
, type
, array
, idx
, NULL_TREE
, NULL_TREE
);
4037 /* Array ref is const/volatile if the array elements are
4038 or if the array is.. */
4039 TREE_READONLY (rval
)
4040 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
4041 TREE_SIDE_EFFECTS (rval
)
4042 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
4043 TREE_THIS_VOLATILE (rval
)
4044 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
4045 ret
= require_complete_type (rval
, complain
);
4046 protected_set_expr_location (ret
, loc
);
4048 ret
= non_lvalue_loc (loc
, ret
);
4053 tree ar
= cp_default_conversion (array
, complain
);
4054 tree ind
= cp_default_conversion (idx
, complain
);
4055 tree first
= NULL_TREE
;
4057 if (flag_strong_eval_order
== 2 && TREE_SIDE_EFFECTS (ind
))
4058 ar
= first
= save_expr (ar
);
4060 /* Put the integer in IND to simplify error checking. */
4061 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
4062 std::swap (ar
, ind
);
4064 if (ar
== error_mark_node
|| ind
== error_mark_node
)
4065 return error_mark_node
;
4067 if (!TYPE_PTR_P (TREE_TYPE (ar
)))
4069 if (complain
& tf_error
)
4070 error_at (loc
, "subscripted value is neither array nor pointer");
4071 return error_mark_node
;
4073 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
4075 if (complain
& tf_error
)
4076 error_at (loc
, "array subscript is not an integer");
4077 return error_mark_node
;
4080 warn_array_subscript_with_type_char (loc
, idx
);
4082 ret
= cp_build_binary_op (input_location
, PLUS_EXPR
, ar
, ind
, complain
);
4084 ret
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (ret
), first
, ret
);
4085 ret
= cp_build_indirect_ref (loc
, ret
, RO_ARRAY_INDEXING
, complain
);
4086 protected_set_expr_location (ret
, loc
);
4088 ret
= non_lvalue_loc (loc
, ret
);
4093 /* Entry point for Obj-C++. */
4096 build_array_ref (location_t loc
, tree array
, tree idx
)
4098 return cp_build_array_ref (loc
, array
, idx
, tf_warning_or_error
);
4101 /* Resolve a pointer to member function. INSTANCE is the object
4102 instance to use, if the member points to a virtual member.
4104 This used to avoid checking for virtual functions if basetype
4105 has no virtual functions, according to an earlier ANSI draft.
4106 With the final ISO C++ rules, such an optimization is
4107 incorrect: A pointer to a derived member can be static_cast
4108 to pointer-to-base-member, as long as the dynamic object
4109 later has the right member. So now we only do this optimization
4110 when we know the dynamic type of the object. */
4113 get_member_function_from_ptrfunc (tree
*instance_ptrptr
, tree function
,
4114 tsubst_flags_t complain
)
4116 if (TREE_CODE (function
) == OFFSET_REF
)
4117 function
= TREE_OPERAND (function
, 1);
4119 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
4121 tree idx
, delta
, e1
, e2
, e3
, vtbl
;
4123 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
4124 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
4126 tree instance_ptr
= *instance_ptrptr
;
4127 tree instance_save_expr
= 0;
4128 if (instance_ptr
== error_mark_node
)
4130 if (TREE_CODE (function
) == PTRMEM_CST
)
4132 /* Extracting the function address from a pmf is only
4133 allowed with -Wno-pmf-conversions. It only works for
4135 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
), complain
);
4136 e1
= convert (fntype
, e1
);
4141 if (complain
& tf_error
)
4142 error ("object missing in use of %qE", function
);
4143 return error_mark_node
;
4147 /* True if we know that the dynamic type of the object doesn't have
4148 virtual functions, so we can assume the PFN field is a pointer. */
4149 nonvirtual
= (COMPLETE_TYPE_P (basetype
)
4150 && !TYPE_POLYMORPHIC_P (basetype
)
4151 && resolves_to_fixed_type_p (instance_ptr
, 0));
4153 /* If we don't really have an object (i.e. in an ill-formed
4154 conversion from PMF to pointer), we can't resolve virtual
4155 functions anyway. */
4156 if (!nonvirtual
&& is_dummy_object (instance_ptr
))
4159 if (TREE_SIDE_EFFECTS (instance_ptr
))
4160 instance_ptr
= instance_save_expr
= save_expr (instance_ptr
);
4162 if (TREE_SIDE_EFFECTS (function
))
4163 function
= save_expr (function
);
4165 /* Start by extracting all the information from the PMF itself. */
4166 e3
= pfn_from_ptrmemfunc (function
);
4167 delta
= delta_from_ptrmemfunc (function
);
4168 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
4169 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
4171 int flag_sanitize_save
;
4172 case ptrmemfunc_vbit_in_pfn
:
4173 e1
= cp_build_binary_op (input_location
,
4174 BIT_AND_EXPR
, idx
, integer_one_node
,
4176 idx
= cp_build_binary_op (input_location
,
4177 MINUS_EXPR
, idx
, integer_one_node
,
4179 if (idx
== error_mark_node
)
4180 return error_mark_node
;
4183 case ptrmemfunc_vbit_in_delta
:
4184 e1
= cp_build_binary_op (input_location
,
4185 BIT_AND_EXPR
, delta
, integer_one_node
,
4187 /* Don't instrument the RSHIFT_EXPR we're about to create because
4188 we're going to use DELTA number of times, and that wouldn't play
4189 well with SAVE_EXPRs therein. */
4190 flag_sanitize_save
= flag_sanitize
;
4192 delta
= cp_build_binary_op (input_location
,
4193 RSHIFT_EXPR
, delta
, integer_one_node
,
4195 flag_sanitize
= flag_sanitize_save
;
4196 if (delta
== error_mark_node
)
4197 return error_mark_node
;
4204 if (e1
== error_mark_node
)
4205 return error_mark_node
;
4207 /* Convert down to the right base before using the instance. A
4208 special case is that in a pointer to member of class C, C may
4209 be incomplete. In that case, the function will of course be
4210 a member of C, and no conversion is required. In fact,
4211 lookup_base will fail in that case, because incomplete
4212 classes do not have BINFOs. */
4213 if (!same_type_ignoring_top_level_qualifiers_p
4214 (basetype
, TREE_TYPE (TREE_TYPE (instance_ptr
))))
4216 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
4217 basetype
, ba_check
, NULL
, complain
);
4218 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
,
4220 if (instance_ptr
== error_mark_node
)
4221 return error_mark_node
;
4223 /* ...and then the delta in the PMF. */
4224 instance_ptr
= fold_build_pointer_plus (instance_ptr
, delta
);
4226 /* Hand back the adjusted 'this' argument to our caller. */
4227 *instance_ptrptr
= instance_ptr
;
4230 /* Now just return the pointer. */
4233 /* Next extract the vtable pointer from the object. */
4234 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
4236 vtbl
= cp_build_fold_indirect_ref (vtbl
);
4237 if (vtbl
== error_mark_node
)
4238 return error_mark_node
;
4240 /* Finally, extract the function pointer from the vtable. */
4241 e2
= fold_build_pointer_plus_loc (input_location
, vtbl
, idx
);
4242 e2
= cp_build_fold_indirect_ref (e2
);
4243 if (e2
== error_mark_node
)
4244 return error_mark_node
;
4245 TREE_CONSTANT (e2
) = 1;
4247 /* When using function descriptors, the address of the
4248 vtable entry is treated as a function pointer. */
4249 if (TARGET_VTABLE_USES_DESCRIPTORS
)
4250 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
4251 cp_build_addr_expr (e2
, complain
));
4253 e2
= fold_convert (TREE_TYPE (e3
), e2
);
4254 e1
= build_conditional_expr (input_location
, e1
, e2
, e3
, complain
);
4255 if (e1
== error_mark_node
)
4256 return error_mark_node
;
4258 /* Make sure this doesn't get evaluated first inside one of the
4259 branches of the COND_EXPR. */
4260 if (instance_save_expr
)
4261 e1
= build2 (COMPOUND_EXPR
, TREE_TYPE (e1
),
4262 instance_save_expr
, e1
);
4269 /* Used by the C-common bits. */
4271 build_function_call (location_t
/*loc*/,
4272 tree function
, tree params
)
4274 return cp_build_function_call (function
, params
, tf_warning_or_error
);
4277 /* Used by the C-common bits. */
4279 build_function_call_vec (location_t
/*loc*/, vec
<location_t
> /*arg_loc*/,
4280 tree function
, vec
<tree
, va_gc
> *params
,
4281 vec
<tree
, va_gc
> * /*origtypes*/, tree orig_function
)
4283 vec
<tree
, va_gc
> *orig_params
= params
;
4284 tree ret
= cp_build_function_call_vec (function
, ¶ms
,
4285 tf_warning_or_error
, orig_function
);
4287 /* cp_build_function_call_vec can reallocate PARAMS by adding
4288 default arguments. That should never happen here. Verify
4290 gcc_assert (params
== orig_params
);
4295 /* Build a function call using a tree list of arguments. */
4298 cp_build_function_call (tree function
, tree params
, tsubst_flags_t complain
)
4303 for (; params
!= NULL_TREE
; params
= TREE_CHAIN (params
))
4304 vec_safe_push (vec
, TREE_VALUE (params
));
4305 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
4309 /* Build a function call using varargs. */
4312 cp_build_function_call_nary (tree function
, tsubst_flags_t complain
, ...)
4318 va_start (args
, complain
);
4319 for (t
= va_arg (args
, tree
); t
!= NULL_TREE
; t
= va_arg (args
, tree
))
4320 vec_safe_push (vec
, t
);
4322 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
4326 /* Build a function call using a vector of arguments.
4327 If FUNCTION is the result of resolving an overloaded target built-in,
4328 ORIG_FNDECL is the original function decl, otherwise it is null.
4329 PARAMS may be NULL if there are no parameters. This changes the
4330 contents of PARAMS. */
4333 cp_build_function_call_vec (tree function
, vec
<tree
, va_gc
> **params
,
4334 tsubst_flags_t complain
, tree orig_fndecl
)
4336 tree fntype
, fndecl
;
4338 tree original
= function
;
4342 vec
<tree
, va_gc
> *allocated
= NULL
;
4345 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4346 expressions, like those used for ObjC messenger dispatches. */
4347 if (params
!= NULL
&& !vec_safe_is_empty (*params
))
4348 function
= objc_rewrite_function_call (function
, (**params
)[0]);
4350 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4351 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4352 if (TREE_CODE (function
) == NOP_EXPR
4353 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
4354 function
= TREE_OPERAND (function
, 0);
4356 if (TREE_CODE (function
) == FUNCTION_DECL
)
4358 if (!mark_used (function
, complain
))
4359 return error_mark_node
;
4362 /* Convert anything with function type to a pointer-to-function. */
4363 if (DECL_MAIN_P (function
))
4365 if (complain
& tf_error
)
4366 pedwarn (input_location
, OPT_Wpedantic
,
4367 "ISO C++ forbids calling %<::main%> from within program");
4369 return error_mark_node
;
4371 function
= build_addr_func (function
, complain
);
4377 function
= build_addr_func (function
, complain
);
4380 if (function
== error_mark_node
)
4381 return error_mark_node
;
4383 fntype
= TREE_TYPE (function
);
4385 if (TYPE_PTRMEMFUNC_P (fntype
))
4387 if (complain
& tf_error
)
4388 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4389 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4390 original
, original
);
4391 return error_mark_node
;
4394 is_method
= (TYPE_PTR_P (fntype
)
4395 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
4397 if (!(TYPE_PTRFN_P (fntype
)
4399 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
4401 if (complain
& tf_error
)
4403 if (!flag_diagnostics_show_caret
)
4404 error_at (input_location
,
4405 "%qE cannot be used as a function", original
);
4406 else if (DECL_P (original
))
4407 error_at (input_location
,
4408 "%qD cannot be used as a function", original
);
4410 error_at (input_location
,
4411 "expression cannot be used as a function");
4414 return error_mark_node
;
4417 /* fntype now gets the type of function pointed to. */
4418 fntype
= TREE_TYPE (fntype
);
4419 parm_types
= TYPE_ARG_TYPES (fntype
);
4423 allocated
= make_tree_vector ();
4424 params
= &allocated
;
4427 nargs
= convert_arguments (parm_types
, params
, fndecl
, LOOKUP_NORMAL
,
4430 return error_mark_node
;
4432 argarray
= (*params
)->address ();
4434 /* Check for errors in format strings and inappropriately
4436 bool warned_p
= check_function_arguments (input_location
, fndecl
, fntype
,
4437 nargs
, argarray
, NULL
);
4439 ret
= build_cxx_call (function
, nargs
, argarray
, complain
, orig_fndecl
);
4443 tree c
= extract_call_expr (ret
);
4444 if (TREE_CODE (c
) == CALL_EXPR
)
4445 suppress_warning (c
, OPT_Wnonnull
);
4448 if (allocated
!= NULL
)
4449 release_tree_vector (allocated
);
4454 /* Subroutine of convert_arguments.
4455 Print an error message about a wrong number of arguments. */
4458 error_args_num (location_t loc
, tree fndecl
, bool too_many_p
)
4462 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
4464 if (DECL_NAME (fndecl
) == NULL_TREE
4465 || (DECL_NAME (fndecl
)
4466 == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl
)))))
4469 ? G_("too many arguments to constructor %q#D")
4470 : G_("too few arguments to constructor %q#D"),
4475 ? G_("too many arguments to member function %q#D")
4476 : G_("too few arguments to member function %q#D"),
4482 ? G_("too many arguments to function %q#D")
4483 : G_("too few arguments to function %q#D"),
4485 if (!DECL_IS_UNDECLARED_BUILTIN (fndecl
))
4486 inform (DECL_SOURCE_LOCATION (fndecl
), "declared here");
4490 if (c_dialect_objc () && objc_message_selector ())
4493 ? G_("too many arguments to method %q#D")
4494 : G_("too few arguments to method %q#D"),
4495 objc_message_selector ());
4497 error_at (loc
, too_many_p
? G_("too many arguments to function")
4498 : G_("too few arguments to function"));
4502 /* Convert the actual parameter expressions in the list VALUES to the
4503 types in the list TYPELIST. The converted expressions are stored
4504 back in the VALUES vector.
4505 If parmdecls is exhausted, or when an element has NULL as its type,
4506 perform the default conversions.
4508 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4510 This is also where warnings about wrong number of args are generated.
4512 Returns the actual number of arguments processed (which might be less
4513 than the length of the vector), or -1 on error.
4515 In C++, unspecified trailing parameters can be filled in with their
4516 default arguments, if such were specified. Do so here. */
4519 convert_arguments (tree typelist
, vec
<tree
, va_gc
> **values
, tree fndecl
,
4520 int flags
, tsubst_flags_t complain
)
4525 /* Argument passing is always copy-initialization. */
4526 flags
|= LOOKUP_ONLYCONVERTING
;
4528 for (i
= 0, typetail
= typelist
;
4529 i
< vec_safe_length (*values
);
4532 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
4533 tree val
= (**values
)[i
];
4535 if (val
== error_mark_node
|| type
== error_mark_node
)
4538 if (type
== void_type_node
)
4540 if (complain
& tf_error
)
4542 error_args_num (input_location
, fndecl
, /*too_many_p=*/true);
4549 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4550 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4551 if (TREE_CODE (val
) == NOP_EXPR
4552 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
4553 && (type
== 0 || !TYPE_REF_P (type
)))
4554 val
= TREE_OPERAND (val
, 0);
4556 if (type
== 0 || !TYPE_REF_P (type
))
4558 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
4559 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val
)))
4560 val
= decay_conversion (val
, complain
);
4563 if (val
== error_mark_node
)
4568 /* Formal parm type is specified by a function prototype. */
4571 if (!COMPLETE_TYPE_P (complete_type (type
)))
4573 if (complain
& tf_error
)
4575 location_t loc
= EXPR_LOC_OR_LOC (val
, input_location
);
4578 auto_diagnostic_group d
;
4580 "parameter %P of %qD has incomplete type %qT",
4582 inform (get_fndecl_argument_location (fndecl
, i
),
4586 error_at (loc
, "parameter %P has incomplete type %qT", i
,
4589 parmval
= error_mark_node
;
4593 parmval
= convert_for_initialization
4594 (NULL_TREE
, type
, val
, flags
,
4595 ICR_ARGPASS
, fndecl
, i
, complain
);
4596 parmval
= convert_for_arg_passing (type
, parmval
, complain
);
4599 if (parmval
== error_mark_node
)
4602 (**values
)[i
] = parmval
;
4606 if (fndecl
&& magic_varargs_p (fndecl
))
4607 /* Don't do ellipsis conversion for __built_in_constant_p
4608 as this will result in spurious errors for non-trivial
4610 val
= require_complete_type (val
, complain
);
4612 val
= convert_arg_to_ellipsis (val
, complain
);
4614 (**values
)[i
] = val
;
4618 typetail
= TREE_CHAIN (typetail
);
4621 if (typetail
!= 0 && typetail
!= void_list_node
)
4623 /* See if there are default arguments that can be used. Because
4624 we hold default arguments in the FUNCTION_TYPE (which is so
4625 wrong), we can see default parameters here from deduced
4626 contexts (and via typeof) for indirect function calls.
4627 Fortunately we know whether we have a function decl to
4628 provide default arguments in a language conformant
4630 if (fndecl
&& TREE_PURPOSE (typetail
)
4631 && TREE_CODE (TREE_PURPOSE (typetail
)) != DEFERRED_PARSE
)
4633 for (; typetail
!= void_list_node
; ++i
)
4635 /* After DR777, with explicit template args we can end up with a
4636 default argument followed by no default argument. */
4637 if (!TREE_PURPOSE (typetail
))
4640 = convert_default_arg (TREE_VALUE (typetail
),
4641 TREE_PURPOSE (typetail
),
4642 fndecl
, i
, complain
);
4644 if (parmval
== error_mark_node
)
4647 vec_safe_push (*values
, parmval
);
4648 typetail
= TREE_CHAIN (typetail
);
4649 /* ends with `...'. */
4650 if (typetail
== NULL_TREE
)
4655 if (typetail
&& typetail
!= void_list_node
)
4657 if (complain
& tf_error
)
4658 error_args_num (input_location
, fndecl
, /*too_many_p=*/false);
4666 /* Build a binary-operation expression, after performing default
4667 conversions on the operands. CODE is the kind of expression to
4668 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4669 are the tree codes which correspond to ARG1 and ARG2 when issuing
4670 warnings about possibly misplaced parentheses. They may differ
4671 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4672 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4673 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4674 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4675 ARG2_CODE as ERROR_MARK. */
4678 build_x_binary_op (const op_location_t
&loc
, enum tree_code code
, tree arg1
,
4679 enum tree_code arg1_code
, tree arg2
,
4680 enum tree_code arg2_code
, tree lookups
,
4681 tree
*overload_p
, tsubst_flags_t complain
)
4686 tree overload
= NULL_TREE
;
4691 if (processing_template_decl
)
4693 if (type_dependent_expression_p (arg1
)
4694 || type_dependent_expression_p (arg2
))
4696 expr
= build_min_nt_loc (loc
, code
, arg1
, arg2
);
4698 = build_dependent_operator_type (lookups
, code
, false);
4701 arg1
= build_non_dependent_expr (arg1
);
4702 arg2
= build_non_dependent_expr (arg2
);
4705 if (code
== DOTSTAR_EXPR
)
4706 expr
= build_m_component_ref (arg1
, arg2
, complain
);
4708 expr
= build_new_op (loc
, code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
,
4709 lookups
, &overload
, complain
);
4711 if (overload_p
!= NULL
)
4712 *overload_p
= overload
;
4714 /* Check for cases such as x+y<<z which users are likely to
4715 misinterpret. But don't warn about obj << x + y, since that is a
4716 common idiom for I/O. */
4717 if (warn_parentheses
4718 && (complain
& tf_warning
)
4719 && !processing_template_decl
4720 && !error_operand_p (arg1
)
4721 && !error_operand_p (arg2
)
4722 && (code
!= LSHIFT_EXPR
4723 || !CLASS_TYPE_P (TREE_TYPE (arg1
))))
4724 warn_about_parentheses (loc
, code
, arg1_code
, orig_arg1
,
4725 arg2_code
, orig_arg2
);
4727 if (processing_template_decl
&& expr
!= error_mark_node
)
4729 if (overload
!= NULL_TREE
)
4730 return (build_min_non_dep_op_overload
4731 (code
, expr
, overload
, orig_arg1
, orig_arg2
));
4733 return build_min_non_dep (code
, expr
, orig_arg1
, orig_arg2
);
4739 /* Build and return an ARRAY_REF expression. */
4742 build_x_array_ref (location_t loc
, tree arg1
, tree arg2
,
4743 tsubst_flags_t complain
)
4745 tree orig_arg1
= arg1
;
4746 tree orig_arg2
= arg2
;
4748 tree overload
= NULL_TREE
;
4750 if (processing_template_decl
)
4752 if (type_dependent_expression_p (arg1
)
4753 || type_dependent_expression_p (arg2
))
4754 return build_min_nt_loc (loc
, ARRAY_REF
, arg1
, arg2
,
4755 NULL_TREE
, NULL_TREE
);
4756 arg1
= build_non_dependent_expr (arg1
);
4757 arg2
= build_non_dependent_expr (arg2
);
4760 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, arg1
, arg2
,
4761 NULL_TREE
, NULL_TREE
, &overload
, complain
);
4763 if (processing_template_decl
&& expr
!= error_mark_node
)
4765 if (overload
!= NULL_TREE
)
4766 return (build_min_non_dep_op_overload
4767 (ARRAY_REF
, expr
, overload
, orig_arg1
, orig_arg2
));
4769 return build_min_non_dep (ARRAY_REF
, expr
, orig_arg1
, orig_arg2
,
4770 NULL_TREE
, NULL_TREE
);
4775 /* Return whether OP is an expression of enum type cast to integer
4776 type. In C++ even unsigned enum types are cast to signed integer
4777 types. We do not want to issue warnings about comparisons between
4778 signed and unsigned types when one of the types is an enum type.
4779 Those warnings are always false positives in practice. */
4782 enum_cast_to_int (tree op
)
4784 if (CONVERT_EXPR_P (op
)
4785 && TREE_TYPE (op
) == integer_type_node
4786 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == ENUMERAL_TYPE
4787 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op
, 0))))
4790 /* The cast may have been pushed into a COND_EXPR. */
4791 if (TREE_CODE (op
) == COND_EXPR
)
4792 return (enum_cast_to_int (TREE_OPERAND (op
, 1))
4793 || enum_cast_to_int (TREE_OPERAND (op
, 2)));
4798 /* For the c-common bits. */
4800 build_binary_op (location_t location
, enum tree_code code
, tree op0
, tree op1
,
4803 return cp_build_binary_op (location
, code
, op0
, op1
, tf_warning_or_error
);
4806 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4807 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4810 build_vec_cmp (tree_code code
, tree type
,
4811 tree arg0
, tree arg1
)
4813 tree zero_vec
= build_zero_cst (type
);
4814 tree minus_one_vec
= build_minus_one_cst (type
);
4815 tree cmp_type
= truth_type_for (type
);
4816 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
4817 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
4820 /* Possibly warn about an address never being NULL. */
4823 warn_for_null_address (location_t location
, tree op
, tsubst_flags_t complain
)
4825 /* Prevent warnings issued for macro expansion. */
4827 || (complain
& tf_warning
) == 0
4828 || c_inhibit_evaluation_warnings
!= 0
4829 || from_macro_expansion_at (location
)
4830 || warning_suppressed_p (op
, OPT_Waddress
))
4833 if (TREE_CODE (op
) == NON_DEPENDENT_EXPR
)
4834 op
= TREE_OPERAND (op
, 0);
4836 tree cop
= fold_for_warn (op
);
4838 if (TREE_CODE (cop
) == NON_LVALUE_EXPR
)
4839 /* Unwrap the expression for C++ 98. */
4840 cop
= TREE_OPERAND (cop
, 0);
4842 if (TREE_CODE (cop
) == PTRMEM_CST
)
4844 /* The address of a nonstatic data member is never null. */
4845 warning_at (location
, OPT_Waddress
,
4846 "the address %qE will never be NULL",
4851 if (TREE_CODE (cop
) == NOP_EXPR
)
4853 /* Allow casts to intptr_t to suppress the warning. */
4854 tree type
= TREE_TYPE (cop
);
4855 if (TREE_CODE (type
) == INTEGER_TYPE
)
4861 bool warned
= false;
4862 if (TREE_CODE (cop
) == ADDR_EXPR
)
4864 cop
= TREE_OPERAND (cop
, 0);
4866 /* Set to true in the loop below if OP dereferences its operand.
4867 In such a case the ultimate target need not be a decl for
4868 the null [in]equality test to be necessarily constant. */
4871 /* Get the outermost array or object, or member. */
4872 while (handled_component_p (cop
))
4874 if (TREE_CODE (cop
) == COMPONENT_REF
)
4876 /* Get the member (its address is never null). */
4877 cop
= TREE_OPERAND (cop
, 1);
4881 /* Get the outer array/object to refer to in the warning. */
4882 cop
= TREE_OPERAND (cop
, 0);
4886 if ((!deref
&& !decl_with_nonnull_addr_p (cop
))
4887 || from_macro_expansion_at (location
)
4888 || warning_suppressed_p (cop
, OPT_Waddress
))
4891 warned
= warning_at (location
, OPT_Waddress
,
4892 "the address of %qD will never be NULL", cop
);
4895 else if (TREE_CODE (cop
) == POINTER_PLUS_EXPR
)
4897 /* Adding zero to the null pointer is well-defined in C++. When
4898 the offset is unknown (i.e., not a constant) warn anyway since
4899 it's less likely that the pointer operand is null than not. */
4900 tree off
= TREE_OPERAND (cop
, 1);
4901 if (!integer_zerop (off
)
4902 && !warning_suppressed_p (cop
, OPT_Waddress
))
4904 tree base
= TREE_OPERAND (cop
, 0);
4906 if (TYPE_REF_P (TREE_TYPE (base
)))
4907 warning_at (location
, OPT_Waddress
, "the compiler can assume that "
4908 "the address of %qE will never be NULL", base
);
4910 warning_at (location
, OPT_Waddress
, "comparing the result of "
4911 "pointer addition %qE and NULL", cop
);
4915 else if (CONVERT_EXPR_P (op
)
4916 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op
, 0))))
4920 if (TREE_CODE (op
) == COMPONENT_REF
)
4921 op
= TREE_OPERAND (op
, 1);
4924 warned
= warning_at (location
, OPT_Waddress
,
4925 "the compiler can assume that the address of "
4926 "%qD will never be NULL", op
);
4929 if (warned
&& DECL_P (op
))
4930 inform (DECL_SOURCE_LOCATION (op
), "%qD declared here", op
);
4933 /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
4934 the other operand is of a different enumeration type or a floating-point
4935 type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
4936 code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
4937 and LOC is the location for the whole binary expression.
4938 TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
4941 do_warn_enum_conversions (location_t loc
, enum tree_code code
, tree type0
,
4944 if (TREE_CODE (type0
) == ENUMERAL_TYPE
4945 && TREE_CODE (type1
) == ENUMERAL_TYPE
4946 && TYPE_MAIN_VARIANT (type0
) != TYPE_MAIN_VARIANT (type1
))
4948 /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
4949 Otherwise, warn if -Wenum-conversion is on. */
4951 if (warn_deprecated_enum_enum_conv
)
4952 opt
= OPT_Wdeprecated_enum_enum_conversion
;
4953 else if (warn_enum_conversion
)
4954 opt
= OPT_Wenum_conversion
;
4966 /* Comparisons are handled by -Wenum-compare. */
4968 case SPACESHIP_EXPR
:
4969 /* This is invalid, don't warn. */
4974 warning_at (loc
, opt
, "bitwise operation between different "
4975 "enumeration types %qT and %qT is deprecated",
4979 warning_at (loc
, opt
, "arithmetic between different enumeration "
4980 "types %qT and %qT is deprecated", type0
, type1
);
4984 else if ((TREE_CODE (type0
) == ENUMERAL_TYPE
4985 && TREE_CODE (type1
) == REAL_TYPE
)
4986 || (TREE_CODE (type0
) == REAL_TYPE
4987 && TREE_CODE (type1
) == ENUMERAL_TYPE
))
4989 const bool enum_first_p
= TREE_CODE (type0
) == ENUMERAL_TYPE
;
4990 /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
4991 Otherwise, warn if -Wenum-conversion is on. */
4993 if (warn_deprecated_enum_float_conv
)
4994 opt
= OPT_Wdeprecated_enum_float_conversion
;
4995 else if (warn_enum_conversion
)
4996 opt
= OPT_Wenum_conversion
;
5009 warning_at (loc
, opt
, "comparison of enumeration type %qT with "
5010 "floating-point type %qT is deprecated",
5013 warning_at (loc
, opt
, "comparison of floating-point type %qT "
5014 "with enumeration type %qT is deprecated",
5017 case SPACESHIP_EXPR
:
5018 /* This is invalid, don't warn. */
5022 warning_at (loc
, opt
, "arithmetic between enumeration type %qT "
5023 "and floating-point type %qT is deprecated",
5026 warning_at (loc
, opt
, "arithmetic between floating-point type %qT "
5027 "and enumeration type %qT is deprecated",
5034 /* Build a binary-operation expression without default conversions.
5035 CODE is the kind of expression to build.
5036 LOCATION is the location_t of the operator in the source code.
5037 This function differs from `build' in several ways:
5038 the data type of the result is computed and recorded in it,
5039 warnings are generated if arg data types are invalid,
5040 special handling for addition and subtraction of pointers is known,
5041 and some optimization is done (operations on narrow ints
5042 are done in the narrower type when that gives the same result).
5043 Constant folding is also done before the result is returned.
5045 Note that the operands will never have enumeral types
5046 because either they have just had the default conversions performed
5047 or they have both just been converted to some other type in which
5048 the arithmetic is to be done.
5050 C++: must do special pointer arithmetic when implementing
5051 multiple inheritance, and deal with pointer to member functions. */
5054 cp_build_binary_op (const op_location_t
&location
,
5055 enum tree_code code
, tree orig_op0
, tree orig_op1
,
5056 tsubst_flags_t complain
)
5059 enum tree_code code0
, code1
;
5061 const char *invalid_op_diag
;
5063 /* Expression code to give to the expression when it is built.
5064 Normally this is CODE, which is what the caller asked for,
5065 but in some special cases we change it. */
5066 enum tree_code resultcode
= code
;
5068 /* Data type in which the computation is to be performed.
5069 In the simplest cases this is the common type of the arguments. */
5070 tree result_type
= NULL_TREE
;
5072 /* Nonzero means operands have already been type-converted
5073 in whatever way is necessary.
5074 Zero means they need to be converted to RESULT_TYPE. */
5077 /* Nonzero means create the expression with this type, rather than
5079 tree build_type
= 0;
5081 /* Nonzero means after finally constructing the expression
5082 convert it to this type. */
5083 tree final_type
= 0;
5087 /* Nonzero if this is an operation like MIN or MAX which can
5088 safely be computed in short if both args are promoted shorts.
5089 Also implies COMMON.
5090 -1 indicates a bitwise operation; this makes a difference
5091 in the exact conditions for when it is safe to do the operation
5092 in a narrower mode. */
5095 /* Nonzero if this is a comparison operation;
5096 if both args are promoted shorts, compare the original shorts.
5097 Also implies COMMON. */
5098 int short_compare
= 0;
5100 /* Nonzero if this is a right-shift operation, which can be computed on the
5101 original short and then promoted if the operand is a promoted short. */
5102 int short_shift
= 0;
5104 /* Nonzero means set RESULT_TYPE to the common type of the args. */
5107 /* True if both operands have arithmetic type. */
5108 bool arithmetic_types_p
;
5110 /* Remember whether we're doing / or %. */
5111 bool doing_div_or_mod
= false;
5113 /* Remember whether we're doing << or >>. */
5114 bool doing_shift
= false;
5116 /* Tree holding instrumentation expression. */
5117 tree instrument_expr
= NULL_TREE
;
5119 /* Apply default conversions. */
5120 op0
= resolve_nondeduced_context (orig_op0
, complain
);
5121 op1
= resolve_nondeduced_context (orig_op1
, complain
);
5123 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
5124 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
5125 || code
== TRUTH_XOR_EXPR
)
5127 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
5128 op0
= decay_conversion (op0
, complain
);
5129 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
5130 op1
= decay_conversion (op1
, complain
);
5134 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
5135 op0
= cp_default_conversion (op0
, complain
);
5136 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
5137 op1
= cp_default_conversion (op1
, complain
);
5140 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
5141 STRIP_TYPE_NOPS (op0
);
5142 STRIP_TYPE_NOPS (op1
);
5144 /* DTRT if one side is an overloaded function, but complain about it. */
5145 if (type_unknown_p (op0
))
5147 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
5148 if (t
!= error_mark_node
)
5150 if (complain
& tf_error
)
5151 permerror (location
,
5152 "assuming cast to type %qT from overloaded function",
5157 if (type_unknown_p (op1
))
5159 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
5160 if (t
!= error_mark_node
)
5162 if (complain
& tf_error
)
5163 permerror (location
,
5164 "assuming cast to type %qT from overloaded function",
5170 type0
= TREE_TYPE (op0
);
5171 type1
= TREE_TYPE (op1
);
5173 /* The expression codes of the data types of the arguments tell us
5174 whether the arguments are integers, floating, pointers, etc. */
5175 code0
= TREE_CODE (type0
);
5176 code1
= TREE_CODE (type1
);
5178 /* If an error was already reported for one of the arguments,
5179 avoid reporting another error. */
5180 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
5181 return error_mark_node
;
5183 if ((invalid_op_diag
5184 = targetm
.invalid_binary_op (code
, type0
, type1
)))
5186 if (complain
& tf_error
)
5188 if (code0
== REAL_TYPE
5189 && code1
== REAL_TYPE
5190 && (extended_float_type_p (type0
)
5191 || extended_float_type_p (type1
))
5192 && cp_compare_floating_point_conversion_ranks (type0
,
5195 rich_location
richloc (line_table
, location
);
5196 binary_op_error (&richloc
, code
, type0
, type1
);
5199 error (invalid_op_diag
);
5201 return error_mark_node
;
5204 /* Issue warnings about peculiar, but valid, uses of NULL. */
5205 if ((null_node_p (orig_op0
) || null_node_p (orig_op1
))
5206 /* It's reasonable to use pointer values as operands of &&
5207 and ||, so NULL is no exception. */
5208 && code
!= TRUTH_ANDIF_EXPR
&& code
!= TRUTH_ORIF_EXPR
5209 && ( /* Both are NULL (or 0) and the operation was not a
5210 comparison or a pointer subtraction. */
5211 (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)
5212 && code
!= EQ_EXPR
&& code
!= NE_EXPR
&& code
!= MINUS_EXPR
)
5213 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5214 || (!null_ptr_cst_p (orig_op0
)
5215 && !TYPE_PTR_OR_PTRMEM_P (type0
))
5216 || (!null_ptr_cst_p (orig_op1
)
5217 && !TYPE_PTR_OR_PTRMEM_P (type1
)))
5218 && (complain
& tf_warning
))
5221 expansion_point_location_if_in_system_header (input_location
);
5223 warning_at (loc
, OPT_Wpointer_arith
, "NULL used in arithmetic");
5226 /* In case when one of the operands of the binary operation is
5227 a vector and another is a scalar -- convert scalar to vector. */
5228 if ((gnu_vector_type_p (type0
) && code1
!= VECTOR_TYPE
)
5229 || (gnu_vector_type_p (type1
) && code0
!= VECTOR_TYPE
))
5231 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
5232 complain
& tf_error
);
5234 switch (convert_flag
)
5237 return error_mark_node
;
5240 op0
= convert (TREE_TYPE (type1
), op0
);
5241 op0
= save_expr (op0
);
5242 op0
= build_vector_from_val (type1
, op0
);
5243 type0
= TREE_TYPE (op0
);
5244 code0
= TREE_CODE (type0
);
5250 op1
= convert (TREE_TYPE (type0
), op1
);
5251 op1
= save_expr (op1
);
5252 op1
= build_vector_from_val (type0
, op1
);
5253 type1
= TREE_TYPE (op1
);
5254 code1
= TREE_CODE (type1
);
5266 /* Subtraction of two similar pointers.
5267 We must subtract them as integers, then divide by object size. */
5268 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
5269 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
5272 result
= pointer_diff (location
, op0
, op1
,
5273 common_pointer_type (type0
, type1
), complain
,
5275 if (instrument_expr
!= NULL
)
5276 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
5277 instrument_expr
, result
);
5281 /* In all other cases except pointer - int, the usual arithmetic
5283 else if (!(code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
))
5288 /* The pointer - int case is just like pointer + int; fall
5292 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
5293 && (code0
== INTEGER_TYPE
|| code1
== INTEGER_TYPE
))
5297 ptr_operand
= ((code0
== POINTER_TYPE
) ? op0
: op1
);
5298 int_operand
= ((code0
== INTEGER_TYPE
) ? op0
: op1
);
5299 if (processing_template_decl
)
5301 result_type
= TREE_TYPE (ptr_operand
);
5304 return cp_pointer_int_sum (location
, code
,
5316 case TRUNC_DIV_EXPR
:
5318 case FLOOR_DIV_EXPR
:
5319 case ROUND_DIV_EXPR
:
5320 case EXACT_DIV_EXPR
:
5321 if (TREE_CODE (op0
) == SIZEOF_EXPR
&& TREE_CODE (op1
) == SIZEOF_EXPR
)
5323 tree type0
= TREE_OPERAND (op0
, 0);
5324 tree type1
= TREE_OPERAND (op1
, 0);
5325 tree first_arg
= tree_strip_any_location_wrapper (type0
);
5326 if (!TYPE_P (type0
))
5327 type0
= TREE_TYPE (type0
);
5328 if (!TYPE_P (type1
))
5329 type1
= TREE_TYPE (type1
);
5330 if (INDIRECT_TYPE_P (type0
) && same_type_p (TREE_TYPE (type0
), type1
))
5332 if (!(TREE_CODE (first_arg
) == PARM_DECL
5333 && DECL_ARRAY_PARAMETER_P (first_arg
)
5334 && warn_sizeof_array_argument
)
5335 && (complain
& tf_warning
))
5337 auto_diagnostic_group d
;
5338 if (warning_at (location
, OPT_Wsizeof_pointer_div
,
5339 "division %<sizeof (%T) / sizeof (%T)%> does "
5340 "not compute the number of array elements",
5342 if (DECL_P (first_arg
))
5343 inform (DECL_SOURCE_LOCATION (first_arg
),
5344 "first %<sizeof%> operand was declared here");
5347 else if (TREE_CODE (type0
) == ARRAY_TYPE
5348 && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0
)))
5349 /* Set by finish_parenthesized_expr. */
5350 && !warning_suppressed_p (op1
, OPT_Wsizeof_array_div
)
5351 && (complain
& tf_warning
))
5352 maybe_warn_sizeof_array_div (location
, first_arg
, type0
,
5353 op1
, non_reference (type1
));
5356 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
5357 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
5358 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5359 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
5361 enum tree_code tcode0
= code0
, tcode1
= code1
;
5362 doing_div_or_mod
= true;
5363 warn_for_div_by_zero (location
, fold_for_warn (op1
));
5365 if (tcode0
== COMPLEX_TYPE
|| tcode0
== VECTOR_TYPE
)
5366 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
5367 if (tcode1
== COMPLEX_TYPE
|| tcode1
== VECTOR_TYPE
)
5368 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
5370 if (!(tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
))
5371 resultcode
= RDIV_EXPR
;
5374 /* When dividing two signed integers, we have to promote to int.
5375 unless we divide by a constant != -1. Note that default
5376 conversion will have been performed on the operands at this
5377 point, so we have to dig out the original type to find out if
5379 tree stripped_op1
= tree_strip_any_location_wrapper (op1
);
5380 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
5381 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
5382 || (TREE_CODE (stripped_op1
) == INTEGER_CST
5383 && ! integer_all_onesp (stripped_op1
)));
5393 if ((code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
5394 || (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
5395 && !VECTOR_FLOAT_TYPE_P (type0
)
5396 && !VECTOR_FLOAT_TYPE_P (type1
)))
5400 case TRUNC_MOD_EXPR
:
5401 case FLOOR_MOD_EXPR
:
5402 doing_div_or_mod
= true;
5403 warn_for_div_by_zero (location
, fold_for_warn (op1
));
5405 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
5406 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
5407 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
5409 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
5411 /* Although it would be tempting to shorten always here, that loses
5412 on some targets, since the modulo instruction is undefined if the
5413 quotient can't be represented in the computation mode. We shorten
5414 only if unsigned or if dividing by something we know != -1. */
5415 tree stripped_op1
= tree_strip_any_location_wrapper (op1
);
5416 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
5417 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
5418 || (TREE_CODE (stripped_op1
) == INTEGER_CST
5419 && ! integer_all_onesp (stripped_op1
)));
5424 case TRUTH_ANDIF_EXPR
:
5425 case TRUTH_ORIF_EXPR
:
5426 case TRUTH_AND_EXPR
:
5428 if (!VECTOR_TYPE_P (type0
) && gnu_vector_type_p (type1
))
5430 if (!COMPARISON_CLASS_P (op1
))
5431 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
5432 build_zero_cst (type1
), complain
);
5433 if (code
== TRUTH_ANDIF_EXPR
)
5435 tree z
= build_zero_cst (TREE_TYPE (op1
));
5436 return build_conditional_expr (location
, op0
, op1
, z
, complain
);
5438 else if (code
== TRUTH_ORIF_EXPR
)
5440 tree m1
= build_all_ones_cst (TREE_TYPE (op1
));
5441 return build_conditional_expr (location
, op0
, m1
, op1
, complain
);
5446 if (gnu_vector_type_p (type0
)
5447 && (!VECTOR_TYPE_P (type1
) || gnu_vector_type_p (type1
)))
5449 if (!COMPARISON_CLASS_P (op0
))
5450 op0
= cp_build_binary_op (EXPR_LOCATION (op0
), NE_EXPR
, op0
,
5451 build_zero_cst (type0
), complain
);
5452 if (!VECTOR_TYPE_P (type1
))
5454 tree m1
= build_all_ones_cst (TREE_TYPE (op0
));
5455 tree z
= build_zero_cst (TREE_TYPE (op0
));
5456 op1
= build_conditional_expr (location
, op1
, m1
, z
, complain
);
5458 else if (!COMPARISON_CLASS_P (op1
))
5459 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
5460 build_zero_cst (type1
), complain
);
5462 if (code
== TRUTH_ANDIF_EXPR
)
5463 code
= BIT_AND_EXPR
;
5464 else if (code
== TRUTH_ORIF_EXPR
)
5465 code
= BIT_IOR_EXPR
;
5469 return cp_build_binary_op (location
, code
, op0
, op1
, complain
);
5472 result_type
= boolean_type_node
;
5475 /* Shift operations: result has same type as first operand;
5476 always convert second operand to int.
5477 Also set SHORT_SHIFT if shifting rightward. */
5480 if (gnu_vector_type_p (type0
)
5481 && code1
== INTEGER_TYPE
5482 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
5484 result_type
= type0
;
5487 else if (gnu_vector_type_p (type0
)
5488 && gnu_vector_type_p (type1
)
5489 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
5490 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
5491 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
5492 TYPE_VECTOR_SUBPARTS (type1
)))
5494 result_type
= type0
;
5497 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
5499 tree const_op1
= fold_for_warn (op1
);
5500 if (TREE_CODE (const_op1
) != INTEGER_CST
)
5502 result_type
= type0
;
5504 if (TREE_CODE (const_op1
) == INTEGER_CST
)
5506 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
5508 if ((complain
& tf_warning
)
5509 && c_inhibit_evaluation_warnings
== 0)
5510 warning_at (location
, OPT_Wshift_count_negative
,
5511 "right shift count is negative");
5515 if (!integer_zerop (const_op1
))
5518 if (compare_tree_int (const_op1
, TYPE_PRECISION (type0
)) >= 0
5519 && (complain
& tf_warning
)
5520 && c_inhibit_evaluation_warnings
== 0)
5521 warning_at (location
, OPT_Wshift_count_overflow
,
5522 "right shift count >= width of type");
5525 /* Avoid converting op1 to result_type later. */
5531 if (gnu_vector_type_p (type0
)
5532 && code1
== INTEGER_TYPE
5533 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
5535 result_type
= type0
;
5538 else if (gnu_vector_type_p (type0
)
5539 && gnu_vector_type_p (type1
)
5540 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
5541 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
5542 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
5543 TYPE_VECTOR_SUBPARTS (type1
)))
5545 result_type
= type0
;
5548 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
5550 tree const_op0
= fold_for_warn (op0
);
5551 if (TREE_CODE (const_op0
) != INTEGER_CST
)
5553 tree const_op1
= fold_for_warn (op1
);
5554 if (TREE_CODE (const_op1
) != INTEGER_CST
)
5556 result_type
= type0
;
5558 if (TREE_CODE (const_op0
) == INTEGER_CST
5559 && tree_int_cst_sgn (const_op0
) < 0
5560 && !TYPE_OVERFLOW_WRAPS (type0
)
5561 && (complain
& tf_warning
)
5562 && c_inhibit_evaluation_warnings
== 0)
5563 warning_at (location
, OPT_Wshift_negative_value
,
5564 "left shift of negative value");
5565 if (TREE_CODE (const_op1
) == INTEGER_CST
)
5567 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
5569 if ((complain
& tf_warning
)
5570 && c_inhibit_evaluation_warnings
== 0)
5571 warning_at (location
, OPT_Wshift_count_negative
,
5572 "left shift count is negative");
5574 else if (compare_tree_int (const_op1
,
5575 TYPE_PRECISION (type0
)) >= 0)
5577 if ((complain
& tf_warning
)
5578 && c_inhibit_evaluation_warnings
== 0)
5579 warning_at (location
, OPT_Wshift_count_overflow
,
5580 "left shift count >= width of type");
5582 else if (TREE_CODE (const_op0
) == INTEGER_CST
5583 && (complain
& tf_warning
))
5584 maybe_warn_shift_overflow (location
, const_op0
, const_op1
);
5586 /* Avoid converting op1 to result_type later. */
5593 if (gnu_vector_type_p (type0
) && gnu_vector_type_p (type1
))
5594 goto vector_compare
;
5595 if ((complain
& tf_warning
)
5596 && c_inhibit_evaluation_warnings
== 0
5597 && (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
)))
5598 warning_at (location
, OPT_Wfloat_equal
,
5599 "comparing floating-point with %<==%> "
5600 "or %<!=%> is unsafe");
5601 if (complain
& tf_warning
)
5603 tree stripped_orig_op0
= tree_strip_any_location_wrapper (orig_op0
);
5604 tree stripped_orig_op1
= tree_strip_any_location_wrapper (orig_op1
);
5605 if ((TREE_CODE (stripped_orig_op0
) == STRING_CST
5606 && !integer_zerop (cp_fully_fold (op1
)))
5607 || (TREE_CODE (stripped_orig_op1
) == STRING_CST
5608 && !integer_zerop (cp_fully_fold (op0
))))
5609 warning_at (location
, OPT_Waddress
,
5610 "comparison with string literal results in "
5611 "unspecified behavior");
5612 else if (warn_array_compare
5613 && TREE_CODE (TREE_TYPE (orig_op0
)) == ARRAY_TYPE
5614 && TREE_CODE (TREE_TYPE (orig_op1
)) == ARRAY_TYPE
)
5615 do_warn_array_compare (location
, code
, stripped_orig_op0
,
5619 build_type
= boolean_type_node
;
5620 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
5621 || code0
== COMPLEX_TYPE
|| code0
== ENUMERAL_TYPE
)
5622 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5623 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
))
5625 else if (((code0
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type0
))
5626 && null_ptr_cst_p (orig_op1
))
5627 /* Handle, eg, (void*)0 (c++/43906), and more. */
5628 || (code0
== POINTER_TYPE
5629 && TYPE_PTR_P (type1
) && integer_zerop (op1
)))
5631 if (TYPE_PTR_P (type1
))
5632 result_type
= composite_pointer_type (location
,
5633 type0
, type1
, op0
, op1
,
5634 CPO_COMPARISON
, complain
);
5636 result_type
= type0
;
5638 if (char_type_p (TREE_TYPE (orig_op1
)))
5640 auto_diagnostic_group d
;
5641 if (warning_at (location
, OPT_Wpointer_compare
,
5642 "comparison between pointer and zero character "
5645 "did you mean to dereference the pointer?");
5647 warn_for_null_address (location
, op0
, complain
);
5649 else if (((code1
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type1
))
5650 && null_ptr_cst_p (orig_op0
))
5651 /* Handle, eg, (void*)0 (c++/43906), and more. */
5652 || (code1
== POINTER_TYPE
5653 && TYPE_PTR_P (type0
) && integer_zerop (op0
)))
5655 if (TYPE_PTR_P (type0
))
5656 result_type
= composite_pointer_type (location
,
5657 type0
, type1
, op0
, op1
,
5658 CPO_COMPARISON
, complain
);
5660 result_type
= type1
;
5662 if (char_type_p (TREE_TYPE (orig_op0
)))
5664 auto_diagnostic_group d
;
5665 if (warning_at (location
, OPT_Wpointer_compare
,
5666 "comparison between pointer and zero character "
5669 "did you mean to dereference the pointer?");
5671 warn_for_null_address (location
, op1
, complain
);
5673 else if ((code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5674 || (TYPE_PTRDATAMEM_P (type0
) && TYPE_PTRDATAMEM_P (type1
)))
5675 result_type
= composite_pointer_type (location
,
5676 type0
, type1
, op0
, op1
,
5677 CPO_COMPARISON
, complain
);
5678 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
5679 /* One of the operands must be of nullptr_t type. */
5680 result_type
= TREE_TYPE (nullptr_node
);
5681 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5683 result_type
= type0
;
5684 if (complain
& tf_error
)
5685 permerror (location
, "ISO C++ forbids comparison between "
5686 "pointer and integer");
5688 return error_mark_node
;
5690 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
5692 result_type
= type1
;
5693 if (complain
& tf_error
)
5694 permerror (location
, "ISO C++ forbids comparison between "
5695 "pointer and integer");
5697 return error_mark_node
;
5699 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (orig_op1
))
5701 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5702 == ptrmemfunc_vbit_in_delta
)
5704 tree pfn0
, delta0
, e1
, e2
;
5706 if (TREE_SIDE_EFFECTS (op0
))
5707 op0
= cp_save_expr (op0
);
5709 pfn0
= pfn_from_ptrmemfunc (op0
);
5710 delta0
= delta_from_ptrmemfunc (op0
);
5711 e1
= cp_build_binary_op (location
,
5714 build_zero_cst (TREE_TYPE (pfn0
)),
5716 e2
= cp_build_binary_op (location
,
5722 if (complain
& tf_warning
)
5723 maybe_warn_zero_as_null_pointer_constant (op1
, input_location
);
5725 e2
= cp_build_binary_op (location
,
5726 EQ_EXPR
, e2
, integer_zero_node
,
5728 op0
= cp_build_binary_op (location
,
5729 TRUTH_ANDIF_EXPR
, e1
, e2
,
5731 op1
= cp_convert (TREE_TYPE (op0
), integer_one_node
, complain
);
5735 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
5736 op1
= cp_convert (TREE_TYPE (op0
), op1
, complain
);
5738 result_type
= TREE_TYPE (op0
);
5740 warn_for_null_address (location
, orig_op0
, complain
);
5742 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (orig_op0
))
5743 return cp_build_binary_op (location
, code
, op1
, op0
, complain
);
5744 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
))
5747 /* E will be the final comparison. */
5749 /* E1 and E2 are for scratch. */
5757 type
= composite_pointer_type (location
, type0
, type1
, op0
, op1
,
5758 CPO_COMPARISON
, complain
);
5760 if (!same_type_p (TREE_TYPE (op0
), type
))
5761 op0
= cp_convert_and_check (type
, op0
, complain
);
5762 if (!same_type_p (TREE_TYPE (op1
), type
))
5763 op1
= cp_convert_and_check (type
, op1
, complain
);
5765 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5766 return error_mark_node
;
5768 if (TREE_SIDE_EFFECTS (op0
))
5769 op0
= save_expr (op0
);
5770 if (TREE_SIDE_EFFECTS (op1
))
5771 op1
= save_expr (op1
);
5773 pfn0
= pfn_from_ptrmemfunc (op0
);
5774 pfn0
= cp_fully_fold (pfn0
);
5775 /* Avoid -Waddress warnings (c++/64877). */
5776 if (TREE_CODE (pfn0
) == ADDR_EXPR
)
5777 suppress_warning (pfn0
, OPT_Waddress
);
5778 pfn1
= pfn_from_ptrmemfunc (op1
);
5779 pfn1
= cp_fully_fold (pfn1
);
5780 delta0
= delta_from_ptrmemfunc (op0
);
5781 delta1
= delta_from_ptrmemfunc (op1
);
5782 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5783 == ptrmemfunc_vbit_in_delta
)
5788 && ((op0.delta == op1.delta)
5789 || (!op0.pfn && op0.delta & 1 == 0
5790 && op1.delta & 1 == 0))
5792 The reason for the `!op0.pfn' bit is that a NULL
5793 pointer-to-member is any member with a zero PFN and
5794 LSB of the DELTA field is 0. */
5796 e1
= cp_build_binary_op (location
, BIT_AND_EXPR
,
5800 e1
= cp_build_binary_op (location
,
5801 EQ_EXPR
, e1
, integer_zero_node
,
5803 e2
= cp_build_binary_op (location
, BIT_AND_EXPR
,
5807 e2
= cp_build_binary_op (location
,
5808 EQ_EXPR
, e2
, integer_zero_node
,
5810 e1
= cp_build_binary_op (location
,
5811 TRUTH_ANDIF_EXPR
, e2
, e1
,
5813 e2
= cp_build_binary_op (location
, EQ_EXPR
,
5815 build_zero_cst (TREE_TYPE (pfn0
)),
5817 e2
= cp_build_binary_op (location
,
5818 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
5819 e1
= cp_build_binary_op (location
,
5820 EQ_EXPR
, delta0
, delta1
, complain
);
5821 e1
= cp_build_binary_op (location
,
5822 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
5829 && (!op0.pfn || op0.delta == op1.delta))
5831 The reason for the `!op0.pfn' bit is that a NULL
5832 pointer-to-member is any member with a zero PFN; the
5833 DELTA field is unspecified. */
5835 e1
= cp_build_binary_op (location
,
5836 EQ_EXPR
, delta0
, delta1
, complain
);
5837 e2
= cp_build_binary_op (location
,
5840 build_zero_cst (TREE_TYPE (pfn0
)),
5842 e1
= cp_build_binary_op (location
,
5843 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
5845 e2
= build2 (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
5846 e
= cp_build_binary_op (location
,
5847 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
5848 if (code
== EQ_EXPR
)
5850 return cp_build_binary_op (location
,
5851 EQ_EXPR
, e
, integer_zero_node
, complain
);
5855 gcc_assert (!TYPE_PTRMEMFUNC_P (type0
)
5856 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
),
5858 gcc_assert (!TYPE_PTRMEMFUNC_P (type1
)
5859 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
),
5867 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
5868 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
5870 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5871 result_type
= composite_pointer_type (location
,
5872 type0
, type1
, op0
, op1
,
5873 CPO_COMPARISON
, complain
);
5880 case SPACESHIP_EXPR
:
5881 if (TREE_CODE (orig_op0
) == STRING_CST
5882 || TREE_CODE (orig_op1
) == STRING_CST
)
5884 if (complain
& tf_warning
)
5885 warning_at (location
, OPT_Waddress
,
5886 "comparison with string literal results "
5887 "in unspecified behavior");
5889 else if (warn_array_compare
5890 && TREE_CODE (TREE_TYPE (orig_op0
)) == ARRAY_TYPE
5891 && TREE_CODE (TREE_TYPE (orig_op1
)) == ARRAY_TYPE
5892 && code
!= SPACESHIP_EXPR
5893 && (complain
& tf_warning
))
5894 do_warn_array_compare (location
, code
,
5895 tree_strip_any_location_wrapper (orig_op0
),
5896 tree_strip_any_location_wrapper (orig_op1
));
5898 if (gnu_vector_type_p (type0
) && gnu_vector_type_p (type1
))
5902 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
5904 && !vector_types_compatible_elements_p (type0
, type1
))
5906 if (complain
& tf_error
)
5908 error_at (location
, "comparing vectors with different "
5910 inform (location
, "operand types are %qT and %qT",
5913 return error_mark_node
;
5916 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0
),
5917 TYPE_VECTOR_SUBPARTS (type1
)))
5919 if (complain
& tf_error
)
5921 error_at (location
, "comparing vectors with different "
5922 "number of elements");
5923 inform (location
, "operand types are %qT and %qT",
5926 return error_mark_node
;
5929 /* It's not precisely specified how the usual arithmetic
5930 conversions apply to the vector types. Here, we use
5931 the unsigned type if one of the operands is signed and
5932 the other one is unsigned. */
5933 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
5935 if (!TYPE_UNSIGNED (type0
))
5936 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
5938 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
5939 warning_at (location
, OPT_Wsign_compare
, "comparison between "
5940 "types %qT and %qT", type0
, type1
);
5943 if (resultcode
== SPACESHIP_EXPR
)
5945 if (complain
& tf_error
)
5946 sorry_at (location
, "three-way comparison of vectors");
5947 return error_mark_node
;
5950 /* Always construct signed integer vector type. */
5951 intt
= c_common_type_for_size
5952 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0
))), 0);
5955 if (complain
& tf_error
)
5956 error_at (location
, "could not find an integer type "
5957 "of the same size as %qT", TREE_TYPE (type0
));
5958 return error_mark_node
;
5960 result_type
= build_opaque_vector_type (intt
,
5961 TYPE_VECTOR_SUBPARTS (type0
));
5962 return build_vec_cmp (resultcode
, result_type
, op0
, op1
);
5964 build_type
= boolean_type_node
;
5965 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
5966 || code0
== ENUMERAL_TYPE
)
5967 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5968 || code1
== ENUMERAL_TYPE
))
5970 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5971 result_type
= composite_pointer_type (location
,
5972 type0
, type1
, op0
, op1
,
5973 CPO_COMPARISON
, complain
);
5974 else if ((code0
== POINTER_TYPE
&& null_ptr_cst_p (orig_op1
))
5975 || (code1
== POINTER_TYPE
&& null_ptr_cst_p (orig_op0
))
5976 || (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)))
5978 /* Core Issue 1512 made this ill-formed. */
5979 if (complain
& tf_error
)
5980 error_at (location
, "ordered comparison of pointer with "
5981 "integer zero (%qT and %qT)", type0
, type1
);
5982 return error_mark_node
;
5984 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5986 result_type
= type0
;
5987 if (complain
& tf_error
)
5988 permerror (location
, "ISO C++ forbids comparison between "
5989 "pointer and integer");
5991 return error_mark_node
;
5993 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
5995 result_type
= type1
;
5996 if (complain
& tf_error
)
5997 permerror (location
, "ISO C++ forbids comparison between "
5998 "pointer and integer");
6000 return error_mark_node
;
6003 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
6004 && !processing_template_decl
6005 && sanitize_flags_p (SANITIZE_POINTER_COMPARE
))
6007 op0
= save_expr (op0
);
6008 op1
= save_expr (op1
);
6010 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE
);
6011 instrument_expr
= build_call_expr_loc (location
, tt
, 2, op0
, op1
);
6016 case UNORDERED_EXPR
:
6023 build_type
= integer_type_node
;
6024 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
6026 if (complain
& tf_error
)
6027 error ("unordered comparison on non-floating-point argument");
6028 return error_mark_node
;
6037 if (((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
6038 || code0
== ENUMERAL_TYPE
)
6039 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
6040 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
)))
6041 arithmetic_types_p
= 1;
6044 arithmetic_types_p
= 0;
6045 /* Vector arithmetic is only allowed when both sides are vectors. */
6046 if (gnu_vector_type_p (type0
) && gnu_vector_type_p (type1
))
6048 if (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
6049 || !vector_types_compatible_elements_p (type0
, type1
))
6051 if (complain
& tf_error
)
6053 /* "location" already embeds the locations of the
6054 operands, so we don't need to add them separately
6056 rich_location
richloc (line_table
, location
);
6057 binary_op_error (&richloc
, code
, type0
, type1
);
6059 return error_mark_node
;
6061 arithmetic_types_p
= 1;
6064 /* Determine the RESULT_TYPE, if it is not already known. */
6066 && arithmetic_types_p
6067 && (shorten
|| common
|| short_compare
))
6069 result_type
= cp_common_type (type0
, type1
);
6070 if (result_type
== error_mark_node
6071 && code0
== REAL_TYPE
6072 && code1
== REAL_TYPE
6073 && (extended_float_type_p (type0
) || extended_float_type_p (type1
))
6074 && cp_compare_floating_point_conversion_ranks (type0
, type1
) == 3)
6076 if (complain
& tf_error
)
6078 rich_location
richloc (line_table
, location
);
6079 binary_op_error (&richloc
, code
, type0
, type1
);
6081 return error_mark_node
;
6083 if (complain
& tf_warning
)
6085 do_warn_double_promotion (result_type
, type0
, type1
,
6086 "implicit conversion from %qH to %qI "
6087 "to match other operand of binary "
6090 do_warn_enum_conversions (location
, code
, TREE_TYPE (orig_op0
),
6091 TREE_TYPE (orig_op1
));
6095 if (code
== SPACESHIP_EXPR
)
6097 iloc_sentinel
s (location
);
6099 tree orig_type0
= TREE_TYPE (orig_op0
);
6100 tree_code orig_code0
= TREE_CODE (orig_type0
);
6101 tree orig_type1
= TREE_TYPE (orig_op1
);
6102 tree_code orig_code1
= TREE_CODE (orig_type1
);
6105 else if ((orig_code0
== BOOLEAN_TYPE
) != (orig_code1
== BOOLEAN_TYPE
))
6106 /* "If one of the operands is of type bool and the other is not, the
6107 program is ill-formed." */
6108 result_type
= NULL_TREE
;
6109 else if (code0
== POINTER_TYPE
&& orig_code0
!= POINTER_TYPE
6110 && code1
== POINTER_TYPE
&& orig_code1
!= POINTER_TYPE
)
6111 /* We only do array/function-to-pointer conversion if "at least one of
6112 the operands is of pointer type". */
6113 result_type
= NULL_TREE
;
6114 else if (TYPE_PTRFN_P (result_type
) || NULLPTR_TYPE_P (result_type
))
6115 /* <=> no longer supports equality relations. */
6116 result_type
= NULL_TREE
;
6117 else if (orig_code0
== ENUMERAL_TYPE
&& orig_code1
== ENUMERAL_TYPE
6118 && !(same_type_ignoring_top_level_qualifiers_p
6119 (orig_type0
, orig_type1
)))
6120 /* "If both operands have arithmetic types, or one operand has integral
6121 type and the other operand has unscoped enumeration type, the usual
6122 arithmetic conversions are applied to the operands." So we don't do
6123 arithmetic conversions if the operands both have enumeral type. */
6124 result_type
= NULL_TREE
;
6125 else if ((orig_code0
== ENUMERAL_TYPE
&& orig_code1
== REAL_TYPE
)
6126 || (orig_code0
== REAL_TYPE
&& orig_code1
== ENUMERAL_TYPE
))
6127 /* [depr.arith.conv.enum]: Three-way comparisons between such operands
6128 [where one is of enumeration type and the other is of a different
6129 enumeration type or a floating-point type] are ill-formed. */
6130 result_type
= NULL_TREE
;
6134 build_type
= spaceship_type (result_type
, complain
);
6135 if (build_type
== error_mark_node
)
6136 return error_mark_node
;
6139 if (result_type
&& arithmetic_types_p
)
6141 /* If a narrowing conversion is required, other than from an integral
6142 type to a floating point type, the program is ill-formed. */
6144 if (TREE_CODE (result_type
) == REAL_TYPE
6145 && CP_INTEGRAL_TYPE_P (orig_type0
))
6147 else if (!check_narrowing (result_type
, orig_op0
, complain
))
6149 if (TREE_CODE (result_type
) == REAL_TYPE
6150 && CP_INTEGRAL_TYPE_P (orig_type1
))
6152 else if (!check_narrowing (result_type
, orig_op1
, complain
))
6154 if (!ok
&& !(complain
& tf_error
))
6155 return error_mark_node
;
6161 if (complain
& tf_error
)
6163 binary_op_rich_location
richloc (location
,
6164 orig_op0
, orig_op1
, true);
6166 "invalid operands of types %qT and %qT to binary %qO",
6167 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
6169 return error_mark_node
;
6172 /* If we're in a template, the only thing we need to know is the
6174 if (processing_template_decl
)
6176 /* Since the middle-end checks the type when doing a build2, we
6177 need to build the tree in pieces. This built tree will never
6178 get out of the front-end as we replace it when instantiating
6180 tree tmp
= build2 (resultcode
,
6181 build_type
? build_type
: result_type
,
6183 TREE_OPERAND (tmp
, 0) = op0
;
6187 /* Remember the original type; RESULT_TYPE might be changed later on
6188 by shorten_binary_op. */
6189 tree orig_type
= result_type
;
6191 if (arithmetic_types_p
)
6193 bool first_complex
= (code0
== COMPLEX_TYPE
);
6194 bool second_complex
= (code1
== COMPLEX_TYPE
);
6195 int none_complex
= (!first_complex
&& !second_complex
);
6197 /* Adapted from patch for c/24581. */
6198 if (first_complex
!= second_complex
6199 && (code
== PLUS_EXPR
6200 || code
== MINUS_EXPR
6201 || code
== MULT_EXPR
6202 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
6203 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
6204 && flag_signed_zeros
)
6206 /* An operation on mixed real/complex operands must be
6207 handled specially, but the language-independent code can
6208 more easily optimize the plain complex arithmetic if
6209 -fno-signed-zeros. */
6210 tree real_type
= TREE_TYPE (result_type
);
6214 if (TREE_TYPE (op0
) != result_type
)
6215 op0
= cp_convert_and_check (result_type
, op0
, complain
);
6216 if (TREE_TYPE (op1
) != real_type
)
6217 op1
= cp_convert_and_check (real_type
, op1
, complain
);
6221 if (TREE_TYPE (op0
) != real_type
)
6222 op0
= cp_convert_and_check (real_type
, op0
, complain
);
6223 if (TREE_TYPE (op1
) != result_type
)
6224 op1
= cp_convert_and_check (result_type
, op1
, complain
);
6226 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
6227 return error_mark_node
;
6230 op0
= save_expr (op0
);
6231 real
= cp_build_unary_op (REALPART_EXPR
, op0
, true, complain
);
6232 imag
= cp_build_unary_op (IMAGPART_EXPR
, op0
, true, complain
);
6236 case TRUNC_DIV_EXPR
:
6237 op1
= save_expr (op1
);
6238 imag
= build2 (resultcode
, real_type
, imag
, op1
);
6242 real
= build2 (resultcode
, real_type
, real
, op1
);
6250 op1
= save_expr (op1
);
6251 real
= cp_build_unary_op (REALPART_EXPR
, op1
, true, complain
);
6252 imag
= cp_build_unary_op (IMAGPART_EXPR
, op1
, true, complain
);
6256 op0
= save_expr (op0
);
6257 imag
= build2 (resultcode
, real_type
, op0
, imag
);
6260 real
= build2 (resultcode
, real_type
, op0
, real
);
6263 real
= build2 (resultcode
, real_type
, op0
, real
);
6264 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
6270 result
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
6274 /* For certain operations (which identify themselves by shorten != 0)
6275 if both args were extended from the same smaller type,
6276 do the arithmetic in that type and then extend.
6278 shorten !=0 and !=1 indicates a bitwise operation.
6279 For them, this optimization is safe only if
6280 both args are zero-extended or both are sign-extended.
6281 Otherwise, we might change the result.
6282 E.g., (short)-1 | (unsigned short)-1 is (int)-1
6283 but calculated in (unsigned short) it would be (unsigned short)-1. */
6285 if (shorten
&& none_complex
)
6287 final_type
= result_type
;
6288 result_type
= shorten_binary_op (result_type
, op0
, op1
,
6292 /* Shifts can be shortened if shifting right. */
6297 tree arg0
= get_narrower (op0
, &unsigned_arg
);
6298 /* We're not really warning here but when we set short_shift we
6299 used fold_for_warn to fold the operand. */
6300 tree const_op1
= fold_for_warn (op1
);
6302 final_type
= result_type
;
6304 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
6305 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
6307 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
6308 && tree_int_cst_sgn (const_op1
) > 0
6309 /* We can shorten only if the shift count is less than the
6310 number of bits in the smaller type size. */
6311 && compare_tree_int (const_op1
,
6312 TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
6313 /* We cannot drop an unsigned shift after sign-extension. */
6314 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
6316 /* Do an unsigned shift if the operand was zero-extended. */
6318 = c_common_signed_or_unsigned_type (unsigned_arg
,
6320 /* Convert value-to-be-shifted to that type. */
6321 if (TREE_TYPE (op0
) != result_type
)
6322 op0
= convert (result_type
, op0
);
6327 /* Comparison operations are shortened too but differently.
6328 They identify themselves by setting short_compare = 1. */
6332 /* We call shorten_compare only for diagnostics. */
6333 tree xop0
= fold_simple (op0
);
6334 tree xop1
= fold_simple (op1
);
6335 tree xresult_type
= result_type
;
6336 enum tree_code xresultcode
= resultcode
;
6337 shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
6341 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
6342 && warn_sign_compare
6343 /* Do not warn until the template is instantiated; we cannot
6344 bound the ranges of the arguments until that point. */
6345 && !processing_template_decl
6346 && (complain
& tf_warning
)
6347 && c_inhibit_evaluation_warnings
== 0
6348 /* Even unsigned enum types promote to signed int. We don't
6349 want to issue -Wsign-compare warnings for this case. */
6350 && !enum_cast_to_int (orig_op0
)
6351 && !enum_cast_to_int (orig_op1
))
6353 warn_for_sign_compare (location
, orig_op0
, orig_op1
, op0
, op1
,
6354 result_type
, resultcode
);
6358 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6359 Then the expression will be built.
6360 It will be given type FINAL_TYPE if that is nonzero;
6361 otherwise, it will be given type RESULT_TYPE. */
6364 warning_sentinel
w (warn_sign_conversion
, short_compare
);
6365 if (!same_type_p (TREE_TYPE (op0
), result_type
))
6366 op0
= cp_convert_and_check (result_type
, op0
, complain
);
6367 if (!same_type_p (TREE_TYPE (op1
), result_type
))
6368 op1
= cp_convert_and_check (result_type
, op1
, complain
);
6370 if (op0
== error_mark_node
|| op1
== error_mark_node
)
6371 return error_mark_node
;
6374 if (build_type
== NULL_TREE
)
6375 build_type
= result_type
;
6378 && flag_strong_eval_order
== 2
6379 && TREE_SIDE_EFFECTS (op1
)
6380 && !processing_template_decl
)
6382 /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6383 op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6384 op0
= cp_save_expr (op0
);
6385 instrument_expr
= op0
;
6388 if (sanitize_flags_p ((SANITIZE_SHIFT
6390 | SANITIZE_FLOAT_DIVIDE
6391 | SANITIZE_SI_OVERFLOW
))
6392 && current_function_decl
!= NULL_TREE
6393 && !processing_template_decl
6394 && (doing_div_or_mod
|| doing_shift
))
6396 /* OP0 and/or OP1 might have side-effects. */
6397 op0
= cp_save_expr (op0
);
6398 op1
= cp_save_expr (op1
);
6399 op0
= fold_non_dependent_expr (op0
, complain
);
6400 op1
= fold_non_dependent_expr (op1
, complain
);
6401 tree instrument_expr1
= NULL_TREE
;
6402 if (doing_div_or_mod
6403 && sanitize_flags_p (SANITIZE_DIVIDE
6404 | SANITIZE_FLOAT_DIVIDE
6405 | SANITIZE_SI_OVERFLOW
))
6407 /* For diagnostics we want to use the promoted types without
6408 shorten_binary_op. So convert the arguments to the
6409 original result_type. */
6412 if (TREE_TYPE (cop0
) != orig_type
)
6413 cop0
= cp_convert (orig_type
, op0
, complain
);
6414 if (TREE_TYPE (cop1
) != orig_type
)
6415 cop1
= cp_convert (orig_type
, op1
, complain
);
6416 instrument_expr1
= ubsan_instrument_division (location
, cop0
, cop1
);
6418 else if (doing_shift
&& sanitize_flags_p (SANITIZE_SHIFT
))
6419 instrument_expr1
= ubsan_instrument_shift (location
, code
, op0
, op1
);
6420 if (instrument_expr
!= NULL
)
6421 instrument_expr
= add_stmt_to_compound (instrument_expr
,
6424 instrument_expr
= instrument_expr1
;
6427 result
= build2_loc (location
, resultcode
, build_type
, op0
, op1
);
6428 if (final_type
!= 0)
6429 result
= cp_convert (final_type
, result
, complain
);
6431 if (instrument_expr
!= NULL
)
6432 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
6433 instrument_expr
, result
);
6435 if (resultcode
== SPACESHIP_EXPR
&& !processing_template_decl
)
6436 result
= get_target_expr (result
, complain
);
6438 if (!c_inhibit_evaluation_warnings
)
6440 if (!processing_template_decl
)
6442 op0
= cp_fully_fold (op0
);
6443 /* Only consider the second argument if the first isn't overflowed. */
6444 if (!CONSTANT_CLASS_P (op0
) || TREE_OVERFLOW_P (op0
))
6446 op1
= cp_fully_fold (op1
);
6447 if (!CONSTANT_CLASS_P (op1
) || TREE_OVERFLOW_P (op1
))
6450 else if (!CONSTANT_CLASS_P (op0
) || !CONSTANT_CLASS_P (op1
)
6451 || TREE_OVERFLOW_P (op0
) || TREE_OVERFLOW_P (op1
))
6454 tree result_ovl
= fold_build2 (resultcode
, build_type
, op0
, op1
);
6455 if (TREE_OVERFLOW_P (result_ovl
))
6456 overflow_warning (location
, result_ovl
);
6462 /* Build a VEC_PERM_EXPR.
6463 This is a simple wrapper for c_build_vec_perm_expr. */
6465 build_x_vec_perm_expr (location_t loc
,
6466 tree arg0
, tree arg1
, tree arg2
,
6467 tsubst_flags_t complain
)
6469 tree orig_arg0
= arg0
;
6470 tree orig_arg1
= arg1
;
6471 tree orig_arg2
= arg2
;
6472 if (processing_template_decl
)
6474 if (type_dependent_expression_p (arg0
)
6475 || type_dependent_expression_p (arg1
)
6476 || type_dependent_expression_p (arg2
))
6477 return build_min_nt_loc (loc
, VEC_PERM_EXPR
, arg0
, arg1
, arg2
);
6478 arg0
= build_non_dependent_expr (arg0
);
6480 arg1
= build_non_dependent_expr (arg1
);
6481 arg2
= build_non_dependent_expr (arg2
);
6483 tree exp
= c_build_vec_perm_expr (loc
, arg0
, arg1
, arg2
, complain
& tf_error
);
6484 if (processing_template_decl
&& exp
!= error_mark_node
)
6485 return build_min_non_dep (VEC_PERM_EXPR
, exp
, orig_arg0
,
6486 orig_arg1
, orig_arg2
);
6490 /* Build a VEC_PERM_EXPR.
6491 This is a simple wrapper for c_build_shufflevector. */
6493 build_x_shufflevector (location_t loc
, vec
<tree
, va_gc
> *args
,
6494 tsubst_flags_t complain
)
6496 tree arg0
= (*args
)[0];
6497 tree arg1
= (*args
)[1];
6498 if (processing_template_decl
)
6500 for (unsigned i
= 0; i
< args
->length (); ++i
)
6502 ? type_dependent_expression_p ((*args
)[i
])
6503 : instantiation_dependent_expression_p ((*args
)[i
]))
6505 tree exp
= build_min_nt_call_vec (NULL
, args
);
6506 CALL_EXPR_IFN (exp
) = IFN_SHUFFLEVECTOR
;
6509 arg0
= build_non_dependent_expr (arg0
);
6510 arg1
= build_non_dependent_expr (arg1
);
6511 /* ??? Nothing needed for the index arguments? */
6513 auto_vec
<tree
, 16> mask
;
6514 for (unsigned i
= 2; i
< args
->length (); ++i
)
6516 tree idx
= fold_non_dependent_expr ((*args
)[i
], complain
);
6517 mask
.safe_push (idx
);
6519 tree exp
= c_build_shufflevector (loc
, arg0
, arg1
, mask
, complain
& tf_error
);
6520 if (processing_template_decl
&& exp
!= error_mark_node
)
6522 exp
= build_min_non_dep_call_vec (exp
, NULL
, args
);
6523 CALL_EXPR_IFN (exp
) = IFN_SHUFFLEVECTOR
;
6528 /* Return a tree for the sum or difference (RESULTCODE says which)
6529 of pointer PTROP and integer INTOP. */
6532 cp_pointer_int_sum (location_t loc
, enum tree_code resultcode
, tree ptrop
,
6533 tree intop
, tsubst_flags_t complain
)
6535 tree res_type
= TREE_TYPE (ptrop
);
6537 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
6538 in certain circumstance (when it's valid to do so). So we need
6539 to make sure it's complete. We don't need to check here, if we
6540 can actually complete it at all, as those checks will be done in
6541 pointer_int_sum() anyway. */
6542 complete_type (TREE_TYPE (res_type
));
6544 return pointer_int_sum (loc
, resultcode
, ptrop
,
6545 intop
, complain
& tf_warning_or_error
);
6548 /* Return a tree for the difference of pointers OP0 and OP1.
6549 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
6550 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
6553 pointer_diff (location_t loc
, tree op0
, tree op1
, tree ptrtype
,
6554 tsubst_flags_t complain
, tree
*instrument_expr
)
6556 tree result
, inttype
;
6557 tree restype
= ptrdiff_type_node
;
6558 tree target_type
= TREE_TYPE (ptrtype
);
6560 if (!complete_type_or_maybe_complain (target_type
, NULL_TREE
, complain
))
6561 return error_mark_node
;
6563 if (VOID_TYPE_P (target_type
))
6565 if (complain
& tf_error
)
6566 permerror (loc
, "ISO C++ forbids using pointer of "
6567 "type %<void *%> in subtraction");
6569 return error_mark_node
;
6571 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
6573 if (complain
& tf_error
)
6574 permerror (loc
, "ISO C++ forbids using pointer to "
6575 "a function in subtraction");
6577 return error_mark_node
;
6579 if (TREE_CODE (target_type
) == METHOD_TYPE
)
6581 if (complain
& tf_error
)
6582 permerror (loc
, "ISO C++ forbids using pointer to "
6583 "a method in subtraction");
6585 return error_mark_node
;
6587 else if (!verify_type_context (loc
, TCTX_POINTER_ARITH
,
6588 TREE_TYPE (TREE_TYPE (op0
)),
6589 !(complain
& tf_error
))
6590 || !verify_type_context (loc
, TCTX_POINTER_ARITH
,
6591 TREE_TYPE (TREE_TYPE (op1
)),
6592 !(complain
& tf_error
)))
6593 return error_mark_node
;
6595 /* Determine integer type result of the subtraction. This will usually
6596 be the same as the result type (ptrdiff_t), but may need to be a wider
6597 type if pointers for the address space are wider than ptrdiff_t. */
6598 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
6599 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
6603 if (!processing_template_decl
6604 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT
))
6606 op0
= save_expr (op0
);
6607 op1
= save_expr (op1
);
6609 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT
);
6610 *instrument_expr
= build_call_expr_loc (loc
, tt
, 2, op0
, op1
);
6613 /* First do the subtraction, then build the divide operator
6614 and only convert at the very end.
6615 Do not do default conversions in case restype is a short type. */
6617 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
6618 pointers. If some platform cannot provide that, or has a larger
6619 ptrdiff_type to support differences larger than half the address
6620 space, cast the pointers to some larger integer type and do the
6621 computations in that type. */
6622 if (TYPE_PRECISION (inttype
) > TYPE_PRECISION (TREE_TYPE (op0
)))
6623 op0
= cp_build_binary_op (loc
,
6625 cp_convert (inttype
, op0
, complain
),
6626 cp_convert (inttype
, op1
, complain
),
6629 op0
= build2_loc (loc
, POINTER_DIFF_EXPR
, inttype
, op0
, op1
);
6631 /* This generates an error if op1 is a pointer to an incomplete type. */
6632 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
6634 if (complain
& tf_error
)
6635 error_at (loc
, "invalid use of a pointer to an incomplete type in "
6636 "pointer arithmetic");
6638 return error_mark_node
;
6641 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1
)))
6643 if (complain
& tf_error
)
6644 error_at (loc
, "arithmetic on pointer to an empty aggregate");
6646 return error_mark_node
;
6649 op1
= (TYPE_PTROB_P (ptrtype
)
6650 ? size_in_bytes_loc (loc
, target_type
)
6651 : integer_one_node
);
6653 /* Do the division. */
6655 result
= build2_loc (loc
, EXACT_DIV_EXPR
, inttype
, op0
,
6656 cp_convert (inttype
, op1
, complain
));
6657 return cp_convert (restype
, result
, complain
);
6660 /* Construct and perhaps optimize a tree representation
6661 for a unary operation. CODE, a tree_code, specifies the operation
6662 and XARG is the operand. */
6665 build_x_unary_op (location_t loc
, enum tree_code code
, cp_expr xarg
,
6666 tree lookups
, tsubst_flags_t complain
)
6668 tree orig_expr
= xarg
;
6671 tree overload
= NULL_TREE
;
6673 if (processing_template_decl
)
6675 if (type_dependent_expression_p (xarg
))
6677 tree e
= build_min_nt_loc (loc
, code
, xarg
.get_value (), NULL_TREE
);
6678 TREE_TYPE (e
) = build_dependent_operator_type (lookups
, code
, false);
6682 xarg
= build_non_dependent_expr (xarg
);
6687 /* [expr.unary.op] says:
6689 The address of an object of incomplete type can be taken.
6691 (And is just the ordinary address operator, not an overloaded
6692 "operator &".) However, if the type is a template
6693 specialization, we must complete the type at this point so that
6694 an overloaded "operator &" will be available if required. */
6695 if (code
== ADDR_EXPR
6696 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
6697 && ((CLASS_TYPE_P (TREE_TYPE (xarg
))
6698 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg
))))
6699 || (TREE_CODE (xarg
) == OFFSET_REF
)))
6700 /* Don't look for a function. */;
6702 exp
= build_new_op (loc
, code
, LOOKUP_NORMAL
, xarg
, NULL_TREE
,
6703 NULL_TREE
, lookups
, &overload
, complain
);
6705 if (!exp
&& code
== ADDR_EXPR
)
6707 if (is_overloaded_fn (xarg
))
6709 tree fn
= get_first_fn (xarg
);
6710 if (DECL_CONSTRUCTOR_P (fn
) || DECL_DESTRUCTOR_P (fn
))
6712 if (complain
& tf_error
)
6713 error_at (loc
, DECL_CONSTRUCTOR_P (fn
)
6714 ? G_("taking address of constructor %qD")
6715 : G_("taking address of destructor %qD"),
6717 return error_mark_node
;
6721 /* A pointer to member-function can be formed only by saying
6723 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
6724 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
6726 if (TREE_CODE (xarg
) != OFFSET_REF
6727 || !TYPE_P (TREE_OPERAND (xarg
, 0)))
6729 if (complain
& tf_error
)
6731 error_at (loc
, "invalid use of %qE to form a "
6732 "pointer-to-member-function", xarg
.get_value ());
6733 if (TREE_CODE (xarg
) != OFFSET_REF
)
6734 inform (loc
, " a qualified-id is required");
6736 return error_mark_node
;
6740 if (complain
& tf_error
)
6741 error_at (loc
, "parentheses around %qE cannot be used to "
6742 "form a pointer-to-member-function",
6745 return error_mark_node
;
6746 PTRMEM_OK_P (xarg
) = 1;
6750 if (TREE_CODE (xarg
) == OFFSET_REF
)
6752 ptrmem
= PTRMEM_OK_P (xarg
);
6754 if (!ptrmem
&& !flag_ms_extensions
6755 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
6757 /* A single non-static member, make sure we don't allow a
6758 pointer-to-member. */
6759 xarg
= build2 (OFFSET_REF
, TREE_TYPE (xarg
),
6760 TREE_OPERAND (xarg
, 0),
6761 ovl_make (TREE_OPERAND (xarg
, 1)));
6762 PTRMEM_OK_P (xarg
) = ptrmem
;
6766 exp
= cp_build_addr_expr_strict (xarg
, complain
);
6768 if (TREE_CODE (exp
) == PTRMEM_CST
)
6769 PTRMEM_CST_LOCATION (exp
) = loc
;
6771 protected_set_expr_location (exp
, loc
);
6774 if (processing_template_decl
&& exp
!= error_mark_node
)
6776 if (overload
!= NULL_TREE
)
6777 return (build_min_non_dep_op_overload
6778 (code
, exp
, overload
, orig_expr
, integer_zero_node
));
6780 exp
= build_min_non_dep (code
, exp
, orig_expr
,
6781 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE
);
6783 if (TREE_CODE (exp
) == ADDR_EXPR
)
6784 PTRMEM_OK_P (exp
) = ptrmem
;
6788 /* Construct and perhaps optimize a tree representation
6789 for __builtin_addressof operation. ARG specifies the operand. */
6792 cp_build_addressof (location_t loc
, tree arg
, tsubst_flags_t complain
)
6794 tree orig_expr
= arg
;
6796 if (processing_template_decl
)
6798 if (type_dependent_expression_p (arg
))
6799 return build_min_nt_loc (loc
, ADDRESSOF_EXPR
, arg
, NULL_TREE
);
6801 arg
= build_non_dependent_expr (arg
);
6804 tree exp
= cp_build_addr_expr_strict (arg
, complain
);
6806 if (processing_template_decl
&& exp
!= error_mark_node
)
6807 exp
= build_min_non_dep (ADDRESSOF_EXPR
, exp
, orig_expr
, NULL_TREE
);
6811 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
6812 constants, where a null value is represented by an INTEGER_CST of
6816 cp_truthvalue_conversion (tree expr
, tsubst_flags_t complain
)
6818 tree type
= TREE_TYPE (expr
);
6819 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6820 if (TYPE_PTR_OR_PTRMEM_P (type
)
6821 /* Avoid ICE on invalid use of non-static member function. */
6822 || TREE_CODE (expr
) == FUNCTION_DECL
)
6823 return cp_build_binary_op (loc
, NE_EXPR
, expr
, nullptr_node
, complain
);
6825 return c_common_truthvalue_conversion (loc
, expr
);
6828 /* Returns EXPR contextually converted to bool. */
6831 contextual_conv_bool (tree expr
, tsubst_flags_t complain
)
6833 return perform_implicit_conversion_flags (boolean_type_node
, expr
,
6834 complain
, LOOKUP_NORMAL
);
6837 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
6838 is a low-level function; most callers should use maybe_convert_cond. */
6841 condition_conversion (tree expr
)
6843 tree t
= contextual_conv_bool (expr
, tf_warning_or_error
);
6844 if (!processing_template_decl
)
6845 t
= fold_build_cleanup_point_expr (boolean_type_node
, t
);
6849 /* Returns the address of T. This function will fold away
6850 ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
6851 most places should use cp_build_addr_expr instead. */
6854 build_address (tree t
)
6856 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
6857 return error_mark_node
;
6858 gcc_checking_assert (TREE_CODE (t
) != CONSTRUCTOR
6859 || processing_template_decl
);
6860 t
= build_fold_addr_expr_loc (EXPR_LOCATION (t
), t
);
6861 if (TREE_CODE (t
) != ADDR_EXPR
)
6866 /* Return a NOP_EXPR converting EXPR to TYPE. */
6869 build_nop (tree type
, tree expr
)
6871 if (type
== error_mark_node
|| error_operand_p (expr
))
6873 return build1_loc (EXPR_LOCATION (expr
), NOP_EXPR
, type
, expr
);
6876 /* Take the address of ARG, whatever that means under C++ semantics.
6877 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
6878 and class rvalues as well.
6880 Nothing should call this function directly; instead, callers should use
6881 cp_build_addr_expr or cp_build_addr_expr_strict. */
6884 cp_build_addr_expr_1 (tree arg
, bool strict_lvalue
, tsubst_flags_t complain
)
6889 if (!arg
|| error_operand_p (arg
))
6890 return error_mark_node
;
6892 arg
= mark_lvalue_use (arg
);
6893 if (error_operand_p (arg
))
6894 return error_mark_node
;
6896 argtype
= lvalue_type (arg
);
6897 location_t loc
= cp_expr_loc_or_input_loc (arg
);
6899 gcc_assert (!(identifier_p (arg
) && IDENTIFIER_ANY_OP_P (arg
)));
6901 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
6902 && !really_overloaded_fn (arg
))
6904 /* They're trying to take the address of a unique non-static
6905 member function. This is ill-formed (except in MS-land),
6906 but let's try to DTRT.
6907 Note: We only handle unique functions here because we don't
6908 want to complain if there's a static overload; non-unique
6909 cases will be handled by instantiate_type. But we need to
6910 handle this case here to allow casts on the resulting PMF.
6911 We could defer this in non-MS mode, but it's easier to give
6912 a useful error here. */
6914 /* Inside constant member functions, the `this' pointer
6915 contains an extra const qualifier. TYPE_MAIN_VARIANT
6916 is used here to remove this const from the diagnostics
6917 and the created OFFSET_REF. */
6918 tree base
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg
, 0)));
6919 tree fn
= get_first_fn (TREE_OPERAND (arg
, 1));
6920 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
6921 return error_mark_node
;
6923 if (! flag_ms_extensions
)
6925 tree name
= DECL_NAME (fn
);
6926 if (!(complain
& tf_error
))
6927 return error_mark_node
;
6928 else if (current_class_type
6929 && TREE_OPERAND (arg
, 0) == current_class_ref
)
6930 /* An expression like &memfn. */
6932 "ISO C++ forbids taking the address of an unqualified"
6933 " or parenthesized non-static member function to form"
6934 " a pointer to member function. Say %<&%T::%D%>",
6938 "ISO C++ forbids taking the address of a bound member"
6939 " function to form a pointer to member function."
6943 arg
= build_offset_ref (base
, fn
, /*address_p=*/true, complain
);
6946 /* Uninstantiated types are all functions. Taking the
6947 address of a function is a no-op, so just return the
6949 if (type_unknown_p (arg
))
6950 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
6952 if (TREE_CODE (arg
) == OFFSET_REF
)
6953 /* We want a pointer to member; bypass all the code for actually taking
6954 the address of something. */
6957 /* Anything not already handled and not a true memory reference
6959 if (!FUNC_OR_METHOD_TYPE_P (argtype
))
6961 cp_lvalue_kind kind
= lvalue_kind (arg
);
6962 if (kind
== clk_none
)
6964 if (complain
& tf_error
)
6965 lvalue_error (loc
, lv_addressof
);
6966 return error_mark_node
;
6968 if (strict_lvalue
&& (kind
& (clk_rvalueref
|clk_class
)))
6970 if (!(complain
& tf_error
))
6971 return error_mark_node
;
6972 /* Make this a permerror because we used to accept it. */
6973 permerror (loc
, "taking address of rvalue");
6977 if (TYPE_REF_P (argtype
))
6979 tree type
= build_pointer_type (TREE_TYPE (argtype
));
6980 arg
= build1 (CONVERT_EXPR
, type
, arg
);
6983 else if (pedantic
&& DECL_MAIN_P (tree_strip_any_location_wrapper (arg
)))
6986 /* Apparently a lot of autoconf scripts for C++ packages do this,
6987 so only complain if -Wpedantic. */
6988 if (complain
& (flag_pedantic_errors
? tf_error
: tf_warning
))
6989 pedwarn (loc
, OPT_Wpedantic
,
6990 "ISO C++ forbids taking address of function %<::main%>");
6991 else if (flag_pedantic_errors
)
6992 return error_mark_node
;
6995 /* Let &* cancel out to simplify resulting code. */
6996 if (INDIRECT_REF_P (arg
))
6998 arg
= TREE_OPERAND (arg
, 0);
6999 if (TYPE_REF_P (TREE_TYPE (arg
)))
7001 tree type
= build_pointer_type (TREE_TYPE (TREE_TYPE (arg
)));
7002 arg
= build1 (CONVERT_EXPR
, type
, arg
);
7005 /* Don't let this be an lvalue. */
7010 /* Handle complex lvalues (when permitted)
7011 by reduction to simpler cases. */
7012 val
= unary_complex_lvalue (ADDR_EXPR
, arg
);
7016 switch (TREE_CODE (arg
))
7020 case FIX_TRUNC_EXPR
:
7021 /* We should have handled this above in the lvalue_kind check. */
7026 arg
= BASELINK_FUNCTIONS (arg
);
7030 arg
= OVL_FIRST (arg
);
7035 /* Turn a reference to a non-static data member into a
7036 pointer-to-member. */
7041 gcc_assert (PTRMEM_OK_P (arg
));
7043 t
= TREE_OPERAND (arg
, 1);
7044 if (TYPE_REF_P (TREE_TYPE (t
)))
7046 if (complain
& tf_error
)
7048 "cannot create pointer to reference member %qD", t
);
7049 return error_mark_node
;
7052 /* Forming a pointer-to-member is a use of non-pure-virtual fns. */
7053 if (TREE_CODE (t
) == FUNCTION_DECL
7054 && !DECL_PURE_VIRTUAL_P (t
)
7055 && !mark_used (t
, complain
) && !(complain
& tf_error
))
7056 return error_mark_node
;
7058 type
= build_ptrmem_type (context_for_name_lookup (t
),
7060 t
= make_ptrmem_cst (type
, t
);
7068 if (argtype
!= error_mark_node
)
7069 argtype
= build_pointer_type (argtype
);
7071 if (bitfield_p (arg
))
7073 if (complain
& tf_error
)
7074 error_at (loc
, "attempt to take address of bit-field");
7075 return error_mark_node
;
7078 /* In a template, we are processing a non-dependent expression
7079 so we can just form an ADDR_EXPR with the correct type. */
7080 if (processing_template_decl
|| TREE_CODE (arg
) != COMPONENT_REF
)
7082 if (!mark_single_function (arg
, complain
))
7083 return error_mark_node
;
7084 val
= build_address (arg
);
7085 if (TREE_CODE (arg
) == OFFSET_REF
)
7086 PTRMEM_OK_P (val
) = PTRMEM_OK_P (arg
);
7088 else if (BASELINK_P (TREE_OPERAND (arg
, 1)))
7090 tree fn
= BASELINK_FUNCTIONS (TREE_OPERAND (arg
, 1));
7092 /* We can only get here with a single static member
7094 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
7095 && DECL_STATIC_FUNCTION_P (fn
));
7096 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
7097 return error_mark_node
;
7098 val
= build_address (fn
);
7099 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
7100 /* Do not lose object's side effects. */
7101 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
),
7102 TREE_OPERAND (arg
, 0), val
);
7106 tree object
= TREE_OPERAND (arg
, 0);
7107 tree field
= TREE_OPERAND (arg
, 1);
7108 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7109 (TREE_TYPE (object
), decl_type_context (field
)));
7110 val
= build_address (arg
);
7113 if (TYPE_PTR_P (argtype
)
7114 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
7116 build_ptrmemfunc_type (argtype
);
7117 val
= build_ptrmemfunc (argtype
, val
, 0,
7122 /* For addresses of immediate functions ensure we have EXPR_LOCATION
7123 set for possible later diagnostics. */
7124 if (TREE_CODE (val
) == ADDR_EXPR
7125 && TREE_CODE (TREE_OPERAND (val
, 0)) == FUNCTION_DECL
7126 && DECL_IMMEDIATE_FUNCTION_P (TREE_OPERAND (val
, 0)))
7127 SET_EXPR_LOCATION (val
, input_location
);
7132 /* Take the address of ARG if it has one, even if it's an rvalue. */
7135 cp_build_addr_expr (tree arg
, tsubst_flags_t complain
)
7137 return cp_build_addr_expr_1 (arg
, 0, complain
);
7140 /* Take the address of ARG, but only if it's an lvalue. */
7143 cp_build_addr_expr_strict (tree arg
, tsubst_flags_t complain
)
7145 return cp_build_addr_expr_1 (arg
, 1, complain
);
7148 /* C++: Must handle pointers to members.
7150 Perhaps type instantiation should be extended to handle conversion
7151 from aggregates to types we don't yet know we want? (Or are those
7152 cases typically errors which should be reported?)
7154 NOCONVERT suppresses the default promotions (such as from short to int). */
7157 cp_build_unary_op (enum tree_code code
, tree xarg
, bool noconvert
,
7158 tsubst_flags_t complain
)
7160 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
7162 location_t location
= cp_expr_loc_or_input_loc (arg
);
7164 const char *errstring
= NULL
;
7166 const char *invalid_op_diag
;
7168 if (!arg
|| error_operand_p (arg
))
7169 return error_mark_node
;
7171 arg
= resolve_nondeduced_context (arg
, complain
);
7173 if ((invalid_op_diag
7174 = targetm
.invalid_unary_op ((code
== UNARY_PLUS_EXPR
7179 if (complain
& tf_error
)
7180 error (invalid_op_diag
);
7181 return error_mark_node
;
7186 case UNARY_PLUS_EXPR
:
7189 int flags
= WANT_ARITH
| WANT_ENUM
;
7190 /* Unary plus (but not unary minus) is allowed on pointers. */
7191 if (code
== UNARY_PLUS_EXPR
)
7192 flags
|= WANT_POINTER
;
7193 arg
= build_expr_type_conversion (flags
, arg
, true);
7195 errstring
= (code
== NEGATE_EXPR
7196 ? _("wrong type argument to unary minus")
7197 : _("wrong type argument to unary plus"));
7200 if (!noconvert
&& INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg
)))
7201 arg
= cp_perform_integral_promotions (arg
, complain
);
7203 /* Make sure the result is not an lvalue: a unary plus or minus
7204 expression is always a rvalue. */
7211 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
7216 arg
= cp_default_conversion (arg
, complain
);
7217 if (arg
== error_mark_node
)
7218 return error_mark_node
;
7221 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
7222 | WANT_VECTOR_OR_COMPLEX
,
7224 errstring
= _("wrong type argument to bit-complement");
7225 else if (!noconvert
&& INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg
)))
7227 /* Warn if the expression has boolean value. */
7228 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
7229 && (complain
& tf_warning
)
7230 && warning_at (location
, OPT_Wbool_operation
,
7231 "%<~%> on an expression of type %<bool%>"))
7232 inform (location
, "did you mean to use logical not (%<!%>)?");
7233 arg
= cp_perform_integral_promotions (arg
, complain
);
7235 else if (!noconvert
&& VECTOR_TYPE_P (TREE_TYPE (arg
)))
7236 arg
= mark_rvalue_use (arg
);
7240 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
7241 errstring
= _("wrong type argument to abs");
7242 else if (!noconvert
)
7244 arg
= cp_default_conversion (arg
, complain
);
7245 if (arg
== error_mark_node
)
7246 return error_mark_node
;
7251 /* Conjugating a real value is a no-op, but allow it anyway. */
7252 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
7253 errstring
= _("wrong type argument to conjugation");
7254 else if (!noconvert
)
7256 arg
= cp_default_conversion (arg
, complain
);
7257 if (arg
== error_mark_node
)
7258 return error_mark_node
;
7262 case TRUTH_NOT_EXPR
:
7263 if (gnu_vector_type_p (TREE_TYPE (arg
)))
7264 return cp_build_binary_op (input_location
, EQ_EXPR
, arg
,
7265 build_zero_cst (TREE_TYPE (arg
)), complain
);
7266 arg
= perform_implicit_conversion (boolean_type_node
, arg
,
7268 val
= invert_truthvalue_loc (location
, arg
);
7269 if (arg
!= error_mark_node
)
7271 errstring
= _("in argument to unary !");
7279 arg
= build_real_imag_expr (input_location
, code
, arg
);
7282 case PREINCREMENT_EXPR
:
7283 case POSTINCREMENT_EXPR
:
7284 case PREDECREMENT_EXPR
:
7285 case POSTDECREMENT_EXPR
:
7286 /* Handle complex lvalues (when permitted)
7287 by reduction to simpler cases. */
7289 val
= unary_complex_lvalue (code
, arg
);
7293 arg
= mark_lvalue_use (arg
);
7295 /* Increment or decrement the real part of the value,
7296 and don't change the imaginary part. */
7297 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
7301 arg
= cp_stabilize_reference (arg
);
7302 real
= cp_build_unary_op (REALPART_EXPR
, arg
, true, complain
);
7303 imag
= cp_build_unary_op (IMAGPART_EXPR
, arg
, true, complain
);
7304 real
= cp_build_unary_op (code
, real
, true, complain
);
7305 if (real
== error_mark_node
|| imag
== error_mark_node
)
7306 return error_mark_node
;
7307 return build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
7311 /* Report invalid types. */
7313 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
7316 if (code
== PREINCREMENT_EXPR
)
7317 errstring
= _("no pre-increment operator for type");
7318 else if (code
== POSTINCREMENT_EXPR
)
7319 errstring
= _("no post-increment operator for type");
7320 else if (code
== PREDECREMENT_EXPR
)
7321 errstring
= _("no pre-decrement operator for type");
7323 errstring
= _("no post-decrement operator for type");
7326 else if (arg
== error_mark_node
)
7327 return error_mark_node
;
7329 /* Report something read-only. */
7331 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
7332 || TREE_READONLY (arg
))
7334 if (complain
& tf_error
)
7335 cxx_readonly_error (location
, arg
,
7336 ((code
== PREINCREMENT_EXPR
7337 || code
== POSTINCREMENT_EXPR
)
7338 ? lv_increment
: lv_decrement
));
7340 return error_mark_node
;
7345 tree declared_type
= unlowered_expr_type (arg
);
7347 argtype
= TREE_TYPE (arg
);
7349 /* ARM $5.2.5 last annotation says this should be forbidden. */
7350 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
7352 if (complain
& tf_error
)
7353 permerror (location
, (code
== PREINCREMENT_EXPR
7354 || code
== POSTINCREMENT_EXPR
)
7355 ? G_("ISO C++ forbids incrementing an enum")
7356 : G_("ISO C++ forbids decrementing an enum"));
7358 return error_mark_node
;
7361 /* Compute the increment. */
7363 if (TYPE_PTR_P (argtype
))
7365 tree type
= complete_type (TREE_TYPE (argtype
));
7367 if (!COMPLETE_OR_VOID_TYPE_P (type
))
7369 if (complain
& tf_error
)
7370 error_at (location
, ((code
== PREINCREMENT_EXPR
7371 || code
== POSTINCREMENT_EXPR
))
7372 ? G_("cannot increment a pointer to incomplete "
7374 : G_("cannot decrement a pointer to incomplete "
7376 TREE_TYPE (argtype
));
7378 return error_mark_node
;
7380 else if (!TYPE_PTROB_P (argtype
))
7382 if (complain
& tf_error
)
7383 pedwarn (location
, OPT_Wpointer_arith
,
7384 (code
== PREINCREMENT_EXPR
7385 || code
== POSTINCREMENT_EXPR
)
7386 ? G_("ISO C++ forbids incrementing a pointer "
7388 : G_("ISO C++ forbids decrementing a pointer "
7392 return error_mark_node
;
7394 else if (!verify_type_context (location
, TCTX_POINTER_ARITH
,
7395 TREE_TYPE (argtype
),
7396 !(complain
& tf_error
)))
7397 return error_mark_node
;
7399 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
7402 inc
= VECTOR_TYPE_P (argtype
)
7403 ? build_one_cst (argtype
)
7406 inc
= cp_convert (argtype
, inc
, complain
);
7408 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
7409 need to ask Objective-C to build the increment or decrement
7410 expression for it. */
7411 if (objc_is_property_ref (arg
))
7412 return objc_build_incr_expr_for_property_ref (input_location
, code
,
7415 /* Complain about anything else that is not a true lvalue. */
7416 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
7417 || code
== POSTINCREMENT_EXPR
)
7418 ? lv_increment
: lv_decrement
),
7420 return error_mark_node
;
7422 /* [depr.volatile.type] "Postfix ++ and -- expressions and
7423 prefix ++ and -- expressions of volatile-qualified arithmetic
7424 and pointer types are deprecated." */
7425 if (TREE_THIS_VOLATILE (arg
) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg
)))
7426 warning_at (location
, OPT_Wvolatile
,
7427 "%qs expression of %<volatile%>-qualified type is "
7429 ((code
== PREINCREMENT_EXPR
7430 || code
== POSTINCREMENT_EXPR
)
7433 /* Forbid using -- or ++ in C++17 on `bool'. */
7434 if (TREE_CODE (declared_type
) == BOOLEAN_TYPE
)
7436 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
7438 if (complain
& tf_error
)
7440 "use of an operand of type %qT in %<operator--%> "
7441 "is forbidden", boolean_type_node
);
7442 return error_mark_node
;
7446 if (cxx_dialect
>= cxx17
)
7448 if (complain
& tf_error
)
7450 "use of an operand of type %qT in "
7451 "%<operator++%> is forbidden in C++17",
7453 return error_mark_node
;
7455 /* Otherwise, [depr.incr.bool] says this is deprecated. */
7457 warning_at (location
, OPT_Wdeprecated
,
7458 "use of an operand of type %qT "
7459 "in %<operator++%> is deprecated",
7462 val
= boolean_increment (code
, arg
);
7464 else if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
7465 /* An rvalue has no cv-qualifiers. */
7466 val
= build2 (code
, cv_unqualified (TREE_TYPE (arg
)), arg
, inc
);
7468 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
7470 TREE_SIDE_EFFECTS (val
) = 1;
7475 /* Note that this operation never does default_conversion
7476 regardless of NOCONVERT. */
7477 return cp_build_addr_expr (arg
, complain
);
7486 argtype
= TREE_TYPE (arg
);
7487 return build1 (code
, argtype
, arg
);
7490 if (complain
& tf_error
)
7491 error_at (location
, "%s", errstring
);
7492 return error_mark_node
;
7495 /* Hook for the c-common bits that build a unary op. */
7497 build_unary_op (location_t
/*location*/,
7498 enum tree_code code
, tree xarg
, bool noconvert
)
7500 return cp_build_unary_op (code
, xarg
, noconvert
, tf_warning_or_error
);
7503 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
7504 so that it is a valid lvalue even for GENERIC by replacing
7505 (lhs = rhs) with ((lhs = rhs), lhs)
7506 (--lhs) with ((--lhs), lhs)
7507 (++lhs) with ((++lhs), lhs)
7508 and if lhs has side-effects, calling cp_stabilize_reference on it, so
7509 that it can be evaluated multiple times. */
7512 genericize_compound_lvalue (tree lvalue
)
7514 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue
, 0)))
7515 lvalue
= build2 (TREE_CODE (lvalue
), TREE_TYPE (lvalue
),
7516 cp_stabilize_reference (TREE_OPERAND (lvalue
, 0)),
7517 TREE_OPERAND (lvalue
, 1));
7518 return build2 (COMPOUND_EXPR
, TREE_TYPE (TREE_OPERAND (lvalue
, 0)),
7519 lvalue
, TREE_OPERAND (lvalue
, 0));
7522 /* Apply unary lvalue-demanding operator CODE to the expression ARG
7523 for certain kinds of expressions which are not really lvalues
7524 but which we can accept as lvalues.
7526 If ARG is not a kind of expression we can handle, return
7530 unary_complex_lvalue (enum tree_code code
, tree arg
)
7532 /* Inside a template, making these kinds of adjustments is
7533 pointless; we are only concerned with the type of the
7535 if (processing_template_decl
)
7538 /* Handle (a, b) used as an "lvalue". */
7539 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
7541 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 1), false,
7542 tf_warning_or_error
);
7543 return build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
7544 TREE_OPERAND (arg
, 0), real_result
);
7547 /* Handle (a ? b : c) used as an "lvalue". */
7548 if (TREE_CODE (arg
) == COND_EXPR
7549 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
7550 return rationalize_conditional_expr (code
, arg
, tf_warning_or_error
);
7552 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
7553 if (TREE_CODE (arg
) == MODIFY_EXPR
7554 || TREE_CODE (arg
) == PREINCREMENT_EXPR
7555 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
7556 return unary_complex_lvalue (code
, genericize_compound_lvalue (arg
));
7558 if (code
!= ADDR_EXPR
)
7561 /* Handle (a = b) used as an "lvalue" for `&'. */
7562 if (TREE_CODE (arg
) == MODIFY_EXPR
7563 || TREE_CODE (arg
) == INIT_EXPR
)
7565 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 0), false,
7566 tf_warning_or_error
);
7567 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
7569 suppress_warning (arg
/* What warning? */);
7573 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg
))
7574 || TREE_CODE (arg
) == OFFSET_REF
)
7577 /* We permit compiler to make function calls returning
7578 objects of aggregate type look like lvalues. */
7582 if (TREE_CODE (targ
) == SAVE_EXPR
)
7583 targ
= TREE_OPERAND (targ
, 0);
7585 if (TREE_CODE (targ
) == CALL_EXPR
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (targ
)))
7587 if (TREE_CODE (arg
) == SAVE_EXPR
)
7590 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, tf_warning_or_error
);
7591 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
7594 if (TREE_CODE (arg
) == SAVE_EXPR
&& INDIRECT_REF_P (targ
))
7595 return build3 (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
7596 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
7599 /* Don't let anything else be handled specially. */
7603 /* Mark EXP saying that we need to be able to take the
7604 address of it; it should not be allocated in a register.
7605 Value is true if successful. ARRAY_REF_P is true if this
7606 is for ARRAY_REF construction - in that case we don't want
7607 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
7608 it is fine to use ARRAY_REFs for vector subscripts on vector
7611 C++: we do not allow `current_class_ptr' to be addressable. */
7614 cxx_mark_addressable (tree exp
, bool array_ref_p
)
7619 switch (TREE_CODE (x
))
7621 case VIEW_CONVERT_EXPR
:
7623 && TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
7624 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x
, 0))))
7626 x
= TREE_OPERAND (x
, 0);
7631 error ("attempt to take address of bit-field");
7637 x
= TREE_OPERAND (x
, 0);
7641 if (x
== current_class_ptr
)
7643 error ("cannot take the address of %<this%>, which is an rvalue expression");
7644 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later. */
7650 /* Caller should not be trying to mark initialized
7651 constant fields addressable. */
7652 gcc_assert (DECL_LANG_SPECIFIC (x
) == 0
7653 || DECL_IN_AGGR_P (x
) == 0
7655 || DECL_EXTERNAL (x
));
7659 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
7660 && !DECL_ARTIFICIAL (x
))
7662 if (VAR_P (x
) && DECL_HARD_REGISTER (x
))
7665 ("address of explicit register variable %qD requested", x
);
7668 else if (extra_warnings
)
7670 (OPT_Wextra
, "address requested for %qD, which is declared %<register%>", x
);
7672 TREE_ADDRESSABLE (x
) = 1;
7677 TREE_ADDRESSABLE (x
) = 1;
7681 TREE_ADDRESSABLE (x
) = 1;
7685 TREE_ADDRESSABLE (x
) = 1;
7686 cxx_mark_addressable (TREE_OPERAND (x
, 0));
7694 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
7697 build_x_conditional_expr (location_t loc
, tree ifexp
, tree op1
, tree op2
,
7698 tsubst_flags_t complain
)
7700 tree orig_ifexp
= ifexp
;
7701 tree orig_op1
= op1
;
7702 tree orig_op2
= op2
;
7705 if (processing_template_decl
)
7707 /* The standard says that the expression is type-dependent if
7708 IFEXP is type-dependent, even though the eventual type of the
7709 expression doesn't dependent on IFEXP. */
7710 if (type_dependent_expression_p (ifexp
)
7711 /* As a GNU extension, the middle operand may be omitted. */
7712 || (op1
&& type_dependent_expression_p (op1
))
7713 || type_dependent_expression_p (op2
))
7714 return build_min_nt_loc (loc
, COND_EXPR
, ifexp
, op1
, op2
);
7715 ifexp
= build_non_dependent_expr (ifexp
);
7717 op1
= build_non_dependent_expr (op1
);
7718 op2
= build_non_dependent_expr (op2
);
7721 expr
= build_conditional_expr (loc
, ifexp
, op1
, op2
, complain
);
7722 if (processing_template_decl
&& expr
!= error_mark_node
)
7724 tree min
= build_min_non_dep (COND_EXPR
, expr
,
7725 orig_ifexp
, orig_op1
, orig_op2
);
7726 expr
= convert_from_reference (min
);
7731 /* Given a list of expressions, return a compound expression
7732 that performs them all and returns the value of the last of them. */
7735 build_x_compound_expr_from_list (tree list
, expr_list_kind exp
,
7736 tsubst_flags_t complain
)
7738 tree expr
= TREE_VALUE (list
);
7740 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
7741 && !CONSTRUCTOR_IS_DIRECT_INIT (expr
))
7743 if (complain
& tf_error
)
7744 pedwarn (cp_expr_loc_or_input_loc (expr
), 0,
7745 "list-initializer for non-class type must not "
7746 "be parenthesized");
7748 return error_mark_node
;
7751 if (TREE_CHAIN (list
))
7753 if (complain
& tf_error
)
7757 permerror (input_location
, "expression list treated as compound "
7758 "expression in initializer");
7761 permerror (input_location
, "expression list treated as compound "
7762 "expression in mem-initializer");
7765 permerror (input_location
, "expression list treated as compound "
7766 "expression in functional cast");
7772 return error_mark_node
;
7774 for (list
= TREE_CHAIN (list
); list
; list
= TREE_CHAIN (list
))
7775 expr
= build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list
)),
7776 expr
, TREE_VALUE (list
), NULL_TREE
,
7783 /* Like build_x_compound_expr_from_list, but using a VEC. */
7786 build_x_compound_expr_from_vec (vec
<tree
, va_gc
> *vec
, const char *msg
,
7787 tsubst_flags_t complain
)
7789 if (vec_safe_is_empty (vec
))
7791 else if (vec
->length () == 1)
7801 if (complain
& tf_error
)
7802 permerror (input_location
,
7803 "%s expression list treated as compound expression",
7806 return error_mark_node
;
7810 for (ix
= 1; vec
->iterate (ix
, &t
); ++ix
)
7811 expr
= build_x_compound_expr (EXPR_LOCATION (t
), expr
,
7812 t
, NULL_TREE
, complain
);
7818 /* Handle overloading of the ',' operator when needed. */
7821 build_x_compound_expr (location_t loc
, tree op1
, tree op2
,
7822 tree lookups
, tsubst_flags_t complain
)
7825 tree orig_op1
= op1
;
7826 tree orig_op2
= op2
;
7827 tree overload
= NULL_TREE
;
7829 if (processing_template_decl
)
7831 if (type_dependent_expression_p (op1
)
7832 || type_dependent_expression_p (op2
))
7834 result
= build_min_nt_loc (loc
, COMPOUND_EXPR
, op1
, op2
);
7836 = build_dependent_operator_type (lookups
, COMPOUND_EXPR
, false);
7839 op1
= build_non_dependent_expr (op1
);
7840 op2
= build_non_dependent_expr (op2
);
7843 result
= build_new_op (loc
, COMPOUND_EXPR
, LOOKUP_NORMAL
, op1
, op2
,
7844 NULL_TREE
, lookups
, &overload
, complain
);
7846 result
= cp_build_compound_expr (op1
, op2
, complain
);
7848 if (processing_template_decl
&& result
!= error_mark_node
)
7850 if (overload
!= NULL_TREE
)
7851 return (build_min_non_dep_op_overload
7852 (COMPOUND_EXPR
, result
, overload
, orig_op1
, orig_op2
));
7854 return build_min_non_dep (COMPOUND_EXPR
, result
, orig_op1
, orig_op2
);
7860 /* Like cp_build_compound_expr, but for the c-common bits. */
7863 build_compound_expr (location_t
/*loc*/, tree lhs
, tree rhs
)
7865 return cp_build_compound_expr (lhs
, rhs
, tf_warning_or_error
);
7868 /* Build a compound expression. */
7871 cp_build_compound_expr (tree lhs
, tree rhs
, tsubst_flags_t complain
)
7873 lhs
= convert_to_void (lhs
, ICV_LEFT_OF_COMMA
, complain
);
7875 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
7876 return error_mark_node
;
7878 if (TREE_CODE (rhs
) == TARGET_EXPR
)
7880 /* If the rhs is a TARGET_EXPR, then build the compound
7881 expression inside the target_expr's initializer. This
7882 helps the compiler to eliminate unnecessary temporaries. */
7883 tree init
= TREE_OPERAND (rhs
, 1);
7885 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), lhs
, init
);
7886 TREE_OPERAND (rhs
, 1) = init
;
7891 if (type_unknown_p (rhs
))
7893 if (complain
& tf_error
)
7894 error_at (cp_expr_loc_or_input_loc (rhs
),
7895 "no context to resolve type of %qE", rhs
);
7896 return error_mark_node
;
7899 return build2 (COMPOUND_EXPR
, TREE_TYPE (rhs
), lhs
, rhs
);
7902 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
7903 casts away constness. CAST gives the type of cast. Returns true
7904 if the cast is ill-formed, false if it is well-formed.
7906 ??? This function warns for casting away any qualifier not just
7907 const. We would like to specify exactly what qualifiers are casted
7912 check_for_casting_away_constness (location_t loc
, tree src_type
,
7913 tree dest_type
, enum tree_code cast
,
7914 tsubst_flags_t complain
)
7916 /* C-style casts are allowed to cast away constness. With
7917 WARN_CAST_QUAL, we still want to issue a warning. */
7918 if (cast
== CAST_EXPR
&& !warn_cast_qual
)
7921 if (!casts_away_constness (src_type
, dest_type
, complain
))
7927 if (complain
& tf_warning
)
7928 warning_at (loc
, OPT_Wcast_qual
,
7929 "cast from type %qT to type %qT casts away qualifiers",
7930 src_type
, dest_type
);
7933 case STATIC_CAST_EXPR
:
7934 if (complain
& tf_error
)
7935 error_at (loc
, "%<static_cast%> from type %qT to type %qT casts "
7937 src_type
, dest_type
);
7940 case REINTERPRET_CAST_EXPR
:
7941 if (complain
& tf_error
)
7942 error_at (loc
, "%<reinterpret_cast%> from type %qT to type %qT "
7943 "casts away qualifiers",
7944 src_type
, dest_type
);
7952 /* Warns if the cast from expression EXPR to type TYPE is useless. */
7954 maybe_warn_about_useless_cast (location_t loc
, tree type
, tree expr
,
7955 tsubst_flags_t complain
)
7957 if (warn_useless_cast
7958 && complain
& tf_warning
)
7960 if ((TYPE_REF_P (type
)
7961 && (TYPE_REF_IS_RVALUE (type
)
7962 ? xvalue_p (expr
) : lvalue_p (expr
))
7963 && same_type_p (TREE_TYPE (expr
), TREE_TYPE (type
)))
7964 || same_type_p (TREE_TYPE (expr
), type
))
7965 warning_at (loc
, OPT_Wuseless_cast
,
7966 "useless cast to type %q#T", type
);
7970 /* Warns if the cast ignores cv-qualifiers on TYPE. */
7972 maybe_warn_about_cast_ignoring_quals (location_t loc
, tree type
,
7973 tsubst_flags_t complain
)
7975 if (warn_ignored_qualifiers
7976 && complain
& tf_warning
7977 && !CLASS_TYPE_P (type
)
7978 && (cp_type_quals (type
) & (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)))
7979 warning_at (loc
, OPT_Wignored_qualifiers
,
7980 "type qualifiers ignored on cast result type");
7983 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
7984 (another pointer-to-member type in the same hierarchy) and return
7985 the converted expression. If ALLOW_INVERSE_P is permitted, a
7986 pointer-to-derived may be converted to pointer-to-base; otherwise,
7987 only the other direction is permitted. If C_CAST_P is true, this
7988 conversion is taking place as part of a C-style cast. */
7991 convert_ptrmem (tree type
, tree expr
, bool allow_inverse_p
,
7992 bool c_cast_p
, tsubst_flags_t complain
)
7994 if (same_type_p (type
, TREE_TYPE (expr
)))
7997 if (TYPE_PTRDATAMEM_P (type
))
7999 tree obase
= TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr
));
8000 tree nbase
= TYPE_PTRMEM_CLASS_TYPE (type
);
8001 tree delta
= (get_delta_difference
8003 allow_inverse_p
, c_cast_p
, complain
));
8005 if (delta
== error_mark_node
)
8006 return error_mark_node
;
8008 if (!same_type_p (obase
, nbase
))
8010 if (TREE_CODE (expr
) == PTRMEM_CST
)
8011 expr
= cplus_expand_constant (expr
);
8013 tree cond
= cp_build_binary_op (input_location
, EQ_EXPR
, expr
,
8014 build_int_cst (TREE_TYPE (expr
), -1),
8016 tree op1
= build_nop (ptrdiff_type_node
, expr
);
8017 tree op2
= cp_build_binary_op (input_location
, PLUS_EXPR
, op1
, delta
,
8020 expr
= fold_build3_loc (input_location
,
8021 COND_EXPR
, ptrdiff_type_node
, cond
, op1
, op2
);
8024 return build_nop (type
, expr
);
8027 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
,
8028 allow_inverse_p
, c_cast_p
, complain
);
8031 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
8032 this static_cast is being attempted as one of the possible casts
8033 allowed by a C-style cast. (In that case, accessibility of base
8034 classes is not considered, and it is OK to cast away
8035 constness.) Return the result of the cast. *VALID_P is set to
8036 indicate whether or not the cast was valid. */
8039 build_static_cast_1 (location_t loc
, tree type
, tree expr
, bool c_cast_p
,
8040 bool *valid_p
, tsubst_flags_t complain
)
8046 /* Assume the cast is valid. */
8049 intype
= unlowered_expr_type (expr
);
8051 /* Save casted types in the function's used types hash table. */
8052 used_types_insert (type
);
8054 /* A prvalue of non-class type is cv-unqualified. */
8055 if (!CLASS_TYPE_P (type
))
8056 type
= cv_unqualified (type
);
8058 /* [expr.static.cast]
8060 An lvalue of type "cv1 B", where B is a class type, can be cast
8061 to type "reference to cv2 D", where D is a class derived (clause
8062 _class.derived_) from B, if a valid standard conversion from
8063 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
8064 same cv-qualification as, or greater cv-qualification than, cv1,
8065 and B is not a virtual base class of D. */
8066 /* We check this case before checking the validity of "TYPE t =
8067 EXPR;" below because for this case:
8070 struct D : public B { D(const B&); };
8072 void f() { static_cast<const D&>(b); }
8074 we want to avoid constructing a new D. The standard is not
8075 completely clear about this issue, but our interpretation is
8076 consistent with other compilers. */
8077 if (TYPE_REF_P (type
)
8078 && CLASS_TYPE_P (TREE_TYPE (type
))
8079 && CLASS_TYPE_P (intype
)
8080 && (TYPE_REF_IS_RVALUE (type
) || lvalue_p (expr
))
8081 && DERIVED_FROM_P (intype
, TREE_TYPE (type
))
8082 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype
)),
8083 build_pointer_type (TYPE_MAIN_VARIANT
8084 (TREE_TYPE (type
))),
8087 || at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
8091 if (processing_template_decl
)
8094 /* There is a standard conversion from "D*" to "B*" even if "B"
8095 is ambiguous or inaccessible. If this is really a
8096 static_cast, then we check both for inaccessibility and
8097 ambiguity. However, if this is a static_cast being performed
8098 because the user wrote a C-style cast, then accessibility is
8100 base
= lookup_base (TREE_TYPE (type
), intype
,
8101 c_cast_p
? ba_unique
: ba_check
,
8103 expr
= cp_build_addr_expr (expr
, complain
);
8105 if (sanitize_flags_p (SANITIZE_VPTR
))
8108 = cp_ubsan_maybe_instrument_downcast (loc
, type
,
8114 /* Convert from "B*" to "D*". This function will check that "B"
8115 is not a virtual base of "D". Even if we don't have a guarantee
8116 that expr is NULL, if the static_cast is to a reference type,
8117 it is UB if it would be NULL, so omit the non-NULL check. */
8118 expr
= build_base_path (MINUS_EXPR
, expr
, base
,
8119 /*nonnull=*/flag_delete_null_pointer_checks
,
8122 /* Convert the pointer to a reference -- but then remember that
8123 there are no expressions with reference type in C++.
8125 We call rvalue so that there's an actual tree code
8126 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
8127 is a variable with the same type, the conversion would get folded
8128 away, leaving just the variable and causing lvalue_kind to give
8129 the wrong answer. */
8130 expr
= cp_fold_convert (type
, expr
);
8132 /* When -fsanitize=null, make sure to diagnose reference binding to
8133 NULL even when the reference is converted to pointer later on. */
8134 if (sanitize_flags_p (SANITIZE_NULL
)
8135 && TREE_CODE (expr
) == COND_EXPR
8136 && TREE_OPERAND (expr
, 2)
8137 && TREE_CODE (TREE_OPERAND (expr
, 2)) == INTEGER_CST
8138 && TREE_TYPE (TREE_OPERAND (expr
, 2)) == type
)
8139 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr
, 2));
8141 return convert_from_reference (rvalue (expr
));
8144 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
8145 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
8146 if (TYPE_REF_P (type
)
8147 && TYPE_REF_IS_RVALUE (type
)
8148 && (clk
= real_lvalue_p (expr
))
8149 && reference_compatible_p (TREE_TYPE (type
), intype
)
8150 && (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
8152 if (processing_template_decl
)
8154 if (clk
== clk_ordinary
)
8156 /* Handle the (non-bit-field) lvalue case here by casting to
8157 lvalue reference and then changing it to an rvalue reference.
8158 Casting an xvalue to rvalue reference will be handled by the
8160 tree lref
= cp_build_reference_type (TREE_TYPE (type
), false);
8161 result
= (perform_direct_initialization_if_possible
8162 (lref
, expr
, c_cast_p
, complain
));
8163 result
= build1 (NON_LVALUE_EXPR
, type
, result
);
8164 return convert_from_reference (result
);
8167 /* For a bit-field or packed field, bind to a temporary. */
8168 expr
= rvalue (expr
);
8171 /* Resolve overloaded address here rather than once in
8172 implicit_conversion and again in the inverse code below. */
8173 if (TYPE_PTRMEMFUNC_P (type
) && type_unknown_p (expr
))
8175 expr
= instantiate_type (type
, expr
, complain
);
8176 intype
= TREE_TYPE (expr
);
8179 /* [expr.static.cast]
8181 Any expression can be explicitly converted to type cv void. */
8182 if (VOID_TYPE_P (type
))
8183 return convert_to_void (expr
, ICV_CAST
, complain
);
8186 An abstract class shall not be used ... as the type of an explicit
8188 if (abstract_virtuals_error (ACU_CAST
, type
, complain
))
8189 return error_mark_node
;
8191 /* [expr.static.cast]
8193 An expression e can be explicitly converted to a type T using a
8194 static_cast of the form static_cast<T>(e) if the declaration T
8195 t(e);" is well-formed, for some invented temporary variable
8197 result
= perform_direct_initialization_if_possible (type
, expr
,
8198 c_cast_p
, complain
);
8199 /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
8200 which initialize the first element of the aggregate. We need to handle
8201 the array case specifically. */
8202 if (result
== NULL_TREE
8203 && cxx_dialect
>= cxx20
8204 && TREE_CODE (type
) == ARRAY_TYPE
)
8206 /* Create { EXPR } and perform direct-initialization from it. */
8207 tree e
= build_constructor_single (init_list_type_node
, NULL_TREE
, expr
);
8208 CONSTRUCTOR_IS_DIRECT_INIT (e
) = true;
8209 CONSTRUCTOR_IS_PAREN_INIT (e
) = true;
8210 result
= perform_direct_initialization_if_possible (type
, e
, c_cast_p
,
8215 if (processing_template_decl
)
8218 result
= convert_from_reference (result
);
8220 /* [expr.static.cast]
8222 If T is a reference type, the result is an lvalue; otherwise,
8223 the result is an rvalue. */
8224 if (!TYPE_REF_P (type
))
8226 result
= rvalue (result
);
8228 if (result
== expr
&& SCALAR_TYPE_P (type
))
8229 /* Leave some record of the cast. */
8230 result
= build_nop (type
, expr
);
8235 /* [expr.static.cast]
8237 The inverse of any standard conversion sequence (clause _conv_),
8238 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
8239 (_conv.array_), function-to-pointer (_conv.func_), and boolean
8240 (_conv.bool_) conversions, can be performed explicitly using
8241 static_cast subject to the restriction that the explicit
8242 conversion does not cast away constness (_expr.const.cast_), and
8243 the following additional rules for specific cases: */
8244 /* For reference, the conversions not excluded are: integral
8245 promotions, floating-point promotion, integral conversions,
8246 floating-point conversions, floating-integral conversions,
8247 pointer conversions, and pointer to member conversions. */
8250 A value of integral _or enumeration_ type can be explicitly
8251 converted to an enumeration type. */
8252 /* The effect of all that is that any conversion between any two
8253 types which are integral, floating, or enumeration types can be
8255 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
8256 || SCALAR_FLOAT_TYPE_P (type
))
8257 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
8258 || SCALAR_FLOAT_TYPE_P (intype
)))
8260 if (processing_template_decl
)
8262 return ocp_convert (type
, expr
, CONV_C_CAST
, LOOKUP_NORMAL
, complain
);
8265 if (TYPE_PTR_P (type
) && TYPE_PTR_P (intype
)
8266 && CLASS_TYPE_P (TREE_TYPE (type
))
8267 && CLASS_TYPE_P (TREE_TYPE (intype
))
8268 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
8269 (TREE_TYPE (intype
))),
8270 build_pointer_type (TYPE_MAIN_VARIANT
8271 (TREE_TYPE (type
))),
8276 if (processing_template_decl
)
8280 && check_for_casting_away_constness (loc
, intype
, type
,
8283 return error_mark_node
;
8284 base
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
8285 c_cast_p
? ba_unique
: ba_check
,
8287 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
8290 if (sanitize_flags_p (SANITIZE_VPTR
))
8293 = cp_ubsan_maybe_instrument_downcast (loc
, type
,
8299 return cp_fold_convert (type
, expr
);
8302 if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
8303 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
8310 c1
= TYPE_PTRMEM_CLASS_TYPE (intype
);
8311 c2
= TYPE_PTRMEM_CLASS_TYPE (type
);
8313 if (TYPE_PTRDATAMEM_P (type
))
8315 t1
= (build_ptrmem_type
8317 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype
))));
8318 t2
= (build_ptrmem_type
8320 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
8327 if (can_convert (t1
, t2
, complain
) || can_convert (t2
, t1
, complain
))
8330 && check_for_casting_away_constness (loc
, intype
, type
,
8333 return error_mark_node
;
8334 if (processing_template_decl
)
8336 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/1,
8337 c_cast_p
, complain
);
8341 /* [expr.static.cast]
8343 An rvalue of type "pointer to cv void" can be explicitly
8344 converted to a pointer to object type. A value of type pointer
8345 to object converted to "pointer to cv void" and back to the
8346 original pointer type will have its original value. */
8347 if (TYPE_PTR_P (intype
)
8348 && VOID_TYPE_P (TREE_TYPE (intype
))
8349 && TYPE_PTROB_P (type
))
8352 && check_for_casting_away_constness (loc
, intype
, type
,
8355 return error_mark_node
;
8356 if (processing_template_decl
)
8358 return build_nop (type
, expr
);
8362 return error_mark_node
;
8365 /* Return an expression representing static_cast<TYPE>(EXPR). */
8368 build_static_cast (location_t loc
, tree type
, tree oexpr
,
8369 tsubst_flags_t complain
)
8375 if (type
== error_mark_node
|| expr
== error_mark_node
)
8376 return error_mark_node
;
8378 bool dependent
= (dependent_type_p (type
)
8379 || type_dependent_expression_p (expr
));
8383 expr
= build_min (STATIC_CAST_EXPR
, type
, oexpr
);
8384 /* We don't know if it will or will not have side effects. */
8385 TREE_SIDE_EFFECTS (expr
) = 1;
8386 result
= convert_from_reference (expr
);
8387 protected_set_expr_location (result
, loc
);
8390 else if (processing_template_decl
)
8391 expr
= build_non_dependent_expr (expr
);
8393 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8394 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8395 if (!TYPE_REF_P (type
)
8396 && TREE_CODE (expr
) == NOP_EXPR
8397 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
8398 expr
= TREE_OPERAND (expr
, 0);
8400 result
= build_static_cast_1 (loc
, type
, expr
, /*c_cast_p=*/false,
8401 &valid_p
, complain
);
8404 if (result
!= error_mark_node
)
8406 maybe_warn_about_useless_cast (loc
, type
, expr
, complain
);
8407 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
8409 if (processing_template_decl
)
8411 protected_set_expr_location (result
, loc
);
8415 if (complain
& tf_error
)
8417 error_at (loc
, "invalid %<static_cast%> from type %qT to type %qT",
8418 TREE_TYPE (expr
), type
);
8419 if ((TYPE_PTR_P (type
) || TYPE_REF_P (type
))
8420 && CLASS_TYPE_P (TREE_TYPE (type
))
8421 && !COMPLETE_TYPE_P (TREE_TYPE (type
)))
8422 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type
))),
8423 "class type %qT is incomplete", TREE_TYPE (type
));
8424 tree expr_type
= TREE_TYPE (expr
);
8425 if (TYPE_PTR_P (expr_type
))
8426 expr_type
= TREE_TYPE (expr_type
);
8427 if (CLASS_TYPE_P (expr_type
) && !COMPLETE_TYPE_P (expr_type
))
8428 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type
)),
8429 "class type %qT is incomplete", expr_type
);
8431 return error_mark_node
;
8434 /* EXPR is an expression with member function or pointer-to-member
8435 function type. TYPE is a pointer type. Converting EXPR to TYPE is
8436 not permitted by ISO C++, but we accept it in some modes. If we
8437 are not in one of those modes, issue a diagnostic. Return the
8438 converted expression. */
8441 convert_member_func_to_ptr (tree type
, tree expr
, tsubst_flags_t complain
)
8446 intype
= TREE_TYPE (expr
);
8447 gcc_assert (TYPE_PTRMEMFUNC_P (intype
)
8448 || TREE_CODE (intype
) == METHOD_TYPE
);
8450 if (!(complain
& tf_warning_or_error
))
8451 return error_mark_node
;
8453 location_t loc
= cp_expr_loc_or_input_loc (expr
);
8455 if (pedantic
|| warn_pmf2ptr
)
8456 pedwarn (loc
, pedantic
? OPT_Wpedantic
: OPT_Wpmf_conversions
,
8457 "converting from %qH to %qI", intype
, type
);
8459 STRIP_ANY_LOCATION_WRAPPER (expr
);
8461 if (TREE_CODE (intype
) == METHOD_TYPE
)
8462 expr
= build_addr_func (expr
, complain
);
8463 else if (TREE_CODE (expr
) == PTRMEM_CST
)
8464 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
8467 decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
), 0);
8468 decl
= build_address (decl
);
8469 expr
= get_member_function_from_ptrfunc (&decl
, expr
, complain
);
8472 if (expr
== error_mark_node
)
8473 return error_mark_node
;
8475 expr
= build_nop (type
, expr
);
8476 SET_EXPR_LOCATION (expr
, loc
);
8480 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
8481 constexpr evaluation knows to reject it. */
8484 build_nop_reinterpret (tree type
, tree expr
)
8486 tree ret
= build_nop (type
, expr
);
8488 REINTERPRET_CAST_P (ret
) = true;
8492 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
8493 If C_CAST_P is true, this reinterpret cast is being done as part of
8494 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
8495 indicate whether or not reinterpret_cast was valid. */
8498 build_reinterpret_cast_1 (location_t loc
, tree type
, tree expr
,
8499 bool c_cast_p
, bool *valid_p
,
8500 tsubst_flags_t complain
)
8504 /* Assume the cast is invalid. */
8508 if (type
== error_mark_node
|| error_operand_p (expr
))
8509 return error_mark_node
;
8511 intype
= TREE_TYPE (expr
);
8513 /* Save casted types in the function's used types hash table. */
8514 used_types_insert (type
);
8516 /* A prvalue of non-class type is cv-unqualified. */
8517 if (!CLASS_TYPE_P (type
))
8518 type
= cv_unqualified (type
);
8520 /* [expr.reinterpret.cast]
8521 A glvalue of type T1, designating an object x, can be cast to the type
8522 "reference to T2" if an expression of type "pointer to T1" can be
8523 explicitly converted to the type "pointer to T2" using a reinterpret_cast.
8524 The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
8525 of type "pointer to T1". No temporary is created, no copy is made, and no
8526 constructors (11.4.4) or conversion functions (11.4.7) are called. */
8527 if (TYPE_REF_P (type
))
8529 if (!glvalue_p (expr
))
8531 if (complain
& tf_error
)
8532 error_at (loc
, "invalid cast of a prvalue expression of type "
8535 return error_mark_node
;
8538 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
8539 "B" are related class types; the reinterpret_cast does not
8540 adjust the pointer. */
8541 if (TYPE_PTR_P (intype
)
8542 && (complain
& tf_warning
)
8543 && (comptypes (TREE_TYPE (intype
), TREE_TYPE (type
),
8544 COMPARE_BASE
| COMPARE_DERIVED
)))
8545 warning_at (loc
, 0, "casting %qT to %qT does not dereference pointer",
8548 expr
= cp_build_addr_expr (expr
, complain
);
8550 if (warn_strict_aliasing
> 2)
8551 cp_strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
8553 if (expr
!= error_mark_node
)
8554 expr
= build_reinterpret_cast_1
8555 (loc
, build_pointer_type (TREE_TYPE (type
)), expr
, c_cast_p
,
8557 if (expr
!= error_mark_node
)
8558 /* cp_build_indirect_ref isn't right for rvalue refs. */
8559 expr
= convert_from_reference (fold_convert (type
, expr
));
8563 /* As a G++ extension, we consider conversions from member
8564 functions, and pointers to member functions to
8565 pointer-to-function and pointer-to-void types. If
8566 -Wno-pmf-conversions has not been specified,
8567 convert_member_func_to_ptr will issue an error message. */
8568 if ((TYPE_PTRMEMFUNC_P (intype
)
8569 || TREE_CODE (intype
) == METHOD_TYPE
)
8570 && TYPE_PTR_P (type
)
8571 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
8572 || VOID_TYPE_P (TREE_TYPE (type
))))
8573 return convert_member_func_to_ptr (type
, expr
, complain
);
8575 /* If the cast is not to a reference type, the lvalue-to-rvalue,
8576 array-to-pointer, and function-to-pointer conversions are
8578 expr
= decay_conversion (expr
, complain
);
8580 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8581 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8582 if (TREE_CODE (expr
) == NOP_EXPR
8583 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
8584 expr
= TREE_OPERAND (expr
, 0);
8586 if (error_operand_p (expr
))
8587 return error_mark_node
;
8589 intype
= TREE_TYPE (expr
);
8591 /* [expr.reinterpret.cast]
8592 A pointer can be converted to any integral type large enough to
8593 hold it. ... A value of type std::nullptr_t can be converted to
8594 an integral type; the conversion has the same meaning and
8595 validity as a conversion of (void*)0 to the integral type. */
8596 if (CP_INTEGRAL_TYPE_P (type
)
8597 && (TYPE_PTR_P (intype
) || NULLPTR_TYPE_P (intype
)))
8599 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
8601 if (complain
& tf_error
)
8602 permerror (loc
, "cast from %qH to %qI loses precision",
8605 return error_mark_node
;
8607 if (NULLPTR_TYPE_P (intype
))
8608 return build_int_cst (type
, 0);
8610 /* [expr.reinterpret.cast]
8611 A value of integral or enumeration type can be explicitly
8612 converted to a pointer. */
8613 else if (TYPE_PTR_P (type
) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype
))
8616 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
8617 || TYPE_PTR_OR_PTRMEM_P (type
))
8618 && same_type_p (type
, intype
))
8620 return rvalue (expr
);
8621 else if (TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
8623 if ((complain
& tf_warning
)
8624 && !cxx_safe_function_type_cast_p (TREE_TYPE (type
),
8625 TREE_TYPE (intype
)))
8626 warning_at (loc
, OPT_Wcast_function_type
,
8627 "cast between incompatible function types"
8628 " from %qH to %qI", intype
, type
);
8629 return build_nop_reinterpret (type
, expr
);
8631 else if (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
))
8633 if ((complain
& tf_warning
)
8634 && !cxx_safe_function_type_cast_p
8635 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type
)),
8636 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype
))))
8637 warning_at (loc
, OPT_Wcast_function_type
,
8638 "cast between incompatible pointer to member types"
8639 " from %qH to %qI", intype
, type
);
8640 return build_nop_reinterpret (type
, expr
);
8642 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
8643 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
8646 && check_for_casting_away_constness (loc
, intype
, type
,
8647 REINTERPRET_CAST_EXPR
,
8649 return error_mark_node
;
8650 /* Warn about possible alignment problems. */
8651 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
8652 && (complain
& tf_warning
)
8653 && !VOID_TYPE_P (type
)
8654 && TREE_CODE (TREE_TYPE (intype
)) != FUNCTION_TYPE
8655 && COMPLETE_TYPE_P (TREE_TYPE (type
))
8656 && COMPLETE_TYPE_P (TREE_TYPE (intype
))
8657 && min_align_of_type (TREE_TYPE (type
))
8658 > min_align_of_type (TREE_TYPE (intype
)))
8659 warning_at (loc
, OPT_Wcast_align
, "cast from %qH to %qI "
8660 "increases required alignment of target type",
8663 if (warn_strict_aliasing
<= 2)
8664 /* strict_aliasing_warning STRIP_NOPs its expr. */
8665 cp_strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
8667 return build_nop_reinterpret (type
, expr
);
8669 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
8670 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
8672 if (complain
& tf_warning
)
8673 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
8674 object pointer type or vice versa is conditionally-supported." */
8675 warning_at (loc
, OPT_Wconditionally_supported
,
8676 "casting between pointer-to-function and "
8677 "pointer-to-object is conditionally-supported");
8678 return build_nop_reinterpret (type
, expr
);
8680 else if (gnu_vector_type_p (type
) && scalarish_type_p (intype
))
8681 return convert_to_vector (type
, rvalue (expr
));
8682 else if (gnu_vector_type_p (intype
)
8683 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
8684 return convert_to_integer_nofold (type
, expr
);
8689 if (complain
& tf_error
)
8690 error_at (loc
, "invalid cast from type %qT to type %qT",
8692 return error_mark_node
;
8695 expr
= cp_convert (type
, expr
, complain
);
8696 if (TREE_CODE (expr
) == NOP_EXPR
)
8697 /* Mark any nop_expr that created as a reintepret_cast. */
8698 REINTERPRET_CAST_P (expr
) = true;
8703 build_reinterpret_cast (location_t loc
, tree type
, tree expr
,
8704 tsubst_flags_t complain
)
8708 if (type
== error_mark_node
|| expr
== error_mark_node
)
8709 return error_mark_node
;
8711 if (processing_template_decl
)
8713 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
8715 if (!TREE_SIDE_EFFECTS (t
)
8716 && type_dependent_expression_p (expr
))
8717 /* There might turn out to be side effects inside expr. */
8718 TREE_SIDE_EFFECTS (t
) = 1;
8719 r
= convert_from_reference (t
);
8720 protected_set_expr_location (r
, loc
);
8724 r
= build_reinterpret_cast_1 (loc
, type
, expr
, /*c_cast_p=*/false,
8725 /*valid_p=*/NULL
, complain
);
8726 if (r
!= error_mark_node
)
8728 maybe_warn_about_useless_cast (loc
, type
, expr
, complain
);
8729 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
8731 protected_set_expr_location (r
, loc
);
8735 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
8736 return an appropriate expression. Otherwise, return
8737 error_mark_node. If the cast is not valid, and COMPLAIN is true,
8738 then a diagnostic will be issued. If VALID_P is non-NULL, we are
8739 performing a C-style cast, its value upon return will indicate
8740 whether or not the conversion succeeded. */
8743 build_const_cast_1 (location_t loc
, tree dst_type
, tree expr
,
8744 tsubst_flags_t complain
, bool *valid_p
)
8747 tree reference_type
;
8749 /* Callers are responsible for handling error_mark_node as a
8750 destination type. */
8751 gcc_assert (dst_type
!= error_mark_node
);
8752 /* In a template, callers should be building syntactic
8753 representations of casts, not using this machinery. */
8754 gcc_assert (!processing_template_decl
);
8756 /* Assume the conversion is invalid. */
8760 if (!INDIRECT_TYPE_P (dst_type
) && !TYPE_PTRDATAMEM_P (dst_type
))
8762 if (complain
& tf_error
)
8763 error_at (loc
, "invalid use of %<const_cast%> with type %qT, "
8764 "which is not a pointer, reference, "
8765 "nor a pointer-to-data-member type", dst_type
);
8766 return error_mark_node
;
8769 if (TREE_CODE (TREE_TYPE (dst_type
)) == FUNCTION_TYPE
)
8771 if (complain
& tf_error
)
8772 error_at (loc
, "invalid use of %<const_cast%> with type %qT, "
8773 "which is a pointer or reference to a function type",
8775 return error_mark_node
;
8778 /* A prvalue of non-class type is cv-unqualified. */
8779 dst_type
= cv_unqualified (dst_type
);
8781 /* Save casted types in the function's used types hash table. */
8782 used_types_insert (dst_type
);
8784 src_type
= TREE_TYPE (expr
);
8785 /* Expressions do not really have reference types. */
8786 if (TYPE_REF_P (src_type
))
8787 src_type
= TREE_TYPE (src_type
);
8789 /* [expr.const.cast]
8791 For two object types T1 and T2, if a pointer to T1 can be explicitly
8792 converted to the type "pointer to T2" using a const_cast, then the
8793 following conversions can also be made:
8795 -- an lvalue of type T1 can be explicitly converted to an lvalue of
8796 type T2 using the cast const_cast<T2&>;
8798 -- a glvalue of type T1 can be explicitly converted to an xvalue of
8799 type T2 using the cast const_cast<T2&&>; and
8801 -- if T1 is a class type, a prvalue of type T1 can be explicitly
8802 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
8804 if (TYPE_REF_P (dst_type
))
8806 reference_type
= dst_type
;
8807 if (!TYPE_REF_IS_RVALUE (dst_type
)
8813 if (complain
& tf_error
)
8814 error_at (loc
, "invalid %<const_cast%> of an rvalue of type %qT "
8816 src_type
, dst_type
);
8817 return error_mark_node
;
8819 dst_type
= build_pointer_type (TREE_TYPE (dst_type
));
8820 src_type
= build_pointer_type (src_type
);
8824 reference_type
= NULL_TREE
;
8825 /* If the destination type is not a reference type, the
8826 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
8827 conversions are performed. */
8828 src_type
= type_decays_to (src_type
);
8829 if (src_type
== error_mark_node
)
8830 return error_mark_node
;
8833 if (TYPE_PTR_P (src_type
) || TYPE_PTRDATAMEM_P (src_type
))
8835 if (comp_ptr_ttypes_const (dst_type
, src_type
, bounds_none
))
8840 /* This cast is actually a C-style cast. Issue a warning if
8841 the user is making a potentially unsafe cast. */
8842 check_for_casting_away_constness (loc
, src_type
, dst_type
,
8843 CAST_EXPR
, complain
);
8844 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
8845 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
8846 && (complain
& tf_warning
)
8847 && min_align_of_type (TREE_TYPE (dst_type
))
8848 > min_align_of_type (TREE_TYPE (src_type
)))
8849 warning_at (loc
, OPT_Wcast_align
, "cast from %qH to %qI "
8850 "increases required alignment of target type",
8851 src_type
, dst_type
);
8855 expr
= cp_build_addr_expr (expr
, complain
);
8856 if (expr
== error_mark_node
)
8857 return error_mark_node
;
8858 expr
= build_nop (reference_type
, expr
);
8859 return convert_from_reference (expr
);
8863 expr
= decay_conversion (expr
, complain
);
8864 if (expr
== error_mark_node
)
8865 return error_mark_node
;
8867 /* build_c_cast puts on a NOP_EXPR to make the result not an
8868 lvalue. Strip such NOP_EXPRs if VALUE is being used in
8869 non-lvalue context. */
8870 if (TREE_CODE (expr
) == NOP_EXPR
8871 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
8872 expr
= TREE_OPERAND (expr
, 0);
8873 return build_nop (dst_type
, expr
);
8877 && !at_least_as_qualified_p (TREE_TYPE (dst_type
),
8878 TREE_TYPE (src_type
)))
8879 check_for_casting_away_constness (loc
, src_type
, dst_type
,
8880 CAST_EXPR
, complain
);
8883 if (complain
& tf_error
)
8884 error_at (loc
, "invalid %<const_cast%> from type %qT to type %qT",
8885 src_type
, dst_type
);
8886 return error_mark_node
;
8890 build_const_cast (location_t loc
, tree type
, tree expr
,
8891 tsubst_flags_t complain
)
8895 if (type
== error_mark_node
|| error_operand_p (expr
))
8896 return error_mark_node
;
8898 if (processing_template_decl
)
8900 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
8902 if (!TREE_SIDE_EFFECTS (t
)
8903 && type_dependent_expression_p (expr
))
8904 /* There might turn out to be side effects inside expr. */
8905 TREE_SIDE_EFFECTS (t
) = 1;
8906 r
= convert_from_reference (t
);
8907 protected_set_expr_location (r
, loc
);
8911 r
= build_const_cast_1 (loc
, type
, expr
, complain
, /*valid_p=*/NULL
);
8912 if (r
!= error_mark_node
)
8914 maybe_warn_about_useless_cast (loc
, type
, expr
, complain
);
8915 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
8917 protected_set_expr_location (r
, loc
);
8921 /* Like cp_build_c_cast, but for the c-common bits. */
8924 build_c_cast (location_t loc
, tree type
, tree expr
)
8926 return cp_build_c_cast (loc
, type
, expr
, tf_warning_or_error
);
8929 /* Like the "build_c_cast" used for c-common, but using cp_expr to
8930 preserve location information even for tree nodes that don't
8934 build_c_cast (location_t loc
, tree type
, cp_expr expr
)
8936 cp_expr result
= cp_build_c_cast (loc
, type
, expr
, tf_warning_or_error
);
8937 result
.set_location (loc
);
8941 /* Build an expression representing an explicit C-style cast to type
8942 TYPE of expression EXPR. */
8945 cp_build_c_cast (location_t loc
, tree type
, tree expr
,
8946 tsubst_flags_t complain
)
8952 if (type
== error_mark_node
|| error_operand_p (expr
))
8953 return error_mark_node
;
8955 if (processing_template_decl
)
8957 tree t
= build_min (CAST_EXPR
, type
,
8958 tree_cons (NULL_TREE
, value
, NULL_TREE
));
8959 /* We don't know if it will or will not have side effects. */
8960 TREE_SIDE_EFFECTS (t
) = 1;
8961 return convert_from_reference (t
);
8964 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
8965 'Class') should always be retained, because this information aids
8966 in method lookup. */
8967 if (objc_is_object_ptr (type
)
8968 && objc_is_object_ptr (TREE_TYPE (expr
)))
8969 return build_nop (type
, expr
);
8971 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8972 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8973 if (!TYPE_REF_P (type
)
8974 && TREE_CODE (value
) == NOP_EXPR
8975 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
8976 value
= TREE_OPERAND (value
, 0);
8978 if (TREE_CODE (type
) == ARRAY_TYPE
)
8980 /* Allow casting from T1* to T2[] because Cfront allows it.
8981 NIHCL uses it. It is not valid ISO C++ however. */
8982 if (TYPE_PTR_P (TREE_TYPE (expr
)))
8984 if (complain
& tf_error
)
8985 permerror (loc
, "ISO C++ forbids casting to an array type %qT",
8988 return error_mark_node
;
8989 type
= build_pointer_type (TREE_TYPE (type
));
8993 if (complain
& tf_error
)
8994 error_at (loc
, "ISO C++ forbids casting to an array type %qT",
8996 return error_mark_node
;
9000 if (FUNC_OR_METHOD_TYPE_P (type
))
9002 if (complain
& tf_error
)
9003 error_at (loc
, "invalid cast to function type %qT", type
);
9004 return error_mark_node
;
9007 if (TYPE_PTR_P (type
)
9008 && TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
9009 /* Casting to an integer of smaller size is an error detected elsewhere. */
9010 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (value
))
9011 /* Don't warn about converting any constant. */
9012 && !TREE_CONSTANT (value
))
9013 warning_at (loc
, OPT_Wint_to_pointer_cast
,
9014 "cast to pointer from integer of different size");
9016 /* A C-style cast can be a const_cast. */
9017 result
= build_const_cast_1 (loc
, type
, value
, complain
& tf_warning
,
9021 if (result
!= error_mark_node
)
9023 maybe_warn_about_useless_cast (loc
, type
, value
, complain
);
9024 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
9029 /* Or a static cast. */
9030 result
= build_static_cast_1 (loc
, type
, value
, /*c_cast_p=*/true,
9031 &valid_p
, complain
);
9032 /* Or a reinterpret_cast. */
9034 result
= build_reinterpret_cast_1 (loc
, type
, value
, /*c_cast_p=*/true,
9035 &valid_p
, complain
);
9036 /* The static_cast or reinterpret_cast may be followed by a
9039 /* A valid cast may result in errors if, for example, a
9040 conversion to an ambiguous base class is required. */
9041 && !error_operand_p (result
))
9045 maybe_warn_about_useless_cast (loc
, type
, value
, complain
);
9046 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
9048 /* Non-class rvalues always have cv-unqualified type. */
9049 if (!CLASS_TYPE_P (type
))
9050 type
= TYPE_MAIN_VARIANT (type
);
9051 result_type
= TREE_TYPE (result
);
9052 if (!CLASS_TYPE_P (result_type
) && !TYPE_REF_P (type
))
9053 result_type
= TYPE_MAIN_VARIANT (result_type
);
9054 /* If the type of RESULT does not match TYPE, perform a
9055 const_cast to make it match. If the static_cast or
9056 reinterpret_cast succeeded, we will differ by at most
9057 cv-qualification, so the follow-on const_cast is guaranteed
9059 if (!same_type_p (non_reference (type
), non_reference (result_type
)))
9061 result
= build_const_cast_1 (loc
, type
, result
, false, &valid_p
);
9062 gcc_assert (valid_p
);
9067 return error_mark_node
;
9070 /* Warn when a value is moved to itself with std::move. LHS is the target,
9071 RHS may be the std::move call, and LOC is the location of the whole
9075 maybe_warn_self_move (location_t loc
, tree lhs
, tree rhs
)
9077 if (!warn_self_move
)
9080 /* C++98 doesn't know move. */
9081 if (cxx_dialect
< cxx11
)
9084 if (processing_template_decl
)
9087 if (!REFERENCE_REF_P (rhs
)
9088 || TREE_CODE (TREE_OPERAND (rhs
, 0)) != CALL_EXPR
)
9090 tree fn
= TREE_OPERAND (rhs
, 0);
9091 if (!is_std_move_p (fn
))
9094 /* Just a little helper to strip * and various NOPs. */
9095 auto extract_op
= [] (tree
&op
) {
9097 while (INDIRECT_REF_P (op
))
9098 op
= TREE_OPERAND (op
, 0);
9099 op
= maybe_undo_parenthesized_ref (op
);
9100 STRIP_ANY_LOCATION_WRAPPER (op
);
9103 tree arg
= CALL_EXPR_ARG (fn
, 0);
9105 if (TREE_CODE (arg
) == ADDR_EXPR
)
9106 arg
= TREE_OPERAND (arg
, 0);
9107 tree type
= TREE_TYPE (lhs
);
9108 tree orig_lhs
= lhs
;
9110 if (cp_tree_equal (lhs
, arg
))
9112 auto_diagnostic_group d
;
9113 if (warning_at (loc
, OPT_Wself_move
,
9114 "moving %qE of type %qT to itself", orig_lhs
, type
))
9115 inform (loc
, "remove %<std::move%> call");
9119 /* For use from the C common bits. */
9121 build_modify_expr (location_t location
,
9122 tree lhs
, tree
/*lhs_origtype*/,
9123 enum tree_code modifycode
,
9124 location_t
/*rhs_location*/, tree rhs
,
9125 tree
/*rhs_origtype*/)
9127 return cp_build_modify_expr (location
, lhs
, modifycode
, rhs
,
9128 tf_warning_or_error
);
9131 /* Build an assignment expression of lvalue LHS from value RHS.
9132 MODIFYCODE is the code for a binary operator that we use
9133 to combine the old value of LHS with RHS to get the new value.
9134 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
9136 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
9139 cp_build_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
9140 tree rhs
, tsubst_flags_t complain
)
9142 lhs
= mark_lvalue_use_nonread (lhs
);
9144 tree result
= NULL_TREE
;
9146 tree lhstype
= TREE_TYPE (lhs
);
9148 tree olhstype
= lhstype
;
9149 bool plain_assign
= (modifycode
== NOP_EXPR
);
9150 bool compound_side_effects_p
= false;
9151 tree preeval
= NULL_TREE
;
9153 /* Avoid duplicate error messages from operands that had errors. */
9154 if (error_operand_p (lhs
) || error_operand_p (rhs
))
9155 return error_mark_node
;
9157 while (TREE_CODE (lhs
) == COMPOUND_EXPR
)
9159 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
9160 compound_side_effects_p
= true;
9161 lhs
= TREE_OPERAND (lhs
, 1);
9164 /* Handle control structure constructs used as "lvalues". Note that we
9165 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
9166 switch (TREE_CODE (lhs
))
9168 /* Handle --foo = 5; as these are valid constructs in C++. */
9169 case PREDECREMENT_EXPR
:
9170 case PREINCREMENT_EXPR
:
9171 if (compound_side_effects_p
)
9172 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
9173 lhs
= genericize_compound_lvalue (lhs
);
9175 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
9176 and looked through the COMPOUND_EXPRs, readd them now around
9177 the resulting lhs. */
9178 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
9180 lhs
= build2 (COMPOUND_EXPR
, lhstype
, TREE_OPERAND (olhs
, 0), lhs
);
9181 tree
*ptr
= &TREE_OPERAND (lhs
, 1);
9182 for (olhs
= TREE_OPERAND (olhs
, 1);
9183 TREE_CODE (olhs
) == COMPOUND_EXPR
;
9184 olhs
= TREE_OPERAND (olhs
, 1))
9186 *ptr
= build2 (COMPOUND_EXPR
, lhstype
,
9187 TREE_OPERAND (olhs
, 0), *ptr
);
9188 ptr
= &TREE_OPERAND (*ptr
, 1);
9194 if (compound_side_effects_p
)
9195 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
9196 lhs
= genericize_compound_lvalue (lhs
);
9197 goto maybe_add_compound
;
9201 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
9202 when neither operand has side-effects. */
9203 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
9204 return error_mark_node
;
9206 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0))
9207 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 1)));
9209 lhs
= build3 (COND_EXPR
, TREE_TYPE (lhs
),
9210 build2 (TREE_CODE (lhs
) == MIN_EXPR
? LE_EXPR
: GE_EXPR
,
9212 TREE_OPERAND (lhs
, 0),
9213 TREE_OPERAND (lhs
, 1)),
9214 TREE_OPERAND (lhs
, 0),
9215 TREE_OPERAND (lhs
, 1));
9218 /* Handle (a ? b : c) used as an "lvalue". */
9221 /* Produce (a ? (b = rhs) : (c = rhs))
9222 except that the RHS goes through a save-expr
9223 so the code to compute it is only emitted once. */
9224 if (VOID_TYPE_P (TREE_TYPE (rhs
)))
9226 if (complain
& tf_error
)
9227 error_at (cp_expr_loc_or_loc (rhs
, loc
),
9228 "void value not ignored as it ought to be");
9229 return error_mark_node
;
9232 rhs
= stabilize_expr (rhs
, &preeval
);
9234 /* Check this here to avoid odd errors when trying to convert
9235 a throw to the type of the COND_EXPR. */
9236 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
9237 return error_mark_node
;
9239 tree op1
= TREE_OPERAND (lhs
, 1);
9240 if (TREE_CODE (op1
) != THROW_EXPR
)
9241 op1
= cp_build_modify_expr (loc
, op1
, modifycode
, rhs
, complain
);
9242 /* When sanitizing undefined behavior, even when rhs doesn't need
9243 stabilization at this point, the sanitization might add extra
9244 SAVE_EXPRs in there and so make sure there is no tree sharing
9245 in the rhs, otherwise those SAVE_EXPRs will have initialization
9246 only in one of the two branches. */
9247 if (sanitize_flags_p (SANITIZE_UNDEFINED
9248 | SANITIZE_UNDEFINED_NONDEFAULT
))
9249 rhs
= unshare_expr (rhs
);
9250 tree op2
= TREE_OPERAND (lhs
, 2);
9251 if (TREE_CODE (op2
) != THROW_EXPR
)
9252 op2
= cp_build_modify_expr (loc
, op2
, modifycode
, rhs
, complain
);
9253 tree cond
= build_conditional_expr (input_location
,
9254 TREE_OPERAND (lhs
, 0), op1
, op2
,
9257 if (cond
== error_mark_node
)
9259 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
9260 and looked through the COMPOUND_EXPRs, readd them now around
9261 the resulting cond before adding the preevaluated rhs. */
9262 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
9264 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
9265 TREE_OPERAND (olhs
, 0), cond
);
9266 tree
*ptr
= &TREE_OPERAND (cond
, 1);
9267 for (olhs
= TREE_OPERAND (olhs
, 1);
9268 TREE_CODE (olhs
) == COMPOUND_EXPR
;
9269 olhs
= TREE_OPERAND (olhs
, 1))
9271 *ptr
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
9272 TREE_OPERAND (olhs
, 0), *ptr
);
9273 ptr
= &TREE_OPERAND (*ptr
, 1);
9276 /* Make sure the code to compute the rhs comes out
9277 before the split. */
9287 if (modifycode
== INIT_EXPR
)
9289 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9290 /* Do the default thing. */;
9291 else if (TREE_CODE (rhs
) == CONSTRUCTOR
)
9293 /* Compound literal. */
9294 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
9295 /* Call convert to generate an error; see PR 11063. */
9296 rhs
= convert (lhstype
, rhs
);
9297 result
= build2 (INIT_EXPR
, lhstype
, lhs
, rhs
);
9298 TREE_SIDE_EFFECTS (result
) = 1;
9301 else if (! MAYBE_CLASS_TYPE_P (lhstype
))
9302 /* Do the default thing. */;
9305 releasing_vec rhs_vec
= make_tree_vector_single (rhs
);
9306 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
9307 &rhs_vec
, lhstype
, LOOKUP_NORMAL
,
9309 if (result
== NULL_TREE
)
9310 return error_mark_node
;
9316 lhs
= require_complete_type (lhs
, complain
);
9317 if (lhs
== error_mark_node
)
9318 return error_mark_node
;
9320 if (modifycode
== NOP_EXPR
)
9322 maybe_warn_self_move (loc
, lhs
, rhs
);
9324 if (c_dialect_objc ())
9326 result
= objc_maybe_build_modify_expr (lhs
, rhs
);
9331 /* `operator=' is not an inheritable operator. */
9332 if (! MAYBE_CLASS_TYPE_P (lhstype
))
9333 /* Do the default thing. */;
9336 result
= build_new_op (input_location
, MODIFY_EXPR
,
9337 LOOKUP_NORMAL
, lhs
, rhs
,
9338 make_node (NOP_EXPR
), NULL_TREE
,
9339 /*overload=*/NULL
, complain
);
9340 if (result
== NULL_TREE
)
9341 return error_mark_node
;
9348 tree init
= NULL_TREE
;
9350 /* A binary op has been requested. Combine the old LHS
9351 value with the RHS producing the value we should actually
9352 store into the LHS. */
9353 gcc_assert (!((TYPE_REF_P (lhstype
)
9354 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype
)))
9355 || MAYBE_CLASS_TYPE_P (lhstype
)));
9357 /* An expression of the form E1 op= E2. [expr.ass] says:
9358 "Such expressions are deprecated if E1 has volatile-qualified
9359 type and op is not one of the bitwise operators |, &, ^."
9360 We warn here rather than in cp_genericize_r because
9361 for compound assignments we are supposed to warn even if the
9362 assignment is a discarded-value expression. */
9363 if (modifycode
!= BIT_AND_EXPR
9364 && modifycode
!= BIT_IOR_EXPR
9365 && modifycode
!= BIT_XOR_EXPR
9366 && (TREE_THIS_VOLATILE (lhs
) || CP_TYPE_VOLATILE_P (lhstype
)))
9367 warning_at (loc
, OPT_Wvolatile
,
9368 "compound assignment with %<volatile%>-qualified left "
9369 "operand is deprecated");
9370 /* Preevaluate the RHS to make sure its evaluation is complete
9371 before the lvalue-to-rvalue conversion of the LHS:
9373 [expr.ass] With respect to an indeterminately-sequenced
9374 function call, the operation of a compound assignment is a
9375 single evaluation. [ Note: Therefore, a function call shall
9376 not intervene between the lvalue-to-rvalue conversion and the
9377 side effect associated with any single compound assignment
9378 operator. -- end note ] */
9379 lhs
= cp_stabilize_reference (lhs
);
9380 rhs
= decay_conversion (rhs
, complain
);
9381 if (rhs
== error_mark_node
)
9382 return error_mark_node
;
9383 rhs
= stabilize_expr (rhs
, &init
);
9384 newrhs
= cp_build_binary_op (loc
, modifycode
, lhs
, rhs
, complain
);
9385 if (newrhs
== error_mark_node
)
9387 if (complain
& tf_error
)
9388 inform (loc
, " in evaluation of %<%Q(%#T, %#T)%>",
9389 modifycode
, TREE_TYPE (lhs
), TREE_TYPE (rhs
));
9390 return error_mark_node
;
9394 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), init
, newrhs
);
9396 /* Now it looks like a plain assignment. */
9397 modifycode
= NOP_EXPR
;
9398 if (c_dialect_objc ())
9400 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
9405 gcc_assert (!TYPE_REF_P (lhstype
));
9406 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs
)));
9409 /* The left-hand side must be an lvalue. */
9410 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
9411 return error_mark_node
;
9413 /* Warn about modifying something that is `const'. Don't warn if
9414 this is initialization. */
9415 if (modifycode
!= INIT_EXPR
9416 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
9417 /* Functions are not modifiable, even though they are
9419 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs
))
9420 /* If it's an aggregate and any field is const, then it is
9421 effectively const. */
9422 || (CLASS_TYPE_P (lhstype
)
9423 && C_TYPE_FIELDS_READONLY (lhstype
))))
9425 if (complain
& tf_error
)
9426 cxx_readonly_error (loc
, lhs
, lv_assign
);
9427 return error_mark_node
;
9430 /* If storing into a structure or union member, it may have been given a
9431 lowered bitfield type. We need to convert to the declared type first,
9432 so retrieve it now. */
9434 olhstype
= unlowered_expr_type (lhs
);
9436 /* Convert new value to destination type. */
9438 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
9442 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs
))
9444 if (modifycode
!= INIT_EXPR
)
9446 if (complain
& tf_error
)
9448 "assigning to an array from an initializer list");
9449 return error_mark_node
;
9451 if (check_array_initializer (lhs
, lhstype
, newrhs
))
9452 return error_mark_node
;
9453 newrhs
= digest_init (lhstype
, newrhs
, complain
);
9454 if (newrhs
== error_mark_node
)
9455 return error_mark_node
;
9458 /* C++11 8.5/17: "If the destination type is an array of characters,
9459 an array of char16_t, an array of char32_t, or an array of wchar_t,
9460 and the initializer is a string literal...". */
9461 else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs
))
9463 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype
)))
9464 && modifycode
== INIT_EXPR
)
9466 newrhs
= digest_init (lhstype
, newrhs
, complain
);
9467 if (newrhs
== error_mark_node
)
9468 return error_mark_node
;
9471 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
9472 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))))
9474 if (complain
& tf_error
)
9475 error_at (loc
, "incompatible types in assignment of %qT to %qT",
9476 TREE_TYPE (rhs
), lhstype
);
9477 return error_mark_node
;
9480 /* Allow array assignment in compiler-generated code. */
9481 else if (!current_function_decl
9482 || !DECL_DEFAULTED_FN (current_function_decl
))
9484 /* This routine is used for both initialization and assignment.
9485 Make sure the diagnostic message differentiates the context. */
9486 if (complain
& tf_error
)
9488 if (modifycode
== INIT_EXPR
)
9489 error_at (loc
, "array used as initializer");
9491 error_at (loc
, "invalid array assignment");
9493 return error_mark_node
;
9496 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
9497 ? 1 + (modifycode
!= INIT_EXPR
): 0;
9498 result
= build_vec_init (lhs
, NULL_TREE
, newrhs
,
9499 /*explicit_value_init_p=*/false,
9500 from_array
, complain
);
9504 if (modifycode
== INIT_EXPR
)
9505 /* Calls with INIT_EXPR are all direct-initialization, so don't set
9506 LOOKUP_ONLYCONVERTING. */
9507 newrhs
= convert_for_initialization (lhs
, olhstype
, newrhs
, LOOKUP_NORMAL
,
9508 ICR_INIT
, NULL_TREE
, 0,
9509 complain
| tf_no_cleanup
);
9511 newrhs
= convert_for_assignment (olhstype
, newrhs
, ICR_ASSIGN
,
9512 NULL_TREE
, 0, complain
, LOOKUP_IMPLICIT
);
9514 if (!same_type_p (lhstype
, olhstype
))
9515 newrhs
= cp_convert_and_check (lhstype
, newrhs
, complain
);
9517 if (modifycode
!= INIT_EXPR
)
9519 if (TREE_CODE (newrhs
) == CALL_EXPR
9520 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
9521 newrhs
= build_cplus_new (lhstype
, newrhs
, complain
);
9523 /* Can't initialize directly from a TARGET_EXPR, since that would
9524 cause the lhs to be constructed twice, and possibly result in
9525 accidental self-initialization. So we force the TARGET_EXPR to be
9526 expanded without a target. */
9527 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
9528 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
9529 TREE_OPERAND (newrhs
, 0));
9532 if (newrhs
== error_mark_node
)
9533 return error_mark_node
;
9535 if (c_dialect_objc () && flag_objc_gc
)
9537 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
9543 result
= build2_loc (loc
, modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
9544 lhstype
, lhs
, newrhs
);
9546 TREE_SIDE_EFFECTS (result
) = 1;
9548 suppress_warning (result
, OPT_Wparentheses
);
9552 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), preeval
, result
);
9557 build_x_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
9558 tree rhs
, tree lookups
, tsubst_flags_t complain
)
9560 tree orig_lhs
= lhs
;
9561 tree orig_rhs
= rhs
;
9562 tree overload
= NULL_TREE
;
9564 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
9565 return cp_expr (error_mark_node
, loc
);
9567 if (processing_template_decl
)
9569 if (modifycode
== NOP_EXPR
9570 || type_dependent_expression_p (lhs
)
9571 || type_dependent_expression_p (rhs
))
9573 tree op
= build_min_nt_loc (loc
, modifycode
, NULL_TREE
, NULL_TREE
);
9574 tree rval
= build_min_nt_loc (loc
, MODOP_EXPR
, lhs
, op
, rhs
);
9575 if (modifycode
!= NOP_EXPR
)
9577 = build_dependent_operator_type (lookups
, modifycode
, true);
9581 lhs
= build_non_dependent_expr (lhs
);
9582 rhs
= build_non_dependent_expr (rhs
);
9585 if (modifycode
!= NOP_EXPR
)
9587 tree op
= build_nt (modifycode
, NULL_TREE
, NULL_TREE
);
9588 tree rval
= build_new_op (loc
, MODIFY_EXPR
, LOOKUP_NORMAL
,
9589 lhs
, rhs
, op
, lookups
, &overload
, complain
);
9592 if (rval
== error_mark_node
)
9594 suppress_warning (rval
/* What warning? */);
9595 if (processing_template_decl
)
9597 if (overload
!= NULL_TREE
)
9598 return (build_min_non_dep_op_overload
9599 (MODIFY_EXPR
, rval
, overload
, orig_lhs
, orig_rhs
));
9601 return (build_min_non_dep
9602 (MODOP_EXPR
, rval
, orig_lhs
, op
, orig_rhs
));
9607 return cp_build_modify_expr (loc
, lhs
, modifycode
, rhs
, complain
);
9610 /* Helper function for get_delta_difference which assumes FROM is a base
9611 class of TO. Returns a delta for the conversion of pointer-to-member
9612 of FROM to pointer-to-member of TO. If the conversion is invalid and
9613 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
9614 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
9615 If C_CAST_P is true, this conversion is taking place as part of a
9619 get_delta_difference_1 (tree from
, tree to
, bool c_cast_p
,
9620 tsubst_flags_t complain
)
9625 binfo
= lookup_base (to
, from
, c_cast_p
? ba_unique
: ba_check
,
9628 if (binfo
== error_mark_node
)
9630 if (!(complain
& tf_error
))
9631 return error_mark_node
;
9633 inform (input_location
, " in pointer to member function conversion");
9634 return size_zero_node
;
9638 if (kind
!= bk_via_virtual
)
9639 return BINFO_OFFSET (binfo
);
9641 /* FROM is a virtual base class of TO. Issue an error or warning
9642 depending on whether or not this is a reinterpret cast. */
9644 if (!(complain
& tf_error
))
9645 return error_mark_node
;
9647 error ("pointer to member conversion via virtual base %qT",
9648 BINFO_TYPE (binfo_from_vbase (binfo
)));
9650 return size_zero_node
;
9657 /* Get difference in deltas for different pointer to member function
9658 types. If the conversion is invalid and tf_error is not set in
9659 COMPLAIN, returns error_mark_node, otherwise returns an integer
9660 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
9661 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
9662 conversions as well. If C_CAST_P is true this conversion is taking
9663 place as part of a C-style cast.
9665 Note that the naming of FROM and TO is kind of backwards; the return
9666 value is what we add to a TO in order to get a FROM. They are named
9667 this way because we call this function to find out how to convert from
9668 a pointer to member of FROM to a pointer to member of TO. */
9671 get_delta_difference (tree from
, tree to
,
9672 bool allow_inverse_p
,
9673 bool c_cast_p
, tsubst_flags_t complain
)
9677 if (same_type_ignoring_top_level_qualifiers_p (from
, to
))
9678 /* Pointer to member of incomplete class is permitted*/
9679 result
= size_zero_node
;
9681 result
= get_delta_difference_1 (from
, to
, c_cast_p
, complain
);
9683 if (result
== error_mark_node
)
9684 return error_mark_node
;
9688 if (!allow_inverse_p
)
9690 if (!(complain
& tf_error
))
9691 return error_mark_node
;
9693 error_not_base_type (from
, to
);
9694 inform (input_location
, " in pointer to member conversion");
9695 result
= size_zero_node
;
9699 result
= get_delta_difference_1 (to
, from
, c_cast_p
, complain
);
9701 if (result
== error_mark_node
)
9702 return error_mark_node
;
9705 result
= size_diffop_loc (input_location
,
9706 size_zero_node
, result
);
9709 if (!(complain
& tf_error
))
9710 return error_mark_node
;
9712 error_not_base_type (from
, to
);
9713 inform (input_location
, " in pointer to member conversion");
9714 result
= size_zero_node
;
9719 return convert_to_integer (ptrdiff_type_node
, result
);
9722 /* Return a constructor for the pointer-to-member-function TYPE using
9723 the other components as specified. */
9726 build_ptrmemfunc1 (tree type
, tree delta
, tree pfn
)
9731 vec
<constructor_elt
, va_gc
> *v
;
9733 /* Pull the FIELD_DECLs out of the type. */
9734 pfn_field
= TYPE_FIELDS (type
);
9735 delta_field
= DECL_CHAIN (pfn_field
);
9737 /* Make sure DELTA has the type we want. */
9738 delta
= convert_and_check (input_location
, delta_type_node
, delta
);
9740 /* Convert to the correct target type if necessary. */
9741 pfn
= fold_convert (TREE_TYPE (pfn_field
), pfn
);
9743 /* Finish creating the initializer. */
9745 CONSTRUCTOR_APPEND_ELT(v
, pfn_field
, pfn
);
9746 CONSTRUCTOR_APPEND_ELT(v
, delta_field
, delta
);
9747 u
= build_constructor (type
, v
);
9748 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) & TREE_CONSTANT (delta
);
9749 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
9750 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
9752 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
9757 /* Build a constructor for a pointer to member function. It can be
9758 used to initialize global variables, local variable, or used
9759 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
9762 If FORCE is nonzero, then force this conversion, even if
9763 we would rather not do it. Usually set when using an explicit
9764 cast. A C-style cast is being processed iff C_CAST_P is true.
9766 Return error_mark_node, if something goes wrong. */
9769 build_ptrmemfunc (tree type
, tree pfn
, int force
, bool c_cast_p
,
9770 tsubst_flags_t complain
)
9776 if (error_operand_p (pfn
))
9777 return error_mark_node
;
9779 pfn_type
= TREE_TYPE (pfn
);
9780 to_type
= build_ptrmemfunc_type (type
);
9782 /* Handle multiple conversions of pointer to member functions. */
9783 if (TYPE_PTRMEMFUNC_P (pfn_type
))
9785 tree delta
= NULL_TREE
;
9786 tree npfn
= NULL_TREE
;
9790 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
,
9791 LOOKUP_NORMAL
, complain
))
9793 if (complain
& tf_error
)
9794 error ("invalid conversion to type %qT from type %qT",
9797 return error_mark_node
;
9800 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
9801 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
9803 c_cast_p
, complain
);
9804 if (n
== error_mark_node
)
9805 return error_mark_node
;
9807 /* We don't have to do any conversion to convert a
9808 pointer-to-member to its own type. But, we don't want to
9809 just return a PTRMEM_CST if there's an explicit cast; that
9810 cast should make the expression an invalid template argument. */
9811 if (TREE_CODE (pfn
) != PTRMEM_CST
)
9813 if (same_type_p (to_type
, pfn_type
))
9815 else if (integer_zerop (n
) && TREE_CODE (pfn
) != CONSTRUCTOR
)
9816 return build_reinterpret_cast (input_location
, to_type
, pfn
,
9820 if (TREE_SIDE_EFFECTS (pfn
))
9821 pfn
= save_expr (pfn
);
9823 /* Obtain the function pointer and the current DELTA. */
9824 if (TREE_CODE (pfn
) == PTRMEM_CST
)
9825 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
9828 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
9829 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
9832 /* Just adjust the DELTA field. */
9833 gcc_assert (same_type_ignoring_top_level_qualifiers_p
9834 (TREE_TYPE (delta
), ptrdiff_type_node
));
9835 if (!integer_zerop (n
))
9837 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
9838 n
= cp_build_binary_op (input_location
,
9839 LSHIFT_EXPR
, n
, integer_one_node
,
9841 delta
= cp_build_binary_op (input_location
,
9842 PLUS_EXPR
, delta
, n
, complain
);
9844 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
9847 /* Handle null pointer to member function conversions. */
9848 if (null_ptr_cst_p (pfn
))
9850 pfn
= cp_build_c_cast (input_location
,
9851 TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type
),
9853 return build_ptrmemfunc1 (to_type
,
9858 if (type_unknown_p (pfn
))
9859 return instantiate_type (type
, pfn
, complain
);
9861 fn
= TREE_OPERAND (pfn
, 0);
9862 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
9863 /* In a template, we will have preserved the
9865 || (processing_template_decl
&& TREE_CODE (fn
) == OFFSET_REF
));
9866 return make_ptrmem_cst (to_type
, fn
);
9869 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
9872 ??? There is no consistency as to the types returned for the above
9873 values. Some code acts as if it were a sizetype and some as if it were
9874 integer_type_node. */
9877 expand_ptrmemfunc_cst (tree cst
, tree
*delta
, tree
*pfn
)
9879 tree type
= TREE_TYPE (cst
);
9880 tree fn
= PTRMEM_CST_MEMBER (cst
);
9881 tree ptr_class
, fn_class
;
9883 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
9885 /* The class that the function belongs to. */
9886 fn_class
= DECL_CONTEXT (fn
);
9888 /* The class that we're creating a pointer to member of. */
9889 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
9891 /* First, calculate the adjustment to the function's class. */
9892 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0,
9893 /*c_cast_p=*/0, tf_warning_or_error
);
9895 if (!DECL_VIRTUAL_P (fn
))
9897 tree t
= build_addr_func (fn
, tf_warning_or_error
);
9898 if (TREE_CODE (t
) == ADDR_EXPR
)
9899 SET_EXPR_LOCATION (t
, PTRMEM_CST_LOCATION (cst
));
9900 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), t
);
9904 /* If we're dealing with a virtual function, we have to adjust 'this'
9905 again, to point to the base which provides the vtable entry for
9906 fn; the call will do the opposite adjustment. */
9907 tree orig_class
= DECL_CONTEXT (fn
);
9908 tree binfo
= binfo_or_else (orig_class
, fn_class
);
9909 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
9910 *delta
, BINFO_OFFSET (binfo
));
9912 /* We set PFN to the vtable offset at which the function can be
9913 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
9914 case delta is shifted left, and then incremented). */
9915 *pfn
= DECL_VINDEX (fn
);
9916 *pfn
= fold_build2 (MULT_EXPR
, integer_type_node
, *pfn
,
9917 TYPE_SIZE_UNIT (vtable_entry_type
));
9919 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
9921 case ptrmemfunc_vbit_in_pfn
:
9922 *pfn
= fold_build2 (PLUS_EXPR
, integer_type_node
, *pfn
,
9926 case ptrmemfunc_vbit_in_delta
:
9927 *delta
= fold_build2 (LSHIFT_EXPR
, TREE_TYPE (*delta
),
9928 *delta
, integer_one_node
);
9929 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
9930 *delta
, integer_one_node
);
9937 *pfn
= fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), *pfn
);
9941 /* Return an expression for PFN from the pointer-to-member function
9945 pfn_from_ptrmemfunc (tree t
)
9947 if (TREE_CODE (t
) == PTRMEM_CST
)
9952 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
9957 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
9960 /* Return an expression for DELTA from the pointer-to-member function
9964 delta_from_ptrmemfunc (tree t
)
9966 if (TREE_CODE (t
) == PTRMEM_CST
)
9971 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
9976 return build_ptrmemfunc_access_expr (t
, delta_identifier
);
9979 /* Convert value RHS to type TYPE as preparation for an assignment to
9980 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
9981 implicit conversion is. If FNDECL is non-NULL, we are doing the
9982 conversion in order to pass the PARMNUMth argument of FNDECL.
9983 If FNDECL is NULL, we are doing the conversion in function pointer
9984 argument passing, conversion in initialization, etc. */
9987 convert_for_assignment (tree type
, tree rhs
,
9988 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
9989 tsubst_flags_t complain
, int flags
)
9992 enum tree_code coder
;
9994 location_t rhs_loc
= cp_expr_loc_or_input_loc (rhs
);
9995 bool has_loc
= EXPR_LOCATION (rhs
) != UNKNOWN_LOCATION
;
9996 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
9997 but preserve location wrappers. */
9998 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
9999 && !location_wrapper_p (rhs
))
10000 rhs
= TREE_OPERAND (rhs
, 0);
10002 /* Handle [dcl.init.list] direct-list-initialization from
10003 single element of enumeration with a fixed underlying type. */
10004 if (is_direct_enum_init (type
, rhs
))
10006 tree elt
= CONSTRUCTOR_ELT (rhs
, 0)->value
;
10007 if (check_narrowing (ENUM_UNDERLYING_TYPE (type
), elt
, complain
))
10009 warning_sentinel
w (warn_useless_cast
);
10010 warning_sentinel
w2 (warn_ignored_qualifiers
);
10011 rhs
= cp_build_c_cast (rhs_loc
, type
, elt
, complain
);
10014 rhs
= error_mark_node
;
10017 rhstype
= TREE_TYPE (rhs
);
10018 coder
= TREE_CODE (rhstype
);
10020 if (VECTOR_TYPE_P (type
) && coder
== VECTOR_TYPE
10021 && vector_types_convertible_p (type
, rhstype
, true))
10023 rhs
= mark_rvalue_use (rhs
);
10024 return convert (type
, rhs
);
10027 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
10028 return error_mark_node
;
10029 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
10030 return error_mark_node
;
10032 /* The RHS of an assignment cannot have void type. */
10033 if (coder
== VOID_TYPE
)
10035 if (complain
& tf_error
)
10036 error_at (rhs_loc
, "void value not ignored as it ought to be");
10037 return error_mark_node
;
10040 if (c_dialect_objc ())
10044 tree rname
= fndecl
;
10055 selector
= objc_message_selector ();
10057 if (selector
&& parmno
> 1)
10064 if (objc_compare_types (type
, rhstype
, parmno
, rname
))
10066 rhs
= mark_rvalue_use (rhs
);
10067 return convert (type
, rhs
);
10073 The expression is implicitly converted (clause _conv_) to the
10074 cv-unqualified type of the left operand.
10076 We allow bad conversions here because by the time we get to this point
10077 we are committed to doing the conversion. If we end up doing a bad
10078 conversion, convert_like will complain. */
10079 if (!can_convert_arg_bad (type
, rhstype
, rhs
, flags
, complain
))
10081 /* When -Wno-pmf-conversions is use, we just silently allow
10082 conversions from pointers-to-members to plain pointers. If
10083 the conversion doesn't work, cp_convert will complain. */
10085 && TYPE_PTR_P (type
)
10086 && TYPE_PTRMEMFUNC_P (rhstype
))
10087 rhs
= cp_convert (strip_top_quals (type
), rhs
, complain
);
10090 if (complain
& tf_error
)
10092 /* If the right-hand side has unknown type, then it is an
10093 overloaded function. Call instantiate_type to get error
10095 if (rhstype
== unknown_type_node
)
10097 tree r
= instantiate_type (type
, rhs
, tf_warning_or_error
);
10098 /* -fpermissive might allow this; recurse. */
10099 if (!seen_error ())
10100 return convert_for_assignment (type
, r
, errtype
, fndecl
,
10101 parmnum
, complain
, flags
);
10104 complain_about_bad_argument (rhs_loc
,
10109 range_label_for_type_mismatch
label (rhstype
, type
);
10110 gcc_rich_location
richloc (rhs_loc
, has_loc
? &label
: NULL
);
10113 case ICR_DEFAULT_ARGUMENT
:
10114 error_at (&richloc
,
10115 "cannot convert %qH to %qI in default argument",
10119 error_at (&richloc
,
10120 "cannot convert %qH to %qI in argument passing",
10123 case ICR_CONVERTING
:
10124 error_at (&richloc
, "cannot convert %qH to %qI",
10128 error_at (&richloc
,
10129 "cannot convert %qH to %qI in initialization",
10133 error_at (&richloc
, "cannot convert %qH to %qI in return",
10137 error_at (&richloc
,
10138 "cannot convert %qH to %qI in assignment",
10145 if (TYPE_PTR_P (rhstype
)
10146 && TYPE_PTR_P (type
)
10147 && CLASS_TYPE_P (TREE_TYPE (rhstype
))
10148 && CLASS_TYPE_P (TREE_TYPE (type
))
10149 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype
)))
10150 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
10151 (TREE_TYPE (rhstype
))),
10152 "class type %qT is incomplete", TREE_TYPE (rhstype
));
10154 return error_mark_node
;
10157 if (warn_suggest_attribute_format
)
10159 const enum tree_code codel
= TREE_CODE (type
);
10160 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
10162 && check_missing_format_attribute (type
, rhstype
)
10163 && (complain
& tf_warning
))
10167 case ICR_DEFAULT_ARGUMENT
:
10169 warning (OPT_Wsuggest_attribute_format
,
10170 "parameter %qP of %qD might be a candidate "
10171 "for a format attribute", parmnum
, fndecl
);
10173 warning (OPT_Wsuggest_attribute_format
,
10174 "parameter might be a candidate "
10175 "for a format attribute");
10177 case ICR_CONVERTING
:
10178 warning (OPT_Wsuggest_attribute_format
,
10179 "target of conversion might be a candidate "
10180 "for a format attribute");
10183 warning (OPT_Wsuggest_attribute_format
,
10184 "target of initialization might be a candidate "
10185 "for a format attribute");
10188 warning (OPT_Wsuggest_attribute_format
,
10189 "return type might be a candidate "
10190 "for a format attribute");
10193 warning (OPT_Wsuggest_attribute_format
,
10194 "left-hand side of assignment might be a candidate "
10195 "for a format attribute");
10202 /* If -Wparentheses, warn about a = b = c when a has type bool and b
10204 if (warn_parentheses
10205 && TREE_CODE (type
) == BOOLEAN_TYPE
10206 && TREE_CODE (rhs
) == MODIFY_EXPR
10207 && !warning_suppressed_p (rhs
, OPT_Wparentheses
)
10208 && TREE_CODE (TREE_TYPE (rhs
)) != BOOLEAN_TYPE
10209 && (complain
& tf_warning
)
10210 && warning_at (rhs_loc
, OPT_Wparentheses
,
10211 "suggest parentheses around assignment used as "
10213 suppress_warning (rhs
, OPT_Wparentheses
);
10215 if (complain
& tf_warning
)
10216 warn_for_address_or_pointer_of_packed_member (type
, rhs
);
10218 return perform_implicit_conversion_flags (strip_top_quals (type
), rhs
,
10222 /* Convert RHS to be of type TYPE.
10223 If EXP is nonzero, it is the target of the initialization.
10224 ERRTYPE indicates what kind of error the implicit conversion is.
10226 Two major differences between the behavior of
10227 `convert_for_assignment' and `convert_for_initialization'
10228 are that references are bashed in the former, while
10229 copied in the latter, and aggregates are assigned in
10230 the former (operator=) while initialized in the
10233 If using constructor make sure no conversion operator exists, if one does
10234 exist, an ambiguity exists. */
10237 convert_for_initialization (tree exp
, tree type
, tree rhs
, int flags
,
10238 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
10239 tsubst_flags_t complain
)
10241 enum tree_code codel
= TREE_CODE (type
);
10243 enum tree_code coder
;
10245 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
10246 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
10247 if (TREE_CODE (rhs
) == NOP_EXPR
10248 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
10249 && codel
!= REFERENCE_TYPE
)
10250 rhs
= TREE_OPERAND (rhs
, 0);
10252 if (type
== error_mark_node
10253 || rhs
== error_mark_node
10254 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
10255 return error_mark_node
;
10257 if (MAYBE_CLASS_TYPE_P (non_reference (type
)))
10259 else if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
10260 && TREE_CODE (type
) != ARRAY_TYPE
10261 && (!TYPE_REF_P (type
)
10262 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
10263 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
10264 && !TYPE_REFFN_P (type
))
10265 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
10266 rhs
= decay_conversion (rhs
, complain
);
10268 rhstype
= TREE_TYPE (rhs
);
10269 coder
= TREE_CODE (rhstype
);
10271 if (coder
== ERROR_MARK
)
10272 return error_mark_node
;
10274 /* We accept references to incomplete types, so we can
10275 return here before checking if RHS is of complete type. */
10277 if (codel
== REFERENCE_TYPE
)
10279 auto_diagnostic_group d
;
10280 /* This should eventually happen in convert_arguments. */
10281 int savew
= 0, savee
= 0;
10284 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
10285 rhs
= initialize_reference (type
, rhs
, flags
, complain
);
10288 && (warningcount
+ werrorcount
> savew
|| errorcount
> savee
))
10289 inform (get_fndecl_argument_location (fndecl
, parmnum
),
10290 "in passing argument %P of %qD", parmnum
, fndecl
);
10295 exp
= require_complete_type (exp
, complain
);
10296 if (exp
== error_mark_node
)
10297 return error_mark_node
;
10299 type
= complete_type (type
);
10301 if (DIRECT_INIT_EXPR_P (type
, rhs
))
10302 /* Don't try to do copy-initialization if we already have
10303 direct-initialization. */
10306 if (MAYBE_CLASS_TYPE_P (type
))
10307 return perform_implicit_conversion_flags (type
, rhs
, complain
, flags
);
10309 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
,
10313 /* If RETVAL is the address of, or a reference to, a local variable or
10314 temporary give an appropriate warning and return true. */
10317 maybe_warn_about_returning_address_of_local (tree retval
, location_t loc
)
10319 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
10320 tree whats_returned
= fold_for_warn (retval
);
10322 loc
= cp_expr_loc_or_input_loc (retval
);
10326 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
10327 whats_returned
= TREE_OPERAND (whats_returned
, 1);
10328 else if (CONVERT_EXPR_P (whats_returned
)
10329 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
)
10330 whats_returned
= TREE_OPERAND (whats_returned
, 0);
10335 if (TREE_CODE (whats_returned
) == TARGET_EXPR
10336 && is_std_init_list (TREE_TYPE (whats_returned
)))
10338 tree init
= TARGET_EXPR_INITIAL (whats_returned
);
10339 if (TREE_CODE (init
) == CONSTRUCTOR
)
10340 /* Pull out the array address. */
10341 whats_returned
= CONSTRUCTOR_ELT (init
, 0)->value
;
10342 else if (TREE_CODE (init
) == INDIRECT_REF
)
10343 /* The source of a trivial copy looks like *(T*)&var. */
10344 whats_returned
= TREE_OPERAND (init
, 0);
10347 STRIP_NOPS (whats_returned
);
10350 /* As a special case, we handle a call to std::move or std::forward. */
10351 if (TREE_CODE (whats_returned
) == CALL_EXPR
10352 && (is_std_move_p (whats_returned
)
10353 || is_std_forward_p (whats_returned
)))
10355 tree arg
= CALL_EXPR_ARG (whats_returned
, 0);
10356 return maybe_warn_about_returning_address_of_local (arg
, loc
);
10359 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
10361 whats_returned
= TREE_OPERAND (whats_returned
, 0);
10363 while (TREE_CODE (whats_returned
) == COMPONENT_REF
10364 || TREE_CODE (whats_returned
) == ARRAY_REF
)
10365 whats_returned
= TREE_OPERAND (whats_returned
, 0);
10367 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
10368 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
10370 if (TYPE_REF_P (valtype
))
10371 warning_at (loc
, OPT_Wreturn_local_addr
,
10372 "returning reference to temporary");
10373 else if (is_std_init_list (valtype
))
10374 warning_at (loc
, OPT_Winit_list_lifetime
,
10375 "returning temporary %<initializer_list%> does not extend "
10376 "the lifetime of the underlying array");
10380 STRIP_ANY_LOCATION_WRAPPER (whats_returned
);
10382 if (DECL_P (whats_returned
)
10383 && DECL_NAME (whats_returned
)
10384 && DECL_FUNCTION_SCOPE_P (whats_returned
)
10385 && !is_capture_proxy (whats_returned
)
10386 && !(TREE_STATIC (whats_returned
)
10387 || TREE_PUBLIC (whats_returned
)))
10389 if (VAR_P (whats_returned
)
10390 && DECL_DECOMPOSITION_P (whats_returned
)
10391 && DECL_DECOMP_BASE (whats_returned
)
10392 && DECL_HAS_VALUE_EXPR_P (whats_returned
))
10394 /* When returning address of a structured binding, if the structured
10395 binding is not a reference, continue normally, if it is a
10396 reference, recurse on the initializer of the structured
10398 tree base
= DECL_DECOMP_BASE (whats_returned
);
10399 if (TYPE_REF_P (TREE_TYPE (base
)))
10401 if (tree init
= DECL_INITIAL (base
))
10402 return maybe_warn_about_returning_address_of_local (init
, loc
);
10408 auto_diagnostic_group d
;
10409 if (TYPE_REF_P (valtype
))
10410 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
10411 "reference to local variable %qD returned",
10413 else if (is_std_init_list (valtype
))
10414 w
= warning_at (loc
, OPT_Winit_list_lifetime
,
10415 "returning local %<initializer_list%> variable %qD "
10416 "does not extend the lifetime of the underlying array",
10418 else if (POINTER_TYPE_P (valtype
)
10419 && TREE_CODE (whats_returned
) == LABEL_DECL
)
10420 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
10421 "address of label %qD returned",
10423 else if (POINTER_TYPE_P (valtype
))
10424 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
10425 "address of local variable %qD returned",
10428 inform (DECL_SOURCE_LOCATION (whats_returned
),
10436 /* Returns true if DECL is in the std namespace. */
10439 decl_in_std_namespace_p (tree decl
)
10443 decl
= decl_namespace_context (decl
);
10444 if (DECL_NAMESPACE_STD_P (decl
))
10446 /* Allow inline namespaces inside of std namespace, e.g. with
10447 --enable-symvers=gnu-versioned-namespace std::forward would be
10448 actually std::_8::forward. */
10449 if (!DECL_NAMESPACE_INLINE_P (decl
))
10451 decl
= CP_DECL_CONTEXT (decl
);
10456 /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
10459 is_std_forward_p (tree fn
)
10461 /* std::forward only takes one argument. */
10462 if (call_expr_nargs (fn
) != 1)
10465 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
10466 if (!decl_in_std_namespace_p (fndecl
))
10469 tree name
= DECL_NAME (fndecl
);
10470 return name
&& id_equal (name
, "forward");
10473 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
10476 is_std_move_p (tree fn
)
10478 /* std::move only takes one argument. */
10479 if (call_expr_nargs (fn
) != 1)
10482 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
10483 if (!decl_in_std_namespace_p (fndecl
))
10486 tree name
= DECL_NAME (fndecl
);
10487 return name
&& id_equal (name
, "move");
10490 /* Returns true if RETVAL is a good candidate for the NRVO as per
10491 [class.copy.elision]. FUNCTYPE is the type the function is declared
10495 can_do_nrvo_p (tree retval
, tree functype
)
10497 if (functype
== error_mark_node
)
10500 STRIP_ANY_LOCATION_WRAPPER (retval
);
10501 tree result
= DECL_RESULT (current_function_decl
);
10502 return (retval
!= NULL_TREE
10503 && !processing_template_decl
10504 /* Must be a local, automatic variable. */
10506 && DECL_CONTEXT (retval
) == current_function_decl
10507 && !TREE_STATIC (retval
)
10508 /* And not a lambda or anonymous union proxy. */
10509 && !DECL_HAS_VALUE_EXPR_P (retval
)
10510 && (DECL_ALIGN (retval
) <= DECL_ALIGN (result
))
10511 /* The cv-unqualified type of the returned value must be the
10512 same as the cv-unqualified return type of the
10514 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval
)),
10515 TYPE_MAIN_VARIANT (functype
))
10516 /* And the returned value must be non-volatile. */
10517 && !TYPE_VOLATILE (TREE_TYPE (retval
)));
10520 /* Like can_do_nrvo_p, but we check if we're trying to move a class
10524 can_elide_copy_prvalue_p (tree retval
, tree functype
)
10526 if (functype
== error_mark_node
)
10529 STRIP_ANY_LOCATION_WRAPPER (retval
);
10530 return (retval
!= NULL_TREE
10531 && !glvalue_p (retval
)
10532 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval
)),
10533 TYPE_MAIN_VARIANT (functype
))
10534 && !TYPE_VOLATILE (TREE_TYPE (retval
)));
10537 /* If we should treat RETVAL, an expression being returned, as if it were
10538 designated by an rvalue, returns it adjusted accordingly; otherwise, returns
10539 NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
10540 context (rather than throw). */
10543 treat_lvalue_as_rvalue_p (tree expr
, bool return_p
)
10545 if (cxx_dialect
== cxx98
)
10548 tree retval
= expr
;
10549 STRIP_ANY_LOCATION_WRAPPER (retval
);
10550 if (REFERENCE_REF_P (retval
))
10551 retval
= TREE_OPERAND (retval
, 0);
10553 /* An implicitly movable entity is a variable of automatic storage duration
10554 that is either a non-volatile object or (C++20) an rvalue reference to a
10555 non-volatile object type. */
10556 if (!(((VAR_P (retval
) && !DECL_HAS_VALUE_EXPR_P (retval
))
10557 || TREE_CODE (retval
) == PARM_DECL
)
10558 && !TREE_STATIC (retval
)
10559 && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval
)))
10560 && (TREE_CODE (TREE_TYPE (retval
)) != REFERENCE_TYPE
10561 || (cxx_dialect
>= cxx20
10562 && TYPE_REF_IS_RVALUE (TREE_TYPE (retval
))))))
10565 /* If the expression in a return or co_return statement is a (possibly
10566 parenthesized) id-expression that names an implicitly movable entity
10567 declared in the body or parameter-declaration-clause of the innermost
10568 enclosing function or lambda-expression, */
10569 if (DECL_CONTEXT (retval
) != current_function_decl
)
10572 return set_implicit_rvalue_p (move (expr
));
10574 /* if the operand of a throw-expression is a (possibly parenthesized)
10575 id-expression that names an implicitly movable entity whose scope does not
10576 extend beyond the compound-statement of the innermost try-block or
10577 function-try-block (if any) whose compound-statement or ctor-initializer
10578 encloses the throw-expression, */
10580 /* C++20 added move on throw of parms. */
10581 if (TREE_CODE (retval
) == PARM_DECL
&& cxx_dialect
< cxx20
)
10584 for (cp_binding_level
*b
= current_binding_level
;
10585 ; b
= b
->level_chain
)
10587 for (tree decl
= b
->names
; decl
; decl
= TREE_CHAIN (decl
))
10588 if (decl
== retval
)
10589 return set_implicit_rvalue_p (move (expr
));
10590 if (b
->kind
== sk_function_parms
|| b
->kind
== sk_try
)
10595 /* Warn about dubious usage of std::move (in a return statement, if RETURN_P
10596 is true). EXPR is the std::move expression; TYPE is the type of the object
10597 being initialized. */
10600 maybe_warn_pessimizing_move (tree expr
, tree type
, bool return_p
)
10602 if (!(warn_pessimizing_move
|| warn_redundant_move
))
10605 const location_t loc
= cp_expr_loc_or_input_loc (expr
);
10607 /* C++98 doesn't know move. */
10608 if (cxx_dialect
< cxx11
)
10611 /* Wait until instantiation time, since we can't gauge if we should do
10612 the NRVO until then. */
10613 if (processing_template_decl
)
10616 /* This is only interesting for class types. */
10617 if (!CLASS_TYPE_P (type
))
10620 bool wrapped_p
= false;
10621 /* A a = std::move (A()); */
10622 if (TREE_CODE (expr
) == TREE_LIST
)
10624 if (list_length (expr
) == 1)
10626 expr
= TREE_VALUE (expr
);
10632 /* A a = {std::move (A())};
10633 A a{std::move (A())}; */
10634 else if (TREE_CODE (expr
) == CONSTRUCTOR
)
10636 if (CONSTRUCTOR_NELTS (expr
) == 1)
10638 expr
= CONSTRUCTOR_ELT (expr
, 0)->value
;
10645 /* First, check if this is a call to std::move. */
10646 if (!REFERENCE_REF_P (expr
)
10647 || TREE_CODE (TREE_OPERAND (expr
, 0)) != CALL_EXPR
)
10649 tree fn
= TREE_OPERAND (expr
, 0);
10650 if (!is_std_move_p (fn
))
10652 tree arg
= CALL_EXPR_ARG (fn
, 0);
10653 if (TREE_CODE (arg
) != NOP_EXPR
)
10655 /* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
10656 and implicitly-movable warnings. */
10657 if (TREE_CODE (TREE_OPERAND (arg
, 0)) == ADDR_EXPR
)
10659 arg
= TREE_OPERAND (arg
, 0);
10660 arg
= TREE_OPERAND (arg
, 0);
10661 arg
= convert_from_reference (arg
);
10662 if (can_elide_copy_prvalue_p (arg
, type
))
10664 auto_diagnostic_group d
;
10665 if (warning_at (loc
, OPT_Wpessimizing_move
,
10666 "moving a temporary object prevents copy elision"))
10667 inform (loc
, "remove %<std::move%> call");
10669 /* The rest of the warnings is only relevant for when we are returning
10670 from a function. */
10675 /* Warn if we could do copy elision were it not for the move. */
10676 if (can_do_nrvo_p (arg
, type
))
10678 auto_diagnostic_group d
;
10679 if (!warning_suppressed_p (expr
, OPT_Wpessimizing_move
)
10680 && warning_at (loc
, OPT_Wpessimizing_move
,
10681 "moving a local object in a return statement "
10682 "prevents copy elision"))
10683 inform (loc
, "remove %<std::move%> call");
10685 /* Warn if the move is redundant. It is redundant when we would
10686 do maybe-rvalue overload resolution even without std::move. */
10687 else if (warn_redundant_move
10688 /* This doesn't apply for return {std::move (t)};. */
10690 && !warning_suppressed_p (expr
, OPT_Wredundant_move
)
10691 && (moved
= treat_lvalue_as_rvalue_p (arg
, /*return*/true)))
10693 /* Make sure that overload resolution would actually succeed
10694 if we removed the std::move call. */
10695 tree t
= convert_for_initialization (NULL_TREE
, type
,
10698 | LOOKUP_ONLYCONVERTING
10699 | LOOKUP_PREFER_RVALUE
),
10700 ICR_RETURN
, NULL_TREE
, 0,
10702 /* If this worked, implicit rvalue would work, so the call to
10703 std::move is redundant. */
10704 if (t
!= error_mark_node
10705 /* Trying to move something const will never succeed unless
10706 there's T(const T&&), which it almost never is, and if
10707 so, T wouldn't be error_mark_node now: the above convert_
10708 call with LOOKUP_PREFER_RVALUE returns an error if a const T&
10709 overload is selected. */
10710 || (CP_TYPE_CONST_P (TREE_TYPE (arg
))
10711 && same_type_ignoring_top_level_qualifiers_p
10712 (TREE_TYPE (arg
), type
)))
10714 auto_diagnostic_group d
;
10715 if (warning_at (loc
, OPT_Wredundant_move
,
10716 "redundant move in return statement"))
10717 inform (loc
, "remove %<std::move%> call");
10721 /* Also try to warn about redundant std::move in code such as
10724 return std::move(t);
10726 for which EXPR will be something like
10727 *std::move<const T&> ((const struct T &) (const struct T *) t)
10728 and where the std::move does nothing if T does not have a T(const T&&)
10729 constructor, because the argument is const. It will not use T(T&&)
10730 because that would mean losing the const. */
10731 else if (TYPE_REF_P (TREE_TYPE (arg
))
10732 && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg
))))
10734 tree rtype
= TREE_TYPE (TREE_TYPE (arg
));
10735 if (!same_type_ignoring_top_level_qualifiers_p (rtype
, type
))
10737 /* Check for the unlikely case there's T(const T&&) (we don't care if
10739 for (tree fn
: ovl_range (CLASSTYPE_CONSTRUCTORS (rtype
)))
10740 if (move_fn_p (fn
))
10742 tree t
= TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn
));
10743 if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t
))))
10746 auto_diagnostic_group d
;
10747 if (warning_at (loc
, OPT_Wredundant_move
,
10748 "redundant move in return statement"))
10749 inform (loc
, "remove %<std::move%> call");
10753 /* Check that returning RETVAL from the current function is valid.
10754 Return an expression explicitly showing all conversions required to
10755 change RETVAL into the function return type, and to assign it to
10756 the DECL_RESULT for the function. Set *NO_WARNING to true if
10757 code reaches end of non-void function warning shouldn't be issued
10758 on this RETURN_EXPR. */
10761 check_return_expr (tree retval
, bool *no_warning
)
10764 /* The type actually returned by the function. */
10766 /* The type the function is declared to return, or void if
10767 the declared type is incomplete. */
10769 int fn_returns_value_p
;
10770 location_t loc
= cp_expr_loc_or_input_loc (retval
);
10772 *no_warning
= false;
10774 /* A `volatile' function is one that isn't supposed to return, ever.
10775 (This is a G++ extension, used to get better code for functions
10776 that call the `volatile' function.) */
10777 if (TREE_THIS_VOLATILE (current_function_decl
))
10778 warning (0, "function declared %<noreturn%> has a %<return%> statement");
10780 /* Check for various simple errors. */
10781 if (DECL_DESTRUCTOR_P (current_function_decl
))
10784 error_at (loc
, "returning a value from a destructor");
10786 if (targetm
.cxx
.cdtor_returns_this () && !processing_template_decl
)
10787 retval
= current_class_ptr
;
10791 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
10793 if (in_function_try_handler
)
10794 /* If a return statement appears in a handler of the
10795 function-try-block of a constructor, the program is ill-formed. */
10796 error ("cannot return from a handler of a function-try-block of a constructor");
10798 /* You can't return a value from a constructor. */
10799 error_at (loc
, "returning a value from a constructor");
10801 if (targetm
.cxx
.cdtor_returns_this () && !processing_template_decl
)
10802 retval
= current_class_ptr
;
10807 const tree saved_retval
= retval
;
10809 if (processing_template_decl
)
10811 current_function_returns_value
= 1;
10813 if (check_for_bare_parameter_packs (retval
))
10814 return error_mark_node
;
10816 /* If one of the types might be void, we can't tell whether we're
10817 returning a value. */
10818 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl
)))
10819 && !FNDECL_USED_AUTO (current_function_decl
))
10820 || (retval
!= NULL_TREE
10821 && (TREE_TYPE (retval
) == NULL_TREE
10822 || WILDCARD_TYPE_P (TREE_TYPE (retval
)))))
10826 functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
10828 /* Deduce auto return type from a return statement. */
10829 if (FNDECL_USED_AUTO (current_function_decl
))
10831 tree pattern
= DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
);
10835 if (!retval
&& !is_auto (pattern
))
10837 /* Give a helpful error message. */
10838 error ("return-statement with no value, in function returning %qT",
10840 inform (input_location
, "only plain %<auto%> return type can be "
10841 "deduced to %<void%>");
10842 type
= error_mark_node
;
10844 else if (retval
&& BRACE_ENCLOSED_INITIALIZER_P (retval
))
10846 error ("returning initializer list");
10847 type
= error_mark_node
;
10852 retval
= void_node
;
10853 auto_node
= type_uses_auto (pattern
);
10854 type
= do_auto_deduction (pattern
, retval
, auto_node
,
10855 tf_warning_or_error
, adc_return_type
);
10858 if (type
== error_mark_node
)
10860 else if (functype
== pattern
)
10861 apply_deduced_return_type (current_function_decl
, type
);
10862 else if (!same_type_p (type
, functype
))
10864 if (LAMBDA_FUNCTION_P (current_function_decl
))
10865 error_at (loc
, "inconsistent types %qT and %qT deduced for "
10866 "lambda return type", functype
, type
);
10868 error_at (loc
, "inconsistent deduction for auto return type: "
10869 "%qT and then %qT", functype
, type
);
10874 result
= DECL_RESULT (current_function_decl
);
10875 valtype
= TREE_TYPE (result
);
10876 gcc_assert (valtype
!= NULL_TREE
);
10877 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
10879 /* Check for a return statement with no return value in a function
10880 that's supposed to return a value. */
10881 if (!retval
&& fn_returns_value_p
)
10883 if (functype
!= error_mark_node
)
10884 permerror (input_location
, "return-statement with no value, in "
10885 "function returning %qT", valtype
);
10886 /* Remember that this function did return. */
10887 current_function_returns_value
= 1;
10888 /* And signal caller that TREE_NO_WARNING should be set on the
10889 RETURN_EXPR to avoid control reaches end of non-void function
10890 warnings in tree-cfg.cc. */
10891 *no_warning
= true;
10893 /* Check for a return statement with a value in a function that
10894 isn't supposed to return a value. */
10895 else if (retval
&& !fn_returns_value_p
)
10897 if (VOID_TYPE_P (TREE_TYPE (retval
)))
10898 /* You can return a `void' value from a function of `void'
10899 type. In that case, we have to evaluate the expression for
10900 its side-effects. */
10901 finish_expr_stmt (retval
);
10902 else if (retval
!= error_mark_node
)
10903 permerror (loc
, "return-statement with a value, in function "
10904 "returning %qT", valtype
);
10905 current_function_returns_null
= 1;
10907 /* There's really no value to return, after all. */
10911 /* Remember that this function can sometimes return without a
10913 current_function_returns_null
= 1;
10915 /* Remember that this function did return a value. */
10916 current_function_returns_value
= 1;
10918 /* Check for erroneous operands -- but after giving ourselves a
10919 chance to provide an error about returning a value from a void
10921 if (error_operand_p (retval
))
10923 current_function_return_value
= error_mark_node
;
10924 return error_mark_node
;
10927 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
10928 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl
))
10929 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
10930 && ! flag_check_new
10931 && retval
&& null_ptr_cst_p (retval
))
10932 warning (0, "%<operator new%> must not return NULL unless it is "
10933 "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
10935 /* Effective C++ rule 15. See also start_function. */
10937 && DECL_NAME (current_function_decl
) == assign_op_identifier
10938 && !type_dependent_expression_p (retval
))
10942 /* The function return type must be a reference to the current
10944 if (TYPE_REF_P (valtype
)
10945 && same_type_ignoring_top_level_qualifiers_p
10946 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
)))
10948 /* Returning '*this' is obviously OK. */
10949 if (retval
== current_class_ref
)
10951 /* If we are calling a function whose return type is the same of
10952 the current class reference, it is ok. */
10953 else if (INDIRECT_REF_P (retval
)
10954 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
10959 warning_at (loc
, OPT_Weffc__
,
10960 "%<operator=%> should return a reference to %<*this%>");
10963 if (dependent_type_p (functype
)
10964 || type_dependent_expression_p (retval
))
10967 /* We should not have changed the return value. */
10968 gcc_assert (retval
== saved_retval
);
10969 /* We don't know if this is an lvalue or rvalue use, but
10970 either way we can mark it as read. */
10971 mark_exp_read (retval
);
10972 /* Disable our std::move warnings when we're returning
10973 a dependent expression (c++/89780). */
10974 if (retval
&& TREE_CODE (retval
) == CALL_EXPR
)
10975 /* This also suppresses -Wredundant-move. */
10976 suppress_warning (retval
, OPT_Wpessimizing_move
);
10980 /* The fabled Named Return Value optimization, as per [class.copy]/15:
10982 [...] For a function with a class return type, if the expression
10983 in the return statement is the name of a local object, and the cv-
10984 unqualified type of the local object is the same as the function
10985 return type, an implementation is permitted to omit creating the tem-
10986 porary object to hold the function return value [...]
10988 So, if this is a value-returning function that always returns the same
10989 local variable, remember it.
10991 It might be nice to be more flexible, and choose the first suitable
10992 variable even if the function sometimes returns something else, but
10993 then we run the risk of clobbering the variable we chose if the other
10994 returned expression uses the chosen variable somehow. And people expect
10995 this restriction, anyway. (jason 2000-11-19)
10997 See finish_function and finalize_nrv for the rest of this optimization. */
10998 tree bare_retval
= NULL_TREE
;
11001 retval
= maybe_undo_parenthesized_ref (retval
);
11002 bare_retval
= tree_strip_any_location_wrapper (retval
);
11005 bool named_return_value_okay_p
= can_do_nrvo_p (bare_retval
, functype
);
11006 if (fn_returns_value_p
&& flag_elide_constructors
)
11008 if (named_return_value_okay_p
11009 && (current_function_return_value
== NULL_TREE
11010 || current_function_return_value
== bare_retval
))
11011 current_function_return_value
= bare_retval
;
11013 current_function_return_value
= error_mark_node
;
11016 /* We don't need to do any conversions when there's nothing being
11021 if (!named_return_value_okay_p
)
11022 maybe_warn_pessimizing_move (retval
, functype
, /*return_p*/true);
11024 /* Do any required conversions. */
11025 if (bare_retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
11026 /* No conversions are required. */
11030 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
11032 /* The functype's return type will have been set to void, if it
11033 was an incomplete type. Just treat this as 'return;' */
11034 if (VOID_TYPE_P (functype
))
11035 return error_mark_node
;
11037 if (processing_template_decl
)
11038 retval
= build_non_dependent_expr (retval
);
11040 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
11041 treated as an rvalue for the purposes of overload resolution to
11042 favor move constructors over copy constructors.
11044 Note that these conditions are similar to, but not as strict as,
11045 the conditions for the named return value optimization. */
11046 bool converted
= false;
11048 /* Until C++23, this was only interesting for class type, but in C++23,
11049 we should do the below when we're converting rom/to a class/reference
11050 (a non-scalar type). */
11051 if ((cxx_dialect
< cxx23
11052 ? CLASS_TYPE_P (functype
)
11053 : !SCALAR_TYPE_P (functype
) || !SCALAR_TYPE_P (TREE_TYPE (retval
)))
11054 && (moved
= treat_lvalue_as_rvalue_p (retval
, /*return*/true)))
11056 if (cxx_dialect
< cxx20
)
11058 moved
= convert_for_initialization
11059 (NULL_TREE
, functype
, moved
, flags
|LOOKUP_PREFER_RVALUE
,
11060 ICR_RETURN
, NULL_TREE
, 0, tf_none
);
11061 if (moved
!= error_mark_node
)
11068 /* In C++20 we just treat the return value as an rvalue that
11069 can bind to lvalue refs. */
11073 /* The call in a (lambda) thunk needs no conversions. */
11074 if (TREE_CODE (retval
) == CALL_EXPR
11075 && call_from_lambda_thunk_p (retval
))
11078 /* First convert the value to the function's return type, then
11079 to the type of return value's location to handle the
11080 case that functype is smaller than the valtype. */
11082 retval
= convert_for_initialization
11083 (NULL_TREE
, functype
, retval
, flags
, ICR_RETURN
, NULL_TREE
, 0,
11084 tf_warning_or_error
);
11085 retval
= convert (valtype
, retval
);
11087 /* If the conversion failed, treat this just like `return;'. */
11088 if (retval
== error_mark_node
)
11090 /* We can't initialize a register from a AGGR_INIT_EXPR. */
11091 else if (! cfun
->returns_struct
11092 && TREE_CODE (retval
) == TARGET_EXPR
11093 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
11094 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
11095 TREE_OPERAND (retval
, 0));
11096 else if (!processing_template_decl
11097 && maybe_warn_about_returning_address_of_local (retval
, loc
)
11098 && INDIRECT_TYPE_P (valtype
))
11099 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
11100 build_zero_cst (TREE_TYPE (retval
)));
11103 if (processing_template_decl
)
11104 return saved_retval
;
11106 /* Actually copy the value returned into the appropriate location. */
11107 if (retval
&& retval
!= result
)
11108 retval
= build2 (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
11110 if (tree set
= maybe_set_retval_sentinel ())
11111 retval
= build2 (COMPOUND_EXPR
, void_type_node
, retval
, set
);
11117 /* Returns nonzero if the pointer-type FROM can be converted to the
11118 pointer-type TO via a qualification conversion. If CONSTP is -1,
11119 then we return nonzero if the pointers are similar, and the
11120 cv-qualification signature of FROM is a proper subset of that of TO.
11122 If CONSTP is positive, then all outer pointers have been
11123 const-qualified. */
11126 comp_ptr_ttypes_real (tree to
, tree from
, int constp
)
11128 bool to_more_cv_qualified
= false;
11129 bool is_opaque_pointer
= false;
11131 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
11133 if (TREE_CODE (to
) != TREE_CODE (from
))
11136 if (TREE_CODE (from
) == OFFSET_TYPE
11137 && !same_type_p (TYPE_OFFSET_BASETYPE (from
),
11138 TYPE_OFFSET_BASETYPE (to
)))
11141 /* Const and volatile mean something different for function and
11142 array types, so the usual checks are not appropriate. We'll
11143 check the array type elements in further iterations. */
11144 if (!FUNC_OR_METHOD_TYPE_P (to
) && TREE_CODE (to
) != ARRAY_TYPE
)
11146 if (!at_least_as_qualified_p (to
, from
))
11149 if (!at_least_as_qualified_p (from
, to
))
11153 to_more_cv_qualified
= true;
11157 constp
&= TYPE_READONLY (to
);
11160 if (VECTOR_TYPE_P (to
))
11161 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
11163 /* P0388R4 allows a conversion from int[N] to int[] but not the
11164 other way round. When both arrays have bounds but they do
11165 not match, then no conversion is possible. */
11166 if (TREE_CODE (to
) == ARRAY_TYPE
11167 && !comp_array_types (to
, from
, bounds_first
, /*strict=*/false))
11170 if (!TYPE_PTR_P (to
)
11171 && !TYPE_PTRDATAMEM_P (to
)
11172 /* CWG 330 says we need to look through arrays. */
11173 && TREE_CODE (to
) != ARRAY_TYPE
)
11174 return ((constp
>= 0 || to_more_cv_qualified
)
11175 && (is_opaque_pointer
11176 || same_type_ignoring_top_level_qualifiers_p (to
, from
)));
11180 /* When comparing, say, char ** to char const **, this function takes
11181 the 'char *' and 'char const *'. Do not pass non-pointer/reference
11182 types to this function. */
11185 comp_ptr_ttypes (tree to
, tree from
)
11187 return comp_ptr_ttypes_real (to
, from
, 1);
11190 /* Returns true iff FNTYPE is a non-class type that involves
11191 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
11192 if a parameter type is ill-formed. */
11195 error_type_p (const_tree type
)
11199 switch (TREE_CODE (type
))
11205 case REFERENCE_TYPE
:
11207 return error_type_p (TREE_TYPE (type
));
11209 case FUNCTION_TYPE
:
11211 if (error_type_p (TREE_TYPE (type
)))
11213 for (t
= TYPE_ARG_TYPES (type
); t
; t
= TREE_CHAIN (t
))
11214 if (error_type_p (TREE_VALUE (t
)))
11219 if (TYPE_PTRMEMFUNC_P (type
))
11220 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type
));
11228 /* Returns true if to and from are (possibly multi-level) pointers to the same
11229 type or inheritance-related types, regardless of cv-quals. */
11232 ptr_reasonably_similar (const_tree to
, const_tree from
)
11234 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
11236 /* Any target type is similar enough to void. */
11237 if (VOID_TYPE_P (to
))
11238 return !error_type_p (from
);
11239 if (VOID_TYPE_P (from
))
11240 return !error_type_p (to
);
11242 if (TREE_CODE (to
) != TREE_CODE (from
))
11245 if (TREE_CODE (from
) == OFFSET_TYPE
11246 && comptypes (TYPE_OFFSET_BASETYPE (to
),
11247 TYPE_OFFSET_BASETYPE (from
),
11248 COMPARE_BASE
| COMPARE_DERIVED
))
11251 if (VECTOR_TYPE_P (to
)
11252 && vector_types_convertible_p (to
, from
, false))
11255 if (TREE_CODE (to
) == INTEGER_TYPE
11256 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
11259 if (TREE_CODE (to
) == FUNCTION_TYPE
)
11260 return !error_type_p (to
) && !error_type_p (from
);
11262 if (!TYPE_PTR_P (to
))
11264 /* When either type is incomplete avoid DERIVED_FROM_P,
11265 which may call complete_type (c++/57942). */
11266 bool b
= !COMPLETE_TYPE_P (to
) || !COMPLETE_TYPE_P (from
);
11268 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
11269 b
? COMPARE_STRICT
: COMPARE_BASE
| COMPARE_DERIVED
);
11274 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
11275 pointer-to-member types) are the same, ignoring cv-qualification at
11276 all levels. CB says how we should behave when comparing array bounds. */
11279 comp_ptr_ttypes_const (tree to
, tree from
, compare_bounds_t cb
)
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 if (VECTOR_TYPE_P (to
))
11294 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
11296 if (TREE_CODE (to
) == ARRAY_TYPE
11297 /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
11299 && !comp_array_types (to
, from
, cb
, /*strict=*/false))
11302 /* CWG 330 says we need to look through arrays. */
11303 if (!TYPE_PTR_P (to
) && TREE_CODE (to
) != ARRAY_TYPE
)
11304 return (is_opaque_pointer
11305 || same_type_ignoring_top_level_qualifiers_p (to
, from
));
11309 /* Returns the type qualifiers for this type, including the qualifiers on the
11310 elements for an array type. */
11313 cp_type_quals (const_tree type
)
11316 /* This CONST_CAST is okay because strip_array_types returns its
11317 argument unmodified and we assign it to a const_tree. */
11318 type
= strip_array_types (CONST_CAST_TREE (type
));
11319 if (type
== error_mark_node
11320 /* Quals on a FUNCTION_TYPE are memfn quals. */
11321 || TREE_CODE (type
) == FUNCTION_TYPE
)
11322 return TYPE_UNQUALIFIED
;
11323 quals
= TYPE_QUALS (type
);
11324 /* METHOD and REFERENCE_TYPEs should never have quals. */
11325 gcc_assert ((TREE_CODE (type
) != METHOD_TYPE
11326 && !TYPE_REF_P (type
))
11327 || ((quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
))
11328 == TYPE_UNQUALIFIED
));
11332 /* Returns the function-ref-qualifier for TYPE */
11335 type_memfn_rqual (const_tree type
)
11337 gcc_assert (FUNC_OR_METHOD_TYPE_P (type
));
11339 if (!FUNCTION_REF_QUALIFIED (type
))
11340 return REF_QUAL_NONE
;
11341 else if (FUNCTION_RVALUE_QUALIFIED (type
))
11342 return REF_QUAL_RVALUE
;
11344 return REF_QUAL_LVALUE
;
11347 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
11351 type_memfn_quals (const_tree type
)
11353 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11354 return TYPE_QUALS (type
);
11355 else if (TREE_CODE (type
) == METHOD_TYPE
)
11356 return cp_type_quals (class_of_this_parm (type
));
11358 gcc_unreachable ();
11361 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
11362 MEMFN_QUALS and its ref-qualifier to RQUAL. */
11365 apply_memfn_quals (tree type
, cp_cv_quals memfn_quals
, cp_ref_qualifier rqual
)
11367 /* Could handle METHOD_TYPE here if necessary. */
11368 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
11369 if (TYPE_QUALS (type
) == memfn_quals
11370 && type_memfn_rqual (type
) == rqual
)
11373 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
11375 tree result
= build_qualified_type (type
, memfn_quals
);
11376 return build_ref_qualified_type (result
, rqual
);
11379 /* Returns nonzero if TYPE is const or volatile. */
11382 cv_qualified_p (const_tree type
)
11384 int quals
= cp_type_quals (type
);
11385 return (quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)) != 0;
11388 /* Returns nonzero if the TYPE contains a mutable member. */
11391 cp_has_mutable_p (const_tree type
)
11393 /* This CONST_CAST is okay because strip_array_types returns its
11394 argument unmodified and we assign it to a const_tree. */
11395 type
= strip_array_types (CONST_CAST_TREE(type
));
11397 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
11400 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
11401 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
11402 approximation. In particular, consider:
11405 struct S { int i; };
11406 const S s = { f(); }
11408 Here, we will make "s" as TREE_READONLY (because it is declared
11409 "const") -- only to reverse ourselves upon seeing that the
11410 initializer is non-constant. */
11413 cp_apply_type_quals_to_decl (int type_quals
, tree decl
)
11415 tree type
= TREE_TYPE (decl
);
11417 if (type
== error_mark_node
)
11420 if (TREE_CODE (decl
) == TYPE_DECL
)
11423 gcc_assert (!(TREE_CODE (type
) == FUNCTION_TYPE
11424 && type_quals
!= TYPE_UNQUALIFIED
));
11426 /* Avoid setting TREE_READONLY incorrectly. */
11427 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
11428 constructor can produce constant init, so rely on cp_finish_decl to
11429 clear TREE_READONLY if the variable has non-constant init. */
11431 /* If the type has (or might have) a mutable component, that component
11432 might be modified. */
11433 if (TYPE_HAS_MUTABLE_P (type
) || !COMPLETE_TYPE_P (type
))
11434 type_quals
&= ~TYPE_QUAL_CONST
;
11436 c_apply_type_quals_to_decl (type_quals
, decl
);
11439 /* Subroutine of casts_away_constness. Make T1 and T2 point at
11440 exemplar types such that casting T1 to T2 is casting away constness
11441 if and only if there is no implicit conversion from T1 to T2. */
11444 casts_away_constness_r (tree
*t1
, tree
*t2
, tsubst_flags_t complain
)
11449 /* [expr.const.cast]
11451 For multi-level pointer to members and multi-level mixed pointers
11452 and pointers to members (conv.qual), the "member" aspect of a
11453 pointer to member level is ignored when determining if a const
11454 cv-qualifier has been cast away. */
11455 /* [expr.const.cast]
11457 For two pointer types:
11459 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
11460 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
11463 casting from X1 to X2 casts away constness if, for a non-pointer
11464 type T there does not exist an implicit conversion (clause
11467 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
11471 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
11472 if ((!TYPE_PTR_P (*t1
) && !TYPE_PTRDATAMEM_P (*t1
))
11473 || (!TYPE_PTR_P (*t2
) && !TYPE_PTRDATAMEM_P (*t2
)))
11475 *t1
= cp_build_qualified_type (void_type_node
,
11476 cp_type_quals (*t1
));
11477 *t2
= cp_build_qualified_type (void_type_node
,
11478 cp_type_quals (*t2
));
11482 quals1
= cp_type_quals (*t1
);
11483 quals2
= cp_type_quals (*t2
);
11485 if (TYPE_PTRDATAMEM_P (*t1
))
11486 *t1
= TYPE_PTRMEM_POINTED_TO_TYPE (*t1
);
11488 *t1
= TREE_TYPE (*t1
);
11489 if (TYPE_PTRDATAMEM_P (*t2
))
11490 *t2
= TYPE_PTRMEM_POINTED_TO_TYPE (*t2
);
11492 *t2
= TREE_TYPE (*t2
);
11494 casts_away_constness_r (t1
, t2
, complain
);
11495 *t1
= build_pointer_type (*t1
);
11496 *t2
= build_pointer_type (*t2
);
11497 *t1
= cp_build_qualified_type (*t1
, quals1
);
11498 *t2
= cp_build_qualified_type (*t2
, quals2
);
11501 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
11504 ??? This function returns non-zero if casting away qualifiers not
11505 just const. We would like to return to the caller exactly which
11506 qualifiers are casted away to give more accurate diagnostics.
11510 casts_away_constness (tree t1
, tree t2
, tsubst_flags_t complain
)
11512 if (TYPE_REF_P (t2
))
11514 /* [expr.const.cast]
11516 Casting from an lvalue of type T1 to an lvalue of type T2
11517 using a reference cast casts away constness if a cast from an
11518 rvalue of type "pointer to T1" to the type "pointer to T2"
11519 casts away constness. */
11520 t1
= (TYPE_REF_P (t1
) ? TREE_TYPE (t1
) : t1
);
11521 return casts_away_constness (build_pointer_type (t1
),
11522 build_pointer_type (TREE_TYPE (t2
)),
11526 if (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
11527 /* [expr.const.cast]
11529 Casting from an rvalue of type "pointer to data member of X
11530 of type T1" to the type "pointer to data member of Y of type
11531 T2" casts away constness if a cast from an rvalue of type
11532 "pointer to T1" to the type "pointer to T2" casts away
11534 return casts_away_constness
11535 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1
)),
11536 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2
)),
11539 /* Casting away constness is only something that makes sense for
11540 pointer or reference types. */
11541 if (!TYPE_PTR_P (t1
) || !TYPE_PTR_P (t2
))
11544 /* Top-level qualifiers don't matter. */
11545 t1
= TYPE_MAIN_VARIANT (t1
);
11546 t2
= TYPE_MAIN_VARIANT (t2
);
11547 casts_away_constness_r (&t1
, &t2
, complain
);
11548 if (!can_convert (t2
, t1
, complain
))
11554 /* If T is a REFERENCE_TYPE return the type to which T refers.
11555 Otherwise, return T itself. */
11558 non_reference (tree t
)
11560 if (t
&& TYPE_REF_P (t
))
11566 /* Return nonzero if REF is an lvalue valid for this language;
11567 otherwise, print an error message and return zero. USE says
11568 how the lvalue is being used and so selects the error message. */
11571 lvalue_or_else (tree ref
, enum lvalue_use use
, tsubst_flags_t complain
)
11573 cp_lvalue_kind kind
= lvalue_kind (ref
);
11575 if (kind
== clk_none
)
11577 if (complain
& tf_error
)
11578 lvalue_error (cp_expr_loc_or_input_loc (ref
), use
);
11581 else if (kind
& (clk_rvalueref
|clk_class
))
11583 if (!(complain
& tf_error
))
11585 /* Make this a permerror because we used to accept it. */
11586 permerror (cp_expr_loc_or_input_loc (ref
),
11587 "using rvalue as lvalue");
11592 /* Return true if a user-defined literal operator is a raw operator. */
11595 check_raw_literal_operator (const_tree decl
)
11597 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
11600 bool maybe_raw_p
= false;
11602 /* Count the number and type of arguments and check for ellipsis. */
11603 for (argtype
= argtypes
, arity
= 0;
11604 argtype
&& argtype
!= void_list_node
;
11605 ++arity
, argtype
= TREE_CHAIN (argtype
))
11607 tree t
= TREE_VALUE (argtype
);
11609 if (same_type_p (t
, const_string_type_node
))
11610 maybe_raw_p
= true;
11613 return false; /* Found ellipsis. */
11615 if (!maybe_raw_p
|| arity
!= 1)
11622 /* Return true if a user-defined literal operator has one of the allowed
11626 check_literal_operator_args (const_tree decl
,
11627 bool *long_long_unsigned_p
, bool *long_double_p
)
11629 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
11631 *long_long_unsigned_p
= false;
11632 *long_double_p
= false;
11633 if (processing_template_decl
|| processing_specialization
)
11634 return argtypes
== void_list_node
;
11641 /* Count the number and type of arguments and check for ellipsis. */
11642 for (argtype
= argtypes
, arity
= 0;
11643 argtype
&& argtype
!= void_list_node
;
11644 argtype
= TREE_CHAIN (argtype
))
11646 tree t
= TREE_VALUE (argtype
);
11649 if (TYPE_PTR_P (t
))
11651 bool maybe_raw_p
= false;
11653 if (cp_type_quals (t
) != TYPE_QUAL_CONST
)
11655 t
= TYPE_MAIN_VARIANT (t
);
11656 if ((maybe_raw_p
= same_type_p (t
, char_type_node
))
11657 || same_type_p (t
, wchar_type_node
)
11658 || same_type_p (t
, char8_type_node
)
11659 || same_type_p (t
, char16_type_node
)
11660 || same_type_p (t
, char32_type_node
))
11662 argtype
= TREE_CHAIN (argtype
);
11665 t
= TREE_VALUE (argtype
);
11666 if (maybe_raw_p
&& argtype
== void_list_node
)
11668 else if (same_type_p (t
, size_type_node
))
11677 else if (same_type_p (t
, long_long_unsigned_type_node
))
11680 *long_long_unsigned_p
= true;
11682 else if (same_type_p (t
, long_double_type_node
))
11685 *long_double_p
= true;
11687 else if (same_type_p (t
, char_type_node
))
11689 else if (same_type_p (t
, wchar_type_node
))
11691 else if (same_type_p (t
, char8_type_node
))
11693 else if (same_type_p (t
, char16_type_node
))
11695 else if (same_type_p (t
, char32_type_node
))
11701 return false; /* Found ellipsis. */
11703 if (arity
!= max_arity
)
11710 /* Always returns false since unlike C90, C++ has no concept of implicit
11711 function declarations. */
11714 c_decl_implicit (const_tree
)