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
)))
1624 case UNDERLYING_TYPE
:
1625 if (!same_type_p (UNDERLYING_TYPE_TYPE (t1
), UNDERLYING_TYPE_TYPE (t2
)))
1630 if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1
), TYPEOF_TYPE_EXPR (t2
)))
1638 /* If we get here, we know that from a target independent POV the
1639 types are the same. Make sure the target attributes are also
1641 if (!comp_type_attributes (t1
, t2
))
1645 if (comparing_dependent_aliases
)
1647 /* Don't treat an alias template specialization with dependent
1648 arguments as equivalent to its underlying type when used as a
1649 template argument; we need them to be distinct so that we
1650 substitute into the specialization arguments at instantiation
1651 time. And aliases can't be equivalent without being ==, so
1652 we don't need to look any deeper. */
1653 ++processing_template_decl
;
1654 tree dep1
= dependent_alias_template_spec_p (t1
, nt_transparent
);
1655 tree dep2
= dependent_alias_template_spec_p (t2
, nt_transparent
);
1656 --processing_template_decl
;
1657 if ((dep1
|| dep2
) && dep1
!= dep2
)
1664 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1665 is a bitwise-or of the COMPARE_* flags. */
1668 comptypes (tree t1
, tree t2
, int strict
)
1670 gcc_checking_assert (t1
&& t2
);
1672 /* TYPE_ARGUMENT_PACKS are not really types. */
1673 gcc_checking_assert (TREE_CODE (t1
) != TYPE_ARGUMENT_PACK
1674 && TREE_CODE (t2
) != TYPE_ARGUMENT_PACK
);
1679 /* Suppress errors caused by previously reported errors. */
1680 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1683 if (strict
== COMPARE_STRICT
)
1685 if (TYPE_STRUCTURAL_EQUALITY_P (t1
) || TYPE_STRUCTURAL_EQUALITY_P (t2
))
1686 /* At least one of the types requires structural equality, so
1687 perform a deep check. */
1688 return structural_comptypes (t1
, t2
, strict
);
1690 if (flag_checking
&& param_use_canonical_types
)
1692 bool result
= structural_comptypes (t1
, t2
, strict
);
1694 if (result
&& TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1695 /* The two types are structurally equivalent, but their
1696 canonical types were different. This is a failure of the
1697 canonical type propagation code.*/
1699 ("canonical types differ for identical types %qT and %qT",
1701 else if (!result
&& TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
))
1702 /* Two types are structurally different, but the canonical
1703 types are the same. This means we were over-eager in
1704 assigning canonical types. */
1706 ("same canonical type node for different types %qT and %qT",
1711 if (!flag_checking
&& param_use_canonical_types
)
1712 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1714 return structural_comptypes (t1
, t2
, strict
);
1716 else if (strict
== COMPARE_STRUCTURAL
)
1717 return structural_comptypes (t1
, t2
, COMPARE_STRICT
);
1719 return structural_comptypes (t1
, t2
, strict
);
1722 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1723 top-level qualifiers. */
1726 same_type_ignoring_top_level_qualifiers_p (tree type1
, tree type2
)
1728 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1733 type1
= cp_build_qualified_type (type1
, TYPE_UNQUALIFIED
);
1734 type2
= cp_build_qualified_type (type2
, TYPE_UNQUALIFIED
);
1735 return same_type_p (type1
, type2
);
1738 /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1741 similar_type_p (tree type1
, tree type2
)
1743 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1746 /* Informally, two types are similar if, ignoring top-level cv-qualification:
1747 * they are the same type; or
1748 * they are both pointers, and the pointed-to types are similar; or
1749 * they are both pointers to member of the same class, and the types of
1750 the pointed-to members are similar; or
1751 * they are both arrays of the same size or both arrays of unknown bound,
1752 and the array element types are similar. */
1754 if (same_type_ignoring_top_level_qualifiers_p (type1
, type2
))
1757 if ((TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
1758 || (TYPE_PTRDATAMEM_P (type1
) && TYPE_PTRDATAMEM_P (type2
))
1759 || (TREE_CODE (type1
) == ARRAY_TYPE
&& TREE_CODE (type2
) == ARRAY_TYPE
))
1760 return comp_ptr_ttypes_const (type1
, type2
, bounds_either
);
1765 /* Helper function for layout_compatible_type_p and
1766 is_corresponding_member_aggr. Advance to next members (NULL if
1767 no further ones) and return true if those members are still part of
1768 the common initial sequence. */
1771 next_common_initial_seqence (tree
&memb1
, tree
&memb2
)
1775 if (TREE_CODE (memb1
) != FIELD_DECL
1776 || (DECL_FIELD_IS_BASE (memb1
) && is_empty_field (memb1
)))
1778 memb1
= DECL_CHAIN (memb1
);
1781 if (DECL_FIELD_IS_BASE (memb1
))
1783 memb1
= TYPE_FIELDS (TREE_TYPE (memb1
));
1790 if (TREE_CODE (memb2
) != FIELD_DECL
1791 || (DECL_FIELD_IS_BASE (memb2
) && is_empty_field (memb2
)))
1793 memb2
= DECL_CHAIN (memb2
);
1796 if (DECL_FIELD_IS_BASE (memb2
))
1798 memb2
= TYPE_FIELDS (TREE_TYPE (memb2
));
1803 if (memb1
== NULL_TREE
&& memb2
== NULL_TREE
)
1805 if (memb1
== NULL_TREE
|| memb2
== NULL_TREE
)
1807 if (DECL_BIT_FIELD_TYPE (memb1
))
1809 if (!DECL_BIT_FIELD_TYPE (memb2
))
1811 if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1
),
1812 DECL_BIT_FIELD_TYPE (memb2
)))
1814 if (TYPE_PRECISION (TREE_TYPE (memb1
))
1815 != TYPE_PRECISION (TREE_TYPE (memb2
)))
1818 else if (DECL_BIT_FIELD_TYPE (memb2
))
1820 else if (!layout_compatible_type_p (TREE_TYPE (memb1
), TREE_TYPE (memb2
)))
1822 if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1
)))
1823 != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2
)))
1825 if (!tree_int_cst_equal (bit_position (memb1
), bit_position (memb2
)))
1830 /* Return true if TYPE1 and TYPE2 are layout-compatible types. */
1833 layout_compatible_type_p (tree type1
, tree type2
)
1835 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1839 if (TREE_CODE (type1
) != TREE_CODE (type2
))
1842 type1
= cp_build_qualified_type (type1
, TYPE_UNQUALIFIED
);
1843 type2
= cp_build_qualified_type (type2
, TYPE_UNQUALIFIED
);
1845 if (TREE_CODE (type1
) == ENUMERAL_TYPE
)
1846 return (TYPE_ALIGN (type1
) == TYPE_ALIGN (type2
)
1847 && tree_int_cst_equal (TYPE_SIZE (type1
), TYPE_SIZE (type2
))
1848 && same_type_p (finish_underlying_type (type1
),
1849 finish_underlying_type (type2
)));
1851 if (CLASS_TYPE_P (type1
)
1852 && std_layout_type_p (type1
)
1853 && std_layout_type_p (type2
)
1854 && TYPE_ALIGN (type1
) == TYPE_ALIGN (type2
)
1855 && tree_int_cst_equal (TYPE_SIZE (type1
), TYPE_SIZE (type2
)))
1857 tree field1
= TYPE_FIELDS (type1
);
1858 tree field2
= TYPE_FIELDS (type2
);
1859 if (TREE_CODE (type1
) == RECORD_TYPE
)
1863 if (!next_common_initial_seqence (field1
, field2
))
1865 if (field1
== NULL_TREE
)
1867 field1
= DECL_CHAIN (field1
);
1868 field2
= DECL_CHAIN (field2
);
1871 /* Otherwise both types must be union types.
1873 "Two standard-layout unions are layout-compatible if they have
1874 the same number of non-static data members and corresponding
1875 non-static data members (in any order) have layout-compatible
1877 but the code anticipates that bitfield vs. non-bitfield,
1878 different bitfield widths or presence/absence of
1879 [[no_unique_address]] should be checked as well. */
1880 auto_vec
<tree
, 16> vec
;
1881 unsigned int count
= 0;
1882 for (; field1
; field1
= DECL_CHAIN (field1
))
1883 if (TREE_CODE (field1
) == FIELD_DECL
)
1885 for (; field2
; field2
= DECL_CHAIN (field2
))
1886 if (TREE_CODE (field2
) == FIELD_DECL
)
1887 vec
.safe_push (field2
);
1888 /* Discussions on core lean towards treating multiple union fields
1889 of the same type as the same field, so this might need changing
1891 if (count
!= vec
.length ())
1893 for (field1
= TYPE_FIELDS (type1
); field1
; field1
= DECL_CHAIN (field1
))
1895 if (TREE_CODE (field1
) != FIELD_DECL
)
1898 tree t1
= DECL_BIT_FIELD_TYPE (field1
);
1899 if (t1
== NULL_TREE
)
1900 t1
= TREE_TYPE (field1
);
1901 FOR_EACH_VEC_ELT (vec
, j
, field2
)
1903 tree t2
= DECL_BIT_FIELD_TYPE (field2
);
1904 if (t2
== NULL_TREE
)
1905 t2
= TREE_TYPE (field2
);
1906 if (DECL_BIT_FIELD_TYPE (field1
))
1908 if (!DECL_BIT_FIELD_TYPE (field2
))
1910 if (TYPE_PRECISION (TREE_TYPE (field1
))
1911 != TYPE_PRECISION (TREE_TYPE (field2
)))
1914 else if (DECL_BIT_FIELD_TYPE (field2
))
1916 if (!layout_compatible_type_p (t1
, t2
))
1918 if ((!lookup_attribute ("no_unique_address",
1919 DECL_ATTRIBUTES (field1
)))
1920 != !lookup_attribute ("no_unique_address",
1921 DECL_ATTRIBUTES (field2
)))
1925 if (j
== vec
.length ())
1927 vec
.unordered_remove (j
);
1932 return same_type_p (type1
, type2
);
1935 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1938 at_least_as_qualified_p (const_tree type1
, const_tree type2
)
1940 int q1
= cp_type_quals (type1
);
1941 int q2
= cp_type_quals (type2
);
1943 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1944 return (q1
& q2
) == q2
;
1947 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1948 more cv-qualified that TYPE1, and 0 otherwise. */
1951 comp_cv_qualification (int q1
, int q2
)
1956 if ((q1
& q2
) == q2
)
1958 else if ((q1
& q2
) == q1
)
1965 comp_cv_qualification (const_tree type1
, const_tree type2
)
1967 int q1
= cp_type_quals (type1
);
1968 int q2
= cp_type_quals (type2
);
1969 return comp_cv_qualification (q1
, q2
);
1972 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1973 subset of the cv-qualification signature of TYPE2, and the types
1974 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1977 comp_cv_qual_signature (tree type1
, tree type2
)
1979 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1981 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1987 /* Subroutines of `comptypes'. */
1989 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1990 equivalent in the sense that functions with those parameter types
1991 can have equivalent types. The two lists must be equivalent,
1992 element by element. */
1995 compparms (const_tree parms1
, const_tree parms2
)
1999 /* An unspecified parmlist matches any specified parmlist
2000 whose argument types don't need default promotions. */
2002 for (t1
= parms1
, t2
= parms2
;
2004 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
2006 /* If one parmlist is shorter than the other,
2007 they fail to match. */
2010 if (!same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
2017 /* Process a sizeof or alignof expression where the operand is a type.
2018 STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
2019 or GNU (preferred alignment) semantics; it is ignored if OP is
2023 cxx_sizeof_or_alignof_type (location_t loc
, tree type
, enum tree_code op
,
2024 bool std_alignof
, bool complain
)
2026 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
2027 if (type
== error_mark_node
)
2028 return error_mark_node
;
2030 type
= non_reference (type
);
2031 if (TREE_CODE (type
) == METHOD_TYPE
)
2035 pedwarn (loc
, OPT_Wpointer_arith
,
2036 "invalid application of %qs to a member function",
2037 OVL_OP_INFO (false, op
)->name
);
2038 return size_one_node
;
2041 return error_mark_node
;
2043 else if (VOID_TYPE_P (type
) && std_alignof
)
2046 error_at (loc
, "invalid application of %qs to a void type",
2047 OVL_OP_INFO (false, op
)->name
);
2048 return error_mark_node
;
2051 bool dependent_p
= dependent_type_p (type
);
2053 complete_type (type
);
2055 /* VLA types will have a non-constant size. In the body of an
2056 uninstantiated template, we don't need to try to compute the
2057 value, because the sizeof expression is not an integral
2058 constant expression in that case. And, if we do try to
2059 compute the value, we'll likely end up with SAVE_EXPRs, which
2060 the template substitution machinery does not expect to see. */
2061 || (processing_template_decl
2062 && COMPLETE_TYPE_P (type
)
2063 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
))
2065 tree value
= build_min (op
, size_type_node
, type
);
2066 TREE_READONLY (value
) = 1;
2067 if (op
== ALIGNOF_EXPR
&& std_alignof
)
2068 ALIGNOF_EXPR_STD_P (value
) = true;
2069 SET_EXPR_LOCATION (value
, loc
);
2073 return c_sizeof_or_alignof_type (loc
, complete_type (type
),
2074 op
== SIZEOF_EXPR
, std_alignof
,
2078 /* Return the size of the type, without producing any warnings for
2079 types whose size cannot be taken. This routine should be used only
2080 in some other routine that has already produced a diagnostic about
2081 using the size of such a type. */
2083 cxx_sizeof_nowarn (tree type
)
2085 if (TREE_CODE (type
) == FUNCTION_TYPE
2086 || VOID_TYPE_P (type
)
2087 || TREE_CODE (type
) == ERROR_MARK
)
2088 return size_one_node
;
2089 else if (!COMPLETE_TYPE_P (type
))
2090 return size_zero_node
;
2092 return cxx_sizeof_or_alignof_type (input_location
, type
,
2093 SIZEOF_EXPR
, false, false);
2096 /* Process a sizeof expression where the operand is an expression. */
2099 cxx_sizeof_expr (location_t loc
, tree e
, tsubst_flags_t complain
)
2101 if (e
== error_mark_node
)
2102 return error_mark_node
;
2104 if (instantiation_dependent_uneval_expression_p (e
))
2106 e
= build_min (SIZEOF_EXPR
, size_type_node
, e
);
2107 TREE_SIDE_EFFECTS (e
) = 0;
2108 TREE_READONLY (e
) = 1;
2109 SET_EXPR_LOCATION (e
, loc
);
2114 location_t e_loc
= cp_expr_loc_or_loc (e
, loc
);
2115 STRIP_ANY_LOCATION_WRAPPER (e
);
2117 /* To get the size of a static data member declared as an array of
2118 unknown bound, we need to instantiate it. */
2120 && VAR_HAD_UNKNOWN_BOUND (e
)
2121 && DECL_TEMPLATE_INSTANTIATION (e
))
2122 instantiate_decl (e
, /*defer_ok*/true, /*expl_inst_mem*/false);
2124 if (TREE_CODE (e
) == PARM_DECL
2125 && DECL_ARRAY_PARAMETER_P (e
)
2126 && (complain
& tf_warning
))
2128 auto_diagnostic_group d
;
2129 if (warning_at (e_loc
, OPT_Wsizeof_array_argument
,
2130 "%<sizeof%> on array function parameter %qE "
2131 "will return size of %qT", e
, TREE_TYPE (e
)))
2132 inform (DECL_SOURCE_LOCATION (e
), "declared here");
2135 e
= mark_type_use (e
);
2139 if (complain
& tf_error
)
2141 "invalid application of %<sizeof%> to a bit-field");
2143 return error_mark_node
;
2146 else if (is_overloaded_fn (e
))
2148 if (complain
& tf_error
)
2149 permerror (e_loc
, "ISO C++ forbids applying %<sizeof%> to "
2150 "an expression of function type");
2152 return error_mark_node
;
2155 else if (type_unknown_p (e
))
2157 if (complain
& tf_error
)
2158 cxx_incomplete_type_error (e_loc
, e
, TREE_TYPE (e
));
2160 return error_mark_node
;
2166 return cxx_sizeof_or_alignof_type (loc
, e
, SIZEOF_EXPR
, false,
2167 complain
& tf_error
);
2170 /* Implement the __alignof keyword: Return the minimum required
2171 alignment of E, measured in bytes. For VAR_DECL's and
2172 FIELD_DECL's return DECL_ALIGN (which can be set from an
2173 "aligned" __attribute__ specification). STD_ALIGNOF acts
2174 like in cxx_sizeof_or_alignof_type. */
2177 cxx_alignof_expr (location_t loc
, tree e
, bool std_alignof
,
2178 tsubst_flags_t complain
)
2182 if (e
== error_mark_node
)
2183 return error_mark_node
;
2185 if (processing_template_decl
)
2187 e
= build_min (ALIGNOF_EXPR
, size_type_node
, e
);
2188 TREE_SIDE_EFFECTS (e
) = 0;
2189 TREE_READONLY (e
) = 1;
2190 SET_EXPR_LOCATION (e
, loc
);
2191 ALIGNOF_EXPR_STD_P (e
) = std_alignof
;
2196 location_t e_loc
= cp_expr_loc_or_loc (e
, loc
);
2197 STRIP_ANY_LOCATION_WRAPPER (e
);
2199 e
= mark_type_use (e
);
2201 if (!verify_type_context (loc
, TCTX_ALIGNOF
, TREE_TYPE (e
),
2202 !(complain
& tf_error
)))
2204 if (!(complain
& tf_error
))
2205 return error_mark_node
;
2209 t
= size_int (DECL_ALIGN_UNIT (e
));
2210 else if (bitfield_p (e
))
2212 if (complain
& tf_error
)
2214 "invalid application of %<__alignof%> to a bit-field");
2216 return error_mark_node
;
2219 else if (TREE_CODE (e
) == COMPONENT_REF
2220 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
)
2221 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e
, 1)));
2222 else if (is_overloaded_fn (e
))
2224 if (complain
& tf_error
)
2225 permerror (e_loc
, "ISO C++ forbids applying %<__alignof%> to "
2226 "an expression of function type");
2228 return error_mark_node
;
2229 if (TREE_CODE (e
) == FUNCTION_DECL
)
2230 t
= size_int (DECL_ALIGN_UNIT (e
));
2234 else if (type_unknown_p (e
))
2236 if (complain
& tf_error
)
2237 cxx_incomplete_type_error (e_loc
, e
, TREE_TYPE (e
));
2239 return error_mark_node
;
2243 return cxx_sizeof_or_alignof_type (loc
, TREE_TYPE (e
),
2244 ALIGNOF_EXPR
, std_alignof
,
2245 complain
& tf_error
);
2247 return fold_convert_loc (loc
, size_type_node
, t
);
2250 /* Process a sizeof or alignof expression E with code OP where the operand
2251 is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2254 cxx_sizeof_or_alignof_expr (location_t loc
, tree e
, enum tree_code op
,
2255 bool std_alignof
, bool complain
)
2257 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
2258 if (op
== SIZEOF_EXPR
)
2259 return cxx_sizeof_expr (loc
, e
, complain
? tf_warning_or_error
: tf_none
);
2261 return cxx_alignof_expr (loc
, e
, std_alignof
,
2262 complain
? tf_warning_or_error
: tf_none
);
2265 /* Build a representation of an expression 'alignas(E).' Return the
2266 folded integer value of E if it is an integral constant expression
2267 that resolves to a valid alignment. If E depends on a template
2268 parameter, return a syntactic representation tree of kind
2269 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2270 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2273 cxx_alignas_expr (tree e
)
2275 if (e
== NULL_TREE
|| e
== error_mark_node
2276 || (!TYPE_P (e
) && !require_potential_rvalue_constant_expression (e
)))
2282 When the alignment-specifier is of the form
2283 alignas(type-id), it shall have the same effect as
2284 alignas(alignof(type-id)). */
2286 return cxx_sizeof_or_alignof_type (input_location
,
2288 /*std_alignof=*/true,
2291 /* If we reach this point, it means the alignas expression if of
2292 the form "alignas(assignment-expression)", so we should follow
2293 what is stated by [dcl.align]/2. */
2295 if (value_dependent_expression_p (e
))
2296 /* Leave value-dependent expression alone for now. */
2299 e
= instantiate_non_dependent_expr (e
);
2300 e
= mark_rvalue_use (e
);
2302 /* [dcl.align]/2 says:
2304 the assignment-expression shall be an integral constant
2307 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e
)))
2309 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e
));
2310 return error_mark_node
;
2313 return cxx_constant_value (e
);
2317 /* EXPR is being used in a context that is not a function call.
2322 The expression can be used only as the left-hand operand of a
2323 member function call.
2325 [expr.mptr.operator]
2327 If the result of .* or ->* is a function, then that result can be
2328 used only as the operand for the function call operator ().
2330 by issuing an error message if appropriate. Returns true iff EXPR
2331 violates these rules. */
2334 invalid_nonstatic_memfn_p (location_t loc
, tree expr
, tsubst_flags_t complain
)
2336 if (expr
== NULL_TREE
)
2338 /* Don't enforce this in MS mode. */
2339 if (flag_ms_extensions
)
2341 if (is_overloaded_fn (expr
) && !really_overloaded_fn (expr
))
2342 expr
= get_first_fn (expr
);
2343 if (TREE_TYPE (expr
)
2344 && DECL_NONSTATIC_MEMBER_FUNCTION_P (expr
))
2346 if (complain
& tf_error
)
2350 error_at (loc
, "invalid use of non-static member function %qD",
2352 inform (DECL_SOURCE_LOCATION (expr
), "declared here");
2355 error_at (loc
, "invalid use of non-static member function of "
2356 "type %qT", TREE_TYPE (expr
));
2363 /* If EXP is a reference to a bit-field, and the type of EXP does not
2364 match the declared type of the bit-field, return the declared type
2365 of the bit-field. Otherwise, return NULL_TREE. */
2368 is_bitfield_expr_with_lowered_type (const_tree exp
)
2370 switch (TREE_CODE (exp
))
2373 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1)
2374 ? TREE_OPERAND (exp
, 1)
2375 : TREE_OPERAND (exp
, 0)))
2377 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 2));
2380 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1));
2384 case UNARY_PLUS_EXPR
:
2385 case PREDECREMENT_EXPR
:
2386 case PREINCREMENT_EXPR
:
2387 case POSTDECREMENT_EXPR
:
2388 case POSTINCREMENT_EXPR
:
2390 case NON_LVALUE_EXPR
:
2392 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
2398 field
= TREE_OPERAND (exp
, 1);
2399 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_BIT_FIELD_TYPE (field
))
2401 if (same_type_ignoring_top_level_qualifiers_p
2402 (TREE_TYPE (exp
), DECL_BIT_FIELD_TYPE (field
)))
2404 return DECL_BIT_FIELD_TYPE (field
);
2408 if (DECL_HAS_VALUE_EXPR_P (exp
))
2409 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2410 (CONST_CAST_TREE (exp
)));
2413 case VIEW_CONVERT_EXPR
:
2414 if (location_wrapper_p (exp
))
2415 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
2424 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2425 bitfield with a lowered type, the type of EXP is returned, rather
2429 unlowered_expr_type (const_tree exp
)
2432 tree etype
= TREE_TYPE (exp
);
2434 type
= is_bitfield_expr_with_lowered_type (exp
);
2436 type
= cp_build_qualified_type (type
, cp_type_quals (etype
));
2443 /* Perform the conversions in [expr] that apply when an lvalue appears
2444 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2445 function-to-pointer conversions. In addition, bitfield references are
2446 converted to their declared types. Note that this function does not perform
2447 the lvalue-to-rvalue conversion for class types. If you need that conversion
2448 for class types, then you probably need to use force_rvalue.
2450 Although the returned value is being used as an rvalue, this
2451 function does not wrap the returned expression in a
2452 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2453 that the return value is no longer an lvalue. */
2456 decay_conversion (tree exp
,
2457 tsubst_flags_t complain
,
2458 bool reject_builtin
/* = true */)
2461 enum tree_code code
;
2462 location_t loc
= cp_expr_loc_or_input_loc (exp
);
2464 type
= TREE_TYPE (exp
);
2465 if (type
== error_mark_node
)
2466 return error_mark_node
;
2468 exp
= resolve_nondeduced_context_or_error (exp
, complain
);
2470 code
= TREE_CODE (type
);
2472 if (error_operand_p (exp
))
2473 return error_mark_node
;
2475 if (NULLPTR_TYPE_P (type
) && !TREE_SIDE_EFFECTS (exp
))
2477 mark_rvalue_use (exp
, loc
, reject_builtin
);
2478 return nullptr_node
;
2481 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2482 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2483 if (code
== VOID_TYPE
)
2485 if (complain
& tf_error
)
2486 error_at (loc
, "void value not ignored as it ought to be");
2487 return error_mark_node
;
2489 if (invalid_nonstatic_memfn_p (loc
, exp
, complain
))
2490 return error_mark_node
;
2491 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
2493 exp
= mark_lvalue_use (exp
);
2494 if (reject_builtin
&& reject_gcc_builtin (exp
, loc
))
2495 return error_mark_node
;
2496 return cp_build_addr_expr (exp
, complain
);
2498 if (code
== ARRAY_TYPE
)
2503 exp
= mark_lvalue_use (exp
);
2505 if (INDIRECT_REF_P (exp
))
2506 return build_nop (build_pointer_type (TREE_TYPE (type
)),
2507 TREE_OPERAND (exp
, 0));
2509 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
2511 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1), complain
);
2512 if (op1
== error_mark_node
)
2513 return error_mark_node
;
2514 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
2515 TREE_OPERAND (exp
, 0), op1
);
2518 if (!obvalue_p (exp
)
2519 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
2521 if (complain
& tf_error
)
2522 error_at (loc
, "invalid use of non-lvalue array");
2523 return error_mark_node
;
2526 /* Don't let an array compound literal decay to a pointer. It can
2527 still be used to initialize an array or bind to a reference. */
2528 if (TREE_CODE (exp
) == TARGET_EXPR
)
2530 if (complain
& tf_error
)
2531 error_at (loc
, "taking address of temporary array");
2532 return error_mark_node
;
2535 ptrtype
= build_pointer_type (TREE_TYPE (type
));
2539 if (!cxx_mark_addressable (exp
))
2540 return error_mark_node
;
2541 adr
= build_nop (ptrtype
, build_address (exp
));
2544 /* This way is better for a COMPONENT_REF since it can
2545 simplify the offset for a component. */
2546 adr
= cp_build_addr_expr (exp
, complain
);
2547 return cp_convert (ptrtype
, adr
, complain
);
2550 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2551 exp
= mark_rvalue_use (exp
, loc
, reject_builtin
);
2553 /* If a bitfield is used in a context where integral promotion
2554 applies, then the caller is expected to have used
2555 default_conversion. That function promotes bitfields correctly
2556 before calling this function. At this point, if we have a
2557 bitfield referenced, we may assume that is not subject to
2558 promotion, and that, therefore, the type of the resulting rvalue
2559 is the declared type of the bitfield. */
2560 exp
= convert_bitfield_to_declared_type (exp
);
2562 /* We do not call rvalue() here because we do not want to wrap EXP
2563 in a NON_LVALUE_EXPR. */
2567 Non-class rvalues always have cv-unqualified types. */
2568 type
= TREE_TYPE (exp
);
2569 if (!CLASS_TYPE_P (type
) && cv_qualified_p (type
))
2570 exp
= build_nop (cv_unqualified (type
), exp
);
2572 if (!complete_type_or_maybe_complain (type
, exp
, complain
))
2573 return error_mark_node
;
2578 /* Perform preparatory conversions, as part of the "usual arithmetic
2579 conversions". In particular, as per [expr]:
2581 Whenever an lvalue expression appears as an operand of an
2582 operator that expects the rvalue for that operand, the
2583 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2584 standard conversions are applied to convert the expression to an
2587 In addition, we perform integral promotions here, as those are
2588 applied to both operands to a binary operator before determining
2589 what additional conversions should apply. */
2592 cp_default_conversion (tree exp
, tsubst_flags_t complain
)
2594 /* Check for target-specific promotions. */
2595 tree promoted_type
= targetm
.promoted_type (TREE_TYPE (exp
));
2597 exp
= cp_convert (promoted_type
, exp
, complain
);
2598 /* Perform the integral promotions first so that bitfield
2599 expressions (which may promote to "int", even if the bitfield is
2600 declared "unsigned") are promoted correctly. */
2601 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp
)))
2602 exp
= cp_perform_integral_promotions (exp
, complain
);
2603 /* Perform the other conversions. */
2604 exp
= decay_conversion (exp
, complain
);
2612 default_conversion (tree exp
)
2614 return cp_default_conversion (exp
, tf_warning_or_error
);
2617 /* EXPR is an expression with an integral or enumeration type.
2618 Perform the integral promotions in [conv.prom], and return the
2622 cp_perform_integral_promotions (tree expr
, tsubst_flags_t complain
)
2627 expr
= mark_rvalue_use (expr
);
2628 if (error_operand_p (expr
))
2629 return error_mark_node
;
2631 type
= TREE_TYPE (expr
);
2635 A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2636 of type int if int can represent all the values of the bit-field;
2637 otherwise, it can be converted to unsigned int if unsigned int can
2638 represent all the values of the bit-field. If the bit-field is larger yet,
2639 no integral promotion applies to it. If the bit-field has an enumerated
2640 type, it is treated as any other value of that type for promotion
2642 tree bitfield_type
= is_bitfield_expr_with_lowered_type (expr
);
2644 && (TREE_CODE (bitfield_type
) == ENUMERAL_TYPE
2645 || TYPE_PRECISION (type
) > TYPE_PRECISION (integer_type_node
)))
2646 type
= bitfield_type
;
2648 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
2649 /* Scoped enums don't promote. */
2650 if (SCOPED_ENUM_P (type
))
2652 promoted_type
= type_promotes_to (type
);
2653 if (type
!= promoted_type
)
2654 expr
= cp_convert (promoted_type
, expr
, complain
);
2655 else if (bitfield_type
&& bitfield_type
!= type
)
2656 /* Prevent decay_conversion from converting to bitfield_type. */
2657 expr
= build_nop (type
, expr
);
2664 perform_integral_promotions (tree expr
)
2666 return cp_perform_integral_promotions (expr
, tf_warning_or_error
);
2669 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2670 decay_conversion to one. */
2673 string_conv_p (const_tree totype
, const_tree exp
, int warn
)
2677 if (!TYPE_PTR_P (totype
))
2680 t
= TREE_TYPE (totype
);
2681 if (!same_type_p (t
, char_type_node
)
2682 && !same_type_p (t
, char8_type_node
)
2683 && !same_type_p (t
, char16_type_node
)
2684 && !same_type_p (t
, char32_type_node
)
2685 && !same_type_p (t
, wchar_type_node
))
2688 location_t loc
= EXPR_LOC_OR_LOC (exp
, input_location
);
2690 STRIP_ANY_LOCATION_WRAPPER (exp
);
2692 if (TREE_CODE (exp
) == STRING_CST
)
2694 /* Make sure that we don't try to convert between char and wide chars. */
2695 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
2700 /* Is this a string constant which has decayed to 'const char *'? */
2701 t
= build_pointer_type (cp_build_qualified_type (t
, TYPE_QUAL_CONST
));
2702 if (!same_type_p (TREE_TYPE (exp
), t
))
2705 if (TREE_CODE (exp
) != ADDR_EXPR
2706 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
2711 if (cxx_dialect
>= cxx11
)
2712 pedwarn (loc
, OPT_Wwrite_strings
,
2713 "ISO C++ forbids converting a string constant to %qT",
2716 warning_at (loc
, OPT_Wwrite_strings
,
2717 "deprecated conversion from string constant to %qT",
2724 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2725 can, for example, use as an lvalue. This code used to be in
2726 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2727 expressions, where we're dealing with aggregates. But now it's again only
2728 called from unary_complex_lvalue. The case (in particular) that led to
2729 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2733 rationalize_conditional_expr (enum tree_code code
, tree t
,
2734 tsubst_flags_t complain
)
2736 location_t loc
= cp_expr_loc_or_input_loc (t
);
2738 /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that
2739 the first operand is always the one to be used if both operands
2740 are equal, so we know what conditional expression this used to be. */
2741 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
2743 tree op0
= TREE_OPERAND (t
, 0);
2744 tree op1
= TREE_OPERAND (t
, 1);
2746 /* The following code is incorrect if either operand side-effects. */
2747 gcc_assert (!TREE_SIDE_EFFECTS (op0
)
2748 && !TREE_SIDE_EFFECTS (op1
));
2750 build_conditional_expr (loc
,
2751 build_x_binary_op (loc
,
2752 (TREE_CODE (t
) == MIN_EXPR
2753 ? LE_EXPR
: GE_EXPR
),
2754 op0
, TREE_CODE (op0
),
2755 op1
, TREE_CODE (op1
),
2759 cp_build_unary_op (code
, op0
, false, complain
),
2760 cp_build_unary_op (code
, op1
, false, complain
),
2764 tree op1
= TREE_OPERAND (t
, 1);
2765 if (TREE_CODE (op1
) != THROW_EXPR
)
2766 op1
= cp_build_unary_op (code
, op1
, false, complain
);
2767 tree op2
= TREE_OPERAND (t
, 2);
2768 if (TREE_CODE (op2
) != THROW_EXPR
)
2769 op2
= cp_build_unary_op (code
, op2
, false, complain
);
2772 build_conditional_expr (loc
, TREE_OPERAND (t
, 0), op1
, op2
, complain
);
2775 /* Given the TYPE of an anonymous union field inside T, return the
2776 FIELD_DECL for the field. If not found return NULL_TREE. Because
2777 anonymous unions can nest, we must also search all anonymous unions
2778 that are directly reachable. */
2781 lookup_anon_field (tree
, tree type
)
2785 type
= TYPE_MAIN_VARIANT (type
);
2786 field
= ANON_AGGR_TYPE_FIELD (type
);
2791 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2792 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2793 non-NULL, it indicates the path to the base used to name MEMBER.
2794 If PRESERVE_REFERENCE is true, the expression returned will have
2795 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2796 returned will have the type referred to by the reference.
2798 This function does not perform access control; that is either done
2799 earlier by the parser when the name of MEMBER is resolved to MEMBER
2800 itself, or later when overload resolution selects one of the
2801 functions indicated by MEMBER. */
2804 build_class_member_access_expr (cp_expr object
, tree member
,
2805 tree access_path
, bool preserve_reference
,
2806 tsubst_flags_t complain
)
2810 tree result
= NULL_TREE
;
2811 tree using_decl
= NULL_TREE
;
2813 if (error_operand_p (object
) || error_operand_p (member
))
2814 return error_mark_node
;
2816 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2820 The type of the first expression shall be "class object" (of a
2822 object_type
= TREE_TYPE (object
);
2823 if (!currently_open_class (object_type
)
2824 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2825 return error_mark_node
;
2826 if (!CLASS_TYPE_P (object_type
))
2828 if (complain
& tf_error
)
2830 if (INDIRECT_TYPE_P (object_type
)
2831 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2832 error ("request for member %qD in %qE, which is of pointer "
2833 "type %qT (maybe you meant to use %<->%> ?)",
2834 member
, object
.get_value (), object_type
);
2836 error ("request for member %qD in %qE, which is of non-class "
2837 "type %qT", member
, object
.get_value (), object_type
);
2839 return error_mark_node
;
2842 /* The standard does not seem to actually say that MEMBER must be a
2843 member of OBJECT_TYPE. However, that is clearly what is
2845 if (DECL_P (member
))
2847 member_scope
= DECL_CLASS_CONTEXT (member
);
2848 if (!mark_used (member
, complain
) && !(complain
& tf_error
))
2849 return error_mark_node
;
2851 if (TREE_UNAVAILABLE (member
))
2852 error_unavailable_use (member
, NULL_TREE
);
2853 else if (TREE_DEPRECATED (member
))
2854 warn_deprecated_use (member
, NULL_TREE
);
2857 member_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (member
));
2858 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2859 presently be the anonymous union. Go outwards until we find a
2860 type related to OBJECT_TYPE. */
2861 while ((ANON_AGGR_TYPE_P (member_scope
) || UNSCOPED_ENUM_P (member_scope
))
2862 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
2864 member_scope
= TYPE_CONTEXT (member_scope
);
2865 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
2867 if (complain
& tf_error
)
2869 if (TREE_CODE (member
) == FIELD_DECL
)
2870 error ("invalid use of non-static data member %qE", member
);
2872 error ("%qD is not a member of %qT", member
, object_type
);
2874 return error_mark_node
;
2877 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2878 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2879 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2880 if (tree temp
= unary_complex_lvalue (ADDR_EXPR
, object
))
2882 temp
= cp_build_fold_indirect_ref (temp
);
2883 if (!lvalue_p (object
) && lvalue_p (temp
))
2884 /* Preserve rvalueness. */
2889 /* In [expr.ref], there is an explicit list of the valid choices for
2890 MEMBER. We check for each of those cases here. */
2893 /* A static data member. */
2895 mark_exp_read (object
);
2897 if (tree wrap
= maybe_get_tls_wrapper_call (result
))
2898 /* Replace an evaluated use of the thread_local variable with
2899 a call to its wrapper. */
2902 /* If OBJECT has side-effects, they are supposed to occur. */
2903 if (TREE_SIDE_EFFECTS (object
))
2904 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
2906 else if (TREE_CODE (member
) == FIELD_DECL
)
2908 /* A non-static data member. */
2913 if (INDIRECT_REF_P (object
))
2915 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object
, 0)));
2917 null_object_p
= false;
2919 /* Convert OBJECT to the type of MEMBER. */
2920 if (!same_type_p (TYPE_MAIN_VARIANT (object_type
),
2921 TYPE_MAIN_VARIANT (member_scope
)))
2926 /* We didn't complain above about a currently open class, but now we
2927 must: we don't know how to refer to a base member before layout is
2928 complete. But still don't complain in a template. */
2929 if (!cp_unevaluated_operand
2930 && !dependent_type_p (object_type
)
2931 && !complete_type_or_maybe_complain (object_type
, object
,
2933 return error_mark_node
;
2935 binfo
= lookup_base (access_path
? access_path
: object_type
,
2936 member_scope
, ba_unique
, &kind
, complain
);
2937 if (binfo
== error_mark_node
)
2938 return error_mark_node
;
2940 /* It is invalid to try to get to a virtual base of a
2941 NULL object. The most common cause is invalid use of
2943 if (null_object_p
&& kind
== bk_via_virtual
)
2945 if (complain
& tf_error
)
2947 error ("invalid access to non-static data member %qD in "
2948 "virtual base of NULL object", member
);
2950 return error_mark_node
;
2953 /* Convert to the base. */
2954 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
2955 /*nonnull=*/1, complain
);
2956 /* If we found the base successfully then we should be able
2957 to convert to it successfully. */
2958 gcc_assert (object
!= error_mark_node
);
2961 /* If MEMBER is from an anonymous aggregate, we have converted
2962 OBJECT so that it refers to the class containing the
2963 anonymous union. Generate a reference to the anonymous union
2964 itself, and recur to find MEMBER. */
2965 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
))
2966 /* When this code is called from build_field_call, the
2967 object already has the type of the anonymous union.
2968 That is because the COMPONENT_REF was already
2969 constructed, and was then disassembled before calling
2970 build_field_call. After the function-call code is
2971 cleaned up, this waste can be eliminated. */
2972 && (!same_type_ignoring_top_level_qualifiers_p
2973 (TREE_TYPE (object
), DECL_CONTEXT (member
))))
2975 tree anonymous_union
;
2977 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
2978 DECL_CONTEXT (member
));
2979 object
= build_class_member_access_expr (object
,
2981 /*access_path=*/NULL_TREE
,
2986 /* Compute the type of the field, as described in [expr.ref]. */
2987 type_quals
= TYPE_UNQUALIFIED
;
2988 member_type
= TREE_TYPE (member
);
2989 if (!TYPE_REF_P (member_type
))
2991 type_quals
= (cp_type_quals (member_type
)
2992 | cp_type_quals (object_type
));
2994 /* A field is const (volatile) if the enclosing object, or the
2995 field itself, is const (volatile). But, a mutable field is
2996 not const, even within a const object. */
2997 if (DECL_MUTABLE_P (member
))
2998 type_quals
&= ~TYPE_QUAL_CONST
;
2999 member_type
= cp_build_qualified_type (member_type
, type_quals
);
3002 result
= build3_loc (input_location
, COMPONENT_REF
, member_type
,
3003 object
, member
, NULL_TREE
);
3005 /* Mark the expression const or volatile, as appropriate. Even
3006 though we've dealt with the type above, we still have to mark the
3007 expression itself. */
3008 if (type_quals
& TYPE_QUAL_CONST
)
3009 TREE_READONLY (result
) = 1;
3010 if (type_quals
& TYPE_QUAL_VOLATILE
)
3011 TREE_THIS_VOLATILE (result
) = 1;
3013 else if (BASELINK_P (member
))
3015 /* The member is a (possibly overloaded) member function. */
3019 /* If the MEMBER is exactly one static member function, then we
3020 know the type of the expression. Otherwise, we must wait
3021 until overload resolution has been performed. */
3022 functions
= BASELINK_FUNCTIONS (member
);
3023 if (TREE_CODE (functions
) == FUNCTION_DECL
3024 && DECL_STATIC_FUNCTION_P (functions
))
3025 type
= TREE_TYPE (functions
);
3027 type
= unknown_type_node
;
3028 /* Note that we do not convert OBJECT to the BASELINK_BINFO
3029 base. That will happen when the function is called. */
3030 result
= build3_loc (input_location
, COMPONENT_REF
, type
, object
, member
,
3033 else if (TREE_CODE (member
) == CONST_DECL
)
3035 /* The member is an enumerator. */
3037 /* If OBJECT has side-effects, they are supposed to occur. */
3038 if (TREE_SIDE_EFFECTS (object
))
3039 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
3042 else if ((using_decl
= strip_using_decl (member
)) != member
)
3043 result
= build_class_member_access_expr (object
,
3045 access_path
, preserve_reference
,
3049 if (complain
& tf_error
)
3050 error ("invalid use of %qD", member
);
3051 return error_mark_node
;
3054 if (!preserve_reference
)
3057 If E2 is declared to have type "reference to T", then ... the
3058 type of E1.E2 is T. */
3059 result
= convert_from_reference (result
);
3064 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
3065 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
3068 lookup_destructor (tree object
, tree scope
, tree dtor_name
,
3069 tsubst_flags_t complain
)
3071 tree object_type
= TREE_TYPE (object
);
3072 tree dtor_type
= TREE_OPERAND (dtor_name
, 0);
3075 /* We've already complained about this destructor. */
3076 if (dtor_type
== error_mark_node
)
3077 return error_mark_node
;
3079 if (scope
&& !check_dtor_name (scope
, dtor_type
))
3081 if (complain
& tf_error
)
3082 error ("qualified type %qT does not match destructor name ~%qT",
3084 return error_mark_node
;
3086 if (is_auto (dtor_type
))
3087 dtor_type
= object_type
;
3088 else if (identifier_p (dtor_type
))
3090 /* In a template, names we can't find a match for are still accepted
3091 destructor names, and we check them here. */
3092 if (check_dtor_name (object_type
, dtor_type
))
3093 dtor_type
= object_type
;
3096 if (complain
& tf_error
)
3097 error ("object type %qT does not match destructor name ~%qT",
3098 object_type
, dtor_type
);
3099 return error_mark_node
;
3103 else if (!DERIVED_FROM_P (dtor_type
, TYPE_MAIN_VARIANT (object_type
)))
3105 if (complain
& tf_error
)
3106 error ("the type being destroyed is %qT, but the destructor "
3107 "refers to %qT", TYPE_MAIN_VARIANT (object_type
), dtor_type
);
3108 return error_mark_node
;
3110 expr
= lookup_member (dtor_type
, complete_dtor_identifier
,
3111 /*protect=*/1, /*want_type=*/false,
3112 tf_warning_or_error
);
3115 if (complain
& tf_error
)
3116 cxx_incomplete_type_error (dtor_name
, dtor_type
);
3117 return error_mark_node
;
3119 expr
= (adjust_result_of_qualified_name_lookup
3120 (expr
, dtor_type
, object_type
));
3121 if (scope
== NULL_TREE
)
3122 /* We need to call adjust_result_of_qualified_name_lookup in case the
3123 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
3124 that we still get virtual function binding. */
3125 BASELINK_QUALIFIED_P (expr
) = false;
3129 /* An expression of the form "A::template B" has been resolved to
3130 DECL. Issue a diagnostic if B is not a template or template
3134 check_template_keyword (tree decl
)
3136 /* The standard says:
3140 If a name prefixed by the keyword template is not a member
3141 template, the program is ill-formed.
3143 DR 228 removed the restriction that the template be a member
3146 DR 96, if accepted would add the further restriction that explicit
3147 template arguments must be provided if the template keyword is
3148 used, but, as of 2005-10-16, that DR is still in "drafting". If
3149 this DR is accepted, then the semantic checks here can be
3150 simplified, as the entity named must in fact be a template
3151 specialization, rather than, as at present, a set of overloaded
3152 functions containing at least one template function. */
3153 if (TREE_CODE (decl
) != TEMPLATE_DECL
3154 && TREE_CODE (decl
) != TEMPLATE_ID_EXPR
)
3158 if (DECL_USE_TEMPLATE (decl
)
3159 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
3162 permerror (input_location
, "%qD is not a template", decl
);
3164 else if (!is_overloaded_fn (decl
))
3165 permerror (input_location
, "%qD is not a template", decl
);
3170 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
3171 !found
&& iter
; ++iter
)
3174 if (TREE_CODE (fn
) == TEMPLATE_DECL
3175 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
3176 || (TREE_CODE (fn
) == FUNCTION_DECL
3177 && DECL_USE_TEMPLATE (fn
)
3178 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn
))))
3182 permerror (input_location
, "%qD is not a template", decl
);
3187 /* Record that an access failure occurred on BASETYPE_PATH attempting
3188 to access DECL, where DIAG_DECL should be used for diagnostics. */
3191 access_failure_info::record_access_failure (tree basetype_path
,
3192 tree decl
, tree diag_decl
)
3194 m_was_inaccessible
= true;
3195 m_basetype_path
= basetype_path
;
3197 m_diag_decl
= diag_decl
;
3200 /* If an access failure was recorded, then attempt to locate an
3201 accessor function for the pertinent field.
3202 Otherwise, return NULL_TREE. */
3205 access_failure_info::get_any_accessor (bool const_p
) const
3207 if (!was_inaccessible_p ())
3211 = locate_field_accessor (m_basetype_path
, m_diag_decl
, const_p
);
3215 /* The accessor must itself be accessible for it to be a reasonable
3217 if (!accessible_p (m_basetype_path
, accessor
, true))
3223 /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3224 replacing the primary location in RICHLOC with "accessor()". */
3227 access_failure_info::add_fixit_hint (rich_location
*richloc
,
3231 pp_string (&pp
, IDENTIFIER_POINTER (DECL_NAME (accessor_decl
)));
3232 pp_string (&pp
, "()");
3233 richloc
->add_fixit_replace (pp_formatted_text (&pp
));
3236 /* If an access failure was recorded, then attempt to locate an
3237 accessor function for the pertinent field, and if one is
3238 available, add a note and fix-it hint suggesting using it. */
3241 access_failure_info::maybe_suggest_accessor (bool const_p
) const
3243 tree accessor
= get_any_accessor (const_p
);
3244 if (accessor
== NULL_TREE
)
3246 rich_location
richloc (line_table
, input_location
);
3247 add_fixit_hint (&richloc
, accessor
);
3248 inform (&richloc
, "field %q#D can be accessed via %q#D",
3249 m_diag_decl
, accessor
);
3252 /* Subroutine of finish_class_member_access_expr.
3253 Issue an error about NAME not being a member of ACCESS_PATH (or
3254 OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3258 complain_about_unrecognized_member (tree access_path
, tree name
,
3261 /* Attempt to provide a hint about misspelled names. */
3262 tree guessed_id
= lookup_member_fuzzy (access_path
, name
,
3263 /*want_type=*/false);
3264 if (guessed_id
== NULL_TREE
)
3267 error ("%q#T has no member named %qE",
3268 TREE_CODE (access_path
) == TREE_BINFO
3269 ? TREE_TYPE (access_path
) : object_type
, name
);
3273 location_t bogus_component_loc
= input_location
;
3274 gcc_rich_location
rich_loc (bogus_component_loc
);
3276 /* Check that the guessed name is accessible along access_path. */
3277 access_failure_info afi
;
3278 lookup_member (access_path
, guessed_id
, /*protect=*/1,
3279 /*want_type=*/false, /*complain=*/false,
3281 if (afi
.was_inaccessible_p ())
3283 tree accessor
= afi
.get_any_accessor (TYPE_READONLY (object_type
));
3286 /* The guessed name isn't directly accessible, but can be accessed
3287 via an accessor member function. */
3288 afi
.add_fixit_hint (&rich_loc
, accessor
);
3289 error_at (&rich_loc
,
3290 "%q#T has no member named %qE;"
3291 " did you mean %q#D? (accessible via %q#D)",
3292 TREE_CODE (access_path
) == TREE_BINFO
3293 ? TREE_TYPE (access_path
) : object_type
,
3294 name
, afi
.get_diag_decl (), accessor
);
3298 /* The guessed name isn't directly accessible, and no accessor
3299 member function could be found. */
3300 error_at (&rich_loc
,
3301 "%q#T has no member named %qE;"
3302 " did you mean %q#D? (not accessible from this context)",
3303 TREE_CODE (access_path
) == TREE_BINFO
3304 ? TREE_TYPE (access_path
) : object_type
,
3305 name
, afi
.get_diag_decl ());
3306 complain_about_access (afi
.get_decl (), afi
.get_diag_decl (),
3307 afi
.get_diag_decl (), false, ak_none
);
3312 /* The guessed name is directly accessible; suggest it. */
3313 rich_loc
.add_fixit_misspelled_id (bogus_component_loc
,
3315 error_at (&rich_loc
,
3316 "%q#T has no member named %qE;"
3317 " did you mean %qE?",
3318 TREE_CODE (access_path
) == TREE_BINFO
3319 ? TREE_TYPE (access_path
) : object_type
,
3324 /* This function is called by the parser to process a class member
3325 access expression of the form OBJECT.NAME. NAME is a node used by
3326 the parser to represent a name; it is not yet a DECL. It may,
3327 however, be a BASELINK where the BASELINK_FUNCTIONS is a
3328 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3329 there is no reason to do the lookup twice, so the parser keeps the
3330 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3331 be a template via the use of the "A::template B" syntax. */
3334 finish_class_member_access_expr (cp_expr object
, tree name
, bool template_p
,
3335 tsubst_flags_t complain
)
3340 tree access_path
= NULL_TREE
;
3341 tree orig_object
= object
;
3342 tree orig_name
= name
;
3344 if (object
== error_mark_node
|| name
== error_mark_node
)
3345 return error_mark_node
;
3347 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3348 if (!objc_is_public (object
, name
))
3349 return error_mark_node
;
3351 object_type
= TREE_TYPE (object
);
3353 if (processing_template_decl
)
3355 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3356 type_dependent_object_expression_p (object
)
3357 /* If NAME is "f<args>", where either 'f' or 'args' is
3358 dependent, then the expression is dependent. */
3359 || (TREE_CODE (name
) == TEMPLATE_ID_EXPR
3360 && dependent_template_id_p (TREE_OPERAND (name
, 0),
3361 TREE_OPERAND (name
, 1)))
3362 /* If NAME is "T::X" where "T" is dependent, then the
3363 expression is dependent. */
3364 || (TREE_CODE (name
) == SCOPE_REF
3365 && TYPE_P (TREE_OPERAND (name
, 0))
3366 && dependent_scope_p (TREE_OPERAND (name
, 0)))
3367 /* If NAME is operator T where "T" is dependent, we can't
3368 lookup until we instantiate the T. */
3369 || (TREE_CODE (name
) == IDENTIFIER_NODE
3370 && IDENTIFIER_CONV_OP_P (name
)
3371 && dependent_type_p (TREE_TYPE (name
))))
3374 return build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
,
3375 orig_object
, orig_name
, NULL_TREE
);
3377 object
= build_non_dependent_expr (object
);
3379 else if (c_dialect_objc ()
3380 && identifier_p (name
)
3381 && (expr
= objc_maybe_build_component_ref (object
, name
)))
3386 The type of the first expression shall be "class object" (of a
3388 if (!currently_open_class (object_type
)
3389 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
3390 return error_mark_node
;
3391 if (!CLASS_TYPE_P (object_type
))
3393 if (complain
& tf_error
)
3395 if (INDIRECT_TYPE_P (object_type
)
3396 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
3397 error ("request for member %qD in %qE, which is of pointer "
3398 "type %qT (maybe you meant to use %<->%> ?)",
3399 name
, object
.get_value (), object_type
);
3401 error ("request for member %qD in %qE, which is of non-class "
3402 "type %qT", name
, object
.get_value (), object_type
);
3404 return error_mark_node
;
3407 if (BASELINK_P (name
))
3408 /* A member function that has already been looked up. */
3412 bool is_template_id
= false;
3413 tree template_args
= NULL_TREE
;
3414 tree scope
= NULL_TREE
;
3416 access_path
= object_type
;
3418 if (TREE_CODE (name
) == SCOPE_REF
)
3420 /* A qualified name. The qualifying class or namespace `S'
3421 has already been looked up; it is either a TYPE or a
3423 scope
= TREE_OPERAND (name
, 0);
3424 name
= TREE_OPERAND (name
, 1);
3426 /* If SCOPE is a namespace, then the qualified name does not
3427 name a member of OBJECT_TYPE. */
3428 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
3430 if (complain
& tf_error
)
3431 error ("%<%D::%D%> is not a member of %qT",
3432 scope
, name
, object_type
);
3433 return error_mark_node
;
3437 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
3439 is_template_id
= true;
3440 template_args
= TREE_OPERAND (name
, 1);
3441 name
= TREE_OPERAND (name
, 0);
3443 if (!identifier_p (name
))
3444 name
= OVL_NAME (name
);
3449 if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
3451 gcc_assert (!is_template_id
);
3452 /* Looking up a member enumerator (c++/56793). */
3453 if (!TYPE_CLASS_SCOPE_P (scope
)
3454 || !DERIVED_FROM_P (TYPE_CONTEXT (scope
), object_type
))
3456 if (complain
& tf_error
)
3457 error ("%<%D::%D%> is not a member of %qT",
3458 scope
, name
, object_type
);
3459 return error_mark_node
;
3461 tree val
= lookup_enumerator (scope
, name
);
3464 if (complain
& tf_error
)
3465 error ("%qD is not a member of %qD",
3467 return error_mark_node
;
3470 if (TREE_SIDE_EFFECTS (object
))
3471 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
), object
, val
);
3475 gcc_assert (CLASS_TYPE_P (scope
));
3476 gcc_assert (identifier_p (name
) || TREE_CODE (name
) == BIT_NOT_EXPR
);
3478 if (constructor_name_p (name
, scope
))
3480 if (complain
& tf_error
)
3481 error ("cannot call constructor %<%T::%D%> directly",
3483 return error_mark_node
;
3486 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3487 access_path
= lookup_base (object_type
, scope
, ba_check
,
3489 if (access_path
== error_mark_node
)
3490 return error_mark_node
;
3493 if (any_dependent_bases_p (object_type
))
3495 if (complain
& tf_error
)
3496 error ("%qT is not a base of %qT", scope
, object_type
);
3497 return error_mark_node
;
3501 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
3503 if (dependent_type_p (object_type
))
3504 /* The destructor isn't declared yet. */
3506 member
= lookup_destructor (object
, scope
, name
, complain
);
3510 /* Look up the member. */
3511 access_failure_info afi
;
3512 if (processing_template_decl
)
3513 /* Even though this class member access expression is at this
3514 point not dependent, the member itself may be dependent, and
3515 we must not potentially push a access check for a dependent
3516 member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3517 ahead of time here; we're going to redo this member lookup at
3518 instantiation time anyway. */
3519 push_deferring_access_checks (dk_no_check
);
3520 member
= lookup_member (access_path
, name
, /*protect=*/1,
3521 /*want_type=*/false, complain
,
3523 if (processing_template_decl
)
3524 pop_deferring_access_checks ();
3525 afi
.maybe_suggest_accessor (TYPE_READONLY (object_type
));
3526 if (member
== NULL_TREE
)
3528 if (dependent_type_p (object_type
))
3529 /* Try again at instantiation time. */
3531 if (complain
& tf_error
)
3532 complain_about_unrecognized_member (access_path
, name
,
3534 return error_mark_node
;
3536 if (member
== error_mark_node
)
3537 return error_mark_node
;
3539 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member
)))
3540 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3541 wrong, so don't use it. */
3543 if (TREE_CODE (member
) == USING_DECL
&& DECL_DEPENDENT_P (member
))
3549 tree templ
= member
;
3551 if (BASELINK_P (templ
))
3552 member
= lookup_template_function (templ
, template_args
);
3553 else if (variable_template_p (templ
))
3554 member
= (lookup_and_finish_template_variable
3555 (templ
, template_args
, complain
));
3558 if (complain
& tf_error
)
3559 error ("%qD is not a member template function", name
);
3560 return error_mark_node
;
3565 if (TREE_UNAVAILABLE (member
))
3566 error_unavailable_use (member
, NULL_TREE
);
3567 else if (TREE_DEPRECATED (member
))
3568 warn_deprecated_use (member
, NULL_TREE
);
3571 check_template_keyword (member
);
3573 expr
= build_class_member_access_expr (object
, member
, access_path
,
3574 /*preserve_reference=*/false,
3576 if (processing_template_decl
&& expr
!= error_mark_node
)
3578 if (BASELINK_P (member
))
3580 if (TREE_CODE (orig_name
) == SCOPE_REF
)
3581 BASELINK_QUALIFIED_P (member
) = 1;
3584 return build_min_non_dep (COMPONENT_REF
, expr
,
3585 orig_object
, orig_name
,
3592 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3596 build_simple_component_ref (tree object
, tree member
)
3598 tree type
= cp_build_qualified_type (TREE_TYPE (member
),
3599 cp_type_quals (TREE_TYPE (object
)));
3600 return build3_loc (input_location
,
3601 COMPONENT_REF
, type
,
3602 object
, member
, NULL_TREE
);
3605 /* Return an expression for the MEMBER_NAME field in the internal
3606 representation of PTRMEM, a pointer-to-member function. (Each
3607 pointer-to-member function type gets its own RECORD_TYPE so it is
3608 more convenient to access the fields by name than by FIELD_DECL.)
3609 This routine converts the NAME to a FIELD_DECL and then creates the
3610 node for the complete expression. */
3613 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
3618 if (TREE_CODE (ptrmem
) == CONSTRUCTOR
)
3620 for (auto &e
: CONSTRUCTOR_ELTS (ptrmem
))
3621 if (e
.index
&& DECL_P (e
.index
) && DECL_NAME (e
.index
) == member_name
)
3626 /* This code is a stripped down version of
3627 build_class_member_access_expr. It does not work to use that
3628 routine directly because it expects the object to be of class
3630 ptrmem_type
= TREE_TYPE (ptrmem
);
3631 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
));
3632 for (member
= TYPE_FIELDS (ptrmem_type
); member
;
3633 member
= DECL_CHAIN (member
))
3634 if (DECL_NAME (member
) == member_name
)
3636 return build_simple_component_ref (ptrmem
, member
);
3639 /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3640 and for any other relevant operator. */
3643 op_unqualified_lookup (tree_code code
, bool is_assign
)
3645 tree lookups
= NULL_TREE
;
3647 if (cxx_dialect
>= cxx20
&& !is_assign
)
3649 if (code
== NE_EXPR
)
3651 /* != can get rewritten in terms of ==. */
3652 tree fnname
= ovl_op_identifier (false, EQ_EXPR
);
3653 if (tree fns
= lookup_name (fnname
, LOOK_where::BLOCK_NAMESPACE
))
3654 lookups
= tree_cons (fnname
, fns
, lookups
);
3656 else if (code
== GT_EXPR
|| code
== LE_EXPR
3657 || code
== LT_EXPR
|| code
== GE_EXPR
)
3659 /* These can get rewritten in terms of <=>. */
3660 tree fnname
= ovl_op_identifier (false, SPACESHIP_EXPR
);
3661 if (tree fns
= lookup_name (fnname
, LOOK_where::BLOCK_NAMESPACE
))
3662 lookups
= tree_cons (fnname
, fns
, lookups
);
3666 tree fnname
= ovl_op_identifier (is_assign
, code
);
3667 if (tree fns
= lookup_name (fnname
, LOOK_where::BLOCK_NAMESPACE
))
3668 lookups
= tree_cons (fnname
, fns
, lookups
);
3673 return build_tree_list (NULL_TREE
, NULL_TREE
);
3676 /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3677 the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3678 name lookup for the given operator. */
3681 build_dependent_operator_type (tree lookups
, tree_code code
, bool is_assign
)
3684 /* We're partially instantiating a dependent operator expression, and
3685 LOOKUPS is the result of phase 1 name lookup that we performed
3686 earlier at template definition time, so just reuse the corresponding
3687 DEPENDENT_OPERATOR_TYPE. */
3688 return TREE_TYPE (lookups
);
3690 /* Otherwise we're processing a dependent operator expression at template
3691 definition time, so perform phase 1 name lookup now. */
3692 lookups
= op_unqualified_lookup (code
, is_assign
);
3694 tree type
= cxx_make_type (DEPENDENT_OPERATOR_TYPE
);
3695 DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type
) = lookups
;
3696 TREE_TYPE (lookups
) = type
;
3700 /* Given an expression PTR for a pointer, return an expression
3701 for the value pointed to.
3702 ERRORSTRING is the name of the operator to appear in error messages.
3704 This function may need to overload OPERATOR_FNNAME.
3705 Must also handle REFERENCE_TYPEs for C++. */
3708 build_x_indirect_ref (location_t loc
, tree expr
, ref_operator errorstring
,
3709 tree lookups
, tsubst_flags_t complain
)
3711 tree orig_expr
= expr
;
3713 tree overload
= NULL_TREE
;
3715 if (processing_template_decl
)
3717 /* Retain the type if we know the operand is a pointer. */
3718 if (TREE_TYPE (expr
) && INDIRECT_TYPE_P (TREE_TYPE (expr
)))
3720 if (expr
== current_class_ptr
3721 || (TREE_CODE (expr
) == NOP_EXPR
3722 && TREE_OPERAND (expr
, 0) == current_class_ptr
3723 && (same_type_ignoring_top_level_qualifiers_p
3724 (TREE_TYPE (expr
), TREE_TYPE (current_class_ptr
)))))
3725 return current_class_ref
;
3726 return build_min (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
3728 if (type_dependent_expression_p (expr
))
3730 expr
= build_min_nt_loc (loc
, INDIRECT_REF
, expr
);
3732 = build_dependent_operator_type (lookups
, INDIRECT_REF
, false);
3735 expr
= build_non_dependent_expr (expr
);
3738 rval
= build_new_op (loc
, INDIRECT_REF
, LOOKUP_NORMAL
, expr
,
3739 NULL_TREE
, NULL_TREE
, lookups
,
3740 &overload
, complain
);
3742 rval
= cp_build_indirect_ref (loc
, expr
, errorstring
, complain
);
3744 if (processing_template_decl
&& rval
!= error_mark_node
)
3746 if (overload
!= NULL_TREE
)
3747 return (build_min_non_dep_op_overload
3748 (INDIRECT_REF
, rval
, overload
, orig_expr
));
3750 return build_min_non_dep (INDIRECT_REF
, rval
, orig_expr
);
3756 /* Like c-family strict_aliasing_warning, but don't warn for dependent
3757 types or expressions. */
3760 cp_strict_aliasing_warning (location_t loc
, tree type
, tree expr
)
3762 if (processing_template_decl
)
3766 if (dependent_type_p (type
) || type_dependent_expression_p (e
))
3769 return strict_aliasing_warning (loc
, type
, expr
);
3772 /* The implementation of the above, and of indirection implied by other
3773 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3776 cp_build_indirect_ref_1 (location_t loc
, tree ptr
, ref_operator errorstring
,
3777 tsubst_flags_t complain
, bool do_fold
)
3781 /* RO_NULL should only be used with the folding entry points below, not
3782 cp_build_indirect_ref. */
3783 gcc_checking_assert (errorstring
!= RO_NULL
|| do_fold
);
3785 if (ptr
== current_class_ptr
3786 || (TREE_CODE (ptr
) == NOP_EXPR
3787 && TREE_OPERAND (ptr
, 0) == current_class_ptr
3788 && (same_type_ignoring_top_level_qualifiers_p
3789 (TREE_TYPE (ptr
), TREE_TYPE (current_class_ptr
)))))
3790 return current_class_ref
;
3792 pointer
= (TYPE_REF_P (TREE_TYPE (ptr
))
3793 ? ptr
: decay_conversion (ptr
, complain
));
3794 if (pointer
== error_mark_node
)
3795 return error_mark_node
;
3797 type
= TREE_TYPE (pointer
);
3799 if (INDIRECT_TYPE_P (type
))
3803 If the type of the expression is "pointer to T," the type
3804 of the result is "T." */
3805 tree t
= TREE_TYPE (type
);
3807 if ((CONVERT_EXPR_P (ptr
)
3808 || TREE_CODE (ptr
) == VIEW_CONVERT_EXPR
)
3809 && (!CLASS_TYPE_P (t
) || !CLASSTYPE_EMPTY_P (t
)))
3811 /* If a warning is issued, mark it to avoid duplicates from
3812 the backend. This only needs to be done at
3813 warn_strict_aliasing > 2. */
3814 if (warn_strict_aliasing
> 2
3815 && cp_strict_aliasing_warning (EXPR_LOCATION (ptr
),
3816 type
, TREE_OPERAND (ptr
, 0)))
3817 suppress_warning (ptr
, OPT_Wstrict_aliasing
);
3820 if (VOID_TYPE_P (t
))
3822 /* A pointer to incomplete type (other than cv void) can be
3823 dereferenced [expr.unary.op]/1 */
3824 if (complain
& tf_error
)
3825 error_at (loc
, "%qT is not a pointer-to-object type", type
);
3826 return error_mark_node
;
3828 else if (do_fold
&& TREE_CODE (pointer
) == ADDR_EXPR
3829 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
3830 /* The POINTER was something like `&x'. We simplify `*&x' to
3832 return TREE_OPERAND (pointer
, 0);
3835 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
3837 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3838 so that we get the proper error message if the result is used
3839 to assign to. Also, &* is supposed to be a no-op. */
3840 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
3841 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
3842 TREE_SIDE_EFFECTS (ref
)
3843 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
));
3847 else if (!(complain
& tf_error
))
3848 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3850 /* `pointer' won't be an error_mark_node if we were given a
3851 pointer to member, so it's cool to check for this here. */
3852 else if (TYPE_PTRMEM_P (type
))
3853 switch (errorstring
)
3855 case RO_ARRAY_INDEXING
:
3857 "invalid use of array indexing on pointer to member");
3860 error_at (loc
, "invalid use of unary %<*%> on pointer to member");
3862 case RO_IMPLICIT_CONVERSION
:
3863 error_at (loc
, "invalid use of implicit conversion on pointer "
3867 error_at (loc
, "left hand operand of %<->*%> must be a pointer to "
3868 "class, but is a pointer to member of type %qT", type
);
3873 else if (pointer
!= error_mark_node
)
3874 invalid_indirection_error (loc
, type
, errorstring
);
3876 return error_mark_node
;
3879 /* Entry point used by c-common, which expects folding. */
3882 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errorstring
)
3884 return cp_build_indirect_ref_1 (loc
, ptr
, errorstring
,
3885 tf_warning_or_error
, true);
3888 /* Entry point used by internal indirection needs that don't correspond to any
3889 syntactic construct. */
3892 cp_build_fold_indirect_ref (tree pointer
)
3894 return cp_build_indirect_ref_1 (input_location
, pointer
, RO_NULL
,
3895 tf_warning_or_error
, true);
3898 /* Entry point used by indirection needs that correspond to some syntactic
3902 cp_build_indirect_ref (location_t loc
, tree ptr
, ref_operator errorstring
,
3903 tsubst_flags_t complain
)
3905 return cp_build_indirect_ref_1 (loc
, ptr
, errorstring
, complain
, false);
3908 /* This handles expressions of the form "a[i]", which denotes
3911 This is logically equivalent in C to *(a+i), but we may do it differently.
3912 If A is a variable or a member, we generate a primitive ARRAY_REF.
3913 This avoids forcing the array out of registers, and can work on
3914 arrays that are not lvalues (for example, members of structures returned
3917 If INDEX is of some user-defined type, it must be converted to
3918 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3919 will inherit the type of the array, which will be some pointer type.
3921 LOC is the location to use in building the array reference. */
3924 cp_build_array_ref (location_t loc
, tree array
, tree idx
,
3925 tsubst_flags_t complain
)
3931 if (complain
& tf_error
)
3932 error_at (loc
, "subscript missing in array reference");
3933 return error_mark_node
;
3936 if (TREE_TYPE (array
) == error_mark_node
3937 || TREE_TYPE (idx
) == error_mark_node
)
3938 return error_mark_node
;
3940 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3942 switch (TREE_CODE (array
))
3946 tree value
= cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3948 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3949 TREE_OPERAND (array
, 0), value
);
3950 SET_EXPR_LOCATION (ret
, loc
);
3955 ret
= build_conditional_expr
3956 (loc
, TREE_OPERAND (array
, 0),
3957 cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3959 cp_build_array_ref (loc
, TREE_OPERAND (array
, 2), idx
,
3962 protected_set_expr_location (ret
, loc
);
3969 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, idx
);
3971 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
3975 warn_array_subscript_with_type_char (loc
, idx
);
3977 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx
)))
3979 if (complain
& tf_error
)
3980 error_at (loc
, "array subscript is not an integer");
3981 return error_mark_node
;
3984 /* Apply integral promotions *after* noticing character types.
3985 (It is unclear why we do these promotions -- the standard
3986 does not say that we should. In fact, the natural thing would
3987 seem to be to convert IDX to ptrdiff_t; we're performing
3988 pointer arithmetic.) */
3989 idx
= cp_perform_integral_promotions (idx
, complain
);
3991 idx
= maybe_fold_non_dependent_expr (idx
, complain
);
3993 /* An array that is indexed by a non-constant
3994 cannot be stored in a register; we must be able to do
3995 address arithmetic on its address.
3996 Likewise an array of elements of variable size. */
3997 if (TREE_CODE (idx
) != INTEGER_CST
3998 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
3999 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
4002 if (!cxx_mark_addressable (array
, true))
4003 return error_mark_node
;
4006 /* An array that is indexed by a constant value which is not within
4007 the array bounds cannot be stored in a register either; because we
4008 would get a crash in store_bit_field/extract_bit_field when trying
4009 to access a non-existent part of the register. */
4010 if (TREE_CODE (idx
) == INTEGER_CST
4011 && TYPE_DOMAIN (TREE_TYPE (array
))
4012 && ! int_fits_type_p (idx
, TYPE_DOMAIN (TREE_TYPE (array
))))
4014 if (!cxx_mark_addressable (array
))
4015 return error_mark_node
;
4018 /* Note in C++ it is valid to subscript a `register' array, since
4019 it is valid to take the address of something with that
4020 storage specification. */
4024 while (TREE_CODE (foo
) == COMPONENT_REF
)
4025 foo
= TREE_OPERAND (foo
, 0);
4026 if (VAR_P (foo
) && DECL_REGISTER (foo
)
4027 && (complain
& tf_warning
))
4028 warning_at (loc
, OPT_Wextra
,
4029 "subscripting array declared %<register%>");
4032 type
= TREE_TYPE (TREE_TYPE (array
));
4033 rval
= build4 (ARRAY_REF
, type
, array
, idx
, NULL_TREE
, NULL_TREE
);
4034 /* Array ref is const/volatile if the array elements are
4035 or if the array is.. */
4036 TREE_READONLY (rval
)
4037 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
4038 TREE_SIDE_EFFECTS (rval
)
4039 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
4040 TREE_THIS_VOLATILE (rval
)
4041 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
4042 ret
= require_complete_type (rval
, complain
);
4043 protected_set_expr_location (ret
, loc
);
4045 ret
= non_lvalue_loc (loc
, ret
);
4050 tree ar
= cp_default_conversion (array
, complain
);
4051 tree ind
= cp_default_conversion (idx
, complain
);
4052 tree first
= NULL_TREE
;
4054 if (flag_strong_eval_order
== 2 && TREE_SIDE_EFFECTS (ind
))
4055 ar
= first
= save_expr (ar
);
4057 /* Put the integer in IND to simplify error checking. */
4058 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
4059 std::swap (ar
, ind
);
4061 if (ar
== error_mark_node
|| ind
== error_mark_node
)
4062 return error_mark_node
;
4064 if (!TYPE_PTR_P (TREE_TYPE (ar
)))
4066 if (complain
& tf_error
)
4067 error_at (loc
, "subscripted value is neither array nor pointer");
4068 return error_mark_node
;
4070 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
4072 if (complain
& tf_error
)
4073 error_at (loc
, "array subscript is not an integer");
4074 return error_mark_node
;
4077 warn_array_subscript_with_type_char (loc
, idx
);
4079 ret
= cp_build_binary_op (input_location
, PLUS_EXPR
, ar
, ind
, complain
);
4081 ret
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (ret
), first
, ret
);
4082 ret
= cp_build_indirect_ref (loc
, ret
, RO_ARRAY_INDEXING
, complain
);
4083 protected_set_expr_location (ret
, loc
);
4085 ret
= non_lvalue_loc (loc
, ret
);
4090 /* Entry point for Obj-C++. */
4093 build_array_ref (location_t loc
, tree array
, tree idx
)
4095 return cp_build_array_ref (loc
, array
, idx
, tf_warning_or_error
);
4098 /* Resolve a pointer to member function. INSTANCE is the object
4099 instance to use, if the member points to a virtual member.
4101 This used to avoid checking for virtual functions if basetype
4102 has no virtual functions, according to an earlier ANSI draft.
4103 With the final ISO C++ rules, such an optimization is
4104 incorrect: A pointer to a derived member can be static_cast
4105 to pointer-to-base-member, as long as the dynamic object
4106 later has the right member. So now we only do this optimization
4107 when we know the dynamic type of the object. */
4110 get_member_function_from_ptrfunc (tree
*instance_ptrptr
, tree function
,
4111 tsubst_flags_t complain
)
4113 if (TREE_CODE (function
) == OFFSET_REF
)
4114 function
= TREE_OPERAND (function
, 1);
4116 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
4118 tree idx
, delta
, e1
, e2
, e3
, vtbl
;
4120 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
4121 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
4123 tree instance_ptr
= *instance_ptrptr
;
4124 tree instance_save_expr
= 0;
4125 if (instance_ptr
== error_mark_node
)
4127 if (TREE_CODE (function
) == PTRMEM_CST
)
4129 /* Extracting the function address from a pmf is only
4130 allowed with -Wno-pmf-conversions. It only works for
4132 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
), complain
);
4133 e1
= convert (fntype
, e1
);
4138 if (complain
& tf_error
)
4139 error ("object missing in use of %qE", function
);
4140 return error_mark_node
;
4144 /* True if we know that the dynamic type of the object doesn't have
4145 virtual functions, so we can assume the PFN field is a pointer. */
4146 nonvirtual
= (COMPLETE_TYPE_P (basetype
)
4147 && !TYPE_POLYMORPHIC_P (basetype
)
4148 && resolves_to_fixed_type_p (instance_ptr
, 0));
4150 /* If we don't really have an object (i.e. in an ill-formed
4151 conversion from PMF to pointer), we can't resolve virtual
4152 functions anyway. */
4153 if (!nonvirtual
&& is_dummy_object (instance_ptr
))
4156 if (TREE_SIDE_EFFECTS (instance_ptr
))
4157 instance_ptr
= instance_save_expr
= save_expr (instance_ptr
);
4159 if (TREE_SIDE_EFFECTS (function
))
4160 function
= save_expr (function
);
4162 /* Start by extracting all the information from the PMF itself. */
4163 e3
= pfn_from_ptrmemfunc (function
);
4164 delta
= delta_from_ptrmemfunc (function
);
4165 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
4166 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
4168 int flag_sanitize_save
;
4169 case ptrmemfunc_vbit_in_pfn
:
4170 e1
= cp_build_binary_op (input_location
,
4171 BIT_AND_EXPR
, idx
, integer_one_node
,
4173 idx
= cp_build_binary_op (input_location
,
4174 MINUS_EXPR
, idx
, integer_one_node
,
4176 if (idx
== error_mark_node
)
4177 return error_mark_node
;
4180 case ptrmemfunc_vbit_in_delta
:
4181 e1
= cp_build_binary_op (input_location
,
4182 BIT_AND_EXPR
, delta
, integer_one_node
,
4184 /* Don't instrument the RSHIFT_EXPR we're about to create because
4185 we're going to use DELTA number of times, and that wouldn't play
4186 well with SAVE_EXPRs therein. */
4187 flag_sanitize_save
= flag_sanitize
;
4189 delta
= cp_build_binary_op (input_location
,
4190 RSHIFT_EXPR
, delta
, integer_one_node
,
4192 flag_sanitize
= flag_sanitize_save
;
4193 if (delta
== error_mark_node
)
4194 return error_mark_node
;
4201 if (e1
== error_mark_node
)
4202 return error_mark_node
;
4204 /* Convert down to the right base before using the instance. A
4205 special case is that in a pointer to member of class C, C may
4206 be incomplete. In that case, the function will of course be
4207 a member of C, and no conversion is required. In fact,
4208 lookup_base will fail in that case, because incomplete
4209 classes do not have BINFOs. */
4210 if (!same_type_ignoring_top_level_qualifiers_p
4211 (basetype
, TREE_TYPE (TREE_TYPE (instance_ptr
))))
4213 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
4214 basetype
, ba_check
, NULL
, complain
);
4215 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
,
4217 if (instance_ptr
== error_mark_node
)
4218 return error_mark_node
;
4220 /* ...and then the delta in the PMF. */
4221 instance_ptr
= fold_build_pointer_plus (instance_ptr
, delta
);
4223 /* Hand back the adjusted 'this' argument to our caller. */
4224 *instance_ptrptr
= instance_ptr
;
4227 /* Now just return the pointer. */
4230 /* Next extract the vtable pointer from the object. */
4231 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
4233 vtbl
= cp_build_fold_indirect_ref (vtbl
);
4234 if (vtbl
== error_mark_node
)
4235 return error_mark_node
;
4237 /* Finally, extract the function pointer from the vtable. */
4238 e2
= fold_build_pointer_plus_loc (input_location
, vtbl
, idx
);
4239 e2
= cp_build_fold_indirect_ref (e2
);
4240 if (e2
== error_mark_node
)
4241 return error_mark_node
;
4242 TREE_CONSTANT (e2
) = 1;
4244 /* When using function descriptors, the address of the
4245 vtable entry is treated as a function pointer. */
4246 if (TARGET_VTABLE_USES_DESCRIPTORS
)
4247 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
4248 cp_build_addr_expr (e2
, complain
));
4250 e2
= fold_convert (TREE_TYPE (e3
), e2
);
4251 e1
= build_conditional_expr (input_location
, e1
, e2
, e3
, complain
);
4252 if (e1
== error_mark_node
)
4253 return error_mark_node
;
4255 /* Make sure this doesn't get evaluated first inside one of the
4256 branches of the COND_EXPR. */
4257 if (instance_save_expr
)
4258 e1
= build2 (COMPOUND_EXPR
, TREE_TYPE (e1
),
4259 instance_save_expr
, e1
);
4266 /* Used by the C-common bits. */
4268 build_function_call (location_t
/*loc*/,
4269 tree function
, tree params
)
4271 return cp_build_function_call (function
, params
, tf_warning_or_error
);
4274 /* Used by the C-common bits. */
4276 build_function_call_vec (location_t
/*loc*/, vec
<location_t
> /*arg_loc*/,
4277 tree function
, vec
<tree
, va_gc
> *params
,
4278 vec
<tree
, va_gc
> * /*origtypes*/, tree orig_function
)
4280 vec
<tree
, va_gc
> *orig_params
= params
;
4281 tree ret
= cp_build_function_call_vec (function
, ¶ms
,
4282 tf_warning_or_error
, orig_function
);
4284 /* cp_build_function_call_vec can reallocate PARAMS by adding
4285 default arguments. That should never happen here. Verify
4287 gcc_assert (params
== orig_params
);
4292 /* Build a function call using a tree list of arguments. */
4295 cp_build_function_call (tree function
, tree params
, tsubst_flags_t complain
)
4300 for (; params
!= NULL_TREE
; params
= TREE_CHAIN (params
))
4301 vec_safe_push (vec
, TREE_VALUE (params
));
4302 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
4306 /* Build a function call using varargs. */
4309 cp_build_function_call_nary (tree function
, tsubst_flags_t complain
, ...)
4315 va_start (args
, complain
);
4316 for (t
= va_arg (args
, tree
); t
!= NULL_TREE
; t
= va_arg (args
, tree
))
4317 vec_safe_push (vec
, t
);
4319 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
4323 /* Build a function call using a vector of arguments.
4324 If FUNCTION is the result of resolving an overloaded target built-in,
4325 ORIG_FNDECL is the original function decl, otherwise it is null.
4326 PARAMS may be NULL if there are no parameters. This changes the
4327 contents of PARAMS. */
4330 cp_build_function_call_vec (tree function
, vec
<tree
, va_gc
> **params
,
4331 tsubst_flags_t complain
, tree orig_fndecl
)
4333 tree fntype
, fndecl
;
4335 tree original
= function
;
4339 vec
<tree
, va_gc
> *allocated
= NULL
;
4342 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4343 expressions, like those used for ObjC messenger dispatches. */
4344 if (params
!= NULL
&& !vec_safe_is_empty (*params
))
4345 function
= objc_rewrite_function_call (function
, (**params
)[0]);
4347 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4348 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4349 if (TREE_CODE (function
) == NOP_EXPR
4350 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
4351 function
= TREE_OPERAND (function
, 0);
4353 if (TREE_CODE (function
) == FUNCTION_DECL
)
4355 if (!mark_used (function
, complain
))
4356 return error_mark_node
;
4359 /* Convert anything with function type to a pointer-to-function. */
4360 if (DECL_MAIN_P (function
))
4362 if (complain
& tf_error
)
4363 pedwarn (input_location
, OPT_Wpedantic
,
4364 "ISO C++ forbids calling %<::main%> from within program");
4366 return error_mark_node
;
4368 function
= build_addr_func (function
, complain
);
4374 function
= build_addr_func (function
, complain
);
4377 if (function
== error_mark_node
)
4378 return error_mark_node
;
4380 fntype
= TREE_TYPE (function
);
4382 if (TYPE_PTRMEMFUNC_P (fntype
))
4384 if (complain
& tf_error
)
4385 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4386 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4387 original
, original
);
4388 return error_mark_node
;
4391 is_method
= (TYPE_PTR_P (fntype
)
4392 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
4394 if (!(TYPE_PTRFN_P (fntype
)
4396 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
4398 if (complain
& tf_error
)
4400 if (!flag_diagnostics_show_caret
)
4401 error_at (input_location
,
4402 "%qE cannot be used as a function", original
);
4403 else if (DECL_P (original
))
4404 error_at (input_location
,
4405 "%qD cannot be used as a function", original
);
4407 error_at (input_location
,
4408 "expression cannot be used as a function");
4411 return error_mark_node
;
4414 /* fntype now gets the type of function pointed to. */
4415 fntype
= TREE_TYPE (fntype
);
4416 parm_types
= TYPE_ARG_TYPES (fntype
);
4420 allocated
= make_tree_vector ();
4421 params
= &allocated
;
4424 nargs
= convert_arguments (parm_types
, params
, fndecl
, LOOKUP_NORMAL
,
4427 return error_mark_node
;
4429 argarray
= (*params
)->address ();
4431 /* Check for errors in format strings and inappropriately
4433 bool warned_p
= check_function_arguments (input_location
, fndecl
, fntype
,
4434 nargs
, argarray
, NULL
);
4436 ret
= build_cxx_call (function
, nargs
, argarray
, complain
, orig_fndecl
);
4440 tree c
= extract_call_expr (ret
);
4441 if (TREE_CODE (c
) == CALL_EXPR
)
4442 suppress_warning (c
, OPT_Wnonnull
);
4445 if (allocated
!= NULL
)
4446 release_tree_vector (allocated
);
4451 /* Subroutine of convert_arguments.
4452 Print an error message about a wrong number of arguments. */
4455 error_args_num (location_t loc
, tree fndecl
, bool too_many_p
)
4459 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
4461 if (DECL_NAME (fndecl
) == NULL_TREE
4462 || (DECL_NAME (fndecl
)
4463 == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl
)))))
4466 ? G_("too many arguments to constructor %q#D")
4467 : G_("too few arguments to constructor %q#D"),
4472 ? G_("too many arguments to member function %q#D")
4473 : G_("too few arguments to member function %q#D"),
4479 ? G_("too many arguments to function %q#D")
4480 : G_("too few arguments to function %q#D"),
4482 if (!DECL_IS_UNDECLARED_BUILTIN (fndecl
))
4483 inform (DECL_SOURCE_LOCATION (fndecl
), "declared here");
4487 if (c_dialect_objc () && objc_message_selector ())
4490 ? G_("too many arguments to method %q#D")
4491 : G_("too few arguments to method %q#D"),
4492 objc_message_selector ());
4494 error_at (loc
, too_many_p
? G_("too many arguments to function")
4495 : G_("too few arguments to function"));
4499 /* Convert the actual parameter expressions in the list VALUES to the
4500 types in the list TYPELIST. The converted expressions are stored
4501 back in the VALUES vector.
4502 If parmdecls is exhausted, or when an element has NULL as its type,
4503 perform the default conversions.
4505 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4507 This is also where warnings about wrong number of args are generated.
4509 Returns the actual number of arguments processed (which might be less
4510 than the length of the vector), or -1 on error.
4512 In C++, unspecified trailing parameters can be filled in with their
4513 default arguments, if such were specified. Do so here. */
4516 convert_arguments (tree typelist
, vec
<tree
, va_gc
> **values
, tree fndecl
,
4517 int flags
, tsubst_flags_t complain
)
4522 /* Argument passing is always copy-initialization. */
4523 flags
|= LOOKUP_ONLYCONVERTING
;
4525 for (i
= 0, typetail
= typelist
;
4526 i
< vec_safe_length (*values
);
4529 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
4530 tree val
= (**values
)[i
];
4532 if (val
== error_mark_node
|| type
== error_mark_node
)
4535 if (type
== void_type_node
)
4537 if (complain
& tf_error
)
4539 error_args_num (input_location
, fndecl
, /*too_many_p=*/true);
4546 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4547 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4548 if (TREE_CODE (val
) == NOP_EXPR
4549 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
4550 && (type
== 0 || !TYPE_REF_P (type
)))
4551 val
= TREE_OPERAND (val
, 0);
4553 if (type
== 0 || !TYPE_REF_P (type
))
4555 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
4556 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val
)))
4557 val
= decay_conversion (val
, complain
);
4560 if (val
== error_mark_node
)
4565 /* Formal parm type is specified by a function prototype. */
4568 if (!COMPLETE_TYPE_P (complete_type (type
)))
4570 if (complain
& tf_error
)
4572 location_t loc
= EXPR_LOC_OR_LOC (val
, input_location
);
4575 auto_diagnostic_group d
;
4577 "parameter %P of %qD has incomplete type %qT",
4579 inform (get_fndecl_argument_location (fndecl
, i
),
4583 error_at (loc
, "parameter %P has incomplete type %qT", i
,
4586 parmval
= error_mark_node
;
4590 parmval
= convert_for_initialization
4591 (NULL_TREE
, type
, val
, flags
,
4592 ICR_ARGPASS
, fndecl
, i
, complain
);
4593 parmval
= convert_for_arg_passing (type
, parmval
, complain
);
4596 if (parmval
== error_mark_node
)
4599 (**values
)[i
] = parmval
;
4603 if (fndecl
&& magic_varargs_p (fndecl
))
4604 /* Don't do ellipsis conversion for __built_in_constant_p
4605 as this will result in spurious errors for non-trivial
4607 val
= require_complete_type (val
, complain
);
4609 val
= convert_arg_to_ellipsis (val
, complain
);
4611 (**values
)[i
] = val
;
4615 typetail
= TREE_CHAIN (typetail
);
4618 if (typetail
!= 0 && typetail
!= void_list_node
)
4620 /* See if there are default arguments that can be used. Because
4621 we hold default arguments in the FUNCTION_TYPE (which is so
4622 wrong), we can see default parameters here from deduced
4623 contexts (and via typeof) for indirect function calls.
4624 Fortunately we know whether we have a function decl to
4625 provide default arguments in a language conformant
4627 if (fndecl
&& TREE_PURPOSE (typetail
)
4628 && TREE_CODE (TREE_PURPOSE (typetail
)) != DEFERRED_PARSE
)
4630 for (; typetail
!= void_list_node
; ++i
)
4632 /* After DR777, with explicit template args we can end up with a
4633 default argument followed by no default argument. */
4634 if (!TREE_PURPOSE (typetail
))
4637 = convert_default_arg (TREE_VALUE (typetail
),
4638 TREE_PURPOSE (typetail
),
4639 fndecl
, i
, complain
);
4641 if (parmval
== error_mark_node
)
4644 vec_safe_push (*values
, parmval
);
4645 typetail
= TREE_CHAIN (typetail
);
4646 /* ends with `...'. */
4647 if (typetail
== NULL_TREE
)
4652 if (typetail
&& typetail
!= void_list_node
)
4654 if (complain
& tf_error
)
4655 error_args_num (input_location
, fndecl
, /*too_many_p=*/false);
4663 /* Build a binary-operation expression, after performing default
4664 conversions on the operands. CODE is the kind of expression to
4665 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4666 are the tree codes which correspond to ARG1 and ARG2 when issuing
4667 warnings about possibly misplaced parentheses. They may differ
4668 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4669 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4670 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4671 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4672 ARG2_CODE as ERROR_MARK. */
4675 build_x_binary_op (const op_location_t
&loc
, enum tree_code code
, tree arg1
,
4676 enum tree_code arg1_code
, tree arg2
,
4677 enum tree_code arg2_code
, tree lookups
,
4678 tree
*overload_p
, tsubst_flags_t complain
)
4683 tree overload
= NULL_TREE
;
4688 if (processing_template_decl
)
4690 if (type_dependent_expression_p (arg1
)
4691 || type_dependent_expression_p (arg2
))
4693 expr
= build_min_nt_loc (loc
, code
, arg1
, arg2
);
4695 = build_dependent_operator_type (lookups
, code
, false);
4698 arg1
= build_non_dependent_expr (arg1
);
4699 arg2
= build_non_dependent_expr (arg2
);
4702 if (code
== DOTSTAR_EXPR
)
4703 expr
= build_m_component_ref (arg1
, arg2
, complain
);
4705 expr
= build_new_op (loc
, code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
,
4706 lookups
, &overload
, complain
);
4708 if (overload_p
!= NULL
)
4709 *overload_p
= overload
;
4711 /* Check for cases such as x+y<<z which users are likely to
4712 misinterpret. But don't warn about obj << x + y, since that is a
4713 common idiom for I/O. */
4714 if (warn_parentheses
4715 && (complain
& tf_warning
)
4716 && !processing_template_decl
4717 && !error_operand_p (arg1
)
4718 && !error_operand_p (arg2
)
4719 && (code
!= LSHIFT_EXPR
4720 || !CLASS_TYPE_P (TREE_TYPE (arg1
))))
4721 warn_about_parentheses (loc
, code
, arg1_code
, orig_arg1
,
4722 arg2_code
, orig_arg2
);
4724 if (processing_template_decl
&& expr
!= error_mark_node
)
4726 if (overload
!= NULL_TREE
)
4727 return (build_min_non_dep_op_overload
4728 (code
, expr
, overload
, orig_arg1
, orig_arg2
));
4730 return build_min_non_dep (code
, expr
, orig_arg1
, orig_arg2
);
4736 /* Build and return an ARRAY_REF expression. */
4739 build_x_array_ref (location_t loc
, tree arg1
, tree arg2
,
4740 tsubst_flags_t complain
)
4742 tree orig_arg1
= arg1
;
4743 tree orig_arg2
= arg2
;
4745 tree overload
= NULL_TREE
;
4747 if (processing_template_decl
)
4749 if (type_dependent_expression_p (arg1
)
4750 || type_dependent_expression_p (arg2
))
4751 return build_min_nt_loc (loc
, ARRAY_REF
, arg1
, arg2
,
4752 NULL_TREE
, NULL_TREE
);
4753 arg1
= build_non_dependent_expr (arg1
);
4754 arg2
= build_non_dependent_expr (arg2
);
4757 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, arg1
, arg2
,
4758 NULL_TREE
, NULL_TREE
, &overload
, complain
);
4760 if (processing_template_decl
&& expr
!= error_mark_node
)
4762 if (overload
!= NULL_TREE
)
4763 return (build_min_non_dep_op_overload
4764 (ARRAY_REF
, expr
, overload
, orig_arg1
, orig_arg2
));
4766 return build_min_non_dep (ARRAY_REF
, expr
, orig_arg1
, orig_arg2
,
4767 NULL_TREE
, NULL_TREE
);
4772 /* Return whether OP is an expression of enum type cast to integer
4773 type. In C++ even unsigned enum types are cast to signed integer
4774 types. We do not want to issue warnings about comparisons between
4775 signed and unsigned types when one of the types is an enum type.
4776 Those warnings are always false positives in practice. */
4779 enum_cast_to_int (tree op
)
4781 if (CONVERT_EXPR_P (op
)
4782 && TREE_TYPE (op
) == integer_type_node
4783 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == ENUMERAL_TYPE
4784 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op
, 0))))
4787 /* The cast may have been pushed into a COND_EXPR. */
4788 if (TREE_CODE (op
) == COND_EXPR
)
4789 return (enum_cast_to_int (TREE_OPERAND (op
, 1))
4790 || enum_cast_to_int (TREE_OPERAND (op
, 2)));
4795 /* For the c-common bits. */
4797 build_binary_op (location_t location
, enum tree_code code
, tree op0
, tree op1
,
4800 return cp_build_binary_op (location
, code
, op0
, op1
, tf_warning_or_error
);
4803 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4804 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4807 build_vec_cmp (tree_code code
, tree type
,
4808 tree arg0
, tree arg1
)
4810 tree zero_vec
= build_zero_cst (type
);
4811 tree minus_one_vec
= build_minus_one_cst (type
);
4812 tree cmp_type
= truth_type_for (type
);
4813 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
4814 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
4817 /* Possibly warn about an address never being NULL. */
4820 warn_for_null_address (location_t location
, tree op
, tsubst_flags_t complain
)
4822 /* Prevent warnings issued for macro expansion. */
4824 || (complain
& tf_warning
) == 0
4825 || c_inhibit_evaluation_warnings
!= 0
4826 || from_macro_expansion_at (location
)
4827 || warning_suppressed_p (op
, OPT_Waddress
))
4830 if (TREE_CODE (op
) == NON_DEPENDENT_EXPR
)
4831 op
= TREE_OPERAND (op
, 0);
4833 tree cop
= fold_for_warn (op
);
4835 if (TREE_CODE (cop
) == NON_LVALUE_EXPR
)
4836 /* Unwrap the expression for C++ 98. */
4837 cop
= TREE_OPERAND (cop
, 0);
4839 if (TREE_CODE (cop
) == PTRMEM_CST
)
4841 /* The address of a nonstatic data member is never null. */
4842 warning_at (location
, OPT_Waddress
,
4843 "the address %qE will never be NULL",
4848 if (TREE_CODE (cop
) == NOP_EXPR
)
4850 /* Allow casts to intptr_t to suppress the warning. */
4851 tree type
= TREE_TYPE (cop
);
4852 if (TREE_CODE (type
) == INTEGER_TYPE
)
4858 bool warned
= false;
4859 if (TREE_CODE (cop
) == ADDR_EXPR
)
4861 cop
= TREE_OPERAND (cop
, 0);
4863 /* Set to true in the loop below if OP dereferences its operand.
4864 In such a case the ultimate target need not be a decl for
4865 the null [in]equality test to be necessarily constant. */
4868 /* Get the outermost array or object, or member. */
4869 while (handled_component_p (cop
))
4871 if (TREE_CODE (cop
) == COMPONENT_REF
)
4873 /* Get the member (its address is never null). */
4874 cop
= TREE_OPERAND (cop
, 1);
4878 /* Get the outer array/object to refer to in the warning. */
4879 cop
= TREE_OPERAND (cop
, 0);
4883 if ((!deref
&& !decl_with_nonnull_addr_p (cop
))
4884 || from_macro_expansion_at (location
)
4885 || warning_suppressed_p (cop
, OPT_Waddress
))
4888 warned
= warning_at (location
, OPT_Waddress
,
4889 "the address of %qD will never be NULL", cop
);
4892 else if (TREE_CODE (cop
) == POINTER_PLUS_EXPR
)
4894 /* Adding zero to the null pointer is well-defined in C++. When
4895 the offset is unknown (i.e., not a constant) warn anyway since
4896 it's less likely that the pointer operand is null than not. */
4897 tree off
= TREE_OPERAND (cop
, 1);
4898 if (!integer_zerop (off
)
4899 && !warning_suppressed_p (cop
, OPT_Waddress
))
4901 tree base
= TREE_OPERAND (cop
, 0);
4903 if (TYPE_REF_P (TREE_TYPE (base
)))
4904 warning_at (location
, OPT_Waddress
, "the compiler can assume that "
4905 "the address of %qE will never be NULL", base
);
4907 warning_at (location
, OPT_Waddress
, "comparing the result of "
4908 "pointer addition %qE and NULL", cop
);
4912 else if (CONVERT_EXPR_P (op
)
4913 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op
, 0))))
4917 if (TREE_CODE (op
) == COMPONENT_REF
)
4918 op
= TREE_OPERAND (op
, 1);
4921 warned
= warning_at (location
, OPT_Waddress
,
4922 "the compiler can assume that the address of "
4923 "%qD will never be NULL", op
);
4926 if (warned
&& DECL_P (op
))
4927 inform (DECL_SOURCE_LOCATION (op
), "%qD declared here", op
);
4930 /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
4931 the other operand is of a different enumeration type or a floating-point
4932 type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
4933 code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
4934 and LOC is the location for the whole binary expression.
4935 TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
4938 do_warn_enum_conversions (location_t loc
, enum tree_code code
, tree type0
,
4941 if (TREE_CODE (type0
) == ENUMERAL_TYPE
4942 && TREE_CODE (type1
) == ENUMERAL_TYPE
4943 && TYPE_MAIN_VARIANT (type0
) != TYPE_MAIN_VARIANT (type1
))
4945 /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
4946 Otherwise, warn if -Wenum-conversion is on. */
4948 if (warn_deprecated_enum_enum_conv
)
4949 opt
= OPT_Wdeprecated_enum_enum_conversion
;
4950 else if (warn_enum_conversion
)
4951 opt
= OPT_Wenum_conversion
;
4963 /* Comparisons are handled by -Wenum-compare. */
4965 case SPACESHIP_EXPR
:
4966 /* This is invalid, don't warn. */
4971 warning_at (loc
, opt
, "bitwise operation between different "
4972 "enumeration types %qT and %qT is deprecated",
4976 warning_at (loc
, opt
, "arithmetic between different enumeration "
4977 "types %qT and %qT is deprecated", type0
, type1
);
4981 else if ((TREE_CODE (type0
) == ENUMERAL_TYPE
4982 && TREE_CODE (type1
) == REAL_TYPE
)
4983 || (TREE_CODE (type0
) == REAL_TYPE
4984 && TREE_CODE (type1
) == ENUMERAL_TYPE
))
4986 const bool enum_first_p
= TREE_CODE (type0
) == ENUMERAL_TYPE
;
4987 /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
4988 Otherwise, warn if -Wenum-conversion is on. */
4990 if (warn_deprecated_enum_float_conv
)
4991 opt
= OPT_Wdeprecated_enum_float_conversion
;
4992 else if (warn_enum_conversion
)
4993 opt
= OPT_Wenum_conversion
;
5006 warning_at (loc
, opt
, "comparison of enumeration type %qT with "
5007 "floating-point type %qT is deprecated",
5010 warning_at (loc
, opt
, "comparison of floating-point type %qT "
5011 "with enumeration type %qT is deprecated",
5014 case SPACESHIP_EXPR
:
5015 /* This is invalid, don't warn. */
5019 warning_at (loc
, opt
, "arithmetic between enumeration type %qT "
5020 "and floating-point type %qT is deprecated",
5023 warning_at (loc
, opt
, "arithmetic between floating-point type %qT "
5024 "and enumeration type %qT is deprecated",
5031 /* Build a binary-operation expression without default conversions.
5032 CODE is the kind of expression to build.
5033 LOCATION is the location_t of the operator in the source code.
5034 This function differs from `build' in several ways:
5035 the data type of the result is computed and recorded in it,
5036 warnings are generated if arg data types are invalid,
5037 special handling for addition and subtraction of pointers is known,
5038 and some optimization is done (operations on narrow ints
5039 are done in the narrower type when that gives the same result).
5040 Constant folding is also done before the result is returned.
5042 Note that the operands will never have enumeral types
5043 because either they have just had the default conversions performed
5044 or they have both just been converted to some other type in which
5045 the arithmetic is to be done.
5047 C++: must do special pointer arithmetic when implementing
5048 multiple inheritance, and deal with pointer to member functions. */
5051 cp_build_binary_op (const op_location_t
&location
,
5052 enum tree_code code
, tree orig_op0
, tree orig_op1
,
5053 tsubst_flags_t complain
)
5056 enum tree_code code0
, code1
;
5058 const char *invalid_op_diag
;
5060 /* Expression code to give to the expression when it is built.
5061 Normally this is CODE, which is what the caller asked for,
5062 but in some special cases we change it. */
5063 enum tree_code resultcode
= code
;
5065 /* Data type in which the computation is to be performed.
5066 In the simplest cases this is the common type of the arguments. */
5067 tree result_type
= NULL_TREE
;
5069 /* Nonzero means operands have already been type-converted
5070 in whatever way is necessary.
5071 Zero means they need to be converted to RESULT_TYPE. */
5074 /* Nonzero means create the expression with this type, rather than
5076 tree build_type
= 0;
5078 /* Nonzero means after finally constructing the expression
5079 convert it to this type. */
5080 tree final_type
= 0;
5084 /* Nonzero if this is an operation like MIN or MAX which can
5085 safely be computed in short if both args are promoted shorts.
5086 Also implies COMMON.
5087 -1 indicates a bitwise operation; this makes a difference
5088 in the exact conditions for when it is safe to do the operation
5089 in a narrower mode. */
5092 /* Nonzero if this is a comparison operation;
5093 if both args are promoted shorts, compare the original shorts.
5094 Also implies COMMON. */
5095 int short_compare
= 0;
5097 /* Nonzero if this is a right-shift operation, which can be computed on the
5098 original short and then promoted if the operand is a promoted short. */
5099 int short_shift
= 0;
5101 /* Nonzero means set RESULT_TYPE to the common type of the args. */
5104 /* True if both operands have arithmetic type. */
5105 bool arithmetic_types_p
;
5107 /* Remember whether we're doing / or %. */
5108 bool doing_div_or_mod
= false;
5110 /* Remember whether we're doing << or >>. */
5111 bool doing_shift
= false;
5113 /* Tree holding instrumentation expression. */
5114 tree instrument_expr
= NULL_TREE
;
5116 /* Apply default conversions. */
5117 op0
= resolve_nondeduced_context (orig_op0
, complain
);
5118 op1
= resolve_nondeduced_context (orig_op1
, complain
);
5120 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
5121 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
5122 || code
== TRUTH_XOR_EXPR
)
5124 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
5125 op0
= decay_conversion (op0
, complain
);
5126 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
5127 op1
= decay_conversion (op1
, complain
);
5131 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
5132 op0
= cp_default_conversion (op0
, complain
);
5133 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
5134 op1
= cp_default_conversion (op1
, complain
);
5137 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
5138 STRIP_TYPE_NOPS (op0
);
5139 STRIP_TYPE_NOPS (op1
);
5141 /* DTRT if one side is an overloaded function, but complain about it. */
5142 if (type_unknown_p (op0
))
5144 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
5145 if (t
!= error_mark_node
)
5147 if (complain
& tf_error
)
5148 permerror (location
,
5149 "assuming cast to type %qT from overloaded function",
5154 if (type_unknown_p (op1
))
5156 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
5157 if (t
!= error_mark_node
)
5159 if (complain
& tf_error
)
5160 permerror (location
,
5161 "assuming cast to type %qT from overloaded function",
5167 type0
= TREE_TYPE (op0
);
5168 type1
= TREE_TYPE (op1
);
5170 /* The expression codes of the data types of the arguments tell us
5171 whether the arguments are integers, floating, pointers, etc. */
5172 code0
= TREE_CODE (type0
);
5173 code1
= TREE_CODE (type1
);
5175 /* If an error was already reported for one of the arguments,
5176 avoid reporting another error. */
5177 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
5178 return error_mark_node
;
5180 if ((invalid_op_diag
5181 = targetm
.invalid_binary_op (code
, type0
, type1
)))
5183 if (complain
& tf_error
)
5185 if (code0
== REAL_TYPE
5186 && code1
== REAL_TYPE
5187 && (extended_float_type_p (type0
)
5188 || extended_float_type_p (type1
))
5189 && cp_compare_floating_point_conversion_ranks (type0
,
5192 rich_location
richloc (line_table
, location
);
5193 binary_op_error (&richloc
, code
, type0
, type1
);
5196 error (invalid_op_diag
);
5198 return error_mark_node
;
5201 /* Issue warnings about peculiar, but valid, uses of NULL. */
5202 if ((null_node_p (orig_op0
) || null_node_p (orig_op1
))
5203 /* It's reasonable to use pointer values as operands of &&
5204 and ||, so NULL is no exception. */
5205 && code
!= TRUTH_ANDIF_EXPR
&& code
!= TRUTH_ORIF_EXPR
5206 && ( /* Both are NULL (or 0) and the operation was not a
5207 comparison or a pointer subtraction. */
5208 (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)
5209 && code
!= EQ_EXPR
&& code
!= NE_EXPR
&& code
!= MINUS_EXPR
)
5210 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5211 || (!null_ptr_cst_p (orig_op0
)
5212 && !TYPE_PTR_OR_PTRMEM_P (type0
))
5213 || (!null_ptr_cst_p (orig_op1
)
5214 && !TYPE_PTR_OR_PTRMEM_P (type1
)))
5215 && (complain
& tf_warning
))
5218 expansion_point_location_if_in_system_header (input_location
);
5220 warning_at (loc
, OPT_Wpointer_arith
, "NULL used in arithmetic");
5223 /* In case when one of the operands of the binary operation is
5224 a vector and another is a scalar -- convert scalar to vector. */
5225 if ((gnu_vector_type_p (type0
) && code1
!= VECTOR_TYPE
)
5226 || (gnu_vector_type_p (type1
) && code0
!= VECTOR_TYPE
))
5228 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
5229 complain
& tf_error
);
5231 switch (convert_flag
)
5234 return error_mark_node
;
5237 op0
= convert (TREE_TYPE (type1
), op0
);
5238 op0
= save_expr (op0
);
5239 op0
= build_vector_from_val (type1
, op0
);
5240 type0
= TREE_TYPE (op0
);
5241 code0
= TREE_CODE (type0
);
5247 op1
= convert (TREE_TYPE (type0
), op1
);
5248 op1
= save_expr (op1
);
5249 op1
= build_vector_from_val (type0
, op1
);
5250 type1
= TREE_TYPE (op1
);
5251 code1
= TREE_CODE (type1
);
5263 /* Subtraction of two similar pointers.
5264 We must subtract them as integers, then divide by object size. */
5265 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
5266 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
5269 result
= pointer_diff (location
, op0
, op1
,
5270 common_pointer_type (type0
, type1
), complain
,
5272 if (instrument_expr
!= NULL
)
5273 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
5274 instrument_expr
, result
);
5278 /* In all other cases except pointer - int, the usual arithmetic
5280 else if (!(code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
))
5285 /* The pointer - int case is just like pointer + int; fall
5289 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
5290 && (code0
== INTEGER_TYPE
|| code1
== INTEGER_TYPE
))
5294 ptr_operand
= ((code0
== POINTER_TYPE
) ? op0
: op1
);
5295 int_operand
= ((code0
== INTEGER_TYPE
) ? op0
: op1
);
5296 if (processing_template_decl
)
5298 result_type
= TREE_TYPE (ptr_operand
);
5301 return cp_pointer_int_sum (location
, code
,
5313 case TRUNC_DIV_EXPR
:
5315 case FLOOR_DIV_EXPR
:
5316 case ROUND_DIV_EXPR
:
5317 case EXACT_DIV_EXPR
:
5318 if (TREE_CODE (op0
) == SIZEOF_EXPR
&& TREE_CODE (op1
) == SIZEOF_EXPR
)
5320 tree type0
= TREE_OPERAND (op0
, 0);
5321 tree type1
= TREE_OPERAND (op1
, 0);
5322 tree first_arg
= tree_strip_any_location_wrapper (type0
);
5323 if (!TYPE_P (type0
))
5324 type0
= TREE_TYPE (type0
);
5325 if (!TYPE_P (type1
))
5326 type1
= TREE_TYPE (type1
);
5327 if (INDIRECT_TYPE_P (type0
) && same_type_p (TREE_TYPE (type0
), type1
))
5329 if (!(TREE_CODE (first_arg
) == PARM_DECL
5330 && DECL_ARRAY_PARAMETER_P (first_arg
)
5331 && warn_sizeof_array_argument
)
5332 && (complain
& tf_warning
))
5334 auto_diagnostic_group d
;
5335 if (warning_at (location
, OPT_Wsizeof_pointer_div
,
5336 "division %<sizeof (%T) / sizeof (%T)%> does "
5337 "not compute the number of array elements",
5339 if (DECL_P (first_arg
))
5340 inform (DECL_SOURCE_LOCATION (first_arg
),
5341 "first %<sizeof%> operand was declared here");
5344 else if (TREE_CODE (type0
) == ARRAY_TYPE
5345 && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0
)))
5346 /* Set by finish_parenthesized_expr. */
5347 && !warning_suppressed_p (op1
, OPT_Wsizeof_array_div
)
5348 && (complain
& tf_warning
))
5349 maybe_warn_sizeof_array_div (location
, first_arg
, type0
,
5350 op1
, non_reference (type1
));
5353 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
5354 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
5355 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5356 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
5358 enum tree_code tcode0
= code0
, tcode1
= code1
;
5359 doing_div_or_mod
= true;
5360 warn_for_div_by_zero (location
, fold_for_warn (op1
));
5362 if (tcode0
== COMPLEX_TYPE
|| tcode0
== VECTOR_TYPE
)
5363 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
5364 if (tcode1
== COMPLEX_TYPE
|| tcode1
== VECTOR_TYPE
)
5365 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
5367 if (!(tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
))
5368 resultcode
= RDIV_EXPR
;
5371 /* When dividing two signed integers, we have to promote to int.
5372 unless we divide by a constant != -1. Note that default
5373 conversion will have been performed on the operands at this
5374 point, so we have to dig out the original type to find out if
5376 tree stripped_op1
= tree_strip_any_location_wrapper (op1
);
5377 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
5378 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
5379 || (TREE_CODE (stripped_op1
) == INTEGER_CST
5380 && ! integer_all_onesp (stripped_op1
)));
5390 if ((code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
5391 || (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
5392 && !VECTOR_FLOAT_TYPE_P (type0
)
5393 && !VECTOR_FLOAT_TYPE_P (type1
)))
5397 case TRUNC_MOD_EXPR
:
5398 case FLOOR_MOD_EXPR
:
5399 doing_div_or_mod
= true;
5400 warn_for_div_by_zero (location
, fold_for_warn (op1
));
5402 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
5403 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
5404 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
5406 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
5408 /* Although it would be tempting to shorten always here, that loses
5409 on some targets, since the modulo instruction is undefined if the
5410 quotient can't be represented in the computation mode. We shorten
5411 only if unsigned or if dividing by something we know != -1. */
5412 tree stripped_op1
= tree_strip_any_location_wrapper (op1
);
5413 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
5414 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
5415 || (TREE_CODE (stripped_op1
) == INTEGER_CST
5416 && ! integer_all_onesp (stripped_op1
)));
5421 case TRUTH_ANDIF_EXPR
:
5422 case TRUTH_ORIF_EXPR
:
5423 case TRUTH_AND_EXPR
:
5425 if (!VECTOR_TYPE_P (type0
) && gnu_vector_type_p (type1
))
5427 if (!COMPARISON_CLASS_P (op1
))
5428 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
5429 build_zero_cst (type1
), complain
);
5430 if (code
== TRUTH_ANDIF_EXPR
)
5432 tree z
= build_zero_cst (TREE_TYPE (op1
));
5433 return build_conditional_expr (location
, op0
, op1
, z
, complain
);
5435 else if (code
== TRUTH_ORIF_EXPR
)
5437 tree m1
= build_all_ones_cst (TREE_TYPE (op1
));
5438 return build_conditional_expr (location
, op0
, m1
, op1
, complain
);
5443 if (gnu_vector_type_p (type0
)
5444 && (!VECTOR_TYPE_P (type1
) || gnu_vector_type_p (type1
)))
5446 if (!COMPARISON_CLASS_P (op0
))
5447 op0
= cp_build_binary_op (EXPR_LOCATION (op0
), NE_EXPR
, op0
,
5448 build_zero_cst (type0
), complain
);
5449 if (!VECTOR_TYPE_P (type1
))
5451 tree m1
= build_all_ones_cst (TREE_TYPE (op0
));
5452 tree z
= build_zero_cst (TREE_TYPE (op0
));
5453 op1
= build_conditional_expr (location
, op1
, m1
, z
, complain
);
5455 else if (!COMPARISON_CLASS_P (op1
))
5456 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
5457 build_zero_cst (type1
), complain
);
5459 if (code
== TRUTH_ANDIF_EXPR
)
5460 code
= BIT_AND_EXPR
;
5461 else if (code
== TRUTH_ORIF_EXPR
)
5462 code
= BIT_IOR_EXPR
;
5466 return cp_build_binary_op (location
, code
, op0
, op1
, complain
);
5469 result_type
= boolean_type_node
;
5472 /* Shift operations: result has same type as first operand;
5473 always convert second operand to int.
5474 Also set SHORT_SHIFT if shifting rightward. */
5477 if (gnu_vector_type_p (type0
)
5478 && code1
== INTEGER_TYPE
5479 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
5481 result_type
= type0
;
5484 else if (gnu_vector_type_p (type0
)
5485 && gnu_vector_type_p (type1
)
5486 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
5487 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
5488 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
5489 TYPE_VECTOR_SUBPARTS (type1
)))
5491 result_type
= type0
;
5494 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
5496 tree const_op1
= fold_for_warn (op1
);
5497 if (TREE_CODE (const_op1
) != INTEGER_CST
)
5499 result_type
= type0
;
5501 if (TREE_CODE (const_op1
) == INTEGER_CST
)
5503 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
5505 if ((complain
& tf_warning
)
5506 && c_inhibit_evaluation_warnings
== 0)
5507 warning_at (location
, OPT_Wshift_count_negative
,
5508 "right shift count is negative");
5512 if (!integer_zerop (const_op1
))
5515 if (compare_tree_int (const_op1
, TYPE_PRECISION (type0
)) >= 0
5516 && (complain
& tf_warning
)
5517 && c_inhibit_evaluation_warnings
== 0)
5518 warning_at (location
, OPT_Wshift_count_overflow
,
5519 "right shift count >= width of type");
5522 /* Avoid converting op1 to result_type later. */
5528 if (gnu_vector_type_p (type0
)
5529 && code1
== INTEGER_TYPE
5530 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
5532 result_type
= type0
;
5535 else if (gnu_vector_type_p (type0
)
5536 && gnu_vector_type_p (type1
)
5537 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
5538 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
5539 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
5540 TYPE_VECTOR_SUBPARTS (type1
)))
5542 result_type
= type0
;
5545 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
5547 tree const_op0
= fold_for_warn (op0
);
5548 if (TREE_CODE (const_op0
) != INTEGER_CST
)
5550 tree const_op1
= fold_for_warn (op1
);
5551 if (TREE_CODE (const_op1
) != INTEGER_CST
)
5553 result_type
= type0
;
5555 if (TREE_CODE (const_op0
) == INTEGER_CST
5556 && tree_int_cst_sgn (const_op0
) < 0
5557 && !TYPE_OVERFLOW_WRAPS (type0
)
5558 && (complain
& tf_warning
)
5559 && c_inhibit_evaluation_warnings
== 0)
5560 warning_at (location
, OPT_Wshift_negative_value
,
5561 "left shift of negative value");
5562 if (TREE_CODE (const_op1
) == INTEGER_CST
)
5564 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
5566 if ((complain
& tf_warning
)
5567 && c_inhibit_evaluation_warnings
== 0)
5568 warning_at (location
, OPT_Wshift_count_negative
,
5569 "left shift count is negative");
5571 else if (compare_tree_int (const_op1
,
5572 TYPE_PRECISION (type0
)) >= 0)
5574 if ((complain
& tf_warning
)
5575 && c_inhibit_evaluation_warnings
== 0)
5576 warning_at (location
, OPT_Wshift_count_overflow
,
5577 "left shift count >= width of type");
5579 else if (TREE_CODE (const_op0
) == INTEGER_CST
5580 && (complain
& tf_warning
))
5581 maybe_warn_shift_overflow (location
, const_op0
, const_op1
);
5583 /* Avoid converting op1 to result_type later. */
5590 if (gnu_vector_type_p (type0
) && gnu_vector_type_p (type1
))
5591 goto vector_compare
;
5592 if ((complain
& tf_warning
)
5593 && c_inhibit_evaluation_warnings
== 0
5594 && (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
)))
5595 warning_at (location
, OPT_Wfloat_equal
,
5596 "comparing floating-point with %<==%> "
5597 "or %<!=%> is unsafe");
5598 if (complain
& tf_warning
)
5600 tree stripped_orig_op0
= tree_strip_any_location_wrapper (orig_op0
);
5601 tree stripped_orig_op1
= tree_strip_any_location_wrapper (orig_op1
);
5602 if ((TREE_CODE (stripped_orig_op0
) == STRING_CST
5603 && !integer_zerop (cp_fully_fold (op1
)))
5604 || (TREE_CODE (stripped_orig_op1
) == STRING_CST
5605 && !integer_zerop (cp_fully_fold (op0
))))
5606 warning_at (location
, OPT_Waddress
,
5607 "comparison with string literal results in "
5608 "unspecified behavior");
5609 else if (warn_array_compare
5610 && TREE_CODE (TREE_TYPE (orig_op0
)) == ARRAY_TYPE
5611 && TREE_CODE (TREE_TYPE (orig_op1
)) == ARRAY_TYPE
)
5612 do_warn_array_compare (location
, code
, stripped_orig_op0
,
5616 build_type
= boolean_type_node
;
5617 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
5618 || code0
== COMPLEX_TYPE
|| code0
== ENUMERAL_TYPE
)
5619 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5620 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
))
5622 else if (((code0
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type0
))
5623 && null_ptr_cst_p (orig_op1
))
5624 /* Handle, eg, (void*)0 (c++/43906), and more. */
5625 || (code0
== POINTER_TYPE
5626 && TYPE_PTR_P (type1
) && integer_zerop (op1
)))
5628 if (TYPE_PTR_P (type1
))
5629 result_type
= composite_pointer_type (location
,
5630 type0
, type1
, op0
, op1
,
5631 CPO_COMPARISON
, complain
);
5633 result_type
= type0
;
5635 if (char_type_p (TREE_TYPE (orig_op1
)))
5637 auto_diagnostic_group d
;
5638 if (warning_at (location
, OPT_Wpointer_compare
,
5639 "comparison between pointer and zero character "
5642 "did you mean to dereference the pointer?");
5644 warn_for_null_address (location
, op0
, complain
);
5646 else if (((code1
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type1
))
5647 && null_ptr_cst_p (orig_op0
))
5648 /* Handle, eg, (void*)0 (c++/43906), and more. */
5649 || (code1
== POINTER_TYPE
5650 && TYPE_PTR_P (type0
) && integer_zerop (op0
)))
5652 if (TYPE_PTR_P (type0
))
5653 result_type
= composite_pointer_type (location
,
5654 type0
, type1
, op0
, op1
,
5655 CPO_COMPARISON
, complain
);
5657 result_type
= type1
;
5659 if (char_type_p (TREE_TYPE (orig_op0
)))
5661 auto_diagnostic_group d
;
5662 if (warning_at (location
, OPT_Wpointer_compare
,
5663 "comparison between pointer and zero character "
5666 "did you mean to dereference the pointer?");
5668 warn_for_null_address (location
, op1
, complain
);
5670 else if ((code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5671 || (TYPE_PTRDATAMEM_P (type0
) && TYPE_PTRDATAMEM_P (type1
)))
5672 result_type
= composite_pointer_type (location
,
5673 type0
, type1
, op0
, op1
,
5674 CPO_COMPARISON
, complain
);
5675 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
5676 /* One of the operands must be of nullptr_t type. */
5677 result_type
= TREE_TYPE (nullptr_node
);
5678 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5680 result_type
= type0
;
5681 if (complain
& tf_error
)
5682 permerror (location
, "ISO C++ forbids comparison between "
5683 "pointer and integer");
5685 return error_mark_node
;
5687 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
5689 result_type
= type1
;
5690 if (complain
& tf_error
)
5691 permerror (location
, "ISO C++ forbids comparison between "
5692 "pointer and integer");
5694 return error_mark_node
;
5696 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (orig_op1
))
5698 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5699 == ptrmemfunc_vbit_in_delta
)
5701 tree pfn0
, delta0
, e1
, e2
;
5703 if (TREE_SIDE_EFFECTS (op0
))
5704 op0
= cp_save_expr (op0
);
5706 pfn0
= pfn_from_ptrmemfunc (op0
);
5707 delta0
= delta_from_ptrmemfunc (op0
);
5708 e1
= cp_build_binary_op (location
,
5711 build_zero_cst (TREE_TYPE (pfn0
)),
5713 e2
= cp_build_binary_op (location
,
5719 if (complain
& tf_warning
)
5720 maybe_warn_zero_as_null_pointer_constant (op1
, input_location
);
5722 e2
= cp_build_binary_op (location
,
5723 EQ_EXPR
, e2
, integer_zero_node
,
5725 op0
= cp_build_binary_op (location
,
5726 TRUTH_ANDIF_EXPR
, e1
, e2
,
5728 op1
= cp_convert (TREE_TYPE (op0
), integer_one_node
, complain
);
5732 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
5733 op1
= cp_convert (TREE_TYPE (op0
), op1
, complain
);
5735 result_type
= TREE_TYPE (op0
);
5737 warn_for_null_address (location
, orig_op0
, complain
);
5739 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (orig_op0
))
5740 return cp_build_binary_op (location
, code
, op1
, op0
, complain
);
5741 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
))
5744 /* E will be the final comparison. */
5746 /* E1 and E2 are for scratch. */
5754 type
= composite_pointer_type (location
, type0
, type1
, op0
, op1
,
5755 CPO_COMPARISON
, complain
);
5757 if (!same_type_p (TREE_TYPE (op0
), type
))
5758 op0
= cp_convert_and_check (type
, op0
, complain
);
5759 if (!same_type_p (TREE_TYPE (op1
), type
))
5760 op1
= cp_convert_and_check (type
, op1
, complain
);
5762 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5763 return error_mark_node
;
5765 if (TREE_SIDE_EFFECTS (op0
))
5766 op0
= save_expr (op0
);
5767 if (TREE_SIDE_EFFECTS (op1
))
5768 op1
= save_expr (op1
);
5770 pfn0
= pfn_from_ptrmemfunc (op0
);
5771 pfn0
= cp_fully_fold (pfn0
);
5772 /* Avoid -Waddress warnings (c++/64877). */
5773 if (TREE_CODE (pfn0
) == ADDR_EXPR
)
5774 suppress_warning (pfn0
, OPT_Waddress
);
5775 pfn1
= pfn_from_ptrmemfunc (op1
);
5776 pfn1
= cp_fully_fold (pfn1
);
5777 delta0
= delta_from_ptrmemfunc (op0
);
5778 delta1
= delta_from_ptrmemfunc (op1
);
5779 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5780 == ptrmemfunc_vbit_in_delta
)
5785 && ((op0.delta == op1.delta)
5786 || (!op0.pfn && op0.delta & 1 == 0
5787 && op1.delta & 1 == 0))
5789 The reason for the `!op0.pfn' bit is that a NULL
5790 pointer-to-member is any member with a zero PFN and
5791 LSB of the DELTA field is 0. */
5793 e1
= cp_build_binary_op (location
, BIT_AND_EXPR
,
5797 e1
= cp_build_binary_op (location
,
5798 EQ_EXPR
, e1
, integer_zero_node
,
5800 e2
= cp_build_binary_op (location
, BIT_AND_EXPR
,
5804 e2
= cp_build_binary_op (location
,
5805 EQ_EXPR
, e2
, integer_zero_node
,
5807 e1
= cp_build_binary_op (location
,
5808 TRUTH_ANDIF_EXPR
, e2
, e1
,
5810 e2
= cp_build_binary_op (location
, EQ_EXPR
,
5812 build_zero_cst (TREE_TYPE (pfn0
)),
5814 e2
= cp_build_binary_op (location
,
5815 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
5816 e1
= cp_build_binary_op (location
,
5817 EQ_EXPR
, delta0
, delta1
, complain
);
5818 e1
= cp_build_binary_op (location
,
5819 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
5826 && (!op0.pfn || op0.delta == op1.delta))
5828 The reason for the `!op0.pfn' bit is that a NULL
5829 pointer-to-member is any member with a zero PFN; the
5830 DELTA field is unspecified. */
5832 e1
= cp_build_binary_op (location
,
5833 EQ_EXPR
, delta0
, delta1
, complain
);
5834 e2
= cp_build_binary_op (location
,
5837 build_zero_cst (TREE_TYPE (pfn0
)),
5839 e1
= cp_build_binary_op (location
,
5840 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
5842 e2
= build2 (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
5843 e
= cp_build_binary_op (location
,
5844 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
5845 if (code
== EQ_EXPR
)
5847 return cp_build_binary_op (location
,
5848 EQ_EXPR
, e
, integer_zero_node
, complain
);
5852 gcc_assert (!TYPE_PTRMEMFUNC_P (type0
)
5853 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
),
5855 gcc_assert (!TYPE_PTRMEMFUNC_P (type1
)
5856 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
),
5864 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
5865 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
5867 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5868 result_type
= composite_pointer_type (location
,
5869 type0
, type1
, op0
, op1
,
5870 CPO_COMPARISON
, complain
);
5877 case SPACESHIP_EXPR
:
5878 if (TREE_CODE (orig_op0
) == STRING_CST
5879 || TREE_CODE (orig_op1
) == STRING_CST
)
5881 if (complain
& tf_warning
)
5882 warning_at (location
, OPT_Waddress
,
5883 "comparison with string literal results "
5884 "in unspecified behavior");
5886 else if (warn_array_compare
5887 && TREE_CODE (TREE_TYPE (orig_op0
)) == ARRAY_TYPE
5888 && TREE_CODE (TREE_TYPE (orig_op1
)) == ARRAY_TYPE
5889 && code
!= SPACESHIP_EXPR
5890 && (complain
& tf_warning
))
5891 do_warn_array_compare (location
, code
,
5892 tree_strip_any_location_wrapper (orig_op0
),
5893 tree_strip_any_location_wrapper (orig_op1
));
5895 if (gnu_vector_type_p (type0
) && gnu_vector_type_p (type1
))
5899 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
5901 && !vector_types_compatible_elements_p (type0
, type1
))
5903 if (complain
& tf_error
)
5905 error_at (location
, "comparing vectors with different "
5907 inform (location
, "operand types are %qT and %qT",
5910 return error_mark_node
;
5913 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0
),
5914 TYPE_VECTOR_SUBPARTS (type1
)))
5916 if (complain
& tf_error
)
5918 error_at (location
, "comparing vectors with different "
5919 "number of elements");
5920 inform (location
, "operand types are %qT and %qT",
5923 return error_mark_node
;
5926 /* It's not precisely specified how the usual arithmetic
5927 conversions apply to the vector types. Here, we use
5928 the unsigned type if one of the operands is signed and
5929 the other one is unsigned. */
5930 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
5932 if (!TYPE_UNSIGNED (type0
))
5933 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
5935 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
5936 warning_at (location
, OPT_Wsign_compare
, "comparison between "
5937 "types %qT and %qT", type0
, type1
);
5940 if (resultcode
== SPACESHIP_EXPR
)
5942 if (complain
& tf_error
)
5943 sorry_at (location
, "three-way comparison of vectors");
5944 return error_mark_node
;
5947 /* Always construct signed integer vector type. */
5948 intt
= c_common_type_for_size
5949 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0
))), 0);
5952 if (complain
& tf_error
)
5953 error_at (location
, "could not find an integer type "
5954 "of the same size as %qT", TREE_TYPE (type0
));
5955 return error_mark_node
;
5957 result_type
= build_opaque_vector_type (intt
,
5958 TYPE_VECTOR_SUBPARTS (type0
));
5959 return build_vec_cmp (resultcode
, result_type
, op0
, op1
);
5961 build_type
= boolean_type_node
;
5962 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
5963 || code0
== ENUMERAL_TYPE
)
5964 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5965 || code1
== ENUMERAL_TYPE
))
5967 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5968 result_type
= composite_pointer_type (location
,
5969 type0
, type1
, op0
, op1
,
5970 CPO_COMPARISON
, complain
);
5971 else if ((code0
== POINTER_TYPE
&& null_ptr_cst_p (orig_op1
))
5972 || (code1
== POINTER_TYPE
&& null_ptr_cst_p (orig_op0
))
5973 || (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)))
5975 /* Core Issue 1512 made this ill-formed. */
5976 if (complain
& tf_error
)
5977 error_at (location
, "ordered comparison of pointer with "
5978 "integer zero (%qT and %qT)", type0
, type1
);
5979 return error_mark_node
;
5981 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5983 result_type
= type0
;
5984 if (complain
& tf_error
)
5985 permerror (location
, "ISO C++ forbids comparison between "
5986 "pointer and integer");
5988 return error_mark_node
;
5990 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
5992 result_type
= type1
;
5993 if (complain
& tf_error
)
5994 permerror (location
, "ISO C++ forbids comparison between "
5995 "pointer and integer");
5997 return error_mark_node
;
6000 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
6001 && !processing_template_decl
6002 && sanitize_flags_p (SANITIZE_POINTER_COMPARE
))
6004 op0
= save_expr (op0
);
6005 op1
= save_expr (op1
);
6007 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE
);
6008 instrument_expr
= build_call_expr_loc (location
, tt
, 2, op0
, op1
);
6013 case UNORDERED_EXPR
:
6020 build_type
= integer_type_node
;
6021 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
6023 if (complain
& tf_error
)
6024 error ("unordered comparison on non-floating-point argument");
6025 return error_mark_node
;
6034 if (((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
6035 || code0
== ENUMERAL_TYPE
)
6036 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
6037 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
)))
6038 arithmetic_types_p
= 1;
6041 arithmetic_types_p
= 0;
6042 /* Vector arithmetic is only allowed when both sides are vectors. */
6043 if (gnu_vector_type_p (type0
) && gnu_vector_type_p (type1
))
6045 if (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
6046 || !vector_types_compatible_elements_p (type0
, type1
))
6048 if (complain
& tf_error
)
6050 /* "location" already embeds the locations of the
6051 operands, so we don't need to add them separately
6053 rich_location
richloc (line_table
, location
);
6054 binary_op_error (&richloc
, code
, type0
, type1
);
6056 return error_mark_node
;
6058 arithmetic_types_p
= 1;
6061 /* Determine the RESULT_TYPE, if it is not already known. */
6063 && arithmetic_types_p
6064 && (shorten
|| common
|| short_compare
))
6066 result_type
= cp_common_type (type0
, type1
);
6067 if (result_type
== error_mark_node
6068 && code0
== REAL_TYPE
6069 && code1
== REAL_TYPE
6070 && (extended_float_type_p (type0
) || extended_float_type_p (type1
))
6071 && cp_compare_floating_point_conversion_ranks (type0
, type1
) == 3)
6073 if (complain
& tf_error
)
6075 rich_location
richloc (line_table
, location
);
6076 binary_op_error (&richloc
, code
, type0
, type1
);
6078 return error_mark_node
;
6080 if (complain
& tf_warning
)
6082 do_warn_double_promotion (result_type
, type0
, type1
,
6083 "implicit conversion from %qH to %qI "
6084 "to match other operand of binary "
6087 do_warn_enum_conversions (location
, code
, TREE_TYPE (orig_op0
),
6088 TREE_TYPE (orig_op1
));
6092 if (code
== SPACESHIP_EXPR
)
6094 iloc_sentinel
s (location
);
6096 tree orig_type0
= TREE_TYPE (orig_op0
);
6097 tree_code orig_code0
= TREE_CODE (orig_type0
);
6098 tree orig_type1
= TREE_TYPE (orig_op1
);
6099 tree_code orig_code1
= TREE_CODE (orig_type1
);
6102 else if ((orig_code0
== BOOLEAN_TYPE
) != (orig_code1
== BOOLEAN_TYPE
))
6103 /* "If one of the operands is of type bool and the other is not, the
6104 program is ill-formed." */
6105 result_type
= NULL_TREE
;
6106 else if (code0
== POINTER_TYPE
&& orig_code0
!= POINTER_TYPE
6107 && code1
== POINTER_TYPE
&& orig_code1
!= POINTER_TYPE
)
6108 /* We only do array/function-to-pointer conversion if "at least one of
6109 the operands is of pointer type". */
6110 result_type
= NULL_TREE
;
6111 else if (TYPE_PTRFN_P (result_type
) || NULLPTR_TYPE_P (result_type
))
6112 /* <=> no longer supports equality relations. */
6113 result_type
= NULL_TREE
;
6114 else if (orig_code0
== ENUMERAL_TYPE
&& orig_code1
== ENUMERAL_TYPE
6115 && !(same_type_ignoring_top_level_qualifiers_p
6116 (orig_type0
, orig_type1
)))
6117 /* "If both operands have arithmetic types, or one operand has integral
6118 type and the other operand has unscoped enumeration type, the usual
6119 arithmetic conversions are applied to the operands." So we don't do
6120 arithmetic conversions if the operands both have enumeral type. */
6121 result_type
= NULL_TREE
;
6122 else if ((orig_code0
== ENUMERAL_TYPE
&& orig_code1
== REAL_TYPE
)
6123 || (orig_code0
== REAL_TYPE
&& orig_code1
== ENUMERAL_TYPE
))
6124 /* [depr.arith.conv.enum]: Three-way comparisons between such operands
6125 [where one is of enumeration type and the other is of a different
6126 enumeration type or a floating-point type] are ill-formed. */
6127 result_type
= NULL_TREE
;
6131 build_type
= spaceship_type (result_type
, complain
);
6132 if (build_type
== error_mark_node
)
6133 return error_mark_node
;
6136 if (result_type
&& arithmetic_types_p
)
6138 /* If a narrowing conversion is required, other than from an integral
6139 type to a floating point type, the program is ill-formed. */
6141 if (TREE_CODE (result_type
) == REAL_TYPE
6142 && CP_INTEGRAL_TYPE_P (orig_type0
))
6144 else if (!check_narrowing (result_type
, orig_op0
, complain
))
6146 if (TREE_CODE (result_type
) == REAL_TYPE
6147 && CP_INTEGRAL_TYPE_P (orig_type1
))
6149 else if (!check_narrowing (result_type
, orig_op1
, complain
))
6151 if (!ok
&& !(complain
& tf_error
))
6152 return error_mark_node
;
6158 if (complain
& tf_error
)
6160 binary_op_rich_location
richloc (location
,
6161 orig_op0
, orig_op1
, true);
6163 "invalid operands of types %qT and %qT to binary %qO",
6164 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
6166 return error_mark_node
;
6169 /* If we're in a template, the only thing we need to know is the
6171 if (processing_template_decl
)
6173 /* Since the middle-end checks the type when doing a build2, we
6174 need to build the tree in pieces. This built tree will never
6175 get out of the front-end as we replace it when instantiating
6177 tree tmp
= build2 (resultcode
,
6178 build_type
? build_type
: result_type
,
6180 TREE_OPERAND (tmp
, 0) = op0
;
6184 /* Remember the original type; RESULT_TYPE might be changed later on
6185 by shorten_binary_op. */
6186 tree orig_type
= result_type
;
6188 if (arithmetic_types_p
)
6190 bool first_complex
= (code0
== COMPLEX_TYPE
);
6191 bool second_complex
= (code1
== COMPLEX_TYPE
);
6192 int none_complex
= (!first_complex
&& !second_complex
);
6194 /* Adapted from patch for c/24581. */
6195 if (first_complex
!= second_complex
6196 && (code
== PLUS_EXPR
6197 || code
== MINUS_EXPR
6198 || code
== MULT_EXPR
6199 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
6200 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
6201 && flag_signed_zeros
)
6203 /* An operation on mixed real/complex operands must be
6204 handled specially, but the language-independent code can
6205 more easily optimize the plain complex arithmetic if
6206 -fno-signed-zeros. */
6207 tree real_type
= TREE_TYPE (result_type
);
6211 if (TREE_TYPE (op0
) != result_type
)
6212 op0
= cp_convert_and_check (result_type
, op0
, complain
);
6213 if (TREE_TYPE (op1
) != real_type
)
6214 op1
= cp_convert_and_check (real_type
, op1
, complain
);
6218 if (TREE_TYPE (op0
) != real_type
)
6219 op0
= cp_convert_and_check (real_type
, op0
, complain
);
6220 if (TREE_TYPE (op1
) != result_type
)
6221 op1
= cp_convert_and_check (result_type
, op1
, complain
);
6223 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
6224 return error_mark_node
;
6227 op0
= save_expr (op0
);
6228 real
= cp_build_unary_op (REALPART_EXPR
, op0
, true, complain
);
6229 imag
= cp_build_unary_op (IMAGPART_EXPR
, op0
, true, complain
);
6233 case TRUNC_DIV_EXPR
:
6234 op1
= save_expr (op1
);
6235 imag
= build2 (resultcode
, real_type
, imag
, op1
);
6239 real
= build2 (resultcode
, real_type
, real
, op1
);
6247 op1
= save_expr (op1
);
6248 real
= cp_build_unary_op (REALPART_EXPR
, op1
, true, complain
);
6249 imag
= cp_build_unary_op (IMAGPART_EXPR
, op1
, true, complain
);
6253 op0
= save_expr (op0
);
6254 imag
= build2 (resultcode
, real_type
, op0
, imag
);
6257 real
= build2 (resultcode
, real_type
, op0
, real
);
6260 real
= build2 (resultcode
, real_type
, op0
, real
);
6261 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
6267 result
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
6271 /* For certain operations (which identify themselves by shorten != 0)
6272 if both args were extended from the same smaller type,
6273 do the arithmetic in that type and then extend.
6275 shorten !=0 and !=1 indicates a bitwise operation.
6276 For them, this optimization is safe only if
6277 both args are zero-extended or both are sign-extended.
6278 Otherwise, we might change the result.
6279 E.g., (short)-1 | (unsigned short)-1 is (int)-1
6280 but calculated in (unsigned short) it would be (unsigned short)-1. */
6282 if (shorten
&& none_complex
)
6284 final_type
= result_type
;
6285 result_type
= shorten_binary_op (result_type
, op0
, op1
,
6289 /* Shifts can be shortened if shifting right. */
6294 tree arg0
= get_narrower (op0
, &unsigned_arg
);
6295 /* We're not really warning here but when we set short_shift we
6296 used fold_for_warn to fold the operand. */
6297 tree const_op1
= fold_for_warn (op1
);
6299 final_type
= result_type
;
6301 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
6302 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
6304 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
6305 && tree_int_cst_sgn (const_op1
) > 0
6306 /* We can shorten only if the shift count is less than the
6307 number of bits in the smaller type size. */
6308 && compare_tree_int (const_op1
,
6309 TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
6310 /* We cannot drop an unsigned shift after sign-extension. */
6311 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
6313 /* Do an unsigned shift if the operand was zero-extended. */
6315 = c_common_signed_or_unsigned_type (unsigned_arg
,
6317 /* Convert value-to-be-shifted to that type. */
6318 if (TREE_TYPE (op0
) != result_type
)
6319 op0
= convert (result_type
, op0
);
6324 /* Comparison operations are shortened too but differently.
6325 They identify themselves by setting short_compare = 1. */
6329 /* We call shorten_compare only for diagnostics. */
6330 tree xop0
= fold_simple (op0
);
6331 tree xop1
= fold_simple (op1
);
6332 tree xresult_type
= result_type
;
6333 enum tree_code xresultcode
= resultcode
;
6334 shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
6338 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
6339 && warn_sign_compare
6340 /* Do not warn until the template is instantiated; we cannot
6341 bound the ranges of the arguments until that point. */
6342 && !processing_template_decl
6343 && (complain
& tf_warning
)
6344 && c_inhibit_evaluation_warnings
== 0
6345 /* Even unsigned enum types promote to signed int. We don't
6346 want to issue -Wsign-compare warnings for this case. */
6347 && !enum_cast_to_int (orig_op0
)
6348 && !enum_cast_to_int (orig_op1
))
6350 warn_for_sign_compare (location
, orig_op0
, orig_op1
, op0
, op1
,
6351 result_type
, resultcode
);
6355 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6356 Then the expression will be built.
6357 It will be given type FINAL_TYPE if that is nonzero;
6358 otherwise, it will be given type RESULT_TYPE. */
6361 warning_sentinel
w (warn_sign_conversion
, short_compare
);
6362 if (!same_type_p (TREE_TYPE (op0
), result_type
))
6363 op0
= cp_convert_and_check (result_type
, op0
, complain
);
6364 if (!same_type_p (TREE_TYPE (op1
), result_type
))
6365 op1
= cp_convert_and_check (result_type
, op1
, complain
);
6367 if (op0
== error_mark_node
|| op1
== error_mark_node
)
6368 return error_mark_node
;
6371 if (build_type
== NULL_TREE
)
6372 build_type
= result_type
;
6375 && flag_strong_eval_order
== 2
6376 && TREE_SIDE_EFFECTS (op1
)
6377 && !processing_template_decl
)
6379 /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6380 op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6381 op0
= cp_save_expr (op0
);
6382 instrument_expr
= op0
;
6385 if (sanitize_flags_p ((SANITIZE_SHIFT
6387 | SANITIZE_FLOAT_DIVIDE
6388 | SANITIZE_SI_OVERFLOW
))
6389 && current_function_decl
!= NULL_TREE
6390 && !processing_template_decl
6391 && (doing_div_or_mod
|| doing_shift
))
6393 /* OP0 and/or OP1 might have side-effects. */
6394 op0
= cp_save_expr (op0
);
6395 op1
= cp_save_expr (op1
);
6396 op0
= fold_non_dependent_expr (op0
, complain
);
6397 op1
= fold_non_dependent_expr (op1
, complain
);
6398 tree instrument_expr1
= NULL_TREE
;
6399 if (doing_div_or_mod
6400 && sanitize_flags_p (SANITIZE_DIVIDE
6401 | SANITIZE_FLOAT_DIVIDE
6402 | SANITIZE_SI_OVERFLOW
))
6404 /* For diagnostics we want to use the promoted types without
6405 shorten_binary_op. So convert the arguments to the
6406 original result_type. */
6409 if (TREE_TYPE (cop0
) != orig_type
)
6410 cop0
= cp_convert (orig_type
, op0
, complain
);
6411 if (TREE_TYPE (cop1
) != orig_type
)
6412 cop1
= cp_convert (orig_type
, op1
, complain
);
6413 instrument_expr1
= ubsan_instrument_division (location
, cop0
, cop1
);
6415 else if (doing_shift
&& sanitize_flags_p (SANITIZE_SHIFT
))
6416 instrument_expr1
= ubsan_instrument_shift (location
, code
, op0
, op1
);
6417 if (instrument_expr
!= NULL
)
6418 instrument_expr
= add_stmt_to_compound (instrument_expr
,
6421 instrument_expr
= instrument_expr1
;
6424 result
= build2_loc (location
, resultcode
, build_type
, op0
, op1
);
6425 if (final_type
!= 0)
6426 result
= cp_convert (final_type
, result
, complain
);
6428 if (instrument_expr
!= NULL
)
6429 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
6430 instrument_expr
, result
);
6432 if (resultcode
== SPACESHIP_EXPR
&& !processing_template_decl
)
6433 result
= get_target_expr (result
, complain
);
6435 if (!c_inhibit_evaluation_warnings
)
6437 if (!processing_template_decl
)
6439 op0
= cp_fully_fold (op0
);
6440 /* Only consider the second argument if the first isn't overflowed. */
6441 if (!CONSTANT_CLASS_P (op0
) || TREE_OVERFLOW_P (op0
))
6443 op1
= cp_fully_fold (op1
);
6444 if (!CONSTANT_CLASS_P (op1
) || TREE_OVERFLOW_P (op1
))
6447 else if (!CONSTANT_CLASS_P (op0
) || !CONSTANT_CLASS_P (op1
)
6448 || TREE_OVERFLOW_P (op0
) || TREE_OVERFLOW_P (op1
))
6451 tree result_ovl
= fold_build2 (resultcode
, build_type
, op0
, op1
);
6452 if (TREE_OVERFLOW_P (result_ovl
))
6453 overflow_warning (location
, result_ovl
);
6459 /* Build a VEC_PERM_EXPR.
6460 This is a simple wrapper for c_build_vec_perm_expr. */
6462 build_x_vec_perm_expr (location_t loc
,
6463 tree arg0
, tree arg1
, tree arg2
,
6464 tsubst_flags_t complain
)
6466 tree orig_arg0
= arg0
;
6467 tree orig_arg1
= arg1
;
6468 tree orig_arg2
= arg2
;
6469 if (processing_template_decl
)
6471 if (type_dependent_expression_p (arg0
)
6472 || type_dependent_expression_p (arg1
)
6473 || type_dependent_expression_p (arg2
))
6474 return build_min_nt_loc (loc
, VEC_PERM_EXPR
, arg0
, arg1
, arg2
);
6475 arg0
= build_non_dependent_expr (arg0
);
6477 arg1
= build_non_dependent_expr (arg1
);
6478 arg2
= build_non_dependent_expr (arg2
);
6480 tree exp
= c_build_vec_perm_expr (loc
, arg0
, arg1
, arg2
, complain
& tf_error
);
6481 if (processing_template_decl
&& exp
!= error_mark_node
)
6482 return build_min_non_dep (VEC_PERM_EXPR
, exp
, orig_arg0
,
6483 orig_arg1
, orig_arg2
);
6487 /* Build a VEC_PERM_EXPR.
6488 This is a simple wrapper for c_build_shufflevector. */
6490 build_x_shufflevector (location_t loc
, vec
<tree
, va_gc
> *args
,
6491 tsubst_flags_t complain
)
6493 tree arg0
= (*args
)[0];
6494 tree arg1
= (*args
)[1];
6495 if (processing_template_decl
)
6497 for (unsigned i
= 0; i
< args
->length (); ++i
)
6499 ? type_dependent_expression_p ((*args
)[i
])
6500 : instantiation_dependent_expression_p ((*args
)[i
]))
6502 tree exp
= build_min_nt_call_vec (NULL
, args
);
6503 CALL_EXPR_IFN (exp
) = IFN_SHUFFLEVECTOR
;
6506 arg0
= build_non_dependent_expr (arg0
);
6507 arg1
= build_non_dependent_expr (arg1
);
6508 /* ??? Nothing needed for the index arguments? */
6510 auto_vec
<tree
, 16> mask
;
6511 for (unsigned i
= 2; i
< args
->length (); ++i
)
6513 tree idx
= fold_non_dependent_expr ((*args
)[i
], complain
);
6514 mask
.safe_push (idx
);
6516 tree exp
= c_build_shufflevector (loc
, arg0
, arg1
, mask
, complain
& tf_error
);
6517 if (processing_template_decl
&& exp
!= error_mark_node
)
6519 exp
= build_min_non_dep_call_vec (exp
, NULL
, args
);
6520 CALL_EXPR_IFN (exp
) = IFN_SHUFFLEVECTOR
;
6525 /* Return a tree for the sum or difference (RESULTCODE says which)
6526 of pointer PTROP and integer INTOP. */
6529 cp_pointer_int_sum (location_t loc
, enum tree_code resultcode
, tree ptrop
,
6530 tree intop
, tsubst_flags_t complain
)
6532 tree res_type
= TREE_TYPE (ptrop
);
6534 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
6535 in certain circumstance (when it's valid to do so). So we need
6536 to make sure it's complete. We don't need to check here, if we
6537 can actually complete it at all, as those checks will be done in
6538 pointer_int_sum() anyway. */
6539 complete_type (TREE_TYPE (res_type
));
6541 return pointer_int_sum (loc
, resultcode
, ptrop
,
6542 intop
, complain
& tf_warning_or_error
);
6545 /* Return a tree for the difference of pointers OP0 and OP1.
6546 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
6547 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
6550 pointer_diff (location_t loc
, tree op0
, tree op1
, tree ptrtype
,
6551 tsubst_flags_t complain
, tree
*instrument_expr
)
6553 tree result
, inttype
;
6554 tree restype
= ptrdiff_type_node
;
6555 tree target_type
= TREE_TYPE (ptrtype
);
6557 if (!complete_type_or_maybe_complain (target_type
, NULL_TREE
, complain
))
6558 return error_mark_node
;
6560 if (VOID_TYPE_P (target_type
))
6562 if (complain
& tf_error
)
6563 permerror (loc
, "ISO C++ forbids using pointer of "
6564 "type %<void *%> in subtraction");
6566 return error_mark_node
;
6568 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
6570 if (complain
& tf_error
)
6571 permerror (loc
, "ISO C++ forbids using pointer to "
6572 "a function in subtraction");
6574 return error_mark_node
;
6576 if (TREE_CODE (target_type
) == METHOD_TYPE
)
6578 if (complain
& tf_error
)
6579 permerror (loc
, "ISO C++ forbids using pointer to "
6580 "a method in subtraction");
6582 return error_mark_node
;
6584 else if (!verify_type_context (loc
, TCTX_POINTER_ARITH
,
6585 TREE_TYPE (TREE_TYPE (op0
)),
6586 !(complain
& tf_error
))
6587 || !verify_type_context (loc
, TCTX_POINTER_ARITH
,
6588 TREE_TYPE (TREE_TYPE (op1
)),
6589 !(complain
& tf_error
)))
6590 return error_mark_node
;
6592 /* Determine integer type result of the subtraction. This will usually
6593 be the same as the result type (ptrdiff_t), but may need to be a wider
6594 type if pointers for the address space are wider than ptrdiff_t. */
6595 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
6596 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
6600 if (!processing_template_decl
6601 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT
))
6603 op0
= save_expr (op0
);
6604 op1
= save_expr (op1
);
6606 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT
);
6607 *instrument_expr
= build_call_expr_loc (loc
, tt
, 2, op0
, op1
);
6610 /* First do the subtraction, then build the divide operator
6611 and only convert at the very end.
6612 Do not do default conversions in case restype is a short type. */
6614 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
6615 pointers. If some platform cannot provide that, or has a larger
6616 ptrdiff_type to support differences larger than half the address
6617 space, cast the pointers to some larger integer type and do the
6618 computations in that type. */
6619 if (TYPE_PRECISION (inttype
) > TYPE_PRECISION (TREE_TYPE (op0
)))
6620 op0
= cp_build_binary_op (loc
,
6622 cp_convert (inttype
, op0
, complain
),
6623 cp_convert (inttype
, op1
, complain
),
6626 op0
= build2_loc (loc
, POINTER_DIFF_EXPR
, inttype
, op0
, op1
);
6628 /* This generates an error if op1 is a pointer to an incomplete type. */
6629 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
6631 if (complain
& tf_error
)
6632 error_at (loc
, "invalid use of a pointer to an incomplete type in "
6633 "pointer arithmetic");
6635 return error_mark_node
;
6638 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1
)))
6640 if (complain
& tf_error
)
6641 error_at (loc
, "arithmetic on pointer to an empty aggregate");
6643 return error_mark_node
;
6646 op1
= (TYPE_PTROB_P (ptrtype
)
6647 ? size_in_bytes_loc (loc
, target_type
)
6648 : integer_one_node
);
6650 /* Do the division. */
6652 result
= build2_loc (loc
, EXACT_DIV_EXPR
, inttype
, op0
,
6653 cp_convert (inttype
, op1
, complain
));
6654 return cp_convert (restype
, result
, complain
);
6657 /* Construct and perhaps optimize a tree representation
6658 for a unary operation. CODE, a tree_code, specifies the operation
6659 and XARG is the operand. */
6662 build_x_unary_op (location_t loc
, enum tree_code code
, cp_expr xarg
,
6663 tree lookups
, tsubst_flags_t complain
)
6665 tree orig_expr
= xarg
;
6668 tree overload
= NULL_TREE
;
6670 if (processing_template_decl
)
6672 if (type_dependent_expression_p (xarg
))
6674 tree e
= build_min_nt_loc (loc
, code
, xarg
.get_value (), NULL_TREE
);
6675 TREE_TYPE (e
) = build_dependent_operator_type (lookups
, code
, false);
6679 xarg
= build_non_dependent_expr (xarg
);
6684 /* [expr.unary.op] says:
6686 The address of an object of incomplete type can be taken.
6688 (And is just the ordinary address operator, not an overloaded
6689 "operator &".) However, if the type is a template
6690 specialization, we must complete the type at this point so that
6691 an overloaded "operator &" will be available if required. */
6692 if (code
== ADDR_EXPR
6693 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
6694 && ((CLASS_TYPE_P (TREE_TYPE (xarg
))
6695 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg
))))
6696 || (TREE_CODE (xarg
) == OFFSET_REF
)))
6697 /* Don't look for a function. */;
6699 exp
= build_new_op (loc
, code
, LOOKUP_NORMAL
, xarg
, NULL_TREE
,
6700 NULL_TREE
, lookups
, &overload
, complain
);
6702 if (!exp
&& code
== ADDR_EXPR
)
6704 if (is_overloaded_fn (xarg
))
6706 tree fn
= get_first_fn (xarg
);
6707 if (DECL_CONSTRUCTOR_P (fn
) || DECL_DESTRUCTOR_P (fn
))
6709 if (complain
& tf_error
)
6710 error_at (loc
, DECL_CONSTRUCTOR_P (fn
)
6711 ? G_("taking address of constructor %qD")
6712 : G_("taking address of destructor %qD"),
6714 return error_mark_node
;
6718 /* A pointer to member-function can be formed only by saying
6720 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
6721 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
6723 if (TREE_CODE (xarg
) != OFFSET_REF
6724 || !TYPE_P (TREE_OPERAND (xarg
, 0)))
6726 if (complain
& tf_error
)
6728 error_at (loc
, "invalid use of %qE to form a "
6729 "pointer-to-member-function", xarg
.get_value ());
6730 if (TREE_CODE (xarg
) != OFFSET_REF
)
6731 inform (loc
, " a qualified-id is required");
6733 return error_mark_node
;
6737 if (complain
& tf_error
)
6738 error_at (loc
, "parentheses around %qE cannot be used to "
6739 "form a pointer-to-member-function",
6742 return error_mark_node
;
6743 PTRMEM_OK_P (xarg
) = 1;
6747 if (TREE_CODE (xarg
) == OFFSET_REF
)
6749 ptrmem
= PTRMEM_OK_P (xarg
);
6751 if (!ptrmem
&& !flag_ms_extensions
6752 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
6754 /* A single non-static member, make sure we don't allow a
6755 pointer-to-member. */
6756 xarg
= build2 (OFFSET_REF
, TREE_TYPE (xarg
),
6757 TREE_OPERAND (xarg
, 0),
6758 ovl_make (TREE_OPERAND (xarg
, 1)));
6759 PTRMEM_OK_P (xarg
) = ptrmem
;
6763 exp
= cp_build_addr_expr_strict (xarg
, complain
);
6765 if (TREE_CODE (exp
) == PTRMEM_CST
)
6766 PTRMEM_CST_LOCATION (exp
) = loc
;
6768 protected_set_expr_location (exp
, loc
);
6771 if (processing_template_decl
&& exp
!= error_mark_node
)
6773 if (overload
!= NULL_TREE
)
6774 return (build_min_non_dep_op_overload
6775 (code
, exp
, overload
, orig_expr
, integer_zero_node
));
6777 exp
= build_min_non_dep (code
, exp
, orig_expr
,
6778 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE
);
6780 if (TREE_CODE (exp
) == ADDR_EXPR
)
6781 PTRMEM_OK_P (exp
) = ptrmem
;
6785 /* Construct and perhaps optimize a tree representation
6786 for __builtin_addressof operation. ARG specifies the operand. */
6789 cp_build_addressof (location_t loc
, tree arg
, tsubst_flags_t complain
)
6791 tree orig_expr
= arg
;
6793 if (processing_template_decl
)
6795 if (type_dependent_expression_p (arg
))
6796 return build_min_nt_loc (loc
, ADDRESSOF_EXPR
, arg
, NULL_TREE
);
6798 arg
= build_non_dependent_expr (arg
);
6801 tree exp
= cp_build_addr_expr_strict (arg
, complain
);
6803 if (processing_template_decl
&& exp
!= error_mark_node
)
6804 exp
= build_min_non_dep (ADDRESSOF_EXPR
, exp
, orig_expr
, NULL_TREE
);
6808 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
6809 constants, where a null value is represented by an INTEGER_CST of
6813 cp_truthvalue_conversion (tree expr
, tsubst_flags_t complain
)
6815 tree type
= TREE_TYPE (expr
);
6816 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6817 if (TYPE_PTR_OR_PTRMEM_P (type
)
6818 /* Avoid ICE on invalid use of non-static member function. */
6819 || TREE_CODE (expr
) == FUNCTION_DECL
)
6820 return cp_build_binary_op (loc
, NE_EXPR
, expr
, nullptr_node
, complain
);
6822 return c_common_truthvalue_conversion (loc
, expr
);
6825 /* Returns EXPR contextually converted to bool. */
6828 contextual_conv_bool (tree expr
, tsubst_flags_t complain
)
6830 return perform_implicit_conversion_flags (boolean_type_node
, expr
,
6831 complain
, LOOKUP_NORMAL
);
6834 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
6835 is a low-level function; most callers should use maybe_convert_cond. */
6838 condition_conversion (tree expr
)
6840 tree t
= contextual_conv_bool (expr
, tf_warning_or_error
);
6841 if (!processing_template_decl
)
6842 t
= fold_build_cleanup_point_expr (boolean_type_node
, t
);
6846 /* Returns the address of T. This function will fold away
6847 ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
6848 most places should use cp_build_addr_expr instead. */
6851 build_address (tree t
)
6853 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
6854 return error_mark_node
;
6855 gcc_checking_assert (TREE_CODE (t
) != CONSTRUCTOR
6856 || processing_template_decl
);
6857 t
= build_fold_addr_expr_loc (EXPR_LOCATION (t
), t
);
6858 if (TREE_CODE (t
) != ADDR_EXPR
)
6863 /* Return a NOP_EXPR converting EXPR to TYPE. */
6866 build_nop (tree type
, tree expr
)
6868 if (type
== error_mark_node
|| error_operand_p (expr
))
6870 return build1_loc (EXPR_LOCATION (expr
), NOP_EXPR
, type
, expr
);
6873 /* Take the address of ARG, whatever that means under C++ semantics.
6874 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
6875 and class rvalues as well.
6877 Nothing should call this function directly; instead, callers should use
6878 cp_build_addr_expr or cp_build_addr_expr_strict. */
6881 cp_build_addr_expr_1 (tree arg
, bool strict_lvalue
, tsubst_flags_t complain
)
6886 if (!arg
|| error_operand_p (arg
))
6887 return error_mark_node
;
6889 arg
= mark_lvalue_use (arg
);
6890 if (error_operand_p (arg
))
6891 return error_mark_node
;
6893 argtype
= lvalue_type (arg
);
6894 location_t loc
= cp_expr_loc_or_input_loc (arg
);
6896 gcc_assert (!(identifier_p (arg
) && IDENTIFIER_ANY_OP_P (arg
)));
6898 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
6899 && !really_overloaded_fn (arg
))
6901 /* They're trying to take the address of a unique non-static
6902 member function. This is ill-formed (except in MS-land),
6903 but let's try to DTRT.
6904 Note: We only handle unique functions here because we don't
6905 want to complain if there's a static overload; non-unique
6906 cases will be handled by instantiate_type. But we need to
6907 handle this case here to allow casts on the resulting PMF.
6908 We could defer this in non-MS mode, but it's easier to give
6909 a useful error here. */
6911 /* Inside constant member functions, the `this' pointer
6912 contains an extra const qualifier. TYPE_MAIN_VARIANT
6913 is used here to remove this const from the diagnostics
6914 and the created OFFSET_REF. */
6915 tree base
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg
, 0)));
6916 tree fn
= get_first_fn (TREE_OPERAND (arg
, 1));
6917 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
6918 return error_mark_node
;
6920 if (! flag_ms_extensions
)
6922 tree name
= DECL_NAME (fn
);
6923 if (!(complain
& tf_error
))
6924 return error_mark_node
;
6925 else if (current_class_type
6926 && TREE_OPERAND (arg
, 0) == current_class_ref
)
6927 /* An expression like &memfn. */
6929 "ISO C++ forbids taking the address of an unqualified"
6930 " or parenthesized non-static member function to form"
6931 " a pointer to member function. Say %<&%T::%D%>",
6935 "ISO C++ forbids taking the address of a bound member"
6936 " function to form a pointer to member function."
6940 arg
= build_offset_ref (base
, fn
, /*address_p=*/true, complain
);
6943 /* Uninstantiated types are all functions. Taking the
6944 address of a function is a no-op, so just return the
6946 if (type_unknown_p (arg
))
6947 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
6949 if (TREE_CODE (arg
) == OFFSET_REF
)
6950 /* We want a pointer to member; bypass all the code for actually taking
6951 the address of something. */
6954 /* Anything not already handled and not a true memory reference
6956 if (!FUNC_OR_METHOD_TYPE_P (argtype
))
6958 cp_lvalue_kind kind
= lvalue_kind (arg
);
6959 if (kind
== clk_none
)
6961 if (complain
& tf_error
)
6962 lvalue_error (loc
, lv_addressof
);
6963 return error_mark_node
;
6965 if (strict_lvalue
&& (kind
& (clk_rvalueref
|clk_class
)))
6967 if (!(complain
& tf_error
))
6968 return error_mark_node
;
6969 /* Make this a permerror because we used to accept it. */
6970 permerror (loc
, "taking address of rvalue");
6974 if (TYPE_REF_P (argtype
))
6976 tree type
= build_pointer_type (TREE_TYPE (argtype
));
6977 arg
= build1 (CONVERT_EXPR
, type
, arg
);
6980 else if (pedantic
&& DECL_MAIN_P (tree_strip_any_location_wrapper (arg
)))
6983 /* Apparently a lot of autoconf scripts for C++ packages do this,
6984 so only complain if -Wpedantic. */
6985 if (complain
& (flag_pedantic_errors
? tf_error
: tf_warning
))
6986 pedwarn (loc
, OPT_Wpedantic
,
6987 "ISO C++ forbids taking address of function %<::main%>");
6988 else if (flag_pedantic_errors
)
6989 return error_mark_node
;
6992 /* Let &* cancel out to simplify resulting code. */
6993 if (INDIRECT_REF_P (arg
))
6995 arg
= TREE_OPERAND (arg
, 0);
6996 if (TYPE_REF_P (TREE_TYPE (arg
)))
6998 tree type
= build_pointer_type (TREE_TYPE (TREE_TYPE (arg
)));
6999 arg
= build1 (CONVERT_EXPR
, type
, arg
);
7002 /* Don't let this be an lvalue. */
7007 /* Handle complex lvalues (when permitted)
7008 by reduction to simpler cases. */
7009 val
= unary_complex_lvalue (ADDR_EXPR
, arg
);
7013 switch (TREE_CODE (arg
))
7017 case FIX_TRUNC_EXPR
:
7018 /* We should have handled this above in the lvalue_kind check. */
7023 arg
= BASELINK_FUNCTIONS (arg
);
7027 arg
= OVL_FIRST (arg
);
7032 /* Turn a reference to a non-static data member into a
7033 pointer-to-member. */
7038 gcc_assert (PTRMEM_OK_P (arg
));
7040 t
= TREE_OPERAND (arg
, 1);
7041 if (TYPE_REF_P (TREE_TYPE (t
)))
7043 if (complain
& tf_error
)
7045 "cannot create pointer to reference member %qD", t
);
7046 return error_mark_node
;
7049 /* Forming a pointer-to-member is a use of non-pure-virtual fns. */
7050 if (TREE_CODE (t
) == FUNCTION_DECL
7051 && !DECL_PURE_VIRTUAL_P (t
)
7052 && !mark_used (t
, complain
) && !(complain
& tf_error
))
7053 return error_mark_node
;
7055 type
= build_ptrmem_type (context_for_name_lookup (t
),
7057 t
= make_ptrmem_cst (type
, t
);
7065 if (argtype
!= error_mark_node
)
7066 argtype
= build_pointer_type (argtype
);
7068 if (bitfield_p (arg
))
7070 if (complain
& tf_error
)
7071 error_at (loc
, "attempt to take address of bit-field");
7072 return error_mark_node
;
7075 /* In a template, we are processing a non-dependent expression
7076 so we can just form an ADDR_EXPR with the correct type. */
7077 if (processing_template_decl
|| TREE_CODE (arg
) != COMPONENT_REF
)
7079 if (!mark_single_function (arg
, complain
))
7080 return error_mark_node
;
7081 val
= build_address (arg
);
7082 if (TREE_CODE (arg
) == OFFSET_REF
)
7083 PTRMEM_OK_P (val
) = PTRMEM_OK_P (arg
);
7085 else if (BASELINK_P (TREE_OPERAND (arg
, 1)))
7087 tree fn
= BASELINK_FUNCTIONS (TREE_OPERAND (arg
, 1));
7089 /* We can only get here with a single static member
7091 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
7092 && DECL_STATIC_FUNCTION_P (fn
));
7093 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
7094 return error_mark_node
;
7095 val
= build_address (fn
);
7096 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
7097 /* Do not lose object's side effects. */
7098 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
),
7099 TREE_OPERAND (arg
, 0), val
);
7103 tree object
= TREE_OPERAND (arg
, 0);
7104 tree field
= TREE_OPERAND (arg
, 1);
7105 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7106 (TREE_TYPE (object
), decl_type_context (field
)));
7107 val
= build_address (arg
);
7110 if (TYPE_PTR_P (argtype
)
7111 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
7113 build_ptrmemfunc_type (argtype
);
7114 val
= build_ptrmemfunc (argtype
, val
, 0,
7119 /* For addresses of immediate functions ensure we have EXPR_LOCATION
7120 set for possible later diagnostics. */
7121 if (TREE_CODE (val
) == ADDR_EXPR
7122 && TREE_CODE (TREE_OPERAND (val
, 0)) == FUNCTION_DECL
7123 && DECL_IMMEDIATE_FUNCTION_P (TREE_OPERAND (val
, 0)))
7124 SET_EXPR_LOCATION (val
, input_location
);
7129 /* Take the address of ARG if it has one, even if it's an rvalue. */
7132 cp_build_addr_expr (tree arg
, tsubst_flags_t complain
)
7134 return cp_build_addr_expr_1 (arg
, 0, complain
);
7137 /* Take the address of ARG, but only if it's an lvalue. */
7140 cp_build_addr_expr_strict (tree arg
, tsubst_flags_t complain
)
7142 return cp_build_addr_expr_1 (arg
, 1, complain
);
7145 /* C++: Must handle pointers to members.
7147 Perhaps type instantiation should be extended to handle conversion
7148 from aggregates to types we don't yet know we want? (Or are those
7149 cases typically errors which should be reported?)
7151 NOCONVERT suppresses the default promotions (such as from short to int). */
7154 cp_build_unary_op (enum tree_code code
, tree xarg
, bool noconvert
,
7155 tsubst_flags_t complain
)
7157 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
7159 location_t location
= cp_expr_loc_or_input_loc (arg
);
7161 const char *errstring
= NULL
;
7163 const char *invalid_op_diag
;
7165 if (!arg
|| error_operand_p (arg
))
7166 return error_mark_node
;
7168 arg
= resolve_nondeduced_context (arg
, complain
);
7170 if ((invalid_op_diag
7171 = targetm
.invalid_unary_op ((code
== UNARY_PLUS_EXPR
7176 if (complain
& tf_error
)
7177 error (invalid_op_diag
);
7178 return error_mark_node
;
7183 case UNARY_PLUS_EXPR
:
7186 int flags
= WANT_ARITH
| WANT_ENUM
;
7187 /* Unary plus (but not unary minus) is allowed on pointers. */
7188 if (code
== UNARY_PLUS_EXPR
)
7189 flags
|= WANT_POINTER
;
7190 arg
= build_expr_type_conversion (flags
, arg
, true);
7192 errstring
= (code
== NEGATE_EXPR
7193 ? _("wrong type argument to unary minus")
7194 : _("wrong type argument to unary plus"));
7197 if (!noconvert
&& INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg
)))
7198 arg
= cp_perform_integral_promotions (arg
, complain
);
7200 /* Make sure the result is not an lvalue: a unary plus or minus
7201 expression is always a rvalue. */
7208 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
7213 arg
= cp_default_conversion (arg
, complain
);
7214 if (arg
== error_mark_node
)
7215 return error_mark_node
;
7218 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
7219 | WANT_VECTOR_OR_COMPLEX
,
7221 errstring
= _("wrong type argument to bit-complement");
7222 else if (!noconvert
&& INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg
)))
7224 /* Warn if the expression has boolean value. */
7225 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
7226 && (complain
& tf_warning
)
7227 && warning_at (location
, OPT_Wbool_operation
,
7228 "%<~%> on an expression of type %<bool%>"))
7229 inform (location
, "did you mean to use logical not (%<!%>)?");
7230 arg
= cp_perform_integral_promotions (arg
, complain
);
7232 else if (!noconvert
&& VECTOR_TYPE_P (TREE_TYPE (arg
)))
7233 arg
= mark_rvalue_use (arg
);
7237 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
7238 errstring
= _("wrong type argument to abs");
7239 else if (!noconvert
)
7241 arg
= cp_default_conversion (arg
, complain
);
7242 if (arg
== error_mark_node
)
7243 return error_mark_node
;
7248 /* Conjugating a real value is a no-op, but allow it anyway. */
7249 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
7250 errstring
= _("wrong type argument to conjugation");
7251 else if (!noconvert
)
7253 arg
= cp_default_conversion (arg
, complain
);
7254 if (arg
== error_mark_node
)
7255 return error_mark_node
;
7259 case TRUTH_NOT_EXPR
:
7260 if (gnu_vector_type_p (TREE_TYPE (arg
)))
7261 return cp_build_binary_op (input_location
, EQ_EXPR
, arg
,
7262 build_zero_cst (TREE_TYPE (arg
)), complain
);
7263 arg
= perform_implicit_conversion (boolean_type_node
, arg
,
7265 val
= invert_truthvalue_loc (location
, arg
);
7266 if (arg
!= error_mark_node
)
7268 errstring
= _("in argument to unary !");
7276 arg
= build_real_imag_expr (input_location
, code
, arg
);
7279 case PREINCREMENT_EXPR
:
7280 case POSTINCREMENT_EXPR
:
7281 case PREDECREMENT_EXPR
:
7282 case POSTDECREMENT_EXPR
:
7283 /* Handle complex lvalues (when permitted)
7284 by reduction to simpler cases. */
7286 val
= unary_complex_lvalue (code
, arg
);
7290 arg
= mark_lvalue_use (arg
);
7292 /* Increment or decrement the real part of the value,
7293 and don't change the imaginary part. */
7294 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
7298 arg
= cp_stabilize_reference (arg
);
7299 real
= cp_build_unary_op (REALPART_EXPR
, arg
, true, complain
);
7300 imag
= cp_build_unary_op (IMAGPART_EXPR
, arg
, true, complain
);
7301 real
= cp_build_unary_op (code
, real
, true, complain
);
7302 if (real
== error_mark_node
|| imag
== error_mark_node
)
7303 return error_mark_node
;
7304 return build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
7308 /* Report invalid types. */
7310 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
7313 if (code
== PREINCREMENT_EXPR
)
7314 errstring
= _("no pre-increment operator for type");
7315 else if (code
== POSTINCREMENT_EXPR
)
7316 errstring
= _("no post-increment operator for type");
7317 else if (code
== PREDECREMENT_EXPR
)
7318 errstring
= _("no pre-decrement operator for type");
7320 errstring
= _("no post-decrement operator for type");
7323 else if (arg
== error_mark_node
)
7324 return error_mark_node
;
7326 /* Report something read-only. */
7328 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
7329 || TREE_READONLY (arg
))
7331 if (complain
& tf_error
)
7332 cxx_readonly_error (location
, arg
,
7333 ((code
== PREINCREMENT_EXPR
7334 || code
== POSTINCREMENT_EXPR
)
7335 ? lv_increment
: lv_decrement
));
7337 return error_mark_node
;
7342 tree declared_type
= unlowered_expr_type (arg
);
7344 argtype
= TREE_TYPE (arg
);
7346 /* ARM $5.2.5 last annotation says this should be forbidden. */
7347 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
7349 if (complain
& tf_error
)
7350 permerror (location
, (code
== PREINCREMENT_EXPR
7351 || code
== POSTINCREMENT_EXPR
)
7352 ? G_("ISO C++ forbids incrementing an enum")
7353 : G_("ISO C++ forbids decrementing an enum"));
7355 return error_mark_node
;
7358 /* Compute the increment. */
7360 if (TYPE_PTR_P (argtype
))
7362 tree type
= complete_type (TREE_TYPE (argtype
));
7364 if (!COMPLETE_OR_VOID_TYPE_P (type
))
7366 if (complain
& tf_error
)
7367 error_at (location
, ((code
== PREINCREMENT_EXPR
7368 || code
== POSTINCREMENT_EXPR
))
7369 ? G_("cannot increment a pointer to incomplete "
7371 : G_("cannot decrement a pointer to incomplete "
7373 TREE_TYPE (argtype
));
7375 return error_mark_node
;
7377 else if (!TYPE_PTROB_P (argtype
))
7379 if (complain
& tf_error
)
7380 pedwarn (location
, OPT_Wpointer_arith
,
7381 (code
== PREINCREMENT_EXPR
7382 || code
== POSTINCREMENT_EXPR
)
7383 ? G_("ISO C++ forbids incrementing a pointer "
7385 : G_("ISO C++ forbids decrementing a pointer "
7389 return error_mark_node
;
7391 else if (!verify_type_context (location
, TCTX_POINTER_ARITH
,
7392 TREE_TYPE (argtype
),
7393 !(complain
& tf_error
)))
7394 return error_mark_node
;
7396 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
7399 inc
= VECTOR_TYPE_P (argtype
)
7400 ? build_one_cst (argtype
)
7403 inc
= cp_convert (argtype
, inc
, complain
);
7405 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
7406 need to ask Objective-C to build the increment or decrement
7407 expression for it. */
7408 if (objc_is_property_ref (arg
))
7409 return objc_build_incr_expr_for_property_ref (input_location
, code
,
7412 /* Complain about anything else that is not a true lvalue. */
7413 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
7414 || code
== POSTINCREMENT_EXPR
)
7415 ? lv_increment
: lv_decrement
),
7417 return error_mark_node
;
7419 /* [depr.volatile.type] "Postfix ++ and -- expressions and
7420 prefix ++ and -- expressions of volatile-qualified arithmetic
7421 and pointer types are deprecated." */
7422 if (TREE_THIS_VOLATILE (arg
) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg
)))
7423 warning_at (location
, OPT_Wvolatile
,
7424 "%qs expression of %<volatile%>-qualified type is "
7426 ((code
== PREINCREMENT_EXPR
7427 || code
== POSTINCREMENT_EXPR
)
7430 /* Forbid using -- or ++ in C++17 on `bool'. */
7431 if (TREE_CODE (declared_type
) == BOOLEAN_TYPE
)
7433 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
7435 if (complain
& tf_error
)
7437 "use of an operand of type %qT in %<operator--%> "
7438 "is forbidden", boolean_type_node
);
7439 return error_mark_node
;
7443 if (cxx_dialect
>= cxx17
)
7445 if (complain
& tf_error
)
7447 "use of an operand of type %qT in "
7448 "%<operator++%> is forbidden in C++17",
7450 return error_mark_node
;
7452 /* Otherwise, [depr.incr.bool] says this is deprecated. */
7454 warning_at (location
, OPT_Wdeprecated
,
7455 "use of an operand of type %qT "
7456 "in %<operator++%> is deprecated",
7459 val
= boolean_increment (code
, arg
);
7461 else if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
7462 /* An rvalue has no cv-qualifiers. */
7463 val
= build2 (code
, cv_unqualified (TREE_TYPE (arg
)), arg
, inc
);
7465 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
7467 TREE_SIDE_EFFECTS (val
) = 1;
7472 /* Note that this operation never does default_conversion
7473 regardless of NOCONVERT. */
7474 return cp_build_addr_expr (arg
, complain
);
7483 argtype
= TREE_TYPE (arg
);
7484 return build1 (code
, argtype
, arg
);
7487 if (complain
& tf_error
)
7488 error_at (location
, "%s", errstring
);
7489 return error_mark_node
;
7492 /* Hook for the c-common bits that build a unary op. */
7494 build_unary_op (location_t
/*location*/,
7495 enum tree_code code
, tree xarg
, bool noconvert
)
7497 return cp_build_unary_op (code
, xarg
, noconvert
, tf_warning_or_error
);
7500 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
7501 so that it is a valid lvalue even for GENERIC by replacing
7502 (lhs = rhs) with ((lhs = rhs), lhs)
7503 (--lhs) with ((--lhs), lhs)
7504 (++lhs) with ((++lhs), lhs)
7505 and if lhs has side-effects, calling cp_stabilize_reference on it, so
7506 that it can be evaluated multiple times. */
7509 genericize_compound_lvalue (tree lvalue
)
7511 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue
, 0)))
7512 lvalue
= build2 (TREE_CODE (lvalue
), TREE_TYPE (lvalue
),
7513 cp_stabilize_reference (TREE_OPERAND (lvalue
, 0)),
7514 TREE_OPERAND (lvalue
, 1));
7515 return build2 (COMPOUND_EXPR
, TREE_TYPE (TREE_OPERAND (lvalue
, 0)),
7516 lvalue
, TREE_OPERAND (lvalue
, 0));
7519 /* Apply unary lvalue-demanding operator CODE to the expression ARG
7520 for certain kinds of expressions which are not really lvalues
7521 but which we can accept as lvalues.
7523 If ARG is not a kind of expression we can handle, return
7527 unary_complex_lvalue (enum tree_code code
, tree arg
)
7529 /* Inside a template, making these kinds of adjustments is
7530 pointless; we are only concerned with the type of the
7532 if (processing_template_decl
)
7535 /* Handle (a, b) used as an "lvalue". */
7536 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
7538 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 1), false,
7539 tf_warning_or_error
);
7540 return build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
7541 TREE_OPERAND (arg
, 0), real_result
);
7544 /* Handle (a ? b : c) used as an "lvalue". */
7545 if (TREE_CODE (arg
) == COND_EXPR
7546 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
7547 return rationalize_conditional_expr (code
, arg
, tf_warning_or_error
);
7549 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
7550 if (TREE_CODE (arg
) == MODIFY_EXPR
7551 || TREE_CODE (arg
) == PREINCREMENT_EXPR
7552 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
7553 return unary_complex_lvalue (code
, genericize_compound_lvalue (arg
));
7555 if (code
!= ADDR_EXPR
)
7558 /* Handle (a = b) used as an "lvalue" for `&'. */
7559 if (TREE_CODE (arg
) == MODIFY_EXPR
7560 || TREE_CODE (arg
) == INIT_EXPR
)
7562 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 0), false,
7563 tf_warning_or_error
);
7564 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
7566 suppress_warning (arg
/* What warning? */);
7570 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg
))
7571 || TREE_CODE (arg
) == OFFSET_REF
)
7574 /* We permit compiler to make function calls returning
7575 objects of aggregate type look like lvalues. */
7579 if (TREE_CODE (targ
) == SAVE_EXPR
)
7580 targ
= TREE_OPERAND (targ
, 0);
7582 if (TREE_CODE (targ
) == CALL_EXPR
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (targ
)))
7584 if (TREE_CODE (arg
) == SAVE_EXPR
)
7587 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, tf_warning_or_error
);
7588 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
7591 if (TREE_CODE (arg
) == SAVE_EXPR
&& INDIRECT_REF_P (targ
))
7592 return build3 (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
7593 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
7596 /* Don't let anything else be handled specially. */
7600 /* Mark EXP saying that we need to be able to take the
7601 address of it; it should not be allocated in a register.
7602 Value is true if successful. ARRAY_REF_P is true if this
7603 is for ARRAY_REF construction - in that case we don't want
7604 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
7605 it is fine to use ARRAY_REFs for vector subscripts on vector
7608 C++: we do not allow `current_class_ptr' to be addressable. */
7611 cxx_mark_addressable (tree exp
, bool array_ref_p
)
7616 switch (TREE_CODE (x
))
7618 case VIEW_CONVERT_EXPR
:
7620 && TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
7621 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x
, 0))))
7623 x
= TREE_OPERAND (x
, 0);
7628 error ("attempt to take address of bit-field");
7634 x
= TREE_OPERAND (x
, 0);
7638 if (x
== current_class_ptr
)
7640 error ("cannot take the address of %<this%>, which is an rvalue expression");
7641 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later. */
7647 /* Caller should not be trying to mark initialized
7648 constant fields addressable. */
7649 gcc_assert (DECL_LANG_SPECIFIC (x
) == 0
7650 || DECL_IN_AGGR_P (x
) == 0
7652 || DECL_EXTERNAL (x
));
7656 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
7657 && !DECL_ARTIFICIAL (x
))
7659 if (VAR_P (x
) && DECL_HARD_REGISTER (x
))
7662 ("address of explicit register variable %qD requested", x
);
7665 else if (extra_warnings
)
7667 (OPT_Wextra
, "address requested for %qD, which is declared %<register%>", x
);
7669 TREE_ADDRESSABLE (x
) = 1;
7674 TREE_ADDRESSABLE (x
) = 1;
7678 TREE_ADDRESSABLE (x
) = 1;
7682 TREE_ADDRESSABLE (x
) = 1;
7683 cxx_mark_addressable (TREE_OPERAND (x
, 0));
7691 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
7694 build_x_conditional_expr (location_t loc
, tree ifexp
, tree op1
, tree op2
,
7695 tsubst_flags_t complain
)
7697 tree orig_ifexp
= ifexp
;
7698 tree orig_op1
= op1
;
7699 tree orig_op2
= op2
;
7702 if (processing_template_decl
)
7704 /* The standard says that the expression is type-dependent if
7705 IFEXP is type-dependent, even though the eventual type of the
7706 expression doesn't dependent on IFEXP. */
7707 if (type_dependent_expression_p (ifexp
)
7708 /* As a GNU extension, the middle operand may be omitted. */
7709 || (op1
&& type_dependent_expression_p (op1
))
7710 || type_dependent_expression_p (op2
))
7711 return build_min_nt_loc (loc
, COND_EXPR
, ifexp
, op1
, op2
);
7712 ifexp
= build_non_dependent_expr (ifexp
);
7714 op1
= build_non_dependent_expr (op1
);
7715 op2
= build_non_dependent_expr (op2
);
7718 expr
= build_conditional_expr (loc
, ifexp
, op1
, op2
, complain
);
7719 if (processing_template_decl
&& expr
!= error_mark_node
)
7721 tree min
= build_min_non_dep (COND_EXPR
, expr
,
7722 orig_ifexp
, orig_op1
, orig_op2
);
7723 expr
= convert_from_reference (min
);
7728 /* Given a list of expressions, return a compound expression
7729 that performs them all and returns the value of the last of them. */
7732 build_x_compound_expr_from_list (tree list
, expr_list_kind exp
,
7733 tsubst_flags_t complain
)
7735 tree expr
= TREE_VALUE (list
);
7737 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
7738 && !CONSTRUCTOR_IS_DIRECT_INIT (expr
))
7740 if (complain
& tf_error
)
7741 pedwarn (cp_expr_loc_or_input_loc (expr
), 0,
7742 "list-initializer for non-class type must not "
7743 "be parenthesized");
7745 return error_mark_node
;
7748 if (TREE_CHAIN (list
))
7750 if (complain
& tf_error
)
7754 permerror (input_location
, "expression list treated as compound "
7755 "expression in initializer");
7758 permerror (input_location
, "expression list treated as compound "
7759 "expression in mem-initializer");
7762 permerror (input_location
, "expression list treated as compound "
7763 "expression in functional cast");
7769 return error_mark_node
;
7771 for (list
= TREE_CHAIN (list
); list
; list
= TREE_CHAIN (list
))
7772 expr
= build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list
)),
7773 expr
, TREE_VALUE (list
), NULL_TREE
,
7780 /* Like build_x_compound_expr_from_list, but using a VEC. */
7783 build_x_compound_expr_from_vec (vec
<tree
, va_gc
> *vec
, const char *msg
,
7784 tsubst_flags_t complain
)
7786 if (vec_safe_is_empty (vec
))
7788 else if (vec
->length () == 1)
7798 if (complain
& tf_error
)
7799 permerror (input_location
,
7800 "%s expression list treated as compound expression",
7803 return error_mark_node
;
7807 for (ix
= 1; vec
->iterate (ix
, &t
); ++ix
)
7808 expr
= build_x_compound_expr (EXPR_LOCATION (t
), expr
,
7809 t
, NULL_TREE
, complain
);
7815 /* Handle overloading of the ',' operator when needed. */
7818 build_x_compound_expr (location_t loc
, tree op1
, tree op2
,
7819 tree lookups
, tsubst_flags_t complain
)
7822 tree orig_op1
= op1
;
7823 tree orig_op2
= op2
;
7824 tree overload
= NULL_TREE
;
7826 if (processing_template_decl
)
7828 if (type_dependent_expression_p (op1
)
7829 || type_dependent_expression_p (op2
))
7831 result
= build_min_nt_loc (loc
, COMPOUND_EXPR
, op1
, op2
);
7833 = build_dependent_operator_type (lookups
, COMPOUND_EXPR
, false);
7836 op1
= build_non_dependent_expr (op1
);
7837 op2
= build_non_dependent_expr (op2
);
7840 result
= build_new_op (loc
, COMPOUND_EXPR
, LOOKUP_NORMAL
, op1
, op2
,
7841 NULL_TREE
, lookups
, &overload
, complain
);
7843 result
= cp_build_compound_expr (op1
, op2
, complain
);
7845 if (processing_template_decl
&& result
!= error_mark_node
)
7847 if (overload
!= NULL_TREE
)
7848 return (build_min_non_dep_op_overload
7849 (COMPOUND_EXPR
, result
, overload
, orig_op1
, orig_op2
));
7851 return build_min_non_dep (COMPOUND_EXPR
, result
, orig_op1
, orig_op2
);
7857 /* Like cp_build_compound_expr, but for the c-common bits. */
7860 build_compound_expr (location_t
/*loc*/, tree lhs
, tree rhs
)
7862 return cp_build_compound_expr (lhs
, rhs
, tf_warning_or_error
);
7865 /* Build a compound expression. */
7868 cp_build_compound_expr (tree lhs
, tree rhs
, tsubst_flags_t complain
)
7870 lhs
= convert_to_void (lhs
, ICV_LEFT_OF_COMMA
, complain
);
7872 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
7873 return error_mark_node
;
7875 if (TREE_CODE (rhs
) == TARGET_EXPR
)
7877 /* If the rhs is a TARGET_EXPR, then build the compound
7878 expression inside the target_expr's initializer. This
7879 helps the compiler to eliminate unnecessary temporaries. */
7880 tree init
= TREE_OPERAND (rhs
, 1);
7882 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), lhs
, init
);
7883 TREE_OPERAND (rhs
, 1) = init
;
7888 if (type_unknown_p (rhs
))
7890 if (complain
& tf_error
)
7891 error_at (cp_expr_loc_or_input_loc (rhs
),
7892 "no context to resolve type of %qE", rhs
);
7893 return error_mark_node
;
7896 return build2 (COMPOUND_EXPR
, TREE_TYPE (rhs
), lhs
, rhs
);
7899 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
7900 casts away constness. CAST gives the type of cast. Returns true
7901 if the cast is ill-formed, false if it is well-formed.
7903 ??? This function warns for casting away any qualifier not just
7904 const. We would like to specify exactly what qualifiers are casted
7909 check_for_casting_away_constness (location_t loc
, tree src_type
,
7910 tree dest_type
, enum tree_code cast
,
7911 tsubst_flags_t complain
)
7913 /* C-style casts are allowed to cast away constness. With
7914 WARN_CAST_QUAL, we still want to issue a warning. */
7915 if (cast
== CAST_EXPR
&& !warn_cast_qual
)
7918 if (!casts_away_constness (src_type
, dest_type
, complain
))
7924 if (complain
& tf_warning
)
7925 warning_at (loc
, OPT_Wcast_qual
,
7926 "cast from type %qT to type %qT casts away qualifiers",
7927 src_type
, dest_type
);
7930 case STATIC_CAST_EXPR
:
7931 if (complain
& tf_error
)
7932 error_at (loc
, "%<static_cast%> from type %qT to type %qT casts "
7934 src_type
, dest_type
);
7937 case REINTERPRET_CAST_EXPR
:
7938 if (complain
& tf_error
)
7939 error_at (loc
, "%<reinterpret_cast%> from type %qT to type %qT "
7940 "casts away qualifiers",
7941 src_type
, dest_type
);
7949 /* Warns if the cast from expression EXPR to type TYPE is useless. */
7951 maybe_warn_about_useless_cast (location_t loc
, tree type
, tree expr
,
7952 tsubst_flags_t complain
)
7954 if (warn_useless_cast
7955 && complain
& tf_warning
)
7957 if ((TYPE_REF_P (type
)
7958 && (TYPE_REF_IS_RVALUE (type
)
7959 ? xvalue_p (expr
) : lvalue_p (expr
))
7960 && same_type_p (TREE_TYPE (expr
), TREE_TYPE (type
)))
7961 || same_type_p (TREE_TYPE (expr
), type
))
7962 warning_at (loc
, OPT_Wuseless_cast
,
7963 "useless cast to type %q#T", type
);
7967 /* Warns if the cast ignores cv-qualifiers on TYPE. */
7969 maybe_warn_about_cast_ignoring_quals (location_t loc
, tree type
,
7970 tsubst_flags_t complain
)
7972 if (warn_ignored_qualifiers
7973 && complain
& tf_warning
7974 && !CLASS_TYPE_P (type
)
7975 && (cp_type_quals (type
) & (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)))
7976 warning_at (loc
, OPT_Wignored_qualifiers
,
7977 "type qualifiers ignored on cast result type");
7980 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
7981 (another pointer-to-member type in the same hierarchy) and return
7982 the converted expression. If ALLOW_INVERSE_P is permitted, a
7983 pointer-to-derived may be converted to pointer-to-base; otherwise,
7984 only the other direction is permitted. If C_CAST_P is true, this
7985 conversion is taking place as part of a C-style cast. */
7988 convert_ptrmem (tree type
, tree expr
, bool allow_inverse_p
,
7989 bool c_cast_p
, tsubst_flags_t complain
)
7991 if (same_type_p (type
, TREE_TYPE (expr
)))
7994 if (TYPE_PTRDATAMEM_P (type
))
7996 tree obase
= TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr
));
7997 tree nbase
= TYPE_PTRMEM_CLASS_TYPE (type
);
7998 tree delta
= (get_delta_difference
8000 allow_inverse_p
, c_cast_p
, complain
));
8002 if (delta
== error_mark_node
)
8003 return error_mark_node
;
8005 if (!same_type_p (obase
, nbase
))
8007 if (TREE_CODE (expr
) == PTRMEM_CST
)
8008 expr
= cplus_expand_constant (expr
);
8010 tree cond
= cp_build_binary_op (input_location
, EQ_EXPR
, expr
,
8011 build_int_cst (TREE_TYPE (expr
), -1),
8013 tree op1
= build_nop (ptrdiff_type_node
, expr
);
8014 tree op2
= cp_build_binary_op (input_location
, PLUS_EXPR
, op1
, delta
,
8017 expr
= fold_build3_loc (input_location
,
8018 COND_EXPR
, ptrdiff_type_node
, cond
, op1
, op2
);
8021 return build_nop (type
, expr
);
8024 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
,
8025 allow_inverse_p
, c_cast_p
, complain
);
8028 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
8029 this static_cast is being attempted as one of the possible casts
8030 allowed by a C-style cast. (In that case, accessibility of base
8031 classes is not considered, and it is OK to cast away
8032 constness.) Return the result of the cast. *VALID_P is set to
8033 indicate whether or not the cast was valid. */
8036 build_static_cast_1 (location_t loc
, tree type
, tree expr
, bool c_cast_p
,
8037 bool *valid_p
, tsubst_flags_t complain
)
8043 /* Assume the cast is valid. */
8046 intype
= unlowered_expr_type (expr
);
8048 /* Save casted types in the function's used types hash table. */
8049 used_types_insert (type
);
8051 /* A prvalue of non-class type is cv-unqualified. */
8052 if (!CLASS_TYPE_P (type
))
8053 type
= cv_unqualified (type
);
8055 /* [expr.static.cast]
8057 An lvalue of type "cv1 B", where B is a class type, can be cast
8058 to type "reference to cv2 D", where D is a class derived (clause
8059 _class.derived_) from B, if a valid standard conversion from
8060 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
8061 same cv-qualification as, or greater cv-qualification than, cv1,
8062 and B is not a virtual base class of D. */
8063 /* We check this case before checking the validity of "TYPE t =
8064 EXPR;" below because for this case:
8067 struct D : public B { D(const B&); };
8069 void f() { static_cast<const D&>(b); }
8071 we want to avoid constructing a new D. The standard is not
8072 completely clear about this issue, but our interpretation is
8073 consistent with other compilers. */
8074 if (TYPE_REF_P (type
)
8075 && CLASS_TYPE_P (TREE_TYPE (type
))
8076 && CLASS_TYPE_P (intype
)
8077 && (TYPE_REF_IS_RVALUE (type
) || lvalue_p (expr
))
8078 && DERIVED_FROM_P (intype
, TREE_TYPE (type
))
8079 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype
)),
8080 build_pointer_type (TYPE_MAIN_VARIANT
8081 (TREE_TYPE (type
))),
8084 || at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
8088 if (processing_template_decl
)
8091 /* There is a standard conversion from "D*" to "B*" even if "B"
8092 is ambiguous or inaccessible. If this is really a
8093 static_cast, then we check both for inaccessibility and
8094 ambiguity. However, if this is a static_cast being performed
8095 because the user wrote a C-style cast, then accessibility is
8097 base
= lookup_base (TREE_TYPE (type
), intype
,
8098 c_cast_p
? ba_unique
: ba_check
,
8100 expr
= cp_build_addr_expr (expr
, complain
);
8102 if (sanitize_flags_p (SANITIZE_VPTR
))
8105 = cp_ubsan_maybe_instrument_downcast (loc
, type
,
8111 /* Convert from "B*" to "D*". This function will check that "B"
8112 is not a virtual base of "D". Even if we don't have a guarantee
8113 that expr is NULL, if the static_cast is to a reference type,
8114 it is UB if it would be NULL, so omit the non-NULL check. */
8115 expr
= build_base_path (MINUS_EXPR
, expr
, base
,
8116 /*nonnull=*/flag_delete_null_pointer_checks
,
8119 /* Convert the pointer to a reference -- but then remember that
8120 there are no expressions with reference type in C++.
8122 We call rvalue so that there's an actual tree code
8123 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
8124 is a variable with the same type, the conversion would get folded
8125 away, leaving just the variable and causing lvalue_kind to give
8126 the wrong answer. */
8127 expr
= cp_fold_convert (type
, expr
);
8129 /* When -fsanitize=null, make sure to diagnose reference binding to
8130 NULL even when the reference is converted to pointer later on. */
8131 if (sanitize_flags_p (SANITIZE_NULL
)
8132 && TREE_CODE (expr
) == COND_EXPR
8133 && TREE_OPERAND (expr
, 2)
8134 && TREE_CODE (TREE_OPERAND (expr
, 2)) == INTEGER_CST
8135 && TREE_TYPE (TREE_OPERAND (expr
, 2)) == type
)
8136 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr
, 2));
8138 return convert_from_reference (rvalue (expr
));
8141 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
8142 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
8143 if (TYPE_REF_P (type
)
8144 && TYPE_REF_IS_RVALUE (type
)
8145 && (clk
= real_lvalue_p (expr
))
8146 && reference_compatible_p (TREE_TYPE (type
), intype
)
8147 && (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
8149 if (processing_template_decl
)
8151 if (clk
== clk_ordinary
)
8153 /* Handle the (non-bit-field) lvalue case here by casting to
8154 lvalue reference and then changing it to an rvalue reference.
8155 Casting an xvalue to rvalue reference will be handled by the
8157 tree lref
= cp_build_reference_type (TREE_TYPE (type
), false);
8158 result
= (perform_direct_initialization_if_possible
8159 (lref
, expr
, c_cast_p
, complain
));
8160 result
= build1 (NON_LVALUE_EXPR
, type
, result
);
8161 return convert_from_reference (result
);
8164 /* For a bit-field or packed field, bind to a temporary. */
8165 expr
= rvalue (expr
);
8168 /* Resolve overloaded address here rather than once in
8169 implicit_conversion and again in the inverse code below. */
8170 if (TYPE_PTRMEMFUNC_P (type
) && type_unknown_p (expr
))
8172 expr
= instantiate_type (type
, expr
, complain
);
8173 intype
= TREE_TYPE (expr
);
8176 /* [expr.static.cast]
8178 Any expression can be explicitly converted to type cv void. */
8179 if (VOID_TYPE_P (type
))
8180 return convert_to_void (expr
, ICV_CAST
, complain
);
8183 An abstract class shall not be used ... as the type of an explicit
8185 if (abstract_virtuals_error (ACU_CAST
, type
, complain
))
8186 return error_mark_node
;
8188 /* [expr.static.cast]
8190 An expression e can be explicitly converted to a type T using a
8191 static_cast of the form static_cast<T>(e) if the declaration T
8192 t(e);" is well-formed, for some invented temporary variable
8194 result
= perform_direct_initialization_if_possible (type
, expr
,
8195 c_cast_p
, complain
);
8196 /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
8197 which initialize the first element of the aggregate. We need to handle
8198 the array case specifically. */
8199 if (result
== NULL_TREE
8200 && cxx_dialect
>= cxx20
8201 && TREE_CODE (type
) == ARRAY_TYPE
)
8203 /* Create { EXPR } and perform direct-initialization from it. */
8204 tree e
= build_constructor_single (init_list_type_node
, NULL_TREE
, expr
);
8205 CONSTRUCTOR_IS_DIRECT_INIT (e
) = true;
8206 CONSTRUCTOR_IS_PAREN_INIT (e
) = true;
8207 result
= perform_direct_initialization_if_possible (type
, e
, c_cast_p
,
8212 if (processing_template_decl
)
8215 result
= convert_from_reference (result
);
8217 /* [expr.static.cast]
8219 If T is a reference type, the result is an lvalue; otherwise,
8220 the result is an rvalue. */
8221 if (!TYPE_REF_P (type
))
8223 result
= rvalue (result
);
8225 if (result
== expr
&& SCALAR_TYPE_P (type
))
8226 /* Leave some record of the cast. */
8227 result
= build_nop (type
, expr
);
8232 /* [expr.static.cast]
8234 The inverse of any standard conversion sequence (clause _conv_),
8235 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
8236 (_conv.array_), function-to-pointer (_conv.func_), and boolean
8237 (_conv.bool_) conversions, can be performed explicitly using
8238 static_cast subject to the restriction that the explicit
8239 conversion does not cast away constness (_expr.const.cast_), and
8240 the following additional rules for specific cases: */
8241 /* For reference, the conversions not excluded are: integral
8242 promotions, floating-point promotion, integral conversions,
8243 floating-point conversions, floating-integral conversions,
8244 pointer conversions, and pointer to member conversions. */
8247 A value of integral _or enumeration_ type can be explicitly
8248 converted to an enumeration type. */
8249 /* The effect of all that is that any conversion between any two
8250 types which are integral, floating, or enumeration types can be
8252 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
8253 || SCALAR_FLOAT_TYPE_P (type
))
8254 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
8255 || SCALAR_FLOAT_TYPE_P (intype
)))
8257 if (processing_template_decl
)
8259 return ocp_convert (type
, expr
, CONV_C_CAST
, LOOKUP_NORMAL
, complain
);
8262 if (TYPE_PTR_P (type
) && TYPE_PTR_P (intype
)
8263 && CLASS_TYPE_P (TREE_TYPE (type
))
8264 && CLASS_TYPE_P (TREE_TYPE (intype
))
8265 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
8266 (TREE_TYPE (intype
))),
8267 build_pointer_type (TYPE_MAIN_VARIANT
8268 (TREE_TYPE (type
))),
8273 if (processing_template_decl
)
8277 && check_for_casting_away_constness (loc
, intype
, type
,
8280 return error_mark_node
;
8281 base
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
8282 c_cast_p
? ba_unique
: ba_check
,
8284 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
8287 if (sanitize_flags_p (SANITIZE_VPTR
))
8290 = cp_ubsan_maybe_instrument_downcast (loc
, type
,
8296 return cp_fold_convert (type
, expr
);
8299 if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
8300 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
8307 c1
= TYPE_PTRMEM_CLASS_TYPE (intype
);
8308 c2
= TYPE_PTRMEM_CLASS_TYPE (type
);
8310 if (TYPE_PTRDATAMEM_P (type
))
8312 t1
= (build_ptrmem_type
8314 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype
))));
8315 t2
= (build_ptrmem_type
8317 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
8324 if (can_convert (t1
, t2
, complain
) || can_convert (t2
, t1
, complain
))
8327 && check_for_casting_away_constness (loc
, intype
, type
,
8330 return error_mark_node
;
8331 if (processing_template_decl
)
8333 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/1,
8334 c_cast_p
, complain
);
8338 /* [expr.static.cast]
8340 An rvalue of type "pointer to cv void" can be explicitly
8341 converted to a pointer to object type. A value of type pointer
8342 to object converted to "pointer to cv void" and back to the
8343 original pointer type will have its original value. */
8344 if (TYPE_PTR_P (intype
)
8345 && VOID_TYPE_P (TREE_TYPE (intype
))
8346 && TYPE_PTROB_P (type
))
8349 && check_for_casting_away_constness (loc
, intype
, type
,
8352 return error_mark_node
;
8353 if (processing_template_decl
)
8355 return build_nop (type
, expr
);
8359 return error_mark_node
;
8362 /* Return an expression representing static_cast<TYPE>(EXPR). */
8365 build_static_cast (location_t loc
, tree type
, tree oexpr
,
8366 tsubst_flags_t complain
)
8372 if (type
== error_mark_node
|| expr
== error_mark_node
)
8373 return error_mark_node
;
8375 bool dependent
= (dependent_type_p (type
)
8376 || type_dependent_expression_p (expr
));
8380 expr
= build_min (STATIC_CAST_EXPR
, type
, oexpr
);
8381 /* We don't know if it will or will not have side effects. */
8382 TREE_SIDE_EFFECTS (expr
) = 1;
8383 result
= convert_from_reference (expr
);
8384 protected_set_expr_location (result
, loc
);
8387 else if (processing_template_decl
)
8388 expr
= build_non_dependent_expr (expr
);
8390 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8391 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8392 if (!TYPE_REF_P (type
)
8393 && TREE_CODE (expr
) == NOP_EXPR
8394 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
8395 expr
= TREE_OPERAND (expr
, 0);
8397 result
= build_static_cast_1 (loc
, type
, expr
, /*c_cast_p=*/false,
8398 &valid_p
, complain
);
8401 if (result
!= error_mark_node
)
8403 maybe_warn_about_useless_cast (loc
, type
, expr
, complain
);
8404 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
8406 if (processing_template_decl
)
8408 protected_set_expr_location (result
, loc
);
8412 if (complain
& tf_error
)
8414 error_at (loc
, "invalid %<static_cast%> from type %qT to type %qT",
8415 TREE_TYPE (expr
), type
);
8416 if ((TYPE_PTR_P (type
) || TYPE_REF_P (type
))
8417 && CLASS_TYPE_P (TREE_TYPE (type
))
8418 && !COMPLETE_TYPE_P (TREE_TYPE (type
)))
8419 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type
))),
8420 "class type %qT is incomplete", TREE_TYPE (type
));
8421 tree expr_type
= TREE_TYPE (expr
);
8422 if (TYPE_PTR_P (expr_type
))
8423 expr_type
= TREE_TYPE (expr_type
);
8424 if (CLASS_TYPE_P (expr_type
) && !COMPLETE_TYPE_P (expr_type
))
8425 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type
)),
8426 "class type %qT is incomplete", expr_type
);
8428 return error_mark_node
;
8431 /* EXPR is an expression with member function or pointer-to-member
8432 function type. TYPE is a pointer type. Converting EXPR to TYPE is
8433 not permitted by ISO C++, but we accept it in some modes. If we
8434 are not in one of those modes, issue a diagnostic. Return the
8435 converted expression. */
8438 convert_member_func_to_ptr (tree type
, tree expr
, tsubst_flags_t complain
)
8443 intype
= TREE_TYPE (expr
);
8444 gcc_assert (TYPE_PTRMEMFUNC_P (intype
)
8445 || TREE_CODE (intype
) == METHOD_TYPE
);
8447 if (!(complain
& tf_warning_or_error
))
8448 return error_mark_node
;
8450 location_t loc
= cp_expr_loc_or_input_loc (expr
);
8452 if (pedantic
|| warn_pmf2ptr
)
8453 pedwarn (loc
, pedantic
? OPT_Wpedantic
: OPT_Wpmf_conversions
,
8454 "converting from %qH to %qI", intype
, type
);
8456 STRIP_ANY_LOCATION_WRAPPER (expr
);
8458 if (TREE_CODE (intype
) == METHOD_TYPE
)
8459 expr
= build_addr_func (expr
, complain
);
8460 else if (TREE_CODE (expr
) == PTRMEM_CST
)
8461 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
8464 decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
), 0);
8465 decl
= build_address (decl
);
8466 expr
= get_member_function_from_ptrfunc (&decl
, expr
, complain
);
8469 if (expr
== error_mark_node
)
8470 return error_mark_node
;
8472 expr
= build_nop (type
, expr
);
8473 SET_EXPR_LOCATION (expr
, loc
);
8477 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
8478 constexpr evaluation knows to reject it. */
8481 build_nop_reinterpret (tree type
, tree expr
)
8483 tree ret
= build_nop (type
, expr
);
8485 REINTERPRET_CAST_P (ret
) = true;
8489 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
8490 If C_CAST_P is true, this reinterpret cast is being done as part of
8491 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
8492 indicate whether or not reinterpret_cast was valid. */
8495 build_reinterpret_cast_1 (location_t loc
, tree type
, tree expr
,
8496 bool c_cast_p
, bool *valid_p
,
8497 tsubst_flags_t complain
)
8501 /* Assume the cast is invalid. */
8505 if (type
== error_mark_node
|| error_operand_p (expr
))
8506 return error_mark_node
;
8508 intype
= TREE_TYPE (expr
);
8510 /* Save casted types in the function's used types hash table. */
8511 used_types_insert (type
);
8513 /* A prvalue of non-class type is cv-unqualified. */
8514 if (!CLASS_TYPE_P (type
))
8515 type
= cv_unqualified (type
);
8517 /* [expr.reinterpret.cast]
8518 A glvalue of type T1, designating an object x, can be cast to the type
8519 "reference to T2" if an expression of type "pointer to T1" can be
8520 explicitly converted to the type "pointer to T2" using a reinterpret_cast.
8521 The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
8522 of type "pointer to T1". No temporary is created, no copy is made, and no
8523 constructors (11.4.4) or conversion functions (11.4.7) are called. */
8524 if (TYPE_REF_P (type
))
8526 if (!glvalue_p (expr
))
8528 if (complain
& tf_error
)
8529 error_at (loc
, "invalid cast of a prvalue expression of type "
8532 return error_mark_node
;
8535 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
8536 "B" are related class types; the reinterpret_cast does not
8537 adjust the pointer. */
8538 if (TYPE_PTR_P (intype
)
8539 && (complain
& tf_warning
)
8540 && (comptypes (TREE_TYPE (intype
), TREE_TYPE (type
),
8541 COMPARE_BASE
| COMPARE_DERIVED
)))
8542 warning_at (loc
, 0, "casting %qT to %qT does not dereference pointer",
8545 expr
= cp_build_addr_expr (expr
, complain
);
8547 if (warn_strict_aliasing
> 2)
8548 cp_strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
8550 if (expr
!= error_mark_node
)
8551 expr
= build_reinterpret_cast_1
8552 (loc
, build_pointer_type (TREE_TYPE (type
)), expr
, c_cast_p
,
8554 if (expr
!= error_mark_node
)
8555 /* cp_build_indirect_ref isn't right for rvalue refs. */
8556 expr
= convert_from_reference (fold_convert (type
, expr
));
8560 /* As a G++ extension, we consider conversions from member
8561 functions, and pointers to member functions to
8562 pointer-to-function and pointer-to-void types. If
8563 -Wno-pmf-conversions has not been specified,
8564 convert_member_func_to_ptr will issue an error message. */
8565 if ((TYPE_PTRMEMFUNC_P (intype
)
8566 || TREE_CODE (intype
) == METHOD_TYPE
)
8567 && TYPE_PTR_P (type
)
8568 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
8569 || VOID_TYPE_P (TREE_TYPE (type
))))
8570 return convert_member_func_to_ptr (type
, expr
, complain
);
8572 /* If the cast is not to a reference type, the lvalue-to-rvalue,
8573 array-to-pointer, and function-to-pointer conversions are
8575 expr
= decay_conversion (expr
, complain
);
8577 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8578 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8579 if (TREE_CODE (expr
) == NOP_EXPR
8580 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
8581 expr
= TREE_OPERAND (expr
, 0);
8583 if (error_operand_p (expr
))
8584 return error_mark_node
;
8586 intype
= TREE_TYPE (expr
);
8588 /* [expr.reinterpret.cast]
8589 A pointer can be converted to any integral type large enough to
8590 hold it. ... A value of type std::nullptr_t can be converted to
8591 an integral type; the conversion has the same meaning and
8592 validity as a conversion of (void*)0 to the integral type. */
8593 if (CP_INTEGRAL_TYPE_P (type
)
8594 && (TYPE_PTR_P (intype
) || NULLPTR_TYPE_P (intype
)))
8596 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
8598 if (complain
& tf_error
)
8599 permerror (loc
, "cast from %qH to %qI loses precision",
8602 return error_mark_node
;
8604 if (NULLPTR_TYPE_P (intype
))
8605 return build_int_cst (type
, 0);
8607 /* [expr.reinterpret.cast]
8608 A value of integral or enumeration type can be explicitly
8609 converted to a pointer. */
8610 else if (TYPE_PTR_P (type
) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype
))
8613 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
8614 || TYPE_PTR_OR_PTRMEM_P (type
))
8615 && same_type_p (type
, intype
))
8617 return rvalue (expr
);
8618 else if (TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
8620 if ((complain
& tf_warning
)
8621 && !cxx_safe_function_type_cast_p (TREE_TYPE (type
),
8622 TREE_TYPE (intype
)))
8623 warning_at (loc
, OPT_Wcast_function_type
,
8624 "cast between incompatible function types"
8625 " from %qH to %qI", intype
, type
);
8626 return build_nop_reinterpret (type
, expr
);
8628 else if (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
))
8630 if ((complain
& tf_warning
)
8631 && !cxx_safe_function_type_cast_p
8632 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type
)),
8633 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype
))))
8634 warning_at (loc
, OPT_Wcast_function_type
,
8635 "cast between incompatible pointer to member types"
8636 " from %qH to %qI", intype
, type
);
8637 return build_nop_reinterpret (type
, expr
);
8639 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
8640 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
8643 && check_for_casting_away_constness (loc
, intype
, type
,
8644 REINTERPRET_CAST_EXPR
,
8646 return error_mark_node
;
8647 /* Warn about possible alignment problems. */
8648 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
8649 && (complain
& tf_warning
)
8650 && !VOID_TYPE_P (type
)
8651 && TREE_CODE (TREE_TYPE (intype
)) != FUNCTION_TYPE
8652 && COMPLETE_TYPE_P (TREE_TYPE (type
))
8653 && COMPLETE_TYPE_P (TREE_TYPE (intype
))
8654 && min_align_of_type (TREE_TYPE (type
))
8655 > min_align_of_type (TREE_TYPE (intype
)))
8656 warning_at (loc
, OPT_Wcast_align
, "cast from %qH to %qI "
8657 "increases required alignment of target type",
8660 if (warn_strict_aliasing
<= 2)
8661 /* strict_aliasing_warning STRIP_NOPs its expr. */
8662 cp_strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
8664 return build_nop_reinterpret (type
, expr
);
8666 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
8667 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
8669 if (complain
& tf_warning
)
8670 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
8671 object pointer type or vice versa is conditionally-supported." */
8672 warning_at (loc
, OPT_Wconditionally_supported
,
8673 "casting between pointer-to-function and "
8674 "pointer-to-object is conditionally-supported");
8675 return build_nop_reinterpret (type
, expr
);
8677 else if (gnu_vector_type_p (type
) && scalarish_type_p (intype
))
8678 return convert_to_vector (type
, rvalue (expr
));
8679 else if (gnu_vector_type_p (intype
)
8680 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
8681 return convert_to_integer_nofold (type
, expr
);
8686 if (complain
& tf_error
)
8687 error_at (loc
, "invalid cast from type %qT to type %qT",
8689 return error_mark_node
;
8692 expr
= cp_convert (type
, expr
, complain
);
8693 if (TREE_CODE (expr
) == NOP_EXPR
)
8694 /* Mark any nop_expr that created as a reintepret_cast. */
8695 REINTERPRET_CAST_P (expr
) = true;
8700 build_reinterpret_cast (location_t loc
, tree type
, tree expr
,
8701 tsubst_flags_t complain
)
8705 if (type
== error_mark_node
|| expr
== error_mark_node
)
8706 return error_mark_node
;
8708 if (processing_template_decl
)
8710 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
8712 if (!TREE_SIDE_EFFECTS (t
)
8713 && type_dependent_expression_p (expr
))
8714 /* There might turn out to be side effects inside expr. */
8715 TREE_SIDE_EFFECTS (t
) = 1;
8716 r
= convert_from_reference (t
);
8717 protected_set_expr_location (r
, loc
);
8721 r
= build_reinterpret_cast_1 (loc
, type
, expr
, /*c_cast_p=*/false,
8722 /*valid_p=*/NULL
, complain
);
8723 if (r
!= error_mark_node
)
8725 maybe_warn_about_useless_cast (loc
, type
, expr
, complain
);
8726 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
8728 protected_set_expr_location (r
, loc
);
8732 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
8733 return an appropriate expression. Otherwise, return
8734 error_mark_node. If the cast is not valid, and COMPLAIN is true,
8735 then a diagnostic will be issued. If VALID_P is non-NULL, we are
8736 performing a C-style cast, its value upon return will indicate
8737 whether or not the conversion succeeded. */
8740 build_const_cast_1 (location_t loc
, tree dst_type
, tree expr
,
8741 tsubst_flags_t complain
, bool *valid_p
)
8744 tree reference_type
;
8746 /* Callers are responsible for handling error_mark_node as a
8747 destination type. */
8748 gcc_assert (dst_type
!= error_mark_node
);
8749 /* In a template, callers should be building syntactic
8750 representations of casts, not using this machinery. */
8751 gcc_assert (!processing_template_decl
);
8753 /* Assume the conversion is invalid. */
8757 if (!INDIRECT_TYPE_P (dst_type
) && !TYPE_PTRDATAMEM_P (dst_type
))
8759 if (complain
& tf_error
)
8760 error_at (loc
, "invalid use of %<const_cast%> with type %qT, "
8761 "which is not a pointer, reference, "
8762 "nor a pointer-to-data-member type", dst_type
);
8763 return error_mark_node
;
8766 if (TREE_CODE (TREE_TYPE (dst_type
)) == FUNCTION_TYPE
)
8768 if (complain
& tf_error
)
8769 error_at (loc
, "invalid use of %<const_cast%> with type %qT, "
8770 "which is a pointer or reference to a function type",
8772 return error_mark_node
;
8775 /* A prvalue of non-class type is cv-unqualified. */
8776 dst_type
= cv_unqualified (dst_type
);
8778 /* Save casted types in the function's used types hash table. */
8779 used_types_insert (dst_type
);
8781 src_type
= TREE_TYPE (expr
);
8782 /* Expressions do not really have reference types. */
8783 if (TYPE_REF_P (src_type
))
8784 src_type
= TREE_TYPE (src_type
);
8786 /* [expr.const.cast]
8788 For two object types T1 and T2, if a pointer to T1 can be explicitly
8789 converted to the type "pointer to T2" using a const_cast, then the
8790 following conversions can also be made:
8792 -- an lvalue of type T1 can be explicitly converted to an lvalue of
8793 type T2 using the cast const_cast<T2&>;
8795 -- a glvalue of type T1 can be explicitly converted to an xvalue of
8796 type T2 using the cast const_cast<T2&&>; and
8798 -- if T1 is a class type, a prvalue of type T1 can be explicitly
8799 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
8801 if (TYPE_REF_P (dst_type
))
8803 reference_type
= dst_type
;
8804 if (!TYPE_REF_IS_RVALUE (dst_type
)
8810 if (complain
& tf_error
)
8811 error_at (loc
, "invalid %<const_cast%> of an rvalue of type %qT "
8813 src_type
, dst_type
);
8814 return error_mark_node
;
8816 dst_type
= build_pointer_type (TREE_TYPE (dst_type
));
8817 src_type
= build_pointer_type (src_type
);
8821 reference_type
= NULL_TREE
;
8822 /* If the destination type is not a reference type, the
8823 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
8824 conversions are performed. */
8825 src_type
= type_decays_to (src_type
);
8826 if (src_type
== error_mark_node
)
8827 return error_mark_node
;
8830 if (TYPE_PTR_P (src_type
) || TYPE_PTRDATAMEM_P (src_type
))
8832 if (comp_ptr_ttypes_const (dst_type
, src_type
, bounds_none
))
8837 /* This cast is actually a C-style cast. Issue a warning if
8838 the user is making a potentially unsafe cast. */
8839 check_for_casting_away_constness (loc
, src_type
, dst_type
,
8840 CAST_EXPR
, complain
);
8841 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
8842 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
8843 && (complain
& tf_warning
)
8844 && min_align_of_type (TREE_TYPE (dst_type
))
8845 > min_align_of_type (TREE_TYPE (src_type
)))
8846 warning_at (loc
, OPT_Wcast_align
, "cast from %qH to %qI "
8847 "increases required alignment of target type",
8848 src_type
, dst_type
);
8852 expr
= cp_build_addr_expr (expr
, complain
);
8853 if (expr
== error_mark_node
)
8854 return error_mark_node
;
8855 expr
= build_nop (reference_type
, expr
);
8856 return convert_from_reference (expr
);
8860 expr
= decay_conversion (expr
, complain
);
8861 if (expr
== error_mark_node
)
8862 return error_mark_node
;
8864 /* build_c_cast puts on a NOP_EXPR to make the result not an
8865 lvalue. Strip such NOP_EXPRs if VALUE is being used in
8866 non-lvalue context. */
8867 if (TREE_CODE (expr
) == NOP_EXPR
8868 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
8869 expr
= TREE_OPERAND (expr
, 0);
8870 return build_nop (dst_type
, expr
);
8874 && !at_least_as_qualified_p (TREE_TYPE (dst_type
),
8875 TREE_TYPE (src_type
)))
8876 check_for_casting_away_constness (loc
, src_type
, dst_type
,
8877 CAST_EXPR
, complain
);
8880 if (complain
& tf_error
)
8881 error_at (loc
, "invalid %<const_cast%> from type %qT to type %qT",
8882 src_type
, dst_type
);
8883 return error_mark_node
;
8887 build_const_cast (location_t loc
, tree type
, tree expr
,
8888 tsubst_flags_t complain
)
8892 if (type
== error_mark_node
|| error_operand_p (expr
))
8893 return error_mark_node
;
8895 if (processing_template_decl
)
8897 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
8899 if (!TREE_SIDE_EFFECTS (t
)
8900 && type_dependent_expression_p (expr
))
8901 /* There might turn out to be side effects inside expr. */
8902 TREE_SIDE_EFFECTS (t
) = 1;
8903 r
= convert_from_reference (t
);
8904 protected_set_expr_location (r
, loc
);
8908 r
= build_const_cast_1 (loc
, type
, expr
, complain
, /*valid_p=*/NULL
);
8909 if (r
!= error_mark_node
)
8911 maybe_warn_about_useless_cast (loc
, type
, expr
, complain
);
8912 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
8914 protected_set_expr_location (r
, loc
);
8918 /* Like cp_build_c_cast, but for the c-common bits. */
8921 build_c_cast (location_t loc
, tree type
, tree expr
)
8923 return cp_build_c_cast (loc
, type
, expr
, tf_warning_or_error
);
8926 /* Like the "build_c_cast" used for c-common, but using cp_expr to
8927 preserve location information even for tree nodes that don't
8931 build_c_cast (location_t loc
, tree type
, cp_expr expr
)
8933 cp_expr result
= cp_build_c_cast (loc
, type
, expr
, tf_warning_or_error
);
8934 result
.set_location (loc
);
8938 /* Build an expression representing an explicit C-style cast to type
8939 TYPE of expression EXPR. */
8942 cp_build_c_cast (location_t loc
, tree type
, tree expr
,
8943 tsubst_flags_t complain
)
8949 if (type
== error_mark_node
|| error_operand_p (expr
))
8950 return error_mark_node
;
8952 if (processing_template_decl
)
8954 tree t
= build_min (CAST_EXPR
, type
,
8955 tree_cons (NULL_TREE
, value
, NULL_TREE
));
8956 /* We don't know if it will or will not have side effects. */
8957 TREE_SIDE_EFFECTS (t
) = 1;
8958 return convert_from_reference (t
);
8961 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
8962 'Class') should always be retained, because this information aids
8963 in method lookup. */
8964 if (objc_is_object_ptr (type
)
8965 && objc_is_object_ptr (TREE_TYPE (expr
)))
8966 return build_nop (type
, expr
);
8968 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8969 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8970 if (!TYPE_REF_P (type
)
8971 && TREE_CODE (value
) == NOP_EXPR
8972 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
8973 value
= TREE_OPERAND (value
, 0);
8975 if (TREE_CODE (type
) == ARRAY_TYPE
)
8977 /* Allow casting from T1* to T2[] because Cfront allows it.
8978 NIHCL uses it. It is not valid ISO C++ however. */
8979 if (TYPE_PTR_P (TREE_TYPE (expr
)))
8981 if (complain
& tf_error
)
8982 permerror (loc
, "ISO C++ forbids casting to an array type %qT",
8985 return error_mark_node
;
8986 type
= build_pointer_type (TREE_TYPE (type
));
8990 if (complain
& tf_error
)
8991 error_at (loc
, "ISO C++ forbids casting to an array type %qT",
8993 return error_mark_node
;
8997 if (FUNC_OR_METHOD_TYPE_P (type
))
8999 if (complain
& tf_error
)
9000 error_at (loc
, "invalid cast to function type %qT", type
);
9001 return error_mark_node
;
9004 if (TYPE_PTR_P (type
)
9005 && TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
9006 /* Casting to an integer of smaller size is an error detected elsewhere. */
9007 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (value
))
9008 /* Don't warn about converting any constant. */
9009 && !TREE_CONSTANT (value
))
9010 warning_at (loc
, OPT_Wint_to_pointer_cast
,
9011 "cast to pointer from integer of different size");
9013 /* A C-style cast can be a const_cast. */
9014 result
= build_const_cast_1 (loc
, type
, value
, complain
& tf_warning
,
9018 if (result
!= error_mark_node
)
9020 maybe_warn_about_useless_cast (loc
, type
, value
, complain
);
9021 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
9026 /* Or a static cast. */
9027 result
= build_static_cast_1 (loc
, type
, value
, /*c_cast_p=*/true,
9028 &valid_p
, complain
);
9029 /* Or a reinterpret_cast. */
9031 result
= build_reinterpret_cast_1 (loc
, type
, value
, /*c_cast_p=*/true,
9032 &valid_p
, complain
);
9033 /* The static_cast or reinterpret_cast may be followed by a
9036 /* A valid cast may result in errors if, for example, a
9037 conversion to an ambiguous base class is required. */
9038 && !error_operand_p (result
))
9042 maybe_warn_about_useless_cast (loc
, type
, value
, complain
);
9043 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
9045 /* Non-class rvalues always have cv-unqualified type. */
9046 if (!CLASS_TYPE_P (type
))
9047 type
= TYPE_MAIN_VARIANT (type
);
9048 result_type
= TREE_TYPE (result
);
9049 if (!CLASS_TYPE_P (result_type
) && !TYPE_REF_P (type
))
9050 result_type
= TYPE_MAIN_VARIANT (result_type
);
9051 /* If the type of RESULT does not match TYPE, perform a
9052 const_cast to make it match. If the static_cast or
9053 reinterpret_cast succeeded, we will differ by at most
9054 cv-qualification, so the follow-on const_cast is guaranteed
9056 if (!same_type_p (non_reference (type
), non_reference (result_type
)))
9058 result
= build_const_cast_1 (loc
, type
, result
, false, &valid_p
);
9059 gcc_assert (valid_p
);
9064 return error_mark_node
;
9067 /* Warn when a value is moved to itself with std::move. LHS is the target,
9068 RHS may be the std::move call, and LOC is the location of the whole
9072 maybe_warn_self_move (location_t loc
, tree lhs
, tree rhs
)
9074 if (!warn_self_move
)
9077 /* C++98 doesn't know move. */
9078 if (cxx_dialect
< cxx11
)
9081 if (processing_template_decl
)
9084 if (!REFERENCE_REF_P (rhs
)
9085 || TREE_CODE (TREE_OPERAND (rhs
, 0)) != CALL_EXPR
)
9087 tree fn
= TREE_OPERAND (rhs
, 0);
9088 if (!is_std_move_p (fn
))
9091 /* Just a little helper to strip * and various NOPs. */
9092 auto extract_op
= [] (tree
&op
) {
9094 while (INDIRECT_REF_P (op
))
9095 op
= TREE_OPERAND (op
, 0);
9096 op
= maybe_undo_parenthesized_ref (op
);
9097 STRIP_ANY_LOCATION_WRAPPER (op
);
9100 tree arg
= CALL_EXPR_ARG (fn
, 0);
9102 if (TREE_CODE (arg
) == ADDR_EXPR
)
9103 arg
= TREE_OPERAND (arg
, 0);
9104 tree type
= TREE_TYPE (lhs
);
9105 tree orig_lhs
= lhs
;
9107 if (cp_tree_equal (lhs
, arg
))
9109 auto_diagnostic_group d
;
9110 if (warning_at (loc
, OPT_Wself_move
,
9111 "moving %qE of type %qT to itself", orig_lhs
, type
))
9112 inform (loc
, "remove %<std::move%> call");
9116 /* For use from the C common bits. */
9118 build_modify_expr (location_t location
,
9119 tree lhs
, tree
/*lhs_origtype*/,
9120 enum tree_code modifycode
,
9121 location_t
/*rhs_location*/, tree rhs
,
9122 tree
/*rhs_origtype*/)
9124 return cp_build_modify_expr (location
, lhs
, modifycode
, rhs
,
9125 tf_warning_or_error
);
9128 /* Build an assignment expression of lvalue LHS from value RHS.
9129 MODIFYCODE is the code for a binary operator that we use
9130 to combine the old value of LHS with RHS to get the new value.
9131 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
9133 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
9136 cp_build_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
9137 tree rhs
, tsubst_flags_t complain
)
9139 lhs
= mark_lvalue_use_nonread (lhs
);
9141 tree result
= NULL_TREE
;
9143 tree lhstype
= TREE_TYPE (lhs
);
9145 tree olhstype
= lhstype
;
9146 bool plain_assign
= (modifycode
== NOP_EXPR
);
9147 bool compound_side_effects_p
= false;
9148 tree preeval
= NULL_TREE
;
9150 /* Avoid duplicate error messages from operands that had errors. */
9151 if (error_operand_p (lhs
) || error_operand_p (rhs
))
9152 return error_mark_node
;
9154 while (TREE_CODE (lhs
) == COMPOUND_EXPR
)
9156 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
9157 compound_side_effects_p
= true;
9158 lhs
= TREE_OPERAND (lhs
, 1);
9161 /* Handle control structure constructs used as "lvalues". Note that we
9162 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
9163 switch (TREE_CODE (lhs
))
9165 /* Handle --foo = 5; as these are valid constructs in C++. */
9166 case PREDECREMENT_EXPR
:
9167 case PREINCREMENT_EXPR
:
9168 if (compound_side_effects_p
)
9169 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
9170 lhs
= genericize_compound_lvalue (lhs
);
9172 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
9173 and looked through the COMPOUND_EXPRs, readd them now around
9174 the resulting lhs. */
9175 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
9177 lhs
= build2 (COMPOUND_EXPR
, lhstype
, TREE_OPERAND (olhs
, 0), lhs
);
9178 tree
*ptr
= &TREE_OPERAND (lhs
, 1);
9179 for (olhs
= TREE_OPERAND (olhs
, 1);
9180 TREE_CODE (olhs
) == COMPOUND_EXPR
;
9181 olhs
= TREE_OPERAND (olhs
, 1))
9183 *ptr
= build2 (COMPOUND_EXPR
, lhstype
,
9184 TREE_OPERAND (olhs
, 0), *ptr
);
9185 ptr
= &TREE_OPERAND (*ptr
, 1);
9191 if (compound_side_effects_p
)
9192 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
9193 lhs
= genericize_compound_lvalue (lhs
);
9194 goto maybe_add_compound
;
9198 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
9199 when neither operand has side-effects. */
9200 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
9201 return error_mark_node
;
9203 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0))
9204 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 1)));
9206 lhs
= build3 (COND_EXPR
, TREE_TYPE (lhs
),
9207 build2 (TREE_CODE (lhs
) == MIN_EXPR
? LE_EXPR
: GE_EXPR
,
9209 TREE_OPERAND (lhs
, 0),
9210 TREE_OPERAND (lhs
, 1)),
9211 TREE_OPERAND (lhs
, 0),
9212 TREE_OPERAND (lhs
, 1));
9215 /* Handle (a ? b : c) used as an "lvalue". */
9218 /* Produce (a ? (b = rhs) : (c = rhs))
9219 except that the RHS goes through a save-expr
9220 so the code to compute it is only emitted once. */
9221 if (VOID_TYPE_P (TREE_TYPE (rhs
)))
9223 if (complain
& tf_error
)
9224 error_at (cp_expr_loc_or_loc (rhs
, loc
),
9225 "void value not ignored as it ought to be");
9226 return error_mark_node
;
9229 rhs
= stabilize_expr (rhs
, &preeval
);
9231 /* Check this here to avoid odd errors when trying to convert
9232 a throw to the type of the COND_EXPR. */
9233 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
9234 return error_mark_node
;
9236 tree op1
= TREE_OPERAND (lhs
, 1);
9237 if (TREE_CODE (op1
) != THROW_EXPR
)
9238 op1
= cp_build_modify_expr (loc
, op1
, modifycode
, rhs
, complain
);
9239 /* When sanitizing undefined behavior, even when rhs doesn't need
9240 stabilization at this point, the sanitization might add extra
9241 SAVE_EXPRs in there and so make sure there is no tree sharing
9242 in the rhs, otherwise those SAVE_EXPRs will have initialization
9243 only in one of the two branches. */
9244 if (sanitize_flags_p (SANITIZE_UNDEFINED
9245 | SANITIZE_UNDEFINED_NONDEFAULT
))
9246 rhs
= unshare_expr (rhs
);
9247 tree op2
= TREE_OPERAND (lhs
, 2);
9248 if (TREE_CODE (op2
) != THROW_EXPR
)
9249 op2
= cp_build_modify_expr (loc
, op2
, modifycode
, rhs
, complain
);
9250 tree cond
= build_conditional_expr (input_location
,
9251 TREE_OPERAND (lhs
, 0), op1
, op2
,
9254 if (cond
== error_mark_node
)
9256 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
9257 and looked through the COMPOUND_EXPRs, readd them now around
9258 the resulting cond before adding the preevaluated rhs. */
9259 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
9261 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
9262 TREE_OPERAND (olhs
, 0), cond
);
9263 tree
*ptr
= &TREE_OPERAND (cond
, 1);
9264 for (olhs
= TREE_OPERAND (olhs
, 1);
9265 TREE_CODE (olhs
) == COMPOUND_EXPR
;
9266 olhs
= TREE_OPERAND (olhs
, 1))
9268 *ptr
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
9269 TREE_OPERAND (olhs
, 0), *ptr
);
9270 ptr
= &TREE_OPERAND (*ptr
, 1);
9273 /* Make sure the code to compute the rhs comes out
9274 before the split. */
9284 if (modifycode
== INIT_EXPR
)
9286 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9287 /* Do the default thing. */;
9288 else if (TREE_CODE (rhs
) == CONSTRUCTOR
)
9290 /* Compound literal. */
9291 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
9292 /* Call convert to generate an error; see PR 11063. */
9293 rhs
= convert (lhstype
, rhs
);
9294 result
= build2 (INIT_EXPR
, lhstype
, lhs
, rhs
);
9295 TREE_SIDE_EFFECTS (result
) = 1;
9298 else if (! MAYBE_CLASS_TYPE_P (lhstype
))
9299 /* Do the default thing. */;
9302 releasing_vec rhs_vec
= make_tree_vector_single (rhs
);
9303 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
9304 &rhs_vec
, lhstype
, LOOKUP_NORMAL
,
9306 if (result
== NULL_TREE
)
9307 return error_mark_node
;
9313 lhs
= require_complete_type (lhs
, complain
);
9314 if (lhs
== error_mark_node
)
9315 return error_mark_node
;
9317 if (modifycode
== NOP_EXPR
)
9319 maybe_warn_self_move (loc
, lhs
, rhs
);
9321 if (c_dialect_objc ())
9323 result
= objc_maybe_build_modify_expr (lhs
, rhs
);
9328 /* `operator=' is not an inheritable operator. */
9329 if (! MAYBE_CLASS_TYPE_P (lhstype
))
9330 /* Do the default thing. */;
9333 result
= build_new_op (input_location
, MODIFY_EXPR
,
9334 LOOKUP_NORMAL
, lhs
, rhs
,
9335 make_node (NOP_EXPR
), NULL_TREE
,
9336 /*overload=*/NULL
, complain
);
9337 if (result
== NULL_TREE
)
9338 return error_mark_node
;
9345 tree init
= NULL_TREE
;
9347 /* A binary op has been requested. Combine the old LHS
9348 value with the RHS producing the value we should actually
9349 store into the LHS. */
9350 gcc_assert (!((TYPE_REF_P (lhstype
)
9351 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype
)))
9352 || MAYBE_CLASS_TYPE_P (lhstype
)));
9354 /* An expression of the form E1 op= E2. [expr.ass] says:
9355 "Such expressions are deprecated if E1 has volatile-qualified
9356 type and op is not one of the bitwise operators |, &, ^."
9357 We warn here rather than in cp_genericize_r because
9358 for compound assignments we are supposed to warn even if the
9359 assignment is a discarded-value expression. */
9360 if (modifycode
!= BIT_AND_EXPR
9361 && modifycode
!= BIT_IOR_EXPR
9362 && modifycode
!= BIT_XOR_EXPR
9363 && (TREE_THIS_VOLATILE (lhs
) || CP_TYPE_VOLATILE_P (lhstype
)))
9364 warning_at (loc
, OPT_Wvolatile
,
9365 "compound assignment with %<volatile%>-qualified left "
9366 "operand is deprecated");
9367 /* Preevaluate the RHS to make sure its evaluation is complete
9368 before the lvalue-to-rvalue conversion of the LHS:
9370 [expr.ass] With respect to an indeterminately-sequenced
9371 function call, the operation of a compound assignment is a
9372 single evaluation. [ Note: Therefore, a function call shall
9373 not intervene between the lvalue-to-rvalue conversion and the
9374 side effect associated with any single compound assignment
9375 operator. -- end note ] */
9376 lhs
= cp_stabilize_reference (lhs
);
9377 rhs
= decay_conversion (rhs
, complain
);
9378 if (rhs
== error_mark_node
)
9379 return error_mark_node
;
9380 rhs
= stabilize_expr (rhs
, &init
);
9381 newrhs
= cp_build_binary_op (loc
, modifycode
, lhs
, rhs
, complain
);
9382 if (newrhs
== error_mark_node
)
9384 if (complain
& tf_error
)
9385 inform (loc
, " in evaluation of %<%Q(%#T, %#T)%>",
9386 modifycode
, TREE_TYPE (lhs
), TREE_TYPE (rhs
));
9387 return error_mark_node
;
9391 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), init
, newrhs
);
9393 /* Now it looks like a plain assignment. */
9394 modifycode
= NOP_EXPR
;
9395 if (c_dialect_objc ())
9397 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
9402 gcc_assert (!TYPE_REF_P (lhstype
));
9403 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs
)));
9406 /* The left-hand side must be an lvalue. */
9407 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
9408 return error_mark_node
;
9410 /* Warn about modifying something that is `const'. Don't warn if
9411 this is initialization. */
9412 if (modifycode
!= INIT_EXPR
9413 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
9414 /* Functions are not modifiable, even though they are
9416 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs
))
9417 /* If it's an aggregate and any field is const, then it is
9418 effectively const. */
9419 || (CLASS_TYPE_P (lhstype
)
9420 && C_TYPE_FIELDS_READONLY (lhstype
))))
9422 if (complain
& tf_error
)
9423 cxx_readonly_error (loc
, lhs
, lv_assign
);
9424 return error_mark_node
;
9427 /* If storing into a structure or union member, it may have been given a
9428 lowered bitfield type. We need to convert to the declared type first,
9429 so retrieve it now. */
9431 olhstype
= unlowered_expr_type (lhs
);
9433 /* Convert new value to destination type. */
9435 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
9439 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs
))
9441 if (modifycode
!= INIT_EXPR
)
9443 if (complain
& tf_error
)
9445 "assigning to an array from an initializer list");
9446 return error_mark_node
;
9448 if (check_array_initializer (lhs
, lhstype
, newrhs
))
9449 return error_mark_node
;
9450 newrhs
= digest_init (lhstype
, newrhs
, complain
);
9451 if (newrhs
== error_mark_node
)
9452 return error_mark_node
;
9455 /* C++11 8.5/17: "If the destination type is an array of characters,
9456 an array of char16_t, an array of char32_t, or an array of wchar_t,
9457 and the initializer is a string literal...". */
9458 else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs
))
9460 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype
)))
9461 && modifycode
== INIT_EXPR
)
9463 newrhs
= digest_init (lhstype
, newrhs
, complain
);
9464 if (newrhs
== error_mark_node
)
9465 return error_mark_node
;
9468 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
9469 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))))
9471 if (complain
& tf_error
)
9472 error_at (loc
, "incompatible types in assignment of %qT to %qT",
9473 TREE_TYPE (rhs
), lhstype
);
9474 return error_mark_node
;
9477 /* Allow array assignment in compiler-generated code. */
9478 else if (!current_function_decl
9479 || !DECL_DEFAULTED_FN (current_function_decl
))
9481 /* This routine is used for both initialization and assignment.
9482 Make sure the diagnostic message differentiates the context. */
9483 if (complain
& tf_error
)
9485 if (modifycode
== INIT_EXPR
)
9486 error_at (loc
, "array used as initializer");
9488 error_at (loc
, "invalid array assignment");
9490 return error_mark_node
;
9493 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
9494 ? 1 + (modifycode
!= INIT_EXPR
): 0;
9495 result
= build_vec_init (lhs
, NULL_TREE
, newrhs
,
9496 /*explicit_value_init_p=*/false,
9497 from_array
, complain
);
9501 if (modifycode
== INIT_EXPR
)
9502 /* Calls with INIT_EXPR are all direct-initialization, so don't set
9503 LOOKUP_ONLYCONVERTING. */
9504 newrhs
= convert_for_initialization (lhs
, olhstype
, newrhs
, LOOKUP_NORMAL
,
9505 ICR_INIT
, NULL_TREE
, 0,
9506 complain
| tf_no_cleanup
);
9508 newrhs
= convert_for_assignment (olhstype
, newrhs
, ICR_ASSIGN
,
9509 NULL_TREE
, 0, complain
, LOOKUP_IMPLICIT
);
9511 if (!same_type_p (lhstype
, olhstype
))
9512 newrhs
= cp_convert_and_check (lhstype
, newrhs
, complain
);
9514 if (modifycode
!= INIT_EXPR
)
9516 if (TREE_CODE (newrhs
) == CALL_EXPR
9517 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
9518 newrhs
= build_cplus_new (lhstype
, newrhs
, complain
);
9520 /* Can't initialize directly from a TARGET_EXPR, since that would
9521 cause the lhs to be constructed twice, and possibly result in
9522 accidental self-initialization. So we force the TARGET_EXPR to be
9523 expanded without a target. */
9524 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
9525 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
9526 TREE_OPERAND (newrhs
, 0));
9529 if (newrhs
== error_mark_node
)
9530 return error_mark_node
;
9532 if (c_dialect_objc () && flag_objc_gc
)
9534 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
9540 result
= build2_loc (loc
, modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
9541 lhstype
, lhs
, newrhs
);
9543 TREE_SIDE_EFFECTS (result
) = 1;
9545 suppress_warning (result
, OPT_Wparentheses
);
9549 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), preeval
, result
);
9554 build_x_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
9555 tree rhs
, tree lookups
, tsubst_flags_t complain
)
9557 tree orig_lhs
= lhs
;
9558 tree orig_rhs
= rhs
;
9559 tree overload
= NULL_TREE
;
9561 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
9562 return cp_expr (error_mark_node
, loc
);
9564 if (processing_template_decl
)
9566 if (modifycode
== NOP_EXPR
9567 || type_dependent_expression_p (lhs
)
9568 || type_dependent_expression_p (rhs
))
9570 tree op
= build_min_nt_loc (loc
, modifycode
, NULL_TREE
, NULL_TREE
);
9571 tree rval
= build_min_nt_loc (loc
, MODOP_EXPR
, lhs
, op
, rhs
);
9572 if (modifycode
!= NOP_EXPR
)
9574 = build_dependent_operator_type (lookups
, modifycode
, true);
9578 lhs
= build_non_dependent_expr (lhs
);
9579 rhs
= build_non_dependent_expr (rhs
);
9582 if (modifycode
!= NOP_EXPR
)
9584 tree op
= build_nt (modifycode
, NULL_TREE
, NULL_TREE
);
9585 tree rval
= build_new_op (loc
, MODIFY_EXPR
, LOOKUP_NORMAL
,
9586 lhs
, rhs
, op
, lookups
, &overload
, complain
);
9589 if (rval
== error_mark_node
)
9591 suppress_warning (rval
/* What warning? */);
9592 if (processing_template_decl
)
9594 if (overload
!= NULL_TREE
)
9595 return (build_min_non_dep_op_overload
9596 (MODIFY_EXPR
, rval
, overload
, orig_lhs
, orig_rhs
));
9598 return (build_min_non_dep
9599 (MODOP_EXPR
, rval
, orig_lhs
, op
, orig_rhs
));
9604 return cp_build_modify_expr (loc
, lhs
, modifycode
, rhs
, complain
);
9607 /* Helper function for get_delta_difference which assumes FROM is a base
9608 class of TO. Returns a delta for the conversion of pointer-to-member
9609 of FROM to pointer-to-member of TO. If the conversion is invalid and
9610 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
9611 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
9612 If C_CAST_P is true, this conversion is taking place as part of a
9616 get_delta_difference_1 (tree from
, tree to
, bool c_cast_p
,
9617 tsubst_flags_t complain
)
9622 binfo
= lookup_base (to
, from
, c_cast_p
? ba_unique
: ba_check
,
9625 if (binfo
== error_mark_node
)
9627 if (!(complain
& tf_error
))
9628 return error_mark_node
;
9630 inform (input_location
, " in pointer to member function conversion");
9631 return size_zero_node
;
9635 if (kind
!= bk_via_virtual
)
9636 return BINFO_OFFSET (binfo
);
9638 /* FROM is a virtual base class of TO. Issue an error or warning
9639 depending on whether or not this is a reinterpret cast. */
9641 if (!(complain
& tf_error
))
9642 return error_mark_node
;
9644 error ("pointer to member conversion via virtual base %qT",
9645 BINFO_TYPE (binfo_from_vbase (binfo
)));
9647 return size_zero_node
;
9654 /* Get difference in deltas for different pointer to member function
9655 types. If the conversion is invalid and tf_error is not set in
9656 COMPLAIN, returns error_mark_node, otherwise returns an integer
9657 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
9658 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
9659 conversions as well. If C_CAST_P is true this conversion is taking
9660 place as part of a C-style cast.
9662 Note that the naming of FROM and TO is kind of backwards; the return
9663 value is what we add to a TO in order to get a FROM. They are named
9664 this way because we call this function to find out how to convert from
9665 a pointer to member of FROM to a pointer to member of TO. */
9668 get_delta_difference (tree from
, tree to
,
9669 bool allow_inverse_p
,
9670 bool c_cast_p
, tsubst_flags_t complain
)
9674 if (same_type_ignoring_top_level_qualifiers_p (from
, to
))
9675 /* Pointer to member of incomplete class is permitted*/
9676 result
= size_zero_node
;
9678 result
= get_delta_difference_1 (from
, to
, c_cast_p
, complain
);
9680 if (result
== error_mark_node
)
9681 return error_mark_node
;
9685 if (!allow_inverse_p
)
9687 if (!(complain
& tf_error
))
9688 return error_mark_node
;
9690 error_not_base_type (from
, to
);
9691 inform (input_location
, " in pointer to member conversion");
9692 result
= size_zero_node
;
9696 result
= get_delta_difference_1 (to
, from
, c_cast_p
, complain
);
9698 if (result
== error_mark_node
)
9699 return error_mark_node
;
9702 result
= size_diffop_loc (input_location
,
9703 size_zero_node
, result
);
9706 if (!(complain
& tf_error
))
9707 return error_mark_node
;
9709 error_not_base_type (from
, to
);
9710 inform (input_location
, " in pointer to member conversion");
9711 result
= size_zero_node
;
9716 return convert_to_integer (ptrdiff_type_node
, result
);
9719 /* Return a constructor for the pointer-to-member-function TYPE using
9720 the other components as specified. */
9723 build_ptrmemfunc1 (tree type
, tree delta
, tree pfn
)
9728 vec
<constructor_elt
, va_gc
> *v
;
9730 /* Pull the FIELD_DECLs out of the type. */
9731 pfn_field
= TYPE_FIELDS (type
);
9732 delta_field
= DECL_CHAIN (pfn_field
);
9734 /* Make sure DELTA has the type we want. */
9735 delta
= convert_and_check (input_location
, delta_type_node
, delta
);
9737 /* Convert to the correct target type if necessary. */
9738 pfn
= fold_convert (TREE_TYPE (pfn_field
), pfn
);
9740 /* Finish creating the initializer. */
9742 CONSTRUCTOR_APPEND_ELT(v
, pfn_field
, pfn
);
9743 CONSTRUCTOR_APPEND_ELT(v
, delta_field
, delta
);
9744 u
= build_constructor (type
, v
);
9745 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) & TREE_CONSTANT (delta
);
9746 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
9747 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
9749 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
9754 /* Build a constructor for a pointer to member function. It can be
9755 used to initialize global variables, local variable, or used
9756 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
9759 If FORCE is nonzero, then force this conversion, even if
9760 we would rather not do it. Usually set when using an explicit
9761 cast. A C-style cast is being processed iff C_CAST_P is true.
9763 Return error_mark_node, if something goes wrong. */
9766 build_ptrmemfunc (tree type
, tree pfn
, int force
, bool c_cast_p
,
9767 tsubst_flags_t complain
)
9773 if (error_operand_p (pfn
))
9774 return error_mark_node
;
9776 pfn_type
= TREE_TYPE (pfn
);
9777 to_type
= build_ptrmemfunc_type (type
);
9779 /* Handle multiple conversions of pointer to member functions. */
9780 if (TYPE_PTRMEMFUNC_P (pfn_type
))
9782 tree delta
= NULL_TREE
;
9783 tree npfn
= NULL_TREE
;
9787 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
,
9788 LOOKUP_NORMAL
, complain
))
9790 if (complain
& tf_error
)
9791 error ("invalid conversion to type %qT from type %qT",
9794 return error_mark_node
;
9797 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
9798 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
9800 c_cast_p
, complain
);
9801 if (n
== error_mark_node
)
9802 return error_mark_node
;
9804 /* We don't have to do any conversion to convert a
9805 pointer-to-member to its own type. But, we don't want to
9806 just return a PTRMEM_CST if there's an explicit cast; that
9807 cast should make the expression an invalid template argument. */
9808 if (TREE_CODE (pfn
) != PTRMEM_CST
)
9810 if (same_type_p (to_type
, pfn_type
))
9812 else if (integer_zerop (n
) && TREE_CODE (pfn
) != CONSTRUCTOR
)
9813 return build_reinterpret_cast (input_location
, to_type
, pfn
,
9817 if (TREE_SIDE_EFFECTS (pfn
))
9818 pfn
= save_expr (pfn
);
9820 /* Obtain the function pointer and the current DELTA. */
9821 if (TREE_CODE (pfn
) == PTRMEM_CST
)
9822 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
9825 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
9826 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
9829 /* Just adjust the DELTA field. */
9830 gcc_assert (same_type_ignoring_top_level_qualifiers_p
9831 (TREE_TYPE (delta
), ptrdiff_type_node
));
9832 if (!integer_zerop (n
))
9834 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
9835 n
= cp_build_binary_op (input_location
,
9836 LSHIFT_EXPR
, n
, integer_one_node
,
9838 delta
= cp_build_binary_op (input_location
,
9839 PLUS_EXPR
, delta
, n
, complain
);
9841 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
9844 /* Handle null pointer to member function conversions. */
9845 if (null_ptr_cst_p (pfn
))
9847 pfn
= cp_build_c_cast (input_location
,
9848 TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type
),
9850 return build_ptrmemfunc1 (to_type
,
9855 if (type_unknown_p (pfn
))
9856 return instantiate_type (type
, pfn
, complain
);
9858 fn
= TREE_OPERAND (pfn
, 0);
9859 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
9860 /* In a template, we will have preserved the
9862 || (processing_template_decl
&& TREE_CODE (fn
) == OFFSET_REF
));
9863 return make_ptrmem_cst (to_type
, fn
);
9866 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
9869 ??? There is no consistency as to the types returned for the above
9870 values. Some code acts as if it were a sizetype and some as if it were
9871 integer_type_node. */
9874 expand_ptrmemfunc_cst (tree cst
, tree
*delta
, tree
*pfn
)
9876 tree type
= TREE_TYPE (cst
);
9877 tree fn
= PTRMEM_CST_MEMBER (cst
);
9878 tree ptr_class
, fn_class
;
9880 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
9882 /* The class that the function belongs to. */
9883 fn_class
= DECL_CONTEXT (fn
);
9885 /* The class that we're creating a pointer to member of. */
9886 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
9888 /* First, calculate the adjustment to the function's class. */
9889 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0,
9890 /*c_cast_p=*/0, tf_warning_or_error
);
9892 if (!DECL_VIRTUAL_P (fn
))
9894 tree t
= build_addr_func (fn
, tf_warning_or_error
);
9895 if (TREE_CODE (t
) == ADDR_EXPR
)
9896 SET_EXPR_LOCATION (t
, PTRMEM_CST_LOCATION (cst
));
9897 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), t
);
9901 /* If we're dealing with a virtual function, we have to adjust 'this'
9902 again, to point to the base which provides the vtable entry for
9903 fn; the call will do the opposite adjustment. */
9904 tree orig_class
= DECL_CONTEXT (fn
);
9905 tree binfo
= binfo_or_else (orig_class
, fn_class
);
9906 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
9907 *delta
, BINFO_OFFSET (binfo
));
9909 /* We set PFN to the vtable offset at which the function can be
9910 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
9911 case delta is shifted left, and then incremented). */
9912 *pfn
= DECL_VINDEX (fn
);
9913 *pfn
= fold_build2 (MULT_EXPR
, integer_type_node
, *pfn
,
9914 TYPE_SIZE_UNIT (vtable_entry_type
));
9916 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
9918 case ptrmemfunc_vbit_in_pfn
:
9919 *pfn
= fold_build2 (PLUS_EXPR
, integer_type_node
, *pfn
,
9923 case ptrmemfunc_vbit_in_delta
:
9924 *delta
= fold_build2 (LSHIFT_EXPR
, TREE_TYPE (*delta
),
9925 *delta
, integer_one_node
);
9926 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
9927 *delta
, integer_one_node
);
9934 *pfn
= fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), *pfn
);
9938 /* Return an expression for PFN from the pointer-to-member function
9942 pfn_from_ptrmemfunc (tree t
)
9944 if (TREE_CODE (t
) == PTRMEM_CST
)
9949 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
9954 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
9957 /* Return an expression for DELTA from the pointer-to-member function
9961 delta_from_ptrmemfunc (tree t
)
9963 if (TREE_CODE (t
) == PTRMEM_CST
)
9968 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
9973 return build_ptrmemfunc_access_expr (t
, delta_identifier
);
9976 /* Convert value RHS to type TYPE as preparation for an assignment to
9977 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
9978 implicit conversion is. If FNDECL is non-NULL, we are doing the
9979 conversion in order to pass the PARMNUMth argument of FNDECL.
9980 If FNDECL is NULL, we are doing the conversion in function pointer
9981 argument passing, conversion in initialization, etc. */
9984 convert_for_assignment (tree type
, tree rhs
,
9985 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
9986 tsubst_flags_t complain
, int flags
)
9989 enum tree_code coder
;
9991 location_t rhs_loc
= cp_expr_loc_or_input_loc (rhs
);
9992 bool has_loc
= EXPR_LOCATION (rhs
) != UNKNOWN_LOCATION
;
9993 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
9994 but preserve location wrappers. */
9995 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
9996 && !location_wrapper_p (rhs
))
9997 rhs
= TREE_OPERAND (rhs
, 0);
9999 /* Handle [dcl.init.list] direct-list-initialization from
10000 single element of enumeration with a fixed underlying type. */
10001 if (is_direct_enum_init (type
, rhs
))
10003 tree elt
= CONSTRUCTOR_ELT (rhs
, 0)->value
;
10004 if (check_narrowing (ENUM_UNDERLYING_TYPE (type
), elt
, complain
))
10006 warning_sentinel
w (warn_useless_cast
);
10007 warning_sentinel
w2 (warn_ignored_qualifiers
);
10008 rhs
= cp_build_c_cast (rhs_loc
, type
, elt
, complain
);
10011 rhs
= error_mark_node
;
10014 rhstype
= TREE_TYPE (rhs
);
10015 coder
= TREE_CODE (rhstype
);
10017 if (VECTOR_TYPE_P (type
) && coder
== VECTOR_TYPE
10018 && vector_types_convertible_p (type
, rhstype
, true))
10020 rhs
= mark_rvalue_use (rhs
);
10021 return convert (type
, rhs
);
10024 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
10025 return error_mark_node
;
10026 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
10027 return error_mark_node
;
10029 /* The RHS of an assignment cannot have void type. */
10030 if (coder
== VOID_TYPE
)
10032 if (complain
& tf_error
)
10033 error_at (rhs_loc
, "void value not ignored as it ought to be");
10034 return error_mark_node
;
10037 if (c_dialect_objc ())
10041 tree rname
= fndecl
;
10052 selector
= objc_message_selector ();
10054 if (selector
&& parmno
> 1)
10061 if (objc_compare_types (type
, rhstype
, parmno
, rname
))
10063 rhs
= mark_rvalue_use (rhs
);
10064 return convert (type
, rhs
);
10070 The expression is implicitly converted (clause _conv_) to the
10071 cv-unqualified type of the left operand.
10073 We allow bad conversions here because by the time we get to this point
10074 we are committed to doing the conversion. If we end up doing a bad
10075 conversion, convert_like will complain. */
10076 if (!can_convert_arg_bad (type
, rhstype
, rhs
, flags
, complain
))
10078 /* When -Wno-pmf-conversions is use, we just silently allow
10079 conversions from pointers-to-members to plain pointers. If
10080 the conversion doesn't work, cp_convert will complain. */
10082 && TYPE_PTR_P (type
)
10083 && TYPE_PTRMEMFUNC_P (rhstype
))
10084 rhs
= cp_convert (strip_top_quals (type
), rhs
, complain
);
10087 if (complain
& tf_error
)
10089 /* If the right-hand side has unknown type, then it is an
10090 overloaded function. Call instantiate_type to get error
10092 if (rhstype
== unknown_type_node
)
10094 tree r
= instantiate_type (type
, rhs
, tf_warning_or_error
);
10095 /* -fpermissive might allow this; recurse. */
10096 if (!seen_error ())
10097 return convert_for_assignment (type
, r
, errtype
, fndecl
,
10098 parmnum
, complain
, flags
);
10101 complain_about_bad_argument (rhs_loc
,
10106 range_label_for_type_mismatch
label (rhstype
, type
);
10107 gcc_rich_location
richloc (rhs_loc
, has_loc
? &label
: NULL
);
10110 case ICR_DEFAULT_ARGUMENT
:
10111 error_at (&richloc
,
10112 "cannot convert %qH to %qI in default argument",
10116 error_at (&richloc
,
10117 "cannot convert %qH to %qI in argument passing",
10120 case ICR_CONVERTING
:
10121 error_at (&richloc
, "cannot convert %qH to %qI",
10125 error_at (&richloc
,
10126 "cannot convert %qH to %qI in initialization",
10130 error_at (&richloc
, "cannot convert %qH to %qI in return",
10134 error_at (&richloc
,
10135 "cannot convert %qH to %qI in assignment",
10142 if (TYPE_PTR_P (rhstype
)
10143 && TYPE_PTR_P (type
)
10144 && CLASS_TYPE_P (TREE_TYPE (rhstype
))
10145 && CLASS_TYPE_P (TREE_TYPE (type
))
10146 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype
)))
10147 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
10148 (TREE_TYPE (rhstype
))),
10149 "class type %qT is incomplete", TREE_TYPE (rhstype
));
10151 return error_mark_node
;
10154 if (warn_suggest_attribute_format
)
10156 const enum tree_code codel
= TREE_CODE (type
);
10157 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
10159 && check_missing_format_attribute (type
, rhstype
)
10160 && (complain
& tf_warning
))
10164 case ICR_DEFAULT_ARGUMENT
:
10166 warning (OPT_Wsuggest_attribute_format
,
10167 "parameter %qP of %qD might be a candidate "
10168 "for a format attribute", parmnum
, fndecl
);
10170 warning (OPT_Wsuggest_attribute_format
,
10171 "parameter might be a candidate "
10172 "for a format attribute");
10174 case ICR_CONVERTING
:
10175 warning (OPT_Wsuggest_attribute_format
,
10176 "target of conversion might be a candidate "
10177 "for a format attribute");
10180 warning (OPT_Wsuggest_attribute_format
,
10181 "target of initialization might be a candidate "
10182 "for a format attribute");
10185 warning (OPT_Wsuggest_attribute_format
,
10186 "return type might be a candidate "
10187 "for a format attribute");
10190 warning (OPT_Wsuggest_attribute_format
,
10191 "left-hand side of assignment might be a candidate "
10192 "for a format attribute");
10199 /* If -Wparentheses, warn about a = b = c when a has type bool and b
10201 if (warn_parentheses
10202 && TREE_CODE (type
) == BOOLEAN_TYPE
10203 && TREE_CODE (rhs
) == MODIFY_EXPR
10204 && !warning_suppressed_p (rhs
, OPT_Wparentheses
)
10205 && TREE_CODE (TREE_TYPE (rhs
)) != BOOLEAN_TYPE
10206 && (complain
& tf_warning
)
10207 && warning_at (rhs_loc
, OPT_Wparentheses
,
10208 "suggest parentheses around assignment used as "
10210 suppress_warning (rhs
, OPT_Wparentheses
);
10212 if (complain
& tf_warning
)
10213 warn_for_address_or_pointer_of_packed_member (type
, rhs
);
10215 return perform_implicit_conversion_flags (strip_top_quals (type
), rhs
,
10219 /* Convert RHS to be of type TYPE.
10220 If EXP is nonzero, it is the target of the initialization.
10221 ERRTYPE indicates what kind of error the implicit conversion is.
10223 Two major differences between the behavior of
10224 `convert_for_assignment' and `convert_for_initialization'
10225 are that references are bashed in the former, while
10226 copied in the latter, and aggregates are assigned in
10227 the former (operator=) while initialized in the
10230 If using constructor make sure no conversion operator exists, if one does
10231 exist, an ambiguity exists. */
10234 convert_for_initialization (tree exp
, tree type
, tree rhs
, int flags
,
10235 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
10236 tsubst_flags_t complain
)
10238 enum tree_code codel
= TREE_CODE (type
);
10240 enum tree_code coder
;
10242 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
10243 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
10244 if (TREE_CODE (rhs
) == NOP_EXPR
10245 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
10246 && codel
!= REFERENCE_TYPE
)
10247 rhs
= TREE_OPERAND (rhs
, 0);
10249 if (type
== error_mark_node
10250 || rhs
== error_mark_node
10251 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
10252 return error_mark_node
;
10254 if (MAYBE_CLASS_TYPE_P (non_reference (type
)))
10256 else if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
10257 && TREE_CODE (type
) != ARRAY_TYPE
10258 && (!TYPE_REF_P (type
)
10259 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
10260 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
10261 && !TYPE_REFFN_P (type
))
10262 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
10263 rhs
= decay_conversion (rhs
, complain
);
10265 rhstype
= TREE_TYPE (rhs
);
10266 coder
= TREE_CODE (rhstype
);
10268 if (coder
== ERROR_MARK
)
10269 return error_mark_node
;
10271 /* We accept references to incomplete types, so we can
10272 return here before checking if RHS is of complete type. */
10274 if (codel
== REFERENCE_TYPE
)
10276 auto_diagnostic_group d
;
10277 /* This should eventually happen in convert_arguments. */
10278 int savew
= 0, savee
= 0;
10281 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
10282 rhs
= initialize_reference (type
, rhs
, flags
, complain
);
10285 && (warningcount
+ werrorcount
> savew
|| errorcount
> savee
))
10286 inform (get_fndecl_argument_location (fndecl
, parmnum
),
10287 "in passing argument %P of %qD", parmnum
, fndecl
);
10292 exp
= require_complete_type (exp
, complain
);
10293 if (exp
== error_mark_node
)
10294 return error_mark_node
;
10296 type
= complete_type (type
);
10298 if (DIRECT_INIT_EXPR_P (type
, rhs
))
10299 /* Don't try to do copy-initialization if we already have
10300 direct-initialization. */
10303 if (MAYBE_CLASS_TYPE_P (type
))
10304 return perform_implicit_conversion_flags (type
, rhs
, complain
, flags
);
10306 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
,
10310 /* If RETVAL is the address of, or a reference to, a local variable or
10311 temporary give an appropriate warning and return true. */
10314 maybe_warn_about_returning_address_of_local (tree retval
, location_t loc
)
10316 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
10317 tree whats_returned
= fold_for_warn (retval
);
10319 loc
= cp_expr_loc_or_input_loc (retval
);
10323 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
10324 whats_returned
= TREE_OPERAND (whats_returned
, 1);
10325 else if (CONVERT_EXPR_P (whats_returned
)
10326 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
)
10327 whats_returned
= TREE_OPERAND (whats_returned
, 0);
10332 if (TREE_CODE (whats_returned
) == TARGET_EXPR
10333 && is_std_init_list (TREE_TYPE (whats_returned
)))
10335 tree init
= TARGET_EXPR_INITIAL (whats_returned
);
10336 if (TREE_CODE (init
) == CONSTRUCTOR
)
10337 /* Pull out the array address. */
10338 whats_returned
= CONSTRUCTOR_ELT (init
, 0)->value
;
10339 else if (TREE_CODE (init
) == INDIRECT_REF
)
10340 /* The source of a trivial copy looks like *(T*)&var. */
10341 whats_returned
= TREE_OPERAND (init
, 0);
10344 STRIP_NOPS (whats_returned
);
10347 /* As a special case, we handle a call to std::move or std::forward. */
10348 if (TREE_CODE (whats_returned
) == CALL_EXPR
10349 && (is_std_move_p (whats_returned
)
10350 || is_std_forward_p (whats_returned
)))
10352 tree arg
= CALL_EXPR_ARG (whats_returned
, 0);
10353 return maybe_warn_about_returning_address_of_local (arg
, loc
);
10356 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
10358 whats_returned
= TREE_OPERAND (whats_returned
, 0);
10360 while (TREE_CODE (whats_returned
) == COMPONENT_REF
10361 || TREE_CODE (whats_returned
) == ARRAY_REF
)
10362 whats_returned
= TREE_OPERAND (whats_returned
, 0);
10364 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
10365 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
10367 if (TYPE_REF_P (valtype
))
10368 warning_at (loc
, OPT_Wreturn_local_addr
,
10369 "returning reference to temporary");
10370 else if (is_std_init_list (valtype
))
10371 warning_at (loc
, OPT_Winit_list_lifetime
,
10372 "returning temporary %<initializer_list%> does not extend "
10373 "the lifetime of the underlying array");
10377 STRIP_ANY_LOCATION_WRAPPER (whats_returned
);
10379 if (DECL_P (whats_returned
)
10380 && DECL_NAME (whats_returned
)
10381 && DECL_FUNCTION_SCOPE_P (whats_returned
)
10382 && !is_capture_proxy (whats_returned
)
10383 && !(TREE_STATIC (whats_returned
)
10384 || TREE_PUBLIC (whats_returned
)))
10386 if (VAR_P (whats_returned
)
10387 && DECL_DECOMPOSITION_P (whats_returned
)
10388 && DECL_DECOMP_BASE (whats_returned
)
10389 && DECL_HAS_VALUE_EXPR_P (whats_returned
))
10391 /* When returning address of a structured binding, if the structured
10392 binding is not a reference, continue normally, if it is a
10393 reference, recurse on the initializer of the structured
10395 tree base
= DECL_DECOMP_BASE (whats_returned
);
10396 if (TYPE_REF_P (TREE_TYPE (base
)))
10398 if (tree init
= DECL_INITIAL (base
))
10399 return maybe_warn_about_returning_address_of_local (init
, loc
);
10405 auto_diagnostic_group d
;
10406 if (TYPE_REF_P (valtype
))
10407 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
10408 "reference to local variable %qD returned",
10410 else if (is_std_init_list (valtype
))
10411 w
= warning_at (loc
, OPT_Winit_list_lifetime
,
10412 "returning local %<initializer_list%> variable %qD "
10413 "does not extend the lifetime of the underlying array",
10415 else if (POINTER_TYPE_P (valtype
)
10416 && TREE_CODE (whats_returned
) == LABEL_DECL
)
10417 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
10418 "address of label %qD returned",
10420 else if (POINTER_TYPE_P (valtype
))
10421 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
10422 "address of local variable %qD returned",
10425 inform (DECL_SOURCE_LOCATION (whats_returned
),
10433 /* Returns true if DECL is in the std namespace. */
10436 decl_in_std_namespace_p (tree decl
)
10440 decl
= decl_namespace_context (decl
);
10441 if (DECL_NAMESPACE_STD_P (decl
))
10443 /* Allow inline namespaces inside of std namespace, e.g. with
10444 --enable-symvers=gnu-versioned-namespace std::forward would be
10445 actually std::_8::forward. */
10446 if (!DECL_NAMESPACE_INLINE_P (decl
))
10448 decl
= CP_DECL_CONTEXT (decl
);
10453 /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
10456 is_std_forward_p (tree fn
)
10458 /* std::forward only takes one argument. */
10459 if (call_expr_nargs (fn
) != 1)
10462 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
10463 if (!decl_in_std_namespace_p (fndecl
))
10466 tree name
= DECL_NAME (fndecl
);
10467 return name
&& id_equal (name
, "forward");
10470 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
10473 is_std_move_p (tree fn
)
10475 /* std::move only takes one argument. */
10476 if (call_expr_nargs (fn
) != 1)
10479 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
10480 if (!decl_in_std_namespace_p (fndecl
))
10483 tree name
= DECL_NAME (fndecl
);
10484 return name
&& id_equal (name
, "move");
10487 /* Returns true if RETVAL is a good candidate for the NRVO as per
10488 [class.copy.elision]. FUNCTYPE is the type the function is declared
10492 can_do_nrvo_p (tree retval
, tree functype
)
10494 if (functype
== error_mark_node
)
10497 STRIP_ANY_LOCATION_WRAPPER (retval
);
10498 tree result
= DECL_RESULT (current_function_decl
);
10499 return (retval
!= NULL_TREE
10500 && !processing_template_decl
10501 /* Must be a local, automatic variable. */
10503 && DECL_CONTEXT (retval
) == current_function_decl
10504 && !TREE_STATIC (retval
)
10505 /* And not a lambda or anonymous union proxy. */
10506 && !DECL_HAS_VALUE_EXPR_P (retval
)
10507 && (DECL_ALIGN (retval
) <= DECL_ALIGN (result
))
10508 /* The cv-unqualified type of the returned value must be the
10509 same as the cv-unqualified return type of the
10511 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval
)),
10512 TYPE_MAIN_VARIANT (functype
))
10513 /* And the returned value must be non-volatile. */
10514 && !TYPE_VOLATILE (TREE_TYPE (retval
)));
10517 /* Like can_do_nrvo_p, but we check if we're trying to move a class
10521 can_elide_copy_prvalue_p (tree retval
, tree functype
)
10523 if (functype
== error_mark_node
)
10526 STRIP_ANY_LOCATION_WRAPPER (retval
);
10527 return (retval
!= NULL_TREE
10528 && !glvalue_p (retval
)
10529 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval
)),
10530 TYPE_MAIN_VARIANT (functype
))
10531 && !TYPE_VOLATILE (TREE_TYPE (retval
)));
10534 /* If we should treat RETVAL, an expression being returned, as if it were
10535 designated by an rvalue, returns it adjusted accordingly; otherwise, returns
10536 NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
10537 context (rather than throw). */
10540 treat_lvalue_as_rvalue_p (tree expr
, bool return_p
)
10542 if (cxx_dialect
== cxx98
)
10545 tree retval
= expr
;
10546 STRIP_ANY_LOCATION_WRAPPER (retval
);
10547 if (REFERENCE_REF_P (retval
))
10548 retval
= TREE_OPERAND (retval
, 0);
10550 /* An implicitly movable entity is a variable of automatic storage duration
10551 that is either a non-volatile object or (C++20) an rvalue reference to a
10552 non-volatile object type. */
10553 if (!(((VAR_P (retval
) && !DECL_HAS_VALUE_EXPR_P (retval
))
10554 || TREE_CODE (retval
) == PARM_DECL
)
10555 && !TREE_STATIC (retval
)
10556 && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval
)))
10557 && (TREE_CODE (TREE_TYPE (retval
)) != REFERENCE_TYPE
10558 || (cxx_dialect
>= cxx20
10559 && TYPE_REF_IS_RVALUE (TREE_TYPE (retval
))))))
10562 /* If the expression in a return or co_return statement is a (possibly
10563 parenthesized) id-expression that names an implicitly movable entity
10564 declared in the body or parameter-declaration-clause of the innermost
10565 enclosing function or lambda-expression, */
10566 if (DECL_CONTEXT (retval
) != current_function_decl
)
10569 return set_implicit_rvalue_p (move (expr
));
10571 /* if the operand of a throw-expression is a (possibly parenthesized)
10572 id-expression that names an implicitly movable entity whose scope does not
10573 extend beyond the compound-statement of the innermost try-block or
10574 function-try-block (if any) whose compound-statement or ctor-initializer
10575 encloses the throw-expression, */
10577 /* C++20 added move on throw of parms. */
10578 if (TREE_CODE (retval
) == PARM_DECL
&& cxx_dialect
< cxx20
)
10581 for (cp_binding_level
*b
= current_binding_level
;
10582 ; b
= b
->level_chain
)
10584 for (tree decl
= b
->names
; decl
; decl
= TREE_CHAIN (decl
))
10585 if (decl
== retval
)
10586 return set_implicit_rvalue_p (move (expr
));
10587 if (b
->kind
== sk_function_parms
|| b
->kind
== sk_try
)
10592 /* Warn about dubious usage of std::move (in a return statement, if RETURN_P
10593 is true). EXPR is the std::move expression; TYPE is the type of the object
10594 being initialized. */
10597 maybe_warn_pessimizing_move (tree expr
, tree type
, bool return_p
)
10599 if (!(warn_pessimizing_move
|| warn_redundant_move
))
10602 const location_t loc
= cp_expr_loc_or_input_loc (expr
);
10604 /* C++98 doesn't know move. */
10605 if (cxx_dialect
< cxx11
)
10608 /* Wait until instantiation time, since we can't gauge if we should do
10609 the NRVO until then. */
10610 if (processing_template_decl
)
10613 /* This is only interesting for class types. */
10614 if (!CLASS_TYPE_P (type
))
10617 bool wrapped_p
= false;
10618 /* A a = std::move (A()); */
10619 if (TREE_CODE (expr
) == TREE_LIST
)
10621 if (list_length (expr
) == 1)
10623 expr
= TREE_VALUE (expr
);
10629 /* A a = {std::move (A())};
10630 A a{std::move (A())}; */
10631 else if (TREE_CODE (expr
) == CONSTRUCTOR
)
10633 if (CONSTRUCTOR_NELTS (expr
) == 1)
10635 expr
= CONSTRUCTOR_ELT (expr
, 0)->value
;
10642 /* First, check if this is a call to std::move. */
10643 if (!REFERENCE_REF_P (expr
)
10644 || TREE_CODE (TREE_OPERAND (expr
, 0)) != CALL_EXPR
)
10646 tree fn
= TREE_OPERAND (expr
, 0);
10647 if (!is_std_move_p (fn
))
10649 tree arg
= CALL_EXPR_ARG (fn
, 0);
10650 if (TREE_CODE (arg
) != NOP_EXPR
)
10652 /* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
10653 and implicitly-movable warnings. */
10654 if (TREE_CODE (TREE_OPERAND (arg
, 0)) == ADDR_EXPR
)
10656 arg
= TREE_OPERAND (arg
, 0);
10657 arg
= TREE_OPERAND (arg
, 0);
10658 arg
= convert_from_reference (arg
);
10659 if (can_elide_copy_prvalue_p (arg
, type
))
10661 auto_diagnostic_group d
;
10662 if (warning_at (loc
, OPT_Wpessimizing_move
,
10663 "moving a temporary object prevents copy elision"))
10664 inform (loc
, "remove %<std::move%> call");
10666 /* The rest of the warnings is only relevant for when we are returning
10667 from a function. */
10672 /* Warn if we could do copy elision were it not for the move. */
10673 if (can_do_nrvo_p (arg
, type
))
10675 auto_diagnostic_group d
;
10676 if (!warning_suppressed_p (expr
, OPT_Wpessimizing_move
)
10677 && warning_at (loc
, OPT_Wpessimizing_move
,
10678 "moving a local object in a return statement "
10679 "prevents copy elision"))
10680 inform (loc
, "remove %<std::move%> call");
10682 /* Warn if the move is redundant. It is redundant when we would
10683 do maybe-rvalue overload resolution even without std::move. */
10684 else if (warn_redundant_move
10685 /* This doesn't apply for return {std::move (t)};. */
10687 && !warning_suppressed_p (expr
, OPT_Wredundant_move
)
10688 && (moved
= treat_lvalue_as_rvalue_p (arg
, /*return*/true)))
10690 /* Make sure that overload resolution would actually succeed
10691 if we removed the std::move call. */
10692 tree t
= convert_for_initialization (NULL_TREE
, type
,
10695 | LOOKUP_ONLYCONVERTING
10696 | LOOKUP_PREFER_RVALUE
),
10697 ICR_RETURN
, NULL_TREE
, 0,
10699 /* If this worked, implicit rvalue would work, so the call to
10700 std::move is redundant. */
10701 if (t
!= error_mark_node
10702 /* Trying to move something const will never succeed unless
10703 there's T(const T&&), which it almost never is, and if
10704 so, T wouldn't be error_mark_node now: the above convert_
10705 call with LOOKUP_PREFER_RVALUE returns an error if a const T&
10706 overload is selected. */
10707 || (CP_TYPE_CONST_P (TREE_TYPE (arg
))
10708 && same_type_ignoring_top_level_qualifiers_p
10709 (TREE_TYPE (arg
), type
)))
10711 auto_diagnostic_group d
;
10712 if (warning_at (loc
, OPT_Wredundant_move
,
10713 "redundant move in return statement"))
10714 inform (loc
, "remove %<std::move%> call");
10718 /* Also try to warn about redundant std::move in code such as
10721 return std::move(t);
10723 for which EXPR will be something like
10724 *std::move<const T&> ((const struct T &) (const struct T *) t)
10725 and where the std::move does nothing if T does not have a T(const T&&)
10726 constructor, because the argument is const. It will not use T(T&&)
10727 because that would mean losing the const. */
10728 else if (TYPE_REF_P (TREE_TYPE (arg
))
10729 && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg
))))
10731 tree rtype
= TREE_TYPE (TREE_TYPE (arg
));
10732 if (!same_type_ignoring_top_level_qualifiers_p (rtype
, type
))
10734 /* Check for the unlikely case there's T(const T&&) (we don't care if
10736 for (tree fn
: ovl_range (CLASSTYPE_CONSTRUCTORS (rtype
)))
10737 if (move_fn_p (fn
))
10739 tree t
= TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn
));
10740 if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t
))))
10743 auto_diagnostic_group d
;
10744 if (warning_at (loc
, OPT_Wredundant_move
,
10745 "redundant move in return statement"))
10746 inform (loc
, "remove %<std::move%> call");
10750 /* Check that returning RETVAL from the current function is valid.
10751 Return an expression explicitly showing all conversions required to
10752 change RETVAL into the function return type, and to assign it to
10753 the DECL_RESULT for the function. Set *NO_WARNING to true if
10754 code reaches end of non-void function warning shouldn't be issued
10755 on this RETURN_EXPR. */
10758 check_return_expr (tree retval
, bool *no_warning
)
10761 /* The type actually returned by the function. */
10763 /* The type the function is declared to return, or void if
10764 the declared type is incomplete. */
10766 int fn_returns_value_p
;
10767 location_t loc
= cp_expr_loc_or_input_loc (retval
);
10769 *no_warning
= false;
10771 /* A `volatile' function is one that isn't supposed to return, ever.
10772 (This is a G++ extension, used to get better code for functions
10773 that call the `volatile' function.) */
10774 if (TREE_THIS_VOLATILE (current_function_decl
))
10775 warning (0, "function declared %<noreturn%> has a %<return%> statement");
10777 /* Check for various simple errors. */
10778 if (DECL_DESTRUCTOR_P (current_function_decl
))
10781 error_at (loc
, "returning a value from a destructor");
10783 if (targetm
.cxx
.cdtor_returns_this () && !processing_template_decl
)
10784 retval
= current_class_ptr
;
10788 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
10790 if (in_function_try_handler
)
10791 /* If a return statement appears in a handler of the
10792 function-try-block of a constructor, the program is ill-formed. */
10793 error ("cannot return from a handler of a function-try-block of a constructor");
10795 /* You can't return a value from a constructor. */
10796 error_at (loc
, "returning a value from a constructor");
10798 if (targetm
.cxx
.cdtor_returns_this () && !processing_template_decl
)
10799 retval
= current_class_ptr
;
10804 const tree saved_retval
= retval
;
10806 if (processing_template_decl
)
10808 current_function_returns_value
= 1;
10810 if (check_for_bare_parameter_packs (retval
))
10811 return error_mark_node
;
10813 /* If one of the types might be void, we can't tell whether we're
10814 returning a value. */
10815 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl
)))
10816 && !FNDECL_USED_AUTO (current_function_decl
))
10817 || (retval
!= NULL_TREE
10818 && (TREE_TYPE (retval
) == NULL_TREE
10819 || WILDCARD_TYPE_P (TREE_TYPE (retval
)))))
10823 functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
10825 /* Deduce auto return type from a return statement. */
10826 if (FNDECL_USED_AUTO (current_function_decl
))
10828 tree pattern
= DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
);
10832 if (!retval
&& !is_auto (pattern
))
10834 /* Give a helpful error message. */
10835 error ("return-statement with no value, in function returning %qT",
10837 inform (input_location
, "only plain %<auto%> return type can be "
10838 "deduced to %<void%>");
10839 type
= error_mark_node
;
10841 else if (retval
&& BRACE_ENCLOSED_INITIALIZER_P (retval
))
10843 error ("returning initializer list");
10844 type
= error_mark_node
;
10849 retval
= void_node
;
10850 auto_node
= type_uses_auto (pattern
);
10851 type
= do_auto_deduction (pattern
, retval
, auto_node
,
10852 tf_warning_or_error
, adc_return_type
);
10855 if (type
== error_mark_node
)
10857 else if (functype
== pattern
)
10858 apply_deduced_return_type (current_function_decl
, type
);
10859 else if (!same_type_p (type
, functype
))
10861 if (LAMBDA_FUNCTION_P (current_function_decl
))
10862 error_at (loc
, "inconsistent types %qT and %qT deduced for "
10863 "lambda return type", functype
, type
);
10865 error_at (loc
, "inconsistent deduction for auto return type: "
10866 "%qT and then %qT", functype
, type
);
10871 result
= DECL_RESULT (current_function_decl
);
10872 valtype
= TREE_TYPE (result
);
10873 gcc_assert (valtype
!= NULL_TREE
);
10874 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
10876 /* Check for a return statement with no return value in a function
10877 that's supposed to return a value. */
10878 if (!retval
&& fn_returns_value_p
)
10880 if (functype
!= error_mark_node
)
10881 permerror (input_location
, "return-statement with no value, in "
10882 "function returning %qT", valtype
);
10883 /* Remember that this function did return. */
10884 current_function_returns_value
= 1;
10885 /* And signal caller that TREE_NO_WARNING should be set on the
10886 RETURN_EXPR to avoid control reaches end of non-void function
10887 warnings in tree-cfg.cc. */
10888 *no_warning
= true;
10890 /* Check for a return statement with a value in a function that
10891 isn't supposed to return a value. */
10892 else if (retval
&& !fn_returns_value_p
)
10894 if (VOID_TYPE_P (TREE_TYPE (retval
)))
10895 /* You can return a `void' value from a function of `void'
10896 type. In that case, we have to evaluate the expression for
10897 its side-effects. */
10898 finish_expr_stmt (retval
);
10899 else if (retval
!= error_mark_node
)
10900 permerror (loc
, "return-statement with a value, in function "
10901 "returning %qT", valtype
);
10902 current_function_returns_null
= 1;
10904 /* There's really no value to return, after all. */
10908 /* Remember that this function can sometimes return without a
10910 current_function_returns_null
= 1;
10912 /* Remember that this function did return a value. */
10913 current_function_returns_value
= 1;
10915 /* Check for erroneous operands -- but after giving ourselves a
10916 chance to provide an error about returning a value from a void
10918 if (error_operand_p (retval
))
10920 current_function_return_value
= error_mark_node
;
10921 return error_mark_node
;
10924 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
10925 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl
))
10926 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
10927 && ! flag_check_new
10928 && retval
&& null_ptr_cst_p (retval
))
10929 warning (0, "%<operator new%> must not return NULL unless it is "
10930 "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
10932 /* Effective C++ rule 15. See also start_function. */
10934 && DECL_NAME (current_function_decl
) == assign_op_identifier
10935 && !type_dependent_expression_p (retval
))
10939 /* The function return type must be a reference to the current
10941 if (TYPE_REF_P (valtype
)
10942 && same_type_ignoring_top_level_qualifiers_p
10943 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
)))
10945 /* Returning '*this' is obviously OK. */
10946 if (retval
== current_class_ref
)
10948 /* If we are calling a function whose return type is the same of
10949 the current class reference, it is ok. */
10950 else if (INDIRECT_REF_P (retval
)
10951 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
10956 warning_at (loc
, OPT_Weffc__
,
10957 "%<operator=%> should return a reference to %<*this%>");
10960 if (dependent_type_p (functype
)
10961 || type_dependent_expression_p (retval
))
10964 /* We should not have changed the return value. */
10965 gcc_assert (retval
== saved_retval
);
10966 /* We don't know if this is an lvalue or rvalue use, but
10967 either way we can mark it as read. */
10968 mark_exp_read (retval
);
10969 /* Disable our std::move warnings when we're returning
10970 a dependent expression (c++/89780). */
10971 if (retval
&& TREE_CODE (retval
) == CALL_EXPR
)
10972 /* This also suppresses -Wredundant-move. */
10973 suppress_warning (retval
, OPT_Wpessimizing_move
);
10977 /* The fabled Named Return Value optimization, as per [class.copy]/15:
10979 [...] For a function with a class return type, if the expression
10980 in the return statement is the name of a local object, and the cv-
10981 unqualified type of the local object is the same as the function
10982 return type, an implementation is permitted to omit creating the tem-
10983 porary object to hold the function return value [...]
10985 So, if this is a value-returning function that always returns the same
10986 local variable, remember it.
10988 It might be nice to be more flexible, and choose the first suitable
10989 variable even if the function sometimes returns something else, but
10990 then we run the risk of clobbering the variable we chose if the other
10991 returned expression uses the chosen variable somehow. And people expect
10992 this restriction, anyway. (jason 2000-11-19)
10994 See finish_function and finalize_nrv for the rest of this optimization. */
10995 tree bare_retval
= NULL_TREE
;
10998 retval
= maybe_undo_parenthesized_ref (retval
);
10999 bare_retval
= tree_strip_any_location_wrapper (retval
);
11002 bool named_return_value_okay_p
= can_do_nrvo_p (bare_retval
, functype
);
11003 if (fn_returns_value_p
&& flag_elide_constructors
)
11005 if (named_return_value_okay_p
11006 && (current_function_return_value
== NULL_TREE
11007 || current_function_return_value
== bare_retval
))
11008 current_function_return_value
= bare_retval
;
11010 current_function_return_value
= error_mark_node
;
11013 /* We don't need to do any conversions when there's nothing being
11018 if (!named_return_value_okay_p
)
11019 maybe_warn_pessimizing_move (retval
, functype
, /*return_p*/true);
11021 /* Do any required conversions. */
11022 if (bare_retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
11023 /* No conversions are required. */
11027 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
11029 /* The functype's return type will have been set to void, if it
11030 was an incomplete type. Just treat this as 'return;' */
11031 if (VOID_TYPE_P (functype
))
11032 return error_mark_node
;
11034 if (processing_template_decl
)
11035 retval
= build_non_dependent_expr (retval
);
11037 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
11038 treated as an rvalue for the purposes of overload resolution to
11039 favor move constructors over copy constructors.
11041 Note that these conditions are similar to, but not as strict as,
11042 the conditions for the named return value optimization. */
11043 bool converted
= false;
11045 /* This is only interesting for class type. */
11046 if (CLASS_TYPE_P (functype
)
11047 && (moved
= treat_lvalue_as_rvalue_p (retval
, /*return*/true)))
11049 if (cxx_dialect
< cxx20
)
11051 moved
= convert_for_initialization
11052 (NULL_TREE
, functype
, moved
, flags
|LOOKUP_PREFER_RVALUE
,
11053 ICR_RETURN
, NULL_TREE
, 0, tf_none
);
11054 if (moved
!= error_mark_node
)
11061 /* In C++20 we just treat the return value as an rvalue that
11062 can bind to lvalue refs. */
11066 /* The call in a (lambda) thunk needs no conversions. */
11067 if (TREE_CODE (retval
) == CALL_EXPR
11068 && call_from_lambda_thunk_p (retval
))
11071 /* First convert the value to the function's return type, then
11072 to the type of return value's location to handle the
11073 case that functype is smaller than the valtype. */
11075 retval
= convert_for_initialization
11076 (NULL_TREE
, functype
, retval
, flags
, ICR_RETURN
, NULL_TREE
, 0,
11077 tf_warning_or_error
);
11078 retval
= convert (valtype
, retval
);
11080 /* If the conversion failed, treat this just like `return;'. */
11081 if (retval
== error_mark_node
)
11083 /* We can't initialize a register from a AGGR_INIT_EXPR. */
11084 else if (! cfun
->returns_struct
11085 && TREE_CODE (retval
) == TARGET_EXPR
11086 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
11087 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
11088 TREE_OPERAND (retval
, 0));
11089 else if (!processing_template_decl
11090 && maybe_warn_about_returning_address_of_local (retval
, loc
)
11091 && INDIRECT_TYPE_P (valtype
))
11092 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
11093 build_zero_cst (TREE_TYPE (retval
)));
11096 if (processing_template_decl
)
11097 return saved_retval
;
11099 /* Actually copy the value returned into the appropriate location. */
11100 if (retval
&& retval
!= result
)
11101 retval
= build2 (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
11103 if (tree set
= maybe_set_retval_sentinel ())
11104 retval
= build2 (COMPOUND_EXPR
, void_type_node
, retval
, set
);
11110 /* Returns nonzero if the pointer-type FROM can be converted to the
11111 pointer-type TO via a qualification conversion. If CONSTP is -1,
11112 then we return nonzero if the pointers are similar, and the
11113 cv-qualification signature of FROM is a proper subset of that of TO.
11115 If CONSTP is positive, then all outer pointers have been
11116 const-qualified. */
11119 comp_ptr_ttypes_real (tree to
, tree from
, int constp
)
11121 bool to_more_cv_qualified
= false;
11122 bool is_opaque_pointer
= false;
11124 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
11126 if (TREE_CODE (to
) != TREE_CODE (from
))
11129 if (TREE_CODE (from
) == OFFSET_TYPE
11130 && !same_type_p (TYPE_OFFSET_BASETYPE (from
),
11131 TYPE_OFFSET_BASETYPE (to
)))
11134 /* Const and volatile mean something different for function and
11135 array types, so the usual checks are not appropriate. We'll
11136 check the array type elements in further iterations. */
11137 if (!FUNC_OR_METHOD_TYPE_P (to
) && TREE_CODE (to
) != ARRAY_TYPE
)
11139 if (!at_least_as_qualified_p (to
, from
))
11142 if (!at_least_as_qualified_p (from
, to
))
11146 to_more_cv_qualified
= true;
11150 constp
&= TYPE_READONLY (to
);
11153 if (VECTOR_TYPE_P (to
))
11154 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
11156 /* P0388R4 allows a conversion from int[N] to int[] but not the
11157 other way round. When both arrays have bounds but they do
11158 not match, then no conversion is possible. */
11159 if (TREE_CODE (to
) == ARRAY_TYPE
11160 && !comp_array_types (to
, from
, bounds_first
, /*strict=*/false))
11163 if (!TYPE_PTR_P (to
)
11164 && !TYPE_PTRDATAMEM_P (to
)
11165 /* CWG 330 says we need to look through arrays. */
11166 && TREE_CODE (to
) != ARRAY_TYPE
)
11167 return ((constp
>= 0 || to_more_cv_qualified
)
11168 && (is_opaque_pointer
11169 || same_type_ignoring_top_level_qualifiers_p (to
, from
)));
11173 /* When comparing, say, char ** to char const **, this function takes
11174 the 'char *' and 'char const *'. Do not pass non-pointer/reference
11175 types to this function. */
11178 comp_ptr_ttypes (tree to
, tree from
)
11180 return comp_ptr_ttypes_real (to
, from
, 1);
11183 /* Returns true iff FNTYPE is a non-class type that involves
11184 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
11185 if a parameter type is ill-formed. */
11188 error_type_p (const_tree type
)
11192 switch (TREE_CODE (type
))
11198 case REFERENCE_TYPE
:
11200 return error_type_p (TREE_TYPE (type
));
11202 case FUNCTION_TYPE
:
11204 if (error_type_p (TREE_TYPE (type
)))
11206 for (t
= TYPE_ARG_TYPES (type
); t
; t
= TREE_CHAIN (t
))
11207 if (error_type_p (TREE_VALUE (t
)))
11212 if (TYPE_PTRMEMFUNC_P (type
))
11213 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type
));
11221 /* Returns true if to and from are (possibly multi-level) pointers to the same
11222 type or inheritance-related types, regardless of cv-quals. */
11225 ptr_reasonably_similar (const_tree to
, const_tree from
)
11227 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
11229 /* Any target type is similar enough to void. */
11230 if (VOID_TYPE_P (to
))
11231 return !error_type_p (from
);
11232 if (VOID_TYPE_P (from
))
11233 return !error_type_p (to
);
11235 if (TREE_CODE (to
) != TREE_CODE (from
))
11238 if (TREE_CODE (from
) == OFFSET_TYPE
11239 && comptypes (TYPE_OFFSET_BASETYPE (to
),
11240 TYPE_OFFSET_BASETYPE (from
),
11241 COMPARE_BASE
| COMPARE_DERIVED
))
11244 if (VECTOR_TYPE_P (to
)
11245 && vector_types_convertible_p (to
, from
, false))
11248 if (TREE_CODE (to
) == INTEGER_TYPE
11249 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
11252 if (TREE_CODE (to
) == FUNCTION_TYPE
)
11253 return !error_type_p (to
) && !error_type_p (from
);
11255 if (!TYPE_PTR_P (to
))
11257 /* When either type is incomplete avoid DERIVED_FROM_P,
11258 which may call complete_type (c++/57942). */
11259 bool b
= !COMPLETE_TYPE_P (to
) || !COMPLETE_TYPE_P (from
);
11261 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
11262 b
? COMPARE_STRICT
: COMPARE_BASE
| COMPARE_DERIVED
);
11267 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
11268 pointer-to-member types) are the same, ignoring cv-qualification at
11269 all levels. CB says how we should behave when comparing array bounds. */
11272 comp_ptr_ttypes_const (tree to
, tree from
, compare_bounds_t cb
)
11274 bool is_opaque_pointer
= false;
11276 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
11278 if (TREE_CODE (to
) != TREE_CODE (from
))
11281 if (TREE_CODE (from
) == OFFSET_TYPE
11282 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
11283 TYPE_OFFSET_BASETYPE (to
)))
11286 if (VECTOR_TYPE_P (to
))
11287 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
11289 if (TREE_CODE (to
) == ARRAY_TYPE
11290 /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
11292 && !comp_array_types (to
, from
, cb
, /*strict=*/false))
11295 /* CWG 330 says we need to look through arrays. */
11296 if (!TYPE_PTR_P (to
) && TREE_CODE (to
) != ARRAY_TYPE
)
11297 return (is_opaque_pointer
11298 || same_type_ignoring_top_level_qualifiers_p (to
, from
));
11302 /* Returns the type qualifiers for this type, including the qualifiers on the
11303 elements for an array type. */
11306 cp_type_quals (const_tree type
)
11309 /* This CONST_CAST is okay because strip_array_types returns its
11310 argument unmodified and we assign it to a const_tree. */
11311 type
= strip_array_types (CONST_CAST_TREE (type
));
11312 if (type
== error_mark_node
11313 /* Quals on a FUNCTION_TYPE are memfn quals. */
11314 || TREE_CODE (type
) == FUNCTION_TYPE
)
11315 return TYPE_UNQUALIFIED
;
11316 quals
= TYPE_QUALS (type
);
11317 /* METHOD and REFERENCE_TYPEs should never have quals. */
11318 gcc_assert ((TREE_CODE (type
) != METHOD_TYPE
11319 && !TYPE_REF_P (type
))
11320 || ((quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
))
11321 == TYPE_UNQUALIFIED
));
11325 /* Returns the function-ref-qualifier for TYPE */
11328 type_memfn_rqual (const_tree type
)
11330 gcc_assert (FUNC_OR_METHOD_TYPE_P (type
));
11332 if (!FUNCTION_REF_QUALIFIED (type
))
11333 return REF_QUAL_NONE
;
11334 else if (FUNCTION_RVALUE_QUALIFIED (type
))
11335 return REF_QUAL_RVALUE
;
11337 return REF_QUAL_LVALUE
;
11340 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
11344 type_memfn_quals (const_tree type
)
11346 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11347 return TYPE_QUALS (type
);
11348 else if (TREE_CODE (type
) == METHOD_TYPE
)
11349 return cp_type_quals (class_of_this_parm (type
));
11351 gcc_unreachable ();
11354 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
11355 MEMFN_QUALS and its ref-qualifier to RQUAL. */
11358 apply_memfn_quals (tree type
, cp_cv_quals memfn_quals
, cp_ref_qualifier rqual
)
11360 /* Could handle METHOD_TYPE here if necessary. */
11361 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
11362 if (TYPE_QUALS (type
) == memfn_quals
11363 && type_memfn_rqual (type
) == rqual
)
11366 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
11368 tree result
= build_qualified_type (type
, memfn_quals
);
11369 return build_ref_qualified_type (result
, rqual
);
11372 /* Returns nonzero if TYPE is const or volatile. */
11375 cv_qualified_p (const_tree type
)
11377 int quals
= cp_type_quals (type
);
11378 return (quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)) != 0;
11381 /* Returns nonzero if the TYPE contains a mutable member. */
11384 cp_has_mutable_p (const_tree type
)
11386 /* This CONST_CAST is okay because strip_array_types returns its
11387 argument unmodified and we assign it to a const_tree. */
11388 type
= strip_array_types (CONST_CAST_TREE(type
));
11390 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
11393 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
11394 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
11395 approximation. In particular, consider:
11398 struct S { int i; };
11399 const S s = { f(); }
11401 Here, we will make "s" as TREE_READONLY (because it is declared
11402 "const") -- only to reverse ourselves upon seeing that the
11403 initializer is non-constant. */
11406 cp_apply_type_quals_to_decl (int type_quals
, tree decl
)
11408 tree type
= TREE_TYPE (decl
);
11410 if (type
== error_mark_node
)
11413 if (TREE_CODE (decl
) == TYPE_DECL
)
11416 gcc_assert (!(TREE_CODE (type
) == FUNCTION_TYPE
11417 && type_quals
!= TYPE_UNQUALIFIED
));
11419 /* Avoid setting TREE_READONLY incorrectly. */
11420 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
11421 constructor can produce constant init, so rely on cp_finish_decl to
11422 clear TREE_READONLY if the variable has non-constant init. */
11424 /* If the type has (or might have) a mutable component, that component
11425 might be modified. */
11426 if (TYPE_HAS_MUTABLE_P (type
) || !COMPLETE_TYPE_P (type
))
11427 type_quals
&= ~TYPE_QUAL_CONST
;
11429 c_apply_type_quals_to_decl (type_quals
, decl
);
11432 /* Subroutine of casts_away_constness. Make T1 and T2 point at
11433 exemplar types such that casting T1 to T2 is casting away constness
11434 if and only if there is no implicit conversion from T1 to T2. */
11437 casts_away_constness_r (tree
*t1
, tree
*t2
, tsubst_flags_t complain
)
11442 /* [expr.const.cast]
11444 For multi-level pointer to members and multi-level mixed pointers
11445 and pointers to members (conv.qual), the "member" aspect of a
11446 pointer to member level is ignored when determining if a const
11447 cv-qualifier has been cast away. */
11448 /* [expr.const.cast]
11450 For two pointer types:
11452 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
11453 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
11456 casting from X1 to X2 casts away constness if, for a non-pointer
11457 type T there does not exist an implicit conversion (clause
11460 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
11464 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
11465 if ((!TYPE_PTR_P (*t1
) && !TYPE_PTRDATAMEM_P (*t1
))
11466 || (!TYPE_PTR_P (*t2
) && !TYPE_PTRDATAMEM_P (*t2
)))
11468 *t1
= cp_build_qualified_type (void_type_node
,
11469 cp_type_quals (*t1
));
11470 *t2
= cp_build_qualified_type (void_type_node
,
11471 cp_type_quals (*t2
));
11475 quals1
= cp_type_quals (*t1
);
11476 quals2
= cp_type_quals (*t2
);
11478 if (TYPE_PTRDATAMEM_P (*t1
))
11479 *t1
= TYPE_PTRMEM_POINTED_TO_TYPE (*t1
);
11481 *t1
= TREE_TYPE (*t1
);
11482 if (TYPE_PTRDATAMEM_P (*t2
))
11483 *t2
= TYPE_PTRMEM_POINTED_TO_TYPE (*t2
);
11485 *t2
= TREE_TYPE (*t2
);
11487 casts_away_constness_r (t1
, t2
, complain
);
11488 *t1
= build_pointer_type (*t1
);
11489 *t2
= build_pointer_type (*t2
);
11490 *t1
= cp_build_qualified_type (*t1
, quals1
);
11491 *t2
= cp_build_qualified_type (*t2
, quals2
);
11494 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
11497 ??? This function returns non-zero if casting away qualifiers not
11498 just const. We would like to return to the caller exactly which
11499 qualifiers are casted away to give more accurate diagnostics.
11503 casts_away_constness (tree t1
, tree t2
, tsubst_flags_t complain
)
11505 if (TYPE_REF_P (t2
))
11507 /* [expr.const.cast]
11509 Casting from an lvalue of type T1 to an lvalue of type T2
11510 using a reference cast casts away constness if a cast from an
11511 rvalue of type "pointer to T1" to the type "pointer to T2"
11512 casts away constness. */
11513 t1
= (TYPE_REF_P (t1
) ? TREE_TYPE (t1
) : t1
);
11514 return casts_away_constness (build_pointer_type (t1
),
11515 build_pointer_type (TREE_TYPE (t2
)),
11519 if (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
11520 /* [expr.const.cast]
11522 Casting from an rvalue of type "pointer to data member of X
11523 of type T1" to the type "pointer to data member of Y of type
11524 T2" casts away constness if a cast from an rvalue of type
11525 "pointer to T1" to the type "pointer to T2" casts away
11527 return casts_away_constness
11528 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1
)),
11529 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2
)),
11532 /* Casting away constness is only something that makes sense for
11533 pointer or reference types. */
11534 if (!TYPE_PTR_P (t1
) || !TYPE_PTR_P (t2
))
11537 /* Top-level qualifiers don't matter. */
11538 t1
= TYPE_MAIN_VARIANT (t1
);
11539 t2
= TYPE_MAIN_VARIANT (t2
);
11540 casts_away_constness_r (&t1
, &t2
, complain
);
11541 if (!can_convert (t2
, t1
, complain
))
11547 /* If T is a REFERENCE_TYPE return the type to which T refers.
11548 Otherwise, return T itself. */
11551 non_reference (tree t
)
11553 if (t
&& TYPE_REF_P (t
))
11559 /* Return nonzero if REF is an lvalue valid for this language;
11560 otherwise, print an error message and return zero. USE says
11561 how the lvalue is being used and so selects the error message. */
11564 lvalue_or_else (tree ref
, enum lvalue_use use
, tsubst_flags_t complain
)
11566 cp_lvalue_kind kind
= lvalue_kind (ref
);
11568 if (kind
== clk_none
)
11570 if (complain
& tf_error
)
11571 lvalue_error (cp_expr_loc_or_input_loc (ref
), use
);
11574 else if (kind
& (clk_rvalueref
|clk_class
))
11576 if (!(complain
& tf_error
))
11578 /* Make this a permerror because we used to accept it. */
11579 permerror (cp_expr_loc_or_input_loc (ref
),
11580 "using rvalue as lvalue");
11585 /* Return true if a user-defined literal operator is a raw operator. */
11588 check_raw_literal_operator (const_tree decl
)
11590 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
11593 bool maybe_raw_p
= false;
11595 /* Count the number and type of arguments and check for ellipsis. */
11596 for (argtype
= argtypes
, arity
= 0;
11597 argtype
&& argtype
!= void_list_node
;
11598 ++arity
, argtype
= TREE_CHAIN (argtype
))
11600 tree t
= TREE_VALUE (argtype
);
11602 if (same_type_p (t
, const_string_type_node
))
11603 maybe_raw_p
= true;
11606 return false; /* Found ellipsis. */
11608 if (!maybe_raw_p
|| arity
!= 1)
11615 /* Return true if a user-defined literal operator has one of the allowed
11619 check_literal_operator_args (const_tree decl
,
11620 bool *long_long_unsigned_p
, bool *long_double_p
)
11622 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
11624 *long_long_unsigned_p
= false;
11625 *long_double_p
= false;
11626 if (processing_template_decl
|| processing_specialization
)
11627 return argtypes
== void_list_node
;
11634 /* Count the number and type of arguments and check for ellipsis. */
11635 for (argtype
= argtypes
, arity
= 0;
11636 argtype
&& argtype
!= void_list_node
;
11637 argtype
= TREE_CHAIN (argtype
))
11639 tree t
= TREE_VALUE (argtype
);
11642 if (TYPE_PTR_P (t
))
11644 bool maybe_raw_p
= false;
11646 if (cp_type_quals (t
) != TYPE_QUAL_CONST
)
11648 t
= TYPE_MAIN_VARIANT (t
);
11649 if ((maybe_raw_p
= same_type_p (t
, char_type_node
))
11650 || same_type_p (t
, wchar_type_node
)
11651 || same_type_p (t
, char8_type_node
)
11652 || same_type_p (t
, char16_type_node
)
11653 || same_type_p (t
, char32_type_node
))
11655 argtype
= TREE_CHAIN (argtype
);
11658 t
= TREE_VALUE (argtype
);
11659 if (maybe_raw_p
&& argtype
== void_list_node
)
11661 else if (same_type_p (t
, size_type_node
))
11670 else if (same_type_p (t
, long_long_unsigned_type_node
))
11673 *long_long_unsigned_p
= true;
11675 else if (same_type_p (t
, long_double_type_node
))
11678 *long_double_p
= true;
11680 else if (same_type_p (t
, char_type_node
))
11682 else if (same_type_p (t
, wchar_type_node
))
11684 else if (same_type_p (t
, char8_type_node
))
11686 else if (same_type_p (t
, char16_type_node
))
11688 else if (same_type_p (t
, char32_type_node
))
11694 return false; /* Found ellipsis. */
11696 if (arity
!= max_arity
)
11703 /* Always returns false since unlike C90, C++ has no concept of implicit
11704 function declarations. */
11707 c_decl_implicit (const_tree
)