1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2019 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization. */
29 #include "coretypes.h"
32 #include "stor-layout.h"
36 #include "c-family/c-objc.h"
37 #include "c-family/c-ubsan.h"
39 #include "gcc-rich-location.h"
40 #include "stringpool.h"
45 static tree
cp_build_addr_expr_strict (tree
, tsubst_flags_t
);
46 static tree
cp_build_function_call (tree
, tree
, tsubst_flags_t
);
47 static tree
pfn_from_ptrmemfunc (tree
);
48 static tree
delta_from_ptrmemfunc (tree
);
49 static tree
convert_for_assignment (tree
, tree
, impl_conv_rhs
, tree
, int,
51 static tree
cp_pointer_int_sum (location_t
, enum tree_code
, tree
, tree
,
53 static tree
rationalize_conditional_expr (enum tree_code
, tree
,
55 static int comp_ptr_ttypes_real (tree
, tree
, int);
56 static bool comp_except_types (tree
, tree
, bool);
57 static bool comp_array_types (const_tree
, const_tree
, bool);
58 static tree
pointer_diff (location_t
, tree
, tree
, tree
, tsubst_flags_t
, tree
*);
59 static tree
get_delta_difference (tree
, tree
, bool, bool, tsubst_flags_t
);
60 static void casts_away_constness_r (tree
*, tree
*, tsubst_flags_t
);
61 static bool casts_away_constness (tree
, tree
, tsubst_flags_t
);
62 static bool maybe_warn_about_returning_address_of_local (tree
);
63 static tree
lookup_destructor (tree
, tree
, tree
, tsubst_flags_t
);
64 static void error_args_num (location_t
, tree
, bool);
65 static int convert_arguments (tree
, vec
<tree
, va_gc
> **, tree
, int,
67 static bool is_std_move_p (tree
);
68 static bool is_std_forward_p (tree
);
70 /* Do `exp = require_complete_type (exp);' to make sure exp
71 does not have an incomplete type. (That includes void types.)
72 Returns error_mark_node if the VALUE does not have
73 complete type when this function returns. */
76 require_complete_type_sfinae (tree value
, tsubst_flags_t complain
)
80 if (processing_template_decl
|| value
== error_mark_node
)
83 if (TREE_CODE (value
) == OVERLOAD
)
84 type
= unknown_type_node
;
86 type
= TREE_TYPE (value
);
88 if (type
== error_mark_node
)
89 return error_mark_node
;
91 /* First, detect a valid value with a complete type. */
92 if (COMPLETE_TYPE_P (type
))
95 if (complete_type_or_maybe_complain (type
, value
, complain
))
98 return error_mark_node
;
102 require_complete_type (tree value
)
104 return require_complete_type_sfinae (value
, tf_warning_or_error
);
107 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
108 a template instantiation, do the instantiation. Returns TYPE,
109 whether or not it could be completed, unless something goes
110 horribly wrong, in which case the error_mark_node is returned. */
113 complete_type (tree type
)
115 if (type
== NULL_TREE
)
116 /* Rather than crash, we return something sure to cause an error
118 return error_mark_node
;
120 if (type
== error_mark_node
|| COMPLETE_TYPE_P (type
))
122 else if (TREE_CODE (type
) == ARRAY_TYPE
)
124 tree t
= complete_type (TREE_TYPE (type
));
125 unsigned int needs_constructing
, has_nontrivial_dtor
;
126 if (COMPLETE_TYPE_P (t
) && !dependent_type_p (type
))
129 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
131 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
132 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
134 TYPE_NEEDS_CONSTRUCTING (t
) = needs_constructing
;
135 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = has_nontrivial_dtor
;
138 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
139 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
144 /* Like complete_type, but issue an error if the TYPE cannot be completed.
145 VALUE is used for informative diagnostics.
146 Returns NULL_TREE if the type cannot be made complete. */
149 complete_type_or_maybe_complain (tree type
, tree value
, tsubst_flags_t complain
)
151 type
= complete_type (type
);
152 if (type
== error_mark_node
)
153 /* We already issued an error. */
155 else if (!COMPLETE_TYPE_P (type
))
157 if (complain
& tf_error
)
158 cxx_incomplete_type_diagnostic (value
, type
, DK_ERROR
);
166 complete_type_or_else (tree type
, tree value
)
168 return complete_type_or_maybe_complain (type
, value
, tf_warning_or_error
);
172 /* Return the common type of two parameter lists.
173 We assume that comptypes has already been done and returned 1;
174 if that isn't so, this may crash.
176 As an optimization, free the space we allocate if the parameter
177 lists are already common. */
180 commonparms (tree p1
, tree p2
)
182 tree oldargs
= p1
, newargs
, n
;
186 len
= list_length (p1
);
187 newargs
= tree_last (p1
);
189 if (newargs
== void_list_node
)
198 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
203 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
205 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
207 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
210 else if (! TREE_PURPOSE (p1
))
212 if (TREE_PURPOSE (p2
))
214 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
220 if (simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)) != 1)
222 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
224 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
227 TREE_VALUE (n
) = merge_types (TREE_VALUE (p1
), TREE_VALUE (p2
));
230 TREE_VALUE (n
) = TREE_VALUE (p1
);
238 /* Given a type, perhaps copied for a typedef,
239 find the "original" version of it. */
241 original_type (tree t
)
243 int quals
= cp_type_quals (t
);
244 while (t
!= error_mark_node
245 && TYPE_NAME (t
) != NULL_TREE
)
247 tree x
= TYPE_NAME (t
);
248 if (TREE_CODE (x
) != TYPE_DECL
)
250 x
= DECL_ORIGINAL_TYPE (x
);
255 return cp_build_qualified_type (t
, quals
);
258 /* Return the common type for two arithmetic types T1 and T2 under the
259 usual arithmetic conversions. The default conversions have already
260 been applied, and enumerated types converted to their compatible
264 cp_common_type (tree t1
, tree t2
)
266 enum tree_code code1
= TREE_CODE (t1
);
267 enum tree_code code2
= TREE_CODE (t2
);
272 /* In what follows, we slightly generalize the rules given in [expr] so
273 as to deal with `long long' and `complex'. First, merge the
275 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
277 if (SCOPED_ENUM_P (t1
) || SCOPED_ENUM_P (t2
))
279 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
280 return build_type_attribute_variant (t1
, attributes
);
285 /* FIXME: Attributes. */
286 gcc_assert (ARITHMETIC_TYPE_P (t1
)
287 || VECTOR_TYPE_P (t1
)
288 || UNSCOPED_ENUM_P (t1
));
289 gcc_assert (ARITHMETIC_TYPE_P (t2
)
290 || VECTOR_TYPE_P (t2
)
291 || UNSCOPED_ENUM_P (t2
));
293 /* If one type is complex, form the common type of the non-complex
294 components, then make that complex. Use T1 or T2 if it is the
296 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
298 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
299 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
301 = type_after_usual_arithmetic_conversions (subtype1
, subtype2
);
303 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
304 return build_type_attribute_variant (t1
, attributes
);
305 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
306 return build_type_attribute_variant (t2
, attributes
);
308 return build_type_attribute_variant (build_complex_type (subtype
),
312 if (code1
== VECTOR_TYPE
)
314 /* When we get here we should have two vectors of the same size.
315 Just prefer the unsigned one if present. */
316 if (TYPE_UNSIGNED (t1
))
317 return build_type_attribute_variant (t1
, attributes
);
319 return build_type_attribute_variant (t2
, attributes
);
322 /* If only one is real, use it as the result. */
323 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
324 return build_type_attribute_variant (t1
, attributes
);
325 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
326 return build_type_attribute_variant (t2
, attributes
);
328 /* Both real or both integers; use the one with greater precision. */
329 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
330 return build_type_attribute_variant (t1
, attributes
);
331 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
332 return build_type_attribute_variant (t2
, attributes
);
334 /* The types are the same; no need to do anything fancy. */
335 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
336 return build_type_attribute_variant (t1
, attributes
);
338 if (code1
!= REAL_TYPE
)
340 /* If one is unsigned long long, then convert the other to unsigned
342 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_unsigned_type_node
)
343 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_unsigned_type_node
))
344 return build_type_attribute_variant (long_long_unsigned_type_node
,
346 /* If one is a long long, and the other is an unsigned long, and
347 long long can represent all the values of an unsigned long, then
348 convert to a long long. Otherwise, convert to an unsigned long
349 long. Otherwise, if either operand is long long, convert the
352 Since we're here, we know the TYPE_PRECISION is the same;
353 therefore converting to long long cannot represent all the values
354 of an unsigned long, so we choose unsigned long long in that
356 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_integer_type_node
)
357 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_integer_type_node
))
359 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
360 ? long_long_unsigned_type_node
361 : long_long_integer_type_node
);
362 return build_type_attribute_variant (t
, attributes
);
365 /* Go through the same procedure, but for longs. */
366 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_unsigned_type_node
)
367 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_unsigned_type_node
))
368 return build_type_attribute_variant (long_unsigned_type_node
,
370 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_integer_type_node
)
371 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_integer_type_node
))
373 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
374 ? long_unsigned_type_node
: long_integer_type_node
);
375 return build_type_attribute_variant (t
, attributes
);
378 /* For __intN types, either the type is __int128 (and is lower
379 priority than the types checked above, but higher than other
380 128-bit types) or it's known to not be the same size as other
381 types (enforced in toplev.c). Prefer the unsigned type. */
382 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
384 if (int_n_enabled_p
[i
]
385 && (same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].signed_type
)
386 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].signed_type
)
387 || same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].unsigned_type
)
388 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].unsigned_type
)))
390 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
391 ? int_n_trees
[i
].unsigned_type
392 : int_n_trees
[i
].signed_type
);
393 return build_type_attribute_variant (t
, attributes
);
397 /* Otherwise prefer the unsigned one. */
398 if (TYPE_UNSIGNED (t1
))
399 return build_type_attribute_variant (t1
, attributes
);
401 return build_type_attribute_variant (t2
, attributes
);
405 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_double_type_node
)
406 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_double_type_node
))
407 return build_type_attribute_variant (long_double_type_node
,
409 if (same_type_p (TYPE_MAIN_VARIANT (t1
), double_type_node
)
410 || same_type_p (TYPE_MAIN_VARIANT (t2
), double_type_node
))
411 return build_type_attribute_variant (double_type_node
,
413 if (same_type_p (TYPE_MAIN_VARIANT (t1
), float_type_node
)
414 || same_type_p (TYPE_MAIN_VARIANT (t2
), float_type_node
))
415 return build_type_attribute_variant (float_type_node
,
418 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
419 the standard C++ floating-point types. Logic earlier in this
420 function has already eliminated the possibility that
421 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
422 compelling reason to choose one or the other. */
423 return build_type_attribute_variant (t1
, attributes
);
427 /* T1 and T2 are arithmetic or enumeration types. Return the type
428 that will result from the "usual arithmetic conversions" on T1 and
429 T2 as described in [expr]. */
432 type_after_usual_arithmetic_conversions (tree t1
, tree t2
)
434 gcc_assert (ARITHMETIC_TYPE_P (t1
)
435 || VECTOR_TYPE_P (t1
)
436 || UNSCOPED_ENUM_P (t1
));
437 gcc_assert (ARITHMETIC_TYPE_P (t2
)
438 || VECTOR_TYPE_P (t2
)
439 || UNSCOPED_ENUM_P (t2
));
441 /* Perform the integral promotions. We do not promote real types here. */
442 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1
)
443 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2
))
445 t1
= type_promotes_to (t1
);
446 t2
= type_promotes_to (t2
);
449 return cp_common_type (t1
, t2
);
453 composite_pointer_error (diagnostic_t kind
, tree t1
, tree t2
,
454 composite_pointer_operation operation
)
459 emit_diagnostic (kind
, input_location
, 0,
460 "comparison between "
461 "distinct pointer types %qT and %qT lacks a cast",
465 emit_diagnostic (kind
, input_location
, 0,
466 "conversion between "
467 "distinct pointer types %qT and %qT lacks a cast",
470 case CPO_CONDITIONAL_EXPR
:
471 emit_diagnostic (kind
, input_location
, 0,
472 "conditional expression between "
473 "distinct pointer types %qT and %qT lacks a cast",
481 /* Subroutine of composite_pointer_type to implement the recursive
482 case. See that function for documentation of the parameters. */
485 composite_pointer_type_r (tree t1
, tree t2
,
486 composite_pointer_operation operation
,
487 tsubst_flags_t complain
)
494 /* Determine the types pointed to by T1 and T2. */
497 pointee1
= TREE_TYPE (t1
);
498 pointee2
= TREE_TYPE (t2
);
502 pointee1
= TYPE_PTRMEM_POINTED_TO_TYPE (t1
);
503 pointee2
= TYPE_PTRMEM_POINTED_TO_TYPE (t2
);
508 Otherwise, the composite pointer type is a pointer type
509 similar (_conv.qual_) to the type of one of the operands,
510 with a cv-qualification signature (_conv.qual_) that is the
511 union of the cv-qualification signatures of the operand
513 if (same_type_ignoring_top_level_qualifiers_p (pointee1
, pointee2
))
514 result_type
= pointee1
;
515 else if ((TYPE_PTR_P (pointee1
) && TYPE_PTR_P (pointee2
))
516 || (TYPE_PTRMEM_P (pointee1
) && TYPE_PTRMEM_P (pointee2
)))
518 result_type
= composite_pointer_type_r (pointee1
, pointee2
, operation
,
520 if (result_type
== error_mark_node
)
521 return error_mark_node
;
525 if (complain
& tf_error
)
526 composite_pointer_error (DK_PERMERROR
, t1
, t2
, operation
);
528 return error_mark_node
;
529 result_type
= void_type_node
;
531 result_type
= cp_build_qualified_type (result_type
,
532 (cp_type_quals (pointee1
)
533 | cp_type_quals (pointee2
)));
534 /* If the original types were pointers to members, so is the
536 if (TYPE_PTRMEM_P (t1
))
538 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
539 TYPE_PTRMEM_CLASS_TYPE (t2
)))
541 if (complain
& tf_error
)
542 composite_pointer_error (DK_PERMERROR
, t1
, t2
, operation
);
544 return error_mark_node
;
546 result_type
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
550 result_type
= build_pointer_type (result_type
);
552 /* Merge the attributes. */
553 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
554 return build_type_attribute_variant (result_type
, attributes
);
557 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
558 ARG1 and ARG2 are the values with those types. The OPERATION is to
559 describe the operation between the pointer types,
560 in case an error occurs.
562 This routine also implements the computation of a common type for
563 pointers-to-members as per [expr.eq]. */
566 composite_pointer_type (tree t1
, tree t2
, tree arg1
, tree arg2
,
567 composite_pointer_operation operation
,
568 tsubst_flags_t complain
)
575 If one operand is a null pointer constant, the composite pointer
576 type is the type of the other operand. */
577 if (null_ptr_cst_p (arg1
))
579 if (null_ptr_cst_p (arg2
))
586 If one of the operands has type "pointer to cv1 void*", then
587 the other has type "pointer to cv2T", and the composite pointer
588 type is "pointer to cv12 void", where cv12 is the union of cv1
591 If either type is a pointer to void, make sure it is T1. */
592 if (TYPE_PTR_P (t2
) && VOID_TYPE_P (TREE_TYPE (t2
)))
595 /* Now, if T1 is a pointer to void, merge the qualifiers. */
596 if (TYPE_PTR_P (t1
) && VOID_TYPE_P (TREE_TYPE (t1
)))
601 if (TYPE_PTRFN_P (t2
))
603 if (complain
& tf_error
)
608 pedwarn (input_location
, OPT_Wpedantic
,
609 "ISO C++ forbids comparison between pointer "
610 "of type %<void *%> and pointer-to-function");
613 pedwarn (input_location
, OPT_Wpedantic
,
614 "ISO C++ forbids conversion between pointer "
615 "of type %<void *%> and pointer-to-function");
617 case CPO_CONDITIONAL_EXPR
:
618 pedwarn (input_location
, OPT_Wpedantic
,
619 "ISO C++ forbids conditional expression between "
620 "pointer of type %<void *%> and "
621 "pointer-to-function");
628 return error_mark_node
;
631 = cp_build_qualified_type (void_type_node
,
632 (cp_type_quals (TREE_TYPE (t1
))
633 | cp_type_quals (TREE_TYPE (t2
))));
634 result_type
= build_pointer_type (result_type
);
635 /* Merge the attributes. */
636 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
637 return build_type_attribute_variant (result_type
, attributes
);
640 if (c_dialect_objc () && TYPE_PTR_P (t1
)
643 if (objc_have_common_type (t1
, t2
, -3, NULL_TREE
))
644 return objc_common_type (t1
, t2
);
647 /* if T1 or T2 is "pointer to noexcept function" and the other type is
648 "pointer to function", where the function types are otherwise the same,
649 "pointer to function" */
650 if (fnptr_conv_p (t1
, t2
))
652 if (fnptr_conv_p (t2
, t1
))
655 /* [expr.eq] permits the application of a pointer conversion to
656 bring the pointers to a common type. */
657 if (TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
)
658 && CLASS_TYPE_P (TREE_TYPE (t1
))
659 && CLASS_TYPE_P (TREE_TYPE (t2
))
660 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1
),
663 class1
= TREE_TYPE (t1
);
664 class2
= TREE_TYPE (t2
);
666 if (DERIVED_FROM_P (class1
, class2
))
667 t2
= (build_pointer_type
668 (cp_build_qualified_type (class1
, cp_type_quals (class2
))));
669 else if (DERIVED_FROM_P (class2
, class1
))
670 t1
= (build_pointer_type
671 (cp_build_qualified_type (class2
, cp_type_quals (class1
))));
674 if (complain
& tf_error
)
675 composite_pointer_error (DK_ERROR
, t1
, t2
, operation
);
676 return error_mark_node
;
679 /* [expr.eq] permits the application of a pointer-to-member
680 conversion to change the class type of one of the types. */
681 else if (TYPE_PTRMEM_P (t1
)
682 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
683 TYPE_PTRMEM_CLASS_TYPE (t2
)))
685 class1
= TYPE_PTRMEM_CLASS_TYPE (t1
);
686 class2
= TYPE_PTRMEM_CLASS_TYPE (t2
);
688 if (DERIVED_FROM_P (class1
, class2
))
689 t1
= build_ptrmem_type (class2
, TYPE_PTRMEM_POINTED_TO_TYPE (t1
));
690 else if (DERIVED_FROM_P (class2
, class1
))
691 t2
= build_ptrmem_type (class1
, TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
694 if (complain
& tf_error
)
698 error ("comparison between distinct "
699 "pointer-to-member types %qT and %qT lacks a cast",
703 error ("conversion between distinct "
704 "pointer-to-member types %qT and %qT lacks a cast",
707 case CPO_CONDITIONAL_EXPR
:
708 error ("conditional expression between distinct "
709 "pointer-to-member types %qT and %qT lacks a cast",
715 return error_mark_node
;
719 return composite_pointer_type_r (t1
, t2
, operation
, complain
);
722 /* Return the merged type of two types.
723 We assume that comptypes has already been done and returned 1;
724 if that isn't so, this may crash.
726 This just combines attributes and default arguments; any other
727 differences would cause the two types to compare unalike. */
730 merge_types (tree t1
, tree t2
)
732 enum tree_code code1
;
733 enum tree_code code2
;
736 /* Save time if the two types are the same. */
739 if (original_type (t1
) == original_type (t2
))
742 /* If one type is nonsense, use the other. */
743 if (t1
== error_mark_node
)
745 if (t2
== error_mark_node
)
748 /* Handle merging an auto redeclaration with a previous deduced
753 /* Merge the attributes. */
754 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
756 if (TYPE_PTRMEMFUNC_P (t1
))
757 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
758 if (TYPE_PTRMEMFUNC_P (t2
))
759 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
761 code1
= TREE_CODE (t1
);
762 code2
= TREE_CODE (t2
);
765 gcc_assert (code1
== TYPENAME_TYPE
|| code2
== TYPENAME_TYPE
);
766 if (code1
== TYPENAME_TYPE
)
768 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
769 code1
= TREE_CODE (t1
);
773 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
774 code2
= TREE_CODE (t2
);
782 /* For two pointers, do this recursively on the target type. */
784 tree target
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
785 int quals
= cp_type_quals (t1
);
787 if (code1
== POINTER_TYPE
)
789 t1
= build_pointer_type (target
);
790 if (TREE_CODE (target
) == METHOD_TYPE
)
791 t1
= build_ptrmemfunc_type (t1
);
794 t1
= cp_build_reference_type (target
, TYPE_REF_IS_RVALUE (t1
));
795 t1
= build_type_attribute_variant (t1
, attributes
);
796 t1
= cp_build_qualified_type (t1
, quals
);
805 quals
= cp_type_quals (t1
);
806 pointee
= merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1
),
807 TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
808 t1
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
810 t1
= cp_build_qualified_type (t1
, quals
);
816 tree elt
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
817 /* Save space: see if the result is identical to one of the args. */
818 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
819 return build_type_attribute_variant (t1
, attributes
);
820 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
821 return build_type_attribute_variant (t2
, attributes
);
822 /* Merge the element types, and have a size if either arg has one. */
823 t1
= build_cplus_array_type
824 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
829 /* Function types: prefer the one that specified arg types.
830 If both do, merge the arg types. Also merge the return types. */
832 tree valtype
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
833 tree p1
= TYPE_ARG_TYPES (t1
);
834 tree p2
= TYPE_ARG_TYPES (t2
);
837 /* Save space: see if the result is identical to one of the args. */
838 if (valtype
== TREE_TYPE (t1
) && ! p2
)
839 return cp_build_type_attribute_variant (t1
, attributes
);
840 if (valtype
== TREE_TYPE (t2
) && ! p1
)
841 return cp_build_type_attribute_variant (t2
, attributes
);
843 /* Simple way if one arg fails to specify argument types. */
844 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
846 else if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
849 parms
= commonparms (p1
, p2
);
851 cp_cv_quals quals
= type_memfn_quals (t1
);
852 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
853 gcc_assert (quals
== type_memfn_quals (t2
));
854 gcc_assert (rqual
== type_memfn_rqual (t2
));
856 tree rval
= build_function_type (valtype
, parms
);
857 rval
= apply_memfn_quals (rval
, quals
);
858 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
859 TYPE_RAISES_EXCEPTIONS (t2
));
860 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
861 t1
= build_cp_fntype_variant (rval
, rqual
, raises
, late_return_type_p
);
867 /* Get this value the long way, since TYPE_METHOD_BASETYPE
868 is just the main variant of this. */
869 tree basetype
= class_of_this_parm (t2
);
870 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
871 TYPE_RAISES_EXCEPTIONS (t2
));
872 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
874 bool late_return_type_1_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
876 /* If this was a member function type, get back to the
877 original type of type member function (i.e., without
878 the class instance variable up front. */
879 t1
= build_function_type (TREE_TYPE (t1
),
880 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
881 t2
= build_function_type (TREE_TYPE (t2
),
882 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
883 t3
= merge_types (t1
, t2
);
884 t3
= build_method_type_directly (basetype
, TREE_TYPE (t3
),
885 TYPE_ARG_TYPES (t3
));
886 t1
= build_cp_fntype_variant (t3
, rqual
, raises
, late_return_type_1_p
);
891 /* There is no need to merge attributes into a TYPENAME_TYPE.
892 When the type is instantiated it will have whatever
893 attributes result from the instantiation. */
897 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
899 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
904 return cp_build_type_attribute_variant (t1
, attributes
);
907 /* Return the ARRAY_TYPE type without its domain. */
910 strip_array_domain (tree type
)
913 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
914 if (TYPE_DOMAIN (type
) == NULL_TREE
)
916 t2
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
917 return cp_build_type_attribute_variant (t2
, TYPE_ATTRIBUTES (type
));
920 /* Wrapper around cp_common_type that is used by c-common.c and other
921 front end optimizations that remove promotions.
923 Return the common type for two arithmetic types T1 and T2 under the
924 usual arithmetic conversions. The default conversions have already
925 been applied, and enumerated types converted to their compatible
929 common_type (tree t1
, tree t2
)
931 /* If one type is nonsense, use the other */
932 if (t1
== error_mark_node
)
934 if (t2
== error_mark_node
)
937 return cp_common_type (t1
, t2
);
940 /* Return the common type of two pointer types T1 and T2. This is the
941 type for the result of most arithmetic operations if the operands
942 have the given two types.
944 We assume that comp_target_types has already been done and returned
945 nonzero; if that isn't so, this may crash. */
948 common_pointer_type (tree t1
, tree t2
)
950 gcc_assert ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
951 || (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
952 || (TYPE_PTRMEMFUNC_P (t1
) && TYPE_PTRMEMFUNC_P (t2
)));
954 return composite_pointer_type (t1
, t2
, error_mark_node
, error_mark_node
,
955 CPO_CONVERSION
, tf_warning_or_error
);
958 /* Compare two exception specifier types for exactness or subsetness, if
959 allowed. Returns false for mismatch, true for match (same, or
962 [except.spec] "If a class X ... objects of class X or any class publicly
963 and unambiguously derived from X. Similarly, if a pointer type Y * ...
964 exceptions of type Y * or that are pointers to any type publicly and
965 unambiguously derived from Y. Otherwise a function only allows exceptions
966 that have the same type ..."
967 This does not mention cv qualifiers and is different to what throw
968 [except.throw] and catch [except.catch] will do. They will ignore the
969 top level cv qualifiers, and allow qualifiers in the pointer to class
972 We implement the letter of the standard. */
975 comp_except_types (tree a
, tree b
, bool exact
)
977 if (same_type_p (a
, b
))
981 if (cp_type_quals (a
) || cp_type_quals (b
))
984 if (TYPE_PTR_P (a
) && TYPE_PTR_P (b
))
988 if (cp_type_quals (a
) || cp_type_quals (b
))
992 if (TREE_CODE (a
) != RECORD_TYPE
993 || TREE_CODE (b
) != RECORD_TYPE
)
996 if (publicly_uniquely_derived_p (a
, b
))
1002 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1003 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1004 If EXACT is ce_type, the C++17 type compatibility rules apply.
1005 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1006 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1007 are unordered, but we've already filtered out duplicates. Most lists will
1008 be in order, we should try to make use of that. */
1011 comp_except_specs (const_tree t1
, const_tree t2
, int exact
)
1020 /* First handle noexcept. */
1021 if (exact
< ce_exact
)
1023 if (exact
== ce_type
1024 && (canonical_eh_spec (CONST_CAST_TREE (t1
))
1025 == canonical_eh_spec (CONST_CAST_TREE (t2
))))
1028 /* noexcept(false) is compatible with no exception-specification,
1029 and less strict than any spec. */
1030 if (t1
== noexcept_false_spec
)
1031 return t2
== NULL_TREE
|| exact
== ce_derived
;
1032 /* Even a derived noexcept(false) is compatible with no
1033 exception-specification. */
1034 if (t2
== noexcept_false_spec
)
1035 return t1
== NULL_TREE
;
1037 /* Otherwise, if we aren't looking for an exact match, noexcept is
1038 equivalent to throw(). */
1039 if (t1
== noexcept_true_spec
)
1040 t1
= empty_except_spec
;
1041 if (t2
== noexcept_true_spec
)
1042 t2
= empty_except_spec
;
1045 /* If any noexcept is left, it is only comparable to itself;
1046 either we're looking for an exact match or we're redeclaring a
1047 template with dependent noexcept. */
1048 if ((t1
&& TREE_PURPOSE (t1
))
1049 || (t2
&& TREE_PURPOSE (t2
)))
1051 && cp_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)));
1053 if (t1
== NULL_TREE
) /* T1 is ... */
1054 return t2
== NULL_TREE
|| exact
== ce_derived
;
1055 if (!TREE_VALUE (t1
)) /* t1 is EMPTY */
1056 return t2
!= NULL_TREE
&& !TREE_VALUE (t2
);
1057 if (t2
== NULL_TREE
) /* T2 is ... */
1059 if (TREE_VALUE (t1
) && !TREE_VALUE (t2
)) /* T2 is EMPTY, T1 is not */
1060 return exact
== ce_derived
;
1062 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1063 Count how many we find, to determine exactness. For exact matching and
1064 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1066 for (base
= t1
; t2
!= NULL_TREE
; t2
= TREE_CHAIN (t2
))
1068 for (probe
= base
; probe
!= NULL_TREE
; probe
= TREE_CHAIN (probe
))
1070 tree a
= TREE_VALUE (probe
);
1071 tree b
= TREE_VALUE (t2
);
1073 if (comp_except_types (a
, b
, exact
))
1075 if (probe
== base
&& exact
> ce_derived
)
1076 base
= TREE_CHAIN (probe
);
1081 if (probe
== NULL_TREE
)
1084 return exact
== ce_derived
|| base
== NULL_TREE
|| length
== list_length (t1
);
1087 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1088 [] can match [size]. */
1091 comp_array_types (const_tree t1
, const_tree t2
, bool allow_redeclaration
)
1100 /* The type of the array elements must be the same. */
1101 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1104 d1
= TYPE_DOMAIN (t1
);
1105 d2
= TYPE_DOMAIN (t2
);
1110 /* If one of the arrays is dimensionless, and the other has a
1111 dimension, they are of different types. However, it is valid to
1119 declarations for an array object can specify
1120 array types that differ by the presence or absence of a major
1121 array bound (_dcl.array_). */
1123 return allow_redeclaration
;
1125 /* Check that the dimensions are the same. */
1127 if (!cp_tree_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
)))
1129 max1
= TYPE_MAX_VALUE (d1
);
1130 max2
= TYPE_MAX_VALUE (d2
);
1132 if (!cp_tree_equal (max1
, max2
))
1138 /* Compare the relative position of T1 and T2 into their respective
1139 template parameter list.
1140 T1 and T2 must be template parameter types.
1141 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1144 comp_template_parms_position (tree t1
, tree t2
)
1146 tree index1
, index2
;
1147 gcc_assert (t1
&& t2
1148 && TREE_CODE (t1
) == TREE_CODE (t2
)
1149 && (TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
1150 || TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
1151 || TREE_CODE (t1
) == TEMPLATE_TYPE_PARM
));
1153 index1
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1
));
1154 index2
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2
));
1156 /* Then compare their relative position. */
1157 if (TEMPLATE_PARM_IDX (index1
) != TEMPLATE_PARM_IDX (index2
)
1158 || TEMPLATE_PARM_LEVEL (index1
) != TEMPLATE_PARM_LEVEL (index2
)
1159 || (TEMPLATE_PARM_PARAMETER_PACK (index1
)
1160 != TEMPLATE_PARM_PARAMETER_PACK (index2
)))
1163 /* In C++14 we can end up comparing 'auto' to a normal template
1164 parameter. Don't confuse them. */
1165 if (cxx_dialect
>= cxx14
&& (is_auto (t1
) || is_auto (t2
)))
1166 return TYPE_IDENTIFIER (t1
) == TYPE_IDENTIFIER (t2
);
1171 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1174 cxx_safe_arg_type_equiv_p (tree t1
, tree t2
)
1176 t1
= TYPE_MAIN_VARIANT (t1
);
1177 t2
= TYPE_MAIN_VARIANT (t2
);
1183 /* The signedness of the parameter matters only when an integral
1184 type smaller than int is promoted to int, otherwise only the
1185 precision of the parameter matters.
1186 This check should make sure that the callee does not see
1187 undefined values in argument registers. */
1188 if (INTEGRAL_TYPE_P (t1
)
1189 && INTEGRAL_TYPE_P (t2
)
1190 && TYPE_PRECISION (t1
) == TYPE_PRECISION (t2
)
1191 && (TYPE_UNSIGNED (t1
) == TYPE_UNSIGNED (t2
)
1192 || !targetm
.calls
.promote_prototypes (NULL_TREE
)
1193 || TYPE_PRECISION (t1
) >= TYPE_PRECISION (integer_type_node
)))
1196 return same_type_p (t1
, t2
);
1199 /* Check if a type cast between two function types can be considered safe. */
1202 cxx_safe_function_type_cast_p (tree t1
, tree t2
)
1204 if (TREE_TYPE (t1
) == void_type_node
&&
1205 TYPE_ARG_TYPES (t1
) == void_list_node
)
1208 if (TREE_TYPE (t2
) == void_type_node
&&
1209 TYPE_ARG_TYPES (t2
) == void_list_node
)
1212 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1215 for (t1
= TYPE_ARG_TYPES (t1
), t2
= TYPE_ARG_TYPES (t2
);
1217 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1218 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1224 /* Subroutine in comptypes. */
1227 structural_comptypes (tree t1
, tree t2
, int strict
)
1232 /* Suppress errors caused by previously reported errors. */
1233 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1236 gcc_assert (TYPE_P (t1
) && TYPE_P (t2
));
1238 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1239 current instantiation. */
1240 if (TREE_CODE (t1
) == TYPENAME_TYPE
)
1241 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
1243 if (TREE_CODE (t2
) == TYPENAME_TYPE
)
1244 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
1246 if (TYPE_PTRMEMFUNC_P (t1
))
1247 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
1248 if (TYPE_PTRMEMFUNC_P (t2
))
1249 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
1251 /* Different classes of types can't be compatible. */
1252 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1255 /* Qualifiers must match. For array types, we will check when we
1256 recur on the array element types. */
1257 if (TREE_CODE (t1
) != ARRAY_TYPE
1258 && cp_type_quals (t1
) != cp_type_quals (t2
))
1260 if (TREE_CODE (t1
) == FUNCTION_TYPE
1261 && type_memfn_quals (t1
) != type_memfn_quals (t2
))
1263 /* Need to check this before TYPE_MAIN_VARIANT.
1264 FIXME function qualifiers should really change the main variant. */
1265 if (TREE_CODE (t1
) == FUNCTION_TYPE
1266 || TREE_CODE (t1
) == METHOD_TYPE
)
1268 if (type_memfn_rqual (t1
) != type_memfn_rqual (t2
))
1270 if (flag_noexcept_type
1271 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1
),
1272 TYPE_RAISES_EXCEPTIONS (t2
),
1277 /* Allow for two different type nodes which have essentially the same
1278 definition. Note that we already checked for equality of the type
1279 qualifiers (just above). */
1281 if (TREE_CODE (t1
) != ARRAY_TYPE
1282 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1286 /* Compare the types. Break out if they could be the same. */
1287 switch (TREE_CODE (t1
))
1291 /* All void and bool types are the same. */
1295 case FIXED_POINT_TYPE
:
1297 /* With these nodes, we can't determine type equivalence by
1298 looking at what is stored in the nodes themselves, because
1299 two nodes might have different TYPE_MAIN_VARIANTs but still
1300 represent the same type. For example, wchar_t and int could
1301 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1302 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1303 and are distinct types. On the other hand, int and the
1306 typedef int INT __attribute((may_alias));
1308 have identical properties, different TYPE_MAIN_VARIANTs, but
1309 represent the same type. The canonical type system keeps
1310 track of equivalence in this case, so we fall back on it. */
1311 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1313 case TEMPLATE_TEMPLATE_PARM
:
1314 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1315 if (!comp_template_parms_position (t1
, t2
))
1317 if (!comp_template_parms
1318 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1
)),
1319 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2
))))
1321 if (TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
)
1323 /* Don't check inheritance. */
1324 strict
= COMPARE_STRICT
;
1329 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
1330 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
1331 || TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1332 && comp_template_args (TYPE_TI_ARGS (t1
), TYPE_TI_ARGS (t2
)))
1335 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
1337 else if ((strict
& COMPARE_DERIVED
) && DERIVED_FROM_P (t2
, t1
))
1343 if (!comptypes (TYPE_OFFSET_BASETYPE (t1
), TYPE_OFFSET_BASETYPE (t2
),
1344 strict
& ~COMPARE_REDECLARATION
))
1346 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1350 case REFERENCE_TYPE
:
1351 if (TYPE_REF_IS_RVALUE (t1
) != TYPE_REF_IS_RVALUE (t2
))
1353 /* fall through to checks for pointer types */
1357 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1358 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1364 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1366 if (!compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)))
1371 /* Target types must match incl. qualifiers. */
1372 if (!comp_array_types (t1
, t2
, !!(strict
& COMPARE_REDECLARATION
)))
1376 case TEMPLATE_TYPE_PARM
:
1377 /* If T1 and T2 don't have the same relative position in their
1378 template parameters set, they can't be equal. */
1379 if (!comp_template_parms_position (t1
, t2
))
1381 /* If T1 and T2 don't represent the same class template deduction,
1382 they aren't equal. */
1383 if (CLASS_PLACEHOLDER_TEMPLATE (t1
)
1384 != CLASS_PLACEHOLDER_TEMPLATE (t2
))
1386 /* Constrained 'auto's are distinct from parms that don't have the same
1388 if (!equivalent_placeholder_constraints (t1
, t2
))
1393 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1
),
1394 TYPENAME_TYPE_FULLNAME (t2
)))
1396 /* Qualifiers don't matter on scopes. */
1397 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1
),
1402 case UNBOUND_CLASS_TEMPLATE
:
1403 if (!cp_tree_equal (TYPE_IDENTIFIER (t1
), TYPE_IDENTIFIER (t2
)))
1405 if (!same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
)))
1410 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1415 if (maybe_ne (TYPE_VECTOR_SUBPARTS (t1
), TYPE_VECTOR_SUBPARTS (t2
))
1416 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1420 case TYPE_PACK_EXPANSION
:
1421 return (same_type_p (PACK_EXPANSION_PATTERN (t1
),
1422 PACK_EXPANSION_PATTERN (t2
))
1423 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1
),
1424 PACK_EXPANSION_EXTRA_ARGS (t2
)));
1427 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1
)
1428 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2
)
1429 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1
)
1430 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2
))
1431 || (DECLTYPE_FOR_LAMBDA_PROXY (t1
)
1432 != DECLTYPE_FOR_LAMBDA_PROXY (t2
))
1433 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1
),
1434 DECLTYPE_TYPE_EXPR (t2
)))
1438 case UNDERLYING_TYPE
:
1439 return same_type_p (UNDERLYING_TYPE_TYPE (t1
),
1440 UNDERLYING_TYPE_TYPE (t2
));
1446 /* Don't treat an alias template specialization with dependent
1447 arguments as equivalent to its underlying type when used as a
1448 template argument; we need them to be distinct so that we
1449 substitute into the specialization arguments at instantiation
1450 time. And aliases can't be equivalent without being ==, so
1451 we don't need to look any deeper. */
1452 if (comparing_specializations
1453 && (dependent_alias_template_spec_p (t1
)
1454 || dependent_alias_template_spec_p (t2
)))
1457 /* If we get here, we know that from a target independent POV the
1458 types are the same. Make sure the target attributes are also
1460 return comp_type_attributes (t1
, t2
);
1463 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1464 is a bitwise-or of the COMPARE_* flags. */
1467 comptypes (tree t1
, tree t2
, int strict
)
1469 if (strict
== COMPARE_STRICT
&& comparing_specializations
1470 && (t1
!= TYPE_CANONICAL (t1
) || t2
!= TYPE_CANONICAL (t2
)))
1471 /* If comparing_specializations, treat dependent aliases as distinct. */
1472 strict
= COMPARE_STRUCTURAL
;
1473 if (strict
== COMPARE_STRICT
)
1478 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1481 if (TYPE_STRUCTURAL_EQUALITY_P (t1
) || TYPE_STRUCTURAL_EQUALITY_P (t2
))
1482 /* At least one of the types requires structural equality, so
1483 perform a deep check. */
1484 return structural_comptypes (t1
, t2
, strict
);
1486 if (flag_checking
&& USE_CANONICAL_TYPES
)
1488 bool result
= structural_comptypes (t1
, t2
, strict
);
1490 if (result
&& TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1491 /* The two types are structurally equivalent, but their
1492 canonical types were different. This is a failure of the
1493 canonical type propagation code.*/
1495 ("canonical types differ for identical types %qT and %qT",
1497 else if (!result
&& TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
))
1498 /* Two types are structurally different, but the canonical
1499 types are the same. This means we were over-eager in
1500 assigning canonical types. */
1502 ("same canonical type node for different types %qT and %qT",
1507 if (!flag_checking
&& USE_CANONICAL_TYPES
)
1508 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1510 return structural_comptypes (t1
, t2
, strict
);
1512 else if (strict
== COMPARE_STRUCTURAL
)
1513 return structural_comptypes (t1
, t2
, COMPARE_STRICT
);
1515 return structural_comptypes (t1
, t2
, strict
);
1518 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1519 top-level qualifiers. */
1522 same_type_ignoring_top_level_qualifiers_p (tree type1
, tree type2
)
1524 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1529 type1
= cp_build_qualified_type (type1
, TYPE_UNQUALIFIED
);
1530 type2
= cp_build_qualified_type (type2
, TYPE_UNQUALIFIED
);
1531 return same_type_p (type1
, type2
);
1534 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1537 at_least_as_qualified_p (const_tree type1
, const_tree type2
)
1539 int q1
= cp_type_quals (type1
);
1540 int q2
= cp_type_quals (type2
);
1542 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1543 return (q1
& q2
) == q2
;
1546 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1547 more cv-qualified that TYPE1, and 0 otherwise. */
1550 comp_cv_qualification (int q1
, int q2
)
1555 if ((q1
& q2
) == q2
)
1557 else if ((q1
& q2
) == q1
)
1564 comp_cv_qualification (const_tree type1
, const_tree type2
)
1566 int q1
= cp_type_quals (type1
);
1567 int q2
= cp_type_quals (type2
);
1568 return comp_cv_qualification (q1
, q2
);
1571 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1572 subset of the cv-qualification signature of TYPE2, and the types
1573 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1576 comp_cv_qual_signature (tree type1
, tree type2
)
1578 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1580 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1586 /* Subroutines of `comptypes'. */
1588 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1589 equivalent in the sense that functions with those parameter types
1590 can have equivalent types. The two lists must be equivalent,
1591 element by element. */
1594 compparms (const_tree parms1
, const_tree parms2
)
1598 /* An unspecified parmlist matches any specified parmlist
1599 whose argument types don't need default promotions. */
1601 for (t1
= parms1
, t2
= parms2
;
1603 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1605 /* If one parmlist is shorter than the other,
1606 they fail to match. */
1609 if (!same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1616 /* Process a sizeof or alignof expression where the operand is a
1617 type. STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
1618 or GNU (preferred alignment) semantics; it is ignored if op is
1622 cxx_sizeof_or_alignof_type (tree type
, enum tree_code op
, bool std_alignof
,
1625 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
1626 if (type
== error_mark_node
)
1627 return error_mark_node
;
1629 type
= non_reference (type
);
1630 if (TREE_CODE (type
) == METHOD_TYPE
)
1634 pedwarn (input_location
, OPT_Wpointer_arith
,
1635 "invalid application of %qs to a member function",
1636 OVL_OP_INFO (false, op
)->name
);
1637 return size_one_node
;
1640 return error_mark_node
;
1643 bool dependent_p
= dependent_type_p (type
);
1645 complete_type (type
);
1647 /* VLA types will have a non-constant size. In the body of an
1648 uninstantiated template, we don't need to try to compute the
1649 value, because the sizeof expression is not an integral
1650 constant expression in that case. And, if we do try to
1651 compute the value, we'll likely end up with SAVE_EXPRs, which
1652 the template substitution machinery does not expect to see. */
1653 || (processing_template_decl
1654 && COMPLETE_TYPE_P (type
)
1655 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
))
1657 tree value
= build_min (op
, size_type_node
, type
);
1658 TREE_READONLY (value
) = 1;
1659 if (op
== ALIGNOF_EXPR
&& std_alignof
)
1660 ALIGNOF_EXPR_STD_P (value
) = true;
1664 return c_sizeof_or_alignof_type (input_location
, complete_type (type
),
1665 op
== SIZEOF_EXPR
, std_alignof
,
1669 /* Return the size of the type, without producing any warnings for
1670 types whose size cannot be taken. This routine should be used only
1671 in some other routine that has already produced a diagnostic about
1672 using the size of such a type. */
1674 cxx_sizeof_nowarn (tree type
)
1676 if (TREE_CODE (type
) == FUNCTION_TYPE
1677 || VOID_TYPE_P (type
)
1678 || TREE_CODE (type
) == ERROR_MARK
)
1679 return size_one_node
;
1680 else if (!COMPLETE_TYPE_P (type
))
1681 return size_zero_node
;
1683 return cxx_sizeof_or_alignof_type (type
, SIZEOF_EXPR
, false, false);
1686 /* Process a sizeof expression where the operand is an expression. */
1689 cxx_sizeof_expr (tree e
, tsubst_flags_t complain
)
1691 if (e
== error_mark_node
)
1692 return error_mark_node
;
1694 if (processing_template_decl
)
1696 e
= build_min (SIZEOF_EXPR
, size_type_node
, e
);
1697 TREE_SIDE_EFFECTS (e
) = 0;
1698 TREE_READONLY (e
) = 1;
1703 STRIP_ANY_LOCATION_WRAPPER (e
);
1705 /* To get the size of a static data member declared as an array of
1706 unknown bound, we need to instantiate it. */
1708 && VAR_HAD_UNKNOWN_BOUND (e
)
1709 && DECL_TEMPLATE_INSTANTIATION (e
))
1710 instantiate_decl (e
, /*defer_ok*/true, /*expl_inst_mem*/false);
1712 if (TREE_CODE (e
) == PARM_DECL
1713 && DECL_ARRAY_PARAMETER_P (e
)
1714 && (complain
& tf_warning
))
1716 auto_diagnostic_group d
;
1717 if (warning (OPT_Wsizeof_array_argument
, "%<sizeof%> on array function "
1718 "parameter %qE will return size of %qT", e
, TREE_TYPE (e
)))
1719 inform (DECL_SOURCE_LOCATION (e
), "declared here");
1722 e
= mark_type_use (e
);
1726 if (complain
& tf_error
)
1727 error ("invalid application of %<sizeof%> to a bit-field");
1729 return error_mark_node
;
1732 else if (is_overloaded_fn (e
))
1734 if (complain
& tf_error
)
1735 permerror (input_location
, "ISO C++ forbids applying %<sizeof%> to an expression of "
1738 return error_mark_node
;
1741 else if (type_unknown_p (e
))
1743 if (complain
& tf_error
)
1744 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1746 return error_mark_node
;
1752 return cxx_sizeof_or_alignof_type (e
, SIZEOF_EXPR
, false, complain
& tf_error
);
1755 /* Implement the __alignof keyword: Return the minimum required
1756 alignment of E, measured in bytes. For VAR_DECL's and
1757 FIELD_DECL's return DECL_ALIGN (which can be set from an
1758 "aligned" __attribute__ specification). */
1761 cxx_alignof_expr (tree e
, tsubst_flags_t complain
)
1765 if (e
== error_mark_node
)
1766 return error_mark_node
;
1768 if (processing_template_decl
)
1770 e
= build_min (ALIGNOF_EXPR
, size_type_node
, e
);
1771 TREE_SIDE_EFFECTS (e
) = 0;
1772 TREE_READONLY (e
) = 1;
1777 STRIP_ANY_LOCATION_WRAPPER (e
);
1779 e
= mark_type_use (e
);
1782 t
= size_int (DECL_ALIGN_UNIT (e
));
1783 else if (bitfield_p (e
))
1785 if (complain
& tf_error
)
1786 error ("invalid application of %<__alignof%> to a bit-field");
1788 return error_mark_node
;
1791 else if (TREE_CODE (e
) == COMPONENT_REF
1792 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
)
1793 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e
, 1)));
1794 else if (is_overloaded_fn (e
))
1796 if (complain
& tf_error
)
1797 permerror (input_location
, "ISO C++ forbids applying %<__alignof%> to an expression of "
1800 return error_mark_node
;
1801 if (TREE_CODE (e
) == FUNCTION_DECL
)
1802 t
= size_int (DECL_ALIGN_UNIT (e
));
1806 else if (type_unknown_p (e
))
1808 if (complain
& tf_error
)
1809 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1811 return error_mark_node
;
1815 return cxx_sizeof_or_alignof_type (TREE_TYPE (e
), ALIGNOF_EXPR
, false,
1816 complain
& tf_error
);
1818 return fold_convert (size_type_node
, t
);
1821 /* Process a sizeof or alignof expression E with code OP where the operand
1822 is an expression. */
1825 cxx_sizeof_or_alignof_expr (tree e
, enum tree_code op
, bool complain
)
1827 if (op
== SIZEOF_EXPR
)
1828 return cxx_sizeof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1830 return cxx_alignof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1833 /* Build a representation of an expression 'alignas(E).' Return the
1834 folded integer value of E if it is an integral constant expression
1835 that resolves to a valid alignment. If E depends on a template
1836 parameter, return a syntactic representation tree of kind
1837 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1838 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1841 cxx_alignas_expr (tree e
)
1843 if (e
== NULL_TREE
|| e
== error_mark_node
1844 || (!TYPE_P (e
) && !require_potential_rvalue_constant_expression (e
)))
1850 When the alignment-specifier is of the form
1851 alignas(type-id ), it shall have the same effect as
1852 alignas(alignof(type-id )). */
1854 return cxx_sizeof_or_alignof_type (e
, ALIGNOF_EXPR
, true, false);
1856 /* If we reach this point, it means the alignas expression if of
1857 the form "alignas(assignment-expression)", so we should follow
1858 what is stated by [dcl.align]/2. */
1860 if (value_dependent_expression_p (e
))
1861 /* Leave value-dependent expression alone for now. */
1864 e
= instantiate_non_dependent_expr (e
);
1865 e
= mark_rvalue_use (e
);
1867 /* [dcl.align]/2 says:
1869 the assignment-expression shall be an integral constant
1872 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e
)))
1874 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e
));
1875 return error_mark_node
;
1878 return cxx_constant_value (e
);
1882 /* EXPR is being used in a context that is not a function call.
1887 The expression can be used only as the left-hand operand of a
1888 member function call.
1890 [expr.mptr.operator]
1892 If the result of .* or ->* is a function, then that result can be
1893 used only as the operand for the function call operator ().
1895 by issuing an error message if appropriate. Returns true iff EXPR
1896 violates these rules. */
1899 invalid_nonstatic_memfn_p (location_t loc
, tree expr
, tsubst_flags_t complain
)
1901 if (expr
== NULL_TREE
)
1903 /* Don't enforce this in MS mode. */
1904 if (flag_ms_extensions
)
1906 if (is_overloaded_fn (expr
) && !really_overloaded_fn (expr
))
1907 expr
= get_first_fn (expr
);
1908 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr
))
1910 if (complain
& tf_error
)
1914 error_at (loc
, "invalid use of non-static member function %qD",
1916 inform (DECL_SOURCE_LOCATION (expr
), "declared here");
1919 error_at (loc
, "invalid use of non-static member function of "
1920 "type %qT", TREE_TYPE (expr
));
1927 /* If EXP is a reference to a bitfield, and the type of EXP does not
1928 match the declared type of the bitfield, return the declared type
1929 of the bitfield. Otherwise, return NULL_TREE. */
1932 is_bitfield_expr_with_lowered_type (const_tree exp
)
1934 switch (TREE_CODE (exp
))
1937 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1)
1938 ? TREE_OPERAND (exp
, 1)
1939 : TREE_OPERAND (exp
, 0)))
1941 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 2));
1944 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1));
1948 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1954 field
= TREE_OPERAND (exp
, 1);
1955 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_BIT_FIELD_TYPE (field
))
1957 if (same_type_ignoring_top_level_qualifiers_p
1958 (TREE_TYPE (exp
), DECL_BIT_FIELD_TYPE (field
)))
1960 return DECL_BIT_FIELD_TYPE (field
);
1964 if (DECL_HAS_VALUE_EXPR_P (exp
))
1965 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
1966 (CONST_CAST_TREE (exp
)));
1969 case VIEW_CONVERT_EXPR
:
1970 if (location_wrapper_p (exp
))
1971 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1976 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp
, 0)))
1977 == TYPE_MAIN_VARIANT (TREE_TYPE (exp
)))
1978 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1986 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1987 bitfield with a lowered type, the type of EXP is returned, rather
1991 unlowered_expr_type (const_tree exp
)
1994 tree etype
= TREE_TYPE (exp
);
1996 type
= is_bitfield_expr_with_lowered_type (exp
);
1998 type
= cp_build_qualified_type (type
, cp_type_quals (etype
));
2005 /* Perform the conversions in [expr] that apply when an lvalue appears
2006 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2007 function-to-pointer conversions. In addition, bitfield references are
2008 converted to their declared types. Note that this function does not perform
2009 the lvalue-to-rvalue conversion for class types. If you need that conversion
2010 for class types, then you probably need to use force_rvalue.
2012 Although the returned value is being used as an rvalue, this
2013 function does not wrap the returned expression in a
2014 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2015 that the return value is no longer an lvalue. */
2018 decay_conversion (tree exp
,
2019 tsubst_flags_t complain
,
2020 bool reject_builtin
/* = true */)
2023 enum tree_code code
;
2024 location_t loc
= cp_expr_loc_or_loc (exp
, input_location
);
2026 type
= TREE_TYPE (exp
);
2027 if (type
== error_mark_node
)
2028 return error_mark_node
;
2030 exp
= resolve_nondeduced_context_or_error (exp
, complain
);
2032 code
= TREE_CODE (type
);
2034 if (error_operand_p (exp
))
2035 return error_mark_node
;
2037 if (NULLPTR_TYPE_P (type
) && !TREE_SIDE_EFFECTS (exp
))
2039 mark_rvalue_use (exp
, loc
, reject_builtin
);
2040 return nullptr_node
;
2043 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2044 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2045 if (code
== VOID_TYPE
)
2047 if (complain
& tf_error
)
2048 error_at (loc
, "void value not ignored as it ought to be");
2049 return error_mark_node
;
2051 if (invalid_nonstatic_memfn_p (loc
, exp
, complain
))
2052 return error_mark_node
;
2053 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
2055 exp
= mark_lvalue_use (exp
);
2056 if (reject_builtin
&& reject_gcc_builtin (exp
, loc
))
2057 return error_mark_node
;
2058 return cp_build_addr_expr (exp
, complain
);
2060 if (code
== ARRAY_TYPE
)
2065 exp
= mark_lvalue_use (exp
);
2067 if (INDIRECT_REF_P (exp
))
2068 return build_nop (build_pointer_type (TREE_TYPE (type
)),
2069 TREE_OPERAND (exp
, 0));
2071 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
2073 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1), complain
);
2074 if (op1
== error_mark_node
)
2075 return error_mark_node
;
2076 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
2077 TREE_OPERAND (exp
, 0), op1
);
2080 if (!obvalue_p (exp
)
2081 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
2083 if (complain
& tf_error
)
2084 error_at (loc
, "invalid use of non-lvalue array");
2085 return error_mark_node
;
2088 /* Don't let an array compound literal decay to a pointer. It can
2089 still be used to initialize an array or bind to a reference. */
2090 if (TREE_CODE (exp
) == TARGET_EXPR
)
2092 if (complain
& tf_error
)
2093 error_at (loc
, "taking address of temporary array");
2094 return error_mark_node
;
2097 ptrtype
= build_pointer_type (TREE_TYPE (type
));
2101 if (!cxx_mark_addressable (exp
))
2102 return error_mark_node
;
2103 adr
= build_nop (ptrtype
, build_address (exp
));
2106 /* This way is better for a COMPONENT_REF since it can
2107 simplify the offset for a component. */
2108 adr
= cp_build_addr_expr (exp
, complain
);
2109 return cp_convert (ptrtype
, adr
, complain
);
2112 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2113 exp
= mark_rvalue_use (exp
, loc
, reject_builtin
);
2115 /* If a bitfield is used in a context where integral promotion
2116 applies, then the caller is expected to have used
2117 default_conversion. That function promotes bitfields correctly
2118 before calling this function. At this point, if we have a
2119 bitfield referenced, we may assume that is not subject to
2120 promotion, and that, therefore, the type of the resulting rvalue
2121 is the declared type of the bitfield. */
2122 exp
= convert_bitfield_to_declared_type (exp
);
2124 /* We do not call rvalue() here because we do not want to wrap EXP
2125 in a NON_LVALUE_EXPR. */
2129 Non-class rvalues always have cv-unqualified types. */
2130 type
= TREE_TYPE (exp
);
2131 if (!CLASS_TYPE_P (type
) && cv_qualified_p (type
))
2132 exp
= build_nop (cv_unqualified (type
), exp
);
2134 if (!complete_type_or_maybe_complain (type
, exp
, complain
))
2135 return error_mark_node
;
2140 /* Perform preparatory conversions, as part of the "usual arithmetic
2141 conversions". In particular, as per [expr]:
2143 Whenever an lvalue expression appears as an operand of an
2144 operator that expects the rvalue for that operand, the
2145 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2146 standard conversions are applied to convert the expression to an
2149 In addition, we perform integral promotions here, as those are
2150 applied to both operands to a binary operator before determining
2151 what additional conversions should apply. */
2154 cp_default_conversion (tree exp
, tsubst_flags_t complain
)
2156 /* Check for target-specific promotions. */
2157 tree promoted_type
= targetm
.promoted_type (TREE_TYPE (exp
));
2159 exp
= cp_convert (promoted_type
, exp
, complain
);
2160 /* Perform the integral promotions first so that bitfield
2161 expressions (which may promote to "int", even if the bitfield is
2162 declared "unsigned") are promoted correctly. */
2163 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp
)))
2164 exp
= cp_perform_integral_promotions (exp
, complain
);
2165 /* Perform the other conversions. */
2166 exp
= decay_conversion (exp
, complain
);
2174 default_conversion (tree exp
)
2176 return cp_default_conversion (exp
, tf_warning_or_error
);
2179 /* EXPR is an expression with an integral or enumeration type.
2180 Perform the integral promotions in [conv.prom], and return the
2184 cp_perform_integral_promotions (tree expr
, tsubst_flags_t complain
)
2189 expr
= mark_rvalue_use (expr
);
2190 if (error_operand_p (expr
))
2191 return error_mark_node
;
2195 If the bitfield has an enumerated type, it is treated as any
2196 other value of that type for promotion purposes. */
2197 type
= is_bitfield_expr_with_lowered_type (expr
);
2198 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
2199 type
= TREE_TYPE (expr
);
2200 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
2201 /* Scoped enums don't promote. */
2202 if (SCOPED_ENUM_P (type
))
2204 promoted_type
= type_promotes_to (type
);
2205 if (type
!= promoted_type
)
2206 expr
= cp_convert (promoted_type
, expr
, complain
);
2213 perform_integral_promotions (tree expr
)
2215 return cp_perform_integral_promotions (expr
, tf_warning_or_error
);
2218 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2219 decay_conversion to one. */
2222 string_conv_p (const_tree totype
, const_tree exp
, int warn
)
2226 if (!TYPE_PTR_P (totype
))
2229 t
= TREE_TYPE (totype
);
2230 if (!same_type_p (t
, char_type_node
)
2231 && !same_type_p (t
, char8_type_node
)
2232 && !same_type_p (t
, char16_type_node
)
2233 && !same_type_p (t
, char32_type_node
)
2234 && !same_type_p (t
, wchar_type_node
))
2237 location_t loc
= EXPR_LOC_OR_LOC (exp
, input_location
);
2239 STRIP_ANY_LOCATION_WRAPPER (exp
);
2241 if (TREE_CODE (exp
) == STRING_CST
)
2243 /* Make sure that we don't try to convert between char and wide chars. */
2244 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
2249 /* Is this a string constant which has decayed to 'const char *'? */
2250 t
= build_pointer_type (cp_build_qualified_type (t
, TYPE_QUAL_CONST
));
2251 if (!same_type_p (TREE_TYPE (exp
), t
))
2254 if (TREE_CODE (exp
) != ADDR_EXPR
2255 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
2260 if (cxx_dialect
>= cxx11
)
2261 pedwarn (loc
, OPT_Wwrite_strings
,
2262 "ISO C++ forbids converting a string constant to %qT",
2265 warning_at (loc
, OPT_Wwrite_strings
,
2266 "deprecated conversion from string constant to %qT",
2273 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2274 can, for example, use as an lvalue. This code used to be in
2275 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2276 expressions, where we're dealing with aggregates. But now it's again only
2277 called from unary_complex_lvalue. The case (in particular) that led to
2278 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2282 rationalize_conditional_expr (enum tree_code code
, tree t
,
2283 tsubst_flags_t complain
)
2285 location_t loc
= cp_expr_loc_or_loc (t
, input_location
);
2287 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2288 the first operand is always the one to be used if both operands
2289 are equal, so we know what conditional expression this used to be. */
2290 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
2292 tree op0
= TREE_OPERAND (t
, 0);
2293 tree op1
= TREE_OPERAND (t
, 1);
2295 /* The following code is incorrect if either operand side-effects. */
2296 gcc_assert (!TREE_SIDE_EFFECTS (op0
)
2297 && !TREE_SIDE_EFFECTS (op1
));
2299 build_conditional_expr (loc
,
2300 build_x_binary_op (loc
,
2301 (TREE_CODE (t
) == MIN_EXPR
2302 ? LE_EXPR
: GE_EXPR
),
2303 op0
, TREE_CODE (op0
),
2304 op1
, TREE_CODE (op1
),
2307 cp_build_unary_op (code
, op0
, false, complain
),
2308 cp_build_unary_op (code
, op1
, false, complain
),
2313 build_conditional_expr (loc
, TREE_OPERAND (t
, 0),
2314 cp_build_unary_op (code
, TREE_OPERAND (t
, 1), false,
2316 cp_build_unary_op (code
, TREE_OPERAND (t
, 2), false,
2321 /* Given the TYPE of an anonymous union field inside T, return the
2322 FIELD_DECL for the field. If not found return NULL_TREE. Because
2323 anonymous unions can nest, we must also search all anonymous unions
2324 that are directly reachable. */
2327 lookup_anon_field (tree t
, tree type
)
2331 t
= TYPE_MAIN_VARIANT (t
);
2333 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
2335 if (TREE_STATIC (field
))
2337 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
2340 /* If we find it directly, return the field. */
2341 if (DECL_NAME (field
) == NULL_TREE
2342 && type
== TYPE_MAIN_VARIANT (TREE_TYPE (field
)))
2347 /* Otherwise, it could be nested, search harder. */
2348 if (DECL_NAME (field
) == NULL_TREE
2349 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2351 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
2359 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2360 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2361 non-NULL, it indicates the path to the base used to name MEMBER.
2362 If PRESERVE_REFERENCE is true, the expression returned will have
2363 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2364 returned will have the type referred to by the reference.
2366 This function does not perform access control; that is either done
2367 earlier by the parser when the name of MEMBER is resolved to MEMBER
2368 itself, or later when overload resolution selects one of the
2369 functions indicated by MEMBER. */
2372 build_class_member_access_expr (cp_expr object
, tree member
,
2373 tree access_path
, bool preserve_reference
,
2374 tsubst_flags_t complain
)
2378 tree result
= NULL_TREE
;
2379 tree using_decl
= NULL_TREE
;
2381 if (error_operand_p (object
) || error_operand_p (member
))
2382 return error_mark_node
;
2384 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2388 The type of the first expression shall be "class object" (of a
2390 object_type
= TREE_TYPE (object
);
2391 if (!currently_open_class (object_type
)
2392 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2393 return error_mark_node
;
2394 if (!CLASS_TYPE_P (object_type
))
2396 if (complain
& tf_error
)
2398 if (INDIRECT_TYPE_P (object_type
)
2399 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2400 error ("request for member %qD in %qE, which is of pointer "
2401 "type %qT (maybe you meant to use %<->%> ?)",
2402 member
, object
.get_value (), object_type
);
2404 error ("request for member %qD in %qE, which is of non-class "
2405 "type %qT", member
, object
.get_value (), object_type
);
2407 return error_mark_node
;
2410 /* The standard does not seem to actually say that MEMBER must be a
2411 member of OBJECT_TYPE. However, that is clearly what is
2413 if (DECL_P (member
))
2415 member_scope
= DECL_CLASS_CONTEXT (member
);
2416 if (!mark_used (member
, complain
) && !(complain
& tf_error
))
2417 return error_mark_node
;
2418 if (TREE_DEPRECATED (member
))
2419 warn_deprecated_use (member
, NULL_TREE
);
2422 member_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (member
));
2423 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2424 presently be the anonymous union. Go outwards until we find a
2425 type related to OBJECT_TYPE. */
2426 while ((ANON_AGGR_TYPE_P (member_scope
) || UNSCOPED_ENUM_P (member_scope
))
2427 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
2429 member_scope
= TYPE_CONTEXT (member_scope
);
2430 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
2432 if (complain
& tf_error
)
2434 if (TREE_CODE (member
) == FIELD_DECL
)
2435 error ("invalid use of nonstatic data member %qE", member
);
2437 error ("%qD is not a member of %qT", member
, object_type
);
2439 return error_mark_node
;
2442 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2443 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2444 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2446 tree temp
= unary_complex_lvalue (ADDR_EXPR
, object
);
2449 temp
= cp_build_fold_indirect_ref (temp
);
2450 if (xvalue_p (object
) && !xvalue_p (temp
))
2451 /* Preserve xvalue kind. */
2457 /* In [expr.ref], there is an explicit list of the valid choices for
2458 MEMBER. We check for each of those cases here. */
2461 /* A static data member. */
2463 mark_exp_read (object
);
2465 if (tree wrap
= maybe_get_tls_wrapper_call (result
))
2466 /* Replace an evaluated use of the thread_local variable with
2467 a call to its wrapper. */
2470 /* If OBJECT has side-effects, they are supposed to occur. */
2471 if (TREE_SIDE_EFFECTS (object
))
2472 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
2474 else if (TREE_CODE (member
) == FIELD_DECL
)
2476 /* A non-static data member. */
2481 if (INDIRECT_REF_P (object
))
2483 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object
, 0)));
2485 null_object_p
= false;
2487 /* Convert OBJECT to the type of MEMBER. */
2488 if (!same_type_p (TYPE_MAIN_VARIANT (object_type
),
2489 TYPE_MAIN_VARIANT (member_scope
)))
2494 /* We didn't complain above about a currently open class, but now we
2495 must: we don't know how to refer to a base member before layout is
2496 complete. But still don't complain in a template. */
2497 if (!cp_unevaluated_operand
2498 && !dependent_type_p (object_type
)
2499 && !complete_type_or_maybe_complain (object_type
, object
,
2501 return error_mark_node
;
2503 binfo
= lookup_base (access_path
? access_path
: object_type
,
2504 member_scope
, ba_unique
, &kind
, complain
);
2505 if (binfo
== error_mark_node
)
2506 return error_mark_node
;
2508 /* It is invalid to try to get to a virtual base of a
2509 NULL object. The most common cause is invalid use of
2511 if (null_object_p
&& kind
== bk_via_virtual
)
2513 if (complain
& tf_error
)
2515 error ("invalid access to non-static data member %qD in "
2516 "virtual base of NULL object", member
);
2518 return error_mark_node
;
2521 /* Convert to the base. */
2522 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
2523 /*nonnull=*/1, complain
);
2524 /* If we found the base successfully then we should be able
2525 to convert to it successfully. */
2526 gcc_assert (object
!= error_mark_node
);
2529 /* If MEMBER is from an anonymous aggregate, we have converted
2530 OBJECT so that it refers to the class containing the
2531 anonymous union. Generate a reference to the anonymous union
2532 itself, and recur to find MEMBER. */
2533 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
))
2534 /* When this code is called from build_field_call, the
2535 object already has the type of the anonymous union.
2536 That is because the COMPONENT_REF was already
2537 constructed, and was then disassembled before calling
2538 build_field_call. After the function-call code is
2539 cleaned up, this waste can be eliminated. */
2540 && (!same_type_ignoring_top_level_qualifiers_p
2541 (TREE_TYPE (object
), DECL_CONTEXT (member
))))
2543 tree anonymous_union
;
2545 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
2546 DECL_CONTEXT (member
));
2547 object
= build_class_member_access_expr (object
,
2549 /*access_path=*/NULL_TREE
,
2554 /* Compute the type of the field, as described in [expr.ref]. */
2555 type_quals
= TYPE_UNQUALIFIED
;
2556 member_type
= TREE_TYPE (member
);
2557 if (!TYPE_REF_P (member_type
))
2559 type_quals
= (cp_type_quals (member_type
)
2560 | cp_type_quals (object_type
));
2562 /* A field is const (volatile) if the enclosing object, or the
2563 field itself, is const (volatile). But, a mutable field is
2564 not const, even within a const object. */
2565 if (DECL_MUTABLE_P (member
))
2566 type_quals
&= ~TYPE_QUAL_CONST
;
2567 member_type
= cp_build_qualified_type (member_type
, type_quals
);
2570 result
= build3_loc (input_location
, COMPONENT_REF
, member_type
,
2571 object
, member
, NULL_TREE
);
2573 /* Mark the expression const or volatile, as appropriate. Even
2574 though we've dealt with the type above, we still have to mark the
2575 expression itself. */
2576 if (type_quals
& TYPE_QUAL_CONST
)
2577 TREE_READONLY (result
) = 1;
2578 if (type_quals
& TYPE_QUAL_VOLATILE
)
2579 TREE_THIS_VOLATILE (result
) = 1;
2581 else if (BASELINK_P (member
))
2583 /* The member is a (possibly overloaded) member function. */
2587 /* If the MEMBER is exactly one static member function, then we
2588 know the type of the expression. Otherwise, we must wait
2589 until overload resolution has been performed. */
2590 functions
= BASELINK_FUNCTIONS (member
);
2591 if (TREE_CODE (functions
) == FUNCTION_DECL
2592 && DECL_STATIC_FUNCTION_P (functions
))
2593 type
= TREE_TYPE (functions
);
2595 type
= unknown_type_node
;
2596 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2597 base. That will happen when the function is called. */
2598 result
= build3_loc (input_location
, COMPONENT_REF
, type
, object
, member
,
2601 else if (TREE_CODE (member
) == CONST_DECL
)
2603 /* The member is an enumerator. */
2605 /* If OBJECT has side-effects, they are supposed to occur. */
2606 if (TREE_SIDE_EFFECTS (object
))
2607 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
2610 else if ((using_decl
= strip_using_decl (member
)) != member
)
2611 result
= build_class_member_access_expr (object
,
2613 access_path
, preserve_reference
,
2617 if (complain
& tf_error
)
2618 error ("invalid use of %qD", member
);
2619 return error_mark_node
;
2622 if (!preserve_reference
)
2625 If E2 is declared to have type "reference to T", then ... the
2626 type of E1.E2 is T. */
2627 result
= convert_from_reference (result
);
2632 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2633 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2636 lookup_destructor (tree object
, tree scope
, tree dtor_name
,
2637 tsubst_flags_t complain
)
2639 tree object_type
= TREE_TYPE (object
);
2640 tree dtor_type
= TREE_OPERAND (dtor_name
, 0);
2643 /* We've already complained about this destructor. */
2644 if (dtor_type
== error_mark_node
)
2645 return error_mark_node
;
2647 if (scope
&& !check_dtor_name (scope
, dtor_type
))
2649 if (complain
& tf_error
)
2650 error ("qualified type %qT does not match destructor name ~%qT",
2652 return error_mark_node
;
2654 if (is_auto (dtor_type
))
2655 dtor_type
= object_type
;
2656 else if (identifier_p (dtor_type
))
2658 /* In a template, names we can't find a match for are still accepted
2659 destructor names, and we check them here. */
2660 if (check_dtor_name (object_type
, dtor_type
))
2661 dtor_type
= object_type
;
2664 if (complain
& tf_error
)
2665 error ("object type %qT does not match destructor name ~%qT",
2666 object_type
, dtor_type
);
2667 return error_mark_node
;
2671 else if (!DERIVED_FROM_P (dtor_type
, TYPE_MAIN_VARIANT (object_type
)))
2673 if (complain
& tf_error
)
2674 error ("the type being destroyed is %qT, but the destructor "
2675 "refers to %qT", TYPE_MAIN_VARIANT (object_type
), dtor_type
);
2676 return error_mark_node
;
2678 expr
= lookup_member (dtor_type
, complete_dtor_identifier
,
2679 /*protect=*/1, /*want_type=*/false,
2680 tf_warning_or_error
);
2683 if (complain
& tf_error
)
2684 cxx_incomplete_type_error (dtor_name
, dtor_type
);
2685 return error_mark_node
;
2687 expr
= (adjust_result_of_qualified_name_lookup
2688 (expr
, dtor_type
, object_type
));
2689 if (scope
== NULL_TREE
)
2690 /* We need to call adjust_result_of_qualified_name_lookup in case the
2691 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2692 that we still get virtual function binding. */
2693 BASELINK_QUALIFIED_P (expr
) = false;
2697 /* An expression of the form "A::template B" has been resolved to
2698 DECL. Issue a diagnostic if B is not a template or template
2702 check_template_keyword (tree decl
)
2704 /* The standard says:
2708 If a name prefixed by the keyword template is not a member
2709 template, the program is ill-formed.
2711 DR 228 removed the restriction that the template be a member
2714 DR 96, if accepted would add the further restriction that explicit
2715 template arguments must be provided if the template keyword is
2716 used, but, as of 2005-10-16, that DR is still in "drafting". If
2717 this DR is accepted, then the semantic checks here can be
2718 simplified, as the entity named must in fact be a template
2719 specialization, rather than, as at present, a set of overloaded
2720 functions containing at least one template function. */
2721 if (TREE_CODE (decl
) != TEMPLATE_DECL
2722 && TREE_CODE (decl
) != TEMPLATE_ID_EXPR
)
2726 if (DECL_USE_TEMPLATE (decl
)
2727 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2730 permerror (input_location
, "%qD is not a template", decl
);
2732 else if (!is_overloaded_fn (decl
))
2733 permerror (input_location
, "%qD is not a template", decl
);
2738 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
2739 !found
&& iter
; ++iter
)
2742 if (TREE_CODE (fn
) == TEMPLATE_DECL
2743 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
2744 || (TREE_CODE (fn
) == FUNCTION_DECL
2745 && DECL_USE_TEMPLATE (fn
)
2746 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn
))))
2750 permerror (input_location
, "%qD is not a template", decl
);
2755 /* Record that an access failure occurred on BASETYPE_PATH attempting
2756 to access DECL, where DIAG_DECL should be used for diagnostics. */
2759 access_failure_info::record_access_failure (tree basetype_path
,
2760 tree decl
, tree diag_decl
)
2762 m_was_inaccessible
= true;
2763 m_basetype_path
= basetype_path
;
2765 m_diag_decl
= diag_decl
;
2768 /* If an access failure was recorded, then attempt to locate an
2769 accessor function for the pertinent field.
2770 Otherwise, return NULL_TREE. */
2773 access_failure_info::get_any_accessor (bool const_p
) const
2775 if (!was_inaccessible_p ())
2779 = locate_field_accessor (m_basetype_path
, m_diag_decl
, const_p
);
2783 /* The accessor must itself be accessible for it to be a reasonable
2785 if (!accessible_p (m_basetype_path
, accessor
, true))
2791 /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
2792 replacing the primary location in RICHLOC with "accessor()". */
2795 access_failure_info::add_fixit_hint (rich_location
*richloc
,
2799 pp_printf (&pp
, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor_decl
)));
2800 richloc
->add_fixit_replace (pp_formatted_text (&pp
));
2803 /* If an access failure was recorded, then attempt to locate an
2804 accessor function for the pertinent field, and if one is
2805 available, add a note and fix-it hint suggesting using it. */
2808 access_failure_info::maybe_suggest_accessor (bool const_p
) const
2810 tree accessor
= get_any_accessor (const_p
);
2811 if (accessor
== NULL_TREE
)
2813 rich_location
richloc (line_table
, input_location
);
2814 add_fixit_hint (&richloc
, accessor
);
2815 inform (&richloc
, "field %q#D can be accessed via %q#D",
2816 m_diag_decl
, accessor
);
2819 /* Subroutine of finish_class_member_access_expr.
2820 Issue an error about NAME not being a member of ACCESS_PATH (or
2821 OBJECT_TYPE), potentially providing a fix-it hint for misspelled
2825 complain_about_unrecognized_member (tree access_path
, tree name
,
2828 /* Attempt to provide a hint about misspelled names. */
2829 tree guessed_id
= lookup_member_fuzzy (access_path
, name
,
2830 /*want_type=*/false);
2831 if (guessed_id
== NULL_TREE
)
2834 error ("%q#T has no member named %qE",
2835 TREE_CODE (access_path
) == TREE_BINFO
2836 ? TREE_TYPE (access_path
) : object_type
, name
);
2840 location_t bogus_component_loc
= input_location
;
2841 gcc_rich_location
rich_loc (bogus_component_loc
);
2843 /* Check that the guessed name is accessible along access_path. */
2844 access_failure_info afi
;
2845 lookup_member (access_path
, guessed_id
, /*protect=*/1,
2846 /*want_type=*/false, /*complain=*/false,
2848 if (afi
.was_inaccessible_p ())
2850 tree accessor
= afi
.get_any_accessor (TYPE_READONLY (object_type
));
2853 /* The guessed name isn't directly accessible, but can be accessed
2854 via an accessor member function. */
2855 afi
.add_fixit_hint (&rich_loc
, accessor
);
2856 error_at (&rich_loc
,
2857 "%q#T has no member named %qE;"
2858 " did you mean %q#D? (accessible via %q#D)",
2859 TREE_CODE (access_path
) == TREE_BINFO
2860 ? TREE_TYPE (access_path
) : object_type
,
2861 name
, afi
.get_diag_decl (), accessor
);
2865 /* The guessed name isn't directly accessible, and no accessor
2866 member function could be found. */
2867 error_at (&rich_loc
,
2868 "%q#T has no member named %qE;"
2869 " did you mean %q#D? (not accessible from this context)",
2870 TREE_CODE (access_path
) == TREE_BINFO
2871 ? TREE_TYPE (access_path
) : object_type
,
2872 name
, afi
.get_diag_decl ());
2873 complain_about_access (afi
.get_decl (), afi
.get_diag_decl (), false);
2878 /* The guessed name is directly accessible; suggest it. */
2879 rich_loc
.add_fixit_misspelled_id (bogus_component_loc
,
2881 error_at (&rich_loc
,
2882 "%q#T has no member named %qE;"
2883 " did you mean %qE?",
2884 TREE_CODE (access_path
) == TREE_BINFO
2885 ? TREE_TYPE (access_path
) : object_type
,
2890 /* This function is called by the parser to process a class member
2891 access expression of the form OBJECT.NAME. NAME is a node used by
2892 the parser to represent a name; it is not yet a DECL. It may,
2893 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2894 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2895 there is no reason to do the lookup twice, so the parser keeps the
2896 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2897 be a template via the use of the "A::template B" syntax. */
2900 finish_class_member_access_expr (cp_expr object
, tree name
, bool template_p
,
2901 tsubst_flags_t complain
)
2906 tree access_path
= NULL_TREE
;
2907 tree orig_object
= object
;
2908 tree orig_name
= name
;
2910 if (object
== error_mark_node
|| name
== error_mark_node
)
2911 return error_mark_node
;
2913 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2914 if (!objc_is_public (object
, name
))
2915 return error_mark_node
;
2917 object_type
= TREE_TYPE (object
);
2919 if (processing_template_decl
)
2921 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2922 type_dependent_object_expression_p (object
)
2923 /* If NAME is "f<args>", where either 'f' or 'args' is
2924 dependent, then the expression is dependent. */
2925 || (TREE_CODE (name
) == TEMPLATE_ID_EXPR
2926 && dependent_template_id_p (TREE_OPERAND (name
, 0),
2927 TREE_OPERAND (name
, 1)))
2928 /* If NAME is "T::X" where "T" is dependent, then the
2929 expression is dependent. */
2930 || (TREE_CODE (name
) == SCOPE_REF
2931 && TYPE_P (TREE_OPERAND (name
, 0))
2932 && dependent_scope_p (TREE_OPERAND (name
, 0)))
2933 /* If NAME is operator T where "T" is dependent, we can't
2934 lookup until we instantiate the T. */
2935 || (TREE_CODE (name
) == IDENTIFIER_NODE
2936 && IDENTIFIER_CONV_OP_P (name
)
2937 && dependent_type_p (TREE_TYPE (name
))))
2940 return build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
,
2941 orig_object
, orig_name
, NULL_TREE
);
2943 object
= build_non_dependent_expr (object
);
2945 else if (c_dialect_objc ()
2946 && identifier_p (name
)
2947 && (expr
= objc_maybe_build_component_ref (object
, name
)))
2952 The type of the first expression shall be "class object" (of a
2954 if (!currently_open_class (object_type
)
2955 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2956 return error_mark_node
;
2957 if (!CLASS_TYPE_P (object_type
))
2959 if (complain
& tf_error
)
2961 if (INDIRECT_TYPE_P (object_type
)
2962 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2963 error ("request for member %qD in %qE, which is of pointer "
2964 "type %qT (maybe you meant to use %<->%> ?)",
2965 name
, object
.get_value (), object_type
);
2967 error ("request for member %qD in %qE, which is of non-class "
2968 "type %qT", name
, object
.get_value (), object_type
);
2970 return error_mark_node
;
2973 if (BASELINK_P (name
))
2974 /* A member function that has already been looked up. */
2978 bool is_template_id
= false;
2979 tree template_args
= NULL_TREE
;
2980 tree scope
= NULL_TREE
;
2982 access_path
= object_type
;
2984 if (TREE_CODE (name
) == SCOPE_REF
)
2986 /* A qualified name. The qualifying class or namespace `S'
2987 has already been looked up; it is either a TYPE or a
2989 scope
= TREE_OPERAND (name
, 0);
2990 name
= TREE_OPERAND (name
, 1);
2992 /* If SCOPE is a namespace, then the qualified name does not
2993 name a member of OBJECT_TYPE. */
2994 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
2996 if (complain
& tf_error
)
2997 error ("%<%D::%D%> is not a member of %qT",
2998 scope
, name
, object_type
);
2999 return error_mark_node
;
3003 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
3005 is_template_id
= true;
3006 template_args
= TREE_OPERAND (name
, 1);
3007 name
= TREE_OPERAND (name
, 0);
3009 if (!identifier_p (name
))
3010 name
= OVL_NAME (name
);
3015 if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
3017 gcc_assert (!is_template_id
);
3018 /* Looking up a member enumerator (c++/56793). */
3019 if (!TYPE_CLASS_SCOPE_P (scope
)
3020 || !DERIVED_FROM_P (TYPE_CONTEXT (scope
), object_type
))
3022 if (complain
& tf_error
)
3023 error ("%<%D::%D%> is not a member of %qT",
3024 scope
, name
, object_type
);
3025 return error_mark_node
;
3027 tree val
= lookup_enumerator (scope
, name
);
3030 if (complain
& tf_error
)
3031 error ("%qD is not a member of %qD",
3033 return error_mark_node
;
3036 if (TREE_SIDE_EFFECTS (object
))
3037 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
), object
, val
);
3041 gcc_assert (CLASS_TYPE_P (scope
));
3042 gcc_assert (identifier_p (name
) || TREE_CODE (name
) == BIT_NOT_EXPR
);
3044 if (constructor_name_p (name
, scope
))
3046 if (complain
& tf_error
)
3047 error ("cannot call constructor %<%T::%D%> directly",
3049 return error_mark_node
;
3052 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3053 access_path
= lookup_base (object_type
, scope
, ba_check
,
3055 if (access_path
== error_mark_node
)
3056 return error_mark_node
;
3059 if (any_dependent_bases_p (object_type
))
3061 if (complain
& tf_error
)
3062 error ("%qT is not a base of %qT", scope
, object_type
);
3063 return error_mark_node
;
3067 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
3069 if (dependent_type_p (object_type
))
3070 /* The destructor isn't declared yet. */
3072 member
= lookup_destructor (object
, scope
, name
, complain
);
3076 /* Look up the member. */
3077 access_failure_info afi
;
3078 member
= lookup_member (access_path
, name
, /*protect=*/1,
3079 /*want_type=*/false, complain
,
3081 afi
.maybe_suggest_accessor (TYPE_READONLY (object_type
));
3082 if (member
== NULL_TREE
)
3084 if (dependent_type_p (object_type
))
3085 /* Try again at instantiation time. */
3087 if (complain
& tf_error
)
3088 complain_about_unrecognized_member (access_path
, name
,
3090 return error_mark_node
;
3092 if (member
== error_mark_node
)
3093 return error_mark_node
;
3095 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member
)))
3096 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3097 wrong, so don't use it. */
3099 if (TREE_CODE (member
) == USING_DECL
&& DECL_DEPENDENT_P (member
))
3105 tree templ
= member
;
3107 if (BASELINK_P (templ
))
3108 member
= lookup_template_function (templ
, template_args
);
3109 else if (variable_template_p (templ
))
3110 member
= (lookup_and_finish_template_variable
3111 (templ
, template_args
, complain
));
3114 if (complain
& tf_error
)
3115 error ("%qD is not a member template function", name
);
3116 return error_mark_node
;
3121 if (TREE_DEPRECATED (member
))
3122 warn_deprecated_use (member
, NULL_TREE
);
3125 check_template_keyword (member
);
3127 expr
= build_class_member_access_expr (object
, member
, access_path
,
3128 /*preserve_reference=*/false,
3130 if (processing_template_decl
&& expr
!= error_mark_node
)
3132 if (BASELINK_P (member
))
3134 if (TREE_CODE (orig_name
) == SCOPE_REF
)
3135 BASELINK_QUALIFIED_P (member
) = 1;
3138 return build_min_non_dep (COMPONENT_REF
, expr
,
3139 orig_object
, orig_name
,
3146 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3150 build_simple_component_ref (tree object
, tree member
)
3152 tree type
= cp_build_qualified_type (TREE_TYPE (member
),
3153 cp_type_quals (TREE_TYPE (object
)));
3154 return build3_loc (input_location
,
3155 COMPONENT_REF
, type
,
3156 object
, member
, NULL_TREE
);
3159 /* Return an expression for the MEMBER_NAME field in the internal
3160 representation of PTRMEM, a pointer-to-member function. (Each
3161 pointer-to-member function type gets its own RECORD_TYPE so it is
3162 more convenient to access the fields by name than by FIELD_DECL.)
3163 This routine converts the NAME to a FIELD_DECL and then creates the
3164 node for the complete expression. */
3167 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
3172 if (TREE_CODE (ptrmem
) == CONSTRUCTOR
)
3176 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ptrmem
),
3178 if (index
&& DECL_P (index
) && DECL_NAME (index
) == member_name
)
3183 /* This code is a stripped down version of
3184 build_class_member_access_expr. It does not work to use that
3185 routine directly because it expects the object to be of class
3187 ptrmem_type
= TREE_TYPE (ptrmem
);
3188 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
));
3189 for (member
= TYPE_FIELDS (ptrmem_type
); member
;
3190 member
= DECL_CHAIN (member
))
3191 if (DECL_NAME (member
) == member_name
)
3193 tree res
= build_simple_component_ref (ptrmem
, member
);
3195 TREE_NO_WARNING (res
) = 1;
3199 /* Given an expression PTR for a pointer, return an expression
3200 for the value pointed to.
3201 ERRORSTRING is the name of the operator to appear in error messages.
3203 This function may need to overload OPERATOR_FNNAME.
3204 Must also handle REFERENCE_TYPEs for C++. */
3207 build_x_indirect_ref (location_t loc
, tree expr
, ref_operator errorstring
,
3208 tsubst_flags_t complain
)
3210 tree orig_expr
= expr
;
3212 tree overload
= NULL_TREE
;
3214 if (processing_template_decl
)
3216 /* Retain the type if we know the operand is a pointer. */
3217 if (TREE_TYPE (expr
) && INDIRECT_TYPE_P (TREE_TYPE (expr
)))
3218 return build_min (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
3219 if (type_dependent_expression_p (expr
))
3220 return build_min_nt_loc (loc
, INDIRECT_REF
, expr
);
3221 expr
= build_non_dependent_expr (expr
);
3224 rval
= build_new_op (loc
, INDIRECT_REF
, LOOKUP_NORMAL
, expr
,
3225 NULL_TREE
, NULL_TREE
, &overload
, complain
);
3227 rval
= cp_build_indirect_ref (expr
, errorstring
, complain
);
3229 if (processing_template_decl
&& rval
!= error_mark_node
)
3231 if (overload
!= NULL_TREE
)
3232 return (build_min_non_dep_op_overload
3233 (INDIRECT_REF
, rval
, overload
, orig_expr
));
3235 return build_min_non_dep (INDIRECT_REF
, rval
, orig_expr
);
3241 /* The implementation of the above, and of indirection implied by other
3242 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3245 cp_build_indirect_ref_1 (tree ptr
, ref_operator errorstring
,
3246 tsubst_flags_t complain
, bool do_fold
)
3250 /* RO_NULL should only be used with the folding entry points below, not
3251 cp_build_indirect_ref. */
3252 gcc_checking_assert (errorstring
!= RO_NULL
|| do_fold
);
3254 if (ptr
== current_class_ptr
3255 || (TREE_CODE (ptr
) == NOP_EXPR
3256 && TREE_OPERAND (ptr
, 0) == current_class_ptr
3257 && (same_type_ignoring_top_level_qualifiers_p
3258 (TREE_TYPE (ptr
), TREE_TYPE (current_class_ptr
)))))
3259 return current_class_ref
;
3261 pointer
= (TYPE_REF_P (TREE_TYPE (ptr
))
3262 ? ptr
: decay_conversion (ptr
, complain
));
3263 if (pointer
== error_mark_node
)
3264 return error_mark_node
;
3266 type
= TREE_TYPE (pointer
);
3268 if (INDIRECT_TYPE_P (type
))
3272 If the type of the expression is "pointer to T," the type
3273 of the result is "T." */
3274 tree t
= TREE_TYPE (type
);
3276 if ((CONVERT_EXPR_P (ptr
)
3277 || TREE_CODE (ptr
) == VIEW_CONVERT_EXPR
)
3278 && (!CLASS_TYPE_P (t
) || !CLASSTYPE_EMPTY_P (t
)))
3280 /* If a warning is issued, mark it to avoid duplicates from
3281 the backend. This only needs to be done at
3282 warn_strict_aliasing > 2. */
3283 if (warn_strict_aliasing
> 2)
3284 if (strict_aliasing_warning (EXPR_LOCATION (ptr
),
3285 type
, TREE_OPERAND (ptr
, 0)))
3286 TREE_NO_WARNING (ptr
) = 1;
3289 if (VOID_TYPE_P (t
))
3291 /* A pointer to incomplete type (other than cv void) can be
3292 dereferenced [expr.unary.op]/1 */
3293 if (complain
& tf_error
)
3294 error ("%qT is not a pointer-to-object type", type
);
3295 return error_mark_node
;
3297 else if (do_fold
&& TREE_CODE (pointer
) == ADDR_EXPR
3298 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
3299 /* The POINTER was something like `&x'. We simplify `*&x' to
3301 return TREE_OPERAND (pointer
, 0);
3304 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
3306 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3307 so that we get the proper error message if the result is used
3308 to assign to. Also, &* is supposed to be a no-op. */
3309 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
3310 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
3311 TREE_SIDE_EFFECTS (ref
)
3312 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
));
3316 else if (!(complain
& tf_error
))
3317 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3319 /* `pointer' won't be an error_mark_node if we were given a
3320 pointer to member, so it's cool to check for this here. */
3321 else if (TYPE_PTRMEM_P (type
))
3322 switch (errorstring
)
3324 case RO_ARRAY_INDEXING
:
3325 error ("invalid use of array indexing on pointer to member");
3328 error ("invalid use of unary %<*%> on pointer to member");
3330 case RO_IMPLICIT_CONVERSION
:
3331 error ("invalid use of implicit conversion on pointer to member");
3334 error ("left hand operand of %<->*%> must be a pointer to class, "
3335 "but is a pointer to member of type %qT", type
);
3340 else if (pointer
!= error_mark_node
)
3341 invalid_indirection_error (input_location
, type
, errorstring
);
3343 return error_mark_node
;
3346 /* Entry point used by c-common, which expects folding. */
3349 build_indirect_ref (location_t
/*loc*/,
3350 tree ptr
, ref_operator errorstring
)
3352 return cp_build_indirect_ref_1 (ptr
, errorstring
, tf_warning_or_error
, true);
3355 /* Entry point used by internal indirection needs that don't correspond to any
3356 syntactic construct. */
3359 cp_build_fold_indirect_ref (tree pointer
)
3361 return cp_build_indirect_ref_1 (pointer
, RO_NULL
, tf_warning_or_error
, true);
3364 /* Entry point used by indirection needs that correspond to some syntactic
3368 cp_build_indirect_ref (tree ptr
, ref_operator errorstring
,
3369 tsubst_flags_t complain
)
3371 return cp_build_indirect_ref_1 (ptr
, errorstring
, complain
, false);
3374 /* This handles expressions of the form "a[i]", which denotes
3377 This is logically equivalent in C to *(a+i), but we may do it differently.
3378 If A is a variable or a member, we generate a primitive ARRAY_REF.
3379 This avoids forcing the array out of registers, and can work on
3380 arrays that are not lvalues (for example, members of structures returned
3383 If INDEX is of some user-defined type, it must be converted to
3384 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3385 will inherit the type of the array, which will be some pointer type.
3387 LOC is the location to use in building the array reference. */
3390 cp_build_array_ref (location_t loc
, tree array
, tree idx
,
3391 tsubst_flags_t complain
)
3397 if (complain
& tf_error
)
3398 error_at (loc
, "subscript missing in array reference");
3399 return error_mark_node
;
3402 if (TREE_TYPE (array
) == error_mark_node
3403 || TREE_TYPE (idx
) == error_mark_node
)
3404 return error_mark_node
;
3406 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3408 switch (TREE_CODE (array
))
3412 tree value
= cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3414 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3415 TREE_OPERAND (array
, 0), value
);
3416 SET_EXPR_LOCATION (ret
, loc
);
3421 ret
= build_conditional_expr
3422 (loc
, TREE_OPERAND (array
, 0),
3423 cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3425 cp_build_array_ref (loc
, TREE_OPERAND (array
, 2), idx
,
3428 protected_set_expr_location (ret
, loc
);
3435 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, idx
);
3437 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
3441 warn_array_subscript_with_type_char (loc
, idx
);
3443 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx
)))
3445 if (complain
& tf_error
)
3446 error_at (loc
, "array subscript is not an integer");
3447 return error_mark_node
;
3450 /* Apply integral promotions *after* noticing character types.
3451 (It is unclear why we do these promotions -- the standard
3452 does not say that we should. In fact, the natural thing would
3453 seem to be to convert IDX to ptrdiff_t; we're performing
3454 pointer arithmetic.) */
3455 idx
= cp_perform_integral_promotions (idx
, complain
);
3457 idx
= maybe_constant_value (idx
);
3459 /* An array that is indexed by a non-constant
3460 cannot be stored in a register; we must be able to do
3461 address arithmetic on its address.
3462 Likewise an array of elements of variable size. */
3463 if (TREE_CODE (idx
) != INTEGER_CST
3464 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
3465 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
3468 if (!cxx_mark_addressable (array
, true))
3469 return error_mark_node
;
3472 /* An array that is indexed by a constant value which is not within
3473 the array bounds cannot be stored in a register either; because we
3474 would get a crash in store_bit_field/extract_bit_field when trying
3475 to access a non-existent part of the register. */
3476 if (TREE_CODE (idx
) == INTEGER_CST
3477 && TYPE_DOMAIN (TREE_TYPE (array
))
3478 && ! int_fits_type_p (idx
, TYPE_DOMAIN (TREE_TYPE (array
))))
3480 if (!cxx_mark_addressable (array
))
3481 return error_mark_node
;
3484 /* Note in C++ it is valid to subscript a `register' array, since
3485 it is valid to take the address of something with that
3486 storage specification. */
3490 while (TREE_CODE (foo
) == COMPONENT_REF
)
3491 foo
= TREE_OPERAND (foo
, 0);
3492 if (VAR_P (foo
) && DECL_REGISTER (foo
)
3493 && (complain
& tf_warning
))
3494 warning_at (loc
, OPT_Wextra
,
3495 "subscripting array declared %<register%>");
3498 type
= TREE_TYPE (TREE_TYPE (array
));
3499 rval
= build4 (ARRAY_REF
, type
, array
, idx
, NULL_TREE
, NULL_TREE
);
3500 /* Array ref is const/volatile if the array elements are
3501 or if the array is.. */
3502 TREE_READONLY (rval
)
3503 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
3504 TREE_SIDE_EFFECTS (rval
)
3505 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
3506 TREE_THIS_VOLATILE (rval
)
3507 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
3508 ret
= require_complete_type_sfinae (rval
, complain
);
3509 protected_set_expr_location (ret
, loc
);
3511 ret
= non_lvalue_loc (loc
, ret
);
3516 tree ar
= cp_default_conversion (array
, complain
);
3517 tree ind
= cp_default_conversion (idx
, complain
);
3519 /* Put the integer in IND to simplify error checking. */
3520 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
3521 std::swap (ar
, ind
);
3523 if (ar
== error_mark_node
|| ind
== error_mark_node
)
3524 return error_mark_node
;
3526 if (!TYPE_PTR_P (TREE_TYPE (ar
)))
3528 if (complain
& tf_error
)
3529 error_at (loc
, "subscripted value is neither array nor pointer");
3530 return error_mark_node
;
3532 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
3534 if (complain
& tf_error
)
3535 error_at (loc
, "array subscript is not an integer");
3536 return error_mark_node
;
3539 warn_array_subscript_with_type_char (loc
, idx
);
3541 ret
= cp_build_indirect_ref (cp_build_binary_op (input_location
,
3546 protected_set_expr_location (ret
, loc
);
3548 ret
= non_lvalue_loc (loc
, ret
);
3553 /* Entry point for Obj-C++. */
3556 build_array_ref (location_t loc
, tree array
, tree idx
)
3558 return cp_build_array_ref (loc
, array
, idx
, tf_warning_or_error
);
3561 /* Resolve a pointer to member function. INSTANCE is the object
3562 instance to use, if the member points to a virtual member.
3564 This used to avoid checking for virtual functions if basetype
3565 has no virtual functions, according to an earlier ANSI draft.
3566 With the final ISO C++ rules, such an optimization is
3567 incorrect: A pointer to a derived member can be static_cast
3568 to pointer-to-base-member, as long as the dynamic object
3569 later has the right member. So now we only do this optimization
3570 when we know the dynamic type of the object. */
3573 get_member_function_from_ptrfunc (tree
*instance_ptrptr
, tree function
,
3574 tsubst_flags_t complain
)
3576 if (TREE_CODE (function
) == OFFSET_REF
)
3577 function
= TREE_OPERAND (function
, 1);
3579 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
3581 tree idx
, delta
, e1
, e2
, e3
, vtbl
;
3583 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
3584 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
3586 tree instance_ptr
= *instance_ptrptr
;
3587 tree instance_save_expr
= 0;
3588 if (instance_ptr
== error_mark_node
)
3590 if (TREE_CODE (function
) == PTRMEM_CST
)
3592 /* Extracting the function address from a pmf is only
3593 allowed with -Wno-pmf-conversions. It only works for
3595 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
), complain
);
3596 e1
= convert (fntype
, e1
);
3601 if (complain
& tf_error
)
3602 error ("object missing in use of %qE", function
);
3603 return error_mark_node
;
3607 /* True if we know that the dynamic type of the object doesn't have
3608 virtual functions, so we can assume the PFN field is a pointer. */
3609 nonvirtual
= (COMPLETE_TYPE_P (basetype
)
3610 && !TYPE_POLYMORPHIC_P (basetype
)
3611 && resolves_to_fixed_type_p (instance_ptr
, 0));
3613 /* If we don't really have an object (i.e. in an ill-formed
3614 conversion from PMF to pointer), we can't resolve virtual
3615 functions anyway. */
3616 if (!nonvirtual
&& is_dummy_object (instance_ptr
))
3619 if (TREE_SIDE_EFFECTS (instance_ptr
))
3620 instance_ptr
= instance_save_expr
= save_expr (instance_ptr
);
3622 if (TREE_SIDE_EFFECTS (function
))
3623 function
= save_expr (function
);
3625 /* Start by extracting all the information from the PMF itself. */
3626 e3
= pfn_from_ptrmemfunc (function
);
3627 delta
= delta_from_ptrmemfunc (function
);
3628 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
3629 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
3631 int flag_sanitize_save
;
3632 case ptrmemfunc_vbit_in_pfn
:
3633 e1
= cp_build_binary_op (input_location
,
3634 BIT_AND_EXPR
, idx
, integer_one_node
,
3636 idx
= cp_build_binary_op (input_location
,
3637 MINUS_EXPR
, idx
, integer_one_node
,
3639 if (idx
== error_mark_node
)
3640 return error_mark_node
;
3643 case ptrmemfunc_vbit_in_delta
:
3644 e1
= cp_build_binary_op (input_location
,
3645 BIT_AND_EXPR
, delta
, integer_one_node
,
3647 /* Don't instrument the RSHIFT_EXPR we're about to create because
3648 we're going to use DELTA number of times, and that wouldn't play
3649 well with SAVE_EXPRs therein. */
3650 flag_sanitize_save
= flag_sanitize
;
3652 delta
= cp_build_binary_op (input_location
,
3653 RSHIFT_EXPR
, delta
, integer_one_node
,
3655 flag_sanitize
= flag_sanitize_save
;
3656 if (delta
== error_mark_node
)
3657 return error_mark_node
;
3664 if (e1
== error_mark_node
)
3665 return error_mark_node
;
3667 /* Convert down to the right base before using the instance. A
3668 special case is that in a pointer to member of class C, C may
3669 be incomplete. In that case, the function will of course be
3670 a member of C, and no conversion is required. In fact,
3671 lookup_base will fail in that case, because incomplete
3672 classes do not have BINFOs. */
3673 if (!same_type_ignoring_top_level_qualifiers_p
3674 (basetype
, TREE_TYPE (TREE_TYPE (instance_ptr
))))
3676 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
3677 basetype
, ba_check
, NULL
, complain
);
3678 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
,
3680 if (instance_ptr
== error_mark_node
)
3681 return error_mark_node
;
3683 /* ...and then the delta in the PMF. */
3684 instance_ptr
= fold_build_pointer_plus (instance_ptr
, delta
);
3686 /* Hand back the adjusted 'this' argument to our caller. */
3687 *instance_ptrptr
= instance_ptr
;
3690 /* Now just return the pointer. */
3693 /* Next extract the vtable pointer from the object. */
3694 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
3696 vtbl
= cp_build_fold_indirect_ref (vtbl
);
3697 if (vtbl
== error_mark_node
)
3698 return error_mark_node
;
3700 /* Finally, extract the function pointer from the vtable. */
3701 e2
= fold_build_pointer_plus_loc (input_location
, vtbl
, idx
);
3702 e2
= cp_build_fold_indirect_ref (e2
);
3703 if (e2
== error_mark_node
)
3704 return error_mark_node
;
3705 TREE_CONSTANT (e2
) = 1;
3707 /* When using function descriptors, the address of the
3708 vtable entry is treated as a function pointer. */
3709 if (TARGET_VTABLE_USES_DESCRIPTORS
)
3710 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
3711 cp_build_addr_expr (e2
, complain
));
3713 e2
= fold_convert (TREE_TYPE (e3
), e2
);
3714 e1
= build_conditional_expr (input_location
, e1
, e2
, e3
, complain
);
3715 if (e1
== error_mark_node
)
3716 return error_mark_node
;
3718 /* Make sure this doesn't get evaluated first inside one of the
3719 branches of the COND_EXPR. */
3720 if (instance_save_expr
)
3721 e1
= build2 (COMPOUND_EXPR
, TREE_TYPE (e1
),
3722 instance_save_expr
, e1
);
3729 /* Used by the C-common bits. */
3731 build_function_call (location_t
/*loc*/,
3732 tree function
, tree params
)
3734 return cp_build_function_call (function
, params
, tf_warning_or_error
);
3737 /* Used by the C-common bits. */
3739 build_function_call_vec (location_t
/*loc*/, vec
<location_t
> /*arg_loc*/,
3740 tree function
, vec
<tree
, va_gc
> *params
,
3741 vec
<tree
, va_gc
> * /*origtypes*/)
3743 vec
<tree
, va_gc
> *orig_params
= params
;
3744 tree ret
= cp_build_function_call_vec (function
, ¶ms
,
3745 tf_warning_or_error
);
3747 /* cp_build_function_call_vec can reallocate PARAMS by adding
3748 default arguments. That should never happen here. Verify
3750 gcc_assert (params
== orig_params
);
3755 /* Build a function call using a tree list of arguments. */
3758 cp_build_function_call (tree function
, tree params
, tsubst_flags_t complain
)
3760 vec
<tree
, va_gc
> *vec
;
3763 vec
= make_tree_vector ();
3764 for (; params
!= NULL_TREE
; params
= TREE_CHAIN (params
))
3765 vec_safe_push (vec
, TREE_VALUE (params
));
3766 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3767 release_tree_vector (vec
);
3771 /* Build a function call using varargs. */
3774 cp_build_function_call_nary (tree function
, tsubst_flags_t complain
, ...)
3776 vec
<tree
, va_gc
> *vec
;
3780 vec
= make_tree_vector ();
3781 va_start (args
, complain
);
3782 for (t
= va_arg (args
, tree
); t
!= NULL_TREE
; t
= va_arg (args
, tree
))
3783 vec_safe_push (vec
, t
);
3785 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3786 release_tree_vector (vec
);
3790 /* Build a function call using a vector of arguments. PARAMS may be
3791 NULL if there are no parameters. This changes the contents of
3795 cp_build_function_call_vec (tree function
, vec
<tree
, va_gc
> **params
,
3796 tsubst_flags_t complain
)
3798 tree fntype
, fndecl
;
3800 tree original
= function
;
3804 vec
<tree
, va_gc
> *allocated
= NULL
;
3807 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3808 expressions, like those used for ObjC messenger dispatches. */
3809 if (params
!= NULL
&& !vec_safe_is_empty (*params
))
3810 function
= objc_rewrite_function_call (function
, (**params
)[0]);
3812 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3813 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3814 if (TREE_CODE (function
) == NOP_EXPR
3815 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
3816 function
= TREE_OPERAND (function
, 0);
3818 if (TREE_CODE (function
) == FUNCTION_DECL
)
3820 /* If the function is a non-template member function
3821 or a non-template friend, then we need to check the
3824 Note that if overload resolution failed with a single
3825 candidate this function will be used to explicitly diagnose
3826 the failure for the single call expression. The check is
3827 technically redundant since we also would have failed in
3828 add_function_candidate. */
3830 && (complain
& tf_error
)
3831 && !constraints_satisfied_p (function
))
3833 auto_diagnostic_group d
;
3834 error ("cannot call function %qD", function
);
3835 location_t loc
= DECL_SOURCE_LOCATION (function
);
3836 diagnose_constraints (loc
, function
, NULL_TREE
);
3837 return error_mark_node
;
3840 if (!mark_used (function
, complain
) && !(complain
& tf_error
))
3841 return error_mark_node
;
3844 /* Convert anything with function type to a pointer-to-function. */
3845 if (DECL_MAIN_P (function
))
3847 if (complain
& tf_error
)
3848 pedwarn (input_location
, OPT_Wpedantic
,
3849 "ISO C++ forbids calling %<::main%> from within program");
3851 return error_mark_node
;
3853 function
= build_addr_func (function
, complain
);
3859 function
= build_addr_func (function
, complain
);
3862 if (function
== error_mark_node
)
3863 return error_mark_node
;
3865 fntype
= TREE_TYPE (function
);
3867 if (TYPE_PTRMEMFUNC_P (fntype
))
3869 if (complain
& tf_error
)
3870 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3871 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3872 original
, original
);
3873 return error_mark_node
;
3876 is_method
= (TYPE_PTR_P (fntype
)
3877 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
3879 if (!(TYPE_PTRFN_P (fntype
)
3881 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
3883 if (complain
& tf_error
)
3885 if (!flag_diagnostics_show_caret
)
3886 error_at (input_location
,
3887 "%qE cannot be used as a function", original
);
3888 else if (DECL_P (original
))
3889 error_at (input_location
,
3890 "%qD cannot be used as a function", original
);
3892 error_at (input_location
,
3893 "expression cannot be used as a function");
3896 return error_mark_node
;
3899 /* fntype now gets the type of function pointed to. */
3900 fntype
= TREE_TYPE (fntype
);
3901 parm_types
= TYPE_ARG_TYPES (fntype
);
3905 allocated
= make_tree_vector ();
3906 params
= &allocated
;
3909 nargs
= convert_arguments (parm_types
, params
, fndecl
, LOOKUP_NORMAL
,
3912 return error_mark_node
;
3914 argarray
= (*params
)->address ();
3916 /* Check for errors in format strings and inappropriately
3918 bool warned_p
= check_function_arguments (input_location
, fndecl
, fntype
,
3919 nargs
, argarray
, NULL
);
3921 ret
= build_cxx_call (function
, nargs
, argarray
, complain
);
3925 tree c
= extract_call_expr (ret
);
3926 if (TREE_CODE (c
) == CALL_EXPR
)
3927 TREE_NO_WARNING (c
) = 1;
3930 if (allocated
!= NULL
)
3931 release_tree_vector (allocated
);
3936 /* Subroutine of convert_arguments.
3937 Print an error message about a wrong number of arguments. */
3940 error_args_num (location_t loc
, tree fndecl
, bool too_many_p
)
3944 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
3946 if (DECL_NAME (fndecl
) == NULL_TREE
3947 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
3950 ? G_("too many arguments to constructor %q#D")
3951 : G_("too few arguments to constructor %q#D"),
3956 ? G_("too many arguments to member function %q#D")
3957 : G_("too few arguments to member function %q#D"),
3963 ? G_("too many arguments to function %q#D")
3964 : G_("too few arguments to function %q#D"),
3966 if (!DECL_IS_BUILTIN (fndecl
))
3967 inform (DECL_SOURCE_LOCATION (fndecl
), "declared here");
3971 if (c_dialect_objc () && objc_message_selector ())
3974 ? G_("too many arguments to method %q#D")
3975 : G_("too few arguments to method %q#D"),
3976 objc_message_selector ());
3978 error_at (loc
, too_many_p
? G_("too many arguments to function")
3979 : G_("too few arguments to function"));
3983 /* Convert the actual parameter expressions in the list VALUES to the
3984 types in the list TYPELIST. The converted expressions are stored
3985 back in the VALUES vector.
3986 If parmdecls is exhausted, or when an element has NULL as its type,
3987 perform the default conversions.
3989 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3991 This is also where warnings about wrong number of args are generated.
3993 Returns the actual number of arguments processed (which might be less
3994 than the length of the vector), or -1 on error.
3996 In C++, unspecified trailing parameters can be filled in with their
3997 default arguments, if such were specified. Do so here. */
4000 convert_arguments (tree typelist
, vec
<tree
, va_gc
> **values
, tree fndecl
,
4001 int flags
, tsubst_flags_t complain
)
4006 /* Argument passing is always copy-initialization. */
4007 flags
|= LOOKUP_ONLYCONVERTING
;
4009 for (i
= 0, typetail
= typelist
;
4010 i
< vec_safe_length (*values
);
4013 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
4014 tree val
= (**values
)[i
];
4016 if (val
== error_mark_node
|| type
== error_mark_node
)
4019 if (type
== void_type_node
)
4021 if (complain
& tf_error
)
4023 error_args_num (input_location
, fndecl
, /*too_many_p=*/true);
4030 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4031 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4032 if (TREE_CODE (val
) == NOP_EXPR
4033 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
4034 && (type
== 0 || !TYPE_REF_P (type
)))
4035 val
= TREE_OPERAND (val
, 0);
4037 if (type
== 0 || !TYPE_REF_P (type
))
4039 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
4040 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
4041 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
4042 val
= decay_conversion (val
, complain
);
4045 if (val
== error_mark_node
)
4050 /* Formal parm type is specified by a function prototype. */
4053 if (!COMPLETE_TYPE_P (complete_type (type
)))
4055 if (complain
& tf_error
)
4057 location_t loc
= EXPR_LOC_OR_LOC (val
, input_location
);
4060 auto_diagnostic_group d
;
4062 "parameter %P of %qD has incomplete type %qT",
4064 inform (get_fndecl_argument_location (fndecl
, i
),
4068 error_at (loc
, "parameter %P has incomplete type %qT", i
,
4071 parmval
= error_mark_node
;
4075 parmval
= convert_for_initialization
4076 (NULL_TREE
, type
, val
, flags
,
4077 ICR_ARGPASS
, fndecl
, i
, complain
);
4078 parmval
= convert_for_arg_passing (type
, parmval
, complain
);
4081 if (parmval
== error_mark_node
)
4084 (**values
)[i
] = parmval
;
4088 if (fndecl
&& magic_varargs_p (fndecl
))
4089 /* Don't do ellipsis conversion for __built_in_constant_p
4090 as this will result in spurious errors for non-trivial
4092 val
= require_complete_type_sfinae (val
, complain
);
4094 val
= convert_arg_to_ellipsis (val
, complain
);
4096 (**values
)[i
] = val
;
4100 typetail
= TREE_CHAIN (typetail
);
4103 if (typetail
!= 0 && typetail
!= void_list_node
)
4105 /* See if there are default arguments that can be used. Because
4106 we hold default arguments in the FUNCTION_TYPE (which is so
4107 wrong), we can see default parameters here from deduced
4108 contexts (and via typeof) for indirect function calls.
4109 Fortunately we know whether we have a function decl to
4110 provide default arguments in a language conformant
4112 if (fndecl
&& TREE_PURPOSE (typetail
)
4113 && TREE_CODE (TREE_PURPOSE (typetail
)) != DEFAULT_ARG
)
4115 for (; typetail
!= void_list_node
; ++i
)
4117 /* After DR777, with explicit template args we can end up with a
4118 default argument followed by no default argument. */
4119 if (!TREE_PURPOSE (typetail
))
4122 = convert_default_arg (TREE_VALUE (typetail
),
4123 TREE_PURPOSE (typetail
),
4124 fndecl
, i
, complain
);
4126 if (parmval
== error_mark_node
)
4129 vec_safe_push (*values
, parmval
);
4130 typetail
= TREE_CHAIN (typetail
);
4131 /* ends with `...'. */
4132 if (typetail
== NULL_TREE
)
4137 if (typetail
&& typetail
!= void_list_node
)
4139 if (complain
& tf_error
)
4140 error_args_num (input_location
, fndecl
, /*too_many_p=*/false);
4148 /* Build a binary-operation expression, after performing default
4149 conversions on the operands. CODE is the kind of expression to
4150 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4151 are the tree codes which correspond to ARG1 and ARG2 when issuing
4152 warnings about possibly misplaced parentheses. They may differ
4153 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4154 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4155 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4156 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4157 ARG2_CODE as ERROR_MARK. */
4160 build_x_binary_op (const op_location_t
&loc
, enum tree_code code
, tree arg1
,
4161 enum tree_code arg1_code
, tree arg2
,
4162 enum tree_code arg2_code
, tree
*overload_p
,
4163 tsubst_flags_t complain
)
4168 tree overload
= NULL_TREE
;
4173 if (processing_template_decl
)
4175 if (type_dependent_expression_p (arg1
)
4176 || type_dependent_expression_p (arg2
))
4178 expr
= build_min_nt_loc (loc
, code
, arg1
, arg2
);
4179 maybe_save_operator_binding (expr
);
4182 arg1
= build_non_dependent_expr (arg1
);
4183 arg2
= build_non_dependent_expr (arg2
);
4186 if (code
== DOTSTAR_EXPR
)
4187 expr
= build_m_component_ref (arg1
, arg2
, complain
);
4189 expr
= build_new_op (loc
, code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
,
4190 &overload
, complain
);
4192 if (overload_p
!= NULL
)
4193 *overload_p
= overload
;
4195 /* Check for cases such as x+y<<z which users are likely to
4196 misinterpret. But don't warn about obj << x + y, since that is a
4197 common idiom for I/O. */
4198 if (warn_parentheses
4199 && (complain
& tf_warning
)
4200 && !processing_template_decl
4201 && !error_operand_p (arg1
)
4202 && !error_operand_p (arg2
)
4203 && (code
!= LSHIFT_EXPR
4204 || !CLASS_TYPE_P (TREE_TYPE (arg1
))))
4205 warn_about_parentheses (loc
, code
, arg1_code
, orig_arg1
,
4206 arg2_code
, orig_arg2
);
4208 if (processing_template_decl
&& expr
!= error_mark_node
)
4210 if (overload
!= NULL_TREE
)
4211 return (build_min_non_dep_op_overload
4212 (code
, expr
, overload
, orig_arg1
, orig_arg2
));
4214 return build_min_non_dep (code
, expr
, orig_arg1
, orig_arg2
);
4220 /* Build and return an ARRAY_REF expression. */
4223 build_x_array_ref (location_t loc
, tree arg1
, tree arg2
,
4224 tsubst_flags_t complain
)
4226 tree orig_arg1
= arg1
;
4227 tree orig_arg2
= arg2
;
4229 tree overload
= NULL_TREE
;
4231 if (processing_template_decl
)
4233 if (type_dependent_expression_p (arg1
)
4234 || type_dependent_expression_p (arg2
))
4235 return build_min_nt_loc (loc
, ARRAY_REF
, arg1
, arg2
,
4236 NULL_TREE
, NULL_TREE
);
4237 arg1
= build_non_dependent_expr (arg1
);
4238 arg2
= build_non_dependent_expr (arg2
);
4241 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, arg1
, arg2
,
4242 NULL_TREE
, &overload
, complain
);
4244 if (processing_template_decl
&& expr
!= error_mark_node
)
4246 if (overload
!= NULL_TREE
)
4247 return (build_min_non_dep_op_overload
4248 (ARRAY_REF
, expr
, overload
, orig_arg1
, orig_arg2
));
4250 return build_min_non_dep (ARRAY_REF
, expr
, orig_arg1
, orig_arg2
,
4251 NULL_TREE
, NULL_TREE
);
4256 /* Return whether OP is an expression of enum type cast to integer
4257 type. In C++ even unsigned enum types are cast to signed integer
4258 types. We do not want to issue warnings about comparisons between
4259 signed and unsigned types when one of the types is an enum type.
4260 Those warnings are always false positives in practice. */
4263 enum_cast_to_int (tree op
)
4265 if (CONVERT_EXPR_P (op
)
4266 && TREE_TYPE (op
) == integer_type_node
4267 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == ENUMERAL_TYPE
4268 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op
, 0))))
4271 /* The cast may have been pushed into a COND_EXPR. */
4272 if (TREE_CODE (op
) == COND_EXPR
)
4273 return (enum_cast_to_int (TREE_OPERAND (op
, 1))
4274 || enum_cast_to_int (TREE_OPERAND (op
, 2)));
4279 /* For the c-common bits. */
4281 build_binary_op (location_t location
, enum tree_code code
, tree op0
, tree op1
,
4284 return cp_build_binary_op (location
, code
, op0
, op1
, tf_warning_or_error
);
4287 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4288 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4291 build_vec_cmp (tree_code code
, tree type
,
4292 tree arg0
, tree arg1
)
4294 tree zero_vec
= build_zero_cst (type
);
4295 tree minus_one_vec
= build_minus_one_cst (type
);
4296 tree cmp_type
= build_same_sized_truth_vector_type(type
);
4297 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
4298 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
4301 /* Possibly warn about an address never being NULL. */
4304 warn_for_null_address (location_t location
, tree op
, tsubst_flags_t complain
)
4307 || (complain
& tf_warning
) == 0
4308 || c_inhibit_evaluation_warnings
!= 0
4309 || TREE_NO_WARNING (op
))
4312 tree cop
= fold_non_dependent_expr (op
, complain
);
4314 if (TREE_CODE (cop
) == ADDR_EXPR
4315 && decl_with_nonnull_addr_p (TREE_OPERAND (cop
, 0))
4316 && !TREE_NO_WARNING (cop
))
4317 warning_at (location
, OPT_Waddress
, "the address of %qD will never "
4318 "be NULL", TREE_OPERAND (cop
, 0));
4320 if (CONVERT_EXPR_P (op
)
4321 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op
, 0))))
4324 STRIP_NOPS (inner_op
);
4326 if (DECL_P (inner_op
))
4327 warning_at (location
, OPT_Waddress
,
4328 "the compiler can assume that the address of "
4329 "%qD will never be NULL", inner_op
);
4333 /* Build a binary-operation expression without default conversions.
4334 CODE is the kind of expression to build.
4335 LOCATION is the location_t of the operator in the source code.
4336 This function differs from `build' in several ways:
4337 the data type of the result is computed and recorded in it,
4338 warnings are generated if arg data types are invalid,
4339 special handling for addition and subtraction of pointers is known,
4340 and some optimization is done (operations on narrow ints
4341 are done in the narrower type when that gives the same result).
4342 Constant folding is also done before the result is returned.
4344 Note that the operands will never have enumeral types
4345 because either they have just had the default conversions performed
4346 or they have both just been converted to some other type in which
4347 the arithmetic is to be done.
4349 C++: must do special pointer arithmetic when implementing
4350 multiple inheritance, and deal with pointer to member functions. */
4353 cp_build_binary_op (const op_location_t
&location
,
4354 enum tree_code code
, tree orig_op0
, tree orig_op1
,
4355 tsubst_flags_t complain
)
4358 enum tree_code code0
, code1
;
4360 const char *invalid_op_diag
;
4362 /* Expression code to give to the expression when it is built.
4363 Normally this is CODE, which is what the caller asked for,
4364 but in some special cases we change it. */
4365 enum tree_code resultcode
= code
;
4367 /* Data type in which the computation is to be performed.
4368 In the simplest cases this is the common type of the arguments. */
4369 tree result_type
= NULL_TREE
;
4371 /* Nonzero means operands have already been type-converted
4372 in whatever way is necessary.
4373 Zero means they need to be converted to RESULT_TYPE. */
4376 /* Nonzero means create the expression with this type, rather than
4378 tree build_type
= 0;
4380 /* Nonzero means after finally constructing the expression
4381 convert it to this type. */
4382 tree final_type
= 0;
4384 tree result
, result_ovl
;
4386 /* Nonzero if this is an operation like MIN or MAX which can
4387 safely be computed in short if both args are promoted shorts.
4388 Also implies COMMON.
4389 -1 indicates a bitwise operation; this makes a difference
4390 in the exact conditions for when it is safe to do the operation
4391 in a narrower mode. */
4394 /* Nonzero if this is a comparison operation;
4395 if both args are promoted shorts, compare the original shorts.
4396 Also implies COMMON. */
4397 int short_compare
= 0;
4399 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4402 /* True if both operands have arithmetic type. */
4403 bool arithmetic_types_p
;
4405 /* Apply default conversions. */
4409 /* Remember whether we're doing / or %. */
4410 bool doing_div_or_mod
= false;
4412 /* Remember whether we're doing << or >>. */
4413 bool doing_shift
= false;
4415 /* Tree holding instrumentation expression. */
4416 tree instrument_expr
= NULL_TREE
;
4418 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
4419 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
4420 || code
== TRUTH_XOR_EXPR
)
4422 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
4423 op0
= decay_conversion (op0
, complain
);
4424 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
4425 op1
= decay_conversion (op1
, complain
);
4429 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
4430 op0
= cp_default_conversion (op0
, complain
);
4431 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
4432 op1
= cp_default_conversion (op1
, complain
);
4435 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4436 STRIP_TYPE_NOPS (op0
);
4437 STRIP_TYPE_NOPS (op1
);
4439 /* DTRT if one side is an overloaded function, but complain about it. */
4440 if (type_unknown_p (op0
))
4442 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
4443 if (t
!= error_mark_node
)
4445 if (complain
& tf_error
)
4446 permerror (input_location
, "assuming cast to type %qT from overloaded function",
4451 if (type_unknown_p (op1
))
4453 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
4454 if (t
!= error_mark_node
)
4456 if (complain
& tf_error
)
4457 permerror (input_location
, "assuming cast to type %qT from overloaded function",
4463 type0
= TREE_TYPE (op0
);
4464 type1
= TREE_TYPE (op1
);
4466 /* The expression codes of the data types of the arguments tell us
4467 whether the arguments are integers, floating, pointers, etc. */
4468 code0
= TREE_CODE (type0
);
4469 code1
= TREE_CODE (type1
);
4471 /* If an error was already reported for one of the arguments,
4472 avoid reporting another error. */
4473 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
4474 return error_mark_node
;
4476 if ((invalid_op_diag
4477 = targetm
.invalid_binary_op (code
, type0
, type1
)))
4479 if (complain
& tf_error
)
4480 error (invalid_op_diag
);
4481 return error_mark_node
;
4484 /* Issue warnings about peculiar, but valid, uses of NULL. */
4485 if ((null_node_p (orig_op0
) || null_node_p (orig_op1
))
4486 /* It's reasonable to use pointer values as operands of &&
4487 and ||, so NULL is no exception. */
4488 && code
!= TRUTH_ANDIF_EXPR
&& code
!= TRUTH_ORIF_EXPR
4489 && ( /* Both are NULL (or 0) and the operation was not a
4490 comparison or a pointer subtraction. */
4491 (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)
4492 && code
!= EQ_EXPR
&& code
!= NE_EXPR
&& code
!= MINUS_EXPR
)
4493 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4494 || (!null_ptr_cst_p (orig_op0
)
4495 && !TYPE_PTR_OR_PTRMEM_P (type0
))
4496 || (!null_ptr_cst_p (orig_op1
)
4497 && !TYPE_PTR_OR_PTRMEM_P (type1
)))
4498 && (complain
& tf_warning
))
4501 expansion_point_location_if_in_system_header (input_location
);
4503 warning_at (loc
, OPT_Wpointer_arith
, "NULL used in arithmetic");
4506 /* In case when one of the operands of the binary operation is
4507 a vector and another is a scalar -- convert scalar to vector. */
4508 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
4510 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
4511 complain
& tf_error
);
4513 switch (convert_flag
)
4516 return error_mark_node
;
4519 op0
= convert (TREE_TYPE (type1
), op0
);
4520 op0
= save_expr (op0
);
4521 op0
= build_vector_from_val (type1
, op0
);
4522 type0
= TREE_TYPE (op0
);
4523 code0
= TREE_CODE (type0
);
4529 op1
= convert (TREE_TYPE (type0
), op1
);
4530 op1
= save_expr (op1
);
4531 op1
= build_vector_from_val (type0
, op1
);
4532 type1
= TREE_TYPE (op1
);
4533 code1
= TREE_CODE (type1
);
4545 /* Subtraction of two similar pointers.
4546 We must subtract them as integers, then divide by object size. */
4547 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
4548 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4551 result
= pointer_diff (location
, op0
, op1
,
4552 common_pointer_type (type0
, type1
), complain
,
4554 if (instrument_expr
!= NULL
)
4555 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
4556 instrument_expr
, result
);
4560 /* In all other cases except pointer - int, the usual arithmetic
4562 else if (!(code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
))
4567 /* The pointer - int case is just like pointer + int; fall
4571 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
4572 && (code0
== INTEGER_TYPE
|| code1
== INTEGER_TYPE
))
4576 ptr_operand
= ((code0
== POINTER_TYPE
) ? op0
: op1
);
4577 int_operand
= ((code0
== INTEGER_TYPE
) ? op0
: op1
);
4578 if (processing_template_decl
)
4580 result_type
= TREE_TYPE (ptr_operand
);
4583 return cp_pointer_int_sum (location
, code
,
4595 case TRUNC_DIV_EXPR
:
4597 case FLOOR_DIV_EXPR
:
4598 case ROUND_DIV_EXPR
:
4599 case EXACT_DIV_EXPR
:
4600 if (TREE_CODE (op0
) == SIZEOF_EXPR
&& TREE_CODE (op1
) == SIZEOF_EXPR
)
4602 tree type0
= TREE_OPERAND (op0
, 0);
4603 tree type1
= TREE_OPERAND (op1
, 0);
4604 tree first_arg
= type0
;
4605 if (!TYPE_P (type0
))
4606 type0
= TREE_TYPE (type0
);
4607 if (!TYPE_P (type1
))
4608 type1
= TREE_TYPE (type1
);
4609 if (INDIRECT_TYPE_P (type0
) && same_type_p (TREE_TYPE (type0
), type1
))
4611 STRIP_ANY_LOCATION_WRAPPER (first_arg
);
4612 if (!(TREE_CODE (first_arg
) == PARM_DECL
4613 && DECL_ARRAY_PARAMETER_P (first_arg
)
4614 && warn_sizeof_array_argument
)
4615 && (complain
& tf_warning
))
4617 auto_diagnostic_group d
;
4618 if (warning_at (location
, OPT_Wsizeof_pointer_div
,
4619 "division %<sizeof (%T) / sizeof (%T)%> does "
4620 "not compute the number of array elements",
4622 if (DECL_P (first_arg
))
4623 inform (DECL_SOURCE_LOCATION (first_arg
),
4624 "first %<sizeof%> operand was declared here");
4629 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4630 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
4631 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4632 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
4634 enum tree_code tcode0
= code0
, tcode1
= code1
;
4635 tree cop1
= fold_non_dependent_expr (op1
, complain
);
4636 doing_div_or_mod
= true;
4637 warn_for_div_by_zero (location
, cop1
);
4639 if (tcode0
== COMPLEX_TYPE
|| tcode0
== VECTOR_TYPE
)
4640 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
4641 if (tcode1
== COMPLEX_TYPE
|| tcode1
== VECTOR_TYPE
)
4642 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
4644 if (!(tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
))
4645 resultcode
= RDIV_EXPR
;
4648 /* When dividing two signed integers, we have to promote to int.
4649 unless we divide by a constant != -1. Note that default
4650 conversion will have been performed on the operands at this
4651 point, so we have to dig out the original type to find out if
4653 tree stripped_op1
= tree_strip_any_location_wrapper (op1
);
4654 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4655 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4656 || (TREE_CODE (stripped_op1
) == INTEGER_CST
4657 && ! integer_all_onesp (stripped_op1
)));
4667 if ((code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4668 || (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4669 && !VECTOR_FLOAT_TYPE_P (type0
)
4670 && !VECTOR_FLOAT_TYPE_P (type1
)))
4674 case TRUNC_MOD_EXPR
:
4675 case FLOOR_MOD_EXPR
:
4677 tree cop1
= fold_non_dependent_expr (op1
, complain
);
4678 doing_div_or_mod
= true;
4679 warn_for_div_by_zero (location
, cop1
);
4682 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4683 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4684 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
4686 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4688 /* Although it would be tempting to shorten always here, that loses
4689 on some targets, since the modulo instruction is undefined if the
4690 quotient can't be represented in the computation mode. We shorten
4691 only if unsigned or if dividing by something we know != -1. */
4692 tree stripped_op1
= tree_strip_any_location_wrapper (op1
);
4693 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4694 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4695 || (TREE_CODE (stripped_op1
) == INTEGER_CST
4696 && ! integer_all_onesp (stripped_op1
)));
4701 case TRUTH_ANDIF_EXPR
:
4702 case TRUTH_ORIF_EXPR
:
4703 case TRUTH_AND_EXPR
:
4705 if (!VECTOR_TYPE_P (type0
) && VECTOR_TYPE_P (type1
))
4707 if (!COMPARISON_CLASS_P (op1
))
4708 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4709 build_zero_cst (type1
), complain
);
4710 if (code
== TRUTH_ANDIF_EXPR
)
4712 tree z
= build_zero_cst (TREE_TYPE (op1
));
4713 return build_conditional_expr (location
, op0
, op1
, z
, complain
);
4715 else if (code
== TRUTH_ORIF_EXPR
)
4717 tree m1
= build_all_ones_cst (TREE_TYPE (op1
));
4718 return build_conditional_expr (location
, op0
, m1
, op1
, complain
);
4723 if (VECTOR_TYPE_P (type0
))
4725 if (!COMPARISON_CLASS_P (op0
))
4726 op0
= cp_build_binary_op (EXPR_LOCATION (op0
), NE_EXPR
, op0
,
4727 build_zero_cst (type0
), complain
);
4728 if (!VECTOR_TYPE_P (type1
))
4730 tree m1
= build_all_ones_cst (TREE_TYPE (op0
));
4731 tree z
= build_zero_cst (TREE_TYPE (op0
));
4732 op1
= build_conditional_expr (location
, op1
, m1
, z
, complain
);
4734 else if (!COMPARISON_CLASS_P (op1
))
4735 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4736 build_zero_cst (type1
), complain
);
4738 if (code
== TRUTH_ANDIF_EXPR
)
4739 code
= BIT_AND_EXPR
;
4740 else if (code
== TRUTH_ORIF_EXPR
)
4741 code
= BIT_IOR_EXPR
;
4745 return cp_build_binary_op (location
, code
, op0
, op1
, complain
);
4748 result_type
= boolean_type_node
;
4751 /* Shift operations: result has same type as first operand;
4752 always convert second operand to int.
4753 Also set SHORT_SHIFT if shifting rightward. */
4756 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4757 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4759 result_type
= type0
;
4762 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4763 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4764 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4765 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
4766 TYPE_VECTOR_SUBPARTS (type1
)))
4768 result_type
= type0
;
4771 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4773 tree const_op1
= fold_non_dependent_expr (op1
, complain
);
4774 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4776 result_type
= type0
;
4778 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4780 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4782 if ((complain
& tf_warning
)
4783 && c_inhibit_evaluation_warnings
== 0)
4784 warning (OPT_Wshift_count_negative
,
4785 "right shift count is negative");
4789 if (compare_tree_int (const_op1
, TYPE_PRECISION (type0
)) >= 0
4790 && (complain
& tf_warning
)
4791 && c_inhibit_evaluation_warnings
== 0)
4792 warning (OPT_Wshift_count_overflow
,
4793 "right shift count >= width of type");
4796 /* Avoid converting op1 to result_type later. */
4802 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4803 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4805 result_type
= type0
;
4808 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4809 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4810 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4811 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
4812 TYPE_VECTOR_SUBPARTS (type1
)))
4814 result_type
= type0
;
4817 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4819 tree const_op0
= fold_non_dependent_expr (op0
, complain
);
4820 if (TREE_CODE (const_op0
) != INTEGER_CST
)
4822 tree const_op1
= fold_non_dependent_expr (op1
, complain
);
4823 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4825 result_type
= type0
;
4827 if (TREE_CODE (const_op0
) == INTEGER_CST
4828 && tree_int_cst_sgn (const_op0
) < 0
4829 && (complain
& tf_warning
)
4830 && c_inhibit_evaluation_warnings
== 0)
4831 warning (OPT_Wshift_negative_value
,
4832 "left shift of negative value");
4833 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4835 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4837 if ((complain
& tf_warning
)
4838 && c_inhibit_evaluation_warnings
== 0)
4839 warning (OPT_Wshift_count_negative
,
4840 "left shift count is negative");
4842 else if (compare_tree_int (const_op1
,
4843 TYPE_PRECISION (type0
)) >= 0)
4845 if ((complain
& tf_warning
)
4846 && c_inhibit_evaluation_warnings
== 0)
4847 warning (OPT_Wshift_count_overflow
,
4848 "left shift count >= width of type");
4850 else if (TREE_CODE (const_op0
) == INTEGER_CST
4851 && (complain
& tf_warning
))
4852 maybe_warn_shift_overflow (location
, const_op0
, const_op1
);
4854 /* Avoid converting op1 to result_type later. */
4861 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4863 result_type
= type0
;
4864 if (TREE_CODE (op1
) == INTEGER_CST
)
4866 if (tree_int_cst_lt (op1
, integer_zero_node
))
4868 if (complain
& tf_warning
)
4869 warning (0, (code
== LROTATE_EXPR
)
4870 ? G_("left rotate count is negative")
4871 : G_("right rotate count is negative"));
4873 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
4875 if (complain
& tf_warning
)
4876 warning (0, (code
== LROTATE_EXPR
)
4877 ? G_("left rotate count >= width of type")
4878 : G_("right rotate count >= width of type"));
4881 /* Convert the shift-count to an integer, regardless of
4882 size of value being shifted. */
4883 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4884 op1
= cp_convert (integer_type_node
, op1
, complain
);
4890 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4891 goto vector_compare
;
4892 if ((complain
& tf_warning
)
4893 && c_inhibit_evaluation_warnings
== 0
4894 && (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
)))
4895 warning (OPT_Wfloat_equal
,
4896 "comparing floating point with == or != is unsafe");
4897 if (complain
& tf_warning
)
4899 tree stripped_orig_op0
= tree_strip_any_location_wrapper (orig_op0
);
4900 tree stripped_orig_op1
= tree_strip_any_location_wrapper (orig_op1
);
4901 if ((TREE_CODE (stripped_orig_op0
) == STRING_CST
4902 && !integer_zerop (cp_fully_fold (op1
)))
4903 || (TREE_CODE (stripped_orig_op1
) == STRING_CST
4904 && !integer_zerop (cp_fully_fold (op0
))))
4905 warning (OPT_Waddress
, "comparison with string literal results "
4906 "in unspecified behavior");
4909 build_type
= boolean_type_node
;
4910 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4911 || code0
== COMPLEX_TYPE
|| code0
== ENUMERAL_TYPE
)
4912 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4913 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
))
4915 else if (((code0
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type0
))
4916 && null_ptr_cst_p (orig_op1
))
4917 /* Handle, eg, (void*)0 (c++/43906), and more. */
4918 || (code0
== POINTER_TYPE
4919 && TYPE_PTR_P (type1
) && integer_zerop (op1
)))
4921 if (TYPE_PTR_P (type1
))
4922 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4923 CPO_COMPARISON
, complain
);
4925 result_type
= type0
;
4927 if (char_type_p (TREE_TYPE (orig_op1
)))
4929 auto_diagnostic_group d
;
4930 if (warning (OPT_Wpointer_compare
,
4931 "comparison between pointer and zero character "
4933 inform (input_location
,
4934 "did you mean to dereference the pointer?");
4936 warn_for_null_address (location
, op0
, complain
);
4938 else if (((code1
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type1
))
4939 && null_ptr_cst_p (orig_op0
))
4940 /* Handle, eg, (void*)0 (c++/43906), and more. */
4941 || (code1
== POINTER_TYPE
4942 && TYPE_PTR_P (type0
) && integer_zerop (op0
)))
4944 if (TYPE_PTR_P (type0
))
4945 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4946 CPO_COMPARISON
, complain
);
4948 result_type
= type1
;
4950 if (char_type_p (TREE_TYPE (orig_op0
)))
4952 auto_diagnostic_group d
;
4953 if (warning (OPT_Wpointer_compare
,
4954 "comparison between pointer and zero character "
4956 inform (input_location
,
4957 "did you mean to dereference the pointer?");
4959 warn_for_null_address (location
, op1
, complain
);
4961 else if ((code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4962 || (TYPE_PTRDATAMEM_P (type0
) && TYPE_PTRDATAMEM_P (type1
)))
4963 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4964 CPO_COMPARISON
, complain
);
4965 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
4966 /* One of the operands must be of nullptr_t type. */
4967 result_type
= TREE_TYPE (nullptr_node
);
4968 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4970 result_type
= type0
;
4971 if (complain
& tf_error
)
4972 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4974 return error_mark_node
;
4976 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4978 result_type
= type1
;
4979 if (complain
& tf_error
)
4980 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4982 return error_mark_node
;
4984 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (orig_op1
))
4986 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4987 == ptrmemfunc_vbit_in_delta
)
4989 tree pfn0
, delta0
, e1
, e2
;
4991 if (TREE_SIDE_EFFECTS (op0
))
4992 op0
= cp_save_expr (op0
);
4994 pfn0
= pfn_from_ptrmemfunc (op0
);
4995 delta0
= delta_from_ptrmemfunc (op0
);
4996 e1
= cp_build_binary_op (location
,
4999 build_zero_cst (TREE_TYPE (pfn0
)),
5001 e2
= cp_build_binary_op (location
,
5007 if (complain
& tf_warning
)
5008 maybe_warn_zero_as_null_pointer_constant (op1
, input_location
);
5010 e2
= cp_build_binary_op (location
,
5011 EQ_EXPR
, e2
, integer_zero_node
,
5013 op0
= cp_build_binary_op (location
,
5014 TRUTH_ANDIF_EXPR
, e1
, e2
,
5016 op1
= cp_convert (TREE_TYPE (op0
), integer_one_node
, complain
);
5020 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
5021 op1
= cp_convert (TREE_TYPE (op0
), op1
, complain
);
5023 result_type
= TREE_TYPE (op0
);
5025 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (orig_op0
))
5026 return cp_build_binary_op (location
, code
, op1
, op0
, complain
);
5027 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
))
5030 /* E will be the final comparison. */
5032 /* E1 and E2 are for scratch. */
5040 type
= composite_pointer_type (type0
, type1
, op0
, op1
,
5041 CPO_COMPARISON
, complain
);
5043 if (!same_type_p (TREE_TYPE (op0
), type
))
5044 op0
= cp_convert_and_check (type
, op0
, complain
);
5045 if (!same_type_p (TREE_TYPE (op1
), type
))
5046 op1
= cp_convert_and_check (type
, op1
, complain
);
5048 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5049 return error_mark_node
;
5051 if (TREE_SIDE_EFFECTS (op0
))
5052 op0
= save_expr (op0
);
5053 if (TREE_SIDE_EFFECTS (op1
))
5054 op1
= save_expr (op1
);
5056 pfn0
= pfn_from_ptrmemfunc (op0
);
5057 pfn0
= cp_fully_fold (pfn0
);
5058 /* Avoid -Waddress warnings (c++/64877). */
5059 if (TREE_CODE (pfn0
) == ADDR_EXPR
)
5060 TREE_NO_WARNING (pfn0
) = 1;
5061 pfn1
= pfn_from_ptrmemfunc (op1
);
5062 pfn1
= cp_fully_fold (pfn1
);
5063 delta0
= delta_from_ptrmemfunc (op0
);
5064 delta1
= delta_from_ptrmemfunc (op1
);
5065 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5066 == ptrmemfunc_vbit_in_delta
)
5071 && ((op0.delta == op1.delta)
5072 || (!op0.pfn && op0.delta & 1 == 0
5073 && op1.delta & 1 == 0))
5075 The reason for the `!op0.pfn' bit is that a NULL
5076 pointer-to-member is any member with a zero PFN and
5077 LSB of the DELTA field is 0. */
5079 e1
= cp_build_binary_op (location
, BIT_AND_EXPR
,
5083 e1
= cp_build_binary_op (location
,
5084 EQ_EXPR
, e1
, integer_zero_node
,
5086 e2
= cp_build_binary_op (location
, BIT_AND_EXPR
,
5090 e2
= cp_build_binary_op (location
,
5091 EQ_EXPR
, e2
, integer_zero_node
,
5093 e1
= cp_build_binary_op (location
,
5094 TRUTH_ANDIF_EXPR
, e2
, e1
,
5096 e2
= cp_build_binary_op (location
, EQ_EXPR
,
5098 build_zero_cst (TREE_TYPE (pfn0
)),
5100 e2
= cp_build_binary_op (location
,
5101 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
5102 e1
= cp_build_binary_op (location
,
5103 EQ_EXPR
, delta0
, delta1
, complain
);
5104 e1
= cp_build_binary_op (location
,
5105 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
5112 && (!op0.pfn || op0.delta == op1.delta))
5114 The reason for the `!op0.pfn' bit is that a NULL
5115 pointer-to-member is any member with a zero PFN; the
5116 DELTA field is unspecified. */
5118 e1
= cp_build_binary_op (location
,
5119 EQ_EXPR
, delta0
, delta1
, complain
);
5120 e2
= cp_build_binary_op (location
,
5123 build_zero_cst (TREE_TYPE (pfn0
)),
5125 e1
= cp_build_binary_op (location
,
5126 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
5128 e2
= build2 (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
5129 e
= cp_build_binary_op (location
,
5130 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
5131 if (code
== EQ_EXPR
)
5133 return cp_build_binary_op (location
,
5134 EQ_EXPR
, e
, integer_zero_node
, complain
);
5138 gcc_assert (!TYPE_PTRMEMFUNC_P (type0
)
5139 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
),
5141 gcc_assert (!TYPE_PTRMEMFUNC_P (type1
)
5142 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
),
5150 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
5151 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
5153 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5154 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
5155 CPO_COMPARISON
, complain
);
5162 if (TREE_CODE (orig_op0
) == STRING_CST
5163 || TREE_CODE (orig_op1
) == STRING_CST
)
5165 if (complain
& tf_warning
)
5166 warning (OPT_Waddress
, "comparison with string literal results "
5167 "in unspecified behavior");
5170 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
5174 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
5176 && !vector_types_compatible_elements_p (type0
, type1
))
5178 if (complain
& tf_error
)
5180 error_at (location
, "comparing vectors with different "
5182 inform (location
, "operand types are %qT and %qT",
5185 return error_mark_node
;
5188 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0
),
5189 TYPE_VECTOR_SUBPARTS (type1
)))
5191 if (complain
& tf_error
)
5193 error_at (location
, "comparing vectors with different "
5194 "number of elements");
5195 inform (location
, "operand types are %qT and %qT",
5198 return error_mark_node
;
5201 /* It's not precisely specified how the usual arithmetic
5202 conversions apply to the vector types. Here, we use
5203 the unsigned type if one of the operands is signed and
5204 the other one is unsigned. */
5205 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
5207 if (!TYPE_UNSIGNED (type0
))
5208 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
5210 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
5211 warning_at (location
, OPT_Wsign_compare
, "comparison between "
5212 "types %qT and %qT", type0
, type1
);
5215 /* Always construct signed integer vector type. */
5216 intt
= c_common_type_for_size
5217 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0
))), 0);
5220 if (complain
& tf_error
)
5221 error_at (location
, "could not find an integer type "
5222 "of the same size as %qT", TREE_TYPE (type0
));
5223 return error_mark_node
;
5225 result_type
= build_opaque_vector_type (intt
,
5226 TYPE_VECTOR_SUBPARTS (type0
));
5228 return build_vec_cmp (resultcode
, result_type
, op0
, op1
);
5230 build_type
= boolean_type_node
;
5231 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
5232 || code0
== ENUMERAL_TYPE
)
5233 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5234 || code1
== ENUMERAL_TYPE
))
5236 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5237 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
5238 CPO_COMPARISON
, complain
);
5239 else if (code0
== POINTER_TYPE
&& null_ptr_cst_p (orig_op1
))
5241 result_type
= type0
;
5242 if (extra_warnings
&& (complain
& tf_warning
))
5243 warning (OPT_Wextra
,
5244 "ordered comparison of pointer with integer zero");
5246 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (orig_op0
))
5248 result_type
= type1
;
5249 if (extra_warnings
&& (complain
& tf_warning
))
5250 warning (OPT_Wextra
,
5251 "ordered comparison of pointer with integer zero");
5253 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
5254 /* One of the operands must be of nullptr_t type. */
5255 result_type
= TREE_TYPE (nullptr_node
);
5256 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5258 result_type
= type0
;
5259 if (complain
& tf_error
)
5260 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
5262 return error_mark_node
;
5264 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
5266 result_type
= type1
;
5267 if (complain
& tf_error
)
5268 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
5270 return error_mark_node
;
5273 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
5274 && !processing_template_decl
5275 && sanitize_flags_p (SANITIZE_POINTER_COMPARE
))
5277 op0
= save_expr (op0
);
5278 op1
= save_expr (op1
);
5280 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE
);
5281 instrument_expr
= build_call_expr_loc (location
, tt
, 2, op0
, op1
);
5286 case UNORDERED_EXPR
:
5293 build_type
= integer_type_node
;
5294 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
5296 if (complain
& tf_error
)
5297 error ("unordered comparison on non-floating point argument");
5298 return error_mark_node
;
5307 if (((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
5308 || code0
== ENUMERAL_TYPE
)
5309 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5310 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
)))
5311 arithmetic_types_p
= 1;
5314 arithmetic_types_p
= 0;
5315 /* Vector arithmetic is only allowed when both sides are vectors. */
5316 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
5318 if (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
5319 || !vector_types_compatible_elements_p (type0
, type1
))
5321 if (complain
& tf_error
)
5323 /* "location" already embeds the locations of the
5324 operands, so we don't need to add them separately
5326 rich_location
richloc (line_table
, location
);
5327 binary_op_error (&richloc
, code
, type0
, type1
);
5329 return error_mark_node
;
5331 arithmetic_types_p
= 1;
5334 /* Determine the RESULT_TYPE, if it is not already known. */
5336 && arithmetic_types_p
5337 && (shorten
|| common
|| short_compare
))
5339 result_type
= cp_common_type (type0
, type1
);
5340 if (complain
& tf_warning
)
5341 do_warn_double_promotion (result_type
, type0
, type1
,
5342 "implicit conversion from %qH to %qI "
5343 "to match other operand of binary "
5350 if (complain
& tf_error
)
5352 binary_op_rich_location
richloc (location
,
5353 orig_op0
, orig_op1
, true);
5355 "invalid operands of types %qT and %qT to binary %qO",
5356 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
5358 return error_mark_node
;
5361 /* If we're in a template, the only thing we need to know is the
5363 if (processing_template_decl
)
5365 /* Since the middle-end checks the type when doing a build2, we
5366 need to build the tree in pieces. This built tree will never
5367 get out of the front-end as we replace it when instantiating
5369 tree tmp
= build2 (resultcode
,
5370 build_type
? build_type
: result_type
,
5372 TREE_OPERAND (tmp
, 0) = op0
;
5376 /* Remember the original type; RESULT_TYPE might be changed later on
5377 by shorten_binary_op. */
5378 tree orig_type
= result_type
;
5380 if (arithmetic_types_p
)
5382 bool first_complex
= (code0
== COMPLEX_TYPE
);
5383 bool second_complex
= (code1
== COMPLEX_TYPE
);
5384 int none_complex
= (!first_complex
&& !second_complex
);
5386 /* Adapted from patch for c/24581. */
5387 if (first_complex
!= second_complex
5388 && (code
== PLUS_EXPR
5389 || code
== MINUS_EXPR
5390 || code
== MULT_EXPR
5391 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
5392 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
5393 && flag_signed_zeros
)
5395 /* An operation on mixed real/complex operands must be
5396 handled specially, but the language-independent code can
5397 more easily optimize the plain complex arithmetic if
5398 -fno-signed-zeros. */
5399 tree real_type
= TREE_TYPE (result_type
);
5403 if (TREE_TYPE (op0
) != result_type
)
5404 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5405 if (TREE_TYPE (op1
) != real_type
)
5406 op1
= cp_convert_and_check (real_type
, op1
, complain
);
5410 if (TREE_TYPE (op0
) != real_type
)
5411 op0
= cp_convert_and_check (real_type
, op0
, complain
);
5412 if (TREE_TYPE (op1
) != result_type
)
5413 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5415 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
5416 return error_mark_node
;
5419 op0
= save_expr (op0
);
5420 real
= cp_build_unary_op (REALPART_EXPR
, op0
, true, complain
);
5421 imag
= cp_build_unary_op (IMAGPART_EXPR
, op0
, true, complain
);
5425 case TRUNC_DIV_EXPR
:
5426 op1
= save_expr (op1
);
5427 imag
= build2 (resultcode
, real_type
, imag
, op1
);
5431 real
= build2 (resultcode
, real_type
, real
, op1
);
5439 op1
= save_expr (op1
);
5440 real
= cp_build_unary_op (REALPART_EXPR
, op1
, true, complain
);
5441 imag
= cp_build_unary_op (IMAGPART_EXPR
, op1
, true, complain
);
5445 op0
= save_expr (op0
);
5446 imag
= build2 (resultcode
, real_type
, op0
, imag
);
5449 real
= build2 (resultcode
, real_type
, op0
, real
);
5452 real
= build2 (resultcode
, real_type
, op0
, real
);
5453 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
5459 result
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
5463 /* For certain operations (which identify themselves by shorten != 0)
5464 if both args were extended from the same smaller type,
5465 do the arithmetic in that type and then extend.
5467 shorten !=0 and !=1 indicates a bitwise operation.
5468 For them, this optimization is safe only if
5469 both args are zero-extended or both are sign-extended.
5470 Otherwise, we might change the result.
5471 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5472 but calculated in (unsigned short) it would be (unsigned short)-1. */
5474 if (shorten
&& none_complex
)
5476 final_type
= result_type
;
5477 result_type
= shorten_binary_op (result_type
, op0
, op1
,
5481 /* Comparison operations are shortened too but differently.
5482 They identify themselves by setting short_compare = 1. */
5486 /* We call shorten_compare only for diagnostics. */
5487 tree xop0
= fold_simple (op0
);
5488 tree xop1
= fold_simple (op1
);
5489 tree xresult_type
= result_type
;
5490 enum tree_code xresultcode
= resultcode
;
5491 shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
5495 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
5496 && warn_sign_compare
5497 /* Do not warn until the template is instantiated; we cannot
5498 bound the ranges of the arguments until that point. */
5499 && !processing_template_decl
5500 && (complain
& tf_warning
)
5501 && c_inhibit_evaluation_warnings
== 0
5502 /* Even unsigned enum types promote to signed int. We don't
5503 want to issue -Wsign-compare warnings for this case. */
5504 && !enum_cast_to_int (orig_op0
)
5505 && !enum_cast_to_int (orig_op1
))
5507 warn_for_sign_compare (location
, orig_op0
, orig_op1
, op0
, op1
,
5508 result_type
, resultcode
);
5512 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5513 Then the expression will be built.
5514 It will be given type FINAL_TYPE if that is nonzero;
5515 otherwise, it will be given type RESULT_TYPE. */
5518 warning_sentinel
w (warn_sign_conversion
, short_compare
);
5519 if (TREE_TYPE (op0
) != result_type
)
5520 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5521 if (TREE_TYPE (op1
) != result_type
)
5522 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5524 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5525 return error_mark_node
;
5528 if (build_type
== NULL_TREE
)
5529 build_type
= result_type
;
5531 if (sanitize_flags_p ((SANITIZE_SHIFT
5532 | SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
))
5533 && current_function_decl
!= NULL_TREE
5534 && !processing_template_decl
5535 && (doing_div_or_mod
|| doing_shift
))
5537 /* OP0 and/or OP1 might have side-effects. */
5538 op0
= cp_save_expr (op0
);
5539 op1
= cp_save_expr (op1
);
5540 op0
= fold_non_dependent_expr (op0
, complain
);
5541 op1
= fold_non_dependent_expr (op1
, complain
);
5542 if (doing_div_or_mod
5543 && sanitize_flags_p (SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
))
5545 /* For diagnostics we want to use the promoted types without
5546 shorten_binary_op. So convert the arguments to the
5547 original result_type. */
5550 if (TREE_TYPE (cop0
) != orig_type
)
5551 cop0
= cp_convert (orig_type
, op0
, complain
);
5552 if (TREE_TYPE (cop1
) != orig_type
)
5553 cop1
= cp_convert (orig_type
, op1
, complain
);
5554 instrument_expr
= ubsan_instrument_division (location
, cop0
, cop1
);
5556 else if (doing_shift
&& sanitize_flags_p (SANITIZE_SHIFT
))
5557 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
5560 result
= build2_loc (location
, resultcode
, build_type
, op0
, op1
);
5561 if (final_type
!= 0)
5562 result
= cp_convert (final_type
, result
, complain
);
5564 if (instrument_expr
!= NULL
)
5565 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
5566 instrument_expr
, result
);
5568 if (!processing_template_decl
)
5570 op0
= cp_fully_fold (op0
);
5571 /* Only consider the second argument if the first isn't overflowed. */
5572 if (!CONSTANT_CLASS_P (op0
) || TREE_OVERFLOW_P (op0
))
5574 op1
= cp_fully_fold (op1
);
5575 if (!CONSTANT_CLASS_P (op1
) || TREE_OVERFLOW_P (op1
))
5578 else if (!CONSTANT_CLASS_P (op0
) || !CONSTANT_CLASS_P (op1
)
5579 || TREE_OVERFLOW_P (op0
) || TREE_OVERFLOW_P (op1
))
5582 result_ovl
= fold_build2 (resultcode
, build_type
, op0
, op1
);
5583 if (TREE_OVERFLOW_P (result_ovl
))
5584 overflow_warning (location
, result_ovl
);
5589 /* Build a VEC_PERM_EXPR.
5590 This is a simple wrapper for c_build_vec_perm_expr. */
5592 build_x_vec_perm_expr (location_t loc
,
5593 tree arg0
, tree arg1
, tree arg2
,
5594 tsubst_flags_t complain
)
5596 tree orig_arg0
= arg0
;
5597 tree orig_arg1
= arg1
;
5598 tree orig_arg2
= arg2
;
5599 if (processing_template_decl
)
5601 if (type_dependent_expression_p (arg0
)
5602 || type_dependent_expression_p (arg1
)
5603 || type_dependent_expression_p (arg2
))
5604 return build_min_nt_loc (loc
, VEC_PERM_EXPR
, arg0
, arg1
, arg2
);
5605 arg0
= build_non_dependent_expr (arg0
);
5607 arg1
= build_non_dependent_expr (arg1
);
5608 arg2
= build_non_dependent_expr (arg2
);
5610 tree exp
= c_build_vec_perm_expr (loc
, arg0
, arg1
, arg2
, complain
& tf_error
);
5611 if (processing_template_decl
&& exp
!= error_mark_node
)
5612 return build_min_non_dep (VEC_PERM_EXPR
, exp
, orig_arg0
,
5613 orig_arg1
, orig_arg2
);
5617 /* Return a tree for the sum or difference (RESULTCODE says which)
5618 of pointer PTROP and integer INTOP. */
5621 cp_pointer_int_sum (location_t loc
, enum tree_code resultcode
, tree ptrop
,
5622 tree intop
, tsubst_flags_t complain
)
5624 tree res_type
= TREE_TYPE (ptrop
);
5626 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5627 in certain circumstance (when it's valid to do so). So we need
5628 to make sure it's complete. We don't need to check here, if we
5629 can actually complete it at all, as those checks will be done in
5630 pointer_int_sum() anyway. */
5631 complete_type (TREE_TYPE (res_type
));
5633 return pointer_int_sum (loc
, resultcode
, ptrop
,
5634 intop
, complain
& tf_warning_or_error
);
5637 /* Return a tree for the difference of pointers OP0 and OP1.
5638 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
5639 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5642 pointer_diff (location_t loc
, tree op0
, tree op1
, tree ptrtype
,
5643 tsubst_flags_t complain
, tree
*instrument_expr
)
5645 tree result
, inttype
;
5646 tree restype
= ptrdiff_type_node
;
5647 tree target_type
= TREE_TYPE (ptrtype
);
5649 if (!complete_type_or_else (target_type
, NULL_TREE
))
5650 return error_mark_node
;
5652 if (VOID_TYPE_P (target_type
))
5654 if (complain
& tf_error
)
5655 permerror (loc
, "ISO C++ forbids using pointer of "
5656 "type %<void *%> in subtraction");
5658 return error_mark_node
;
5660 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
5662 if (complain
& tf_error
)
5663 permerror (loc
, "ISO C++ forbids using pointer to "
5664 "a function in subtraction");
5666 return error_mark_node
;
5668 if (TREE_CODE (target_type
) == METHOD_TYPE
)
5670 if (complain
& tf_error
)
5671 permerror (loc
, "ISO C++ forbids using pointer to "
5672 "a method in subtraction");
5674 return error_mark_node
;
5677 /* Determine integer type result of the subtraction. This will usually
5678 be the same as the result type (ptrdiff_t), but may need to be a wider
5679 type if pointers for the address space are wider than ptrdiff_t. */
5680 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
5681 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
5685 if (!processing_template_decl
5686 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT
))
5688 op0
= save_expr (op0
);
5689 op1
= save_expr (op1
);
5691 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT
);
5692 *instrument_expr
= build_call_expr_loc (loc
, tt
, 2, op0
, op1
);
5695 /* First do the subtraction, then build the divide operator
5696 and only convert at the very end.
5697 Do not do default conversions in case restype is a short type. */
5699 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5700 pointers. If some platform cannot provide that, or has a larger
5701 ptrdiff_type to support differences larger than half the address
5702 space, cast the pointers to some larger integer type and do the
5703 computations in that type. */
5704 if (TYPE_PRECISION (inttype
) > TYPE_PRECISION (TREE_TYPE (op0
)))
5705 op0
= cp_build_binary_op (loc
,
5707 cp_convert (inttype
, op0
, complain
),
5708 cp_convert (inttype
, op1
, complain
),
5711 op0
= build2_loc (loc
, POINTER_DIFF_EXPR
, inttype
, op0
, op1
);
5713 /* This generates an error if op1 is a pointer to an incomplete type. */
5714 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
5716 if (complain
& tf_error
)
5717 error_at (loc
, "invalid use of a pointer to an incomplete type in "
5718 "pointer arithmetic");
5720 return error_mark_node
;
5723 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1
)))
5725 if (complain
& tf_error
)
5726 error_at (loc
, "arithmetic on pointer to an empty aggregate");
5728 return error_mark_node
;
5731 op1
= (TYPE_PTROB_P (ptrtype
)
5732 ? size_in_bytes_loc (loc
, target_type
)
5733 : integer_one_node
);
5735 /* Do the division. */
5737 result
= build2_loc (loc
, EXACT_DIV_EXPR
, inttype
, op0
,
5738 cp_convert (inttype
, op1
, complain
));
5739 return cp_convert (restype
, result
, complain
);
5742 /* Construct and perhaps optimize a tree representation
5743 for a unary operation. CODE, a tree_code, specifies the operation
5744 and XARG is the operand. */
5747 build_x_unary_op (location_t loc
, enum tree_code code
, cp_expr xarg
,
5748 tsubst_flags_t complain
)
5750 tree orig_expr
= xarg
;
5753 tree overload
= NULL_TREE
;
5755 if (processing_template_decl
)
5757 if (type_dependent_expression_p (xarg
))
5759 tree e
= build_min_nt_loc (loc
, code
, xarg
.get_value (), NULL_TREE
);
5760 maybe_save_operator_binding (e
);
5764 xarg
= build_non_dependent_expr (xarg
);
5769 /* [expr.unary.op] says:
5771 The address of an object of incomplete type can be taken.
5773 (And is just the ordinary address operator, not an overloaded
5774 "operator &".) However, if the type is a template
5775 specialization, we must complete the type at this point so that
5776 an overloaded "operator &" will be available if required. */
5777 if (code
== ADDR_EXPR
5778 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
5779 && ((CLASS_TYPE_P (TREE_TYPE (xarg
))
5780 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg
))))
5781 || (TREE_CODE (xarg
) == OFFSET_REF
)))
5782 /* Don't look for a function. */;
5784 exp
= build_new_op (loc
, code
, LOOKUP_NORMAL
, xarg
, NULL_TREE
,
5785 NULL_TREE
, &overload
, complain
);
5787 if (!exp
&& code
== ADDR_EXPR
)
5789 if (is_overloaded_fn (xarg
))
5791 tree fn
= get_first_fn (xarg
);
5792 if (DECL_CONSTRUCTOR_P (fn
) || DECL_DESTRUCTOR_P (fn
))
5794 if (complain
& tf_error
)
5795 error (DECL_CONSTRUCTOR_P (fn
)
5796 ? G_("taking address of constructor %qD")
5797 : G_("taking address of destructor %qD"),
5799 return error_mark_node
;
5803 /* A pointer to member-function can be formed only by saying
5805 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
5806 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
5808 if (TREE_CODE (xarg
) != OFFSET_REF
5809 || !TYPE_P (TREE_OPERAND (xarg
, 0)))
5811 if (complain
& tf_error
)
5813 error ("invalid use of %qE to form a "
5814 "pointer-to-member-function", xarg
.get_value ());
5815 if (TREE_CODE (xarg
) != OFFSET_REF
)
5816 inform (input_location
, " a qualified-id is required");
5818 return error_mark_node
;
5822 if (complain
& tf_error
)
5823 error ("parentheses around %qE cannot be used to form a"
5824 " pointer-to-member-function",
5827 return error_mark_node
;
5828 PTRMEM_OK_P (xarg
) = 1;
5832 if (TREE_CODE (xarg
) == OFFSET_REF
)
5834 ptrmem
= PTRMEM_OK_P (xarg
);
5836 if (!ptrmem
&& !flag_ms_extensions
5837 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
5839 /* A single non-static member, make sure we don't allow a
5840 pointer-to-member. */
5841 xarg
= build2 (OFFSET_REF
, TREE_TYPE (xarg
),
5842 TREE_OPERAND (xarg
, 0),
5843 ovl_make (TREE_OPERAND (xarg
, 1)));
5844 PTRMEM_OK_P (xarg
) = ptrmem
;
5848 exp
= cp_build_addr_expr_strict (xarg
, complain
);
5851 if (processing_template_decl
&& exp
!= error_mark_node
)
5853 if (overload
!= NULL_TREE
)
5854 return (build_min_non_dep_op_overload
5855 (code
, exp
, overload
, orig_expr
, integer_zero_node
));
5857 exp
= build_min_non_dep (code
, exp
, orig_expr
,
5858 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE
);
5860 if (TREE_CODE (exp
) == ADDR_EXPR
)
5861 PTRMEM_OK_P (exp
) = ptrmem
;
5865 /* Construct and perhaps optimize a tree representation
5866 for __builtin_addressof operation. ARG specifies the operand. */
5869 cp_build_addressof (location_t loc
, tree arg
, tsubst_flags_t complain
)
5871 tree orig_expr
= arg
;
5873 if (processing_template_decl
)
5875 if (type_dependent_expression_p (arg
))
5876 return build_min_nt_loc (loc
, ADDRESSOF_EXPR
, arg
, NULL_TREE
);
5878 arg
= build_non_dependent_expr (arg
);
5881 tree exp
= cp_build_addr_expr_strict (arg
, complain
);
5883 if (processing_template_decl
&& exp
!= error_mark_node
)
5884 exp
= build_min_non_dep (ADDRESSOF_EXPR
, exp
, orig_expr
, NULL_TREE
);
5888 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5889 constants, where a null value is represented by an INTEGER_CST of
5893 cp_truthvalue_conversion (tree expr
)
5895 tree type
= TREE_TYPE (expr
);
5896 if (TYPE_PTR_OR_PTRMEM_P (type
)
5897 /* Avoid ICE on invalid use of non-static member function. */
5898 || TREE_CODE (expr
) == FUNCTION_DECL
)
5899 return build_binary_op (input_location
, NE_EXPR
, expr
, nullptr_node
, true);
5901 return c_common_truthvalue_conversion (input_location
, expr
);
5904 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
5905 is a low-level function; most callers should use maybe_convert_cond. */
5908 condition_conversion (tree expr
)
5911 t
= perform_implicit_conversion_flags (boolean_type_node
, expr
,
5912 tf_warning_or_error
, LOOKUP_NORMAL
);
5913 if (!processing_template_decl
)
5914 t
= fold_build_cleanup_point_expr (boolean_type_node
, t
);
5918 /* Returns the address of T. This function will fold away
5919 ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
5920 most places should use cp_build_addr_expr instead. */
5923 build_address (tree t
)
5925 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
5926 return error_mark_node
;
5927 gcc_checking_assert (TREE_CODE (t
) != CONSTRUCTOR
5928 || processing_template_decl
);
5929 t
= build_fold_addr_expr_loc (EXPR_LOCATION (t
), t
);
5930 if (TREE_CODE (t
) != ADDR_EXPR
)
5935 /* Return a NOP_EXPR converting EXPR to TYPE. */
5938 build_nop (tree type
, tree expr
)
5940 if (type
== error_mark_node
|| error_operand_p (expr
))
5942 return build1_loc (EXPR_LOCATION (expr
), NOP_EXPR
, type
, expr
);
5945 /* Take the address of ARG, whatever that means under C++ semantics.
5946 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5947 and class rvalues as well.
5949 Nothing should call this function directly; instead, callers should use
5950 cp_build_addr_expr or cp_build_addr_expr_strict. */
5953 cp_build_addr_expr_1 (tree arg
, bool strict_lvalue
, tsubst_flags_t complain
)
5958 if (!arg
|| error_operand_p (arg
))
5959 return error_mark_node
;
5961 arg
= mark_lvalue_use (arg
);
5962 if (error_operand_p (arg
))
5963 return error_mark_node
;
5965 argtype
= lvalue_type (arg
);
5967 gcc_assert (!(identifier_p (arg
) && IDENTIFIER_ANY_OP_P (arg
)));
5969 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
5970 && !really_overloaded_fn (arg
))
5972 /* They're trying to take the address of a unique non-static
5973 member function. This is ill-formed (except in MS-land),
5974 but let's try to DTRT.
5975 Note: We only handle unique functions here because we don't
5976 want to complain if there's a static overload; non-unique
5977 cases will be handled by instantiate_type. But we need to
5978 handle this case here to allow casts on the resulting PMF.
5979 We could defer this in non-MS mode, but it's easier to give
5980 a useful error here. */
5982 /* Inside constant member functions, the `this' pointer
5983 contains an extra const qualifier. TYPE_MAIN_VARIANT
5984 is used here to remove this const from the diagnostics
5985 and the created OFFSET_REF. */
5986 tree base
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg
, 0)));
5987 tree fn
= get_first_fn (TREE_OPERAND (arg
, 1));
5988 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
5989 return error_mark_node
;
5991 if (! flag_ms_extensions
)
5993 tree name
= DECL_NAME (fn
);
5994 if (!(complain
& tf_error
))
5995 return error_mark_node
;
5996 else if (current_class_type
5997 && TREE_OPERAND (arg
, 0) == current_class_ref
)
5998 /* An expression like &memfn. */
5999 permerror (input_location
, "ISO C++ forbids taking the address of an unqualified"
6000 " or parenthesized non-static member function to form"
6001 " a pointer to member function. Say %<&%T::%D%>",
6004 permerror (input_location
, "ISO C++ forbids taking the address of a bound member"
6005 " function to form a pointer to member function."
6009 arg
= build_offset_ref (base
, fn
, /*address_p=*/true, complain
);
6012 /* Uninstantiated types are all functions. Taking the
6013 address of a function is a no-op, so just return the
6015 if (type_unknown_p (arg
))
6016 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
6018 if (TREE_CODE (arg
) == OFFSET_REF
)
6019 /* We want a pointer to member; bypass all the code for actually taking
6020 the address of something. */
6023 /* Anything not already handled and not a true memory reference
6025 if (TREE_CODE (argtype
) != FUNCTION_TYPE
6026 && TREE_CODE (argtype
) != METHOD_TYPE
)
6028 cp_lvalue_kind kind
= lvalue_kind (arg
);
6029 if (kind
== clk_none
)
6031 if (complain
& tf_error
)
6032 lvalue_error (input_location
, lv_addressof
);
6033 return error_mark_node
;
6035 if (strict_lvalue
&& (kind
& (clk_rvalueref
|clk_class
)))
6037 if (!(complain
& tf_error
))
6038 return error_mark_node
;
6039 /* Make this a permerror because we used to accept it. */
6040 permerror (input_location
, "taking address of rvalue");
6044 if (TYPE_REF_P (argtype
))
6046 tree type
= build_pointer_type (TREE_TYPE (argtype
));
6047 arg
= build1 (CONVERT_EXPR
, type
, arg
);
6050 else if (pedantic
&& DECL_MAIN_P (arg
))
6053 /* Apparently a lot of autoconf scripts for C++ packages do this,
6054 so only complain if -Wpedantic. */
6055 if (complain
& (flag_pedantic_errors
? tf_error
: tf_warning
))
6056 pedwarn (input_location
, OPT_Wpedantic
,
6057 "ISO C++ forbids taking address of function %<::main%>");
6058 else if (flag_pedantic_errors
)
6059 return error_mark_node
;
6062 /* Let &* cancel out to simplify resulting code. */
6063 if (INDIRECT_REF_P (arg
))
6065 arg
= TREE_OPERAND (arg
, 0);
6066 if (TYPE_REF_P (TREE_TYPE (arg
)))
6068 tree type
= build_pointer_type (TREE_TYPE (TREE_TYPE (arg
)));
6069 arg
= build1 (CONVERT_EXPR
, type
, arg
);
6072 /* Don't let this be an lvalue. */
6077 /* Handle complex lvalues (when permitted)
6078 by reduction to simpler cases. */
6079 val
= unary_complex_lvalue (ADDR_EXPR
, arg
);
6083 switch (TREE_CODE (arg
))
6087 case FIX_TRUNC_EXPR
:
6088 /* We should have handled this above in the lvalue_kind check. */
6093 arg
= BASELINK_FUNCTIONS (arg
);
6097 arg
= OVL_FIRST (arg
);
6102 /* Turn a reference to a non-static data member into a
6103 pointer-to-member. */
6108 gcc_assert (PTRMEM_OK_P (arg
));
6110 t
= TREE_OPERAND (arg
, 1);
6111 if (TYPE_REF_P (TREE_TYPE (t
)))
6113 if (complain
& tf_error
)
6114 error ("cannot create pointer to reference member %qD", t
);
6115 return error_mark_node
;
6118 type
= build_ptrmem_type (context_for_name_lookup (t
),
6120 t
= make_ptrmem_cst (type
, TREE_OPERAND (arg
, 1));
6128 if (argtype
!= error_mark_node
)
6129 argtype
= build_pointer_type (argtype
);
6131 if (bitfield_p (arg
))
6133 if (complain
& tf_error
)
6134 error ("attempt to take address of bit-field");
6135 return error_mark_node
;
6138 /* In a template, we are processing a non-dependent expression
6139 so we can just form an ADDR_EXPR with the correct type. */
6140 if (processing_template_decl
|| TREE_CODE (arg
) != COMPONENT_REF
)
6142 tree stripped_arg
= tree_strip_any_location_wrapper (arg
);
6143 if (TREE_CODE (stripped_arg
) == FUNCTION_DECL
6144 && !mark_used (stripped_arg
, complain
) && !(complain
& tf_error
))
6145 return error_mark_node
;
6146 val
= build_address (arg
);
6147 if (TREE_CODE (arg
) == OFFSET_REF
)
6148 PTRMEM_OK_P (val
) = PTRMEM_OK_P (arg
);
6150 else if (BASELINK_P (TREE_OPERAND (arg
, 1)))
6152 tree fn
= BASELINK_FUNCTIONS (TREE_OPERAND (arg
, 1));
6154 /* We can only get here with a single static member
6156 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
6157 && DECL_STATIC_FUNCTION_P (fn
));
6158 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
6159 return error_mark_node
;
6160 val
= build_address (fn
);
6161 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
6162 /* Do not lose object's side effects. */
6163 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
),
6164 TREE_OPERAND (arg
, 0), val
);
6168 tree object
= TREE_OPERAND (arg
, 0);
6169 tree field
= TREE_OPERAND (arg
, 1);
6170 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6171 (TREE_TYPE (object
), decl_type_context (field
)));
6172 val
= build_address (arg
);
6175 if (TYPE_PTR_P (argtype
)
6176 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
6178 build_ptrmemfunc_type (argtype
);
6179 val
= build_ptrmemfunc (argtype
, val
, 0,
6187 /* Take the address of ARG if it has one, even if it's an rvalue. */
6190 cp_build_addr_expr (tree arg
, tsubst_flags_t complain
)
6192 return cp_build_addr_expr_1 (arg
, 0, complain
);
6195 /* Take the address of ARG, but only if it's an lvalue. */
6198 cp_build_addr_expr_strict (tree arg
, tsubst_flags_t complain
)
6200 return cp_build_addr_expr_1 (arg
, 1, complain
);
6203 /* C++: Must handle pointers to members.
6205 Perhaps type instantiation should be extended to handle conversion
6206 from aggregates to types we don't yet know we want? (Or are those
6207 cases typically errors which should be reported?)
6209 NOCONVERT suppresses the default promotions (such as from short to int). */
6212 cp_build_unary_op (enum tree_code code
, tree xarg
, bool noconvert
,
6213 tsubst_flags_t complain
)
6215 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
6217 location_t location
= cp_expr_loc_or_loc (arg
, input_location
);
6219 const char *errstring
= NULL
;
6221 const char *invalid_op_diag
;
6223 if (!arg
|| error_operand_p (arg
))
6224 return error_mark_node
;
6226 if ((invalid_op_diag
6227 = targetm
.invalid_unary_op ((code
== UNARY_PLUS_EXPR
6232 if (complain
& tf_error
)
6233 error (invalid_op_diag
);
6234 return error_mark_node
;
6239 case UNARY_PLUS_EXPR
:
6242 int flags
= WANT_ARITH
| WANT_ENUM
;
6243 /* Unary plus (but not unary minus) is allowed on pointers. */
6244 if (code
== UNARY_PLUS_EXPR
)
6245 flags
|= WANT_POINTER
;
6246 arg
= build_expr_type_conversion (flags
, arg
, true);
6248 errstring
= (code
== NEGATE_EXPR
6249 ? _("wrong type argument to unary minus")
6250 : _("wrong type argument to unary plus"));
6253 if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
6254 arg
= cp_perform_integral_promotions (arg
, complain
);
6256 /* Make sure the result is not an lvalue: a unary plus or minus
6257 expression is always a rvalue. */
6264 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
6269 arg
= cp_default_conversion (arg
, complain
);
6270 if (arg
== error_mark_node
)
6271 return error_mark_node
;
6274 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
6275 | WANT_VECTOR_OR_COMPLEX
,
6277 errstring
= _("wrong type argument to bit-complement");
6278 else if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
6280 /* Warn if the expression has boolean value. */
6281 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
6282 && (complain
& tf_warning
)
6283 && warning_at (location
, OPT_Wbool_operation
,
6284 "%<~%> on an expression of type bool"))
6285 inform (location
, "did you mean to use logical not (%<!%>)?");
6286 arg
= cp_perform_integral_promotions (arg
, complain
);
6288 else if (!noconvert
&& VECTOR_TYPE_P (TREE_TYPE (arg
)))
6289 arg
= mark_rvalue_use (arg
);
6293 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
6294 errstring
= _("wrong type argument to abs");
6295 else if (!noconvert
)
6297 arg
= cp_default_conversion (arg
, complain
);
6298 if (arg
== error_mark_node
)
6299 return error_mark_node
;
6304 /* Conjugating a real value is a no-op, but allow it anyway. */
6305 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
6306 errstring
= _("wrong type argument to conjugation");
6307 else if (!noconvert
)
6309 arg
= cp_default_conversion (arg
, complain
);
6310 if (arg
== error_mark_node
)
6311 return error_mark_node
;
6315 case TRUTH_NOT_EXPR
:
6316 if (VECTOR_TYPE_P (TREE_TYPE (arg
)))
6317 return cp_build_binary_op (input_location
, EQ_EXPR
, arg
,
6318 build_zero_cst (TREE_TYPE (arg
)), complain
);
6319 arg
= perform_implicit_conversion (boolean_type_node
, arg
,
6321 val
= invert_truthvalue_loc (input_location
, arg
);
6322 if (arg
!= error_mark_node
)
6324 errstring
= _("in argument to unary !");
6332 arg
= build_real_imag_expr (input_location
, code
, arg
);
6335 case PREINCREMENT_EXPR
:
6336 case POSTINCREMENT_EXPR
:
6337 case PREDECREMENT_EXPR
:
6338 case POSTDECREMENT_EXPR
:
6339 /* Handle complex lvalues (when permitted)
6340 by reduction to simpler cases. */
6342 val
= unary_complex_lvalue (code
, arg
);
6346 arg
= mark_lvalue_use (arg
);
6348 /* Increment or decrement the real part of the value,
6349 and don't change the imaginary part. */
6350 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
6354 arg
= cp_stabilize_reference (arg
);
6355 real
= cp_build_unary_op (REALPART_EXPR
, arg
, true, complain
);
6356 imag
= cp_build_unary_op (IMAGPART_EXPR
, arg
, true, complain
);
6357 real
= cp_build_unary_op (code
, real
, true, complain
);
6358 if (real
== error_mark_node
|| imag
== error_mark_node
)
6359 return error_mark_node
;
6360 return build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
6364 /* Report invalid types. */
6366 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
6369 if (code
== PREINCREMENT_EXPR
)
6370 errstring
= _("no pre-increment operator for type");
6371 else if (code
== POSTINCREMENT_EXPR
)
6372 errstring
= _("no post-increment operator for type");
6373 else if (code
== PREDECREMENT_EXPR
)
6374 errstring
= _("no pre-decrement operator for type");
6376 errstring
= _("no post-decrement operator for type");
6379 else if (arg
== error_mark_node
)
6380 return error_mark_node
;
6382 /* Report something read-only. */
6384 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
6385 || TREE_READONLY (arg
))
6387 if (complain
& tf_error
)
6388 cxx_readonly_error (location
, arg
,
6389 ((code
== PREINCREMENT_EXPR
6390 || code
== POSTINCREMENT_EXPR
)
6391 ? lv_increment
: lv_decrement
));
6393 return error_mark_node
;
6398 tree declared_type
= unlowered_expr_type (arg
);
6400 argtype
= TREE_TYPE (arg
);
6402 /* ARM $5.2.5 last annotation says this should be forbidden. */
6403 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
6405 if (complain
& tf_error
)
6406 permerror (input_location
, (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
6407 ? G_("ISO C++ forbids incrementing an enum")
6408 : G_("ISO C++ forbids decrementing an enum"));
6410 return error_mark_node
;
6413 /* Compute the increment. */
6415 if (TYPE_PTR_P (argtype
))
6417 tree type
= complete_type (TREE_TYPE (argtype
));
6419 if (!COMPLETE_OR_VOID_TYPE_P (type
))
6421 if (complain
& tf_error
)
6422 error (((code
== PREINCREMENT_EXPR
6423 || code
== POSTINCREMENT_EXPR
))
6424 ? G_("cannot increment a pointer to incomplete type %qT")
6425 : G_("cannot decrement a pointer to incomplete type %qT"),
6426 TREE_TYPE (argtype
));
6428 return error_mark_node
;
6430 else if (!TYPE_PTROB_P (argtype
))
6432 if (complain
& tf_error
)
6433 pedwarn (input_location
, OPT_Wpointer_arith
,
6434 (code
== PREINCREMENT_EXPR
6435 || code
== POSTINCREMENT_EXPR
)
6436 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6437 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6440 return error_mark_node
;
6443 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
6446 inc
= VECTOR_TYPE_P (argtype
)
6447 ? build_one_cst (argtype
)
6450 inc
= cp_convert (argtype
, inc
, complain
);
6452 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6453 need to ask Objective-C to build the increment or decrement
6454 expression for it. */
6455 if (objc_is_property_ref (arg
))
6456 return objc_build_incr_expr_for_property_ref (input_location
, code
,
6459 /* Complain about anything else that is not a true lvalue. */
6460 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
6461 || code
== POSTINCREMENT_EXPR
)
6462 ? lv_increment
: lv_decrement
),
6464 return error_mark_node
;
6466 /* Forbid using -- or ++ in C++17 on `bool'. */
6467 if (TREE_CODE (declared_type
) == BOOLEAN_TYPE
)
6469 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
6471 if (complain
& tf_error
)
6472 error ("use of an operand of type %qT in %<operator--%> "
6473 "is forbidden", boolean_type_node
);
6474 return error_mark_node
;
6478 if (cxx_dialect
>= cxx17
)
6480 if (complain
& tf_error
)
6481 error ("use of an operand of type %qT in "
6482 "%<operator++%> is forbidden in C++17",
6484 return error_mark_node
;
6486 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6487 else if (!in_system_header_at (input_location
))
6488 warning (OPT_Wdeprecated
, "use of an operand of type %qT "
6489 "in %<operator++%> is deprecated",
6492 val
= boolean_increment (code
, arg
);
6494 else if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
6495 /* An rvalue has no cv-qualifiers. */
6496 val
= build2 (code
, cv_unqualified (TREE_TYPE (arg
)), arg
, inc
);
6498 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
6500 TREE_SIDE_EFFECTS (val
) = 1;
6505 /* Note that this operation never does default_conversion
6506 regardless of NOCONVERT. */
6507 return cp_build_addr_expr (arg
, complain
);
6516 argtype
= TREE_TYPE (arg
);
6517 return build1 (code
, argtype
, arg
);
6520 if (complain
& tf_error
)
6521 error ("%s", errstring
);
6522 return error_mark_node
;
6525 /* Hook for the c-common bits that build a unary op. */
6527 build_unary_op (location_t
/*location*/,
6528 enum tree_code code
, tree xarg
, bool noconvert
)
6530 return cp_build_unary_op (code
, xarg
, noconvert
, tf_warning_or_error
);
6533 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
6534 so that it is a valid lvalue even for GENERIC by replacing
6535 (lhs = rhs) with ((lhs = rhs), lhs)
6536 (--lhs) with ((--lhs), lhs)
6537 (++lhs) with ((++lhs), lhs)
6538 and if lhs has side-effects, calling cp_stabilize_reference on it, so
6539 that it can be evaluated multiple times. */
6542 genericize_compound_lvalue (tree lvalue
)
6544 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue
, 0)))
6545 lvalue
= build2 (TREE_CODE (lvalue
), TREE_TYPE (lvalue
),
6546 cp_stabilize_reference (TREE_OPERAND (lvalue
, 0)),
6547 TREE_OPERAND (lvalue
, 1));
6548 return build2 (COMPOUND_EXPR
, TREE_TYPE (TREE_OPERAND (lvalue
, 0)),
6549 lvalue
, TREE_OPERAND (lvalue
, 0));
6552 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6553 for certain kinds of expressions which are not really lvalues
6554 but which we can accept as lvalues.
6556 If ARG is not a kind of expression we can handle, return
6560 unary_complex_lvalue (enum tree_code code
, tree arg
)
6562 /* Inside a template, making these kinds of adjustments is
6563 pointless; we are only concerned with the type of the
6565 if (processing_template_decl
)
6568 /* Handle (a, b) used as an "lvalue". */
6569 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
6571 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 1), false,
6572 tf_warning_or_error
);
6573 return build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6574 TREE_OPERAND (arg
, 0), real_result
);
6577 /* Handle (a ? b : c) used as an "lvalue". */
6578 if (TREE_CODE (arg
) == COND_EXPR
6579 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
6580 return rationalize_conditional_expr (code
, arg
, tf_warning_or_error
);
6582 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6583 if (TREE_CODE (arg
) == MODIFY_EXPR
6584 || TREE_CODE (arg
) == PREINCREMENT_EXPR
6585 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
6586 return unary_complex_lvalue (code
, genericize_compound_lvalue (arg
));
6588 if (code
!= ADDR_EXPR
)
6591 /* Handle (a = b) used as an "lvalue" for `&'. */
6592 if (TREE_CODE (arg
) == MODIFY_EXPR
6593 || TREE_CODE (arg
) == INIT_EXPR
)
6595 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 0), false,
6596 tf_warning_or_error
);
6597 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6599 TREE_NO_WARNING (arg
) = 1;
6603 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
6604 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
6605 || TREE_CODE (arg
) == OFFSET_REF
)
6608 /* We permit compiler to make function calls returning
6609 objects of aggregate type look like lvalues. */
6613 if (TREE_CODE (targ
) == SAVE_EXPR
)
6614 targ
= TREE_OPERAND (targ
, 0);
6616 if (TREE_CODE (targ
) == CALL_EXPR
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (targ
)))
6618 if (TREE_CODE (arg
) == SAVE_EXPR
)
6621 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, tf_warning_or_error
);
6622 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
6625 if (TREE_CODE (arg
) == SAVE_EXPR
&& INDIRECT_REF_P (targ
))
6626 return build3 (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
6627 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
6630 /* Don't let anything else be handled specially. */
6634 /* Mark EXP saying that we need to be able to take the
6635 address of it; it should not be allocated in a register.
6636 Value is true if successful. ARRAY_REF_P is true if this
6637 is for ARRAY_REF construction - in that case we don't want
6638 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6639 it is fine to use ARRAY_REFs for vector subscripts on vector
6642 C++: we do not allow `current_class_ptr' to be addressable. */
6645 cxx_mark_addressable (tree exp
, bool array_ref_p
)
6650 switch (TREE_CODE (x
))
6652 case VIEW_CONVERT_EXPR
:
6654 && TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
6655 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x
, 0))))
6663 x
= TREE_OPERAND (x
, 0);
6667 if (x
== current_class_ptr
)
6669 error ("cannot take the address of %<this%>, which is an rvalue expression");
6670 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later. */
6676 /* Caller should not be trying to mark initialized
6677 constant fields addressable. */
6678 gcc_assert (DECL_LANG_SPECIFIC (x
) == 0
6679 || DECL_IN_AGGR_P (x
) == 0
6681 || DECL_EXTERNAL (x
));
6685 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
6686 && !DECL_ARTIFICIAL (x
))
6688 if (VAR_P (x
) && DECL_HARD_REGISTER (x
))
6691 ("address of explicit register variable %qD requested", x
);
6694 else if (extra_warnings
)
6696 (OPT_Wextra
, "address requested for %qD, which is declared %<register%>", x
);
6698 TREE_ADDRESSABLE (x
) = 1;
6703 TREE_ADDRESSABLE (x
) = 1;
6707 TREE_ADDRESSABLE (x
) = 1;
6711 TREE_ADDRESSABLE (x
) = 1;
6712 cxx_mark_addressable (TREE_OPERAND (x
, 0));
6720 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6723 build_x_conditional_expr (location_t loc
, tree ifexp
, tree op1
, tree op2
,
6724 tsubst_flags_t complain
)
6726 tree orig_ifexp
= ifexp
;
6727 tree orig_op1
= op1
;
6728 tree orig_op2
= op2
;
6731 if (processing_template_decl
)
6733 /* The standard says that the expression is type-dependent if
6734 IFEXP is type-dependent, even though the eventual type of the
6735 expression doesn't dependent on IFEXP. */
6736 if (type_dependent_expression_p (ifexp
)
6737 /* As a GNU extension, the middle operand may be omitted. */
6738 || (op1
&& type_dependent_expression_p (op1
))
6739 || type_dependent_expression_p (op2
))
6740 return build_min_nt_loc (loc
, COND_EXPR
, ifexp
, op1
, op2
);
6741 ifexp
= build_non_dependent_expr (ifexp
);
6743 op1
= build_non_dependent_expr (op1
);
6744 op2
= build_non_dependent_expr (op2
);
6747 expr
= build_conditional_expr (loc
, ifexp
, op1
, op2
, complain
);
6748 if (processing_template_decl
&& expr
!= error_mark_node
)
6750 tree min
= build_min_non_dep (COND_EXPR
, expr
,
6751 orig_ifexp
, orig_op1
, orig_op2
);
6752 expr
= convert_from_reference (min
);
6757 /* Given a list of expressions, return a compound expression
6758 that performs them all and returns the value of the last of them. */
6761 build_x_compound_expr_from_list (tree list
, expr_list_kind exp
,
6762 tsubst_flags_t complain
)
6764 tree expr
= TREE_VALUE (list
);
6766 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
6767 && !CONSTRUCTOR_IS_DIRECT_INIT (expr
))
6769 if (complain
& tf_error
)
6770 pedwarn (cp_expr_loc_or_loc (expr
, input_location
), 0,
6771 "list-initializer for non-class type must not "
6772 "be parenthesized");
6774 return error_mark_node
;
6777 if (TREE_CHAIN (list
))
6779 if (complain
& tf_error
)
6783 permerror (input_location
, "expression list treated as compound "
6784 "expression in initializer");
6787 permerror (input_location
, "expression list treated as compound "
6788 "expression in mem-initializer");
6791 permerror (input_location
, "expression list treated as compound "
6792 "expression in functional cast");
6798 return error_mark_node
;
6800 for (list
= TREE_CHAIN (list
); list
; list
= TREE_CHAIN (list
))
6801 expr
= build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list
)),
6802 expr
, TREE_VALUE (list
), complain
);
6808 /* Like build_x_compound_expr_from_list, but using a VEC. */
6811 build_x_compound_expr_from_vec (vec
<tree
, va_gc
> *vec
, const char *msg
,
6812 tsubst_flags_t complain
)
6814 if (vec_safe_is_empty (vec
))
6816 else if (vec
->length () == 1)
6826 if (complain
& tf_error
)
6827 permerror (input_location
,
6828 "%s expression list treated as compound expression",
6831 return error_mark_node
;
6835 for (ix
= 1; vec
->iterate (ix
, &t
); ++ix
)
6836 expr
= build_x_compound_expr (EXPR_LOCATION (t
), expr
,
6843 /* Handle overloading of the ',' operator when needed. */
6846 build_x_compound_expr (location_t loc
, tree op1
, tree op2
,
6847 tsubst_flags_t complain
)
6850 tree orig_op1
= op1
;
6851 tree orig_op2
= op2
;
6852 tree overload
= NULL_TREE
;
6854 if (processing_template_decl
)
6856 if (type_dependent_expression_p (op1
)
6857 || type_dependent_expression_p (op2
))
6858 return build_min_nt_loc (loc
, COMPOUND_EXPR
, op1
, op2
);
6859 op1
= build_non_dependent_expr (op1
);
6860 op2
= build_non_dependent_expr (op2
);
6863 result
= build_new_op (loc
, COMPOUND_EXPR
, LOOKUP_NORMAL
, op1
, op2
,
6864 NULL_TREE
, &overload
, complain
);
6866 result
= cp_build_compound_expr (op1
, op2
, complain
);
6868 if (processing_template_decl
&& result
!= error_mark_node
)
6870 if (overload
!= NULL_TREE
)
6871 return (build_min_non_dep_op_overload
6872 (COMPOUND_EXPR
, result
, overload
, orig_op1
, orig_op2
));
6874 return build_min_non_dep (COMPOUND_EXPR
, result
, orig_op1
, orig_op2
);
6880 /* Like cp_build_compound_expr, but for the c-common bits. */
6883 build_compound_expr (location_t
/*loc*/, tree lhs
, tree rhs
)
6885 return cp_build_compound_expr (lhs
, rhs
, tf_warning_or_error
);
6888 /* Build a compound expression. */
6891 cp_build_compound_expr (tree lhs
, tree rhs
, tsubst_flags_t complain
)
6893 lhs
= convert_to_void (lhs
, ICV_LEFT_OF_COMMA
, complain
);
6895 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
6896 return error_mark_node
;
6898 if (TREE_CODE (rhs
) == TARGET_EXPR
)
6900 /* If the rhs is a TARGET_EXPR, then build the compound
6901 expression inside the target_expr's initializer. This
6902 helps the compiler to eliminate unnecessary temporaries. */
6903 tree init
= TREE_OPERAND (rhs
, 1);
6905 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), lhs
, init
);
6906 TREE_OPERAND (rhs
, 1) = init
;
6911 if (type_unknown_p (rhs
))
6913 if (complain
& tf_error
)
6914 error ("no context to resolve type of %qE", rhs
);
6915 return error_mark_node
;
6918 return build2 (COMPOUND_EXPR
, TREE_TYPE (rhs
), lhs
, rhs
);
6921 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6922 casts away constness. CAST gives the type of cast. Returns true
6923 if the cast is ill-formed, false if it is well-formed.
6925 ??? This function warns for casting away any qualifier not just
6926 const. We would like to specify exactly what qualifiers are casted
6931 check_for_casting_away_constness (tree src_type
, tree dest_type
,
6932 enum tree_code cast
, tsubst_flags_t complain
)
6934 /* C-style casts are allowed to cast away constness. With
6935 WARN_CAST_QUAL, we still want to issue a warning. */
6936 if (cast
== CAST_EXPR
&& !warn_cast_qual
)
6939 if (!casts_away_constness (src_type
, dest_type
, complain
))
6945 if (complain
& tf_warning
)
6946 warning (OPT_Wcast_qual
,
6947 "cast from type %qT to type %qT casts away qualifiers",
6948 src_type
, dest_type
);
6951 case STATIC_CAST_EXPR
:
6952 if (complain
& tf_error
)
6953 error ("static_cast from type %qT to type %qT casts away qualifiers",
6954 src_type
, dest_type
);
6957 case REINTERPRET_CAST_EXPR
:
6958 if (complain
& tf_error
)
6959 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6960 src_type
, dest_type
);
6968 /* Warns if the cast from expression EXPR to type TYPE is useless. */
6970 maybe_warn_about_useless_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6972 if (warn_useless_cast
6973 && complain
& tf_warning
)
6975 if ((TYPE_REF_P (type
)
6976 && (TYPE_REF_IS_RVALUE (type
)
6977 ? xvalue_p (expr
) : lvalue_p (expr
))
6978 && same_type_p (TREE_TYPE (expr
), TREE_TYPE (type
)))
6979 || same_type_p (TREE_TYPE (expr
), type
))
6980 warning (OPT_Wuseless_cast
, "useless cast to type %q#T", type
);
6984 /* Warns if the cast ignores cv-qualifiers on TYPE. */
6986 maybe_warn_about_cast_ignoring_quals (tree type
, tsubst_flags_t complain
)
6988 if (warn_ignored_qualifiers
6989 && complain
& tf_warning
6990 && !CLASS_TYPE_P (type
)
6991 && (cp_type_quals (type
) & (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)))
6993 warning (OPT_Wignored_qualifiers
, "type qualifiers ignored on cast "
6998 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6999 (another pointer-to-member type in the same hierarchy) and return
7000 the converted expression. If ALLOW_INVERSE_P is permitted, a
7001 pointer-to-derived may be converted to pointer-to-base; otherwise,
7002 only the other direction is permitted. If C_CAST_P is true, this
7003 conversion is taking place as part of a C-style cast. */
7006 convert_ptrmem (tree type
, tree expr
, bool allow_inverse_p
,
7007 bool c_cast_p
, tsubst_flags_t complain
)
7009 if (same_type_p (type
, TREE_TYPE (expr
)))
7012 if (TYPE_PTRDATAMEM_P (type
))
7014 tree obase
= TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr
));
7015 tree nbase
= TYPE_PTRMEM_CLASS_TYPE (type
);
7016 tree delta
= (get_delta_difference
7018 allow_inverse_p
, c_cast_p
, complain
));
7020 if (delta
== error_mark_node
)
7021 return error_mark_node
;
7023 if (!same_type_p (obase
, nbase
))
7025 if (TREE_CODE (expr
) == PTRMEM_CST
)
7026 expr
= cplus_expand_constant (expr
);
7028 tree cond
= cp_build_binary_op (input_location
, EQ_EXPR
, expr
,
7029 build_int_cst (TREE_TYPE (expr
), -1),
7031 tree op1
= build_nop (ptrdiff_type_node
, expr
);
7032 tree op2
= cp_build_binary_op (input_location
, PLUS_EXPR
, op1
, delta
,
7035 expr
= fold_build3_loc (input_location
,
7036 COND_EXPR
, ptrdiff_type_node
, cond
, op1
, op2
);
7039 return build_nop (type
, expr
);
7042 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
,
7043 allow_inverse_p
, c_cast_p
, complain
);
7046 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
7047 this static_cast is being attempted as one of the possible casts
7048 allowed by a C-style cast. (In that case, accessibility of base
7049 classes is not considered, and it is OK to cast away
7050 constness.) Return the result of the cast. *VALID_P is set to
7051 indicate whether or not the cast was valid. */
7054 build_static_cast_1 (tree type
, tree expr
, bool c_cast_p
,
7055 bool *valid_p
, tsubst_flags_t complain
)
7061 /* Assume the cast is valid. */
7064 intype
= unlowered_expr_type (expr
);
7066 /* Save casted types in the function's used types hash table. */
7067 used_types_insert (type
);
7069 /* A prvalue of non-class type is cv-unqualified. */
7070 if (!CLASS_TYPE_P (type
))
7071 type
= cv_unqualified (type
);
7073 /* [expr.static.cast]
7075 An lvalue of type "cv1 B", where B is a class type, can be cast
7076 to type "reference to cv2 D", where D is a class derived (clause
7077 _class.derived_) from B, if a valid standard conversion from
7078 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
7079 same cv-qualification as, or greater cv-qualification than, cv1,
7080 and B is not a virtual base class of D. */
7081 /* We check this case before checking the validity of "TYPE t =
7082 EXPR;" below because for this case:
7085 struct D : public B { D(const B&); };
7087 void f() { static_cast<const D&>(b); }
7089 we want to avoid constructing a new D. The standard is not
7090 completely clear about this issue, but our interpretation is
7091 consistent with other compilers. */
7092 if (TYPE_REF_P (type
)
7093 && CLASS_TYPE_P (TREE_TYPE (type
))
7094 && CLASS_TYPE_P (intype
)
7095 && (TYPE_REF_IS_RVALUE (type
) || lvalue_p (expr
))
7096 && DERIVED_FROM_P (intype
, TREE_TYPE (type
))
7097 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype
)),
7098 build_pointer_type (TYPE_MAIN_VARIANT
7099 (TREE_TYPE (type
))),
7102 || at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
7106 if (processing_template_decl
)
7109 /* There is a standard conversion from "D*" to "B*" even if "B"
7110 is ambiguous or inaccessible. If this is really a
7111 static_cast, then we check both for inaccessibility and
7112 ambiguity. However, if this is a static_cast being performed
7113 because the user wrote a C-style cast, then accessibility is
7115 base
= lookup_base (TREE_TYPE (type
), intype
,
7116 c_cast_p
? ba_unique
: ba_check
,
7118 expr
= cp_build_addr_expr (expr
, complain
);
7120 if (sanitize_flags_p (SANITIZE_VPTR
))
7123 = cp_ubsan_maybe_instrument_downcast (input_location
, type
,
7129 /* Convert from "B*" to "D*". This function will check that "B"
7130 is not a virtual base of "D". Even if we don't have a guarantee
7131 that expr is NULL, if the static_cast is to a reference type,
7132 it is UB if it would be NULL, so omit the non-NULL check. */
7133 expr
= build_base_path (MINUS_EXPR
, expr
, base
,
7134 /*nonnull=*/flag_delete_null_pointer_checks
,
7137 /* Convert the pointer to a reference -- but then remember that
7138 there are no expressions with reference type in C++.
7140 We call rvalue so that there's an actual tree code
7141 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
7142 is a variable with the same type, the conversion would get folded
7143 away, leaving just the variable and causing lvalue_kind to give
7144 the wrong answer. */
7145 expr
= cp_fold_convert (type
, expr
);
7147 /* When -fsanitize=null, make sure to diagnose reference binding to
7148 NULL even when the reference is converted to pointer later on. */
7149 if (sanitize_flags_p (SANITIZE_NULL
)
7150 && TREE_CODE (expr
) == COND_EXPR
7151 && TREE_OPERAND (expr
, 2)
7152 && TREE_CODE (TREE_OPERAND (expr
, 2)) == INTEGER_CST
7153 && TREE_TYPE (TREE_OPERAND (expr
, 2)) == type
)
7154 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr
, 2));
7156 return convert_from_reference (rvalue (expr
));
7159 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
7160 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
7161 if (TYPE_REF_P (type
)
7162 && TYPE_REF_IS_RVALUE (type
)
7163 && (clk
= real_lvalue_p (expr
))
7164 && reference_related_p (TREE_TYPE (type
), intype
)
7165 && (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
7167 if (processing_template_decl
)
7169 if (clk
== clk_ordinary
)
7171 /* Handle the (non-bit-field) lvalue case here by casting to
7172 lvalue reference and then changing it to an rvalue reference.
7173 Casting an xvalue to rvalue reference will be handled by the
7175 tree lref
= cp_build_reference_type (TREE_TYPE (type
), false);
7176 result
= (perform_direct_initialization_if_possible
7177 (lref
, expr
, c_cast_p
, complain
));
7178 result
= build1 (NON_LVALUE_EXPR
, type
, result
);
7179 return convert_from_reference (result
);
7182 /* For a bit-field or packed field, bind to a temporary. */
7183 expr
= rvalue (expr
);
7186 /* Resolve overloaded address here rather than once in
7187 implicit_conversion and again in the inverse code below. */
7188 if (TYPE_PTRMEMFUNC_P (type
) && type_unknown_p (expr
))
7190 expr
= instantiate_type (type
, expr
, complain
);
7191 intype
= TREE_TYPE (expr
);
7194 /* [expr.static.cast]
7196 Any expression can be explicitly converted to type cv void. */
7197 if (VOID_TYPE_P (type
))
7198 return convert_to_void (expr
, ICV_CAST
, complain
);
7201 An abstract class shall not be used ... as the type of an explicit
7203 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
7204 return error_mark_node
;
7206 /* [expr.static.cast]
7208 An expression e can be explicitly converted to a type T using a
7209 static_cast of the form static_cast<T>(e) if the declaration T
7210 t(e);" is well-formed, for some invented temporary variable
7212 result
= perform_direct_initialization_if_possible (type
, expr
,
7213 c_cast_p
, complain
);
7216 if (processing_template_decl
)
7219 result
= convert_from_reference (result
);
7221 /* [expr.static.cast]
7223 If T is a reference type, the result is an lvalue; otherwise,
7224 the result is an rvalue. */
7225 if (!TYPE_REF_P (type
))
7227 result
= rvalue (result
);
7229 if (result
== expr
&& SCALAR_TYPE_P (type
))
7230 /* Leave some record of the cast. */
7231 result
= build_nop (type
, expr
);
7236 /* [expr.static.cast]
7238 The inverse of any standard conversion sequence (clause _conv_),
7239 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
7240 (_conv.array_), function-to-pointer (_conv.func_), and boolean
7241 (_conv.bool_) conversions, can be performed explicitly using
7242 static_cast subject to the restriction that the explicit
7243 conversion does not cast away constness (_expr.const.cast_), and
7244 the following additional rules for specific cases: */
7245 /* For reference, the conversions not excluded are: integral
7246 promotions, floating point promotion, integral conversions,
7247 floating point conversions, floating-integral conversions,
7248 pointer conversions, and pointer to member conversions. */
7251 A value of integral _or enumeration_ type can be explicitly
7252 converted to an enumeration type. */
7253 /* The effect of all that is that any conversion between any two
7254 types which are integral, floating, or enumeration types can be
7256 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7257 || SCALAR_FLOAT_TYPE_P (type
))
7258 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
7259 || SCALAR_FLOAT_TYPE_P (intype
)))
7261 if (processing_template_decl
)
7263 return ocp_convert (type
, expr
, CONV_C_CAST
, LOOKUP_NORMAL
, complain
);
7266 if (TYPE_PTR_P (type
) && TYPE_PTR_P (intype
)
7267 && CLASS_TYPE_P (TREE_TYPE (type
))
7268 && CLASS_TYPE_P (TREE_TYPE (intype
))
7269 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7270 (TREE_TYPE (intype
))),
7271 build_pointer_type (TYPE_MAIN_VARIANT
7272 (TREE_TYPE (type
))),
7277 if (processing_template_decl
)
7281 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
7283 return error_mark_node
;
7284 base
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
7285 c_cast_p
? ba_unique
: ba_check
,
7287 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
7290 if (sanitize_flags_p (SANITIZE_VPTR
))
7293 = cp_ubsan_maybe_instrument_downcast (input_location
, type
,
7299 return cp_fold_convert (type
, expr
);
7302 if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
7303 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
7310 c1
= TYPE_PTRMEM_CLASS_TYPE (intype
);
7311 c2
= TYPE_PTRMEM_CLASS_TYPE (type
);
7313 if (TYPE_PTRDATAMEM_P (type
))
7315 t1
= (build_ptrmem_type
7317 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype
))));
7318 t2
= (build_ptrmem_type
7320 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
7327 if (can_convert (t1
, t2
, complain
) || can_convert (t2
, t1
, complain
))
7330 && check_for_casting_away_constness (intype
, type
,
7333 return error_mark_node
;
7334 if (processing_template_decl
)
7336 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/1,
7337 c_cast_p
, complain
);
7341 /* [expr.static.cast]
7343 An rvalue of type "pointer to cv void" can be explicitly
7344 converted to a pointer to object type. A value of type pointer
7345 to object converted to "pointer to cv void" and back to the
7346 original pointer type will have its original value. */
7347 if (TYPE_PTR_P (intype
)
7348 && VOID_TYPE_P (TREE_TYPE (intype
))
7349 && TYPE_PTROB_P (type
))
7352 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
7354 return error_mark_node
;
7355 if (processing_template_decl
)
7357 return build_nop (type
, expr
);
7361 return error_mark_node
;
7364 /* Return an expression representing static_cast<TYPE>(EXPR). */
7367 build_static_cast (tree type
, tree oexpr
, tsubst_flags_t complain
)
7373 if (type
== error_mark_node
|| expr
== error_mark_node
)
7374 return error_mark_node
;
7376 bool dependent
= (dependent_type_p (type
)
7377 || type_dependent_expression_p (expr
));
7381 expr
= build_min (STATIC_CAST_EXPR
, type
, oexpr
);
7382 /* We don't know if it will or will not have side effects. */
7383 TREE_SIDE_EFFECTS (expr
) = 1;
7384 return convert_from_reference (expr
);
7386 else if (processing_template_decl
)
7387 expr
= build_non_dependent_expr (expr
);
7389 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7390 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7391 if (!TYPE_REF_P (type
)
7392 && TREE_CODE (expr
) == NOP_EXPR
7393 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7394 expr
= TREE_OPERAND (expr
, 0);
7396 result
= build_static_cast_1 (type
, expr
, /*c_cast_p=*/false, &valid_p
,
7400 if (result
!= error_mark_node
)
7402 maybe_warn_about_useless_cast (type
, expr
, complain
);
7403 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7405 if (processing_template_decl
)
7410 if (complain
& tf_error
)
7412 error ("invalid static_cast from type %qT to type %qT",
7413 TREE_TYPE (expr
), type
);
7414 if ((TYPE_PTR_P (type
) || TYPE_REF_P (type
))
7415 && CLASS_TYPE_P (TREE_TYPE (type
))
7416 && !COMPLETE_TYPE_P (TREE_TYPE (type
)))
7417 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type
))),
7418 "class type %qT is incomplete", TREE_TYPE (type
));
7419 tree expr_type
= TREE_TYPE (expr
);
7420 if (TYPE_PTR_P (expr_type
))
7421 expr_type
= TREE_TYPE (expr_type
);
7422 if (CLASS_TYPE_P (expr_type
) && !COMPLETE_TYPE_P (expr_type
))
7423 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type
)),
7424 "class type %qT is incomplete", expr_type
);
7426 return error_mark_node
;
7429 /* EXPR is an expression with member function or pointer-to-member
7430 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7431 not permitted by ISO C++, but we accept it in some modes. If we
7432 are not in one of those modes, issue a diagnostic. Return the
7433 converted expression. */
7436 convert_member_func_to_ptr (tree type
, tree expr
, tsubst_flags_t complain
)
7441 intype
= TREE_TYPE (expr
);
7442 gcc_assert (TYPE_PTRMEMFUNC_P (intype
)
7443 || TREE_CODE (intype
) == METHOD_TYPE
);
7445 if (!(complain
& tf_warning_or_error
))
7446 return error_mark_node
;
7448 if (pedantic
|| warn_pmf2ptr
)
7449 pedwarn (input_location
, pedantic
? OPT_Wpedantic
: OPT_Wpmf_conversions
,
7450 "converting from %qH to %qI", intype
, type
);
7452 if (TREE_CODE (intype
) == METHOD_TYPE
)
7453 expr
= build_addr_func (expr
, complain
);
7454 else if (TREE_CODE (expr
) == PTRMEM_CST
)
7455 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
7458 decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
), 0);
7459 decl
= build_address (decl
);
7460 expr
= get_member_function_from_ptrfunc (&decl
, expr
, complain
);
7463 if (expr
== error_mark_node
)
7464 return error_mark_node
;
7466 return build_nop (type
, expr
);
7469 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
7470 constexpr evaluation knows to reject it. */
7473 build_nop_reinterpret (tree type
, tree expr
)
7475 tree ret
= build_nop (type
, expr
);
7477 REINTERPRET_CAST_P (ret
) = true;
7481 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7482 If C_CAST_P is true, this reinterpret cast is being done as part of
7483 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7484 indicate whether or not reinterpret_cast was valid. */
7487 build_reinterpret_cast_1 (tree type
, tree expr
, bool c_cast_p
,
7488 bool *valid_p
, tsubst_flags_t complain
)
7492 /* Assume the cast is invalid. */
7496 if (type
== error_mark_node
|| error_operand_p (expr
))
7497 return error_mark_node
;
7499 intype
= TREE_TYPE (expr
);
7501 /* Save casted types in the function's used types hash table. */
7502 used_types_insert (type
);
7504 /* A prvalue of non-class type is cv-unqualified. */
7505 if (!CLASS_TYPE_P (type
))
7506 type
= cv_unqualified (type
);
7508 /* [expr.reinterpret.cast]
7509 A glvalue expression of type T1 can be cast to the type
7510 "reference to T2" if an expression of type "pointer to T1" can be
7511 explicitly converted to the type "pointer to T2" using a
7512 reinterpret_cast. */
7513 if (TYPE_REF_P (type
))
7515 if (TYPE_REF_IS_RVALUE (type
) && !VOID_TYPE_P (intype
))
7517 if (!obvalue_p (expr
))
7518 /* Perform the temporary materialization conversion. */
7519 expr
= get_target_expr_sfinae (expr
, complain
);
7521 else if (!lvalue_p (expr
))
7523 if (complain
& tf_error
)
7524 error ("invalid cast of an rvalue expression of type "
7527 return error_mark_node
;
7530 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7531 "B" are related class types; the reinterpret_cast does not
7532 adjust the pointer. */
7533 if (TYPE_PTR_P (intype
)
7534 && (complain
& tf_warning
)
7535 && (comptypes (TREE_TYPE (intype
), TREE_TYPE (type
),
7536 COMPARE_BASE
| COMPARE_DERIVED
)))
7537 warning (0, "casting %qT to %qT does not dereference pointer",
7540 expr
= cp_build_addr_expr (expr
, complain
);
7542 if (warn_strict_aliasing
> 2)
7543 strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
7545 if (expr
!= error_mark_node
)
7546 expr
= build_reinterpret_cast_1
7547 (build_pointer_type (TREE_TYPE (type
)), expr
, c_cast_p
,
7549 if (expr
!= error_mark_node
)
7550 /* cp_build_indirect_ref isn't right for rvalue refs. */
7551 expr
= convert_from_reference (fold_convert (type
, expr
));
7555 /* As a G++ extension, we consider conversions from member
7556 functions, and pointers to member functions to
7557 pointer-to-function and pointer-to-void types. If
7558 -Wno-pmf-conversions has not been specified,
7559 convert_member_func_to_ptr will issue an error message. */
7560 if ((TYPE_PTRMEMFUNC_P (intype
)
7561 || TREE_CODE (intype
) == METHOD_TYPE
)
7562 && TYPE_PTR_P (type
)
7563 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
7564 || VOID_TYPE_P (TREE_TYPE (type
))))
7565 return convert_member_func_to_ptr (type
, expr
, complain
);
7567 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7568 array-to-pointer, and function-to-pointer conversions are
7570 expr
= decay_conversion (expr
, complain
);
7572 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7573 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7574 if (TREE_CODE (expr
) == NOP_EXPR
7575 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7576 expr
= TREE_OPERAND (expr
, 0);
7578 if (error_operand_p (expr
))
7579 return error_mark_node
;
7581 intype
= TREE_TYPE (expr
);
7583 /* [expr.reinterpret.cast]
7584 A pointer can be converted to any integral type large enough to
7585 hold it. ... A value of type std::nullptr_t can be converted to
7586 an integral type; the conversion has the same meaning and
7587 validity as a conversion of (void*)0 to the integral type. */
7588 if (CP_INTEGRAL_TYPE_P (type
)
7589 && (TYPE_PTR_P (intype
) || NULLPTR_TYPE_P (intype
)))
7591 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
7593 if (complain
& tf_error
)
7594 permerror (input_location
, "cast from %qH to %qI loses precision",
7597 return error_mark_node
;
7599 if (NULLPTR_TYPE_P (intype
))
7600 return build_int_cst (type
, 0);
7602 /* [expr.reinterpret.cast]
7603 A value of integral or enumeration type can be explicitly
7604 converted to a pointer. */
7605 else if (TYPE_PTR_P (type
) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype
))
7608 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7609 || TYPE_PTR_OR_PTRMEM_P (type
))
7610 && same_type_p (type
, intype
))
7612 return rvalue (expr
);
7613 else if (TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
7615 if ((complain
& tf_warning
)
7616 && !cxx_safe_function_type_cast_p (TREE_TYPE (type
),
7617 TREE_TYPE (intype
)))
7618 warning (OPT_Wcast_function_type
,
7619 "cast between incompatible function types"
7620 " from %qH to %qI", intype
, type
);
7621 return build_nop_reinterpret (type
, expr
);
7623 else if (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
))
7625 if ((complain
& tf_warning
)
7626 && !cxx_safe_function_type_cast_p
7627 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type
)),
7628 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype
))))
7629 warning (OPT_Wcast_function_type
,
7630 "cast between incompatible pointer to member types"
7631 " from %qH to %qI", intype
, type
);
7632 return build_nop_reinterpret (type
, expr
);
7634 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
7635 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
7638 && check_for_casting_away_constness (intype
, type
,
7639 REINTERPRET_CAST_EXPR
,
7641 return error_mark_node
;
7642 /* Warn about possible alignment problems. */
7643 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
7644 && (complain
& tf_warning
)
7645 && !VOID_TYPE_P (type
)
7646 && TREE_CODE (TREE_TYPE (intype
)) != FUNCTION_TYPE
7647 && COMPLETE_TYPE_P (TREE_TYPE (type
))
7648 && COMPLETE_TYPE_P (TREE_TYPE (intype
))
7649 && min_align_of_type (TREE_TYPE (type
))
7650 > min_align_of_type (TREE_TYPE (intype
)))
7651 warning (OPT_Wcast_align
, "cast from %qH to %qI "
7652 "increases required alignment of target type", intype
, type
);
7654 if (warn_strict_aliasing
<= 2)
7655 /* strict_aliasing_warning STRIP_NOPs its expr. */
7656 strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
7658 return build_nop_reinterpret (type
, expr
);
7660 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
7661 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
7663 if (complain
& tf_warning
)
7664 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7665 object pointer type or vice versa is conditionally-supported." */
7666 warning (OPT_Wconditionally_supported
,
7667 "casting between pointer-to-function and pointer-to-object "
7668 "is conditionally-supported");
7669 return build_nop_reinterpret (type
, expr
);
7671 else if (VECTOR_TYPE_P (type
))
7672 return convert_to_vector (type
, expr
);
7673 else if (VECTOR_TYPE_P (intype
)
7674 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
7675 return convert_to_integer_nofold (type
, expr
);
7680 if (complain
& tf_error
)
7681 error ("invalid cast from type %qT to type %qT", intype
, type
);
7682 return error_mark_node
;
7685 expr
= cp_convert (type
, expr
, complain
);
7686 if (TREE_CODE (expr
) == NOP_EXPR
)
7687 /* Mark any nop_expr that created as a reintepret_cast. */
7688 REINTERPRET_CAST_P (expr
) = true;
7693 build_reinterpret_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7697 if (type
== error_mark_node
|| expr
== error_mark_node
)
7698 return error_mark_node
;
7700 if (processing_template_decl
)
7702 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
7704 if (!TREE_SIDE_EFFECTS (t
)
7705 && type_dependent_expression_p (expr
))
7706 /* There might turn out to be side effects inside expr. */
7707 TREE_SIDE_EFFECTS (t
) = 1;
7708 return convert_from_reference (t
);
7711 r
= build_reinterpret_cast_1 (type
, expr
, /*c_cast_p=*/false,
7712 /*valid_p=*/NULL
, complain
);
7713 if (r
!= error_mark_node
)
7715 maybe_warn_about_useless_cast (type
, expr
, complain
);
7716 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7721 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7722 return an appropriate expression. Otherwise, return
7723 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7724 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7725 performing a C-style cast, its value upon return will indicate
7726 whether or not the conversion succeeded. */
7729 build_const_cast_1 (tree dst_type
, tree expr
, tsubst_flags_t complain
,
7733 tree reference_type
;
7735 /* Callers are responsible for handling error_mark_node as a
7736 destination type. */
7737 gcc_assert (dst_type
!= error_mark_node
);
7738 /* In a template, callers should be building syntactic
7739 representations of casts, not using this machinery. */
7740 gcc_assert (!processing_template_decl
);
7742 /* Assume the conversion is invalid. */
7746 if (!INDIRECT_TYPE_P (dst_type
) && !TYPE_PTRDATAMEM_P (dst_type
))
7748 if (complain
& tf_error
)
7749 error ("invalid use of const_cast with type %qT, "
7750 "which is not a pointer, "
7751 "reference, nor a pointer-to-data-member type", dst_type
);
7752 return error_mark_node
;
7755 if (TREE_CODE (TREE_TYPE (dst_type
)) == FUNCTION_TYPE
)
7757 if (complain
& tf_error
)
7758 error ("invalid use of const_cast with type %qT, which is a pointer "
7759 "or reference to a function type", dst_type
);
7760 return error_mark_node
;
7763 /* A prvalue of non-class type is cv-unqualified. */
7764 dst_type
= cv_unqualified (dst_type
);
7766 /* Save casted types in the function's used types hash table. */
7767 used_types_insert (dst_type
);
7769 src_type
= TREE_TYPE (expr
);
7770 /* Expressions do not really have reference types. */
7771 if (TYPE_REF_P (src_type
))
7772 src_type
= TREE_TYPE (src_type
);
7774 /* [expr.const.cast]
7776 For two object types T1 and T2, if a pointer to T1 can be explicitly
7777 converted to the type "pointer to T2" using a const_cast, then the
7778 following conversions can also be made:
7780 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7781 type T2 using the cast const_cast<T2&>;
7783 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7784 type T2 using the cast const_cast<T2&&>; and
7786 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7787 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7789 if (TYPE_REF_P (dst_type
))
7791 reference_type
= dst_type
;
7792 if (!TYPE_REF_IS_RVALUE (dst_type
)
7798 if (complain
& tf_error
)
7799 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7800 src_type
, dst_type
);
7801 return error_mark_node
;
7803 dst_type
= build_pointer_type (TREE_TYPE (dst_type
));
7804 src_type
= build_pointer_type (src_type
);
7808 reference_type
= NULL_TREE
;
7809 /* If the destination type is not a reference type, the
7810 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7811 conversions are performed. */
7812 src_type
= type_decays_to (src_type
);
7813 if (src_type
== error_mark_node
)
7814 return error_mark_node
;
7817 if (TYPE_PTR_P (src_type
) || TYPE_PTRDATAMEM_P (src_type
))
7819 if (comp_ptr_ttypes_const (dst_type
, src_type
))
7824 /* This cast is actually a C-style cast. Issue a warning if
7825 the user is making a potentially unsafe cast. */
7826 check_for_casting_away_constness (src_type
, dst_type
,
7827 CAST_EXPR
, complain
);
7828 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
7829 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
7830 && (complain
& tf_warning
)
7831 && min_align_of_type (TREE_TYPE (dst_type
))
7832 > min_align_of_type (TREE_TYPE (src_type
)))
7833 warning (OPT_Wcast_align
, "cast from %qH to %qI "
7834 "increases required alignment of target type",
7835 src_type
, dst_type
);
7839 expr
= cp_build_addr_expr (expr
, complain
);
7840 if (expr
== error_mark_node
)
7841 return error_mark_node
;
7842 expr
= build_nop (reference_type
, expr
);
7843 return convert_from_reference (expr
);
7847 expr
= decay_conversion (expr
, complain
);
7848 if (expr
== error_mark_node
)
7849 return error_mark_node
;
7851 /* build_c_cast puts on a NOP_EXPR to make the result not an
7852 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7853 non-lvalue context. */
7854 if (TREE_CODE (expr
) == NOP_EXPR
7855 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7856 expr
= TREE_OPERAND (expr
, 0);
7857 return build_nop (dst_type
, expr
);
7861 && !at_least_as_qualified_p (TREE_TYPE (dst_type
),
7862 TREE_TYPE (src_type
)))
7863 check_for_casting_away_constness (src_type
, dst_type
, CAST_EXPR
,
7867 if (complain
& tf_error
)
7868 error ("invalid const_cast from type %qT to type %qT",
7869 src_type
, dst_type
);
7870 return error_mark_node
;
7874 build_const_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7878 if (type
== error_mark_node
|| error_operand_p (expr
))
7879 return error_mark_node
;
7881 if (processing_template_decl
)
7883 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
7885 if (!TREE_SIDE_EFFECTS (t
)
7886 && type_dependent_expression_p (expr
))
7887 /* There might turn out to be side effects inside expr. */
7888 TREE_SIDE_EFFECTS (t
) = 1;
7889 return convert_from_reference (t
);
7892 r
= build_const_cast_1 (type
, expr
, complain
, /*valid_p=*/NULL
);
7893 if (r
!= error_mark_node
)
7895 maybe_warn_about_useless_cast (type
, expr
, complain
);
7896 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7901 /* Like cp_build_c_cast, but for the c-common bits. */
7904 build_c_cast (location_t
/*loc*/, tree type
, tree expr
)
7906 return cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7909 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7910 preserve location information even for tree nodes that don't
7914 build_c_cast (location_t loc
, tree type
, cp_expr expr
)
7916 cp_expr result
= cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7917 result
.set_location (loc
);
7921 /* Build an expression representing an explicit C-style cast to type
7922 TYPE of expression EXPR. */
7925 cp_build_c_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7931 if (type
== error_mark_node
|| error_operand_p (expr
))
7932 return error_mark_node
;
7934 if (processing_template_decl
)
7936 tree t
= build_min (CAST_EXPR
, type
,
7937 tree_cons (NULL_TREE
, value
, NULL_TREE
));
7938 /* We don't know if it will or will not have side effects. */
7939 TREE_SIDE_EFFECTS (t
) = 1;
7940 return convert_from_reference (t
);
7943 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7944 'Class') should always be retained, because this information aids
7945 in method lookup. */
7946 if (objc_is_object_ptr (type
)
7947 && objc_is_object_ptr (TREE_TYPE (expr
)))
7948 return build_nop (type
, expr
);
7950 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7951 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7952 if (!TYPE_REF_P (type
)
7953 && TREE_CODE (value
) == NOP_EXPR
7954 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
7955 value
= TREE_OPERAND (value
, 0);
7957 if (TREE_CODE (type
) == ARRAY_TYPE
)
7959 /* Allow casting from T1* to T2[] because Cfront allows it.
7960 NIHCL uses it. It is not valid ISO C++ however. */
7961 if (TYPE_PTR_P (TREE_TYPE (expr
)))
7963 if (complain
& tf_error
)
7964 permerror (input_location
, "ISO C++ forbids casting to an array type %qT", type
);
7966 return error_mark_node
;
7967 type
= build_pointer_type (TREE_TYPE (type
));
7971 if (complain
& tf_error
)
7972 error ("ISO C++ forbids casting to an array type %qT", type
);
7973 return error_mark_node
;
7977 if (TREE_CODE (type
) == FUNCTION_TYPE
7978 || TREE_CODE (type
) == METHOD_TYPE
)
7980 if (complain
& tf_error
)
7981 error ("invalid cast to function type %qT", type
);
7982 return error_mark_node
;
7985 if (TYPE_PTR_P (type
)
7986 && TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
7987 /* Casting to an integer of smaller size is an error detected elsewhere. */
7988 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (value
))
7989 /* Don't warn about converting any constant. */
7990 && !TREE_CONSTANT (value
))
7991 warning_at (input_location
, OPT_Wint_to_pointer_cast
,
7992 "cast to pointer from integer of different size");
7994 /* A C-style cast can be a const_cast. */
7995 result
= build_const_cast_1 (type
, value
, complain
& tf_warning
,
7999 if (result
!= error_mark_node
)
8001 maybe_warn_about_useless_cast (type
, value
, complain
);
8002 maybe_warn_about_cast_ignoring_quals (type
, complain
);
8007 /* Or a static cast. */
8008 result
= build_static_cast_1 (type
, value
, /*c_cast_p=*/true,
8009 &valid_p
, complain
);
8010 /* Or a reinterpret_cast. */
8012 result
= build_reinterpret_cast_1 (type
, value
, /*c_cast_p=*/true,
8013 &valid_p
, complain
);
8014 /* The static_cast or reinterpret_cast may be followed by a
8017 /* A valid cast may result in errors if, for example, a
8018 conversion to an ambiguous base class is required. */
8019 && !error_operand_p (result
))
8023 maybe_warn_about_useless_cast (type
, value
, complain
);
8024 maybe_warn_about_cast_ignoring_quals (type
, complain
);
8026 /* Non-class rvalues always have cv-unqualified type. */
8027 if (!CLASS_TYPE_P (type
))
8028 type
= TYPE_MAIN_VARIANT (type
);
8029 result_type
= TREE_TYPE (result
);
8030 if (!CLASS_TYPE_P (result_type
) && !TYPE_REF_P (type
))
8031 result_type
= TYPE_MAIN_VARIANT (result_type
);
8032 /* If the type of RESULT does not match TYPE, perform a
8033 const_cast to make it match. If the static_cast or
8034 reinterpret_cast succeeded, we will differ by at most
8035 cv-qualification, so the follow-on const_cast is guaranteed
8037 if (!same_type_p (non_reference (type
), non_reference (result_type
)))
8039 result
= build_const_cast_1 (type
, result
, false, &valid_p
);
8040 gcc_assert (valid_p
);
8045 return error_mark_node
;
8048 /* For use from the C common bits. */
8050 build_modify_expr (location_t location
,
8051 tree lhs
, tree
/*lhs_origtype*/,
8052 enum tree_code modifycode
,
8053 location_t
/*rhs_location*/, tree rhs
,
8054 tree
/*rhs_origtype*/)
8056 return cp_build_modify_expr (location
, lhs
, modifycode
, rhs
,
8057 tf_warning_or_error
);
8060 /* Build an assignment expression of lvalue LHS from value RHS.
8061 MODIFYCODE is the code for a binary operator that we use
8062 to combine the old value of LHS with RHS to get the new value.
8063 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
8065 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
8068 cp_build_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
8069 tree rhs
, tsubst_flags_t complain
)
8071 lhs
= mark_lvalue_use_nonread (lhs
);
8073 tree result
= NULL_TREE
;
8075 tree lhstype
= TREE_TYPE (lhs
);
8077 tree olhstype
= lhstype
;
8078 bool plain_assign
= (modifycode
== NOP_EXPR
);
8079 bool compound_side_effects_p
= false;
8080 tree preeval
= NULL_TREE
;
8082 /* Avoid duplicate error messages from operands that had errors. */
8083 if (error_operand_p (lhs
) || error_operand_p (rhs
))
8084 return error_mark_node
;
8086 while (TREE_CODE (lhs
) == COMPOUND_EXPR
)
8088 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
8089 compound_side_effects_p
= true;
8090 lhs
= TREE_OPERAND (lhs
, 1);
8093 /* Handle control structure constructs used as "lvalues". Note that we
8094 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
8095 switch (TREE_CODE (lhs
))
8097 /* Handle --foo = 5; as these are valid constructs in C++. */
8098 case PREDECREMENT_EXPR
:
8099 case PREINCREMENT_EXPR
:
8100 if (compound_side_effects_p
)
8101 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
8102 lhs
= genericize_compound_lvalue (lhs
);
8104 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
8105 and looked through the COMPOUND_EXPRs, readd them now around
8106 the resulting lhs. */
8107 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
8109 lhs
= build2 (COMPOUND_EXPR
, lhstype
, TREE_OPERAND (olhs
, 0), lhs
);
8110 tree
*ptr
= &TREE_OPERAND (lhs
, 1);
8111 for (olhs
= TREE_OPERAND (olhs
, 1);
8112 TREE_CODE (olhs
) == COMPOUND_EXPR
;
8113 olhs
= TREE_OPERAND (olhs
, 1))
8115 *ptr
= build2 (COMPOUND_EXPR
, lhstype
,
8116 TREE_OPERAND (olhs
, 0), *ptr
);
8117 ptr
= &TREE_OPERAND (*ptr
, 1);
8123 if (compound_side_effects_p
)
8124 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
8125 lhs
= genericize_compound_lvalue (lhs
);
8126 goto maybe_add_compound
;
8130 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
8131 when neither operand has side-effects. */
8132 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8133 return error_mark_node
;
8135 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0))
8136 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 1)));
8138 lhs
= build3 (COND_EXPR
, TREE_TYPE (lhs
),
8139 build2 (TREE_CODE (lhs
) == MIN_EXPR
? LE_EXPR
: GE_EXPR
,
8141 TREE_OPERAND (lhs
, 0),
8142 TREE_OPERAND (lhs
, 1)),
8143 TREE_OPERAND (lhs
, 0),
8144 TREE_OPERAND (lhs
, 1));
8147 /* Handle (a ? b : c) used as an "lvalue". */
8150 /* Produce (a ? (b = rhs) : (c = rhs))
8151 except that the RHS goes through a save-expr
8152 so the code to compute it is only emitted once. */
8153 if (VOID_TYPE_P (TREE_TYPE (rhs
)))
8155 if (complain
& tf_error
)
8156 error ("void value not ignored as it ought to be");
8157 return error_mark_node
;
8160 rhs
= stabilize_expr (rhs
, &preeval
);
8162 /* Check this here to avoid odd errors when trying to convert
8163 a throw to the type of the COND_EXPR. */
8164 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8165 return error_mark_node
;
8167 tree op1
= cp_build_modify_expr (loc
, TREE_OPERAND (lhs
, 1),
8168 modifycode
, rhs
, complain
);
8169 /* When sanitizing undefined behavior, even when rhs doesn't need
8170 stabilization at this point, the sanitization might add extra
8171 SAVE_EXPRs in there and so make sure there is no tree sharing
8172 in the rhs, otherwise those SAVE_EXPRs will have initialization
8173 only in one of the two branches. */
8174 if (sanitize_flags_p (SANITIZE_UNDEFINED
8175 | SANITIZE_UNDEFINED_NONDEFAULT
))
8176 rhs
= unshare_expr (rhs
);
8177 tree op2
= cp_build_modify_expr (loc
, TREE_OPERAND (lhs
, 2),
8178 modifycode
, rhs
, complain
);
8179 tree cond
= build_conditional_expr (input_location
,
8180 TREE_OPERAND (lhs
, 0), op1
, op2
,
8183 if (cond
== error_mark_node
)
8185 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
8186 and looked through the COMPOUND_EXPRs, readd them now around
8187 the resulting cond before adding the preevaluated rhs. */
8188 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
8190 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
8191 TREE_OPERAND (olhs
, 0), cond
);
8192 tree
*ptr
= &TREE_OPERAND (cond
, 1);
8193 for (olhs
= TREE_OPERAND (olhs
, 1);
8194 TREE_CODE (olhs
) == COMPOUND_EXPR
;
8195 olhs
= TREE_OPERAND (olhs
, 1))
8197 *ptr
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
8198 TREE_OPERAND (olhs
, 0), *ptr
);
8199 ptr
= &TREE_OPERAND (*ptr
, 1);
8202 /* Make sure the code to compute the rhs comes out
8203 before the split. */
8213 if (modifycode
== INIT_EXPR
)
8215 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
8216 /* Do the default thing. */;
8217 else if (TREE_CODE (rhs
) == CONSTRUCTOR
)
8219 /* Compound literal. */
8220 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
8221 /* Call convert to generate an error; see PR 11063. */
8222 rhs
= convert (lhstype
, rhs
);
8223 result
= build2 (INIT_EXPR
, lhstype
, lhs
, rhs
);
8224 TREE_SIDE_EFFECTS (result
) = 1;
8227 else if (! MAYBE_CLASS_TYPE_P (lhstype
))
8228 /* Do the default thing. */;
8231 vec
<tree
, va_gc
> *rhs_vec
= make_tree_vector_single (rhs
);
8232 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
8233 &rhs_vec
, lhstype
, LOOKUP_NORMAL
,
8235 release_tree_vector (rhs_vec
);
8236 if (result
== NULL_TREE
)
8237 return error_mark_node
;
8243 lhs
= require_complete_type_sfinae (lhs
, complain
);
8244 if (lhs
== error_mark_node
)
8245 return error_mark_node
;
8247 if (modifycode
== NOP_EXPR
)
8249 if (c_dialect_objc ())
8251 result
= objc_maybe_build_modify_expr (lhs
, rhs
);
8256 /* `operator=' is not an inheritable operator. */
8257 if (! MAYBE_CLASS_TYPE_P (lhstype
))
8258 /* Do the default thing. */;
8261 result
= build_new_op (input_location
, MODIFY_EXPR
,
8262 LOOKUP_NORMAL
, lhs
, rhs
,
8263 make_node (NOP_EXPR
), /*overload=*/NULL
,
8265 if (result
== NULL_TREE
)
8266 return error_mark_node
;
8273 tree init
= NULL_TREE
;
8275 /* A binary op has been requested. Combine the old LHS
8276 value with the RHS producing the value we should actually
8277 store into the LHS. */
8278 gcc_assert (!((TYPE_REF_P (lhstype
)
8279 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype
)))
8280 || MAYBE_CLASS_TYPE_P (lhstype
)));
8282 /* Preevaluate the RHS to make sure its evaluation is complete
8283 before the lvalue-to-rvalue conversion of the LHS:
8285 [expr.ass] With respect to an indeterminately-sequenced
8286 function call, the operation of a compound assignment is a
8287 single evaluation. [ Note: Therefore, a function call shall
8288 not intervene between the lvalue-to-rvalue conversion and the
8289 side effect associated with any single compound assignment
8290 operator. -- end note ] */
8291 lhs
= cp_stabilize_reference (lhs
);
8292 rhs
= decay_conversion (rhs
, complain
);
8293 if (rhs
== error_mark_node
)
8294 return error_mark_node
;
8295 rhs
= stabilize_expr (rhs
, &init
);
8296 newrhs
= cp_build_binary_op (loc
, modifycode
, lhs
, rhs
, complain
);
8297 if (newrhs
== error_mark_node
)
8299 if (complain
& tf_error
)
8300 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode
,
8301 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
8302 return error_mark_node
;
8306 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), init
, newrhs
);
8308 /* Now it looks like a plain assignment. */
8309 modifycode
= NOP_EXPR
;
8310 if (c_dialect_objc ())
8312 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
8317 gcc_assert (!TYPE_REF_P (lhstype
));
8318 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs
)));
8321 /* The left-hand side must be an lvalue. */
8322 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8323 return error_mark_node
;
8325 /* Warn about modifying something that is `const'. Don't warn if
8326 this is initialization. */
8327 if (modifycode
!= INIT_EXPR
8328 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
8329 /* Functions are not modifiable, even though they are
8331 || TREE_CODE (TREE_TYPE (lhs
)) == FUNCTION_TYPE
8332 || TREE_CODE (TREE_TYPE (lhs
)) == METHOD_TYPE
8333 /* If it's an aggregate and any field is const, then it is
8334 effectively const. */
8335 || (CLASS_TYPE_P (lhstype
)
8336 && C_TYPE_FIELDS_READONLY (lhstype
))))
8338 if (complain
& tf_error
)
8339 cxx_readonly_error (loc
, lhs
, lv_assign
);
8340 return error_mark_node
;
8343 /* If storing into a structure or union member, it may have been given a
8344 lowered bitfield type. We need to convert to the declared type first,
8345 so retrieve it now. */
8347 olhstype
= unlowered_expr_type (lhs
);
8349 /* Convert new value to destination type. */
8351 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
8355 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs
))
8357 if (modifycode
!= INIT_EXPR
)
8359 if (complain
& tf_error
)
8360 error ("assigning to an array from an initializer list");
8361 return error_mark_node
;
8363 if (check_array_initializer (lhs
, lhstype
, newrhs
))
8364 return error_mark_node
;
8365 newrhs
= digest_init (lhstype
, newrhs
, complain
);
8366 if (newrhs
== error_mark_node
)
8367 return error_mark_node
;
8370 /* C++11 8.5/17: "If the destination type is an array of characters,
8371 an array of char16_t, an array of char32_t, or an array of wchar_t,
8372 and the initializer is a string literal...". */
8373 else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs
))
8375 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype
)))
8376 && modifycode
== INIT_EXPR
)
8378 newrhs
= digest_init (lhstype
, newrhs
, complain
);
8379 if (newrhs
== error_mark_node
)
8380 return error_mark_node
;
8383 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
8384 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))))
8386 if (complain
& tf_error
)
8387 error ("incompatible types in assignment of %qT to %qT",
8388 TREE_TYPE (rhs
), lhstype
);
8389 return error_mark_node
;
8392 /* Allow array assignment in compiler-generated code. */
8393 else if (!current_function_decl
8394 || !DECL_DEFAULTED_FN (current_function_decl
))
8396 /* This routine is used for both initialization and assignment.
8397 Make sure the diagnostic message differentiates the context. */
8398 if (complain
& tf_error
)
8400 if (modifycode
== INIT_EXPR
)
8401 error ("array used as initializer");
8403 error ("invalid array assignment");
8405 return error_mark_node
;
8408 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
8409 ? 1 + (modifycode
!= INIT_EXPR
): 0;
8410 result
= build_vec_init (lhs
, NULL_TREE
, newrhs
,
8411 /*explicit_value_init_p=*/false,
8412 from_array
, complain
);
8416 if (modifycode
== INIT_EXPR
)
8417 /* Calls with INIT_EXPR are all direct-initialization, so don't set
8418 LOOKUP_ONLYCONVERTING. */
8419 newrhs
= convert_for_initialization (lhs
, olhstype
, newrhs
, LOOKUP_NORMAL
,
8420 ICR_INIT
, NULL_TREE
, 0,
8423 newrhs
= convert_for_assignment (olhstype
, newrhs
, ICR_ASSIGN
,
8424 NULL_TREE
, 0, complain
, LOOKUP_IMPLICIT
);
8426 if (!same_type_p (lhstype
, olhstype
))
8427 newrhs
= cp_convert_and_check (lhstype
, newrhs
, complain
);
8429 if (modifycode
!= INIT_EXPR
)
8431 if (TREE_CODE (newrhs
) == CALL_EXPR
8432 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
8433 newrhs
= build_cplus_new (lhstype
, newrhs
, complain
);
8435 /* Can't initialize directly from a TARGET_EXPR, since that would
8436 cause the lhs to be constructed twice, and possibly result in
8437 accidental self-initialization. So we force the TARGET_EXPR to be
8438 expanded without a target. */
8439 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
8440 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
8441 TREE_OPERAND (newrhs
, 0));
8444 if (newrhs
== error_mark_node
)
8445 return error_mark_node
;
8447 if (c_dialect_objc () && flag_objc_gc
)
8449 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
8455 result
= build2 (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
8456 lhstype
, lhs
, newrhs
);
8458 TREE_SIDE_EFFECTS (result
) = 1;
8460 TREE_NO_WARNING (result
) = 1;
8464 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), preeval
, result
);
8469 build_x_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
8470 tree rhs
, tsubst_flags_t complain
)
8472 tree orig_lhs
= lhs
;
8473 tree orig_rhs
= rhs
;
8474 tree overload
= NULL_TREE
;
8475 tree op
= build_nt (modifycode
, NULL_TREE
, NULL_TREE
);
8477 if (processing_template_decl
)
8479 if (modifycode
== NOP_EXPR
8480 || type_dependent_expression_p (lhs
)
8481 || type_dependent_expression_p (rhs
))
8482 return build_min_nt_loc (loc
, MODOP_EXPR
, lhs
,
8483 build_min_nt_loc (loc
, modifycode
, NULL_TREE
,
8486 lhs
= build_non_dependent_expr (lhs
);
8487 rhs
= build_non_dependent_expr (rhs
);
8490 if (modifycode
!= NOP_EXPR
)
8492 tree rval
= build_new_op (loc
, MODIFY_EXPR
, LOOKUP_NORMAL
,
8493 lhs
, rhs
, op
, &overload
, complain
);
8496 if (rval
== error_mark_node
)
8498 TREE_NO_WARNING (rval
) = 1;
8499 if (processing_template_decl
)
8501 if (overload
!= NULL_TREE
)
8502 return (build_min_non_dep_op_overload
8503 (MODIFY_EXPR
, rval
, overload
, orig_lhs
, orig_rhs
));
8505 return (build_min_non_dep
8506 (MODOP_EXPR
, rval
, orig_lhs
, op
, orig_rhs
));
8511 return cp_build_modify_expr (loc
, lhs
, modifycode
, rhs
, complain
);
8514 /* Helper function for get_delta_difference which assumes FROM is a base
8515 class of TO. Returns a delta for the conversion of pointer-to-member
8516 of FROM to pointer-to-member of TO. If the conversion is invalid and
8517 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8518 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8519 If C_CAST_P is true, this conversion is taking place as part of a
8523 get_delta_difference_1 (tree from
, tree to
, bool c_cast_p
,
8524 tsubst_flags_t complain
)
8529 binfo
= lookup_base (to
, from
, c_cast_p
? ba_unique
: ba_check
,
8532 if (binfo
== error_mark_node
)
8534 if (!(complain
& tf_error
))
8535 return error_mark_node
;
8537 error (" in pointer to member function conversion");
8538 return size_zero_node
;
8542 if (kind
!= bk_via_virtual
)
8543 return BINFO_OFFSET (binfo
);
8545 /* FROM is a virtual base class of TO. Issue an error or warning
8546 depending on whether or not this is a reinterpret cast. */
8548 if (!(complain
& tf_error
))
8549 return error_mark_node
;
8551 error ("pointer to member conversion via virtual base %qT",
8552 BINFO_TYPE (binfo_from_vbase (binfo
)));
8554 return size_zero_node
;
8561 /* Get difference in deltas for different pointer to member function
8562 types. If the conversion is invalid and tf_error is not set in
8563 COMPLAIN, returns error_mark_node, otherwise returns an integer
8564 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8565 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8566 conversions as well. If C_CAST_P is true this conversion is taking
8567 place as part of a C-style cast.
8569 Note that the naming of FROM and TO is kind of backwards; the return
8570 value is what we add to a TO in order to get a FROM. They are named
8571 this way because we call this function to find out how to convert from
8572 a pointer to member of FROM to a pointer to member of TO. */
8575 get_delta_difference (tree from
, tree to
,
8576 bool allow_inverse_p
,
8577 bool c_cast_p
, tsubst_flags_t complain
)
8581 if (same_type_ignoring_top_level_qualifiers_p (from
, to
))
8582 /* Pointer to member of incomplete class is permitted*/
8583 result
= size_zero_node
;
8585 result
= get_delta_difference_1 (from
, to
, c_cast_p
, complain
);
8587 if (result
== error_mark_node
)
8588 return error_mark_node
;
8592 if (!allow_inverse_p
)
8594 if (!(complain
& tf_error
))
8595 return error_mark_node
;
8597 error_not_base_type (from
, to
);
8598 error (" in pointer to member conversion");
8599 result
= size_zero_node
;
8603 result
= get_delta_difference_1 (to
, from
, c_cast_p
, complain
);
8605 if (result
== error_mark_node
)
8606 return error_mark_node
;
8609 result
= size_diffop_loc (input_location
,
8610 size_zero_node
, result
);
8613 if (!(complain
& tf_error
))
8614 return error_mark_node
;
8616 error_not_base_type (from
, to
);
8617 error (" in pointer to member conversion");
8618 result
= size_zero_node
;
8623 return convert_to_integer (ptrdiff_type_node
, result
);
8626 /* Return a constructor for the pointer-to-member-function TYPE using
8627 the other components as specified. */
8630 build_ptrmemfunc1 (tree type
, tree delta
, tree pfn
)
8635 vec
<constructor_elt
, va_gc
> *v
;
8637 /* Pull the FIELD_DECLs out of the type. */
8638 pfn_field
= TYPE_FIELDS (type
);
8639 delta_field
= DECL_CHAIN (pfn_field
);
8641 /* Make sure DELTA has the type we want. */
8642 delta
= convert_and_check (input_location
, delta_type_node
, delta
);
8644 /* Convert to the correct target type if necessary. */
8645 pfn
= fold_convert (TREE_TYPE (pfn_field
), pfn
);
8647 /* Finish creating the initializer. */
8649 CONSTRUCTOR_APPEND_ELT(v
, pfn_field
, pfn
);
8650 CONSTRUCTOR_APPEND_ELT(v
, delta_field
, delta
);
8651 u
= build_constructor (type
, v
);
8652 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) & TREE_CONSTANT (delta
);
8653 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
8654 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
8656 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
8661 /* Build a constructor for a pointer to member function. It can be
8662 used to initialize global variables, local variable, or used
8663 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8666 If FORCE is nonzero, then force this conversion, even if
8667 we would rather not do it. Usually set when using an explicit
8668 cast. A C-style cast is being processed iff C_CAST_P is true.
8670 Return error_mark_node, if something goes wrong. */
8673 build_ptrmemfunc (tree type
, tree pfn
, int force
, bool c_cast_p
,
8674 tsubst_flags_t complain
)
8680 if (error_operand_p (pfn
))
8681 return error_mark_node
;
8683 pfn_type
= TREE_TYPE (pfn
);
8684 to_type
= build_ptrmemfunc_type (type
);
8686 /* Handle multiple conversions of pointer to member functions. */
8687 if (TYPE_PTRMEMFUNC_P (pfn_type
))
8689 tree delta
= NULL_TREE
;
8690 tree npfn
= NULL_TREE
;
8694 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
,
8695 LOOKUP_NORMAL
, complain
))
8697 if (complain
& tf_error
)
8698 error ("invalid conversion to type %qT from type %qT",
8701 return error_mark_node
;
8704 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
8705 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
8707 c_cast_p
, complain
);
8708 if (n
== error_mark_node
)
8709 return error_mark_node
;
8711 /* We don't have to do any conversion to convert a
8712 pointer-to-member to its own type. But, we don't want to
8713 just return a PTRMEM_CST if there's an explicit cast; that
8714 cast should make the expression an invalid template argument. */
8715 if (TREE_CODE (pfn
) != PTRMEM_CST
)
8717 if (same_type_p (to_type
, pfn_type
))
8719 else if (integer_zerop (n
) && TREE_CODE (pfn
) != CONSTRUCTOR
)
8720 return build_reinterpret_cast (to_type
, pfn
,
8724 if (TREE_SIDE_EFFECTS (pfn
))
8725 pfn
= save_expr (pfn
);
8727 /* Obtain the function pointer and the current DELTA. */
8728 if (TREE_CODE (pfn
) == PTRMEM_CST
)
8729 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
8732 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
8733 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
8736 /* Just adjust the DELTA field. */
8737 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8738 (TREE_TYPE (delta
), ptrdiff_type_node
));
8739 if (!integer_zerop (n
))
8741 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
8742 n
= cp_build_binary_op (input_location
,
8743 LSHIFT_EXPR
, n
, integer_one_node
,
8745 delta
= cp_build_binary_op (input_location
,
8746 PLUS_EXPR
, delta
, n
, complain
);
8748 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
8751 /* Handle null pointer to member function conversions. */
8752 if (null_ptr_cst_p (pfn
))
8754 pfn
= cp_build_c_cast (type
, pfn
, complain
);
8755 return build_ptrmemfunc1 (to_type
,
8760 if (type_unknown_p (pfn
))
8761 return instantiate_type (type
, pfn
, complain
);
8763 fn
= TREE_OPERAND (pfn
, 0);
8764 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
8765 /* In a template, we will have preserved the
8767 || (processing_template_decl
&& TREE_CODE (fn
) == OFFSET_REF
));
8768 return make_ptrmem_cst (to_type
, fn
);
8771 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8774 ??? There is no consistency as to the types returned for the above
8775 values. Some code acts as if it were a sizetype and some as if it were
8776 integer_type_node. */
8779 expand_ptrmemfunc_cst (tree cst
, tree
*delta
, tree
*pfn
)
8781 tree type
= TREE_TYPE (cst
);
8782 tree fn
= PTRMEM_CST_MEMBER (cst
);
8783 tree ptr_class
, fn_class
;
8785 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
8787 /* The class that the function belongs to. */
8788 fn_class
= DECL_CONTEXT (fn
);
8790 /* The class that we're creating a pointer to member of. */
8791 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
8793 /* First, calculate the adjustment to the function's class. */
8794 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0,
8795 /*c_cast_p=*/0, tf_warning_or_error
);
8797 if (!DECL_VIRTUAL_P (fn
))
8798 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
),
8799 build_addr_func (fn
, tf_warning_or_error
));
8802 /* If we're dealing with a virtual function, we have to adjust 'this'
8803 again, to point to the base which provides the vtable entry for
8804 fn; the call will do the opposite adjustment. */
8805 tree orig_class
= DECL_CONTEXT (fn
);
8806 tree binfo
= binfo_or_else (orig_class
, fn_class
);
8807 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
8808 *delta
, BINFO_OFFSET (binfo
));
8810 /* We set PFN to the vtable offset at which the function can be
8811 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8812 case delta is shifted left, and then incremented). */
8813 *pfn
= DECL_VINDEX (fn
);
8814 *pfn
= fold_build2 (MULT_EXPR
, integer_type_node
, *pfn
,
8815 TYPE_SIZE_UNIT (vtable_entry_type
));
8817 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
8819 case ptrmemfunc_vbit_in_pfn
:
8820 *pfn
= fold_build2 (PLUS_EXPR
, integer_type_node
, *pfn
,
8824 case ptrmemfunc_vbit_in_delta
:
8825 *delta
= fold_build2 (LSHIFT_EXPR
, TREE_TYPE (*delta
),
8826 *delta
, integer_one_node
);
8827 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
8828 *delta
, integer_one_node
);
8835 *pfn
= fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), *pfn
);
8839 /* Return an expression for PFN from the pointer-to-member function
8843 pfn_from_ptrmemfunc (tree t
)
8845 if (TREE_CODE (t
) == PTRMEM_CST
)
8850 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
8855 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
8858 /* Return an expression for DELTA from the pointer-to-member function
8862 delta_from_ptrmemfunc (tree t
)
8864 if (TREE_CODE (t
) == PTRMEM_CST
)
8869 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
8874 return build_ptrmemfunc_access_expr (t
, delta_identifier
);
8877 /* Convert value RHS to type TYPE as preparation for an assignment to
8878 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8879 implicit conversion is. If FNDECL is non-NULL, we are doing the
8880 conversion in order to pass the PARMNUMth argument of FNDECL.
8881 If FNDECL is NULL, we are doing the conversion in function pointer
8882 argument passing, conversion in initialization, etc. */
8885 convert_for_assignment (tree type
, tree rhs
,
8886 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
8887 tsubst_flags_t complain
, int flags
)
8890 enum tree_code coder
;
8892 location_t rhs_loc
= EXPR_LOC_OR_LOC (rhs
, input_location
);
8893 bool has_loc
= EXPR_LOCATION (rhs
) != UNKNOWN_LOCATION
;
8894 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
8895 but preserve location wrappers. */
8896 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
8897 && !location_wrapper_p (rhs
))
8898 rhs
= TREE_OPERAND (rhs
, 0);
8900 /* Handle [dcl.init.list] direct-list-initialization from
8901 single element of enumeration with a fixed underlying type. */
8902 if (is_direct_enum_init (type
, rhs
))
8904 tree elt
= CONSTRUCTOR_ELT (rhs
, 0)->value
;
8905 if (check_narrowing (ENUM_UNDERLYING_TYPE (type
), elt
, complain
))
8907 warning_sentinel
w (warn_useless_cast
);
8908 warning_sentinel
w2 (warn_ignored_qualifiers
);
8909 rhs
= cp_build_c_cast (type
, elt
, complain
);
8912 rhs
= error_mark_node
;
8915 rhstype
= TREE_TYPE (rhs
);
8916 coder
= TREE_CODE (rhstype
);
8918 if (VECTOR_TYPE_P (type
) && coder
== VECTOR_TYPE
8919 && vector_types_convertible_p (type
, rhstype
, true))
8921 rhs
= mark_rvalue_use (rhs
);
8922 return convert (type
, rhs
);
8925 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
8926 return error_mark_node
;
8927 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
8928 return error_mark_node
;
8930 /* The RHS of an assignment cannot have void type. */
8931 if (coder
== VOID_TYPE
)
8933 if (complain
& tf_error
)
8934 error_at (rhs_loc
, "void value not ignored as it ought to be");
8935 return error_mark_node
;
8938 if (c_dialect_objc ())
8942 tree rname
= fndecl
;
8953 selector
= objc_message_selector ();
8955 if (selector
&& parmno
> 1)
8962 if (objc_compare_types (type
, rhstype
, parmno
, rname
))
8964 rhs
= mark_rvalue_use (rhs
);
8965 return convert (type
, rhs
);
8971 The expression is implicitly converted (clause _conv_) to the
8972 cv-unqualified type of the left operand.
8974 We allow bad conversions here because by the time we get to this point
8975 we are committed to doing the conversion. If we end up doing a bad
8976 conversion, convert_like will complain. */
8977 if (!can_convert_arg_bad (type
, rhstype
, rhs
, flags
, complain
))
8979 /* When -Wno-pmf-conversions is use, we just silently allow
8980 conversions from pointers-to-members to plain pointers. If
8981 the conversion doesn't work, cp_convert will complain. */
8983 && TYPE_PTR_P (type
)
8984 && TYPE_PTRMEMFUNC_P (rhstype
))
8985 rhs
= cp_convert (strip_top_quals (type
), rhs
, complain
);
8988 if (complain
& tf_error
)
8990 /* If the right-hand side has unknown type, then it is an
8991 overloaded function. Call instantiate_type to get error
8993 if (rhstype
== unknown_type_node
)
8995 tree r
= instantiate_type (type
, rhs
, tf_warning_or_error
);
8996 /* -fpermissive might allow this; recurse. */
8998 return convert_for_assignment (type
, r
, errtype
, fndecl
,
8999 parmnum
, complain
, flags
);
9002 complain_about_bad_argument (rhs_loc
,
9007 range_label_for_type_mismatch
label (rhstype
, type
);
9008 gcc_rich_location
richloc (rhs_loc
, has_loc
? &label
: NULL
);
9011 case ICR_DEFAULT_ARGUMENT
:
9013 "cannot convert %qH to %qI in default argument",
9018 "cannot convert %qH to %qI in argument passing",
9021 case ICR_CONVERTING
:
9022 error_at (&richloc
, "cannot convert %qH to %qI",
9027 "cannot convert %qH to %qI in initialization",
9031 error_at (&richloc
, "cannot convert %qH to %qI in return",
9036 "cannot convert %qH to %qI in assignment",
9043 if (TYPE_PTR_P (rhstype
)
9044 && TYPE_PTR_P (type
)
9045 && CLASS_TYPE_P (TREE_TYPE (rhstype
))
9046 && CLASS_TYPE_P (TREE_TYPE (type
))
9047 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype
)))
9048 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
9049 (TREE_TYPE (rhstype
))),
9050 "class type %qT is incomplete", TREE_TYPE (rhstype
));
9052 return error_mark_node
;
9055 if (warn_suggest_attribute_format
)
9057 const enum tree_code codel
= TREE_CODE (type
);
9058 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
9060 && check_missing_format_attribute (type
, rhstype
)
9061 && (complain
& tf_warning
))
9065 case ICR_DEFAULT_ARGUMENT
:
9067 warning (OPT_Wsuggest_attribute_format
,
9068 "parameter %qP of %qD might be a candidate "
9069 "for a format attribute", parmnum
, fndecl
);
9071 warning (OPT_Wsuggest_attribute_format
,
9072 "parameter might be a candidate "
9073 "for a format attribute");
9075 case ICR_CONVERTING
:
9076 warning (OPT_Wsuggest_attribute_format
,
9077 "target of conversion might be a candidate "
9078 "for a format attribute");
9081 warning (OPT_Wsuggest_attribute_format
,
9082 "target of initialization might be a candidate "
9083 "for a format attribute");
9086 warning (OPT_Wsuggest_attribute_format
,
9087 "return type might be a candidate "
9088 "for a format attribute");
9091 warning (OPT_Wsuggest_attribute_format
,
9092 "left-hand side of assignment might be a candidate "
9093 "for a format attribute");
9100 /* If -Wparentheses, warn about a = b = c when a has type bool and b
9102 if (warn_parentheses
9103 && TREE_CODE (type
) == BOOLEAN_TYPE
9104 && TREE_CODE (rhs
) == MODIFY_EXPR
9105 && !TREE_NO_WARNING (rhs
)
9106 && TREE_CODE (TREE_TYPE (rhs
)) != BOOLEAN_TYPE
9107 && (complain
& tf_warning
)
9108 && warning_at (rhs_loc
, OPT_Wparentheses
,
9109 "suggest parentheses around assignment used as "
9111 TREE_NO_WARNING (rhs
) = 1;
9113 if (complain
& tf_warning
)
9114 warn_for_address_or_pointer_of_packed_member (type
, rhs
);
9116 return perform_implicit_conversion_flags (strip_top_quals (type
), rhs
,
9120 /* Convert RHS to be of type TYPE.
9121 If EXP is nonzero, it is the target of the initialization.
9122 ERRTYPE indicates what kind of error the implicit conversion is.
9124 Two major differences between the behavior of
9125 `convert_for_assignment' and `convert_for_initialization'
9126 are that references are bashed in the former, while
9127 copied in the latter, and aggregates are assigned in
9128 the former (operator=) while initialized in the
9131 If using constructor make sure no conversion operator exists, if one does
9132 exist, an ambiguity exists. */
9135 convert_for_initialization (tree exp
, tree type
, tree rhs
, int flags
,
9136 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
9137 tsubst_flags_t complain
)
9139 enum tree_code codel
= TREE_CODE (type
);
9141 enum tree_code coder
;
9143 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9144 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
9145 if (TREE_CODE (rhs
) == NOP_EXPR
9146 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
9147 && codel
!= REFERENCE_TYPE
)
9148 rhs
= TREE_OPERAND (rhs
, 0);
9150 if (type
== error_mark_node
9151 || rhs
== error_mark_node
9152 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
9153 return error_mark_node
;
9155 if (MAYBE_CLASS_TYPE_P (non_reference (type
)))
9157 else if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
9158 && TREE_CODE (type
) != ARRAY_TYPE
9159 && (!TYPE_REF_P (type
)
9160 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
9161 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
9162 && !TYPE_REFFN_P (type
))
9163 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
9164 rhs
= decay_conversion (rhs
, complain
);
9166 rhstype
= TREE_TYPE (rhs
);
9167 coder
= TREE_CODE (rhstype
);
9169 if (coder
== ERROR_MARK
)
9170 return error_mark_node
;
9172 /* We accept references to incomplete types, so we can
9173 return here before checking if RHS is of complete type. */
9175 if (codel
== REFERENCE_TYPE
)
9177 auto_diagnostic_group d
;
9178 /* This should eventually happen in convert_arguments. */
9179 int savew
= 0, savee
= 0;
9182 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
9183 rhs
= initialize_reference (type
, rhs
, flags
, complain
);
9186 && (warningcount
+ werrorcount
> savew
|| errorcount
> savee
))
9187 inform (get_fndecl_argument_location (fndecl
, parmnum
),
9188 "in passing argument %P of %qD", parmnum
, fndecl
);
9193 exp
= require_complete_type_sfinae (exp
, complain
);
9194 if (exp
== error_mark_node
)
9195 return error_mark_node
;
9197 rhstype
= non_reference (rhstype
);
9199 type
= complete_type (type
);
9201 if (DIRECT_INIT_EXPR_P (type
, rhs
))
9202 /* Don't try to do copy-initialization if we already have
9203 direct-initialization. */
9206 if (MAYBE_CLASS_TYPE_P (type
))
9207 return perform_implicit_conversion_flags (type
, rhs
, complain
, flags
);
9209 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
,
9213 /* If RETVAL is the address of, or a reference to, a local variable or
9214 temporary give an appropriate warning and return true. */
9217 maybe_warn_about_returning_address_of_local (tree retval
)
9219 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
9220 tree whats_returned
= fold_for_warn (retval
);
9221 location_t loc
= cp_expr_loc_or_loc (retval
, input_location
);
9225 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
9226 whats_returned
= TREE_OPERAND (whats_returned
, 1);
9227 else if (CONVERT_EXPR_P (whats_returned
)
9228 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
)
9229 whats_returned
= TREE_OPERAND (whats_returned
, 0);
9234 if (TREE_CODE (whats_returned
) == TARGET_EXPR
9235 && is_std_init_list (TREE_TYPE (whats_returned
)))
9237 tree init
= TARGET_EXPR_INITIAL (whats_returned
);
9238 if (TREE_CODE (init
) == CONSTRUCTOR
)
9239 /* Pull out the array address. */
9240 whats_returned
= CONSTRUCTOR_ELT (init
, 0)->value
;
9241 else if (TREE_CODE (init
) == INDIRECT_REF
)
9242 /* The source of a trivial copy looks like *(T*)&var. */
9243 whats_returned
= TREE_OPERAND (init
, 0);
9246 STRIP_NOPS (whats_returned
);
9249 /* As a special case, we handle a call to std::move or std::forward. */
9250 if (TREE_CODE (whats_returned
) == CALL_EXPR
9251 && (is_std_move_p (whats_returned
)
9252 || is_std_forward_p (whats_returned
)))
9254 tree arg
= CALL_EXPR_ARG (whats_returned
, 0);
9255 return maybe_warn_about_returning_address_of_local (arg
);
9258 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
9260 whats_returned
= TREE_OPERAND (whats_returned
, 0);
9262 while (TREE_CODE (whats_returned
) == COMPONENT_REF
9263 || TREE_CODE (whats_returned
) == ARRAY_REF
)
9264 whats_returned
= TREE_OPERAND (whats_returned
, 0);
9266 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
9267 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
9269 if (TYPE_REF_P (valtype
))
9270 warning_at (loc
, OPT_Wreturn_local_addr
,
9271 "returning reference to temporary");
9272 else if (is_std_init_list (valtype
))
9273 warning_at (loc
, OPT_Winit_list_lifetime
,
9274 "returning temporary initializer_list does not extend "
9275 "the lifetime of the underlying array");
9279 STRIP_ANY_LOCATION_WRAPPER (whats_returned
);
9281 if (DECL_P (whats_returned
)
9282 && DECL_NAME (whats_returned
)
9283 && DECL_FUNCTION_SCOPE_P (whats_returned
)
9284 && !is_capture_proxy (whats_returned
)
9285 && !(TREE_STATIC (whats_returned
)
9286 || TREE_PUBLIC (whats_returned
)))
9288 if (VAR_P (whats_returned
)
9289 && DECL_DECOMPOSITION_P (whats_returned
)
9290 && DECL_DECOMP_BASE (whats_returned
)
9291 && DECL_HAS_VALUE_EXPR_P (whats_returned
))
9293 /* When returning address of a structured binding, if the structured
9294 binding is not a reference, continue normally, if it is a
9295 reference, recurse on the initializer of the structured
9297 tree base
= DECL_DECOMP_BASE (whats_returned
);
9298 if (TYPE_REF_P (TREE_TYPE (base
)))
9300 tree init
= DECL_INITIAL (base
);
9301 return maybe_warn_about_returning_address_of_local (init
);
9305 auto_diagnostic_group d
;
9306 if (TYPE_REF_P (valtype
))
9307 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
9308 "reference to local variable %qD returned",
9310 else if (is_std_init_list (valtype
))
9311 w
= warning_at (loc
, OPT_Winit_list_lifetime
,
9312 "returning local initializer_list variable %qD "
9313 "does not extend the lifetime of the underlying array",
9315 else if (TREE_CODE (whats_returned
) == LABEL_DECL
)
9316 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
9317 "address of label %qD returned",
9320 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
9321 "address of local variable %qD returned",
9324 inform (DECL_SOURCE_LOCATION (whats_returned
),
9332 /* Returns true if DECL is in the std namespace. */
9335 decl_in_std_namespace_p (tree decl
)
9337 return (decl
!= NULL_TREE
9338 && DECL_NAMESPACE_STD_P (decl_namespace_context (decl
)));
9341 /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
9344 is_std_forward_p (tree fn
)
9346 /* std::forward only takes one argument. */
9347 if (call_expr_nargs (fn
) != 1)
9350 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
9351 if (!decl_in_std_namespace_p (fndecl
))
9354 tree name
= DECL_NAME (fndecl
);
9355 return name
&& id_equal (name
, "forward");
9358 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
9361 is_std_move_p (tree fn
)
9363 /* std::move only takes one argument. */
9364 if (call_expr_nargs (fn
) != 1)
9367 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
9368 if (!decl_in_std_namespace_p (fndecl
))
9371 tree name
= DECL_NAME (fndecl
);
9372 return name
&& id_equal (name
, "move");
9375 /* Returns true if RETVAL is a good candidate for the NRVO as per
9376 [class.copy.elision]. FUNCTYPE is the type the function is declared
9380 can_do_nrvo_p (tree retval
, tree functype
)
9382 if (functype
== error_mark_node
)
9385 STRIP_ANY_LOCATION_WRAPPER (retval
);
9386 tree result
= DECL_RESULT (current_function_decl
);
9387 return (retval
!= NULL_TREE
9388 && !processing_template_decl
9389 /* Must be a local, automatic variable. */
9391 && DECL_CONTEXT (retval
) == current_function_decl
9392 && !TREE_STATIC (retval
)
9393 /* And not a lambda or anonymous union proxy. */
9394 && !DECL_HAS_VALUE_EXPR_P (retval
)
9395 && (DECL_ALIGN (retval
) <= DECL_ALIGN (result
))
9396 /* The cv-unqualified type of the returned value must be the
9397 same as the cv-unqualified return type of the
9399 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval
))),
9400 (TYPE_MAIN_VARIANT (functype
)))
9401 /* And the returned value must be non-volatile. */
9402 && !TYPE_VOLATILE (TREE_TYPE (retval
)));
9405 /* Returns true if we should treat RETVAL, an expression being returned,
9406 as if it were designated by an rvalue. See [class.copy.elision].
9407 PARM_P is true if a function parameter is OK in this context. */
9410 treat_lvalue_as_rvalue_p (tree retval
, bool parm_ok
)
9412 STRIP_ANY_LOCATION_WRAPPER (retval
);
9413 return ((cxx_dialect
!= cxx98
)
9414 && ((VAR_P (retval
) && !DECL_HAS_VALUE_EXPR_P (retval
))
9415 || (parm_ok
&& TREE_CODE (retval
) == PARM_DECL
))
9416 && DECL_CONTEXT (retval
) == current_function_decl
9417 && !TREE_STATIC (retval
));
9420 /* Warn about wrong usage of std::move in a return statement. RETVAL
9421 is the expression we are returning; FUNCTYPE is the type the function
9422 is declared to return. */
9425 maybe_warn_pessimizing_move (tree retval
, tree functype
)
9427 if (!(warn_pessimizing_move
|| warn_redundant_move
))
9430 location_t loc
= cp_expr_loc_or_loc (retval
, input_location
);
9432 /* C++98 doesn't know move. */
9433 if (cxx_dialect
< cxx11
)
9436 /* Wait until instantiation time, since we can't gauge if we should do
9437 the NRVO until then. */
9438 if (processing_template_decl
)
9441 /* This is only interesting for class types. */
9442 if (!CLASS_TYPE_P (functype
))
9445 /* We're looking for *std::move<T&> ((T &) &arg). */
9446 if (REFERENCE_REF_P (retval
)
9447 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
9449 tree fn
= TREE_OPERAND (retval
, 0);
9450 if (is_std_move_p (fn
))
9452 tree arg
= CALL_EXPR_ARG (fn
, 0);
9453 if (TREE_CODE (arg
) != NOP_EXPR
)
9455 arg
= TREE_OPERAND (arg
, 0);
9456 if (TREE_CODE (arg
) != ADDR_EXPR
)
9458 arg
= TREE_OPERAND (arg
, 0);
9459 arg
= convert_from_reference (arg
);
9460 /* Warn if we could do copy elision were it not for the move. */
9461 if (can_do_nrvo_p (arg
, functype
))
9463 auto_diagnostic_group d
;
9464 if (warning_at (loc
, OPT_Wpessimizing_move
,
9465 "moving a local object in a return statement "
9466 "prevents copy elision"))
9467 inform (loc
, "remove %<std::move%> call");
9469 /* Warn if the move is redundant. It is redundant when we would
9470 do maybe-rvalue overload resolution even without std::move. */
9471 else if (warn_redundant_move
9472 && treat_lvalue_as_rvalue_p (arg
, /*parm_ok*/true))
9474 /* Make sure that the overload resolution would actually succeed
9475 if we removed the std::move call. */
9476 tree t
= convert_for_initialization (NULL_TREE
, functype
,
9479 | LOOKUP_ONLYCONVERTING
9480 | LOOKUP_PREFER_RVALUE
),
9481 ICR_RETURN
, NULL_TREE
, 0,
9483 /* If this worked, implicit rvalue would work, so the call to
9484 std::move is redundant. */
9485 if (t
!= error_mark_node
)
9487 auto_diagnostic_group d
;
9488 if (warning_at (loc
, OPT_Wredundant_move
,
9489 "redundant move in return statement"))
9490 inform (loc
, "remove %<std::move%> call");
9497 /* Check that returning RETVAL from the current function is valid.
9498 Return an expression explicitly showing all conversions required to
9499 change RETVAL into the function return type, and to assign it to
9500 the DECL_RESULT for the function. Set *NO_WARNING to true if
9501 code reaches end of non-void function warning shouldn't be issued
9502 on this RETURN_EXPR. */
9505 check_return_expr (tree retval
, bool *no_warning
)
9508 /* The type actually returned by the function. */
9510 /* The type the function is declared to return, or void if
9511 the declared type is incomplete. */
9513 int fn_returns_value_p
;
9515 *no_warning
= false;
9517 /* A `volatile' function is one that isn't supposed to return, ever.
9518 (This is a G++ extension, used to get better code for functions
9519 that call the `volatile' function.) */
9520 if (TREE_THIS_VOLATILE (current_function_decl
))
9521 warning (0, "function declared %<noreturn%> has a %<return%> statement");
9523 /* Check for various simple errors. */
9524 if (DECL_DESTRUCTOR_P (current_function_decl
))
9527 error ("returning a value from a destructor");
9530 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
9532 if (in_function_try_handler
)
9533 /* If a return statement appears in a handler of the
9534 function-try-block of a constructor, the program is ill-formed. */
9535 error ("cannot return from a handler of a function-try-block of a constructor");
9537 /* You can't return a value from a constructor. */
9538 error ("returning a value from a constructor");
9542 const tree saved_retval
= retval
;
9544 if (processing_template_decl
)
9546 current_function_returns_value
= 1;
9548 if (check_for_bare_parameter_packs (retval
))
9549 return error_mark_node
;
9551 /* If one of the types might be void, we can't tell whether we're
9552 returning a value. */
9553 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl
)))
9554 && !FNDECL_USED_AUTO (current_function_decl
))
9555 || (retval
!= NULL_TREE
9556 && (TREE_TYPE (retval
) == NULL_TREE
9557 || WILDCARD_TYPE_P (TREE_TYPE (retval
)))))
9561 functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
9563 /* Deduce auto return type from a return statement. */
9564 if (FNDECL_USED_AUTO (current_function_decl
))
9566 tree pattern
= DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
);
9570 if (!retval
&& !is_auto (pattern
))
9572 /* Give a helpful error message. */
9573 error ("return-statement with no value, in function returning %qT",
9575 inform (input_location
, "only plain %<auto%> return type can be "
9576 "deduced to %<void%>");
9577 type
= error_mark_node
;
9579 else if (retval
&& BRACE_ENCLOSED_INITIALIZER_P (retval
))
9581 error ("returning initializer list");
9582 type
= error_mark_node
;
9588 auto_node
= type_uses_auto (pattern
);
9589 type
= do_auto_deduction (pattern
, retval
, auto_node
);
9592 if (type
== error_mark_node
)
9594 else if (functype
== pattern
)
9595 apply_deduced_return_type (current_function_decl
, type
);
9596 else if (!same_type_p (type
, functype
))
9598 if (LAMBDA_FUNCTION_P (current_function_decl
))
9599 error ("inconsistent types %qT and %qT deduced for "
9600 "lambda return type", functype
, type
);
9602 error ("inconsistent deduction for auto return type: "
9603 "%qT and then %qT", functype
, type
);
9608 result
= DECL_RESULT (current_function_decl
);
9609 valtype
= TREE_TYPE (result
);
9610 gcc_assert (valtype
!= NULL_TREE
);
9611 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
9613 /* Check for a return statement with no return value in a function
9614 that's supposed to return a value. */
9615 if (!retval
&& fn_returns_value_p
)
9617 if (functype
!= error_mark_node
)
9618 permerror (input_location
, "return-statement with no value, in "
9619 "function returning %qT", valtype
);
9620 /* Remember that this function did return. */
9621 current_function_returns_value
= 1;
9622 /* And signal caller that TREE_NO_WARNING should be set on the
9623 RETURN_EXPR to avoid control reaches end of non-void function
9624 warnings in tree-cfg.c. */
9627 /* Check for a return statement with a value in a function that
9628 isn't supposed to return a value. */
9629 else if (retval
&& !fn_returns_value_p
)
9631 if (VOID_TYPE_P (TREE_TYPE (retval
)))
9632 /* You can return a `void' value from a function of `void'
9633 type. In that case, we have to evaluate the expression for
9634 its side-effects. */
9635 finish_expr_stmt (retval
);
9637 permerror (input_location
,
9638 "return-statement with a value, in function "
9639 "returning %qT", valtype
);
9640 current_function_returns_null
= 1;
9642 /* There's really no value to return, after all. */
9646 /* Remember that this function can sometimes return without a
9648 current_function_returns_null
= 1;
9650 /* Remember that this function did return a value. */
9651 current_function_returns_value
= 1;
9653 /* Check for erroneous operands -- but after giving ourselves a
9654 chance to provide an error about returning a value from a void
9656 if (error_operand_p (retval
))
9658 current_function_return_value
= error_mark_node
;
9659 return error_mark_node
;
9662 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
9663 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl
))
9664 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
9666 && retval
&& null_ptr_cst_p (retval
))
9667 warning (0, "%<operator new%> must not return NULL unless it is "
9668 "declared %<throw()%> (or -fcheck-new is in effect)");
9670 /* Effective C++ rule 15. See also start_function. */
9672 && DECL_NAME (current_function_decl
) == assign_op_identifier
9673 && !type_dependent_expression_p (retval
))
9677 /* The function return type must be a reference to the current
9679 if (TYPE_REF_P (valtype
)
9680 && same_type_ignoring_top_level_qualifiers_p
9681 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
)))
9683 /* Returning '*this' is obviously OK. */
9684 if (retval
== current_class_ref
)
9686 /* If we are calling a function whose return type is the same of
9687 the current class reference, it is ok. */
9688 else if (INDIRECT_REF_P (retval
)
9689 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
9694 warning (OPT_Weffc__
, "%<operator=%> should return a reference to %<*this%>");
9697 if (dependent_type_p (functype
)
9698 || type_dependent_expression_p (retval
))
9701 /* We should not have changed the return value. */
9702 gcc_assert (retval
== saved_retval
);
9706 /* The fabled Named Return Value optimization, as per [class.copy]/15:
9708 [...] For a function with a class return type, if the expression
9709 in the return statement is the name of a local object, and the cv-
9710 unqualified type of the local object is the same as the function
9711 return type, an implementation is permitted to omit creating the tem-
9712 porary object to hold the function return value [...]
9714 So, if this is a value-returning function that always returns the same
9715 local variable, remember it.
9717 It might be nice to be more flexible, and choose the first suitable
9718 variable even if the function sometimes returns something else, but
9719 then we run the risk of clobbering the variable we chose if the other
9720 returned expression uses the chosen variable somehow. And people expect
9721 this restriction, anyway. (jason 2000-11-19)
9723 See finish_function and finalize_nrv for the rest of this optimization. */
9725 STRIP_ANY_LOCATION_WRAPPER (retval
);
9727 bool named_return_value_okay_p
= can_do_nrvo_p (retval
, functype
);
9728 if (fn_returns_value_p
&& flag_elide_constructors
)
9730 if (named_return_value_okay_p
9731 && (current_function_return_value
== NULL_TREE
9732 || current_function_return_value
== retval
))
9733 current_function_return_value
= retval
;
9735 current_function_return_value
= error_mark_node
;
9738 /* We don't need to do any conversions when there's nothing being
9743 if (!named_return_value_okay_p
)
9744 maybe_warn_pessimizing_move (retval
, functype
);
9746 /* Do any required conversions. */
9747 if (retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
9748 /* No conversions are required. */
9752 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
9754 /* The functype's return type will have been set to void, if it
9755 was an incomplete type. Just treat this as 'return;' */
9756 if (VOID_TYPE_P (functype
))
9757 return error_mark_node
;
9759 /* If we had an id-expression obfuscated by force_paren_expr, we need
9760 to undo it so we can try to treat it as an rvalue below. */
9761 retval
= maybe_undo_parenthesized_ref (retval
);
9763 if (processing_template_decl
)
9764 retval
= build_non_dependent_expr (retval
);
9766 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9767 treated as an rvalue for the purposes of overload resolution to
9768 favor move constructors over copy constructors.
9770 Note that these conditions are similar to, but not as strict as,
9771 the conditions for the named return value optimization. */
9772 bool converted
= false;
9773 if (treat_lvalue_as_rvalue_p (retval
, /*parm_ok*/true)
9774 /* This is only interesting for class type. */
9775 && CLASS_TYPE_P (functype
))
9777 tree moved
= move (retval
);
9778 moved
= convert_for_initialization
9779 (NULL_TREE
, functype
, moved
, flags
|LOOKUP_PREFER_RVALUE
,
9780 ICR_RETURN
, NULL_TREE
, 0, tf_none
);
9781 if (moved
!= error_mark_node
)
9788 /* The call in a (lambda) thunk needs no conversions. */
9789 if (TREE_CODE (retval
) == CALL_EXPR
9790 && CALL_FROM_THUNK_P (retval
))
9793 /* First convert the value to the function's return type, then
9794 to the type of return value's location to handle the
9795 case that functype is smaller than the valtype. */
9797 retval
= convert_for_initialization
9798 (NULL_TREE
, functype
, retval
, flags
, ICR_RETURN
, NULL_TREE
, 0,
9799 tf_warning_or_error
);
9800 retval
= convert (valtype
, retval
);
9802 /* If the conversion failed, treat this just like `return;'. */
9803 if (retval
== error_mark_node
)
9805 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9806 else if (! cfun
->returns_struct
9807 && TREE_CODE (retval
) == TARGET_EXPR
9808 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
9809 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
9810 TREE_OPERAND (retval
, 0));
9811 else if (!processing_template_decl
9812 && maybe_warn_about_returning_address_of_local (retval
)
9813 && INDIRECT_TYPE_P (valtype
))
9814 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
9815 build_zero_cst (TREE_TYPE (retval
)));
9818 if (processing_template_decl
)
9819 return saved_retval
;
9821 /* Actually copy the value returned into the appropriate location. */
9822 if (retval
&& retval
!= result
)
9823 retval
= build2 (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
9829 /* Returns nonzero if the pointer-type FROM can be converted to the
9830 pointer-type TO via a qualification conversion. If CONSTP is -1,
9831 then we return nonzero if the pointers are similar, and the
9832 cv-qualification signature of FROM is a proper subset of that of TO.
9834 If CONSTP is positive, then all outer pointers have been
9838 comp_ptr_ttypes_real (tree to
, tree from
, int constp
)
9840 bool to_more_cv_qualified
= false;
9841 bool is_opaque_pointer
= false;
9843 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9845 if (TREE_CODE (to
) != TREE_CODE (from
))
9848 if (TREE_CODE (from
) == OFFSET_TYPE
9849 && !same_type_p (TYPE_OFFSET_BASETYPE (from
),
9850 TYPE_OFFSET_BASETYPE (to
)))
9853 /* Const and volatile mean something different for function types,
9854 so the usual checks are not appropriate. */
9855 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
9857 if (!at_least_as_qualified_p (to
, from
))
9860 if (!at_least_as_qualified_p (from
, to
))
9864 to_more_cv_qualified
= true;
9868 constp
&= TYPE_READONLY (to
);
9871 if (VECTOR_TYPE_P (to
))
9872 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
9874 if (!TYPE_PTR_P (to
) && !TYPE_PTRDATAMEM_P (to
))
9875 return ((constp
>= 0 || to_more_cv_qualified
)
9876 && (is_opaque_pointer
9877 || same_type_ignoring_top_level_qualifiers_p (to
, from
)));
9881 /* When comparing, say, char ** to char const **, this function takes
9882 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9883 types to this function. */
9886 comp_ptr_ttypes (tree to
, tree from
)
9888 return comp_ptr_ttypes_real (to
, from
, 1);
9891 /* Returns true iff FNTYPE is a non-class type that involves
9892 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9893 if a parameter type is ill-formed. */
9896 error_type_p (const_tree type
)
9900 switch (TREE_CODE (type
))
9906 case REFERENCE_TYPE
:
9908 return error_type_p (TREE_TYPE (type
));
9912 if (error_type_p (TREE_TYPE (type
)))
9914 for (t
= TYPE_ARG_TYPES (type
); t
; t
= TREE_CHAIN (t
))
9915 if (error_type_p (TREE_VALUE (t
)))
9920 if (TYPE_PTRMEMFUNC_P (type
))
9921 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type
));
9929 /* Returns true if to and from are (possibly multi-level) pointers to the same
9930 type or inheritance-related types, regardless of cv-quals. */
9933 ptr_reasonably_similar (const_tree to
, const_tree from
)
9935 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9937 /* Any target type is similar enough to void. */
9938 if (VOID_TYPE_P (to
))
9939 return !error_type_p (from
);
9940 if (VOID_TYPE_P (from
))
9941 return !error_type_p (to
);
9943 if (TREE_CODE (to
) != TREE_CODE (from
))
9946 if (TREE_CODE (from
) == OFFSET_TYPE
9947 && comptypes (TYPE_OFFSET_BASETYPE (to
),
9948 TYPE_OFFSET_BASETYPE (from
),
9949 COMPARE_BASE
| COMPARE_DERIVED
))
9952 if (VECTOR_TYPE_P (to
)
9953 && vector_types_convertible_p (to
, from
, false))
9956 if (TREE_CODE (to
) == INTEGER_TYPE
9957 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
9960 if (TREE_CODE (to
) == FUNCTION_TYPE
)
9961 return !error_type_p (to
) && !error_type_p (from
);
9963 if (!TYPE_PTR_P (to
))
9965 /* When either type is incomplete avoid DERIVED_FROM_P,
9966 which may call complete_type (c++/57942). */
9967 bool b
= !COMPLETE_TYPE_P (to
) || !COMPLETE_TYPE_P (from
);
9969 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
9970 b
? COMPARE_STRICT
: COMPARE_BASE
| COMPARE_DERIVED
);
9975 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9976 pointer-to-member types) are the same, ignoring cv-qualification at
9980 comp_ptr_ttypes_const (tree to
, tree from
)
9982 bool is_opaque_pointer
= false;
9984 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9986 if (TREE_CODE (to
) != TREE_CODE (from
))
9989 if (TREE_CODE (from
) == OFFSET_TYPE
9990 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
9991 TYPE_OFFSET_BASETYPE (to
)))
9994 if (VECTOR_TYPE_P (to
))
9995 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
9997 if (!TYPE_PTR_P (to
))
9998 return (is_opaque_pointer
9999 || same_type_ignoring_top_level_qualifiers_p (to
, from
));
10003 /* Returns the type qualifiers for this type, including the qualifiers on the
10004 elements for an array type. */
10007 cp_type_quals (const_tree type
)
10010 /* This CONST_CAST is okay because strip_array_types returns its
10011 argument unmodified and we assign it to a const_tree. */
10012 type
= strip_array_types (CONST_CAST_TREE (type
));
10013 if (type
== error_mark_node
10014 /* Quals on a FUNCTION_TYPE are memfn quals. */
10015 || TREE_CODE (type
) == FUNCTION_TYPE
)
10016 return TYPE_UNQUALIFIED
;
10017 quals
= TYPE_QUALS (type
);
10018 /* METHOD and REFERENCE_TYPEs should never have quals. */
10019 gcc_assert ((TREE_CODE (type
) != METHOD_TYPE
10020 && !TYPE_REF_P (type
))
10021 || ((quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
))
10022 == TYPE_UNQUALIFIED
));
10026 /* Returns the function-ref-qualifier for TYPE */
10029 type_memfn_rqual (const_tree type
)
10031 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
10032 || TREE_CODE (type
) == METHOD_TYPE
);
10034 if (!FUNCTION_REF_QUALIFIED (type
))
10035 return REF_QUAL_NONE
;
10036 else if (FUNCTION_RVALUE_QUALIFIED (type
))
10037 return REF_QUAL_RVALUE
;
10039 return REF_QUAL_LVALUE
;
10042 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
10046 type_memfn_quals (const_tree type
)
10048 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10049 return TYPE_QUALS (type
);
10050 else if (TREE_CODE (type
) == METHOD_TYPE
)
10051 return cp_type_quals (class_of_this_parm (type
));
10053 gcc_unreachable ();
10056 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
10057 MEMFN_QUALS and its ref-qualifier to RQUAL. */
10060 apply_memfn_quals (tree type
, cp_cv_quals memfn_quals
, cp_ref_qualifier rqual
)
10062 /* Could handle METHOD_TYPE here if necessary. */
10063 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
10064 if (TYPE_QUALS (type
) == memfn_quals
10065 && type_memfn_rqual (type
) == rqual
)
10068 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
10070 tree result
= build_qualified_type (type
, memfn_quals
);
10071 return build_ref_qualified_type (result
, rqual
);
10074 /* Returns nonzero if TYPE is const or volatile. */
10077 cv_qualified_p (const_tree type
)
10079 int quals
= cp_type_quals (type
);
10080 return (quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)) != 0;
10083 /* Returns nonzero if the TYPE contains a mutable member. */
10086 cp_has_mutable_p (const_tree type
)
10088 /* This CONST_CAST is okay because strip_array_types returns its
10089 argument unmodified and we assign it to a const_tree. */
10090 type
= strip_array_types (CONST_CAST_TREE(type
));
10092 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
10095 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
10096 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
10097 approximation. In particular, consider:
10100 struct S { int i; };
10101 const S s = { f(); }
10103 Here, we will make "s" as TREE_READONLY (because it is declared
10104 "const") -- only to reverse ourselves upon seeing that the
10105 initializer is non-constant. */
10108 cp_apply_type_quals_to_decl (int type_quals
, tree decl
)
10110 tree type
= TREE_TYPE (decl
);
10112 if (type
== error_mark_node
)
10115 if (TREE_CODE (decl
) == TYPE_DECL
)
10118 gcc_assert (!(TREE_CODE (type
) == FUNCTION_TYPE
10119 && type_quals
!= TYPE_UNQUALIFIED
));
10121 /* Avoid setting TREE_READONLY incorrectly. */
10122 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
10123 constructor can produce constant init, so rely on cp_finish_decl to
10124 clear TREE_READONLY if the variable has non-constant init. */
10126 /* If the type has (or might have) a mutable component, that component
10127 might be modified. */
10128 if (TYPE_HAS_MUTABLE_P (type
) || !COMPLETE_TYPE_P (type
))
10129 type_quals
&= ~TYPE_QUAL_CONST
;
10131 c_apply_type_quals_to_decl (type_quals
, decl
);
10134 /* Subroutine of casts_away_constness. Make T1 and T2 point at
10135 exemplar types such that casting T1 to T2 is casting away constness
10136 if and only if there is no implicit conversion from T1 to T2. */
10139 casts_away_constness_r (tree
*t1
, tree
*t2
, tsubst_flags_t complain
)
10144 /* [expr.const.cast]
10146 For multi-level pointer to members and multi-level mixed pointers
10147 and pointers to members (conv.qual), the "member" aspect of a
10148 pointer to member level is ignored when determining if a const
10149 cv-qualifier has been cast away. */
10150 /* [expr.const.cast]
10152 For two pointer types:
10154 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
10155 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
10158 casting from X1 to X2 casts away constness if, for a non-pointer
10159 type T there does not exist an implicit conversion (clause
10162 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
10166 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
10167 if ((!TYPE_PTR_P (*t1
) && !TYPE_PTRDATAMEM_P (*t1
))
10168 || (!TYPE_PTR_P (*t2
) && !TYPE_PTRDATAMEM_P (*t2
)))
10170 *t1
= cp_build_qualified_type (void_type_node
,
10171 cp_type_quals (*t1
));
10172 *t2
= cp_build_qualified_type (void_type_node
,
10173 cp_type_quals (*t2
));
10177 quals1
= cp_type_quals (*t1
);
10178 quals2
= cp_type_quals (*t2
);
10180 if (TYPE_PTRDATAMEM_P (*t1
))
10181 *t1
= TYPE_PTRMEM_POINTED_TO_TYPE (*t1
);
10183 *t1
= TREE_TYPE (*t1
);
10184 if (TYPE_PTRDATAMEM_P (*t2
))
10185 *t2
= TYPE_PTRMEM_POINTED_TO_TYPE (*t2
);
10187 *t2
= TREE_TYPE (*t2
);
10189 casts_away_constness_r (t1
, t2
, complain
);
10190 *t1
= build_pointer_type (*t1
);
10191 *t2
= build_pointer_type (*t2
);
10192 *t1
= cp_build_qualified_type (*t1
, quals1
);
10193 *t2
= cp_build_qualified_type (*t2
, quals2
);
10196 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
10199 ??? This function returns non-zero if casting away qualifiers not
10200 just const. We would like to return to the caller exactly which
10201 qualifiers are casted away to give more accurate diagnostics.
10205 casts_away_constness (tree t1
, tree t2
, tsubst_flags_t complain
)
10207 if (TYPE_REF_P (t2
))
10209 /* [expr.const.cast]
10211 Casting from an lvalue of type T1 to an lvalue of type T2
10212 using a reference cast casts away constness if a cast from an
10213 rvalue of type "pointer to T1" to the type "pointer to T2"
10214 casts away constness. */
10215 t1
= (TYPE_REF_P (t1
) ? TREE_TYPE (t1
) : t1
);
10216 return casts_away_constness (build_pointer_type (t1
),
10217 build_pointer_type (TREE_TYPE (t2
)),
10221 if (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
10222 /* [expr.const.cast]
10224 Casting from an rvalue of type "pointer to data member of X
10225 of type T1" to the type "pointer to data member of Y of type
10226 T2" casts away constness if a cast from an rvalue of type
10227 "pointer to T1" to the type "pointer to T2" casts away
10229 return casts_away_constness
10230 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1
)),
10231 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2
)),
10234 /* Casting away constness is only something that makes sense for
10235 pointer or reference types. */
10236 if (!TYPE_PTR_P (t1
) || !TYPE_PTR_P (t2
))
10239 /* Top-level qualifiers don't matter. */
10240 t1
= TYPE_MAIN_VARIANT (t1
);
10241 t2
= TYPE_MAIN_VARIANT (t2
);
10242 casts_away_constness_r (&t1
, &t2
, complain
);
10243 if (!can_convert (t2
, t1
, complain
))
10249 /* If T is a REFERENCE_TYPE return the type to which T refers.
10250 Otherwise, return T itself. */
10253 non_reference (tree t
)
10255 if (t
&& TYPE_REF_P (t
))
10261 /* Return nonzero if REF is an lvalue valid for this language;
10262 otherwise, print an error message and return zero. USE says
10263 how the lvalue is being used and so selects the error message. */
10266 lvalue_or_else (tree ref
, enum lvalue_use use
, tsubst_flags_t complain
)
10268 cp_lvalue_kind kind
= lvalue_kind (ref
);
10270 if (kind
== clk_none
)
10272 if (complain
& tf_error
)
10273 lvalue_error (input_location
, use
);
10276 else if (kind
& (clk_rvalueref
|clk_class
))
10278 if (!(complain
& tf_error
))
10280 /* Make this a permerror because we used to accept it. */
10281 permerror (input_location
, "using rvalue as lvalue");
10286 /* Return true if a user-defined literal operator is a raw operator. */
10289 check_raw_literal_operator (const_tree decl
)
10291 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
10294 bool maybe_raw_p
= false;
10296 /* Count the number and type of arguments and check for ellipsis. */
10297 for (argtype
= argtypes
, arity
= 0;
10298 argtype
&& argtype
!= void_list_node
;
10299 ++arity
, argtype
= TREE_CHAIN (argtype
))
10301 tree t
= TREE_VALUE (argtype
);
10303 if (same_type_p (t
, const_string_type_node
))
10304 maybe_raw_p
= true;
10307 return false; /* Found ellipsis. */
10309 if (!maybe_raw_p
|| arity
!= 1)
10316 /* Return true if a user-defined literal operator has one of the allowed
10320 check_literal_operator_args (const_tree decl
,
10321 bool *long_long_unsigned_p
, bool *long_double_p
)
10323 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
10325 *long_long_unsigned_p
= false;
10326 *long_double_p
= false;
10327 if (processing_template_decl
|| processing_specialization
)
10328 return argtypes
== void_list_node
;
10335 /* Count the number and type of arguments and check for ellipsis. */
10336 for (argtype
= argtypes
, arity
= 0;
10337 argtype
&& argtype
!= void_list_node
;
10338 argtype
= TREE_CHAIN (argtype
))
10340 tree t
= TREE_VALUE (argtype
);
10343 if (TYPE_PTR_P (t
))
10345 bool maybe_raw_p
= false;
10347 if (cp_type_quals (t
) != TYPE_QUAL_CONST
)
10349 t
= TYPE_MAIN_VARIANT (t
);
10350 if ((maybe_raw_p
= same_type_p (t
, char_type_node
))
10351 || same_type_p (t
, wchar_type_node
)
10352 || same_type_p (t
, char8_type_node
)
10353 || same_type_p (t
, char16_type_node
)
10354 || same_type_p (t
, char32_type_node
))
10356 argtype
= TREE_CHAIN (argtype
);
10359 t
= TREE_VALUE (argtype
);
10360 if (maybe_raw_p
&& argtype
== void_list_node
)
10362 else if (same_type_p (t
, size_type_node
))
10371 else if (same_type_p (t
, long_long_unsigned_type_node
))
10374 *long_long_unsigned_p
= true;
10376 else if (same_type_p (t
, long_double_type_node
))
10379 *long_double_p
= true;
10381 else if (same_type_p (t
, char_type_node
))
10383 else if (same_type_p (t
, wchar_type_node
))
10385 else if (same_type_p (t
, char8_type_node
))
10387 else if (same_type_p (t
, char16_type_node
))
10389 else if (same_type_p (t
, char32_type_node
))
10395 return false; /* Found ellipsis. */
10397 if (arity
!= max_arity
)
10404 /* Always returns false since unlike C90, C++ has no concept of implicit
10405 function declarations. */
10408 c_decl_implicit (const_tree
)