1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2015 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"
33 #include "fold-const.h"
34 #include "stor-layout.h"
38 #include "diagnostic.h"
42 #include "c-family/c-common.h"
43 #include "c-family/c-objc.h"
44 #include "c-family/c-ubsan.h"
47 static tree
cp_build_addr_expr_strict (tree
, tsubst_flags_t
);
48 static tree
cp_build_function_call (tree
, tree
, tsubst_flags_t
);
49 static tree
pfn_from_ptrmemfunc (tree
);
50 static tree
delta_from_ptrmemfunc (tree
);
51 static tree
convert_for_assignment (tree
, tree
, impl_conv_rhs
, tree
, int,
53 static tree
cp_pointer_int_sum (enum tree_code
, tree
, tree
, tsubst_flags_t
);
54 static tree
rationalize_conditional_expr (enum tree_code
, tree
,
56 static int comp_ptr_ttypes_real (tree
, tree
, int);
57 static bool comp_except_types (tree
, tree
, bool);
58 static bool comp_array_types (const_tree
, const_tree
, bool);
59 static tree
pointer_diff (tree
, tree
, tree
, tsubst_flags_t
);
60 static tree
get_delta_difference (tree
, tree
, bool, bool, tsubst_flags_t
);
61 static void casts_away_constness_r (tree
*, tree
*, tsubst_flags_t
);
62 static bool casts_away_constness (tree
, tree
, tsubst_flags_t
);
63 static bool maybe_warn_about_returning_address_of_local (tree
);
64 static tree
lookup_destructor (tree
, tree
, tree
, tsubst_flags_t
);
65 static void error_args_num (location_t
, tree
, bool);
66 static int convert_arguments (tree
, vec
<tree
, va_gc
> **, tree
, int,
69 /* Do `exp = require_complete_type (exp);' to make sure exp
70 does not have an incomplete type. (That includes void types.)
71 Returns error_mark_node if the VALUE does not have
72 complete type when this function returns. */
75 require_complete_type_sfinae (tree value
, tsubst_flags_t complain
)
79 if (processing_template_decl
|| value
== error_mark_node
)
82 if (TREE_CODE (value
) == OVERLOAD
)
83 type
= unknown_type_node
;
85 type
= TREE_TYPE (value
);
87 if (type
== error_mark_node
)
88 return error_mark_node
;
90 /* First, detect a valid value with a complete type. */
91 if (COMPLETE_TYPE_P (type
))
94 if (complete_type_or_maybe_complain (type
, value
, complain
))
97 return error_mark_node
;
101 require_complete_type (tree value
)
103 return require_complete_type_sfinae (value
, tf_warning_or_error
);
106 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
107 a template instantiation, do the instantiation. Returns TYPE,
108 whether or not it could be completed, unless something goes
109 horribly wrong, in which case the error_mark_node is returned. */
112 complete_type (tree type
)
114 if (type
== NULL_TREE
)
115 /* Rather than crash, we return something sure to cause an error
117 return error_mark_node
;
119 if (type
== error_mark_node
|| COMPLETE_TYPE_P (type
))
121 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
123 tree t
= complete_type (TREE_TYPE (type
));
124 unsigned int needs_constructing
, has_nontrivial_dtor
;
125 if (COMPLETE_TYPE_P (t
) && !dependent_type_p (type
))
128 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
130 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
131 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
133 TYPE_NEEDS_CONSTRUCTING (t
) = needs_constructing
;
134 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = has_nontrivial_dtor
;
137 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
138 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
143 /* Like complete_type, but issue an error if the TYPE cannot be completed.
144 VALUE is used for informative diagnostics.
145 Returns NULL_TREE if the type cannot be made complete. */
148 complete_type_or_maybe_complain (tree type
, tree value
, tsubst_flags_t complain
)
150 type
= complete_type (type
);
151 if (type
== error_mark_node
)
152 /* We already issued an error. */
154 else if (!COMPLETE_TYPE_P (type
))
156 if (complain
& tf_error
)
157 cxx_incomplete_type_diagnostic (value
, type
, DK_ERROR
);
165 complete_type_or_else (tree type
, tree value
)
167 return complete_type_or_maybe_complain (type
, value
, tf_warning_or_error
);
170 /* Return truthvalue of whether type of EXP is instantiated. */
173 type_unknown_p (const_tree exp
)
175 return (TREE_CODE (exp
) == TREE_LIST
176 || TREE_TYPE (exp
) == unknown_type_node
);
180 /* Return the common type of two parameter lists.
181 We assume that comptypes has already been done and returned 1;
182 if that isn't so, this may crash.
184 As an optimization, free the space we allocate if the parameter
185 lists are already common. */
188 commonparms (tree p1
, tree p2
)
190 tree oldargs
= p1
, newargs
, n
;
194 len
= list_length (p1
);
195 newargs
= tree_last (p1
);
197 if (newargs
== void_list_node
)
206 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
211 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
213 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
215 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
218 else if (! TREE_PURPOSE (p1
))
220 if (TREE_PURPOSE (p2
))
222 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
228 if (1 != simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)))
230 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
232 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
235 TREE_VALUE (n
) = merge_types (TREE_VALUE (p1
), TREE_VALUE (p2
));
238 TREE_VALUE (n
) = TREE_VALUE (p1
);
246 /* Given a type, perhaps copied for a typedef,
247 find the "original" version of it. */
249 original_type (tree t
)
251 int quals
= cp_type_quals (t
);
252 while (t
!= error_mark_node
253 && TYPE_NAME (t
) != NULL_TREE
)
255 tree x
= TYPE_NAME (t
);
256 if (TREE_CODE (x
) != TYPE_DECL
)
258 x
= DECL_ORIGINAL_TYPE (x
);
263 return cp_build_qualified_type (t
, quals
);
266 /* Return the common type for two arithmetic types T1 and T2 under the
267 usual arithmetic conversions. The default conversions have already
268 been applied, and enumerated types converted to their compatible
272 cp_common_type (tree t1
, tree t2
)
274 enum tree_code code1
= TREE_CODE (t1
);
275 enum tree_code code2
= TREE_CODE (t2
);
280 /* In what follows, we slightly generalize the rules given in [expr] so
281 as to deal with `long long' and `complex'. First, merge the
283 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
285 if (SCOPED_ENUM_P (t1
) || SCOPED_ENUM_P (t2
))
287 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
288 return build_type_attribute_variant (t1
, attributes
);
293 /* FIXME: Attributes. */
294 gcc_assert (ARITHMETIC_TYPE_P (t1
)
295 || VECTOR_TYPE_P (t1
)
296 || UNSCOPED_ENUM_P (t1
));
297 gcc_assert (ARITHMETIC_TYPE_P (t2
)
298 || VECTOR_TYPE_P (t2
)
299 || UNSCOPED_ENUM_P (t2
));
301 /* If one type is complex, form the common type of the non-complex
302 components, then make that complex. Use T1 or T2 if it is the
304 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
306 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
307 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
309 = type_after_usual_arithmetic_conversions (subtype1
, subtype2
);
311 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
312 return build_type_attribute_variant (t1
, attributes
);
313 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
314 return build_type_attribute_variant (t2
, attributes
);
316 return build_type_attribute_variant (build_complex_type (subtype
),
320 if (code1
== VECTOR_TYPE
)
322 /* When we get here we should have two vectors of the same size.
323 Just prefer the unsigned one if present. */
324 if (TYPE_UNSIGNED (t1
))
325 return build_type_attribute_variant (t1
, attributes
);
327 return build_type_attribute_variant (t2
, attributes
);
330 /* If only one is real, use it as the result. */
331 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
332 return build_type_attribute_variant (t1
, attributes
);
333 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
334 return build_type_attribute_variant (t2
, attributes
);
336 /* Both real or both integers; use the one with greater precision. */
337 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
338 return build_type_attribute_variant (t1
, attributes
);
339 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
340 return build_type_attribute_variant (t2
, attributes
);
342 /* The types are the same; no need to do anything fancy. */
343 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
344 return build_type_attribute_variant (t1
, attributes
);
346 if (code1
!= REAL_TYPE
)
348 /* If one is unsigned long long, then convert the other to unsigned
350 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_unsigned_type_node
)
351 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_unsigned_type_node
))
352 return build_type_attribute_variant (long_long_unsigned_type_node
,
354 /* If one is a long long, and the other is an unsigned long, and
355 long long can represent all the values of an unsigned long, then
356 convert to a long long. Otherwise, convert to an unsigned long
357 long. Otherwise, if either operand is long long, convert the
360 Since we're here, we know the TYPE_PRECISION is the same;
361 therefore converting to long long cannot represent all the values
362 of an unsigned long, so we choose unsigned long long in that
364 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_integer_type_node
)
365 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_integer_type_node
))
367 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
368 ? long_long_unsigned_type_node
369 : long_long_integer_type_node
);
370 return build_type_attribute_variant (t
, attributes
);
373 /* Go through the same procedure, but for longs. */
374 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_unsigned_type_node
)
375 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_unsigned_type_node
))
376 return build_type_attribute_variant (long_unsigned_type_node
,
378 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_integer_type_node
)
379 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_integer_type_node
))
381 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
382 ? long_unsigned_type_node
: long_integer_type_node
);
383 return build_type_attribute_variant (t
, attributes
);
386 /* For __intN types, either the type is __int128 (and is lower
387 priority than the types checked above, but higher than other
388 128-bit types) or it's known to not be the same size as other
389 types (enforced in toplev.c). Prefer the unsigned type. */
390 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
392 if (int_n_enabled_p
[i
]
393 && (same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].signed_type
)
394 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].signed_type
)
395 || same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].unsigned_type
)
396 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].unsigned_type
)))
398 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
399 ? int_n_trees
[i
].unsigned_type
400 : int_n_trees
[i
].signed_type
);
401 return build_type_attribute_variant (t
, attributes
);
405 /* Otherwise prefer the unsigned one. */
406 if (TYPE_UNSIGNED (t1
))
407 return build_type_attribute_variant (t1
, attributes
);
409 return build_type_attribute_variant (t2
, attributes
);
413 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_double_type_node
)
414 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_double_type_node
))
415 return build_type_attribute_variant (long_double_type_node
,
417 if (same_type_p (TYPE_MAIN_VARIANT (t1
), double_type_node
)
418 || same_type_p (TYPE_MAIN_VARIANT (t2
), double_type_node
))
419 return build_type_attribute_variant (double_type_node
,
421 if (same_type_p (TYPE_MAIN_VARIANT (t1
), float_type_node
)
422 || same_type_p (TYPE_MAIN_VARIANT (t2
), float_type_node
))
423 return build_type_attribute_variant (float_type_node
,
426 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
427 the standard C++ floating-point types. Logic earlier in this
428 function has already eliminated the possibility that
429 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
430 compelling reason to choose one or the other. */
431 return build_type_attribute_variant (t1
, attributes
);
435 /* T1 and T2 are arithmetic or enumeration types. Return the type
436 that will result from the "usual arithmetic conversions" on T1 and
437 T2 as described in [expr]. */
440 type_after_usual_arithmetic_conversions (tree t1
, tree t2
)
442 gcc_assert (ARITHMETIC_TYPE_P (t1
)
443 || VECTOR_TYPE_P (t1
)
444 || UNSCOPED_ENUM_P (t1
));
445 gcc_assert (ARITHMETIC_TYPE_P (t2
)
446 || VECTOR_TYPE_P (t2
)
447 || UNSCOPED_ENUM_P (t2
));
449 /* Perform the integral promotions. We do not promote real types here. */
450 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1
)
451 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2
))
453 t1
= type_promotes_to (t1
);
454 t2
= type_promotes_to (t2
);
457 return cp_common_type (t1
, t2
);
461 composite_pointer_error (diagnostic_t kind
, tree t1
, tree t2
,
462 composite_pointer_operation operation
)
467 emit_diagnostic (kind
, input_location
, 0,
468 "comparison between "
469 "distinct pointer types %qT and %qT lacks a cast",
473 emit_diagnostic (kind
, input_location
, 0,
474 "conversion between "
475 "distinct pointer types %qT and %qT lacks a cast",
478 case CPO_CONDITIONAL_EXPR
:
479 emit_diagnostic (kind
, input_location
, 0,
480 "conditional expression between "
481 "distinct pointer types %qT and %qT lacks a cast",
489 /* Subroutine of composite_pointer_type to implement the recursive
490 case. See that function for documentation of the parameters. */
493 composite_pointer_type_r (tree t1
, tree t2
,
494 composite_pointer_operation operation
,
495 tsubst_flags_t complain
)
502 /* Determine the types pointed to by T1 and T2. */
505 pointee1
= TREE_TYPE (t1
);
506 pointee2
= TREE_TYPE (t2
);
510 pointee1
= TYPE_PTRMEM_POINTED_TO_TYPE (t1
);
511 pointee2
= TYPE_PTRMEM_POINTED_TO_TYPE (t2
);
516 Otherwise, the composite pointer type is a pointer type
517 similar (_conv.qual_) to the type of one of the operands,
518 with a cv-qualification signature (_conv.qual_) that is the
519 union of the cv-qualification signatures of the operand
521 if (same_type_ignoring_top_level_qualifiers_p (pointee1
, pointee2
))
522 result_type
= pointee1
;
523 else if ((TYPE_PTR_P (pointee1
) && TYPE_PTR_P (pointee2
))
524 || (TYPE_PTRMEM_P (pointee1
) && TYPE_PTRMEM_P (pointee2
)))
526 result_type
= composite_pointer_type_r (pointee1
, pointee2
, operation
,
528 if (result_type
== error_mark_node
)
529 return error_mark_node
;
533 if (complain
& tf_error
)
534 composite_pointer_error (DK_PERMERROR
, t1
, t2
, operation
);
536 return error_mark_node
;
537 result_type
= void_type_node
;
539 result_type
= cp_build_qualified_type (result_type
,
540 (cp_type_quals (pointee1
)
541 | cp_type_quals (pointee2
)));
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 (DK_PERMERROR
, t1
, t2
, operation
);
552 return error_mark_node
;
554 result_type
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
558 result_type
= build_pointer_type (result_type
);
560 /* Merge the attributes. */
561 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
562 return build_type_attribute_variant (result_type
, attributes
);
565 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
566 ARG1 and ARG2 are the values with those types. The OPERATION is to
567 describe the operation between the pointer types,
568 in case an error occurs.
570 This routine also implements the computation of a common type for
571 pointers-to-members as per [expr.eq]. */
574 composite_pointer_type (tree t1
, tree t2
, tree arg1
, tree arg2
,
575 composite_pointer_operation operation
,
576 tsubst_flags_t complain
)
583 If one operand is a null pointer constant, the composite pointer
584 type is the type of the other operand. */
585 if (null_ptr_cst_p (arg1
))
587 if (null_ptr_cst_p (arg2
))
594 If one of the operands has type "pointer to cv1 void*", then
595 the other has type "pointer to cv2T", and the composite pointer
596 type is "pointer to cv12 void", where cv12 is the union of cv1
599 If either type is a pointer to void, make sure it is T1. */
600 if (TYPE_PTR_P (t2
) && VOID_TYPE_P (TREE_TYPE (t2
)))
603 /* Now, if T1 is a pointer to void, merge the qualifiers. */
604 if (TYPE_PTR_P (t1
) && VOID_TYPE_P (TREE_TYPE (t1
)))
609 if (TYPE_PTRFN_P (t2
))
611 if (complain
& tf_error
)
616 pedwarn (input_location
, OPT_Wpedantic
,
617 "ISO C++ forbids comparison between pointer "
618 "of type %<void *%> and pointer-to-function");
621 pedwarn (input_location
, OPT_Wpedantic
,
622 "ISO C++ forbids conversion between pointer "
623 "of type %<void *%> and pointer-to-function");
625 case CPO_CONDITIONAL_EXPR
:
626 pedwarn (input_location
, OPT_Wpedantic
,
627 "ISO C++ forbids conditional expression between "
628 "pointer of type %<void *%> and "
629 "pointer-to-function");
636 return error_mark_node
;
639 = cp_build_qualified_type (void_type_node
,
640 (cp_type_quals (TREE_TYPE (t1
))
641 | cp_type_quals (TREE_TYPE (t2
))));
642 result_type
= build_pointer_type (result_type
);
643 /* Merge the attributes. */
644 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
645 return build_type_attribute_variant (result_type
, attributes
);
648 if (c_dialect_objc () && TYPE_PTR_P (t1
)
651 if (objc_have_common_type (t1
, t2
, -3, NULL_TREE
))
652 return objc_common_type (t1
, t2
);
655 /* [expr.eq] permits the application of a pointer conversion to
656 bring the pointers to a common type. */
657 if (TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
)
658 && CLASS_TYPE_P (TREE_TYPE (t1
))
659 && CLASS_TYPE_P (TREE_TYPE (t2
))
660 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1
),
663 class1
= TREE_TYPE (t1
);
664 class2
= TREE_TYPE (t2
);
666 if (DERIVED_FROM_P (class1
, class2
))
667 t2
= (build_pointer_type
668 (cp_build_qualified_type (class1
, cp_type_quals (class2
))));
669 else if (DERIVED_FROM_P (class2
, class1
))
670 t1
= (build_pointer_type
671 (cp_build_qualified_type (class2
, cp_type_quals (class1
))));
674 if (complain
& tf_error
)
675 composite_pointer_error (DK_ERROR
, t1
, t2
, operation
);
676 return error_mark_node
;
679 /* [expr.eq] permits the application of a pointer-to-member
680 conversion to change the class type of one of the types. */
681 else if (TYPE_PTRMEM_P (t1
)
682 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
683 TYPE_PTRMEM_CLASS_TYPE (t2
)))
685 class1
= TYPE_PTRMEM_CLASS_TYPE (t1
);
686 class2
= TYPE_PTRMEM_CLASS_TYPE (t2
);
688 if (DERIVED_FROM_P (class1
, class2
))
689 t1
= build_ptrmem_type (class2
, TYPE_PTRMEM_POINTED_TO_TYPE (t1
));
690 else if (DERIVED_FROM_P (class2
, class1
))
691 t2
= build_ptrmem_type (class1
, TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
694 if (complain
& tf_error
)
698 error ("comparison between distinct "
699 "pointer-to-member types %qT and %qT lacks a cast",
703 error ("conversion between distinct "
704 "pointer-to-member types %qT and %qT lacks a cast",
707 case CPO_CONDITIONAL_EXPR
:
708 error ("conditional expression between distinct "
709 "pointer-to-member types %qT and %qT lacks a cast",
715 return error_mark_node
;
719 return composite_pointer_type_r (t1
, t2
, operation
, complain
);
722 /* Return the merged type of two types.
723 We assume that comptypes has already been done and returned 1;
724 if that isn't so, this may crash.
726 This just combines attributes and default arguments; any other
727 differences would cause the two types to compare unalike. */
730 merge_types (tree t1
, tree t2
)
732 enum tree_code code1
;
733 enum tree_code code2
;
736 /* Save time if the two types are the same. */
739 if (original_type (t1
) == original_type (t2
))
742 /* If one type is nonsense, use the other. */
743 if (t1
== error_mark_node
)
745 if (t2
== error_mark_node
)
748 /* Handle merging an auto redeclaration with a previous deduced
753 /* Merge the attributes. */
754 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
756 if (TYPE_PTRMEMFUNC_P (t1
))
757 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
758 if (TYPE_PTRMEMFUNC_P (t2
))
759 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
761 code1
= TREE_CODE (t1
);
762 code2
= TREE_CODE (t2
);
765 gcc_assert (code1
== TYPENAME_TYPE
|| code2
== TYPENAME_TYPE
);
766 if (code1
== TYPENAME_TYPE
)
768 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
769 code1
= TREE_CODE (t1
);
773 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
774 code2
= TREE_CODE (t2
);
782 /* For two pointers, do this recursively on the target type. */
784 tree target
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
785 int quals
= cp_type_quals (t1
);
787 if (code1
== POINTER_TYPE
)
789 t1
= build_pointer_type (target
);
790 if (TREE_CODE (target
) == METHOD_TYPE
)
791 t1
= build_ptrmemfunc_type (t1
);
794 t1
= cp_build_reference_type (target
, TYPE_REF_IS_RVALUE (t1
));
795 t1
= build_type_attribute_variant (t1
, attributes
);
796 t1
= cp_build_qualified_type (t1
, quals
);
805 quals
= cp_type_quals (t1
);
806 pointee
= merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1
),
807 TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
808 t1
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
810 t1
= cp_build_qualified_type (t1
, quals
);
816 tree elt
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
817 /* Save space: see if the result is identical to one of the args. */
818 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
819 return build_type_attribute_variant (t1
, attributes
);
820 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
821 return build_type_attribute_variant (t2
, attributes
);
822 /* Merge the element types, and have a size if either arg has one. */
823 t1
= build_cplus_array_type
824 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
829 /* Function types: prefer the one that specified arg types.
830 If both do, merge the arg types. Also merge the return types. */
832 tree valtype
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
833 tree p1
= TYPE_ARG_TYPES (t1
);
834 tree p2
= TYPE_ARG_TYPES (t2
);
837 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
839 /* Save space: see if the result is identical to one of the args. */
840 if (valtype
== TREE_TYPE (t1
) && ! p2
)
841 return cp_build_type_attribute_variant (t1
, attributes
);
842 if (valtype
== TREE_TYPE (t2
) && ! p1
)
843 return cp_build_type_attribute_variant (t2
, attributes
);
845 /* Simple way if one arg fails to specify argument types. */
846 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
848 else if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
851 parms
= commonparms (p1
, p2
);
853 rval
= build_function_type (valtype
, parms
);
854 gcc_assert (type_memfn_quals (t1
) == type_memfn_quals (t2
));
855 gcc_assert (type_memfn_rqual (t1
) == type_memfn_rqual (t2
));
856 rval
= apply_memfn_quals (rval
,
857 type_memfn_quals (t1
),
858 type_memfn_rqual (t1
));
859 raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
860 TYPE_RAISES_EXCEPTIONS (t2
));
861 t1
= build_exception_variant (rval
, raises
);
862 if (late_return_type_p
)
863 TYPE_HAS_LATE_RETURN_TYPE (t1
) = 1;
869 /* Get this value the long way, since TYPE_METHOD_BASETYPE
870 is just the main variant of this. */
871 tree basetype
= class_of_this_parm (t2
);
872 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
873 TYPE_RAISES_EXCEPTIONS (t2
));
874 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
876 bool late_return_type_1_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
877 bool late_return_type_2_p
= TYPE_HAS_LATE_RETURN_TYPE (t2
);
879 /* If this was a member function type, get back to the
880 original type of type member function (i.e., without
881 the class instance variable up front. */
882 t1
= build_function_type (TREE_TYPE (t1
),
883 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
884 t2
= build_function_type (TREE_TYPE (t2
),
885 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
886 t3
= merge_types (t1
, t2
);
887 t3
= build_method_type_directly (basetype
, TREE_TYPE (t3
),
888 TYPE_ARG_TYPES (t3
));
889 t1
= build_exception_variant (t3
, raises
);
890 t1
= build_ref_qualified_type (t1
, rqual
);
891 if (late_return_type_1_p
)
892 TYPE_HAS_LATE_RETURN_TYPE (t1
) = 1;
893 if (late_return_type_2_p
)
894 TYPE_HAS_LATE_RETURN_TYPE (t2
) = 1;
899 /* There is no need to merge attributes into a TYPENAME_TYPE.
900 When the type is instantiated it will have whatever
901 attributes result from the instantiation. */
907 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
909 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
912 return cp_build_type_attribute_variant (t1
, attributes
);
915 /* Return the ARRAY_TYPE type without its domain. */
918 strip_array_domain (tree type
)
921 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
922 if (TYPE_DOMAIN (type
) == NULL_TREE
)
924 t2
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
925 return cp_build_type_attribute_variant (t2
, TYPE_ATTRIBUTES (type
));
928 /* Wrapper around cp_common_type that is used by c-common.c and other
929 front end optimizations that remove promotions.
931 Return the common type for two arithmetic types T1 and T2 under the
932 usual arithmetic conversions. The default conversions have already
933 been applied, and enumerated types converted to their compatible
937 common_type (tree t1
, tree t2
)
939 /* If one type is nonsense, use the other */
940 if (t1
== error_mark_node
)
942 if (t2
== error_mark_node
)
945 return cp_common_type (t1
, t2
);
948 /* Return the common type of two pointer types T1 and T2. This is the
949 type for the result of most arithmetic operations if the operands
950 have the given two types.
952 We assume that comp_target_types has already been done and returned
953 nonzero; if that isn't so, this may crash. */
956 common_pointer_type (tree t1
, tree t2
)
958 gcc_assert ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
959 || (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
960 || (TYPE_PTRMEMFUNC_P (t1
) && TYPE_PTRMEMFUNC_P (t2
)));
962 return composite_pointer_type (t1
, t2
, error_mark_node
, error_mark_node
,
963 CPO_CONVERSION
, tf_warning_or_error
);
966 /* Compare two exception specifier types for exactness or subsetness, if
967 allowed. Returns false for mismatch, true for match (same, or
970 [except.spec] "If a class X ... objects of class X or any class publicly
971 and unambiguously derived from X. Similarly, if a pointer type Y * ...
972 exceptions of type Y * or that are pointers to any type publicly and
973 unambiguously derived from Y. Otherwise a function only allows exceptions
974 that have the same type ..."
975 This does not mention cv qualifiers and is different to what throw
976 [except.throw] and catch [except.catch] will do. They will ignore the
977 top level cv qualifiers, and allow qualifiers in the pointer to class
980 We implement the letter of the standard. */
983 comp_except_types (tree a
, tree b
, bool exact
)
985 if (same_type_p (a
, b
))
989 if (cp_type_quals (a
) || cp_type_quals (b
))
992 if (TYPE_PTR_P (a
) && TYPE_PTR_P (b
))
996 if (cp_type_quals (a
) || cp_type_quals (b
))
1000 if (TREE_CODE (a
) != RECORD_TYPE
1001 || TREE_CODE (b
) != RECORD_TYPE
)
1004 if (publicly_uniquely_derived_p (a
, b
))
1010 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1011 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1012 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1013 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1014 are unordered, but we've already filtered out duplicates. Most lists will
1015 be in order, we should try to make use of that. */
1018 comp_except_specs (const_tree t1
, const_tree t2
, int exact
)
1027 /* First handle noexcept. */
1028 if (exact
< ce_exact
)
1030 /* noexcept(false) is compatible with no exception-specification,
1031 and stricter than any spec. */
1032 if (t1
== noexcept_false_spec
)
1033 return t2
== NULL_TREE
|| exact
== ce_derived
;
1034 /* Even a derived noexcept(false) is compatible with no
1035 exception-specification. */
1036 if (t2
== noexcept_false_spec
)
1037 return t1
== NULL_TREE
;
1039 /* Otherwise, if we aren't looking for an exact match, noexcept is
1040 equivalent to throw(). */
1041 if (t1
== noexcept_true_spec
)
1042 t1
= empty_except_spec
;
1043 if (t2
== noexcept_true_spec
)
1044 t2
= empty_except_spec
;
1047 /* If any noexcept is left, it is only comparable to itself;
1048 either we're looking for an exact match or we're redeclaring a
1049 template with dependent noexcept. */
1050 if ((t1
&& TREE_PURPOSE (t1
))
1051 || (t2
&& TREE_PURPOSE (t2
)))
1053 && cp_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)));
1055 if (t1
== NULL_TREE
) /* T1 is ... */
1056 return t2
== NULL_TREE
|| exact
== ce_derived
;
1057 if (!TREE_VALUE (t1
)) /* t1 is EMPTY */
1058 return t2
!= NULL_TREE
&& !TREE_VALUE (t2
);
1059 if (t2
== NULL_TREE
) /* T2 is ... */
1061 if (TREE_VALUE (t1
) && !TREE_VALUE (t2
)) /* T2 is EMPTY, T1 is not */
1062 return exact
== ce_derived
;
1064 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1065 Count how many we find, to determine exactness. For exact matching and
1066 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1068 for (base
= t1
; t2
!= NULL_TREE
; t2
= TREE_CHAIN (t2
))
1070 for (probe
= base
; probe
!= NULL_TREE
; probe
= TREE_CHAIN (probe
))
1072 tree a
= TREE_VALUE (probe
);
1073 tree b
= TREE_VALUE (t2
);
1075 if (comp_except_types (a
, b
, exact
))
1077 if (probe
== base
&& exact
> ce_derived
)
1078 base
= TREE_CHAIN (probe
);
1083 if (probe
== NULL_TREE
)
1086 return exact
== ce_derived
|| base
== NULL_TREE
|| length
== list_length (t1
);
1089 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1090 [] can match [size]. */
1093 comp_array_types (const_tree t1
, const_tree t2
, bool allow_redeclaration
)
1102 /* The type of the array elements must be the same. */
1103 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1106 d1
= TYPE_DOMAIN (t1
);
1107 d2
= TYPE_DOMAIN (t2
);
1112 /* If one of the arrays is dimensionless, and the other has a
1113 dimension, they are of different types. However, it is valid to
1121 declarations for an array object can specify
1122 array types that differ by the presence or absence of a major
1123 array bound (_dcl.array_). */
1125 return allow_redeclaration
;
1127 /* Check that the dimensions are the same. */
1129 if (!cp_tree_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
)))
1131 max1
= TYPE_MAX_VALUE (d1
);
1132 max2
= TYPE_MAX_VALUE (d2
);
1134 if (!cp_tree_equal (max1
, max2
))
1140 /* Compare the relative position of T1 and T2 into their respective
1141 template parameter list.
1142 T1 and T2 must be template parameter types.
1143 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1146 comp_template_parms_position (tree t1
, tree t2
)
1148 tree index1
, index2
;
1149 gcc_assert (t1
&& t2
1150 && TREE_CODE (t1
) == TREE_CODE (t2
)
1151 && (TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
1152 || TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
1153 || TREE_CODE (t1
) == TEMPLATE_TYPE_PARM
));
1155 index1
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1
));
1156 index2
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2
));
1158 /* Then compare their relative position. */
1159 if (TEMPLATE_PARM_IDX (index1
) != TEMPLATE_PARM_IDX (index2
)
1160 || TEMPLATE_PARM_LEVEL (index1
) != TEMPLATE_PARM_LEVEL (index2
)
1161 || (TEMPLATE_PARM_PARAMETER_PACK (index1
)
1162 != TEMPLATE_PARM_PARAMETER_PACK (index2
)))
1165 /* In C++14 we can end up comparing 'auto' to a normal template
1166 parameter. Don't confuse them. */
1167 if (cxx_dialect
>= cxx14
&& (is_auto (t1
) || is_auto (t2
)))
1168 return TYPE_IDENTIFIER (t1
) == TYPE_IDENTIFIER (t2
);
1173 /* Subroutine in comptypes. */
1176 structural_comptypes (tree t1
, tree t2
, int strict
)
1181 /* Suppress errors caused by previously reported errors. */
1182 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1185 gcc_assert (TYPE_P (t1
) && TYPE_P (t2
));
1187 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1188 current instantiation. */
1189 if (TREE_CODE (t1
) == TYPENAME_TYPE
)
1190 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
1192 if (TREE_CODE (t2
) == TYPENAME_TYPE
)
1193 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
1195 if (TYPE_PTRMEMFUNC_P (t1
))
1196 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
1197 if (TYPE_PTRMEMFUNC_P (t2
))
1198 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
1200 /* Different classes of types can't be compatible. */
1201 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1204 /* Qualifiers must match. For array types, we will check when we
1205 recur on the array element types. */
1206 if (TREE_CODE (t1
) != ARRAY_TYPE
1207 && cp_type_quals (t1
) != cp_type_quals (t2
))
1209 if (TREE_CODE (t1
) == FUNCTION_TYPE
1210 && type_memfn_quals (t1
) != type_memfn_quals (t2
))
1212 /* Need to check this before TYPE_MAIN_VARIANT.
1213 FIXME function qualifiers should really change the main variant. */
1214 if ((TREE_CODE (t1
) == FUNCTION_TYPE
1215 || TREE_CODE (t1
) == METHOD_TYPE
)
1216 && type_memfn_rqual (t1
) != type_memfn_rqual (t2
))
1218 if (TYPE_FOR_JAVA (t1
) != TYPE_FOR_JAVA (t2
))
1221 /* Allow for two different type nodes which have essentially the same
1222 definition. Note that we already checked for equality of the type
1223 qualifiers (just above). */
1225 if (TREE_CODE (t1
) != ARRAY_TYPE
1226 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1230 /* Compare the types. Break out if they could be the same. */
1231 switch (TREE_CODE (t1
))
1235 /* All void and bool types are the same. */
1239 case FIXED_POINT_TYPE
:
1241 /* With these nodes, we can't determine type equivalence by
1242 looking at what is stored in the nodes themselves, because
1243 two nodes might have different TYPE_MAIN_VARIANTs but still
1244 represent the same type. For example, wchar_t and int could
1245 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1246 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1247 and are distinct types. On the other hand, int and the
1250 typedef int INT __attribute((may_alias));
1252 have identical properties, different TYPE_MAIN_VARIANTs, but
1253 represent the same type. The canonical type system keeps
1254 track of equivalence in this case, so we fall back on it. */
1255 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1257 case TEMPLATE_TEMPLATE_PARM
:
1258 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1259 if (!comp_template_parms_position (t1
, t2
))
1261 if (!comp_template_parms
1262 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1
)),
1263 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2
))))
1265 if (TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
)
1267 /* Don't check inheritance. */
1268 strict
= COMPARE_STRICT
;
1273 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
1274 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
1275 || TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1276 && comp_template_args (TYPE_TI_ARGS (t1
), TYPE_TI_ARGS (t2
)))
1279 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
1281 else if ((strict
& COMPARE_DERIVED
) && DERIVED_FROM_P (t2
, t1
))
1287 if (!comptypes (TYPE_OFFSET_BASETYPE (t1
), TYPE_OFFSET_BASETYPE (t2
),
1288 strict
& ~COMPARE_REDECLARATION
))
1290 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1294 case REFERENCE_TYPE
:
1295 if (TYPE_REF_IS_RVALUE (t1
) != TYPE_REF_IS_RVALUE (t2
))
1297 /* fall through to checks for pointer types */
1300 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1301 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1307 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1309 if (!compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)))
1314 /* Target types must match incl. qualifiers. */
1315 if (!comp_array_types (t1
, t2
, !!(strict
& COMPARE_REDECLARATION
)))
1319 case TEMPLATE_TYPE_PARM
:
1320 /* If T1 and T2 don't have the same relative position in their
1321 template parameters set, they can't be equal. */
1322 if (!comp_template_parms_position (t1
, t2
))
1327 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1
),
1328 TYPENAME_TYPE_FULLNAME (t2
)))
1330 /* Qualifiers don't matter on scopes. */
1331 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1
),
1336 case UNBOUND_CLASS_TEMPLATE
:
1337 if (!cp_tree_equal (TYPE_IDENTIFIER (t1
), TYPE_IDENTIFIER (t2
)))
1339 if (!same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
)))
1344 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1349 if (TYPE_VECTOR_SUBPARTS (t1
) != TYPE_VECTOR_SUBPARTS (t2
)
1350 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1354 case TYPE_PACK_EXPANSION
:
1355 return (same_type_p (PACK_EXPANSION_PATTERN (t1
),
1356 PACK_EXPANSION_PATTERN (t2
))
1357 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1
),
1358 PACK_EXPANSION_EXTRA_ARGS (t2
)));
1361 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1
)
1362 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2
)
1363 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1
)
1364 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2
))
1365 || (DECLTYPE_FOR_LAMBDA_PROXY (t1
)
1366 != DECLTYPE_FOR_LAMBDA_PROXY (t2
))
1367 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1
),
1368 DECLTYPE_TYPE_EXPR (t2
)))
1372 case UNDERLYING_TYPE
:
1373 return same_type_p (UNDERLYING_TYPE_TYPE (t1
),
1374 UNDERLYING_TYPE_TYPE (t2
));
1380 /* If we get here, we know that from a target independent POV the
1381 types are the same. Make sure the target attributes are also
1383 return comp_type_attributes (t1
, t2
);
1386 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1387 is a bitwise-or of the COMPARE_* flags. */
1390 comptypes (tree t1
, tree t2
, int strict
)
1392 if (strict
== COMPARE_STRICT
)
1397 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1400 if (TYPE_STRUCTURAL_EQUALITY_P (t1
) || TYPE_STRUCTURAL_EQUALITY_P (t2
))
1401 /* At least one of the types requires structural equality, so
1402 perform a deep check. */
1403 return structural_comptypes (t1
, t2
, strict
);
1405 #ifdef ENABLE_CHECKING
1406 if (USE_CANONICAL_TYPES
)
1408 bool result
= structural_comptypes (t1
, t2
, strict
);
1410 if (result
&& TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1411 /* The two types are structurally equivalent, but their
1412 canonical types were different. This is a failure of the
1413 canonical type propagation code.*/
1415 ("canonical types differ for identical types %T and %T",
1417 else if (!result
&& TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
))
1418 /* Two types are structurally different, but the canonical
1419 types are the same. This means we were over-eager in
1420 assigning canonical types. */
1422 ("same canonical type node for different types %T and %T",
1428 if (USE_CANONICAL_TYPES
)
1429 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1432 return structural_comptypes (t1
, t2
, strict
);
1434 else if (strict
== COMPARE_STRUCTURAL
)
1435 return structural_comptypes (t1
, t2
, COMPARE_STRICT
);
1437 return structural_comptypes (t1
, t2
, strict
);
1440 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1441 top-level qualifiers. */
1444 same_type_ignoring_top_level_qualifiers_p (tree type1
, tree type2
)
1446 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1449 return same_type_p (TYPE_MAIN_VARIANT (type1
), TYPE_MAIN_VARIANT (type2
));
1452 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1455 at_least_as_qualified_p (const_tree type1
, const_tree type2
)
1457 int q1
= cp_type_quals (type1
);
1458 int q2
= cp_type_quals (type2
);
1460 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1461 return (q1
& q2
) == q2
;
1464 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1465 more cv-qualified that TYPE1, and 0 otherwise. */
1468 comp_cv_qualification (int q1
, int q2
)
1473 if ((q1
& q2
) == q2
)
1475 else if ((q1
& q2
) == q1
)
1482 comp_cv_qualification (const_tree type1
, const_tree type2
)
1484 int q1
= cp_type_quals (type1
);
1485 int q2
= cp_type_quals (type2
);
1486 return comp_cv_qualification (q1
, q2
);
1489 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1490 subset of the cv-qualification signature of TYPE2, and the types
1491 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1494 comp_cv_qual_signature (tree type1
, tree type2
)
1496 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1498 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1504 /* Subroutines of `comptypes'. */
1506 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1507 equivalent in the sense that functions with those parameter types
1508 can have equivalent types. The two lists must be equivalent,
1509 element by element. */
1512 compparms (const_tree parms1
, const_tree parms2
)
1516 /* An unspecified parmlist matches any specified parmlist
1517 whose argument types don't need default promotions. */
1519 for (t1
= parms1
, t2
= parms2
;
1521 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1523 /* If one parmlist is shorter than the other,
1524 they fail to match. */
1527 if (!same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1534 /* Process a sizeof or alignof expression where the operand is a
1538 cxx_sizeof_or_alignof_type (tree type
, enum tree_code op
, bool complain
)
1543 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
1544 if (type
== error_mark_node
)
1545 return error_mark_node
;
1547 type
= non_reference (type
);
1548 if (TREE_CODE (type
) == METHOD_TYPE
)
1551 pedwarn (input_location
, OPT_Wpointer_arith
,
1552 "invalid application of %qs to a member function",
1553 operator_name_info
[(int) op
].name
);
1555 return error_mark_node
;
1556 value
= size_one_node
;
1559 dependent_p
= dependent_type_p (type
);
1561 complete_type (type
);
1563 /* VLA types will have a non-constant size. In the body of an
1564 uninstantiated template, we don't need to try to compute the
1565 value, because the sizeof expression is not an integral
1566 constant expression in that case. And, if we do try to
1567 compute the value, we'll likely end up with SAVE_EXPRs, which
1568 the template substitution machinery does not expect to see. */
1569 || (processing_template_decl
1570 && COMPLETE_TYPE_P (type
)
1571 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
))
1573 value
= build_min (op
, size_type_node
, type
);
1574 TREE_READONLY (value
) = 1;
1578 return c_sizeof_or_alignof_type (input_location
, complete_type (type
),
1579 op
== SIZEOF_EXPR
, false,
1583 /* Return the size of the type, without producing any warnings for
1584 types whose size cannot be taken. This routine should be used only
1585 in some other routine that has already produced a diagnostic about
1586 using the size of such a type. */
1588 cxx_sizeof_nowarn (tree type
)
1590 if (TREE_CODE (type
) == FUNCTION_TYPE
1591 || VOID_TYPE_P (type
)
1592 || TREE_CODE (type
) == ERROR_MARK
)
1593 return size_one_node
;
1594 else if (!COMPLETE_TYPE_P (type
))
1595 return size_zero_node
;
1597 return cxx_sizeof_or_alignof_type (type
, SIZEOF_EXPR
, false);
1600 /* Process a sizeof expression where the operand is an expression. */
1603 cxx_sizeof_expr (tree e
, tsubst_flags_t complain
)
1605 if (e
== error_mark_node
)
1606 return error_mark_node
;
1608 if (processing_template_decl
)
1610 e
= build_min (SIZEOF_EXPR
, size_type_node
, e
);
1611 TREE_SIDE_EFFECTS (e
) = 0;
1612 TREE_READONLY (e
) = 1;
1617 /* To get the size of a static data member declared as an array of
1618 unknown bound, we need to instantiate it. */
1620 && VAR_HAD_UNKNOWN_BOUND (e
)
1621 && DECL_TEMPLATE_INSTANTIATION (e
))
1622 instantiate_decl (e
, /*defer_ok*/true, /*expl_inst_mem*/false);
1624 if (TREE_CODE (e
) == PARM_DECL
1625 && DECL_ARRAY_PARAMETER_P (e
)
1626 && (complain
& tf_warning
))
1628 if (warning (OPT_Wsizeof_array_argument
, "%<sizeof%> on array function "
1629 "parameter %qE will return size of %qT", e
, TREE_TYPE (e
)))
1630 inform (DECL_SOURCE_LOCATION (e
), "declared here");
1633 e
= mark_type_use (e
);
1635 if (TREE_CODE (e
) == COMPONENT_REF
1636 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
1637 && DECL_C_BIT_FIELD (TREE_OPERAND (e
, 1)))
1639 if (complain
& tf_error
)
1640 error ("invalid application of %<sizeof%> to a bit-field");
1642 return error_mark_node
;
1645 else if (is_overloaded_fn (e
))
1647 if (complain
& tf_error
)
1648 permerror (input_location
, "ISO C++ forbids applying %<sizeof%> to an expression of "
1651 return error_mark_node
;
1654 else if (type_unknown_p (e
))
1656 if (complain
& tf_error
)
1657 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1659 return error_mark_node
;
1665 return cxx_sizeof_or_alignof_type (e
, SIZEOF_EXPR
, complain
& tf_error
);
1668 /* Implement the __alignof keyword: Return the minimum required
1669 alignment of E, measured in bytes. For VAR_DECL's and
1670 FIELD_DECL's return DECL_ALIGN (which can be set from an
1671 "aligned" __attribute__ specification). */
1674 cxx_alignof_expr (tree e
, tsubst_flags_t complain
)
1678 if (e
== error_mark_node
)
1679 return error_mark_node
;
1681 if (processing_template_decl
)
1683 e
= build_min (ALIGNOF_EXPR
, size_type_node
, e
);
1684 TREE_SIDE_EFFECTS (e
) = 0;
1685 TREE_READONLY (e
) = 1;
1690 e
= mark_type_use (e
);
1693 t
= size_int (DECL_ALIGN_UNIT (e
));
1694 else if (TREE_CODE (e
) == COMPONENT_REF
1695 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
1696 && DECL_C_BIT_FIELD (TREE_OPERAND (e
, 1)))
1698 if (complain
& tf_error
)
1699 error ("invalid application of %<__alignof%> to a bit-field");
1701 return error_mark_node
;
1704 else if (TREE_CODE (e
) == COMPONENT_REF
1705 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
)
1706 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e
, 1)));
1707 else if (is_overloaded_fn (e
))
1709 if (complain
& tf_error
)
1710 permerror (input_location
, "ISO C++ forbids applying %<__alignof%> to an expression of "
1713 return error_mark_node
;
1714 if (TREE_CODE (e
) == FUNCTION_DECL
)
1715 t
= size_int (DECL_ALIGN_UNIT (e
));
1719 else if (type_unknown_p (e
))
1721 if (complain
& tf_error
)
1722 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1724 return error_mark_node
;
1728 return cxx_sizeof_or_alignof_type (TREE_TYPE (e
), ALIGNOF_EXPR
,
1729 complain
& tf_error
);
1731 return fold_convert (size_type_node
, t
);
1734 /* Process a sizeof or alignof expression E with code OP where the operand
1735 is an expression. */
1738 cxx_sizeof_or_alignof_expr (tree e
, enum tree_code op
, bool complain
)
1740 if (op
== SIZEOF_EXPR
)
1741 return cxx_sizeof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1743 return cxx_alignof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1746 /* Build a representation of an expression 'alignas(E).' Return the
1747 folded integer value of E if it is an integral constant expression
1748 that resolves to a valid alignment. If E depends on a template
1749 parameter, return a syntactic representation tree of kind
1750 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1751 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1754 cxx_alignas_expr (tree e
)
1756 if (e
== NULL_TREE
|| e
== error_mark_node
1757 || (!TYPE_P (e
) && !require_potential_rvalue_constant_expression (e
)))
1763 When the alignment-specifier is of the form
1764 alignas(type-id ), it shall have the same effect as
1765 alignas(alignof(type-id )). */
1767 return cxx_sizeof_or_alignof_type (e
, ALIGNOF_EXPR
, false);
1769 /* If we reach this point, it means the alignas expression if of
1770 the form "alignas(assignment-expression)", so we should follow
1771 what is stated by [dcl.align]/2. */
1773 if (value_dependent_expression_p (e
))
1774 /* Leave value-dependent expression alone for now. */
1777 e
= instantiate_non_dependent_expr (e
);
1778 e
= mark_rvalue_use (e
);
1780 /* [dcl.align]/2 says:
1782 the assignment-expression shall be an integral constant
1785 return cxx_constant_value (e
);
1789 /* EXPR is being used in a context that is not a function call.
1794 The expression can be used only as the left-hand operand of a
1795 member function call.
1797 [expr.mptr.operator]
1799 If the result of .* or ->* is a function, then that result can be
1800 used only as the operand for the function call operator ().
1802 by issuing an error message if appropriate. Returns true iff EXPR
1803 violates these rules. */
1806 invalid_nonstatic_memfn_p (location_t loc
, tree expr
, tsubst_flags_t complain
)
1808 if (expr
== NULL_TREE
)
1810 /* Don't enforce this in MS mode. */
1811 if (flag_ms_extensions
)
1813 if (is_overloaded_fn (expr
) && !really_overloaded_fn (expr
))
1814 expr
= get_first_fn (expr
);
1815 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr
))
1817 if (complain
& tf_error
)
1821 error_at (loc
, "invalid use of non-static member function %qD",
1823 inform (DECL_SOURCE_LOCATION (expr
), "declared here");
1826 error_at (loc
, "invalid use of non-static member function of "
1827 "type %qT", TREE_TYPE (expr
));
1834 /* If EXP is a reference to a bitfield, and the type of EXP does not
1835 match the declared type of the bitfield, return the declared type
1836 of the bitfield. Otherwise, return NULL_TREE. */
1839 is_bitfield_expr_with_lowered_type (const_tree exp
)
1841 switch (TREE_CODE (exp
))
1844 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1)
1845 ? TREE_OPERAND (exp
, 1)
1846 : TREE_OPERAND (exp
, 0)))
1848 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 2));
1851 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1));
1855 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1861 field
= TREE_OPERAND (exp
, 1);
1862 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_BIT_FIELD_TYPE (field
))
1864 if (same_type_ignoring_top_level_qualifiers_p
1865 (TREE_TYPE (exp
), DECL_BIT_FIELD_TYPE (field
)))
1867 return DECL_BIT_FIELD_TYPE (field
);
1871 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp
, 0)))
1872 == TYPE_MAIN_VARIANT (TREE_TYPE (exp
)))
1873 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1881 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1882 bitfield with a lowered type, the type of EXP is returned, rather
1886 unlowered_expr_type (const_tree exp
)
1889 tree etype
= TREE_TYPE (exp
);
1891 type
= is_bitfield_expr_with_lowered_type (exp
);
1893 type
= cp_build_qualified_type (type
, cp_type_quals (etype
));
1900 /* Perform the conversions in [expr] that apply when an lvalue appears
1901 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1902 function-to-pointer conversions. In addition, manifest constants
1903 are replaced by their values, and bitfield references are converted
1904 to their declared types. Note that this function does not perform the
1905 lvalue-to-rvalue conversion for class types. If you need that conversion
1906 to for class types, then you probably need to use force_rvalue.
1908 Although the returned value is being used as an rvalue, this
1909 function does not wrap the returned expression in a
1910 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1911 that the return value is no longer an lvalue. */
1914 decay_conversion (tree exp
, tsubst_flags_t complain
)
1917 enum tree_code code
;
1918 location_t loc
= EXPR_LOC_OR_LOC (exp
, input_location
);
1920 type
= TREE_TYPE (exp
);
1921 if (type
== error_mark_node
)
1922 return error_mark_node
;
1924 exp
= mark_rvalue_use (exp
);
1926 exp
= resolve_nondeduced_context (exp
);
1927 if (type_unknown_p (exp
))
1929 if (complain
& tf_error
)
1930 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
1931 return error_mark_node
;
1934 code
= TREE_CODE (type
);
1936 /* FIXME remove for delayed folding. */
1937 exp
= scalar_constant_value (exp
);
1938 if (error_operand_p (exp
))
1939 return error_mark_node
;
1941 if (NULLPTR_TYPE_P (type
) && !TREE_SIDE_EFFECTS (exp
))
1942 return nullptr_node
;
1944 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1945 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1946 if (code
== VOID_TYPE
)
1948 if (complain
& tf_error
)
1949 error_at (loc
, "void value not ignored as it ought to be");
1950 return error_mark_node
;
1952 if (invalid_nonstatic_memfn_p (loc
, exp
, complain
))
1953 return error_mark_node
;
1954 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
1955 return cp_build_addr_expr (exp
, complain
);
1956 if (code
== ARRAY_TYPE
)
1961 if (INDIRECT_REF_P (exp
))
1962 return build_nop (build_pointer_type (TREE_TYPE (type
)),
1963 TREE_OPERAND (exp
, 0));
1965 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1967 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1), complain
);
1968 if (op1
== error_mark_node
)
1969 return error_mark_node
;
1970 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
1971 TREE_OPERAND (exp
, 0), op1
);
1975 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
1977 if (complain
& tf_error
)
1978 error_at (loc
, "invalid use of non-lvalue array");
1979 return error_mark_node
;
1982 /* Don't let an array compound literal decay to a pointer. It can
1983 still be used to initialize an array or bind to a reference. */
1984 if (TREE_CODE (exp
) == TARGET_EXPR
)
1986 if (complain
& tf_error
)
1987 error_at (loc
, "taking address of temporary array");
1988 return error_mark_node
;
1991 ptrtype
= build_pointer_type (TREE_TYPE (type
));
1995 if (!cxx_mark_addressable (exp
))
1996 return error_mark_node
;
1997 adr
= build_nop (ptrtype
, build_address (exp
));
2000 /* This way is better for a COMPONENT_REF since it can
2001 simplify the offset for a component. */
2002 adr
= cp_build_addr_expr (exp
, complain
);
2003 return cp_convert (ptrtype
, adr
, complain
);
2006 /* If a bitfield is used in a context where integral promotion
2007 applies, then the caller is expected to have used
2008 default_conversion. That function promotes bitfields correctly
2009 before calling this function. At this point, if we have a
2010 bitfield referenced, we may assume that is not subject to
2011 promotion, and that, therefore, the type of the resulting rvalue
2012 is the declared type of the bitfield. */
2013 exp
= convert_bitfield_to_declared_type (exp
);
2015 /* We do not call rvalue() here because we do not want to wrap EXP
2016 in a NON_LVALUE_EXPR. */
2020 Non-class rvalues always have cv-unqualified types. */
2021 type
= TREE_TYPE (exp
);
2022 if (!CLASS_TYPE_P (type
) && cv_qualified_p (type
))
2023 exp
= build_nop (cv_unqualified (type
), exp
);
2028 /* Perform preparatory conversions, as part of the "usual arithmetic
2029 conversions". In particular, as per [expr]:
2031 Whenever an lvalue expression appears as an operand of an
2032 operator that expects the rvalue for that operand, the
2033 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2034 standard conversions are applied to convert the expression to an
2037 In addition, we perform integral promotions here, as those are
2038 applied to both operands to a binary operator before determining
2039 what additional conversions should apply. */
2042 cp_default_conversion (tree exp
, tsubst_flags_t complain
)
2044 /* Check for target-specific promotions. */
2045 tree promoted_type
= targetm
.promoted_type (TREE_TYPE (exp
));
2047 exp
= cp_convert (promoted_type
, exp
, complain
);
2048 /* Perform the integral promotions first so that bitfield
2049 expressions (which may promote to "int", even if the bitfield is
2050 declared "unsigned") are promoted correctly. */
2051 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp
)))
2052 exp
= cp_perform_integral_promotions (exp
, complain
);
2053 /* Perform the other conversions. */
2054 exp
= decay_conversion (exp
, complain
);
2062 default_conversion (tree exp
)
2064 return cp_default_conversion (exp
, tf_warning_or_error
);
2067 /* EXPR is an expression with an integral or enumeration type.
2068 Perform the integral promotions in [conv.prom], and return the
2072 cp_perform_integral_promotions (tree expr
, tsubst_flags_t complain
)
2077 expr
= mark_rvalue_use (expr
);
2081 If the bitfield has an enumerated type, it is treated as any
2082 other value of that type for promotion purposes. */
2083 type
= is_bitfield_expr_with_lowered_type (expr
);
2084 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
2085 type
= TREE_TYPE (expr
);
2086 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
2087 /* Scoped enums don't promote. */
2088 if (SCOPED_ENUM_P (type
))
2090 promoted_type
= type_promotes_to (type
);
2091 if (type
!= promoted_type
)
2092 expr
= cp_convert (promoted_type
, expr
, complain
);
2099 perform_integral_promotions (tree expr
)
2101 return cp_perform_integral_promotions (expr
, tf_warning_or_error
);
2104 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2105 decay_conversion to one. */
2108 string_conv_p (const_tree totype
, const_tree exp
, int warn
)
2112 if (!TYPE_PTR_P (totype
))
2115 t
= TREE_TYPE (totype
);
2116 if (!same_type_p (t
, char_type_node
)
2117 && !same_type_p (t
, char16_type_node
)
2118 && !same_type_p (t
, char32_type_node
)
2119 && !same_type_p (t
, wchar_type_node
))
2122 if (TREE_CODE (exp
) == STRING_CST
)
2124 /* Make sure that we don't try to convert between char and wide chars. */
2125 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
2130 /* Is this a string constant which has decayed to 'const char *'? */
2131 t
= build_pointer_type (cp_build_qualified_type (t
, TYPE_QUAL_CONST
));
2132 if (!same_type_p (TREE_TYPE (exp
), t
))
2135 if (TREE_CODE (exp
) != ADDR_EXPR
2136 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
2141 if (cxx_dialect
>= cxx11
)
2142 pedwarn (input_location
,
2143 pedantic
? OPT_Wpedantic
: OPT_Wwrite_strings
,
2144 "ISO C++ forbids converting a string constant to %qT",
2147 warning (OPT_Wwrite_strings
,
2148 "deprecated conversion from string constant to %qT",
2155 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2156 can, for example, use as an lvalue. This code used to be in
2157 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2158 expressions, where we're dealing with aggregates. But now it's again only
2159 called from unary_complex_lvalue. The case (in particular) that led to
2160 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2164 rationalize_conditional_expr (enum tree_code code
, tree t
,
2165 tsubst_flags_t complain
)
2167 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
2169 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2170 the first operand is always the one to be used if both operands
2171 are equal, so we know what conditional expression this used to be. */
2172 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
2174 tree op0
= TREE_OPERAND (t
, 0);
2175 tree op1
= TREE_OPERAND (t
, 1);
2177 /* The following code is incorrect if either operand side-effects. */
2178 gcc_assert (!TREE_SIDE_EFFECTS (op0
)
2179 && !TREE_SIDE_EFFECTS (op1
));
2181 build_conditional_expr (loc
,
2182 build_x_binary_op (loc
,
2183 (TREE_CODE (t
) == MIN_EXPR
2184 ? LE_EXPR
: GE_EXPR
),
2185 op0
, TREE_CODE (op0
),
2186 op1
, TREE_CODE (op1
),
2189 cp_build_unary_op (code
, op0
, 0, complain
),
2190 cp_build_unary_op (code
, op1
, 0, complain
),
2195 build_conditional_expr (loc
, TREE_OPERAND (t
, 0),
2196 cp_build_unary_op (code
, TREE_OPERAND (t
, 1), 0,
2198 cp_build_unary_op (code
, TREE_OPERAND (t
, 2), 0,
2203 /* Given the TYPE of an anonymous union field inside T, return the
2204 FIELD_DECL for the field. If not found return NULL_TREE. Because
2205 anonymous unions can nest, we must also search all anonymous unions
2206 that are directly reachable. */
2209 lookup_anon_field (tree t
, tree type
)
2213 t
= TYPE_MAIN_VARIANT (t
);
2215 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
2217 if (TREE_STATIC (field
))
2219 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
2222 /* If we find it directly, return the field. */
2223 if (DECL_NAME (field
) == NULL_TREE
2224 && type
== TYPE_MAIN_VARIANT (TREE_TYPE (field
)))
2229 /* Otherwise, it could be nested, search harder. */
2230 if (DECL_NAME (field
) == NULL_TREE
2231 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2233 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
2241 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2242 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2243 non-NULL, it indicates the path to the base used to name MEMBER.
2244 If PRESERVE_REFERENCE is true, the expression returned will have
2245 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2246 returned will have the type referred to by the reference.
2248 This function does not perform access control; that is either done
2249 earlier by the parser when the name of MEMBER is resolved to MEMBER
2250 itself, or later when overload resolution selects one of the
2251 functions indicated by MEMBER. */
2254 build_class_member_access_expr (tree object
, tree member
,
2255 tree access_path
, bool preserve_reference
,
2256 tsubst_flags_t complain
)
2260 tree result
= NULL_TREE
;
2261 tree using_decl
= NULL_TREE
;
2263 if (error_operand_p (object
) || error_operand_p (member
))
2264 return error_mark_node
;
2266 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2270 The type of the first expression shall be "class object" (of a
2272 object_type
= TREE_TYPE (object
);
2273 if (!currently_open_class (object_type
)
2274 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2275 return error_mark_node
;
2276 if (!CLASS_TYPE_P (object_type
))
2278 if (complain
& tf_error
)
2280 if (POINTER_TYPE_P (object_type
)
2281 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2282 error ("request for member %qD in %qE, which is of pointer "
2283 "type %qT (maybe you meant to use %<->%> ?)",
2284 member
, object
, object_type
);
2286 error ("request for member %qD in %qE, which is of non-class "
2287 "type %qT", member
, object
, object_type
);
2289 return error_mark_node
;
2292 /* The standard does not seem to actually say that MEMBER must be a
2293 member of OBJECT_TYPE. However, that is clearly what is
2295 if (DECL_P (member
))
2297 member_scope
= DECL_CLASS_CONTEXT (member
);
2298 if (!mark_used (member
, complain
) && !(complain
& tf_error
))
2299 return error_mark_node
;
2300 if (TREE_DEPRECATED (member
))
2301 warn_deprecated_use (member
, NULL_TREE
);
2304 member_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (member
));
2305 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2306 presently be the anonymous union. Go outwards until we find a
2307 type related to OBJECT_TYPE. */
2308 while ((ANON_AGGR_TYPE_P (member_scope
) || UNSCOPED_ENUM_P (member_scope
))
2309 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
2311 member_scope
= TYPE_CONTEXT (member_scope
);
2312 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
2314 if (complain
& tf_error
)
2316 if (TREE_CODE (member
) == FIELD_DECL
)
2317 error ("invalid use of nonstatic data member %qE", member
);
2319 error ("%qD is not a member of %qT", member
, object_type
);
2321 return error_mark_node
;
2324 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2325 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2326 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2328 tree temp
= unary_complex_lvalue (ADDR_EXPR
, object
);
2330 object
= cp_build_indirect_ref (temp
, RO_NULL
, complain
);
2333 /* In [expr.ref], there is an explicit list of the valid choices for
2334 MEMBER. We check for each of those cases here. */
2337 /* A static data member. */
2339 mark_exp_read (object
);
2340 /* If OBJECT has side-effects, they are supposed to occur. */
2341 if (TREE_SIDE_EFFECTS (object
))
2342 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
2344 else if (TREE_CODE (member
) == FIELD_DECL
)
2346 /* A non-static data member. */
2351 null_object_p
= (INDIRECT_REF_P (object
)
2352 && integer_zerop (TREE_OPERAND (object
, 0)));
2354 /* Convert OBJECT to the type of MEMBER. */
2355 if (!same_type_p (TYPE_MAIN_VARIANT (object_type
),
2356 TYPE_MAIN_VARIANT (member_scope
)))
2361 binfo
= lookup_base (access_path
? access_path
: object_type
,
2362 member_scope
, ba_unique
, &kind
, complain
);
2363 if (binfo
== error_mark_node
)
2364 return error_mark_node
;
2366 /* It is invalid to try to get to a virtual base of a
2367 NULL object. The most common cause is invalid use of
2369 if (null_object_p
&& kind
== bk_via_virtual
)
2371 if (complain
& tf_error
)
2373 error ("invalid access to non-static data member %qD in "
2374 "virtual base of NULL object", member
);
2376 return error_mark_node
;
2379 /* Convert to the base. */
2380 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
2381 /*nonnull=*/1, complain
);
2382 /* If we found the base successfully then we should be able
2383 to convert to it successfully. */
2384 gcc_assert (object
!= error_mark_node
);
2387 /* If MEMBER is from an anonymous aggregate, we have converted
2388 OBJECT so that it refers to the class containing the
2389 anonymous union. Generate a reference to the anonymous union
2390 itself, and recur to find MEMBER. */
2391 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
))
2392 /* When this code is called from build_field_call, the
2393 object already has the type of the anonymous union.
2394 That is because the COMPONENT_REF was already
2395 constructed, and was then disassembled before calling
2396 build_field_call. After the function-call code is
2397 cleaned up, this waste can be eliminated. */
2398 && (!same_type_ignoring_top_level_qualifiers_p
2399 (TREE_TYPE (object
), DECL_CONTEXT (member
))))
2401 tree anonymous_union
;
2403 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
2404 DECL_CONTEXT (member
));
2405 object
= build_class_member_access_expr (object
,
2407 /*access_path=*/NULL_TREE
,
2412 /* Compute the type of the field, as described in [expr.ref]. */
2413 type_quals
= TYPE_UNQUALIFIED
;
2414 member_type
= TREE_TYPE (member
);
2415 if (TREE_CODE (member_type
) != REFERENCE_TYPE
)
2417 type_quals
= (cp_type_quals (member_type
)
2418 | cp_type_quals (object_type
));
2420 /* A field is const (volatile) if the enclosing object, or the
2421 field itself, is const (volatile). But, a mutable field is
2422 not const, even within a const object. */
2423 if (DECL_MUTABLE_P (member
))
2424 type_quals
&= ~TYPE_QUAL_CONST
;
2425 member_type
= cp_build_qualified_type (member_type
, type_quals
);
2428 result
= build3_loc (input_location
, COMPONENT_REF
, member_type
,
2429 object
, member
, NULL_TREE
);
2430 result
= fold_if_not_in_template (result
);
2432 /* Mark the expression const or volatile, as appropriate. Even
2433 though we've dealt with the type above, we still have to mark the
2434 expression itself. */
2435 if (type_quals
& TYPE_QUAL_CONST
)
2436 TREE_READONLY (result
) = 1;
2437 if (type_quals
& TYPE_QUAL_VOLATILE
)
2438 TREE_THIS_VOLATILE (result
) = 1;
2440 else if (BASELINK_P (member
))
2442 /* The member is a (possibly overloaded) member function. */
2446 /* If the MEMBER is exactly one static member function, then we
2447 know the type of the expression. Otherwise, we must wait
2448 until overload resolution has been performed. */
2449 functions
= BASELINK_FUNCTIONS (member
);
2450 if (TREE_CODE (functions
) == FUNCTION_DECL
2451 && DECL_STATIC_FUNCTION_P (functions
))
2452 type
= TREE_TYPE (functions
);
2454 type
= unknown_type_node
;
2455 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2456 base. That will happen when the function is called. */
2457 result
= build3 (COMPONENT_REF
, type
, object
, member
, NULL_TREE
);
2459 else if (TREE_CODE (member
) == CONST_DECL
)
2461 /* The member is an enumerator. */
2463 /* If OBJECT has side-effects, they are supposed to occur. */
2464 if (TREE_SIDE_EFFECTS (object
))
2465 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
2468 else if ((using_decl
= strip_using_decl (member
)) != member
)
2469 result
= build_class_member_access_expr (object
,
2471 access_path
, preserve_reference
,
2475 if (complain
& tf_error
)
2476 error ("invalid use of %qD", member
);
2477 return error_mark_node
;
2480 if (!preserve_reference
)
2483 If E2 is declared to have type "reference to T", then ... the
2484 type of E1.E2 is T. */
2485 result
= convert_from_reference (result
);
2490 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2491 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2494 lookup_destructor (tree object
, tree scope
, tree dtor_name
,
2495 tsubst_flags_t complain
)
2497 tree object_type
= TREE_TYPE (object
);
2498 tree dtor_type
= TREE_OPERAND (dtor_name
, 0);
2501 /* We've already complained about this destructor. */
2502 if (dtor_type
== error_mark_node
)
2503 return error_mark_node
;
2505 if (scope
&& !check_dtor_name (scope
, dtor_type
))
2507 if (complain
& tf_error
)
2508 error ("qualified type %qT does not match destructor name ~%qT",
2510 return error_mark_node
;
2512 if (is_auto (dtor_type
))
2513 dtor_type
= object_type
;
2514 else if (identifier_p (dtor_type
))
2516 /* In a template, names we can't find a match for are still accepted
2517 destructor names, and we check them here. */
2518 if (check_dtor_name (object_type
, dtor_type
))
2519 dtor_type
= object_type
;
2522 if (complain
& tf_error
)
2523 error ("object type %qT does not match destructor name ~%qT",
2524 object_type
, dtor_type
);
2525 return error_mark_node
;
2529 else if (!DERIVED_FROM_P (dtor_type
, TYPE_MAIN_VARIANT (object_type
)))
2531 if (complain
& tf_error
)
2532 error ("the type being destroyed is %qT, but the destructor "
2533 "refers to %qT", TYPE_MAIN_VARIANT (object_type
), dtor_type
);
2534 return error_mark_node
;
2536 expr
= lookup_member (dtor_type
, complete_dtor_identifier
,
2537 /*protect=*/1, /*want_type=*/false,
2538 tf_warning_or_error
);
2541 if (complain
& tf_error
)
2542 cxx_incomplete_type_error (dtor_name
, dtor_type
);
2543 return error_mark_node
;
2545 expr
= (adjust_result_of_qualified_name_lookup
2546 (expr
, dtor_type
, object_type
));
2547 if (scope
== NULL_TREE
)
2548 /* We need to call adjust_result_of_qualified_name_lookup in case the
2549 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2550 that we still get virtual function binding. */
2551 BASELINK_QUALIFIED_P (expr
) = false;
2555 /* An expression of the form "A::template B" has been resolved to
2556 DECL. Issue a diagnostic if B is not a template or template
2560 check_template_keyword (tree decl
)
2562 /* The standard says:
2566 If a name prefixed by the keyword template is not a member
2567 template, the program is ill-formed.
2569 DR 228 removed the restriction that the template be a member
2572 DR 96, if accepted would add the further restriction that explicit
2573 template arguments must be provided if the template keyword is
2574 used, but, as of 2005-10-16, that DR is still in "drafting". If
2575 this DR is accepted, then the semantic checks here can be
2576 simplified, as the entity named must in fact be a template
2577 specialization, rather than, as at present, a set of overloaded
2578 functions containing at least one template function. */
2579 if (TREE_CODE (decl
) != TEMPLATE_DECL
2580 && TREE_CODE (decl
) != TEMPLATE_ID_EXPR
)
2582 if (!is_overloaded_fn (decl
))
2583 permerror (input_location
, "%qD is not a template", decl
);
2588 if (BASELINK_P (fns
))
2589 fns
= BASELINK_FUNCTIONS (fns
);
2592 tree fn
= OVL_CURRENT (fns
);
2593 if (TREE_CODE (fn
) == TEMPLATE_DECL
2594 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
2596 if (TREE_CODE (fn
) == FUNCTION_DECL
2597 && DECL_USE_TEMPLATE (fn
)
2598 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn
)))
2600 fns
= OVL_NEXT (fns
);
2603 permerror (input_location
, "%qD is not a template", decl
);
2608 /* This function is called by the parser to process a class member
2609 access expression of the form OBJECT.NAME. NAME is a node used by
2610 the parser to represent a name; it is not yet a DECL. It may,
2611 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2612 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2613 there is no reason to do the lookup twice, so the parser keeps the
2614 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2615 be a template via the use of the "A::template B" syntax. */
2618 finish_class_member_access_expr (tree object
, tree name
, bool template_p
,
2619 tsubst_flags_t complain
)
2624 tree access_path
= NULL_TREE
;
2625 tree orig_object
= object
;
2626 tree orig_name
= name
;
2628 if (object
== error_mark_node
|| name
== error_mark_node
)
2629 return error_mark_node
;
2631 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2632 if (!objc_is_public (object
, name
))
2633 return error_mark_node
;
2635 object_type
= TREE_TYPE (object
);
2637 if (processing_template_decl
)
2639 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2640 dependent_type_p (object_type
)
2641 /* If NAME is just an IDENTIFIER_NODE, then the expression
2643 || identifier_p (object
)
2644 /* If NAME is "f<args>", where either 'f' or 'args' is
2645 dependent, then the expression is dependent. */
2646 || (TREE_CODE (name
) == TEMPLATE_ID_EXPR
2647 && dependent_template_id_p (TREE_OPERAND (name
, 0),
2648 TREE_OPERAND (name
, 1)))
2649 /* If NAME is "T::X" where "T" is dependent, then the
2650 expression is dependent. */
2651 || (TREE_CODE (name
) == SCOPE_REF
2652 && TYPE_P (TREE_OPERAND (name
, 0))
2653 && dependent_type_p (TREE_OPERAND (name
, 0))))
2654 return build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
,
2655 object
, name
, NULL_TREE
);
2656 object
= build_non_dependent_expr (object
);
2658 else if (c_dialect_objc ()
2659 && identifier_p (name
)
2660 && (expr
= objc_maybe_build_component_ref (object
, name
)))
2665 The type of the first expression shall be "class object" (of a
2667 if (!currently_open_class (object_type
)
2668 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2669 return error_mark_node
;
2670 if (!CLASS_TYPE_P (object_type
))
2672 if (complain
& tf_error
)
2674 if (POINTER_TYPE_P (object_type
)
2675 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2676 error ("request for member %qD in %qE, which is of pointer "
2677 "type %qT (maybe you meant to use %<->%> ?)",
2678 name
, object
, object_type
);
2680 error ("request for member %qD in %qE, which is of non-class "
2681 "type %qT", name
, object
, object_type
);
2683 return error_mark_node
;
2686 if (BASELINK_P (name
))
2687 /* A member function that has already been looked up. */
2691 bool is_template_id
= false;
2692 tree template_args
= NULL_TREE
;
2695 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
2697 is_template_id
= true;
2698 template_args
= TREE_OPERAND (name
, 1);
2699 name
= TREE_OPERAND (name
, 0);
2701 if (TREE_CODE (name
) == OVERLOAD
)
2702 name
= DECL_NAME (get_first_fn (name
));
2703 else if (DECL_P (name
))
2704 name
= DECL_NAME (name
);
2707 if (TREE_CODE (name
) == SCOPE_REF
)
2709 /* A qualified name. The qualifying class or namespace `S'
2710 has already been looked up; it is either a TYPE or a
2712 scope
= TREE_OPERAND (name
, 0);
2713 name
= TREE_OPERAND (name
, 1);
2715 /* If SCOPE is a namespace, then the qualified name does not
2716 name a member of OBJECT_TYPE. */
2717 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
2719 if (complain
& tf_error
)
2720 error ("%<%D::%D%> is not a member of %qT",
2721 scope
, name
, object_type
);
2722 return error_mark_node
;
2725 if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
2727 /* Looking up a member enumerator (c++/56793). */
2728 if (!TYPE_CLASS_SCOPE_P (scope
)
2729 || !DERIVED_FROM_P (TYPE_CONTEXT (scope
), object_type
))
2731 if (complain
& tf_error
)
2732 error ("%<%D::%D%> is not a member of %qT",
2733 scope
, name
, object_type
);
2734 return error_mark_node
;
2736 tree val
= lookup_enumerator (scope
, name
);
2739 if (complain
& tf_error
)
2740 error ("%qD is not a member of %qD",
2742 return error_mark_node
;
2745 if (TREE_SIDE_EFFECTS (object
))
2746 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
), object
, val
);
2750 gcc_assert (CLASS_TYPE_P (scope
));
2751 gcc_assert (identifier_p (name
) || TREE_CODE (name
) == BIT_NOT_EXPR
);
2753 if (constructor_name_p (name
, scope
))
2755 if (complain
& tf_error
)
2756 error ("cannot call constructor %<%T::%D%> directly",
2758 return error_mark_node
;
2761 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2762 access_path
= lookup_base (object_type
, scope
, ba_check
,
2764 if (access_path
== error_mark_node
)
2765 return error_mark_node
;
2768 if (complain
& tf_error
)
2769 error ("%qT is not a base of %qT", scope
, object_type
);
2770 return error_mark_node
;
2776 access_path
= object_type
;
2779 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2780 member
= lookup_destructor (object
, scope
, name
, complain
);
2783 /* Look up the member. */
2784 member
= lookup_member (access_path
, name
, /*protect=*/1,
2785 /*want_type=*/false, complain
);
2786 if (member
== NULL_TREE
)
2788 if (complain
& tf_error
)
2789 error ("%q#T has no member named %qE",
2790 TREE_CODE (access_path
) == TREE_BINFO
2791 ? TREE_TYPE (access_path
) : object_type
, name
);
2792 return error_mark_node
;
2794 if (member
== error_mark_node
)
2795 return error_mark_node
;
2800 tree templ
= member
;
2802 if (BASELINK_P (templ
))
2803 templ
= lookup_template_function (templ
, template_args
);
2806 if (complain
& tf_error
)
2807 error ("%qD is not a member template function", name
);
2808 return error_mark_node
;
2813 if (TREE_DEPRECATED (member
))
2814 warn_deprecated_use (member
, NULL_TREE
);
2817 check_template_keyword (member
);
2819 expr
= build_class_member_access_expr (object
, member
, access_path
,
2820 /*preserve_reference=*/false,
2822 if (processing_template_decl
&& expr
!= error_mark_node
)
2824 if (BASELINK_P (member
))
2826 if (TREE_CODE (orig_name
) == SCOPE_REF
)
2827 BASELINK_QUALIFIED_P (member
) = 1;
2830 return build_min_non_dep (COMPONENT_REF
, expr
,
2831 orig_object
, orig_name
,
2838 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2842 build_simple_component_ref (tree object
, tree member
)
2844 tree type
= cp_build_qualified_type (TREE_TYPE (member
),
2845 cp_type_quals (TREE_TYPE (object
)));
2846 return fold_build3_loc (input_location
,
2847 COMPONENT_REF
, type
,
2848 object
, member
, NULL_TREE
);
2851 /* Return an expression for the MEMBER_NAME field in the internal
2852 representation of PTRMEM, a pointer-to-member function. (Each
2853 pointer-to-member function type gets its own RECORD_TYPE so it is
2854 more convenient to access the fields by name than by FIELD_DECL.)
2855 This routine converts the NAME to a FIELD_DECL and then creates the
2856 node for the complete expression. */
2859 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
2864 /* This code is a stripped down version of
2865 build_class_member_access_expr. It does not work to use that
2866 routine directly because it expects the object to be of class
2868 ptrmem_type
= TREE_TYPE (ptrmem
);
2869 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
));
2870 for (member
= TYPE_FIELDS (ptrmem_type
); member
;
2871 member
= DECL_CHAIN (member
))
2872 if (DECL_NAME (member
) == member_name
)
2874 return build_simple_component_ref (ptrmem
, member
);
2877 /* Given an expression PTR for a pointer, return an expression
2878 for the value pointed to.
2879 ERRORSTRING is the name of the operator to appear in error messages.
2881 This function may need to overload OPERATOR_FNNAME.
2882 Must also handle REFERENCE_TYPEs for C++. */
2885 build_x_indirect_ref (location_t loc
, tree expr
, ref_operator errorstring
,
2886 tsubst_flags_t complain
)
2888 tree orig_expr
= expr
;
2891 if (processing_template_decl
)
2893 /* Retain the type if we know the operand is a pointer. */
2894 if (TREE_TYPE (expr
) && POINTER_TYPE_P (TREE_TYPE (expr
)))
2895 return build_min (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
2896 if (type_dependent_expression_p (expr
))
2897 return build_min_nt_loc (loc
, INDIRECT_REF
, expr
);
2898 expr
= build_non_dependent_expr (expr
);
2901 rval
= build_new_op (loc
, INDIRECT_REF
, LOOKUP_NORMAL
, expr
,
2902 NULL_TREE
, NULL_TREE
, /*overload=*/NULL
, complain
);
2904 rval
= cp_build_indirect_ref (expr
, errorstring
, complain
);
2906 if (processing_template_decl
&& rval
!= error_mark_node
)
2907 return build_min_non_dep (INDIRECT_REF
, rval
, orig_expr
);
2912 /* Helper function called from c-common. */
2914 build_indirect_ref (location_t
/*loc*/,
2915 tree ptr
, ref_operator errorstring
)
2917 return cp_build_indirect_ref (ptr
, errorstring
, tf_warning_or_error
);
2921 cp_build_indirect_ref (tree ptr
, ref_operator errorstring
,
2922 tsubst_flags_t complain
)
2926 if (ptr
== current_class_ptr
2927 || (TREE_CODE (ptr
) == NOP_EXPR
2928 && TREE_OPERAND (ptr
, 0) == current_class_ptr
2929 && (same_type_ignoring_top_level_qualifiers_p
2930 (TREE_TYPE (ptr
), TREE_TYPE (current_class_ptr
)))))
2931 return current_class_ref
;
2933 pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
2934 ? ptr
: decay_conversion (ptr
, complain
));
2935 if (pointer
== error_mark_node
)
2936 return error_mark_node
;
2938 type
= TREE_TYPE (pointer
);
2940 if (POINTER_TYPE_P (type
))
2944 If the type of the expression is "pointer to T," the type
2945 of the result is "T." */
2946 tree t
= TREE_TYPE (type
);
2948 if ((CONVERT_EXPR_P (ptr
)
2949 || TREE_CODE (ptr
) == VIEW_CONVERT_EXPR
)
2950 && (!CLASS_TYPE_P (t
) || !CLASSTYPE_EMPTY_P (t
)))
2952 /* If a warning is issued, mark it to avoid duplicates from
2953 the backend. This only needs to be done at
2954 warn_strict_aliasing > 2. */
2955 if (warn_strict_aliasing
> 2)
2956 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr
, 0)),
2957 type
, TREE_OPERAND (ptr
, 0)))
2958 TREE_NO_WARNING (ptr
) = 1;
2961 if (VOID_TYPE_P (t
))
2963 /* A pointer to incomplete type (other than cv void) can be
2964 dereferenced [expr.unary.op]/1 */
2965 if (complain
& tf_error
)
2966 error ("%qT is not a pointer-to-object type", type
);
2967 return error_mark_node
;
2969 else if (TREE_CODE (pointer
) == ADDR_EXPR
2970 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
2971 /* The POINTER was something like `&x'. We simplify `*&x' to
2973 return TREE_OPERAND (pointer
, 0);
2976 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
2978 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2979 so that we get the proper error message if the result is used
2980 to assign to. Also, &* is supposed to be a no-op. */
2981 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
2982 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
2983 TREE_SIDE_EFFECTS (ref
)
2984 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
));
2988 else if (!(complain
& tf_error
))
2989 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2991 /* `pointer' won't be an error_mark_node if we were given a
2992 pointer to member, so it's cool to check for this here. */
2993 else if (TYPE_PTRMEM_P (type
))
2994 switch (errorstring
)
2996 case RO_ARRAY_INDEXING
:
2997 error ("invalid use of array indexing on pointer to member");
3000 error ("invalid use of unary %<*%> on pointer to member");
3002 case RO_IMPLICIT_CONVERSION
:
3003 error ("invalid use of implicit conversion on pointer to member");
3006 error ("left hand operand of %<->*%> must be a pointer to class, "
3007 "but is a pointer to member of type %qT", type
);
3012 else if (pointer
!= error_mark_node
)
3013 invalid_indirection_error (input_location
, type
, errorstring
);
3015 return error_mark_node
;
3018 /* This handles expressions of the form "a[i]", which denotes
3021 This is logically equivalent in C to *(a+i), but we may do it differently.
3022 If A is a variable or a member, we generate a primitive ARRAY_REF.
3023 This avoids forcing the array out of registers, and can work on
3024 arrays that are not lvalues (for example, members of structures returned
3027 If INDEX is of some user-defined type, it must be converted to
3028 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3029 will inherit the type of the array, which will be some pointer type.
3031 LOC is the location to use in building the array reference. */
3034 cp_build_array_ref (location_t loc
, tree array
, tree idx
,
3035 tsubst_flags_t complain
)
3041 if (complain
& tf_error
)
3042 error_at (loc
, "subscript missing in array reference");
3043 return error_mark_node
;
3046 /* If an array's index is an array notation, then its rank cannot be
3047 greater than one. */
3048 if (flag_cilkplus
&& contains_array_notation_expr (idx
))
3052 /* If find_rank returns false, then it should have reported an error,
3053 thus it is unnecessary for repetition. */
3054 if (!find_rank (loc
, idx
, idx
, true, &rank
))
3055 return error_mark_node
;
3058 error_at (loc
, "rank of the array%'s index is greater than 1");
3059 return error_mark_node
;
3062 if (TREE_TYPE (array
) == error_mark_node
3063 || TREE_TYPE (idx
) == error_mark_node
)
3064 return error_mark_node
;
3066 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3068 switch (TREE_CODE (array
))
3072 tree value
= cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3074 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3075 TREE_OPERAND (array
, 0), value
);
3076 SET_EXPR_LOCATION (ret
, loc
);
3081 ret
= build_conditional_expr
3082 (loc
, TREE_OPERAND (array
, 0),
3083 cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3085 cp_build_array_ref (loc
, TREE_OPERAND (array
, 2), idx
,
3088 protected_set_expr_location (ret
, loc
);
3096 = convert_vector_to_pointer_for_subscript (loc
, &array
, idx
);
3098 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
3102 warn_array_subscript_with_type_char (loc
, idx
);
3104 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx
)))
3106 if (complain
& tf_error
)
3107 error_at (loc
, "array subscript is not an integer");
3108 return error_mark_node
;
3111 /* Apply integral promotions *after* noticing character types.
3112 (It is unclear why we do these promotions -- the standard
3113 does not say that we should. In fact, the natural thing would
3114 seem to be to convert IDX to ptrdiff_t; we're performing
3115 pointer arithmetic.) */
3116 idx
= cp_perform_integral_promotions (idx
, complain
);
3118 /* An array that is indexed by a non-constant
3119 cannot be stored in a register; we must be able to do
3120 address arithmetic on its address.
3121 Likewise an array of elements of variable size. */
3122 if (TREE_CODE (idx
) != INTEGER_CST
3123 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
3124 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
3127 if (!cxx_mark_addressable (array
))
3128 return error_mark_node
;
3131 /* An array that is indexed by a constant value which is not within
3132 the array bounds cannot be stored in a register either; because we
3133 would get a crash in store_bit_field/extract_bit_field when trying
3134 to access a non-existent part of the register. */
3135 if (TREE_CODE (idx
) == INTEGER_CST
3136 && TYPE_DOMAIN (TREE_TYPE (array
))
3137 && ! int_fits_type_p (idx
, TYPE_DOMAIN (TREE_TYPE (array
))))
3139 if (!cxx_mark_addressable (array
))
3140 return error_mark_node
;
3143 /* Note in C++ it is valid to subscript a `register' array, since
3144 it is valid to take the address of something with that
3145 storage specification. */
3149 while (TREE_CODE (foo
) == COMPONENT_REF
)
3150 foo
= TREE_OPERAND (foo
, 0);
3151 if (VAR_P (foo
) && DECL_REGISTER (foo
)
3152 && (complain
& tf_warning
))
3153 warning_at (loc
, OPT_Wextra
,
3154 "subscripting array declared %<register%>");
3157 type
= TREE_TYPE (TREE_TYPE (array
));
3158 rval
= build4 (ARRAY_REF
, type
, array
, idx
, NULL_TREE
, NULL_TREE
);
3159 /* Array ref is const/volatile if the array elements are
3160 or if the array is.. */
3161 TREE_READONLY (rval
)
3162 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
3163 TREE_SIDE_EFFECTS (rval
)
3164 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
3165 TREE_THIS_VOLATILE (rval
)
3166 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
3167 ret
= require_complete_type_sfinae (fold_if_not_in_template (rval
),
3169 protected_set_expr_location (ret
, loc
);
3171 ret
= non_lvalue_loc (loc
, ret
);
3176 tree ar
= cp_default_conversion (array
, complain
);
3177 tree ind
= cp_default_conversion (idx
, complain
);
3179 /* Put the integer in IND to simplify error checking. */
3180 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
3181 std::swap (ar
, ind
);
3183 if (ar
== error_mark_node
|| ind
== error_mark_node
)
3184 return error_mark_node
;
3186 if (!TYPE_PTR_P (TREE_TYPE (ar
)))
3188 if (complain
& tf_error
)
3189 error_at (loc
, "subscripted value is neither array nor pointer");
3190 return error_mark_node
;
3192 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
3194 if (complain
& tf_error
)
3195 error_at (loc
, "array subscript is not an integer");
3196 return error_mark_node
;
3199 warn_array_subscript_with_type_char (loc
, idx
);
3201 ret
= cp_build_indirect_ref (cp_build_binary_op (input_location
,
3206 protected_set_expr_location (ret
, loc
);
3208 ret
= non_lvalue_loc (loc
, ret
);
3213 /* Entry point for Obj-C++. */
3216 build_array_ref (location_t loc
, tree array
, tree idx
)
3218 return cp_build_array_ref (loc
, array
, idx
, tf_warning_or_error
);
3221 /* Resolve a pointer to member function. INSTANCE is the object
3222 instance to use, if the member points to a virtual member.
3224 This used to avoid checking for virtual functions if basetype
3225 has no virtual functions, according to an earlier ANSI draft.
3226 With the final ISO C++ rules, such an optimization is
3227 incorrect: A pointer to a derived member can be static_cast
3228 to pointer-to-base-member, as long as the dynamic object
3229 later has the right member. So now we only do this optimization
3230 when we know the dynamic type of the object. */
3233 get_member_function_from_ptrfunc (tree
*instance_ptrptr
, tree function
,
3234 tsubst_flags_t complain
)
3236 if (TREE_CODE (function
) == OFFSET_REF
)
3237 function
= TREE_OPERAND (function
, 1);
3239 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
3241 tree idx
, delta
, e1
, e2
, e3
, vtbl
;
3243 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
3244 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
3246 tree instance_ptr
= *instance_ptrptr
;
3247 tree instance_save_expr
= 0;
3248 if (instance_ptr
== error_mark_node
)
3250 if (TREE_CODE (function
) == PTRMEM_CST
)
3252 /* Extracting the function address from a pmf is only
3253 allowed with -Wno-pmf-conversions. It only works for
3255 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
), complain
);
3256 e1
= convert (fntype
, e1
);
3261 if (complain
& tf_error
)
3262 error ("object missing in use of %qE", function
);
3263 return error_mark_node
;
3267 /* True if we know that the dynamic type of the object doesn't have
3268 virtual functions, so we can assume the PFN field is a pointer. */
3269 nonvirtual
= (COMPLETE_TYPE_P (basetype
)
3270 && !TYPE_POLYMORPHIC_P (basetype
)
3271 && resolves_to_fixed_type_p (instance_ptr
, 0));
3273 /* If we don't really have an object (i.e. in an ill-formed
3274 conversion from PMF to pointer), we can't resolve virtual
3275 functions anyway. */
3276 if (!nonvirtual
&& is_dummy_object (instance_ptr
))
3279 if (TREE_SIDE_EFFECTS (instance_ptr
))
3280 instance_ptr
= instance_save_expr
= save_expr (instance_ptr
);
3282 if (TREE_SIDE_EFFECTS (function
))
3283 function
= save_expr (function
);
3285 /* Start by extracting all the information from the PMF itself. */
3286 e3
= pfn_from_ptrmemfunc (function
);
3287 delta
= delta_from_ptrmemfunc (function
);
3288 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
3289 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
3291 int flag_sanitize_save
;
3292 case ptrmemfunc_vbit_in_pfn
:
3293 e1
= cp_build_binary_op (input_location
,
3294 BIT_AND_EXPR
, idx
, integer_one_node
,
3296 idx
= cp_build_binary_op (input_location
,
3297 MINUS_EXPR
, idx
, integer_one_node
,
3299 if (idx
== error_mark_node
)
3300 return error_mark_node
;
3303 case ptrmemfunc_vbit_in_delta
:
3304 e1
= cp_build_binary_op (input_location
,
3305 BIT_AND_EXPR
, delta
, integer_one_node
,
3307 /* Don't instrument the RSHIFT_EXPR we're about to create because
3308 we're going to use DELTA number of times, and that wouldn't play
3309 well with SAVE_EXPRs therein. */
3310 flag_sanitize_save
= flag_sanitize
;
3312 delta
= cp_build_binary_op (input_location
,
3313 RSHIFT_EXPR
, delta
, integer_one_node
,
3315 flag_sanitize
= flag_sanitize_save
;
3316 if (delta
== error_mark_node
)
3317 return error_mark_node
;
3324 if (e1
== error_mark_node
)
3325 return error_mark_node
;
3327 /* Convert down to the right base before using the instance. A
3328 special case is that in a pointer to member of class C, C may
3329 be incomplete. In that case, the function will of course be
3330 a member of C, and no conversion is required. In fact,
3331 lookup_base will fail in that case, because incomplete
3332 classes do not have BINFOs. */
3333 if (!same_type_ignoring_top_level_qualifiers_p
3334 (basetype
, TREE_TYPE (TREE_TYPE (instance_ptr
))))
3336 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
3337 basetype
, ba_check
, NULL
, complain
);
3338 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
,
3340 if (instance_ptr
== error_mark_node
)
3341 return error_mark_node
;
3343 /* ...and then the delta in the PMF. */
3344 instance_ptr
= fold_build_pointer_plus (instance_ptr
, delta
);
3346 /* Hand back the adjusted 'this' argument to our caller. */
3347 *instance_ptrptr
= instance_ptr
;
3350 /* Now just return the pointer. */
3353 /* Next extract the vtable pointer from the object. */
3354 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
3356 vtbl
= cp_build_indirect_ref (vtbl
, RO_NULL
, complain
);
3357 if (vtbl
== error_mark_node
)
3358 return error_mark_node
;
3360 /* Finally, extract the function pointer from the vtable. */
3361 e2
= fold_build_pointer_plus_loc (input_location
, vtbl
, idx
);
3362 e2
= cp_build_indirect_ref (e2
, RO_NULL
, complain
);
3363 if (e2
== error_mark_node
)
3364 return error_mark_node
;
3365 TREE_CONSTANT (e2
) = 1;
3367 /* When using function descriptors, the address of the
3368 vtable entry is treated as a function pointer. */
3369 if (TARGET_VTABLE_USES_DESCRIPTORS
)
3370 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
3371 cp_build_addr_expr (e2
, complain
));
3373 e2
= fold_convert (TREE_TYPE (e3
), e2
);
3374 e1
= build_conditional_expr (input_location
, e1
, e2
, e3
, complain
);
3375 if (e1
== error_mark_node
)
3376 return error_mark_node
;
3378 /* Make sure this doesn't get evaluated first inside one of the
3379 branches of the COND_EXPR. */
3380 if (instance_save_expr
)
3381 e1
= build2 (COMPOUND_EXPR
, TREE_TYPE (e1
),
3382 instance_save_expr
, e1
);
3389 /* Used by the C-common bits. */
3391 build_function_call (location_t
/*loc*/,
3392 tree function
, tree params
)
3394 return cp_build_function_call (function
, params
, tf_warning_or_error
);
3397 /* Used by the C-common bits. */
3399 build_function_call_vec (location_t
/*loc*/, vec
<location_t
> /*arg_loc*/,
3400 tree function
, vec
<tree
, va_gc
> *params
,
3401 vec
<tree
, va_gc
> * /*origtypes*/)
3403 vec
<tree
, va_gc
> *orig_params
= params
;
3404 tree ret
= cp_build_function_call_vec (function
, ¶ms
,
3405 tf_warning_or_error
);
3407 /* cp_build_function_call_vec can reallocate PARAMS by adding
3408 default arguments. That should never happen here. Verify
3410 gcc_assert (params
== orig_params
);
3415 /* Build a function call using a tree list of arguments. */
3418 cp_build_function_call (tree function
, tree params
, tsubst_flags_t complain
)
3420 vec
<tree
, va_gc
> *vec
;
3423 vec
= make_tree_vector ();
3424 for (; params
!= NULL_TREE
; params
= TREE_CHAIN (params
))
3425 vec_safe_push (vec
, TREE_VALUE (params
));
3426 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3427 release_tree_vector (vec
);
3431 /* Build a function call using varargs. */
3434 cp_build_function_call_nary (tree function
, tsubst_flags_t complain
, ...)
3436 vec
<tree
, va_gc
> *vec
;
3440 vec
= make_tree_vector ();
3441 va_start (args
, complain
);
3442 for (t
= va_arg (args
, tree
); t
!= NULL_TREE
; t
= va_arg (args
, tree
))
3443 vec_safe_push (vec
, t
);
3445 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3446 release_tree_vector (vec
);
3450 /* Build a function call using a vector of arguments. PARAMS may be
3451 NULL if there are no parameters. This changes the contents of
3455 cp_build_function_call_vec (tree function
, vec
<tree
, va_gc
> **params
,
3456 tsubst_flags_t complain
)
3458 tree fntype
, fndecl
;
3460 tree original
= function
;
3464 vec
<tree
, va_gc
> *allocated
= NULL
;
3467 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3468 expressions, like those used for ObjC messenger dispatches. */
3469 if (params
!= NULL
&& !vec_safe_is_empty (*params
))
3470 function
= objc_rewrite_function_call (function
, (**params
)[0]);
3472 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3473 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3474 if (TREE_CODE (function
) == NOP_EXPR
3475 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
3476 function
= TREE_OPERAND (function
, 0);
3478 if (TREE_CODE (function
) == FUNCTION_DECL
)
3480 if (!mark_used (function
, complain
) && !(complain
& tf_error
))
3481 return error_mark_node
;
3484 /* Convert anything with function type to a pointer-to-function. */
3485 if (DECL_MAIN_P (function
))
3487 if (complain
& tf_error
)
3488 pedwarn (input_location
, OPT_Wpedantic
,
3489 "ISO C++ forbids calling %<::main%> from within program");
3491 return error_mark_node
;
3493 function
= build_addr_func (function
, complain
);
3499 function
= build_addr_func (function
, complain
);
3502 if (function
== error_mark_node
)
3503 return error_mark_node
;
3505 fntype
= TREE_TYPE (function
);
3507 if (TYPE_PTRMEMFUNC_P (fntype
))
3509 if (complain
& tf_error
)
3510 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3511 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3512 original
, original
);
3513 return error_mark_node
;
3516 is_method
= (TYPE_PTR_P (fntype
)
3517 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
3519 if (!(TYPE_PTRFN_P (fntype
)
3521 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
3523 if (complain
& tf_error
)
3525 if (!flag_diagnostics_show_caret
)
3526 error_at (input_location
,
3527 "%qE cannot be used as a function", original
);
3528 else if (DECL_P (original
))
3529 error_at (input_location
,
3530 "%qD cannot be used as a function", original
);
3532 error_at (input_location
,
3533 "expression cannot be used as a function");
3536 return error_mark_node
;
3539 /* fntype now gets the type of function pointed to. */
3540 fntype
= TREE_TYPE (fntype
);
3541 parm_types
= TYPE_ARG_TYPES (fntype
);
3545 allocated
= make_tree_vector ();
3546 params
= &allocated
;
3549 nargs
= convert_arguments (parm_types
, params
, fndecl
, LOOKUP_NORMAL
,
3552 return error_mark_node
;
3554 argarray
= (*params
)->address ();
3556 /* Check for errors in format strings and inappropriately
3558 check_function_arguments (fntype
, nargs
, argarray
);
3560 ret
= build_cxx_call (function
, nargs
, argarray
, complain
);
3562 if (allocated
!= NULL
)
3563 release_tree_vector (allocated
);
3568 /* Subroutine of convert_arguments.
3569 Print an error message about a wrong number of arguments. */
3572 error_args_num (location_t loc
, tree fndecl
, bool too_many_p
)
3576 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
3578 if (DECL_NAME (fndecl
) == NULL_TREE
3579 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
3582 ? G_("too many arguments to constructor %q#D")
3583 : G_("too few arguments to constructor %q#D"),
3588 ? G_("too many arguments to member function %q#D")
3589 : G_("too few arguments to member function %q#D"),
3595 ? G_("too many arguments to function %q#D")
3596 : G_("too few arguments to function %q#D"),
3598 if (!DECL_IS_BUILTIN (fndecl
))
3599 inform (DECL_SOURCE_LOCATION (fndecl
), "declared here");
3603 if (c_dialect_objc () && objc_message_selector ())
3606 ? G_("too many arguments to method %q#D")
3607 : G_("too few arguments to method %q#D"),
3608 objc_message_selector ());
3610 error_at (loc
, too_many_p
? G_("too many arguments to function")
3611 : G_("too few arguments to function"));
3615 /* Convert the actual parameter expressions in the list VALUES to the
3616 types in the list TYPELIST. The converted expressions are stored
3617 back in the VALUES vector.
3618 If parmdecls is exhausted, or when an element has NULL as its type,
3619 perform the default conversions.
3621 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3623 This is also where warnings about wrong number of args are generated.
3625 Returns the actual number of arguments processed (which might be less
3626 than the length of the vector), or -1 on error.
3628 In C++, unspecified trailing parameters can be filled in with their
3629 default arguments, if such were specified. Do so here. */
3632 convert_arguments (tree typelist
, vec
<tree
, va_gc
> **values
, tree fndecl
,
3633 int flags
, tsubst_flags_t complain
)
3638 /* Argument passing is always copy-initialization. */
3639 flags
|= LOOKUP_ONLYCONVERTING
;
3641 for (i
= 0, typetail
= typelist
;
3642 i
< vec_safe_length (*values
);
3645 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3646 tree val
= (**values
)[i
];
3648 if (val
== error_mark_node
|| type
== error_mark_node
)
3651 if (type
== void_type_node
)
3653 if (complain
& tf_error
)
3655 error_args_num (input_location
, fndecl
, /*too_many_p=*/true);
3662 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3663 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3664 if (TREE_CODE (val
) == NOP_EXPR
3665 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
3666 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
3667 val
= TREE_OPERAND (val
, 0);
3669 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
3671 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
3672 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
3673 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
3674 val
= decay_conversion (val
, complain
);
3677 if (val
== error_mark_node
)
3682 /* Formal parm type is specified by a function prototype. */
3685 if (!COMPLETE_TYPE_P (complete_type (type
)))
3687 if (complain
& tf_error
)
3690 error ("parameter %P of %qD has incomplete type %qT",
3693 error ("parameter %P has incomplete type %qT", i
, type
);
3695 parmval
= error_mark_node
;
3699 parmval
= convert_for_initialization
3700 (NULL_TREE
, type
, val
, flags
,
3701 ICR_ARGPASS
, fndecl
, i
, complain
);
3702 parmval
= convert_for_arg_passing (type
, parmval
, complain
);
3705 if (parmval
== error_mark_node
)
3708 (**values
)[i
] = parmval
;
3712 if (fndecl
&& magic_varargs_p (fndecl
))
3713 /* Don't do ellipsis conversion for __built_in_constant_p
3714 as this will result in spurious errors for non-trivial
3716 val
= require_complete_type_sfinae (val
, complain
);
3718 val
= convert_arg_to_ellipsis (val
, complain
);
3720 (**values
)[i
] = val
;
3724 typetail
= TREE_CHAIN (typetail
);
3727 if (typetail
!= 0 && typetail
!= void_list_node
)
3729 /* See if there are default arguments that can be used. Because
3730 we hold default arguments in the FUNCTION_TYPE (which is so
3731 wrong), we can see default parameters here from deduced
3732 contexts (and via typeof) for indirect function calls.
3733 Fortunately we know whether we have a function decl to
3734 provide default arguments in a language conformant
3736 if (fndecl
&& TREE_PURPOSE (typetail
)
3737 && TREE_CODE (TREE_PURPOSE (typetail
)) != DEFAULT_ARG
)
3739 for (; typetail
!= void_list_node
; ++i
)
3742 = convert_default_arg (TREE_VALUE (typetail
),
3743 TREE_PURPOSE (typetail
),
3744 fndecl
, i
, complain
);
3746 if (parmval
== error_mark_node
)
3749 vec_safe_push (*values
, parmval
);
3750 typetail
= TREE_CHAIN (typetail
);
3751 /* ends with `...'. */
3752 if (typetail
== NULL_TREE
)
3758 if (complain
& tf_error
)
3759 error_args_num (input_location
, fndecl
, /*too_many_p=*/false);
3767 /* Build a binary-operation expression, after performing default
3768 conversions on the operands. CODE is the kind of expression to
3769 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3770 are the tree codes which correspond to ARG1 and ARG2 when issuing
3771 warnings about possibly misplaced parentheses. They may differ
3772 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3773 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3774 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3775 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3776 ARG2_CODE as ERROR_MARK. */
3779 build_x_binary_op (location_t loc
, enum tree_code code
, tree arg1
,
3780 enum tree_code arg1_code
, tree arg2
,
3781 enum tree_code arg2_code
, tree
*overload
,
3782 tsubst_flags_t complain
)
3791 if (processing_template_decl
)
3793 if (type_dependent_expression_p (arg1
)
3794 || type_dependent_expression_p (arg2
))
3795 return build_min_nt_loc (loc
, code
, arg1
, arg2
);
3796 arg1
= build_non_dependent_expr (arg1
);
3797 arg2
= build_non_dependent_expr (arg2
);
3800 if (code
== DOTSTAR_EXPR
)
3801 expr
= build_m_component_ref (arg1
, arg2
, complain
);
3803 expr
= build_new_op (loc
, code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
,
3804 overload
, complain
);
3806 /* Check for cases such as x+y<<z which users are likely to
3807 misinterpret. But don't warn about obj << x + y, since that is a
3808 common idiom for I/O. */
3809 if (warn_parentheses
3810 && (complain
& tf_warning
)
3811 && !processing_template_decl
3812 && !error_operand_p (arg1
)
3813 && !error_operand_p (arg2
)
3814 && (code
!= LSHIFT_EXPR
3815 || !CLASS_TYPE_P (TREE_TYPE (arg1
))))
3816 warn_about_parentheses (loc
, code
, arg1_code
, orig_arg1
,
3817 arg2_code
, orig_arg2
);
3819 if (processing_template_decl
&& expr
!= error_mark_node
)
3820 return build_min_non_dep (code
, expr
, orig_arg1
, orig_arg2
);
3825 /* Build and return an ARRAY_REF expression. */
3828 build_x_array_ref (location_t loc
, tree arg1
, tree arg2
,
3829 tsubst_flags_t complain
)
3831 tree orig_arg1
= arg1
;
3832 tree orig_arg2
= arg2
;
3835 if (processing_template_decl
)
3837 if (type_dependent_expression_p (arg1
)
3838 || type_dependent_expression_p (arg2
))
3839 return build_min_nt_loc (loc
, ARRAY_REF
, arg1
, arg2
,
3840 NULL_TREE
, NULL_TREE
);
3841 arg1
= build_non_dependent_expr (arg1
);
3842 arg2
= build_non_dependent_expr (arg2
);
3845 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, arg1
, arg2
,
3846 NULL_TREE
, /*overload=*/NULL
, complain
);
3848 if (processing_template_decl
&& expr
!= error_mark_node
)
3849 return build_min_non_dep (ARRAY_REF
, expr
, orig_arg1
, orig_arg2
,
3850 NULL_TREE
, NULL_TREE
);
3854 /* Return whether OP is an expression of enum type cast to integer
3855 type. In C++ even unsigned enum types are cast to signed integer
3856 types. We do not want to issue warnings about comparisons between
3857 signed and unsigned types when one of the types is an enum type.
3858 Those warnings are always false positives in practice. */
3861 enum_cast_to_int (tree op
)
3863 if (CONVERT_EXPR_P (op
)
3864 && TREE_TYPE (op
) == integer_type_node
3865 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == ENUMERAL_TYPE
3866 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op
, 0))))
3869 /* The cast may have been pushed into a COND_EXPR. */
3870 if (TREE_CODE (op
) == COND_EXPR
)
3871 return (enum_cast_to_int (TREE_OPERAND (op
, 1))
3872 || enum_cast_to_int (TREE_OPERAND (op
, 2)));
3877 /* For the c-common bits. */
3879 build_binary_op (location_t location
, enum tree_code code
, tree op0
, tree op1
,
3882 return cp_build_binary_op (location
, code
, op0
, op1
, tf_warning_or_error
);
3886 /* Build a binary-operation expression without default conversions.
3887 CODE is the kind of expression to build.
3888 LOCATION is the location_t of the operator in the source code.
3889 This function differs from `build' in several ways:
3890 the data type of the result is computed and recorded in it,
3891 warnings are generated if arg data types are invalid,
3892 special handling for addition and subtraction of pointers is known,
3893 and some optimization is done (operations on narrow ints
3894 are done in the narrower type when that gives the same result).
3895 Constant folding is also done before the result is returned.
3897 Note that the operands will never have enumeral types
3898 because either they have just had the default conversions performed
3899 or they have both just been converted to some other type in which
3900 the arithmetic is to be done.
3902 C++: must do special pointer arithmetic when implementing
3903 multiple inheritance, and deal with pointer to member functions. */
3906 cp_build_binary_op (location_t location
,
3907 enum tree_code code
, tree orig_op0
, tree orig_op1
,
3908 tsubst_flags_t complain
)
3911 enum tree_code code0
, code1
;
3913 const char *invalid_op_diag
;
3915 /* Expression code to give to the expression when it is built.
3916 Normally this is CODE, which is what the caller asked for,
3917 but in some special cases we change it. */
3918 enum tree_code resultcode
= code
;
3920 /* Data type in which the computation is to be performed.
3921 In the simplest cases this is the common type of the arguments. */
3922 tree result_type
= NULL
;
3924 /* Nonzero means operands have already been type-converted
3925 in whatever way is necessary.
3926 Zero means they need to be converted to RESULT_TYPE. */
3929 /* Nonzero means create the expression with this type, rather than
3931 tree build_type
= 0;
3933 /* Nonzero means after finally constructing the expression
3934 convert it to this type. */
3935 tree final_type
= 0;
3938 tree orig_type
= NULL
;
3940 /* Nonzero if this is an operation like MIN or MAX which can
3941 safely be computed in short if both args are promoted shorts.
3942 Also implies COMMON.
3943 -1 indicates a bitwise operation; this makes a difference
3944 in the exact conditions for when it is safe to do the operation
3945 in a narrower mode. */
3948 /* Nonzero if this is a comparison operation;
3949 if both args are promoted shorts, compare the original shorts.
3950 Also implies COMMON. */
3951 int short_compare
= 0;
3953 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3956 /* True if both operands have arithmetic type. */
3957 bool arithmetic_types_p
;
3959 /* Apply default conversions. */
3963 /* Remember whether we're doing / or %. */
3964 bool doing_div_or_mod
= false;
3966 /* Remember whether we're doing << or >>. */
3967 bool doing_shift
= false;
3969 /* Tree holding instrumentation expression. */
3970 tree instrument_expr
= NULL
;
3972 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
3973 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
3974 || code
== TRUTH_XOR_EXPR
)
3976 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
3977 op0
= decay_conversion (op0
, complain
);
3978 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
3979 op1
= decay_conversion (op1
, complain
);
3983 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
3984 op0
= cp_default_conversion (op0
, complain
);
3985 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
3986 op1
= cp_default_conversion (op1
, complain
);
3989 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3990 STRIP_TYPE_NOPS (op0
);
3991 STRIP_TYPE_NOPS (op1
);
3993 /* DTRT if one side is an overloaded function, but complain about it. */
3994 if (type_unknown_p (op0
))
3996 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
3997 if (t
!= error_mark_node
)
3999 if (complain
& tf_error
)
4000 permerror (input_location
, "assuming cast to type %qT from overloaded function",
4005 if (type_unknown_p (op1
))
4007 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
4008 if (t
!= error_mark_node
)
4010 if (complain
& tf_error
)
4011 permerror (input_location
, "assuming cast to type %qT from overloaded function",
4017 type0
= TREE_TYPE (op0
);
4018 type1
= TREE_TYPE (op1
);
4020 /* The expression codes of the data types of the arguments tell us
4021 whether the arguments are integers, floating, pointers, etc. */
4022 code0
= TREE_CODE (type0
);
4023 code1
= TREE_CODE (type1
);
4025 /* If an error was already reported for one of the arguments,
4026 avoid reporting another error. */
4027 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
4028 return error_mark_node
;
4030 if ((invalid_op_diag
4031 = targetm
.invalid_binary_op (code
, type0
, type1
)))
4033 if (complain
& tf_error
)
4034 error (invalid_op_diag
);
4035 return error_mark_node
;
4038 /* Issue warnings about peculiar, but valid, uses of NULL. */
4039 if ((orig_op0
== null_node
|| orig_op1
== null_node
)
4040 /* It's reasonable to use pointer values as operands of &&
4041 and ||, so NULL is no exception. */
4042 && code
!= TRUTH_ANDIF_EXPR
&& code
!= TRUTH_ORIF_EXPR
4043 && ( /* Both are NULL (or 0) and the operation was not a
4044 comparison or a pointer subtraction. */
4045 (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)
4046 && code
!= EQ_EXPR
&& code
!= NE_EXPR
&& code
!= MINUS_EXPR
)
4047 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4048 || (!null_ptr_cst_p (orig_op0
)
4049 && !TYPE_PTR_OR_PTRMEM_P (type0
))
4050 || (!null_ptr_cst_p (orig_op1
)
4051 && !TYPE_PTR_OR_PTRMEM_P (type1
)))
4052 && (complain
& tf_warning
))
4054 source_location loc
=
4055 expansion_point_location_if_in_system_header (input_location
);
4057 warning_at (loc
, OPT_Wpointer_arith
, "NULL used in arithmetic");
4060 /* In case when one of the operands of the binary operation is
4061 a vector and another is a scalar -- convert scalar to vector. */
4062 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
4064 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
4065 complain
& tf_error
);
4067 switch (convert_flag
)
4070 return error_mark_node
;
4073 op0
= convert (TREE_TYPE (type1
), op0
);
4074 op0
= save_expr (op0
);
4075 op0
= build_vector_from_val (type1
, op0
);
4076 type0
= TREE_TYPE (op0
);
4077 code0
= TREE_CODE (type0
);
4083 op1
= convert (TREE_TYPE (type0
), op1
);
4084 op1
= save_expr (op1
);
4085 op1
= build_vector_from_val (type0
, op1
);
4086 type1
= TREE_TYPE (op1
);
4087 code1
= TREE_CODE (type1
);
4099 /* Subtraction of two similar pointers.
4100 We must subtract them as integers, then divide by object size. */
4101 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
4102 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4104 return pointer_diff (op0
, op1
, common_pointer_type (type0
, type1
),
4106 /* In all other cases except pointer - int, the usual arithmetic
4108 else if (!(code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
))
4113 /* The pointer - int case is just like pointer + int; fall
4116 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
4117 && (code0
== INTEGER_TYPE
|| code1
== INTEGER_TYPE
))
4121 ptr_operand
= ((code0
== POINTER_TYPE
) ? op0
: op1
);
4122 int_operand
= ((code0
== INTEGER_TYPE
) ? op0
: op1
);
4123 if (processing_template_decl
)
4125 result_type
= TREE_TYPE (ptr_operand
);
4128 return cp_pointer_int_sum (code
,
4140 case TRUNC_DIV_EXPR
:
4142 case FLOOR_DIV_EXPR
:
4143 case ROUND_DIV_EXPR
:
4144 case EXACT_DIV_EXPR
:
4145 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4146 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
4147 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4148 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
4150 enum tree_code tcode0
= code0
, tcode1
= code1
;
4151 tree cop1
= fold_non_dependent_expr (op1
);
4152 doing_div_or_mod
= true;
4153 warn_for_div_by_zero (location
, cop1
);
4155 if (tcode0
== COMPLEX_TYPE
|| tcode0
== VECTOR_TYPE
)
4156 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
4157 if (tcode1
== COMPLEX_TYPE
|| tcode1
== VECTOR_TYPE
)
4158 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
4160 if (!(tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
))
4161 resultcode
= RDIV_EXPR
;
4163 /* When dividing two signed integers, we have to promote to int.
4164 unless we divide by a constant != -1. Note that default
4165 conversion will have been performed on the operands at this
4166 point, so we have to dig out the original type to find out if
4168 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4169 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4170 || (TREE_CODE (op1
) == INTEGER_CST
4171 && ! integer_all_onesp (op1
)));
4180 if ((code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4181 || (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4182 && !VECTOR_FLOAT_TYPE_P (type0
)
4183 && !VECTOR_FLOAT_TYPE_P (type1
)))
4187 case TRUNC_MOD_EXPR
:
4188 case FLOOR_MOD_EXPR
:
4190 tree cop1
= fold_non_dependent_expr (op1
);
4191 doing_div_or_mod
= true;
4192 warn_for_div_by_zero (location
, cop1
);
4195 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4196 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4197 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
4199 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4201 /* Although it would be tempting to shorten always here, that loses
4202 on some targets, since the modulo instruction is undefined if the
4203 quotient can't be represented in the computation mode. We shorten
4204 only if unsigned or if dividing by something we know != -1. */
4205 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4206 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4207 || (TREE_CODE (op1
) == INTEGER_CST
4208 && ! integer_all_onesp (op1
)));
4213 case TRUTH_ANDIF_EXPR
:
4214 case TRUTH_ORIF_EXPR
:
4215 case TRUTH_AND_EXPR
:
4217 if (!VECTOR_TYPE_P (type0
) && VECTOR_TYPE_P (type1
))
4219 if (!COMPARISON_CLASS_P (op1
))
4220 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4221 build_zero_cst (type1
), complain
);
4222 if (code
== TRUTH_ANDIF_EXPR
)
4224 tree z
= build_zero_cst (TREE_TYPE (op1
));
4225 return build_conditional_expr (location
, op0
, op1
, z
, complain
);
4227 else if (code
== TRUTH_ORIF_EXPR
)
4229 tree m1
= build_all_ones_cst (TREE_TYPE (op1
));
4230 return build_conditional_expr (location
, op0
, m1
, op1
, complain
);
4235 if (VECTOR_TYPE_P (type0
))
4237 if (!COMPARISON_CLASS_P (op0
))
4238 op0
= cp_build_binary_op (EXPR_LOCATION (op0
), NE_EXPR
, op0
,
4239 build_zero_cst (type0
), complain
);
4240 if (!VECTOR_TYPE_P (type1
))
4242 tree m1
= build_all_ones_cst (TREE_TYPE (op0
));
4243 tree z
= build_zero_cst (TREE_TYPE (op0
));
4244 op1
= build_conditional_expr (location
, op1
, z
, m1
, complain
);
4246 else if (!COMPARISON_CLASS_P (op1
))
4247 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4248 build_zero_cst (type1
), complain
);
4250 if (code
== TRUTH_ANDIF_EXPR
)
4251 code
= BIT_AND_EXPR
;
4252 else if (code
== TRUTH_ORIF_EXPR
)
4253 code
= BIT_IOR_EXPR
;
4257 return cp_build_binary_op (location
, code
, op0
, op1
, complain
);
4260 result_type
= boolean_type_node
;
4263 /* Shift operations: result has same type as first operand;
4264 always convert second operand to int.
4265 Also set SHORT_SHIFT if shifting rightward. */
4268 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4269 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4271 result_type
= type0
;
4274 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4275 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4276 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4277 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
4279 result_type
= type0
;
4282 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4284 tree const_op1
= fold_non_dependent_expr (op1
);
4285 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4287 result_type
= type0
;
4289 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4291 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4293 if ((complain
& tf_warning
)
4294 && c_inhibit_evaluation_warnings
== 0)
4295 warning (OPT_Wshift_count_negative
,
4296 "right shift count is negative");
4300 if (compare_tree_int (const_op1
, TYPE_PRECISION (type0
)) >= 0
4301 && (complain
& tf_warning
)
4302 && c_inhibit_evaluation_warnings
== 0)
4303 warning (OPT_Wshift_count_overflow
,
4304 "right shift count >= width of type");
4307 /* Avoid converting op1 to result_type later. */
4313 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4314 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4316 result_type
= type0
;
4319 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4320 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4321 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4322 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
4324 result_type
= type0
;
4327 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4329 tree const_op0
= fold_non_dependent_expr (op0
);
4330 if (TREE_CODE (const_op0
) != INTEGER_CST
)
4332 tree const_op1
= fold_non_dependent_expr (op1
);
4333 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4335 result_type
= type0
;
4337 if (TREE_CODE (const_op0
) == INTEGER_CST
4338 && tree_int_cst_sgn (const_op0
) < 0
4339 && (complain
& tf_warning
)
4340 && c_inhibit_evaluation_warnings
== 0)
4341 warning (OPT_Wshift_negative_value
,
4342 "left shift of negative value");
4343 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4345 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4347 if ((complain
& tf_warning
)
4348 && c_inhibit_evaluation_warnings
== 0)
4349 warning (OPT_Wshift_count_negative
,
4350 "left shift count is negative");
4352 else if (compare_tree_int (const_op1
,
4353 TYPE_PRECISION (type0
)) >= 0)
4355 if ((complain
& tf_warning
)
4356 && c_inhibit_evaluation_warnings
== 0)
4357 warning (OPT_Wshift_count_overflow
,
4358 "left shift count >= width of type");
4360 else if (TREE_CODE (const_op0
) == INTEGER_CST
4361 && (complain
& tf_warning
))
4362 maybe_warn_shift_overflow (location
, const_op0
, const_op1
);
4364 /* Avoid converting op1 to result_type later. */
4371 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4373 result_type
= type0
;
4374 if (TREE_CODE (op1
) == INTEGER_CST
)
4376 if (tree_int_cst_lt (op1
, integer_zero_node
))
4378 if (complain
& tf_warning
)
4379 warning (0, (code
== LROTATE_EXPR
)
4380 ? G_("left rotate count is negative")
4381 : G_("right rotate count is negative"));
4383 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
4385 if (complain
& tf_warning
)
4386 warning (0, (code
== LROTATE_EXPR
)
4387 ? G_("left rotate count >= width of type")
4388 : G_("right rotate count >= width of type"));
4391 /* Convert the shift-count to an integer, regardless of
4392 size of value being shifted. */
4393 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4394 op1
= cp_convert (integer_type_node
, op1
, complain
);
4400 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4401 goto vector_compare
;
4402 if ((complain
& tf_warning
)
4403 && (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
)))
4404 warning (OPT_Wfloat_equal
,
4405 "comparing floating point with == or != is unsafe");
4406 if ((complain
& tf_warning
)
4407 && ((TREE_CODE (orig_op0
) == STRING_CST
&& !integer_zerop (op1
))
4408 || (TREE_CODE (orig_op1
) == STRING_CST
&& !integer_zerop (op0
))))
4409 warning (OPT_Waddress
, "comparison with string literal results in unspecified behaviour");
4411 build_type
= boolean_type_node
;
4412 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4413 || code0
== COMPLEX_TYPE
|| code0
== ENUMERAL_TYPE
)
4414 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4415 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
))
4417 else if (((code0
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type0
))
4418 && null_ptr_cst_p (op1
))
4419 /* Handle, eg, (void*)0 (c++/43906), and more. */
4420 || (code0
== POINTER_TYPE
4421 && TYPE_PTR_P (type1
) && integer_zerop (op1
)))
4423 if (TYPE_PTR_P (type1
))
4424 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4425 CPO_COMPARISON
, complain
);
4427 result_type
= type0
;
4429 if (TREE_CODE (op0
) == ADDR_EXPR
4430 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0)))
4432 if ((complain
& tf_warning
)
4433 && c_inhibit_evaluation_warnings
== 0
4434 && !TREE_NO_WARNING (op0
))
4435 warning (OPT_Waddress
, "the address of %qD will never be NULL",
4436 TREE_OPERAND (op0
, 0));
4439 if (CONVERT_EXPR_P (op0
)
4440 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op0
, 0)))
4443 tree inner_op0
= op0
;
4444 STRIP_NOPS (inner_op0
);
4446 if ((complain
& tf_warning
)
4447 && c_inhibit_evaluation_warnings
== 0
4448 && !TREE_NO_WARNING (op0
)
4449 && DECL_P (inner_op0
))
4450 warning_at (location
, OPT_Waddress
,
4451 "the compiler can assume that the address of "
4452 "%qD will never be NULL",
4456 else if (((code1
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type1
))
4457 && null_ptr_cst_p (op0
))
4458 /* Handle, eg, (void*)0 (c++/43906), and more. */
4459 || (code1
== POINTER_TYPE
4460 && TYPE_PTR_P (type0
) && integer_zerop (op0
)))
4462 if (TYPE_PTR_P (type0
))
4463 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4464 CPO_COMPARISON
, complain
);
4466 result_type
= type1
;
4468 if (TREE_CODE (op1
) == ADDR_EXPR
4469 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0)))
4471 if ((complain
& tf_warning
)
4472 && c_inhibit_evaluation_warnings
== 0
4473 && !TREE_NO_WARNING (op1
))
4474 warning (OPT_Waddress
, "the address of %qD will never be NULL",
4475 TREE_OPERAND (op1
, 0));
4478 if (CONVERT_EXPR_P (op1
)
4479 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op1
, 0)))
4482 tree inner_op1
= op1
;
4483 STRIP_NOPS (inner_op1
);
4485 if ((complain
& tf_warning
)
4486 && c_inhibit_evaluation_warnings
== 0
4487 && !TREE_NO_WARNING (op1
)
4488 && DECL_P (inner_op1
))
4489 warning_at (location
, OPT_Waddress
,
4490 "the compiler can assume that the address of "
4491 "%qD will never be NULL",
4495 else if ((code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4496 || (TYPE_PTRDATAMEM_P (type0
) && TYPE_PTRDATAMEM_P (type1
)))
4497 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4498 CPO_COMPARISON
, complain
);
4499 else if (null_ptr_cst_p (op0
) && null_ptr_cst_p (op1
))
4500 /* One of the operands must be of nullptr_t type. */
4501 result_type
= TREE_TYPE (nullptr_node
);
4502 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4504 result_type
= type0
;
4505 if (complain
& tf_error
)
4506 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4508 return error_mark_node
;
4510 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4512 result_type
= type1
;
4513 if (complain
& tf_error
)
4514 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4516 return error_mark_node
;
4518 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (op1
))
4520 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4521 == ptrmemfunc_vbit_in_delta
)
4523 tree pfn0
, delta0
, e1
, e2
;
4525 if (TREE_SIDE_EFFECTS (op0
))
4526 op0
= save_expr (op0
);
4528 pfn0
= pfn_from_ptrmemfunc (op0
);
4529 delta0
= delta_from_ptrmemfunc (op0
);
4530 e1
= cp_build_binary_op (location
,
4533 build_zero_cst (TREE_TYPE (pfn0
)),
4535 e2
= cp_build_binary_op (location
,
4541 if (complain
& tf_warning
)
4542 maybe_warn_zero_as_null_pointer_constant (op1
, input_location
);
4544 e2
= cp_build_binary_op (location
,
4545 EQ_EXPR
, e2
, integer_zero_node
,
4547 op0
= cp_build_binary_op (location
,
4548 TRUTH_ANDIF_EXPR
, e1
, e2
,
4550 op1
= cp_convert (TREE_TYPE (op0
), integer_one_node
, complain
);
4554 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
4555 op1
= cp_convert (TREE_TYPE (op0
), op1
, complain
);
4557 result_type
= TREE_TYPE (op0
);
4559 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (op0
))
4560 return cp_build_binary_op (location
, code
, op1
, op0
, complain
);
4561 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
))
4564 /* E will be the final comparison. */
4566 /* E1 and E2 are for scratch. */
4574 type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4575 CPO_COMPARISON
, complain
);
4577 if (!same_type_p (TREE_TYPE (op0
), type
))
4578 op0
= cp_convert_and_check (type
, op0
, complain
);
4579 if (!same_type_p (TREE_TYPE (op1
), type
))
4580 op1
= cp_convert_and_check (type
, op1
, complain
);
4582 if (op0
== error_mark_node
|| op1
== error_mark_node
)
4583 return error_mark_node
;
4585 if (TREE_SIDE_EFFECTS (op0
))
4586 op0
= save_expr (op0
);
4587 if (TREE_SIDE_EFFECTS (op1
))
4588 op1
= save_expr (op1
);
4590 pfn0
= pfn_from_ptrmemfunc (op0
);
4591 /* Avoid -Waddress warnings (c++/64877). */
4592 if (TREE_CODE (pfn0
) == ADDR_EXPR
)
4593 TREE_NO_WARNING (pfn0
) = 1;
4594 pfn1
= pfn_from_ptrmemfunc (op1
);
4595 delta0
= delta_from_ptrmemfunc (op0
);
4596 delta1
= delta_from_ptrmemfunc (op1
);
4597 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4598 == ptrmemfunc_vbit_in_delta
)
4603 && ((op0.delta == op1.delta)
4604 || (!op0.pfn && op0.delta & 1 == 0
4605 && op1.delta & 1 == 0))
4607 The reason for the `!op0.pfn' bit is that a NULL
4608 pointer-to-member is any member with a zero PFN and
4609 LSB of the DELTA field is 0. */
4611 e1
= cp_build_binary_op (location
, BIT_AND_EXPR
,
4615 e1
= cp_build_binary_op (location
,
4616 EQ_EXPR
, e1
, integer_zero_node
,
4618 e2
= cp_build_binary_op (location
, BIT_AND_EXPR
,
4622 e2
= cp_build_binary_op (location
,
4623 EQ_EXPR
, e2
, integer_zero_node
,
4625 e1
= cp_build_binary_op (location
,
4626 TRUTH_ANDIF_EXPR
, e2
, e1
,
4628 e2
= cp_build_binary_op (location
, EQ_EXPR
,
4630 build_zero_cst (TREE_TYPE (pfn0
)),
4632 e2
= cp_build_binary_op (location
,
4633 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
4634 e1
= cp_build_binary_op (location
,
4635 EQ_EXPR
, delta0
, delta1
, complain
);
4636 e1
= cp_build_binary_op (location
,
4637 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
4644 && (!op0.pfn || op0.delta == op1.delta))
4646 The reason for the `!op0.pfn' bit is that a NULL
4647 pointer-to-member is any member with a zero PFN; the
4648 DELTA field is unspecified. */
4650 e1
= cp_build_binary_op (location
,
4651 EQ_EXPR
, delta0
, delta1
, complain
);
4652 e2
= cp_build_binary_op (location
,
4655 build_zero_cst (TREE_TYPE (pfn0
)),
4657 e1
= cp_build_binary_op (location
,
4658 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
4660 e2
= build2 (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
4661 e
= cp_build_binary_op (location
,
4662 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
4663 if (code
== EQ_EXPR
)
4665 return cp_build_binary_op (location
,
4666 EQ_EXPR
, e
, integer_zero_node
, complain
);
4670 gcc_assert (!TYPE_PTRMEMFUNC_P (type0
)
4671 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
),
4673 gcc_assert (!TYPE_PTRMEMFUNC_P (type1
)
4674 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
),
4682 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
4683 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
4685 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4686 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4687 CPO_COMPARISON
, complain
);
4694 if (TREE_CODE (orig_op0
) == STRING_CST
4695 || TREE_CODE (orig_op1
) == STRING_CST
)
4697 if (complain
& tf_warning
)
4698 warning (OPT_Waddress
, "comparison with string literal results in unspecified behaviour");
4701 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4705 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4707 && !vector_types_compatible_elements_p (type0
, type1
))
4709 if (complain
& tf_error
)
4711 error_at (location
, "comparing vectors with different "
4713 inform (location
, "operand types are %qT and %qT",
4716 return error_mark_node
;
4719 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
4721 if (complain
& tf_error
)
4723 error_at (location
, "comparing vectors with different "
4724 "number of elements");
4725 inform (location
, "operand types are %qT and %qT",
4728 return error_mark_node
;
4731 /* Always construct signed integer vector type. */
4732 intt
= c_common_type_for_size (GET_MODE_BITSIZE
4733 (TYPE_MODE (TREE_TYPE (type0
))), 0);
4736 if (complain
& tf_error
)
4737 error_at (location
, "could not find an integer type "
4738 "of the same size as %qT", TREE_TYPE (type0
));
4739 return error_mark_node
;
4741 result_type
= build_opaque_vector_type (intt
,
4742 TYPE_VECTOR_SUBPARTS (type0
));
4746 build_type
= boolean_type_node
;
4747 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4748 || code0
== ENUMERAL_TYPE
)
4749 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4750 || code1
== ENUMERAL_TYPE
))
4752 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4753 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4754 CPO_COMPARISON
, complain
);
4755 else if (code0
== POINTER_TYPE
&& null_ptr_cst_p (op1
))
4757 result_type
= type0
;
4758 if (extra_warnings
&& (complain
& tf_warning
))
4759 warning (OPT_Wextra
,
4760 "ordered comparison of pointer with integer zero");
4762 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (op0
))
4764 result_type
= type1
;
4765 if (extra_warnings
&& (complain
& tf_warning
))
4766 warning (OPT_Wextra
,
4767 "ordered comparison of pointer with integer zero");
4769 else if (null_ptr_cst_p (op0
) && null_ptr_cst_p (op1
))
4770 /* One of the operands must be of nullptr_t type. */
4771 result_type
= TREE_TYPE (nullptr_node
);
4772 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4774 result_type
= type0
;
4775 if (complain
& tf_error
)
4776 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4778 return error_mark_node
;
4780 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4782 result_type
= type1
;
4783 if (complain
& tf_error
)
4784 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4786 return error_mark_node
;
4790 case UNORDERED_EXPR
:
4797 build_type
= integer_type_node
;
4798 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
4800 if (complain
& tf_error
)
4801 error ("unordered comparison on non-floating point argument");
4802 return error_mark_node
;
4811 if (((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
4812 || code0
== ENUMERAL_TYPE
)
4813 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4814 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
)))
4815 arithmetic_types_p
= 1;
4818 arithmetic_types_p
= 0;
4819 /* Vector arithmetic is only allowed when both sides are vectors. */
4820 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4822 if (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
4823 || !vector_types_compatible_elements_p (type0
, type1
))
4825 if (complain
& tf_error
)
4826 binary_op_error (location
, code
, type0
, type1
);
4827 return error_mark_node
;
4829 arithmetic_types_p
= 1;
4832 /* Determine the RESULT_TYPE, if it is not already known. */
4834 && arithmetic_types_p
4835 && (shorten
|| common
|| short_compare
))
4837 result_type
= cp_common_type (type0
, type1
);
4838 if (complain
& tf_warning
)
4839 do_warn_double_promotion (result_type
, type0
, type1
,
4840 "implicit conversion from %qT to %qT "
4841 "to match other operand of binary "
4848 if (complain
& tf_error
)
4849 error ("invalid operands of types %qT and %qT to binary %qO",
4850 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
4851 return error_mark_node
;
4854 /* If we're in a template, the only thing we need to know is the
4856 if (processing_template_decl
)
4858 /* Since the middle-end checks the type when doing a build2, we
4859 need to build the tree in pieces. This built tree will never
4860 get out of the front-end as we replace it when instantiating
4862 tree tmp
= build2 (resultcode
,
4863 build_type
? build_type
: result_type
,
4865 TREE_OPERAND (tmp
, 0) = op0
;
4869 if (arithmetic_types_p
)
4871 bool first_complex
= (code0
== COMPLEX_TYPE
);
4872 bool second_complex
= (code1
== COMPLEX_TYPE
);
4873 int none_complex
= (!first_complex
&& !second_complex
);
4875 /* Adapted from patch for c/24581. */
4876 if (first_complex
!= second_complex
4877 && (code
== PLUS_EXPR
4878 || code
== MINUS_EXPR
4879 || code
== MULT_EXPR
4880 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
4881 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
4882 && flag_signed_zeros
)
4884 /* An operation on mixed real/complex operands must be
4885 handled specially, but the language-independent code can
4886 more easily optimize the plain complex arithmetic if
4887 -fno-signed-zeros. */
4888 tree real_type
= TREE_TYPE (result_type
);
4892 if (TREE_TYPE (op0
) != result_type
)
4893 op0
= cp_convert_and_check (result_type
, op0
, complain
);
4894 if (TREE_TYPE (op1
) != real_type
)
4895 op1
= cp_convert_and_check (real_type
, op1
, complain
);
4899 if (TREE_TYPE (op0
) != real_type
)
4900 op0
= cp_convert_and_check (real_type
, op0
, complain
);
4901 if (TREE_TYPE (op1
) != result_type
)
4902 op1
= cp_convert_and_check (result_type
, op1
, complain
);
4904 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
4905 return error_mark_node
;
4908 op0
= save_expr (op0
);
4909 real
= cp_build_unary_op (REALPART_EXPR
, op0
, 1, complain
);
4910 imag
= cp_build_unary_op (IMAGPART_EXPR
, op0
, 1, complain
);
4914 case TRUNC_DIV_EXPR
:
4915 op1
= save_expr (op1
);
4916 imag
= build2 (resultcode
, real_type
, imag
, op1
);
4920 real
= build2 (resultcode
, real_type
, real
, op1
);
4928 op1
= save_expr (op1
);
4929 real
= cp_build_unary_op (REALPART_EXPR
, op1
, 1, complain
);
4930 imag
= cp_build_unary_op (IMAGPART_EXPR
, op1
, 1, complain
);
4934 op0
= save_expr (op0
);
4935 imag
= build2 (resultcode
, real_type
, op0
, imag
);
4938 real
= build2 (resultcode
, real_type
, op0
, real
);
4941 real
= build2 (resultcode
, real_type
, op0
, real
);
4942 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
4948 real
= fold_if_not_in_template (real
);
4949 imag
= fold_if_not_in_template (imag
);
4950 result
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
4951 result
= fold_if_not_in_template (result
);
4955 /* For certain operations (which identify themselves by shorten != 0)
4956 if both args were extended from the same smaller type,
4957 do the arithmetic in that type and then extend.
4959 shorten !=0 and !=1 indicates a bitwise operation.
4960 For them, this optimization is safe only if
4961 both args are zero-extended or both are sign-extended.
4962 Otherwise, we might change the result.
4963 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4964 but calculated in (unsigned short) it would be (unsigned short)-1. */
4966 if (shorten
&& none_complex
)
4968 orig_type
= result_type
;
4969 final_type
= result_type
;
4970 result_type
= shorten_binary_op (result_type
, op0
, op1
,
4974 /* Comparison operations are shortened too but differently.
4975 They identify themselves by setting short_compare = 1. */
4979 /* Don't write &op0, etc., because that would prevent op0
4980 from being kept in a register.
4981 Instead, make copies of the our local variables and
4982 pass the copies by reference, then copy them back afterward. */
4983 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
4984 enum tree_code xresultcode
= resultcode
;
4986 = shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
4989 return cp_convert (boolean_type_node
, val
, complain
);
4990 op0
= xop0
, op1
= xop1
;
4992 resultcode
= xresultcode
;
4995 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
4996 && warn_sign_compare
4997 /* Do not warn until the template is instantiated; we cannot
4998 bound the ranges of the arguments until that point. */
4999 && !processing_template_decl
5000 && (complain
& tf_warning
)
5001 && c_inhibit_evaluation_warnings
== 0
5002 /* Even unsigned enum types promote to signed int. We don't
5003 want to issue -Wsign-compare warnings for this case. */
5004 && !enum_cast_to_int (orig_op0
)
5005 && !enum_cast_to_int (orig_op1
))
5007 tree oop0
= maybe_constant_value (orig_op0
);
5008 tree oop1
= maybe_constant_value (orig_op1
);
5010 if (TREE_CODE (oop0
) != INTEGER_CST
)
5012 if (TREE_CODE (oop1
) != INTEGER_CST
)
5014 warn_for_sign_compare (location
, oop0
, oop1
, op0
, op1
,
5015 result_type
, resultcode
);
5019 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5020 Then the expression will be built.
5021 It will be given type FINAL_TYPE if that is nonzero;
5022 otherwise, it will be given type RESULT_TYPE. */
5025 if (TREE_TYPE (op0
) != result_type
)
5026 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5027 if (TREE_TYPE (op1
) != result_type
)
5028 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5030 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5031 return error_mark_node
;
5034 if (build_type
== NULL_TREE
)
5035 build_type
= result_type
;
5037 if ((flag_sanitize
& (SANITIZE_SHIFT
| SANITIZE_DIVIDE
5038 | SANITIZE_FLOAT_DIVIDE
))
5039 && !processing_template_decl
5040 && do_ubsan_in_current_function ()
5041 && (doing_div_or_mod
|| doing_shift
))
5043 /* OP0 and/or OP1 might have side-effects. */
5044 op0
= cp_save_expr (op0
);
5045 op1
= cp_save_expr (op1
);
5046 op0
= fold_non_dependent_expr (op0
);
5047 op1
= fold_non_dependent_expr (op1
);
5048 if (doing_div_or_mod
&& (flag_sanitize
& (SANITIZE_DIVIDE
5049 | SANITIZE_FLOAT_DIVIDE
)))
5051 /* For diagnostics we want to use the promoted types without
5052 shorten_binary_op. So convert the arguments to the
5053 original result_type. */
5056 if (orig_type
!= NULL
&& result_type
!= orig_type
)
5058 cop0
= cp_convert (orig_type
, op0
, complain
);
5059 cop1
= cp_convert (orig_type
, op1
, complain
);
5061 instrument_expr
= ubsan_instrument_division (location
, cop0
, cop1
);
5063 else if (doing_shift
&& (flag_sanitize
& SANITIZE_SHIFT
))
5064 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
5067 result
= build2 (resultcode
, build_type
, op0
, op1
);
5068 result
= fold_if_not_in_template (result
);
5069 if (final_type
!= 0)
5070 result
= cp_convert (final_type
, result
, complain
);
5072 if (TREE_OVERFLOW_P (result
)
5073 && !TREE_OVERFLOW_P (op0
)
5074 && !TREE_OVERFLOW_P (op1
))
5075 overflow_warning (location
, result
);
5077 if (instrument_expr
!= NULL
)
5078 result
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
5079 instrument_expr
, result
);
5084 /* Build a VEC_PERM_EXPR.
5085 This is a simple wrapper for c_build_vec_perm_expr. */
5087 build_x_vec_perm_expr (location_t loc
,
5088 tree arg0
, tree arg1
, tree arg2
,
5089 tsubst_flags_t complain
)
5091 tree orig_arg0
= arg0
;
5092 tree orig_arg1
= arg1
;
5093 tree orig_arg2
= arg2
;
5094 if (processing_template_decl
)
5096 if (type_dependent_expression_p (arg0
)
5097 || type_dependent_expression_p (arg1
)
5098 || type_dependent_expression_p (arg2
))
5099 return build_min_nt_loc (loc
, VEC_PERM_EXPR
, arg0
, arg1
, arg2
);
5100 arg0
= build_non_dependent_expr (arg0
);
5102 arg1
= build_non_dependent_expr (arg1
);
5103 arg2
= build_non_dependent_expr (arg2
);
5105 tree exp
= c_build_vec_perm_expr (loc
, arg0
, arg1
, arg2
, complain
& tf_error
);
5106 if (processing_template_decl
&& exp
!= error_mark_node
)
5107 return build_min_non_dep (VEC_PERM_EXPR
, exp
, orig_arg0
,
5108 orig_arg1
, orig_arg2
);
5112 /* Return a tree for the sum or difference (RESULTCODE says which)
5113 of pointer PTROP and integer INTOP. */
5116 cp_pointer_int_sum (enum tree_code resultcode
, tree ptrop
, tree intop
,
5117 tsubst_flags_t complain
)
5119 tree res_type
= TREE_TYPE (ptrop
);
5121 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5122 in certain circumstance (when it's valid to do so). So we need
5123 to make sure it's complete. We don't need to check here, if we
5124 can actually complete it at all, as those checks will be done in
5125 pointer_int_sum() anyway. */
5126 complete_type (TREE_TYPE (res_type
));
5128 return pointer_int_sum (input_location
, resultcode
, ptrop
,
5129 fold_if_not_in_template (intop
),
5130 complain
& tf_warning_or_error
);
5133 /* Return a tree for the difference of pointers OP0 and OP1.
5134 The resulting tree has type int. */
5137 pointer_diff (tree op0
, tree op1
, tree ptrtype
, tsubst_flags_t complain
)
5140 tree restype
= ptrdiff_type_node
;
5141 tree target_type
= TREE_TYPE (ptrtype
);
5143 if (!complete_type_or_else (target_type
, NULL_TREE
))
5144 return error_mark_node
;
5146 if (VOID_TYPE_P (target_type
))
5148 if (complain
& tf_error
)
5149 permerror (input_location
, "ISO C++ forbids using pointer of "
5150 "type %<void *%> in subtraction");
5152 return error_mark_node
;
5154 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
5156 if (complain
& tf_error
)
5157 permerror (input_location
, "ISO C++ forbids using pointer to "
5158 "a function in subtraction");
5160 return error_mark_node
;
5162 if (TREE_CODE (target_type
) == METHOD_TYPE
)
5164 if (complain
& tf_error
)
5165 permerror (input_location
, "ISO C++ forbids using pointer to "
5166 "a method in subtraction");
5168 return error_mark_node
;
5171 /* First do the subtraction as integers;
5172 then drop through to build the divide operator. */
5174 op0
= cp_build_binary_op (input_location
,
5176 cp_convert (restype
, op0
, complain
),
5177 cp_convert (restype
, op1
, complain
),
5180 /* This generates an error if op1 is a pointer to an incomplete type. */
5181 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
5183 if (complain
& tf_error
)
5184 error ("invalid use of a pointer to an incomplete type in "
5185 "pointer arithmetic");
5187 return error_mark_node
;
5190 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1
)))
5192 if (complain
& tf_error
)
5193 error ("arithmetic on pointer to an empty aggregate");
5195 return error_mark_node
;
5198 op1
= (TYPE_PTROB_P (ptrtype
)
5199 ? size_in_bytes (target_type
)
5200 : integer_one_node
);
5202 /* Do the division. */
5204 result
= build2 (EXACT_DIV_EXPR
, restype
, op0
,
5205 cp_convert (restype
, op1
, complain
));
5206 return fold_if_not_in_template (result
);
5209 /* Construct and perhaps optimize a tree representation
5210 for a unary operation. CODE, a tree_code, specifies the operation
5211 and XARG is the operand. */
5214 build_x_unary_op (location_t loc
, enum tree_code code
, tree xarg
,
5215 tsubst_flags_t complain
)
5217 tree orig_expr
= xarg
;
5221 if (processing_template_decl
)
5223 if (type_dependent_expression_p (xarg
))
5224 return build_min_nt_loc (loc
, code
, xarg
, NULL_TREE
);
5226 xarg
= build_non_dependent_expr (xarg
);
5231 /* [expr.unary.op] says:
5233 The address of an object of incomplete type can be taken.
5235 (And is just the ordinary address operator, not an overloaded
5236 "operator &".) However, if the type is a template
5237 specialization, we must complete the type at this point so that
5238 an overloaded "operator &" will be available if required. */
5239 if (code
== ADDR_EXPR
5240 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
5241 && ((CLASS_TYPE_P (TREE_TYPE (xarg
))
5242 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg
))))
5243 || (TREE_CODE (xarg
) == OFFSET_REF
)))
5244 /* Don't look for a function. */;
5246 exp
= build_new_op (loc
, code
, LOOKUP_NORMAL
, xarg
, NULL_TREE
,
5247 NULL_TREE
, /*overload=*/NULL
, complain
);
5248 if (!exp
&& code
== ADDR_EXPR
)
5250 if (is_overloaded_fn (xarg
))
5252 tree fn
= get_first_fn (xarg
);
5253 if (DECL_CONSTRUCTOR_P (fn
) || DECL_DESTRUCTOR_P (fn
))
5255 if (complain
& tf_error
)
5256 error (DECL_CONSTRUCTOR_P (fn
)
5257 ? G_("taking address of constructor %qE")
5258 : G_("taking address of destructor %qE"),
5260 return error_mark_node
;
5264 /* A pointer to member-function can be formed only by saying
5266 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
5267 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
5269 if (TREE_CODE (xarg
) != OFFSET_REF
5270 || !TYPE_P (TREE_OPERAND (xarg
, 0)))
5272 if (complain
& tf_error
)
5274 error ("invalid use of %qE to form a "
5275 "pointer-to-member-function", xarg
);
5276 if (TREE_CODE (xarg
) != OFFSET_REF
)
5277 inform (input_location
, " a qualified-id is required");
5279 return error_mark_node
;
5283 if (complain
& tf_error
)
5284 error ("parentheses around %qE cannot be used to form a"
5285 " pointer-to-member-function",
5288 return error_mark_node
;
5289 PTRMEM_OK_P (xarg
) = 1;
5293 if (TREE_CODE (xarg
) == OFFSET_REF
)
5295 ptrmem
= PTRMEM_OK_P (xarg
);
5297 if (!ptrmem
&& !flag_ms_extensions
5298 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
5300 /* A single non-static member, make sure we don't allow a
5301 pointer-to-member. */
5302 xarg
= build2 (OFFSET_REF
, TREE_TYPE (xarg
),
5303 TREE_OPERAND (xarg
, 0),
5304 ovl_cons (TREE_OPERAND (xarg
, 1), NULL_TREE
));
5305 PTRMEM_OK_P (xarg
) = ptrmem
;
5309 exp
= cp_build_addr_expr_strict (xarg
, complain
);
5312 if (processing_template_decl
&& exp
!= error_mark_node
)
5313 exp
= build_min_non_dep (code
, exp
, orig_expr
,
5314 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE
);
5315 if (TREE_CODE (exp
) == ADDR_EXPR
)
5316 PTRMEM_OK_P (exp
) = ptrmem
;
5320 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5321 constants, where a null value is represented by an INTEGER_CST of
5325 cp_truthvalue_conversion (tree expr
)
5327 tree type
= TREE_TYPE (expr
);
5328 if (TYPE_PTRDATAMEM_P (type
)
5329 /* Avoid ICE on invalid use of non-static member function. */
5330 || TREE_CODE (expr
) == FUNCTION_DECL
)
5331 return build_binary_op (EXPR_LOCATION (expr
),
5332 NE_EXPR
, expr
, nullptr_node
, 1);
5333 else if (TYPE_PTR_P (type
) || TYPE_PTRMEMFUNC_P (type
))
5335 /* With -Wzero-as-null-pointer-constant do not warn for an
5336 'if (p)' or a 'while (!p)', where p is a pointer. */
5338 ++c_inhibit_evaluation_warnings
;
5339 ret
= c_common_truthvalue_conversion (input_location
, expr
);
5340 --c_inhibit_evaluation_warnings
;
5344 return c_common_truthvalue_conversion (input_location
, expr
);
5347 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5350 condition_conversion (tree expr
)
5353 if (processing_template_decl
)
5355 t
= perform_implicit_conversion_flags (boolean_type_node
, expr
,
5356 tf_warning_or_error
, LOOKUP_NORMAL
);
5357 t
= fold_build_cleanup_point_expr (boolean_type_node
, t
);
5361 /* Returns the address of T. This function will fold away
5362 ADDR_EXPR of INDIRECT_REF. */
5365 build_address (tree t
)
5367 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
5368 return error_mark_node
;
5369 gcc_checking_assert (TREE_CODE (t
) != CONSTRUCTOR
);
5370 t
= build_fold_addr_expr (t
);
5371 if (TREE_CODE (t
) != ADDR_EXPR
)
5376 /* Return a NOP_EXPR converting EXPR to TYPE. */
5379 build_nop (tree type
, tree expr
)
5381 if (type
== error_mark_node
|| error_operand_p (expr
))
5383 return build1 (NOP_EXPR
, type
, expr
);
5386 /* Take the address of ARG, whatever that means under C++ semantics.
5387 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5388 and class rvalues as well.
5390 Nothing should call this function directly; instead, callers should use
5391 cp_build_addr_expr or cp_build_addr_expr_strict. */
5394 cp_build_addr_expr_1 (tree arg
, bool strict_lvalue
, tsubst_flags_t complain
)
5399 if (!arg
|| error_operand_p (arg
))
5400 return error_mark_node
;
5402 arg
= mark_lvalue_use (arg
);
5403 argtype
= lvalue_type (arg
);
5405 gcc_assert (!identifier_p (arg
) || !IDENTIFIER_OPNAME_P (arg
));
5407 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
5408 && !really_overloaded_fn (TREE_OPERAND (arg
, 1)))
5410 /* They're trying to take the address of a unique non-static
5411 member function. This is ill-formed (except in MS-land),
5412 but let's try to DTRT.
5413 Note: We only handle unique functions here because we don't
5414 want to complain if there's a static overload; non-unique
5415 cases will be handled by instantiate_type. But we need to
5416 handle this case here to allow casts on the resulting PMF.
5417 We could defer this in non-MS mode, but it's easier to give
5418 a useful error here. */
5420 /* Inside constant member functions, the `this' pointer
5421 contains an extra const qualifier. TYPE_MAIN_VARIANT
5422 is used here to remove this const from the diagnostics
5423 and the created OFFSET_REF. */
5424 tree base
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg
, 0)));
5425 tree fn
= get_first_fn (TREE_OPERAND (arg
, 1));
5426 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
5427 return error_mark_node
;
5429 if (! flag_ms_extensions
)
5431 tree name
= DECL_NAME (fn
);
5432 if (!(complain
& tf_error
))
5433 return error_mark_node
;
5434 else if (current_class_type
5435 && TREE_OPERAND (arg
, 0) == current_class_ref
)
5436 /* An expression like &memfn. */
5437 permerror (input_location
, "ISO C++ forbids taking the address of an unqualified"
5438 " or parenthesized non-static member function to form"
5439 " a pointer to member function. Say %<&%T::%D%>",
5442 permerror (input_location
, "ISO C++ forbids taking the address of a bound member"
5443 " function to form a pointer to member function."
5447 arg
= build_offset_ref (base
, fn
, /*address_p=*/true, complain
);
5450 /* Uninstantiated types are all functions. Taking the
5451 address of a function is a no-op, so just return the
5453 if (type_unknown_p (arg
))
5454 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
5456 if (TREE_CODE (arg
) == OFFSET_REF
)
5457 /* We want a pointer to member; bypass all the code for actually taking
5458 the address of something. */
5461 /* Anything not already handled and not a true memory reference
5463 if (TREE_CODE (argtype
) != FUNCTION_TYPE
5464 && TREE_CODE (argtype
) != METHOD_TYPE
)
5466 cp_lvalue_kind kind
= lvalue_kind (arg
);
5467 if (kind
== clk_none
)
5469 if (complain
& tf_error
)
5470 lvalue_error (input_location
, lv_addressof
);
5471 return error_mark_node
;
5473 if (strict_lvalue
&& (kind
& (clk_rvalueref
|clk_class
)))
5475 if (!(complain
& tf_error
))
5476 return error_mark_node
;
5477 if (kind
& clk_class
)
5478 /* Make this a permerror because we used to accept it. */
5479 permerror (input_location
, "taking address of temporary");
5481 error ("taking address of xvalue (rvalue reference)");
5485 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
5487 tree type
= build_pointer_type (TREE_TYPE (argtype
));
5488 arg
= build1 (CONVERT_EXPR
, type
, arg
);
5491 else if (pedantic
&& DECL_MAIN_P (arg
))
5494 /* Apparently a lot of autoconf scripts for C++ packages do this,
5495 so only complain if -Wpedantic. */
5496 if (complain
& (flag_pedantic_errors
? tf_error
: tf_warning
))
5497 pedwarn (input_location
, OPT_Wpedantic
,
5498 "ISO C++ forbids taking address of function %<::main%>");
5499 else if (flag_pedantic_errors
)
5500 return error_mark_node
;
5503 /* Let &* cancel out to simplify resulting code. */
5504 if (INDIRECT_REF_P (arg
))
5506 /* We don't need to have `current_class_ptr' wrapped in a
5507 NON_LVALUE_EXPR node. */
5508 if (arg
== current_class_ref
)
5509 return current_class_ptr
;
5511 arg
= TREE_OPERAND (arg
, 0);
5512 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
5514 tree type
= build_pointer_type (TREE_TYPE (TREE_TYPE (arg
)));
5515 arg
= build1 (CONVERT_EXPR
, type
, arg
);
5518 /* Don't let this be an lvalue. */
5523 /* ??? Cope with user tricks that amount to offsetof. */
5524 if (TREE_CODE (argtype
) != FUNCTION_TYPE
5525 && TREE_CODE (argtype
) != METHOD_TYPE
5526 && argtype
!= unknown_type_node
5527 && (val
= get_base_address (arg
))
5528 && COMPLETE_TYPE_P (TREE_TYPE (val
))
5529 && INDIRECT_REF_P (val
)
5530 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
5532 tree type
= build_pointer_type (argtype
);
5533 return fold_convert (type
, fold_offsetof_1 (arg
));
5536 /* Handle complex lvalues (when permitted)
5537 by reduction to simpler cases. */
5538 val
= unary_complex_lvalue (ADDR_EXPR
, arg
);
5542 switch (TREE_CODE (arg
))
5546 case FIX_TRUNC_EXPR
:
5547 /* Even if we're not being pedantic, we cannot allow this
5548 extension when we're instantiating in a SFINAE
5550 if (! lvalue_p (arg
) && complain
== tf_none
)
5552 if (complain
& tf_error
)
5553 permerror (input_location
, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5555 return error_mark_node
;
5560 arg
= BASELINK_FUNCTIONS (arg
);
5564 arg
= OVL_CURRENT (arg
);
5569 /* Turn a reference to a non-static data member into a
5570 pointer-to-member. */
5575 gcc_assert (PTRMEM_OK_P (arg
));
5577 t
= TREE_OPERAND (arg
, 1);
5578 if (TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
5580 if (complain
& tf_error
)
5581 error ("cannot create pointer to reference member %qD", t
);
5582 return error_mark_node
;
5585 type
= build_ptrmem_type (context_for_name_lookup (t
),
5587 t
= make_ptrmem_cst (type
, TREE_OPERAND (arg
, 1));
5595 if (argtype
!= error_mark_node
)
5596 argtype
= build_pointer_type (argtype
);
5598 /* In a template, we are processing a non-dependent expression
5599 so we can just form an ADDR_EXPR with the correct type. */
5600 if (processing_template_decl
|| TREE_CODE (arg
) != COMPONENT_REF
)
5602 val
= build_address (arg
);
5603 if (TREE_CODE (arg
) == OFFSET_REF
)
5604 PTRMEM_OK_P (val
) = PTRMEM_OK_P (arg
);
5606 else if (BASELINK_P (TREE_OPERAND (arg
, 1)))
5608 tree fn
= BASELINK_FUNCTIONS (TREE_OPERAND (arg
, 1));
5610 /* We can only get here with a single static member
5612 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
5613 && DECL_STATIC_FUNCTION_P (fn
));
5614 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
5615 return error_mark_node
;
5616 val
= build_address (fn
);
5617 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
5618 /* Do not lose object's side effects. */
5619 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
),
5620 TREE_OPERAND (arg
, 0), val
);
5622 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)))
5624 if (complain
& tf_error
)
5625 error ("attempt to take address of bit-field structure member %qD",
5626 TREE_OPERAND (arg
, 1));
5627 return error_mark_node
;
5631 tree object
= TREE_OPERAND (arg
, 0);
5632 tree field
= TREE_OPERAND (arg
, 1);
5633 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5634 (TREE_TYPE (object
), decl_type_context (field
)));
5635 val
= build_address (arg
);
5638 if (TYPE_PTR_P (argtype
)
5639 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
5641 build_ptrmemfunc_type (argtype
);
5642 val
= build_ptrmemfunc (argtype
, val
, 0,
5650 /* Take the address of ARG if it has one, even if it's an rvalue. */
5653 cp_build_addr_expr (tree arg
, tsubst_flags_t complain
)
5655 return cp_build_addr_expr_1 (arg
, 0, complain
);
5658 /* Take the address of ARG, but only if it's an lvalue. */
5661 cp_build_addr_expr_strict (tree arg
, tsubst_flags_t complain
)
5663 return cp_build_addr_expr_1 (arg
, 1, complain
);
5666 /* C++: Must handle pointers to members.
5668 Perhaps type instantiation should be extended to handle conversion
5669 from aggregates to types we don't yet know we want? (Or are those
5670 cases typically errors which should be reported?)
5672 NOCONVERT nonzero suppresses the default promotions
5673 (such as from short to int). */
5676 cp_build_unary_op (enum tree_code code
, tree xarg
, int noconvert
,
5677 tsubst_flags_t complain
)
5679 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5682 const char *errstring
= NULL
;
5684 const char *invalid_op_diag
;
5686 if (!arg
|| error_operand_p (arg
))
5687 return error_mark_node
;
5689 if ((invalid_op_diag
5690 = targetm
.invalid_unary_op ((code
== UNARY_PLUS_EXPR
5695 if (complain
& tf_error
)
5696 error (invalid_op_diag
);
5697 return error_mark_node
;
5702 case UNARY_PLUS_EXPR
:
5705 int flags
= WANT_ARITH
| WANT_ENUM
;
5706 /* Unary plus (but not unary minus) is allowed on pointers. */
5707 if (code
== UNARY_PLUS_EXPR
)
5708 flags
|= WANT_POINTER
;
5709 arg
= build_expr_type_conversion (flags
, arg
, true);
5711 errstring
= (code
== NEGATE_EXPR
5712 ? _("wrong type argument to unary minus")
5713 : _("wrong type argument to unary plus"));
5716 if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
5717 arg
= cp_perform_integral_promotions (arg
, complain
);
5719 /* Make sure the result is not an lvalue: a unary plus or minus
5720 expression is always a rvalue. */
5727 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
5732 arg
= cp_default_conversion (arg
, complain
);
5733 if (arg
== error_mark_node
)
5734 return error_mark_node
;
5737 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
5738 | WANT_VECTOR_OR_COMPLEX
,
5740 errstring
= _("wrong type argument to bit-complement");
5741 else if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
5742 arg
= cp_perform_integral_promotions (arg
, complain
);
5746 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
5747 errstring
= _("wrong type argument to abs");
5748 else if (!noconvert
)
5750 arg
= cp_default_conversion (arg
, complain
);
5751 if (arg
== error_mark_node
)
5752 return error_mark_node
;
5757 /* Conjugating a real value is a no-op, but allow it anyway. */
5758 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
5759 errstring
= _("wrong type argument to conjugation");
5760 else if (!noconvert
)
5762 arg
= cp_default_conversion (arg
, complain
);
5763 if (arg
== error_mark_node
)
5764 return error_mark_node
;
5768 case TRUTH_NOT_EXPR
:
5769 if (VECTOR_TYPE_P (TREE_TYPE (arg
)))
5770 return cp_build_binary_op (input_location
, EQ_EXPR
, arg
,
5771 build_zero_cst (TREE_TYPE (arg
)), complain
);
5772 arg
= perform_implicit_conversion (boolean_type_node
, arg
,
5774 val
= invert_truthvalue_loc (input_location
, arg
);
5775 if (arg
!= error_mark_node
)
5777 errstring
= _("in argument to unary !");
5785 arg
= build_real_imag_expr (input_location
, code
, arg
);
5786 if (arg
== error_mark_node
)
5789 return fold_if_not_in_template (arg
);
5791 case PREINCREMENT_EXPR
:
5792 case POSTINCREMENT_EXPR
:
5793 case PREDECREMENT_EXPR
:
5794 case POSTDECREMENT_EXPR
:
5795 /* Handle complex lvalues (when permitted)
5796 by reduction to simpler cases. */
5798 val
= unary_complex_lvalue (code
, arg
);
5802 arg
= mark_lvalue_use (arg
);
5804 /* Increment or decrement the real part of the value,
5805 and don't change the imaginary part. */
5806 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
5810 arg
= stabilize_reference (arg
);
5811 real
= cp_build_unary_op (REALPART_EXPR
, arg
, 1, complain
);
5812 imag
= cp_build_unary_op (IMAGPART_EXPR
, arg
, 1, complain
);
5813 real
= cp_build_unary_op (code
, real
, 1, complain
);
5814 if (real
== error_mark_node
|| imag
== error_mark_node
)
5815 return error_mark_node
;
5816 return build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
5820 /* Report invalid types. */
5822 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
5825 if (code
== PREINCREMENT_EXPR
)
5826 errstring
= _("no pre-increment operator for type");
5827 else if (code
== POSTINCREMENT_EXPR
)
5828 errstring
= _("no post-increment operator for type");
5829 else if (code
== PREDECREMENT_EXPR
)
5830 errstring
= _("no pre-decrement operator for type");
5832 errstring
= _("no post-decrement operator for type");
5835 else if (arg
== error_mark_node
)
5836 return error_mark_node
;
5838 /* Report something read-only. */
5840 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
5841 || TREE_READONLY (arg
))
5843 if (complain
& tf_error
)
5844 cxx_readonly_error (arg
, ((code
== PREINCREMENT_EXPR
5845 || code
== POSTINCREMENT_EXPR
)
5846 ? lv_increment
: lv_decrement
));
5848 return error_mark_node
;
5853 tree declared_type
= unlowered_expr_type (arg
);
5855 argtype
= TREE_TYPE (arg
);
5857 /* ARM $5.2.5 last annotation says this should be forbidden. */
5858 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
5860 if (complain
& tf_error
)
5861 permerror (input_location
, (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
5862 ? G_("ISO C++ forbids incrementing an enum")
5863 : G_("ISO C++ forbids decrementing an enum"));
5865 return error_mark_node
;
5868 /* Compute the increment. */
5870 if (TYPE_PTR_P (argtype
))
5872 tree type
= complete_type (TREE_TYPE (argtype
));
5874 if (!COMPLETE_OR_VOID_TYPE_P (type
))
5876 if (complain
& tf_error
)
5877 error (((code
== PREINCREMENT_EXPR
5878 || code
== POSTINCREMENT_EXPR
))
5879 ? G_("cannot increment a pointer to incomplete type %qT")
5880 : G_("cannot decrement a pointer to incomplete type %qT"),
5881 TREE_TYPE (argtype
));
5883 return error_mark_node
;
5885 else if (!TYPE_PTROB_P (argtype
))
5887 if (complain
& tf_error
)
5888 pedwarn (input_location
, OPT_Wpointer_arith
,
5889 (code
== PREINCREMENT_EXPR
5890 || code
== POSTINCREMENT_EXPR
)
5891 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5892 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5895 return error_mark_node
;
5898 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
5901 inc
= VECTOR_TYPE_P (argtype
)
5902 ? build_one_cst (argtype
)
5905 inc
= cp_convert (argtype
, inc
, complain
);
5907 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5908 need to ask Objective-C to build the increment or decrement
5909 expression for it. */
5910 if (objc_is_property_ref (arg
))
5911 return objc_build_incr_expr_for_property_ref (input_location
, code
,
5914 /* Complain about anything else that is not a true lvalue. */
5915 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
5916 || code
== POSTINCREMENT_EXPR
)
5917 ? lv_increment
: lv_decrement
),
5919 return error_mark_node
;
5921 /* Forbid using -- on `bool'. */
5922 if (TREE_CODE (declared_type
) == BOOLEAN_TYPE
)
5924 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
5926 if (complain
& tf_error
)
5927 error ("invalid use of Boolean expression as operand "
5928 "to %<operator--%>");
5929 return error_mark_node
;
5931 val
= boolean_increment (code
, arg
);
5933 else if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
5934 /* An rvalue has no cv-qualifiers. */
5935 val
= build2 (code
, cv_unqualified (TREE_TYPE (arg
)), arg
, inc
);
5937 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
5939 TREE_SIDE_EFFECTS (val
) = 1;
5944 /* Note that this operation never does default_conversion
5945 regardless of NOCONVERT. */
5946 return cp_build_addr_expr (arg
, complain
);
5955 argtype
= TREE_TYPE (arg
);
5956 return fold_if_not_in_template (build1 (code
, argtype
, arg
));
5959 if (complain
& tf_error
)
5960 error ("%s", errstring
);
5961 return error_mark_node
;
5964 /* Hook for the c-common bits that build a unary op. */
5966 build_unary_op (location_t
/*location*/,
5967 enum tree_code code
, tree xarg
, int noconvert
)
5969 return cp_build_unary_op (code
, xarg
, noconvert
, tf_warning_or_error
);
5972 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5973 for certain kinds of expressions which are not really lvalues
5974 but which we can accept as lvalues.
5976 If ARG is not a kind of expression we can handle, return
5980 unary_complex_lvalue (enum tree_code code
, tree arg
)
5982 /* Inside a template, making these kinds of adjustments is
5983 pointless; we are only concerned with the type of the
5985 if (processing_template_decl
)
5988 /* Handle (a, b) used as an "lvalue". */
5989 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
5991 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 1), 0,
5992 tf_warning_or_error
);
5993 return build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
5994 TREE_OPERAND (arg
, 0), real_result
);
5997 /* Handle (a ? b : c) used as an "lvalue". */
5998 if (TREE_CODE (arg
) == COND_EXPR
5999 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
6000 return rationalize_conditional_expr (code
, arg
, tf_warning_or_error
);
6002 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6003 if (TREE_CODE (arg
) == MODIFY_EXPR
6004 || TREE_CODE (arg
) == PREINCREMENT_EXPR
6005 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
6007 tree lvalue
= TREE_OPERAND (arg
, 0);
6008 if (TREE_SIDE_EFFECTS (lvalue
))
6010 lvalue
= stabilize_reference (lvalue
);
6011 arg
= build2 (TREE_CODE (arg
), TREE_TYPE (arg
),
6012 lvalue
, TREE_OPERAND (arg
, 1));
6014 return unary_complex_lvalue
6015 (code
, build2 (COMPOUND_EXPR
, TREE_TYPE (lvalue
), arg
, lvalue
));
6018 if (code
!= ADDR_EXPR
)
6021 /* Handle (a = b) used as an "lvalue" for `&'. */
6022 if (TREE_CODE (arg
) == MODIFY_EXPR
6023 || TREE_CODE (arg
) == INIT_EXPR
)
6025 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 0), 0,
6026 tf_warning_or_error
);
6027 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6029 TREE_NO_WARNING (arg
) = 1;
6033 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
6034 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
6035 || TREE_CODE (arg
) == OFFSET_REF
)
6038 /* We permit compiler to make function calls returning
6039 objects of aggregate type look like lvalues. */
6043 if (TREE_CODE (targ
) == SAVE_EXPR
)
6044 targ
= TREE_OPERAND (targ
, 0);
6046 if (TREE_CODE (targ
) == CALL_EXPR
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (targ
)))
6048 if (TREE_CODE (arg
) == SAVE_EXPR
)
6051 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, tf_warning_or_error
);
6052 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
6055 if (TREE_CODE (arg
) == SAVE_EXPR
&& INDIRECT_REF_P (targ
))
6056 return build3 (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
6057 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
6060 /* Don't let anything else be handled specially. */
6064 /* Mark EXP saying that we need to be able to take the
6065 address of it; it should not be allocated in a register.
6066 Value is true if successful.
6068 C++: we do not allow `current_class_ptr' to be addressable. */
6071 cxx_mark_addressable (tree exp
)
6076 switch (TREE_CODE (x
))
6083 x
= TREE_OPERAND (x
, 0);
6087 if (x
== current_class_ptr
)
6089 error ("cannot take the address of %<this%>, which is an rvalue expression");
6090 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later. */
6096 /* Caller should not be trying to mark initialized
6097 constant fields addressable. */
6098 gcc_assert (DECL_LANG_SPECIFIC (x
) == 0
6099 || DECL_IN_AGGR_P (x
) == 0
6101 || DECL_EXTERNAL (x
));
6105 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
6106 && !DECL_ARTIFICIAL (x
))
6108 if (VAR_P (x
) && DECL_HARD_REGISTER (x
))
6111 ("address of explicit register variable %qD requested", x
);
6114 else if (extra_warnings
)
6116 (OPT_Wextra
, "address requested for %qD, which is declared %<register%>", x
);
6118 TREE_ADDRESSABLE (x
) = 1;
6123 TREE_ADDRESSABLE (x
) = 1;
6127 TREE_ADDRESSABLE (x
) = 1;
6131 TREE_ADDRESSABLE (x
) = 1;
6132 cxx_mark_addressable (TREE_OPERAND (x
, 0));
6140 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6143 build_x_conditional_expr (location_t loc
, tree ifexp
, tree op1
, tree op2
,
6144 tsubst_flags_t complain
)
6146 tree orig_ifexp
= ifexp
;
6147 tree orig_op1
= op1
;
6148 tree orig_op2
= op2
;
6151 if (processing_template_decl
)
6153 /* The standard says that the expression is type-dependent if
6154 IFEXP is type-dependent, even though the eventual type of the
6155 expression doesn't dependent on IFEXP. */
6156 if (type_dependent_expression_p (ifexp
)
6157 /* As a GNU extension, the middle operand may be omitted. */
6158 || (op1
&& type_dependent_expression_p (op1
))
6159 || type_dependent_expression_p (op2
))
6160 return build_min_nt_loc (loc
, COND_EXPR
, ifexp
, op1
, op2
);
6161 ifexp
= build_non_dependent_expr (ifexp
);
6163 op1
= build_non_dependent_expr (op1
);
6164 op2
= build_non_dependent_expr (op2
);
6167 expr
= build_conditional_expr (loc
, ifexp
, op1
, op2
, complain
);
6168 if (processing_template_decl
&& expr
!= error_mark_node
6169 && TREE_CODE (expr
) != VEC_COND_EXPR
)
6171 tree min
= build_min_non_dep (COND_EXPR
, expr
,
6172 orig_ifexp
, orig_op1
, orig_op2
);
6173 /* In C++11, remember that the result is an lvalue or xvalue.
6174 In C++98, lvalue_kind can just assume lvalue in a template. */
6175 if (cxx_dialect
>= cxx11
6176 && lvalue_or_rvalue_with_address_p (expr
)
6177 && !lvalue_or_rvalue_with_address_p (min
))
6178 TREE_TYPE (min
) = cp_build_reference_type (TREE_TYPE (min
),
6179 !real_lvalue_p (expr
));
6180 expr
= convert_from_reference (min
);
6185 /* Given a list of expressions, return a compound expression
6186 that performs them all and returns the value of the last of them. */
6189 build_x_compound_expr_from_list (tree list
, expr_list_kind exp
,
6190 tsubst_flags_t complain
)
6192 tree expr
= TREE_VALUE (list
);
6194 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
6195 && !CONSTRUCTOR_IS_DIRECT_INIT (expr
))
6197 if (complain
& tf_error
)
6198 pedwarn (EXPR_LOC_OR_LOC (expr
, input_location
), 0,
6199 "list-initializer for non-class type must not "
6200 "be parenthesized");
6202 return error_mark_node
;
6205 if (TREE_CHAIN (list
))
6207 if (complain
& tf_error
)
6211 permerror (input_location
, "expression list treated as compound "
6212 "expression in initializer");
6215 permerror (input_location
, "expression list treated as compound "
6216 "expression in mem-initializer");
6219 permerror (input_location
, "expression list treated as compound "
6220 "expression in functional cast");
6226 return error_mark_node
;
6228 for (list
= TREE_CHAIN (list
); list
; list
= TREE_CHAIN (list
))
6229 expr
= build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list
)),
6230 expr
, TREE_VALUE (list
), complain
);
6236 /* Like build_x_compound_expr_from_list, but using a VEC. */
6239 build_x_compound_expr_from_vec (vec
<tree
, va_gc
> *vec
, const char *msg
,
6240 tsubst_flags_t complain
)
6242 if (vec_safe_is_empty (vec
))
6244 else if (vec
->length () == 1)
6254 if (complain
& tf_error
)
6255 permerror (input_location
,
6256 "%s expression list treated as compound expression",
6259 return error_mark_node
;
6263 for (ix
= 1; vec
->iterate (ix
, &t
); ++ix
)
6264 expr
= build_x_compound_expr (EXPR_LOCATION (t
), expr
,
6271 /* Handle overloading of the ',' operator when needed. */
6274 build_x_compound_expr (location_t loc
, tree op1
, tree op2
,
6275 tsubst_flags_t complain
)
6278 tree orig_op1
= op1
;
6279 tree orig_op2
= op2
;
6281 if (processing_template_decl
)
6283 if (type_dependent_expression_p (op1
)
6284 || type_dependent_expression_p (op2
))
6285 return build_min_nt_loc (loc
, COMPOUND_EXPR
, op1
, op2
);
6286 op1
= build_non_dependent_expr (op1
);
6287 op2
= build_non_dependent_expr (op2
);
6290 result
= build_new_op (loc
, COMPOUND_EXPR
, LOOKUP_NORMAL
, op1
, op2
,
6291 NULL_TREE
, /*overload=*/NULL
, complain
);
6293 result
= cp_build_compound_expr (op1
, op2
, complain
);
6295 if (processing_template_decl
&& result
!= error_mark_node
)
6296 return build_min_non_dep (COMPOUND_EXPR
, result
, orig_op1
, orig_op2
);
6301 /* Like cp_build_compound_expr, but for the c-common bits. */
6304 build_compound_expr (location_t
/*loc*/, tree lhs
, tree rhs
)
6306 return cp_build_compound_expr (lhs
, rhs
, tf_warning_or_error
);
6309 /* Build a compound expression. */
6312 cp_build_compound_expr (tree lhs
, tree rhs
, tsubst_flags_t complain
)
6314 lhs
= convert_to_void (lhs
, ICV_LEFT_OF_COMMA
, complain
);
6316 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
6317 return error_mark_node
;
6320 && (TREE_CODE (lhs
) == CILK_SPAWN_STMT
6321 || TREE_CODE (rhs
) == CILK_SPAWN_STMT
))
6323 location_t loc
= (EXPR_HAS_LOCATION (lhs
) ? EXPR_LOCATION (lhs
)
6324 : EXPR_LOCATION (rhs
));
6326 "spawned function call cannot be part of a comma expression");
6327 return error_mark_node
;
6330 if (TREE_CODE (rhs
) == TARGET_EXPR
)
6332 /* If the rhs is a TARGET_EXPR, then build the compound
6333 expression inside the target_expr's initializer. This
6334 helps the compiler to eliminate unnecessary temporaries. */
6335 tree init
= TREE_OPERAND (rhs
, 1);
6337 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), lhs
, init
);
6338 TREE_OPERAND (rhs
, 1) = init
;
6343 if (type_unknown_p (rhs
))
6345 if (complain
& tf_error
)
6346 error ("no context to resolve type of %qE", rhs
);
6347 return error_mark_node
;
6350 return build2 (COMPOUND_EXPR
, TREE_TYPE (rhs
), lhs
, rhs
);
6353 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6354 casts away constness. CAST gives the type of cast. Returns true
6355 if the cast is ill-formed, false if it is well-formed.
6357 ??? This function warns for casting away any qualifier not just
6358 const. We would like to specify exactly what qualifiers are casted
6363 check_for_casting_away_constness (tree src_type
, tree dest_type
,
6364 enum tree_code cast
, tsubst_flags_t complain
)
6366 /* C-style casts are allowed to cast away constness. With
6367 WARN_CAST_QUAL, we still want to issue a warning. */
6368 if (cast
== CAST_EXPR
&& !warn_cast_qual
)
6371 if (!casts_away_constness (src_type
, dest_type
, complain
))
6377 if (complain
& tf_warning
)
6378 warning (OPT_Wcast_qual
,
6379 "cast from type %qT to type %qT casts away qualifiers",
6380 src_type
, dest_type
);
6383 case STATIC_CAST_EXPR
:
6384 if (complain
& tf_error
)
6385 error ("static_cast from type %qT to type %qT casts away qualifiers",
6386 src_type
, dest_type
);
6389 case REINTERPRET_CAST_EXPR
:
6390 if (complain
& tf_error
)
6391 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6392 src_type
, dest_type
);
6401 Warns if the cast from expression EXPR to type TYPE is useless.
6404 maybe_warn_about_useless_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6406 if (warn_useless_cast
6407 && complain
& tf_warning
)
6409 if ((TREE_CODE (type
) == REFERENCE_TYPE
6410 && (TYPE_REF_IS_RVALUE (type
)
6411 ? xvalue_p (expr
) : real_lvalue_p (expr
))
6412 && same_type_p (TREE_TYPE (expr
), TREE_TYPE (type
)))
6413 || same_type_p (TREE_TYPE (expr
), type
))
6414 warning (OPT_Wuseless_cast
, "useless cast to type %qT", type
);
6418 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6419 (another pointer-to-member type in the same hierarchy) and return
6420 the converted expression. If ALLOW_INVERSE_P is permitted, a
6421 pointer-to-derived may be converted to pointer-to-base; otherwise,
6422 only the other direction is permitted. If C_CAST_P is true, this
6423 conversion is taking place as part of a C-style cast. */
6426 convert_ptrmem (tree type
, tree expr
, bool allow_inverse_p
,
6427 bool c_cast_p
, tsubst_flags_t complain
)
6429 if (TYPE_PTRDATAMEM_P (type
))
6433 if (TREE_CODE (expr
) == PTRMEM_CST
)
6434 expr
= cplus_expand_constant (expr
);
6435 delta
= get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr
)),
6436 TYPE_PTRMEM_CLASS_TYPE (type
),
6438 c_cast_p
, complain
);
6439 if (delta
== error_mark_node
)
6440 return error_mark_node
;
6442 if (!integer_zerop (delta
))
6444 tree cond
, op1
, op2
;
6446 cond
= cp_build_binary_op (input_location
,
6449 build_int_cst (TREE_TYPE (expr
), -1),
6451 op1
= build_nop (ptrdiff_type_node
, expr
);
6452 op2
= cp_build_binary_op (input_location
,
6453 PLUS_EXPR
, op1
, delta
,
6456 expr
= fold_build3_loc (input_location
,
6457 COND_EXPR
, ptrdiff_type_node
, cond
, op1
, op2
);
6461 return build_nop (type
, expr
);
6464 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
,
6465 allow_inverse_p
, c_cast_p
, complain
);
6468 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6469 this static_cast is being attempted as one of the possible casts
6470 allowed by a C-style cast. (In that case, accessibility of base
6471 classes is not considered, and it is OK to cast away
6472 constness.) Return the result of the cast. *VALID_P is set to
6473 indicate whether or not the cast was valid. */
6476 build_static_cast_1 (tree type
, tree expr
, bool c_cast_p
,
6477 bool *valid_p
, tsubst_flags_t complain
)
6483 /* Assume the cast is valid. */
6486 intype
= unlowered_expr_type (expr
);
6488 /* Save casted types in the function's used types hash table. */
6489 used_types_insert (type
);
6491 /* [expr.static.cast]
6493 An lvalue of type "cv1 B", where B is a class type, can be cast
6494 to type "reference to cv2 D", where D is a class derived (clause
6495 _class.derived_) from B, if a valid standard conversion from
6496 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6497 same cv-qualification as, or greater cv-qualification than, cv1,
6498 and B is not a virtual base class of D. */
6499 /* We check this case before checking the validity of "TYPE t =
6500 EXPR;" below because for this case:
6503 struct D : public B { D(const B&); };
6505 void f() { static_cast<const D&>(b); }
6507 we want to avoid constructing a new D. The standard is not
6508 completely clear about this issue, but our interpretation is
6509 consistent with other compilers. */
6510 if (TREE_CODE (type
) == REFERENCE_TYPE
6511 && CLASS_TYPE_P (TREE_TYPE (type
))
6512 && CLASS_TYPE_P (intype
)
6513 && (TYPE_REF_IS_RVALUE (type
) || real_lvalue_p (expr
))
6514 && DERIVED_FROM_P (intype
, TREE_TYPE (type
))
6515 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype
)),
6516 build_pointer_type (TYPE_MAIN_VARIANT
6517 (TREE_TYPE (type
))),
6520 || at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
6524 /* There is a standard conversion from "D*" to "B*" even if "B"
6525 is ambiguous or inaccessible. If this is really a
6526 static_cast, then we check both for inaccessibility and
6527 ambiguity. However, if this is a static_cast being performed
6528 because the user wrote a C-style cast, then accessibility is
6530 base
= lookup_base (TREE_TYPE (type
), intype
,
6531 c_cast_p
? ba_unique
: ba_check
,
6533 expr
= build_address (expr
);
6535 if (flag_sanitize
& SANITIZE_VPTR
)
6538 = cp_ubsan_maybe_instrument_downcast (input_location
, type
, expr
);
6543 /* Convert from "B*" to "D*". This function will check that "B"
6544 is not a virtual base of "D". */
6545 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
6548 /* Convert the pointer to a reference -- but then remember that
6549 there are no expressions with reference type in C++.
6551 We call rvalue so that there's an actual tree code
6552 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6553 is a variable with the same type, the conversion would get folded
6554 away, leaving just the variable and causing lvalue_kind to give
6555 the wrong answer. */
6556 return convert_from_reference (rvalue (cp_fold_convert (type
, expr
)));
6559 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6560 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6561 if (TREE_CODE (type
) == REFERENCE_TYPE
6562 && TYPE_REF_IS_RVALUE (type
)
6563 && (clk
= real_lvalue_p (expr
))
6564 && reference_related_p (TREE_TYPE (type
), intype
)
6565 && (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
6567 if (clk
== clk_ordinary
)
6569 /* Handle the (non-bit-field) lvalue case here by casting to
6570 lvalue reference and then changing it to an rvalue reference.
6571 Casting an xvalue to rvalue reference will be handled by the
6573 tree lref
= cp_build_reference_type (TREE_TYPE (type
), false);
6574 result
= (perform_direct_initialization_if_possible
6575 (lref
, expr
, c_cast_p
, complain
));
6576 result
= cp_fold_convert (type
, result
);
6577 /* Make sure we don't fold back down to a named rvalue reference,
6578 because that would be an lvalue. */
6579 if (DECL_P (result
))
6580 result
= build1 (NON_LVALUE_EXPR
, type
, result
);
6581 return convert_from_reference (result
);
6584 /* For a bit-field or packed field, bind to a temporary. */
6585 expr
= rvalue (expr
);
6588 /* Resolve overloaded address here rather than once in
6589 implicit_conversion and again in the inverse code below. */
6590 if (TYPE_PTRMEMFUNC_P (type
) && type_unknown_p (expr
))
6592 expr
= instantiate_type (type
, expr
, complain
);
6593 intype
= TREE_TYPE (expr
);
6596 /* [expr.static.cast]
6598 Any expression can be explicitly converted to type cv void. */
6599 if (VOID_TYPE_P (type
))
6600 return convert_to_void (expr
, ICV_CAST
, complain
);
6603 An abstract class shall not be used ... as the type of an explicit
6605 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
6606 return error_mark_node
;
6608 /* [expr.static.cast]
6610 An expression e can be explicitly converted to a type T using a
6611 static_cast of the form static_cast<T>(e) if the declaration T
6612 t(e);" is well-formed, for some invented temporary variable
6614 result
= perform_direct_initialization_if_possible (type
, expr
,
6615 c_cast_p
, complain
);
6618 result
= convert_from_reference (result
);
6620 /* [expr.static.cast]
6622 If T is a reference type, the result is an lvalue; otherwise,
6623 the result is an rvalue. */
6624 if (TREE_CODE (type
) != REFERENCE_TYPE
)
6625 result
= rvalue (result
);
6629 /* [expr.static.cast]
6631 The inverse of any standard conversion sequence (clause _conv_),
6632 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6633 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6634 (_conv.bool_) conversions, can be performed explicitly using
6635 static_cast subject to the restriction that the explicit
6636 conversion does not cast away constness (_expr.const.cast_), and
6637 the following additional rules for specific cases: */
6638 /* For reference, the conversions not excluded are: integral
6639 promotions, floating point promotion, integral conversions,
6640 floating point conversions, floating-integral conversions,
6641 pointer conversions, and pointer to member conversions. */
6644 A value of integral _or enumeration_ type can be explicitly
6645 converted to an enumeration type. */
6646 /* The effect of all that is that any conversion between any two
6647 types which are integral, floating, or enumeration types can be
6649 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6650 || SCALAR_FLOAT_TYPE_P (type
))
6651 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
6652 || SCALAR_FLOAT_TYPE_P (intype
)))
6653 return ocp_convert (type
, expr
, CONV_C_CAST
, LOOKUP_NORMAL
, complain
);
6655 if (TYPE_PTR_P (type
) && TYPE_PTR_P (intype
)
6656 && CLASS_TYPE_P (TREE_TYPE (type
))
6657 && CLASS_TYPE_P (TREE_TYPE (intype
))
6658 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6659 (TREE_TYPE (intype
))),
6660 build_pointer_type (TYPE_MAIN_VARIANT
6661 (TREE_TYPE (type
))),
6667 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
6669 return error_mark_node
;
6670 base
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
6671 c_cast_p
? ba_unique
: ba_check
,
6673 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
6676 if (flag_sanitize
& SANITIZE_VPTR
)
6679 = cp_ubsan_maybe_instrument_downcast (input_location
, type
, expr
);
6684 return cp_fold_convert (type
, expr
);
6687 if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
6688 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
6695 c1
= TYPE_PTRMEM_CLASS_TYPE (intype
);
6696 c2
= TYPE_PTRMEM_CLASS_TYPE (type
);
6698 if (TYPE_PTRDATAMEM_P (type
))
6700 t1
= (build_ptrmem_type
6702 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype
))));
6703 t2
= (build_ptrmem_type
6705 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
6712 if (can_convert (t1
, t2
, complain
) || can_convert (t2
, t1
, complain
))
6715 && check_for_casting_away_constness (intype
, type
,
6718 return error_mark_node
;
6719 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/1,
6720 c_cast_p
, complain
);
6724 /* [expr.static.cast]
6726 An rvalue of type "pointer to cv void" can be explicitly
6727 converted to a pointer to object type. A value of type pointer
6728 to object converted to "pointer to cv void" and back to the
6729 original pointer type will have its original value. */
6730 if (TYPE_PTR_P (intype
)
6731 && VOID_TYPE_P (TREE_TYPE (intype
))
6732 && TYPE_PTROB_P (type
))
6735 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
6737 return error_mark_node
;
6738 return build_nop (type
, expr
);
6742 return error_mark_node
;
6745 /* Return an expression representing static_cast<TYPE>(EXPR). */
6748 build_static_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6753 if (type
== error_mark_node
|| expr
== error_mark_node
)
6754 return error_mark_node
;
6756 if (processing_template_decl
)
6758 expr
= build_min (STATIC_CAST_EXPR
, type
, expr
);
6759 /* We don't know if it will or will not have side effects. */
6760 TREE_SIDE_EFFECTS (expr
) = 1;
6761 return convert_from_reference (expr
);
6764 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6765 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6766 if (TREE_CODE (type
) != REFERENCE_TYPE
6767 && TREE_CODE (expr
) == NOP_EXPR
6768 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
6769 expr
= TREE_OPERAND (expr
, 0);
6771 result
= build_static_cast_1 (type
, expr
, /*c_cast_p=*/false, &valid_p
,
6775 if (result
!= error_mark_node
)
6776 maybe_warn_about_useless_cast (type
, expr
, complain
);
6780 if (complain
& tf_error
)
6781 error ("invalid static_cast from type %qT to type %qT",
6782 TREE_TYPE (expr
), type
);
6783 return error_mark_node
;
6786 /* EXPR is an expression with member function or pointer-to-member
6787 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6788 not permitted by ISO C++, but we accept it in some modes. If we
6789 are not in one of those modes, issue a diagnostic. Return the
6790 converted expression. */
6793 convert_member_func_to_ptr (tree type
, tree expr
, tsubst_flags_t complain
)
6798 intype
= TREE_TYPE (expr
);
6799 gcc_assert (TYPE_PTRMEMFUNC_P (intype
)
6800 || TREE_CODE (intype
) == METHOD_TYPE
);
6802 if (!(complain
& tf_warning_or_error
))
6803 return error_mark_node
;
6805 if (pedantic
|| warn_pmf2ptr
)
6806 pedwarn (input_location
, pedantic
? OPT_Wpedantic
: OPT_Wpmf_conversions
,
6807 "converting from %qT to %qT", intype
, type
);
6809 if (TREE_CODE (intype
) == METHOD_TYPE
)
6810 expr
= build_addr_func (expr
, complain
);
6811 else if (TREE_CODE (expr
) == PTRMEM_CST
)
6812 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
6815 decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
), 0);
6816 decl
= build_address (decl
);
6817 expr
= get_member_function_from_ptrfunc (&decl
, expr
, complain
);
6820 if (expr
== error_mark_node
)
6821 return error_mark_node
;
6823 return build_nop (type
, expr
);
6826 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6827 If C_CAST_P is true, this reinterpret cast is being done as part of
6828 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6829 indicate whether or not reinterpret_cast was valid. */
6832 build_reinterpret_cast_1 (tree type
, tree expr
, bool c_cast_p
,
6833 bool *valid_p
, tsubst_flags_t complain
)
6837 /* Assume the cast is invalid. */
6841 if (type
== error_mark_node
|| error_operand_p (expr
))
6842 return error_mark_node
;
6844 intype
= TREE_TYPE (expr
);
6846 /* Save casted types in the function's used types hash table. */
6847 used_types_insert (type
);
6849 /* [expr.reinterpret.cast]
6850 An lvalue expression of type T1 can be cast to the type
6851 "reference to T2" if an expression of type "pointer to T1" can be
6852 explicitly converted to the type "pointer to T2" using a
6853 reinterpret_cast. */
6854 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6856 if (! real_lvalue_p (expr
))
6858 if (complain
& tf_error
)
6859 error ("invalid cast of an rvalue expression of type "
6862 return error_mark_node
;
6865 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6866 "B" are related class types; the reinterpret_cast does not
6867 adjust the pointer. */
6868 if (TYPE_PTR_P (intype
)
6869 && (complain
& tf_warning
)
6870 && (comptypes (TREE_TYPE (intype
), TREE_TYPE (type
),
6871 COMPARE_BASE
| COMPARE_DERIVED
)))
6872 warning (0, "casting %qT to %qT does not dereference pointer",
6875 expr
= cp_build_addr_expr (expr
, complain
);
6877 if (warn_strict_aliasing
> 2)
6878 strict_aliasing_warning (TREE_TYPE (expr
), type
, expr
);
6880 if (expr
!= error_mark_node
)
6881 expr
= build_reinterpret_cast_1
6882 (build_pointer_type (TREE_TYPE (type
)), expr
, c_cast_p
,
6884 if (expr
!= error_mark_node
)
6885 /* cp_build_indirect_ref isn't right for rvalue refs. */
6886 expr
= convert_from_reference (fold_convert (type
, expr
));
6890 /* As a G++ extension, we consider conversions from member
6891 functions, and pointers to member functions to
6892 pointer-to-function and pointer-to-void types. If
6893 -Wno-pmf-conversions has not been specified,
6894 convert_member_func_to_ptr will issue an error message. */
6895 if ((TYPE_PTRMEMFUNC_P (intype
)
6896 || TREE_CODE (intype
) == METHOD_TYPE
)
6897 && TYPE_PTR_P (type
)
6898 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
6899 || VOID_TYPE_P (TREE_TYPE (type
))))
6900 return convert_member_func_to_ptr (type
, expr
, complain
);
6902 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6903 array-to-pointer, and function-to-pointer conversions are
6905 expr
= decay_conversion (expr
, complain
);
6907 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6908 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6909 if (TREE_CODE (expr
) == NOP_EXPR
6910 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
6911 expr
= TREE_OPERAND (expr
, 0);
6913 if (error_operand_p (expr
))
6914 return error_mark_node
;
6916 intype
= TREE_TYPE (expr
);
6918 /* [expr.reinterpret.cast]
6919 A pointer can be converted to any integral type large enough to
6920 hold it. ... A value of type std::nullptr_t can be converted to
6921 an integral type; the conversion has the same meaning and
6922 validity as a conversion of (void*)0 to the integral type. */
6923 if (CP_INTEGRAL_TYPE_P (type
)
6924 && (TYPE_PTR_P (intype
) || NULLPTR_TYPE_P (intype
)))
6926 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
6928 if (complain
& tf_error
)
6929 permerror (input_location
, "cast from %qT to %qT loses precision",
6932 return error_mark_node
;
6934 if (NULLPTR_TYPE_P (intype
))
6935 return build_int_cst (type
, 0);
6937 /* [expr.reinterpret.cast]
6938 A value of integral or enumeration type can be explicitly
6939 converted to a pointer. */
6940 else if (TYPE_PTR_P (type
) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype
))
6943 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6944 || TYPE_PTR_OR_PTRMEM_P (type
))
6945 && same_type_p (type
, intype
))
6947 return rvalue (expr
);
6948 else if ((TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
6949 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
6950 return fold_if_not_in_template (build_nop (type
, expr
));
6951 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
6952 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
6957 && check_for_casting_away_constness (intype
, type
,
6958 REINTERPRET_CAST_EXPR
,
6960 return error_mark_node
;
6961 /* Warn about possible alignment problems. */
6962 if (STRICT_ALIGNMENT
&& warn_cast_align
6963 && (complain
& tf_warning
)
6964 && !VOID_TYPE_P (type
)
6965 && TREE_CODE (TREE_TYPE (intype
)) != FUNCTION_TYPE
6966 && COMPLETE_TYPE_P (TREE_TYPE (type
))
6967 && COMPLETE_TYPE_P (TREE_TYPE (intype
))
6968 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (intype
)))
6969 warning (OPT_Wcast_align
, "cast from %qT to %qT "
6970 "increases required alignment of target type", intype
, type
);
6972 /* We need to strip nops here, because the front end likes to
6973 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6975 if (warn_strict_aliasing
<= 2)
6976 strict_aliasing_warning (intype
, type
, sexpr
);
6978 return fold_if_not_in_template (build_nop (type
, expr
));
6980 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
6981 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
6983 if (complain
& tf_warning
)
6984 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6985 object pointer type or vice versa is conditionally-supported." */
6986 warning (OPT_Wconditionally_supported
,
6987 "casting between pointer-to-function and pointer-to-object "
6988 "is conditionally-supported");
6989 return fold_if_not_in_template (build_nop (type
, expr
));
6991 else if (VECTOR_TYPE_P (type
))
6992 return fold_if_not_in_template (convert_to_vector (type
, expr
));
6993 else if (VECTOR_TYPE_P (intype
)
6994 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6995 return fold_if_not_in_template (convert_to_integer (type
, expr
));
7000 if (complain
& tf_error
)
7001 error ("invalid cast from type %qT to type %qT", intype
, type
);
7002 return error_mark_node
;
7005 return cp_convert (type
, expr
, complain
);
7009 build_reinterpret_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7013 if (type
== error_mark_node
|| expr
== error_mark_node
)
7014 return error_mark_node
;
7016 if (processing_template_decl
)
7018 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
7020 if (!TREE_SIDE_EFFECTS (t
)
7021 && type_dependent_expression_p (expr
))
7022 /* There might turn out to be side effects inside expr. */
7023 TREE_SIDE_EFFECTS (t
) = 1;
7024 return convert_from_reference (t
);
7027 r
= build_reinterpret_cast_1 (type
, expr
, /*c_cast_p=*/false,
7028 /*valid_p=*/NULL
, complain
);
7029 if (r
!= error_mark_node
)
7030 maybe_warn_about_useless_cast (type
, expr
, complain
);
7034 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7035 return an appropriate expression. Otherwise, return
7036 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7037 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7038 performing a C-style cast, its value upon return will indicate
7039 whether or not the conversion succeeded. */
7042 build_const_cast_1 (tree dst_type
, tree expr
, tsubst_flags_t complain
,
7046 tree reference_type
;
7048 /* Callers are responsible for handling error_mark_node as a
7049 destination type. */
7050 gcc_assert (dst_type
!= error_mark_node
);
7051 /* In a template, callers should be building syntactic
7052 representations of casts, not using this machinery. */
7053 gcc_assert (!processing_template_decl
);
7055 /* Assume the conversion is invalid. */
7059 if (!POINTER_TYPE_P (dst_type
) && !TYPE_PTRDATAMEM_P (dst_type
))
7061 if (complain
& tf_error
)
7062 error ("invalid use of const_cast with type %qT, "
7063 "which is not a pointer, "
7064 "reference, nor a pointer-to-data-member type", dst_type
);
7065 return error_mark_node
;
7068 if (TREE_CODE (TREE_TYPE (dst_type
)) == FUNCTION_TYPE
)
7070 if (complain
& tf_error
)
7071 error ("invalid use of const_cast with type %qT, which is a pointer "
7072 "or reference to a function type", dst_type
);
7073 return error_mark_node
;
7076 /* Save casted types in the function's used types hash table. */
7077 used_types_insert (dst_type
);
7079 src_type
= TREE_TYPE (expr
);
7080 /* Expressions do not really have reference types. */
7081 if (TREE_CODE (src_type
) == REFERENCE_TYPE
)
7082 src_type
= TREE_TYPE (src_type
);
7084 /* [expr.const.cast]
7086 For two object types T1 and T2, if a pointer to T1 can be explicitly
7087 converted to the type "pointer to T2" using a const_cast, then the
7088 following conversions can also be made:
7090 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7091 type T2 using the cast const_cast<T2&>;
7093 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7094 type T2 using the cast const_cast<T2&&>; and
7096 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7097 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7099 if (TREE_CODE (dst_type
) == REFERENCE_TYPE
)
7101 reference_type
= dst_type
;
7102 if (!TYPE_REF_IS_RVALUE (dst_type
)
7103 ? real_lvalue_p (expr
)
7104 : (CLASS_TYPE_P (TREE_TYPE (dst_type
))
7106 : lvalue_or_rvalue_with_address_p (expr
)))
7110 if (complain
& tf_error
)
7111 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7112 src_type
, dst_type
);
7113 return error_mark_node
;
7115 dst_type
= build_pointer_type (TREE_TYPE (dst_type
));
7116 src_type
= build_pointer_type (src_type
);
7120 reference_type
= NULL_TREE
;
7121 /* If the destination type is not a reference type, the
7122 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7123 conversions are performed. */
7124 src_type
= type_decays_to (src_type
);
7125 if (src_type
== error_mark_node
)
7126 return error_mark_node
;
7129 if (TYPE_PTR_P (src_type
) || TYPE_PTRDATAMEM_P (src_type
))
7131 if (comp_ptr_ttypes_const (dst_type
, src_type
))
7136 /* This cast is actually a C-style cast. Issue a warning if
7137 the user is making a potentially unsafe cast. */
7138 check_for_casting_away_constness (src_type
, dst_type
,
7139 CAST_EXPR
, complain
);
7143 expr
= cp_build_addr_expr (expr
, complain
);
7144 if (expr
== error_mark_node
)
7145 return error_mark_node
;
7146 expr
= build_nop (reference_type
, expr
);
7147 return convert_from_reference (expr
);
7151 expr
= decay_conversion (expr
, complain
);
7152 if (expr
== error_mark_node
)
7153 return error_mark_node
;
7155 /* build_c_cast puts on a NOP_EXPR to make the result not an
7156 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7157 non-lvalue context. */
7158 if (TREE_CODE (expr
) == NOP_EXPR
7159 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7160 expr
= TREE_OPERAND (expr
, 0);
7161 return build_nop (dst_type
, expr
);
7165 && !at_least_as_qualified_p (TREE_TYPE (dst_type
),
7166 TREE_TYPE (src_type
)))
7167 check_for_casting_away_constness (src_type
, dst_type
, CAST_EXPR
,
7171 if (complain
& tf_error
)
7172 error ("invalid const_cast from type %qT to type %qT",
7173 src_type
, dst_type
);
7174 return error_mark_node
;
7178 build_const_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7182 if (type
== error_mark_node
|| error_operand_p (expr
))
7183 return error_mark_node
;
7185 if (processing_template_decl
)
7187 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
7189 if (!TREE_SIDE_EFFECTS (t
)
7190 && type_dependent_expression_p (expr
))
7191 /* There might turn out to be side effects inside expr. */
7192 TREE_SIDE_EFFECTS (t
) = 1;
7193 return convert_from_reference (t
);
7196 r
= build_const_cast_1 (type
, expr
, complain
, /*valid_p=*/NULL
);
7197 if (r
!= error_mark_node
)
7198 maybe_warn_about_useless_cast (type
, expr
, complain
);
7202 /* Like cp_build_c_cast, but for the c-common bits. */
7205 build_c_cast (location_t
/*loc*/, tree type
, tree expr
)
7207 return cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7210 /* Build an expression representing an explicit C-style cast to type
7211 TYPE of expression EXPR. */
7214 cp_build_c_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7220 if (type
== error_mark_node
|| error_operand_p (expr
))
7221 return error_mark_node
;
7223 if (processing_template_decl
)
7225 tree t
= build_min (CAST_EXPR
, type
,
7226 tree_cons (NULL_TREE
, value
, NULL_TREE
));
7227 /* We don't know if it will or will not have side effects. */
7228 TREE_SIDE_EFFECTS (t
) = 1;
7229 return convert_from_reference (t
);
7232 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7233 'Class') should always be retained, because this information aids
7234 in method lookup. */
7235 if (objc_is_object_ptr (type
)
7236 && objc_is_object_ptr (TREE_TYPE (expr
)))
7237 return build_nop (type
, expr
);
7239 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7240 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7241 if (TREE_CODE (type
) != REFERENCE_TYPE
7242 && TREE_CODE (value
) == NOP_EXPR
7243 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
7244 value
= TREE_OPERAND (value
, 0);
7246 if (TREE_CODE (type
) == ARRAY_TYPE
)
7248 /* Allow casting from T1* to T2[] because Cfront allows it.
7249 NIHCL uses it. It is not valid ISO C++ however. */
7250 if (TYPE_PTR_P (TREE_TYPE (expr
)))
7252 if (complain
& tf_error
)
7253 permerror (input_location
, "ISO C++ forbids casting to an array type %qT", type
);
7255 return error_mark_node
;
7256 type
= build_pointer_type (TREE_TYPE (type
));
7260 if (complain
& tf_error
)
7261 error ("ISO C++ forbids casting to an array type %qT", type
);
7262 return error_mark_node
;
7266 if (TREE_CODE (type
) == FUNCTION_TYPE
7267 || TREE_CODE (type
) == METHOD_TYPE
)
7269 if (complain
& tf_error
)
7270 error ("invalid cast to function type %qT", type
);
7271 return error_mark_node
;
7274 if (TYPE_PTR_P (type
)
7275 && TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
7276 /* Casting to an integer of smaller size is an error detected elsewhere. */
7277 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (value
))
7278 /* Don't warn about converting any constant. */
7279 && !TREE_CONSTANT (value
))
7280 warning_at (input_location
, OPT_Wint_to_pointer_cast
,
7281 "cast to pointer from integer of different size");
7283 /* A C-style cast can be a const_cast. */
7284 result
= build_const_cast_1 (type
, value
, complain
& tf_warning
,
7288 if (result
!= error_mark_node
)
7289 maybe_warn_about_useless_cast (type
, value
, complain
);
7293 /* Or a static cast. */
7294 result
= build_static_cast_1 (type
, value
, /*c_cast_p=*/true,
7295 &valid_p
, complain
);
7296 /* Or a reinterpret_cast. */
7298 result
= build_reinterpret_cast_1 (type
, value
, /*c_cast_p=*/true,
7299 &valid_p
, complain
);
7300 /* The static_cast or reinterpret_cast may be followed by a
7303 /* A valid cast may result in errors if, for example, a
7304 conversion to an ambiguous base class is required. */
7305 && !error_operand_p (result
))
7309 maybe_warn_about_useless_cast (type
, value
, complain
);
7311 /* Non-class rvalues always have cv-unqualified type. */
7312 if (!CLASS_TYPE_P (type
))
7313 type
= TYPE_MAIN_VARIANT (type
);
7314 result_type
= TREE_TYPE (result
);
7315 if (!CLASS_TYPE_P (result_type
) && TREE_CODE (type
) != REFERENCE_TYPE
)
7316 result_type
= TYPE_MAIN_VARIANT (result_type
);
7317 /* If the type of RESULT does not match TYPE, perform a
7318 const_cast to make it match. If the static_cast or
7319 reinterpret_cast succeeded, we will differ by at most
7320 cv-qualification, so the follow-on const_cast is guaranteed
7322 if (!same_type_p (non_reference (type
), non_reference (result_type
)))
7324 result
= build_const_cast_1 (type
, result
, false, &valid_p
);
7325 gcc_assert (valid_p
);
7330 return error_mark_node
;
7333 /* For use from the C common bits. */
7335 build_modify_expr (location_t
/*location*/,
7336 tree lhs
, tree
/*lhs_origtype*/,
7337 enum tree_code modifycode
,
7338 location_t
/*rhs_location*/, tree rhs
,
7339 tree
/*rhs_origtype*/)
7341 return cp_build_modify_expr (lhs
, modifycode
, rhs
, tf_warning_or_error
);
7344 /* Build an assignment expression of lvalue LHS from value RHS.
7345 MODIFYCODE is the code for a binary operator that we use
7346 to combine the old value of LHS with RHS to get the new value.
7347 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7349 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7352 cp_build_modify_expr (tree lhs
, enum tree_code modifycode
, tree rhs
,
7353 tsubst_flags_t complain
)
7357 tree lhstype
= TREE_TYPE (lhs
);
7358 tree olhstype
= lhstype
;
7359 bool plain_assign
= (modifycode
== NOP_EXPR
);
7361 /* Avoid duplicate error messages from operands that had errors. */
7362 if (error_operand_p (lhs
) || error_operand_p (rhs
))
7363 return error_mark_node
;
7365 /* Handle control structure constructs used as "lvalues". */
7366 switch (TREE_CODE (lhs
))
7368 /* Handle --foo = 5; as these are valid constructs in C++. */
7369 case PREDECREMENT_EXPR
:
7370 case PREINCREMENT_EXPR
:
7371 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
7372 lhs
= build2 (TREE_CODE (lhs
), TREE_TYPE (lhs
),
7373 stabilize_reference (TREE_OPERAND (lhs
, 0)),
7374 TREE_OPERAND (lhs
, 1));
7375 newrhs
= cp_build_modify_expr (TREE_OPERAND (lhs
, 0),
7376 modifycode
, rhs
, complain
);
7377 if (newrhs
== error_mark_node
)
7378 return error_mark_node
;
7379 return build2 (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
7381 /* Handle (a, b) used as an "lvalue". */
7383 newrhs
= cp_build_modify_expr (TREE_OPERAND (lhs
, 1),
7384 modifycode
, rhs
, complain
);
7385 if (newrhs
== error_mark_node
)
7386 return error_mark_node
;
7387 return build2 (COMPOUND_EXPR
, lhstype
,
7388 TREE_OPERAND (lhs
, 0), newrhs
);
7391 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
7392 lhs
= build2 (TREE_CODE (lhs
), TREE_TYPE (lhs
),
7393 stabilize_reference (TREE_OPERAND (lhs
, 0)),
7394 TREE_OPERAND (lhs
, 1));
7395 newrhs
= cp_build_modify_expr (TREE_OPERAND (lhs
, 0), modifycode
, rhs
,
7397 if (newrhs
== error_mark_node
)
7398 return error_mark_node
;
7399 return build2 (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
7403 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7404 when neither operand has side-effects. */
7405 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7406 return error_mark_node
;
7408 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0))
7409 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 1)));
7411 lhs
= build3 (COND_EXPR
, TREE_TYPE (lhs
),
7412 build2 (TREE_CODE (lhs
) == MIN_EXPR
? LE_EXPR
: GE_EXPR
,
7414 TREE_OPERAND (lhs
, 0),
7415 TREE_OPERAND (lhs
, 1)),
7416 TREE_OPERAND (lhs
, 0),
7417 TREE_OPERAND (lhs
, 1));
7420 /* Handle (a ? b : c) used as an "lvalue". */
7423 /* Produce (a ? (b = rhs) : (c = rhs))
7424 except that the RHS goes through a save-expr
7425 so the code to compute it is only emitted once. */
7427 tree preeval
= NULL_TREE
;
7429 if (VOID_TYPE_P (TREE_TYPE (rhs
)))
7431 if (complain
& tf_error
)
7432 error ("void value not ignored as it ought to be");
7433 return error_mark_node
;
7436 rhs
= stabilize_expr (rhs
, &preeval
);
7438 /* Check this here to avoid odd errors when trying to convert
7439 a throw to the type of the COND_EXPR. */
7440 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7441 return error_mark_node
;
7443 cond
= build_conditional_expr
7444 (input_location
, TREE_OPERAND (lhs
, 0),
7445 cp_build_modify_expr (TREE_OPERAND (lhs
, 1),
7446 modifycode
, rhs
, complain
),
7447 cp_build_modify_expr (TREE_OPERAND (lhs
, 2),
7448 modifycode
, rhs
, complain
),
7451 if (cond
== error_mark_node
)
7453 /* Make sure the code to compute the rhs comes out
7454 before the split. */
7456 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (lhs
), preeval
, cond
);
7464 if (modifycode
== INIT_EXPR
)
7466 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
7467 /* Do the default thing. */;
7468 else if (TREE_CODE (rhs
) == CONSTRUCTOR
)
7470 /* Compound literal. */
7471 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
7472 /* Call convert to generate an error; see PR 11063. */
7473 rhs
= convert (lhstype
, rhs
);
7474 result
= build2 (INIT_EXPR
, lhstype
, lhs
, rhs
);
7475 TREE_SIDE_EFFECTS (result
) = 1;
7478 else if (! MAYBE_CLASS_TYPE_P (lhstype
))
7479 /* Do the default thing. */;
7482 vec
<tree
, va_gc
> *rhs_vec
= make_tree_vector_single (rhs
);
7483 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
7484 &rhs_vec
, lhstype
, LOOKUP_NORMAL
,
7486 release_tree_vector (rhs_vec
);
7487 if (result
== NULL_TREE
)
7488 return error_mark_node
;
7494 lhs
= require_complete_type_sfinae (lhs
, complain
);
7495 if (lhs
== error_mark_node
)
7496 return error_mark_node
;
7498 if (modifycode
== NOP_EXPR
)
7500 if (c_dialect_objc ())
7502 result
= objc_maybe_build_modify_expr (lhs
, rhs
);
7507 /* `operator=' is not an inheritable operator. */
7508 if (! MAYBE_CLASS_TYPE_P (lhstype
))
7509 /* Do the default thing. */;
7512 result
= build_new_op (input_location
, MODIFY_EXPR
,
7513 LOOKUP_NORMAL
, lhs
, rhs
,
7514 make_node (NOP_EXPR
), /*overload=*/NULL
,
7516 if (result
== NULL_TREE
)
7517 return error_mark_node
;
7524 tree init
= NULL_TREE
;
7526 /* A binary op has been requested. Combine the old LHS
7527 value with the RHS producing the value we should actually
7528 store into the LHS. */
7529 gcc_assert (!((TREE_CODE (lhstype
) == REFERENCE_TYPE
7530 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype
)))
7531 || MAYBE_CLASS_TYPE_P (lhstype
)));
7533 /* Preevaluate the RHS to make sure its evaluation is complete
7534 before the lvalue-to-rvalue conversion of the LHS:
7536 [expr.ass] With respect to an indeterminately-sequenced
7537 function call, the operation of a compound assignment is a
7538 single evaluation. [ Note: Therefore, a function call shall
7539 not intervene between the lvalue-to-rvalue conversion and the
7540 side effect associated with any single compound assignment
7541 operator. -- end note ] */
7542 lhs
= stabilize_reference (lhs
);
7544 rhs
= stabilize_expr (rhs
, &init
);
7545 newrhs
= cp_build_binary_op (input_location
,
7546 modifycode
, lhs
, rhs
,
7548 if (newrhs
== error_mark_node
)
7550 if (complain
& tf_error
)
7551 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode
,
7552 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
7553 return error_mark_node
;
7557 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), init
, newrhs
);
7559 /* Now it looks like a plain assignment. */
7560 modifycode
= NOP_EXPR
;
7561 if (c_dialect_objc ())
7563 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
7568 gcc_assert (TREE_CODE (lhstype
) != REFERENCE_TYPE
);
7569 gcc_assert (TREE_CODE (TREE_TYPE (newrhs
)) != REFERENCE_TYPE
);
7572 /* The left-hand side must be an lvalue. */
7573 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7574 return error_mark_node
;
7576 /* Warn about modifying something that is `const'. Don't warn if
7577 this is initialization. */
7578 if (modifycode
!= INIT_EXPR
7579 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
7580 /* Functions are not modifiable, even though they are
7582 || TREE_CODE (TREE_TYPE (lhs
)) == FUNCTION_TYPE
7583 || TREE_CODE (TREE_TYPE (lhs
)) == METHOD_TYPE
7584 /* If it's an aggregate and any field is const, then it is
7585 effectively const. */
7586 || (CLASS_TYPE_P (lhstype
)
7587 && C_TYPE_FIELDS_READONLY (lhstype
))))
7589 if (complain
& tf_error
)
7590 cxx_readonly_error (lhs
, lv_assign
);
7592 return error_mark_node
;
7595 /* If storing into a structure or union member, it may have been given a
7596 lowered bitfield type. We need to convert to the declared type first,
7597 so retrieve it now. */
7599 olhstype
= unlowered_expr_type (lhs
);
7601 /* Convert new value to destination type. */
7603 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
7607 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs
))
7609 if (modifycode
!= INIT_EXPR
)
7611 if (complain
& tf_error
)
7612 error ("assigning to an array from an initializer list");
7613 return error_mark_node
;
7615 if (check_array_initializer (lhs
, lhstype
, newrhs
))
7616 return error_mark_node
;
7617 newrhs
= digest_init (lhstype
, newrhs
, complain
);
7618 if (newrhs
== error_mark_node
)
7619 return error_mark_node
;
7622 /* C++11 8.5/17: "If the destination type is an array of characters,
7623 an array of char16_t, an array of char32_t, or an array of wchar_t,
7624 and the initializer is a string literal...". */
7625 else if (TREE_CODE (newrhs
) == STRING_CST
7626 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype
)))
7627 && modifycode
== INIT_EXPR
)
7629 newrhs
= digest_init (lhstype
, newrhs
, complain
);
7630 if (newrhs
== error_mark_node
)
7631 return error_mark_node
;
7634 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
7635 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))))
7637 if (complain
& tf_error
)
7638 error ("incompatible types in assignment of %qT to %qT",
7639 TREE_TYPE (rhs
), lhstype
);
7640 return error_mark_node
;
7643 /* Allow array assignment in compiler-generated code. */
7644 else if (!current_function_decl
7645 || !DECL_DEFAULTED_FN (current_function_decl
))
7647 /* This routine is used for both initialization and assignment.
7648 Make sure the diagnostic message differentiates the context. */
7649 if (complain
& tf_error
)
7651 if (modifycode
== INIT_EXPR
)
7652 error ("array used as initializer");
7654 error ("invalid array assignment");
7656 return error_mark_node
;
7659 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
7660 ? 1 + (modifycode
!= INIT_EXPR
): 0;
7661 return build_vec_init (lhs
, NULL_TREE
, newrhs
,
7662 /*explicit_value_init_p=*/false,
7663 from_array
, complain
);
7666 if (modifycode
== INIT_EXPR
)
7667 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7668 LOOKUP_ONLYCONVERTING. */
7669 newrhs
= convert_for_initialization (lhs
, olhstype
, newrhs
, LOOKUP_NORMAL
,
7670 ICR_INIT
, NULL_TREE
, 0,
7673 newrhs
= convert_for_assignment (olhstype
, newrhs
, ICR_ASSIGN
,
7674 NULL_TREE
, 0, complain
, LOOKUP_IMPLICIT
);
7676 if (!same_type_p (lhstype
, olhstype
))
7677 newrhs
= cp_convert_and_check (lhstype
, newrhs
, complain
);
7679 if (modifycode
!= INIT_EXPR
)
7681 if (TREE_CODE (newrhs
) == CALL_EXPR
7682 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
7683 newrhs
= build_cplus_new (lhstype
, newrhs
, complain
);
7685 /* Can't initialize directly from a TARGET_EXPR, since that would
7686 cause the lhs to be constructed twice, and possibly result in
7687 accidental self-initialization. So we force the TARGET_EXPR to be
7688 expanded without a target. */
7689 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
7690 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
7691 TREE_OPERAND (newrhs
, 0));
7694 if (newrhs
== error_mark_node
)
7695 return error_mark_node
;
7697 if (c_dialect_objc () && flag_objc_gc
)
7699 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
7705 result
= build2 (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
7706 lhstype
, lhs
, newrhs
);
7708 TREE_SIDE_EFFECTS (result
) = 1;
7710 TREE_NO_WARNING (result
) = 1;
7716 build_x_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
7717 tree rhs
, tsubst_flags_t complain
)
7719 if (processing_template_decl
)
7720 return build_min_nt_loc (loc
, MODOP_EXPR
, lhs
,
7721 build_min_nt_loc (loc
, modifycode
, NULL_TREE
,
7724 if (modifycode
!= NOP_EXPR
)
7726 tree rval
= build_new_op (loc
, MODIFY_EXPR
, LOOKUP_NORMAL
, lhs
, rhs
,
7727 make_node (modifycode
), /*overload=*/NULL
,
7731 TREE_NO_WARNING (rval
) = 1;
7735 return cp_build_modify_expr (lhs
, modifycode
, rhs
, complain
);
7738 /* Helper function for get_delta_difference which assumes FROM is a base
7739 class of TO. Returns a delta for the conversion of pointer-to-member
7740 of FROM to pointer-to-member of TO. If the conversion is invalid and
7741 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7742 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7743 If C_CAST_P is true, this conversion is taking place as part of a
7747 get_delta_difference_1 (tree from
, tree to
, bool c_cast_p
,
7748 tsubst_flags_t complain
)
7753 binfo
= lookup_base (to
, from
, c_cast_p
? ba_unique
: ba_check
,
7756 if (binfo
== error_mark_node
)
7758 if (!(complain
& tf_error
))
7759 return error_mark_node
;
7761 error (" in pointer to member function conversion");
7762 return size_zero_node
;
7766 if (kind
!= bk_via_virtual
)
7767 return BINFO_OFFSET (binfo
);
7769 /* FROM is a virtual base class of TO. Issue an error or warning
7770 depending on whether or not this is a reinterpret cast. */
7772 if (!(complain
& tf_error
))
7773 return error_mark_node
;
7775 error ("pointer to member conversion via virtual base %qT",
7776 BINFO_TYPE (binfo_from_vbase (binfo
)));
7778 return size_zero_node
;
7785 /* Get difference in deltas for different pointer to member function
7786 types. If the conversion is invalid and tf_error is not set in
7787 COMPLAIN, returns error_mark_node, otherwise returns an integer
7788 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7789 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7790 conversions as well. If C_CAST_P is true this conversion is taking
7791 place as part of a C-style cast.
7793 Note that the naming of FROM and TO is kind of backwards; the return
7794 value is what we add to a TO in order to get a FROM. They are named
7795 this way because we call this function to find out how to convert from
7796 a pointer to member of FROM to a pointer to member of TO. */
7799 get_delta_difference (tree from
, tree to
,
7800 bool allow_inverse_p
,
7801 bool c_cast_p
, tsubst_flags_t complain
)
7805 if (same_type_ignoring_top_level_qualifiers_p (from
, to
))
7806 /* Pointer to member of incomplete class is permitted*/
7807 result
= size_zero_node
;
7809 result
= get_delta_difference_1 (from
, to
, c_cast_p
, complain
);
7811 if (result
== error_mark_node
)
7812 return error_mark_node
;
7816 if (!allow_inverse_p
)
7818 if (!(complain
& tf_error
))
7819 return error_mark_node
;
7821 error_not_base_type (from
, to
);
7822 error (" in pointer to member conversion");
7823 result
= size_zero_node
;
7827 result
= get_delta_difference_1 (to
, from
, c_cast_p
, complain
);
7829 if (result
== error_mark_node
)
7830 return error_mark_node
;
7833 result
= size_diffop_loc (input_location
,
7834 size_zero_node
, result
);
7837 if (!(complain
& tf_error
))
7838 return error_mark_node
;
7840 error_not_base_type (from
, to
);
7841 error (" in pointer to member conversion");
7842 result
= size_zero_node
;
7847 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node
,
7851 /* Return a constructor for the pointer-to-member-function TYPE using
7852 the other components as specified. */
7855 build_ptrmemfunc1 (tree type
, tree delta
, tree pfn
)
7860 vec
<constructor_elt
, va_gc
> *v
;
7862 /* Pull the FIELD_DECLs out of the type. */
7863 pfn_field
= TYPE_FIELDS (type
);
7864 delta_field
= DECL_CHAIN (pfn_field
);
7866 /* Make sure DELTA has the type we want. */
7867 delta
= convert_and_check (input_location
, delta_type_node
, delta
);
7869 /* Convert to the correct target type if necessary. */
7870 pfn
= fold_convert (TREE_TYPE (pfn_field
), pfn
);
7872 /* Finish creating the initializer. */
7874 CONSTRUCTOR_APPEND_ELT(v
, pfn_field
, pfn
);
7875 CONSTRUCTOR_APPEND_ELT(v
, delta_field
, delta
);
7876 u
= build_constructor (type
, v
);
7877 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) & TREE_CONSTANT (delta
);
7878 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
7879 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
7881 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
7886 /* Build a constructor for a pointer to member function. It can be
7887 used to initialize global variables, local variable, or used
7888 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7891 If FORCE is nonzero, then force this conversion, even if
7892 we would rather not do it. Usually set when using an explicit
7893 cast. A C-style cast is being processed iff C_CAST_P is true.
7895 Return error_mark_node, if something goes wrong. */
7898 build_ptrmemfunc (tree type
, tree pfn
, int force
, bool c_cast_p
,
7899 tsubst_flags_t complain
)
7905 if (error_operand_p (pfn
))
7906 return error_mark_node
;
7908 pfn_type
= TREE_TYPE (pfn
);
7909 to_type
= build_ptrmemfunc_type (type
);
7911 /* Handle multiple conversions of pointer to member functions. */
7912 if (TYPE_PTRMEMFUNC_P (pfn_type
))
7914 tree delta
= NULL_TREE
;
7915 tree npfn
= NULL_TREE
;
7919 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
,
7920 LOOKUP_NORMAL
, complain
))
7922 if (complain
& tf_error
)
7923 error ("invalid conversion to type %qT from type %qT",
7926 return error_mark_node
;
7929 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
7930 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
7932 c_cast_p
, complain
);
7933 if (n
== error_mark_node
)
7934 return error_mark_node
;
7936 /* We don't have to do any conversion to convert a
7937 pointer-to-member to its own type. But, we don't want to
7938 just return a PTRMEM_CST if there's an explicit cast; that
7939 cast should make the expression an invalid template argument. */
7940 if (TREE_CODE (pfn
) != PTRMEM_CST
)
7942 if (same_type_p (to_type
, pfn_type
))
7944 else if (integer_zerop (n
))
7945 return build_reinterpret_cast (to_type
, pfn
,
7949 if (TREE_SIDE_EFFECTS (pfn
))
7950 pfn
= save_expr (pfn
);
7952 /* Obtain the function pointer and the current DELTA. */
7953 if (TREE_CODE (pfn
) == PTRMEM_CST
)
7954 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
7957 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
7958 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
7961 /* Just adjust the DELTA field. */
7962 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7963 (TREE_TYPE (delta
), ptrdiff_type_node
));
7964 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
7965 n
= cp_build_binary_op (input_location
,
7966 LSHIFT_EXPR
, n
, integer_one_node
,
7968 delta
= cp_build_binary_op (input_location
,
7969 PLUS_EXPR
, delta
, n
, complain
);
7970 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
7973 /* Handle null pointer to member function conversions. */
7974 if (null_ptr_cst_p (pfn
))
7976 pfn
= cp_build_c_cast (type
, pfn
, complain
);
7977 return build_ptrmemfunc1 (to_type
,
7982 if (type_unknown_p (pfn
))
7983 return instantiate_type (type
, pfn
, complain
);
7985 fn
= TREE_OPERAND (pfn
, 0);
7986 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
7987 /* In a template, we will have preserved the
7989 || (processing_template_decl
&& TREE_CODE (fn
) == OFFSET_REF
));
7990 return make_ptrmem_cst (to_type
, fn
);
7993 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7996 ??? There is no consistency as to the types returned for the above
7997 values. Some code acts as if it were a sizetype and some as if it were
7998 integer_type_node. */
8001 expand_ptrmemfunc_cst (tree cst
, tree
*delta
, tree
*pfn
)
8003 tree type
= TREE_TYPE (cst
);
8004 tree fn
= PTRMEM_CST_MEMBER (cst
);
8005 tree ptr_class
, fn_class
;
8007 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
8009 /* The class that the function belongs to. */
8010 fn_class
= DECL_CONTEXT (fn
);
8012 /* The class that we're creating a pointer to member of. */
8013 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
8015 /* First, calculate the adjustment to the function's class. */
8016 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0,
8017 /*c_cast_p=*/0, tf_warning_or_error
);
8019 if (!DECL_VIRTUAL_P (fn
))
8020 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
),
8021 build_addr_func (fn
, tf_warning_or_error
));
8024 /* If we're dealing with a virtual function, we have to adjust 'this'
8025 again, to point to the base which provides the vtable entry for
8026 fn; the call will do the opposite adjustment. */
8027 tree orig_class
= DECL_CONTEXT (fn
);
8028 tree binfo
= binfo_or_else (orig_class
, fn_class
);
8029 *delta
= build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
8030 *delta
, BINFO_OFFSET (binfo
));
8031 *delta
= fold_if_not_in_template (*delta
);
8033 /* We set PFN to the vtable offset at which the function can be
8034 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8035 case delta is shifted left, and then incremented). */
8036 *pfn
= DECL_VINDEX (fn
);
8037 *pfn
= build2 (MULT_EXPR
, integer_type_node
, *pfn
,
8038 TYPE_SIZE_UNIT (vtable_entry_type
));
8039 *pfn
= fold_if_not_in_template (*pfn
);
8041 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
8043 case ptrmemfunc_vbit_in_pfn
:
8044 *pfn
= build2 (PLUS_EXPR
, integer_type_node
, *pfn
,
8046 *pfn
= fold_if_not_in_template (*pfn
);
8049 case ptrmemfunc_vbit_in_delta
:
8050 *delta
= build2 (LSHIFT_EXPR
, TREE_TYPE (*delta
),
8051 *delta
, integer_one_node
);
8052 *delta
= fold_if_not_in_template (*delta
);
8053 *delta
= build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
8054 *delta
, integer_one_node
);
8055 *delta
= fold_if_not_in_template (*delta
);
8062 *pfn
= build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type
), *pfn
);
8063 *pfn
= fold_if_not_in_template (*pfn
);
8067 /* Return an expression for PFN from the pointer-to-member function
8071 pfn_from_ptrmemfunc (tree t
)
8073 if (TREE_CODE (t
) == PTRMEM_CST
)
8078 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
8083 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
8086 /* Return an expression for DELTA from the pointer-to-member function
8090 delta_from_ptrmemfunc (tree t
)
8092 if (TREE_CODE (t
) == PTRMEM_CST
)
8097 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
8102 return build_ptrmemfunc_access_expr (t
, delta_identifier
);
8105 /* Convert value RHS to type TYPE as preparation for an assignment to
8106 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8107 implicit conversion is. If FNDECL is non-NULL, we are doing the
8108 conversion in order to pass the PARMNUMth argument of FNDECL.
8109 If FNDECL is NULL, we are doing the conversion in function pointer
8110 argument passing, conversion in initialization, etc. */
8113 convert_for_assignment (tree type
, tree rhs
,
8114 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
8115 tsubst_flags_t complain
, int flags
)
8118 enum tree_code coder
;
8120 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8121 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
8122 rhs
= TREE_OPERAND (rhs
, 0);
8124 rhstype
= TREE_TYPE (rhs
);
8125 coder
= TREE_CODE (rhstype
);
8127 if (VECTOR_TYPE_P (type
) && coder
== VECTOR_TYPE
8128 && vector_types_convertible_p (type
, rhstype
, true))
8130 rhs
= mark_rvalue_use (rhs
);
8131 return convert (type
, rhs
);
8134 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
8135 return error_mark_node
;
8136 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
8137 return error_mark_node
;
8139 /* The RHS of an assignment cannot have void type. */
8140 if (coder
== VOID_TYPE
)
8142 if (complain
& tf_error
)
8143 error ("void value not ignored as it ought to be");
8144 return error_mark_node
;
8147 if (c_dialect_objc ())
8151 tree rname
= fndecl
;
8162 selector
= objc_message_selector ();
8164 if (selector
&& parmno
> 1)
8171 if (objc_compare_types (type
, rhstype
, parmno
, rname
))
8173 rhs
= mark_rvalue_use (rhs
);
8174 return convert (type
, rhs
);
8180 The expression is implicitly converted (clause _conv_) to the
8181 cv-unqualified type of the left operand.
8183 We allow bad conversions here because by the time we get to this point
8184 we are committed to doing the conversion. If we end up doing a bad
8185 conversion, convert_like will complain. */
8186 if (!can_convert_arg_bad (type
, rhstype
, rhs
, flags
, complain
))
8188 /* When -Wno-pmf-conversions is use, we just silently allow
8189 conversions from pointers-to-members to plain pointers. If
8190 the conversion doesn't work, cp_convert will complain. */
8192 && TYPE_PTR_P (type
)
8193 && TYPE_PTRMEMFUNC_P (rhstype
))
8194 rhs
= cp_convert (strip_top_quals (type
), rhs
, complain
);
8197 if (complain
& tf_error
)
8199 /* If the right-hand side has unknown type, then it is an
8200 overloaded function. Call instantiate_type to get error
8202 if (rhstype
== unknown_type_node
)
8203 instantiate_type (type
, rhs
, tf_warning_or_error
);
8205 error ("cannot convert %qT to %qT for argument %qP to %qD",
8206 rhstype
, type
, parmnum
, fndecl
);
8210 case ICR_DEFAULT_ARGUMENT
:
8211 error ("cannot convert %qT to %qT in default argument",
8215 error ("cannot convert %qT to %qT in argument passing",
8218 case ICR_CONVERTING
:
8219 error ("cannot convert %qT to %qT",
8223 error ("cannot convert %qT to %qT in initialization",
8227 error ("cannot convert %qT to %qT in return",
8231 error ("cannot convert %qT to %qT in assignment",
8237 if (TYPE_PTR_P (rhstype
)
8238 && TYPE_PTR_P (type
)
8239 && CLASS_TYPE_P (TREE_TYPE (rhstype
))
8240 && CLASS_TYPE_P (TREE_TYPE (type
))
8241 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype
)))
8242 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8243 (TREE_TYPE (rhstype
))),
8244 "class type %qT is incomplete", TREE_TYPE (rhstype
));
8246 return error_mark_node
;
8249 if (warn_suggest_attribute_format
)
8251 const enum tree_code codel
= TREE_CODE (type
);
8252 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
8254 && check_missing_format_attribute (type
, rhstype
)
8255 && (complain
& tf_warning
))
8259 case ICR_DEFAULT_ARGUMENT
:
8261 warning (OPT_Wsuggest_attribute_format
,
8262 "parameter %qP of %qD might be a candidate "
8263 "for a format attribute", parmnum
, fndecl
);
8265 warning (OPT_Wsuggest_attribute_format
,
8266 "parameter might be a candidate "
8267 "for a format attribute");
8269 case ICR_CONVERTING
:
8270 warning (OPT_Wsuggest_attribute_format
,
8271 "target of conversion might be a candidate "
8272 "for a format attribute");
8275 warning (OPT_Wsuggest_attribute_format
,
8276 "target of initialization might be a candidate "
8277 "for a format attribute");
8280 warning (OPT_Wsuggest_attribute_format
,
8281 "return type might be a candidate "
8282 "for a format attribute");
8285 warning (OPT_Wsuggest_attribute_format
,
8286 "left-hand side of assignment might be a candidate "
8287 "for a format attribute");
8294 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8296 if (warn_parentheses
8297 && TREE_CODE (type
) == BOOLEAN_TYPE
8298 && TREE_CODE (rhs
) == MODIFY_EXPR
8299 && !TREE_NO_WARNING (rhs
)
8300 && TREE_CODE (TREE_TYPE (rhs
)) != BOOLEAN_TYPE
8301 && (complain
& tf_warning
))
8303 location_t loc
= EXPR_LOC_OR_LOC (rhs
, input_location
);
8305 warning_at (loc
, OPT_Wparentheses
,
8306 "suggest parentheses around assignment used as truth value");
8307 TREE_NO_WARNING (rhs
) = 1;
8310 return perform_implicit_conversion_flags (strip_top_quals (type
), rhs
,
8314 /* Convert RHS to be of type TYPE.
8315 If EXP is nonzero, it is the target of the initialization.
8316 ERRTYPE indicates what kind of error the implicit conversion is.
8318 Two major differences between the behavior of
8319 `convert_for_assignment' and `convert_for_initialization'
8320 are that references are bashed in the former, while
8321 copied in the latter, and aggregates are assigned in
8322 the former (operator=) while initialized in the
8325 If using constructor make sure no conversion operator exists, if one does
8326 exist, an ambiguity exists. */
8329 convert_for_initialization (tree exp
, tree type
, tree rhs
, int flags
,
8330 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
8331 tsubst_flags_t complain
)
8333 enum tree_code codel
= TREE_CODE (type
);
8335 enum tree_code coder
;
8337 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8338 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8339 if (TREE_CODE (rhs
) == NOP_EXPR
8340 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
8341 && codel
!= REFERENCE_TYPE
)
8342 rhs
= TREE_OPERAND (rhs
, 0);
8344 if (type
== error_mark_node
8345 || rhs
== error_mark_node
8346 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
8347 return error_mark_node
;
8349 if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
8350 && TREE_CODE (type
) != ARRAY_TYPE
8351 && (TREE_CODE (type
) != REFERENCE_TYPE
8352 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
8353 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
8354 && !TYPE_REFFN_P (type
))
8355 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
8356 rhs
= decay_conversion (rhs
, complain
);
8358 rhstype
= TREE_TYPE (rhs
);
8359 coder
= TREE_CODE (rhstype
);
8361 if (coder
== ERROR_MARK
)
8362 return error_mark_node
;
8364 /* We accept references to incomplete types, so we can
8365 return here before checking if RHS is of complete type. */
8367 if (codel
== REFERENCE_TYPE
)
8369 /* This should eventually happen in convert_arguments. */
8370 int savew
= 0, savee
= 0;
8373 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
8374 rhs
= initialize_reference (type
, rhs
, flags
, complain
);
8377 && (warningcount
+ werrorcount
> savew
|| errorcount
> savee
))
8378 inform (DECL_SOURCE_LOCATION (fndecl
),
8379 "in passing argument %P of %qD", parmnum
, fndecl
);
8385 exp
= require_complete_type_sfinae (exp
, complain
);
8386 if (exp
== error_mark_node
)
8387 return error_mark_node
;
8389 rhstype
= non_reference (rhstype
);
8391 type
= complete_type (type
);
8393 if (DIRECT_INIT_EXPR_P (type
, rhs
))
8394 /* Don't try to do copy-initialization if we already have
8395 direct-initialization. */
8398 if (MAYBE_CLASS_TYPE_P (type
))
8399 return perform_implicit_conversion_flags (type
, rhs
, complain
, flags
);
8401 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
,
8405 /* If RETVAL is the address of, or a reference to, a local variable or
8406 temporary give an appropriate warning and return true. */
8409 maybe_warn_about_returning_address_of_local (tree retval
)
8411 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
8412 tree whats_returned
= retval
;
8416 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
8417 whats_returned
= TREE_OPERAND (whats_returned
, 1);
8418 else if (CONVERT_EXPR_P (whats_returned
)
8419 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
)
8420 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8425 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
8427 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8429 while (TREE_CODE (whats_returned
) == COMPONENT_REF
8430 || TREE_CODE (whats_returned
) == ARRAY_REF
)
8431 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8433 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
8435 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
8436 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
8438 warning (OPT_Wreturn_local_addr
, "returning reference to temporary");
8441 if (VAR_P (whats_returned
)
8442 && DECL_NAME (whats_returned
)
8443 && TEMP_NAME_P (DECL_NAME (whats_returned
)))
8445 warning (OPT_Wreturn_local_addr
, "reference to non-lvalue returned");
8450 if (DECL_P (whats_returned
)
8451 && DECL_NAME (whats_returned
)
8452 && DECL_FUNCTION_SCOPE_P (whats_returned
)
8453 && !is_capture_proxy (whats_returned
)
8454 && !(TREE_STATIC (whats_returned
)
8455 || TREE_PUBLIC (whats_returned
)))
8457 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
8458 warning_at (DECL_SOURCE_LOCATION (whats_returned
),
8459 OPT_Wreturn_local_addr
,
8460 "reference to local variable %qD returned",
8462 else if (TREE_CODE (whats_returned
) == LABEL_DECL
)
8463 warning_at (DECL_SOURCE_LOCATION (whats_returned
),
8464 OPT_Wreturn_local_addr
, "address of label %qD returned",
8467 warning_at (DECL_SOURCE_LOCATION (whats_returned
),
8468 OPT_Wreturn_local_addr
, "address of local variable %qD "
8469 "returned", whats_returned
);
8476 /* Check that returning RETVAL from the current function is valid.
8477 Return an expression explicitly showing all conversions required to
8478 change RETVAL into the function return type, and to assign it to
8479 the DECL_RESULT for the function. Set *NO_WARNING to true if
8480 code reaches end of non-void function warning shouldn't be issued
8481 on this RETURN_EXPR. */
8484 check_return_expr (tree retval
, bool *no_warning
)
8487 /* The type actually returned by the function. */
8489 /* The type the function is declared to return, or void if
8490 the declared type is incomplete. */
8492 int fn_returns_value_p
;
8493 bool named_return_value_okay_p
;
8495 *no_warning
= false;
8497 if (flag_cilkplus
&& retval
&& contains_cilk_spawn_stmt (retval
))
8499 error_at (EXPR_LOCATION (retval
), "use of %<_Cilk_spawn%> in a return "
8500 "statement is not allowed");
8504 /* A `volatile' function is one that isn't supposed to return, ever.
8505 (This is a G++ extension, used to get better code for functions
8506 that call the `volatile' function.) */
8507 if (TREE_THIS_VOLATILE (current_function_decl
))
8508 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8510 /* Check for various simple errors. */
8511 if (DECL_DESTRUCTOR_P (current_function_decl
))
8514 error ("returning a value from a destructor");
8517 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
8519 if (in_function_try_handler
)
8520 /* If a return statement appears in a handler of the
8521 function-try-block of a constructor, the program is ill-formed. */
8522 error ("cannot return from a handler of a function-try-block of a constructor");
8524 /* You can't return a value from a constructor. */
8525 error ("returning a value from a constructor");
8529 const tree saved_retval
= retval
;
8531 if (processing_template_decl
)
8533 current_function_returns_value
= 1;
8535 if (check_for_bare_parameter_packs (retval
))
8536 return error_mark_node
;
8538 if (WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl
)))
8539 || (retval
!= NULL_TREE
8540 && type_dependent_expression_p (retval
)))
8544 functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
8546 /* Deduce auto return type from a return statement. */
8547 if (current_function_auto_return_pattern
)
8552 if (!retval
&& !is_auto (current_function_auto_return_pattern
))
8554 /* Give a helpful error message. */
8555 error ("return-statement with no value, in function returning %qT",
8556 current_function_auto_return_pattern
);
8557 inform (input_location
, "only plain %<auto%> return type can be "
8558 "deduced to %<void%>");
8559 type
= error_mark_node
;
8561 else if (retval
&& BRACE_ENCLOSED_INITIALIZER_P (retval
))
8563 error ("returning initializer list");
8564 type
= error_mark_node
;
8570 auto_node
= type_uses_auto (current_function_auto_return_pattern
);
8571 type
= do_auto_deduction (current_function_auto_return_pattern
,
8575 if (type
== error_mark_node
)
8577 else if (functype
== current_function_auto_return_pattern
)
8578 apply_deduced_return_type (current_function_decl
, type
);
8580 /* A mismatch should have been diagnosed in do_auto_deduction. */
8581 gcc_assert (same_type_p (type
, functype
));
8585 result
= DECL_RESULT (current_function_decl
);
8586 valtype
= TREE_TYPE (result
);
8587 gcc_assert (valtype
!= NULL_TREE
);
8588 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
8590 /* Check for a return statement with no return value in a function
8591 that's supposed to return a value. */
8592 if (!retval
&& fn_returns_value_p
)
8594 if (functype
!= error_mark_node
)
8595 permerror (input_location
, "return-statement with no value, in "
8596 "function returning %qT", valtype
);
8597 /* Remember that this function did return. */
8598 current_function_returns_value
= 1;
8599 /* And signal caller that TREE_NO_WARNING should be set on the
8600 RETURN_EXPR to avoid control reaches end of non-void function
8601 warnings in tree-cfg.c. */
8604 /* Check for a return statement with a value in a function that
8605 isn't supposed to return a value. */
8606 else if (retval
&& !fn_returns_value_p
)
8608 if (VOID_TYPE_P (TREE_TYPE (retval
)))
8609 /* You can return a `void' value from a function of `void'
8610 type. In that case, we have to evaluate the expression for
8611 its side-effects. */
8612 finish_expr_stmt (retval
);
8614 permerror (input_location
, "return-statement with a value, in function "
8615 "returning 'void'");
8616 current_function_returns_null
= 1;
8618 /* There's really no value to return, after all. */
8622 /* Remember that this function can sometimes return without a
8624 current_function_returns_null
= 1;
8626 /* Remember that this function did return a value. */
8627 current_function_returns_value
= 1;
8629 /* Check for erroneous operands -- but after giving ourselves a
8630 chance to provide an error about returning a value from a void
8632 if (error_operand_p (retval
))
8634 current_function_return_value
= error_mark_node
;
8635 return error_mark_node
;
8638 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8639 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == NEW_EXPR
8640 || DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == VEC_NEW_EXPR
)
8641 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
8643 && retval
&& null_ptr_cst_p (retval
))
8644 warning (0, "%<operator new%> must not return NULL unless it is "
8645 "declared %<throw()%> (or -fcheck-new is in effect)");
8647 /* Effective C++ rule 15. See also start_function. */
8649 && DECL_NAME (current_function_decl
) == ansi_assopname(NOP_EXPR
))
8653 /* The function return type must be a reference to the current
8655 if (TREE_CODE (valtype
) == REFERENCE_TYPE
8656 && same_type_ignoring_top_level_qualifiers_p
8657 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
)))
8659 /* Returning '*this' is obviously OK. */
8660 if (retval
== current_class_ref
)
8662 /* If we are calling a function whose return type is the same of
8663 the current class reference, it is ok. */
8664 else if (INDIRECT_REF_P (retval
)
8665 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
8670 warning (OPT_Weffc__
, "%<operator=%> should return a reference to %<*this%>");
8673 if (processing_template_decl
)
8675 /* We should not have changed the return value. */
8676 gcc_assert (retval
== saved_retval
);
8680 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8682 [...] For a function with a class return type, if the expression
8683 in the return statement is the name of a local object, and the cv-
8684 unqualified type of the local object is the same as the function
8685 return type, an implementation is permitted to omit creating the tem-
8686 porary object to hold the function return value [...]
8688 So, if this is a value-returning function that always returns the same
8689 local variable, remember it.
8691 It might be nice to be more flexible, and choose the first suitable
8692 variable even if the function sometimes returns something else, but
8693 then we run the risk of clobbering the variable we chose if the other
8694 returned expression uses the chosen variable somehow. And people expect
8695 this restriction, anyway. (jason 2000-11-19)
8697 See finish_function and finalize_nrv for the rest of this optimization. */
8699 named_return_value_okay_p
=
8700 (retval
!= NULL_TREE
8701 /* Must be a local, automatic variable. */
8703 && DECL_CONTEXT (retval
) == current_function_decl
8704 && ! TREE_STATIC (retval
)
8705 /* And not a lambda or anonymous union proxy. */
8706 && !DECL_HAS_VALUE_EXPR_P (retval
)
8707 && (DECL_ALIGN (retval
) <= DECL_ALIGN (result
))
8708 /* The cv-unqualified type of the returned value must be the
8709 same as the cv-unqualified return type of the
8711 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval
))),
8712 (TYPE_MAIN_VARIANT (functype
)))
8713 /* And the returned value must be non-volatile. */
8714 && ! TYPE_VOLATILE (TREE_TYPE (retval
)));
8716 if (fn_returns_value_p
&& flag_elide_constructors
)
8718 if (named_return_value_okay_p
8719 && (current_function_return_value
== NULL_TREE
8720 || current_function_return_value
== retval
))
8721 current_function_return_value
= retval
;
8723 current_function_return_value
= error_mark_node
;
8726 /* We don't need to do any conversions when there's nothing being
8731 /* Do any required conversions. */
8732 if (retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
8733 /* No conversions are required. */
8737 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
8739 /* The functype's return type will have been set to void, if it
8740 was an incomplete type. Just treat this as 'return;' */
8741 if (VOID_TYPE_P (functype
))
8742 return error_mark_node
;
8744 /* If we had an id-expression obfuscated by force_paren_expr, we need
8745 to undo it so we can try to treat it as an rvalue below. */
8746 if (cxx_dialect
>= cxx14
8747 && INDIRECT_REF_P (retval
)
8748 && REF_PARENTHESIZED_P (retval
))
8750 retval
= TREE_OPERAND (retval
, 0);
8751 while (TREE_CODE (retval
) == NON_LVALUE_EXPR
8752 || TREE_CODE (retval
) == NOP_EXPR
)
8753 retval
= TREE_OPERAND (retval
, 0);
8754 gcc_assert (TREE_CODE (retval
) == ADDR_EXPR
);
8755 retval
= TREE_OPERAND (retval
, 0);
8758 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
8759 treated as an rvalue for the purposes of overload resolution to
8760 favor move constructors over copy constructors.
8762 Note that these conditions are similar to, but not as strict as,
8763 the conditions for the named return value optimization. */
8764 if ((cxx_dialect
!= cxx98
)
8765 && ((VAR_P (retval
) && !DECL_HAS_VALUE_EXPR_P (retval
))
8766 || TREE_CODE (retval
) == PARM_DECL
)
8767 && DECL_CONTEXT (retval
) == current_function_decl
8768 && !TREE_STATIC (retval
)
8769 /* This is only interesting for class type. */
8770 && CLASS_TYPE_P (functype
))
8771 flags
= flags
| LOOKUP_PREFER_RVALUE
;
8773 /* First convert the value to the function's return type, then
8774 to the type of return value's location to handle the
8775 case that functype is smaller than the valtype. */
8776 retval
= convert_for_initialization
8777 (NULL_TREE
, functype
, retval
, flags
, ICR_RETURN
, NULL_TREE
, 0,
8778 tf_warning_or_error
);
8779 retval
= convert (valtype
, retval
);
8781 /* If the conversion failed, treat this just like `return;'. */
8782 if (retval
== error_mark_node
)
8784 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8785 else if (! cfun
->returns_struct
8786 && TREE_CODE (retval
) == TARGET_EXPR
8787 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
8788 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
8789 TREE_OPERAND (retval
, 0));
8790 else if (maybe_warn_about_returning_address_of_local (retval
))
8791 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
8792 build_zero_cst (TREE_TYPE (retval
)));
8795 /* Actually copy the value returned into the appropriate location. */
8796 if (retval
&& retval
!= result
)
8797 retval
= build2 (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
8803 /* Returns nonzero if the pointer-type FROM can be converted to the
8804 pointer-type TO via a qualification conversion. If CONSTP is -1,
8805 then we return nonzero if the pointers are similar, and the
8806 cv-qualification signature of FROM is a proper subset of that of TO.
8808 If CONSTP is positive, then all outer pointers have been
8812 comp_ptr_ttypes_real (tree to
, tree from
, int constp
)
8814 bool to_more_cv_qualified
= false;
8815 bool is_opaque_pointer
= false;
8817 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
8819 if (TREE_CODE (to
) != TREE_CODE (from
))
8822 if (TREE_CODE (from
) == OFFSET_TYPE
8823 && !same_type_p (TYPE_OFFSET_BASETYPE (from
),
8824 TYPE_OFFSET_BASETYPE (to
)))
8827 /* Const and volatile mean something different for function types,
8828 so the usual checks are not appropriate. */
8829 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
8831 if (!at_least_as_qualified_p (to
, from
))
8834 if (!at_least_as_qualified_p (from
, to
))
8838 to_more_cv_qualified
= true;
8842 constp
&= TYPE_READONLY (to
);
8845 if (VECTOR_TYPE_P (to
))
8846 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
8848 if (!TYPE_PTR_P (to
) && !TYPE_PTRDATAMEM_P (to
))
8849 return ((constp
>= 0 || to_more_cv_qualified
)
8850 && (is_opaque_pointer
8851 || same_type_ignoring_top_level_qualifiers_p (to
, from
)));
8855 /* When comparing, say, char ** to char const **, this function takes
8856 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8857 types to this function. */
8860 comp_ptr_ttypes (tree to
, tree from
)
8862 return comp_ptr_ttypes_real (to
, from
, 1);
8865 /* Returns true iff FNTYPE is a non-class type that involves
8866 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8867 if a parameter type is ill-formed. */
8870 error_type_p (const_tree type
)
8874 switch (TREE_CODE (type
))
8880 case REFERENCE_TYPE
:
8882 return error_type_p (TREE_TYPE (type
));
8886 if (error_type_p (TREE_TYPE (type
)))
8888 for (t
= TYPE_ARG_TYPES (type
); t
; t
= TREE_CHAIN (t
))
8889 if (error_type_p (TREE_VALUE (t
)))
8894 if (TYPE_PTRMEMFUNC_P (type
))
8895 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type
));
8903 /* Returns true if to and from are (possibly multi-level) pointers to the same
8904 type or inheritance-related types, regardless of cv-quals. */
8907 ptr_reasonably_similar (const_tree to
, const_tree from
)
8909 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
8911 /* Any target type is similar enough to void. */
8912 if (VOID_TYPE_P (to
))
8913 return !error_type_p (from
);
8914 if (VOID_TYPE_P (from
))
8915 return !error_type_p (to
);
8917 if (TREE_CODE (to
) != TREE_CODE (from
))
8920 if (TREE_CODE (from
) == OFFSET_TYPE
8921 && comptypes (TYPE_OFFSET_BASETYPE (to
),
8922 TYPE_OFFSET_BASETYPE (from
),
8923 COMPARE_BASE
| COMPARE_DERIVED
))
8926 if (VECTOR_TYPE_P (to
)
8927 && vector_types_convertible_p (to
, from
, false))
8930 if (TREE_CODE (to
) == INTEGER_TYPE
8931 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
8934 if (TREE_CODE (to
) == FUNCTION_TYPE
)
8935 return !error_type_p (to
) && !error_type_p (from
);
8937 if (!TYPE_PTR_P (to
))
8939 /* When either type is incomplete avoid DERIVED_FROM_P,
8940 which may call complete_type (c++/57942). */
8941 bool b
= !COMPLETE_TYPE_P (to
) || !COMPLETE_TYPE_P (from
);
8943 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
8944 b
? COMPARE_STRICT
: COMPARE_BASE
| COMPARE_DERIVED
);
8949 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8950 pointer-to-member types) are the same, ignoring cv-qualification at
8954 comp_ptr_ttypes_const (tree to
, tree from
)
8956 bool is_opaque_pointer
= false;
8958 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
8960 if (TREE_CODE (to
) != TREE_CODE (from
))
8963 if (TREE_CODE (from
) == OFFSET_TYPE
8964 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
8965 TYPE_OFFSET_BASETYPE (to
)))
8968 if (VECTOR_TYPE_P (to
))
8969 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
8971 if (!TYPE_PTR_P (to
))
8972 return (is_opaque_pointer
8973 || same_type_ignoring_top_level_qualifiers_p (to
, from
));
8977 /* Returns the type qualifiers for this type, including the qualifiers on the
8978 elements for an array type. */
8981 cp_type_quals (const_tree type
)
8984 /* This CONST_CAST is okay because strip_array_types returns its
8985 argument unmodified and we assign it to a const_tree. */
8986 type
= strip_array_types (CONST_CAST_TREE (type
));
8987 if (type
== error_mark_node
8988 /* Quals on a FUNCTION_TYPE are memfn quals. */
8989 || TREE_CODE (type
) == FUNCTION_TYPE
)
8990 return TYPE_UNQUALIFIED
;
8991 quals
= TYPE_QUALS (type
);
8992 /* METHOD and REFERENCE_TYPEs should never have quals. */
8993 gcc_assert ((TREE_CODE (type
) != METHOD_TYPE
8994 && TREE_CODE (type
) != REFERENCE_TYPE
)
8995 || ((quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
))
8996 == TYPE_UNQUALIFIED
));
9000 /* Returns the function-ref-qualifier for TYPE */
9003 type_memfn_rqual (const_tree type
)
9005 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
9006 || TREE_CODE (type
) == METHOD_TYPE
);
9008 if (!FUNCTION_REF_QUALIFIED (type
))
9009 return REF_QUAL_NONE
;
9010 else if (FUNCTION_RVALUE_QUALIFIED (type
))
9011 return REF_QUAL_RVALUE
;
9013 return REF_QUAL_LVALUE
;
9016 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9020 type_memfn_quals (const_tree type
)
9022 if (TREE_CODE (type
) == FUNCTION_TYPE
)
9023 return TYPE_QUALS (type
);
9024 else if (TREE_CODE (type
) == METHOD_TYPE
)
9025 return cp_type_quals (class_of_this_parm (type
));
9030 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9031 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9034 apply_memfn_quals (tree type
, cp_cv_quals memfn_quals
, cp_ref_qualifier rqual
)
9036 /* Could handle METHOD_TYPE here if necessary. */
9037 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
9038 if (TYPE_QUALS (type
) == memfn_quals
9039 && type_memfn_rqual (type
) == rqual
)
9042 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9044 tree result
= build_qualified_type (type
, memfn_quals
);
9045 if (tree canon
= TYPE_CANONICAL (result
))
9046 if (canon
!= result
)
9047 /* check_qualified_type doesn't check the ref-qualifier, so make sure
9048 TYPE_CANONICAL is correct. */
9049 TYPE_CANONICAL (result
)
9050 = build_ref_qualified_type (canon
, type_memfn_rqual (result
));
9051 result
= build_exception_variant (result
, TYPE_RAISES_EXCEPTIONS (type
));
9052 return build_ref_qualified_type (result
, rqual
);
9055 /* Returns nonzero if TYPE is const or volatile. */
9058 cv_qualified_p (const_tree type
)
9060 int quals
= cp_type_quals (type
);
9061 return (quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)) != 0;
9064 /* Returns nonzero if the TYPE contains a mutable member. */
9067 cp_has_mutable_p (const_tree type
)
9069 /* This CONST_CAST is okay because strip_array_types returns its
9070 argument unmodified and we assign it to a const_tree. */
9071 type
= strip_array_types (CONST_CAST_TREE(type
));
9073 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
9076 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9077 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9078 approximation. In particular, consider:
9081 struct S { int i; };
9082 const S s = { f(); }
9084 Here, we will make "s" as TREE_READONLY (because it is declared
9085 "const") -- only to reverse ourselves upon seeing that the
9086 initializer is non-constant. */
9089 cp_apply_type_quals_to_decl (int type_quals
, tree decl
)
9091 tree type
= TREE_TYPE (decl
);
9093 if (type
== error_mark_node
)
9096 if (TREE_CODE (decl
) == TYPE_DECL
)
9099 gcc_assert (!(TREE_CODE (type
) == FUNCTION_TYPE
9100 && type_quals
!= TYPE_UNQUALIFIED
));
9102 /* Avoid setting TREE_READONLY incorrectly. */
9103 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9104 constructor can produce constant init, so rely on cp_finish_decl to
9105 clear TREE_READONLY if the variable has non-constant init. */
9107 /* If the type has (or might have) a mutable component, that component
9108 might be modified. */
9109 if (TYPE_HAS_MUTABLE_P (type
) || !COMPLETE_TYPE_P (type
))
9110 type_quals
&= ~TYPE_QUAL_CONST
;
9112 c_apply_type_quals_to_decl (type_quals
, decl
);
9115 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9116 exemplar types such that casting T1 to T2 is casting away constness
9117 if and only if there is no implicit conversion from T1 to T2. */
9120 casts_away_constness_r (tree
*t1
, tree
*t2
, tsubst_flags_t complain
)
9125 /* [expr.const.cast]
9127 For multi-level pointer to members and multi-level mixed pointers
9128 and pointers to members (conv.qual), the "member" aspect of a
9129 pointer to member level is ignored when determining if a const
9130 cv-qualifier has been cast away. */
9131 /* [expr.const.cast]
9133 For two pointer types:
9135 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9136 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9139 casting from X1 to X2 casts away constness if, for a non-pointer
9140 type T there does not exist an implicit conversion (clause
9143 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9147 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9148 if ((!TYPE_PTR_P (*t1
) && !TYPE_PTRDATAMEM_P (*t1
))
9149 || (!TYPE_PTR_P (*t2
) && !TYPE_PTRDATAMEM_P (*t2
)))
9151 *t1
= cp_build_qualified_type (void_type_node
,
9152 cp_type_quals (*t1
));
9153 *t2
= cp_build_qualified_type (void_type_node
,
9154 cp_type_quals (*t2
));
9158 quals1
= cp_type_quals (*t1
);
9159 quals2
= cp_type_quals (*t2
);
9161 if (TYPE_PTRDATAMEM_P (*t1
))
9162 *t1
= TYPE_PTRMEM_POINTED_TO_TYPE (*t1
);
9164 *t1
= TREE_TYPE (*t1
);
9165 if (TYPE_PTRDATAMEM_P (*t2
))
9166 *t2
= TYPE_PTRMEM_POINTED_TO_TYPE (*t2
);
9168 *t2
= TREE_TYPE (*t2
);
9170 casts_away_constness_r (t1
, t2
, complain
);
9171 *t1
= build_pointer_type (*t1
);
9172 *t2
= build_pointer_type (*t2
);
9173 *t1
= cp_build_qualified_type (*t1
, quals1
);
9174 *t2
= cp_build_qualified_type (*t2
, quals2
);
9177 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9180 ??? This function returns non-zero if casting away qualifiers not
9181 just const. We would like to return to the caller exactly which
9182 qualifiers are casted away to give more accurate diagnostics.
9186 casts_away_constness (tree t1
, tree t2
, tsubst_flags_t complain
)
9188 if (TREE_CODE (t2
) == REFERENCE_TYPE
)
9190 /* [expr.const.cast]
9192 Casting from an lvalue of type T1 to an lvalue of type T2
9193 using a reference cast casts away constness if a cast from an
9194 rvalue of type "pointer to T1" to the type "pointer to T2"
9195 casts away constness. */
9196 t1
= (TREE_CODE (t1
) == REFERENCE_TYPE
? TREE_TYPE (t1
) : t1
);
9197 return casts_away_constness (build_pointer_type (t1
),
9198 build_pointer_type (TREE_TYPE (t2
)),
9202 if (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
9203 /* [expr.const.cast]
9205 Casting from an rvalue of type "pointer to data member of X
9206 of type T1" to the type "pointer to data member of Y of type
9207 T2" casts away constness if a cast from an rvalue of type
9208 "pointer to T1" to the type "pointer to T2" casts away
9210 return casts_away_constness
9211 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1
)),
9212 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2
)),
9215 /* Casting away constness is only something that makes sense for
9216 pointer or reference types. */
9217 if (!TYPE_PTR_P (t1
) || !TYPE_PTR_P (t2
))
9220 /* Top-level qualifiers don't matter. */
9221 t1
= TYPE_MAIN_VARIANT (t1
);
9222 t2
= TYPE_MAIN_VARIANT (t2
);
9223 casts_away_constness_r (&t1
, &t2
, complain
);
9224 if (!can_convert (t2
, t1
, complain
))
9230 /* If T is a REFERENCE_TYPE return the type to which T refers.
9231 Otherwise, return T itself. */
9234 non_reference (tree t
)
9236 if (t
&& TREE_CODE (t
) == REFERENCE_TYPE
)
9242 /* Return nonzero if REF is an lvalue valid for this language;
9243 otherwise, print an error message and return zero. USE says
9244 how the lvalue is being used and so selects the error message. */
9247 lvalue_or_else (tree ref
, enum lvalue_use use
, tsubst_flags_t complain
)
9249 cp_lvalue_kind kind
= lvalue_kind (ref
);
9251 if (kind
== clk_none
)
9253 if (complain
& tf_error
)
9254 lvalue_error (input_location
, use
);
9257 else if (kind
& (clk_rvalueref
|clk_class
))
9259 if (!(complain
& tf_error
))
9261 if (kind
& clk_class
)
9262 /* Make this a permerror because we used to accept it. */
9263 permerror (input_location
, "using temporary as lvalue");
9265 error ("using xvalue (rvalue reference) as lvalue");
9270 /* Return true if a user-defined literal operator is a raw operator. */
9273 check_raw_literal_operator (const_tree decl
)
9275 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9278 bool maybe_raw_p
= false;
9280 /* Count the number and type of arguments and check for ellipsis. */
9281 for (argtype
= argtypes
, arity
= 0;
9282 argtype
&& argtype
!= void_list_node
;
9283 ++arity
, argtype
= TREE_CHAIN (argtype
))
9285 tree t
= TREE_VALUE (argtype
);
9287 if (same_type_p (t
, const_string_type_node
))
9291 return false; /* Found ellipsis. */
9293 if (!maybe_raw_p
|| arity
!= 1)
9300 /* Return true if a user-defined literal operator has one of the allowed
9304 check_literal_operator_args (const_tree decl
,
9305 bool *long_long_unsigned_p
, bool *long_double_p
)
9307 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9309 *long_long_unsigned_p
= false;
9310 *long_double_p
= false;
9311 if (processing_template_decl
|| processing_specialization
)
9312 return argtypes
== void_list_node
;
9319 /* Count the number and type of arguments and check for ellipsis. */
9320 for (argtype
= argtypes
, arity
= 0;
9321 argtype
&& argtype
!= void_list_node
;
9322 argtype
= TREE_CHAIN (argtype
))
9324 tree t
= TREE_VALUE (argtype
);
9329 bool maybe_raw_p
= false;
9331 if (cp_type_quals (t
) != TYPE_QUAL_CONST
)
9333 t
= TYPE_MAIN_VARIANT (t
);
9334 if ((maybe_raw_p
= same_type_p (t
, char_type_node
))
9335 || same_type_p (t
, wchar_type_node
)
9336 || same_type_p (t
, char16_type_node
)
9337 || same_type_p (t
, char32_type_node
))
9339 argtype
= TREE_CHAIN (argtype
);
9342 t
= TREE_VALUE (argtype
);
9343 if (maybe_raw_p
&& argtype
== void_list_node
)
9345 else if (same_type_p (t
, size_type_node
))
9354 else if (same_type_p (t
, long_long_unsigned_type_node
))
9357 *long_long_unsigned_p
= true;
9359 else if (same_type_p (t
, long_double_type_node
))
9362 *long_double_p
= true;
9364 else if (same_type_p (t
, char_type_node
))
9366 else if (same_type_p (t
, wchar_type_node
))
9368 else if (same_type_p (t
, char16_type_node
))
9370 else if (same_type_p (t
, char32_type_node
))
9376 return false; /* Found ellipsis. */
9378 if (arity
!= max_arity
)