1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2013 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"
34 #include "diagnostic.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "c-family/c-ubsan.h"
43 static tree
pfn_from_ptrmemfunc (tree
);
44 static tree
delta_from_ptrmemfunc (tree
);
45 static tree
convert_for_assignment (tree
, tree
, impl_conv_rhs
, tree
, int,
47 static tree
cp_pointer_int_sum (enum tree_code
, tree
, tree
, tsubst_flags_t
);
48 static tree
rationalize_conditional_expr (enum tree_code
, tree
,
50 static int comp_ptr_ttypes_real (tree
, tree
, int);
51 static bool comp_except_types (tree
, tree
, bool);
52 static bool comp_array_types (const_tree
, const_tree
, bool);
53 static tree
pointer_diff (tree
, tree
, tree
, tsubst_flags_t
);
54 static tree
get_delta_difference (tree
, tree
, bool, bool, tsubst_flags_t
);
55 static void casts_away_constness_r (tree
*, tree
*, tsubst_flags_t
);
56 static bool casts_away_constness (tree
, tree
, tsubst_flags_t
);
57 static void maybe_warn_about_returning_address_of_local (tree
);
58 static tree
lookup_destructor (tree
, tree
, tree
, tsubst_flags_t
);
59 static void warn_args_num (location_t
, tree
, bool);
60 static int convert_arguments (tree
, vec
<tree
, va_gc
> **, tree
, int,
63 /* Do `exp = require_complete_type (exp);' to make sure exp
64 does not have an incomplete type. (That includes void types.)
65 Returns error_mark_node if the VALUE does not have
66 complete type when this function returns. */
69 require_complete_type_sfinae (tree value
, tsubst_flags_t complain
)
73 if (processing_template_decl
|| value
== error_mark_node
)
76 if (TREE_CODE (value
) == OVERLOAD
)
77 type
= unknown_type_node
;
79 type
= TREE_TYPE (value
);
81 if (type
== error_mark_node
)
82 return error_mark_node
;
84 /* First, detect a valid value with a complete type. */
85 if (COMPLETE_TYPE_P (type
))
88 if (complete_type_or_maybe_complain (type
, value
, complain
))
91 return error_mark_node
;
95 require_complete_type (tree value
)
97 return require_complete_type_sfinae (value
, tf_warning_or_error
);
100 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
101 a template instantiation, do the instantiation. Returns TYPE,
102 whether or not it could be completed, unless something goes
103 horribly wrong, in which case the error_mark_node is returned. */
106 complete_type (tree type
)
108 if (type
== NULL_TREE
)
109 /* Rather than crash, we return something sure to cause an error
111 return error_mark_node
;
113 if (type
== error_mark_node
|| COMPLETE_TYPE_P (type
))
115 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
117 tree t
= complete_type (TREE_TYPE (type
));
118 unsigned int needs_constructing
, has_nontrivial_dtor
;
119 if (COMPLETE_TYPE_P (t
) && !dependent_type_p (type
))
122 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
124 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
125 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
127 TYPE_NEEDS_CONSTRUCTING (t
) = needs_constructing
;
128 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = has_nontrivial_dtor
;
131 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
132 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
137 /* Like complete_type, but issue an error if the TYPE cannot be completed.
138 VALUE is used for informative diagnostics.
139 Returns NULL_TREE if the type cannot be made complete. */
142 complete_type_or_maybe_complain (tree type
, tree value
, tsubst_flags_t complain
)
144 type
= complete_type (type
);
145 if (type
== error_mark_node
)
146 /* We already issued an error. */
148 else if (!COMPLETE_TYPE_P (type
))
150 if (complain
& tf_error
)
151 cxx_incomplete_type_diagnostic (value
, type
, DK_ERROR
);
159 complete_type_or_else (tree type
, tree value
)
161 return complete_type_or_maybe_complain (type
, value
, tf_warning_or_error
);
164 /* Return truthvalue of whether type of EXP is instantiated. */
167 type_unknown_p (const_tree exp
)
169 return (TREE_CODE (exp
) == TREE_LIST
170 || TREE_TYPE (exp
) == unknown_type_node
);
174 /* Return the common type of two parameter lists.
175 We assume that comptypes has already been done and returned 1;
176 if that isn't so, this may crash.
178 As an optimization, free the space we allocate if the parameter
179 lists are already common. */
182 commonparms (tree p1
, tree p2
)
184 tree oldargs
= p1
, newargs
, n
;
188 len
= list_length (p1
);
189 newargs
= tree_last (p1
);
191 if (newargs
== void_list_node
)
200 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
205 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
207 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
209 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
212 else if (! TREE_PURPOSE (p1
))
214 if (TREE_PURPOSE (p2
))
216 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
222 if (1 != simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)))
224 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
226 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
229 TREE_VALUE (n
) = merge_types (TREE_VALUE (p1
), TREE_VALUE (p2
));
232 TREE_VALUE (n
) = TREE_VALUE (p1
);
240 /* Given a type, perhaps copied for a typedef,
241 find the "original" version of it. */
243 original_type (tree t
)
245 int quals
= cp_type_quals (t
);
246 while (t
!= error_mark_node
247 && TYPE_NAME (t
) != NULL_TREE
)
249 tree x
= TYPE_NAME (t
);
250 if (TREE_CODE (x
) != TYPE_DECL
)
252 x
= DECL_ORIGINAL_TYPE (x
);
257 return cp_build_qualified_type (t
, quals
);
260 /* Return the common type for two arithmetic types T1 and T2 under the
261 usual arithmetic conversions. The default conversions have already
262 been applied, and enumerated types converted to their compatible
266 cp_common_type (tree t1
, tree t2
)
268 enum tree_code code1
= TREE_CODE (t1
);
269 enum tree_code code2
= TREE_CODE (t2
);
273 /* In what follows, we slightly generalize the rules given in [expr] so
274 as to deal with `long long' and `complex'. First, merge the
276 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
278 if (SCOPED_ENUM_P (t1
) || SCOPED_ENUM_P (t2
))
280 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
281 return build_type_attribute_variant (t1
, attributes
);
286 /* FIXME: Attributes. */
287 gcc_assert (ARITHMETIC_TYPE_P (t1
)
288 || TREE_CODE (t1
) == VECTOR_TYPE
289 || UNSCOPED_ENUM_P (t1
));
290 gcc_assert (ARITHMETIC_TYPE_P (t2
)
291 || TREE_CODE (t2
) == VECTOR_TYPE
292 || UNSCOPED_ENUM_P (t2
));
294 /* If one type is complex, form the common type of the non-complex
295 components, then make that complex. Use T1 or T2 if it is the
297 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
299 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
300 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
302 = type_after_usual_arithmetic_conversions (subtype1
, subtype2
);
304 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
305 return build_type_attribute_variant (t1
, attributes
);
306 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
307 return build_type_attribute_variant (t2
, attributes
);
309 return build_type_attribute_variant (build_complex_type (subtype
),
313 if (code1
== VECTOR_TYPE
)
315 /* When we get here we should have two vectors of the same size.
316 Just prefer the unsigned one if present. */
317 if (TYPE_UNSIGNED (t1
))
318 return build_type_attribute_variant (t1
, attributes
);
320 return build_type_attribute_variant (t2
, attributes
);
323 /* If only one is real, use it as the result. */
324 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
325 return build_type_attribute_variant (t1
, attributes
);
326 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
327 return build_type_attribute_variant (t2
, attributes
);
329 /* Both real or both integers; use the one with greater precision. */
330 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
331 return build_type_attribute_variant (t1
, attributes
);
332 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
333 return build_type_attribute_variant (t2
, attributes
);
335 /* The types are the same; no need to do anything fancy. */
336 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
337 return build_type_attribute_variant (t1
, attributes
);
339 if (code1
!= REAL_TYPE
)
341 /* If one is unsigned long long, then convert the other to unsigned
343 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_unsigned_type_node
)
344 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_unsigned_type_node
))
345 return build_type_attribute_variant (long_long_unsigned_type_node
,
347 /* If one is a long long, and the other is an unsigned long, and
348 long long can represent all the values of an unsigned long, then
349 convert to a long long. Otherwise, convert to an unsigned long
350 long. Otherwise, if either operand is long long, convert the
353 Since we're here, we know the TYPE_PRECISION is the same;
354 therefore converting to long long cannot represent all the values
355 of an unsigned long, so we choose unsigned long long in that
357 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_integer_type_node
)
358 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_integer_type_node
))
360 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
361 ? long_long_unsigned_type_node
362 : long_long_integer_type_node
);
363 return build_type_attribute_variant (t
, attributes
);
365 if (int128_integer_type_node
!= NULL_TREE
366 && (same_type_p (TYPE_MAIN_VARIANT (t1
),
367 int128_integer_type_node
)
368 || same_type_p (TYPE_MAIN_VARIANT (t2
),
369 int128_integer_type_node
)))
371 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
372 ? int128_unsigned_type_node
373 : int128_integer_type_node
);
374 return build_type_attribute_variant (t
, attributes
);
377 /* Go through the same procedure, but for longs. */
378 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_unsigned_type_node
)
379 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_unsigned_type_node
))
380 return build_type_attribute_variant (long_unsigned_type_node
,
382 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_integer_type_node
)
383 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_integer_type_node
))
385 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
386 ? long_unsigned_type_node
: long_integer_type_node
);
387 return build_type_attribute_variant (t
, attributes
);
389 /* Otherwise prefer the unsigned one. */
390 if (TYPE_UNSIGNED (t1
))
391 return build_type_attribute_variant (t1
, attributes
);
393 return build_type_attribute_variant (t2
, attributes
);
397 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_double_type_node
)
398 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_double_type_node
))
399 return build_type_attribute_variant (long_double_type_node
,
401 if (same_type_p (TYPE_MAIN_VARIANT (t1
), double_type_node
)
402 || same_type_p (TYPE_MAIN_VARIANT (t2
), double_type_node
))
403 return build_type_attribute_variant (double_type_node
,
405 if (same_type_p (TYPE_MAIN_VARIANT (t1
), float_type_node
)
406 || same_type_p (TYPE_MAIN_VARIANT (t2
), float_type_node
))
407 return build_type_attribute_variant (float_type_node
,
410 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
411 the standard C++ floating-point types. Logic earlier in this
412 function has already eliminated the possibility that
413 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
414 compelling reason to choose one or the other. */
415 return build_type_attribute_variant (t1
, attributes
);
419 /* T1 and T2 are arithmetic or enumeration types. Return the type
420 that will result from the "usual arithmetic conversions" on T1 and
421 T2 as described in [expr]. */
424 type_after_usual_arithmetic_conversions (tree t1
, tree t2
)
426 gcc_assert (ARITHMETIC_TYPE_P (t1
)
427 || TREE_CODE (t1
) == VECTOR_TYPE
428 || UNSCOPED_ENUM_P (t1
));
429 gcc_assert (ARITHMETIC_TYPE_P (t2
)
430 || TREE_CODE (t2
) == VECTOR_TYPE
431 || UNSCOPED_ENUM_P (t2
));
433 /* Perform the integral promotions. We do not promote real types here. */
434 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1
)
435 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2
))
437 t1
= type_promotes_to (t1
);
438 t2
= type_promotes_to (t2
);
441 return cp_common_type (t1
, t2
);
445 composite_pointer_error (diagnostic_t kind
, tree t1
, tree t2
,
446 composite_pointer_operation operation
)
451 emit_diagnostic (kind
, input_location
, 0,
452 "comparison between "
453 "distinct pointer types %qT and %qT lacks a cast",
457 emit_diagnostic (kind
, input_location
, 0,
458 "conversion between "
459 "distinct pointer types %qT and %qT lacks a cast",
462 case CPO_CONDITIONAL_EXPR
:
463 emit_diagnostic (kind
, input_location
, 0,
464 "conditional expression between "
465 "distinct pointer types %qT and %qT lacks a cast",
473 /* Subroutine of composite_pointer_type to implement the recursive
474 case. See that function for documentation of the parameters. */
477 composite_pointer_type_r (tree t1
, tree t2
,
478 composite_pointer_operation operation
,
479 tsubst_flags_t complain
)
486 /* Determine the types pointed to by T1 and T2. */
489 pointee1
= TREE_TYPE (t1
);
490 pointee2
= TREE_TYPE (t2
);
494 pointee1
= TYPE_PTRMEM_POINTED_TO_TYPE (t1
);
495 pointee2
= TYPE_PTRMEM_POINTED_TO_TYPE (t2
);
500 Otherwise, the composite pointer type is a pointer type
501 similar (_conv.qual_) to the type of one of the operands,
502 with a cv-qualification signature (_conv.qual_) that is the
503 union of the cv-qualification signatures of the operand
505 if (same_type_ignoring_top_level_qualifiers_p (pointee1
, pointee2
))
506 result_type
= pointee1
;
507 else if ((TYPE_PTR_P (pointee1
) && TYPE_PTR_P (pointee2
))
508 || (TYPE_PTRMEM_P (pointee1
) && TYPE_PTRMEM_P (pointee2
)))
510 result_type
= composite_pointer_type_r (pointee1
, pointee2
, operation
,
512 if (result_type
== error_mark_node
)
513 return error_mark_node
;
517 if (complain
& tf_error
)
518 composite_pointer_error (DK_PERMERROR
, t1
, t2
, operation
);
520 return error_mark_node
;
521 result_type
= void_type_node
;
523 result_type
= cp_build_qualified_type (result_type
,
524 (cp_type_quals (pointee1
)
525 | cp_type_quals (pointee2
)));
526 /* If the original types were pointers to members, so is the
528 if (TYPE_PTRMEM_P (t1
))
530 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
531 TYPE_PTRMEM_CLASS_TYPE (t2
)))
533 if (complain
& tf_error
)
534 composite_pointer_error (DK_PERMERROR
, t1
, t2
, operation
);
536 return error_mark_node
;
538 result_type
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
542 result_type
= build_pointer_type (result_type
);
544 /* Merge the attributes. */
545 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
546 return build_type_attribute_variant (result_type
, attributes
);
549 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
550 ARG1 and ARG2 are the values with those types. The OPERATION is to
551 describe the operation between the pointer types,
552 in case an error occurs.
554 This routine also implements the computation of a common type for
555 pointers-to-members as per [expr.eq]. */
558 composite_pointer_type (tree t1
, tree t2
, tree arg1
, tree arg2
,
559 composite_pointer_operation operation
,
560 tsubst_flags_t complain
)
567 If one operand is a null pointer constant, the composite pointer
568 type is the type of the other operand. */
569 if (null_ptr_cst_p (arg1
))
571 if (null_ptr_cst_p (arg2
))
578 If one of the operands has type "pointer to cv1 void*", then
579 the other has type "pointer to cv2T", and the composite pointer
580 type is "pointer to cv12 void", where cv12 is the union of cv1
583 If either type is a pointer to void, make sure it is T1. */
584 if (TYPE_PTR_P (t2
) && VOID_TYPE_P (TREE_TYPE (t2
)))
592 /* Now, if T1 is a pointer to void, merge the qualifiers. */
593 if (TYPE_PTR_P (t1
) && VOID_TYPE_P (TREE_TYPE (t1
)))
598 if (TYPE_PTRFN_P (t2
) && (complain
& tf_error
))
603 pedwarn (input_location
, OPT_Wpedantic
,
604 "ISO C++ forbids comparison between "
605 "pointer of type %<void *%> and pointer-to-function");
608 pedwarn (input_location
, OPT_Wpedantic
,
609 "ISO C++ forbids conversion between "
610 "pointer of type %<void *%> and pointer-to-function");
612 case CPO_CONDITIONAL_EXPR
:
613 pedwarn (input_location
, OPT_Wpedantic
,
614 "ISO C++ forbids conditional expression between "
615 "pointer of type %<void *%> and pointer-to-function");
622 = cp_build_qualified_type (void_type_node
,
623 (cp_type_quals (TREE_TYPE (t1
))
624 | cp_type_quals (TREE_TYPE (t2
))));
625 result_type
= build_pointer_type (result_type
);
626 /* Merge the attributes. */
627 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
628 return build_type_attribute_variant (result_type
, attributes
);
631 if (c_dialect_objc () && TYPE_PTR_P (t1
)
634 if (objc_have_common_type (t1
, t2
, -3, NULL_TREE
))
635 return objc_common_type (t1
, t2
);
638 /* [expr.eq] permits the application of a pointer conversion to
639 bring the pointers to a common type. */
640 if (TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
)
641 && CLASS_TYPE_P (TREE_TYPE (t1
))
642 && CLASS_TYPE_P (TREE_TYPE (t2
))
643 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1
),
646 class1
= TREE_TYPE (t1
);
647 class2
= TREE_TYPE (t2
);
649 if (DERIVED_FROM_P (class1
, class2
))
650 t2
= (build_pointer_type
651 (cp_build_qualified_type (class1
, cp_type_quals (class2
))));
652 else if (DERIVED_FROM_P (class2
, class1
))
653 t1
= (build_pointer_type
654 (cp_build_qualified_type (class2
, cp_type_quals (class1
))));
657 if (complain
& tf_error
)
658 composite_pointer_error (DK_ERROR
, t1
, t2
, operation
);
659 return error_mark_node
;
662 /* [expr.eq] permits the application of a pointer-to-member
663 conversion to change the class type of one of the types. */
664 else if (TYPE_PTRMEM_P (t1
)
665 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
666 TYPE_PTRMEM_CLASS_TYPE (t2
)))
668 class1
= TYPE_PTRMEM_CLASS_TYPE (t1
);
669 class2
= TYPE_PTRMEM_CLASS_TYPE (t2
);
671 if (DERIVED_FROM_P (class1
, class2
))
672 t1
= build_ptrmem_type (class2
, TYPE_PTRMEM_POINTED_TO_TYPE (t1
));
673 else if (DERIVED_FROM_P (class2
, class1
))
674 t2
= build_ptrmem_type (class1
, TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
677 if (complain
& tf_error
)
681 error ("comparison between distinct "
682 "pointer-to-member types %qT and %qT lacks a cast",
686 error ("conversion between distinct "
687 "pointer-to-member types %qT and %qT lacks a cast",
690 case CPO_CONDITIONAL_EXPR
:
691 error ("conditional expression between distinct "
692 "pointer-to-member types %qT and %qT lacks a cast",
698 return error_mark_node
;
702 return composite_pointer_type_r (t1
, t2
, operation
, complain
);
705 /* Return the merged type of two types.
706 We assume that comptypes has already been done and returned 1;
707 if that isn't so, this may crash.
709 This just combines attributes and default arguments; any other
710 differences would cause the two types to compare unalike. */
713 merge_types (tree t1
, tree t2
)
715 enum tree_code code1
;
716 enum tree_code code2
;
719 /* Save time if the two types are the same. */
722 if (original_type (t1
) == original_type (t2
))
725 /* If one type is nonsense, use the other. */
726 if (t1
== error_mark_node
)
728 if (t2
== error_mark_node
)
731 /* Handle merging an auto redeclaration with a previous deduced
736 /* Merge the attributes. */
737 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
739 if (TYPE_PTRMEMFUNC_P (t1
))
740 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
741 if (TYPE_PTRMEMFUNC_P (t2
))
742 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
744 code1
= TREE_CODE (t1
);
745 code2
= TREE_CODE (t2
);
748 gcc_assert (code1
== TYPENAME_TYPE
|| code2
== TYPENAME_TYPE
);
749 if (code1
== TYPENAME_TYPE
)
751 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
752 code1
= TREE_CODE (t1
);
756 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
757 code2
= TREE_CODE (t2
);
765 /* For two pointers, do this recursively on the target type. */
767 tree target
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
768 int quals
= cp_type_quals (t1
);
770 if (code1
== POINTER_TYPE
)
771 t1
= build_pointer_type (target
);
773 t1
= cp_build_reference_type (target
, TYPE_REF_IS_RVALUE (t1
));
774 t1
= build_type_attribute_variant (t1
, attributes
);
775 t1
= cp_build_qualified_type (t1
, quals
);
777 if (TREE_CODE (target
) == METHOD_TYPE
)
778 t1
= build_ptrmemfunc_type (t1
);
787 quals
= cp_type_quals (t1
);
788 pointee
= merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1
),
789 TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
790 t1
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
792 t1
= cp_build_qualified_type (t1
, quals
);
798 tree elt
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
799 /* Save space: see if the result is identical to one of the args. */
800 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
801 return build_type_attribute_variant (t1
, attributes
);
802 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
803 return build_type_attribute_variant (t2
, attributes
);
804 /* Merge the element types, and have a size if either arg has one. */
805 t1
= build_cplus_array_type
806 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
811 /* Function types: prefer the one that specified arg types.
812 If both do, merge the arg types. Also merge the return types. */
814 tree valtype
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
815 tree p1
= TYPE_ARG_TYPES (t1
);
816 tree p2
= TYPE_ARG_TYPES (t2
);
820 /* Save space: see if the result is identical to one of the args. */
821 if (valtype
== TREE_TYPE (t1
) && ! p2
)
822 return cp_build_type_attribute_variant (t1
, attributes
);
823 if (valtype
== TREE_TYPE (t2
) && ! p1
)
824 return cp_build_type_attribute_variant (t2
, attributes
);
826 /* Simple way if one arg fails to specify argument types. */
827 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
829 else if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
832 parms
= commonparms (p1
, p2
);
834 rval
= build_function_type (valtype
, parms
);
835 gcc_assert (type_memfn_quals (t1
) == type_memfn_quals (t2
));
836 gcc_assert (type_memfn_rqual (t1
) == type_memfn_rqual (t2
));
837 rval
= apply_memfn_quals (rval
,
838 type_memfn_quals (t1
),
839 type_memfn_rqual (t1
));
840 raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
841 TYPE_RAISES_EXCEPTIONS (t2
),
843 t1
= build_exception_variant (rval
, raises
);
849 /* Get this value the long way, since TYPE_METHOD_BASETYPE
850 is just the main variant of this. */
851 tree basetype
= class_of_this_parm (t2
);
852 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
853 TYPE_RAISES_EXCEPTIONS (t2
),
855 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
858 /* If this was a member function type, get back to the
859 original type of type member function (i.e., without
860 the class instance variable up front. */
861 t1
= build_function_type (TREE_TYPE (t1
),
862 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
863 t2
= build_function_type (TREE_TYPE (t2
),
864 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
865 t3
= merge_types (t1
, t2
);
866 t3
= build_method_type_directly (basetype
, TREE_TYPE (t3
),
867 TYPE_ARG_TYPES (t3
));
868 t1
= build_exception_variant (t3
, raises
);
869 t1
= build_ref_qualified_type (t1
, rqual
);
874 /* There is no need to merge attributes into a TYPENAME_TYPE.
875 When the type is instantiated it will have whatever
876 attributes result from the instantiation. */
882 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
884 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
887 return cp_build_type_attribute_variant (t1
, attributes
);
890 /* Return the ARRAY_TYPE type without its domain. */
893 strip_array_domain (tree type
)
896 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
897 if (TYPE_DOMAIN (type
) == NULL_TREE
)
899 t2
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
900 return cp_build_type_attribute_variant (t2
, TYPE_ATTRIBUTES (type
));
903 /* Wrapper around cp_common_type that is used by c-common.c and other
904 front end optimizations that remove promotions.
906 Return the common type for two arithmetic types T1 and T2 under the
907 usual arithmetic conversions. The default conversions have already
908 been applied, and enumerated types converted to their compatible
912 common_type (tree t1
, tree t2
)
914 /* If one type is nonsense, use the other */
915 if (t1
== error_mark_node
)
917 if (t2
== error_mark_node
)
920 return cp_common_type (t1
, t2
);
923 /* Return the common type of two pointer types T1 and T2. This is the
924 type for the result of most arithmetic operations if the operands
925 have the given two types.
927 We assume that comp_target_types has already been done and returned
928 nonzero; if that isn't so, this may crash. */
931 common_pointer_type (tree t1
, tree t2
)
933 gcc_assert ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
934 || (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
935 || (TYPE_PTRMEMFUNC_P (t1
) && TYPE_PTRMEMFUNC_P (t2
)));
937 return composite_pointer_type (t1
, t2
, error_mark_node
, error_mark_node
,
938 CPO_CONVERSION
, tf_warning_or_error
);
941 /* Compare two exception specifier types for exactness or subsetness, if
942 allowed. Returns false for mismatch, true for match (same, or
945 [except.spec] "If a class X ... objects of class X or any class publicly
946 and unambiguously derived from X. Similarly, if a pointer type Y * ...
947 exceptions of type Y * or that are pointers to any type publicly and
948 unambiguously derived from Y. Otherwise a function only allows exceptions
949 that have the same type ..."
950 This does not mention cv qualifiers and is different to what throw
951 [except.throw] and catch [except.catch] will do. They will ignore the
952 top level cv qualifiers, and allow qualifiers in the pointer to class
955 We implement the letter of the standard. */
958 comp_except_types (tree a
, tree b
, bool exact
)
960 if (same_type_p (a
, b
))
964 if (cp_type_quals (a
) || cp_type_quals (b
))
967 if (TYPE_PTR_P (a
) && TYPE_PTR_P (b
))
971 if (cp_type_quals (a
) || cp_type_quals (b
))
975 if (TREE_CODE (a
) != RECORD_TYPE
976 || TREE_CODE (b
) != RECORD_TYPE
)
979 if (publicly_uniquely_derived_p (a
, b
))
985 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
986 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
987 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
988 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
989 are unordered, but we've already filtered out duplicates. Most lists will
990 be in order, we should try to make use of that. */
993 comp_except_specs (const_tree t1
, const_tree t2
, int exact
)
1002 /* First handle noexcept. */
1003 if (exact
< ce_exact
)
1005 /* noexcept(false) is compatible with no exception-specification,
1006 and stricter than any spec. */
1007 if (t1
== noexcept_false_spec
)
1008 return t2
== NULL_TREE
|| exact
== ce_derived
;
1009 /* Even a derived noexcept(false) is compatible with no
1010 exception-specification. */
1011 if (t2
== noexcept_false_spec
)
1012 return t1
== NULL_TREE
;
1014 /* Otherwise, if we aren't looking for an exact match, noexcept is
1015 equivalent to throw(). */
1016 if (t1
== noexcept_true_spec
)
1017 t1
= empty_except_spec
;
1018 if (t2
== noexcept_true_spec
)
1019 t2
= empty_except_spec
;
1022 /* If any noexcept is left, it is only comparable to itself;
1023 either we're looking for an exact match or we're redeclaring a
1024 template with dependent noexcept. */
1025 if ((t1
&& TREE_PURPOSE (t1
))
1026 || (t2
&& TREE_PURPOSE (t2
)))
1028 && cp_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)));
1030 if (t1
== NULL_TREE
) /* T1 is ... */
1031 return t2
== NULL_TREE
|| exact
== ce_derived
;
1032 if (!TREE_VALUE (t1
)) /* t1 is EMPTY */
1033 return t2
!= NULL_TREE
&& !TREE_VALUE (t2
);
1034 if (t2
== NULL_TREE
) /* T2 is ... */
1036 if (TREE_VALUE (t1
) && !TREE_VALUE (t2
)) /* T2 is EMPTY, T1 is not */
1037 return exact
== ce_derived
;
1039 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1040 Count how many we find, to determine exactness. For exact matching and
1041 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1043 for (base
= t1
; t2
!= NULL_TREE
; t2
= TREE_CHAIN (t2
))
1045 for (probe
= base
; probe
!= NULL_TREE
; probe
= TREE_CHAIN (probe
))
1047 tree a
= TREE_VALUE (probe
);
1048 tree b
= TREE_VALUE (t2
);
1050 if (comp_except_types (a
, b
, exact
))
1052 if (probe
== base
&& exact
> ce_derived
)
1053 base
= TREE_CHAIN (probe
);
1058 if (probe
== NULL_TREE
)
1061 return exact
== ce_derived
|| base
== NULL_TREE
|| length
== list_length (t1
);
1064 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1065 [] can match [size]. */
1068 comp_array_types (const_tree t1
, const_tree t2
, bool allow_redeclaration
)
1077 /* The type of the array elements must be the same. */
1078 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1081 d1
= TYPE_DOMAIN (t1
);
1082 d2
= TYPE_DOMAIN (t2
);
1087 /* If one of the arrays is dimensionless, and the other has a
1088 dimension, they are of different types. However, it is valid to
1096 declarations for an array object can specify
1097 array types that differ by the presence or absence of a major
1098 array bound (_dcl.array_). */
1100 return allow_redeclaration
;
1102 /* Check that the dimensions are the same. */
1104 if (!cp_tree_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
)))
1106 max1
= TYPE_MAX_VALUE (d1
);
1107 max2
= TYPE_MAX_VALUE (d2
);
1108 if (processing_template_decl
&& !abi_version_at_least (2)
1109 && !value_dependent_expression_p (max1
)
1110 && !value_dependent_expression_p (max2
))
1112 /* With abi-1 we do not fold non-dependent array bounds, (and
1113 consequently mangle them incorrectly). We must therefore
1114 fold them here, to verify the domains have the same
1120 if (!cp_tree_equal (max1
, max2
))
1126 /* Compare the relative position of T1 and T2 into their respective
1127 template parameter list.
1128 T1 and T2 must be template parameter types.
1129 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1132 comp_template_parms_position (tree t1
, tree t2
)
1134 tree index1
, index2
;
1135 gcc_assert (t1
&& t2
1136 && TREE_CODE (t1
) == TREE_CODE (t2
)
1137 && (TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
1138 || TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
1139 || TREE_CODE (t1
) == TEMPLATE_TYPE_PARM
));
1141 index1
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1
));
1142 index2
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2
));
1144 /* Then compare their relative position. */
1145 if (TEMPLATE_PARM_IDX (index1
) != TEMPLATE_PARM_IDX (index2
)
1146 || TEMPLATE_PARM_LEVEL (index1
) != TEMPLATE_PARM_LEVEL (index2
)
1147 || (TEMPLATE_PARM_PARAMETER_PACK (index1
)
1148 != TEMPLATE_PARM_PARAMETER_PACK (index2
)))
1154 /* Subroutine in comptypes. */
1157 structural_comptypes (tree t1
, tree t2
, int strict
)
1162 /* Suppress errors caused by previously reported errors. */
1163 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1166 gcc_assert (TYPE_P (t1
) && TYPE_P (t2
));
1168 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1169 current instantiation. */
1170 if (TREE_CODE (t1
) == TYPENAME_TYPE
)
1171 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
1173 if (TREE_CODE (t2
) == TYPENAME_TYPE
)
1174 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
1176 if (TYPE_PTRMEMFUNC_P (t1
))
1177 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
1178 if (TYPE_PTRMEMFUNC_P (t2
))
1179 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
1181 /* Different classes of types can't be compatible. */
1182 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1185 /* Qualifiers must match. For array types, we will check when we
1186 recur on the array element types. */
1187 if (TREE_CODE (t1
) != ARRAY_TYPE
1188 && cp_type_quals (t1
) != cp_type_quals (t2
))
1190 if (TREE_CODE (t1
) == FUNCTION_TYPE
1191 && type_memfn_quals (t1
) != type_memfn_quals (t2
))
1193 /* Need to check this before TYPE_MAIN_VARIANT.
1194 FIXME function qualifiers should really change the main variant. */
1195 if ((TREE_CODE (t1
) == FUNCTION_TYPE
1196 || TREE_CODE (t1
) == METHOD_TYPE
)
1197 && type_memfn_rqual (t1
) != type_memfn_rqual (t2
))
1199 if (TYPE_FOR_JAVA (t1
) != TYPE_FOR_JAVA (t2
))
1202 /* Allow for two different type nodes which have essentially the same
1203 definition. Note that we already checked for equality of the type
1204 qualifiers (just above). */
1206 if (TREE_CODE (t1
) != ARRAY_TYPE
1207 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1211 /* Compare the types. Break out if they could be the same. */
1212 switch (TREE_CODE (t1
))
1216 /* All void and bool types are the same. */
1220 case FIXED_POINT_TYPE
:
1222 /* With these nodes, we can't determine type equivalence by
1223 looking at what is stored in the nodes themselves, because
1224 two nodes might have different TYPE_MAIN_VARIANTs but still
1225 represent the same type. For example, wchar_t and int could
1226 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1227 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1228 and are distinct types. On the other hand, int and the
1231 typedef int INT __attribute((may_alias));
1233 have identical properties, different TYPE_MAIN_VARIANTs, but
1234 represent the same type. The canonical type system keeps
1235 track of equivalence in this case, so we fall back on it. */
1236 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1238 case TEMPLATE_TEMPLATE_PARM
:
1239 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1240 if (!comp_template_parms_position (t1
, t2
))
1242 if (!comp_template_parms
1243 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1
)),
1244 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2
))))
1246 if (TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
)
1248 /* Don't check inheritance. */
1249 strict
= COMPARE_STRICT
;
1254 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
1255 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
1256 || TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1257 && comp_template_args (TYPE_TI_ARGS (t1
), TYPE_TI_ARGS (t2
)))
1260 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
1262 else if ((strict
& COMPARE_DERIVED
) && DERIVED_FROM_P (t2
, t1
))
1268 if (!comptypes (TYPE_OFFSET_BASETYPE (t1
), TYPE_OFFSET_BASETYPE (t2
),
1269 strict
& ~COMPARE_REDECLARATION
))
1271 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1275 case REFERENCE_TYPE
:
1276 if (TYPE_REF_IS_RVALUE (t1
) != TYPE_REF_IS_RVALUE (t2
))
1278 /* fall through to checks for pointer types */
1281 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1282 || TYPE_REF_CAN_ALIAS_ALL (t1
) != TYPE_REF_CAN_ALIAS_ALL (t2
)
1283 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1289 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1291 if (!compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)))
1296 /* Target types must match incl. qualifiers. */
1297 if (!comp_array_types (t1
, t2
, !!(strict
& COMPARE_REDECLARATION
)))
1301 case TEMPLATE_TYPE_PARM
:
1302 /* If T1 and T2 don't have the same relative position in their
1303 template parameters set, they can't be equal. */
1304 if (!comp_template_parms_position (t1
, t2
))
1309 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1
),
1310 TYPENAME_TYPE_FULLNAME (t2
)))
1312 /* Qualifiers don't matter on scopes. */
1313 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1
),
1318 case UNBOUND_CLASS_TEMPLATE
:
1319 if (!cp_tree_equal (TYPE_IDENTIFIER (t1
), TYPE_IDENTIFIER (t2
)))
1321 if (!same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
)))
1326 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1331 if (TYPE_VECTOR_SUBPARTS (t1
) != TYPE_VECTOR_SUBPARTS (t2
)
1332 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1336 case TYPE_PACK_EXPANSION
:
1337 return (same_type_p (PACK_EXPANSION_PATTERN (t1
),
1338 PACK_EXPANSION_PATTERN (t2
))
1339 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1
),
1340 PACK_EXPANSION_EXTRA_ARGS (t2
)));
1343 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1
)
1344 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2
)
1345 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1
)
1346 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2
))
1347 || (DECLTYPE_FOR_LAMBDA_PROXY (t1
)
1348 != DECLTYPE_FOR_LAMBDA_PROXY (t2
))
1349 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1
),
1350 DECLTYPE_TYPE_EXPR (t2
)))
1354 case UNDERLYING_TYPE
:
1355 return same_type_p (UNDERLYING_TYPE_TYPE (t1
),
1356 UNDERLYING_TYPE_TYPE (t2
));
1362 /* If we get here, we know that from a target independent POV the
1363 types are the same. Make sure the target attributes are also
1365 return comp_type_attributes (t1
, t2
);
1368 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1369 is a bitwise-or of the COMPARE_* flags. */
1372 comptypes (tree t1
, tree t2
, int strict
)
1374 if (strict
== COMPARE_STRICT
)
1379 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1382 if (TYPE_STRUCTURAL_EQUALITY_P (t1
) || TYPE_STRUCTURAL_EQUALITY_P (t2
))
1383 /* At least one of the types requires structural equality, so
1384 perform a deep check. */
1385 return structural_comptypes (t1
, t2
, strict
);
1387 #ifdef ENABLE_CHECKING
1388 if (USE_CANONICAL_TYPES
)
1390 bool result
= structural_comptypes (t1
, t2
, strict
);
1392 if (result
&& TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1393 /* The two types are structurally equivalent, but their
1394 canonical types were different. This is a failure of the
1395 canonical type propagation code.*/
1397 ("canonical types differ for identical types %T and %T",
1399 else if (!result
&& TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
))
1400 /* Two types are structurally different, but the canonical
1401 types are the same. This means we were over-eager in
1402 assigning canonical types. */
1404 ("same canonical type node for different types %T and %T",
1410 if (USE_CANONICAL_TYPES
)
1411 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1414 return structural_comptypes (t1
, t2
, strict
);
1416 else if (strict
== COMPARE_STRUCTURAL
)
1417 return structural_comptypes (t1
, t2
, COMPARE_STRICT
);
1419 return structural_comptypes (t1
, t2
, strict
);
1422 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1423 top-level qualifiers. */
1426 same_type_ignoring_top_level_qualifiers_p (tree type1
, tree type2
)
1428 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1431 return same_type_p (TYPE_MAIN_VARIANT (type1
), TYPE_MAIN_VARIANT (type2
));
1434 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1437 at_least_as_qualified_p (const_tree type1
, const_tree type2
)
1439 int q1
= cp_type_quals (type1
);
1440 int q2
= cp_type_quals (type2
);
1442 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1443 return (q1
& q2
) == q2
;
1446 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1447 more cv-qualified that TYPE1, and 0 otherwise. */
1450 comp_cv_qualification (const_tree type1
, const_tree type2
)
1452 int q1
= cp_type_quals (type1
);
1453 int q2
= cp_type_quals (type2
);
1458 if ((q1
& q2
) == q2
)
1460 else if ((q1
& q2
) == q1
)
1466 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1467 subset of the cv-qualification signature of TYPE2, and the types
1468 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1471 comp_cv_qual_signature (tree type1
, tree type2
)
1473 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1475 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1481 /* Subroutines of `comptypes'. */
1483 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1484 equivalent in the sense that functions with those parameter types
1485 can have equivalent types. The two lists must be equivalent,
1486 element by element. */
1489 compparms (const_tree parms1
, const_tree parms2
)
1493 /* An unspecified parmlist matches any specified parmlist
1494 whose argument types don't need default promotions. */
1496 for (t1
= parms1
, t2
= parms2
;
1498 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1500 /* If one parmlist is shorter than the other,
1501 they fail to match. */
1504 if (!same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1511 /* Process a sizeof or alignof expression where the operand is a
1515 cxx_sizeof_or_alignof_type (tree type
, enum tree_code op
, bool complain
)
1520 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
1521 if (type
== error_mark_node
)
1522 return error_mark_node
;
1524 type
= non_reference (type
);
1525 if (TREE_CODE (type
) == METHOD_TYPE
)
1528 pedwarn (input_location
, OPT_Wpointer_arith
,
1529 "invalid application of %qs to a member function",
1530 operator_name_info
[(int) op
].name
);
1531 value
= size_one_node
;
1534 dependent_p
= dependent_type_p (type
);
1536 complete_type (type
);
1538 /* VLA types will have a non-constant size. In the body of an
1539 uninstantiated template, we don't need to try to compute the
1540 value, because the sizeof expression is not an integral
1541 constant expression in that case. And, if we do try to
1542 compute the value, we'll likely end up with SAVE_EXPRs, which
1543 the template substitution machinery does not expect to see. */
1544 || (processing_template_decl
1545 && COMPLETE_TYPE_P (type
)
1546 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
))
1548 value
= build_min (op
, size_type_node
, type
);
1549 TREE_READONLY (value
) = 1;
1553 if (cxx_dialect
>= cxx1y
&& array_of_runtime_bound_p (type
))
1555 if (complain
& tf_warning_or_error
)
1556 pedwarn (input_location
, OPT_Wvla
,
1557 "taking sizeof array of runtime bound");
1559 return error_mark_node
;
1562 return c_sizeof_or_alignof_type (input_location
, complete_type (type
),
1567 /* Return the size of the type, without producing any warnings for
1568 types whose size cannot be taken. This routine should be used only
1569 in some other routine that has already produced a diagnostic about
1570 using the size of such a type. */
1572 cxx_sizeof_nowarn (tree type
)
1574 if (TREE_CODE (type
) == FUNCTION_TYPE
1575 || VOID_TYPE_P (type
)
1576 || TREE_CODE (type
) == ERROR_MARK
)
1577 return size_one_node
;
1578 else if (!COMPLETE_TYPE_P (type
))
1579 return size_zero_node
;
1581 return cxx_sizeof_or_alignof_type (type
, SIZEOF_EXPR
, false);
1584 /* Process a sizeof expression where the operand is an expression. */
1587 cxx_sizeof_expr (tree e
, tsubst_flags_t complain
)
1589 if (e
== error_mark_node
)
1590 return error_mark_node
;
1592 if (processing_template_decl
)
1594 e
= build_min (SIZEOF_EXPR
, size_type_node
, e
);
1595 TREE_SIDE_EFFECTS (e
) = 0;
1596 TREE_READONLY (e
) = 1;
1601 /* To get the size of a static data member declared as an array of
1602 unknown bound, we need to instantiate it. */
1604 && VAR_HAD_UNKNOWN_BOUND (e
)
1605 && DECL_TEMPLATE_INSTANTIATION (e
))
1606 instantiate_decl (e
, /*defer_ok*/true, /*expl_inst_mem*/false);
1608 e
= mark_type_use (e
);
1610 if (TREE_CODE (e
) == COMPONENT_REF
1611 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
1612 && DECL_C_BIT_FIELD (TREE_OPERAND (e
, 1)))
1614 if (complain
& tf_error
)
1615 error ("invalid application of %<sizeof%> to a bit-field");
1617 return error_mark_node
;
1620 else if (is_overloaded_fn (e
))
1622 if (complain
& tf_error
)
1623 permerror (input_location
, "ISO C++ forbids applying %<sizeof%> to an expression of "
1626 return error_mark_node
;
1629 else if (type_unknown_p (e
))
1631 if (complain
& tf_error
)
1632 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1634 return error_mark_node
;
1640 return cxx_sizeof_or_alignof_type (e
, SIZEOF_EXPR
, complain
& tf_error
);
1643 /* Implement the __alignof keyword: Return the minimum required
1644 alignment of E, measured in bytes. For VAR_DECL's and
1645 FIELD_DECL's return DECL_ALIGN (which can be set from an
1646 "aligned" __attribute__ specification). */
1649 cxx_alignof_expr (tree e
, tsubst_flags_t complain
)
1653 if (e
== error_mark_node
)
1654 return error_mark_node
;
1656 if (processing_template_decl
)
1658 e
= build_min (ALIGNOF_EXPR
, size_type_node
, e
);
1659 TREE_SIDE_EFFECTS (e
) = 0;
1660 TREE_READONLY (e
) = 1;
1665 e
= mark_type_use (e
);
1668 t
= size_int (DECL_ALIGN_UNIT (e
));
1669 else if (TREE_CODE (e
) == COMPONENT_REF
1670 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
1671 && DECL_C_BIT_FIELD (TREE_OPERAND (e
, 1)))
1673 if (complain
& tf_error
)
1674 error ("invalid application of %<__alignof%> to a bit-field");
1676 return error_mark_node
;
1679 else if (TREE_CODE (e
) == COMPONENT_REF
1680 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
)
1681 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e
, 1)));
1682 else if (is_overloaded_fn (e
))
1684 if (complain
& tf_error
)
1685 permerror (input_location
, "ISO C++ forbids applying %<__alignof%> to an expression of "
1688 return error_mark_node
;
1689 if (TREE_CODE (e
) == FUNCTION_DECL
)
1690 t
= size_int (DECL_ALIGN_UNIT (e
));
1694 else if (type_unknown_p (e
))
1696 if (complain
& tf_error
)
1697 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1699 return error_mark_node
;
1703 return cxx_sizeof_or_alignof_type (TREE_TYPE (e
), ALIGNOF_EXPR
,
1704 complain
& tf_error
);
1706 return fold_convert (size_type_node
, t
);
1709 /* Process a sizeof or alignof expression E with code OP where the operand
1710 is an expression. */
1713 cxx_sizeof_or_alignof_expr (tree e
, enum tree_code op
, bool complain
)
1715 if (op
== SIZEOF_EXPR
)
1716 return cxx_sizeof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1718 return cxx_alignof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1721 /* Build a representation of an expression 'alignas(E).' Return the
1722 folded integer value of E if it is an integral constant expression
1723 that resolves to a valid alignment. If E depends on a template
1724 parameter, return a syntactic representation tree of kind
1725 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1726 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1729 cxx_alignas_expr (tree e
)
1731 if (e
== NULL_TREE
|| e
== error_mark_node
1732 || (!TYPE_P (e
) && !require_potential_rvalue_constant_expression (e
)))
1738 When the alignment-specifier is of the form
1739 alignas(type-id ), it shall have the same effect as
1740 alignas(alignof(type-id )). */
1742 return cxx_sizeof_or_alignof_type (e
, ALIGNOF_EXPR
, false);
1744 /* If we reach this point, it means the alignas expression if of
1745 the form "alignas(assignment-expression)", so we should follow
1746 what is stated by [dcl.align]/2. */
1748 if (value_dependent_expression_p (e
))
1749 /* Leave value-dependent expression alone for now. */
1752 e
= fold_non_dependent_expr (e
);
1753 e
= mark_rvalue_use (e
);
1755 /* [dcl.align]/2 says:
1757 the assignment-expression shall be an integral constant
1760 return cxx_constant_value (e
);
1764 /* EXPR is being used in a context that is not a function call.
1769 The expression can be used only as the left-hand operand of a
1770 member function call.
1772 [expr.mptr.operator]
1774 If the result of .* or ->* is a function, then that result can be
1775 used only as the operand for the function call operator ().
1777 by issuing an error message if appropriate. Returns true iff EXPR
1778 violates these rules. */
1781 invalid_nonstatic_memfn_p (tree expr
, tsubst_flags_t complain
)
1783 if (expr
== NULL_TREE
)
1785 /* Don't enforce this in MS mode. */
1786 if (flag_ms_extensions
)
1788 if (is_overloaded_fn (expr
) && !really_overloaded_fn (expr
))
1789 expr
= get_first_fn (expr
);
1790 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr
))
1792 if (complain
& tf_error
)
1793 error ("invalid use of non-static member function");
1799 /* If EXP is a reference to a bitfield, and the type of EXP does not
1800 match the declared type of the bitfield, return the declared type
1801 of the bitfield. Otherwise, return NULL_TREE. */
1804 is_bitfield_expr_with_lowered_type (const_tree exp
)
1806 switch (TREE_CODE (exp
))
1809 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1)
1810 ? TREE_OPERAND (exp
, 1)
1811 : TREE_OPERAND (exp
, 0)))
1813 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 2));
1816 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1));
1820 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1826 field
= TREE_OPERAND (exp
, 1);
1827 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_BIT_FIELD_TYPE (field
))
1829 if (same_type_ignoring_top_level_qualifiers_p
1830 (TREE_TYPE (exp
), DECL_BIT_FIELD_TYPE (field
)))
1832 return DECL_BIT_FIELD_TYPE (field
);
1836 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp
, 0)))
1837 == TYPE_MAIN_VARIANT (TREE_TYPE (exp
)))
1838 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1846 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1847 bitfield with a lowered type, the type of EXP is returned, rather
1851 unlowered_expr_type (const_tree exp
)
1854 tree etype
= TREE_TYPE (exp
);
1856 type
= is_bitfield_expr_with_lowered_type (exp
);
1858 type
= cp_build_qualified_type (type
, cp_type_quals (etype
));
1865 /* Perform the conversions in [expr] that apply when an lvalue appears
1866 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1867 function-to-pointer conversions. In addition, manifest constants
1868 are replaced by their values, and bitfield references are converted
1869 to their declared types. Note that this function does not perform the
1870 lvalue-to-rvalue conversion for class types. If you need that conversion
1871 to for class types, then you probably need to use force_rvalue.
1873 Although the returned value is being used as an rvalue, this
1874 function does not wrap the returned expression in a
1875 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1876 that the return value is no longer an lvalue. */
1879 decay_conversion (tree exp
, tsubst_flags_t complain
)
1882 enum tree_code code
;
1883 location_t loc
= EXPR_LOC_OR_HERE (exp
);
1885 type
= TREE_TYPE (exp
);
1886 if (type
== error_mark_node
)
1887 return error_mark_node
;
1889 exp
= mark_rvalue_use (exp
);
1891 exp
= resolve_nondeduced_context (exp
);
1892 if (type_unknown_p (exp
))
1894 if (complain
& tf_error
)
1895 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
1896 return error_mark_node
;
1899 code
= TREE_CODE (type
);
1901 /* For an array decl decay_conversion should not try to return its
1903 if (code
!= ARRAY_TYPE
)
1905 /* FIXME remove? at least need to remember that this isn't really a
1906 constant expression if EXP isn't decl_constant_var_p, like with
1907 C_MAYBE_CONST_EXPR. */
1908 exp
= decl_constant_value_safe (exp
);
1909 if (error_operand_p (exp
))
1910 return error_mark_node
;
1913 if (NULLPTR_TYPE_P (type
) && !TREE_SIDE_EFFECTS (exp
))
1914 return nullptr_node
;
1916 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1917 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1918 if (code
== VOID_TYPE
)
1920 if (complain
& tf_error
)
1921 error_at (loc
, "void value not ignored as it ought to be");
1922 return error_mark_node
;
1924 if (invalid_nonstatic_memfn_p (exp
, complain
))
1925 return error_mark_node
;
1926 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
1927 return cp_build_addr_expr (exp
, complain
);
1928 if (code
== ARRAY_TYPE
)
1933 if (INDIRECT_REF_P (exp
))
1934 return build_nop (build_pointer_type (TREE_TYPE (type
)),
1935 TREE_OPERAND (exp
, 0));
1937 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1939 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1), complain
);
1940 if (op1
== error_mark_node
)
1941 return error_mark_node
;
1942 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
1943 TREE_OPERAND (exp
, 0), op1
);
1947 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
1949 if (complain
& tf_error
)
1950 error_at (loc
, "invalid use of non-lvalue array");
1951 return error_mark_node
;
1954 /* Don't let an array compound literal decay to a pointer. It can
1955 still be used to initialize an array or bind to a reference. */
1956 if (TREE_CODE (exp
) == TARGET_EXPR
)
1958 if (complain
& tf_error
)
1959 error_at (loc
, "taking address of temporary array");
1960 return error_mark_node
;
1963 ptrtype
= build_pointer_type (TREE_TYPE (type
));
1967 if (!cxx_mark_addressable (exp
))
1968 return error_mark_node
;
1969 adr
= build_nop (ptrtype
, build_address (exp
));
1972 /* This way is better for a COMPONENT_REF since it can
1973 simplify the offset for a component. */
1974 adr
= cp_build_addr_expr (exp
, complain
);
1975 return cp_convert (ptrtype
, adr
, complain
);
1978 /* If a bitfield is used in a context where integral promotion
1979 applies, then the caller is expected to have used
1980 default_conversion. That function promotes bitfields correctly
1981 before calling this function. At this point, if we have a
1982 bitfield referenced, we may assume that is not subject to
1983 promotion, and that, therefore, the type of the resulting rvalue
1984 is the declared type of the bitfield. */
1985 exp
= convert_bitfield_to_declared_type (exp
);
1987 /* We do not call rvalue() here because we do not want to wrap EXP
1988 in a NON_LVALUE_EXPR. */
1992 Non-class rvalues always have cv-unqualified types. */
1993 type
= TREE_TYPE (exp
);
1994 if (!CLASS_TYPE_P (type
) && cv_qualified_p (type
))
1995 exp
= build_nop (cv_unqualified (type
), exp
);
2000 /* Perform preparatory conversions, as part of the "usual arithmetic
2001 conversions". In particular, as per [expr]:
2003 Whenever an lvalue expression appears as an operand of an
2004 operator that expects the rvalue for that operand, the
2005 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2006 standard conversions are applied to convert the expression to an
2009 In addition, we perform integral promotions here, as those are
2010 applied to both operands to a binary operator before determining
2011 what additional conversions should apply. */
2014 cp_default_conversion (tree exp
, tsubst_flags_t complain
)
2016 /* Check for target-specific promotions. */
2017 tree promoted_type
= targetm
.promoted_type (TREE_TYPE (exp
));
2019 exp
= cp_convert (promoted_type
, exp
, complain
);
2020 /* Perform the integral promotions first so that bitfield
2021 expressions (which may promote to "int", even if the bitfield is
2022 declared "unsigned") are promoted correctly. */
2023 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp
)))
2024 exp
= cp_perform_integral_promotions (exp
, complain
);
2025 /* Perform the other conversions. */
2026 exp
= decay_conversion (exp
, complain
);
2034 default_conversion (tree exp
)
2036 return cp_default_conversion (exp
, tf_warning_or_error
);
2039 /* EXPR is an expression with an integral or enumeration type.
2040 Perform the integral promotions in [conv.prom], and return the
2044 cp_perform_integral_promotions (tree expr
, tsubst_flags_t complain
)
2049 expr
= mark_rvalue_use (expr
);
2053 If the bitfield has an enumerated type, it is treated as any
2054 other value of that type for promotion purposes. */
2055 type
= is_bitfield_expr_with_lowered_type (expr
);
2056 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
2057 type
= TREE_TYPE (expr
);
2058 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
2059 /* Scoped enums don't promote. */
2060 if (SCOPED_ENUM_P (type
))
2062 promoted_type
= type_promotes_to (type
);
2063 if (type
!= promoted_type
)
2064 expr
= cp_convert (promoted_type
, expr
, complain
);
2071 perform_integral_promotions (tree expr
)
2073 return cp_perform_integral_promotions (expr
, tf_warning_or_error
);
2076 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2077 decay_conversion to one. */
2080 string_conv_p (const_tree totype
, const_tree exp
, int warn
)
2084 if (!TYPE_PTR_P (totype
))
2087 t
= TREE_TYPE (totype
);
2088 if (!same_type_p (t
, char_type_node
)
2089 && !same_type_p (t
, char16_type_node
)
2090 && !same_type_p (t
, char32_type_node
)
2091 && !same_type_p (t
, wchar_type_node
))
2094 if (TREE_CODE (exp
) == STRING_CST
)
2096 /* Make sure that we don't try to convert between char and wide chars. */
2097 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
2102 /* Is this a string constant which has decayed to 'const char *'? */
2103 t
= build_pointer_type (cp_build_qualified_type (t
, TYPE_QUAL_CONST
));
2104 if (!same_type_p (TREE_TYPE (exp
), t
))
2107 if (TREE_CODE (exp
) != ADDR_EXPR
2108 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
2112 /* This warning is not very useful, as it complains about printf. */
2114 warning (OPT_Wwrite_strings
,
2115 "deprecated conversion from string constant to %qT",
2121 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2122 can, for example, use as an lvalue. This code used to be in
2123 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2124 expressions, where we're dealing with aggregates. But now it's again only
2125 called from unary_complex_lvalue. The case (in particular) that led to
2126 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2130 rationalize_conditional_expr (enum tree_code code
, tree t
,
2131 tsubst_flags_t complain
)
2133 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2134 the first operand is always the one to be used if both operands
2135 are equal, so we know what conditional expression this used to be. */
2136 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
2138 tree op0
= TREE_OPERAND (t
, 0);
2139 tree op1
= TREE_OPERAND (t
, 1);
2141 /* The following code is incorrect if either operand side-effects. */
2142 gcc_assert (!TREE_SIDE_EFFECTS (op0
)
2143 && !TREE_SIDE_EFFECTS (op1
));
2145 build_conditional_expr (EXPR_LOC_OR_HERE (t
),
2146 build_x_binary_op (EXPR_LOC_OR_HERE (t
),
2147 (TREE_CODE (t
) == MIN_EXPR
2148 ? LE_EXPR
: GE_EXPR
),
2149 op0
, TREE_CODE (op0
),
2150 op1
, TREE_CODE (op1
),
2153 cp_build_unary_op (code
, op0
, 0, complain
),
2154 cp_build_unary_op (code
, op1
, 0, complain
),
2159 build_conditional_expr (EXPR_LOC_OR_HERE (t
), TREE_OPERAND (t
, 0),
2160 cp_build_unary_op (code
, TREE_OPERAND (t
, 1), 0,
2162 cp_build_unary_op (code
, TREE_OPERAND (t
, 2), 0,
2167 /* Given the TYPE of an anonymous union field inside T, return the
2168 FIELD_DECL for the field. If not found return NULL_TREE. Because
2169 anonymous unions can nest, we must also search all anonymous unions
2170 that are directly reachable. */
2173 lookup_anon_field (tree t
, tree type
)
2177 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
2179 if (TREE_STATIC (field
))
2181 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
2184 /* If we find it directly, return the field. */
2185 if (DECL_NAME (field
) == NULL_TREE
2186 && type
== TYPE_MAIN_VARIANT (TREE_TYPE (field
)))
2191 /* Otherwise, it could be nested, search harder. */
2192 if (DECL_NAME (field
) == NULL_TREE
2193 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2195 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
2203 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2204 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2205 non-NULL, it indicates the path to the base used to name MEMBER.
2206 If PRESERVE_REFERENCE is true, the expression returned will have
2207 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2208 returned will have the type referred to by the reference.
2210 This function does not perform access control; that is either done
2211 earlier by the parser when the name of MEMBER is resolved to MEMBER
2212 itself, or later when overload resolution selects one of the
2213 functions indicated by MEMBER. */
2216 build_class_member_access_expr (tree object
, tree member
,
2217 tree access_path
, bool preserve_reference
,
2218 tsubst_flags_t complain
)
2222 tree result
= NULL_TREE
;
2223 tree using_decl
= NULL_TREE
;
2225 if (error_operand_p (object
) || error_operand_p (member
))
2226 return error_mark_node
;
2228 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2232 The type of the first expression shall be "class object" (of a
2234 object_type
= TREE_TYPE (object
);
2235 if (!currently_open_class (object_type
)
2236 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2237 return error_mark_node
;
2238 if (!CLASS_TYPE_P (object_type
))
2240 if (complain
& tf_error
)
2242 if (POINTER_TYPE_P (object_type
)
2243 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2244 error ("request for member %qD in %qE, which is of pointer "
2245 "type %qT (maybe you meant to use %<->%> ?)",
2246 member
, object
, object_type
);
2248 error ("request for member %qD in %qE, which is of non-class "
2249 "type %qT", member
, object
, object_type
);
2251 return error_mark_node
;
2254 /* The standard does not seem to actually say that MEMBER must be a
2255 member of OBJECT_TYPE. However, that is clearly what is
2257 if (DECL_P (member
))
2259 member_scope
= DECL_CLASS_CONTEXT (member
);
2261 if (TREE_DEPRECATED (member
))
2262 warn_deprecated_use (member
, NULL_TREE
);
2265 member_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (member
));
2266 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2267 presently be the anonymous union. Go outwards until we find a
2268 type related to OBJECT_TYPE. */
2269 while ((ANON_AGGR_TYPE_P (member_scope
) || UNSCOPED_ENUM_P (member_scope
))
2270 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
2272 member_scope
= TYPE_CONTEXT (member_scope
);
2273 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
2275 if (complain
& tf_error
)
2277 if (TREE_CODE (member
) == FIELD_DECL
)
2278 error ("invalid use of nonstatic data member %qE", member
);
2280 error ("%qD is not a member of %qT", member
, object_type
);
2282 return error_mark_node
;
2285 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2286 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2287 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2289 tree temp
= unary_complex_lvalue (ADDR_EXPR
, object
);
2291 object
= cp_build_indirect_ref (temp
, RO_NULL
, complain
);
2294 /* In [expr.ref], there is an explicit list of the valid choices for
2295 MEMBER. We check for each of those cases here. */
2298 /* A static data member. */
2300 mark_exp_read (object
);
2301 /* If OBJECT has side-effects, they are supposed to occur. */
2302 if (TREE_SIDE_EFFECTS (object
))
2303 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
2305 else if (TREE_CODE (member
) == FIELD_DECL
)
2307 /* A non-static data member. */
2312 null_object_p
= (INDIRECT_REF_P (object
)
2313 && integer_zerop (TREE_OPERAND (object
, 0)));
2315 /* Convert OBJECT to the type of MEMBER. */
2316 if (!same_type_p (TYPE_MAIN_VARIANT (object_type
),
2317 TYPE_MAIN_VARIANT (member_scope
)))
2322 binfo
= lookup_base (access_path
? access_path
: object_type
,
2323 member_scope
, ba_unique
, &kind
, complain
);
2324 if (binfo
== error_mark_node
)
2325 return error_mark_node
;
2327 /* It is invalid to try to get to a virtual base of a
2328 NULL object. The most common cause is invalid use of
2330 if (null_object_p
&& kind
== bk_via_virtual
)
2332 if (complain
& tf_error
)
2334 error ("invalid access to non-static data member %qD of "
2337 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2339 return error_mark_node
;
2342 /* Convert to the base. */
2343 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
2344 /*nonnull=*/1, complain
);
2345 /* If we found the base successfully then we should be able
2346 to convert to it successfully. */
2347 gcc_assert (object
!= error_mark_node
);
2350 /* Complain about other invalid uses of offsetof, even though they will
2351 give the right answer. Note that we complain whether or not they
2352 actually used the offsetof macro, since there's no way to know at this
2353 point. So we just give a warning, instead of a pedwarn. */
2354 /* Do not produce this warning for base class field references, because
2355 we know for a fact that didn't come from offsetof. This does occur
2356 in various testsuite cases where a null object is passed where a
2357 vtable access is required. */
2358 if (null_object_p
&& warn_invalid_offsetof
2359 && CLASSTYPE_NON_STD_LAYOUT (object_type
)
2360 && !DECL_FIELD_IS_BASE (member
)
2361 && cp_unevaluated_operand
== 0
2362 && (complain
& tf_warning
))
2364 warning (OPT_Winvalid_offsetof
,
2365 "invalid access to non-static data member %qD "
2366 " of NULL object", member
);
2367 warning (OPT_Winvalid_offsetof
,
2368 "(perhaps the %<offsetof%> macro was used incorrectly)");
2371 /* If MEMBER is from an anonymous aggregate, we have converted
2372 OBJECT so that it refers to the class containing the
2373 anonymous union. Generate a reference to the anonymous union
2374 itself, and recur to find MEMBER. */
2375 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
))
2376 /* When this code is called from build_field_call, the
2377 object already has the type of the anonymous union.
2378 That is because the COMPONENT_REF was already
2379 constructed, and was then disassembled before calling
2380 build_field_call. After the function-call code is
2381 cleaned up, this waste can be eliminated. */
2382 && (!same_type_ignoring_top_level_qualifiers_p
2383 (TREE_TYPE (object
), DECL_CONTEXT (member
))))
2385 tree anonymous_union
;
2387 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
2388 DECL_CONTEXT (member
));
2389 object
= build_class_member_access_expr (object
,
2391 /*access_path=*/NULL_TREE
,
2396 /* Compute the type of the field, as described in [expr.ref]. */
2397 type_quals
= TYPE_UNQUALIFIED
;
2398 member_type
= TREE_TYPE (member
);
2399 if (TREE_CODE (member_type
) != REFERENCE_TYPE
)
2401 type_quals
= (cp_type_quals (member_type
)
2402 | cp_type_quals (object_type
));
2404 /* A field is const (volatile) if the enclosing object, or the
2405 field itself, is const (volatile). But, a mutable field is
2406 not const, even within a const object. */
2407 if (DECL_MUTABLE_P (member
))
2408 type_quals
&= ~TYPE_QUAL_CONST
;
2409 member_type
= cp_build_qualified_type (member_type
, type_quals
);
2412 result
= build3 (COMPONENT_REF
, member_type
, object
, member
,
2414 result
= fold_if_not_in_template (result
);
2416 /* Mark the expression const or volatile, as appropriate. Even
2417 though we've dealt with the type above, we still have to mark the
2418 expression itself. */
2419 if (type_quals
& TYPE_QUAL_CONST
)
2420 TREE_READONLY (result
) = 1;
2421 if (type_quals
& TYPE_QUAL_VOLATILE
)
2422 TREE_THIS_VOLATILE (result
) = 1;
2424 else if (BASELINK_P (member
))
2426 /* The member is a (possibly overloaded) member function. */
2430 /* If the MEMBER is exactly one static member function, then we
2431 know the type of the expression. Otherwise, we must wait
2432 until overload resolution has been performed. */
2433 functions
= BASELINK_FUNCTIONS (member
);
2434 if (TREE_CODE (functions
) == FUNCTION_DECL
2435 && DECL_STATIC_FUNCTION_P (functions
))
2436 type
= TREE_TYPE (functions
);
2438 type
= unknown_type_node
;
2439 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2440 base. That will happen when the function is called. */
2441 result
= build3 (COMPONENT_REF
, type
, object
, member
, NULL_TREE
);
2443 else if (TREE_CODE (member
) == CONST_DECL
)
2445 /* The member is an enumerator. */
2447 /* If OBJECT has side-effects, they are supposed to occur. */
2448 if (TREE_SIDE_EFFECTS (object
))
2449 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
2452 else if ((using_decl
= strip_using_decl (member
)) != member
)
2453 result
= build_class_member_access_expr (object
,
2455 access_path
, preserve_reference
,
2459 if (complain
& tf_error
)
2460 error ("invalid use of %qD", member
);
2461 return error_mark_node
;
2464 if (!preserve_reference
)
2467 If E2 is declared to have type "reference to T", then ... the
2468 type of E1.E2 is T. */
2469 result
= convert_from_reference (result
);
2474 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2475 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2478 lookup_destructor (tree object
, tree scope
, tree dtor_name
,
2479 tsubst_flags_t complain
)
2481 tree object_type
= TREE_TYPE (object
);
2482 tree dtor_type
= TREE_OPERAND (dtor_name
, 0);
2485 if (scope
&& !check_dtor_name (scope
, dtor_type
))
2487 if (complain
& tf_error
)
2488 error ("qualified type %qT does not match destructor name ~%qT",
2490 return error_mark_node
;
2492 if (is_auto (dtor_type
))
2493 dtor_type
= object_type
;
2494 else if (identifier_p (dtor_type
))
2496 /* In a template, names we can't find a match for are still accepted
2497 destructor names, and we check them here. */
2498 if (check_dtor_name (object_type
, dtor_type
))
2499 dtor_type
= object_type
;
2502 if (complain
& tf_error
)
2503 error ("object type %qT does not match destructor name ~%qT",
2504 object_type
, dtor_type
);
2505 return error_mark_node
;
2509 else if (!DERIVED_FROM_P (dtor_type
, TYPE_MAIN_VARIANT (object_type
)))
2511 if (complain
& tf_error
)
2512 error ("the type being destroyed is %qT, but the destructor "
2513 "refers to %qT", TYPE_MAIN_VARIANT (object_type
), dtor_type
);
2514 return error_mark_node
;
2516 expr
= lookup_member (dtor_type
, complete_dtor_identifier
,
2517 /*protect=*/1, /*want_type=*/false,
2518 tf_warning_or_error
);
2519 expr
= (adjust_result_of_qualified_name_lookup
2520 (expr
, dtor_type
, object_type
));
2521 if (scope
== NULL_TREE
)
2522 /* We need to call adjust_result_of_qualified_name_lookup in case the
2523 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2524 that we still get virtual function binding. */
2525 BASELINK_QUALIFIED_P (expr
) = false;
2529 /* An expression of the form "A::template B" has been resolved to
2530 DECL. Issue a diagnostic if B is not a template or template
2534 check_template_keyword (tree decl
)
2536 /* The standard says:
2540 If a name prefixed by the keyword template is not a member
2541 template, the program is ill-formed.
2543 DR 228 removed the restriction that the template be a member
2546 DR 96, if accepted would add the further restriction that explicit
2547 template arguments must be provided if the template keyword is
2548 used, but, as of 2005-10-16, that DR is still in "drafting". If
2549 this DR is accepted, then the semantic checks here can be
2550 simplified, as the entity named must in fact be a template
2551 specialization, rather than, as at present, a set of overloaded
2552 functions containing at least one template function. */
2553 if (TREE_CODE (decl
) != TEMPLATE_DECL
2554 && TREE_CODE (decl
) != TEMPLATE_ID_EXPR
)
2556 if (!is_overloaded_fn (decl
))
2557 permerror (input_location
, "%qD is not a template", decl
);
2562 if (BASELINK_P (fns
))
2563 fns
= BASELINK_FUNCTIONS (fns
);
2566 tree fn
= OVL_CURRENT (fns
);
2567 if (TREE_CODE (fn
) == TEMPLATE_DECL
2568 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
2570 if (TREE_CODE (fn
) == FUNCTION_DECL
2571 && DECL_USE_TEMPLATE (fn
)
2572 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn
)))
2574 fns
= OVL_NEXT (fns
);
2577 permerror (input_location
, "%qD is not a template", decl
);
2582 /* This function is called by the parser to process a class member
2583 access expression of the form OBJECT.NAME. NAME is a node used by
2584 the parser to represent a name; it is not yet a DECL. It may,
2585 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2586 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2587 there is no reason to do the lookup twice, so the parser keeps the
2588 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2589 be a template via the use of the "A::template B" syntax. */
2592 finish_class_member_access_expr (tree object
, tree name
, bool template_p
,
2593 tsubst_flags_t complain
)
2598 tree access_path
= NULL_TREE
;
2599 tree orig_object
= object
;
2600 tree orig_name
= name
;
2602 if (object
== error_mark_node
|| name
== error_mark_node
)
2603 return error_mark_node
;
2605 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2606 if (!objc_is_public (object
, name
))
2607 return error_mark_node
;
2609 object_type
= TREE_TYPE (object
);
2611 if (processing_template_decl
)
2613 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2614 dependent_type_p (object_type
)
2615 /* If NAME is just an IDENTIFIER_NODE, then the expression
2617 || identifier_p (object
)
2618 /* If NAME is "f<args>", where either 'f' or 'args' is
2619 dependent, then the expression is dependent. */
2620 || (TREE_CODE (name
) == TEMPLATE_ID_EXPR
2621 && dependent_template_id_p (TREE_OPERAND (name
, 0),
2622 TREE_OPERAND (name
, 1)))
2623 /* If NAME is "T::X" where "T" is dependent, then the
2624 expression is dependent. */
2625 || (TREE_CODE (name
) == SCOPE_REF
2626 && TYPE_P (TREE_OPERAND (name
, 0))
2627 && dependent_type_p (TREE_OPERAND (name
, 0))))
2628 return build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
,
2629 object
, name
, NULL_TREE
);
2630 object
= build_non_dependent_expr (object
);
2632 else if (c_dialect_objc ()
2633 && identifier_p (name
)
2634 && (expr
= objc_maybe_build_component_ref (object
, name
)))
2639 The type of the first expression shall be "class object" (of a
2641 if (!currently_open_class (object_type
)
2642 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2643 return error_mark_node
;
2644 if (!CLASS_TYPE_P (object_type
))
2646 if (complain
& tf_error
)
2648 if (POINTER_TYPE_P (object_type
)
2649 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2650 error ("request for member %qD in %qE, which is of pointer "
2651 "type %qT (maybe you meant to use %<->%> ?)",
2652 name
, object
, object_type
);
2654 error ("request for member %qD in %qE, which is of non-class "
2655 "type %qT", name
, object
, object_type
);
2657 return error_mark_node
;
2660 if (BASELINK_P (name
))
2661 /* A member function that has already been looked up. */
2665 bool is_template_id
= false;
2666 tree template_args
= NULL_TREE
;
2669 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
2671 is_template_id
= true;
2672 template_args
= TREE_OPERAND (name
, 1);
2673 name
= TREE_OPERAND (name
, 0);
2675 if (TREE_CODE (name
) == OVERLOAD
)
2676 name
= DECL_NAME (get_first_fn (name
));
2677 else if (DECL_P (name
))
2678 name
= DECL_NAME (name
);
2681 if (TREE_CODE (name
) == SCOPE_REF
)
2683 /* A qualified name. The qualifying class or namespace `S'
2684 has already been looked up; it is either a TYPE or a
2686 scope
= TREE_OPERAND (name
, 0);
2687 name
= TREE_OPERAND (name
, 1);
2689 /* If SCOPE is a namespace, then the qualified name does not
2690 name a member of OBJECT_TYPE. */
2691 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
2693 if (complain
& tf_error
)
2694 error ("%<%D::%D%> is not a member of %qT",
2695 scope
, name
, object_type
);
2696 return error_mark_node
;
2699 if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
2701 /* Looking up a member enumerator (c++/56793). */
2702 if (!TYPE_CLASS_SCOPE_P (scope
)
2703 || !DERIVED_FROM_P (TYPE_CONTEXT (scope
), object_type
))
2705 if (complain
& tf_error
)
2706 error ("%<%D::%D%> is not a member of %qT",
2707 scope
, name
, object_type
);
2708 return error_mark_node
;
2710 tree val
= lookup_enumerator (scope
, name
);
2711 if (TREE_SIDE_EFFECTS (object
))
2712 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
), object
, val
);
2716 gcc_assert (CLASS_TYPE_P (scope
));
2717 gcc_assert (identifier_p (name
) || TREE_CODE (name
) == BIT_NOT_EXPR
);
2719 if (constructor_name_p (name
, scope
))
2721 if (complain
& tf_error
)
2722 error ("cannot call constructor %<%T::%D%> directly",
2724 return error_mark_node
;
2727 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2728 access_path
= lookup_base (object_type
, scope
, ba_check
,
2730 if (access_path
== error_mark_node
)
2731 return error_mark_node
;
2734 if (complain
& tf_error
)
2735 error ("%qT is not a base of %qT", scope
, object_type
);
2736 return error_mark_node
;
2742 access_path
= object_type
;
2745 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2746 member
= lookup_destructor (object
, scope
, name
, complain
);
2749 /* Look up the member. */
2750 member
= lookup_member (access_path
, name
, /*protect=*/1,
2751 /*want_type=*/false, complain
);
2752 if (member
== NULL_TREE
)
2754 if (complain
& tf_error
)
2755 error ("%qD has no member named %qE",
2756 TREE_CODE (access_path
) == TREE_BINFO
2757 ? TREE_TYPE (access_path
) : object_type
, name
);
2758 return error_mark_node
;
2760 if (member
== error_mark_node
)
2761 return error_mark_node
;
2766 tree templ
= member
;
2768 if (BASELINK_P (templ
))
2769 templ
= lookup_template_function (templ
, template_args
);
2772 if (complain
& tf_error
)
2773 error ("%qD is not a member template function", name
);
2774 return error_mark_node
;
2779 if (TREE_DEPRECATED (member
))
2780 warn_deprecated_use (member
, NULL_TREE
);
2783 check_template_keyword (member
);
2785 expr
= build_class_member_access_expr (object
, member
, access_path
,
2786 /*preserve_reference=*/false,
2788 if (processing_template_decl
&& expr
!= error_mark_node
)
2790 if (BASELINK_P (member
))
2792 if (TREE_CODE (orig_name
) == SCOPE_REF
)
2793 BASELINK_QUALIFIED_P (member
) = 1;
2796 return build_min_non_dep (COMPONENT_REF
, expr
,
2797 orig_object
, orig_name
,
2804 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2808 build_simple_component_ref (tree object
, tree member
)
2810 tree type
= cp_build_qualified_type (TREE_TYPE (member
),
2811 cp_type_quals (TREE_TYPE (object
)));
2812 return fold_build3_loc (input_location
,
2813 COMPONENT_REF
, type
,
2814 object
, member
, NULL_TREE
);
2817 /* Return an expression for the MEMBER_NAME field in the internal
2818 representation of PTRMEM, a pointer-to-member function. (Each
2819 pointer-to-member function type gets its own RECORD_TYPE so it is
2820 more convenient to access the fields by name than by FIELD_DECL.)
2821 This routine converts the NAME to a FIELD_DECL and then creates the
2822 node for the complete expression. */
2825 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
2830 /* This code is a stripped down version of
2831 build_class_member_access_expr. It does not work to use that
2832 routine directly because it expects the object to be of class
2834 ptrmem_type
= TREE_TYPE (ptrmem
);
2835 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
));
2836 member
= lookup_member (ptrmem_type
, member_name
, /*protect=*/0,
2837 /*want_type=*/false, tf_warning_or_error
);
2838 return build_simple_component_ref (ptrmem
, member
);
2841 /* Given an expression PTR for a pointer, return an expression
2842 for the value pointed to.
2843 ERRORSTRING is the name of the operator to appear in error messages.
2845 This function may need to overload OPERATOR_FNNAME.
2846 Must also handle REFERENCE_TYPEs for C++. */
2849 build_x_indirect_ref (location_t loc
, tree expr
, ref_operator errorstring
,
2850 tsubst_flags_t complain
)
2852 tree orig_expr
= expr
;
2855 if (processing_template_decl
)
2857 /* Retain the type if we know the operand is a pointer. */
2858 if (TREE_TYPE (expr
) && POINTER_TYPE_P (TREE_TYPE (expr
)))
2859 return build_min (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
2860 if (type_dependent_expression_p (expr
))
2861 return build_min_nt_loc (loc
, INDIRECT_REF
, expr
);
2862 expr
= build_non_dependent_expr (expr
);
2865 rval
= build_new_op (loc
, INDIRECT_REF
, LOOKUP_NORMAL
, expr
,
2866 NULL_TREE
, NULL_TREE
, /*overload=*/NULL
, complain
);
2868 rval
= cp_build_indirect_ref (expr
, errorstring
, complain
);
2870 if (processing_template_decl
&& rval
!= error_mark_node
)
2871 return build_min_non_dep (INDIRECT_REF
, rval
, orig_expr
);
2876 /* Helper function called from c-common. */
2878 build_indirect_ref (location_t
/*loc*/,
2879 tree ptr
, ref_operator errorstring
)
2881 return cp_build_indirect_ref (ptr
, errorstring
, tf_warning_or_error
);
2885 cp_build_indirect_ref (tree ptr
, ref_operator errorstring
,
2886 tsubst_flags_t complain
)
2890 if (ptr
== current_class_ptr
2891 || (TREE_CODE (ptr
) == NOP_EXPR
2892 && TREE_OPERAND (ptr
, 0) == current_class_ptr
2893 && (same_type_ignoring_top_level_qualifiers_p
2894 (TREE_TYPE (ptr
), TREE_TYPE (current_class_ptr
)))))
2895 return current_class_ref
;
2897 pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
2898 ? ptr
: decay_conversion (ptr
, complain
));
2899 if (pointer
== error_mark_node
)
2900 return error_mark_node
;
2902 type
= TREE_TYPE (pointer
);
2904 if (POINTER_TYPE_P (type
))
2908 If the type of the expression is "pointer to T," the type
2909 of the result is "T." */
2910 tree t
= TREE_TYPE (type
);
2912 if (CONVERT_EXPR_P (ptr
)
2913 || TREE_CODE (ptr
) == VIEW_CONVERT_EXPR
)
2915 /* If a warning is issued, mark it to avoid duplicates from
2916 the backend. This only needs to be done at
2917 warn_strict_aliasing > 2. */
2918 if (warn_strict_aliasing
> 2)
2919 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr
, 0)),
2920 type
, TREE_OPERAND (ptr
, 0)))
2921 TREE_NO_WARNING (ptr
) = 1;
2924 if (VOID_TYPE_P (t
))
2926 /* A pointer to incomplete type (other than cv void) can be
2927 dereferenced [expr.unary.op]/1 */
2928 if (complain
& tf_error
)
2929 error ("%qT is not a pointer-to-object type", type
);
2930 return error_mark_node
;
2932 else if (TREE_CODE (pointer
) == ADDR_EXPR
2933 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
2934 /* The POINTER was something like `&x'. We simplify `*&x' to
2936 return TREE_OPERAND (pointer
, 0);
2939 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
2941 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2942 so that we get the proper error message if the result is used
2943 to assign to. Also, &* is supposed to be a no-op. */
2944 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
2945 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
2946 TREE_SIDE_EFFECTS (ref
)
2947 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
));
2951 else if (!(complain
& tf_error
))
2952 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2954 /* `pointer' won't be an error_mark_node if we were given a
2955 pointer to member, so it's cool to check for this here. */
2956 else if (TYPE_PTRMEM_P (type
))
2957 switch (errorstring
)
2959 case RO_ARRAY_INDEXING
:
2960 error ("invalid use of array indexing on pointer to member");
2963 error ("invalid use of unary %<*%> on pointer to member");
2965 case RO_IMPLICIT_CONVERSION
:
2966 error ("invalid use of implicit conversion on pointer to member");
2969 error ("left hand operand of %<->*%> must be a pointer to class, "
2970 "but is a pointer to member of type %qT", type
);
2975 else if (pointer
!= error_mark_node
)
2976 invalid_indirection_error (input_location
, type
, errorstring
);
2978 return error_mark_node
;
2981 /* This handles expressions of the form "a[i]", which denotes
2984 This is logically equivalent in C to *(a+i), but we may do it differently.
2985 If A is a variable or a member, we generate a primitive ARRAY_REF.
2986 This avoids forcing the array out of registers, and can work on
2987 arrays that are not lvalues (for example, members of structures returned
2990 If INDEX is of some user-defined type, it must be converted to
2991 integer type. Otherwise, to make a compatible PLUS_EXPR, it
2992 will inherit the type of the array, which will be some pointer type.
2994 LOC is the location to use in building the array reference. */
2997 cp_build_array_ref (location_t loc
, tree array
, tree idx
,
2998 tsubst_flags_t complain
)
3004 if (complain
& tf_error
)
3005 error_at (loc
, "subscript missing in array reference");
3006 return error_mark_node
;
3009 /* If an array's index is an array notation, then its rank cannot be
3010 greater than one. */
3011 if (flag_enable_cilkplus
&& contains_array_notation_expr (idx
))
3015 /* If find_rank returns false, then it should have reported an error,
3016 thus it is unnecessary for repetition. */
3017 if (!find_rank (loc
, idx
, idx
, true, &rank
))
3018 return error_mark_node
;
3021 error_at (loc
, "rank of the array%'s index is greater than 1");
3022 return error_mark_node
;
3025 if (TREE_TYPE (array
) == error_mark_node
3026 || TREE_TYPE (idx
) == error_mark_node
)
3027 return error_mark_node
;
3029 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3031 switch (TREE_CODE (array
))
3035 tree value
= cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3037 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3038 TREE_OPERAND (array
, 0), value
);
3039 SET_EXPR_LOCATION (ret
, loc
);
3044 ret
= build_conditional_expr
3045 (loc
, TREE_OPERAND (array
, 0),
3046 cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3048 cp_build_array_ref (loc
, TREE_OPERAND (array
, 2), idx
,
3051 protected_set_expr_location (ret
, loc
);
3058 convert_vector_to_pointer_for_subscript (loc
, &array
, idx
);
3060 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
3064 warn_array_subscript_with_type_char (idx
);
3066 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx
)))
3068 if (complain
& tf_error
)
3069 error_at (loc
, "array subscript is not an integer");
3070 return error_mark_node
;
3073 /* Apply integral promotions *after* noticing character types.
3074 (It is unclear why we do these promotions -- the standard
3075 does not say that we should. In fact, the natural thing would
3076 seem to be to convert IDX to ptrdiff_t; we're performing
3077 pointer arithmetic.) */
3078 idx
= cp_perform_integral_promotions (idx
, complain
);
3080 /* An array that is indexed by a non-constant
3081 cannot be stored in a register; we must be able to do
3082 address arithmetic on its address.
3083 Likewise an array of elements of variable size. */
3084 if (TREE_CODE (idx
) != INTEGER_CST
3085 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
3086 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
3089 if (!cxx_mark_addressable (array
))
3090 return error_mark_node
;
3093 /* An array that is indexed by a constant value which is not within
3094 the array bounds cannot be stored in a register either; because we
3095 would get a crash in store_bit_field/extract_bit_field when trying
3096 to access a non-existent part of the register. */
3097 if (TREE_CODE (idx
) == INTEGER_CST
3098 && TYPE_DOMAIN (TREE_TYPE (array
))
3099 && ! int_fits_type_p (idx
, TYPE_DOMAIN (TREE_TYPE (array
))))
3101 if (!cxx_mark_addressable (array
))
3102 return error_mark_node
;
3105 if (!lvalue_p (array
) && (complain
& tf_error
))
3106 pedwarn (loc
, OPT_Wpedantic
,
3107 "ISO C++ forbids subscripting non-lvalue array");
3109 /* Note in C++ it is valid to subscript a `register' array, since
3110 it is valid to take the address of something with that
3111 storage specification. */
3115 while (TREE_CODE (foo
) == COMPONENT_REF
)
3116 foo
= TREE_OPERAND (foo
, 0);
3117 if (VAR_P (foo
) && DECL_REGISTER (foo
)
3118 && (complain
& tf_warning
))
3119 warning_at (loc
, OPT_Wextra
,
3120 "subscripting array declared %<register%>");
3123 type
= TREE_TYPE (TREE_TYPE (array
));
3124 rval
= build4 (ARRAY_REF
, type
, array
, idx
, NULL_TREE
, NULL_TREE
);
3125 /* Array ref is const/volatile if the array elements are
3126 or if the array is.. */
3127 TREE_READONLY (rval
)
3128 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
3129 TREE_SIDE_EFFECTS (rval
)
3130 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
3131 TREE_THIS_VOLATILE (rval
)
3132 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
3133 ret
= require_complete_type_sfinae (fold_if_not_in_template (rval
),
3135 protected_set_expr_location (ret
, loc
);
3140 tree ar
= cp_default_conversion (array
, complain
);
3141 tree ind
= cp_default_conversion (idx
, complain
);
3143 /* Put the integer in IND to simplify error checking. */
3144 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
3151 if (ar
== error_mark_node
|| ind
== error_mark_node
)
3152 return error_mark_node
;
3154 if (!TYPE_PTR_P (TREE_TYPE (ar
)))
3156 if (complain
& tf_error
)
3157 error_at (loc
, "subscripted value is neither array nor pointer");
3158 return error_mark_node
;
3160 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
3162 if (complain
& tf_error
)
3163 error_at (loc
, "array subscript is not an integer");
3164 return error_mark_node
;
3167 warn_array_subscript_with_type_char (idx
);
3169 ret
= cp_build_indirect_ref (cp_build_binary_op (input_location
,
3174 protected_set_expr_location (ret
, loc
);
3179 /* Entry point for Obj-C++. */
3182 build_array_ref (location_t loc
, tree array
, tree idx
)
3184 return cp_build_array_ref (loc
, array
, idx
, tf_warning_or_error
);
3187 /* Resolve a pointer to member function. INSTANCE is the object
3188 instance to use, if the member points to a virtual member.
3190 This used to avoid checking for virtual functions if basetype
3191 has no virtual functions, according to an earlier ANSI draft.
3192 With the final ISO C++ rules, such an optimization is
3193 incorrect: A pointer to a derived member can be static_cast
3194 to pointer-to-base-member, as long as the dynamic object
3195 later has the right member. So now we only do this optimization
3196 when we know the dynamic type of the object. */
3199 get_member_function_from_ptrfunc (tree
*instance_ptrptr
, tree function
,
3200 tsubst_flags_t complain
)
3202 if (TREE_CODE (function
) == OFFSET_REF
)
3203 function
= TREE_OPERAND (function
, 1);
3205 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
3207 tree idx
, delta
, e1
, e2
, e3
, vtbl
;
3209 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
3210 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
3212 tree instance_ptr
= *instance_ptrptr
;
3213 tree instance_save_expr
= 0;
3214 if (instance_ptr
== error_mark_node
)
3216 if (TREE_CODE (function
) == PTRMEM_CST
)
3218 /* Extracting the function address from a pmf is only
3219 allowed with -Wno-pmf-conversions. It only works for
3221 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
), complain
);
3222 e1
= convert (fntype
, e1
);
3227 if (complain
& tf_error
)
3228 error ("object missing in use of %qE", function
);
3229 return error_mark_node
;
3233 /* True if we know that the dynamic type of the object doesn't have
3234 virtual functions, so we can assume the PFN field is a pointer. */
3235 nonvirtual
= (COMPLETE_TYPE_P (basetype
)
3236 && !TYPE_POLYMORPHIC_P (basetype
)
3237 && resolves_to_fixed_type_p (instance_ptr
, 0));
3239 if (TREE_SIDE_EFFECTS (instance_ptr
))
3240 instance_ptr
= instance_save_expr
= save_expr (instance_ptr
);
3242 if (TREE_SIDE_EFFECTS (function
))
3243 function
= save_expr (function
);
3245 /* Start by extracting all the information from the PMF itself. */
3246 e3
= pfn_from_ptrmemfunc (function
);
3247 delta
= delta_from_ptrmemfunc (function
);
3248 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
3249 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
3251 case ptrmemfunc_vbit_in_pfn
:
3252 e1
= cp_build_binary_op (input_location
,
3253 BIT_AND_EXPR
, idx
, integer_one_node
,
3255 idx
= cp_build_binary_op (input_location
,
3256 MINUS_EXPR
, idx
, integer_one_node
,
3258 if (idx
== error_mark_node
)
3259 return error_mark_node
;
3262 case ptrmemfunc_vbit_in_delta
:
3263 e1
= cp_build_binary_op (input_location
,
3264 BIT_AND_EXPR
, delta
, integer_one_node
,
3266 delta
= cp_build_binary_op (input_location
,
3267 RSHIFT_EXPR
, delta
, integer_one_node
,
3269 if (delta
== error_mark_node
)
3270 return error_mark_node
;
3277 if (e1
== error_mark_node
)
3278 return error_mark_node
;
3280 /* Convert down to the right base before using the instance. A
3281 special case is that in a pointer to member of class C, C may
3282 be incomplete. In that case, the function will of course be
3283 a member of C, and no conversion is required. In fact,
3284 lookup_base will fail in that case, because incomplete
3285 classes do not have BINFOs. */
3286 if (!same_type_ignoring_top_level_qualifiers_p
3287 (basetype
, TREE_TYPE (TREE_TYPE (instance_ptr
))))
3289 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
3290 basetype
, ba_check
, NULL
, complain
);
3291 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
,
3293 if (instance_ptr
== error_mark_node
)
3294 return error_mark_node
;
3296 /* ...and then the delta in the PMF. */
3297 instance_ptr
= fold_build_pointer_plus (instance_ptr
, delta
);
3299 /* Hand back the adjusted 'this' argument to our caller. */
3300 *instance_ptrptr
= instance_ptr
;
3303 /* Now just return the pointer. */
3306 /* Next extract the vtable pointer from the object. */
3307 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
3309 vtbl
= cp_build_indirect_ref (vtbl
, RO_NULL
, complain
);
3310 if (vtbl
== error_mark_node
)
3311 return error_mark_node
;
3313 /* Finally, extract the function pointer from the vtable. */
3314 e2
= fold_build_pointer_plus_loc (input_location
, vtbl
, idx
);
3315 e2
= cp_build_indirect_ref (e2
, RO_NULL
, complain
);
3316 if (e2
== error_mark_node
)
3317 return error_mark_node
;
3318 TREE_CONSTANT (e2
) = 1;
3320 /* When using function descriptors, the address of the
3321 vtable entry is treated as a function pointer. */
3322 if (TARGET_VTABLE_USES_DESCRIPTORS
)
3323 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
3324 cp_build_addr_expr (e2
, complain
));
3326 e2
= fold_convert (TREE_TYPE (e3
), e2
);
3327 e1
= build_conditional_expr (input_location
, e1
, e2
, e3
, complain
);
3328 if (e1
== error_mark_node
)
3329 return error_mark_node
;
3331 /* Make sure this doesn't get evaluated first inside one of the
3332 branches of the COND_EXPR. */
3333 if (instance_save_expr
)
3334 e1
= build2 (COMPOUND_EXPR
, TREE_TYPE (e1
),
3335 instance_save_expr
, e1
);
3342 /* Used by the C-common bits. */
3344 build_function_call (location_t
/*loc*/,
3345 tree function
, tree params
)
3347 return cp_build_function_call (function
, params
, tf_warning_or_error
);
3350 /* Used by the C-common bits. */
3352 build_function_call_vec (location_t
/*loc*/,
3353 tree function
, vec
<tree
, va_gc
> *params
,
3354 vec
<tree
, va_gc
> * /*origtypes*/)
3356 vec
<tree
, va_gc
> *orig_params
= params
;
3357 tree ret
= cp_build_function_call_vec (function
, ¶ms
,
3358 tf_warning_or_error
);
3360 /* cp_build_function_call_vec can reallocate PARAMS by adding
3361 default arguments. That should never happen here. Verify
3363 gcc_assert (params
== orig_params
);
3368 /* Build a function call using a tree list of arguments. */
3371 cp_build_function_call (tree function
, tree params
, tsubst_flags_t complain
)
3373 vec
<tree
, va_gc
> *vec
;
3376 vec
= make_tree_vector ();
3377 for (; params
!= NULL_TREE
; params
= TREE_CHAIN (params
))
3378 vec_safe_push (vec
, TREE_VALUE (params
));
3379 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3380 release_tree_vector (vec
);
3384 /* Build a function call using varargs. */
3387 cp_build_function_call_nary (tree function
, tsubst_flags_t complain
, ...)
3389 vec
<tree
, va_gc
> *vec
;
3393 vec
= make_tree_vector ();
3394 va_start (args
, complain
);
3395 for (t
= va_arg (args
, tree
); t
!= NULL_TREE
; t
= va_arg (args
, tree
))
3396 vec_safe_push (vec
, t
);
3398 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3399 release_tree_vector (vec
);
3403 /* Build a function call using a vector of arguments. PARAMS may be
3404 NULL if there are no parameters. This changes the contents of
3408 cp_build_function_call_vec (tree function
, vec
<tree
, va_gc
> **params
,
3409 tsubst_flags_t complain
)
3411 tree fntype
, fndecl
;
3413 tree original
= function
;
3417 vec
<tree
, va_gc
> *allocated
= NULL
;
3420 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3421 expressions, like those used for ObjC messenger dispatches. */
3422 if (params
!= NULL
&& !vec_safe_is_empty (*params
))
3423 function
= objc_rewrite_function_call (function
, (**params
)[0]);
3425 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3426 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3427 if (TREE_CODE (function
) == NOP_EXPR
3428 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
3429 function
= TREE_OPERAND (function
, 0);
3431 if (TREE_CODE (function
) == FUNCTION_DECL
)
3433 mark_used (function
);
3436 /* Convert anything with function type to a pointer-to-function. */
3437 if (DECL_MAIN_P (function
) && (complain
& tf_error
))
3438 pedwarn (input_location
, OPT_Wpedantic
,
3439 "ISO C++ forbids calling %<::main%> from within program");
3441 function
= build_addr_func (function
, complain
);
3447 function
= build_addr_func (function
, complain
);
3450 if (function
== error_mark_node
)
3451 return error_mark_node
;
3453 fntype
= TREE_TYPE (function
);
3455 if (TYPE_PTRMEMFUNC_P (fntype
))
3457 if (complain
& tf_error
)
3458 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3459 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3460 original
, original
);
3461 return error_mark_node
;
3464 is_method
= (TYPE_PTR_P (fntype
)
3465 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
3467 if (!(TYPE_PTRFN_P (fntype
)
3469 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
3471 if (complain
& tf_error
)
3473 if (!flag_diagnostics_show_caret
)
3474 error_at (input_location
,
3475 "%qE cannot be used as a function", original
);
3476 else if (DECL_P (original
))
3477 error_at (input_location
,
3478 "%qD cannot be used as a function", original
);
3480 error_at (input_location
,
3481 "expression cannot be used as a function");
3484 return error_mark_node
;
3487 /* fntype now gets the type of function pointed to. */
3488 fntype
= TREE_TYPE (fntype
);
3489 parm_types
= TYPE_ARG_TYPES (fntype
);
3493 allocated
= make_tree_vector ();
3494 params
= &allocated
;
3497 nargs
= convert_arguments (parm_types
, params
, fndecl
, LOOKUP_NORMAL
,
3500 return error_mark_node
;
3502 argarray
= (*params
)->address ();
3504 /* Check for errors in format strings and inappropriately
3506 check_function_arguments (fntype
, nargs
, argarray
);
3508 ret
= build_cxx_call (function
, nargs
, argarray
, complain
);
3510 if (allocated
!= NULL
)
3511 release_tree_vector (allocated
);
3516 /* Subroutine of convert_arguments.
3517 Warn about wrong number of args are genereted. */
3520 warn_args_num (location_t loc
, tree fndecl
, bool too_many_p
)
3524 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
3526 if (DECL_NAME (fndecl
) == NULL_TREE
3527 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
3530 ? G_("too many arguments to constructor %q#D")
3531 : G_("too few arguments to constructor %q#D"),
3536 ? G_("too many arguments to member function %q#D")
3537 : G_("too few arguments to member function %q#D"),
3543 ? G_("too many arguments to function %q#D")
3544 : G_("too few arguments to function %q#D"),
3546 inform (DECL_SOURCE_LOCATION (fndecl
),
3551 if (c_dialect_objc () && objc_message_selector ())
3554 ? G_("too many arguments to method %q#D")
3555 : G_("too few arguments to method %q#D"),
3556 objc_message_selector ());
3558 error_at (loc
, too_many_p
? G_("too many arguments to function")
3559 : G_("too few arguments to function"));
3563 /* Convert the actual parameter expressions in the list VALUES to the
3564 types in the list TYPELIST. The converted expressions are stored
3565 back in the VALUES vector.
3566 If parmdecls is exhausted, or when an element has NULL as its type,
3567 perform the default conversions.
3569 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3571 This is also where warnings about wrong number of args are generated.
3573 Returns the actual number of arguments processed (which might be less
3574 than the length of the vector), or -1 on error.
3576 In C++, unspecified trailing parameters can be filled in with their
3577 default arguments, if such were specified. Do so here. */
3580 convert_arguments (tree typelist
, vec
<tree
, va_gc
> **values
, tree fndecl
,
3581 int flags
, tsubst_flags_t complain
)
3586 /* Argument passing is always copy-initialization. */
3587 flags
|= LOOKUP_ONLYCONVERTING
;
3589 for (i
= 0, typetail
= typelist
;
3590 i
< vec_safe_length (*values
);
3593 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3594 tree val
= (**values
)[i
];
3596 if (val
== error_mark_node
|| type
== error_mark_node
)
3599 if (type
== void_type_node
)
3601 if (complain
& tf_error
)
3603 warn_args_num (input_location
, fndecl
, /*too_many_p=*/true);
3610 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3611 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3612 if (TREE_CODE (val
) == NOP_EXPR
3613 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
3614 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
3615 val
= TREE_OPERAND (val
, 0);
3617 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
3619 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
3620 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
3621 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
3622 val
= decay_conversion (val
, complain
);
3625 if (val
== error_mark_node
)
3630 /* Formal parm type is specified by a function prototype. */
3633 if (!COMPLETE_TYPE_P (complete_type (type
)))
3635 if (complain
& tf_error
)
3638 error ("parameter %P of %qD has incomplete type %qT",
3641 error ("parameter %P has incomplete type %qT", i
, type
);
3643 parmval
= error_mark_node
;
3647 parmval
= convert_for_initialization
3648 (NULL_TREE
, type
, val
, flags
,
3649 ICR_ARGPASS
, fndecl
, i
, complain
);
3650 parmval
= convert_for_arg_passing (type
, parmval
, complain
);
3653 if (parmval
== error_mark_node
)
3656 (**values
)[i
] = parmval
;
3660 if (fndecl
&& magic_varargs_p (fndecl
))
3661 /* Don't do ellipsis conversion for __built_in_constant_p
3662 as this will result in spurious errors for non-trivial
3664 val
= require_complete_type_sfinae (val
, complain
);
3666 val
= convert_arg_to_ellipsis (val
, complain
);
3668 (**values
)[i
] = val
;
3672 typetail
= TREE_CHAIN (typetail
);
3675 if (typetail
!= 0 && typetail
!= void_list_node
)
3677 /* See if there are default arguments that can be used. Because
3678 we hold default arguments in the FUNCTION_TYPE (which is so
3679 wrong), we can see default parameters here from deduced
3680 contexts (and via typeof) for indirect function calls.
3681 Fortunately we know whether we have a function decl to
3682 provide default arguments in a language conformant
3684 if (fndecl
&& TREE_PURPOSE (typetail
)
3685 && TREE_CODE (TREE_PURPOSE (typetail
)) != DEFAULT_ARG
)
3687 for (; typetail
!= void_list_node
; ++i
)
3690 = convert_default_arg (TREE_VALUE (typetail
),
3691 TREE_PURPOSE (typetail
),
3692 fndecl
, i
, complain
);
3694 if (parmval
== error_mark_node
)
3697 vec_safe_push (*values
, parmval
);
3698 typetail
= TREE_CHAIN (typetail
);
3699 /* ends with `...'. */
3700 if (typetail
== NULL_TREE
)
3706 if (complain
& tf_error
)
3707 warn_args_num (input_location
, fndecl
, /*too_many_p=*/false);
3715 /* Build a binary-operation expression, after performing default
3716 conversions on the operands. CODE is the kind of expression to
3717 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3718 are the tree codes which correspond to ARG1 and ARG2 when issuing
3719 warnings about possibly misplaced parentheses. They may differ
3720 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3721 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3722 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3723 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3724 ARG2_CODE as ERROR_MARK. */
3727 build_x_binary_op (location_t loc
, enum tree_code code
, tree arg1
,
3728 enum tree_code arg1_code
, tree arg2
,
3729 enum tree_code arg2_code
, tree
*overload
,
3730 tsubst_flags_t complain
)
3739 if (processing_template_decl
)
3741 if (type_dependent_expression_p (arg1
)
3742 || type_dependent_expression_p (arg2
))
3743 return build_min_nt_loc (loc
, code
, arg1
, arg2
);
3744 arg1
= build_non_dependent_expr (arg1
);
3745 arg2
= build_non_dependent_expr (arg2
);
3748 if (code
== DOTSTAR_EXPR
)
3749 expr
= build_m_component_ref (arg1
, arg2
, complain
);
3751 expr
= build_new_op (loc
, code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
,
3752 overload
, complain
);
3754 /* Check for cases such as x+y<<z which users are likely to
3755 misinterpret. But don't warn about obj << x + y, since that is a
3756 common idiom for I/O. */
3757 if (warn_parentheses
3758 && (complain
& tf_warning
)
3759 && !processing_template_decl
3760 && !error_operand_p (arg1
)
3761 && !error_operand_p (arg2
)
3762 && (code
!= LSHIFT_EXPR
3763 || !CLASS_TYPE_P (TREE_TYPE (arg1
))))
3764 warn_about_parentheses (loc
, code
, arg1_code
, orig_arg1
,
3765 arg2_code
, orig_arg2
);
3767 if (processing_template_decl
&& expr
!= error_mark_node
)
3768 return build_min_non_dep (code
, expr
, orig_arg1
, orig_arg2
);
3773 /* Build and return an ARRAY_REF expression. */
3776 build_x_array_ref (location_t loc
, tree arg1
, tree arg2
,
3777 tsubst_flags_t complain
)
3779 tree orig_arg1
= arg1
;
3780 tree orig_arg2
= arg2
;
3783 if (processing_template_decl
)
3785 if (type_dependent_expression_p (arg1
)
3786 || type_dependent_expression_p (arg2
))
3787 return build_min_nt_loc (loc
, ARRAY_REF
, arg1
, arg2
,
3788 NULL_TREE
, NULL_TREE
);
3789 arg1
= build_non_dependent_expr (arg1
);
3790 arg2
= build_non_dependent_expr (arg2
);
3793 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, arg1
, arg2
,
3794 NULL_TREE
, /*overload=*/NULL
, complain
);
3796 if (processing_template_decl
&& expr
!= error_mark_node
)
3797 return build_min_non_dep (ARRAY_REF
, expr
, orig_arg1
, orig_arg2
,
3798 NULL_TREE
, NULL_TREE
);
3802 /* Return whether OP is an expression of enum type cast to integer
3803 type. In C++ even unsigned enum types are cast to signed integer
3804 types. We do not want to issue warnings about comparisons between
3805 signed and unsigned types when one of the types is an enum type.
3806 Those warnings are always false positives in practice. */
3809 enum_cast_to_int (tree op
)
3811 if (TREE_CODE (op
) == NOP_EXPR
3812 && TREE_TYPE (op
) == integer_type_node
3813 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == ENUMERAL_TYPE
3814 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op
, 0))))
3817 /* The cast may have been pushed into a COND_EXPR. */
3818 if (TREE_CODE (op
) == COND_EXPR
)
3819 return (enum_cast_to_int (TREE_OPERAND (op
, 1))
3820 || enum_cast_to_int (TREE_OPERAND (op
, 2)));
3825 /* For the c-common bits. */
3827 build_binary_op (location_t location
, enum tree_code code
, tree op0
, tree op1
,
3830 return cp_build_binary_op (location
, code
, op0
, op1
, tf_warning_or_error
);
3834 /* Build a binary-operation expression without default conversions.
3835 CODE is the kind of expression to build.
3836 LOCATION is the location_t of the operator in the source code.
3837 This function differs from `build' in several ways:
3838 the data type of the result is computed and recorded in it,
3839 warnings are generated if arg data types are invalid,
3840 special handling for addition and subtraction of pointers is known,
3841 and some optimization is done (operations on narrow ints
3842 are done in the narrower type when that gives the same result).
3843 Constant folding is also done before the result is returned.
3845 Note that the operands will never have enumeral types
3846 because either they have just had the default conversions performed
3847 or they have both just been converted to some other type in which
3848 the arithmetic is to be done.
3850 C++: must do special pointer arithmetic when implementing
3851 multiple inheritance, and deal with pointer to member functions. */
3854 cp_build_binary_op (location_t location
,
3855 enum tree_code code
, tree orig_op0
, tree orig_op1
,
3856 tsubst_flags_t complain
)
3859 enum tree_code code0
, code1
;
3861 const char *invalid_op_diag
;
3863 /* Expression code to give to the expression when it is built.
3864 Normally this is CODE, which is what the caller asked for,
3865 but in some special cases we change it. */
3866 enum tree_code resultcode
= code
;
3868 /* Data type in which the computation is to be performed.
3869 In the simplest cases this is the common type of the arguments. */
3870 tree result_type
= NULL
;
3872 /* Nonzero means operands have already been type-converted
3873 in whatever way is necessary.
3874 Zero means they need to be converted to RESULT_TYPE. */
3877 /* Nonzero means create the expression with this type, rather than
3879 tree build_type
= 0;
3881 /* Nonzero means after finally constructing the expression
3882 convert it to this type. */
3883 tree final_type
= 0;
3886 tree orig_type
= NULL
;
3888 /* Nonzero if this is an operation like MIN or MAX which can
3889 safely be computed in short if both args are promoted shorts.
3890 Also implies COMMON.
3891 -1 indicates a bitwise operation; this makes a difference
3892 in the exact conditions for when it is safe to do the operation
3893 in a narrower mode. */
3896 /* Nonzero if this is a comparison operation;
3897 if both args are promoted shorts, compare the original shorts.
3898 Also implies COMMON. */
3899 int short_compare
= 0;
3901 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3904 /* True if both operands have arithmetic type. */
3905 bool arithmetic_types_p
;
3907 /* Apply default conversions. */
3911 /* Remember whether we're doing / or %. */
3912 bool doing_div_or_mod
= false;
3914 /* Remember whether we're doing << or >>. */
3915 bool doing_shift
= false;
3917 /* Tree holding instrumentation expression. */
3918 tree instrument_expr
= NULL
;
3920 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
3921 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
3922 || code
== TRUTH_XOR_EXPR
)
3924 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
3925 op0
= decay_conversion (op0
, complain
);
3926 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
3927 op1
= decay_conversion (op1
, complain
);
3931 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
3932 op0
= cp_default_conversion (op0
, complain
);
3933 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
3934 op1
= cp_default_conversion (op1
, complain
);
3937 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3938 STRIP_TYPE_NOPS (op0
);
3939 STRIP_TYPE_NOPS (op1
);
3941 /* DTRT if one side is an overloaded function, but complain about it. */
3942 if (type_unknown_p (op0
))
3944 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
3945 if (t
!= error_mark_node
)
3947 if (complain
& tf_error
)
3948 permerror (input_location
, "assuming cast to type %qT from overloaded function",
3953 if (type_unknown_p (op1
))
3955 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
3956 if (t
!= error_mark_node
)
3958 if (complain
& tf_error
)
3959 permerror (input_location
, "assuming cast to type %qT from overloaded function",
3965 type0
= TREE_TYPE (op0
);
3966 type1
= TREE_TYPE (op1
);
3968 /* The expression codes of the data types of the arguments tell us
3969 whether the arguments are integers, floating, pointers, etc. */
3970 code0
= TREE_CODE (type0
);
3971 code1
= TREE_CODE (type1
);
3973 /* If an error was already reported for one of the arguments,
3974 avoid reporting another error. */
3975 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
3976 return error_mark_node
;
3978 if ((invalid_op_diag
3979 = targetm
.invalid_binary_op (code
, type0
, type1
)))
3981 if (complain
& tf_error
)
3982 error (invalid_op_diag
);
3983 return error_mark_node
;
3986 /* Issue warnings about peculiar, but valid, uses of NULL. */
3987 if ((orig_op0
== null_node
|| orig_op1
== null_node
)
3988 /* It's reasonable to use pointer values as operands of &&
3989 and ||, so NULL is no exception. */
3990 && code
!= TRUTH_ANDIF_EXPR
&& code
!= TRUTH_ORIF_EXPR
3991 && ( /* Both are NULL (or 0) and the operation was not a
3992 comparison or a pointer subtraction. */
3993 (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)
3994 && code
!= EQ_EXPR
&& code
!= NE_EXPR
&& code
!= MINUS_EXPR
)
3995 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
3996 || (!null_ptr_cst_p (orig_op0
)
3997 && !TYPE_PTR_OR_PTRMEM_P (type0
))
3998 || (!null_ptr_cst_p (orig_op1
)
3999 && !TYPE_PTR_OR_PTRMEM_P (type1
)))
4000 && (complain
& tf_warning
))
4002 source_location loc
=
4003 expansion_point_location_if_in_system_header (input_location
);
4005 warning_at (loc
, OPT_Wpointer_arith
, "NULL used in arithmetic");
4008 /* In case when one of the operands of the binary operation is
4009 a vector and another is a scalar -- convert scalar to vector. */
4010 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
4012 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
4013 complain
& tf_error
);
4015 switch (convert_flag
)
4018 return error_mark_node
;
4021 op0
= save_expr (op0
);
4022 op0
= convert (TREE_TYPE (type1
), op0
);
4023 op0
= build_vector_from_val (type1
, op0
);
4024 type0
= TREE_TYPE (op0
);
4025 code0
= TREE_CODE (type0
);
4031 op1
= save_expr (op1
);
4032 op1
= convert (TREE_TYPE (type0
), op1
);
4033 op1
= build_vector_from_val (type0
, op1
);
4034 type1
= TREE_TYPE (op1
);
4035 code1
= TREE_CODE (type1
);
4047 /* Subtraction of two similar pointers.
4048 We must subtract them as integers, then divide by object size. */
4049 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
4050 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4052 return pointer_diff (op0
, op1
, common_pointer_type (type0
, type1
),
4054 /* In all other cases except pointer - int, the usual arithmetic
4056 else if (!(code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
))
4061 /* The pointer - int case is just like pointer + int; fall
4064 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
4065 && (code0
== INTEGER_TYPE
|| code1
== INTEGER_TYPE
))
4069 ptr_operand
= ((code0
== POINTER_TYPE
) ? op0
: op1
);
4070 int_operand
= ((code0
== INTEGER_TYPE
) ? op0
: op1
);
4071 if (processing_template_decl
)
4073 result_type
= TREE_TYPE (ptr_operand
);
4076 return cp_pointer_int_sum (code
,
4088 case TRUNC_DIV_EXPR
:
4090 case FLOOR_DIV_EXPR
:
4091 case ROUND_DIV_EXPR
:
4092 case EXACT_DIV_EXPR
:
4093 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4094 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
4095 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4096 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
4098 enum tree_code tcode0
= code0
, tcode1
= code1
;
4099 tree cop1
= fold_non_dependent_expr_sfinae (op1
, tf_none
);
4100 cop1
= maybe_constant_value (cop1
);
4102 if (tcode0
== INTEGER_TYPE
)
4103 doing_div_or_mod
= true;
4105 warn_for_div_by_zero (location
, cop1
);
4107 if (tcode0
== COMPLEX_TYPE
|| tcode0
== VECTOR_TYPE
)
4108 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
4109 if (tcode1
== COMPLEX_TYPE
|| tcode1
== VECTOR_TYPE
)
4110 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
4112 if (!(tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
))
4113 resultcode
= RDIV_EXPR
;
4115 /* When dividing two signed integers, we have to promote to int.
4116 unless we divide by a constant != -1. Note that default
4117 conversion will have been performed on the operands at this
4118 point, so we have to dig out the original type to find out if
4120 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4121 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4122 || (TREE_CODE (op1
) == INTEGER_CST
4123 && ! integer_all_onesp (op1
)));
4132 if ((code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4133 || (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4134 && !VECTOR_FLOAT_TYPE_P (type0
)
4135 && !VECTOR_FLOAT_TYPE_P (type1
)))
4139 case TRUNC_MOD_EXPR
:
4140 case FLOOR_MOD_EXPR
:
4142 tree cop1
= fold_non_dependent_expr_sfinae (op1
, tf_none
);
4143 cop1
= maybe_constant_value (cop1
);
4145 if (code0
== INTEGER_TYPE
)
4146 doing_div_or_mod
= true;
4147 warn_for_div_by_zero (location
, cop1
);
4150 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4151 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4152 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
4154 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4156 /* Although it would be tempting to shorten always here, that loses
4157 on some targets, since the modulo instruction is undefined if the
4158 quotient can't be represented in the computation mode. We shorten
4159 only if unsigned or if dividing by something we know != -1. */
4160 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4161 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4162 || (TREE_CODE (op1
) == INTEGER_CST
4163 && ! integer_all_onesp (op1
)));
4168 case TRUTH_ANDIF_EXPR
:
4169 case TRUTH_ORIF_EXPR
:
4170 case TRUTH_AND_EXPR
:
4172 result_type
= boolean_type_node
;
4175 /* Shift operations: result has same type as first operand;
4176 always convert second operand to int.
4177 Also set SHORT_SHIFT if shifting rightward. */
4180 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4181 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4183 result_type
= type0
;
4186 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4187 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4188 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4189 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
4191 result_type
= type0
;
4194 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4196 tree const_op1
= fold_non_dependent_expr_sfinae (op1
, tf_none
);
4197 const_op1
= maybe_constant_value (const_op1
);
4198 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4200 result_type
= type0
;
4202 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4204 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4206 if ((complain
& tf_warning
)
4207 && c_inhibit_evaluation_warnings
== 0)
4208 warning (0, "right shift count is negative");
4212 if (compare_tree_int (const_op1
, TYPE_PRECISION (type0
)) >= 0
4213 && (complain
& tf_warning
)
4214 && c_inhibit_evaluation_warnings
== 0)
4215 warning (0, "right shift count >= width of type");
4218 /* Convert the shift-count to an integer, regardless of
4219 size of value being shifted. */
4220 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4221 op1
= cp_convert (integer_type_node
, op1
, complain
);
4222 /* Avoid converting op1 to result_type later. */
4228 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4229 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4231 result_type
= type0
;
4234 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4235 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4236 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4237 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
4239 result_type
= type0
;
4242 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4244 tree const_op1
= fold_non_dependent_expr_sfinae (op1
, tf_none
);
4245 const_op1
= maybe_constant_value (const_op1
);
4246 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4248 result_type
= type0
;
4250 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4252 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4254 if ((complain
& tf_warning
)
4255 && c_inhibit_evaluation_warnings
== 0)
4256 warning (0, "left shift count is negative");
4258 else if (compare_tree_int (const_op1
,
4259 TYPE_PRECISION (type0
)) >= 0)
4261 if ((complain
& tf_warning
)
4262 && c_inhibit_evaluation_warnings
== 0)
4263 warning (0, "left shift count >= width of type");
4266 /* Convert the shift-count to an integer, regardless of
4267 size of value being shifted. */
4268 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4269 op1
= cp_convert (integer_type_node
, op1
, complain
);
4270 /* Avoid converting op1 to result_type later. */
4277 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4279 result_type
= type0
;
4280 if (TREE_CODE (op1
) == INTEGER_CST
)
4282 if (tree_int_cst_lt (op1
, integer_zero_node
))
4284 if (complain
& tf_warning
)
4285 warning (0, (code
== LROTATE_EXPR
)
4286 ? G_("left rotate count is negative")
4287 : G_("right rotate count is negative"));
4289 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
4291 if (complain
& tf_warning
)
4292 warning (0, (code
== LROTATE_EXPR
)
4293 ? G_("left rotate count >= width of type")
4294 : G_("right rotate count >= width of type"));
4297 /* Convert the shift-count to an integer, regardless of
4298 size of value being shifted. */
4299 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4300 op1
= cp_convert (integer_type_node
, op1
, complain
);
4306 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4307 goto vector_compare
;
4308 if ((complain
& tf_warning
)
4309 && (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
)))
4310 warning (OPT_Wfloat_equal
,
4311 "comparing floating point with == or != is unsafe");
4312 if ((complain
& tf_warning
)
4313 && ((TREE_CODE (orig_op0
) == STRING_CST
&& !integer_zerop (op1
))
4314 || (TREE_CODE (orig_op1
) == STRING_CST
&& !integer_zerop (op0
))))
4315 warning (OPT_Waddress
, "comparison with string literal results in unspecified behaviour");
4317 build_type
= boolean_type_node
;
4318 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4319 || code0
== COMPLEX_TYPE
|| code0
== ENUMERAL_TYPE
)
4320 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4321 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
))
4323 else if ((code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4324 || (TYPE_PTRDATAMEM_P (type0
) && TYPE_PTRDATAMEM_P (type1
)))
4325 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4326 CPO_COMPARISON
, complain
);
4327 else if ((code0
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type0
))
4328 && null_ptr_cst_p (op1
))
4330 if (TREE_CODE (op0
) == ADDR_EXPR
4331 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0)))
4333 if ((complain
& tf_warning
)
4334 && c_inhibit_evaluation_warnings
== 0)
4335 warning (OPT_Waddress
, "the address of %qD will never be NULL",
4336 TREE_OPERAND (op0
, 0));
4338 result_type
= type0
;
4340 else if ((code1
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type1
))
4341 && null_ptr_cst_p (op0
))
4343 if (TREE_CODE (op1
) == ADDR_EXPR
4344 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0)))
4346 if ((complain
& tf_warning
)
4347 && c_inhibit_evaluation_warnings
== 0)
4348 warning (OPT_Waddress
, "the address of %qD will never be NULL",
4349 TREE_OPERAND (op1
, 0));
4351 result_type
= type1
;
4353 else if (null_ptr_cst_p (op0
) && null_ptr_cst_p (op1
))
4354 /* One of the operands must be of nullptr_t type. */
4355 result_type
= TREE_TYPE (nullptr_node
);
4356 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4358 result_type
= type0
;
4359 if (complain
& tf_error
)
4360 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4362 return error_mark_node
;
4364 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4366 result_type
= type1
;
4367 if (complain
& tf_error
)
4368 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4370 return error_mark_node
;
4372 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (op1
))
4374 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4375 == ptrmemfunc_vbit_in_delta
)
4377 tree pfn0
, delta0
, e1
, e2
;
4379 if (TREE_SIDE_EFFECTS (op0
))
4380 op0
= save_expr (op0
);
4382 pfn0
= pfn_from_ptrmemfunc (op0
);
4383 delta0
= delta_from_ptrmemfunc (op0
);
4384 e1
= cp_build_binary_op (location
,
4387 build_zero_cst (TREE_TYPE (pfn0
)),
4389 e2
= cp_build_binary_op (location
,
4395 if (complain
& tf_warning
)
4396 maybe_warn_zero_as_null_pointer_constant (op1
, input_location
);
4398 e2
= cp_build_binary_op (location
,
4399 EQ_EXPR
, e2
, integer_zero_node
,
4401 op0
= cp_build_binary_op (location
,
4402 TRUTH_ANDIF_EXPR
, e1
, e2
,
4404 op1
= cp_convert (TREE_TYPE (op0
), integer_one_node
, complain
);
4408 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
4409 op1
= cp_convert (TREE_TYPE (op0
), op1
, complain
);
4411 result_type
= TREE_TYPE (op0
);
4413 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (op0
))
4414 return cp_build_binary_op (location
, code
, op1
, op0
, complain
);
4415 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
))
4418 /* E will be the final comparison. */
4420 /* E1 and E2 are for scratch. */
4428 type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4429 CPO_COMPARISON
, complain
);
4431 if (!same_type_p (TREE_TYPE (op0
), type
))
4432 op0
= cp_convert_and_check (type
, op0
, complain
);
4433 if (!same_type_p (TREE_TYPE (op1
), type
))
4434 op1
= cp_convert_and_check (type
, op1
, complain
);
4436 if (op0
== error_mark_node
|| op1
== error_mark_node
)
4437 return error_mark_node
;
4439 if (TREE_SIDE_EFFECTS (op0
))
4440 op0
= save_expr (op0
);
4441 if (TREE_SIDE_EFFECTS (op1
))
4442 op1
= save_expr (op1
);
4444 pfn0
= pfn_from_ptrmemfunc (op0
);
4445 pfn1
= pfn_from_ptrmemfunc (op1
);
4446 delta0
= delta_from_ptrmemfunc (op0
);
4447 delta1
= delta_from_ptrmemfunc (op1
);
4448 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4449 == ptrmemfunc_vbit_in_delta
)
4454 && ((op0.delta == op1.delta)
4455 || (!op0.pfn && op0.delta & 1 == 0
4456 && op1.delta & 1 == 0))
4458 The reason for the `!op0.pfn' bit is that a NULL
4459 pointer-to-member is any member with a zero PFN and
4460 LSB of the DELTA field is 0. */
4462 e1
= cp_build_binary_op (location
, BIT_AND_EXPR
,
4466 e1
= cp_build_binary_op (location
,
4467 EQ_EXPR
, e1
, integer_zero_node
,
4469 e2
= cp_build_binary_op (location
, BIT_AND_EXPR
,
4473 e2
= cp_build_binary_op (location
,
4474 EQ_EXPR
, e2
, integer_zero_node
,
4476 e1
= cp_build_binary_op (location
,
4477 TRUTH_ANDIF_EXPR
, e2
, e1
,
4479 e2
= cp_build_binary_op (location
, EQ_EXPR
,
4481 build_zero_cst (TREE_TYPE (pfn0
)),
4483 e2
= cp_build_binary_op (location
,
4484 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
4485 e1
= cp_build_binary_op (location
,
4486 EQ_EXPR
, delta0
, delta1
, complain
);
4487 e1
= cp_build_binary_op (location
,
4488 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
4495 && (!op0.pfn || op0.delta == op1.delta))
4497 The reason for the `!op0.pfn' bit is that a NULL
4498 pointer-to-member is any member with a zero PFN; the
4499 DELTA field is unspecified. */
4501 e1
= cp_build_binary_op (location
,
4502 EQ_EXPR
, delta0
, delta1
, complain
);
4503 e2
= cp_build_binary_op (location
,
4506 build_zero_cst (TREE_TYPE (pfn0
)),
4508 e1
= cp_build_binary_op (location
,
4509 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
4511 e2
= build2 (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
4512 e
= cp_build_binary_op (location
,
4513 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
4514 if (code
== EQ_EXPR
)
4516 return cp_build_binary_op (location
,
4517 EQ_EXPR
, e
, integer_zero_node
, complain
);
4521 gcc_assert (!TYPE_PTRMEMFUNC_P (type0
)
4522 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
),
4524 gcc_assert (!TYPE_PTRMEMFUNC_P (type1
)
4525 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
),
4533 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
4534 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
4536 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4537 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4538 CPO_COMPARISON
, complain
);
4545 if (TREE_CODE (orig_op0
) == STRING_CST
4546 || TREE_CODE (orig_op1
) == STRING_CST
)
4548 if (complain
& tf_warning
)
4549 warning (OPT_Waddress
, "comparison with string literal results in unspecified behaviour");
4552 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4556 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4559 if (complain
& tf_error
)
4561 error_at (location
, "comparing vectors with different "
4563 inform (location
, "operand types are %qT and %qT",
4566 return error_mark_node
;
4569 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
4571 if (complain
& tf_error
)
4573 error_at (location
, "comparing vectors with different "
4574 "number of elements");
4575 inform (location
, "operand types are %qT and %qT",
4578 return error_mark_node
;
4581 /* Always construct signed integer vector type. */
4582 intt
= c_common_type_for_size (GET_MODE_BITSIZE
4583 (TYPE_MODE (TREE_TYPE (type0
))), 0);
4586 if (complain
& tf_error
)
4587 error_at (location
, "could not find an integer type "
4588 "of the same size as %qT", TREE_TYPE (type0
));
4589 return error_mark_node
;
4591 result_type
= build_opaque_vector_type (intt
,
4592 TYPE_VECTOR_SUBPARTS (type0
));
4596 build_type
= boolean_type_node
;
4597 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4598 || code0
== ENUMERAL_TYPE
)
4599 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4600 || code1
== ENUMERAL_TYPE
))
4602 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4603 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4604 CPO_COMPARISON
, complain
);
4605 else if (code0
== POINTER_TYPE
&& null_ptr_cst_p (op1
))
4607 result_type
= type0
;
4608 if (extra_warnings
&& (complain
& tf_warning
))
4609 warning (OPT_Wextra
,
4610 "ordered comparison of pointer with integer zero");
4612 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (op0
))
4614 result_type
= type1
;
4615 if (extra_warnings
&& (complain
& tf_warning
))
4616 warning (OPT_Wextra
,
4617 "ordered comparison of pointer with integer zero");
4619 else if (null_ptr_cst_p (op0
) && null_ptr_cst_p (op1
))
4620 /* One of the operands must be of nullptr_t type. */
4621 result_type
= TREE_TYPE (nullptr_node
);
4622 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4624 result_type
= type0
;
4625 if (complain
& tf_error
)
4626 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4628 return error_mark_node
;
4630 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4632 result_type
= type1
;
4633 if (complain
& tf_error
)
4634 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4636 return error_mark_node
;
4640 case UNORDERED_EXPR
:
4647 build_type
= integer_type_node
;
4648 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
4650 if (complain
& tf_error
)
4651 error ("unordered comparison on non-floating point argument");
4652 return error_mark_node
;
4661 if (((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
4662 || code0
== ENUMERAL_TYPE
)
4663 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4664 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
)))
4665 arithmetic_types_p
= 1;
4668 arithmetic_types_p
= 0;
4669 /* Vector arithmetic is only allowed when both sides are vectors. */
4670 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4672 if (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
4673 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0
),
4676 if (complain
& tf_error
)
4677 binary_op_error (location
, code
, type0
, type1
);
4678 return error_mark_node
;
4680 arithmetic_types_p
= 1;
4683 /* Determine the RESULT_TYPE, if it is not already known. */
4685 && arithmetic_types_p
4686 && (shorten
|| common
|| short_compare
))
4688 result_type
= cp_common_type (type0
, type1
);
4689 if (complain
& tf_warning
)
4690 do_warn_double_promotion (result_type
, type0
, type1
,
4691 "implicit conversion from %qT to %qT "
4692 "to match other operand of binary "
4699 if (complain
& tf_error
)
4700 error ("invalid operands of types %qT and %qT to binary %qO",
4701 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
4702 return error_mark_node
;
4705 /* If we're in a template, the only thing we need to know is the
4707 if (processing_template_decl
)
4709 /* Since the middle-end checks the type when doing a build2, we
4710 need to build the tree in pieces. This built tree will never
4711 get out of the front-end as we replace it when instantiating
4713 tree tmp
= build2 (resultcode
,
4714 build_type
? build_type
: result_type
,
4716 TREE_OPERAND (tmp
, 0) = op0
;
4720 if (arithmetic_types_p
)
4722 bool first_complex
= (code0
== COMPLEX_TYPE
);
4723 bool second_complex
= (code1
== COMPLEX_TYPE
);
4724 int none_complex
= (!first_complex
&& !second_complex
);
4726 /* Adapted from patch for c/24581. */
4727 if (first_complex
!= second_complex
4728 && (code
== PLUS_EXPR
4729 || code
== MINUS_EXPR
4730 || code
== MULT_EXPR
4731 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
4732 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
4733 && flag_signed_zeros
)
4735 /* An operation on mixed real/complex operands must be
4736 handled specially, but the language-independent code can
4737 more easily optimize the plain complex arithmetic if
4738 -fno-signed-zeros. */
4739 tree real_type
= TREE_TYPE (result_type
);
4743 if (TREE_TYPE (op0
) != result_type
)
4744 op0
= cp_convert_and_check (result_type
, op0
, complain
);
4745 if (TREE_TYPE (op1
) != real_type
)
4746 op1
= cp_convert_and_check (real_type
, op1
, complain
);
4750 if (TREE_TYPE (op0
) != real_type
)
4751 op0
= cp_convert_and_check (real_type
, op0
, complain
);
4752 if (TREE_TYPE (op1
) != result_type
)
4753 op1
= cp_convert_and_check (result_type
, op1
, complain
);
4755 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
4756 return error_mark_node
;
4759 op0
= save_expr (op0
);
4760 real
= cp_build_unary_op (REALPART_EXPR
, op0
, 1, complain
);
4761 imag
= cp_build_unary_op (IMAGPART_EXPR
, op0
, 1, complain
);
4765 case TRUNC_DIV_EXPR
:
4766 op1
= save_expr (op1
);
4767 imag
= build2 (resultcode
, real_type
, imag
, op1
);
4771 real
= build2 (resultcode
, real_type
, real
, op1
);
4779 op1
= save_expr (op1
);
4780 real
= cp_build_unary_op (REALPART_EXPR
, op1
, 1, complain
);
4781 imag
= cp_build_unary_op (IMAGPART_EXPR
, op1
, 1, complain
);
4785 op0
= save_expr (op0
);
4786 imag
= build2 (resultcode
, real_type
, op0
, imag
);
4789 real
= build2 (resultcode
, real_type
, op0
, real
);
4792 real
= build2 (resultcode
, real_type
, op0
, real
);
4793 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
4799 real
= fold_if_not_in_template (real
);
4800 imag
= fold_if_not_in_template (imag
);
4801 result
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
4802 result
= fold_if_not_in_template (result
);
4806 /* For certain operations (which identify themselves by shorten != 0)
4807 if both args were extended from the same smaller type,
4808 do the arithmetic in that type and then extend.
4810 shorten !=0 and !=1 indicates a bitwise operation.
4811 For them, this optimization is safe only if
4812 both args are zero-extended or both are sign-extended.
4813 Otherwise, we might change the result.
4814 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4815 but calculated in (unsigned short) it would be (unsigned short)-1. */
4817 if (shorten
&& none_complex
)
4819 orig_type
= result_type
;
4820 final_type
= result_type
;
4821 result_type
= shorten_binary_op (result_type
, op0
, op1
,
4825 /* Comparison operations are shortened too but differently.
4826 They identify themselves by setting short_compare = 1. */
4830 /* Don't write &op0, etc., because that would prevent op0
4831 from being kept in a register.
4832 Instead, make copies of the our local variables and
4833 pass the copies by reference, then copy them back afterward. */
4834 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
4835 enum tree_code xresultcode
= resultcode
;
4837 = shorten_compare (&xop0
, &xop1
, &xresult_type
, &xresultcode
);
4839 return cp_convert (boolean_type_node
, val
, complain
);
4840 op0
= xop0
, op1
= xop1
;
4842 resultcode
= xresultcode
;
4845 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
4846 && warn_sign_compare
4847 /* Do not warn until the template is instantiated; we cannot
4848 bound the ranges of the arguments until that point. */
4849 && !processing_template_decl
4850 && (complain
& tf_warning
)
4851 && c_inhibit_evaluation_warnings
== 0
4852 /* Even unsigned enum types promote to signed int. We don't
4853 want to issue -Wsign-compare warnings for this case. */
4854 && !enum_cast_to_int (orig_op0
)
4855 && !enum_cast_to_int (orig_op1
))
4857 tree oop0
= maybe_constant_value (orig_op0
);
4858 tree oop1
= maybe_constant_value (orig_op1
);
4860 if (TREE_CODE (oop0
) != INTEGER_CST
)
4862 if (TREE_CODE (oop1
) != INTEGER_CST
)
4864 warn_for_sign_compare (location
, oop0
, oop1
, op0
, op1
,
4865 result_type
, resultcode
);
4869 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4870 Then the expression will be built.
4871 It will be given type FINAL_TYPE if that is nonzero;
4872 otherwise, it will be given type RESULT_TYPE. */
4875 if (TREE_TYPE (op0
) != result_type
)
4876 op0
= cp_convert_and_check (result_type
, op0
, complain
);
4877 if (TREE_TYPE (op1
) != result_type
)
4878 op1
= cp_convert_and_check (result_type
, op1
, complain
);
4880 if (op0
== error_mark_node
|| op1
== error_mark_node
)
4881 return error_mark_node
;
4884 if (build_type
== NULL_TREE
)
4885 build_type
= result_type
;
4887 if ((flag_sanitize
& SANITIZE_UNDEFINED
)
4888 && !processing_template_decl
4889 && current_function_decl
!= 0
4890 && (doing_div_or_mod
|| doing_shift
))
4892 /* OP0 and/or OP1 might have side-effects. */
4893 op0
= cp_save_expr (op0
);
4894 op1
= cp_save_expr (op1
);
4895 op0
= maybe_constant_value (fold_non_dependent_expr_sfinae (op0
,
4897 op1
= maybe_constant_value (fold_non_dependent_expr_sfinae (op1
,
4899 if (doing_div_or_mod
)
4901 /* For diagnostics we want to use the promoted types without
4902 shorten_binary_op. So convert the arguments to the
4903 original result_type. */
4906 if (orig_type
!= NULL
&& result_type
!= orig_type
)
4908 cop0
= cp_convert (orig_type
, op0
, complain
);
4909 cop1
= cp_convert (orig_type
, op1
, complain
);
4911 instrument_expr
= ubsan_instrument_division (location
, cop0
, cop1
);
4913 else if (doing_shift
)
4914 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
4917 result
= build2 (resultcode
, build_type
, op0
, op1
);
4918 result
= fold_if_not_in_template (result
);
4919 if (final_type
!= 0)
4920 result
= cp_convert (final_type
, result
, complain
);
4922 if (TREE_OVERFLOW_P (result
)
4923 && !TREE_OVERFLOW_P (op0
)
4924 && !TREE_OVERFLOW_P (op1
))
4925 overflow_warning (location
, result
);
4927 if ((flag_sanitize
& SANITIZE_UNDEFINED
) && instrument_expr
!= NULL
)
4928 result
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
4929 instrument_expr
, result
);
4934 /* Build a VEC_PERM_EXPR.
4935 This is a simple wrapper for c_build_vec_perm_expr. */
4937 build_x_vec_perm_expr (location_t loc
,
4938 tree arg0
, tree arg1
, tree arg2
,
4939 tsubst_flags_t complain
)
4941 if (processing_template_decl
4942 && (type_dependent_expression_p (arg0
)
4943 || type_dependent_expression_p (arg1
)
4944 || type_dependent_expression_p (arg2
)))
4945 return build_min_nt_loc (loc
, VEC_PERM_EXPR
, arg0
, arg1
, arg2
);
4946 return c_build_vec_perm_expr (loc
, arg0
, arg1
, arg2
, complain
& tf_error
);
4949 /* Return a tree for the sum or difference (RESULTCODE says which)
4950 of pointer PTROP and integer INTOP. */
4953 cp_pointer_int_sum (enum tree_code resultcode
, tree ptrop
, tree intop
,
4954 tsubst_flags_t complain
)
4956 tree res_type
= TREE_TYPE (ptrop
);
4958 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
4959 in certain circumstance (when it's valid to do so). So we need
4960 to make sure it's complete. We don't need to check here, if we
4961 can actually complete it at all, as those checks will be done in
4962 pointer_int_sum() anyway. */
4963 complete_type (TREE_TYPE (res_type
));
4965 return pointer_int_sum (input_location
, resultcode
, ptrop
,
4966 fold_if_not_in_template (intop
),
4967 complain
& tf_warning_or_error
);
4970 /* Return a tree for the difference of pointers OP0 and OP1.
4971 The resulting tree has type int. */
4974 pointer_diff (tree op0
, tree op1
, tree ptrtype
, tsubst_flags_t complain
)
4977 tree restype
= ptrdiff_type_node
;
4978 tree target_type
= TREE_TYPE (ptrtype
);
4980 if (!complete_type_or_else (target_type
, NULL_TREE
))
4981 return error_mark_node
;
4983 if (VOID_TYPE_P (target_type
))
4985 if (complain
& tf_error
)
4986 permerror (input_location
, "ISO C++ forbids using pointer of "
4987 "type %<void *%> in subtraction");
4989 return error_mark_node
;
4991 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
4993 if (complain
& tf_error
)
4994 permerror (input_location
, "ISO C++ forbids using pointer to "
4995 "a function in subtraction");
4997 return error_mark_node
;
4999 if (TREE_CODE (target_type
) == METHOD_TYPE
)
5001 if (complain
& tf_error
)
5002 permerror (input_location
, "ISO C++ forbids using pointer to "
5003 "a method in subtraction");
5005 return error_mark_node
;
5008 /* First do the subtraction as integers;
5009 then drop through to build the divide operator. */
5011 op0
= cp_build_binary_op (input_location
,
5013 cp_convert (restype
, op0
, complain
),
5014 cp_convert (restype
, op1
, complain
),
5017 /* This generates an error if op1 is a pointer to an incomplete type. */
5018 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
5020 if (complain
& tf_error
)
5021 error ("invalid use of a pointer to an incomplete type in "
5022 "pointer arithmetic");
5024 return error_mark_node
;
5027 op1
= (TYPE_PTROB_P (ptrtype
)
5028 ? size_in_bytes (target_type
)
5029 : integer_one_node
);
5031 /* Do the division. */
5033 result
= build2 (EXACT_DIV_EXPR
, restype
, op0
,
5034 cp_convert (restype
, op1
, complain
));
5035 return fold_if_not_in_template (result
);
5038 /* Construct and perhaps optimize a tree representation
5039 for a unary operation. CODE, a tree_code, specifies the operation
5040 and XARG is the operand. */
5043 build_x_unary_op (location_t loc
, enum tree_code code
, tree xarg
,
5044 tsubst_flags_t complain
)
5046 tree orig_expr
= xarg
;
5050 if (processing_template_decl
)
5052 if (type_dependent_expression_p (xarg
))
5053 return build_min_nt_loc (loc
, code
, xarg
, NULL_TREE
);
5055 xarg
= build_non_dependent_expr (xarg
);
5060 /* [expr.unary.op] says:
5062 The address of an object of incomplete type can be taken.
5064 (And is just the ordinary address operator, not an overloaded
5065 "operator &".) However, if the type is a template
5066 specialization, we must complete the type at this point so that
5067 an overloaded "operator &" will be available if required. */
5068 if (code
== ADDR_EXPR
5069 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
5070 && ((CLASS_TYPE_P (TREE_TYPE (xarg
))
5071 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg
))))
5072 || (TREE_CODE (xarg
) == OFFSET_REF
)))
5073 /* Don't look for a function. */;
5075 exp
= build_new_op (loc
, code
, LOOKUP_NORMAL
, xarg
, NULL_TREE
,
5076 NULL_TREE
, /*overload=*/NULL
, complain
);
5077 if (!exp
&& code
== ADDR_EXPR
)
5079 if (is_overloaded_fn (xarg
))
5081 tree fn
= get_first_fn (xarg
);
5082 if (DECL_CONSTRUCTOR_P (fn
) || DECL_DESTRUCTOR_P (fn
))
5084 if (complain
& tf_error
)
5085 error (DECL_CONSTRUCTOR_P (fn
)
5086 ? G_("taking address of constructor %qE")
5087 : G_("taking address of destructor %qE"),
5089 return error_mark_node
;
5093 /* A pointer to member-function can be formed only by saying
5095 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
5096 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
5098 if (TREE_CODE (xarg
) != OFFSET_REF
5099 || !TYPE_P (TREE_OPERAND (xarg
, 0)))
5101 if (complain
& tf_error
)
5103 error ("invalid use of %qE to form a "
5104 "pointer-to-member-function", xarg
);
5105 if (TREE_CODE (xarg
) != OFFSET_REF
)
5106 inform (input_location
, " a qualified-id is required");
5108 return error_mark_node
;
5112 if (complain
& tf_error
)
5113 error ("parentheses around %qE cannot be used to form a"
5114 " pointer-to-member-function",
5117 return error_mark_node
;
5118 PTRMEM_OK_P (xarg
) = 1;
5122 if (TREE_CODE (xarg
) == OFFSET_REF
)
5124 ptrmem
= PTRMEM_OK_P (xarg
);
5126 if (!ptrmem
&& !flag_ms_extensions
5127 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
5129 /* A single non-static member, make sure we don't allow a
5130 pointer-to-member. */
5131 xarg
= build2 (OFFSET_REF
, TREE_TYPE (xarg
),
5132 TREE_OPERAND (xarg
, 0),
5133 ovl_cons (TREE_OPERAND (xarg
, 1), NULL_TREE
));
5134 PTRMEM_OK_P (xarg
) = ptrmem
;
5138 exp
= cp_build_addr_expr_strict (xarg
, complain
);
5141 if (processing_template_decl
&& exp
!= error_mark_node
)
5142 exp
= build_min_non_dep (code
, exp
, orig_expr
,
5143 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE
);
5144 if (TREE_CODE (exp
) == ADDR_EXPR
)
5145 PTRMEM_OK_P (exp
) = ptrmem
;
5149 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5150 constants, where a null value is represented by an INTEGER_CST of
5154 cp_truthvalue_conversion (tree expr
)
5156 tree type
= TREE_TYPE (expr
);
5157 if (TYPE_PTRDATAMEM_P (type
))
5158 return build_binary_op (EXPR_LOCATION (expr
),
5159 NE_EXPR
, expr
, nullptr_node
, 1);
5160 else if (TYPE_PTR_P (type
) || TYPE_PTRMEMFUNC_P (type
))
5162 /* With -Wzero-as-null-pointer-constant do not warn for an
5163 'if (p)' or a 'while (!p)', where p is a pointer. */
5165 ++c_inhibit_evaluation_warnings
;
5166 ret
= c_common_truthvalue_conversion (input_location
, expr
);
5167 --c_inhibit_evaluation_warnings
;
5171 return c_common_truthvalue_conversion (input_location
, expr
);
5174 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5177 condition_conversion (tree expr
)
5180 if (processing_template_decl
)
5182 t
= perform_implicit_conversion_flags (boolean_type_node
, expr
,
5183 tf_warning_or_error
, LOOKUP_NORMAL
);
5184 t
= fold_build_cleanup_point_expr (boolean_type_node
, t
);
5188 /* Returns the address of T. This function will fold away
5189 ADDR_EXPR of INDIRECT_REF. */
5192 build_address (tree t
)
5194 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
5195 return error_mark_node
;
5196 t
= build_fold_addr_expr (t
);
5197 if (TREE_CODE (t
) != ADDR_EXPR
)
5202 /* Returns the address of T with type TYPE. */
5205 build_typed_address (tree t
, tree type
)
5207 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
5208 return error_mark_node
;
5209 t
= build_fold_addr_expr_with_type (t
, type
);
5210 if (TREE_CODE (t
) != ADDR_EXPR
)
5215 /* Return a NOP_EXPR converting EXPR to TYPE. */
5218 build_nop (tree type
, tree expr
)
5220 if (type
== error_mark_node
|| error_operand_p (expr
))
5222 return build1 (NOP_EXPR
, type
, expr
);
5225 /* Take the address of ARG, whatever that means under C++ semantics.
5226 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5227 and class rvalues as well.
5229 Nothing should call this function directly; instead, callers should use
5230 cp_build_addr_expr or cp_build_addr_expr_strict. */
5233 cp_build_addr_expr_1 (tree arg
, bool strict_lvalue
, tsubst_flags_t complain
)
5238 if (!arg
|| error_operand_p (arg
))
5239 return error_mark_node
;
5241 arg
= mark_lvalue_use (arg
);
5242 argtype
= lvalue_type (arg
);
5244 gcc_assert (!identifier_p (arg
) || !IDENTIFIER_OPNAME_P (arg
));
5246 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
5247 && !really_overloaded_fn (TREE_OPERAND (arg
, 1)))
5249 /* They're trying to take the address of a unique non-static
5250 member function. This is ill-formed (except in MS-land),
5251 but let's try to DTRT.
5252 Note: We only handle unique functions here because we don't
5253 want to complain if there's a static overload; non-unique
5254 cases will be handled by instantiate_type. But we need to
5255 handle this case here to allow casts on the resulting PMF.
5256 We could defer this in non-MS mode, but it's easier to give
5257 a useful error here. */
5259 /* Inside constant member functions, the `this' pointer
5260 contains an extra const qualifier. TYPE_MAIN_VARIANT
5261 is used here to remove this const from the diagnostics
5262 and the created OFFSET_REF. */
5263 tree base
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg
, 0)));
5264 tree fn
= get_first_fn (TREE_OPERAND (arg
, 1));
5267 if (! flag_ms_extensions
)
5269 tree name
= DECL_NAME (fn
);
5270 if (!(complain
& tf_error
))
5271 return error_mark_node
;
5272 else if (current_class_type
5273 && TREE_OPERAND (arg
, 0) == current_class_ref
)
5274 /* An expression like &memfn. */
5275 permerror (input_location
, "ISO C++ forbids taking the address of an unqualified"
5276 " or parenthesized non-static member function to form"
5277 " a pointer to member function. Say %<&%T::%D%>",
5280 permerror (input_location
, "ISO C++ forbids taking the address of a bound member"
5281 " function to form a pointer to member function."
5285 arg
= build_offset_ref (base
, fn
, /*address_p=*/true, complain
);
5288 /* Uninstantiated types are all functions. Taking the
5289 address of a function is a no-op, so just return the
5291 if (type_unknown_p (arg
))
5292 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
5294 if (TREE_CODE (arg
) == OFFSET_REF
)
5295 /* We want a pointer to member; bypass all the code for actually taking
5296 the address of something. */
5299 /* Anything not already handled and not a true memory reference
5301 if (TREE_CODE (argtype
) != FUNCTION_TYPE
5302 && TREE_CODE (argtype
) != METHOD_TYPE
)
5304 cp_lvalue_kind kind
= lvalue_kind (arg
);
5305 if (kind
== clk_none
)
5307 if (complain
& tf_error
)
5308 lvalue_error (input_location
, lv_addressof
);
5309 return error_mark_node
;
5311 if (strict_lvalue
&& (kind
& (clk_rvalueref
|clk_class
)))
5313 if (!(complain
& tf_error
))
5314 return error_mark_node
;
5315 if (kind
& clk_class
)
5316 /* Make this a permerror because we used to accept it. */
5317 permerror (input_location
, "taking address of temporary");
5319 error ("taking address of xvalue (rvalue reference)");
5323 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
5325 tree type
= build_pointer_type (TREE_TYPE (argtype
));
5326 arg
= build1 (CONVERT_EXPR
, type
, arg
);
5329 else if (pedantic
&& DECL_MAIN_P (arg
))
5332 /* Apparently a lot of autoconf scripts for C++ packages do this,
5333 so only complain if -Wpedantic. */
5334 if (complain
& (flag_pedantic_errors
? tf_error
: tf_warning
))
5335 pedwarn (input_location
, OPT_Wpedantic
,
5336 "ISO C++ forbids taking address of function %<::main%>");
5337 else if (flag_pedantic_errors
)
5338 return error_mark_node
;
5341 /* Let &* cancel out to simplify resulting code. */
5342 if (INDIRECT_REF_P (arg
))
5344 /* We don't need to have `current_class_ptr' wrapped in a
5345 NON_LVALUE_EXPR node. */
5346 if (arg
== current_class_ref
)
5347 return current_class_ptr
;
5349 arg
= TREE_OPERAND (arg
, 0);
5350 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
5352 tree type
= build_pointer_type (TREE_TYPE (TREE_TYPE (arg
)));
5353 arg
= build1 (CONVERT_EXPR
, type
, arg
);
5356 /* Don't let this be an lvalue. */
5361 /* ??? Cope with user tricks that amount to offsetof. */
5362 if (TREE_CODE (argtype
) != FUNCTION_TYPE
5363 && TREE_CODE (argtype
) != METHOD_TYPE
5364 && argtype
!= unknown_type_node
5365 && (val
= get_base_address (arg
))
5366 && COMPLETE_TYPE_P (TREE_TYPE (val
))
5367 && INDIRECT_REF_P (val
)
5368 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
5370 tree type
= build_pointer_type (argtype
);
5371 return fold_convert (type
, fold_offsetof_1 (arg
));
5374 /* Handle complex lvalues (when permitted)
5375 by reduction to simpler cases. */
5376 val
= unary_complex_lvalue (ADDR_EXPR
, arg
);
5380 switch (TREE_CODE (arg
))
5384 case FIX_TRUNC_EXPR
:
5385 /* Even if we're not being pedantic, we cannot allow this
5386 extension when we're instantiating in a SFINAE
5388 if (! lvalue_p (arg
) && complain
== tf_none
)
5390 if (complain
& tf_error
)
5391 permerror (input_location
, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5393 return error_mark_node
;
5398 arg
= BASELINK_FUNCTIONS (arg
);
5402 arg
= OVL_CURRENT (arg
);
5407 /* Turn a reference to a non-static data member into a
5408 pointer-to-member. */
5413 gcc_assert (PTRMEM_OK_P (arg
));
5415 t
= TREE_OPERAND (arg
, 1);
5416 if (TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
5418 if (complain
& tf_error
)
5419 error ("cannot create pointer to reference member %qD", t
);
5420 return error_mark_node
;
5423 type
= build_ptrmem_type (context_for_name_lookup (t
),
5425 t
= make_ptrmem_cst (type
, TREE_OPERAND (arg
, 1));
5433 if (argtype
!= error_mark_node
)
5435 if (cxx_dialect
>= cxx1y
&& array_of_runtime_bound_p (argtype
))
5437 if (complain
& tf_warning_or_error
)
5438 pedwarn (input_location
, OPT_Wvla
,
5439 "taking address of array of runtime bound");
5441 return error_mark_node
;
5443 argtype
= build_pointer_type (argtype
);
5446 /* In a template, we are processing a non-dependent expression
5447 so we can just form an ADDR_EXPR with the correct type. */
5448 if (processing_template_decl
|| TREE_CODE (arg
) != COMPONENT_REF
)
5450 val
= build_address (arg
);
5451 if (TREE_CODE (arg
) == OFFSET_REF
)
5452 PTRMEM_OK_P (val
) = PTRMEM_OK_P (arg
);
5454 else if (BASELINK_P (TREE_OPERAND (arg
, 1)))
5456 tree fn
= BASELINK_FUNCTIONS (TREE_OPERAND (arg
, 1));
5458 /* We can only get here with a single static member
5460 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
5461 && DECL_STATIC_FUNCTION_P (fn
));
5463 val
= build_address (fn
);
5464 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
5465 /* Do not lose object's side effects. */
5466 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
),
5467 TREE_OPERAND (arg
, 0), val
);
5469 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)))
5471 if (complain
& tf_error
)
5472 error ("attempt to take address of bit-field structure member %qD",
5473 TREE_OPERAND (arg
, 1));
5474 return error_mark_node
;
5478 tree object
= TREE_OPERAND (arg
, 0);
5479 tree field
= TREE_OPERAND (arg
, 1);
5480 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5481 (TREE_TYPE (object
), decl_type_context (field
)));
5482 val
= build_address (arg
);
5485 if (TYPE_PTR_P (argtype
)
5486 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
5488 build_ptrmemfunc_type (argtype
);
5489 val
= build_ptrmemfunc (argtype
, val
, 0,
5497 /* Take the address of ARG if it has one, even if it's an rvalue. */
5500 cp_build_addr_expr (tree arg
, tsubst_flags_t complain
)
5502 return cp_build_addr_expr_1 (arg
, 0, complain
);
5505 /* Take the address of ARG, but only if it's an lvalue. */
5508 cp_build_addr_expr_strict (tree arg
, tsubst_flags_t complain
)
5510 return cp_build_addr_expr_1 (arg
, 1, complain
);
5513 /* C++: Must handle pointers to members.
5515 Perhaps type instantiation should be extended to handle conversion
5516 from aggregates to types we don't yet know we want? (Or are those
5517 cases typically errors which should be reported?)
5519 NOCONVERT nonzero suppresses the default promotions
5520 (such as from short to int). */
5523 cp_build_unary_op (enum tree_code code
, tree xarg
, int noconvert
,
5524 tsubst_flags_t complain
)
5526 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5529 const char *errstring
= NULL
;
5531 const char *invalid_op_diag
;
5533 if (!arg
|| error_operand_p (arg
))
5534 return error_mark_node
;
5536 if ((invalid_op_diag
5537 = targetm
.invalid_unary_op ((code
== UNARY_PLUS_EXPR
5542 if (complain
& tf_error
)
5543 error (invalid_op_diag
);
5544 return error_mark_node
;
5549 case UNARY_PLUS_EXPR
:
5552 int flags
= WANT_ARITH
| WANT_ENUM
;
5553 /* Unary plus (but not unary minus) is allowed on pointers. */
5554 if (code
== UNARY_PLUS_EXPR
)
5555 flags
|= WANT_POINTER
;
5556 arg
= build_expr_type_conversion (flags
, arg
, true);
5558 errstring
= (code
== NEGATE_EXPR
5559 ? _("wrong type argument to unary minus")
5560 : _("wrong type argument to unary plus"));
5563 if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
5564 arg
= cp_perform_integral_promotions (arg
, complain
);
5566 /* Make sure the result is not an lvalue: a unary plus or minus
5567 expression is always a rvalue. */
5574 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
5579 arg
= cp_default_conversion (arg
, complain
);
5580 if (arg
== error_mark_node
)
5581 return error_mark_node
;
5584 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
5585 | WANT_VECTOR_OR_COMPLEX
,
5587 errstring
= _("wrong type argument to bit-complement");
5588 else if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
5589 arg
= cp_perform_integral_promotions (arg
, complain
);
5593 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
5594 errstring
= _("wrong type argument to abs");
5595 else if (!noconvert
)
5597 arg
= cp_default_conversion (arg
, complain
);
5598 if (arg
== error_mark_node
)
5599 return error_mark_node
;
5604 /* Conjugating a real value is a no-op, but allow it anyway. */
5605 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
5606 errstring
= _("wrong type argument to conjugation");
5607 else if (!noconvert
)
5609 arg
= cp_default_conversion (arg
, complain
);
5610 if (arg
== error_mark_node
)
5611 return error_mark_node
;
5615 case TRUTH_NOT_EXPR
:
5616 arg
= perform_implicit_conversion (boolean_type_node
, arg
,
5618 val
= invert_truthvalue_loc (input_location
, arg
);
5619 if (arg
!= error_mark_node
)
5621 errstring
= _("in argument to unary !");
5629 arg
= build_real_imag_expr (input_location
, code
, arg
);
5630 if (arg
== error_mark_node
)
5633 return fold_if_not_in_template (arg
);
5635 case PREINCREMENT_EXPR
:
5636 case POSTINCREMENT_EXPR
:
5637 case PREDECREMENT_EXPR
:
5638 case POSTDECREMENT_EXPR
:
5639 /* Handle complex lvalues (when permitted)
5640 by reduction to simpler cases. */
5642 val
= unary_complex_lvalue (code
, arg
);
5646 arg
= mark_lvalue_use (arg
);
5648 /* Increment or decrement the real part of the value,
5649 and don't change the imaginary part. */
5650 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
5654 arg
= stabilize_reference (arg
);
5655 real
= cp_build_unary_op (REALPART_EXPR
, arg
, 1, complain
);
5656 imag
= cp_build_unary_op (IMAGPART_EXPR
, arg
, 1, complain
);
5657 real
= cp_build_unary_op (code
, real
, 1, complain
);
5658 if (real
== error_mark_node
|| imag
== error_mark_node
)
5659 return error_mark_node
;
5660 return build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
5664 /* Report invalid types. */
5666 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
5669 if (code
== PREINCREMENT_EXPR
)
5670 errstring
= _("no pre-increment operator for type");
5671 else if (code
== POSTINCREMENT_EXPR
)
5672 errstring
= _("no post-increment operator for type");
5673 else if (code
== PREDECREMENT_EXPR
)
5674 errstring
= _("no pre-decrement operator for type");
5676 errstring
= _("no post-decrement operator for type");
5679 else if (arg
== error_mark_node
)
5680 return error_mark_node
;
5682 /* Report something read-only. */
5684 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
5685 || TREE_READONLY (arg
))
5687 if (complain
& tf_error
)
5688 cxx_readonly_error (arg
, ((code
== PREINCREMENT_EXPR
5689 || code
== POSTINCREMENT_EXPR
)
5690 ? lv_increment
: lv_decrement
));
5692 return error_mark_node
;
5697 tree declared_type
= unlowered_expr_type (arg
);
5699 argtype
= TREE_TYPE (arg
);
5701 /* ARM $5.2.5 last annotation says this should be forbidden. */
5702 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
5704 if (complain
& tf_error
)
5705 permerror (input_location
, (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
5706 ? G_("ISO C++ forbids incrementing an enum")
5707 : G_("ISO C++ forbids decrementing an enum"));
5709 return error_mark_node
;
5712 /* Compute the increment. */
5714 if (TYPE_PTR_P (argtype
))
5716 tree type
= complete_type (TREE_TYPE (argtype
));
5718 if (!COMPLETE_OR_VOID_TYPE_P (type
))
5720 if (complain
& tf_error
)
5721 error (((code
== PREINCREMENT_EXPR
5722 || code
== POSTINCREMENT_EXPR
))
5723 ? G_("cannot increment a pointer to incomplete type %qT")
5724 : G_("cannot decrement a pointer to incomplete type %qT"),
5725 TREE_TYPE (argtype
));
5727 return error_mark_node
;
5729 else if (!TYPE_PTROB_P (argtype
))
5731 if (complain
& tf_error
)
5732 pedwarn (input_location
, OPT_Wpointer_arith
,
5733 (code
== PREINCREMENT_EXPR
5734 || code
== POSTINCREMENT_EXPR
)
5735 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5736 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5739 return error_mark_node
;
5742 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
5745 inc
= integer_one_node
;
5747 inc
= cp_convert (argtype
, inc
, complain
);
5749 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5750 need to ask Objective-C to build the increment or decrement
5751 expression for it. */
5752 if (objc_is_property_ref (arg
))
5753 return objc_build_incr_expr_for_property_ref (input_location
, code
,
5756 /* Complain about anything else that is not a true lvalue. */
5757 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
5758 || code
== POSTINCREMENT_EXPR
)
5759 ? lv_increment
: lv_decrement
),
5761 return error_mark_node
;
5763 /* Forbid using -- on `bool'. */
5764 if (TREE_CODE (declared_type
) == BOOLEAN_TYPE
)
5766 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
5768 if (complain
& tf_error
)
5769 error ("invalid use of Boolean expression as operand "
5770 "to %<operator--%>");
5771 return error_mark_node
;
5773 val
= boolean_increment (code
, arg
);
5775 else if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
5776 /* An rvalue has no cv-qualifiers. */
5777 val
= build2 (code
, cv_unqualified (TREE_TYPE (arg
)), arg
, inc
);
5779 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
5781 TREE_SIDE_EFFECTS (val
) = 1;
5786 /* Note that this operation never does default_conversion
5787 regardless of NOCONVERT. */
5788 return cp_build_addr_expr (arg
, complain
);
5797 argtype
= TREE_TYPE (arg
);
5798 return fold_if_not_in_template (build1 (code
, argtype
, arg
));
5801 if (complain
& tf_error
)
5802 error ("%s", errstring
);
5803 return error_mark_node
;
5806 /* Hook for the c-common bits that build a unary op. */
5808 build_unary_op (location_t
/*location*/,
5809 enum tree_code code
, tree xarg
, int noconvert
)
5811 return cp_build_unary_op (code
, xarg
, noconvert
, tf_warning_or_error
);
5814 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5815 for certain kinds of expressions which are not really lvalues
5816 but which we can accept as lvalues.
5818 If ARG is not a kind of expression we can handle, return
5822 unary_complex_lvalue (enum tree_code code
, tree arg
)
5824 /* Inside a template, making these kinds of adjustments is
5825 pointless; we are only concerned with the type of the
5827 if (processing_template_decl
)
5830 /* Handle (a, b) used as an "lvalue". */
5831 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
5833 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 1), 0,
5834 tf_warning_or_error
);
5835 return build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
5836 TREE_OPERAND (arg
, 0), real_result
);
5839 /* Handle (a ? b : c) used as an "lvalue". */
5840 if (TREE_CODE (arg
) == COND_EXPR
5841 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
5842 return rationalize_conditional_expr (code
, arg
, tf_warning_or_error
);
5844 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5845 if (TREE_CODE (arg
) == MODIFY_EXPR
5846 || TREE_CODE (arg
) == PREINCREMENT_EXPR
5847 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
5849 tree lvalue
= TREE_OPERAND (arg
, 0);
5850 if (TREE_SIDE_EFFECTS (lvalue
))
5852 lvalue
= stabilize_reference (lvalue
);
5853 arg
= build2 (TREE_CODE (arg
), TREE_TYPE (arg
),
5854 lvalue
, TREE_OPERAND (arg
, 1));
5856 return unary_complex_lvalue
5857 (code
, build2 (COMPOUND_EXPR
, TREE_TYPE (lvalue
), arg
, lvalue
));
5860 if (code
!= ADDR_EXPR
)
5863 /* Handle (a = b) used as an "lvalue" for `&'. */
5864 if (TREE_CODE (arg
) == MODIFY_EXPR
5865 || TREE_CODE (arg
) == INIT_EXPR
)
5867 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 0), 0,
5868 tf_warning_or_error
);
5869 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
5871 TREE_NO_WARNING (arg
) = 1;
5875 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
5876 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
5877 || TREE_CODE (arg
) == OFFSET_REF
)
5880 /* We permit compiler to make function calls returning
5881 objects of aggregate type look like lvalues. */
5885 if (TREE_CODE (targ
) == SAVE_EXPR
)
5886 targ
= TREE_OPERAND (targ
, 0);
5888 if (TREE_CODE (targ
) == CALL_EXPR
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (targ
)))
5890 if (TREE_CODE (arg
) == SAVE_EXPR
)
5893 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, tf_warning_or_error
);
5894 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
5897 if (TREE_CODE (arg
) == SAVE_EXPR
&& INDIRECT_REF_P (targ
))
5898 return build3 (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
5899 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
5902 /* Don't let anything else be handled specially. */
5906 /* Mark EXP saying that we need to be able to take the
5907 address of it; it should not be allocated in a register.
5908 Value is true if successful.
5910 C++: we do not allow `current_class_ptr' to be addressable. */
5913 cxx_mark_addressable (tree exp
)
5918 switch (TREE_CODE (x
))
5925 x
= TREE_OPERAND (x
, 0);
5929 if (x
== current_class_ptr
)
5931 error ("cannot take the address of %<this%>, which is an rvalue expression");
5932 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later. */
5938 /* Caller should not be trying to mark initialized
5939 constant fields addressable. */
5940 gcc_assert (DECL_LANG_SPECIFIC (x
) == 0
5941 || DECL_IN_AGGR_P (x
) == 0
5943 || DECL_EXTERNAL (x
));
5947 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
5948 && !DECL_ARTIFICIAL (x
))
5950 if (VAR_P (x
) && DECL_HARD_REGISTER (x
))
5953 ("address of explicit register variable %qD requested", x
);
5956 else if (extra_warnings
)
5958 (OPT_Wextra
, "address requested for %qD, which is declared %<register%>", x
);
5960 TREE_ADDRESSABLE (x
) = 1;
5965 TREE_ADDRESSABLE (x
) = 1;
5969 TREE_ADDRESSABLE (x
) = 1;
5973 TREE_ADDRESSABLE (x
) = 1;
5974 cxx_mark_addressable (TREE_OPERAND (x
, 0));
5982 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
5985 build_x_conditional_expr (location_t loc
, tree ifexp
, tree op1
, tree op2
,
5986 tsubst_flags_t complain
)
5988 tree orig_ifexp
= ifexp
;
5989 tree orig_op1
= op1
;
5990 tree orig_op2
= op2
;
5993 if (processing_template_decl
)
5995 /* The standard says that the expression is type-dependent if
5996 IFEXP is type-dependent, even though the eventual type of the
5997 expression doesn't dependent on IFEXP. */
5998 if (type_dependent_expression_p (ifexp
)
5999 /* As a GNU extension, the middle operand may be omitted. */
6000 || (op1
&& type_dependent_expression_p (op1
))
6001 || type_dependent_expression_p (op2
))
6002 return build_min_nt_loc (loc
, COND_EXPR
, ifexp
, op1
, op2
);
6003 ifexp
= build_non_dependent_expr (ifexp
);
6005 op1
= build_non_dependent_expr (op1
);
6006 op2
= build_non_dependent_expr (op2
);
6009 expr
= build_conditional_expr (loc
, ifexp
, op1
, op2
, complain
);
6010 if (processing_template_decl
&& expr
!= error_mark_node
6011 && TREE_CODE (expr
) != VEC_COND_EXPR
)
6013 tree min
= build_min_non_dep (COND_EXPR
, expr
,
6014 orig_ifexp
, orig_op1
, orig_op2
);
6015 /* In C++11, remember that the result is an lvalue or xvalue.
6016 In C++98, lvalue_kind can just assume lvalue in a template. */
6017 if (cxx_dialect
>= cxx11
6018 && lvalue_or_rvalue_with_address_p (expr
)
6019 && !lvalue_or_rvalue_with_address_p (min
))
6020 TREE_TYPE (min
) = cp_build_reference_type (TREE_TYPE (min
),
6021 !real_lvalue_p (expr
));
6022 expr
= convert_from_reference (min
);
6027 /* Given a list of expressions, return a compound expression
6028 that performs them all and returns the value of the last of them. */
6031 build_x_compound_expr_from_list (tree list
, expr_list_kind exp
,
6032 tsubst_flags_t complain
)
6034 tree expr
= TREE_VALUE (list
);
6036 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
6037 && !CONSTRUCTOR_IS_DIRECT_INIT (expr
))
6039 if (complain
& tf_error
)
6040 pedwarn (EXPR_LOC_OR_HERE (expr
), 0, "list-initializer for "
6041 "non-class type must not be parenthesized");
6043 return error_mark_node
;
6046 if (TREE_CHAIN (list
))
6048 if (complain
& tf_error
)
6052 permerror (input_location
, "expression list treated as compound "
6053 "expression in initializer");
6056 permerror (input_location
, "expression list treated as compound "
6057 "expression in mem-initializer");
6060 permerror (input_location
, "expression list treated as compound "
6061 "expression in functional cast");
6067 return error_mark_node
;
6069 for (list
= TREE_CHAIN (list
); list
; list
= TREE_CHAIN (list
))
6070 expr
= build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list
)),
6071 expr
, TREE_VALUE (list
), complain
);
6077 /* Like build_x_compound_expr_from_list, but using a VEC. */
6080 build_x_compound_expr_from_vec (vec
<tree
, va_gc
> *vec
, const char *msg
,
6081 tsubst_flags_t complain
)
6083 if (vec_safe_is_empty (vec
))
6085 else if (vec
->length () == 1)
6095 if (complain
& tf_error
)
6096 permerror (input_location
,
6097 "%s expression list treated as compound expression",
6100 return error_mark_node
;
6104 for (ix
= 1; vec
->iterate (ix
, &t
); ++ix
)
6105 expr
= build_x_compound_expr (EXPR_LOCATION (t
), expr
,
6112 /* Handle overloading of the ',' operator when needed. */
6115 build_x_compound_expr (location_t loc
, tree op1
, tree op2
,
6116 tsubst_flags_t complain
)
6119 tree orig_op1
= op1
;
6120 tree orig_op2
= op2
;
6122 if (processing_template_decl
)
6124 if (type_dependent_expression_p (op1
)
6125 || type_dependent_expression_p (op2
))
6126 return build_min_nt_loc (loc
, COMPOUND_EXPR
, op1
, op2
);
6127 op1
= build_non_dependent_expr (op1
);
6128 op2
= build_non_dependent_expr (op2
);
6131 result
= build_new_op (loc
, COMPOUND_EXPR
, LOOKUP_NORMAL
, op1
, op2
,
6132 NULL_TREE
, /*overload=*/NULL
, complain
);
6134 result
= cp_build_compound_expr (op1
, op2
, complain
);
6136 if (processing_template_decl
&& result
!= error_mark_node
)
6137 return build_min_non_dep (COMPOUND_EXPR
, result
, orig_op1
, orig_op2
);
6142 /* Like cp_build_compound_expr, but for the c-common bits. */
6145 build_compound_expr (location_t
/*loc*/, tree lhs
, tree rhs
)
6147 return cp_build_compound_expr (lhs
, rhs
, tf_warning_or_error
);
6150 /* Build a compound expression. */
6153 cp_build_compound_expr (tree lhs
, tree rhs
, tsubst_flags_t complain
)
6155 lhs
= convert_to_void (lhs
, ICV_LEFT_OF_COMMA
, complain
);
6157 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
6158 return error_mark_node
;
6160 if (TREE_CODE (rhs
) == TARGET_EXPR
)
6162 /* If the rhs is a TARGET_EXPR, then build the compound
6163 expression inside the target_expr's initializer. This
6164 helps the compiler to eliminate unnecessary temporaries. */
6165 tree init
= TREE_OPERAND (rhs
, 1);
6167 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), lhs
, init
);
6168 TREE_OPERAND (rhs
, 1) = init
;
6173 if (type_unknown_p (rhs
))
6175 if (complain
& tf_error
)
6176 error ("no context to resolve type of %qE", rhs
);
6177 return error_mark_node
;
6180 return build2 (COMPOUND_EXPR
, TREE_TYPE (rhs
), lhs
, rhs
);
6183 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6184 casts away constness. CAST gives the type of cast. Returns true
6185 if the cast is ill-formed, false if it is well-formed.
6187 ??? This function warns for casting away any qualifier not just
6188 const. We would like to specify exactly what qualifiers are casted
6193 check_for_casting_away_constness (tree src_type
, tree dest_type
,
6194 enum tree_code cast
, tsubst_flags_t complain
)
6196 /* C-style casts are allowed to cast away constness. With
6197 WARN_CAST_QUAL, we still want to issue a warning. */
6198 if (cast
== CAST_EXPR
&& !warn_cast_qual
)
6201 if (!casts_away_constness (src_type
, dest_type
, complain
))
6207 if (complain
& tf_warning
)
6208 warning (OPT_Wcast_qual
,
6209 "cast from type %qT to type %qT casts away qualifiers",
6210 src_type
, dest_type
);
6213 case STATIC_CAST_EXPR
:
6214 if (complain
& tf_error
)
6215 error ("static_cast from type %qT to type %qT casts away qualifiers",
6216 src_type
, dest_type
);
6219 case REINTERPRET_CAST_EXPR
:
6220 if (complain
& tf_error
)
6221 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6222 src_type
, dest_type
);
6231 Warns if the cast from expression EXPR to type TYPE is useless.
6234 maybe_warn_about_useless_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6236 if (warn_useless_cast
6237 && complain
& tf_warning
6238 && c_inhibit_evaluation_warnings
== 0)
6240 if (REFERENCE_REF_P (expr
))
6241 expr
= TREE_OPERAND (expr
, 0);
6243 if ((TREE_CODE (type
) == REFERENCE_TYPE
6244 && (TYPE_REF_IS_RVALUE (type
)
6245 ? xvalue_p (expr
) : real_lvalue_p (expr
))
6246 && same_type_p (TREE_TYPE (expr
), TREE_TYPE (type
)))
6247 || same_type_p (TREE_TYPE (expr
), type
))
6248 warning (OPT_Wuseless_cast
, "useless cast to type %qT", type
);
6252 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6253 (another pointer-to-member type in the same hierarchy) and return
6254 the converted expression. If ALLOW_INVERSE_P is permitted, a
6255 pointer-to-derived may be converted to pointer-to-base; otherwise,
6256 only the other direction is permitted. If C_CAST_P is true, this
6257 conversion is taking place as part of a C-style cast. */
6260 convert_ptrmem (tree type
, tree expr
, bool allow_inverse_p
,
6261 bool c_cast_p
, tsubst_flags_t complain
)
6263 if (TYPE_PTRDATAMEM_P (type
))
6267 if (TREE_CODE (expr
) == PTRMEM_CST
)
6268 expr
= cplus_expand_constant (expr
);
6269 delta
= get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr
)),
6270 TYPE_PTRMEM_CLASS_TYPE (type
),
6272 c_cast_p
, complain
);
6273 if (delta
== error_mark_node
)
6274 return error_mark_node
;
6276 if (!integer_zerop (delta
))
6278 tree cond
, op1
, op2
;
6280 cond
= cp_build_binary_op (input_location
,
6283 build_int_cst (TREE_TYPE (expr
), -1),
6285 op1
= build_nop (ptrdiff_type_node
, expr
);
6286 op2
= cp_build_binary_op (input_location
,
6287 PLUS_EXPR
, op1
, delta
,
6290 expr
= fold_build3_loc (input_location
,
6291 COND_EXPR
, ptrdiff_type_node
, cond
, op1
, op2
);
6295 return build_nop (type
, expr
);
6298 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
,
6299 allow_inverse_p
, c_cast_p
, complain
);
6302 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6303 this static_cast is being attempted as one of the possible casts
6304 allowed by a C-style cast. (In that case, accessibility of base
6305 classes is not considered, and it is OK to cast away
6306 constness.) Return the result of the cast. *VALID_P is set to
6307 indicate whether or not the cast was valid. */
6310 build_static_cast_1 (tree type
, tree expr
, bool c_cast_p
,
6311 bool *valid_p
, tsubst_flags_t complain
)
6317 /* Assume the cast is valid. */
6320 intype
= unlowered_expr_type (expr
);
6322 /* Save casted types in the function's used types hash table. */
6323 used_types_insert (type
);
6325 /* [expr.static.cast]
6327 An lvalue of type "cv1 B", where B is a class type, can be cast
6328 to type "reference to cv2 D", where D is a class derived (clause
6329 _class.derived_) from B, if a valid standard conversion from
6330 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6331 same cv-qualification as, or greater cv-qualification than, cv1,
6332 and B is not a virtual base class of D. */
6333 /* We check this case before checking the validity of "TYPE t =
6334 EXPR;" below because for this case:
6337 struct D : public B { D(const B&); };
6339 void f() { static_cast<const D&>(b); }
6341 we want to avoid constructing a new D. The standard is not
6342 completely clear about this issue, but our interpretation is
6343 consistent with other compilers. */
6344 if (TREE_CODE (type
) == REFERENCE_TYPE
6345 && CLASS_TYPE_P (TREE_TYPE (type
))
6346 && CLASS_TYPE_P (intype
)
6347 && (TYPE_REF_IS_RVALUE (type
) || real_lvalue_p (expr
))
6348 && DERIVED_FROM_P (intype
, TREE_TYPE (type
))
6349 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype
)),
6350 build_pointer_type (TYPE_MAIN_VARIANT
6351 (TREE_TYPE (type
))),
6354 || at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
6358 /* There is a standard conversion from "D*" to "B*" even if "B"
6359 is ambiguous or inaccessible. If this is really a
6360 static_cast, then we check both for inaccessibility and
6361 ambiguity. However, if this is a static_cast being performed
6362 because the user wrote a C-style cast, then accessibility is
6364 base
= lookup_base (TREE_TYPE (type
), intype
,
6365 c_cast_p
? ba_unique
: ba_check
,
6368 /* Convert from "B*" to "D*". This function will check that "B"
6369 is not a virtual base of "D". */
6370 expr
= build_base_path (MINUS_EXPR
, build_address (expr
),
6371 base
, /*nonnull=*/false, complain
);
6372 /* Convert the pointer to a reference -- but then remember that
6373 there are no expressions with reference type in C++.
6375 We call rvalue so that there's an actual tree code
6376 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6377 is a variable with the same type, the conversion would get folded
6378 away, leaving just the variable and causing lvalue_kind to give
6379 the wrong answer. */
6380 return convert_from_reference (rvalue (cp_fold_convert (type
, expr
)));
6383 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6384 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6385 if (TREE_CODE (type
) == REFERENCE_TYPE
6386 && TYPE_REF_IS_RVALUE (type
)
6387 && (clk
= real_lvalue_p (expr
))
6388 && reference_related_p (TREE_TYPE (type
), intype
)
6389 && (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
6391 if (clk
== clk_ordinary
)
6393 /* Handle the (non-bit-field) lvalue case here by casting to
6394 lvalue reference and then changing it to an rvalue reference.
6395 Casting an xvalue to rvalue reference will be handled by the
6397 tree lref
= cp_build_reference_type (TREE_TYPE (type
), false);
6398 result
= (perform_direct_initialization_if_possible
6399 (lref
, expr
, c_cast_p
, complain
));
6400 result
= cp_fold_convert (type
, result
);
6401 /* Make sure we don't fold back down to a named rvalue reference,
6402 because that would be an lvalue. */
6403 if (DECL_P (result
))
6404 result
= build1 (NON_LVALUE_EXPR
, type
, result
);
6405 return convert_from_reference (result
);
6408 /* For a bit-field or packed field, bind to a temporary. */
6409 expr
= rvalue (expr
);
6412 /* Resolve overloaded address here rather than once in
6413 implicit_conversion and again in the inverse code below. */
6414 if (TYPE_PTRMEMFUNC_P (type
) && type_unknown_p (expr
))
6416 expr
= instantiate_type (type
, expr
, complain
);
6417 intype
= TREE_TYPE (expr
);
6420 /* [expr.static.cast]
6422 Any expression can be explicitly converted to type cv void. */
6423 if (VOID_TYPE_P (type
))
6424 return convert_to_void (expr
, ICV_CAST
, complain
);
6427 An abstract class shall not be used ... as the type of an explicit
6429 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
6430 return error_mark_node
;
6432 /* [expr.static.cast]
6434 An expression e can be explicitly converted to a type T using a
6435 static_cast of the form static_cast<T>(e) if the declaration T
6436 t(e);" is well-formed, for some invented temporary variable
6438 result
= perform_direct_initialization_if_possible (type
, expr
,
6439 c_cast_p
, complain
);
6442 result
= convert_from_reference (result
);
6444 /* [expr.static.cast]
6446 If T is a reference type, the result is an lvalue; otherwise,
6447 the result is an rvalue. */
6448 if (TREE_CODE (type
) != REFERENCE_TYPE
)
6449 result
= rvalue (result
);
6453 /* [expr.static.cast]
6455 The inverse of any standard conversion sequence (clause _conv_),
6456 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6457 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6458 (_conv.bool_) conversions, can be performed explicitly using
6459 static_cast subject to the restriction that the explicit
6460 conversion does not cast away constness (_expr.const.cast_), and
6461 the following additional rules for specific cases: */
6462 /* For reference, the conversions not excluded are: integral
6463 promotions, floating point promotion, integral conversions,
6464 floating point conversions, floating-integral conversions,
6465 pointer conversions, and pointer to member conversions. */
6468 A value of integral _or enumeration_ type can be explicitly
6469 converted to an enumeration type. */
6470 /* The effect of all that is that any conversion between any two
6471 types which are integral, floating, or enumeration types can be
6473 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6474 || SCALAR_FLOAT_TYPE_P (type
))
6475 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
6476 || SCALAR_FLOAT_TYPE_P (intype
)))
6477 return ocp_convert (type
, expr
, CONV_C_CAST
, LOOKUP_NORMAL
, complain
);
6479 if (TYPE_PTR_P (type
) && TYPE_PTR_P (intype
)
6480 && CLASS_TYPE_P (TREE_TYPE (type
))
6481 && CLASS_TYPE_P (TREE_TYPE (intype
))
6482 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6483 (TREE_TYPE (intype
))),
6484 build_pointer_type (TYPE_MAIN_VARIANT
6485 (TREE_TYPE (type
))),
6491 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
6493 return error_mark_node
;
6494 base
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
6495 c_cast_p
? ba_unique
: ba_check
,
6497 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
6499 return cp_fold_convert(type
, expr
);
6502 if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
6503 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
6510 c1
= TYPE_PTRMEM_CLASS_TYPE (intype
);
6511 c2
= TYPE_PTRMEM_CLASS_TYPE (type
);
6513 if (TYPE_PTRDATAMEM_P (type
))
6515 t1
= (build_ptrmem_type
6517 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype
))));
6518 t2
= (build_ptrmem_type
6520 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
6527 if (can_convert (t1
, t2
, complain
) || can_convert (t2
, t1
, complain
))
6530 && check_for_casting_away_constness (intype
, type
,
6533 return error_mark_node
;
6534 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/1,
6535 c_cast_p
, complain
);
6539 /* [expr.static.cast]
6541 An rvalue of type "pointer to cv void" can be explicitly
6542 converted to a pointer to object type. A value of type pointer
6543 to object converted to "pointer to cv void" and back to the
6544 original pointer type will have its original value. */
6545 if (TYPE_PTR_P (intype
)
6546 && VOID_TYPE_P (TREE_TYPE (intype
))
6547 && TYPE_PTROB_P (type
))
6550 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
6552 return error_mark_node
;
6553 return build_nop (type
, expr
);
6557 return error_mark_node
;
6560 /* Return an expression representing static_cast<TYPE>(EXPR). */
6563 build_static_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6568 if (type
== error_mark_node
|| expr
== error_mark_node
)
6569 return error_mark_node
;
6571 if (processing_template_decl
)
6573 expr
= build_min (STATIC_CAST_EXPR
, type
, expr
);
6574 /* We don't know if it will or will not have side effects. */
6575 TREE_SIDE_EFFECTS (expr
) = 1;
6576 return convert_from_reference (expr
);
6579 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6580 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6581 if (TREE_CODE (type
) != REFERENCE_TYPE
6582 && TREE_CODE (expr
) == NOP_EXPR
6583 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
6584 expr
= TREE_OPERAND (expr
, 0);
6586 result
= build_static_cast_1 (type
, expr
, /*c_cast_p=*/false, &valid_p
,
6590 if (result
!= error_mark_node
)
6591 maybe_warn_about_useless_cast (type
, expr
, complain
);
6595 if (complain
& tf_error
)
6596 error ("invalid static_cast from type %qT to type %qT",
6597 TREE_TYPE (expr
), type
);
6598 return error_mark_node
;
6601 /* EXPR is an expression with member function or pointer-to-member
6602 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6603 not permitted by ISO C++, but we accept it in some modes. If we
6604 are not in one of those modes, issue a diagnostic. Return the
6605 converted expression. */
6608 convert_member_func_to_ptr (tree type
, tree expr
, tsubst_flags_t complain
)
6613 intype
= TREE_TYPE (expr
);
6614 gcc_assert (TYPE_PTRMEMFUNC_P (intype
)
6615 || TREE_CODE (intype
) == METHOD_TYPE
);
6617 if (!(complain
& tf_warning_or_error
))
6618 return error_mark_node
;
6620 if (pedantic
|| warn_pmf2ptr
)
6621 pedwarn (input_location
, pedantic
? OPT_Wpedantic
: OPT_Wpmf_conversions
,
6622 "converting from %qT to %qT", intype
, type
);
6624 if (TREE_CODE (intype
) == METHOD_TYPE
)
6625 expr
= build_addr_func (expr
, complain
);
6626 else if (TREE_CODE (expr
) == PTRMEM_CST
)
6627 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
6630 decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
), 0);
6631 decl
= build_address (decl
);
6632 expr
= get_member_function_from_ptrfunc (&decl
, expr
, complain
);
6635 if (expr
== error_mark_node
)
6636 return error_mark_node
;
6638 return build_nop (type
, expr
);
6641 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6642 If C_CAST_P is true, this reinterpret cast is being done as part of
6643 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6644 indicate whether or not reinterpret_cast was valid. */
6647 build_reinterpret_cast_1 (tree type
, tree expr
, bool c_cast_p
,
6648 bool *valid_p
, tsubst_flags_t complain
)
6652 /* Assume the cast is invalid. */
6656 if (type
== error_mark_node
|| error_operand_p (expr
))
6657 return error_mark_node
;
6659 intype
= TREE_TYPE (expr
);
6661 /* Save casted types in the function's used types hash table. */
6662 used_types_insert (type
);
6664 /* [expr.reinterpret.cast]
6665 An lvalue expression of type T1 can be cast to the type
6666 "reference to T2" if an expression of type "pointer to T1" can be
6667 explicitly converted to the type "pointer to T2" using a
6668 reinterpret_cast. */
6669 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6671 if (! real_lvalue_p (expr
))
6673 if (complain
& tf_error
)
6674 error ("invalid cast of an rvalue expression of type "
6677 return error_mark_node
;
6680 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6681 "B" are related class types; the reinterpret_cast does not
6682 adjust the pointer. */
6683 if (TYPE_PTR_P (intype
)
6684 && (complain
& tf_warning
)
6685 && (comptypes (TREE_TYPE (intype
), TREE_TYPE (type
),
6686 COMPARE_BASE
| COMPARE_DERIVED
)))
6687 warning (0, "casting %qT to %qT does not dereference pointer",
6690 expr
= cp_build_addr_expr (expr
, complain
);
6692 if (warn_strict_aliasing
> 2)
6693 strict_aliasing_warning (TREE_TYPE (expr
), type
, expr
);
6695 if (expr
!= error_mark_node
)
6696 expr
= build_reinterpret_cast_1
6697 (build_pointer_type (TREE_TYPE (type
)), expr
, c_cast_p
,
6699 if (expr
!= error_mark_node
)
6700 /* cp_build_indirect_ref isn't right for rvalue refs. */
6701 expr
= convert_from_reference (fold_convert (type
, expr
));
6705 /* As a G++ extension, we consider conversions from member
6706 functions, and pointers to member functions to
6707 pointer-to-function and pointer-to-void types. If
6708 -Wno-pmf-conversions has not been specified,
6709 convert_member_func_to_ptr will issue an error message. */
6710 if ((TYPE_PTRMEMFUNC_P (intype
)
6711 || TREE_CODE (intype
) == METHOD_TYPE
)
6712 && TYPE_PTR_P (type
)
6713 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
6714 || VOID_TYPE_P (TREE_TYPE (type
))))
6715 return convert_member_func_to_ptr (type
, expr
, complain
);
6717 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6718 array-to-pointer, and function-to-pointer conversions are
6720 expr
= decay_conversion (expr
, complain
);
6722 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6723 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6724 if (TREE_CODE (expr
) == NOP_EXPR
6725 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
6726 expr
= TREE_OPERAND (expr
, 0);
6728 if (error_operand_p (expr
))
6729 return error_mark_node
;
6731 intype
= TREE_TYPE (expr
);
6733 /* [expr.reinterpret.cast]
6734 A pointer can be converted to any integral type large enough to
6735 hold it. ... A value of type std::nullptr_t can be converted to
6736 an integral type; the conversion has the same meaning and
6737 validity as a conversion of (void*)0 to the integral type. */
6738 if (CP_INTEGRAL_TYPE_P (type
)
6739 && (TYPE_PTR_P (intype
) || NULLPTR_TYPE_P (intype
)))
6741 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
6743 if (complain
& tf_error
)
6744 permerror (input_location
, "cast from %qT to %qT loses precision",
6747 return error_mark_node
;
6749 if (NULLPTR_TYPE_P (intype
))
6750 return build_int_cst (type
, 0);
6752 /* [expr.reinterpret.cast]
6753 A value of integral or enumeration type can be explicitly
6754 converted to a pointer. */
6755 else if (TYPE_PTR_P (type
) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype
))
6758 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6759 || TYPE_PTR_OR_PTRMEM_P (type
))
6760 && same_type_p (type
, intype
))
6762 return fold_if_not_in_template (build_nop (type
, expr
));
6763 else if ((TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
6764 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
6765 return fold_if_not_in_template (build_nop (type
, expr
));
6766 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
6767 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
6772 && check_for_casting_away_constness (intype
, type
,
6773 REINTERPRET_CAST_EXPR
,
6775 return error_mark_node
;
6776 /* Warn about possible alignment problems. */
6777 if (STRICT_ALIGNMENT
&& warn_cast_align
6778 && (complain
& tf_warning
)
6779 && !VOID_TYPE_P (type
)
6780 && TREE_CODE (TREE_TYPE (intype
)) != FUNCTION_TYPE
6781 && COMPLETE_TYPE_P (TREE_TYPE (type
))
6782 && COMPLETE_TYPE_P (TREE_TYPE (intype
))
6783 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (intype
)))
6784 warning (OPT_Wcast_align
, "cast from %qT to %qT "
6785 "increases required alignment of target type", intype
, type
);
6787 /* We need to strip nops here, because the front end likes to
6788 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6790 if (warn_strict_aliasing
<= 2)
6791 strict_aliasing_warning (intype
, type
, sexpr
);
6793 return fold_if_not_in_template (build_nop (type
, expr
));
6795 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
6796 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
6798 if (complain
& tf_warning
)
6799 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6800 object pointer type or vice versa is conditionally-supported." */
6801 warning (OPT_Wconditionally_supported
,
6802 "casting between pointer-to-function and pointer-to-object "
6803 "is conditionally-supported");
6804 return fold_if_not_in_template (build_nop (type
, expr
));
6806 else if (TREE_CODE (type
) == VECTOR_TYPE
)
6807 return fold_if_not_in_template (convert_to_vector (type
, expr
));
6808 else if (TREE_CODE (intype
) == VECTOR_TYPE
6809 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6810 return fold_if_not_in_template (convert_to_integer (type
, expr
));
6815 if (complain
& tf_error
)
6816 error ("invalid cast from type %qT to type %qT", intype
, type
);
6817 return error_mark_node
;
6820 return cp_convert (type
, expr
, complain
);
6824 build_reinterpret_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6828 if (type
== error_mark_node
|| expr
== error_mark_node
)
6829 return error_mark_node
;
6831 if (processing_template_decl
)
6833 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
6835 if (!TREE_SIDE_EFFECTS (t
)
6836 && type_dependent_expression_p (expr
))
6837 /* There might turn out to be side effects inside expr. */
6838 TREE_SIDE_EFFECTS (t
) = 1;
6839 return convert_from_reference (t
);
6842 r
= build_reinterpret_cast_1 (type
, expr
, /*c_cast_p=*/false,
6843 /*valid_p=*/NULL
, complain
);
6844 if (r
!= error_mark_node
)
6845 maybe_warn_about_useless_cast (type
, expr
, complain
);
6849 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6850 return an appropriate expression. Otherwise, return
6851 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6852 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6853 performing a C-style cast, its value upon return will indicate
6854 whether or not the conversion succeeded. */
6857 build_const_cast_1 (tree dst_type
, tree expr
, tsubst_flags_t complain
,
6861 tree reference_type
;
6863 /* Callers are responsible for handling error_mark_node as a
6864 destination type. */
6865 gcc_assert (dst_type
!= error_mark_node
);
6866 /* In a template, callers should be building syntactic
6867 representations of casts, not using this machinery. */
6868 gcc_assert (!processing_template_decl
);
6870 /* Assume the conversion is invalid. */
6874 if (!POINTER_TYPE_P (dst_type
) && !TYPE_PTRDATAMEM_P (dst_type
))
6876 if (complain
& tf_error
)
6877 error ("invalid use of const_cast with type %qT, "
6878 "which is not a pointer, "
6879 "reference, nor a pointer-to-data-member type", dst_type
);
6880 return error_mark_node
;
6883 if (TREE_CODE (TREE_TYPE (dst_type
)) == FUNCTION_TYPE
)
6885 if (complain
& tf_error
)
6886 error ("invalid use of const_cast with type %qT, which is a pointer "
6887 "or reference to a function type", dst_type
);
6888 return error_mark_node
;
6891 /* Save casted types in the function's used types hash table. */
6892 used_types_insert (dst_type
);
6894 src_type
= TREE_TYPE (expr
);
6895 /* Expressions do not really have reference types. */
6896 if (TREE_CODE (src_type
) == REFERENCE_TYPE
)
6897 src_type
= TREE_TYPE (src_type
);
6899 /* [expr.const.cast]
6901 For two object types T1 and T2, if a pointer to T1 can be explicitly
6902 converted to the type "pointer to T2" using a const_cast, then the
6903 following conversions can also be made:
6905 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6906 type T2 using the cast const_cast<T2&>;
6908 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6909 type T2 using the cast const_cast<T2&&>; and
6911 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6912 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6914 if (TREE_CODE (dst_type
) == REFERENCE_TYPE
)
6916 reference_type
= dst_type
;
6917 if (!TYPE_REF_IS_RVALUE (dst_type
)
6918 ? real_lvalue_p (expr
)
6919 : (CLASS_TYPE_P (TREE_TYPE (dst_type
))
6921 : lvalue_or_rvalue_with_address_p (expr
)))
6925 if (complain
& tf_error
)
6926 error ("invalid const_cast of an rvalue of type %qT to type %qT",
6927 src_type
, dst_type
);
6928 return error_mark_node
;
6930 dst_type
= build_pointer_type (TREE_TYPE (dst_type
));
6931 src_type
= build_pointer_type (src_type
);
6935 reference_type
= NULL_TREE
;
6936 /* If the destination type is not a reference type, the
6937 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
6938 conversions are performed. */
6939 src_type
= type_decays_to (src_type
);
6940 if (src_type
== error_mark_node
)
6941 return error_mark_node
;
6944 if (TYPE_PTR_P (src_type
) || TYPE_PTRDATAMEM_P (src_type
))
6946 if (comp_ptr_ttypes_const (dst_type
, src_type
))
6951 /* This cast is actually a C-style cast. Issue a warning if
6952 the user is making a potentially unsafe cast. */
6953 check_for_casting_away_constness (src_type
, dst_type
,
6954 CAST_EXPR
, complain
);
6958 expr
= cp_build_addr_expr (expr
, complain
);
6959 if (expr
== error_mark_node
)
6960 return error_mark_node
;
6961 expr
= build_nop (reference_type
, expr
);
6962 return convert_from_reference (expr
);
6966 expr
= decay_conversion (expr
, complain
);
6967 if (expr
== error_mark_node
)
6968 return error_mark_node
;
6970 /* build_c_cast puts on a NOP_EXPR to make the result not an
6971 lvalue. Strip such NOP_EXPRs if VALUE is being used in
6972 non-lvalue context. */
6973 if (TREE_CODE (expr
) == NOP_EXPR
6974 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
6975 expr
= TREE_OPERAND (expr
, 0);
6976 return build_nop (dst_type
, expr
);
6980 && !at_least_as_qualified_p (TREE_TYPE (dst_type
),
6981 TREE_TYPE (src_type
)))
6982 check_for_casting_away_constness (src_type
, dst_type
, CAST_EXPR
,
6986 if (complain
& tf_error
)
6987 error ("invalid const_cast from type %qT to type %qT",
6988 src_type
, dst_type
);
6989 return error_mark_node
;
6993 build_const_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6997 if (type
== error_mark_node
|| error_operand_p (expr
))
6998 return error_mark_node
;
7000 if (processing_template_decl
)
7002 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
7004 if (!TREE_SIDE_EFFECTS (t
)
7005 && type_dependent_expression_p (expr
))
7006 /* There might turn out to be side effects inside expr. */
7007 TREE_SIDE_EFFECTS (t
) = 1;
7008 return convert_from_reference (t
);
7011 r
= build_const_cast_1 (type
, expr
, complain
, /*valid_p=*/NULL
);
7012 if (r
!= error_mark_node
)
7013 maybe_warn_about_useless_cast (type
, expr
, complain
);
7017 /* Like cp_build_c_cast, but for the c-common bits. */
7020 build_c_cast (location_t
/*loc*/, tree type
, tree expr
)
7022 return cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7025 /* Build an expression representing an explicit C-style cast to type
7026 TYPE of expression EXPR. */
7029 cp_build_c_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7035 if (type
== error_mark_node
|| error_operand_p (expr
))
7036 return error_mark_node
;
7038 if (processing_template_decl
)
7040 tree t
= build_min (CAST_EXPR
, type
,
7041 tree_cons (NULL_TREE
, value
, NULL_TREE
));
7042 /* We don't know if it will or will not have side effects. */
7043 TREE_SIDE_EFFECTS (t
) = 1;
7044 return convert_from_reference (t
);
7047 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7048 'Class') should always be retained, because this information aids
7049 in method lookup. */
7050 if (objc_is_object_ptr (type
)
7051 && objc_is_object_ptr (TREE_TYPE (expr
)))
7052 return build_nop (type
, expr
);
7054 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7055 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7056 if (TREE_CODE (type
) != REFERENCE_TYPE
7057 && TREE_CODE (value
) == NOP_EXPR
7058 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
7059 value
= TREE_OPERAND (value
, 0);
7061 if (TREE_CODE (type
) == ARRAY_TYPE
)
7063 /* Allow casting from T1* to T2[] because Cfront allows it.
7064 NIHCL uses it. It is not valid ISO C++ however. */
7065 if (TYPE_PTR_P (TREE_TYPE (expr
)))
7067 if (complain
& tf_error
)
7068 permerror (input_location
, "ISO C++ forbids casting to an array type %qT", type
);
7070 return error_mark_node
;
7071 type
= build_pointer_type (TREE_TYPE (type
));
7075 if (complain
& tf_error
)
7076 error ("ISO C++ forbids casting to an array type %qT", type
);
7077 return error_mark_node
;
7081 if (TREE_CODE (type
) == FUNCTION_TYPE
7082 || TREE_CODE (type
) == METHOD_TYPE
)
7084 if (complain
& tf_error
)
7085 error ("invalid cast to function type %qT", type
);
7086 return error_mark_node
;
7089 if (TYPE_PTR_P (type
)
7090 && TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
7091 /* Casting to an integer of smaller size is an error detected elsewhere. */
7092 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (value
))
7093 /* Don't warn about converting any constant. */
7094 && !TREE_CONSTANT (value
))
7095 warning_at (input_location
, OPT_Wint_to_pointer_cast
,
7096 "cast to pointer from integer of different size");
7098 /* A C-style cast can be a const_cast. */
7099 result
= build_const_cast_1 (type
, value
, complain
& tf_warning
,
7103 if (result
!= error_mark_node
)
7104 maybe_warn_about_useless_cast (type
, value
, complain
);
7108 /* Or a static cast. */
7109 result
= build_static_cast_1 (type
, value
, /*c_cast_p=*/true,
7110 &valid_p
, complain
);
7111 /* Or a reinterpret_cast. */
7113 result
= build_reinterpret_cast_1 (type
, value
, /*c_cast_p=*/true,
7114 &valid_p
, complain
);
7115 /* The static_cast or reinterpret_cast may be followed by a
7118 /* A valid cast may result in errors if, for example, a
7119 conversion to an ambiguous base class is required. */
7120 && !error_operand_p (result
))
7124 maybe_warn_about_useless_cast (type
, value
, complain
);
7126 /* Non-class rvalues always have cv-unqualified type. */
7127 if (!CLASS_TYPE_P (type
))
7128 type
= TYPE_MAIN_VARIANT (type
);
7129 result_type
= TREE_TYPE (result
);
7130 if (!CLASS_TYPE_P (result_type
) && TREE_CODE (type
) != REFERENCE_TYPE
)
7131 result_type
= TYPE_MAIN_VARIANT (result_type
);
7132 /* If the type of RESULT does not match TYPE, perform a
7133 const_cast to make it match. If the static_cast or
7134 reinterpret_cast succeeded, we will differ by at most
7135 cv-qualification, so the follow-on const_cast is guaranteed
7137 if (!same_type_p (non_reference (type
), non_reference (result_type
)))
7139 result
= build_const_cast_1 (type
, result
, false, &valid_p
);
7140 gcc_assert (valid_p
);
7145 return error_mark_node
;
7148 /* For use from the C common bits. */
7150 build_modify_expr (location_t
/*location*/,
7151 tree lhs
, tree
/*lhs_origtype*/,
7152 enum tree_code modifycode
,
7153 location_t
/*rhs_location*/, tree rhs
,
7154 tree
/*rhs_origtype*/)
7156 return cp_build_modify_expr (lhs
, modifycode
, rhs
, tf_warning_or_error
);
7159 /* Build an assignment expression of lvalue LHS from value RHS.
7160 MODIFYCODE is the code for a binary operator that we use
7161 to combine the old value of LHS with RHS to get the new value.
7162 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7164 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7167 cp_build_modify_expr (tree lhs
, enum tree_code modifycode
, tree rhs
,
7168 tsubst_flags_t complain
)
7172 tree lhstype
= TREE_TYPE (lhs
);
7173 tree olhstype
= lhstype
;
7174 bool plain_assign
= (modifycode
== NOP_EXPR
);
7176 /* Avoid duplicate error messages from operands that had errors. */
7177 if (error_operand_p (lhs
) || error_operand_p (rhs
))
7178 return error_mark_node
;
7180 /* Handle control structure constructs used as "lvalues". */
7181 switch (TREE_CODE (lhs
))
7183 /* Handle --foo = 5; as these are valid constructs in C++. */
7184 case PREDECREMENT_EXPR
:
7185 case PREINCREMENT_EXPR
:
7186 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
7187 lhs
= build2 (TREE_CODE (lhs
), TREE_TYPE (lhs
),
7188 stabilize_reference (TREE_OPERAND (lhs
, 0)),
7189 TREE_OPERAND (lhs
, 1));
7190 newrhs
= cp_build_modify_expr (TREE_OPERAND (lhs
, 0),
7191 modifycode
, rhs
, complain
);
7192 if (newrhs
== error_mark_node
)
7193 return error_mark_node
;
7194 return build2 (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
7196 /* Handle (a, b) used as an "lvalue". */
7198 newrhs
= cp_build_modify_expr (TREE_OPERAND (lhs
, 1),
7199 modifycode
, rhs
, complain
);
7200 if (newrhs
== error_mark_node
)
7201 return error_mark_node
;
7202 return build2 (COMPOUND_EXPR
, lhstype
,
7203 TREE_OPERAND (lhs
, 0), newrhs
);
7206 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
7207 lhs
= build2 (TREE_CODE (lhs
), TREE_TYPE (lhs
),
7208 stabilize_reference (TREE_OPERAND (lhs
, 0)),
7209 TREE_OPERAND (lhs
, 1));
7210 newrhs
= cp_build_modify_expr (TREE_OPERAND (lhs
, 0), modifycode
, rhs
,
7212 if (newrhs
== error_mark_node
)
7213 return error_mark_node
;
7214 return build2 (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
7218 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7219 when neither operand has side-effects. */
7220 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7221 return error_mark_node
;
7223 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0))
7224 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 1)));
7226 lhs
= build3 (COND_EXPR
, TREE_TYPE (lhs
),
7227 build2 (TREE_CODE (lhs
) == MIN_EXPR
? LE_EXPR
: GE_EXPR
,
7229 TREE_OPERAND (lhs
, 0),
7230 TREE_OPERAND (lhs
, 1)),
7231 TREE_OPERAND (lhs
, 0),
7232 TREE_OPERAND (lhs
, 1));
7235 /* Handle (a ? b : c) used as an "lvalue". */
7238 /* Produce (a ? (b = rhs) : (c = rhs))
7239 except that the RHS goes through a save-expr
7240 so the code to compute it is only emitted once. */
7242 tree preeval
= NULL_TREE
;
7244 if (VOID_TYPE_P (TREE_TYPE (rhs
)))
7246 if (complain
& tf_error
)
7247 error ("void value not ignored as it ought to be");
7248 return error_mark_node
;
7251 rhs
= stabilize_expr (rhs
, &preeval
);
7253 /* Check this here to avoid odd errors when trying to convert
7254 a throw to the type of the COND_EXPR. */
7255 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7256 return error_mark_node
;
7258 cond
= build_conditional_expr
7259 (input_location
, TREE_OPERAND (lhs
, 0),
7260 cp_build_modify_expr (TREE_OPERAND (lhs
, 1),
7261 modifycode
, rhs
, complain
),
7262 cp_build_modify_expr (TREE_OPERAND (lhs
, 2),
7263 modifycode
, rhs
, complain
),
7266 if (cond
== error_mark_node
)
7268 /* Make sure the code to compute the rhs comes out
7269 before the split. */
7271 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (lhs
), preeval
, cond
);
7279 if (modifycode
== INIT_EXPR
)
7281 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
7282 /* Do the default thing. */;
7283 else if (TREE_CODE (rhs
) == CONSTRUCTOR
)
7285 /* Compound literal. */
7286 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
7287 /* Call convert to generate an error; see PR 11063. */
7288 rhs
= convert (lhstype
, rhs
);
7289 result
= build2 (INIT_EXPR
, lhstype
, lhs
, rhs
);
7290 TREE_SIDE_EFFECTS (result
) = 1;
7293 else if (! MAYBE_CLASS_TYPE_P (lhstype
))
7294 /* Do the default thing. */;
7297 vec
<tree
, va_gc
> *rhs_vec
= make_tree_vector_single (rhs
);
7298 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
7299 &rhs_vec
, lhstype
, LOOKUP_NORMAL
,
7301 release_tree_vector (rhs_vec
);
7302 if (result
== NULL_TREE
)
7303 return error_mark_node
;
7309 lhs
= require_complete_type_sfinae (lhs
, complain
);
7310 if (lhs
== error_mark_node
)
7311 return error_mark_node
;
7313 if (modifycode
== NOP_EXPR
)
7315 if (c_dialect_objc ())
7317 result
= objc_maybe_build_modify_expr (lhs
, rhs
);
7322 /* `operator=' is not an inheritable operator. */
7323 if (! MAYBE_CLASS_TYPE_P (lhstype
))
7324 /* Do the default thing. */;
7327 result
= build_new_op (input_location
, MODIFY_EXPR
,
7328 LOOKUP_NORMAL
, lhs
, rhs
,
7329 make_node (NOP_EXPR
), /*overload=*/NULL
,
7331 if (result
== NULL_TREE
)
7332 return error_mark_node
;
7339 tree init
= NULL_TREE
;
7341 /* A binary op has been requested. Combine the old LHS
7342 value with the RHS producing the value we should actually
7343 store into the LHS. */
7344 gcc_assert (!((TREE_CODE (lhstype
) == REFERENCE_TYPE
7345 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype
)))
7346 || MAYBE_CLASS_TYPE_P (lhstype
)));
7348 /* Preevaluate the RHS to make sure its evaluation is complete
7349 before the lvalue-to-rvalue conversion of the LHS:
7351 [expr.ass] With respect to an indeterminately-sequenced
7352 function call, the operation of a compound assignment is a
7353 single evaluation. [ Note: Therefore, a function call shall
7354 not intervene between the lvalue-to-rvalue conversion and the
7355 side effect associated with any single compound assignment
7356 operator. -- end note ] */
7357 lhs
= stabilize_reference (lhs
);
7358 if (TREE_SIDE_EFFECTS (rhs
))
7359 rhs
= mark_rvalue_use (rhs
);
7360 rhs
= stabilize_expr (rhs
, &init
);
7361 newrhs
= cp_build_binary_op (input_location
,
7362 modifycode
, lhs
, rhs
,
7364 if (newrhs
== error_mark_node
)
7366 if (complain
& tf_error
)
7367 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode
,
7368 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
7369 return error_mark_node
;
7373 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), init
, newrhs
);
7375 /* Now it looks like a plain assignment. */
7376 modifycode
= NOP_EXPR
;
7377 if (c_dialect_objc ())
7379 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
7384 gcc_assert (TREE_CODE (lhstype
) != REFERENCE_TYPE
);
7385 gcc_assert (TREE_CODE (TREE_TYPE (newrhs
)) != REFERENCE_TYPE
);
7388 /* The left-hand side must be an lvalue. */
7389 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7390 return error_mark_node
;
7392 /* Warn about modifying something that is `const'. Don't warn if
7393 this is initialization. */
7394 if (modifycode
!= INIT_EXPR
7395 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
7396 /* Functions are not modifiable, even though they are
7398 || TREE_CODE (TREE_TYPE (lhs
)) == FUNCTION_TYPE
7399 || TREE_CODE (TREE_TYPE (lhs
)) == METHOD_TYPE
7400 /* If it's an aggregate and any field is const, then it is
7401 effectively const. */
7402 || (CLASS_TYPE_P (lhstype
)
7403 && C_TYPE_FIELDS_READONLY (lhstype
))))
7405 if (complain
& tf_error
)
7406 cxx_readonly_error (lhs
, lv_assign
);
7408 return error_mark_node
;
7411 /* If storing into a structure or union member, it may have been given a
7412 lowered bitfield type. We need to convert to the declared type first,
7413 so retrieve it now. */
7415 olhstype
= unlowered_expr_type (lhs
);
7417 /* Convert new value to destination type. */
7419 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
7423 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs
))
7425 if (modifycode
!= INIT_EXPR
)
7427 if (complain
& tf_error
)
7428 error ("assigning to an array from an initializer list");
7429 return error_mark_node
;
7431 if (check_array_initializer (lhs
, lhstype
, newrhs
))
7432 return error_mark_node
;
7433 newrhs
= digest_init (lhstype
, newrhs
, complain
);
7434 if (newrhs
== error_mark_node
)
7435 return error_mark_node
;
7438 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
7439 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))))
7441 if (complain
& tf_error
)
7442 error ("incompatible types in assignment of %qT to %qT",
7443 TREE_TYPE (rhs
), lhstype
);
7444 return error_mark_node
;
7447 /* Allow array assignment in compiler-generated code. */
7448 else if (!current_function_decl
7449 || !DECL_DEFAULTED_FN (current_function_decl
))
7451 /* This routine is used for both initialization and assignment.
7452 Make sure the diagnostic message differentiates the context. */
7453 if (complain
& tf_error
)
7455 if (modifycode
== INIT_EXPR
)
7456 error ("array used as initializer");
7458 error ("invalid array assignment");
7460 return error_mark_node
;
7463 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
7464 ? 1 + (modifycode
!= INIT_EXPR
): 0;
7465 return build_vec_init (lhs
, NULL_TREE
, newrhs
,
7466 /*explicit_value_init_p=*/false,
7467 from_array
, complain
);
7470 if (modifycode
== INIT_EXPR
)
7471 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7472 LOOKUP_ONLYCONVERTING. */
7473 newrhs
= convert_for_initialization (lhs
, olhstype
, newrhs
, LOOKUP_NORMAL
,
7474 ICR_INIT
, NULL_TREE
, 0,
7477 newrhs
= convert_for_assignment (olhstype
, newrhs
, ICR_ASSIGN
,
7478 NULL_TREE
, 0, complain
, LOOKUP_IMPLICIT
);
7480 if (!same_type_p (lhstype
, olhstype
))
7481 newrhs
= cp_convert_and_check (lhstype
, newrhs
, complain
);
7483 if (modifycode
!= INIT_EXPR
)
7485 if (TREE_CODE (newrhs
) == CALL_EXPR
7486 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
7487 newrhs
= build_cplus_new (lhstype
, newrhs
, complain
);
7489 /* Can't initialize directly from a TARGET_EXPR, since that would
7490 cause the lhs to be constructed twice, and possibly result in
7491 accidental self-initialization. So we force the TARGET_EXPR to be
7492 expanded without a target. */
7493 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
7494 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
7495 TREE_OPERAND (newrhs
, 0));
7498 if (newrhs
== error_mark_node
)
7499 return error_mark_node
;
7501 if (c_dialect_objc () && flag_objc_gc
)
7503 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
7509 result
= build2 (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
7510 lhstype
, lhs
, newrhs
);
7512 TREE_SIDE_EFFECTS (result
) = 1;
7514 TREE_NO_WARNING (result
) = 1;
7520 build_x_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
7521 tree rhs
, tsubst_flags_t complain
)
7523 if (processing_template_decl
)
7524 return build_min_nt_loc (loc
, MODOP_EXPR
, lhs
,
7525 build_min_nt_loc (loc
, modifycode
, NULL_TREE
,
7528 if (modifycode
!= NOP_EXPR
)
7530 tree rval
= build_new_op (loc
, MODIFY_EXPR
, LOOKUP_NORMAL
, lhs
, rhs
,
7531 make_node (modifycode
), /*overload=*/NULL
,
7535 TREE_NO_WARNING (rval
) = 1;
7539 return cp_build_modify_expr (lhs
, modifycode
, rhs
, complain
);
7542 /* Helper function for get_delta_difference which assumes FROM is a base
7543 class of TO. Returns a delta for the conversion of pointer-to-member
7544 of FROM to pointer-to-member of TO. If the conversion is invalid and
7545 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7546 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7547 If C_CAST_P is true, this conversion is taking place as part of a
7551 get_delta_difference_1 (tree from
, tree to
, bool c_cast_p
,
7552 tsubst_flags_t complain
)
7557 binfo
= lookup_base (to
, from
, c_cast_p
? ba_unique
: ba_check
,
7560 if (binfo
== error_mark_node
)
7562 if (!(complain
& tf_error
))
7563 return error_mark_node
;
7565 error (" in pointer to member function conversion");
7566 return size_zero_node
;
7570 if (kind
!= bk_via_virtual
)
7571 return BINFO_OFFSET (binfo
);
7573 /* FROM is a virtual base class of TO. Issue an error or warning
7574 depending on whether or not this is a reinterpret cast. */
7576 if (!(complain
& tf_error
))
7577 return error_mark_node
;
7579 error ("pointer to member conversion via virtual base %qT",
7580 BINFO_TYPE (binfo_from_vbase (binfo
)));
7582 return size_zero_node
;
7589 /* Get difference in deltas for different pointer to member function
7590 types. If the conversion is invalid and tf_error is not set in
7591 COMPLAIN, returns error_mark_node, otherwise returns an integer
7592 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7593 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7594 conversions as well. If C_CAST_P is true this conversion is taking
7595 place as part of a C-style cast.
7597 Note that the naming of FROM and TO is kind of backwards; the return
7598 value is what we add to a TO in order to get a FROM. They are named
7599 this way because we call this function to find out how to convert from
7600 a pointer to member of FROM to a pointer to member of TO. */
7603 get_delta_difference (tree from
, tree to
,
7604 bool allow_inverse_p
,
7605 bool c_cast_p
, tsubst_flags_t complain
)
7609 if (same_type_ignoring_top_level_qualifiers_p (from
, to
))
7610 /* Pointer to member of incomplete class is permitted*/
7611 result
= size_zero_node
;
7613 result
= get_delta_difference_1 (from
, to
, c_cast_p
, complain
);
7615 if (result
== error_mark_node
)
7616 return error_mark_node
;
7620 if (!allow_inverse_p
)
7622 if (!(complain
& tf_error
))
7623 return error_mark_node
;
7625 error_not_base_type (from
, to
);
7626 error (" in pointer to member conversion");
7627 result
= size_zero_node
;
7631 result
= get_delta_difference_1 (to
, from
, c_cast_p
, complain
);
7633 if (result
== error_mark_node
)
7634 return error_mark_node
;
7637 result
= size_diffop_loc (input_location
,
7638 size_zero_node
, result
);
7641 if (!(complain
& tf_error
))
7642 return error_mark_node
;
7644 error_not_base_type (from
, to
);
7645 error (" in pointer to member conversion");
7646 result
= size_zero_node
;
7651 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node
,
7655 /* Return a constructor for the pointer-to-member-function TYPE using
7656 the other components as specified. */
7659 build_ptrmemfunc1 (tree type
, tree delta
, tree pfn
)
7664 vec
<constructor_elt
, va_gc
> *v
;
7666 /* Pull the FIELD_DECLs out of the type. */
7667 pfn_field
= TYPE_FIELDS (type
);
7668 delta_field
= DECL_CHAIN (pfn_field
);
7670 /* Make sure DELTA has the type we want. */
7671 delta
= convert_and_check (delta_type_node
, delta
);
7673 /* Convert to the correct target type if necessary. */
7674 pfn
= fold_convert (TREE_TYPE (pfn_field
), pfn
);
7676 /* Finish creating the initializer. */
7678 CONSTRUCTOR_APPEND_ELT(v
, pfn_field
, pfn
);
7679 CONSTRUCTOR_APPEND_ELT(v
, delta_field
, delta
);
7680 u
= build_constructor (type
, v
);
7681 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) & TREE_CONSTANT (delta
);
7682 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
7683 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
7685 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
7690 /* Build a constructor for a pointer to member function. It can be
7691 used to initialize global variables, local variable, or used
7692 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7695 If FORCE is nonzero, then force this conversion, even if
7696 we would rather not do it. Usually set when using an explicit
7697 cast. A C-style cast is being processed iff C_CAST_P is true.
7699 Return error_mark_node, if something goes wrong. */
7702 build_ptrmemfunc (tree type
, tree pfn
, int force
, bool c_cast_p
,
7703 tsubst_flags_t complain
)
7709 if (error_operand_p (pfn
))
7710 return error_mark_node
;
7712 pfn_type
= TREE_TYPE (pfn
);
7713 to_type
= build_ptrmemfunc_type (type
);
7715 /* Handle multiple conversions of pointer to member functions. */
7716 if (TYPE_PTRMEMFUNC_P (pfn_type
))
7718 tree delta
= NULL_TREE
;
7719 tree npfn
= NULL_TREE
;
7723 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
,
7724 LOOKUP_NORMAL
, complain
))
7726 if (complain
& tf_error
)
7727 error ("invalid conversion to type %qT from type %qT",
7730 return error_mark_node
;
7733 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
7734 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
7736 c_cast_p
, complain
);
7737 if (n
== error_mark_node
)
7738 return error_mark_node
;
7740 /* We don't have to do any conversion to convert a
7741 pointer-to-member to its own type. But, we don't want to
7742 just return a PTRMEM_CST if there's an explicit cast; that
7743 cast should make the expression an invalid template argument. */
7744 if (TREE_CODE (pfn
) != PTRMEM_CST
)
7746 if (same_type_p (to_type
, pfn_type
))
7748 else if (integer_zerop (n
))
7749 return build_reinterpret_cast (to_type
, pfn
,
7753 if (TREE_SIDE_EFFECTS (pfn
))
7754 pfn
= save_expr (pfn
);
7756 /* Obtain the function pointer and the current DELTA. */
7757 if (TREE_CODE (pfn
) == PTRMEM_CST
)
7758 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
7761 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
7762 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
7765 /* Just adjust the DELTA field. */
7766 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7767 (TREE_TYPE (delta
), ptrdiff_type_node
));
7768 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
7769 n
= cp_build_binary_op (input_location
,
7770 LSHIFT_EXPR
, n
, integer_one_node
,
7772 delta
= cp_build_binary_op (input_location
,
7773 PLUS_EXPR
, delta
, n
, complain
);
7774 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
7777 /* Handle null pointer to member function conversions. */
7778 if (null_ptr_cst_p (pfn
))
7780 pfn
= build_c_cast (input_location
, type
, pfn
);
7781 return build_ptrmemfunc1 (to_type
,
7786 if (type_unknown_p (pfn
))
7787 return instantiate_type (type
, pfn
, complain
);
7789 fn
= TREE_OPERAND (pfn
, 0);
7790 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
7791 /* In a template, we will have preserved the
7793 || (processing_template_decl
&& TREE_CODE (fn
) == OFFSET_REF
));
7794 return make_ptrmem_cst (to_type
, fn
);
7797 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7800 ??? There is no consistency as to the types returned for the above
7801 values. Some code acts as if it were a sizetype and some as if it were
7802 integer_type_node. */
7805 expand_ptrmemfunc_cst (tree cst
, tree
*delta
, tree
*pfn
)
7807 tree type
= TREE_TYPE (cst
);
7808 tree fn
= PTRMEM_CST_MEMBER (cst
);
7809 tree ptr_class
, fn_class
;
7811 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
7813 /* The class that the function belongs to. */
7814 fn_class
= DECL_CONTEXT (fn
);
7816 /* The class that we're creating a pointer to member of. */
7817 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
7819 /* First, calculate the adjustment to the function's class. */
7820 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0,
7821 /*c_cast_p=*/0, tf_warning_or_error
);
7823 if (!DECL_VIRTUAL_P (fn
))
7824 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
),
7825 build_addr_func (fn
, tf_warning_or_error
));
7828 /* If we're dealing with a virtual function, we have to adjust 'this'
7829 again, to point to the base which provides the vtable entry for
7830 fn; the call will do the opposite adjustment. */
7831 tree orig_class
= DECL_CONTEXT (fn
);
7832 tree binfo
= binfo_or_else (orig_class
, fn_class
);
7833 *delta
= build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
7834 *delta
, BINFO_OFFSET (binfo
));
7835 *delta
= fold_if_not_in_template (*delta
);
7837 /* We set PFN to the vtable offset at which the function can be
7838 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7839 case delta is shifted left, and then incremented). */
7840 *pfn
= DECL_VINDEX (fn
);
7841 *pfn
= build2 (MULT_EXPR
, integer_type_node
, *pfn
,
7842 TYPE_SIZE_UNIT (vtable_entry_type
));
7843 *pfn
= fold_if_not_in_template (*pfn
);
7845 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
7847 case ptrmemfunc_vbit_in_pfn
:
7848 *pfn
= build2 (PLUS_EXPR
, integer_type_node
, *pfn
,
7850 *pfn
= fold_if_not_in_template (*pfn
);
7853 case ptrmemfunc_vbit_in_delta
:
7854 *delta
= build2 (LSHIFT_EXPR
, TREE_TYPE (*delta
),
7855 *delta
, integer_one_node
);
7856 *delta
= fold_if_not_in_template (*delta
);
7857 *delta
= build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
7858 *delta
, integer_one_node
);
7859 *delta
= fold_if_not_in_template (*delta
);
7866 *pfn
= build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type
), *pfn
);
7867 *pfn
= fold_if_not_in_template (*pfn
);
7871 /* Return an expression for PFN from the pointer-to-member function
7875 pfn_from_ptrmemfunc (tree t
)
7877 if (TREE_CODE (t
) == PTRMEM_CST
)
7882 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
7887 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
7890 /* Return an expression for DELTA from the pointer-to-member function
7894 delta_from_ptrmemfunc (tree t
)
7896 if (TREE_CODE (t
) == PTRMEM_CST
)
7901 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
7906 return build_ptrmemfunc_access_expr (t
, delta_identifier
);
7909 /* Convert value RHS to type TYPE as preparation for an assignment to
7910 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
7911 implicit conversion is. If FNDECL is non-NULL, we are doing the
7912 conversion in order to pass the PARMNUMth argument of FNDECL.
7913 If FNDECL is NULL, we are doing the conversion in function pointer
7914 argument passing, conversion in initialization, etc. */
7917 convert_for_assignment (tree type
, tree rhs
,
7918 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
7919 tsubst_flags_t complain
, int flags
)
7922 enum tree_code coder
;
7924 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
7925 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
7926 rhs
= TREE_OPERAND (rhs
, 0);
7928 rhstype
= TREE_TYPE (rhs
);
7929 coder
= TREE_CODE (rhstype
);
7931 if (TREE_CODE (type
) == VECTOR_TYPE
&& coder
== VECTOR_TYPE
7932 && vector_types_convertible_p (type
, rhstype
, true))
7934 rhs
= mark_rvalue_use (rhs
);
7935 return convert (type
, rhs
);
7938 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
7939 return error_mark_node
;
7940 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
7941 return error_mark_node
;
7943 /* The RHS of an assignment cannot have void type. */
7944 if (coder
== VOID_TYPE
)
7946 if (complain
& tf_error
)
7947 error ("void value not ignored as it ought to be");
7948 return error_mark_node
;
7951 if (c_dialect_objc ())
7955 tree rname
= fndecl
;
7966 selector
= objc_message_selector ();
7968 if (selector
&& parmno
> 1)
7975 if (objc_compare_types (type
, rhstype
, parmno
, rname
))
7977 rhs
= mark_rvalue_use (rhs
);
7978 return convert (type
, rhs
);
7984 The expression is implicitly converted (clause _conv_) to the
7985 cv-unqualified type of the left operand.
7987 We allow bad conversions here because by the time we get to this point
7988 we are committed to doing the conversion. If we end up doing a bad
7989 conversion, convert_like will complain. */
7990 if (!can_convert_arg_bad (type
, rhstype
, rhs
, flags
, complain
))
7992 /* When -Wno-pmf-conversions is use, we just silently allow
7993 conversions from pointers-to-members to plain pointers. If
7994 the conversion doesn't work, cp_convert will complain. */
7996 && TYPE_PTR_P (type
)
7997 && TYPE_PTRMEMFUNC_P (rhstype
))
7998 rhs
= cp_convert (strip_top_quals (type
), rhs
, complain
);
8001 if (complain
& tf_error
)
8003 /* If the right-hand side has unknown type, then it is an
8004 overloaded function. Call instantiate_type to get error
8006 if (rhstype
== unknown_type_node
)
8007 instantiate_type (type
, rhs
, tf_warning_or_error
);
8009 error ("cannot convert %qT to %qT for argument %qP to %qD",
8010 rhstype
, type
, parmnum
, fndecl
);
8014 case ICR_DEFAULT_ARGUMENT
:
8015 error ("cannot convert %qT to %qT in default argument",
8019 error ("cannot convert %qT to %qT in argument passing",
8022 case ICR_CONVERTING
:
8023 error ("cannot convert %qT to %qT",
8027 error ("cannot convert %qT to %qT in initialization",
8031 error ("cannot convert %qT to %qT in return",
8035 error ("cannot convert %qT to %qT in assignment",
8042 return error_mark_node
;
8045 if (warn_suggest_attribute_format
)
8047 const enum tree_code codel
= TREE_CODE (type
);
8048 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
8050 && check_missing_format_attribute (type
, rhstype
)
8051 && (complain
& tf_warning
))
8055 case ICR_DEFAULT_ARGUMENT
:
8057 warning (OPT_Wsuggest_attribute_format
,
8058 "parameter %qP of %qD might be a candidate "
8059 "for a format attribute", parmnum
, fndecl
);
8061 warning (OPT_Wsuggest_attribute_format
,
8062 "parameter might be a candidate "
8063 "for a format attribute");
8065 case ICR_CONVERTING
:
8066 warning (OPT_Wsuggest_attribute_format
,
8067 "target of conversion might be a candidate "
8068 "for a format attribute");
8071 warning (OPT_Wsuggest_attribute_format
,
8072 "target of initialization might be a candidate "
8073 "for a format attribute");
8076 warning (OPT_Wsuggest_attribute_format
,
8077 "return type might be a candidate "
8078 "for a format attribute");
8081 warning (OPT_Wsuggest_attribute_format
,
8082 "left-hand side of assignment might be a candidate "
8083 "for a format attribute");
8090 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8092 if (warn_parentheses
8093 && TREE_CODE (type
) == BOOLEAN_TYPE
8094 && TREE_CODE (rhs
) == MODIFY_EXPR
8095 && !TREE_NO_WARNING (rhs
)
8096 && TREE_CODE (TREE_TYPE (rhs
)) != BOOLEAN_TYPE
8097 && (complain
& tf_warning
))
8099 location_t loc
= EXPR_LOC_OR_HERE (rhs
);
8101 warning_at (loc
, OPT_Wparentheses
,
8102 "suggest parentheses around assignment used as truth value");
8103 TREE_NO_WARNING (rhs
) = 1;
8106 return perform_implicit_conversion_flags (strip_top_quals (type
), rhs
,
8110 /* Convert RHS to be of type TYPE.
8111 If EXP is nonzero, it is the target of the initialization.
8112 ERRTYPE indicates what kind of error the implicit conversion is.
8114 Two major differences between the behavior of
8115 `convert_for_assignment' and `convert_for_initialization'
8116 are that references are bashed in the former, while
8117 copied in the latter, and aggregates are assigned in
8118 the former (operator=) while initialized in the
8121 If using constructor make sure no conversion operator exists, if one does
8122 exist, an ambiguity exists. */
8125 convert_for_initialization (tree exp
, tree type
, tree rhs
, int flags
,
8126 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
8127 tsubst_flags_t complain
)
8129 enum tree_code codel
= TREE_CODE (type
);
8131 enum tree_code coder
;
8133 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8134 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8135 if (TREE_CODE (rhs
) == NOP_EXPR
8136 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
8137 && codel
!= REFERENCE_TYPE
)
8138 rhs
= TREE_OPERAND (rhs
, 0);
8140 if (type
== error_mark_node
8141 || rhs
== error_mark_node
8142 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
8143 return error_mark_node
;
8145 if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
8146 && TREE_CODE (type
) != ARRAY_TYPE
8147 && (TREE_CODE (type
) != REFERENCE_TYPE
8148 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
8149 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
8150 && !TYPE_REFFN_P (type
))
8151 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
8152 rhs
= decay_conversion (rhs
, complain
);
8154 rhstype
= TREE_TYPE (rhs
);
8155 coder
= TREE_CODE (rhstype
);
8157 if (coder
== ERROR_MARK
)
8158 return error_mark_node
;
8160 /* We accept references to incomplete types, so we can
8161 return here before checking if RHS is of complete type. */
8163 if (codel
== REFERENCE_TYPE
)
8165 /* This should eventually happen in convert_arguments. */
8166 int savew
= 0, savee
= 0;
8169 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
8170 rhs
= initialize_reference (type
, rhs
, flags
, complain
);
8173 && (warningcount
+ werrorcount
> savew
|| errorcount
> savee
))
8174 inform (input_location
,
8175 "in passing argument %P of %q+D", parmnum
, fndecl
);
8181 exp
= require_complete_type_sfinae (exp
, complain
);
8182 if (exp
== error_mark_node
)
8183 return error_mark_node
;
8185 rhstype
= non_reference (rhstype
);
8187 type
= complete_type (type
);
8189 if (DIRECT_INIT_EXPR_P (type
, rhs
))
8190 /* Don't try to do copy-initialization if we already have
8191 direct-initialization. */
8194 if (MAYBE_CLASS_TYPE_P (type
))
8195 return perform_implicit_conversion_flags (type
, rhs
, complain
, flags
);
8197 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
,
8201 /* If RETVAL is the address of, or a reference to, a local variable or
8202 temporary give an appropriate warning. */
8205 maybe_warn_about_returning_address_of_local (tree retval
)
8207 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
8208 tree whats_returned
= retval
;
8212 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
8213 whats_returned
= TREE_OPERAND (whats_returned
, 1);
8214 else if (CONVERT_EXPR_P (whats_returned
)
8215 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
)
8216 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8221 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
8223 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8225 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
8227 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
8228 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
8230 warning (OPT_Wreturn_local_addr
, "returning reference to temporary");
8233 if (VAR_P (whats_returned
)
8234 && DECL_NAME (whats_returned
)
8235 && TEMP_NAME_P (DECL_NAME (whats_returned
)))
8237 warning (OPT_Wreturn_local_addr
, "reference to non-lvalue returned");
8242 while (TREE_CODE (whats_returned
) == COMPONENT_REF
8243 || TREE_CODE (whats_returned
) == ARRAY_REF
)
8244 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8246 if (DECL_P (whats_returned
)
8247 && DECL_NAME (whats_returned
)
8248 && DECL_FUNCTION_SCOPE_P (whats_returned
)
8249 && !is_capture_proxy (whats_returned
)
8250 && !(TREE_STATIC (whats_returned
)
8251 || TREE_PUBLIC (whats_returned
)))
8253 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
8254 warning (OPT_Wreturn_local_addr
, "reference to local variable %q+D returned",
8257 warning (OPT_Wreturn_local_addr
, "address of local variable %q+D returned",
8263 /* Check that returning RETVAL from the current function is valid.
8264 Return an expression explicitly showing all conversions required to
8265 change RETVAL into the function return type, and to assign it to
8266 the DECL_RESULT for the function. Set *NO_WARNING to true if
8267 code reaches end of non-void function warning shouldn't be issued
8268 on this RETURN_EXPR. */
8271 check_return_expr (tree retval
, bool *no_warning
)
8274 /* The type actually returned by the function. */
8276 /* The type the function is declared to return, or void if
8277 the declared type is incomplete. */
8279 int fn_returns_value_p
;
8280 bool named_return_value_okay_p
;
8282 *no_warning
= false;
8284 /* A `volatile' function is one that isn't supposed to return, ever.
8285 (This is a G++ extension, used to get better code for functions
8286 that call the `volatile' function.) */
8287 if (TREE_THIS_VOLATILE (current_function_decl
))
8288 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8290 /* Check for various simple errors. */
8291 if (DECL_DESTRUCTOR_P (current_function_decl
))
8294 error ("returning a value from a destructor");
8297 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
8299 if (in_function_try_handler
)
8300 /* If a return statement appears in a handler of the
8301 function-try-block of a constructor, the program is ill-formed. */
8302 error ("cannot return from a handler of a function-try-block of a constructor");
8304 /* You can't return a value from a constructor. */
8305 error ("returning a value from a constructor");
8309 if (processing_template_decl
)
8311 current_function_returns_value
= 1;
8312 if (check_for_bare_parameter_packs (retval
))
8313 retval
= error_mark_node
;
8317 functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
8319 /* Deduce auto return type from a return statement. */
8320 if (current_function_auto_return_pattern
)
8325 if (!retval
&& !is_auto (current_function_auto_return_pattern
))
8327 /* Give a helpful error message. */
8328 error ("return-statement with no value, in function returning %qT",
8329 current_function_auto_return_pattern
);
8330 inform (input_location
, "only plain %<auto%> return type can be "
8331 "deduced to %<void%>");
8332 type
= error_mark_node
;
8334 else if (retval
&& BRACE_ENCLOSED_INITIALIZER_P (retval
))
8336 error ("returning initializer list");
8337 type
= error_mark_node
;
8342 retval
= void_zero_node
;
8343 auto_node
= type_uses_auto (current_function_auto_return_pattern
);
8344 type
= do_auto_deduction (current_function_auto_return_pattern
,
8348 if (type
== error_mark_node
)
8350 else if (functype
== current_function_auto_return_pattern
)
8351 apply_deduced_return_type (current_function_decl
, type
);
8353 /* A mismatch should have been diagnosed in do_auto_deduction. */
8354 gcc_assert (same_type_p (type
, functype
));
8358 /* When no explicit return-value is given in a function with a named
8359 return value, the named return value is used. */
8360 result
= DECL_RESULT (current_function_decl
);
8361 valtype
= TREE_TYPE (result
);
8362 gcc_assert (valtype
!= NULL_TREE
);
8363 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
8364 if (!retval
&& DECL_NAME (result
) && fn_returns_value_p
)
8367 /* Check for a return statement with no return value in a function
8368 that's supposed to return a value. */
8369 if (!retval
&& fn_returns_value_p
)
8371 if (functype
!= error_mark_node
)
8372 permerror (input_location
, "return-statement with no value, in "
8373 "function returning %qT", valtype
);
8374 /* Remember that this function did return. */
8375 current_function_returns_value
= 1;
8376 /* And signal caller that TREE_NO_WARNING should be set on the
8377 RETURN_EXPR to avoid control reaches end of non-void function
8378 warnings in tree-cfg.c. */
8381 /* Check for a return statement with a value in a function that
8382 isn't supposed to return a value. */
8383 else if (retval
&& !fn_returns_value_p
)
8385 if (VOID_TYPE_P (TREE_TYPE (retval
)))
8386 /* You can return a `void' value from a function of `void'
8387 type. In that case, we have to evaluate the expression for
8388 its side-effects. */
8389 finish_expr_stmt (retval
);
8391 permerror (input_location
, "return-statement with a value, in function "
8392 "returning 'void'");
8393 current_function_returns_null
= 1;
8395 /* There's really no value to return, after all. */
8399 /* Remember that this function can sometimes return without a
8401 current_function_returns_null
= 1;
8403 /* Remember that this function did return a value. */
8404 current_function_returns_value
= 1;
8406 /* Check for erroneous operands -- but after giving ourselves a
8407 chance to provide an error about returning a value from a void
8409 if (error_operand_p (retval
))
8411 current_function_return_value
= error_mark_node
;
8412 return error_mark_node
;
8415 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8416 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == NEW_EXPR
8417 || DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == VEC_NEW_EXPR
)
8418 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
8420 && retval
&& null_ptr_cst_p (retval
))
8421 warning (0, "%<operator new%> must not return NULL unless it is "
8422 "declared %<throw()%> (or -fcheck-new is in effect)");
8424 /* Effective C++ rule 15. See also start_function. */
8426 && DECL_NAME (current_function_decl
) == ansi_assopname(NOP_EXPR
))
8430 /* The function return type must be a reference to the current
8432 if (TREE_CODE (valtype
) == REFERENCE_TYPE
8433 && same_type_ignoring_top_level_qualifiers_p
8434 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
)))
8436 /* Returning '*this' is obviously OK. */
8437 if (retval
== current_class_ref
)
8439 /* If we are calling a function whose return type is the same of
8440 the current class reference, it is ok. */
8441 else if (INDIRECT_REF_P (retval
)
8442 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
8447 warning (OPT_Weffc__
, "%<operator=%> should return a reference to %<*this%>");
8450 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8452 [...] For a function with a class return type, if the expression
8453 in the return statement is the name of a local object, and the cv-
8454 unqualified type of the local object is the same as the function
8455 return type, an implementation is permitted to omit creating the tem-
8456 porary object to hold the function return value [...]
8458 So, if this is a value-returning function that always returns the same
8459 local variable, remember it.
8461 It might be nice to be more flexible, and choose the first suitable
8462 variable even if the function sometimes returns something else, but
8463 then we run the risk of clobbering the variable we chose if the other
8464 returned expression uses the chosen variable somehow. And people expect
8465 this restriction, anyway. (jason 2000-11-19)
8467 See finish_function and finalize_nrv for the rest of this optimization. */
8469 named_return_value_okay_p
=
8470 (retval
!= NULL_TREE
8471 /* Must be a local, automatic variable. */
8473 && DECL_CONTEXT (retval
) == current_function_decl
8474 && ! TREE_STATIC (retval
)
8475 /* And not a lambda or anonymous union proxy. */
8476 && !DECL_HAS_VALUE_EXPR_P (retval
)
8477 && (DECL_ALIGN (retval
) <= DECL_ALIGN (result
))
8478 /* The cv-unqualified type of the returned value must be the
8479 same as the cv-unqualified return type of the
8481 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval
))),
8482 (TYPE_MAIN_VARIANT (functype
)))
8483 /* And the returned value must be non-volatile. */
8484 && ! TYPE_VOLATILE (TREE_TYPE (retval
)));
8486 if (fn_returns_value_p
&& flag_elide_constructors
)
8488 if (named_return_value_okay_p
8489 && (current_function_return_value
== NULL_TREE
8490 || current_function_return_value
== retval
))
8491 current_function_return_value
= retval
;
8493 current_function_return_value
= error_mark_node
;
8496 /* We don't need to do any conversions when there's nothing being
8501 /* Do any required conversions. */
8502 if (retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
8503 /* No conversions are required. */
8507 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
8509 /* The functype's return type will have been set to void, if it
8510 was an incomplete type. Just treat this as 'return;' */
8511 if (VOID_TYPE_P (functype
))
8512 return error_mark_node
;
8514 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
8515 treated as an rvalue for the purposes of overload resolution to
8516 favor move constructors over copy constructors.
8518 Note that these conditions are similar to, but not as strict as,
8519 the conditions for the named return value optimization. */
8520 if ((cxx_dialect
!= cxx98
)
8521 && ((VAR_P (retval
) && !DECL_HAS_VALUE_EXPR_P (retval
))
8522 || TREE_CODE (retval
) == PARM_DECL
)
8523 && DECL_CONTEXT (retval
) == current_function_decl
8524 && !TREE_STATIC (retval
)
8525 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval
))),
8526 (TYPE_MAIN_VARIANT (functype
)))
8527 /* This is only interesting for class type. */
8528 && CLASS_TYPE_P (functype
))
8529 flags
= flags
| LOOKUP_PREFER_RVALUE
;
8531 /* First convert the value to the function's return type, then
8532 to the type of return value's location to handle the
8533 case that functype is smaller than the valtype. */
8534 retval
= convert_for_initialization
8535 (NULL_TREE
, functype
, retval
, flags
, ICR_RETURN
, NULL_TREE
, 0,
8536 tf_warning_or_error
);
8537 retval
= convert (valtype
, retval
);
8539 /* If the conversion failed, treat this just like `return;'. */
8540 if (retval
== error_mark_node
)
8542 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8543 else if (! cfun
->returns_struct
8544 && TREE_CODE (retval
) == TARGET_EXPR
8545 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
8546 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
8547 TREE_OPERAND (retval
, 0));
8549 maybe_warn_about_returning_address_of_local (retval
);
8552 /* Actually copy the value returned into the appropriate location. */
8553 if (retval
&& retval
!= result
)
8554 retval
= build2 (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
8560 /* Returns nonzero if the pointer-type FROM can be converted to the
8561 pointer-type TO via a qualification conversion. If CONSTP is -1,
8562 then we return nonzero if the pointers are similar, and the
8563 cv-qualification signature of FROM is a proper subset of that of TO.
8565 If CONSTP is positive, then all outer pointers have been
8569 comp_ptr_ttypes_real (tree to
, tree from
, int constp
)
8571 bool to_more_cv_qualified
= false;
8572 bool is_opaque_pointer
= false;
8574 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
8576 if (TREE_CODE (to
) != TREE_CODE (from
))
8579 if (TREE_CODE (from
) == OFFSET_TYPE
8580 && !same_type_p (TYPE_OFFSET_BASETYPE (from
),
8581 TYPE_OFFSET_BASETYPE (to
)))
8584 /* Const and volatile mean something different for function types,
8585 so the usual checks are not appropriate. */
8586 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
8588 if (!at_least_as_qualified_p (to
, from
))
8591 if (!at_least_as_qualified_p (from
, to
))
8595 to_more_cv_qualified
= true;
8599 constp
&= TYPE_READONLY (to
);
8602 if (TREE_CODE (to
) == VECTOR_TYPE
)
8603 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
8605 if (!TYPE_PTR_P (to
) && !TYPE_PTRDATAMEM_P (to
))
8606 return ((constp
>= 0 || to_more_cv_qualified
)
8607 && (is_opaque_pointer
8608 || same_type_ignoring_top_level_qualifiers_p (to
, from
)));
8612 /* When comparing, say, char ** to char const **, this function takes
8613 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8614 types to this function. */
8617 comp_ptr_ttypes (tree to
, tree from
)
8619 return comp_ptr_ttypes_real (to
, from
, 1);
8622 /* Returns true iff FNTYPE is a non-class type that involves
8623 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8624 if a parameter type is ill-formed. */
8627 error_type_p (const_tree type
)
8631 switch (TREE_CODE (type
))
8637 case REFERENCE_TYPE
:
8639 return error_type_p (TREE_TYPE (type
));
8643 if (error_type_p (TREE_TYPE (type
)))
8645 for (t
= TYPE_ARG_TYPES (type
); t
; t
= TREE_CHAIN (t
))
8646 if (error_type_p (TREE_VALUE (t
)))
8651 if (TYPE_PTRMEMFUNC_P (type
))
8652 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type
));
8660 /* Returns true if to and from are (possibly multi-level) pointers to the same
8661 type or inheritance-related types, regardless of cv-quals. */
8664 ptr_reasonably_similar (const_tree to
, const_tree from
)
8666 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
8668 /* Any target type is similar enough to void. */
8669 if (VOID_TYPE_P (to
))
8670 return !error_type_p (from
);
8671 if (VOID_TYPE_P (from
))
8672 return !error_type_p (to
);
8674 if (TREE_CODE (to
) != TREE_CODE (from
))
8677 if (TREE_CODE (from
) == OFFSET_TYPE
8678 && comptypes (TYPE_OFFSET_BASETYPE (to
),
8679 TYPE_OFFSET_BASETYPE (from
),
8680 COMPARE_BASE
| COMPARE_DERIVED
))
8683 if (TREE_CODE (to
) == VECTOR_TYPE
8684 && vector_types_convertible_p (to
, from
, false))
8687 if (TREE_CODE (to
) == INTEGER_TYPE
8688 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
8691 if (TREE_CODE (to
) == FUNCTION_TYPE
)
8692 return !error_type_p (to
) && !error_type_p (from
);
8694 if (!TYPE_PTR_P (to
))
8696 /* When either type is incomplete avoid DERIVED_FROM_P,
8697 which may call complete_type (c++/57942). */
8698 bool b
= !COMPLETE_TYPE_P (to
) || !COMPLETE_TYPE_P (from
);
8700 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
8701 b
? COMPARE_STRICT
: COMPARE_BASE
| COMPARE_DERIVED
);
8706 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8707 pointer-to-member types) are the same, ignoring cv-qualification at
8711 comp_ptr_ttypes_const (tree to
, tree from
)
8713 bool is_opaque_pointer
= false;
8715 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
8717 if (TREE_CODE (to
) != TREE_CODE (from
))
8720 if (TREE_CODE (from
) == OFFSET_TYPE
8721 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
8722 TYPE_OFFSET_BASETYPE (to
)))
8725 if (TREE_CODE (to
) == VECTOR_TYPE
)
8726 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
8728 if (!TYPE_PTR_P (to
))
8729 return (is_opaque_pointer
8730 || same_type_ignoring_top_level_qualifiers_p (to
, from
));
8734 /* Returns the type qualifiers for this type, including the qualifiers on the
8735 elements for an array type. */
8738 cp_type_quals (const_tree type
)
8741 /* This CONST_CAST is okay because strip_array_types returns its
8742 argument unmodified and we assign it to a const_tree. */
8743 type
= strip_array_types (CONST_CAST_TREE (type
));
8744 if (type
== error_mark_node
8745 /* Quals on a FUNCTION_TYPE are memfn quals. */
8746 || TREE_CODE (type
) == FUNCTION_TYPE
)
8747 return TYPE_UNQUALIFIED
;
8748 quals
= TYPE_QUALS (type
);
8749 /* METHOD and REFERENCE_TYPEs should never have quals. */
8750 gcc_assert ((TREE_CODE (type
) != METHOD_TYPE
8751 && TREE_CODE (type
) != REFERENCE_TYPE
)
8752 || ((quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
))
8753 == TYPE_UNQUALIFIED
));
8757 /* Returns the function-ref-qualifier for TYPE */
8760 type_memfn_rqual (const_tree type
)
8762 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
8763 || TREE_CODE (type
) == METHOD_TYPE
);
8765 if (!FUNCTION_REF_QUALIFIED (type
))
8766 return REF_QUAL_NONE
;
8767 else if (FUNCTION_RVALUE_QUALIFIED (type
))
8768 return REF_QUAL_RVALUE
;
8770 return REF_QUAL_LVALUE
;
8773 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8777 type_memfn_quals (const_tree type
)
8779 if (TREE_CODE (type
) == FUNCTION_TYPE
)
8780 return TYPE_QUALS (type
);
8781 else if (TREE_CODE (type
) == METHOD_TYPE
)
8782 return cp_type_quals (class_of_this_parm (type
));
8787 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8788 MEMFN_QUALS and its ref-qualifier to RQUAL. */
8791 apply_memfn_quals (tree type
, cp_cv_quals memfn_quals
, cp_ref_qualifier rqual
)
8793 /* Could handle METHOD_TYPE here if necessary. */
8794 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
8795 if (TYPE_QUALS (type
) == memfn_quals
8796 && type_memfn_rqual (type
) == rqual
)
8799 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8801 tree result
= build_qualified_type (type
, memfn_quals
);
8802 result
= build_exception_variant (result
, TYPE_RAISES_EXCEPTIONS (type
));
8803 return build_ref_qualified_type (result
, rqual
);
8806 /* Returns nonzero if TYPE is const or volatile. */
8809 cv_qualified_p (const_tree type
)
8811 int quals
= cp_type_quals (type
);
8812 return (quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)) != 0;
8815 /* Returns nonzero if the TYPE contains a mutable member. */
8818 cp_has_mutable_p (const_tree type
)
8820 /* This CONST_CAST is okay because strip_array_types returns its
8821 argument unmodified and we assign it to a const_tree. */
8822 type
= strip_array_types (CONST_CAST_TREE(type
));
8824 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
8827 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8828 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8829 approximation. In particular, consider:
8832 struct S { int i; };
8833 const S s = { f(); }
8835 Here, we will make "s" as TREE_READONLY (because it is declared
8836 "const") -- only to reverse ourselves upon seeing that the
8837 initializer is non-constant. */
8840 cp_apply_type_quals_to_decl (int type_quals
, tree decl
)
8842 tree type
= TREE_TYPE (decl
);
8844 if (type
== error_mark_node
)
8847 if (TREE_CODE (decl
) == TYPE_DECL
)
8850 gcc_assert (!(TREE_CODE (type
) == FUNCTION_TYPE
8851 && type_quals
!= TYPE_UNQUALIFIED
));
8853 /* Avoid setting TREE_READONLY incorrectly. */
8854 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8855 constructor can produce constant init, so rely on cp_finish_decl to
8856 clear TREE_READONLY if the variable has non-constant init. */
8858 /* If the type has (or might have) a mutable component, that component
8859 might be modified. */
8860 if (TYPE_HAS_MUTABLE_P (type
) || !COMPLETE_TYPE_P (type
))
8861 type_quals
&= ~TYPE_QUAL_CONST
;
8863 c_apply_type_quals_to_decl (type_quals
, decl
);
8866 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8867 exemplar types such that casting T1 to T2 is casting away constness
8868 if and only if there is no implicit conversion from T1 to T2. */
8871 casts_away_constness_r (tree
*t1
, tree
*t2
, tsubst_flags_t complain
)
8876 /* [expr.const.cast]
8878 For multi-level pointer to members and multi-level mixed pointers
8879 and pointers to members (conv.qual), the "member" aspect of a
8880 pointer to member level is ignored when determining if a const
8881 cv-qualifier has been cast away. */
8882 /* [expr.const.cast]
8884 For two pointer types:
8886 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8887 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
8890 casting from X1 to X2 casts away constness if, for a non-pointer
8891 type T there does not exist an implicit conversion (clause
8894 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
8898 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
8899 if ((!TYPE_PTR_P (*t1
) && !TYPE_PTRDATAMEM_P (*t1
))
8900 || (!TYPE_PTR_P (*t2
) && !TYPE_PTRDATAMEM_P (*t2
)))
8902 *t1
= cp_build_qualified_type (void_type_node
,
8903 cp_type_quals (*t1
));
8904 *t2
= cp_build_qualified_type (void_type_node
,
8905 cp_type_quals (*t2
));
8909 quals1
= cp_type_quals (*t1
);
8910 quals2
= cp_type_quals (*t2
);
8912 if (TYPE_PTRDATAMEM_P (*t1
))
8913 *t1
= TYPE_PTRMEM_POINTED_TO_TYPE (*t1
);
8915 *t1
= TREE_TYPE (*t1
);
8916 if (TYPE_PTRDATAMEM_P (*t2
))
8917 *t2
= TYPE_PTRMEM_POINTED_TO_TYPE (*t2
);
8919 *t2
= TREE_TYPE (*t2
);
8921 casts_away_constness_r (t1
, t2
, complain
);
8922 *t1
= build_pointer_type (*t1
);
8923 *t2
= build_pointer_type (*t2
);
8924 *t1
= cp_build_qualified_type (*t1
, quals1
);
8925 *t2
= cp_build_qualified_type (*t2
, quals2
);
8928 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
8931 ??? This function returns non-zero if casting away qualifiers not
8932 just const. We would like to return to the caller exactly which
8933 qualifiers are casted away to give more accurate diagnostics.
8937 casts_away_constness (tree t1
, tree t2
, tsubst_flags_t complain
)
8939 if (TREE_CODE (t2
) == REFERENCE_TYPE
)
8941 /* [expr.const.cast]
8943 Casting from an lvalue of type T1 to an lvalue of type T2
8944 using a reference cast casts away constness if a cast from an
8945 rvalue of type "pointer to T1" to the type "pointer to T2"
8946 casts away constness. */
8947 t1
= (TREE_CODE (t1
) == REFERENCE_TYPE
? TREE_TYPE (t1
) : t1
);
8948 return casts_away_constness (build_pointer_type (t1
),
8949 build_pointer_type (TREE_TYPE (t2
)),
8953 if (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
8954 /* [expr.const.cast]
8956 Casting from an rvalue of type "pointer to data member of X
8957 of type T1" to the type "pointer to data member of Y of type
8958 T2" casts away constness if a cast from an rvalue of type
8959 "pointer to T1" to the type "pointer to T2" casts away
8961 return casts_away_constness
8962 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1
)),
8963 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2
)),
8966 /* Casting away constness is only something that makes sense for
8967 pointer or reference types. */
8968 if (!TYPE_PTR_P (t1
) || !TYPE_PTR_P (t2
))
8971 /* Top-level qualifiers don't matter. */
8972 t1
= TYPE_MAIN_VARIANT (t1
);
8973 t2
= TYPE_MAIN_VARIANT (t2
);
8974 casts_away_constness_r (&t1
, &t2
, complain
);
8975 if (!can_convert (t2
, t1
, complain
))
8981 /* If T is a REFERENCE_TYPE return the type to which T refers.
8982 Otherwise, return T itself. */
8985 non_reference (tree t
)
8987 if (t
&& TREE_CODE (t
) == REFERENCE_TYPE
)
8993 /* Return nonzero if REF is an lvalue valid for this language;
8994 otherwise, print an error message and return zero. USE says
8995 how the lvalue is being used and so selects the error message. */
8998 lvalue_or_else (tree ref
, enum lvalue_use use
, tsubst_flags_t complain
)
9000 cp_lvalue_kind kind
= lvalue_kind (ref
);
9002 if (kind
== clk_none
)
9004 if (complain
& tf_error
)
9005 lvalue_error (input_location
, use
);
9008 else if (kind
& (clk_rvalueref
|clk_class
))
9010 if (!(complain
& tf_error
))
9012 if (kind
& clk_class
)
9013 /* Make this a permerror because we used to accept it. */
9014 permerror (input_location
, "using temporary as lvalue");
9016 error ("using xvalue (rvalue reference) as lvalue");
9021 /* Return true if a user-defined literal operator is a raw operator. */
9024 check_raw_literal_operator (const_tree decl
)
9026 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9029 bool maybe_raw_p
= false;
9031 /* Count the number and type of arguments and check for ellipsis. */
9032 for (argtype
= argtypes
, arity
= 0;
9033 argtype
&& argtype
!= void_list_node
;
9034 ++arity
, argtype
= TREE_CHAIN (argtype
))
9036 tree t
= TREE_VALUE (argtype
);
9038 if (same_type_p (t
, const_string_type_node
))
9042 return false; /* Found ellipsis. */
9044 if (!maybe_raw_p
|| arity
!= 1)
9051 /* Return true if a user-defined literal operator has one of the allowed
9055 check_literal_operator_args (const_tree decl
,
9056 bool *long_long_unsigned_p
, bool *long_double_p
)
9058 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9060 *long_long_unsigned_p
= false;
9061 *long_double_p
= false;
9062 if (processing_template_decl
|| processing_specialization
)
9063 return argtypes
== void_list_node
;
9070 /* Count the number and type of arguments and check for ellipsis. */
9071 for (argtype
= argtypes
, arity
= 0;
9072 argtype
&& argtype
!= void_list_node
;
9073 argtype
= TREE_CHAIN (argtype
))
9075 tree t
= TREE_VALUE (argtype
);
9080 bool maybe_raw_p
= false;
9082 if (cp_type_quals (t
) != TYPE_QUAL_CONST
)
9084 t
= TYPE_MAIN_VARIANT (t
);
9085 if ((maybe_raw_p
= same_type_p (t
, char_type_node
))
9086 || same_type_p (t
, wchar_type_node
)
9087 || same_type_p (t
, char16_type_node
)
9088 || same_type_p (t
, char32_type_node
))
9090 argtype
= TREE_CHAIN (argtype
);
9093 t
= TREE_VALUE (argtype
);
9094 if (maybe_raw_p
&& argtype
== void_list_node
)
9096 else if (same_type_p (t
, size_type_node
))
9105 else if (same_type_p (t
, long_long_unsigned_type_node
))
9108 *long_long_unsigned_p
= true;
9110 else if (same_type_p (t
, long_double_type_node
))
9113 *long_double_p
= true;
9115 else if (same_type_p (t
, char_type_node
))
9117 else if (same_type_p (t
, wchar_type_node
))
9119 else if (same_type_p (t
, char16_type_node
))
9121 else if (same_type_p (t
, char32_type_node
))
9127 return false; /* Found ellipsis. */
9129 if (arity
!= max_arity
)