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"
44 static tree
cp_build_addr_expr_strict (tree
, tsubst_flags_t
);
45 static tree
cp_build_function_call (tree
, tree
, tsubst_flags_t
);
46 static tree
pfn_from_ptrmemfunc (tree
);
47 static tree
delta_from_ptrmemfunc (tree
);
48 static tree
convert_for_assignment (tree
, tree
, impl_conv_rhs
, tree
, int,
50 static tree
cp_pointer_int_sum (location_t
, enum tree_code
, tree
, tree
,
52 static tree
rationalize_conditional_expr (enum tree_code
, tree
,
54 static int comp_ptr_ttypes_real (tree
, tree
, int);
55 static bool comp_except_types (tree
, tree
, bool);
56 static bool comp_array_types (const_tree
, const_tree
, bool);
57 static tree
pointer_diff (location_t
, tree
, tree
, tree
, tsubst_flags_t
, tree
*);
58 static tree
get_delta_difference (tree
, tree
, bool, bool, tsubst_flags_t
);
59 static void casts_away_constness_r (tree
*, tree
*, tsubst_flags_t
);
60 static bool casts_away_constness (tree
, tree
, tsubst_flags_t
);
61 static bool maybe_warn_about_returning_address_of_local (tree
);
62 static tree
lookup_destructor (tree
, tree
, tree
, tsubst_flags_t
);
63 static void error_args_num (location_t
, tree
, bool);
64 static int convert_arguments (tree
, vec
<tree
, va_gc
> **, tree
, int,
66 static bool is_std_move_p (tree
);
67 static bool is_std_forward_p (tree
);
69 /* Do `exp = require_complete_type (exp);' to make sure exp
70 does not have an incomplete type. (That includes void types.)
71 Returns error_mark_node if the VALUE does not have
72 complete type when this function returns. */
75 require_complete_type_sfinae (tree value
, tsubst_flags_t complain
)
79 if (processing_template_decl
|| value
== error_mark_node
)
82 if (TREE_CODE (value
) == OVERLOAD
)
83 type
= unknown_type_node
;
85 type
= TREE_TYPE (value
);
87 if (type
== error_mark_node
)
88 return error_mark_node
;
90 /* First, detect a valid value with a complete type. */
91 if (COMPLETE_TYPE_P (type
))
94 if (complete_type_or_maybe_complain (type
, value
, complain
))
97 return error_mark_node
;
101 require_complete_type (tree value
)
103 return require_complete_type_sfinae (value
, tf_warning_or_error
);
106 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
107 a template instantiation, do the instantiation. Returns TYPE,
108 whether or not it could be completed, unless something goes
109 horribly wrong, in which case the error_mark_node is returned. */
112 complete_type (tree type
)
114 if (type
== NULL_TREE
)
115 /* Rather than crash, we return something sure to cause an error
117 return error_mark_node
;
119 if (type
== error_mark_node
|| COMPLETE_TYPE_P (type
))
121 else if (TREE_CODE (type
) == ARRAY_TYPE
)
123 tree t
= complete_type (TREE_TYPE (type
));
124 unsigned int needs_constructing
, has_nontrivial_dtor
;
125 if (COMPLETE_TYPE_P (t
) && !dependent_type_p (type
))
128 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
130 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
131 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
133 TYPE_NEEDS_CONSTRUCTING (t
) = needs_constructing
;
134 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = has_nontrivial_dtor
;
137 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
138 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
143 /* Like complete_type, but issue an error if the TYPE cannot be completed.
144 VALUE is used for informative diagnostics.
145 Returns NULL_TREE if the type cannot be made complete. */
148 complete_type_or_maybe_complain (tree type
, tree value
, tsubst_flags_t complain
)
150 type
= complete_type (type
);
151 if (type
== error_mark_node
)
152 /* We already issued an error. */
154 else if (!COMPLETE_TYPE_P (type
))
156 if (complain
& tf_error
)
157 cxx_incomplete_type_diagnostic (value
, type
, DK_ERROR
);
165 complete_type_or_else (tree type
, tree value
)
167 return complete_type_or_maybe_complain (type
, value
, tf_warning_or_error
);
171 /* Return the common type of two parameter lists.
172 We assume that comptypes has already been done and returned 1;
173 if that isn't so, this may crash.
175 As an optimization, free the space we allocate if the parameter
176 lists are already common. */
179 commonparms (tree p1
, tree p2
)
181 tree oldargs
= p1
, newargs
, n
;
185 len
= list_length (p1
);
186 newargs
= tree_last (p1
);
188 if (newargs
== void_list_node
)
197 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
202 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
204 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
206 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
209 else if (! TREE_PURPOSE (p1
))
211 if (TREE_PURPOSE (p2
))
213 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
219 if (simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)) != 1)
221 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
223 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
226 TREE_VALUE (n
) = merge_types (TREE_VALUE (p1
), TREE_VALUE (p2
));
229 TREE_VALUE (n
) = TREE_VALUE (p1
);
237 /* Given a type, perhaps copied for a typedef,
238 find the "original" version of it. */
240 original_type (tree t
)
242 int quals
= cp_type_quals (t
);
243 while (t
!= error_mark_node
244 && TYPE_NAME (t
) != NULL_TREE
)
246 tree x
= TYPE_NAME (t
);
247 if (TREE_CODE (x
) != TYPE_DECL
)
249 x
= DECL_ORIGINAL_TYPE (x
);
254 return cp_build_qualified_type (t
, quals
);
257 /* Return the common type for two arithmetic types T1 and T2 under the
258 usual arithmetic conversions. The default conversions have already
259 been applied, and enumerated types converted to their compatible
263 cp_common_type (tree t1
, tree t2
)
265 enum tree_code code1
= TREE_CODE (t1
);
266 enum tree_code code2
= TREE_CODE (t2
);
271 /* In what follows, we slightly generalize the rules given in [expr] so
272 as to deal with `long long' and `complex'. First, merge the
274 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
276 if (SCOPED_ENUM_P (t1
) || SCOPED_ENUM_P (t2
))
278 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
279 return build_type_attribute_variant (t1
, attributes
);
284 /* FIXME: Attributes. */
285 gcc_assert (ARITHMETIC_TYPE_P (t1
)
286 || VECTOR_TYPE_P (t1
)
287 || UNSCOPED_ENUM_P (t1
));
288 gcc_assert (ARITHMETIC_TYPE_P (t2
)
289 || VECTOR_TYPE_P (t2
)
290 || UNSCOPED_ENUM_P (t2
));
292 /* If one type is complex, form the common type of the non-complex
293 components, then make that complex. Use T1 or T2 if it is the
295 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
297 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
298 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
300 = type_after_usual_arithmetic_conversions (subtype1
, subtype2
);
302 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
303 return build_type_attribute_variant (t1
, attributes
);
304 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
305 return build_type_attribute_variant (t2
, attributes
);
307 return build_type_attribute_variant (build_complex_type (subtype
),
311 if (code1
== VECTOR_TYPE
)
313 /* When we get here we should have two vectors of the same size.
314 Just prefer the unsigned one if present. */
315 if (TYPE_UNSIGNED (t1
))
316 return build_type_attribute_variant (t1
, attributes
);
318 return build_type_attribute_variant (t2
, attributes
);
321 /* If only one is real, use it as the result. */
322 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
323 return build_type_attribute_variant (t1
, attributes
);
324 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
325 return build_type_attribute_variant (t2
, attributes
);
327 /* Both real or both integers; use the one with greater precision. */
328 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
329 return build_type_attribute_variant (t1
, attributes
);
330 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
331 return build_type_attribute_variant (t2
, attributes
);
333 /* The types are the same; no need to do anything fancy. */
334 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
335 return build_type_attribute_variant (t1
, attributes
);
337 if (code1
!= REAL_TYPE
)
339 /* If one is unsigned long long, then convert the other to unsigned
341 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_unsigned_type_node
)
342 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_unsigned_type_node
))
343 return build_type_attribute_variant (long_long_unsigned_type_node
,
345 /* If one is a long long, and the other is an unsigned long, and
346 long long can represent all the values of an unsigned long, then
347 convert to a long long. Otherwise, convert to an unsigned long
348 long. Otherwise, if either operand is long long, convert the
351 Since we're here, we know the TYPE_PRECISION is the same;
352 therefore converting to long long cannot represent all the values
353 of an unsigned long, so we choose unsigned long long in that
355 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_integer_type_node
)
356 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_integer_type_node
))
358 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
359 ? long_long_unsigned_type_node
360 : long_long_integer_type_node
);
361 return build_type_attribute_variant (t
, attributes
);
364 /* Go through the same procedure, but for longs. */
365 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_unsigned_type_node
)
366 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_unsigned_type_node
))
367 return build_type_attribute_variant (long_unsigned_type_node
,
369 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_integer_type_node
)
370 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_integer_type_node
))
372 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
373 ? long_unsigned_type_node
: long_integer_type_node
);
374 return build_type_attribute_variant (t
, attributes
);
377 /* For __intN types, either the type is __int128 (and is lower
378 priority than the types checked above, but higher than other
379 128-bit types) or it's known to not be the same size as other
380 types (enforced in toplev.c). Prefer the unsigned type. */
381 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
383 if (int_n_enabled_p
[i
]
384 && (same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].signed_type
)
385 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].signed_type
)
386 || same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].unsigned_type
)
387 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].unsigned_type
)))
389 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
390 ? int_n_trees
[i
].unsigned_type
391 : int_n_trees
[i
].signed_type
);
392 return build_type_attribute_variant (t
, attributes
);
396 /* Otherwise prefer the unsigned one. */
397 if (TYPE_UNSIGNED (t1
))
398 return build_type_attribute_variant (t1
, attributes
);
400 return build_type_attribute_variant (t2
, attributes
);
404 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_double_type_node
)
405 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_double_type_node
))
406 return build_type_attribute_variant (long_double_type_node
,
408 if (same_type_p (TYPE_MAIN_VARIANT (t1
), double_type_node
)
409 || same_type_p (TYPE_MAIN_VARIANT (t2
), double_type_node
))
410 return build_type_attribute_variant (double_type_node
,
412 if (same_type_p (TYPE_MAIN_VARIANT (t1
), float_type_node
)
413 || same_type_p (TYPE_MAIN_VARIANT (t2
), float_type_node
))
414 return build_type_attribute_variant (float_type_node
,
417 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
418 the standard C++ floating-point types. Logic earlier in this
419 function has already eliminated the possibility that
420 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
421 compelling reason to choose one or the other. */
422 return build_type_attribute_variant (t1
, attributes
);
426 /* T1 and T2 are arithmetic or enumeration types. Return the type
427 that will result from the "usual arithmetic conversions" on T1 and
428 T2 as described in [expr]. */
431 type_after_usual_arithmetic_conversions (tree t1
, tree t2
)
433 gcc_assert (ARITHMETIC_TYPE_P (t1
)
434 || VECTOR_TYPE_P (t1
)
435 || UNSCOPED_ENUM_P (t1
));
436 gcc_assert (ARITHMETIC_TYPE_P (t2
)
437 || VECTOR_TYPE_P (t2
)
438 || UNSCOPED_ENUM_P (t2
));
440 /* Perform the integral promotions. We do not promote real types here. */
441 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1
)
442 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2
))
444 t1
= type_promotes_to (t1
);
445 t2
= type_promotes_to (t2
);
448 return cp_common_type (t1
, t2
);
452 composite_pointer_error (diagnostic_t kind
, tree t1
, tree t2
,
453 composite_pointer_operation operation
)
458 emit_diagnostic (kind
, input_location
, 0,
459 "comparison between "
460 "distinct pointer types %qT and %qT lacks a cast",
464 emit_diagnostic (kind
, input_location
, 0,
465 "conversion between "
466 "distinct pointer types %qT and %qT lacks a cast",
469 case CPO_CONDITIONAL_EXPR
:
470 emit_diagnostic (kind
, input_location
, 0,
471 "conditional expression between "
472 "distinct pointer types %qT and %qT lacks a cast",
480 /* Subroutine of composite_pointer_type to implement the recursive
481 case. See that function for documentation of the parameters. */
484 composite_pointer_type_r (tree t1
, tree t2
,
485 composite_pointer_operation operation
,
486 tsubst_flags_t complain
)
493 /* Determine the types pointed to by T1 and T2. */
496 pointee1
= TREE_TYPE (t1
);
497 pointee2
= TREE_TYPE (t2
);
501 pointee1
= TYPE_PTRMEM_POINTED_TO_TYPE (t1
);
502 pointee2
= TYPE_PTRMEM_POINTED_TO_TYPE (t2
);
507 Otherwise, the composite pointer type is a pointer type
508 similar (_conv.qual_) to the type of one of the operands,
509 with a cv-qualification signature (_conv.qual_) that is the
510 union of the cv-qualification signatures of the operand
512 if (same_type_ignoring_top_level_qualifiers_p (pointee1
, pointee2
))
513 result_type
= pointee1
;
514 else if ((TYPE_PTR_P (pointee1
) && TYPE_PTR_P (pointee2
))
515 || (TYPE_PTRMEM_P (pointee1
) && TYPE_PTRMEM_P (pointee2
)))
517 result_type
= composite_pointer_type_r (pointee1
, pointee2
, operation
,
519 if (result_type
== error_mark_node
)
520 return error_mark_node
;
524 if (complain
& tf_error
)
525 composite_pointer_error (DK_PERMERROR
, t1
, t2
, operation
);
527 return error_mark_node
;
528 result_type
= void_type_node
;
530 result_type
= cp_build_qualified_type (result_type
,
531 (cp_type_quals (pointee1
)
532 | cp_type_quals (pointee2
)));
533 /* If the original types were pointers to members, so is the
535 if (TYPE_PTRMEM_P (t1
))
537 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
538 TYPE_PTRMEM_CLASS_TYPE (t2
)))
540 if (complain
& tf_error
)
541 composite_pointer_error (DK_PERMERROR
, t1
, t2
, operation
);
543 return error_mark_node
;
545 result_type
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
549 result_type
= build_pointer_type (result_type
);
551 /* Merge the attributes. */
552 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
553 return build_type_attribute_variant (result_type
, attributes
);
556 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
557 ARG1 and ARG2 are the values with those types. The OPERATION is to
558 describe the operation between the pointer types,
559 in case an error occurs.
561 This routine also implements the computation of a common type for
562 pointers-to-members as per [expr.eq]. */
565 composite_pointer_type (tree t1
, tree t2
, tree arg1
, tree arg2
,
566 composite_pointer_operation operation
,
567 tsubst_flags_t complain
)
574 If one operand is a null pointer constant, the composite pointer
575 type is the type of the other operand. */
576 if (null_ptr_cst_p (arg1
))
578 if (null_ptr_cst_p (arg2
))
585 If one of the operands has type "pointer to cv1 void*", then
586 the other has type "pointer to cv2T", and the composite pointer
587 type is "pointer to cv12 void", where cv12 is the union of cv1
590 If either type is a pointer to void, make sure it is T1. */
591 if (TYPE_PTR_P (t2
) && VOID_TYPE_P (TREE_TYPE (t2
)))
594 /* Now, if T1 is a pointer to void, merge the qualifiers. */
595 if (TYPE_PTR_P (t1
) && VOID_TYPE_P (TREE_TYPE (t1
)))
600 if (TYPE_PTRFN_P (t2
))
602 if (complain
& tf_error
)
607 pedwarn (input_location
, OPT_Wpedantic
,
608 "ISO C++ forbids comparison between pointer "
609 "of type %<void *%> and pointer-to-function");
612 pedwarn (input_location
, OPT_Wpedantic
,
613 "ISO C++ forbids conversion between pointer "
614 "of type %<void *%> and pointer-to-function");
616 case CPO_CONDITIONAL_EXPR
:
617 pedwarn (input_location
, OPT_Wpedantic
,
618 "ISO C++ forbids conditional expression between "
619 "pointer of type %<void *%> and "
620 "pointer-to-function");
627 return error_mark_node
;
630 = cp_build_qualified_type (void_type_node
,
631 (cp_type_quals (TREE_TYPE (t1
))
632 | cp_type_quals (TREE_TYPE (t2
))));
633 result_type
= build_pointer_type (result_type
);
634 /* Merge the attributes. */
635 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
636 return build_type_attribute_variant (result_type
, attributes
);
639 if (c_dialect_objc () && TYPE_PTR_P (t1
)
642 if (objc_have_common_type (t1
, t2
, -3, NULL_TREE
))
643 return objc_common_type (t1
, t2
);
646 /* if T1 or T2 is "pointer to noexcept function" and the other type is
647 "pointer to function", where the function types are otherwise the same,
648 "pointer to function" */
649 if (fnptr_conv_p (t1
, t2
))
651 if (fnptr_conv_p (t2
, t1
))
654 /* [expr.eq] permits the application of a pointer conversion to
655 bring the pointers to a common type. */
656 if (TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
)
657 && CLASS_TYPE_P (TREE_TYPE (t1
))
658 && CLASS_TYPE_P (TREE_TYPE (t2
))
659 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1
),
662 class1
= TREE_TYPE (t1
);
663 class2
= TREE_TYPE (t2
);
665 if (DERIVED_FROM_P (class1
, class2
))
666 t2
= (build_pointer_type
667 (cp_build_qualified_type (class1
, cp_type_quals (class2
))));
668 else if (DERIVED_FROM_P (class2
, class1
))
669 t1
= (build_pointer_type
670 (cp_build_qualified_type (class2
, cp_type_quals (class1
))));
673 if (complain
& tf_error
)
674 composite_pointer_error (DK_ERROR
, t1
, t2
, operation
);
675 return error_mark_node
;
678 /* [expr.eq] permits the application of a pointer-to-member
679 conversion to change the class type of one of the types. */
680 else if (TYPE_PTRMEM_P (t1
)
681 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
682 TYPE_PTRMEM_CLASS_TYPE (t2
)))
684 class1
= TYPE_PTRMEM_CLASS_TYPE (t1
);
685 class2
= TYPE_PTRMEM_CLASS_TYPE (t2
);
687 if (DERIVED_FROM_P (class1
, class2
))
688 t1
= build_ptrmem_type (class2
, TYPE_PTRMEM_POINTED_TO_TYPE (t1
));
689 else if (DERIVED_FROM_P (class2
, class1
))
690 t2
= build_ptrmem_type (class1
, TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
693 if (complain
& tf_error
)
697 error ("comparison between distinct "
698 "pointer-to-member types %qT and %qT lacks a cast",
702 error ("conversion between distinct "
703 "pointer-to-member types %qT and %qT lacks a cast",
706 case CPO_CONDITIONAL_EXPR
:
707 error ("conditional expression between distinct "
708 "pointer-to-member types %qT and %qT lacks a cast",
714 return error_mark_node
;
718 return composite_pointer_type_r (t1
, t2
, operation
, complain
);
721 /* Return the merged type of two types.
722 We assume that comptypes has already been done and returned 1;
723 if that isn't so, this may crash.
725 This just combines attributes and default arguments; any other
726 differences would cause the two types to compare unalike. */
729 merge_types (tree t1
, tree t2
)
731 enum tree_code code1
;
732 enum tree_code code2
;
735 /* Save time if the two types are the same. */
738 if (original_type (t1
) == original_type (t2
))
741 /* If one type is nonsense, use the other. */
742 if (t1
== error_mark_node
)
744 if (t2
== error_mark_node
)
747 /* Handle merging an auto redeclaration with a previous deduced
752 /* Merge the attributes. */
753 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
755 if (TYPE_PTRMEMFUNC_P (t1
))
756 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
757 if (TYPE_PTRMEMFUNC_P (t2
))
758 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
760 code1
= TREE_CODE (t1
);
761 code2
= TREE_CODE (t2
);
764 gcc_assert (code1
== TYPENAME_TYPE
|| code2
== TYPENAME_TYPE
);
765 if (code1
== TYPENAME_TYPE
)
767 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
768 code1
= TREE_CODE (t1
);
772 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
773 code2
= TREE_CODE (t2
);
781 /* For two pointers, do this recursively on the target type. */
783 tree target
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
784 int quals
= cp_type_quals (t1
);
786 if (code1
== POINTER_TYPE
)
788 t1
= build_pointer_type (target
);
789 if (TREE_CODE (target
) == METHOD_TYPE
)
790 t1
= build_ptrmemfunc_type (t1
);
793 t1
= cp_build_reference_type (target
, TYPE_REF_IS_RVALUE (t1
));
794 t1
= build_type_attribute_variant (t1
, attributes
);
795 t1
= cp_build_qualified_type (t1
, quals
);
804 quals
= cp_type_quals (t1
);
805 pointee
= merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1
),
806 TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
807 t1
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
809 t1
= cp_build_qualified_type (t1
, quals
);
815 tree elt
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
816 /* Save space: see if the result is identical to one of the args. */
817 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
818 return build_type_attribute_variant (t1
, attributes
);
819 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
820 return build_type_attribute_variant (t2
, attributes
);
821 /* Merge the element types, and have a size if either arg has one. */
822 t1
= build_cplus_array_type
823 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
828 /* Function types: prefer the one that specified arg types.
829 If both do, merge the arg types. Also merge the return types. */
831 tree valtype
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
832 tree p1
= TYPE_ARG_TYPES (t1
);
833 tree p2
= TYPE_ARG_TYPES (t2
);
836 /* Save space: see if the result is identical to one of the args. */
837 if (valtype
== TREE_TYPE (t1
) && ! p2
)
838 return cp_build_type_attribute_variant (t1
, attributes
);
839 if (valtype
== TREE_TYPE (t2
) && ! p1
)
840 return cp_build_type_attribute_variant (t2
, attributes
);
842 /* Simple way if one arg fails to specify argument types. */
843 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
845 else if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
848 parms
= commonparms (p1
, p2
);
850 cp_cv_quals quals
= type_memfn_quals (t1
);
851 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
852 gcc_assert (quals
== type_memfn_quals (t2
));
853 gcc_assert (rqual
== type_memfn_rqual (t2
));
855 tree rval
= build_function_type (valtype
, parms
);
856 rval
= apply_memfn_quals (rval
, quals
);
857 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
858 TYPE_RAISES_EXCEPTIONS (t2
));
859 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
860 t1
= build_cp_fntype_variant (rval
, rqual
, raises
, late_return_type_p
);
866 /* Get this value the long way, since TYPE_METHOD_BASETYPE
867 is just the main variant of this. */
868 tree basetype
= class_of_this_parm (t2
);
869 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
870 TYPE_RAISES_EXCEPTIONS (t2
));
871 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
873 bool late_return_type_1_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
875 /* If this was a member function type, get back to the
876 original type of type member function (i.e., without
877 the class instance variable up front. */
878 t1
= build_function_type (TREE_TYPE (t1
),
879 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
880 t2
= build_function_type (TREE_TYPE (t2
),
881 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
882 t3
= merge_types (t1
, t2
);
883 t3
= build_method_type_directly (basetype
, TREE_TYPE (t3
),
884 TYPE_ARG_TYPES (t3
));
885 t1
= build_cp_fntype_variant (t3
, rqual
, raises
, late_return_type_1_p
);
890 /* There is no need to merge attributes into a TYPENAME_TYPE.
891 When the type is instantiated it will have whatever
892 attributes result from the instantiation. */
896 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
898 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
903 return cp_build_type_attribute_variant (t1
, attributes
);
906 /* Return the ARRAY_TYPE type without its domain. */
909 strip_array_domain (tree type
)
912 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
913 if (TYPE_DOMAIN (type
) == NULL_TREE
)
915 t2
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
916 return cp_build_type_attribute_variant (t2
, TYPE_ATTRIBUTES (type
));
919 /* Wrapper around cp_common_type that is used by c-common.c and other
920 front end optimizations that remove promotions.
922 Return the common type for two arithmetic types T1 and T2 under the
923 usual arithmetic conversions. The default conversions have already
924 been applied, and enumerated types converted to their compatible
928 common_type (tree t1
, tree t2
)
930 /* If one type is nonsense, use the other */
931 if (t1
== error_mark_node
)
933 if (t2
== error_mark_node
)
936 return cp_common_type (t1
, t2
);
939 /* Return the common type of two pointer types T1 and T2. This is the
940 type for the result of most arithmetic operations if the operands
941 have the given two types.
943 We assume that comp_target_types has already been done and returned
944 nonzero; if that isn't so, this may crash. */
947 common_pointer_type (tree t1
, tree t2
)
949 gcc_assert ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
950 || (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
951 || (TYPE_PTRMEMFUNC_P (t1
) && TYPE_PTRMEMFUNC_P (t2
)));
953 return composite_pointer_type (t1
, t2
, error_mark_node
, error_mark_node
,
954 CPO_CONVERSION
, tf_warning_or_error
);
957 /* Compare two exception specifier types for exactness or subsetness, if
958 allowed. Returns false for mismatch, true for match (same, or
961 [except.spec] "If a class X ... objects of class X or any class publicly
962 and unambiguously derived from X. Similarly, if a pointer type Y * ...
963 exceptions of type Y * or that are pointers to any type publicly and
964 unambiguously derived from Y. Otherwise a function only allows exceptions
965 that have the same type ..."
966 This does not mention cv qualifiers and is different to what throw
967 [except.throw] and catch [except.catch] will do. They will ignore the
968 top level cv qualifiers, and allow qualifiers in the pointer to class
971 We implement the letter of the standard. */
974 comp_except_types (tree a
, tree b
, bool exact
)
976 if (same_type_p (a
, b
))
980 if (cp_type_quals (a
) || cp_type_quals (b
))
983 if (TYPE_PTR_P (a
) && TYPE_PTR_P (b
))
987 if (cp_type_quals (a
) || cp_type_quals (b
))
991 if (TREE_CODE (a
) != RECORD_TYPE
992 || TREE_CODE (b
) != RECORD_TYPE
)
995 if (publicly_uniquely_derived_p (a
, b
))
1001 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1002 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1003 If EXACT is ce_type, the C++17 type compatibility rules apply.
1004 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1005 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1006 are unordered, but we've already filtered out duplicates. Most lists will
1007 be in order, we should try to make use of that. */
1010 comp_except_specs (const_tree t1
, const_tree t2
, int exact
)
1019 /* First handle noexcept. */
1020 if (exact
< ce_exact
)
1022 if (exact
== ce_type
1023 && (canonical_eh_spec (CONST_CAST_TREE (t1
))
1024 == canonical_eh_spec (CONST_CAST_TREE (t2
))))
1027 /* noexcept(false) is compatible with no exception-specification,
1028 and less strict than any spec. */
1029 if (t1
== noexcept_false_spec
)
1030 return t2
== NULL_TREE
|| exact
== ce_derived
;
1031 /* Even a derived noexcept(false) is compatible with no
1032 exception-specification. */
1033 if (t2
== noexcept_false_spec
)
1034 return t1
== NULL_TREE
;
1036 /* Otherwise, if we aren't looking for an exact match, noexcept is
1037 equivalent to throw(). */
1038 if (t1
== noexcept_true_spec
)
1039 t1
= empty_except_spec
;
1040 if (t2
== noexcept_true_spec
)
1041 t2
= empty_except_spec
;
1044 /* If any noexcept is left, it is only comparable to itself;
1045 either we're looking for an exact match or we're redeclaring a
1046 template with dependent noexcept. */
1047 if ((t1
&& TREE_PURPOSE (t1
))
1048 || (t2
&& TREE_PURPOSE (t2
)))
1050 && cp_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)));
1052 if (t1
== NULL_TREE
) /* T1 is ... */
1053 return t2
== NULL_TREE
|| exact
== ce_derived
;
1054 if (!TREE_VALUE (t1
)) /* t1 is EMPTY */
1055 return t2
!= NULL_TREE
&& !TREE_VALUE (t2
);
1056 if (t2
== NULL_TREE
) /* T2 is ... */
1058 if (TREE_VALUE (t1
) && !TREE_VALUE (t2
)) /* T2 is EMPTY, T1 is not */
1059 return exact
== ce_derived
;
1061 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1062 Count how many we find, to determine exactness. For exact matching and
1063 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1065 for (base
= t1
; t2
!= NULL_TREE
; t2
= TREE_CHAIN (t2
))
1067 for (probe
= base
; probe
!= NULL_TREE
; probe
= TREE_CHAIN (probe
))
1069 tree a
= TREE_VALUE (probe
);
1070 tree b
= TREE_VALUE (t2
);
1072 if (comp_except_types (a
, b
, exact
))
1074 if (probe
== base
&& exact
> ce_derived
)
1075 base
= TREE_CHAIN (probe
);
1080 if (probe
== NULL_TREE
)
1083 return exact
== ce_derived
|| base
== NULL_TREE
|| length
== list_length (t1
);
1086 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1087 [] can match [size]. */
1090 comp_array_types (const_tree t1
, const_tree t2
, bool allow_redeclaration
)
1099 /* The type of the array elements must be the same. */
1100 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1103 d1
= TYPE_DOMAIN (t1
);
1104 d2
= TYPE_DOMAIN (t2
);
1109 /* If one of the arrays is dimensionless, and the other has a
1110 dimension, they are of different types. However, it is valid to
1118 declarations for an array object can specify
1119 array types that differ by the presence or absence of a major
1120 array bound (_dcl.array_). */
1122 return allow_redeclaration
;
1124 /* Check that the dimensions are the same. */
1126 if (!cp_tree_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
)))
1128 max1
= TYPE_MAX_VALUE (d1
);
1129 max2
= TYPE_MAX_VALUE (d2
);
1131 if (!cp_tree_equal (max1
, max2
))
1137 /* Compare the relative position of T1 and T2 into their respective
1138 template parameter list.
1139 T1 and T2 must be template parameter types.
1140 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1143 comp_template_parms_position (tree t1
, tree t2
)
1145 tree index1
, index2
;
1146 gcc_assert (t1
&& t2
1147 && TREE_CODE (t1
) == TREE_CODE (t2
)
1148 && (TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
1149 || TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
1150 || TREE_CODE (t1
) == TEMPLATE_TYPE_PARM
));
1152 index1
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1
));
1153 index2
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2
));
1155 /* Then compare their relative position. */
1156 if (TEMPLATE_PARM_IDX (index1
) != TEMPLATE_PARM_IDX (index2
)
1157 || TEMPLATE_PARM_LEVEL (index1
) != TEMPLATE_PARM_LEVEL (index2
)
1158 || (TEMPLATE_PARM_PARAMETER_PACK (index1
)
1159 != TEMPLATE_PARM_PARAMETER_PACK (index2
)))
1162 /* In C++14 we can end up comparing 'auto' to a normal template
1163 parameter. Don't confuse them. */
1164 if (cxx_dialect
>= cxx14
&& (is_auto (t1
) || is_auto (t2
)))
1165 return TYPE_IDENTIFIER (t1
) == TYPE_IDENTIFIER (t2
);
1170 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1173 cxx_safe_arg_type_equiv_p (tree t1
, tree t2
)
1175 t1
= TYPE_MAIN_VARIANT (t1
);
1176 t2
= TYPE_MAIN_VARIANT (t2
);
1182 /* The signedness of the parameter matters only when an integral
1183 type smaller than int is promoted to int, otherwise only the
1184 precision of the parameter matters.
1185 This check should make sure that the callee does not see
1186 undefined values in argument registers. */
1187 if (INTEGRAL_TYPE_P (t1
)
1188 && INTEGRAL_TYPE_P (t2
)
1189 && TYPE_PRECISION (t1
) == TYPE_PRECISION (t2
)
1190 && (TYPE_UNSIGNED (t1
) == TYPE_UNSIGNED (t2
)
1191 || !targetm
.calls
.promote_prototypes (NULL_TREE
)
1192 || TYPE_PRECISION (t1
) >= TYPE_PRECISION (integer_type_node
)))
1195 return same_type_p (t1
, t2
);
1198 /* Check if a type cast between two function types can be considered safe. */
1201 cxx_safe_function_type_cast_p (tree t1
, tree t2
)
1203 if (TREE_TYPE (t1
) == void_type_node
&&
1204 TYPE_ARG_TYPES (t1
) == void_list_node
)
1207 if (TREE_TYPE (t2
) == void_type_node
&&
1208 TYPE_ARG_TYPES (t2
) == void_list_node
)
1211 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1214 for (t1
= TYPE_ARG_TYPES (t1
), t2
= TYPE_ARG_TYPES (t2
);
1216 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1217 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1223 /* Subroutine in comptypes. */
1226 structural_comptypes (tree t1
, tree t2
, int strict
)
1231 /* Suppress errors caused by previously reported errors. */
1232 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1235 gcc_assert (TYPE_P (t1
) && TYPE_P (t2
));
1237 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1238 current instantiation. */
1239 if (TREE_CODE (t1
) == TYPENAME_TYPE
)
1240 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
1242 if (TREE_CODE (t2
) == TYPENAME_TYPE
)
1243 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
1245 if (TYPE_PTRMEMFUNC_P (t1
))
1246 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
1247 if (TYPE_PTRMEMFUNC_P (t2
))
1248 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
1250 /* Different classes of types can't be compatible. */
1251 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1254 /* Qualifiers must match. For array types, we will check when we
1255 recur on the array element types. */
1256 if (TREE_CODE (t1
) != ARRAY_TYPE
1257 && cp_type_quals (t1
) != cp_type_quals (t2
))
1259 if (TREE_CODE (t1
) == FUNCTION_TYPE
1260 && type_memfn_quals (t1
) != type_memfn_quals (t2
))
1262 /* Need to check this before TYPE_MAIN_VARIANT.
1263 FIXME function qualifiers should really change the main variant. */
1264 if (TREE_CODE (t1
) == FUNCTION_TYPE
1265 || TREE_CODE (t1
) == METHOD_TYPE
)
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 /* If we get here, we know that from a target independent POV the
1446 types are the same. Make sure the target attributes are also
1448 return comp_type_attributes (t1
, t2
);
1451 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1452 is a bitwise-or of the COMPARE_* flags. */
1455 comptypes (tree t1
, tree t2
, int strict
)
1457 if (strict
== COMPARE_STRICT
)
1462 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1465 if (TYPE_STRUCTURAL_EQUALITY_P (t1
) || TYPE_STRUCTURAL_EQUALITY_P (t2
))
1466 /* At least one of the types requires structural equality, so
1467 perform a deep check. */
1468 return structural_comptypes (t1
, t2
, strict
);
1470 if (flag_checking
&& USE_CANONICAL_TYPES
)
1472 bool result
= structural_comptypes (t1
, t2
, strict
);
1474 if (result
&& TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1475 /* The two types are structurally equivalent, but their
1476 canonical types were different. This is a failure of the
1477 canonical type propagation code.*/
1479 ("canonical types differ for identical types %qT and %qT",
1481 else if (!result
&& TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
))
1482 /* Two types are structurally different, but the canonical
1483 types are the same. This means we were over-eager in
1484 assigning canonical types. */
1486 ("same canonical type node for different types %qT and %qT",
1491 if (!flag_checking
&& USE_CANONICAL_TYPES
)
1492 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1494 return structural_comptypes (t1
, t2
, strict
);
1496 else if (strict
== COMPARE_STRUCTURAL
)
1497 return structural_comptypes (t1
, t2
, COMPARE_STRICT
);
1499 return structural_comptypes (t1
, t2
, strict
);
1502 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1503 top-level qualifiers. */
1506 same_type_ignoring_top_level_qualifiers_p (tree type1
, tree type2
)
1508 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1511 type1
= cp_build_qualified_type (type1
, TYPE_UNQUALIFIED
);
1512 type2
= cp_build_qualified_type (type2
, TYPE_UNQUALIFIED
);
1513 return same_type_p (type1
, type2
);
1516 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1519 at_least_as_qualified_p (const_tree type1
, const_tree type2
)
1521 int q1
= cp_type_quals (type1
);
1522 int q2
= cp_type_quals (type2
);
1524 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1525 return (q1
& q2
) == q2
;
1528 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1529 more cv-qualified that TYPE1, and 0 otherwise. */
1532 comp_cv_qualification (int q1
, int q2
)
1537 if ((q1
& q2
) == q2
)
1539 else if ((q1
& q2
) == q1
)
1546 comp_cv_qualification (const_tree type1
, const_tree type2
)
1548 int q1
= cp_type_quals (type1
);
1549 int q2
= cp_type_quals (type2
);
1550 return comp_cv_qualification (q1
, q2
);
1553 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1554 subset of the cv-qualification signature of TYPE2, and the types
1555 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1558 comp_cv_qual_signature (tree type1
, tree type2
)
1560 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1562 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1568 /* Subroutines of `comptypes'. */
1570 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1571 equivalent in the sense that functions with those parameter types
1572 can have equivalent types. The two lists must be equivalent,
1573 element by element. */
1576 compparms (const_tree parms1
, const_tree parms2
)
1580 /* An unspecified parmlist matches any specified parmlist
1581 whose argument types don't need default promotions. */
1583 for (t1
= parms1
, t2
= parms2
;
1585 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1587 /* If one parmlist is shorter than the other,
1588 they fail to match. */
1591 if (!same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1598 /* Process a sizeof or alignof expression where the operand is a
1599 type. STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
1600 or GNU (preferred alignment) semantics; it is ignored if op is
1604 cxx_sizeof_or_alignof_type (tree type
, enum tree_code op
, bool std_alignof
,
1607 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
1608 if (type
== error_mark_node
)
1609 return error_mark_node
;
1611 type
= non_reference (type
);
1612 if (TREE_CODE (type
) == METHOD_TYPE
)
1616 pedwarn (input_location
, OPT_Wpointer_arith
,
1617 "invalid application of %qs to a member function",
1618 OVL_OP_INFO (false, op
)->name
);
1619 return size_one_node
;
1622 return error_mark_node
;
1625 bool dependent_p
= dependent_type_p (type
);
1627 complete_type (type
);
1629 /* VLA types will have a non-constant size. In the body of an
1630 uninstantiated template, we don't need to try to compute the
1631 value, because the sizeof expression is not an integral
1632 constant expression in that case. And, if we do try to
1633 compute the value, we'll likely end up with SAVE_EXPRs, which
1634 the template substitution machinery does not expect to see. */
1635 || (processing_template_decl
1636 && COMPLETE_TYPE_P (type
)
1637 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
))
1639 tree value
= build_min (op
, size_type_node
, type
);
1640 TREE_READONLY (value
) = 1;
1641 if (op
== ALIGNOF_EXPR
&& std_alignof
)
1642 ALIGNOF_EXPR_STD_P (value
) = true;
1646 return c_sizeof_or_alignof_type (input_location
, complete_type (type
),
1647 op
== SIZEOF_EXPR
, std_alignof
,
1651 /* Return the size of the type, without producing any warnings for
1652 types whose size cannot be taken. This routine should be used only
1653 in some other routine that has already produced a diagnostic about
1654 using the size of such a type. */
1656 cxx_sizeof_nowarn (tree type
)
1658 if (TREE_CODE (type
) == FUNCTION_TYPE
1659 || VOID_TYPE_P (type
)
1660 || TREE_CODE (type
) == ERROR_MARK
)
1661 return size_one_node
;
1662 else if (!COMPLETE_TYPE_P (type
))
1663 return size_zero_node
;
1665 return cxx_sizeof_or_alignof_type (type
, SIZEOF_EXPR
, false, false);
1668 /* Process a sizeof expression where the operand is an expression. */
1671 cxx_sizeof_expr (tree e
, tsubst_flags_t complain
)
1673 if (e
== error_mark_node
)
1674 return error_mark_node
;
1676 if (processing_template_decl
)
1678 e
= build_min (SIZEOF_EXPR
, size_type_node
, e
);
1679 TREE_SIDE_EFFECTS (e
) = 0;
1680 TREE_READONLY (e
) = 1;
1685 STRIP_ANY_LOCATION_WRAPPER (e
);
1687 /* To get the size of a static data member declared as an array of
1688 unknown bound, we need to instantiate it. */
1690 && VAR_HAD_UNKNOWN_BOUND (e
)
1691 && DECL_TEMPLATE_INSTANTIATION (e
))
1692 instantiate_decl (e
, /*defer_ok*/true, /*expl_inst_mem*/false);
1694 if (TREE_CODE (e
) == PARM_DECL
1695 && DECL_ARRAY_PARAMETER_P (e
)
1696 && (complain
& tf_warning
))
1698 auto_diagnostic_group d
;
1699 if (warning (OPT_Wsizeof_array_argument
, "%<sizeof%> on array function "
1700 "parameter %qE will return size of %qT", e
, TREE_TYPE (e
)))
1701 inform (DECL_SOURCE_LOCATION (e
), "declared here");
1704 e
= mark_type_use (e
);
1708 if (complain
& tf_error
)
1709 error ("invalid application of %<sizeof%> to a bit-field");
1711 return error_mark_node
;
1714 else if (is_overloaded_fn (e
))
1716 if (complain
& tf_error
)
1717 permerror (input_location
, "ISO C++ forbids applying %<sizeof%> to an expression of "
1720 return error_mark_node
;
1723 else if (type_unknown_p (e
))
1725 if (complain
& tf_error
)
1726 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1728 return error_mark_node
;
1734 return cxx_sizeof_or_alignof_type (e
, SIZEOF_EXPR
, false, complain
& tf_error
);
1737 /* Implement the __alignof keyword: Return the minimum required
1738 alignment of E, measured in bytes. For VAR_DECL's and
1739 FIELD_DECL's return DECL_ALIGN (which can be set from an
1740 "aligned" __attribute__ specification). */
1743 cxx_alignof_expr (tree e
, tsubst_flags_t complain
)
1747 if (e
== error_mark_node
)
1748 return error_mark_node
;
1750 if (processing_template_decl
)
1752 e
= build_min (ALIGNOF_EXPR
, size_type_node
, e
);
1753 TREE_SIDE_EFFECTS (e
) = 0;
1754 TREE_READONLY (e
) = 1;
1759 STRIP_ANY_LOCATION_WRAPPER (e
);
1761 e
= mark_type_use (e
);
1764 t
= size_int (DECL_ALIGN_UNIT (e
));
1765 else if (bitfield_p (e
))
1767 if (complain
& tf_error
)
1768 error ("invalid application of %<__alignof%> to a bit-field");
1770 return error_mark_node
;
1773 else if (TREE_CODE (e
) == COMPONENT_REF
1774 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
)
1775 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e
, 1)));
1776 else if (is_overloaded_fn (e
))
1778 if (complain
& tf_error
)
1779 permerror (input_location
, "ISO C++ forbids applying %<__alignof%> to an expression of "
1782 return error_mark_node
;
1783 if (TREE_CODE (e
) == FUNCTION_DECL
)
1784 t
= size_int (DECL_ALIGN_UNIT (e
));
1788 else if (type_unknown_p (e
))
1790 if (complain
& tf_error
)
1791 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1793 return error_mark_node
;
1797 return cxx_sizeof_or_alignof_type (TREE_TYPE (e
), ALIGNOF_EXPR
, false,
1798 complain
& tf_error
);
1800 return fold_convert (size_type_node
, t
);
1803 /* Process a sizeof or alignof expression E with code OP where the operand
1804 is an expression. */
1807 cxx_sizeof_or_alignof_expr (tree e
, enum tree_code op
, bool complain
)
1809 if (op
== SIZEOF_EXPR
)
1810 return cxx_sizeof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1812 return cxx_alignof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1815 /* Build a representation of an expression 'alignas(E).' Return the
1816 folded integer value of E if it is an integral constant expression
1817 that resolves to a valid alignment. If E depends on a template
1818 parameter, return a syntactic representation tree of kind
1819 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1820 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1823 cxx_alignas_expr (tree e
)
1825 if (e
== NULL_TREE
|| e
== error_mark_node
1826 || (!TYPE_P (e
) && !require_potential_rvalue_constant_expression (e
)))
1832 When the alignment-specifier is of the form
1833 alignas(type-id ), it shall have the same effect as
1834 alignas(alignof(type-id )). */
1836 return cxx_sizeof_or_alignof_type (e
, ALIGNOF_EXPR
, true, false);
1838 /* If we reach this point, it means the alignas expression if of
1839 the form "alignas(assignment-expression)", so we should follow
1840 what is stated by [dcl.align]/2. */
1842 if (value_dependent_expression_p (e
))
1843 /* Leave value-dependent expression alone for now. */
1846 e
= instantiate_non_dependent_expr (e
);
1847 e
= mark_rvalue_use (e
);
1849 /* [dcl.align]/2 says:
1851 the assignment-expression shall be an integral constant
1854 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e
)))
1856 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e
));
1857 return error_mark_node
;
1860 return cxx_constant_value (e
);
1864 /* EXPR is being used in a context that is not a function call.
1869 The expression can be used only as the left-hand operand of a
1870 member function call.
1872 [expr.mptr.operator]
1874 If the result of .* or ->* is a function, then that result can be
1875 used only as the operand for the function call operator ().
1877 by issuing an error message if appropriate. Returns true iff EXPR
1878 violates these rules. */
1881 invalid_nonstatic_memfn_p (location_t loc
, tree expr
, tsubst_flags_t complain
)
1883 if (expr
== NULL_TREE
)
1885 /* Don't enforce this in MS mode. */
1886 if (flag_ms_extensions
)
1888 if (is_overloaded_fn (expr
) && !really_overloaded_fn (expr
))
1889 expr
= get_first_fn (expr
);
1890 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr
))
1892 if (complain
& tf_error
)
1896 error_at (loc
, "invalid use of non-static member function %qD",
1898 inform (DECL_SOURCE_LOCATION (expr
), "declared here");
1901 error_at (loc
, "invalid use of non-static member function of "
1902 "type %qT", TREE_TYPE (expr
));
1909 /* If EXP is a reference to a bitfield, and the type of EXP does not
1910 match the declared type of the bitfield, return the declared type
1911 of the bitfield. Otherwise, return NULL_TREE. */
1914 is_bitfield_expr_with_lowered_type (const_tree exp
)
1916 switch (TREE_CODE (exp
))
1919 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1)
1920 ? TREE_OPERAND (exp
, 1)
1921 : TREE_OPERAND (exp
, 0)))
1923 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 2));
1926 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1));
1930 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1936 field
= TREE_OPERAND (exp
, 1);
1937 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_BIT_FIELD_TYPE (field
))
1939 if (same_type_ignoring_top_level_qualifiers_p
1940 (TREE_TYPE (exp
), DECL_BIT_FIELD_TYPE (field
)))
1942 return DECL_BIT_FIELD_TYPE (field
);
1946 if (DECL_HAS_VALUE_EXPR_P (exp
))
1947 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
1948 (CONST_CAST_TREE (exp
)));
1951 case VIEW_CONVERT_EXPR
:
1952 if (location_wrapper_p (exp
))
1953 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1958 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp
, 0)))
1959 == TYPE_MAIN_VARIANT (TREE_TYPE (exp
)))
1960 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1968 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1969 bitfield with a lowered type, the type of EXP is returned, rather
1973 unlowered_expr_type (const_tree exp
)
1976 tree etype
= TREE_TYPE (exp
);
1978 type
= is_bitfield_expr_with_lowered_type (exp
);
1980 type
= cp_build_qualified_type (type
, cp_type_quals (etype
));
1987 /* Perform the conversions in [expr] that apply when an lvalue appears
1988 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1989 function-to-pointer conversions. In addition, bitfield references are
1990 converted to their declared types. Note that this function does not perform
1991 the lvalue-to-rvalue conversion for class types. If you need that conversion
1992 for class types, then you probably need to use force_rvalue.
1994 Although the returned value is being used as an rvalue, this
1995 function does not wrap the returned expression in a
1996 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1997 that the return value is no longer an lvalue. */
2000 decay_conversion (tree exp
,
2001 tsubst_flags_t complain
,
2002 bool reject_builtin
/* = true */)
2005 enum tree_code code
;
2006 location_t loc
= cp_expr_loc_or_loc (exp
, input_location
);
2008 type
= TREE_TYPE (exp
);
2009 if (type
== error_mark_node
)
2010 return error_mark_node
;
2012 exp
= resolve_nondeduced_context_or_error (exp
, complain
);
2014 code
= TREE_CODE (type
);
2016 if (error_operand_p (exp
))
2017 return error_mark_node
;
2019 if (NULLPTR_TYPE_P (type
) && !TREE_SIDE_EFFECTS (exp
))
2021 mark_rvalue_use (exp
, loc
, reject_builtin
);
2022 return nullptr_node
;
2025 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2026 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2027 if (code
== VOID_TYPE
)
2029 if (complain
& tf_error
)
2030 error_at (loc
, "void value not ignored as it ought to be");
2031 return error_mark_node
;
2033 if (invalid_nonstatic_memfn_p (loc
, exp
, complain
))
2034 return error_mark_node
;
2035 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
2037 exp
= mark_lvalue_use (exp
);
2038 if (reject_builtin
&& reject_gcc_builtin (exp
, loc
))
2039 return error_mark_node
;
2040 return cp_build_addr_expr (exp
, complain
);
2042 if (code
== ARRAY_TYPE
)
2047 exp
= mark_lvalue_use (exp
);
2049 if (INDIRECT_REF_P (exp
))
2050 return build_nop (build_pointer_type (TREE_TYPE (type
)),
2051 TREE_OPERAND (exp
, 0));
2053 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
2055 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1), complain
);
2056 if (op1
== error_mark_node
)
2057 return error_mark_node
;
2058 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
2059 TREE_OPERAND (exp
, 0), op1
);
2062 if (!obvalue_p (exp
)
2063 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
2065 if (complain
& tf_error
)
2066 error_at (loc
, "invalid use of non-lvalue array");
2067 return error_mark_node
;
2070 /* Don't let an array compound literal decay to a pointer. It can
2071 still be used to initialize an array or bind to a reference. */
2072 if (TREE_CODE (exp
) == TARGET_EXPR
)
2074 if (complain
& tf_error
)
2075 error_at (loc
, "taking address of temporary array");
2076 return error_mark_node
;
2079 ptrtype
= build_pointer_type (TREE_TYPE (type
));
2083 if (!cxx_mark_addressable (exp
))
2084 return error_mark_node
;
2085 adr
= build_nop (ptrtype
, build_address (exp
));
2088 /* This way is better for a COMPONENT_REF since it can
2089 simplify the offset for a component. */
2090 adr
= cp_build_addr_expr (exp
, complain
);
2091 return cp_convert (ptrtype
, adr
, complain
);
2094 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2095 exp
= mark_rvalue_use (exp
, loc
, reject_builtin
);
2097 /* If a bitfield is used in a context where integral promotion
2098 applies, then the caller is expected to have used
2099 default_conversion. That function promotes bitfields correctly
2100 before calling this function. At this point, if we have a
2101 bitfield referenced, we may assume that is not subject to
2102 promotion, and that, therefore, the type of the resulting rvalue
2103 is the declared type of the bitfield. */
2104 exp
= convert_bitfield_to_declared_type (exp
);
2106 /* We do not call rvalue() here because we do not want to wrap EXP
2107 in a NON_LVALUE_EXPR. */
2111 Non-class rvalues always have cv-unqualified types. */
2112 type
= TREE_TYPE (exp
);
2113 if (!CLASS_TYPE_P (type
) && cv_qualified_p (type
))
2114 exp
= build_nop (cv_unqualified (type
), exp
);
2116 if (!complete_type_or_maybe_complain (type
, exp
, complain
))
2117 return error_mark_node
;
2122 /* Perform preparatory conversions, as part of the "usual arithmetic
2123 conversions". In particular, as per [expr]:
2125 Whenever an lvalue expression appears as an operand of an
2126 operator that expects the rvalue for that operand, the
2127 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2128 standard conversions are applied to convert the expression to an
2131 In addition, we perform integral promotions here, as those are
2132 applied to both operands to a binary operator before determining
2133 what additional conversions should apply. */
2136 cp_default_conversion (tree exp
, tsubst_flags_t complain
)
2138 /* Check for target-specific promotions. */
2139 tree promoted_type
= targetm
.promoted_type (TREE_TYPE (exp
));
2141 exp
= cp_convert (promoted_type
, exp
, complain
);
2142 /* Perform the integral promotions first so that bitfield
2143 expressions (which may promote to "int", even if the bitfield is
2144 declared "unsigned") are promoted correctly. */
2145 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp
)))
2146 exp
= cp_perform_integral_promotions (exp
, complain
);
2147 /* Perform the other conversions. */
2148 exp
= decay_conversion (exp
, complain
);
2156 default_conversion (tree exp
)
2158 return cp_default_conversion (exp
, tf_warning_or_error
);
2161 /* EXPR is an expression with an integral or enumeration type.
2162 Perform the integral promotions in [conv.prom], and return the
2166 cp_perform_integral_promotions (tree expr
, tsubst_flags_t complain
)
2171 expr
= mark_rvalue_use (expr
);
2172 if (error_operand_p (expr
))
2173 return error_mark_node
;
2177 If the bitfield has an enumerated type, it is treated as any
2178 other value of that type for promotion purposes. */
2179 type
= is_bitfield_expr_with_lowered_type (expr
);
2180 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
2181 type
= TREE_TYPE (expr
);
2182 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
2183 /* Scoped enums don't promote. */
2184 if (SCOPED_ENUM_P (type
))
2186 promoted_type
= type_promotes_to (type
);
2187 if (type
!= promoted_type
)
2188 expr
= cp_convert (promoted_type
, expr
, complain
);
2195 perform_integral_promotions (tree expr
)
2197 return cp_perform_integral_promotions (expr
, tf_warning_or_error
);
2200 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2201 decay_conversion to one. */
2204 string_conv_p (const_tree totype
, const_tree exp
, int warn
)
2208 if (!TYPE_PTR_P (totype
))
2211 t
= TREE_TYPE (totype
);
2212 if (!same_type_p (t
, char_type_node
)
2213 && !same_type_p (t
, char8_type_node
)
2214 && !same_type_p (t
, char16_type_node
)
2215 && !same_type_p (t
, char32_type_node
)
2216 && !same_type_p (t
, wchar_type_node
))
2219 location_t loc
= EXPR_LOC_OR_LOC (exp
, input_location
);
2221 STRIP_ANY_LOCATION_WRAPPER (exp
);
2223 if (TREE_CODE (exp
) == STRING_CST
)
2225 /* Make sure that we don't try to convert between char and wide chars. */
2226 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
2231 /* Is this a string constant which has decayed to 'const char *'? */
2232 t
= build_pointer_type (cp_build_qualified_type (t
, TYPE_QUAL_CONST
));
2233 if (!same_type_p (TREE_TYPE (exp
), t
))
2236 if (TREE_CODE (exp
) != ADDR_EXPR
2237 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
2242 if (cxx_dialect
>= cxx11
)
2243 pedwarn (loc
, OPT_Wwrite_strings
,
2244 "ISO C++ forbids converting a string constant to %qT",
2247 warning_at (loc
, OPT_Wwrite_strings
,
2248 "deprecated conversion from string constant to %qT",
2255 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2256 can, for example, use as an lvalue. This code used to be in
2257 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2258 expressions, where we're dealing with aggregates. But now it's again only
2259 called from unary_complex_lvalue. The case (in particular) that led to
2260 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2264 rationalize_conditional_expr (enum tree_code code
, tree t
,
2265 tsubst_flags_t complain
)
2267 location_t loc
= cp_expr_loc_or_loc (t
, input_location
);
2269 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2270 the first operand is always the one to be used if both operands
2271 are equal, so we know what conditional expression this used to be. */
2272 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
2274 tree op0
= TREE_OPERAND (t
, 0);
2275 tree op1
= TREE_OPERAND (t
, 1);
2277 /* The following code is incorrect if either operand side-effects. */
2278 gcc_assert (!TREE_SIDE_EFFECTS (op0
)
2279 && !TREE_SIDE_EFFECTS (op1
));
2281 build_conditional_expr (loc
,
2282 build_x_binary_op (loc
,
2283 (TREE_CODE (t
) == MIN_EXPR
2284 ? LE_EXPR
: GE_EXPR
),
2285 op0
, TREE_CODE (op0
),
2286 op1
, TREE_CODE (op1
),
2289 cp_build_unary_op (code
, op0
, false, complain
),
2290 cp_build_unary_op (code
, op1
, false, complain
),
2295 build_conditional_expr (loc
, TREE_OPERAND (t
, 0),
2296 cp_build_unary_op (code
, TREE_OPERAND (t
, 1), false,
2298 cp_build_unary_op (code
, TREE_OPERAND (t
, 2), false,
2303 /* Given the TYPE of an anonymous union field inside T, return the
2304 FIELD_DECL for the field. If not found return NULL_TREE. Because
2305 anonymous unions can nest, we must also search all anonymous unions
2306 that are directly reachable. */
2309 lookup_anon_field (tree t
, tree type
)
2313 t
= TYPE_MAIN_VARIANT (t
);
2315 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
2317 if (TREE_STATIC (field
))
2319 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
2322 /* If we find it directly, return the field. */
2323 if (DECL_NAME (field
) == NULL_TREE
2324 && type
== TYPE_MAIN_VARIANT (TREE_TYPE (field
)))
2329 /* Otherwise, it could be nested, search harder. */
2330 if (DECL_NAME (field
) == NULL_TREE
2331 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2333 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
2341 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2342 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2343 non-NULL, it indicates the path to the base used to name MEMBER.
2344 If PRESERVE_REFERENCE is true, the expression returned will have
2345 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2346 returned will have the type referred to by the reference.
2348 This function does not perform access control; that is either done
2349 earlier by the parser when the name of MEMBER is resolved to MEMBER
2350 itself, or later when overload resolution selects one of the
2351 functions indicated by MEMBER. */
2354 build_class_member_access_expr (cp_expr object
, tree member
,
2355 tree access_path
, bool preserve_reference
,
2356 tsubst_flags_t complain
)
2360 tree result
= NULL_TREE
;
2361 tree using_decl
= NULL_TREE
;
2363 if (error_operand_p (object
) || error_operand_p (member
))
2364 return error_mark_node
;
2366 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2370 The type of the first expression shall be "class object" (of a
2372 object_type
= TREE_TYPE (object
);
2373 if (!currently_open_class (object_type
)
2374 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2375 return error_mark_node
;
2376 if (!CLASS_TYPE_P (object_type
))
2378 if (complain
& tf_error
)
2380 if (INDIRECT_TYPE_P (object_type
)
2381 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2382 error ("request for member %qD in %qE, which is of pointer "
2383 "type %qT (maybe you meant to use %<->%> ?)",
2384 member
, object
.get_value (), object_type
);
2386 error ("request for member %qD in %qE, which is of non-class "
2387 "type %qT", member
, object
.get_value (), object_type
);
2389 return error_mark_node
;
2392 /* The standard does not seem to actually say that MEMBER must be a
2393 member of OBJECT_TYPE. However, that is clearly what is
2395 if (DECL_P (member
))
2397 member_scope
= DECL_CLASS_CONTEXT (member
);
2398 if (!mark_used (member
, complain
) && !(complain
& tf_error
))
2399 return error_mark_node
;
2400 if (TREE_DEPRECATED (member
))
2401 warn_deprecated_use (member
, NULL_TREE
);
2404 member_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (member
));
2405 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2406 presently be the anonymous union. Go outwards until we find a
2407 type related to OBJECT_TYPE. */
2408 while ((ANON_AGGR_TYPE_P (member_scope
) || UNSCOPED_ENUM_P (member_scope
))
2409 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
2411 member_scope
= TYPE_CONTEXT (member_scope
);
2412 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
2414 if (complain
& tf_error
)
2416 if (TREE_CODE (member
) == FIELD_DECL
)
2417 error ("invalid use of nonstatic data member %qE", member
);
2419 error ("%qD is not a member of %qT", member
, object_type
);
2421 return error_mark_node
;
2424 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2425 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2426 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2428 tree temp
= unary_complex_lvalue (ADDR_EXPR
, object
);
2431 temp
= cp_build_fold_indirect_ref (temp
);
2432 if (xvalue_p (object
) && !xvalue_p (temp
))
2433 /* Preserve xvalue kind. */
2439 /* In [expr.ref], there is an explicit list of the valid choices for
2440 MEMBER. We check for each of those cases here. */
2443 /* A static data member. */
2445 mark_exp_read (object
);
2446 /* If OBJECT has side-effects, they are supposed to occur. */
2447 if (TREE_SIDE_EFFECTS (object
))
2448 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
2450 else if (TREE_CODE (member
) == FIELD_DECL
)
2452 /* A non-static data member. */
2457 if (INDIRECT_REF_P (object
))
2459 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object
, 0)));
2461 null_object_p
= false;
2463 /* Convert OBJECT to the type of MEMBER. */
2464 if (!same_type_p (TYPE_MAIN_VARIANT (object_type
),
2465 TYPE_MAIN_VARIANT (member_scope
)))
2470 binfo
= lookup_base (access_path
? access_path
: object_type
,
2471 member_scope
, ba_unique
, &kind
, complain
);
2472 if (binfo
== error_mark_node
)
2473 return error_mark_node
;
2475 /* It is invalid to try to get to a virtual base of a
2476 NULL object. The most common cause is invalid use of
2478 if (null_object_p
&& kind
== bk_via_virtual
)
2480 if (complain
& tf_error
)
2482 error ("invalid access to non-static data member %qD in "
2483 "virtual base of NULL object", member
);
2485 return error_mark_node
;
2488 /* Convert to the base. */
2489 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
2490 /*nonnull=*/1, complain
);
2491 /* If we found the base successfully then we should be able
2492 to convert to it successfully. */
2493 gcc_assert (object
!= error_mark_node
);
2496 /* If MEMBER is from an anonymous aggregate, we have converted
2497 OBJECT so that it refers to the class containing the
2498 anonymous union. Generate a reference to the anonymous union
2499 itself, and recur to find MEMBER. */
2500 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
))
2501 /* When this code is called from build_field_call, the
2502 object already has the type of the anonymous union.
2503 That is because the COMPONENT_REF was already
2504 constructed, and was then disassembled before calling
2505 build_field_call. After the function-call code is
2506 cleaned up, this waste can be eliminated. */
2507 && (!same_type_ignoring_top_level_qualifiers_p
2508 (TREE_TYPE (object
), DECL_CONTEXT (member
))))
2510 tree anonymous_union
;
2512 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
2513 DECL_CONTEXT (member
));
2514 object
= build_class_member_access_expr (object
,
2516 /*access_path=*/NULL_TREE
,
2521 /* Compute the type of the field, as described in [expr.ref]. */
2522 type_quals
= TYPE_UNQUALIFIED
;
2523 member_type
= TREE_TYPE (member
);
2524 if (!TYPE_REF_P (member_type
))
2526 type_quals
= (cp_type_quals (member_type
)
2527 | cp_type_quals (object_type
));
2529 /* A field is const (volatile) if the enclosing object, or the
2530 field itself, is const (volatile). But, a mutable field is
2531 not const, even within a const object. */
2532 if (DECL_MUTABLE_P (member
))
2533 type_quals
&= ~TYPE_QUAL_CONST
;
2534 member_type
= cp_build_qualified_type (member_type
, type_quals
);
2537 result
= build3_loc (input_location
, COMPONENT_REF
, member_type
,
2538 object
, member
, NULL_TREE
);
2540 /* Mark the expression const or volatile, as appropriate. Even
2541 though we've dealt with the type above, we still have to mark the
2542 expression itself. */
2543 if (type_quals
& TYPE_QUAL_CONST
)
2544 TREE_READONLY (result
) = 1;
2545 if (type_quals
& TYPE_QUAL_VOLATILE
)
2546 TREE_THIS_VOLATILE (result
) = 1;
2548 else if (BASELINK_P (member
))
2550 /* The member is a (possibly overloaded) member function. */
2554 /* If the MEMBER is exactly one static member function, then we
2555 know the type of the expression. Otherwise, we must wait
2556 until overload resolution has been performed. */
2557 functions
= BASELINK_FUNCTIONS (member
);
2558 if (TREE_CODE (functions
) == FUNCTION_DECL
2559 && DECL_STATIC_FUNCTION_P (functions
))
2560 type
= TREE_TYPE (functions
);
2562 type
= unknown_type_node
;
2563 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2564 base. That will happen when the function is called. */
2565 result
= build3 (COMPONENT_REF
, type
, object
, member
, NULL_TREE
);
2567 else if (TREE_CODE (member
) == CONST_DECL
)
2569 /* The member is an enumerator. */
2571 /* If OBJECT has side-effects, they are supposed to occur. */
2572 if (TREE_SIDE_EFFECTS (object
))
2573 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
2576 else if ((using_decl
= strip_using_decl (member
)) != member
)
2577 result
= build_class_member_access_expr (object
,
2579 access_path
, preserve_reference
,
2583 if (complain
& tf_error
)
2584 error ("invalid use of %qD", member
);
2585 return error_mark_node
;
2588 if (!preserve_reference
)
2591 If E2 is declared to have type "reference to T", then ... the
2592 type of E1.E2 is T. */
2593 result
= convert_from_reference (result
);
2598 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2599 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2602 lookup_destructor (tree object
, tree scope
, tree dtor_name
,
2603 tsubst_flags_t complain
)
2605 tree object_type
= TREE_TYPE (object
);
2606 tree dtor_type
= TREE_OPERAND (dtor_name
, 0);
2609 /* We've already complained about this destructor. */
2610 if (dtor_type
== error_mark_node
)
2611 return error_mark_node
;
2613 if (scope
&& !check_dtor_name (scope
, dtor_type
))
2615 if (complain
& tf_error
)
2616 error ("qualified type %qT does not match destructor name ~%qT",
2618 return error_mark_node
;
2620 if (is_auto (dtor_type
))
2621 dtor_type
= object_type
;
2622 else if (identifier_p (dtor_type
))
2624 /* In a template, names we can't find a match for are still accepted
2625 destructor names, and we check them here. */
2626 if (check_dtor_name (object_type
, dtor_type
))
2627 dtor_type
= object_type
;
2630 if (complain
& tf_error
)
2631 error ("object type %qT does not match destructor name ~%qT",
2632 object_type
, dtor_type
);
2633 return error_mark_node
;
2637 else if (!DERIVED_FROM_P (dtor_type
, TYPE_MAIN_VARIANT (object_type
)))
2639 if (complain
& tf_error
)
2640 error ("the type being destroyed is %qT, but the destructor "
2641 "refers to %qT", TYPE_MAIN_VARIANT (object_type
), dtor_type
);
2642 return error_mark_node
;
2644 expr
= lookup_member (dtor_type
, complete_dtor_identifier
,
2645 /*protect=*/1, /*want_type=*/false,
2646 tf_warning_or_error
);
2649 if (complain
& tf_error
)
2650 cxx_incomplete_type_error (dtor_name
, dtor_type
);
2651 return error_mark_node
;
2653 expr
= (adjust_result_of_qualified_name_lookup
2654 (expr
, dtor_type
, object_type
));
2655 if (scope
== NULL_TREE
)
2656 /* We need to call adjust_result_of_qualified_name_lookup in case the
2657 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2658 that we still get virtual function binding. */
2659 BASELINK_QUALIFIED_P (expr
) = false;
2663 /* An expression of the form "A::template B" has been resolved to
2664 DECL. Issue a diagnostic if B is not a template or template
2668 check_template_keyword (tree decl
)
2670 /* The standard says:
2674 If a name prefixed by the keyword template is not a member
2675 template, the program is ill-formed.
2677 DR 228 removed the restriction that the template be a member
2680 DR 96, if accepted would add the further restriction that explicit
2681 template arguments must be provided if the template keyword is
2682 used, but, as of 2005-10-16, that DR is still in "drafting". If
2683 this DR is accepted, then the semantic checks here can be
2684 simplified, as the entity named must in fact be a template
2685 specialization, rather than, as at present, a set of overloaded
2686 functions containing at least one template function. */
2687 if (TREE_CODE (decl
) != TEMPLATE_DECL
2688 && TREE_CODE (decl
) != TEMPLATE_ID_EXPR
)
2692 if (DECL_USE_TEMPLATE (decl
)
2693 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2696 permerror (input_location
, "%qD is not a template", decl
);
2698 else if (!is_overloaded_fn (decl
))
2699 permerror (input_location
, "%qD is not a template", decl
);
2704 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
2705 !found
&& iter
; ++iter
)
2708 if (TREE_CODE (fn
) == TEMPLATE_DECL
2709 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
2710 || (TREE_CODE (fn
) == FUNCTION_DECL
2711 && DECL_USE_TEMPLATE (fn
)
2712 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn
))))
2716 permerror (input_location
, "%qD is not a template", decl
);
2721 /* Record that an access failure occurred on BASETYPE_PATH attempting
2722 to access DECL, where DIAG_DECL should be used for diagnostics. */
2725 access_failure_info::record_access_failure (tree basetype_path
,
2726 tree decl
, tree diag_decl
)
2728 m_was_inaccessible
= true;
2729 m_basetype_path
= basetype_path
;
2731 m_diag_decl
= diag_decl
;
2734 /* If an access failure was recorded, then attempt to locate an
2735 accessor function for the pertinent field.
2736 Otherwise, return NULL_TREE. */
2739 access_failure_info::get_any_accessor (bool const_p
) const
2741 if (!was_inaccessible_p ())
2745 = locate_field_accessor (m_basetype_path
, m_diag_decl
, const_p
);
2749 /* The accessor must itself be accessible for it to be a reasonable
2751 if (!accessible_p (m_basetype_path
, accessor
, true))
2757 /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
2758 replacing the primary location in RICHLOC with "accessor()". */
2761 access_failure_info::add_fixit_hint (rich_location
*richloc
,
2765 pp_printf (&pp
, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor_decl
)));
2766 richloc
->add_fixit_replace (pp_formatted_text (&pp
));
2769 /* If an access failure was recorded, then attempt to locate an
2770 accessor function for the pertinent field, and if one is
2771 available, add a note and fix-it hint suggesting using it. */
2774 access_failure_info::maybe_suggest_accessor (bool const_p
) const
2776 tree accessor
= get_any_accessor (const_p
);
2777 if (accessor
== NULL_TREE
)
2779 rich_location
richloc (line_table
, input_location
);
2780 add_fixit_hint (&richloc
, accessor
);
2781 inform (&richloc
, "field %q#D can be accessed via %q#D",
2782 m_diag_decl
, accessor
);
2785 /* Subroutine of finish_class_member_access_expr.
2786 Issue an error about NAME not being a member of ACCESS_PATH (or
2787 OBJECT_TYPE), potentially providing a fix-it hint for misspelled
2791 complain_about_unrecognized_member (tree access_path
, tree name
,
2794 /* Attempt to provide a hint about misspelled names. */
2795 tree guessed_id
= lookup_member_fuzzy (access_path
, name
,
2796 /*want_type=*/false);
2797 if (guessed_id
== NULL_TREE
)
2800 error ("%q#T has no member named %qE",
2801 TREE_CODE (access_path
) == TREE_BINFO
2802 ? TREE_TYPE (access_path
) : object_type
, name
);
2806 location_t bogus_component_loc
= input_location
;
2807 gcc_rich_location
rich_loc (bogus_component_loc
);
2809 /* Check that the guessed name is accessible along access_path. */
2810 access_failure_info afi
;
2811 lookup_member (access_path
, guessed_id
, /*protect=*/1,
2812 /*want_type=*/false, /*complain=*/false,
2814 if (afi
.was_inaccessible_p ())
2816 tree accessor
= afi
.get_any_accessor (TYPE_READONLY (object_type
));
2819 /* The guessed name isn't directly accessible, but can be accessed
2820 via an accessor member function. */
2821 afi
.add_fixit_hint (&rich_loc
, accessor
);
2822 error_at (&rich_loc
,
2823 "%q#T has no member named %qE;"
2824 " did you mean %q#D? (accessible via %q#D)",
2825 TREE_CODE (access_path
) == TREE_BINFO
2826 ? TREE_TYPE (access_path
) : object_type
,
2827 name
, afi
.get_diag_decl (), accessor
);
2831 /* The guessed name isn't directly accessible, and no accessor
2832 member function could be found. */
2833 error_at (&rich_loc
,
2834 "%q#T has no member named %qE;"
2835 " did you mean %q#D? (not accessible from this context)",
2836 TREE_CODE (access_path
) == TREE_BINFO
2837 ? TREE_TYPE (access_path
) : object_type
,
2838 name
, afi
.get_diag_decl ());
2839 complain_about_access (afi
.get_decl (), afi
.get_diag_decl (), false);
2844 /* The guessed name is directly accessible; suggest it. */
2845 rich_loc
.add_fixit_misspelled_id (bogus_component_loc
,
2847 error_at (&rich_loc
,
2848 "%q#T has no member named %qE;"
2849 " did you mean %qE?",
2850 TREE_CODE (access_path
) == TREE_BINFO
2851 ? TREE_TYPE (access_path
) : object_type
,
2856 /* This function is called by the parser to process a class member
2857 access expression of the form OBJECT.NAME. NAME is a node used by
2858 the parser to represent a name; it is not yet a DECL. It may,
2859 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2860 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2861 there is no reason to do the lookup twice, so the parser keeps the
2862 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2863 be a template via the use of the "A::template B" syntax. */
2866 finish_class_member_access_expr (cp_expr object
, tree name
, bool template_p
,
2867 tsubst_flags_t complain
)
2872 tree access_path
= NULL_TREE
;
2873 tree orig_object
= object
;
2874 tree orig_name
= name
;
2876 if (object
== error_mark_node
|| name
== error_mark_node
)
2877 return error_mark_node
;
2879 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2880 if (!objc_is_public (object
, name
))
2881 return error_mark_node
;
2883 object_type
= TREE_TYPE (object
);
2885 if (processing_template_decl
)
2887 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2888 type_dependent_object_expression_p (object
)
2889 /* If NAME is "f<args>", where either 'f' or 'args' is
2890 dependent, then the expression is dependent. */
2891 || (TREE_CODE (name
) == TEMPLATE_ID_EXPR
2892 && dependent_template_id_p (TREE_OPERAND (name
, 0),
2893 TREE_OPERAND (name
, 1)))
2894 /* If NAME is "T::X" where "T" is dependent, then the
2895 expression is dependent. */
2896 || (TREE_CODE (name
) == SCOPE_REF
2897 && TYPE_P (TREE_OPERAND (name
, 0))
2898 && dependent_scope_p (TREE_OPERAND (name
, 0)))
2899 /* If NAME is operator T where "T" is dependent, we can't
2900 lookup until we instantiate the T. */
2901 || (TREE_CODE (name
) == IDENTIFIER_NODE
2902 && IDENTIFIER_CONV_OP_P (name
)
2903 && dependent_type_p (TREE_TYPE (name
))))
2906 return build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
,
2907 orig_object
, orig_name
, NULL_TREE
);
2909 object
= build_non_dependent_expr (object
);
2911 else if (c_dialect_objc ()
2912 && identifier_p (name
)
2913 && (expr
= objc_maybe_build_component_ref (object
, name
)))
2918 The type of the first expression shall be "class object" (of a
2920 if (!currently_open_class (object_type
)
2921 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2922 return error_mark_node
;
2923 if (!CLASS_TYPE_P (object_type
))
2925 if (complain
& tf_error
)
2927 if (INDIRECT_TYPE_P (object_type
)
2928 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2929 error ("request for member %qD in %qE, which is of pointer "
2930 "type %qT (maybe you meant to use %<->%> ?)",
2931 name
, object
.get_value (), object_type
);
2933 error ("request for member %qD in %qE, which is of non-class "
2934 "type %qT", name
, object
.get_value (), object_type
);
2936 return error_mark_node
;
2939 if (BASELINK_P (name
))
2940 /* A member function that has already been looked up. */
2944 bool is_template_id
= false;
2945 tree template_args
= NULL_TREE
;
2946 tree scope
= NULL_TREE
;
2948 access_path
= object_type
;
2950 if (TREE_CODE (name
) == SCOPE_REF
)
2952 /* A qualified name. The qualifying class or namespace `S'
2953 has already been looked up; it is either a TYPE or a
2955 scope
= TREE_OPERAND (name
, 0);
2956 name
= TREE_OPERAND (name
, 1);
2958 /* If SCOPE is a namespace, then the qualified name does not
2959 name a member of OBJECT_TYPE. */
2960 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
2962 if (complain
& tf_error
)
2963 error ("%<%D::%D%> is not a member of %qT",
2964 scope
, name
, object_type
);
2965 return error_mark_node
;
2969 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
2971 is_template_id
= true;
2972 template_args
= TREE_OPERAND (name
, 1);
2973 name
= TREE_OPERAND (name
, 0);
2975 if (!identifier_p (name
))
2976 name
= OVL_NAME (name
);
2981 if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
2983 gcc_assert (!is_template_id
);
2984 /* Looking up a member enumerator (c++/56793). */
2985 if (!TYPE_CLASS_SCOPE_P (scope
)
2986 || !DERIVED_FROM_P (TYPE_CONTEXT (scope
), object_type
))
2988 if (complain
& tf_error
)
2989 error ("%<%D::%D%> is not a member of %qT",
2990 scope
, name
, object_type
);
2991 return error_mark_node
;
2993 tree val
= lookup_enumerator (scope
, name
);
2996 if (complain
& tf_error
)
2997 error ("%qD is not a member of %qD",
2999 return error_mark_node
;
3002 if (TREE_SIDE_EFFECTS (object
))
3003 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
), object
, val
);
3007 gcc_assert (CLASS_TYPE_P (scope
));
3008 gcc_assert (identifier_p (name
) || TREE_CODE (name
) == BIT_NOT_EXPR
);
3010 if (constructor_name_p (name
, scope
))
3012 if (complain
& tf_error
)
3013 error ("cannot call constructor %<%T::%D%> directly",
3015 return error_mark_node
;
3018 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3019 access_path
= lookup_base (object_type
, scope
, ba_check
,
3021 if (access_path
== error_mark_node
)
3022 return error_mark_node
;
3025 if (any_dependent_bases_p (object_type
))
3027 if (complain
& tf_error
)
3028 error ("%qT is not a base of %qT", scope
, object_type
);
3029 return error_mark_node
;
3033 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
3035 if (dependent_type_p (object_type
))
3036 /* The destructor isn't declared yet. */
3038 member
= lookup_destructor (object
, scope
, name
, complain
);
3042 /* Look up the member. */
3043 access_failure_info afi
;
3044 member
= lookup_member (access_path
, name
, /*protect=*/1,
3045 /*want_type=*/false, complain
,
3047 afi
.maybe_suggest_accessor (TYPE_READONLY (object_type
));
3048 if (member
== NULL_TREE
)
3050 if (dependent_type_p (object_type
))
3051 /* Try again at instantiation time. */
3053 if (complain
& tf_error
)
3054 complain_about_unrecognized_member (access_path
, name
,
3056 return error_mark_node
;
3058 if (member
== error_mark_node
)
3059 return error_mark_node
;
3061 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member
)))
3062 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3063 wrong, so don't use it. */
3065 if (TREE_CODE (member
) == USING_DECL
&& DECL_DEPENDENT_P (member
))
3071 tree templ
= member
;
3073 if (BASELINK_P (templ
))
3074 member
= lookup_template_function (templ
, template_args
);
3075 else if (variable_template_p (templ
))
3076 member
= (lookup_and_finish_template_variable
3077 (templ
, template_args
, complain
));
3080 if (complain
& tf_error
)
3081 error ("%qD is not a member template function", name
);
3082 return error_mark_node
;
3087 if (TREE_DEPRECATED (member
))
3088 warn_deprecated_use (member
, NULL_TREE
);
3091 check_template_keyword (member
);
3093 expr
= build_class_member_access_expr (object
, member
, access_path
,
3094 /*preserve_reference=*/false,
3096 if (processing_template_decl
&& expr
!= error_mark_node
)
3098 if (BASELINK_P (member
))
3100 if (TREE_CODE (orig_name
) == SCOPE_REF
)
3101 BASELINK_QUALIFIED_P (member
) = 1;
3104 return build_min_non_dep (COMPONENT_REF
, expr
,
3105 orig_object
, orig_name
,
3112 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3116 build_simple_component_ref (tree object
, tree member
)
3118 tree type
= cp_build_qualified_type (TREE_TYPE (member
),
3119 cp_type_quals (TREE_TYPE (object
)));
3120 return build3_loc (input_location
,
3121 COMPONENT_REF
, type
,
3122 object
, member
, NULL_TREE
);
3125 /* Return an expression for the MEMBER_NAME field in the internal
3126 representation of PTRMEM, a pointer-to-member function. (Each
3127 pointer-to-member function type gets its own RECORD_TYPE so it is
3128 more convenient to access the fields by name than by FIELD_DECL.)
3129 This routine converts the NAME to a FIELD_DECL and then creates the
3130 node for the complete expression. */
3133 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
3138 if (TREE_CODE (ptrmem
) == CONSTRUCTOR
)
3142 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ptrmem
),
3144 if (index
&& DECL_P (index
) && DECL_NAME (index
) == member_name
)
3149 /* This code is a stripped down version of
3150 build_class_member_access_expr. It does not work to use that
3151 routine directly because it expects the object to be of class
3153 ptrmem_type
= TREE_TYPE (ptrmem
);
3154 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
));
3155 for (member
= TYPE_FIELDS (ptrmem_type
); member
;
3156 member
= DECL_CHAIN (member
))
3157 if (DECL_NAME (member
) == member_name
)
3159 tree res
= build_simple_component_ref (ptrmem
, member
);
3161 TREE_NO_WARNING (res
) = 1;
3165 /* Given an expression PTR for a pointer, return an expression
3166 for the value pointed to.
3167 ERRORSTRING is the name of the operator to appear in error messages.
3169 This function may need to overload OPERATOR_FNNAME.
3170 Must also handle REFERENCE_TYPEs for C++. */
3173 build_x_indirect_ref (location_t loc
, tree expr
, ref_operator errorstring
,
3174 tsubst_flags_t complain
)
3176 tree orig_expr
= expr
;
3178 tree overload
= NULL_TREE
;
3180 if (processing_template_decl
)
3182 /* Retain the type if we know the operand is a pointer. */
3183 if (TREE_TYPE (expr
) && INDIRECT_TYPE_P (TREE_TYPE (expr
)))
3184 return build_min (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
3185 if (type_dependent_expression_p (expr
))
3186 return build_min_nt_loc (loc
, INDIRECT_REF
, expr
);
3187 expr
= build_non_dependent_expr (expr
);
3190 rval
= build_new_op (loc
, INDIRECT_REF
, LOOKUP_NORMAL
, expr
,
3191 NULL_TREE
, NULL_TREE
, &overload
, complain
);
3193 rval
= cp_build_indirect_ref (expr
, errorstring
, complain
);
3195 if (processing_template_decl
&& rval
!= error_mark_node
)
3197 if (overload
!= NULL_TREE
)
3198 return (build_min_non_dep_op_overload
3199 (INDIRECT_REF
, rval
, overload
, orig_expr
));
3201 return build_min_non_dep (INDIRECT_REF
, rval
, orig_expr
);
3207 /* The implementation of the above, and of indirection implied by other
3208 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3211 cp_build_indirect_ref_1 (tree ptr
, ref_operator errorstring
,
3212 tsubst_flags_t complain
, bool do_fold
)
3216 /* RO_NULL should only be used with the folding entry points below, not
3217 cp_build_indirect_ref. */
3218 gcc_checking_assert (errorstring
!= RO_NULL
|| do_fold
);
3220 if (ptr
== current_class_ptr
3221 || (TREE_CODE (ptr
) == NOP_EXPR
3222 && TREE_OPERAND (ptr
, 0) == current_class_ptr
3223 && (same_type_ignoring_top_level_qualifiers_p
3224 (TREE_TYPE (ptr
), TREE_TYPE (current_class_ptr
)))))
3225 return current_class_ref
;
3227 pointer
= (TYPE_REF_P (TREE_TYPE (ptr
))
3228 ? ptr
: decay_conversion (ptr
, complain
));
3229 if (pointer
== error_mark_node
)
3230 return error_mark_node
;
3232 type
= TREE_TYPE (pointer
);
3234 if (INDIRECT_TYPE_P (type
))
3238 If the type of the expression is "pointer to T," the type
3239 of the result is "T." */
3240 tree t
= TREE_TYPE (type
);
3242 if ((CONVERT_EXPR_P (ptr
)
3243 || TREE_CODE (ptr
) == VIEW_CONVERT_EXPR
)
3244 && (!CLASS_TYPE_P (t
) || !CLASSTYPE_EMPTY_P (t
)))
3246 /* If a warning is issued, mark it to avoid duplicates from
3247 the backend. This only needs to be done at
3248 warn_strict_aliasing > 2. */
3249 if (warn_strict_aliasing
> 2)
3250 if (strict_aliasing_warning (EXPR_LOCATION (ptr
),
3251 type
, TREE_OPERAND (ptr
, 0)))
3252 TREE_NO_WARNING (ptr
) = 1;
3255 if (VOID_TYPE_P (t
))
3257 /* A pointer to incomplete type (other than cv void) can be
3258 dereferenced [expr.unary.op]/1 */
3259 if (complain
& tf_error
)
3260 error ("%qT is not a pointer-to-object type", type
);
3261 return error_mark_node
;
3263 else if (do_fold
&& TREE_CODE (pointer
) == ADDR_EXPR
3264 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
3265 /* The POINTER was something like `&x'. We simplify `*&x' to
3267 return TREE_OPERAND (pointer
, 0);
3270 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
3272 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3273 so that we get the proper error message if the result is used
3274 to assign to. Also, &* is supposed to be a no-op. */
3275 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
3276 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
3277 TREE_SIDE_EFFECTS (ref
)
3278 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
));
3282 else if (!(complain
& tf_error
))
3283 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3285 /* `pointer' won't be an error_mark_node if we were given a
3286 pointer to member, so it's cool to check for this here. */
3287 else if (TYPE_PTRMEM_P (type
))
3288 switch (errorstring
)
3290 case RO_ARRAY_INDEXING
:
3291 error ("invalid use of array indexing on pointer to member");
3294 error ("invalid use of unary %<*%> on pointer to member");
3296 case RO_IMPLICIT_CONVERSION
:
3297 error ("invalid use of implicit conversion on pointer to member");
3300 error ("left hand operand of %<->*%> must be a pointer to class, "
3301 "but is a pointer to member of type %qT", type
);
3306 else if (pointer
!= error_mark_node
)
3307 invalid_indirection_error (input_location
, type
, errorstring
);
3309 return error_mark_node
;
3312 /* Entry point used by c-common, which expects folding. */
3315 build_indirect_ref (location_t
/*loc*/,
3316 tree ptr
, ref_operator errorstring
)
3318 return cp_build_indirect_ref_1 (ptr
, errorstring
, tf_warning_or_error
, true);
3321 /* Entry point used by internal indirection needs that don't correspond to any
3322 syntactic construct. */
3325 cp_build_fold_indirect_ref (tree pointer
)
3327 return cp_build_indirect_ref_1 (pointer
, RO_NULL
, tf_warning_or_error
, true);
3330 /* Entry point used by indirection needs that correspond to some syntactic
3334 cp_build_indirect_ref (tree ptr
, ref_operator errorstring
,
3335 tsubst_flags_t complain
)
3337 return cp_build_indirect_ref_1 (ptr
, errorstring
, complain
, false);
3340 /* This handles expressions of the form "a[i]", which denotes
3343 This is logically equivalent in C to *(a+i), but we may do it differently.
3344 If A is a variable or a member, we generate a primitive ARRAY_REF.
3345 This avoids forcing the array out of registers, and can work on
3346 arrays that are not lvalues (for example, members of structures returned
3349 If INDEX is of some user-defined type, it must be converted to
3350 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3351 will inherit the type of the array, which will be some pointer type.
3353 LOC is the location to use in building the array reference. */
3356 cp_build_array_ref (location_t loc
, tree array
, tree idx
,
3357 tsubst_flags_t complain
)
3363 if (complain
& tf_error
)
3364 error_at (loc
, "subscript missing in array reference");
3365 return error_mark_node
;
3368 if (TREE_TYPE (array
) == error_mark_node
3369 || TREE_TYPE (idx
) == error_mark_node
)
3370 return error_mark_node
;
3372 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3374 switch (TREE_CODE (array
))
3378 tree value
= cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3380 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3381 TREE_OPERAND (array
, 0), value
);
3382 SET_EXPR_LOCATION (ret
, loc
);
3387 ret
= build_conditional_expr
3388 (loc
, TREE_OPERAND (array
, 0),
3389 cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3391 cp_build_array_ref (loc
, TREE_OPERAND (array
, 2), idx
,
3394 protected_set_expr_location (ret
, loc
);
3401 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, idx
);
3403 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
3407 warn_array_subscript_with_type_char (loc
, idx
);
3409 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx
)))
3411 if (complain
& tf_error
)
3412 error_at (loc
, "array subscript is not an integer");
3413 return error_mark_node
;
3416 /* Apply integral promotions *after* noticing character types.
3417 (It is unclear why we do these promotions -- the standard
3418 does not say that we should. In fact, the natural thing would
3419 seem to be to convert IDX to ptrdiff_t; we're performing
3420 pointer arithmetic.) */
3421 idx
= cp_perform_integral_promotions (idx
, complain
);
3423 idx
= maybe_constant_value (idx
);
3425 /* An array that is indexed by a non-constant
3426 cannot be stored in a register; we must be able to do
3427 address arithmetic on its address.
3428 Likewise an array of elements of variable size. */
3429 if (TREE_CODE (idx
) != INTEGER_CST
3430 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
3431 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
3434 if (!cxx_mark_addressable (array
, true))
3435 return error_mark_node
;
3438 /* An array that is indexed by a constant value which is not within
3439 the array bounds cannot be stored in a register either; because we
3440 would get a crash in store_bit_field/extract_bit_field when trying
3441 to access a non-existent part of the register. */
3442 if (TREE_CODE (idx
) == INTEGER_CST
3443 && TYPE_DOMAIN (TREE_TYPE (array
))
3444 && ! int_fits_type_p (idx
, TYPE_DOMAIN (TREE_TYPE (array
))))
3446 if (!cxx_mark_addressable (array
))
3447 return error_mark_node
;
3450 /* Note in C++ it is valid to subscript a `register' array, since
3451 it is valid to take the address of something with that
3452 storage specification. */
3456 while (TREE_CODE (foo
) == COMPONENT_REF
)
3457 foo
= TREE_OPERAND (foo
, 0);
3458 if (VAR_P (foo
) && DECL_REGISTER (foo
)
3459 && (complain
& tf_warning
))
3460 warning_at (loc
, OPT_Wextra
,
3461 "subscripting array declared %<register%>");
3464 type
= TREE_TYPE (TREE_TYPE (array
));
3465 rval
= build4 (ARRAY_REF
, type
, array
, idx
, NULL_TREE
, NULL_TREE
);
3466 /* Array ref is const/volatile if the array elements are
3467 or if the array is.. */
3468 TREE_READONLY (rval
)
3469 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
3470 TREE_SIDE_EFFECTS (rval
)
3471 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
3472 TREE_THIS_VOLATILE (rval
)
3473 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
3474 ret
= require_complete_type_sfinae (rval
, complain
);
3475 protected_set_expr_location (ret
, loc
);
3477 ret
= non_lvalue_loc (loc
, ret
);
3482 tree ar
= cp_default_conversion (array
, complain
);
3483 tree ind
= cp_default_conversion (idx
, complain
);
3485 /* Put the integer in IND to simplify error checking. */
3486 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
3487 std::swap (ar
, ind
);
3489 if (ar
== error_mark_node
|| ind
== error_mark_node
)
3490 return error_mark_node
;
3492 if (!TYPE_PTR_P (TREE_TYPE (ar
)))
3494 if (complain
& tf_error
)
3495 error_at (loc
, "subscripted value is neither array nor pointer");
3496 return error_mark_node
;
3498 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
3500 if (complain
& tf_error
)
3501 error_at (loc
, "array subscript is not an integer");
3502 return error_mark_node
;
3505 warn_array_subscript_with_type_char (loc
, idx
);
3507 ret
= cp_build_indirect_ref (cp_build_binary_op (input_location
,
3512 protected_set_expr_location (ret
, loc
);
3514 ret
= non_lvalue_loc (loc
, ret
);
3519 /* Entry point for Obj-C++. */
3522 build_array_ref (location_t loc
, tree array
, tree idx
)
3524 return cp_build_array_ref (loc
, array
, idx
, tf_warning_or_error
);
3527 /* Resolve a pointer to member function. INSTANCE is the object
3528 instance to use, if the member points to a virtual member.
3530 This used to avoid checking for virtual functions if basetype
3531 has no virtual functions, according to an earlier ANSI draft.
3532 With the final ISO C++ rules, such an optimization is
3533 incorrect: A pointer to a derived member can be static_cast
3534 to pointer-to-base-member, as long as the dynamic object
3535 later has the right member. So now we only do this optimization
3536 when we know the dynamic type of the object. */
3539 get_member_function_from_ptrfunc (tree
*instance_ptrptr
, tree function
,
3540 tsubst_flags_t complain
)
3542 if (TREE_CODE (function
) == OFFSET_REF
)
3543 function
= TREE_OPERAND (function
, 1);
3545 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
3547 tree idx
, delta
, e1
, e2
, e3
, vtbl
;
3549 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
3550 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
3552 tree instance_ptr
= *instance_ptrptr
;
3553 tree instance_save_expr
= 0;
3554 if (instance_ptr
== error_mark_node
)
3556 if (TREE_CODE (function
) == PTRMEM_CST
)
3558 /* Extracting the function address from a pmf is only
3559 allowed with -Wno-pmf-conversions. It only works for
3561 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
), complain
);
3562 e1
= convert (fntype
, e1
);
3567 if (complain
& tf_error
)
3568 error ("object missing in use of %qE", function
);
3569 return error_mark_node
;
3573 /* True if we know that the dynamic type of the object doesn't have
3574 virtual functions, so we can assume the PFN field is a pointer. */
3575 nonvirtual
= (COMPLETE_TYPE_P (basetype
)
3576 && !TYPE_POLYMORPHIC_P (basetype
)
3577 && resolves_to_fixed_type_p (instance_ptr
, 0));
3579 /* If we don't really have an object (i.e. in an ill-formed
3580 conversion from PMF to pointer), we can't resolve virtual
3581 functions anyway. */
3582 if (!nonvirtual
&& is_dummy_object (instance_ptr
))
3585 if (TREE_SIDE_EFFECTS (instance_ptr
))
3586 instance_ptr
= instance_save_expr
= save_expr (instance_ptr
);
3588 if (TREE_SIDE_EFFECTS (function
))
3589 function
= save_expr (function
);
3591 /* Start by extracting all the information from the PMF itself. */
3592 e3
= pfn_from_ptrmemfunc (function
);
3593 delta
= delta_from_ptrmemfunc (function
);
3594 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
3595 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
3597 int flag_sanitize_save
;
3598 case ptrmemfunc_vbit_in_pfn
:
3599 e1
= cp_build_binary_op (input_location
,
3600 BIT_AND_EXPR
, idx
, integer_one_node
,
3602 idx
= cp_build_binary_op (input_location
,
3603 MINUS_EXPR
, idx
, integer_one_node
,
3605 if (idx
== error_mark_node
)
3606 return error_mark_node
;
3609 case ptrmemfunc_vbit_in_delta
:
3610 e1
= cp_build_binary_op (input_location
,
3611 BIT_AND_EXPR
, delta
, integer_one_node
,
3613 /* Don't instrument the RSHIFT_EXPR we're about to create because
3614 we're going to use DELTA number of times, and that wouldn't play
3615 well with SAVE_EXPRs therein. */
3616 flag_sanitize_save
= flag_sanitize
;
3618 delta
= cp_build_binary_op (input_location
,
3619 RSHIFT_EXPR
, delta
, integer_one_node
,
3621 flag_sanitize
= flag_sanitize_save
;
3622 if (delta
== error_mark_node
)
3623 return error_mark_node
;
3630 if (e1
== error_mark_node
)
3631 return error_mark_node
;
3633 /* Convert down to the right base before using the instance. A
3634 special case is that in a pointer to member of class C, C may
3635 be incomplete. In that case, the function will of course be
3636 a member of C, and no conversion is required. In fact,
3637 lookup_base will fail in that case, because incomplete
3638 classes do not have BINFOs. */
3639 if (!same_type_ignoring_top_level_qualifiers_p
3640 (basetype
, TREE_TYPE (TREE_TYPE (instance_ptr
))))
3642 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
3643 basetype
, ba_check
, NULL
, complain
);
3644 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
,
3646 if (instance_ptr
== error_mark_node
)
3647 return error_mark_node
;
3649 /* ...and then the delta in the PMF. */
3650 instance_ptr
= fold_build_pointer_plus (instance_ptr
, delta
);
3652 /* Hand back the adjusted 'this' argument to our caller. */
3653 *instance_ptrptr
= instance_ptr
;
3656 /* Now just return the pointer. */
3659 /* Next extract the vtable pointer from the object. */
3660 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
3662 vtbl
= cp_build_fold_indirect_ref (vtbl
);
3663 if (vtbl
== error_mark_node
)
3664 return error_mark_node
;
3666 /* Finally, extract the function pointer from the vtable. */
3667 e2
= fold_build_pointer_plus_loc (input_location
, vtbl
, idx
);
3668 e2
= cp_build_fold_indirect_ref (e2
);
3669 if (e2
== error_mark_node
)
3670 return error_mark_node
;
3671 TREE_CONSTANT (e2
) = 1;
3673 /* When using function descriptors, the address of the
3674 vtable entry is treated as a function pointer. */
3675 if (TARGET_VTABLE_USES_DESCRIPTORS
)
3676 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
3677 cp_build_addr_expr (e2
, complain
));
3679 e2
= fold_convert (TREE_TYPE (e3
), e2
);
3680 e1
= build_conditional_expr (input_location
, e1
, e2
, e3
, complain
);
3681 if (e1
== error_mark_node
)
3682 return error_mark_node
;
3684 /* Make sure this doesn't get evaluated first inside one of the
3685 branches of the COND_EXPR. */
3686 if (instance_save_expr
)
3687 e1
= build2 (COMPOUND_EXPR
, TREE_TYPE (e1
),
3688 instance_save_expr
, e1
);
3695 /* Used by the C-common bits. */
3697 build_function_call (location_t
/*loc*/,
3698 tree function
, tree params
)
3700 return cp_build_function_call (function
, params
, tf_warning_or_error
);
3703 /* Used by the C-common bits. */
3705 build_function_call_vec (location_t
/*loc*/, vec
<location_t
> /*arg_loc*/,
3706 tree function
, vec
<tree
, va_gc
> *params
,
3707 vec
<tree
, va_gc
> * /*origtypes*/)
3709 vec
<tree
, va_gc
> *orig_params
= params
;
3710 tree ret
= cp_build_function_call_vec (function
, ¶ms
,
3711 tf_warning_or_error
);
3713 /* cp_build_function_call_vec can reallocate PARAMS by adding
3714 default arguments. That should never happen here. Verify
3716 gcc_assert (params
== orig_params
);
3721 /* Build a function call using a tree list of arguments. */
3724 cp_build_function_call (tree function
, tree params
, tsubst_flags_t complain
)
3726 vec
<tree
, va_gc
> *vec
;
3729 vec
= make_tree_vector ();
3730 for (; params
!= NULL_TREE
; params
= TREE_CHAIN (params
))
3731 vec_safe_push (vec
, TREE_VALUE (params
));
3732 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3733 release_tree_vector (vec
);
3737 /* Build a function call using varargs. */
3740 cp_build_function_call_nary (tree function
, tsubst_flags_t complain
, ...)
3742 vec
<tree
, va_gc
> *vec
;
3746 vec
= make_tree_vector ();
3747 va_start (args
, complain
);
3748 for (t
= va_arg (args
, tree
); t
!= NULL_TREE
; t
= va_arg (args
, tree
))
3749 vec_safe_push (vec
, t
);
3751 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3752 release_tree_vector (vec
);
3756 /* Build a function call using a vector of arguments. PARAMS may be
3757 NULL if there are no parameters. This changes the contents of
3761 cp_build_function_call_vec (tree function
, vec
<tree
, va_gc
> **params
,
3762 tsubst_flags_t complain
)
3764 tree fntype
, fndecl
;
3766 tree original
= function
;
3770 vec
<tree
, va_gc
> *allocated
= NULL
;
3773 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3774 expressions, like those used for ObjC messenger dispatches. */
3775 if (params
!= NULL
&& !vec_safe_is_empty (*params
))
3776 function
= objc_rewrite_function_call (function
, (**params
)[0]);
3778 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3779 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3780 if (TREE_CODE (function
) == NOP_EXPR
3781 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
3782 function
= TREE_OPERAND (function
, 0);
3784 if (TREE_CODE (function
) == FUNCTION_DECL
)
3786 /* If the function is a non-template member function
3787 or a non-template friend, then we need to check the
3790 Note that if overload resolution failed with a single
3791 candidate this function will be used to explicitly diagnose
3792 the failure for the single call expression. The check is
3793 technically redundant since we also would have failed in
3794 add_function_candidate. */
3796 && (complain
& tf_error
)
3797 && !constraints_satisfied_p (function
))
3799 auto_diagnostic_group d
;
3800 error ("cannot call function %qD", function
);
3801 location_t loc
= DECL_SOURCE_LOCATION (function
);
3802 diagnose_constraints (loc
, function
, NULL_TREE
);
3803 return error_mark_node
;
3806 if (!mark_used (function
, complain
) && !(complain
& tf_error
))
3807 return error_mark_node
;
3810 /* Convert anything with function type to a pointer-to-function. */
3811 if (DECL_MAIN_P (function
))
3813 if (complain
& tf_error
)
3814 pedwarn (input_location
, OPT_Wpedantic
,
3815 "ISO C++ forbids calling %<::main%> from within program");
3817 return error_mark_node
;
3819 function
= build_addr_func (function
, complain
);
3825 function
= build_addr_func (function
, complain
);
3828 if (function
== error_mark_node
)
3829 return error_mark_node
;
3831 fntype
= TREE_TYPE (function
);
3833 if (TYPE_PTRMEMFUNC_P (fntype
))
3835 if (complain
& tf_error
)
3836 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3837 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3838 original
, original
);
3839 return error_mark_node
;
3842 is_method
= (TYPE_PTR_P (fntype
)
3843 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
3845 if (!(TYPE_PTRFN_P (fntype
)
3847 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
3849 if (complain
& tf_error
)
3851 if (!flag_diagnostics_show_caret
)
3852 error_at (input_location
,
3853 "%qE cannot be used as a function", original
);
3854 else if (DECL_P (original
))
3855 error_at (input_location
,
3856 "%qD cannot be used as a function", original
);
3858 error_at (input_location
,
3859 "expression cannot be used as a function");
3862 return error_mark_node
;
3865 /* fntype now gets the type of function pointed to. */
3866 fntype
= TREE_TYPE (fntype
);
3867 parm_types
= TYPE_ARG_TYPES (fntype
);
3871 allocated
= make_tree_vector ();
3872 params
= &allocated
;
3875 nargs
= convert_arguments (parm_types
, params
, fndecl
, LOOKUP_NORMAL
,
3878 return error_mark_node
;
3880 argarray
= (*params
)->address ();
3882 /* Check for errors in format strings and inappropriately
3884 bool warned_p
= check_function_arguments (input_location
, fndecl
, fntype
,
3885 nargs
, argarray
, NULL
);
3887 ret
= build_cxx_call (function
, nargs
, argarray
, complain
);
3891 tree c
= extract_call_expr (ret
);
3892 if (TREE_CODE (c
) == CALL_EXPR
)
3893 TREE_NO_WARNING (c
) = 1;
3896 if (allocated
!= NULL
)
3897 release_tree_vector (allocated
);
3902 /* Subroutine of convert_arguments.
3903 Print an error message about a wrong number of arguments. */
3906 error_args_num (location_t loc
, tree fndecl
, bool too_many_p
)
3910 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
3912 if (DECL_NAME (fndecl
) == NULL_TREE
3913 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
3916 ? G_("too many arguments to constructor %q#D")
3917 : G_("too few arguments to constructor %q#D"),
3922 ? G_("too many arguments to member function %q#D")
3923 : G_("too few arguments to member function %q#D"),
3929 ? G_("too many arguments to function %q#D")
3930 : G_("too few arguments to function %q#D"),
3932 if (!DECL_IS_BUILTIN (fndecl
))
3933 inform (DECL_SOURCE_LOCATION (fndecl
), "declared here");
3937 if (c_dialect_objc () && objc_message_selector ())
3940 ? G_("too many arguments to method %q#D")
3941 : G_("too few arguments to method %q#D"),
3942 objc_message_selector ());
3944 error_at (loc
, too_many_p
? G_("too many arguments to function")
3945 : G_("too few arguments to function"));
3949 /* Convert the actual parameter expressions in the list VALUES to the
3950 types in the list TYPELIST. The converted expressions are stored
3951 back in the VALUES vector.
3952 If parmdecls is exhausted, or when an element has NULL as its type,
3953 perform the default conversions.
3955 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3957 This is also where warnings about wrong number of args are generated.
3959 Returns the actual number of arguments processed (which might be less
3960 than the length of the vector), or -1 on error.
3962 In C++, unspecified trailing parameters can be filled in with their
3963 default arguments, if such were specified. Do so here. */
3966 convert_arguments (tree typelist
, vec
<tree
, va_gc
> **values
, tree fndecl
,
3967 int flags
, tsubst_flags_t complain
)
3972 /* Argument passing is always copy-initialization. */
3973 flags
|= LOOKUP_ONLYCONVERTING
;
3975 for (i
= 0, typetail
= typelist
;
3976 i
< vec_safe_length (*values
);
3979 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3980 tree val
= (**values
)[i
];
3982 if (val
== error_mark_node
|| type
== error_mark_node
)
3985 if (type
== void_type_node
)
3987 if (complain
& tf_error
)
3989 error_args_num (input_location
, fndecl
, /*too_many_p=*/true);
3996 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3997 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3998 if (TREE_CODE (val
) == NOP_EXPR
3999 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
4000 && (type
== 0 || !TYPE_REF_P (type
)))
4001 val
= TREE_OPERAND (val
, 0);
4003 if (type
== 0 || !TYPE_REF_P (type
))
4005 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
4006 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
4007 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
4008 val
= decay_conversion (val
, complain
);
4011 if (val
== error_mark_node
)
4016 /* Formal parm type is specified by a function prototype. */
4019 if (!COMPLETE_TYPE_P (complete_type (type
)))
4021 if (complain
& tf_error
)
4023 location_t loc
= EXPR_LOC_OR_LOC (val
, input_location
);
4026 auto_diagnostic_group d
;
4028 "parameter %P of %qD has incomplete type %qT",
4030 inform (get_fndecl_argument_location (fndecl
, i
),
4034 error_at (loc
, "parameter %P has incomplete type %qT", i
,
4037 parmval
= error_mark_node
;
4041 parmval
= convert_for_initialization
4042 (NULL_TREE
, type
, val
, flags
,
4043 ICR_ARGPASS
, fndecl
, i
, complain
);
4044 parmval
= convert_for_arg_passing (type
, parmval
, complain
);
4047 if (parmval
== error_mark_node
)
4050 (**values
)[i
] = parmval
;
4054 if (fndecl
&& magic_varargs_p (fndecl
))
4055 /* Don't do ellipsis conversion for __built_in_constant_p
4056 as this will result in spurious errors for non-trivial
4058 val
= require_complete_type_sfinae (val
, complain
);
4060 val
= convert_arg_to_ellipsis (val
, complain
);
4062 (**values
)[i
] = val
;
4066 typetail
= TREE_CHAIN (typetail
);
4069 if (typetail
!= 0 && typetail
!= void_list_node
)
4071 /* See if there are default arguments that can be used. Because
4072 we hold default arguments in the FUNCTION_TYPE (which is so
4073 wrong), we can see default parameters here from deduced
4074 contexts (and via typeof) for indirect function calls.
4075 Fortunately we know whether we have a function decl to
4076 provide default arguments in a language conformant
4078 if (fndecl
&& TREE_PURPOSE (typetail
)
4079 && TREE_CODE (TREE_PURPOSE (typetail
)) != DEFAULT_ARG
)
4081 for (; typetail
!= void_list_node
; ++i
)
4083 /* After DR777, with explicit template args we can end up with a
4084 default argument followed by no default argument. */
4085 if (!TREE_PURPOSE (typetail
))
4088 = convert_default_arg (TREE_VALUE (typetail
),
4089 TREE_PURPOSE (typetail
),
4090 fndecl
, i
, complain
);
4092 if (parmval
== error_mark_node
)
4095 vec_safe_push (*values
, parmval
);
4096 typetail
= TREE_CHAIN (typetail
);
4097 /* ends with `...'. */
4098 if (typetail
== NULL_TREE
)
4103 if (typetail
&& typetail
!= void_list_node
)
4105 if (complain
& tf_error
)
4106 error_args_num (input_location
, fndecl
, /*too_many_p=*/false);
4114 /* Build a binary-operation expression, after performing default
4115 conversions on the operands. CODE is the kind of expression to
4116 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4117 are the tree codes which correspond to ARG1 and ARG2 when issuing
4118 warnings about possibly misplaced parentheses. They may differ
4119 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4120 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4121 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4122 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4123 ARG2_CODE as ERROR_MARK. */
4126 build_x_binary_op (const op_location_t
&loc
, enum tree_code code
, tree arg1
,
4127 enum tree_code arg1_code
, tree arg2
,
4128 enum tree_code arg2_code
, tree
*overload_p
,
4129 tsubst_flags_t complain
)
4134 tree overload
= NULL_TREE
;
4139 if (processing_template_decl
)
4141 if (type_dependent_expression_p (arg1
)
4142 || type_dependent_expression_p (arg2
))
4143 return build_min_nt_loc (loc
, code
, arg1
, arg2
);
4144 arg1
= build_non_dependent_expr (arg1
);
4145 arg2
= build_non_dependent_expr (arg2
);
4148 if (code
== DOTSTAR_EXPR
)
4149 expr
= build_m_component_ref (arg1
, arg2
, complain
);
4151 expr
= build_new_op (loc
, code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
,
4152 &overload
, complain
);
4154 if (overload_p
!= NULL
)
4155 *overload_p
= overload
;
4157 /* Check for cases such as x+y<<z which users are likely to
4158 misinterpret. But don't warn about obj << x + y, since that is a
4159 common idiom for I/O. */
4160 if (warn_parentheses
4161 && (complain
& tf_warning
)
4162 && !processing_template_decl
4163 && !error_operand_p (arg1
)
4164 && !error_operand_p (arg2
)
4165 && (code
!= LSHIFT_EXPR
4166 || !CLASS_TYPE_P (TREE_TYPE (arg1
))))
4167 warn_about_parentheses (loc
, code
, arg1_code
, orig_arg1
,
4168 arg2_code
, orig_arg2
);
4170 if (processing_template_decl
&& expr
!= error_mark_node
)
4172 if (overload
!= NULL_TREE
)
4173 return (build_min_non_dep_op_overload
4174 (code
, expr
, overload
, orig_arg1
, orig_arg2
));
4176 return build_min_non_dep (code
, expr
, orig_arg1
, orig_arg2
);
4182 /* Build and return an ARRAY_REF expression. */
4185 build_x_array_ref (location_t loc
, tree arg1
, tree arg2
,
4186 tsubst_flags_t complain
)
4188 tree orig_arg1
= arg1
;
4189 tree orig_arg2
= arg2
;
4191 tree overload
= NULL_TREE
;
4193 if (processing_template_decl
)
4195 if (type_dependent_expression_p (arg1
)
4196 || type_dependent_expression_p (arg2
))
4197 return build_min_nt_loc (loc
, ARRAY_REF
, arg1
, arg2
,
4198 NULL_TREE
, NULL_TREE
);
4199 arg1
= build_non_dependent_expr (arg1
);
4200 arg2
= build_non_dependent_expr (arg2
);
4203 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, arg1
, arg2
,
4204 NULL_TREE
, &overload
, complain
);
4206 if (processing_template_decl
&& expr
!= error_mark_node
)
4208 if (overload
!= NULL_TREE
)
4209 return (build_min_non_dep_op_overload
4210 (ARRAY_REF
, expr
, overload
, orig_arg1
, orig_arg2
));
4212 return build_min_non_dep (ARRAY_REF
, expr
, orig_arg1
, orig_arg2
,
4213 NULL_TREE
, NULL_TREE
);
4218 /* Return whether OP is an expression of enum type cast to integer
4219 type. In C++ even unsigned enum types are cast to signed integer
4220 types. We do not want to issue warnings about comparisons between
4221 signed and unsigned types when one of the types is an enum type.
4222 Those warnings are always false positives in practice. */
4225 enum_cast_to_int (tree op
)
4227 if (CONVERT_EXPR_P (op
)
4228 && TREE_TYPE (op
) == integer_type_node
4229 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == ENUMERAL_TYPE
4230 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op
, 0))))
4233 /* The cast may have been pushed into a COND_EXPR. */
4234 if (TREE_CODE (op
) == COND_EXPR
)
4235 return (enum_cast_to_int (TREE_OPERAND (op
, 1))
4236 || enum_cast_to_int (TREE_OPERAND (op
, 2)));
4241 /* For the c-common bits. */
4243 build_binary_op (location_t location
, enum tree_code code
, tree op0
, tree op1
,
4246 return cp_build_binary_op (location
, code
, op0
, op1
, tf_warning_or_error
);
4249 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4250 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4253 build_vec_cmp (tree_code code
, tree type
,
4254 tree arg0
, tree arg1
)
4256 tree zero_vec
= build_zero_cst (type
);
4257 tree minus_one_vec
= build_minus_one_cst (type
);
4258 tree cmp_type
= build_same_sized_truth_vector_type(type
);
4259 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
4260 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
4263 /* Possibly warn about an address never being NULL. */
4266 warn_for_null_address (location_t location
, tree op
, tsubst_flags_t complain
)
4269 || (complain
& tf_warning
) == 0
4270 || c_inhibit_evaluation_warnings
!= 0
4271 || TREE_NO_WARNING (op
))
4274 tree cop
= fold_non_dependent_expr (op
, complain
);
4276 if (TREE_CODE (cop
) == ADDR_EXPR
4277 && decl_with_nonnull_addr_p (TREE_OPERAND (cop
, 0))
4278 && !TREE_NO_WARNING (cop
))
4279 warning_at (location
, OPT_Waddress
, "the address of %qD will never "
4280 "be NULL", TREE_OPERAND (cop
, 0));
4282 if (CONVERT_EXPR_P (op
)
4283 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op
, 0))))
4286 STRIP_NOPS (inner_op
);
4288 if (DECL_P (inner_op
))
4289 warning_at (location
, OPT_Waddress
,
4290 "the compiler can assume that the address of "
4291 "%qD will never be NULL", inner_op
);
4295 /* Build a binary-operation expression without default conversions.
4296 CODE is the kind of expression to build.
4297 LOCATION is the location_t of the operator in the source code.
4298 This function differs from `build' in several ways:
4299 the data type of the result is computed and recorded in it,
4300 warnings are generated if arg data types are invalid,
4301 special handling for addition and subtraction of pointers is known,
4302 and some optimization is done (operations on narrow ints
4303 are done in the narrower type when that gives the same result).
4304 Constant folding is also done before the result is returned.
4306 Note that the operands will never have enumeral types
4307 because either they have just had the default conversions performed
4308 or they have both just been converted to some other type in which
4309 the arithmetic is to be done.
4311 C++: must do special pointer arithmetic when implementing
4312 multiple inheritance, and deal with pointer to member functions. */
4315 cp_build_binary_op (const op_location_t
&location
,
4316 enum tree_code code
, tree orig_op0
, tree orig_op1
,
4317 tsubst_flags_t complain
)
4320 enum tree_code code0
, code1
;
4322 const char *invalid_op_diag
;
4324 /* Expression code to give to the expression when it is built.
4325 Normally this is CODE, which is what the caller asked for,
4326 but in some special cases we change it. */
4327 enum tree_code resultcode
= code
;
4329 /* Data type in which the computation is to be performed.
4330 In the simplest cases this is the common type of the arguments. */
4331 tree result_type
= NULL_TREE
;
4333 /* Nonzero means operands have already been type-converted
4334 in whatever way is necessary.
4335 Zero means they need to be converted to RESULT_TYPE. */
4338 /* Nonzero means create the expression with this type, rather than
4340 tree build_type
= 0;
4342 /* Nonzero means after finally constructing the expression
4343 convert it to this type. */
4344 tree final_type
= 0;
4346 tree result
, result_ovl
;
4348 /* Nonzero if this is an operation like MIN or MAX which can
4349 safely be computed in short if both args are promoted shorts.
4350 Also implies COMMON.
4351 -1 indicates a bitwise operation; this makes a difference
4352 in the exact conditions for when it is safe to do the operation
4353 in a narrower mode. */
4356 /* Nonzero if this is a comparison operation;
4357 if both args are promoted shorts, compare the original shorts.
4358 Also implies COMMON. */
4359 int short_compare
= 0;
4361 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4364 /* True if both operands have arithmetic type. */
4365 bool arithmetic_types_p
;
4367 /* Apply default conversions. */
4371 /* Remember whether we're doing / or %. */
4372 bool doing_div_or_mod
= false;
4374 /* Remember whether we're doing << or >>. */
4375 bool doing_shift
= false;
4377 /* Tree holding instrumentation expression. */
4378 tree instrument_expr
= NULL_TREE
;
4380 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
4381 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
4382 || code
== TRUTH_XOR_EXPR
)
4384 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
4385 op0
= decay_conversion (op0
, complain
);
4386 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
4387 op1
= decay_conversion (op1
, complain
);
4391 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
4392 op0
= cp_default_conversion (op0
, complain
);
4393 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
4394 op1
= cp_default_conversion (op1
, complain
);
4397 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4398 STRIP_TYPE_NOPS (op0
);
4399 STRIP_TYPE_NOPS (op1
);
4401 /* DTRT if one side is an overloaded function, but complain about it. */
4402 if (type_unknown_p (op0
))
4404 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
4405 if (t
!= error_mark_node
)
4407 if (complain
& tf_error
)
4408 permerror (input_location
, "assuming cast to type %qT from overloaded function",
4413 if (type_unknown_p (op1
))
4415 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
4416 if (t
!= error_mark_node
)
4418 if (complain
& tf_error
)
4419 permerror (input_location
, "assuming cast to type %qT from overloaded function",
4425 type0
= TREE_TYPE (op0
);
4426 type1
= TREE_TYPE (op1
);
4428 /* The expression codes of the data types of the arguments tell us
4429 whether the arguments are integers, floating, pointers, etc. */
4430 code0
= TREE_CODE (type0
);
4431 code1
= TREE_CODE (type1
);
4433 /* If an error was already reported for one of the arguments,
4434 avoid reporting another error. */
4435 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
4436 return error_mark_node
;
4438 if ((invalid_op_diag
4439 = targetm
.invalid_binary_op (code
, type0
, type1
)))
4441 if (complain
& tf_error
)
4442 error (invalid_op_diag
);
4443 return error_mark_node
;
4446 /* Issue warnings about peculiar, but valid, uses of NULL. */
4447 if ((null_node_p (orig_op0
) || null_node_p (orig_op1
))
4448 /* It's reasonable to use pointer values as operands of &&
4449 and ||, so NULL is no exception. */
4450 && code
!= TRUTH_ANDIF_EXPR
&& code
!= TRUTH_ORIF_EXPR
4451 && ( /* Both are NULL (or 0) and the operation was not a
4452 comparison or a pointer subtraction. */
4453 (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)
4454 && code
!= EQ_EXPR
&& code
!= NE_EXPR
&& code
!= MINUS_EXPR
)
4455 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4456 || (!null_ptr_cst_p (orig_op0
)
4457 && !TYPE_PTR_OR_PTRMEM_P (type0
))
4458 || (!null_ptr_cst_p (orig_op1
)
4459 && !TYPE_PTR_OR_PTRMEM_P (type1
)))
4460 && (complain
& tf_warning
))
4463 expansion_point_location_if_in_system_header (input_location
);
4465 warning_at (loc
, OPT_Wpointer_arith
, "NULL used in arithmetic");
4468 /* In case when one of the operands of the binary operation is
4469 a vector and another is a scalar -- convert scalar to vector. */
4470 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
4472 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
4473 complain
& tf_error
);
4475 switch (convert_flag
)
4478 return error_mark_node
;
4481 op0
= convert (TREE_TYPE (type1
), op0
);
4482 op0
= save_expr (op0
);
4483 op0
= build_vector_from_val (type1
, op0
);
4484 type0
= TREE_TYPE (op0
);
4485 code0
= TREE_CODE (type0
);
4491 op1
= convert (TREE_TYPE (type0
), op1
);
4492 op1
= save_expr (op1
);
4493 op1
= build_vector_from_val (type0
, op1
);
4494 type1
= TREE_TYPE (op1
);
4495 code1
= TREE_CODE (type1
);
4507 /* Subtraction of two similar pointers.
4508 We must subtract them as integers, then divide by object size. */
4509 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
4510 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4513 result
= pointer_diff (location
, op0
, op1
,
4514 common_pointer_type (type0
, type1
), complain
,
4516 if (instrument_expr
!= NULL
)
4517 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
4518 instrument_expr
, result
);
4522 /* In all other cases except pointer - int, the usual arithmetic
4524 else if (!(code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
))
4529 /* The pointer - int case is just like pointer + int; fall
4533 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
4534 && (code0
== INTEGER_TYPE
|| code1
== INTEGER_TYPE
))
4538 ptr_operand
= ((code0
== POINTER_TYPE
) ? op0
: op1
);
4539 int_operand
= ((code0
== INTEGER_TYPE
) ? op0
: op1
);
4540 if (processing_template_decl
)
4542 result_type
= TREE_TYPE (ptr_operand
);
4545 return cp_pointer_int_sum (location
, code
,
4557 case TRUNC_DIV_EXPR
:
4559 case FLOOR_DIV_EXPR
:
4560 case ROUND_DIV_EXPR
:
4561 case EXACT_DIV_EXPR
:
4562 if (TREE_CODE (op0
) == SIZEOF_EXPR
&& TREE_CODE (op1
) == SIZEOF_EXPR
)
4564 tree type0
= TREE_OPERAND (op0
, 0);
4565 tree type1
= TREE_OPERAND (op1
, 0);
4566 tree first_arg
= type0
;
4567 if (!TYPE_P (type0
))
4568 type0
= TREE_TYPE (type0
);
4569 if (!TYPE_P (type1
))
4570 type1
= TREE_TYPE (type1
);
4571 if (INDIRECT_TYPE_P (type0
) && same_type_p (TREE_TYPE (type0
), type1
))
4573 STRIP_ANY_LOCATION_WRAPPER (first_arg
);
4574 if (!(TREE_CODE (first_arg
) == PARM_DECL
4575 && DECL_ARRAY_PARAMETER_P (first_arg
)
4576 && warn_sizeof_array_argument
)
4577 && (complain
& tf_warning
))
4579 auto_diagnostic_group d
;
4580 if (warning_at (location
, OPT_Wsizeof_pointer_div
,
4581 "division %<sizeof (%T) / sizeof (%T)%> does "
4582 "not compute the number of array elements",
4584 if (DECL_P (first_arg
))
4585 inform (DECL_SOURCE_LOCATION (first_arg
),
4586 "first %<sizeof%> operand was declared here");
4591 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4592 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
4593 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4594 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
4596 enum tree_code tcode0
= code0
, tcode1
= code1
;
4597 tree cop1
= fold_non_dependent_expr (op1
, complain
);
4598 doing_div_or_mod
= true;
4599 warn_for_div_by_zero (location
, cop1
);
4601 if (tcode0
== COMPLEX_TYPE
|| tcode0
== VECTOR_TYPE
)
4602 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
4603 if (tcode1
== COMPLEX_TYPE
|| tcode1
== VECTOR_TYPE
)
4604 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
4606 if (!(tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
))
4607 resultcode
= RDIV_EXPR
;
4610 /* When dividing two signed integers, we have to promote to int.
4611 unless we divide by a constant != -1. Note that default
4612 conversion will have been performed on the operands at this
4613 point, so we have to dig out the original type to find out if
4615 tree stripped_op1
= tree_strip_any_location_wrapper (op1
);
4616 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4617 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4618 || (TREE_CODE (stripped_op1
) == INTEGER_CST
4619 && ! integer_all_onesp (stripped_op1
)));
4629 if ((code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4630 || (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4631 && !VECTOR_FLOAT_TYPE_P (type0
)
4632 && !VECTOR_FLOAT_TYPE_P (type1
)))
4636 case TRUNC_MOD_EXPR
:
4637 case FLOOR_MOD_EXPR
:
4639 tree cop1
= fold_non_dependent_expr (op1
, complain
);
4640 doing_div_or_mod
= true;
4641 warn_for_div_by_zero (location
, cop1
);
4644 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4645 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4646 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
4648 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4650 /* Although it would be tempting to shorten always here, that loses
4651 on some targets, since the modulo instruction is undefined if the
4652 quotient can't be represented in the computation mode. We shorten
4653 only if unsigned or if dividing by something we know != -1. */
4654 tree stripped_op1
= tree_strip_any_location_wrapper (op1
);
4655 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4656 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4657 || (TREE_CODE (stripped_op1
) == INTEGER_CST
4658 && ! integer_all_onesp (stripped_op1
)));
4663 case TRUTH_ANDIF_EXPR
:
4664 case TRUTH_ORIF_EXPR
:
4665 case TRUTH_AND_EXPR
:
4667 if (!VECTOR_TYPE_P (type0
) && VECTOR_TYPE_P (type1
))
4669 if (!COMPARISON_CLASS_P (op1
))
4670 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4671 build_zero_cst (type1
), complain
);
4672 if (code
== TRUTH_ANDIF_EXPR
)
4674 tree z
= build_zero_cst (TREE_TYPE (op1
));
4675 return build_conditional_expr (location
, op0
, op1
, z
, complain
);
4677 else if (code
== TRUTH_ORIF_EXPR
)
4679 tree m1
= build_all_ones_cst (TREE_TYPE (op1
));
4680 return build_conditional_expr (location
, op0
, m1
, op1
, complain
);
4685 if (VECTOR_TYPE_P (type0
))
4687 if (!COMPARISON_CLASS_P (op0
))
4688 op0
= cp_build_binary_op (EXPR_LOCATION (op0
), NE_EXPR
, op0
,
4689 build_zero_cst (type0
), complain
);
4690 if (!VECTOR_TYPE_P (type1
))
4692 tree m1
= build_all_ones_cst (TREE_TYPE (op0
));
4693 tree z
= build_zero_cst (TREE_TYPE (op0
));
4694 op1
= build_conditional_expr (location
, op1
, m1
, z
, complain
);
4696 else if (!COMPARISON_CLASS_P (op1
))
4697 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4698 build_zero_cst (type1
), complain
);
4700 if (code
== TRUTH_ANDIF_EXPR
)
4701 code
= BIT_AND_EXPR
;
4702 else if (code
== TRUTH_ORIF_EXPR
)
4703 code
= BIT_IOR_EXPR
;
4707 return cp_build_binary_op (location
, code
, op0
, op1
, complain
);
4710 result_type
= boolean_type_node
;
4713 /* Shift operations: result has same type as first operand;
4714 always convert second operand to int.
4715 Also set SHORT_SHIFT if shifting rightward. */
4718 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4719 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4721 result_type
= type0
;
4724 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4725 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4726 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4727 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
4728 TYPE_VECTOR_SUBPARTS (type1
)))
4730 result_type
= type0
;
4733 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4735 tree const_op1
= fold_non_dependent_expr (op1
, complain
);
4736 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4738 result_type
= type0
;
4740 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4742 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4744 if ((complain
& tf_warning
)
4745 && c_inhibit_evaluation_warnings
== 0)
4746 warning (OPT_Wshift_count_negative
,
4747 "right shift count is negative");
4751 if (compare_tree_int (const_op1
, TYPE_PRECISION (type0
)) >= 0
4752 && (complain
& tf_warning
)
4753 && c_inhibit_evaluation_warnings
== 0)
4754 warning (OPT_Wshift_count_overflow
,
4755 "right shift count >= width of type");
4758 /* Avoid converting op1 to result_type later. */
4764 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4765 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4767 result_type
= type0
;
4770 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4771 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4772 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4773 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
4774 TYPE_VECTOR_SUBPARTS (type1
)))
4776 result_type
= type0
;
4779 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4781 tree const_op0
= fold_non_dependent_expr (op0
, complain
);
4782 if (TREE_CODE (const_op0
) != INTEGER_CST
)
4784 tree const_op1
= fold_non_dependent_expr (op1
, complain
);
4785 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4787 result_type
= type0
;
4789 if (TREE_CODE (const_op0
) == INTEGER_CST
4790 && tree_int_cst_sgn (const_op0
) < 0
4791 && (complain
& tf_warning
)
4792 && c_inhibit_evaluation_warnings
== 0)
4793 warning (OPT_Wshift_negative_value
,
4794 "left shift of negative value");
4795 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4797 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4799 if ((complain
& tf_warning
)
4800 && c_inhibit_evaluation_warnings
== 0)
4801 warning (OPT_Wshift_count_negative
,
4802 "left shift count is negative");
4804 else if (compare_tree_int (const_op1
,
4805 TYPE_PRECISION (type0
)) >= 0)
4807 if ((complain
& tf_warning
)
4808 && c_inhibit_evaluation_warnings
== 0)
4809 warning (OPT_Wshift_count_overflow
,
4810 "left shift count >= width of type");
4812 else if (TREE_CODE (const_op0
) == INTEGER_CST
4813 && (complain
& tf_warning
))
4814 maybe_warn_shift_overflow (location
, const_op0
, const_op1
);
4816 /* Avoid converting op1 to result_type later. */
4823 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4825 result_type
= type0
;
4826 if (TREE_CODE (op1
) == INTEGER_CST
)
4828 if (tree_int_cst_lt (op1
, integer_zero_node
))
4830 if (complain
& tf_warning
)
4831 warning (0, (code
== LROTATE_EXPR
)
4832 ? G_("left rotate count is negative")
4833 : G_("right rotate count is negative"));
4835 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
4837 if (complain
& tf_warning
)
4838 warning (0, (code
== LROTATE_EXPR
)
4839 ? G_("left rotate count >= width of type")
4840 : G_("right rotate count >= width of type"));
4843 /* Convert the shift-count to an integer, regardless of
4844 size of value being shifted. */
4845 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4846 op1
= cp_convert (integer_type_node
, op1
, complain
);
4852 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4853 goto vector_compare
;
4854 if ((complain
& tf_warning
)
4855 && c_inhibit_evaluation_warnings
== 0
4856 && (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
)))
4857 warning (OPT_Wfloat_equal
,
4858 "comparing floating point with == or != is unsafe");
4859 if (complain
& tf_warning
)
4861 tree stripped_orig_op0
= tree_strip_any_location_wrapper (orig_op0
);
4862 tree stripped_orig_op1
= tree_strip_any_location_wrapper (orig_op1
);
4863 if ((TREE_CODE (stripped_orig_op0
) == STRING_CST
4864 && !integer_zerop (cp_fully_fold (op1
)))
4865 || (TREE_CODE (stripped_orig_op1
) == STRING_CST
4866 && !integer_zerop (cp_fully_fold (op0
))))
4867 warning (OPT_Waddress
, "comparison with string literal results "
4868 "in unspecified behavior");
4871 build_type
= boolean_type_node
;
4872 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4873 || code0
== COMPLEX_TYPE
|| code0
== ENUMERAL_TYPE
)
4874 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4875 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
))
4877 else if (((code0
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type0
))
4878 && null_ptr_cst_p (orig_op1
))
4879 /* Handle, eg, (void*)0 (c++/43906), and more. */
4880 || (code0
== POINTER_TYPE
4881 && TYPE_PTR_P (type1
) && integer_zerop (op1
)))
4883 if (TYPE_PTR_P (type1
))
4884 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4885 CPO_COMPARISON
, complain
);
4887 result_type
= type0
;
4889 if (char_type_p (TREE_TYPE (orig_op1
)))
4891 auto_diagnostic_group d
;
4892 if (warning (OPT_Wpointer_compare
,
4893 "comparison between pointer and zero character "
4895 inform (input_location
,
4896 "did you mean to dereference the pointer?");
4898 warn_for_null_address (location
, op0
, complain
);
4900 else if (((code1
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type1
))
4901 && null_ptr_cst_p (orig_op0
))
4902 /* Handle, eg, (void*)0 (c++/43906), and more. */
4903 || (code1
== POINTER_TYPE
4904 && TYPE_PTR_P (type0
) && integer_zerop (op0
)))
4906 if (TYPE_PTR_P (type0
))
4907 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4908 CPO_COMPARISON
, complain
);
4910 result_type
= type1
;
4912 if (char_type_p (TREE_TYPE (orig_op0
)))
4914 auto_diagnostic_group d
;
4915 if (warning (OPT_Wpointer_compare
,
4916 "comparison between pointer and zero character "
4918 inform (input_location
,
4919 "did you mean to dereference the pointer?");
4921 warn_for_null_address (location
, op1
, complain
);
4923 else if ((code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4924 || (TYPE_PTRDATAMEM_P (type0
) && TYPE_PTRDATAMEM_P (type1
)))
4925 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4926 CPO_COMPARISON
, complain
);
4927 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
4928 /* One of the operands must be of nullptr_t type. */
4929 result_type
= TREE_TYPE (nullptr_node
);
4930 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4932 result_type
= type0
;
4933 if (complain
& tf_error
)
4934 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4936 return error_mark_node
;
4938 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4940 result_type
= type1
;
4941 if (complain
& tf_error
)
4942 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4944 return error_mark_node
;
4946 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (orig_op1
))
4948 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4949 == ptrmemfunc_vbit_in_delta
)
4951 tree pfn0
, delta0
, e1
, e2
;
4953 if (TREE_SIDE_EFFECTS (op0
))
4954 op0
= cp_save_expr (op0
);
4956 pfn0
= pfn_from_ptrmemfunc (op0
);
4957 delta0
= delta_from_ptrmemfunc (op0
);
4958 e1
= cp_build_binary_op (location
,
4961 build_zero_cst (TREE_TYPE (pfn0
)),
4963 e2
= cp_build_binary_op (location
,
4969 if (complain
& tf_warning
)
4970 maybe_warn_zero_as_null_pointer_constant (op1
, input_location
);
4972 e2
= cp_build_binary_op (location
,
4973 EQ_EXPR
, e2
, integer_zero_node
,
4975 op0
= cp_build_binary_op (location
,
4976 TRUTH_ANDIF_EXPR
, e1
, e2
,
4978 op1
= cp_convert (TREE_TYPE (op0
), integer_one_node
, complain
);
4982 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
4983 op1
= cp_convert (TREE_TYPE (op0
), op1
, complain
);
4985 result_type
= TREE_TYPE (op0
);
4987 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (orig_op0
))
4988 return cp_build_binary_op (location
, code
, op1
, op0
, complain
);
4989 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
))
4992 /* E will be the final comparison. */
4994 /* E1 and E2 are for scratch. */
5002 type
= composite_pointer_type (type0
, type1
, op0
, op1
,
5003 CPO_COMPARISON
, complain
);
5005 if (!same_type_p (TREE_TYPE (op0
), type
))
5006 op0
= cp_convert_and_check (type
, op0
, complain
);
5007 if (!same_type_p (TREE_TYPE (op1
), type
))
5008 op1
= cp_convert_and_check (type
, op1
, complain
);
5010 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5011 return error_mark_node
;
5013 if (TREE_SIDE_EFFECTS (op0
))
5014 op0
= save_expr (op0
);
5015 if (TREE_SIDE_EFFECTS (op1
))
5016 op1
= save_expr (op1
);
5018 pfn0
= pfn_from_ptrmemfunc (op0
);
5019 pfn0
= cp_fully_fold (pfn0
);
5020 /* Avoid -Waddress warnings (c++/64877). */
5021 if (TREE_CODE (pfn0
) == ADDR_EXPR
)
5022 TREE_NO_WARNING (pfn0
) = 1;
5023 pfn1
= pfn_from_ptrmemfunc (op1
);
5024 pfn1
= cp_fully_fold (pfn1
);
5025 delta0
= delta_from_ptrmemfunc (op0
);
5026 delta1
= delta_from_ptrmemfunc (op1
);
5027 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5028 == ptrmemfunc_vbit_in_delta
)
5033 && ((op0.delta == op1.delta)
5034 || (!op0.pfn && op0.delta & 1 == 0
5035 && op1.delta & 1 == 0))
5037 The reason for the `!op0.pfn' bit is that a NULL
5038 pointer-to-member is any member with a zero PFN and
5039 LSB of the DELTA field is 0. */
5041 e1
= cp_build_binary_op (location
, BIT_AND_EXPR
,
5045 e1
= cp_build_binary_op (location
,
5046 EQ_EXPR
, e1
, integer_zero_node
,
5048 e2
= cp_build_binary_op (location
, BIT_AND_EXPR
,
5052 e2
= cp_build_binary_op (location
,
5053 EQ_EXPR
, e2
, integer_zero_node
,
5055 e1
= cp_build_binary_op (location
,
5056 TRUTH_ANDIF_EXPR
, e2
, e1
,
5058 e2
= cp_build_binary_op (location
, EQ_EXPR
,
5060 build_zero_cst (TREE_TYPE (pfn0
)),
5062 e2
= cp_build_binary_op (location
,
5063 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
5064 e1
= cp_build_binary_op (location
,
5065 EQ_EXPR
, delta0
, delta1
, complain
);
5066 e1
= cp_build_binary_op (location
,
5067 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
5074 && (!op0.pfn || op0.delta == op1.delta))
5076 The reason for the `!op0.pfn' bit is that a NULL
5077 pointer-to-member is any member with a zero PFN; the
5078 DELTA field is unspecified. */
5080 e1
= cp_build_binary_op (location
,
5081 EQ_EXPR
, delta0
, delta1
, complain
);
5082 e2
= cp_build_binary_op (location
,
5085 build_zero_cst (TREE_TYPE (pfn0
)),
5087 e1
= cp_build_binary_op (location
,
5088 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
5090 e2
= build2 (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
5091 e
= cp_build_binary_op (location
,
5092 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
5093 if (code
== EQ_EXPR
)
5095 return cp_build_binary_op (location
,
5096 EQ_EXPR
, e
, integer_zero_node
, complain
);
5100 gcc_assert (!TYPE_PTRMEMFUNC_P (type0
)
5101 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
),
5103 gcc_assert (!TYPE_PTRMEMFUNC_P (type1
)
5104 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
),
5112 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
5113 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
5115 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5116 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
5117 CPO_COMPARISON
, complain
);
5124 if (TREE_CODE (orig_op0
) == STRING_CST
5125 || TREE_CODE (orig_op1
) == STRING_CST
)
5127 if (complain
& tf_warning
)
5128 warning (OPT_Waddress
, "comparison with string literal results "
5129 "in unspecified behavior");
5132 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
5136 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
5138 && !vector_types_compatible_elements_p (type0
, type1
))
5140 if (complain
& tf_error
)
5142 error_at (location
, "comparing vectors with different "
5144 inform (location
, "operand types are %qT and %qT",
5147 return error_mark_node
;
5150 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0
),
5151 TYPE_VECTOR_SUBPARTS (type1
)))
5153 if (complain
& tf_error
)
5155 error_at (location
, "comparing vectors with different "
5156 "number of elements");
5157 inform (location
, "operand types are %qT and %qT",
5160 return error_mark_node
;
5163 /* It's not precisely specified how the usual arithmetic
5164 conversions apply to the vector types. Here, we use
5165 the unsigned type if one of the operands is signed and
5166 the other one is unsigned. */
5167 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
5169 if (!TYPE_UNSIGNED (type0
))
5170 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
5172 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
5173 warning_at (location
, OPT_Wsign_compare
, "comparison between "
5174 "types %qT and %qT", type0
, type1
);
5177 /* Always construct signed integer vector type. */
5178 intt
= c_common_type_for_size
5179 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0
))), 0);
5182 if (complain
& tf_error
)
5183 error_at (location
, "could not find an integer type "
5184 "of the same size as %qT", TREE_TYPE (type0
));
5185 return error_mark_node
;
5187 result_type
= build_opaque_vector_type (intt
,
5188 TYPE_VECTOR_SUBPARTS (type0
));
5190 return build_vec_cmp (resultcode
, result_type
, op0
, op1
);
5192 build_type
= boolean_type_node
;
5193 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
5194 || code0
== ENUMERAL_TYPE
)
5195 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5196 || code1
== ENUMERAL_TYPE
))
5198 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5199 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
5200 CPO_COMPARISON
, complain
);
5201 else if (code0
== POINTER_TYPE
&& null_ptr_cst_p (orig_op1
))
5203 result_type
= type0
;
5204 if (extra_warnings
&& (complain
& tf_warning
))
5205 warning (OPT_Wextra
,
5206 "ordered comparison of pointer with integer zero");
5208 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (orig_op0
))
5210 result_type
= type1
;
5211 if (extra_warnings
&& (complain
& tf_warning
))
5212 warning (OPT_Wextra
,
5213 "ordered comparison of pointer with integer zero");
5215 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
5216 /* One of the operands must be of nullptr_t type. */
5217 result_type
= TREE_TYPE (nullptr_node
);
5218 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5220 result_type
= type0
;
5221 if (complain
& tf_error
)
5222 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
5224 return error_mark_node
;
5226 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
5228 result_type
= type1
;
5229 if (complain
& tf_error
)
5230 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
5232 return error_mark_node
;
5235 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
5236 && sanitize_flags_p (SANITIZE_POINTER_COMPARE
))
5238 op0
= save_expr (op0
);
5239 op1
= save_expr (op1
);
5241 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE
);
5242 instrument_expr
= build_call_expr_loc (location
, tt
, 2, op0
, op1
);
5247 case UNORDERED_EXPR
:
5254 build_type
= integer_type_node
;
5255 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
5257 if (complain
& tf_error
)
5258 error ("unordered comparison on non-floating point argument");
5259 return error_mark_node
;
5268 if (((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
5269 || code0
== ENUMERAL_TYPE
)
5270 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5271 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
)))
5272 arithmetic_types_p
= 1;
5275 arithmetic_types_p
= 0;
5276 /* Vector arithmetic is only allowed when both sides are vectors. */
5277 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
5279 if (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
5280 || !vector_types_compatible_elements_p (type0
, type1
))
5282 if (complain
& tf_error
)
5284 /* "location" already embeds the locations of the
5285 operands, so we don't need to add them separately
5287 rich_location
richloc (line_table
, location
);
5288 binary_op_error (&richloc
, code
, type0
, type1
);
5290 return error_mark_node
;
5292 arithmetic_types_p
= 1;
5295 /* Determine the RESULT_TYPE, if it is not already known. */
5297 && arithmetic_types_p
5298 && (shorten
|| common
|| short_compare
))
5300 result_type
= cp_common_type (type0
, type1
);
5301 if (complain
& tf_warning
)
5302 do_warn_double_promotion (result_type
, type0
, type1
,
5303 "implicit conversion from %qH to %qI "
5304 "to match other operand of binary "
5311 if (complain
& tf_error
)
5313 binary_op_rich_location
richloc (location
,
5314 orig_op0
, orig_op1
, true);
5316 "invalid operands of types %qT and %qT to binary %qO",
5317 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
5319 return error_mark_node
;
5322 /* If we're in a template, the only thing we need to know is the
5324 if (processing_template_decl
)
5326 /* Since the middle-end checks the type when doing a build2, we
5327 need to build the tree in pieces. This built tree will never
5328 get out of the front-end as we replace it when instantiating
5330 tree tmp
= build2 (resultcode
,
5331 build_type
? build_type
: result_type
,
5333 TREE_OPERAND (tmp
, 0) = op0
;
5337 /* Remember the original type; RESULT_TYPE might be changed later on
5338 by shorten_binary_op. */
5339 tree orig_type
= result_type
;
5341 if (arithmetic_types_p
)
5343 bool first_complex
= (code0
== COMPLEX_TYPE
);
5344 bool second_complex
= (code1
== COMPLEX_TYPE
);
5345 int none_complex
= (!first_complex
&& !second_complex
);
5347 /* Adapted from patch for c/24581. */
5348 if (first_complex
!= second_complex
5349 && (code
== PLUS_EXPR
5350 || code
== MINUS_EXPR
5351 || code
== MULT_EXPR
5352 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
5353 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
5354 && flag_signed_zeros
)
5356 /* An operation on mixed real/complex operands must be
5357 handled specially, but the language-independent code can
5358 more easily optimize the plain complex arithmetic if
5359 -fno-signed-zeros. */
5360 tree real_type
= TREE_TYPE (result_type
);
5364 if (TREE_TYPE (op0
) != result_type
)
5365 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5366 if (TREE_TYPE (op1
) != real_type
)
5367 op1
= cp_convert_and_check (real_type
, op1
, complain
);
5371 if (TREE_TYPE (op0
) != real_type
)
5372 op0
= cp_convert_and_check (real_type
, op0
, complain
);
5373 if (TREE_TYPE (op1
) != result_type
)
5374 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5376 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
5377 return error_mark_node
;
5380 op0
= save_expr (op0
);
5381 real
= cp_build_unary_op (REALPART_EXPR
, op0
, true, complain
);
5382 imag
= cp_build_unary_op (IMAGPART_EXPR
, op0
, true, complain
);
5386 case TRUNC_DIV_EXPR
:
5387 op1
= save_expr (op1
);
5388 imag
= build2 (resultcode
, real_type
, imag
, op1
);
5392 real
= build2 (resultcode
, real_type
, real
, op1
);
5400 op1
= save_expr (op1
);
5401 real
= cp_build_unary_op (REALPART_EXPR
, op1
, true, complain
);
5402 imag
= cp_build_unary_op (IMAGPART_EXPR
, op1
, true, complain
);
5406 op0
= save_expr (op0
);
5407 imag
= build2 (resultcode
, real_type
, op0
, imag
);
5410 real
= build2 (resultcode
, real_type
, op0
, real
);
5413 real
= build2 (resultcode
, real_type
, op0
, real
);
5414 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
5420 result
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
5424 /* For certain operations (which identify themselves by shorten != 0)
5425 if both args were extended from the same smaller type,
5426 do the arithmetic in that type and then extend.
5428 shorten !=0 and !=1 indicates a bitwise operation.
5429 For them, this optimization is safe only if
5430 both args are zero-extended or both are sign-extended.
5431 Otherwise, we might change the result.
5432 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5433 but calculated in (unsigned short) it would be (unsigned short)-1. */
5435 if (shorten
&& none_complex
)
5437 final_type
= result_type
;
5438 result_type
= shorten_binary_op (result_type
, op0
, op1
,
5442 /* Comparison operations are shortened too but differently.
5443 They identify themselves by setting short_compare = 1. */
5447 /* We call shorten_compare only for diagnostics. */
5448 tree xop0
= fold_simple (op0
);
5449 tree xop1
= fold_simple (op1
);
5450 tree xresult_type
= result_type
;
5451 enum tree_code xresultcode
= resultcode
;
5452 shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
5456 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
5457 && warn_sign_compare
5458 /* Do not warn until the template is instantiated; we cannot
5459 bound the ranges of the arguments until that point. */
5460 && !processing_template_decl
5461 && (complain
& tf_warning
)
5462 && c_inhibit_evaluation_warnings
== 0
5463 /* Even unsigned enum types promote to signed int. We don't
5464 want to issue -Wsign-compare warnings for this case. */
5465 && !enum_cast_to_int (orig_op0
)
5466 && !enum_cast_to_int (orig_op1
))
5468 tree oop0
= maybe_constant_value (orig_op0
);
5469 tree oop1
= maybe_constant_value (orig_op1
);
5471 if (TREE_CODE (oop0
) != INTEGER_CST
)
5472 oop0
= cp_fully_fold (orig_op0
);
5473 if (TREE_CODE (oop1
) != INTEGER_CST
)
5474 oop1
= cp_fully_fold (orig_op1
);
5475 warn_for_sign_compare (location
, oop0
, oop1
, op0
, op1
,
5476 result_type
, resultcode
);
5480 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5481 Then the expression will be built.
5482 It will be given type FINAL_TYPE if that is nonzero;
5483 otherwise, it will be given type RESULT_TYPE. */
5486 warning_sentinel
w (warn_sign_conversion
, short_compare
);
5487 if (TREE_TYPE (op0
) != result_type
)
5488 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5489 if (TREE_TYPE (op1
) != result_type
)
5490 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5492 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5493 return error_mark_node
;
5496 if (build_type
== NULL_TREE
)
5497 build_type
= result_type
;
5499 if (sanitize_flags_p ((SANITIZE_SHIFT
5500 | SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
))
5501 && current_function_decl
!= NULL_TREE
5502 && !processing_template_decl
5503 && (doing_div_or_mod
|| doing_shift
))
5505 /* OP0 and/or OP1 might have side-effects. */
5506 op0
= cp_save_expr (op0
);
5507 op1
= cp_save_expr (op1
);
5508 op0
= fold_non_dependent_expr (op0
, complain
);
5509 op1
= fold_non_dependent_expr (op1
, complain
);
5510 if (doing_div_or_mod
5511 && sanitize_flags_p (SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
))
5513 /* For diagnostics we want to use the promoted types without
5514 shorten_binary_op. So convert the arguments to the
5515 original result_type. */
5518 if (TREE_TYPE (cop0
) != orig_type
)
5519 cop0
= cp_convert (orig_type
, op0
, complain
);
5520 if (TREE_TYPE (cop1
) != orig_type
)
5521 cop1
= cp_convert (orig_type
, op1
, complain
);
5522 instrument_expr
= ubsan_instrument_division (location
, cop0
, cop1
);
5524 else if (doing_shift
&& sanitize_flags_p (SANITIZE_SHIFT
))
5525 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
5528 result
= build2_loc (location
, resultcode
, build_type
, op0
, op1
);
5529 if (final_type
!= 0)
5530 result
= cp_convert (final_type
, result
, complain
);
5532 if (instrument_expr
!= NULL
)
5533 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
5534 instrument_expr
, result
);
5536 if (!processing_template_decl
)
5538 op0
= cp_fully_fold (op0
);
5539 /* Only consider the second argument if the first isn't overflowed. */
5540 if (!CONSTANT_CLASS_P (op0
) || TREE_OVERFLOW_P (op0
))
5542 op1
= cp_fully_fold (op1
);
5543 if (!CONSTANT_CLASS_P (op1
) || TREE_OVERFLOW_P (op1
))
5546 else if (!CONSTANT_CLASS_P (op0
) || !CONSTANT_CLASS_P (op1
)
5547 || TREE_OVERFLOW_P (op0
) || TREE_OVERFLOW_P (op1
))
5550 result_ovl
= fold_build2 (resultcode
, build_type
, op0
, op1
);
5551 if (TREE_OVERFLOW_P (result_ovl
))
5552 overflow_warning (location
, result_ovl
);
5557 /* Build a VEC_PERM_EXPR.
5558 This is a simple wrapper for c_build_vec_perm_expr. */
5560 build_x_vec_perm_expr (location_t loc
,
5561 tree arg0
, tree arg1
, tree arg2
,
5562 tsubst_flags_t complain
)
5564 tree orig_arg0
= arg0
;
5565 tree orig_arg1
= arg1
;
5566 tree orig_arg2
= arg2
;
5567 if (processing_template_decl
)
5569 if (type_dependent_expression_p (arg0
)
5570 || type_dependent_expression_p (arg1
)
5571 || type_dependent_expression_p (arg2
))
5572 return build_min_nt_loc (loc
, VEC_PERM_EXPR
, arg0
, arg1
, arg2
);
5573 arg0
= build_non_dependent_expr (arg0
);
5575 arg1
= build_non_dependent_expr (arg1
);
5576 arg2
= build_non_dependent_expr (arg2
);
5578 tree exp
= c_build_vec_perm_expr (loc
, arg0
, arg1
, arg2
, complain
& tf_error
);
5579 if (processing_template_decl
&& exp
!= error_mark_node
)
5580 return build_min_non_dep (VEC_PERM_EXPR
, exp
, orig_arg0
,
5581 orig_arg1
, orig_arg2
);
5585 /* Return a tree for the sum or difference (RESULTCODE says which)
5586 of pointer PTROP and integer INTOP. */
5589 cp_pointer_int_sum (location_t loc
, enum tree_code resultcode
, tree ptrop
,
5590 tree intop
, tsubst_flags_t complain
)
5592 tree res_type
= TREE_TYPE (ptrop
);
5594 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5595 in certain circumstance (when it's valid to do so). So we need
5596 to make sure it's complete. We don't need to check here, if we
5597 can actually complete it at all, as those checks will be done in
5598 pointer_int_sum() anyway. */
5599 complete_type (TREE_TYPE (res_type
));
5601 return pointer_int_sum (loc
, resultcode
, ptrop
,
5602 intop
, complain
& tf_warning_or_error
);
5605 /* Return a tree for the difference of pointers OP0 and OP1.
5606 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
5607 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5610 pointer_diff (location_t loc
, tree op0
, tree op1
, tree ptrtype
,
5611 tsubst_flags_t complain
, tree
*instrument_expr
)
5613 tree result
, inttype
;
5614 tree restype
= ptrdiff_type_node
;
5615 tree target_type
= TREE_TYPE (ptrtype
);
5617 if (!complete_type_or_else (target_type
, NULL_TREE
))
5618 return error_mark_node
;
5620 if (VOID_TYPE_P (target_type
))
5622 if (complain
& tf_error
)
5623 permerror (loc
, "ISO C++ forbids using pointer of "
5624 "type %<void *%> in subtraction");
5626 return error_mark_node
;
5628 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
5630 if (complain
& tf_error
)
5631 permerror (loc
, "ISO C++ forbids using pointer to "
5632 "a function in subtraction");
5634 return error_mark_node
;
5636 if (TREE_CODE (target_type
) == METHOD_TYPE
)
5638 if (complain
& tf_error
)
5639 permerror (loc
, "ISO C++ forbids using pointer to "
5640 "a method in subtraction");
5642 return error_mark_node
;
5645 /* Determine integer type result of the subtraction. This will usually
5646 be the same as the result type (ptrdiff_t), but may need to be a wider
5647 type if pointers for the address space are wider than ptrdiff_t. */
5648 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
5649 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
5653 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT
))
5655 op0
= save_expr (op0
);
5656 op1
= save_expr (op1
);
5658 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT
);
5659 *instrument_expr
= build_call_expr_loc (loc
, tt
, 2, op0
, op1
);
5662 /* First do the subtraction, then build the divide operator
5663 and only convert at the very end.
5664 Do not do default conversions in case restype is a short type. */
5666 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5667 pointers. If some platform cannot provide that, or has a larger
5668 ptrdiff_type to support differences larger than half the address
5669 space, cast the pointers to some larger integer type and do the
5670 computations in that type. */
5671 if (TYPE_PRECISION (inttype
) > TYPE_PRECISION (TREE_TYPE (op0
)))
5672 op0
= cp_build_binary_op (loc
,
5674 cp_convert (inttype
, op0
, complain
),
5675 cp_convert (inttype
, op1
, complain
),
5678 op0
= build2_loc (loc
, POINTER_DIFF_EXPR
, inttype
, op0
, op1
);
5680 /* This generates an error if op1 is a pointer to an incomplete type. */
5681 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
5683 if (complain
& tf_error
)
5684 error_at (loc
, "invalid use of a pointer to an incomplete type in "
5685 "pointer arithmetic");
5687 return error_mark_node
;
5690 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1
)))
5692 if (complain
& tf_error
)
5693 error_at (loc
, "arithmetic on pointer to an empty aggregate");
5695 return error_mark_node
;
5698 op1
= (TYPE_PTROB_P (ptrtype
)
5699 ? size_in_bytes_loc (loc
, target_type
)
5700 : integer_one_node
);
5702 /* Do the division. */
5704 result
= build2_loc (loc
, EXACT_DIV_EXPR
, inttype
, op0
,
5705 cp_convert (inttype
, op1
, complain
));
5706 return cp_convert (restype
, result
, complain
);
5709 /* Construct and perhaps optimize a tree representation
5710 for a unary operation. CODE, a tree_code, specifies the operation
5711 and XARG is the operand. */
5714 build_x_unary_op (location_t loc
, enum tree_code code
, cp_expr xarg
,
5715 tsubst_flags_t complain
)
5717 tree orig_expr
= xarg
;
5720 tree overload
= NULL_TREE
;
5722 if (processing_template_decl
)
5724 if (type_dependent_expression_p (xarg
))
5725 return build_min_nt_loc (loc
, code
, xarg
.get_value (), NULL_TREE
);
5727 xarg
= build_non_dependent_expr (xarg
);
5732 /* [expr.unary.op] says:
5734 The address of an object of incomplete type can be taken.
5736 (And is just the ordinary address operator, not an overloaded
5737 "operator &".) However, if the type is a template
5738 specialization, we must complete the type at this point so that
5739 an overloaded "operator &" will be available if required. */
5740 if (code
== ADDR_EXPR
5741 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
5742 && ((CLASS_TYPE_P (TREE_TYPE (xarg
))
5743 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg
))))
5744 || (TREE_CODE (xarg
) == OFFSET_REF
)))
5745 /* Don't look for a function. */;
5747 exp
= build_new_op (loc
, code
, LOOKUP_NORMAL
, xarg
, NULL_TREE
,
5748 NULL_TREE
, &overload
, complain
);
5750 if (!exp
&& code
== ADDR_EXPR
)
5752 if (is_overloaded_fn (xarg
))
5754 tree fn
= get_first_fn (xarg
);
5755 if (DECL_CONSTRUCTOR_P (fn
) || DECL_DESTRUCTOR_P (fn
))
5757 if (complain
& tf_error
)
5758 error (DECL_CONSTRUCTOR_P (fn
)
5759 ? G_("taking address of constructor %qD")
5760 : G_("taking address of destructor %qD"),
5762 return error_mark_node
;
5766 /* A pointer to member-function can be formed only by saying
5768 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
5769 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
5771 if (TREE_CODE (xarg
) != OFFSET_REF
5772 || !TYPE_P (TREE_OPERAND (xarg
, 0)))
5774 if (complain
& tf_error
)
5776 error ("invalid use of %qE to form a "
5777 "pointer-to-member-function", xarg
.get_value ());
5778 if (TREE_CODE (xarg
) != OFFSET_REF
)
5779 inform (input_location
, " a qualified-id is required");
5781 return error_mark_node
;
5785 if (complain
& tf_error
)
5786 error ("parentheses around %qE cannot be used to form a"
5787 " pointer-to-member-function",
5790 return error_mark_node
;
5791 PTRMEM_OK_P (xarg
) = 1;
5795 if (TREE_CODE (xarg
) == OFFSET_REF
)
5797 ptrmem
= PTRMEM_OK_P (xarg
);
5799 if (!ptrmem
&& !flag_ms_extensions
5800 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
5802 /* A single non-static member, make sure we don't allow a
5803 pointer-to-member. */
5804 xarg
= build2 (OFFSET_REF
, TREE_TYPE (xarg
),
5805 TREE_OPERAND (xarg
, 0),
5806 ovl_make (TREE_OPERAND (xarg
, 1)));
5807 PTRMEM_OK_P (xarg
) = ptrmem
;
5811 exp
= cp_build_addr_expr_strict (xarg
, complain
);
5814 if (processing_template_decl
&& exp
!= error_mark_node
)
5816 if (overload
!= NULL_TREE
)
5817 return (build_min_non_dep_op_overload
5818 (code
, exp
, overload
, orig_expr
, integer_zero_node
));
5820 exp
= build_min_non_dep (code
, exp
, orig_expr
,
5821 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE
);
5823 if (TREE_CODE (exp
) == ADDR_EXPR
)
5824 PTRMEM_OK_P (exp
) = ptrmem
;
5828 /* Construct and perhaps optimize a tree representation
5829 for __builtin_addressof operation. ARG specifies the operand. */
5832 cp_build_addressof (location_t loc
, tree arg
, tsubst_flags_t complain
)
5834 tree orig_expr
= arg
;
5836 if (processing_template_decl
)
5838 if (type_dependent_expression_p (arg
))
5839 return build_min_nt_loc (loc
, ADDRESSOF_EXPR
, arg
, NULL_TREE
);
5841 arg
= build_non_dependent_expr (arg
);
5844 tree exp
= cp_build_addr_expr_strict (arg
, complain
);
5846 if (processing_template_decl
&& exp
!= error_mark_node
)
5847 exp
= build_min_non_dep (ADDRESSOF_EXPR
, exp
, orig_expr
, NULL_TREE
);
5851 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5852 constants, where a null value is represented by an INTEGER_CST of
5856 cp_truthvalue_conversion (tree expr
)
5858 tree type
= TREE_TYPE (expr
);
5859 if (TYPE_PTR_OR_PTRMEM_P (type
)
5860 /* Avoid ICE on invalid use of non-static member function. */
5861 || TREE_CODE (expr
) == FUNCTION_DECL
)
5862 return build_binary_op (input_location
, NE_EXPR
, expr
, nullptr_node
, true);
5864 return c_common_truthvalue_conversion (input_location
, expr
);
5867 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5870 condition_conversion (tree expr
)
5873 /* Anything that might happen in a template should go through
5874 maybe_convert_cond. */
5875 gcc_assert (!processing_template_decl
);
5876 t
= perform_implicit_conversion_flags (boolean_type_node
, expr
,
5877 tf_warning_or_error
, LOOKUP_NORMAL
);
5878 t
= fold_build_cleanup_point_expr (boolean_type_node
, t
);
5882 /* Returns the address of T. This function will fold away
5883 ADDR_EXPR of INDIRECT_REF. */
5886 build_address (tree t
)
5888 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
5889 return error_mark_node
;
5890 gcc_checking_assert (TREE_CODE (t
) != CONSTRUCTOR
5891 || processing_template_decl
);
5892 t
= build_fold_addr_expr_loc (EXPR_LOCATION (t
), t
);
5893 if (TREE_CODE (t
) != ADDR_EXPR
)
5898 /* Return a NOP_EXPR converting EXPR to TYPE. */
5901 build_nop (tree type
, tree expr
)
5903 if (type
== error_mark_node
|| error_operand_p (expr
))
5905 return build1_loc (EXPR_LOCATION (expr
), NOP_EXPR
, type
, expr
);
5908 /* Take the address of ARG, whatever that means under C++ semantics.
5909 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5910 and class rvalues as well.
5912 Nothing should call this function directly; instead, callers should use
5913 cp_build_addr_expr or cp_build_addr_expr_strict. */
5916 cp_build_addr_expr_1 (tree arg
, bool strict_lvalue
, tsubst_flags_t complain
)
5921 if (!arg
|| error_operand_p (arg
))
5922 return error_mark_node
;
5924 arg
= mark_lvalue_use (arg
);
5925 if (error_operand_p (arg
))
5926 return error_mark_node
;
5928 argtype
= lvalue_type (arg
);
5930 gcc_assert (!(identifier_p (arg
) && IDENTIFIER_ANY_OP_P (arg
)));
5932 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
5933 && !really_overloaded_fn (arg
))
5935 /* They're trying to take the address of a unique non-static
5936 member function. This is ill-formed (except in MS-land),
5937 but let's try to DTRT.
5938 Note: We only handle unique functions here because we don't
5939 want to complain if there's a static overload; non-unique
5940 cases will be handled by instantiate_type. But we need to
5941 handle this case here to allow casts on the resulting PMF.
5942 We could defer this in non-MS mode, but it's easier to give
5943 a useful error here. */
5945 /* Inside constant member functions, the `this' pointer
5946 contains an extra const qualifier. TYPE_MAIN_VARIANT
5947 is used here to remove this const from the diagnostics
5948 and the created OFFSET_REF. */
5949 tree base
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg
, 0)));
5950 tree fn
= get_first_fn (TREE_OPERAND (arg
, 1));
5951 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
5952 return error_mark_node
;
5954 if (! flag_ms_extensions
)
5956 tree name
= DECL_NAME (fn
);
5957 if (!(complain
& tf_error
))
5958 return error_mark_node
;
5959 else if (current_class_type
5960 && TREE_OPERAND (arg
, 0) == current_class_ref
)
5961 /* An expression like &memfn. */
5962 permerror (input_location
, "ISO C++ forbids taking the address of an unqualified"
5963 " or parenthesized non-static member function to form"
5964 " a pointer to member function. Say %<&%T::%D%>",
5967 permerror (input_location
, "ISO C++ forbids taking the address of a bound member"
5968 " function to form a pointer to member function."
5972 arg
= build_offset_ref (base
, fn
, /*address_p=*/true, complain
);
5975 /* Uninstantiated types are all functions. Taking the
5976 address of a function is a no-op, so just return the
5978 if (type_unknown_p (arg
))
5979 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
5981 if (TREE_CODE (arg
) == OFFSET_REF
)
5982 /* We want a pointer to member; bypass all the code for actually taking
5983 the address of something. */
5986 /* Anything not already handled and not a true memory reference
5988 if (TREE_CODE (argtype
) != FUNCTION_TYPE
5989 && TREE_CODE (argtype
) != METHOD_TYPE
)
5991 cp_lvalue_kind kind
= lvalue_kind (arg
);
5992 if (kind
== clk_none
)
5994 if (complain
& tf_error
)
5995 lvalue_error (input_location
, lv_addressof
);
5996 return error_mark_node
;
5998 if (strict_lvalue
&& (kind
& (clk_rvalueref
|clk_class
)))
6000 if (!(complain
& tf_error
))
6001 return error_mark_node
;
6002 /* Make this a permerror because we used to accept it. */
6003 permerror (input_location
, "taking address of rvalue");
6007 if (TYPE_REF_P (argtype
))
6009 tree type
= build_pointer_type (TREE_TYPE (argtype
));
6010 arg
= build1 (CONVERT_EXPR
, type
, arg
);
6013 else if (pedantic
&& DECL_MAIN_P (arg
))
6016 /* Apparently a lot of autoconf scripts for C++ packages do this,
6017 so only complain if -Wpedantic. */
6018 if (complain
& (flag_pedantic_errors
? tf_error
: tf_warning
))
6019 pedwarn (input_location
, OPT_Wpedantic
,
6020 "ISO C++ forbids taking address of function %<::main%>");
6021 else if (flag_pedantic_errors
)
6022 return error_mark_node
;
6025 /* Let &* cancel out to simplify resulting code. */
6026 if (INDIRECT_REF_P (arg
))
6028 arg
= TREE_OPERAND (arg
, 0);
6029 if (TYPE_REF_P (TREE_TYPE (arg
)))
6031 tree type
= build_pointer_type (TREE_TYPE (TREE_TYPE (arg
)));
6032 arg
= build1 (CONVERT_EXPR
, type
, arg
);
6035 /* Don't let this be an lvalue. */
6040 /* Handle complex lvalues (when permitted)
6041 by reduction to simpler cases. */
6042 val
= unary_complex_lvalue (ADDR_EXPR
, arg
);
6046 switch (TREE_CODE (arg
))
6050 case FIX_TRUNC_EXPR
:
6051 /* We should have handled this above in the lvalue_kind check. */
6056 arg
= BASELINK_FUNCTIONS (arg
);
6060 arg
= OVL_FIRST (arg
);
6065 /* Turn a reference to a non-static data member into a
6066 pointer-to-member. */
6071 gcc_assert (PTRMEM_OK_P (arg
));
6073 t
= TREE_OPERAND (arg
, 1);
6074 if (TYPE_REF_P (TREE_TYPE (t
)))
6076 if (complain
& tf_error
)
6077 error ("cannot create pointer to reference member %qD", t
);
6078 return error_mark_node
;
6081 type
= build_ptrmem_type (context_for_name_lookup (t
),
6083 t
= make_ptrmem_cst (type
, TREE_OPERAND (arg
, 1));
6091 if (argtype
!= error_mark_node
)
6092 argtype
= build_pointer_type (argtype
);
6094 if (bitfield_p (arg
))
6096 if (complain
& tf_error
)
6097 error ("attempt to take address of bit-field");
6098 return error_mark_node
;
6101 /* In a template, we are processing a non-dependent expression
6102 so we can just form an ADDR_EXPR with the correct type. */
6103 if (processing_template_decl
|| TREE_CODE (arg
) != COMPONENT_REF
)
6105 tree stripped_arg
= tree_strip_any_location_wrapper (arg
);
6106 if (TREE_CODE (stripped_arg
) == FUNCTION_DECL
6107 && !mark_used (stripped_arg
, complain
) && !(complain
& tf_error
))
6108 return error_mark_node
;
6109 val
= build_address (arg
);
6110 if (TREE_CODE (arg
) == OFFSET_REF
)
6111 PTRMEM_OK_P (val
) = PTRMEM_OK_P (arg
);
6113 else if (BASELINK_P (TREE_OPERAND (arg
, 1)))
6115 tree fn
= BASELINK_FUNCTIONS (TREE_OPERAND (arg
, 1));
6117 /* We can only get here with a single static member
6119 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
6120 && DECL_STATIC_FUNCTION_P (fn
));
6121 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
6122 return error_mark_node
;
6123 val
= build_address (fn
);
6124 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
6125 /* Do not lose object's side effects. */
6126 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
),
6127 TREE_OPERAND (arg
, 0), val
);
6131 tree object
= TREE_OPERAND (arg
, 0);
6132 tree field
= TREE_OPERAND (arg
, 1);
6133 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6134 (TREE_TYPE (object
), decl_type_context (field
)));
6135 val
= build_address (arg
);
6138 if (TYPE_PTR_P (argtype
)
6139 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
6141 build_ptrmemfunc_type (argtype
);
6142 val
= build_ptrmemfunc (argtype
, val
, 0,
6150 /* Take the address of ARG if it has one, even if it's an rvalue. */
6153 cp_build_addr_expr (tree arg
, tsubst_flags_t complain
)
6155 return cp_build_addr_expr_1 (arg
, 0, complain
);
6158 /* Take the address of ARG, but only if it's an lvalue. */
6161 cp_build_addr_expr_strict (tree arg
, tsubst_flags_t complain
)
6163 return cp_build_addr_expr_1 (arg
, 1, complain
);
6166 /* C++: Must handle pointers to members.
6168 Perhaps type instantiation should be extended to handle conversion
6169 from aggregates to types we don't yet know we want? (Or are those
6170 cases typically errors which should be reported?)
6172 NOCONVERT suppresses the default promotions (such as from short to int). */
6175 cp_build_unary_op (enum tree_code code
, tree xarg
, bool noconvert
,
6176 tsubst_flags_t complain
)
6178 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
6180 location_t location
= cp_expr_loc_or_loc (arg
, input_location
);
6182 const char *errstring
= NULL
;
6184 const char *invalid_op_diag
;
6186 if (!arg
|| error_operand_p (arg
))
6187 return error_mark_node
;
6189 if ((invalid_op_diag
6190 = targetm
.invalid_unary_op ((code
== UNARY_PLUS_EXPR
6195 if (complain
& tf_error
)
6196 error (invalid_op_diag
);
6197 return error_mark_node
;
6202 case UNARY_PLUS_EXPR
:
6205 int flags
= WANT_ARITH
| WANT_ENUM
;
6206 /* Unary plus (but not unary minus) is allowed on pointers. */
6207 if (code
== UNARY_PLUS_EXPR
)
6208 flags
|= WANT_POINTER
;
6209 arg
= build_expr_type_conversion (flags
, arg
, true);
6211 errstring
= (code
== NEGATE_EXPR
6212 ? _("wrong type argument to unary minus")
6213 : _("wrong type argument to unary plus"));
6216 if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
6217 arg
= cp_perform_integral_promotions (arg
, complain
);
6219 /* Make sure the result is not an lvalue: a unary plus or minus
6220 expression is always a rvalue. */
6227 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
6232 arg
= cp_default_conversion (arg
, complain
);
6233 if (arg
== error_mark_node
)
6234 return error_mark_node
;
6237 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
6238 | WANT_VECTOR_OR_COMPLEX
,
6240 errstring
= _("wrong type argument to bit-complement");
6241 else if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
6243 /* Warn if the expression has boolean value. */
6244 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
6245 && (complain
& tf_warning
)
6246 && warning_at (location
, OPT_Wbool_operation
,
6247 "%<~%> on an expression of type bool"))
6248 inform (location
, "did you mean to use logical not (%<!%>)?");
6249 arg
= cp_perform_integral_promotions (arg
, complain
);
6251 else if (!noconvert
&& VECTOR_TYPE_P (TREE_TYPE (arg
)))
6252 arg
= mark_rvalue_use (arg
);
6256 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
6257 errstring
= _("wrong type argument to abs");
6258 else if (!noconvert
)
6260 arg
= cp_default_conversion (arg
, complain
);
6261 if (arg
== error_mark_node
)
6262 return error_mark_node
;
6267 /* Conjugating a real value is a no-op, but allow it anyway. */
6268 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
6269 errstring
= _("wrong type argument to conjugation");
6270 else if (!noconvert
)
6272 arg
= cp_default_conversion (arg
, complain
);
6273 if (arg
== error_mark_node
)
6274 return error_mark_node
;
6278 case TRUTH_NOT_EXPR
:
6279 if (VECTOR_TYPE_P (TREE_TYPE (arg
)))
6280 return cp_build_binary_op (input_location
, EQ_EXPR
, arg
,
6281 build_zero_cst (TREE_TYPE (arg
)), complain
);
6282 arg
= perform_implicit_conversion (boolean_type_node
, arg
,
6284 val
= invert_truthvalue_loc (input_location
, arg
);
6285 if (arg
!= error_mark_node
)
6287 errstring
= _("in argument to unary !");
6295 arg
= build_real_imag_expr (input_location
, code
, arg
);
6298 case PREINCREMENT_EXPR
:
6299 case POSTINCREMENT_EXPR
:
6300 case PREDECREMENT_EXPR
:
6301 case POSTDECREMENT_EXPR
:
6302 /* Handle complex lvalues (when permitted)
6303 by reduction to simpler cases. */
6305 val
= unary_complex_lvalue (code
, arg
);
6309 arg
= mark_lvalue_use (arg
);
6311 /* Increment or decrement the real part of the value,
6312 and don't change the imaginary part. */
6313 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
6317 arg
= cp_stabilize_reference (arg
);
6318 real
= cp_build_unary_op (REALPART_EXPR
, arg
, true, complain
);
6319 imag
= cp_build_unary_op (IMAGPART_EXPR
, arg
, true, complain
);
6320 real
= cp_build_unary_op (code
, real
, true, complain
);
6321 if (real
== error_mark_node
|| imag
== error_mark_node
)
6322 return error_mark_node
;
6323 return build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
6327 /* Report invalid types. */
6329 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
6332 if (code
== PREINCREMENT_EXPR
)
6333 errstring
= _("no pre-increment operator for type");
6334 else if (code
== POSTINCREMENT_EXPR
)
6335 errstring
= _("no post-increment operator for type");
6336 else if (code
== PREDECREMENT_EXPR
)
6337 errstring
= _("no pre-decrement operator for type");
6339 errstring
= _("no post-decrement operator for type");
6342 else if (arg
== error_mark_node
)
6343 return error_mark_node
;
6345 /* Report something read-only. */
6347 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
6348 || TREE_READONLY (arg
))
6350 if (complain
& tf_error
)
6351 cxx_readonly_error (location
, arg
,
6352 ((code
== PREINCREMENT_EXPR
6353 || code
== POSTINCREMENT_EXPR
)
6354 ? lv_increment
: lv_decrement
));
6356 return error_mark_node
;
6361 tree declared_type
= unlowered_expr_type (arg
);
6363 argtype
= TREE_TYPE (arg
);
6365 /* ARM $5.2.5 last annotation says this should be forbidden. */
6366 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
6368 if (complain
& tf_error
)
6369 permerror (input_location
, (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
6370 ? G_("ISO C++ forbids incrementing an enum")
6371 : G_("ISO C++ forbids decrementing an enum"));
6373 return error_mark_node
;
6376 /* Compute the increment. */
6378 if (TYPE_PTR_P (argtype
))
6380 tree type
= complete_type (TREE_TYPE (argtype
));
6382 if (!COMPLETE_OR_VOID_TYPE_P (type
))
6384 if (complain
& tf_error
)
6385 error (((code
== PREINCREMENT_EXPR
6386 || code
== POSTINCREMENT_EXPR
))
6387 ? G_("cannot increment a pointer to incomplete type %qT")
6388 : G_("cannot decrement a pointer to incomplete type %qT"),
6389 TREE_TYPE (argtype
));
6391 return error_mark_node
;
6393 else if (!TYPE_PTROB_P (argtype
))
6395 if (complain
& tf_error
)
6396 pedwarn (input_location
, OPT_Wpointer_arith
,
6397 (code
== PREINCREMENT_EXPR
6398 || code
== POSTINCREMENT_EXPR
)
6399 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6400 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6403 return error_mark_node
;
6406 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
6409 inc
= VECTOR_TYPE_P (argtype
)
6410 ? build_one_cst (argtype
)
6413 inc
= cp_convert (argtype
, inc
, complain
);
6415 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6416 need to ask Objective-C to build the increment or decrement
6417 expression for it. */
6418 if (objc_is_property_ref (arg
))
6419 return objc_build_incr_expr_for_property_ref (input_location
, code
,
6422 /* Complain about anything else that is not a true lvalue. */
6423 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
6424 || code
== POSTINCREMENT_EXPR
)
6425 ? lv_increment
: lv_decrement
),
6427 return error_mark_node
;
6429 /* Forbid using -- or ++ in C++17 on `bool'. */
6430 if (TREE_CODE (declared_type
) == BOOLEAN_TYPE
)
6432 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
6434 if (complain
& tf_error
)
6435 error ("use of an operand of type %qT in %<operator--%> "
6436 "is forbidden", boolean_type_node
);
6437 return error_mark_node
;
6441 if (cxx_dialect
>= cxx17
)
6443 if (complain
& tf_error
)
6444 error ("use of an operand of type %qT in "
6445 "%<operator++%> is forbidden in C++17",
6447 return error_mark_node
;
6449 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6450 else if (!in_system_header_at (input_location
))
6451 warning (OPT_Wdeprecated
, "use of an operand of type %qT "
6452 "in %<operator++%> is deprecated",
6455 val
= boolean_increment (code
, arg
);
6457 else if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
6458 /* An rvalue has no cv-qualifiers. */
6459 val
= build2 (code
, cv_unqualified (TREE_TYPE (arg
)), arg
, inc
);
6461 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
6463 TREE_SIDE_EFFECTS (val
) = 1;
6468 /* Note that this operation never does default_conversion
6469 regardless of NOCONVERT. */
6470 return cp_build_addr_expr (arg
, complain
);
6479 argtype
= TREE_TYPE (arg
);
6480 return build1 (code
, argtype
, arg
);
6483 if (complain
& tf_error
)
6484 error ("%s", errstring
);
6485 return error_mark_node
;
6488 /* Hook for the c-common bits that build a unary op. */
6490 build_unary_op (location_t
/*location*/,
6491 enum tree_code code
, tree xarg
, bool noconvert
)
6493 return cp_build_unary_op (code
, xarg
, noconvert
, tf_warning_or_error
);
6496 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
6497 so that it is a valid lvalue even for GENERIC by replacing
6498 (lhs = rhs) with ((lhs = rhs), lhs)
6499 (--lhs) with ((--lhs), lhs)
6500 (++lhs) with ((++lhs), lhs)
6501 and if lhs has side-effects, calling cp_stabilize_reference on it, so
6502 that it can be evaluated multiple times. */
6505 genericize_compound_lvalue (tree lvalue
)
6507 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue
, 0)))
6508 lvalue
= build2 (TREE_CODE (lvalue
), TREE_TYPE (lvalue
),
6509 cp_stabilize_reference (TREE_OPERAND (lvalue
, 0)),
6510 TREE_OPERAND (lvalue
, 1));
6511 return build2 (COMPOUND_EXPR
, TREE_TYPE (TREE_OPERAND (lvalue
, 0)),
6512 lvalue
, TREE_OPERAND (lvalue
, 0));
6515 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6516 for certain kinds of expressions which are not really lvalues
6517 but which we can accept as lvalues.
6519 If ARG is not a kind of expression we can handle, return
6523 unary_complex_lvalue (enum tree_code code
, tree arg
)
6525 /* Inside a template, making these kinds of adjustments is
6526 pointless; we are only concerned with the type of the
6528 if (processing_template_decl
)
6531 /* Handle (a, b) used as an "lvalue". */
6532 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
6534 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 1), false,
6535 tf_warning_or_error
);
6536 return build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6537 TREE_OPERAND (arg
, 0), real_result
);
6540 /* Handle (a ? b : c) used as an "lvalue". */
6541 if (TREE_CODE (arg
) == COND_EXPR
6542 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
6543 return rationalize_conditional_expr (code
, arg
, tf_warning_or_error
);
6545 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6546 if (TREE_CODE (arg
) == MODIFY_EXPR
6547 || TREE_CODE (arg
) == PREINCREMENT_EXPR
6548 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
6549 return unary_complex_lvalue (code
, genericize_compound_lvalue (arg
));
6551 if (code
!= ADDR_EXPR
)
6554 /* Handle (a = b) used as an "lvalue" for `&'. */
6555 if (TREE_CODE (arg
) == MODIFY_EXPR
6556 || TREE_CODE (arg
) == INIT_EXPR
)
6558 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 0), false,
6559 tf_warning_or_error
);
6560 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6562 TREE_NO_WARNING (arg
) = 1;
6566 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
6567 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
6568 || TREE_CODE (arg
) == OFFSET_REF
)
6571 /* We permit compiler to make function calls returning
6572 objects of aggregate type look like lvalues. */
6576 if (TREE_CODE (targ
) == SAVE_EXPR
)
6577 targ
= TREE_OPERAND (targ
, 0);
6579 if (TREE_CODE (targ
) == CALL_EXPR
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (targ
)))
6581 if (TREE_CODE (arg
) == SAVE_EXPR
)
6584 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, tf_warning_or_error
);
6585 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
6588 if (TREE_CODE (arg
) == SAVE_EXPR
&& INDIRECT_REF_P (targ
))
6589 return build3 (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
6590 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
6593 /* Don't let anything else be handled specially. */
6597 /* Mark EXP saying that we need to be able to take the
6598 address of it; it should not be allocated in a register.
6599 Value is true if successful. ARRAY_REF_P is true if this
6600 is for ARRAY_REF construction - in that case we don't want
6601 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6602 it is fine to use ARRAY_REFs for vector subscripts on vector
6605 C++: we do not allow `current_class_ptr' to be addressable. */
6608 cxx_mark_addressable (tree exp
, bool array_ref_p
)
6613 switch (TREE_CODE (x
))
6615 case VIEW_CONVERT_EXPR
:
6617 && TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
6618 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x
, 0))))
6626 x
= TREE_OPERAND (x
, 0);
6630 if (x
== current_class_ptr
)
6632 error ("cannot take the address of %<this%>, which is an rvalue expression");
6633 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later. */
6639 /* Caller should not be trying to mark initialized
6640 constant fields addressable. */
6641 gcc_assert (DECL_LANG_SPECIFIC (x
) == 0
6642 || DECL_IN_AGGR_P (x
) == 0
6644 || DECL_EXTERNAL (x
));
6648 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
6649 && !DECL_ARTIFICIAL (x
))
6651 if (VAR_P (x
) && DECL_HARD_REGISTER (x
))
6654 ("address of explicit register variable %qD requested", x
);
6657 else if (extra_warnings
)
6659 (OPT_Wextra
, "address requested for %qD, which is declared %<register%>", x
);
6661 TREE_ADDRESSABLE (x
) = 1;
6666 TREE_ADDRESSABLE (x
) = 1;
6670 TREE_ADDRESSABLE (x
) = 1;
6674 TREE_ADDRESSABLE (x
) = 1;
6675 cxx_mark_addressable (TREE_OPERAND (x
, 0));
6683 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6686 build_x_conditional_expr (location_t loc
, tree ifexp
, tree op1
, tree op2
,
6687 tsubst_flags_t complain
)
6689 tree orig_ifexp
= ifexp
;
6690 tree orig_op1
= op1
;
6691 tree orig_op2
= op2
;
6694 if (processing_template_decl
)
6696 /* The standard says that the expression is type-dependent if
6697 IFEXP is type-dependent, even though the eventual type of the
6698 expression doesn't dependent on IFEXP. */
6699 if (type_dependent_expression_p (ifexp
)
6700 /* As a GNU extension, the middle operand may be omitted. */
6701 || (op1
&& type_dependent_expression_p (op1
))
6702 || type_dependent_expression_p (op2
))
6703 return build_min_nt_loc (loc
, COND_EXPR
, ifexp
, op1
, op2
);
6704 ifexp
= build_non_dependent_expr (ifexp
);
6706 op1
= build_non_dependent_expr (op1
);
6707 op2
= build_non_dependent_expr (op2
);
6710 expr
= build_conditional_expr (loc
, ifexp
, op1
, op2
, complain
);
6711 if (processing_template_decl
&& expr
!= error_mark_node
)
6713 tree min
= build_min_non_dep (COND_EXPR
, expr
,
6714 orig_ifexp
, orig_op1
, orig_op2
);
6715 expr
= convert_from_reference (min
);
6720 /* Given a list of expressions, return a compound expression
6721 that performs them all and returns the value of the last of them. */
6724 build_x_compound_expr_from_list (tree list
, expr_list_kind exp
,
6725 tsubst_flags_t complain
)
6727 tree expr
= TREE_VALUE (list
);
6729 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
6730 && !CONSTRUCTOR_IS_DIRECT_INIT (expr
))
6732 if (complain
& tf_error
)
6733 pedwarn (cp_expr_loc_or_loc (expr
, input_location
), 0,
6734 "list-initializer for non-class type must not "
6735 "be parenthesized");
6737 return error_mark_node
;
6740 if (TREE_CHAIN (list
))
6742 if (complain
& tf_error
)
6746 permerror (input_location
, "expression list treated as compound "
6747 "expression in initializer");
6750 permerror (input_location
, "expression list treated as compound "
6751 "expression in mem-initializer");
6754 permerror (input_location
, "expression list treated as compound "
6755 "expression in functional cast");
6761 return error_mark_node
;
6763 for (list
= TREE_CHAIN (list
); list
; list
= TREE_CHAIN (list
))
6764 expr
= build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list
)),
6765 expr
, TREE_VALUE (list
), complain
);
6771 /* Like build_x_compound_expr_from_list, but using a VEC. */
6774 build_x_compound_expr_from_vec (vec
<tree
, va_gc
> *vec
, const char *msg
,
6775 tsubst_flags_t complain
)
6777 if (vec_safe_is_empty (vec
))
6779 else if (vec
->length () == 1)
6789 if (complain
& tf_error
)
6790 permerror (input_location
,
6791 "%s expression list treated as compound expression",
6794 return error_mark_node
;
6798 for (ix
= 1; vec
->iterate (ix
, &t
); ++ix
)
6799 expr
= build_x_compound_expr (EXPR_LOCATION (t
), expr
,
6806 /* Handle overloading of the ',' operator when needed. */
6809 build_x_compound_expr (location_t loc
, tree op1
, tree op2
,
6810 tsubst_flags_t complain
)
6813 tree orig_op1
= op1
;
6814 tree orig_op2
= op2
;
6815 tree overload
= NULL_TREE
;
6817 if (processing_template_decl
)
6819 if (type_dependent_expression_p (op1
)
6820 || type_dependent_expression_p (op2
))
6821 return build_min_nt_loc (loc
, COMPOUND_EXPR
, op1
, op2
);
6822 op1
= build_non_dependent_expr (op1
);
6823 op2
= build_non_dependent_expr (op2
);
6826 result
= build_new_op (loc
, COMPOUND_EXPR
, LOOKUP_NORMAL
, op1
, op2
,
6827 NULL_TREE
, &overload
, complain
);
6829 result
= cp_build_compound_expr (op1
, op2
, complain
);
6831 if (processing_template_decl
&& result
!= error_mark_node
)
6833 if (overload
!= NULL_TREE
)
6834 return (build_min_non_dep_op_overload
6835 (COMPOUND_EXPR
, result
, overload
, orig_op1
, orig_op2
));
6837 return build_min_non_dep (COMPOUND_EXPR
, result
, orig_op1
, orig_op2
);
6843 /* Like cp_build_compound_expr, but for the c-common bits. */
6846 build_compound_expr (location_t
/*loc*/, tree lhs
, tree rhs
)
6848 return cp_build_compound_expr (lhs
, rhs
, tf_warning_or_error
);
6851 /* Build a compound expression. */
6854 cp_build_compound_expr (tree lhs
, tree rhs
, tsubst_flags_t complain
)
6856 lhs
= convert_to_void (lhs
, ICV_LEFT_OF_COMMA
, complain
);
6858 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
6859 return error_mark_node
;
6861 if (TREE_CODE (rhs
) == TARGET_EXPR
)
6863 /* If the rhs is a TARGET_EXPR, then build the compound
6864 expression inside the target_expr's initializer. This
6865 helps the compiler to eliminate unnecessary temporaries. */
6866 tree init
= TREE_OPERAND (rhs
, 1);
6868 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), lhs
, init
);
6869 TREE_OPERAND (rhs
, 1) = init
;
6874 if (type_unknown_p (rhs
))
6876 if (complain
& tf_error
)
6877 error ("no context to resolve type of %qE", rhs
);
6878 return error_mark_node
;
6881 return build2 (COMPOUND_EXPR
, TREE_TYPE (rhs
), lhs
, rhs
);
6884 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6885 casts away constness. CAST gives the type of cast. Returns true
6886 if the cast is ill-formed, false if it is well-formed.
6888 ??? This function warns for casting away any qualifier not just
6889 const. We would like to specify exactly what qualifiers are casted
6894 check_for_casting_away_constness (tree src_type
, tree dest_type
,
6895 enum tree_code cast
, tsubst_flags_t complain
)
6897 /* C-style casts are allowed to cast away constness. With
6898 WARN_CAST_QUAL, we still want to issue a warning. */
6899 if (cast
== CAST_EXPR
&& !warn_cast_qual
)
6902 if (!casts_away_constness (src_type
, dest_type
, complain
))
6908 if (complain
& tf_warning
)
6909 warning (OPT_Wcast_qual
,
6910 "cast from type %qT to type %qT casts away qualifiers",
6911 src_type
, dest_type
);
6914 case STATIC_CAST_EXPR
:
6915 if (complain
& tf_error
)
6916 error ("static_cast from type %qT to type %qT casts away qualifiers",
6917 src_type
, dest_type
);
6920 case REINTERPRET_CAST_EXPR
:
6921 if (complain
& tf_error
)
6922 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6923 src_type
, dest_type
);
6931 /* Warns if the cast from expression EXPR to type TYPE is useless. */
6933 maybe_warn_about_useless_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6935 if (warn_useless_cast
6936 && complain
& tf_warning
)
6938 if ((TYPE_REF_P (type
)
6939 && (TYPE_REF_IS_RVALUE (type
)
6940 ? xvalue_p (expr
) : lvalue_p (expr
))
6941 && same_type_p (TREE_TYPE (expr
), TREE_TYPE (type
)))
6942 || same_type_p (TREE_TYPE (expr
), type
))
6943 warning (OPT_Wuseless_cast
, "useless cast to type %q#T", type
);
6947 /* Warns if the cast ignores cv-qualifiers on TYPE. */
6949 maybe_warn_about_cast_ignoring_quals (tree type
, tsubst_flags_t complain
)
6951 if (warn_ignored_qualifiers
6952 && complain
& tf_warning
6953 && !CLASS_TYPE_P (type
)
6954 && (cp_type_quals (type
) & (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)))
6956 warning (OPT_Wignored_qualifiers
, "type qualifiers ignored on cast "
6961 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6962 (another pointer-to-member type in the same hierarchy) and return
6963 the converted expression. If ALLOW_INVERSE_P is permitted, a
6964 pointer-to-derived may be converted to pointer-to-base; otherwise,
6965 only the other direction is permitted. If C_CAST_P is true, this
6966 conversion is taking place as part of a C-style cast. */
6969 convert_ptrmem (tree type
, tree expr
, bool allow_inverse_p
,
6970 bool c_cast_p
, tsubst_flags_t complain
)
6972 if (same_type_p (type
, TREE_TYPE (expr
)))
6975 if (TYPE_PTRDATAMEM_P (type
))
6977 tree obase
= TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr
));
6978 tree nbase
= TYPE_PTRMEM_CLASS_TYPE (type
);
6979 tree delta
= (get_delta_difference
6981 allow_inverse_p
, c_cast_p
, complain
));
6983 if (delta
== error_mark_node
)
6984 return error_mark_node
;
6986 if (!same_type_p (obase
, nbase
))
6988 if (TREE_CODE (expr
) == PTRMEM_CST
)
6989 expr
= cplus_expand_constant (expr
);
6991 tree cond
= cp_build_binary_op (input_location
, EQ_EXPR
, expr
,
6992 build_int_cst (TREE_TYPE (expr
), -1),
6994 tree op1
= build_nop (ptrdiff_type_node
, expr
);
6995 tree op2
= cp_build_binary_op (input_location
, PLUS_EXPR
, op1
, delta
,
6998 expr
= fold_build3_loc (input_location
,
6999 COND_EXPR
, ptrdiff_type_node
, cond
, op1
, op2
);
7002 return build_nop (type
, expr
);
7005 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
,
7006 allow_inverse_p
, c_cast_p
, complain
);
7009 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
7010 this static_cast is being attempted as one of the possible casts
7011 allowed by a C-style cast. (In that case, accessibility of base
7012 classes is not considered, and it is OK to cast away
7013 constness.) Return the result of the cast. *VALID_P is set to
7014 indicate whether or not the cast was valid. */
7017 build_static_cast_1 (tree type
, tree expr
, bool c_cast_p
,
7018 bool *valid_p
, tsubst_flags_t complain
)
7024 /* Assume the cast is valid. */
7027 intype
= unlowered_expr_type (expr
);
7029 /* Save casted types in the function's used types hash table. */
7030 used_types_insert (type
);
7032 /* A prvalue of non-class type is cv-unqualified. */
7033 if (!CLASS_TYPE_P (type
))
7034 type
= cv_unqualified (type
);
7036 /* [expr.static.cast]
7038 An lvalue of type "cv1 B", where B is a class type, can be cast
7039 to type "reference to cv2 D", where D is a class derived (clause
7040 _class.derived_) from B, if a valid standard conversion from
7041 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
7042 same cv-qualification as, or greater cv-qualification than, cv1,
7043 and B is not a virtual base class of D. */
7044 /* We check this case before checking the validity of "TYPE t =
7045 EXPR;" below because for this case:
7048 struct D : public B { D(const B&); };
7050 void f() { static_cast<const D&>(b); }
7052 we want to avoid constructing a new D. The standard is not
7053 completely clear about this issue, but our interpretation is
7054 consistent with other compilers. */
7055 if (TYPE_REF_P (type
)
7056 && CLASS_TYPE_P (TREE_TYPE (type
))
7057 && CLASS_TYPE_P (intype
)
7058 && (TYPE_REF_IS_RVALUE (type
) || lvalue_p (expr
))
7059 && DERIVED_FROM_P (intype
, TREE_TYPE (type
))
7060 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype
)),
7061 build_pointer_type (TYPE_MAIN_VARIANT
7062 (TREE_TYPE (type
))),
7065 || at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
7069 if (processing_template_decl
)
7072 /* There is a standard conversion from "D*" to "B*" even if "B"
7073 is ambiguous or inaccessible. If this is really a
7074 static_cast, then we check both for inaccessibility and
7075 ambiguity. However, if this is a static_cast being performed
7076 because the user wrote a C-style cast, then accessibility is
7078 base
= lookup_base (TREE_TYPE (type
), intype
,
7079 c_cast_p
? ba_unique
: ba_check
,
7081 expr
= build_address (expr
);
7083 if (sanitize_flags_p (SANITIZE_VPTR
))
7086 = cp_ubsan_maybe_instrument_downcast (input_location
, type
,
7092 /* Convert from "B*" to "D*". This function will check that "B"
7093 is not a virtual base of "D". Even if we don't have a guarantee
7094 that expr is NULL, if the static_cast is to a reference type,
7095 it is UB if it would be NULL, so omit the non-NULL check. */
7096 expr
= build_base_path (MINUS_EXPR
, expr
, base
,
7097 /*nonnull=*/flag_delete_null_pointer_checks
,
7100 /* Convert the pointer to a reference -- but then remember that
7101 there are no expressions with reference type in C++.
7103 We call rvalue so that there's an actual tree code
7104 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
7105 is a variable with the same type, the conversion would get folded
7106 away, leaving just the variable and causing lvalue_kind to give
7107 the wrong answer. */
7108 expr
= cp_fold_convert (type
, expr
);
7110 /* When -fsanitize=null, make sure to diagnose reference binding to
7111 NULL even when the reference is converted to pointer later on. */
7112 if (sanitize_flags_p (SANITIZE_NULL
)
7113 && TREE_CODE (expr
) == COND_EXPR
7114 && TREE_OPERAND (expr
, 2)
7115 && TREE_CODE (TREE_OPERAND (expr
, 2)) == INTEGER_CST
7116 && TREE_TYPE (TREE_OPERAND (expr
, 2)) == type
)
7117 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr
, 2));
7119 return convert_from_reference (rvalue (expr
));
7122 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
7123 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
7124 if (TYPE_REF_P (type
)
7125 && TYPE_REF_IS_RVALUE (type
)
7126 && (clk
= real_lvalue_p (expr
))
7127 && reference_related_p (TREE_TYPE (type
), intype
)
7128 && (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
7130 if (processing_template_decl
)
7132 if (clk
== clk_ordinary
)
7134 /* Handle the (non-bit-field) lvalue case here by casting to
7135 lvalue reference and then changing it to an rvalue reference.
7136 Casting an xvalue to rvalue reference will be handled by the
7138 tree lref
= cp_build_reference_type (TREE_TYPE (type
), false);
7139 result
= (perform_direct_initialization_if_possible
7140 (lref
, expr
, c_cast_p
, complain
));
7141 result
= build1 (NON_LVALUE_EXPR
, type
, result
);
7142 return convert_from_reference (result
);
7145 /* For a bit-field or packed field, bind to a temporary. */
7146 expr
= rvalue (expr
);
7149 /* Resolve overloaded address here rather than once in
7150 implicit_conversion and again in the inverse code below. */
7151 if (TYPE_PTRMEMFUNC_P (type
) && type_unknown_p (expr
))
7153 expr
= instantiate_type (type
, expr
, complain
);
7154 intype
= TREE_TYPE (expr
);
7157 /* [expr.static.cast]
7159 Any expression can be explicitly converted to type cv void. */
7160 if (VOID_TYPE_P (type
))
7161 return convert_to_void (expr
, ICV_CAST
, complain
);
7164 An abstract class shall not be used ... as the type of an explicit
7166 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
7167 return error_mark_node
;
7169 /* [expr.static.cast]
7171 An expression e can be explicitly converted to a type T using a
7172 static_cast of the form static_cast<T>(e) if the declaration T
7173 t(e);" is well-formed, for some invented temporary variable
7175 result
= perform_direct_initialization_if_possible (type
, expr
,
7176 c_cast_p
, complain
);
7179 if (processing_template_decl
)
7182 result
= convert_from_reference (result
);
7184 /* [expr.static.cast]
7186 If T is a reference type, the result is an lvalue; otherwise,
7187 the result is an rvalue. */
7188 if (!TYPE_REF_P (type
))
7190 result
= rvalue (result
);
7192 if (result
== expr
&& SCALAR_TYPE_P (type
))
7193 /* Leave some record of the cast. */
7194 result
= build_nop (type
, expr
);
7199 /* [expr.static.cast]
7201 The inverse of any standard conversion sequence (clause _conv_),
7202 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
7203 (_conv.array_), function-to-pointer (_conv.func_), and boolean
7204 (_conv.bool_) conversions, can be performed explicitly using
7205 static_cast subject to the restriction that the explicit
7206 conversion does not cast away constness (_expr.const.cast_), and
7207 the following additional rules for specific cases: */
7208 /* For reference, the conversions not excluded are: integral
7209 promotions, floating point promotion, integral conversions,
7210 floating point conversions, floating-integral conversions,
7211 pointer conversions, and pointer to member conversions. */
7214 A value of integral _or enumeration_ type can be explicitly
7215 converted to an enumeration type. */
7216 /* The effect of all that is that any conversion between any two
7217 types which are integral, floating, or enumeration types can be
7219 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7220 || SCALAR_FLOAT_TYPE_P (type
))
7221 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
7222 || SCALAR_FLOAT_TYPE_P (intype
)))
7224 if (processing_template_decl
)
7226 return ocp_convert (type
, expr
, CONV_C_CAST
, LOOKUP_NORMAL
, complain
);
7229 if (TYPE_PTR_P (type
) && TYPE_PTR_P (intype
)
7230 && CLASS_TYPE_P (TREE_TYPE (type
))
7231 && CLASS_TYPE_P (TREE_TYPE (intype
))
7232 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7233 (TREE_TYPE (intype
))),
7234 build_pointer_type (TYPE_MAIN_VARIANT
7235 (TREE_TYPE (type
))),
7240 if (processing_template_decl
)
7244 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
7246 return error_mark_node
;
7247 base
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
7248 c_cast_p
? ba_unique
: ba_check
,
7250 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
7253 if (sanitize_flags_p (SANITIZE_VPTR
))
7256 = cp_ubsan_maybe_instrument_downcast (input_location
, type
,
7262 return cp_fold_convert (type
, expr
);
7265 if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
7266 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
7273 c1
= TYPE_PTRMEM_CLASS_TYPE (intype
);
7274 c2
= TYPE_PTRMEM_CLASS_TYPE (type
);
7276 if (TYPE_PTRDATAMEM_P (type
))
7278 t1
= (build_ptrmem_type
7280 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype
))));
7281 t2
= (build_ptrmem_type
7283 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
7290 if (can_convert (t1
, t2
, complain
) || can_convert (t2
, t1
, complain
))
7293 && check_for_casting_away_constness (intype
, type
,
7296 return error_mark_node
;
7297 if (processing_template_decl
)
7299 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/1,
7300 c_cast_p
, complain
);
7304 /* [expr.static.cast]
7306 An rvalue of type "pointer to cv void" can be explicitly
7307 converted to a pointer to object type. A value of type pointer
7308 to object converted to "pointer to cv void" and back to the
7309 original pointer type will have its original value. */
7310 if (TYPE_PTR_P (intype
)
7311 && VOID_TYPE_P (TREE_TYPE (intype
))
7312 && TYPE_PTROB_P (type
))
7315 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
7317 return error_mark_node
;
7318 if (processing_template_decl
)
7320 return build_nop (type
, expr
);
7324 return error_mark_node
;
7327 /* Return an expression representing static_cast<TYPE>(EXPR). */
7330 build_static_cast (tree type
, tree oexpr
, tsubst_flags_t complain
)
7336 if (type
== error_mark_node
|| expr
== error_mark_node
)
7337 return error_mark_node
;
7339 bool dependent
= (dependent_type_p (type
)
7340 || type_dependent_expression_p (expr
));
7344 expr
= build_min (STATIC_CAST_EXPR
, type
, oexpr
);
7345 /* We don't know if it will or will not have side effects. */
7346 TREE_SIDE_EFFECTS (expr
) = 1;
7347 return convert_from_reference (expr
);
7349 else if (processing_template_decl
)
7350 expr
= build_non_dependent_expr (expr
);
7352 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7353 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7354 if (!TYPE_REF_P (type
)
7355 && TREE_CODE (expr
) == NOP_EXPR
7356 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7357 expr
= TREE_OPERAND (expr
, 0);
7359 result
= build_static_cast_1 (type
, expr
, /*c_cast_p=*/false, &valid_p
,
7363 if (result
!= error_mark_node
)
7365 maybe_warn_about_useless_cast (type
, expr
, complain
);
7366 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7368 if (processing_template_decl
)
7373 if (complain
& tf_error
)
7375 error ("invalid static_cast from type %qT to type %qT",
7376 TREE_TYPE (expr
), type
);
7377 if ((TYPE_PTR_P (type
) || TYPE_REF_P (type
))
7378 && CLASS_TYPE_P (TREE_TYPE (type
))
7379 && !COMPLETE_TYPE_P (TREE_TYPE (type
)))
7380 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type
))),
7381 "class type %qT is incomplete", TREE_TYPE (type
));
7382 tree expr_type
= TREE_TYPE (expr
);
7383 if (TYPE_PTR_P (expr_type
))
7384 expr_type
= TREE_TYPE (expr_type
);
7385 if (CLASS_TYPE_P (expr_type
) && !COMPLETE_TYPE_P (expr_type
))
7386 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type
)),
7387 "class type %qT is incomplete", expr_type
);
7389 return error_mark_node
;
7392 /* EXPR is an expression with member function or pointer-to-member
7393 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7394 not permitted by ISO C++, but we accept it in some modes. If we
7395 are not in one of those modes, issue a diagnostic. Return the
7396 converted expression. */
7399 convert_member_func_to_ptr (tree type
, tree expr
, tsubst_flags_t complain
)
7404 intype
= TREE_TYPE (expr
);
7405 gcc_assert (TYPE_PTRMEMFUNC_P (intype
)
7406 || TREE_CODE (intype
) == METHOD_TYPE
);
7408 if (!(complain
& tf_warning_or_error
))
7409 return error_mark_node
;
7411 if (pedantic
|| warn_pmf2ptr
)
7412 pedwarn (input_location
, pedantic
? OPT_Wpedantic
: OPT_Wpmf_conversions
,
7413 "converting from %qH to %qI", intype
, type
);
7415 if (TREE_CODE (intype
) == METHOD_TYPE
)
7416 expr
= build_addr_func (expr
, complain
);
7417 else if (TREE_CODE (expr
) == PTRMEM_CST
)
7418 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
7421 decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
), 0);
7422 decl
= build_address (decl
);
7423 expr
= get_member_function_from_ptrfunc (&decl
, expr
, complain
);
7426 if (expr
== error_mark_node
)
7427 return error_mark_node
;
7429 return build_nop (type
, expr
);
7432 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
7433 constexpr evaluation knows to reject it. */
7436 build_nop_reinterpret (tree type
, tree expr
)
7438 tree ret
= build_nop (type
, expr
);
7440 REINTERPRET_CAST_P (ret
) = true;
7444 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7445 If C_CAST_P is true, this reinterpret cast is being done as part of
7446 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7447 indicate whether or not reinterpret_cast was valid. */
7450 build_reinterpret_cast_1 (tree type
, tree expr
, bool c_cast_p
,
7451 bool *valid_p
, tsubst_flags_t complain
)
7455 /* Assume the cast is invalid. */
7459 if (type
== error_mark_node
|| error_operand_p (expr
))
7460 return error_mark_node
;
7462 intype
= TREE_TYPE (expr
);
7464 /* Save casted types in the function's used types hash table. */
7465 used_types_insert (type
);
7467 /* A prvalue of non-class type is cv-unqualified. */
7468 if (!CLASS_TYPE_P (type
))
7469 type
= cv_unqualified (type
);
7471 /* [expr.reinterpret.cast]
7472 A glvalue expression of type T1 can be cast to the type
7473 "reference to T2" if an expression of type "pointer to T1" can be
7474 explicitly converted to the type "pointer to T2" using a
7475 reinterpret_cast. */
7476 if (TYPE_REF_P (type
))
7478 if (TYPE_REF_IS_RVALUE (type
))
7480 if (!obvalue_p (expr
))
7481 /* Perform the temporary materialization conversion. */
7482 expr
= get_target_expr_sfinae (expr
, complain
);
7484 else if (!lvalue_p (expr
))
7486 if (complain
& tf_error
)
7487 error ("invalid cast of an rvalue expression of type "
7490 return error_mark_node
;
7493 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7494 "B" are related class types; the reinterpret_cast does not
7495 adjust the pointer. */
7496 if (TYPE_PTR_P (intype
)
7497 && (complain
& tf_warning
)
7498 && (comptypes (TREE_TYPE (intype
), TREE_TYPE (type
),
7499 COMPARE_BASE
| COMPARE_DERIVED
)))
7500 warning (0, "casting %qT to %qT does not dereference pointer",
7503 expr
= cp_build_addr_expr (expr
, complain
);
7505 if (warn_strict_aliasing
> 2)
7506 strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
7508 if (expr
!= error_mark_node
)
7509 expr
= build_reinterpret_cast_1
7510 (build_pointer_type (TREE_TYPE (type
)), expr
, c_cast_p
,
7512 if (expr
!= error_mark_node
)
7513 /* cp_build_indirect_ref isn't right for rvalue refs. */
7514 expr
= convert_from_reference (fold_convert (type
, expr
));
7518 /* As a G++ extension, we consider conversions from member
7519 functions, and pointers to member functions to
7520 pointer-to-function and pointer-to-void types. If
7521 -Wno-pmf-conversions has not been specified,
7522 convert_member_func_to_ptr will issue an error message. */
7523 if ((TYPE_PTRMEMFUNC_P (intype
)
7524 || TREE_CODE (intype
) == METHOD_TYPE
)
7525 && TYPE_PTR_P (type
)
7526 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
7527 || VOID_TYPE_P (TREE_TYPE (type
))))
7528 return convert_member_func_to_ptr (type
, expr
, complain
);
7530 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7531 array-to-pointer, and function-to-pointer conversions are
7533 expr
= decay_conversion (expr
, complain
);
7535 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7536 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7537 if (TREE_CODE (expr
) == NOP_EXPR
7538 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7539 expr
= TREE_OPERAND (expr
, 0);
7541 if (error_operand_p (expr
))
7542 return error_mark_node
;
7544 intype
= TREE_TYPE (expr
);
7546 /* [expr.reinterpret.cast]
7547 A pointer can be converted to any integral type large enough to
7548 hold it. ... A value of type std::nullptr_t can be converted to
7549 an integral type; the conversion has the same meaning and
7550 validity as a conversion of (void*)0 to the integral type. */
7551 if (CP_INTEGRAL_TYPE_P (type
)
7552 && (TYPE_PTR_P (intype
) || NULLPTR_TYPE_P (intype
)))
7554 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
7556 if (complain
& tf_error
)
7557 permerror (input_location
, "cast from %qH to %qI loses precision",
7560 return error_mark_node
;
7562 if (NULLPTR_TYPE_P (intype
))
7563 return build_int_cst (type
, 0);
7565 /* [expr.reinterpret.cast]
7566 A value of integral or enumeration type can be explicitly
7567 converted to a pointer. */
7568 else if (TYPE_PTR_P (type
) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype
))
7571 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7572 || TYPE_PTR_OR_PTRMEM_P (type
))
7573 && same_type_p (type
, intype
))
7575 return rvalue (expr
);
7576 else if (TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
7578 if ((complain
& tf_warning
)
7579 && !cxx_safe_function_type_cast_p (TREE_TYPE (type
),
7580 TREE_TYPE (intype
)))
7581 warning (OPT_Wcast_function_type
,
7582 "cast between incompatible function types"
7583 " from %qH to %qI", intype
, type
);
7584 return build_nop_reinterpret (type
, expr
);
7586 else if (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
))
7588 if ((complain
& tf_warning
)
7589 && !cxx_safe_function_type_cast_p
7590 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type
)),
7591 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype
))))
7592 warning (OPT_Wcast_function_type
,
7593 "cast between incompatible pointer to member types"
7594 " from %qH to %qI", intype
, type
);
7595 return build_nop_reinterpret (type
, expr
);
7597 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
7598 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
7601 && check_for_casting_away_constness (intype
, type
,
7602 REINTERPRET_CAST_EXPR
,
7604 return error_mark_node
;
7605 /* Warn about possible alignment problems. */
7606 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
7607 && (complain
& tf_warning
)
7608 && !VOID_TYPE_P (type
)
7609 && TREE_CODE (TREE_TYPE (intype
)) != FUNCTION_TYPE
7610 && COMPLETE_TYPE_P (TREE_TYPE (type
))
7611 && COMPLETE_TYPE_P (TREE_TYPE (intype
))
7612 && min_align_of_type (TREE_TYPE (type
))
7613 > min_align_of_type (TREE_TYPE (intype
)))
7614 warning (OPT_Wcast_align
, "cast from %qH to %qI "
7615 "increases required alignment of target type", intype
, type
);
7617 if (warn_strict_aliasing
<= 2)
7618 /* strict_aliasing_warning STRIP_NOPs its expr. */
7619 strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
7621 return build_nop_reinterpret (type
, expr
);
7623 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
7624 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
7626 if (complain
& tf_warning
)
7627 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7628 object pointer type or vice versa is conditionally-supported." */
7629 warning (OPT_Wconditionally_supported
,
7630 "casting between pointer-to-function and pointer-to-object "
7631 "is conditionally-supported");
7632 return build_nop_reinterpret (type
, expr
);
7634 else if (VECTOR_TYPE_P (type
))
7635 return convert_to_vector (type
, expr
);
7636 else if (VECTOR_TYPE_P (intype
)
7637 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
7638 return convert_to_integer_nofold (type
, expr
);
7643 if (complain
& tf_error
)
7644 error ("invalid cast from type %qT to type %qT", intype
, type
);
7645 return error_mark_node
;
7648 expr
= cp_convert (type
, expr
, complain
);
7649 if (TREE_CODE (expr
) == NOP_EXPR
)
7650 /* Mark any nop_expr that created as a reintepret_cast. */
7651 REINTERPRET_CAST_P (expr
) = true;
7656 build_reinterpret_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7660 if (type
== error_mark_node
|| expr
== error_mark_node
)
7661 return error_mark_node
;
7663 if (processing_template_decl
)
7665 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
7667 if (!TREE_SIDE_EFFECTS (t
)
7668 && type_dependent_expression_p (expr
))
7669 /* There might turn out to be side effects inside expr. */
7670 TREE_SIDE_EFFECTS (t
) = 1;
7671 return convert_from_reference (t
);
7674 r
= build_reinterpret_cast_1 (type
, expr
, /*c_cast_p=*/false,
7675 /*valid_p=*/NULL
, complain
);
7676 if (r
!= error_mark_node
)
7678 maybe_warn_about_useless_cast (type
, expr
, complain
);
7679 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7684 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7685 return an appropriate expression. Otherwise, return
7686 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7687 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7688 performing a C-style cast, its value upon return will indicate
7689 whether or not the conversion succeeded. */
7692 build_const_cast_1 (tree dst_type
, tree expr
, tsubst_flags_t complain
,
7696 tree reference_type
;
7698 /* Callers are responsible for handling error_mark_node as a
7699 destination type. */
7700 gcc_assert (dst_type
!= error_mark_node
);
7701 /* In a template, callers should be building syntactic
7702 representations of casts, not using this machinery. */
7703 gcc_assert (!processing_template_decl
);
7705 /* Assume the conversion is invalid. */
7709 if (!INDIRECT_TYPE_P (dst_type
) && !TYPE_PTRDATAMEM_P (dst_type
))
7711 if (complain
& tf_error
)
7712 error ("invalid use of const_cast with type %qT, "
7713 "which is not a pointer, "
7714 "reference, nor a pointer-to-data-member type", dst_type
);
7715 return error_mark_node
;
7718 if (TREE_CODE (TREE_TYPE (dst_type
)) == FUNCTION_TYPE
)
7720 if (complain
& tf_error
)
7721 error ("invalid use of const_cast with type %qT, which is a pointer "
7722 "or reference to a function type", dst_type
);
7723 return error_mark_node
;
7726 /* A prvalue of non-class type is cv-unqualified. */
7727 dst_type
= cv_unqualified (dst_type
);
7729 /* Save casted types in the function's used types hash table. */
7730 used_types_insert (dst_type
);
7732 src_type
= TREE_TYPE (expr
);
7733 /* Expressions do not really have reference types. */
7734 if (TYPE_REF_P (src_type
))
7735 src_type
= TREE_TYPE (src_type
);
7737 /* [expr.const.cast]
7739 For two object types T1 and T2, if a pointer to T1 can be explicitly
7740 converted to the type "pointer to T2" using a const_cast, then the
7741 following conversions can also be made:
7743 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7744 type T2 using the cast const_cast<T2&>;
7746 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7747 type T2 using the cast const_cast<T2&&>; and
7749 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7750 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7752 if (TYPE_REF_P (dst_type
))
7754 reference_type
= dst_type
;
7755 if (!TYPE_REF_IS_RVALUE (dst_type
)
7761 if (complain
& tf_error
)
7762 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7763 src_type
, dst_type
);
7764 return error_mark_node
;
7766 dst_type
= build_pointer_type (TREE_TYPE (dst_type
));
7767 src_type
= build_pointer_type (src_type
);
7771 reference_type
= NULL_TREE
;
7772 /* If the destination type is not a reference type, the
7773 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7774 conversions are performed. */
7775 src_type
= type_decays_to (src_type
);
7776 if (src_type
== error_mark_node
)
7777 return error_mark_node
;
7780 if (TYPE_PTR_P (src_type
) || TYPE_PTRDATAMEM_P (src_type
))
7782 if (comp_ptr_ttypes_const (dst_type
, src_type
))
7787 /* This cast is actually a C-style cast. Issue a warning if
7788 the user is making a potentially unsafe cast. */
7789 check_for_casting_away_constness (src_type
, dst_type
,
7790 CAST_EXPR
, complain
);
7791 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
7792 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
7793 && (complain
& tf_warning
)
7794 && min_align_of_type (TREE_TYPE (dst_type
))
7795 > min_align_of_type (TREE_TYPE (src_type
)))
7796 warning (OPT_Wcast_align
, "cast from %qH to %qI "
7797 "increases required alignment of target type",
7798 src_type
, dst_type
);
7802 expr
= cp_build_addr_expr (expr
, complain
);
7803 if (expr
== error_mark_node
)
7804 return error_mark_node
;
7805 expr
= build_nop (reference_type
, expr
);
7806 return convert_from_reference (expr
);
7810 expr
= decay_conversion (expr
, complain
);
7811 if (expr
== error_mark_node
)
7812 return error_mark_node
;
7814 /* build_c_cast puts on a NOP_EXPR to make the result not an
7815 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7816 non-lvalue context. */
7817 if (TREE_CODE (expr
) == NOP_EXPR
7818 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7819 expr
= TREE_OPERAND (expr
, 0);
7820 return build_nop (dst_type
, expr
);
7824 && !at_least_as_qualified_p (TREE_TYPE (dst_type
),
7825 TREE_TYPE (src_type
)))
7826 check_for_casting_away_constness (src_type
, dst_type
, CAST_EXPR
,
7830 if (complain
& tf_error
)
7831 error ("invalid const_cast from type %qT to type %qT",
7832 src_type
, dst_type
);
7833 return error_mark_node
;
7837 build_const_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7841 if (type
== error_mark_node
|| error_operand_p (expr
))
7842 return error_mark_node
;
7844 if (processing_template_decl
)
7846 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
7848 if (!TREE_SIDE_EFFECTS (t
)
7849 && type_dependent_expression_p (expr
))
7850 /* There might turn out to be side effects inside expr. */
7851 TREE_SIDE_EFFECTS (t
) = 1;
7852 return convert_from_reference (t
);
7855 r
= build_const_cast_1 (type
, expr
, complain
, /*valid_p=*/NULL
);
7856 if (r
!= error_mark_node
)
7858 maybe_warn_about_useless_cast (type
, expr
, complain
);
7859 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7864 /* Like cp_build_c_cast, but for the c-common bits. */
7867 build_c_cast (location_t
/*loc*/, tree type
, tree expr
)
7869 return cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7872 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7873 preserve location information even for tree nodes that don't
7877 build_c_cast (location_t loc
, tree type
, cp_expr expr
)
7879 cp_expr result
= cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7880 result
.set_location (loc
);
7884 /* Build an expression representing an explicit C-style cast to type
7885 TYPE of expression EXPR. */
7888 cp_build_c_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7894 if (type
== error_mark_node
|| error_operand_p (expr
))
7895 return error_mark_node
;
7897 if (processing_template_decl
)
7899 tree t
= build_min (CAST_EXPR
, type
,
7900 tree_cons (NULL_TREE
, value
, NULL_TREE
));
7901 /* We don't know if it will or will not have side effects. */
7902 TREE_SIDE_EFFECTS (t
) = 1;
7903 return convert_from_reference (t
);
7906 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7907 'Class') should always be retained, because this information aids
7908 in method lookup. */
7909 if (objc_is_object_ptr (type
)
7910 && objc_is_object_ptr (TREE_TYPE (expr
)))
7911 return build_nop (type
, expr
);
7913 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7914 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7915 if (!TYPE_REF_P (type
)
7916 && TREE_CODE (value
) == NOP_EXPR
7917 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
7918 value
= TREE_OPERAND (value
, 0);
7920 if (TREE_CODE (type
) == ARRAY_TYPE
)
7922 /* Allow casting from T1* to T2[] because Cfront allows it.
7923 NIHCL uses it. It is not valid ISO C++ however. */
7924 if (TYPE_PTR_P (TREE_TYPE (expr
)))
7926 if (complain
& tf_error
)
7927 permerror (input_location
, "ISO C++ forbids casting to an array type %qT", type
);
7929 return error_mark_node
;
7930 type
= build_pointer_type (TREE_TYPE (type
));
7934 if (complain
& tf_error
)
7935 error ("ISO C++ forbids casting to an array type %qT", type
);
7936 return error_mark_node
;
7940 if (TREE_CODE (type
) == FUNCTION_TYPE
7941 || TREE_CODE (type
) == METHOD_TYPE
)
7943 if (complain
& tf_error
)
7944 error ("invalid cast to function type %qT", type
);
7945 return error_mark_node
;
7948 if (TYPE_PTR_P (type
)
7949 && TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
7950 /* Casting to an integer of smaller size is an error detected elsewhere. */
7951 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (value
))
7952 /* Don't warn about converting any constant. */
7953 && !TREE_CONSTANT (value
))
7954 warning_at (input_location
, OPT_Wint_to_pointer_cast
,
7955 "cast to pointer from integer of different size");
7957 /* A C-style cast can be a const_cast. */
7958 result
= build_const_cast_1 (type
, value
, complain
& tf_warning
,
7962 if (result
!= error_mark_node
)
7964 maybe_warn_about_useless_cast (type
, value
, complain
);
7965 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7970 /* Or a static cast. */
7971 result
= build_static_cast_1 (type
, value
, /*c_cast_p=*/true,
7972 &valid_p
, complain
);
7973 /* Or a reinterpret_cast. */
7975 result
= build_reinterpret_cast_1 (type
, value
, /*c_cast_p=*/true,
7976 &valid_p
, complain
);
7977 /* The static_cast or reinterpret_cast may be followed by a
7980 /* A valid cast may result in errors if, for example, a
7981 conversion to an ambiguous base class is required. */
7982 && !error_operand_p (result
))
7986 maybe_warn_about_useless_cast (type
, value
, complain
);
7987 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7989 /* Non-class rvalues always have cv-unqualified type. */
7990 if (!CLASS_TYPE_P (type
))
7991 type
= TYPE_MAIN_VARIANT (type
);
7992 result_type
= TREE_TYPE (result
);
7993 if (!CLASS_TYPE_P (result_type
) && !TYPE_REF_P (type
))
7994 result_type
= TYPE_MAIN_VARIANT (result_type
);
7995 /* If the type of RESULT does not match TYPE, perform a
7996 const_cast to make it match. If the static_cast or
7997 reinterpret_cast succeeded, we will differ by at most
7998 cv-qualification, so the follow-on const_cast is guaranteed
8000 if (!same_type_p (non_reference (type
), non_reference (result_type
)))
8002 result
= build_const_cast_1 (type
, result
, false, &valid_p
);
8003 gcc_assert (valid_p
);
8008 return error_mark_node
;
8011 /* For use from the C common bits. */
8013 build_modify_expr (location_t location
,
8014 tree lhs
, tree
/*lhs_origtype*/,
8015 enum tree_code modifycode
,
8016 location_t
/*rhs_location*/, tree rhs
,
8017 tree
/*rhs_origtype*/)
8019 return cp_build_modify_expr (location
, lhs
, modifycode
, rhs
,
8020 tf_warning_or_error
);
8023 /* Build an assignment expression of lvalue LHS from value RHS.
8024 MODIFYCODE is the code for a binary operator that we use
8025 to combine the old value of LHS with RHS to get the new value.
8026 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
8028 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
8031 cp_build_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
8032 tree rhs
, tsubst_flags_t complain
)
8034 lhs
= mark_lvalue_use_nonread (lhs
);
8036 tree result
= NULL_TREE
;
8038 tree lhstype
= TREE_TYPE (lhs
);
8040 tree olhstype
= lhstype
;
8041 bool plain_assign
= (modifycode
== NOP_EXPR
);
8042 bool compound_side_effects_p
= false;
8043 tree preeval
= NULL_TREE
;
8045 /* Avoid duplicate error messages from operands that had errors. */
8046 if (error_operand_p (lhs
) || error_operand_p (rhs
))
8047 return error_mark_node
;
8049 while (TREE_CODE (lhs
) == COMPOUND_EXPR
)
8051 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
8052 compound_side_effects_p
= true;
8053 lhs
= TREE_OPERAND (lhs
, 1);
8056 /* Handle control structure constructs used as "lvalues". Note that we
8057 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
8058 switch (TREE_CODE (lhs
))
8060 /* Handle --foo = 5; as these are valid constructs in C++. */
8061 case PREDECREMENT_EXPR
:
8062 case PREINCREMENT_EXPR
:
8063 if (compound_side_effects_p
)
8064 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
8065 lhs
= genericize_compound_lvalue (lhs
);
8067 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
8068 and looked through the COMPOUND_EXPRs, readd them now around
8069 the resulting lhs. */
8070 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
8072 lhs
= build2 (COMPOUND_EXPR
, lhstype
, TREE_OPERAND (olhs
, 0), lhs
);
8073 tree
*ptr
= &TREE_OPERAND (lhs
, 1);
8074 for (olhs
= TREE_OPERAND (olhs
, 1);
8075 TREE_CODE (olhs
) == COMPOUND_EXPR
;
8076 olhs
= TREE_OPERAND (olhs
, 1))
8078 *ptr
= build2 (COMPOUND_EXPR
, lhstype
,
8079 TREE_OPERAND (olhs
, 0), *ptr
);
8080 ptr
= &TREE_OPERAND (*ptr
, 1);
8086 if (compound_side_effects_p
)
8087 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
8088 lhs
= genericize_compound_lvalue (lhs
);
8089 goto maybe_add_compound
;
8093 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
8094 when neither operand has side-effects. */
8095 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8096 return error_mark_node
;
8098 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0))
8099 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 1)));
8101 lhs
= build3 (COND_EXPR
, TREE_TYPE (lhs
),
8102 build2 (TREE_CODE (lhs
) == MIN_EXPR
? LE_EXPR
: GE_EXPR
,
8104 TREE_OPERAND (lhs
, 0),
8105 TREE_OPERAND (lhs
, 1)),
8106 TREE_OPERAND (lhs
, 0),
8107 TREE_OPERAND (lhs
, 1));
8110 /* Handle (a ? b : c) used as an "lvalue". */
8113 /* Produce (a ? (b = rhs) : (c = rhs))
8114 except that the RHS goes through a save-expr
8115 so the code to compute it is only emitted once. */
8118 if (VOID_TYPE_P (TREE_TYPE (rhs
)))
8120 if (complain
& tf_error
)
8121 error ("void value not ignored as it ought to be");
8122 return error_mark_node
;
8125 rhs
= stabilize_expr (rhs
, &preeval
);
8127 /* Check this here to avoid odd errors when trying to convert
8128 a throw to the type of the COND_EXPR. */
8129 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8130 return error_mark_node
;
8132 cond
= build_conditional_expr
8133 (input_location
, TREE_OPERAND (lhs
, 0),
8134 cp_build_modify_expr (loc
, TREE_OPERAND (lhs
, 1),
8135 modifycode
, rhs
, complain
),
8136 cp_build_modify_expr (loc
, TREE_OPERAND (lhs
, 2),
8137 modifycode
, rhs
, complain
),
8140 if (cond
== error_mark_node
)
8142 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
8143 and looked through the COMPOUND_EXPRs, readd them now around
8144 the resulting cond before adding the preevaluated rhs. */
8145 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
8147 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
8148 TREE_OPERAND (olhs
, 0), cond
);
8149 tree
*ptr
= &TREE_OPERAND (cond
, 1);
8150 for (olhs
= TREE_OPERAND (olhs
, 1);
8151 TREE_CODE (olhs
) == COMPOUND_EXPR
;
8152 olhs
= TREE_OPERAND (olhs
, 1))
8154 *ptr
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
8155 TREE_OPERAND (olhs
, 0), *ptr
);
8156 ptr
= &TREE_OPERAND (*ptr
, 1);
8159 /* Make sure the code to compute the rhs comes out
8160 before the split. */
8170 if (modifycode
== INIT_EXPR
)
8172 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
8173 /* Do the default thing. */;
8174 else if (TREE_CODE (rhs
) == CONSTRUCTOR
)
8176 /* Compound literal. */
8177 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
8178 /* Call convert to generate an error; see PR 11063. */
8179 rhs
= convert (lhstype
, rhs
);
8180 result
= build2 (INIT_EXPR
, lhstype
, lhs
, rhs
);
8181 TREE_SIDE_EFFECTS (result
) = 1;
8184 else if (! MAYBE_CLASS_TYPE_P (lhstype
))
8185 /* Do the default thing. */;
8188 vec
<tree
, va_gc
> *rhs_vec
= make_tree_vector_single (rhs
);
8189 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
8190 &rhs_vec
, lhstype
, LOOKUP_NORMAL
,
8192 release_tree_vector (rhs_vec
);
8193 if (result
== NULL_TREE
)
8194 return error_mark_node
;
8200 lhs
= require_complete_type_sfinae (lhs
, complain
);
8201 if (lhs
== error_mark_node
)
8202 return error_mark_node
;
8204 if (modifycode
== NOP_EXPR
)
8206 if (c_dialect_objc ())
8208 result
= objc_maybe_build_modify_expr (lhs
, rhs
);
8213 /* `operator=' is not an inheritable operator. */
8214 if (! MAYBE_CLASS_TYPE_P (lhstype
))
8215 /* Do the default thing. */;
8218 result
= build_new_op (input_location
, MODIFY_EXPR
,
8219 LOOKUP_NORMAL
, lhs
, rhs
,
8220 make_node (NOP_EXPR
), /*overload=*/NULL
,
8222 if (result
== NULL_TREE
)
8223 return error_mark_node
;
8230 tree init
= NULL_TREE
;
8232 /* A binary op has been requested. Combine the old LHS
8233 value with the RHS producing the value we should actually
8234 store into the LHS. */
8235 gcc_assert (!((TYPE_REF_P (lhstype
)
8236 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype
)))
8237 || MAYBE_CLASS_TYPE_P (lhstype
)));
8239 /* Preevaluate the RHS to make sure its evaluation is complete
8240 before the lvalue-to-rvalue conversion of the LHS:
8242 [expr.ass] With respect to an indeterminately-sequenced
8243 function call, the operation of a compound assignment is a
8244 single evaluation. [ Note: Therefore, a function call shall
8245 not intervene between the lvalue-to-rvalue conversion and the
8246 side effect associated with any single compound assignment
8247 operator. -- end note ] */
8248 lhs
= cp_stabilize_reference (lhs
);
8249 rhs
= decay_conversion (rhs
, complain
);
8250 if (rhs
== error_mark_node
)
8251 return error_mark_node
;
8252 rhs
= stabilize_expr (rhs
, &init
);
8253 newrhs
= cp_build_binary_op (loc
, modifycode
, lhs
, rhs
, complain
);
8254 if (newrhs
== error_mark_node
)
8256 if (complain
& tf_error
)
8257 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode
,
8258 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
8259 return error_mark_node
;
8263 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), init
, newrhs
);
8265 /* Now it looks like a plain assignment. */
8266 modifycode
= NOP_EXPR
;
8267 if (c_dialect_objc ())
8269 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
8274 gcc_assert (!TYPE_REF_P (lhstype
));
8275 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs
)));
8278 /* The left-hand side must be an lvalue. */
8279 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8280 return error_mark_node
;
8282 /* Warn about modifying something that is `const'. Don't warn if
8283 this is initialization. */
8284 if (modifycode
!= INIT_EXPR
8285 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
8286 /* Functions are not modifiable, even though they are
8288 || TREE_CODE (TREE_TYPE (lhs
)) == FUNCTION_TYPE
8289 || TREE_CODE (TREE_TYPE (lhs
)) == METHOD_TYPE
8290 /* If it's an aggregate and any field is const, then it is
8291 effectively const. */
8292 || (CLASS_TYPE_P (lhstype
)
8293 && C_TYPE_FIELDS_READONLY (lhstype
))))
8295 if (complain
& tf_error
)
8296 cxx_readonly_error (loc
, lhs
, lv_assign
);
8297 return error_mark_node
;
8300 /* If storing into a structure or union member, it may have been given a
8301 lowered bitfield type. We need to convert to the declared type first,
8302 so retrieve it now. */
8304 olhstype
= unlowered_expr_type (lhs
);
8306 /* Convert new value to destination type. */
8308 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
8312 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs
))
8314 if (modifycode
!= INIT_EXPR
)
8316 if (complain
& tf_error
)
8317 error ("assigning to an array from an initializer list");
8318 return error_mark_node
;
8320 if (check_array_initializer (lhs
, lhstype
, newrhs
))
8321 return error_mark_node
;
8322 newrhs
= digest_init (lhstype
, newrhs
, complain
);
8323 if (newrhs
== error_mark_node
)
8324 return error_mark_node
;
8327 /* C++11 8.5/17: "If the destination type is an array of characters,
8328 an array of char16_t, an array of char32_t, or an array of wchar_t,
8329 and the initializer is a string literal...". */
8330 else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs
))
8332 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype
)))
8333 && modifycode
== INIT_EXPR
)
8335 newrhs
= digest_init (lhstype
, newrhs
, complain
);
8336 if (newrhs
== error_mark_node
)
8337 return error_mark_node
;
8340 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
8341 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))))
8343 if (complain
& tf_error
)
8344 error ("incompatible types in assignment of %qT to %qT",
8345 TREE_TYPE (rhs
), lhstype
);
8346 return error_mark_node
;
8349 /* Allow array assignment in compiler-generated code. */
8350 else if (!current_function_decl
8351 || !DECL_DEFAULTED_FN (current_function_decl
))
8353 /* This routine is used for both initialization and assignment.
8354 Make sure the diagnostic message differentiates the context. */
8355 if (complain
& tf_error
)
8357 if (modifycode
== INIT_EXPR
)
8358 error ("array used as initializer");
8360 error ("invalid array assignment");
8362 return error_mark_node
;
8365 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
8366 ? 1 + (modifycode
!= INIT_EXPR
): 0;
8367 result
= build_vec_init (lhs
, NULL_TREE
, newrhs
,
8368 /*explicit_value_init_p=*/false,
8369 from_array
, complain
);
8373 if (modifycode
== INIT_EXPR
)
8374 /* Calls with INIT_EXPR are all direct-initialization, so don't set
8375 LOOKUP_ONLYCONVERTING. */
8376 newrhs
= convert_for_initialization (lhs
, olhstype
, newrhs
, LOOKUP_NORMAL
,
8377 ICR_INIT
, NULL_TREE
, 0,
8380 newrhs
= convert_for_assignment (olhstype
, newrhs
, ICR_ASSIGN
,
8381 NULL_TREE
, 0, complain
, LOOKUP_IMPLICIT
);
8383 if (!same_type_p (lhstype
, olhstype
))
8384 newrhs
= cp_convert_and_check (lhstype
, newrhs
, complain
);
8386 if (modifycode
!= INIT_EXPR
)
8388 if (TREE_CODE (newrhs
) == CALL_EXPR
8389 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
8390 newrhs
= build_cplus_new (lhstype
, newrhs
, complain
);
8392 /* Can't initialize directly from a TARGET_EXPR, since that would
8393 cause the lhs to be constructed twice, and possibly result in
8394 accidental self-initialization. So we force the TARGET_EXPR to be
8395 expanded without a target. */
8396 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
8397 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
8398 TREE_OPERAND (newrhs
, 0));
8401 if (newrhs
== error_mark_node
)
8402 return error_mark_node
;
8404 if (c_dialect_objc () && flag_objc_gc
)
8406 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
8412 result
= build2 (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
8413 lhstype
, lhs
, newrhs
);
8415 TREE_SIDE_EFFECTS (result
) = 1;
8417 TREE_NO_WARNING (result
) = 1;
8421 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), preeval
, result
);
8426 build_x_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
8427 tree rhs
, tsubst_flags_t complain
)
8429 tree orig_lhs
= lhs
;
8430 tree orig_rhs
= rhs
;
8431 tree overload
= NULL_TREE
;
8432 tree op
= build_nt (modifycode
, NULL_TREE
, NULL_TREE
);
8434 if (processing_template_decl
)
8436 if (modifycode
== NOP_EXPR
8437 || type_dependent_expression_p (lhs
)
8438 || type_dependent_expression_p (rhs
))
8439 return build_min_nt_loc (loc
, MODOP_EXPR
, lhs
,
8440 build_min_nt_loc (loc
, modifycode
, NULL_TREE
,
8443 lhs
= build_non_dependent_expr (lhs
);
8444 rhs
= build_non_dependent_expr (rhs
);
8447 if (modifycode
!= NOP_EXPR
)
8449 tree rval
= build_new_op (loc
, MODIFY_EXPR
, LOOKUP_NORMAL
,
8450 lhs
, rhs
, op
, &overload
, complain
);
8453 if (rval
== error_mark_node
)
8455 TREE_NO_WARNING (rval
) = 1;
8456 if (processing_template_decl
)
8458 if (overload
!= NULL_TREE
)
8459 return (build_min_non_dep_op_overload
8460 (MODIFY_EXPR
, rval
, overload
, orig_lhs
, orig_rhs
));
8462 return (build_min_non_dep
8463 (MODOP_EXPR
, rval
, orig_lhs
, op
, orig_rhs
));
8468 return cp_build_modify_expr (loc
, lhs
, modifycode
, rhs
, complain
);
8471 /* Helper function for get_delta_difference which assumes FROM is a base
8472 class of TO. Returns a delta for the conversion of pointer-to-member
8473 of FROM to pointer-to-member of TO. If the conversion is invalid and
8474 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8475 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8476 If C_CAST_P is true, this conversion is taking place as part of a
8480 get_delta_difference_1 (tree from
, tree to
, bool c_cast_p
,
8481 tsubst_flags_t complain
)
8486 binfo
= lookup_base (to
, from
, c_cast_p
? ba_unique
: ba_check
,
8489 if (binfo
== error_mark_node
)
8491 if (!(complain
& tf_error
))
8492 return error_mark_node
;
8494 error (" in pointer to member function conversion");
8495 return size_zero_node
;
8499 if (kind
!= bk_via_virtual
)
8500 return BINFO_OFFSET (binfo
);
8502 /* FROM is a virtual base class of TO. Issue an error or warning
8503 depending on whether or not this is a reinterpret cast. */
8505 if (!(complain
& tf_error
))
8506 return error_mark_node
;
8508 error ("pointer to member conversion via virtual base %qT",
8509 BINFO_TYPE (binfo_from_vbase (binfo
)));
8511 return size_zero_node
;
8518 /* Get difference in deltas for different pointer to member function
8519 types. If the conversion is invalid and tf_error is not set in
8520 COMPLAIN, returns error_mark_node, otherwise returns an integer
8521 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8522 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8523 conversions as well. If C_CAST_P is true this conversion is taking
8524 place as part of a C-style cast.
8526 Note that the naming of FROM and TO is kind of backwards; the return
8527 value is what we add to a TO in order to get a FROM. They are named
8528 this way because we call this function to find out how to convert from
8529 a pointer to member of FROM to a pointer to member of TO. */
8532 get_delta_difference (tree from
, tree to
,
8533 bool allow_inverse_p
,
8534 bool c_cast_p
, tsubst_flags_t complain
)
8538 if (same_type_ignoring_top_level_qualifiers_p (from
, to
))
8539 /* Pointer to member of incomplete class is permitted*/
8540 result
= size_zero_node
;
8542 result
= get_delta_difference_1 (from
, to
, c_cast_p
, complain
);
8544 if (result
== error_mark_node
)
8545 return error_mark_node
;
8549 if (!allow_inverse_p
)
8551 if (!(complain
& tf_error
))
8552 return error_mark_node
;
8554 error_not_base_type (from
, to
);
8555 error (" in pointer to member conversion");
8556 result
= size_zero_node
;
8560 result
= get_delta_difference_1 (to
, from
, c_cast_p
, complain
);
8562 if (result
== error_mark_node
)
8563 return error_mark_node
;
8566 result
= size_diffop_loc (input_location
,
8567 size_zero_node
, result
);
8570 if (!(complain
& tf_error
))
8571 return error_mark_node
;
8573 error_not_base_type (from
, to
);
8574 error (" in pointer to member conversion");
8575 result
= size_zero_node
;
8580 return convert_to_integer (ptrdiff_type_node
, result
);
8583 /* Return a constructor for the pointer-to-member-function TYPE using
8584 the other components as specified. */
8587 build_ptrmemfunc1 (tree type
, tree delta
, tree pfn
)
8592 vec
<constructor_elt
, va_gc
> *v
;
8594 /* Pull the FIELD_DECLs out of the type. */
8595 pfn_field
= TYPE_FIELDS (type
);
8596 delta_field
= DECL_CHAIN (pfn_field
);
8598 /* Make sure DELTA has the type we want. */
8599 delta
= convert_and_check (input_location
, delta_type_node
, delta
);
8601 /* Convert to the correct target type if necessary. */
8602 pfn
= fold_convert (TREE_TYPE (pfn_field
), pfn
);
8604 /* Finish creating the initializer. */
8606 CONSTRUCTOR_APPEND_ELT(v
, pfn_field
, pfn
);
8607 CONSTRUCTOR_APPEND_ELT(v
, delta_field
, delta
);
8608 u
= build_constructor (type
, v
);
8609 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) & TREE_CONSTANT (delta
);
8610 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
8611 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
8613 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
8618 /* Build a constructor for a pointer to member function. It can be
8619 used to initialize global variables, local variable, or used
8620 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8623 If FORCE is nonzero, then force this conversion, even if
8624 we would rather not do it. Usually set when using an explicit
8625 cast. A C-style cast is being processed iff C_CAST_P is true.
8627 Return error_mark_node, if something goes wrong. */
8630 build_ptrmemfunc (tree type
, tree pfn
, int force
, bool c_cast_p
,
8631 tsubst_flags_t complain
)
8637 if (error_operand_p (pfn
))
8638 return error_mark_node
;
8640 pfn_type
= TREE_TYPE (pfn
);
8641 to_type
= build_ptrmemfunc_type (type
);
8643 /* Handle multiple conversions of pointer to member functions. */
8644 if (TYPE_PTRMEMFUNC_P (pfn_type
))
8646 tree delta
= NULL_TREE
;
8647 tree npfn
= NULL_TREE
;
8651 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
,
8652 LOOKUP_NORMAL
, complain
))
8654 if (complain
& tf_error
)
8655 error ("invalid conversion to type %qT from type %qT",
8658 return error_mark_node
;
8661 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
8662 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
8664 c_cast_p
, complain
);
8665 if (n
== error_mark_node
)
8666 return error_mark_node
;
8668 /* We don't have to do any conversion to convert a
8669 pointer-to-member to its own type. But, we don't want to
8670 just return a PTRMEM_CST if there's an explicit cast; that
8671 cast should make the expression an invalid template argument. */
8672 if (TREE_CODE (pfn
) != PTRMEM_CST
)
8674 if (same_type_p (to_type
, pfn_type
))
8676 else if (integer_zerop (n
) && TREE_CODE (pfn
) != CONSTRUCTOR
)
8677 return build_reinterpret_cast (to_type
, pfn
,
8681 if (TREE_SIDE_EFFECTS (pfn
))
8682 pfn
= save_expr (pfn
);
8684 /* Obtain the function pointer and the current DELTA. */
8685 if (TREE_CODE (pfn
) == PTRMEM_CST
)
8686 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
8689 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
8690 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
8693 /* Just adjust the DELTA field. */
8694 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8695 (TREE_TYPE (delta
), ptrdiff_type_node
));
8696 if (!integer_zerop (n
))
8698 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
8699 n
= cp_build_binary_op (input_location
,
8700 LSHIFT_EXPR
, n
, integer_one_node
,
8702 delta
= cp_build_binary_op (input_location
,
8703 PLUS_EXPR
, delta
, n
, complain
);
8705 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
8708 /* Handle null pointer to member function conversions. */
8709 if (null_ptr_cst_p (pfn
))
8711 pfn
= cp_build_c_cast (type
, pfn
, complain
);
8712 return build_ptrmemfunc1 (to_type
,
8717 if (type_unknown_p (pfn
))
8718 return instantiate_type (type
, pfn
, complain
);
8720 fn
= TREE_OPERAND (pfn
, 0);
8721 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
8722 /* In a template, we will have preserved the
8724 || (processing_template_decl
&& TREE_CODE (fn
) == OFFSET_REF
));
8725 return make_ptrmem_cst (to_type
, fn
);
8728 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8731 ??? There is no consistency as to the types returned for the above
8732 values. Some code acts as if it were a sizetype and some as if it were
8733 integer_type_node. */
8736 expand_ptrmemfunc_cst (tree cst
, tree
*delta
, tree
*pfn
)
8738 tree type
= TREE_TYPE (cst
);
8739 tree fn
= PTRMEM_CST_MEMBER (cst
);
8740 tree ptr_class
, fn_class
;
8742 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
8744 /* The class that the function belongs to. */
8745 fn_class
= DECL_CONTEXT (fn
);
8747 /* The class that we're creating a pointer to member of. */
8748 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
8750 /* First, calculate the adjustment to the function's class. */
8751 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0,
8752 /*c_cast_p=*/0, tf_warning_or_error
);
8754 if (!DECL_VIRTUAL_P (fn
))
8755 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
),
8756 build_addr_func (fn
, tf_warning_or_error
));
8759 /* If we're dealing with a virtual function, we have to adjust 'this'
8760 again, to point to the base which provides the vtable entry for
8761 fn; the call will do the opposite adjustment. */
8762 tree orig_class
= DECL_CONTEXT (fn
);
8763 tree binfo
= binfo_or_else (orig_class
, fn_class
);
8764 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
8765 *delta
, BINFO_OFFSET (binfo
));
8767 /* We set PFN to the vtable offset at which the function can be
8768 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8769 case delta is shifted left, and then incremented). */
8770 *pfn
= DECL_VINDEX (fn
);
8771 *pfn
= fold_build2 (MULT_EXPR
, integer_type_node
, *pfn
,
8772 TYPE_SIZE_UNIT (vtable_entry_type
));
8774 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
8776 case ptrmemfunc_vbit_in_pfn
:
8777 *pfn
= fold_build2 (PLUS_EXPR
, integer_type_node
, *pfn
,
8781 case ptrmemfunc_vbit_in_delta
:
8782 *delta
= fold_build2 (LSHIFT_EXPR
, TREE_TYPE (*delta
),
8783 *delta
, integer_one_node
);
8784 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
8785 *delta
, integer_one_node
);
8792 *pfn
= fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), *pfn
);
8796 /* Return an expression for PFN from the pointer-to-member function
8800 pfn_from_ptrmemfunc (tree t
)
8802 if (TREE_CODE (t
) == PTRMEM_CST
)
8807 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
8812 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
8815 /* Return an expression for DELTA from the pointer-to-member function
8819 delta_from_ptrmemfunc (tree t
)
8821 if (TREE_CODE (t
) == PTRMEM_CST
)
8826 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
8831 return build_ptrmemfunc_access_expr (t
, delta_identifier
);
8834 /* Convert value RHS to type TYPE as preparation for an assignment to
8835 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8836 implicit conversion is. If FNDECL is non-NULL, we are doing the
8837 conversion in order to pass the PARMNUMth argument of FNDECL.
8838 If FNDECL is NULL, we are doing the conversion in function pointer
8839 argument passing, conversion in initialization, etc. */
8842 convert_for_assignment (tree type
, tree rhs
,
8843 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
8844 tsubst_flags_t complain
, int flags
)
8847 enum tree_code coder
;
8849 location_t rhs_loc
= EXPR_LOC_OR_LOC (rhs
, input_location
);
8850 bool has_loc
= EXPR_LOCATION (rhs
) != UNKNOWN_LOCATION
;
8851 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
8852 but preserve location wrappers. */
8853 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
8854 && !location_wrapper_p (rhs
))
8855 rhs
= TREE_OPERAND (rhs
, 0);
8857 /* Handle [dcl.init.list] direct-list-initialization from
8858 single element of enumeration with a fixed underlying type. */
8859 if (is_direct_enum_init (type
, rhs
))
8861 tree elt
= CONSTRUCTOR_ELT (rhs
, 0)->value
;
8862 if (check_narrowing (ENUM_UNDERLYING_TYPE (type
), elt
, complain
))
8864 warning_sentinel
w (warn_useless_cast
);
8865 warning_sentinel
w2 (warn_ignored_qualifiers
);
8866 rhs
= cp_build_c_cast (type
, elt
, complain
);
8869 rhs
= error_mark_node
;
8872 rhstype
= TREE_TYPE (rhs
);
8873 coder
= TREE_CODE (rhstype
);
8875 if (VECTOR_TYPE_P (type
) && coder
== VECTOR_TYPE
8876 && vector_types_convertible_p (type
, rhstype
, true))
8878 rhs
= mark_rvalue_use (rhs
);
8879 return convert (type
, rhs
);
8882 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
8883 return error_mark_node
;
8884 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
8885 return error_mark_node
;
8887 /* The RHS of an assignment cannot have void type. */
8888 if (coder
== VOID_TYPE
)
8890 if (complain
& tf_error
)
8891 error_at (rhs_loc
, "void value not ignored as it ought to be");
8892 return error_mark_node
;
8895 if (c_dialect_objc ())
8899 tree rname
= fndecl
;
8910 selector
= objc_message_selector ();
8912 if (selector
&& parmno
> 1)
8919 if (objc_compare_types (type
, rhstype
, parmno
, rname
))
8921 rhs
= mark_rvalue_use (rhs
);
8922 return convert (type
, rhs
);
8928 The expression is implicitly converted (clause _conv_) to the
8929 cv-unqualified type of the left operand.
8931 We allow bad conversions here because by the time we get to this point
8932 we are committed to doing the conversion. If we end up doing a bad
8933 conversion, convert_like will complain. */
8934 if (!can_convert_arg_bad (type
, rhstype
, rhs
, flags
, complain
))
8936 /* When -Wno-pmf-conversions is use, we just silently allow
8937 conversions from pointers-to-members to plain pointers. If
8938 the conversion doesn't work, cp_convert will complain. */
8940 && TYPE_PTR_P (type
)
8941 && TYPE_PTRMEMFUNC_P (rhstype
))
8942 rhs
= cp_convert (strip_top_quals (type
), rhs
, complain
);
8945 if (complain
& tf_error
)
8947 /* If the right-hand side has unknown type, then it is an
8948 overloaded function. Call instantiate_type to get error
8950 if (rhstype
== unknown_type_node
)
8952 tree r
= instantiate_type (type
, rhs
, tf_warning_or_error
);
8953 /* -fpermissive might allow this; recurse. */
8955 return convert_for_assignment (type
, r
, errtype
, fndecl
,
8956 parmnum
, complain
, flags
);
8959 complain_about_bad_argument (rhs_loc
,
8964 range_label_for_type_mismatch
label (rhstype
, type
);
8965 gcc_rich_location
richloc (rhs_loc
, has_loc
? &label
: NULL
);
8968 case ICR_DEFAULT_ARGUMENT
:
8970 "cannot convert %qH to %qI in default argument",
8975 "cannot convert %qH to %qI in argument passing",
8978 case ICR_CONVERTING
:
8979 error_at (&richloc
, "cannot convert %qH to %qI",
8984 "cannot convert %qH to %qI in initialization",
8988 error_at (&richloc
, "cannot convert %qH to %qI in return",
8993 "cannot convert %qH to %qI in assignment",
9000 if (TYPE_PTR_P (rhstype
)
9001 && TYPE_PTR_P (type
)
9002 && CLASS_TYPE_P (TREE_TYPE (rhstype
))
9003 && CLASS_TYPE_P (TREE_TYPE (type
))
9004 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype
)))
9005 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
9006 (TREE_TYPE (rhstype
))),
9007 "class type %qT is incomplete", TREE_TYPE (rhstype
));
9009 return error_mark_node
;
9012 if (warn_suggest_attribute_format
)
9014 const enum tree_code codel
= TREE_CODE (type
);
9015 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
9017 && check_missing_format_attribute (type
, rhstype
)
9018 && (complain
& tf_warning
))
9022 case ICR_DEFAULT_ARGUMENT
:
9024 warning (OPT_Wsuggest_attribute_format
,
9025 "parameter %qP of %qD might be a candidate "
9026 "for a format attribute", parmnum
, fndecl
);
9028 warning (OPT_Wsuggest_attribute_format
,
9029 "parameter might be a candidate "
9030 "for a format attribute");
9032 case ICR_CONVERTING
:
9033 warning (OPT_Wsuggest_attribute_format
,
9034 "target of conversion might be a candidate "
9035 "for a format attribute");
9038 warning (OPT_Wsuggest_attribute_format
,
9039 "target of initialization might be a candidate "
9040 "for a format attribute");
9043 warning (OPT_Wsuggest_attribute_format
,
9044 "return type might be a candidate "
9045 "for a format attribute");
9048 warning (OPT_Wsuggest_attribute_format
,
9049 "left-hand side of assignment might be a candidate "
9050 "for a format attribute");
9057 /* If -Wparentheses, warn about a = b = c when a has type bool and b
9059 if (warn_parentheses
9060 && TREE_CODE (type
) == BOOLEAN_TYPE
9061 && TREE_CODE (rhs
) == MODIFY_EXPR
9062 && !TREE_NO_WARNING (rhs
)
9063 && TREE_CODE (TREE_TYPE (rhs
)) != BOOLEAN_TYPE
9064 && (complain
& tf_warning
))
9066 warning_at (rhs_loc
, OPT_Wparentheses
,
9067 "suggest parentheses around assignment used as truth value");
9068 TREE_NO_WARNING (rhs
) = 1;
9071 if (complain
& tf_warning
)
9072 warn_for_address_or_pointer_of_packed_member (type
, rhs
);
9074 return perform_implicit_conversion_flags (strip_top_quals (type
), rhs
,
9078 /* Convert RHS to be of type TYPE.
9079 If EXP is nonzero, it is the target of the initialization.
9080 ERRTYPE indicates what kind of error the implicit conversion is.
9082 Two major differences between the behavior of
9083 `convert_for_assignment' and `convert_for_initialization'
9084 are that references are bashed in the former, while
9085 copied in the latter, and aggregates are assigned in
9086 the former (operator=) while initialized in the
9089 If using constructor make sure no conversion operator exists, if one does
9090 exist, an ambiguity exists. */
9093 convert_for_initialization (tree exp
, tree type
, tree rhs
, int flags
,
9094 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
9095 tsubst_flags_t complain
)
9097 enum tree_code codel
= TREE_CODE (type
);
9099 enum tree_code coder
;
9101 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9102 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
9103 if (TREE_CODE (rhs
) == NOP_EXPR
9104 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
9105 && codel
!= REFERENCE_TYPE
)
9106 rhs
= TREE_OPERAND (rhs
, 0);
9108 if (type
== error_mark_node
9109 || rhs
== error_mark_node
9110 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
9111 return error_mark_node
;
9113 if (MAYBE_CLASS_TYPE_P (non_reference (type
)))
9115 else if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
9116 && TREE_CODE (type
) != ARRAY_TYPE
9117 && (!TYPE_REF_P (type
)
9118 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
9119 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
9120 && !TYPE_REFFN_P (type
))
9121 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
9122 rhs
= decay_conversion (rhs
, complain
);
9124 rhstype
= TREE_TYPE (rhs
);
9125 coder
= TREE_CODE (rhstype
);
9127 if (coder
== ERROR_MARK
)
9128 return error_mark_node
;
9130 /* We accept references to incomplete types, so we can
9131 return here before checking if RHS is of complete type. */
9133 if (codel
== REFERENCE_TYPE
)
9135 auto_diagnostic_group d
;
9136 /* This should eventually happen in convert_arguments. */
9137 int savew
= 0, savee
= 0;
9140 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
9141 rhs
= initialize_reference (type
, rhs
, flags
, complain
);
9144 && (warningcount
+ werrorcount
> savew
|| errorcount
> savee
))
9145 inform (get_fndecl_argument_location (fndecl
, parmnum
),
9146 "in passing argument %P of %qD", parmnum
, fndecl
);
9151 exp
= require_complete_type_sfinae (exp
, complain
);
9152 if (exp
== error_mark_node
)
9153 return error_mark_node
;
9155 rhstype
= non_reference (rhstype
);
9157 type
= complete_type (type
);
9159 if (DIRECT_INIT_EXPR_P (type
, rhs
))
9160 /* Don't try to do copy-initialization if we already have
9161 direct-initialization. */
9164 if (MAYBE_CLASS_TYPE_P (type
))
9165 return perform_implicit_conversion_flags (type
, rhs
, complain
, flags
);
9167 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
,
9171 /* If RETVAL is the address of, or a reference to, a local variable or
9172 temporary give an appropriate warning and return true. */
9175 maybe_warn_about_returning_address_of_local (tree retval
)
9177 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
9178 tree whats_returned
= fold_for_warn (retval
);
9179 location_t loc
= cp_expr_loc_or_loc (retval
, input_location
);
9183 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
9184 whats_returned
= TREE_OPERAND (whats_returned
, 1);
9185 else if (CONVERT_EXPR_P (whats_returned
)
9186 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
)
9187 whats_returned
= TREE_OPERAND (whats_returned
, 0);
9192 if (TREE_CODE (whats_returned
) == TARGET_EXPR
9193 && is_std_init_list (TREE_TYPE (whats_returned
)))
9195 tree init
= TARGET_EXPR_INITIAL (whats_returned
);
9196 if (TREE_CODE (init
) == CONSTRUCTOR
)
9197 /* Pull out the array address. */
9198 whats_returned
= CONSTRUCTOR_ELT (init
, 0)->value
;
9199 else if (TREE_CODE (init
) == INDIRECT_REF
)
9200 /* The source of a trivial copy looks like *(T*)&var. */
9201 whats_returned
= TREE_OPERAND (init
, 0);
9204 STRIP_NOPS (whats_returned
);
9207 /* As a special case, we handle a call to std::move or std::forward. */
9208 if (TREE_CODE (whats_returned
) == CALL_EXPR
9209 && (is_std_move_p (whats_returned
)
9210 || is_std_forward_p (whats_returned
)))
9212 tree arg
= CALL_EXPR_ARG (whats_returned
, 0);
9213 return maybe_warn_about_returning_address_of_local (arg
);
9216 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
9218 whats_returned
= TREE_OPERAND (whats_returned
, 0);
9220 while (TREE_CODE (whats_returned
) == COMPONENT_REF
9221 || TREE_CODE (whats_returned
) == ARRAY_REF
)
9222 whats_returned
= TREE_OPERAND (whats_returned
, 0);
9224 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
9225 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
9227 if (TYPE_REF_P (valtype
))
9228 warning_at (loc
, OPT_Wreturn_local_addr
,
9229 "returning reference to temporary");
9230 else if (is_std_init_list (valtype
))
9231 warning_at (loc
, OPT_Winit_list_lifetime
,
9232 "returning temporary initializer_list does not extend "
9233 "the lifetime of the underlying array");
9237 STRIP_ANY_LOCATION_WRAPPER (whats_returned
);
9239 if (DECL_P (whats_returned
)
9240 && DECL_NAME (whats_returned
)
9241 && DECL_FUNCTION_SCOPE_P (whats_returned
)
9242 && !is_capture_proxy (whats_returned
)
9243 && !(TREE_STATIC (whats_returned
)
9244 || TREE_PUBLIC (whats_returned
)))
9246 if (VAR_P (whats_returned
)
9247 && DECL_DECOMPOSITION_P (whats_returned
)
9248 && DECL_DECOMP_BASE (whats_returned
)
9249 && DECL_HAS_VALUE_EXPR_P (whats_returned
))
9251 /* When returning address of a structured binding, if the structured
9252 binding is not a reference, continue normally, if it is a
9253 reference, recurse on the initializer of the structured
9255 tree base
= DECL_DECOMP_BASE (whats_returned
);
9256 if (TYPE_REF_P (TREE_TYPE (base
)))
9258 tree init
= DECL_INITIAL (base
);
9259 return maybe_warn_about_returning_address_of_local (init
);
9263 auto_diagnostic_group d
;
9264 if (TYPE_REF_P (valtype
))
9265 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
9266 "reference to local variable %qD returned",
9268 else if (is_std_init_list (valtype
))
9269 w
= warning_at (loc
, OPT_Winit_list_lifetime
,
9270 "returning local initializer_list variable %qD "
9271 "does not extend the lifetime of the underlying array",
9273 else if (TREE_CODE (whats_returned
) == LABEL_DECL
)
9274 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
9275 "address of label %qD returned",
9278 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
9279 "address of local variable %qD returned",
9282 inform (DECL_SOURCE_LOCATION (whats_returned
),
9290 /* Returns true if DECL is in the std namespace. */
9293 decl_in_std_namespace_p (tree decl
)
9295 return (decl
!= NULL_TREE
9296 && DECL_NAMESPACE_STD_P (decl_namespace_context (decl
)));
9299 /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
9302 is_std_forward_p (tree fn
)
9304 /* std::forward only takes one argument. */
9305 if (call_expr_nargs (fn
) != 1)
9308 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
9309 if (!decl_in_std_namespace_p (fndecl
))
9312 tree name
= DECL_NAME (fndecl
);
9313 return name
&& id_equal (name
, "forward");
9316 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
9319 is_std_move_p (tree fn
)
9321 /* std::move only takes one argument. */
9322 if (call_expr_nargs (fn
) != 1)
9325 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
9326 if (!decl_in_std_namespace_p (fndecl
))
9329 tree name
= DECL_NAME (fndecl
);
9330 return name
&& id_equal (name
, "move");
9333 /* Returns true if RETVAL is a good candidate for the NRVO as per
9334 [class.copy.elision]. FUNCTYPE is the type the function is declared
9338 can_do_nrvo_p (tree retval
, tree functype
)
9340 if (functype
== error_mark_node
)
9343 STRIP_ANY_LOCATION_WRAPPER (retval
);
9344 tree result
= DECL_RESULT (current_function_decl
);
9345 return (retval
!= NULL_TREE
9346 && !processing_template_decl
9347 /* Must be a local, automatic variable. */
9349 && DECL_CONTEXT (retval
) == current_function_decl
9350 && !TREE_STATIC (retval
)
9351 /* And not a lambda or anonymous union proxy. */
9352 && !DECL_HAS_VALUE_EXPR_P (retval
)
9353 && (DECL_ALIGN (retval
) <= DECL_ALIGN (result
))
9354 /* The cv-unqualified type of the returned value must be the
9355 same as the cv-unqualified return type of the
9357 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval
))),
9358 (TYPE_MAIN_VARIANT (functype
)))
9359 /* And the returned value must be non-volatile. */
9360 && !TYPE_VOLATILE (TREE_TYPE (retval
)));
9363 /* Returns true if we should treat RETVAL, an expression being returned,
9364 as if it were designated by an rvalue. See [class.copy.elision].
9365 PARM_P is true if a function parameter is OK in this context. */
9368 treat_lvalue_as_rvalue_p (tree retval
, bool parm_ok
)
9370 STRIP_ANY_LOCATION_WRAPPER (retval
);
9371 return ((cxx_dialect
!= cxx98
)
9372 && ((VAR_P (retval
) && !DECL_HAS_VALUE_EXPR_P (retval
))
9373 || (parm_ok
&& TREE_CODE (retval
) == PARM_DECL
))
9374 && DECL_CONTEXT (retval
) == current_function_decl
9375 && !TREE_STATIC (retval
));
9378 /* Warn about wrong usage of std::move in a return statement. RETVAL
9379 is the expression we are returning; FUNCTYPE is the type the function
9380 is declared to return. */
9383 maybe_warn_pessimizing_move (tree retval
, tree functype
)
9385 if (!(warn_pessimizing_move
|| warn_redundant_move
))
9388 location_t loc
= cp_expr_loc_or_loc (retval
, input_location
);
9390 /* C++98 doesn't know move. */
9391 if (cxx_dialect
< cxx11
)
9394 /* Wait until instantiation time, since we can't gauge if we should do
9395 the NRVO until then. */
9396 if (processing_template_decl
)
9399 /* This is only interesting for class types. */
9400 if (!CLASS_TYPE_P (functype
))
9403 /* We're looking for *std::move<T&> (&arg). */
9404 if (REFERENCE_REF_P (retval
)
9405 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
9407 tree fn
= TREE_OPERAND (retval
, 0);
9408 if (is_std_move_p (fn
))
9410 tree arg
= CALL_EXPR_ARG (fn
, 0);
9412 if (TREE_CODE (arg
) != ADDR_EXPR
)
9414 arg
= TREE_OPERAND (arg
, 0);
9415 arg
= convert_from_reference (arg
);
9416 /* Warn if we could do copy elision were it not for the move. */
9417 if (can_do_nrvo_p (arg
, functype
))
9419 auto_diagnostic_group d
;
9420 if (warning_at (loc
, OPT_Wpessimizing_move
,
9421 "moving a local object in a return statement "
9422 "prevents copy elision"))
9423 inform (loc
, "remove %<std::move%> call");
9425 /* Warn if the move is redundant. It is redundant when we would
9426 do maybe-rvalue overload resolution even without std::move. */
9427 else if (treat_lvalue_as_rvalue_p (arg
, /*parm_ok*/true))
9429 auto_diagnostic_group d
;
9430 if (warning_at (loc
, OPT_Wredundant_move
,
9431 "redundant move in return statement"))
9432 inform (loc
, "remove %<std::move%> call");
9438 /* Check that returning RETVAL from the current function is valid.
9439 Return an expression explicitly showing all conversions required to
9440 change RETVAL into the function return type, and to assign it to
9441 the DECL_RESULT for the function. Set *NO_WARNING to true if
9442 code reaches end of non-void function warning shouldn't be issued
9443 on this RETURN_EXPR. */
9446 check_return_expr (tree retval
, bool *no_warning
)
9449 /* The type actually returned by the function. */
9451 /* The type the function is declared to return, or void if
9452 the declared type is incomplete. */
9454 int fn_returns_value_p
;
9456 *no_warning
= false;
9458 /* A `volatile' function is one that isn't supposed to return, ever.
9459 (This is a G++ extension, used to get better code for functions
9460 that call the `volatile' function.) */
9461 if (TREE_THIS_VOLATILE (current_function_decl
))
9462 warning (0, "function declared %<noreturn%> has a %<return%> statement");
9464 /* Check for various simple errors. */
9465 if (DECL_DESTRUCTOR_P (current_function_decl
))
9468 error ("returning a value from a destructor");
9471 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
9473 if (in_function_try_handler
)
9474 /* If a return statement appears in a handler of the
9475 function-try-block of a constructor, the program is ill-formed. */
9476 error ("cannot return from a handler of a function-try-block of a constructor");
9478 /* You can't return a value from a constructor. */
9479 error ("returning a value from a constructor");
9483 const tree saved_retval
= retval
;
9485 if (processing_template_decl
)
9487 current_function_returns_value
= 1;
9489 if (check_for_bare_parameter_packs (retval
))
9490 return error_mark_node
;
9492 /* If one of the types might be void, we can't tell whether we're
9493 returning a value. */
9494 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl
)))
9495 && !current_function_auto_return_pattern
)
9496 || (retval
!= NULL_TREE
9497 && (TREE_TYPE (retval
) == NULL_TREE
9498 || WILDCARD_TYPE_P (TREE_TYPE (retval
)))))
9502 functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
9504 /* Deduce auto return type from a return statement. */
9505 if (current_function_auto_return_pattern
)
9510 if (!retval
&& !is_auto (current_function_auto_return_pattern
))
9512 /* Give a helpful error message. */
9513 error ("return-statement with no value, in function returning %qT",
9514 current_function_auto_return_pattern
);
9515 inform (input_location
, "only plain %<auto%> return type can be "
9516 "deduced to %<void%>");
9517 type
= error_mark_node
;
9519 else if (retval
&& BRACE_ENCLOSED_INITIALIZER_P (retval
))
9521 error ("returning initializer list");
9522 type
= error_mark_node
;
9528 auto_node
= type_uses_auto (current_function_auto_return_pattern
);
9529 type
= do_auto_deduction (current_function_auto_return_pattern
,
9533 if (type
== error_mark_node
)
9535 else if (functype
== current_function_auto_return_pattern
)
9536 apply_deduced_return_type (current_function_decl
, type
);
9537 else if (!same_type_p (type
, functype
))
9539 if (LAMBDA_FUNCTION_P (current_function_decl
))
9540 error ("inconsistent types %qT and %qT deduced for "
9541 "lambda return type", functype
, type
);
9543 error ("inconsistent deduction for auto return type: "
9544 "%qT and then %qT", functype
, type
);
9549 result
= DECL_RESULT (current_function_decl
);
9550 valtype
= TREE_TYPE (result
);
9551 gcc_assert (valtype
!= NULL_TREE
);
9552 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
9554 /* Check for a return statement with no return value in a function
9555 that's supposed to return a value. */
9556 if (!retval
&& fn_returns_value_p
)
9558 if (functype
!= error_mark_node
)
9559 permerror (input_location
, "return-statement with no value, in "
9560 "function returning %qT", valtype
);
9561 /* Remember that this function did return. */
9562 current_function_returns_value
= 1;
9563 /* And signal caller that TREE_NO_WARNING should be set on the
9564 RETURN_EXPR to avoid control reaches end of non-void function
9565 warnings in tree-cfg.c. */
9568 /* Check for a return statement with a value in a function that
9569 isn't supposed to return a value. */
9570 else if (retval
&& !fn_returns_value_p
)
9572 if (VOID_TYPE_P (TREE_TYPE (retval
)))
9573 /* You can return a `void' value from a function of `void'
9574 type. In that case, we have to evaluate the expression for
9575 its side-effects. */
9576 finish_expr_stmt (retval
);
9578 permerror (input_location
,
9579 "return-statement with a value, in function "
9580 "returning %qT", valtype
);
9581 current_function_returns_null
= 1;
9583 /* There's really no value to return, after all. */
9587 /* Remember that this function can sometimes return without a
9589 current_function_returns_null
= 1;
9591 /* Remember that this function did return a value. */
9592 current_function_returns_value
= 1;
9594 /* Check for erroneous operands -- but after giving ourselves a
9595 chance to provide an error about returning a value from a void
9597 if (error_operand_p (retval
))
9599 current_function_return_value
= error_mark_node
;
9600 return error_mark_node
;
9603 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
9604 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl
))
9605 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
9607 && retval
&& null_ptr_cst_p (retval
))
9608 warning (0, "%<operator new%> must not return NULL unless it is "
9609 "declared %<throw()%> (or -fcheck-new is in effect)");
9611 /* Effective C++ rule 15. See also start_function. */
9613 && DECL_NAME (current_function_decl
) == assign_op_identifier
9614 && !type_dependent_expression_p (retval
))
9618 /* The function return type must be a reference to the current
9620 if (TYPE_REF_P (valtype
)
9621 && same_type_ignoring_top_level_qualifiers_p
9622 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
)))
9624 /* Returning '*this' is obviously OK. */
9625 if (retval
== current_class_ref
)
9627 /* If we are calling a function whose return type is the same of
9628 the current class reference, it is ok. */
9629 else if (INDIRECT_REF_P (retval
)
9630 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
9635 warning (OPT_Weffc__
, "%<operator=%> should return a reference to %<*this%>");
9638 if (dependent_type_p (functype
)
9639 || type_dependent_expression_p (retval
))
9642 /* We should not have changed the return value. */
9643 gcc_assert (retval
== saved_retval
);
9647 /* The fabled Named Return Value optimization, as per [class.copy]/15:
9649 [...] For a function with a class return type, if the expression
9650 in the return statement is the name of a local object, and the cv-
9651 unqualified type of the local object is the same as the function
9652 return type, an implementation is permitted to omit creating the tem-
9653 porary object to hold the function return value [...]
9655 So, if this is a value-returning function that always returns the same
9656 local variable, remember it.
9658 It might be nice to be more flexible, and choose the first suitable
9659 variable even if the function sometimes returns something else, but
9660 then we run the risk of clobbering the variable we chose if the other
9661 returned expression uses the chosen variable somehow. And people expect
9662 this restriction, anyway. (jason 2000-11-19)
9664 See finish_function and finalize_nrv for the rest of this optimization. */
9666 STRIP_ANY_LOCATION_WRAPPER (retval
);
9668 bool named_return_value_okay_p
= can_do_nrvo_p (retval
, functype
);
9669 if (fn_returns_value_p
&& flag_elide_constructors
)
9671 if (named_return_value_okay_p
9672 && (current_function_return_value
== NULL_TREE
9673 || current_function_return_value
== retval
))
9674 current_function_return_value
= retval
;
9676 current_function_return_value
= error_mark_node
;
9679 /* We don't need to do any conversions when there's nothing being
9684 if (!named_return_value_okay_p
)
9685 maybe_warn_pessimizing_move (retval
, functype
);
9687 /* Do any required conversions. */
9688 if (retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
9689 /* No conversions are required. */
9693 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
9695 /* The functype's return type will have been set to void, if it
9696 was an incomplete type. Just treat this as 'return;' */
9697 if (VOID_TYPE_P (functype
))
9698 return error_mark_node
;
9700 /* If we had an id-expression obfuscated by force_paren_expr, we need
9701 to undo it so we can try to treat it as an rvalue below. */
9702 retval
= maybe_undo_parenthesized_ref (retval
);
9704 if (processing_template_decl
)
9705 retval
= build_non_dependent_expr (retval
);
9707 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9708 treated as an rvalue for the purposes of overload resolution to
9709 favor move constructors over copy constructors.
9711 Note that these conditions are similar to, but not as strict as,
9712 the conditions for the named return value optimization. */
9713 bool converted
= false;
9714 if (treat_lvalue_as_rvalue_p (retval
, /*parm_ok*/true)
9715 /* This is only interesting for class type. */
9716 && CLASS_TYPE_P (functype
))
9718 tree moved
= move (retval
);
9719 moved
= convert_for_initialization
9720 (NULL_TREE
, functype
, moved
, flags
|LOOKUP_PREFER_RVALUE
,
9721 ICR_RETURN
, NULL_TREE
, 0, tf_none
);
9722 if (moved
!= error_mark_node
)
9729 /* First convert the value to the function's return type, then
9730 to the type of return value's location to handle the
9731 case that functype is smaller than the valtype. */
9733 retval
= convert_for_initialization
9734 (NULL_TREE
, functype
, retval
, flags
, ICR_RETURN
, NULL_TREE
, 0,
9735 tf_warning_or_error
);
9736 retval
= convert (valtype
, retval
);
9738 /* If the conversion failed, treat this just like `return;'. */
9739 if (retval
== error_mark_node
)
9741 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9742 else if (! cfun
->returns_struct
9743 && TREE_CODE (retval
) == TARGET_EXPR
9744 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
9745 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
9746 TREE_OPERAND (retval
, 0));
9747 else if (!processing_template_decl
9748 && maybe_warn_about_returning_address_of_local (retval
)
9749 && INDIRECT_TYPE_P (valtype
))
9750 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
9751 build_zero_cst (TREE_TYPE (retval
)));
9754 if (processing_template_decl
)
9755 return saved_retval
;
9757 /* Actually copy the value returned into the appropriate location. */
9758 if (retval
&& retval
!= result
)
9759 retval
= build2 (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
9765 /* Returns nonzero if the pointer-type FROM can be converted to the
9766 pointer-type TO via a qualification conversion. If CONSTP is -1,
9767 then we return nonzero if the pointers are similar, and the
9768 cv-qualification signature of FROM is a proper subset of that of TO.
9770 If CONSTP is positive, then all outer pointers have been
9774 comp_ptr_ttypes_real (tree to
, tree from
, int constp
)
9776 bool to_more_cv_qualified
= false;
9777 bool is_opaque_pointer
= false;
9779 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9781 if (TREE_CODE (to
) != TREE_CODE (from
))
9784 if (TREE_CODE (from
) == OFFSET_TYPE
9785 && !same_type_p (TYPE_OFFSET_BASETYPE (from
),
9786 TYPE_OFFSET_BASETYPE (to
)))
9789 /* Const and volatile mean something different for function types,
9790 so the usual checks are not appropriate. */
9791 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
9793 if (!at_least_as_qualified_p (to
, from
))
9796 if (!at_least_as_qualified_p (from
, to
))
9800 to_more_cv_qualified
= true;
9804 constp
&= TYPE_READONLY (to
);
9807 if (VECTOR_TYPE_P (to
))
9808 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
9810 if (!TYPE_PTR_P (to
) && !TYPE_PTRDATAMEM_P (to
))
9811 return ((constp
>= 0 || to_more_cv_qualified
)
9812 && (is_opaque_pointer
9813 || same_type_ignoring_top_level_qualifiers_p (to
, from
)));
9817 /* When comparing, say, char ** to char const **, this function takes
9818 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9819 types to this function. */
9822 comp_ptr_ttypes (tree to
, tree from
)
9824 return comp_ptr_ttypes_real (to
, from
, 1);
9827 /* Returns true iff FNTYPE is a non-class type that involves
9828 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9829 if a parameter type is ill-formed. */
9832 error_type_p (const_tree type
)
9836 switch (TREE_CODE (type
))
9842 case REFERENCE_TYPE
:
9844 return error_type_p (TREE_TYPE (type
));
9848 if (error_type_p (TREE_TYPE (type
)))
9850 for (t
= TYPE_ARG_TYPES (type
); t
; t
= TREE_CHAIN (t
))
9851 if (error_type_p (TREE_VALUE (t
)))
9856 if (TYPE_PTRMEMFUNC_P (type
))
9857 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type
));
9865 /* Returns true if to and from are (possibly multi-level) pointers to the same
9866 type or inheritance-related types, regardless of cv-quals. */
9869 ptr_reasonably_similar (const_tree to
, const_tree from
)
9871 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9873 /* Any target type is similar enough to void. */
9874 if (VOID_TYPE_P (to
))
9875 return !error_type_p (from
);
9876 if (VOID_TYPE_P (from
))
9877 return !error_type_p (to
);
9879 if (TREE_CODE (to
) != TREE_CODE (from
))
9882 if (TREE_CODE (from
) == OFFSET_TYPE
9883 && comptypes (TYPE_OFFSET_BASETYPE (to
),
9884 TYPE_OFFSET_BASETYPE (from
),
9885 COMPARE_BASE
| COMPARE_DERIVED
))
9888 if (VECTOR_TYPE_P (to
)
9889 && vector_types_convertible_p (to
, from
, false))
9892 if (TREE_CODE (to
) == INTEGER_TYPE
9893 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
9896 if (TREE_CODE (to
) == FUNCTION_TYPE
)
9897 return !error_type_p (to
) && !error_type_p (from
);
9899 if (!TYPE_PTR_P (to
))
9901 /* When either type is incomplete avoid DERIVED_FROM_P,
9902 which may call complete_type (c++/57942). */
9903 bool b
= !COMPLETE_TYPE_P (to
) || !COMPLETE_TYPE_P (from
);
9905 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
9906 b
? COMPARE_STRICT
: COMPARE_BASE
| COMPARE_DERIVED
);
9911 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9912 pointer-to-member types) are the same, ignoring cv-qualification at
9916 comp_ptr_ttypes_const (tree to
, tree from
)
9918 bool is_opaque_pointer
= false;
9920 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9922 if (TREE_CODE (to
) != TREE_CODE (from
))
9925 if (TREE_CODE (from
) == OFFSET_TYPE
9926 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
9927 TYPE_OFFSET_BASETYPE (to
)))
9930 if (VECTOR_TYPE_P (to
))
9931 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
9933 if (!TYPE_PTR_P (to
))
9934 return (is_opaque_pointer
9935 || same_type_ignoring_top_level_qualifiers_p (to
, from
));
9939 /* Returns the type qualifiers for this type, including the qualifiers on the
9940 elements for an array type. */
9943 cp_type_quals (const_tree type
)
9946 /* This CONST_CAST is okay because strip_array_types returns its
9947 argument unmodified and we assign it to a const_tree. */
9948 type
= strip_array_types (CONST_CAST_TREE (type
));
9949 if (type
== error_mark_node
9950 /* Quals on a FUNCTION_TYPE are memfn quals. */
9951 || TREE_CODE (type
) == FUNCTION_TYPE
)
9952 return TYPE_UNQUALIFIED
;
9953 quals
= TYPE_QUALS (type
);
9954 /* METHOD and REFERENCE_TYPEs should never have quals. */
9955 gcc_assert ((TREE_CODE (type
) != METHOD_TYPE
9956 && !TYPE_REF_P (type
))
9957 || ((quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
))
9958 == TYPE_UNQUALIFIED
));
9962 /* Returns the function-ref-qualifier for TYPE */
9965 type_memfn_rqual (const_tree type
)
9967 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
9968 || TREE_CODE (type
) == METHOD_TYPE
);
9970 if (!FUNCTION_REF_QUALIFIED (type
))
9971 return REF_QUAL_NONE
;
9972 else if (FUNCTION_RVALUE_QUALIFIED (type
))
9973 return REF_QUAL_RVALUE
;
9975 return REF_QUAL_LVALUE
;
9978 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9982 type_memfn_quals (const_tree type
)
9984 if (TREE_CODE (type
) == FUNCTION_TYPE
)
9985 return TYPE_QUALS (type
);
9986 else if (TREE_CODE (type
) == METHOD_TYPE
)
9987 return cp_type_quals (class_of_this_parm (type
));
9992 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9993 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9996 apply_memfn_quals (tree type
, cp_cv_quals memfn_quals
, cp_ref_qualifier rqual
)
9998 /* Could handle METHOD_TYPE here if necessary. */
9999 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
10000 if (TYPE_QUALS (type
) == memfn_quals
10001 && type_memfn_rqual (type
) == rqual
)
10004 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
10006 tree result
= build_qualified_type (type
, memfn_quals
);
10007 return build_ref_qualified_type (result
, rqual
);
10010 /* Returns nonzero if TYPE is const or volatile. */
10013 cv_qualified_p (const_tree type
)
10015 int quals
= cp_type_quals (type
);
10016 return (quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)) != 0;
10019 /* Returns nonzero if the TYPE contains a mutable member. */
10022 cp_has_mutable_p (const_tree type
)
10024 /* This CONST_CAST is okay because strip_array_types returns its
10025 argument unmodified and we assign it to a const_tree. */
10026 type
= strip_array_types (CONST_CAST_TREE(type
));
10028 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
10031 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
10032 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
10033 approximation. In particular, consider:
10036 struct S { int i; };
10037 const S s = { f(); }
10039 Here, we will make "s" as TREE_READONLY (because it is declared
10040 "const") -- only to reverse ourselves upon seeing that the
10041 initializer is non-constant. */
10044 cp_apply_type_quals_to_decl (int type_quals
, tree decl
)
10046 tree type
= TREE_TYPE (decl
);
10048 if (type
== error_mark_node
)
10051 if (TREE_CODE (decl
) == TYPE_DECL
)
10054 gcc_assert (!(TREE_CODE (type
) == FUNCTION_TYPE
10055 && type_quals
!= TYPE_UNQUALIFIED
));
10057 /* Avoid setting TREE_READONLY incorrectly. */
10058 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
10059 constructor can produce constant init, so rely on cp_finish_decl to
10060 clear TREE_READONLY if the variable has non-constant init. */
10062 /* If the type has (or might have) a mutable component, that component
10063 might be modified. */
10064 if (TYPE_HAS_MUTABLE_P (type
) || !COMPLETE_TYPE_P (type
))
10065 type_quals
&= ~TYPE_QUAL_CONST
;
10067 c_apply_type_quals_to_decl (type_quals
, decl
);
10070 /* Subroutine of casts_away_constness. Make T1 and T2 point at
10071 exemplar types such that casting T1 to T2 is casting away constness
10072 if and only if there is no implicit conversion from T1 to T2. */
10075 casts_away_constness_r (tree
*t1
, tree
*t2
, tsubst_flags_t complain
)
10080 /* [expr.const.cast]
10082 For multi-level pointer to members and multi-level mixed pointers
10083 and pointers to members (conv.qual), the "member" aspect of a
10084 pointer to member level is ignored when determining if a const
10085 cv-qualifier has been cast away. */
10086 /* [expr.const.cast]
10088 For two pointer types:
10090 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
10091 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
10094 casting from X1 to X2 casts away constness if, for a non-pointer
10095 type T there does not exist an implicit conversion (clause
10098 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
10102 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
10103 if ((!TYPE_PTR_P (*t1
) && !TYPE_PTRDATAMEM_P (*t1
))
10104 || (!TYPE_PTR_P (*t2
) && !TYPE_PTRDATAMEM_P (*t2
)))
10106 *t1
= cp_build_qualified_type (void_type_node
,
10107 cp_type_quals (*t1
));
10108 *t2
= cp_build_qualified_type (void_type_node
,
10109 cp_type_quals (*t2
));
10113 quals1
= cp_type_quals (*t1
);
10114 quals2
= cp_type_quals (*t2
);
10116 if (TYPE_PTRDATAMEM_P (*t1
))
10117 *t1
= TYPE_PTRMEM_POINTED_TO_TYPE (*t1
);
10119 *t1
= TREE_TYPE (*t1
);
10120 if (TYPE_PTRDATAMEM_P (*t2
))
10121 *t2
= TYPE_PTRMEM_POINTED_TO_TYPE (*t2
);
10123 *t2
= TREE_TYPE (*t2
);
10125 casts_away_constness_r (t1
, t2
, complain
);
10126 *t1
= build_pointer_type (*t1
);
10127 *t2
= build_pointer_type (*t2
);
10128 *t1
= cp_build_qualified_type (*t1
, quals1
);
10129 *t2
= cp_build_qualified_type (*t2
, quals2
);
10132 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
10135 ??? This function returns non-zero if casting away qualifiers not
10136 just const. We would like to return to the caller exactly which
10137 qualifiers are casted away to give more accurate diagnostics.
10141 casts_away_constness (tree t1
, tree t2
, tsubst_flags_t complain
)
10143 if (TYPE_REF_P (t2
))
10145 /* [expr.const.cast]
10147 Casting from an lvalue of type T1 to an lvalue of type T2
10148 using a reference cast casts away constness if a cast from an
10149 rvalue of type "pointer to T1" to the type "pointer to T2"
10150 casts away constness. */
10151 t1
= (TYPE_REF_P (t1
) ? TREE_TYPE (t1
) : t1
);
10152 return casts_away_constness (build_pointer_type (t1
),
10153 build_pointer_type (TREE_TYPE (t2
)),
10157 if (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
10158 /* [expr.const.cast]
10160 Casting from an rvalue of type "pointer to data member of X
10161 of type T1" to the type "pointer to data member of Y of type
10162 T2" casts away constness if a cast from an rvalue of type
10163 "pointer to T1" to the type "pointer to T2" casts away
10165 return casts_away_constness
10166 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1
)),
10167 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2
)),
10170 /* Casting away constness is only something that makes sense for
10171 pointer or reference types. */
10172 if (!TYPE_PTR_P (t1
) || !TYPE_PTR_P (t2
))
10175 /* Top-level qualifiers don't matter. */
10176 t1
= TYPE_MAIN_VARIANT (t1
);
10177 t2
= TYPE_MAIN_VARIANT (t2
);
10178 casts_away_constness_r (&t1
, &t2
, complain
);
10179 if (!can_convert (t2
, t1
, complain
))
10185 /* If T is a REFERENCE_TYPE return the type to which T refers.
10186 Otherwise, return T itself. */
10189 non_reference (tree t
)
10191 if (t
&& TYPE_REF_P (t
))
10197 /* Return nonzero if REF is an lvalue valid for this language;
10198 otherwise, print an error message and return zero. USE says
10199 how the lvalue is being used and so selects the error message. */
10202 lvalue_or_else (tree ref
, enum lvalue_use use
, tsubst_flags_t complain
)
10204 cp_lvalue_kind kind
= lvalue_kind (ref
);
10206 if (kind
== clk_none
)
10208 if (complain
& tf_error
)
10209 lvalue_error (input_location
, use
);
10212 else if (kind
& (clk_rvalueref
|clk_class
))
10214 if (!(complain
& tf_error
))
10216 /* Make this a permerror because we used to accept it. */
10217 permerror (input_location
, "using rvalue as lvalue");
10222 /* Return true if a user-defined literal operator is a raw operator. */
10225 check_raw_literal_operator (const_tree decl
)
10227 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
10230 bool maybe_raw_p
= false;
10232 /* Count the number and type of arguments and check for ellipsis. */
10233 for (argtype
= argtypes
, arity
= 0;
10234 argtype
&& argtype
!= void_list_node
;
10235 ++arity
, argtype
= TREE_CHAIN (argtype
))
10237 tree t
= TREE_VALUE (argtype
);
10239 if (same_type_p (t
, const_string_type_node
))
10240 maybe_raw_p
= true;
10243 return false; /* Found ellipsis. */
10245 if (!maybe_raw_p
|| arity
!= 1)
10252 /* Return true if a user-defined literal operator has one of the allowed
10256 check_literal_operator_args (const_tree decl
,
10257 bool *long_long_unsigned_p
, bool *long_double_p
)
10259 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
10261 *long_long_unsigned_p
= false;
10262 *long_double_p
= false;
10263 if (processing_template_decl
|| processing_specialization
)
10264 return argtypes
== void_list_node
;
10271 /* Count the number and type of arguments and check for ellipsis. */
10272 for (argtype
= argtypes
, arity
= 0;
10273 argtype
&& argtype
!= void_list_node
;
10274 argtype
= TREE_CHAIN (argtype
))
10276 tree t
= TREE_VALUE (argtype
);
10279 if (TYPE_PTR_P (t
))
10281 bool maybe_raw_p
= false;
10283 if (cp_type_quals (t
) != TYPE_QUAL_CONST
)
10285 t
= TYPE_MAIN_VARIANT (t
);
10286 if ((maybe_raw_p
= same_type_p (t
, char_type_node
))
10287 || same_type_p (t
, wchar_type_node
)
10288 || same_type_p (t
, char8_type_node
)
10289 || same_type_p (t
, char16_type_node
)
10290 || same_type_p (t
, char32_type_node
))
10292 argtype
= TREE_CHAIN (argtype
);
10295 t
= TREE_VALUE (argtype
);
10296 if (maybe_raw_p
&& argtype
== void_list_node
)
10298 else if (same_type_p (t
, size_type_node
))
10307 else if (same_type_p (t
, long_long_unsigned_type_node
))
10310 *long_long_unsigned_p
= true;
10312 else if (same_type_p (t
, long_double_type_node
))
10315 *long_double_p
= true;
10317 else if (same_type_p (t
, char_type_node
))
10319 else if (same_type_p (t
, wchar_type_node
))
10321 else if (same_type_p (t
, char8_type_node
))
10323 else if (same_type_p (t
, char16_type_node
))
10325 else if (same_type_p (t
, char32_type_node
))
10331 return false; /* Found ellipsis. */
10333 if (arity
!= max_arity
)
10340 /* Always returns false since unlike C90, C++ has no concept of implicit
10341 function declarations. */
10344 c_decl_implicit (const_tree
)