1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2018 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 /* To get the size of a static data member declared as an array of
1686 unknown bound, we need to instantiate it. */
1688 && VAR_HAD_UNKNOWN_BOUND (e
)
1689 && DECL_TEMPLATE_INSTANTIATION (e
))
1690 instantiate_decl (e
, /*defer_ok*/true, /*expl_inst_mem*/false);
1692 if (TREE_CODE (e
) == PARM_DECL
1693 && DECL_ARRAY_PARAMETER_P (e
)
1694 && (complain
& tf_warning
))
1696 auto_diagnostic_group d
;
1697 if (warning (OPT_Wsizeof_array_argument
, "%<sizeof%> on array function "
1698 "parameter %qE will return size of %qT", e
, TREE_TYPE (e
)))
1699 inform (DECL_SOURCE_LOCATION (e
), "declared here");
1702 e
= mark_type_use (e
);
1706 if (complain
& tf_error
)
1707 error ("invalid application of %<sizeof%> to a bit-field");
1709 return error_mark_node
;
1712 else if (is_overloaded_fn (e
))
1714 if (complain
& tf_error
)
1715 permerror (input_location
, "ISO C++ forbids applying %<sizeof%> to an expression of "
1718 return error_mark_node
;
1721 else if (type_unknown_p (e
))
1723 if (complain
& tf_error
)
1724 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1726 return error_mark_node
;
1732 return cxx_sizeof_or_alignof_type (e
, SIZEOF_EXPR
, false, complain
& tf_error
);
1735 /* Implement the __alignof keyword: Return the minimum required
1736 alignment of E, measured in bytes. For VAR_DECL's and
1737 FIELD_DECL's return DECL_ALIGN (which can be set from an
1738 "aligned" __attribute__ specification). */
1741 cxx_alignof_expr (tree e
, tsubst_flags_t complain
)
1745 if (e
== error_mark_node
)
1746 return error_mark_node
;
1748 if (processing_template_decl
)
1750 e
= build_min (ALIGNOF_EXPR
, size_type_node
, e
);
1751 TREE_SIDE_EFFECTS (e
) = 0;
1752 TREE_READONLY (e
) = 1;
1757 e
= mark_type_use (e
);
1760 t
= size_int (DECL_ALIGN_UNIT (e
));
1761 else if (bitfield_p (e
))
1763 if (complain
& tf_error
)
1764 error ("invalid application of %<__alignof%> to a bit-field");
1766 return error_mark_node
;
1769 else if (TREE_CODE (e
) == COMPONENT_REF
1770 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
)
1771 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e
, 1)));
1772 else if (is_overloaded_fn (e
))
1774 if (complain
& tf_error
)
1775 permerror (input_location
, "ISO C++ forbids applying %<__alignof%> to an expression of "
1778 return error_mark_node
;
1779 if (TREE_CODE (e
) == FUNCTION_DECL
)
1780 t
= size_int (DECL_ALIGN_UNIT (e
));
1784 else if (type_unknown_p (e
))
1786 if (complain
& tf_error
)
1787 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1789 return error_mark_node
;
1793 return cxx_sizeof_or_alignof_type (TREE_TYPE (e
), ALIGNOF_EXPR
, false,
1794 complain
& tf_error
);
1796 return fold_convert (size_type_node
, t
);
1799 /* Process a sizeof or alignof expression E with code OP where the operand
1800 is an expression. */
1803 cxx_sizeof_or_alignof_expr (tree e
, enum tree_code op
, bool complain
)
1805 if (op
== SIZEOF_EXPR
)
1806 return cxx_sizeof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1808 return cxx_alignof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1811 /* Build a representation of an expression 'alignas(E).' Return the
1812 folded integer value of E if it is an integral constant expression
1813 that resolves to a valid alignment. If E depends on a template
1814 parameter, return a syntactic representation tree of kind
1815 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1816 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1819 cxx_alignas_expr (tree e
)
1821 if (e
== NULL_TREE
|| e
== error_mark_node
1822 || (!TYPE_P (e
) && !require_potential_rvalue_constant_expression (e
)))
1828 When the alignment-specifier is of the form
1829 alignas(type-id ), it shall have the same effect as
1830 alignas(alignof(type-id )). */
1832 return cxx_sizeof_or_alignof_type (e
, ALIGNOF_EXPR
, true, false);
1834 /* If we reach this point, it means the alignas expression if of
1835 the form "alignas(assignment-expression)", so we should follow
1836 what is stated by [dcl.align]/2. */
1838 if (value_dependent_expression_p (e
))
1839 /* Leave value-dependent expression alone for now. */
1842 e
= instantiate_non_dependent_expr (e
);
1843 e
= mark_rvalue_use (e
);
1845 /* [dcl.align]/2 says:
1847 the assignment-expression shall be an integral constant
1850 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e
)))
1852 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e
));
1853 return error_mark_node
;
1856 return cxx_constant_value (e
);
1860 /* EXPR is being used in a context that is not a function call.
1865 The expression can be used only as the left-hand operand of a
1866 member function call.
1868 [expr.mptr.operator]
1870 If the result of .* or ->* is a function, then that result can be
1871 used only as the operand for the function call operator ().
1873 by issuing an error message if appropriate. Returns true iff EXPR
1874 violates these rules. */
1877 invalid_nonstatic_memfn_p (location_t loc
, tree expr
, tsubst_flags_t complain
)
1879 if (expr
== NULL_TREE
)
1881 /* Don't enforce this in MS mode. */
1882 if (flag_ms_extensions
)
1884 if (is_overloaded_fn (expr
) && !really_overloaded_fn (expr
))
1885 expr
= get_first_fn (expr
);
1886 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr
))
1888 if (complain
& tf_error
)
1892 error_at (loc
, "invalid use of non-static member function %qD",
1894 inform (DECL_SOURCE_LOCATION (expr
), "declared here");
1897 error_at (loc
, "invalid use of non-static member function of "
1898 "type %qT", TREE_TYPE (expr
));
1905 /* If EXP is a reference to a bitfield, and the type of EXP does not
1906 match the declared type of the bitfield, return the declared type
1907 of the bitfield. Otherwise, return NULL_TREE. */
1910 is_bitfield_expr_with_lowered_type (const_tree exp
)
1912 switch (TREE_CODE (exp
))
1915 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1)
1916 ? TREE_OPERAND (exp
, 1)
1917 : TREE_OPERAND (exp
, 0)))
1919 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 2));
1922 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1));
1926 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1932 field
= TREE_OPERAND (exp
, 1);
1933 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_BIT_FIELD_TYPE (field
))
1935 if (same_type_ignoring_top_level_qualifiers_p
1936 (TREE_TYPE (exp
), DECL_BIT_FIELD_TYPE (field
)))
1938 return DECL_BIT_FIELD_TYPE (field
);
1942 if (DECL_HAS_VALUE_EXPR_P (exp
))
1943 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
1944 (CONST_CAST_TREE (exp
)));
1948 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp
, 0)))
1949 == TYPE_MAIN_VARIANT (TREE_TYPE (exp
)))
1950 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1958 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1959 bitfield with a lowered type, the type of EXP is returned, rather
1963 unlowered_expr_type (const_tree exp
)
1966 tree etype
= TREE_TYPE (exp
);
1968 type
= is_bitfield_expr_with_lowered_type (exp
);
1970 type
= cp_build_qualified_type (type
, cp_type_quals (etype
));
1977 /* Perform the conversions in [expr] that apply when an lvalue appears
1978 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1979 function-to-pointer conversions. In addition, bitfield references are
1980 converted to their declared types. Note that this function does not perform
1981 the lvalue-to-rvalue conversion for class types. If you need that conversion
1982 for class types, then you probably need to use force_rvalue.
1984 Although the returned value is being used as an rvalue, this
1985 function does not wrap the returned expression in a
1986 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1987 that the return value is no longer an lvalue. */
1990 decay_conversion (tree exp
,
1991 tsubst_flags_t complain
,
1992 bool reject_builtin
/* = true */)
1995 enum tree_code code
;
1996 location_t loc
= cp_expr_loc_or_loc (exp
, input_location
);
1998 type
= TREE_TYPE (exp
);
1999 if (type
== error_mark_node
)
2000 return error_mark_node
;
2002 exp
= resolve_nondeduced_context (exp
, complain
);
2003 if (type_unknown_p (exp
))
2005 if (complain
& tf_error
)
2006 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
2007 return error_mark_node
;
2010 code
= TREE_CODE (type
);
2012 if (error_operand_p (exp
))
2013 return error_mark_node
;
2015 if (NULLPTR_TYPE_P (type
) && !TREE_SIDE_EFFECTS (exp
))
2017 mark_rvalue_use (exp
, loc
, reject_builtin
);
2018 return nullptr_node
;
2021 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2022 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2023 if (code
== VOID_TYPE
)
2025 if (complain
& tf_error
)
2026 error_at (loc
, "void value not ignored as it ought to be");
2027 return error_mark_node
;
2029 if (invalid_nonstatic_memfn_p (loc
, exp
, complain
))
2030 return error_mark_node
;
2031 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
2033 exp
= mark_lvalue_use (exp
);
2034 if (reject_builtin
&& reject_gcc_builtin (exp
, loc
))
2035 return error_mark_node
;
2036 return cp_build_addr_expr (exp
, complain
);
2038 if (code
== ARRAY_TYPE
)
2043 exp
= mark_lvalue_use (exp
);
2045 if (INDIRECT_REF_P (exp
))
2046 return build_nop (build_pointer_type (TREE_TYPE (type
)),
2047 TREE_OPERAND (exp
, 0));
2049 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
2051 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1), complain
);
2052 if (op1
== error_mark_node
)
2053 return error_mark_node
;
2054 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
2055 TREE_OPERAND (exp
, 0), op1
);
2058 if (!obvalue_p (exp
)
2059 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
2061 if (complain
& tf_error
)
2062 error_at (loc
, "invalid use of non-lvalue array");
2063 return error_mark_node
;
2066 /* Don't let an array compound literal decay to a pointer. It can
2067 still be used to initialize an array or bind to a reference. */
2068 if (TREE_CODE (exp
) == TARGET_EXPR
)
2070 if (complain
& tf_error
)
2071 error_at (loc
, "taking address of temporary array");
2072 return error_mark_node
;
2075 ptrtype
= build_pointer_type (TREE_TYPE (type
));
2079 if (!cxx_mark_addressable (exp
))
2080 return error_mark_node
;
2081 adr
= build_nop (ptrtype
, build_address (exp
));
2084 /* This way is better for a COMPONENT_REF since it can
2085 simplify the offset for a component. */
2086 adr
= cp_build_addr_expr (exp
, complain
);
2087 return cp_convert (ptrtype
, adr
, complain
);
2090 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2091 exp
= mark_rvalue_use (exp
, loc
, reject_builtin
);
2093 /* If a bitfield is used in a context where integral promotion
2094 applies, then the caller is expected to have used
2095 default_conversion. That function promotes bitfields correctly
2096 before calling this function. At this point, if we have a
2097 bitfield referenced, we may assume that is not subject to
2098 promotion, and that, therefore, the type of the resulting rvalue
2099 is the declared type of the bitfield. */
2100 exp
= convert_bitfield_to_declared_type (exp
);
2102 /* We do not call rvalue() here because we do not want to wrap EXP
2103 in a NON_LVALUE_EXPR. */
2107 Non-class rvalues always have cv-unqualified types. */
2108 type
= TREE_TYPE (exp
);
2109 if (!CLASS_TYPE_P (type
) && cv_qualified_p (type
))
2110 exp
= build_nop (cv_unqualified (type
), exp
);
2112 if (!complete_type_or_maybe_complain (type
, exp
, complain
))
2113 return error_mark_node
;
2118 /* Perform preparatory conversions, as part of the "usual arithmetic
2119 conversions". In particular, as per [expr]:
2121 Whenever an lvalue expression appears as an operand of an
2122 operator that expects the rvalue for that operand, the
2123 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2124 standard conversions are applied to convert the expression to an
2127 In addition, we perform integral promotions here, as those are
2128 applied to both operands to a binary operator before determining
2129 what additional conversions should apply. */
2132 cp_default_conversion (tree exp
, tsubst_flags_t complain
)
2134 /* Check for target-specific promotions. */
2135 tree promoted_type
= targetm
.promoted_type (TREE_TYPE (exp
));
2137 exp
= cp_convert (promoted_type
, exp
, complain
);
2138 /* Perform the integral promotions first so that bitfield
2139 expressions (which may promote to "int", even if the bitfield is
2140 declared "unsigned") are promoted correctly. */
2141 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp
)))
2142 exp
= cp_perform_integral_promotions (exp
, complain
);
2143 /* Perform the other conversions. */
2144 exp
= decay_conversion (exp
, complain
);
2152 default_conversion (tree exp
)
2154 return cp_default_conversion (exp
, tf_warning_or_error
);
2157 /* EXPR is an expression with an integral or enumeration type.
2158 Perform the integral promotions in [conv.prom], and return the
2162 cp_perform_integral_promotions (tree expr
, tsubst_flags_t complain
)
2167 expr
= mark_rvalue_use (expr
);
2168 if (error_operand_p (expr
))
2169 return error_mark_node
;
2173 If the bitfield has an enumerated type, it is treated as any
2174 other value of that type for promotion purposes. */
2175 type
= is_bitfield_expr_with_lowered_type (expr
);
2176 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
2177 type
= TREE_TYPE (expr
);
2178 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
2179 /* Scoped enums don't promote. */
2180 if (SCOPED_ENUM_P (type
))
2182 promoted_type
= type_promotes_to (type
);
2183 if (type
!= promoted_type
)
2184 expr
= cp_convert (promoted_type
, expr
, complain
);
2191 perform_integral_promotions (tree expr
)
2193 return cp_perform_integral_promotions (expr
, tf_warning_or_error
);
2196 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2197 decay_conversion to one. */
2200 string_conv_p (const_tree totype
, const_tree exp
, int warn
)
2204 if (!TYPE_PTR_P (totype
))
2207 t
= TREE_TYPE (totype
);
2208 if (!same_type_p (t
, char_type_node
)
2209 && !same_type_p (t
, char16_type_node
)
2210 && !same_type_p (t
, char32_type_node
)
2211 && !same_type_p (t
, wchar_type_node
))
2214 location_t loc
= EXPR_LOC_OR_LOC (exp
, input_location
);
2216 STRIP_ANY_LOCATION_WRAPPER (exp
);
2218 if (TREE_CODE (exp
) == STRING_CST
)
2220 /* Make sure that we don't try to convert between char and wide chars. */
2221 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
2226 /* Is this a string constant which has decayed to 'const char *'? */
2227 t
= build_pointer_type (cp_build_qualified_type (t
, TYPE_QUAL_CONST
));
2228 if (!same_type_p (TREE_TYPE (exp
), t
))
2231 if (TREE_CODE (exp
) != ADDR_EXPR
2232 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
2237 if (cxx_dialect
>= cxx11
)
2238 pedwarn (loc
, OPT_Wwrite_strings
,
2239 "ISO C++ forbids converting a string constant to %qT",
2242 warning_at (loc
, OPT_Wwrite_strings
,
2243 "deprecated conversion from string constant to %qT",
2250 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2251 can, for example, use as an lvalue. This code used to be in
2252 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2253 expressions, where we're dealing with aggregates. But now it's again only
2254 called from unary_complex_lvalue. The case (in particular) that led to
2255 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2259 rationalize_conditional_expr (enum tree_code code
, tree t
,
2260 tsubst_flags_t complain
)
2262 location_t loc
= cp_expr_loc_or_loc (t
, input_location
);
2264 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2265 the first operand is always the one to be used if both operands
2266 are equal, so we know what conditional expression this used to be. */
2267 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
2269 tree op0
= TREE_OPERAND (t
, 0);
2270 tree op1
= TREE_OPERAND (t
, 1);
2272 /* The following code is incorrect if either operand side-effects. */
2273 gcc_assert (!TREE_SIDE_EFFECTS (op0
)
2274 && !TREE_SIDE_EFFECTS (op1
));
2276 build_conditional_expr (loc
,
2277 build_x_binary_op (loc
,
2278 (TREE_CODE (t
) == MIN_EXPR
2279 ? LE_EXPR
: GE_EXPR
),
2280 op0
, TREE_CODE (op0
),
2281 op1
, TREE_CODE (op1
),
2284 cp_build_unary_op (code
, op0
, false, complain
),
2285 cp_build_unary_op (code
, op1
, false, complain
),
2290 build_conditional_expr (loc
, TREE_OPERAND (t
, 0),
2291 cp_build_unary_op (code
, TREE_OPERAND (t
, 1), false,
2293 cp_build_unary_op (code
, TREE_OPERAND (t
, 2), false,
2298 /* Given the TYPE of an anonymous union field inside T, return the
2299 FIELD_DECL for the field. If not found return NULL_TREE. Because
2300 anonymous unions can nest, we must also search all anonymous unions
2301 that are directly reachable. */
2304 lookup_anon_field (tree t
, tree type
)
2308 t
= TYPE_MAIN_VARIANT (t
);
2310 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
2312 if (TREE_STATIC (field
))
2314 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
2317 /* If we find it directly, return the field. */
2318 if (DECL_NAME (field
) == NULL_TREE
2319 && type
== TYPE_MAIN_VARIANT (TREE_TYPE (field
)))
2324 /* Otherwise, it could be nested, search harder. */
2325 if (DECL_NAME (field
) == NULL_TREE
2326 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2328 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
2336 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2337 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2338 non-NULL, it indicates the path to the base used to name MEMBER.
2339 If PRESERVE_REFERENCE is true, the expression returned will have
2340 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2341 returned will have the type referred to by the reference.
2343 This function does not perform access control; that is either done
2344 earlier by the parser when the name of MEMBER is resolved to MEMBER
2345 itself, or later when overload resolution selects one of the
2346 functions indicated by MEMBER. */
2349 build_class_member_access_expr (cp_expr object
, tree member
,
2350 tree access_path
, bool preserve_reference
,
2351 tsubst_flags_t complain
)
2355 tree result
= NULL_TREE
;
2356 tree using_decl
= NULL_TREE
;
2358 if (error_operand_p (object
) || error_operand_p (member
))
2359 return error_mark_node
;
2361 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2365 The type of the first expression shall be "class object" (of a
2367 object_type
= TREE_TYPE (object
);
2368 if (!currently_open_class (object_type
)
2369 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2370 return error_mark_node
;
2371 if (!CLASS_TYPE_P (object_type
))
2373 if (complain
& tf_error
)
2375 if (INDIRECT_TYPE_P (object_type
)
2376 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2377 error ("request for member %qD in %qE, which is of pointer "
2378 "type %qT (maybe you meant to use %<->%> ?)",
2379 member
, object
.get_value (), object_type
);
2381 error ("request for member %qD in %qE, which is of non-class "
2382 "type %qT", member
, object
.get_value (), object_type
);
2384 return error_mark_node
;
2387 /* The standard does not seem to actually say that MEMBER must be a
2388 member of OBJECT_TYPE. However, that is clearly what is
2390 if (DECL_P (member
))
2392 member_scope
= DECL_CLASS_CONTEXT (member
);
2393 if (!mark_used (member
, complain
) && !(complain
& tf_error
))
2394 return error_mark_node
;
2395 if (TREE_DEPRECATED (member
))
2396 warn_deprecated_use (member
, NULL_TREE
);
2399 member_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (member
));
2400 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2401 presently be the anonymous union. Go outwards until we find a
2402 type related to OBJECT_TYPE. */
2403 while ((ANON_AGGR_TYPE_P (member_scope
) || UNSCOPED_ENUM_P (member_scope
))
2404 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
2406 member_scope
= TYPE_CONTEXT (member_scope
);
2407 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
2409 if (complain
& tf_error
)
2411 if (TREE_CODE (member
) == FIELD_DECL
)
2412 error ("invalid use of nonstatic data member %qE", member
);
2414 error ("%qD is not a member of %qT", member
, object_type
);
2416 return error_mark_node
;
2419 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2420 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2421 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2423 tree temp
= unary_complex_lvalue (ADDR_EXPR
, object
);
2425 object
= cp_build_fold_indirect_ref (temp
);
2428 /* In [expr.ref], there is an explicit list of the valid choices for
2429 MEMBER. We check for each of those cases here. */
2432 /* A static data member. */
2434 mark_exp_read (object
);
2435 /* If OBJECT has side-effects, they are supposed to occur. */
2436 if (TREE_SIDE_EFFECTS (object
))
2437 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
2439 else if (TREE_CODE (member
) == FIELD_DECL
)
2441 /* A non-static data member. */
2446 if (INDIRECT_REF_P (object
))
2448 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object
, 0)));
2450 null_object_p
= false;
2452 /* Convert OBJECT to the type of MEMBER. */
2453 if (!same_type_p (TYPE_MAIN_VARIANT (object_type
),
2454 TYPE_MAIN_VARIANT (member_scope
)))
2459 binfo
= lookup_base (access_path
? access_path
: object_type
,
2460 member_scope
, ba_unique
, &kind
, complain
);
2461 if (binfo
== error_mark_node
)
2462 return error_mark_node
;
2464 /* It is invalid to try to get to a virtual base of a
2465 NULL object. The most common cause is invalid use of
2467 if (null_object_p
&& kind
== bk_via_virtual
)
2469 if (complain
& tf_error
)
2471 error ("invalid access to non-static data member %qD in "
2472 "virtual base of NULL object", member
);
2474 return error_mark_node
;
2477 /* Convert to the base. */
2478 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
2479 /*nonnull=*/1, complain
);
2480 /* If we found the base successfully then we should be able
2481 to convert to it successfully. */
2482 gcc_assert (object
!= error_mark_node
);
2485 /* If MEMBER is from an anonymous aggregate, we have converted
2486 OBJECT so that it refers to the class containing the
2487 anonymous union. Generate a reference to the anonymous union
2488 itself, and recur to find MEMBER. */
2489 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
))
2490 /* When this code is called from build_field_call, the
2491 object already has the type of the anonymous union.
2492 That is because the COMPONENT_REF was already
2493 constructed, and was then disassembled before calling
2494 build_field_call. After the function-call code is
2495 cleaned up, this waste can be eliminated. */
2496 && (!same_type_ignoring_top_level_qualifiers_p
2497 (TREE_TYPE (object
), DECL_CONTEXT (member
))))
2499 tree anonymous_union
;
2501 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
2502 DECL_CONTEXT (member
));
2503 object
= build_class_member_access_expr (object
,
2505 /*access_path=*/NULL_TREE
,
2510 /* Compute the type of the field, as described in [expr.ref]. */
2511 type_quals
= TYPE_UNQUALIFIED
;
2512 member_type
= TREE_TYPE (member
);
2513 if (!TYPE_REF_P (member_type
))
2515 type_quals
= (cp_type_quals (member_type
)
2516 | cp_type_quals (object_type
));
2518 /* A field is const (volatile) if the enclosing object, or the
2519 field itself, is const (volatile). But, a mutable field is
2520 not const, even within a const object. */
2521 if (DECL_MUTABLE_P (member
))
2522 type_quals
&= ~TYPE_QUAL_CONST
;
2523 member_type
= cp_build_qualified_type (member_type
, type_quals
);
2526 result
= build3_loc (input_location
, COMPONENT_REF
, member_type
,
2527 object
, member
, NULL_TREE
);
2529 /* Mark the expression const or volatile, as appropriate. Even
2530 though we've dealt with the type above, we still have to mark the
2531 expression itself. */
2532 if (type_quals
& TYPE_QUAL_CONST
)
2533 TREE_READONLY (result
) = 1;
2534 if (type_quals
& TYPE_QUAL_VOLATILE
)
2535 TREE_THIS_VOLATILE (result
) = 1;
2537 else if (BASELINK_P (member
))
2539 /* The member is a (possibly overloaded) member function. */
2543 /* If the MEMBER is exactly one static member function, then we
2544 know the type of the expression. Otherwise, we must wait
2545 until overload resolution has been performed. */
2546 functions
= BASELINK_FUNCTIONS (member
);
2547 if (TREE_CODE (functions
) == FUNCTION_DECL
2548 && DECL_STATIC_FUNCTION_P (functions
))
2549 type
= TREE_TYPE (functions
);
2551 type
= unknown_type_node
;
2552 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2553 base. That will happen when the function is called. */
2554 result
= build3 (COMPONENT_REF
, type
, object
, member
, NULL_TREE
);
2556 else if (TREE_CODE (member
) == CONST_DECL
)
2558 /* The member is an enumerator. */
2560 /* If OBJECT has side-effects, they are supposed to occur. */
2561 if (TREE_SIDE_EFFECTS (object
))
2562 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
2565 else if ((using_decl
= strip_using_decl (member
)) != member
)
2566 result
= build_class_member_access_expr (object
,
2568 access_path
, preserve_reference
,
2572 if (complain
& tf_error
)
2573 error ("invalid use of %qD", member
);
2574 return error_mark_node
;
2577 if (!preserve_reference
)
2580 If E2 is declared to have type "reference to T", then ... the
2581 type of E1.E2 is T. */
2582 result
= convert_from_reference (result
);
2587 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2588 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2591 lookup_destructor (tree object
, tree scope
, tree dtor_name
,
2592 tsubst_flags_t complain
)
2594 tree object_type
= TREE_TYPE (object
);
2595 tree dtor_type
= TREE_OPERAND (dtor_name
, 0);
2598 /* We've already complained about this destructor. */
2599 if (dtor_type
== error_mark_node
)
2600 return error_mark_node
;
2602 if (scope
&& !check_dtor_name (scope
, dtor_type
))
2604 if (complain
& tf_error
)
2605 error ("qualified type %qT does not match destructor name ~%qT",
2607 return error_mark_node
;
2609 if (is_auto (dtor_type
))
2610 dtor_type
= object_type
;
2611 else if (identifier_p (dtor_type
))
2613 /* In a template, names we can't find a match for are still accepted
2614 destructor names, and we check them here. */
2615 if (check_dtor_name (object_type
, dtor_type
))
2616 dtor_type
= object_type
;
2619 if (complain
& tf_error
)
2620 error ("object type %qT does not match destructor name ~%qT",
2621 object_type
, dtor_type
);
2622 return error_mark_node
;
2626 else if (!DERIVED_FROM_P (dtor_type
, TYPE_MAIN_VARIANT (object_type
)))
2628 if (complain
& tf_error
)
2629 error ("the type being destroyed is %qT, but the destructor "
2630 "refers to %qT", TYPE_MAIN_VARIANT (object_type
), dtor_type
);
2631 return error_mark_node
;
2633 expr
= lookup_member (dtor_type
, complete_dtor_identifier
,
2634 /*protect=*/1, /*want_type=*/false,
2635 tf_warning_or_error
);
2638 if (complain
& tf_error
)
2639 cxx_incomplete_type_error (dtor_name
, dtor_type
);
2640 return error_mark_node
;
2642 expr
= (adjust_result_of_qualified_name_lookup
2643 (expr
, dtor_type
, object_type
));
2644 if (scope
== NULL_TREE
)
2645 /* We need to call adjust_result_of_qualified_name_lookup in case the
2646 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2647 that we still get virtual function binding. */
2648 BASELINK_QUALIFIED_P (expr
) = false;
2652 /* An expression of the form "A::template B" has been resolved to
2653 DECL. Issue a diagnostic if B is not a template or template
2657 check_template_keyword (tree decl
)
2659 /* The standard says:
2663 If a name prefixed by the keyword template is not a member
2664 template, the program is ill-formed.
2666 DR 228 removed the restriction that the template be a member
2669 DR 96, if accepted would add the further restriction that explicit
2670 template arguments must be provided if the template keyword is
2671 used, but, as of 2005-10-16, that DR is still in "drafting". If
2672 this DR is accepted, then the semantic checks here can be
2673 simplified, as the entity named must in fact be a template
2674 specialization, rather than, as at present, a set of overloaded
2675 functions containing at least one template function. */
2676 if (TREE_CODE (decl
) != TEMPLATE_DECL
2677 && TREE_CODE (decl
) != TEMPLATE_ID_EXPR
)
2681 if (DECL_USE_TEMPLATE (decl
)
2682 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2685 permerror (input_location
, "%qD is not a template", decl
);
2687 else if (!is_overloaded_fn (decl
))
2688 permerror (input_location
, "%qD is not a template", decl
);
2693 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
2694 !found
&& iter
; ++iter
)
2697 if (TREE_CODE (fn
) == TEMPLATE_DECL
2698 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
2699 || (TREE_CODE (fn
) == FUNCTION_DECL
2700 && DECL_USE_TEMPLATE (fn
)
2701 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn
))))
2705 permerror (input_location
, "%qD is not a template", decl
);
2710 /* Record that an access failure occurred on BASETYPE_PATH attempting
2711 to access DECL, where DIAG_DECL should be used for diagnostics. */
2714 access_failure_info::record_access_failure (tree basetype_path
,
2715 tree decl
, tree diag_decl
)
2717 m_was_inaccessible
= true;
2718 m_basetype_path
= basetype_path
;
2720 m_diag_decl
= diag_decl
;
2723 /* If an access failure was recorded, then attempt to locate an
2724 accessor function for the pertinent field.
2725 Otherwise, return NULL_TREE. */
2728 access_failure_info::get_any_accessor (bool const_p
) const
2730 if (!was_inaccessible_p ())
2734 = locate_field_accessor (m_basetype_path
, m_diag_decl
, const_p
);
2738 /* The accessor must itself be accessible for it to be a reasonable
2740 if (!accessible_p (m_basetype_path
, accessor
, true))
2746 /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
2747 replacing the primary location in RICHLOC with "accessor()". */
2750 access_failure_info::add_fixit_hint (rich_location
*richloc
,
2754 pp_printf (&pp
, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor_decl
)));
2755 richloc
->add_fixit_replace (pp_formatted_text (&pp
));
2758 /* If an access failure was recorded, then attempt to locate an
2759 accessor function for the pertinent field, and if one is
2760 available, add a note and fix-it hint suggesting using it. */
2763 access_failure_info::maybe_suggest_accessor (bool const_p
) const
2765 tree accessor
= get_any_accessor (const_p
);
2766 if (accessor
== NULL_TREE
)
2768 rich_location
richloc (line_table
, input_location
);
2769 add_fixit_hint (&richloc
, accessor
);
2770 inform (&richloc
, "field %q#D can be accessed via %q#D",
2771 m_diag_decl
, accessor
);
2774 /* Subroutine of finish_class_member_access_expr.
2775 Issue an error about NAME not being a member of ACCESS_PATH (or
2776 OBJECT_TYPE), potentially providing a fix-it hint for misspelled
2780 complain_about_unrecognized_member (tree access_path
, tree name
,
2783 /* Attempt to provide a hint about misspelled names. */
2784 tree guessed_id
= lookup_member_fuzzy (access_path
, name
,
2785 /*want_type=*/false);
2786 if (guessed_id
== NULL_TREE
)
2789 error ("%q#T has no member named %qE",
2790 TREE_CODE (access_path
) == TREE_BINFO
2791 ? TREE_TYPE (access_path
) : object_type
, name
);
2795 location_t bogus_component_loc
= input_location
;
2796 gcc_rich_location
rich_loc (bogus_component_loc
);
2798 /* Check that the guessed name is accessible along access_path. */
2799 access_failure_info afi
;
2800 lookup_member (access_path
, guessed_id
, /*protect=*/1,
2801 /*want_type=*/false, /*complain=*/false,
2803 if (afi
.was_inaccessible_p ())
2805 tree accessor
= afi
.get_any_accessor (TYPE_READONLY (object_type
));
2808 /* The guessed name isn't directly accessible, but can be accessed
2809 via an accessor member function. */
2810 afi
.add_fixit_hint (&rich_loc
, accessor
);
2811 error_at (&rich_loc
,
2812 "%q#T has no member named %qE;"
2813 " did you mean %q#D? (accessible via %q#D)",
2814 TREE_CODE (access_path
) == TREE_BINFO
2815 ? TREE_TYPE (access_path
) : object_type
,
2816 name
, afi
.get_diag_decl (), accessor
);
2820 /* The guessed name isn't directly accessible, and no accessor
2821 member function could be found. */
2822 error_at (&rich_loc
,
2823 "%q#T has no member named %qE;"
2824 " did you mean %q#D? (not accessible from this context)",
2825 TREE_CODE (access_path
) == TREE_BINFO
2826 ? TREE_TYPE (access_path
) : object_type
,
2827 name
, afi
.get_diag_decl ());
2828 complain_about_access (afi
.get_decl (), afi
.get_diag_decl (), false);
2833 /* The guessed name is directly accessible; suggest it. */
2834 rich_loc
.add_fixit_misspelled_id (bogus_component_loc
,
2836 error_at (&rich_loc
,
2837 "%q#T has no member named %qE;"
2838 " did you mean %qE?",
2839 TREE_CODE (access_path
) == TREE_BINFO
2840 ? TREE_TYPE (access_path
) : object_type
,
2845 /* This function is called by the parser to process a class member
2846 access expression of the form OBJECT.NAME. NAME is a node used by
2847 the parser to represent a name; it is not yet a DECL. It may,
2848 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2849 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2850 there is no reason to do the lookup twice, so the parser keeps the
2851 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2852 be a template via the use of the "A::template B" syntax. */
2855 finish_class_member_access_expr (cp_expr object
, tree name
, bool template_p
,
2856 tsubst_flags_t complain
)
2861 tree access_path
= NULL_TREE
;
2862 tree orig_object
= object
;
2863 tree orig_name
= name
;
2865 if (object
== error_mark_node
|| name
== error_mark_node
)
2866 return error_mark_node
;
2868 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2869 if (!objc_is_public (object
, name
))
2870 return error_mark_node
;
2872 object_type
= TREE_TYPE (object
);
2874 if (processing_template_decl
)
2876 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2877 type_dependent_object_expression_p (object
)
2878 /* If NAME is "f<args>", where either 'f' or 'args' is
2879 dependent, then the expression is dependent. */
2880 || (TREE_CODE (name
) == TEMPLATE_ID_EXPR
2881 && dependent_template_id_p (TREE_OPERAND (name
, 0),
2882 TREE_OPERAND (name
, 1)))
2883 /* If NAME is "T::X" where "T" is dependent, then the
2884 expression is dependent. */
2885 || (TREE_CODE (name
) == SCOPE_REF
2886 && TYPE_P (TREE_OPERAND (name
, 0))
2887 && dependent_scope_p (TREE_OPERAND (name
, 0))))
2890 return build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
,
2891 orig_object
, orig_name
, NULL_TREE
);
2893 object
= build_non_dependent_expr (object
);
2895 else if (c_dialect_objc ()
2896 && identifier_p (name
)
2897 && (expr
= objc_maybe_build_component_ref (object
, name
)))
2902 The type of the first expression shall be "class object" (of a
2904 if (!currently_open_class (object_type
)
2905 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2906 return error_mark_node
;
2907 if (!CLASS_TYPE_P (object_type
))
2909 if (complain
& tf_error
)
2911 if (INDIRECT_TYPE_P (object_type
)
2912 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2913 error ("request for member %qD in %qE, which is of pointer "
2914 "type %qT (maybe you meant to use %<->%> ?)",
2915 name
, object
.get_value (), object_type
);
2917 error ("request for member %qD in %qE, which is of non-class "
2918 "type %qT", name
, object
.get_value (), object_type
);
2920 return error_mark_node
;
2923 if (BASELINK_P (name
))
2924 /* A member function that has already been looked up. */
2928 bool is_template_id
= false;
2929 tree template_args
= NULL_TREE
;
2930 tree scope
= NULL_TREE
;
2932 access_path
= object_type
;
2934 if (TREE_CODE (name
) == SCOPE_REF
)
2936 /* A qualified name. The qualifying class or namespace `S'
2937 has already been looked up; it is either a TYPE or a
2939 scope
= TREE_OPERAND (name
, 0);
2940 name
= TREE_OPERAND (name
, 1);
2942 /* If SCOPE is a namespace, then the qualified name does not
2943 name a member of OBJECT_TYPE. */
2944 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
2946 if (complain
& tf_error
)
2947 error ("%<%D::%D%> is not a member of %qT",
2948 scope
, name
, object_type
);
2949 return error_mark_node
;
2953 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
2955 is_template_id
= true;
2956 template_args
= TREE_OPERAND (name
, 1);
2957 name
= TREE_OPERAND (name
, 0);
2959 if (!identifier_p (name
))
2960 name
= OVL_NAME (name
);
2965 if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
2967 gcc_assert (!is_template_id
);
2968 /* Looking up a member enumerator (c++/56793). */
2969 if (!TYPE_CLASS_SCOPE_P (scope
)
2970 || !DERIVED_FROM_P (TYPE_CONTEXT (scope
), object_type
))
2972 if (complain
& tf_error
)
2973 error ("%<%D::%D%> is not a member of %qT",
2974 scope
, name
, object_type
);
2975 return error_mark_node
;
2977 tree val
= lookup_enumerator (scope
, name
);
2980 if (complain
& tf_error
)
2981 error ("%qD is not a member of %qD",
2983 return error_mark_node
;
2986 if (TREE_SIDE_EFFECTS (object
))
2987 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
), object
, val
);
2991 gcc_assert (CLASS_TYPE_P (scope
));
2992 gcc_assert (identifier_p (name
) || TREE_CODE (name
) == BIT_NOT_EXPR
);
2994 if (constructor_name_p (name
, scope
))
2996 if (complain
& tf_error
)
2997 error ("cannot call constructor %<%T::%D%> directly",
2999 return error_mark_node
;
3002 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3003 access_path
= lookup_base (object_type
, scope
, ba_check
,
3005 if (access_path
== error_mark_node
)
3006 return error_mark_node
;
3009 if (any_dependent_bases_p (object_type
))
3011 if (complain
& tf_error
)
3012 error ("%qT is not a base of %qT", scope
, object_type
);
3013 return error_mark_node
;
3017 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
3019 if (dependent_type_p (object_type
))
3020 /* The destructor isn't declared yet. */
3022 member
= lookup_destructor (object
, scope
, name
, complain
);
3026 /* Look up the member. */
3027 access_failure_info afi
;
3028 member
= lookup_member (access_path
, name
, /*protect=*/1,
3029 /*want_type=*/false, complain
,
3031 afi
.maybe_suggest_accessor (TYPE_READONLY (object_type
));
3032 if (member
== NULL_TREE
)
3034 if (dependent_type_p (object_type
))
3035 /* Try again at instantiation time. */
3037 if (complain
& tf_error
)
3038 complain_about_unrecognized_member (access_path
, name
,
3040 return error_mark_node
;
3042 if (member
== error_mark_node
)
3043 return error_mark_node
;
3045 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member
)))
3046 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3047 wrong, so don't use it. */
3049 if (TREE_CODE (member
) == USING_DECL
&& DECL_DEPENDENT_P (member
))
3055 tree templ
= member
;
3057 if (BASELINK_P (templ
))
3058 member
= lookup_template_function (templ
, template_args
);
3059 else if (variable_template_p (templ
))
3060 member
= (lookup_and_finish_template_variable
3061 (templ
, template_args
, complain
));
3064 if (complain
& tf_error
)
3065 error ("%qD is not a member template function", name
);
3066 return error_mark_node
;
3071 if (TREE_DEPRECATED (member
))
3072 warn_deprecated_use (member
, NULL_TREE
);
3075 check_template_keyword (member
);
3077 expr
= build_class_member_access_expr (object
, member
, access_path
,
3078 /*preserve_reference=*/false,
3080 if (processing_template_decl
&& expr
!= error_mark_node
)
3082 if (BASELINK_P (member
))
3084 if (TREE_CODE (orig_name
) == SCOPE_REF
)
3085 BASELINK_QUALIFIED_P (member
) = 1;
3088 return build_min_non_dep (COMPONENT_REF
, expr
,
3089 orig_object
, orig_name
,
3096 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3100 build_simple_component_ref (tree object
, tree member
)
3102 tree type
= cp_build_qualified_type (TREE_TYPE (member
),
3103 cp_type_quals (TREE_TYPE (object
)));
3104 return build3_loc (input_location
,
3105 COMPONENT_REF
, type
,
3106 object
, member
, NULL_TREE
);
3109 /* Return an expression for the MEMBER_NAME field in the internal
3110 representation of PTRMEM, a pointer-to-member function. (Each
3111 pointer-to-member function type gets its own RECORD_TYPE so it is
3112 more convenient to access the fields by name than by FIELD_DECL.)
3113 This routine converts the NAME to a FIELD_DECL and then creates the
3114 node for the complete expression. */
3117 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
3122 if (TREE_CODE (ptrmem
) == CONSTRUCTOR
)
3126 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ptrmem
),
3128 if (index
&& DECL_P (index
) && DECL_NAME (index
) == member_name
)
3133 /* This code is a stripped down version of
3134 build_class_member_access_expr. It does not work to use that
3135 routine directly because it expects the object to be of class
3137 ptrmem_type
= TREE_TYPE (ptrmem
);
3138 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
));
3139 for (member
= TYPE_FIELDS (ptrmem_type
); member
;
3140 member
= DECL_CHAIN (member
))
3141 if (DECL_NAME (member
) == member_name
)
3143 tree res
= build_simple_component_ref (ptrmem
, member
);
3145 TREE_NO_WARNING (res
) = 1;
3149 /* Given an expression PTR for a pointer, return an expression
3150 for the value pointed to.
3151 ERRORSTRING is the name of the operator to appear in error messages.
3153 This function may need to overload OPERATOR_FNNAME.
3154 Must also handle REFERENCE_TYPEs for C++. */
3157 build_x_indirect_ref (location_t loc
, tree expr
, ref_operator errorstring
,
3158 tsubst_flags_t complain
)
3160 tree orig_expr
= expr
;
3162 tree overload
= NULL_TREE
;
3164 if (processing_template_decl
)
3166 /* Retain the type if we know the operand is a pointer. */
3167 if (TREE_TYPE (expr
) && INDIRECT_TYPE_P (TREE_TYPE (expr
)))
3168 return build_min (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
3169 if (type_dependent_expression_p (expr
))
3170 return build_min_nt_loc (loc
, INDIRECT_REF
, expr
);
3171 expr
= build_non_dependent_expr (expr
);
3174 rval
= build_new_op (loc
, INDIRECT_REF
, LOOKUP_NORMAL
, expr
,
3175 NULL_TREE
, NULL_TREE
, &overload
, complain
);
3177 rval
= cp_build_indirect_ref (expr
, errorstring
, complain
);
3179 if (processing_template_decl
&& rval
!= error_mark_node
)
3181 if (overload
!= NULL_TREE
)
3182 return (build_min_non_dep_op_overload
3183 (INDIRECT_REF
, rval
, overload
, orig_expr
));
3185 return build_min_non_dep (INDIRECT_REF
, rval
, orig_expr
);
3191 /* The implementation of the above, and of indirection implied by other
3192 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3195 cp_build_indirect_ref_1 (tree ptr
, ref_operator errorstring
,
3196 tsubst_flags_t complain
, bool do_fold
)
3200 /* RO_NULL should only be used with the folding entry points below, not
3201 cp_build_indirect_ref. */
3202 gcc_checking_assert (errorstring
!= RO_NULL
|| do_fold
);
3204 if (ptr
== current_class_ptr
3205 || (TREE_CODE (ptr
) == NOP_EXPR
3206 && TREE_OPERAND (ptr
, 0) == current_class_ptr
3207 && (same_type_ignoring_top_level_qualifiers_p
3208 (TREE_TYPE (ptr
), TREE_TYPE (current_class_ptr
)))))
3209 return current_class_ref
;
3211 pointer
= (TYPE_REF_P (TREE_TYPE (ptr
))
3212 ? ptr
: decay_conversion (ptr
, complain
));
3213 if (pointer
== error_mark_node
)
3214 return error_mark_node
;
3216 type
= TREE_TYPE (pointer
);
3218 if (INDIRECT_TYPE_P (type
))
3222 If the type of the expression is "pointer to T," the type
3223 of the result is "T." */
3224 tree t
= TREE_TYPE (type
);
3226 if ((CONVERT_EXPR_P (ptr
)
3227 || TREE_CODE (ptr
) == VIEW_CONVERT_EXPR
)
3228 && (!CLASS_TYPE_P (t
) || !CLASSTYPE_EMPTY_P (t
)))
3230 /* If a warning is issued, mark it to avoid duplicates from
3231 the backend. This only needs to be done at
3232 warn_strict_aliasing > 2. */
3233 if (warn_strict_aliasing
> 2)
3234 if (strict_aliasing_warning (EXPR_LOCATION (ptr
),
3235 type
, TREE_OPERAND (ptr
, 0)))
3236 TREE_NO_WARNING (ptr
) = 1;
3239 if (VOID_TYPE_P (t
))
3241 /* A pointer to incomplete type (other than cv void) can be
3242 dereferenced [expr.unary.op]/1 */
3243 if (complain
& tf_error
)
3244 error ("%qT is not a pointer-to-object type", type
);
3245 return error_mark_node
;
3247 else if (do_fold
&& TREE_CODE (pointer
) == ADDR_EXPR
3248 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
3249 /* The POINTER was something like `&x'. We simplify `*&x' to
3251 return TREE_OPERAND (pointer
, 0);
3254 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
3256 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3257 so that we get the proper error message if the result is used
3258 to assign to. Also, &* is supposed to be a no-op. */
3259 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
3260 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
3261 TREE_SIDE_EFFECTS (ref
)
3262 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
));
3266 else if (!(complain
& tf_error
))
3267 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3269 /* `pointer' won't be an error_mark_node if we were given a
3270 pointer to member, so it's cool to check for this here. */
3271 else if (TYPE_PTRMEM_P (type
))
3272 switch (errorstring
)
3274 case RO_ARRAY_INDEXING
:
3275 error ("invalid use of array indexing on pointer to member");
3278 error ("invalid use of unary %<*%> on pointer to member");
3280 case RO_IMPLICIT_CONVERSION
:
3281 error ("invalid use of implicit conversion on pointer to member");
3284 error ("left hand operand of %<->*%> must be a pointer to class, "
3285 "but is a pointer to member of type %qT", type
);
3290 else if (pointer
!= error_mark_node
)
3291 invalid_indirection_error (input_location
, type
, errorstring
);
3293 return error_mark_node
;
3296 /* Entry point used by c-common, which expects folding. */
3299 build_indirect_ref (location_t
/*loc*/,
3300 tree ptr
, ref_operator errorstring
)
3302 return cp_build_indirect_ref_1 (ptr
, errorstring
, tf_warning_or_error
, true);
3305 /* Entry point used by internal indirection needs that don't correspond to any
3306 syntactic construct. */
3309 cp_build_fold_indirect_ref (tree pointer
)
3311 return cp_build_indirect_ref_1 (pointer
, RO_NULL
, tf_warning_or_error
, true);
3314 /* Entry point used by indirection needs that correspond to some syntactic
3318 cp_build_indirect_ref (tree ptr
, ref_operator errorstring
,
3319 tsubst_flags_t complain
)
3321 return cp_build_indirect_ref_1 (ptr
, errorstring
, complain
, false);
3324 /* This handles expressions of the form "a[i]", which denotes
3327 This is logically equivalent in C to *(a+i), but we may do it differently.
3328 If A is a variable or a member, we generate a primitive ARRAY_REF.
3329 This avoids forcing the array out of registers, and can work on
3330 arrays that are not lvalues (for example, members of structures returned
3333 If INDEX is of some user-defined type, it must be converted to
3334 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3335 will inherit the type of the array, which will be some pointer type.
3337 LOC is the location to use in building the array reference. */
3340 cp_build_array_ref (location_t loc
, tree array
, tree idx
,
3341 tsubst_flags_t complain
)
3347 if (complain
& tf_error
)
3348 error_at (loc
, "subscript missing in array reference");
3349 return error_mark_node
;
3352 if (TREE_TYPE (array
) == error_mark_node
3353 || TREE_TYPE (idx
) == error_mark_node
)
3354 return error_mark_node
;
3356 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3358 switch (TREE_CODE (array
))
3362 tree value
= cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3364 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3365 TREE_OPERAND (array
, 0), value
);
3366 SET_EXPR_LOCATION (ret
, loc
);
3371 ret
= build_conditional_expr
3372 (loc
, TREE_OPERAND (array
, 0),
3373 cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3375 cp_build_array_ref (loc
, TREE_OPERAND (array
, 2), idx
,
3378 protected_set_expr_location (ret
, loc
);
3385 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, idx
);
3387 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
3391 warn_array_subscript_with_type_char (loc
, idx
);
3393 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx
)))
3395 if (complain
& tf_error
)
3396 error_at (loc
, "array subscript is not an integer");
3397 return error_mark_node
;
3400 /* Apply integral promotions *after* noticing character types.
3401 (It is unclear why we do these promotions -- the standard
3402 does not say that we should. In fact, the natural thing would
3403 seem to be to convert IDX to ptrdiff_t; we're performing
3404 pointer arithmetic.) */
3405 idx
= cp_perform_integral_promotions (idx
, complain
);
3407 /* An array that is indexed by a non-constant
3408 cannot be stored in a register; we must be able to do
3409 address arithmetic on its address.
3410 Likewise an array of elements of variable size. */
3411 if (TREE_CODE (idx
) != INTEGER_CST
3412 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
3413 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
3416 if (!cxx_mark_addressable (array
, true))
3417 return error_mark_node
;
3420 /* An array that is indexed by a constant value which is not within
3421 the array bounds cannot be stored in a register either; because we
3422 would get a crash in store_bit_field/extract_bit_field when trying
3423 to access a non-existent part of the register. */
3424 if (TREE_CODE (idx
) == INTEGER_CST
3425 && TYPE_DOMAIN (TREE_TYPE (array
))
3426 && ! int_fits_type_p (idx
, TYPE_DOMAIN (TREE_TYPE (array
))))
3428 if (!cxx_mark_addressable (array
))
3429 return error_mark_node
;
3432 /* Note in C++ it is valid to subscript a `register' array, since
3433 it is valid to take the address of something with that
3434 storage specification. */
3438 while (TREE_CODE (foo
) == COMPONENT_REF
)
3439 foo
= TREE_OPERAND (foo
, 0);
3440 if (VAR_P (foo
) && DECL_REGISTER (foo
)
3441 && (complain
& tf_warning
))
3442 warning_at (loc
, OPT_Wextra
,
3443 "subscripting array declared %<register%>");
3446 type
= TREE_TYPE (TREE_TYPE (array
));
3447 rval
= build4 (ARRAY_REF
, type
, array
, idx
, NULL_TREE
, NULL_TREE
);
3448 /* Array ref is const/volatile if the array elements are
3449 or if the array is.. */
3450 TREE_READONLY (rval
)
3451 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
3452 TREE_SIDE_EFFECTS (rval
)
3453 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
3454 TREE_THIS_VOLATILE (rval
)
3455 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
3456 ret
= require_complete_type_sfinae (rval
, complain
);
3457 protected_set_expr_location (ret
, loc
);
3459 ret
= non_lvalue_loc (loc
, ret
);
3464 tree ar
= cp_default_conversion (array
, complain
);
3465 tree ind
= cp_default_conversion (idx
, complain
);
3467 /* Put the integer in IND to simplify error checking. */
3468 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
3469 std::swap (ar
, ind
);
3471 if (ar
== error_mark_node
|| ind
== error_mark_node
)
3472 return error_mark_node
;
3474 if (!TYPE_PTR_P (TREE_TYPE (ar
)))
3476 if (complain
& tf_error
)
3477 error_at (loc
, "subscripted value is neither array nor pointer");
3478 return error_mark_node
;
3480 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
3482 if (complain
& tf_error
)
3483 error_at (loc
, "array subscript is not an integer");
3484 return error_mark_node
;
3487 warn_array_subscript_with_type_char (loc
, idx
);
3489 ret
= cp_build_indirect_ref (cp_build_binary_op (input_location
,
3494 protected_set_expr_location (ret
, loc
);
3496 ret
= non_lvalue_loc (loc
, ret
);
3501 /* Entry point for Obj-C++. */
3504 build_array_ref (location_t loc
, tree array
, tree idx
)
3506 return cp_build_array_ref (loc
, array
, idx
, tf_warning_or_error
);
3509 /* Resolve a pointer to member function. INSTANCE is the object
3510 instance to use, if the member points to a virtual member.
3512 This used to avoid checking for virtual functions if basetype
3513 has no virtual functions, according to an earlier ANSI draft.
3514 With the final ISO C++ rules, such an optimization is
3515 incorrect: A pointer to a derived member can be static_cast
3516 to pointer-to-base-member, as long as the dynamic object
3517 later has the right member. So now we only do this optimization
3518 when we know the dynamic type of the object. */
3521 get_member_function_from_ptrfunc (tree
*instance_ptrptr
, tree function
,
3522 tsubst_flags_t complain
)
3524 if (TREE_CODE (function
) == OFFSET_REF
)
3525 function
= TREE_OPERAND (function
, 1);
3527 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
3529 tree idx
, delta
, e1
, e2
, e3
, vtbl
;
3531 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
3532 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
3534 tree instance_ptr
= *instance_ptrptr
;
3535 tree instance_save_expr
= 0;
3536 if (instance_ptr
== error_mark_node
)
3538 if (TREE_CODE (function
) == PTRMEM_CST
)
3540 /* Extracting the function address from a pmf is only
3541 allowed with -Wno-pmf-conversions. It only works for
3543 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
), complain
);
3544 e1
= convert (fntype
, e1
);
3549 if (complain
& tf_error
)
3550 error ("object missing in use of %qE", function
);
3551 return error_mark_node
;
3555 /* True if we know that the dynamic type of the object doesn't have
3556 virtual functions, so we can assume the PFN field is a pointer. */
3557 nonvirtual
= (COMPLETE_TYPE_P (basetype
)
3558 && !TYPE_POLYMORPHIC_P (basetype
)
3559 && resolves_to_fixed_type_p (instance_ptr
, 0));
3561 /* If we don't really have an object (i.e. in an ill-formed
3562 conversion from PMF to pointer), we can't resolve virtual
3563 functions anyway. */
3564 if (!nonvirtual
&& is_dummy_object (instance_ptr
))
3567 if (TREE_SIDE_EFFECTS (instance_ptr
))
3568 instance_ptr
= instance_save_expr
= save_expr (instance_ptr
);
3570 if (TREE_SIDE_EFFECTS (function
))
3571 function
= save_expr (function
);
3573 /* Start by extracting all the information from the PMF itself. */
3574 e3
= pfn_from_ptrmemfunc (function
);
3575 delta
= delta_from_ptrmemfunc (function
);
3576 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
3577 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
3579 int flag_sanitize_save
;
3580 case ptrmemfunc_vbit_in_pfn
:
3581 e1
= cp_build_binary_op (input_location
,
3582 BIT_AND_EXPR
, idx
, integer_one_node
,
3584 idx
= cp_build_binary_op (input_location
,
3585 MINUS_EXPR
, idx
, integer_one_node
,
3587 if (idx
== error_mark_node
)
3588 return error_mark_node
;
3591 case ptrmemfunc_vbit_in_delta
:
3592 e1
= cp_build_binary_op (input_location
,
3593 BIT_AND_EXPR
, delta
, integer_one_node
,
3595 /* Don't instrument the RSHIFT_EXPR we're about to create because
3596 we're going to use DELTA number of times, and that wouldn't play
3597 well with SAVE_EXPRs therein. */
3598 flag_sanitize_save
= flag_sanitize
;
3600 delta
= cp_build_binary_op (input_location
,
3601 RSHIFT_EXPR
, delta
, integer_one_node
,
3603 flag_sanitize
= flag_sanitize_save
;
3604 if (delta
== error_mark_node
)
3605 return error_mark_node
;
3612 if (e1
== error_mark_node
)
3613 return error_mark_node
;
3615 /* Convert down to the right base before using the instance. A
3616 special case is that in a pointer to member of class C, C may
3617 be incomplete. In that case, the function will of course be
3618 a member of C, and no conversion is required. In fact,
3619 lookup_base will fail in that case, because incomplete
3620 classes do not have BINFOs. */
3621 if (!same_type_ignoring_top_level_qualifiers_p
3622 (basetype
, TREE_TYPE (TREE_TYPE (instance_ptr
))))
3624 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
3625 basetype
, ba_check
, NULL
, complain
);
3626 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
,
3628 if (instance_ptr
== error_mark_node
)
3629 return error_mark_node
;
3631 /* ...and then the delta in the PMF. */
3632 instance_ptr
= fold_build_pointer_plus (instance_ptr
, delta
);
3634 /* Hand back the adjusted 'this' argument to our caller. */
3635 *instance_ptrptr
= instance_ptr
;
3638 /* Now just return the pointer. */
3641 /* Next extract the vtable pointer from the object. */
3642 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
3644 vtbl
= cp_build_fold_indirect_ref (vtbl
);
3645 if (vtbl
== error_mark_node
)
3646 return error_mark_node
;
3648 /* Finally, extract the function pointer from the vtable. */
3649 e2
= fold_build_pointer_plus_loc (input_location
, vtbl
, idx
);
3650 e2
= cp_build_fold_indirect_ref (e2
);
3651 if (e2
== error_mark_node
)
3652 return error_mark_node
;
3653 TREE_CONSTANT (e2
) = 1;
3655 /* When using function descriptors, the address of the
3656 vtable entry is treated as a function pointer. */
3657 if (TARGET_VTABLE_USES_DESCRIPTORS
)
3658 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
3659 cp_build_addr_expr (e2
, complain
));
3661 e2
= fold_convert (TREE_TYPE (e3
), e2
);
3662 e1
= build_conditional_expr (input_location
, e1
, e2
, e3
, complain
);
3663 if (e1
== error_mark_node
)
3664 return error_mark_node
;
3666 /* Make sure this doesn't get evaluated first inside one of the
3667 branches of the COND_EXPR. */
3668 if (instance_save_expr
)
3669 e1
= build2 (COMPOUND_EXPR
, TREE_TYPE (e1
),
3670 instance_save_expr
, e1
);
3677 /* Used by the C-common bits. */
3679 build_function_call (location_t
/*loc*/,
3680 tree function
, tree params
)
3682 return cp_build_function_call (function
, params
, tf_warning_or_error
);
3685 /* Used by the C-common bits. */
3687 build_function_call_vec (location_t
/*loc*/, vec
<location_t
> /*arg_loc*/,
3688 tree function
, vec
<tree
, va_gc
> *params
,
3689 vec
<tree
, va_gc
> * /*origtypes*/)
3691 vec
<tree
, va_gc
> *orig_params
= params
;
3692 tree ret
= cp_build_function_call_vec (function
, ¶ms
,
3693 tf_warning_or_error
);
3695 /* cp_build_function_call_vec can reallocate PARAMS by adding
3696 default arguments. That should never happen here. Verify
3698 gcc_assert (params
== orig_params
);
3703 /* Build a function call using a tree list of arguments. */
3706 cp_build_function_call (tree function
, tree params
, tsubst_flags_t complain
)
3708 vec
<tree
, va_gc
> *vec
;
3711 vec
= make_tree_vector ();
3712 for (; params
!= NULL_TREE
; params
= TREE_CHAIN (params
))
3713 vec_safe_push (vec
, TREE_VALUE (params
));
3714 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3715 release_tree_vector (vec
);
3719 /* Build a function call using varargs. */
3722 cp_build_function_call_nary (tree function
, tsubst_flags_t complain
, ...)
3724 vec
<tree
, va_gc
> *vec
;
3728 vec
= make_tree_vector ();
3729 va_start (args
, complain
);
3730 for (t
= va_arg (args
, tree
); t
!= NULL_TREE
; t
= va_arg (args
, tree
))
3731 vec_safe_push (vec
, t
);
3733 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3734 release_tree_vector (vec
);
3738 /* Build a function call using a vector of arguments. PARAMS may be
3739 NULL if there are no parameters. This changes the contents of
3743 cp_build_function_call_vec (tree function
, vec
<tree
, va_gc
> **params
,
3744 tsubst_flags_t complain
)
3746 tree fntype
, fndecl
;
3748 tree original
= function
;
3752 vec
<tree
, va_gc
> *allocated
= NULL
;
3755 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3756 expressions, like those used for ObjC messenger dispatches. */
3757 if (params
!= NULL
&& !vec_safe_is_empty (*params
))
3758 function
= objc_rewrite_function_call (function
, (**params
)[0]);
3760 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3761 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3762 if (TREE_CODE (function
) == NOP_EXPR
3763 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
3764 function
= TREE_OPERAND (function
, 0);
3766 if (TREE_CODE (function
) == FUNCTION_DECL
)
3768 /* If the function is a non-template member function
3769 or a non-template friend, then we need to check the
3772 Note that if overload resolution failed with a single
3773 candidate this function will be used to explicitly diagnose
3774 the failure for the single call expression. The check is
3775 technically redundant since we also would have failed in
3776 add_function_candidate. */
3778 && (complain
& tf_error
)
3779 && !constraints_satisfied_p (function
))
3781 auto_diagnostic_group d
;
3782 error ("cannot call function %qD", function
);
3783 location_t loc
= DECL_SOURCE_LOCATION (function
);
3784 diagnose_constraints (loc
, function
, NULL_TREE
);
3785 return error_mark_node
;
3788 if (!mark_used (function
, complain
) && !(complain
& tf_error
))
3789 return error_mark_node
;
3792 /* Convert anything with function type to a pointer-to-function. */
3793 if (DECL_MAIN_P (function
))
3795 if (complain
& tf_error
)
3796 pedwarn (input_location
, OPT_Wpedantic
,
3797 "ISO C++ forbids calling %<::main%> from within program");
3799 return error_mark_node
;
3801 function
= build_addr_func (function
, complain
);
3807 function
= build_addr_func (function
, complain
);
3810 if (function
== error_mark_node
)
3811 return error_mark_node
;
3813 fntype
= TREE_TYPE (function
);
3815 if (TYPE_PTRMEMFUNC_P (fntype
))
3817 if (complain
& tf_error
)
3818 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3819 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3820 original
, original
);
3821 return error_mark_node
;
3824 is_method
= (TYPE_PTR_P (fntype
)
3825 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
3827 if (!(TYPE_PTRFN_P (fntype
)
3829 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
3831 if (complain
& tf_error
)
3833 if (!flag_diagnostics_show_caret
)
3834 error_at (input_location
,
3835 "%qE cannot be used as a function", original
);
3836 else if (DECL_P (original
))
3837 error_at (input_location
,
3838 "%qD cannot be used as a function", original
);
3840 error_at (input_location
,
3841 "expression cannot be used as a function");
3844 return error_mark_node
;
3847 /* fntype now gets the type of function pointed to. */
3848 fntype
= TREE_TYPE (fntype
);
3849 parm_types
= TYPE_ARG_TYPES (fntype
);
3853 allocated
= make_tree_vector ();
3854 params
= &allocated
;
3857 nargs
= convert_arguments (parm_types
, params
, fndecl
, LOOKUP_NORMAL
,
3860 return error_mark_node
;
3862 argarray
= (*params
)->address ();
3864 /* Check for errors in format strings and inappropriately
3866 bool warned_p
= check_function_arguments (input_location
, fndecl
, fntype
,
3867 nargs
, argarray
, NULL
);
3869 ret
= build_cxx_call (function
, nargs
, argarray
, complain
);
3873 tree c
= extract_call_expr (ret
);
3874 if (TREE_CODE (c
) == CALL_EXPR
)
3875 TREE_NO_WARNING (c
) = 1;
3878 if (allocated
!= NULL
)
3879 release_tree_vector (allocated
);
3884 /* Subroutine of convert_arguments.
3885 Print an error message about a wrong number of arguments. */
3888 error_args_num (location_t loc
, tree fndecl
, bool too_many_p
)
3892 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
3894 if (DECL_NAME (fndecl
) == NULL_TREE
3895 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
3898 ? G_("too many arguments to constructor %q#D")
3899 : G_("too few arguments to constructor %q#D"),
3904 ? G_("too many arguments to member function %q#D")
3905 : G_("too few arguments to member function %q#D"),
3911 ? G_("too many arguments to function %q#D")
3912 : G_("too few arguments to function %q#D"),
3914 if (!DECL_IS_BUILTIN (fndecl
))
3915 inform (DECL_SOURCE_LOCATION (fndecl
), "declared here");
3919 if (c_dialect_objc () && objc_message_selector ())
3922 ? G_("too many arguments to method %q#D")
3923 : G_("too few arguments to method %q#D"),
3924 objc_message_selector ());
3926 error_at (loc
, too_many_p
? G_("too many arguments to function")
3927 : G_("too few arguments to function"));
3931 /* Convert the actual parameter expressions in the list VALUES to the
3932 types in the list TYPELIST. The converted expressions are stored
3933 back in the VALUES vector.
3934 If parmdecls is exhausted, or when an element has NULL as its type,
3935 perform the default conversions.
3937 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3939 This is also where warnings about wrong number of args are generated.
3941 Returns the actual number of arguments processed (which might be less
3942 than the length of the vector), or -1 on error.
3944 In C++, unspecified trailing parameters can be filled in with their
3945 default arguments, if such were specified. Do so here. */
3948 convert_arguments (tree typelist
, vec
<tree
, va_gc
> **values
, tree fndecl
,
3949 int flags
, tsubst_flags_t complain
)
3954 /* Argument passing is always copy-initialization. */
3955 flags
|= LOOKUP_ONLYCONVERTING
;
3957 for (i
= 0, typetail
= typelist
;
3958 i
< vec_safe_length (*values
);
3961 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3962 tree val
= (**values
)[i
];
3964 if (val
== error_mark_node
|| type
== error_mark_node
)
3967 if (type
== void_type_node
)
3969 if (complain
& tf_error
)
3971 error_args_num (input_location
, fndecl
, /*too_many_p=*/true);
3978 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3979 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3980 if (TREE_CODE (val
) == NOP_EXPR
3981 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
3982 && (type
== 0 || !TYPE_REF_P (type
)))
3983 val
= TREE_OPERAND (val
, 0);
3985 if (type
== 0 || !TYPE_REF_P (type
))
3987 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
3988 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
3989 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
3990 val
= decay_conversion (val
, complain
);
3993 if (val
== error_mark_node
)
3998 /* Formal parm type is specified by a function prototype. */
4001 if (!COMPLETE_TYPE_P (complete_type (type
)))
4003 if (complain
& tf_error
)
4006 error ("parameter %P of %qD has incomplete type %qT",
4009 error ("parameter %P has incomplete type %qT", i
, type
);
4011 parmval
= error_mark_node
;
4015 parmval
= convert_for_initialization
4016 (NULL_TREE
, type
, val
, flags
,
4017 ICR_ARGPASS
, fndecl
, i
, complain
);
4018 parmval
= convert_for_arg_passing (type
, parmval
, complain
);
4021 if (parmval
== error_mark_node
)
4024 (**values
)[i
] = parmval
;
4028 if (fndecl
&& magic_varargs_p (fndecl
))
4029 /* Don't do ellipsis conversion for __built_in_constant_p
4030 as this will result in spurious errors for non-trivial
4032 val
= require_complete_type_sfinae (val
, complain
);
4034 val
= convert_arg_to_ellipsis (val
, complain
);
4036 (**values
)[i
] = val
;
4040 typetail
= TREE_CHAIN (typetail
);
4043 if (typetail
!= 0 && typetail
!= void_list_node
)
4045 /* See if there are default arguments that can be used. Because
4046 we hold default arguments in the FUNCTION_TYPE (which is so
4047 wrong), we can see default parameters here from deduced
4048 contexts (and via typeof) for indirect function calls.
4049 Fortunately we know whether we have a function decl to
4050 provide default arguments in a language conformant
4052 if (fndecl
&& TREE_PURPOSE (typetail
)
4053 && TREE_CODE (TREE_PURPOSE (typetail
)) != DEFAULT_ARG
)
4055 for (; typetail
!= void_list_node
; ++i
)
4057 /* After DR777, with explicit template args we can end up with a
4058 default argument followed by no default argument. */
4059 if (!TREE_PURPOSE (typetail
))
4062 = convert_default_arg (TREE_VALUE (typetail
),
4063 TREE_PURPOSE (typetail
),
4064 fndecl
, i
, complain
);
4066 if (parmval
== error_mark_node
)
4069 vec_safe_push (*values
, parmval
);
4070 typetail
= TREE_CHAIN (typetail
);
4071 /* ends with `...'. */
4072 if (typetail
== NULL_TREE
)
4077 if (typetail
&& typetail
!= void_list_node
)
4079 if (complain
& tf_error
)
4080 error_args_num (input_location
, fndecl
, /*too_many_p=*/false);
4088 /* Build a binary-operation expression, after performing default
4089 conversions on the operands. CODE is the kind of expression to
4090 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4091 are the tree codes which correspond to ARG1 and ARG2 when issuing
4092 warnings about possibly misplaced parentheses. They may differ
4093 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4094 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4095 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4096 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4097 ARG2_CODE as ERROR_MARK. */
4100 build_x_binary_op (location_t loc
, enum tree_code code
, tree arg1
,
4101 enum tree_code arg1_code
, tree arg2
,
4102 enum tree_code arg2_code
, tree
*overload_p
,
4103 tsubst_flags_t complain
)
4108 tree overload
= NULL_TREE
;
4113 if (processing_template_decl
)
4115 if (type_dependent_expression_p (arg1
)
4116 || type_dependent_expression_p (arg2
))
4117 return build_min_nt_loc (loc
, code
, arg1
, arg2
);
4118 arg1
= build_non_dependent_expr (arg1
);
4119 arg2
= build_non_dependent_expr (arg2
);
4122 if (code
== DOTSTAR_EXPR
)
4123 expr
= build_m_component_ref (arg1
, arg2
, complain
);
4125 expr
= build_new_op (loc
, code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
,
4126 &overload
, complain
);
4128 if (overload_p
!= NULL
)
4129 *overload_p
= overload
;
4131 /* Check for cases such as x+y<<z which users are likely to
4132 misinterpret. But don't warn about obj << x + y, since that is a
4133 common idiom for I/O. */
4134 if (warn_parentheses
4135 && (complain
& tf_warning
)
4136 && !processing_template_decl
4137 && !error_operand_p (arg1
)
4138 && !error_operand_p (arg2
)
4139 && (code
!= LSHIFT_EXPR
4140 || !CLASS_TYPE_P (TREE_TYPE (arg1
))))
4141 warn_about_parentheses (loc
, code
, arg1_code
, orig_arg1
,
4142 arg2_code
, orig_arg2
);
4144 if (processing_template_decl
&& expr
!= error_mark_node
)
4146 if (overload
!= NULL_TREE
)
4147 return (build_min_non_dep_op_overload
4148 (code
, expr
, overload
, orig_arg1
, orig_arg2
));
4150 return build_min_non_dep (code
, expr
, orig_arg1
, orig_arg2
);
4156 /* Build and return an ARRAY_REF expression. */
4159 build_x_array_ref (location_t loc
, tree arg1
, tree arg2
,
4160 tsubst_flags_t complain
)
4162 tree orig_arg1
= arg1
;
4163 tree orig_arg2
= arg2
;
4165 tree overload
= NULL_TREE
;
4167 if (processing_template_decl
)
4169 if (type_dependent_expression_p (arg1
)
4170 || type_dependent_expression_p (arg2
))
4171 return build_min_nt_loc (loc
, ARRAY_REF
, arg1
, arg2
,
4172 NULL_TREE
, NULL_TREE
);
4173 arg1
= build_non_dependent_expr (arg1
);
4174 arg2
= build_non_dependent_expr (arg2
);
4177 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, arg1
, arg2
,
4178 NULL_TREE
, &overload
, complain
);
4180 if (processing_template_decl
&& expr
!= error_mark_node
)
4182 if (overload
!= NULL_TREE
)
4183 return (build_min_non_dep_op_overload
4184 (ARRAY_REF
, expr
, overload
, orig_arg1
, orig_arg2
));
4186 return build_min_non_dep (ARRAY_REF
, expr
, orig_arg1
, orig_arg2
,
4187 NULL_TREE
, NULL_TREE
);
4192 /* Return whether OP is an expression of enum type cast to integer
4193 type. In C++ even unsigned enum types are cast to signed integer
4194 types. We do not want to issue warnings about comparisons between
4195 signed and unsigned types when one of the types is an enum type.
4196 Those warnings are always false positives in practice. */
4199 enum_cast_to_int (tree op
)
4201 if (CONVERT_EXPR_P (op
)
4202 && TREE_TYPE (op
) == integer_type_node
4203 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == ENUMERAL_TYPE
4204 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op
, 0))))
4207 /* The cast may have been pushed into a COND_EXPR. */
4208 if (TREE_CODE (op
) == COND_EXPR
)
4209 return (enum_cast_to_int (TREE_OPERAND (op
, 1))
4210 || enum_cast_to_int (TREE_OPERAND (op
, 2)));
4215 /* For the c-common bits. */
4217 build_binary_op (location_t location
, enum tree_code code
, tree op0
, tree op1
,
4220 return cp_build_binary_op (location
, code
, op0
, op1
, tf_warning_or_error
);
4223 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4224 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4227 build_vec_cmp (tree_code code
, tree type
,
4228 tree arg0
, tree arg1
)
4230 tree zero_vec
= build_zero_cst (type
);
4231 tree minus_one_vec
= build_minus_one_cst (type
);
4232 tree cmp_type
= build_same_sized_truth_vector_type(type
);
4233 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
4234 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
4237 /* Possibly warn about an address never being NULL. */
4240 warn_for_null_address (location_t location
, tree op
, tsubst_flags_t complain
)
4243 || (complain
& tf_warning
) == 0
4244 || c_inhibit_evaluation_warnings
!= 0
4245 || TREE_NO_WARNING (op
))
4248 tree cop
= fold_non_dependent_expr (op
, complain
);
4250 if (TREE_CODE (cop
) == ADDR_EXPR
4251 && decl_with_nonnull_addr_p (TREE_OPERAND (cop
, 0))
4252 && !TREE_NO_WARNING (cop
))
4253 warning_at (location
, OPT_Waddress
, "the address of %qD will never "
4254 "be NULL", TREE_OPERAND (cop
, 0));
4256 if (CONVERT_EXPR_P (op
)
4257 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op
, 0))))
4260 STRIP_NOPS (inner_op
);
4262 if (DECL_P (inner_op
))
4263 warning_at (location
, OPT_Waddress
,
4264 "the compiler can assume that the address of "
4265 "%qD will never be NULL", inner_op
);
4269 /* Build a binary-operation expression without default conversions.
4270 CODE is the kind of expression to build.
4271 LOCATION is the location_t of the operator in the source code.
4272 This function differs from `build' in several ways:
4273 the data type of the result is computed and recorded in it,
4274 warnings are generated if arg data types are invalid,
4275 special handling for addition and subtraction of pointers is known,
4276 and some optimization is done (operations on narrow ints
4277 are done in the narrower type when that gives the same result).
4278 Constant folding is also done before the result is returned.
4280 Note that the operands will never have enumeral types
4281 because either they have just had the default conversions performed
4282 or they have both just been converted to some other type in which
4283 the arithmetic is to be done.
4285 C++: must do special pointer arithmetic when implementing
4286 multiple inheritance, and deal with pointer to member functions. */
4289 cp_build_binary_op (location_t location
,
4290 enum tree_code code
, tree orig_op0
, tree orig_op1
,
4291 tsubst_flags_t complain
)
4294 enum tree_code code0
, code1
;
4296 const char *invalid_op_diag
;
4298 /* Expression code to give to the expression when it is built.
4299 Normally this is CODE, which is what the caller asked for,
4300 but in some special cases we change it. */
4301 enum tree_code resultcode
= code
;
4303 /* Data type in which the computation is to be performed.
4304 In the simplest cases this is the common type of the arguments. */
4305 tree result_type
= NULL_TREE
;
4307 /* Nonzero means operands have already been type-converted
4308 in whatever way is necessary.
4309 Zero means they need to be converted to RESULT_TYPE. */
4312 /* Nonzero means create the expression with this type, rather than
4314 tree build_type
= 0;
4316 /* Nonzero means after finally constructing the expression
4317 convert it to this type. */
4318 tree final_type
= 0;
4320 tree result
, result_ovl
;
4322 /* Nonzero if this is an operation like MIN or MAX which can
4323 safely be computed in short if both args are promoted shorts.
4324 Also implies COMMON.
4325 -1 indicates a bitwise operation; this makes a difference
4326 in the exact conditions for when it is safe to do the operation
4327 in a narrower mode. */
4330 /* Nonzero if this is a comparison operation;
4331 if both args are promoted shorts, compare the original shorts.
4332 Also implies COMMON. */
4333 int short_compare
= 0;
4335 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4338 /* True if both operands have arithmetic type. */
4339 bool arithmetic_types_p
;
4341 /* Apply default conversions. */
4345 /* Remember whether we're doing / or %. */
4346 bool doing_div_or_mod
= false;
4348 /* Remember whether we're doing << or >>. */
4349 bool doing_shift
= false;
4351 /* Tree holding instrumentation expression. */
4352 tree instrument_expr
= NULL_TREE
;
4354 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
4355 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
4356 || code
== TRUTH_XOR_EXPR
)
4358 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
4359 op0
= decay_conversion (op0
, complain
);
4360 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
4361 op1
= decay_conversion (op1
, complain
);
4365 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
4366 op0
= cp_default_conversion (op0
, complain
);
4367 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
4368 op1
= cp_default_conversion (op1
, complain
);
4371 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4372 STRIP_TYPE_NOPS (op0
);
4373 STRIP_TYPE_NOPS (op1
);
4375 /* DTRT if one side is an overloaded function, but complain about it. */
4376 if (type_unknown_p (op0
))
4378 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
4379 if (t
!= error_mark_node
)
4381 if (complain
& tf_error
)
4382 permerror (input_location
, "assuming cast to type %qT from overloaded function",
4387 if (type_unknown_p (op1
))
4389 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
4390 if (t
!= error_mark_node
)
4392 if (complain
& tf_error
)
4393 permerror (input_location
, "assuming cast to type %qT from overloaded function",
4399 type0
= TREE_TYPE (op0
);
4400 type1
= TREE_TYPE (op1
);
4402 /* The expression codes of the data types of the arguments tell us
4403 whether the arguments are integers, floating, pointers, etc. */
4404 code0
= TREE_CODE (type0
);
4405 code1
= TREE_CODE (type1
);
4407 /* If an error was already reported for one of the arguments,
4408 avoid reporting another error. */
4409 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
4410 return error_mark_node
;
4412 if ((invalid_op_diag
4413 = targetm
.invalid_binary_op (code
, type0
, type1
)))
4415 if (complain
& tf_error
)
4416 error (invalid_op_diag
);
4417 return error_mark_node
;
4420 /* Issue warnings about peculiar, but valid, uses of NULL. */
4421 if ((null_node_p (orig_op0
) || null_node_p (orig_op1
))
4422 /* It's reasonable to use pointer values as operands of &&
4423 and ||, so NULL is no exception. */
4424 && code
!= TRUTH_ANDIF_EXPR
&& code
!= TRUTH_ORIF_EXPR
4425 && ( /* Both are NULL (or 0) and the operation was not a
4426 comparison or a pointer subtraction. */
4427 (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)
4428 && code
!= EQ_EXPR
&& code
!= NE_EXPR
&& code
!= MINUS_EXPR
)
4429 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4430 || (!null_ptr_cst_p (orig_op0
)
4431 && !TYPE_PTR_OR_PTRMEM_P (type0
))
4432 || (!null_ptr_cst_p (orig_op1
)
4433 && !TYPE_PTR_OR_PTRMEM_P (type1
)))
4434 && (complain
& tf_warning
))
4436 source_location loc
=
4437 expansion_point_location_if_in_system_header (input_location
);
4439 warning_at (loc
, OPT_Wpointer_arith
, "NULL used in arithmetic");
4442 /* In case when one of the operands of the binary operation is
4443 a vector and another is a scalar -- convert scalar to vector. */
4444 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
4446 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
4447 complain
& tf_error
);
4449 switch (convert_flag
)
4452 return error_mark_node
;
4455 op0
= convert (TREE_TYPE (type1
), op0
);
4456 op0
= save_expr (op0
);
4457 op0
= build_vector_from_val (type1
, op0
);
4458 type0
= TREE_TYPE (op0
);
4459 code0
= TREE_CODE (type0
);
4465 op1
= convert (TREE_TYPE (type0
), op1
);
4466 op1
= save_expr (op1
);
4467 op1
= build_vector_from_val (type0
, op1
);
4468 type1
= TREE_TYPE (op1
);
4469 code1
= TREE_CODE (type1
);
4481 /* Subtraction of two similar pointers.
4482 We must subtract them as integers, then divide by object size. */
4483 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
4484 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4487 result
= pointer_diff (location
, op0
, op1
,
4488 common_pointer_type (type0
, type1
), complain
,
4490 if (instrument_expr
!= NULL
)
4491 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
4492 instrument_expr
, result
);
4496 /* In all other cases except pointer - int, the usual arithmetic
4498 else if (!(code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
))
4503 /* The pointer - int case is just like pointer + int; fall
4507 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
4508 && (code0
== INTEGER_TYPE
|| code1
== INTEGER_TYPE
))
4512 ptr_operand
= ((code0
== POINTER_TYPE
) ? op0
: op1
);
4513 int_operand
= ((code0
== INTEGER_TYPE
) ? op0
: op1
);
4514 if (processing_template_decl
)
4516 result_type
= TREE_TYPE (ptr_operand
);
4519 return cp_pointer_int_sum (location
, code
,
4531 case TRUNC_DIV_EXPR
:
4533 case FLOOR_DIV_EXPR
:
4534 case ROUND_DIV_EXPR
:
4535 case EXACT_DIV_EXPR
:
4536 if (TREE_CODE (op0
) == SIZEOF_EXPR
&& TREE_CODE (op1
) == SIZEOF_EXPR
)
4538 tree type0
= TREE_OPERAND (op0
, 0);
4539 tree type1
= TREE_OPERAND (op1
, 0);
4540 tree first_arg
= type0
;
4541 if (!TYPE_P (type0
))
4542 type0
= TREE_TYPE (type0
);
4543 if (!TYPE_P (type1
))
4544 type1
= TREE_TYPE (type1
);
4545 if (INDIRECT_TYPE_P (type0
) && same_type_p (TREE_TYPE (type0
), type1
)
4546 && !(TREE_CODE (first_arg
) == PARM_DECL
4547 && DECL_ARRAY_PARAMETER_P (first_arg
)
4548 && warn_sizeof_array_argument
)
4549 && (complain
& tf_warning
))
4551 auto_diagnostic_group d
;
4552 if (warning_at (location
, OPT_Wsizeof_pointer_div
,
4553 "division %<sizeof (%T) / sizeof (%T)%> does "
4554 "not compute the number of array elements",
4556 if (DECL_P (first_arg
))
4557 inform (DECL_SOURCE_LOCATION (first_arg
),
4558 "first %<sizeof%> operand was declared here");
4562 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4563 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
4564 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4565 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
4567 enum tree_code tcode0
= code0
, tcode1
= code1
;
4568 tree cop1
= fold_non_dependent_expr (op1
, complain
);
4569 doing_div_or_mod
= true;
4570 warn_for_div_by_zero (location
, cop1
);
4572 if (tcode0
== COMPLEX_TYPE
|| tcode0
== VECTOR_TYPE
)
4573 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
4574 if (tcode1
== COMPLEX_TYPE
|| tcode1
== VECTOR_TYPE
)
4575 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
4577 if (!(tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
))
4578 resultcode
= RDIV_EXPR
;
4580 /* When dividing two signed integers, we have to promote to int.
4581 unless we divide by a constant != -1. Note that default
4582 conversion will have been performed on the operands at this
4583 point, so we have to dig out the original type to find out if
4585 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4586 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4587 || (TREE_CODE (op1
) == INTEGER_CST
4588 && ! integer_all_onesp (op1
)));
4597 if ((code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4598 || (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4599 && !VECTOR_FLOAT_TYPE_P (type0
)
4600 && !VECTOR_FLOAT_TYPE_P (type1
)))
4604 case TRUNC_MOD_EXPR
:
4605 case FLOOR_MOD_EXPR
:
4607 tree cop1
= fold_non_dependent_expr (op1
, complain
);
4608 doing_div_or_mod
= true;
4609 warn_for_div_by_zero (location
, cop1
);
4612 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4613 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4614 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
4616 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4618 /* Although it would be tempting to shorten always here, that loses
4619 on some targets, since the modulo instruction is undefined if the
4620 quotient can't be represented in the computation mode. We shorten
4621 only if unsigned or if dividing by something we know != -1. */
4622 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4623 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4624 || (TREE_CODE (op1
) == INTEGER_CST
4625 && ! integer_all_onesp (op1
)));
4630 case TRUTH_ANDIF_EXPR
:
4631 case TRUTH_ORIF_EXPR
:
4632 case TRUTH_AND_EXPR
:
4634 if (!VECTOR_TYPE_P (type0
) && VECTOR_TYPE_P (type1
))
4636 if (!COMPARISON_CLASS_P (op1
))
4637 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4638 build_zero_cst (type1
), complain
);
4639 if (code
== TRUTH_ANDIF_EXPR
)
4641 tree z
= build_zero_cst (TREE_TYPE (op1
));
4642 return build_conditional_expr (location
, op0
, op1
, z
, complain
);
4644 else if (code
== TRUTH_ORIF_EXPR
)
4646 tree m1
= build_all_ones_cst (TREE_TYPE (op1
));
4647 return build_conditional_expr (location
, op0
, m1
, op1
, complain
);
4652 if (VECTOR_TYPE_P (type0
))
4654 if (!COMPARISON_CLASS_P (op0
))
4655 op0
= cp_build_binary_op (EXPR_LOCATION (op0
), NE_EXPR
, op0
,
4656 build_zero_cst (type0
), complain
);
4657 if (!VECTOR_TYPE_P (type1
))
4659 tree m1
= build_all_ones_cst (TREE_TYPE (op0
));
4660 tree z
= build_zero_cst (TREE_TYPE (op0
));
4661 op1
= build_conditional_expr (location
, op1
, m1
, z
, complain
);
4663 else if (!COMPARISON_CLASS_P (op1
))
4664 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4665 build_zero_cst (type1
), complain
);
4667 if (code
== TRUTH_ANDIF_EXPR
)
4668 code
= BIT_AND_EXPR
;
4669 else if (code
== TRUTH_ORIF_EXPR
)
4670 code
= BIT_IOR_EXPR
;
4674 return cp_build_binary_op (location
, code
, op0
, op1
, complain
);
4677 result_type
= boolean_type_node
;
4680 /* Shift operations: result has same type as first operand;
4681 always convert second operand to int.
4682 Also set SHORT_SHIFT if shifting rightward. */
4685 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4686 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4688 result_type
= type0
;
4691 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4692 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4693 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4694 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
4695 TYPE_VECTOR_SUBPARTS (type1
)))
4697 result_type
= type0
;
4700 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4702 tree const_op1
= fold_non_dependent_expr (op1
, complain
);
4703 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4705 result_type
= type0
;
4707 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4709 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4711 if ((complain
& tf_warning
)
4712 && c_inhibit_evaluation_warnings
== 0)
4713 warning (OPT_Wshift_count_negative
,
4714 "right shift count is negative");
4718 if (compare_tree_int (const_op1
, TYPE_PRECISION (type0
)) >= 0
4719 && (complain
& tf_warning
)
4720 && c_inhibit_evaluation_warnings
== 0)
4721 warning (OPT_Wshift_count_overflow
,
4722 "right shift count >= width of type");
4725 /* Avoid converting op1 to result_type later. */
4731 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4732 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4734 result_type
= type0
;
4737 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4738 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4739 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4740 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
4741 TYPE_VECTOR_SUBPARTS (type1
)))
4743 result_type
= type0
;
4746 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4748 tree const_op0
= fold_non_dependent_expr (op0
, complain
);
4749 if (TREE_CODE (const_op0
) != INTEGER_CST
)
4751 tree const_op1
= fold_non_dependent_expr (op1
, complain
);
4752 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4754 result_type
= type0
;
4756 if (TREE_CODE (const_op0
) == INTEGER_CST
4757 && tree_int_cst_sgn (const_op0
) < 0
4758 && (complain
& tf_warning
)
4759 && c_inhibit_evaluation_warnings
== 0)
4760 warning (OPT_Wshift_negative_value
,
4761 "left shift of negative value");
4762 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4764 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4766 if ((complain
& tf_warning
)
4767 && c_inhibit_evaluation_warnings
== 0)
4768 warning (OPT_Wshift_count_negative
,
4769 "left shift count is negative");
4771 else if (compare_tree_int (const_op1
,
4772 TYPE_PRECISION (type0
)) >= 0)
4774 if ((complain
& tf_warning
)
4775 && c_inhibit_evaluation_warnings
== 0)
4776 warning (OPT_Wshift_count_overflow
,
4777 "left shift count >= width of type");
4779 else if (TREE_CODE (const_op0
) == INTEGER_CST
4780 && (complain
& tf_warning
))
4781 maybe_warn_shift_overflow (location
, const_op0
, const_op1
);
4783 /* Avoid converting op1 to result_type later. */
4790 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4792 result_type
= type0
;
4793 if (TREE_CODE (op1
) == INTEGER_CST
)
4795 if (tree_int_cst_lt (op1
, integer_zero_node
))
4797 if (complain
& tf_warning
)
4798 warning (0, (code
== LROTATE_EXPR
)
4799 ? G_("left rotate count is negative")
4800 : G_("right rotate count is negative"));
4802 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
4804 if (complain
& tf_warning
)
4805 warning (0, (code
== LROTATE_EXPR
)
4806 ? G_("left rotate count >= width of type")
4807 : G_("right rotate count >= width of type"));
4810 /* Convert the shift-count to an integer, regardless of
4811 size of value being shifted. */
4812 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4813 op1
= cp_convert (integer_type_node
, op1
, complain
);
4819 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4820 goto vector_compare
;
4821 if ((complain
& tf_warning
)
4822 && c_inhibit_evaluation_warnings
== 0
4823 && (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
)))
4824 warning (OPT_Wfloat_equal
,
4825 "comparing floating point with == or != is unsafe");
4826 if ((complain
& tf_warning
)
4827 && ((TREE_CODE (orig_op0
) == STRING_CST
4828 && !integer_zerop (cp_fully_fold (op1
)))
4829 || (TREE_CODE (orig_op1
) == STRING_CST
4830 && !integer_zerop (cp_fully_fold (op0
)))))
4831 warning (OPT_Waddress
, "comparison with string literal results "
4832 "in unspecified behavior");
4834 build_type
= boolean_type_node
;
4835 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4836 || code0
== COMPLEX_TYPE
|| code0
== ENUMERAL_TYPE
)
4837 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4838 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
))
4840 else if (((code0
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type0
))
4841 && null_ptr_cst_p (orig_op1
))
4842 /* Handle, eg, (void*)0 (c++/43906), and more. */
4843 || (code0
== POINTER_TYPE
4844 && TYPE_PTR_P (type1
) && integer_zerop (op1
)))
4846 if (TYPE_PTR_P (type1
))
4847 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4848 CPO_COMPARISON
, complain
);
4850 result_type
= type0
;
4852 if (char_type_p (TREE_TYPE (orig_op1
)))
4854 auto_diagnostic_group d
;
4855 if (warning (OPT_Wpointer_compare
,
4856 "comparison between pointer and zero character "
4858 inform (input_location
,
4859 "did you mean to dereference the pointer?");
4861 warn_for_null_address (location
, op0
, complain
);
4863 else if (((code1
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type1
))
4864 && null_ptr_cst_p (orig_op0
))
4865 /* Handle, eg, (void*)0 (c++/43906), and more. */
4866 || (code1
== POINTER_TYPE
4867 && TYPE_PTR_P (type0
) && integer_zerop (op0
)))
4869 if (TYPE_PTR_P (type0
))
4870 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4871 CPO_COMPARISON
, complain
);
4873 result_type
= type1
;
4875 if (char_type_p (TREE_TYPE (orig_op0
)))
4877 auto_diagnostic_group d
;
4878 if (warning (OPT_Wpointer_compare
,
4879 "comparison between pointer and zero character "
4881 inform (input_location
,
4882 "did you mean to dereference the pointer?");
4884 warn_for_null_address (location
, op1
, complain
);
4886 else if ((code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4887 || (TYPE_PTRDATAMEM_P (type0
) && TYPE_PTRDATAMEM_P (type1
)))
4888 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4889 CPO_COMPARISON
, complain
);
4890 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
4891 /* One of the operands must be of nullptr_t type. */
4892 result_type
= TREE_TYPE (nullptr_node
);
4893 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4895 result_type
= type0
;
4896 if (complain
& tf_error
)
4897 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4899 return error_mark_node
;
4901 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4903 result_type
= type1
;
4904 if (complain
& tf_error
)
4905 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4907 return error_mark_node
;
4909 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (orig_op1
))
4911 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4912 == ptrmemfunc_vbit_in_delta
)
4914 tree pfn0
, delta0
, e1
, e2
;
4916 if (TREE_SIDE_EFFECTS (op0
))
4917 op0
= save_expr (op0
);
4919 pfn0
= pfn_from_ptrmemfunc (op0
);
4920 delta0
= delta_from_ptrmemfunc (op0
);
4921 e1
= cp_build_binary_op (location
,
4924 build_zero_cst (TREE_TYPE (pfn0
)),
4926 e2
= cp_build_binary_op (location
,
4932 if (complain
& tf_warning
)
4933 maybe_warn_zero_as_null_pointer_constant (op1
, input_location
);
4935 e2
= cp_build_binary_op (location
,
4936 EQ_EXPR
, e2
, integer_zero_node
,
4938 op0
= cp_build_binary_op (location
,
4939 TRUTH_ANDIF_EXPR
, e1
, e2
,
4941 op1
= cp_convert (TREE_TYPE (op0
), integer_one_node
, complain
);
4945 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
4946 op1
= cp_convert (TREE_TYPE (op0
), op1
, complain
);
4948 result_type
= TREE_TYPE (op0
);
4950 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (orig_op0
))
4951 return cp_build_binary_op (location
, code
, op1
, op0
, complain
);
4952 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
))
4955 /* E will be the final comparison. */
4957 /* E1 and E2 are for scratch. */
4965 type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4966 CPO_COMPARISON
, complain
);
4968 if (!same_type_p (TREE_TYPE (op0
), type
))
4969 op0
= cp_convert_and_check (type
, op0
, complain
);
4970 if (!same_type_p (TREE_TYPE (op1
), type
))
4971 op1
= cp_convert_and_check (type
, op1
, complain
);
4973 if (op0
== error_mark_node
|| op1
== error_mark_node
)
4974 return error_mark_node
;
4976 if (TREE_SIDE_EFFECTS (op0
))
4977 op0
= save_expr (op0
);
4978 if (TREE_SIDE_EFFECTS (op1
))
4979 op1
= save_expr (op1
);
4981 pfn0
= pfn_from_ptrmemfunc (op0
);
4982 pfn0
= cp_fully_fold (pfn0
);
4983 /* Avoid -Waddress warnings (c++/64877). */
4984 if (TREE_CODE (pfn0
) == ADDR_EXPR
)
4985 TREE_NO_WARNING (pfn0
) = 1;
4986 pfn1
= pfn_from_ptrmemfunc (op1
);
4987 pfn1
= cp_fully_fold (pfn1
);
4988 delta0
= delta_from_ptrmemfunc (op0
);
4989 delta1
= delta_from_ptrmemfunc (op1
);
4990 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4991 == ptrmemfunc_vbit_in_delta
)
4996 && ((op0.delta == op1.delta)
4997 || (!op0.pfn && op0.delta & 1 == 0
4998 && op1.delta & 1 == 0))
5000 The reason for the `!op0.pfn' bit is that a NULL
5001 pointer-to-member is any member with a zero PFN and
5002 LSB of the DELTA field is 0. */
5004 e1
= cp_build_binary_op (location
, BIT_AND_EXPR
,
5008 e1
= cp_build_binary_op (location
,
5009 EQ_EXPR
, e1
, integer_zero_node
,
5011 e2
= cp_build_binary_op (location
, BIT_AND_EXPR
,
5015 e2
= cp_build_binary_op (location
,
5016 EQ_EXPR
, e2
, integer_zero_node
,
5018 e1
= cp_build_binary_op (location
,
5019 TRUTH_ANDIF_EXPR
, e2
, e1
,
5021 e2
= cp_build_binary_op (location
, EQ_EXPR
,
5023 build_zero_cst (TREE_TYPE (pfn0
)),
5025 e2
= cp_build_binary_op (location
,
5026 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
5027 e1
= cp_build_binary_op (location
,
5028 EQ_EXPR
, delta0
, delta1
, complain
);
5029 e1
= cp_build_binary_op (location
,
5030 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
5037 && (!op0.pfn || op0.delta == op1.delta))
5039 The reason for the `!op0.pfn' bit is that a NULL
5040 pointer-to-member is any member with a zero PFN; the
5041 DELTA field is unspecified. */
5043 e1
= cp_build_binary_op (location
,
5044 EQ_EXPR
, delta0
, delta1
, complain
);
5045 e2
= cp_build_binary_op (location
,
5048 build_zero_cst (TREE_TYPE (pfn0
)),
5050 e1
= cp_build_binary_op (location
,
5051 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
5053 e2
= build2 (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
5054 e
= cp_build_binary_op (location
,
5055 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
5056 if (code
== EQ_EXPR
)
5058 return cp_build_binary_op (location
,
5059 EQ_EXPR
, e
, integer_zero_node
, complain
);
5063 gcc_assert (!TYPE_PTRMEMFUNC_P (type0
)
5064 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
),
5066 gcc_assert (!TYPE_PTRMEMFUNC_P (type1
)
5067 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
),
5075 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
5076 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
5078 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5079 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
5080 CPO_COMPARISON
, complain
);
5087 if (TREE_CODE (orig_op0
) == STRING_CST
5088 || TREE_CODE (orig_op1
) == STRING_CST
)
5090 if (complain
& tf_warning
)
5091 warning (OPT_Waddress
, "comparison with string literal results "
5092 "in unspecified behavior");
5095 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
5099 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
5101 && !vector_types_compatible_elements_p (type0
, type1
))
5103 if (complain
& tf_error
)
5105 error_at (location
, "comparing vectors with different "
5107 inform (location
, "operand types are %qT and %qT",
5110 return error_mark_node
;
5113 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0
),
5114 TYPE_VECTOR_SUBPARTS (type1
)))
5116 if (complain
& tf_error
)
5118 error_at (location
, "comparing vectors with different "
5119 "number of elements");
5120 inform (location
, "operand types are %qT and %qT",
5123 return error_mark_node
;
5126 /* It's not precisely specified how the usual arithmetic
5127 conversions apply to the vector types. Here, we use
5128 the unsigned type if one of the operands is signed and
5129 the other one is unsigned. */
5130 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
5132 if (!TYPE_UNSIGNED (type0
))
5133 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
5135 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
5136 warning_at (location
, OPT_Wsign_compare
, "comparison between "
5137 "types %qT and %qT", type0
, type1
);
5140 /* Always construct signed integer vector type. */
5141 intt
= c_common_type_for_size
5142 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0
))), 0);
5145 if (complain
& tf_error
)
5146 error_at (location
, "could not find an integer type "
5147 "of the same size as %qT", TREE_TYPE (type0
));
5148 return error_mark_node
;
5150 result_type
= build_opaque_vector_type (intt
,
5151 TYPE_VECTOR_SUBPARTS (type0
));
5153 return build_vec_cmp (resultcode
, result_type
, op0
, op1
);
5155 build_type
= boolean_type_node
;
5156 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
5157 || code0
== ENUMERAL_TYPE
)
5158 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5159 || code1
== ENUMERAL_TYPE
))
5161 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5162 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
5163 CPO_COMPARISON
, complain
);
5164 else if (code0
== POINTER_TYPE
&& null_ptr_cst_p (orig_op1
))
5166 result_type
= type0
;
5167 if (extra_warnings
&& (complain
& tf_warning
))
5168 warning (OPT_Wextra
,
5169 "ordered comparison of pointer with integer zero");
5171 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (orig_op0
))
5173 result_type
= type1
;
5174 if (extra_warnings
&& (complain
& tf_warning
))
5175 warning (OPT_Wextra
,
5176 "ordered comparison of pointer with integer zero");
5178 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
5179 /* One of the operands must be of nullptr_t type. */
5180 result_type
= TREE_TYPE (nullptr_node
);
5181 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5183 result_type
= type0
;
5184 if (complain
& tf_error
)
5185 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
5187 return error_mark_node
;
5189 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
5191 result_type
= type1
;
5192 if (complain
& tf_error
)
5193 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
5195 return error_mark_node
;
5198 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
5199 && sanitize_flags_p (SANITIZE_POINTER_COMPARE
))
5201 op0
= save_expr (op0
);
5202 op1
= save_expr (op1
);
5204 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE
);
5205 instrument_expr
= build_call_expr_loc (location
, tt
, 2, op0
, op1
);
5210 case UNORDERED_EXPR
:
5217 build_type
= integer_type_node
;
5218 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
5220 if (complain
& tf_error
)
5221 error ("unordered comparison on non-floating point argument");
5222 return error_mark_node
;
5231 if (((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
5232 || code0
== ENUMERAL_TYPE
)
5233 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5234 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
)))
5235 arithmetic_types_p
= 1;
5238 arithmetic_types_p
= 0;
5239 /* Vector arithmetic is only allowed when both sides are vectors. */
5240 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
5242 if (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
5243 || !vector_types_compatible_elements_p (type0
, type1
))
5245 if (complain
& tf_error
)
5247 /* "location" already embeds the locations of the
5248 operands, so we don't need to add them separately
5250 rich_location
richloc (line_table
, location
);
5251 binary_op_error (&richloc
, code
, type0
, type1
);
5253 return error_mark_node
;
5255 arithmetic_types_p
= 1;
5258 /* Determine the RESULT_TYPE, if it is not already known. */
5260 && arithmetic_types_p
5261 && (shorten
|| common
|| short_compare
))
5263 result_type
= cp_common_type (type0
, type1
);
5264 if (complain
& tf_warning
)
5265 do_warn_double_promotion (result_type
, type0
, type1
,
5266 "implicit conversion from %qH to %qI "
5267 "to match other operand of binary "
5274 if (complain
& tf_error
)
5276 "invalid operands of types %qT and %qT to binary %qO",
5277 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
5278 return error_mark_node
;
5281 /* If we're in a template, the only thing we need to know is the
5283 if (processing_template_decl
)
5285 /* Since the middle-end checks the type when doing a build2, we
5286 need to build the tree in pieces. This built tree will never
5287 get out of the front-end as we replace it when instantiating
5289 tree tmp
= build2 (resultcode
,
5290 build_type
? build_type
: result_type
,
5292 TREE_OPERAND (tmp
, 0) = op0
;
5296 /* Remember the original type; RESULT_TYPE might be changed later on
5297 by shorten_binary_op. */
5298 tree orig_type
= result_type
;
5300 if (arithmetic_types_p
)
5302 bool first_complex
= (code0
== COMPLEX_TYPE
);
5303 bool second_complex
= (code1
== COMPLEX_TYPE
);
5304 int none_complex
= (!first_complex
&& !second_complex
);
5306 /* Adapted from patch for c/24581. */
5307 if (first_complex
!= second_complex
5308 && (code
== PLUS_EXPR
5309 || code
== MINUS_EXPR
5310 || code
== MULT_EXPR
5311 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
5312 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
5313 && flag_signed_zeros
)
5315 /* An operation on mixed real/complex operands must be
5316 handled specially, but the language-independent code can
5317 more easily optimize the plain complex arithmetic if
5318 -fno-signed-zeros. */
5319 tree real_type
= TREE_TYPE (result_type
);
5323 if (TREE_TYPE (op0
) != result_type
)
5324 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5325 if (TREE_TYPE (op1
) != real_type
)
5326 op1
= cp_convert_and_check (real_type
, op1
, complain
);
5330 if (TREE_TYPE (op0
) != real_type
)
5331 op0
= cp_convert_and_check (real_type
, op0
, complain
);
5332 if (TREE_TYPE (op1
) != result_type
)
5333 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5335 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
5336 return error_mark_node
;
5339 op0
= save_expr (op0
);
5340 real
= cp_build_unary_op (REALPART_EXPR
, op0
, true, complain
);
5341 imag
= cp_build_unary_op (IMAGPART_EXPR
, op0
, true, complain
);
5345 case TRUNC_DIV_EXPR
:
5346 op1
= save_expr (op1
);
5347 imag
= build2 (resultcode
, real_type
, imag
, op1
);
5351 real
= build2 (resultcode
, real_type
, real
, op1
);
5359 op1
= save_expr (op1
);
5360 real
= cp_build_unary_op (REALPART_EXPR
, op1
, true, complain
);
5361 imag
= cp_build_unary_op (IMAGPART_EXPR
, op1
, true, complain
);
5365 op0
= save_expr (op0
);
5366 imag
= build2 (resultcode
, real_type
, op0
, imag
);
5369 real
= build2 (resultcode
, real_type
, op0
, real
);
5372 real
= build2 (resultcode
, real_type
, op0
, real
);
5373 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
5379 result
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
5383 /* For certain operations (which identify themselves by shorten != 0)
5384 if both args were extended from the same smaller type,
5385 do the arithmetic in that type and then extend.
5387 shorten !=0 and !=1 indicates a bitwise operation.
5388 For them, this optimization is safe only if
5389 both args are zero-extended or both are sign-extended.
5390 Otherwise, we might change the result.
5391 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5392 but calculated in (unsigned short) it would be (unsigned short)-1. */
5394 if (shorten
&& none_complex
)
5396 final_type
= result_type
;
5397 result_type
= shorten_binary_op (result_type
, op0
, op1
,
5401 /* Comparison operations are shortened too but differently.
5402 They identify themselves by setting short_compare = 1. */
5406 /* We call shorten_compare only for diagnostics. */
5407 tree xop0
= fold_simple (op0
);
5408 tree xop1
= fold_simple (op1
);
5409 tree xresult_type
= result_type
;
5410 enum tree_code xresultcode
= resultcode
;
5411 shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
5415 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
5416 && warn_sign_compare
5417 /* Do not warn until the template is instantiated; we cannot
5418 bound the ranges of the arguments until that point. */
5419 && !processing_template_decl
5420 && (complain
& tf_warning
)
5421 && c_inhibit_evaluation_warnings
== 0
5422 /* Even unsigned enum types promote to signed int. We don't
5423 want to issue -Wsign-compare warnings for this case. */
5424 && !enum_cast_to_int (orig_op0
)
5425 && !enum_cast_to_int (orig_op1
))
5427 tree oop0
= maybe_constant_value (orig_op0
);
5428 tree oop1
= maybe_constant_value (orig_op1
);
5430 if (TREE_CODE (oop0
) != INTEGER_CST
)
5431 oop0
= cp_fully_fold (orig_op0
);
5432 if (TREE_CODE (oop1
) != INTEGER_CST
)
5433 oop1
= cp_fully_fold (orig_op1
);
5434 warn_for_sign_compare (location
, oop0
, oop1
, op0
, op1
,
5435 result_type
, resultcode
);
5439 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5440 Then the expression will be built.
5441 It will be given type FINAL_TYPE if that is nonzero;
5442 otherwise, it will be given type RESULT_TYPE. */
5445 warning_sentinel
w (warn_sign_conversion
, short_compare
);
5446 if (TREE_TYPE (op0
) != result_type
)
5447 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5448 if (TREE_TYPE (op1
) != result_type
)
5449 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5451 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5452 return error_mark_node
;
5455 if (build_type
== NULL_TREE
)
5456 build_type
= result_type
;
5458 if (sanitize_flags_p ((SANITIZE_SHIFT
5459 | SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
))
5460 && current_function_decl
!= NULL_TREE
5461 && !processing_template_decl
5462 && (doing_div_or_mod
|| doing_shift
))
5464 /* OP0 and/or OP1 might have side-effects. */
5465 op0
= cp_save_expr (op0
);
5466 op1
= cp_save_expr (op1
);
5467 op0
= fold_non_dependent_expr (op0
, complain
);
5468 op1
= fold_non_dependent_expr (op1
, complain
);
5469 if (doing_div_or_mod
5470 && sanitize_flags_p (SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
))
5472 /* For diagnostics we want to use the promoted types without
5473 shorten_binary_op. So convert the arguments to the
5474 original result_type. */
5477 if (TREE_TYPE (cop0
) != orig_type
)
5478 cop0
= cp_convert (orig_type
, op0
, complain
);
5479 if (TREE_TYPE (cop1
) != orig_type
)
5480 cop1
= cp_convert (orig_type
, op1
, complain
);
5481 instrument_expr
= ubsan_instrument_division (location
, cop0
, cop1
);
5483 else if (doing_shift
&& sanitize_flags_p (SANITIZE_SHIFT
))
5484 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
5487 result
= build2_loc (location
, resultcode
, build_type
, op0
, op1
);
5488 if (final_type
!= 0)
5489 result
= cp_convert (final_type
, result
, complain
);
5491 if (instrument_expr
!= NULL
)
5492 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
5493 instrument_expr
, result
);
5495 if (!processing_template_decl
)
5497 op0
= cp_fully_fold (op0
);
5498 /* Only consider the second argument if the first isn't overflowed. */
5499 if (!CONSTANT_CLASS_P (op0
) || TREE_OVERFLOW_P (op0
))
5501 op1
= cp_fully_fold (op1
);
5502 if (!CONSTANT_CLASS_P (op1
) || TREE_OVERFLOW_P (op1
))
5505 else if (!CONSTANT_CLASS_P (op0
) || !CONSTANT_CLASS_P (op1
)
5506 || TREE_OVERFLOW_P (op0
) || TREE_OVERFLOW_P (op1
))
5509 result_ovl
= fold_build2 (resultcode
, build_type
, op0
, op1
);
5510 if (TREE_OVERFLOW_P (result_ovl
))
5511 overflow_warning (location
, result_ovl
);
5516 /* Build a VEC_PERM_EXPR.
5517 This is a simple wrapper for c_build_vec_perm_expr. */
5519 build_x_vec_perm_expr (location_t loc
,
5520 tree arg0
, tree arg1
, tree arg2
,
5521 tsubst_flags_t complain
)
5523 tree orig_arg0
= arg0
;
5524 tree orig_arg1
= arg1
;
5525 tree orig_arg2
= arg2
;
5526 if (processing_template_decl
)
5528 if (type_dependent_expression_p (arg0
)
5529 || type_dependent_expression_p (arg1
)
5530 || type_dependent_expression_p (arg2
))
5531 return build_min_nt_loc (loc
, VEC_PERM_EXPR
, arg0
, arg1
, arg2
);
5532 arg0
= build_non_dependent_expr (arg0
);
5534 arg1
= build_non_dependent_expr (arg1
);
5535 arg2
= build_non_dependent_expr (arg2
);
5537 tree exp
= c_build_vec_perm_expr (loc
, arg0
, arg1
, arg2
, complain
& tf_error
);
5538 if (processing_template_decl
&& exp
!= error_mark_node
)
5539 return build_min_non_dep (VEC_PERM_EXPR
, exp
, orig_arg0
,
5540 orig_arg1
, orig_arg2
);
5544 /* Return a tree for the sum or difference (RESULTCODE says which)
5545 of pointer PTROP and integer INTOP. */
5548 cp_pointer_int_sum (location_t loc
, enum tree_code resultcode
, tree ptrop
,
5549 tree intop
, tsubst_flags_t complain
)
5551 tree res_type
= TREE_TYPE (ptrop
);
5553 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5554 in certain circumstance (when it's valid to do so). So we need
5555 to make sure it's complete. We don't need to check here, if we
5556 can actually complete it at all, as those checks will be done in
5557 pointer_int_sum() anyway. */
5558 complete_type (TREE_TYPE (res_type
));
5560 return pointer_int_sum (loc
, resultcode
, ptrop
,
5561 intop
, complain
& tf_warning_or_error
);
5564 /* Return a tree for the difference of pointers OP0 and OP1.
5565 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
5566 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5569 pointer_diff (location_t loc
, tree op0
, tree op1
, tree ptrtype
,
5570 tsubst_flags_t complain
, tree
*instrument_expr
)
5572 tree result
, inttype
;
5573 tree restype
= ptrdiff_type_node
;
5574 tree target_type
= TREE_TYPE (ptrtype
);
5576 if (!complete_type_or_else (target_type
, NULL_TREE
))
5577 return error_mark_node
;
5579 if (VOID_TYPE_P (target_type
))
5581 if (complain
& tf_error
)
5582 permerror (loc
, "ISO C++ forbids using pointer of "
5583 "type %<void *%> in subtraction");
5585 return error_mark_node
;
5587 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
5589 if (complain
& tf_error
)
5590 permerror (loc
, "ISO C++ forbids using pointer to "
5591 "a function in subtraction");
5593 return error_mark_node
;
5595 if (TREE_CODE (target_type
) == METHOD_TYPE
)
5597 if (complain
& tf_error
)
5598 permerror (loc
, "ISO C++ forbids using pointer to "
5599 "a method in subtraction");
5601 return error_mark_node
;
5604 /* Determine integer type result of the subtraction. This will usually
5605 be the same as the result type (ptrdiff_t), but may need to be a wider
5606 type if pointers for the address space are wider than ptrdiff_t. */
5607 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
5608 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
5612 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT
))
5614 op0
= save_expr (op0
);
5615 op1
= save_expr (op1
);
5617 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT
);
5618 *instrument_expr
= build_call_expr_loc (loc
, tt
, 2, op0
, op1
);
5621 /* First do the subtraction, then build the divide operator
5622 and only convert at the very end.
5623 Do not do default conversions in case restype is a short type. */
5625 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5626 pointers. If some platform cannot provide that, or has a larger
5627 ptrdiff_type to support differences larger than half the address
5628 space, cast the pointers to some larger integer type and do the
5629 computations in that type. */
5630 if (TYPE_PRECISION (inttype
) > TYPE_PRECISION (TREE_TYPE (op0
)))
5631 op0
= cp_build_binary_op (loc
,
5633 cp_convert (inttype
, op0
, complain
),
5634 cp_convert (inttype
, op1
, complain
),
5637 op0
= build2_loc (loc
, POINTER_DIFF_EXPR
, inttype
, op0
, op1
);
5639 /* This generates an error if op1 is a pointer to an incomplete type. */
5640 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
5642 if (complain
& tf_error
)
5643 error_at (loc
, "invalid use of a pointer to an incomplete type in "
5644 "pointer arithmetic");
5646 return error_mark_node
;
5649 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1
)))
5651 if (complain
& tf_error
)
5652 error_at (loc
, "arithmetic on pointer to an empty aggregate");
5654 return error_mark_node
;
5657 op1
= (TYPE_PTROB_P (ptrtype
)
5658 ? size_in_bytes_loc (loc
, target_type
)
5659 : integer_one_node
);
5661 /* Do the division. */
5663 result
= build2_loc (loc
, EXACT_DIV_EXPR
, inttype
, op0
,
5664 cp_convert (inttype
, op1
, complain
));
5665 return cp_convert (restype
, result
, complain
);
5668 /* Construct and perhaps optimize a tree representation
5669 for a unary operation. CODE, a tree_code, specifies the operation
5670 and XARG is the operand. */
5673 build_x_unary_op (location_t loc
, enum tree_code code
, cp_expr xarg
,
5674 tsubst_flags_t complain
)
5676 tree orig_expr
= xarg
;
5679 tree overload
= NULL_TREE
;
5681 if (processing_template_decl
)
5683 if (type_dependent_expression_p (xarg
))
5684 return build_min_nt_loc (loc
, code
, xarg
.get_value (), NULL_TREE
);
5686 xarg
= build_non_dependent_expr (xarg
);
5691 /* [expr.unary.op] says:
5693 The address of an object of incomplete type can be taken.
5695 (And is just the ordinary address operator, not an overloaded
5696 "operator &".) However, if the type is a template
5697 specialization, we must complete the type at this point so that
5698 an overloaded "operator &" will be available if required. */
5699 if (code
== ADDR_EXPR
5700 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
5701 && ((CLASS_TYPE_P (TREE_TYPE (xarg
))
5702 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg
))))
5703 || (TREE_CODE (xarg
) == OFFSET_REF
)))
5704 /* Don't look for a function. */;
5706 exp
= build_new_op (loc
, code
, LOOKUP_NORMAL
, xarg
, NULL_TREE
,
5707 NULL_TREE
, &overload
, complain
);
5709 if (!exp
&& code
== ADDR_EXPR
)
5711 if (is_overloaded_fn (xarg
))
5713 tree fn
= get_first_fn (xarg
);
5714 if (DECL_CONSTRUCTOR_P (fn
) || DECL_DESTRUCTOR_P (fn
))
5716 if (complain
& tf_error
)
5717 error (DECL_CONSTRUCTOR_P (fn
)
5718 ? G_("taking address of constructor %qD")
5719 : G_("taking address of destructor %qD"),
5721 return error_mark_node
;
5725 /* A pointer to member-function can be formed only by saying
5727 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
5728 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
5730 if (TREE_CODE (xarg
) != OFFSET_REF
5731 || !TYPE_P (TREE_OPERAND (xarg
, 0)))
5733 if (complain
& tf_error
)
5735 error ("invalid use of %qE to form a "
5736 "pointer-to-member-function", xarg
.get_value ());
5737 if (TREE_CODE (xarg
) != OFFSET_REF
)
5738 inform (input_location
, " a qualified-id is required");
5740 return error_mark_node
;
5744 if (complain
& tf_error
)
5745 error ("parentheses around %qE cannot be used to form a"
5746 " pointer-to-member-function",
5749 return error_mark_node
;
5750 PTRMEM_OK_P (xarg
) = 1;
5754 if (TREE_CODE (xarg
) == OFFSET_REF
)
5756 ptrmem
= PTRMEM_OK_P (xarg
);
5758 if (!ptrmem
&& !flag_ms_extensions
5759 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
5761 /* A single non-static member, make sure we don't allow a
5762 pointer-to-member. */
5763 xarg
= build2 (OFFSET_REF
, TREE_TYPE (xarg
),
5764 TREE_OPERAND (xarg
, 0),
5765 ovl_make (TREE_OPERAND (xarg
, 1)));
5766 PTRMEM_OK_P (xarg
) = ptrmem
;
5770 exp
= cp_build_addr_expr_strict (xarg
, complain
);
5773 if (processing_template_decl
&& exp
!= error_mark_node
)
5775 if (overload
!= NULL_TREE
)
5776 return (build_min_non_dep_op_overload
5777 (code
, exp
, overload
, orig_expr
, integer_zero_node
));
5779 exp
= build_min_non_dep (code
, exp
, orig_expr
,
5780 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE
);
5782 if (TREE_CODE (exp
) == ADDR_EXPR
)
5783 PTRMEM_OK_P (exp
) = ptrmem
;
5787 /* Construct and perhaps optimize a tree representation
5788 for __builtin_addressof operation. ARG specifies the operand. */
5791 cp_build_addressof (location_t loc
, tree arg
, tsubst_flags_t complain
)
5793 tree orig_expr
= arg
;
5795 if (processing_template_decl
)
5797 if (type_dependent_expression_p (arg
))
5798 return build_min_nt_loc (loc
, ADDRESSOF_EXPR
, arg
, NULL_TREE
);
5800 arg
= build_non_dependent_expr (arg
);
5803 tree exp
= cp_build_addr_expr_strict (arg
, complain
);
5805 if (processing_template_decl
&& exp
!= error_mark_node
)
5806 exp
= build_min_non_dep (ADDRESSOF_EXPR
, exp
, orig_expr
, NULL_TREE
);
5810 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5811 constants, where a null value is represented by an INTEGER_CST of
5815 cp_truthvalue_conversion (tree expr
)
5817 tree type
= TREE_TYPE (expr
);
5818 if (TYPE_PTR_OR_PTRMEM_P (type
)
5819 /* Avoid ICE on invalid use of non-static member function. */
5820 || TREE_CODE (expr
) == FUNCTION_DECL
)
5821 return build_binary_op (input_location
, NE_EXPR
, expr
, nullptr_node
, true);
5823 return c_common_truthvalue_conversion (input_location
, expr
);
5826 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5829 condition_conversion (tree expr
)
5832 /* Anything that might happen in a template should go through
5833 maybe_convert_cond. */
5834 gcc_assert (!processing_template_decl
);
5835 t
= perform_implicit_conversion_flags (boolean_type_node
, expr
,
5836 tf_warning_or_error
, LOOKUP_NORMAL
);
5837 t
= fold_build_cleanup_point_expr (boolean_type_node
, t
);
5841 /* Returns the address of T. This function will fold away
5842 ADDR_EXPR of INDIRECT_REF. */
5845 build_address (tree t
)
5847 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
5848 return error_mark_node
;
5849 gcc_checking_assert (TREE_CODE (t
) != CONSTRUCTOR
5850 || processing_template_decl
);
5851 t
= build_fold_addr_expr_loc (EXPR_LOCATION (t
), t
);
5852 if (TREE_CODE (t
) != ADDR_EXPR
)
5857 /* Return a NOP_EXPR converting EXPR to TYPE. */
5860 build_nop (tree type
, tree expr
)
5862 if (type
== error_mark_node
|| error_operand_p (expr
))
5864 return build1_loc (EXPR_LOCATION (expr
), NOP_EXPR
, type
, expr
);
5867 /* Take the address of ARG, whatever that means under C++ semantics.
5868 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5869 and class rvalues as well.
5871 Nothing should call this function directly; instead, callers should use
5872 cp_build_addr_expr or cp_build_addr_expr_strict. */
5875 cp_build_addr_expr_1 (tree arg
, bool strict_lvalue
, tsubst_flags_t complain
)
5880 if (!arg
|| error_operand_p (arg
))
5881 return error_mark_node
;
5883 arg
= mark_lvalue_use (arg
);
5884 if (error_operand_p (arg
))
5885 return error_mark_node
;
5887 argtype
= lvalue_type (arg
);
5889 gcc_assert (!(identifier_p (arg
) && IDENTIFIER_ANY_OP_P (arg
)));
5891 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
5892 && !really_overloaded_fn (arg
))
5894 /* They're trying to take the address of a unique non-static
5895 member function. This is ill-formed (except in MS-land),
5896 but let's try to DTRT.
5897 Note: We only handle unique functions here because we don't
5898 want to complain if there's a static overload; non-unique
5899 cases will be handled by instantiate_type. But we need to
5900 handle this case here to allow casts on the resulting PMF.
5901 We could defer this in non-MS mode, but it's easier to give
5902 a useful error here. */
5904 /* Inside constant member functions, the `this' pointer
5905 contains an extra const qualifier. TYPE_MAIN_VARIANT
5906 is used here to remove this const from the diagnostics
5907 and the created OFFSET_REF. */
5908 tree base
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg
, 0)));
5909 tree fn
= get_first_fn (TREE_OPERAND (arg
, 1));
5910 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
5911 return error_mark_node
;
5913 if (! flag_ms_extensions
)
5915 tree name
= DECL_NAME (fn
);
5916 if (!(complain
& tf_error
))
5917 return error_mark_node
;
5918 else if (current_class_type
5919 && TREE_OPERAND (arg
, 0) == current_class_ref
)
5920 /* An expression like &memfn. */
5921 permerror (input_location
, "ISO C++ forbids taking the address of an unqualified"
5922 " or parenthesized non-static member function to form"
5923 " a pointer to member function. Say %<&%T::%D%>",
5926 permerror (input_location
, "ISO C++ forbids taking the address of a bound member"
5927 " function to form a pointer to member function."
5931 arg
= build_offset_ref (base
, fn
, /*address_p=*/true, complain
);
5934 /* Uninstantiated types are all functions. Taking the
5935 address of a function is a no-op, so just return the
5937 if (type_unknown_p (arg
))
5938 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
5940 if (TREE_CODE (arg
) == OFFSET_REF
)
5941 /* We want a pointer to member; bypass all the code for actually taking
5942 the address of something. */
5945 /* Anything not already handled and not a true memory reference
5947 if (TREE_CODE (argtype
) != FUNCTION_TYPE
5948 && TREE_CODE (argtype
) != METHOD_TYPE
)
5950 cp_lvalue_kind kind
= lvalue_kind (arg
);
5951 if (kind
== clk_none
)
5953 if (complain
& tf_error
)
5954 lvalue_error (input_location
, lv_addressof
);
5955 return error_mark_node
;
5957 if (strict_lvalue
&& (kind
& (clk_rvalueref
|clk_class
)))
5959 if (!(complain
& tf_error
))
5960 return error_mark_node
;
5961 /* Make this a permerror because we used to accept it. */
5962 permerror (input_location
, "taking address of rvalue");
5966 if (TYPE_REF_P (argtype
))
5968 tree type
= build_pointer_type (TREE_TYPE (argtype
));
5969 arg
= build1 (CONVERT_EXPR
, type
, arg
);
5972 else if (pedantic
&& DECL_MAIN_P (arg
))
5975 /* Apparently a lot of autoconf scripts for C++ packages do this,
5976 so only complain if -Wpedantic. */
5977 if (complain
& (flag_pedantic_errors
? tf_error
: tf_warning
))
5978 pedwarn (input_location
, OPT_Wpedantic
,
5979 "ISO C++ forbids taking address of function %<::main%>");
5980 else if (flag_pedantic_errors
)
5981 return error_mark_node
;
5984 /* Let &* cancel out to simplify resulting code. */
5985 if (INDIRECT_REF_P (arg
))
5987 arg
= TREE_OPERAND (arg
, 0);
5988 if (TYPE_REF_P (TREE_TYPE (arg
)))
5990 tree type
= build_pointer_type (TREE_TYPE (TREE_TYPE (arg
)));
5991 arg
= build1 (CONVERT_EXPR
, type
, arg
);
5994 /* Don't let this be an lvalue. */
5999 /* Handle complex lvalues (when permitted)
6000 by reduction to simpler cases. */
6001 val
= unary_complex_lvalue (ADDR_EXPR
, arg
);
6005 switch (TREE_CODE (arg
))
6009 case FIX_TRUNC_EXPR
:
6010 /* We should have handled this above in the lvalue_kind check. */
6015 arg
= BASELINK_FUNCTIONS (arg
);
6019 arg
= OVL_FIRST (arg
);
6024 /* Turn a reference to a non-static data member into a
6025 pointer-to-member. */
6030 gcc_assert (PTRMEM_OK_P (arg
));
6032 t
= TREE_OPERAND (arg
, 1);
6033 if (TYPE_REF_P (TREE_TYPE (t
)))
6035 if (complain
& tf_error
)
6036 error ("cannot create pointer to reference member %qD", t
);
6037 return error_mark_node
;
6040 type
= build_ptrmem_type (context_for_name_lookup (t
),
6042 t
= make_ptrmem_cst (type
, TREE_OPERAND (arg
, 1));
6050 if (argtype
!= error_mark_node
)
6051 argtype
= build_pointer_type (argtype
);
6053 if (bitfield_p (arg
))
6055 if (complain
& tf_error
)
6056 error ("attempt to take address of bit-field");
6057 return error_mark_node
;
6060 /* In a template, we are processing a non-dependent expression
6061 so we can just form an ADDR_EXPR with the correct type. */
6062 if (processing_template_decl
|| TREE_CODE (arg
) != COMPONENT_REF
)
6064 if (TREE_CODE (arg
) == FUNCTION_DECL
6065 && !mark_used (arg
, complain
) && !(complain
& tf_error
))
6066 return error_mark_node
;
6067 val
= build_address (arg
);
6068 if (TREE_CODE (arg
) == OFFSET_REF
)
6069 PTRMEM_OK_P (val
) = PTRMEM_OK_P (arg
);
6071 else if (BASELINK_P (TREE_OPERAND (arg
, 1)))
6073 tree fn
= BASELINK_FUNCTIONS (TREE_OPERAND (arg
, 1));
6075 /* We can only get here with a single static member
6077 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
6078 && DECL_STATIC_FUNCTION_P (fn
));
6079 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
6080 return error_mark_node
;
6081 val
= build_address (fn
);
6082 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
6083 /* Do not lose object's side effects. */
6084 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
),
6085 TREE_OPERAND (arg
, 0), val
);
6089 tree object
= TREE_OPERAND (arg
, 0);
6090 tree field
= TREE_OPERAND (arg
, 1);
6091 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6092 (TREE_TYPE (object
), decl_type_context (field
)));
6093 val
= build_address (arg
);
6096 if (TYPE_PTR_P (argtype
)
6097 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
6099 build_ptrmemfunc_type (argtype
);
6100 val
= build_ptrmemfunc (argtype
, val
, 0,
6108 /* Take the address of ARG if it has one, even if it's an rvalue. */
6111 cp_build_addr_expr (tree arg
, tsubst_flags_t complain
)
6113 return cp_build_addr_expr_1 (arg
, 0, complain
);
6116 /* Take the address of ARG, but only if it's an lvalue. */
6119 cp_build_addr_expr_strict (tree arg
, tsubst_flags_t complain
)
6121 return cp_build_addr_expr_1 (arg
, 1, complain
);
6124 /* C++: Must handle pointers to members.
6126 Perhaps type instantiation should be extended to handle conversion
6127 from aggregates to types we don't yet know we want? (Or are those
6128 cases typically errors which should be reported?)
6130 NOCONVERT suppresses the default promotions (such as from short to int). */
6133 cp_build_unary_op (enum tree_code code
, tree xarg
, bool noconvert
,
6134 tsubst_flags_t complain
)
6136 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
6138 location_t location
= cp_expr_loc_or_loc (arg
, input_location
);
6140 const char *errstring
= NULL
;
6142 const char *invalid_op_diag
;
6144 if (!arg
|| error_operand_p (arg
))
6145 return error_mark_node
;
6147 if ((invalid_op_diag
6148 = targetm
.invalid_unary_op ((code
== UNARY_PLUS_EXPR
6153 if (complain
& tf_error
)
6154 error (invalid_op_diag
);
6155 return error_mark_node
;
6160 case UNARY_PLUS_EXPR
:
6163 int flags
= WANT_ARITH
| WANT_ENUM
;
6164 /* Unary plus (but not unary minus) is allowed on pointers. */
6165 if (code
== UNARY_PLUS_EXPR
)
6166 flags
|= WANT_POINTER
;
6167 arg
= build_expr_type_conversion (flags
, arg
, true);
6169 errstring
= (code
== NEGATE_EXPR
6170 ? _("wrong type argument to unary minus")
6171 : _("wrong type argument to unary plus"));
6174 if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
6175 arg
= cp_perform_integral_promotions (arg
, complain
);
6177 /* Make sure the result is not an lvalue: a unary plus or minus
6178 expression is always a rvalue. */
6185 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
6190 arg
= cp_default_conversion (arg
, complain
);
6191 if (arg
== error_mark_node
)
6192 return error_mark_node
;
6195 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
6196 | WANT_VECTOR_OR_COMPLEX
,
6198 errstring
= _("wrong type argument to bit-complement");
6199 else if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
6201 /* Warn if the expression has boolean value. */
6202 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
6203 && (complain
& tf_warning
)
6204 && warning_at (location
, OPT_Wbool_operation
,
6205 "%<~%> on an expression of type bool"))
6206 inform (location
, "did you mean to use logical not (%<!%>)?");
6207 arg
= cp_perform_integral_promotions (arg
, complain
);
6209 else if (!noconvert
&& VECTOR_TYPE_P (TREE_TYPE (arg
)))
6210 arg
= mark_rvalue_use (arg
);
6214 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
6215 errstring
= _("wrong type argument to abs");
6216 else if (!noconvert
)
6218 arg
= cp_default_conversion (arg
, complain
);
6219 if (arg
== error_mark_node
)
6220 return error_mark_node
;
6225 /* Conjugating a real value is a no-op, but allow it anyway. */
6226 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
6227 errstring
= _("wrong type argument to conjugation");
6228 else if (!noconvert
)
6230 arg
= cp_default_conversion (arg
, complain
);
6231 if (arg
== error_mark_node
)
6232 return error_mark_node
;
6236 case TRUTH_NOT_EXPR
:
6237 if (VECTOR_TYPE_P (TREE_TYPE (arg
)))
6238 return cp_build_binary_op (input_location
, EQ_EXPR
, arg
,
6239 build_zero_cst (TREE_TYPE (arg
)), complain
);
6240 arg
= perform_implicit_conversion (boolean_type_node
, arg
,
6242 val
= invert_truthvalue_loc (input_location
, arg
);
6243 if (arg
!= error_mark_node
)
6245 errstring
= _("in argument to unary !");
6253 arg
= build_real_imag_expr (input_location
, code
, arg
);
6256 case PREINCREMENT_EXPR
:
6257 case POSTINCREMENT_EXPR
:
6258 case PREDECREMENT_EXPR
:
6259 case POSTDECREMENT_EXPR
:
6260 /* Handle complex lvalues (when permitted)
6261 by reduction to simpler cases. */
6263 val
= unary_complex_lvalue (code
, arg
);
6267 arg
= mark_lvalue_use (arg
);
6269 /* Increment or decrement the real part of the value,
6270 and don't change the imaginary part. */
6271 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
6275 arg
= cp_stabilize_reference (arg
);
6276 real
= cp_build_unary_op (REALPART_EXPR
, arg
, true, complain
);
6277 imag
= cp_build_unary_op (IMAGPART_EXPR
, arg
, true, complain
);
6278 real
= cp_build_unary_op (code
, real
, true, complain
);
6279 if (real
== error_mark_node
|| imag
== error_mark_node
)
6280 return error_mark_node
;
6281 return build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
6285 /* Report invalid types. */
6287 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
6290 if (code
== PREINCREMENT_EXPR
)
6291 errstring
= _("no pre-increment operator for type");
6292 else if (code
== POSTINCREMENT_EXPR
)
6293 errstring
= _("no post-increment operator for type");
6294 else if (code
== PREDECREMENT_EXPR
)
6295 errstring
= _("no pre-decrement operator for type");
6297 errstring
= _("no post-decrement operator for type");
6300 else if (arg
== error_mark_node
)
6301 return error_mark_node
;
6303 /* Report something read-only. */
6305 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
6306 || TREE_READONLY (arg
))
6308 if (complain
& tf_error
)
6309 cxx_readonly_error (location
, arg
,
6310 ((code
== PREINCREMENT_EXPR
6311 || code
== POSTINCREMENT_EXPR
)
6312 ? lv_increment
: lv_decrement
));
6314 return error_mark_node
;
6319 tree declared_type
= unlowered_expr_type (arg
);
6321 argtype
= TREE_TYPE (arg
);
6323 /* ARM $5.2.5 last annotation says this should be forbidden. */
6324 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
6326 if (complain
& tf_error
)
6327 permerror (input_location
, (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
6328 ? G_("ISO C++ forbids incrementing an enum")
6329 : G_("ISO C++ forbids decrementing an enum"));
6331 return error_mark_node
;
6334 /* Compute the increment. */
6336 if (TYPE_PTR_P (argtype
))
6338 tree type
= complete_type (TREE_TYPE (argtype
));
6340 if (!COMPLETE_OR_VOID_TYPE_P (type
))
6342 if (complain
& tf_error
)
6343 error (((code
== PREINCREMENT_EXPR
6344 || code
== POSTINCREMENT_EXPR
))
6345 ? G_("cannot increment a pointer to incomplete type %qT")
6346 : G_("cannot decrement a pointer to incomplete type %qT"),
6347 TREE_TYPE (argtype
));
6349 return error_mark_node
;
6351 else if (!TYPE_PTROB_P (argtype
))
6353 if (complain
& tf_error
)
6354 pedwarn (input_location
, OPT_Wpointer_arith
,
6355 (code
== PREINCREMENT_EXPR
6356 || code
== POSTINCREMENT_EXPR
)
6357 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6358 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6361 return error_mark_node
;
6364 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
6367 inc
= VECTOR_TYPE_P (argtype
)
6368 ? build_one_cst (argtype
)
6371 inc
= cp_convert (argtype
, inc
, complain
);
6373 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6374 need to ask Objective-C to build the increment or decrement
6375 expression for it. */
6376 if (objc_is_property_ref (arg
))
6377 return objc_build_incr_expr_for_property_ref (input_location
, code
,
6380 /* Complain about anything else that is not a true lvalue. */
6381 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
6382 || code
== POSTINCREMENT_EXPR
)
6383 ? lv_increment
: lv_decrement
),
6385 return error_mark_node
;
6387 /* Forbid using -- or ++ in C++17 on `bool'. */
6388 if (TREE_CODE (declared_type
) == BOOLEAN_TYPE
)
6390 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
6392 if (complain
& tf_error
)
6393 error ("use of an operand of type %qT in %<operator--%> "
6394 "is forbidden", boolean_type_node
);
6395 return error_mark_node
;
6399 if (cxx_dialect
>= cxx17
)
6401 if (complain
& tf_error
)
6402 error ("use of an operand of type %qT in "
6403 "%<operator++%> is forbidden in C++17",
6405 return error_mark_node
;
6407 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6408 else if (!in_system_header_at (input_location
))
6409 warning (OPT_Wdeprecated
, "use of an operand of type %qT "
6410 "in %<operator++%> is deprecated",
6413 val
= boolean_increment (code
, arg
);
6415 else if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
6416 /* An rvalue has no cv-qualifiers. */
6417 val
= build2 (code
, cv_unqualified (TREE_TYPE (arg
)), arg
, inc
);
6419 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
6421 TREE_SIDE_EFFECTS (val
) = 1;
6426 /* Note that this operation never does default_conversion
6427 regardless of NOCONVERT. */
6428 return cp_build_addr_expr (arg
, complain
);
6437 argtype
= TREE_TYPE (arg
);
6438 return build1 (code
, argtype
, arg
);
6441 if (complain
& tf_error
)
6442 error ("%s", errstring
);
6443 return error_mark_node
;
6446 /* Hook for the c-common bits that build a unary op. */
6448 build_unary_op (location_t
/*location*/,
6449 enum tree_code code
, tree xarg
, bool noconvert
)
6451 return cp_build_unary_op (code
, xarg
, noconvert
, tf_warning_or_error
);
6454 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
6455 so that it is a valid lvalue even for GENERIC by replacing
6456 (lhs = rhs) with ((lhs = rhs), lhs)
6457 (--lhs) with ((--lhs), lhs)
6458 (++lhs) with ((++lhs), lhs)
6459 and if lhs has side-effects, calling cp_stabilize_reference on it, so
6460 that it can be evaluated multiple times. */
6463 genericize_compound_lvalue (tree lvalue
)
6465 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue
, 0)))
6466 lvalue
= build2 (TREE_CODE (lvalue
), TREE_TYPE (lvalue
),
6467 cp_stabilize_reference (TREE_OPERAND (lvalue
, 0)),
6468 TREE_OPERAND (lvalue
, 1));
6469 return build2 (COMPOUND_EXPR
, TREE_TYPE (TREE_OPERAND (lvalue
, 0)),
6470 lvalue
, TREE_OPERAND (lvalue
, 0));
6473 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6474 for certain kinds of expressions which are not really lvalues
6475 but which we can accept as lvalues.
6477 If ARG is not a kind of expression we can handle, return
6481 unary_complex_lvalue (enum tree_code code
, tree arg
)
6483 /* Inside a template, making these kinds of adjustments is
6484 pointless; we are only concerned with the type of the
6486 if (processing_template_decl
)
6489 /* Handle (a, b) used as an "lvalue". */
6490 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
6492 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 1), false,
6493 tf_warning_or_error
);
6494 return build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6495 TREE_OPERAND (arg
, 0), real_result
);
6498 /* Handle (a ? b : c) used as an "lvalue". */
6499 if (TREE_CODE (arg
) == COND_EXPR
6500 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
6501 return rationalize_conditional_expr (code
, arg
, tf_warning_or_error
);
6503 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6504 if (TREE_CODE (arg
) == MODIFY_EXPR
6505 || TREE_CODE (arg
) == PREINCREMENT_EXPR
6506 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
6507 return unary_complex_lvalue (code
, genericize_compound_lvalue (arg
));
6509 if (code
!= ADDR_EXPR
)
6512 /* Handle (a = b) used as an "lvalue" for `&'. */
6513 if (TREE_CODE (arg
) == MODIFY_EXPR
6514 || TREE_CODE (arg
) == INIT_EXPR
)
6516 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 0), false,
6517 tf_warning_or_error
);
6518 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6520 TREE_NO_WARNING (arg
) = 1;
6524 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
6525 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
6526 || TREE_CODE (arg
) == OFFSET_REF
)
6529 /* We permit compiler to make function calls returning
6530 objects of aggregate type look like lvalues. */
6534 if (TREE_CODE (targ
) == SAVE_EXPR
)
6535 targ
= TREE_OPERAND (targ
, 0);
6537 if (TREE_CODE (targ
) == CALL_EXPR
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (targ
)))
6539 if (TREE_CODE (arg
) == SAVE_EXPR
)
6542 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, tf_warning_or_error
);
6543 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
6546 if (TREE_CODE (arg
) == SAVE_EXPR
&& INDIRECT_REF_P (targ
))
6547 return build3 (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
6548 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
6551 /* Don't let anything else be handled specially. */
6555 /* Mark EXP saying that we need to be able to take the
6556 address of it; it should not be allocated in a register.
6557 Value is true if successful. ARRAY_REF_P is true if this
6558 is for ARRAY_REF construction - in that case we don't want
6559 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6560 it is fine to use ARRAY_REFs for vector subscripts on vector
6563 C++: we do not allow `current_class_ptr' to be addressable. */
6566 cxx_mark_addressable (tree exp
, bool array_ref_p
)
6571 switch (TREE_CODE (x
))
6573 case VIEW_CONVERT_EXPR
:
6575 && TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
6576 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x
, 0))))
6584 x
= TREE_OPERAND (x
, 0);
6588 if (x
== current_class_ptr
)
6590 error ("cannot take the address of %<this%>, which is an rvalue expression");
6591 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later. */
6597 /* Caller should not be trying to mark initialized
6598 constant fields addressable. */
6599 gcc_assert (DECL_LANG_SPECIFIC (x
) == 0
6600 || DECL_IN_AGGR_P (x
) == 0
6602 || DECL_EXTERNAL (x
));
6606 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
6607 && !DECL_ARTIFICIAL (x
))
6609 if (VAR_P (x
) && DECL_HARD_REGISTER (x
))
6612 ("address of explicit register variable %qD requested", x
);
6615 else if (extra_warnings
)
6617 (OPT_Wextra
, "address requested for %qD, which is declared %<register%>", x
);
6619 TREE_ADDRESSABLE (x
) = 1;
6624 TREE_ADDRESSABLE (x
) = 1;
6628 TREE_ADDRESSABLE (x
) = 1;
6632 TREE_ADDRESSABLE (x
) = 1;
6633 cxx_mark_addressable (TREE_OPERAND (x
, 0));
6641 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6644 build_x_conditional_expr (location_t loc
, tree ifexp
, tree op1
, tree op2
,
6645 tsubst_flags_t complain
)
6647 tree orig_ifexp
= ifexp
;
6648 tree orig_op1
= op1
;
6649 tree orig_op2
= op2
;
6652 if (processing_template_decl
)
6654 /* The standard says that the expression is type-dependent if
6655 IFEXP is type-dependent, even though the eventual type of the
6656 expression doesn't dependent on IFEXP. */
6657 if (type_dependent_expression_p (ifexp
)
6658 /* As a GNU extension, the middle operand may be omitted. */
6659 || (op1
&& type_dependent_expression_p (op1
))
6660 || type_dependent_expression_p (op2
))
6661 return build_min_nt_loc (loc
, COND_EXPR
, ifexp
, op1
, op2
);
6662 ifexp
= build_non_dependent_expr (ifexp
);
6664 op1
= build_non_dependent_expr (op1
);
6665 op2
= build_non_dependent_expr (op2
);
6668 expr
= build_conditional_expr (loc
, ifexp
, op1
, op2
, complain
);
6669 if (processing_template_decl
&& expr
!= error_mark_node
)
6671 tree min
= build_min_non_dep (COND_EXPR
, expr
,
6672 orig_ifexp
, orig_op1
, orig_op2
);
6673 expr
= convert_from_reference (min
);
6678 /* Given a list of expressions, return a compound expression
6679 that performs them all and returns the value of the last of them. */
6682 build_x_compound_expr_from_list (tree list
, expr_list_kind exp
,
6683 tsubst_flags_t complain
)
6685 tree expr
= TREE_VALUE (list
);
6687 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
6688 && !CONSTRUCTOR_IS_DIRECT_INIT (expr
))
6690 if (complain
& tf_error
)
6691 pedwarn (cp_expr_loc_or_loc (expr
, input_location
), 0,
6692 "list-initializer for non-class type must not "
6693 "be parenthesized");
6695 return error_mark_node
;
6698 if (TREE_CHAIN (list
))
6700 if (complain
& tf_error
)
6704 permerror (input_location
, "expression list treated as compound "
6705 "expression in initializer");
6708 permerror (input_location
, "expression list treated as compound "
6709 "expression in mem-initializer");
6712 permerror (input_location
, "expression list treated as compound "
6713 "expression in functional cast");
6719 return error_mark_node
;
6721 for (list
= TREE_CHAIN (list
); list
; list
= TREE_CHAIN (list
))
6722 expr
= build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list
)),
6723 expr
, TREE_VALUE (list
), complain
);
6729 /* Like build_x_compound_expr_from_list, but using a VEC. */
6732 build_x_compound_expr_from_vec (vec
<tree
, va_gc
> *vec
, const char *msg
,
6733 tsubst_flags_t complain
)
6735 if (vec_safe_is_empty (vec
))
6737 else if (vec
->length () == 1)
6747 if (complain
& tf_error
)
6748 permerror (input_location
,
6749 "%s expression list treated as compound expression",
6752 return error_mark_node
;
6756 for (ix
= 1; vec
->iterate (ix
, &t
); ++ix
)
6757 expr
= build_x_compound_expr (EXPR_LOCATION (t
), expr
,
6764 /* Handle overloading of the ',' operator when needed. */
6767 build_x_compound_expr (location_t loc
, tree op1
, tree op2
,
6768 tsubst_flags_t complain
)
6771 tree orig_op1
= op1
;
6772 tree orig_op2
= op2
;
6773 tree overload
= NULL_TREE
;
6775 if (processing_template_decl
)
6777 if (type_dependent_expression_p (op1
)
6778 || type_dependent_expression_p (op2
))
6779 return build_min_nt_loc (loc
, COMPOUND_EXPR
, op1
, op2
);
6780 op1
= build_non_dependent_expr (op1
);
6781 op2
= build_non_dependent_expr (op2
);
6784 result
= build_new_op (loc
, COMPOUND_EXPR
, LOOKUP_NORMAL
, op1
, op2
,
6785 NULL_TREE
, &overload
, complain
);
6787 result
= cp_build_compound_expr (op1
, op2
, complain
);
6789 if (processing_template_decl
&& result
!= error_mark_node
)
6791 if (overload
!= NULL_TREE
)
6792 return (build_min_non_dep_op_overload
6793 (COMPOUND_EXPR
, result
, overload
, orig_op1
, orig_op2
));
6795 return build_min_non_dep (COMPOUND_EXPR
, result
, orig_op1
, orig_op2
);
6801 /* Like cp_build_compound_expr, but for the c-common bits. */
6804 build_compound_expr (location_t
/*loc*/, tree lhs
, tree rhs
)
6806 return cp_build_compound_expr (lhs
, rhs
, tf_warning_or_error
);
6809 /* Build a compound expression. */
6812 cp_build_compound_expr (tree lhs
, tree rhs
, tsubst_flags_t complain
)
6814 lhs
= convert_to_void (lhs
, ICV_LEFT_OF_COMMA
, complain
);
6816 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
6817 return error_mark_node
;
6819 if (TREE_CODE (rhs
) == TARGET_EXPR
)
6821 /* If the rhs is a TARGET_EXPR, then build the compound
6822 expression inside the target_expr's initializer. This
6823 helps the compiler to eliminate unnecessary temporaries. */
6824 tree init
= TREE_OPERAND (rhs
, 1);
6826 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), lhs
, init
);
6827 TREE_OPERAND (rhs
, 1) = init
;
6832 if (type_unknown_p (rhs
))
6834 if (complain
& tf_error
)
6835 error ("no context to resolve type of %qE", rhs
);
6836 return error_mark_node
;
6839 return build2 (COMPOUND_EXPR
, TREE_TYPE (rhs
), lhs
, rhs
);
6842 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6843 casts away constness. CAST gives the type of cast. Returns true
6844 if the cast is ill-formed, false if it is well-formed.
6846 ??? This function warns for casting away any qualifier not just
6847 const. We would like to specify exactly what qualifiers are casted
6852 check_for_casting_away_constness (tree src_type
, tree dest_type
,
6853 enum tree_code cast
, tsubst_flags_t complain
)
6855 /* C-style casts are allowed to cast away constness. With
6856 WARN_CAST_QUAL, we still want to issue a warning. */
6857 if (cast
== CAST_EXPR
&& !warn_cast_qual
)
6860 if (!casts_away_constness (src_type
, dest_type
, complain
))
6866 if (complain
& tf_warning
)
6867 warning (OPT_Wcast_qual
,
6868 "cast from type %qT to type %qT casts away qualifiers",
6869 src_type
, dest_type
);
6872 case STATIC_CAST_EXPR
:
6873 if (complain
& tf_error
)
6874 error ("static_cast from type %qT to type %qT casts away qualifiers",
6875 src_type
, dest_type
);
6878 case REINTERPRET_CAST_EXPR
:
6879 if (complain
& tf_error
)
6880 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6881 src_type
, dest_type
);
6889 /* Warns if the cast from expression EXPR to type TYPE is useless. */
6891 maybe_warn_about_useless_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6893 if (warn_useless_cast
6894 && complain
& tf_warning
)
6896 if ((TYPE_REF_P (type
)
6897 && (TYPE_REF_IS_RVALUE (type
)
6898 ? xvalue_p (expr
) : lvalue_p (expr
))
6899 && same_type_p (TREE_TYPE (expr
), TREE_TYPE (type
)))
6900 || same_type_p (TREE_TYPE (expr
), type
))
6901 warning (OPT_Wuseless_cast
, "useless cast to type %q#T", type
);
6905 /* Warns if the cast ignores cv-qualifiers on TYPE. */
6907 maybe_warn_about_cast_ignoring_quals (tree type
, tsubst_flags_t complain
)
6909 if (warn_ignored_qualifiers
6910 && complain
& tf_warning
6911 && !CLASS_TYPE_P (type
)
6912 && (cp_type_quals (type
) & (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)))
6914 warning (OPT_Wignored_qualifiers
, "type qualifiers ignored on cast "
6919 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6920 (another pointer-to-member type in the same hierarchy) and return
6921 the converted expression. If ALLOW_INVERSE_P is permitted, a
6922 pointer-to-derived may be converted to pointer-to-base; otherwise,
6923 only the other direction is permitted. If C_CAST_P is true, this
6924 conversion is taking place as part of a C-style cast. */
6927 convert_ptrmem (tree type
, tree expr
, bool allow_inverse_p
,
6928 bool c_cast_p
, tsubst_flags_t complain
)
6930 if (same_type_p (type
, TREE_TYPE (expr
)))
6933 if (TYPE_PTRDATAMEM_P (type
))
6935 tree obase
= TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr
));
6936 tree nbase
= TYPE_PTRMEM_CLASS_TYPE (type
);
6937 tree delta
= (get_delta_difference
6939 allow_inverse_p
, c_cast_p
, complain
));
6941 if (delta
== error_mark_node
)
6942 return error_mark_node
;
6944 if (!same_type_p (obase
, nbase
))
6946 if (TREE_CODE (expr
) == PTRMEM_CST
)
6947 expr
= cplus_expand_constant (expr
);
6949 tree cond
= cp_build_binary_op (input_location
, EQ_EXPR
, expr
,
6950 build_int_cst (TREE_TYPE (expr
), -1),
6952 tree op1
= build_nop (ptrdiff_type_node
, expr
);
6953 tree op2
= cp_build_binary_op (input_location
, PLUS_EXPR
, op1
, delta
,
6956 expr
= fold_build3_loc (input_location
,
6957 COND_EXPR
, ptrdiff_type_node
, cond
, op1
, op2
);
6960 return build_nop (type
, expr
);
6963 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
,
6964 allow_inverse_p
, c_cast_p
, complain
);
6967 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6968 this static_cast is being attempted as one of the possible casts
6969 allowed by a C-style cast. (In that case, accessibility of base
6970 classes is not considered, and it is OK to cast away
6971 constness.) Return the result of the cast. *VALID_P is set to
6972 indicate whether or not the cast was valid. */
6975 build_static_cast_1 (tree type
, tree expr
, bool c_cast_p
,
6976 bool *valid_p
, tsubst_flags_t complain
)
6982 /* Assume the cast is valid. */
6985 intype
= unlowered_expr_type (expr
);
6987 /* Save casted types in the function's used types hash table. */
6988 used_types_insert (type
);
6990 /* A prvalue of non-class type is cv-unqualified. */
6991 if (!CLASS_TYPE_P (type
))
6992 type
= cv_unqualified (type
);
6994 /* [expr.static.cast]
6996 An lvalue of type "cv1 B", where B is a class type, can be cast
6997 to type "reference to cv2 D", where D is a class derived (clause
6998 _class.derived_) from B, if a valid standard conversion from
6999 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
7000 same cv-qualification as, or greater cv-qualification than, cv1,
7001 and B is not a virtual base class of D. */
7002 /* We check this case before checking the validity of "TYPE t =
7003 EXPR;" below because for this case:
7006 struct D : public B { D(const B&); };
7008 void f() { static_cast<const D&>(b); }
7010 we want to avoid constructing a new D. The standard is not
7011 completely clear about this issue, but our interpretation is
7012 consistent with other compilers. */
7013 if (TYPE_REF_P (type
)
7014 && CLASS_TYPE_P (TREE_TYPE (type
))
7015 && CLASS_TYPE_P (intype
)
7016 && (TYPE_REF_IS_RVALUE (type
) || lvalue_p (expr
))
7017 && DERIVED_FROM_P (intype
, TREE_TYPE (type
))
7018 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype
)),
7019 build_pointer_type (TYPE_MAIN_VARIANT
7020 (TREE_TYPE (type
))),
7023 || at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
7027 if (processing_template_decl
)
7030 /* There is a standard conversion from "D*" to "B*" even if "B"
7031 is ambiguous or inaccessible. If this is really a
7032 static_cast, then we check both for inaccessibility and
7033 ambiguity. However, if this is a static_cast being performed
7034 because the user wrote a C-style cast, then accessibility is
7036 base
= lookup_base (TREE_TYPE (type
), intype
,
7037 c_cast_p
? ba_unique
: ba_check
,
7039 expr
= build_address (expr
);
7041 if (sanitize_flags_p (SANITIZE_VPTR
))
7044 = cp_ubsan_maybe_instrument_downcast (input_location
, type
,
7050 /* Convert from "B*" to "D*". This function will check that "B"
7051 is not a virtual base of "D". Even if we don't have a guarantee
7052 that expr is NULL, if the static_cast is to a reference type,
7053 it is UB if it would be NULL, so omit the non-NULL check. */
7054 expr
= build_base_path (MINUS_EXPR
, expr
, base
,
7055 /*nonnull=*/flag_delete_null_pointer_checks
,
7058 /* Convert the pointer to a reference -- but then remember that
7059 there are no expressions with reference type in C++.
7061 We call rvalue so that there's an actual tree code
7062 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
7063 is a variable with the same type, the conversion would get folded
7064 away, leaving just the variable and causing lvalue_kind to give
7065 the wrong answer. */
7066 expr
= cp_fold_convert (type
, expr
);
7068 /* When -fsanitize=null, make sure to diagnose reference binding to
7069 NULL even when the reference is converted to pointer later on. */
7070 if (sanitize_flags_p (SANITIZE_NULL
)
7071 && TREE_CODE (expr
) == COND_EXPR
7072 && TREE_OPERAND (expr
, 2)
7073 && TREE_CODE (TREE_OPERAND (expr
, 2)) == INTEGER_CST
7074 && TREE_TYPE (TREE_OPERAND (expr
, 2)) == type
)
7075 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr
, 2));
7077 return convert_from_reference (rvalue (expr
));
7080 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
7081 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
7082 if (TYPE_REF_P (type
)
7083 && TYPE_REF_IS_RVALUE (type
)
7084 && (clk
= real_lvalue_p (expr
))
7085 && reference_related_p (TREE_TYPE (type
), intype
)
7086 && (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
7088 if (processing_template_decl
)
7090 if (clk
== clk_ordinary
)
7092 /* Handle the (non-bit-field) lvalue case here by casting to
7093 lvalue reference and then changing it to an rvalue reference.
7094 Casting an xvalue to rvalue reference will be handled by the
7096 tree lref
= cp_build_reference_type (TREE_TYPE (type
), false);
7097 result
= (perform_direct_initialization_if_possible
7098 (lref
, expr
, c_cast_p
, complain
));
7099 result
= build1 (NON_LVALUE_EXPR
, type
, result
);
7100 return convert_from_reference (result
);
7103 /* For a bit-field or packed field, bind to a temporary. */
7104 expr
= rvalue (expr
);
7107 /* Resolve overloaded address here rather than once in
7108 implicit_conversion and again in the inverse code below. */
7109 if (TYPE_PTRMEMFUNC_P (type
) && type_unknown_p (expr
))
7111 expr
= instantiate_type (type
, expr
, complain
);
7112 intype
= TREE_TYPE (expr
);
7115 /* [expr.static.cast]
7117 Any expression can be explicitly converted to type cv void. */
7118 if (VOID_TYPE_P (type
))
7119 return convert_to_void (expr
, ICV_CAST
, complain
);
7122 An abstract class shall not be used ... as the type of an explicit
7124 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
7125 return error_mark_node
;
7127 /* [expr.static.cast]
7129 An expression e can be explicitly converted to a type T using a
7130 static_cast of the form static_cast<T>(e) if the declaration T
7131 t(e);" is well-formed, for some invented temporary variable
7133 result
= perform_direct_initialization_if_possible (type
, expr
,
7134 c_cast_p
, complain
);
7137 if (processing_template_decl
)
7140 result
= convert_from_reference (result
);
7142 /* [expr.static.cast]
7144 If T is a reference type, the result is an lvalue; otherwise,
7145 the result is an rvalue. */
7146 if (!TYPE_REF_P (type
))
7148 result
= rvalue (result
);
7150 if (result
== expr
&& SCALAR_TYPE_P (type
))
7151 /* Leave some record of the cast. */
7152 result
= build_nop (type
, expr
);
7157 /* [expr.static.cast]
7159 The inverse of any standard conversion sequence (clause _conv_),
7160 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
7161 (_conv.array_), function-to-pointer (_conv.func_), and boolean
7162 (_conv.bool_) conversions, can be performed explicitly using
7163 static_cast subject to the restriction that the explicit
7164 conversion does not cast away constness (_expr.const.cast_), and
7165 the following additional rules for specific cases: */
7166 /* For reference, the conversions not excluded are: integral
7167 promotions, floating point promotion, integral conversions,
7168 floating point conversions, floating-integral conversions,
7169 pointer conversions, and pointer to member conversions. */
7172 A value of integral _or enumeration_ type can be explicitly
7173 converted to an enumeration type. */
7174 /* The effect of all that is that any conversion between any two
7175 types which are integral, floating, or enumeration types can be
7177 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7178 || SCALAR_FLOAT_TYPE_P (type
))
7179 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
7180 || SCALAR_FLOAT_TYPE_P (intype
)))
7182 if (processing_template_decl
)
7184 return ocp_convert (type
, expr
, CONV_C_CAST
, LOOKUP_NORMAL
, complain
);
7187 if (TYPE_PTR_P (type
) && TYPE_PTR_P (intype
)
7188 && CLASS_TYPE_P (TREE_TYPE (type
))
7189 && CLASS_TYPE_P (TREE_TYPE (intype
))
7190 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7191 (TREE_TYPE (intype
))),
7192 build_pointer_type (TYPE_MAIN_VARIANT
7193 (TREE_TYPE (type
))),
7198 if (processing_template_decl
)
7202 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
7204 return error_mark_node
;
7205 base
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
7206 c_cast_p
? ba_unique
: ba_check
,
7208 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
7211 if (sanitize_flags_p (SANITIZE_VPTR
))
7214 = cp_ubsan_maybe_instrument_downcast (input_location
, type
,
7220 return cp_fold_convert (type
, expr
);
7223 if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
7224 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
7231 c1
= TYPE_PTRMEM_CLASS_TYPE (intype
);
7232 c2
= TYPE_PTRMEM_CLASS_TYPE (type
);
7234 if (TYPE_PTRDATAMEM_P (type
))
7236 t1
= (build_ptrmem_type
7238 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype
))));
7239 t2
= (build_ptrmem_type
7241 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
7248 if (can_convert (t1
, t2
, complain
) || can_convert (t2
, t1
, complain
))
7251 && check_for_casting_away_constness (intype
, type
,
7254 return error_mark_node
;
7255 if (processing_template_decl
)
7257 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/1,
7258 c_cast_p
, complain
);
7262 /* [expr.static.cast]
7264 An rvalue of type "pointer to cv void" can be explicitly
7265 converted to a pointer to object type. A value of type pointer
7266 to object converted to "pointer to cv void" and back to the
7267 original pointer type will have its original value. */
7268 if (TYPE_PTR_P (intype
)
7269 && VOID_TYPE_P (TREE_TYPE (intype
))
7270 && TYPE_PTROB_P (type
))
7273 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
7275 return error_mark_node
;
7276 if (processing_template_decl
)
7278 return build_nop (type
, expr
);
7282 return error_mark_node
;
7285 /* Return an expression representing static_cast<TYPE>(EXPR). */
7288 build_static_cast (tree type
, tree oexpr
, tsubst_flags_t complain
)
7294 if (type
== error_mark_node
|| expr
== error_mark_node
)
7295 return error_mark_node
;
7297 bool dependent
= (dependent_type_p (type
)
7298 || type_dependent_expression_p (expr
));
7302 expr
= build_min (STATIC_CAST_EXPR
, type
, oexpr
);
7303 /* We don't know if it will or will not have side effects. */
7304 TREE_SIDE_EFFECTS (expr
) = 1;
7305 return convert_from_reference (expr
);
7307 else if (processing_template_decl
)
7308 expr
= build_non_dependent_expr (expr
);
7310 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7311 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7312 if (!TYPE_REF_P (type
)
7313 && TREE_CODE (expr
) == NOP_EXPR
7314 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7315 expr
= TREE_OPERAND (expr
, 0);
7317 result
= build_static_cast_1 (type
, expr
, /*c_cast_p=*/false, &valid_p
,
7321 if (result
!= error_mark_node
)
7323 maybe_warn_about_useless_cast (type
, expr
, complain
);
7324 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7326 if (processing_template_decl
)
7331 if (complain
& tf_error
)
7332 error ("invalid static_cast from type %qT to type %qT",
7333 TREE_TYPE (expr
), type
);
7334 return error_mark_node
;
7337 /* EXPR is an expression with member function or pointer-to-member
7338 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7339 not permitted by ISO C++, but we accept it in some modes. If we
7340 are not in one of those modes, issue a diagnostic. Return the
7341 converted expression. */
7344 convert_member_func_to_ptr (tree type
, tree expr
, tsubst_flags_t complain
)
7349 intype
= TREE_TYPE (expr
);
7350 gcc_assert (TYPE_PTRMEMFUNC_P (intype
)
7351 || TREE_CODE (intype
) == METHOD_TYPE
);
7353 if (!(complain
& tf_warning_or_error
))
7354 return error_mark_node
;
7356 if (pedantic
|| warn_pmf2ptr
)
7357 pedwarn (input_location
, pedantic
? OPT_Wpedantic
: OPT_Wpmf_conversions
,
7358 "converting from %qH to %qI", intype
, type
);
7360 if (TREE_CODE (intype
) == METHOD_TYPE
)
7361 expr
= build_addr_func (expr
, complain
);
7362 else if (TREE_CODE (expr
) == PTRMEM_CST
)
7363 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
7366 decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
), 0);
7367 decl
= build_address (decl
);
7368 expr
= get_member_function_from_ptrfunc (&decl
, expr
, complain
);
7371 if (expr
== error_mark_node
)
7372 return error_mark_node
;
7374 return build_nop (type
, expr
);
7377 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
7378 constexpr evaluation knows to reject it. */
7381 build_nop_reinterpret (tree type
, tree expr
)
7383 tree ret
= build_nop (type
, expr
);
7385 REINTERPRET_CAST_P (ret
) = true;
7389 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7390 If C_CAST_P is true, this reinterpret cast is being done as part of
7391 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7392 indicate whether or not reinterpret_cast was valid. */
7395 build_reinterpret_cast_1 (tree type
, tree expr
, bool c_cast_p
,
7396 bool *valid_p
, tsubst_flags_t complain
)
7400 /* Assume the cast is invalid. */
7404 if (type
== error_mark_node
|| error_operand_p (expr
))
7405 return error_mark_node
;
7407 intype
= TREE_TYPE (expr
);
7409 /* Save casted types in the function's used types hash table. */
7410 used_types_insert (type
);
7412 /* A prvalue of non-class type is cv-unqualified. */
7413 if (!CLASS_TYPE_P (type
))
7414 type
= cv_unqualified (type
);
7416 /* [expr.reinterpret.cast]
7417 A glvalue expression of type T1 can be cast to the type
7418 "reference to T2" if an expression of type "pointer to T1" can be
7419 explicitly converted to the type "pointer to T2" using a
7420 reinterpret_cast. */
7421 if (TYPE_REF_P (type
))
7423 if (TYPE_REF_IS_RVALUE (type
))
7425 if (!obvalue_p (expr
))
7426 /* Perform the temporary materialization conversion. */
7427 expr
= get_target_expr_sfinae (expr
, complain
);
7429 else if (!lvalue_p (expr
))
7431 if (complain
& tf_error
)
7432 error ("invalid cast of an rvalue expression of type "
7435 return error_mark_node
;
7438 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7439 "B" are related class types; the reinterpret_cast does not
7440 adjust the pointer. */
7441 if (TYPE_PTR_P (intype
)
7442 && (complain
& tf_warning
)
7443 && (comptypes (TREE_TYPE (intype
), TREE_TYPE (type
),
7444 COMPARE_BASE
| COMPARE_DERIVED
)))
7445 warning (0, "casting %qT to %qT does not dereference pointer",
7448 expr
= cp_build_addr_expr (expr
, complain
);
7450 if (warn_strict_aliasing
> 2)
7451 strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
7453 if (expr
!= error_mark_node
)
7454 expr
= build_reinterpret_cast_1
7455 (build_pointer_type (TREE_TYPE (type
)), expr
, c_cast_p
,
7457 if (expr
!= error_mark_node
)
7458 /* cp_build_indirect_ref isn't right for rvalue refs. */
7459 expr
= convert_from_reference (fold_convert (type
, expr
));
7463 /* As a G++ extension, we consider conversions from member
7464 functions, and pointers to member functions to
7465 pointer-to-function and pointer-to-void types. If
7466 -Wno-pmf-conversions has not been specified,
7467 convert_member_func_to_ptr will issue an error message. */
7468 if ((TYPE_PTRMEMFUNC_P (intype
)
7469 || TREE_CODE (intype
) == METHOD_TYPE
)
7470 && TYPE_PTR_P (type
)
7471 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
7472 || VOID_TYPE_P (TREE_TYPE (type
))))
7473 return convert_member_func_to_ptr (type
, expr
, complain
);
7475 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7476 array-to-pointer, and function-to-pointer conversions are
7478 expr
= decay_conversion (expr
, complain
);
7480 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7481 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7482 if (TREE_CODE (expr
) == NOP_EXPR
7483 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7484 expr
= TREE_OPERAND (expr
, 0);
7486 if (error_operand_p (expr
))
7487 return error_mark_node
;
7489 intype
= TREE_TYPE (expr
);
7491 /* [expr.reinterpret.cast]
7492 A pointer can be converted to any integral type large enough to
7493 hold it. ... A value of type std::nullptr_t can be converted to
7494 an integral type; the conversion has the same meaning and
7495 validity as a conversion of (void*)0 to the integral type. */
7496 if (CP_INTEGRAL_TYPE_P (type
)
7497 && (TYPE_PTR_P (intype
) || NULLPTR_TYPE_P (intype
)))
7499 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
7501 if (complain
& tf_error
)
7502 permerror (input_location
, "cast from %qH to %qI loses precision",
7505 return error_mark_node
;
7507 if (NULLPTR_TYPE_P (intype
))
7508 return build_int_cst (type
, 0);
7510 /* [expr.reinterpret.cast]
7511 A value of integral or enumeration type can be explicitly
7512 converted to a pointer. */
7513 else if (TYPE_PTR_P (type
) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype
))
7516 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7517 || TYPE_PTR_OR_PTRMEM_P (type
))
7518 && same_type_p (type
, intype
))
7520 return rvalue (expr
);
7521 else if (TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
7523 if ((complain
& tf_warning
)
7524 && !cxx_safe_function_type_cast_p (TREE_TYPE (type
),
7525 TREE_TYPE (intype
)))
7526 warning (OPT_Wcast_function_type
,
7527 "cast between incompatible function types"
7528 " from %qH to %qI", intype
, type
);
7529 return build_nop_reinterpret (type
, expr
);
7531 else if (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
))
7533 if ((complain
& tf_warning
)
7534 && !cxx_safe_function_type_cast_p
7535 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type
)),
7536 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype
))))
7537 warning (OPT_Wcast_function_type
,
7538 "cast between incompatible pointer to member types"
7539 " from %qH to %qI", intype
, type
);
7540 return build_nop_reinterpret (type
, expr
);
7542 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
7543 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
7546 && check_for_casting_away_constness (intype
, type
,
7547 REINTERPRET_CAST_EXPR
,
7549 return error_mark_node
;
7550 /* Warn about possible alignment problems. */
7551 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
7552 && (complain
& tf_warning
)
7553 && !VOID_TYPE_P (type
)
7554 && TREE_CODE (TREE_TYPE (intype
)) != FUNCTION_TYPE
7555 && COMPLETE_TYPE_P (TREE_TYPE (type
))
7556 && COMPLETE_TYPE_P (TREE_TYPE (intype
))
7557 && min_align_of_type (TREE_TYPE (type
))
7558 > min_align_of_type (TREE_TYPE (intype
)))
7559 warning (OPT_Wcast_align
, "cast from %qH to %qI "
7560 "increases required alignment of target type", intype
, type
);
7562 if (warn_strict_aliasing
<= 2)
7563 /* strict_aliasing_warning STRIP_NOPs its expr. */
7564 strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
7566 return build_nop_reinterpret (type
, expr
);
7568 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
7569 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
7571 if (complain
& tf_warning
)
7572 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7573 object pointer type or vice versa is conditionally-supported." */
7574 warning (OPT_Wconditionally_supported
,
7575 "casting between pointer-to-function and pointer-to-object "
7576 "is conditionally-supported");
7577 return build_nop_reinterpret (type
, expr
);
7579 else if (VECTOR_TYPE_P (type
))
7580 return convert_to_vector (type
, expr
);
7581 else if (VECTOR_TYPE_P (intype
)
7582 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
7583 return convert_to_integer_nofold (type
, expr
);
7588 if (complain
& tf_error
)
7589 error ("invalid cast from type %qT to type %qT", intype
, type
);
7590 return error_mark_node
;
7593 expr
= cp_convert (type
, expr
, complain
);
7594 if (TREE_CODE (expr
) == NOP_EXPR
)
7595 /* Mark any nop_expr that created as a reintepret_cast. */
7596 REINTERPRET_CAST_P (expr
) = true;
7601 build_reinterpret_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7605 if (type
== error_mark_node
|| expr
== error_mark_node
)
7606 return error_mark_node
;
7608 if (processing_template_decl
)
7610 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
7612 if (!TREE_SIDE_EFFECTS (t
)
7613 && type_dependent_expression_p (expr
))
7614 /* There might turn out to be side effects inside expr. */
7615 TREE_SIDE_EFFECTS (t
) = 1;
7616 return convert_from_reference (t
);
7619 r
= build_reinterpret_cast_1 (type
, expr
, /*c_cast_p=*/false,
7620 /*valid_p=*/NULL
, complain
);
7621 if (r
!= error_mark_node
)
7623 maybe_warn_about_useless_cast (type
, expr
, complain
);
7624 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7629 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7630 return an appropriate expression. Otherwise, return
7631 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7632 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7633 performing a C-style cast, its value upon return will indicate
7634 whether or not the conversion succeeded. */
7637 build_const_cast_1 (tree dst_type
, tree expr
, tsubst_flags_t complain
,
7641 tree reference_type
;
7643 /* Callers are responsible for handling error_mark_node as a
7644 destination type. */
7645 gcc_assert (dst_type
!= error_mark_node
);
7646 /* In a template, callers should be building syntactic
7647 representations of casts, not using this machinery. */
7648 gcc_assert (!processing_template_decl
);
7650 /* Assume the conversion is invalid. */
7654 if (!INDIRECT_TYPE_P (dst_type
) && !TYPE_PTRDATAMEM_P (dst_type
))
7656 if (complain
& tf_error
)
7657 error ("invalid use of const_cast with type %qT, "
7658 "which is not a pointer, "
7659 "reference, nor a pointer-to-data-member type", dst_type
);
7660 return error_mark_node
;
7663 if (TREE_CODE (TREE_TYPE (dst_type
)) == FUNCTION_TYPE
)
7665 if (complain
& tf_error
)
7666 error ("invalid use of const_cast with type %qT, which is a pointer "
7667 "or reference to a function type", dst_type
);
7668 return error_mark_node
;
7671 /* A prvalue of non-class type is cv-unqualified. */
7672 dst_type
= cv_unqualified (dst_type
);
7674 /* Save casted types in the function's used types hash table. */
7675 used_types_insert (dst_type
);
7677 src_type
= TREE_TYPE (expr
);
7678 /* Expressions do not really have reference types. */
7679 if (TYPE_REF_P (src_type
))
7680 src_type
= TREE_TYPE (src_type
);
7682 /* [expr.const.cast]
7684 For two object types T1 and T2, if a pointer to T1 can be explicitly
7685 converted to the type "pointer to T2" using a const_cast, then the
7686 following conversions can also be made:
7688 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7689 type T2 using the cast const_cast<T2&>;
7691 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7692 type T2 using the cast const_cast<T2&&>; and
7694 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7695 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7697 if (TYPE_REF_P (dst_type
))
7699 reference_type
= dst_type
;
7700 if (!TYPE_REF_IS_RVALUE (dst_type
)
7706 if (complain
& tf_error
)
7707 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7708 src_type
, dst_type
);
7709 return error_mark_node
;
7711 dst_type
= build_pointer_type (TREE_TYPE (dst_type
));
7712 src_type
= build_pointer_type (src_type
);
7716 reference_type
= NULL_TREE
;
7717 /* If the destination type is not a reference type, the
7718 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7719 conversions are performed. */
7720 src_type
= type_decays_to (src_type
);
7721 if (src_type
== error_mark_node
)
7722 return error_mark_node
;
7725 if (TYPE_PTR_P (src_type
) || TYPE_PTRDATAMEM_P (src_type
))
7727 if (comp_ptr_ttypes_const (dst_type
, src_type
))
7732 /* This cast is actually a C-style cast. Issue a warning if
7733 the user is making a potentially unsafe cast. */
7734 check_for_casting_away_constness (src_type
, dst_type
,
7735 CAST_EXPR
, complain
);
7736 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
7737 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
7738 && (complain
& tf_warning
)
7739 && min_align_of_type (TREE_TYPE (dst_type
))
7740 > min_align_of_type (TREE_TYPE (src_type
)))
7741 warning (OPT_Wcast_align
, "cast from %qH to %qI "
7742 "increases required alignment of target type",
7743 src_type
, dst_type
);
7747 expr
= cp_build_addr_expr (expr
, complain
);
7748 if (expr
== error_mark_node
)
7749 return error_mark_node
;
7750 expr
= build_nop (reference_type
, expr
);
7751 return convert_from_reference (expr
);
7755 expr
= decay_conversion (expr
, complain
);
7756 if (expr
== error_mark_node
)
7757 return error_mark_node
;
7759 /* build_c_cast puts on a NOP_EXPR to make the result not an
7760 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7761 non-lvalue context. */
7762 if (TREE_CODE (expr
) == NOP_EXPR
7763 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7764 expr
= TREE_OPERAND (expr
, 0);
7765 return build_nop (dst_type
, expr
);
7769 && !at_least_as_qualified_p (TREE_TYPE (dst_type
),
7770 TREE_TYPE (src_type
)))
7771 check_for_casting_away_constness (src_type
, dst_type
, CAST_EXPR
,
7775 if (complain
& tf_error
)
7776 error ("invalid const_cast from type %qT to type %qT",
7777 src_type
, dst_type
);
7778 return error_mark_node
;
7782 build_const_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7786 if (type
== error_mark_node
|| error_operand_p (expr
))
7787 return error_mark_node
;
7789 if (processing_template_decl
)
7791 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
7793 if (!TREE_SIDE_EFFECTS (t
)
7794 && type_dependent_expression_p (expr
))
7795 /* There might turn out to be side effects inside expr. */
7796 TREE_SIDE_EFFECTS (t
) = 1;
7797 return convert_from_reference (t
);
7800 r
= build_const_cast_1 (type
, expr
, complain
, /*valid_p=*/NULL
);
7801 if (r
!= error_mark_node
)
7803 maybe_warn_about_useless_cast (type
, expr
, complain
);
7804 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7809 /* Like cp_build_c_cast, but for the c-common bits. */
7812 build_c_cast (location_t
/*loc*/, tree type
, tree expr
)
7814 return cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7817 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7818 preserve location information even for tree nodes that don't
7822 build_c_cast (location_t loc
, tree type
, cp_expr expr
)
7824 cp_expr result
= cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7825 result
.set_location (loc
);
7829 /* Build an expression representing an explicit C-style cast to type
7830 TYPE of expression EXPR. */
7833 cp_build_c_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7839 if (type
== error_mark_node
|| error_operand_p (expr
))
7840 return error_mark_node
;
7842 if (processing_template_decl
)
7844 tree t
= build_min (CAST_EXPR
, type
,
7845 tree_cons (NULL_TREE
, value
, NULL_TREE
));
7846 /* We don't know if it will or will not have side effects. */
7847 TREE_SIDE_EFFECTS (t
) = 1;
7848 return convert_from_reference (t
);
7851 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7852 'Class') should always be retained, because this information aids
7853 in method lookup. */
7854 if (objc_is_object_ptr (type
)
7855 && objc_is_object_ptr (TREE_TYPE (expr
)))
7856 return build_nop (type
, expr
);
7858 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7859 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7860 if (!TYPE_REF_P (type
)
7861 && TREE_CODE (value
) == NOP_EXPR
7862 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
7863 value
= TREE_OPERAND (value
, 0);
7865 if (TREE_CODE (type
) == ARRAY_TYPE
)
7867 /* Allow casting from T1* to T2[] because Cfront allows it.
7868 NIHCL uses it. It is not valid ISO C++ however. */
7869 if (TYPE_PTR_P (TREE_TYPE (expr
)))
7871 if (complain
& tf_error
)
7872 permerror (input_location
, "ISO C++ forbids casting to an array type %qT", type
);
7874 return error_mark_node
;
7875 type
= build_pointer_type (TREE_TYPE (type
));
7879 if (complain
& tf_error
)
7880 error ("ISO C++ forbids casting to an array type %qT", type
);
7881 return error_mark_node
;
7885 if (TREE_CODE (type
) == FUNCTION_TYPE
7886 || TREE_CODE (type
) == METHOD_TYPE
)
7888 if (complain
& tf_error
)
7889 error ("invalid cast to function type %qT", type
);
7890 return error_mark_node
;
7893 if (TYPE_PTR_P (type
)
7894 && TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
7895 /* Casting to an integer of smaller size is an error detected elsewhere. */
7896 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (value
))
7897 /* Don't warn about converting any constant. */
7898 && !TREE_CONSTANT (value
))
7899 warning_at (input_location
, OPT_Wint_to_pointer_cast
,
7900 "cast to pointer from integer of different size");
7902 /* A C-style cast can be a const_cast. */
7903 result
= build_const_cast_1 (type
, value
, complain
& tf_warning
,
7907 if (result
!= error_mark_node
)
7909 maybe_warn_about_useless_cast (type
, value
, complain
);
7910 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7915 /* Or a static cast. */
7916 result
= build_static_cast_1 (type
, value
, /*c_cast_p=*/true,
7917 &valid_p
, complain
);
7918 /* Or a reinterpret_cast. */
7920 result
= build_reinterpret_cast_1 (type
, value
, /*c_cast_p=*/true,
7921 &valid_p
, complain
);
7922 /* The static_cast or reinterpret_cast may be followed by a
7925 /* A valid cast may result in errors if, for example, a
7926 conversion to an ambiguous base class is required. */
7927 && !error_operand_p (result
))
7931 maybe_warn_about_useless_cast (type
, value
, complain
);
7932 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7934 /* Non-class rvalues always have cv-unqualified type. */
7935 if (!CLASS_TYPE_P (type
))
7936 type
= TYPE_MAIN_VARIANT (type
);
7937 result_type
= TREE_TYPE (result
);
7938 if (!CLASS_TYPE_P (result_type
) && !TYPE_REF_P (type
))
7939 result_type
= TYPE_MAIN_VARIANT (result_type
);
7940 /* If the type of RESULT does not match TYPE, perform a
7941 const_cast to make it match. If the static_cast or
7942 reinterpret_cast succeeded, we will differ by at most
7943 cv-qualification, so the follow-on const_cast is guaranteed
7945 if (!same_type_p (non_reference (type
), non_reference (result_type
)))
7947 result
= build_const_cast_1 (type
, result
, false, &valid_p
);
7948 gcc_assert (valid_p
);
7953 return error_mark_node
;
7956 /* For use from the C common bits. */
7958 build_modify_expr (location_t location
,
7959 tree lhs
, tree
/*lhs_origtype*/,
7960 enum tree_code modifycode
,
7961 location_t
/*rhs_location*/, tree rhs
,
7962 tree
/*rhs_origtype*/)
7964 return cp_build_modify_expr (location
, lhs
, modifycode
, rhs
,
7965 tf_warning_or_error
);
7968 /* Build an assignment expression of lvalue LHS from value RHS.
7969 MODIFYCODE is the code for a binary operator that we use
7970 to combine the old value of LHS with RHS to get the new value.
7971 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7973 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7976 cp_build_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
7977 tree rhs
, tsubst_flags_t complain
)
7979 lhs
= mark_lvalue_use_nonread (lhs
);
7981 tree result
= NULL_TREE
;
7983 tree lhstype
= TREE_TYPE (lhs
);
7985 tree olhstype
= lhstype
;
7986 bool plain_assign
= (modifycode
== NOP_EXPR
);
7987 bool compound_side_effects_p
= false;
7988 tree preeval
= NULL_TREE
;
7990 /* Avoid duplicate error messages from operands that had errors. */
7991 if (error_operand_p (lhs
) || error_operand_p (rhs
))
7992 return error_mark_node
;
7994 while (TREE_CODE (lhs
) == COMPOUND_EXPR
)
7996 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
7997 compound_side_effects_p
= true;
7998 lhs
= TREE_OPERAND (lhs
, 1);
8001 /* Handle control structure constructs used as "lvalues". Note that we
8002 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
8003 switch (TREE_CODE (lhs
))
8005 /* Handle --foo = 5; as these are valid constructs in C++. */
8006 case PREDECREMENT_EXPR
:
8007 case PREINCREMENT_EXPR
:
8008 if (compound_side_effects_p
)
8009 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
8010 lhs
= genericize_compound_lvalue (lhs
);
8012 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
8013 and looked through the COMPOUND_EXPRs, readd them now around
8014 the resulting lhs. */
8015 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
8017 lhs
= build2 (COMPOUND_EXPR
, lhstype
, TREE_OPERAND (olhs
, 0), lhs
);
8018 tree
*ptr
= &TREE_OPERAND (lhs
, 1);
8019 for (olhs
= TREE_OPERAND (olhs
, 1);
8020 TREE_CODE (olhs
) == COMPOUND_EXPR
;
8021 olhs
= TREE_OPERAND (olhs
, 1))
8023 *ptr
= build2 (COMPOUND_EXPR
, lhstype
,
8024 TREE_OPERAND (olhs
, 0), *ptr
);
8025 ptr
= &TREE_OPERAND (*ptr
, 1);
8031 if (compound_side_effects_p
)
8032 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
8033 lhs
= genericize_compound_lvalue (lhs
);
8034 goto maybe_add_compound
;
8038 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
8039 when neither operand has side-effects. */
8040 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8041 return error_mark_node
;
8043 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0))
8044 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 1)));
8046 lhs
= build3 (COND_EXPR
, TREE_TYPE (lhs
),
8047 build2 (TREE_CODE (lhs
) == MIN_EXPR
? LE_EXPR
: GE_EXPR
,
8049 TREE_OPERAND (lhs
, 0),
8050 TREE_OPERAND (lhs
, 1)),
8051 TREE_OPERAND (lhs
, 0),
8052 TREE_OPERAND (lhs
, 1));
8055 /* Handle (a ? b : c) used as an "lvalue". */
8058 /* Produce (a ? (b = rhs) : (c = rhs))
8059 except that the RHS goes through a save-expr
8060 so the code to compute it is only emitted once. */
8063 if (VOID_TYPE_P (TREE_TYPE (rhs
)))
8065 if (complain
& tf_error
)
8066 error ("void value not ignored as it ought to be");
8067 return error_mark_node
;
8070 rhs
= stabilize_expr (rhs
, &preeval
);
8072 /* Check this here to avoid odd errors when trying to convert
8073 a throw to the type of the COND_EXPR. */
8074 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8075 return error_mark_node
;
8077 cond
= build_conditional_expr
8078 (input_location
, TREE_OPERAND (lhs
, 0),
8079 cp_build_modify_expr (loc
, TREE_OPERAND (lhs
, 1),
8080 modifycode
, rhs
, complain
),
8081 cp_build_modify_expr (loc
, TREE_OPERAND (lhs
, 2),
8082 modifycode
, rhs
, complain
),
8085 if (cond
== error_mark_node
)
8087 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
8088 and looked through the COMPOUND_EXPRs, readd them now around
8089 the resulting cond before adding the preevaluated rhs. */
8090 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
8092 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
8093 TREE_OPERAND (olhs
, 0), cond
);
8094 tree
*ptr
= &TREE_OPERAND (cond
, 1);
8095 for (olhs
= TREE_OPERAND (olhs
, 1);
8096 TREE_CODE (olhs
) == COMPOUND_EXPR
;
8097 olhs
= TREE_OPERAND (olhs
, 1))
8099 *ptr
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
8100 TREE_OPERAND (olhs
, 0), *ptr
);
8101 ptr
= &TREE_OPERAND (*ptr
, 1);
8104 /* Make sure the code to compute the rhs comes out
8105 before the split. */
8115 if (modifycode
== INIT_EXPR
)
8117 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
8118 /* Do the default thing. */;
8119 else if (TREE_CODE (rhs
) == CONSTRUCTOR
)
8121 /* Compound literal. */
8122 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
8123 /* Call convert to generate an error; see PR 11063. */
8124 rhs
= convert (lhstype
, rhs
);
8125 result
= build2 (INIT_EXPR
, lhstype
, lhs
, rhs
);
8126 TREE_SIDE_EFFECTS (result
) = 1;
8129 else if (! MAYBE_CLASS_TYPE_P (lhstype
))
8130 /* Do the default thing. */;
8133 vec
<tree
, va_gc
> *rhs_vec
= make_tree_vector_single (rhs
);
8134 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
8135 &rhs_vec
, lhstype
, LOOKUP_NORMAL
,
8137 release_tree_vector (rhs_vec
);
8138 if (result
== NULL_TREE
)
8139 return error_mark_node
;
8145 lhs
= require_complete_type_sfinae (lhs
, complain
);
8146 if (lhs
== error_mark_node
)
8147 return error_mark_node
;
8149 if (modifycode
== NOP_EXPR
)
8151 if (c_dialect_objc ())
8153 result
= objc_maybe_build_modify_expr (lhs
, rhs
);
8158 /* `operator=' is not an inheritable operator. */
8159 if (! MAYBE_CLASS_TYPE_P (lhstype
))
8160 /* Do the default thing. */;
8163 result
= build_new_op (input_location
, MODIFY_EXPR
,
8164 LOOKUP_NORMAL
, lhs
, rhs
,
8165 make_node (NOP_EXPR
), /*overload=*/NULL
,
8167 if (result
== NULL_TREE
)
8168 return error_mark_node
;
8175 tree init
= NULL_TREE
;
8177 /* A binary op has been requested. Combine the old LHS
8178 value with the RHS producing the value we should actually
8179 store into the LHS. */
8180 gcc_assert (!((TYPE_REF_P (lhstype
)
8181 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype
)))
8182 || MAYBE_CLASS_TYPE_P (lhstype
)));
8184 /* Preevaluate the RHS to make sure its evaluation is complete
8185 before the lvalue-to-rvalue conversion of the LHS:
8187 [expr.ass] With respect to an indeterminately-sequenced
8188 function call, the operation of a compound assignment is a
8189 single evaluation. [ Note: Therefore, a function call shall
8190 not intervene between the lvalue-to-rvalue conversion and the
8191 side effect associated with any single compound assignment
8192 operator. -- end note ] */
8193 lhs
= cp_stabilize_reference (lhs
);
8194 rhs
= decay_conversion (rhs
, complain
);
8195 if (rhs
== error_mark_node
)
8196 return error_mark_node
;
8197 rhs
= stabilize_expr (rhs
, &init
);
8198 newrhs
= cp_build_binary_op (loc
, modifycode
, lhs
, rhs
, complain
);
8199 if (newrhs
== error_mark_node
)
8201 if (complain
& tf_error
)
8202 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode
,
8203 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
8204 return error_mark_node
;
8208 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), init
, newrhs
);
8210 /* Now it looks like a plain assignment. */
8211 modifycode
= NOP_EXPR
;
8212 if (c_dialect_objc ())
8214 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
8219 gcc_assert (!TYPE_REF_P (lhstype
));
8220 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs
)));
8223 /* The left-hand side must be an lvalue. */
8224 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8225 return error_mark_node
;
8227 /* Warn about modifying something that is `const'. Don't warn if
8228 this is initialization. */
8229 if (modifycode
!= INIT_EXPR
8230 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
8231 /* Functions are not modifiable, even though they are
8233 || TREE_CODE (TREE_TYPE (lhs
)) == FUNCTION_TYPE
8234 || TREE_CODE (TREE_TYPE (lhs
)) == METHOD_TYPE
8235 /* If it's an aggregate and any field is const, then it is
8236 effectively const. */
8237 || (CLASS_TYPE_P (lhstype
)
8238 && C_TYPE_FIELDS_READONLY (lhstype
))))
8240 if (complain
& tf_error
)
8241 cxx_readonly_error (loc
, lhs
, lv_assign
);
8242 return error_mark_node
;
8245 /* If storing into a structure or union member, it may have been given a
8246 lowered bitfield type. We need to convert to the declared type first,
8247 so retrieve it now. */
8249 olhstype
= unlowered_expr_type (lhs
);
8251 /* Convert new value to destination type. */
8253 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
8257 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs
))
8259 if (modifycode
!= INIT_EXPR
)
8261 if (complain
& tf_error
)
8262 error ("assigning to an array from an initializer list");
8263 return error_mark_node
;
8265 if (check_array_initializer (lhs
, lhstype
, newrhs
))
8266 return error_mark_node
;
8267 newrhs
= digest_init (lhstype
, newrhs
, complain
);
8268 if (newrhs
== error_mark_node
)
8269 return error_mark_node
;
8272 /* C++11 8.5/17: "If the destination type is an array of characters,
8273 an array of char16_t, an array of char32_t, or an array of wchar_t,
8274 and the initializer is a string literal...". */
8275 else if (TREE_CODE (newrhs
) == STRING_CST
8276 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype
)))
8277 && modifycode
== INIT_EXPR
)
8279 newrhs
= digest_init (lhstype
, newrhs
, complain
);
8280 if (newrhs
== error_mark_node
)
8281 return error_mark_node
;
8284 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
8285 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))))
8287 if (complain
& tf_error
)
8288 error ("incompatible types in assignment of %qT to %qT",
8289 TREE_TYPE (rhs
), lhstype
);
8290 return error_mark_node
;
8293 /* Allow array assignment in compiler-generated code. */
8294 else if (!current_function_decl
8295 || !DECL_DEFAULTED_FN (current_function_decl
))
8297 /* This routine is used for both initialization and assignment.
8298 Make sure the diagnostic message differentiates the context. */
8299 if (complain
& tf_error
)
8301 if (modifycode
== INIT_EXPR
)
8302 error ("array used as initializer");
8304 error ("invalid array assignment");
8306 return error_mark_node
;
8309 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
8310 ? 1 + (modifycode
!= INIT_EXPR
): 0;
8311 result
= build_vec_init (lhs
, NULL_TREE
, newrhs
,
8312 /*explicit_value_init_p=*/false,
8313 from_array
, complain
);
8317 if (modifycode
== INIT_EXPR
)
8318 /* Calls with INIT_EXPR are all direct-initialization, so don't set
8319 LOOKUP_ONLYCONVERTING. */
8320 newrhs
= convert_for_initialization (lhs
, olhstype
, newrhs
, LOOKUP_NORMAL
,
8321 ICR_INIT
, NULL_TREE
, 0,
8324 newrhs
= convert_for_assignment (olhstype
, newrhs
, ICR_ASSIGN
,
8325 NULL_TREE
, 0, complain
, LOOKUP_IMPLICIT
);
8327 if (!same_type_p (lhstype
, olhstype
))
8328 newrhs
= cp_convert_and_check (lhstype
, newrhs
, complain
);
8330 if (modifycode
!= INIT_EXPR
)
8332 if (TREE_CODE (newrhs
) == CALL_EXPR
8333 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
8334 newrhs
= build_cplus_new (lhstype
, newrhs
, complain
);
8336 /* Can't initialize directly from a TARGET_EXPR, since that would
8337 cause the lhs to be constructed twice, and possibly result in
8338 accidental self-initialization. So we force the TARGET_EXPR to be
8339 expanded without a target. */
8340 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
8341 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
8342 TREE_OPERAND (newrhs
, 0));
8345 if (newrhs
== error_mark_node
)
8346 return error_mark_node
;
8348 if (c_dialect_objc () && flag_objc_gc
)
8350 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
8356 result
= build2 (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
8357 lhstype
, lhs
, newrhs
);
8359 TREE_SIDE_EFFECTS (result
) = 1;
8361 TREE_NO_WARNING (result
) = 1;
8365 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), preeval
, result
);
8370 build_x_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
8371 tree rhs
, tsubst_flags_t complain
)
8373 tree orig_lhs
= lhs
;
8374 tree orig_rhs
= rhs
;
8375 tree overload
= NULL_TREE
;
8376 tree op
= build_nt (modifycode
, NULL_TREE
, NULL_TREE
);
8378 if (processing_template_decl
)
8380 if (modifycode
== NOP_EXPR
8381 || type_dependent_expression_p (lhs
)
8382 || type_dependent_expression_p (rhs
))
8383 return build_min_nt_loc (loc
, MODOP_EXPR
, lhs
,
8384 build_min_nt_loc (loc
, modifycode
, NULL_TREE
,
8387 lhs
= build_non_dependent_expr (lhs
);
8388 rhs
= build_non_dependent_expr (rhs
);
8391 if (modifycode
!= NOP_EXPR
)
8393 tree rval
= build_new_op (loc
, MODIFY_EXPR
, LOOKUP_NORMAL
,
8394 lhs
, rhs
, op
, &overload
, complain
);
8397 if (rval
== error_mark_node
)
8399 TREE_NO_WARNING (rval
) = 1;
8400 if (processing_template_decl
)
8402 if (overload
!= NULL_TREE
)
8403 return (build_min_non_dep_op_overload
8404 (MODIFY_EXPR
, rval
, overload
, orig_lhs
, orig_rhs
));
8406 return (build_min_non_dep
8407 (MODOP_EXPR
, rval
, orig_lhs
, op
, orig_rhs
));
8412 return cp_build_modify_expr (loc
, lhs
, modifycode
, rhs
, complain
);
8415 /* Helper function for get_delta_difference which assumes FROM is a base
8416 class of TO. Returns a delta for the conversion of pointer-to-member
8417 of FROM to pointer-to-member of TO. If the conversion is invalid and
8418 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8419 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8420 If C_CAST_P is true, this conversion is taking place as part of a
8424 get_delta_difference_1 (tree from
, tree to
, bool c_cast_p
,
8425 tsubst_flags_t complain
)
8430 binfo
= lookup_base (to
, from
, c_cast_p
? ba_unique
: ba_check
,
8433 if (binfo
== error_mark_node
)
8435 if (!(complain
& tf_error
))
8436 return error_mark_node
;
8438 error (" in pointer to member function conversion");
8439 return size_zero_node
;
8443 if (kind
!= bk_via_virtual
)
8444 return BINFO_OFFSET (binfo
);
8446 /* FROM is a virtual base class of TO. Issue an error or warning
8447 depending on whether or not this is a reinterpret cast. */
8449 if (!(complain
& tf_error
))
8450 return error_mark_node
;
8452 error ("pointer to member conversion via virtual base %qT",
8453 BINFO_TYPE (binfo_from_vbase (binfo
)));
8455 return size_zero_node
;
8462 /* Get difference in deltas for different pointer to member function
8463 types. If the conversion is invalid and tf_error is not set in
8464 COMPLAIN, returns error_mark_node, otherwise returns an integer
8465 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8466 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8467 conversions as well. If C_CAST_P is true this conversion is taking
8468 place as part of a C-style cast.
8470 Note that the naming of FROM and TO is kind of backwards; the return
8471 value is what we add to a TO in order to get a FROM. They are named
8472 this way because we call this function to find out how to convert from
8473 a pointer to member of FROM to a pointer to member of TO. */
8476 get_delta_difference (tree from
, tree to
,
8477 bool allow_inverse_p
,
8478 bool c_cast_p
, tsubst_flags_t complain
)
8482 if (same_type_ignoring_top_level_qualifiers_p (from
, to
))
8483 /* Pointer to member of incomplete class is permitted*/
8484 result
= size_zero_node
;
8486 result
= get_delta_difference_1 (from
, to
, c_cast_p
, complain
);
8488 if (result
== error_mark_node
)
8489 return error_mark_node
;
8493 if (!allow_inverse_p
)
8495 if (!(complain
& tf_error
))
8496 return error_mark_node
;
8498 error_not_base_type (from
, to
);
8499 error (" in pointer to member conversion");
8500 result
= size_zero_node
;
8504 result
= get_delta_difference_1 (to
, from
, c_cast_p
, complain
);
8506 if (result
== error_mark_node
)
8507 return error_mark_node
;
8510 result
= size_diffop_loc (input_location
,
8511 size_zero_node
, result
);
8514 if (!(complain
& tf_error
))
8515 return error_mark_node
;
8517 error_not_base_type (from
, to
);
8518 error (" in pointer to member conversion");
8519 result
= size_zero_node
;
8524 return convert_to_integer (ptrdiff_type_node
, result
);
8527 /* Return a constructor for the pointer-to-member-function TYPE using
8528 the other components as specified. */
8531 build_ptrmemfunc1 (tree type
, tree delta
, tree pfn
)
8536 vec
<constructor_elt
, va_gc
> *v
;
8538 /* Pull the FIELD_DECLs out of the type. */
8539 pfn_field
= TYPE_FIELDS (type
);
8540 delta_field
= DECL_CHAIN (pfn_field
);
8542 /* Make sure DELTA has the type we want. */
8543 delta
= convert_and_check (input_location
, delta_type_node
, delta
);
8545 /* Convert to the correct target type if necessary. */
8546 pfn
= fold_convert (TREE_TYPE (pfn_field
), pfn
);
8548 /* Finish creating the initializer. */
8550 CONSTRUCTOR_APPEND_ELT(v
, pfn_field
, pfn
);
8551 CONSTRUCTOR_APPEND_ELT(v
, delta_field
, delta
);
8552 u
= build_constructor (type
, v
);
8553 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) & TREE_CONSTANT (delta
);
8554 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
8555 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
8557 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
8562 /* Build a constructor for a pointer to member function. It can be
8563 used to initialize global variables, local variable, or used
8564 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8567 If FORCE is nonzero, then force this conversion, even if
8568 we would rather not do it. Usually set when using an explicit
8569 cast. A C-style cast is being processed iff C_CAST_P is true.
8571 Return error_mark_node, if something goes wrong. */
8574 build_ptrmemfunc (tree type
, tree pfn
, int force
, bool c_cast_p
,
8575 tsubst_flags_t complain
)
8581 if (error_operand_p (pfn
))
8582 return error_mark_node
;
8584 pfn_type
= TREE_TYPE (pfn
);
8585 to_type
= build_ptrmemfunc_type (type
);
8587 /* Handle multiple conversions of pointer to member functions. */
8588 if (TYPE_PTRMEMFUNC_P (pfn_type
))
8590 tree delta
= NULL_TREE
;
8591 tree npfn
= NULL_TREE
;
8595 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
,
8596 LOOKUP_NORMAL
, complain
))
8598 if (complain
& tf_error
)
8599 error ("invalid conversion to type %qT from type %qT",
8602 return error_mark_node
;
8605 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
8606 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
8608 c_cast_p
, complain
);
8609 if (n
== error_mark_node
)
8610 return error_mark_node
;
8612 /* We don't have to do any conversion to convert a
8613 pointer-to-member to its own type. But, we don't want to
8614 just return a PTRMEM_CST if there's an explicit cast; that
8615 cast should make the expression an invalid template argument. */
8616 if (TREE_CODE (pfn
) != PTRMEM_CST
)
8618 if (same_type_p (to_type
, pfn_type
))
8620 else if (integer_zerop (n
) && TREE_CODE (pfn
) != CONSTRUCTOR
)
8621 return build_reinterpret_cast (to_type
, pfn
,
8625 if (TREE_SIDE_EFFECTS (pfn
))
8626 pfn
= save_expr (pfn
);
8628 /* Obtain the function pointer and the current DELTA. */
8629 if (TREE_CODE (pfn
) == PTRMEM_CST
)
8630 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
8633 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
8634 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
8637 /* Just adjust the DELTA field. */
8638 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8639 (TREE_TYPE (delta
), ptrdiff_type_node
));
8640 if (!integer_zerop (n
))
8642 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
8643 n
= cp_build_binary_op (input_location
,
8644 LSHIFT_EXPR
, n
, integer_one_node
,
8646 delta
= cp_build_binary_op (input_location
,
8647 PLUS_EXPR
, delta
, n
, complain
);
8649 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
8652 /* Handle null pointer to member function conversions. */
8653 if (null_ptr_cst_p (pfn
))
8655 pfn
= cp_build_c_cast (type
, pfn
, complain
);
8656 return build_ptrmemfunc1 (to_type
,
8661 if (type_unknown_p (pfn
))
8662 return instantiate_type (type
, pfn
, complain
);
8664 fn
= TREE_OPERAND (pfn
, 0);
8665 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
8666 /* In a template, we will have preserved the
8668 || (processing_template_decl
&& TREE_CODE (fn
) == OFFSET_REF
));
8669 return make_ptrmem_cst (to_type
, fn
);
8672 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8675 ??? There is no consistency as to the types returned for the above
8676 values. Some code acts as if it were a sizetype and some as if it were
8677 integer_type_node. */
8680 expand_ptrmemfunc_cst (tree cst
, tree
*delta
, tree
*pfn
)
8682 tree type
= TREE_TYPE (cst
);
8683 tree fn
= PTRMEM_CST_MEMBER (cst
);
8684 tree ptr_class
, fn_class
;
8686 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
8688 /* The class that the function belongs to. */
8689 fn_class
= DECL_CONTEXT (fn
);
8691 /* The class that we're creating a pointer to member of. */
8692 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
8694 /* First, calculate the adjustment to the function's class. */
8695 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0,
8696 /*c_cast_p=*/0, tf_warning_or_error
);
8698 if (!DECL_VIRTUAL_P (fn
))
8699 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
),
8700 build_addr_func (fn
, tf_warning_or_error
));
8703 /* If we're dealing with a virtual function, we have to adjust 'this'
8704 again, to point to the base which provides the vtable entry for
8705 fn; the call will do the opposite adjustment. */
8706 tree orig_class
= DECL_CONTEXT (fn
);
8707 tree binfo
= binfo_or_else (orig_class
, fn_class
);
8708 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
8709 *delta
, BINFO_OFFSET (binfo
));
8711 /* We set PFN to the vtable offset at which the function can be
8712 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8713 case delta is shifted left, and then incremented). */
8714 *pfn
= DECL_VINDEX (fn
);
8715 *pfn
= fold_build2 (MULT_EXPR
, integer_type_node
, *pfn
,
8716 TYPE_SIZE_UNIT (vtable_entry_type
));
8718 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
8720 case ptrmemfunc_vbit_in_pfn
:
8721 *pfn
= fold_build2 (PLUS_EXPR
, integer_type_node
, *pfn
,
8725 case ptrmemfunc_vbit_in_delta
:
8726 *delta
= fold_build2 (LSHIFT_EXPR
, TREE_TYPE (*delta
),
8727 *delta
, integer_one_node
);
8728 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
8729 *delta
, integer_one_node
);
8736 *pfn
= fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), *pfn
);
8740 /* Return an expression for PFN from the pointer-to-member function
8744 pfn_from_ptrmemfunc (tree t
)
8746 if (TREE_CODE (t
) == PTRMEM_CST
)
8751 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
8756 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
8759 /* Return an expression for DELTA from the pointer-to-member function
8763 delta_from_ptrmemfunc (tree t
)
8765 if (TREE_CODE (t
) == PTRMEM_CST
)
8770 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
8775 return build_ptrmemfunc_access_expr (t
, delta_identifier
);
8778 /* Convert value RHS to type TYPE as preparation for an assignment to
8779 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8780 implicit conversion is. If FNDECL is non-NULL, we are doing the
8781 conversion in order to pass the PARMNUMth argument of FNDECL.
8782 If FNDECL is NULL, we are doing the conversion in function pointer
8783 argument passing, conversion in initialization, etc. */
8786 convert_for_assignment (tree type
, tree rhs
,
8787 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
8788 tsubst_flags_t complain
, int flags
)
8791 enum tree_code coder
;
8793 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8794 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
8795 rhs
= TREE_OPERAND (rhs
, 0);
8797 /* Handle [dcl.init.list] direct-list-initialization from
8798 single element of enumeration with a fixed underlying type. */
8799 if (is_direct_enum_init (type
, rhs
))
8801 tree elt
= CONSTRUCTOR_ELT (rhs
, 0)->value
;
8802 if (check_narrowing (ENUM_UNDERLYING_TYPE (type
), elt
, complain
))
8804 warning_sentinel
w (warn_useless_cast
);
8805 warning_sentinel
w2 (warn_ignored_qualifiers
);
8806 rhs
= cp_build_c_cast (type
, elt
, complain
);
8809 rhs
= error_mark_node
;
8812 rhstype
= TREE_TYPE (rhs
);
8813 coder
= TREE_CODE (rhstype
);
8815 if (VECTOR_TYPE_P (type
) && coder
== VECTOR_TYPE
8816 && vector_types_convertible_p (type
, rhstype
, true))
8818 rhs
= mark_rvalue_use (rhs
);
8819 return convert (type
, rhs
);
8822 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
8823 return error_mark_node
;
8824 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
8825 return error_mark_node
;
8827 /* The RHS of an assignment cannot have void type. */
8828 if (coder
== VOID_TYPE
)
8830 if (complain
& tf_error
)
8831 error ("void value not ignored as it ought to be");
8832 return error_mark_node
;
8835 if (c_dialect_objc ())
8839 tree rname
= fndecl
;
8850 selector
= objc_message_selector ();
8852 if (selector
&& parmno
> 1)
8859 if (objc_compare_types (type
, rhstype
, parmno
, rname
))
8861 rhs
= mark_rvalue_use (rhs
);
8862 return convert (type
, rhs
);
8868 The expression is implicitly converted (clause _conv_) to the
8869 cv-unqualified type of the left operand.
8871 We allow bad conversions here because by the time we get to this point
8872 we are committed to doing the conversion. If we end up doing a bad
8873 conversion, convert_like will complain. */
8874 if (!can_convert_arg_bad (type
, rhstype
, rhs
, flags
, complain
))
8876 /* When -Wno-pmf-conversions is use, we just silently allow
8877 conversions from pointers-to-members to plain pointers. If
8878 the conversion doesn't work, cp_convert will complain. */
8880 && TYPE_PTR_P (type
)
8881 && TYPE_PTRMEMFUNC_P (rhstype
))
8882 rhs
= cp_convert (strip_top_quals (type
), rhs
, complain
);
8885 if (complain
& tf_error
)
8887 /* If the right-hand side has unknown type, then it is an
8888 overloaded function. Call instantiate_type to get error
8890 if (rhstype
== unknown_type_node
)
8892 tree r
= instantiate_type (type
, rhs
, tf_warning_or_error
);
8893 /* -fpermissive might allow this; recurse. */
8895 return convert_for_assignment (type
, r
, errtype
, fndecl
,
8896 parmnum
, complain
, flags
);
8899 complain_about_bad_argument (cp_expr_location (rhs
),
8905 case ICR_DEFAULT_ARGUMENT
:
8906 error ("cannot convert %qH to %qI in default argument",
8910 error ("cannot convert %qH to %qI in argument passing",
8913 case ICR_CONVERTING
:
8914 error ("cannot convert %qH to %qI",
8918 error ("cannot convert %qH to %qI in initialization",
8922 error ("cannot convert %qH to %qI in return",
8926 error ("cannot convert %qH to %qI in assignment",
8932 if (TYPE_PTR_P (rhstype
)
8933 && TYPE_PTR_P (type
)
8934 && CLASS_TYPE_P (TREE_TYPE (rhstype
))
8935 && CLASS_TYPE_P (TREE_TYPE (type
))
8936 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype
)))
8937 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8938 (TREE_TYPE (rhstype
))),
8939 "class type %qT is incomplete", TREE_TYPE (rhstype
));
8941 return error_mark_node
;
8944 if (warn_suggest_attribute_format
)
8946 const enum tree_code codel
= TREE_CODE (type
);
8947 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
8949 && check_missing_format_attribute (type
, rhstype
)
8950 && (complain
& tf_warning
))
8954 case ICR_DEFAULT_ARGUMENT
:
8956 warning (OPT_Wsuggest_attribute_format
,
8957 "parameter %qP of %qD might be a candidate "
8958 "for a format attribute", parmnum
, fndecl
);
8960 warning (OPT_Wsuggest_attribute_format
,
8961 "parameter might be a candidate "
8962 "for a format attribute");
8964 case ICR_CONVERTING
:
8965 warning (OPT_Wsuggest_attribute_format
,
8966 "target of conversion might be a candidate "
8967 "for a format attribute");
8970 warning (OPT_Wsuggest_attribute_format
,
8971 "target of initialization might be a candidate "
8972 "for a format attribute");
8975 warning (OPT_Wsuggest_attribute_format
,
8976 "return type might be a candidate "
8977 "for a format attribute");
8980 warning (OPT_Wsuggest_attribute_format
,
8981 "left-hand side of assignment might be a candidate "
8982 "for a format attribute");
8989 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8991 if (warn_parentheses
8992 && TREE_CODE (type
) == BOOLEAN_TYPE
8993 && TREE_CODE (rhs
) == MODIFY_EXPR
8994 && !TREE_NO_WARNING (rhs
)
8995 && TREE_CODE (TREE_TYPE (rhs
)) != BOOLEAN_TYPE
8996 && (complain
& tf_warning
))
8998 location_t loc
= cp_expr_loc_or_loc (rhs
, input_location
);
9000 warning_at (loc
, OPT_Wparentheses
,
9001 "suggest parentheses around assignment used as truth value");
9002 TREE_NO_WARNING (rhs
) = 1;
9005 return perform_implicit_conversion_flags (strip_top_quals (type
), rhs
,
9009 /* Convert RHS to be of type TYPE.
9010 If EXP is nonzero, it is the target of the initialization.
9011 ERRTYPE indicates what kind of error the implicit conversion is.
9013 Two major differences between the behavior of
9014 `convert_for_assignment' and `convert_for_initialization'
9015 are that references are bashed in the former, while
9016 copied in the latter, and aggregates are assigned in
9017 the former (operator=) while initialized in the
9020 If using constructor make sure no conversion operator exists, if one does
9021 exist, an ambiguity exists. */
9024 convert_for_initialization (tree exp
, tree type
, tree rhs
, int flags
,
9025 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
9026 tsubst_flags_t complain
)
9028 enum tree_code codel
= TREE_CODE (type
);
9030 enum tree_code coder
;
9032 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9033 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
9034 if (TREE_CODE (rhs
) == NOP_EXPR
9035 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
9036 && codel
!= REFERENCE_TYPE
)
9037 rhs
= TREE_OPERAND (rhs
, 0);
9039 if (type
== error_mark_node
9040 || rhs
== error_mark_node
9041 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
9042 return error_mark_node
;
9044 if (MAYBE_CLASS_TYPE_P (non_reference (type
)))
9046 else if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
9047 && TREE_CODE (type
) != ARRAY_TYPE
9048 && (!TYPE_REF_P (type
)
9049 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
9050 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
9051 && !TYPE_REFFN_P (type
))
9052 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
9053 rhs
= decay_conversion (rhs
, complain
);
9055 rhstype
= TREE_TYPE (rhs
);
9056 coder
= TREE_CODE (rhstype
);
9058 if (coder
== ERROR_MARK
)
9059 return error_mark_node
;
9061 /* We accept references to incomplete types, so we can
9062 return here before checking if RHS is of complete type. */
9064 if (codel
== REFERENCE_TYPE
)
9066 /* This should eventually happen in convert_arguments. */
9067 int savew
= 0, savee
= 0;
9070 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
9071 rhs
= initialize_reference (type
, rhs
, flags
, complain
);
9074 && (warningcount
+ werrorcount
> savew
|| errorcount
> savee
))
9075 inform (DECL_SOURCE_LOCATION (fndecl
),
9076 "in passing argument %P of %qD", parmnum
, fndecl
);
9082 exp
= require_complete_type_sfinae (exp
, complain
);
9083 if (exp
== error_mark_node
)
9084 return error_mark_node
;
9086 rhstype
= non_reference (rhstype
);
9088 type
= complete_type (type
);
9090 if (DIRECT_INIT_EXPR_P (type
, rhs
))
9091 /* Don't try to do copy-initialization if we already have
9092 direct-initialization. */
9095 if (MAYBE_CLASS_TYPE_P (type
))
9096 return perform_implicit_conversion_flags (type
, rhs
, complain
, flags
);
9098 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
,
9102 /* If RETVAL is the address of, or a reference to, a local variable or
9103 temporary give an appropriate warning and return true. */
9106 maybe_warn_about_returning_address_of_local (tree retval
)
9108 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
9109 tree whats_returned
= fold_for_warn (retval
);
9110 location_t loc
= cp_expr_loc_or_loc (retval
, input_location
);
9114 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
9115 whats_returned
= TREE_OPERAND (whats_returned
, 1);
9116 else if (CONVERT_EXPR_P (whats_returned
)
9117 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
)
9118 whats_returned
= TREE_OPERAND (whats_returned
, 0);
9123 if (TREE_CODE (whats_returned
) == TARGET_EXPR
9124 && is_std_init_list (TREE_TYPE (whats_returned
)))
9126 tree init
= TARGET_EXPR_INITIAL (whats_returned
);
9127 if (TREE_CODE (init
) == CONSTRUCTOR
)
9128 /* Pull out the array address. */
9129 whats_returned
= CONSTRUCTOR_ELT (init
, 0)->value
;
9130 else if (TREE_CODE (init
) == INDIRECT_REF
)
9131 /* The source of a trivial copy looks like *(T*)&var. */
9132 whats_returned
= TREE_OPERAND (init
, 0);
9135 STRIP_NOPS (whats_returned
);
9138 /* As a special case, we handle a call to std::move or std::forward. */
9139 if (TREE_CODE (whats_returned
) == CALL_EXPR
9140 && (is_std_move_p (whats_returned
)
9141 || is_std_forward_p (whats_returned
)))
9143 tree arg
= CALL_EXPR_ARG (whats_returned
, 0);
9144 return maybe_warn_about_returning_address_of_local (arg
);
9147 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
9149 whats_returned
= TREE_OPERAND (whats_returned
, 0);
9151 while (TREE_CODE (whats_returned
) == COMPONENT_REF
9152 || TREE_CODE (whats_returned
) == ARRAY_REF
)
9153 whats_returned
= TREE_OPERAND (whats_returned
, 0);
9155 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
9156 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
9158 if (TYPE_REF_P (valtype
))
9159 warning_at (loc
, OPT_Wreturn_local_addr
,
9160 "returning reference to temporary");
9161 else if (is_std_init_list (valtype
))
9162 warning_at (loc
, OPT_Winit_list_lifetime
,
9163 "returning temporary initializer_list does not extend "
9164 "the lifetime of the underlying array");
9168 if (DECL_P (whats_returned
)
9169 && DECL_NAME (whats_returned
)
9170 && DECL_FUNCTION_SCOPE_P (whats_returned
)
9171 && !is_capture_proxy (whats_returned
)
9172 && !(TREE_STATIC (whats_returned
)
9173 || TREE_PUBLIC (whats_returned
)))
9175 if (VAR_P (whats_returned
)
9176 && DECL_DECOMPOSITION_P (whats_returned
)
9177 && DECL_DECOMP_BASE (whats_returned
)
9178 && DECL_HAS_VALUE_EXPR_P (whats_returned
))
9180 /* When returning address of a structured binding, if the structured
9181 binding is not a reference, continue normally, if it is a
9182 reference, recurse on the initializer of the structured
9184 tree base
= DECL_DECOMP_BASE (whats_returned
);
9185 if (TYPE_REF_P (TREE_TYPE (base
)))
9187 tree init
= DECL_INITIAL (base
);
9188 return maybe_warn_about_returning_address_of_local (init
);
9192 auto_diagnostic_group d
;
9193 if (TYPE_REF_P (valtype
))
9194 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
9195 "reference to local variable %qD returned",
9197 else if (is_std_init_list (valtype
))
9198 w
= warning_at (loc
, OPT_Winit_list_lifetime
,
9199 "returning local initializer_list variable %qD "
9200 "does not extend the lifetime of the underlying array",
9202 else if (TREE_CODE (whats_returned
) == LABEL_DECL
)
9203 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
9204 "address of label %qD returned",
9207 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
9208 "address of local variable %qD returned",
9211 inform (DECL_SOURCE_LOCATION (whats_returned
),
9219 /* Returns true if DECL is in the std namespace. */
9222 decl_in_std_namespace_p (tree decl
)
9224 return (decl
!= NULL_TREE
9225 && DECL_NAMESPACE_STD_P (decl_namespace_context (decl
)));
9228 /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
9231 is_std_forward_p (tree fn
)
9233 /* std::forward only takes one argument. */
9234 if (call_expr_nargs (fn
) != 1)
9237 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
9238 if (!decl_in_std_namespace_p (fndecl
))
9241 tree name
= DECL_NAME (fndecl
);
9242 return name
&& id_equal (name
, "forward");
9245 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
9248 is_std_move_p (tree fn
)
9250 /* std::move only takes one argument. */
9251 if (call_expr_nargs (fn
) != 1)
9254 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
9255 if (!decl_in_std_namespace_p (fndecl
))
9258 tree name
= DECL_NAME (fndecl
);
9259 return name
&& id_equal (name
, "move");
9262 /* Returns true if RETVAL is a good candidate for the NRVO as per
9263 [class.copy.elision]. FUNCTYPE is the type the function is declared
9267 can_do_nrvo_p (tree retval
, tree functype
)
9269 tree result
= DECL_RESULT (current_function_decl
);
9270 return (retval
!= NULL_TREE
9271 && !processing_template_decl
9272 /* Must be a local, automatic variable. */
9274 && DECL_CONTEXT (retval
) == current_function_decl
9275 && !TREE_STATIC (retval
)
9276 /* And not a lambda or anonymous union proxy. */
9277 && !DECL_HAS_VALUE_EXPR_P (retval
)
9278 && (DECL_ALIGN (retval
) <= DECL_ALIGN (result
))
9279 /* The cv-unqualified type of the returned value must be the
9280 same as the cv-unqualified return type of the
9282 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval
))),
9283 (TYPE_MAIN_VARIANT (functype
)))
9284 /* And the returned value must be non-volatile. */
9285 && !TYPE_VOLATILE (TREE_TYPE (retval
)));
9288 /* Returns true if we should treat RETVAL, an expression being returned,
9289 as if it were designated by an rvalue. See [class.copy.elision].
9290 PARM_P is true if a function parameter is OK in this context. */
9293 treat_lvalue_as_rvalue_p (tree retval
, bool parm_ok
)
9295 return ((cxx_dialect
!= cxx98
)
9296 && ((VAR_P (retval
) && !DECL_HAS_VALUE_EXPR_P (retval
))
9297 || (parm_ok
&& TREE_CODE (retval
) == PARM_DECL
))
9298 && DECL_CONTEXT (retval
) == current_function_decl
9299 && !TREE_STATIC (retval
));
9302 /* Warn about wrong usage of std::move in a return statement. RETVAL
9303 is the expression we are returning; FUNCTYPE is the type the function
9304 is declared to return. */
9307 maybe_warn_pessimizing_move (tree retval
, tree functype
)
9309 if (!(warn_pessimizing_move
|| warn_redundant_move
))
9312 location_t loc
= cp_expr_loc_or_loc (retval
, input_location
);
9314 /* C++98 doesn't know move. */
9315 if (cxx_dialect
< cxx11
)
9318 /* Wait until instantiation time, since we can't gauge if we should do
9319 the NRVO until then. */
9320 if (processing_template_decl
)
9323 /* This is only interesting for class types. */
9324 if (!CLASS_TYPE_P (functype
))
9327 /* We're looking for *std::move<T&> (&arg). */
9328 if (REFERENCE_REF_P (retval
)
9329 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
9331 tree fn
= TREE_OPERAND (retval
, 0);
9332 if (is_std_move_p (fn
))
9334 tree arg
= CALL_EXPR_ARG (fn
, 0);
9336 if (TREE_CODE (arg
) == ADDR_EXPR
)
9337 arg
= TREE_OPERAND (arg
, 0);
9338 arg
= convert_from_reference (arg
);
9339 /* Warn if we could do copy elision were it not for the move. */
9340 if (can_do_nrvo_p (arg
, functype
))
9342 auto_diagnostic_group d
;
9343 if (warning_at (loc
, OPT_Wpessimizing_move
,
9344 "moving a local object in a return statement "
9345 "prevents copy elision"))
9346 inform (loc
, "remove %<std::move%> call");
9348 /* Warn if the move is redundant. It is redundant when we would
9349 do maybe-rvalue overload resolution even without std::move. */
9350 else if (treat_lvalue_as_rvalue_p (arg
, /*parm_ok*/true))
9352 auto_diagnostic_group d
;
9353 if (warning_at (loc
, OPT_Wredundant_move
,
9354 "redundant move in return statement"))
9355 inform (loc
, "remove %<std::move%> call");
9361 /* Check that returning RETVAL from the current function is valid.
9362 Return an expression explicitly showing all conversions required to
9363 change RETVAL into the function return type, and to assign it to
9364 the DECL_RESULT for the function. Set *NO_WARNING to true if
9365 code reaches end of non-void function warning shouldn't be issued
9366 on this RETURN_EXPR. */
9369 check_return_expr (tree retval
, bool *no_warning
)
9372 /* The type actually returned by the function. */
9374 /* The type the function is declared to return, or void if
9375 the declared type is incomplete. */
9377 int fn_returns_value_p
;
9379 *no_warning
= false;
9381 /* A `volatile' function is one that isn't supposed to return, ever.
9382 (This is a G++ extension, used to get better code for functions
9383 that call the `volatile' function.) */
9384 if (TREE_THIS_VOLATILE (current_function_decl
))
9385 warning (0, "function declared %<noreturn%> has a %<return%> statement");
9387 /* Check for various simple errors. */
9388 if (DECL_DESTRUCTOR_P (current_function_decl
))
9391 error ("returning a value from a destructor");
9394 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
9396 if (in_function_try_handler
)
9397 /* If a return statement appears in a handler of the
9398 function-try-block of a constructor, the program is ill-formed. */
9399 error ("cannot return from a handler of a function-try-block of a constructor");
9401 /* You can't return a value from a constructor. */
9402 error ("returning a value from a constructor");
9406 const tree saved_retval
= retval
;
9408 if (processing_template_decl
)
9410 current_function_returns_value
= 1;
9412 if (check_for_bare_parameter_packs (retval
))
9413 return error_mark_node
;
9415 /* If one of the types might be void, we can't tell whether we're
9416 returning a value. */
9417 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl
)))
9418 && !current_function_auto_return_pattern
)
9419 || (retval
!= NULL_TREE
9420 && (TREE_TYPE (retval
) == NULL_TREE
9421 || WILDCARD_TYPE_P (TREE_TYPE (retval
)))))
9425 functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
9427 /* Deduce auto return type from a return statement. */
9428 if (current_function_auto_return_pattern
)
9433 if (!retval
&& !is_auto (current_function_auto_return_pattern
))
9435 /* Give a helpful error message. */
9436 error ("return-statement with no value, in function returning %qT",
9437 current_function_auto_return_pattern
);
9438 inform (input_location
, "only plain %<auto%> return type can be "
9439 "deduced to %<void%>");
9440 type
= error_mark_node
;
9442 else if (retval
&& BRACE_ENCLOSED_INITIALIZER_P (retval
))
9444 error ("returning initializer list");
9445 type
= error_mark_node
;
9451 auto_node
= type_uses_auto (current_function_auto_return_pattern
);
9452 type
= do_auto_deduction (current_function_auto_return_pattern
,
9456 if (type
== error_mark_node
)
9458 else if (functype
== current_function_auto_return_pattern
)
9459 apply_deduced_return_type (current_function_decl
, type
);
9460 else if (!same_type_p (type
, functype
))
9462 if (LAMBDA_FUNCTION_P (current_function_decl
))
9463 error ("inconsistent types %qT and %qT deduced for "
9464 "lambda return type", functype
, type
);
9466 error ("inconsistent deduction for auto return type: "
9467 "%qT and then %qT", functype
, type
);
9472 result
= DECL_RESULT (current_function_decl
);
9473 valtype
= TREE_TYPE (result
);
9474 gcc_assert (valtype
!= NULL_TREE
);
9475 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
9477 /* Check for a return statement with no return value in a function
9478 that's supposed to return a value. */
9479 if (!retval
&& fn_returns_value_p
)
9481 if (functype
!= error_mark_node
)
9482 permerror (input_location
, "return-statement with no value, in "
9483 "function returning %qT", valtype
);
9484 /* Remember that this function did return. */
9485 current_function_returns_value
= 1;
9486 /* And signal caller that TREE_NO_WARNING should be set on the
9487 RETURN_EXPR to avoid control reaches end of non-void function
9488 warnings in tree-cfg.c. */
9491 /* Check for a return statement with a value in a function that
9492 isn't supposed to return a value. */
9493 else if (retval
&& !fn_returns_value_p
)
9495 if (VOID_TYPE_P (TREE_TYPE (retval
)))
9496 /* You can return a `void' value from a function of `void'
9497 type. In that case, we have to evaluate the expression for
9498 its side-effects. */
9499 finish_expr_stmt (retval
);
9501 permerror (input_location
,
9502 "return-statement with a value, in function "
9503 "returning %qT", valtype
);
9504 current_function_returns_null
= 1;
9506 /* There's really no value to return, after all. */
9510 /* Remember that this function can sometimes return without a
9512 current_function_returns_null
= 1;
9514 /* Remember that this function did return a value. */
9515 current_function_returns_value
= 1;
9517 /* Check for erroneous operands -- but after giving ourselves a
9518 chance to provide an error about returning a value from a void
9520 if (error_operand_p (retval
))
9522 current_function_return_value
= error_mark_node
;
9523 return error_mark_node
;
9526 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
9527 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl
))
9528 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
9530 && retval
&& null_ptr_cst_p (retval
))
9531 warning (0, "%<operator new%> must not return NULL unless it is "
9532 "declared %<throw()%> (or -fcheck-new is in effect)");
9534 /* Effective C++ rule 15. See also start_function. */
9536 && DECL_NAME (current_function_decl
) == assign_op_identifier
9537 && !type_dependent_expression_p (retval
))
9541 /* The function return type must be a reference to the current
9543 if (TYPE_REF_P (valtype
)
9544 && same_type_ignoring_top_level_qualifiers_p
9545 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
)))
9547 /* Returning '*this' is obviously OK. */
9548 if (retval
== current_class_ref
)
9550 /* If we are calling a function whose return type is the same of
9551 the current class reference, it is ok. */
9552 else if (INDIRECT_REF_P (retval
)
9553 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
9558 warning (OPT_Weffc__
, "%<operator=%> should return a reference to %<*this%>");
9561 if (dependent_type_p (functype
)
9562 || type_dependent_expression_p (retval
))
9565 /* We should not have changed the return value. */
9566 gcc_assert (retval
== saved_retval
);
9570 /* The fabled Named Return Value optimization, as per [class.copy]/15:
9572 [...] For a function with a class return type, if the expression
9573 in the return statement is the name of a local object, and the cv-
9574 unqualified type of the local object is the same as the function
9575 return type, an implementation is permitted to omit creating the tem-
9576 porary object to hold the function return value [...]
9578 So, if this is a value-returning function that always returns the same
9579 local variable, remember it.
9581 It might be nice to be more flexible, and choose the first suitable
9582 variable even if the function sometimes returns something else, but
9583 then we run the risk of clobbering the variable we chose if the other
9584 returned expression uses the chosen variable somehow. And people expect
9585 this restriction, anyway. (jason 2000-11-19)
9587 See finish_function and finalize_nrv for the rest of this optimization. */
9589 bool named_return_value_okay_p
= can_do_nrvo_p (retval
, functype
);
9590 if (fn_returns_value_p
&& flag_elide_constructors
)
9592 if (named_return_value_okay_p
9593 && (current_function_return_value
== NULL_TREE
9594 || current_function_return_value
== retval
))
9595 current_function_return_value
= retval
;
9597 current_function_return_value
= error_mark_node
;
9600 /* We don't need to do any conversions when there's nothing being
9605 if (!named_return_value_okay_p
)
9606 maybe_warn_pessimizing_move (retval
, functype
);
9608 /* Do any required conversions. */
9609 if (retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
9610 /* No conversions are required. */
9614 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
9616 /* The functype's return type will have been set to void, if it
9617 was an incomplete type. Just treat this as 'return;' */
9618 if (VOID_TYPE_P (functype
))
9619 return error_mark_node
;
9621 /* If we had an id-expression obfuscated by force_paren_expr, we need
9622 to undo it so we can try to treat it as an rvalue below. */
9623 retval
= maybe_undo_parenthesized_ref (retval
);
9625 if (processing_template_decl
)
9626 retval
= build_non_dependent_expr (retval
);
9628 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9629 treated as an rvalue for the purposes of overload resolution to
9630 favor move constructors over copy constructors.
9632 Note that these conditions are similar to, but not as strict as,
9633 the conditions for the named return value optimization. */
9634 bool converted
= false;
9635 if (treat_lvalue_as_rvalue_p (retval
, /*parm_ok*/true)
9636 /* This is only interesting for class type. */
9637 && CLASS_TYPE_P (functype
))
9639 tree moved
= move (retval
);
9640 moved
= convert_for_initialization
9641 (NULL_TREE
, functype
, moved
, flags
|LOOKUP_PREFER_RVALUE
,
9642 ICR_RETURN
, NULL_TREE
, 0, tf_none
);
9643 if (moved
!= error_mark_node
)
9650 /* First convert the value to the function's return type, then
9651 to the type of return value's location to handle the
9652 case that functype is smaller than the valtype. */
9654 retval
= convert_for_initialization
9655 (NULL_TREE
, functype
, retval
, flags
, ICR_RETURN
, NULL_TREE
, 0,
9656 tf_warning_or_error
);
9657 retval
= convert (valtype
, retval
);
9659 /* If the conversion failed, treat this just like `return;'. */
9660 if (retval
== error_mark_node
)
9662 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9663 else if (! cfun
->returns_struct
9664 && TREE_CODE (retval
) == TARGET_EXPR
9665 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
9666 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
9667 TREE_OPERAND (retval
, 0));
9668 else if (!processing_template_decl
9669 && maybe_warn_about_returning_address_of_local (retval
)
9670 && INDIRECT_TYPE_P (valtype
))
9671 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
9672 build_zero_cst (TREE_TYPE (retval
)));
9675 if (processing_template_decl
)
9676 return saved_retval
;
9678 /* Actually copy the value returned into the appropriate location. */
9679 if (retval
&& retval
!= result
)
9680 retval
= build2 (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
9686 /* Returns nonzero if the pointer-type FROM can be converted to the
9687 pointer-type TO via a qualification conversion. If CONSTP is -1,
9688 then we return nonzero if the pointers are similar, and the
9689 cv-qualification signature of FROM is a proper subset of that of TO.
9691 If CONSTP is positive, then all outer pointers have been
9695 comp_ptr_ttypes_real (tree to
, tree from
, int constp
)
9697 bool to_more_cv_qualified
= false;
9698 bool is_opaque_pointer
= false;
9700 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9702 if (TREE_CODE (to
) != TREE_CODE (from
))
9705 if (TREE_CODE (from
) == OFFSET_TYPE
9706 && !same_type_p (TYPE_OFFSET_BASETYPE (from
),
9707 TYPE_OFFSET_BASETYPE (to
)))
9710 /* Const and volatile mean something different for function types,
9711 so the usual checks are not appropriate. */
9712 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
9714 if (!at_least_as_qualified_p (to
, from
))
9717 if (!at_least_as_qualified_p (from
, to
))
9721 to_more_cv_qualified
= true;
9725 constp
&= TYPE_READONLY (to
);
9728 if (VECTOR_TYPE_P (to
))
9729 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
9731 if (!TYPE_PTR_P (to
) && !TYPE_PTRDATAMEM_P (to
))
9732 return ((constp
>= 0 || to_more_cv_qualified
)
9733 && (is_opaque_pointer
9734 || same_type_ignoring_top_level_qualifiers_p (to
, from
)));
9738 /* When comparing, say, char ** to char const **, this function takes
9739 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9740 types to this function. */
9743 comp_ptr_ttypes (tree to
, tree from
)
9745 return comp_ptr_ttypes_real (to
, from
, 1);
9748 /* Returns true iff FNTYPE is a non-class type that involves
9749 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9750 if a parameter type is ill-formed. */
9753 error_type_p (const_tree type
)
9757 switch (TREE_CODE (type
))
9763 case REFERENCE_TYPE
:
9765 return error_type_p (TREE_TYPE (type
));
9769 if (error_type_p (TREE_TYPE (type
)))
9771 for (t
= TYPE_ARG_TYPES (type
); t
; t
= TREE_CHAIN (t
))
9772 if (error_type_p (TREE_VALUE (t
)))
9777 if (TYPE_PTRMEMFUNC_P (type
))
9778 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type
));
9786 /* Returns true if to and from are (possibly multi-level) pointers to the same
9787 type or inheritance-related types, regardless of cv-quals. */
9790 ptr_reasonably_similar (const_tree to
, const_tree from
)
9792 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9794 /* Any target type is similar enough to void. */
9795 if (VOID_TYPE_P (to
))
9796 return !error_type_p (from
);
9797 if (VOID_TYPE_P (from
))
9798 return !error_type_p (to
);
9800 if (TREE_CODE (to
) != TREE_CODE (from
))
9803 if (TREE_CODE (from
) == OFFSET_TYPE
9804 && comptypes (TYPE_OFFSET_BASETYPE (to
),
9805 TYPE_OFFSET_BASETYPE (from
),
9806 COMPARE_BASE
| COMPARE_DERIVED
))
9809 if (VECTOR_TYPE_P (to
)
9810 && vector_types_convertible_p (to
, from
, false))
9813 if (TREE_CODE (to
) == INTEGER_TYPE
9814 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
9817 if (TREE_CODE (to
) == FUNCTION_TYPE
)
9818 return !error_type_p (to
) && !error_type_p (from
);
9820 if (!TYPE_PTR_P (to
))
9822 /* When either type is incomplete avoid DERIVED_FROM_P,
9823 which may call complete_type (c++/57942). */
9824 bool b
= !COMPLETE_TYPE_P (to
) || !COMPLETE_TYPE_P (from
);
9826 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
9827 b
? COMPARE_STRICT
: COMPARE_BASE
| COMPARE_DERIVED
);
9832 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9833 pointer-to-member types) are the same, ignoring cv-qualification at
9837 comp_ptr_ttypes_const (tree to
, tree from
)
9839 bool is_opaque_pointer
= false;
9841 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9843 if (TREE_CODE (to
) != TREE_CODE (from
))
9846 if (TREE_CODE (from
) == OFFSET_TYPE
9847 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
9848 TYPE_OFFSET_BASETYPE (to
)))
9851 if (VECTOR_TYPE_P (to
))
9852 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
9854 if (!TYPE_PTR_P (to
))
9855 return (is_opaque_pointer
9856 || same_type_ignoring_top_level_qualifiers_p (to
, from
));
9860 /* Returns the type qualifiers for this type, including the qualifiers on the
9861 elements for an array type. */
9864 cp_type_quals (const_tree type
)
9867 /* This CONST_CAST is okay because strip_array_types returns its
9868 argument unmodified and we assign it to a const_tree. */
9869 type
= strip_array_types (CONST_CAST_TREE (type
));
9870 if (type
== error_mark_node
9871 /* Quals on a FUNCTION_TYPE are memfn quals. */
9872 || TREE_CODE (type
) == FUNCTION_TYPE
)
9873 return TYPE_UNQUALIFIED
;
9874 quals
= TYPE_QUALS (type
);
9875 /* METHOD and REFERENCE_TYPEs should never have quals. */
9876 gcc_assert ((TREE_CODE (type
) != METHOD_TYPE
9877 && !TYPE_REF_P (type
))
9878 || ((quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
))
9879 == TYPE_UNQUALIFIED
));
9883 /* Returns the function-ref-qualifier for TYPE */
9886 type_memfn_rqual (const_tree type
)
9888 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
9889 || TREE_CODE (type
) == METHOD_TYPE
);
9891 if (!FUNCTION_REF_QUALIFIED (type
))
9892 return REF_QUAL_NONE
;
9893 else if (FUNCTION_RVALUE_QUALIFIED (type
))
9894 return REF_QUAL_RVALUE
;
9896 return REF_QUAL_LVALUE
;
9899 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9903 type_memfn_quals (const_tree type
)
9905 if (TREE_CODE (type
) == FUNCTION_TYPE
)
9906 return TYPE_QUALS (type
);
9907 else if (TREE_CODE (type
) == METHOD_TYPE
)
9908 return cp_type_quals (class_of_this_parm (type
));
9913 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9914 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9917 apply_memfn_quals (tree type
, cp_cv_quals memfn_quals
, cp_ref_qualifier rqual
)
9919 /* Could handle METHOD_TYPE here if necessary. */
9920 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
9921 if (TYPE_QUALS (type
) == memfn_quals
9922 && type_memfn_rqual (type
) == rqual
)
9925 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9927 tree result
= build_qualified_type (type
, memfn_quals
);
9928 return build_ref_qualified_type (result
, rqual
);
9931 /* Returns nonzero if TYPE is const or volatile. */
9934 cv_qualified_p (const_tree type
)
9936 int quals
= cp_type_quals (type
);
9937 return (quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)) != 0;
9940 /* Returns nonzero if the TYPE contains a mutable member. */
9943 cp_has_mutable_p (const_tree type
)
9945 /* This CONST_CAST is okay because strip_array_types returns its
9946 argument unmodified and we assign it to a const_tree. */
9947 type
= strip_array_types (CONST_CAST_TREE(type
));
9949 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
9952 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9953 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9954 approximation. In particular, consider:
9957 struct S { int i; };
9958 const S s = { f(); }
9960 Here, we will make "s" as TREE_READONLY (because it is declared
9961 "const") -- only to reverse ourselves upon seeing that the
9962 initializer is non-constant. */
9965 cp_apply_type_quals_to_decl (int type_quals
, tree decl
)
9967 tree type
= TREE_TYPE (decl
);
9969 if (type
== error_mark_node
)
9972 if (TREE_CODE (decl
) == TYPE_DECL
)
9975 gcc_assert (!(TREE_CODE (type
) == FUNCTION_TYPE
9976 && type_quals
!= TYPE_UNQUALIFIED
));
9978 /* Avoid setting TREE_READONLY incorrectly. */
9979 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9980 constructor can produce constant init, so rely on cp_finish_decl to
9981 clear TREE_READONLY if the variable has non-constant init. */
9983 /* If the type has (or might have) a mutable component, that component
9984 might be modified. */
9985 if (TYPE_HAS_MUTABLE_P (type
) || !COMPLETE_TYPE_P (type
))
9986 type_quals
&= ~TYPE_QUAL_CONST
;
9988 c_apply_type_quals_to_decl (type_quals
, decl
);
9991 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9992 exemplar types such that casting T1 to T2 is casting away constness
9993 if and only if there is no implicit conversion from T1 to T2. */
9996 casts_away_constness_r (tree
*t1
, tree
*t2
, tsubst_flags_t complain
)
10001 /* [expr.const.cast]
10003 For multi-level pointer to members and multi-level mixed pointers
10004 and pointers to members (conv.qual), the "member" aspect of a
10005 pointer to member level is ignored when determining if a const
10006 cv-qualifier has been cast away. */
10007 /* [expr.const.cast]
10009 For two pointer types:
10011 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
10012 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
10015 casting from X1 to X2 casts away constness if, for a non-pointer
10016 type T there does not exist an implicit conversion (clause
10019 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
10023 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
10024 if ((!TYPE_PTR_P (*t1
) && !TYPE_PTRDATAMEM_P (*t1
))
10025 || (!TYPE_PTR_P (*t2
) && !TYPE_PTRDATAMEM_P (*t2
)))
10027 *t1
= cp_build_qualified_type (void_type_node
,
10028 cp_type_quals (*t1
));
10029 *t2
= cp_build_qualified_type (void_type_node
,
10030 cp_type_quals (*t2
));
10034 quals1
= cp_type_quals (*t1
);
10035 quals2
= cp_type_quals (*t2
);
10037 if (TYPE_PTRDATAMEM_P (*t1
))
10038 *t1
= TYPE_PTRMEM_POINTED_TO_TYPE (*t1
);
10040 *t1
= TREE_TYPE (*t1
);
10041 if (TYPE_PTRDATAMEM_P (*t2
))
10042 *t2
= TYPE_PTRMEM_POINTED_TO_TYPE (*t2
);
10044 *t2
= TREE_TYPE (*t2
);
10046 casts_away_constness_r (t1
, t2
, complain
);
10047 *t1
= build_pointer_type (*t1
);
10048 *t2
= build_pointer_type (*t2
);
10049 *t1
= cp_build_qualified_type (*t1
, quals1
);
10050 *t2
= cp_build_qualified_type (*t2
, quals2
);
10053 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
10056 ??? This function returns non-zero if casting away qualifiers not
10057 just const. We would like to return to the caller exactly which
10058 qualifiers are casted away to give more accurate diagnostics.
10062 casts_away_constness (tree t1
, tree t2
, tsubst_flags_t complain
)
10064 if (TYPE_REF_P (t2
))
10066 /* [expr.const.cast]
10068 Casting from an lvalue of type T1 to an lvalue of type T2
10069 using a reference cast casts away constness if a cast from an
10070 rvalue of type "pointer to T1" to the type "pointer to T2"
10071 casts away constness. */
10072 t1
= (TYPE_REF_P (t1
) ? TREE_TYPE (t1
) : t1
);
10073 return casts_away_constness (build_pointer_type (t1
),
10074 build_pointer_type (TREE_TYPE (t2
)),
10078 if (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
10079 /* [expr.const.cast]
10081 Casting from an rvalue of type "pointer to data member of X
10082 of type T1" to the type "pointer to data member of Y of type
10083 T2" casts away constness if a cast from an rvalue of type
10084 "pointer to T1" to the type "pointer to T2" casts away
10086 return casts_away_constness
10087 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1
)),
10088 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2
)),
10091 /* Casting away constness is only something that makes sense for
10092 pointer or reference types. */
10093 if (!TYPE_PTR_P (t1
) || !TYPE_PTR_P (t2
))
10096 /* Top-level qualifiers don't matter. */
10097 t1
= TYPE_MAIN_VARIANT (t1
);
10098 t2
= TYPE_MAIN_VARIANT (t2
);
10099 casts_away_constness_r (&t1
, &t2
, complain
);
10100 if (!can_convert (t2
, t1
, complain
))
10106 /* If T is a REFERENCE_TYPE return the type to which T refers.
10107 Otherwise, return T itself. */
10110 non_reference (tree t
)
10112 if (t
&& TYPE_REF_P (t
))
10118 /* Return nonzero if REF is an lvalue valid for this language;
10119 otherwise, print an error message and return zero. USE says
10120 how the lvalue is being used and so selects the error message. */
10123 lvalue_or_else (tree ref
, enum lvalue_use use
, tsubst_flags_t complain
)
10125 cp_lvalue_kind kind
= lvalue_kind (ref
);
10127 if (kind
== clk_none
)
10129 if (complain
& tf_error
)
10130 lvalue_error (input_location
, use
);
10133 else if (kind
& (clk_rvalueref
|clk_class
))
10135 if (!(complain
& tf_error
))
10137 /* Make this a permerror because we used to accept it. */
10138 permerror (input_location
, "using rvalue as lvalue");
10143 /* Return true if a user-defined literal operator is a raw operator. */
10146 check_raw_literal_operator (const_tree decl
)
10148 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
10151 bool maybe_raw_p
= false;
10153 /* Count the number and type of arguments and check for ellipsis. */
10154 for (argtype
= argtypes
, arity
= 0;
10155 argtype
&& argtype
!= void_list_node
;
10156 ++arity
, argtype
= TREE_CHAIN (argtype
))
10158 tree t
= TREE_VALUE (argtype
);
10160 if (same_type_p (t
, const_string_type_node
))
10161 maybe_raw_p
= true;
10164 return false; /* Found ellipsis. */
10166 if (!maybe_raw_p
|| arity
!= 1)
10173 /* Return true if a user-defined literal operator has one of the allowed
10177 check_literal_operator_args (const_tree decl
,
10178 bool *long_long_unsigned_p
, bool *long_double_p
)
10180 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
10182 *long_long_unsigned_p
= false;
10183 *long_double_p
= false;
10184 if (processing_template_decl
|| processing_specialization
)
10185 return argtypes
== void_list_node
;
10192 /* Count the number and type of arguments and check for ellipsis. */
10193 for (argtype
= argtypes
, arity
= 0;
10194 argtype
&& argtype
!= void_list_node
;
10195 argtype
= TREE_CHAIN (argtype
))
10197 tree t
= TREE_VALUE (argtype
);
10200 if (TYPE_PTR_P (t
))
10202 bool maybe_raw_p
= false;
10204 if (cp_type_quals (t
) != TYPE_QUAL_CONST
)
10206 t
= TYPE_MAIN_VARIANT (t
);
10207 if ((maybe_raw_p
= same_type_p (t
, char_type_node
))
10208 || same_type_p (t
, wchar_type_node
)
10209 || same_type_p (t
, char16_type_node
)
10210 || same_type_p (t
, char32_type_node
))
10212 argtype
= TREE_CHAIN (argtype
);
10215 t
= TREE_VALUE (argtype
);
10216 if (maybe_raw_p
&& argtype
== void_list_node
)
10218 else if (same_type_p (t
, size_type_node
))
10227 else if (same_type_p (t
, long_long_unsigned_type_node
))
10230 *long_long_unsigned_p
= true;
10232 else if (same_type_p (t
, long_double_type_node
))
10235 *long_double_p
= true;
10237 else if (same_type_p (t
, char_type_node
))
10239 else if (same_type_p (t
, wchar_type_node
))
10241 else if (same_type_p (t
, char16_type_node
))
10243 else if (same_type_p (t
, char32_type_node
))
10249 return false; /* Found ellipsis. */
10251 if (arity
!= max_arity
)
10258 /* Always returns false since unlike C90, C++ has no concept of implicit
10259 function declarations. */
10262 c_decl_implicit (const_tree
)