1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2020 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization. */
29 #include "coretypes.h"
32 #include "stor-layout.h"
36 #include "c-family/c-objc.h"
37 #include "c-family/c-ubsan.h"
38 #include "gcc-rich-location.h"
39 #include "stringpool.h"
44 static tree
cp_build_addr_expr_strict (tree
, tsubst_flags_t
);
45 static tree
cp_build_function_call (tree
, tree
, tsubst_flags_t
);
46 static tree
pfn_from_ptrmemfunc (tree
);
47 static tree
delta_from_ptrmemfunc (tree
);
48 static tree
convert_for_assignment (tree
, tree
, impl_conv_rhs
, tree
, int,
50 static tree
cp_pointer_int_sum (location_t
, enum tree_code
, tree
, tree
,
52 static tree
rationalize_conditional_expr (enum tree_code
, tree
,
54 static bool comp_ptr_ttypes_real (tree
, tree
, int);
55 static bool comp_except_types (tree
, tree
, bool);
56 static bool comp_array_types (const_tree
, const_tree
, compare_bounds_t
, bool);
57 static tree
pointer_diff (location_t
, tree
, tree
, tree
, tsubst_flags_t
, tree
*);
58 static tree
get_delta_difference (tree
, tree
, bool, bool, tsubst_flags_t
);
59 static void casts_away_constness_r (tree
*, tree
*, tsubst_flags_t
);
60 static bool casts_away_constness (tree
, tree
, tsubst_flags_t
);
61 static bool maybe_warn_about_returning_address_of_local (tree
, location_t
= UNKNOWN_LOCATION
);
62 static void error_args_num (location_t
, tree
, bool);
63 static int convert_arguments (tree
, vec
<tree
, va_gc
> **, tree
, int,
65 static bool is_std_move_p (tree
);
66 static bool is_std_forward_p (tree
);
68 /* Do `exp = require_complete_type (exp);' to make sure exp
69 does not have an incomplete type. (That includes void types.)
70 Returns error_mark_node if the VALUE does not have
71 complete type when this function returns. */
74 require_complete_type_sfinae (tree value
, tsubst_flags_t complain
)
78 if (processing_template_decl
|| value
== error_mark_node
)
81 if (TREE_CODE (value
) == OVERLOAD
)
82 type
= unknown_type_node
;
84 type
= TREE_TYPE (value
);
86 if (type
== error_mark_node
)
87 return error_mark_node
;
89 /* First, detect a valid value with a complete type. */
90 if (COMPLETE_TYPE_P (type
))
93 if (complete_type_or_maybe_complain (type
, value
, complain
))
96 return error_mark_node
;
100 require_complete_type (tree value
)
102 return require_complete_type_sfinae (value
, tf_warning_or_error
);
105 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
106 a template instantiation, do the instantiation. Returns TYPE,
107 whether or not it could be completed, unless something goes
108 horribly wrong, in which case the error_mark_node is returned. */
111 complete_type (tree type
)
113 if (type
== NULL_TREE
)
114 /* Rather than crash, we return something sure to cause an error
116 return error_mark_node
;
118 if (type
== error_mark_node
|| COMPLETE_TYPE_P (type
))
120 else if (TREE_CODE (type
) == ARRAY_TYPE
)
122 tree t
= complete_type (TREE_TYPE (type
));
123 unsigned int needs_constructing
, has_nontrivial_dtor
;
124 if (COMPLETE_TYPE_P (t
) && !dependent_type_p (type
))
127 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
129 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
130 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
132 TYPE_NEEDS_CONSTRUCTING (t
) = needs_constructing
;
133 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = has_nontrivial_dtor
;
136 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
137 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
142 /* Like complete_type, but issue an error if the TYPE cannot be completed.
143 VALUE is used for informative diagnostics.
144 Returns NULL_TREE if the type cannot be made complete. */
147 complete_type_or_maybe_complain (tree type
, tree value
, tsubst_flags_t complain
)
149 type
= complete_type (type
);
150 if (type
== error_mark_node
)
151 /* We already issued an error. */
153 else if (!COMPLETE_TYPE_P (type
))
155 if (complain
& tf_error
)
156 cxx_incomplete_type_diagnostic (value
, type
, DK_ERROR
);
164 complete_type_or_else (tree type
, tree value
)
166 return complete_type_or_maybe_complain (type
, value
, tf_warning_or_error
);
170 /* Return the common type of two parameter lists.
171 We assume that comptypes has already been done and returned 1;
172 if that isn't so, this may crash.
174 As an optimization, free the space we allocate if the parameter
175 lists are already common. */
178 commonparms (tree p1
, tree p2
)
180 tree oldargs
= p1
, newargs
, n
;
184 len
= list_length (p1
);
185 newargs
= tree_last (p1
);
187 if (newargs
== void_list_node
)
196 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
201 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
203 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
205 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
208 else if (! TREE_PURPOSE (p1
))
210 if (TREE_PURPOSE (p2
))
212 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
218 if (simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)) != 1)
220 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
222 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
225 TREE_VALUE (n
) = merge_types (TREE_VALUE (p1
), TREE_VALUE (p2
));
228 TREE_VALUE (n
) = TREE_VALUE (p1
);
236 /* Given a type, perhaps copied for a typedef,
237 find the "original" version of it. */
239 original_type (tree t
)
241 int quals
= cp_type_quals (t
);
242 while (t
!= error_mark_node
243 && TYPE_NAME (t
) != NULL_TREE
)
245 tree x
= TYPE_NAME (t
);
246 if (TREE_CODE (x
) != TYPE_DECL
)
248 x
= DECL_ORIGINAL_TYPE (x
);
253 return cp_build_qualified_type (t
, quals
);
256 /* Return the common type for two arithmetic types T1 and T2 under the
257 usual arithmetic conversions. The default conversions have already
258 been applied, and enumerated types converted to their compatible
262 cp_common_type (tree t1
, tree t2
)
264 enum tree_code code1
= TREE_CODE (t1
);
265 enum tree_code code2
= TREE_CODE (t2
);
270 /* In what follows, we slightly generalize the rules given in [expr] so
271 as to deal with `long long' and `complex'. First, merge the
273 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
275 if (SCOPED_ENUM_P (t1
) || SCOPED_ENUM_P (t2
))
277 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
278 return build_type_attribute_variant (t1
, attributes
);
283 /* FIXME: Attributes. */
284 gcc_assert (ARITHMETIC_TYPE_P (t1
)
285 || VECTOR_TYPE_P (t1
)
286 || UNSCOPED_ENUM_P (t1
));
287 gcc_assert (ARITHMETIC_TYPE_P (t2
)
288 || VECTOR_TYPE_P (t2
)
289 || UNSCOPED_ENUM_P (t2
));
291 /* If one type is complex, form the common type of the non-complex
292 components, then make that complex. Use T1 or T2 if it is the
294 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
296 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
297 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
299 = type_after_usual_arithmetic_conversions (subtype1
, subtype2
);
301 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
302 return build_type_attribute_variant (t1
, attributes
);
303 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
304 return build_type_attribute_variant (t2
, attributes
);
306 return build_type_attribute_variant (build_complex_type (subtype
),
310 if (code1
== VECTOR_TYPE
)
312 /* When we get here we should have two vectors of the same size.
313 Just prefer the unsigned one if present. */
314 if (TYPE_UNSIGNED (t1
))
315 return build_type_attribute_variant (t1
, attributes
);
317 return build_type_attribute_variant (t2
, attributes
);
320 /* If only one is real, use it as the result. */
321 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
322 return build_type_attribute_variant (t1
, attributes
);
323 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
324 return build_type_attribute_variant (t2
, attributes
);
326 /* Both real or both integers; use the one with greater precision. */
327 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
328 return build_type_attribute_variant (t1
, attributes
);
329 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
330 return build_type_attribute_variant (t2
, attributes
);
332 /* The types are the same; no need to do anything fancy. */
333 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
334 return build_type_attribute_variant (t1
, attributes
);
336 if (code1
!= REAL_TYPE
)
338 /* If one is unsigned long long, then convert the other to unsigned
340 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_unsigned_type_node
)
341 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_unsigned_type_node
))
342 return build_type_attribute_variant (long_long_unsigned_type_node
,
344 /* If one is a long long, and the other is an unsigned long, and
345 long long can represent all the values of an unsigned long, then
346 convert to a long long. Otherwise, convert to an unsigned long
347 long. Otherwise, if either operand is long long, convert the
350 Since we're here, we know the TYPE_PRECISION is the same;
351 therefore converting to long long cannot represent all the values
352 of an unsigned long, so we choose unsigned long long in that
354 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_integer_type_node
)
355 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_integer_type_node
))
357 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
358 ? long_long_unsigned_type_node
359 : long_long_integer_type_node
);
360 return build_type_attribute_variant (t
, attributes
);
363 /* Go through the same procedure, but for longs. */
364 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_unsigned_type_node
)
365 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_unsigned_type_node
))
366 return build_type_attribute_variant (long_unsigned_type_node
,
368 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_integer_type_node
)
369 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_integer_type_node
))
371 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
372 ? long_unsigned_type_node
: long_integer_type_node
);
373 return build_type_attribute_variant (t
, attributes
);
376 /* For __intN types, either the type is __int128 (and is lower
377 priority than the types checked above, but higher than other
378 128-bit types) or it's known to not be the same size as other
379 types (enforced in toplev.c). Prefer the unsigned type. */
380 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
382 if (int_n_enabled_p
[i
]
383 && (same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].signed_type
)
384 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].signed_type
)
385 || same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].unsigned_type
)
386 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].unsigned_type
)))
388 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
389 ? int_n_trees
[i
].unsigned_type
390 : int_n_trees
[i
].signed_type
);
391 return build_type_attribute_variant (t
, attributes
);
395 /* Otherwise prefer the unsigned one. */
396 if (TYPE_UNSIGNED (t1
))
397 return build_type_attribute_variant (t1
, attributes
);
399 return build_type_attribute_variant (t2
, attributes
);
403 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_double_type_node
)
404 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_double_type_node
))
405 return build_type_attribute_variant (long_double_type_node
,
407 if (same_type_p (TYPE_MAIN_VARIANT (t1
), double_type_node
)
408 || same_type_p (TYPE_MAIN_VARIANT (t2
), double_type_node
))
409 return build_type_attribute_variant (double_type_node
,
411 if (same_type_p (TYPE_MAIN_VARIANT (t1
), float_type_node
)
412 || same_type_p (TYPE_MAIN_VARIANT (t2
), float_type_node
))
413 return build_type_attribute_variant (float_type_node
,
416 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
417 the standard C++ floating-point types. Logic earlier in this
418 function has already eliminated the possibility that
419 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
420 compelling reason to choose one or the other. */
421 return build_type_attribute_variant (t1
, attributes
);
425 /* T1 and T2 are arithmetic or enumeration types. Return the type
426 that will result from the "usual arithmetic conversions" on T1 and
427 T2 as described in [expr]. */
430 type_after_usual_arithmetic_conversions (tree t1
, tree t2
)
432 gcc_assert (ARITHMETIC_TYPE_P (t1
)
433 || VECTOR_TYPE_P (t1
)
434 || UNSCOPED_ENUM_P (t1
));
435 gcc_assert (ARITHMETIC_TYPE_P (t2
)
436 || VECTOR_TYPE_P (t2
)
437 || UNSCOPED_ENUM_P (t2
));
439 /* Perform the integral promotions. We do not promote real types here. */
440 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1
)
441 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2
))
443 t1
= type_promotes_to (t1
);
444 t2
= type_promotes_to (t2
);
447 return cp_common_type (t1
, t2
);
451 composite_pointer_error (const op_location_t
&location
,
452 diagnostic_t kind
, tree t1
, tree t2
,
453 composite_pointer_operation operation
)
458 emit_diagnostic (kind
, location
, 0,
459 "comparison between "
460 "distinct pointer types %qT and %qT lacks a cast",
464 emit_diagnostic (kind
, location
, 0,
465 "conversion between "
466 "distinct pointer types %qT and %qT lacks a cast",
469 case CPO_CONDITIONAL_EXPR
:
470 emit_diagnostic (kind
, 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. And ADD_CONST
482 is used to track adding "const" where needed. */
485 composite_pointer_type_r (const op_location_t
&location
,
486 tree t1
, tree t2
, bool *add_const
,
487 composite_pointer_operation operation
,
488 tsubst_flags_t complain
)
495 /* Determine the types pointed to by T1 and T2. */
498 pointee1
= TREE_TYPE (t1
);
499 pointee2
= TREE_TYPE (t2
);
503 pointee1
= TYPE_PTRMEM_POINTED_TO_TYPE (t1
);
504 pointee2
= TYPE_PTRMEM_POINTED_TO_TYPE (t2
);
509 If T1 and T2 are similar types, the result is the cv-combined type of
511 if (same_type_ignoring_top_level_qualifiers_p (pointee1
, pointee2
))
512 result_type
= pointee1
;
513 else if ((TYPE_PTR_P (pointee1
) && TYPE_PTR_P (pointee2
))
514 || (TYPE_PTRMEM_P (pointee1
) && TYPE_PTRMEM_P (pointee2
)))
516 result_type
= composite_pointer_type_r (location
, pointee1
, pointee2
,
517 add_const
, operation
, complain
);
518 if (result_type
== error_mark_node
)
519 return error_mark_node
;
523 if (complain
& tf_error
)
524 composite_pointer_error (location
, DK_PERMERROR
,
527 return error_mark_node
;
528 result_type
= void_type_node
;
530 const int q1
= cp_type_quals (pointee1
);
531 const int q2
= cp_type_quals (pointee2
);
532 const int quals
= q1
| q2
;
533 result_type
= cp_build_qualified_type (result_type
,
536 : TYPE_UNQUALIFIED
)));
537 /* The cv-combined type can add "const" as per [conv.qual]/3.3 (except for
538 the TLQ). The reason is that both T1 and T2 can then be converted to the
539 cv-combined type of T1 and T2. */
540 if (quals
!= q1
|| quals
!= q2
)
542 /* If the original types were pointers to members, so is the
544 if (TYPE_PTRMEM_P (t1
))
546 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
547 TYPE_PTRMEM_CLASS_TYPE (t2
)))
549 if (complain
& tf_error
)
550 composite_pointer_error (location
, DK_PERMERROR
,
553 return error_mark_node
;
555 result_type
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
559 result_type
= build_pointer_type (result_type
);
561 /* Merge the attributes. */
562 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
563 return build_type_attribute_variant (result_type
, attributes
);
566 /* Return the composite pointer type (see [expr.type]) for T1 and T2.
567 ARG1 and ARG2 are the values with those types. The OPERATION is to
568 describe the operation between the pointer types,
569 in case an error occurs.
571 This routine also implements the computation of a common type for
572 pointers-to-members as per [expr.eq]. */
575 composite_pointer_type (const op_location_t
&location
,
576 tree t1
, tree t2
, tree arg1
, tree arg2
,
577 composite_pointer_operation operation
,
578 tsubst_flags_t complain
)
585 If one operand is a null pointer constant, the composite pointer
586 type is the type of the other operand. */
587 if (null_ptr_cst_p (arg1
))
589 if (null_ptr_cst_p (arg2
))
596 If one of the operands has type "pointer to cv1 void", then
597 the other has type "pointer to cv2 T", and the composite pointer
598 type is "pointer to cv12 void", where cv12 is the union of cv1
601 If either type is a pointer to void, make sure it is T1. */
602 if (TYPE_PTR_P (t2
) && VOID_TYPE_P (TREE_TYPE (t2
)))
605 /* Now, if T1 is a pointer to void, merge the qualifiers. */
606 if (TYPE_PTR_P (t1
) && VOID_TYPE_P (TREE_TYPE (t1
)))
611 if (TYPE_PTRFN_P (t2
))
613 if (complain
& tf_error
)
618 pedwarn (location
, OPT_Wpedantic
,
619 "ISO C++ forbids comparison between pointer "
620 "of type %<void *%> and pointer-to-function");
623 pedwarn (location
, OPT_Wpedantic
,
624 "ISO C++ forbids conversion between pointer "
625 "of type %<void *%> and pointer-to-function");
627 case CPO_CONDITIONAL_EXPR
:
628 pedwarn (location
, OPT_Wpedantic
,
629 "ISO C++ forbids conditional expression between "
630 "pointer of type %<void *%> and "
631 "pointer-to-function");
638 return error_mark_node
;
641 = cp_build_qualified_type (void_type_node
,
642 (cp_type_quals (TREE_TYPE (t1
))
643 | cp_type_quals (TREE_TYPE (t2
))));
644 result_type
= build_pointer_type (result_type
);
645 /* Merge the attributes. */
646 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
647 return build_type_attribute_variant (result_type
, attributes
);
650 if (c_dialect_objc () && TYPE_PTR_P (t1
)
653 if (objc_have_common_type (t1
, t2
, -3, NULL_TREE
))
654 return objc_common_type (t1
, t2
);
657 /* if T1 or T2 is "pointer to noexcept function" and the other type is
658 "pointer to function", where the function types are otherwise the same,
659 "pointer to function" */
660 if (fnptr_conv_p (t1
, t2
))
662 if (fnptr_conv_p (t2
, t1
))
665 /* [expr.eq] permits the application of a pointer conversion to
666 bring the pointers to a common type. */
667 if (TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
)
668 && CLASS_TYPE_P (TREE_TYPE (t1
))
669 && CLASS_TYPE_P (TREE_TYPE (t2
))
670 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1
),
673 class1
= TREE_TYPE (t1
);
674 class2
= TREE_TYPE (t2
);
676 if (DERIVED_FROM_P (class1
, class2
))
677 t2
= (build_pointer_type
678 (cp_build_qualified_type (class1
, cp_type_quals (class2
))));
679 else if (DERIVED_FROM_P (class2
, class1
))
680 t1
= (build_pointer_type
681 (cp_build_qualified_type (class2
, cp_type_quals (class1
))));
684 if (complain
& tf_error
)
685 composite_pointer_error (location
, DK_ERROR
, t1
, t2
, operation
);
686 return error_mark_node
;
689 /* [expr.eq] permits the application of a pointer-to-member
690 conversion to change the class type of one of the types. */
691 else if (TYPE_PTRMEM_P (t1
)
692 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
693 TYPE_PTRMEM_CLASS_TYPE (t2
)))
695 class1
= TYPE_PTRMEM_CLASS_TYPE (t1
);
696 class2
= TYPE_PTRMEM_CLASS_TYPE (t2
);
698 if (DERIVED_FROM_P (class1
, class2
))
699 t1
= build_ptrmem_type (class2
, TYPE_PTRMEM_POINTED_TO_TYPE (t1
));
700 else if (DERIVED_FROM_P (class2
, class1
))
701 t2
= build_ptrmem_type (class1
, TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
704 if (complain
& tf_error
)
708 error_at (location
, "comparison between distinct "
709 "pointer-to-member types %qT and %qT lacks a cast",
713 error_at (location
, "conversion between distinct "
714 "pointer-to-member types %qT and %qT lacks a cast",
717 case CPO_CONDITIONAL_EXPR
:
718 error_at (location
, "conditional expression between distinct "
719 "pointer-to-member types %qT and %qT lacks a cast",
725 return error_mark_node
;
729 bool add_const
= false;
730 return composite_pointer_type_r (location
, t1
, t2
, &add_const
, operation
,
734 /* Return the merged type of two types.
735 We assume that comptypes has already been done and returned 1;
736 if that isn't so, this may crash.
738 This just combines attributes and default arguments; any other
739 differences would cause the two types to compare unalike. */
742 merge_types (tree t1
, tree t2
)
744 enum tree_code code1
;
745 enum tree_code code2
;
748 /* Save time if the two types are the same. */
751 if (original_type (t1
) == original_type (t2
))
754 /* If one type is nonsense, use the other. */
755 if (t1
== error_mark_node
)
757 if (t2
== error_mark_node
)
760 /* Handle merging an auto redeclaration with a previous deduced
765 /* Merge the attributes. */
766 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
768 if (TYPE_PTRMEMFUNC_P (t1
))
769 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
770 if (TYPE_PTRMEMFUNC_P (t2
))
771 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
773 code1
= TREE_CODE (t1
);
774 code2
= TREE_CODE (t2
);
777 gcc_assert (code1
== TYPENAME_TYPE
|| code2
== TYPENAME_TYPE
);
778 if (code1
== TYPENAME_TYPE
)
780 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
781 code1
= TREE_CODE (t1
);
785 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
786 code2
= TREE_CODE (t2
);
794 /* For two pointers, do this recursively on the target type. */
796 tree target
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
797 int quals
= cp_type_quals (t1
);
799 if (code1
== POINTER_TYPE
)
801 t1
= build_pointer_type (target
);
802 if (TREE_CODE (target
) == METHOD_TYPE
)
803 t1
= build_ptrmemfunc_type (t1
);
806 t1
= cp_build_reference_type (target
, TYPE_REF_IS_RVALUE (t1
));
807 t1
= build_type_attribute_variant (t1
, attributes
);
808 t1
= cp_build_qualified_type (t1
, quals
);
817 quals
= cp_type_quals (t1
);
818 pointee
= merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1
),
819 TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
820 t1
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
822 t1
= cp_build_qualified_type (t1
, quals
);
828 tree elt
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
829 /* Save space: see if the result is identical to one of the args. */
830 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
831 return build_type_attribute_variant (t1
, attributes
);
832 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
833 return build_type_attribute_variant (t2
, attributes
);
834 /* Merge the element types, and have a size if either arg has one. */
835 t1
= build_cplus_array_type
836 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
841 /* Function types: prefer the one that specified arg types.
842 If both do, merge the arg types. Also merge the return types. */
844 tree valtype
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
845 tree p1
= TYPE_ARG_TYPES (t1
);
846 tree p2
= TYPE_ARG_TYPES (t2
);
849 /* Save space: see if the result is identical to one of the args. */
850 if (valtype
== TREE_TYPE (t1
) && ! p2
)
851 return cp_build_type_attribute_variant (t1
, attributes
);
852 if (valtype
== TREE_TYPE (t2
) && ! p1
)
853 return cp_build_type_attribute_variant (t2
, attributes
);
855 /* Simple way if one arg fails to specify argument types. */
856 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
858 else if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
861 parms
= commonparms (p1
, p2
);
863 cp_cv_quals quals
= type_memfn_quals (t1
);
864 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
865 gcc_assert (quals
== type_memfn_quals (t2
));
866 gcc_assert (rqual
== type_memfn_rqual (t2
));
868 tree rval
= build_function_type (valtype
, parms
);
869 rval
= apply_memfn_quals (rval
, quals
);
870 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
871 TYPE_RAISES_EXCEPTIONS (t2
));
872 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
873 t1
= build_cp_fntype_variant (rval
, rqual
, raises
, late_return_type_p
);
879 /* Get this value the long way, since TYPE_METHOD_BASETYPE
880 is just the main variant of this. */
881 tree basetype
= class_of_this_parm (t2
);
882 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
883 TYPE_RAISES_EXCEPTIONS (t2
));
884 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
886 bool late_return_type_1_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
888 /* If this was a member function type, get back to the
889 original type of type member function (i.e., without
890 the class instance variable up front. */
891 t1
= build_function_type (TREE_TYPE (t1
),
892 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
893 t2
= build_function_type (TREE_TYPE (t2
),
894 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
895 t3
= merge_types (t1
, t2
);
896 t3
= build_method_type_directly (basetype
, TREE_TYPE (t3
),
897 TYPE_ARG_TYPES (t3
));
898 t1
= build_cp_fntype_variant (t3
, rqual
, raises
, late_return_type_1_p
);
903 /* There is no need to merge attributes into a TYPENAME_TYPE.
904 When the type is instantiated it will have whatever
905 attributes result from the instantiation. */
909 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
911 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
916 return cp_build_type_attribute_variant (t1
, attributes
);
919 /* Return the ARRAY_TYPE type without its domain. */
922 strip_array_domain (tree type
)
925 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
926 if (TYPE_DOMAIN (type
) == NULL_TREE
)
928 t2
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
929 return cp_build_type_attribute_variant (t2
, TYPE_ATTRIBUTES (type
));
932 /* Wrapper around cp_common_type that is used by c-common.c and other
933 front end optimizations that remove promotions.
935 Return the common type for two arithmetic types T1 and T2 under the
936 usual arithmetic conversions. The default conversions have already
937 been applied, and enumerated types converted to their compatible
941 common_type (tree t1
, tree t2
)
943 /* If one type is nonsense, use the other */
944 if (t1
== error_mark_node
)
946 if (t2
== error_mark_node
)
949 return cp_common_type (t1
, t2
);
952 /* Return the common type of two pointer types T1 and T2. This is the
953 type for the result of most arithmetic operations if the operands
954 have the given two types.
956 We assume that comp_target_types has already been done and returned
957 nonzero; if that isn't so, this may crash. */
960 common_pointer_type (tree t1
, tree t2
)
962 gcc_assert ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
963 || (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
964 || (TYPE_PTRMEMFUNC_P (t1
) && TYPE_PTRMEMFUNC_P (t2
)));
966 return composite_pointer_type (input_location
, t1
, t2
,
967 error_mark_node
, error_mark_node
,
968 CPO_CONVERSION
, tf_warning_or_error
);
971 /* Compare two exception specifier types for exactness or subsetness, if
972 allowed. Returns false for mismatch, true for match (same, or
975 [except.spec] "If a class X ... objects of class X or any class publicly
976 and unambiguously derived from X. Similarly, if a pointer type Y * ...
977 exceptions of type Y * or that are pointers to any type publicly and
978 unambiguously derived from Y. Otherwise a function only allows exceptions
979 that have the same type ..."
980 This does not mention cv qualifiers and is different to what throw
981 [except.throw] and catch [except.catch] will do. They will ignore the
982 top level cv qualifiers, and allow qualifiers in the pointer to class
985 We implement the letter of the standard. */
988 comp_except_types (tree a
, tree b
, bool exact
)
990 if (same_type_p (a
, b
))
994 if (cp_type_quals (a
) || cp_type_quals (b
))
997 if (TYPE_PTR_P (a
) && TYPE_PTR_P (b
))
1001 if (cp_type_quals (a
) || cp_type_quals (b
))
1005 if (TREE_CODE (a
) != RECORD_TYPE
1006 || TREE_CODE (b
) != RECORD_TYPE
)
1009 if (publicly_uniquely_derived_p (a
, b
))
1015 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1016 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1017 If EXACT is ce_type, the C++17 type compatibility rules apply.
1018 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1019 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1020 are unordered, but we've already filtered out duplicates. Most lists will
1021 be in order, we should try to make use of that. */
1024 comp_except_specs (const_tree t1
, const_tree t2
, int exact
)
1033 /* First handle noexcept. */
1034 if (exact
< ce_exact
)
1036 if (exact
== ce_type
1037 && (canonical_eh_spec (CONST_CAST_TREE (t1
))
1038 == canonical_eh_spec (CONST_CAST_TREE (t2
))))
1041 /* noexcept(false) is compatible with no exception-specification,
1042 and less strict than any spec. */
1043 if (t1
== noexcept_false_spec
)
1044 return t2
== NULL_TREE
|| exact
== ce_derived
;
1045 /* Even a derived noexcept(false) is compatible with no
1046 exception-specification. */
1047 if (t2
== noexcept_false_spec
)
1048 return t1
== NULL_TREE
;
1050 /* Otherwise, if we aren't looking for an exact match, noexcept is
1051 equivalent to throw(). */
1052 if (t1
== noexcept_true_spec
)
1053 t1
= empty_except_spec
;
1054 if (t2
== noexcept_true_spec
)
1055 t2
= empty_except_spec
;
1058 /* If any noexcept is left, it is only comparable to itself;
1059 either we're looking for an exact match or we're redeclaring a
1060 template with dependent noexcept. */
1061 if ((t1
&& TREE_PURPOSE (t1
))
1062 || (t2
&& TREE_PURPOSE (t2
)))
1064 && cp_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)));
1066 if (t1
== NULL_TREE
) /* T1 is ... */
1067 return t2
== NULL_TREE
|| exact
== ce_derived
;
1068 if (!TREE_VALUE (t1
)) /* t1 is EMPTY */
1069 return t2
!= NULL_TREE
&& !TREE_VALUE (t2
);
1070 if (t2
== NULL_TREE
) /* T2 is ... */
1072 if (TREE_VALUE (t1
) && !TREE_VALUE (t2
)) /* T2 is EMPTY, T1 is not */
1073 return exact
== ce_derived
;
1075 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1076 Count how many we find, to determine exactness. For exact matching and
1077 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1079 for (base
= t1
; t2
!= NULL_TREE
; t2
= TREE_CHAIN (t2
))
1081 for (probe
= base
; probe
!= NULL_TREE
; probe
= TREE_CHAIN (probe
))
1083 tree a
= TREE_VALUE (probe
);
1084 tree b
= TREE_VALUE (t2
);
1086 if (comp_except_types (a
, b
, exact
))
1088 if (probe
== base
&& exact
> ce_derived
)
1089 base
= TREE_CHAIN (probe
);
1094 if (probe
== NULL_TREE
)
1097 return exact
== ce_derived
|| base
== NULL_TREE
|| length
== list_length (t1
);
1100 /* Compare the array types T1 and T2. CB says how we should behave when
1101 comparing array bounds: bounds_none doesn't allow dimensionless arrays,
1102 bounds_either says than any array can be [], bounds_first means that
1103 onlt T1 can be an array with unknown bounds. STRICT is true if
1104 qualifiers must match when comparing the types of the array elements. */
1107 comp_array_types (const_tree t1
, const_tree t2
, compare_bounds_t cb
,
1117 /* The type of the array elements must be the same. */
1119 ? !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
))
1120 : !similar_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1123 d1
= TYPE_DOMAIN (t1
);
1124 d2
= TYPE_DOMAIN (t2
);
1129 /* If one of the arrays is dimensionless, and the other has a
1130 dimension, they are of different types. However, it is valid to
1138 declarations for an array object can specify
1139 array types that differ by the presence or absence of a major
1140 array bound (_dcl.array_). */
1142 return cb
>= bounds_either
;
1144 return cb
== bounds_either
;
1146 /* Check that the dimensions are the same. */
1148 if (!cp_tree_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
)))
1150 max1
= TYPE_MAX_VALUE (d1
);
1151 max2
= TYPE_MAX_VALUE (d2
);
1153 if (!cp_tree_equal (max1
, max2
))
1159 /* Compare the relative position of T1 and T2 into their respective
1160 template parameter list.
1161 T1 and T2 must be template parameter types.
1162 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1165 comp_template_parms_position (tree t1
, tree t2
)
1167 tree index1
, index2
;
1168 gcc_assert (t1
&& t2
1169 && TREE_CODE (t1
) == TREE_CODE (t2
)
1170 && (TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
1171 || TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
1172 || TREE_CODE (t1
) == TEMPLATE_TYPE_PARM
));
1174 index1
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1
));
1175 index2
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2
));
1177 /* Then compare their relative position. */
1178 if (TEMPLATE_PARM_IDX (index1
) != TEMPLATE_PARM_IDX (index2
)
1179 || TEMPLATE_PARM_LEVEL (index1
) != TEMPLATE_PARM_LEVEL (index2
)
1180 || (TEMPLATE_PARM_PARAMETER_PACK (index1
)
1181 != TEMPLATE_PARM_PARAMETER_PACK (index2
)))
1184 /* In C++14 we can end up comparing 'auto' to a normal template
1185 parameter. Don't confuse them. */
1186 if (cxx_dialect
>= cxx14
&& (is_auto (t1
) || is_auto (t2
)))
1187 return TYPE_IDENTIFIER (t1
) == TYPE_IDENTIFIER (t2
);
1192 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1195 cxx_safe_arg_type_equiv_p (tree t1
, tree t2
)
1197 t1
= TYPE_MAIN_VARIANT (t1
);
1198 t2
= TYPE_MAIN_VARIANT (t2
);
1204 /* The signedness of the parameter matters only when an integral
1205 type smaller than int is promoted to int, otherwise only the
1206 precision of the parameter matters.
1207 This check should make sure that the callee does not see
1208 undefined values in argument registers. */
1209 if (INTEGRAL_TYPE_P (t1
)
1210 && INTEGRAL_TYPE_P (t2
)
1211 && TYPE_PRECISION (t1
) == TYPE_PRECISION (t2
)
1212 && (TYPE_UNSIGNED (t1
) == TYPE_UNSIGNED (t2
)
1213 || !targetm
.calls
.promote_prototypes (NULL_TREE
)
1214 || TYPE_PRECISION (t1
) >= TYPE_PRECISION (integer_type_node
)))
1217 return same_type_p (t1
, t2
);
1220 /* Check if a type cast between two function types can be considered safe. */
1223 cxx_safe_function_type_cast_p (tree t1
, tree t2
)
1225 if (TREE_TYPE (t1
) == void_type_node
&&
1226 TYPE_ARG_TYPES (t1
) == void_list_node
)
1229 if (TREE_TYPE (t2
) == void_type_node
&&
1230 TYPE_ARG_TYPES (t2
) == void_list_node
)
1233 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1236 for (t1
= TYPE_ARG_TYPES (t1
), t2
= TYPE_ARG_TYPES (t2
);
1238 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1239 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1245 /* Subroutine in comptypes. */
1248 structural_comptypes (tree t1
, tree t2
, int strict
)
1253 /* Suppress errors caused by previously reported errors. */
1254 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1257 gcc_assert (TYPE_P (t1
) && TYPE_P (t2
));
1259 if (!comparing_specializations
)
1261 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1262 current instantiation. */
1263 if (TREE_CODE (t1
) == TYPENAME_TYPE
)
1264 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
1266 if (TREE_CODE (t2
) == TYPENAME_TYPE
)
1267 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
1270 if (TYPE_PTRMEMFUNC_P (t1
))
1271 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
1272 if (TYPE_PTRMEMFUNC_P (t2
))
1273 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
1275 /* Different classes of types can't be compatible. */
1276 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1279 /* Qualifiers must match. For array types, we will check when we
1280 recur on the array element types. */
1281 if (TREE_CODE (t1
) != ARRAY_TYPE
1282 && cp_type_quals (t1
) != cp_type_quals (t2
))
1284 if (TREE_CODE (t1
) == FUNCTION_TYPE
1285 && type_memfn_quals (t1
) != type_memfn_quals (t2
))
1287 /* Need to check this before TYPE_MAIN_VARIANT.
1288 FIXME function qualifiers should really change the main variant. */
1289 if (FUNC_OR_METHOD_TYPE_P (t1
))
1291 if (type_memfn_rqual (t1
) != type_memfn_rqual (t2
))
1293 if (flag_noexcept_type
1294 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1
),
1295 TYPE_RAISES_EXCEPTIONS (t2
),
1300 /* Allow for two different type nodes which have essentially the same
1301 definition. Note that we already checked for equality of the type
1302 qualifiers (just above). */
1304 if (TREE_CODE (t1
) != ARRAY_TYPE
1305 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1309 /* Compare the types. Break out if they could be the same. */
1310 switch (TREE_CODE (t1
))
1314 /* All void and bool types are the same. */
1318 case FIXED_POINT_TYPE
:
1320 /* With these nodes, we can't determine type equivalence by
1321 looking at what is stored in the nodes themselves, because
1322 two nodes might have different TYPE_MAIN_VARIANTs but still
1323 represent the same type. For example, wchar_t and int could
1324 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1325 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1326 and are distinct types. On the other hand, int and the
1329 typedef int INT __attribute((may_alias));
1331 have identical properties, different TYPE_MAIN_VARIANTs, but
1332 represent the same type. The canonical type system keeps
1333 track of equivalence in this case, so we fall back on it. */
1334 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1336 case TEMPLATE_TEMPLATE_PARM
:
1337 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1338 if (!comp_template_parms_position (t1
, t2
))
1340 if (!comp_template_parms
1341 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1
)),
1342 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2
))))
1344 if (TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
)
1346 /* Don't check inheritance. */
1347 strict
= COMPARE_STRICT
;
1352 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
1353 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
1354 || TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1355 && comp_template_args (TYPE_TI_ARGS (t1
), TYPE_TI_ARGS (t2
)))
1358 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
1360 else if ((strict
& COMPARE_DERIVED
) && DERIVED_FROM_P (t2
, t1
))
1366 if (!comptypes (TYPE_OFFSET_BASETYPE (t1
), TYPE_OFFSET_BASETYPE (t2
),
1367 strict
& ~COMPARE_REDECLARATION
))
1369 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1373 case REFERENCE_TYPE
:
1374 if (TYPE_REF_IS_RVALUE (t1
) != TYPE_REF_IS_RVALUE (t2
))
1376 /* fall through to checks for pointer types */
1380 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1381 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1387 /* Exception specs and memfn_rquals were checked above. */
1388 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1390 if (!compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)))
1395 /* Target types must match incl. qualifiers. */
1396 if (!comp_array_types (t1
, t2
, ((strict
& COMPARE_REDECLARATION
)
1397 ? bounds_either
: bounds_none
),
1402 case TEMPLATE_TYPE_PARM
:
1403 /* If T1 and T2 don't have the same relative position in their
1404 template parameters set, they can't be equal. */
1405 if (!comp_template_parms_position (t1
, t2
))
1407 /* If T1 and T2 don't represent the same class template deduction,
1408 they aren't equal. */
1409 if (CLASS_PLACEHOLDER_TEMPLATE (t1
)
1410 != CLASS_PLACEHOLDER_TEMPLATE (t2
))
1412 /* Constrained 'auto's are distinct from parms that don't have the same
1414 if (!equivalent_placeholder_constraints (t1
, t2
))
1419 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1
),
1420 TYPENAME_TYPE_FULLNAME (t2
)))
1422 /* Qualifiers don't matter on scopes. */
1423 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1
),
1428 case UNBOUND_CLASS_TEMPLATE
:
1429 if (!cp_tree_equal (TYPE_IDENTIFIER (t1
), TYPE_IDENTIFIER (t2
)))
1431 if (!same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
)))
1436 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1441 if (gnu_vector_type_p (t1
) != gnu_vector_type_p (t2
)
1442 || maybe_ne (TYPE_VECTOR_SUBPARTS (t1
), TYPE_VECTOR_SUBPARTS (t2
))
1443 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1447 case TYPE_PACK_EXPANSION
:
1448 return (same_type_p (PACK_EXPANSION_PATTERN (t1
),
1449 PACK_EXPANSION_PATTERN (t2
))
1450 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1
),
1451 PACK_EXPANSION_EXTRA_ARGS (t2
)));
1454 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1
)
1455 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2
))
1457 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1
) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2
))
1459 if (DECLTYPE_FOR_LAMBDA_PROXY (t1
) != DECLTYPE_FOR_LAMBDA_PROXY (t2
))
1461 if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1
), DECLTYPE_TYPE_EXPR (t2
)))
1465 case UNDERLYING_TYPE
:
1466 if (!same_type_p (UNDERLYING_TYPE_TYPE (t1
), UNDERLYING_TYPE_TYPE (t2
)))
1471 if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1
), TYPEOF_TYPE_EXPR (t2
)))
1479 /* Don't treat an alias template specialization with dependent
1480 arguments as equivalent to its underlying type when used as a
1481 template argument; we need them to be distinct so that we
1482 substitute into the specialization arguments at instantiation
1483 time. And aliases can't be equivalent without being ==, so
1484 we don't need to look any deeper. */
1485 if (comparing_specializations
)
1487 tree dep1
= dependent_alias_template_spec_p (t1
, nt_transparent
);
1488 tree dep2
= dependent_alias_template_spec_p (t2
, nt_transparent
);
1489 if ((dep1
|| dep2
) && dep1
!= dep2
)
1493 /* If we get here, we know that from a target independent POV the
1494 types are the same. Make sure the target attributes are also
1496 return comp_type_attributes (t1
, t2
);
1499 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1500 is a bitwise-or of the COMPARE_* flags. */
1503 comptypes (tree t1
, tree t2
, int strict
)
1505 gcc_checking_assert (t1
&& t2
);
1507 /* TYPE_ARGUMENT_PACKS are not really types. */
1508 gcc_checking_assert (TREE_CODE (t1
) != TYPE_ARGUMENT_PACK
1509 && TREE_CODE (t2
) != TYPE_ARGUMENT_PACK
);
1511 if (strict
== COMPARE_STRICT
&& comparing_specializations
1512 && (t1
!= TYPE_CANONICAL (t1
) || t2
!= TYPE_CANONICAL (t2
)))
1513 /* If comparing_specializations, treat dependent aliases as distinct. */
1514 strict
= COMPARE_STRUCTURAL
;
1516 if (strict
== COMPARE_STRICT
)
1521 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1524 if (TYPE_STRUCTURAL_EQUALITY_P (t1
) || TYPE_STRUCTURAL_EQUALITY_P (t2
))
1525 /* At least one of the types requires structural equality, so
1526 perform a deep check. */
1527 return structural_comptypes (t1
, t2
, strict
);
1529 if (flag_checking
&& param_use_canonical_types
)
1531 bool result
= structural_comptypes (t1
, t2
, strict
);
1533 if (result
&& TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1534 /* The two types are structurally equivalent, but their
1535 canonical types were different. This is a failure of the
1536 canonical type propagation code.*/
1538 ("canonical types differ for identical types %qT and %qT",
1540 else if (!result
&& TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
))
1541 /* Two types are structurally different, but the canonical
1542 types are the same. This means we were over-eager in
1543 assigning canonical types. */
1545 ("same canonical type node for different types %qT and %qT",
1550 if (!flag_checking
&& param_use_canonical_types
)
1551 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1553 return structural_comptypes (t1
, t2
, strict
);
1555 else if (strict
== COMPARE_STRUCTURAL
)
1556 return structural_comptypes (t1
, t2
, COMPARE_STRICT
);
1558 return structural_comptypes (t1
, t2
, strict
);
1561 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1562 top-level qualifiers. */
1565 same_type_ignoring_top_level_qualifiers_p (tree type1
, tree type2
)
1567 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1572 type1
= cp_build_qualified_type (type1
, TYPE_UNQUALIFIED
);
1573 type2
= cp_build_qualified_type (type2
, TYPE_UNQUALIFIED
);
1574 return same_type_p (type1
, type2
);
1577 /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1580 similar_type_p (tree type1
, tree type2
)
1582 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1585 /* Informally, two types are similar if, ignoring top-level cv-qualification:
1586 * they are the same type; or
1587 * they are both pointers, and the pointed-to types are similar; or
1588 * they are both pointers to member of the same class, and the types of
1589 the pointed-to members are similar; or
1590 * they are both arrays of the same size or both arrays of unknown bound,
1591 and the array element types are similar. */
1593 if (same_type_ignoring_top_level_qualifiers_p (type1
, type2
))
1596 if ((TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
1597 || (TYPE_PTRDATAMEM_P (type1
) && TYPE_PTRDATAMEM_P (type2
))
1598 || (TREE_CODE (type1
) == ARRAY_TYPE
&& TREE_CODE (type2
) == ARRAY_TYPE
))
1599 return comp_ptr_ttypes_const (type1
, type2
, bounds_either
);
1604 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1607 at_least_as_qualified_p (const_tree type1
, const_tree type2
)
1609 int q1
= cp_type_quals (type1
);
1610 int q2
= cp_type_quals (type2
);
1612 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1613 return (q1
& q2
) == q2
;
1616 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1617 more cv-qualified that TYPE1, and 0 otherwise. */
1620 comp_cv_qualification (int q1
, int q2
)
1625 if ((q1
& q2
) == q2
)
1627 else if ((q1
& q2
) == q1
)
1634 comp_cv_qualification (const_tree type1
, const_tree type2
)
1636 int q1
= cp_type_quals (type1
);
1637 int q2
= cp_type_quals (type2
);
1638 return comp_cv_qualification (q1
, q2
);
1641 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1642 subset of the cv-qualification signature of TYPE2, and the types
1643 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1646 comp_cv_qual_signature (tree type1
, tree type2
)
1648 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1650 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1656 /* Subroutines of `comptypes'. */
1658 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1659 equivalent in the sense that functions with those parameter types
1660 can have equivalent types. The two lists must be equivalent,
1661 element by element. */
1664 compparms (const_tree parms1
, const_tree parms2
)
1668 /* An unspecified parmlist matches any specified parmlist
1669 whose argument types don't need default promotions. */
1671 for (t1
= parms1
, t2
= parms2
;
1673 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1675 /* If one parmlist is shorter than the other,
1676 they fail to match. */
1679 if (!same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1686 /* Process a sizeof or alignof expression where the operand is a
1687 type. STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
1688 or GNU (preferred alignment) semantics; it is ignored if op is
1692 cxx_sizeof_or_alignof_type (location_t loc
, tree type
, enum tree_code op
,
1693 bool std_alignof
, bool complain
)
1695 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
1696 if (type
== error_mark_node
)
1697 return error_mark_node
;
1699 type
= non_reference (type
);
1700 if (TREE_CODE (type
) == METHOD_TYPE
)
1704 pedwarn (loc
, OPT_Wpointer_arith
,
1705 "invalid application of %qs to a member function",
1706 OVL_OP_INFO (false, op
)->name
);
1707 return size_one_node
;
1710 return error_mark_node
;
1713 bool dependent_p
= dependent_type_p (type
);
1715 complete_type (type
);
1717 /* VLA types will have a non-constant size. In the body of an
1718 uninstantiated template, we don't need to try to compute the
1719 value, because the sizeof expression is not an integral
1720 constant expression in that case. And, if we do try to
1721 compute the value, we'll likely end up with SAVE_EXPRs, which
1722 the template substitution machinery does not expect to see. */
1723 || (processing_template_decl
1724 && COMPLETE_TYPE_P (type
)
1725 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
))
1727 tree value
= build_min (op
, size_type_node
, type
);
1728 TREE_READONLY (value
) = 1;
1729 if (op
== ALIGNOF_EXPR
&& std_alignof
)
1730 ALIGNOF_EXPR_STD_P (value
) = true;
1731 SET_EXPR_LOCATION (value
, loc
);
1735 return c_sizeof_or_alignof_type (loc
, complete_type (type
),
1736 op
== SIZEOF_EXPR
, std_alignof
,
1740 /* Return the size of the type, without producing any warnings for
1741 types whose size cannot be taken. This routine should be used only
1742 in some other routine that has already produced a diagnostic about
1743 using the size of such a type. */
1745 cxx_sizeof_nowarn (tree type
)
1747 if (TREE_CODE (type
) == FUNCTION_TYPE
1748 || VOID_TYPE_P (type
)
1749 || TREE_CODE (type
) == ERROR_MARK
)
1750 return size_one_node
;
1751 else if (!COMPLETE_TYPE_P (type
))
1752 return size_zero_node
;
1754 return cxx_sizeof_or_alignof_type (input_location
, type
,
1755 SIZEOF_EXPR
, false, false);
1758 /* Process a sizeof expression where the operand is an expression. */
1761 cxx_sizeof_expr (location_t loc
, tree e
, tsubst_flags_t complain
)
1763 if (e
== error_mark_node
)
1764 return error_mark_node
;
1766 if (instantiation_dependent_uneval_expression_p (e
))
1768 e
= build_min (SIZEOF_EXPR
, size_type_node
, e
);
1769 TREE_SIDE_EFFECTS (e
) = 0;
1770 TREE_READONLY (e
) = 1;
1771 SET_EXPR_LOCATION (e
, loc
);
1776 location_t e_loc
= cp_expr_loc_or_loc (e
, loc
);
1777 STRIP_ANY_LOCATION_WRAPPER (e
);
1779 /* To get the size of a static data member declared as an array of
1780 unknown bound, we need to instantiate it. */
1782 && VAR_HAD_UNKNOWN_BOUND (e
)
1783 && DECL_TEMPLATE_INSTANTIATION (e
))
1784 instantiate_decl (e
, /*defer_ok*/true, /*expl_inst_mem*/false);
1786 if (TREE_CODE (e
) == PARM_DECL
1787 && DECL_ARRAY_PARAMETER_P (e
)
1788 && (complain
& tf_warning
))
1790 auto_diagnostic_group d
;
1791 if (warning_at (e_loc
, OPT_Wsizeof_array_argument
,
1792 "%<sizeof%> on array function parameter %qE "
1793 "will return size of %qT", e
, TREE_TYPE (e
)))
1794 inform (DECL_SOURCE_LOCATION (e
), "declared here");
1797 e
= mark_type_use (e
);
1801 if (complain
& tf_error
)
1803 "invalid application of %<sizeof%> to a bit-field");
1805 return error_mark_node
;
1808 else if (is_overloaded_fn (e
))
1810 if (complain
& tf_error
)
1811 permerror (e_loc
, "ISO C++ forbids applying %<sizeof%> to "
1812 "an expression of function type");
1814 return error_mark_node
;
1817 else if (type_unknown_p (e
))
1819 if (complain
& tf_error
)
1820 cxx_incomplete_type_error (e_loc
, e
, TREE_TYPE (e
));
1822 return error_mark_node
;
1828 return cxx_sizeof_or_alignof_type (loc
, e
, SIZEOF_EXPR
, false,
1829 complain
& tf_error
);
1832 /* Implement the __alignof keyword: Return the minimum required
1833 alignment of E, measured in bytes. For VAR_DECL's and
1834 FIELD_DECL's return DECL_ALIGN (which can be set from an
1835 "aligned" __attribute__ specification). */
1838 cxx_alignof_expr (location_t loc
, tree e
, tsubst_flags_t complain
)
1842 if (e
== error_mark_node
)
1843 return error_mark_node
;
1845 if (processing_template_decl
)
1847 e
= build_min (ALIGNOF_EXPR
, size_type_node
, e
);
1848 TREE_SIDE_EFFECTS (e
) = 0;
1849 TREE_READONLY (e
) = 1;
1850 SET_EXPR_LOCATION (e
, loc
);
1855 location_t e_loc
= cp_expr_loc_or_loc (e
, loc
);
1856 STRIP_ANY_LOCATION_WRAPPER (e
);
1858 e
= mark_type_use (e
);
1860 if (!verify_type_context (loc
, TCTX_ALIGNOF
, TREE_TYPE (e
),
1861 !(complain
& tf_error
)))
1863 if (!(complain
& tf_error
))
1864 return error_mark_node
;
1868 t
= size_int (DECL_ALIGN_UNIT (e
));
1869 else if (bitfield_p (e
))
1871 if (complain
& tf_error
)
1873 "invalid application of %<__alignof%> to a bit-field");
1875 return error_mark_node
;
1878 else if (TREE_CODE (e
) == COMPONENT_REF
1879 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
)
1880 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e
, 1)));
1881 else if (is_overloaded_fn (e
))
1883 if (complain
& tf_error
)
1884 permerror (e_loc
, "ISO C++ forbids applying %<__alignof%> to "
1885 "an expression of function type");
1887 return error_mark_node
;
1888 if (TREE_CODE (e
) == FUNCTION_DECL
)
1889 t
= size_int (DECL_ALIGN_UNIT (e
));
1893 else if (type_unknown_p (e
))
1895 if (complain
& tf_error
)
1896 cxx_incomplete_type_error (e_loc
, e
, TREE_TYPE (e
));
1898 return error_mark_node
;
1902 return cxx_sizeof_or_alignof_type (loc
, TREE_TYPE (e
),
1903 ALIGNOF_EXPR
, false,
1904 complain
& tf_error
);
1906 return fold_convert_loc (loc
, size_type_node
, t
);
1909 /* Process a sizeof or alignof expression E with code OP where the operand
1910 is an expression. */
1913 cxx_sizeof_or_alignof_expr (location_t loc
, tree e
, enum tree_code op
,
1916 if (op
== SIZEOF_EXPR
)
1917 return cxx_sizeof_expr (loc
, e
, complain
? tf_warning_or_error
: tf_none
);
1919 return cxx_alignof_expr (loc
, e
, complain
? tf_warning_or_error
: tf_none
);
1922 /* Build a representation of an expression 'alignas(E).' Return the
1923 folded integer value of E if it is an integral constant expression
1924 that resolves to a valid alignment. If E depends on a template
1925 parameter, return a syntactic representation tree of kind
1926 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1927 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1930 cxx_alignas_expr (tree e
)
1932 if (e
== NULL_TREE
|| e
== error_mark_node
1933 || (!TYPE_P (e
) && !require_potential_rvalue_constant_expression (e
)))
1939 When the alignment-specifier is of the form
1940 alignas(type-id ), it shall have the same effect as
1941 alignas(alignof(type-id )). */
1943 return cxx_sizeof_or_alignof_type (input_location
,
1944 e
, ALIGNOF_EXPR
, true, false);
1946 /* If we reach this point, it means the alignas expression if of
1947 the form "alignas(assignment-expression)", so we should follow
1948 what is stated by [dcl.align]/2. */
1950 if (value_dependent_expression_p (e
))
1951 /* Leave value-dependent expression alone for now. */
1954 e
= instantiate_non_dependent_expr (e
);
1955 e
= mark_rvalue_use (e
);
1957 /* [dcl.align]/2 says:
1959 the assignment-expression shall be an integral constant
1962 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e
)))
1964 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e
));
1965 return error_mark_node
;
1968 return cxx_constant_value (e
);
1972 /* EXPR is being used in a context that is not a function call.
1977 The expression can be used only as the left-hand operand of a
1978 member function call.
1980 [expr.mptr.operator]
1982 If the result of .* or ->* is a function, then that result can be
1983 used only as the operand for the function call operator ().
1985 by issuing an error message if appropriate. Returns true iff EXPR
1986 violates these rules. */
1989 invalid_nonstatic_memfn_p (location_t loc
, tree expr
, tsubst_flags_t complain
)
1991 if (expr
== NULL_TREE
)
1993 /* Don't enforce this in MS mode. */
1994 if (flag_ms_extensions
)
1996 if (is_overloaded_fn (expr
) && !really_overloaded_fn (expr
))
1997 expr
= get_first_fn (expr
);
1998 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr
))
2000 if (complain
& tf_error
)
2004 error_at (loc
, "invalid use of non-static member function %qD",
2006 inform (DECL_SOURCE_LOCATION (expr
), "declared here");
2009 error_at (loc
, "invalid use of non-static member function of "
2010 "type %qT", TREE_TYPE (expr
));
2017 /* If EXP is a reference to a bitfield, and the type of EXP does not
2018 match the declared type of the bitfield, return the declared type
2019 of the bitfield. Otherwise, return NULL_TREE. */
2022 is_bitfield_expr_with_lowered_type (const_tree exp
)
2024 switch (TREE_CODE (exp
))
2027 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1)
2028 ? TREE_OPERAND (exp
, 1)
2029 : TREE_OPERAND (exp
, 0)))
2031 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 2));
2034 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1));
2038 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
2044 field
= TREE_OPERAND (exp
, 1);
2045 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_BIT_FIELD_TYPE (field
))
2047 if (same_type_ignoring_top_level_qualifiers_p
2048 (TREE_TYPE (exp
), DECL_BIT_FIELD_TYPE (field
)))
2050 return DECL_BIT_FIELD_TYPE (field
);
2054 if (DECL_HAS_VALUE_EXPR_P (exp
))
2055 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2056 (CONST_CAST_TREE (exp
)));
2059 case VIEW_CONVERT_EXPR
:
2060 if (location_wrapper_p (exp
))
2061 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
2070 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2071 bitfield with a lowered type, the type of EXP is returned, rather
2075 unlowered_expr_type (const_tree exp
)
2078 tree etype
= TREE_TYPE (exp
);
2080 type
= is_bitfield_expr_with_lowered_type (exp
);
2082 type
= cp_build_qualified_type (type
, cp_type_quals (etype
));
2089 /* Perform the conversions in [expr] that apply when an lvalue appears
2090 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2091 function-to-pointer conversions. In addition, bitfield references are
2092 converted to their declared types. Note that this function does not perform
2093 the lvalue-to-rvalue conversion for class types. If you need that conversion
2094 for class types, then you probably need to use force_rvalue.
2096 Although the returned value is being used as an rvalue, this
2097 function does not wrap the returned expression in a
2098 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2099 that the return value is no longer an lvalue. */
2102 decay_conversion (tree exp
,
2103 tsubst_flags_t complain
,
2104 bool reject_builtin
/* = true */)
2107 enum tree_code code
;
2108 location_t loc
= cp_expr_loc_or_input_loc (exp
);
2110 type
= TREE_TYPE (exp
);
2111 if (type
== error_mark_node
)
2112 return error_mark_node
;
2114 exp
= resolve_nondeduced_context_or_error (exp
, complain
);
2116 code
= TREE_CODE (type
);
2118 if (error_operand_p (exp
))
2119 return error_mark_node
;
2121 if (NULLPTR_TYPE_P (type
) && !TREE_SIDE_EFFECTS (exp
))
2123 mark_rvalue_use (exp
, loc
, reject_builtin
);
2124 return nullptr_node
;
2127 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2128 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2129 if (code
== VOID_TYPE
)
2131 if (complain
& tf_error
)
2132 error_at (loc
, "void value not ignored as it ought to be");
2133 return error_mark_node
;
2135 if (invalid_nonstatic_memfn_p (loc
, exp
, complain
))
2136 return error_mark_node
;
2137 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
2139 exp
= mark_lvalue_use (exp
);
2140 if (reject_builtin
&& reject_gcc_builtin (exp
, loc
))
2141 return error_mark_node
;
2142 return cp_build_addr_expr (exp
, complain
);
2144 if (code
== ARRAY_TYPE
)
2149 exp
= mark_lvalue_use (exp
);
2151 if (INDIRECT_REF_P (exp
))
2152 return build_nop (build_pointer_type (TREE_TYPE (type
)),
2153 TREE_OPERAND (exp
, 0));
2155 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
2157 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1), complain
);
2158 if (op1
== error_mark_node
)
2159 return error_mark_node
;
2160 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
2161 TREE_OPERAND (exp
, 0), op1
);
2164 if (!obvalue_p (exp
)
2165 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
2167 if (complain
& tf_error
)
2168 error_at (loc
, "invalid use of non-lvalue array");
2169 return error_mark_node
;
2172 /* Don't let an array compound literal decay to a pointer. It can
2173 still be used to initialize an array or bind to a reference. */
2174 if (TREE_CODE (exp
) == TARGET_EXPR
)
2176 if (complain
& tf_error
)
2177 error_at (loc
, "taking address of temporary array");
2178 return error_mark_node
;
2181 ptrtype
= build_pointer_type (TREE_TYPE (type
));
2185 if (!cxx_mark_addressable (exp
))
2186 return error_mark_node
;
2187 adr
= build_nop (ptrtype
, build_address (exp
));
2190 /* This way is better for a COMPONENT_REF since it can
2191 simplify the offset for a component. */
2192 adr
= cp_build_addr_expr (exp
, complain
);
2193 return cp_convert (ptrtype
, adr
, complain
);
2196 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2197 exp
= mark_rvalue_use (exp
, loc
, reject_builtin
);
2199 /* If a bitfield is used in a context where integral promotion
2200 applies, then the caller is expected to have used
2201 default_conversion. That function promotes bitfields correctly
2202 before calling this function. At this point, if we have a
2203 bitfield referenced, we may assume that is not subject to
2204 promotion, and that, therefore, the type of the resulting rvalue
2205 is the declared type of the bitfield. */
2206 exp
= convert_bitfield_to_declared_type (exp
);
2208 /* We do not call rvalue() here because we do not want to wrap EXP
2209 in a NON_LVALUE_EXPR. */
2213 Non-class rvalues always have cv-unqualified types. */
2214 type
= TREE_TYPE (exp
);
2215 if (!CLASS_TYPE_P (type
) && cv_qualified_p (type
))
2216 exp
= build_nop (cv_unqualified (type
), exp
);
2218 if (!complete_type_or_maybe_complain (type
, exp
, complain
))
2219 return error_mark_node
;
2224 /* Perform preparatory conversions, as part of the "usual arithmetic
2225 conversions". In particular, as per [expr]:
2227 Whenever an lvalue expression appears as an operand of an
2228 operator that expects the rvalue for that operand, the
2229 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2230 standard conversions are applied to convert the expression to an
2233 In addition, we perform integral promotions here, as those are
2234 applied to both operands to a binary operator before determining
2235 what additional conversions should apply. */
2238 cp_default_conversion (tree exp
, tsubst_flags_t complain
)
2240 /* Check for target-specific promotions. */
2241 tree promoted_type
= targetm
.promoted_type (TREE_TYPE (exp
));
2243 exp
= cp_convert (promoted_type
, exp
, complain
);
2244 /* Perform the integral promotions first so that bitfield
2245 expressions (which may promote to "int", even if the bitfield is
2246 declared "unsigned") are promoted correctly. */
2247 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp
)))
2248 exp
= cp_perform_integral_promotions (exp
, complain
);
2249 /* Perform the other conversions. */
2250 exp
= decay_conversion (exp
, complain
);
2258 default_conversion (tree exp
)
2260 return cp_default_conversion (exp
, tf_warning_or_error
);
2263 /* EXPR is an expression with an integral or enumeration type.
2264 Perform the integral promotions in [conv.prom], and return the
2268 cp_perform_integral_promotions (tree expr
, tsubst_flags_t complain
)
2273 expr
= mark_rvalue_use (expr
);
2274 if (error_operand_p (expr
))
2275 return error_mark_node
;
2277 type
= TREE_TYPE (expr
);
2281 A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2282 of type int if int can represent all the values of the bit-field;
2283 otherwise, it can be converted to unsigned int if unsigned int can
2284 represent all the values of the bit-field. If the bit-field is larger yet,
2285 no integral promotion applies to it. If the bit-field has an enumerated
2286 type, it is treated as any other value of that type for promotion
2288 tree bitfield_type
= is_bitfield_expr_with_lowered_type (expr
);
2290 && (TREE_CODE (bitfield_type
) == ENUMERAL_TYPE
2291 || TYPE_PRECISION (type
) > TYPE_PRECISION (integer_type_node
)))
2292 type
= bitfield_type
;
2294 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
2295 /* Scoped enums don't promote. */
2296 if (SCOPED_ENUM_P (type
))
2298 promoted_type
= type_promotes_to (type
);
2299 if (type
!= promoted_type
)
2300 expr
= cp_convert (promoted_type
, expr
, complain
);
2301 else if (bitfield_type
&& bitfield_type
!= type
)
2302 /* Prevent decay_conversion from converting to bitfield_type. */
2303 expr
= build_nop (type
, expr
);
2310 perform_integral_promotions (tree expr
)
2312 return cp_perform_integral_promotions (expr
, tf_warning_or_error
);
2315 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2316 decay_conversion to one. */
2319 string_conv_p (const_tree totype
, const_tree exp
, int warn
)
2323 if (!TYPE_PTR_P (totype
))
2326 t
= TREE_TYPE (totype
);
2327 if (!same_type_p (t
, char_type_node
)
2328 && !same_type_p (t
, char8_type_node
)
2329 && !same_type_p (t
, char16_type_node
)
2330 && !same_type_p (t
, char32_type_node
)
2331 && !same_type_p (t
, wchar_type_node
))
2334 location_t loc
= EXPR_LOC_OR_LOC (exp
, input_location
);
2336 STRIP_ANY_LOCATION_WRAPPER (exp
);
2338 if (TREE_CODE (exp
) == STRING_CST
)
2340 /* Make sure that we don't try to convert between char and wide chars. */
2341 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
2346 /* Is this a string constant which has decayed to 'const char *'? */
2347 t
= build_pointer_type (cp_build_qualified_type (t
, TYPE_QUAL_CONST
));
2348 if (!same_type_p (TREE_TYPE (exp
), t
))
2351 if (TREE_CODE (exp
) != ADDR_EXPR
2352 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
2357 if (cxx_dialect
>= cxx11
)
2358 pedwarn (loc
, OPT_Wwrite_strings
,
2359 "ISO C++ forbids converting a string constant to %qT",
2362 warning_at (loc
, OPT_Wwrite_strings
,
2363 "deprecated conversion from string constant to %qT",
2370 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2371 can, for example, use as an lvalue. This code used to be in
2372 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2373 expressions, where we're dealing with aggregates. But now it's again only
2374 called from unary_complex_lvalue. The case (in particular) that led to
2375 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2379 rationalize_conditional_expr (enum tree_code code
, tree t
,
2380 tsubst_flags_t complain
)
2382 location_t loc
= cp_expr_loc_or_input_loc (t
);
2384 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2385 the first operand is always the one to be used if both operands
2386 are equal, so we know what conditional expression this used to be. */
2387 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
2389 tree op0
= TREE_OPERAND (t
, 0);
2390 tree op1
= TREE_OPERAND (t
, 1);
2392 /* The following code is incorrect if either operand side-effects. */
2393 gcc_assert (!TREE_SIDE_EFFECTS (op0
)
2394 && !TREE_SIDE_EFFECTS (op1
));
2396 build_conditional_expr (loc
,
2397 build_x_binary_op (loc
,
2398 (TREE_CODE (t
) == MIN_EXPR
2399 ? LE_EXPR
: GE_EXPR
),
2400 op0
, TREE_CODE (op0
),
2401 op1
, TREE_CODE (op1
),
2404 cp_build_unary_op (code
, op0
, false, complain
),
2405 cp_build_unary_op (code
, op1
, false, complain
),
2409 tree op1
= TREE_OPERAND (t
, 1);
2410 if (TREE_CODE (op1
) != THROW_EXPR
)
2411 op1
= cp_build_unary_op (code
, op1
, false, complain
);
2412 tree op2
= TREE_OPERAND (t
, 2);
2413 if (TREE_CODE (op2
) != THROW_EXPR
)
2414 op2
= cp_build_unary_op (code
, op2
, false, complain
);
2417 build_conditional_expr (loc
, TREE_OPERAND (t
, 0), op1
, op2
, complain
);
2420 /* Given the TYPE of an anonymous union field inside T, return the
2421 FIELD_DECL for the field. If not found return NULL_TREE. Because
2422 anonymous unions can nest, we must also search all anonymous unions
2423 that are directly reachable. */
2426 lookup_anon_field (tree t
, tree type
)
2430 t
= TYPE_MAIN_VARIANT (t
);
2432 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
2434 if (TREE_STATIC (field
))
2436 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
2439 /* If we find it directly, return the field. */
2440 if (DECL_NAME (field
) == NULL_TREE
2441 && type
== TYPE_MAIN_VARIANT (TREE_TYPE (field
)))
2446 /* Otherwise, it could be nested, search harder. */
2447 if (DECL_NAME (field
) == NULL_TREE
2448 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2450 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
2458 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2459 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2460 non-NULL, it indicates the path to the base used to name MEMBER.
2461 If PRESERVE_REFERENCE is true, the expression returned will have
2462 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2463 returned will have the type referred to by the reference.
2465 This function does not perform access control; that is either done
2466 earlier by the parser when the name of MEMBER is resolved to MEMBER
2467 itself, or later when overload resolution selects one of the
2468 functions indicated by MEMBER. */
2471 build_class_member_access_expr (cp_expr object
, tree member
,
2472 tree access_path
, bool preserve_reference
,
2473 tsubst_flags_t complain
)
2477 tree result
= NULL_TREE
;
2478 tree using_decl
= NULL_TREE
;
2480 if (error_operand_p (object
) || error_operand_p (member
))
2481 return error_mark_node
;
2483 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2487 The type of the first expression shall be "class object" (of a
2489 object_type
= TREE_TYPE (object
);
2490 if (!currently_open_class (object_type
)
2491 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2492 return error_mark_node
;
2493 if (!CLASS_TYPE_P (object_type
))
2495 if (complain
& tf_error
)
2497 if (INDIRECT_TYPE_P (object_type
)
2498 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2499 error ("request for member %qD in %qE, which is of pointer "
2500 "type %qT (maybe you meant to use %<->%> ?)",
2501 member
, object
.get_value (), object_type
);
2503 error ("request for member %qD in %qE, which is of non-class "
2504 "type %qT", member
, object
.get_value (), object_type
);
2506 return error_mark_node
;
2509 /* The standard does not seem to actually say that MEMBER must be a
2510 member of OBJECT_TYPE. However, that is clearly what is
2512 if (DECL_P (member
))
2514 member_scope
= DECL_CLASS_CONTEXT (member
);
2515 if (!mark_used (member
, complain
) && !(complain
& tf_error
))
2516 return error_mark_node
;
2517 if (TREE_DEPRECATED (member
))
2518 warn_deprecated_use (member
, NULL_TREE
);
2521 member_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (member
));
2522 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2523 presently be the anonymous union. Go outwards until we find a
2524 type related to OBJECT_TYPE. */
2525 while ((ANON_AGGR_TYPE_P (member_scope
) || UNSCOPED_ENUM_P (member_scope
))
2526 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
2528 member_scope
= TYPE_CONTEXT (member_scope
);
2529 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
2531 if (complain
& tf_error
)
2533 if (TREE_CODE (member
) == FIELD_DECL
)
2534 error ("invalid use of non-static data member %qE", member
);
2536 error ("%qD is not a member of %qT", member
, object_type
);
2538 return error_mark_node
;
2541 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2542 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2543 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2545 tree temp
= unary_complex_lvalue (ADDR_EXPR
, object
);
2548 temp
= cp_build_fold_indirect_ref (temp
);
2549 if (xvalue_p (object
) && !xvalue_p (temp
))
2550 /* Preserve xvalue kind. */
2556 /* In [expr.ref], there is an explicit list of the valid choices for
2557 MEMBER. We check for each of those cases here. */
2560 /* A static data member. */
2562 mark_exp_read (object
);
2564 if (tree wrap
= maybe_get_tls_wrapper_call (result
))
2565 /* Replace an evaluated use of the thread_local variable with
2566 a call to its wrapper. */
2569 /* If OBJECT has side-effects, they are supposed to occur. */
2570 if (TREE_SIDE_EFFECTS (object
))
2571 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
2573 else if (TREE_CODE (member
) == FIELD_DECL
)
2575 /* A non-static data member. */
2580 if (INDIRECT_REF_P (object
))
2582 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object
, 0)));
2584 null_object_p
= false;
2586 /* Convert OBJECT to the type of MEMBER. */
2587 if (!same_type_p (TYPE_MAIN_VARIANT (object_type
),
2588 TYPE_MAIN_VARIANT (member_scope
)))
2593 /* We didn't complain above about a currently open class, but now we
2594 must: we don't know how to refer to a base member before layout is
2595 complete. But still don't complain in a template. */
2596 if (!cp_unevaluated_operand
2597 && !dependent_type_p (object_type
)
2598 && !complete_type_or_maybe_complain (object_type
, object
,
2600 return error_mark_node
;
2602 binfo
= lookup_base (access_path
? access_path
: object_type
,
2603 member_scope
, ba_unique
, &kind
, complain
);
2604 if (binfo
== error_mark_node
)
2605 return error_mark_node
;
2607 /* It is invalid to try to get to a virtual base of a
2608 NULL object. The most common cause is invalid use of
2610 if (null_object_p
&& kind
== bk_via_virtual
)
2612 if (complain
& tf_error
)
2614 error ("invalid access to non-static data member %qD in "
2615 "virtual base of NULL object", member
);
2617 return error_mark_node
;
2620 /* Convert to the base. */
2621 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
2622 /*nonnull=*/1, complain
);
2623 /* If we found the base successfully then we should be able
2624 to convert to it successfully. */
2625 gcc_assert (object
!= error_mark_node
);
2628 /* If MEMBER is from an anonymous aggregate, we have converted
2629 OBJECT so that it refers to the class containing the
2630 anonymous union. Generate a reference to the anonymous union
2631 itself, and recur to find MEMBER. */
2632 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
))
2633 /* When this code is called from build_field_call, the
2634 object already has the type of the anonymous union.
2635 That is because the COMPONENT_REF was already
2636 constructed, and was then disassembled before calling
2637 build_field_call. After the function-call code is
2638 cleaned up, this waste can be eliminated. */
2639 && (!same_type_ignoring_top_level_qualifiers_p
2640 (TREE_TYPE (object
), DECL_CONTEXT (member
))))
2642 tree anonymous_union
;
2644 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
2645 DECL_CONTEXT (member
));
2646 object
= build_class_member_access_expr (object
,
2648 /*access_path=*/NULL_TREE
,
2653 /* Compute the type of the field, as described in [expr.ref]. */
2654 type_quals
= TYPE_UNQUALIFIED
;
2655 member_type
= TREE_TYPE (member
);
2656 if (!TYPE_REF_P (member_type
))
2658 type_quals
= (cp_type_quals (member_type
)
2659 | cp_type_quals (object_type
));
2661 /* A field is const (volatile) if the enclosing object, or the
2662 field itself, is const (volatile). But, a mutable field is
2663 not const, even within a const object. */
2664 if (DECL_MUTABLE_P (member
))
2665 type_quals
&= ~TYPE_QUAL_CONST
;
2666 member_type
= cp_build_qualified_type (member_type
, type_quals
);
2669 result
= build3_loc (input_location
, COMPONENT_REF
, member_type
,
2670 object
, member
, NULL_TREE
);
2672 /* Mark the expression const or volatile, as appropriate. Even
2673 though we've dealt with the type above, we still have to mark the
2674 expression itself. */
2675 if (type_quals
& TYPE_QUAL_CONST
)
2676 TREE_READONLY (result
) = 1;
2677 if (type_quals
& TYPE_QUAL_VOLATILE
)
2678 TREE_THIS_VOLATILE (result
) = 1;
2680 else if (BASELINK_P (member
))
2682 /* The member is a (possibly overloaded) member function. */
2686 /* If the MEMBER is exactly one static member function, then we
2687 know the type of the expression. Otherwise, we must wait
2688 until overload resolution has been performed. */
2689 functions
= BASELINK_FUNCTIONS (member
);
2690 if (TREE_CODE (functions
) == FUNCTION_DECL
2691 && DECL_STATIC_FUNCTION_P (functions
))
2692 type
= TREE_TYPE (functions
);
2694 type
= unknown_type_node
;
2695 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2696 base. That will happen when the function is called. */
2697 result
= build3_loc (input_location
, COMPONENT_REF
, type
, object
, member
,
2700 else if (TREE_CODE (member
) == CONST_DECL
)
2702 /* The member is an enumerator. */
2704 /* If OBJECT has side-effects, they are supposed to occur. */
2705 if (TREE_SIDE_EFFECTS (object
))
2706 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
2709 else if ((using_decl
= strip_using_decl (member
)) != member
)
2710 result
= build_class_member_access_expr (object
,
2712 access_path
, preserve_reference
,
2716 if (complain
& tf_error
)
2717 error ("invalid use of %qD", member
);
2718 return error_mark_node
;
2721 if (!preserve_reference
)
2724 If E2 is declared to have type "reference to T", then ... the
2725 type of E1.E2 is T. */
2726 result
= convert_from_reference (result
);
2731 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2732 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2735 lookup_destructor (tree object
, tree scope
, tree dtor_name
,
2736 tsubst_flags_t complain
)
2738 tree object_type
= TREE_TYPE (object
);
2739 tree dtor_type
= TREE_OPERAND (dtor_name
, 0);
2742 /* We've already complained about this destructor. */
2743 if (dtor_type
== error_mark_node
)
2744 return error_mark_node
;
2746 if (scope
&& !check_dtor_name (scope
, dtor_type
))
2748 if (complain
& tf_error
)
2749 error ("qualified type %qT does not match destructor name ~%qT",
2751 return error_mark_node
;
2753 if (is_auto (dtor_type
))
2754 dtor_type
= object_type
;
2755 else if (identifier_p (dtor_type
))
2757 /* In a template, names we can't find a match for are still accepted
2758 destructor names, and we check them here. */
2759 if (check_dtor_name (object_type
, dtor_type
))
2760 dtor_type
= object_type
;
2763 if (complain
& tf_error
)
2764 error ("object type %qT does not match destructor name ~%qT",
2765 object_type
, dtor_type
);
2766 return error_mark_node
;
2770 else if (!DERIVED_FROM_P (dtor_type
, TYPE_MAIN_VARIANT (object_type
)))
2772 if (complain
& tf_error
)
2773 error ("the type being destroyed is %qT, but the destructor "
2774 "refers to %qT", TYPE_MAIN_VARIANT (object_type
), dtor_type
);
2775 return error_mark_node
;
2777 expr
= lookup_member (dtor_type
, complete_dtor_identifier
,
2778 /*protect=*/1, /*want_type=*/false,
2779 tf_warning_or_error
);
2782 if (complain
& tf_error
)
2783 cxx_incomplete_type_error (dtor_name
, dtor_type
);
2784 return error_mark_node
;
2786 expr
= (adjust_result_of_qualified_name_lookup
2787 (expr
, dtor_type
, object_type
));
2788 if (scope
== NULL_TREE
)
2789 /* We need to call adjust_result_of_qualified_name_lookup in case the
2790 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2791 that we still get virtual function binding. */
2792 BASELINK_QUALIFIED_P (expr
) = false;
2796 /* An expression of the form "A::template B" has been resolved to
2797 DECL. Issue a diagnostic if B is not a template or template
2801 check_template_keyword (tree decl
)
2803 /* The standard says:
2807 If a name prefixed by the keyword template is not a member
2808 template, the program is ill-formed.
2810 DR 228 removed the restriction that the template be a member
2813 DR 96, if accepted would add the further restriction that explicit
2814 template arguments must be provided if the template keyword is
2815 used, but, as of 2005-10-16, that DR is still in "drafting". If
2816 this DR is accepted, then the semantic checks here can be
2817 simplified, as the entity named must in fact be a template
2818 specialization, rather than, as at present, a set of overloaded
2819 functions containing at least one template function. */
2820 if (TREE_CODE (decl
) != TEMPLATE_DECL
2821 && TREE_CODE (decl
) != TEMPLATE_ID_EXPR
)
2825 if (DECL_USE_TEMPLATE (decl
)
2826 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2829 permerror (input_location
, "%qD is not a template", decl
);
2831 else if (!is_overloaded_fn (decl
))
2832 permerror (input_location
, "%qD is not a template", decl
);
2837 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
2838 !found
&& iter
; ++iter
)
2841 if (TREE_CODE (fn
) == TEMPLATE_DECL
2842 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
2843 || (TREE_CODE (fn
) == FUNCTION_DECL
2844 && DECL_USE_TEMPLATE (fn
)
2845 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn
))))
2849 permerror (input_location
, "%qD is not a template", decl
);
2854 /* Record that an access failure occurred on BASETYPE_PATH attempting
2855 to access DECL, where DIAG_DECL should be used for diagnostics. */
2858 access_failure_info::record_access_failure (tree basetype_path
,
2859 tree decl
, tree diag_decl
)
2861 m_was_inaccessible
= true;
2862 m_basetype_path
= basetype_path
;
2864 m_diag_decl
= diag_decl
;
2867 /* If an access failure was recorded, then attempt to locate an
2868 accessor function for the pertinent field.
2869 Otherwise, return NULL_TREE. */
2872 access_failure_info::get_any_accessor (bool const_p
) const
2874 if (!was_inaccessible_p ())
2878 = locate_field_accessor (m_basetype_path
, m_diag_decl
, const_p
);
2882 /* The accessor must itself be accessible for it to be a reasonable
2884 if (!accessible_p (m_basetype_path
, accessor
, true))
2890 /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
2891 replacing the primary location in RICHLOC with "accessor()". */
2894 access_failure_info::add_fixit_hint (rich_location
*richloc
,
2898 pp_printf (&pp
, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor_decl
)));
2899 richloc
->add_fixit_replace (pp_formatted_text (&pp
));
2902 /* If an access failure was recorded, then attempt to locate an
2903 accessor function for the pertinent field, and if one is
2904 available, add a note and fix-it hint suggesting using it. */
2907 access_failure_info::maybe_suggest_accessor (bool const_p
) const
2909 tree accessor
= get_any_accessor (const_p
);
2910 if (accessor
== NULL_TREE
)
2912 rich_location
richloc (line_table
, input_location
);
2913 add_fixit_hint (&richloc
, accessor
);
2914 inform (&richloc
, "field %q#D can be accessed via %q#D",
2915 m_diag_decl
, accessor
);
2918 /* Subroutine of finish_class_member_access_expr.
2919 Issue an error about NAME not being a member of ACCESS_PATH (or
2920 OBJECT_TYPE), potentially providing a fix-it hint for misspelled
2924 complain_about_unrecognized_member (tree access_path
, tree name
,
2927 /* Attempt to provide a hint about misspelled names. */
2928 tree guessed_id
= lookup_member_fuzzy (access_path
, name
,
2929 /*want_type=*/false);
2930 if (guessed_id
== NULL_TREE
)
2933 error ("%q#T has no member named %qE",
2934 TREE_CODE (access_path
) == TREE_BINFO
2935 ? TREE_TYPE (access_path
) : object_type
, name
);
2939 location_t bogus_component_loc
= input_location
;
2940 gcc_rich_location
rich_loc (bogus_component_loc
);
2942 /* Check that the guessed name is accessible along access_path. */
2943 access_failure_info afi
;
2944 lookup_member (access_path
, guessed_id
, /*protect=*/1,
2945 /*want_type=*/false, /*complain=*/false,
2947 if (afi
.was_inaccessible_p ())
2949 tree accessor
= afi
.get_any_accessor (TYPE_READONLY (object_type
));
2952 /* The guessed name isn't directly accessible, but can be accessed
2953 via an accessor member function. */
2954 afi
.add_fixit_hint (&rich_loc
, accessor
);
2955 error_at (&rich_loc
,
2956 "%q#T has no member named %qE;"
2957 " did you mean %q#D? (accessible via %q#D)",
2958 TREE_CODE (access_path
) == TREE_BINFO
2959 ? TREE_TYPE (access_path
) : object_type
,
2960 name
, afi
.get_diag_decl (), accessor
);
2964 /* The guessed name isn't directly accessible, and no accessor
2965 member function could be found. */
2966 error_at (&rich_loc
,
2967 "%q#T has no member named %qE;"
2968 " did you mean %q#D? (not accessible from this context)",
2969 TREE_CODE (access_path
) == TREE_BINFO
2970 ? TREE_TYPE (access_path
) : object_type
,
2971 name
, afi
.get_diag_decl ());
2972 complain_about_access (afi
.get_decl (), afi
.get_diag_decl (), false);
2977 /* The guessed name is directly accessible; suggest it. */
2978 rich_loc
.add_fixit_misspelled_id (bogus_component_loc
,
2980 error_at (&rich_loc
,
2981 "%q#T has no member named %qE;"
2982 " did you mean %qE?",
2983 TREE_CODE (access_path
) == TREE_BINFO
2984 ? TREE_TYPE (access_path
) : object_type
,
2989 /* This function is called by the parser to process a class member
2990 access expression of the form OBJECT.NAME. NAME is a node used by
2991 the parser to represent a name; it is not yet a DECL. It may,
2992 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2993 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2994 there is no reason to do the lookup twice, so the parser keeps the
2995 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2996 be a template via the use of the "A::template B" syntax. */
2999 finish_class_member_access_expr (cp_expr object
, tree name
, bool template_p
,
3000 tsubst_flags_t complain
)
3005 tree access_path
= NULL_TREE
;
3006 tree orig_object
= object
;
3007 tree orig_name
= name
;
3009 if (object
== error_mark_node
|| name
== error_mark_node
)
3010 return error_mark_node
;
3012 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3013 if (!objc_is_public (object
, name
))
3014 return error_mark_node
;
3016 object_type
= TREE_TYPE (object
);
3018 if (processing_template_decl
)
3020 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3021 type_dependent_object_expression_p (object
)
3022 /* If NAME is "f<args>", where either 'f' or 'args' is
3023 dependent, then the expression is dependent. */
3024 || (TREE_CODE (name
) == TEMPLATE_ID_EXPR
3025 && dependent_template_id_p (TREE_OPERAND (name
, 0),
3026 TREE_OPERAND (name
, 1)))
3027 /* If NAME is "T::X" where "T" is dependent, then the
3028 expression is dependent. */
3029 || (TREE_CODE (name
) == SCOPE_REF
3030 && TYPE_P (TREE_OPERAND (name
, 0))
3031 && dependent_scope_p (TREE_OPERAND (name
, 0)))
3032 /* If NAME is operator T where "T" is dependent, we can't
3033 lookup until we instantiate the T. */
3034 || (TREE_CODE (name
) == IDENTIFIER_NODE
3035 && IDENTIFIER_CONV_OP_P (name
)
3036 && dependent_type_p (TREE_TYPE (name
))))
3039 return build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
,
3040 orig_object
, orig_name
, NULL_TREE
);
3042 object
= build_non_dependent_expr (object
);
3044 else if (c_dialect_objc ()
3045 && identifier_p (name
)
3046 && (expr
= objc_maybe_build_component_ref (object
, name
)))
3051 The type of the first expression shall be "class object" (of a
3053 if (!currently_open_class (object_type
)
3054 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
3055 return error_mark_node
;
3056 if (!CLASS_TYPE_P (object_type
))
3058 if (complain
& tf_error
)
3060 if (INDIRECT_TYPE_P (object_type
)
3061 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
3062 error ("request for member %qD in %qE, which is of pointer "
3063 "type %qT (maybe you meant to use %<->%> ?)",
3064 name
, object
.get_value (), object_type
);
3066 error ("request for member %qD in %qE, which is of non-class "
3067 "type %qT", name
, object
.get_value (), object_type
);
3069 return error_mark_node
;
3072 if (BASELINK_P (name
))
3073 /* A member function that has already been looked up. */
3077 bool is_template_id
= false;
3078 tree template_args
= NULL_TREE
;
3079 tree scope
= NULL_TREE
;
3081 access_path
= object_type
;
3083 if (TREE_CODE (name
) == SCOPE_REF
)
3085 /* A qualified name. The qualifying class or namespace `S'
3086 has already been looked up; it is either a TYPE or a
3088 scope
= TREE_OPERAND (name
, 0);
3089 name
= TREE_OPERAND (name
, 1);
3091 /* If SCOPE is a namespace, then the qualified name does not
3092 name a member of OBJECT_TYPE. */
3093 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
3095 if (complain
& tf_error
)
3096 error ("%<%D::%D%> is not a member of %qT",
3097 scope
, name
, object_type
);
3098 return error_mark_node
;
3102 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
3104 is_template_id
= true;
3105 template_args
= TREE_OPERAND (name
, 1);
3106 name
= TREE_OPERAND (name
, 0);
3108 if (!identifier_p (name
))
3109 name
= OVL_NAME (name
);
3114 if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
3116 gcc_assert (!is_template_id
);
3117 /* Looking up a member enumerator (c++/56793). */
3118 if (!TYPE_CLASS_SCOPE_P (scope
)
3119 || !DERIVED_FROM_P (TYPE_CONTEXT (scope
), object_type
))
3121 if (complain
& tf_error
)
3122 error ("%<%D::%D%> is not a member of %qT",
3123 scope
, name
, object_type
);
3124 return error_mark_node
;
3126 tree val
= lookup_enumerator (scope
, name
);
3129 if (complain
& tf_error
)
3130 error ("%qD is not a member of %qD",
3132 return error_mark_node
;
3135 if (TREE_SIDE_EFFECTS (object
))
3136 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
), object
, val
);
3140 gcc_assert (CLASS_TYPE_P (scope
));
3141 gcc_assert (identifier_p (name
) || TREE_CODE (name
) == BIT_NOT_EXPR
);
3143 if (constructor_name_p (name
, scope
))
3145 if (complain
& tf_error
)
3146 error ("cannot call constructor %<%T::%D%> directly",
3148 return error_mark_node
;
3151 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3152 access_path
= lookup_base (object_type
, scope
, ba_check
,
3154 if (access_path
== error_mark_node
)
3155 return error_mark_node
;
3158 if (any_dependent_bases_p (object_type
))
3160 if (complain
& tf_error
)
3161 error ("%qT is not a base of %qT", scope
, object_type
);
3162 return error_mark_node
;
3166 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
3168 if (dependent_type_p (object_type
))
3169 /* The destructor isn't declared yet. */
3171 member
= lookup_destructor (object
, scope
, name
, complain
);
3175 /* Look up the member. */
3176 access_failure_info afi
;
3177 member
= lookup_member (access_path
, name
, /*protect=*/1,
3178 /*want_type=*/false, complain
,
3180 afi
.maybe_suggest_accessor (TYPE_READONLY (object_type
));
3181 if (member
== NULL_TREE
)
3183 if (dependent_type_p (object_type
))
3184 /* Try again at instantiation time. */
3186 if (complain
& tf_error
)
3187 complain_about_unrecognized_member (access_path
, name
,
3189 return error_mark_node
;
3191 if (member
== error_mark_node
)
3192 return error_mark_node
;
3194 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member
)))
3195 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3196 wrong, so don't use it. */
3198 if (TREE_CODE (member
) == USING_DECL
&& DECL_DEPENDENT_P (member
))
3204 tree templ
= member
;
3206 if (BASELINK_P (templ
))
3207 member
= lookup_template_function (templ
, template_args
);
3208 else if (variable_template_p (templ
))
3209 member
= (lookup_and_finish_template_variable
3210 (templ
, template_args
, complain
));
3213 if (complain
& tf_error
)
3214 error ("%qD is not a member template function", name
);
3215 return error_mark_node
;
3220 if (TREE_DEPRECATED (member
))
3221 warn_deprecated_use (member
, NULL_TREE
);
3224 check_template_keyword (member
);
3226 expr
= build_class_member_access_expr (object
, member
, access_path
,
3227 /*preserve_reference=*/false,
3229 if (processing_template_decl
&& expr
!= error_mark_node
)
3231 if (BASELINK_P (member
))
3233 if (TREE_CODE (orig_name
) == SCOPE_REF
)
3234 BASELINK_QUALIFIED_P (member
) = 1;
3237 return build_min_non_dep (COMPONENT_REF
, expr
,
3238 orig_object
, orig_name
,
3245 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3249 build_simple_component_ref (tree object
, tree member
)
3251 tree type
= cp_build_qualified_type (TREE_TYPE (member
),
3252 cp_type_quals (TREE_TYPE (object
)));
3253 return build3_loc (input_location
,
3254 COMPONENT_REF
, type
,
3255 object
, member
, NULL_TREE
);
3258 /* Return an expression for the MEMBER_NAME field in the internal
3259 representation of PTRMEM, a pointer-to-member function. (Each
3260 pointer-to-member function type gets its own RECORD_TYPE so it is
3261 more convenient to access the fields by name than by FIELD_DECL.)
3262 This routine converts the NAME to a FIELD_DECL and then creates the
3263 node for the complete expression. */
3266 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
3271 if (TREE_CODE (ptrmem
) == CONSTRUCTOR
)
3275 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ptrmem
),
3277 if (index
&& DECL_P (index
) && DECL_NAME (index
) == member_name
)
3282 /* This code is a stripped down version of
3283 build_class_member_access_expr. It does not work to use that
3284 routine directly because it expects the object to be of class
3286 ptrmem_type
= TREE_TYPE (ptrmem
);
3287 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
));
3288 for (member
= TYPE_FIELDS (ptrmem_type
); member
;
3289 member
= DECL_CHAIN (member
))
3290 if (DECL_NAME (member
) == member_name
)
3292 tree res
= build_simple_component_ref (ptrmem
, member
);
3294 TREE_NO_WARNING (res
) = 1;
3298 /* Given an expression PTR for a pointer, return an expression
3299 for the value pointed to.
3300 ERRORSTRING is the name of the operator to appear in error messages.
3302 This function may need to overload OPERATOR_FNNAME.
3303 Must also handle REFERENCE_TYPEs for C++. */
3306 build_x_indirect_ref (location_t loc
, tree expr
, ref_operator errorstring
,
3307 tsubst_flags_t complain
)
3309 tree orig_expr
= expr
;
3311 tree overload
= NULL_TREE
;
3313 if (processing_template_decl
)
3315 /* Retain the type if we know the operand is a pointer. */
3316 if (TREE_TYPE (expr
) && INDIRECT_TYPE_P (TREE_TYPE (expr
)))
3317 return build_min (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
3318 if (type_dependent_expression_p (expr
))
3319 return build_min_nt_loc (loc
, INDIRECT_REF
, expr
);
3320 expr
= build_non_dependent_expr (expr
);
3323 rval
= build_new_op (loc
, INDIRECT_REF
, LOOKUP_NORMAL
, expr
,
3324 NULL_TREE
, NULL_TREE
, &overload
, complain
);
3326 rval
= cp_build_indirect_ref (loc
, expr
, errorstring
, complain
);
3328 if (processing_template_decl
&& rval
!= error_mark_node
)
3330 if (overload
!= NULL_TREE
)
3331 return (build_min_non_dep_op_overload
3332 (INDIRECT_REF
, rval
, overload
, orig_expr
));
3334 return build_min_non_dep (INDIRECT_REF
, rval
, orig_expr
);
3340 /* Like c-family strict_aliasing_warning, but don't warn for dependent
3341 types or expressions. */
3344 cp_strict_aliasing_warning (location_t loc
, tree type
, tree expr
)
3346 if (processing_template_decl
)
3350 if (dependent_type_p (type
) || type_dependent_expression_p (e
))
3353 return strict_aliasing_warning (loc
, type
, expr
);
3356 /* The implementation of the above, and of indirection implied by other
3357 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3360 cp_build_indirect_ref_1 (location_t loc
, tree ptr
, ref_operator errorstring
,
3361 tsubst_flags_t complain
, bool do_fold
)
3365 /* RO_NULL should only be used with the folding entry points below, not
3366 cp_build_indirect_ref. */
3367 gcc_checking_assert (errorstring
!= RO_NULL
|| do_fold
);
3369 if (ptr
== current_class_ptr
3370 || (TREE_CODE (ptr
) == NOP_EXPR
3371 && TREE_OPERAND (ptr
, 0) == current_class_ptr
3372 && (same_type_ignoring_top_level_qualifiers_p
3373 (TREE_TYPE (ptr
), TREE_TYPE (current_class_ptr
)))))
3374 return current_class_ref
;
3376 pointer
= (TYPE_REF_P (TREE_TYPE (ptr
))
3377 ? ptr
: decay_conversion (ptr
, complain
));
3378 if (pointer
== error_mark_node
)
3379 return error_mark_node
;
3381 type
= TREE_TYPE (pointer
);
3383 if (INDIRECT_TYPE_P (type
))
3387 If the type of the expression is "pointer to T," the type
3388 of the result is "T." */
3389 tree t
= TREE_TYPE (type
);
3391 if ((CONVERT_EXPR_P (ptr
)
3392 || TREE_CODE (ptr
) == VIEW_CONVERT_EXPR
)
3393 && (!CLASS_TYPE_P (t
) || !CLASSTYPE_EMPTY_P (t
)))
3395 /* If a warning is issued, mark it to avoid duplicates from
3396 the backend. This only needs to be done at
3397 warn_strict_aliasing > 2. */
3398 if (warn_strict_aliasing
> 2
3399 && cp_strict_aliasing_warning (EXPR_LOCATION (ptr
),
3400 type
, TREE_OPERAND (ptr
, 0)))
3401 TREE_NO_WARNING (ptr
) = 1;
3404 if (VOID_TYPE_P (t
))
3406 /* A pointer to incomplete type (other than cv void) can be
3407 dereferenced [expr.unary.op]/1 */
3408 if (complain
& tf_error
)
3409 error_at (loc
, "%qT is not a pointer-to-object type", type
);
3410 return error_mark_node
;
3412 else if (do_fold
&& TREE_CODE (pointer
) == ADDR_EXPR
3413 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
3414 /* The POINTER was something like `&x'. We simplify `*&x' to
3416 return TREE_OPERAND (pointer
, 0);
3419 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
3421 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3422 so that we get the proper error message if the result is used
3423 to assign to. Also, &* is supposed to be a no-op. */
3424 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
3425 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
3426 TREE_SIDE_EFFECTS (ref
)
3427 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
));
3431 else if (!(complain
& tf_error
))
3432 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3434 /* `pointer' won't be an error_mark_node if we were given a
3435 pointer to member, so it's cool to check for this here. */
3436 else if (TYPE_PTRMEM_P (type
))
3437 switch (errorstring
)
3439 case RO_ARRAY_INDEXING
:
3441 "invalid use of array indexing on pointer to member");
3444 error_at (loc
, "invalid use of unary %<*%> on pointer to member");
3446 case RO_IMPLICIT_CONVERSION
:
3447 error_at (loc
, "invalid use of implicit conversion on pointer "
3451 error_at (loc
, "left hand operand of %<->*%> must be a pointer to "
3452 "class, but is a pointer to member of type %qT", type
);
3457 else if (pointer
!= error_mark_node
)
3458 invalid_indirection_error (loc
, type
, errorstring
);
3460 return error_mark_node
;
3463 /* Entry point used by c-common, which expects folding. */
3466 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errorstring
)
3468 return cp_build_indirect_ref_1 (loc
, ptr
, errorstring
,
3469 tf_warning_or_error
, true);
3472 /* Entry point used by internal indirection needs that don't correspond to any
3473 syntactic construct. */
3476 cp_build_fold_indirect_ref (tree pointer
)
3478 return cp_build_indirect_ref_1 (input_location
, pointer
, RO_NULL
,
3479 tf_warning_or_error
, true);
3482 /* Entry point used by indirection needs that correspond to some syntactic
3486 cp_build_indirect_ref (location_t loc
, tree ptr
, ref_operator errorstring
,
3487 tsubst_flags_t complain
)
3489 return cp_build_indirect_ref_1 (loc
, ptr
, errorstring
, complain
, false);
3492 /* This handles expressions of the form "a[i]", which denotes
3495 This is logically equivalent in C to *(a+i), but we may do it differently.
3496 If A is a variable or a member, we generate a primitive ARRAY_REF.
3497 This avoids forcing the array out of registers, and can work on
3498 arrays that are not lvalues (for example, members of structures returned
3501 If INDEX is of some user-defined type, it must be converted to
3502 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3503 will inherit the type of the array, which will be some pointer type.
3505 LOC is the location to use in building the array reference. */
3508 cp_build_array_ref (location_t loc
, tree array
, tree idx
,
3509 tsubst_flags_t complain
)
3515 if (complain
& tf_error
)
3516 error_at (loc
, "subscript missing in array reference");
3517 return error_mark_node
;
3520 if (TREE_TYPE (array
) == error_mark_node
3521 || TREE_TYPE (idx
) == error_mark_node
)
3522 return error_mark_node
;
3524 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3526 switch (TREE_CODE (array
))
3530 tree value
= cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3532 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3533 TREE_OPERAND (array
, 0), value
);
3534 SET_EXPR_LOCATION (ret
, loc
);
3539 ret
= build_conditional_expr
3540 (loc
, TREE_OPERAND (array
, 0),
3541 cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3543 cp_build_array_ref (loc
, TREE_OPERAND (array
, 2), idx
,
3546 protected_set_expr_location (ret
, loc
);
3553 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, idx
);
3555 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
3559 warn_array_subscript_with_type_char (loc
, idx
);
3561 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx
)))
3563 if (complain
& tf_error
)
3564 error_at (loc
, "array subscript is not an integer");
3565 return error_mark_node
;
3568 /* Apply integral promotions *after* noticing character types.
3569 (It is unclear why we do these promotions -- the standard
3570 does not say that we should. In fact, the natural thing would
3571 seem to be to convert IDX to ptrdiff_t; we're performing
3572 pointer arithmetic.) */
3573 idx
= cp_perform_integral_promotions (idx
, complain
);
3575 idx
= maybe_fold_non_dependent_expr (idx
, complain
);
3577 /* An array that is indexed by a non-constant
3578 cannot be stored in a register; we must be able to do
3579 address arithmetic on its address.
3580 Likewise an array of elements of variable size. */
3581 if (TREE_CODE (idx
) != INTEGER_CST
3582 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
3583 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
3586 if (!cxx_mark_addressable (array
, true))
3587 return error_mark_node
;
3590 /* An array that is indexed by a constant value which is not within
3591 the array bounds cannot be stored in a register either; because we
3592 would get a crash in store_bit_field/extract_bit_field when trying
3593 to access a non-existent part of the register. */
3594 if (TREE_CODE (idx
) == INTEGER_CST
3595 && TYPE_DOMAIN (TREE_TYPE (array
))
3596 && ! int_fits_type_p (idx
, TYPE_DOMAIN (TREE_TYPE (array
))))
3598 if (!cxx_mark_addressable (array
))
3599 return error_mark_node
;
3602 /* Note in C++ it is valid to subscript a `register' array, since
3603 it is valid to take the address of something with that
3604 storage specification. */
3608 while (TREE_CODE (foo
) == COMPONENT_REF
)
3609 foo
= TREE_OPERAND (foo
, 0);
3610 if (VAR_P (foo
) && DECL_REGISTER (foo
)
3611 && (complain
& tf_warning
))
3612 warning_at (loc
, OPT_Wextra
,
3613 "subscripting array declared %<register%>");
3616 type
= TREE_TYPE (TREE_TYPE (array
));
3617 rval
= build4 (ARRAY_REF
, type
, array
, idx
, NULL_TREE
, NULL_TREE
);
3618 /* Array ref is const/volatile if the array elements are
3619 or if the array is.. */
3620 TREE_READONLY (rval
)
3621 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
3622 TREE_SIDE_EFFECTS (rval
)
3623 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
3624 TREE_THIS_VOLATILE (rval
)
3625 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
3626 ret
= require_complete_type_sfinae (rval
, complain
);
3627 protected_set_expr_location (ret
, loc
);
3629 ret
= non_lvalue_loc (loc
, ret
);
3634 tree ar
= cp_default_conversion (array
, complain
);
3635 tree ind
= cp_default_conversion (idx
, complain
);
3636 tree first
= NULL_TREE
;
3638 if (flag_strong_eval_order
== 2 && TREE_SIDE_EFFECTS (ind
))
3639 ar
= first
= save_expr (ar
);
3641 /* Put the integer in IND to simplify error checking. */
3642 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
3643 std::swap (ar
, ind
);
3645 if (ar
== error_mark_node
|| ind
== error_mark_node
)
3646 return error_mark_node
;
3648 if (!TYPE_PTR_P (TREE_TYPE (ar
)))
3650 if (complain
& tf_error
)
3651 error_at (loc
, "subscripted value is neither array nor pointer");
3652 return error_mark_node
;
3654 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
3656 if (complain
& tf_error
)
3657 error_at (loc
, "array subscript is not an integer");
3658 return error_mark_node
;
3661 warn_array_subscript_with_type_char (loc
, idx
);
3663 ret
= cp_build_binary_op (input_location
, PLUS_EXPR
, ar
, ind
, complain
);
3665 ret
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (ret
), first
, ret
);
3666 ret
= cp_build_indirect_ref (loc
, ret
, RO_ARRAY_INDEXING
, complain
);
3667 protected_set_expr_location (ret
, loc
);
3669 ret
= non_lvalue_loc (loc
, ret
);
3674 /* Entry point for Obj-C++. */
3677 build_array_ref (location_t loc
, tree array
, tree idx
)
3679 return cp_build_array_ref (loc
, array
, idx
, tf_warning_or_error
);
3682 /* Resolve a pointer to member function. INSTANCE is the object
3683 instance to use, if the member points to a virtual member.
3685 This used to avoid checking for virtual functions if basetype
3686 has no virtual functions, according to an earlier ANSI draft.
3687 With the final ISO C++ rules, such an optimization is
3688 incorrect: A pointer to a derived member can be static_cast
3689 to pointer-to-base-member, as long as the dynamic object
3690 later has the right member. So now we only do this optimization
3691 when we know the dynamic type of the object. */
3694 get_member_function_from_ptrfunc (tree
*instance_ptrptr
, tree function
,
3695 tsubst_flags_t complain
)
3697 if (TREE_CODE (function
) == OFFSET_REF
)
3698 function
= TREE_OPERAND (function
, 1);
3700 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
3702 tree idx
, delta
, e1
, e2
, e3
, vtbl
;
3704 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
3705 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
3707 tree instance_ptr
= *instance_ptrptr
;
3708 tree instance_save_expr
= 0;
3709 if (instance_ptr
== error_mark_node
)
3711 if (TREE_CODE (function
) == PTRMEM_CST
)
3713 /* Extracting the function address from a pmf is only
3714 allowed with -Wno-pmf-conversions. It only works for
3716 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
), complain
);
3717 e1
= convert (fntype
, e1
);
3722 if (complain
& tf_error
)
3723 error ("object missing in use of %qE", function
);
3724 return error_mark_node
;
3728 /* True if we know that the dynamic type of the object doesn't have
3729 virtual functions, so we can assume the PFN field is a pointer. */
3730 nonvirtual
= (COMPLETE_TYPE_P (basetype
)
3731 && !TYPE_POLYMORPHIC_P (basetype
)
3732 && resolves_to_fixed_type_p (instance_ptr
, 0));
3734 /* If we don't really have an object (i.e. in an ill-formed
3735 conversion from PMF to pointer), we can't resolve virtual
3736 functions anyway. */
3737 if (!nonvirtual
&& is_dummy_object (instance_ptr
))
3740 if (TREE_SIDE_EFFECTS (instance_ptr
))
3741 instance_ptr
= instance_save_expr
= save_expr (instance_ptr
);
3743 if (TREE_SIDE_EFFECTS (function
))
3744 function
= save_expr (function
);
3746 /* Start by extracting all the information from the PMF itself. */
3747 e3
= pfn_from_ptrmemfunc (function
);
3748 delta
= delta_from_ptrmemfunc (function
);
3749 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
3750 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
3752 int flag_sanitize_save
;
3753 case ptrmemfunc_vbit_in_pfn
:
3754 e1
= cp_build_binary_op (input_location
,
3755 BIT_AND_EXPR
, idx
, integer_one_node
,
3757 idx
= cp_build_binary_op (input_location
,
3758 MINUS_EXPR
, idx
, integer_one_node
,
3760 if (idx
== error_mark_node
)
3761 return error_mark_node
;
3764 case ptrmemfunc_vbit_in_delta
:
3765 e1
= cp_build_binary_op (input_location
,
3766 BIT_AND_EXPR
, delta
, integer_one_node
,
3768 /* Don't instrument the RSHIFT_EXPR we're about to create because
3769 we're going to use DELTA number of times, and that wouldn't play
3770 well with SAVE_EXPRs therein. */
3771 flag_sanitize_save
= flag_sanitize
;
3773 delta
= cp_build_binary_op (input_location
,
3774 RSHIFT_EXPR
, delta
, integer_one_node
,
3776 flag_sanitize
= flag_sanitize_save
;
3777 if (delta
== error_mark_node
)
3778 return error_mark_node
;
3785 if (e1
== error_mark_node
)
3786 return error_mark_node
;
3788 /* Convert down to the right base before using the instance. A
3789 special case is that in a pointer to member of class C, C may
3790 be incomplete. In that case, the function will of course be
3791 a member of C, and no conversion is required. In fact,
3792 lookup_base will fail in that case, because incomplete
3793 classes do not have BINFOs. */
3794 if (!same_type_ignoring_top_level_qualifiers_p
3795 (basetype
, TREE_TYPE (TREE_TYPE (instance_ptr
))))
3797 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
3798 basetype
, ba_check
, NULL
, complain
);
3799 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
,
3801 if (instance_ptr
== error_mark_node
)
3802 return error_mark_node
;
3804 /* ...and then the delta in the PMF. */
3805 instance_ptr
= fold_build_pointer_plus (instance_ptr
, delta
);
3807 /* Hand back the adjusted 'this' argument to our caller. */
3808 *instance_ptrptr
= instance_ptr
;
3811 /* Now just return the pointer. */
3814 /* Next extract the vtable pointer from the object. */
3815 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
3817 vtbl
= cp_build_fold_indirect_ref (vtbl
);
3818 if (vtbl
== error_mark_node
)
3819 return error_mark_node
;
3821 /* Finally, extract the function pointer from the vtable. */
3822 e2
= fold_build_pointer_plus_loc (input_location
, vtbl
, idx
);
3823 e2
= cp_build_fold_indirect_ref (e2
);
3824 if (e2
== error_mark_node
)
3825 return error_mark_node
;
3826 TREE_CONSTANT (e2
) = 1;
3828 /* When using function descriptors, the address of the
3829 vtable entry is treated as a function pointer. */
3830 if (TARGET_VTABLE_USES_DESCRIPTORS
)
3831 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
3832 cp_build_addr_expr (e2
, complain
));
3834 e2
= fold_convert (TREE_TYPE (e3
), e2
);
3835 e1
= build_conditional_expr (input_location
, e1
, e2
, e3
, complain
);
3836 if (e1
== error_mark_node
)
3837 return error_mark_node
;
3839 /* Make sure this doesn't get evaluated first inside one of the
3840 branches of the COND_EXPR. */
3841 if (instance_save_expr
)
3842 e1
= build2 (COMPOUND_EXPR
, TREE_TYPE (e1
),
3843 instance_save_expr
, e1
);
3850 /* Used by the C-common bits. */
3852 build_function_call (location_t
/*loc*/,
3853 tree function
, tree params
)
3855 return cp_build_function_call (function
, params
, tf_warning_or_error
);
3858 /* Used by the C-common bits. */
3860 build_function_call_vec (location_t
/*loc*/, vec
<location_t
> /*arg_loc*/,
3861 tree function
, vec
<tree
, va_gc
> *params
,
3862 vec
<tree
, va_gc
> * /*origtypes*/, tree orig_function
)
3864 vec
<tree
, va_gc
> *orig_params
= params
;
3865 tree ret
= cp_build_function_call_vec (function
, ¶ms
,
3866 tf_warning_or_error
, orig_function
);
3868 /* cp_build_function_call_vec can reallocate PARAMS by adding
3869 default arguments. That should never happen here. Verify
3871 gcc_assert (params
== orig_params
);
3876 /* Build a function call using a tree list of arguments. */
3879 cp_build_function_call (tree function
, tree params
, tsubst_flags_t complain
)
3884 for (; params
!= NULL_TREE
; params
= TREE_CHAIN (params
))
3885 vec_safe_push (vec
, TREE_VALUE (params
));
3886 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3890 /* Build a function call using varargs. */
3893 cp_build_function_call_nary (tree function
, tsubst_flags_t complain
, ...)
3899 va_start (args
, complain
);
3900 for (t
= va_arg (args
, tree
); t
!= NULL_TREE
; t
= va_arg (args
, tree
))
3901 vec_safe_push (vec
, t
);
3903 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3907 /* Build a function call using a vector of arguments.
3908 If FUNCTION is the result of resolving an overloaded target built-in,
3909 ORIG_FNDECL is the original function decl, otherwise it is null.
3910 PARAMS may be NULL if there are no parameters. This changes the
3911 contents of PARAMS. */
3914 cp_build_function_call_vec (tree function
, vec
<tree
, va_gc
> **params
,
3915 tsubst_flags_t complain
, tree orig_fndecl
)
3917 tree fntype
, fndecl
;
3919 tree original
= function
;
3923 vec
<tree
, va_gc
> *allocated
= NULL
;
3926 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3927 expressions, like those used for ObjC messenger dispatches. */
3928 if (params
!= NULL
&& !vec_safe_is_empty (*params
))
3929 function
= objc_rewrite_function_call (function
, (**params
)[0]);
3931 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3932 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3933 if (TREE_CODE (function
) == NOP_EXPR
3934 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
3935 function
= TREE_OPERAND (function
, 0);
3937 if (TREE_CODE (function
) == FUNCTION_DECL
)
3939 if (!mark_used (function
, complain
))
3940 return error_mark_node
;
3943 /* Convert anything with function type to a pointer-to-function. */
3944 if (DECL_MAIN_P (function
))
3946 if (complain
& tf_error
)
3947 pedwarn (input_location
, OPT_Wpedantic
,
3948 "ISO C++ forbids calling %<::main%> from within program");
3950 return error_mark_node
;
3952 function
= build_addr_func (function
, complain
);
3958 function
= build_addr_func (function
, complain
);
3961 if (function
== error_mark_node
)
3962 return error_mark_node
;
3964 fntype
= TREE_TYPE (function
);
3966 if (TYPE_PTRMEMFUNC_P (fntype
))
3968 if (complain
& tf_error
)
3969 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3970 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3971 original
, original
);
3972 return error_mark_node
;
3975 is_method
= (TYPE_PTR_P (fntype
)
3976 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
3978 if (!(TYPE_PTRFN_P (fntype
)
3980 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
3982 if (complain
& tf_error
)
3984 if (!flag_diagnostics_show_caret
)
3985 error_at (input_location
,
3986 "%qE cannot be used as a function", original
);
3987 else if (DECL_P (original
))
3988 error_at (input_location
,
3989 "%qD cannot be used as a function", original
);
3991 error_at (input_location
,
3992 "expression cannot be used as a function");
3995 return error_mark_node
;
3998 /* fntype now gets the type of function pointed to. */
3999 fntype
= TREE_TYPE (fntype
);
4000 parm_types
= TYPE_ARG_TYPES (fntype
);
4004 allocated
= make_tree_vector ();
4005 params
= &allocated
;
4008 nargs
= convert_arguments (parm_types
, params
, fndecl
, LOOKUP_NORMAL
,
4011 return error_mark_node
;
4013 argarray
= (*params
)->address ();
4015 /* Check for errors in format strings and inappropriately
4017 bool warned_p
= check_function_arguments (input_location
, fndecl
, fntype
,
4018 nargs
, argarray
, NULL
);
4020 ret
= build_cxx_call (function
, nargs
, argarray
, complain
, orig_fndecl
);
4024 tree c
= extract_call_expr (ret
);
4025 if (TREE_CODE (c
) == CALL_EXPR
)
4026 TREE_NO_WARNING (c
) = 1;
4029 if (allocated
!= NULL
)
4030 release_tree_vector (allocated
);
4035 /* Subroutine of convert_arguments.
4036 Print an error message about a wrong number of arguments. */
4039 error_args_num (location_t loc
, tree fndecl
, bool too_many_p
)
4043 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
4045 if (DECL_NAME (fndecl
) == NULL_TREE
4046 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
4049 ? G_("too many arguments to constructor %q#D")
4050 : G_("too few arguments to constructor %q#D"),
4055 ? G_("too many arguments to member function %q#D")
4056 : G_("too few arguments to member function %q#D"),
4062 ? G_("too many arguments to function %q#D")
4063 : G_("too few arguments to function %q#D"),
4065 if (!DECL_IS_BUILTIN (fndecl
))
4066 inform (DECL_SOURCE_LOCATION (fndecl
), "declared here");
4070 if (c_dialect_objc () && objc_message_selector ())
4073 ? G_("too many arguments to method %q#D")
4074 : G_("too few arguments to method %q#D"),
4075 objc_message_selector ());
4077 error_at (loc
, too_many_p
? G_("too many arguments to function")
4078 : G_("too few arguments to function"));
4082 /* Convert the actual parameter expressions in the list VALUES to the
4083 types in the list TYPELIST. The converted expressions are stored
4084 back in the VALUES vector.
4085 If parmdecls is exhausted, or when an element has NULL as its type,
4086 perform the default conversions.
4088 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4090 This is also where warnings about wrong number of args are generated.
4092 Returns the actual number of arguments processed (which might be less
4093 than the length of the vector), or -1 on error.
4095 In C++, unspecified trailing parameters can be filled in with their
4096 default arguments, if such were specified. Do so here. */
4099 convert_arguments (tree typelist
, vec
<tree
, va_gc
> **values
, tree fndecl
,
4100 int flags
, tsubst_flags_t complain
)
4105 /* Argument passing is always copy-initialization. */
4106 flags
|= LOOKUP_ONLYCONVERTING
;
4108 for (i
= 0, typetail
= typelist
;
4109 i
< vec_safe_length (*values
);
4112 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
4113 tree val
= (**values
)[i
];
4115 if (val
== error_mark_node
|| type
== error_mark_node
)
4118 if (type
== void_type_node
)
4120 if (complain
& tf_error
)
4122 error_args_num (input_location
, fndecl
, /*too_many_p=*/true);
4129 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4130 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4131 if (TREE_CODE (val
) == NOP_EXPR
4132 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
4133 && (type
== 0 || !TYPE_REF_P (type
)))
4134 val
= TREE_OPERAND (val
, 0);
4136 if (type
== 0 || !TYPE_REF_P (type
))
4138 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
4139 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val
)))
4140 val
= decay_conversion (val
, complain
);
4143 if (val
== error_mark_node
)
4148 /* Formal parm type is specified by a function prototype. */
4151 if (!COMPLETE_TYPE_P (complete_type (type
)))
4153 if (complain
& tf_error
)
4155 location_t loc
= EXPR_LOC_OR_LOC (val
, input_location
);
4158 auto_diagnostic_group d
;
4160 "parameter %P of %qD has incomplete type %qT",
4162 inform (get_fndecl_argument_location (fndecl
, i
),
4166 error_at (loc
, "parameter %P has incomplete type %qT", i
,
4169 parmval
= error_mark_node
;
4173 parmval
= convert_for_initialization
4174 (NULL_TREE
, type
, val
, flags
,
4175 ICR_ARGPASS
, fndecl
, i
, complain
);
4176 parmval
= convert_for_arg_passing (type
, parmval
, complain
);
4179 if (parmval
== error_mark_node
)
4182 (**values
)[i
] = parmval
;
4186 if (fndecl
&& magic_varargs_p (fndecl
))
4187 /* Don't do ellipsis conversion for __built_in_constant_p
4188 as this will result in spurious errors for non-trivial
4190 val
= require_complete_type_sfinae (val
, complain
);
4192 val
= convert_arg_to_ellipsis (val
, complain
);
4194 (**values
)[i
] = val
;
4198 typetail
= TREE_CHAIN (typetail
);
4201 if (typetail
!= 0 && typetail
!= void_list_node
)
4203 /* See if there are default arguments that can be used. Because
4204 we hold default arguments in the FUNCTION_TYPE (which is so
4205 wrong), we can see default parameters here from deduced
4206 contexts (and via typeof) for indirect function calls.
4207 Fortunately we know whether we have a function decl to
4208 provide default arguments in a language conformant
4210 if (fndecl
&& TREE_PURPOSE (typetail
)
4211 && TREE_CODE (TREE_PURPOSE (typetail
)) != DEFERRED_PARSE
)
4213 for (; typetail
!= void_list_node
; ++i
)
4215 /* After DR777, with explicit template args we can end up with a
4216 default argument followed by no default argument. */
4217 if (!TREE_PURPOSE (typetail
))
4220 = convert_default_arg (TREE_VALUE (typetail
),
4221 TREE_PURPOSE (typetail
),
4222 fndecl
, i
, complain
);
4224 if (parmval
== error_mark_node
)
4227 vec_safe_push (*values
, parmval
);
4228 typetail
= TREE_CHAIN (typetail
);
4229 /* ends with `...'. */
4230 if (typetail
== NULL_TREE
)
4235 if (typetail
&& typetail
!= void_list_node
)
4237 if (complain
& tf_error
)
4238 error_args_num (input_location
, fndecl
, /*too_many_p=*/false);
4246 /* Build a binary-operation expression, after performing default
4247 conversions on the operands. CODE is the kind of expression to
4248 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4249 are the tree codes which correspond to ARG1 and ARG2 when issuing
4250 warnings about possibly misplaced parentheses. They may differ
4251 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4252 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4253 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4254 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4255 ARG2_CODE as ERROR_MARK. */
4258 build_x_binary_op (const op_location_t
&loc
, enum tree_code code
, tree arg1
,
4259 enum tree_code arg1_code
, tree arg2
,
4260 enum tree_code arg2_code
, tree
*overload_p
,
4261 tsubst_flags_t complain
)
4266 tree overload
= NULL_TREE
;
4271 if (processing_template_decl
)
4273 if (type_dependent_expression_p (arg1
)
4274 || type_dependent_expression_p (arg2
))
4276 expr
= build_min_nt_loc (loc
, code
, arg1
, arg2
);
4277 maybe_save_operator_binding (expr
);
4280 arg1
= build_non_dependent_expr (arg1
);
4281 arg2
= build_non_dependent_expr (arg2
);
4284 if (code
== DOTSTAR_EXPR
)
4285 expr
= build_m_component_ref (arg1
, arg2
, complain
);
4287 expr
= build_new_op (loc
, code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
,
4288 &overload
, complain
);
4290 if (overload_p
!= NULL
)
4291 *overload_p
= overload
;
4293 /* Check for cases such as x+y<<z which users are likely to
4294 misinterpret. But don't warn about obj << x + y, since that is a
4295 common idiom for I/O. */
4296 if (warn_parentheses
4297 && (complain
& tf_warning
)
4298 && !processing_template_decl
4299 && !error_operand_p (arg1
)
4300 && !error_operand_p (arg2
)
4301 && (code
!= LSHIFT_EXPR
4302 || !CLASS_TYPE_P (TREE_TYPE (arg1
))))
4303 warn_about_parentheses (loc
, code
, arg1_code
, orig_arg1
,
4304 arg2_code
, orig_arg2
);
4306 if (processing_template_decl
&& expr
!= error_mark_node
)
4308 if (overload
!= NULL_TREE
)
4309 return (build_min_non_dep_op_overload
4310 (code
, expr
, overload
, orig_arg1
, orig_arg2
));
4312 return build_min_non_dep (code
, expr
, orig_arg1
, orig_arg2
);
4318 /* Build and return an ARRAY_REF expression. */
4321 build_x_array_ref (location_t loc
, tree arg1
, tree arg2
,
4322 tsubst_flags_t complain
)
4324 tree orig_arg1
= arg1
;
4325 tree orig_arg2
= arg2
;
4327 tree overload
= NULL_TREE
;
4329 if (processing_template_decl
)
4331 if (type_dependent_expression_p (arg1
)
4332 || type_dependent_expression_p (arg2
))
4333 return build_min_nt_loc (loc
, ARRAY_REF
, arg1
, arg2
,
4334 NULL_TREE
, NULL_TREE
);
4335 arg1
= build_non_dependent_expr (arg1
);
4336 arg2
= build_non_dependent_expr (arg2
);
4339 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, arg1
, arg2
,
4340 NULL_TREE
, &overload
, complain
);
4342 if (processing_template_decl
&& expr
!= error_mark_node
)
4344 if (overload
!= NULL_TREE
)
4345 return (build_min_non_dep_op_overload
4346 (ARRAY_REF
, expr
, overload
, orig_arg1
, orig_arg2
));
4348 return build_min_non_dep (ARRAY_REF
, expr
, orig_arg1
, orig_arg2
,
4349 NULL_TREE
, NULL_TREE
);
4354 /* Return whether OP is an expression of enum type cast to integer
4355 type. In C++ even unsigned enum types are cast to signed integer
4356 types. We do not want to issue warnings about comparisons between
4357 signed and unsigned types when one of the types is an enum type.
4358 Those warnings are always false positives in practice. */
4361 enum_cast_to_int (tree op
)
4363 if (CONVERT_EXPR_P (op
)
4364 && TREE_TYPE (op
) == integer_type_node
4365 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == ENUMERAL_TYPE
4366 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op
, 0))))
4369 /* The cast may have been pushed into a COND_EXPR. */
4370 if (TREE_CODE (op
) == COND_EXPR
)
4371 return (enum_cast_to_int (TREE_OPERAND (op
, 1))
4372 || enum_cast_to_int (TREE_OPERAND (op
, 2)));
4377 /* For the c-common bits. */
4379 build_binary_op (location_t location
, enum tree_code code
, tree op0
, tree op1
,
4382 return cp_build_binary_op (location
, code
, op0
, op1
, tf_warning_or_error
);
4385 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4386 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4389 build_vec_cmp (tree_code code
, tree type
,
4390 tree arg0
, tree arg1
)
4392 tree zero_vec
= build_zero_cst (type
);
4393 tree minus_one_vec
= build_minus_one_cst (type
);
4394 tree cmp_type
= truth_type_for (type
);
4395 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
4396 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
4399 /* Possibly warn about an address never being NULL. */
4402 warn_for_null_address (location_t location
, tree op
, tsubst_flags_t complain
)
4405 || (complain
& tf_warning
) == 0
4406 || c_inhibit_evaluation_warnings
!= 0
4407 || TREE_NO_WARNING (op
))
4410 tree cop
= fold_for_warn (op
);
4412 if (TREE_CODE (cop
) == ADDR_EXPR
4413 && decl_with_nonnull_addr_p (TREE_OPERAND (cop
, 0))
4414 && !TREE_NO_WARNING (cop
))
4415 warning_at (location
, OPT_Waddress
, "the address of %qD will never "
4416 "be NULL", TREE_OPERAND (cop
, 0));
4418 if (CONVERT_EXPR_P (op
)
4419 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op
, 0))))
4422 STRIP_NOPS (inner_op
);
4424 if (DECL_P (inner_op
))
4425 warning_at (location
, OPT_Waddress
,
4426 "the compiler can assume that the address of "
4427 "%qD will never be NULL", inner_op
);
4431 /* Build a binary-operation expression without default conversions.
4432 CODE is the kind of expression to build.
4433 LOCATION is the location_t of the operator in the source code.
4434 This function differs from `build' in several ways:
4435 the data type of the result is computed and recorded in it,
4436 warnings are generated if arg data types are invalid,
4437 special handling for addition and subtraction of pointers is known,
4438 and some optimization is done (operations on narrow ints
4439 are done in the narrower type when that gives the same result).
4440 Constant folding is also done before the result is returned.
4442 Note that the operands will never have enumeral types
4443 because either they have just had the default conversions performed
4444 or they have both just been converted to some other type in which
4445 the arithmetic is to be done.
4447 C++: must do special pointer arithmetic when implementing
4448 multiple inheritance, and deal with pointer to member functions. */
4451 cp_build_binary_op (const op_location_t
&location
,
4452 enum tree_code code
, tree orig_op0
, tree orig_op1
,
4453 tsubst_flags_t complain
)
4456 enum tree_code code0
, code1
;
4458 const char *invalid_op_diag
;
4460 /* Expression code to give to the expression when it is built.
4461 Normally this is CODE, which is what the caller asked for,
4462 but in some special cases we change it. */
4463 enum tree_code resultcode
= code
;
4465 /* Data type in which the computation is to be performed.
4466 In the simplest cases this is the common type of the arguments. */
4467 tree result_type
= NULL_TREE
;
4469 /* Nonzero means operands have already been type-converted
4470 in whatever way is necessary.
4471 Zero means they need to be converted to RESULT_TYPE. */
4474 /* Nonzero means create the expression with this type, rather than
4476 tree build_type
= 0;
4478 /* Nonzero means after finally constructing the expression
4479 convert it to this type. */
4480 tree final_type
= 0;
4482 tree result
, result_ovl
;
4484 /* Nonzero if this is an operation like MIN or MAX which can
4485 safely be computed in short if both args are promoted shorts.
4486 Also implies COMMON.
4487 -1 indicates a bitwise operation; this makes a difference
4488 in the exact conditions for when it is safe to do the operation
4489 in a narrower mode. */
4492 /* Nonzero if this is a comparison operation;
4493 if both args are promoted shorts, compare the original shorts.
4494 Also implies COMMON. */
4495 int short_compare
= 0;
4497 /* Nonzero if this is a right-shift operation, which can be computed on the
4498 original short and then promoted if the operand is a promoted short. */
4499 int short_shift
= 0;
4501 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4504 /* True if both operands have arithmetic type. */
4505 bool arithmetic_types_p
;
4507 /* Remember whether we're doing / or %. */
4508 bool doing_div_or_mod
= false;
4510 /* Remember whether we're doing << or >>. */
4511 bool doing_shift
= false;
4513 /* Tree holding instrumentation expression. */
4514 tree instrument_expr
= NULL_TREE
;
4516 /* Apply default conversions. */
4517 op0
= resolve_nondeduced_context (orig_op0
, complain
);
4518 op1
= resolve_nondeduced_context (orig_op1
, complain
);
4520 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
4521 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
4522 || code
== TRUTH_XOR_EXPR
)
4524 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
4525 op0
= decay_conversion (op0
, complain
);
4526 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
4527 op1
= decay_conversion (op1
, complain
);
4531 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
4532 op0
= cp_default_conversion (op0
, complain
);
4533 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
4534 op1
= cp_default_conversion (op1
, complain
);
4537 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4538 STRIP_TYPE_NOPS (op0
);
4539 STRIP_TYPE_NOPS (op1
);
4541 /* DTRT if one side is an overloaded function, but complain about it. */
4542 if (type_unknown_p (op0
))
4544 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
4545 if (t
!= error_mark_node
)
4547 if (complain
& tf_error
)
4548 permerror (location
,
4549 "assuming cast to type %qT from overloaded function",
4554 if (type_unknown_p (op1
))
4556 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
4557 if (t
!= error_mark_node
)
4559 if (complain
& tf_error
)
4560 permerror (location
,
4561 "assuming cast to type %qT from overloaded function",
4567 type0
= TREE_TYPE (op0
);
4568 type1
= TREE_TYPE (op1
);
4570 /* The expression codes of the data types of the arguments tell us
4571 whether the arguments are integers, floating, pointers, etc. */
4572 code0
= TREE_CODE (type0
);
4573 code1
= TREE_CODE (type1
);
4575 /* If an error was already reported for one of the arguments,
4576 avoid reporting another error. */
4577 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
4578 return error_mark_node
;
4580 if ((invalid_op_diag
4581 = targetm
.invalid_binary_op (code
, type0
, type1
)))
4583 if (complain
& tf_error
)
4584 error (invalid_op_diag
);
4585 return error_mark_node
;
4588 /* Issue warnings about peculiar, but valid, uses of NULL. */
4589 if ((null_node_p (orig_op0
) || null_node_p (orig_op1
))
4590 /* It's reasonable to use pointer values as operands of &&
4591 and ||, so NULL is no exception. */
4592 && code
!= TRUTH_ANDIF_EXPR
&& code
!= TRUTH_ORIF_EXPR
4593 && ( /* Both are NULL (or 0) and the operation was not a
4594 comparison or a pointer subtraction. */
4595 (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)
4596 && code
!= EQ_EXPR
&& code
!= NE_EXPR
&& code
!= MINUS_EXPR
)
4597 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4598 || (!null_ptr_cst_p (orig_op0
)
4599 && !TYPE_PTR_OR_PTRMEM_P (type0
))
4600 || (!null_ptr_cst_p (orig_op1
)
4601 && !TYPE_PTR_OR_PTRMEM_P (type1
)))
4602 && (complain
& tf_warning
))
4605 expansion_point_location_if_in_system_header (input_location
);
4607 warning_at (loc
, OPT_Wpointer_arith
, "NULL used in arithmetic");
4610 /* In case when one of the operands of the binary operation is
4611 a vector and another is a scalar -- convert scalar to vector. */
4612 if ((gnu_vector_type_p (type0
) && code1
!= VECTOR_TYPE
)
4613 || (gnu_vector_type_p (type1
) && code0
!= VECTOR_TYPE
))
4615 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
4616 complain
& tf_error
);
4618 switch (convert_flag
)
4621 return error_mark_node
;
4624 op0
= convert (TREE_TYPE (type1
), op0
);
4625 op0
= save_expr (op0
);
4626 op0
= build_vector_from_val (type1
, op0
);
4627 type0
= TREE_TYPE (op0
);
4628 code0
= TREE_CODE (type0
);
4634 op1
= convert (TREE_TYPE (type0
), op1
);
4635 op1
= save_expr (op1
);
4636 op1
= build_vector_from_val (type0
, op1
);
4637 type1
= TREE_TYPE (op1
);
4638 code1
= TREE_CODE (type1
);
4650 /* Subtraction of two similar pointers.
4651 We must subtract them as integers, then divide by object size. */
4652 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
4653 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4656 result
= pointer_diff (location
, op0
, op1
,
4657 common_pointer_type (type0
, type1
), complain
,
4659 if (instrument_expr
!= NULL
)
4660 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
4661 instrument_expr
, result
);
4665 /* In all other cases except pointer - int, the usual arithmetic
4667 else if (!(code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
))
4672 /* The pointer - int case is just like pointer + int; fall
4676 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
4677 && (code0
== INTEGER_TYPE
|| code1
== INTEGER_TYPE
))
4681 ptr_operand
= ((code0
== POINTER_TYPE
) ? op0
: op1
);
4682 int_operand
= ((code0
== INTEGER_TYPE
) ? op0
: op1
);
4683 if (processing_template_decl
)
4685 result_type
= TREE_TYPE (ptr_operand
);
4688 return cp_pointer_int_sum (location
, code
,
4700 case TRUNC_DIV_EXPR
:
4702 case FLOOR_DIV_EXPR
:
4703 case ROUND_DIV_EXPR
:
4704 case EXACT_DIV_EXPR
:
4705 if (TREE_CODE (op0
) == SIZEOF_EXPR
&& TREE_CODE (op1
) == SIZEOF_EXPR
)
4707 tree type0
= TREE_OPERAND (op0
, 0);
4708 tree type1
= TREE_OPERAND (op1
, 0);
4709 tree first_arg
= type0
;
4710 if (!TYPE_P (type0
))
4711 type0
= TREE_TYPE (type0
);
4712 if (!TYPE_P (type1
))
4713 type1
= TREE_TYPE (type1
);
4714 if (INDIRECT_TYPE_P (type0
) && same_type_p (TREE_TYPE (type0
), type1
))
4716 STRIP_ANY_LOCATION_WRAPPER (first_arg
);
4717 if (!(TREE_CODE (first_arg
) == PARM_DECL
4718 && DECL_ARRAY_PARAMETER_P (first_arg
)
4719 && warn_sizeof_array_argument
)
4720 && (complain
& tf_warning
))
4722 auto_diagnostic_group d
;
4723 if (warning_at (location
, OPT_Wsizeof_pointer_div
,
4724 "division %<sizeof (%T) / sizeof (%T)%> does "
4725 "not compute the number of array elements",
4727 if (DECL_P (first_arg
))
4728 inform (DECL_SOURCE_LOCATION (first_arg
),
4729 "first %<sizeof%> operand was declared here");
4734 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4735 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
4736 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4737 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
4739 enum tree_code tcode0
= code0
, tcode1
= code1
;
4740 doing_div_or_mod
= true;
4741 warn_for_div_by_zero (location
, fold_for_warn (op1
));
4743 if (tcode0
== COMPLEX_TYPE
|| tcode0
== VECTOR_TYPE
)
4744 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
4745 if (tcode1
== COMPLEX_TYPE
|| tcode1
== VECTOR_TYPE
)
4746 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
4748 if (!(tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
))
4749 resultcode
= RDIV_EXPR
;
4752 /* When dividing two signed integers, we have to promote to int.
4753 unless we divide by a constant != -1. Note that default
4754 conversion will have been performed on the operands at this
4755 point, so we have to dig out the original type to find out if
4757 tree stripped_op1
= tree_strip_any_location_wrapper (op1
);
4758 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4759 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4760 || (TREE_CODE (stripped_op1
) == INTEGER_CST
4761 && ! integer_all_onesp (stripped_op1
)));
4771 if ((code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4772 || (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4773 && !VECTOR_FLOAT_TYPE_P (type0
)
4774 && !VECTOR_FLOAT_TYPE_P (type1
)))
4778 case TRUNC_MOD_EXPR
:
4779 case FLOOR_MOD_EXPR
:
4780 doing_div_or_mod
= true;
4781 warn_for_div_by_zero (location
, fold_for_warn (op1
));
4783 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4784 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4785 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
4787 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4789 /* Although it would be tempting to shorten always here, that loses
4790 on some targets, since the modulo instruction is undefined if the
4791 quotient can't be represented in the computation mode. We shorten
4792 only if unsigned or if dividing by something we know != -1. */
4793 tree stripped_op1
= tree_strip_any_location_wrapper (op1
);
4794 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4795 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4796 || (TREE_CODE (stripped_op1
) == INTEGER_CST
4797 && ! integer_all_onesp (stripped_op1
)));
4802 case TRUTH_ANDIF_EXPR
:
4803 case TRUTH_ORIF_EXPR
:
4804 case TRUTH_AND_EXPR
:
4806 if (!VECTOR_TYPE_P (type0
) && gnu_vector_type_p (type1
))
4808 if (!COMPARISON_CLASS_P (op1
))
4809 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4810 build_zero_cst (type1
), complain
);
4811 if (code
== TRUTH_ANDIF_EXPR
)
4813 tree z
= build_zero_cst (TREE_TYPE (op1
));
4814 return build_conditional_expr (location
, op0
, op1
, z
, complain
);
4816 else if (code
== TRUTH_ORIF_EXPR
)
4818 tree m1
= build_all_ones_cst (TREE_TYPE (op1
));
4819 return build_conditional_expr (location
, op0
, m1
, op1
, complain
);
4824 if (gnu_vector_type_p (type0
)
4825 && (!VECTOR_TYPE_P (type1
) || gnu_vector_type_p (type1
)))
4827 if (!COMPARISON_CLASS_P (op0
))
4828 op0
= cp_build_binary_op (EXPR_LOCATION (op0
), NE_EXPR
, op0
,
4829 build_zero_cst (type0
), complain
);
4830 if (!VECTOR_TYPE_P (type1
))
4832 tree m1
= build_all_ones_cst (TREE_TYPE (op0
));
4833 tree z
= build_zero_cst (TREE_TYPE (op0
));
4834 op1
= build_conditional_expr (location
, op1
, m1
, z
, complain
);
4836 else if (!COMPARISON_CLASS_P (op1
))
4837 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4838 build_zero_cst (type1
), complain
);
4840 if (code
== TRUTH_ANDIF_EXPR
)
4841 code
= BIT_AND_EXPR
;
4842 else if (code
== TRUTH_ORIF_EXPR
)
4843 code
= BIT_IOR_EXPR
;
4847 return cp_build_binary_op (location
, code
, op0
, op1
, complain
);
4850 result_type
= boolean_type_node
;
4853 /* Shift operations: result has same type as first operand;
4854 always convert second operand to int.
4855 Also set SHORT_SHIFT if shifting rightward. */
4858 if (gnu_vector_type_p (type0
)
4859 && code1
== INTEGER_TYPE
4860 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4862 result_type
= type0
;
4865 else if (gnu_vector_type_p (type0
)
4866 && gnu_vector_type_p (type1
)
4867 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4868 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4869 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
4870 TYPE_VECTOR_SUBPARTS (type1
)))
4872 result_type
= type0
;
4875 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4877 tree const_op1
= fold_for_warn (op1
);
4878 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4880 result_type
= type0
;
4882 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4884 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4886 if ((complain
& tf_warning
)
4887 && c_inhibit_evaluation_warnings
== 0)
4888 warning_at (location
, OPT_Wshift_count_negative
,
4889 "right shift count is negative");
4893 if (!integer_zerop (const_op1
))
4896 if (compare_tree_int (const_op1
, TYPE_PRECISION (type0
)) >= 0
4897 && (complain
& tf_warning
)
4898 && c_inhibit_evaluation_warnings
== 0)
4899 warning_at (location
, OPT_Wshift_count_overflow
,
4900 "right shift count >= width of type");
4903 /* Avoid converting op1 to result_type later. */
4909 if (gnu_vector_type_p (type0
)
4910 && code1
== INTEGER_TYPE
4911 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4913 result_type
= type0
;
4916 else if (gnu_vector_type_p (type0
)
4917 && gnu_vector_type_p (type1
)
4918 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4919 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4920 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
4921 TYPE_VECTOR_SUBPARTS (type1
)))
4923 result_type
= type0
;
4926 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4928 tree const_op0
= fold_for_warn (op0
);
4929 if (TREE_CODE (const_op0
) != INTEGER_CST
)
4931 tree const_op1
= fold_for_warn (op1
);
4932 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4934 result_type
= type0
;
4936 if (TREE_CODE (const_op0
) == INTEGER_CST
4937 && tree_int_cst_sgn (const_op0
) < 0
4938 && (complain
& tf_warning
)
4939 && c_inhibit_evaluation_warnings
== 0)
4940 warning_at (location
, OPT_Wshift_negative_value
,
4941 "left shift of negative value");
4942 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4944 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4946 if ((complain
& tf_warning
)
4947 && c_inhibit_evaluation_warnings
== 0)
4948 warning_at (location
, OPT_Wshift_count_negative
,
4949 "left shift count is negative");
4951 else if (compare_tree_int (const_op1
,
4952 TYPE_PRECISION (type0
)) >= 0)
4954 if ((complain
& tf_warning
)
4955 && c_inhibit_evaluation_warnings
== 0)
4956 warning_at (location
, OPT_Wshift_count_overflow
,
4957 "left shift count >= width of type");
4959 else if (TREE_CODE (const_op0
) == INTEGER_CST
4960 && (complain
& tf_warning
))
4961 maybe_warn_shift_overflow (location
, const_op0
, const_op1
);
4963 /* Avoid converting op1 to result_type later. */
4970 if (gnu_vector_type_p (type0
) && gnu_vector_type_p (type1
))
4971 goto vector_compare
;
4972 if ((complain
& tf_warning
)
4973 && c_inhibit_evaluation_warnings
== 0
4974 && (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
)))
4975 warning_at (location
, OPT_Wfloat_equal
,
4976 "comparing floating-point with %<==%> "
4977 "or %<!=%> is unsafe");
4978 if (complain
& tf_warning
)
4980 tree stripped_orig_op0
= tree_strip_any_location_wrapper (orig_op0
);
4981 tree stripped_orig_op1
= tree_strip_any_location_wrapper (orig_op1
);
4982 if ((TREE_CODE (stripped_orig_op0
) == STRING_CST
4983 && !integer_zerop (cp_fully_fold (op1
)))
4984 || (TREE_CODE (stripped_orig_op1
) == STRING_CST
4985 && !integer_zerop (cp_fully_fold (op0
))))
4986 warning_at (location
, OPT_Waddress
,
4987 "comparison with string literal results in "
4988 "unspecified behavior");
4991 build_type
= boolean_type_node
;
4992 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4993 || code0
== COMPLEX_TYPE
|| code0
== ENUMERAL_TYPE
)
4994 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4995 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
))
4997 else if (((code0
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type0
))
4998 && null_ptr_cst_p (orig_op1
))
4999 /* Handle, eg, (void*)0 (c++/43906), and more. */
5000 || (code0
== POINTER_TYPE
5001 && TYPE_PTR_P (type1
) && integer_zerop (op1
)))
5003 if (TYPE_PTR_P (type1
))
5004 result_type
= composite_pointer_type (location
,
5005 type0
, type1
, op0
, op1
,
5006 CPO_COMPARISON
, complain
);
5008 result_type
= type0
;
5010 if (char_type_p (TREE_TYPE (orig_op1
)))
5012 auto_diagnostic_group d
;
5013 if (warning_at (location
, OPT_Wpointer_compare
,
5014 "comparison between pointer and zero character "
5017 "did you mean to dereference the pointer?");
5019 warn_for_null_address (location
, op0
, complain
);
5021 else if (((code1
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type1
))
5022 && null_ptr_cst_p (orig_op0
))
5023 /* Handle, eg, (void*)0 (c++/43906), and more. */
5024 || (code1
== POINTER_TYPE
5025 && TYPE_PTR_P (type0
) && integer_zerop (op0
)))
5027 if (TYPE_PTR_P (type0
))
5028 result_type
= composite_pointer_type (location
,
5029 type0
, type1
, op0
, op1
,
5030 CPO_COMPARISON
, complain
);
5032 result_type
= type1
;
5034 if (char_type_p (TREE_TYPE (orig_op0
)))
5036 auto_diagnostic_group d
;
5037 if (warning_at (location
, OPT_Wpointer_compare
,
5038 "comparison between pointer and zero character "
5041 "did you mean to dereference the pointer?");
5043 warn_for_null_address (location
, op1
, complain
);
5045 else if ((code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5046 || (TYPE_PTRDATAMEM_P (type0
) && TYPE_PTRDATAMEM_P (type1
)))
5047 result_type
= composite_pointer_type (location
,
5048 type0
, type1
, op0
, op1
,
5049 CPO_COMPARISON
, complain
);
5050 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
5051 /* One of the operands must be of nullptr_t type. */
5052 result_type
= TREE_TYPE (nullptr_node
);
5053 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5055 result_type
= type0
;
5056 if (complain
& tf_error
)
5057 permerror (location
, "ISO C++ forbids comparison between "
5058 "pointer and integer");
5060 return error_mark_node
;
5062 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
5064 result_type
= type1
;
5065 if (complain
& tf_error
)
5066 permerror (location
, "ISO C++ forbids comparison between "
5067 "pointer and integer");
5069 return error_mark_node
;
5071 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (orig_op1
))
5073 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5074 == ptrmemfunc_vbit_in_delta
)
5076 tree pfn0
, delta0
, e1
, e2
;
5078 if (TREE_SIDE_EFFECTS (op0
))
5079 op0
= cp_save_expr (op0
);
5081 pfn0
= pfn_from_ptrmemfunc (op0
);
5082 delta0
= delta_from_ptrmemfunc (op0
);
5083 e1
= cp_build_binary_op (location
,
5086 build_zero_cst (TREE_TYPE (pfn0
)),
5088 e2
= cp_build_binary_op (location
,
5094 if (complain
& tf_warning
)
5095 maybe_warn_zero_as_null_pointer_constant (op1
, input_location
);
5097 e2
= cp_build_binary_op (location
,
5098 EQ_EXPR
, e2
, integer_zero_node
,
5100 op0
= cp_build_binary_op (location
,
5101 TRUTH_ANDIF_EXPR
, e1
, e2
,
5103 op1
= cp_convert (TREE_TYPE (op0
), integer_one_node
, complain
);
5107 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
5108 op1
= cp_convert (TREE_TYPE (op0
), op1
, complain
);
5110 result_type
= TREE_TYPE (op0
);
5112 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (orig_op0
))
5113 return cp_build_binary_op (location
, code
, op1
, op0
, complain
);
5114 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
))
5117 /* E will be the final comparison. */
5119 /* E1 and E2 are for scratch. */
5127 type
= composite_pointer_type (location
, type0
, type1
, op0
, op1
,
5128 CPO_COMPARISON
, complain
);
5130 if (!same_type_p (TREE_TYPE (op0
), type
))
5131 op0
= cp_convert_and_check (type
, op0
, complain
);
5132 if (!same_type_p (TREE_TYPE (op1
), type
))
5133 op1
= cp_convert_and_check (type
, op1
, complain
);
5135 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5136 return error_mark_node
;
5138 if (TREE_SIDE_EFFECTS (op0
))
5139 op0
= save_expr (op0
);
5140 if (TREE_SIDE_EFFECTS (op1
))
5141 op1
= save_expr (op1
);
5143 pfn0
= pfn_from_ptrmemfunc (op0
);
5144 pfn0
= cp_fully_fold (pfn0
);
5145 /* Avoid -Waddress warnings (c++/64877). */
5146 if (TREE_CODE (pfn0
) == ADDR_EXPR
)
5147 TREE_NO_WARNING (pfn0
) = 1;
5148 pfn1
= pfn_from_ptrmemfunc (op1
);
5149 pfn1
= cp_fully_fold (pfn1
);
5150 delta0
= delta_from_ptrmemfunc (op0
);
5151 delta1
= delta_from_ptrmemfunc (op1
);
5152 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5153 == ptrmemfunc_vbit_in_delta
)
5158 && ((op0.delta == op1.delta)
5159 || (!op0.pfn && op0.delta & 1 == 0
5160 && op1.delta & 1 == 0))
5162 The reason for the `!op0.pfn' bit is that a NULL
5163 pointer-to-member is any member with a zero PFN and
5164 LSB of the DELTA field is 0. */
5166 e1
= cp_build_binary_op (location
, BIT_AND_EXPR
,
5170 e1
= cp_build_binary_op (location
,
5171 EQ_EXPR
, e1
, integer_zero_node
,
5173 e2
= cp_build_binary_op (location
, BIT_AND_EXPR
,
5177 e2
= cp_build_binary_op (location
,
5178 EQ_EXPR
, e2
, integer_zero_node
,
5180 e1
= cp_build_binary_op (location
,
5181 TRUTH_ANDIF_EXPR
, e2
, e1
,
5183 e2
= cp_build_binary_op (location
, EQ_EXPR
,
5185 build_zero_cst (TREE_TYPE (pfn0
)),
5187 e2
= cp_build_binary_op (location
,
5188 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
5189 e1
= cp_build_binary_op (location
,
5190 EQ_EXPR
, delta0
, delta1
, complain
);
5191 e1
= cp_build_binary_op (location
,
5192 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
5199 && (!op0.pfn || op0.delta == op1.delta))
5201 The reason for the `!op0.pfn' bit is that a NULL
5202 pointer-to-member is any member with a zero PFN; the
5203 DELTA field is unspecified. */
5205 e1
= cp_build_binary_op (location
,
5206 EQ_EXPR
, delta0
, delta1
, complain
);
5207 e2
= cp_build_binary_op (location
,
5210 build_zero_cst (TREE_TYPE (pfn0
)),
5212 e1
= cp_build_binary_op (location
,
5213 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
5215 e2
= build2 (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
5216 e
= cp_build_binary_op (location
,
5217 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
5218 if (code
== EQ_EXPR
)
5220 return cp_build_binary_op (location
,
5221 EQ_EXPR
, e
, integer_zero_node
, complain
);
5225 gcc_assert (!TYPE_PTRMEMFUNC_P (type0
)
5226 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
),
5228 gcc_assert (!TYPE_PTRMEMFUNC_P (type1
)
5229 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
),
5237 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
5238 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
5240 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5241 result_type
= composite_pointer_type (location
,
5242 type0
, type1
, op0
, op1
,
5243 CPO_COMPARISON
, complain
);
5250 case SPACESHIP_EXPR
:
5251 if (TREE_CODE (orig_op0
) == STRING_CST
5252 || TREE_CODE (orig_op1
) == STRING_CST
)
5254 if (complain
& tf_warning
)
5255 warning_at (location
, OPT_Waddress
,
5256 "comparison with string literal results "
5257 "in unspecified behavior");
5260 if (gnu_vector_type_p (type0
) && gnu_vector_type_p (type1
))
5264 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
5266 && !vector_types_compatible_elements_p (type0
, type1
))
5268 if (complain
& tf_error
)
5270 error_at (location
, "comparing vectors with different "
5272 inform (location
, "operand types are %qT and %qT",
5275 return error_mark_node
;
5278 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0
),
5279 TYPE_VECTOR_SUBPARTS (type1
)))
5281 if (complain
& tf_error
)
5283 error_at (location
, "comparing vectors with different "
5284 "number of elements");
5285 inform (location
, "operand types are %qT and %qT",
5288 return error_mark_node
;
5291 /* It's not precisely specified how the usual arithmetic
5292 conversions apply to the vector types. Here, we use
5293 the unsigned type if one of the operands is signed and
5294 the other one is unsigned. */
5295 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
5297 if (!TYPE_UNSIGNED (type0
))
5298 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
5300 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
5301 warning_at (location
, OPT_Wsign_compare
, "comparison between "
5302 "types %qT and %qT", type0
, type1
);
5305 if (resultcode
== SPACESHIP_EXPR
)
5307 if (complain
& tf_error
)
5308 sorry_at (location
, "three-way comparison of vectors");
5309 return error_mark_node
;
5312 /* Always construct signed integer vector type. */
5313 intt
= c_common_type_for_size
5314 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0
))), 0);
5317 if (complain
& tf_error
)
5318 error_at (location
, "could not find an integer type "
5319 "of the same size as %qT", TREE_TYPE (type0
));
5320 return error_mark_node
;
5322 result_type
= build_opaque_vector_type (intt
,
5323 TYPE_VECTOR_SUBPARTS (type0
));
5324 return build_vec_cmp (resultcode
, result_type
, op0
, op1
);
5326 build_type
= boolean_type_node
;
5327 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
5328 || code0
== ENUMERAL_TYPE
)
5329 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5330 || code1
== ENUMERAL_TYPE
))
5332 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5333 result_type
= composite_pointer_type (location
,
5334 type0
, type1
, op0
, op1
,
5335 CPO_COMPARISON
, complain
);
5336 else if (code0
== POINTER_TYPE
&& null_ptr_cst_p (orig_op1
))
5338 /* Core Issue 1512 made this ill-formed. */
5339 if (complain
& tf_error
)
5340 error_at (location
, "ordered comparison of pointer with "
5341 "integer zero (%qT and %qT)", type0
, type1
);
5342 return error_mark_node
;
5344 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (orig_op0
))
5346 /* Core Issue 1512 made this ill-formed. */
5347 if (complain
& tf_error
)
5348 error_at (location
, "ordered comparison of pointer with "
5349 "integer zero (%qT and %qT)", type0
, type1
);
5350 return error_mark_node
;
5352 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
5353 /* One of the operands must be of nullptr_t type. */
5354 result_type
= TREE_TYPE (nullptr_node
);
5355 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5357 result_type
= type0
;
5358 if (complain
& tf_error
)
5359 permerror (location
, "ISO C++ forbids comparison between "
5360 "pointer and integer");
5362 return error_mark_node
;
5364 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
5366 result_type
= type1
;
5367 if (complain
& tf_error
)
5368 permerror (location
, "ISO C++ forbids comparison between "
5369 "pointer and integer");
5371 return error_mark_node
;
5374 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
5375 && !processing_template_decl
5376 && sanitize_flags_p (SANITIZE_POINTER_COMPARE
))
5378 op0
= save_expr (op0
);
5379 op1
= save_expr (op1
);
5381 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE
);
5382 instrument_expr
= build_call_expr_loc (location
, tt
, 2, op0
, op1
);
5387 case UNORDERED_EXPR
:
5394 build_type
= integer_type_node
;
5395 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
5397 if (complain
& tf_error
)
5398 error ("unordered comparison on non-floating-point argument");
5399 return error_mark_node
;
5408 if (((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
5409 || code0
== ENUMERAL_TYPE
)
5410 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5411 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
)))
5412 arithmetic_types_p
= 1;
5415 arithmetic_types_p
= 0;
5416 /* Vector arithmetic is only allowed when both sides are vectors. */
5417 if (gnu_vector_type_p (type0
) && gnu_vector_type_p (type1
))
5419 if (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
5420 || !vector_types_compatible_elements_p (type0
, type1
))
5422 if (complain
& tf_error
)
5424 /* "location" already embeds the locations of the
5425 operands, so we don't need to add them separately
5427 rich_location
richloc (line_table
, location
);
5428 binary_op_error (&richloc
, code
, type0
, type1
);
5430 return error_mark_node
;
5432 arithmetic_types_p
= 1;
5435 /* Determine the RESULT_TYPE, if it is not already known. */
5437 && arithmetic_types_p
5438 && (shorten
|| common
|| short_compare
))
5440 result_type
= cp_common_type (type0
, type1
);
5441 if (complain
& tf_warning
)
5442 do_warn_double_promotion (result_type
, type0
, type1
,
5443 "implicit conversion from %qH to %qI "
5444 "to match other operand of binary "
5449 if (code
== SPACESHIP_EXPR
)
5451 iloc_sentinel
s (location
);
5453 tree orig_type0
= TREE_TYPE (orig_op0
);
5454 tree_code orig_code0
= TREE_CODE (orig_type0
);
5455 tree orig_type1
= TREE_TYPE (orig_op1
);
5456 tree_code orig_code1
= TREE_CODE (orig_type1
);
5459 else if ((orig_code0
== BOOLEAN_TYPE
) != (orig_code1
== BOOLEAN_TYPE
))
5460 /* "If one of the operands is of type bool and the other is not, the
5461 program is ill-formed." */
5462 result_type
= NULL_TREE
;
5463 else if (code0
== POINTER_TYPE
&& orig_code0
!= POINTER_TYPE
5464 && code1
== POINTER_TYPE
&& orig_code1
!= POINTER_TYPE
)
5465 /* We only do array/function-to-pointer conversion if "at least one of
5466 the operands is of pointer type". */
5467 result_type
= NULL_TREE
;
5468 else if (TYPE_PTRFN_P (result_type
) || NULLPTR_TYPE_P (result_type
))
5469 /* <=> no longer supports equality relations. */
5470 result_type
= NULL_TREE
;
5471 else if (orig_code0
== ENUMERAL_TYPE
&& orig_code1
== ENUMERAL_TYPE
5472 && !(same_type_ignoring_top_level_qualifiers_p
5473 (orig_type0
, orig_type1
)))
5474 /* "If both operands have arithmetic types, or one operand has integral
5475 type and the other operand has unscoped enumeration type, the usual
5476 arithmetic conversions are applied to the operands." So we don't do
5477 arithmetic conversions if the operands both have enumeral type. */
5478 result_type
= NULL_TREE
;
5482 build_type
= spaceship_type (result_type
, complain
);
5483 if (build_type
== error_mark_node
)
5484 return error_mark_node
;
5487 if (result_type
&& arithmetic_types_p
)
5489 /* If a narrowing conversion is required, other than from an integral
5490 type to a floating point type, the program is ill-formed. */
5492 if (TREE_CODE (result_type
) == REAL_TYPE
5493 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (orig_op0
)))
5495 else if (!check_narrowing (result_type
, orig_op0
, complain
))
5497 if (TREE_CODE (result_type
) == REAL_TYPE
5498 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (orig_op1
)))
5500 else if (!check_narrowing (result_type
, orig_op1
, complain
))
5502 if (!ok
&& !(complain
& tf_error
))
5503 return error_mark_node
;
5509 if (complain
& tf_error
)
5511 binary_op_rich_location
richloc (location
,
5512 orig_op0
, orig_op1
, true);
5514 "invalid operands of types %qT and %qT to binary %qO",
5515 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
5517 return error_mark_node
;
5520 /* If we're in a template, the only thing we need to know is the
5522 if (processing_template_decl
)
5524 /* Since the middle-end checks the type when doing a build2, we
5525 need to build the tree in pieces. This built tree will never
5526 get out of the front-end as we replace it when instantiating
5528 tree tmp
= build2 (resultcode
,
5529 build_type
? build_type
: result_type
,
5531 TREE_OPERAND (tmp
, 0) = op0
;
5535 /* Remember the original type; RESULT_TYPE might be changed later on
5536 by shorten_binary_op. */
5537 tree orig_type
= result_type
;
5539 if (arithmetic_types_p
)
5541 bool first_complex
= (code0
== COMPLEX_TYPE
);
5542 bool second_complex
= (code1
== COMPLEX_TYPE
);
5543 int none_complex
= (!first_complex
&& !second_complex
);
5545 /* Adapted from patch for c/24581. */
5546 if (first_complex
!= second_complex
5547 && (code
== PLUS_EXPR
5548 || code
== MINUS_EXPR
5549 || code
== MULT_EXPR
5550 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
5551 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
5552 && flag_signed_zeros
)
5554 /* An operation on mixed real/complex operands must be
5555 handled specially, but the language-independent code can
5556 more easily optimize the plain complex arithmetic if
5557 -fno-signed-zeros. */
5558 tree real_type
= TREE_TYPE (result_type
);
5562 if (TREE_TYPE (op0
) != result_type
)
5563 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5564 if (TREE_TYPE (op1
) != real_type
)
5565 op1
= cp_convert_and_check (real_type
, op1
, complain
);
5569 if (TREE_TYPE (op0
) != real_type
)
5570 op0
= cp_convert_and_check (real_type
, op0
, complain
);
5571 if (TREE_TYPE (op1
) != result_type
)
5572 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5574 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
5575 return error_mark_node
;
5578 op0
= save_expr (op0
);
5579 real
= cp_build_unary_op (REALPART_EXPR
, op0
, true, complain
);
5580 imag
= cp_build_unary_op (IMAGPART_EXPR
, op0
, true, complain
);
5584 case TRUNC_DIV_EXPR
:
5585 op1
= save_expr (op1
);
5586 imag
= build2 (resultcode
, real_type
, imag
, op1
);
5590 real
= build2 (resultcode
, real_type
, real
, op1
);
5598 op1
= save_expr (op1
);
5599 real
= cp_build_unary_op (REALPART_EXPR
, op1
, true, complain
);
5600 imag
= cp_build_unary_op (IMAGPART_EXPR
, op1
, true, complain
);
5604 op0
= save_expr (op0
);
5605 imag
= build2 (resultcode
, real_type
, op0
, imag
);
5608 real
= build2 (resultcode
, real_type
, op0
, real
);
5611 real
= build2 (resultcode
, real_type
, op0
, real
);
5612 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
5618 result
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
5622 /* For certain operations (which identify themselves by shorten != 0)
5623 if both args were extended from the same smaller type,
5624 do the arithmetic in that type and then extend.
5626 shorten !=0 and !=1 indicates a bitwise operation.
5627 For them, this optimization is safe only if
5628 both args are zero-extended or both are sign-extended.
5629 Otherwise, we might change the result.
5630 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5631 but calculated in (unsigned short) it would be (unsigned short)-1. */
5633 if (shorten
&& none_complex
)
5635 final_type
= result_type
;
5636 result_type
= shorten_binary_op (result_type
, op0
, op1
,
5640 /* Shifts can be shortened if shifting right. */
5645 tree arg0
= get_narrower (op0
, &unsigned_arg
);
5646 /* We're not really warning here but when we set short_shift we
5647 used fold_for_warn to fold the operand. */
5648 tree const_op1
= fold_for_warn (op1
);
5650 final_type
= result_type
;
5652 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
5653 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
5655 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
5656 && tree_int_cst_sgn (const_op1
) > 0
5657 /* We can shorten only if the shift count is less than the
5658 number of bits in the smaller type size. */
5659 && compare_tree_int (const_op1
,
5660 TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
5661 /* We cannot drop an unsigned shift after sign-extension. */
5662 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
5664 /* Do an unsigned shift if the operand was zero-extended. */
5666 = c_common_signed_or_unsigned_type (unsigned_arg
,
5668 /* Convert value-to-be-shifted to that type. */
5669 if (TREE_TYPE (op0
) != result_type
)
5670 op0
= convert (result_type
, op0
);
5675 /* Comparison operations are shortened too but differently.
5676 They identify themselves by setting short_compare = 1. */
5680 /* We call shorten_compare only for diagnostics. */
5681 tree xop0
= fold_simple (op0
);
5682 tree xop1
= fold_simple (op1
);
5683 tree xresult_type
= result_type
;
5684 enum tree_code xresultcode
= resultcode
;
5685 shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
5689 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
5690 && warn_sign_compare
5691 /* Do not warn until the template is instantiated; we cannot
5692 bound the ranges of the arguments until that point. */
5693 && !processing_template_decl
5694 && (complain
& tf_warning
)
5695 && c_inhibit_evaluation_warnings
== 0
5696 /* Even unsigned enum types promote to signed int. We don't
5697 want to issue -Wsign-compare warnings for this case. */
5698 && !enum_cast_to_int (orig_op0
)
5699 && !enum_cast_to_int (orig_op1
))
5701 warn_for_sign_compare (location
, orig_op0
, orig_op1
, op0
, op1
,
5702 result_type
, resultcode
);
5706 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5707 Then the expression will be built.
5708 It will be given type FINAL_TYPE if that is nonzero;
5709 otherwise, it will be given type RESULT_TYPE. */
5712 warning_sentinel
w (warn_sign_conversion
, short_compare
);
5713 if (!same_type_p (TREE_TYPE (op0
), result_type
))
5714 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5715 if (!same_type_p (TREE_TYPE (op1
), result_type
))
5716 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5718 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5719 return error_mark_node
;
5722 if (build_type
== NULL_TREE
)
5723 build_type
= result_type
;
5726 && flag_strong_eval_order
== 2
5727 && TREE_SIDE_EFFECTS (op1
)
5728 && !processing_template_decl
)
5730 /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
5731 op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
5732 op0
= cp_save_expr (op0
);
5733 instrument_expr
= op0
;
5736 if (sanitize_flags_p ((SANITIZE_SHIFT
5737 | SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
))
5738 && current_function_decl
!= NULL_TREE
5739 && !processing_template_decl
5740 && (doing_div_or_mod
|| doing_shift
))
5742 /* OP0 and/or OP1 might have side-effects. */
5743 op0
= cp_save_expr (op0
);
5744 op1
= cp_save_expr (op1
);
5745 op0
= fold_non_dependent_expr (op0
, complain
);
5746 op1
= fold_non_dependent_expr (op1
, complain
);
5747 tree instrument_expr1
= NULL_TREE
;
5748 if (doing_div_or_mod
5749 && sanitize_flags_p (SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
))
5751 /* For diagnostics we want to use the promoted types without
5752 shorten_binary_op. So convert the arguments to the
5753 original result_type. */
5756 if (TREE_TYPE (cop0
) != orig_type
)
5757 cop0
= cp_convert (orig_type
, op0
, complain
);
5758 if (TREE_TYPE (cop1
) != orig_type
)
5759 cop1
= cp_convert (orig_type
, op1
, complain
);
5760 instrument_expr1
= ubsan_instrument_division (location
, cop0
, cop1
);
5762 else if (doing_shift
&& sanitize_flags_p (SANITIZE_SHIFT
))
5763 instrument_expr1
= ubsan_instrument_shift (location
, code
, op0
, op1
);
5764 if (instrument_expr
!= NULL
)
5765 instrument_expr
= add_stmt_to_compound (instrument_expr
,
5768 instrument_expr
= instrument_expr1
;
5771 result
= build2_loc (location
, resultcode
, build_type
, op0
, op1
);
5772 if (final_type
!= 0)
5773 result
= cp_convert (final_type
, result
, complain
);
5775 if (instrument_expr
!= NULL
)
5776 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
5777 instrument_expr
, result
);
5779 if (!processing_template_decl
)
5781 op0
= cp_fully_fold (op0
);
5782 /* Only consider the second argument if the first isn't overflowed. */
5783 if (!CONSTANT_CLASS_P (op0
) || TREE_OVERFLOW_P (op0
))
5785 op1
= cp_fully_fold (op1
);
5786 if (!CONSTANT_CLASS_P (op1
) || TREE_OVERFLOW_P (op1
))
5789 else if (!CONSTANT_CLASS_P (op0
) || !CONSTANT_CLASS_P (op1
)
5790 || TREE_OVERFLOW_P (op0
) || TREE_OVERFLOW_P (op1
))
5793 result_ovl
= fold_build2 (resultcode
, build_type
, op0
, op1
);
5794 if (TREE_OVERFLOW_P (result_ovl
))
5795 overflow_warning (location
, result_ovl
);
5800 /* Build a VEC_PERM_EXPR.
5801 This is a simple wrapper for c_build_vec_perm_expr. */
5803 build_x_vec_perm_expr (location_t loc
,
5804 tree arg0
, tree arg1
, tree arg2
,
5805 tsubst_flags_t complain
)
5807 tree orig_arg0
= arg0
;
5808 tree orig_arg1
= arg1
;
5809 tree orig_arg2
= arg2
;
5810 if (processing_template_decl
)
5812 if (type_dependent_expression_p (arg0
)
5813 || type_dependent_expression_p (arg1
)
5814 || type_dependent_expression_p (arg2
))
5815 return build_min_nt_loc (loc
, VEC_PERM_EXPR
, arg0
, arg1
, arg2
);
5816 arg0
= build_non_dependent_expr (arg0
);
5818 arg1
= build_non_dependent_expr (arg1
);
5819 arg2
= build_non_dependent_expr (arg2
);
5821 tree exp
= c_build_vec_perm_expr (loc
, arg0
, arg1
, arg2
, complain
& tf_error
);
5822 if (processing_template_decl
&& exp
!= error_mark_node
)
5823 return build_min_non_dep (VEC_PERM_EXPR
, exp
, orig_arg0
,
5824 orig_arg1
, orig_arg2
);
5828 /* Return a tree for the sum or difference (RESULTCODE says which)
5829 of pointer PTROP and integer INTOP. */
5832 cp_pointer_int_sum (location_t loc
, enum tree_code resultcode
, tree ptrop
,
5833 tree intop
, tsubst_flags_t complain
)
5835 tree res_type
= TREE_TYPE (ptrop
);
5837 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5838 in certain circumstance (when it's valid to do so). So we need
5839 to make sure it's complete. We don't need to check here, if we
5840 can actually complete it at all, as those checks will be done in
5841 pointer_int_sum() anyway. */
5842 complete_type (TREE_TYPE (res_type
));
5844 return pointer_int_sum (loc
, resultcode
, ptrop
,
5845 intop
, complain
& tf_warning_or_error
);
5848 /* Return a tree for the difference of pointers OP0 and OP1.
5849 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
5850 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
5853 pointer_diff (location_t loc
, tree op0
, tree op1
, tree ptrtype
,
5854 tsubst_flags_t complain
, tree
*instrument_expr
)
5856 tree result
, inttype
;
5857 tree restype
= ptrdiff_type_node
;
5858 tree target_type
= TREE_TYPE (ptrtype
);
5860 if (!complete_type_or_else (target_type
, NULL_TREE
))
5861 return error_mark_node
;
5863 if (VOID_TYPE_P (target_type
))
5865 if (complain
& tf_error
)
5866 permerror (loc
, "ISO C++ forbids using pointer of "
5867 "type %<void *%> in subtraction");
5869 return error_mark_node
;
5871 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
5873 if (complain
& tf_error
)
5874 permerror (loc
, "ISO C++ forbids using pointer to "
5875 "a function in subtraction");
5877 return error_mark_node
;
5879 if (TREE_CODE (target_type
) == METHOD_TYPE
)
5881 if (complain
& tf_error
)
5882 permerror (loc
, "ISO C++ forbids using pointer to "
5883 "a method in subtraction");
5885 return error_mark_node
;
5887 else if (!verify_type_context (loc
, TCTX_POINTER_ARITH
,
5888 TREE_TYPE (TREE_TYPE (op0
)),
5889 !(complain
& tf_error
))
5890 || !verify_type_context (loc
, TCTX_POINTER_ARITH
,
5891 TREE_TYPE (TREE_TYPE (op1
)),
5892 !(complain
& tf_error
)))
5893 return error_mark_node
;
5895 /* Determine integer type result of the subtraction. This will usually
5896 be the same as the result type (ptrdiff_t), but may need to be a wider
5897 type if pointers for the address space are wider than ptrdiff_t. */
5898 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
5899 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
5903 if (!processing_template_decl
5904 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT
))
5906 op0
= save_expr (op0
);
5907 op1
= save_expr (op1
);
5909 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT
);
5910 *instrument_expr
= build_call_expr_loc (loc
, tt
, 2, op0
, op1
);
5913 /* First do the subtraction, then build the divide operator
5914 and only convert at the very end.
5915 Do not do default conversions in case restype is a short type. */
5917 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5918 pointers. If some platform cannot provide that, or has a larger
5919 ptrdiff_type to support differences larger than half the address
5920 space, cast the pointers to some larger integer type and do the
5921 computations in that type. */
5922 if (TYPE_PRECISION (inttype
) > TYPE_PRECISION (TREE_TYPE (op0
)))
5923 op0
= cp_build_binary_op (loc
,
5925 cp_convert (inttype
, op0
, complain
),
5926 cp_convert (inttype
, op1
, complain
),
5929 op0
= build2_loc (loc
, POINTER_DIFF_EXPR
, inttype
, op0
, op1
);
5931 /* This generates an error if op1 is a pointer to an incomplete type. */
5932 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
5934 if (complain
& tf_error
)
5935 error_at (loc
, "invalid use of a pointer to an incomplete type in "
5936 "pointer arithmetic");
5938 return error_mark_node
;
5941 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1
)))
5943 if (complain
& tf_error
)
5944 error_at (loc
, "arithmetic on pointer to an empty aggregate");
5946 return error_mark_node
;
5949 op1
= (TYPE_PTROB_P (ptrtype
)
5950 ? size_in_bytes_loc (loc
, target_type
)
5951 : integer_one_node
);
5953 /* Do the division. */
5955 result
= build2_loc (loc
, EXACT_DIV_EXPR
, inttype
, op0
,
5956 cp_convert (inttype
, op1
, complain
));
5957 return cp_convert (restype
, result
, complain
);
5960 /* Construct and perhaps optimize a tree representation
5961 for a unary operation. CODE, a tree_code, specifies the operation
5962 and XARG is the operand. */
5965 build_x_unary_op (location_t loc
, enum tree_code code
, cp_expr xarg
,
5966 tsubst_flags_t complain
)
5968 tree orig_expr
= xarg
;
5971 tree overload
= NULL_TREE
;
5973 if (processing_template_decl
)
5975 if (type_dependent_expression_p (xarg
))
5977 tree e
= build_min_nt_loc (loc
, code
, xarg
.get_value (), NULL_TREE
);
5978 maybe_save_operator_binding (e
);
5982 xarg
= build_non_dependent_expr (xarg
);
5987 /* [expr.unary.op] says:
5989 The address of an object of incomplete type can be taken.
5991 (And is just the ordinary address operator, not an overloaded
5992 "operator &".) However, if the type is a template
5993 specialization, we must complete the type at this point so that
5994 an overloaded "operator &" will be available if required. */
5995 if (code
== ADDR_EXPR
5996 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
5997 && ((CLASS_TYPE_P (TREE_TYPE (xarg
))
5998 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg
))))
5999 || (TREE_CODE (xarg
) == OFFSET_REF
)))
6000 /* Don't look for a function. */;
6002 exp
= build_new_op (loc
, code
, LOOKUP_NORMAL
, xarg
, NULL_TREE
,
6003 NULL_TREE
, &overload
, complain
);
6005 if (!exp
&& code
== ADDR_EXPR
)
6007 if (is_overloaded_fn (xarg
))
6009 tree fn
= get_first_fn (xarg
);
6010 if (DECL_CONSTRUCTOR_P (fn
) || DECL_DESTRUCTOR_P (fn
))
6012 if (complain
& tf_error
)
6013 error_at (loc
, DECL_CONSTRUCTOR_P (fn
)
6014 ? G_("taking address of constructor %qD")
6015 : G_("taking address of destructor %qD"),
6017 return error_mark_node
;
6021 /* A pointer to member-function can be formed only by saying
6023 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
6024 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
6026 if (TREE_CODE (xarg
) != OFFSET_REF
6027 || !TYPE_P (TREE_OPERAND (xarg
, 0)))
6029 if (complain
& tf_error
)
6031 error_at (loc
, "invalid use of %qE to form a "
6032 "pointer-to-member-function", xarg
.get_value ());
6033 if (TREE_CODE (xarg
) != OFFSET_REF
)
6034 inform (loc
, " a qualified-id is required");
6036 return error_mark_node
;
6040 if (complain
& tf_error
)
6041 error_at (loc
, "parentheses around %qE cannot be used to "
6042 "form a pointer-to-member-function",
6045 return error_mark_node
;
6046 PTRMEM_OK_P (xarg
) = 1;
6050 if (TREE_CODE (xarg
) == OFFSET_REF
)
6052 ptrmem
= PTRMEM_OK_P (xarg
);
6054 if (!ptrmem
&& !flag_ms_extensions
6055 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
6057 /* A single non-static member, make sure we don't allow a
6058 pointer-to-member. */
6059 xarg
= build2 (OFFSET_REF
, TREE_TYPE (xarg
),
6060 TREE_OPERAND (xarg
, 0),
6061 ovl_make (TREE_OPERAND (xarg
, 1)));
6062 PTRMEM_OK_P (xarg
) = ptrmem
;
6066 exp
= cp_build_addr_expr_strict (xarg
, complain
);
6069 if (processing_template_decl
&& exp
!= error_mark_node
)
6071 if (overload
!= NULL_TREE
)
6072 return (build_min_non_dep_op_overload
6073 (code
, exp
, overload
, orig_expr
, integer_zero_node
));
6075 exp
= build_min_non_dep (code
, exp
, orig_expr
,
6076 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE
);
6078 if (TREE_CODE (exp
) == ADDR_EXPR
)
6079 PTRMEM_OK_P (exp
) = ptrmem
;
6083 /* Construct and perhaps optimize a tree representation
6084 for __builtin_addressof operation. ARG specifies the operand. */
6087 cp_build_addressof (location_t loc
, tree arg
, tsubst_flags_t complain
)
6089 tree orig_expr
= arg
;
6091 if (processing_template_decl
)
6093 if (type_dependent_expression_p (arg
))
6094 return build_min_nt_loc (loc
, ADDRESSOF_EXPR
, arg
, NULL_TREE
);
6096 arg
= build_non_dependent_expr (arg
);
6099 tree exp
= cp_build_addr_expr_strict (arg
, complain
);
6101 if (processing_template_decl
&& exp
!= error_mark_node
)
6102 exp
= build_min_non_dep (ADDRESSOF_EXPR
, exp
, orig_expr
, NULL_TREE
);
6106 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
6107 constants, where a null value is represented by an INTEGER_CST of
6111 cp_truthvalue_conversion (tree expr
, tsubst_flags_t complain
)
6113 tree type
= TREE_TYPE (expr
);
6114 location_t loc
= cp_expr_loc_or_input_loc (expr
);
6115 if (TYPE_PTR_OR_PTRMEM_P (type
)
6116 /* Avoid ICE on invalid use of non-static member function. */
6117 || TREE_CODE (expr
) == FUNCTION_DECL
)
6118 return cp_build_binary_op (loc
, NE_EXPR
, expr
, nullptr_node
, complain
);
6120 return c_common_truthvalue_conversion (loc
, expr
);
6123 /* Returns EXPR contextually converted to bool. */
6126 contextual_conv_bool (tree expr
, tsubst_flags_t complain
)
6128 return perform_implicit_conversion_flags (boolean_type_node
, expr
,
6129 complain
, LOOKUP_NORMAL
);
6132 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
6133 is a low-level function; most callers should use maybe_convert_cond. */
6136 condition_conversion (tree expr
)
6138 tree t
= contextual_conv_bool (expr
, tf_warning_or_error
);
6139 if (!processing_template_decl
)
6140 t
= fold_build_cleanup_point_expr (boolean_type_node
, t
);
6144 /* Returns the address of T. This function will fold away
6145 ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
6146 most places should use cp_build_addr_expr instead. */
6149 build_address (tree t
)
6151 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
6152 return error_mark_node
;
6153 gcc_checking_assert (TREE_CODE (t
) != CONSTRUCTOR
6154 || processing_template_decl
);
6155 t
= build_fold_addr_expr_loc (EXPR_LOCATION (t
), t
);
6156 if (TREE_CODE (t
) != ADDR_EXPR
)
6161 /* Return a NOP_EXPR converting EXPR to TYPE. */
6164 build_nop (tree type
, tree expr
)
6166 if (type
== error_mark_node
|| error_operand_p (expr
))
6168 return build1_loc (EXPR_LOCATION (expr
), NOP_EXPR
, type
, expr
);
6171 /* Take the address of ARG, whatever that means under C++ semantics.
6172 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
6173 and class rvalues as well.
6175 Nothing should call this function directly; instead, callers should use
6176 cp_build_addr_expr or cp_build_addr_expr_strict. */
6179 cp_build_addr_expr_1 (tree arg
, bool strict_lvalue
, tsubst_flags_t complain
)
6184 if (!arg
|| error_operand_p (arg
))
6185 return error_mark_node
;
6187 arg
= mark_lvalue_use (arg
);
6188 if (error_operand_p (arg
))
6189 return error_mark_node
;
6191 argtype
= lvalue_type (arg
);
6192 location_t loc
= cp_expr_loc_or_input_loc (arg
);
6194 gcc_assert (!(identifier_p (arg
) && IDENTIFIER_ANY_OP_P (arg
)));
6196 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
6197 && !really_overloaded_fn (arg
))
6199 /* They're trying to take the address of a unique non-static
6200 member function. This is ill-formed (except in MS-land),
6201 but let's try to DTRT.
6202 Note: We only handle unique functions here because we don't
6203 want to complain if there's a static overload; non-unique
6204 cases will be handled by instantiate_type. But we need to
6205 handle this case here to allow casts on the resulting PMF.
6206 We could defer this in non-MS mode, but it's easier to give
6207 a useful error here. */
6209 /* Inside constant member functions, the `this' pointer
6210 contains an extra const qualifier. TYPE_MAIN_VARIANT
6211 is used here to remove this const from the diagnostics
6212 and the created OFFSET_REF. */
6213 tree base
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg
, 0)));
6214 tree fn
= get_first_fn (TREE_OPERAND (arg
, 1));
6215 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
6216 return error_mark_node
;
6218 if (! flag_ms_extensions
)
6220 tree name
= DECL_NAME (fn
);
6221 if (!(complain
& tf_error
))
6222 return error_mark_node
;
6223 else if (current_class_type
6224 && TREE_OPERAND (arg
, 0) == current_class_ref
)
6225 /* An expression like &memfn. */
6227 "ISO C++ forbids taking the address of an unqualified"
6228 " or parenthesized non-static member function to form"
6229 " a pointer to member function. Say %<&%T::%D%>",
6233 "ISO C++ forbids taking the address of a bound member"
6234 " function to form a pointer to member function."
6238 arg
= build_offset_ref (base
, fn
, /*address_p=*/true, complain
);
6241 /* Uninstantiated types are all functions. Taking the
6242 address of a function is a no-op, so just return the
6244 if (type_unknown_p (arg
))
6245 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
6247 if (TREE_CODE (arg
) == OFFSET_REF
)
6248 /* We want a pointer to member; bypass all the code for actually taking
6249 the address of something. */
6252 /* Anything not already handled and not a true memory reference
6254 if (!FUNC_OR_METHOD_TYPE_P (argtype
))
6256 cp_lvalue_kind kind
= lvalue_kind (arg
);
6257 if (kind
== clk_none
)
6259 if (complain
& tf_error
)
6260 lvalue_error (loc
, lv_addressof
);
6261 return error_mark_node
;
6263 if (strict_lvalue
&& (kind
& (clk_rvalueref
|clk_class
)))
6265 if (!(complain
& tf_error
))
6266 return error_mark_node
;
6267 /* Make this a permerror because we used to accept it. */
6268 permerror (loc
, "taking address of rvalue");
6272 if (TYPE_REF_P (argtype
))
6274 tree type
= build_pointer_type (TREE_TYPE (argtype
));
6275 arg
= build1 (CONVERT_EXPR
, type
, arg
);
6278 else if (pedantic
&& DECL_MAIN_P (tree_strip_any_location_wrapper (arg
)))
6281 /* Apparently a lot of autoconf scripts for C++ packages do this,
6282 so only complain if -Wpedantic. */
6283 if (complain
& (flag_pedantic_errors
? tf_error
: tf_warning
))
6284 pedwarn (loc
, OPT_Wpedantic
,
6285 "ISO C++ forbids taking address of function %<::main%>");
6286 else if (flag_pedantic_errors
)
6287 return error_mark_node
;
6290 /* Let &* cancel out to simplify resulting code. */
6291 if (INDIRECT_REF_P (arg
))
6293 arg
= TREE_OPERAND (arg
, 0);
6294 if (TYPE_REF_P (TREE_TYPE (arg
)))
6296 tree type
= build_pointer_type (TREE_TYPE (TREE_TYPE (arg
)));
6297 arg
= build1 (CONVERT_EXPR
, type
, arg
);
6300 /* Don't let this be an lvalue. */
6305 /* Handle complex lvalues (when permitted)
6306 by reduction to simpler cases. */
6307 val
= unary_complex_lvalue (ADDR_EXPR
, arg
);
6311 switch (TREE_CODE (arg
))
6315 case FIX_TRUNC_EXPR
:
6316 /* We should have handled this above in the lvalue_kind check. */
6321 arg
= BASELINK_FUNCTIONS (arg
);
6325 arg
= OVL_FIRST (arg
);
6330 /* Turn a reference to a non-static data member into a
6331 pointer-to-member. */
6336 gcc_assert (PTRMEM_OK_P (arg
));
6338 t
= TREE_OPERAND (arg
, 1);
6339 if (TYPE_REF_P (TREE_TYPE (t
)))
6341 if (complain
& tf_error
)
6343 "cannot create pointer to reference member %qD", t
);
6344 return error_mark_node
;
6347 type
= build_ptrmem_type (context_for_name_lookup (t
),
6349 t
= make_ptrmem_cst (type
, TREE_OPERAND (arg
, 1));
6357 if (argtype
!= error_mark_node
)
6358 argtype
= build_pointer_type (argtype
);
6360 if (bitfield_p (arg
))
6362 if (complain
& tf_error
)
6363 error_at (loc
, "attempt to take address of bit-field");
6364 return error_mark_node
;
6367 /* In a template, we are processing a non-dependent expression
6368 so we can just form an ADDR_EXPR with the correct type. */
6369 if (processing_template_decl
|| TREE_CODE (arg
) != COMPONENT_REF
)
6371 tree stripped_arg
= tree_strip_any_location_wrapper (arg
);
6372 if (TREE_CODE (stripped_arg
) == FUNCTION_DECL
6373 && DECL_IMMEDIATE_FUNCTION_P (stripped_arg
)
6374 && cp_unevaluated_operand
== 0
6375 && (current_function_decl
== NULL_TREE
6376 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl
)))
6378 if (complain
& tf_error
)
6379 error_at (loc
, "taking address of an immediate function %qD",
6381 return error_mark_node
;
6383 if (TREE_CODE (stripped_arg
) == FUNCTION_DECL
6384 && !mark_used (stripped_arg
, complain
) && !(complain
& tf_error
))
6385 return error_mark_node
;
6386 val
= build_address (arg
);
6387 if (TREE_CODE (arg
) == OFFSET_REF
)
6388 PTRMEM_OK_P (val
) = PTRMEM_OK_P (arg
);
6390 else if (BASELINK_P (TREE_OPERAND (arg
, 1)))
6392 tree fn
= BASELINK_FUNCTIONS (TREE_OPERAND (arg
, 1));
6394 /* We can only get here with a single static member
6396 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
6397 && DECL_STATIC_FUNCTION_P (fn
));
6398 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
6399 return error_mark_node
;
6400 val
= build_address (fn
);
6401 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
6402 /* Do not lose object's side effects. */
6403 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
),
6404 TREE_OPERAND (arg
, 0), val
);
6408 tree object
= TREE_OPERAND (arg
, 0);
6409 tree field
= TREE_OPERAND (arg
, 1);
6410 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6411 (TREE_TYPE (object
), decl_type_context (field
)));
6412 val
= build_address (arg
);
6415 if (TYPE_PTR_P (argtype
)
6416 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
6418 build_ptrmemfunc_type (argtype
);
6419 val
= build_ptrmemfunc (argtype
, val
, 0,
6427 /* Take the address of ARG if it has one, even if it's an rvalue. */
6430 cp_build_addr_expr (tree arg
, tsubst_flags_t complain
)
6432 return cp_build_addr_expr_1 (arg
, 0, complain
);
6435 /* Take the address of ARG, but only if it's an lvalue. */
6438 cp_build_addr_expr_strict (tree arg
, tsubst_flags_t complain
)
6440 return cp_build_addr_expr_1 (arg
, 1, complain
);
6443 /* C++: Must handle pointers to members.
6445 Perhaps type instantiation should be extended to handle conversion
6446 from aggregates to types we don't yet know we want? (Or are those
6447 cases typically errors which should be reported?)
6449 NOCONVERT suppresses the default promotions (such as from short to int). */
6452 cp_build_unary_op (enum tree_code code
, tree xarg
, bool noconvert
,
6453 tsubst_flags_t complain
)
6455 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
6457 location_t location
= cp_expr_loc_or_input_loc (arg
);
6459 const char *errstring
= NULL
;
6461 const char *invalid_op_diag
;
6463 if (!arg
|| error_operand_p (arg
))
6464 return error_mark_node
;
6466 arg
= resolve_nondeduced_context (arg
, complain
);
6468 if ((invalid_op_diag
6469 = targetm
.invalid_unary_op ((code
== UNARY_PLUS_EXPR
6474 if (complain
& tf_error
)
6475 error (invalid_op_diag
);
6476 return error_mark_node
;
6481 case UNARY_PLUS_EXPR
:
6484 int flags
= WANT_ARITH
| WANT_ENUM
;
6485 /* Unary plus (but not unary minus) is allowed on pointers. */
6486 if (code
== UNARY_PLUS_EXPR
)
6487 flags
|= WANT_POINTER
;
6488 arg
= build_expr_type_conversion (flags
, arg
, true);
6490 errstring
= (code
== NEGATE_EXPR
6491 ? _("wrong type argument to unary minus")
6492 : _("wrong type argument to unary plus"));
6495 if (!noconvert
&& INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg
)))
6496 arg
= cp_perform_integral_promotions (arg
, complain
);
6498 /* Make sure the result is not an lvalue: a unary plus or minus
6499 expression is always a rvalue. */
6506 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
6511 arg
= cp_default_conversion (arg
, complain
);
6512 if (arg
== error_mark_node
)
6513 return error_mark_node
;
6516 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
6517 | WANT_VECTOR_OR_COMPLEX
,
6519 errstring
= _("wrong type argument to bit-complement");
6520 else if (!noconvert
&& INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg
)))
6522 /* Warn if the expression has boolean value. */
6523 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
6524 && (complain
& tf_warning
)
6525 && warning_at (location
, OPT_Wbool_operation
,
6526 "%<~%> on an expression of type %<bool%>"))
6527 inform (location
, "did you mean to use logical not (%<!%>)?");
6528 arg
= cp_perform_integral_promotions (arg
, complain
);
6530 else if (!noconvert
&& VECTOR_TYPE_P (TREE_TYPE (arg
)))
6531 arg
= mark_rvalue_use (arg
);
6535 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
6536 errstring
= _("wrong type argument to abs");
6537 else if (!noconvert
)
6539 arg
= cp_default_conversion (arg
, complain
);
6540 if (arg
== error_mark_node
)
6541 return error_mark_node
;
6546 /* Conjugating a real value is a no-op, but allow it anyway. */
6547 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
6548 errstring
= _("wrong type argument to conjugation");
6549 else if (!noconvert
)
6551 arg
= cp_default_conversion (arg
, complain
);
6552 if (arg
== error_mark_node
)
6553 return error_mark_node
;
6557 case TRUTH_NOT_EXPR
:
6558 if (gnu_vector_type_p (TREE_TYPE (arg
)))
6559 return cp_build_binary_op (input_location
, EQ_EXPR
, arg
,
6560 build_zero_cst (TREE_TYPE (arg
)), complain
);
6561 arg
= perform_implicit_conversion (boolean_type_node
, arg
,
6563 val
= invert_truthvalue_loc (location
, arg
);
6564 if (arg
!= error_mark_node
)
6566 errstring
= _("in argument to unary !");
6574 arg
= build_real_imag_expr (input_location
, code
, arg
);
6577 case PREINCREMENT_EXPR
:
6578 case POSTINCREMENT_EXPR
:
6579 case PREDECREMENT_EXPR
:
6580 case POSTDECREMENT_EXPR
:
6581 /* Handle complex lvalues (when permitted)
6582 by reduction to simpler cases. */
6584 val
= unary_complex_lvalue (code
, arg
);
6588 arg
= mark_lvalue_use (arg
);
6590 /* Increment or decrement the real part of the value,
6591 and don't change the imaginary part. */
6592 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
6596 arg
= cp_stabilize_reference (arg
);
6597 real
= cp_build_unary_op (REALPART_EXPR
, arg
, true, complain
);
6598 imag
= cp_build_unary_op (IMAGPART_EXPR
, arg
, true, complain
);
6599 real
= cp_build_unary_op (code
, real
, true, complain
);
6600 if (real
== error_mark_node
|| imag
== error_mark_node
)
6601 return error_mark_node
;
6602 return build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
6606 /* Report invalid types. */
6608 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
6611 if (code
== PREINCREMENT_EXPR
)
6612 errstring
= _("no pre-increment operator for type");
6613 else if (code
== POSTINCREMENT_EXPR
)
6614 errstring
= _("no post-increment operator for type");
6615 else if (code
== PREDECREMENT_EXPR
)
6616 errstring
= _("no pre-decrement operator for type");
6618 errstring
= _("no post-decrement operator for type");
6621 else if (arg
== error_mark_node
)
6622 return error_mark_node
;
6624 /* Report something read-only. */
6626 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
6627 || TREE_READONLY (arg
))
6629 if (complain
& tf_error
)
6630 cxx_readonly_error (location
, arg
,
6631 ((code
== PREINCREMENT_EXPR
6632 || code
== POSTINCREMENT_EXPR
)
6633 ? lv_increment
: lv_decrement
));
6635 return error_mark_node
;
6640 tree declared_type
= unlowered_expr_type (arg
);
6642 argtype
= TREE_TYPE (arg
);
6644 /* ARM $5.2.5 last annotation says this should be forbidden. */
6645 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
6647 if (complain
& tf_error
)
6648 permerror (location
, (code
== PREINCREMENT_EXPR
6649 || code
== POSTINCREMENT_EXPR
)
6650 ? G_("ISO C++ forbids incrementing an enum")
6651 : G_("ISO C++ forbids decrementing an enum"));
6653 return error_mark_node
;
6656 /* Compute the increment. */
6658 if (TYPE_PTR_P (argtype
))
6660 tree type
= complete_type (TREE_TYPE (argtype
));
6662 if (!COMPLETE_OR_VOID_TYPE_P (type
))
6664 if (complain
& tf_error
)
6665 error_at (location
, ((code
== PREINCREMENT_EXPR
6666 || code
== POSTINCREMENT_EXPR
))
6667 ? G_("cannot increment a pointer to incomplete "
6669 : G_("cannot decrement a pointer to incomplete "
6671 TREE_TYPE (argtype
));
6673 return error_mark_node
;
6675 else if (!TYPE_PTROB_P (argtype
))
6677 if (complain
& tf_error
)
6678 pedwarn (location
, OPT_Wpointer_arith
,
6679 (code
== PREINCREMENT_EXPR
6680 || code
== POSTINCREMENT_EXPR
)
6681 ? G_("ISO C++ forbids incrementing a pointer "
6683 : G_("ISO C++ forbids decrementing a pointer "
6687 return error_mark_node
;
6689 else if (!verify_type_context (location
, TCTX_POINTER_ARITH
,
6690 TREE_TYPE (argtype
),
6691 !(complain
& tf_error
)))
6692 return error_mark_node
;
6694 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
6697 inc
= VECTOR_TYPE_P (argtype
)
6698 ? build_one_cst (argtype
)
6701 inc
= cp_convert (argtype
, inc
, complain
);
6703 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6704 need to ask Objective-C to build the increment or decrement
6705 expression for it. */
6706 if (objc_is_property_ref (arg
))
6707 return objc_build_incr_expr_for_property_ref (input_location
, code
,
6710 /* Complain about anything else that is not a true lvalue. */
6711 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
6712 || code
== POSTINCREMENT_EXPR
)
6713 ? lv_increment
: lv_decrement
),
6715 return error_mark_node
;
6717 /* [depr.volatile.type] "Postfix ++ and -- expressions and
6718 prefix ++ and -- expressions of volatile-qualified arithmetic
6719 and pointer types are deprecated." */
6720 if (TREE_THIS_VOLATILE (arg
) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg
)))
6721 warning_at (location
, OPT_Wvolatile
,
6722 "%qs expression of %<volatile%>-qualified type is "
6724 ((code
== PREINCREMENT_EXPR
6725 || code
== POSTINCREMENT_EXPR
)
6728 /* Forbid using -- or ++ in C++17 on `bool'. */
6729 if (TREE_CODE (declared_type
) == BOOLEAN_TYPE
)
6731 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
6733 if (complain
& tf_error
)
6735 "use of an operand of type %qT in %<operator--%> "
6736 "is forbidden", boolean_type_node
);
6737 return error_mark_node
;
6741 if (cxx_dialect
>= cxx17
)
6743 if (complain
& tf_error
)
6745 "use of an operand of type %qT in "
6746 "%<operator++%> is forbidden in C++17",
6748 return error_mark_node
;
6750 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6752 warning_at (location
, OPT_Wdeprecated
,
6753 "use of an operand of type %qT "
6754 "in %<operator++%> is deprecated",
6757 val
= boolean_increment (code
, arg
);
6759 else if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
6760 /* An rvalue has no cv-qualifiers. */
6761 val
= build2 (code
, cv_unqualified (TREE_TYPE (arg
)), arg
, inc
);
6763 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
6765 TREE_SIDE_EFFECTS (val
) = 1;
6770 /* Note that this operation never does default_conversion
6771 regardless of NOCONVERT. */
6772 return cp_build_addr_expr (arg
, complain
);
6781 argtype
= TREE_TYPE (arg
);
6782 return build1 (code
, argtype
, arg
);
6785 if (complain
& tf_error
)
6786 error_at (location
, "%s", errstring
);
6787 return error_mark_node
;
6790 /* Hook for the c-common bits that build a unary op. */
6792 build_unary_op (location_t
/*location*/,
6793 enum tree_code code
, tree xarg
, bool noconvert
)
6795 return cp_build_unary_op (code
, xarg
, noconvert
, tf_warning_or_error
);
6798 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
6799 so that it is a valid lvalue even for GENERIC by replacing
6800 (lhs = rhs) with ((lhs = rhs), lhs)
6801 (--lhs) with ((--lhs), lhs)
6802 (++lhs) with ((++lhs), lhs)
6803 and if lhs has side-effects, calling cp_stabilize_reference on it, so
6804 that it can be evaluated multiple times. */
6807 genericize_compound_lvalue (tree lvalue
)
6809 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue
, 0)))
6810 lvalue
= build2 (TREE_CODE (lvalue
), TREE_TYPE (lvalue
),
6811 cp_stabilize_reference (TREE_OPERAND (lvalue
, 0)),
6812 TREE_OPERAND (lvalue
, 1));
6813 return build2 (COMPOUND_EXPR
, TREE_TYPE (TREE_OPERAND (lvalue
, 0)),
6814 lvalue
, TREE_OPERAND (lvalue
, 0));
6817 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6818 for certain kinds of expressions which are not really lvalues
6819 but which we can accept as lvalues.
6821 If ARG is not a kind of expression we can handle, return
6825 unary_complex_lvalue (enum tree_code code
, tree arg
)
6827 /* Inside a template, making these kinds of adjustments is
6828 pointless; we are only concerned with the type of the
6830 if (processing_template_decl
)
6833 /* Handle (a, b) used as an "lvalue". */
6834 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
6836 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 1), false,
6837 tf_warning_or_error
);
6838 return build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6839 TREE_OPERAND (arg
, 0), real_result
);
6842 /* Handle (a ? b : c) used as an "lvalue". */
6843 if (TREE_CODE (arg
) == COND_EXPR
6844 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
6845 return rationalize_conditional_expr (code
, arg
, tf_warning_or_error
);
6847 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6848 if (TREE_CODE (arg
) == MODIFY_EXPR
6849 || TREE_CODE (arg
) == PREINCREMENT_EXPR
6850 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
6851 return unary_complex_lvalue (code
, genericize_compound_lvalue (arg
));
6853 if (code
!= ADDR_EXPR
)
6856 /* Handle (a = b) used as an "lvalue" for `&'. */
6857 if (TREE_CODE (arg
) == MODIFY_EXPR
6858 || TREE_CODE (arg
) == INIT_EXPR
)
6860 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 0), false,
6861 tf_warning_or_error
);
6862 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6864 TREE_NO_WARNING (arg
) = 1;
6868 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg
))
6869 || TREE_CODE (arg
) == OFFSET_REF
)
6872 /* We permit compiler to make function calls returning
6873 objects of aggregate type look like lvalues. */
6877 if (TREE_CODE (targ
) == SAVE_EXPR
)
6878 targ
= TREE_OPERAND (targ
, 0);
6880 if (TREE_CODE (targ
) == CALL_EXPR
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (targ
)))
6882 if (TREE_CODE (arg
) == SAVE_EXPR
)
6885 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, tf_warning_or_error
);
6886 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
6889 if (TREE_CODE (arg
) == SAVE_EXPR
&& INDIRECT_REF_P (targ
))
6890 return build3 (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
6891 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
6894 /* Don't let anything else be handled specially. */
6898 /* Mark EXP saying that we need to be able to take the
6899 address of it; it should not be allocated in a register.
6900 Value is true if successful. ARRAY_REF_P is true if this
6901 is for ARRAY_REF construction - in that case we don't want
6902 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6903 it is fine to use ARRAY_REFs for vector subscripts on vector
6906 C++: we do not allow `current_class_ptr' to be addressable. */
6909 cxx_mark_addressable (tree exp
, bool array_ref_p
)
6914 switch (TREE_CODE (x
))
6916 case VIEW_CONVERT_EXPR
:
6918 && TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
6919 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x
, 0))))
6927 x
= TREE_OPERAND (x
, 0);
6931 if (x
== current_class_ptr
)
6933 error ("cannot take the address of %<this%>, which is an rvalue expression");
6934 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later. */
6940 /* Caller should not be trying to mark initialized
6941 constant fields addressable. */
6942 gcc_assert (DECL_LANG_SPECIFIC (x
) == 0
6943 || DECL_IN_AGGR_P (x
) == 0
6945 || DECL_EXTERNAL (x
));
6949 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
6950 && !DECL_ARTIFICIAL (x
))
6952 if (VAR_P (x
) && DECL_HARD_REGISTER (x
))
6955 ("address of explicit register variable %qD requested", x
);
6958 else if (extra_warnings
)
6960 (OPT_Wextra
, "address requested for %qD, which is declared %<register%>", x
);
6962 TREE_ADDRESSABLE (x
) = 1;
6967 TREE_ADDRESSABLE (x
) = 1;
6971 TREE_ADDRESSABLE (x
) = 1;
6975 TREE_ADDRESSABLE (x
) = 1;
6976 cxx_mark_addressable (TREE_OPERAND (x
, 0));
6984 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6987 build_x_conditional_expr (location_t loc
, tree ifexp
, tree op1
, tree op2
,
6988 tsubst_flags_t complain
)
6990 tree orig_ifexp
= ifexp
;
6991 tree orig_op1
= op1
;
6992 tree orig_op2
= op2
;
6995 if (processing_template_decl
)
6997 /* The standard says that the expression is type-dependent if
6998 IFEXP is type-dependent, even though the eventual type of the
6999 expression doesn't dependent on IFEXP. */
7000 if (type_dependent_expression_p (ifexp
)
7001 /* As a GNU extension, the middle operand may be omitted. */
7002 || (op1
&& type_dependent_expression_p (op1
))
7003 || type_dependent_expression_p (op2
))
7004 return build_min_nt_loc (loc
, COND_EXPR
, ifexp
, op1
, op2
);
7005 ifexp
= build_non_dependent_expr (ifexp
);
7007 op1
= build_non_dependent_expr (op1
);
7008 op2
= build_non_dependent_expr (op2
);
7011 expr
= build_conditional_expr (loc
, ifexp
, op1
, op2
, complain
);
7012 if (processing_template_decl
&& expr
!= error_mark_node
)
7014 tree min
= build_min_non_dep (COND_EXPR
, expr
,
7015 orig_ifexp
, orig_op1
, orig_op2
);
7016 expr
= convert_from_reference (min
);
7021 /* Given a list of expressions, return a compound expression
7022 that performs them all and returns the value of the last of them. */
7025 build_x_compound_expr_from_list (tree list
, expr_list_kind exp
,
7026 tsubst_flags_t complain
)
7028 tree expr
= TREE_VALUE (list
);
7030 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
7031 && !CONSTRUCTOR_IS_DIRECT_INIT (expr
))
7033 if (complain
& tf_error
)
7034 pedwarn (cp_expr_loc_or_input_loc (expr
), 0,
7035 "list-initializer for non-class type must not "
7036 "be parenthesized");
7038 return error_mark_node
;
7041 if (TREE_CHAIN (list
))
7043 if (complain
& tf_error
)
7047 permerror (input_location
, "expression list treated as compound "
7048 "expression in initializer");
7051 permerror (input_location
, "expression list treated as compound "
7052 "expression in mem-initializer");
7055 permerror (input_location
, "expression list treated as compound "
7056 "expression in functional cast");
7062 return error_mark_node
;
7064 for (list
= TREE_CHAIN (list
); list
; list
= TREE_CHAIN (list
))
7065 expr
= build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list
)),
7066 expr
, TREE_VALUE (list
), complain
);
7072 /* Like build_x_compound_expr_from_list, but using a VEC. */
7075 build_x_compound_expr_from_vec (vec
<tree
, va_gc
> *vec
, const char *msg
,
7076 tsubst_flags_t complain
)
7078 if (vec_safe_is_empty (vec
))
7080 else if (vec
->length () == 1)
7090 if (complain
& tf_error
)
7091 permerror (input_location
,
7092 "%s expression list treated as compound expression",
7095 return error_mark_node
;
7099 for (ix
= 1; vec
->iterate (ix
, &t
); ++ix
)
7100 expr
= build_x_compound_expr (EXPR_LOCATION (t
), expr
,
7107 /* Handle overloading of the ',' operator when needed. */
7110 build_x_compound_expr (location_t loc
, tree op1
, tree op2
,
7111 tsubst_flags_t complain
)
7114 tree orig_op1
= op1
;
7115 tree orig_op2
= op2
;
7116 tree overload
= NULL_TREE
;
7118 if (processing_template_decl
)
7120 if (type_dependent_expression_p (op1
)
7121 || type_dependent_expression_p (op2
))
7122 return build_min_nt_loc (loc
, COMPOUND_EXPR
, op1
, op2
);
7123 op1
= build_non_dependent_expr (op1
);
7124 op2
= build_non_dependent_expr (op2
);
7127 result
= build_new_op (loc
, COMPOUND_EXPR
, LOOKUP_NORMAL
, op1
, op2
,
7128 NULL_TREE
, &overload
, complain
);
7130 result
= cp_build_compound_expr (op1
, op2
, complain
);
7132 if (processing_template_decl
&& result
!= error_mark_node
)
7134 if (overload
!= NULL_TREE
)
7135 return (build_min_non_dep_op_overload
7136 (COMPOUND_EXPR
, result
, overload
, orig_op1
, orig_op2
));
7138 return build_min_non_dep (COMPOUND_EXPR
, result
, orig_op1
, orig_op2
);
7144 /* Like cp_build_compound_expr, but for the c-common bits. */
7147 build_compound_expr (location_t
/*loc*/, tree lhs
, tree rhs
)
7149 return cp_build_compound_expr (lhs
, rhs
, tf_warning_or_error
);
7152 /* Build a compound expression. */
7155 cp_build_compound_expr (tree lhs
, tree rhs
, tsubst_flags_t complain
)
7157 lhs
= convert_to_void (lhs
, ICV_LEFT_OF_COMMA
, complain
);
7159 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
7160 return error_mark_node
;
7162 if (TREE_CODE (rhs
) == TARGET_EXPR
)
7164 /* If the rhs is a TARGET_EXPR, then build the compound
7165 expression inside the target_expr's initializer. This
7166 helps the compiler to eliminate unnecessary temporaries. */
7167 tree init
= TREE_OPERAND (rhs
, 1);
7169 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), lhs
, init
);
7170 TREE_OPERAND (rhs
, 1) = init
;
7175 if (type_unknown_p (rhs
))
7177 if (complain
& tf_error
)
7178 error_at (cp_expr_loc_or_input_loc (rhs
),
7179 "no context to resolve type of %qE", rhs
);
7180 return error_mark_node
;
7183 return build2 (COMPOUND_EXPR
, TREE_TYPE (rhs
), lhs
, rhs
);
7186 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
7187 casts away constness. CAST gives the type of cast. Returns true
7188 if the cast is ill-formed, false if it is well-formed.
7190 ??? This function warns for casting away any qualifier not just
7191 const. We would like to specify exactly what qualifiers are casted
7196 check_for_casting_away_constness (location_t loc
, tree src_type
,
7197 tree dest_type
, enum tree_code cast
,
7198 tsubst_flags_t complain
)
7200 /* C-style casts are allowed to cast away constness. With
7201 WARN_CAST_QUAL, we still want to issue a warning. */
7202 if (cast
== CAST_EXPR
&& !warn_cast_qual
)
7205 if (!casts_away_constness (src_type
, dest_type
, complain
))
7211 if (complain
& tf_warning
)
7212 warning_at (loc
, OPT_Wcast_qual
,
7213 "cast from type %qT to type %qT casts away qualifiers",
7214 src_type
, dest_type
);
7217 case STATIC_CAST_EXPR
:
7218 if (complain
& tf_error
)
7219 error_at (loc
, "%<static_cast%> from type %qT to type %qT casts "
7221 src_type
, dest_type
);
7224 case REINTERPRET_CAST_EXPR
:
7225 if (complain
& tf_error
)
7226 error_at (loc
, "%<reinterpret_cast%> from type %qT to type %qT "
7227 "casts away qualifiers",
7228 src_type
, dest_type
);
7236 /* Warns if the cast from expression EXPR to type TYPE is useless. */
7238 maybe_warn_about_useless_cast (location_t loc
, tree type
, tree expr
,
7239 tsubst_flags_t complain
)
7241 if (warn_useless_cast
7242 && complain
& tf_warning
)
7244 if ((TYPE_REF_P (type
)
7245 && (TYPE_REF_IS_RVALUE (type
)
7246 ? xvalue_p (expr
) : lvalue_p (expr
))
7247 && same_type_p (TREE_TYPE (expr
), TREE_TYPE (type
)))
7248 || same_type_p (TREE_TYPE (expr
), type
))
7249 warning_at (loc
, OPT_Wuseless_cast
,
7250 "useless cast to type %q#T", type
);
7254 /* Warns if the cast ignores cv-qualifiers on TYPE. */
7256 maybe_warn_about_cast_ignoring_quals (location_t loc
, tree type
,
7257 tsubst_flags_t complain
)
7259 if (warn_ignored_qualifiers
7260 && complain
& tf_warning
7261 && !CLASS_TYPE_P (type
)
7262 && (cp_type_quals (type
) & (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)))
7263 warning_at (loc
, OPT_Wignored_qualifiers
,
7264 "type qualifiers ignored on cast result type");
7267 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
7268 (another pointer-to-member type in the same hierarchy) and return
7269 the converted expression. If ALLOW_INVERSE_P is permitted, a
7270 pointer-to-derived may be converted to pointer-to-base; otherwise,
7271 only the other direction is permitted. If C_CAST_P is true, this
7272 conversion is taking place as part of a C-style cast. */
7275 convert_ptrmem (tree type
, tree expr
, bool allow_inverse_p
,
7276 bool c_cast_p
, tsubst_flags_t complain
)
7278 if (same_type_p (type
, TREE_TYPE (expr
)))
7281 if (TYPE_PTRDATAMEM_P (type
))
7283 tree obase
= TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr
));
7284 tree nbase
= TYPE_PTRMEM_CLASS_TYPE (type
);
7285 tree delta
= (get_delta_difference
7287 allow_inverse_p
, c_cast_p
, complain
));
7289 if (delta
== error_mark_node
)
7290 return error_mark_node
;
7292 if (!same_type_p (obase
, nbase
))
7294 if (TREE_CODE (expr
) == PTRMEM_CST
)
7295 expr
= cplus_expand_constant (expr
);
7297 tree cond
= cp_build_binary_op (input_location
, EQ_EXPR
, expr
,
7298 build_int_cst (TREE_TYPE (expr
), -1),
7300 tree op1
= build_nop (ptrdiff_type_node
, expr
);
7301 tree op2
= cp_build_binary_op (input_location
, PLUS_EXPR
, op1
, delta
,
7304 expr
= fold_build3_loc (input_location
,
7305 COND_EXPR
, ptrdiff_type_node
, cond
, op1
, op2
);
7308 return build_nop (type
, expr
);
7311 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
,
7312 allow_inverse_p
, c_cast_p
, complain
);
7315 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
7316 this static_cast is being attempted as one of the possible casts
7317 allowed by a C-style cast. (In that case, accessibility of base
7318 classes is not considered, and it is OK to cast away
7319 constness.) Return the result of the cast. *VALID_P is set to
7320 indicate whether or not the cast was valid. */
7323 build_static_cast_1 (location_t loc
, tree type
, tree expr
, bool c_cast_p
,
7324 bool *valid_p
, tsubst_flags_t complain
)
7330 /* Assume the cast is valid. */
7333 intype
= unlowered_expr_type (expr
);
7335 /* Save casted types in the function's used types hash table. */
7336 used_types_insert (type
);
7338 /* A prvalue of non-class type is cv-unqualified. */
7339 if (!CLASS_TYPE_P (type
))
7340 type
= cv_unqualified (type
);
7342 /* [expr.static.cast]
7344 An lvalue of type "cv1 B", where B is a class type, can be cast
7345 to type "reference to cv2 D", where D is a class derived (clause
7346 _class.derived_) from B, if a valid standard conversion from
7347 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
7348 same cv-qualification as, or greater cv-qualification than, cv1,
7349 and B is not a virtual base class of D. */
7350 /* We check this case before checking the validity of "TYPE t =
7351 EXPR;" below because for this case:
7354 struct D : public B { D(const B&); };
7356 void f() { static_cast<const D&>(b); }
7358 we want to avoid constructing a new D. The standard is not
7359 completely clear about this issue, but our interpretation is
7360 consistent with other compilers. */
7361 if (TYPE_REF_P (type
)
7362 && CLASS_TYPE_P (TREE_TYPE (type
))
7363 && CLASS_TYPE_P (intype
)
7364 && (TYPE_REF_IS_RVALUE (type
) || lvalue_p (expr
))
7365 && DERIVED_FROM_P (intype
, TREE_TYPE (type
))
7366 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype
)),
7367 build_pointer_type (TYPE_MAIN_VARIANT
7368 (TREE_TYPE (type
))),
7371 || at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
7375 if (processing_template_decl
)
7378 /* There is a standard conversion from "D*" to "B*" even if "B"
7379 is ambiguous or inaccessible. If this is really a
7380 static_cast, then we check both for inaccessibility and
7381 ambiguity. However, if this is a static_cast being performed
7382 because the user wrote a C-style cast, then accessibility is
7384 base
= lookup_base (TREE_TYPE (type
), intype
,
7385 c_cast_p
? ba_unique
: ba_check
,
7387 expr
= cp_build_addr_expr (expr
, complain
);
7389 if (sanitize_flags_p (SANITIZE_VPTR
))
7392 = cp_ubsan_maybe_instrument_downcast (loc
, type
,
7398 /* Convert from "B*" to "D*". This function will check that "B"
7399 is not a virtual base of "D". Even if we don't have a guarantee
7400 that expr is NULL, if the static_cast is to a reference type,
7401 it is UB if it would be NULL, so omit the non-NULL check. */
7402 expr
= build_base_path (MINUS_EXPR
, expr
, base
,
7403 /*nonnull=*/flag_delete_null_pointer_checks
,
7406 /* Convert the pointer to a reference -- but then remember that
7407 there are no expressions with reference type in C++.
7409 We call rvalue so that there's an actual tree code
7410 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
7411 is a variable with the same type, the conversion would get folded
7412 away, leaving just the variable and causing lvalue_kind to give
7413 the wrong answer. */
7414 expr
= cp_fold_convert (type
, expr
);
7416 /* When -fsanitize=null, make sure to diagnose reference binding to
7417 NULL even when the reference is converted to pointer later on. */
7418 if (sanitize_flags_p (SANITIZE_NULL
)
7419 && TREE_CODE (expr
) == COND_EXPR
7420 && TREE_OPERAND (expr
, 2)
7421 && TREE_CODE (TREE_OPERAND (expr
, 2)) == INTEGER_CST
7422 && TREE_TYPE (TREE_OPERAND (expr
, 2)) == type
)
7423 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr
, 2));
7425 return convert_from_reference (rvalue (expr
));
7428 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
7429 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
7430 if (TYPE_REF_P (type
)
7431 && TYPE_REF_IS_RVALUE (type
)
7432 && (clk
= real_lvalue_p (expr
))
7433 && reference_compatible_p (TREE_TYPE (type
), intype
)
7434 && (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
7436 if (processing_template_decl
)
7438 if (clk
== clk_ordinary
)
7440 /* Handle the (non-bit-field) lvalue case here by casting to
7441 lvalue reference and then changing it to an rvalue reference.
7442 Casting an xvalue to rvalue reference will be handled by the
7444 tree lref
= cp_build_reference_type (TREE_TYPE (type
), false);
7445 result
= (perform_direct_initialization_if_possible
7446 (lref
, expr
, c_cast_p
, complain
));
7447 result
= build1 (NON_LVALUE_EXPR
, type
, result
);
7448 return convert_from_reference (result
);
7451 /* For a bit-field or packed field, bind to a temporary. */
7452 expr
= rvalue (expr
);
7455 /* Resolve overloaded address here rather than once in
7456 implicit_conversion and again in the inverse code below. */
7457 if (TYPE_PTRMEMFUNC_P (type
) && type_unknown_p (expr
))
7459 expr
= instantiate_type (type
, expr
, complain
);
7460 intype
= TREE_TYPE (expr
);
7463 /* [expr.static.cast]
7465 Any expression can be explicitly converted to type cv void. */
7466 if (VOID_TYPE_P (type
))
7467 return convert_to_void (expr
, ICV_CAST
, complain
);
7470 An abstract class shall not be used ... as the type of an explicit
7472 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
7473 return error_mark_node
;
7475 /* [expr.static.cast]
7477 An expression e can be explicitly converted to a type T using a
7478 static_cast of the form static_cast<T>(e) if the declaration T
7479 t(e);" is well-formed, for some invented temporary variable
7481 result
= perform_direct_initialization_if_possible (type
, expr
,
7482 c_cast_p
, complain
);
7483 /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
7484 which initialize the first element of the aggregate. We need to handle
7485 the array case specifically. */
7486 if (result
== NULL_TREE
7487 && cxx_dialect
>= cxx20
7488 && TREE_CODE (type
) == ARRAY_TYPE
)
7490 /* Create { EXPR } and perform direct-initialization from it. */
7491 tree e
= build_constructor_single (init_list_type_node
, NULL_TREE
, expr
);
7492 CONSTRUCTOR_IS_DIRECT_INIT (e
) = true;
7493 CONSTRUCTOR_IS_PAREN_INIT (e
) = true;
7494 result
= perform_direct_initialization_if_possible (type
, e
, c_cast_p
,
7499 if (processing_template_decl
)
7502 result
= convert_from_reference (result
);
7504 /* [expr.static.cast]
7506 If T is a reference type, the result is an lvalue; otherwise,
7507 the result is an rvalue. */
7508 if (!TYPE_REF_P (type
))
7510 result
= rvalue (result
);
7512 if (result
== expr
&& SCALAR_TYPE_P (type
))
7513 /* Leave some record of the cast. */
7514 result
= build_nop (type
, expr
);
7519 /* [expr.static.cast]
7521 The inverse of any standard conversion sequence (clause _conv_),
7522 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
7523 (_conv.array_), function-to-pointer (_conv.func_), and boolean
7524 (_conv.bool_) conversions, can be performed explicitly using
7525 static_cast subject to the restriction that the explicit
7526 conversion does not cast away constness (_expr.const.cast_), and
7527 the following additional rules for specific cases: */
7528 /* For reference, the conversions not excluded are: integral
7529 promotions, floating-point promotion, integral conversions,
7530 floating-point conversions, floating-integral conversions,
7531 pointer conversions, and pointer to member conversions. */
7534 A value of integral _or enumeration_ type can be explicitly
7535 converted to an enumeration type. */
7536 /* The effect of all that is that any conversion between any two
7537 types which are integral, floating, or enumeration types can be
7539 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7540 || SCALAR_FLOAT_TYPE_P (type
))
7541 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
7542 || SCALAR_FLOAT_TYPE_P (intype
)))
7544 if (processing_template_decl
)
7546 return ocp_convert (type
, expr
, CONV_C_CAST
, LOOKUP_NORMAL
, complain
);
7549 if (TYPE_PTR_P (type
) && TYPE_PTR_P (intype
)
7550 && CLASS_TYPE_P (TREE_TYPE (type
))
7551 && CLASS_TYPE_P (TREE_TYPE (intype
))
7552 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7553 (TREE_TYPE (intype
))),
7554 build_pointer_type (TYPE_MAIN_VARIANT
7555 (TREE_TYPE (type
))),
7560 if (processing_template_decl
)
7564 && check_for_casting_away_constness (loc
, intype
, type
,
7567 return error_mark_node
;
7568 base
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
7569 c_cast_p
? ba_unique
: ba_check
,
7571 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
7574 if (sanitize_flags_p (SANITIZE_VPTR
))
7577 = cp_ubsan_maybe_instrument_downcast (loc
, type
,
7583 return cp_fold_convert (type
, expr
);
7586 if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
7587 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
7594 c1
= TYPE_PTRMEM_CLASS_TYPE (intype
);
7595 c2
= TYPE_PTRMEM_CLASS_TYPE (type
);
7597 if (TYPE_PTRDATAMEM_P (type
))
7599 t1
= (build_ptrmem_type
7601 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype
))));
7602 t2
= (build_ptrmem_type
7604 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
7611 if (can_convert (t1
, t2
, complain
) || can_convert (t2
, t1
, complain
))
7614 && check_for_casting_away_constness (loc
, intype
, type
,
7617 return error_mark_node
;
7618 if (processing_template_decl
)
7620 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/1,
7621 c_cast_p
, complain
);
7625 /* [expr.static.cast]
7627 An rvalue of type "pointer to cv void" can be explicitly
7628 converted to a pointer to object type. A value of type pointer
7629 to object converted to "pointer to cv void" and back to the
7630 original pointer type will have its original value. */
7631 if (TYPE_PTR_P (intype
)
7632 && VOID_TYPE_P (TREE_TYPE (intype
))
7633 && TYPE_PTROB_P (type
))
7636 && check_for_casting_away_constness (loc
, intype
, type
,
7639 return error_mark_node
;
7640 if (processing_template_decl
)
7642 return build_nop (type
, expr
);
7646 return error_mark_node
;
7649 /* Return an expression representing static_cast<TYPE>(EXPR). */
7652 build_static_cast (location_t loc
, tree type
, tree oexpr
,
7653 tsubst_flags_t complain
)
7659 if (type
== error_mark_node
|| expr
== error_mark_node
)
7660 return error_mark_node
;
7662 bool dependent
= (dependent_type_p (type
)
7663 || type_dependent_expression_p (expr
));
7667 expr
= build_min (STATIC_CAST_EXPR
, type
, oexpr
);
7668 /* We don't know if it will or will not have side effects. */
7669 TREE_SIDE_EFFECTS (expr
) = 1;
7670 result
= convert_from_reference (expr
);
7671 protected_set_expr_location (result
, loc
);
7674 else if (processing_template_decl
)
7675 expr
= build_non_dependent_expr (expr
);
7677 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7678 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7679 if (!TYPE_REF_P (type
)
7680 && TREE_CODE (expr
) == NOP_EXPR
7681 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7682 expr
= TREE_OPERAND (expr
, 0);
7684 result
= build_static_cast_1 (loc
, type
, expr
, /*c_cast_p=*/false,
7685 &valid_p
, complain
);
7688 if (result
!= error_mark_node
)
7690 maybe_warn_about_useless_cast (loc
, type
, expr
, complain
);
7691 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
7693 if (processing_template_decl
)
7695 protected_set_expr_location (result
, loc
);
7699 if (complain
& tf_error
)
7701 error_at (loc
, "invalid %<static_cast%> from type %qT to type %qT",
7702 TREE_TYPE (expr
), type
);
7703 if ((TYPE_PTR_P (type
) || TYPE_REF_P (type
))
7704 && CLASS_TYPE_P (TREE_TYPE (type
))
7705 && !COMPLETE_TYPE_P (TREE_TYPE (type
)))
7706 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type
))),
7707 "class type %qT is incomplete", TREE_TYPE (type
));
7708 tree expr_type
= TREE_TYPE (expr
);
7709 if (TYPE_PTR_P (expr_type
))
7710 expr_type
= TREE_TYPE (expr_type
);
7711 if (CLASS_TYPE_P (expr_type
) && !COMPLETE_TYPE_P (expr_type
))
7712 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type
)),
7713 "class type %qT is incomplete", expr_type
);
7715 return error_mark_node
;
7718 /* EXPR is an expression with member function or pointer-to-member
7719 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7720 not permitted by ISO C++, but we accept it in some modes. If we
7721 are not in one of those modes, issue a diagnostic. Return the
7722 converted expression. */
7725 convert_member_func_to_ptr (tree type
, tree expr
, tsubst_flags_t complain
)
7730 intype
= TREE_TYPE (expr
);
7731 gcc_assert (TYPE_PTRMEMFUNC_P (intype
)
7732 || TREE_CODE (intype
) == METHOD_TYPE
);
7734 if (!(complain
& tf_warning_or_error
))
7735 return error_mark_node
;
7737 if (pedantic
|| warn_pmf2ptr
)
7738 pedwarn (input_location
, pedantic
? OPT_Wpedantic
: OPT_Wpmf_conversions
,
7739 "converting from %qH to %qI", intype
, type
);
7741 if (TREE_CODE (intype
) == METHOD_TYPE
)
7742 expr
= build_addr_func (expr
, complain
);
7743 else if (TREE_CODE (expr
) == PTRMEM_CST
)
7744 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
7747 decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
), 0);
7748 decl
= build_address (decl
);
7749 expr
= get_member_function_from_ptrfunc (&decl
, expr
, complain
);
7752 if (expr
== error_mark_node
)
7753 return error_mark_node
;
7755 return build_nop (type
, expr
);
7758 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
7759 constexpr evaluation knows to reject it. */
7762 build_nop_reinterpret (tree type
, tree expr
)
7764 tree ret
= build_nop (type
, expr
);
7766 REINTERPRET_CAST_P (ret
) = true;
7770 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7771 If C_CAST_P is true, this reinterpret cast is being done as part of
7772 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7773 indicate whether or not reinterpret_cast was valid. */
7776 build_reinterpret_cast_1 (location_t loc
, tree type
, tree expr
,
7777 bool c_cast_p
, bool *valid_p
,
7778 tsubst_flags_t complain
)
7782 /* Assume the cast is invalid. */
7786 if (type
== error_mark_node
|| error_operand_p (expr
))
7787 return error_mark_node
;
7789 intype
= TREE_TYPE (expr
);
7791 /* Save casted types in the function's used types hash table. */
7792 used_types_insert (type
);
7794 /* A prvalue of non-class type is cv-unqualified. */
7795 if (!CLASS_TYPE_P (type
))
7796 type
= cv_unqualified (type
);
7798 /* [expr.reinterpret.cast]
7799 A glvalue expression of type T1 can be cast to the type
7800 "reference to T2" if an expression of type "pointer to T1" can be
7801 explicitly converted to the type "pointer to T2" using a
7802 reinterpret_cast. */
7803 if (TYPE_REF_P (type
))
7805 if (TYPE_REF_IS_RVALUE (type
) && !VOID_TYPE_P (intype
))
7807 if (!obvalue_p (expr
))
7808 /* Perform the temporary materialization conversion. */
7809 expr
= get_target_expr_sfinae (expr
, complain
);
7811 else if (!lvalue_p (expr
))
7813 if (complain
& tf_error
)
7814 error_at (loc
, "invalid cast of an rvalue expression of type "
7817 return error_mark_node
;
7820 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7821 "B" are related class types; the reinterpret_cast does not
7822 adjust the pointer. */
7823 if (TYPE_PTR_P (intype
)
7824 && (complain
& tf_warning
)
7825 && (comptypes (TREE_TYPE (intype
), TREE_TYPE (type
),
7826 COMPARE_BASE
| COMPARE_DERIVED
)))
7827 warning_at (loc
, 0, "casting %qT to %qT does not dereference pointer",
7830 expr
= cp_build_addr_expr (expr
, complain
);
7832 if (warn_strict_aliasing
> 2)
7833 cp_strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
7835 if (expr
!= error_mark_node
)
7836 expr
= build_reinterpret_cast_1
7837 (loc
, build_pointer_type (TREE_TYPE (type
)), expr
, c_cast_p
,
7839 if (expr
!= error_mark_node
)
7840 /* cp_build_indirect_ref isn't right for rvalue refs. */
7841 expr
= convert_from_reference (fold_convert (type
, expr
));
7845 /* As a G++ extension, we consider conversions from member
7846 functions, and pointers to member functions to
7847 pointer-to-function and pointer-to-void types. If
7848 -Wno-pmf-conversions has not been specified,
7849 convert_member_func_to_ptr will issue an error message. */
7850 if ((TYPE_PTRMEMFUNC_P (intype
)
7851 || TREE_CODE (intype
) == METHOD_TYPE
)
7852 && TYPE_PTR_P (type
)
7853 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
7854 || VOID_TYPE_P (TREE_TYPE (type
))))
7855 return convert_member_func_to_ptr (type
, expr
, complain
);
7857 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7858 array-to-pointer, and function-to-pointer conversions are
7860 expr
= decay_conversion (expr
, complain
);
7862 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7863 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7864 if (TREE_CODE (expr
) == NOP_EXPR
7865 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7866 expr
= TREE_OPERAND (expr
, 0);
7868 if (error_operand_p (expr
))
7869 return error_mark_node
;
7871 intype
= TREE_TYPE (expr
);
7873 /* [expr.reinterpret.cast]
7874 A pointer can be converted to any integral type large enough to
7875 hold it. ... A value of type std::nullptr_t can be converted to
7876 an integral type; the conversion has the same meaning and
7877 validity as a conversion of (void*)0 to the integral type. */
7878 if (CP_INTEGRAL_TYPE_P (type
)
7879 && (TYPE_PTR_P (intype
) || NULLPTR_TYPE_P (intype
)))
7881 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
7883 if (complain
& tf_error
)
7884 permerror (loc
, "cast from %qH to %qI loses precision",
7887 return error_mark_node
;
7889 if (NULLPTR_TYPE_P (intype
))
7890 return build_int_cst (type
, 0);
7892 /* [expr.reinterpret.cast]
7893 A value of integral or enumeration type can be explicitly
7894 converted to a pointer. */
7895 else if (TYPE_PTR_P (type
) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype
))
7898 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7899 || TYPE_PTR_OR_PTRMEM_P (type
))
7900 && same_type_p (type
, intype
))
7902 return rvalue (expr
);
7903 else if (TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
7905 if ((complain
& tf_warning
)
7906 && !cxx_safe_function_type_cast_p (TREE_TYPE (type
),
7907 TREE_TYPE (intype
)))
7908 warning_at (loc
, OPT_Wcast_function_type
,
7909 "cast between incompatible function types"
7910 " from %qH to %qI", intype
, type
);
7911 return build_nop_reinterpret (type
, expr
);
7913 else if (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
))
7915 if ((complain
& tf_warning
)
7916 && !cxx_safe_function_type_cast_p
7917 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type
)),
7918 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype
))))
7919 warning_at (loc
, OPT_Wcast_function_type
,
7920 "cast between incompatible pointer to member types"
7921 " from %qH to %qI", intype
, type
);
7922 return build_nop_reinterpret (type
, expr
);
7924 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
7925 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
7928 && check_for_casting_away_constness (loc
, intype
, type
,
7929 REINTERPRET_CAST_EXPR
,
7931 return error_mark_node
;
7932 /* Warn about possible alignment problems. */
7933 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
7934 && (complain
& tf_warning
)
7935 && !VOID_TYPE_P (type
)
7936 && TREE_CODE (TREE_TYPE (intype
)) != FUNCTION_TYPE
7937 && COMPLETE_TYPE_P (TREE_TYPE (type
))
7938 && COMPLETE_TYPE_P (TREE_TYPE (intype
))
7939 && min_align_of_type (TREE_TYPE (type
))
7940 > min_align_of_type (TREE_TYPE (intype
)))
7941 warning_at (loc
, OPT_Wcast_align
, "cast from %qH to %qI "
7942 "increases required alignment of target type",
7945 if (warn_strict_aliasing
<= 2)
7946 /* strict_aliasing_warning STRIP_NOPs its expr. */
7947 cp_strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
7949 return build_nop_reinterpret (type
, expr
);
7951 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
7952 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
7954 if (complain
& tf_warning
)
7955 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7956 object pointer type or vice versa is conditionally-supported." */
7957 warning_at (loc
, OPT_Wconditionally_supported
,
7958 "casting between pointer-to-function and "
7959 "pointer-to-object is conditionally-supported");
7960 return build_nop_reinterpret (type
, expr
);
7962 else if (gnu_vector_type_p (type
))
7963 return convert_to_vector (type
, rvalue (expr
));
7964 else if (gnu_vector_type_p (intype
)
7965 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
7966 return convert_to_integer_nofold (type
, expr
);
7971 if (complain
& tf_error
)
7972 error_at (loc
, "invalid cast from type %qT to type %qT",
7974 return error_mark_node
;
7977 expr
= cp_convert (type
, expr
, complain
);
7978 if (TREE_CODE (expr
) == NOP_EXPR
)
7979 /* Mark any nop_expr that created as a reintepret_cast. */
7980 REINTERPRET_CAST_P (expr
) = true;
7985 build_reinterpret_cast (location_t loc
, tree type
, tree expr
,
7986 tsubst_flags_t complain
)
7990 if (type
== error_mark_node
|| expr
== error_mark_node
)
7991 return error_mark_node
;
7993 if (processing_template_decl
)
7995 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
7997 if (!TREE_SIDE_EFFECTS (t
)
7998 && type_dependent_expression_p (expr
))
7999 /* There might turn out to be side effects inside expr. */
8000 TREE_SIDE_EFFECTS (t
) = 1;
8001 r
= convert_from_reference (t
);
8002 protected_set_expr_location (r
, loc
);
8006 r
= build_reinterpret_cast_1 (loc
, type
, expr
, /*c_cast_p=*/false,
8007 /*valid_p=*/NULL
, complain
);
8008 if (r
!= error_mark_node
)
8010 maybe_warn_about_useless_cast (loc
, type
, expr
, complain
);
8011 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
8013 protected_set_expr_location (r
, loc
);
8017 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
8018 return an appropriate expression. Otherwise, return
8019 error_mark_node. If the cast is not valid, and COMPLAIN is true,
8020 then a diagnostic will be issued. If VALID_P is non-NULL, we are
8021 performing a C-style cast, its value upon return will indicate
8022 whether or not the conversion succeeded. */
8025 build_const_cast_1 (location_t loc
, tree dst_type
, tree expr
,
8026 tsubst_flags_t complain
, bool *valid_p
)
8029 tree reference_type
;
8031 /* Callers are responsible for handling error_mark_node as a
8032 destination type. */
8033 gcc_assert (dst_type
!= error_mark_node
);
8034 /* In a template, callers should be building syntactic
8035 representations of casts, not using this machinery. */
8036 gcc_assert (!processing_template_decl
);
8038 /* Assume the conversion is invalid. */
8042 if (!INDIRECT_TYPE_P (dst_type
) && !TYPE_PTRDATAMEM_P (dst_type
))
8044 if (complain
& tf_error
)
8045 error_at (loc
, "invalid use of %<const_cast%> with type %qT, "
8046 "which is not a pointer, reference, "
8047 "nor a pointer-to-data-member type", dst_type
);
8048 return error_mark_node
;
8051 if (TREE_CODE (TREE_TYPE (dst_type
)) == FUNCTION_TYPE
)
8053 if (complain
& tf_error
)
8054 error_at (loc
, "invalid use of %<const_cast%> with type %qT, "
8055 "which is a pointer or reference to a function type",
8057 return error_mark_node
;
8060 /* A prvalue of non-class type is cv-unqualified. */
8061 dst_type
= cv_unqualified (dst_type
);
8063 /* Save casted types in the function's used types hash table. */
8064 used_types_insert (dst_type
);
8066 src_type
= TREE_TYPE (expr
);
8067 /* Expressions do not really have reference types. */
8068 if (TYPE_REF_P (src_type
))
8069 src_type
= TREE_TYPE (src_type
);
8071 /* [expr.const.cast]
8073 For two object types T1 and T2, if a pointer to T1 can be explicitly
8074 converted to the type "pointer to T2" using a const_cast, then the
8075 following conversions can also be made:
8077 -- an lvalue of type T1 can be explicitly converted to an lvalue of
8078 type T2 using the cast const_cast<T2&>;
8080 -- a glvalue of type T1 can be explicitly converted to an xvalue of
8081 type T2 using the cast const_cast<T2&&>; and
8083 -- if T1 is a class type, a prvalue of type T1 can be explicitly
8084 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
8086 if (TYPE_REF_P (dst_type
))
8088 reference_type
= dst_type
;
8089 if (!TYPE_REF_IS_RVALUE (dst_type
)
8095 if (complain
& tf_error
)
8096 error_at (loc
, "invalid %<const_cast%> of an rvalue of type %qT "
8098 src_type
, dst_type
);
8099 return error_mark_node
;
8101 dst_type
= build_pointer_type (TREE_TYPE (dst_type
));
8102 src_type
= build_pointer_type (src_type
);
8106 reference_type
= NULL_TREE
;
8107 /* If the destination type is not a reference type, the
8108 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
8109 conversions are performed. */
8110 src_type
= type_decays_to (src_type
);
8111 if (src_type
== error_mark_node
)
8112 return error_mark_node
;
8115 if (TYPE_PTR_P (src_type
) || TYPE_PTRDATAMEM_P (src_type
))
8117 if (comp_ptr_ttypes_const (dst_type
, src_type
, bounds_none
))
8122 /* This cast is actually a C-style cast. Issue a warning if
8123 the user is making a potentially unsafe cast. */
8124 check_for_casting_away_constness (loc
, src_type
, dst_type
,
8125 CAST_EXPR
, complain
);
8126 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
8127 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
8128 && (complain
& tf_warning
)
8129 && min_align_of_type (TREE_TYPE (dst_type
))
8130 > min_align_of_type (TREE_TYPE (src_type
)))
8131 warning_at (loc
, OPT_Wcast_align
, "cast from %qH to %qI "
8132 "increases required alignment of target type",
8133 src_type
, dst_type
);
8137 expr
= cp_build_addr_expr (expr
, complain
);
8138 if (expr
== error_mark_node
)
8139 return error_mark_node
;
8140 expr
= build_nop (reference_type
, expr
);
8141 return convert_from_reference (expr
);
8145 expr
= decay_conversion (expr
, complain
);
8146 if (expr
== error_mark_node
)
8147 return error_mark_node
;
8149 /* build_c_cast puts on a NOP_EXPR to make the result not an
8150 lvalue. Strip such NOP_EXPRs if VALUE is being used in
8151 non-lvalue context. */
8152 if (TREE_CODE (expr
) == NOP_EXPR
8153 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
8154 expr
= TREE_OPERAND (expr
, 0);
8155 return build_nop (dst_type
, expr
);
8159 && !at_least_as_qualified_p (TREE_TYPE (dst_type
),
8160 TREE_TYPE (src_type
)))
8161 check_for_casting_away_constness (loc
, src_type
, dst_type
,
8162 CAST_EXPR
, complain
);
8165 if (complain
& tf_error
)
8166 error_at (loc
, "invalid %<const_cast%> from type %qT to type %qT",
8167 src_type
, dst_type
);
8168 return error_mark_node
;
8172 build_const_cast (location_t loc
, tree type
, tree expr
,
8173 tsubst_flags_t complain
)
8177 if (type
== error_mark_node
|| error_operand_p (expr
))
8178 return error_mark_node
;
8180 if (processing_template_decl
)
8182 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
8184 if (!TREE_SIDE_EFFECTS (t
)
8185 && type_dependent_expression_p (expr
))
8186 /* There might turn out to be side effects inside expr. */
8187 TREE_SIDE_EFFECTS (t
) = 1;
8188 r
= convert_from_reference (t
);
8189 protected_set_expr_location (r
, loc
);
8193 r
= build_const_cast_1 (loc
, type
, expr
, complain
, /*valid_p=*/NULL
);
8194 if (r
!= error_mark_node
)
8196 maybe_warn_about_useless_cast (loc
, type
, expr
, complain
);
8197 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
8199 protected_set_expr_location (r
, loc
);
8203 /* Like cp_build_c_cast, but for the c-common bits. */
8206 build_c_cast (location_t loc
, tree type
, tree expr
)
8208 return cp_build_c_cast (loc
, type
, expr
, tf_warning_or_error
);
8211 /* Like the "build_c_cast" used for c-common, but using cp_expr to
8212 preserve location information even for tree nodes that don't
8216 build_c_cast (location_t loc
, tree type
, cp_expr expr
)
8218 cp_expr result
= cp_build_c_cast (loc
, type
, expr
, tf_warning_or_error
);
8219 result
.set_location (loc
);
8223 /* Build an expression representing an explicit C-style cast to type
8224 TYPE of expression EXPR. */
8227 cp_build_c_cast (location_t loc
, tree type
, tree expr
,
8228 tsubst_flags_t complain
)
8234 if (type
== error_mark_node
|| error_operand_p (expr
))
8235 return error_mark_node
;
8237 if (processing_template_decl
)
8239 tree t
= build_min (CAST_EXPR
, type
,
8240 tree_cons (NULL_TREE
, value
, NULL_TREE
));
8241 /* We don't know if it will or will not have side effects. */
8242 TREE_SIDE_EFFECTS (t
) = 1;
8243 return convert_from_reference (t
);
8246 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
8247 'Class') should always be retained, because this information aids
8248 in method lookup. */
8249 if (objc_is_object_ptr (type
)
8250 && objc_is_object_ptr (TREE_TYPE (expr
)))
8251 return build_nop (type
, expr
);
8253 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8254 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8255 if (!TYPE_REF_P (type
)
8256 && TREE_CODE (value
) == NOP_EXPR
8257 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
8258 value
= TREE_OPERAND (value
, 0);
8260 if (TREE_CODE (type
) == ARRAY_TYPE
)
8262 /* Allow casting from T1* to T2[] because Cfront allows it.
8263 NIHCL uses it. It is not valid ISO C++ however. */
8264 if (TYPE_PTR_P (TREE_TYPE (expr
)))
8266 if (complain
& tf_error
)
8267 permerror (loc
, "ISO C++ forbids casting to an array type %qT",
8270 return error_mark_node
;
8271 type
= build_pointer_type (TREE_TYPE (type
));
8275 if (complain
& tf_error
)
8276 error_at (loc
, "ISO C++ forbids casting to an array type %qT",
8278 return error_mark_node
;
8282 if (FUNC_OR_METHOD_TYPE_P (type
))
8284 if (complain
& tf_error
)
8285 error_at (loc
, "invalid cast to function type %qT", type
);
8286 return error_mark_node
;
8289 if (TYPE_PTR_P (type
)
8290 && TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
8291 /* Casting to an integer of smaller size is an error detected elsewhere. */
8292 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (value
))
8293 /* Don't warn about converting any constant. */
8294 && !TREE_CONSTANT (value
))
8295 warning_at (loc
, OPT_Wint_to_pointer_cast
,
8296 "cast to pointer from integer of different size");
8298 /* A C-style cast can be a const_cast. */
8299 result
= build_const_cast_1 (loc
, type
, value
, complain
& tf_warning
,
8303 if (result
!= error_mark_node
)
8305 maybe_warn_about_useless_cast (loc
, type
, value
, complain
);
8306 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
8311 /* Or a static cast. */
8312 result
= build_static_cast_1 (loc
, type
, value
, /*c_cast_p=*/true,
8313 &valid_p
, complain
);
8314 /* Or a reinterpret_cast. */
8316 result
= build_reinterpret_cast_1 (loc
, type
, value
, /*c_cast_p=*/true,
8317 &valid_p
, complain
);
8318 /* The static_cast or reinterpret_cast may be followed by a
8321 /* A valid cast may result in errors if, for example, a
8322 conversion to an ambiguous base class is required. */
8323 && !error_operand_p (result
))
8327 maybe_warn_about_useless_cast (loc
, type
, value
, complain
);
8328 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
8330 /* Non-class rvalues always have cv-unqualified type. */
8331 if (!CLASS_TYPE_P (type
))
8332 type
= TYPE_MAIN_VARIANT (type
);
8333 result_type
= TREE_TYPE (result
);
8334 if (!CLASS_TYPE_P (result_type
) && !TYPE_REF_P (type
))
8335 result_type
= TYPE_MAIN_VARIANT (result_type
);
8336 /* If the type of RESULT does not match TYPE, perform a
8337 const_cast to make it match. If the static_cast or
8338 reinterpret_cast succeeded, we will differ by at most
8339 cv-qualification, so the follow-on const_cast is guaranteed
8341 if (!same_type_p (non_reference (type
), non_reference (result_type
)))
8343 result
= build_const_cast_1 (loc
, type
, result
, false, &valid_p
);
8344 gcc_assert (valid_p
);
8349 return error_mark_node
;
8352 /* For use from the C common bits. */
8354 build_modify_expr (location_t location
,
8355 tree lhs
, tree
/*lhs_origtype*/,
8356 enum tree_code modifycode
,
8357 location_t
/*rhs_location*/, tree rhs
,
8358 tree
/*rhs_origtype*/)
8360 return cp_build_modify_expr (location
, lhs
, modifycode
, rhs
,
8361 tf_warning_or_error
);
8364 /* Build an assignment expression of lvalue LHS from value RHS.
8365 MODIFYCODE is the code for a binary operator that we use
8366 to combine the old value of LHS with RHS to get the new value.
8367 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
8369 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
8372 cp_build_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
8373 tree rhs
, tsubst_flags_t complain
)
8375 lhs
= mark_lvalue_use_nonread (lhs
);
8377 tree result
= NULL_TREE
;
8379 tree lhstype
= TREE_TYPE (lhs
);
8381 tree olhstype
= lhstype
;
8382 bool plain_assign
= (modifycode
== NOP_EXPR
);
8383 bool compound_side_effects_p
= false;
8384 tree preeval
= NULL_TREE
;
8386 /* Avoid duplicate error messages from operands that had errors. */
8387 if (error_operand_p (lhs
) || error_operand_p (rhs
))
8388 return error_mark_node
;
8390 while (TREE_CODE (lhs
) == COMPOUND_EXPR
)
8392 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
8393 compound_side_effects_p
= true;
8394 lhs
= TREE_OPERAND (lhs
, 1);
8397 /* Handle control structure constructs used as "lvalues". Note that we
8398 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
8399 switch (TREE_CODE (lhs
))
8401 /* Handle --foo = 5; as these are valid constructs in C++. */
8402 case PREDECREMENT_EXPR
:
8403 case PREINCREMENT_EXPR
:
8404 if (compound_side_effects_p
)
8405 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
8406 lhs
= genericize_compound_lvalue (lhs
);
8408 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
8409 and looked through the COMPOUND_EXPRs, readd them now around
8410 the resulting lhs. */
8411 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
8413 lhs
= build2 (COMPOUND_EXPR
, lhstype
, TREE_OPERAND (olhs
, 0), lhs
);
8414 tree
*ptr
= &TREE_OPERAND (lhs
, 1);
8415 for (olhs
= TREE_OPERAND (olhs
, 1);
8416 TREE_CODE (olhs
) == COMPOUND_EXPR
;
8417 olhs
= TREE_OPERAND (olhs
, 1))
8419 *ptr
= build2 (COMPOUND_EXPR
, lhstype
,
8420 TREE_OPERAND (olhs
, 0), *ptr
);
8421 ptr
= &TREE_OPERAND (*ptr
, 1);
8427 if (compound_side_effects_p
)
8428 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
8429 lhs
= genericize_compound_lvalue (lhs
);
8430 goto maybe_add_compound
;
8434 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
8435 when neither operand has side-effects. */
8436 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8437 return error_mark_node
;
8439 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0))
8440 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 1)));
8442 lhs
= build3 (COND_EXPR
, TREE_TYPE (lhs
),
8443 build2 (TREE_CODE (lhs
) == MIN_EXPR
? LE_EXPR
: GE_EXPR
,
8445 TREE_OPERAND (lhs
, 0),
8446 TREE_OPERAND (lhs
, 1)),
8447 TREE_OPERAND (lhs
, 0),
8448 TREE_OPERAND (lhs
, 1));
8451 /* Handle (a ? b : c) used as an "lvalue". */
8454 /* Produce (a ? (b = rhs) : (c = rhs))
8455 except that the RHS goes through a save-expr
8456 so the code to compute it is only emitted once. */
8457 if (VOID_TYPE_P (TREE_TYPE (rhs
)))
8459 if (complain
& tf_error
)
8460 error_at (cp_expr_loc_or_loc (rhs
, loc
),
8461 "void value not ignored as it ought to be");
8462 return error_mark_node
;
8465 rhs
= stabilize_expr (rhs
, &preeval
);
8467 /* Check this here to avoid odd errors when trying to convert
8468 a throw to the type of the COND_EXPR. */
8469 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8470 return error_mark_node
;
8472 tree op1
= TREE_OPERAND (lhs
, 1);
8473 if (TREE_CODE (op1
) != THROW_EXPR
)
8474 op1
= cp_build_modify_expr (loc
, op1
, modifycode
, rhs
, complain
);
8475 /* When sanitizing undefined behavior, even when rhs doesn't need
8476 stabilization at this point, the sanitization might add extra
8477 SAVE_EXPRs in there and so make sure there is no tree sharing
8478 in the rhs, otherwise those SAVE_EXPRs will have initialization
8479 only in one of the two branches. */
8480 if (sanitize_flags_p (SANITIZE_UNDEFINED
8481 | SANITIZE_UNDEFINED_NONDEFAULT
))
8482 rhs
= unshare_expr (rhs
);
8483 tree op2
= TREE_OPERAND (lhs
, 2);
8484 if (TREE_CODE (op2
) != THROW_EXPR
)
8485 op2
= cp_build_modify_expr (loc
, op2
, modifycode
, rhs
, complain
);
8486 tree cond
= build_conditional_expr (input_location
,
8487 TREE_OPERAND (lhs
, 0), op1
, op2
,
8490 if (cond
== error_mark_node
)
8492 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
8493 and looked through the COMPOUND_EXPRs, readd them now around
8494 the resulting cond before adding the preevaluated rhs. */
8495 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
8497 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
8498 TREE_OPERAND (olhs
, 0), cond
);
8499 tree
*ptr
= &TREE_OPERAND (cond
, 1);
8500 for (olhs
= TREE_OPERAND (olhs
, 1);
8501 TREE_CODE (olhs
) == COMPOUND_EXPR
;
8502 olhs
= TREE_OPERAND (olhs
, 1))
8504 *ptr
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
8505 TREE_OPERAND (olhs
, 0), *ptr
);
8506 ptr
= &TREE_OPERAND (*ptr
, 1);
8509 /* Make sure the code to compute the rhs comes out
8510 before the split. */
8520 if (modifycode
== INIT_EXPR
)
8522 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
8523 /* Do the default thing. */;
8524 else if (TREE_CODE (rhs
) == CONSTRUCTOR
)
8526 /* Compound literal. */
8527 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
8528 /* Call convert to generate an error; see PR 11063. */
8529 rhs
= convert (lhstype
, rhs
);
8530 result
= build2 (INIT_EXPR
, lhstype
, lhs
, rhs
);
8531 TREE_SIDE_EFFECTS (result
) = 1;
8534 else if (! MAYBE_CLASS_TYPE_P (lhstype
))
8535 /* Do the default thing. */;
8538 releasing_vec rhs_vec
= make_tree_vector_single (rhs
);
8539 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
8540 &rhs_vec
, lhstype
, LOOKUP_NORMAL
,
8542 if (result
== NULL_TREE
)
8543 return error_mark_node
;
8549 lhs
= require_complete_type_sfinae (lhs
, complain
);
8550 if (lhs
== error_mark_node
)
8551 return error_mark_node
;
8553 if (modifycode
== NOP_EXPR
)
8555 if (c_dialect_objc ())
8557 result
= objc_maybe_build_modify_expr (lhs
, rhs
);
8562 /* `operator=' is not an inheritable operator. */
8563 if (! MAYBE_CLASS_TYPE_P (lhstype
))
8564 /* Do the default thing. */;
8567 result
= build_new_op (input_location
, MODIFY_EXPR
,
8568 LOOKUP_NORMAL
, lhs
, rhs
,
8569 make_node (NOP_EXPR
), /*overload=*/NULL
,
8571 if (result
== NULL_TREE
)
8572 return error_mark_node
;
8579 tree init
= NULL_TREE
;
8581 /* A binary op has been requested. Combine the old LHS
8582 value with the RHS producing the value we should actually
8583 store into the LHS. */
8584 gcc_assert (!((TYPE_REF_P (lhstype
)
8585 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype
)))
8586 || MAYBE_CLASS_TYPE_P (lhstype
)));
8588 /* An expression of the form E1 op= E2. [expr.ass] says:
8589 "Such expressions are deprecated if E1 has volatile-qualified
8590 type." We warn here rather than in cp_genericize_r because
8591 for compound assignments we are supposed to warn even if the
8592 assignment is a discarded-value expression. */
8593 if (TREE_THIS_VOLATILE (lhs
) || CP_TYPE_VOLATILE_P (lhstype
))
8594 warning_at (loc
, OPT_Wvolatile
,
8595 "compound assignment with %<volatile%>-qualified left "
8596 "operand is deprecated");
8597 /* Preevaluate the RHS to make sure its evaluation is complete
8598 before the lvalue-to-rvalue conversion of the LHS:
8600 [expr.ass] With respect to an indeterminately-sequenced
8601 function call, the operation of a compound assignment is a
8602 single evaluation. [ Note: Therefore, a function call shall
8603 not intervene between the lvalue-to-rvalue conversion and the
8604 side effect associated with any single compound assignment
8605 operator. -- end note ] */
8606 lhs
= cp_stabilize_reference (lhs
);
8607 rhs
= decay_conversion (rhs
, complain
);
8608 if (rhs
== error_mark_node
)
8609 return error_mark_node
;
8610 rhs
= stabilize_expr (rhs
, &init
);
8611 newrhs
= cp_build_binary_op (loc
, modifycode
, lhs
, rhs
, complain
);
8612 if (newrhs
== error_mark_node
)
8614 if (complain
& tf_error
)
8615 inform (loc
, " in evaluation of %<%Q(%#T, %#T)%>",
8616 modifycode
, TREE_TYPE (lhs
), TREE_TYPE (rhs
));
8617 return error_mark_node
;
8621 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), init
, newrhs
);
8623 /* Now it looks like a plain assignment. */
8624 modifycode
= NOP_EXPR
;
8625 if (c_dialect_objc ())
8627 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
8632 gcc_assert (!TYPE_REF_P (lhstype
));
8633 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs
)));
8636 /* The left-hand side must be an lvalue. */
8637 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8638 return error_mark_node
;
8640 /* Warn about modifying something that is `const'. Don't warn if
8641 this is initialization. */
8642 if (modifycode
!= INIT_EXPR
8643 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
8644 /* Functions are not modifiable, even though they are
8646 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs
))
8647 /* If it's an aggregate and any field is const, then it is
8648 effectively const. */
8649 || (CLASS_TYPE_P (lhstype
)
8650 && C_TYPE_FIELDS_READONLY (lhstype
))))
8652 if (complain
& tf_error
)
8653 cxx_readonly_error (loc
, lhs
, lv_assign
);
8654 return error_mark_node
;
8657 /* If storing into a structure or union member, it may have been given a
8658 lowered bitfield type. We need to convert to the declared type first,
8659 so retrieve it now. */
8661 olhstype
= unlowered_expr_type (lhs
);
8663 /* Convert new value to destination type. */
8665 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
8669 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs
))
8671 if (modifycode
!= INIT_EXPR
)
8673 if (complain
& tf_error
)
8675 "assigning to an array from an initializer list");
8676 return error_mark_node
;
8678 if (check_array_initializer (lhs
, lhstype
, newrhs
))
8679 return error_mark_node
;
8680 newrhs
= digest_init (lhstype
, newrhs
, complain
);
8681 if (newrhs
== error_mark_node
)
8682 return error_mark_node
;
8685 /* C++11 8.5/17: "If the destination type is an array of characters,
8686 an array of char16_t, an array of char32_t, or an array of wchar_t,
8687 and the initializer is a string literal...". */
8688 else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs
))
8690 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype
)))
8691 && modifycode
== INIT_EXPR
)
8693 newrhs
= digest_init (lhstype
, newrhs
, complain
);
8694 if (newrhs
== error_mark_node
)
8695 return error_mark_node
;
8698 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
8699 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))))
8701 if (complain
& tf_error
)
8702 error_at (loc
, "incompatible types in assignment of %qT to %qT",
8703 TREE_TYPE (rhs
), lhstype
);
8704 return error_mark_node
;
8707 /* Allow array assignment in compiler-generated code. */
8708 else if (!current_function_decl
8709 || !DECL_DEFAULTED_FN (current_function_decl
))
8711 /* This routine is used for both initialization and assignment.
8712 Make sure the diagnostic message differentiates the context. */
8713 if (complain
& tf_error
)
8715 if (modifycode
== INIT_EXPR
)
8716 error_at (loc
, "array used as initializer");
8718 error_at (loc
, "invalid array assignment");
8720 return error_mark_node
;
8723 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
8724 ? 1 + (modifycode
!= INIT_EXPR
): 0;
8725 result
= build_vec_init (lhs
, NULL_TREE
, newrhs
,
8726 /*explicit_value_init_p=*/false,
8727 from_array
, complain
);
8731 if (modifycode
== INIT_EXPR
)
8732 /* Calls with INIT_EXPR are all direct-initialization, so don't set
8733 LOOKUP_ONLYCONVERTING. */
8734 newrhs
= convert_for_initialization (lhs
, olhstype
, newrhs
, LOOKUP_NORMAL
,
8735 ICR_INIT
, NULL_TREE
, 0,
8738 newrhs
= convert_for_assignment (olhstype
, newrhs
, ICR_ASSIGN
,
8739 NULL_TREE
, 0, complain
, LOOKUP_IMPLICIT
);
8741 if (!same_type_p (lhstype
, olhstype
))
8742 newrhs
= cp_convert_and_check (lhstype
, newrhs
, complain
);
8744 if (modifycode
!= INIT_EXPR
)
8746 if (TREE_CODE (newrhs
) == CALL_EXPR
8747 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
8748 newrhs
= build_cplus_new (lhstype
, newrhs
, complain
);
8750 /* Can't initialize directly from a TARGET_EXPR, since that would
8751 cause the lhs to be constructed twice, and possibly result in
8752 accidental self-initialization. So we force the TARGET_EXPR to be
8753 expanded without a target. */
8754 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
8755 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
8756 TREE_OPERAND (newrhs
, 0));
8759 if (newrhs
== error_mark_node
)
8760 return error_mark_node
;
8762 if (c_dialect_objc () && flag_objc_gc
)
8764 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
8770 result
= build2_loc (loc
, modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
8771 lhstype
, lhs
, newrhs
);
8773 TREE_SIDE_EFFECTS (result
) = 1;
8775 TREE_NO_WARNING (result
) = 1;
8779 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), preeval
, result
);
8784 build_x_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
8785 tree rhs
, tsubst_flags_t complain
)
8787 tree orig_lhs
= lhs
;
8788 tree orig_rhs
= rhs
;
8789 tree overload
= NULL_TREE
;
8790 tree op
= build_nt (modifycode
, NULL_TREE
, NULL_TREE
);
8792 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
8793 return cp_expr (error_mark_node
, loc
);
8795 if (processing_template_decl
)
8797 if (modifycode
== NOP_EXPR
8798 || type_dependent_expression_p (lhs
)
8799 || type_dependent_expression_p (rhs
))
8800 return build_min_nt_loc (loc
, MODOP_EXPR
, lhs
,
8801 build_min_nt_loc (loc
, modifycode
, NULL_TREE
,
8804 lhs
= build_non_dependent_expr (lhs
);
8805 rhs
= build_non_dependent_expr (rhs
);
8808 if (modifycode
!= NOP_EXPR
)
8810 tree rval
= build_new_op (loc
, MODIFY_EXPR
, LOOKUP_NORMAL
,
8811 lhs
, rhs
, op
, &overload
, complain
);
8814 if (rval
== error_mark_node
)
8816 TREE_NO_WARNING (rval
) = 1;
8817 if (processing_template_decl
)
8819 if (overload
!= NULL_TREE
)
8820 return (build_min_non_dep_op_overload
8821 (MODIFY_EXPR
, rval
, overload
, orig_lhs
, orig_rhs
));
8823 return (build_min_non_dep
8824 (MODOP_EXPR
, rval
, orig_lhs
, op
, orig_rhs
));
8829 return cp_build_modify_expr (loc
, lhs
, modifycode
, rhs
, complain
);
8832 /* Helper function for get_delta_difference which assumes FROM is a base
8833 class of TO. Returns a delta for the conversion of pointer-to-member
8834 of FROM to pointer-to-member of TO. If the conversion is invalid and
8835 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8836 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8837 If C_CAST_P is true, this conversion is taking place as part of a
8841 get_delta_difference_1 (tree from
, tree to
, bool c_cast_p
,
8842 tsubst_flags_t complain
)
8847 binfo
= lookup_base (to
, from
, c_cast_p
? ba_unique
: ba_check
,
8850 if (binfo
== error_mark_node
)
8852 if (!(complain
& tf_error
))
8853 return error_mark_node
;
8855 inform (input_location
, " in pointer to member function conversion");
8856 return size_zero_node
;
8860 if (kind
!= bk_via_virtual
)
8861 return BINFO_OFFSET (binfo
);
8863 /* FROM is a virtual base class of TO. Issue an error or warning
8864 depending on whether or not this is a reinterpret cast. */
8866 if (!(complain
& tf_error
))
8867 return error_mark_node
;
8869 error ("pointer to member conversion via virtual base %qT",
8870 BINFO_TYPE (binfo_from_vbase (binfo
)));
8872 return size_zero_node
;
8879 /* Get difference in deltas for different pointer to member function
8880 types. If the conversion is invalid and tf_error is not set in
8881 COMPLAIN, returns error_mark_node, otherwise returns an integer
8882 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8883 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8884 conversions as well. If C_CAST_P is true this conversion is taking
8885 place as part of a C-style cast.
8887 Note that the naming of FROM and TO is kind of backwards; the return
8888 value is what we add to a TO in order to get a FROM. They are named
8889 this way because we call this function to find out how to convert from
8890 a pointer to member of FROM to a pointer to member of TO. */
8893 get_delta_difference (tree from
, tree to
,
8894 bool allow_inverse_p
,
8895 bool c_cast_p
, tsubst_flags_t complain
)
8899 if (same_type_ignoring_top_level_qualifiers_p (from
, to
))
8900 /* Pointer to member of incomplete class is permitted*/
8901 result
= size_zero_node
;
8903 result
= get_delta_difference_1 (from
, to
, c_cast_p
, complain
);
8905 if (result
== error_mark_node
)
8906 return error_mark_node
;
8910 if (!allow_inverse_p
)
8912 if (!(complain
& tf_error
))
8913 return error_mark_node
;
8915 error_not_base_type (from
, to
);
8916 inform (input_location
, " in pointer to member conversion");
8917 result
= size_zero_node
;
8921 result
= get_delta_difference_1 (to
, from
, c_cast_p
, complain
);
8923 if (result
== error_mark_node
)
8924 return error_mark_node
;
8927 result
= size_diffop_loc (input_location
,
8928 size_zero_node
, result
);
8931 if (!(complain
& tf_error
))
8932 return error_mark_node
;
8934 error_not_base_type (from
, to
);
8935 inform (input_location
, " in pointer to member conversion");
8936 result
= size_zero_node
;
8941 return convert_to_integer (ptrdiff_type_node
, result
);
8944 /* Return a constructor for the pointer-to-member-function TYPE using
8945 the other components as specified. */
8948 build_ptrmemfunc1 (tree type
, tree delta
, tree pfn
)
8953 vec
<constructor_elt
, va_gc
> *v
;
8955 /* Pull the FIELD_DECLs out of the type. */
8956 pfn_field
= TYPE_FIELDS (type
);
8957 delta_field
= DECL_CHAIN (pfn_field
);
8959 /* Make sure DELTA has the type we want. */
8960 delta
= convert_and_check (input_location
, delta_type_node
, delta
);
8962 /* Convert to the correct target type if necessary. */
8963 pfn
= fold_convert (TREE_TYPE (pfn_field
), pfn
);
8965 /* Finish creating the initializer. */
8967 CONSTRUCTOR_APPEND_ELT(v
, pfn_field
, pfn
);
8968 CONSTRUCTOR_APPEND_ELT(v
, delta_field
, delta
);
8969 u
= build_constructor (type
, v
);
8970 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) & TREE_CONSTANT (delta
);
8971 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
8972 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
8974 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
8979 /* Build a constructor for a pointer to member function. It can be
8980 used to initialize global variables, local variable, or used
8981 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8984 If FORCE is nonzero, then force this conversion, even if
8985 we would rather not do it. Usually set when using an explicit
8986 cast. A C-style cast is being processed iff C_CAST_P is true.
8988 Return error_mark_node, if something goes wrong. */
8991 build_ptrmemfunc (tree type
, tree pfn
, int force
, bool c_cast_p
,
8992 tsubst_flags_t complain
)
8998 if (error_operand_p (pfn
))
8999 return error_mark_node
;
9001 pfn_type
= TREE_TYPE (pfn
);
9002 to_type
= build_ptrmemfunc_type (type
);
9004 /* Handle multiple conversions of pointer to member functions. */
9005 if (TYPE_PTRMEMFUNC_P (pfn_type
))
9007 tree delta
= NULL_TREE
;
9008 tree npfn
= NULL_TREE
;
9012 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
,
9013 LOOKUP_NORMAL
, complain
))
9015 if (complain
& tf_error
)
9016 error ("invalid conversion to type %qT from type %qT",
9019 return error_mark_node
;
9022 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
9023 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
9025 c_cast_p
, complain
);
9026 if (n
== error_mark_node
)
9027 return error_mark_node
;
9029 /* We don't have to do any conversion to convert a
9030 pointer-to-member to its own type. But, we don't want to
9031 just return a PTRMEM_CST if there's an explicit cast; that
9032 cast should make the expression an invalid template argument. */
9033 if (TREE_CODE (pfn
) != PTRMEM_CST
)
9035 if (same_type_p (to_type
, pfn_type
))
9037 else if (integer_zerop (n
) && TREE_CODE (pfn
) != CONSTRUCTOR
)
9038 return build_reinterpret_cast (input_location
, to_type
, pfn
,
9042 if (TREE_SIDE_EFFECTS (pfn
))
9043 pfn
= save_expr (pfn
);
9045 /* Obtain the function pointer and the current DELTA. */
9046 if (TREE_CODE (pfn
) == PTRMEM_CST
)
9047 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
9050 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
9051 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
9054 /* Just adjust the DELTA field. */
9055 gcc_assert (same_type_ignoring_top_level_qualifiers_p
9056 (TREE_TYPE (delta
), ptrdiff_type_node
));
9057 if (!integer_zerop (n
))
9059 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
9060 n
= cp_build_binary_op (input_location
,
9061 LSHIFT_EXPR
, n
, integer_one_node
,
9063 delta
= cp_build_binary_op (input_location
,
9064 PLUS_EXPR
, delta
, n
, complain
);
9066 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
9069 /* Handle null pointer to member function conversions. */
9070 if (null_ptr_cst_p (pfn
))
9072 pfn
= cp_build_c_cast (input_location
, type
, pfn
, complain
);
9073 return build_ptrmemfunc1 (to_type
,
9078 if (type_unknown_p (pfn
))
9079 return instantiate_type (type
, pfn
, complain
);
9081 fn
= TREE_OPERAND (pfn
, 0);
9082 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
9083 /* In a template, we will have preserved the
9085 || (processing_template_decl
&& TREE_CODE (fn
) == OFFSET_REF
));
9086 return make_ptrmem_cst (to_type
, fn
);
9089 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
9092 ??? There is no consistency as to the types returned for the above
9093 values. Some code acts as if it were a sizetype and some as if it were
9094 integer_type_node. */
9097 expand_ptrmemfunc_cst (tree cst
, tree
*delta
, tree
*pfn
)
9099 tree type
= TREE_TYPE (cst
);
9100 tree fn
= PTRMEM_CST_MEMBER (cst
);
9101 tree ptr_class
, fn_class
;
9103 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
9105 /* The class that the function belongs to. */
9106 fn_class
= DECL_CONTEXT (fn
);
9108 /* The class that we're creating a pointer to member of. */
9109 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
9111 /* First, calculate the adjustment to the function's class. */
9112 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0,
9113 /*c_cast_p=*/0, tf_warning_or_error
);
9115 if (!DECL_VIRTUAL_P (fn
))
9116 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
),
9117 build_addr_func (fn
, tf_warning_or_error
));
9120 /* If we're dealing with a virtual function, we have to adjust 'this'
9121 again, to point to the base which provides the vtable entry for
9122 fn; the call will do the opposite adjustment. */
9123 tree orig_class
= DECL_CONTEXT (fn
);
9124 tree binfo
= binfo_or_else (orig_class
, fn_class
);
9125 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
9126 *delta
, BINFO_OFFSET (binfo
));
9128 /* We set PFN to the vtable offset at which the function can be
9129 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
9130 case delta is shifted left, and then incremented). */
9131 *pfn
= DECL_VINDEX (fn
);
9132 *pfn
= fold_build2 (MULT_EXPR
, integer_type_node
, *pfn
,
9133 TYPE_SIZE_UNIT (vtable_entry_type
));
9135 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
9137 case ptrmemfunc_vbit_in_pfn
:
9138 *pfn
= fold_build2 (PLUS_EXPR
, integer_type_node
, *pfn
,
9142 case ptrmemfunc_vbit_in_delta
:
9143 *delta
= fold_build2 (LSHIFT_EXPR
, TREE_TYPE (*delta
),
9144 *delta
, integer_one_node
);
9145 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
9146 *delta
, integer_one_node
);
9153 *pfn
= fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), *pfn
);
9157 /* Return an expression for PFN from the pointer-to-member function
9161 pfn_from_ptrmemfunc (tree t
)
9163 if (TREE_CODE (t
) == PTRMEM_CST
)
9168 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
9173 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
9176 /* Return an expression for DELTA from the pointer-to-member function
9180 delta_from_ptrmemfunc (tree t
)
9182 if (TREE_CODE (t
) == PTRMEM_CST
)
9187 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
9192 return build_ptrmemfunc_access_expr (t
, delta_identifier
);
9195 /* Convert value RHS to type TYPE as preparation for an assignment to
9196 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
9197 implicit conversion is. If FNDECL is non-NULL, we are doing the
9198 conversion in order to pass the PARMNUMth argument of FNDECL.
9199 If FNDECL is NULL, we are doing the conversion in function pointer
9200 argument passing, conversion in initialization, etc. */
9203 convert_for_assignment (tree type
, tree rhs
,
9204 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
9205 tsubst_flags_t complain
, int flags
)
9208 enum tree_code coder
;
9210 location_t rhs_loc
= EXPR_LOC_OR_LOC (rhs
, input_location
);
9211 bool has_loc
= EXPR_LOCATION (rhs
) != UNKNOWN_LOCATION
;
9212 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
9213 but preserve location wrappers. */
9214 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
9215 && !location_wrapper_p (rhs
))
9216 rhs
= TREE_OPERAND (rhs
, 0);
9218 /* Handle [dcl.init.list] direct-list-initialization from
9219 single element of enumeration with a fixed underlying type. */
9220 if (is_direct_enum_init (type
, rhs
))
9222 tree elt
= CONSTRUCTOR_ELT (rhs
, 0)->value
;
9223 if (check_narrowing (ENUM_UNDERLYING_TYPE (type
), elt
, complain
))
9225 warning_sentinel
w (warn_useless_cast
);
9226 warning_sentinel
w2 (warn_ignored_qualifiers
);
9227 rhs
= cp_build_c_cast (rhs_loc
, type
, elt
, complain
);
9230 rhs
= error_mark_node
;
9233 rhstype
= TREE_TYPE (rhs
);
9234 coder
= TREE_CODE (rhstype
);
9236 if (VECTOR_TYPE_P (type
) && coder
== VECTOR_TYPE
9237 && vector_types_convertible_p (type
, rhstype
, true))
9239 rhs
= mark_rvalue_use (rhs
);
9240 return convert (type
, rhs
);
9243 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
9244 return error_mark_node
;
9245 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
9246 return error_mark_node
;
9248 /* The RHS of an assignment cannot have void type. */
9249 if (coder
== VOID_TYPE
)
9251 if (complain
& tf_error
)
9252 error_at (rhs_loc
, "void value not ignored as it ought to be");
9253 return error_mark_node
;
9256 if (c_dialect_objc ())
9260 tree rname
= fndecl
;
9271 selector
= objc_message_selector ();
9273 if (selector
&& parmno
> 1)
9280 if (objc_compare_types (type
, rhstype
, parmno
, rname
))
9282 rhs
= mark_rvalue_use (rhs
);
9283 return convert (type
, rhs
);
9289 The expression is implicitly converted (clause _conv_) to the
9290 cv-unqualified type of the left operand.
9292 We allow bad conversions here because by the time we get to this point
9293 we are committed to doing the conversion. If we end up doing a bad
9294 conversion, convert_like will complain. */
9295 if (!can_convert_arg_bad (type
, rhstype
, rhs
, flags
, complain
))
9297 /* When -Wno-pmf-conversions is use, we just silently allow
9298 conversions from pointers-to-members to plain pointers. If
9299 the conversion doesn't work, cp_convert will complain. */
9301 && TYPE_PTR_P (type
)
9302 && TYPE_PTRMEMFUNC_P (rhstype
))
9303 rhs
= cp_convert (strip_top_quals (type
), rhs
, complain
);
9306 if (complain
& tf_error
)
9308 /* If the right-hand side has unknown type, then it is an
9309 overloaded function. Call instantiate_type to get error
9311 if (rhstype
== unknown_type_node
)
9313 tree r
= instantiate_type (type
, rhs
, tf_warning_or_error
);
9314 /* -fpermissive might allow this; recurse. */
9316 return convert_for_assignment (type
, r
, errtype
, fndecl
,
9317 parmnum
, complain
, flags
);
9320 complain_about_bad_argument (rhs_loc
,
9325 range_label_for_type_mismatch
label (rhstype
, type
);
9326 gcc_rich_location
richloc (rhs_loc
, has_loc
? &label
: NULL
);
9329 case ICR_DEFAULT_ARGUMENT
:
9331 "cannot convert %qH to %qI in default argument",
9336 "cannot convert %qH to %qI in argument passing",
9339 case ICR_CONVERTING
:
9340 error_at (&richloc
, "cannot convert %qH to %qI",
9345 "cannot convert %qH to %qI in initialization",
9349 error_at (&richloc
, "cannot convert %qH to %qI in return",
9354 "cannot convert %qH to %qI in assignment",
9361 if (TYPE_PTR_P (rhstype
)
9362 && TYPE_PTR_P (type
)
9363 && CLASS_TYPE_P (TREE_TYPE (rhstype
))
9364 && CLASS_TYPE_P (TREE_TYPE (type
))
9365 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype
)))
9366 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
9367 (TREE_TYPE (rhstype
))),
9368 "class type %qT is incomplete", TREE_TYPE (rhstype
));
9370 return error_mark_node
;
9373 if (warn_suggest_attribute_format
)
9375 const enum tree_code codel
= TREE_CODE (type
);
9376 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
9378 && check_missing_format_attribute (type
, rhstype
)
9379 && (complain
& tf_warning
))
9383 case ICR_DEFAULT_ARGUMENT
:
9385 warning (OPT_Wsuggest_attribute_format
,
9386 "parameter %qP of %qD might be a candidate "
9387 "for a format attribute", parmnum
, fndecl
);
9389 warning (OPT_Wsuggest_attribute_format
,
9390 "parameter might be a candidate "
9391 "for a format attribute");
9393 case ICR_CONVERTING
:
9394 warning (OPT_Wsuggest_attribute_format
,
9395 "target of conversion might be a candidate "
9396 "for a format attribute");
9399 warning (OPT_Wsuggest_attribute_format
,
9400 "target of initialization might be a candidate "
9401 "for a format attribute");
9404 warning (OPT_Wsuggest_attribute_format
,
9405 "return type might be a candidate "
9406 "for a format attribute");
9409 warning (OPT_Wsuggest_attribute_format
,
9410 "left-hand side of assignment might be a candidate "
9411 "for a format attribute");
9418 /* If -Wparentheses, warn about a = b = c when a has type bool and b
9420 if (warn_parentheses
9421 && TREE_CODE (type
) == BOOLEAN_TYPE
9422 && TREE_CODE (rhs
) == MODIFY_EXPR
9423 && !TREE_NO_WARNING (rhs
)
9424 && TREE_CODE (TREE_TYPE (rhs
)) != BOOLEAN_TYPE
9425 && (complain
& tf_warning
)
9426 && warning_at (rhs_loc
, OPT_Wparentheses
,
9427 "suggest parentheses around assignment used as "
9429 TREE_NO_WARNING (rhs
) = 1;
9431 if (complain
& tf_warning
)
9432 warn_for_address_or_pointer_of_packed_member (type
, rhs
);
9434 return perform_implicit_conversion_flags (strip_top_quals (type
), rhs
,
9438 /* Convert RHS to be of type TYPE.
9439 If EXP is nonzero, it is the target of the initialization.
9440 ERRTYPE indicates what kind of error the implicit conversion is.
9442 Two major differences between the behavior of
9443 `convert_for_assignment' and `convert_for_initialization'
9444 are that references are bashed in the former, while
9445 copied in the latter, and aggregates are assigned in
9446 the former (operator=) while initialized in the
9449 If using constructor make sure no conversion operator exists, if one does
9450 exist, an ambiguity exists. */
9453 convert_for_initialization (tree exp
, tree type
, tree rhs
, int flags
,
9454 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
9455 tsubst_flags_t complain
)
9457 enum tree_code codel
= TREE_CODE (type
);
9459 enum tree_code coder
;
9461 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9462 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
9463 if (TREE_CODE (rhs
) == NOP_EXPR
9464 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
9465 && codel
!= REFERENCE_TYPE
)
9466 rhs
= TREE_OPERAND (rhs
, 0);
9468 if (type
== error_mark_node
9469 || rhs
== error_mark_node
9470 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
9471 return error_mark_node
;
9473 if (MAYBE_CLASS_TYPE_P (non_reference (type
)))
9475 else if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
9476 && TREE_CODE (type
) != ARRAY_TYPE
9477 && (!TYPE_REF_P (type
)
9478 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
9479 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
9480 && !TYPE_REFFN_P (type
))
9481 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
9482 rhs
= decay_conversion (rhs
, complain
);
9484 rhstype
= TREE_TYPE (rhs
);
9485 coder
= TREE_CODE (rhstype
);
9487 if (coder
== ERROR_MARK
)
9488 return error_mark_node
;
9490 /* We accept references to incomplete types, so we can
9491 return here before checking if RHS is of complete type. */
9493 if (codel
== REFERENCE_TYPE
)
9495 auto_diagnostic_group d
;
9496 /* This should eventually happen in convert_arguments. */
9497 int savew
= 0, savee
= 0;
9500 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
9501 rhs
= initialize_reference (type
, rhs
, flags
, complain
);
9504 && (warningcount
+ werrorcount
> savew
|| errorcount
> savee
))
9505 inform (get_fndecl_argument_location (fndecl
, parmnum
),
9506 "in passing argument %P of %qD", parmnum
, fndecl
);
9511 exp
= require_complete_type_sfinae (exp
, complain
);
9512 if (exp
== error_mark_node
)
9513 return error_mark_node
;
9515 type
= complete_type (type
);
9517 if (DIRECT_INIT_EXPR_P (type
, rhs
))
9518 /* Don't try to do copy-initialization if we already have
9519 direct-initialization. */
9522 if (MAYBE_CLASS_TYPE_P (type
))
9523 return perform_implicit_conversion_flags (type
, rhs
, complain
, flags
);
9525 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
,
9529 /* If RETVAL is the address of, or a reference to, a local variable or
9530 temporary give an appropriate warning and return true. */
9533 maybe_warn_about_returning_address_of_local (tree retval
, location_t loc
)
9535 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
9536 tree whats_returned
= fold_for_warn (retval
);
9538 loc
= cp_expr_loc_or_input_loc (retval
);
9542 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
9543 whats_returned
= TREE_OPERAND (whats_returned
, 1);
9544 else if (CONVERT_EXPR_P (whats_returned
)
9545 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
)
9546 whats_returned
= TREE_OPERAND (whats_returned
, 0);
9551 if (TREE_CODE (whats_returned
) == TARGET_EXPR
9552 && is_std_init_list (TREE_TYPE (whats_returned
)))
9554 tree init
= TARGET_EXPR_INITIAL (whats_returned
);
9555 if (TREE_CODE (init
) == CONSTRUCTOR
)
9556 /* Pull out the array address. */
9557 whats_returned
= CONSTRUCTOR_ELT (init
, 0)->value
;
9558 else if (TREE_CODE (init
) == INDIRECT_REF
)
9559 /* The source of a trivial copy looks like *(T*)&var. */
9560 whats_returned
= TREE_OPERAND (init
, 0);
9563 STRIP_NOPS (whats_returned
);
9566 /* As a special case, we handle a call to std::move or std::forward. */
9567 if (TREE_CODE (whats_returned
) == CALL_EXPR
9568 && (is_std_move_p (whats_returned
)
9569 || is_std_forward_p (whats_returned
)))
9571 tree arg
= CALL_EXPR_ARG (whats_returned
, 0);
9572 return maybe_warn_about_returning_address_of_local (arg
, loc
);
9575 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
9577 whats_returned
= TREE_OPERAND (whats_returned
, 0);
9579 while (TREE_CODE (whats_returned
) == COMPONENT_REF
9580 || TREE_CODE (whats_returned
) == ARRAY_REF
)
9581 whats_returned
= TREE_OPERAND (whats_returned
, 0);
9583 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
9584 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
9586 if (TYPE_REF_P (valtype
))
9587 warning_at (loc
, OPT_Wreturn_local_addr
,
9588 "returning reference to temporary");
9589 else if (is_std_init_list (valtype
))
9590 warning_at (loc
, OPT_Winit_list_lifetime
,
9591 "returning temporary %<initializer_list%> does not extend "
9592 "the lifetime of the underlying array");
9596 STRIP_ANY_LOCATION_WRAPPER (whats_returned
);
9598 if (DECL_P (whats_returned
)
9599 && DECL_NAME (whats_returned
)
9600 && DECL_FUNCTION_SCOPE_P (whats_returned
)
9601 && !is_capture_proxy (whats_returned
)
9602 && !(TREE_STATIC (whats_returned
)
9603 || TREE_PUBLIC (whats_returned
)))
9605 if (VAR_P (whats_returned
)
9606 && DECL_DECOMPOSITION_P (whats_returned
)
9607 && DECL_DECOMP_BASE (whats_returned
)
9608 && DECL_HAS_VALUE_EXPR_P (whats_returned
))
9610 /* When returning address of a structured binding, if the structured
9611 binding is not a reference, continue normally, if it is a
9612 reference, recurse on the initializer of the structured
9614 tree base
= DECL_DECOMP_BASE (whats_returned
);
9615 if (TYPE_REF_P (TREE_TYPE (base
)))
9617 if (tree init
= DECL_INITIAL (base
))
9618 return maybe_warn_about_returning_address_of_local (init
, loc
);
9624 auto_diagnostic_group d
;
9625 if (TYPE_REF_P (valtype
))
9626 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
9627 "reference to local variable %qD returned",
9629 else if (is_std_init_list (valtype
))
9630 w
= warning_at (loc
, OPT_Winit_list_lifetime
,
9631 "returning local %<initializer_list%> variable %qD "
9632 "does not extend the lifetime of the underlying array",
9634 else if (POINTER_TYPE_P (valtype
)
9635 && TREE_CODE (whats_returned
) == LABEL_DECL
)
9636 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
9637 "address of label %qD returned",
9639 else if (POINTER_TYPE_P (valtype
))
9640 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
9641 "address of local variable %qD returned",
9644 inform (DECL_SOURCE_LOCATION (whats_returned
),
9652 /* Returns true if DECL is in the std namespace. */
9655 decl_in_std_namespace_p (tree decl
)
9659 decl
= decl_namespace_context (decl
);
9660 if (DECL_NAMESPACE_STD_P (decl
))
9662 /* Allow inline namespaces inside of std namespace, e.g. with
9663 --enable-symvers=gnu-versioned-namespace std::forward would be
9664 actually std::_8::forward. */
9665 if (!DECL_NAMESPACE_INLINE_P (decl
))
9667 decl
= CP_DECL_CONTEXT (decl
);
9672 /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
9675 is_std_forward_p (tree fn
)
9677 /* std::forward only takes one argument. */
9678 if (call_expr_nargs (fn
) != 1)
9681 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
9682 if (!decl_in_std_namespace_p (fndecl
))
9685 tree name
= DECL_NAME (fndecl
);
9686 return name
&& id_equal (name
, "forward");
9689 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
9692 is_std_move_p (tree fn
)
9694 /* std::move only takes one argument. */
9695 if (call_expr_nargs (fn
) != 1)
9698 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
9699 if (!decl_in_std_namespace_p (fndecl
))
9702 tree name
= DECL_NAME (fndecl
);
9703 return name
&& id_equal (name
, "move");
9706 /* Returns true if RETVAL is a good candidate for the NRVO as per
9707 [class.copy.elision]. FUNCTYPE is the type the function is declared
9711 can_do_nrvo_p (tree retval
, tree functype
)
9713 if (functype
== error_mark_node
)
9716 STRIP_ANY_LOCATION_WRAPPER (retval
);
9717 tree result
= DECL_RESULT (current_function_decl
);
9718 return (retval
!= NULL_TREE
9719 && !processing_template_decl
9720 /* Must be a local, automatic variable. */
9722 && DECL_CONTEXT (retval
) == current_function_decl
9723 && !TREE_STATIC (retval
)
9724 /* And not a lambda or anonymous union proxy. */
9725 && !DECL_HAS_VALUE_EXPR_P (retval
)
9726 && (DECL_ALIGN (retval
) <= DECL_ALIGN (result
))
9727 /* The cv-unqualified type of the returned value must be the
9728 same as the cv-unqualified return type of the
9730 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval
))),
9731 (TYPE_MAIN_VARIANT (functype
)))
9732 /* And the returned value must be non-volatile. */
9733 && !TYPE_VOLATILE (TREE_TYPE (retval
)));
9736 /* If we should treat RETVAL, an expression being returned, as if it were
9737 designated by an rvalue, returns it adjusted accordingly; otherwise, returns
9738 NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
9739 context (rather than throw). */
9742 treat_lvalue_as_rvalue_p (tree expr
, bool return_p
)
9744 if (cxx_dialect
== cxx98
)
9748 STRIP_ANY_LOCATION_WRAPPER (retval
);
9749 if (REFERENCE_REF_P (retval
))
9750 retval
= TREE_OPERAND (retval
, 0);
9752 /* An implicitly movable entity is a variable of automatic storage duration
9753 that is either a non-volatile object or (C++20) an rvalue reference to a
9754 non-volatile object type. */
9755 if (!(((VAR_P (retval
) && !DECL_HAS_VALUE_EXPR_P (retval
))
9756 || TREE_CODE (retval
) == PARM_DECL
)
9757 && !TREE_STATIC (retval
)
9758 && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval
)))
9759 && (TREE_CODE (TREE_TYPE (retval
)) != REFERENCE_TYPE
9760 || (cxx_dialect
>= cxx20
9761 && TYPE_REF_IS_RVALUE (TREE_TYPE (retval
))))))
9764 /* If the expression in a return or co_return statement is a (possibly
9765 parenthesized) id-expression that names an implicitly movable entity
9766 declared in the body or parameter-declaration-clause of the innermost
9767 enclosing function or lambda-expression, */
9768 if (DECL_CONTEXT (retval
) != current_function_decl
)
9771 return set_implicit_rvalue_p (move (expr
));
9773 /* if the operand of a throw-expression is a (possibly parenthesized)
9774 id-expression that names an implicitly movable entity whose scope does not
9775 extend beyond the compound-statement of the innermost try-block or
9776 function-try-block (if any) whose compound-statement or ctor-initializer
9777 encloses the throw-expression, */
9779 /* C++20 added move on throw of parms. */
9780 if (TREE_CODE (retval
) == PARM_DECL
&& cxx_dialect
< cxx20
)
9783 for (cp_binding_level
*b
= current_binding_level
;
9784 ; b
= b
->level_chain
)
9786 for (tree decl
= b
->names
; decl
; decl
= TREE_CHAIN (decl
))
9788 return set_implicit_rvalue_p (move (expr
));
9789 if (b
->kind
== sk_function_parms
|| b
->kind
== sk_try
)
9794 /* Warn about wrong usage of std::move in a return statement. RETVAL
9795 is the expression we are returning; FUNCTYPE is the type the function
9796 is declared to return. */
9799 maybe_warn_pessimizing_move (tree retval
, tree functype
)
9801 if (!(warn_pessimizing_move
|| warn_redundant_move
))
9804 location_t loc
= cp_expr_loc_or_input_loc (retval
);
9806 /* C++98 doesn't know move. */
9807 if (cxx_dialect
< cxx11
)
9810 /* Wait until instantiation time, since we can't gauge if we should do
9811 the NRVO until then. */
9812 if (processing_template_decl
)
9815 /* This is only interesting for class types. */
9816 if (!CLASS_TYPE_P (functype
))
9819 /* We're looking for *std::move<T&> ((T &) &arg). */
9820 if (REFERENCE_REF_P (retval
)
9821 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
9823 tree fn
= TREE_OPERAND (retval
, 0);
9824 if (is_std_move_p (fn
))
9826 tree arg
= CALL_EXPR_ARG (fn
, 0);
9828 if (TREE_CODE (arg
) != NOP_EXPR
)
9830 arg
= TREE_OPERAND (arg
, 0);
9831 if (TREE_CODE (arg
) != ADDR_EXPR
)
9833 arg
= TREE_OPERAND (arg
, 0);
9834 arg
= convert_from_reference (arg
);
9835 /* Warn if we could do copy elision were it not for the move. */
9836 if (can_do_nrvo_p (arg
, functype
))
9838 auto_diagnostic_group d
;
9839 if (warning_at (loc
, OPT_Wpessimizing_move
,
9840 "moving a local object in a return statement "
9841 "prevents copy elision"))
9842 inform (loc
, "remove %<std::move%> call");
9844 /* Warn if the move is redundant. It is redundant when we would
9845 do maybe-rvalue overload resolution even without std::move. */
9846 else if (warn_redundant_move
9847 && (moved
= treat_lvalue_as_rvalue_p (arg
, /*return*/true)))
9849 /* Make sure that the overload resolution would actually succeed
9850 if we removed the std::move call. */
9851 tree t
= convert_for_initialization (NULL_TREE
, functype
,
9854 | LOOKUP_ONLYCONVERTING
9855 | LOOKUP_PREFER_RVALUE
),
9856 ICR_RETURN
, NULL_TREE
, 0,
9858 /* If this worked, implicit rvalue would work, so the call to
9859 std::move is redundant. */
9860 if (t
!= error_mark_node
)
9862 auto_diagnostic_group d
;
9863 if (warning_at (loc
, OPT_Wredundant_move
,
9864 "redundant move in return statement"))
9865 inform (loc
, "remove %<std::move%> call");
9872 /* Check that returning RETVAL from the current function is valid.
9873 Return an expression explicitly showing all conversions required to
9874 change RETVAL into the function return type, and to assign it to
9875 the DECL_RESULT for the function. Set *NO_WARNING to true if
9876 code reaches end of non-void function warning shouldn't be issued
9877 on this RETURN_EXPR. */
9880 check_return_expr (tree retval
, bool *no_warning
)
9883 /* The type actually returned by the function. */
9885 /* The type the function is declared to return, or void if
9886 the declared type is incomplete. */
9888 int fn_returns_value_p
;
9889 location_t loc
= cp_expr_loc_or_input_loc (retval
);
9891 *no_warning
= false;
9893 /* A `volatile' function is one that isn't supposed to return, ever.
9894 (This is a G++ extension, used to get better code for functions
9895 that call the `volatile' function.) */
9896 if (TREE_THIS_VOLATILE (current_function_decl
))
9897 warning (0, "function declared %<noreturn%> has a %<return%> statement");
9899 /* Check for various simple errors. */
9900 if (DECL_DESTRUCTOR_P (current_function_decl
))
9903 error_at (loc
, "returning a value from a destructor");
9906 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
9908 if (in_function_try_handler
)
9909 /* If a return statement appears in a handler of the
9910 function-try-block of a constructor, the program is ill-formed. */
9911 error ("cannot return from a handler of a function-try-block of a constructor");
9913 /* You can't return a value from a constructor. */
9914 error_at (loc
, "returning a value from a constructor");
9918 const tree saved_retval
= retval
;
9920 if (processing_template_decl
)
9922 current_function_returns_value
= 1;
9924 if (check_for_bare_parameter_packs (retval
))
9925 return error_mark_node
;
9927 /* If one of the types might be void, we can't tell whether we're
9928 returning a value. */
9929 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl
)))
9930 && !FNDECL_USED_AUTO (current_function_decl
))
9931 || (retval
!= NULL_TREE
9932 && (TREE_TYPE (retval
) == NULL_TREE
9933 || WILDCARD_TYPE_P (TREE_TYPE (retval
)))))
9937 functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
9939 /* Deduce auto return type from a return statement. */
9940 if (FNDECL_USED_AUTO (current_function_decl
))
9942 tree pattern
= DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
);
9946 if (!retval
&& !is_auto (pattern
))
9948 /* Give a helpful error message. */
9949 error ("return-statement with no value, in function returning %qT",
9951 inform (input_location
, "only plain %<auto%> return type can be "
9952 "deduced to %<void%>");
9953 type
= error_mark_node
;
9955 else if (retval
&& BRACE_ENCLOSED_INITIALIZER_P (retval
))
9957 error ("returning initializer list");
9958 type
= error_mark_node
;
9964 auto_node
= type_uses_auto (pattern
);
9965 type
= do_auto_deduction (pattern
, retval
, auto_node
,
9966 tf_warning_or_error
, adc_return_type
);
9969 if (type
== error_mark_node
)
9971 else if (functype
== pattern
)
9972 apply_deduced_return_type (current_function_decl
, type
);
9973 else if (!same_type_p (type
, functype
))
9975 if (LAMBDA_FUNCTION_P (current_function_decl
))
9976 error_at (loc
, "inconsistent types %qT and %qT deduced for "
9977 "lambda return type", functype
, type
);
9979 error_at (loc
, "inconsistent deduction for auto return type: "
9980 "%qT and then %qT", functype
, type
);
9985 result
= DECL_RESULT (current_function_decl
);
9986 valtype
= TREE_TYPE (result
);
9987 gcc_assert (valtype
!= NULL_TREE
);
9988 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
9990 /* Check for a return statement with no return value in a function
9991 that's supposed to return a value. */
9992 if (!retval
&& fn_returns_value_p
)
9994 if (functype
!= error_mark_node
)
9995 permerror (input_location
, "return-statement with no value, in "
9996 "function returning %qT", valtype
);
9997 /* Remember that this function did return. */
9998 current_function_returns_value
= 1;
9999 /* And signal caller that TREE_NO_WARNING should be set on the
10000 RETURN_EXPR to avoid control reaches end of non-void function
10001 warnings in tree-cfg.c. */
10002 *no_warning
= true;
10004 /* Check for a return statement with a value in a function that
10005 isn't supposed to return a value. */
10006 else if (retval
&& !fn_returns_value_p
)
10008 if (VOID_TYPE_P (TREE_TYPE (retval
)))
10009 /* You can return a `void' value from a function of `void'
10010 type. In that case, we have to evaluate the expression for
10011 its side-effects. */
10012 finish_expr_stmt (retval
);
10013 else if (retval
!= error_mark_node
)
10014 permerror (loc
, "return-statement with a value, in function "
10015 "returning %qT", valtype
);
10016 current_function_returns_null
= 1;
10018 /* There's really no value to return, after all. */
10022 /* Remember that this function can sometimes return without a
10024 current_function_returns_null
= 1;
10026 /* Remember that this function did return a value. */
10027 current_function_returns_value
= 1;
10029 /* Check for erroneous operands -- but after giving ourselves a
10030 chance to provide an error about returning a value from a void
10032 if (error_operand_p (retval
))
10034 current_function_return_value
= error_mark_node
;
10035 return error_mark_node
;
10038 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
10039 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl
))
10040 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
10041 && ! flag_check_new
10042 && retval
&& null_ptr_cst_p (retval
))
10043 warning (0, "%<operator new%> must not return NULL unless it is "
10044 "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
10046 /* Effective C++ rule 15. See also start_function. */
10048 && DECL_NAME (current_function_decl
) == assign_op_identifier
10049 && !type_dependent_expression_p (retval
))
10053 /* The function return type must be a reference to the current
10055 if (TYPE_REF_P (valtype
)
10056 && same_type_ignoring_top_level_qualifiers_p
10057 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
)))
10059 /* Returning '*this' is obviously OK. */
10060 if (retval
== current_class_ref
)
10062 /* If we are calling a function whose return type is the same of
10063 the current class reference, it is ok. */
10064 else if (INDIRECT_REF_P (retval
)
10065 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
10070 warning_at (loc
, OPT_Weffc__
,
10071 "%<operator=%> should return a reference to %<*this%>");
10074 if (dependent_type_p (functype
)
10075 || type_dependent_expression_p (retval
))
10078 /* We should not have changed the return value. */
10079 gcc_assert (retval
== saved_retval
);
10083 /* The fabled Named Return Value optimization, as per [class.copy]/15:
10085 [...] For a function with a class return type, if the expression
10086 in the return statement is the name of a local object, and the cv-
10087 unqualified type of the local object is the same as the function
10088 return type, an implementation is permitted to omit creating the tem-
10089 porary object to hold the function return value [...]
10091 So, if this is a value-returning function that always returns the same
10092 local variable, remember it.
10094 It might be nice to be more flexible, and choose the first suitable
10095 variable even if the function sometimes returns something else, but
10096 then we run the risk of clobbering the variable we chose if the other
10097 returned expression uses the chosen variable somehow. And people expect
10098 this restriction, anyway. (jason 2000-11-19)
10100 See finish_function and finalize_nrv for the rest of this optimization. */
10102 STRIP_ANY_LOCATION_WRAPPER (retval
);
10104 bool named_return_value_okay_p
= can_do_nrvo_p (retval
, functype
);
10105 if (fn_returns_value_p
&& flag_elide_constructors
)
10107 if (named_return_value_okay_p
10108 && (current_function_return_value
== NULL_TREE
10109 || current_function_return_value
== retval
))
10110 current_function_return_value
= retval
;
10112 current_function_return_value
= error_mark_node
;
10115 /* We don't need to do any conversions when there's nothing being
10120 if (!named_return_value_okay_p
)
10121 maybe_warn_pessimizing_move (retval
, functype
);
10123 /* Do any required conversions. */
10124 if (retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
10125 /* No conversions are required. */
10129 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
10131 /* The functype's return type will have been set to void, if it
10132 was an incomplete type. Just treat this as 'return;' */
10133 if (VOID_TYPE_P (functype
))
10134 return error_mark_node
;
10136 /* If we had an id-expression obfuscated by force_paren_expr, we need
10137 to undo it so we can try to treat it as an rvalue below. */
10138 retval
= maybe_undo_parenthesized_ref (retval
);
10140 if (processing_template_decl
)
10141 retval
= build_non_dependent_expr (retval
);
10143 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
10144 treated as an rvalue for the purposes of overload resolution to
10145 favor move constructors over copy constructors.
10147 Note that these conditions are similar to, but not as strict as,
10148 the conditions for the named return value optimization. */
10149 bool converted
= false;
10151 /* This is only interesting for class type. */
10152 if (CLASS_TYPE_P (functype
)
10153 && (moved
= treat_lvalue_as_rvalue_p (retval
, /*return*/true)))
10155 if (cxx_dialect
< cxx20
)
10157 moved
= convert_for_initialization
10158 (NULL_TREE
, functype
, moved
, flags
|LOOKUP_PREFER_RVALUE
,
10159 ICR_RETURN
, NULL_TREE
, 0, tf_none
);
10160 if (moved
!= error_mark_node
)
10167 /* In C++20 we just treat the return value as an rvalue that
10168 can bind to lvalue refs. */
10172 /* The call in a (lambda) thunk needs no conversions. */
10173 if (TREE_CODE (retval
) == CALL_EXPR
10174 && CALL_FROM_THUNK_P (retval
))
10177 /* First convert the value to the function's return type, then
10178 to the type of return value's location to handle the
10179 case that functype is smaller than the valtype. */
10181 retval
= convert_for_initialization
10182 (NULL_TREE
, functype
, retval
, flags
, ICR_RETURN
, NULL_TREE
, 0,
10183 tf_warning_or_error
);
10184 retval
= convert (valtype
, retval
);
10186 /* If the conversion failed, treat this just like `return;'. */
10187 if (retval
== error_mark_node
)
10189 /* We can't initialize a register from a AGGR_INIT_EXPR. */
10190 else if (! cfun
->returns_struct
10191 && TREE_CODE (retval
) == TARGET_EXPR
10192 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
10193 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
10194 TREE_OPERAND (retval
, 0));
10195 else if (!processing_template_decl
10196 && maybe_warn_about_returning_address_of_local (retval
, loc
)
10197 && INDIRECT_TYPE_P (valtype
))
10198 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
10199 build_zero_cst (TREE_TYPE (retval
)));
10202 if (processing_template_decl
)
10203 return saved_retval
;
10205 /* Actually copy the value returned into the appropriate location. */
10206 if (retval
&& retval
!= result
)
10207 retval
= build2 (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
10209 if (tree set
= maybe_set_retval_sentinel ())
10210 retval
= build2 (COMPOUND_EXPR
, void_type_node
, retval
, set
);
10216 /* Returns nonzero if the pointer-type FROM can be converted to the
10217 pointer-type TO via a qualification conversion. If CONSTP is -1,
10218 then we return nonzero if the pointers are similar, and the
10219 cv-qualification signature of FROM is a proper subset of that of TO.
10221 If CONSTP is positive, then all outer pointers have been
10222 const-qualified. */
10225 comp_ptr_ttypes_real (tree to
, tree from
, int constp
)
10227 bool to_more_cv_qualified
= false;
10228 bool is_opaque_pointer
= false;
10230 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
10232 if (TREE_CODE (to
) != TREE_CODE (from
))
10235 if (TREE_CODE (from
) == OFFSET_TYPE
10236 && !same_type_p (TYPE_OFFSET_BASETYPE (from
),
10237 TYPE_OFFSET_BASETYPE (to
)))
10240 /* Const and volatile mean something different for function and
10241 array types, so the usual checks are not appropriate. We'll
10242 check the array type elements in further iterations. */
10243 if (!FUNC_OR_METHOD_TYPE_P (to
) && TREE_CODE (to
) != ARRAY_TYPE
)
10245 if (!at_least_as_qualified_p (to
, from
))
10248 if (!at_least_as_qualified_p (from
, to
))
10252 to_more_cv_qualified
= true;
10256 constp
&= TYPE_READONLY (to
);
10259 if (VECTOR_TYPE_P (to
))
10260 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
10262 /* P0388R4 allows a conversion from int[N] to int[] but not the
10263 other way round. When both arrays have bounds but they do
10264 not match, then no conversion is possible. */
10265 if (TREE_CODE (to
) == ARRAY_TYPE
10266 && !comp_array_types (to
, from
, bounds_first
, /*strict=*/false))
10269 if (!TYPE_PTR_P (to
)
10270 && !TYPE_PTRDATAMEM_P (to
)
10271 /* CWG 330 says we need to look through arrays. */
10272 && TREE_CODE (to
) != ARRAY_TYPE
)
10273 return ((constp
>= 0 || to_more_cv_qualified
)
10274 && (is_opaque_pointer
10275 || same_type_ignoring_top_level_qualifiers_p (to
, from
)));
10279 /* When comparing, say, char ** to char const **, this function takes
10280 the 'char *' and 'char const *'. Do not pass non-pointer/reference
10281 types to this function. */
10284 comp_ptr_ttypes (tree to
, tree from
)
10286 return comp_ptr_ttypes_real (to
, from
, 1);
10289 /* Returns true iff FNTYPE is a non-class type that involves
10290 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
10291 if a parameter type is ill-formed. */
10294 error_type_p (const_tree type
)
10298 switch (TREE_CODE (type
))
10304 case REFERENCE_TYPE
:
10306 return error_type_p (TREE_TYPE (type
));
10308 case FUNCTION_TYPE
:
10310 if (error_type_p (TREE_TYPE (type
)))
10312 for (t
= TYPE_ARG_TYPES (type
); t
; t
= TREE_CHAIN (t
))
10313 if (error_type_p (TREE_VALUE (t
)))
10318 if (TYPE_PTRMEMFUNC_P (type
))
10319 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type
));
10327 /* Returns true if to and from are (possibly multi-level) pointers to the same
10328 type or inheritance-related types, regardless of cv-quals. */
10331 ptr_reasonably_similar (const_tree to
, const_tree from
)
10333 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
10335 /* Any target type is similar enough to void. */
10336 if (VOID_TYPE_P (to
))
10337 return !error_type_p (from
);
10338 if (VOID_TYPE_P (from
))
10339 return !error_type_p (to
);
10341 if (TREE_CODE (to
) != TREE_CODE (from
))
10344 if (TREE_CODE (from
) == OFFSET_TYPE
10345 && comptypes (TYPE_OFFSET_BASETYPE (to
),
10346 TYPE_OFFSET_BASETYPE (from
),
10347 COMPARE_BASE
| COMPARE_DERIVED
))
10350 if (VECTOR_TYPE_P (to
)
10351 && vector_types_convertible_p (to
, from
, false))
10354 if (TREE_CODE (to
) == INTEGER_TYPE
10355 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
10358 if (TREE_CODE (to
) == FUNCTION_TYPE
)
10359 return !error_type_p (to
) && !error_type_p (from
);
10361 if (!TYPE_PTR_P (to
))
10363 /* When either type is incomplete avoid DERIVED_FROM_P,
10364 which may call complete_type (c++/57942). */
10365 bool b
= !COMPLETE_TYPE_P (to
) || !COMPLETE_TYPE_P (from
);
10367 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
10368 b
? COMPARE_STRICT
: COMPARE_BASE
| COMPARE_DERIVED
);
10373 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
10374 pointer-to-member types) are the same, ignoring cv-qualification at
10375 all levels. CB says how we should behave when comparing array bounds. */
10378 comp_ptr_ttypes_const (tree to
, tree from
, compare_bounds_t cb
)
10380 bool is_opaque_pointer
= false;
10382 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
10384 if (TREE_CODE (to
) != TREE_CODE (from
))
10387 if (TREE_CODE (from
) == OFFSET_TYPE
10388 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
10389 TYPE_OFFSET_BASETYPE (to
)))
10392 if (VECTOR_TYPE_P (to
))
10393 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
10395 if (TREE_CODE (to
) == ARRAY_TYPE
10396 /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
10398 && !comp_array_types (to
, from
, cb
, /*strict=*/false))
10401 /* CWG 330 says we need to look through arrays. */
10402 if (!TYPE_PTR_P (to
) && TREE_CODE (to
) != ARRAY_TYPE
)
10403 return (is_opaque_pointer
10404 || same_type_ignoring_top_level_qualifiers_p (to
, from
));
10408 /* Returns the type qualifiers for this type, including the qualifiers on the
10409 elements for an array type. */
10412 cp_type_quals (const_tree type
)
10415 /* This CONST_CAST is okay because strip_array_types returns its
10416 argument unmodified and we assign it to a const_tree. */
10417 type
= strip_array_types (CONST_CAST_TREE (type
));
10418 if (type
== error_mark_node
10419 /* Quals on a FUNCTION_TYPE are memfn quals. */
10420 || TREE_CODE (type
) == FUNCTION_TYPE
)
10421 return TYPE_UNQUALIFIED
;
10422 quals
= TYPE_QUALS (type
);
10423 /* METHOD and REFERENCE_TYPEs should never have quals. */
10424 gcc_assert ((TREE_CODE (type
) != METHOD_TYPE
10425 && !TYPE_REF_P (type
))
10426 || ((quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
))
10427 == TYPE_UNQUALIFIED
));
10431 /* Returns the function-ref-qualifier for TYPE */
10434 type_memfn_rqual (const_tree type
)
10436 gcc_assert (FUNC_OR_METHOD_TYPE_P (type
));
10438 if (!FUNCTION_REF_QUALIFIED (type
))
10439 return REF_QUAL_NONE
;
10440 else if (FUNCTION_RVALUE_QUALIFIED (type
))
10441 return REF_QUAL_RVALUE
;
10443 return REF_QUAL_LVALUE
;
10446 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
10450 type_memfn_quals (const_tree type
)
10452 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10453 return TYPE_QUALS (type
);
10454 else if (TREE_CODE (type
) == METHOD_TYPE
)
10455 return cp_type_quals (class_of_this_parm (type
));
10457 gcc_unreachable ();
10460 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
10461 MEMFN_QUALS and its ref-qualifier to RQUAL. */
10464 apply_memfn_quals (tree type
, cp_cv_quals memfn_quals
, cp_ref_qualifier rqual
)
10466 /* Could handle METHOD_TYPE here if necessary. */
10467 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
10468 if (TYPE_QUALS (type
) == memfn_quals
10469 && type_memfn_rqual (type
) == rqual
)
10472 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
10474 tree result
= build_qualified_type (type
, memfn_quals
);
10475 return build_ref_qualified_type (result
, rqual
);
10478 /* Returns nonzero if TYPE is const or volatile. */
10481 cv_qualified_p (const_tree type
)
10483 int quals
= cp_type_quals (type
);
10484 return (quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)) != 0;
10487 /* Returns nonzero if the TYPE contains a mutable member. */
10490 cp_has_mutable_p (const_tree type
)
10492 /* This CONST_CAST is okay because strip_array_types returns its
10493 argument unmodified and we assign it to a const_tree. */
10494 type
= strip_array_types (CONST_CAST_TREE(type
));
10496 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
10499 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
10500 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
10501 approximation. In particular, consider:
10504 struct S { int i; };
10505 const S s = { f(); }
10507 Here, we will make "s" as TREE_READONLY (because it is declared
10508 "const") -- only to reverse ourselves upon seeing that the
10509 initializer is non-constant. */
10512 cp_apply_type_quals_to_decl (int type_quals
, tree decl
)
10514 tree type
= TREE_TYPE (decl
);
10516 if (type
== error_mark_node
)
10519 if (TREE_CODE (decl
) == TYPE_DECL
)
10522 gcc_assert (!(TREE_CODE (type
) == FUNCTION_TYPE
10523 && type_quals
!= TYPE_UNQUALIFIED
));
10525 /* Avoid setting TREE_READONLY incorrectly. */
10526 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
10527 constructor can produce constant init, so rely on cp_finish_decl to
10528 clear TREE_READONLY if the variable has non-constant init. */
10530 /* If the type has (or might have) a mutable component, that component
10531 might be modified. */
10532 if (TYPE_HAS_MUTABLE_P (type
) || !COMPLETE_TYPE_P (type
))
10533 type_quals
&= ~TYPE_QUAL_CONST
;
10535 c_apply_type_quals_to_decl (type_quals
, decl
);
10538 /* Subroutine of casts_away_constness. Make T1 and T2 point at
10539 exemplar types such that casting T1 to T2 is casting away constness
10540 if and only if there is no implicit conversion from T1 to T2. */
10543 casts_away_constness_r (tree
*t1
, tree
*t2
, tsubst_flags_t complain
)
10548 /* [expr.const.cast]
10550 For multi-level pointer to members and multi-level mixed pointers
10551 and pointers to members (conv.qual), the "member" aspect of a
10552 pointer to member level is ignored when determining if a const
10553 cv-qualifier has been cast away. */
10554 /* [expr.const.cast]
10556 For two pointer types:
10558 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
10559 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
10562 casting from X1 to X2 casts away constness if, for a non-pointer
10563 type T there does not exist an implicit conversion (clause
10566 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
10570 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
10571 if ((!TYPE_PTR_P (*t1
) && !TYPE_PTRDATAMEM_P (*t1
))
10572 || (!TYPE_PTR_P (*t2
) && !TYPE_PTRDATAMEM_P (*t2
)))
10574 *t1
= cp_build_qualified_type (void_type_node
,
10575 cp_type_quals (*t1
));
10576 *t2
= cp_build_qualified_type (void_type_node
,
10577 cp_type_quals (*t2
));
10581 quals1
= cp_type_quals (*t1
);
10582 quals2
= cp_type_quals (*t2
);
10584 if (TYPE_PTRDATAMEM_P (*t1
))
10585 *t1
= TYPE_PTRMEM_POINTED_TO_TYPE (*t1
);
10587 *t1
= TREE_TYPE (*t1
);
10588 if (TYPE_PTRDATAMEM_P (*t2
))
10589 *t2
= TYPE_PTRMEM_POINTED_TO_TYPE (*t2
);
10591 *t2
= TREE_TYPE (*t2
);
10593 casts_away_constness_r (t1
, t2
, complain
);
10594 *t1
= build_pointer_type (*t1
);
10595 *t2
= build_pointer_type (*t2
);
10596 *t1
= cp_build_qualified_type (*t1
, quals1
);
10597 *t2
= cp_build_qualified_type (*t2
, quals2
);
10600 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
10603 ??? This function returns non-zero if casting away qualifiers not
10604 just const. We would like to return to the caller exactly which
10605 qualifiers are casted away to give more accurate diagnostics.
10609 casts_away_constness (tree t1
, tree t2
, tsubst_flags_t complain
)
10611 if (TYPE_REF_P (t2
))
10613 /* [expr.const.cast]
10615 Casting from an lvalue of type T1 to an lvalue of type T2
10616 using a reference cast casts away constness if a cast from an
10617 rvalue of type "pointer to T1" to the type "pointer to T2"
10618 casts away constness. */
10619 t1
= (TYPE_REF_P (t1
) ? TREE_TYPE (t1
) : t1
);
10620 return casts_away_constness (build_pointer_type (t1
),
10621 build_pointer_type (TREE_TYPE (t2
)),
10625 if (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
10626 /* [expr.const.cast]
10628 Casting from an rvalue of type "pointer to data member of X
10629 of type T1" to the type "pointer to data member of Y of type
10630 T2" casts away constness if a cast from an rvalue of type
10631 "pointer to T1" to the type "pointer to T2" casts away
10633 return casts_away_constness
10634 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1
)),
10635 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2
)),
10638 /* Casting away constness is only something that makes sense for
10639 pointer or reference types. */
10640 if (!TYPE_PTR_P (t1
) || !TYPE_PTR_P (t2
))
10643 /* Top-level qualifiers don't matter. */
10644 t1
= TYPE_MAIN_VARIANT (t1
);
10645 t2
= TYPE_MAIN_VARIANT (t2
);
10646 casts_away_constness_r (&t1
, &t2
, complain
);
10647 if (!can_convert (t2
, t1
, complain
))
10653 /* If T is a REFERENCE_TYPE return the type to which T refers.
10654 Otherwise, return T itself. */
10657 non_reference (tree t
)
10659 if (t
&& TYPE_REF_P (t
))
10665 /* Return nonzero if REF is an lvalue valid for this language;
10666 otherwise, print an error message and return zero. USE says
10667 how the lvalue is being used and so selects the error message. */
10670 lvalue_or_else (tree ref
, enum lvalue_use use
, tsubst_flags_t complain
)
10672 cp_lvalue_kind kind
= lvalue_kind (ref
);
10674 if (kind
== clk_none
)
10676 if (complain
& tf_error
)
10677 lvalue_error (cp_expr_loc_or_input_loc (ref
), use
);
10680 else if (kind
& (clk_rvalueref
|clk_class
))
10682 if (!(complain
& tf_error
))
10684 /* Make this a permerror because we used to accept it. */
10685 permerror (cp_expr_loc_or_input_loc (ref
),
10686 "using rvalue as lvalue");
10691 /* Return true if a user-defined literal operator is a raw operator. */
10694 check_raw_literal_operator (const_tree decl
)
10696 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
10699 bool maybe_raw_p
= false;
10701 /* Count the number and type of arguments and check for ellipsis. */
10702 for (argtype
= argtypes
, arity
= 0;
10703 argtype
&& argtype
!= void_list_node
;
10704 ++arity
, argtype
= TREE_CHAIN (argtype
))
10706 tree t
= TREE_VALUE (argtype
);
10708 if (same_type_p (t
, const_string_type_node
))
10709 maybe_raw_p
= true;
10712 return false; /* Found ellipsis. */
10714 if (!maybe_raw_p
|| arity
!= 1)
10721 /* Return true if a user-defined literal operator has one of the allowed
10725 check_literal_operator_args (const_tree decl
,
10726 bool *long_long_unsigned_p
, bool *long_double_p
)
10728 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
10730 *long_long_unsigned_p
= false;
10731 *long_double_p
= false;
10732 if (processing_template_decl
|| processing_specialization
)
10733 return argtypes
== void_list_node
;
10740 /* Count the number and type of arguments and check for ellipsis. */
10741 for (argtype
= argtypes
, arity
= 0;
10742 argtype
&& argtype
!= void_list_node
;
10743 argtype
= TREE_CHAIN (argtype
))
10745 tree t
= TREE_VALUE (argtype
);
10748 if (TYPE_PTR_P (t
))
10750 bool maybe_raw_p
= false;
10752 if (cp_type_quals (t
) != TYPE_QUAL_CONST
)
10754 t
= TYPE_MAIN_VARIANT (t
);
10755 if ((maybe_raw_p
= same_type_p (t
, char_type_node
))
10756 || same_type_p (t
, wchar_type_node
)
10757 || same_type_p (t
, char8_type_node
)
10758 || same_type_p (t
, char16_type_node
)
10759 || same_type_p (t
, char32_type_node
))
10761 argtype
= TREE_CHAIN (argtype
);
10764 t
= TREE_VALUE (argtype
);
10765 if (maybe_raw_p
&& argtype
== void_list_node
)
10767 else if (same_type_p (t
, size_type_node
))
10776 else if (same_type_p (t
, long_long_unsigned_type_node
))
10779 *long_long_unsigned_p
= true;
10781 else if (same_type_p (t
, long_double_type_node
))
10784 *long_double_p
= true;
10786 else if (same_type_p (t
, char_type_node
))
10788 else if (same_type_p (t
, wchar_type_node
))
10790 else if (same_type_p (t
, char8_type_node
))
10792 else if (same_type_p (t
, char16_type_node
))
10794 else if (same_type_p (t
, char32_type_node
))
10800 return false; /* Found ellipsis. */
10802 if (arity
!= max_arity
)
10809 /* Always returns false since unlike C90, C++ has no concept of implicit
10810 function declarations. */
10813 c_decl_implicit (const_tree
)