1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file is part of the C++ front end.
23 It contains routines to build C++ expressions given their operands,
24 including computing the types of the result, C and C++ specific error
25 checks, and some optimization. */
29 #include "coretypes.h"
32 #include "stor-layout.h"
36 #include "c-family/c-objc.h"
37 #include "c-family/c-ubsan.h"
39 #include "gcc-rich-location.h"
40 #include "stringpool.h"
44 static tree
cp_build_addr_expr_strict (tree
, tsubst_flags_t
);
45 static tree
cp_build_function_call (tree
, tree
, tsubst_flags_t
);
46 static tree
pfn_from_ptrmemfunc (tree
);
47 static tree
delta_from_ptrmemfunc (tree
);
48 static tree
convert_for_assignment (tree
, tree
, impl_conv_rhs
, tree
, int,
50 static tree
cp_pointer_int_sum (location_t
, enum tree_code
, tree
, tree
,
52 static tree
rationalize_conditional_expr (enum tree_code
, tree
,
54 static int comp_ptr_ttypes_real (tree
, tree
, int);
55 static bool comp_except_types (tree
, tree
, bool);
56 static bool comp_array_types (const_tree
, const_tree
, bool);
57 static tree
pointer_diff (location_t
, tree
, tree
, tree
, tsubst_flags_t
);
58 static tree
get_delta_difference (tree
, tree
, bool, bool, tsubst_flags_t
);
59 static void casts_away_constness_r (tree
*, tree
*, tsubst_flags_t
);
60 static bool casts_away_constness (tree
, tree
, tsubst_flags_t
);
61 static bool maybe_warn_about_returning_address_of_local (tree
);
62 static tree
lookup_destructor (tree
, tree
, tree
, tsubst_flags_t
);
63 static void error_args_num (location_t
, tree
, bool);
64 static int convert_arguments (tree
, vec
<tree
, va_gc
> **, tree
, int,
67 /* Do `exp = require_complete_type (exp);' to make sure exp
68 does not have an incomplete type. (That includes void types.)
69 Returns error_mark_node if the VALUE does not have
70 complete type when this function returns. */
73 require_complete_type_sfinae (tree value
, tsubst_flags_t complain
)
77 if (processing_template_decl
|| value
== error_mark_node
)
80 if (TREE_CODE (value
) == OVERLOAD
)
81 type
= unknown_type_node
;
83 type
= TREE_TYPE (value
);
85 if (type
== error_mark_node
)
86 return error_mark_node
;
88 /* First, detect a valid value with a complete type. */
89 if (COMPLETE_TYPE_P (type
))
92 if (complete_type_or_maybe_complain (type
, value
, complain
))
95 return error_mark_node
;
99 require_complete_type (tree value
)
101 return require_complete_type_sfinae (value
, tf_warning_or_error
);
104 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
105 a template instantiation, do the instantiation. Returns TYPE,
106 whether or not it could be completed, unless something goes
107 horribly wrong, in which case the error_mark_node is returned. */
110 complete_type (tree type
)
112 if (type
== NULL_TREE
)
113 /* Rather than crash, we return something sure to cause an error
115 return error_mark_node
;
117 if (type
== error_mark_node
|| COMPLETE_TYPE_P (type
))
119 else if (TREE_CODE (type
) == ARRAY_TYPE
)
121 tree t
= complete_type (TREE_TYPE (type
));
122 unsigned int needs_constructing
, has_nontrivial_dtor
;
123 if (COMPLETE_TYPE_P (t
) && !dependent_type_p (type
))
126 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
128 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
129 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
131 TYPE_NEEDS_CONSTRUCTING (t
) = needs_constructing
;
132 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = has_nontrivial_dtor
;
135 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
136 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
141 /* Like complete_type, but issue an error if the TYPE cannot be completed.
142 VALUE is used for informative diagnostics.
143 Returns NULL_TREE if the type cannot be made complete. */
146 complete_type_or_maybe_complain (tree type
, tree value
, tsubst_flags_t complain
)
148 type
= complete_type (type
);
149 if (type
== error_mark_node
)
150 /* We already issued an error. */
152 else if (!COMPLETE_TYPE_P (type
))
154 if (complain
& tf_error
)
155 cxx_incomplete_type_diagnostic (value
, type
, DK_ERROR
);
163 complete_type_or_else (tree type
, tree value
)
165 return complete_type_or_maybe_complain (type
, value
, tf_warning_or_error
);
169 /* Return the common type of two parameter lists.
170 We assume that comptypes has already been done and returned 1;
171 if that isn't so, this may crash.
173 As an optimization, free the space we allocate if the parameter
174 lists are already common. */
177 commonparms (tree p1
, tree p2
)
179 tree oldargs
= p1
, newargs
, n
;
183 len
= list_length (p1
);
184 newargs
= tree_last (p1
);
186 if (newargs
== void_list_node
)
195 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
200 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
202 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
204 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
207 else if (! TREE_PURPOSE (p1
))
209 if (TREE_PURPOSE (p2
))
211 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
217 if (1 != simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)))
219 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
221 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
224 TREE_VALUE (n
) = merge_types (TREE_VALUE (p1
), TREE_VALUE (p2
));
227 TREE_VALUE (n
) = TREE_VALUE (p1
);
235 /* Given a type, perhaps copied for a typedef,
236 find the "original" version of it. */
238 original_type (tree t
)
240 int quals
= cp_type_quals (t
);
241 while (t
!= error_mark_node
242 && TYPE_NAME (t
) != NULL_TREE
)
244 tree x
= TYPE_NAME (t
);
245 if (TREE_CODE (x
) != TYPE_DECL
)
247 x
= DECL_ORIGINAL_TYPE (x
);
252 return cp_build_qualified_type (t
, quals
);
255 /* Return the common type for two arithmetic types T1 and T2 under the
256 usual arithmetic conversions. The default conversions have already
257 been applied, and enumerated types converted to their compatible
261 cp_common_type (tree t1
, tree t2
)
263 enum tree_code code1
= TREE_CODE (t1
);
264 enum tree_code code2
= TREE_CODE (t2
);
269 /* In what follows, we slightly generalize the rules given in [expr] so
270 as to deal with `long long' and `complex'. First, merge the
272 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
274 if (SCOPED_ENUM_P (t1
) || SCOPED_ENUM_P (t2
))
276 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
277 return build_type_attribute_variant (t1
, attributes
);
282 /* FIXME: Attributes. */
283 gcc_assert (ARITHMETIC_TYPE_P (t1
)
284 || VECTOR_TYPE_P (t1
)
285 || UNSCOPED_ENUM_P (t1
));
286 gcc_assert (ARITHMETIC_TYPE_P (t2
)
287 || VECTOR_TYPE_P (t2
)
288 || UNSCOPED_ENUM_P (t2
));
290 /* If one type is complex, form the common type of the non-complex
291 components, then make that complex. Use T1 or T2 if it is the
293 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
295 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
296 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
298 = type_after_usual_arithmetic_conversions (subtype1
, subtype2
);
300 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
301 return build_type_attribute_variant (t1
, attributes
);
302 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
303 return build_type_attribute_variant (t2
, attributes
);
305 return build_type_attribute_variant (build_complex_type (subtype
),
309 if (code1
== VECTOR_TYPE
)
311 /* When we get here we should have two vectors of the same size.
312 Just prefer the unsigned one if present. */
313 if (TYPE_UNSIGNED (t1
))
314 return build_type_attribute_variant (t1
, attributes
);
316 return build_type_attribute_variant (t2
, attributes
);
319 /* If only one is real, use it as the result. */
320 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
321 return build_type_attribute_variant (t1
, attributes
);
322 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
323 return build_type_attribute_variant (t2
, attributes
);
325 /* Both real or both integers; use the one with greater precision. */
326 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
327 return build_type_attribute_variant (t1
, attributes
);
328 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
329 return build_type_attribute_variant (t2
, attributes
);
331 /* The types are the same; no need to do anything fancy. */
332 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
333 return build_type_attribute_variant (t1
, attributes
);
335 if (code1
!= REAL_TYPE
)
337 /* If one is unsigned long long, then convert the other to unsigned
339 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_unsigned_type_node
)
340 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_unsigned_type_node
))
341 return build_type_attribute_variant (long_long_unsigned_type_node
,
343 /* If one is a long long, and the other is an unsigned long, and
344 long long can represent all the values of an unsigned long, then
345 convert to a long long. Otherwise, convert to an unsigned long
346 long. Otherwise, if either operand is long long, convert the
349 Since we're here, we know the TYPE_PRECISION is the same;
350 therefore converting to long long cannot represent all the values
351 of an unsigned long, so we choose unsigned long long in that
353 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_integer_type_node
)
354 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_integer_type_node
))
356 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
357 ? long_long_unsigned_type_node
358 : long_long_integer_type_node
);
359 return build_type_attribute_variant (t
, attributes
);
362 /* Go through the same procedure, but for longs. */
363 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_unsigned_type_node
)
364 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_unsigned_type_node
))
365 return build_type_attribute_variant (long_unsigned_type_node
,
367 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_integer_type_node
)
368 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_integer_type_node
))
370 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
371 ? long_unsigned_type_node
: long_integer_type_node
);
372 return build_type_attribute_variant (t
, attributes
);
375 /* For __intN types, either the type is __int128 (and is lower
376 priority than the types checked above, but higher than other
377 128-bit types) or it's known to not be the same size as other
378 types (enforced in toplev.c). Prefer the unsigned type. */
379 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
381 if (int_n_enabled_p
[i
]
382 && (same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].signed_type
)
383 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].signed_type
)
384 || same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].unsigned_type
)
385 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].unsigned_type
)))
387 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
388 ? int_n_trees
[i
].unsigned_type
389 : int_n_trees
[i
].signed_type
);
390 return build_type_attribute_variant (t
, attributes
);
394 /* Otherwise prefer the unsigned one. */
395 if (TYPE_UNSIGNED (t1
))
396 return build_type_attribute_variant (t1
, attributes
);
398 return build_type_attribute_variant (t2
, attributes
);
402 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_double_type_node
)
403 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_double_type_node
))
404 return build_type_attribute_variant (long_double_type_node
,
406 if (same_type_p (TYPE_MAIN_VARIANT (t1
), double_type_node
)
407 || same_type_p (TYPE_MAIN_VARIANT (t2
), double_type_node
))
408 return build_type_attribute_variant (double_type_node
,
410 if (same_type_p (TYPE_MAIN_VARIANT (t1
), float_type_node
)
411 || same_type_p (TYPE_MAIN_VARIANT (t2
), float_type_node
))
412 return build_type_attribute_variant (float_type_node
,
415 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
416 the standard C++ floating-point types. Logic earlier in this
417 function has already eliminated the possibility that
418 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
419 compelling reason to choose one or the other. */
420 return build_type_attribute_variant (t1
, attributes
);
424 /* T1 and T2 are arithmetic or enumeration types. Return the type
425 that will result from the "usual arithmetic conversions" on T1 and
426 T2 as described in [expr]. */
429 type_after_usual_arithmetic_conversions (tree t1
, tree t2
)
431 gcc_assert (ARITHMETIC_TYPE_P (t1
)
432 || VECTOR_TYPE_P (t1
)
433 || UNSCOPED_ENUM_P (t1
));
434 gcc_assert (ARITHMETIC_TYPE_P (t2
)
435 || VECTOR_TYPE_P (t2
)
436 || UNSCOPED_ENUM_P (t2
));
438 /* Perform the integral promotions. We do not promote real types here. */
439 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1
)
440 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2
))
442 t1
= type_promotes_to (t1
);
443 t2
= type_promotes_to (t2
);
446 return cp_common_type (t1
, t2
);
450 composite_pointer_error (diagnostic_t kind
, tree t1
, tree t2
,
451 composite_pointer_operation operation
)
456 emit_diagnostic (kind
, input_location
, 0,
457 "comparison between "
458 "distinct pointer types %qT and %qT lacks a cast",
462 emit_diagnostic (kind
, input_location
, 0,
463 "conversion between "
464 "distinct pointer types %qT and %qT lacks a cast",
467 case CPO_CONDITIONAL_EXPR
:
468 emit_diagnostic (kind
, input_location
, 0,
469 "conditional expression between "
470 "distinct pointer types %qT and %qT lacks a cast",
478 /* Subroutine of composite_pointer_type to implement the recursive
479 case. See that function for documentation of the parameters. */
482 composite_pointer_type_r (tree t1
, tree t2
,
483 composite_pointer_operation operation
,
484 tsubst_flags_t complain
)
491 /* Determine the types pointed to by T1 and T2. */
494 pointee1
= TREE_TYPE (t1
);
495 pointee2
= TREE_TYPE (t2
);
499 pointee1
= TYPE_PTRMEM_POINTED_TO_TYPE (t1
);
500 pointee2
= TYPE_PTRMEM_POINTED_TO_TYPE (t2
);
505 Otherwise, the composite pointer type is a pointer type
506 similar (_conv.qual_) to the type of one of the operands,
507 with a cv-qualification signature (_conv.qual_) that is the
508 union of the cv-qualification signatures of the operand
510 if (same_type_ignoring_top_level_qualifiers_p (pointee1
, pointee2
))
511 result_type
= pointee1
;
512 else if ((TYPE_PTR_P (pointee1
) && TYPE_PTR_P (pointee2
))
513 || (TYPE_PTRMEM_P (pointee1
) && TYPE_PTRMEM_P (pointee2
)))
515 result_type
= composite_pointer_type_r (pointee1
, pointee2
, operation
,
517 if (result_type
== error_mark_node
)
518 return error_mark_node
;
522 if (complain
& tf_error
)
523 composite_pointer_error (DK_PERMERROR
, t1
, t2
, operation
);
525 return error_mark_node
;
526 result_type
= void_type_node
;
528 result_type
= cp_build_qualified_type (result_type
,
529 (cp_type_quals (pointee1
)
530 | cp_type_quals (pointee2
)));
531 /* If the original types were pointers to members, so is the
533 if (TYPE_PTRMEM_P (t1
))
535 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
536 TYPE_PTRMEM_CLASS_TYPE (t2
)))
538 if (complain
& tf_error
)
539 composite_pointer_error (DK_PERMERROR
, t1
, t2
, operation
);
541 return error_mark_node
;
543 result_type
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
547 result_type
= build_pointer_type (result_type
);
549 /* Merge the attributes. */
550 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
551 return build_type_attribute_variant (result_type
, attributes
);
554 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
555 ARG1 and ARG2 are the values with those types. The OPERATION is to
556 describe the operation between the pointer types,
557 in case an error occurs.
559 This routine also implements the computation of a common type for
560 pointers-to-members as per [expr.eq]. */
563 composite_pointer_type (tree t1
, tree t2
, tree arg1
, tree arg2
,
564 composite_pointer_operation operation
,
565 tsubst_flags_t complain
)
572 If one operand is a null pointer constant, the composite pointer
573 type is the type of the other operand. */
574 if (null_ptr_cst_p (arg1
))
576 if (null_ptr_cst_p (arg2
))
583 If one of the operands has type "pointer to cv1 void*", then
584 the other has type "pointer to cv2T", and the composite pointer
585 type is "pointer to cv12 void", where cv12 is the union of cv1
588 If either type is a pointer to void, make sure it is T1. */
589 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
))
600 if (complain
& tf_error
)
605 pedwarn (input_location
, OPT_Wpedantic
,
606 "ISO C++ forbids comparison between pointer "
607 "of type %<void *%> and pointer-to-function");
610 pedwarn (input_location
, OPT_Wpedantic
,
611 "ISO C++ forbids conversion between pointer "
612 "of type %<void *%> and pointer-to-function");
614 case CPO_CONDITIONAL_EXPR
:
615 pedwarn (input_location
, OPT_Wpedantic
,
616 "ISO C++ forbids conditional expression between "
617 "pointer of type %<void *%> and "
618 "pointer-to-function");
625 return error_mark_node
;
628 = cp_build_qualified_type (void_type_node
,
629 (cp_type_quals (TREE_TYPE (t1
))
630 | cp_type_quals (TREE_TYPE (t2
))));
631 result_type
= build_pointer_type (result_type
);
632 /* Merge the attributes. */
633 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
634 return build_type_attribute_variant (result_type
, attributes
);
637 if (c_dialect_objc () && TYPE_PTR_P (t1
)
640 if (objc_have_common_type (t1
, t2
, -3, NULL_TREE
))
641 return objc_common_type (t1
, t2
);
644 /* if T1 or T2 is "pointer to noexcept function" and the other type is
645 "pointer to function", where the function types are otherwise the same,
646 "pointer to function" */
647 if (fnptr_conv_p (t1
, t2
))
649 if (fnptr_conv_p (t2
, t1
))
652 /* [expr.eq] permits the application of a pointer conversion to
653 bring the pointers to a common type. */
654 if (TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
)
655 && CLASS_TYPE_P (TREE_TYPE (t1
))
656 && CLASS_TYPE_P (TREE_TYPE (t2
))
657 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1
),
660 class1
= TREE_TYPE (t1
);
661 class2
= TREE_TYPE (t2
);
663 if (DERIVED_FROM_P (class1
, class2
))
664 t2
= (build_pointer_type
665 (cp_build_qualified_type (class1
, cp_type_quals (class2
))));
666 else if (DERIVED_FROM_P (class2
, class1
))
667 t1
= (build_pointer_type
668 (cp_build_qualified_type (class2
, cp_type_quals (class1
))));
671 if (complain
& tf_error
)
672 composite_pointer_error (DK_ERROR
, t1
, t2
, operation
);
673 return error_mark_node
;
676 /* [expr.eq] permits the application of a pointer-to-member
677 conversion to change the class type of one of the types. */
678 else if (TYPE_PTRMEM_P (t1
)
679 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
680 TYPE_PTRMEM_CLASS_TYPE (t2
)))
682 class1
= TYPE_PTRMEM_CLASS_TYPE (t1
);
683 class2
= TYPE_PTRMEM_CLASS_TYPE (t2
);
685 if (DERIVED_FROM_P (class1
, class2
))
686 t1
= build_ptrmem_type (class2
, TYPE_PTRMEM_POINTED_TO_TYPE (t1
));
687 else if (DERIVED_FROM_P (class2
, class1
))
688 t2
= build_ptrmem_type (class1
, TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
691 if (complain
& tf_error
)
695 error ("comparison between distinct "
696 "pointer-to-member types %qT and %qT lacks a cast",
700 error ("conversion between distinct "
701 "pointer-to-member types %qT and %qT lacks a cast",
704 case CPO_CONDITIONAL_EXPR
:
705 error ("conditional expression between distinct "
706 "pointer-to-member types %qT and %qT lacks a cast",
712 return error_mark_node
;
716 return composite_pointer_type_r (t1
, t2
, operation
, complain
);
719 /* Return the merged type of two types.
720 We assume that comptypes has already been done and returned 1;
721 if that isn't so, this may crash.
723 This just combines attributes and default arguments; any other
724 differences would cause the two types to compare unalike. */
727 merge_types (tree t1
, tree t2
)
729 enum tree_code code1
;
730 enum tree_code code2
;
733 /* Save time if the two types are the same. */
736 if (original_type (t1
) == original_type (t2
))
739 /* If one type is nonsense, use the other. */
740 if (t1
== error_mark_node
)
742 if (t2
== error_mark_node
)
745 /* Handle merging an auto redeclaration with a previous deduced
750 /* Merge the attributes. */
751 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
753 if (TYPE_PTRMEMFUNC_P (t1
))
754 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
755 if (TYPE_PTRMEMFUNC_P (t2
))
756 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
758 code1
= TREE_CODE (t1
);
759 code2
= TREE_CODE (t2
);
762 gcc_assert (code1
== TYPENAME_TYPE
|| code2
== TYPENAME_TYPE
);
763 if (code1
== TYPENAME_TYPE
)
765 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
766 code1
= TREE_CODE (t1
);
770 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
771 code2
= TREE_CODE (t2
);
779 /* For two pointers, do this recursively on the target type. */
781 tree target
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
782 int quals
= cp_type_quals (t1
);
784 if (code1
== POINTER_TYPE
)
786 t1
= build_pointer_type (target
);
787 if (TREE_CODE (target
) == METHOD_TYPE
)
788 t1
= build_ptrmemfunc_type (t1
);
791 t1
= cp_build_reference_type (target
, TYPE_REF_IS_RVALUE (t1
));
792 t1
= build_type_attribute_variant (t1
, attributes
);
793 t1
= cp_build_qualified_type (t1
, quals
);
802 quals
= cp_type_quals (t1
);
803 pointee
= merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1
),
804 TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
805 t1
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
807 t1
= cp_build_qualified_type (t1
, quals
);
813 tree elt
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
814 /* Save space: see if the result is identical to one of the args. */
815 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
816 return build_type_attribute_variant (t1
, attributes
);
817 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
818 return build_type_attribute_variant (t2
, attributes
);
819 /* Merge the element types, and have a size if either arg has one. */
820 t1
= build_cplus_array_type
821 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
826 /* Function types: prefer the one that specified arg types.
827 If both do, merge the arg types. Also merge the return types. */
829 tree valtype
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
830 tree p1
= TYPE_ARG_TYPES (t1
);
831 tree p2
= TYPE_ARG_TYPES (t2
);
834 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
836 /* Save space: see if the result is identical to one of the args. */
837 if (valtype
== TREE_TYPE (t1
) && ! p2
)
838 return cp_build_type_attribute_variant (t1
, attributes
);
839 if (valtype
== TREE_TYPE (t2
) && ! p1
)
840 return cp_build_type_attribute_variant (t2
, attributes
);
842 /* Simple way if one arg fails to specify argument types. */
843 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
845 else if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
848 parms
= commonparms (p1
, p2
);
850 rval
= build_function_type (valtype
, parms
);
851 gcc_assert (type_memfn_quals (t1
) == type_memfn_quals (t2
));
852 gcc_assert (type_memfn_rqual (t1
) == type_memfn_rqual (t2
));
853 rval
= apply_memfn_quals (rval
,
854 type_memfn_quals (t1
),
855 type_memfn_rqual (t1
));
856 raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
857 TYPE_RAISES_EXCEPTIONS (t2
));
858 t1
= build_exception_variant (rval
, raises
);
859 if (late_return_type_p
)
860 TYPE_HAS_LATE_RETURN_TYPE (t1
) = 1;
866 /* Get this value the long way, since TYPE_METHOD_BASETYPE
867 is just the main variant of this. */
868 tree basetype
= class_of_this_parm (t2
);
869 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
870 TYPE_RAISES_EXCEPTIONS (t2
));
871 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
873 bool late_return_type_1_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
874 bool late_return_type_2_p
= TYPE_HAS_LATE_RETURN_TYPE (t2
);
876 /* If this was a member function type, get back to the
877 original type of type member function (i.e., without
878 the class instance variable up front. */
879 t1
= build_function_type (TREE_TYPE (t1
),
880 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
881 t2
= build_function_type (TREE_TYPE (t2
),
882 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
883 t3
= merge_types (t1
, t2
);
884 t3
= build_method_type_directly (basetype
, TREE_TYPE (t3
),
885 TYPE_ARG_TYPES (t3
));
886 t1
= build_exception_variant (t3
, raises
);
887 t1
= build_ref_qualified_type (t1
, rqual
);
888 if (late_return_type_1_p
)
889 TYPE_HAS_LATE_RETURN_TYPE (t1
) = 1;
890 if (late_return_type_2_p
)
891 TYPE_HAS_LATE_RETURN_TYPE (t2
) = 1;
896 /* There is no need to merge attributes into a TYPENAME_TYPE.
897 When the type is instantiated it will have whatever
898 attributes result from the instantiation. */
904 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
906 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
909 return cp_build_type_attribute_variant (t1
, attributes
);
912 /* Return the ARRAY_TYPE type without its domain. */
915 strip_array_domain (tree type
)
918 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
919 if (TYPE_DOMAIN (type
) == NULL_TREE
)
921 t2
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
922 return cp_build_type_attribute_variant (t2
, TYPE_ATTRIBUTES (type
));
925 /* Wrapper around cp_common_type that is used by c-common.c and other
926 front end optimizations that remove promotions.
928 Return the common type for two arithmetic types T1 and T2 under the
929 usual arithmetic conversions. The default conversions have already
930 been applied, and enumerated types converted to their compatible
934 common_type (tree t1
, tree t2
)
936 /* If one type is nonsense, use the other */
937 if (t1
== error_mark_node
)
939 if (t2
== error_mark_node
)
942 return cp_common_type (t1
, t2
);
945 /* Return the common type of two pointer types T1 and T2. This is the
946 type for the result of most arithmetic operations if the operands
947 have the given two types.
949 We assume that comp_target_types has already been done and returned
950 nonzero; if that isn't so, this may crash. */
953 common_pointer_type (tree t1
, tree t2
)
955 gcc_assert ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
956 || (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
957 || (TYPE_PTRMEMFUNC_P (t1
) && TYPE_PTRMEMFUNC_P (t2
)));
959 return composite_pointer_type (t1
, t2
, error_mark_node
, error_mark_node
,
960 CPO_CONVERSION
, tf_warning_or_error
);
963 /* Compare two exception specifier types for exactness or subsetness, if
964 allowed. Returns false for mismatch, true for match (same, or
967 [except.spec] "If a class X ... objects of class X or any class publicly
968 and unambiguously derived from X. Similarly, if a pointer type Y * ...
969 exceptions of type Y * or that are pointers to any type publicly and
970 unambiguously derived from Y. Otherwise a function only allows exceptions
971 that have the same type ..."
972 This does not mention cv qualifiers and is different to what throw
973 [except.throw] and catch [except.catch] will do. They will ignore the
974 top level cv qualifiers, and allow qualifiers in the pointer to class
977 We implement the letter of the standard. */
980 comp_except_types (tree a
, tree b
, bool exact
)
982 if (same_type_p (a
, b
))
986 if (cp_type_quals (a
) || cp_type_quals (b
))
989 if (TYPE_PTR_P (a
) && TYPE_PTR_P (b
))
993 if (cp_type_quals (a
) || cp_type_quals (b
))
997 if (TREE_CODE (a
) != RECORD_TYPE
998 || TREE_CODE (b
) != RECORD_TYPE
)
1001 if (publicly_uniquely_derived_p (a
, b
))
1007 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1008 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1009 If EXACT is ce_type, the C++17 type compatibility rules apply.
1010 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1011 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1012 are unordered, but we've already filtered out duplicates. Most lists will
1013 be in order, we should try to make use of that. */
1016 comp_except_specs (const_tree t1
, const_tree t2
, int exact
)
1025 /* First handle noexcept. */
1026 if (exact
< ce_exact
)
1028 if (exact
== ce_type
1029 && (canonical_eh_spec (CONST_CAST_TREE (t1
))
1030 == canonical_eh_spec (CONST_CAST_TREE (t2
))))
1033 /* noexcept(false) is compatible with no exception-specification,
1034 and less strict than any spec. */
1035 if (t1
== noexcept_false_spec
)
1036 return t2
== NULL_TREE
|| exact
== ce_derived
;
1037 /* Even a derived noexcept(false) is compatible with no
1038 exception-specification. */
1039 if (t2
== noexcept_false_spec
)
1040 return t1
== NULL_TREE
;
1042 /* Otherwise, if we aren't looking for an exact match, noexcept is
1043 equivalent to throw(). */
1044 if (t1
== noexcept_true_spec
)
1045 t1
= empty_except_spec
;
1046 if (t2
== noexcept_true_spec
)
1047 t2
= empty_except_spec
;
1050 /* If any noexcept is left, it is only comparable to itself;
1051 either we're looking for an exact match or we're redeclaring a
1052 template with dependent noexcept. */
1053 if ((t1
&& TREE_PURPOSE (t1
))
1054 || (t2
&& TREE_PURPOSE (t2
)))
1056 && cp_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)));
1058 if (t1
== NULL_TREE
) /* T1 is ... */
1059 return t2
== NULL_TREE
|| exact
== ce_derived
;
1060 if (!TREE_VALUE (t1
)) /* t1 is EMPTY */
1061 return t2
!= NULL_TREE
&& !TREE_VALUE (t2
);
1062 if (t2
== NULL_TREE
) /* T2 is ... */
1064 if (TREE_VALUE (t1
) && !TREE_VALUE (t2
)) /* T2 is EMPTY, T1 is not */
1065 return exact
== ce_derived
;
1067 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1068 Count how many we find, to determine exactness. For exact matching and
1069 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1071 for (base
= t1
; t2
!= NULL_TREE
; t2
= TREE_CHAIN (t2
))
1073 for (probe
= base
; probe
!= NULL_TREE
; probe
= TREE_CHAIN (probe
))
1075 tree a
= TREE_VALUE (probe
);
1076 tree b
= TREE_VALUE (t2
);
1078 if (comp_except_types (a
, b
, exact
))
1080 if (probe
== base
&& exact
> ce_derived
)
1081 base
= TREE_CHAIN (probe
);
1086 if (probe
== NULL_TREE
)
1089 return exact
== ce_derived
|| base
== NULL_TREE
|| length
== list_length (t1
);
1092 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1093 [] can match [size]. */
1096 comp_array_types (const_tree t1
, const_tree t2
, bool allow_redeclaration
)
1105 /* The type of the array elements must be the same. */
1106 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1109 d1
= TYPE_DOMAIN (t1
);
1110 d2
= TYPE_DOMAIN (t2
);
1115 /* If one of the arrays is dimensionless, and the other has a
1116 dimension, they are of different types. However, it is valid to
1124 declarations for an array object can specify
1125 array types that differ by the presence or absence of a major
1126 array bound (_dcl.array_). */
1128 return allow_redeclaration
;
1130 /* Check that the dimensions are the same. */
1132 if (!cp_tree_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
)))
1134 max1
= TYPE_MAX_VALUE (d1
);
1135 max2
= TYPE_MAX_VALUE (d2
);
1137 if (!cp_tree_equal (max1
, max2
))
1143 /* Compare the relative position of T1 and T2 into their respective
1144 template parameter list.
1145 T1 and T2 must be template parameter types.
1146 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1149 comp_template_parms_position (tree t1
, tree t2
)
1151 tree index1
, index2
;
1152 gcc_assert (t1
&& t2
1153 && TREE_CODE (t1
) == TREE_CODE (t2
)
1154 && (TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
1155 || TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
1156 || TREE_CODE (t1
) == TEMPLATE_TYPE_PARM
));
1158 index1
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1
));
1159 index2
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2
));
1161 /* Then compare their relative position. */
1162 if (TEMPLATE_PARM_IDX (index1
) != TEMPLATE_PARM_IDX (index2
)
1163 || TEMPLATE_PARM_LEVEL (index1
) != TEMPLATE_PARM_LEVEL (index2
)
1164 || (TEMPLATE_PARM_PARAMETER_PACK (index1
)
1165 != TEMPLATE_PARM_PARAMETER_PACK (index2
)))
1168 /* In C++14 we can end up comparing 'auto' to a normal template
1169 parameter. Don't confuse them. */
1170 if (cxx_dialect
>= cxx14
&& (is_auto (t1
) || is_auto (t2
)))
1171 return TYPE_IDENTIFIER (t1
) == TYPE_IDENTIFIER (t2
);
1176 /* Subroutine in comptypes. */
1179 structural_comptypes (tree t1
, tree t2
, int strict
)
1184 /* Suppress errors caused by previously reported errors. */
1185 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1188 gcc_assert (TYPE_P (t1
) && TYPE_P (t2
));
1190 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1191 current instantiation. */
1192 if (TREE_CODE (t1
) == TYPENAME_TYPE
)
1193 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
1195 if (TREE_CODE (t2
) == TYPENAME_TYPE
)
1196 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
1198 if (TYPE_PTRMEMFUNC_P (t1
))
1199 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
1200 if (TYPE_PTRMEMFUNC_P (t2
))
1201 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
1203 /* Different classes of types can't be compatible. */
1204 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1207 /* Qualifiers must match. For array types, we will check when we
1208 recur on the array element types. */
1209 if (TREE_CODE (t1
) != ARRAY_TYPE
1210 && cp_type_quals (t1
) != cp_type_quals (t2
))
1212 if (TREE_CODE (t1
) == FUNCTION_TYPE
1213 && type_memfn_quals (t1
) != type_memfn_quals (t2
))
1215 /* Need to check this before TYPE_MAIN_VARIANT.
1216 FIXME function qualifiers should really change the main variant. */
1217 if (TREE_CODE (t1
) == FUNCTION_TYPE
1218 || TREE_CODE (t1
) == METHOD_TYPE
)
1220 if (type_memfn_rqual (t1
) != type_memfn_rqual (t2
))
1222 if (flag_noexcept_type
1223 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1
),
1224 TYPE_RAISES_EXCEPTIONS (t2
),
1229 /* Allow for two different type nodes which have essentially the same
1230 definition. Note that we already checked for equality of the type
1231 qualifiers (just above). */
1233 if (TREE_CODE (t1
) != ARRAY_TYPE
1234 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1238 /* Compare the types. Break out if they could be the same. */
1239 switch (TREE_CODE (t1
))
1243 /* All void and bool types are the same. */
1247 case FIXED_POINT_TYPE
:
1249 /* With these nodes, we can't determine type equivalence by
1250 looking at what is stored in the nodes themselves, because
1251 two nodes might have different TYPE_MAIN_VARIANTs but still
1252 represent the same type. For example, wchar_t and int could
1253 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1254 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1255 and are distinct types. On the other hand, int and the
1258 typedef int INT __attribute((may_alias));
1260 have identical properties, different TYPE_MAIN_VARIANTs, but
1261 represent the same type. The canonical type system keeps
1262 track of equivalence in this case, so we fall back on it. */
1263 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1265 case TEMPLATE_TEMPLATE_PARM
:
1266 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1267 if (!comp_template_parms_position (t1
, t2
))
1269 if (!comp_template_parms
1270 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1
)),
1271 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2
))))
1273 if (TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
)
1275 /* Don't check inheritance. */
1276 strict
= COMPARE_STRICT
;
1281 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
1282 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
1283 || TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1284 && comp_template_args (TYPE_TI_ARGS (t1
), TYPE_TI_ARGS (t2
)))
1287 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
1289 else if ((strict
& COMPARE_DERIVED
) && DERIVED_FROM_P (t2
, t1
))
1295 if (!comptypes (TYPE_OFFSET_BASETYPE (t1
), TYPE_OFFSET_BASETYPE (t2
),
1296 strict
& ~COMPARE_REDECLARATION
))
1298 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1302 case REFERENCE_TYPE
:
1303 if (TYPE_REF_IS_RVALUE (t1
) != TYPE_REF_IS_RVALUE (t2
))
1305 /* fall through to checks for pointer types */
1309 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1310 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1316 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1318 if (!compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)))
1323 /* Target types must match incl. qualifiers. */
1324 if (!comp_array_types (t1
, t2
, !!(strict
& COMPARE_REDECLARATION
)))
1328 case TEMPLATE_TYPE_PARM
:
1329 /* If T1 and T2 don't have the same relative position in their
1330 template parameters set, they can't be equal. */
1331 if (!comp_template_parms_position (t1
, t2
))
1333 /* Constrained 'auto's are distinct from parms that don't have the same
1335 if (!equivalent_placeholder_constraints (t1
, t2
))
1340 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1
),
1341 TYPENAME_TYPE_FULLNAME (t2
)))
1343 /* Qualifiers don't matter on scopes. */
1344 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1
),
1349 case UNBOUND_CLASS_TEMPLATE
:
1350 if (!cp_tree_equal (TYPE_IDENTIFIER (t1
), TYPE_IDENTIFIER (t2
)))
1352 if (!same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
)))
1357 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1362 if (TYPE_VECTOR_SUBPARTS (t1
) != TYPE_VECTOR_SUBPARTS (t2
)
1363 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1367 case TYPE_PACK_EXPANSION
:
1368 return (same_type_p (PACK_EXPANSION_PATTERN (t1
),
1369 PACK_EXPANSION_PATTERN (t2
))
1370 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1
),
1371 PACK_EXPANSION_EXTRA_ARGS (t2
)));
1374 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1
)
1375 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2
)
1376 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1
)
1377 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2
))
1378 || (DECLTYPE_FOR_LAMBDA_PROXY (t1
)
1379 != DECLTYPE_FOR_LAMBDA_PROXY (t2
))
1380 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1
),
1381 DECLTYPE_TYPE_EXPR (t2
)))
1385 case UNDERLYING_TYPE
:
1386 return same_type_p (UNDERLYING_TYPE_TYPE (t1
),
1387 UNDERLYING_TYPE_TYPE (t2
));
1393 /* If we get here, we know that from a target independent POV the
1394 types are the same. Make sure the target attributes are also
1396 return comp_type_attributes (t1
, t2
);
1399 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1400 is a bitwise-or of the COMPARE_* flags. */
1403 comptypes (tree t1
, tree t2
, int strict
)
1405 if (strict
== COMPARE_STRICT
)
1410 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1413 if (TYPE_STRUCTURAL_EQUALITY_P (t1
) || TYPE_STRUCTURAL_EQUALITY_P (t2
))
1414 /* At least one of the types requires structural equality, so
1415 perform a deep check. */
1416 return structural_comptypes (t1
, t2
, strict
);
1418 if (flag_checking
&& USE_CANONICAL_TYPES
)
1420 bool result
= structural_comptypes (t1
, t2
, strict
);
1422 if (result
&& TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1423 /* The two types are structurally equivalent, but their
1424 canonical types were different. This is a failure of the
1425 canonical type propagation code.*/
1427 ("canonical types differ for identical types %qT and %qT",
1429 else if (!result
&& TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
))
1430 /* Two types are structurally different, but the canonical
1431 types are the same. This means we were over-eager in
1432 assigning canonical types. */
1434 ("same canonical type node for different types %qT and %qT",
1439 if (!flag_checking
&& USE_CANONICAL_TYPES
)
1440 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1442 return structural_comptypes (t1
, t2
, strict
);
1444 else if (strict
== COMPARE_STRUCTURAL
)
1445 return structural_comptypes (t1
, t2
, COMPARE_STRICT
);
1447 return structural_comptypes (t1
, t2
, strict
);
1450 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1451 top-level qualifiers. */
1454 same_type_ignoring_top_level_qualifiers_p (tree type1
, tree type2
)
1456 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1459 type1
= cp_build_qualified_type (type1
, TYPE_UNQUALIFIED
);
1460 type2
= cp_build_qualified_type (type2
, TYPE_UNQUALIFIED
);
1461 return same_type_p (type1
, type2
);
1464 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1467 at_least_as_qualified_p (const_tree type1
, const_tree type2
)
1469 int q1
= cp_type_quals (type1
);
1470 int q2
= cp_type_quals (type2
);
1472 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1473 return (q1
& q2
) == q2
;
1476 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1477 more cv-qualified that TYPE1, and 0 otherwise. */
1480 comp_cv_qualification (int q1
, int q2
)
1485 if ((q1
& q2
) == q2
)
1487 else if ((q1
& q2
) == q1
)
1494 comp_cv_qualification (const_tree type1
, const_tree type2
)
1496 int q1
= cp_type_quals (type1
);
1497 int q2
= cp_type_quals (type2
);
1498 return comp_cv_qualification (q1
, q2
);
1501 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1502 subset of the cv-qualification signature of TYPE2, and the types
1503 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1506 comp_cv_qual_signature (tree type1
, tree type2
)
1508 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1510 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1516 /* Subroutines of `comptypes'. */
1518 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1519 equivalent in the sense that functions with those parameter types
1520 can have equivalent types. The two lists must be equivalent,
1521 element by element. */
1524 compparms (const_tree parms1
, const_tree parms2
)
1528 /* An unspecified parmlist matches any specified parmlist
1529 whose argument types don't need default promotions. */
1531 for (t1
= parms1
, t2
= parms2
;
1533 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1535 /* If one parmlist is shorter than the other,
1536 they fail to match. */
1539 if (!same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1546 /* Process a sizeof or alignof expression where the operand is a
1550 cxx_sizeof_or_alignof_type (tree type
, enum tree_code op
, bool complain
)
1555 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
1556 if (type
== error_mark_node
)
1557 return error_mark_node
;
1559 type
= non_reference (type
);
1560 if (TREE_CODE (type
) == METHOD_TYPE
)
1563 pedwarn (input_location
, OPT_Wpointer_arith
,
1564 "invalid application of %qs to a member function",
1565 OVL_OP_INFO (false, op
)->name
);
1567 return error_mark_node
;
1568 value
= size_one_node
;
1571 dependent_p
= dependent_type_p (type
);
1573 complete_type (type
);
1575 /* VLA types will have a non-constant size. In the body of an
1576 uninstantiated template, we don't need to try to compute the
1577 value, because the sizeof expression is not an integral
1578 constant expression in that case. And, if we do try to
1579 compute the value, we'll likely end up with SAVE_EXPRs, which
1580 the template substitution machinery does not expect to see. */
1581 || (processing_template_decl
1582 && COMPLETE_TYPE_P (type
)
1583 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
))
1585 value
= build_min (op
, size_type_node
, type
);
1586 TREE_READONLY (value
) = 1;
1590 return c_sizeof_or_alignof_type (input_location
, complete_type (type
),
1591 op
== SIZEOF_EXPR
, false,
1595 /* Return the size of the type, without producing any warnings for
1596 types whose size cannot be taken. This routine should be used only
1597 in some other routine that has already produced a diagnostic about
1598 using the size of such a type. */
1600 cxx_sizeof_nowarn (tree type
)
1602 if (TREE_CODE (type
) == FUNCTION_TYPE
1603 || VOID_TYPE_P (type
)
1604 || TREE_CODE (type
) == ERROR_MARK
)
1605 return size_one_node
;
1606 else if (!COMPLETE_TYPE_P (type
))
1607 return size_zero_node
;
1609 return cxx_sizeof_or_alignof_type (type
, SIZEOF_EXPR
, false);
1612 /* Process a sizeof expression where the operand is an expression. */
1615 cxx_sizeof_expr (tree e
, tsubst_flags_t complain
)
1617 if (e
== error_mark_node
)
1618 return error_mark_node
;
1620 if (processing_template_decl
)
1622 e
= build_min (SIZEOF_EXPR
, size_type_node
, e
);
1623 TREE_SIDE_EFFECTS (e
) = 0;
1624 TREE_READONLY (e
) = 1;
1629 /* To get the size of a static data member declared as an array of
1630 unknown bound, we need to instantiate it. */
1632 && VAR_HAD_UNKNOWN_BOUND (e
)
1633 && DECL_TEMPLATE_INSTANTIATION (e
))
1634 instantiate_decl (e
, /*defer_ok*/true, /*expl_inst_mem*/false);
1636 if (TREE_CODE (e
) == PARM_DECL
1637 && DECL_ARRAY_PARAMETER_P (e
)
1638 && (complain
& tf_warning
))
1640 if (warning (OPT_Wsizeof_array_argument
, "%<sizeof%> on array function "
1641 "parameter %qE will return size of %qT", e
, TREE_TYPE (e
)))
1642 inform (DECL_SOURCE_LOCATION (e
), "declared here");
1645 e
= mark_type_use (e
);
1649 if (complain
& tf_error
)
1650 error ("invalid application of %<sizeof%> to a bit-field");
1652 return error_mark_node
;
1655 else if (is_overloaded_fn (e
))
1657 if (complain
& tf_error
)
1658 permerror (input_location
, "ISO C++ forbids applying %<sizeof%> to an expression of "
1661 return error_mark_node
;
1664 else if (type_unknown_p (e
))
1666 if (complain
& tf_error
)
1667 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1669 return error_mark_node
;
1675 return cxx_sizeof_or_alignof_type (e
, SIZEOF_EXPR
, complain
& tf_error
);
1678 /* Implement the __alignof keyword: Return the minimum required
1679 alignment of E, measured in bytes. For VAR_DECL's and
1680 FIELD_DECL's return DECL_ALIGN (which can be set from an
1681 "aligned" __attribute__ specification). */
1684 cxx_alignof_expr (tree e
, tsubst_flags_t complain
)
1688 if (e
== error_mark_node
)
1689 return error_mark_node
;
1691 if (processing_template_decl
)
1693 e
= build_min (ALIGNOF_EXPR
, size_type_node
, e
);
1694 TREE_SIDE_EFFECTS (e
) = 0;
1695 TREE_READONLY (e
) = 1;
1700 e
= mark_type_use (e
);
1703 t
= size_int (DECL_ALIGN_UNIT (e
));
1704 else if (bitfield_p (e
))
1706 if (complain
& tf_error
)
1707 error ("invalid application of %<__alignof%> to a bit-field");
1709 return error_mark_node
;
1712 else if (TREE_CODE (e
) == COMPONENT_REF
1713 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
)
1714 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e
, 1)));
1715 else if (is_overloaded_fn (e
))
1717 if (complain
& tf_error
)
1718 permerror (input_location
, "ISO C++ forbids applying %<__alignof%> to an expression of "
1721 return error_mark_node
;
1722 if (TREE_CODE (e
) == FUNCTION_DECL
)
1723 t
= size_int (DECL_ALIGN_UNIT (e
));
1727 else if (type_unknown_p (e
))
1729 if (complain
& tf_error
)
1730 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1732 return error_mark_node
;
1736 return cxx_sizeof_or_alignof_type (TREE_TYPE (e
), ALIGNOF_EXPR
,
1737 complain
& tf_error
);
1739 return fold_convert (size_type_node
, t
);
1742 /* Process a sizeof or alignof expression E with code OP where the operand
1743 is an expression. */
1746 cxx_sizeof_or_alignof_expr (tree e
, enum tree_code op
, bool complain
)
1748 if (op
== SIZEOF_EXPR
)
1749 return cxx_sizeof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1751 return cxx_alignof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1754 /* Build a representation of an expression 'alignas(E).' Return the
1755 folded integer value of E if it is an integral constant expression
1756 that resolves to a valid alignment. If E depends on a template
1757 parameter, return a syntactic representation tree of kind
1758 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1759 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1762 cxx_alignas_expr (tree e
)
1764 if (e
== NULL_TREE
|| e
== error_mark_node
1765 || (!TYPE_P (e
) && !require_potential_rvalue_constant_expression (e
)))
1771 When the alignment-specifier is of the form
1772 alignas(type-id ), it shall have the same effect as
1773 alignas(alignof(type-id )). */
1775 return cxx_sizeof_or_alignof_type (e
, ALIGNOF_EXPR
, false);
1777 /* If we reach this point, it means the alignas expression if of
1778 the form "alignas(assignment-expression)", so we should follow
1779 what is stated by [dcl.align]/2. */
1781 if (value_dependent_expression_p (e
))
1782 /* Leave value-dependent expression alone for now. */
1785 e
= instantiate_non_dependent_expr (e
);
1786 e
= mark_rvalue_use (e
);
1788 /* [dcl.align]/2 says:
1790 the assignment-expression shall be an integral constant
1793 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e
)))
1795 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e
));
1796 return error_mark_node
;
1799 return cxx_constant_value (e
);
1803 /* EXPR is being used in a context that is not a function call.
1808 The expression can be used only as the left-hand operand of a
1809 member function call.
1811 [expr.mptr.operator]
1813 If the result of .* or ->* is a function, then that result can be
1814 used only as the operand for the function call operator ().
1816 by issuing an error message if appropriate. Returns true iff EXPR
1817 violates these rules. */
1820 invalid_nonstatic_memfn_p (location_t loc
, tree expr
, tsubst_flags_t complain
)
1822 if (expr
== NULL_TREE
)
1824 /* Don't enforce this in MS mode. */
1825 if (flag_ms_extensions
)
1827 if (is_overloaded_fn (expr
) && !really_overloaded_fn (expr
))
1828 expr
= get_first_fn (expr
);
1829 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr
))
1831 if (complain
& tf_error
)
1835 error_at (loc
, "invalid use of non-static member function %qD",
1837 inform (DECL_SOURCE_LOCATION (expr
), "declared here");
1840 error_at (loc
, "invalid use of non-static member function of "
1841 "type %qT", TREE_TYPE (expr
));
1848 /* If EXP is a reference to a bitfield, and the type of EXP does not
1849 match the declared type of the bitfield, return the declared type
1850 of the bitfield. Otherwise, return NULL_TREE. */
1853 is_bitfield_expr_with_lowered_type (const_tree exp
)
1855 switch (TREE_CODE (exp
))
1858 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1)
1859 ? TREE_OPERAND (exp
, 1)
1860 : TREE_OPERAND (exp
, 0)))
1862 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 2));
1865 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1));
1869 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1875 field
= TREE_OPERAND (exp
, 1);
1876 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_BIT_FIELD_TYPE (field
))
1878 if (same_type_ignoring_top_level_qualifiers_p
1879 (TREE_TYPE (exp
), DECL_BIT_FIELD_TYPE (field
)))
1881 return DECL_BIT_FIELD_TYPE (field
);
1885 if (DECL_HAS_VALUE_EXPR_P (exp
))
1886 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
1887 (CONST_CAST_TREE (exp
)));
1891 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp
, 0)))
1892 == TYPE_MAIN_VARIANT (TREE_TYPE (exp
)))
1893 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1901 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1902 bitfield with a lowered type, the type of EXP is returned, rather
1906 unlowered_expr_type (const_tree exp
)
1909 tree etype
= TREE_TYPE (exp
);
1911 type
= is_bitfield_expr_with_lowered_type (exp
);
1913 type
= cp_build_qualified_type (type
, cp_type_quals (etype
));
1920 /* Perform the conversions in [expr] that apply when an lvalue appears
1921 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1922 function-to-pointer conversions. In addition, bitfield references are
1923 converted to their declared types. Note that this function does not perform
1924 the lvalue-to-rvalue conversion for class types. If you need that conversion
1925 for class types, then you probably need to use force_rvalue.
1927 Although the returned value is being used as an rvalue, this
1928 function does not wrap the returned expression in a
1929 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1930 that the return value is no longer an lvalue. */
1933 decay_conversion (tree exp
,
1934 tsubst_flags_t complain
,
1935 bool reject_builtin
/* = true */)
1938 enum tree_code code
;
1939 location_t loc
= EXPR_LOC_OR_LOC (exp
, input_location
);
1941 type
= TREE_TYPE (exp
);
1942 if (type
== error_mark_node
)
1943 return error_mark_node
;
1945 exp
= resolve_nondeduced_context (exp
, complain
);
1946 if (type_unknown_p (exp
))
1948 if (complain
& tf_error
)
1949 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
1950 return error_mark_node
;
1953 code
= TREE_CODE (type
);
1955 if (error_operand_p (exp
))
1956 return error_mark_node
;
1958 if (NULLPTR_TYPE_P (type
) && !TREE_SIDE_EFFECTS (exp
))
1959 return nullptr_node
;
1961 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1962 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1963 if (code
== VOID_TYPE
)
1965 if (complain
& tf_error
)
1966 error_at (loc
, "void value not ignored as it ought to be");
1967 return error_mark_node
;
1969 if (invalid_nonstatic_memfn_p (loc
, exp
, complain
))
1970 return error_mark_node
;
1971 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
1973 exp
= mark_lvalue_use (exp
);
1974 if (reject_builtin
&& reject_gcc_builtin (exp
, loc
))
1975 return error_mark_node
;
1976 return cp_build_addr_expr (exp
, complain
);
1978 if (code
== ARRAY_TYPE
)
1983 exp
= mark_lvalue_use (exp
);
1985 if (INDIRECT_REF_P (exp
))
1986 return build_nop (build_pointer_type (TREE_TYPE (type
)),
1987 TREE_OPERAND (exp
, 0));
1989 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1991 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1), complain
);
1992 if (op1
== error_mark_node
)
1993 return error_mark_node
;
1994 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
1995 TREE_OPERAND (exp
, 0), op1
);
1998 if (!obvalue_p (exp
)
1999 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
2001 if (complain
& tf_error
)
2002 error_at (loc
, "invalid use of non-lvalue array");
2003 return error_mark_node
;
2006 /* Don't let an array compound literal decay to a pointer. It can
2007 still be used to initialize an array or bind to a reference. */
2008 if (TREE_CODE (exp
) == TARGET_EXPR
)
2010 if (complain
& tf_error
)
2011 error_at (loc
, "taking address of temporary array");
2012 return error_mark_node
;
2015 ptrtype
= build_pointer_type (TREE_TYPE (type
));
2019 if (!cxx_mark_addressable (exp
))
2020 return error_mark_node
;
2021 adr
= build_nop (ptrtype
, build_address (exp
));
2024 /* This way is better for a COMPONENT_REF since it can
2025 simplify the offset for a component. */
2026 adr
= cp_build_addr_expr (exp
, complain
);
2027 return cp_convert (ptrtype
, adr
, complain
);
2030 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2031 exp
= mark_rvalue_use (exp
, loc
, reject_builtin
);
2033 /* If a bitfield is used in a context where integral promotion
2034 applies, then the caller is expected to have used
2035 default_conversion. That function promotes bitfields correctly
2036 before calling this function. At this point, if we have a
2037 bitfield referenced, we may assume that is not subject to
2038 promotion, and that, therefore, the type of the resulting rvalue
2039 is the declared type of the bitfield. */
2040 exp
= convert_bitfield_to_declared_type (exp
);
2042 /* We do not call rvalue() here because we do not want to wrap EXP
2043 in a NON_LVALUE_EXPR. */
2047 Non-class rvalues always have cv-unqualified types. */
2048 type
= TREE_TYPE (exp
);
2049 if (!CLASS_TYPE_P (type
) && cv_qualified_p (type
))
2050 exp
= build_nop (cv_unqualified (type
), exp
);
2052 if (!complete_type_or_maybe_complain (type
, exp
, complain
))
2053 return error_mark_node
;
2058 /* Perform preparatory conversions, as part of the "usual arithmetic
2059 conversions". In particular, as per [expr]:
2061 Whenever an lvalue expression appears as an operand of an
2062 operator that expects the rvalue for that operand, the
2063 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2064 standard conversions are applied to convert the expression to an
2067 In addition, we perform integral promotions here, as those are
2068 applied to both operands to a binary operator before determining
2069 what additional conversions should apply. */
2072 cp_default_conversion (tree exp
, tsubst_flags_t complain
)
2074 /* Check for target-specific promotions. */
2075 tree promoted_type
= targetm
.promoted_type (TREE_TYPE (exp
));
2077 exp
= cp_convert (promoted_type
, exp
, complain
);
2078 /* Perform the integral promotions first so that bitfield
2079 expressions (which may promote to "int", even if the bitfield is
2080 declared "unsigned") are promoted correctly. */
2081 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp
)))
2082 exp
= cp_perform_integral_promotions (exp
, complain
);
2083 /* Perform the other conversions. */
2084 exp
= decay_conversion (exp
, complain
);
2092 default_conversion (tree exp
)
2094 return cp_default_conversion (exp
, tf_warning_or_error
);
2097 /* EXPR is an expression with an integral or enumeration type.
2098 Perform the integral promotions in [conv.prom], and return the
2102 cp_perform_integral_promotions (tree expr
, tsubst_flags_t complain
)
2107 expr
= mark_rvalue_use (expr
);
2111 If the bitfield has an enumerated type, it is treated as any
2112 other value of that type for promotion purposes. */
2113 type
= is_bitfield_expr_with_lowered_type (expr
);
2114 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
2115 type
= TREE_TYPE (expr
);
2116 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
2117 /* Scoped enums don't promote. */
2118 if (SCOPED_ENUM_P (type
))
2120 promoted_type
= type_promotes_to (type
);
2121 if (type
!= promoted_type
)
2122 expr
= cp_convert (promoted_type
, expr
, complain
);
2129 perform_integral_promotions (tree expr
)
2131 return cp_perform_integral_promotions (expr
, tf_warning_or_error
);
2134 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2135 decay_conversion to one. */
2138 string_conv_p (const_tree totype
, const_tree exp
, int warn
)
2142 if (!TYPE_PTR_P (totype
))
2145 t
= TREE_TYPE (totype
);
2146 if (!same_type_p (t
, char_type_node
)
2147 && !same_type_p (t
, char16_type_node
)
2148 && !same_type_p (t
, char32_type_node
)
2149 && !same_type_p (t
, wchar_type_node
))
2152 if (TREE_CODE (exp
) == STRING_CST
)
2154 /* Make sure that we don't try to convert between char and wide chars. */
2155 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
2160 /* Is this a string constant which has decayed to 'const char *'? */
2161 t
= build_pointer_type (cp_build_qualified_type (t
, TYPE_QUAL_CONST
));
2162 if (!same_type_p (TREE_TYPE (exp
), t
))
2165 if (TREE_CODE (exp
) != ADDR_EXPR
2166 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
2171 if (cxx_dialect
>= cxx11
)
2172 pedwarn (input_location
, OPT_Wwrite_strings
,
2173 "ISO C++ forbids converting a string constant to %qT",
2176 warning (OPT_Wwrite_strings
,
2177 "deprecated conversion from string constant to %qT",
2184 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2185 can, for example, use as an lvalue. This code used to be in
2186 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2187 expressions, where we're dealing with aggregates. But now it's again only
2188 called from unary_complex_lvalue. The case (in particular) that led to
2189 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2193 rationalize_conditional_expr (enum tree_code code
, tree t
,
2194 tsubst_flags_t complain
)
2196 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
2198 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2199 the first operand is always the one to be used if both operands
2200 are equal, so we know what conditional expression this used to be. */
2201 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
2203 tree op0
= TREE_OPERAND (t
, 0);
2204 tree op1
= TREE_OPERAND (t
, 1);
2206 /* The following code is incorrect if either operand side-effects. */
2207 gcc_assert (!TREE_SIDE_EFFECTS (op0
)
2208 && !TREE_SIDE_EFFECTS (op1
));
2210 build_conditional_expr (loc
,
2211 build_x_binary_op (loc
,
2212 (TREE_CODE (t
) == MIN_EXPR
2213 ? LE_EXPR
: GE_EXPR
),
2214 op0
, TREE_CODE (op0
),
2215 op1
, TREE_CODE (op1
),
2218 cp_build_unary_op (code
, op0
, false, complain
),
2219 cp_build_unary_op (code
, op1
, false, complain
),
2224 build_conditional_expr (loc
, TREE_OPERAND (t
, 0),
2225 cp_build_unary_op (code
, TREE_OPERAND (t
, 1), false,
2227 cp_build_unary_op (code
, TREE_OPERAND (t
, 2), false,
2232 /* Given the TYPE of an anonymous union field inside T, return the
2233 FIELD_DECL for the field. If not found return NULL_TREE. Because
2234 anonymous unions can nest, we must also search all anonymous unions
2235 that are directly reachable. */
2238 lookup_anon_field (tree t
, tree type
)
2242 t
= TYPE_MAIN_VARIANT (t
);
2244 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
2246 if (TREE_STATIC (field
))
2248 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
2251 /* If we find it directly, return the field. */
2252 if (DECL_NAME (field
) == NULL_TREE
2253 && type
== TYPE_MAIN_VARIANT (TREE_TYPE (field
)))
2258 /* Otherwise, it could be nested, search harder. */
2259 if (DECL_NAME (field
) == NULL_TREE
2260 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2262 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
2270 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2271 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2272 non-NULL, it indicates the path to the base used to name MEMBER.
2273 If PRESERVE_REFERENCE is true, the expression returned will have
2274 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2275 returned will have the type referred to by the reference.
2277 This function does not perform access control; that is either done
2278 earlier by the parser when the name of MEMBER is resolved to MEMBER
2279 itself, or later when overload resolution selects one of the
2280 functions indicated by MEMBER. */
2283 build_class_member_access_expr (cp_expr object
, tree member
,
2284 tree access_path
, bool preserve_reference
,
2285 tsubst_flags_t complain
)
2289 tree result
= NULL_TREE
;
2290 tree using_decl
= NULL_TREE
;
2292 if (error_operand_p (object
) || error_operand_p (member
))
2293 return error_mark_node
;
2295 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2299 The type of the first expression shall be "class object" (of a
2301 object_type
= TREE_TYPE (object
);
2302 if (!currently_open_class (object_type
)
2303 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2304 return error_mark_node
;
2305 if (!CLASS_TYPE_P (object_type
))
2307 if (complain
& tf_error
)
2309 if (POINTER_TYPE_P (object_type
)
2310 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2311 error ("request for member %qD in %qE, which is of pointer "
2312 "type %qT (maybe you meant to use %<->%> ?)",
2313 member
, object
.get_value (), object_type
);
2315 error ("request for member %qD in %qE, which is of non-class "
2316 "type %qT", member
, object
.get_value (), object_type
);
2318 return error_mark_node
;
2321 /* The standard does not seem to actually say that MEMBER must be a
2322 member of OBJECT_TYPE. However, that is clearly what is
2324 if (DECL_P (member
))
2326 member_scope
= DECL_CLASS_CONTEXT (member
);
2327 if (!mark_used (member
, complain
) && !(complain
& tf_error
))
2328 return error_mark_node
;
2329 if (TREE_DEPRECATED (member
))
2330 warn_deprecated_use (member
, NULL_TREE
);
2333 member_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (member
));
2334 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2335 presently be the anonymous union. Go outwards until we find a
2336 type related to OBJECT_TYPE. */
2337 while ((ANON_AGGR_TYPE_P (member_scope
) || UNSCOPED_ENUM_P (member_scope
))
2338 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
2340 member_scope
= TYPE_CONTEXT (member_scope
);
2341 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
2343 if (complain
& tf_error
)
2345 if (TREE_CODE (member
) == FIELD_DECL
)
2346 error ("invalid use of nonstatic data member %qE", member
);
2348 error ("%qD is not a member of %qT", member
, object_type
);
2350 return error_mark_node
;
2353 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2354 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2355 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2357 tree temp
= unary_complex_lvalue (ADDR_EXPR
, object
);
2359 object
= cp_build_fold_indirect_ref (temp
);
2362 /* In [expr.ref], there is an explicit list of the valid choices for
2363 MEMBER. We check for each of those cases here. */
2366 /* A static data member. */
2368 mark_exp_read (object
);
2369 /* If OBJECT has side-effects, they are supposed to occur. */
2370 if (TREE_SIDE_EFFECTS (object
))
2371 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
2373 else if (TREE_CODE (member
) == FIELD_DECL
)
2375 /* A non-static data member. */
2380 if (INDIRECT_REF_P (object
))
2382 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object
, 0)));
2384 null_object_p
= false;
2386 /* Convert OBJECT to the type of MEMBER. */
2387 if (!same_type_p (TYPE_MAIN_VARIANT (object_type
),
2388 TYPE_MAIN_VARIANT (member_scope
)))
2393 binfo
= lookup_base (access_path
? access_path
: object_type
,
2394 member_scope
, ba_unique
, &kind
, complain
);
2395 if (binfo
== error_mark_node
)
2396 return error_mark_node
;
2398 /* It is invalid to try to get to a virtual base of a
2399 NULL object. The most common cause is invalid use of
2401 if (null_object_p
&& kind
== bk_via_virtual
)
2403 if (complain
& tf_error
)
2405 error ("invalid access to non-static data member %qD in "
2406 "virtual base of NULL object", member
);
2408 return error_mark_node
;
2411 /* Convert to the base. */
2412 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
2413 /*nonnull=*/1, complain
);
2414 /* If we found the base successfully then we should be able
2415 to convert to it successfully. */
2416 gcc_assert (object
!= error_mark_node
);
2419 /* If MEMBER is from an anonymous aggregate, we have converted
2420 OBJECT so that it refers to the class containing the
2421 anonymous union. Generate a reference to the anonymous union
2422 itself, and recur to find MEMBER. */
2423 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
))
2424 /* When this code is called from build_field_call, the
2425 object already has the type of the anonymous union.
2426 That is because the COMPONENT_REF was already
2427 constructed, and was then disassembled before calling
2428 build_field_call. After the function-call code is
2429 cleaned up, this waste can be eliminated. */
2430 && (!same_type_ignoring_top_level_qualifiers_p
2431 (TREE_TYPE (object
), DECL_CONTEXT (member
))))
2433 tree anonymous_union
;
2435 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
2436 DECL_CONTEXT (member
));
2437 object
= build_class_member_access_expr (object
,
2439 /*access_path=*/NULL_TREE
,
2444 /* Compute the type of the field, as described in [expr.ref]. */
2445 type_quals
= TYPE_UNQUALIFIED
;
2446 member_type
= TREE_TYPE (member
);
2447 if (TREE_CODE (member_type
) != REFERENCE_TYPE
)
2449 type_quals
= (cp_type_quals (member_type
)
2450 | cp_type_quals (object_type
));
2452 /* A field is const (volatile) if the enclosing object, or the
2453 field itself, is const (volatile). But, a mutable field is
2454 not const, even within a const object. */
2455 if (DECL_MUTABLE_P (member
))
2456 type_quals
&= ~TYPE_QUAL_CONST
;
2457 member_type
= cp_build_qualified_type (member_type
, type_quals
);
2460 result
= build3_loc (input_location
, COMPONENT_REF
, member_type
,
2461 object
, member
, NULL_TREE
);
2463 /* Mark the expression const or volatile, as appropriate. Even
2464 though we've dealt with the type above, we still have to mark the
2465 expression itself. */
2466 if (type_quals
& TYPE_QUAL_CONST
)
2467 TREE_READONLY (result
) = 1;
2468 if (type_quals
& TYPE_QUAL_VOLATILE
)
2469 TREE_THIS_VOLATILE (result
) = 1;
2471 else if (BASELINK_P (member
))
2473 /* The member is a (possibly overloaded) member function. */
2477 /* If the MEMBER is exactly one static member function, then we
2478 know the type of the expression. Otherwise, we must wait
2479 until overload resolution has been performed. */
2480 functions
= BASELINK_FUNCTIONS (member
);
2481 if (TREE_CODE (functions
) == FUNCTION_DECL
2482 && DECL_STATIC_FUNCTION_P (functions
))
2483 type
= TREE_TYPE (functions
);
2485 type
= unknown_type_node
;
2486 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2487 base. That will happen when the function is called. */
2488 result
= build3 (COMPONENT_REF
, type
, object
, member
, NULL_TREE
);
2490 else if (TREE_CODE (member
) == CONST_DECL
)
2492 /* The member is an enumerator. */
2494 /* If OBJECT has side-effects, they are supposed to occur. */
2495 if (TREE_SIDE_EFFECTS (object
))
2496 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
2499 else if ((using_decl
= strip_using_decl (member
)) != member
)
2500 result
= build_class_member_access_expr (object
,
2502 access_path
, preserve_reference
,
2506 if (complain
& tf_error
)
2507 error ("invalid use of %qD", member
);
2508 return error_mark_node
;
2511 if (!preserve_reference
)
2514 If E2 is declared to have type "reference to T", then ... the
2515 type of E1.E2 is T. */
2516 result
= convert_from_reference (result
);
2521 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2522 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2525 lookup_destructor (tree object
, tree scope
, tree dtor_name
,
2526 tsubst_flags_t complain
)
2528 tree object_type
= TREE_TYPE (object
);
2529 tree dtor_type
= TREE_OPERAND (dtor_name
, 0);
2532 /* We've already complained about this destructor. */
2533 if (dtor_type
== error_mark_node
)
2534 return error_mark_node
;
2536 if (scope
&& !check_dtor_name (scope
, dtor_type
))
2538 if (complain
& tf_error
)
2539 error ("qualified type %qT does not match destructor name ~%qT",
2541 return error_mark_node
;
2543 if (is_auto (dtor_type
))
2544 dtor_type
= object_type
;
2545 else if (identifier_p (dtor_type
))
2547 /* In a template, names we can't find a match for are still accepted
2548 destructor names, and we check them here. */
2549 if (check_dtor_name (object_type
, dtor_type
))
2550 dtor_type
= object_type
;
2553 if (complain
& tf_error
)
2554 error ("object type %qT does not match destructor name ~%qT",
2555 object_type
, dtor_type
);
2556 return error_mark_node
;
2560 else if (!DERIVED_FROM_P (dtor_type
, TYPE_MAIN_VARIANT (object_type
)))
2562 if (complain
& tf_error
)
2563 error ("the type being destroyed is %qT, but the destructor "
2564 "refers to %qT", TYPE_MAIN_VARIANT (object_type
), dtor_type
);
2565 return error_mark_node
;
2567 expr
= lookup_member (dtor_type
, complete_dtor_identifier
,
2568 /*protect=*/1, /*want_type=*/false,
2569 tf_warning_or_error
);
2572 if (complain
& tf_error
)
2573 cxx_incomplete_type_error (dtor_name
, dtor_type
);
2574 return error_mark_node
;
2576 expr
= (adjust_result_of_qualified_name_lookup
2577 (expr
, dtor_type
, object_type
));
2578 if (scope
== NULL_TREE
)
2579 /* We need to call adjust_result_of_qualified_name_lookup in case the
2580 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2581 that we still get virtual function binding. */
2582 BASELINK_QUALIFIED_P (expr
) = false;
2586 /* An expression of the form "A::template B" has been resolved to
2587 DECL. Issue a diagnostic if B is not a template or template
2591 check_template_keyword (tree decl
)
2593 /* The standard says:
2597 If a name prefixed by the keyword template is not a member
2598 template, the program is ill-formed.
2600 DR 228 removed the restriction that the template be a member
2603 DR 96, if accepted would add the further restriction that explicit
2604 template arguments must be provided if the template keyword is
2605 used, but, as of 2005-10-16, that DR is still in "drafting". If
2606 this DR is accepted, then the semantic checks here can be
2607 simplified, as the entity named must in fact be a template
2608 specialization, rather than, as at present, a set of overloaded
2609 functions containing at least one template function. */
2610 if (TREE_CODE (decl
) != TEMPLATE_DECL
2611 && TREE_CODE (decl
) != TEMPLATE_ID_EXPR
)
2615 if (DECL_USE_TEMPLATE (decl
)
2616 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
2619 permerror (input_location
, "%qD is not a template", decl
);
2621 else if (!is_overloaded_fn (decl
))
2622 permerror (input_location
, "%qD is not a template", decl
);
2627 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
2628 !found
&& iter
; ++iter
)
2631 if (TREE_CODE (fn
) == TEMPLATE_DECL
2632 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
2633 || (TREE_CODE (fn
) == FUNCTION_DECL
2634 && DECL_USE_TEMPLATE (fn
)
2635 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn
))))
2639 permerror (input_location
, "%qD is not a template", decl
);
2644 /* Record that an access failure occurred on BASETYPE_PATH attempting
2645 to access FIELD_DECL. */
2648 access_failure_info::record_access_failure (tree basetype_path
,
2651 m_was_inaccessible
= true;
2652 m_basetype_path
= basetype_path
;
2653 m_field_decl
= field_decl
;
2656 /* If an access failure was recorded, then attempt to locate an
2657 accessor function for the pertinent field, and if one is
2658 available, add a note and fix-it hint suggesting using it. */
2661 access_failure_info::maybe_suggest_accessor (bool const_p
) const
2663 if (!m_was_inaccessible
)
2667 = locate_field_accessor (m_basetype_path
, m_field_decl
, const_p
);
2671 /* The accessor must itself be accessible for it to be a reasonable
2673 if (!accessible_p (m_basetype_path
, accessor
, true))
2676 rich_location
richloc (line_table
, input_location
);
2678 pp_printf (&pp
, "%s()", IDENTIFIER_POINTER (DECL_NAME (accessor
)));
2679 richloc
.add_fixit_replace (pp_formatted_text (&pp
));
2680 inform (&richloc
, "field %q#D can be accessed via %q#D",
2681 m_field_decl
, accessor
);
2684 /* This function is called by the parser to process a class member
2685 access expression of the form OBJECT.NAME. NAME is a node used by
2686 the parser to represent a name; it is not yet a DECL. It may,
2687 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2688 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2689 there is no reason to do the lookup twice, so the parser keeps the
2690 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2691 be a template via the use of the "A::template B" syntax. */
2694 finish_class_member_access_expr (cp_expr object
, tree name
, bool template_p
,
2695 tsubst_flags_t complain
)
2700 tree access_path
= NULL_TREE
;
2701 tree orig_object
= object
;
2702 tree orig_name
= name
;
2704 if (object
== error_mark_node
|| name
== error_mark_node
)
2705 return error_mark_node
;
2707 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2708 if (!objc_is_public (object
, name
))
2709 return error_mark_node
;
2711 object_type
= TREE_TYPE (object
);
2713 if (processing_template_decl
)
2715 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2716 type_dependent_object_expression_p (object
)
2717 /* If NAME is "f<args>", where either 'f' or 'args' is
2718 dependent, then the expression is dependent. */
2719 || (TREE_CODE (name
) == TEMPLATE_ID_EXPR
2720 && dependent_template_id_p (TREE_OPERAND (name
, 0),
2721 TREE_OPERAND (name
, 1)))
2722 /* If NAME is "T::X" where "T" is dependent, then the
2723 expression is dependent. */
2724 || (TREE_CODE (name
) == SCOPE_REF
2725 && TYPE_P (TREE_OPERAND (name
, 0))
2726 && dependent_scope_p (TREE_OPERAND (name
, 0))))
2729 return build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
,
2730 orig_object
, orig_name
, NULL_TREE
);
2732 object
= build_non_dependent_expr (object
);
2734 else if (c_dialect_objc ()
2735 && identifier_p (name
)
2736 && (expr
= objc_maybe_build_component_ref (object
, name
)))
2741 The type of the first expression shall be "class object" (of a
2743 if (!currently_open_class (object_type
)
2744 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2745 return error_mark_node
;
2746 if (!CLASS_TYPE_P (object_type
))
2748 if (complain
& tf_error
)
2750 if (POINTER_TYPE_P (object_type
)
2751 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2752 error ("request for member %qD in %qE, which is of pointer "
2753 "type %qT (maybe you meant to use %<->%> ?)",
2754 name
, object
.get_value (), object_type
);
2756 error ("request for member %qD in %qE, which is of non-class "
2757 "type %qT", name
, object
.get_value (), object_type
);
2759 return error_mark_node
;
2762 if (BASELINK_P (name
))
2763 /* A member function that has already been looked up. */
2767 bool is_template_id
= false;
2768 tree template_args
= NULL_TREE
;
2769 tree scope
= NULL_TREE
;
2771 access_path
= object_type
;
2773 if (TREE_CODE (name
) == SCOPE_REF
)
2775 /* A qualified name. The qualifying class or namespace `S'
2776 has already been looked up; it is either a TYPE or a
2778 scope
= TREE_OPERAND (name
, 0);
2779 name
= TREE_OPERAND (name
, 1);
2781 /* If SCOPE is a namespace, then the qualified name does not
2782 name a member of OBJECT_TYPE. */
2783 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
2785 if (complain
& tf_error
)
2786 error ("%<%D::%D%> is not a member of %qT",
2787 scope
, name
, object_type
);
2788 return error_mark_node
;
2792 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
2794 is_template_id
= true;
2795 template_args
= TREE_OPERAND (name
, 1);
2796 name
= TREE_OPERAND (name
, 0);
2798 if (!identifier_p (name
))
2799 name
= OVL_NAME (name
);
2804 if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
2806 gcc_assert (!is_template_id
);
2807 /* Looking up a member enumerator (c++/56793). */
2808 if (!TYPE_CLASS_SCOPE_P (scope
)
2809 || !DERIVED_FROM_P (TYPE_CONTEXT (scope
), object_type
))
2811 if (complain
& tf_error
)
2812 error ("%<%D::%D%> is not a member of %qT",
2813 scope
, name
, object_type
);
2814 return error_mark_node
;
2816 tree val
= lookup_enumerator (scope
, name
);
2819 if (complain
& tf_error
)
2820 error ("%qD is not a member of %qD",
2822 return error_mark_node
;
2825 if (TREE_SIDE_EFFECTS (object
))
2826 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
), object
, val
);
2830 gcc_assert (CLASS_TYPE_P (scope
));
2831 gcc_assert (identifier_p (name
) || TREE_CODE (name
) == BIT_NOT_EXPR
);
2833 if (constructor_name_p (name
, scope
))
2835 if (complain
& tf_error
)
2836 error ("cannot call constructor %<%T::%D%> directly",
2838 return error_mark_node
;
2841 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2842 access_path
= lookup_base (object_type
, scope
, ba_check
,
2844 if (access_path
== error_mark_node
)
2845 return error_mark_node
;
2848 if (any_dependent_bases_p (object_type
))
2850 if (complain
& tf_error
)
2851 error ("%qT is not a base of %qT", scope
, object_type
);
2852 return error_mark_node
;
2856 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2858 if (dependent_type_p (object_type
))
2859 /* The destructor isn't declared yet. */
2861 member
= lookup_destructor (object
, scope
, name
, complain
);
2865 /* Look up the member. */
2866 access_failure_info afi
;
2867 member
= lookup_member (access_path
, name
, /*protect=*/1,
2868 /*want_type=*/false, complain
,
2870 afi
.maybe_suggest_accessor (TYPE_READONLY (object_type
));
2871 if (member
== NULL_TREE
)
2873 if (dependent_type_p (object_type
))
2874 /* Try again at instantiation time. */
2876 if (complain
& tf_error
)
2878 tree guessed_id
= lookup_member_fuzzy (access_path
, name
,
2879 /*want_type=*/false);
2882 location_t bogus_component_loc
= input_location
;
2883 gcc_rich_location
rich_loc (bogus_component_loc
);
2884 rich_loc
.add_fixit_misspelled_id (bogus_component_loc
,
2886 error_at (&rich_loc
,
2887 "%q#T has no member named %qE;"
2888 " did you mean %qE?",
2889 TREE_CODE (access_path
) == TREE_BINFO
2890 ? TREE_TYPE (access_path
) : object_type
,
2894 error ("%q#T has no member named %qE",
2895 TREE_CODE (access_path
) == TREE_BINFO
2896 ? TREE_TYPE (access_path
) : object_type
, name
);
2898 return error_mark_node
;
2900 if (member
== error_mark_node
)
2901 return error_mark_node
;
2903 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member
)))
2904 /* Dependent type attributes on the decl mean that the TREE_TYPE is
2905 wrong, so don't use it. */
2907 if (TREE_CODE (member
) == USING_DECL
&& DECL_DEPENDENT_P (member
))
2913 tree templ
= member
;
2915 if (BASELINK_P (templ
))
2916 member
= lookup_template_function (templ
, template_args
);
2917 else if (variable_template_p (templ
))
2918 member
= (lookup_and_finish_template_variable
2919 (templ
, template_args
, complain
));
2922 if (complain
& tf_error
)
2923 error ("%qD is not a member template function", name
);
2924 return error_mark_node
;
2929 if (TREE_DEPRECATED (member
))
2930 warn_deprecated_use (member
, NULL_TREE
);
2933 check_template_keyword (member
);
2935 expr
= build_class_member_access_expr (object
, member
, access_path
,
2936 /*preserve_reference=*/false,
2938 if (processing_template_decl
&& expr
!= error_mark_node
)
2940 if (BASELINK_P (member
))
2942 if (TREE_CODE (orig_name
) == SCOPE_REF
)
2943 BASELINK_QUALIFIED_P (member
) = 1;
2946 return build_min_non_dep (COMPONENT_REF
, expr
,
2947 orig_object
, orig_name
,
2954 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2958 build_simple_component_ref (tree object
, tree member
)
2960 tree type
= cp_build_qualified_type (TREE_TYPE (member
),
2961 cp_type_quals (TREE_TYPE (object
)));
2962 return build3_loc (input_location
,
2963 COMPONENT_REF
, type
,
2964 object
, member
, NULL_TREE
);
2967 /* Return an expression for the MEMBER_NAME field in the internal
2968 representation of PTRMEM, a pointer-to-member function. (Each
2969 pointer-to-member function type gets its own RECORD_TYPE so it is
2970 more convenient to access the fields by name than by FIELD_DECL.)
2971 This routine converts the NAME to a FIELD_DECL and then creates the
2972 node for the complete expression. */
2975 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
2980 /* This code is a stripped down version of
2981 build_class_member_access_expr. It does not work to use that
2982 routine directly because it expects the object to be of class
2984 ptrmem_type
= TREE_TYPE (ptrmem
);
2985 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
));
2986 for (member
= TYPE_FIELDS (ptrmem_type
); member
;
2987 member
= DECL_CHAIN (member
))
2988 if (DECL_NAME (member
) == member_name
)
2990 tree res
= build_simple_component_ref (ptrmem
, member
);
2992 TREE_NO_WARNING (res
) = 1;
2996 /* Given an expression PTR for a pointer, return an expression
2997 for the value pointed to.
2998 ERRORSTRING is the name of the operator to appear in error messages.
3000 This function may need to overload OPERATOR_FNNAME.
3001 Must also handle REFERENCE_TYPEs for C++. */
3004 build_x_indirect_ref (location_t loc
, tree expr
, ref_operator errorstring
,
3005 tsubst_flags_t complain
)
3007 tree orig_expr
= expr
;
3009 tree overload
= NULL_TREE
;
3011 if (processing_template_decl
)
3013 /* Retain the type if we know the operand is a pointer. */
3014 if (TREE_TYPE (expr
) && POINTER_TYPE_P (TREE_TYPE (expr
)))
3015 return build_min (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
3016 if (type_dependent_expression_p (expr
))
3017 return build_min_nt_loc (loc
, INDIRECT_REF
, expr
);
3018 expr
= build_non_dependent_expr (expr
);
3021 rval
= build_new_op (loc
, INDIRECT_REF
, LOOKUP_NORMAL
, expr
,
3022 NULL_TREE
, NULL_TREE
, &overload
, complain
);
3024 rval
= cp_build_indirect_ref (expr
, errorstring
, complain
);
3026 if (processing_template_decl
&& rval
!= error_mark_node
)
3028 if (overload
!= NULL_TREE
)
3029 return (build_min_non_dep_op_overload
3030 (INDIRECT_REF
, rval
, overload
, orig_expr
));
3032 return build_min_non_dep (INDIRECT_REF
, rval
, orig_expr
);
3038 /* The implementation of the above, and of indirection implied by other
3039 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3042 cp_build_indirect_ref_1 (tree ptr
, ref_operator errorstring
,
3043 tsubst_flags_t complain
, bool do_fold
)
3047 /* RO_NULL should only be used with the folding entry points below, not
3048 cp_build_indirect_ref. */
3049 gcc_checking_assert (errorstring
!= RO_NULL
|| do_fold
);
3051 if (ptr
== current_class_ptr
3052 || (TREE_CODE (ptr
) == NOP_EXPR
3053 && TREE_OPERAND (ptr
, 0) == current_class_ptr
3054 && (same_type_ignoring_top_level_qualifiers_p
3055 (TREE_TYPE (ptr
), TREE_TYPE (current_class_ptr
)))))
3056 return current_class_ref
;
3058 pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
3059 ? ptr
: decay_conversion (ptr
, complain
));
3060 if (pointer
== error_mark_node
)
3061 return error_mark_node
;
3063 type
= TREE_TYPE (pointer
);
3065 if (POINTER_TYPE_P (type
))
3069 If the type of the expression is "pointer to T," the type
3070 of the result is "T." */
3071 tree t
= TREE_TYPE (type
);
3073 if ((CONVERT_EXPR_P (ptr
)
3074 || TREE_CODE (ptr
) == VIEW_CONVERT_EXPR
)
3075 && (!CLASS_TYPE_P (t
) || !CLASSTYPE_EMPTY_P (t
)))
3077 /* If a warning is issued, mark it to avoid duplicates from
3078 the backend. This only needs to be done at
3079 warn_strict_aliasing > 2. */
3080 if (warn_strict_aliasing
> 2)
3081 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr
, 0)),
3082 type
, TREE_OPERAND (ptr
, 0)))
3083 TREE_NO_WARNING (ptr
) = 1;
3086 if (VOID_TYPE_P (t
))
3088 /* A pointer to incomplete type (other than cv void) can be
3089 dereferenced [expr.unary.op]/1 */
3090 if (complain
& tf_error
)
3091 error ("%qT is not a pointer-to-object type", type
);
3092 return error_mark_node
;
3094 else if (do_fold
&& TREE_CODE (pointer
) == ADDR_EXPR
3095 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
3096 /* The POINTER was something like `&x'. We simplify `*&x' to
3098 return TREE_OPERAND (pointer
, 0);
3101 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
3103 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3104 so that we get the proper error message if the result is used
3105 to assign to. Also, &* is supposed to be a no-op. */
3106 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
3107 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
3108 TREE_SIDE_EFFECTS (ref
)
3109 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
));
3113 else if (!(complain
& tf_error
))
3114 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3116 /* `pointer' won't be an error_mark_node if we were given a
3117 pointer to member, so it's cool to check for this here. */
3118 else if (TYPE_PTRMEM_P (type
))
3119 switch (errorstring
)
3121 case RO_ARRAY_INDEXING
:
3122 error ("invalid use of array indexing on pointer to member");
3125 error ("invalid use of unary %<*%> on pointer to member");
3127 case RO_IMPLICIT_CONVERSION
:
3128 error ("invalid use of implicit conversion on pointer to member");
3131 error ("left hand operand of %<->*%> must be a pointer to class, "
3132 "but is a pointer to member of type %qT", type
);
3137 else if (pointer
!= error_mark_node
)
3138 invalid_indirection_error (input_location
, type
, errorstring
);
3140 return error_mark_node
;
3143 /* Entry point used by c-common, which expects folding. */
3146 build_indirect_ref (location_t
/*loc*/,
3147 tree ptr
, ref_operator errorstring
)
3149 return cp_build_indirect_ref_1 (ptr
, errorstring
, tf_warning_or_error
, true);
3152 /* Entry point used by internal indirection needs that don't correspond to any
3153 syntactic construct. */
3156 cp_build_fold_indirect_ref (tree pointer
)
3158 return cp_build_indirect_ref_1 (pointer
, RO_NULL
, tf_warning_or_error
, true);
3161 /* Entry point used by indirection needs that correspond to some syntactic
3165 cp_build_indirect_ref (tree ptr
, ref_operator errorstring
,
3166 tsubst_flags_t complain
)
3168 return cp_build_indirect_ref_1 (ptr
, errorstring
, complain
, false);
3171 /* This handles expressions of the form "a[i]", which denotes
3174 This is logically equivalent in C to *(a+i), but we may do it differently.
3175 If A is a variable or a member, we generate a primitive ARRAY_REF.
3176 This avoids forcing the array out of registers, and can work on
3177 arrays that are not lvalues (for example, members of structures returned
3180 If INDEX is of some user-defined type, it must be converted to
3181 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3182 will inherit the type of the array, which will be some pointer type.
3184 LOC is the location to use in building the array reference. */
3187 cp_build_array_ref (location_t loc
, tree array
, tree idx
,
3188 tsubst_flags_t complain
)
3194 if (complain
& tf_error
)
3195 error_at (loc
, "subscript missing in array reference");
3196 return error_mark_node
;
3199 /* If an array's index is an array notation, then its rank cannot be
3200 greater than one. */
3201 if (flag_cilkplus
&& contains_array_notation_expr (idx
))
3205 /* If find_rank returns false, then it should have reported an error,
3206 thus it is unnecessary for repetition. */
3207 if (!find_rank (loc
, idx
, idx
, true, &rank
))
3208 return error_mark_node
;
3211 error_at (loc
, "rank of the array%'s index is greater than 1");
3212 return error_mark_node
;
3215 if (TREE_TYPE (array
) == error_mark_node
3216 || TREE_TYPE (idx
) == error_mark_node
)
3217 return error_mark_node
;
3219 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3221 switch (TREE_CODE (array
))
3225 tree value
= cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3227 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3228 TREE_OPERAND (array
, 0), value
);
3229 SET_EXPR_LOCATION (ret
, loc
);
3234 ret
= build_conditional_expr
3235 (loc
, TREE_OPERAND (array
, 0),
3236 cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3238 cp_build_array_ref (loc
, TREE_OPERAND (array
, 2), idx
,
3241 protected_set_expr_location (ret
, loc
);
3248 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, idx
);
3250 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
3254 warn_array_subscript_with_type_char (loc
, idx
);
3256 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx
)))
3258 if (complain
& tf_error
)
3259 error_at (loc
, "array subscript is not an integer");
3260 return error_mark_node
;
3263 /* Apply integral promotions *after* noticing character types.
3264 (It is unclear why we do these promotions -- the standard
3265 does not say that we should. In fact, the natural thing would
3266 seem to be to convert IDX to ptrdiff_t; we're performing
3267 pointer arithmetic.) */
3268 idx
= cp_perform_integral_promotions (idx
, complain
);
3270 /* An array that is indexed by a non-constant
3271 cannot be stored in a register; we must be able to do
3272 address arithmetic on its address.
3273 Likewise an array of elements of variable size. */
3274 if (TREE_CODE (idx
) != INTEGER_CST
3275 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
3276 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
3279 if (!cxx_mark_addressable (array
, true))
3280 return error_mark_node
;
3283 /* An array that is indexed by a constant value which is not within
3284 the array bounds cannot be stored in a register either; because we
3285 would get a crash in store_bit_field/extract_bit_field when trying
3286 to access a non-existent part of the register. */
3287 if (TREE_CODE (idx
) == INTEGER_CST
3288 && TYPE_DOMAIN (TREE_TYPE (array
))
3289 && ! int_fits_type_p (idx
, TYPE_DOMAIN (TREE_TYPE (array
))))
3291 if (!cxx_mark_addressable (array
))
3292 return error_mark_node
;
3295 /* Note in C++ it is valid to subscript a `register' array, since
3296 it is valid to take the address of something with that
3297 storage specification. */
3301 while (TREE_CODE (foo
) == COMPONENT_REF
)
3302 foo
= TREE_OPERAND (foo
, 0);
3303 if (VAR_P (foo
) && DECL_REGISTER (foo
)
3304 && (complain
& tf_warning
))
3305 warning_at (loc
, OPT_Wextra
,
3306 "subscripting array declared %<register%>");
3309 type
= TREE_TYPE (TREE_TYPE (array
));
3310 rval
= build4 (ARRAY_REF
, type
, array
, idx
, NULL_TREE
, NULL_TREE
);
3311 /* Array ref is const/volatile if the array elements are
3312 or if the array is.. */
3313 TREE_READONLY (rval
)
3314 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
3315 TREE_SIDE_EFFECTS (rval
)
3316 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
3317 TREE_THIS_VOLATILE (rval
)
3318 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
3319 ret
= require_complete_type_sfinae (rval
, complain
);
3320 protected_set_expr_location (ret
, loc
);
3322 ret
= non_lvalue_loc (loc
, ret
);
3327 tree ar
= cp_default_conversion (array
, complain
);
3328 tree ind
= cp_default_conversion (idx
, complain
);
3330 /* Put the integer in IND to simplify error checking. */
3331 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
3332 std::swap (ar
, ind
);
3334 if (ar
== error_mark_node
|| ind
== error_mark_node
)
3335 return error_mark_node
;
3337 if (!TYPE_PTR_P (TREE_TYPE (ar
)))
3339 if (complain
& tf_error
)
3340 error_at (loc
, "subscripted value is neither array nor pointer");
3341 return error_mark_node
;
3343 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
3345 if (complain
& tf_error
)
3346 error_at (loc
, "array subscript is not an integer");
3347 return error_mark_node
;
3350 warn_array_subscript_with_type_char (loc
, idx
);
3352 ret
= cp_build_indirect_ref (cp_build_binary_op (input_location
,
3357 protected_set_expr_location (ret
, loc
);
3359 ret
= non_lvalue_loc (loc
, ret
);
3364 /* Entry point for Obj-C++. */
3367 build_array_ref (location_t loc
, tree array
, tree idx
)
3369 return cp_build_array_ref (loc
, array
, idx
, tf_warning_or_error
);
3372 /* Resolve a pointer to member function. INSTANCE is the object
3373 instance to use, if the member points to a virtual member.
3375 This used to avoid checking for virtual functions if basetype
3376 has no virtual functions, according to an earlier ANSI draft.
3377 With the final ISO C++ rules, such an optimization is
3378 incorrect: A pointer to a derived member can be static_cast
3379 to pointer-to-base-member, as long as the dynamic object
3380 later has the right member. So now we only do this optimization
3381 when we know the dynamic type of the object. */
3384 get_member_function_from_ptrfunc (tree
*instance_ptrptr
, tree function
,
3385 tsubst_flags_t complain
)
3387 if (TREE_CODE (function
) == OFFSET_REF
)
3388 function
= TREE_OPERAND (function
, 1);
3390 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
3392 tree idx
, delta
, e1
, e2
, e3
, vtbl
;
3394 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
3395 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
3397 tree instance_ptr
= *instance_ptrptr
;
3398 tree instance_save_expr
= 0;
3399 if (instance_ptr
== error_mark_node
)
3401 if (TREE_CODE (function
) == PTRMEM_CST
)
3403 /* Extracting the function address from a pmf is only
3404 allowed with -Wno-pmf-conversions. It only works for
3406 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
), complain
);
3407 e1
= convert (fntype
, e1
);
3412 if (complain
& tf_error
)
3413 error ("object missing in use of %qE", function
);
3414 return error_mark_node
;
3418 /* True if we know that the dynamic type of the object doesn't have
3419 virtual functions, so we can assume the PFN field is a pointer. */
3420 nonvirtual
= (COMPLETE_TYPE_P (basetype
)
3421 && !TYPE_POLYMORPHIC_P (basetype
)
3422 && resolves_to_fixed_type_p (instance_ptr
, 0));
3424 /* If we don't really have an object (i.e. in an ill-formed
3425 conversion from PMF to pointer), we can't resolve virtual
3426 functions anyway. */
3427 if (!nonvirtual
&& is_dummy_object (instance_ptr
))
3430 if (TREE_SIDE_EFFECTS (instance_ptr
))
3431 instance_ptr
= instance_save_expr
= save_expr (instance_ptr
);
3433 if (TREE_SIDE_EFFECTS (function
))
3434 function
= save_expr (function
);
3436 /* Start by extracting all the information from the PMF itself. */
3437 e3
= pfn_from_ptrmemfunc (function
);
3438 delta
= delta_from_ptrmemfunc (function
);
3439 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
3440 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
3442 int flag_sanitize_save
;
3443 case ptrmemfunc_vbit_in_pfn
:
3444 e1
= cp_build_binary_op (input_location
,
3445 BIT_AND_EXPR
, idx
, integer_one_node
,
3447 idx
= cp_build_binary_op (input_location
,
3448 MINUS_EXPR
, idx
, integer_one_node
,
3450 if (idx
== error_mark_node
)
3451 return error_mark_node
;
3454 case ptrmemfunc_vbit_in_delta
:
3455 e1
= cp_build_binary_op (input_location
,
3456 BIT_AND_EXPR
, delta
, integer_one_node
,
3458 /* Don't instrument the RSHIFT_EXPR we're about to create because
3459 we're going to use DELTA number of times, and that wouldn't play
3460 well with SAVE_EXPRs therein. */
3461 flag_sanitize_save
= flag_sanitize
;
3463 delta
= cp_build_binary_op (input_location
,
3464 RSHIFT_EXPR
, delta
, integer_one_node
,
3466 flag_sanitize
= flag_sanitize_save
;
3467 if (delta
== error_mark_node
)
3468 return error_mark_node
;
3475 if (e1
== error_mark_node
)
3476 return error_mark_node
;
3478 /* Convert down to the right base before using the instance. A
3479 special case is that in a pointer to member of class C, C may
3480 be incomplete. In that case, the function will of course be
3481 a member of C, and no conversion is required. In fact,
3482 lookup_base will fail in that case, because incomplete
3483 classes do not have BINFOs. */
3484 if (!same_type_ignoring_top_level_qualifiers_p
3485 (basetype
, TREE_TYPE (TREE_TYPE (instance_ptr
))))
3487 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
3488 basetype
, ba_check
, NULL
, complain
);
3489 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
,
3491 if (instance_ptr
== error_mark_node
)
3492 return error_mark_node
;
3494 /* ...and then the delta in the PMF. */
3495 instance_ptr
= fold_build_pointer_plus (instance_ptr
, delta
);
3497 /* Hand back the adjusted 'this' argument to our caller. */
3498 *instance_ptrptr
= instance_ptr
;
3501 /* Now just return the pointer. */
3504 /* Next extract the vtable pointer from the object. */
3505 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
3507 vtbl
= cp_build_fold_indirect_ref (vtbl
);
3508 if (vtbl
== error_mark_node
)
3509 return error_mark_node
;
3511 /* Finally, extract the function pointer from the vtable. */
3512 e2
= fold_build_pointer_plus_loc (input_location
, vtbl
, idx
);
3513 e2
= cp_build_fold_indirect_ref (e2
);
3514 if (e2
== error_mark_node
)
3515 return error_mark_node
;
3516 TREE_CONSTANT (e2
) = 1;
3518 /* When using function descriptors, the address of the
3519 vtable entry is treated as a function pointer. */
3520 if (TARGET_VTABLE_USES_DESCRIPTORS
)
3521 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
3522 cp_build_addr_expr (e2
, complain
));
3524 e2
= fold_convert (TREE_TYPE (e3
), e2
);
3525 e1
= build_conditional_expr (input_location
, e1
, e2
, e3
, complain
);
3526 if (e1
== error_mark_node
)
3527 return error_mark_node
;
3529 /* Make sure this doesn't get evaluated first inside one of the
3530 branches of the COND_EXPR. */
3531 if (instance_save_expr
)
3532 e1
= build2 (COMPOUND_EXPR
, TREE_TYPE (e1
),
3533 instance_save_expr
, e1
);
3540 /* Used by the C-common bits. */
3542 build_function_call (location_t
/*loc*/,
3543 tree function
, tree params
)
3545 return cp_build_function_call (function
, params
, tf_warning_or_error
);
3548 /* Used by the C-common bits. */
3550 build_function_call_vec (location_t
/*loc*/, vec
<location_t
> /*arg_loc*/,
3551 tree function
, vec
<tree
, va_gc
> *params
,
3552 vec
<tree
, va_gc
> * /*origtypes*/)
3554 vec
<tree
, va_gc
> *orig_params
= params
;
3555 tree ret
= cp_build_function_call_vec (function
, ¶ms
,
3556 tf_warning_or_error
);
3558 /* cp_build_function_call_vec can reallocate PARAMS by adding
3559 default arguments. That should never happen here. Verify
3561 gcc_assert (params
== orig_params
);
3566 /* Build a function call using a tree list of arguments. */
3569 cp_build_function_call (tree function
, tree params
, tsubst_flags_t complain
)
3571 vec
<tree
, va_gc
> *vec
;
3574 vec
= make_tree_vector ();
3575 for (; params
!= NULL_TREE
; params
= TREE_CHAIN (params
))
3576 vec_safe_push (vec
, TREE_VALUE (params
));
3577 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3578 release_tree_vector (vec
);
3582 /* Build a function call using varargs. */
3585 cp_build_function_call_nary (tree function
, tsubst_flags_t complain
, ...)
3587 vec
<tree
, va_gc
> *vec
;
3591 vec
= make_tree_vector ();
3592 va_start (args
, complain
);
3593 for (t
= va_arg (args
, tree
); t
!= NULL_TREE
; t
= va_arg (args
, tree
))
3594 vec_safe_push (vec
, t
);
3596 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3597 release_tree_vector (vec
);
3601 /* Build a function call using a vector of arguments. PARAMS may be
3602 NULL if there are no parameters. This changes the contents of
3606 cp_build_function_call_vec (tree function
, vec
<tree
, va_gc
> **params
,
3607 tsubst_flags_t complain
)
3609 tree fntype
, fndecl
;
3611 tree original
= function
;
3615 vec
<tree
, va_gc
> *allocated
= NULL
;
3618 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3619 expressions, like those used for ObjC messenger dispatches. */
3620 if (params
!= NULL
&& !vec_safe_is_empty (*params
))
3621 function
= objc_rewrite_function_call (function
, (**params
)[0]);
3623 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3624 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3625 if (TREE_CODE (function
) == NOP_EXPR
3626 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
3627 function
= TREE_OPERAND (function
, 0);
3629 if (TREE_CODE (function
) == FUNCTION_DECL
)
3631 /* If the function is a non-template member function
3632 or a non-template friend, then we need to check the
3635 Note that if overload resolution failed with a single
3636 candidate this function will be used to explicitly diagnose
3637 the failure for the single call expression. The check is
3638 technically redundant since we also would have failed in
3639 add_function_candidate. */
3641 && (complain
& tf_error
)
3642 && !constraints_satisfied_p (function
))
3644 error ("cannot call function %qD", function
);
3645 location_t loc
= DECL_SOURCE_LOCATION (function
);
3646 diagnose_constraints (loc
, function
, NULL_TREE
);
3647 return error_mark_node
;
3650 if (!mark_used (function
, complain
) && !(complain
& tf_error
))
3651 return error_mark_node
;
3654 /* Convert anything with function type to a pointer-to-function. */
3655 if (DECL_MAIN_P (function
))
3657 if (complain
& tf_error
)
3658 pedwarn (input_location
, OPT_Wpedantic
,
3659 "ISO C++ forbids calling %<::main%> from within program");
3661 return error_mark_node
;
3663 function
= build_addr_func (function
, complain
);
3669 function
= build_addr_func (function
, complain
);
3672 if (function
== error_mark_node
)
3673 return error_mark_node
;
3675 fntype
= TREE_TYPE (function
);
3677 if (TYPE_PTRMEMFUNC_P (fntype
))
3679 if (complain
& tf_error
)
3680 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3681 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3682 original
, original
);
3683 return error_mark_node
;
3686 is_method
= (TYPE_PTR_P (fntype
)
3687 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
3689 if (!(TYPE_PTRFN_P (fntype
)
3691 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
3693 if (complain
& tf_error
)
3695 if (!flag_diagnostics_show_caret
)
3696 error_at (input_location
,
3697 "%qE cannot be used as a function", original
);
3698 else if (DECL_P (original
))
3699 error_at (input_location
,
3700 "%qD cannot be used as a function", original
);
3702 error_at (input_location
,
3703 "expression cannot be used as a function");
3706 return error_mark_node
;
3709 /* fntype now gets the type of function pointed to. */
3710 fntype
= TREE_TYPE (fntype
);
3711 parm_types
= TYPE_ARG_TYPES (fntype
);
3715 allocated
= make_tree_vector ();
3716 params
= &allocated
;
3719 nargs
= convert_arguments (parm_types
, params
, fndecl
, LOOKUP_NORMAL
,
3722 return error_mark_node
;
3724 argarray
= (*params
)->address ();
3726 /* Check for errors in format strings and inappropriately
3728 bool warned_p
= check_function_arguments (input_location
, fndecl
, fntype
,
3729 nargs
, argarray
, NULL
);
3731 ret
= build_cxx_call (function
, nargs
, argarray
, complain
);
3735 tree c
= extract_call_expr (ret
);
3736 if (TREE_CODE (c
) == CALL_EXPR
)
3737 TREE_NO_WARNING (c
) = 1;
3740 if (allocated
!= NULL
)
3741 release_tree_vector (allocated
);
3746 /* Subroutine of convert_arguments.
3747 Print an error message about a wrong number of arguments. */
3750 error_args_num (location_t loc
, tree fndecl
, bool too_many_p
)
3754 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
3756 if (DECL_NAME (fndecl
) == NULL_TREE
3757 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
3760 ? G_("too many arguments to constructor %q#D")
3761 : G_("too few arguments to constructor %q#D"),
3766 ? G_("too many arguments to member function %q#D")
3767 : G_("too few arguments to member function %q#D"),
3773 ? G_("too many arguments to function %q#D")
3774 : G_("too few arguments to function %q#D"),
3776 if (!DECL_IS_BUILTIN (fndecl
))
3777 inform (DECL_SOURCE_LOCATION (fndecl
), "declared here");
3781 if (c_dialect_objc () && objc_message_selector ())
3784 ? G_("too many arguments to method %q#D")
3785 : G_("too few arguments to method %q#D"),
3786 objc_message_selector ());
3788 error_at (loc
, too_many_p
? G_("too many arguments to function")
3789 : G_("too few arguments to function"));
3793 /* Convert the actual parameter expressions in the list VALUES to the
3794 types in the list TYPELIST. The converted expressions are stored
3795 back in the VALUES vector.
3796 If parmdecls is exhausted, or when an element has NULL as its type,
3797 perform the default conversions.
3799 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3801 This is also where warnings about wrong number of args are generated.
3803 Returns the actual number of arguments processed (which might be less
3804 than the length of the vector), or -1 on error.
3806 In C++, unspecified trailing parameters can be filled in with their
3807 default arguments, if such were specified. Do so here. */
3810 convert_arguments (tree typelist
, vec
<tree
, va_gc
> **values
, tree fndecl
,
3811 int flags
, tsubst_flags_t complain
)
3816 /* Argument passing is always copy-initialization. */
3817 flags
|= LOOKUP_ONLYCONVERTING
;
3819 for (i
= 0, typetail
= typelist
;
3820 i
< vec_safe_length (*values
);
3823 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3824 tree val
= (**values
)[i
];
3826 if (val
== error_mark_node
|| type
== error_mark_node
)
3829 if (type
== void_type_node
)
3831 if (complain
& tf_error
)
3833 error_args_num (input_location
, fndecl
, /*too_many_p=*/true);
3840 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3841 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3842 if (TREE_CODE (val
) == NOP_EXPR
3843 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
3844 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
3845 val
= TREE_OPERAND (val
, 0);
3847 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
3849 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
3850 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
3851 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
3852 val
= decay_conversion (val
, complain
);
3855 if (val
== error_mark_node
)
3860 /* Formal parm type is specified by a function prototype. */
3863 if (!COMPLETE_TYPE_P (complete_type (type
)))
3865 if (complain
& tf_error
)
3868 error ("parameter %P of %qD has incomplete type %qT",
3871 error ("parameter %P has incomplete type %qT", i
, type
);
3873 parmval
= error_mark_node
;
3877 parmval
= convert_for_initialization
3878 (NULL_TREE
, type
, val
, flags
,
3879 ICR_ARGPASS
, fndecl
, i
, complain
);
3880 parmval
= convert_for_arg_passing (type
, parmval
, complain
);
3883 if (parmval
== error_mark_node
)
3886 (**values
)[i
] = parmval
;
3890 if (fndecl
&& magic_varargs_p (fndecl
))
3891 /* Don't do ellipsis conversion for __built_in_constant_p
3892 as this will result in spurious errors for non-trivial
3894 val
= require_complete_type_sfinae (val
, complain
);
3896 val
= convert_arg_to_ellipsis (val
, complain
);
3898 (**values
)[i
] = val
;
3902 typetail
= TREE_CHAIN (typetail
);
3905 if (typetail
!= 0 && typetail
!= void_list_node
)
3907 /* See if there are default arguments that can be used. Because
3908 we hold default arguments in the FUNCTION_TYPE (which is so
3909 wrong), we can see default parameters here from deduced
3910 contexts (and via typeof) for indirect function calls.
3911 Fortunately we know whether we have a function decl to
3912 provide default arguments in a language conformant
3914 if (fndecl
&& TREE_PURPOSE (typetail
)
3915 && TREE_CODE (TREE_PURPOSE (typetail
)) != DEFAULT_ARG
)
3917 for (; typetail
!= void_list_node
; ++i
)
3919 /* After DR777, with explicit template args we can end up with a
3920 default argument followed by no default argument. */
3921 if (!TREE_PURPOSE (typetail
))
3924 = convert_default_arg (TREE_VALUE (typetail
),
3925 TREE_PURPOSE (typetail
),
3926 fndecl
, i
, complain
);
3928 if (parmval
== error_mark_node
)
3931 vec_safe_push (*values
, parmval
);
3932 typetail
= TREE_CHAIN (typetail
);
3933 /* ends with `...'. */
3934 if (typetail
== NULL_TREE
)
3939 if (typetail
&& typetail
!= void_list_node
)
3941 if (complain
& tf_error
)
3942 error_args_num (input_location
, fndecl
, /*too_many_p=*/false);
3950 /* Build a binary-operation expression, after performing default
3951 conversions on the operands. CODE is the kind of expression to
3952 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3953 are the tree codes which correspond to ARG1 and ARG2 when issuing
3954 warnings about possibly misplaced parentheses. They may differ
3955 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3956 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3957 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3958 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3959 ARG2_CODE as ERROR_MARK. */
3962 build_x_binary_op (location_t loc
, enum tree_code code
, tree arg1
,
3963 enum tree_code arg1_code
, tree arg2
,
3964 enum tree_code arg2_code
, tree
*overload_p
,
3965 tsubst_flags_t complain
)
3970 tree overload
= NULL_TREE
;
3975 if (processing_template_decl
)
3977 if (type_dependent_expression_p (arg1
)
3978 || type_dependent_expression_p (arg2
))
3979 return build_min_nt_loc (loc
, code
, arg1
, arg2
);
3980 arg1
= build_non_dependent_expr (arg1
);
3981 arg2
= build_non_dependent_expr (arg2
);
3984 if (code
== DOTSTAR_EXPR
)
3985 expr
= build_m_component_ref (arg1
, arg2
, complain
);
3987 expr
= build_new_op (loc
, code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
,
3988 &overload
, complain
);
3990 if (overload_p
!= NULL
)
3991 *overload_p
= overload
;
3993 /* Check for cases such as x+y<<z which users are likely to
3994 misinterpret. But don't warn about obj << x + y, since that is a
3995 common idiom for I/O. */
3996 if (warn_parentheses
3997 && (complain
& tf_warning
)
3998 && !processing_template_decl
3999 && !error_operand_p (arg1
)
4000 && !error_operand_p (arg2
)
4001 && (code
!= LSHIFT_EXPR
4002 || !CLASS_TYPE_P (TREE_TYPE (arg1
))))
4003 warn_about_parentheses (loc
, code
, arg1_code
, orig_arg1
,
4004 arg2_code
, orig_arg2
);
4006 if (processing_template_decl
&& expr
!= error_mark_node
)
4008 if (overload
!= NULL_TREE
)
4009 return (build_min_non_dep_op_overload
4010 (code
, expr
, overload
, orig_arg1
, orig_arg2
));
4012 return build_min_non_dep (code
, expr
, orig_arg1
, orig_arg2
);
4018 /* Build and return an ARRAY_REF expression. */
4021 build_x_array_ref (location_t loc
, tree arg1
, tree arg2
,
4022 tsubst_flags_t complain
)
4024 tree orig_arg1
= arg1
;
4025 tree orig_arg2
= arg2
;
4027 tree overload
= NULL_TREE
;
4029 if (processing_template_decl
)
4031 if (type_dependent_expression_p (arg1
)
4032 || type_dependent_expression_p (arg2
))
4033 return build_min_nt_loc (loc
, ARRAY_REF
, arg1
, arg2
,
4034 NULL_TREE
, NULL_TREE
);
4035 arg1
= build_non_dependent_expr (arg1
);
4036 arg2
= build_non_dependent_expr (arg2
);
4039 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, arg1
, arg2
,
4040 NULL_TREE
, &overload
, complain
);
4042 if (processing_template_decl
&& expr
!= error_mark_node
)
4044 if (overload
!= NULL_TREE
)
4045 return (build_min_non_dep_op_overload
4046 (ARRAY_REF
, expr
, overload
, orig_arg1
, orig_arg2
));
4048 return build_min_non_dep (ARRAY_REF
, expr
, orig_arg1
, orig_arg2
,
4049 NULL_TREE
, NULL_TREE
);
4054 /* Return whether OP is an expression of enum type cast to integer
4055 type. In C++ even unsigned enum types are cast to signed integer
4056 types. We do not want to issue warnings about comparisons between
4057 signed and unsigned types when one of the types is an enum type.
4058 Those warnings are always false positives in practice. */
4061 enum_cast_to_int (tree op
)
4063 if (CONVERT_EXPR_P (op
)
4064 && TREE_TYPE (op
) == integer_type_node
4065 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == ENUMERAL_TYPE
4066 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op
, 0))))
4069 /* The cast may have been pushed into a COND_EXPR. */
4070 if (TREE_CODE (op
) == COND_EXPR
)
4071 return (enum_cast_to_int (TREE_OPERAND (op
, 1))
4072 || enum_cast_to_int (TREE_OPERAND (op
, 2)));
4077 /* For the c-common bits. */
4079 build_binary_op (location_t location
, enum tree_code code
, tree op0
, tree op1
,
4082 return cp_build_binary_op (location
, code
, op0
, op1
, tf_warning_or_error
);
4085 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4086 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4089 build_vec_cmp (tree_code code
, tree type
,
4090 tree arg0
, tree arg1
)
4092 tree zero_vec
= build_zero_cst (type
);
4093 tree minus_one_vec
= build_minus_one_cst (type
);
4094 tree cmp_type
= build_same_sized_truth_vector_type(type
);
4095 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
4096 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
4099 /* Possibly warn about an address never being NULL. */
4102 warn_for_null_address (location_t location
, tree op
, tsubst_flags_t complain
)
4105 || (complain
& tf_warning
) == 0
4106 || c_inhibit_evaluation_warnings
!= 0
4107 || TREE_NO_WARNING (op
))
4110 tree cop
= fold_non_dependent_expr (op
);
4112 if (TREE_CODE (cop
) == ADDR_EXPR
4113 && decl_with_nonnull_addr_p (TREE_OPERAND (cop
, 0))
4114 && !TREE_NO_WARNING (cop
))
4115 warning_at (location
, OPT_Waddress
, "the address of %qD will never "
4116 "be NULL", TREE_OPERAND (cop
, 0));
4118 if (CONVERT_EXPR_P (op
)
4119 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == REFERENCE_TYPE
)
4122 STRIP_NOPS (inner_op
);
4124 if (DECL_P (inner_op
))
4125 warning_at (location
, OPT_Waddress
,
4126 "the compiler can assume that the address of "
4127 "%qD will never be NULL", inner_op
);
4131 /* Build a binary-operation expression without default conversions.
4132 CODE is the kind of expression to build.
4133 LOCATION is the location_t of the operator in the source code.
4134 This function differs from `build' in several ways:
4135 the data type of the result is computed and recorded in it,
4136 warnings are generated if arg data types are invalid,
4137 special handling for addition and subtraction of pointers is known,
4138 and some optimization is done (operations on narrow ints
4139 are done in the narrower type when that gives the same result).
4140 Constant folding is also done before the result is returned.
4142 Note that the operands will never have enumeral types
4143 because either they have just had the default conversions performed
4144 or they have both just been converted to some other type in which
4145 the arithmetic is to be done.
4147 C++: must do special pointer arithmetic when implementing
4148 multiple inheritance, and deal with pointer to member functions. */
4151 cp_build_binary_op (location_t location
,
4152 enum tree_code code
, tree orig_op0
, tree orig_op1
,
4153 tsubst_flags_t complain
)
4156 enum tree_code code0
, code1
;
4158 const char *invalid_op_diag
;
4160 /* Expression code to give to the expression when it is built.
4161 Normally this is CODE, which is what the caller asked for,
4162 but in some special cases we change it. */
4163 enum tree_code resultcode
= code
;
4165 /* Data type in which the computation is to be performed.
4166 In the simplest cases this is the common type of the arguments. */
4167 tree result_type
= NULL_TREE
;
4169 /* Nonzero means operands have already been type-converted
4170 in whatever way is necessary.
4171 Zero means they need to be converted to RESULT_TYPE. */
4174 /* Nonzero means create the expression with this type, rather than
4176 tree build_type
= 0;
4178 /* Nonzero means after finally constructing the expression
4179 convert it to this type. */
4180 tree final_type
= 0;
4182 tree result
, result_ovl
;
4184 /* Nonzero if this is an operation like MIN or MAX which can
4185 safely be computed in short if both args are promoted shorts.
4186 Also implies COMMON.
4187 -1 indicates a bitwise operation; this makes a difference
4188 in the exact conditions for when it is safe to do the operation
4189 in a narrower mode. */
4192 /* Nonzero if this is a comparison operation;
4193 if both args are promoted shorts, compare the original shorts.
4194 Also implies COMMON. */
4195 int short_compare
= 0;
4197 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4200 /* True if both operands have arithmetic type. */
4201 bool arithmetic_types_p
;
4203 /* Apply default conversions. */
4207 /* Remember whether we're doing / or %. */
4208 bool doing_div_or_mod
= false;
4210 /* Remember whether we're doing << or >>. */
4211 bool doing_shift
= false;
4213 /* Tree holding instrumentation expression. */
4214 tree instrument_expr
= NULL_TREE
;
4216 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
4217 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
4218 || code
== TRUTH_XOR_EXPR
)
4220 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
4221 op0
= decay_conversion (op0
, complain
);
4222 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
4223 op1
= decay_conversion (op1
, complain
);
4227 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
4228 op0
= cp_default_conversion (op0
, complain
);
4229 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
4230 op1
= cp_default_conversion (op1
, complain
);
4233 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4234 STRIP_TYPE_NOPS (op0
);
4235 STRIP_TYPE_NOPS (op1
);
4237 /* DTRT if one side is an overloaded function, but complain about it. */
4238 if (type_unknown_p (op0
))
4240 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
4241 if (t
!= error_mark_node
)
4243 if (complain
& tf_error
)
4244 permerror (input_location
, "assuming cast to type %qT from overloaded function",
4249 if (type_unknown_p (op1
))
4251 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
4252 if (t
!= error_mark_node
)
4254 if (complain
& tf_error
)
4255 permerror (input_location
, "assuming cast to type %qT from overloaded function",
4261 type0
= TREE_TYPE (op0
);
4262 type1
= TREE_TYPE (op1
);
4264 /* The expression codes of the data types of the arguments tell us
4265 whether the arguments are integers, floating, pointers, etc. */
4266 code0
= TREE_CODE (type0
);
4267 code1
= TREE_CODE (type1
);
4269 /* If an error was already reported for one of the arguments,
4270 avoid reporting another error. */
4271 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
4272 return error_mark_node
;
4274 if ((invalid_op_diag
4275 = targetm
.invalid_binary_op (code
, type0
, type1
)))
4277 if (complain
& tf_error
)
4278 error (invalid_op_diag
);
4279 return error_mark_node
;
4282 /* Issue warnings about peculiar, but valid, uses of NULL. */
4283 if ((orig_op0
== null_node
|| orig_op1
== null_node
)
4284 /* It's reasonable to use pointer values as operands of &&
4285 and ||, so NULL is no exception. */
4286 && code
!= TRUTH_ANDIF_EXPR
&& code
!= TRUTH_ORIF_EXPR
4287 && ( /* Both are NULL (or 0) and the operation was not a
4288 comparison or a pointer subtraction. */
4289 (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)
4290 && code
!= EQ_EXPR
&& code
!= NE_EXPR
&& code
!= MINUS_EXPR
)
4291 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4292 || (!null_ptr_cst_p (orig_op0
)
4293 && !TYPE_PTR_OR_PTRMEM_P (type0
))
4294 || (!null_ptr_cst_p (orig_op1
)
4295 && !TYPE_PTR_OR_PTRMEM_P (type1
)))
4296 && (complain
& tf_warning
))
4298 source_location loc
=
4299 expansion_point_location_if_in_system_header (input_location
);
4301 warning_at (loc
, OPT_Wpointer_arith
, "NULL used in arithmetic");
4304 /* In case when one of the operands of the binary operation is
4305 a vector and another is a scalar -- convert scalar to vector. */
4306 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
4308 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
4309 complain
& tf_error
);
4311 switch (convert_flag
)
4314 return error_mark_node
;
4317 op0
= convert (TREE_TYPE (type1
), op0
);
4318 op0
= save_expr (op0
);
4319 op0
= build_vector_from_val (type1
, op0
);
4320 type0
= TREE_TYPE (op0
);
4321 code0
= TREE_CODE (type0
);
4327 op1
= convert (TREE_TYPE (type0
), op1
);
4328 op1
= save_expr (op1
);
4329 op1
= build_vector_from_val (type0
, op1
);
4330 type1
= TREE_TYPE (op1
);
4331 code1
= TREE_CODE (type1
);
4343 /* Subtraction of two similar pointers.
4344 We must subtract them as integers, then divide by object size. */
4345 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
4346 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4348 return pointer_diff (location
, op0
, op1
,
4349 common_pointer_type (type0
, type1
), complain
);
4350 /* In all other cases except pointer - int, the usual arithmetic
4352 else if (!(code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
))
4357 /* The pointer - int case is just like pointer + int; fall
4361 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
4362 && (code0
== INTEGER_TYPE
|| code1
== INTEGER_TYPE
))
4366 ptr_operand
= ((code0
== POINTER_TYPE
) ? op0
: op1
);
4367 int_operand
= ((code0
== INTEGER_TYPE
) ? op0
: op1
);
4368 if (processing_template_decl
)
4370 result_type
= TREE_TYPE (ptr_operand
);
4373 return cp_pointer_int_sum (location
, code
,
4385 case TRUNC_DIV_EXPR
:
4387 case FLOOR_DIV_EXPR
:
4388 case ROUND_DIV_EXPR
:
4389 case EXACT_DIV_EXPR
:
4390 if (TREE_CODE (op0
) == SIZEOF_EXPR
&& TREE_CODE (op1
) == SIZEOF_EXPR
)
4392 tree type0
= TREE_OPERAND (op0
, 0);
4393 tree type1
= TREE_OPERAND (op1
, 0);
4394 tree first_arg
= type0
;
4395 if (!TYPE_P (type0
))
4396 type0
= TREE_TYPE (type0
);
4397 if (!TYPE_P (type1
))
4398 type1
= TREE_TYPE (type1
);
4399 if (POINTER_TYPE_P (type0
) && same_type_p (TREE_TYPE (type0
), type1
)
4400 && !(TREE_CODE (first_arg
) == PARM_DECL
4401 && DECL_ARRAY_PARAMETER_P (first_arg
)
4402 && warn_sizeof_array_argument
)
4403 && (complain
& tf_warning
))
4404 if (warning_at (location
, OPT_Wsizeof_pointer_div
,
4405 "division %<sizeof (%T) / sizeof (%T)%> does "
4406 "not compute the number of array elements",
4408 if (DECL_P (first_arg
))
4409 inform (DECL_SOURCE_LOCATION (first_arg
),
4410 "first %<sizeof%> operand was declared here");
4413 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4414 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
4415 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4416 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
4418 enum tree_code tcode0
= code0
, tcode1
= code1
;
4419 tree cop1
= fold_non_dependent_expr (op1
);
4420 doing_div_or_mod
= true;
4421 warn_for_div_by_zero (location
, cop1
);
4423 if (tcode0
== COMPLEX_TYPE
|| tcode0
== VECTOR_TYPE
)
4424 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
4425 if (tcode1
== COMPLEX_TYPE
|| tcode1
== VECTOR_TYPE
)
4426 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
4428 if (!(tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
))
4429 resultcode
= RDIV_EXPR
;
4431 /* When dividing two signed integers, we have to promote to int.
4432 unless we divide by a constant != -1. Note that default
4433 conversion will have been performed on the operands at this
4434 point, so we have to dig out the original type to find out if
4436 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4437 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4438 || (TREE_CODE (op1
) == INTEGER_CST
4439 && ! integer_all_onesp (op1
)));
4448 if ((code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4449 || (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4450 && !VECTOR_FLOAT_TYPE_P (type0
)
4451 && !VECTOR_FLOAT_TYPE_P (type1
)))
4455 case TRUNC_MOD_EXPR
:
4456 case FLOOR_MOD_EXPR
:
4458 tree cop1
= fold_non_dependent_expr (op1
);
4459 doing_div_or_mod
= true;
4460 warn_for_div_by_zero (location
, cop1
);
4463 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4464 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4465 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
4467 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4469 /* Although it would be tempting to shorten always here, that loses
4470 on some targets, since the modulo instruction is undefined if the
4471 quotient can't be represented in the computation mode. We shorten
4472 only if unsigned or if dividing by something we know != -1. */
4473 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4474 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4475 || (TREE_CODE (op1
) == INTEGER_CST
4476 && ! integer_all_onesp (op1
)));
4481 case TRUTH_ANDIF_EXPR
:
4482 case TRUTH_ORIF_EXPR
:
4483 case TRUTH_AND_EXPR
:
4485 if (!VECTOR_TYPE_P (type0
) && VECTOR_TYPE_P (type1
))
4487 if (!COMPARISON_CLASS_P (op1
))
4488 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4489 build_zero_cst (type1
), complain
);
4490 if (code
== TRUTH_ANDIF_EXPR
)
4492 tree z
= build_zero_cst (TREE_TYPE (op1
));
4493 return build_conditional_expr (location
, op0
, op1
, z
, complain
);
4495 else if (code
== TRUTH_ORIF_EXPR
)
4497 tree m1
= build_all_ones_cst (TREE_TYPE (op1
));
4498 return build_conditional_expr (location
, op0
, m1
, op1
, complain
);
4503 if (VECTOR_TYPE_P (type0
))
4505 if (!COMPARISON_CLASS_P (op0
))
4506 op0
= cp_build_binary_op (EXPR_LOCATION (op0
), NE_EXPR
, op0
,
4507 build_zero_cst (type0
), complain
);
4508 if (!VECTOR_TYPE_P (type1
))
4510 tree m1
= build_all_ones_cst (TREE_TYPE (op0
));
4511 tree z
= build_zero_cst (TREE_TYPE (op0
));
4512 op1
= build_conditional_expr (location
, op1
, m1
, z
, complain
);
4514 else if (!COMPARISON_CLASS_P (op1
))
4515 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
4516 build_zero_cst (type1
), complain
);
4518 if (code
== TRUTH_ANDIF_EXPR
)
4519 code
= BIT_AND_EXPR
;
4520 else if (code
== TRUTH_ORIF_EXPR
)
4521 code
= BIT_IOR_EXPR
;
4525 return cp_build_binary_op (location
, code
, op0
, op1
, complain
);
4528 result_type
= boolean_type_node
;
4531 /* Shift operations: result has same type as first operand;
4532 always convert second operand to int.
4533 Also set SHORT_SHIFT if shifting rightward. */
4536 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4537 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4539 result_type
= type0
;
4542 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4543 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4544 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4545 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
4547 result_type
= type0
;
4550 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4552 tree const_op1
= fold_non_dependent_expr (op1
);
4553 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4555 result_type
= type0
;
4557 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4559 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4561 if ((complain
& tf_warning
)
4562 && c_inhibit_evaluation_warnings
== 0)
4563 warning (OPT_Wshift_count_negative
,
4564 "right shift count is negative");
4568 if (compare_tree_int (const_op1
, TYPE_PRECISION (type0
)) >= 0
4569 && (complain
& tf_warning
)
4570 && c_inhibit_evaluation_warnings
== 0)
4571 warning (OPT_Wshift_count_overflow
,
4572 "right shift count >= width of type");
4575 /* Avoid converting op1 to result_type later. */
4581 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4582 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4584 result_type
= type0
;
4587 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4588 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4589 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4590 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
4592 result_type
= type0
;
4595 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4597 tree const_op0
= fold_non_dependent_expr (op0
);
4598 if (TREE_CODE (const_op0
) != INTEGER_CST
)
4600 tree const_op1
= fold_non_dependent_expr (op1
);
4601 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4603 result_type
= type0
;
4605 if (TREE_CODE (const_op0
) == INTEGER_CST
4606 && tree_int_cst_sgn (const_op0
) < 0
4607 && (complain
& tf_warning
)
4608 && c_inhibit_evaluation_warnings
== 0)
4609 warning (OPT_Wshift_negative_value
,
4610 "left shift of negative value");
4611 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4613 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4615 if ((complain
& tf_warning
)
4616 && c_inhibit_evaluation_warnings
== 0)
4617 warning (OPT_Wshift_count_negative
,
4618 "left shift count is negative");
4620 else if (compare_tree_int (const_op1
,
4621 TYPE_PRECISION (type0
)) >= 0)
4623 if ((complain
& tf_warning
)
4624 && c_inhibit_evaluation_warnings
== 0)
4625 warning (OPT_Wshift_count_overflow
,
4626 "left shift count >= width of type");
4628 else if (TREE_CODE (const_op0
) == INTEGER_CST
4629 && (complain
& tf_warning
))
4630 maybe_warn_shift_overflow (location
, const_op0
, const_op1
);
4632 /* Avoid converting op1 to result_type later. */
4639 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4641 result_type
= type0
;
4642 if (TREE_CODE (op1
) == INTEGER_CST
)
4644 if (tree_int_cst_lt (op1
, integer_zero_node
))
4646 if (complain
& tf_warning
)
4647 warning (0, (code
== LROTATE_EXPR
)
4648 ? G_("left rotate count is negative")
4649 : G_("right rotate count is negative"));
4651 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
4653 if (complain
& tf_warning
)
4654 warning (0, (code
== LROTATE_EXPR
)
4655 ? G_("left rotate count >= width of type")
4656 : G_("right rotate count >= width of type"));
4659 /* Convert the shift-count to an integer, regardless of
4660 size of value being shifted. */
4661 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4662 op1
= cp_convert (integer_type_node
, op1
, complain
);
4668 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4669 goto vector_compare
;
4670 if ((complain
& tf_warning
)
4671 && (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
)))
4672 warning (OPT_Wfloat_equal
,
4673 "comparing floating point with == or != is unsafe");
4674 if ((complain
& tf_warning
)
4675 && ((TREE_CODE (orig_op0
) == STRING_CST
4676 && !integer_zerop (cp_fully_fold (op1
)))
4677 || (TREE_CODE (orig_op1
) == STRING_CST
4678 && !integer_zerop (cp_fully_fold (op0
)))))
4679 warning (OPT_Waddress
, "comparison with string literal results "
4680 "in unspecified behavior");
4682 build_type
= boolean_type_node
;
4683 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4684 || code0
== COMPLEX_TYPE
|| code0
== ENUMERAL_TYPE
)
4685 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4686 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
))
4688 else if (((code0
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type0
))
4689 && null_ptr_cst_p (orig_op1
))
4690 /* Handle, eg, (void*)0 (c++/43906), and more. */
4691 || (code0
== POINTER_TYPE
4692 && TYPE_PTR_P (type1
) && integer_zerop (op1
)))
4694 if (TYPE_PTR_P (type1
))
4695 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4696 CPO_COMPARISON
, complain
);
4698 result_type
= type0
;
4700 if (char_type_p (TREE_TYPE (orig_op1
))
4701 && warning (OPT_Wpointer_compare
,
4702 "comparison between pointer and zero character "
4704 inform (input_location
,
4705 "did you mean to dereference the pointer?");
4706 warn_for_null_address (location
, op0
, complain
);
4708 else if (((code1
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type1
))
4709 && null_ptr_cst_p (orig_op0
))
4710 /* Handle, eg, (void*)0 (c++/43906), and more. */
4711 || (code1
== POINTER_TYPE
4712 && TYPE_PTR_P (type0
) && integer_zerop (op0
)))
4714 if (TYPE_PTR_P (type0
))
4715 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4716 CPO_COMPARISON
, complain
);
4718 result_type
= type1
;
4720 if (char_type_p (TREE_TYPE (orig_op0
))
4721 && warning (OPT_Wpointer_compare
,
4722 "comparison between pointer and zero character "
4724 inform (input_location
,
4725 "did you mean to dereference the pointer?");
4726 warn_for_null_address (location
, op1
, complain
);
4728 else if ((code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4729 || (TYPE_PTRDATAMEM_P (type0
) && TYPE_PTRDATAMEM_P (type1
)))
4730 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4731 CPO_COMPARISON
, complain
);
4732 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
4733 /* One of the operands must be of nullptr_t type. */
4734 result_type
= TREE_TYPE (nullptr_node
);
4735 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4737 result_type
= type0
;
4738 if (complain
& tf_error
)
4739 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4741 return error_mark_node
;
4743 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4745 result_type
= type1
;
4746 if (complain
& tf_error
)
4747 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4749 return error_mark_node
;
4751 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (orig_op1
))
4753 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4754 == ptrmemfunc_vbit_in_delta
)
4756 tree pfn0
, delta0
, e1
, e2
;
4758 if (TREE_SIDE_EFFECTS (op0
))
4759 op0
= save_expr (op0
);
4761 pfn0
= pfn_from_ptrmemfunc (op0
);
4762 delta0
= delta_from_ptrmemfunc (op0
);
4763 e1
= cp_build_binary_op (location
,
4766 build_zero_cst (TREE_TYPE (pfn0
)),
4768 e2
= cp_build_binary_op (location
,
4774 if (complain
& tf_warning
)
4775 maybe_warn_zero_as_null_pointer_constant (op1
, input_location
);
4777 e2
= cp_build_binary_op (location
,
4778 EQ_EXPR
, e2
, integer_zero_node
,
4780 op0
= cp_build_binary_op (location
,
4781 TRUTH_ANDIF_EXPR
, e1
, e2
,
4783 op1
= cp_convert (TREE_TYPE (op0
), integer_one_node
, complain
);
4787 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
4788 op1
= cp_convert (TREE_TYPE (op0
), op1
, complain
);
4790 result_type
= TREE_TYPE (op0
);
4792 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (orig_op0
))
4793 return cp_build_binary_op (location
, code
, op1
, op0
, complain
);
4794 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
))
4797 /* E will be the final comparison. */
4799 /* E1 and E2 are for scratch. */
4807 type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4808 CPO_COMPARISON
, complain
);
4810 if (!same_type_p (TREE_TYPE (op0
), type
))
4811 op0
= cp_convert_and_check (type
, op0
, complain
);
4812 if (!same_type_p (TREE_TYPE (op1
), type
))
4813 op1
= cp_convert_and_check (type
, op1
, complain
);
4815 if (op0
== error_mark_node
|| op1
== error_mark_node
)
4816 return error_mark_node
;
4818 if (TREE_SIDE_EFFECTS (op0
))
4819 op0
= save_expr (op0
);
4820 if (TREE_SIDE_EFFECTS (op1
))
4821 op1
= save_expr (op1
);
4823 pfn0
= pfn_from_ptrmemfunc (op0
);
4824 pfn0
= cp_fully_fold (pfn0
);
4825 /* Avoid -Waddress warnings (c++/64877). */
4826 if (TREE_CODE (pfn0
) == ADDR_EXPR
)
4827 TREE_NO_WARNING (pfn0
) = 1;
4828 pfn1
= pfn_from_ptrmemfunc (op1
);
4829 pfn1
= cp_fully_fold (pfn1
);
4830 delta0
= delta_from_ptrmemfunc (op0
);
4831 delta1
= delta_from_ptrmemfunc (op1
);
4832 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4833 == ptrmemfunc_vbit_in_delta
)
4838 && ((op0.delta == op1.delta)
4839 || (!op0.pfn && op0.delta & 1 == 0
4840 && op1.delta & 1 == 0))
4842 The reason for the `!op0.pfn' bit is that a NULL
4843 pointer-to-member is any member with a zero PFN and
4844 LSB of the DELTA field is 0. */
4846 e1
= cp_build_binary_op (location
, BIT_AND_EXPR
,
4850 e1
= cp_build_binary_op (location
,
4851 EQ_EXPR
, e1
, integer_zero_node
,
4853 e2
= cp_build_binary_op (location
, BIT_AND_EXPR
,
4857 e2
= cp_build_binary_op (location
,
4858 EQ_EXPR
, e2
, integer_zero_node
,
4860 e1
= cp_build_binary_op (location
,
4861 TRUTH_ANDIF_EXPR
, e2
, e1
,
4863 e2
= cp_build_binary_op (location
, EQ_EXPR
,
4865 build_zero_cst (TREE_TYPE (pfn0
)),
4867 e2
= cp_build_binary_op (location
,
4868 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
4869 e1
= cp_build_binary_op (location
,
4870 EQ_EXPR
, delta0
, delta1
, complain
);
4871 e1
= cp_build_binary_op (location
,
4872 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
4879 && (!op0.pfn || op0.delta == op1.delta))
4881 The reason for the `!op0.pfn' bit is that a NULL
4882 pointer-to-member is any member with a zero PFN; the
4883 DELTA field is unspecified. */
4885 e1
= cp_build_binary_op (location
,
4886 EQ_EXPR
, delta0
, delta1
, complain
);
4887 e2
= cp_build_binary_op (location
,
4890 build_zero_cst (TREE_TYPE (pfn0
)),
4892 e1
= cp_build_binary_op (location
,
4893 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
4895 e2
= build2 (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
4896 e
= cp_build_binary_op (location
,
4897 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
4898 if (code
== EQ_EXPR
)
4900 return cp_build_binary_op (location
,
4901 EQ_EXPR
, e
, integer_zero_node
, complain
);
4905 gcc_assert (!TYPE_PTRMEMFUNC_P (type0
)
4906 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
),
4908 gcc_assert (!TYPE_PTRMEMFUNC_P (type1
)
4909 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
),
4917 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
4918 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
4920 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4921 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4922 CPO_COMPARISON
, complain
);
4929 if (TREE_CODE (orig_op0
) == STRING_CST
4930 || TREE_CODE (orig_op1
) == STRING_CST
)
4932 if (complain
& tf_warning
)
4933 warning (OPT_Waddress
, "comparison with string literal results "
4934 "in unspecified behavior");
4937 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4941 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4943 && !vector_types_compatible_elements_p (type0
, type1
))
4945 if (complain
& tf_error
)
4947 error_at (location
, "comparing vectors with different "
4949 inform (location
, "operand types are %qT and %qT",
4952 return error_mark_node
;
4955 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
4957 if (complain
& tf_error
)
4959 error_at (location
, "comparing vectors with different "
4960 "number of elements");
4961 inform (location
, "operand types are %qT and %qT",
4964 return error_mark_node
;
4967 /* It's not precisely specified how the usual arithmetic
4968 conversions apply to the vector types. Here, we use
4969 the unsigned type if one of the operands is signed and
4970 the other one is unsigned. */
4971 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
4973 if (!TYPE_UNSIGNED (type0
))
4974 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
4976 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
4977 warning_at (location
, OPT_Wsign_compare
, "comparison between "
4978 "types %qT and %qT", type0
, type1
);
4981 /* Always construct signed integer vector type. */
4982 intt
= c_common_type_for_size
4983 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0
))), 0);
4986 if (complain
& tf_error
)
4987 error_at (location
, "could not find an integer type "
4988 "of the same size as %qT", TREE_TYPE (type0
));
4989 return error_mark_node
;
4991 result_type
= build_opaque_vector_type (intt
,
4992 TYPE_VECTOR_SUBPARTS (type0
));
4994 return build_vec_cmp (resultcode
, result_type
, op0
, op1
);
4996 build_type
= boolean_type_node
;
4997 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4998 || code0
== ENUMERAL_TYPE
)
4999 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5000 || code1
== ENUMERAL_TYPE
))
5002 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5003 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
5004 CPO_COMPARISON
, complain
);
5005 else if (code0
== POINTER_TYPE
&& null_ptr_cst_p (orig_op1
))
5007 result_type
= type0
;
5008 if (extra_warnings
&& (complain
& tf_warning
))
5009 warning (OPT_Wextra
,
5010 "ordered comparison of pointer with integer zero");
5012 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (orig_op0
))
5014 result_type
= type1
;
5015 if (extra_warnings
&& (complain
& tf_warning
))
5016 warning (OPT_Wextra
,
5017 "ordered comparison of pointer with integer zero");
5019 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
5020 /* One of the operands must be of nullptr_t type. */
5021 result_type
= TREE_TYPE (nullptr_node
);
5022 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5024 result_type
= type0
;
5025 if (complain
& tf_error
)
5026 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
5028 return error_mark_node
;
5030 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
5032 result_type
= type1
;
5033 if (complain
& tf_error
)
5034 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
5036 return error_mark_node
;
5040 case UNORDERED_EXPR
:
5047 build_type
= integer_type_node
;
5048 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
5050 if (complain
& tf_error
)
5051 error ("unordered comparison on non-floating point argument");
5052 return error_mark_node
;
5061 if (((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
5062 || code0
== ENUMERAL_TYPE
)
5063 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5064 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
)))
5065 arithmetic_types_p
= 1;
5068 arithmetic_types_p
= 0;
5069 /* Vector arithmetic is only allowed when both sides are vectors. */
5070 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
5072 if (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
5073 || !vector_types_compatible_elements_p (type0
, type1
))
5075 if (complain
& tf_error
)
5077 /* "location" already embeds the locations of the
5078 operands, so we don't need to add them separately
5080 rich_location
richloc (line_table
, location
);
5081 binary_op_error (&richloc
, code
, type0
, type1
);
5083 return error_mark_node
;
5085 arithmetic_types_p
= 1;
5088 /* Determine the RESULT_TYPE, if it is not already known. */
5090 && arithmetic_types_p
5091 && (shorten
|| common
|| short_compare
))
5093 result_type
= cp_common_type (type0
, type1
);
5094 if (complain
& tf_warning
)
5095 do_warn_double_promotion (result_type
, type0
, type1
,
5096 "implicit conversion from %qH to %qI "
5097 "to match other operand of binary "
5104 if (complain
& tf_error
)
5106 "invalid operands of types %qT and %qT to binary %qO",
5107 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
5108 return error_mark_node
;
5111 /* If we're in a template, the only thing we need to know is the
5113 if (processing_template_decl
)
5115 /* Since the middle-end checks the type when doing a build2, we
5116 need to build the tree in pieces. This built tree will never
5117 get out of the front-end as we replace it when instantiating
5119 tree tmp
= build2 (resultcode
,
5120 build_type
? build_type
: result_type
,
5122 TREE_OPERAND (tmp
, 0) = op0
;
5126 /* Remember the original type; RESULT_TYPE might be changed later on
5127 by shorten_binary_op. */
5128 tree orig_type
= result_type
;
5130 if (arithmetic_types_p
)
5132 bool first_complex
= (code0
== COMPLEX_TYPE
);
5133 bool second_complex
= (code1
== COMPLEX_TYPE
);
5134 int none_complex
= (!first_complex
&& !second_complex
);
5136 /* Adapted from patch for c/24581. */
5137 if (first_complex
!= second_complex
5138 && (code
== PLUS_EXPR
5139 || code
== MINUS_EXPR
5140 || code
== MULT_EXPR
5141 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
5142 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
5143 && flag_signed_zeros
)
5145 /* An operation on mixed real/complex operands must be
5146 handled specially, but the language-independent code can
5147 more easily optimize the plain complex arithmetic if
5148 -fno-signed-zeros. */
5149 tree real_type
= TREE_TYPE (result_type
);
5153 if (TREE_TYPE (op0
) != result_type
)
5154 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5155 if (TREE_TYPE (op1
) != real_type
)
5156 op1
= cp_convert_and_check (real_type
, op1
, complain
);
5160 if (TREE_TYPE (op0
) != real_type
)
5161 op0
= cp_convert_and_check (real_type
, op0
, complain
);
5162 if (TREE_TYPE (op1
) != result_type
)
5163 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5165 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
5166 return error_mark_node
;
5169 op0
= save_expr (op0
);
5170 real
= cp_build_unary_op (REALPART_EXPR
, op0
, true, complain
);
5171 imag
= cp_build_unary_op (IMAGPART_EXPR
, op0
, true, complain
);
5175 case TRUNC_DIV_EXPR
:
5176 op1
= save_expr (op1
);
5177 imag
= build2 (resultcode
, real_type
, imag
, op1
);
5181 real
= build2 (resultcode
, real_type
, real
, op1
);
5189 op1
= save_expr (op1
);
5190 real
= cp_build_unary_op (REALPART_EXPR
, op1
, true, complain
);
5191 imag
= cp_build_unary_op (IMAGPART_EXPR
, op1
, true, complain
);
5195 op0
= save_expr (op0
);
5196 imag
= build2 (resultcode
, real_type
, op0
, imag
);
5199 real
= build2 (resultcode
, real_type
, op0
, real
);
5202 real
= build2 (resultcode
, real_type
, op0
, real
);
5203 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
5209 result
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
5213 /* For certain operations (which identify themselves by shorten != 0)
5214 if both args were extended from the same smaller type,
5215 do the arithmetic in that type and then extend.
5217 shorten !=0 and !=1 indicates a bitwise operation.
5218 For them, this optimization is safe only if
5219 both args are zero-extended or both are sign-extended.
5220 Otherwise, we might change the result.
5221 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5222 but calculated in (unsigned short) it would be (unsigned short)-1. */
5224 if (shorten
&& none_complex
)
5226 final_type
= result_type
;
5227 result_type
= shorten_binary_op (result_type
, op0
, op1
,
5231 /* Comparison operations are shortened too but differently.
5232 They identify themselves by setting short_compare = 1. */
5236 /* We call shorten_compare only for diagnostic-reason. */
5237 tree xop0
= fold_simple (op0
), xop1
= fold_simple (op1
),
5238 xresult_type
= result_type
;
5239 enum tree_code xresultcode
= resultcode
;
5240 shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
5244 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
5245 && warn_sign_compare
5246 /* Do not warn until the template is instantiated; we cannot
5247 bound the ranges of the arguments until that point. */
5248 && !processing_template_decl
5249 && (complain
& tf_warning
)
5250 && c_inhibit_evaluation_warnings
== 0
5251 /* Even unsigned enum types promote to signed int. We don't
5252 want to issue -Wsign-compare warnings for this case. */
5253 && !enum_cast_to_int (orig_op0
)
5254 && !enum_cast_to_int (orig_op1
))
5256 tree oop0
= maybe_constant_value (orig_op0
);
5257 tree oop1
= maybe_constant_value (orig_op1
);
5259 if (TREE_CODE (oop0
) != INTEGER_CST
)
5260 oop0
= cp_fully_fold (orig_op0
);
5261 if (TREE_CODE (oop1
) != INTEGER_CST
)
5262 oop1
= cp_fully_fold (orig_op1
);
5263 warn_for_sign_compare (location
, oop0
, oop1
, op0
, op1
,
5264 result_type
, resultcode
);
5268 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5269 Then the expression will be built.
5270 It will be given type FINAL_TYPE if that is nonzero;
5271 otherwise, it will be given type RESULT_TYPE. */
5274 if (TREE_TYPE (op0
) != result_type
)
5275 op0
= cp_convert_and_check (result_type
, op0
, complain
);
5276 if (TREE_TYPE (op1
) != result_type
)
5277 op1
= cp_convert_and_check (result_type
, op1
, complain
);
5279 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5280 return error_mark_node
;
5283 if (build_type
== NULL_TREE
)
5284 build_type
= result_type
;
5286 if (sanitize_flags_p ((SANITIZE_SHIFT
5287 | SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
))
5288 && current_function_decl
!= NULL_TREE
5289 && !processing_template_decl
5290 && (doing_div_or_mod
|| doing_shift
))
5292 /* OP0 and/or OP1 might have side-effects. */
5293 op0
= cp_save_expr (op0
);
5294 op1
= cp_save_expr (op1
);
5295 op0
= fold_non_dependent_expr (op0
);
5296 op1
= fold_non_dependent_expr (op1
);
5297 if (doing_div_or_mod
5298 && sanitize_flags_p (SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
))
5300 /* For diagnostics we want to use the promoted types without
5301 shorten_binary_op. So convert the arguments to the
5302 original result_type. */
5305 if (TREE_TYPE (cop0
) != orig_type
)
5306 cop0
= cp_convert (orig_type
, op0
, complain
);
5307 if (TREE_TYPE (cop1
) != orig_type
)
5308 cop1
= cp_convert (orig_type
, op1
, complain
);
5309 instrument_expr
= ubsan_instrument_division (location
, cop0
, cop1
);
5311 else if (doing_shift
&& sanitize_flags_p (SANITIZE_SHIFT
))
5312 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
5315 result
= build2_loc (location
, resultcode
, build_type
, op0
, op1
);
5316 if (final_type
!= 0)
5317 result
= cp_convert (final_type
, result
, complain
);
5319 if (instrument_expr
!= NULL
)
5320 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
5321 instrument_expr
, result
);
5323 if (!processing_template_decl
)
5325 op0
= cp_fully_fold (op0
);
5326 /* Only consider the second argument if the first isn't overflowed. */
5327 if (!CONSTANT_CLASS_P (op0
) || TREE_OVERFLOW_P (op0
))
5329 op1
= cp_fully_fold (op1
);
5330 if (!CONSTANT_CLASS_P (op1
) || TREE_OVERFLOW_P (op1
))
5333 else if (!CONSTANT_CLASS_P (op0
) || !CONSTANT_CLASS_P (op1
)
5334 || TREE_OVERFLOW_P (op0
) || TREE_OVERFLOW_P (op1
))
5337 result_ovl
= fold_build2 (resultcode
, build_type
, op0
, op1
);
5338 if (TREE_OVERFLOW_P (result_ovl
))
5339 overflow_warning (location
, result_ovl
);
5344 /* Build a VEC_PERM_EXPR.
5345 This is a simple wrapper for c_build_vec_perm_expr. */
5347 build_x_vec_perm_expr (location_t loc
,
5348 tree arg0
, tree arg1
, tree arg2
,
5349 tsubst_flags_t complain
)
5351 tree orig_arg0
= arg0
;
5352 tree orig_arg1
= arg1
;
5353 tree orig_arg2
= arg2
;
5354 if (processing_template_decl
)
5356 if (type_dependent_expression_p (arg0
)
5357 || type_dependent_expression_p (arg1
)
5358 || type_dependent_expression_p (arg2
))
5359 return build_min_nt_loc (loc
, VEC_PERM_EXPR
, arg0
, arg1
, arg2
);
5360 arg0
= build_non_dependent_expr (arg0
);
5362 arg1
= build_non_dependent_expr (arg1
);
5363 arg2
= build_non_dependent_expr (arg2
);
5365 tree exp
= c_build_vec_perm_expr (loc
, arg0
, arg1
, arg2
, complain
& tf_error
);
5366 if (processing_template_decl
&& exp
!= error_mark_node
)
5367 return build_min_non_dep (VEC_PERM_EXPR
, exp
, orig_arg0
,
5368 orig_arg1
, orig_arg2
);
5372 /* Return a tree for the sum or difference (RESULTCODE says which)
5373 of pointer PTROP and integer INTOP. */
5376 cp_pointer_int_sum (location_t loc
, enum tree_code resultcode
, tree ptrop
,
5377 tree intop
, tsubst_flags_t complain
)
5379 tree res_type
= TREE_TYPE (ptrop
);
5381 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5382 in certain circumstance (when it's valid to do so). So we need
5383 to make sure it's complete. We don't need to check here, if we
5384 can actually complete it at all, as those checks will be done in
5385 pointer_int_sum() anyway. */
5386 complete_type (TREE_TYPE (res_type
));
5388 return pointer_int_sum (loc
, resultcode
, ptrop
,
5389 intop
, complain
& tf_warning_or_error
);
5392 /* Return a tree for the difference of pointers OP0 and OP1.
5393 The resulting tree has type int. */
5396 pointer_diff (location_t loc
, tree op0
, tree op1
, tree ptrtype
,
5397 tsubst_flags_t complain
)
5399 tree result
, inttype
;
5400 tree restype
= ptrdiff_type_node
;
5401 tree target_type
= TREE_TYPE (ptrtype
);
5403 if (!complete_type_or_else (target_type
, NULL_TREE
))
5404 return error_mark_node
;
5406 if (VOID_TYPE_P (target_type
))
5408 if (complain
& tf_error
)
5409 permerror (loc
, "ISO C++ forbids using pointer of "
5410 "type %<void *%> in subtraction");
5412 return error_mark_node
;
5414 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
5416 if (complain
& tf_error
)
5417 permerror (loc
, "ISO C++ forbids using pointer to "
5418 "a function in subtraction");
5420 return error_mark_node
;
5422 if (TREE_CODE (target_type
) == METHOD_TYPE
)
5424 if (complain
& tf_error
)
5425 permerror (loc
, "ISO C++ forbids using pointer to "
5426 "a method in subtraction");
5428 return error_mark_node
;
5431 /* Determine integer type result of the subtraction. This will usually
5432 be the same as the result type (ptrdiff_t), but may need to be a wider
5433 type if pointers for the address space are wider than ptrdiff_t. */
5434 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
5435 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
5439 /* First do the subtraction, then build the divide operator
5440 and only convert at the very end.
5441 Do not do default conversions in case restype is a short type. */
5443 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
5444 pointers. If some platform cannot provide that, or has a larger
5445 ptrdiff_type to support differences larger than half the address
5446 space, cast the pointers to some larger integer type and do the
5447 computations in that type. */
5448 if (TYPE_PRECISION (inttype
) > TYPE_PRECISION (TREE_TYPE (op0
)))
5449 op0
= cp_build_binary_op (loc
,
5451 cp_convert (inttype
, op0
, complain
),
5452 cp_convert (inttype
, op1
, complain
),
5455 op0
= build2_loc (loc
, POINTER_DIFF_EXPR
, inttype
, op0
, op1
);
5457 /* This generates an error if op1 is a pointer to an incomplete type. */
5458 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
5460 if (complain
& tf_error
)
5461 error_at (loc
, "invalid use of a pointer to an incomplete type in "
5462 "pointer arithmetic");
5464 return error_mark_node
;
5467 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1
)))
5469 if (complain
& tf_error
)
5470 error_at (loc
, "arithmetic on pointer to an empty aggregate");
5472 return error_mark_node
;
5475 op1
= (TYPE_PTROB_P (ptrtype
)
5476 ? size_in_bytes_loc (loc
, target_type
)
5477 : integer_one_node
);
5479 /* Do the division. */
5481 result
= build2_loc (loc
, EXACT_DIV_EXPR
, inttype
, op0
,
5482 cp_convert (inttype
, op1
, complain
));
5483 return cp_convert (restype
, result
, complain
);
5486 /* Construct and perhaps optimize a tree representation
5487 for a unary operation. CODE, a tree_code, specifies the operation
5488 and XARG is the operand. */
5491 build_x_unary_op (location_t loc
, enum tree_code code
, cp_expr xarg
,
5492 tsubst_flags_t complain
)
5494 tree orig_expr
= xarg
;
5497 tree overload
= NULL_TREE
;
5499 if (processing_template_decl
)
5501 if (type_dependent_expression_p (xarg
))
5502 return build_min_nt_loc (loc
, code
, xarg
.get_value (), NULL_TREE
);
5504 xarg
= build_non_dependent_expr (xarg
);
5509 /* [expr.unary.op] says:
5511 The address of an object of incomplete type can be taken.
5513 (And is just the ordinary address operator, not an overloaded
5514 "operator &".) However, if the type is a template
5515 specialization, we must complete the type at this point so that
5516 an overloaded "operator &" will be available if required. */
5517 if (code
== ADDR_EXPR
5518 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
5519 && ((CLASS_TYPE_P (TREE_TYPE (xarg
))
5520 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg
))))
5521 || (TREE_CODE (xarg
) == OFFSET_REF
)))
5522 /* Don't look for a function. */;
5524 exp
= build_new_op (loc
, code
, LOOKUP_NORMAL
, xarg
, NULL_TREE
,
5525 NULL_TREE
, &overload
, complain
);
5527 if (!exp
&& code
== ADDR_EXPR
)
5529 if (is_overloaded_fn (xarg
))
5531 tree fn
= get_first_fn (xarg
);
5532 if (DECL_CONSTRUCTOR_P (fn
) || DECL_DESTRUCTOR_P (fn
))
5534 if (complain
& tf_error
)
5535 error (DECL_CONSTRUCTOR_P (fn
)
5536 ? G_("taking address of constructor %qD")
5537 : G_("taking address of destructor %qD"),
5539 return error_mark_node
;
5543 /* A pointer to member-function can be formed only by saying
5545 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
5546 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
5548 if (TREE_CODE (xarg
) != OFFSET_REF
5549 || !TYPE_P (TREE_OPERAND (xarg
, 0)))
5551 if (complain
& tf_error
)
5553 error ("invalid use of %qE to form a "
5554 "pointer-to-member-function", xarg
.get_value ());
5555 if (TREE_CODE (xarg
) != OFFSET_REF
)
5556 inform (input_location
, " a qualified-id is required");
5558 return error_mark_node
;
5562 if (complain
& tf_error
)
5563 error ("parentheses around %qE cannot be used to form a"
5564 " pointer-to-member-function",
5567 return error_mark_node
;
5568 PTRMEM_OK_P (xarg
) = 1;
5572 if (TREE_CODE (xarg
) == OFFSET_REF
)
5574 ptrmem
= PTRMEM_OK_P (xarg
);
5576 if (!ptrmem
&& !flag_ms_extensions
5577 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
5579 /* A single non-static member, make sure we don't allow a
5580 pointer-to-member. */
5581 xarg
= build2 (OFFSET_REF
, TREE_TYPE (xarg
),
5582 TREE_OPERAND (xarg
, 0),
5583 ovl_make (TREE_OPERAND (xarg
, 1)));
5584 PTRMEM_OK_P (xarg
) = ptrmem
;
5588 exp
= cp_build_addr_expr_strict (xarg
, complain
);
5591 if (processing_template_decl
&& exp
!= error_mark_node
)
5593 if (overload
!= NULL_TREE
)
5594 return (build_min_non_dep_op_overload
5595 (code
, exp
, overload
, orig_expr
, integer_zero_node
));
5597 exp
= build_min_non_dep (code
, exp
, orig_expr
,
5598 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE
);
5600 if (TREE_CODE (exp
) == ADDR_EXPR
)
5601 PTRMEM_OK_P (exp
) = ptrmem
;
5605 /* Construct and perhaps optimize a tree representation
5606 for __builtin_addressof operation. ARG specifies the operand. */
5609 cp_build_addressof (location_t loc
, tree arg
, tsubst_flags_t complain
)
5611 tree orig_expr
= arg
;
5613 if (processing_template_decl
)
5615 if (type_dependent_expression_p (arg
))
5616 return build_min_nt_loc (loc
, ADDRESSOF_EXPR
, arg
, NULL_TREE
);
5618 arg
= build_non_dependent_expr (arg
);
5621 tree exp
= cp_build_addr_expr_strict (arg
, complain
);
5623 if (processing_template_decl
&& exp
!= error_mark_node
)
5624 exp
= build_min_non_dep (ADDRESSOF_EXPR
, exp
, orig_expr
, NULL_TREE
);
5628 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5629 constants, where a null value is represented by an INTEGER_CST of
5633 cp_truthvalue_conversion (tree expr
)
5635 tree type
= TREE_TYPE (expr
);
5636 if (TYPE_PTR_OR_PTRMEM_P (type
)
5637 /* Avoid ICE on invalid use of non-static member function. */
5638 || TREE_CODE (expr
) == FUNCTION_DECL
)
5639 return build_binary_op (input_location
, NE_EXPR
, expr
, nullptr_node
, true);
5641 return c_common_truthvalue_conversion (input_location
, expr
);
5644 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5647 condition_conversion (tree expr
)
5650 /* Anything that might happen in a template should go through
5651 maybe_convert_cond. */
5652 gcc_assert (!processing_template_decl
);
5653 t
= perform_implicit_conversion_flags (boolean_type_node
, expr
,
5654 tf_warning_or_error
, LOOKUP_NORMAL
);
5655 t
= fold_build_cleanup_point_expr (boolean_type_node
, t
);
5659 /* Returns the address of T. This function will fold away
5660 ADDR_EXPR of INDIRECT_REF. */
5663 build_address (tree t
)
5665 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
5666 return error_mark_node
;
5667 gcc_checking_assert (TREE_CODE (t
) != CONSTRUCTOR
);
5668 t
= build_fold_addr_expr (t
);
5669 if (TREE_CODE (t
) != ADDR_EXPR
)
5674 /* Return a NOP_EXPR converting EXPR to TYPE. */
5677 build_nop (tree type
, tree expr
)
5679 if (type
== error_mark_node
|| error_operand_p (expr
))
5681 return build1_loc (EXPR_LOCATION (expr
), NOP_EXPR
, type
, expr
);
5684 /* Take the address of ARG, whatever that means under C++ semantics.
5685 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5686 and class rvalues as well.
5688 Nothing should call this function directly; instead, callers should use
5689 cp_build_addr_expr or cp_build_addr_expr_strict. */
5692 cp_build_addr_expr_1 (tree arg
, bool strict_lvalue
, tsubst_flags_t complain
)
5697 if (!arg
|| error_operand_p (arg
))
5698 return error_mark_node
;
5700 arg
= mark_lvalue_use (arg
);
5701 if (error_operand_p (arg
))
5702 return error_mark_node
;
5704 argtype
= lvalue_type (arg
);
5706 gcc_assert (!(identifier_p (arg
) && IDENTIFIER_ANY_OP_P (arg
)));
5708 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
5709 && !really_overloaded_fn (arg
))
5711 /* They're trying to take the address of a unique non-static
5712 member function. This is ill-formed (except in MS-land),
5713 but let's try to DTRT.
5714 Note: We only handle unique functions here because we don't
5715 want to complain if there's a static overload; non-unique
5716 cases will be handled by instantiate_type. But we need to
5717 handle this case here to allow casts on the resulting PMF.
5718 We could defer this in non-MS mode, but it's easier to give
5719 a useful error here. */
5721 /* Inside constant member functions, the `this' pointer
5722 contains an extra const qualifier. TYPE_MAIN_VARIANT
5723 is used here to remove this const from the diagnostics
5724 and the created OFFSET_REF. */
5725 tree base
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg
, 0)));
5726 tree fn
= get_first_fn (TREE_OPERAND (arg
, 1));
5727 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
5728 return error_mark_node
;
5730 if (! flag_ms_extensions
)
5732 tree name
= DECL_NAME (fn
);
5733 if (!(complain
& tf_error
))
5734 return error_mark_node
;
5735 else if (current_class_type
5736 && TREE_OPERAND (arg
, 0) == current_class_ref
)
5737 /* An expression like &memfn. */
5738 permerror (input_location
, "ISO C++ forbids taking the address of an unqualified"
5739 " or parenthesized non-static member function to form"
5740 " a pointer to member function. Say %<&%T::%D%>",
5743 permerror (input_location
, "ISO C++ forbids taking the address of a bound member"
5744 " function to form a pointer to member function."
5748 arg
= build_offset_ref (base
, fn
, /*address_p=*/true, complain
);
5751 /* Uninstantiated types are all functions. Taking the
5752 address of a function is a no-op, so just return the
5754 if (type_unknown_p (arg
))
5755 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
5757 if (TREE_CODE (arg
) == OFFSET_REF
)
5758 /* We want a pointer to member; bypass all the code for actually taking
5759 the address of something. */
5762 /* Anything not already handled and not a true memory reference
5764 if (TREE_CODE (argtype
) != FUNCTION_TYPE
5765 && TREE_CODE (argtype
) != METHOD_TYPE
)
5767 cp_lvalue_kind kind
= lvalue_kind (arg
);
5768 if (kind
== clk_none
)
5770 if (complain
& tf_error
)
5771 lvalue_error (input_location
, lv_addressof
);
5772 return error_mark_node
;
5774 if (strict_lvalue
&& (kind
& (clk_rvalueref
|clk_class
)))
5776 if (!(complain
& tf_error
))
5777 return error_mark_node
;
5778 if (kind
& clk_class
)
5779 /* Make this a permerror because we used to accept it. */
5780 permerror (input_location
, "taking address of temporary");
5782 error ("taking address of xvalue (rvalue reference)");
5786 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
5788 tree type
= build_pointer_type (TREE_TYPE (argtype
));
5789 arg
= build1 (CONVERT_EXPR
, type
, arg
);
5792 else if (pedantic
&& DECL_MAIN_P (arg
))
5795 /* Apparently a lot of autoconf scripts for C++ packages do this,
5796 so only complain if -Wpedantic. */
5797 if (complain
& (flag_pedantic_errors
? tf_error
: tf_warning
))
5798 pedwarn (input_location
, OPT_Wpedantic
,
5799 "ISO C++ forbids taking address of function %<::main%>");
5800 else if (flag_pedantic_errors
)
5801 return error_mark_node
;
5804 /* Let &* cancel out to simplify resulting code. */
5805 if (INDIRECT_REF_P (arg
))
5807 arg
= TREE_OPERAND (arg
, 0);
5808 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
5810 tree type
= build_pointer_type (TREE_TYPE (TREE_TYPE (arg
)));
5811 arg
= build1 (CONVERT_EXPR
, type
, arg
);
5814 /* Don't let this be an lvalue. */
5819 /* ??? Cope with user tricks that amount to offsetof. */
5820 if (TREE_CODE (argtype
) != FUNCTION_TYPE
5821 && TREE_CODE (argtype
) != METHOD_TYPE
5822 && argtype
!= unknown_type_node
5823 && (val
= get_base_address (arg
))
5824 && COMPLETE_TYPE_P (TREE_TYPE (val
))
5825 && INDIRECT_REF_P (val
)
5826 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
5828 tree type
= build_pointer_type (argtype
);
5829 return fold_convert (type
, fold_offsetof_1 (arg
));
5832 /* Handle complex lvalues (when permitted)
5833 by reduction to simpler cases. */
5834 val
= unary_complex_lvalue (ADDR_EXPR
, arg
);
5838 switch (TREE_CODE (arg
))
5842 case FIX_TRUNC_EXPR
:
5843 /* We should have handled this above in the lvalue_kind check. */
5848 arg
= BASELINK_FUNCTIONS (arg
);
5852 arg
= OVL_FIRST (arg
);
5857 /* Turn a reference to a non-static data member into a
5858 pointer-to-member. */
5863 gcc_assert (PTRMEM_OK_P (arg
));
5865 t
= TREE_OPERAND (arg
, 1);
5866 if (TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
5868 if (complain
& tf_error
)
5869 error ("cannot create pointer to reference member %qD", t
);
5870 return error_mark_node
;
5873 type
= build_ptrmem_type (context_for_name_lookup (t
),
5875 t
= make_ptrmem_cst (type
, TREE_OPERAND (arg
, 1));
5883 if (argtype
!= error_mark_node
)
5884 argtype
= build_pointer_type (argtype
);
5886 if (bitfield_p (arg
))
5888 if (complain
& tf_error
)
5889 error ("attempt to take address of bit-field");
5890 return error_mark_node
;
5893 /* In a template, we are processing a non-dependent expression
5894 so we can just form an ADDR_EXPR with the correct type. */
5895 if (processing_template_decl
|| TREE_CODE (arg
) != COMPONENT_REF
)
5897 val
= build_address (arg
);
5898 if (TREE_CODE (arg
) == OFFSET_REF
)
5899 PTRMEM_OK_P (val
) = PTRMEM_OK_P (arg
);
5901 else if (BASELINK_P (TREE_OPERAND (arg
, 1)))
5903 tree fn
= BASELINK_FUNCTIONS (TREE_OPERAND (arg
, 1));
5905 /* We can only get here with a single static member
5907 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
5908 && DECL_STATIC_FUNCTION_P (fn
));
5909 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
5910 return error_mark_node
;
5911 val
= build_address (fn
);
5912 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
5913 /* Do not lose object's side effects. */
5914 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
),
5915 TREE_OPERAND (arg
, 0), val
);
5919 tree object
= TREE_OPERAND (arg
, 0);
5920 tree field
= TREE_OPERAND (arg
, 1);
5921 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5922 (TREE_TYPE (object
), decl_type_context (field
)));
5923 val
= build_address (arg
);
5926 if (TYPE_PTR_P (argtype
)
5927 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
5929 build_ptrmemfunc_type (argtype
);
5930 val
= build_ptrmemfunc (argtype
, val
, 0,
5938 /* Take the address of ARG if it has one, even if it's an rvalue. */
5941 cp_build_addr_expr (tree arg
, tsubst_flags_t complain
)
5943 return cp_build_addr_expr_1 (arg
, 0, complain
);
5946 /* Take the address of ARG, but only if it's an lvalue. */
5949 cp_build_addr_expr_strict (tree arg
, tsubst_flags_t complain
)
5951 return cp_build_addr_expr_1 (arg
, 1, complain
);
5954 /* C++: Must handle pointers to members.
5956 Perhaps type instantiation should be extended to handle conversion
5957 from aggregates to types we don't yet know we want? (Or are those
5958 cases typically errors which should be reported?)
5960 NOCONVERT suppresses the default promotions (such as from short to int). */
5963 cp_build_unary_op (enum tree_code code
, tree xarg
, bool noconvert
,
5964 tsubst_flags_t complain
)
5966 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5968 location_t location
= EXPR_LOC_OR_LOC (arg
, input_location
);
5970 const char *errstring
= NULL
;
5972 const char *invalid_op_diag
;
5974 if (!arg
|| error_operand_p (arg
))
5975 return error_mark_node
;
5977 if ((invalid_op_diag
5978 = targetm
.invalid_unary_op ((code
== UNARY_PLUS_EXPR
5983 if (complain
& tf_error
)
5984 error (invalid_op_diag
);
5985 return error_mark_node
;
5990 case UNARY_PLUS_EXPR
:
5993 int flags
= WANT_ARITH
| WANT_ENUM
;
5994 /* Unary plus (but not unary minus) is allowed on pointers. */
5995 if (code
== UNARY_PLUS_EXPR
)
5996 flags
|= WANT_POINTER
;
5997 arg
= build_expr_type_conversion (flags
, arg
, true);
5999 errstring
= (code
== NEGATE_EXPR
6000 ? _("wrong type argument to unary minus")
6001 : _("wrong type argument to unary plus"));
6004 if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
6005 arg
= cp_perform_integral_promotions (arg
, complain
);
6007 /* Make sure the result is not an lvalue: a unary plus or minus
6008 expression is always a rvalue. */
6015 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
6020 arg
= cp_default_conversion (arg
, complain
);
6021 if (arg
== error_mark_node
)
6022 return error_mark_node
;
6025 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
6026 | WANT_VECTOR_OR_COMPLEX
,
6028 errstring
= _("wrong type argument to bit-complement");
6029 else if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
6031 /* Warn if the expression has boolean value. */
6032 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
6033 && (complain
& tf_warning
)
6034 && warning_at (location
, OPT_Wbool_operation
,
6035 "%<~%> on an expression of type bool"))
6036 inform (location
, "did you mean to use logical not (%<!%>)?");
6037 arg
= cp_perform_integral_promotions (arg
, complain
);
6039 else if (!noconvert
&& VECTOR_TYPE_P (TREE_TYPE (arg
)))
6040 arg
= mark_rvalue_use (arg
);
6044 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
6045 errstring
= _("wrong type argument to abs");
6046 else if (!noconvert
)
6048 arg
= cp_default_conversion (arg
, complain
);
6049 if (arg
== error_mark_node
)
6050 return error_mark_node
;
6055 /* Conjugating a real value is a no-op, but allow it anyway. */
6056 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
6057 errstring
= _("wrong type argument to conjugation");
6058 else if (!noconvert
)
6060 arg
= cp_default_conversion (arg
, complain
);
6061 if (arg
== error_mark_node
)
6062 return error_mark_node
;
6066 case TRUTH_NOT_EXPR
:
6067 if (VECTOR_TYPE_P (TREE_TYPE (arg
)))
6068 return cp_build_binary_op (input_location
, EQ_EXPR
, arg
,
6069 build_zero_cst (TREE_TYPE (arg
)), complain
);
6070 arg
= perform_implicit_conversion (boolean_type_node
, arg
,
6072 val
= invert_truthvalue_loc (input_location
, arg
);
6073 if (arg
!= error_mark_node
)
6075 errstring
= _("in argument to unary !");
6083 arg
= build_real_imag_expr (input_location
, code
, arg
);
6086 case PREINCREMENT_EXPR
:
6087 case POSTINCREMENT_EXPR
:
6088 case PREDECREMENT_EXPR
:
6089 case POSTDECREMENT_EXPR
:
6090 /* Handle complex lvalues (when permitted)
6091 by reduction to simpler cases. */
6093 val
= unary_complex_lvalue (code
, arg
);
6097 arg
= mark_lvalue_use (arg
);
6099 /* Increment or decrement the real part of the value,
6100 and don't change the imaginary part. */
6101 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
6105 arg
= cp_stabilize_reference (arg
);
6106 real
= cp_build_unary_op (REALPART_EXPR
, arg
, true, complain
);
6107 imag
= cp_build_unary_op (IMAGPART_EXPR
, arg
, true, complain
);
6108 real
= cp_build_unary_op (code
, real
, true, complain
);
6109 if (real
== error_mark_node
|| imag
== error_mark_node
)
6110 return error_mark_node
;
6111 return build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
6115 /* Report invalid types. */
6117 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
6120 if (code
== PREINCREMENT_EXPR
)
6121 errstring
= _("no pre-increment operator for type");
6122 else if (code
== POSTINCREMENT_EXPR
)
6123 errstring
= _("no post-increment operator for type");
6124 else if (code
== PREDECREMENT_EXPR
)
6125 errstring
= _("no pre-decrement operator for type");
6127 errstring
= _("no post-decrement operator for type");
6130 else if (arg
== error_mark_node
)
6131 return error_mark_node
;
6133 /* Report something read-only. */
6135 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
6136 || TREE_READONLY (arg
))
6138 if (complain
& tf_error
)
6139 cxx_readonly_error (arg
, ((code
== PREINCREMENT_EXPR
6140 || code
== POSTINCREMENT_EXPR
)
6141 ? lv_increment
: lv_decrement
));
6143 return error_mark_node
;
6148 tree declared_type
= unlowered_expr_type (arg
);
6150 argtype
= TREE_TYPE (arg
);
6152 /* ARM $5.2.5 last annotation says this should be forbidden. */
6153 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
6155 if (complain
& tf_error
)
6156 permerror (input_location
, (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
6157 ? G_("ISO C++ forbids incrementing an enum")
6158 : G_("ISO C++ forbids decrementing an enum"));
6160 return error_mark_node
;
6163 /* Compute the increment. */
6165 if (TYPE_PTR_P (argtype
))
6167 tree type
= complete_type (TREE_TYPE (argtype
));
6169 if (!COMPLETE_OR_VOID_TYPE_P (type
))
6171 if (complain
& tf_error
)
6172 error (((code
== PREINCREMENT_EXPR
6173 || code
== POSTINCREMENT_EXPR
))
6174 ? G_("cannot increment a pointer to incomplete type %qT")
6175 : G_("cannot decrement a pointer to incomplete type %qT"),
6176 TREE_TYPE (argtype
));
6178 return error_mark_node
;
6180 else if (!TYPE_PTROB_P (argtype
))
6182 if (complain
& tf_error
)
6183 pedwarn (input_location
, OPT_Wpointer_arith
,
6184 (code
== PREINCREMENT_EXPR
6185 || code
== POSTINCREMENT_EXPR
)
6186 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
6187 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6190 return error_mark_node
;
6193 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
6196 inc
= VECTOR_TYPE_P (argtype
)
6197 ? build_one_cst (argtype
)
6200 inc
= cp_convert (argtype
, inc
, complain
);
6202 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6203 need to ask Objective-C to build the increment or decrement
6204 expression for it. */
6205 if (objc_is_property_ref (arg
))
6206 return objc_build_incr_expr_for_property_ref (input_location
, code
,
6209 /* Complain about anything else that is not a true lvalue. */
6210 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
6211 || code
== POSTINCREMENT_EXPR
)
6212 ? lv_increment
: lv_decrement
),
6214 return error_mark_node
;
6216 /* Forbid using -- or ++ in C++17 on `bool'. */
6217 if (TREE_CODE (declared_type
) == BOOLEAN_TYPE
)
6219 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
6221 if (complain
& tf_error
)
6222 error ("use of an operand of type %qT in %<operator--%> "
6223 "is forbidden", boolean_type_node
);
6224 return error_mark_node
;
6228 if (cxx_dialect
>= cxx17
)
6230 if (complain
& tf_error
)
6231 error ("use of an operand of type %qT in "
6232 "%<operator++%> is forbidden in C++17",
6234 return error_mark_node
;
6236 /* Otherwise, [depr.incr.bool] says this is deprecated. */
6237 else if (!in_system_header_at (input_location
))
6238 warning (OPT_Wdeprecated
, "use of an operand of type %qT "
6239 "in %<operator++%> is deprecated",
6242 val
= boolean_increment (code
, arg
);
6244 else if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
6245 /* An rvalue has no cv-qualifiers. */
6246 val
= build2 (code
, cv_unqualified (TREE_TYPE (arg
)), arg
, inc
);
6248 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
6250 TREE_SIDE_EFFECTS (val
) = 1;
6255 /* Note that this operation never does default_conversion
6256 regardless of NOCONVERT. */
6257 return cp_build_addr_expr (arg
, complain
);
6266 argtype
= TREE_TYPE (arg
);
6267 return build1 (code
, argtype
, arg
);
6270 if (complain
& tf_error
)
6271 error ("%s", errstring
);
6272 return error_mark_node
;
6275 /* Hook for the c-common bits that build a unary op. */
6277 build_unary_op (location_t
/*location*/,
6278 enum tree_code code
, tree xarg
, bool noconvert
)
6280 return cp_build_unary_op (code
, xarg
, noconvert
, tf_warning_or_error
);
6283 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6284 for certain kinds of expressions which are not really lvalues
6285 but which we can accept as lvalues.
6287 If ARG is not a kind of expression we can handle, return
6291 unary_complex_lvalue (enum tree_code code
, tree arg
)
6293 /* Inside a template, making these kinds of adjustments is
6294 pointless; we are only concerned with the type of the
6296 if (processing_template_decl
)
6299 /* Handle (a, b) used as an "lvalue". */
6300 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
6302 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 1), false,
6303 tf_warning_or_error
);
6304 return build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6305 TREE_OPERAND (arg
, 0), real_result
);
6308 /* Handle (a ? b : c) used as an "lvalue". */
6309 if (TREE_CODE (arg
) == COND_EXPR
6310 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
6311 return rationalize_conditional_expr (code
, arg
, tf_warning_or_error
);
6313 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6314 if (TREE_CODE (arg
) == MODIFY_EXPR
6315 || TREE_CODE (arg
) == PREINCREMENT_EXPR
6316 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
6318 tree lvalue
= TREE_OPERAND (arg
, 0);
6319 if (TREE_SIDE_EFFECTS (lvalue
))
6321 lvalue
= cp_stabilize_reference (lvalue
);
6322 arg
= build2 (TREE_CODE (arg
), TREE_TYPE (arg
),
6323 lvalue
, TREE_OPERAND (arg
, 1));
6325 return unary_complex_lvalue
6326 (code
, build2 (COMPOUND_EXPR
, TREE_TYPE (lvalue
), arg
, lvalue
));
6329 if (code
!= ADDR_EXPR
)
6332 /* Handle (a = b) used as an "lvalue" for `&'. */
6333 if (TREE_CODE (arg
) == MODIFY_EXPR
6334 || TREE_CODE (arg
) == INIT_EXPR
)
6336 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 0), false,
6337 tf_warning_or_error
);
6338 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
6340 TREE_NO_WARNING (arg
) = 1;
6344 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
6345 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
6346 || TREE_CODE (arg
) == OFFSET_REF
)
6349 /* We permit compiler to make function calls returning
6350 objects of aggregate type look like lvalues. */
6354 if (TREE_CODE (targ
) == SAVE_EXPR
)
6355 targ
= TREE_OPERAND (targ
, 0);
6357 if (TREE_CODE (targ
) == CALL_EXPR
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (targ
)))
6359 if (TREE_CODE (arg
) == SAVE_EXPR
)
6362 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, tf_warning_or_error
);
6363 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
6366 if (TREE_CODE (arg
) == SAVE_EXPR
&& INDIRECT_REF_P (targ
))
6367 return build3 (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
6368 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
6371 /* Don't let anything else be handled specially. */
6375 /* Mark EXP saying that we need to be able to take the
6376 address of it; it should not be allocated in a register.
6377 Value is true if successful. ARRAY_REF_P is true if this
6378 is for ARRAY_REF construction - in that case we don't want
6379 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
6380 it is fine to use ARRAY_REFs for vector subscripts on vector
6383 C++: we do not allow `current_class_ptr' to be addressable. */
6386 cxx_mark_addressable (tree exp
, bool array_ref_p
)
6391 switch (TREE_CODE (x
))
6393 case VIEW_CONVERT_EXPR
:
6395 && TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
6396 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x
, 0))))
6404 x
= TREE_OPERAND (x
, 0);
6408 if (x
== current_class_ptr
)
6410 error ("cannot take the address of %<this%>, which is an rvalue expression");
6411 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later. */
6417 /* Caller should not be trying to mark initialized
6418 constant fields addressable. */
6419 gcc_assert (DECL_LANG_SPECIFIC (x
) == 0
6420 || DECL_IN_AGGR_P (x
) == 0
6422 || DECL_EXTERNAL (x
));
6426 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
6427 && !DECL_ARTIFICIAL (x
))
6429 if (VAR_P (x
) && DECL_HARD_REGISTER (x
))
6432 ("address of explicit register variable %qD requested", x
);
6435 else if (extra_warnings
)
6437 (OPT_Wextra
, "address requested for %qD, which is declared %<register%>", x
);
6439 TREE_ADDRESSABLE (x
) = 1;
6444 TREE_ADDRESSABLE (x
) = 1;
6448 TREE_ADDRESSABLE (x
) = 1;
6452 TREE_ADDRESSABLE (x
) = 1;
6453 cxx_mark_addressable (TREE_OPERAND (x
, 0));
6461 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6464 build_x_conditional_expr (location_t loc
, tree ifexp
, tree op1
, tree op2
,
6465 tsubst_flags_t complain
)
6467 tree orig_ifexp
= ifexp
;
6468 tree orig_op1
= op1
;
6469 tree orig_op2
= op2
;
6472 if (processing_template_decl
)
6474 /* The standard says that the expression is type-dependent if
6475 IFEXP is type-dependent, even though the eventual type of the
6476 expression doesn't dependent on IFEXP. */
6477 if (type_dependent_expression_p (ifexp
)
6478 /* As a GNU extension, the middle operand may be omitted. */
6479 || (op1
&& type_dependent_expression_p (op1
))
6480 || type_dependent_expression_p (op2
))
6481 return build_min_nt_loc (loc
, COND_EXPR
, ifexp
, op1
, op2
);
6482 ifexp
= build_non_dependent_expr (ifexp
);
6484 op1
= build_non_dependent_expr (op1
);
6485 op2
= build_non_dependent_expr (op2
);
6488 expr
= build_conditional_expr (loc
, ifexp
, op1
, op2
, complain
);
6489 if (processing_template_decl
&& expr
!= error_mark_node
)
6491 tree min
= build_min_non_dep (COND_EXPR
, expr
,
6492 orig_ifexp
, orig_op1
, orig_op2
);
6493 /* Remember that the result is an lvalue or xvalue. */
6494 if (glvalue_p (expr
) && !glvalue_p (min
))
6495 TREE_TYPE (min
) = cp_build_reference_type (TREE_TYPE (min
),
6497 expr
= convert_from_reference (min
);
6502 /* Given a list of expressions, return a compound expression
6503 that performs them all and returns the value of the last of them. */
6506 build_x_compound_expr_from_list (tree list
, expr_list_kind exp
,
6507 tsubst_flags_t complain
)
6509 tree expr
= TREE_VALUE (list
);
6511 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
6512 && !CONSTRUCTOR_IS_DIRECT_INIT (expr
))
6514 if (complain
& tf_error
)
6515 pedwarn (EXPR_LOC_OR_LOC (expr
, input_location
), 0,
6516 "list-initializer for non-class type must not "
6517 "be parenthesized");
6519 return error_mark_node
;
6522 if (TREE_CHAIN (list
))
6524 if (complain
& tf_error
)
6528 permerror (input_location
, "expression list treated as compound "
6529 "expression in initializer");
6532 permerror (input_location
, "expression list treated as compound "
6533 "expression in mem-initializer");
6536 permerror (input_location
, "expression list treated as compound "
6537 "expression in functional cast");
6543 return error_mark_node
;
6545 for (list
= TREE_CHAIN (list
); list
; list
= TREE_CHAIN (list
))
6546 expr
= build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list
)),
6547 expr
, TREE_VALUE (list
), complain
);
6553 /* Like build_x_compound_expr_from_list, but using a VEC. */
6556 build_x_compound_expr_from_vec (vec
<tree
, va_gc
> *vec
, const char *msg
,
6557 tsubst_flags_t complain
)
6559 if (vec_safe_is_empty (vec
))
6561 else if (vec
->length () == 1)
6571 if (complain
& tf_error
)
6572 permerror (input_location
,
6573 "%s expression list treated as compound expression",
6576 return error_mark_node
;
6580 for (ix
= 1; vec
->iterate (ix
, &t
); ++ix
)
6581 expr
= build_x_compound_expr (EXPR_LOCATION (t
), expr
,
6588 /* Handle overloading of the ',' operator when needed. */
6591 build_x_compound_expr (location_t loc
, tree op1
, tree op2
,
6592 tsubst_flags_t complain
)
6595 tree orig_op1
= op1
;
6596 tree orig_op2
= op2
;
6597 tree overload
= NULL_TREE
;
6599 if (processing_template_decl
)
6601 if (type_dependent_expression_p (op1
)
6602 || type_dependent_expression_p (op2
))
6603 return build_min_nt_loc (loc
, COMPOUND_EXPR
, op1
, op2
);
6604 op1
= build_non_dependent_expr (op1
);
6605 op2
= build_non_dependent_expr (op2
);
6608 result
= build_new_op (loc
, COMPOUND_EXPR
, LOOKUP_NORMAL
, op1
, op2
,
6609 NULL_TREE
, &overload
, complain
);
6611 result
= cp_build_compound_expr (op1
, op2
, complain
);
6613 if (processing_template_decl
&& result
!= error_mark_node
)
6615 if (overload
!= NULL_TREE
)
6616 return (build_min_non_dep_op_overload
6617 (COMPOUND_EXPR
, result
, overload
, orig_op1
, orig_op2
));
6619 return build_min_non_dep (COMPOUND_EXPR
, result
, orig_op1
, orig_op2
);
6625 /* Like cp_build_compound_expr, but for the c-common bits. */
6628 build_compound_expr (location_t
/*loc*/, tree lhs
, tree rhs
)
6630 return cp_build_compound_expr (lhs
, rhs
, tf_warning_or_error
);
6633 /* Build a compound expression. */
6636 cp_build_compound_expr (tree lhs
, tree rhs
, tsubst_flags_t complain
)
6638 lhs
= convert_to_void (lhs
, ICV_LEFT_OF_COMMA
, complain
);
6640 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
6641 return error_mark_node
;
6644 && (TREE_CODE (lhs
) == CILK_SPAWN_STMT
6645 || TREE_CODE (rhs
) == CILK_SPAWN_STMT
))
6647 location_t loc
= (EXPR_HAS_LOCATION (lhs
) ? EXPR_LOCATION (lhs
)
6648 : EXPR_LOCATION (rhs
));
6650 "spawned function call cannot be part of a comma expression");
6651 return error_mark_node
;
6654 if (TREE_CODE (rhs
) == TARGET_EXPR
)
6656 /* If the rhs is a TARGET_EXPR, then build the compound
6657 expression inside the target_expr's initializer. This
6658 helps the compiler to eliminate unnecessary temporaries. */
6659 tree init
= TREE_OPERAND (rhs
, 1);
6661 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), lhs
, init
);
6662 TREE_OPERAND (rhs
, 1) = init
;
6667 if (type_unknown_p (rhs
))
6669 if (complain
& tf_error
)
6670 error ("no context to resolve type of %qE", rhs
);
6671 return error_mark_node
;
6674 return build2 (COMPOUND_EXPR
, TREE_TYPE (rhs
), lhs
, rhs
);
6677 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6678 casts away constness. CAST gives the type of cast. Returns true
6679 if the cast is ill-formed, false if it is well-formed.
6681 ??? This function warns for casting away any qualifier not just
6682 const. We would like to specify exactly what qualifiers are casted
6687 check_for_casting_away_constness (tree src_type
, tree dest_type
,
6688 enum tree_code cast
, tsubst_flags_t complain
)
6690 /* C-style casts are allowed to cast away constness. With
6691 WARN_CAST_QUAL, we still want to issue a warning. */
6692 if (cast
== CAST_EXPR
&& !warn_cast_qual
)
6695 if (!casts_away_constness (src_type
, dest_type
, complain
))
6701 if (complain
& tf_warning
)
6702 warning (OPT_Wcast_qual
,
6703 "cast from type %qT to type %qT casts away qualifiers",
6704 src_type
, dest_type
);
6707 case STATIC_CAST_EXPR
:
6708 if (complain
& tf_error
)
6709 error ("static_cast from type %qT to type %qT casts away qualifiers",
6710 src_type
, dest_type
);
6713 case REINTERPRET_CAST_EXPR
:
6714 if (complain
& tf_error
)
6715 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6716 src_type
, dest_type
);
6724 /* Warns if the cast from expression EXPR to type TYPE is useless. */
6726 maybe_warn_about_useless_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6728 if (warn_useless_cast
6729 && complain
& tf_warning
)
6731 if ((TREE_CODE (type
) == REFERENCE_TYPE
6732 && (TYPE_REF_IS_RVALUE (type
)
6733 ? xvalue_p (expr
) : lvalue_p (expr
))
6734 && same_type_p (TREE_TYPE (expr
), TREE_TYPE (type
)))
6735 || same_type_p (TREE_TYPE (expr
), type
))
6736 warning (OPT_Wuseless_cast
, "useless cast to type %q#T", type
);
6740 /* Warns if the cast ignores cv-qualifiers on TYPE. */
6742 maybe_warn_about_cast_ignoring_quals (tree type
, tsubst_flags_t complain
)
6744 if (warn_ignored_qualifiers
6745 && complain
& tf_warning
6746 && !CLASS_TYPE_P (type
)
6747 && (cp_type_quals (type
) & (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)))
6749 warning (OPT_Wignored_qualifiers
, "type qualifiers ignored on cast "
6754 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6755 (another pointer-to-member type in the same hierarchy) and return
6756 the converted expression. If ALLOW_INVERSE_P is permitted, a
6757 pointer-to-derived may be converted to pointer-to-base; otherwise,
6758 only the other direction is permitted. If C_CAST_P is true, this
6759 conversion is taking place as part of a C-style cast. */
6762 convert_ptrmem (tree type
, tree expr
, bool allow_inverse_p
,
6763 bool c_cast_p
, tsubst_flags_t complain
)
6765 if (same_type_p (type
, TREE_TYPE (expr
)))
6768 if (TYPE_PTRDATAMEM_P (type
))
6772 delta
= get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr
)),
6773 TYPE_PTRMEM_CLASS_TYPE (type
),
6775 c_cast_p
, complain
);
6776 if (delta
== error_mark_node
)
6777 return error_mark_node
;
6779 if (!integer_zerop (delta
))
6781 tree cond
, op1
, op2
;
6783 if (TREE_CODE (expr
) == PTRMEM_CST
)
6784 expr
= cplus_expand_constant (expr
);
6785 cond
= cp_build_binary_op (input_location
,
6788 build_int_cst (TREE_TYPE (expr
), -1),
6790 op1
= build_nop (ptrdiff_type_node
, expr
);
6791 op2
= cp_build_binary_op (input_location
,
6792 PLUS_EXPR
, op1
, delta
,
6795 expr
= fold_build3_loc (input_location
,
6796 COND_EXPR
, ptrdiff_type_node
, cond
, op1
, op2
);
6800 return build_nop (type
, expr
);
6803 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
,
6804 allow_inverse_p
, c_cast_p
, complain
);
6807 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6808 this static_cast is being attempted as one of the possible casts
6809 allowed by a C-style cast. (In that case, accessibility of base
6810 classes is not considered, and it is OK to cast away
6811 constness.) Return the result of the cast. *VALID_P is set to
6812 indicate whether or not the cast was valid. */
6815 build_static_cast_1 (tree type
, tree expr
, bool c_cast_p
,
6816 bool *valid_p
, tsubst_flags_t complain
)
6822 /* Assume the cast is valid. */
6825 intype
= unlowered_expr_type (expr
);
6827 /* Save casted types in the function's used types hash table. */
6828 used_types_insert (type
);
6830 /* A prvalue of non-class type is cv-unqualified. */
6831 if (!CLASS_TYPE_P (type
))
6832 type
= cv_unqualified (type
);
6834 /* [expr.static.cast]
6836 An lvalue of type "cv1 B", where B is a class type, can be cast
6837 to type "reference to cv2 D", where D is a class derived (clause
6838 _class.derived_) from B, if a valid standard conversion from
6839 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6840 same cv-qualification as, or greater cv-qualification than, cv1,
6841 and B is not a virtual base class of D. */
6842 /* We check this case before checking the validity of "TYPE t =
6843 EXPR;" below because for this case:
6846 struct D : public B { D(const B&); };
6848 void f() { static_cast<const D&>(b); }
6850 we want to avoid constructing a new D. The standard is not
6851 completely clear about this issue, but our interpretation is
6852 consistent with other compilers. */
6853 if (TREE_CODE (type
) == REFERENCE_TYPE
6854 && CLASS_TYPE_P (TREE_TYPE (type
))
6855 && CLASS_TYPE_P (intype
)
6856 && (TYPE_REF_IS_RVALUE (type
) || lvalue_p (expr
))
6857 && DERIVED_FROM_P (intype
, TREE_TYPE (type
))
6858 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype
)),
6859 build_pointer_type (TYPE_MAIN_VARIANT
6860 (TREE_TYPE (type
))),
6863 || at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
6867 if (processing_template_decl
)
6870 /* There is a standard conversion from "D*" to "B*" even if "B"
6871 is ambiguous or inaccessible. If this is really a
6872 static_cast, then we check both for inaccessibility and
6873 ambiguity. However, if this is a static_cast being performed
6874 because the user wrote a C-style cast, then accessibility is
6876 base
= lookup_base (TREE_TYPE (type
), intype
,
6877 c_cast_p
? ba_unique
: ba_check
,
6879 expr
= build_address (expr
);
6881 if (sanitize_flags_p (SANITIZE_VPTR
))
6884 = cp_ubsan_maybe_instrument_downcast (input_location
, type
,
6890 /* Convert from "B*" to "D*". This function will check that "B"
6891 is not a virtual base of "D". */
6892 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
6895 /* Convert the pointer to a reference -- but then remember that
6896 there are no expressions with reference type in C++.
6898 We call rvalue so that there's an actual tree code
6899 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6900 is a variable with the same type, the conversion would get folded
6901 away, leaving just the variable and causing lvalue_kind to give
6902 the wrong answer. */
6903 return convert_from_reference (rvalue (cp_fold_convert (type
, expr
)));
6906 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6907 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6908 if (TREE_CODE (type
) == REFERENCE_TYPE
6909 && TYPE_REF_IS_RVALUE (type
)
6910 && (clk
= real_lvalue_p (expr
))
6911 && reference_related_p (TREE_TYPE (type
), intype
)
6912 && (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
6914 if (processing_template_decl
)
6916 if (clk
== clk_ordinary
)
6918 /* Handle the (non-bit-field) lvalue case here by casting to
6919 lvalue reference and then changing it to an rvalue reference.
6920 Casting an xvalue to rvalue reference will be handled by the
6922 tree lref
= cp_build_reference_type (TREE_TYPE (type
), false);
6923 result
= (perform_direct_initialization_if_possible
6924 (lref
, expr
, c_cast_p
, complain
));
6925 result
= build1 (NON_LVALUE_EXPR
, type
, result
);
6926 return convert_from_reference (result
);
6929 /* For a bit-field or packed field, bind to a temporary. */
6930 expr
= rvalue (expr
);
6933 /* Resolve overloaded address here rather than once in
6934 implicit_conversion and again in the inverse code below. */
6935 if (TYPE_PTRMEMFUNC_P (type
) && type_unknown_p (expr
))
6937 expr
= instantiate_type (type
, expr
, complain
);
6938 intype
= TREE_TYPE (expr
);
6941 /* [expr.static.cast]
6943 Any expression can be explicitly converted to type cv void. */
6944 if (VOID_TYPE_P (type
))
6945 return convert_to_void (expr
, ICV_CAST
, complain
);
6948 An abstract class shall not be used ... as the type of an explicit
6950 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
6951 return error_mark_node
;
6953 /* [expr.static.cast]
6955 An expression e can be explicitly converted to a type T using a
6956 static_cast of the form static_cast<T>(e) if the declaration T
6957 t(e);" is well-formed, for some invented temporary variable
6959 result
= perform_direct_initialization_if_possible (type
, expr
,
6960 c_cast_p
, complain
);
6963 if (processing_template_decl
)
6966 result
= convert_from_reference (result
);
6968 /* [expr.static.cast]
6970 If T is a reference type, the result is an lvalue; otherwise,
6971 the result is an rvalue. */
6972 if (TREE_CODE (type
) != REFERENCE_TYPE
)
6974 result
= rvalue (result
);
6976 if (result
== expr
&& SCALAR_TYPE_P (type
))
6977 /* Leave some record of the cast. */
6978 result
= build_nop (type
, expr
);
6983 /* [expr.static.cast]
6985 The inverse of any standard conversion sequence (clause _conv_),
6986 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6987 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6988 (_conv.bool_) conversions, can be performed explicitly using
6989 static_cast subject to the restriction that the explicit
6990 conversion does not cast away constness (_expr.const.cast_), and
6991 the following additional rules for specific cases: */
6992 /* For reference, the conversions not excluded are: integral
6993 promotions, floating point promotion, integral conversions,
6994 floating point conversions, floating-integral conversions,
6995 pointer conversions, and pointer to member conversions. */
6998 A value of integral _or enumeration_ type can be explicitly
6999 converted to an enumeration type. */
7000 /* The effect of all that is that any conversion between any two
7001 types which are integral, floating, or enumeration types can be
7003 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7004 || SCALAR_FLOAT_TYPE_P (type
))
7005 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
7006 || SCALAR_FLOAT_TYPE_P (intype
)))
7008 if (processing_template_decl
)
7010 return ocp_convert (type
, expr
, CONV_C_CAST
, LOOKUP_NORMAL
, complain
);
7013 if (TYPE_PTR_P (type
) && TYPE_PTR_P (intype
)
7014 && CLASS_TYPE_P (TREE_TYPE (type
))
7015 && CLASS_TYPE_P (TREE_TYPE (intype
))
7016 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
7017 (TREE_TYPE (intype
))),
7018 build_pointer_type (TYPE_MAIN_VARIANT
7019 (TREE_TYPE (type
))),
7024 if (processing_template_decl
)
7028 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
7030 return error_mark_node
;
7031 base
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
7032 c_cast_p
? ba_unique
: ba_check
,
7034 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
7037 if (sanitize_flags_p (SANITIZE_VPTR
))
7040 = cp_ubsan_maybe_instrument_downcast (input_location
, type
,
7046 return cp_fold_convert (type
, expr
);
7049 if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
7050 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
7057 c1
= TYPE_PTRMEM_CLASS_TYPE (intype
);
7058 c2
= TYPE_PTRMEM_CLASS_TYPE (type
);
7060 if (TYPE_PTRDATAMEM_P (type
))
7062 t1
= (build_ptrmem_type
7064 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype
))));
7065 t2
= (build_ptrmem_type
7067 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
7074 if (can_convert (t1
, t2
, complain
) || can_convert (t2
, t1
, complain
))
7077 && check_for_casting_away_constness (intype
, type
,
7080 return error_mark_node
;
7081 if (processing_template_decl
)
7083 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/1,
7084 c_cast_p
, complain
);
7088 /* [expr.static.cast]
7090 An rvalue of type "pointer to cv void" can be explicitly
7091 converted to a pointer to object type. A value of type pointer
7092 to object converted to "pointer to cv void" and back to the
7093 original pointer type will have its original value. */
7094 if (TYPE_PTR_P (intype
)
7095 && VOID_TYPE_P (TREE_TYPE (intype
))
7096 && TYPE_PTROB_P (type
))
7099 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
7101 return error_mark_node
;
7102 if (processing_template_decl
)
7104 return build_nop (type
, expr
);
7108 return error_mark_node
;
7111 /* Return an expression representing static_cast<TYPE>(EXPR). */
7114 build_static_cast (tree type
, tree oexpr
, tsubst_flags_t complain
)
7120 if (type
== error_mark_node
|| expr
== error_mark_node
)
7121 return error_mark_node
;
7123 bool dependent
= (dependent_type_p (type
)
7124 || type_dependent_expression_p (expr
));
7128 expr
= build_min (STATIC_CAST_EXPR
, type
, oexpr
);
7129 /* We don't know if it will or will not have side effects. */
7130 TREE_SIDE_EFFECTS (expr
) = 1;
7131 return convert_from_reference (expr
);
7134 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7135 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7136 if (TREE_CODE (type
) != REFERENCE_TYPE
7137 && TREE_CODE (expr
) == NOP_EXPR
7138 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7139 expr
= TREE_OPERAND (expr
, 0);
7141 result
= build_static_cast_1 (type
, expr
, /*c_cast_p=*/false, &valid_p
,
7145 if (result
!= error_mark_node
)
7147 maybe_warn_about_useless_cast (type
, expr
, complain
);
7148 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7150 if (processing_template_decl
)
7155 if (complain
& tf_error
)
7156 error ("invalid static_cast from type %qT to type %qT",
7157 TREE_TYPE (expr
), type
);
7158 return error_mark_node
;
7161 /* EXPR is an expression with member function or pointer-to-member
7162 function type. TYPE is a pointer type. Converting EXPR to TYPE is
7163 not permitted by ISO C++, but we accept it in some modes. If we
7164 are not in one of those modes, issue a diagnostic. Return the
7165 converted expression. */
7168 convert_member_func_to_ptr (tree type
, tree expr
, tsubst_flags_t complain
)
7173 intype
= TREE_TYPE (expr
);
7174 gcc_assert (TYPE_PTRMEMFUNC_P (intype
)
7175 || TREE_CODE (intype
) == METHOD_TYPE
);
7177 if (!(complain
& tf_warning_or_error
))
7178 return error_mark_node
;
7180 if (pedantic
|| warn_pmf2ptr
)
7181 pedwarn (input_location
, pedantic
? OPT_Wpedantic
: OPT_Wpmf_conversions
,
7182 "converting from %qH to %qI", intype
, type
);
7184 if (TREE_CODE (intype
) == METHOD_TYPE
)
7185 expr
= build_addr_func (expr
, complain
);
7186 else if (TREE_CODE (expr
) == PTRMEM_CST
)
7187 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
7190 decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
), 0);
7191 decl
= build_address (decl
);
7192 expr
= get_member_function_from_ptrfunc (&decl
, expr
, complain
);
7195 if (expr
== error_mark_node
)
7196 return error_mark_node
;
7198 return build_nop (type
, expr
);
7201 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
7202 If C_CAST_P is true, this reinterpret cast is being done as part of
7203 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
7204 indicate whether or not reinterpret_cast was valid. */
7207 build_reinterpret_cast_1 (tree type
, tree expr
, bool c_cast_p
,
7208 bool *valid_p
, tsubst_flags_t complain
)
7212 /* Assume the cast is invalid. */
7216 if (type
== error_mark_node
|| error_operand_p (expr
))
7217 return error_mark_node
;
7219 intype
= TREE_TYPE (expr
);
7221 /* Save casted types in the function's used types hash table. */
7222 used_types_insert (type
);
7224 /* A prvalue of non-class type is cv-unqualified. */
7225 if (!CLASS_TYPE_P (type
))
7226 type
= cv_unqualified (type
);
7228 /* [expr.reinterpret.cast]
7229 An lvalue expression of type T1 can be cast to the type
7230 "reference to T2" if an expression of type "pointer to T1" can be
7231 explicitly converted to the type "pointer to T2" using a
7232 reinterpret_cast. */
7233 if (TREE_CODE (type
) == REFERENCE_TYPE
)
7235 if (! lvalue_p (expr
))
7237 if (complain
& tf_error
)
7238 error ("invalid cast of an rvalue expression of type "
7241 return error_mark_node
;
7244 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
7245 "B" are related class types; the reinterpret_cast does not
7246 adjust the pointer. */
7247 if (TYPE_PTR_P (intype
)
7248 && (complain
& tf_warning
)
7249 && (comptypes (TREE_TYPE (intype
), TREE_TYPE (type
),
7250 COMPARE_BASE
| COMPARE_DERIVED
)))
7251 warning (0, "casting %qT to %qT does not dereference pointer",
7254 expr
= cp_build_addr_expr (expr
, complain
);
7256 if (warn_strict_aliasing
> 2)
7257 strict_aliasing_warning (TREE_TYPE (expr
), type
, expr
);
7259 if (expr
!= error_mark_node
)
7260 expr
= build_reinterpret_cast_1
7261 (build_pointer_type (TREE_TYPE (type
)), expr
, c_cast_p
,
7263 if (expr
!= error_mark_node
)
7264 /* cp_build_indirect_ref isn't right for rvalue refs. */
7265 expr
= convert_from_reference (fold_convert (type
, expr
));
7269 /* As a G++ extension, we consider conversions from member
7270 functions, and pointers to member functions to
7271 pointer-to-function and pointer-to-void types. If
7272 -Wno-pmf-conversions has not been specified,
7273 convert_member_func_to_ptr will issue an error message. */
7274 if ((TYPE_PTRMEMFUNC_P (intype
)
7275 || TREE_CODE (intype
) == METHOD_TYPE
)
7276 && TYPE_PTR_P (type
)
7277 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
7278 || VOID_TYPE_P (TREE_TYPE (type
))))
7279 return convert_member_func_to_ptr (type
, expr
, complain
);
7281 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7282 array-to-pointer, and function-to-pointer conversions are
7284 expr
= decay_conversion (expr
, complain
);
7286 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7287 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7288 if (TREE_CODE (expr
) == NOP_EXPR
7289 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7290 expr
= TREE_OPERAND (expr
, 0);
7292 if (error_operand_p (expr
))
7293 return error_mark_node
;
7295 intype
= TREE_TYPE (expr
);
7297 /* [expr.reinterpret.cast]
7298 A pointer can be converted to any integral type large enough to
7299 hold it. ... A value of type std::nullptr_t can be converted to
7300 an integral type; the conversion has the same meaning and
7301 validity as a conversion of (void*)0 to the integral type. */
7302 if (CP_INTEGRAL_TYPE_P (type
)
7303 && (TYPE_PTR_P (intype
) || NULLPTR_TYPE_P (intype
)))
7305 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
7307 if (complain
& tf_error
)
7308 permerror (input_location
, "cast from %qH to %qI loses precision",
7311 return error_mark_node
;
7313 if (NULLPTR_TYPE_P (intype
))
7314 return build_int_cst (type
, 0);
7316 /* [expr.reinterpret.cast]
7317 A value of integral or enumeration type can be explicitly
7318 converted to a pointer. */
7319 else if (TYPE_PTR_P (type
) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype
))
7322 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
7323 || TYPE_PTR_OR_PTRMEM_P (type
))
7324 && same_type_p (type
, intype
))
7326 return rvalue (expr
);
7327 else if ((TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
7328 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
7329 return build_nop (type
, expr
);
7330 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
7331 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
7336 && check_for_casting_away_constness (intype
, type
,
7337 REINTERPRET_CAST_EXPR
,
7339 return error_mark_node
;
7340 /* Warn about possible alignment problems. */
7341 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
7342 && (complain
& tf_warning
)
7343 && !VOID_TYPE_P (type
)
7344 && TREE_CODE (TREE_TYPE (intype
)) != FUNCTION_TYPE
7345 && COMPLETE_TYPE_P (TREE_TYPE (type
))
7346 && COMPLETE_TYPE_P (TREE_TYPE (intype
))
7347 && min_align_of_type (TREE_TYPE (type
))
7348 > min_align_of_type (TREE_TYPE (intype
)))
7349 warning (OPT_Wcast_align
, "cast from %qH to %qI "
7350 "increases required alignment of target type", intype
, type
);
7352 /* We need to strip nops here, because the front end likes to
7353 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
7355 if (warn_strict_aliasing
<= 2)
7356 strict_aliasing_warning (intype
, type
, sexpr
);
7358 return build_nop (type
, expr
);
7360 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
7361 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
7363 if (complain
& tf_warning
)
7364 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7365 object pointer type or vice versa is conditionally-supported." */
7366 warning (OPT_Wconditionally_supported
,
7367 "casting between pointer-to-function and pointer-to-object "
7368 "is conditionally-supported");
7369 return build_nop (type
, expr
);
7371 else if (VECTOR_TYPE_P (type
))
7372 return convert_to_vector (type
, expr
);
7373 else if (VECTOR_TYPE_P (intype
)
7374 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
7375 return convert_to_integer_nofold (type
, expr
);
7380 if (complain
& tf_error
)
7381 error ("invalid cast from type %qT to type %qT", intype
, type
);
7382 return error_mark_node
;
7385 return cp_convert (type
, expr
, complain
);
7389 build_reinterpret_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7393 if (type
== error_mark_node
|| expr
== error_mark_node
)
7394 return error_mark_node
;
7396 if (processing_template_decl
)
7398 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
7400 if (!TREE_SIDE_EFFECTS (t
)
7401 && type_dependent_expression_p (expr
))
7402 /* There might turn out to be side effects inside expr. */
7403 TREE_SIDE_EFFECTS (t
) = 1;
7404 return convert_from_reference (t
);
7407 r
= build_reinterpret_cast_1 (type
, expr
, /*c_cast_p=*/false,
7408 /*valid_p=*/NULL
, complain
);
7409 if (r
!= error_mark_node
)
7411 maybe_warn_about_useless_cast (type
, expr
, complain
);
7412 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7417 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7418 return an appropriate expression. Otherwise, return
7419 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7420 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7421 performing a C-style cast, its value upon return will indicate
7422 whether or not the conversion succeeded. */
7425 build_const_cast_1 (tree dst_type
, tree expr
, tsubst_flags_t complain
,
7429 tree reference_type
;
7431 /* Callers are responsible for handling error_mark_node as a
7432 destination type. */
7433 gcc_assert (dst_type
!= error_mark_node
);
7434 /* In a template, callers should be building syntactic
7435 representations of casts, not using this machinery. */
7436 gcc_assert (!processing_template_decl
);
7438 /* Assume the conversion is invalid. */
7442 if (!POINTER_TYPE_P (dst_type
) && !TYPE_PTRDATAMEM_P (dst_type
))
7444 if (complain
& tf_error
)
7445 error ("invalid use of const_cast with type %qT, "
7446 "which is not a pointer, "
7447 "reference, nor a pointer-to-data-member type", dst_type
);
7448 return error_mark_node
;
7451 if (TREE_CODE (TREE_TYPE (dst_type
)) == FUNCTION_TYPE
)
7453 if (complain
& tf_error
)
7454 error ("invalid use of const_cast with type %qT, which is a pointer "
7455 "or reference to a function type", dst_type
);
7456 return error_mark_node
;
7459 /* A prvalue of non-class type is cv-unqualified. */
7460 dst_type
= cv_unqualified (dst_type
);
7462 /* Save casted types in the function's used types hash table. */
7463 used_types_insert (dst_type
);
7465 src_type
= TREE_TYPE (expr
);
7466 /* Expressions do not really have reference types. */
7467 if (TREE_CODE (src_type
) == REFERENCE_TYPE
)
7468 src_type
= TREE_TYPE (src_type
);
7470 /* [expr.const.cast]
7472 For two object types T1 and T2, if a pointer to T1 can be explicitly
7473 converted to the type "pointer to T2" using a const_cast, then the
7474 following conversions can also be made:
7476 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7477 type T2 using the cast const_cast<T2&>;
7479 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7480 type T2 using the cast const_cast<T2&&>; and
7482 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7483 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7485 if (TREE_CODE (dst_type
) == REFERENCE_TYPE
)
7487 reference_type
= dst_type
;
7488 if (!TYPE_REF_IS_RVALUE (dst_type
)
7494 if (complain
& tf_error
)
7495 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7496 src_type
, dst_type
);
7497 return error_mark_node
;
7499 dst_type
= build_pointer_type (TREE_TYPE (dst_type
));
7500 src_type
= build_pointer_type (src_type
);
7504 reference_type
= NULL_TREE
;
7505 /* If the destination type is not a reference type, the
7506 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7507 conversions are performed. */
7508 src_type
= type_decays_to (src_type
);
7509 if (src_type
== error_mark_node
)
7510 return error_mark_node
;
7513 if (TYPE_PTR_P (src_type
) || TYPE_PTRDATAMEM_P (src_type
))
7515 if (comp_ptr_ttypes_const (dst_type
, src_type
))
7520 /* This cast is actually a C-style cast. Issue a warning if
7521 the user is making a potentially unsafe cast. */
7522 check_for_casting_away_constness (src_type
, dst_type
,
7523 CAST_EXPR
, complain
);
7524 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
7525 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
7526 && (complain
& tf_warning
)
7527 && min_align_of_type (TREE_TYPE (dst_type
))
7528 > min_align_of_type (TREE_TYPE (src_type
)))
7529 warning (OPT_Wcast_align
, "cast from %qH to %qI "
7530 "increases required alignment of target type",
7531 src_type
, dst_type
);
7535 expr
= cp_build_addr_expr (expr
, complain
);
7536 if (expr
== error_mark_node
)
7537 return error_mark_node
;
7538 expr
= build_nop (reference_type
, expr
);
7539 return convert_from_reference (expr
);
7543 expr
= decay_conversion (expr
, complain
);
7544 if (expr
== error_mark_node
)
7545 return error_mark_node
;
7547 /* build_c_cast puts on a NOP_EXPR to make the result not an
7548 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7549 non-lvalue context. */
7550 if (TREE_CODE (expr
) == NOP_EXPR
7551 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7552 expr
= TREE_OPERAND (expr
, 0);
7553 return build_nop (dst_type
, expr
);
7557 && !at_least_as_qualified_p (TREE_TYPE (dst_type
),
7558 TREE_TYPE (src_type
)))
7559 check_for_casting_away_constness (src_type
, dst_type
, CAST_EXPR
,
7563 if (complain
& tf_error
)
7564 error ("invalid const_cast from type %qT to type %qT",
7565 src_type
, dst_type
);
7566 return error_mark_node
;
7570 build_const_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7574 if (type
== error_mark_node
|| error_operand_p (expr
))
7575 return error_mark_node
;
7577 if (processing_template_decl
)
7579 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
7581 if (!TREE_SIDE_EFFECTS (t
)
7582 && type_dependent_expression_p (expr
))
7583 /* There might turn out to be side effects inside expr. */
7584 TREE_SIDE_EFFECTS (t
) = 1;
7585 return convert_from_reference (t
);
7588 r
= build_const_cast_1 (type
, expr
, complain
, /*valid_p=*/NULL
);
7589 if (r
!= error_mark_node
)
7591 maybe_warn_about_useless_cast (type
, expr
, complain
);
7592 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7597 /* Like cp_build_c_cast, but for the c-common bits. */
7600 build_c_cast (location_t
/*loc*/, tree type
, tree expr
)
7602 return cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7605 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7606 preserve location information even for tree nodes that don't
7610 build_c_cast (location_t loc
, tree type
, cp_expr expr
)
7612 cp_expr result
= cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7613 result
.set_location (loc
);
7617 /* Build an expression representing an explicit C-style cast to type
7618 TYPE of expression EXPR. */
7621 cp_build_c_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7627 if (type
== error_mark_node
|| error_operand_p (expr
))
7628 return error_mark_node
;
7630 if (processing_template_decl
)
7632 tree t
= build_min (CAST_EXPR
, type
,
7633 tree_cons (NULL_TREE
, value
, NULL_TREE
));
7634 /* We don't know if it will or will not have side effects. */
7635 TREE_SIDE_EFFECTS (t
) = 1;
7636 return convert_from_reference (t
);
7639 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7640 'Class') should always be retained, because this information aids
7641 in method lookup. */
7642 if (objc_is_object_ptr (type
)
7643 && objc_is_object_ptr (TREE_TYPE (expr
)))
7644 return build_nop (type
, expr
);
7646 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7647 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7648 if (TREE_CODE (type
) != REFERENCE_TYPE
7649 && TREE_CODE (value
) == NOP_EXPR
7650 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
7651 value
= TREE_OPERAND (value
, 0);
7653 if (TREE_CODE (type
) == ARRAY_TYPE
)
7655 /* Allow casting from T1* to T2[] because Cfront allows it.
7656 NIHCL uses it. It is not valid ISO C++ however. */
7657 if (TYPE_PTR_P (TREE_TYPE (expr
)))
7659 if (complain
& tf_error
)
7660 permerror (input_location
, "ISO C++ forbids casting to an array type %qT", type
);
7662 return error_mark_node
;
7663 type
= build_pointer_type (TREE_TYPE (type
));
7667 if (complain
& tf_error
)
7668 error ("ISO C++ forbids casting to an array type %qT", type
);
7669 return error_mark_node
;
7673 if (TREE_CODE (type
) == FUNCTION_TYPE
7674 || TREE_CODE (type
) == METHOD_TYPE
)
7676 if (complain
& tf_error
)
7677 error ("invalid cast to function type %qT", type
);
7678 return error_mark_node
;
7681 if (TYPE_PTR_P (type
)
7682 && TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
7683 /* Casting to an integer of smaller size is an error detected elsewhere. */
7684 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (value
))
7685 /* Don't warn about converting any constant. */
7686 && !TREE_CONSTANT (value
))
7687 warning_at (input_location
, OPT_Wint_to_pointer_cast
,
7688 "cast to pointer from integer of different size");
7690 /* A C-style cast can be a const_cast. */
7691 result
= build_const_cast_1 (type
, value
, complain
& tf_warning
,
7695 if (result
!= error_mark_node
)
7697 maybe_warn_about_useless_cast (type
, value
, complain
);
7698 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7703 /* Or a static cast. */
7704 result
= build_static_cast_1 (type
, value
, /*c_cast_p=*/true,
7705 &valid_p
, complain
);
7706 /* Or a reinterpret_cast. */
7708 result
= build_reinterpret_cast_1 (type
, value
, /*c_cast_p=*/true,
7709 &valid_p
, complain
);
7710 /* The static_cast or reinterpret_cast may be followed by a
7713 /* A valid cast may result in errors if, for example, a
7714 conversion to an ambiguous base class is required. */
7715 && !error_operand_p (result
))
7719 maybe_warn_about_useless_cast (type
, value
, complain
);
7720 maybe_warn_about_cast_ignoring_quals (type
, complain
);
7722 /* Non-class rvalues always have cv-unqualified type. */
7723 if (!CLASS_TYPE_P (type
))
7724 type
= TYPE_MAIN_VARIANT (type
);
7725 result_type
= TREE_TYPE (result
);
7726 if (!CLASS_TYPE_P (result_type
) && TREE_CODE (type
) != REFERENCE_TYPE
)
7727 result_type
= TYPE_MAIN_VARIANT (result_type
);
7728 /* If the type of RESULT does not match TYPE, perform a
7729 const_cast to make it match. If the static_cast or
7730 reinterpret_cast succeeded, we will differ by at most
7731 cv-qualification, so the follow-on const_cast is guaranteed
7733 if (!same_type_p (non_reference (type
), non_reference (result_type
)))
7735 result
= build_const_cast_1 (type
, result
, false, &valid_p
);
7736 gcc_assert (valid_p
);
7741 return error_mark_node
;
7744 /* For use from the C common bits. */
7746 build_modify_expr (location_t location
,
7747 tree lhs
, tree
/*lhs_origtype*/,
7748 enum tree_code modifycode
,
7749 location_t
/*rhs_location*/, tree rhs
,
7750 tree
/*rhs_origtype*/)
7752 return cp_build_modify_expr (location
, lhs
, modifycode
, rhs
,
7753 tf_warning_or_error
);
7756 /* Build an assignment expression of lvalue LHS from value RHS.
7757 MODIFYCODE is the code for a binary operator that we use
7758 to combine the old value of LHS with RHS to get the new value.
7759 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7761 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7764 cp_build_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
7765 tree rhs
, tsubst_flags_t complain
)
7767 lhs
= mark_lvalue_use_nonread (lhs
);
7769 tree result
= NULL_TREE
;
7771 tree lhstype
= TREE_TYPE (lhs
);
7773 tree olhstype
= lhstype
;
7774 bool plain_assign
= (modifycode
== NOP_EXPR
);
7775 bool compound_side_effects_p
= false;
7776 tree preeval
= NULL_TREE
;
7778 /* Avoid duplicate error messages from operands that had errors. */
7779 if (error_operand_p (lhs
) || error_operand_p (rhs
))
7780 return error_mark_node
;
7782 while (TREE_CODE (lhs
) == COMPOUND_EXPR
)
7784 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
7785 compound_side_effects_p
= true;
7786 lhs
= TREE_OPERAND (lhs
, 1);
7789 /* Handle control structure constructs used as "lvalues". Note that we
7790 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
7791 switch (TREE_CODE (lhs
))
7793 /* Handle --foo = 5; as these are valid constructs in C++. */
7794 case PREDECREMENT_EXPR
:
7795 case PREINCREMENT_EXPR
:
7796 if (compound_side_effects_p
)
7797 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
7798 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
7799 lhs
= build2 (TREE_CODE (lhs
), TREE_TYPE (lhs
),
7800 cp_stabilize_reference (TREE_OPERAND (lhs
, 0)),
7801 TREE_OPERAND (lhs
, 1));
7802 lhs
= build2 (COMPOUND_EXPR
, lhstype
, lhs
, TREE_OPERAND (lhs
, 0));
7804 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
7805 and looked through the COMPOUND_EXPRs, readd them now around
7806 the resulting lhs. */
7807 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
7809 lhs
= build2 (COMPOUND_EXPR
, lhstype
, TREE_OPERAND (olhs
, 0), lhs
);
7810 tree
*ptr
= &TREE_OPERAND (lhs
, 1);
7811 for (olhs
= TREE_OPERAND (olhs
, 1);
7812 TREE_CODE (olhs
) == COMPOUND_EXPR
;
7813 olhs
= TREE_OPERAND (olhs
, 1))
7815 *ptr
= build2 (COMPOUND_EXPR
, lhstype
,
7816 TREE_OPERAND (olhs
, 0), *ptr
);
7817 ptr
= &TREE_OPERAND (*ptr
, 1);
7823 if (compound_side_effects_p
)
7824 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
7825 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
7826 lhs
= build2 (TREE_CODE (lhs
), TREE_TYPE (lhs
),
7827 cp_stabilize_reference (TREE_OPERAND (lhs
, 0)),
7828 TREE_OPERAND (lhs
, 1));
7829 lhs
= build2 (COMPOUND_EXPR
, lhstype
, lhs
, TREE_OPERAND (lhs
, 0));
7830 goto maybe_add_compound
;
7834 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7835 when neither operand has side-effects. */
7836 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7837 return error_mark_node
;
7839 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0))
7840 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 1)));
7842 lhs
= build3 (COND_EXPR
, TREE_TYPE (lhs
),
7843 build2 (TREE_CODE (lhs
) == MIN_EXPR
? LE_EXPR
: GE_EXPR
,
7845 TREE_OPERAND (lhs
, 0),
7846 TREE_OPERAND (lhs
, 1)),
7847 TREE_OPERAND (lhs
, 0),
7848 TREE_OPERAND (lhs
, 1));
7851 /* Handle (a ? b : c) used as an "lvalue". */
7854 /* Produce (a ? (b = rhs) : (c = rhs))
7855 except that the RHS goes through a save-expr
7856 so the code to compute it is only emitted once. */
7859 if (VOID_TYPE_P (TREE_TYPE (rhs
)))
7861 if (complain
& tf_error
)
7862 error ("void value not ignored as it ought to be");
7863 return error_mark_node
;
7866 rhs
= stabilize_expr (rhs
, &preeval
);
7868 /* Check this here to avoid odd errors when trying to convert
7869 a throw to the type of the COND_EXPR. */
7870 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7871 return error_mark_node
;
7873 cond
= build_conditional_expr
7874 (input_location
, TREE_OPERAND (lhs
, 0),
7875 cp_build_modify_expr (loc
, TREE_OPERAND (lhs
, 1),
7876 modifycode
, rhs
, complain
),
7877 cp_build_modify_expr (loc
, TREE_OPERAND (lhs
, 2),
7878 modifycode
, rhs
, complain
),
7881 if (cond
== error_mark_node
)
7883 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
7884 and looked through the COMPOUND_EXPRs, readd them now around
7885 the resulting cond before adding the preevaluated rhs. */
7886 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
7888 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
7889 TREE_OPERAND (olhs
, 0), cond
);
7890 tree
*ptr
= &TREE_OPERAND (cond
, 1);
7891 for (olhs
= TREE_OPERAND (olhs
, 1);
7892 TREE_CODE (olhs
) == COMPOUND_EXPR
;
7893 olhs
= TREE_OPERAND (olhs
, 1))
7895 *ptr
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
7896 TREE_OPERAND (olhs
, 0), *ptr
);
7897 ptr
= &TREE_OPERAND (*ptr
, 1);
7900 /* Make sure the code to compute the rhs comes out
7901 before the split. */
7911 if (modifycode
== INIT_EXPR
)
7913 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
7914 /* Do the default thing. */;
7915 else if (TREE_CODE (rhs
) == CONSTRUCTOR
)
7917 /* Compound literal. */
7918 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
7919 /* Call convert to generate an error; see PR 11063. */
7920 rhs
= convert (lhstype
, rhs
);
7921 result
= build2 (INIT_EXPR
, lhstype
, lhs
, rhs
);
7922 TREE_SIDE_EFFECTS (result
) = 1;
7925 else if (! MAYBE_CLASS_TYPE_P (lhstype
))
7926 /* Do the default thing. */;
7929 vec
<tree
, va_gc
> *rhs_vec
= make_tree_vector_single (rhs
);
7930 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
7931 &rhs_vec
, lhstype
, LOOKUP_NORMAL
,
7933 release_tree_vector (rhs_vec
);
7934 if (result
== NULL_TREE
)
7935 return error_mark_node
;
7941 lhs
= require_complete_type_sfinae (lhs
, complain
);
7942 if (lhs
== error_mark_node
)
7943 return error_mark_node
;
7945 if (modifycode
== NOP_EXPR
)
7947 if (c_dialect_objc ())
7949 result
= objc_maybe_build_modify_expr (lhs
, rhs
);
7954 /* `operator=' is not an inheritable operator. */
7955 if (! MAYBE_CLASS_TYPE_P (lhstype
))
7956 /* Do the default thing. */;
7959 result
= build_new_op (input_location
, MODIFY_EXPR
,
7960 LOOKUP_NORMAL
, lhs
, rhs
,
7961 make_node (NOP_EXPR
), /*overload=*/NULL
,
7963 if (result
== NULL_TREE
)
7964 return error_mark_node
;
7971 tree init
= NULL_TREE
;
7973 /* A binary op has been requested. Combine the old LHS
7974 value with the RHS producing the value we should actually
7975 store into the LHS. */
7976 gcc_assert (!((TREE_CODE (lhstype
) == REFERENCE_TYPE
7977 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype
)))
7978 || MAYBE_CLASS_TYPE_P (lhstype
)));
7980 /* Preevaluate the RHS to make sure its evaluation is complete
7981 before the lvalue-to-rvalue conversion of the LHS:
7983 [expr.ass] With respect to an indeterminately-sequenced
7984 function call, the operation of a compound assignment is a
7985 single evaluation. [ Note: Therefore, a function call shall
7986 not intervene between the lvalue-to-rvalue conversion and the
7987 side effect associated with any single compound assignment
7988 operator. -- end note ] */
7989 lhs
= cp_stabilize_reference (lhs
);
7991 if (rhs
== error_mark_node
)
7992 return error_mark_node
;
7993 rhs
= stabilize_expr (rhs
, &init
);
7994 newrhs
= cp_build_binary_op (loc
, modifycode
, lhs
, rhs
, complain
);
7995 if (newrhs
== error_mark_node
)
7997 if (complain
& tf_error
)
7998 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode
,
7999 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
8000 return error_mark_node
;
8004 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), init
, newrhs
);
8006 /* Now it looks like a plain assignment. */
8007 modifycode
= NOP_EXPR
;
8008 if (c_dialect_objc ())
8010 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
8015 gcc_assert (TREE_CODE (lhstype
) != REFERENCE_TYPE
);
8016 gcc_assert (TREE_CODE (TREE_TYPE (newrhs
)) != REFERENCE_TYPE
);
8019 /* The left-hand side must be an lvalue. */
8020 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
8021 return error_mark_node
;
8023 /* Warn about modifying something that is `const'. Don't warn if
8024 this is initialization. */
8025 if (modifycode
!= INIT_EXPR
8026 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
8027 /* Functions are not modifiable, even though they are
8029 || TREE_CODE (TREE_TYPE (lhs
)) == FUNCTION_TYPE
8030 || TREE_CODE (TREE_TYPE (lhs
)) == METHOD_TYPE
8031 /* If it's an aggregate and any field is const, then it is
8032 effectively const. */
8033 || (CLASS_TYPE_P (lhstype
)
8034 && C_TYPE_FIELDS_READONLY (lhstype
))))
8036 if (complain
& tf_error
)
8037 cxx_readonly_error (lhs
, lv_assign
);
8039 return error_mark_node
;
8042 /* If storing into a structure or union member, it may have been given a
8043 lowered bitfield type. We need to convert to the declared type first,
8044 so retrieve it now. */
8046 olhstype
= unlowered_expr_type (lhs
);
8048 /* Convert new value to destination type. */
8050 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
8054 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs
))
8056 if (modifycode
!= INIT_EXPR
)
8058 if (complain
& tf_error
)
8059 error ("assigning to an array from an initializer list");
8060 return error_mark_node
;
8062 if (check_array_initializer (lhs
, lhstype
, newrhs
))
8063 return error_mark_node
;
8064 newrhs
= digest_init (lhstype
, newrhs
, complain
);
8065 if (newrhs
== error_mark_node
)
8066 return error_mark_node
;
8069 /* C++11 8.5/17: "If the destination type is an array of characters,
8070 an array of char16_t, an array of char32_t, or an array of wchar_t,
8071 and the initializer is a string literal...". */
8072 else if (TREE_CODE (newrhs
) == STRING_CST
8073 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype
)))
8074 && modifycode
== INIT_EXPR
)
8076 newrhs
= digest_init (lhstype
, newrhs
, complain
);
8077 if (newrhs
== error_mark_node
)
8078 return error_mark_node
;
8081 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
8082 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))))
8084 if (complain
& tf_error
)
8085 error ("incompatible types in assignment of %qT to %qT",
8086 TREE_TYPE (rhs
), lhstype
);
8087 return error_mark_node
;
8090 /* Allow array assignment in compiler-generated code. */
8091 else if (!current_function_decl
8092 || !DECL_DEFAULTED_FN (current_function_decl
))
8094 /* This routine is used for both initialization and assignment.
8095 Make sure the diagnostic message differentiates the context. */
8096 if (complain
& tf_error
)
8098 if (modifycode
== INIT_EXPR
)
8099 error ("array used as initializer");
8101 error ("invalid array assignment");
8103 return error_mark_node
;
8106 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
8107 ? 1 + (modifycode
!= INIT_EXPR
): 0;
8108 result
= build_vec_init (lhs
, NULL_TREE
, newrhs
,
8109 /*explicit_value_init_p=*/false,
8110 from_array
, complain
);
8114 if (modifycode
== INIT_EXPR
)
8115 /* Calls with INIT_EXPR are all direct-initialization, so don't set
8116 LOOKUP_ONLYCONVERTING. */
8117 newrhs
= convert_for_initialization (lhs
, olhstype
, newrhs
, LOOKUP_NORMAL
,
8118 ICR_INIT
, NULL_TREE
, 0,
8121 newrhs
= convert_for_assignment (olhstype
, newrhs
, ICR_ASSIGN
,
8122 NULL_TREE
, 0, complain
, LOOKUP_IMPLICIT
);
8124 if (!same_type_p (lhstype
, olhstype
))
8125 newrhs
= cp_convert_and_check (lhstype
, newrhs
, complain
);
8127 if (modifycode
!= INIT_EXPR
)
8129 if (TREE_CODE (newrhs
) == CALL_EXPR
8130 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
8131 newrhs
= build_cplus_new (lhstype
, newrhs
, complain
);
8133 /* Can't initialize directly from a TARGET_EXPR, since that would
8134 cause the lhs to be constructed twice, and possibly result in
8135 accidental self-initialization. So we force the TARGET_EXPR to be
8136 expanded without a target. */
8137 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
8138 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
8139 TREE_OPERAND (newrhs
, 0));
8142 if (newrhs
== error_mark_node
)
8143 return error_mark_node
;
8145 if (c_dialect_objc () && flag_objc_gc
)
8147 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
8153 result
= build2 (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
8154 lhstype
, lhs
, newrhs
);
8156 TREE_SIDE_EFFECTS (result
) = 1;
8158 TREE_NO_WARNING (result
) = 1;
8162 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), preeval
, result
);
8167 build_x_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
8168 tree rhs
, tsubst_flags_t complain
)
8170 tree orig_lhs
= lhs
;
8171 tree orig_rhs
= rhs
;
8172 tree overload
= NULL_TREE
;
8173 tree op
= build_nt (modifycode
, NULL_TREE
, NULL_TREE
);
8175 if (processing_template_decl
)
8177 if (modifycode
== NOP_EXPR
8178 || type_dependent_expression_p (lhs
)
8179 || type_dependent_expression_p (rhs
))
8180 return build_min_nt_loc (loc
, MODOP_EXPR
, lhs
,
8181 build_min_nt_loc (loc
, modifycode
, NULL_TREE
,
8184 lhs
= build_non_dependent_expr (lhs
);
8185 rhs
= build_non_dependent_expr (rhs
);
8188 if (modifycode
!= NOP_EXPR
)
8190 tree rval
= build_new_op (loc
, MODIFY_EXPR
, LOOKUP_NORMAL
,
8191 lhs
, rhs
, op
, &overload
, complain
);
8194 if (rval
== error_mark_node
)
8196 TREE_NO_WARNING (rval
) = 1;
8197 if (processing_template_decl
)
8199 if (overload
!= NULL_TREE
)
8200 return (build_min_non_dep_op_overload
8201 (MODIFY_EXPR
, rval
, overload
, orig_lhs
, orig_rhs
));
8203 return (build_min_non_dep
8204 (MODOP_EXPR
, rval
, orig_lhs
, op
, orig_rhs
));
8209 return cp_build_modify_expr (loc
, lhs
, modifycode
, rhs
, complain
);
8212 /* Helper function for get_delta_difference which assumes FROM is a base
8213 class of TO. Returns a delta for the conversion of pointer-to-member
8214 of FROM to pointer-to-member of TO. If the conversion is invalid and
8215 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
8216 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
8217 If C_CAST_P is true, this conversion is taking place as part of a
8221 get_delta_difference_1 (tree from
, tree to
, bool c_cast_p
,
8222 tsubst_flags_t complain
)
8227 binfo
= lookup_base (to
, from
, c_cast_p
? ba_unique
: ba_check
,
8230 if (binfo
== error_mark_node
)
8232 if (!(complain
& tf_error
))
8233 return error_mark_node
;
8235 error (" in pointer to member function conversion");
8236 return size_zero_node
;
8240 if (kind
!= bk_via_virtual
)
8241 return BINFO_OFFSET (binfo
);
8243 /* FROM is a virtual base class of TO. Issue an error or warning
8244 depending on whether or not this is a reinterpret cast. */
8246 if (!(complain
& tf_error
))
8247 return error_mark_node
;
8249 error ("pointer to member conversion via virtual base %qT",
8250 BINFO_TYPE (binfo_from_vbase (binfo
)));
8252 return size_zero_node
;
8259 /* Get difference in deltas for different pointer to member function
8260 types. If the conversion is invalid and tf_error is not set in
8261 COMPLAIN, returns error_mark_node, otherwise returns an integer
8262 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
8263 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
8264 conversions as well. If C_CAST_P is true this conversion is taking
8265 place as part of a C-style cast.
8267 Note that the naming of FROM and TO is kind of backwards; the return
8268 value is what we add to a TO in order to get a FROM. They are named
8269 this way because we call this function to find out how to convert from
8270 a pointer to member of FROM to a pointer to member of TO. */
8273 get_delta_difference (tree from
, tree to
,
8274 bool allow_inverse_p
,
8275 bool c_cast_p
, tsubst_flags_t complain
)
8279 if (same_type_ignoring_top_level_qualifiers_p (from
, to
))
8280 /* Pointer to member of incomplete class is permitted*/
8281 result
= size_zero_node
;
8283 result
= get_delta_difference_1 (from
, to
, c_cast_p
, complain
);
8285 if (result
== error_mark_node
)
8286 return error_mark_node
;
8290 if (!allow_inverse_p
)
8292 if (!(complain
& tf_error
))
8293 return error_mark_node
;
8295 error_not_base_type (from
, to
);
8296 error (" in pointer to member conversion");
8297 result
= size_zero_node
;
8301 result
= get_delta_difference_1 (to
, from
, c_cast_p
, complain
);
8303 if (result
== error_mark_node
)
8304 return error_mark_node
;
8307 result
= size_diffop_loc (input_location
,
8308 size_zero_node
, result
);
8311 if (!(complain
& tf_error
))
8312 return error_mark_node
;
8314 error_not_base_type (from
, to
);
8315 error (" in pointer to member conversion");
8316 result
= size_zero_node
;
8321 return convert_to_integer (ptrdiff_type_node
, result
);
8324 /* Return a constructor for the pointer-to-member-function TYPE using
8325 the other components as specified. */
8328 build_ptrmemfunc1 (tree type
, tree delta
, tree pfn
)
8333 vec
<constructor_elt
, va_gc
> *v
;
8335 /* Pull the FIELD_DECLs out of the type. */
8336 pfn_field
= TYPE_FIELDS (type
);
8337 delta_field
= DECL_CHAIN (pfn_field
);
8339 /* Make sure DELTA has the type we want. */
8340 delta
= convert_and_check (input_location
, delta_type_node
, delta
);
8342 /* Convert to the correct target type if necessary. */
8343 pfn
= fold_convert (TREE_TYPE (pfn_field
), pfn
);
8345 /* Finish creating the initializer. */
8347 CONSTRUCTOR_APPEND_ELT(v
, pfn_field
, pfn
);
8348 CONSTRUCTOR_APPEND_ELT(v
, delta_field
, delta
);
8349 u
= build_constructor (type
, v
);
8350 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) & TREE_CONSTANT (delta
);
8351 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
8352 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
8354 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
8359 /* Build a constructor for a pointer to member function. It can be
8360 used to initialize global variables, local variable, or used
8361 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8364 If FORCE is nonzero, then force this conversion, even if
8365 we would rather not do it. Usually set when using an explicit
8366 cast. A C-style cast is being processed iff C_CAST_P is true.
8368 Return error_mark_node, if something goes wrong. */
8371 build_ptrmemfunc (tree type
, tree pfn
, int force
, bool c_cast_p
,
8372 tsubst_flags_t complain
)
8378 if (error_operand_p (pfn
))
8379 return error_mark_node
;
8381 pfn_type
= TREE_TYPE (pfn
);
8382 to_type
= build_ptrmemfunc_type (type
);
8384 /* Handle multiple conversions of pointer to member functions. */
8385 if (TYPE_PTRMEMFUNC_P (pfn_type
))
8387 tree delta
= NULL_TREE
;
8388 tree npfn
= NULL_TREE
;
8392 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
,
8393 LOOKUP_NORMAL
, complain
))
8395 if (complain
& tf_error
)
8396 error ("invalid conversion to type %qT from type %qT",
8399 return error_mark_node
;
8402 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
8403 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
8405 c_cast_p
, complain
);
8406 if (n
== error_mark_node
)
8407 return error_mark_node
;
8409 /* We don't have to do any conversion to convert a
8410 pointer-to-member to its own type. But, we don't want to
8411 just return a PTRMEM_CST if there's an explicit cast; that
8412 cast should make the expression an invalid template argument. */
8413 if (TREE_CODE (pfn
) != PTRMEM_CST
)
8415 if (same_type_p (to_type
, pfn_type
))
8417 else if (integer_zerop (n
))
8418 return build_reinterpret_cast (to_type
, pfn
,
8422 if (TREE_SIDE_EFFECTS (pfn
))
8423 pfn
= save_expr (pfn
);
8425 /* Obtain the function pointer and the current DELTA. */
8426 if (TREE_CODE (pfn
) == PTRMEM_CST
)
8427 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
8430 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
8431 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
8434 /* Just adjust the DELTA field. */
8435 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8436 (TREE_TYPE (delta
), ptrdiff_type_node
));
8437 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
8438 n
= cp_build_binary_op (input_location
,
8439 LSHIFT_EXPR
, n
, integer_one_node
,
8441 delta
= cp_build_binary_op (input_location
,
8442 PLUS_EXPR
, delta
, n
, complain
);
8443 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
8446 /* Handle null pointer to member function conversions. */
8447 if (null_ptr_cst_p (pfn
))
8449 pfn
= cp_build_c_cast (type
, pfn
, complain
);
8450 return build_ptrmemfunc1 (to_type
,
8455 if (type_unknown_p (pfn
))
8456 return instantiate_type (type
, pfn
, complain
);
8458 fn
= TREE_OPERAND (pfn
, 0);
8459 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
8460 /* In a template, we will have preserved the
8462 || (processing_template_decl
&& TREE_CODE (fn
) == OFFSET_REF
));
8463 return make_ptrmem_cst (to_type
, fn
);
8466 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8469 ??? There is no consistency as to the types returned for the above
8470 values. Some code acts as if it were a sizetype and some as if it were
8471 integer_type_node. */
8474 expand_ptrmemfunc_cst (tree cst
, tree
*delta
, tree
*pfn
)
8476 tree type
= TREE_TYPE (cst
);
8477 tree fn
= PTRMEM_CST_MEMBER (cst
);
8478 tree ptr_class
, fn_class
;
8480 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
8482 /* The class that the function belongs to. */
8483 fn_class
= DECL_CONTEXT (fn
);
8485 /* The class that we're creating a pointer to member of. */
8486 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
8488 /* First, calculate the adjustment to the function's class. */
8489 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0,
8490 /*c_cast_p=*/0, tf_warning_or_error
);
8492 if (!DECL_VIRTUAL_P (fn
))
8493 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
),
8494 build_addr_func (fn
, tf_warning_or_error
));
8497 /* If we're dealing with a virtual function, we have to adjust 'this'
8498 again, to point to the base which provides the vtable entry for
8499 fn; the call will do the opposite adjustment. */
8500 tree orig_class
= DECL_CONTEXT (fn
);
8501 tree binfo
= binfo_or_else (orig_class
, fn_class
);
8502 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
8503 *delta
, BINFO_OFFSET (binfo
));
8505 /* We set PFN to the vtable offset at which the function can be
8506 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8507 case delta is shifted left, and then incremented). */
8508 *pfn
= DECL_VINDEX (fn
);
8509 *pfn
= fold_build2 (MULT_EXPR
, integer_type_node
, *pfn
,
8510 TYPE_SIZE_UNIT (vtable_entry_type
));
8512 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
8514 case ptrmemfunc_vbit_in_pfn
:
8515 *pfn
= fold_build2 (PLUS_EXPR
, integer_type_node
, *pfn
,
8519 case ptrmemfunc_vbit_in_delta
:
8520 *delta
= fold_build2 (LSHIFT_EXPR
, TREE_TYPE (*delta
),
8521 *delta
, integer_one_node
);
8522 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
8523 *delta
, integer_one_node
);
8530 *pfn
= fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), *pfn
);
8534 /* Return an expression for PFN from the pointer-to-member function
8538 pfn_from_ptrmemfunc (tree t
)
8540 if (TREE_CODE (t
) == PTRMEM_CST
)
8545 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
8550 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
8553 /* Return an expression for DELTA from the pointer-to-member function
8557 delta_from_ptrmemfunc (tree t
)
8559 if (TREE_CODE (t
) == PTRMEM_CST
)
8564 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
8569 return build_ptrmemfunc_access_expr (t
, delta_identifier
);
8572 /* Convert value RHS to type TYPE as preparation for an assignment to
8573 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8574 implicit conversion is. If FNDECL is non-NULL, we are doing the
8575 conversion in order to pass the PARMNUMth argument of FNDECL.
8576 If FNDECL is NULL, we are doing the conversion in function pointer
8577 argument passing, conversion in initialization, etc. */
8580 convert_for_assignment (tree type
, tree rhs
,
8581 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
8582 tsubst_flags_t complain
, int flags
)
8585 enum tree_code coder
;
8587 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8588 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
8589 rhs
= TREE_OPERAND (rhs
, 0);
8591 /* Handle [dcl.init.list] direct-list-initialization from
8592 single element of enumeration with a fixed underlying type. */
8593 if (is_direct_enum_init (type
, rhs
))
8595 tree elt
= CONSTRUCTOR_ELT (rhs
, 0)->value
;
8596 if (check_narrowing (ENUM_UNDERLYING_TYPE (type
), elt
, complain
))
8598 warning_sentinel
w (warn_useless_cast
);
8599 rhs
= cp_build_c_cast (type
, elt
, complain
);
8602 rhs
= error_mark_node
;
8605 rhstype
= TREE_TYPE (rhs
);
8606 coder
= TREE_CODE (rhstype
);
8608 if (VECTOR_TYPE_P (type
) && coder
== VECTOR_TYPE
8609 && vector_types_convertible_p (type
, rhstype
, true))
8611 rhs
= mark_rvalue_use (rhs
);
8612 return convert (type
, rhs
);
8615 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
8616 return error_mark_node
;
8617 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
8618 return error_mark_node
;
8620 /* The RHS of an assignment cannot have void type. */
8621 if (coder
== VOID_TYPE
)
8623 if (complain
& tf_error
)
8624 error ("void value not ignored as it ought to be");
8625 return error_mark_node
;
8628 if (c_dialect_objc ())
8632 tree rname
= fndecl
;
8643 selector
= objc_message_selector ();
8645 if (selector
&& parmno
> 1)
8652 if (objc_compare_types (type
, rhstype
, parmno
, rname
))
8654 rhs
= mark_rvalue_use (rhs
);
8655 return convert (type
, rhs
);
8661 The expression is implicitly converted (clause _conv_) to the
8662 cv-unqualified type of the left operand.
8664 We allow bad conversions here because by the time we get to this point
8665 we are committed to doing the conversion. If we end up doing a bad
8666 conversion, convert_like will complain. */
8667 if (!can_convert_arg_bad (type
, rhstype
, rhs
, flags
, complain
))
8669 /* When -Wno-pmf-conversions is use, we just silently allow
8670 conversions from pointers-to-members to plain pointers. If
8671 the conversion doesn't work, cp_convert will complain. */
8673 && TYPE_PTR_P (type
)
8674 && TYPE_PTRMEMFUNC_P (rhstype
))
8675 rhs
= cp_convert (strip_top_quals (type
), rhs
, complain
);
8678 if (complain
& tf_error
)
8680 /* If the right-hand side has unknown type, then it is an
8681 overloaded function. Call instantiate_type to get error
8683 if (rhstype
== unknown_type_node
)
8685 tree r
= instantiate_type (type
, rhs
, tf_warning_or_error
);
8686 /* -fpermissive might allow this; recurse. */
8688 return convert_for_assignment (type
, r
, errtype
, fndecl
,
8689 parmnum
, complain
, flags
);
8692 error ("cannot convert %qH to %qI for argument %qP to %qD",
8693 rhstype
, type
, parmnum
, fndecl
);
8697 case ICR_DEFAULT_ARGUMENT
:
8698 error ("cannot convert %qH to %qI in default argument",
8702 error ("cannot convert %qH to %qI in argument passing",
8705 case ICR_CONVERTING
:
8706 error ("cannot convert %qH to %qI",
8710 error ("cannot convert %qH to %qI in initialization",
8714 error ("cannot convert %qH to %qI in return",
8718 error ("cannot convert %qH to %qI in assignment",
8724 if (TYPE_PTR_P (rhstype
)
8725 && TYPE_PTR_P (type
)
8726 && CLASS_TYPE_P (TREE_TYPE (rhstype
))
8727 && CLASS_TYPE_P (TREE_TYPE (type
))
8728 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype
)))
8729 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8730 (TREE_TYPE (rhstype
))),
8731 "class type %qT is incomplete", TREE_TYPE (rhstype
));
8733 return error_mark_node
;
8736 if (warn_suggest_attribute_format
)
8738 const enum tree_code codel
= TREE_CODE (type
);
8739 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
8741 && check_missing_format_attribute (type
, rhstype
)
8742 && (complain
& tf_warning
))
8746 case ICR_DEFAULT_ARGUMENT
:
8748 warning (OPT_Wsuggest_attribute_format
,
8749 "parameter %qP of %qD might be a candidate "
8750 "for a format attribute", parmnum
, fndecl
);
8752 warning (OPT_Wsuggest_attribute_format
,
8753 "parameter might be a candidate "
8754 "for a format attribute");
8756 case ICR_CONVERTING
:
8757 warning (OPT_Wsuggest_attribute_format
,
8758 "target of conversion might be a candidate "
8759 "for a format attribute");
8762 warning (OPT_Wsuggest_attribute_format
,
8763 "target of initialization might be a candidate "
8764 "for a format attribute");
8767 warning (OPT_Wsuggest_attribute_format
,
8768 "return type might be a candidate "
8769 "for a format attribute");
8772 warning (OPT_Wsuggest_attribute_format
,
8773 "left-hand side of assignment might be a candidate "
8774 "for a format attribute");
8781 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8783 if (warn_parentheses
8784 && TREE_CODE (type
) == BOOLEAN_TYPE
8785 && TREE_CODE (rhs
) == MODIFY_EXPR
8786 && !TREE_NO_WARNING (rhs
)
8787 && TREE_CODE (TREE_TYPE (rhs
)) != BOOLEAN_TYPE
8788 && (complain
& tf_warning
))
8790 location_t loc
= EXPR_LOC_OR_LOC (rhs
, input_location
);
8792 warning_at (loc
, OPT_Wparentheses
,
8793 "suggest parentheses around assignment used as truth value");
8794 TREE_NO_WARNING (rhs
) = 1;
8797 return perform_implicit_conversion_flags (strip_top_quals (type
), rhs
,
8801 /* Convert RHS to be of type TYPE.
8802 If EXP is nonzero, it is the target of the initialization.
8803 ERRTYPE indicates what kind of error the implicit conversion is.
8805 Two major differences between the behavior of
8806 `convert_for_assignment' and `convert_for_initialization'
8807 are that references are bashed in the former, while
8808 copied in the latter, and aggregates are assigned in
8809 the former (operator=) while initialized in the
8812 If using constructor make sure no conversion operator exists, if one does
8813 exist, an ambiguity exists. */
8816 convert_for_initialization (tree exp
, tree type
, tree rhs
, int flags
,
8817 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
8818 tsubst_flags_t complain
)
8820 enum tree_code codel
= TREE_CODE (type
);
8822 enum tree_code coder
;
8824 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8825 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8826 if (TREE_CODE (rhs
) == NOP_EXPR
8827 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
8828 && codel
!= REFERENCE_TYPE
)
8829 rhs
= TREE_OPERAND (rhs
, 0);
8831 if (type
== error_mark_node
8832 || rhs
== error_mark_node
8833 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
8834 return error_mark_node
;
8836 if (MAYBE_CLASS_TYPE_P (non_reference (type
)))
8838 else if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
8839 && TREE_CODE (type
) != ARRAY_TYPE
8840 && (TREE_CODE (type
) != REFERENCE_TYPE
8841 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
8842 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
8843 && !TYPE_REFFN_P (type
))
8844 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
8845 rhs
= decay_conversion (rhs
, complain
);
8847 rhstype
= TREE_TYPE (rhs
);
8848 coder
= TREE_CODE (rhstype
);
8850 if (coder
== ERROR_MARK
)
8851 return error_mark_node
;
8853 /* We accept references to incomplete types, so we can
8854 return here before checking if RHS is of complete type. */
8856 if (codel
== REFERENCE_TYPE
)
8858 /* This should eventually happen in convert_arguments. */
8859 int savew
= 0, savee
= 0;
8862 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
8863 rhs
= initialize_reference (type
, rhs
, flags
, complain
);
8866 && (warningcount
+ werrorcount
> savew
|| errorcount
> savee
))
8867 inform (DECL_SOURCE_LOCATION (fndecl
),
8868 "in passing argument %P of %qD", parmnum
, fndecl
);
8874 exp
= require_complete_type_sfinae (exp
, complain
);
8875 if (exp
== error_mark_node
)
8876 return error_mark_node
;
8878 rhstype
= non_reference (rhstype
);
8880 type
= complete_type (type
);
8882 if (DIRECT_INIT_EXPR_P (type
, rhs
))
8883 /* Don't try to do copy-initialization if we already have
8884 direct-initialization. */
8887 if (MAYBE_CLASS_TYPE_P (type
))
8888 return perform_implicit_conversion_flags (type
, rhs
, complain
, flags
);
8890 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
,
8894 /* If RETVAL is the address of, or a reference to, a local variable or
8895 temporary give an appropriate warning and return true. */
8898 maybe_warn_about_returning_address_of_local (tree retval
)
8900 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
8901 tree whats_returned
= fold_for_warn (retval
);
8905 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
8906 whats_returned
= TREE_OPERAND (whats_returned
, 1);
8907 else if (CONVERT_EXPR_P (whats_returned
)
8908 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
)
8909 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8914 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
8916 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8918 while (TREE_CODE (whats_returned
) == COMPONENT_REF
8919 || TREE_CODE (whats_returned
) == ARRAY_REF
)
8920 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8922 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
8924 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
8925 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
8927 warning (OPT_Wreturn_local_addr
, "returning reference to temporary");
8930 if (VAR_P (whats_returned
)
8931 && DECL_NAME (whats_returned
)
8932 && TEMP_NAME_P (DECL_NAME (whats_returned
)))
8934 warning (OPT_Wreturn_local_addr
, "reference to non-lvalue returned");
8939 if (DECL_P (whats_returned
)
8940 && DECL_NAME (whats_returned
)
8941 && DECL_FUNCTION_SCOPE_P (whats_returned
)
8942 && !is_capture_proxy (whats_returned
)
8943 && !(TREE_STATIC (whats_returned
)
8944 || TREE_PUBLIC (whats_returned
)))
8946 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
8947 warning_at (DECL_SOURCE_LOCATION (whats_returned
),
8948 OPT_Wreturn_local_addr
,
8949 "reference to local variable %qD returned",
8951 else if (TREE_CODE (whats_returned
) == LABEL_DECL
)
8952 warning_at (DECL_SOURCE_LOCATION (whats_returned
),
8953 OPT_Wreturn_local_addr
, "address of label %qD returned",
8956 warning_at (DECL_SOURCE_LOCATION (whats_returned
),
8957 OPT_Wreturn_local_addr
, "address of local variable %qD "
8958 "returned", whats_returned
);
8965 /* Check that returning RETVAL from the current function is valid.
8966 Return an expression explicitly showing all conversions required to
8967 change RETVAL into the function return type, and to assign it to
8968 the DECL_RESULT for the function. Set *NO_WARNING to true if
8969 code reaches end of non-void function warning shouldn't be issued
8970 on this RETURN_EXPR. */
8973 check_return_expr (tree retval
, bool *no_warning
)
8976 /* The type actually returned by the function. */
8978 /* The type the function is declared to return, or void if
8979 the declared type is incomplete. */
8981 int fn_returns_value_p
;
8982 bool named_return_value_okay_p
;
8984 *no_warning
= false;
8986 if (flag_cilkplus
&& retval
&& contains_cilk_spawn_stmt (retval
))
8988 error_at (EXPR_LOCATION (retval
), "use of %<_Cilk_spawn%> in a return "
8989 "statement is not allowed");
8993 /* A `volatile' function is one that isn't supposed to return, ever.
8994 (This is a G++ extension, used to get better code for functions
8995 that call the `volatile' function.) */
8996 if (TREE_THIS_VOLATILE (current_function_decl
))
8997 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8999 /* Check for various simple errors. */
9000 if (DECL_DESTRUCTOR_P (current_function_decl
))
9003 error ("returning a value from a destructor");
9006 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
9008 if (in_function_try_handler
)
9009 /* If a return statement appears in a handler of the
9010 function-try-block of a constructor, the program is ill-formed. */
9011 error ("cannot return from a handler of a function-try-block of a constructor");
9013 /* You can't return a value from a constructor. */
9014 error ("returning a value from a constructor");
9018 const tree saved_retval
= retval
;
9020 if (processing_template_decl
)
9022 current_function_returns_value
= 1;
9024 if (check_for_bare_parameter_packs (retval
))
9025 return error_mark_node
;
9027 /* If one of the types might be void, we can't tell whether we're
9028 returning a value. */
9029 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl
)))
9030 && !current_function_auto_return_pattern
)
9031 || (retval
!= NULL_TREE
9032 && (TREE_TYPE (retval
) == NULL_TREE
9033 || WILDCARD_TYPE_P (TREE_TYPE (retval
)))))
9037 functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
9039 /* Deduce auto return type from a return statement. */
9040 if (current_function_auto_return_pattern
)
9045 if (!retval
&& !is_auto (current_function_auto_return_pattern
))
9047 /* Give a helpful error message. */
9048 error ("return-statement with no value, in function returning %qT",
9049 current_function_auto_return_pattern
);
9050 inform (input_location
, "only plain %<auto%> return type can be "
9051 "deduced to %<void%>");
9052 type
= error_mark_node
;
9054 else if (retval
&& BRACE_ENCLOSED_INITIALIZER_P (retval
))
9056 error ("returning initializer list");
9057 type
= error_mark_node
;
9063 auto_node
= type_uses_auto (current_function_auto_return_pattern
);
9064 type
= do_auto_deduction (current_function_auto_return_pattern
,
9068 if (type
== error_mark_node
)
9070 else if (functype
== current_function_auto_return_pattern
)
9071 apply_deduced_return_type (current_function_decl
, type
);
9072 else if (!same_type_p (type
, functype
))
9074 if (LAMBDA_FUNCTION_P (current_function_decl
))
9075 error ("inconsistent types %qT and %qT deduced for "
9076 "lambda return type", functype
, type
);
9078 error ("inconsistent deduction for auto return type: "
9079 "%qT and then %qT", functype
, type
);
9084 result
= DECL_RESULT (current_function_decl
);
9085 valtype
= TREE_TYPE (result
);
9086 gcc_assert (valtype
!= NULL_TREE
);
9087 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
9089 /* Check for a return statement with no return value in a function
9090 that's supposed to return a value. */
9091 if (!retval
&& fn_returns_value_p
)
9093 if (functype
!= error_mark_node
)
9094 permerror (input_location
, "return-statement with no value, in "
9095 "function returning %qT", valtype
);
9096 /* Remember that this function did return. */
9097 current_function_returns_value
= 1;
9098 /* And signal caller that TREE_NO_WARNING should be set on the
9099 RETURN_EXPR to avoid control reaches end of non-void function
9100 warnings in tree-cfg.c. */
9103 /* Check for a return statement with a value in a function that
9104 isn't supposed to return a value. */
9105 else if (retval
&& !fn_returns_value_p
)
9107 if (VOID_TYPE_P (TREE_TYPE (retval
)))
9108 /* You can return a `void' value from a function of `void'
9109 type. In that case, we have to evaluate the expression for
9110 its side-effects. */
9111 finish_expr_stmt (retval
);
9113 permerror (input_location
,
9114 "return-statement with a value, in function "
9115 "returning %qT", valtype
);
9116 current_function_returns_null
= 1;
9118 /* There's really no value to return, after all. */
9122 /* Remember that this function can sometimes return without a
9124 current_function_returns_null
= 1;
9126 /* Remember that this function did return a value. */
9127 current_function_returns_value
= 1;
9129 /* Check for erroneous operands -- but after giving ourselves a
9130 chance to provide an error about returning a value from a void
9132 if (error_operand_p (retval
))
9134 current_function_return_value
= error_mark_node
;
9135 return error_mark_node
;
9138 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
9139 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl
))
9140 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
9142 && retval
&& null_ptr_cst_p (retval
))
9143 warning (0, "%<operator new%> must not return NULL unless it is "
9144 "declared %<throw()%> (or -fcheck-new is in effect)");
9146 /* Effective C++ rule 15. See also start_function. */
9148 && DECL_NAME (current_function_decl
) == assign_op_identifier
)
9152 /* The function return type must be a reference to the current
9154 if (TREE_CODE (valtype
) == REFERENCE_TYPE
9155 && same_type_ignoring_top_level_qualifiers_p
9156 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
)))
9158 /* Returning '*this' is obviously OK. */
9159 if (retval
== current_class_ref
)
9161 /* If we are calling a function whose return type is the same of
9162 the current class reference, it is ok. */
9163 else if (INDIRECT_REF_P (retval
)
9164 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
9169 warning (OPT_Weffc__
, "%<operator=%> should return a reference to %<*this%>");
9172 if (dependent_type_p (functype
)
9173 || type_dependent_expression_p (retval
))
9176 /* We should not have changed the return value. */
9177 gcc_assert (retval
== saved_retval
);
9181 /* The fabled Named Return Value optimization, as per [class.copy]/15:
9183 [...] For a function with a class return type, if the expression
9184 in the return statement is the name of a local object, and the cv-
9185 unqualified type of the local object is the same as the function
9186 return type, an implementation is permitted to omit creating the tem-
9187 porary object to hold the function return value [...]
9189 So, if this is a value-returning function that always returns the same
9190 local variable, remember it.
9192 It might be nice to be more flexible, and choose the first suitable
9193 variable even if the function sometimes returns something else, but
9194 then we run the risk of clobbering the variable we chose if the other
9195 returned expression uses the chosen variable somehow. And people expect
9196 this restriction, anyway. (jason 2000-11-19)
9198 See finish_function and finalize_nrv for the rest of this optimization. */
9200 named_return_value_okay_p
=
9201 (retval
!= NULL_TREE
9202 && !processing_template_decl
9203 /* Must be a local, automatic variable. */
9205 && DECL_CONTEXT (retval
) == current_function_decl
9206 && ! TREE_STATIC (retval
)
9207 /* And not a lambda or anonymous union proxy. */
9208 && !DECL_HAS_VALUE_EXPR_P (retval
)
9209 && (DECL_ALIGN (retval
) <= DECL_ALIGN (result
))
9210 /* The cv-unqualified type of the returned value must be the
9211 same as the cv-unqualified return type of the
9213 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval
))),
9214 (TYPE_MAIN_VARIANT (functype
)))
9215 /* And the returned value must be non-volatile. */
9216 && ! TYPE_VOLATILE (TREE_TYPE (retval
)));
9218 if (fn_returns_value_p
&& flag_elide_constructors
)
9220 if (named_return_value_okay_p
9221 && (current_function_return_value
== NULL_TREE
9222 || current_function_return_value
== retval
))
9223 current_function_return_value
= retval
;
9225 current_function_return_value
= error_mark_node
;
9228 /* We don't need to do any conversions when there's nothing being
9233 /* Do any required conversions. */
9234 if (retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
9235 /* No conversions are required. */
9239 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
9241 /* The functype's return type will have been set to void, if it
9242 was an incomplete type. Just treat this as 'return;' */
9243 if (VOID_TYPE_P (functype
))
9244 return error_mark_node
;
9246 /* If we had an id-expression obfuscated by force_paren_expr, we need
9247 to undo it so we can try to treat it as an rvalue below. */
9248 retval
= maybe_undo_parenthesized_ref (retval
);
9250 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
9251 treated as an rvalue for the purposes of overload resolution to
9252 favor move constructors over copy constructors.
9254 Note that these conditions are similar to, but not as strict as,
9255 the conditions for the named return value optimization. */
9256 bool converted
= false;
9257 if ((cxx_dialect
!= cxx98
)
9258 && ((VAR_P (retval
) && !DECL_HAS_VALUE_EXPR_P (retval
))
9259 || TREE_CODE (retval
) == PARM_DECL
)
9260 && DECL_CONTEXT (retval
) == current_function_decl
9261 && !TREE_STATIC (retval
)
9262 /* This is only interesting for class type. */
9263 && CLASS_TYPE_P (functype
))
9265 tree moved
= move (retval
);
9266 moved
= convert_for_initialization
9267 (NULL_TREE
, functype
, moved
, flags
|LOOKUP_PREFER_RVALUE
,
9268 ICR_RETURN
, NULL_TREE
, 0, tf_none
);
9269 if (moved
!= error_mark_node
)
9276 /* First convert the value to the function's return type, then
9277 to the type of return value's location to handle the
9278 case that functype is smaller than the valtype. */
9280 retval
= convert_for_initialization
9281 (NULL_TREE
, functype
, retval
, flags
, ICR_RETURN
, NULL_TREE
, 0,
9282 tf_warning_or_error
);
9283 retval
= convert (valtype
, retval
);
9285 /* If the conversion failed, treat this just like `return;'. */
9286 if (retval
== error_mark_node
)
9288 /* We can't initialize a register from a AGGR_INIT_EXPR. */
9289 else if (! cfun
->returns_struct
9290 && TREE_CODE (retval
) == TARGET_EXPR
9291 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
9292 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
9293 TREE_OPERAND (retval
, 0));
9294 else if (!processing_template_decl
9295 && maybe_warn_about_returning_address_of_local (retval
))
9296 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
9297 build_zero_cst (TREE_TYPE (retval
)));
9300 if (processing_template_decl
)
9301 return saved_retval
;
9303 /* Actually copy the value returned into the appropriate location. */
9304 if (retval
&& retval
!= result
)
9305 retval
= build2 (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
9311 /* Returns nonzero if the pointer-type FROM can be converted to the
9312 pointer-type TO via a qualification conversion. If CONSTP is -1,
9313 then we return nonzero if the pointers are similar, and the
9314 cv-qualification signature of FROM is a proper subset of that of TO.
9316 If CONSTP is positive, then all outer pointers have been
9320 comp_ptr_ttypes_real (tree to
, tree from
, int constp
)
9322 bool to_more_cv_qualified
= false;
9323 bool is_opaque_pointer
= false;
9325 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9327 if (TREE_CODE (to
) != TREE_CODE (from
))
9330 if (TREE_CODE (from
) == OFFSET_TYPE
9331 && !same_type_p (TYPE_OFFSET_BASETYPE (from
),
9332 TYPE_OFFSET_BASETYPE (to
)))
9335 /* Const and volatile mean something different for function types,
9336 so the usual checks are not appropriate. */
9337 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
9339 if (!at_least_as_qualified_p (to
, from
))
9342 if (!at_least_as_qualified_p (from
, to
))
9346 to_more_cv_qualified
= true;
9350 constp
&= TYPE_READONLY (to
);
9353 if (VECTOR_TYPE_P (to
))
9354 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
9356 if (!TYPE_PTR_P (to
) && !TYPE_PTRDATAMEM_P (to
))
9357 return ((constp
>= 0 || to_more_cv_qualified
)
9358 && (is_opaque_pointer
9359 || same_type_ignoring_top_level_qualifiers_p (to
, from
)));
9363 /* When comparing, say, char ** to char const **, this function takes
9364 the 'char *' and 'char const *'. Do not pass non-pointer/reference
9365 types to this function. */
9368 comp_ptr_ttypes (tree to
, tree from
)
9370 return comp_ptr_ttypes_real (to
, from
, 1);
9373 /* Returns true iff FNTYPE is a non-class type that involves
9374 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9375 if a parameter type is ill-formed. */
9378 error_type_p (const_tree type
)
9382 switch (TREE_CODE (type
))
9388 case REFERENCE_TYPE
:
9390 return error_type_p (TREE_TYPE (type
));
9394 if (error_type_p (TREE_TYPE (type
)))
9396 for (t
= TYPE_ARG_TYPES (type
); t
; t
= TREE_CHAIN (t
))
9397 if (error_type_p (TREE_VALUE (t
)))
9402 if (TYPE_PTRMEMFUNC_P (type
))
9403 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type
));
9411 /* Returns true if to and from are (possibly multi-level) pointers to the same
9412 type or inheritance-related types, regardless of cv-quals. */
9415 ptr_reasonably_similar (const_tree to
, const_tree from
)
9417 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9419 /* Any target type is similar enough to void. */
9420 if (VOID_TYPE_P (to
))
9421 return !error_type_p (from
);
9422 if (VOID_TYPE_P (from
))
9423 return !error_type_p (to
);
9425 if (TREE_CODE (to
) != TREE_CODE (from
))
9428 if (TREE_CODE (from
) == OFFSET_TYPE
9429 && comptypes (TYPE_OFFSET_BASETYPE (to
),
9430 TYPE_OFFSET_BASETYPE (from
),
9431 COMPARE_BASE
| COMPARE_DERIVED
))
9434 if (VECTOR_TYPE_P (to
)
9435 && vector_types_convertible_p (to
, from
, false))
9438 if (TREE_CODE (to
) == INTEGER_TYPE
9439 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
9442 if (TREE_CODE (to
) == FUNCTION_TYPE
)
9443 return !error_type_p (to
) && !error_type_p (from
);
9445 if (!TYPE_PTR_P (to
))
9447 /* When either type is incomplete avoid DERIVED_FROM_P,
9448 which may call complete_type (c++/57942). */
9449 bool b
= !COMPLETE_TYPE_P (to
) || !COMPLETE_TYPE_P (from
);
9451 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
9452 b
? COMPARE_STRICT
: COMPARE_BASE
| COMPARE_DERIVED
);
9457 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9458 pointer-to-member types) are the same, ignoring cv-qualification at
9462 comp_ptr_ttypes_const (tree to
, tree from
)
9464 bool is_opaque_pointer
= false;
9466 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
9468 if (TREE_CODE (to
) != TREE_CODE (from
))
9471 if (TREE_CODE (from
) == OFFSET_TYPE
9472 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
9473 TYPE_OFFSET_BASETYPE (to
)))
9476 if (VECTOR_TYPE_P (to
))
9477 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
9479 if (!TYPE_PTR_P (to
))
9480 return (is_opaque_pointer
9481 || same_type_ignoring_top_level_qualifiers_p (to
, from
));
9485 /* Returns the type qualifiers for this type, including the qualifiers on the
9486 elements for an array type. */
9489 cp_type_quals (const_tree type
)
9492 /* This CONST_CAST is okay because strip_array_types returns its
9493 argument unmodified and we assign it to a const_tree. */
9494 type
= strip_array_types (CONST_CAST_TREE (type
));
9495 if (type
== error_mark_node
9496 /* Quals on a FUNCTION_TYPE are memfn quals. */
9497 || TREE_CODE (type
) == FUNCTION_TYPE
)
9498 return TYPE_UNQUALIFIED
;
9499 quals
= TYPE_QUALS (type
);
9500 /* METHOD and REFERENCE_TYPEs should never have quals. */
9501 gcc_assert ((TREE_CODE (type
) != METHOD_TYPE
9502 && TREE_CODE (type
) != REFERENCE_TYPE
)
9503 || ((quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
))
9504 == TYPE_UNQUALIFIED
));
9508 /* Returns the function-ref-qualifier for TYPE */
9511 type_memfn_rqual (const_tree type
)
9513 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
9514 || TREE_CODE (type
) == METHOD_TYPE
);
9516 if (!FUNCTION_REF_QUALIFIED (type
))
9517 return REF_QUAL_NONE
;
9518 else if (FUNCTION_RVALUE_QUALIFIED (type
))
9519 return REF_QUAL_RVALUE
;
9521 return REF_QUAL_LVALUE
;
9524 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9528 type_memfn_quals (const_tree type
)
9530 if (TREE_CODE (type
) == FUNCTION_TYPE
)
9531 return TYPE_QUALS (type
);
9532 else if (TREE_CODE (type
) == METHOD_TYPE
)
9533 return cp_type_quals (class_of_this_parm (type
));
9538 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9539 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9542 apply_memfn_quals (tree type
, cp_cv_quals memfn_quals
, cp_ref_qualifier rqual
)
9544 /* Could handle METHOD_TYPE here if necessary. */
9545 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
9546 if (TYPE_QUALS (type
) == memfn_quals
9547 && type_memfn_rqual (type
) == rqual
)
9550 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9552 tree result
= build_qualified_type (type
, memfn_quals
);
9553 return build_ref_qualified_type (result
, rqual
);
9556 /* Returns nonzero if TYPE is const or volatile. */
9559 cv_qualified_p (const_tree type
)
9561 int quals
= cp_type_quals (type
);
9562 return (quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)) != 0;
9565 /* Returns nonzero if the TYPE contains a mutable member. */
9568 cp_has_mutable_p (const_tree type
)
9570 /* This CONST_CAST is okay because strip_array_types returns its
9571 argument unmodified and we assign it to a const_tree. */
9572 type
= strip_array_types (CONST_CAST_TREE(type
));
9574 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
9577 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9578 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9579 approximation. In particular, consider:
9582 struct S { int i; };
9583 const S s = { f(); }
9585 Here, we will make "s" as TREE_READONLY (because it is declared
9586 "const") -- only to reverse ourselves upon seeing that the
9587 initializer is non-constant. */
9590 cp_apply_type_quals_to_decl (int type_quals
, tree decl
)
9592 tree type
= TREE_TYPE (decl
);
9594 if (type
== error_mark_node
)
9597 if (TREE_CODE (decl
) == TYPE_DECL
)
9600 gcc_assert (!(TREE_CODE (type
) == FUNCTION_TYPE
9601 && type_quals
!= TYPE_UNQUALIFIED
));
9603 /* Avoid setting TREE_READONLY incorrectly. */
9604 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9605 constructor can produce constant init, so rely on cp_finish_decl to
9606 clear TREE_READONLY if the variable has non-constant init. */
9608 /* If the type has (or might have) a mutable component, that component
9609 might be modified. */
9610 if (TYPE_HAS_MUTABLE_P (type
) || !COMPLETE_TYPE_P (type
))
9611 type_quals
&= ~TYPE_QUAL_CONST
;
9613 c_apply_type_quals_to_decl (type_quals
, decl
);
9616 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9617 exemplar types such that casting T1 to T2 is casting away constness
9618 if and only if there is no implicit conversion from T1 to T2. */
9621 casts_away_constness_r (tree
*t1
, tree
*t2
, tsubst_flags_t complain
)
9626 /* [expr.const.cast]
9628 For multi-level pointer to members and multi-level mixed pointers
9629 and pointers to members (conv.qual), the "member" aspect of a
9630 pointer to member level is ignored when determining if a const
9631 cv-qualifier has been cast away. */
9632 /* [expr.const.cast]
9634 For two pointer types:
9636 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9637 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9640 casting from X1 to X2 casts away constness if, for a non-pointer
9641 type T there does not exist an implicit conversion (clause
9644 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9648 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9649 if ((!TYPE_PTR_P (*t1
) && !TYPE_PTRDATAMEM_P (*t1
))
9650 || (!TYPE_PTR_P (*t2
) && !TYPE_PTRDATAMEM_P (*t2
)))
9652 *t1
= cp_build_qualified_type (void_type_node
,
9653 cp_type_quals (*t1
));
9654 *t2
= cp_build_qualified_type (void_type_node
,
9655 cp_type_quals (*t2
));
9659 quals1
= cp_type_quals (*t1
);
9660 quals2
= cp_type_quals (*t2
);
9662 if (TYPE_PTRDATAMEM_P (*t1
))
9663 *t1
= TYPE_PTRMEM_POINTED_TO_TYPE (*t1
);
9665 *t1
= TREE_TYPE (*t1
);
9666 if (TYPE_PTRDATAMEM_P (*t2
))
9667 *t2
= TYPE_PTRMEM_POINTED_TO_TYPE (*t2
);
9669 *t2
= TREE_TYPE (*t2
);
9671 casts_away_constness_r (t1
, t2
, complain
);
9672 *t1
= build_pointer_type (*t1
);
9673 *t2
= build_pointer_type (*t2
);
9674 *t1
= cp_build_qualified_type (*t1
, quals1
);
9675 *t2
= cp_build_qualified_type (*t2
, quals2
);
9678 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9681 ??? This function returns non-zero if casting away qualifiers not
9682 just const. We would like to return to the caller exactly which
9683 qualifiers are casted away to give more accurate diagnostics.
9687 casts_away_constness (tree t1
, tree t2
, tsubst_flags_t complain
)
9689 if (TREE_CODE (t2
) == REFERENCE_TYPE
)
9691 /* [expr.const.cast]
9693 Casting from an lvalue of type T1 to an lvalue of type T2
9694 using a reference cast casts away constness if a cast from an
9695 rvalue of type "pointer to T1" to the type "pointer to T2"
9696 casts away constness. */
9697 t1
= (TREE_CODE (t1
) == REFERENCE_TYPE
? TREE_TYPE (t1
) : t1
);
9698 return casts_away_constness (build_pointer_type (t1
),
9699 build_pointer_type (TREE_TYPE (t2
)),
9703 if (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
9704 /* [expr.const.cast]
9706 Casting from an rvalue of type "pointer to data member of X
9707 of type T1" to the type "pointer to data member of Y of type
9708 T2" casts away constness if a cast from an rvalue of type
9709 "pointer to T1" to the type "pointer to T2" casts away
9711 return casts_away_constness
9712 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1
)),
9713 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2
)),
9716 /* Casting away constness is only something that makes sense for
9717 pointer or reference types. */
9718 if (!TYPE_PTR_P (t1
) || !TYPE_PTR_P (t2
))
9721 /* Top-level qualifiers don't matter. */
9722 t1
= TYPE_MAIN_VARIANT (t1
);
9723 t2
= TYPE_MAIN_VARIANT (t2
);
9724 casts_away_constness_r (&t1
, &t2
, complain
);
9725 if (!can_convert (t2
, t1
, complain
))
9731 /* If T is a REFERENCE_TYPE return the type to which T refers.
9732 Otherwise, return T itself. */
9735 non_reference (tree t
)
9737 if (t
&& TREE_CODE (t
) == REFERENCE_TYPE
)
9743 /* Return nonzero if REF is an lvalue valid for this language;
9744 otherwise, print an error message and return zero. USE says
9745 how the lvalue is being used and so selects the error message. */
9748 lvalue_or_else (tree ref
, enum lvalue_use use
, tsubst_flags_t complain
)
9750 cp_lvalue_kind kind
= lvalue_kind (ref
);
9752 if (kind
== clk_none
)
9754 if (complain
& tf_error
)
9755 lvalue_error (input_location
, use
);
9758 else if (kind
& (clk_rvalueref
|clk_class
))
9760 if (!(complain
& tf_error
))
9762 if (kind
& clk_class
)
9763 /* Make this a permerror because we used to accept it. */
9764 permerror (input_location
, "using temporary as lvalue");
9766 error ("using xvalue (rvalue reference) as lvalue");
9771 /* Return true if a user-defined literal operator is a raw operator. */
9774 check_raw_literal_operator (const_tree decl
)
9776 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9779 bool maybe_raw_p
= false;
9781 /* Count the number and type of arguments and check for ellipsis. */
9782 for (argtype
= argtypes
, arity
= 0;
9783 argtype
&& argtype
!= void_list_node
;
9784 ++arity
, argtype
= TREE_CHAIN (argtype
))
9786 tree t
= TREE_VALUE (argtype
);
9788 if (same_type_p (t
, const_string_type_node
))
9792 return false; /* Found ellipsis. */
9794 if (!maybe_raw_p
|| arity
!= 1)
9801 /* Return true if a user-defined literal operator has one of the allowed
9805 check_literal_operator_args (const_tree decl
,
9806 bool *long_long_unsigned_p
, bool *long_double_p
)
9808 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9810 *long_long_unsigned_p
= false;
9811 *long_double_p
= false;
9812 if (processing_template_decl
|| processing_specialization
)
9813 return argtypes
== void_list_node
;
9820 /* Count the number and type of arguments and check for ellipsis. */
9821 for (argtype
= argtypes
, arity
= 0;
9822 argtype
&& argtype
!= void_list_node
;
9823 argtype
= TREE_CHAIN (argtype
))
9825 tree t
= TREE_VALUE (argtype
);
9830 bool maybe_raw_p
= false;
9832 if (cp_type_quals (t
) != TYPE_QUAL_CONST
)
9834 t
= TYPE_MAIN_VARIANT (t
);
9835 if ((maybe_raw_p
= same_type_p (t
, char_type_node
))
9836 || same_type_p (t
, wchar_type_node
)
9837 || same_type_p (t
, char16_type_node
)
9838 || same_type_p (t
, char32_type_node
))
9840 argtype
= TREE_CHAIN (argtype
);
9843 t
= TREE_VALUE (argtype
);
9844 if (maybe_raw_p
&& argtype
== void_list_node
)
9846 else if (same_type_p (t
, size_type_node
))
9855 else if (same_type_p (t
, long_long_unsigned_type_node
))
9858 *long_long_unsigned_p
= true;
9860 else if (same_type_p (t
, long_double_type_node
))
9863 *long_double_p
= true;
9865 else if (same_type_p (t
, char_type_node
))
9867 else if (same_type_p (t
, wchar_type_node
))
9869 else if (same_type_p (t
, char16_type_node
))
9871 else if (same_type_p (t
, char32_type_node
))
9877 return false; /* Found ellipsis. */
9879 if (arity
!= max_arity
)
9886 /* Always returns false since unlike C90, C++ has no concept of implicit
9887 function declarations. */
9890 c_decl_implicit (const_tree
)