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 (FUNC_OR_METHOD_TYPE_P (t1
))
1267 if (type_memfn_rqual (t1
) != type_memfn_rqual (t2
))
1269 if (flag_noexcept_type
1270 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1
),
1271 TYPE_RAISES_EXCEPTIONS (t2
),
1276 /* Allow for two different type nodes which have essentially the same
1277 definition. Note that we already checked for equality of the type
1278 qualifiers (just above). */
1280 if (TREE_CODE (t1
) != ARRAY_TYPE
1281 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1285 /* Compare the types. Break out if they could be the same. */
1286 switch (TREE_CODE (t1
))
1290 /* All void and bool types are the same. */
1294 case FIXED_POINT_TYPE
:
1296 /* With these nodes, we can't determine type equivalence by
1297 looking at what is stored in the nodes themselves, because
1298 two nodes might have different TYPE_MAIN_VARIANTs but still
1299 represent the same type. For example, wchar_t and int could
1300 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1301 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1302 and are distinct types. On the other hand, int and the
1305 typedef int INT __attribute((may_alias));
1307 have identical properties, different TYPE_MAIN_VARIANTs, but
1308 represent the same type. The canonical type system keeps
1309 track of equivalence in this case, so we fall back on it. */
1310 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1312 case TEMPLATE_TEMPLATE_PARM
:
1313 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1314 if (!comp_template_parms_position (t1
, t2
))
1316 if (!comp_template_parms
1317 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1
)),
1318 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2
))))
1320 if (TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
)
1322 /* Don't check inheritance. */
1323 strict
= COMPARE_STRICT
;
1328 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
1329 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
1330 || TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1331 && comp_template_args (TYPE_TI_ARGS (t1
), TYPE_TI_ARGS (t2
)))
1334 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
1336 else if ((strict
& COMPARE_DERIVED
) && DERIVED_FROM_P (t2
, t1
))
1342 if (!comptypes (TYPE_OFFSET_BASETYPE (t1
), TYPE_OFFSET_BASETYPE (t2
),
1343 strict
& ~COMPARE_REDECLARATION
))
1345 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1349 case REFERENCE_TYPE
:
1350 if (TYPE_REF_IS_RVALUE (t1
) != TYPE_REF_IS_RVALUE (t2
))
1352 /* fall through to checks for pointer types */
1356 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1357 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1363 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1365 if (!compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)))
1370 /* Target types must match incl. qualifiers. */
1371 if (!comp_array_types (t1
, t2
, !!(strict
& COMPARE_REDECLARATION
)))
1375 case TEMPLATE_TYPE_PARM
:
1376 /* If T1 and T2 don't have the same relative position in their
1377 template parameters set, they can't be equal. */
1378 if (!comp_template_parms_position (t1
, t2
))
1380 /* If T1 and T2 don't represent the same class template deduction,
1381 they aren't equal. */
1382 if (CLASS_PLACEHOLDER_TEMPLATE (t1
)
1383 != CLASS_PLACEHOLDER_TEMPLATE (t2
))
1385 /* Constrained 'auto's are distinct from parms that don't have the same
1387 if (!equivalent_placeholder_constraints (t1
, t2
))
1392 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1
),
1393 TYPENAME_TYPE_FULLNAME (t2
)))
1395 /* Qualifiers don't matter on scopes. */
1396 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1
),
1401 case UNBOUND_CLASS_TEMPLATE
:
1402 if (!cp_tree_equal (TYPE_IDENTIFIER (t1
), TYPE_IDENTIFIER (t2
)))
1404 if (!same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
)))
1409 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1414 if (maybe_ne (TYPE_VECTOR_SUBPARTS (t1
), TYPE_VECTOR_SUBPARTS (t2
))
1415 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1419 case TYPE_PACK_EXPANSION
:
1420 return (same_type_p (PACK_EXPANSION_PATTERN (t1
),
1421 PACK_EXPANSION_PATTERN (t2
))
1422 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1
),
1423 PACK_EXPANSION_EXTRA_ARGS (t2
)));
1426 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1
)
1427 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2
)
1428 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1
)
1429 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2
))
1430 || (DECLTYPE_FOR_LAMBDA_PROXY (t1
)
1431 != DECLTYPE_FOR_LAMBDA_PROXY (t2
))
1432 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1
),
1433 DECLTYPE_TYPE_EXPR (t2
)))
1437 case UNDERLYING_TYPE
:
1438 return same_type_p (UNDERLYING_TYPE_TYPE (t1
),
1439 UNDERLYING_TYPE_TYPE (t2
));
1445 /* Don't treat an alias template specialization with dependent
1446 arguments as equivalent to its underlying type when used as a
1447 template argument; we need them to be distinct so that we
1448 substitute into the specialization arguments at instantiation
1449 time. And aliases can't be equivalent without being ==, so
1450 we don't need to look any deeper. */
1451 if (comparing_specializations
1452 && (dependent_alias_template_spec_p (t1
)
1453 || dependent_alias_template_spec_p (t2
)))
1456 /* If we get here, we know that from a target independent POV the
1457 types are the same. Make sure the target attributes are also
1459 return comp_type_attributes (t1
, t2
);
1462 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1463 is a bitwise-or of the COMPARE_* flags. */
1466 comptypes (tree t1
, tree t2
, int strict
)
1468 if (strict
== COMPARE_STRICT
&& comparing_specializations
1469 && (t1
!= TYPE_CANONICAL (t1
) || t2
!= TYPE_CANONICAL (t2
)))
1470 /* If comparing_specializations, treat dependent aliases as distinct. */
1471 strict
= COMPARE_STRUCTURAL
;
1472 if (strict
== COMPARE_STRICT
)
1477 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1480 if (TYPE_STRUCTURAL_EQUALITY_P (t1
) || TYPE_STRUCTURAL_EQUALITY_P (t2
))
1481 /* At least one of the types requires structural equality, so
1482 perform a deep check. */
1483 return structural_comptypes (t1
, t2
, strict
);
1485 if (flag_checking
&& USE_CANONICAL_TYPES
)
1487 bool result
= structural_comptypes (t1
, t2
, strict
);
1489 if (result
&& TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1490 /* The two types are structurally equivalent, but their
1491 canonical types were different. This is a failure of the
1492 canonical type propagation code.*/
1494 ("canonical types differ for identical types %qT and %qT",
1496 else if (!result
&& TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
))
1497 /* Two types are structurally different, but the canonical
1498 types are the same. This means we were over-eager in
1499 assigning canonical types. */
1501 ("same canonical type node for different types %qT and %qT",
1506 if (!flag_checking
&& USE_CANONICAL_TYPES
)
1507 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1509 return structural_comptypes (t1
, t2
, strict
);
1511 else if (strict
== COMPARE_STRUCTURAL
)
1512 return structural_comptypes (t1
, t2
, COMPARE_STRICT
);
1514 return structural_comptypes (t1
, t2
, strict
);
1517 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1518 top-level qualifiers. */
1521 same_type_ignoring_top_level_qualifiers_p (tree type1
, tree type2
)
1523 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1528 type1
= cp_build_qualified_type (type1
, TYPE_UNQUALIFIED
);
1529 type2
= cp_build_qualified_type (type2
, TYPE_UNQUALIFIED
);
1530 return same_type_p (type1
, type2
);
1533 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1536 at_least_as_qualified_p (const_tree type1
, const_tree type2
)
1538 int q1
= cp_type_quals (type1
);
1539 int q2
= cp_type_quals (type2
);
1541 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1542 return (q1
& q2
) == q2
;
1545 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1546 more cv-qualified that TYPE1, and 0 otherwise. */
1549 comp_cv_qualification (int q1
, int q2
)
1554 if ((q1
& q2
) == q2
)
1556 else if ((q1
& q2
) == q1
)
1563 comp_cv_qualification (const_tree type1
, const_tree type2
)
1565 int q1
= cp_type_quals (type1
);
1566 int q2
= cp_type_quals (type2
);
1567 return comp_cv_qualification (q1
, q2
);
1570 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1571 subset of the cv-qualification signature of TYPE2, and the types
1572 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1575 comp_cv_qual_signature (tree type1
, tree type2
)
1577 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1579 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1585 /* Subroutines of `comptypes'. */
1587 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1588 equivalent in the sense that functions with those parameter types
1589 can have equivalent types. The two lists must be equivalent,
1590 element by element. */
1593 compparms (const_tree parms1
, const_tree parms2
)
1597 /* An unspecified parmlist matches any specified parmlist
1598 whose argument types don't need default promotions. */
1600 for (t1
= parms1
, t2
= parms2
;
1602 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1604 /* If one parmlist is shorter than the other,
1605 they fail to match. */
1608 if (!same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1615 /* Process a sizeof or alignof expression where the operand is a
1616 type. STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
1617 or GNU (preferred alignment) semantics; it is ignored if op is
1621 cxx_sizeof_or_alignof_type (tree type
, enum tree_code op
, bool std_alignof
,
1624 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
1625 if (type
== error_mark_node
)
1626 return error_mark_node
;
1628 type
= non_reference (type
);
1629 if (TREE_CODE (type
) == METHOD_TYPE
)
1633 pedwarn (input_location
, OPT_Wpointer_arith
,
1634 "invalid application of %qs to a member function",
1635 OVL_OP_INFO (false, op
)->name
);
1636 return size_one_node
;
1639 return error_mark_node
;
1642 bool dependent_p
= dependent_type_p (type
);
1644 complete_type (type
);
1646 /* VLA types will have a non-constant size. In the body of an
1647 uninstantiated template, we don't need to try to compute the
1648 value, because the sizeof expression is not an integral
1649 constant expression in that case. And, if we do try to
1650 compute the value, we'll likely end up with SAVE_EXPRs, which
1651 the template substitution machinery does not expect to see. */
1652 || (processing_template_decl
1653 && COMPLETE_TYPE_P (type
)
1654 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
))
1656 tree value
= build_min (op
, size_type_node
, type
);
1657 TREE_READONLY (value
) = 1;
1658 if (op
== ALIGNOF_EXPR
&& std_alignof
)
1659 ALIGNOF_EXPR_STD_P (value
) = true;
1663 return c_sizeof_or_alignof_type (input_location
, complete_type (type
),
1664 op
== SIZEOF_EXPR
, std_alignof
,
1668 /* Return the size of the type, without producing any warnings for
1669 types whose size cannot be taken. This routine should be used only
1670 in some other routine that has already produced a diagnostic about
1671 using the size of such a type. */
1673 cxx_sizeof_nowarn (tree type
)
1675 if (TREE_CODE (type
) == FUNCTION_TYPE
1676 || VOID_TYPE_P (type
)
1677 || TREE_CODE (type
) == ERROR_MARK
)
1678 return size_one_node
;
1679 else if (!COMPLETE_TYPE_P (type
))
1680 return size_zero_node
;
1682 return cxx_sizeof_or_alignof_type (type
, SIZEOF_EXPR
, false, false);
1685 /* Process a sizeof expression where the operand is an expression. */
1688 cxx_sizeof_expr (tree e
, tsubst_flags_t complain
)
1690 if (e
== error_mark_node
)
1691 return error_mark_node
;
1693 if (instantiation_dependent_uneval_expression_p (e
))
1695 e
= build_min (SIZEOF_EXPR
, size_type_node
, e
);
1696 TREE_SIDE_EFFECTS (e
) = 0;
1697 TREE_READONLY (e
) = 1;
1702 STRIP_ANY_LOCATION_WRAPPER (e
);
1704 /* To get the size of a static data member declared as an array of
1705 unknown bound, we need to instantiate it. */
1707 && VAR_HAD_UNKNOWN_BOUND (e
)
1708 && DECL_TEMPLATE_INSTANTIATION (e
))
1709 instantiate_decl (e
, /*defer_ok*/true, /*expl_inst_mem*/false);
1711 if (TREE_CODE (e
) == PARM_DECL
1712 && DECL_ARRAY_PARAMETER_P (e
)
1713 && (complain
& tf_warning
))
1715 auto_diagnostic_group d
;
1716 if (warning (OPT_Wsizeof_array_argument
, "%<sizeof%> on array function "
1717 "parameter %qE will return size of %qT", e
, TREE_TYPE (e
)))
1718 inform (DECL_SOURCE_LOCATION (e
), "declared here");
1721 e
= mark_type_use (e
);
1725 if (complain
& tf_error
)
1726 error ("invalid application of %<sizeof%> to a bit-field");
1728 return error_mark_node
;
1731 else if (is_overloaded_fn (e
))
1733 if (complain
& tf_error
)
1734 permerror (input_location
, "ISO C++ forbids applying %<sizeof%> to an expression of "
1737 return error_mark_node
;
1740 else if (type_unknown_p (e
))
1742 if (complain
& tf_error
)
1743 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1745 return error_mark_node
;
1751 return cxx_sizeof_or_alignof_type (e
, SIZEOF_EXPR
, false, complain
& tf_error
);
1754 /* Implement the __alignof keyword: Return the minimum required
1755 alignment of E, measured in bytes. For VAR_DECL's and
1756 FIELD_DECL's return DECL_ALIGN (which can be set from an
1757 "aligned" __attribute__ specification). */
1760 cxx_alignof_expr (tree e
, tsubst_flags_t complain
)
1764 if (e
== error_mark_node
)
1765 return error_mark_node
;
1767 if (processing_template_decl
)
1769 e
= build_min (ALIGNOF_EXPR
, size_type_node
, e
);
1770 TREE_SIDE_EFFECTS (e
) = 0;
1771 TREE_READONLY (e
) = 1;
1776 STRIP_ANY_LOCATION_WRAPPER (e
);
1778 e
= mark_type_use (e
);
1781 t
= size_int (DECL_ALIGN_UNIT (e
));
1782 else if (bitfield_p (e
))
1784 if (complain
& tf_error
)
1785 error ("invalid application of %<__alignof%> to a bit-field");
1787 return error_mark_node
;
1790 else if (TREE_CODE (e
) == COMPONENT_REF
1791 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
)
1792 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e
, 1)));
1793 else if (is_overloaded_fn (e
))
1795 if (complain
& tf_error
)
1796 permerror (input_location
, "ISO C++ forbids applying %<__alignof%> to an expression of "
1799 return error_mark_node
;
1800 if (TREE_CODE (e
) == FUNCTION_DECL
)
1801 t
= size_int (DECL_ALIGN_UNIT (e
));
1805 else if (type_unknown_p (e
))
1807 if (complain
& tf_error
)
1808 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1810 return error_mark_node
;
1814 return cxx_sizeof_or_alignof_type (TREE_TYPE (e
), ALIGNOF_EXPR
, false,
1815 complain
& tf_error
);
1817 return fold_convert (size_type_node
, t
);
1820 /* Process a sizeof or alignof expression E with code OP where the operand
1821 is an expression. */
1824 cxx_sizeof_or_alignof_expr (tree e
, enum tree_code op
, bool complain
)
1826 if (op
== SIZEOF_EXPR
)
1827 return cxx_sizeof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1829 return cxx_alignof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1832 /* Build a representation of an expression 'alignas(E).' Return the
1833 folded integer value of E if it is an integral constant expression
1834 that resolves to a valid alignment. If E depends on a template
1835 parameter, return a syntactic representation tree of kind
1836 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1837 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1840 cxx_alignas_expr (tree e
)
1842 if (e
== NULL_TREE
|| e
== error_mark_node
1843 || (!TYPE_P (e
) && !require_potential_rvalue_constant_expression (e
)))
1849 When the alignment-specifier is of the form
1850 alignas(type-id ), it shall have the same effect as
1851 alignas(alignof(type-id )). */
1853 return cxx_sizeof_or_alignof_type (e
, ALIGNOF_EXPR
, true, false);
1855 /* If we reach this point, it means the alignas expression if of
1856 the form "alignas(assignment-expression)", so we should follow
1857 what is stated by [dcl.align]/2. */
1859 if (value_dependent_expression_p (e
))
1860 /* Leave value-dependent expression alone for now. */
1863 e
= instantiate_non_dependent_expr (e
);
1864 e
= mark_rvalue_use (e
);
1866 /* [dcl.align]/2 says:
1868 the assignment-expression shall be an integral constant
1871 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e
)))
1873 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e
));
1874 return error_mark_node
;
1877 return cxx_constant_value (e
);
1881 /* EXPR is being used in a context that is not a function call.
1886 The expression can be used only as the left-hand operand of a
1887 member function call.
1889 [expr.mptr.operator]
1891 If the result of .* or ->* is a function, then that result can be
1892 used only as the operand for the function call operator ().
1894 by issuing an error message if appropriate. Returns true iff EXPR
1895 violates these rules. */
1898 invalid_nonstatic_memfn_p (location_t loc
, tree expr
, tsubst_flags_t complain
)
1900 if (expr
== NULL_TREE
)
1902 /* Don't enforce this in MS mode. */
1903 if (flag_ms_extensions
)
1905 if (is_overloaded_fn (expr
) && !really_overloaded_fn (expr
))
1906 expr
= get_first_fn (expr
);
1907 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr
))
1909 if (complain
& tf_error
)
1913 error_at (loc
, "invalid use of non-static member function %qD",
1915 inform (DECL_SOURCE_LOCATION (expr
), "declared here");
1918 error_at (loc
, "invalid use of non-static member function of "
1919 "type %qT", TREE_TYPE (expr
));
1926 /* If EXP is a reference to a bitfield, and the type of EXP does not
1927 match the declared type of the bitfield, return the declared type
1928 of the bitfield. Otherwise, return NULL_TREE. */
1931 is_bitfield_expr_with_lowered_type (const_tree exp
)
1933 switch (TREE_CODE (exp
))
1936 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1)
1937 ? TREE_OPERAND (exp
, 1)
1938 : TREE_OPERAND (exp
, 0)))
1940 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 2));
1943 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1));
1947 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1953 field
= TREE_OPERAND (exp
, 1);
1954 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_BIT_FIELD_TYPE (field
))
1956 if (same_type_ignoring_top_level_qualifiers_p
1957 (TREE_TYPE (exp
), DECL_BIT_FIELD_TYPE (field
)))
1959 return DECL_BIT_FIELD_TYPE (field
);
1963 if (DECL_HAS_VALUE_EXPR_P (exp
))
1964 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
1965 (CONST_CAST_TREE (exp
)));
1968 case VIEW_CONVERT_EXPR
:
1969 if (location_wrapper_p (exp
))
1970 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1975 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp
, 0)))
1976 == TYPE_MAIN_VARIANT (TREE_TYPE (exp
)))
1977 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1985 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1986 bitfield with a lowered type, the type of EXP is returned, rather
1990 unlowered_expr_type (const_tree exp
)
1993 tree etype
= TREE_TYPE (exp
);
1995 type
= is_bitfield_expr_with_lowered_type (exp
);
1997 type
= cp_build_qualified_type (type
, cp_type_quals (etype
));
2004 /* Perform the conversions in [expr] that apply when an lvalue appears
2005 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2006 function-to-pointer conversions. In addition, bitfield references are
2007 converted to their declared types. Note that this function does not perform
2008 the lvalue-to-rvalue conversion for class types. If you need that conversion
2009 for class types, then you probably need to use force_rvalue.
2011 Although the returned value is being used as an rvalue, this
2012 function does not wrap the returned expression in a
2013 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2014 that the return value is no longer an lvalue. */
2017 decay_conversion (tree exp
,
2018 tsubst_flags_t complain
,
2019 bool reject_builtin
/* = true */)
2022 enum tree_code code
;
2023 location_t loc
= cp_expr_loc_or_loc (exp
, input_location
);
2025 type
= TREE_TYPE (exp
);
2026 if (type
== error_mark_node
)
2027 return error_mark_node
;
2029 exp
= resolve_nondeduced_context_or_error (exp
, complain
);
2031 code
= TREE_CODE (type
);
2033 if (error_operand_p (exp
))
2034 return error_mark_node
;
2036 if (NULLPTR_TYPE_P (type
) && !TREE_SIDE_EFFECTS (exp
))
2038 mark_rvalue_use (exp
, loc
, reject_builtin
);
2039 return nullptr_node
;
2042 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2043 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2044 if (code
== VOID_TYPE
)
2046 if (complain
& tf_error
)
2047 error_at (loc
, "void value not ignored as it ought to be");
2048 return error_mark_node
;
2050 if (invalid_nonstatic_memfn_p (loc
, exp
, complain
))
2051 return error_mark_node
;
2052 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
2054 exp
= mark_lvalue_use (exp
);
2055 if (reject_builtin
&& reject_gcc_builtin (exp
, loc
))
2056 return error_mark_node
;
2057 return cp_build_addr_expr (exp
, complain
);
2059 if (code
== ARRAY_TYPE
)
2064 exp
= mark_lvalue_use (exp
);
2066 if (INDIRECT_REF_P (exp
))
2067 return build_nop (build_pointer_type (TREE_TYPE (type
)),
2068 TREE_OPERAND (exp
, 0));
2070 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
2072 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1), complain
);
2073 if (op1
== error_mark_node
)
2074 return error_mark_node
;
2075 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
2076 TREE_OPERAND (exp
, 0), op1
);
2079 if (!obvalue_p (exp
)
2080 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
2082 if (complain
& tf_error
)
2083 error_at (loc
, "invalid use of non-lvalue array");
2084 return error_mark_node
;
2087 /* Don't let an array compound literal decay to a pointer. It can
2088 still be used to initialize an array or bind to a reference. */
2089 if (TREE_CODE (exp
) == TARGET_EXPR
)
2091 if (complain
& tf_error
)
2092 error_at (loc
, "taking address of temporary array");
2093 return error_mark_node
;
2096 ptrtype
= build_pointer_type (TREE_TYPE (type
));
2100 if (!cxx_mark_addressable (exp
))
2101 return error_mark_node
;
2102 adr
= build_nop (ptrtype
, build_address (exp
));
2105 /* This way is better for a COMPONENT_REF since it can
2106 simplify the offset for a component. */
2107 adr
= cp_build_addr_expr (exp
, complain
);
2108 return cp_convert (ptrtype
, adr
, complain
);
2111 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2112 exp
= mark_rvalue_use (exp
, loc
, reject_builtin
);
2114 /* If a bitfield is used in a context where integral promotion
2115 applies, then the caller is expected to have used
2116 default_conversion. That function promotes bitfields correctly
2117 before calling this function. At this point, if we have a
2118 bitfield referenced, we may assume that is not subject to
2119 promotion, and that, therefore, the type of the resulting rvalue
2120 is the declared type of the bitfield. */
2121 exp
= convert_bitfield_to_declared_type (exp
);
2123 /* We do not call rvalue() here because we do not want to wrap EXP
2124 in a NON_LVALUE_EXPR. */
2128 Non-class rvalues always have cv-unqualified types. */
2129 type
= TREE_TYPE (exp
);
2130 if (!CLASS_TYPE_P (type
) && cv_qualified_p (type
))
2131 exp
= build_nop (cv_unqualified (type
), exp
);
2133 if (!complete_type_or_maybe_complain (type
, exp
, complain
))
2134 return error_mark_node
;
2139 /* Perform preparatory conversions, as part of the "usual arithmetic
2140 conversions". In particular, as per [expr]:
2142 Whenever an lvalue expression appears as an operand of an
2143 operator that expects the rvalue for that operand, the
2144 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2145 standard conversions are applied to convert the expression to an
2148 In addition, we perform integral promotions here, as those are
2149 applied to both operands to a binary operator before determining
2150 what additional conversions should apply. */
2153 cp_default_conversion (tree exp
, tsubst_flags_t complain
)
2155 /* Check for target-specific promotions. */
2156 tree promoted_type
= targetm
.promoted_type (TREE_TYPE (exp
));
2158 exp
= cp_convert (promoted_type
, exp
, complain
);
2159 /* Perform the integral promotions first so that bitfield
2160 expressions (which may promote to "int", even if the bitfield is
2161 declared "unsigned") are promoted correctly. */
2162 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp
)))
2163 exp
= cp_perform_integral_promotions (exp
, complain
);
2164 /* Perform the other conversions. */
2165 exp
= decay_conversion (exp
, complain
);
2173 default_conversion (tree exp
)
2175 return cp_default_conversion (exp
, tf_warning_or_error
);
2178 /* EXPR is an expression with an integral or enumeration type.
2179 Perform the integral promotions in [conv.prom], and return the
2183 cp_perform_integral_promotions (tree expr
, tsubst_flags_t complain
)
2188 expr
= mark_rvalue_use (expr
);
2189 if (error_operand_p (expr
))
2190 return error_mark_node
;
2194 If the bitfield has an enumerated type, it is treated as any
2195 other value of that type for promotion purposes. */
2196 type
= is_bitfield_expr_with_lowered_type (expr
);
2197 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
2198 type
= TREE_TYPE (expr
);
2199 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
2200 /* Scoped enums don't promote. */
2201 if (SCOPED_ENUM_P (type
))
2203 promoted_type
= type_promotes_to (type
);
2204 if (type
!= promoted_type
)
2205 expr
= cp_convert (promoted_type
, expr
, complain
);
2212 perform_integral_promotions (tree expr
)
2214 return cp_perform_integral_promotions (expr
, tf_warning_or_error
);
2217 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2218 decay_conversion to one. */
2221 string_conv_p (const_tree totype
, const_tree exp
, int warn
)
2225 if (!TYPE_PTR_P (totype
))
2228 t
= TREE_TYPE (totype
);
2229 if (!same_type_p (t
, char_type_node
)
2230 && !same_type_p (t
, char8_type_node
)
2231 && !same_type_p (t
, char16_type_node
)
2232 && !same_type_p (t
, char32_type_node
)
2233 && !same_type_p (t
, wchar_type_node
))
2236 location_t loc
= EXPR_LOC_OR_LOC (exp
, input_location
);
2238 STRIP_ANY_LOCATION_WRAPPER (exp
);
2240 if (TREE_CODE (exp
) == STRING_CST
)
2242 /* Make sure that we don't try to convert between char and wide chars. */
2243 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
2248 /* Is this a string constant which has decayed to 'const char *'? */
2249 t
= build_pointer_type (cp_build_qualified_type (t
, TYPE_QUAL_CONST
));
2250 if (!same_type_p (TREE_TYPE (exp
), t
))
2253 if (TREE_CODE (exp
) != ADDR_EXPR
2254 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
2259 if (cxx_dialect
>= cxx11
)
2260 pedwarn (loc
, OPT_Wwrite_strings
,
2261 "ISO C++ forbids converting a string constant to %qT",
2264 warning_at (loc
, OPT_Wwrite_strings
,
2265 "deprecated conversion from string constant to %qT",
2272 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2273 can, for example, use as an lvalue. This code used to be in
2274 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2275 expressions, where we're dealing with aggregates. But now it's again only
2276 called from unary_complex_lvalue. The case (in particular) that led to
2277 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2281 rationalize_conditional_expr (enum tree_code code
, tree t
,
2282 tsubst_flags_t complain
)
2284 location_t loc
= cp_expr_loc_or_loc (t
, input_location
);
2286 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2287 the first operand is always the one to be used if both operands
2288 are equal, so we know what conditional expression this used to be. */
2289 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
2291 tree op0
= TREE_OPERAND (t
, 0);
2292 tree op1
= TREE_OPERAND (t
, 1);
2294 /* The following code is incorrect if either operand side-effects. */
2295 gcc_assert (!TREE_SIDE_EFFECTS (op0
)
2296 && !TREE_SIDE_EFFECTS (op1
));
2298 build_conditional_expr (loc
,
2299 build_x_binary_op (loc
,
2300 (TREE_CODE (t
) == MIN_EXPR
2301 ? LE_EXPR
: GE_EXPR
),
2302 op0
, TREE_CODE (op0
),
2303 op1
, TREE_CODE (op1
),
2306 cp_build_unary_op (code
, op0
, false, complain
),
2307 cp_build_unary_op (code
, op1
, false, complain
),
2312 build_conditional_expr (loc
, TREE_OPERAND (t
, 0),
2313 cp_build_unary_op (code
, TREE_OPERAND (t
, 1), false,
2315 cp_build_unary_op (code
, TREE_OPERAND (t
, 2), false,
2320 /* Given the TYPE of an anonymous union field inside T, return the
2321 FIELD_DECL for the field. If not found return NULL_TREE. Because
2322 anonymous unions can nest, we must also search all anonymous unions
2323 that are directly reachable. */
2326 lookup_anon_field (tree t
, tree type
)
2330 t
= TYPE_MAIN_VARIANT (t
);
2332 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
2334 if (TREE_STATIC (field
))
2336 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
2339 /* If we find it directly, return the field. */
2340 if (DECL_NAME (field
) == NULL_TREE
2341 && type
== TYPE_MAIN_VARIANT (TREE_TYPE (field
)))
2346 /* Otherwise, it could be nested, search harder. */
2347 if (DECL_NAME (field
) == NULL_TREE
2348 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2350 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
2358 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2359 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2360 non-NULL, it indicates the path to the base used to name MEMBER.
2361 If PRESERVE_REFERENCE is true, the expression returned will have
2362 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2363 returned will have the type referred to by the reference.
2365 This function does not perform access control; that is either done
2366 earlier by the parser when the name of MEMBER is resolved to MEMBER
2367 itself, or later when overload resolution selects one of the
2368 functions indicated by MEMBER. */
2371 build_class_member_access_expr (cp_expr object
, tree member
,
2372 tree access_path
, bool preserve_reference
,
2373 tsubst_flags_t complain
)
2377 tree result
= NULL_TREE
;
2378 tree using_decl
= NULL_TREE
;
2380 if (error_operand_p (object
) || error_operand_p (member
))
2381 return error_mark_node
;
2383 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2387 The type of the first expression shall be "class object" (of a
2389 object_type
= TREE_TYPE (object
);
2390 if (!currently_open_class (object_type
)
2391 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2392 return error_mark_node
;
2393 if (!CLASS_TYPE_P (object_type
))
2395 if (complain
& tf_error
)
2397 if (INDIRECT_TYPE_P (object_type
)
2398 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2399 error ("request for member %qD in %qE, which is of pointer "
2400 "type %qT (maybe you meant to use %<->%> ?)",
2401 member
, object
.get_value (), object_type
);
2403 error ("request for member %qD in %qE, which is of non-class "
2404 "type %qT", member
, object
.get_value (), object_type
);
2406 return error_mark_node
;
2409 /* The standard does not seem to actually say that MEMBER must be a
2410 member of OBJECT_TYPE. However, that is clearly what is
2412 if (DECL_P (member
))
2414 member_scope
= DECL_CLASS_CONTEXT (member
);
2415 if (!mark_used (member
, complain
) && !(complain
& tf_error
))
2416 return error_mark_node
;
2417 if (TREE_DEPRECATED (member
))
2418 warn_deprecated_use (member
, NULL_TREE
);
2421 member_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (member
));
2422 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2423 presently be the anonymous union. Go outwards until we find a
2424 type related to OBJECT_TYPE. */
2425 while ((ANON_AGGR_TYPE_P (member_scope
) || UNSCOPED_ENUM_P (member_scope
))
2426 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
2428 member_scope
= TYPE_CONTEXT (member_scope
);
2429 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
2431 if (complain
& tf_error
)
2433 if (TREE_CODE (member
) == FIELD_DECL
)
2434 error ("invalid use of nonstatic data member %qE", member
);
2436 error ("%qD is not a member of %qT", member
, object_type
);
2438 return error_mark_node
;
2441 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2442 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2443 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2445 tree temp
= unary_complex_lvalue (ADDR_EXPR
, object
);
2448 temp
= cp_build_fold_indirect_ref (temp
);
2449 if (xvalue_p (object
) && !xvalue_p (temp
))
2450 /* Preserve xvalue kind. */
2456 /* In [expr.ref], there is an explicit list of the valid choices for
2457 MEMBER. We check for each of those cases here. */
2460 /* A static data member. */
2462 mark_exp_read (object
);
2464 if (tree wrap
= maybe_get_tls_wrapper_call (result
))
2465 /* Replace an evaluated use of the thread_local variable with
2466 a call to its wrapper. */
2469 /* If OBJECT has side-effects, they are supposed to occur. */
2470 if (TREE_SIDE_EFFECTS (object
))
2471 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
2473 else if (TREE_CODE (member
) == FIELD_DECL
)
2475 /* A non-static data member. */
2480 if (INDIRECT_REF_P (object
))
2482 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object
, 0)));
2484 null_object_p
= false;
2486 /* Convert OBJECT to the type of MEMBER. */
2487 if (!same_type_p (TYPE_MAIN_VARIANT (object_type
),
2488 TYPE_MAIN_VARIANT (member_scope
)))
2493 /* We didn't complain above about a currently open class, but now we
2494 must: we don't know how to refer to a base member before layout is
2495 complete. But still don't complain in a template. */
2496 if (!cp_unevaluated_operand
2497 && !dependent_type_p (object_type
)
2498 && !complete_type_or_maybe_complain (object_type
, object
,
2500 return error_mark_node
;
2502 binfo
= lookup_base (access_path
? access_path
: object_type
,
2503 member_scope
, ba_unique
, &kind
, complain
);
2504 if (binfo
== error_mark_node
)
2505 return error_mark_node
;
2507 /* It is invalid to try to get to a virtual base of a
2508 NULL object. The most common cause is invalid use of
2510 if (null_object_p
&& kind
== bk_via_virtual
)
2512 if (complain
& tf_error
)
2514 error ("invalid access to non-static data member %qD in "
2515 "virtual base of NULL object", member
);
2517 return error_mark_node
;
2520 /* Convert to the base. */
2521 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
2522 /*nonnull=*/1, complain
);
2523 /* If we found the base successfully then we should be able
2524 to convert to it successfully. */
2525 gcc_assert (object
!= error_mark_node
);
2528 /* If MEMBER is from an anonymous aggregate, we have converted
2529 OBJECT so that it refers to the class containing the
2530 anonymous union. Generate a reference to the anonymous union
2531 itself, and recur to find MEMBER. */
2532 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
))
2533 /* When this code is called from build_field_call, the
2534 object already has the type of the anonymous union.
2535 That is because the COMPONENT_REF was already
2536 constructed, and was then disassembled before calling
2537 build_field_call. After the function-call code is
2538 cleaned up, this waste can be eliminated. */
2539 && (!same_type_ignoring_top_level_qualifiers_p
2540 (TREE_TYPE (object
), DECL_CONTEXT (member
))))
2542 tree anonymous_union
;
2544 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
2545 DECL_CONTEXT (member
));
2546 object
= build_class_member_access_expr (object
,
2548 /*access_path=*/NULL_TREE
,
2553 /* Compute the type of the field, as described in [expr.ref]. */
2554 type_quals
= TYPE_UNQUALIFIED
;
2555 member_type
= TREE_TYPE (member
);
2556 if (!TYPE_REF_P (member_type
))
2558 type_quals
= (cp_type_quals (member_type
)
2559 | cp_type_quals (object_type
));
2561 /* A field is const (volatile) if the enclosing object, or the
2562 field itself, is const (volatile). But, a mutable field is
2563 not const, even within a const object. */
2564 if (DECL_MUTABLE_P (member
))
2565 type_quals
&= ~TYPE_QUAL_CONST
;
2566 member_type
= cp_build_qualified_type (member_type
, type_quals
);
2569 result
= build3_loc (input_location
, COMPONENT_REF
, member_type
,
2570 object
, member
, NULL_TREE
);
2572 /* Mark the expression const or volatile, as appropriate. Even
2573 though we've dealt with the type above, we still have to mark the
2574 expression itself. */
2575 if (type_quals
& TYPE_QUAL_CONST
)
2576 TREE_READONLY (result
) = 1;
2577 if (type_quals
& TYPE_QUAL_VOLATILE
)
2578 TREE_THIS_VOLATILE (result
) = 1;
2580 else if (BASELINK_P (member
))
2582 /* The member is a (possibly overloaded) member function. */
2586 /* If the MEMBER is exactly one static member function, then we
2587 know the type of the expression. Otherwise, we must wait
2588 until overload resolution has been performed. */
2589 functions
= BASELINK_FUNCTIONS (member
);
2590 if (TREE_CODE (functions
) == FUNCTION_DECL
2591 && DECL_STATIC_FUNCTION_P (functions
))
2592 type
= TREE_TYPE (functions
);
2594 type
= unknown_type_node
;
2595 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2596 base. That will happen when the function is called. */
2597 result
= build3_loc (input_location
, COMPONENT_REF
, type
, object
, member
,
2600 else if (TREE_CODE (member
) == CONST_DECL
)
2602 /* The member is an enumerator. */
2604 /* If OBJECT has side-effects, they are supposed to occur. */
2605 if (TREE_SIDE_EFFECTS (object
))
2606 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
2609 else if ((using_decl
= strip_using_decl (member
)) != member
)
2610 result
= build_class_member_access_expr (object
,
2612 access_path
, preserve_reference
,
2616 if (complain
& tf_error
)
2617 error ("invalid use of %qD", member
);
2618 return error_mark_node
;
2621 if (!preserve_reference
)
2624 If E2 is declared to have type "reference to T", then ... the
2625 type of E1.E2 is T. */
2626 result
= convert_from_reference (result
);
2631 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2632 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2635 lookup_destructor (tree object
, tree scope
, tree dtor_name
,
2636 tsubst_flags_t complain
)
2638 tree object_type
= TREE_TYPE (object
);
2639 tree dtor_type
= TREE_OPERAND (dtor_name
, 0);
2642 /* We've already complained about this destructor. */
2643 if (dtor_type
== error_mark_node
)
2644 return error_mark_node
;
2646 if (scope
&& !check_dtor_name (scope
, dtor_type
))
2648 if (complain
& tf_error
)
2649 error ("qualified type %qT does not match destructor name ~%qT",
2651 return error_mark_node
;
2653 if (is_auto (dtor_type
))
2654 dtor_type
= object_type
;
2655 else if (identifier_p (dtor_type
))
2657 /* In a template, names we can't find a match for are still accepted
2658 destructor names, and we check them here. */
2659 if (check_dtor_name (object_type
, dtor_type
))
2660 dtor_type
= object_type
;
2663 if (complain
& tf_error
)
2664 error ("object type %qT does not match destructor name ~%qT",
2665 object_type
, dtor_type
);
2666 return error_mark_node
;
2670 else if (!DERIVED_FROM_P (dtor_type
, TYPE_MAIN_VARIANT (object_type
)))
2672 if (complain
& tf_error
)
2673 error ("the type being destroyed is %qT, but the destructor "
2674 "refers to %qT", TYPE_MAIN_VARIANT (object_type
), dtor_type
);
2675 return error_mark_node
;
2677 expr
= lookup_member (dtor_type
, complete_dtor_identifier
,
2678 /*protect=*/1, /*want_type=*/false,
2679 tf_warning_or_error
);
2682 if (complain
& tf_error
)
2683 cxx_incomplete_type_error (dtor_name
, dtor_type
);
2684 return error_mark_node
;
2686 expr
= (adjust_result_of_qualified_name_lookup
2687 (expr
, dtor_type
, object_type
));
2688 if (scope
== NULL_TREE
)
2689 /* We need to call adjust_result_of_qualified_name_lookup in case the
2690 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2691 that we still get virtual function binding. */
2692 BASELINK_QUALIFIED_P (expr
) = false;
2696 /* An expression of the form "A::template B" has been resolved to
2697 DECL. Issue a diagnostic if B is not a template or template
2701 check_template_keyword (tree decl
)
2703 /* The standard says:
2707 If a name prefixed by the keyword template is not a member
2708 template, the program is ill-formed.
2710 DR 228 removed the restriction that the template be a member
2713 DR 96, if accepted would add the further restriction that explicit
2714 template arguments must be provided if the template keyword is
2715 used, but, as of 2005-10-16, that DR is still in "drafting". If
2716 this DR is accepted, then the semantic checks here can be
2717 simplified, as the entity named must in fact be a template
2718 specialization, rather than, as at present, a set of overloaded
2719 functions containing at least one template function. */
2720 if (TREE_CODE (decl
) != TEMPLATE_DECL
2721 && TREE_CODE (decl
) != TEMPLATE_ID_EXPR
)
2725 if (DECL_USE_TEMPLATE (decl
)
2726 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2729 permerror (input_location
, "%qD is not a template", decl
);
2731 else if (!is_overloaded_fn (decl
))
2732 permerror (input_location
, "%qD is not a template", decl
);
2737 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
2738 !found
&& iter
; ++iter
)
2741 if (TREE_CODE (fn
) == TEMPLATE_DECL
2742 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
2743 || (TREE_CODE (fn
) == FUNCTION_DECL
2744 && DECL_USE_TEMPLATE (fn
)
2745 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn
))))
2749 permerror (input_location
, "%qD is not a template", decl
);
2754 /* Record that an access failure occurred on BASETYPE_PATH attempting
2755 to access DECL, where DIAG_DECL should be used for diagnostics. */
2758 access_failure_info::record_access_failure (tree basetype_path
,
2759 tree decl
, tree diag_decl
)
2761 m_was_inaccessible
= true;
2762 m_basetype_path
= basetype_path
;
2764 m_diag_decl
= diag_decl
;
2767 /* If an access failure was recorded, then attempt to locate an
2768 accessor function for the pertinent field.
2769 Otherwise, return NULL_TREE. */
2772 access_failure_info::get_any_accessor (bool const_p
) const
2774 if (!was_inaccessible_p ())
2778 = locate_field_accessor (m_basetype_path
, m_diag_decl
, const_p
);
2782 /* The accessor must itself be accessible for it to be a reasonable
2784 if (!accessible_p (m_basetype_path
, accessor
, true))
2790 /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
2791 replacing the primary location in RICHLOC with "accessor()". */
2794 access_failure_info::add_fixit_hint (rich_location
*richloc
,
2798 pp_printf (&pp
, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor_decl
)));
2799 richloc
->add_fixit_replace (pp_formatted_text (&pp
));
2802 /* If an access failure was recorded, then attempt to locate an
2803 accessor function for the pertinent field, and if one is
2804 available, add a note and fix-it hint suggesting using it. */
2807 access_failure_info::maybe_suggest_accessor (bool const_p
) const
2809 tree accessor
= get_any_accessor (const_p
);
2810 if (accessor
== NULL_TREE
)
2812 rich_location
richloc (line_table
, input_location
);
2813 add_fixit_hint (&richloc
, accessor
);
2814 inform (&richloc
, "field %q#D can be accessed via %q#D",
2815 m_diag_decl
, accessor
);
2818 /* Subroutine of finish_class_member_access_expr.
2819 Issue an error about NAME not being a member of ACCESS_PATH (or
2820 OBJECT_TYPE), potentially providing a fix-it hint for misspelled
2824 complain_about_unrecognized_member (tree access_path
, tree name
,
2827 /* Attempt to provide a hint about misspelled names. */
2828 tree guessed_id
= lookup_member_fuzzy (access_path
, name
,
2829 /*want_type=*/false);
2830 if (guessed_id
== NULL_TREE
)
2833 error ("%q#T has no member named %qE",
2834 TREE_CODE (access_path
) == TREE_BINFO
2835 ? TREE_TYPE (access_path
) : object_type
, name
);
2839 location_t bogus_component_loc
= input_location
;
2840 gcc_rich_location
rich_loc (bogus_component_loc
);
2842 /* Check that the guessed name is accessible along access_path. */
2843 access_failure_info afi
;
2844 lookup_member (access_path
, guessed_id
, /*protect=*/1,
2845 /*want_type=*/false, /*complain=*/false,
2847 if (afi
.was_inaccessible_p ())
2849 tree accessor
= afi
.get_any_accessor (TYPE_READONLY (object_type
));
2852 /* The guessed name isn't directly accessible, but can be accessed
2853 via an accessor member function. */
2854 afi
.add_fixit_hint (&rich_loc
, accessor
);
2855 error_at (&rich_loc
,
2856 "%q#T has no member named %qE;"
2857 " did you mean %q#D? (accessible via %q#D)",
2858 TREE_CODE (access_path
) == TREE_BINFO
2859 ? TREE_TYPE (access_path
) : object_type
,
2860 name
, afi
.get_diag_decl (), accessor
);
2864 /* The guessed name isn't directly accessible, and no accessor
2865 member function could be found. */
2866 error_at (&rich_loc
,
2867 "%q#T has no member named %qE;"
2868 " did you mean %q#D? (not accessible from this context)",
2869 TREE_CODE (access_path
) == TREE_BINFO
2870 ? TREE_TYPE (access_path
) : object_type
,
2871 name
, afi
.get_diag_decl ());
2872 complain_about_access (afi
.get_decl (), afi
.get_diag_decl (), false);
2877 /* The guessed name is directly accessible; suggest it. */
2878 rich_loc
.add_fixit_misspelled_id (bogus_component_loc
,
2880 error_at (&rich_loc
,
2881 "%q#T has no member named %qE;"
2882 " did you mean %qE?",
2883 TREE_CODE (access_path
) == TREE_BINFO
2884 ? TREE_TYPE (access_path
) : object_type
,
2889 /* This function is called by the parser to process a class member
2890 access expression of the form OBJECT.NAME. NAME is a node used by
2891 the parser to represent a name; it is not yet a DECL. It may,
2892 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2893 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2894 there is no reason to do the lookup twice, so the parser keeps the
2895 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2896 be a template via the use of the "A::template B" syntax. */
2899 finish_class_member_access_expr (cp_expr object
, tree name
, bool template_p
,
2900 tsubst_flags_t complain
)
2905 tree access_path
= NULL_TREE
;
2906 tree orig_object
= object
;
2907 tree orig_name
= name
;
2909 if (object
== error_mark_node
|| name
== error_mark_node
)
2910 return error_mark_node
;
2912 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2913 if (!objc_is_public (object
, name
))
2914 return error_mark_node
;
2916 object_type
= TREE_TYPE (object
);
2918 if (processing_template_decl
)
2920 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2921 type_dependent_object_expression_p (object
)
2922 /* If NAME is "f<args>", where either 'f' or 'args' is
2923 dependent, then the expression is dependent. */
2924 || (TREE_CODE (name
) == TEMPLATE_ID_EXPR
2925 && dependent_template_id_p (TREE_OPERAND (name
, 0),
2926 TREE_OPERAND (name
, 1)))
2927 /* If NAME is "T::X" where "T" is dependent, then the
2928 expression is dependent. */
2929 || (TREE_CODE (name
) == SCOPE_REF
2930 && TYPE_P (TREE_OPERAND (name
, 0))
2931 && dependent_scope_p (TREE_OPERAND (name
, 0)))
2932 /* If NAME is operator T where "T" is dependent, we can't
2933 lookup until we instantiate the T. */
2934 || (TREE_CODE (name
) == IDENTIFIER_NODE
2935 && IDENTIFIER_CONV_OP_P (name
)
2936 && dependent_type_p (TREE_TYPE (name
))))
2939 return build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
,
2940 orig_object
, orig_name
, NULL_TREE
);
2942 object
= build_non_dependent_expr (object
);
2944 else if (c_dialect_objc ()
2945 && identifier_p (name
)
2946 && (expr
= objc_maybe_build_component_ref (object
, name
)))
2951 The type of the first expression shall be "class object" (of a
2953 if (!currently_open_class (object_type
)
2954 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2955 return error_mark_node
;
2956 if (!CLASS_TYPE_P (object_type
))
2958 if (complain
& tf_error
)
2960 if (INDIRECT_TYPE_P (object_type
)
2961 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2962 error ("request for member %qD in %qE, which is of pointer "
2963 "type %qT (maybe you meant to use %<->%> ?)",
2964 name
, object
.get_value (), object_type
);
2966 error ("request for member %qD in %qE, which is of non-class "
2967 "type %qT", name
, object
.get_value (), object_type
);
2969 return error_mark_node
;
2972 if (BASELINK_P (name
))
2973 /* A member function that has already been looked up. */
2977 bool is_template_id
= false;
2978 tree template_args
= NULL_TREE
;
2979 tree scope
= NULL_TREE
;
2981 access_path
= object_type
;
2983 if (TREE_CODE (name
) == SCOPE_REF
)
2985 /* A qualified name. The qualifying class or namespace `S'
2986 has already been looked up; it is either a TYPE or a
2988 scope
= TREE_OPERAND (name
, 0);
2989 name
= TREE_OPERAND (name
, 1);
2991 /* If SCOPE is a namespace, then the qualified name does not
2992 name a member of OBJECT_TYPE. */
2993 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
2995 if (complain
& tf_error
)
2996 error ("%<%D::%D%> is not a member of %qT",
2997 scope
, name
, object_type
);
2998 return error_mark_node
;
3002 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
3004 is_template_id
= true;
3005 template_args
= TREE_OPERAND (name
, 1);
3006 name
= TREE_OPERAND (name
, 0);
3008 if (!identifier_p (name
))
3009 name
= OVL_NAME (name
);
3014 if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
3016 gcc_assert (!is_template_id
);
3017 /* Looking up a member enumerator (c++/56793). */
3018 if (!TYPE_CLASS_SCOPE_P (scope
)
3019 || !DERIVED_FROM_P (TYPE_CONTEXT (scope
), object_type
))
3021 if (complain
& tf_error
)
3022 error ("%<%D::%D%> is not a member of %qT",
3023 scope
, name
, object_type
);
3024 return error_mark_node
;
3026 tree val
= lookup_enumerator (scope
, name
);
3029 if (complain
& tf_error
)
3030 error ("%qD is not a member of %qD",
3032 return error_mark_node
;
3035 if (TREE_SIDE_EFFECTS (object
))
3036 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
), object
, val
);
3040 gcc_assert (CLASS_TYPE_P (scope
));
3041 gcc_assert (identifier_p (name
) || TREE_CODE (name
) == BIT_NOT_EXPR
);
3043 if (constructor_name_p (name
, scope
))
3045 if (complain
& tf_error
)
3046 error ("cannot call constructor %<%T::%D%> directly",
3048 return error_mark_node
;
3051 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3052 access_path
= lookup_base (object_type
, scope
, ba_check
,
3054 if (access_path
== error_mark_node
)
3055 return error_mark_node
;
3058 if (any_dependent_bases_p (object_type
))
3060 if (complain
& tf_error
)
3061 error ("%qT is not a base of %qT", scope
, object_type
);
3062 return error_mark_node
;
3066 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
3068 if (dependent_type_p (object_type
))
3069 /* The destructor isn't declared yet. */
3071 member
= lookup_destructor (object
, scope
, name
, complain
);
3075 /* Look up the member. */
3076 access_failure_info afi
;
3077 member
= lookup_member (access_path
, name
, /*protect=*/1,
3078 /*want_type=*/false, complain
,
3080 afi
.maybe_suggest_accessor (TYPE_READONLY (object_type
));
3081 if (member
== NULL_TREE
)
3083 if (dependent_type_p (object_type
))
3084 /* Try again at instantiation time. */
3086 if (complain
& tf_error
)
3087 complain_about_unrecognized_member (access_path
, name
,
3089 return error_mark_node
;
3091 if (member
== error_mark_node
)
3092 return error_mark_node
;
3094 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member
)))
3095 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3096 wrong, so don't use it. */
3098 if (TREE_CODE (member
) == USING_DECL
&& DECL_DEPENDENT_P (member
))
3104 tree templ
= member
;
3106 if (BASELINK_P (templ
))
3107 member
= lookup_template_function (templ
, template_args
);
3108 else if (variable_template_p (templ
))
3109 member
= (lookup_and_finish_template_variable
3110 (templ
, template_args
, complain
));
3113 if (complain
& tf_error
)
3114 error ("%qD is not a member template function", name
);
3115 return error_mark_node
;
3120 if (TREE_DEPRECATED (member
))
3121 warn_deprecated_use (member
, NULL_TREE
);
3124 check_template_keyword (member
);
3126 expr
= build_class_member_access_expr (object
, member
, access_path
,
3127 /*preserve_reference=*/false,
3129 if (processing_template_decl
&& expr
!= error_mark_node
)
3131 if (BASELINK_P (member
))
3133 if (TREE_CODE (orig_name
) == SCOPE_REF
)
3134 BASELINK_QUALIFIED_P (member
) = 1;
3137 return build_min_non_dep (COMPONENT_REF
, expr
,
3138 orig_object
, orig_name
,
3145 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3149 build_simple_component_ref (tree object
, tree member
)
3151 tree type
= cp_build_qualified_type (TREE_TYPE (member
),
3152 cp_type_quals (TREE_TYPE (object
)));
3153 return build3_loc (input_location
,
3154 COMPONENT_REF
, type
,
3155 object
, member
, NULL_TREE
);
3158 /* Return an expression for the MEMBER_NAME field in the internal
3159 representation of PTRMEM, a pointer-to-member function. (Each
3160 pointer-to-member function type gets its own RECORD_TYPE so it is
3161 more convenient to access the fields by name than by FIELD_DECL.)
3162 This routine converts the NAME to a FIELD_DECL and then creates the
3163 node for the complete expression. */
3166 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
3171 if (TREE_CODE (ptrmem
) == CONSTRUCTOR
)
3175 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ptrmem
),
3177 if (index
&& DECL_P (index
) && DECL_NAME (index
) == member_name
)
3182 /* This code is a stripped down version of
3183 build_class_member_access_expr. It does not work to use that
3184 routine directly because it expects the object to be of class
3186 ptrmem_type
= TREE_TYPE (ptrmem
);
3187 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
));
3188 for (member
= TYPE_FIELDS (ptrmem_type
); member
;
3189 member
= DECL_CHAIN (member
))
3190 if (DECL_NAME (member
) == member_name
)
3192 tree res
= build_simple_component_ref (ptrmem
, member
);
3194 TREE_NO_WARNING (res
) = 1;
3198 /* Given an expression PTR for a pointer, return an expression
3199 for the value pointed to.
3200 ERRORSTRING is the name of the operator to appear in error messages.
3202 This function may need to overload OPERATOR_FNNAME.
3203 Must also handle REFERENCE_TYPEs for C++. */
3206 build_x_indirect_ref (location_t loc
, tree expr
, ref_operator errorstring
,
3207 tsubst_flags_t complain
)
3209 tree orig_expr
= expr
;
3211 tree overload
= NULL_TREE
;
3213 if (processing_template_decl
)
3215 /* Retain the type if we know the operand is a pointer. */
3216 if (TREE_TYPE (expr
) && INDIRECT_TYPE_P (TREE_TYPE (expr
)))
3217 return build_min (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
3218 if (type_dependent_expression_p (expr
))
3219 return build_min_nt_loc (loc
, INDIRECT_REF
, expr
);
3220 expr
= build_non_dependent_expr (expr
);
3223 rval
= build_new_op (loc
, INDIRECT_REF
, LOOKUP_NORMAL
, expr
,
3224 NULL_TREE
, NULL_TREE
, &overload
, complain
);
3226 rval
= cp_build_indirect_ref (expr
, errorstring
, complain
);
3228 if (processing_template_decl
&& rval
!= error_mark_node
)
3230 if (overload
!= NULL_TREE
)
3231 return (build_min_non_dep_op_overload
3232 (INDIRECT_REF
, rval
, overload
, orig_expr
));
3234 return build_min_non_dep (INDIRECT_REF
, rval
, orig_expr
);
3240 /* The implementation of the above, and of indirection implied by other
3241 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3244 cp_build_indirect_ref_1 (tree ptr
, ref_operator errorstring
,
3245 tsubst_flags_t complain
, bool do_fold
)
3249 /* RO_NULL should only be used with the folding entry points below, not
3250 cp_build_indirect_ref. */
3251 gcc_checking_assert (errorstring
!= RO_NULL
|| do_fold
);
3253 if (ptr
== current_class_ptr
3254 || (TREE_CODE (ptr
) == NOP_EXPR
3255 && TREE_OPERAND (ptr
, 0) == current_class_ptr
3256 && (same_type_ignoring_top_level_qualifiers_p
3257 (TREE_TYPE (ptr
), TREE_TYPE (current_class_ptr
)))))
3258 return current_class_ref
;
3260 pointer
= (TYPE_REF_P (TREE_TYPE (ptr
))
3261 ? ptr
: decay_conversion (ptr
, complain
));
3262 if (pointer
== error_mark_node
)
3263 return error_mark_node
;
3265 type
= TREE_TYPE (pointer
);
3267 if (INDIRECT_TYPE_P (type
))
3271 If the type of the expression is "pointer to T," the type
3272 of the result is "T." */
3273 tree t
= TREE_TYPE (type
);
3275 if ((CONVERT_EXPR_P (ptr
)
3276 || TREE_CODE (ptr
) == VIEW_CONVERT_EXPR
)
3277 && (!CLASS_TYPE_P (t
) || !CLASSTYPE_EMPTY_P (t
)))
3279 /* If a warning is issued, mark it to avoid duplicates from
3280 the backend. This only needs to be done at
3281 warn_strict_aliasing > 2. */
3282 if (warn_strict_aliasing
> 2)
3283 if (strict_aliasing_warning (EXPR_LOCATION (ptr
),
3284 type
, TREE_OPERAND (ptr
, 0)))
3285 TREE_NO_WARNING (ptr
) = 1;
3288 if (VOID_TYPE_P (t
))
3290 /* A pointer to incomplete type (other than cv void) can be
3291 dereferenced [expr.unary.op]/1 */
3292 if (complain
& tf_error
)
3293 error ("%qT is not a pointer-to-object type", type
);
3294 return error_mark_node
;
3296 else if (do_fold
&& TREE_CODE (pointer
) == ADDR_EXPR
3297 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
3298 /* The POINTER was something like `&x'. We simplify `*&x' to
3300 return TREE_OPERAND (pointer
, 0);
3303 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
3305 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3306 so that we get the proper error message if the result is used
3307 to assign to. Also, &* is supposed to be a no-op. */
3308 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
3309 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
3310 TREE_SIDE_EFFECTS (ref
)
3311 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
));
3315 else if (!(complain
& tf_error
))
3316 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3318 /* `pointer' won't be an error_mark_node if we were given a
3319 pointer to member, so it's cool to check for this here. */
3320 else if (TYPE_PTRMEM_P (type
))
3321 switch (errorstring
)
3323 case RO_ARRAY_INDEXING
:
3324 error ("invalid use of array indexing on pointer to member");
3327 error ("invalid use of unary %<*%> on pointer to member");
3329 case RO_IMPLICIT_CONVERSION
:
3330 error ("invalid use of implicit conversion on pointer to member");
3333 error ("left hand operand of %<->*%> must be a pointer to class, "
3334 "but is a pointer to member of type %qT", type
);
3339 else if (pointer
!= error_mark_node
)
3340 invalid_indirection_error (input_location
, type
, errorstring
);
3342 return error_mark_node
;
3345 /* Entry point used by c-common, which expects folding. */
3348 build_indirect_ref (location_t
/*loc*/,
3349 tree ptr
, ref_operator errorstring
)
3351 return cp_build_indirect_ref_1 (ptr
, errorstring
, tf_warning_or_error
, true);
3354 /* Entry point used by internal indirection needs that don't correspond to any
3355 syntactic construct. */
3358 cp_build_fold_indirect_ref (tree pointer
)
3360 return cp_build_indirect_ref_1 (pointer
, RO_NULL
, tf_warning_or_error
, true);
3363 /* Entry point used by indirection needs that correspond to some syntactic
3367 cp_build_indirect_ref (tree ptr
, ref_operator errorstring
,
3368 tsubst_flags_t complain
)
3370 return cp_build_indirect_ref_1 (ptr
, errorstring
, complain
, false);
3373 /* This handles expressions of the form "a[i]", which denotes
3376 This is logically equivalent in C to *(a+i), but we may do it differently.
3377 If A is a variable or a member, we generate a primitive ARRAY_REF.
3378 This avoids forcing the array out of registers, and can work on
3379 arrays that are not lvalues (for example, members of structures returned
3382 If INDEX is of some user-defined type, it must be converted to
3383 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3384 will inherit the type of the array, which will be some pointer type.
3386 LOC is the location to use in building the array reference. */
3389 cp_build_array_ref (location_t loc
, tree array
, tree idx
,
3390 tsubst_flags_t complain
)
3396 if (complain
& tf_error
)
3397 error_at (loc
, "subscript missing in array reference");
3398 return error_mark_node
;
3401 if (TREE_TYPE (array
) == error_mark_node
3402 || TREE_TYPE (idx
) == error_mark_node
)
3403 return error_mark_node
;
3405 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3407 switch (TREE_CODE (array
))
3411 tree value
= cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3413 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3414 TREE_OPERAND (array
, 0), value
);
3415 SET_EXPR_LOCATION (ret
, loc
);
3420 ret
= build_conditional_expr
3421 (loc
, TREE_OPERAND (array
, 0),
3422 cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3424 cp_build_array_ref (loc
, TREE_OPERAND (array
, 2), idx
,
3427 protected_set_expr_location (ret
, loc
);
3434 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, idx
);
3436 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
3440 warn_array_subscript_with_type_char (loc
, idx
);
3442 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx
)))
3444 if (complain
& tf_error
)
3445 error_at (loc
, "array subscript is not an integer");
3446 return error_mark_node
;
3449 /* Apply integral promotions *after* noticing character types.
3450 (It is unclear why we do these promotions -- the standard
3451 does not say that we should. In fact, the natural thing would
3452 seem to be to convert IDX to ptrdiff_t; we're performing
3453 pointer arithmetic.) */
3454 idx
= cp_perform_integral_promotions (idx
, complain
);
3456 idx
= maybe_constant_value (idx
);
3458 /* An array that is indexed by a non-constant
3459 cannot be stored in a register; we must be able to do
3460 address arithmetic on its address.
3461 Likewise an array of elements of variable size. */
3462 if (TREE_CODE (idx
) != INTEGER_CST
3463 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
3464 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
3467 if (!cxx_mark_addressable (array
, true))
3468 return error_mark_node
;
3471 /* An array that is indexed by a constant value which is not within
3472 the array bounds cannot be stored in a register either; because we
3473 would get a crash in store_bit_field/extract_bit_field when trying
3474 to access a non-existent part of the register. */
3475 if (TREE_CODE (idx
) == INTEGER_CST
3476 && TYPE_DOMAIN (TREE_TYPE (array
))
3477 && ! int_fits_type_p (idx
, TYPE_DOMAIN (TREE_TYPE (array
))))
3479 if (!cxx_mark_addressable (array
))
3480 return error_mark_node
;
3483 /* Note in C++ it is valid to subscript a `register' array, since
3484 it is valid to take the address of something with that
3485 storage specification. */
3489 while (TREE_CODE (foo
) == COMPONENT_REF
)
3490 foo
= TREE_OPERAND (foo
, 0);
3491 if (VAR_P (foo
) && DECL_REGISTER (foo
)
3492 && (complain
& tf_warning
))
3493 warning_at (loc
, OPT_Wextra
,
3494 "subscripting array declared %<register%>");
3497 type
= TREE_TYPE (TREE_TYPE (array
));
3498 rval
= build4 (ARRAY_REF
, type
, array
, idx
, NULL_TREE
, NULL_TREE
);
3499 /* Array ref is const/volatile if the array elements are
3500 or if the array is.. */
3501 TREE_READONLY (rval
)
3502 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
3503 TREE_SIDE_EFFECTS (rval
)
3504 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
3505 TREE_THIS_VOLATILE (rval
)
3506 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
3507 ret
= require_complete_type_sfinae (rval
, complain
);
3508 protected_set_expr_location (ret
, loc
);
3510 ret
= non_lvalue_loc (loc
, ret
);
3515 tree ar
= cp_default_conversion (array
, complain
);
3516 tree ind
= cp_default_conversion (idx
, complain
);
3518 /* Put the integer in IND to simplify error checking. */
3519 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
3520 std::swap (ar
, ind
);
3522 if (ar
== error_mark_node
|| ind
== error_mark_node
)
3523 return error_mark_node
;
3525 if (!TYPE_PTR_P (TREE_TYPE (ar
)))
3527 if (complain
& tf_error
)
3528 error_at (loc
, "subscripted value is neither array nor pointer");
3529 return error_mark_node
;
3531 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
3533 if (complain
& tf_error
)
3534 error_at (loc
, "array subscript is not an integer");
3535 return error_mark_node
;
3538 warn_array_subscript_with_type_char (loc
, idx
);
3540 ret
= cp_build_indirect_ref (cp_build_binary_op (input_location
,
3545 protected_set_expr_location (ret
, loc
);
3547 ret
= non_lvalue_loc (loc
, ret
);
3552 /* Entry point for Obj-C++. */
3555 build_array_ref (location_t loc
, tree array
, tree idx
)
3557 return cp_build_array_ref (loc
, array
, idx
, tf_warning_or_error
);
3560 /* Resolve a pointer to member function. INSTANCE is the object
3561 instance to use, if the member points to a virtual member.
3563 This used to avoid checking for virtual functions if basetype
3564 has no virtual functions, according to an earlier ANSI draft.
3565 With the final ISO C++ rules, such an optimization is
3566 incorrect: A pointer to a derived member can be static_cast
3567 to pointer-to-base-member, as long as the dynamic object
3568 later has the right member. So now we only do this optimization
3569 when we know the dynamic type of the object. */
3572 get_member_function_from_ptrfunc (tree
*instance_ptrptr
, tree function
,
3573 tsubst_flags_t complain
)
3575 if (TREE_CODE (function
) == OFFSET_REF
)
3576 function
= TREE_OPERAND (function
, 1);
3578 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
3580 tree idx
, delta
, e1
, e2
, e3
, vtbl
;
3582 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
3583 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
3585 tree instance_ptr
= *instance_ptrptr
;
3586 tree instance_save_expr
= 0;
3587 if (instance_ptr
== error_mark_node
)
3589 if (TREE_CODE (function
) == PTRMEM_CST
)
3591 /* Extracting the function address from a pmf is only
3592 allowed with -Wno-pmf-conversions. It only works for
3594 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
), complain
);
3595 e1
= convert (fntype
, e1
);
3600 if (complain
& tf_error
)
3601 error ("object missing in use of %qE", function
);
3602 return error_mark_node
;
3606 /* True if we know that the dynamic type of the object doesn't have
3607 virtual functions, so we can assume the PFN field is a pointer. */
3608 nonvirtual
= (COMPLETE_TYPE_P (basetype
)
3609 && !TYPE_POLYMORPHIC_P (basetype
)
3610 && resolves_to_fixed_type_p (instance_ptr
, 0));
3612 /* If we don't really have an object (i.e. in an ill-formed
3613 conversion from PMF to pointer), we can't resolve virtual
3614 functions anyway. */
3615 if (!nonvirtual
&& is_dummy_object (instance_ptr
))
3618 if (TREE_SIDE_EFFECTS (instance_ptr
))
3619 instance_ptr
= instance_save_expr
= save_expr (instance_ptr
);
3621 if (TREE_SIDE_EFFECTS (function
))
3622 function
= save_expr (function
);
3624 /* Start by extracting all the information from the PMF itself. */
3625 e3
= pfn_from_ptrmemfunc (function
);
3626 delta
= delta_from_ptrmemfunc (function
);
3627 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
3628 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
3630 int flag_sanitize_save
;
3631 case ptrmemfunc_vbit_in_pfn
:
3632 e1
= cp_build_binary_op (input_location
,
3633 BIT_AND_EXPR
, idx
, integer_one_node
,
3635 idx
= cp_build_binary_op (input_location
,
3636 MINUS_EXPR
, idx
, integer_one_node
,
3638 if (idx
== error_mark_node
)
3639 return error_mark_node
;
3642 case ptrmemfunc_vbit_in_delta
:
3643 e1
= cp_build_binary_op (input_location
,
3644 BIT_AND_EXPR
, delta
, integer_one_node
,
3646 /* Don't instrument the RSHIFT_EXPR we're about to create because
3647 we're going to use DELTA number of times, and that wouldn't play
3648 well with SAVE_EXPRs therein. */
3649 flag_sanitize_save
= flag_sanitize
;
3651 delta
= cp_build_binary_op (input_location
,
3652 RSHIFT_EXPR
, delta
, integer_one_node
,
3654 flag_sanitize
= flag_sanitize_save
;
3655 if (delta
== error_mark_node
)
3656 return error_mark_node
;
3663 if (e1
== error_mark_node
)
3664 return error_mark_node
;
3666 /* Convert down to the right base before using the instance. A
3667 special case is that in a pointer to member of class C, C may
3668 be incomplete. In that case, the function will of course be
3669 a member of C, and no conversion is required. In fact,
3670 lookup_base will fail in that case, because incomplete
3671 classes do not have BINFOs. */
3672 if (!same_type_ignoring_top_level_qualifiers_p
3673 (basetype
, TREE_TYPE (TREE_TYPE (instance_ptr
))))
3675 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
3676 basetype
, ba_check
, NULL
, complain
);
3677 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
,
3679 if (instance_ptr
== error_mark_node
)
3680 return error_mark_node
;
3682 /* ...and then the delta in the PMF. */
3683 instance_ptr
= fold_build_pointer_plus (instance_ptr
, delta
);
3685 /* Hand back the adjusted 'this' argument to our caller. */
3686 *instance_ptrptr
= instance_ptr
;
3689 /* Now just return the pointer. */
3692 /* Next extract the vtable pointer from the object. */
3693 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
3695 vtbl
= cp_build_fold_indirect_ref (vtbl
);
3696 if (vtbl
== error_mark_node
)
3697 return error_mark_node
;
3699 /* Finally, extract the function pointer from the vtable. */
3700 e2
= fold_build_pointer_plus_loc (input_location
, vtbl
, idx
);
3701 e2
= cp_build_fold_indirect_ref (e2
);
3702 if (e2
== error_mark_node
)
3703 return error_mark_node
;
3704 TREE_CONSTANT (e2
) = 1;
3706 /* When using function descriptors, the address of the
3707 vtable entry is treated as a function pointer. */
3708 if (TARGET_VTABLE_USES_DESCRIPTORS
)
3709 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
3710 cp_build_addr_expr (e2
, complain
));
3712 e2
= fold_convert (TREE_TYPE (e3
), e2
);
3713 e1
= build_conditional_expr (input_location
, e1
, e2
, e3
, complain
);
3714 if (e1
== error_mark_node
)
3715 return error_mark_node
;
3717 /* Make sure this doesn't get evaluated first inside one of the
3718 branches of the COND_EXPR. */
3719 if (instance_save_expr
)
3720 e1
= build2 (COMPOUND_EXPR
, TREE_TYPE (e1
),
3721 instance_save_expr
, e1
);
3728 /* Used by the C-common bits. */
3730 build_function_call (location_t
/*loc*/,
3731 tree function
, tree params
)
3733 return cp_build_function_call (function
, params
, tf_warning_or_error
);
3736 /* Used by the C-common bits. */
3738 build_function_call_vec (location_t
/*loc*/, vec
<location_t
> /*arg_loc*/,
3739 tree function
, vec
<tree
, va_gc
> *params
,
3740 vec
<tree
, va_gc
> * /*origtypes*/)
3742 vec
<tree
, va_gc
> *orig_params
= params
;
3743 tree ret
= cp_build_function_call_vec (function
, ¶ms
,
3744 tf_warning_or_error
);
3746 /* cp_build_function_call_vec can reallocate PARAMS by adding
3747 default arguments. That should never happen here. Verify
3749 gcc_assert (params
== orig_params
);
3754 /* Build a function call using a tree list of arguments. */
3757 cp_build_function_call (tree function
, tree params
, tsubst_flags_t complain
)
3762 for (; params
!= NULL_TREE
; params
= TREE_CHAIN (params
))
3763 vec_safe_push (vec
, TREE_VALUE (params
));
3764 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3768 /* Build a function call using varargs. */
3771 cp_build_function_call_nary (tree function
, tsubst_flags_t complain
, ...)
3777 va_start (args
, complain
);
3778 for (t
= va_arg (args
, tree
); t
!= NULL_TREE
; t
= va_arg (args
, tree
))
3779 vec_safe_push (vec
, t
);
3781 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3785 /* Build a function call using a vector of arguments. PARAMS may be
3786 NULL if there are no parameters. This changes the contents of
3790 cp_build_function_call_vec (tree function
, vec
<tree
, va_gc
> **params
,
3791 tsubst_flags_t complain
)
3793 tree fntype
, fndecl
;
3795 tree original
= function
;
3799 vec
<tree
, va_gc
> *allocated
= NULL
;
3802 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3803 expressions, like those used for ObjC messenger dispatches. */
3804 if (params
!= NULL
&& !vec_safe_is_empty (*params
))
3805 function
= objc_rewrite_function_call (function
, (**params
)[0]);
3807 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3808 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3809 if (TREE_CODE (function
) == NOP_EXPR
3810 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
3811 function
= TREE_OPERAND (function
, 0);
3813 if (TREE_CODE (function
) == FUNCTION_DECL
)
3815 /* If the function is a non-template member function
3816 or a non-template friend, then we need to check the
3819 Note that if overload resolution failed with a single
3820 candidate this function will be used to explicitly diagnose
3821 the failure for the single call expression. The check is
3822 technically redundant since we also would have failed in
3823 add_function_candidate. */
3825 && (complain
& tf_error
)
3826 && !constraints_satisfied_p (function
))
3828 auto_diagnostic_group d
;
3829 error ("cannot call function %qD", function
);
3830 location_t loc
= DECL_SOURCE_LOCATION (function
);
3831 diagnose_constraints (loc
, function
, NULL_TREE
);
3832 return error_mark_node
;
3835 if (!mark_used (function
, complain
))
3836 return error_mark_node
;
3839 /* Convert anything with function type to a pointer-to-function. */
3840 if (DECL_MAIN_P (function
))
3842 if (complain
& tf_error
)
3843 pedwarn (input_location
, OPT_Wpedantic
,
3844 "ISO C++ forbids calling %<::main%> from within program");
3846 return error_mark_node
;
3848 function
= build_addr_func (function
, complain
);
3854 function
= build_addr_func (function
, complain
);
3857 if (function
== error_mark_node
)
3858 return error_mark_node
;
3860 fntype
= TREE_TYPE (function
);
3862 if (TYPE_PTRMEMFUNC_P (fntype
))
3864 if (complain
& tf_error
)
3865 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3866 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3867 original
, original
);
3868 return error_mark_node
;
3871 is_method
= (TYPE_PTR_P (fntype
)
3872 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
3874 if (!(TYPE_PTRFN_P (fntype
)
3876 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
3878 if (complain
& tf_error
)
3880 if (!flag_diagnostics_show_caret
)
3881 error_at (input_location
,
3882 "%qE cannot be used as a function", original
);
3883 else if (DECL_P (original
))
3884 error_at (input_location
,
3885 "%qD cannot be used as a function", original
);
3887 error_at (input_location
,
3888 "expression cannot be used as a function");
3891 return error_mark_node
;
3894 /* fntype now gets the type of function pointed to. */
3895 fntype
= TREE_TYPE (fntype
);
3896 parm_types
= TYPE_ARG_TYPES (fntype
);
3900 allocated
= make_tree_vector ();
3901 params
= &allocated
;
3904 nargs
= convert_arguments (parm_types
, params
, fndecl
, LOOKUP_NORMAL
,
3907 return error_mark_node
;
3909 argarray
= (*params
)->address ();
3911 /* Check for errors in format strings and inappropriately
3913 bool warned_p
= check_function_arguments (input_location
, fndecl
, fntype
,
3914 nargs
, argarray
, NULL
);
3916 ret
= build_cxx_call (function
, nargs
, argarray
, complain
);
3920 tree c
= extract_call_expr (ret
);
3921 if (TREE_CODE (c
) == CALL_EXPR
)
3922 TREE_NO_WARNING (c
) = 1;
3925 if (allocated
!= NULL
)
3926 release_tree_vector (allocated
);
3931 /* Subroutine of convert_arguments.
3932 Print an error message about a wrong number of arguments. */
3935 error_args_num (location_t loc
, tree fndecl
, bool too_many_p
)
3939 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
3941 if (DECL_NAME (fndecl
) == NULL_TREE
3942 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
3945 ? G_("too many arguments to constructor %q#D")
3946 : G_("too few arguments to constructor %q#D"),
3951 ? G_("too many arguments to member function %q#D")
3952 : G_("too few arguments to member function %q#D"),
3958 ? G_("too many arguments to function %q#D")
3959 : G_("too few arguments to function %q#D"),
3961 if (!DECL_IS_BUILTIN (fndecl
))
3962 inform (DECL_SOURCE_LOCATION (fndecl
), "declared here");
3966 if (c_dialect_objc () && objc_message_selector ())
3969 ? G_("too many arguments to method %q#D")
3970 : G_("too few arguments to method %q#D"),
3971 objc_message_selector ());
3973 error_at (loc
, too_many_p
? G_("too many arguments to function")
3974 : G_("too few arguments to function"));
3978 /* Convert the actual parameter expressions in the list VALUES to the
3979 types in the list TYPELIST. The converted expressions are stored
3980 back in the VALUES vector.
3981 If parmdecls is exhausted, or when an element has NULL as its type,
3982 perform the default conversions.
3984 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3986 This is also where warnings about wrong number of args are generated.
3988 Returns the actual number of arguments processed (which might be less
3989 than the length of the vector), or -1 on error.
3991 In C++, unspecified trailing parameters can be filled in with their
3992 default arguments, if such were specified. Do so here. */
3995 convert_arguments (tree typelist
, vec
<tree
, va_gc
> **values
, tree fndecl
,
3996 int flags
, tsubst_flags_t complain
)
4001 /* Argument passing is always copy-initialization. */
4002 flags
|= LOOKUP_ONLYCONVERTING
;
4004 for (i
= 0, typetail
= typelist
;
4005 i
< vec_safe_length (*values
);
4008 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
4009 tree val
= (**values
)[i
];
4011 if (val
== error_mark_node
|| type
== error_mark_node
)
4014 if (type
== void_type_node
)
4016 if (complain
& tf_error
)
4018 error_args_num (input_location
, fndecl
, /*too_many_p=*/true);
4025 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4026 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4027 if (TREE_CODE (val
) == NOP_EXPR
4028 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
4029 && (type
== 0 || !TYPE_REF_P (type
)))
4030 val
= TREE_OPERAND (val
, 0);
4032 if (type
== 0 || !TYPE_REF_P (type
))
4034 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
4035 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val
)))
4036 val
= decay_conversion (val
, complain
);
4039 if (val
== error_mark_node
)
4044 /* Formal parm type is specified by a function prototype. */
4047 if (!COMPLETE_TYPE_P (complete_type (type
)))
4049 if (complain
& tf_error
)
4051 location_t loc
= EXPR_LOC_OR_LOC (val
, input_location
);
4054 auto_diagnostic_group d
;
4056 "parameter %P of %qD has incomplete type %qT",
4058 inform (get_fndecl_argument_location (fndecl
, i
),
4062 error_at (loc
, "parameter %P has incomplete type %qT", i
,
4065 parmval
= error_mark_node
;
4069 parmval
= convert_for_initialization
4070 (NULL_TREE
, type
, val
, flags
,
4071 ICR_ARGPASS
, fndecl
, i
, complain
);
4072 parmval
= convert_for_arg_passing (type
, parmval
, complain
);
4075 if (parmval
== error_mark_node
)
4078 (**values
)[i
] = parmval
;
4082 if (fndecl
&& magic_varargs_p (fndecl
))
4083 /* Don't do ellipsis conversion for __built_in_constant_p
4084 as this will result in spurious errors for non-trivial
4086 val
= require_complete_type_sfinae (val
, complain
);
4088 val
= convert_arg_to_ellipsis (val
, complain
);
4090 (**values
)[i
] = val
;
4094 typetail
= TREE_CHAIN (typetail
);
4097 if (typetail
!= 0 && typetail
!= void_list_node
)
4099 /* See if there are default arguments that can be used. Because
4100 we hold default arguments in the FUNCTION_TYPE (which is so
4101 wrong), we can see default parameters here from deduced
4102 contexts (and via typeof) for indirect function calls.
4103 Fortunately we know whether we have a function decl to
4104 provide default arguments in a language conformant
4106 if (fndecl
&& TREE_PURPOSE (typetail
)
4107 && TREE_CODE (TREE_PURPOSE (typetail
)) != DEFERRED_PARSE
)
4109 for (; typetail
!= void_list_node
; ++i
)
4111 /* After DR777, with explicit template args we can end up with a
4112 default argument followed by no default argument. */
4113 if (!TREE_PURPOSE (typetail
))
4116 = convert_default_arg (TREE_VALUE (typetail
),
4117 TREE_PURPOSE (typetail
),
4118 fndecl
, i
, complain
);
4120 if (parmval
== error_mark_node
)
4123 vec_safe_push (*values
, parmval
);
4124 typetail
= TREE_CHAIN (typetail
);
4125 /* ends with `...'. */
4126 if (typetail
== NULL_TREE
)
4131 if (typetail
&& typetail
!= void_list_node
)
4133 if (complain
& tf_error
)
4134 error_args_num (input_location
, fndecl
, /*too_many_p=*/false);
4142 /* Build a binary-operation expression, after performing default
4143 conversions on the operands. CODE is the kind of expression to
4144 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4145 are the tree codes which correspond to ARG1 and ARG2 when issuing
4146 warnings about possibly misplaced parentheses. They may differ
4147 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4148 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4149 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4150 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4151 ARG2_CODE as ERROR_MARK. */
4154 build_x_binary_op (const op_location_t
&loc
, enum tree_code code
, tree arg1
,
4155 enum tree_code arg1_code
, tree arg2
,
4156 enum tree_code arg2_code
, tree
*overload_p
,
4157 tsubst_flags_t complain
)
4162 tree overload
= NULL_TREE
;
4167 if (processing_template_decl
)
4169 if (type_dependent_expression_p (arg1
)
4170 || type_dependent_expression_p (arg2
))
4172 expr
= build_min_nt_loc (loc
, code
, arg1
, arg2
);
4173 maybe_save_operator_binding (expr
);
4176 arg1
= build_non_dependent_expr (arg1
);
4177 arg2
= build_non_dependent_expr (arg2
);
4180 if (code
== DOTSTAR_EXPR
)
4181 expr
= build_m_component_ref (arg1
, arg2
, complain
);
4183 expr
= build_new_op (loc
, code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
,
4184 &overload
, complain
);
4186 if (overload_p
!= NULL
)
4187 *overload_p
= overload
;
4189 /* Check for cases such as x+y<<z which users are likely to
4190 misinterpret. But don't warn about obj << x + y, since that is a
4191 common idiom for I/O. */
4192 if (warn_parentheses
4193 && (complain
& tf_warning
)
4194 && !processing_template_decl
4195 && !error_operand_p (arg1
)
4196 && !error_operand_p (arg2
)
4197 && (code
!= LSHIFT_EXPR
4198 || !CLASS_TYPE_P (TREE_TYPE (arg1
))))
4199 warn_about_parentheses (loc
, code
, arg1_code
, orig_arg1
,
4200 arg2_code
, orig_arg2
);
4202 if (processing_template_decl
&& expr
!= error_mark_node
)
4204 if (overload
!= NULL_TREE
)
4205 return (build_min_non_dep_op_overload
4206 (code
, expr
, overload
, orig_arg1
, orig_arg2
));
4208 return build_min_non_dep (code
, expr
, orig_arg1
, orig_arg2
);
4214 /* Build and return an ARRAY_REF expression. */
4217 build_x_array_ref (location_t loc
, tree arg1
, tree arg2
,
4218 tsubst_flags_t complain
)
4220 tree orig_arg1
= arg1
;
4221 tree orig_arg2
= arg2
;
4223 tree overload
= NULL_TREE
;
4225 if (processing_template_decl
)
4227 if (type_dependent_expression_p (arg1
)
4228 || type_dependent_expression_p (arg2
))
4229 return build_min_nt_loc (loc
, ARRAY_REF
, arg1
, arg2
,
4230 NULL_TREE
, NULL_TREE
);
4231 arg1
= build_non_dependent_expr (arg1
);
4232 arg2
= build_non_dependent_expr (arg2
);
4235 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, arg1
, arg2
,
4236 NULL_TREE
, &overload
, complain
);
4238 if (processing_template_decl
&& expr
!= error_mark_node
)
4240 if (overload
!= NULL_TREE
)
4241 return (build_min_non_dep_op_overload
4242 (ARRAY_REF
, expr
, overload
, orig_arg1
, orig_arg2
));
4244 return build_min_non_dep (ARRAY_REF
, expr
, orig_arg1
, orig_arg2
,
4245 NULL_TREE
, NULL_TREE
);
4250 /* Return whether OP is an expression of enum type cast to integer
4251 type. In C++ even unsigned enum types are cast to signed integer
4252 types. We do not want to issue warnings about comparisons between
4253 signed and unsigned types when one of the types is an enum type.
4254 Those warnings are always false positives in practice. */
4257 enum_cast_to_int (tree op
)
4259 if (CONVERT_EXPR_P (op
)
4260 && TREE_TYPE (op
) == integer_type_node
4261 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == ENUMERAL_TYPE
4262 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op
, 0))))
4265 /* The cast may have been pushed into a COND_EXPR. */
4266 if (TREE_CODE (op
) == COND_EXPR
)
4267 return (enum_cast_to_int (TREE_OPERAND (op
, 1))
4268 || enum_cast_to_int (TREE_OPERAND (op
, 2)));
4273 /* For the c-common bits. */
4275 build_binary_op (location_t location
, enum tree_code code
, tree op0
, tree op1
,
4278 return cp_build_binary_op (location
, code
, op0
, op1
, tf_warning_or_error
);
4281 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4282 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4285 build_vec_cmp (tree_code code
, tree type
,
4286 tree arg0
, tree arg1
)
4288 tree zero_vec
= build_zero_cst (type
);
4289 tree minus_one_vec
= build_minus_one_cst (type
);
4290 tree cmp_type
= build_same_sized_truth_vector_type(type
);
4291 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
4292 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
4295 /* Possibly warn about an address never being NULL. */
4298 warn_for_null_address (location_t location
, tree op
, tsubst_flags_t complain
)
4301 || (complain
& tf_warning
) == 0
4302 || c_inhibit_evaluation_warnings
!= 0
4303 || TREE_NO_WARNING (op
))
4306 tree cop
= fold_non_dependent_expr (op
, complain
);
4308 if (TREE_CODE (cop
) == ADDR_EXPR
4309 && decl_with_nonnull_addr_p (TREE_OPERAND (cop
, 0))
4310 && !TREE_NO_WARNING (cop
))
4311 warning_at (location
, OPT_Waddress
, "the address of %qD will never "
4312 "be NULL", TREE_OPERAND (cop
, 0));
4314 if (CONVERT_EXPR_P (op
)
4315 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op
, 0))))
4318 STRIP_NOPS (inner_op
);
4320 if (DECL_P (inner_op
))
4321 warning_at (location
, OPT_Waddress
,
4322 "the compiler can assume that the address of "
4323 "%qD will never be NULL", inner_op
);
4327 /* Build a binary-operation expression without default conversions.
4328 CODE is the kind of expression to build.
4329 LOCATION is the location_t of the operator in the source code.
4330 This function differs from `build' in several ways:
4331 the data type of the result is computed and recorded in it,
4332 warnings are generated if arg data types are invalid,
4333 special handling for addition and subtraction of pointers is known,
4334 and some optimization is done (operations on narrow ints
4335 are done in the narrower type when that gives the same result).
4336 Constant folding is also done before the result is returned.
4338 Note that the operands will never have enumeral types
4339 because either they have just had the default conversions performed
4340 or they have both just been converted to some other type in which
4341 the arithmetic is to be done.
4343 C++: must do special pointer arithmetic when implementing
4344 multiple inheritance, and deal with pointer to member functions. */
4347 cp_build_binary_op (const op_location_t
&location
,
4348 enum tree_code code
, tree orig_op0
, tree orig_op1
,
4349 tsubst_flags_t complain
)
4352 enum tree_code code0
, code1
;
4354 const char *invalid_op_diag
;
4356 /* Expression code to give to the expression when it is built.
4357 Normally this is CODE, which is what the caller asked for,
4358 but in some special cases we change it. */
4359 enum tree_code resultcode
= code
;
4361 /* Data type in which the computation is to be performed.
4362 In the simplest cases this is the common type of the arguments. */
4363 tree result_type
= NULL_TREE
;
4365 /* Nonzero means operands have already been type-converted
4366 in whatever way is necessary.
4367 Zero means they need to be converted to RESULT_TYPE. */
4370 /* Nonzero means create the expression with this type, rather than
4372 tree build_type
= 0;
4374 /* Nonzero means after finally constructing the expression
4375 convert it to this type. */
4376 tree final_type
= 0;
4378 tree result
, result_ovl
;
4380 /* Nonzero if this is an operation like MIN or MAX which can
4381 safely be computed in short if both args are promoted shorts.
4382 Also implies COMMON.
4383 -1 indicates a bitwise operation; this makes a difference
4384 in the exact conditions for when it is safe to do the operation
4385 in a narrower mode. */
4388 /* Nonzero if this is a comparison operation;
4389 if both args are promoted shorts, compare the original shorts.
4390 Also implies COMMON. */
4391 int short_compare
= 0;
4393 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4396 /* True if both operands have arithmetic type. */
4397 bool arithmetic_types_p
;
4399 /* Remember whether we're doing / or %. */
4400 bool doing_div_or_mod
= false;
4402 /* Remember whether we're doing << or >>. */
4403 bool doing_shift
= false;
4405 /* Tree holding instrumentation expression. */
4406 tree instrument_expr
= NULL_TREE
;
4408 /* Apply default conversions. */
4409 op0
= resolve_nondeduced_context (orig_op0
, complain
);
4410 op1
= resolve_nondeduced_context (orig_op1
, complain
);
4412 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
4413 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
4414 || code
== TRUTH_XOR_EXPR
)
4416 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
4417 op0
= decay_conversion (op0
, complain
);
4418 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
4419 op1
= decay_conversion (op1
, complain
);
4423 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
4424 op0
= cp_default_conversion (op0
, complain
);
4425 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
4426 op1
= cp_default_conversion (op1
, complain
);
4429 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4430 STRIP_TYPE_NOPS (op0
);
4431 STRIP_TYPE_NOPS (op1
);
4433 /* DTRT if one side is an overloaded function, but complain about it. */
4434 if (type_unknown_p (op0
))
4436 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
4437 if (t
!= error_mark_node
)
4439 if (complain
& tf_error
)
4440 permerror (input_location
, "assuming cast to type %qT from overloaded function",
4445 if (type_unknown_p (op1
))
4447 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
4448 if (t
!= error_mark_node
)
4450 if (complain
& tf_error
)
4451 permerror (input_location
, "assuming cast to type %qT from overloaded function",
4457 type0
= TREE_TYPE (op0
);
4458 type1
= TREE_TYPE (op1
);
4460 /* The expression codes of the data types of the arguments tell us
4461 whether the arguments are integers, floating, pointers, etc. */
4462 code0
= TREE_CODE (type0
);
4463 code1
= TREE_CODE (type1
);
4465 /* If an error was already reported for one of the arguments,
4466 avoid reporting another error. */
4467 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
4468 return error_mark_node
;
4470 if ((invalid_op_diag
4471 = targetm
.invalid_binary_op (code
, type0
, type1
)))
4473 if (complain
& tf_error
)
4474 error (invalid_op_diag
);
4475 return error_mark_node
;
4478 /* Issue warnings about peculiar, but valid, uses of NULL. */
4479 if ((null_node_p (orig_op0
) || null_node_p (orig_op1
))
4480 /* It's reasonable to use pointer values as operands of &&
4481 and ||, so NULL is no exception. */
4482 && code
!= TRUTH_ANDIF_EXPR
&& code
!= TRUTH_ORIF_EXPR
4483 && ( /* Both are NULL (or 0) and the operation was not a
4484 comparison or a pointer subtraction. */
4485 (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)
4486 && code
!= EQ_EXPR
&& code
!= NE_EXPR
&& code
!= MINUS_EXPR
)
4487 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4488 || (!null_ptr_cst_p (orig_op0
)
4489 && !TYPE_PTR_OR_PTRMEM_P (type0
))
4490 || (!null_ptr_cst_p (orig_op1
)
4491 && !TYPE_PTR_OR_PTRMEM_P (type1
)))
4492 && (complain
& tf_warning
))
4495 expansion_point_location_if_in_system_header (input_location
);
4497 warning_at (loc
, OPT_Wpointer_arith
, "NULL used in arithmetic");
4500 /* In case when one of the operands of the binary operation is
4501 a vector and another is a scalar -- convert scalar to vector. */
4502 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
4504 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
4505 complain
& tf_error
);
4507 switch (convert_flag
)
4510 return error_mark_node
;
4513 op0
= convert (TREE_TYPE (type1
), op0
);
4514 op0
= save_expr (op0
);
4515 op0
= build_vector_from_val (type1
, op0
);
4516 type0
= TREE_TYPE (op0
);
4517 code0
= TREE_CODE (type0
);
4523 op1
= convert (TREE_TYPE (type0
), op1
);
4524 op1
= save_expr (op1
);
4525 op1
= build_vector_from_val (type0
, op1
);
4526 type1
= TREE_TYPE (op1
);
4527 code1
= TREE_CODE (type1
);
4539 /* Subtraction of two similar pointers.
4540 We must subtract them as integers, then divide by object size. */
4541 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
4542 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4545 result
= pointer_diff (location
, op0
, op1
,
4546 common_pointer_type (type0
, type1
), complain
,
4548 if (instrument_expr
!= NULL
)
4549 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
4550 instrument_expr
, result
);
4554 /* In all other cases except pointer - int, the usual arithmetic
4556 else if (!(code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
))
4561 /* The pointer - int case is just like pointer + int; fall
4565 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
4566 && (code0
== INTEGER_TYPE
|| code1
== INTEGER_TYPE
))
4570 ptr_operand
= ((code0
== POINTER_TYPE
) ? op0
: op1
);
4571 int_operand
= ((code0
== INTEGER_TYPE
) ? op0
: op1
);
4572 if (processing_template_decl
)
4574 result_type
= TREE_TYPE (ptr_operand
);
4577 return cp_pointer_int_sum (location
, code
,
4589 case TRUNC_DIV_EXPR
:
4591 case FLOOR_DIV_EXPR
:
4592 case ROUND_DIV_EXPR
:
4593 case EXACT_DIV_EXPR
:
4594 if (TREE_CODE (op0
) == SIZEOF_EXPR
&& TREE_CODE (op1
) == SIZEOF_EXPR
)
4596 tree type0
= TREE_OPERAND (op0
, 0);
4597 tree type1
= TREE_OPERAND (op1
, 0);
4598 tree first_arg
= type0
;
4599 if (!TYPE_P (type0
))
4600 type0
= TREE_TYPE (type0
);
4601 if (!TYPE_P (type1
))
4602 type1
= TREE_TYPE (type1
);
4603 if (INDIRECT_TYPE_P (type0
) && same_type_p (TREE_TYPE (type0
), type1
))
4605 STRIP_ANY_LOCATION_WRAPPER (first_arg
);
4606 if (!(TREE_CODE (first_arg
) == PARM_DECL
4607 && DECL_ARRAY_PARAMETER_P (first_arg
)
4608 && warn_sizeof_array_argument
)
4609 && (complain
& tf_warning
))
4611 auto_diagnostic_group d
;
4612 if (warning_at (location
, OPT_Wsizeof_pointer_div
,
4613 "division %<sizeof (%T) / sizeof (%T)%> does "
4614 "not compute the number of array elements",
4616 if (DECL_P (first_arg
))
4617 inform (DECL_SOURCE_LOCATION (first_arg
),
4618 "first %<sizeof%> operand was declared here");
4623 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4624 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
4625 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4626 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
4628 enum tree_code tcode0
= code0
, tcode1
= code1
;
4629 tree cop1
= fold_non_dependent_expr (op1
, complain
);
4630 doing_div_or_mod
= true;
4631 warn_for_div_by_zero (location
, cop1
);
4633 if (tcode0
== COMPLEX_TYPE
|| tcode0
== VECTOR_TYPE
)
4634 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
4635 if (tcode1
== COMPLEX_TYPE
|| tcode1
== VECTOR_TYPE
)
4636 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
4638 if (!(tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
))
4639 resultcode
= RDIV_EXPR
;
4642 /* When dividing two signed integers, we have to promote to int.
4643 unless we divide by a constant != -1. Note that default
4644 conversion will have been performed on the operands at this
4645 point, so we have to dig out the original type to find out if
4647 tree stripped_op1
= tree_strip_any_location_wrapper (op1
);
4648 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4649 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4650 || (TREE_CODE (stripped_op1
) == INTEGER_CST
4651 && ! integer_all_onesp (stripped_op1
)));
4661 if ((code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4662 || (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4663 && !VECTOR_FLOAT_TYPE_P (type0
)
4664 && !VECTOR_FLOAT_TYPE_P (type1
)))
4668 case TRUNC_MOD_EXPR
:
4669 case FLOOR_MOD_EXPR
:
4671 tree cop1
= fold_non_dependent_expr (op1
, complain
);
4672 doing_div_or_mod
= true;
4673 warn_for_div_by_zero (location
, cop1
);
4676 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4677 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4678 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
4680 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4682 /* Although it would be tempting to shorten always here, that loses
4683 on some targets, since the modulo instruction is undefined if the
4684 quotient can't be represented in the computation mode. We shorten
4685 only if unsigned or if dividing by something we know != -1. */
4686 tree stripped_op1
= tree_strip_any_location_wrapper (op1
);
4687 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4688 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4689 || (TREE_CODE (stripped_op1
) == INTEGER_CST
4690 && ! integer_all_onesp (stripped_op1
)));
4695 case TRUTH_ANDIF_EXPR
:
4696 case TRUTH_ORIF_EXPR
:
4697 case TRUTH_AND_EXPR
:
4699 if (!VECTOR_TYPE_P (type0
) && VECTOR_TYPE_P (type1
))
4701 if (!COMPARISON_CLASS_P (op1
))
4702 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4703 build_zero_cst (type1
), complain
);
4704 if (code
== TRUTH_ANDIF_EXPR
)
4706 tree z
= build_zero_cst (TREE_TYPE (op1
));
4707 return build_conditional_expr (location
, op0
, op1
, z
, complain
);
4709 else if (code
== TRUTH_ORIF_EXPR
)
4711 tree m1
= build_all_ones_cst (TREE_TYPE (op1
));
4712 return build_conditional_expr (location
, op0
, m1
, op1
, complain
);
4717 if (VECTOR_TYPE_P (type0
))
4719 if (!COMPARISON_CLASS_P (op0
))
4720 op0
= cp_build_binary_op (EXPR_LOCATION (op0
), NE_EXPR
, op0
,
4721 build_zero_cst (type0
), complain
);
4722 if (!VECTOR_TYPE_P (type1
))
4724 tree m1
= build_all_ones_cst (TREE_TYPE (op0
));
4725 tree z
= build_zero_cst (TREE_TYPE (op0
));
4726 op1
= build_conditional_expr (location
, op1
, m1
, z
, complain
);
4728 else if (!COMPARISON_CLASS_P (op1
))
4729 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4730 build_zero_cst (type1
), complain
);
4732 if (code
== TRUTH_ANDIF_EXPR
)
4733 code
= BIT_AND_EXPR
;
4734 else if (code
== TRUTH_ORIF_EXPR
)
4735 code
= BIT_IOR_EXPR
;
4739 return cp_build_binary_op (location
, code
, op0
, op1
, complain
);
4742 result_type
= boolean_type_node
;
4745 /* Shift operations: result has same type as first operand;
4746 always convert second operand to int.
4747 Also set SHORT_SHIFT if shifting rightward. */
4750 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4751 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4753 result_type
= type0
;
4756 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4757 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4758 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4759 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
4760 TYPE_VECTOR_SUBPARTS (type1
)))
4762 result_type
= type0
;
4765 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4767 tree const_op1
= fold_non_dependent_expr (op1
, complain
);
4768 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4770 result_type
= type0
;
4772 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4774 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4776 if ((complain
& tf_warning
)
4777 && c_inhibit_evaluation_warnings
== 0)
4778 warning (OPT_Wshift_count_negative
,
4779 "right shift count is negative");
4783 if (compare_tree_int (const_op1
, TYPE_PRECISION (type0
)) >= 0
4784 && (complain
& tf_warning
)
4785 && c_inhibit_evaluation_warnings
== 0)
4786 warning (OPT_Wshift_count_overflow
,
4787 "right shift count >= width of type");
4790 /* Avoid converting op1 to result_type later. */
4796 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4797 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4799 result_type
= type0
;
4802 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4803 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4804 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4805 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
4806 TYPE_VECTOR_SUBPARTS (type1
)))
4808 result_type
= type0
;
4811 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4813 tree const_op0
= fold_non_dependent_expr (op0
, complain
);
4814 if (TREE_CODE (const_op0
) != INTEGER_CST
)
4816 tree const_op1
= fold_non_dependent_expr (op1
, complain
);
4817 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4819 result_type
= type0
;
4821 if (TREE_CODE (const_op0
) == INTEGER_CST
4822 && tree_int_cst_sgn (const_op0
) < 0
4823 && (complain
& tf_warning
)
4824 && c_inhibit_evaluation_warnings
== 0)
4825 warning (OPT_Wshift_negative_value
,
4826 "left shift of negative value");
4827 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4829 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4831 if ((complain
& tf_warning
)
4832 && c_inhibit_evaluation_warnings
== 0)
4833 warning (OPT_Wshift_count_negative
,
4834 "left shift count is negative");
4836 else if (compare_tree_int (const_op1
,
4837 TYPE_PRECISION (type0
)) >= 0)
4839 if ((complain
& tf_warning
)
4840 && c_inhibit_evaluation_warnings
== 0)
4841 warning (OPT_Wshift_count_overflow
,
4842 "left shift count >= width of type");
4844 else if (TREE_CODE (const_op0
) == INTEGER_CST
4845 && (complain
& tf_warning
))
4846 maybe_warn_shift_overflow (location
, const_op0
, const_op1
);
4848 /* Avoid converting op1 to result_type later. */
4855 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4857 result_type
= type0
;
4858 if (TREE_CODE (op1
) == INTEGER_CST
)
4860 if (tree_int_cst_lt (op1
, integer_zero_node
))
4862 if (complain
& tf_warning
)
4863 warning (0, (code
== LROTATE_EXPR
)
4864 ? G_("left rotate count is negative")
4865 : G_("right rotate count is negative"));
4867 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
4869 if (complain
& tf_warning
)
4870 warning (0, (code
== LROTATE_EXPR
)
4871 ? G_("left rotate count >= width of type")
4872 : G_("right rotate count >= width of type"));
4875 /* Convert the shift-count to an integer, regardless of
4876 size of value being shifted. */
4877 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4878 op1
= cp_convert (integer_type_node
, op1
, complain
);
4884 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4885 goto vector_compare
;
4886 if ((complain
& tf_warning
)
4887 && c_inhibit_evaluation_warnings
== 0
4888 && (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
)))
4889 warning (OPT_Wfloat_equal
,
4890 "comparing floating-point with %<==%> or %<!=%> is unsafe");
4891 if (complain
& tf_warning
)
4893 tree stripped_orig_op0
= tree_strip_any_location_wrapper (orig_op0
);
4894 tree stripped_orig_op1
= tree_strip_any_location_wrapper (orig_op1
);
4895 if ((TREE_CODE (stripped_orig_op0
) == STRING_CST
4896 && !integer_zerop (cp_fully_fold (op1
)))
4897 || (TREE_CODE (stripped_orig_op1
) == STRING_CST
4898 && !integer_zerop (cp_fully_fold (op0
))))
4899 warning (OPT_Waddress
, "comparison with string literal results "
4900 "in unspecified behavior");
4903 build_type
= boolean_type_node
;
4904 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4905 || code0
== COMPLEX_TYPE
|| code0
== ENUMERAL_TYPE
)
4906 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4907 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
))
4909 else if (((code0
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type0
))
4910 && null_ptr_cst_p (orig_op1
))
4911 /* Handle, eg, (void*)0 (c++/43906), and more. */
4912 || (code0
== POINTER_TYPE
4913 && TYPE_PTR_P (type1
) && integer_zerop (op1
)))
4915 if (TYPE_PTR_P (type1
))
4916 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4917 CPO_COMPARISON
, complain
);
4919 result_type
= type0
;
4921 if (char_type_p (TREE_TYPE (orig_op1
)))
4923 auto_diagnostic_group d
;
4924 if (warning (OPT_Wpointer_compare
,
4925 "comparison between pointer and zero character "
4927 inform (input_location
,
4928 "did you mean to dereference the pointer?");
4930 warn_for_null_address (location
, op0
, complain
);
4932 else if (((code1
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type1
))
4933 && null_ptr_cst_p (orig_op0
))
4934 /* Handle, eg, (void*)0 (c++/43906), and more. */
4935 || (code1
== POINTER_TYPE
4936 && TYPE_PTR_P (type0
) && integer_zerop (op0
)))
4938 if (TYPE_PTR_P (type0
))
4939 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4940 CPO_COMPARISON
, complain
);
4942 result_type
= type1
;
4944 if (char_type_p (TREE_TYPE (orig_op0
)))
4946 auto_diagnostic_group d
;
4947 if (warning (OPT_Wpointer_compare
,
4948 "comparison between pointer and zero character "
4950 inform (input_location
,
4951 "did you mean to dereference the pointer?");
4953 warn_for_null_address (location
, op1
, complain
);
4955 else if ((code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4956 || (TYPE_PTRDATAMEM_P (type0
) && TYPE_PTRDATAMEM_P (type1
)))
4957 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4958 CPO_COMPARISON
, complain
);
4959 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
4960 /* One of the operands must be of nullptr_t type. */
4961 result_type
= TREE_TYPE (nullptr_node
);
4962 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4964 result_type
= type0
;
4965 if (complain
& tf_error
)
4966 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4968 return error_mark_node
;
4970 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4972 result_type
= type1
;
4973 if (complain
& tf_error
)
4974 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4976 return error_mark_node
;
4978 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (orig_op1
))
4980 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4981 == ptrmemfunc_vbit_in_delta
)
4983 tree pfn0
, delta0
, e1
, e2
;
4985 if (TREE_SIDE_EFFECTS (op0
))
4986 op0
= cp_save_expr (op0
);
4988 pfn0
= pfn_from_ptrmemfunc (op0
);
4989 delta0
= delta_from_ptrmemfunc (op0
);
4990 e1
= cp_build_binary_op (location
,
4993 build_zero_cst (TREE_TYPE (pfn0
)),
4995 e2
= cp_build_binary_op (location
,
5001 if (complain
& tf_warning
)
5002 maybe_warn_zero_as_null_pointer_constant (op1
, input_location
);
5004 e2
= cp_build_binary_op (location
,
5005 EQ_EXPR
, e2
, integer_zero_node
,
5007 op0
= cp_build_binary_op (location
,
5008 TRUTH_ANDIF_EXPR
, e1
, e2
,
5010 op1
= cp_convert (TREE_TYPE (op0
), integer_one_node
, complain
);
5014 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
5015 op1
= cp_convert (TREE_TYPE (op0
), op1
, complain
);
5017 result_type
= TREE_TYPE (op0
);
5019 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (orig_op0
))
5020 return cp_build_binary_op (location
, code
, op1
, op0
, complain
);
5021 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
))
5024 /* E will be the final comparison. */
5026 /* E1 and E2 are for scratch. */
5034 type
= composite_pointer_type (type0
, type1
, op0
, op1
,
5035 CPO_COMPARISON
, complain
);
5037 if (!same_type_p (TREE_TYPE (op0
), type
))
5038 op0
= cp_convert_and_check (type
, op0
, complain
);
5039 if (!same_type_p (TREE_TYPE (op1
), type
))
5040 op1
= cp_convert_and_check (type
, op1
, complain
);
5042 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5043 return error_mark_node
;
5045 if (TREE_SIDE_EFFECTS (op0
))
5046 op0
= save_expr (op0
);
5047 if (TREE_SIDE_EFFECTS (op1
))
5048 op1
= save_expr (op1
);
5050 pfn0
= pfn_from_ptrmemfunc (op0
);
5051 pfn0
= cp_fully_fold (pfn0
);
5052 /* Avoid -Waddress warnings (c++/64877). */
5053 if (TREE_CODE (pfn0
) == ADDR_EXPR
)
5054 TREE_NO_WARNING (pfn0
) = 1;
5055 pfn1
= pfn_from_ptrmemfunc (op1
);
5056 pfn1
= cp_fully_fold (pfn1
);
5057 delta0
= delta_from_ptrmemfunc (op0
);
5058 delta1
= delta_from_ptrmemfunc (op1
);
5059 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5060 == ptrmemfunc_vbit_in_delta
)
5065 && ((op0.delta == op1.delta)
5066 || (!op0.pfn && op0.delta & 1 == 0
5067 && op1.delta & 1 == 0))
5069 The reason for the `!op0.pfn' bit is that a NULL
5070 pointer-to-member is any member with a zero PFN and
5071 LSB of the DELTA field is 0. */
5073 e1
= cp_build_binary_op (location
, BIT_AND_EXPR
,
5077 e1
= cp_build_binary_op (location
,
5078 EQ_EXPR
, e1
, integer_zero_node
,
5080 e2
= cp_build_binary_op (location
, BIT_AND_EXPR
,
5084 e2
= cp_build_binary_op (location
,
5085 EQ_EXPR
, e2
, integer_zero_node
,
5087 e1
= cp_build_binary_op (location
,
5088 TRUTH_ANDIF_EXPR
, e2
, e1
,
5090 e2
= cp_build_binary_op (location
, EQ_EXPR
,
5092 build_zero_cst (TREE_TYPE (pfn0
)),
5094 e2
= cp_build_binary_op (location
,
5095 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
5096 e1
= cp_build_binary_op (location
,
5097 EQ_EXPR
, delta0
, delta1
, complain
);
5098 e1
= cp_build_binary_op (location
,
5099 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
5106 && (!op0.pfn || op0.delta == op1.delta))
5108 The reason for the `!op0.pfn' bit is that a NULL
5109 pointer-to-member is any member with a zero PFN; the
5110 DELTA field is unspecified. */
5112 e1
= cp_build_binary_op (location
,
5113 EQ_EXPR
, delta0
, delta1
, complain
);
5114 e2
= cp_build_binary_op (location
,
5117 build_zero_cst (TREE_TYPE (pfn0
)),
5119 e1
= cp_build_binary_op (location
,
5120 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
5122 e2
= build2 (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
5123 e
= cp_build_binary_op (location
,
5124 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
5125 if (code
== EQ_EXPR
)
5127 return cp_build_binary_op (location
,
5128 EQ_EXPR
, e
, integer_zero_node
, complain
);
5132 gcc_assert (!TYPE_PTRMEMFUNC_P (type0
)
5133 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
),
5135 gcc_assert (!TYPE_PTRMEMFUNC_P (type1
)
5136 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
),
5144 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
5145 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
5147 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5148 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
5149 CPO_COMPARISON
, complain
);
5156 if (TREE_CODE (orig_op0
) == STRING_CST
5157 || TREE_CODE (orig_op1
) == STRING_CST
)
5159 if (complain
& tf_warning
)
5160 warning (OPT_Waddress
, "comparison with string literal results "
5161 "in unspecified behavior");
5164 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
5168 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
5170 && !vector_types_compatible_elements_p (type0
, type1
))
5172 if (complain
& tf_error
)
5174 error_at (location
, "comparing vectors with different "
5176 inform (location
, "operand types are %qT and %qT",
5179 return error_mark_node
;
5182 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0
),
5183 TYPE_VECTOR_SUBPARTS (type1
)))
5185 if (complain
& tf_error
)
5187 error_at (location
, "comparing vectors with different "
5188 "number of elements");
5189 inform (location
, "operand types are %qT and %qT",
5192 return error_mark_node
;
5195 /* It's not precisely specified how the usual arithmetic
5196 conversions apply to the vector types. Here, we use
5197 the unsigned type if one of the operands is signed and
5198 the other one is unsigned. */
5199 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
5201 if (!TYPE_UNSIGNED (type0
))
5202 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
5204 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
5205 warning_at (location
, OPT_Wsign_compare
, "comparison between "
5206 "types %qT and %qT", type0
, type1
);
5209 /* Always construct signed integer vector type. */
5210 intt
= c_common_type_for_size
5211 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0
))), 0);
5214 if (complain
& tf_error
)
5215 error_at (location
, "could not find an integer type "
5216 "of the same size as %qT", TREE_TYPE (type0
));
5217 return error_mark_node
;
5219 result_type
= build_opaque_vector_type (intt
,
5220 TYPE_VECTOR_SUBPARTS (type0
));
5222 return build_vec_cmp (resultcode
, result_type
, op0
, op1
);
5224 build_type
= boolean_type_node
;
5225 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
5226 || code0
== ENUMERAL_TYPE
)
5227 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5228 || code1
== ENUMERAL_TYPE
))
5230 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5231 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
5232 CPO_COMPARISON
, complain
);
5233 else if (code0
== POINTER_TYPE
&& null_ptr_cst_p (orig_op1
))
5235 result_type
= type0
;
5236 if (extra_warnings
&& (complain
& tf_warning
))
5237 warning (OPT_Wextra
,
5238 "ordered comparison of pointer with integer zero");
5240 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (orig_op0
))
5242 result_type
= type1
;
5243 if (extra_warnings
&& (complain
& tf_warning
))
5244 warning (OPT_Wextra
,
5245 "ordered comparison of pointer with integer zero");
5247 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
5248 /* One of the operands must be of nullptr_t type. */
5249 result_type
= TREE_TYPE (nullptr_node
);
5250 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5252 result_type
= type0
;
5253 if (complain
& tf_error
)
5254 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
5256 return error_mark_node
;
5258 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
5260 result_type
= type1
;
5261 if (complain
& tf_error
)
5262 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
5264 return error_mark_node
;
5267 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
5268 && !processing_template_decl
5269 && sanitize_flags_p (SANITIZE_POINTER_COMPARE
))
5271 op0
= save_expr (op0
);
5272 op1
= save_expr (op1
);
5274 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE
);
5275 instrument_expr
= build_call_expr_loc (location
, tt
, 2, op0
, op1
);
5280 case UNORDERED_EXPR
:
5287 build_type
= integer_type_node
;
5288 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
5290 if (complain
& tf_error
)
5291 error ("unordered comparison on non-floating-point argument");
5292 return error_mark_node
;
5301 if (((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
5302 || code0
== ENUMERAL_TYPE
)
5303 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5304 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
)))
5305 arithmetic_types_p
= 1;
5308 arithmetic_types_p
= 0;
5309 /* Vector arithmetic is only allowed when both sides are vectors. */
5310 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
5312 if (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
5313 || !vector_types_compatible_elements_p (type0
, type1
))
5315 if (complain
& tf_error
)
5317 /* "location" already embeds the locations of the
5318 operands, so we don't need to add them separately
5320 rich_location
richloc (line_table
, location
);
5321 binary_op_error (&richloc
, code
, type0
, type1
);
5323 return error_mark_node
;
5325 arithmetic_types_p
= 1;
5328 /* Determine the RESULT_TYPE, if it is not already known. */
5330 && arithmetic_types_p
5331 && (shorten
|| common
|| short_compare
))
5333 result_type
= cp_common_type (type0
, type1
);
5334 if (complain
& tf_warning
)
5335 do_warn_double_promotion (result_type
, type0
, type1
,
5336 "implicit conversion from %qH to %qI "
5337 "to match other operand of binary "
5344 if (complain
& tf_error
)
5346 binary_op_rich_location
richloc (location
,
5347 orig_op0
, orig_op1
, true);
5349 "invalid operands of types %qT and %qT to binary %qO",
5350 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
5352 return error_mark_node
;
5355 /* If we're in a template, the only thing we need to know is the
5357 if (processing_template_decl
)
5359 /* Since the middle-end checks the type when doing a build2, we
5360 need to build the tree in pieces. This built tree will never
5361 get out of the front-end as we replace it when instantiating
5363 tree tmp
= build2 (resultcode
,
5364 build_type
? build_type
: result_type
,
5366 TREE_OPERAND (tmp
, 0) = op0
;
5370 /* Remember the original type; RESULT_TYPE might be changed later on
5371 by shorten_binary_op. */
5372 tree orig_type
= result_type
;
5374 if (arithmetic_types_p
)
5376 bool first_complex
= (code0
== COMPLEX_TYPE
);
5377 bool second_complex
= (code1
== COMPLEX_TYPE
);
5378 int none_complex
= (!first_complex
&& !second_complex
);
5380 /* Adapted from patch for c/24581. */
5381 if (first_complex
!= second_complex
5382 && (code
== PLUS_EXPR
5383 || code
== MINUS_EXPR
5384 || code
== MULT_EXPR
5385 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
5386 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
5387 && flag_signed_zeros
)
5389 /* An operation on mixed real/complex operands must be
5390 handled specially, but the language-independent code can
5391 more easily optimize the plain complex arithmetic if
5392 -fno-signed-zeros. */
5393 tree real_type
= TREE_TYPE (result_type
);
5397 if (TREE_TYPE (op0
) != result_type
)
5398 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5399 if (TREE_TYPE (op1
) != real_type
)
5400 op1
= cp_convert_and_check (real_type
, op1
, complain
);
5404 if (TREE_TYPE (op0
) != real_type
)
5405 op0
= cp_convert_and_check (real_type
, op0
, complain
);
5406 if (TREE_TYPE (op1
) != result_type
)
5407 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5409 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
5410 return error_mark_node
;
5413 op0
= save_expr (op0
);
5414 real
= cp_build_unary_op (REALPART_EXPR
, op0
, true, complain
);
5415 imag
= cp_build_unary_op (IMAGPART_EXPR
, op0
, true, complain
);
5419 case TRUNC_DIV_EXPR
:
5420 op1
= save_expr (op1
);
5421 imag
= build2 (resultcode
, real_type
, imag
, op1
);
5425 real
= build2 (resultcode
, real_type
, real
, op1
);
5433 op1
= save_expr (op1
);
5434 real
= cp_build_unary_op (REALPART_EXPR
, op1
, true, complain
);
5435 imag
= cp_build_unary_op (IMAGPART_EXPR
, op1
, true, complain
);
5439 op0
= save_expr (op0
);
5440 imag
= build2 (resultcode
, real_type
, op0
, imag
);
5443 real
= build2 (resultcode
, real_type
, op0
, real
);
5446 real
= build2 (resultcode
, real_type
, op0
, real
);
5447 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
5453 result
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
5457 /* For certain operations (which identify themselves by shorten != 0)
5458 if both args were extended from the same smaller type,
5459 do the arithmetic in that type and then extend.
5461 shorten !=0 and !=1 indicates a bitwise operation.
5462 For them, this optimization is safe only if
5463 both args are zero-extended or both are sign-extended.
5464 Otherwise, we might change the result.
5465 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5466 but calculated in (unsigned short) it would be (unsigned short)-1. */
5468 if (shorten
&& none_complex
)
5470 final_type
= result_type
;
5471 result_type
= shorten_binary_op (result_type
, op0
, op1
,
5475 /* Comparison operations are shortened too but differently.
5476 They identify themselves by setting short_compare = 1. */
5480 /* We call shorten_compare only for diagnostics. */
5481 tree xop0
= fold_simple (op0
);
5482 tree xop1
= fold_simple (op1
);
5483 tree xresult_type
= result_type
;
5484 enum tree_code xresultcode
= resultcode
;
5485 shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
5489 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
5490 && warn_sign_compare
5491 /* Do not warn until the template is instantiated; we cannot
5492 bound the ranges of the arguments until that point. */
5493 && !processing_template_decl
5494 && (complain
& tf_warning
)
5495 && c_inhibit_evaluation_warnings
== 0
5496 /* Even unsigned enum types promote to signed int. We don't
5497 want to issue -Wsign-compare warnings for this case. */
5498 && !enum_cast_to_int (orig_op0
)
5499 && !enum_cast_to_int (orig_op1
))
5501 warn_for_sign_compare (location
, orig_op0
, orig_op1
, op0
, op1
,
5502 result_type
, resultcode
);
5506 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5507 Then the expression will be built.
5508 It will be given type FINAL_TYPE if that is nonzero;
5509 otherwise, it will be given type RESULT_TYPE. */
5512 warning_sentinel
w (warn_sign_conversion
, short_compare
);
5513 if (TREE_TYPE (op0
) != result_type
)
5514 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5515 if (TREE_TYPE (op1
) != result_type
)
5516 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5518 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5519 return error_mark_node
;
5522 if (build_type
== NULL_TREE
)
5523 build_type
= result_type
;
5525 if (sanitize_flags_p ((SANITIZE_SHIFT
5526 | SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
))
5527 && current_function_decl
!= NULL_TREE
5528 && !processing_template_decl
5529 && (doing_div_or_mod
|| doing_shift
))
5531 /* OP0 and/or OP1 might have side-effects. */
5532 op0
= cp_save_expr (op0
);
5533 op1
= cp_save_expr (op1
);
5534 op0
= fold_non_dependent_expr (op0
, complain
);
5535 op1
= fold_non_dependent_expr (op1
, complain
);
5536 if (doing_div_or_mod
5537 && sanitize_flags_p (SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
))
5539 /* For diagnostics we want to use the promoted types without
5540 shorten_binary_op. So convert the arguments to the
5541 original result_type. */
5544 if (TREE_TYPE (cop0
) != orig_type
)
5545 cop0
= cp_convert (orig_type
, op0
, complain
);
5546 if (TREE_TYPE (cop1
) != orig_type
)
5547 cop1
= cp_convert (orig_type
, op1
, complain
);
5548 instrument_expr
= ubsan_instrument_division (location
, cop0
, cop1
);
5550 else if (doing_shift
&& sanitize_flags_p (SANITIZE_SHIFT
))
5551 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
5554 result
= build2_loc (location
, resultcode
, build_type
, op0
, op1
);
5555 if (final_type
!= 0)
5556 result
= cp_convert (final_type
, result
, complain
);
5558 if (instrument_expr
!= NULL
)
5559 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
5560 instrument_expr
, result
);
5562 if (!processing_template_decl
)
5564 op0
= cp_fully_fold (op0
);
5565 /* Only consider the second argument if the first isn't overflowed. */
5566 if (!CONSTANT_CLASS_P (op0
) || TREE_OVERFLOW_P (op0
))
5568 op1
= cp_fully_fold (op1
);
5569 if (!CONSTANT_CLASS_P (op1
) || TREE_OVERFLOW_P (op1
))
5572 else if (!CONSTANT_CLASS_P (op0
) || !CONSTANT_CLASS_P (op1
)
5573 || TREE_OVERFLOW_P (op0
) || TREE_OVERFLOW_P (op1
))
5576 result_ovl
= fold_build2 (resultcode
, build_type
, op0
, op1
);
5577 if (TREE_OVERFLOW_P (result_ovl
))
5578 overflow_warning (location
, result_ovl
);
5583 /* Build a VEC_PERM_EXPR.
5584 This is a simple wrapper for c_build_vec_perm_expr. */
5586 build_x_vec_perm_expr (location_t loc
,
5587 tree arg0
, tree arg1
, tree arg2
,
5588 tsubst_flags_t complain
)
5590 tree orig_arg0
= arg0
;
5591 tree orig_arg1
= arg1
;
5592 tree orig_arg2
= arg2
;
5593 if (processing_template_decl
)
5595 if (type_dependent_expression_p (arg0
)
5596 || type_dependent_expression_p (arg1
)
5597 || type_dependent_expression_p (arg2
))
5598 return build_min_nt_loc (loc
, VEC_PERM_EXPR
, arg0
, arg1
, arg2
);
5599 arg0
= build_non_dependent_expr (arg0
);
5601 arg1
= build_non_dependent_expr (arg1
);
5602 arg2
= build_non_dependent_expr (arg2
);
5604 tree exp
= c_build_vec_perm_expr (loc
, arg0
, arg1
, arg2
, complain
& tf_error
);
5605 if (processing_template_decl
&& exp
!= error_mark_node
)
5606 return build_min_non_dep (VEC_PERM_EXPR
, exp
, orig_arg0
,
5607 orig_arg1
, orig_arg2
);
5611 /* Return a tree for the sum or difference (RESULTCODE says which)
5612 of pointer PTROP and integer INTOP. */
5615 cp_pointer_int_sum (location_t loc
, enum tree_code resultcode
, tree ptrop
,
5616 tree intop
, tsubst_flags_t complain
)
5618 tree res_type
= TREE_TYPE (ptrop
);
5620 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5621 in certain circumstance (when it's valid to do so). So we need
5622 to make sure it's complete. We don't need to check here, if we
5623 can actually complete it at all, as those checks will be done in
5624 pointer_int_sum() anyway. */
5625 complete_type (TREE_TYPE (res_type
));
5627 return pointer_int_sum (loc
, resultcode
, ptrop
,
5628 intop
, complain
& tf_warning_or_error
);
5631 /* Return a tree for the difference of pointers OP0 and OP1.
5632 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
5633 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5636 pointer_diff (location_t loc
, tree op0
, tree op1
, tree ptrtype
,
5637 tsubst_flags_t complain
, tree
*instrument_expr
)
5639 tree result
, inttype
;
5640 tree restype
= ptrdiff_type_node
;
5641 tree target_type
= TREE_TYPE (ptrtype
);
5643 if (!complete_type_or_else (target_type
, NULL_TREE
))
5644 return error_mark_node
;
5646 if (VOID_TYPE_P (target_type
))
5648 if (complain
& tf_error
)
5649 permerror (loc
, "ISO C++ forbids using pointer of "
5650 "type %<void *%> in subtraction");
5652 return error_mark_node
;
5654 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
5656 if (complain
& tf_error
)
5657 permerror (loc
, "ISO C++ forbids using pointer to "
5658 "a function in subtraction");
5660 return error_mark_node
;
5662 if (TREE_CODE (target_type
) == METHOD_TYPE
)
5664 if (complain
& tf_error
)
5665 permerror (loc
, "ISO C++ forbids using pointer to "
5666 "a method in subtraction");
5668 return error_mark_node
;
5671 /* Determine integer type result of the subtraction. This will usually
5672 be the same as the result type (ptrdiff_t), but may need to be a wider
5673 type if pointers for the address space are wider than ptrdiff_t. */
5674 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
5675 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
5679 if (!processing_template_decl
5680 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT
))
5682 op0
= save_expr (op0
);
5683 op1
= save_expr (op1
);
5685 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT
);
5686 *instrument_expr
= build_call_expr_loc (loc
, tt
, 2, op0
, op1
);
5689 /* First do the subtraction, then build the divide operator
5690 and only convert at the very end.
5691 Do not do default conversions in case restype is a short type. */
5693 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5694 pointers. If some platform cannot provide that, or has a larger
5695 ptrdiff_type to support differences larger than half the address
5696 space, cast the pointers to some larger integer type and do the
5697 computations in that type. */
5698 if (TYPE_PRECISION (inttype
) > TYPE_PRECISION (TREE_TYPE (op0
)))
5699 op0
= cp_build_binary_op (loc
,
5701 cp_convert (inttype
, op0
, complain
),
5702 cp_convert (inttype
, op1
, complain
),
5705 op0
= build2_loc (loc
, POINTER_DIFF_EXPR
, inttype
, op0
, op1
);
5707 /* This generates an error if op1 is a pointer to an incomplete type. */
5708 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
5710 if (complain
& tf_error
)
5711 error_at (loc
, "invalid use of a pointer to an incomplete type in "
5712 "pointer arithmetic");
5714 return error_mark_node
;
5717 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1
)))
5719 if (complain
& tf_error
)
5720 error_at (loc
, "arithmetic on pointer to an empty aggregate");
5722 return error_mark_node
;
5725 op1
= (TYPE_PTROB_P (ptrtype
)
5726 ? size_in_bytes_loc (loc
, target_type
)
5727 : integer_one_node
);
5729 /* Do the division. */
5731 result
= build2_loc (loc
, EXACT_DIV_EXPR
, inttype
, op0
,
5732 cp_convert (inttype
, op1
, complain
));
5733 return cp_convert (restype
, result
, complain
);
5736 /* Construct and perhaps optimize a tree representation
5737 for a unary operation. CODE, a tree_code, specifies the operation
5738 and XARG is the operand. */
5741 build_x_unary_op (location_t loc
, enum tree_code code
, cp_expr xarg
,
5742 tsubst_flags_t complain
)
5744 tree orig_expr
= xarg
;
5747 tree overload
= NULL_TREE
;
5749 if (processing_template_decl
)
5751 if (type_dependent_expression_p (xarg
))
5753 tree e
= build_min_nt_loc (loc
, code
, xarg
.get_value (), NULL_TREE
);
5754 maybe_save_operator_binding (e
);
5758 xarg
= build_non_dependent_expr (xarg
);
5763 /* [expr.unary.op] says:
5765 The address of an object of incomplete type can be taken.
5767 (And is just the ordinary address operator, not an overloaded
5768 "operator &".) However, if the type is a template
5769 specialization, we must complete the type at this point so that
5770 an overloaded "operator &" will be available if required. */
5771 if (code
== ADDR_EXPR
5772 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
5773 && ((CLASS_TYPE_P (TREE_TYPE (xarg
))
5774 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg
))))
5775 || (TREE_CODE (xarg
) == OFFSET_REF
)))
5776 /* Don't look for a function. */;
5778 exp
= build_new_op (loc
, code
, LOOKUP_NORMAL
, xarg
, NULL_TREE
,
5779 NULL_TREE
, &overload
, complain
);
5781 if (!exp
&& code
== ADDR_EXPR
)
5783 if (is_overloaded_fn (xarg
))
5785 tree fn
= get_first_fn (xarg
);
5786 if (DECL_CONSTRUCTOR_P (fn
) || DECL_DESTRUCTOR_P (fn
))
5788 if (complain
& tf_error
)
5789 error (DECL_CONSTRUCTOR_P (fn
)
5790 ? G_("taking address of constructor %qD")
5791 : G_("taking address of destructor %qD"),
5793 return error_mark_node
;
5797 /* A pointer to member-function can be formed only by saying
5799 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
5800 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
5802 if (TREE_CODE (xarg
) != OFFSET_REF
5803 || !TYPE_P (TREE_OPERAND (xarg
, 0)))
5805 if (complain
& tf_error
)
5807 error ("invalid use of %qE to form a "
5808 "pointer-to-member-function", xarg
.get_value ());
5809 if (TREE_CODE (xarg
) != OFFSET_REF
)
5810 inform (input_location
, " a qualified-id is required");
5812 return error_mark_node
;
5816 if (complain
& tf_error
)
5817 error ("parentheses around %qE cannot be used to form a"
5818 " pointer-to-member-function",
5821 return error_mark_node
;
5822 PTRMEM_OK_P (xarg
) = 1;
5826 if (TREE_CODE (xarg
) == OFFSET_REF
)
5828 ptrmem
= PTRMEM_OK_P (xarg
);
5830 if (!ptrmem
&& !flag_ms_extensions
5831 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
5833 /* A single non-static member, make sure we don't allow a
5834 pointer-to-member. */
5835 xarg
= build2 (OFFSET_REF
, TREE_TYPE (xarg
),
5836 TREE_OPERAND (xarg
, 0),
5837 ovl_make (TREE_OPERAND (xarg
, 1)));
5838 PTRMEM_OK_P (xarg
) = ptrmem
;
5842 exp
= cp_build_addr_expr_strict (xarg
, complain
);
5845 if (processing_template_decl
&& exp
!= error_mark_node
)
5847 if (overload
!= NULL_TREE
)
5848 return (build_min_non_dep_op_overload
5849 (code
, exp
, overload
, orig_expr
, integer_zero_node
));
5851 exp
= build_min_non_dep (code
, exp
, orig_expr
,
5852 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE
);
5854 if (TREE_CODE (exp
) == ADDR_EXPR
)
5855 PTRMEM_OK_P (exp
) = ptrmem
;
5859 /* Construct and perhaps optimize a tree representation
5860 for __builtin_addressof operation. ARG specifies the operand. */
5863 cp_build_addressof (location_t loc
, tree arg
, tsubst_flags_t complain
)
5865 tree orig_expr
= arg
;
5867 if (processing_template_decl
)
5869 if (type_dependent_expression_p (arg
))
5870 return build_min_nt_loc (loc
, ADDRESSOF_EXPR
, arg
, NULL_TREE
);
5872 arg
= build_non_dependent_expr (arg
);
5875 tree exp
= cp_build_addr_expr_strict (arg
, complain
);
5877 if (processing_template_decl
&& exp
!= error_mark_node
)
5878 exp
= build_min_non_dep (ADDRESSOF_EXPR
, exp
, orig_expr
, NULL_TREE
);
5882 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5883 constants, where a null value is represented by an INTEGER_CST of
5887 cp_truthvalue_conversion (tree expr
)
5889 tree type
= TREE_TYPE (expr
);
5890 if (TYPE_PTR_OR_PTRMEM_P (type
)
5891 /* Avoid ICE on invalid use of non-static member function. */
5892 || TREE_CODE (expr
) == FUNCTION_DECL
)
5893 return build_binary_op (input_location
, NE_EXPR
, expr
, nullptr_node
, true);
5895 return c_common_truthvalue_conversion (input_location
, expr
);
5898 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
5899 is a low-level function; most callers should use maybe_convert_cond. */
5902 condition_conversion (tree expr
)
5905 t
= perform_implicit_conversion_flags (boolean_type_node
, expr
,
5906 tf_warning_or_error
, LOOKUP_NORMAL
);
5907 if (!processing_template_decl
)
5908 t
= fold_build_cleanup_point_expr (boolean_type_node
, t
);
5912 /* Returns the address of T. This function will fold away
5913 ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
5914 most places should use cp_build_addr_expr instead. */
5917 build_address (tree t
)
5919 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
5920 return error_mark_node
;
5921 gcc_checking_assert (TREE_CODE (t
) != CONSTRUCTOR
5922 || processing_template_decl
);
5923 t
= build_fold_addr_expr_loc (EXPR_LOCATION (t
), t
);
5924 if (TREE_CODE (t
) != ADDR_EXPR
)
5929 /* Return a NOP_EXPR converting EXPR to TYPE. */
5932 build_nop (tree type
, tree expr
)
5934 if (type
== error_mark_node
|| error_operand_p (expr
))
5936 return build1_loc (EXPR_LOCATION (expr
), NOP_EXPR
, type
, expr
);
5939 /* Take the address of ARG, whatever that means under C++ semantics.
5940 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5941 and class rvalues as well.
5943 Nothing should call this function directly; instead, callers should use
5944 cp_build_addr_expr or cp_build_addr_expr_strict. */
5947 cp_build_addr_expr_1 (tree arg
, bool strict_lvalue
, tsubst_flags_t complain
)
5952 if (!arg
|| error_operand_p (arg
))
5953 return error_mark_node
;
5955 arg
= mark_lvalue_use (arg
);
5956 if (error_operand_p (arg
))
5957 return error_mark_node
;
5959 argtype
= lvalue_type (arg
);
5961 gcc_assert (!(identifier_p (arg
) && IDENTIFIER_ANY_OP_P (arg
)));
5963 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
5964 && !really_overloaded_fn (arg
))
5966 /* They're trying to take the address of a unique non-static
5967 member function. This is ill-formed (except in MS-land),
5968 but let's try to DTRT.
5969 Note: We only handle unique functions here because we don't
5970 want to complain if there's a static overload; non-unique
5971 cases will be handled by instantiate_type. But we need to
5972 handle this case here to allow casts on the resulting PMF.
5973 We could defer this in non-MS mode, but it's easier to give
5974 a useful error here. */
5976 /* Inside constant member functions, the `this' pointer
5977 contains an extra const qualifier. TYPE_MAIN_VARIANT
5978 is used here to remove this const from the diagnostics
5979 and the created OFFSET_REF. */
5980 tree base
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg
, 0)));
5981 tree fn
= get_first_fn (TREE_OPERAND (arg
, 1));
5982 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
5983 return error_mark_node
;
5985 if (! flag_ms_extensions
)
5987 tree name
= DECL_NAME (fn
);
5988 if (!(complain
& tf_error
))
5989 return error_mark_node
;
5990 else if (current_class_type
5991 && TREE_OPERAND (arg
, 0) == current_class_ref
)
5992 /* An expression like &memfn. */
5993 permerror (input_location
, "ISO C++ forbids taking the address of an unqualified"
5994 " or parenthesized non-static member function to form"
5995 " a pointer to member function. Say %<&%T::%D%>",
5998 permerror (input_location
, "ISO C++ forbids taking the address of a bound member"
5999 " function to form a pointer to member function."
6003 arg
= build_offset_ref (base
, fn
, /*address_p=*/true, complain
);
6006 /* Uninstantiated types are all functions. Taking the
6007 address of a function is a no-op, so just return the
6009 if (type_unknown_p (arg
))
6010 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
6012 if (TREE_CODE (arg
) == OFFSET_REF
)
6013 /* We want a pointer to member; bypass all the code for actually taking
6014 the address of something. */
6017 /* Anything not already handled and not a true memory reference
6019 if (!FUNC_OR_METHOD_TYPE_P (argtype
))
6021 cp_lvalue_kind kind
= lvalue_kind (arg
);
6022 if (kind
== clk_none
)
6024 if (complain
& tf_error
)
6025 lvalue_error (input_location
, lv_addressof
);
6026 return error_mark_node
;
6028 if (strict_lvalue
&& (kind
& (clk_rvalueref
|clk_class
)))
6030 if (!(complain
& tf_error
))
6031 return error_mark_node
;
6032 /* Make this a permerror because we used to accept it. */
6033 permerror (input_location
, "taking address of rvalue");
6037 if (TYPE_REF_P (argtype
))
6039 tree type
= build_pointer_type (TREE_TYPE (argtype
));
6040 arg
= build1 (CONVERT_EXPR
, type
, arg
);
6043 else if (pedantic
&& DECL_MAIN_P (arg
))
6046 /* Apparently a lot of autoconf scripts for C++ packages do this,
6047 so only complain if -Wpedantic. */
6048 if (complain
& (flag_pedantic_errors
? tf_error
: tf_warning
))
6049 pedwarn (input_location
, OPT_Wpedantic
,
6050 "ISO C++ forbids taking address of function %<::main%>");
6051 else if (flag_pedantic_errors
)
6052 return error_mark_node
;
6055 /* Let &* cancel out to simplify resulting code. */
6056 if (INDIRECT_REF_P (arg
))
6058 arg
= TREE_OPERAND (arg
, 0);
6059 if (TYPE_REF_P (TREE_TYPE (arg
)))
6061 tree type
= build_pointer_type (TREE_TYPE (TREE_TYPE (arg
)));
6062 arg
= build1 (CONVERT_EXPR
, type
, arg
);
6065 /* Don't let this be an lvalue. */
6070 /* Handle complex lvalues (when permitted)
6071 by reduction to simpler cases. */
6072 val
= unary_complex_lvalue (ADDR_EXPR
, arg
);
6076 switch (TREE_CODE (arg
))
6080 case FIX_TRUNC_EXPR
:
6081 /* We should have handled this above in the lvalue_kind check. */
6086 arg
= BASELINK_FUNCTIONS (arg
);
6090 arg
= OVL_FIRST (arg
);
6095 /* Turn a reference to a non-static data member into a
6096 pointer-to-member. */
6101 gcc_assert (PTRMEM_OK_P (arg
));
6103 t
= TREE_OPERAND (arg
, 1);
6104 if (TYPE_REF_P (TREE_TYPE (t
)))
6106 if (complain
& tf_error
)
6107 error ("cannot create pointer to reference member %qD", t
);
6108 return error_mark_node
;
6111 type
= build_ptrmem_type (context_for_name_lookup (t
),
6113 t
= make_ptrmem_cst (type
, TREE_OPERAND (arg
, 1));
6121 if (argtype
!= error_mark_node
)
6122 argtype
= build_pointer_type (argtype
);
6124 if (bitfield_p (arg
))
6126 if (complain
& tf_error
)
6127 error ("attempt to take address of bit-field");
6128 return error_mark_node
;
6131 /* In a template, we are processing a non-dependent expression
6132 so we can just form an ADDR_EXPR with the correct type. */
6133 if (processing_template_decl
|| TREE_CODE (arg
) != COMPONENT_REF
)
6135 tree stripped_arg
= tree_strip_any_location_wrapper (arg
);
6136 if (TREE_CODE (stripped_arg
) == FUNCTION_DECL
6137 && !mark_used (stripped_arg
, complain
) && !(complain
& tf_error
))
6138 return error_mark_node
;
6139 val
= build_address (arg
);
6140 if (TREE_CODE (arg
) == OFFSET_REF
)
6141 PTRMEM_OK_P (val
) = PTRMEM_OK_P (arg
);
6143 else if (BASELINK_P (TREE_OPERAND (arg
, 1)))
6145 tree fn
= BASELINK_FUNCTIONS (TREE_OPERAND (arg
, 1));
6147 /* We can only get here with a single static member
6149 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
6150 && DECL_STATIC_FUNCTION_P (fn
));
6151 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
6152 return error_mark_node
;
6153 val
= build_address (fn
);
6154 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
6155 /* Do not lose object's side effects. */
6156 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
),
6157 TREE_OPERAND (arg
, 0), val
);
6161 tree object
= TREE_OPERAND (arg
, 0);
6162 tree field
= TREE_OPERAND (arg
, 1);
6163 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6164 (TREE_TYPE (object
), decl_type_context (field
)));
6165 val
= build_address (arg
);
6168 if (TYPE_PTR_P (argtype
)
6169 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
6171 build_ptrmemfunc_type (argtype
);
6172 val
= build_ptrmemfunc (argtype
, val
, 0,
6180 /* Take the address of ARG if it has one, even if it's an rvalue. */
6183 cp_build_addr_expr (tree arg
, tsubst_flags_t complain
)
6185 return cp_build_addr_expr_1 (arg
, 0, complain
);
6188 /* Take the address of ARG, but only if it's an lvalue. */
6191 cp_build_addr_expr_strict (tree arg
, tsubst_flags_t complain
)
6193 return cp_build_addr_expr_1 (arg
, 1, complain
);
6196 /* C++: Must handle pointers to members.
6198 Perhaps type instantiation should be extended to handle conversion
6199 from aggregates to types we don't yet know we want? (Or are those
6200 cases typically errors which should be reported?)
6202 NOCONVERT suppresses the default promotions (such as from short to int). */
6205 cp_build_unary_op (enum tree_code code
, tree xarg
, bool noconvert
,
6206 tsubst_flags_t complain
)
6208 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
6210 location_t location
= cp_expr_loc_or_loc (arg
, input_location
);
6212 const char *errstring
= NULL
;
6214 const char *invalid_op_diag
;
6216 if (!arg
|| error_operand_p (arg
))
6217 return error_mark_node
;
6219 arg
= resolve_nondeduced_context (arg
, complain
);
6221 if ((invalid_op_diag
6222 = targetm
.invalid_unary_op ((code
== UNARY_PLUS_EXPR
6227 if (complain
& tf_error
)
6228 error (invalid_op_diag
);
6229 return error_mark_node
;
6234 case UNARY_PLUS_EXPR
:
6237 int flags
= WANT_ARITH
| WANT_ENUM
;
6238 /* Unary plus (but not unary minus) is allowed on pointers. */
6239 if (code
== UNARY_PLUS_EXPR
)
6240 flags
|= WANT_POINTER
;
6241 arg
= build_expr_type_conversion (flags
, arg
, true);
6243 errstring
= (code
== NEGATE_EXPR
6244 ? _("wrong type argument to unary minus")
6245 : _("wrong type argument to unary plus"));
6248 if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
6249 arg
= cp_perform_integral_promotions (arg
, complain
);
6251 /* Make sure the result is not an lvalue: a unary plus or minus
6252 expression is always a rvalue. */
6259 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
6264 arg
= cp_default_conversion (arg
, complain
);
6265 if (arg
== error_mark_node
)
6266 return error_mark_node
;
6269 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
6270 | WANT_VECTOR_OR_COMPLEX
,
6272 errstring
= _("wrong type argument to bit-complement");
6273 else if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
6275 /* Warn if the expression has boolean value. */
6276 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
6277 && (complain
& tf_warning
)
6278 && warning_at (location
, OPT_Wbool_operation
,
6279 "%<~%> on an expression of type %<bool%>"))
6280 inform (location
, "did you mean to use logical not (%<!%>)?");
6281 arg
= cp_perform_integral_promotions (arg
, complain
);
6283 else if (!noconvert
&& VECTOR_TYPE_P (TREE_TYPE (arg
)))
6284 arg
= mark_rvalue_use (arg
);
6288 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
6289 errstring
= _("wrong type argument to abs");
6290 else if (!noconvert
)
6292 arg
= cp_default_conversion (arg
, complain
);
6293 if (arg
== error_mark_node
)
6294 return error_mark_node
;
6299 /* Conjugating a real value is a no-op, but allow it anyway. */
6300 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
6301 errstring
= _("wrong type argument to conjugation");
6302 else if (!noconvert
)
6304 arg
= cp_default_conversion (arg
, complain
);
6305 if (arg
== error_mark_node
)
6306 return error_mark_node
;
6310 case TRUTH_NOT_EXPR
:
6311 if (VECTOR_TYPE_P (TREE_TYPE (arg
)))
6312 return cp_build_binary_op (input_location
, EQ_EXPR
, arg
,
6313 build_zero_cst (TREE_TYPE (arg
)), complain
);
6314 arg
= perform_implicit_conversion (boolean_type_node
, arg
,
6316 val
= invert_truthvalue_loc (input_location
, arg
);
6317 if (arg
!= error_mark_node
)
6319 errstring
= _("in argument to unary !");
6327 arg
= build_real_imag_expr (input_location
, code
, arg
);
6330 case PREINCREMENT_EXPR
:
6331 case POSTINCREMENT_EXPR
:
6332 case PREDECREMENT_EXPR
:
6333 case POSTDECREMENT_EXPR
:
6334 /* Handle complex lvalues (when permitted)
6335 by reduction to simpler cases. */
6337 val
= unary_complex_lvalue (code
, arg
);
6341 arg
= mark_lvalue_use (arg
);
6343 /* Increment or decrement the real part of the value,
6344 and don't change the imaginary part. */
6345 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
6349 arg
= cp_stabilize_reference (arg
);
6350 real
= cp_build_unary_op (REALPART_EXPR
, arg
, true, complain
);
6351 imag
= cp_build_unary_op (IMAGPART_EXPR
, arg
, true, complain
);
6352 real
= cp_build_unary_op (code
, real
, true, complain
);
6353 if (real
== error_mark_node
|| imag
== error_mark_node
)
6354 return error_mark_node
;
6355 return build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
6359 /* Report invalid types. */
6361 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
6364 if (code
== PREINCREMENT_EXPR
)
6365 errstring
= _("no pre-increment operator for type");
6366 else if (code
== POSTINCREMENT_EXPR
)
6367 errstring
= _("no post-increment operator for type");
6368 else if (code
== PREDECREMENT_EXPR
)
6369 errstring
= _("no pre-decrement operator for type");
6371 errstring
= _("no post-decrement operator for type");
6374 else if (arg
== error_mark_node
)
6375 return error_mark_node
;
6377 /* Report something read-only. */
6379 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
6380 || TREE_READONLY (arg
))
6382 if (complain
& tf_error
)
6383 cxx_readonly_error (location
, arg
,
6384 ((code
== PREINCREMENT_EXPR
6385 || code
== POSTINCREMENT_EXPR
)
6386 ? lv_increment
: lv_decrement
));
6388 return error_mark_node
;
6393 tree declared_type
= unlowered_expr_type (arg
);
6395 argtype
= TREE_TYPE (arg
);
6397 /* ARM $5.2.5 last annotation says this should be forbidden. */
6398 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
6400 if (complain
& tf_error
)
6401 permerror (input_location
, (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
6402 ? G_("ISO C++ forbids incrementing an enum")
6403 : G_("ISO C++ forbids decrementing an enum"));
6405 return error_mark_node
;
6408 /* Compute the increment. */
6410 if (TYPE_PTR_P (argtype
))
6412 tree type
= complete_type (TREE_TYPE (argtype
));
6414 if (!COMPLETE_OR_VOID_TYPE_P (type
))
6416 if (complain
& tf_error
)
6417 error (((code
== PREINCREMENT_EXPR
6418 || code
== POSTINCREMENT_EXPR
))
6419 ? G_("cannot increment a pointer to incomplete type %qT")
6420 : G_("cannot decrement a pointer to incomplete type %qT"),
6421 TREE_TYPE (argtype
));
6423 return error_mark_node
;
6425 else if (!TYPE_PTROB_P (argtype
))
6427 if (complain
& tf_error
)
6428 pedwarn (input_location
, OPT_Wpointer_arith
,
6429 (code
== PREINCREMENT_EXPR
6430 || code
== POSTINCREMENT_EXPR
)
6431 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6432 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6435 return error_mark_node
;
6438 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
6441 inc
= VECTOR_TYPE_P (argtype
)
6442 ? build_one_cst (argtype
)
6445 inc
= cp_convert (argtype
, inc
, complain
);
6447 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6448 need to ask Objective-C to build the increment or decrement
6449 expression for it. */
6450 if (objc_is_property_ref (arg
))
6451 return objc_build_incr_expr_for_property_ref (input_location
, code
,
6454 /* Complain about anything else that is not a true lvalue. */
6455 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
6456 || code
== POSTINCREMENT_EXPR
)
6457 ? lv_increment
: lv_decrement
),
6459 return error_mark_node
;
6461 /* Forbid using -- or ++ in C++17 on `bool'. */
6462 if (TREE_CODE (declared_type
) == BOOLEAN_TYPE
)
6464 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
6466 if (complain
& tf_error
)
6467 error ("use of an operand of type %qT in %<operator--%> "
6468 "is forbidden", boolean_type_node
);
6469 return error_mark_node
;
6473 if (cxx_dialect
>= cxx17
)
6475 if (complain
& tf_error
)
6476 error ("use of an operand of type %qT in "
6477 "%<operator++%> is forbidden in C++17",
6479 return error_mark_node
;
6481 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6482 else if (!in_system_header_at (input_location
))
6483 warning (OPT_Wdeprecated
, "use of an operand of type %qT "
6484 "in %<operator++%> is deprecated",
6487 val
= boolean_increment (code
, arg
);
6489 else if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
6490 /* An rvalue has no cv-qualifiers. */
6491 val
= build2 (code
, cv_unqualified (TREE_TYPE (arg
)), arg
, inc
);
6493 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
6495 TREE_SIDE_EFFECTS (val
) = 1;
6500 /* Note that this operation never does default_conversion
6501 regardless of NOCONVERT. */
6502 return cp_build_addr_expr (arg
, complain
);
6511 argtype
= TREE_TYPE (arg
);
6512 return build1 (code
, argtype
, arg
);
6515 if (complain
& tf_error
)
6516 error ("%s", errstring
);
6517 return error_mark_node
;
6520 /* Hook for the c-common bits that build a unary op. */
6522 build_unary_op (location_t
/*location*/,
6523 enum tree_code code
, tree xarg
, bool noconvert
)
6525 return cp_build_unary_op (code
, xarg
, noconvert
, tf_warning_or_error
);
6528 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
6529 so that it is a valid lvalue even for GENERIC by replacing
6530 (lhs = rhs) with ((lhs = rhs), lhs)
6531 (--lhs) with ((--lhs), lhs)
6532 (++lhs) with ((++lhs), lhs)
6533 and if lhs has side-effects, calling cp_stabilize_reference on it, so
6534 that it can be evaluated multiple times. */
6537 genericize_compound_lvalue (tree lvalue
)
6539 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue
, 0)))
6540 lvalue
= build2 (TREE_CODE (lvalue
), TREE_TYPE (lvalue
),
6541 cp_stabilize_reference (TREE_OPERAND (lvalue
, 0)),
6542 TREE_OPERAND (lvalue
, 1));
6543 return build2 (COMPOUND_EXPR
, TREE_TYPE (TREE_OPERAND (lvalue
, 0)),
6544 lvalue
, TREE_OPERAND (lvalue
, 0));
6547 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6548 for certain kinds of expressions which are not really lvalues
6549 but which we can accept as lvalues.
6551 If ARG is not a kind of expression we can handle, return
6555 unary_complex_lvalue (enum tree_code code
, tree arg
)
6557 /* Inside a template, making these kinds of adjustments is
6558 pointless; we are only concerned with the type of the
6560 if (processing_template_decl
)
6563 /* Handle (a, b) used as an "lvalue". */
6564 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
6566 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 1), false,
6567 tf_warning_or_error
);
6568 return build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6569 TREE_OPERAND (arg
, 0), real_result
);
6572 /* Handle (a ? b : c) used as an "lvalue". */
6573 if (TREE_CODE (arg
) == COND_EXPR
6574 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
6575 return rationalize_conditional_expr (code
, arg
, tf_warning_or_error
);
6577 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6578 if (TREE_CODE (arg
) == MODIFY_EXPR
6579 || TREE_CODE (arg
) == PREINCREMENT_EXPR
6580 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
6581 return unary_complex_lvalue (code
, genericize_compound_lvalue (arg
));
6583 if (code
!= ADDR_EXPR
)
6586 /* Handle (a = b) used as an "lvalue" for `&'. */
6587 if (TREE_CODE (arg
) == MODIFY_EXPR
6588 || TREE_CODE (arg
) == INIT_EXPR
)
6590 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 0), false,
6591 tf_warning_or_error
);
6592 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6594 TREE_NO_WARNING (arg
) = 1;
6598 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg
))
6599 || TREE_CODE (arg
) == OFFSET_REF
)
6602 /* We permit compiler to make function calls returning
6603 objects of aggregate type look like lvalues. */
6607 if (TREE_CODE (targ
) == SAVE_EXPR
)
6608 targ
= TREE_OPERAND (targ
, 0);
6610 if (TREE_CODE (targ
) == CALL_EXPR
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (targ
)))
6612 if (TREE_CODE (arg
) == SAVE_EXPR
)
6615 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, tf_warning_or_error
);
6616 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
6619 if (TREE_CODE (arg
) == SAVE_EXPR
&& INDIRECT_REF_P (targ
))
6620 return build3 (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
6621 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
6624 /* Don't let anything else be handled specially. */
6628 /* Mark EXP saying that we need to be able to take the
6629 address of it; it should not be allocated in a register.
6630 Value is true if successful. ARRAY_REF_P is true if this
6631 is for ARRAY_REF construction - in that case we don't want
6632 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6633 it is fine to use ARRAY_REFs for vector subscripts on vector
6636 C++: we do not allow `current_class_ptr' to be addressable. */
6639 cxx_mark_addressable (tree exp
, bool array_ref_p
)
6644 switch (TREE_CODE (x
))
6646 case VIEW_CONVERT_EXPR
:
6648 && TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
6649 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x
, 0))))
6657 x
= TREE_OPERAND (x
, 0);
6661 if (x
== current_class_ptr
)
6663 error ("cannot take the address of %<this%>, which is an rvalue expression");
6664 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later. */
6670 /* Caller should not be trying to mark initialized
6671 constant fields addressable. */
6672 gcc_assert (DECL_LANG_SPECIFIC (x
) == 0
6673 || DECL_IN_AGGR_P (x
) == 0
6675 || DECL_EXTERNAL (x
));
6679 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
6680 && !DECL_ARTIFICIAL (x
))
6682 if (VAR_P (x
) && DECL_HARD_REGISTER (x
))
6685 ("address of explicit register variable %qD requested", x
);
6688 else if (extra_warnings
)
6690 (OPT_Wextra
, "address requested for %qD, which is declared %<register%>", x
);
6692 TREE_ADDRESSABLE (x
) = 1;
6697 TREE_ADDRESSABLE (x
) = 1;
6701 TREE_ADDRESSABLE (x
) = 1;
6705 TREE_ADDRESSABLE (x
) = 1;
6706 cxx_mark_addressable (TREE_OPERAND (x
, 0));
6714 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6717 build_x_conditional_expr (location_t loc
, tree ifexp
, tree op1
, tree op2
,
6718 tsubst_flags_t complain
)
6720 tree orig_ifexp
= ifexp
;
6721 tree orig_op1
= op1
;
6722 tree orig_op2
= op2
;
6725 if (processing_template_decl
)
6727 /* The standard says that the expression is type-dependent if
6728 IFEXP is type-dependent, even though the eventual type of the
6729 expression doesn't dependent on IFEXP. */
6730 if (type_dependent_expression_p (ifexp
)
6731 /* As a GNU extension, the middle operand may be omitted. */
6732 || (op1
&& type_dependent_expression_p (op1
))
6733 || type_dependent_expression_p (op2
))
6734 return build_min_nt_loc (loc
, COND_EXPR
, ifexp
, op1
, op2
);
6735 ifexp
= build_non_dependent_expr (ifexp
);
6737 op1
= build_non_dependent_expr (op1
);
6738 op2
= build_non_dependent_expr (op2
);
6741 expr
= build_conditional_expr (loc
, ifexp
, op1
, op2
, complain
);
6742 if (processing_template_decl
&& expr
!= error_mark_node
)
6744 tree min
= build_min_non_dep (COND_EXPR
, expr
,
6745 orig_ifexp
, orig_op1
, orig_op2
);
6746 expr
= convert_from_reference (min
);
6751 /* Given a list of expressions, return a compound expression
6752 that performs them all and returns the value of the last of them. */
6755 build_x_compound_expr_from_list (tree list
, expr_list_kind exp
,
6756 tsubst_flags_t complain
)
6758 tree expr
= TREE_VALUE (list
);
6760 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
6761 && !CONSTRUCTOR_IS_DIRECT_INIT (expr
))
6763 if (complain
& tf_error
)
6764 pedwarn (cp_expr_loc_or_loc (expr
, input_location
), 0,
6765 "list-initializer for non-class type must not "
6766 "be parenthesized");
6768 return error_mark_node
;
6771 if (TREE_CHAIN (list
))
6773 if (complain
& tf_error
)
6777 permerror (input_location
, "expression list treated as compound "
6778 "expression in initializer");
6781 permerror (input_location
, "expression list treated as compound "
6782 "expression in mem-initializer");
6785 permerror (input_location
, "expression list treated as compound "
6786 "expression in functional cast");
6792 return error_mark_node
;
6794 for (list
= TREE_CHAIN (list
); list
; list
= TREE_CHAIN (list
))
6795 expr
= build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list
)),
6796 expr
, TREE_VALUE (list
), complain
);
6802 /* Like build_x_compound_expr_from_list, but using a VEC. */
6805 build_x_compound_expr_from_vec (vec
<tree
, va_gc
> *vec
, const char *msg
,
6806 tsubst_flags_t complain
)
6808 if (vec_safe_is_empty (vec
))
6810 else if (vec
->length () == 1)
6820 if (complain
& tf_error
)
6821 permerror (input_location
,
6822 "%s expression list treated as compound expression",
6825 return error_mark_node
;
6829 for (ix
= 1; vec
->iterate (ix
, &t
); ++ix
)
6830 expr
= build_x_compound_expr (EXPR_LOCATION (t
), expr
,
6837 /* Handle overloading of the ',' operator when needed. */
6840 build_x_compound_expr (location_t loc
, tree op1
, tree op2
,
6841 tsubst_flags_t complain
)
6844 tree orig_op1
= op1
;
6845 tree orig_op2
= op2
;
6846 tree overload
= NULL_TREE
;
6848 if (processing_template_decl
)
6850 if (type_dependent_expression_p (op1
)
6851 || type_dependent_expression_p (op2
))
6852 return build_min_nt_loc (loc
, COMPOUND_EXPR
, op1
, op2
);
6853 op1
= build_non_dependent_expr (op1
);
6854 op2
= build_non_dependent_expr (op2
);
6857 result
= build_new_op (loc
, COMPOUND_EXPR
, LOOKUP_NORMAL
, op1
, op2
,
6858 NULL_TREE
, &overload
, complain
);
6860 result
= cp_build_compound_expr (op1
, op2
, complain
);
6862 if (processing_template_decl
&& result
!= error_mark_node
)
6864 if (overload
!= NULL_TREE
)
6865 return (build_min_non_dep_op_overload
6866 (COMPOUND_EXPR
, result
, overload
, orig_op1
, orig_op2
));
6868 return build_min_non_dep (COMPOUND_EXPR
, result
, orig_op1
, orig_op2
);
6874 /* Like cp_build_compound_expr, but for the c-common bits. */
6877 build_compound_expr (location_t
/*loc*/, tree lhs
, tree rhs
)
6879 return cp_build_compound_expr (lhs
, rhs
, tf_warning_or_error
);
6882 /* Build a compound expression. */
6885 cp_build_compound_expr (tree lhs
, tree rhs
, tsubst_flags_t complain
)
6887 lhs
= convert_to_void (lhs
, ICV_LEFT_OF_COMMA
, complain
);
6889 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
6890 return error_mark_node
;
6892 if (TREE_CODE (rhs
) == TARGET_EXPR
)
6894 /* If the rhs is a TARGET_EXPR, then build the compound
6895 expression inside the target_expr's initializer. This
6896 helps the compiler to eliminate unnecessary temporaries. */
6897 tree init
= TREE_OPERAND (rhs
, 1);
6899 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), lhs
, init
);
6900 TREE_OPERAND (rhs
, 1) = init
;
6905 if (type_unknown_p (rhs
))
6907 if (complain
& tf_error
)
6908 error ("no context to resolve type of %qE", rhs
);
6909 return error_mark_node
;
6912 return build2 (COMPOUND_EXPR
, TREE_TYPE (rhs
), lhs
, rhs
);
6915 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6916 casts away constness. CAST gives the type of cast. Returns true
6917 if the cast is ill-formed, false if it is well-formed.
6919 ??? This function warns for casting away any qualifier not just
6920 const. We would like to specify exactly what qualifiers are casted
6925 check_for_casting_away_constness (tree src_type
, tree dest_type
,
6926 enum tree_code cast
, tsubst_flags_t complain
)
6928 /* C-style casts are allowed to cast away constness. With
6929 WARN_CAST_QUAL, we still want to issue a warning. */
6930 if (cast
== CAST_EXPR
&& !warn_cast_qual
)
6933 if (!casts_away_constness (src_type
, dest_type
, complain
))
6939 if (complain
& tf_warning
)
6940 warning (OPT_Wcast_qual
,
6941 "cast from type %qT to type %qT casts away qualifiers",
6942 src_type
, dest_type
);
6945 case STATIC_CAST_EXPR
:
6946 if (complain
& tf_error
)
6947 error ("%<static_cast%> from type %qT to type %qT casts away "
6949 src_type
, dest_type
);
6952 case REINTERPRET_CAST_EXPR
:
6953 if (complain
& tf_error
)
6954 error ("%<reinterpret_cast%> from type %qT to type %qT casts away "
6956 src_type
, dest_type
);
6964 /* Warns if the cast from expression EXPR to type TYPE is useless. */
6966 maybe_warn_about_useless_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6968 if (warn_useless_cast
6969 && complain
& tf_warning
)
6971 if ((TYPE_REF_P (type
)
6972 && (TYPE_REF_IS_RVALUE (type
)
6973 ? xvalue_p (expr
) : lvalue_p (expr
))
6974 && same_type_p (TREE_TYPE (expr
), TREE_TYPE (type
)))
6975 || same_type_p (TREE_TYPE (expr
), type
))
6976 warning (OPT_Wuseless_cast
, "useless cast to type %q#T", type
);
6980 /* Warns if the cast ignores cv-qualifiers on TYPE. */
6982 maybe_warn_about_cast_ignoring_quals (tree type
, tsubst_flags_t complain
)
6984 if (warn_ignored_qualifiers
6985 && complain
& tf_warning
6986 && !CLASS_TYPE_P (type
)
6987 && (cp_type_quals (type
) & (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)))
6989 warning (OPT_Wignored_qualifiers
, "type qualifiers ignored on cast "
6994 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6995 (another pointer-to-member type in the same hierarchy) and return
6996 the converted expression. If ALLOW_INVERSE_P is permitted, a
6997 pointer-to-derived may be converted to pointer-to-base; otherwise,
6998 only the other direction is permitted. If C_CAST_P is true, this
6999 conversion is taking place as part of a C-style cast. */
7002 convert_ptrmem (tree type
, tree expr
, bool allow_inverse_p
,
7003 bool c_cast_p
, tsubst_flags_t complain
)
7005 if (same_type_p (type
, TREE_TYPE (expr
)))
7008 if (TYPE_PTRDATAMEM_P (type
))
7010 tree obase
= TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr
));
7011 tree nbase
= TYPE_PTRMEM_CLASS_TYPE (type
);
7012 tree delta
= (get_delta_difference
7014 allow_inverse_p
, c_cast_p
, complain
));
7016 if (delta
== error_mark_node
)
7017 return error_mark_node
;
7019 if (!same_type_p (obase
, nbase
))
7021 if (TREE_CODE (expr
) == PTRMEM_CST
)
7022 expr
= cplus_expand_constant (expr
);
7024 tree cond
= cp_build_binary_op (input_location
, EQ_EXPR
, expr
,
7025 build_int_cst (TREE_TYPE (expr
), -1),
7027 tree op1
= build_nop (ptrdiff_type_node
, expr
);
7028 tree op2
= cp_build_binary_op (input_location
, PLUS_EXPR
, op1
, delta
,
7031 expr
= fold_build3_loc (input_location
,
7032 COND_EXPR
, ptrdiff_type_node
, cond
, op1
, op2
);
7035 return build_nop (type
, expr
);
7038 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
,
7039 allow_inverse_p
, c_cast_p
, complain
);
7042 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
7043 this static_cast is being attempted as one of the possible casts
7044 allowed by a C-style cast. (In that case, accessibility of base
7045 classes is not considered, and it is OK to cast away
7046 constness.) Return the result of the cast. *VALID_P is set to
7047 indicate whether or not the cast was valid. */
7050 build_static_cast_1 (tree type
, tree expr
, bool c_cast_p
,
7051 bool *valid_p
, tsubst_flags_t complain
)
7057 /* Assume the cast is valid. */
7060 intype
= unlowered_expr_type (expr
);
7062 /* Save casted types in the function's used types hash table. */
7063 used_types_insert (type
);
7065 /* A prvalue of non-class type is cv-unqualified. */
7066 if (!CLASS_TYPE_P (type
))
7067 type
= cv_unqualified (type
);
7069 /* [expr.static.cast]
7071 An lvalue of type "cv1 B", where B is a class type, can be cast
7072 to type "reference to cv2 D", where D is a class derived (clause
7073 _class.derived_) from B, if a valid standard conversion from
7074 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
7075 same cv-qualification as, or greater cv-qualification than, cv1,
7076 and B is not a virtual base class of D. */
7077 /* We check this case before checking the validity of "TYPE t =
7078 EXPR;" below because for this case:
7081 struct D : public B { D(const B&); };
7083 void f() { static_cast<const D&>(b); }
7085 we want to avoid constructing a new D. The standard is not
7086 completely clear about this issue, but our interpretation is
7087 consistent with other compilers. */
7088 if (TYPE_REF_P (type
)
7089 && CLASS_TYPE_P (TREE_TYPE (type
))
7090 && CLASS_TYPE_P (intype
)
7091 && (TYPE_REF_IS_RVALUE (type
) || lvalue_p (expr
))
7092 && DERIVED_FROM_P (intype
, TREE_TYPE (type
))
7093 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype
)),
7094 build_pointer_type (TYPE_MAIN_VARIANT
7095 (TREE_TYPE (type
))),
7098 || at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
7102 if (processing_template_decl
)
7105 /* There is a standard conversion from "D*" to "B*" even if "B"
7106 is ambiguous or inaccessible. If this is really a
7107 static_cast, then we check both for inaccessibility and
7108 ambiguity. However, if this is a static_cast being performed
7109 because the user wrote a C-style cast, then accessibility is
7111 base
= lookup_base (TREE_TYPE (type
), intype
,
7112 c_cast_p
? ba_unique
: ba_check
,
7114 expr
= cp_build_addr_expr (expr
, complain
);
7116 if (sanitize_flags_p (SANITIZE_VPTR
))
7119 = cp_ubsan_maybe_instrument_downcast (input_location
, type
,
7125 /* Convert from "B*" to "D*". This function will check that "B"
7126 is not a virtual base of "D". Even if we don't have a guarantee
7127 that expr is NULL, if the static_cast is to a reference type,
7128 it is UB if it would be NULL, so omit the non-NULL check. */
7129 expr
= build_base_path (MINUS_EXPR
, expr
, base
,
7130 /*nonnull=*/flag_delete_null_pointer_checks
,
7133 /* Convert the pointer to a reference -- but then remember that
7134 there are no expressions with reference type in C++.
7136 We call rvalue so that there's an actual tree code
7137 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
7138 is a variable with the same type, the conversion would get folded
7139 away, leaving just the variable and causing lvalue_kind to give
7140 the wrong answer. */
7141 expr
= cp_fold_convert (type
, expr
);
7143 /* When -fsanitize=null, make sure to diagnose reference binding to
7144 NULL even when the reference is converted to pointer later on. */
7145 if (sanitize_flags_p (SANITIZE_NULL
)
7146 && TREE_CODE (expr
) == COND_EXPR
7147 && TREE_OPERAND (expr
, 2)
7148 && TREE_CODE (TREE_OPERAND (expr
, 2)) == INTEGER_CST
7149 && TREE_TYPE (TREE_OPERAND (expr
, 2)) == type
)
7150 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr
, 2));
7152 return convert_from_reference (rvalue (expr
));
7155 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
7156 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
7157 if (TYPE_REF_P (type
)
7158 && TYPE_REF_IS_RVALUE (type
)
7159 && (clk
= real_lvalue_p (expr
))
7160 && reference_related_p (TREE_TYPE (type
), intype
)
7161 && (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
7163 if (processing_template_decl
)
7165 if (clk
== clk_ordinary
)
7167 /* Handle the (non-bit-field) lvalue case here by casting to
7168 lvalue reference and then changing it to an rvalue reference.
7169 Casting an xvalue to rvalue reference will be handled by the
7171 tree lref
= cp_build_reference_type (TREE_TYPE (type
), false);
7172 result
= (perform_direct_initialization_if_possible
7173 (lref
, expr
, c_cast_p
, complain
));
7174 result
= build1 (NON_LVALUE_EXPR
, type
, result
);
7175 return convert_from_reference (result
);
7178 /* For a bit-field or packed field, bind to a temporary. */
7179 expr
= rvalue (expr
);
7182 /* Resolve overloaded address here rather than once in
7183 implicit_conversion and again in the inverse code below. */
7184 if (TYPE_PTRMEMFUNC_P (type
) && type_unknown_p (expr
))
7186 expr
= instantiate_type (type
, expr
, complain
);
7187 intype
= TREE_TYPE (expr
);
7190 /* [expr.static.cast]
7192 Any expression can be explicitly converted to type cv void. */
7193 if (VOID_TYPE_P (type
))
7194 return convert_to_void (expr
, ICV_CAST
, complain
);
7197 An abstract class shall not be used ... as the type of an explicit
7199 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
7200 return error_mark_node
;
7202 /* [expr.static.cast]
7204 An expression e can be explicitly converted to a type T using a
7205 static_cast of the form static_cast<T>(e) if the declaration T
7206 t(e);" is well-formed, for some invented temporary variable
7208 result
= perform_direct_initialization_if_possible (type
, expr
,
7209 c_cast_p
, complain
);
7212 if (processing_template_decl
)
7215 result
= convert_from_reference (result
);
7217 /* [expr.static.cast]
7219 If T is a reference type, the result is an lvalue; otherwise,
7220 the result is an rvalue. */
7221 if (!TYPE_REF_P (type
))
7223 result
= rvalue (result
);
7225 if (result
== expr
&& SCALAR_TYPE_P (type
))
7226 /* Leave some record of the cast. */
7227 result
= build_nop (type
, expr
);
7232 /* [expr.static.cast]
7234 The inverse of any standard conversion sequence (clause _conv_),
7235 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
7236 (_conv.array_), function-to-pointer (_conv.func_), and boolean
7237 (_conv.bool_) conversions, can be performed explicitly using
7238 static_cast subject to the restriction that the explicit
7239 conversion does not cast away constness (_expr.const.cast_), and
7240 the following additional rules for specific cases: */
7241 /* For reference, the conversions not excluded are: integral
7242 promotions, floating-point promotion, integral conversions,
7243 floating-point conversions, floating-integral conversions,
7244 pointer conversions, and pointer to member conversions. */
7247 A value of integral _or enumeration_ type can be explicitly
7248 converted to an enumeration type. */
7249 /* The effect of all that is that any conversion between any two
7250 types which are integral, floating, or enumeration types can be
7252 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7253 || SCALAR_FLOAT_TYPE_P (type
))
7254 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
7255 || SCALAR_FLOAT_TYPE_P (intype
)))
7257 if (processing_template_decl
)
7259 return ocp_convert (type
, expr
, CONV_C_CAST
, LOOKUP_NORMAL
, complain
);
7262 if (TYPE_PTR_P (type
) && TYPE_PTR_P (intype
)
7263 && CLASS_TYPE_P (TREE_TYPE (type
))
7264 && CLASS_TYPE_P (TREE_TYPE (intype
))
7265 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7266 (TREE_TYPE (intype
))),
7267 build_pointer_type (TYPE_MAIN_VARIANT
7268 (TREE_TYPE (type
))),
7273 if (processing_template_decl
)
7277 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
7279 return error_mark_node
;
7280 base
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
7281 c_cast_p
? ba_unique
: ba_check
,
7283 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
7286 if (sanitize_flags_p (SANITIZE_VPTR
))
7289 = cp_ubsan_maybe_instrument_downcast (input_location
, type
,
7295 return cp_fold_convert (type
, expr
);
7298 if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
7299 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
7306 c1
= TYPE_PTRMEM_CLASS_TYPE (intype
);
7307 c2
= TYPE_PTRMEM_CLASS_TYPE (type
);
7309 if (TYPE_PTRDATAMEM_P (type
))
7311 t1
= (build_ptrmem_type
7313 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype
))));
7314 t2
= (build_ptrmem_type
7316 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
7323 if (can_convert (t1
, t2
, complain
) || can_convert (t2
, t1
, complain
))
7326 && check_for_casting_away_constness (intype
, type
,
7329 return error_mark_node
;
7330 if (processing_template_decl
)
7332 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/1,
7333 c_cast_p
, complain
);
7337 /* [expr.static.cast]
7339 An rvalue of type "pointer to cv void" can be explicitly
7340 converted to a pointer to object type. A value of type pointer
7341 to object converted to "pointer to cv void" and back to the
7342 original pointer type will have its original value. */
7343 if (TYPE_PTR_P (intype
)
7344 && VOID_TYPE_P (TREE_TYPE (intype
))
7345 && TYPE_PTROB_P (type
))
7348 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
7350 return error_mark_node
;
7351 if (processing_template_decl
)
7353 return build_nop (type
, expr
);
7357 return error_mark_node
;
7360 /* Return an expression representing static_cast<TYPE>(EXPR). */
7363 build_static_cast (tree type
, tree oexpr
, tsubst_flags_t complain
)
7369 if (type
== error_mark_node
|| expr
== error_mark_node
)
7370 return error_mark_node
;
7372 bool dependent
= (dependent_type_p (type
)
7373 || type_dependent_expression_p (expr
));
7377 expr
= build_min (STATIC_CAST_EXPR
, type
, oexpr
);
7378 /* We don't know if it will or will not have side effects. */
7379 TREE_SIDE_EFFECTS (expr
) = 1;
7380 return convert_from_reference (expr
);
7382 else if (processing_template_decl
)
7383 expr
= build_non_dependent_expr (expr
);
7385 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7386 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7387 if (!TYPE_REF_P (type
)
7388 && TREE_CODE (expr
) == NOP_EXPR
7389 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7390 expr
= TREE_OPERAND (expr
, 0);
7392 result
= build_static_cast_1 (type
, expr
, /*c_cast_p=*/false, &valid_p
,
7396 if (result
!= error_mark_node
)
7398 maybe_warn_about_useless_cast (type
, expr
, complain
);
7399 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7401 if (processing_template_decl
)
7406 if (complain
& tf_error
)
7408 error ("invalid %<static_cast%> from type %qT to type %qT",
7409 TREE_TYPE (expr
), type
);
7410 if ((TYPE_PTR_P (type
) || TYPE_REF_P (type
))
7411 && CLASS_TYPE_P (TREE_TYPE (type
))
7412 && !COMPLETE_TYPE_P (TREE_TYPE (type
)))
7413 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type
))),
7414 "class type %qT is incomplete", TREE_TYPE (type
));
7415 tree expr_type
= TREE_TYPE (expr
);
7416 if (TYPE_PTR_P (expr_type
))
7417 expr_type
= TREE_TYPE (expr_type
);
7418 if (CLASS_TYPE_P (expr_type
) && !COMPLETE_TYPE_P (expr_type
))
7419 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type
)),
7420 "class type %qT is incomplete", expr_type
);
7422 return error_mark_node
;
7425 /* EXPR is an expression with member function or pointer-to-member
7426 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7427 not permitted by ISO C++, but we accept it in some modes. If we
7428 are not in one of those modes, issue a diagnostic. Return the
7429 converted expression. */
7432 convert_member_func_to_ptr (tree type
, tree expr
, tsubst_flags_t complain
)
7437 intype
= TREE_TYPE (expr
);
7438 gcc_assert (TYPE_PTRMEMFUNC_P (intype
)
7439 || TREE_CODE (intype
) == METHOD_TYPE
);
7441 if (!(complain
& tf_warning_or_error
))
7442 return error_mark_node
;
7444 if (pedantic
|| warn_pmf2ptr
)
7445 pedwarn (input_location
, pedantic
? OPT_Wpedantic
: OPT_Wpmf_conversions
,
7446 "converting from %qH to %qI", intype
, type
);
7448 if (TREE_CODE (intype
) == METHOD_TYPE
)
7449 expr
= build_addr_func (expr
, complain
);
7450 else if (TREE_CODE (expr
) == PTRMEM_CST
)
7451 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
7454 decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
), 0);
7455 decl
= build_address (decl
);
7456 expr
= get_member_function_from_ptrfunc (&decl
, expr
, complain
);
7459 if (expr
== error_mark_node
)
7460 return error_mark_node
;
7462 return build_nop (type
, expr
);
7465 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
7466 constexpr evaluation knows to reject it. */
7469 build_nop_reinterpret (tree type
, tree expr
)
7471 tree ret
= build_nop (type
, expr
);
7473 REINTERPRET_CAST_P (ret
) = true;
7477 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7478 If C_CAST_P is true, this reinterpret cast is being done as part of
7479 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7480 indicate whether or not reinterpret_cast was valid. */
7483 build_reinterpret_cast_1 (tree type
, tree expr
, bool c_cast_p
,
7484 bool *valid_p
, tsubst_flags_t complain
)
7488 /* Assume the cast is invalid. */
7492 if (type
== error_mark_node
|| error_operand_p (expr
))
7493 return error_mark_node
;
7495 intype
= TREE_TYPE (expr
);
7497 /* Save casted types in the function's used types hash table. */
7498 used_types_insert (type
);
7500 /* A prvalue of non-class type is cv-unqualified. */
7501 if (!CLASS_TYPE_P (type
))
7502 type
= cv_unqualified (type
);
7504 /* [expr.reinterpret.cast]
7505 A glvalue expression of type T1 can be cast to the type
7506 "reference to T2" if an expression of type "pointer to T1" can be
7507 explicitly converted to the type "pointer to T2" using a
7508 reinterpret_cast. */
7509 if (TYPE_REF_P (type
))
7511 if (TYPE_REF_IS_RVALUE (type
) && !VOID_TYPE_P (intype
))
7513 if (!obvalue_p (expr
))
7514 /* Perform the temporary materialization conversion. */
7515 expr
= get_target_expr_sfinae (expr
, complain
);
7517 else if (!lvalue_p (expr
))
7519 if (complain
& tf_error
)
7520 error ("invalid cast of an rvalue expression of type "
7523 return error_mark_node
;
7526 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7527 "B" are related class types; the reinterpret_cast does not
7528 adjust the pointer. */
7529 if (TYPE_PTR_P (intype
)
7530 && (complain
& tf_warning
)
7531 && (comptypes (TREE_TYPE (intype
), TREE_TYPE (type
),
7532 COMPARE_BASE
| COMPARE_DERIVED
)))
7533 warning (0, "casting %qT to %qT does not dereference pointer",
7536 expr
= cp_build_addr_expr (expr
, complain
);
7538 if (warn_strict_aliasing
> 2)
7539 strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
7541 if (expr
!= error_mark_node
)
7542 expr
= build_reinterpret_cast_1
7543 (build_pointer_type (TREE_TYPE (type
)), expr
, c_cast_p
,
7545 if (expr
!= error_mark_node
)
7546 /* cp_build_indirect_ref isn't right for rvalue refs. */
7547 expr
= convert_from_reference (fold_convert (type
, expr
));
7551 /* As a G++ extension, we consider conversions from member
7552 functions, and pointers to member functions to
7553 pointer-to-function and pointer-to-void types. If
7554 -Wno-pmf-conversions has not been specified,
7555 convert_member_func_to_ptr will issue an error message. */
7556 if ((TYPE_PTRMEMFUNC_P (intype
)
7557 || TREE_CODE (intype
) == METHOD_TYPE
)
7558 && TYPE_PTR_P (type
)
7559 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
7560 || VOID_TYPE_P (TREE_TYPE (type
))))
7561 return convert_member_func_to_ptr (type
, expr
, complain
);
7563 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7564 array-to-pointer, and function-to-pointer conversions are
7566 expr
= decay_conversion (expr
, complain
);
7568 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7569 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7570 if (TREE_CODE (expr
) == NOP_EXPR
7571 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7572 expr
= TREE_OPERAND (expr
, 0);
7574 if (error_operand_p (expr
))
7575 return error_mark_node
;
7577 intype
= TREE_TYPE (expr
);
7579 /* [expr.reinterpret.cast]
7580 A pointer can be converted to any integral type large enough to
7581 hold it. ... A value of type std::nullptr_t can be converted to
7582 an integral type; the conversion has the same meaning and
7583 validity as a conversion of (void*)0 to the integral type. */
7584 if (CP_INTEGRAL_TYPE_P (type
)
7585 && (TYPE_PTR_P (intype
) || NULLPTR_TYPE_P (intype
)))
7587 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
7589 if (complain
& tf_error
)
7590 permerror (input_location
, "cast from %qH to %qI loses precision",
7593 return error_mark_node
;
7595 if (NULLPTR_TYPE_P (intype
))
7596 return build_int_cst (type
, 0);
7598 /* [expr.reinterpret.cast]
7599 A value of integral or enumeration type can be explicitly
7600 converted to a pointer. */
7601 else if (TYPE_PTR_P (type
) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype
))
7604 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7605 || TYPE_PTR_OR_PTRMEM_P (type
))
7606 && same_type_p (type
, intype
))
7608 return rvalue (expr
);
7609 else if (TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
7611 if ((complain
& tf_warning
)
7612 && !cxx_safe_function_type_cast_p (TREE_TYPE (type
),
7613 TREE_TYPE (intype
)))
7614 warning (OPT_Wcast_function_type
,
7615 "cast between incompatible function types"
7616 " from %qH to %qI", intype
, type
);
7617 return build_nop_reinterpret (type
, expr
);
7619 else if (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
))
7621 if ((complain
& tf_warning
)
7622 && !cxx_safe_function_type_cast_p
7623 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type
)),
7624 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype
))))
7625 warning (OPT_Wcast_function_type
,
7626 "cast between incompatible pointer to member types"
7627 " from %qH to %qI", intype
, type
);
7628 return build_nop_reinterpret (type
, expr
);
7630 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
7631 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
7634 && check_for_casting_away_constness (intype
, type
,
7635 REINTERPRET_CAST_EXPR
,
7637 return error_mark_node
;
7638 /* Warn about possible alignment problems. */
7639 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
7640 && (complain
& tf_warning
)
7641 && !VOID_TYPE_P (type
)
7642 && TREE_CODE (TREE_TYPE (intype
)) != FUNCTION_TYPE
7643 && COMPLETE_TYPE_P (TREE_TYPE (type
))
7644 && COMPLETE_TYPE_P (TREE_TYPE (intype
))
7645 && min_align_of_type (TREE_TYPE (type
))
7646 > min_align_of_type (TREE_TYPE (intype
)))
7647 warning (OPT_Wcast_align
, "cast from %qH to %qI "
7648 "increases required alignment of target type", intype
, type
);
7650 if (warn_strict_aliasing
<= 2)
7651 /* strict_aliasing_warning STRIP_NOPs its expr. */
7652 strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
7654 return build_nop_reinterpret (type
, expr
);
7656 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
7657 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
7659 if (complain
& tf_warning
)
7660 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7661 object pointer type or vice versa is conditionally-supported." */
7662 warning (OPT_Wconditionally_supported
,
7663 "casting between pointer-to-function and pointer-to-object "
7664 "is conditionally-supported");
7665 return build_nop_reinterpret (type
, expr
);
7667 else if (VECTOR_TYPE_P (type
))
7668 return convert_to_vector (type
, expr
);
7669 else if (VECTOR_TYPE_P (intype
)
7670 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
7671 return convert_to_integer_nofold (type
, expr
);
7676 if (complain
& tf_error
)
7677 error ("invalid cast from type %qT to type %qT", intype
, type
);
7678 return error_mark_node
;
7681 expr
= cp_convert (type
, expr
, complain
);
7682 if (TREE_CODE (expr
) == NOP_EXPR
)
7683 /* Mark any nop_expr that created as a reintepret_cast. */
7684 REINTERPRET_CAST_P (expr
) = true;
7689 build_reinterpret_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7693 if (type
== error_mark_node
|| expr
== error_mark_node
)
7694 return error_mark_node
;
7696 if (processing_template_decl
)
7698 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
7700 if (!TREE_SIDE_EFFECTS (t
)
7701 && type_dependent_expression_p (expr
))
7702 /* There might turn out to be side effects inside expr. */
7703 TREE_SIDE_EFFECTS (t
) = 1;
7704 return convert_from_reference (t
);
7707 r
= build_reinterpret_cast_1 (type
, expr
, /*c_cast_p=*/false,
7708 /*valid_p=*/NULL
, complain
);
7709 if (r
!= error_mark_node
)
7711 maybe_warn_about_useless_cast (type
, expr
, complain
);
7712 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7717 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7718 return an appropriate expression. Otherwise, return
7719 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7720 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7721 performing a C-style cast, its value upon return will indicate
7722 whether or not the conversion succeeded. */
7725 build_const_cast_1 (tree dst_type
, tree expr
, tsubst_flags_t complain
,
7729 tree reference_type
;
7731 /* Callers are responsible for handling error_mark_node as a
7732 destination type. */
7733 gcc_assert (dst_type
!= error_mark_node
);
7734 /* In a template, callers should be building syntactic
7735 representations of casts, not using this machinery. */
7736 gcc_assert (!processing_template_decl
);
7738 /* Assume the conversion is invalid. */
7742 if (!INDIRECT_TYPE_P (dst_type
) && !TYPE_PTRDATAMEM_P (dst_type
))
7744 if (complain
& tf_error
)
7745 error ("invalid use of %<const_cast%> with type %qT, "
7746 "which is not a pointer, "
7747 "reference, nor a pointer-to-data-member type", dst_type
);
7748 return error_mark_node
;
7751 if (TREE_CODE (TREE_TYPE (dst_type
)) == FUNCTION_TYPE
)
7753 if (complain
& tf_error
)
7754 error ("invalid use of %<const_cast%> with type %qT, "
7755 "which is a pointer or reference to a function type",
7757 return error_mark_node
;
7760 /* A prvalue of non-class type is cv-unqualified. */
7761 dst_type
= cv_unqualified (dst_type
);
7763 /* Save casted types in the function's used types hash table. */
7764 used_types_insert (dst_type
);
7766 src_type
= TREE_TYPE (expr
);
7767 /* Expressions do not really have reference types. */
7768 if (TYPE_REF_P (src_type
))
7769 src_type
= TREE_TYPE (src_type
);
7771 /* [expr.const.cast]
7773 For two object types T1 and T2, if a pointer to T1 can be explicitly
7774 converted to the type "pointer to T2" using a const_cast, then the
7775 following conversions can also be made:
7777 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7778 type T2 using the cast const_cast<T2&>;
7780 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7781 type T2 using the cast const_cast<T2&&>; and
7783 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7784 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7786 if (TYPE_REF_P (dst_type
))
7788 reference_type
= dst_type
;
7789 if (!TYPE_REF_IS_RVALUE (dst_type
)
7795 if (complain
& tf_error
)
7796 error ("invalid %<const_cast%> of an rvalue of type %qT "
7798 src_type
, dst_type
);
7799 return error_mark_node
;
7801 dst_type
= build_pointer_type (TREE_TYPE (dst_type
));
7802 src_type
= build_pointer_type (src_type
);
7806 reference_type
= NULL_TREE
;
7807 /* If the destination type is not a reference type, the
7808 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7809 conversions are performed. */
7810 src_type
= type_decays_to (src_type
);
7811 if (src_type
== error_mark_node
)
7812 return error_mark_node
;
7815 if (TYPE_PTR_P (src_type
) || TYPE_PTRDATAMEM_P (src_type
))
7817 if (comp_ptr_ttypes_const (dst_type
, src_type
))
7822 /* This cast is actually a C-style cast. Issue a warning if
7823 the user is making a potentially unsafe cast. */
7824 check_for_casting_away_constness (src_type
, dst_type
,
7825 CAST_EXPR
, complain
);
7826 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
7827 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
7828 && (complain
& tf_warning
)
7829 && min_align_of_type (TREE_TYPE (dst_type
))
7830 > min_align_of_type (TREE_TYPE (src_type
)))
7831 warning (OPT_Wcast_align
, "cast from %qH to %qI "
7832 "increases required alignment of target type",
7833 src_type
, dst_type
);
7837 expr
= cp_build_addr_expr (expr
, complain
);
7838 if (expr
== error_mark_node
)
7839 return error_mark_node
;
7840 expr
= build_nop (reference_type
, expr
);
7841 return convert_from_reference (expr
);
7845 expr
= decay_conversion (expr
, complain
);
7846 if (expr
== error_mark_node
)
7847 return error_mark_node
;
7849 /* build_c_cast puts on a NOP_EXPR to make the result not an
7850 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7851 non-lvalue context. */
7852 if (TREE_CODE (expr
) == NOP_EXPR
7853 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7854 expr
= TREE_OPERAND (expr
, 0);
7855 return build_nop (dst_type
, expr
);
7859 && !at_least_as_qualified_p (TREE_TYPE (dst_type
),
7860 TREE_TYPE (src_type
)))
7861 check_for_casting_away_constness (src_type
, dst_type
, CAST_EXPR
,
7865 if (complain
& tf_error
)
7866 error ("invalid %<const_cast%> from type %qT to type %qT",
7867 src_type
, dst_type
);
7868 return error_mark_node
;
7872 build_const_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7876 if (type
== error_mark_node
|| error_operand_p (expr
))
7877 return error_mark_node
;
7879 if (processing_template_decl
)
7881 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
7883 if (!TREE_SIDE_EFFECTS (t
)
7884 && type_dependent_expression_p (expr
))
7885 /* There might turn out to be side effects inside expr. */
7886 TREE_SIDE_EFFECTS (t
) = 1;
7887 return convert_from_reference (t
);
7890 r
= build_const_cast_1 (type
, expr
, complain
, /*valid_p=*/NULL
);
7891 if (r
!= error_mark_node
)
7893 maybe_warn_about_useless_cast (type
, expr
, complain
);
7894 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7899 /* Like cp_build_c_cast, but for the c-common bits. */
7902 build_c_cast (location_t
/*loc*/, tree type
, tree expr
)
7904 return cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7907 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7908 preserve location information even for tree nodes that don't
7912 build_c_cast (location_t loc
, tree type
, cp_expr expr
)
7914 cp_expr result
= cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7915 result
.set_location (loc
);
7919 /* Build an expression representing an explicit C-style cast to type
7920 TYPE of expression EXPR. */
7923 cp_build_c_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7929 if (type
== error_mark_node
|| error_operand_p (expr
))
7930 return error_mark_node
;
7932 if (processing_template_decl
)
7934 tree t
= build_min (CAST_EXPR
, type
,
7935 tree_cons (NULL_TREE
, value
, NULL_TREE
));
7936 /* We don't know if it will or will not have side effects. */
7937 TREE_SIDE_EFFECTS (t
) = 1;
7938 return convert_from_reference (t
);
7941 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7942 'Class') should always be retained, because this information aids
7943 in method lookup. */
7944 if (objc_is_object_ptr (type
)
7945 && objc_is_object_ptr (TREE_TYPE (expr
)))
7946 return build_nop (type
, expr
);
7948 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7949 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7950 if (!TYPE_REF_P (type
)
7951 && TREE_CODE (value
) == NOP_EXPR
7952 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
7953 value
= TREE_OPERAND (value
, 0);
7955 if (TREE_CODE (type
) == ARRAY_TYPE
)
7957 /* Allow casting from T1* to T2[] because Cfront allows it.
7958 NIHCL uses it. It is not valid ISO C++ however. */
7959 if (TYPE_PTR_P (TREE_TYPE (expr
)))
7961 if (complain
& tf_error
)
7962 permerror (input_location
, "ISO C++ forbids casting to an array type %qT", type
);
7964 return error_mark_node
;
7965 type
= build_pointer_type (TREE_TYPE (type
));
7969 if (complain
& tf_error
)
7970 error ("ISO C++ forbids casting to an array type %qT", type
);
7971 return error_mark_node
;
7975 if (FUNC_OR_METHOD_TYPE_P (type
))
7977 if (complain
& tf_error
)
7978 error ("invalid cast to function type %qT", type
);
7979 return error_mark_node
;
7982 if (TYPE_PTR_P (type
)
7983 && TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
7984 /* Casting to an integer of smaller size is an error detected elsewhere. */
7985 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (value
))
7986 /* Don't warn about converting any constant. */
7987 && !TREE_CONSTANT (value
))
7988 warning_at (input_location
, OPT_Wint_to_pointer_cast
,
7989 "cast to pointer from integer of different size");
7991 /* A C-style cast can be a const_cast. */
7992 result
= build_const_cast_1 (type
, value
, complain
& tf_warning
,
7996 if (result
!= error_mark_node
)
7998 maybe_warn_about_useless_cast (type
, value
, complain
);
7999 maybe_warn_about_cast_ignoring_quals (type
, complain
);
8004 /* Or a static cast. */
8005 result
= build_static_cast_1 (type
, value
, /*c_cast_p=*/true,
8006 &valid_p
, complain
);
8007 /* Or a reinterpret_cast. */
8009 result
= build_reinterpret_cast_1 (type
, value
, /*c_cast_p=*/true,
8010 &valid_p
, complain
);
8011 /* The static_cast or reinterpret_cast may be followed by a
8014 /* A valid cast may result in errors if, for example, a
8015 conversion to an ambiguous base class is required. */
8016 && !error_operand_p (result
))
8020 maybe_warn_about_useless_cast (type
, value
, complain
);
8021 maybe_warn_about_cast_ignoring_quals (type
, complain
);
8023 /* Non-class rvalues always have cv-unqualified type. */
8024 if (!CLASS_TYPE_P (type
))
8025 type
= TYPE_MAIN_VARIANT (type
);
8026 result_type
= TREE_TYPE (result
);
8027 if (!CLASS_TYPE_P (result_type
) && !TYPE_REF_P (type
))
8028 result_type
= TYPE_MAIN_VARIANT (result_type
);
8029 /* If the type of RESULT does not match TYPE, perform a
8030 const_cast to make it match. If the static_cast or
8031 reinterpret_cast succeeded, we will differ by at most
8032 cv-qualification, so the follow-on const_cast is guaranteed
8034 if (!same_type_p (non_reference (type
), non_reference (result_type
)))
8036 result
= build_const_cast_1 (type
, result
, false, &valid_p
);
8037 gcc_assert (valid_p
);
8042 return error_mark_node
;
8045 /* For use from the C common bits. */
8047 build_modify_expr (location_t location
,
8048 tree lhs
, tree
/*lhs_origtype*/,
8049 enum tree_code modifycode
,
8050 location_t
/*rhs_location*/, tree rhs
,
8051 tree
/*rhs_origtype*/)
8053 return cp_build_modify_expr (location
, lhs
, modifycode
, rhs
,
8054 tf_warning_or_error
);
8057 /* Build an assignment expression of lvalue LHS from value RHS.
8058 MODIFYCODE is the code for a binary operator that we use
8059 to combine the old value of LHS with RHS to get the new value.
8060 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
8062 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
8065 cp_build_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
8066 tree rhs
, tsubst_flags_t complain
)
8068 lhs
= mark_lvalue_use_nonread (lhs
);
8070 tree result
= NULL_TREE
;
8072 tree lhstype
= TREE_TYPE (lhs
);
8074 tree olhstype
= lhstype
;
8075 bool plain_assign
= (modifycode
== NOP_EXPR
);
8076 bool compound_side_effects_p
= false;
8077 tree preeval
= NULL_TREE
;
8079 /* Avoid duplicate error messages from operands that had errors. */
8080 if (error_operand_p (lhs
) || error_operand_p (rhs
))
8081 return error_mark_node
;
8083 while (TREE_CODE (lhs
) == COMPOUND_EXPR
)
8085 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
8086 compound_side_effects_p
= true;
8087 lhs
= TREE_OPERAND (lhs
, 1);
8090 /* Handle control structure constructs used as "lvalues". Note that we
8091 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
8092 switch (TREE_CODE (lhs
))
8094 /* Handle --foo = 5; as these are valid constructs in C++. */
8095 case PREDECREMENT_EXPR
:
8096 case PREINCREMENT_EXPR
:
8097 if (compound_side_effects_p
)
8098 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
8099 lhs
= genericize_compound_lvalue (lhs
);
8101 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
8102 and looked through the COMPOUND_EXPRs, readd them now around
8103 the resulting lhs. */
8104 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
8106 lhs
= build2 (COMPOUND_EXPR
, lhstype
, TREE_OPERAND (olhs
, 0), lhs
);
8107 tree
*ptr
= &TREE_OPERAND (lhs
, 1);
8108 for (olhs
= TREE_OPERAND (olhs
, 1);
8109 TREE_CODE (olhs
) == COMPOUND_EXPR
;
8110 olhs
= TREE_OPERAND (olhs
, 1))
8112 *ptr
= build2 (COMPOUND_EXPR
, lhstype
,
8113 TREE_OPERAND (olhs
, 0), *ptr
);
8114 ptr
= &TREE_OPERAND (*ptr
, 1);
8120 if (compound_side_effects_p
)
8121 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
8122 lhs
= genericize_compound_lvalue (lhs
);
8123 goto maybe_add_compound
;
8127 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
8128 when neither operand has side-effects. */
8129 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8130 return error_mark_node
;
8132 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0))
8133 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 1)));
8135 lhs
= build3 (COND_EXPR
, TREE_TYPE (lhs
),
8136 build2 (TREE_CODE (lhs
) == MIN_EXPR
? LE_EXPR
: GE_EXPR
,
8138 TREE_OPERAND (lhs
, 0),
8139 TREE_OPERAND (lhs
, 1)),
8140 TREE_OPERAND (lhs
, 0),
8141 TREE_OPERAND (lhs
, 1));
8144 /* Handle (a ? b : c) used as an "lvalue". */
8147 /* Produce (a ? (b = rhs) : (c = rhs))
8148 except that the RHS goes through a save-expr
8149 so the code to compute it is only emitted once. */
8150 if (VOID_TYPE_P (TREE_TYPE (rhs
)))
8152 if (complain
& tf_error
)
8153 error ("void value not ignored as it ought to be");
8154 return error_mark_node
;
8157 rhs
= stabilize_expr (rhs
, &preeval
);
8159 /* Check this here to avoid odd errors when trying to convert
8160 a throw to the type of the COND_EXPR. */
8161 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8162 return error_mark_node
;
8164 tree op1
= cp_build_modify_expr (loc
, TREE_OPERAND (lhs
, 1),
8165 modifycode
, rhs
, complain
);
8166 /* When sanitizing undefined behavior, even when rhs doesn't need
8167 stabilization at this point, the sanitization might add extra
8168 SAVE_EXPRs in there and so make sure there is no tree sharing
8169 in the rhs, otherwise those SAVE_EXPRs will have initialization
8170 only in one of the two branches. */
8171 if (sanitize_flags_p (SANITIZE_UNDEFINED
8172 | SANITIZE_UNDEFINED_NONDEFAULT
))
8173 rhs
= unshare_expr (rhs
);
8174 tree op2
= cp_build_modify_expr (loc
, TREE_OPERAND (lhs
, 2),
8175 modifycode
, rhs
, complain
);
8176 tree cond
= build_conditional_expr (input_location
,
8177 TREE_OPERAND (lhs
, 0), op1
, op2
,
8180 if (cond
== error_mark_node
)
8182 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
8183 and looked through the COMPOUND_EXPRs, readd them now around
8184 the resulting cond before adding the preevaluated rhs. */
8185 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
8187 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
8188 TREE_OPERAND (olhs
, 0), cond
);
8189 tree
*ptr
= &TREE_OPERAND (cond
, 1);
8190 for (olhs
= TREE_OPERAND (olhs
, 1);
8191 TREE_CODE (olhs
) == COMPOUND_EXPR
;
8192 olhs
= TREE_OPERAND (olhs
, 1))
8194 *ptr
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
8195 TREE_OPERAND (olhs
, 0), *ptr
);
8196 ptr
= &TREE_OPERAND (*ptr
, 1);
8199 /* Make sure the code to compute the rhs comes out
8200 before the split. */
8210 if (modifycode
== INIT_EXPR
)
8212 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
8213 /* Do the default thing. */;
8214 else if (TREE_CODE (rhs
) == CONSTRUCTOR
)
8216 /* Compound literal. */
8217 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
8218 /* Call convert to generate an error; see PR 11063. */
8219 rhs
= convert (lhstype
, rhs
);
8220 result
= build2 (INIT_EXPR
, lhstype
, lhs
, rhs
);
8221 TREE_SIDE_EFFECTS (result
) = 1;
8224 else if (! MAYBE_CLASS_TYPE_P (lhstype
))
8225 /* Do the default thing. */;
8228 releasing_vec rhs_vec
= make_tree_vector_single (rhs
);
8229 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
8230 &rhs_vec
, lhstype
, LOOKUP_NORMAL
,
8232 if (result
== NULL_TREE
)
8233 return error_mark_node
;
8239 lhs
= require_complete_type_sfinae (lhs
, complain
);
8240 if (lhs
== error_mark_node
)
8241 return error_mark_node
;
8243 if (modifycode
== NOP_EXPR
)
8245 if (c_dialect_objc ())
8247 result
= objc_maybe_build_modify_expr (lhs
, rhs
);
8252 /* `operator=' is not an inheritable operator. */
8253 if (! MAYBE_CLASS_TYPE_P (lhstype
))
8254 /* Do the default thing. */;
8257 result
= build_new_op (input_location
, MODIFY_EXPR
,
8258 LOOKUP_NORMAL
, lhs
, rhs
,
8259 make_node (NOP_EXPR
), /*overload=*/NULL
,
8261 if (result
== NULL_TREE
)
8262 return error_mark_node
;
8269 tree init
= NULL_TREE
;
8271 /* A binary op has been requested. Combine the old LHS
8272 value with the RHS producing the value we should actually
8273 store into the LHS. */
8274 gcc_assert (!((TYPE_REF_P (lhstype
)
8275 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype
)))
8276 || MAYBE_CLASS_TYPE_P (lhstype
)));
8278 /* Preevaluate the RHS to make sure its evaluation is complete
8279 before the lvalue-to-rvalue conversion of the LHS:
8281 [expr.ass] With respect to an indeterminately-sequenced
8282 function call, the operation of a compound assignment is a
8283 single evaluation. [ Note: Therefore, a function call shall
8284 not intervene between the lvalue-to-rvalue conversion and the
8285 side effect associated with any single compound assignment
8286 operator. -- end note ] */
8287 lhs
= cp_stabilize_reference (lhs
);
8288 rhs
= decay_conversion (rhs
, complain
);
8289 if (rhs
== error_mark_node
)
8290 return error_mark_node
;
8291 rhs
= stabilize_expr (rhs
, &init
);
8292 newrhs
= cp_build_binary_op (loc
, modifycode
, lhs
, rhs
, complain
);
8293 if (newrhs
== error_mark_node
)
8295 if (complain
& tf_error
)
8296 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode
,
8297 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
8298 return error_mark_node
;
8302 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), init
, newrhs
);
8304 /* Now it looks like a plain assignment. */
8305 modifycode
= NOP_EXPR
;
8306 if (c_dialect_objc ())
8308 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
8313 gcc_assert (!TYPE_REF_P (lhstype
));
8314 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs
)));
8317 /* The left-hand side must be an lvalue. */
8318 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8319 return error_mark_node
;
8321 /* Warn about modifying something that is `const'. Don't warn if
8322 this is initialization. */
8323 if (modifycode
!= INIT_EXPR
8324 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
8325 /* Functions are not modifiable, even though they are
8327 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs
))
8328 /* If it's an aggregate and any field is const, then it is
8329 effectively const. */
8330 || (CLASS_TYPE_P (lhstype
)
8331 && C_TYPE_FIELDS_READONLY (lhstype
))))
8333 if (complain
& tf_error
)
8334 cxx_readonly_error (loc
, lhs
, lv_assign
);
8335 return error_mark_node
;
8338 /* If storing into a structure or union member, it may have been given a
8339 lowered bitfield type. We need to convert to the declared type first,
8340 so retrieve it now. */
8342 olhstype
= unlowered_expr_type (lhs
);
8344 /* Convert new value to destination type. */
8346 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
8350 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs
))
8352 if (modifycode
!= INIT_EXPR
)
8354 if (complain
& tf_error
)
8355 error ("assigning to an array from an initializer list");
8356 return error_mark_node
;
8358 if (check_array_initializer (lhs
, lhstype
, newrhs
))
8359 return error_mark_node
;
8360 newrhs
= digest_init (lhstype
, newrhs
, complain
);
8361 if (newrhs
== error_mark_node
)
8362 return error_mark_node
;
8365 /* C++11 8.5/17: "If the destination type is an array of characters,
8366 an array of char16_t, an array of char32_t, or an array of wchar_t,
8367 and the initializer is a string literal...". */
8368 else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs
))
8370 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype
)))
8371 && modifycode
== INIT_EXPR
)
8373 newrhs
= digest_init (lhstype
, newrhs
, complain
);
8374 if (newrhs
== error_mark_node
)
8375 return error_mark_node
;
8378 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
8379 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))))
8381 if (complain
& tf_error
)
8382 error ("incompatible types in assignment of %qT to %qT",
8383 TREE_TYPE (rhs
), lhstype
);
8384 return error_mark_node
;
8387 /* Allow array assignment in compiler-generated code. */
8388 else if (!current_function_decl
8389 || !DECL_DEFAULTED_FN (current_function_decl
))
8391 /* This routine is used for both initialization and assignment.
8392 Make sure the diagnostic message differentiates the context. */
8393 if (complain
& tf_error
)
8395 if (modifycode
== INIT_EXPR
)
8396 error ("array used as initializer");
8398 error ("invalid array assignment");
8400 return error_mark_node
;
8403 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
8404 ? 1 + (modifycode
!= INIT_EXPR
): 0;
8405 result
= build_vec_init (lhs
, NULL_TREE
, newrhs
,
8406 /*explicit_value_init_p=*/false,
8407 from_array
, complain
);
8411 if (modifycode
== INIT_EXPR
)
8412 /* Calls with INIT_EXPR are all direct-initialization, so don't set
8413 LOOKUP_ONLYCONVERTING. */
8414 newrhs
= convert_for_initialization (lhs
, olhstype
, newrhs
, LOOKUP_NORMAL
,
8415 ICR_INIT
, NULL_TREE
, 0,
8418 newrhs
= convert_for_assignment (olhstype
, newrhs
, ICR_ASSIGN
,
8419 NULL_TREE
, 0, complain
, LOOKUP_IMPLICIT
);
8421 if (!same_type_p (lhstype
, olhstype
))
8422 newrhs
= cp_convert_and_check (lhstype
, newrhs
, complain
);
8424 if (modifycode
!= INIT_EXPR
)
8426 if (TREE_CODE (newrhs
) == CALL_EXPR
8427 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
8428 newrhs
= build_cplus_new (lhstype
, newrhs
, complain
);
8430 /* Can't initialize directly from a TARGET_EXPR, since that would
8431 cause the lhs to be constructed twice, and possibly result in
8432 accidental self-initialization. So we force the TARGET_EXPR to be
8433 expanded without a target. */
8434 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
8435 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
8436 TREE_OPERAND (newrhs
, 0));
8439 if (newrhs
== error_mark_node
)
8440 return error_mark_node
;
8442 if (c_dialect_objc () && flag_objc_gc
)
8444 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
8450 result
= build2 (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
8451 lhstype
, lhs
, newrhs
);
8453 TREE_SIDE_EFFECTS (result
) = 1;
8455 TREE_NO_WARNING (result
) = 1;
8459 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), preeval
, result
);
8464 build_x_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
8465 tree rhs
, tsubst_flags_t complain
)
8467 tree orig_lhs
= lhs
;
8468 tree orig_rhs
= rhs
;
8469 tree overload
= NULL_TREE
;
8470 tree op
= build_nt (modifycode
, NULL_TREE
, NULL_TREE
);
8472 if (processing_template_decl
)
8474 if (modifycode
== NOP_EXPR
8475 || type_dependent_expression_p (lhs
)
8476 || type_dependent_expression_p (rhs
))
8477 return build_min_nt_loc (loc
, MODOP_EXPR
, lhs
,
8478 build_min_nt_loc (loc
, modifycode
, NULL_TREE
,
8481 lhs
= build_non_dependent_expr (lhs
);
8482 rhs
= build_non_dependent_expr (rhs
);
8485 if (modifycode
!= NOP_EXPR
)
8487 tree rval
= build_new_op (loc
, MODIFY_EXPR
, LOOKUP_NORMAL
,
8488 lhs
, rhs
, op
, &overload
, complain
);
8491 if (rval
== error_mark_node
)
8493 TREE_NO_WARNING (rval
) = 1;
8494 if (processing_template_decl
)
8496 if (overload
!= NULL_TREE
)
8497 return (build_min_non_dep_op_overload
8498 (MODIFY_EXPR
, rval
, overload
, orig_lhs
, orig_rhs
));
8500 return (build_min_non_dep
8501 (MODOP_EXPR
, rval
, orig_lhs
, op
, orig_rhs
));
8506 return cp_build_modify_expr (loc
, lhs
, modifycode
, rhs
, complain
);
8509 /* Helper function for get_delta_difference which assumes FROM is a base
8510 class of TO. Returns a delta for the conversion of pointer-to-member
8511 of FROM to pointer-to-member of TO. If the conversion is invalid and
8512 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8513 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8514 If C_CAST_P is true, this conversion is taking place as part of a
8518 get_delta_difference_1 (tree from
, tree to
, bool c_cast_p
,
8519 tsubst_flags_t complain
)
8524 binfo
= lookup_base (to
, from
, c_cast_p
? ba_unique
: ba_check
,
8527 if (binfo
== error_mark_node
)
8529 if (!(complain
& tf_error
))
8530 return error_mark_node
;
8532 error (" in pointer to member function conversion");
8533 return size_zero_node
;
8537 if (kind
!= bk_via_virtual
)
8538 return BINFO_OFFSET (binfo
);
8540 /* FROM is a virtual base class of TO. Issue an error or warning
8541 depending on whether or not this is a reinterpret cast. */
8543 if (!(complain
& tf_error
))
8544 return error_mark_node
;
8546 error ("pointer to member conversion via virtual base %qT",
8547 BINFO_TYPE (binfo_from_vbase (binfo
)));
8549 return size_zero_node
;
8556 /* Get difference in deltas for different pointer to member function
8557 types. If the conversion is invalid and tf_error is not set in
8558 COMPLAIN, returns error_mark_node, otherwise returns an integer
8559 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8560 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8561 conversions as well. If C_CAST_P is true this conversion is taking
8562 place as part of a C-style cast.
8564 Note that the naming of FROM and TO is kind of backwards; the return
8565 value is what we add to a TO in order to get a FROM. They are named
8566 this way because we call this function to find out how to convert from
8567 a pointer to member of FROM to a pointer to member of TO. */
8570 get_delta_difference (tree from
, tree to
,
8571 bool allow_inverse_p
,
8572 bool c_cast_p
, tsubst_flags_t complain
)
8576 if (same_type_ignoring_top_level_qualifiers_p (from
, to
))
8577 /* Pointer to member of incomplete class is permitted*/
8578 result
= size_zero_node
;
8580 result
= get_delta_difference_1 (from
, to
, c_cast_p
, complain
);
8582 if (result
== error_mark_node
)
8583 return error_mark_node
;
8587 if (!allow_inverse_p
)
8589 if (!(complain
& tf_error
))
8590 return error_mark_node
;
8592 error_not_base_type (from
, to
);
8593 error (" in pointer to member conversion");
8594 result
= size_zero_node
;
8598 result
= get_delta_difference_1 (to
, from
, c_cast_p
, complain
);
8600 if (result
== error_mark_node
)
8601 return error_mark_node
;
8604 result
= size_diffop_loc (input_location
,
8605 size_zero_node
, result
);
8608 if (!(complain
& tf_error
))
8609 return error_mark_node
;
8611 error_not_base_type (from
, to
);
8612 error (" in pointer to member conversion");
8613 result
= size_zero_node
;
8618 return convert_to_integer (ptrdiff_type_node
, result
);
8621 /* Return a constructor for the pointer-to-member-function TYPE using
8622 the other components as specified. */
8625 build_ptrmemfunc1 (tree type
, tree delta
, tree pfn
)
8630 vec
<constructor_elt
, va_gc
> *v
;
8632 /* Pull the FIELD_DECLs out of the type. */
8633 pfn_field
= TYPE_FIELDS (type
);
8634 delta_field
= DECL_CHAIN (pfn_field
);
8636 /* Make sure DELTA has the type we want. */
8637 delta
= convert_and_check (input_location
, delta_type_node
, delta
);
8639 /* Convert to the correct target type if necessary. */
8640 pfn
= fold_convert (TREE_TYPE (pfn_field
), pfn
);
8642 /* Finish creating the initializer. */
8644 CONSTRUCTOR_APPEND_ELT(v
, pfn_field
, pfn
);
8645 CONSTRUCTOR_APPEND_ELT(v
, delta_field
, delta
);
8646 u
= build_constructor (type
, v
);
8647 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) & TREE_CONSTANT (delta
);
8648 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
8649 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
8651 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
8656 /* Build a constructor for a pointer to member function. It can be
8657 used to initialize global variables, local variable, or used
8658 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8661 If FORCE is nonzero, then force this conversion, even if
8662 we would rather not do it. Usually set when using an explicit
8663 cast. A C-style cast is being processed iff C_CAST_P is true.
8665 Return error_mark_node, if something goes wrong. */
8668 build_ptrmemfunc (tree type
, tree pfn
, int force
, bool c_cast_p
,
8669 tsubst_flags_t complain
)
8675 if (error_operand_p (pfn
))
8676 return error_mark_node
;
8678 pfn_type
= TREE_TYPE (pfn
);
8679 to_type
= build_ptrmemfunc_type (type
);
8681 /* Handle multiple conversions of pointer to member functions. */
8682 if (TYPE_PTRMEMFUNC_P (pfn_type
))
8684 tree delta
= NULL_TREE
;
8685 tree npfn
= NULL_TREE
;
8689 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
,
8690 LOOKUP_NORMAL
, complain
))
8692 if (complain
& tf_error
)
8693 error ("invalid conversion to type %qT from type %qT",
8696 return error_mark_node
;
8699 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
8700 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
8702 c_cast_p
, complain
);
8703 if (n
== error_mark_node
)
8704 return error_mark_node
;
8706 /* We don't have to do any conversion to convert a
8707 pointer-to-member to its own type. But, we don't want to
8708 just return a PTRMEM_CST if there's an explicit cast; that
8709 cast should make the expression an invalid template argument. */
8710 if (TREE_CODE (pfn
) != PTRMEM_CST
)
8712 if (same_type_p (to_type
, pfn_type
))
8714 else if (integer_zerop (n
) && TREE_CODE (pfn
) != CONSTRUCTOR
)
8715 return build_reinterpret_cast (to_type
, pfn
,
8719 if (TREE_SIDE_EFFECTS (pfn
))
8720 pfn
= save_expr (pfn
);
8722 /* Obtain the function pointer and the current DELTA. */
8723 if (TREE_CODE (pfn
) == PTRMEM_CST
)
8724 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
8727 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
8728 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
8731 /* Just adjust the DELTA field. */
8732 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8733 (TREE_TYPE (delta
), ptrdiff_type_node
));
8734 if (!integer_zerop (n
))
8736 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
8737 n
= cp_build_binary_op (input_location
,
8738 LSHIFT_EXPR
, n
, integer_one_node
,
8740 delta
= cp_build_binary_op (input_location
,
8741 PLUS_EXPR
, delta
, n
, complain
);
8743 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
8746 /* Handle null pointer to member function conversions. */
8747 if (null_ptr_cst_p (pfn
))
8749 pfn
= cp_build_c_cast (type
, pfn
, complain
);
8750 return build_ptrmemfunc1 (to_type
,
8755 if (type_unknown_p (pfn
))
8756 return instantiate_type (type
, pfn
, complain
);
8758 fn
= TREE_OPERAND (pfn
, 0);
8759 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
8760 /* In a template, we will have preserved the
8762 || (processing_template_decl
&& TREE_CODE (fn
) == OFFSET_REF
));
8763 return make_ptrmem_cst (to_type
, fn
);
8766 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8769 ??? There is no consistency as to the types returned for the above
8770 values. Some code acts as if it were a sizetype and some as if it were
8771 integer_type_node. */
8774 expand_ptrmemfunc_cst (tree cst
, tree
*delta
, tree
*pfn
)
8776 tree type
= TREE_TYPE (cst
);
8777 tree fn
= PTRMEM_CST_MEMBER (cst
);
8778 tree ptr_class
, fn_class
;
8780 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
8782 /* The class that the function belongs to. */
8783 fn_class
= DECL_CONTEXT (fn
);
8785 /* The class that we're creating a pointer to member of. */
8786 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
8788 /* First, calculate the adjustment to the function's class. */
8789 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0,
8790 /*c_cast_p=*/0, tf_warning_or_error
);
8792 if (!DECL_VIRTUAL_P (fn
))
8793 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
),
8794 build_addr_func (fn
, tf_warning_or_error
));
8797 /* If we're dealing with a virtual function, we have to adjust 'this'
8798 again, to point to the base which provides the vtable entry for
8799 fn; the call will do the opposite adjustment. */
8800 tree orig_class
= DECL_CONTEXT (fn
);
8801 tree binfo
= binfo_or_else (orig_class
, fn_class
);
8802 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
8803 *delta
, BINFO_OFFSET (binfo
));
8805 /* We set PFN to the vtable offset at which the function can be
8806 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8807 case delta is shifted left, and then incremented). */
8808 *pfn
= DECL_VINDEX (fn
);
8809 *pfn
= fold_build2 (MULT_EXPR
, integer_type_node
, *pfn
,
8810 TYPE_SIZE_UNIT (vtable_entry_type
));
8812 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
8814 case ptrmemfunc_vbit_in_pfn
:
8815 *pfn
= fold_build2 (PLUS_EXPR
, integer_type_node
, *pfn
,
8819 case ptrmemfunc_vbit_in_delta
:
8820 *delta
= fold_build2 (LSHIFT_EXPR
, TREE_TYPE (*delta
),
8821 *delta
, integer_one_node
);
8822 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
8823 *delta
, integer_one_node
);
8830 *pfn
= fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), *pfn
);
8834 /* Return an expression for PFN from the pointer-to-member function
8838 pfn_from_ptrmemfunc (tree t
)
8840 if (TREE_CODE (t
) == PTRMEM_CST
)
8845 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
8850 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
8853 /* Return an expression for DELTA from the pointer-to-member function
8857 delta_from_ptrmemfunc (tree t
)
8859 if (TREE_CODE (t
) == PTRMEM_CST
)
8864 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
8869 return build_ptrmemfunc_access_expr (t
, delta_identifier
);
8872 /* Convert value RHS to type TYPE as preparation for an assignment to
8873 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8874 implicit conversion is. If FNDECL is non-NULL, we are doing the
8875 conversion in order to pass the PARMNUMth argument of FNDECL.
8876 If FNDECL is NULL, we are doing the conversion in function pointer
8877 argument passing, conversion in initialization, etc. */
8880 convert_for_assignment (tree type
, tree rhs
,
8881 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
8882 tsubst_flags_t complain
, int flags
)
8885 enum tree_code coder
;
8887 location_t rhs_loc
= EXPR_LOC_OR_LOC (rhs
, input_location
);
8888 bool has_loc
= EXPR_LOCATION (rhs
) != UNKNOWN_LOCATION
;
8889 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
8890 but preserve location wrappers. */
8891 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
8892 && !location_wrapper_p (rhs
))
8893 rhs
= TREE_OPERAND (rhs
, 0);
8895 /* Handle [dcl.init.list] direct-list-initialization from
8896 single element of enumeration with a fixed underlying type. */
8897 if (is_direct_enum_init (type
, rhs
))
8899 tree elt
= CONSTRUCTOR_ELT (rhs
, 0)->value
;
8900 if (check_narrowing (ENUM_UNDERLYING_TYPE (type
), elt
, complain
))
8902 warning_sentinel
w (warn_useless_cast
);
8903 warning_sentinel
w2 (warn_ignored_qualifiers
);
8904 rhs
= cp_build_c_cast (type
, elt
, complain
);
8907 rhs
= error_mark_node
;
8910 rhstype
= TREE_TYPE (rhs
);
8911 coder
= TREE_CODE (rhstype
);
8913 if (VECTOR_TYPE_P (type
) && coder
== VECTOR_TYPE
8914 && vector_types_convertible_p (type
, rhstype
, true))
8916 rhs
= mark_rvalue_use (rhs
);
8917 return convert (type
, rhs
);
8920 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
8921 return error_mark_node
;
8922 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
8923 return error_mark_node
;
8925 /* The RHS of an assignment cannot have void type. */
8926 if (coder
== VOID_TYPE
)
8928 if (complain
& tf_error
)
8929 error_at (rhs_loc
, "void value not ignored as it ought to be");
8930 return error_mark_node
;
8933 if (c_dialect_objc ())
8937 tree rname
= fndecl
;
8948 selector
= objc_message_selector ();
8950 if (selector
&& parmno
> 1)
8957 if (objc_compare_types (type
, rhstype
, parmno
, rname
))
8959 rhs
= mark_rvalue_use (rhs
);
8960 return convert (type
, rhs
);
8966 The expression is implicitly converted (clause _conv_) to the
8967 cv-unqualified type of the left operand.
8969 We allow bad conversions here because by the time we get to this point
8970 we are committed to doing the conversion. If we end up doing a bad
8971 conversion, convert_like will complain. */
8972 if (!can_convert_arg_bad (type
, rhstype
, rhs
, flags
, complain
))
8974 /* When -Wno-pmf-conversions is use, we just silently allow
8975 conversions from pointers-to-members to plain pointers. If
8976 the conversion doesn't work, cp_convert will complain. */
8978 && TYPE_PTR_P (type
)
8979 && TYPE_PTRMEMFUNC_P (rhstype
))
8980 rhs
= cp_convert (strip_top_quals (type
), rhs
, complain
);
8983 if (complain
& tf_error
)
8985 /* If the right-hand side has unknown type, then it is an
8986 overloaded function. Call instantiate_type to get error
8988 if (rhstype
== unknown_type_node
)
8990 tree r
= instantiate_type (type
, rhs
, tf_warning_or_error
);
8991 /* -fpermissive might allow this; recurse. */
8993 return convert_for_assignment (type
, r
, errtype
, fndecl
,
8994 parmnum
, complain
, flags
);
8997 complain_about_bad_argument (rhs_loc
,
9002 range_label_for_type_mismatch
label (rhstype
, type
);
9003 gcc_rich_location
richloc (rhs_loc
, has_loc
? &label
: NULL
);
9006 case ICR_DEFAULT_ARGUMENT
:
9008 "cannot convert %qH to %qI in default argument",
9013 "cannot convert %qH to %qI in argument passing",
9016 case ICR_CONVERTING
:
9017 error_at (&richloc
, "cannot convert %qH to %qI",
9022 "cannot convert %qH to %qI in initialization",
9026 error_at (&richloc
, "cannot convert %qH to %qI in return",
9031 "cannot convert %qH to %qI in assignment",
9038 if (TYPE_PTR_P (rhstype
)
9039 && TYPE_PTR_P (type
)
9040 && CLASS_TYPE_P (TREE_TYPE (rhstype
))
9041 && CLASS_TYPE_P (TREE_TYPE (type
))
9042 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype
)))
9043 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
9044 (TREE_TYPE (rhstype
))),
9045 "class type %qT is incomplete", TREE_TYPE (rhstype
));
9047 return error_mark_node
;
9050 if (warn_suggest_attribute_format
)
9052 const enum tree_code codel
= TREE_CODE (type
);
9053 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
9055 && check_missing_format_attribute (type
, rhstype
)
9056 && (complain
& tf_warning
))
9060 case ICR_DEFAULT_ARGUMENT
:
9062 warning (OPT_Wsuggest_attribute_format
,
9063 "parameter %qP of %qD might be a candidate "
9064 "for a format attribute", parmnum
, fndecl
);
9066 warning (OPT_Wsuggest_attribute_format
,
9067 "parameter might be a candidate "
9068 "for a format attribute");
9070 case ICR_CONVERTING
:
9071 warning (OPT_Wsuggest_attribute_format
,
9072 "target of conversion might be a candidate "
9073 "for a format attribute");
9076 warning (OPT_Wsuggest_attribute_format
,
9077 "target of initialization might be a candidate "
9078 "for a format attribute");
9081 warning (OPT_Wsuggest_attribute_format
,
9082 "return type might be a candidate "
9083 "for a format attribute");
9086 warning (OPT_Wsuggest_attribute_format
,
9087 "left-hand side of assignment might be a candidate "
9088 "for a format attribute");
9095 /* If -Wparentheses, warn about a = b = c when a has type bool and b
9097 if (warn_parentheses
9098 && TREE_CODE (type
) == BOOLEAN_TYPE
9099 && TREE_CODE (rhs
) == MODIFY_EXPR
9100 && !TREE_NO_WARNING (rhs
)
9101 && TREE_CODE (TREE_TYPE (rhs
)) != BOOLEAN_TYPE
9102 && (complain
& tf_warning
)
9103 && warning_at (rhs_loc
, OPT_Wparentheses
,
9104 "suggest parentheses around assignment used as "
9106 TREE_NO_WARNING (rhs
) = 1;
9108 if (complain
& tf_warning
)
9109 warn_for_address_or_pointer_of_packed_member (type
, rhs
);
9111 return perform_implicit_conversion_flags (strip_top_quals (type
), rhs
,
9115 /* Convert RHS to be of type TYPE.
9116 If EXP is nonzero, it is the target of the initialization.
9117 ERRTYPE indicates what kind of error the implicit conversion is.
9119 Two major differences between the behavior of
9120 `convert_for_assignment' and `convert_for_initialization'
9121 are that references are bashed in the former, while
9122 copied in the latter, and aggregates are assigned in
9123 the former (operator=) while initialized in the
9126 If using constructor make sure no conversion operator exists, if one does
9127 exist, an ambiguity exists. */
9130 convert_for_initialization (tree exp
, tree type
, tree rhs
, int flags
,
9131 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
9132 tsubst_flags_t complain
)
9134 enum tree_code codel
= TREE_CODE (type
);
9136 enum tree_code coder
;
9138 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9139 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
9140 if (TREE_CODE (rhs
) == NOP_EXPR
9141 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
9142 && codel
!= REFERENCE_TYPE
)
9143 rhs
= TREE_OPERAND (rhs
, 0);
9145 if (type
== error_mark_node
9146 || rhs
== error_mark_node
9147 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
9148 return error_mark_node
;
9150 if (MAYBE_CLASS_TYPE_P (non_reference (type
)))
9152 else if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
9153 && TREE_CODE (type
) != ARRAY_TYPE
9154 && (!TYPE_REF_P (type
)
9155 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
9156 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
9157 && !TYPE_REFFN_P (type
))
9158 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
9159 rhs
= decay_conversion (rhs
, complain
);
9161 rhstype
= TREE_TYPE (rhs
);
9162 coder
= TREE_CODE (rhstype
);
9164 if (coder
== ERROR_MARK
)
9165 return error_mark_node
;
9167 /* We accept references to incomplete types, so we can
9168 return here before checking if RHS is of complete type. */
9170 if (codel
== REFERENCE_TYPE
)
9172 auto_diagnostic_group d
;
9173 /* This should eventually happen in convert_arguments. */
9174 int savew
= 0, savee
= 0;
9177 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
9178 rhs
= initialize_reference (type
, rhs
, flags
, complain
);
9181 && (warningcount
+ werrorcount
> savew
|| errorcount
> savee
))
9182 inform (get_fndecl_argument_location (fndecl
, parmnum
),
9183 "in passing argument %P of %qD", parmnum
, fndecl
);
9188 exp
= require_complete_type_sfinae (exp
, complain
);
9189 if (exp
== error_mark_node
)
9190 return error_mark_node
;
9192 rhstype
= non_reference (rhstype
);
9194 type
= complete_type (type
);
9196 if (DIRECT_INIT_EXPR_P (type
, rhs
))
9197 /* Don't try to do copy-initialization if we already have
9198 direct-initialization. */
9201 if (MAYBE_CLASS_TYPE_P (type
))
9202 return perform_implicit_conversion_flags (type
, rhs
, complain
, flags
);
9204 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
,
9208 /* If RETVAL is the address of, or a reference to, a local variable or
9209 temporary give an appropriate warning and return true. */
9212 maybe_warn_about_returning_address_of_local (tree retval
)
9214 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
9215 tree whats_returned
= fold_for_warn (retval
);
9216 location_t loc
= cp_expr_loc_or_loc (retval
, input_location
);
9220 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
9221 whats_returned
= TREE_OPERAND (whats_returned
, 1);
9222 else if (CONVERT_EXPR_P (whats_returned
)
9223 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
)
9224 whats_returned
= TREE_OPERAND (whats_returned
, 0);
9229 if (TREE_CODE (whats_returned
) == TARGET_EXPR
9230 && is_std_init_list (TREE_TYPE (whats_returned
)))
9232 tree init
= TARGET_EXPR_INITIAL (whats_returned
);
9233 if (TREE_CODE (init
) == CONSTRUCTOR
)
9234 /* Pull out the array address. */
9235 whats_returned
= CONSTRUCTOR_ELT (init
, 0)->value
;
9236 else if (TREE_CODE (init
) == INDIRECT_REF
)
9237 /* The source of a trivial copy looks like *(T*)&var. */
9238 whats_returned
= TREE_OPERAND (init
, 0);
9241 STRIP_NOPS (whats_returned
);
9244 /* As a special case, we handle a call to std::move or std::forward. */
9245 if (TREE_CODE (whats_returned
) == CALL_EXPR
9246 && (is_std_move_p (whats_returned
)
9247 || is_std_forward_p (whats_returned
)))
9249 tree arg
= CALL_EXPR_ARG (whats_returned
, 0);
9250 return maybe_warn_about_returning_address_of_local (arg
);
9253 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
9255 whats_returned
= TREE_OPERAND (whats_returned
, 0);
9257 while (TREE_CODE (whats_returned
) == COMPONENT_REF
9258 || TREE_CODE (whats_returned
) == ARRAY_REF
)
9259 whats_returned
= TREE_OPERAND (whats_returned
, 0);
9261 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
9262 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
9264 if (TYPE_REF_P (valtype
))
9265 warning_at (loc
, OPT_Wreturn_local_addr
,
9266 "returning reference to temporary");
9267 else if (is_std_init_list (valtype
))
9268 warning_at (loc
, OPT_Winit_list_lifetime
,
9269 "returning temporary %<initializer_list%> does not extend "
9270 "the lifetime of the underlying array");
9274 STRIP_ANY_LOCATION_WRAPPER (whats_returned
);
9276 if (DECL_P (whats_returned
)
9277 && DECL_NAME (whats_returned
)
9278 && DECL_FUNCTION_SCOPE_P (whats_returned
)
9279 && !is_capture_proxy (whats_returned
)
9280 && !(TREE_STATIC (whats_returned
)
9281 || TREE_PUBLIC (whats_returned
)))
9283 if (VAR_P (whats_returned
)
9284 && DECL_DECOMPOSITION_P (whats_returned
)
9285 && DECL_DECOMP_BASE (whats_returned
)
9286 && DECL_HAS_VALUE_EXPR_P (whats_returned
))
9288 /* When returning address of a structured binding, if the structured
9289 binding is not a reference, continue normally, if it is a
9290 reference, recurse on the initializer of the structured
9292 tree base
= DECL_DECOMP_BASE (whats_returned
);
9293 if (TYPE_REF_P (TREE_TYPE (base
)))
9295 tree init
= DECL_INITIAL (base
);
9296 return maybe_warn_about_returning_address_of_local (init
);
9300 auto_diagnostic_group d
;
9301 if (TYPE_REF_P (valtype
))
9302 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
9303 "reference to local variable %qD returned",
9305 else if (is_std_init_list (valtype
))
9306 w
= warning_at (loc
, OPT_Winit_list_lifetime
,
9307 "returning local %<initializer_list%> variable %qD "
9308 "does not extend the lifetime of the underlying array",
9310 else if (POINTER_TYPE_P (valtype
)
9311 && TREE_CODE (whats_returned
) == LABEL_DECL
)
9312 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
9313 "address of label %qD returned",
9315 else if (POINTER_TYPE_P (valtype
))
9316 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
9317 "address of local variable %qD returned",
9320 inform (DECL_SOURCE_LOCATION (whats_returned
),
9328 /* Returns true if DECL is in the std namespace. */
9331 decl_in_std_namespace_p (tree decl
)
9333 return (decl
!= NULL_TREE
9334 && DECL_NAMESPACE_STD_P (decl_namespace_context (decl
)));
9337 /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
9340 is_std_forward_p (tree fn
)
9342 /* std::forward only takes one argument. */
9343 if (call_expr_nargs (fn
) != 1)
9346 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
9347 if (!decl_in_std_namespace_p (fndecl
))
9350 tree name
= DECL_NAME (fndecl
);
9351 return name
&& id_equal (name
, "forward");
9354 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
9357 is_std_move_p (tree fn
)
9359 /* std::move only takes one argument. */
9360 if (call_expr_nargs (fn
) != 1)
9363 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
9364 if (!decl_in_std_namespace_p (fndecl
))
9367 tree name
= DECL_NAME (fndecl
);
9368 return name
&& id_equal (name
, "move");
9371 /* Returns true if RETVAL is a good candidate for the NRVO as per
9372 [class.copy.elision]. FUNCTYPE is the type the function is declared
9376 can_do_nrvo_p (tree retval
, tree functype
)
9378 if (functype
== error_mark_node
)
9381 STRIP_ANY_LOCATION_WRAPPER (retval
);
9382 tree result
= DECL_RESULT (current_function_decl
);
9383 return (retval
!= NULL_TREE
9384 && !processing_template_decl
9385 /* Must be a local, automatic variable. */
9387 && DECL_CONTEXT (retval
) == current_function_decl
9388 && !TREE_STATIC (retval
)
9389 /* And not a lambda or anonymous union proxy. */
9390 && !DECL_HAS_VALUE_EXPR_P (retval
)
9391 && (DECL_ALIGN (retval
) <= DECL_ALIGN (result
))
9392 /* The cv-unqualified type of the returned value must be the
9393 same as the cv-unqualified return type of the
9395 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval
))),
9396 (TYPE_MAIN_VARIANT (functype
)))
9397 /* And the returned value must be non-volatile. */
9398 && !TYPE_VOLATILE (TREE_TYPE (retval
)));
9401 /* Returns true if we should treat RETVAL, an expression being returned,
9402 as if it were designated by an rvalue. See [class.copy.elision].
9403 PARM_P is true if a function parameter is OK in this context. */
9406 treat_lvalue_as_rvalue_p (tree retval
, bool parm_ok
)
9408 STRIP_ANY_LOCATION_WRAPPER (retval
);
9409 return ((cxx_dialect
!= cxx98
)
9410 && ((VAR_P (retval
) && !DECL_HAS_VALUE_EXPR_P (retval
))
9411 || (parm_ok
&& TREE_CODE (retval
) == PARM_DECL
))
9412 && DECL_CONTEXT (retval
) == current_function_decl
9413 && !TREE_STATIC (retval
));
9416 /* Warn about wrong usage of std::move in a return statement. RETVAL
9417 is the expression we are returning; FUNCTYPE is the type the function
9418 is declared to return. */
9421 maybe_warn_pessimizing_move (tree retval
, tree functype
)
9423 if (!(warn_pessimizing_move
|| warn_redundant_move
))
9426 location_t loc
= cp_expr_loc_or_loc (retval
, input_location
);
9428 /* C++98 doesn't know move. */
9429 if (cxx_dialect
< cxx11
)
9432 /* Wait until instantiation time, since we can't gauge if we should do
9433 the NRVO until then. */
9434 if (processing_template_decl
)
9437 /* This is only interesting for class types. */
9438 if (!CLASS_TYPE_P (functype
))
9441 /* We're looking for *std::move<T&> ((T &) &arg). */
9442 if (REFERENCE_REF_P (retval
)
9443 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
9445 tree fn
= TREE_OPERAND (retval
, 0);
9446 if (is_std_move_p (fn
))
9448 tree arg
= CALL_EXPR_ARG (fn
, 0);
9449 if (TREE_CODE (arg
) != NOP_EXPR
)
9451 arg
= TREE_OPERAND (arg
, 0);
9452 if (TREE_CODE (arg
) != ADDR_EXPR
)
9454 arg
= TREE_OPERAND (arg
, 0);
9455 arg
= convert_from_reference (arg
);
9456 /* Warn if we could do copy elision were it not for the move. */
9457 if (can_do_nrvo_p (arg
, functype
))
9459 auto_diagnostic_group d
;
9460 if (warning_at (loc
, OPT_Wpessimizing_move
,
9461 "moving a local object in a return statement "
9462 "prevents copy elision"))
9463 inform (loc
, "remove %<std::move%> call");
9465 /* Warn if the move is redundant. It is redundant when we would
9466 do maybe-rvalue overload resolution even without std::move. */
9467 else if (warn_redundant_move
9468 && treat_lvalue_as_rvalue_p (arg
, /*parm_ok*/true))
9470 /* Make sure that the overload resolution would actually succeed
9471 if we removed the std::move call. */
9472 tree t
= convert_for_initialization (NULL_TREE
, functype
,
9475 | LOOKUP_ONLYCONVERTING
9476 | LOOKUP_PREFER_RVALUE
),
9477 ICR_RETURN
, NULL_TREE
, 0,
9479 /* If this worked, implicit rvalue would work, so the call to
9480 std::move is redundant. */
9481 if (t
!= error_mark_node
)
9483 auto_diagnostic_group d
;
9484 if (warning_at (loc
, OPT_Wredundant_move
,
9485 "redundant move in return statement"))
9486 inform (loc
, "remove %<std::move%> call");
9493 /* Check that returning RETVAL from the current function is valid.
9494 Return an expression explicitly showing all conversions required to
9495 change RETVAL into the function return type, and to assign it to
9496 the DECL_RESULT for the function. Set *NO_WARNING to true if
9497 code reaches end of non-void function warning shouldn't be issued
9498 on this RETURN_EXPR. */
9501 check_return_expr (tree retval
, bool *no_warning
)
9504 /* The type actually returned by the function. */
9506 /* The type the function is declared to return, or void if
9507 the declared type is incomplete. */
9509 int fn_returns_value_p
;
9511 *no_warning
= false;
9513 /* A `volatile' function is one that isn't supposed to return, ever.
9514 (This is a G++ extension, used to get better code for functions
9515 that call the `volatile' function.) */
9516 if (TREE_THIS_VOLATILE (current_function_decl
))
9517 warning (0, "function declared %<noreturn%> has a %<return%> statement");
9519 /* Check for various simple errors. */
9520 if (DECL_DESTRUCTOR_P (current_function_decl
))
9523 error ("returning a value from a destructor");
9526 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
9528 if (in_function_try_handler
)
9529 /* If a return statement appears in a handler of the
9530 function-try-block of a constructor, the program is ill-formed. */
9531 error ("cannot return from a handler of a function-try-block of a constructor");
9533 /* You can't return a value from a constructor. */
9534 error ("returning a value from a constructor");
9538 const tree saved_retval
= retval
;
9540 if (processing_template_decl
)
9542 current_function_returns_value
= 1;
9544 if (check_for_bare_parameter_packs (retval
))
9545 return error_mark_node
;
9547 /* If one of the types might be void, we can't tell whether we're
9548 returning a value. */
9549 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl
)))
9550 && !FNDECL_USED_AUTO (current_function_decl
))
9551 || (retval
!= NULL_TREE
9552 && (TREE_TYPE (retval
) == NULL_TREE
9553 || WILDCARD_TYPE_P (TREE_TYPE (retval
)))))
9557 functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
9559 /* Deduce auto return type from a return statement. */
9560 if (FNDECL_USED_AUTO (current_function_decl
))
9562 tree pattern
= DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
);
9566 if (!retval
&& !is_auto (pattern
))
9568 /* Give a helpful error message. */
9569 error ("return-statement with no value, in function returning %qT",
9571 inform (input_location
, "only plain %<auto%> return type can be "
9572 "deduced to %<void%>");
9573 type
= error_mark_node
;
9575 else if (retval
&& BRACE_ENCLOSED_INITIALIZER_P (retval
))
9577 error ("returning initializer list");
9578 type
= error_mark_node
;
9584 auto_node
= type_uses_auto (pattern
);
9585 type
= do_auto_deduction (pattern
, retval
, auto_node
);
9588 if (type
== error_mark_node
)
9590 else if (functype
== pattern
)
9591 apply_deduced_return_type (current_function_decl
, type
);
9592 else if (!same_type_p (type
, functype
))
9594 if (LAMBDA_FUNCTION_P (current_function_decl
))
9595 error ("inconsistent types %qT and %qT deduced for "
9596 "lambda return type", functype
, type
);
9598 error ("inconsistent deduction for auto return type: "
9599 "%qT and then %qT", functype
, type
);
9604 result
= DECL_RESULT (current_function_decl
);
9605 valtype
= TREE_TYPE (result
);
9606 gcc_assert (valtype
!= NULL_TREE
);
9607 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
9609 /* Check for a return statement with no return value in a function
9610 that's supposed to return a value. */
9611 if (!retval
&& fn_returns_value_p
)
9613 if (functype
!= error_mark_node
)
9614 permerror (input_location
, "return-statement with no value, in "
9615 "function returning %qT", valtype
);
9616 /* Remember that this function did return. */
9617 current_function_returns_value
= 1;
9618 /* And signal caller that TREE_NO_WARNING should be set on the
9619 RETURN_EXPR to avoid control reaches end of non-void function
9620 warnings in tree-cfg.c. */
9623 /* Check for a return statement with a value in a function that
9624 isn't supposed to return a value. */
9625 else if (retval
&& !fn_returns_value_p
)
9627 if (VOID_TYPE_P (TREE_TYPE (retval
)))
9628 /* You can return a `void' value from a function of `void'
9629 type. In that case, we have to evaluate the expression for
9630 its side-effects. */
9631 finish_expr_stmt (retval
);
9633 permerror (input_location
,
9634 "return-statement with a value, in function "
9635 "returning %qT", valtype
);
9636 current_function_returns_null
= 1;
9638 /* There's really no value to return, after all. */
9642 /* Remember that this function can sometimes return without a
9644 current_function_returns_null
= 1;
9646 /* Remember that this function did return a value. */
9647 current_function_returns_value
= 1;
9649 /* Check for erroneous operands -- but after giving ourselves a
9650 chance to provide an error about returning a value from a void
9652 if (error_operand_p (retval
))
9654 current_function_return_value
= error_mark_node
;
9655 return error_mark_node
;
9658 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
9659 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl
))
9660 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
9662 && retval
&& null_ptr_cst_p (retval
))
9663 warning (0, "%<operator new%> must not return NULL unless it is "
9664 "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
9666 /* Effective C++ rule 15. See also start_function. */
9668 && DECL_NAME (current_function_decl
) == assign_op_identifier
9669 && !type_dependent_expression_p (retval
))
9673 /* The function return type must be a reference to the current
9675 if (TYPE_REF_P (valtype
)
9676 && same_type_ignoring_top_level_qualifiers_p
9677 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
)))
9679 /* Returning '*this' is obviously OK. */
9680 if (retval
== current_class_ref
)
9682 /* If we are calling a function whose return type is the same of
9683 the current class reference, it is ok. */
9684 else if (INDIRECT_REF_P (retval
)
9685 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
9690 warning (OPT_Weffc__
, "%<operator=%> should return a reference to %<*this%>");
9693 if (dependent_type_p (functype
)
9694 || type_dependent_expression_p (retval
))
9697 /* We should not have changed the return value. */
9698 gcc_assert (retval
== saved_retval
);
9702 /* The fabled Named Return Value optimization, as per [class.copy]/15:
9704 [...] For a function with a class return type, if the expression
9705 in the return statement is the name of a local object, and the cv-
9706 unqualified type of the local object is the same as the function
9707 return type, an implementation is permitted to omit creating the tem-
9708 porary object to hold the function return value [...]
9710 So, if this is a value-returning function that always returns the same
9711 local variable, remember it.
9713 It might be nice to be more flexible, and choose the first suitable
9714 variable even if the function sometimes returns something else, but
9715 then we run the risk of clobbering the variable we chose if the other
9716 returned expression uses the chosen variable somehow. And people expect
9717 this restriction, anyway. (jason 2000-11-19)
9719 See finish_function and finalize_nrv for the rest of this optimization. */
9721 STRIP_ANY_LOCATION_WRAPPER (retval
);
9723 bool named_return_value_okay_p
= can_do_nrvo_p (retval
, functype
);
9724 if (fn_returns_value_p
&& flag_elide_constructors
)
9726 if (named_return_value_okay_p
9727 && (current_function_return_value
== NULL_TREE
9728 || current_function_return_value
== retval
))
9729 current_function_return_value
= retval
;
9731 current_function_return_value
= error_mark_node
;
9734 /* We don't need to do any conversions when there's nothing being
9739 if (!named_return_value_okay_p
)
9740 maybe_warn_pessimizing_move (retval
, functype
);
9742 /* Do any required conversions. */
9743 if (retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
9744 /* No conversions are required. */
9748 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
9750 /* The functype's return type will have been set to void, if it
9751 was an incomplete type. Just treat this as 'return;' */
9752 if (VOID_TYPE_P (functype
))
9753 return error_mark_node
;
9755 /* If we had an id-expression obfuscated by force_paren_expr, we need
9756 to undo it so we can try to treat it as an rvalue below. */
9757 retval
= maybe_undo_parenthesized_ref (retval
);
9759 if (processing_template_decl
)
9760 retval
= build_non_dependent_expr (retval
);
9762 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9763 treated as an rvalue for the purposes of overload resolution to
9764 favor move constructors over copy constructors.
9766 Note that these conditions are similar to, but not as strict as,
9767 the conditions for the named return value optimization. */
9768 bool converted
= false;
9769 if (treat_lvalue_as_rvalue_p (retval
, /*parm_ok*/true)
9770 /* This is only interesting for class type. */
9771 && CLASS_TYPE_P (functype
))
9773 tree moved
= move (retval
);
9774 moved
= convert_for_initialization
9775 (NULL_TREE
, functype
, moved
, flags
|LOOKUP_PREFER_RVALUE
,
9776 ICR_RETURN
, NULL_TREE
, 0, tf_none
);
9777 if (moved
!= error_mark_node
)
9784 /* The call in a (lambda) thunk needs no conversions. */
9785 if (TREE_CODE (retval
) == CALL_EXPR
9786 && CALL_FROM_THUNK_P (retval
))
9789 /* First convert the value to the function's return type, then
9790 to the type of return value's location to handle the
9791 case that functype is smaller than the valtype. */
9793 retval
= convert_for_initialization
9794 (NULL_TREE
, functype
, retval
, flags
, ICR_RETURN
, NULL_TREE
, 0,
9795 tf_warning_or_error
);
9796 retval
= convert (valtype
, retval
);
9798 /* If the conversion failed, treat this just like `return;'. */
9799 if (retval
== error_mark_node
)
9801 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9802 else if (! cfun
->returns_struct
9803 && TREE_CODE (retval
) == TARGET_EXPR
9804 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
9805 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
9806 TREE_OPERAND (retval
, 0));
9807 else if (!processing_template_decl
9808 && maybe_warn_about_returning_address_of_local (retval
)
9809 && INDIRECT_TYPE_P (valtype
))
9810 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
9811 build_zero_cst (TREE_TYPE (retval
)));
9814 if (processing_template_decl
)
9815 return saved_retval
;
9817 /* Actually copy the value returned into the appropriate location. */
9818 if (retval
&& retval
!= result
)
9819 retval
= build2 (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
9825 /* Returns nonzero if the pointer-type FROM can be converted to the
9826 pointer-type TO via a qualification conversion. If CONSTP is -1,
9827 then we return nonzero if the pointers are similar, and the
9828 cv-qualification signature of FROM is a proper subset of that of TO.
9830 If CONSTP is positive, then all outer pointers have been
9834 comp_ptr_ttypes_real (tree to
, tree from
, int constp
)
9836 bool to_more_cv_qualified
= false;
9837 bool is_opaque_pointer
= false;
9839 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9841 if (TREE_CODE (to
) != TREE_CODE (from
))
9844 if (TREE_CODE (from
) == OFFSET_TYPE
9845 && !same_type_p (TYPE_OFFSET_BASETYPE (from
),
9846 TYPE_OFFSET_BASETYPE (to
)))
9849 /* Const and volatile mean something different for function types,
9850 so the usual checks are not appropriate. */
9851 if (!FUNC_OR_METHOD_TYPE_P (to
))
9853 if (!at_least_as_qualified_p (to
, from
))
9856 if (!at_least_as_qualified_p (from
, to
))
9860 to_more_cv_qualified
= true;
9864 constp
&= TYPE_READONLY (to
);
9867 if (VECTOR_TYPE_P (to
))
9868 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
9870 if (!TYPE_PTR_P (to
) && !TYPE_PTRDATAMEM_P (to
))
9871 return ((constp
>= 0 || to_more_cv_qualified
)
9872 && (is_opaque_pointer
9873 || same_type_ignoring_top_level_qualifiers_p (to
, from
)));
9877 /* When comparing, say, char ** to char const **, this function takes
9878 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9879 types to this function. */
9882 comp_ptr_ttypes (tree to
, tree from
)
9884 return comp_ptr_ttypes_real (to
, from
, 1);
9887 /* Returns true iff FNTYPE is a non-class type that involves
9888 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9889 if a parameter type is ill-formed. */
9892 error_type_p (const_tree type
)
9896 switch (TREE_CODE (type
))
9902 case REFERENCE_TYPE
:
9904 return error_type_p (TREE_TYPE (type
));
9908 if (error_type_p (TREE_TYPE (type
)))
9910 for (t
= TYPE_ARG_TYPES (type
); t
; t
= TREE_CHAIN (t
))
9911 if (error_type_p (TREE_VALUE (t
)))
9916 if (TYPE_PTRMEMFUNC_P (type
))
9917 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type
));
9925 /* Returns true if to and from are (possibly multi-level) pointers to the same
9926 type or inheritance-related types, regardless of cv-quals. */
9929 ptr_reasonably_similar (const_tree to
, const_tree from
)
9931 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9933 /* Any target type is similar enough to void. */
9934 if (VOID_TYPE_P (to
))
9935 return !error_type_p (from
);
9936 if (VOID_TYPE_P (from
))
9937 return !error_type_p (to
);
9939 if (TREE_CODE (to
) != TREE_CODE (from
))
9942 if (TREE_CODE (from
) == OFFSET_TYPE
9943 && comptypes (TYPE_OFFSET_BASETYPE (to
),
9944 TYPE_OFFSET_BASETYPE (from
),
9945 COMPARE_BASE
| COMPARE_DERIVED
))
9948 if (VECTOR_TYPE_P (to
)
9949 && vector_types_convertible_p (to
, from
, false))
9952 if (TREE_CODE (to
) == INTEGER_TYPE
9953 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
9956 if (TREE_CODE (to
) == FUNCTION_TYPE
)
9957 return !error_type_p (to
) && !error_type_p (from
);
9959 if (!TYPE_PTR_P (to
))
9961 /* When either type is incomplete avoid DERIVED_FROM_P,
9962 which may call complete_type (c++/57942). */
9963 bool b
= !COMPLETE_TYPE_P (to
) || !COMPLETE_TYPE_P (from
);
9965 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
9966 b
? COMPARE_STRICT
: COMPARE_BASE
| COMPARE_DERIVED
);
9971 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9972 pointer-to-member types) are the same, ignoring cv-qualification at
9976 comp_ptr_ttypes_const (tree to
, tree from
)
9978 bool is_opaque_pointer
= false;
9980 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9982 if (TREE_CODE (to
) != TREE_CODE (from
))
9985 if (TREE_CODE (from
) == OFFSET_TYPE
9986 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
9987 TYPE_OFFSET_BASETYPE (to
)))
9990 if (VECTOR_TYPE_P (to
))
9991 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
9993 if (!TYPE_PTR_P (to
))
9994 return (is_opaque_pointer
9995 || same_type_ignoring_top_level_qualifiers_p (to
, from
));
9999 /* Returns the type qualifiers for this type, including the qualifiers on the
10000 elements for an array type. */
10003 cp_type_quals (const_tree type
)
10006 /* This CONST_CAST is okay because strip_array_types returns its
10007 argument unmodified and we assign it to a const_tree. */
10008 type
= strip_array_types (CONST_CAST_TREE (type
));
10009 if (type
== error_mark_node
10010 /* Quals on a FUNCTION_TYPE are memfn quals. */
10011 || TREE_CODE (type
) == FUNCTION_TYPE
)
10012 return TYPE_UNQUALIFIED
;
10013 quals
= TYPE_QUALS (type
);
10014 /* METHOD and REFERENCE_TYPEs should never have quals. */
10015 gcc_assert ((TREE_CODE (type
) != METHOD_TYPE
10016 && !TYPE_REF_P (type
))
10017 || ((quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
))
10018 == TYPE_UNQUALIFIED
));
10022 /* Returns the function-ref-qualifier for TYPE */
10025 type_memfn_rqual (const_tree type
)
10027 gcc_assert (FUNC_OR_METHOD_TYPE_P (type
));
10029 if (!FUNCTION_REF_QUALIFIED (type
))
10030 return REF_QUAL_NONE
;
10031 else if (FUNCTION_RVALUE_QUALIFIED (type
))
10032 return REF_QUAL_RVALUE
;
10034 return REF_QUAL_LVALUE
;
10037 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
10041 type_memfn_quals (const_tree type
)
10043 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10044 return TYPE_QUALS (type
);
10045 else if (TREE_CODE (type
) == METHOD_TYPE
)
10046 return cp_type_quals (class_of_this_parm (type
));
10048 gcc_unreachable ();
10051 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
10052 MEMFN_QUALS and its ref-qualifier to RQUAL. */
10055 apply_memfn_quals (tree type
, cp_cv_quals memfn_quals
, cp_ref_qualifier rqual
)
10057 /* Could handle METHOD_TYPE here if necessary. */
10058 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
10059 if (TYPE_QUALS (type
) == memfn_quals
10060 && type_memfn_rqual (type
) == rqual
)
10063 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
10065 tree result
= build_qualified_type (type
, memfn_quals
);
10066 return build_ref_qualified_type (result
, rqual
);
10069 /* Returns nonzero if TYPE is const or volatile. */
10072 cv_qualified_p (const_tree type
)
10074 int quals
= cp_type_quals (type
);
10075 return (quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)) != 0;
10078 /* Returns nonzero if the TYPE contains a mutable member. */
10081 cp_has_mutable_p (const_tree type
)
10083 /* This CONST_CAST is okay because strip_array_types returns its
10084 argument unmodified and we assign it to a const_tree. */
10085 type
= strip_array_types (CONST_CAST_TREE(type
));
10087 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
10090 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
10091 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
10092 approximation. In particular, consider:
10095 struct S { int i; };
10096 const S s = { f(); }
10098 Here, we will make "s" as TREE_READONLY (because it is declared
10099 "const") -- only to reverse ourselves upon seeing that the
10100 initializer is non-constant. */
10103 cp_apply_type_quals_to_decl (int type_quals
, tree decl
)
10105 tree type
= TREE_TYPE (decl
);
10107 if (type
== error_mark_node
)
10110 if (TREE_CODE (decl
) == TYPE_DECL
)
10113 gcc_assert (!(TREE_CODE (type
) == FUNCTION_TYPE
10114 && type_quals
!= TYPE_UNQUALIFIED
));
10116 /* Avoid setting TREE_READONLY incorrectly. */
10117 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
10118 constructor can produce constant init, so rely on cp_finish_decl to
10119 clear TREE_READONLY if the variable has non-constant init. */
10121 /* If the type has (or might have) a mutable component, that component
10122 might be modified. */
10123 if (TYPE_HAS_MUTABLE_P (type
) || !COMPLETE_TYPE_P (type
))
10124 type_quals
&= ~TYPE_QUAL_CONST
;
10126 c_apply_type_quals_to_decl (type_quals
, decl
);
10129 /* Subroutine of casts_away_constness. Make T1 and T2 point at
10130 exemplar types such that casting T1 to T2 is casting away constness
10131 if and only if there is no implicit conversion from T1 to T2. */
10134 casts_away_constness_r (tree
*t1
, tree
*t2
, tsubst_flags_t complain
)
10139 /* [expr.const.cast]
10141 For multi-level pointer to members and multi-level mixed pointers
10142 and pointers to members (conv.qual), the "member" aspect of a
10143 pointer to member level is ignored when determining if a const
10144 cv-qualifier has been cast away. */
10145 /* [expr.const.cast]
10147 For two pointer types:
10149 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
10150 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
10153 casting from X1 to X2 casts away constness if, for a non-pointer
10154 type T there does not exist an implicit conversion (clause
10157 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
10161 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
10162 if ((!TYPE_PTR_P (*t1
) && !TYPE_PTRDATAMEM_P (*t1
))
10163 || (!TYPE_PTR_P (*t2
) && !TYPE_PTRDATAMEM_P (*t2
)))
10165 *t1
= cp_build_qualified_type (void_type_node
,
10166 cp_type_quals (*t1
));
10167 *t2
= cp_build_qualified_type (void_type_node
,
10168 cp_type_quals (*t2
));
10172 quals1
= cp_type_quals (*t1
);
10173 quals2
= cp_type_quals (*t2
);
10175 if (TYPE_PTRDATAMEM_P (*t1
))
10176 *t1
= TYPE_PTRMEM_POINTED_TO_TYPE (*t1
);
10178 *t1
= TREE_TYPE (*t1
);
10179 if (TYPE_PTRDATAMEM_P (*t2
))
10180 *t2
= TYPE_PTRMEM_POINTED_TO_TYPE (*t2
);
10182 *t2
= TREE_TYPE (*t2
);
10184 casts_away_constness_r (t1
, t2
, complain
);
10185 *t1
= build_pointer_type (*t1
);
10186 *t2
= build_pointer_type (*t2
);
10187 *t1
= cp_build_qualified_type (*t1
, quals1
);
10188 *t2
= cp_build_qualified_type (*t2
, quals2
);
10191 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
10194 ??? This function returns non-zero if casting away qualifiers not
10195 just const. We would like to return to the caller exactly which
10196 qualifiers are casted away to give more accurate diagnostics.
10200 casts_away_constness (tree t1
, tree t2
, tsubst_flags_t complain
)
10202 if (TYPE_REF_P (t2
))
10204 /* [expr.const.cast]
10206 Casting from an lvalue of type T1 to an lvalue of type T2
10207 using a reference cast casts away constness if a cast from an
10208 rvalue of type "pointer to T1" to the type "pointer to T2"
10209 casts away constness. */
10210 t1
= (TYPE_REF_P (t1
) ? TREE_TYPE (t1
) : t1
);
10211 return casts_away_constness (build_pointer_type (t1
),
10212 build_pointer_type (TREE_TYPE (t2
)),
10216 if (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
10217 /* [expr.const.cast]
10219 Casting from an rvalue of type "pointer to data member of X
10220 of type T1" to the type "pointer to data member of Y of type
10221 T2" casts away constness if a cast from an rvalue of type
10222 "pointer to T1" to the type "pointer to T2" casts away
10224 return casts_away_constness
10225 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1
)),
10226 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2
)),
10229 /* Casting away constness is only something that makes sense for
10230 pointer or reference types. */
10231 if (!TYPE_PTR_P (t1
) || !TYPE_PTR_P (t2
))
10234 /* Top-level qualifiers don't matter. */
10235 t1
= TYPE_MAIN_VARIANT (t1
);
10236 t2
= TYPE_MAIN_VARIANT (t2
);
10237 casts_away_constness_r (&t1
, &t2
, complain
);
10238 if (!can_convert (t2
, t1
, complain
))
10244 /* If T is a REFERENCE_TYPE return the type to which T refers.
10245 Otherwise, return T itself. */
10248 non_reference (tree t
)
10250 if (t
&& TYPE_REF_P (t
))
10256 /* Return nonzero if REF is an lvalue valid for this language;
10257 otherwise, print an error message and return zero. USE says
10258 how the lvalue is being used and so selects the error message. */
10261 lvalue_or_else (tree ref
, enum lvalue_use use
, tsubst_flags_t complain
)
10263 cp_lvalue_kind kind
= lvalue_kind (ref
);
10265 if (kind
== clk_none
)
10267 if (complain
& tf_error
)
10268 lvalue_error (input_location
, use
);
10271 else if (kind
& (clk_rvalueref
|clk_class
))
10273 if (!(complain
& tf_error
))
10275 /* Make this a permerror because we used to accept it. */
10276 permerror (input_location
, "using rvalue as lvalue");
10281 /* Return true if a user-defined literal operator is a raw operator. */
10284 check_raw_literal_operator (const_tree decl
)
10286 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
10289 bool maybe_raw_p
= false;
10291 /* Count the number and type of arguments and check for ellipsis. */
10292 for (argtype
= argtypes
, arity
= 0;
10293 argtype
&& argtype
!= void_list_node
;
10294 ++arity
, argtype
= TREE_CHAIN (argtype
))
10296 tree t
= TREE_VALUE (argtype
);
10298 if (same_type_p (t
, const_string_type_node
))
10299 maybe_raw_p
= true;
10302 return false; /* Found ellipsis. */
10304 if (!maybe_raw_p
|| arity
!= 1)
10311 /* Return true if a user-defined literal operator has one of the allowed
10315 check_literal_operator_args (const_tree decl
,
10316 bool *long_long_unsigned_p
, bool *long_double_p
)
10318 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
10320 *long_long_unsigned_p
= false;
10321 *long_double_p
= false;
10322 if (processing_template_decl
|| processing_specialization
)
10323 return argtypes
== void_list_node
;
10330 /* Count the number and type of arguments and check for ellipsis. */
10331 for (argtype
= argtypes
, arity
= 0;
10332 argtype
&& argtype
!= void_list_node
;
10333 argtype
= TREE_CHAIN (argtype
))
10335 tree t
= TREE_VALUE (argtype
);
10338 if (TYPE_PTR_P (t
))
10340 bool maybe_raw_p
= false;
10342 if (cp_type_quals (t
) != TYPE_QUAL_CONST
)
10344 t
= TYPE_MAIN_VARIANT (t
);
10345 if ((maybe_raw_p
= same_type_p (t
, char_type_node
))
10346 || same_type_p (t
, wchar_type_node
)
10347 || same_type_p (t
, char8_type_node
)
10348 || same_type_p (t
, char16_type_node
)
10349 || same_type_p (t
, char32_type_node
))
10351 argtype
= TREE_CHAIN (argtype
);
10354 t
= TREE_VALUE (argtype
);
10355 if (maybe_raw_p
&& argtype
== void_list_node
)
10357 else if (same_type_p (t
, size_type_node
))
10366 else if (same_type_p (t
, long_long_unsigned_type_node
))
10369 *long_long_unsigned_p
= true;
10371 else if (same_type_p (t
, long_double_type_node
))
10374 *long_double_p
= true;
10376 else if (same_type_p (t
, char_type_node
))
10378 else if (same_type_p (t
, wchar_type_node
))
10380 else if (same_type_p (t
, char8_type_node
))
10382 else if (same_type_p (t
, char16_type_node
))
10384 else if (same_type_p (t
, char32_type_node
))
10390 return false; /* Found ellipsis. */
10392 if (arity
!= max_arity
)
10399 /* Always returns false since unlike C90, C++ has no concept of implicit
10400 function declarations. */
10403 c_decl_implicit (const_tree
)