1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2014 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 "diagnostic.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "c-family/c-ubsan.h"
45 static tree
pfn_from_ptrmemfunc (tree
);
46 static tree
delta_from_ptrmemfunc (tree
);
47 static tree
convert_for_assignment (tree
, tree
, impl_conv_rhs
, tree
, int,
49 static tree
cp_pointer_int_sum (enum tree_code
, tree
, tree
, tsubst_flags_t
);
50 static tree
rationalize_conditional_expr (enum tree_code
, tree
,
52 static int comp_ptr_ttypes_real (tree
, tree
, int);
53 static bool comp_except_types (tree
, tree
, bool);
54 static bool comp_array_types (const_tree
, const_tree
, bool);
55 static tree
pointer_diff (tree
, tree
, tree
, tsubst_flags_t
);
56 static tree
get_delta_difference (tree
, tree
, bool, bool, tsubst_flags_t
);
57 static void casts_away_constness_r (tree
*, tree
*, tsubst_flags_t
);
58 static bool casts_away_constness (tree
, tree
, tsubst_flags_t
);
59 static void maybe_warn_about_returning_address_of_local (tree
);
60 static tree
lookup_destructor (tree
, tree
, tree
, tsubst_flags_t
);
61 static void warn_args_num (location_t
, tree
, bool);
62 static int convert_arguments (tree
, vec
<tree
, va_gc
> **, tree
, int,
65 /* Do `exp = require_complete_type (exp);' to make sure exp
66 does not have an incomplete type. (That includes void types.)
67 Returns error_mark_node if the VALUE does not have
68 complete type when this function returns. */
71 require_complete_type_sfinae (tree value
, tsubst_flags_t complain
)
75 if (processing_template_decl
|| value
== error_mark_node
)
78 if (TREE_CODE (value
) == OVERLOAD
)
79 type
= unknown_type_node
;
81 type
= TREE_TYPE (value
);
83 if (type
== error_mark_node
)
84 return error_mark_node
;
86 /* First, detect a valid value with a complete type. */
87 if (COMPLETE_TYPE_P (type
))
90 if (complete_type_or_maybe_complain (type
, value
, complain
))
93 return error_mark_node
;
97 require_complete_type (tree value
)
99 return require_complete_type_sfinae (value
, tf_warning_or_error
);
102 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
103 a template instantiation, do the instantiation. Returns TYPE,
104 whether or not it could be completed, unless something goes
105 horribly wrong, in which case the error_mark_node is returned. */
108 complete_type (tree type
)
110 if (type
== NULL_TREE
)
111 /* Rather than crash, we return something sure to cause an error
113 return error_mark_node
;
115 if (type
== error_mark_node
|| COMPLETE_TYPE_P (type
))
117 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
119 tree t
= complete_type (TREE_TYPE (type
));
120 unsigned int needs_constructing
, has_nontrivial_dtor
;
121 if (COMPLETE_TYPE_P (t
) && !dependent_type_p (type
))
124 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
126 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
127 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
129 TYPE_NEEDS_CONSTRUCTING (t
) = needs_constructing
;
130 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = has_nontrivial_dtor
;
133 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
134 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
139 /* Like complete_type, but issue an error if the TYPE cannot be completed.
140 VALUE is used for informative diagnostics.
141 Returns NULL_TREE if the type cannot be made complete. */
144 complete_type_or_maybe_complain (tree type
, tree value
, tsubst_flags_t complain
)
146 type
= complete_type (type
);
147 if (type
== error_mark_node
)
148 /* We already issued an error. */
150 else if (!COMPLETE_TYPE_P (type
))
152 if (complain
& tf_error
)
153 cxx_incomplete_type_diagnostic (value
, type
, DK_ERROR
);
161 complete_type_or_else (tree type
, tree value
)
163 return complete_type_or_maybe_complain (type
, value
, tf_warning_or_error
);
166 /* Return truthvalue of whether type of EXP is instantiated. */
169 type_unknown_p (const_tree exp
)
171 return (TREE_CODE (exp
) == TREE_LIST
172 || TREE_TYPE (exp
) == unknown_type_node
);
176 /* Return the common type of two parameter lists.
177 We assume that comptypes has already been done and returned 1;
178 if that isn't so, this may crash.
180 As an optimization, free the space we allocate if the parameter
181 lists are already common. */
184 commonparms (tree p1
, tree p2
)
186 tree oldargs
= p1
, newargs
, n
;
190 len
= list_length (p1
);
191 newargs
= tree_last (p1
);
193 if (newargs
== void_list_node
)
202 newargs
= tree_cons (NULL_TREE
, NULL_TREE
, newargs
);
207 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
), n
= TREE_CHAIN (n
), i
++)
209 if (TREE_PURPOSE (p1
) && !TREE_PURPOSE (p2
))
211 TREE_PURPOSE (n
) = TREE_PURPOSE (p1
);
214 else if (! TREE_PURPOSE (p1
))
216 if (TREE_PURPOSE (p2
))
218 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
224 if (1 != simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)))
226 TREE_PURPOSE (n
) = TREE_PURPOSE (p2
);
228 if (TREE_VALUE (p1
) != TREE_VALUE (p2
))
231 TREE_VALUE (n
) = merge_types (TREE_VALUE (p1
), TREE_VALUE (p2
));
234 TREE_VALUE (n
) = TREE_VALUE (p1
);
242 /* Given a type, perhaps copied for a typedef,
243 find the "original" version of it. */
245 original_type (tree t
)
247 int quals
= cp_type_quals (t
);
248 while (t
!= error_mark_node
249 && TYPE_NAME (t
) != NULL_TREE
)
251 tree x
= TYPE_NAME (t
);
252 if (TREE_CODE (x
) != TYPE_DECL
)
254 x
= DECL_ORIGINAL_TYPE (x
);
259 return cp_build_qualified_type (t
, quals
);
262 /* Return the common type for two arithmetic types T1 and T2 under the
263 usual arithmetic conversions. The default conversions have already
264 been applied, and enumerated types converted to their compatible
268 cp_common_type (tree t1
, tree t2
)
270 enum tree_code code1
= TREE_CODE (t1
);
271 enum tree_code code2
= TREE_CODE (t2
);
275 /* In what follows, we slightly generalize the rules given in [expr] so
276 as to deal with `long long' and `complex'. First, merge the
278 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
280 if (SCOPED_ENUM_P (t1
) || SCOPED_ENUM_P (t2
))
282 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
283 return build_type_attribute_variant (t1
, attributes
);
288 /* FIXME: Attributes. */
289 gcc_assert (ARITHMETIC_TYPE_P (t1
)
290 || TREE_CODE (t1
) == VECTOR_TYPE
291 || UNSCOPED_ENUM_P (t1
));
292 gcc_assert (ARITHMETIC_TYPE_P (t2
)
293 || TREE_CODE (t2
) == VECTOR_TYPE
294 || UNSCOPED_ENUM_P (t2
));
296 /* If one type is complex, form the common type of the non-complex
297 components, then make that complex. Use T1 or T2 if it is the
299 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
301 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
302 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
304 = type_after_usual_arithmetic_conversions (subtype1
, subtype2
);
306 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
307 return build_type_attribute_variant (t1
, attributes
);
308 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
309 return build_type_attribute_variant (t2
, attributes
);
311 return build_type_attribute_variant (build_complex_type (subtype
),
315 if (code1
== VECTOR_TYPE
)
317 /* When we get here we should have two vectors of the same size.
318 Just prefer the unsigned one if present. */
319 if (TYPE_UNSIGNED (t1
))
320 return build_type_attribute_variant (t1
, attributes
);
322 return build_type_attribute_variant (t2
, attributes
);
325 /* If only one is real, use it as the result. */
326 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
327 return build_type_attribute_variant (t1
, attributes
);
328 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
329 return build_type_attribute_variant (t2
, attributes
);
331 /* Both real or both integers; use the one with greater precision. */
332 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
333 return build_type_attribute_variant (t1
, attributes
);
334 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
335 return build_type_attribute_variant (t2
, attributes
);
337 /* The types are the same; no need to do anything fancy. */
338 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
339 return build_type_attribute_variant (t1
, attributes
);
341 if (code1
!= REAL_TYPE
)
343 /* If one is unsigned long long, then convert the other to unsigned
345 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_unsigned_type_node
)
346 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_unsigned_type_node
))
347 return build_type_attribute_variant (long_long_unsigned_type_node
,
349 /* If one is a long long, and the other is an unsigned long, and
350 long long can represent all the values of an unsigned long, then
351 convert to a long long. Otherwise, convert to an unsigned long
352 long. Otherwise, if either operand is long long, convert the
355 Since we're here, we know the TYPE_PRECISION is the same;
356 therefore converting to long long cannot represent all the values
357 of an unsigned long, so we choose unsigned long long in that
359 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_integer_type_node
)
360 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_integer_type_node
))
362 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
363 ? long_long_unsigned_type_node
364 : long_long_integer_type_node
);
365 return build_type_attribute_variant (t
, attributes
);
367 if (int128_integer_type_node
!= NULL_TREE
368 && (same_type_p (TYPE_MAIN_VARIANT (t1
),
369 int128_integer_type_node
)
370 || same_type_p (TYPE_MAIN_VARIANT (t2
),
371 int128_integer_type_node
)))
373 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
374 ? int128_unsigned_type_node
375 : int128_integer_type_node
);
376 return build_type_attribute_variant (t
, attributes
);
379 /* Go through the same procedure, but for longs. */
380 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_unsigned_type_node
)
381 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_unsigned_type_node
))
382 return build_type_attribute_variant (long_unsigned_type_node
,
384 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_integer_type_node
)
385 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_integer_type_node
))
387 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
388 ? long_unsigned_type_node
: long_integer_type_node
);
389 return build_type_attribute_variant (t
, attributes
);
391 /* Otherwise prefer the unsigned one. */
392 if (TYPE_UNSIGNED (t1
))
393 return build_type_attribute_variant (t1
, attributes
);
395 return build_type_attribute_variant (t2
, attributes
);
399 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_double_type_node
)
400 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_double_type_node
))
401 return build_type_attribute_variant (long_double_type_node
,
403 if (same_type_p (TYPE_MAIN_VARIANT (t1
), double_type_node
)
404 || same_type_p (TYPE_MAIN_VARIANT (t2
), double_type_node
))
405 return build_type_attribute_variant (double_type_node
,
407 if (same_type_p (TYPE_MAIN_VARIANT (t1
), float_type_node
)
408 || same_type_p (TYPE_MAIN_VARIANT (t2
), float_type_node
))
409 return build_type_attribute_variant (float_type_node
,
412 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
413 the standard C++ floating-point types. Logic earlier in this
414 function has already eliminated the possibility that
415 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
416 compelling reason to choose one or the other. */
417 return build_type_attribute_variant (t1
, attributes
);
421 /* T1 and T2 are arithmetic or enumeration types. Return the type
422 that will result from the "usual arithmetic conversions" on T1 and
423 T2 as described in [expr]. */
426 type_after_usual_arithmetic_conversions (tree t1
, tree t2
)
428 gcc_assert (ARITHMETIC_TYPE_P (t1
)
429 || TREE_CODE (t1
) == VECTOR_TYPE
430 || UNSCOPED_ENUM_P (t1
));
431 gcc_assert (ARITHMETIC_TYPE_P (t2
)
432 || TREE_CODE (t2
) == VECTOR_TYPE
433 || UNSCOPED_ENUM_P (t2
));
435 /* Perform the integral promotions. We do not promote real types here. */
436 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1
)
437 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2
))
439 t1
= type_promotes_to (t1
);
440 t2
= type_promotes_to (t2
);
443 return cp_common_type (t1
, t2
);
447 composite_pointer_error (diagnostic_t kind
, tree t1
, tree t2
,
448 composite_pointer_operation operation
)
453 emit_diagnostic (kind
, input_location
, 0,
454 "comparison between "
455 "distinct pointer types %qT and %qT lacks a cast",
459 emit_diagnostic (kind
, input_location
, 0,
460 "conversion between "
461 "distinct pointer types %qT and %qT lacks a cast",
464 case CPO_CONDITIONAL_EXPR
:
465 emit_diagnostic (kind
, input_location
, 0,
466 "conditional expression between "
467 "distinct pointer types %qT and %qT lacks a cast",
475 /* Subroutine of composite_pointer_type to implement the recursive
476 case. See that function for documentation of the parameters. */
479 composite_pointer_type_r (tree t1
, tree t2
,
480 composite_pointer_operation operation
,
481 tsubst_flags_t complain
)
488 /* Determine the types pointed to by T1 and T2. */
491 pointee1
= TREE_TYPE (t1
);
492 pointee2
= TREE_TYPE (t2
);
496 pointee1
= TYPE_PTRMEM_POINTED_TO_TYPE (t1
);
497 pointee2
= TYPE_PTRMEM_POINTED_TO_TYPE (t2
);
502 Otherwise, the composite pointer type is a pointer type
503 similar (_conv.qual_) to the type of one of the operands,
504 with a cv-qualification signature (_conv.qual_) that is the
505 union of the cv-qualification signatures of the operand
507 if (same_type_ignoring_top_level_qualifiers_p (pointee1
, pointee2
))
508 result_type
= pointee1
;
509 else if ((TYPE_PTR_P (pointee1
) && TYPE_PTR_P (pointee2
))
510 || (TYPE_PTRMEM_P (pointee1
) && TYPE_PTRMEM_P (pointee2
)))
512 result_type
= composite_pointer_type_r (pointee1
, pointee2
, operation
,
514 if (result_type
== error_mark_node
)
515 return error_mark_node
;
519 if (complain
& tf_error
)
520 composite_pointer_error (DK_PERMERROR
, t1
, t2
, operation
);
522 return error_mark_node
;
523 result_type
= void_type_node
;
525 result_type
= cp_build_qualified_type (result_type
,
526 (cp_type_quals (pointee1
)
527 | cp_type_quals (pointee2
)));
528 /* If the original types were pointers to members, so is the
530 if (TYPE_PTRMEM_P (t1
))
532 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
533 TYPE_PTRMEM_CLASS_TYPE (t2
)))
535 if (complain
& tf_error
)
536 composite_pointer_error (DK_PERMERROR
, t1
, t2
, operation
);
538 return error_mark_node
;
540 result_type
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
544 result_type
= build_pointer_type (result_type
);
546 /* Merge the attributes. */
547 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
548 return build_type_attribute_variant (result_type
, attributes
);
551 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
552 ARG1 and ARG2 are the values with those types. The OPERATION is to
553 describe the operation between the pointer types,
554 in case an error occurs.
556 This routine also implements the computation of a common type for
557 pointers-to-members as per [expr.eq]. */
560 composite_pointer_type (tree t1
, tree t2
, tree arg1
, tree arg2
,
561 composite_pointer_operation operation
,
562 tsubst_flags_t complain
)
569 If one operand is a null pointer constant, the composite pointer
570 type is the type of the other operand. */
571 if (null_ptr_cst_p (arg1
))
573 if (null_ptr_cst_p (arg2
))
580 If one of the operands has type "pointer to cv1 void*", then
581 the other has type "pointer to cv2T", and the composite pointer
582 type is "pointer to cv12 void", where cv12 is the union of cv1
585 If either type is a pointer to void, make sure it is T1. */
586 if (TYPE_PTR_P (t2
) && VOID_TYPE_P (TREE_TYPE (t2
)))
594 /* Now, if T1 is a pointer to void, merge the qualifiers. */
595 if (TYPE_PTR_P (t1
) && VOID_TYPE_P (TREE_TYPE (t1
)))
600 if (TYPE_PTRFN_P (t2
) && (complain
& tf_error
))
605 pedwarn (input_location
, OPT_Wpedantic
,
606 "ISO C++ forbids comparison between "
607 "pointer of type %<void *%> and pointer-to-function");
610 pedwarn (input_location
, OPT_Wpedantic
,
611 "ISO C++ forbids conversion between "
612 "pointer 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 pointer-to-function");
624 = cp_build_qualified_type (void_type_node
,
625 (cp_type_quals (TREE_TYPE (t1
))
626 | cp_type_quals (TREE_TYPE (t2
))));
627 result_type
= build_pointer_type (result_type
);
628 /* Merge the attributes. */
629 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
630 return build_type_attribute_variant (result_type
, attributes
);
633 if (c_dialect_objc () && TYPE_PTR_P (t1
)
636 if (objc_have_common_type (t1
, t2
, -3, NULL_TREE
))
637 return objc_common_type (t1
, t2
);
640 /* [expr.eq] permits the application of a pointer conversion to
641 bring the pointers to a common type. */
642 if (TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
)
643 && CLASS_TYPE_P (TREE_TYPE (t1
))
644 && CLASS_TYPE_P (TREE_TYPE (t2
))
645 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1
),
648 class1
= TREE_TYPE (t1
);
649 class2
= TREE_TYPE (t2
);
651 if (DERIVED_FROM_P (class1
, class2
))
652 t2
= (build_pointer_type
653 (cp_build_qualified_type (class1
, cp_type_quals (class2
))));
654 else if (DERIVED_FROM_P (class2
, class1
))
655 t1
= (build_pointer_type
656 (cp_build_qualified_type (class2
, cp_type_quals (class1
))));
659 if (complain
& tf_error
)
660 composite_pointer_error (DK_ERROR
, t1
, t2
, operation
);
661 return error_mark_node
;
664 /* [expr.eq] permits the application of a pointer-to-member
665 conversion to change the class type of one of the types. */
666 else if (TYPE_PTRMEM_P (t1
)
667 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
668 TYPE_PTRMEM_CLASS_TYPE (t2
)))
670 class1
= TYPE_PTRMEM_CLASS_TYPE (t1
);
671 class2
= TYPE_PTRMEM_CLASS_TYPE (t2
);
673 if (DERIVED_FROM_P (class1
, class2
))
674 t1
= build_ptrmem_type (class2
, TYPE_PTRMEM_POINTED_TO_TYPE (t1
));
675 else if (DERIVED_FROM_P (class2
, class1
))
676 t2
= build_ptrmem_type (class1
, TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
679 if (complain
& tf_error
)
683 error ("comparison between distinct "
684 "pointer-to-member types %qT and %qT lacks a cast",
688 error ("conversion between distinct "
689 "pointer-to-member types %qT and %qT lacks a cast",
692 case CPO_CONDITIONAL_EXPR
:
693 error ("conditional expression between distinct "
694 "pointer-to-member types %qT and %qT lacks a cast",
700 return error_mark_node
;
704 return composite_pointer_type_r (t1
, t2
, operation
, complain
);
707 /* Return the merged type of two types.
708 We assume that comptypes has already been done and returned 1;
709 if that isn't so, this may crash.
711 This just combines attributes and default arguments; any other
712 differences would cause the two types to compare unalike. */
715 merge_types (tree t1
, tree t2
)
717 enum tree_code code1
;
718 enum tree_code code2
;
721 /* Save time if the two types are the same. */
724 if (original_type (t1
) == original_type (t2
))
727 /* If one type is nonsense, use the other. */
728 if (t1
== error_mark_node
)
730 if (t2
== error_mark_node
)
733 /* Handle merging an auto redeclaration with a previous deduced
738 /* Merge the attributes. */
739 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
741 if (TYPE_PTRMEMFUNC_P (t1
))
742 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
743 if (TYPE_PTRMEMFUNC_P (t2
))
744 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
746 code1
= TREE_CODE (t1
);
747 code2
= TREE_CODE (t2
);
750 gcc_assert (code1
== TYPENAME_TYPE
|| code2
== TYPENAME_TYPE
);
751 if (code1
== TYPENAME_TYPE
)
753 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
754 code1
= TREE_CODE (t1
);
758 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
759 code2
= TREE_CODE (t2
);
767 /* For two pointers, do this recursively on the target type. */
769 tree target
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
770 int quals
= cp_type_quals (t1
);
772 if (code1
== POINTER_TYPE
)
773 t1
= build_pointer_type (target
);
775 t1
= cp_build_reference_type (target
, TYPE_REF_IS_RVALUE (t1
));
776 t1
= build_type_attribute_variant (t1
, attributes
);
777 t1
= cp_build_qualified_type (t1
, quals
);
779 if (TREE_CODE (target
) == METHOD_TYPE
)
780 t1
= build_ptrmemfunc_type (t1
);
789 quals
= cp_type_quals (t1
);
790 pointee
= merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1
),
791 TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
792 t1
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
794 t1
= cp_build_qualified_type (t1
, quals
);
800 tree elt
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
801 /* Save space: see if the result is identical to one of the args. */
802 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
803 return build_type_attribute_variant (t1
, attributes
);
804 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
805 return build_type_attribute_variant (t2
, attributes
);
806 /* Merge the element types, and have a size if either arg has one. */
807 t1
= build_cplus_array_type
808 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
813 /* Function types: prefer the one that specified arg types.
814 If both do, merge the arg types. Also merge the return types. */
816 tree valtype
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
817 tree p1
= TYPE_ARG_TYPES (t1
);
818 tree p2
= TYPE_ARG_TYPES (t2
);
822 /* Save space: see if the result is identical to one of the args. */
823 if (valtype
== TREE_TYPE (t1
) && ! p2
)
824 return cp_build_type_attribute_variant (t1
, attributes
);
825 if (valtype
== TREE_TYPE (t2
) && ! p1
)
826 return cp_build_type_attribute_variant (t2
, attributes
);
828 /* Simple way if one arg fails to specify argument types. */
829 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
831 else if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
834 parms
= commonparms (p1
, p2
);
836 rval
= build_function_type (valtype
, parms
);
837 gcc_assert (type_memfn_quals (t1
) == type_memfn_quals (t2
));
838 gcc_assert (type_memfn_rqual (t1
) == type_memfn_rqual (t2
));
839 rval
= apply_memfn_quals (rval
,
840 type_memfn_quals (t1
),
841 type_memfn_rqual (t1
));
842 raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
843 TYPE_RAISES_EXCEPTIONS (t2
));
844 t1
= build_exception_variant (rval
, raises
);
850 /* Get this value the long way, since TYPE_METHOD_BASETYPE
851 is just the main variant of this. */
852 tree basetype
= class_of_this_parm (t2
);
853 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
854 TYPE_RAISES_EXCEPTIONS (t2
));
855 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
858 /* If this was a member function type, get back to the
859 original type of type member function (i.e., without
860 the class instance variable up front. */
861 t1
= build_function_type (TREE_TYPE (t1
),
862 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
863 t2
= build_function_type (TREE_TYPE (t2
),
864 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
865 t3
= merge_types (t1
, t2
);
866 t3
= build_method_type_directly (basetype
, TREE_TYPE (t3
),
867 TYPE_ARG_TYPES (t3
));
868 t1
= build_exception_variant (t3
, raises
);
869 t1
= build_ref_qualified_type (t1
, rqual
);
874 /* There is no need to merge attributes into a TYPENAME_TYPE.
875 When the type is instantiated it will have whatever
876 attributes result from the instantiation. */
882 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
884 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
887 return cp_build_type_attribute_variant (t1
, attributes
);
890 /* Return the ARRAY_TYPE type without its domain. */
893 strip_array_domain (tree type
)
896 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
897 if (TYPE_DOMAIN (type
) == NULL_TREE
)
899 t2
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
900 return cp_build_type_attribute_variant (t2
, TYPE_ATTRIBUTES (type
));
903 /* Wrapper around cp_common_type that is used by c-common.c and other
904 front end optimizations that remove promotions.
906 Return the common type for two arithmetic types T1 and T2 under the
907 usual arithmetic conversions. The default conversions have already
908 been applied, and enumerated types converted to their compatible
912 common_type (tree t1
, tree t2
)
914 /* If one type is nonsense, use the other */
915 if (t1
== error_mark_node
)
917 if (t2
== error_mark_node
)
920 return cp_common_type (t1
, t2
);
923 /* Return the common type of two pointer types T1 and T2. This is the
924 type for the result of most arithmetic operations if the operands
925 have the given two types.
927 We assume that comp_target_types has already been done and returned
928 nonzero; if that isn't so, this may crash. */
931 common_pointer_type (tree t1
, tree t2
)
933 gcc_assert ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
934 || (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
935 || (TYPE_PTRMEMFUNC_P (t1
) && TYPE_PTRMEMFUNC_P (t2
)));
937 return composite_pointer_type (t1
, t2
, error_mark_node
, error_mark_node
,
938 CPO_CONVERSION
, tf_warning_or_error
);
941 /* Compare two exception specifier types for exactness or subsetness, if
942 allowed. Returns false for mismatch, true for match (same, or
945 [except.spec] "If a class X ... objects of class X or any class publicly
946 and unambiguously derived from X. Similarly, if a pointer type Y * ...
947 exceptions of type Y * or that are pointers to any type publicly and
948 unambiguously derived from Y. Otherwise a function only allows exceptions
949 that have the same type ..."
950 This does not mention cv qualifiers and is different to what throw
951 [except.throw] and catch [except.catch] will do. They will ignore the
952 top level cv qualifiers, and allow qualifiers in the pointer to class
955 We implement the letter of the standard. */
958 comp_except_types (tree a
, tree b
, bool exact
)
960 if (same_type_p (a
, b
))
964 if (cp_type_quals (a
) || cp_type_quals (b
))
967 if (TYPE_PTR_P (a
) && TYPE_PTR_P (b
))
971 if (cp_type_quals (a
) || cp_type_quals (b
))
975 if (TREE_CODE (a
) != RECORD_TYPE
976 || TREE_CODE (b
) != RECORD_TYPE
)
979 if (publicly_uniquely_derived_p (a
, b
))
985 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
986 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
987 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
988 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
989 are unordered, but we've already filtered out duplicates. Most lists will
990 be in order, we should try to make use of that. */
993 comp_except_specs (const_tree t1
, const_tree t2
, int exact
)
1002 /* First handle noexcept. */
1003 if (exact
< ce_exact
)
1005 /* noexcept(false) is compatible with no exception-specification,
1006 and stricter than any spec. */
1007 if (t1
== noexcept_false_spec
)
1008 return t2
== NULL_TREE
|| exact
== ce_derived
;
1009 /* Even a derived noexcept(false) is compatible with no
1010 exception-specification. */
1011 if (t2
== noexcept_false_spec
)
1012 return t1
== NULL_TREE
;
1014 /* Otherwise, if we aren't looking for an exact match, noexcept is
1015 equivalent to throw(). */
1016 if (t1
== noexcept_true_spec
)
1017 t1
= empty_except_spec
;
1018 if (t2
== noexcept_true_spec
)
1019 t2
= empty_except_spec
;
1022 /* If any noexcept is left, it is only comparable to itself;
1023 either we're looking for an exact match or we're redeclaring a
1024 template with dependent noexcept. */
1025 if ((t1
&& TREE_PURPOSE (t1
))
1026 || (t2
&& TREE_PURPOSE (t2
)))
1028 && cp_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
)));
1030 if (t1
== NULL_TREE
) /* T1 is ... */
1031 return t2
== NULL_TREE
|| exact
== ce_derived
;
1032 if (!TREE_VALUE (t1
)) /* t1 is EMPTY */
1033 return t2
!= NULL_TREE
&& !TREE_VALUE (t2
);
1034 if (t2
== NULL_TREE
) /* T2 is ... */
1036 if (TREE_VALUE (t1
) && !TREE_VALUE (t2
)) /* T2 is EMPTY, T1 is not */
1037 return exact
== ce_derived
;
1039 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1040 Count how many we find, to determine exactness. For exact matching and
1041 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1043 for (base
= t1
; t2
!= NULL_TREE
; t2
= TREE_CHAIN (t2
))
1045 for (probe
= base
; probe
!= NULL_TREE
; probe
= TREE_CHAIN (probe
))
1047 tree a
= TREE_VALUE (probe
);
1048 tree b
= TREE_VALUE (t2
);
1050 if (comp_except_types (a
, b
, exact
))
1052 if (probe
== base
&& exact
> ce_derived
)
1053 base
= TREE_CHAIN (probe
);
1058 if (probe
== NULL_TREE
)
1061 return exact
== ce_derived
|| base
== NULL_TREE
|| length
== list_length (t1
);
1064 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1065 [] can match [size]. */
1068 comp_array_types (const_tree t1
, const_tree t2
, bool allow_redeclaration
)
1077 /* The type of the array elements must be the same. */
1078 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1081 d1
= TYPE_DOMAIN (t1
);
1082 d2
= TYPE_DOMAIN (t2
);
1087 /* If one of the arrays is dimensionless, and the other has a
1088 dimension, they are of different types. However, it is valid to
1096 declarations for an array object can specify
1097 array types that differ by the presence or absence of a major
1098 array bound (_dcl.array_). */
1100 return allow_redeclaration
;
1102 /* Check that the dimensions are the same. */
1104 if (!cp_tree_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
)))
1106 max1
= TYPE_MAX_VALUE (d1
);
1107 max2
= TYPE_MAX_VALUE (d2
);
1108 if (processing_template_decl
&& !abi_version_at_least (2)
1109 && !value_dependent_expression_p (max1
)
1110 && !value_dependent_expression_p (max2
))
1112 /* With abi-1 we do not fold non-dependent array bounds, (and
1113 consequently mangle them incorrectly). We must therefore
1114 fold them here, to verify the domains have the same
1120 if (!cp_tree_equal (max1
, max2
))
1126 /* Compare the relative position of T1 and T2 into their respective
1127 template parameter list.
1128 T1 and T2 must be template parameter types.
1129 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1132 comp_template_parms_position (tree t1
, tree t2
)
1134 tree index1
, index2
;
1135 gcc_assert (t1
&& t2
1136 && TREE_CODE (t1
) == TREE_CODE (t2
)
1137 && (TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
1138 || TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
1139 || TREE_CODE (t1
) == TEMPLATE_TYPE_PARM
));
1141 index1
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1
));
1142 index2
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2
));
1144 /* Then compare their relative position. */
1145 if (TEMPLATE_PARM_IDX (index1
) != TEMPLATE_PARM_IDX (index2
)
1146 || TEMPLATE_PARM_LEVEL (index1
) != TEMPLATE_PARM_LEVEL (index2
)
1147 || (TEMPLATE_PARM_PARAMETER_PACK (index1
)
1148 != TEMPLATE_PARM_PARAMETER_PACK (index2
)))
1151 /* In C++14 we can end up comparing 'auto' to a normal template
1152 parameter. Don't confuse them. */
1153 if (cxx_dialect
>= cxx1y
&& (is_auto (t1
) || is_auto (t2
)))
1154 return TYPE_IDENTIFIER (t1
) == TYPE_IDENTIFIER (t2
);
1159 /* Subroutine in comptypes. */
1162 structural_comptypes (tree t1
, tree t2
, int strict
)
1167 /* Suppress errors caused by previously reported errors. */
1168 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1171 gcc_assert (TYPE_P (t1
) && TYPE_P (t2
));
1173 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1174 current instantiation. */
1175 if (TREE_CODE (t1
) == TYPENAME_TYPE
)
1176 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
1178 if (TREE_CODE (t2
) == TYPENAME_TYPE
)
1179 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
1181 if (TYPE_PTRMEMFUNC_P (t1
))
1182 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
1183 if (TYPE_PTRMEMFUNC_P (t2
))
1184 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
1186 /* Different classes of types can't be compatible. */
1187 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1190 /* Qualifiers must match. For array types, we will check when we
1191 recur on the array element types. */
1192 if (TREE_CODE (t1
) != ARRAY_TYPE
1193 && cp_type_quals (t1
) != cp_type_quals (t2
))
1195 if (TREE_CODE (t1
) == FUNCTION_TYPE
1196 && type_memfn_quals (t1
) != type_memfn_quals (t2
))
1198 /* Need to check this before TYPE_MAIN_VARIANT.
1199 FIXME function qualifiers should really change the main variant. */
1200 if ((TREE_CODE (t1
) == FUNCTION_TYPE
1201 || TREE_CODE (t1
) == METHOD_TYPE
)
1202 && type_memfn_rqual (t1
) != type_memfn_rqual (t2
))
1204 if (TYPE_FOR_JAVA (t1
) != TYPE_FOR_JAVA (t2
))
1207 /* Allow for two different type nodes which have essentially the same
1208 definition. Note that we already checked for equality of the type
1209 qualifiers (just above). */
1211 if (TREE_CODE (t1
) != ARRAY_TYPE
1212 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1216 /* Compare the types. Break out if they could be the same. */
1217 switch (TREE_CODE (t1
))
1221 /* All void and bool types are the same. */
1225 case FIXED_POINT_TYPE
:
1227 /* With these nodes, we can't determine type equivalence by
1228 looking at what is stored in the nodes themselves, because
1229 two nodes might have different TYPE_MAIN_VARIANTs but still
1230 represent the same type. For example, wchar_t and int could
1231 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1232 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1233 and are distinct types. On the other hand, int and the
1236 typedef int INT __attribute((may_alias));
1238 have identical properties, different TYPE_MAIN_VARIANTs, but
1239 represent the same type. The canonical type system keeps
1240 track of equivalence in this case, so we fall back on it. */
1241 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1243 case TEMPLATE_TEMPLATE_PARM
:
1244 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1245 if (!comp_template_parms_position (t1
, t2
))
1247 if (!comp_template_parms
1248 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1
)),
1249 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2
))))
1251 if (TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
)
1253 /* Don't check inheritance. */
1254 strict
= COMPARE_STRICT
;
1259 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
1260 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
1261 || TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1262 && comp_template_args (TYPE_TI_ARGS (t1
), TYPE_TI_ARGS (t2
)))
1265 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
1267 else if ((strict
& COMPARE_DERIVED
) && DERIVED_FROM_P (t2
, t1
))
1273 if (!comptypes (TYPE_OFFSET_BASETYPE (t1
), TYPE_OFFSET_BASETYPE (t2
),
1274 strict
& ~COMPARE_REDECLARATION
))
1276 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1280 case REFERENCE_TYPE
:
1281 if (TYPE_REF_IS_RVALUE (t1
) != TYPE_REF_IS_RVALUE (t2
))
1283 /* fall through to checks for pointer types */
1286 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1287 || TYPE_REF_CAN_ALIAS_ALL (t1
) != TYPE_REF_CAN_ALIAS_ALL (t2
)
1288 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1294 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1296 if (!compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)))
1301 /* Target types must match incl. qualifiers. */
1302 if (!comp_array_types (t1
, t2
, !!(strict
& COMPARE_REDECLARATION
)))
1306 case TEMPLATE_TYPE_PARM
:
1307 /* If T1 and T2 don't have the same relative position in their
1308 template parameters set, they can't be equal. */
1309 if (!comp_template_parms_position (t1
, t2
))
1314 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1
),
1315 TYPENAME_TYPE_FULLNAME (t2
)))
1317 /* Qualifiers don't matter on scopes. */
1318 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1
),
1323 case UNBOUND_CLASS_TEMPLATE
:
1324 if (!cp_tree_equal (TYPE_IDENTIFIER (t1
), TYPE_IDENTIFIER (t2
)))
1326 if (!same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
)))
1331 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1336 if (TYPE_VECTOR_SUBPARTS (t1
) != TYPE_VECTOR_SUBPARTS (t2
)
1337 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1341 case TYPE_PACK_EXPANSION
:
1342 return (same_type_p (PACK_EXPANSION_PATTERN (t1
),
1343 PACK_EXPANSION_PATTERN (t2
))
1344 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1
),
1345 PACK_EXPANSION_EXTRA_ARGS (t2
)));
1348 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1
)
1349 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2
)
1350 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1
)
1351 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2
))
1352 || (DECLTYPE_FOR_LAMBDA_PROXY (t1
)
1353 != DECLTYPE_FOR_LAMBDA_PROXY (t2
))
1354 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1
),
1355 DECLTYPE_TYPE_EXPR (t2
)))
1359 case UNDERLYING_TYPE
:
1360 return same_type_p (UNDERLYING_TYPE_TYPE (t1
),
1361 UNDERLYING_TYPE_TYPE (t2
));
1367 /* If we get here, we know that from a target independent POV the
1368 types are the same. Make sure the target attributes are also
1370 return comp_type_attributes (t1
, t2
);
1373 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1374 is a bitwise-or of the COMPARE_* flags. */
1377 comptypes (tree t1
, tree t2
, int strict
)
1379 if (strict
== COMPARE_STRICT
)
1384 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1387 if (TYPE_STRUCTURAL_EQUALITY_P (t1
) || TYPE_STRUCTURAL_EQUALITY_P (t2
))
1388 /* At least one of the types requires structural equality, so
1389 perform a deep check. */
1390 return structural_comptypes (t1
, t2
, strict
);
1392 #ifdef ENABLE_CHECKING
1393 if (USE_CANONICAL_TYPES
)
1395 bool result
= structural_comptypes (t1
, t2
, strict
);
1397 if (result
&& TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1398 /* The two types are structurally equivalent, but their
1399 canonical types were different. This is a failure of the
1400 canonical type propagation code.*/
1402 ("canonical types differ for identical types %T and %T",
1404 else if (!result
&& TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
))
1405 /* Two types are structurally different, but the canonical
1406 types are the same. This means we were over-eager in
1407 assigning canonical types. */
1409 ("same canonical type node for different types %T and %T",
1415 if (USE_CANONICAL_TYPES
)
1416 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1419 return structural_comptypes (t1
, t2
, strict
);
1421 else if (strict
== COMPARE_STRUCTURAL
)
1422 return structural_comptypes (t1
, t2
, COMPARE_STRICT
);
1424 return structural_comptypes (t1
, t2
, strict
);
1427 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1428 top-level qualifiers. */
1431 same_type_ignoring_top_level_qualifiers_p (tree type1
, tree type2
)
1433 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1436 return same_type_p (TYPE_MAIN_VARIANT (type1
), TYPE_MAIN_VARIANT (type2
));
1439 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1442 at_least_as_qualified_p (const_tree type1
, const_tree type2
)
1444 int q1
= cp_type_quals (type1
);
1445 int q2
= cp_type_quals (type2
);
1447 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1448 return (q1
& q2
) == q2
;
1451 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1452 more cv-qualified that TYPE1, and 0 otherwise. */
1455 comp_cv_qualification (int q1
, int q2
)
1460 if ((q1
& q2
) == q2
)
1462 else if ((q1
& q2
) == q1
)
1469 comp_cv_qualification (const_tree type1
, const_tree type2
)
1471 int q1
= cp_type_quals (type1
);
1472 int q2
= cp_type_quals (type2
);
1473 return comp_cv_qualification (q1
, q2
);
1476 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1477 subset of the cv-qualification signature of TYPE2, and the types
1478 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1481 comp_cv_qual_signature (tree type1
, tree type2
)
1483 if (comp_ptr_ttypes_real (type2
, type1
, -1))
1485 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
1491 /* Subroutines of `comptypes'. */
1493 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1494 equivalent in the sense that functions with those parameter types
1495 can have equivalent types. The two lists must be equivalent,
1496 element by element. */
1499 compparms (const_tree parms1
, const_tree parms2
)
1503 /* An unspecified parmlist matches any specified parmlist
1504 whose argument types don't need default promotions. */
1506 for (t1
= parms1
, t2
= parms2
;
1508 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1510 /* If one parmlist is shorter than the other,
1511 they fail to match. */
1514 if (!same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1521 /* Process a sizeof or alignof expression where the operand is a
1525 cxx_sizeof_or_alignof_type (tree type
, enum tree_code op
, bool complain
)
1530 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
1531 if (type
== error_mark_node
)
1532 return error_mark_node
;
1534 type
= non_reference (type
);
1535 if (TREE_CODE (type
) == METHOD_TYPE
)
1538 pedwarn (input_location
, OPT_Wpointer_arith
,
1539 "invalid application of %qs to a member function",
1540 operator_name_info
[(int) op
].name
);
1541 value
= size_one_node
;
1544 dependent_p
= dependent_type_p (type
);
1546 complete_type (type
);
1548 /* VLA types will have a non-constant size. In the body of an
1549 uninstantiated template, we don't need to try to compute the
1550 value, because the sizeof expression is not an integral
1551 constant expression in that case. And, if we do try to
1552 compute the value, we'll likely end up with SAVE_EXPRs, which
1553 the template substitution machinery does not expect to see. */
1554 || (processing_template_decl
1555 && COMPLETE_TYPE_P (type
)
1556 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
))
1558 value
= build_min (op
, size_type_node
, type
);
1559 TREE_READONLY (value
) = 1;
1563 if (cxx_dialect
>= cxx1y
&& array_of_runtime_bound_p (type
)
1564 && (flag_iso
|| warn_vla
> 0))
1566 if (complain
& tf_warning_or_error
)
1567 pedwarn (input_location
, OPT_Wvla
,
1568 "taking sizeof array of runtime bound");
1570 return error_mark_node
;
1573 return c_sizeof_or_alignof_type (input_location
, complete_type (type
),
1574 op
== SIZEOF_EXPR
, false,
1578 /* Return the size of the type, without producing any warnings for
1579 types whose size cannot be taken. This routine should be used only
1580 in some other routine that has already produced a diagnostic about
1581 using the size of such a type. */
1583 cxx_sizeof_nowarn (tree type
)
1585 if (TREE_CODE (type
) == FUNCTION_TYPE
1586 || VOID_TYPE_P (type
)
1587 || TREE_CODE (type
) == ERROR_MARK
)
1588 return size_one_node
;
1589 else if (!COMPLETE_TYPE_P (type
))
1590 return size_zero_node
;
1592 return cxx_sizeof_or_alignof_type (type
, SIZEOF_EXPR
, false);
1595 /* Process a sizeof expression where the operand is an expression. */
1598 cxx_sizeof_expr (tree e
, tsubst_flags_t complain
)
1600 if (e
== error_mark_node
)
1601 return error_mark_node
;
1603 if (processing_template_decl
)
1605 e
= build_min (SIZEOF_EXPR
, size_type_node
, e
);
1606 TREE_SIDE_EFFECTS (e
) = 0;
1607 TREE_READONLY (e
) = 1;
1612 /* To get the size of a static data member declared as an array of
1613 unknown bound, we need to instantiate it. */
1615 && VAR_HAD_UNKNOWN_BOUND (e
)
1616 && DECL_TEMPLATE_INSTANTIATION (e
))
1617 instantiate_decl (e
, /*defer_ok*/true, /*expl_inst_mem*/false);
1619 e
= mark_type_use (e
);
1621 if (TREE_CODE (e
) == COMPONENT_REF
1622 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
1623 && DECL_C_BIT_FIELD (TREE_OPERAND (e
, 1)))
1625 if (complain
& tf_error
)
1626 error ("invalid application of %<sizeof%> to a bit-field");
1628 return error_mark_node
;
1631 else if (is_overloaded_fn (e
))
1633 if (complain
& tf_error
)
1634 permerror (input_location
, "ISO C++ forbids applying %<sizeof%> to an expression of "
1637 return error_mark_node
;
1640 else if (type_unknown_p (e
))
1642 if (complain
& tf_error
)
1643 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1645 return error_mark_node
;
1651 return cxx_sizeof_or_alignof_type (e
, SIZEOF_EXPR
, complain
& tf_error
);
1654 /* Implement the __alignof keyword: Return the minimum required
1655 alignment of E, measured in bytes. For VAR_DECL's and
1656 FIELD_DECL's return DECL_ALIGN (which can be set from an
1657 "aligned" __attribute__ specification). */
1660 cxx_alignof_expr (tree e
, tsubst_flags_t complain
)
1664 if (e
== error_mark_node
)
1665 return error_mark_node
;
1667 if (processing_template_decl
)
1669 e
= build_min (ALIGNOF_EXPR
, size_type_node
, e
);
1670 TREE_SIDE_EFFECTS (e
) = 0;
1671 TREE_READONLY (e
) = 1;
1676 e
= mark_type_use (e
);
1679 t
= size_int (DECL_ALIGN_UNIT (e
));
1680 else if (TREE_CODE (e
) == COMPONENT_REF
1681 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
1682 && DECL_C_BIT_FIELD (TREE_OPERAND (e
, 1)))
1684 if (complain
& tf_error
)
1685 error ("invalid application of %<__alignof%> to a bit-field");
1687 return error_mark_node
;
1690 else if (TREE_CODE (e
) == COMPONENT_REF
1691 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
)
1692 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e
, 1)));
1693 else if (is_overloaded_fn (e
))
1695 if (complain
& tf_error
)
1696 permerror (input_location
, "ISO C++ forbids applying %<__alignof%> to an expression of "
1699 return error_mark_node
;
1700 if (TREE_CODE (e
) == FUNCTION_DECL
)
1701 t
= size_int (DECL_ALIGN_UNIT (e
));
1705 else if (type_unknown_p (e
))
1707 if (complain
& tf_error
)
1708 cxx_incomplete_type_error (e
, TREE_TYPE (e
));
1710 return error_mark_node
;
1714 return cxx_sizeof_or_alignof_type (TREE_TYPE (e
), ALIGNOF_EXPR
,
1715 complain
& tf_error
);
1717 return fold_convert (size_type_node
, t
);
1720 /* Process a sizeof or alignof expression E with code OP where the operand
1721 is an expression. */
1724 cxx_sizeof_or_alignof_expr (tree e
, enum tree_code op
, bool complain
)
1726 if (op
== SIZEOF_EXPR
)
1727 return cxx_sizeof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1729 return cxx_alignof_expr (e
, complain
? tf_warning_or_error
: tf_none
);
1732 /* Build a representation of an expression 'alignas(E).' Return the
1733 folded integer value of E if it is an integral constant expression
1734 that resolves to a valid alignment. If E depends on a template
1735 parameter, return a syntactic representation tree of kind
1736 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1737 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1740 cxx_alignas_expr (tree e
)
1742 if (e
== NULL_TREE
|| e
== error_mark_node
1743 || (!TYPE_P (e
) && !require_potential_rvalue_constant_expression (e
)))
1749 When the alignment-specifier is of the form
1750 alignas(type-id ), it shall have the same effect as
1751 alignas(alignof(type-id )). */
1753 return cxx_sizeof_or_alignof_type (e
, ALIGNOF_EXPR
, false);
1755 /* If we reach this point, it means the alignas expression if of
1756 the form "alignas(assignment-expression)", so we should follow
1757 what is stated by [dcl.align]/2. */
1759 if (value_dependent_expression_p (e
))
1760 /* Leave value-dependent expression alone for now. */
1763 e
= fold_non_dependent_expr (e
);
1764 e
= mark_rvalue_use (e
);
1766 /* [dcl.align]/2 says:
1768 the assignment-expression shall be an integral constant
1771 return cxx_constant_value (e
);
1775 /* EXPR is being used in a context that is not a function call.
1780 The expression can be used only as the left-hand operand of a
1781 member function call.
1783 [expr.mptr.operator]
1785 If the result of .* or ->* is a function, then that result can be
1786 used only as the operand for the function call operator ().
1788 by issuing an error message if appropriate. Returns true iff EXPR
1789 violates these rules. */
1792 invalid_nonstatic_memfn_p (tree expr
, tsubst_flags_t complain
)
1794 if (expr
== NULL_TREE
)
1796 /* Don't enforce this in MS mode. */
1797 if (flag_ms_extensions
)
1799 if (is_overloaded_fn (expr
) && !really_overloaded_fn (expr
))
1800 expr
= get_first_fn (expr
);
1801 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr
))
1803 if (complain
& tf_error
)
1804 error ("invalid use of non-static member function");
1810 /* If EXP is a reference to a bitfield, and the type of EXP does not
1811 match the declared type of the bitfield, return the declared type
1812 of the bitfield. Otherwise, return NULL_TREE. */
1815 is_bitfield_expr_with_lowered_type (const_tree exp
)
1817 switch (TREE_CODE (exp
))
1820 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1)
1821 ? TREE_OPERAND (exp
, 1)
1822 : TREE_OPERAND (exp
, 0)))
1824 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 2));
1827 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1));
1831 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1837 field
= TREE_OPERAND (exp
, 1);
1838 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_BIT_FIELD_TYPE (field
))
1840 if (same_type_ignoring_top_level_qualifiers_p
1841 (TREE_TYPE (exp
), DECL_BIT_FIELD_TYPE (field
)))
1843 return DECL_BIT_FIELD_TYPE (field
);
1847 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp
, 0)))
1848 == TYPE_MAIN_VARIANT (TREE_TYPE (exp
)))
1849 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
1857 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1858 bitfield with a lowered type, the type of EXP is returned, rather
1862 unlowered_expr_type (const_tree exp
)
1865 tree etype
= TREE_TYPE (exp
);
1867 type
= is_bitfield_expr_with_lowered_type (exp
);
1869 type
= cp_build_qualified_type (type
, cp_type_quals (etype
));
1876 /* Perform the conversions in [expr] that apply when an lvalue appears
1877 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1878 function-to-pointer conversions. In addition, manifest constants
1879 are replaced by their values, and bitfield references are converted
1880 to their declared types. Note that this function does not perform the
1881 lvalue-to-rvalue conversion for class types. If you need that conversion
1882 to for class types, then you probably need to use force_rvalue.
1884 Although the returned value is being used as an rvalue, this
1885 function does not wrap the returned expression in a
1886 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1887 that the return value is no longer an lvalue. */
1890 decay_conversion (tree exp
, tsubst_flags_t complain
)
1893 enum tree_code code
;
1894 location_t loc
= EXPR_LOC_OR_LOC (exp
, input_location
);
1896 type
= TREE_TYPE (exp
);
1897 if (type
== error_mark_node
)
1898 return error_mark_node
;
1900 exp
= mark_rvalue_use (exp
);
1902 exp
= resolve_nondeduced_context (exp
);
1903 if (type_unknown_p (exp
))
1905 if (complain
& tf_error
)
1906 cxx_incomplete_type_error (exp
, TREE_TYPE (exp
));
1907 return error_mark_node
;
1910 code
= TREE_CODE (type
);
1912 /* For an array decl decay_conversion should not try to return its
1914 if (code
!= ARRAY_TYPE
)
1916 /* FIXME remove? at least need to remember that this isn't really a
1917 constant expression if EXP isn't decl_constant_var_p, like with
1918 C_MAYBE_CONST_EXPR. */
1919 exp
= decl_constant_value_safe (exp
);
1920 if (error_operand_p (exp
))
1921 return error_mark_node
;
1924 if (NULLPTR_TYPE_P (type
) && !TREE_SIDE_EFFECTS (exp
))
1925 return nullptr_node
;
1927 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1928 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1929 if (code
== VOID_TYPE
)
1931 if (complain
& tf_error
)
1932 error_at (loc
, "void value not ignored as it ought to be");
1933 return error_mark_node
;
1935 if (invalid_nonstatic_memfn_p (exp
, complain
))
1936 return error_mark_node
;
1937 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
1938 return cp_build_addr_expr (exp
, complain
);
1939 if (code
== ARRAY_TYPE
)
1944 if (INDIRECT_REF_P (exp
))
1945 return build_nop (build_pointer_type (TREE_TYPE (type
)),
1946 TREE_OPERAND (exp
, 0));
1948 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
1950 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1), complain
);
1951 if (op1
== error_mark_node
)
1952 return error_mark_node
;
1953 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
1954 TREE_OPERAND (exp
, 0), op1
);
1958 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
1960 if (complain
& tf_error
)
1961 error_at (loc
, "invalid use of non-lvalue array");
1962 return error_mark_node
;
1965 /* Don't let an array compound literal decay to a pointer. It can
1966 still be used to initialize an array or bind to a reference. */
1967 if (TREE_CODE (exp
) == TARGET_EXPR
)
1969 if (complain
& tf_error
)
1970 error_at (loc
, "taking address of temporary array");
1971 return error_mark_node
;
1974 ptrtype
= build_pointer_type (TREE_TYPE (type
));
1978 if (!cxx_mark_addressable (exp
))
1979 return error_mark_node
;
1980 adr
= build_nop (ptrtype
, build_address (exp
));
1983 /* This way is better for a COMPONENT_REF since it can
1984 simplify the offset for a component. */
1985 adr
= cp_build_addr_expr (exp
, complain
);
1986 return cp_convert (ptrtype
, adr
, complain
);
1989 /* If a bitfield is used in a context where integral promotion
1990 applies, then the caller is expected to have used
1991 default_conversion. That function promotes bitfields correctly
1992 before calling this function. At this point, if we have a
1993 bitfield referenced, we may assume that is not subject to
1994 promotion, and that, therefore, the type of the resulting rvalue
1995 is the declared type of the bitfield. */
1996 exp
= convert_bitfield_to_declared_type (exp
);
1998 /* We do not call rvalue() here because we do not want to wrap EXP
1999 in a NON_LVALUE_EXPR. */
2003 Non-class rvalues always have cv-unqualified types. */
2004 type
= TREE_TYPE (exp
);
2005 if (!CLASS_TYPE_P (type
) && cv_qualified_p (type
))
2006 exp
= build_nop (cv_unqualified (type
), exp
);
2011 /* Perform preparatory conversions, as part of the "usual arithmetic
2012 conversions". In particular, as per [expr]:
2014 Whenever an lvalue expression appears as an operand of an
2015 operator that expects the rvalue for that operand, the
2016 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2017 standard conversions are applied to convert the expression to an
2020 In addition, we perform integral promotions here, as those are
2021 applied to both operands to a binary operator before determining
2022 what additional conversions should apply. */
2025 cp_default_conversion (tree exp
, tsubst_flags_t complain
)
2027 /* Check for target-specific promotions. */
2028 tree promoted_type
= targetm
.promoted_type (TREE_TYPE (exp
));
2030 exp
= cp_convert (promoted_type
, exp
, complain
);
2031 /* Perform the integral promotions first so that bitfield
2032 expressions (which may promote to "int", even if the bitfield is
2033 declared "unsigned") are promoted correctly. */
2034 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp
)))
2035 exp
= cp_perform_integral_promotions (exp
, complain
);
2036 /* Perform the other conversions. */
2037 exp
= decay_conversion (exp
, complain
);
2045 default_conversion (tree exp
)
2047 return cp_default_conversion (exp
, tf_warning_or_error
);
2050 /* EXPR is an expression with an integral or enumeration type.
2051 Perform the integral promotions in [conv.prom], and return the
2055 cp_perform_integral_promotions (tree expr
, tsubst_flags_t complain
)
2060 expr
= mark_rvalue_use (expr
);
2064 If the bitfield has an enumerated type, it is treated as any
2065 other value of that type for promotion purposes. */
2066 type
= is_bitfield_expr_with_lowered_type (expr
);
2067 if (!type
|| TREE_CODE (type
) != ENUMERAL_TYPE
)
2068 type
= TREE_TYPE (expr
);
2069 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
2070 /* Scoped enums don't promote. */
2071 if (SCOPED_ENUM_P (type
))
2073 promoted_type
= type_promotes_to (type
);
2074 if (type
!= promoted_type
)
2075 expr
= cp_convert (promoted_type
, expr
, complain
);
2082 perform_integral_promotions (tree expr
)
2084 return cp_perform_integral_promotions (expr
, tf_warning_or_error
);
2087 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2088 decay_conversion to one. */
2091 string_conv_p (const_tree totype
, const_tree exp
, int warn
)
2095 if (!TYPE_PTR_P (totype
))
2098 t
= TREE_TYPE (totype
);
2099 if (!same_type_p (t
, char_type_node
)
2100 && !same_type_p (t
, char16_type_node
)
2101 && !same_type_p (t
, char32_type_node
)
2102 && !same_type_p (t
, wchar_type_node
))
2105 if (TREE_CODE (exp
) == STRING_CST
)
2107 /* Make sure that we don't try to convert between char and wide chars. */
2108 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
2113 /* Is this a string constant which has decayed to 'const char *'? */
2114 t
= build_pointer_type (cp_build_qualified_type (t
, TYPE_QUAL_CONST
));
2115 if (!same_type_p (TREE_TYPE (exp
), t
))
2118 if (TREE_CODE (exp
) != ADDR_EXPR
2119 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
2123 /* This warning is not very useful, as it complains about printf. */
2125 warning (OPT_Wwrite_strings
,
2126 "deprecated conversion from string constant to %qT",
2132 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2133 can, for example, use as an lvalue. This code used to be in
2134 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2135 expressions, where we're dealing with aggregates. But now it's again only
2136 called from unary_complex_lvalue. The case (in particular) that led to
2137 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2141 rationalize_conditional_expr (enum tree_code code
, tree t
,
2142 tsubst_flags_t complain
)
2144 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
2146 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2147 the first operand is always the one to be used if both operands
2148 are equal, so we know what conditional expression this used to be. */
2149 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
2151 tree op0
= TREE_OPERAND (t
, 0);
2152 tree op1
= TREE_OPERAND (t
, 1);
2154 /* The following code is incorrect if either operand side-effects. */
2155 gcc_assert (!TREE_SIDE_EFFECTS (op0
)
2156 && !TREE_SIDE_EFFECTS (op1
));
2158 build_conditional_expr (loc
,
2159 build_x_binary_op (loc
,
2160 (TREE_CODE (t
) == MIN_EXPR
2161 ? LE_EXPR
: GE_EXPR
),
2162 op0
, TREE_CODE (op0
),
2163 op1
, TREE_CODE (op1
),
2166 cp_build_unary_op (code
, op0
, 0, complain
),
2167 cp_build_unary_op (code
, op1
, 0, complain
),
2172 build_conditional_expr (loc
, TREE_OPERAND (t
, 0),
2173 cp_build_unary_op (code
, TREE_OPERAND (t
, 1), 0,
2175 cp_build_unary_op (code
, TREE_OPERAND (t
, 2), 0,
2180 /* Given the TYPE of an anonymous union field inside T, return the
2181 FIELD_DECL for the field. If not found return NULL_TREE. Because
2182 anonymous unions can nest, we must also search all anonymous unions
2183 that are directly reachable. */
2186 lookup_anon_field (tree t
, tree type
)
2190 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
2192 if (TREE_STATIC (field
))
2194 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
2197 /* If we find it directly, return the field. */
2198 if (DECL_NAME (field
) == NULL_TREE
2199 && type
== TYPE_MAIN_VARIANT (TREE_TYPE (field
)))
2204 /* Otherwise, it could be nested, search harder. */
2205 if (DECL_NAME (field
) == NULL_TREE
2206 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2208 tree subfield
= lookup_anon_field (TREE_TYPE (field
), type
);
2216 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2217 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2218 non-NULL, it indicates the path to the base used to name MEMBER.
2219 If PRESERVE_REFERENCE is true, the expression returned will have
2220 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2221 returned will have the type referred to by the reference.
2223 This function does not perform access control; that is either done
2224 earlier by the parser when the name of MEMBER is resolved to MEMBER
2225 itself, or later when overload resolution selects one of the
2226 functions indicated by MEMBER. */
2229 build_class_member_access_expr (tree object
, tree member
,
2230 tree access_path
, bool preserve_reference
,
2231 tsubst_flags_t complain
)
2235 tree result
= NULL_TREE
;
2236 tree using_decl
= NULL_TREE
;
2238 if (error_operand_p (object
) || error_operand_p (member
))
2239 return error_mark_node
;
2241 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2245 The type of the first expression shall be "class object" (of a
2247 object_type
= TREE_TYPE (object
);
2248 if (!currently_open_class (object_type
)
2249 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2250 return error_mark_node
;
2251 if (!CLASS_TYPE_P (object_type
))
2253 if (complain
& tf_error
)
2255 if (POINTER_TYPE_P (object_type
)
2256 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2257 error ("request for member %qD in %qE, which is of pointer "
2258 "type %qT (maybe you meant to use %<->%> ?)",
2259 member
, object
, object_type
);
2261 error ("request for member %qD in %qE, which is of non-class "
2262 "type %qT", member
, object
, object_type
);
2264 return error_mark_node
;
2267 /* The standard does not seem to actually say that MEMBER must be a
2268 member of OBJECT_TYPE. However, that is clearly what is
2270 if (DECL_P (member
))
2272 member_scope
= DECL_CLASS_CONTEXT (member
);
2274 if (TREE_DEPRECATED (member
))
2275 warn_deprecated_use (member
, NULL_TREE
);
2278 member_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (member
));
2279 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2280 presently be the anonymous union. Go outwards until we find a
2281 type related to OBJECT_TYPE. */
2282 while ((ANON_AGGR_TYPE_P (member_scope
) || UNSCOPED_ENUM_P (member_scope
))
2283 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
2285 member_scope
= TYPE_CONTEXT (member_scope
);
2286 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
2288 if (complain
& tf_error
)
2290 if (TREE_CODE (member
) == FIELD_DECL
)
2291 error ("invalid use of nonstatic data member %qE", member
);
2293 error ("%qD is not a member of %qT", member
, object_type
);
2295 return error_mark_node
;
2298 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2299 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2300 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2302 tree temp
= unary_complex_lvalue (ADDR_EXPR
, object
);
2304 object
= cp_build_indirect_ref (temp
, RO_NULL
, complain
);
2307 /* In [expr.ref], there is an explicit list of the valid choices for
2308 MEMBER. We check for each of those cases here. */
2311 /* A static data member. */
2313 mark_exp_read (object
);
2314 /* If OBJECT has side-effects, they are supposed to occur. */
2315 if (TREE_SIDE_EFFECTS (object
))
2316 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
2318 else if (TREE_CODE (member
) == FIELD_DECL
)
2320 /* A non-static data member. */
2325 null_object_p
= (INDIRECT_REF_P (object
)
2326 && integer_zerop (TREE_OPERAND (object
, 0)));
2328 /* Convert OBJECT to the type of MEMBER. */
2329 if (!same_type_p (TYPE_MAIN_VARIANT (object_type
),
2330 TYPE_MAIN_VARIANT (member_scope
)))
2335 binfo
= lookup_base (access_path
? access_path
: object_type
,
2336 member_scope
, ba_unique
, &kind
, complain
);
2337 if (binfo
== error_mark_node
)
2338 return error_mark_node
;
2340 /* It is invalid to try to get to a virtual base of a
2341 NULL object. The most common cause is invalid use of
2343 if (null_object_p
&& kind
== bk_via_virtual
)
2345 if (complain
& tf_error
)
2347 error ("invalid access to non-static data member %qD of "
2350 error ("(perhaps the %<offsetof%> macro was used incorrectly)");
2352 return error_mark_node
;
2355 /* Convert to the base. */
2356 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
2357 /*nonnull=*/1, complain
);
2358 /* If we found the base successfully then we should be able
2359 to convert to it successfully. */
2360 gcc_assert (object
!= error_mark_node
);
2363 /* Complain about other invalid uses of offsetof, even though they will
2364 give the right answer. Note that we complain whether or not they
2365 actually used the offsetof macro, since there's no way to know at this
2366 point. So we just give a warning, instead of a pedwarn. */
2367 /* Do not produce this warning for base class field references, because
2368 we know for a fact that didn't come from offsetof. This does occur
2369 in various testsuite cases where a null object is passed where a
2370 vtable access is required. */
2371 if (null_object_p
&& warn_invalid_offsetof
2372 && CLASSTYPE_NON_STD_LAYOUT (object_type
)
2373 && !DECL_FIELD_IS_BASE (member
)
2374 && cp_unevaluated_operand
== 0
2375 && (complain
& tf_warning
))
2377 warning (OPT_Winvalid_offsetof
,
2378 "invalid access to non-static data member %qD "
2379 " of NULL object", member
);
2380 warning (OPT_Winvalid_offsetof
,
2381 "(perhaps the %<offsetof%> macro was used incorrectly)");
2384 /* If MEMBER is from an anonymous aggregate, we have converted
2385 OBJECT so that it refers to the class containing the
2386 anonymous union. Generate a reference to the anonymous union
2387 itself, and recur to find MEMBER. */
2388 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
))
2389 /* When this code is called from build_field_call, the
2390 object already has the type of the anonymous union.
2391 That is because the COMPONENT_REF was already
2392 constructed, and was then disassembled before calling
2393 build_field_call. After the function-call code is
2394 cleaned up, this waste can be eliminated. */
2395 && (!same_type_ignoring_top_level_qualifiers_p
2396 (TREE_TYPE (object
), DECL_CONTEXT (member
))))
2398 tree anonymous_union
;
2400 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
2401 DECL_CONTEXT (member
));
2402 object
= build_class_member_access_expr (object
,
2404 /*access_path=*/NULL_TREE
,
2409 /* Compute the type of the field, as described in [expr.ref]. */
2410 type_quals
= TYPE_UNQUALIFIED
;
2411 member_type
= TREE_TYPE (member
);
2412 if (TREE_CODE (member_type
) != REFERENCE_TYPE
)
2414 type_quals
= (cp_type_quals (member_type
)
2415 | cp_type_quals (object_type
));
2417 /* A field is const (volatile) if the enclosing object, or the
2418 field itself, is const (volatile). But, a mutable field is
2419 not const, even within a const object. */
2420 if (DECL_MUTABLE_P (member
))
2421 type_quals
&= ~TYPE_QUAL_CONST
;
2422 member_type
= cp_build_qualified_type (member_type
, type_quals
);
2425 result
= build3 (COMPONENT_REF
, member_type
, object
, member
,
2427 result
= fold_if_not_in_template (result
);
2429 /* Mark the expression const or volatile, as appropriate. Even
2430 though we've dealt with the type above, we still have to mark the
2431 expression itself. */
2432 if (type_quals
& TYPE_QUAL_CONST
)
2433 TREE_READONLY (result
) = 1;
2434 if (type_quals
& TYPE_QUAL_VOLATILE
)
2435 TREE_THIS_VOLATILE (result
) = 1;
2437 else if (BASELINK_P (member
))
2439 /* The member is a (possibly overloaded) member function. */
2443 /* If the MEMBER is exactly one static member function, then we
2444 know the type of the expression. Otherwise, we must wait
2445 until overload resolution has been performed. */
2446 functions
= BASELINK_FUNCTIONS (member
);
2447 if (TREE_CODE (functions
) == FUNCTION_DECL
2448 && DECL_STATIC_FUNCTION_P (functions
))
2449 type
= TREE_TYPE (functions
);
2451 type
= unknown_type_node
;
2452 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2453 base. That will happen when the function is called. */
2454 result
= build3 (COMPONENT_REF
, type
, object
, member
, NULL_TREE
);
2456 else if (TREE_CODE (member
) == CONST_DECL
)
2458 /* The member is an enumerator. */
2460 /* If OBJECT has side-effects, they are supposed to occur. */
2461 if (TREE_SIDE_EFFECTS (object
))
2462 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
2465 else if ((using_decl
= strip_using_decl (member
)) != member
)
2466 result
= build_class_member_access_expr (object
,
2468 access_path
, preserve_reference
,
2472 if (complain
& tf_error
)
2473 error ("invalid use of %qD", member
);
2474 return error_mark_node
;
2477 if (!preserve_reference
)
2480 If E2 is declared to have type "reference to T", then ... the
2481 type of E1.E2 is T. */
2482 result
= convert_from_reference (result
);
2487 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2488 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2491 lookup_destructor (tree object
, tree scope
, tree dtor_name
,
2492 tsubst_flags_t complain
)
2494 tree object_type
= TREE_TYPE (object
);
2495 tree dtor_type
= TREE_OPERAND (dtor_name
, 0);
2498 /* We've already complained about this destructor. */
2499 if (dtor_type
== error_mark_node
)
2500 return error_mark_node
;
2502 if (scope
&& !check_dtor_name (scope
, dtor_type
))
2504 if (complain
& tf_error
)
2505 error ("qualified type %qT does not match destructor name ~%qT",
2507 return error_mark_node
;
2509 if (is_auto (dtor_type
))
2510 dtor_type
= object_type
;
2511 else if (identifier_p (dtor_type
))
2513 /* In a template, names we can't find a match for are still accepted
2514 destructor names, and we check them here. */
2515 if (check_dtor_name (object_type
, dtor_type
))
2516 dtor_type
= object_type
;
2519 if (complain
& tf_error
)
2520 error ("object type %qT does not match destructor name ~%qT",
2521 object_type
, dtor_type
);
2522 return error_mark_node
;
2526 else if (!DERIVED_FROM_P (dtor_type
, TYPE_MAIN_VARIANT (object_type
)))
2528 if (complain
& tf_error
)
2529 error ("the type being destroyed is %qT, but the destructor "
2530 "refers to %qT", TYPE_MAIN_VARIANT (object_type
), dtor_type
);
2531 return error_mark_node
;
2533 expr
= lookup_member (dtor_type
, complete_dtor_identifier
,
2534 /*protect=*/1, /*want_type=*/false,
2535 tf_warning_or_error
);
2536 expr
= (adjust_result_of_qualified_name_lookup
2537 (expr
, dtor_type
, object_type
));
2538 if (scope
== NULL_TREE
)
2539 /* We need to call adjust_result_of_qualified_name_lookup in case the
2540 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2541 that we still get virtual function binding. */
2542 BASELINK_QUALIFIED_P (expr
) = false;
2546 /* An expression of the form "A::template B" has been resolved to
2547 DECL. Issue a diagnostic if B is not a template or template
2551 check_template_keyword (tree decl
)
2553 /* The standard says:
2557 If a name prefixed by the keyword template is not a member
2558 template, the program is ill-formed.
2560 DR 228 removed the restriction that the template be a member
2563 DR 96, if accepted would add the further restriction that explicit
2564 template arguments must be provided if the template keyword is
2565 used, but, as of 2005-10-16, that DR is still in "drafting". If
2566 this DR is accepted, then the semantic checks here can be
2567 simplified, as the entity named must in fact be a template
2568 specialization, rather than, as at present, a set of overloaded
2569 functions containing at least one template function. */
2570 if (TREE_CODE (decl
) != TEMPLATE_DECL
2571 && TREE_CODE (decl
) != TEMPLATE_ID_EXPR
)
2573 if (!is_overloaded_fn (decl
))
2574 permerror (input_location
, "%qD is not a template", decl
);
2579 if (BASELINK_P (fns
))
2580 fns
= BASELINK_FUNCTIONS (fns
);
2583 tree fn
= OVL_CURRENT (fns
);
2584 if (TREE_CODE (fn
) == TEMPLATE_DECL
2585 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
2587 if (TREE_CODE (fn
) == FUNCTION_DECL
2588 && DECL_USE_TEMPLATE (fn
)
2589 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn
)))
2591 fns
= OVL_NEXT (fns
);
2594 permerror (input_location
, "%qD is not a template", decl
);
2599 /* This function is called by the parser to process a class member
2600 access expression of the form OBJECT.NAME. NAME is a node used by
2601 the parser to represent a name; it is not yet a DECL. It may,
2602 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2603 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2604 there is no reason to do the lookup twice, so the parser keeps the
2605 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2606 be a template via the use of the "A::template B" syntax. */
2609 finish_class_member_access_expr (tree object
, tree name
, bool template_p
,
2610 tsubst_flags_t complain
)
2615 tree access_path
= NULL_TREE
;
2616 tree orig_object
= object
;
2617 tree orig_name
= name
;
2619 if (object
== error_mark_node
|| name
== error_mark_node
)
2620 return error_mark_node
;
2622 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2623 if (!objc_is_public (object
, name
))
2624 return error_mark_node
;
2626 object_type
= TREE_TYPE (object
);
2628 if (processing_template_decl
)
2630 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2631 dependent_type_p (object_type
)
2632 /* If NAME is just an IDENTIFIER_NODE, then the expression
2634 || identifier_p (object
)
2635 /* If NAME is "f<args>", where either 'f' or 'args' is
2636 dependent, then the expression is dependent. */
2637 || (TREE_CODE (name
) == TEMPLATE_ID_EXPR
2638 && dependent_template_id_p (TREE_OPERAND (name
, 0),
2639 TREE_OPERAND (name
, 1)))
2640 /* If NAME is "T::X" where "T" is dependent, then the
2641 expression is dependent. */
2642 || (TREE_CODE (name
) == SCOPE_REF
2643 && TYPE_P (TREE_OPERAND (name
, 0))
2644 && dependent_type_p (TREE_OPERAND (name
, 0))))
2645 return build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
,
2646 object
, name
, NULL_TREE
);
2647 object
= build_non_dependent_expr (object
);
2649 else if (c_dialect_objc ()
2650 && identifier_p (name
)
2651 && (expr
= objc_maybe_build_component_ref (object
, name
)))
2656 The type of the first expression shall be "class object" (of a
2658 if (!currently_open_class (object_type
)
2659 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2660 return error_mark_node
;
2661 if (!CLASS_TYPE_P (object_type
))
2663 if (complain
& tf_error
)
2665 if (POINTER_TYPE_P (object_type
)
2666 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2667 error ("request for member %qD in %qE, which is of pointer "
2668 "type %qT (maybe you meant to use %<->%> ?)",
2669 name
, object
, object_type
);
2671 error ("request for member %qD in %qE, which is of non-class "
2672 "type %qT", name
, object
, object_type
);
2674 return error_mark_node
;
2677 if (BASELINK_P (name
))
2678 /* A member function that has already been looked up. */
2682 bool is_template_id
= false;
2683 tree template_args
= NULL_TREE
;
2686 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
2688 is_template_id
= true;
2689 template_args
= TREE_OPERAND (name
, 1);
2690 name
= TREE_OPERAND (name
, 0);
2692 if (TREE_CODE (name
) == OVERLOAD
)
2693 name
= DECL_NAME (get_first_fn (name
));
2694 else if (DECL_P (name
))
2695 name
= DECL_NAME (name
);
2698 if (TREE_CODE (name
) == SCOPE_REF
)
2700 /* A qualified name. The qualifying class or namespace `S'
2701 has already been looked up; it is either a TYPE or a
2703 scope
= TREE_OPERAND (name
, 0);
2704 name
= TREE_OPERAND (name
, 1);
2706 /* If SCOPE is a namespace, then the qualified name does not
2707 name a member of OBJECT_TYPE. */
2708 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
2710 if (complain
& tf_error
)
2711 error ("%<%D::%D%> is not a member of %qT",
2712 scope
, name
, object_type
);
2713 return error_mark_node
;
2716 if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
2718 /* Looking up a member enumerator (c++/56793). */
2719 if (!TYPE_CLASS_SCOPE_P (scope
)
2720 || !DERIVED_FROM_P (TYPE_CONTEXT (scope
), object_type
))
2722 if (complain
& tf_error
)
2723 error ("%<%D::%D%> is not a member of %qT",
2724 scope
, name
, object_type
);
2725 return error_mark_node
;
2727 tree val
= lookup_enumerator (scope
, name
);
2728 if (TREE_SIDE_EFFECTS (object
))
2729 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
), object
, val
);
2733 gcc_assert (CLASS_TYPE_P (scope
));
2734 gcc_assert (identifier_p (name
) || TREE_CODE (name
) == BIT_NOT_EXPR
);
2736 if (constructor_name_p (name
, scope
))
2738 if (complain
& tf_error
)
2739 error ("cannot call constructor %<%T::%D%> directly",
2741 return error_mark_node
;
2744 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2745 access_path
= lookup_base (object_type
, scope
, ba_check
,
2747 if (access_path
== error_mark_node
)
2748 return error_mark_node
;
2751 if (complain
& tf_error
)
2752 error ("%qT is not a base of %qT", scope
, object_type
);
2753 return error_mark_node
;
2759 access_path
= object_type
;
2762 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2763 member
= lookup_destructor (object
, scope
, name
, complain
);
2766 /* Look up the member. */
2767 member
= lookup_member (access_path
, name
, /*protect=*/1,
2768 /*want_type=*/false, complain
);
2769 if (member
== NULL_TREE
)
2771 if (complain
& tf_error
)
2772 error ("%qD has no member named %qE",
2773 TREE_CODE (access_path
) == TREE_BINFO
2774 ? TREE_TYPE (access_path
) : object_type
, name
);
2775 return error_mark_node
;
2777 if (member
== error_mark_node
)
2778 return error_mark_node
;
2783 tree templ
= member
;
2785 if (BASELINK_P (templ
))
2786 templ
= lookup_template_function (templ
, template_args
);
2789 if (complain
& tf_error
)
2790 error ("%qD is not a member template function", name
);
2791 return error_mark_node
;
2796 if (TREE_DEPRECATED (member
))
2797 warn_deprecated_use (member
, NULL_TREE
);
2800 check_template_keyword (member
);
2802 expr
= build_class_member_access_expr (object
, member
, access_path
,
2803 /*preserve_reference=*/false,
2805 if (processing_template_decl
&& expr
!= error_mark_node
)
2807 if (BASELINK_P (member
))
2809 if (TREE_CODE (orig_name
) == SCOPE_REF
)
2810 BASELINK_QUALIFIED_P (member
) = 1;
2813 return build_min_non_dep (COMPONENT_REF
, expr
,
2814 orig_object
, orig_name
,
2821 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2825 build_simple_component_ref (tree object
, tree member
)
2827 tree type
= cp_build_qualified_type (TREE_TYPE (member
),
2828 cp_type_quals (TREE_TYPE (object
)));
2829 return fold_build3_loc (input_location
,
2830 COMPONENT_REF
, type
,
2831 object
, member
, NULL_TREE
);
2834 /* Return an expression for the MEMBER_NAME field in the internal
2835 representation of PTRMEM, a pointer-to-member function. (Each
2836 pointer-to-member function type gets its own RECORD_TYPE so it is
2837 more convenient to access the fields by name than by FIELD_DECL.)
2838 This routine converts the NAME to a FIELD_DECL and then creates the
2839 node for the complete expression. */
2842 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
2847 /* This code is a stripped down version of
2848 build_class_member_access_expr. It does not work to use that
2849 routine directly because it expects the object to be of class
2851 ptrmem_type
= TREE_TYPE (ptrmem
);
2852 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
));
2853 member
= lookup_member (ptrmem_type
, member_name
, /*protect=*/0,
2854 /*want_type=*/false, tf_warning_or_error
);
2855 return build_simple_component_ref (ptrmem
, member
);
2858 /* Given an expression PTR for a pointer, return an expression
2859 for the value pointed to.
2860 ERRORSTRING is the name of the operator to appear in error messages.
2862 This function may need to overload OPERATOR_FNNAME.
2863 Must also handle REFERENCE_TYPEs for C++. */
2866 build_x_indirect_ref (location_t loc
, tree expr
, ref_operator errorstring
,
2867 tsubst_flags_t complain
)
2869 tree orig_expr
= expr
;
2872 if (processing_template_decl
)
2874 /* Retain the type if we know the operand is a pointer. */
2875 if (TREE_TYPE (expr
) && POINTER_TYPE_P (TREE_TYPE (expr
)))
2876 return build_min (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
2877 if (type_dependent_expression_p (expr
))
2878 return build_min_nt_loc (loc
, INDIRECT_REF
, expr
);
2879 expr
= build_non_dependent_expr (expr
);
2882 rval
= build_new_op (loc
, INDIRECT_REF
, LOOKUP_NORMAL
, expr
,
2883 NULL_TREE
, NULL_TREE
, /*overload=*/NULL
, complain
);
2885 rval
= cp_build_indirect_ref (expr
, errorstring
, complain
);
2887 if (processing_template_decl
&& rval
!= error_mark_node
)
2888 return build_min_non_dep (INDIRECT_REF
, rval
, orig_expr
);
2893 /* Helper function called from c-common. */
2895 build_indirect_ref (location_t
/*loc*/,
2896 tree ptr
, ref_operator errorstring
)
2898 return cp_build_indirect_ref (ptr
, errorstring
, tf_warning_or_error
);
2902 cp_build_indirect_ref (tree ptr
, ref_operator errorstring
,
2903 tsubst_flags_t complain
)
2907 if (ptr
== current_class_ptr
2908 || (TREE_CODE (ptr
) == NOP_EXPR
2909 && TREE_OPERAND (ptr
, 0) == current_class_ptr
2910 && (same_type_ignoring_top_level_qualifiers_p
2911 (TREE_TYPE (ptr
), TREE_TYPE (current_class_ptr
)))))
2912 return current_class_ref
;
2914 pointer
= (TREE_CODE (TREE_TYPE (ptr
)) == REFERENCE_TYPE
2915 ? ptr
: decay_conversion (ptr
, complain
));
2916 if (pointer
== error_mark_node
)
2917 return error_mark_node
;
2919 type
= TREE_TYPE (pointer
);
2921 if (POINTER_TYPE_P (type
))
2925 If the type of the expression is "pointer to T," the type
2926 of the result is "T." */
2927 tree t
= TREE_TYPE (type
);
2929 if (CONVERT_EXPR_P (ptr
)
2930 || TREE_CODE (ptr
) == VIEW_CONVERT_EXPR
)
2932 /* If a warning is issued, mark it to avoid duplicates from
2933 the backend. This only needs to be done at
2934 warn_strict_aliasing > 2. */
2935 if (warn_strict_aliasing
> 2)
2936 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr
, 0)),
2937 type
, TREE_OPERAND (ptr
, 0)))
2938 TREE_NO_WARNING (ptr
) = 1;
2941 if (VOID_TYPE_P (t
))
2943 /* A pointer to incomplete type (other than cv void) can be
2944 dereferenced [expr.unary.op]/1 */
2945 if (complain
& tf_error
)
2946 error ("%qT is not a pointer-to-object type", type
);
2947 return error_mark_node
;
2949 else if (TREE_CODE (pointer
) == ADDR_EXPR
2950 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
2951 /* The POINTER was something like `&x'. We simplify `*&x' to
2953 return TREE_OPERAND (pointer
, 0);
2956 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
2958 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2959 so that we get the proper error message if the result is used
2960 to assign to. Also, &* is supposed to be a no-op. */
2961 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
2962 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
2963 TREE_SIDE_EFFECTS (ref
)
2964 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
));
2968 else if (!(complain
& tf_error
))
2969 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2971 /* `pointer' won't be an error_mark_node if we were given a
2972 pointer to member, so it's cool to check for this here. */
2973 else if (TYPE_PTRMEM_P (type
))
2974 switch (errorstring
)
2976 case RO_ARRAY_INDEXING
:
2977 error ("invalid use of array indexing on pointer to member");
2980 error ("invalid use of unary %<*%> on pointer to member");
2982 case RO_IMPLICIT_CONVERSION
:
2983 error ("invalid use of implicit conversion on pointer to member");
2986 error ("left hand operand of %<->*%> must be a pointer to class, "
2987 "but is a pointer to member of type %qT", type
);
2992 else if (pointer
!= error_mark_node
)
2993 invalid_indirection_error (input_location
, type
, errorstring
);
2995 return error_mark_node
;
2998 /* This handles expressions of the form "a[i]", which denotes
3001 This is logically equivalent in C to *(a+i), but we may do it differently.
3002 If A is a variable or a member, we generate a primitive ARRAY_REF.
3003 This avoids forcing the array out of registers, and can work on
3004 arrays that are not lvalues (for example, members of structures returned
3007 If INDEX is of some user-defined type, it must be converted to
3008 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3009 will inherit the type of the array, which will be some pointer type.
3011 LOC is the location to use in building the array reference. */
3014 cp_build_array_ref (location_t loc
, tree array
, tree idx
,
3015 tsubst_flags_t complain
)
3021 if (complain
& tf_error
)
3022 error_at (loc
, "subscript missing in array reference");
3023 return error_mark_node
;
3026 /* If an array's index is an array notation, then its rank cannot be
3027 greater than one. */
3028 if (flag_cilkplus
&& contains_array_notation_expr (idx
))
3032 /* If find_rank returns false, then it should have reported an error,
3033 thus it is unnecessary for repetition. */
3034 if (!find_rank (loc
, idx
, idx
, true, &rank
))
3035 return error_mark_node
;
3038 error_at (loc
, "rank of the array%'s index is greater than 1");
3039 return error_mark_node
;
3042 if (TREE_TYPE (array
) == error_mark_node
3043 || TREE_TYPE (idx
) == error_mark_node
)
3044 return error_mark_node
;
3046 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3048 switch (TREE_CODE (array
))
3052 tree value
= cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3054 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3055 TREE_OPERAND (array
, 0), value
);
3056 SET_EXPR_LOCATION (ret
, loc
);
3061 ret
= build_conditional_expr
3062 (loc
, TREE_OPERAND (array
, 0),
3063 cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3065 cp_build_array_ref (loc
, TREE_OPERAND (array
, 2), idx
,
3068 protected_set_expr_location (ret
, loc
);
3075 convert_vector_to_pointer_for_subscript (loc
, &array
, idx
);
3077 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
3081 warn_array_subscript_with_type_char (idx
);
3083 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx
)))
3085 if (complain
& tf_error
)
3086 error_at (loc
, "array subscript is not an integer");
3087 return error_mark_node
;
3090 /* Apply integral promotions *after* noticing character types.
3091 (It is unclear why we do these promotions -- the standard
3092 does not say that we should. In fact, the natural thing would
3093 seem to be to convert IDX to ptrdiff_t; we're performing
3094 pointer arithmetic.) */
3095 idx
= cp_perform_integral_promotions (idx
, complain
);
3097 /* An array that is indexed by a non-constant
3098 cannot be stored in a register; we must be able to do
3099 address arithmetic on its address.
3100 Likewise an array of elements of variable size. */
3101 if (TREE_CODE (idx
) != INTEGER_CST
3102 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
3103 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
3106 if (!cxx_mark_addressable (array
))
3107 return error_mark_node
;
3110 /* An array that is indexed by a constant value which is not within
3111 the array bounds cannot be stored in a register either; because we
3112 would get a crash in store_bit_field/extract_bit_field when trying
3113 to access a non-existent part of the register. */
3114 if (TREE_CODE (idx
) == INTEGER_CST
3115 && TYPE_DOMAIN (TREE_TYPE (array
))
3116 && ! int_fits_type_p (idx
, TYPE_DOMAIN (TREE_TYPE (array
))))
3118 if (!cxx_mark_addressable (array
))
3119 return error_mark_node
;
3122 if (!lvalue_p (array
) && (complain
& tf_error
))
3123 pedwarn (loc
, OPT_Wpedantic
,
3124 "ISO C++ forbids subscripting non-lvalue array");
3126 /* Note in C++ it is valid to subscript a `register' array, since
3127 it is valid to take the address of something with that
3128 storage specification. */
3132 while (TREE_CODE (foo
) == COMPONENT_REF
)
3133 foo
= TREE_OPERAND (foo
, 0);
3134 if (VAR_P (foo
) && DECL_REGISTER (foo
)
3135 && (complain
& tf_warning
))
3136 warning_at (loc
, OPT_Wextra
,
3137 "subscripting array declared %<register%>");
3140 type
= TREE_TYPE (TREE_TYPE (array
));
3141 rval
= build4 (ARRAY_REF
, type
, array
, idx
, NULL_TREE
, NULL_TREE
);
3142 /* Array ref is const/volatile if the array elements are
3143 or if the array is.. */
3144 TREE_READONLY (rval
)
3145 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
3146 TREE_SIDE_EFFECTS (rval
)
3147 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
3148 TREE_THIS_VOLATILE (rval
)
3149 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
3150 ret
= require_complete_type_sfinae (fold_if_not_in_template (rval
),
3152 protected_set_expr_location (ret
, loc
);
3157 tree ar
= cp_default_conversion (array
, complain
);
3158 tree ind
= cp_default_conversion (idx
, complain
);
3160 /* Put the integer in IND to simplify error checking. */
3161 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
3168 if (ar
== error_mark_node
|| ind
== error_mark_node
)
3169 return error_mark_node
;
3171 if (!TYPE_PTR_P (TREE_TYPE (ar
)))
3173 if (complain
& tf_error
)
3174 error_at (loc
, "subscripted value is neither array nor pointer");
3175 return error_mark_node
;
3177 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
3179 if (complain
& tf_error
)
3180 error_at (loc
, "array subscript is not an integer");
3181 return error_mark_node
;
3184 warn_array_subscript_with_type_char (idx
);
3186 ret
= cp_build_indirect_ref (cp_build_binary_op (input_location
,
3191 protected_set_expr_location (ret
, loc
);
3196 /* Entry point for Obj-C++. */
3199 build_array_ref (location_t loc
, tree array
, tree idx
)
3201 return cp_build_array_ref (loc
, array
, idx
, tf_warning_or_error
);
3204 /* Resolve a pointer to member function. INSTANCE is the object
3205 instance to use, if the member points to a virtual member.
3207 This used to avoid checking for virtual functions if basetype
3208 has no virtual functions, according to an earlier ANSI draft.
3209 With the final ISO C++ rules, such an optimization is
3210 incorrect: A pointer to a derived member can be static_cast
3211 to pointer-to-base-member, as long as the dynamic object
3212 later has the right member. So now we only do this optimization
3213 when we know the dynamic type of the object. */
3216 get_member_function_from_ptrfunc (tree
*instance_ptrptr
, tree function
,
3217 tsubst_flags_t complain
)
3219 if (TREE_CODE (function
) == OFFSET_REF
)
3220 function
= TREE_OPERAND (function
, 1);
3222 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
3224 tree idx
, delta
, e1
, e2
, e3
, vtbl
;
3226 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
3227 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
3229 tree instance_ptr
= *instance_ptrptr
;
3230 tree instance_save_expr
= 0;
3231 if (instance_ptr
== error_mark_node
)
3233 if (TREE_CODE (function
) == PTRMEM_CST
)
3235 /* Extracting the function address from a pmf is only
3236 allowed with -Wno-pmf-conversions. It only works for
3238 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
), complain
);
3239 e1
= convert (fntype
, e1
);
3244 if (complain
& tf_error
)
3245 error ("object missing in use of %qE", function
);
3246 return error_mark_node
;
3250 /* True if we know that the dynamic type of the object doesn't have
3251 virtual functions, so we can assume the PFN field is a pointer. */
3252 nonvirtual
= (COMPLETE_TYPE_P (basetype
)
3253 && !TYPE_POLYMORPHIC_P (basetype
)
3254 && resolves_to_fixed_type_p (instance_ptr
, 0));
3256 /* If we don't really have an object (i.e. in an ill-formed
3257 conversion from PMF to pointer), we can't resolve virtual
3258 functions anyway. */
3259 if (!nonvirtual
&& is_dummy_object (instance_ptr
))
3262 if (TREE_SIDE_EFFECTS (instance_ptr
))
3263 instance_ptr
= instance_save_expr
= save_expr (instance_ptr
);
3265 if (TREE_SIDE_EFFECTS (function
))
3266 function
= save_expr (function
);
3268 /* Start by extracting all the information from the PMF itself. */
3269 e3
= pfn_from_ptrmemfunc (function
);
3270 delta
= delta_from_ptrmemfunc (function
);
3271 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
3272 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
3274 case ptrmemfunc_vbit_in_pfn
:
3275 e1
= cp_build_binary_op (input_location
,
3276 BIT_AND_EXPR
, idx
, integer_one_node
,
3278 idx
= cp_build_binary_op (input_location
,
3279 MINUS_EXPR
, idx
, integer_one_node
,
3281 if (idx
== error_mark_node
)
3282 return error_mark_node
;
3285 case ptrmemfunc_vbit_in_delta
:
3286 e1
= cp_build_binary_op (input_location
,
3287 BIT_AND_EXPR
, delta
, integer_one_node
,
3289 delta
= cp_build_binary_op (input_location
,
3290 RSHIFT_EXPR
, delta
, integer_one_node
,
3292 if (delta
== error_mark_node
)
3293 return error_mark_node
;
3300 if (e1
== error_mark_node
)
3301 return error_mark_node
;
3303 /* Convert down to the right base before using the instance. A
3304 special case is that in a pointer to member of class C, C may
3305 be incomplete. In that case, the function will of course be
3306 a member of C, and no conversion is required. In fact,
3307 lookup_base will fail in that case, because incomplete
3308 classes do not have BINFOs. */
3309 if (!same_type_ignoring_top_level_qualifiers_p
3310 (basetype
, TREE_TYPE (TREE_TYPE (instance_ptr
))))
3312 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
3313 basetype
, ba_check
, NULL
, complain
);
3314 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
,
3316 if (instance_ptr
== error_mark_node
)
3317 return error_mark_node
;
3319 /* ...and then the delta in the PMF. */
3320 instance_ptr
= fold_build_pointer_plus (instance_ptr
, delta
);
3322 /* Hand back the adjusted 'this' argument to our caller. */
3323 *instance_ptrptr
= instance_ptr
;
3326 /* Now just return the pointer. */
3329 /* Next extract the vtable pointer from the object. */
3330 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
3332 vtbl
= cp_build_indirect_ref (vtbl
, RO_NULL
, complain
);
3333 if (vtbl
== error_mark_node
)
3334 return error_mark_node
;
3336 /* Finally, extract the function pointer from the vtable. */
3337 e2
= fold_build_pointer_plus_loc (input_location
, vtbl
, idx
);
3338 e2
= cp_build_indirect_ref (e2
, RO_NULL
, complain
);
3339 if (e2
== error_mark_node
)
3340 return error_mark_node
;
3341 TREE_CONSTANT (e2
) = 1;
3343 /* When using function descriptors, the address of the
3344 vtable entry is treated as a function pointer. */
3345 if (TARGET_VTABLE_USES_DESCRIPTORS
)
3346 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
3347 cp_build_addr_expr (e2
, complain
));
3349 e2
= fold_convert (TREE_TYPE (e3
), e2
);
3350 e1
= build_conditional_expr (input_location
, e1
, e2
, e3
, complain
);
3351 if (e1
== error_mark_node
)
3352 return error_mark_node
;
3354 /* Make sure this doesn't get evaluated first inside one of the
3355 branches of the COND_EXPR. */
3356 if (instance_save_expr
)
3357 e1
= build2 (COMPOUND_EXPR
, TREE_TYPE (e1
),
3358 instance_save_expr
, e1
);
3365 /* Used by the C-common bits. */
3367 build_function_call (location_t
/*loc*/,
3368 tree function
, tree params
)
3370 return cp_build_function_call (function
, params
, tf_warning_or_error
);
3373 /* Used by the C-common bits. */
3375 build_function_call_vec (location_t
/*loc*/, vec
<location_t
> /*arg_loc*/,
3376 tree function
, vec
<tree
, va_gc
> *params
,
3377 vec
<tree
, va_gc
> * /*origtypes*/)
3379 vec
<tree
, va_gc
> *orig_params
= params
;
3380 tree ret
= cp_build_function_call_vec (function
, ¶ms
,
3381 tf_warning_or_error
);
3383 /* cp_build_function_call_vec can reallocate PARAMS by adding
3384 default arguments. That should never happen here. Verify
3386 gcc_assert (params
== orig_params
);
3391 /* Build a function call using a tree list of arguments. */
3394 cp_build_function_call (tree function
, tree params
, tsubst_flags_t complain
)
3396 vec
<tree
, va_gc
> *vec
;
3399 vec
= make_tree_vector ();
3400 for (; params
!= NULL_TREE
; params
= TREE_CHAIN (params
))
3401 vec_safe_push (vec
, TREE_VALUE (params
));
3402 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3403 release_tree_vector (vec
);
3407 /* Build a function call using varargs. */
3410 cp_build_function_call_nary (tree function
, tsubst_flags_t complain
, ...)
3412 vec
<tree
, va_gc
> *vec
;
3416 vec
= make_tree_vector ();
3417 va_start (args
, complain
);
3418 for (t
= va_arg (args
, tree
); t
!= NULL_TREE
; t
= va_arg (args
, tree
))
3419 vec_safe_push (vec
, t
);
3421 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
3422 release_tree_vector (vec
);
3426 /* Build a function call using a vector of arguments. PARAMS may be
3427 NULL if there are no parameters. This changes the contents of
3431 cp_build_function_call_vec (tree function
, vec
<tree
, va_gc
> **params
,
3432 tsubst_flags_t complain
)
3434 tree fntype
, fndecl
;
3436 tree original
= function
;
3440 vec
<tree
, va_gc
> *allocated
= NULL
;
3443 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3444 expressions, like those used for ObjC messenger dispatches. */
3445 if (params
!= NULL
&& !vec_safe_is_empty (*params
))
3446 function
= objc_rewrite_function_call (function
, (**params
)[0]);
3448 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3449 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3450 if (TREE_CODE (function
) == NOP_EXPR
3451 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
3452 function
= TREE_OPERAND (function
, 0);
3454 if (TREE_CODE (function
) == FUNCTION_DECL
)
3456 mark_used (function
);
3459 /* Convert anything with function type to a pointer-to-function. */
3460 if (DECL_MAIN_P (function
) && (complain
& tf_error
))
3461 pedwarn (input_location
, OPT_Wpedantic
,
3462 "ISO C++ forbids calling %<::main%> from within program");
3464 function
= build_addr_func (function
, complain
);
3470 function
= build_addr_func (function
, complain
);
3473 if (function
== error_mark_node
)
3474 return error_mark_node
;
3476 fntype
= TREE_TYPE (function
);
3478 if (TYPE_PTRMEMFUNC_P (fntype
))
3480 if (complain
& tf_error
)
3481 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3482 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3483 original
, original
);
3484 return error_mark_node
;
3487 is_method
= (TYPE_PTR_P (fntype
)
3488 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
3490 if (!(TYPE_PTRFN_P (fntype
)
3492 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
3494 if (complain
& tf_error
)
3496 if (!flag_diagnostics_show_caret
)
3497 error_at (input_location
,
3498 "%qE cannot be used as a function", original
);
3499 else if (DECL_P (original
))
3500 error_at (input_location
,
3501 "%qD cannot be used as a function", original
);
3503 error_at (input_location
,
3504 "expression cannot be used as a function");
3507 return error_mark_node
;
3510 /* fntype now gets the type of function pointed to. */
3511 fntype
= TREE_TYPE (fntype
);
3512 parm_types
= TYPE_ARG_TYPES (fntype
);
3516 allocated
= make_tree_vector ();
3517 params
= &allocated
;
3520 nargs
= convert_arguments (parm_types
, params
, fndecl
, LOOKUP_NORMAL
,
3523 return error_mark_node
;
3525 argarray
= (*params
)->address ();
3527 /* Check for errors in format strings and inappropriately
3529 check_function_arguments (fntype
, nargs
, argarray
);
3531 ret
= build_cxx_call (function
, nargs
, argarray
, complain
);
3533 if (allocated
!= NULL
)
3534 release_tree_vector (allocated
);
3539 /* Subroutine of convert_arguments.
3540 Warn about wrong number of args are genereted. */
3543 warn_args_num (location_t loc
, tree fndecl
, bool too_many_p
)
3547 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
3549 if (DECL_NAME (fndecl
) == NULL_TREE
3550 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl
)))
3553 ? G_("too many arguments to constructor %q#D")
3554 : G_("too few arguments to constructor %q#D"),
3559 ? G_("too many arguments to member function %q#D")
3560 : G_("too few arguments to member function %q#D"),
3566 ? G_("too many arguments to function %q#D")
3567 : G_("too few arguments to function %q#D"),
3569 inform (DECL_SOURCE_LOCATION (fndecl
),
3574 if (c_dialect_objc () && objc_message_selector ())
3577 ? G_("too many arguments to method %q#D")
3578 : G_("too few arguments to method %q#D"),
3579 objc_message_selector ());
3581 error_at (loc
, too_many_p
? G_("too many arguments to function")
3582 : G_("too few arguments to function"));
3586 /* Convert the actual parameter expressions in the list VALUES to the
3587 types in the list TYPELIST. The converted expressions are stored
3588 back in the VALUES vector.
3589 If parmdecls is exhausted, or when an element has NULL as its type,
3590 perform the default conversions.
3592 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3594 This is also where warnings about wrong number of args are generated.
3596 Returns the actual number of arguments processed (which might be less
3597 than the length of the vector), or -1 on error.
3599 In C++, unspecified trailing parameters can be filled in with their
3600 default arguments, if such were specified. Do so here. */
3603 convert_arguments (tree typelist
, vec
<tree
, va_gc
> **values
, tree fndecl
,
3604 int flags
, tsubst_flags_t complain
)
3609 /* Argument passing is always copy-initialization. */
3610 flags
|= LOOKUP_ONLYCONVERTING
;
3612 for (i
= 0, typetail
= typelist
;
3613 i
< vec_safe_length (*values
);
3616 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
3617 tree val
= (**values
)[i
];
3619 if (val
== error_mark_node
|| type
== error_mark_node
)
3622 if (type
== void_type_node
)
3624 if (complain
& tf_error
)
3626 warn_args_num (input_location
, fndecl
, /*too_many_p=*/true);
3633 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3634 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3635 if (TREE_CODE (val
) == NOP_EXPR
3636 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
3637 && (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
))
3638 val
= TREE_OPERAND (val
, 0);
3640 if (type
== 0 || TREE_CODE (type
) != REFERENCE_TYPE
)
3642 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
3643 || TREE_CODE (TREE_TYPE (val
)) == FUNCTION_TYPE
3644 || TREE_CODE (TREE_TYPE (val
)) == METHOD_TYPE
)
3645 val
= decay_conversion (val
, complain
);
3648 if (val
== error_mark_node
)
3653 /* Formal parm type is specified by a function prototype. */
3656 if (!COMPLETE_TYPE_P (complete_type (type
)))
3658 if (complain
& tf_error
)
3661 error ("parameter %P of %qD has incomplete type %qT",
3664 error ("parameter %P has incomplete type %qT", i
, type
);
3666 parmval
= error_mark_node
;
3670 parmval
= convert_for_initialization
3671 (NULL_TREE
, type
, val
, flags
,
3672 ICR_ARGPASS
, fndecl
, i
, complain
);
3673 parmval
= convert_for_arg_passing (type
, parmval
, complain
);
3676 if (parmval
== error_mark_node
)
3679 (**values
)[i
] = parmval
;
3683 if (fndecl
&& magic_varargs_p (fndecl
))
3684 /* Don't do ellipsis conversion for __built_in_constant_p
3685 as this will result in spurious errors for non-trivial
3687 val
= require_complete_type_sfinae (val
, complain
);
3689 val
= convert_arg_to_ellipsis (val
, complain
);
3691 (**values
)[i
] = val
;
3695 typetail
= TREE_CHAIN (typetail
);
3698 if (typetail
!= 0 && typetail
!= void_list_node
)
3700 /* See if there are default arguments that can be used. Because
3701 we hold default arguments in the FUNCTION_TYPE (which is so
3702 wrong), we can see default parameters here from deduced
3703 contexts (and via typeof) for indirect function calls.
3704 Fortunately we know whether we have a function decl to
3705 provide default arguments in a language conformant
3707 if (fndecl
&& TREE_PURPOSE (typetail
)
3708 && TREE_CODE (TREE_PURPOSE (typetail
)) != DEFAULT_ARG
)
3710 for (; typetail
!= void_list_node
; ++i
)
3713 = convert_default_arg (TREE_VALUE (typetail
),
3714 TREE_PURPOSE (typetail
),
3715 fndecl
, i
, complain
);
3717 if (parmval
== error_mark_node
)
3720 vec_safe_push (*values
, parmval
);
3721 typetail
= TREE_CHAIN (typetail
);
3722 /* ends with `...'. */
3723 if (typetail
== NULL_TREE
)
3729 if (complain
& tf_error
)
3730 warn_args_num (input_location
, fndecl
, /*too_many_p=*/false);
3738 /* Build a binary-operation expression, after performing default
3739 conversions on the operands. CODE is the kind of expression to
3740 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3741 are the tree codes which correspond to ARG1 and ARG2 when issuing
3742 warnings about possibly misplaced parentheses. They may differ
3743 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3744 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3745 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3746 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3747 ARG2_CODE as ERROR_MARK. */
3750 build_x_binary_op (location_t loc
, enum tree_code code
, tree arg1
,
3751 enum tree_code arg1_code
, tree arg2
,
3752 enum tree_code arg2_code
, tree
*overload
,
3753 tsubst_flags_t complain
)
3762 if (processing_template_decl
)
3764 if (type_dependent_expression_p (arg1
)
3765 || type_dependent_expression_p (arg2
))
3766 return build_min_nt_loc (loc
, code
, arg1
, arg2
);
3767 arg1
= build_non_dependent_expr (arg1
);
3768 arg2
= build_non_dependent_expr (arg2
);
3771 if (code
== DOTSTAR_EXPR
)
3772 expr
= build_m_component_ref (arg1
, arg2
, complain
);
3774 expr
= build_new_op (loc
, code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
,
3775 overload
, complain
);
3777 /* Check for cases such as x+y<<z which users are likely to
3778 misinterpret. But don't warn about obj << x + y, since that is a
3779 common idiom for I/O. */
3780 if (warn_parentheses
3781 && (complain
& tf_warning
)
3782 && !processing_template_decl
3783 && !error_operand_p (arg1
)
3784 && !error_operand_p (arg2
)
3785 && (code
!= LSHIFT_EXPR
3786 || !CLASS_TYPE_P (TREE_TYPE (arg1
))))
3787 warn_about_parentheses (loc
, code
, arg1_code
, orig_arg1
,
3788 arg2_code
, orig_arg2
);
3790 if (processing_template_decl
&& expr
!= error_mark_node
)
3791 return build_min_non_dep (code
, expr
, orig_arg1
, orig_arg2
);
3796 /* Build and return an ARRAY_REF expression. */
3799 build_x_array_ref (location_t loc
, tree arg1
, tree arg2
,
3800 tsubst_flags_t complain
)
3802 tree orig_arg1
= arg1
;
3803 tree orig_arg2
= arg2
;
3806 if (processing_template_decl
)
3808 if (type_dependent_expression_p (arg1
)
3809 || type_dependent_expression_p (arg2
))
3810 return build_min_nt_loc (loc
, ARRAY_REF
, arg1
, arg2
,
3811 NULL_TREE
, NULL_TREE
);
3812 arg1
= build_non_dependent_expr (arg1
);
3813 arg2
= build_non_dependent_expr (arg2
);
3816 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, arg1
, arg2
,
3817 NULL_TREE
, /*overload=*/NULL
, complain
);
3819 if (processing_template_decl
&& expr
!= error_mark_node
)
3820 return build_min_non_dep (ARRAY_REF
, expr
, orig_arg1
, orig_arg2
,
3821 NULL_TREE
, NULL_TREE
);
3825 /* Return whether OP is an expression of enum type cast to integer
3826 type. In C++ even unsigned enum types are cast to signed integer
3827 types. We do not want to issue warnings about comparisons between
3828 signed and unsigned types when one of the types is an enum type.
3829 Those warnings are always false positives in practice. */
3832 enum_cast_to_int (tree op
)
3834 if (TREE_CODE (op
) == NOP_EXPR
3835 && TREE_TYPE (op
) == integer_type_node
3836 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == ENUMERAL_TYPE
3837 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op
, 0))))
3840 /* The cast may have been pushed into a COND_EXPR. */
3841 if (TREE_CODE (op
) == COND_EXPR
)
3842 return (enum_cast_to_int (TREE_OPERAND (op
, 1))
3843 || enum_cast_to_int (TREE_OPERAND (op
, 2)));
3848 /* For the c-common bits. */
3850 build_binary_op (location_t location
, enum tree_code code
, tree op0
, tree op1
,
3853 return cp_build_binary_op (location
, code
, op0
, op1
, tf_warning_or_error
);
3857 /* Build a binary-operation expression without default conversions.
3858 CODE is the kind of expression to build.
3859 LOCATION is the location_t of the operator in the source code.
3860 This function differs from `build' in several ways:
3861 the data type of the result is computed and recorded in it,
3862 warnings are generated if arg data types are invalid,
3863 special handling for addition and subtraction of pointers is known,
3864 and some optimization is done (operations on narrow ints
3865 are done in the narrower type when that gives the same result).
3866 Constant folding is also done before the result is returned.
3868 Note that the operands will never have enumeral types
3869 because either they have just had the default conversions performed
3870 or they have both just been converted to some other type in which
3871 the arithmetic is to be done.
3873 C++: must do special pointer arithmetic when implementing
3874 multiple inheritance, and deal with pointer to member functions. */
3877 cp_build_binary_op (location_t location
,
3878 enum tree_code code
, tree orig_op0
, tree orig_op1
,
3879 tsubst_flags_t complain
)
3882 enum tree_code code0
, code1
;
3884 const char *invalid_op_diag
;
3886 /* Expression code to give to the expression when it is built.
3887 Normally this is CODE, which is what the caller asked for,
3888 but in some special cases we change it. */
3889 enum tree_code resultcode
= code
;
3891 /* Data type in which the computation is to be performed.
3892 In the simplest cases this is the common type of the arguments. */
3893 tree result_type
= NULL
;
3895 /* Nonzero means operands have already been type-converted
3896 in whatever way is necessary.
3897 Zero means they need to be converted to RESULT_TYPE. */
3900 /* Nonzero means create the expression with this type, rather than
3902 tree build_type
= 0;
3904 /* Nonzero means after finally constructing the expression
3905 convert it to this type. */
3906 tree final_type
= 0;
3909 tree orig_type
= NULL
;
3911 /* Nonzero if this is an operation like MIN or MAX which can
3912 safely be computed in short if both args are promoted shorts.
3913 Also implies COMMON.
3914 -1 indicates a bitwise operation; this makes a difference
3915 in the exact conditions for when it is safe to do the operation
3916 in a narrower mode. */
3919 /* Nonzero if this is a comparison operation;
3920 if both args are promoted shorts, compare the original shorts.
3921 Also implies COMMON. */
3922 int short_compare
= 0;
3924 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3927 /* True if both operands have arithmetic type. */
3928 bool arithmetic_types_p
;
3930 /* Apply default conversions. */
3934 /* Remember whether we're doing / or %. */
3935 bool doing_div_or_mod
= false;
3937 /* Remember whether we're doing << or >>. */
3938 bool doing_shift
= false;
3940 /* Tree holding instrumentation expression. */
3941 tree instrument_expr
= NULL
;
3943 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
3944 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
3945 || code
== TRUTH_XOR_EXPR
)
3947 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
3948 op0
= decay_conversion (op0
, complain
);
3949 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
3950 op1
= decay_conversion (op1
, complain
);
3954 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
3955 op0
= cp_default_conversion (op0
, complain
);
3956 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
3957 op1
= cp_default_conversion (op1
, complain
);
3960 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3961 STRIP_TYPE_NOPS (op0
);
3962 STRIP_TYPE_NOPS (op1
);
3964 /* DTRT if one side is an overloaded function, but complain about it. */
3965 if (type_unknown_p (op0
))
3967 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
3968 if (t
!= error_mark_node
)
3970 if (complain
& tf_error
)
3971 permerror (input_location
, "assuming cast to type %qT from overloaded function",
3976 if (type_unknown_p (op1
))
3978 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
3979 if (t
!= error_mark_node
)
3981 if (complain
& tf_error
)
3982 permerror (input_location
, "assuming cast to type %qT from overloaded function",
3988 type0
= TREE_TYPE (op0
);
3989 type1
= TREE_TYPE (op1
);
3991 /* The expression codes of the data types of the arguments tell us
3992 whether the arguments are integers, floating, pointers, etc. */
3993 code0
= TREE_CODE (type0
);
3994 code1
= TREE_CODE (type1
);
3996 /* If an error was already reported for one of the arguments,
3997 avoid reporting another error. */
3998 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
3999 return error_mark_node
;
4001 if ((invalid_op_diag
4002 = targetm
.invalid_binary_op (code
, type0
, type1
)))
4004 if (complain
& tf_error
)
4005 error (invalid_op_diag
);
4006 return error_mark_node
;
4009 /* Issue warnings about peculiar, but valid, uses of NULL. */
4010 if ((orig_op0
== null_node
|| orig_op1
== null_node
)
4011 /* It's reasonable to use pointer values as operands of &&
4012 and ||, so NULL is no exception. */
4013 && code
!= TRUTH_ANDIF_EXPR
&& code
!= TRUTH_ORIF_EXPR
4014 && ( /* Both are NULL (or 0) and the operation was not a
4015 comparison or a pointer subtraction. */
4016 (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)
4017 && code
!= EQ_EXPR
&& code
!= NE_EXPR
&& code
!= MINUS_EXPR
)
4018 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4019 || (!null_ptr_cst_p (orig_op0
)
4020 && !TYPE_PTR_OR_PTRMEM_P (type0
))
4021 || (!null_ptr_cst_p (orig_op1
)
4022 && !TYPE_PTR_OR_PTRMEM_P (type1
)))
4023 && (complain
& tf_warning
))
4025 source_location loc
=
4026 expansion_point_location_if_in_system_header (input_location
);
4028 warning_at (loc
, OPT_Wpointer_arith
, "NULL used in arithmetic");
4031 /* In case when one of the operands of the binary operation is
4032 a vector and another is a scalar -- convert scalar to vector. */
4033 if ((code0
== VECTOR_TYPE
) != (code1
== VECTOR_TYPE
))
4035 enum stv_conv convert_flag
= scalar_to_vector (location
, code
, op0
, op1
,
4036 complain
& tf_error
);
4038 switch (convert_flag
)
4041 return error_mark_node
;
4044 op0
= save_expr (op0
);
4045 op0
= convert (TREE_TYPE (type1
), op0
);
4046 op0
= build_vector_from_val (type1
, op0
);
4047 type0
= TREE_TYPE (op0
);
4048 code0
= TREE_CODE (type0
);
4054 op1
= save_expr (op1
);
4055 op1
= convert (TREE_TYPE (type0
), op1
);
4056 op1
= build_vector_from_val (type0
, op1
);
4057 type1
= TREE_TYPE (op1
);
4058 code1
= TREE_CODE (type1
);
4070 /* Subtraction of two similar pointers.
4071 We must subtract them as integers, then divide by object size. */
4072 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
4073 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4075 return pointer_diff (op0
, op1
, common_pointer_type (type0
, type1
),
4077 /* In all other cases except pointer - int, the usual arithmetic
4079 else if (!(code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
))
4084 /* The pointer - int case is just like pointer + int; fall
4087 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
4088 && (code0
== INTEGER_TYPE
|| code1
== INTEGER_TYPE
))
4092 ptr_operand
= ((code0
== POINTER_TYPE
) ? op0
: op1
);
4093 int_operand
= ((code0
== INTEGER_TYPE
) ? op0
: op1
);
4094 if (processing_template_decl
)
4096 result_type
= TREE_TYPE (ptr_operand
);
4099 return cp_pointer_int_sum (code
,
4111 case TRUNC_DIV_EXPR
:
4113 case FLOOR_DIV_EXPR
:
4114 case ROUND_DIV_EXPR
:
4115 case EXACT_DIV_EXPR
:
4116 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4117 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
4118 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4119 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
4121 enum tree_code tcode0
= code0
, tcode1
= code1
;
4122 tree cop1
= fold_non_dependent_expr_sfinae (op1
, tf_none
);
4123 cop1
= maybe_constant_value (cop1
);
4124 doing_div_or_mod
= true;
4125 warn_for_div_by_zero (location
, cop1
);
4127 if (tcode0
== COMPLEX_TYPE
|| tcode0
== VECTOR_TYPE
)
4128 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
4129 if (tcode1
== COMPLEX_TYPE
|| tcode1
== VECTOR_TYPE
)
4130 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
4132 if (!(tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
))
4133 resultcode
= RDIV_EXPR
;
4135 /* When dividing two signed integers, we have to promote to int.
4136 unless we divide by a constant != -1. Note that default
4137 conversion will have been performed on the operands at this
4138 point, so we have to dig out the original type to find out if
4140 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4141 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4142 || (TREE_CODE (op1
) == INTEGER_CST
4143 && ! integer_all_onesp (op1
)));
4152 if ((code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4153 || (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4154 && !VECTOR_FLOAT_TYPE_P (type0
)
4155 && !VECTOR_FLOAT_TYPE_P (type1
)))
4159 case TRUNC_MOD_EXPR
:
4160 case FLOOR_MOD_EXPR
:
4162 tree cop1
= fold_non_dependent_expr_sfinae (op1
, tf_none
);
4163 cop1
= maybe_constant_value (cop1
);
4164 doing_div_or_mod
= true;
4165 warn_for_div_by_zero (location
, cop1
);
4168 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4169 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4170 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
4172 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4174 /* Although it would be tempting to shorten always here, that loses
4175 on some targets, since the modulo instruction is undefined if the
4176 quotient can't be represented in the computation mode. We shorten
4177 only if unsigned or if dividing by something we know != -1. */
4178 shorten
= ((TREE_CODE (op0
) == NOP_EXPR
4179 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0
, 0))))
4180 || (TREE_CODE (op1
) == INTEGER_CST
4181 && ! integer_all_onesp (op1
)));
4186 case TRUTH_ANDIF_EXPR
:
4187 case TRUTH_ORIF_EXPR
:
4188 case TRUTH_AND_EXPR
:
4190 if (VECTOR_TYPE_P (type0
) || VECTOR_TYPE_P (type1
))
4192 sorry ("logical operation on vector type");
4193 return error_mark_node
;
4195 result_type
= boolean_type_node
;
4198 /* Shift operations: result has same type as first operand;
4199 always convert second operand to int.
4200 Also set SHORT_SHIFT if shifting rightward. */
4203 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4204 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4206 result_type
= type0
;
4209 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4210 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4211 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4212 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
4214 result_type
= type0
;
4217 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4219 tree const_op1
= fold_non_dependent_expr_sfinae (op1
, tf_none
);
4220 const_op1
= maybe_constant_value (const_op1
);
4221 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4223 result_type
= type0
;
4225 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4227 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4229 if ((complain
& tf_warning
)
4230 && c_inhibit_evaluation_warnings
== 0)
4231 warning (0, "right shift count is negative");
4235 if (compare_tree_int (const_op1
, TYPE_PRECISION (type0
)) >= 0
4236 && (complain
& tf_warning
)
4237 && c_inhibit_evaluation_warnings
== 0)
4238 warning (0, "right shift count >= width of type");
4241 /* Convert the shift-count to an integer, regardless of
4242 size of value being shifted. */
4243 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4244 op1
= cp_convert (integer_type_node
, op1
, complain
);
4245 /* Avoid converting op1 to result_type later. */
4251 if (code0
== VECTOR_TYPE
&& code1
== INTEGER_TYPE
4252 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
4254 result_type
= type0
;
4257 else if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
4258 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
4259 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
4260 && TYPE_VECTOR_SUBPARTS (type0
) == TYPE_VECTOR_SUBPARTS (type1
))
4262 result_type
= type0
;
4265 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4267 tree const_op1
= fold_non_dependent_expr_sfinae (op1
, tf_none
);
4268 const_op1
= maybe_constant_value (const_op1
);
4269 if (TREE_CODE (const_op1
) != INTEGER_CST
)
4271 result_type
= type0
;
4273 if (TREE_CODE (const_op1
) == INTEGER_CST
)
4275 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
4277 if ((complain
& tf_warning
)
4278 && c_inhibit_evaluation_warnings
== 0)
4279 warning (0, "left shift count is negative");
4281 else if (compare_tree_int (const_op1
,
4282 TYPE_PRECISION (type0
)) >= 0)
4284 if ((complain
& tf_warning
)
4285 && c_inhibit_evaluation_warnings
== 0)
4286 warning (0, "left shift count >= width of type");
4289 /* Convert the shift-count to an integer, regardless of
4290 size of value being shifted. */
4291 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4292 op1
= cp_convert (integer_type_node
, op1
, complain
);
4293 /* Avoid converting op1 to result_type later. */
4300 if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
4302 result_type
= type0
;
4303 if (TREE_CODE (op1
) == INTEGER_CST
)
4305 if (tree_int_cst_lt (op1
, integer_zero_node
))
4307 if (complain
& tf_warning
)
4308 warning (0, (code
== LROTATE_EXPR
)
4309 ? G_("left rotate count is negative")
4310 : G_("right rotate count is negative"));
4312 else if (compare_tree_int (op1
, TYPE_PRECISION (type0
)) >= 0)
4314 if (complain
& tf_warning
)
4315 warning (0, (code
== LROTATE_EXPR
)
4316 ? G_("left rotate count >= width of type")
4317 : G_("right rotate count >= width of type"));
4320 /* Convert the shift-count to an integer, regardless of
4321 size of value being shifted. */
4322 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1
)) != integer_type_node
)
4323 op1
= cp_convert (integer_type_node
, op1
, complain
);
4329 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4330 goto vector_compare
;
4331 if ((complain
& tf_warning
)
4332 && (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
)))
4333 warning (OPT_Wfloat_equal
,
4334 "comparing floating point with == or != is unsafe");
4335 if ((complain
& tf_warning
)
4336 && ((TREE_CODE (orig_op0
) == STRING_CST
&& !integer_zerop (op1
))
4337 || (TREE_CODE (orig_op1
) == STRING_CST
&& !integer_zerop (op0
))))
4338 warning (OPT_Waddress
, "comparison with string literal results in unspecified behaviour");
4340 build_type
= boolean_type_node
;
4341 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4342 || code0
== COMPLEX_TYPE
|| code0
== ENUMERAL_TYPE
)
4343 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4344 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
))
4346 else if ((code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4347 || (TYPE_PTRDATAMEM_P (type0
) && TYPE_PTRDATAMEM_P (type1
)))
4348 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4349 CPO_COMPARISON
, complain
);
4350 else if ((code0
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type0
))
4351 && null_ptr_cst_p (op1
))
4353 if (TREE_CODE (op0
) == ADDR_EXPR
4354 && decl_with_nonnull_addr_p (TREE_OPERAND (op0
, 0)))
4356 if ((complain
& tf_warning
)
4357 && c_inhibit_evaluation_warnings
== 0)
4358 warning (OPT_Waddress
, "the address of %qD will never be NULL",
4359 TREE_OPERAND (op0
, 0));
4361 result_type
= type0
;
4363 else if ((code1
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type1
))
4364 && null_ptr_cst_p (op0
))
4366 if (TREE_CODE (op1
) == ADDR_EXPR
4367 && decl_with_nonnull_addr_p (TREE_OPERAND (op1
, 0)))
4369 if ((complain
& tf_warning
)
4370 && c_inhibit_evaluation_warnings
== 0)
4371 warning (OPT_Waddress
, "the address of %qD will never be NULL",
4372 TREE_OPERAND (op1
, 0));
4374 result_type
= type1
;
4376 else if (null_ptr_cst_p (op0
) && null_ptr_cst_p (op1
))
4377 /* One of the operands must be of nullptr_t type. */
4378 result_type
= TREE_TYPE (nullptr_node
);
4379 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4381 result_type
= type0
;
4382 if (complain
& tf_error
)
4383 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4385 return error_mark_node
;
4387 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4389 result_type
= type1
;
4390 if (complain
& tf_error
)
4391 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4393 return error_mark_node
;
4395 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (op1
))
4397 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4398 == ptrmemfunc_vbit_in_delta
)
4400 tree pfn0
, delta0
, e1
, e2
;
4402 if (TREE_SIDE_EFFECTS (op0
))
4403 op0
= save_expr (op0
);
4405 pfn0
= pfn_from_ptrmemfunc (op0
);
4406 delta0
= delta_from_ptrmemfunc (op0
);
4407 e1
= cp_build_binary_op (location
,
4410 build_zero_cst (TREE_TYPE (pfn0
)),
4412 e2
= cp_build_binary_op (location
,
4418 if (complain
& tf_warning
)
4419 maybe_warn_zero_as_null_pointer_constant (op1
, input_location
);
4421 e2
= cp_build_binary_op (location
,
4422 EQ_EXPR
, e2
, integer_zero_node
,
4424 op0
= cp_build_binary_op (location
,
4425 TRUTH_ANDIF_EXPR
, e1
, e2
,
4427 op1
= cp_convert (TREE_TYPE (op0
), integer_one_node
, complain
);
4431 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
4432 op1
= cp_convert (TREE_TYPE (op0
), op1
, complain
);
4434 result_type
= TREE_TYPE (op0
);
4436 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (op0
))
4437 return cp_build_binary_op (location
, code
, op1
, op0
, complain
);
4438 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
))
4441 /* E will be the final comparison. */
4443 /* E1 and E2 are for scratch. */
4451 type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4452 CPO_COMPARISON
, complain
);
4454 if (!same_type_p (TREE_TYPE (op0
), type
))
4455 op0
= cp_convert_and_check (type
, op0
, complain
);
4456 if (!same_type_p (TREE_TYPE (op1
), type
))
4457 op1
= cp_convert_and_check (type
, op1
, complain
);
4459 if (op0
== error_mark_node
|| op1
== error_mark_node
)
4460 return error_mark_node
;
4462 if (TREE_SIDE_EFFECTS (op0
))
4463 op0
= save_expr (op0
);
4464 if (TREE_SIDE_EFFECTS (op1
))
4465 op1
= save_expr (op1
);
4467 pfn0
= pfn_from_ptrmemfunc (op0
);
4468 pfn1
= pfn_from_ptrmemfunc (op1
);
4469 delta0
= delta_from_ptrmemfunc (op0
);
4470 delta1
= delta_from_ptrmemfunc (op1
);
4471 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4472 == ptrmemfunc_vbit_in_delta
)
4477 && ((op0.delta == op1.delta)
4478 || (!op0.pfn && op0.delta & 1 == 0
4479 && op1.delta & 1 == 0))
4481 The reason for the `!op0.pfn' bit is that a NULL
4482 pointer-to-member is any member with a zero PFN and
4483 LSB of the DELTA field is 0. */
4485 e1
= cp_build_binary_op (location
, BIT_AND_EXPR
,
4489 e1
= cp_build_binary_op (location
,
4490 EQ_EXPR
, e1
, integer_zero_node
,
4492 e2
= cp_build_binary_op (location
, BIT_AND_EXPR
,
4496 e2
= cp_build_binary_op (location
,
4497 EQ_EXPR
, e2
, integer_zero_node
,
4499 e1
= cp_build_binary_op (location
,
4500 TRUTH_ANDIF_EXPR
, e2
, e1
,
4502 e2
= cp_build_binary_op (location
, EQ_EXPR
,
4504 build_zero_cst (TREE_TYPE (pfn0
)),
4506 e2
= cp_build_binary_op (location
,
4507 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
4508 e1
= cp_build_binary_op (location
,
4509 EQ_EXPR
, delta0
, delta1
, complain
);
4510 e1
= cp_build_binary_op (location
,
4511 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
4518 && (!op0.pfn || op0.delta == op1.delta))
4520 The reason for the `!op0.pfn' bit is that a NULL
4521 pointer-to-member is any member with a zero PFN; the
4522 DELTA field is unspecified. */
4524 e1
= cp_build_binary_op (location
,
4525 EQ_EXPR
, delta0
, delta1
, complain
);
4526 e2
= cp_build_binary_op (location
,
4529 build_zero_cst (TREE_TYPE (pfn0
)),
4531 e1
= cp_build_binary_op (location
,
4532 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
4534 e2
= build2 (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
4535 e
= cp_build_binary_op (location
,
4536 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
4537 if (code
== EQ_EXPR
)
4539 return cp_build_binary_op (location
,
4540 EQ_EXPR
, e
, integer_zero_node
, complain
);
4544 gcc_assert (!TYPE_PTRMEMFUNC_P (type0
)
4545 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
),
4547 gcc_assert (!TYPE_PTRMEMFUNC_P (type1
)
4548 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
),
4556 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
4557 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
4559 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4560 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4561 CPO_COMPARISON
, complain
);
4568 if (TREE_CODE (orig_op0
) == STRING_CST
4569 || TREE_CODE (orig_op1
) == STRING_CST
)
4571 if (complain
& tf_warning
)
4572 warning (OPT_Waddress
, "comparison with string literal results in unspecified behaviour");
4575 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4579 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
4581 && !vector_types_compatible_elements_p (type0
, type1
))
4583 if (complain
& tf_error
)
4585 error_at (location
, "comparing vectors with different "
4587 inform (location
, "operand types are %qT and %qT",
4590 return error_mark_node
;
4593 if (TYPE_VECTOR_SUBPARTS (type0
) != TYPE_VECTOR_SUBPARTS (type1
))
4595 if (complain
& tf_error
)
4597 error_at (location
, "comparing vectors with different "
4598 "number of elements");
4599 inform (location
, "operand types are %qT and %qT",
4602 return error_mark_node
;
4605 /* Always construct signed integer vector type. */
4606 intt
= c_common_type_for_size (GET_MODE_BITSIZE
4607 (TYPE_MODE (TREE_TYPE (type0
))), 0);
4610 if (complain
& tf_error
)
4611 error_at (location
, "could not find an integer type "
4612 "of the same size as %qT", TREE_TYPE (type0
));
4613 return error_mark_node
;
4615 result_type
= build_opaque_vector_type (intt
,
4616 TYPE_VECTOR_SUBPARTS (type0
));
4620 build_type
= boolean_type_node
;
4621 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
4622 || code0
== ENUMERAL_TYPE
)
4623 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4624 || code1
== ENUMERAL_TYPE
))
4626 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
4627 result_type
= composite_pointer_type (type0
, type1
, op0
, op1
,
4628 CPO_COMPARISON
, complain
);
4629 else if (code0
== POINTER_TYPE
&& null_ptr_cst_p (op1
))
4631 result_type
= type0
;
4632 if (extra_warnings
&& (complain
& tf_warning
))
4633 warning (OPT_Wextra
,
4634 "ordered comparison of pointer with integer zero");
4636 else if (code1
== POINTER_TYPE
&& null_ptr_cst_p (op0
))
4638 result_type
= type1
;
4639 if (extra_warnings
&& (complain
& tf_warning
))
4640 warning (OPT_Wextra
,
4641 "ordered comparison of pointer with integer zero");
4643 else if (null_ptr_cst_p (op0
) && null_ptr_cst_p (op1
))
4644 /* One of the operands must be of nullptr_t type. */
4645 result_type
= TREE_TYPE (nullptr_node
);
4646 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
4648 result_type
= type0
;
4649 if (complain
& tf_error
)
4650 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4652 return error_mark_node
;
4654 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
4656 result_type
= type1
;
4657 if (complain
& tf_error
)
4658 permerror (input_location
, "ISO C++ forbids comparison between pointer and integer");
4660 return error_mark_node
;
4664 case UNORDERED_EXPR
:
4671 build_type
= integer_type_node
;
4672 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
4674 if (complain
& tf_error
)
4675 error ("unordered comparison on non-floating point argument");
4676 return error_mark_node
;
4685 if (((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
4686 || code0
== ENUMERAL_TYPE
)
4687 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
4688 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
)))
4689 arithmetic_types_p
= 1;
4692 arithmetic_types_p
= 0;
4693 /* Vector arithmetic is only allowed when both sides are vectors. */
4694 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
)
4696 if (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
4697 || !vector_types_compatible_elements_p (type0
, type1
))
4699 if (complain
& tf_error
)
4700 binary_op_error (location
, code
, type0
, type1
);
4701 return error_mark_node
;
4703 arithmetic_types_p
= 1;
4706 /* Determine the RESULT_TYPE, if it is not already known. */
4708 && arithmetic_types_p
4709 && (shorten
|| common
|| short_compare
))
4711 result_type
= cp_common_type (type0
, type1
);
4712 if (complain
& tf_warning
)
4713 do_warn_double_promotion (result_type
, type0
, type1
,
4714 "implicit conversion from %qT to %qT "
4715 "to match other operand of binary "
4722 if (complain
& tf_error
)
4723 error ("invalid operands of types %qT and %qT to binary %qO",
4724 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
4725 return error_mark_node
;
4728 /* If we're in a template, the only thing we need to know is the
4730 if (processing_template_decl
)
4732 /* Since the middle-end checks the type when doing a build2, we
4733 need to build the tree in pieces. This built tree will never
4734 get out of the front-end as we replace it when instantiating
4736 tree tmp
= build2 (resultcode
,
4737 build_type
? build_type
: result_type
,
4739 TREE_OPERAND (tmp
, 0) = op0
;
4743 if (arithmetic_types_p
)
4745 bool first_complex
= (code0
== COMPLEX_TYPE
);
4746 bool second_complex
= (code1
== COMPLEX_TYPE
);
4747 int none_complex
= (!first_complex
&& !second_complex
);
4749 /* Adapted from patch for c/24581. */
4750 if (first_complex
!= second_complex
4751 && (code
== PLUS_EXPR
4752 || code
== MINUS_EXPR
4753 || code
== MULT_EXPR
4754 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
4755 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
4756 && flag_signed_zeros
)
4758 /* An operation on mixed real/complex operands must be
4759 handled specially, but the language-independent code can
4760 more easily optimize the plain complex arithmetic if
4761 -fno-signed-zeros. */
4762 tree real_type
= TREE_TYPE (result_type
);
4766 if (TREE_TYPE (op0
) != result_type
)
4767 op0
= cp_convert_and_check (result_type
, op0
, complain
);
4768 if (TREE_TYPE (op1
) != real_type
)
4769 op1
= cp_convert_and_check (real_type
, op1
, complain
);
4773 if (TREE_TYPE (op0
) != real_type
)
4774 op0
= cp_convert_and_check (real_type
, op0
, complain
);
4775 if (TREE_TYPE (op1
) != result_type
)
4776 op1
= cp_convert_and_check (result_type
, op1
, complain
);
4778 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
4779 return error_mark_node
;
4782 op0
= save_expr (op0
);
4783 real
= cp_build_unary_op (REALPART_EXPR
, op0
, 1, complain
);
4784 imag
= cp_build_unary_op (IMAGPART_EXPR
, op0
, 1, complain
);
4788 case TRUNC_DIV_EXPR
:
4789 op1
= save_expr (op1
);
4790 imag
= build2 (resultcode
, real_type
, imag
, op1
);
4794 real
= build2 (resultcode
, real_type
, real
, op1
);
4802 op1
= save_expr (op1
);
4803 real
= cp_build_unary_op (REALPART_EXPR
, op1
, 1, complain
);
4804 imag
= cp_build_unary_op (IMAGPART_EXPR
, op1
, 1, complain
);
4808 op0
= save_expr (op0
);
4809 imag
= build2 (resultcode
, real_type
, op0
, imag
);
4812 real
= build2 (resultcode
, real_type
, op0
, real
);
4815 real
= build2 (resultcode
, real_type
, op0
, real
);
4816 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
4822 real
= fold_if_not_in_template (real
);
4823 imag
= fold_if_not_in_template (imag
);
4824 result
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
4825 result
= fold_if_not_in_template (result
);
4829 /* For certain operations (which identify themselves by shorten != 0)
4830 if both args were extended from the same smaller type,
4831 do the arithmetic in that type and then extend.
4833 shorten !=0 and !=1 indicates a bitwise operation.
4834 For them, this optimization is safe only if
4835 both args are zero-extended or both are sign-extended.
4836 Otherwise, we might change the result.
4837 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4838 but calculated in (unsigned short) it would be (unsigned short)-1. */
4840 if (shorten
&& none_complex
)
4842 orig_type
= result_type
;
4843 final_type
= result_type
;
4844 result_type
= shorten_binary_op (result_type
, op0
, op1
,
4848 /* Comparison operations are shortened too but differently.
4849 They identify themselves by setting short_compare = 1. */
4853 /* Don't write &op0, etc., because that would prevent op0
4854 from being kept in a register.
4855 Instead, make copies of the our local variables and
4856 pass the copies by reference, then copy them back afterward. */
4857 tree xop0
= op0
, xop1
= op1
, xresult_type
= result_type
;
4858 enum tree_code xresultcode
= resultcode
;
4860 = shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
4863 return cp_convert (boolean_type_node
, val
, complain
);
4864 op0
= xop0
, op1
= xop1
;
4866 resultcode
= xresultcode
;
4869 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
4870 && warn_sign_compare
4871 /* Do not warn until the template is instantiated; we cannot
4872 bound the ranges of the arguments until that point. */
4873 && !processing_template_decl
4874 && (complain
& tf_warning
)
4875 && c_inhibit_evaluation_warnings
== 0
4876 /* Even unsigned enum types promote to signed int. We don't
4877 want to issue -Wsign-compare warnings for this case. */
4878 && !enum_cast_to_int (orig_op0
)
4879 && !enum_cast_to_int (orig_op1
))
4881 tree oop0
= maybe_constant_value (orig_op0
);
4882 tree oop1
= maybe_constant_value (orig_op1
);
4884 if (TREE_CODE (oop0
) != INTEGER_CST
)
4886 if (TREE_CODE (oop1
) != INTEGER_CST
)
4888 warn_for_sign_compare (location
, oop0
, oop1
, op0
, op1
,
4889 result_type
, resultcode
);
4893 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4894 Then the expression will be built.
4895 It will be given type FINAL_TYPE if that is nonzero;
4896 otherwise, it will be given type RESULT_TYPE. */
4899 if (TREE_TYPE (op0
) != result_type
)
4900 op0
= cp_convert_and_check (result_type
, op0
, complain
);
4901 if (TREE_TYPE (op1
) != result_type
)
4902 op1
= cp_convert_and_check (result_type
, op1
, complain
);
4904 if (op0
== error_mark_node
|| op1
== error_mark_node
)
4905 return error_mark_node
;
4908 if (build_type
== NULL_TREE
)
4909 build_type
= result_type
;
4911 if ((flag_sanitize
& (SANITIZE_SHIFT
| SANITIZE_DIVIDE
4912 | SANITIZE_FLOAT_DIVIDE
))
4913 && !processing_template_decl
4914 && current_function_decl
!= 0
4915 && !lookup_attribute ("no_sanitize_undefined",
4916 DECL_ATTRIBUTES (current_function_decl
))
4917 && (doing_div_or_mod
|| doing_shift
))
4919 /* OP0 and/or OP1 might have side-effects. */
4920 op0
= cp_save_expr (op0
);
4921 op1
= cp_save_expr (op1
);
4922 op0
= maybe_constant_value (fold_non_dependent_expr_sfinae (op0
,
4924 op1
= maybe_constant_value (fold_non_dependent_expr_sfinae (op1
,
4926 if (doing_div_or_mod
&& (flag_sanitize
& (SANITIZE_DIVIDE
4927 | SANITIZE_FLOAT_DIVIDE
)))
4929 /* For diagnostics we want to use the promoted types without
4930 shorten_binary_op. So convert the arguments to the
4931 original result_type. */
4934 if (orig_type
!= NULL
&& result_type
!= orig_type
)
4936 cop0
= cp_convert (orig_type
, op0
, complain
);
4937 cop1
= cp_convert (orig_type
, op1
, complain
);
4939 instrument_expr
= ubsan_instrument_division (location
, cop0
, cop1
);
4941 else if (doing_shift
&& (flag_sanitize
& SANITIZE_SHIFT
))
4942 instrument_expr
= ubsan_instrument_shift (location
, code
, op0
, op1
);
4945 result
= build2 (resultcode
, build_type
, op0
, op1
);
4946 result
= fold_if_not_in_template (result
);
4947 if (final_type
!= 0)
4948 result
= cp_convert (final_type
, result
, complain
);
4950 if (TREE_OVERFLOW_P (result
)
4951 && !TREE_OVERFLOW_P (op0
)
4952 && !TREE_OVERFLOW_P (op1
))
4953 overflow_warning (location
, result
);
4955 if (instrument_expr
!= NULL
)
4956 result
= fold_build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
4957 instrument_expr
, result
);
4962 /* Build a VEC_PERM_EXPR.
4963 This is a simple wrapper for c_build_vec_perm_expr. */
4965 build_x_vec_perm_expr (location_t loc
,
4966 tree arg0
, tree arg1
, tree arg2
,
4967 tsubst_flags_t complain
)
4969 tree orig_arg0
= arg0
;
4970 tree orig_arg1
= arg1
;
4971 tree orig_arg2
= arg2
;
4972 if (processing_template_decl
)
4974 if (type_dependent_expression_p (arg0
)
4975 || type_dependent_expression_p (arg1
)
4976 || type_dependent_expression_p (arg2
))
4977 return build_min_nt_loc (loc
, VEC_PERM_EXPR
, arg0
, arg1
, arg2
);
4978 arg0
= build_non_dependent_expr (arg0
);
4980 arg1
= build_non_dependent_expr (arg1
);
4981 arg2
= build_non_dependent_expr (arg2
);
4983 tree exp
= c_build_vec_perm_expr (loc
, arg0
, arg1
, arg2
, complain
& tf_error
);
4984 if (processing_template_decl
&& exp
!= error_mark_node
)
4985 return build_min_non_dep (VEC_PERM_EXPR
, exp
, orig_arg0
,
4986 orig_arg1
, orig_arg2
);
4990 /* Return a tree for the sum or difference (RESULTCODE says which)
4991 of pointer PTROP and integer INTOP. */
4994 cp_pointer_int_sum (enum tree_code resultcode
, tree ptrop
, tree intop
,
4995 tsubst_flags_t complain
)
4997 tree res_type
= TREE_TYPE (ptrop
);
4999 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5000 in certain circumstance (when it's valid to do so). So we need
5001 to make sure it's complete. We don't need to check here, if we
5002 can actually complete it at all, as those checks will be done in
5003 pointer_int_sum() anyway. */
5004 complete_type (TREE_TYPE (res_type
));
5006 return pointer_int_sum (input_location
, resultcode
, ptrop
,
5007 fold_if_not_in_template (intop
),
5008 complain
& tf_warning_or_error
);
5011 /* Return a tree for the difference of pointers OP0 and OP1.
5012 The resulting tree has type int. */
5015 pointer_diff (tree op0
, tree op1
, tree ptrtype
, tsubst_flags_t complain
)
5018 tree restype
= ptrdiff_type_node
;
5019 tree target_type
= TREE_TYPE (ptrtype
);
5021 if (!complete_type_or_else (target_type
, NULL_TREE
))
5022 return error_mark_node
;
5024 if (VOID_TYPE_P (target_type
))
5026 if (complain
& tf_error
)
5027 permerror (input_location
, "ISO C++ forbids using pointer of "
5028 "type %<void *%> in subtraction");
5030 return error_mark_node
;
5032 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
5034 if (complain
& tf_error
)
5035 permerror (input_location
, "ISO C++ forbids using pointer to "
5036 "a function in subtraction");
5038 return error_mark_node
;
5040 if (TREE_CODE (target_type
) == METHOD_TYPE
)
5042 if (complain
& tf_error
)
5043 permerror (input_location
, "ISO C++ forbids using pointer to "
5044 "a method in subtraction");
5046 return error_mark_node
;
5049 /* First do the subtraction as integers;
5050 then drop through to build the divide operator. */
5052 op0
= cp_build_binary_op (input_location
,
5054 cp_convert (restype
, op0
, complain
),
5055 cp_convert (restype
, op1
, complain
),
5058 /* This generates an error if op1 is a pointer to an incomplete type. */
5059 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
5061 if (complain
& tf_error
)
5062 error ("invalid use of a pointer to an incomplete type in "
5063 "pointer arithmetic");
5065 return error_mark_node
;
5068 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1
)))
5070 if (complain
& tf_error
)
5071 error ("arithmetic on pointer to an empty aggregate");
5073 return error_mark_node
;
5076 op1
= (TYPE_PTROB_P (ptrtype
)
5077 ? size_in_bytes (target_type
)
5078 : integer_one_node
);
5080 /* Do the division. */
5082 result
= build2 (EXACT_DIV_EXPR
, restype
, op0
,
5083 cp_convert (restype
, op1
, complain
));
5084 return fold_if_not_in_template (result
);
5087 /* Construct and perhaps optimize a tree representation
5088 for a unary operation. CODE, a tree_code, specifies the operation
5089 and XARG is the operand. */
5092 build_x_unary_op (location_t loc
, enum tree_code code
, tree xarg
,
5093 tsubst_flags_t complain
)
5095 tree orig_expr
= xarg
;
5099 if (processing_template_decl
)
5101 if (type_dependent_expression_p (xarg
))
5102 return build_min_nt_loc (loc
, code
, xarg
, NULL_TREE
);
5104 xarg
= build_non_dependent_expr (xarg
);
5109 /* [expr.unary.op] says:
5111 The address of an object of incomplete type can be taken.
5113 (And is just the ordinary address operator, not an overloaded
5114 "operator &".) However, if the type is a template
5115 specialization, we must complete the type at this point so that
5116 an overloaded "operator &" will be available if required. */
5117 if (code
== ADDR_EXPR
5118 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
5119 && ((CLASS_TYPE_P (TREE_TYPE (xarg
))
5120 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg
))))
5121 || (TREE_CODE (xarg
) == OFFSET_REF
)))
5122 /* Don't look for a function. */;
5124 exp
= build_new_op (loc
, code
, LOOKUP_NORMAL
, xarg
, NULL_TREE
,
5125 NULL_TREE
, /*overload=*/NULL
, complain
);
5126 if (!exp
&& code
== ADDR_EXPR
)
5128 if (is_overloaded_fn (xarg
))
5130 tree fn
= get_first_fn (xarg
);
5131 if (DECL_CONSTRUCTOR_P (fn
) || DECL_DESTRUCTOR_P (fn
))
5133 if (complain
& tf_error
)
5134 error (DECL_CONSTRUCTOR_P (fn
)
5135 ? G_("taking address of constructor %qE")
5136 : G_("taking address of destructor %qE"),
5138 return error_mark_node
;
5142 /* A pointer to member-function can be formed only by saying
5144 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
5145 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
5147 if (TREE_CODE (xarg
) != OFFSET_REF
5148 || !TYPE_P (TREE_OPERAND (xarg
, 0)))
5150 if (complain
& tf_error
)
5152 error ("invalid use of %qE to form a "
5153 "pointer-to-member-function", xarg
);
5154 if (TREE_CODE (xarg
) != OFFSET_REF
)
5155 inform (input_location
, " a qualified-id is required");
5157 return error_mark_node
;
5161 if (complain
& tf_error
)
5162 error ("parentheses around %qE cannot be used to form a"
5163 " pointer-to-member-function",
5166 return error_mark_node
;
5167 PTRMEM_OK_P (xarg
) = 1;
5171 if (TREE_CODE (xarg
) == OFFSET_REF
)
5173 ptrmem
= PTRMEM_OK_P (xarg
);
5175 if (!ptrmem
&& !flag_ms_extensions
5176 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
5178 /* A single non-static member, make sure we don't allow a
5179 pointer-to-member. */
5180 xarg
= build2 (OFFSET_REF
, TREE_TYPE (xarg
),
5181 TREE_OPERAND (xarg
, 0),
5182 ovl_cons (TREE_OPERAND (xarg
, 1), NULL_TREE
));
5183 PTRMEM_OK_P (xarg
) = ptrmem
;
5187 exp
= cp_build_addr_expr_strict (xarg
, complain
);
5190 if (processing_template_decl
&& exp
!= error_mark_node
)
5191 exp
= build_min_non_dep (code
, exp
, orig_expr
,
5192 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE
);
5193 if (TREE_CODE (exp
) == ADDR_EXPR
)
5194 PTRMEM_OK_P (exp
) = ptrmem
;
5198 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5199 constants, where a null value is represented by an INTEGER_CST of
5203 cp_truthvalue_conversion (tree expr
)
5205 tree type
= TREE_TYPE (expr
);
5206 if (TYPE_PTRDATAMEM_P (type
)
5207 /* Avoid ICE on invalid use of non-static member function. */
5208 || TREE_CODE (expr
) == FUNCTION_DECL
)
5209 return build_binary_op (EXPR_LOCATION (expr
),
5210 NE_EXPR
, expr
, nullptr_node
, 1);
5211 else if (TYPE_PTR_P (type
) || TYPE_PTRMEMFUNC_P (type
))
5213 /* With -Wzero-as-null-pointer-constant do not warn for an
5214 'if (p)' or a 'while (!p)', where p is a pointer. */
5216 ++c_inhibit_evaluation_warnings
;
5217 ret
= c_common_truthvalue_conversion (input_location
, expr
);
5218 --c_inhibit_evaluation_warnings
;
5222 return c_common_truthvalue_conversion (input_location
, expr
);
5225 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5228 condition_conversion (tree expr
)
5231 if (processing_template_decl
)
5233 t
= perform_implicit_conversion_flags (boolean_type_node
, expr
,
5234 tf_warning_or_error
, LOOKUP_NORMAL
);
5235 t
= fold_build_cleanup_point_expr (boolean_type_node
, t
);
5239 /* Returns the address of T. This function will fold away
5240 ADDR_EXPR of INDIRECT_REF. */
5243 build_address (tree t
)
5245 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
5246 return error_mark_node
;
5247 t
= build_fold_addr_expr (t
);
5248 if (TREE_CODE (t
) != ADDR_EXPR
)
5253 /* Returns the address of T with type TYPE. */
5256 build_typed_address (tree t
, tree type
)
5258 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
5259 return error_mark_node
;
5260 t
= build_fold_addr_expr_with_type (t
, type
);
5261 if (TREE_CODE (t
) != ADDR_EXPR
)
5266 /* Return a NOP_EXPR converting EXPR to TYPE. */
5269 build_nop (tree type
, tree expr
)
5271 if (type
== error_mark_node
|| error_operand_p (expr
))
5273 return build1 (NOP_EXPR
, type
, expr
);
5276 /* Take the address of ARG, whatever that means under C++ semantics.
5277 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5278 and class rvalues as well.
5280 Nothing should call this function directly; instead, callers should use
5281 cp_build_addr_expr or cp_build_addr_expr_strict. */
5284 cp_build_addr_expr_1 (tree arg
, bool strict_lvalue
, tsubst_flags_t complain
)
5289 if (!arg
|| error_operand_p (arg
))
5290 return error_mark_node
;
5292 arg
= mark_lvalue_use (arg
);
5293 argtype
= lvalue_type (arg
);
5295 gcc_assert (!identifier_p (arg
) || !IDENTIFIER_OPNAME_P (arg
));
5297 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
5298 && !really_overloaded_fn (TREE_OPERAND (arg
, 1)))
5300 /* They're trying to take the address of a unique non-static
5301 member function. This is ill-formed (except in MS-land),
5302 but let's try to DTRT.
5303 Note: We only handle unique functions here because we don't
5304 want to complain if there's a static overload; non-unique
5305 cases will be handled by instantiate_type. But we need to
5306 handle this case here to allow casts on the resulting PMF.
5307 We could defer this in non-MS mode, but it's easier to give
5308 a useful error here. */
5310 /* Inside constant member functions, the `this' pointer
5311 contains an extra const qualifier. TYPE_MAIN_VARIANT
5312 is used here to remove this const from the diagnostics
5313 and the created OFFSET_REF. */
5314 tree base
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg
, 0)));
5315 tree fn
= get_first_fn (TREE_OPERAND (arg
, 1));
5318 if (! flag_ms_extensions
)
5320 tree name
= DECL_NAME (fn
);
5321 if (!(complain
& tf_error
))
5322 return error_mark_node
;
5323 else if (current_class_type
5324 && TREE_OPERAND (arg
, 0) == current_class_ref
)
5325 /* An expression like &memfn. */
5326 permerror (input_location
, "ISO C++ forbids taking the address of an unqualified"
5327 " or parenthesized non-static member function to form"
5328 " a pointer to member function. Say %<&%T::%D%>",
5331 permerror (input_location
, "ISO C++ forbids taking the address of a bound member"
5332 " function to form a pointer to member function."
5336 arg
= build_offset_ref (base
, fn
, /*address_p=*/true, complain
);
5339 /* Uninstantiated types are all functions. Taking the
5340 address of a function is a no-op, so just return the
5342 if (type_unknown_p (arg
))
5343 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
5345 if (TREE_CODE (arg
) == OFFSET_REF
)
5346 /* We want a pointer to member; bypass all the code for actually taking
5347 the address of something. */
5350 /* Anything not already handled and not a true memory reference
5352 if (TREE_CODE (argtype
) != FUNCTION_TYPE
5353 && TREE_CODE (argtype
) != METHOD_TYPE
)
5355 cp_lvalue_kind kind
= lvalue_kind (arg
);
5356 if (kind
== clk_none
)
5358 if (complain
& tf_error
)
5359 lvalue_error (input_location
, lv_addressof
);
5360 return error_mark_node
;
5362 if (strict_lvalue
&& (kind
& (clk_rvalueref
|clk_class
)))
5364 if (!(complain
& tf_error
))
5365 return error_mark_node
;
5366 if (kind
& clk_class
)
5367 /* Make this a permerror because we used to accept it. */
5368 permerror (input_location
, "taking address of temporary");
5370 error ("taking address of xvalue (rvalue reference)");
5374 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
5376 tree type
= build_pointer_type (TREE_TYPE (argtype
));
5377 arg
= build1 (CONVERT_EXPR
, type
, arg
);
5380 else if (pedantic
&& DECL_MAIN_P (arg
))
5383 /* Apparently a lot of autoconf scripts for C++ packages do this,
5384 so only complain if -Wpedantic. */
5385 if (complain
& (flag_pedantic_errors
? tf_error
: tf_warning
))
5386 pedwarn (input_location
, OPT_Wpedantic
,
5387 "ISO C++ forbids taking address of function %<::main%>");
5388 else if (flag_pedantic_errors
)
5389 return error_mark_node
;
5392 /* Let &* cancel out to simplify resulting code. */
5393 if (INDIRECT_REF_P (arg
))
5395 /* We don't need to have `current_class_ptr' wrapped in a
5396 NON_LVALUE_EXPR node. */
5397 if (arg
== current_class_ref
)
5398 return current_class_ptr
;
5400 arg
= TREE_OPERAND (arg
, 0);
5401 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
)
5403 tree type
= build_pointer_type (TREE_TYPE (TREE_TYPE (arg
)));
5404 arg
= build1 (CONVERT_EXPR
, type
, arg
);
5407 /* Don't let this be an lvalue. */
5412 /* ??? Cope with user tricks that amount to offsetof. */
5413 if (TREE_CODE (argtype
) != FUNCTION_TYPE
5414 && TREE_CODE (argtype
) != METHOD_TYPE
5415 && argtype
!= unknown_type_node
5416 && (val
= get_base_address (arg
))
5417 && COMPLETE_TYPE_P (TREE_TYPE (val
))
5418 && INDIRECT_REF_P (val
)
5419 && TREE_CONSTANT (TREE_OPERAND (val
, 0)))
5421 tree type
= build_pointer_type (argtype
);
5422 return fold_convert (type
, fold_offsetof_1 (arg
));
5425 /* Handle complex lvalues (when permitted)
5426 by reduction to simpler cases. */
5427 val
= unary_complex_lvalue (ADDR_EXPR
, arg
);
5431 switch (TREE_CODE (arg
))
5435 case FIX_TRUNC_EXPR
:
5436 /* Even if we're not being pedantic, we cannot allow this
5437 extension when we're instantiating in a SFINAE
5439 if (! lvalue_p (arg
) && complain
== tf_none
)
5441 if (complain
& tf_error
)
5442 permerror (input_location
, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5444 return error_mark_node
;
5449 arg
= BASELINK_FUNCTIONS (arg
);
5453 arg
= OVL_CURRENT (arg
);
5458 /* Turn a reference to a non-static data member into a
5459 pointer-to-member. */
5464 gcc_assert (PTRMEM_OK_P (arg
));
5466 t
= TREE_OPERAND (arg
, 1);
5467 if (TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
5469 if (complain
& tf_error
)
5470 error ("cannot create pointer to reference member %qD", t
);
5471 return error_mark_node
;
5474 type
= build_ptrmem_type (context_for_name_lookup (t
),
5476 t
= make_ptrmem_cst (type
, TREE_OPERAND (arg
, 1));
5484 if (argtype
!= error_mark_node
)
5486 if (cxx_dialect
>= cxx1y
&& array_of_runtime_bound_p (argtype
)
5487 && (flag_iso
|| warn_vla
> 0))
5489 if (complain
& tf_warning_or_error
)
5490 pedwarn (input_location
, OPT_Wvla
,
5491 "taking address of array of runtime bound");
5493 return error_mark_node
;
5495 argtype
= build_pointer_type (argtype
);
5498 /* In a template, we are processing a non-dependent expression
5499 so we can just form an ADDR_EXPR with the correct type. */
5500 if (processing_template_decl
|| TREE_CODE (arg
) != COMPONENT_REF
)
5502 val
= build_address (arg
);
5503 if (TREE_CODE (arg
) == OFFSET_REF
)
5504 PTRMEM_OK_P (val
) = PTRMEM_OK_P (arg
);
5506 else if (BASELINK_P (TREE_OPERAND (arg
, 1)))
5508 tree fn
= BASELINK_FUNCTIONS (TREE_OPERAND (arg
, 1));
5510 /* We can only get here with a single static member
5512 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
5513 && DECL_STATIC_FUNCTION_P (fn
));
5515 val
= build_address (fn
);
5516 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
5517 /* Do not lose object's side effects. */
5518 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
),
5519 TREE_OPERAND (arg
, 0), val
);
5521 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg
, 1)))
5523 if (complain
& tf_error
)
5524 error ("attempt to take address of bit-field structure member %qD",
5525 TREE_OPERAND (arg
, 1));
5526 return error_mark_node
;
5530 tree object
= TREE_OPERAND (arg
, 0);
5531 tree field
= TREE_OPERAND (arg
, 1);
5532 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5533 (TREE_TYPE (object
), decl_type_context (field
)));
5534 val
= build_address (arg
);
5537 if (TYPE_PTR_P (argtype
)
5538 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
5540 build_ptrmemfunc_type (argtype
);
5541 val
= build_ptrmemfunc (argtype
, val
, 0,
5549 /* Take the address of ARG if it has one, even if it's an rvalue. */
5552 cp_build_addr_expr (tree arg
, tsubst_flags_t complain
)
5554 return cp_build_addr_expr_1 (arg
, 0, complain
);
5557 /* Take the address of ARG, but only if it's an lvalue. */
5560 cp_build_addr_expr_strict (tree arg
, tsubst_flags_t complain
)
5562 return cp_build_addr_expr_1 (arg
, 1, complain
);
5565 /* C++: Must handle pointers to members.
5567 Perhaps type instantiation should be extended to handle conversion
5568 from aggregates to types we don't yet know we want? (Or are those
5569 cases typically errors which should be reported?)
5571 NOCONVERT nonzero suppresses the default promotions
5572 (such as from short to int). */
5575 cp_build_unary_op (enum tree_code code
, tree xarg
, int noconvert
,
5576 tsubst_flags_t complain
)
5578 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5581 const char *errstring
= NULL
;
5583 const char *invalid_op_diag
;
5585 if (!arg
|| error_operand_p (arg
))
5586 return error_mark_node
;
5588 if ((invalid_op_diag
5589 = targetm
.invalid_unary_op ((code
== UNARY_PLUS_EXPR
5594 if (complain
& tf_error
)
5595 error (invalid_op_diag
);
5596 return error_mark_node
;
5601 case UNARY_PLUS_EXPR
:
5604 int flags
= WANT_ARITH
| WANT_ENUM
;
5605 /* Unary plus (but not unary minus) is allowed on pointers. */
5606 if (code
== UNARY_PLUS_EXPR
)
5607 flags
|= WANT_POINTER
;
5608 arg
= build_expr_type_conversion (flags
, arg
, true);
5610 errstring
= (code
== NEGATE_EXPR
5611 ? _("wrong type argument to unary minus")
5612 : _("wrong type argument to unary plus"));
5615 if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
5616 arg
= cp_perform_integral_promotions (arg
, complain
);
5618 /* Make sure the result is not an lvalue: a unary plus or minus
5619 expression is always a rvalue. */
5626 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
5631 arg
= cp_default_conversion (arg
, complain
);
5632 if (arg
== error_mark_node
)
5633 return error_mark_node
;
5636 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
5637 | WANT_VECTOR_OR_COMPLEX
,
5639 errstring
= _("wrong type argument to bit-complement");
5640 else if (!noconvert
&& CP_INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
5641 arg
= cp_perform_integral_promotions (arg
, complain
);
5645 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
5646 errstring
= _("wrong type argument to abs");
5647 else if (!noconvert
)
5649 arg
= cp_default_conversion (arg
, complain
);
5650 if (arg
== error_mark_node
)
5651 return error_mark_node
;
5656 /* Conjugating a real value is a no-op, but allow it anyway. */
5657 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
5658 errstring
= _("wrong type argument to conjugation");
5659 else if (!noconvert
)
5661 arg
= cp_default_conversion (arg
, complain
);
5662 if (arg
== error_mark_node
)
5663 return error_mark_node
;
5667 case TRUTH_NOT_EXPR
:
5668 arg
= perform_implicit_conversion (boolean_type_node
, arg
,
5670 val
= invert_truthvalue_loc (input_location
, arg
);
5671 if (arg
!= error_mark_node
)
5673 errstring
= _("in argument to unary !");
5681 arg
= build_real_imag_expr (input_location
, code
, arg
);
5682 if (arg
== error_mark_node
)
5685 return fold_if_not_in_template (arg
);
5687 case PREINCREMENT_EXPR
:
5688 case POSTINCREMENT_EXPR
:
5689 case PREDECREMENT_EXPR
:
5690 case POSTDECREMENT_EXPR
:
5691 /* Handle complex lvalues (when permitted)
5692 by reduction to simpler cases. */
5694 val
= unary_complex_lvalue (code
, arg
);
5698 arg
= mark_lvalue_use (arg
);
5700 /* Increment or decrement the real part of the value,
5701 and don't change the imaginary part. */
5702 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
5706 arg
= stabilize_reference (arg
);
5707 real
= cp_build_unary_op (REALPART_EXPR
, arg
, 1, complain
);
5708 imag
= cp_build_unary_op (IMAGPART_EXPR
, arg
, 1, complain
);
5709 real
= cp_build_unary_op (code
, real
, 1, complain
);
5710 if (real
== error_mark_node
|| imag
== error_mark_node
)
5711 return error_mark_node
;
5712 return build2 (COMPLEX_EXPR
, TREE_TYPE (arg
),
5716 /* Report invalid types. */
5718 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
5721 if (code
== PREINCREMENT_EXPR
)
5722 errstring
= _("no pre-increment operator for type");
5723 else if (code
== POSTINCREMENT_EXPR
)
5724 errstring
= _("no post-increment operator for type");
5725 else if (code
== PREDECREMENT_EXPR
)
5726 errstring
= _("no pre-decrement operator for type");
5728 errstring
= _("no post-decrement operator for type");
5731 else if (arg
== error_mark_node
)
5732 return error_mark_node
;
5734 /* Report something read-only. */
5736 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
5737 || TREE_READONLY (arg
))
5739 if (complain
& tf_error
)
5740 cxx_readonly_error (arg
, ((code
== PREINCREMENT_EXPR
5741 || code
== POSTINCREMENT_EXPR
)
5742 ? lv_increment
: lv_decrement
));
5744 return error_mark_node
;
5749 tree declared_type
= unlowered_expr_type (arg
);
5751 argtype
= TREE_TYPE (arg
);
5753 /* ARM $5.2.5 last annotation says this should be forbidden. */
5754 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
5756 if (complain
& tf_error
)
5757 permerror (input_location
, (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
)
5758 ? G_("ISO C++ forbids incrementing an enum")
5759 : G_("ISO C++ forbids decrementing an enum"));
5761 return error_mark_node
;
5764 /* Compute the increment. */
5766 if (TYPE_PTR_P (argtype
))
5768 tree type
= complete_type (TREE_TYPE (argtype
));
5770 if (!COMPLETE_OR_VOID_TYPE_P (type
))
5772 if (complain
& tf_error
)
5773 error (((code
== PREINCREMENT_EXPR
5774 || code
== POSTINCREMENT_EXPR
))
5775 ? G_("cannot increment a pointer to incomplete type %qT")
5776 : G_("cannot decrement a pointer to incomplete type %qT"),
5777 TREE_TYPE (argtype
));
5779 return error_mark_node
;
5781 else if (!TYPE_PTROB_P (argtype
))
5783 if (complain
& tf_error
)
5784 pedwarn (input_location
, OPT_Wpointer_arith
,
5785 (code
== PREINCREMENT_EXPR
5786 || code
== POSTINCREMENT_EXPR
)
5787 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5788 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5791 return error_mark_node
;
5794 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
5797 inc
= VECTOR_TYPE_P (argtype
)
5798 ? build_one_cst (argtype
)
5801 inc
= cp_convert (argtype
, inc
, complain
);
5803 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5804 need to ask Objective-C to build the increment or decrement
5805 expression for it. */
5806 if (objc_is_property_ref (arg
))
5807 return objc_build_incr_expr_for_property_ref (input_location
, code
,
5810 /* Complain about anything else that is not a true lvalue. */
5811 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
5812 || code
== POSTINCREMENT_EXPR
)
5813 ? lv_increment
: lv_decrement
),
5815 return error_mark_node
;
5817 /* Forbid using -- on `bool'. */
5818 if (TREE_CODE (declared_type
) == BOOLEAN_TYPE
)
5820 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
5822 if (complain
& tf_error
)
5823 error ("invalid use of Boolean expression as operand "
5824 "to %<operator--%>");
5825 return error_mark_node
;
5827 val
= boolean_increment (code
, arg
);
5829 else if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
5830 /* An rvalue has no cv-qualifiers. */
5831 val
= build2 (code
, cv_unqualified (TREE_TYPE (arg
)), arg
, inc
);
5833 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
5835 TREE_SIDE_EFFECTS (val
) = 1;
5840 /* Note that this operation never does default_conversion
5841 regardless of NOCONVERT. */
5842 return cp_build_addr_expr (arg
, complain
);
5851 argtype
= TREE_TYPE (arg
);
5852 return fold_if_not_in_template (build1 (code
, argtype
, arg
));
5855 if (complain
& tf_error
)
5856 error ("%s", errstring
);
5857 return error_mark_node
;
5860 /* Hook for the c-common bits that build a unary op. */
5862 build_unary_op (location_t
/*location*/,
5863 enum tree_code code
, tree xarg
, int noconvert
)
5865 return cp_build_unary_op (code
, xarg
, noconvert
, tf_warning_or_error
);
5868 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5869 for certain kinds of expressions which are not really lvalues
5870 but which we can accept as lvalues.
5872 If ARG is not a kind of expression we can handle, return
5876 unary_complex_lvalue (enum tree_code code
, tree arg
)
5878 /* Inside a template, making these kinds of adjustments is
5879 pointless; we are only concerned with the type of the
5881 if (processing_template_decl
)
5884 /* Handle (a, b) used as an "lvalue". */
5885 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
5887 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 1), 0,
5888 tf_warning_or_error
);
5889 return build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
5890 TREE_OPERAND (arg
, 0), real_result
);
5893 /* Handle (a ? b : c) used as an "lvalue". */
5894 if (TREE_CODE (arg
) == COND_EXPR
5895 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
5896 return rationalize_conditional_expr (code
, arg
, tf_warning_or_error
);
5898 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5899 if (TREE_CODE (arg
) == MODIFY_EXPR
5900 || TREE_CODE (arg
) == PREINCREMENT_EXPR
5901 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
5903 tree lvalue
= TREE_OPERAND (arg
, 0);
5904 if (TREE_SIDE_EFFECTS (lvalue
))
5906 lvalue
= stabilize_reference (lvalue
);
5907 arg
= build2 (TREE_CODE (arg
), TREE_TYPE (arg
),
5908 lvalue
, TREE_OPERAND (arg
, 1));
5910 return unary_complex_lvalue
5911 (code
, build2 (COMPOUND_EXPR
, TREE_TYPE (lvalue
), arg
, lvalue
));
5914 if (code
!= ADDR_EXPR
)
5917 /* Handle (a = b) used as an "lvalue" for `&'. */
5918 if (TREE_CODE (arg
) == MODIFY_EXPR
5919 || TREE_CODE (arg
) == INIT_EXPR
)
5921 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 0), 0,
5922 tf_warning_or_error
);
5923 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
5925 TREE_NO_WARNING (arg
) = 1;
5929 if (TREE_CODE (TREE_TYPE (arg
)) == FUNCTION_TYPE
5930 || TREE_CODE (TREE_TYPE (arg
)) == METHOD_TYPE
5931 || TREE_CODE (arg
) == OFFSET_REF
)
5934 /* We permit compiler to make function calls returning
5935 objects of aggregate type look like lvalues. */
5939 if (TREE_CODE (targ
) == SAVE_EXPR
)
5940 targ
= TREE_OPERAND (targ
, 0);
5942 if (TREE_CODE (targ
) == CALL_EXPR
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (targ
)))
5944 if (TREE_CODE (arg
) == SAVE_EXPR
)
5947 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, tf_warning_or_error
);
5948 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
5951 if (TREE_CODE (arg
) == SAVE_EXPR
&& INDIRECT_REF_P (targ
))
5952 return build3 (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
5953 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
5956 /* Don't let anything else be handled specially. */
5960 /* Mark EXP saying that we need to be able to take the
5961 address of it; it should not be allocated in a register.
5962 Value is true if successful.
5964 C++: we do not allow `current_class_ptr' to be addressable. */
5967 cxx_mark_addressable (tree exp
)
5972 switch (TREE_CODE (x
))
5979 x
= TREE_OPERAND (x
, 0);
5983 if (x
== current_class_ptr
)
5985 error ("cannot take the address of %<this%>, which is an rvalue expression");
5986 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later. */
5992 /* Caller should not be trying to mark initialized
5993 constant fields addressable. */
5994 gcc_assert (DECL_LANG_SPECIFIC (x
) == 0
5995 || DECL_IN_AGGR_P (x
) == 0
5997 || DECL_EXTERNAL (x
));
6001 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
6002 && !DECL_ARTIFICIAL (x
))
6004 if (VAR_P (x
) && DECL_HARD_REGISTER (x
))
6007 ("address of explicit register variable %qD requested", x
);
6010 else if (extra_warnings
)
6012 (OPT_Wextra
, "address requested for %qD, which is declared %<register%>", x
);
6014 TREE_ADDRESSABLE (x
) = 1;
6019 TREE_ADDRESSABLE (x
) = 1;
6023 TREE_ADDRESSABLE (x
) = 1;
6027 TREE_ADDRESSABLE (x
) = 1;
6028 cxx_mark_addressable (TREE_OPERAND (x
, 0));
6036 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6039 build_x_conditional_expr (location_t loc
, tree ifexp
, tree op1
, tree op2
,
6040 tsubst_flags_t complain
)
6042 tree orig_ifexp
= ifexp
;
6043 tree orig_op1
= op1
;
6044 tree orig_op2
= op2
;
6047 if (processing_template_decl
)
6049 /* The standard says that the expression is type-dependent if
6050 IFEXP is type-dependent, even though the eventual type of the
6051 expression doesn't dependent on IFEXP. */
6052 if (type_dependent_expression_p (ifexp
)
6053 /* As a GNU extension, the middle operand may be omitted. */
6054 || (op1
&& type_dependent_expression_p (op1
))
6055 || type_dependent_expression_p (op2
))
6056 return build_min_nt_loc (loc
, COND_EXPR
, ifexp
, op1
, op2
);
6057 ifexp
= build_non_dependent_expr (ifexp
);
6059 op1
= build_non_dependent_expr (op1
);
6060 op2
= build_non_dependent_expr (op2
);
6063 expr
= build_conditional_expr (loc
, ifexp
, op1
, op2
, complain
);
6064 if (processing_template_decl
&& expr
!= error_mark_node
6065 && TREE_CODE (expr
) != VEC_COND_EXPR
)
6067 tree min
= build_min_non_dep (COND_EXPR
, expr
,
6068 orig_ifexp
, orig_op1
, orig_op2
);
6069 /* In C++11, remember that the result is an lvalue or xvalue.
6070 In C++98, lvalue_kind can just assume lvalue in a template. */
6071 if (cxx_dialect
>= cxx11
6072 && lvalue_or_rvalue_with_address_p (expr
)
6073 && !lvalue_or_rvalue_with_address_p (min
))
6074 TREE_TYPE (min
) = cp_build_reference_type (TREE_TYPE (min
),
6075 !real_lvalue_p (expr
));
6076 expr
= convert_from_reference (min
);
6081 /* Given a list of expressions, return a compound expression
6082 that performs them all and returns the value of the last of them. */
6085 build_x_compound_expr_from_list (tree list
, expr_list_kind exp
,
6086 tsubst_flags_t complain
)
6088 tree expr
= TREE_VALUE (list
);
6090 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
6091 && !CONSTRUCTOR_IS_DIRECT_INIT (expr
))
6093 if (complain
& tf_error
)
6094 pedwarn (EXPR_LOC_OR_LOC (expr
, input_location
), 0,
6095 "list-initializer for non-class type must not "
6096 "be parenthesized");
6098 return error_mark_node
;
6101 if (TREE_CHAIN (list
))
6103 if (complain
& tf_error
)
6107 permerror (input_location
, "expression list treated as compound "
6108 "expression in initializer");
6111 permerror (input_location
, "expression list treated as compound "
6112 "expression in mem-initializer");
6115 permerror (input_location
, "expression list treated as compound "
6116 "expression in functional cast");
6122 return error_mark_node
;
6124 for (list
= TREE_CHAIN (list
); list
; list
= TREE_CHAIN (list
))
6125 expr
= build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list
)),
6126 expr
, TREE_VALUE (list
), complain
);
6132 /* Like build_x_compound_expr_from_list, but using a VEC. */
6135 build_x_compound_expr_from_vec (vec
<tree
, va_gc
> *vec
, const char *msg
,
6136 tsubst_flags_t complain
)
6138 if (vec_safe_is_empty (vec
))
6140 else if (vec
->length () == 1)
6150 if (complain
& tf_error
)
6151 permerror (input_location
,
6152 "%s expression list treated as compound expression",
6155 return error_mark_node
;
6159 for (ix
= 1; vec
->iterate (ix
, &t
); ++ix
)
6160 expr
= build_x_compound_expr (EXPR_LOCATION (t
), expr
,
6167 /* Handle overloading of the ',' operator when needed. */
6170 build_x_compound_expr (location_t loc
, tree op1
, tree op2
,
6171 tsubst_flags_t complain
)
6174 tree orig_op1
= op1
;
6175 tree orig_op2
= op2
;
6177 if (processing_template_decl
)
6179 if (type_dependent_expression_p (op1
)
6180 || type_dependent_expression_p (op2
))
6181 return build_min_nt_loc (loc
, COMPOUND_EXPR
, op1
, op2
);
6182 op1
= build_non_dependent_expr (op1
);
6183 op2
= build_non_dependent_expr (op2
);
6186 result
= build_new_op (loc
, COMPOUND_EXPR
, LOOKUP_NORMAL
, op1
, op2
,
6187 NULL_TREE
, /*overload=*/NULL
, complain
);
6189 result
= cp_build_compound_expr (op1
, op2
, complain
);
6191 if (processing_template_decl
&& result
!= error_mark_node
)
6192 return build_min_non_dep (COMPOUND_EXPR
, result
, orig_op1
, orig_op2
);
6197 /* Like cp_build_compound_expr, but for the c-common bits. */
6200 build_compound_expr (location_t
/*loc*/, tree lhs
, tree rhs
)
6202 return cp_build_compound_expr (lhs
, rhs
, tf_warning_or_error
);
6205 /* Build a compound expression. */
6208 cp_build_compound_expr (tree lhs
, tree rhs
, tsubst_flags_t complain
)
6210 lhs
= convert_to_void (lhs
, ICV_LEFT_OF_COMMA
, complain
);
6212 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
6213 return error_mark_node
;
6216 && (TREE_CODE (lhs
) == CILK_SPAWN_STMT
6217 || TREE_CODE (rhs
) == CILK_SPAWN_STMT
))
6219 location_t loc
= (EXPR_HAS_LOCATION (lhs
) ? EXPR_LOCATION (lhs
)
6220 : EXPR_LOCATION (rhs
));
6222 "spawned function call cannot be part of a comma expression");
6223 return error_mark_node
;
6226 if (TREE_CODE (rhs
) == TARGET_EXPR
)
6228 /* If the rhs is a TARGET_EXPR, then build the compound
6229 expression inside the target_expr's initializer. This
6230 helps the compiler to eliminate unnecessary temporaries. */
6231 tree init
= TREE_OPERAND (rhs
, 1);
6233 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), lhs
, init
);
6234 TREE_OPERAND (rhs
, 1) = init
;
6239 if (type_unknown_p (rhs
))
6241 if (complain
& tf_error
)
6242 error ("no context to resolve type of %qE", rhs
);
6243 return error_mark_node
;
6246 return build2 (COMPOUND_EXPR
, TREE_TYPE (rhs
), lhs
, rhs
);
6249 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6250 casts away constness. CAST gives the type of cast. Returns true
6251 if the cast is ill-formed, false if it is well-formed.
6253 ??? This function warns for casting away any qualifier not just
6254 const. We would like to specify exactly what qualifiers are casted
6259 check_for_casting_away_constness (tree src_type
, tree dest_type
,
6260 enum tree_code cast
, tsubst_flags_t complain
)
6262 /* C-style casts are allowed to cast away constness. With
6263 WARN_CAST_QUAL, we still want to issue a warning. */
6264 if (cast
== CAST_EXPR
&& !warn_cast_qual
)
6267 if (!casts_away_constness (src_type
, dest_type
, complain
))
6273 if (complain
& tf_warning
)
6274 warning (OPT_Wcast_qual
,
6275 "cast from type %qT to type %qT casts away qualifiers",
6276 src_type
, dest_type
);
6279 case STATIC_CAST_EXPR
:
6280 if (complain
& tf_error
)
6281 error ("static_cast from type %qT to type %qT casts away qualifiers",
6282 src_type
, dest_type
);
6285 case REINTERPRET_CAST_EXPR
:
6286 if (complain
& tf_error
)
6287 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6288 src_type
, dest_type
);
6297 Warns if the cast from expression EXPR to type TYPE is useless.
6300 maybe_warn_about_useless_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6302 if (warn_useless_cast
6303 && complain
& tf_warning
)
6305 /* In C++14 mode, this interacts badly with force_paren_expr. And it
6306 isn't necessary in any mode, because the code below handles
6307 glvalues properly. For 4.9, just skip it in C++14 mode. */
6308 if (cxx_dialect
< cxx1y
&& REFERENCE_REF_P (expr
))
6309 expr
= TREE_OPERAND (expr
, 0);
6311 if ((TREE_CODE (type
) == REFERENCE_TYPE
6312 && (TYPE_REF_IS_RVALUE (type
)
6313 ? xvalue_p (expr
) : real_lvalue_p (expr
))
6314 && same_type_p (TREE_TYPE (expr
), TREE_TYPE (type
)))
6315 || same_type_p (TREE_TYPE (expr
), type
))
6316 warning (OPT_Wuseless_cast
, "useless cast to type %qT", type
);
6320 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6321 (another pointer-to-member type in the same hierarchy) and return
6322 the converted expression. If ALLOW_INVERSE_P is permitted, a
6323 pointer-to-derived may be converted to pointer-to-base; otherwise,
6324 only the other direction is permitted. If C_CAST_P is true, this
6325 conversion is taking place as part of a C-style cast. */
6328 convert_ptrmem (tree type
, tree expr
, bool allow_inverse_p
,
6329 bool c_cast_p
, tsubst_flags_t complain
)
6331 if (TYPE_PTRDATAMEM_P (type
))
6335 if (TREE_CODE (expr
) == PTRMEM_CST
)
6336 expr
= cplus_expand_constant (expr
);
6337 delta
= get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr
)),
6338 TYPE_PTRMEM_CLASS_TYPE (type
),
6340 c_cast_p
, complain
);
6341 if (delta
== error_mark_node
)
6342 return error_mark_node
;
6344 if (!integer_zerop (delta
))
6346 tree cond
, op1
, op2
;
6348 cond
= cp_build_binary_op (input_location
,
6351 build_int_cst (TREE_TYPE (expr
), -1),
6353 op1
= build_nop (ptrdiff_type_node
, expr
);
6354 op2
= cp_build_binary_op (input_location
,
6355 PLUS_EXPR
, op1
, delta
,
6358 expr
= fold_build3_loc (input_location
,
6359 COND_EXPR
, ptrdiff_type_node
, cond
, op1
, op2
);
6363 return build_nop (type
, expr
);
6366 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
,
6367 allow_inverse_p
, c_cast_p
, complain
);
6370 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6371 this static_cast is being attempted as one of the possible casts
6372 allowed by a C-style cast. (In that case, accessibility of base
6373 classes is not considered, and it is OK to cast away
6374 constness.) Return the result of the cast. *VALID_P is set to
6375 indicate whether or not the cast was valid. */
6378 build_static_cast_1 (tree type
, tree expr
, bool c_cast_p
,
6379 bool *valid_p
, tsubst_flags_t complain
)
6385 /* Assume the cast is valid. */
6388 intype
= unlowered_expr_type (expr
);
6390 /* Save casted types in the function's used types hash table. */
6391 used_types_insert (type
);
6393 /* [expr.static.cast]
6395 An lvalue of type "cv1 B", where B is a class type, can be cast
6396 to type "reference to cv2 D", where D is a class derived (clause
6397 _class.derived_) from B, if a valid standard conversion from
6398 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6399 same cv-qualification as, or greater cv-qualification than, cv1,
6400 and B is not a virtual base class of D. */
6401 /* We check this case before checking the validity of "TYPE t =
6402 EXPR;" below because for this case:
6405 struct D : public B { D(const B&); };
6407 void f() { static_cast<const D&>(b); }
6409 we want to avoid constructing a new D. The standard is not
6410 completely clear about this issue, but our interpretation is
6411 consistent with other compilers. */
6412 if (TREE_CODE (type
) == REFERENCE_TYPE
6413 && CLASS_TYPE_P (TREE_TYPE (type
))
6414 && CLASS_TYPE_P (intype
)
6415 && (TYPE_REF_IS_RVALUE (type
) || real_lvalue_p (expr
))
6416 && DERIVED_FROM_P (intype
, TREE_TYPE (type
))
6417 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype
)),
6418 build_pointer_type (TYPE_MAIN_VARIANT
6419 (TREE_TYPE (type
))),
6422 || at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
6426 /* There is a standard conversion from "D*" to "B*" even if "B"
6427 is ambiguous or inaccessible. If this is really a
6428 static_cast, then we check both for inaccessibility and
6429 ambiguity. However, if this is a static_cast being performed
6430 because the user wrote a C-style cast, then accessibility is
6432 base
= lookup_base (TREE_TYPE (type
), intype
,
6433 c_cast_p
? ba_unique
: ba_check
,
6436 /* Convert from "B*" to "D*". This function will check that "B"
6437 is not a virtual base of "D". */
6438 expr
= build_base_path (MINUS_EXPR
, build_address (expr
),
6439 base
, /*nonnull=*/false, complain
);
6440 /* Convert the pointer to a reference -- but then remember that
6441 there are no expressions with reference type in C++.
6443 We call rvalue so that there's an actual tree code
6444 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6445 is a variable with the same type, the conversion would get folded
6446 away, leaving just the variable and causing lvalue_kind to give
6447 the wrong answer. */
6448 return convert_from_reference (rvalue (cp_fold_convert (type
, expr
)));
6451 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6452 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6453 if (TREE_CODE (type
) == REFERENCE_TYPE
6454 && TYPE_REF_IS_RVALUE (type
)
6455 && (clk
= real_lvalue_p (expr
))
6456 && reference_related_p (TREE_TYPE (type
), intype
)
6457 && (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
6459 if (clk
== clk_ordinary
)
6461 /* Handle the (non-bit-field) lvalue case here by casting to
6462 lvalue reference and then changing it to an rvalue reference.
6463 Casting an xvalue to rvalue reference will be handled by the
6465 tree lref
= cp_build_reference_type (TREE_TYPE (type
), false);
6466 result
= (perform_direct_initialization_if_possible
6467 (lref
, expr
, c_cast_p
, complain
));
6468 result
= cp_fold_convert (type
, result
);
6469 /* Make sure we don't fold back down to a named rvalue reference,
6470 because that would be an lvalue. */
6471 if (DECL_P (result
))
6472 result
= build1 (NON_LVALUE_EXPR
, type
, result
);
6473 return convert_from_reference (result
);
6476 /* For a bit-field or packed field, bind to a temporary. */
6477 expr
= rvalue (expr
);
6480 /* Resolve overloaded address here rather than once in
6481 implicit_conversion and again in the inverse code below. */
6482 if (TYPE_PTRMEMFUNC_P (type
) && type_unknown_p (expr
))
6484 expr
= instantiate_type (type
, expr
, complain
);
6485 intype
= TREE_TYPE (expr
);
6488 /* [expr.static.cast]
6490 Any expression can be explicitly converted to type cv void. */
6491 if (VOID_TYPE_P (type
))
6492 return convert_to_void (expr
, ICV_CAST
, complain
);
6495 An abstract class shall not be used ... as the type of an explicit
6497 if (abstract_virtuals_error_sfinae (ACU_CAST
, type
, complain
))
6498 return error_mark_node
;
6500 /* [expr.static.cast]
6502 An expression e can be explicitly converted to a type T using a
6503 static_cast of the form static_cast<T>(e) if the declaration T
6504 t(e);" is well-formed, for some invented temporary variable
6506 result
= perform_direct_initialization_if_possible (type
, expr
,
6507 c_cast_p
, complain
);
6510 result
= convert_from_reference (result
);
6512 /* [expr.static.cast]
6514 If T is a reference type, the result is an lvalue; otherwise,
6515 the result is an rvalue. */
6516 if (TREE_CODE (type
) != REFERENCE_TYPE
)
6517 result
= rvalue (result
);
6521 /* [expr.static.cast]
6523 The inverse of any standard conversion sequence (clause _conv_),
6524 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6525 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6526 (_conv.bool_) conversions, can be performed explicitly using
6527 static_cast subject to the restriction that the explicit
6528 conversion does not cast away constness (_expr.const.cast_), and
6529 the following additional rules for specific cases: */
6530 /* For reference, the conversions not excluded are: integral
6531 promotions, floating point promotion, integral conversions,
6532 floating point conversions, floating-integral conversions,
6533 pointer conversions, and pointer to member conversions. */
6536 A value of integral _or enumeration_ type can be explicitly
6537 converted to an enumeration type. */
6538 /* The effect of all that is that any conversion between any two
6539 types which are integral, floating, or enumeration types can be
6541 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6542 || SCALAR_FLOAT_TYPE_P (type
))
6543 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
6544 || SCALAR_FLOAT_TYPE_P (intype
)))
6545 return ocp_convert (type
, expr
, CONV_C_CAST
, LOOKUP_NORMAL
, complain
);
6547 if (TYPE_PTR_P (type
) && TYPE_PTR_P (intype
)
6548 && CLASS_TYPE_P (TREE_TYPE (type
))
6549 && CLASS_TYPE_P (TREE_TYPE (intype
))
6550 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6551 (TREE_TYPE (intype
))),
6552 build_pointer_type (TYPE_MAIN_VARIANT
6553 (TREE_TYPE (type
))),
6559 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
6561 return error_mark_node
;
6562 base
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
6563 c_cast_p
? ba_unique
: ba_check
,
6565 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
6567 return cp_fold_convert(type
, expr
);
6570 if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
6571 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
6578 c1
= TYPE_PTRMEM_CLASS_TYPE (intype
);
6579 c2
= TYPE_PTRMEM_CLASS_TYPE (type
);
6581 if (TYPE_PTRDATAMEM_P (type
))
6583 t1
= (build_ptrmem_type
6585 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype
))));
6586 t2
= (build_ptrmem_type
6588 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
6595 if (can_convert (t1
, t2
, complain
) || can_convert (t2
, t1
, complain
))
6598 && check_for_casting_away_constness (intype
, type
,
6601 return error_mark_node
;
6602 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/1,
6603 c_cast_p
, complain
);
6607 /* [expr.static.cast]
6609 An rvalue of type "pointer to cv void" can be explicitly
6610 converted to a pointer to object type. A value of type pointer
6611 to object converted to "pointer to cv void" and back to the
6612 original pointer type will have its original value. */
6613 if (TYPE_PTR_P (intype
)
6614 && VOID_TYPE_P (TREE_TYPE (intype
))
6615 && TYPE_PTROB_P (type
))
6618 && check_for_casting_away_constness (intype
, type
, STATIC_CAST_EXPR
,
6620 return error_mark_node
;
6621 return build_nop (type
, expr
);
6625 return error_mark_node
;
6628 /* Return an expression representing static_cast<TYPE>(EXPR). */
6631 build_static_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6636 if (type
== error_mark_node
|| expr
== error_mark_node
)
6637 return error_mark_node
;
6639 if (processing_template_decl
)
6641 expr
= build_min (STATIC_CAST_EXPR
, type
, expr
);
6642 /* We don't know if it will or will not have side effects. */
6643 TREE_SIDE_EFFECTS (expr
) = 1;
6644 return convert_from_reference (expr
);
6647 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6648 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6649 if (TREE_CODE (type
) != REFERENCE_TYPE
6650 && TREE_CODE (expr
) == NOP_EXPR
6651 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
6652 expr
= TREE_OPERAND (expr
, 0);
6654 result
= build_static_cast_1 (type
, expr
, /*c_cast_p=*/false, &valid_p
,
6658 if (result
!= error_mark_node
)
6659 maybe_warn_about_useless_cast (type
, expr
, complain
);
6663 if (complain
& tf_error
)
6664 error ("invalid static_cast from type %qT to type %qT",
6665 TREE_TYPE (expr
), type
);
6666 return error_mark_node
;
6669 /* EXPR is an expression with member function or pointer-to-member
6670 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6671 not permitted by ISO C++, but we accept it in some modes. If we
6672 are not in one of those modes, issue a diagnostic. Return the
6673 converted expression. */
6676 convert_member_func_to_ptr (tree type
, tree expr
, tsubst_flags_t complain
)
6681 intype
= TREE_TYPE (expr
);
6682 gcc_assert (TYPE_PTRMEMFUNC_P (intype
)
6683 || TREE_CODE (intype
) == METHOD_TYPE
);
6685 if (!(complain
& tf_warning_or_error
))
6686 return error_mark_node
;
6688 if (pedantic
|| warn_pmf2ptr
)
6689 pedwarn (input_location
, pedantic
? OPT_Wpedantic
: OPT_Wpmf_conversions
,
6690 "converting from %qT to %qT", intype
, type
);
6692 if (TREE_CODE (intype
) == METHOD_TYPE
)
6693 expr
= build_addr_func (expr
, complain
);
6694 else if (TREE_CODE (expr
) == PTRMEM_CST
)
6695 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
6698 decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
), 0);
6699 decl
= build_address (decl
);
6700 expr
= get_member_function_from_ptrfunc (&decl
, expr
, complain
);
6703 if (expr
== error_mark_node
)
6704 return error_mark_node
;
6706 return build_nop (type
, expr
);
6709 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6710 If C_CAST_P is true, this reinterpret cast is being done as part of
6711 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6712 indicate whether or not reinterpret_cast was valid. */
6715 build_reinterpret_cast_1 (tree type
, tree expr
, bool c_cast_p
,
6716 bool *valid_p
, tsubst_flags_t complain
)
6720 /* Assume the cast is invalid. */
6724 if (type
== error_mark_node
|| error_operand_p (expr
))
6725 return error_mark_node
;
6727 intype
= TREE_TYPE (expr
);
6729 /* Save casted types in the function's used types hash table. */
6730 used_types_insert (type
);
6732 /* [expr.reinterpret.cast]
6733 An lvalue expression of type T1 can be cast to the type
6734 "reference to T2" if an expression of type "pointer to T1" can be
6735 explicitly converted to the type "pointer to T2" using a
6736 reinterpret_cast. */
6737 if (TREE_CODE (type
) == REFERENCE_TYPE
)
6739 if (! real_lvalue_p (expr
))
6741 if (complain
& tf_error
)
6742 error ("invalid cast of an rvalue expression of type "
6745 return error_mark_node
;
6748 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6749 "B" are related class types; the reinterpret_cast does not
6750 adjust the pointer. */
6751 if (TYPE_PTR_P (intype
)
6752 && (complain
& tf_warning
)
6753 && (comptypes (TREE_TYPE (intype
), TREE_TYPE (type
),
6754 COMPARE_BASE
| COMPARE_DERIVED
)))
6755 warning (0, "casting %qT to %qT does not dereference pointer",
6758 expr
= cp_build_addr_expr (expr
, complain
);
6760 if (warn_strict_aliasing
> 2)
6761 strict_aliasing_warning (TREE_TYPE (expr
), type
, expr
);
6763 if (expr
!= error_mark_node
)
6764 expr
= build_reinterpret_cast_1
6765 (build_pointer_type (TREE_TYPE (type
)), expr
, c_cast_p
,
6767 if (expr
!= error_mark_node
)
6768 /* cp_build_indirect_ref isn't right for rvalue refs. */
6769 expr
= convert_from_reference (fold_convert (type
, expr
));
6773 /* As a G++ extension, we consider conversions from member
6774 functions, and pointers to member functions to
6775 pointer-to-function and pointer-to-void types. If
6776 -Wno-pmf-conversions has not been specified,
6777 convert_member_func_to_ptr will issue an error message. */
6778 if ((TYPE_PTRMEMFUNC_P (intype
)
6779 || TREE_CODE (intype
) == METHOD_TYPE
)
6780 && TYPE_PTR_P (type
)
6781 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
6782 || VOID_TYPE_P (TREE_TYPE (type
))))
6783 return convert_member_func_to_ptr (type
, expr
, complain
);
6785 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6786 array-to-pointer, and function-to-pointer conversions are
6788 expr
= decay_conversion (expr
, complain
);
6790 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6791 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6792 if (TREE_CODE (expr
) == NOP_EXPR
6793 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
6794 expr
= TREE_OPERAND (expr
, 0);
6796 if (error_operand_p (expr
))
6797 return error_mark_node
;
6799 intype
= TREE_TYPE (expr
);
6801 /* [expr.reinterpret.cast]
6802 A pointer can be converted to any integral type large enough to
6803 hold it. ... A value of type std::nullptr_t can be converted to
6804 an integral type; the conversion has the same meaning and
6805 validity as a conversion of (void*)0 to the integral type. */
6806 if (CP_INTEGRAL_TYPE_P (type
)
6807 && (TYPE_PTR_P (intype
) || NULLPTR_TYPE_P (intype
)))
6809 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
6811 if (complain
& tf_error
)
6812 permerror (input_location
, "cast from %qT to %qT loses precision",
6815 return error_mark_node
;
6817 if (NULLPTR_TYPE_P (intype
))
6818 return build_int_cst (type
, 0);
6820 /* [expr.reinterpret.cast]
6821 A value of integral or enumeration type can be explicitly
6822 converted to a pointer. */
6823 else if (TYPE_PTR_P (type
) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype
))
6826 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
6827 || TYPE_PTR_OR_PTRMEM_P (type
))
6828 && same_type_p (type
, intype
))
6830 return fold_if_not_in_template (build_nop (type
, expr
));
6831 else if ((TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
6832 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
6833 return fold_if_not_in_template (build_nop (type
, expr
));
6834 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
6835 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
6840 && check_for_casting_away_constness (intype
, type
,
6841 REINTERPRET_CAST_EXPR
,
6843 return error_mark_node
;
6844 /* Warn about possible alignment problems. */
6845 if (STRICT_ALIGNMENT
&& warn_cast_align
6846 && (complain
& tf_warning
)
6847 && !VOID_TYPE_P (type
)
6848 && TREE_CODE (TREE_TYPE (intype
)) != FUNCTION_TYPE
6849 && COMPLETE_TYPE_P (TREE_TYPE (type
))
6850 && COMPLETE_TYPE_P (TREE_TYPE (intype
))
6851 && TYPE_ALIGN (TREE_TYPE (type
)) > TYPE_ALIGN (TREE_TYPE (intype
)))
6852 warning (OPT_Wcast_align
, "cast from %qT to %qT "
6853 "increases required alignment of target type", intype
, type
);
6855 /* We need to strip nops here, because the front end likes to
6856 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6858 if (warn_strict_aliasing
<= 2)
6859 strict_aliasing_warning (intype
, type
, sexpr
);
6861 return fold_if_not_in_template (build_nop (type
, expr
));
6863 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
6864 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
6866 if (complain
& tf_warning
)
6867 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6868 object pointer type or vice versa is conditionally-supported." */
6869 warning (OPT_Wconditionally_supported
,
6870 "casting between pointer-to-function and pointer-to-object "
6871 "is conditionally-supported");
6872 return fold_if_not_in_template (build_nop (type
, expr
));
6874 else if (TREE_CODE (type
) == VECTOR_TYPE
)
6875 return fold_if_not_in_template (convert_to_vector (type
, expr
));
6876 else if (TREE_CODE (intype
) == VECTOR_TYPE
6877 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6878 return fold_if_not_in_template (convert_to_integer (type
, expr
));
6883 if (complain
& tf_error
)
6884 error ("invalid cast from type %qT to type %qT", intype
, type
);
6885 return error_mark_node
;
6888 return cp_convert (type
, expr
, complain
);
6892 build_reinterpret_cast (tree type
, tree expr
, tsubst_flags_t complain
)
6896 if (type
== error_mark_node
|| expr
== error_mark_node
)
6897 return error_mark_node
;
6899 if (processing_template_decl
)
6901 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
6903 if (!TREE_SIDE_EFFECTS (t
)
6904 && type_dependent_expression_p (expr
))
6905 /* There might turn out to be side effects inside expr. */
6906 TREE_SIDE_EFFECTS (t
) = 1;
6907 return convert_from_reference (t
);
6910 r
= build_reinterpret_cast_1 (type
, expr
, /*c_cast_p=*/false,
6911 /*valid_p=*/NULL
, complain
);
6912 if (r
!= error_mark_node
)
6913 maybe_warn_about_useless_cast (type
, expr
, complain
);
6917 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6918 return an appropriate expression. Otherwise, return
6919 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6920 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6921 performing a C-style cast, its value upon return will indicate
6922 whether or not the conversion succeeded. */
6925 build_const_cast_1 (tree dst_type
, tree expr
, tsubst_flags_t complain
,
6929 tree reference_type
;
6931 /* Callers are responsible for handling error_mark_node as a
6932 destination type. */
6933 gcc_assert (dst_type
!= error_mark_node
);
6934 /* In a template, callers should be building syntactic
6935 representations of casts, not using this machinery. */
6936 gcc_assert (!processing_template_decl
);
6938 /* Assume the conversion is invalid. */
6942 if (!POINTER_TYPE_P (dst_type
) && !TYPE_PTRDATAMEM_P (dst_type
))
6944 if (complain
& tf_error
)
6945 error ("invalid use of const_cast with type %qT, "
6946 "which is not a pointer, "
6947 "reference, nor a pointer-to-data-member type", dst_type
);
6948 return error_mark_node
;
6951 if (TREE_CODE (TREE_TYPE (dst_type
)) == FUNCTION_TYPE
)
6953 if (complain
& tf_error
)
6954 error ("invalid use of const_cast with type %qT, which is a pointer "
6955 "or reference to a function type", dst_type
);
6956 return error_mark_node
;
6959 /* Save casted types in the function's used types hash table. */
6960 used_types_insert (dst_type
);
6962 src_type
= TREE_TYPE (expr
);
6963 /* Expressions do not really have reference types. */
6964 if (TREE_CODE (src_type
) == REFERENCE_TYPE
)
6965 src_type
= TREE_TYPE (src_type
);
6967 /* [expr.const.cast]
6969 For two object types T1 and T2, if a pointer to T1 can be explicitly
6970 converted to the type "pointer to T2" using a const_cast, then the
6971 following conversions can also be made:
6973 -- an lvalue of type T1 can be explicitly converted to an lvalue of
6974 type T2 using the cast const_cast<T2&>;
6976 -- a glvalue of type T1 can be explicitly converted to an xvalue of
6977 type T2 using the cast const_cast<T2&&>; and
6979 -- if T1 is a class type, a prvalue of type T1 can be explicitly
6980 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
6982 if (TREE_CODE (dst_type
) == REFERENCE_TYPE
)
6984 reference_type
= dst_type
;
6985 if (!TYPE_REF_IS_RVALUE (dst_type
)
6986 ? real_lvalue_p (expr
)
6987 : (CLASS_TYPE_P (TREE_TYPE (dst_type
))
6989 : lvalue_or_rvalue_with_address_p (expr
)))
6993 if (complain
& tf_error
)
6994 error ("invalid const_cast of an rvalue of type %qT to type %qT",
6995 src_type
, dst_type
);
6996 return error_mark_node
;
6998 dst_type
= build_pointer_type (TREE_TYPE (dst_type
));
6999 src_type
= build_pointer_type (src_type
);
7003 reference_type
= NULL_TREE
;
7004 /* If the destination type is not a reference type, the
7005 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7006 conversions are performed. */
7007 src_type
= type_decays_to (src_type
);
7008 if (src_type
== error_mark_node
)
7009 return error_mark_node
;
7012 if (TYPE_PTR_P (src_type
) || TYPE_PTRDATAMEM_P (src_type
))
7014 if (comp_ptr_ttypes_const (dst_type
, src_type
))
7019 /* This cast is actually a C-style cast. Issue a warning if
7020 the user is making a potentially unsafe cast. */
7021 check_for_casting_away_constness (src_type
, dst_type
,
7022 CAST_EXPR
, complain
);
7026 expr
= cp_build_addr_expr (expr
, complain
);
7027 if (expr
== error_mark_node
)
7028 return error_mark_node
;
7029 expr
= build_nop (reference_type
, expr
);
7030 return convert_from_reference (expr
);
7034 expr
= decay_conversion (expr
, complain
);
7035 if (expr
== error_mark_node
)
7036 return error_mark_node
;
7038 /* build_c_cast puts on a NOP_EXPR to make the result not an
7039 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7040 non-lvalue context. */
7041 if (TREE_CODE (expr
) == NOP_EXPR
7042 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
7043 expr
= TREE_OPERAND (expr
, 0);
7044 return build_nop (dst_type
, expr
);
7048 && !at_least_as_qualified_p (TREE_TYPE (dst_type
),
7049 TREE_TYPE (src_type
)))
7050 check_for_casting_away_constness (src_type
, dst_type
, CAST_EXPR
,
7054 if (complain
& tf_error
)
7055 error ("invalid const_cast from type %qT to type %qT",
7056 src_type
, dst_type
);
7057 return error_mark_node
;
7061 build_const_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7065 if (type
== error_mark_node
|| error_operand_p (expr
))
7066 return error_mark_node
;
7068 if (processing_template_decl
)
7070 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
7072 if (!TREE_SIDE_EFFECTS (t
)
7073 && type_dependent_expression_p (expr
))
7074 /* There might turn out to be side effects inside expr. */
7075 TREE_SIDE_EFFECTS (t
) = 1;
7076 return convert_from_reference (t
);
7079 r
= build_const_cast_1 (type
, expr
, complain
, /*valid_p=*/NULL
);
7080 if (r
!= error_mark_node
)
7081 maybe_warn_about_useless_cast (type
, expr
, complain
);
7085 /* Like cp_build_c_cast, but for the c-common bits. */
7088 build_c_cast (location_t
/*loc*/, tree type
, tree expr
)
7090 return cp_build_c_cast (type
, expr
, tf_warning_or_error
);
7093 /* Build an expression representing an explicit C-style cast to type
7094 TYPE of expression EXPR. */
7097 cp_build_c_cast (tree type
, tree expr
, tsubst_flags_t complain
)
7103 if (type
== error_mark_node
|| error_operand_p (expr
))
7104 return error_mark_node
;
7106 if (processing_template_decl
)
7108 tree t
= build_min (CAST_EXPR
, type
,
7109 tree_cons (NULL_TREE
, value
, NULL_TREE
));
7110 /* We don't know if it will or will not have side effects. */
7111 TREE_SIDE_EFFECTS (t
) = 1;
7112 return convert_from_reference (t
);
7115 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7116 'Class') should always be retained, because this information aids
7117 in method lookup. */
7118 if (objc_is_object_ptr (type
)
7119 && objc_is_object_ptr (TREE_TYPE (expr
)))
7120 return build_nop (type
, expr
);
7122 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7123 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7124 if (TREE_CODE (type
) != REFERENCE_TYPE
7125 && TREE_CODE (value
) == NOP_EXPR
7126 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
7127 value
= TREE_OPERAND (value
, 0);
7129 if (TREE_CODE (type
) == ARRAY_TYPE
)
7131 /* Allow casting from T1* to T2[] because Cfront allows it.
7132 NIHCL uses it. It is not valid ISO C++ however. */
7133 if (TYPE_PTR_P (TREE_TYPE (expr
)))
7135 if (complain
& tf_error
)
7136 permerror (input_location
, "ISO C++ forbids casting to an array type %qT", type
);
7138 return error_mark_node
;
7139 type
= build_pointer_type (TREE_TYPE (type
));
7143 if (complain
& tf_error
)
7144 error ("ISO C++ forbids casting to an array type %qT", type
);
7145 return error_mark_node
;
7149 if (TREE_CODE (type
) == FUNCTION_TYPE
7150 || TREE_CODE (type
) == METHOD_TYPE
)
7152 if (complain
& tf_error
)
7153 error ("invalid cast to function type %qT", type
);
7154 return error_mark_node
;
7157 if (TYPE_PTR_P (type
)
7158 && TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
7159 /* Casting to an integer of smaller size is an error detected elsewhere. */
7160 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (value
))
7161 /* Don't warn about converting any constant. */
7162 && !TREE_CONSTANT (value
))
7163 warning_at (input_location
, OPT_Wint_to_pointer_cast
,
7164 "cast to pointer from integer of different size");
7166 /* A C-style cast can be a const_cast. */
7167 result
= build_const_cast_1 (type
, value
, complain
& tf_warning
,
7171 if (result
!= error_mark_node
)
7172 maybe_warn_about_useless_cast (type
, value
, complain
);
7176 /* Or a static cast. */
7177 result
= build_static_cast_1 (type
, value
, /*c_cast_p=*/true,
7178 &valid_p
, complain
);
7179 /* Or a reinterpret_cast. */
7181 result
= build_reinterpret_cast_1 (type
, value
, /*c_cast_p=*/true,
7182 &valid_p
, complain
);
7183 /* The static_cast or reinterpret_cast may be followed by a
7186 /* A valid cast may result in errors if, for example, a
7187 conversion to an ambiguous base class is required. */
7188 && !error_operand_p (result
))
7192 maybe_warn_about_useless_cast (type
, value
, complain
);
7194 /* Non-class rvalues always have cv-unqualified type. */
7195 if (!CLASS_TYPE_P (type
))
7196 type
= TYPE_MAIN_VARIANT (type
);
7197 result_type
= TREE_TYPE (result
);
7198 if (!CLASS_TYPE_P (result_type
) && TREE_CODE (type
) != REFERENCE_TYPE
)
7199 result_type
= TYPE_MAIN_VARIANT (result_type
);
7200 /* If the type of RESULT does not match TYPE, perform a
7201 const_cast to make it match. If the static_cast or
7202 reinterpret_cast succeeded, we will differ by at most
7203 cv-qualification, so the follow-on const_cast is guaranteed
7205 if (!same_type_p (non_reference (type
), non_reference (result_type
)))
7207 result
= build_const_cast_1 (type
, result
, false, &valid_p
);
7208 gcc_assert (valid_p
);
7213 return error_mark_node
;
7216 /* For use from the C common bits. */
7218 build_modify_expr (location_t
/*location*/,
7219 tree lhs
, tree
/*lhs_origtype*/,
7220 enum tree_code modifycode
,
7221 location_t
/*rhs_location*/, tree rhs
,
7222 tree
/*rhs_origtype*/)
7224 return cp_build_modify_expr (lhs
, modifycode
, rhs
, tf_warning_or_error
);
7227 /* Build an assignment expression of lvalue LHS from value RHS.
7228 MODIFYCODE is the code for a binary operator that we use
7229 to combine the old value of LHS with RHS to get the new value.
7230 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7232 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7235 cp_build_modify_expr (tree lhs
, enum tree_code modifycode
, tree rhs
,
7236 tsubst_flags_t complain
)
7240 tree lhstype
= TREE_TYPE (lhs
);
7241 tree olhstype
= lhstype
;
7242 bool plain_assign
= (modifycode
== NOP_EXPR
);
7244 /* Avoid duplicate error messages from operands that had errors. */
7245 if (error_operand_p (lhs
) || error_operand_p (rhs
))
7246 return error_mark_node
;
7248 /* Handle control structure constructs used as "lvalues". */
7249 switch (TREE_CODE (lhs
))
7251 /* Handle --foo = 5; as these are valid constructs in C++. */
7252 case PREDECREMENT_EXPR
:
7253 case PREINCREMENT_EXPR
:
7254 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
7255 lhs
= build2 (TREE_CODE (lhs
), TREE_TYPE (lhs
),
7256 stabilize_reference (TREE_OPERAND (lhs
, 0)),
7257 TREE_OPERAND (lhs
, 1));
7258 newrhs
= cp_build_modify_expr (TREE_OPERAND (lhs
, 0),
7259 modifycode
, rhs
, complain
);
7260 if (newrhs
== error_mark_node
)
7261 return error_mark_node
;
7262 return build2 (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
7264 /* Handle (a, b) used as an "lvalue". */
7266 newrhs
= cp_build_modify_expr (TREE_OPERAND (lhs
, 1),
7267 modifycode
, rhs
, complain
);
7268 if (newrhs
== error_mark_node
)
7269 return error_mark_node
;
7270 return build2 (COMPOUND_EXPR
, lhstype
,
7271 TREE_OPERAND (lhs
, 0), newrhs
);
7274 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
7275 lhs
= build2 (TREE_CODE (lhs
), TREE_TYPE (lhs
),
7276 stabilize_reference (TREE_OPERAND (lhs
, 0)),
7277 TREE_OPERAND (lhs
, 1));
7278 newrhs
= cp_build_modify_expr (TREE_OPERAND (lhs
, 0), modifycode
, rhs
,
7280 if (newrhs
== error_mark_node
)
7281 return error_mark_node
;
7282 return build2 (COMPOUND_EXPR
, lhstype
, lhs
, newrhs
);
7286 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7287 when neither operand has side-effects. */
7288 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7289 return error_mark_node
;
7291 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0))
7292 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 1)));
7294 lhs
= build3 (COND_EXPR
, TREE_TYPE (lhs
),
7295 build2 (TREE_CODE (lhs
) == MIN_EXPR
? LE_EXPR
: GE_EXPR
,
7297 TREE_OPERAND (lhs
, 0),
7298 TREE_OPERAND (lhs
, 1)),
7299 TREE_OPERAND (lhs
, 0),
7300 TREE_OPERAND (lhs
, 1));
7303 /* Handle (a ? b : c) used as an "lvalue". */
7306 /* Produce (a ? (b = rhs) : (c = rhs))
7307 except that the RHS goes through a save-expr
7308 so the code to compute it is only emitted once. */
7310 tree preeval
= NULL_TREE
;
7312 if (VOID_TYPE_P (TREE_TYPE (rhs
)))
7314 if (complain
& tf_error
)
7315 error ("void value not ignored as it ought to be");
7316 return error_mark_node
;
7319 rhs
= stabilize_expr (rhs
, &preeval
);
7321 /* Check this here to avoid odd errors when trying to convert
7322 a throw to the type of the COND_EXPR. */
7323 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7324 return error_mark_node
;
7326 cond
= build_conditional_expr
7327 (input_location
, TREE_OPERAND (lhs
, 0),
7328 cp_build_modify_expr (TREE_OPERAND (lhs
, 1),
7329 modifycode
, rhs
, complain
),
7330 cp_build_modify_expr (TREE_OPERAND (lhs
, 2),
7331 modifycode
, rhs
, complain
),
7334 if (cond
== error_mark_node
)
7336 /* Make sure the code to compute the rhs comes out
7337 before the split. */
7339 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (lhs
), preeval
, cond
);
7347 if (modifycode
== INIT_EXPR
)
7349 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
7350 /* Do the default thing. */;
7351 else if (TREE_CODE (rhs
) == CONSTRUCTOR
)
7353 /* Compound literal. */
7354 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
7355 /* Call convert to generate an error; see PR 11063. */
7356 rhs
= convert (lhstype
, rhs
);
7357 result
= build2 (INIT_EXPR
, lhstype
, lhs
, rhs
);
7358 TREE_SIDE_EFFECTS (result
) = 1;
7361 else if (! MAYBE_CLASS_TYPE_P (lhstype
))
7362 /* Do the default thing. */;
7365 vec
<tree
, va_gc
> *rhs_vec
= make_tree_vector_single (rhs
);
7366 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
7367 &rhs_vec
, lhstype
, LOOKUP_NORMAL
,
7369 release_tree_vector (rhs_vec
);
7370 if (result
== NULL_TREE
)
7371 return error_mark_node
;
7377 lhs
= require_complete_type_sfinae (lhs
, complain
);
7378 if (lhs
== error_mark_node
)
7379 return error_mark_node
;
7381 if (modifycode
== NOP_EXPR
)
7383 if (c_dialect_objc ())
7385 result
= objc_maybe_build_modify_expr (lhs
, rhs
);
7390 /* `operator=' is not an inheritable operator. */
7391 if (! MAYBE_CLASS_TYPE_P (lhstype
))
7392 /* Do the default thing. */;
7395 result
= build_new_op (input_location
, MODIFY_EXPR
,
7396 LOOKUP_NORMAL
, lhs
, rhs
,
7397 make_node (NOP_EXPR
), /*overload=*/NULL
,
7399 if (result
== NULL_TREE
)
7400 return error_mark_node
;
7407 tree init
= NULL_TREE
;
7409 /* A binary op has been requested. Combine the old LHS
7410 value with the RHS producing the value we should actually
7411 store into the LHS. */
7412 gcc_assert (!((TREE_CODE (lhstype
) == REFERENCE_TYPE
7413 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype
)))
7414 || MAYBE_CLASS_TYPE_P (lhstype
)));
7416 /* Preevaluate the RHS to make sure its evaluation is complete
7417 before the lvalue-to-rvalue conversion of the LHS:
7419 [expr.ass] With respect to an indeterminately-sequenced
7420 function call, the operation of a compound assignment is a
7421 single evaluation. [ Note: Therefore, a function call shall
7422 not intervene between the lvalue-to-rvalue conversion and the
7423 side effect associated with any single compound assignment
7424 operator. -- end note ] */
7425 lhs
= stabilize_reference (lhs
);
7427 rhs
= stabilize_expr (rhs
, &init
);
7428 newrhs
= cp_build_binary_op (input_location
,
7429 modifycode
, lhs
, rhs
,
7431 if (newrhs
== error_mark_node
)
7433 if (complain
& tf_error
)
7434 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode
,
7435 TREE_TYPE (lhs
), TREE_TYPE (rhs
));
7436 return error_mark_node
;
7440 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), init
, newrhs
);
7442 /* Now it looks like a plain assignment. */
7443 modifycode
= NOP_EXPR
;
7444 if (c_dialect_objc ())
7446 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
7451 gcc_assert (TREE_CODE (lhstype
) != REFERENCE_TYPE
);
7452 gcc_assert (TREE_CODE (TREE_TYPE (newrhs
)) != REFERENCE_TYPE
);
7455 /* The left-hand side must be an lvalue. */
7456 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
7457 return error_mark_node
;
7459 /* Warn about modifying something that is `const'. Don't warn if
7460 this is initialization. */
7461 if (modifycode
!= INIT_EXPR
7462 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
7463 /* Functions are not modifiable, even though they are
7465 || TREE_CODE (TREE_TYPE (lhs
)) == FUNCTION_TYPE
7466 || TREE_CODE (TREE_TYPE (lhs
)) == METHOD_TYPE
7467 /* If it's an aggregate and any field is const, then it is
7468 effectively const. */
7469 || (CLASS_TYPE_P (lhstype
)
7470 && C_TYPE_FIELDS_READONLY (lhstype
))))
7472 if (complain
& tf_error
)
7473 cxx_readonly_error (lhs
, lv_assign
);
7475 return error_mark_node
;
7478 /* If storing into a structure or union member, it may have been given a
7479 lowered bitfield type. We need to convert to the declared type first,
7480 so retrieve it now. */
7482 olhstype
= unlowered_expr_type (lhs
);
7484 /* Convert new value to destination type. */
7486 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
7490 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs
))
7492 if (modifycode
!= INIT_EXPR
)
7494 if (complain
& tf_error
)
7495 error ("assigning to an array from an initializer list");
7496 return error_mark_node
;
7498 if (check_array_initializer (lhs
, lhstype
, newrhs
))
7499 return error_mark_node
;
7500 newrhs
= digest_init (lhstype
, newrhs
, complain
);
7501 if (newrhs
== error_mark_node
)
7502 return error_mark_node
;
7505 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
7506 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))))
7508 if (complain
& tf_error
)
7509 error ("incompatible types in assignment of %qT to %qT",
7510 TREE_TYPE (rhs
), lhstype
);
7511 return error_mark_node
;
7514 /* Allow array assignment in compiler-generated code. */
7515 else if (!current_function_decl
7516 || !DECL_DEFAULTED_FN (current_function_decl
))
7518 /* This routine is used for both initialization and assignment.
7519 Make sure the diagnostic message differentiates the context. */
7520 if (complain
& tf_error
)
7522 if (modifycode
== INIT_EXPR
)
7523 error ("array used as initializer");
7525 error ("invalid array assignment");
7527 return error_mark_node
;
7530 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
7531 ? 1 + (modifycode
!= INIT_EXPR
): 0;
7532 return build_vec_init (lhs
, NULL_TREE
, newrhs
,
7533 /*explicit_value_init_p=*/false,
7534 from_array
, complain
);
7537 if (modifycode
== INIT_EXPR
)
7538 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7539 LOOKUP_ONLYCONVERTING. */
7540 newrhs
= convert_for_initialization (lhs
, olhstype
, newrhs
, LOOKUP_NORMAL
,
7541 ICR_INIT
, NULL_TREE
, 0,
7544 newrhs
= convert_for_assignment (olhstype
, newrhs
, ICR_ASSIGN
,
7545 NULL_TREE
, 0, complain
, LOOKUP_IMPLICIT
);
7547 if (!same_type_p (lhstype
, olhstype
))
7548 newrhs
= cp_convert_and_check (lhstype
, newrhs
, complain
);
7550 if (modifycode
!= INIT_EXPR
)
7552 if (TREE_CODE (newrhs
) == CALL_EXPR
7553 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
7554 newrhs
= build_cplus_new (lhstype
, newrhs
, complain
);
7556 /* Can't initialize directly from a TARGET_EXPR, since that would
7557 cause the lhs to be constructed twice, and possibly result in
7558 accidental self-initialization. So we force the TARGET_EXPR to be
7559 expanded without a target. */
7560 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
7561 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
7562 TREE_OPERAND (newrhs
, 0));
7565 if (newrhs
== error_mark_node
)
7566 return error_mark_node
;
7568 if (c_dialect_objc () && flag_objc_gc
)
7570 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
7576 result
= build2 (modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
7577 lhstype
, lhs
, newrhs
);
7579 TREE_SIDE_EFFECTS (result
) = 1;
7581 TREE_NO_WARNING (result
) = 1;
7587 build_x_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
7588 tree rhs
, tsubst_flags_t complain
)
7590 if (processing_template_decl
)
7591 return build_min_nt_loc (loc
, MODOP_EXPR
, lhs
,
7592 build_min_nt_loc (loc
, modifycode
, NULL_TREE
,
7595 if (modifycode
!= NOP_EXPR
)
7597 tree rval
= build_new_op (loc
, MODIFY_EXPR
, LOOKUP_NORMAL
, lhs
, rhs
,
7598 make_node (modifycode
), /*overload=*/NULL
,
7602 TREE_NO_WARNING (rval
) = 1;
7606 return cp_build_modify_expr (lhs
, modifycode
, rhs
, complain
);
7609 /* Helper function for get_delta_difference which assumes FROM is a base
7610 class of TO. Returns a delta for the conversion of pointer-to-member
7611 of FROM to pointer-to-member of TO. If the conversion is invalid and
7612 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7613 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7614 If C_CAST_P is true, this conversion is taking place as part of a
7618 get_delta_difference_1 (tree from
, tree to
, bool c_cast_p
,
7619 tsubst_flags_t complain
)
7624 binfo
= lookup_base (to
, from
, c_cast_p
? ba_unique
: ba_check
,
7627 if (binfo
== error_mark_node
)
7629 if (!(complain
& tf_error
))
7630 return error_mark_node
;
7632 error (" in pointer to member function conversion");
7633 return size_zero_node
;
7637 if (kind
!= bk_via_virtual
)
7638 return BINFO_OFFSET (binfo
);
7640 /* FROM is a virtual base class of TO. Issue an error or warning
7641 depending on whether or not this is a reinterpret cast. */
7643 if (!(complain
& tf_error
))
7644 return error_mark_node
;
7646 error ("pointer to member conversion via virtual base %qT",
7647 BINFO_TYPE (binfo_from_vbase (binfo
)));
7649 return size_zero_node
;
7656 /* Get difference in deltas for different pointer to member function
7657 types. If the conversion is invalid and tf_error is not set in
7658 COMPLAIN, returns error_mark_node, otherwise returns an integer
7659 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7660 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7661 conversions as well. If C_CAST_P is true this conversion is taking
7662 place as part of a C-style cast.
7664 Note that the naming of FROM and TO is kind of backwards; the return
7665 value is what we add to a TO in order to get a FROM. They are named
7666 this way because we call this function to find out how to convert from
7667 a pointer to member of FROM to a pointer to member of TO. */
7670 get_delta_difference (tree from
, tree to
,
7671 bool allow_inverse_p
,
7672 bool c_cast_p
, tsubst_flags_t complain
)
7676 if (same_type_ignoring_top_level_qualifiers_p (from
, to
))
7677 /* Pointer to member of incomplete class is permitted*/
7678 result
= size_zero_node
;
7680 result
= get_delta_difference_1 (from
, to
, c_cast_p
, complain
);
7682 if (result
== error_mark_node
)
7683 return error_mark_node
;
7687 if (!allow_inverse_p
)
7689 if (!(complain
& tf_error
))
7690 return error_mark_node
;
7692 error_not_base_type (from
, to
);
7693 error (" in pointer to member conversion");
7694 result
= size_zero_node
;
7698 result
= get_delta_difference_1 (to
, from
, c_cast_p
, complain
);
7700 if (result
== error_mark_node
)
7701 return error_mark_node
;
7704 result
= size_diffop_loc (input_location
,
7705 size_zero_node
, result
);
7708 if (!(complain
& tf_error
))
7709 return error_mark_node
;
7711 error_not_base_type (from
, to
);
7712 error (" in pointer to member conversion");
7713 result
= size_zero_node
;
7718 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node
,
7722 /* Return a constructor for the pointer-to-member-function TYPE using
7723 the other components as specified. */
7726 build_ptrmemfunc1 (tree type
, tree delta
, tree pfn
)
7731 vec
<constructor_elt
, va_gc
> *v
;
7733 /* Pull the FIELD_DECLs out of the type. */
7734 pfn_field
= TYPE_FIELDS (type
);
7735 delta_field
= DECL_CHAIN (pfn_field
);
7737 /* Make sure DELTA has the type we want. */
7738 delta
= convert_and_check (input_location
, delta_type_node
, delta
);
7740 /* Convert to the correct target type if necessary. */
7741 pfn
= fold_convert (TREE_TYPE (pfn_field
), pfn
);
7743 /* Finish creating the initializer. */
7745 CONSTRUCTOR_APPEND_ELT(v
, pfn_field
, pfn
);
7746 CONSTRUCTOR_APPEND_ELT(v
, delta_field
, delta
);
7747 u
= build_constructor (type
, v
);
7748 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) & TREE_CONSTANT (delta
);
7749 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
7750 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
7752 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
7757 /* Build a constructor for a pointer to member function. It can be
7758 used to initialize global variables, local variable, or used
7759 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7762 If FORCE is nonzero, then force this conversion, even if
7763 we would rather not do it. Usually set when using an explicit
7764 cast. A C-style cast is being processed iff C_CAST_P is true.
7766 Return error_mark_node, if something goes wrong. */
7769 build_ptrmemfunc (tree type
, tree pfn
, int force
, bool c_cast_p
,
7770 tsubst_flags_t complain
)
7776 if (error_operand_p (pfn
))
7777 return error_mark_node
;
7779 pfn_type
= TREE_TYPE (pfn
);
7780 to_type
= build_ptrmemfunc_type (type
);
7782 /* Handle multiple conversions of pointer to member functions. */
7783 if (TYPE_PTRMEMFUNC_P (pfn_type
))
7785 tree delta
= NULL_TREE
;
7786 tree npfn
= NULL_TREE
;
7790 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
,
7791 LOOKUP_NORMAL
, complain
))
7793 if (complain
& tf_error
)
7794 error ("invalid conversion to type %qT from type %qT",
7797 return error_mark_node
;
7800 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
7801 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
7803 c_cast_p
, complain
);
7804 if (n
== error_mark_node
)
7805 return error_mark_node
;
7807 /* We don't have to do any conversion to convert a
7808 pointer-to-member to its own type. But, we don't want to
7809 just return a PTRMEM_CST if there's an explicit cast; that
7810 cast should make the expression an invalid template argument. */
7811 if (TREE_CODE (pfn
) != PTRMEM_CST
)
7813 if (same_type_p (to_type
, pfn_type
))
7815 else if (integer_zerop (n
))
7816 return build_reinterpret_cast (to_type
, pfn
,
7820 if (TREE_SIDE_EFFECTS (pfn
))
7821 pfn
= save_expr (pfn
);
7823 /* Obtain the function pointer and the current DELTA. */
7824 if (TREE_CODE (pfn
) == PTRMEM_CST
)
7825 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
7828 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
7829 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
7832 /* Just adjust the DELTA field. */
7833 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7834 (TREE_TYPE (delta
), ptrdiff_type_node
));
7835 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
7836 n
= cp_build_binary_op (input_location
,
7837 LSHIFT_EXPR
, n
, integer_one_node
,
7839 delta
= cp_build_binary_op (input_location
,
7840 PLUS_EXPR
, delta
, n
, complain
);
7841 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
7844 /* Handle null pointer to member function conversions. */
7845 if (null_ptr_cst_p (pfn
))
7847 pfn
= cp_build_c_cast (type
, pfn
, complain
);
7848 return build_ptrmemfunc1 (to_type
,
7853 if (type_unknown_p (pfn
))
7854 return instantiate_type (type
, pfn
, complain
);
7856 fn
= TREE_OPERAND (pfn
, 0);
7857 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
7858 /* In a template, we will have preserved the
7860 || (processing_template_decl
&& TREE_CODE (fn
) == OFFSET_REF
));
7861 return make_ptrmem_cst (to_type
, fn
);
7864 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7867 ??? There is no consistency as to the types returned for the above
7868 values. Some code acts as if it were a sizetype and some as if it were
7869 integer_type_node. */
7872 expand_ptrmemfunc_cst (tree cst
, tree
*delta
, tree
*pfn
)
7874 tree type
= TREE_TYPE (cst
);
7875 tree fn
= PTRMEM_CST_MEMBER (cst
);
7876 tree ptr_class
, fn_class
;
7878 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
7880 /* The class that the function belongs to. */
7881 fn_class
= DECL_CONTEXT (fn
);
7883 /* The class that we're creating a pointer to member of. */
7884 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
7886 /* First, calculate the adjustment to the function's class. */
7887 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0,
7888 /*c_cast_p=*/0, tf_warning_or_error
);
7890 if (!DECL_VIRTUAL_P (fn
))
7891 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
),
7892 build_addr_func (fn
, tf_warning_or_error
));
7895 /* If we're dealing with a virtual function, we have to adjust 'this'
7896 again, to point to the base which provides the vtable entry for
7897 fn; the call will do the opposite adjustment. */
7898 tree orig_class
= DECL_CONTEXT (fn
);
7899 tree binfo
= binfo_or_else (orig_class
, fn_class
);
7900 *delta
= build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
7901 *delta
, BINFO_OFFSET (binfo
));
7902 *delta
= fold_if_not_in_template (*delta
);
7904 /* We set PFN to the vtable offset at which the function can be
7905 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7906 case delta is shifted left, and then incremented). */
7907 *pfn
= DECL_VINDEX (fn
);
7908 *pfn
= build2 (MULT_EXPR
, integer_type_node
, *pfn
,
7909 TYPE_SIZE_UNIT (vtable_entry_type
));
7910 *pfn
= fold_if_not_in_template (*pfn
);
7912 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
7914 case ptrmemfunc_vbit_in_pfn
:
7915 *pfn
= build2 (PLUS_EXPR
, integer_type_node
, *pfn
,
7917 *pfn
= fold_if_not_in_template (*pfn
);
7920 case ptrmemfunc_vbit_in_delta
:
7921 *delta
= build2 (LSHIFT_EXPR
, TREE_TYPE (*delta
),
7922 *delta
, integer_one_node
);
7923 *delta
= fold_if_not_in_template (*delta
);
7924 *delta
= build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
7925 *delta
, integer_one_node
);
7926 *delta
= fold_if_not_in_template (*delta
);
7933 *pfn
= build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type
), *pfn
);
7934 *pfn
= fold_if_not_in_template (*pfn
);
7938 /* Return an expression for PFN from the pointer-to-member function
7942 pfn_from_ptrmemfunc (tree t
)
7944 if (TREE_CODE (t
) == PTRMEM_CST
)
7949 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
7954 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
7957 /* Return an expression for DELTA from the pointer-to-member function
7961 delta_from_ptrmemfunc (tree t
)
7963 if (TREE_CODE (t
) == PTRMEM_CST
)
7968 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
7973 return build_ptrmemfunc_access_expr (t
, delta_identifier
);
7976 /* Convert value RHS to type TYPE as preparation for an assignment to
7977 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
7978 implicit conversion is. If FNDECL is non-NULL, we are doing the
7979 conversion in order to pass the PARMNUMth argument of FNDECL.
7980 If FNDECL is NULL, we are doing the conversion in function pointer
7981 argument passing, conversion in initialization, etc. */
7984 convert_for_assignment (tree type
, tree rhs
,
7985 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
7986 tsubst_flags_t complain
, int flags
)
7989 enum tree_code coder
;
7991 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
7992 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
)
7993 rhs
= TREE_OPERAND (rhs
, 0);
7995 rhstype
= TREE_TYPE (rhs
);
7996 coder
= TREE_CODE (rhstype
);
7998 if (TREE_CODE (type
) == VECTOR_TYPE
&& coder
== VECTOR_TYPE
7999 && vector_types_convertible_p (type
, rhstype
, true))
8001 rhs
= mark_rvalue_use (rhs
);
8002 return convert (type
, rhs
);
8005 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
8006 return error_mark_node
;
8007 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
8008 return error_mark_node
;
8010 /* The RHS of an assignment cannot have void type. */
8011 if (coder
== VOID_TYPE
)
8013 if (complain
& tf_error
)
8014 error ("void value not ignored as it ought to be");
8015 return error_mark_node
;
8018 if (c_dialect_objc ())
8022 tree rname
= fndecl
;
8033 selector
= objc_message_selector ();
8035 if (selector
&& parmno
> 1)
8042 if (objc_compare_types (type
, rhstype
, parmno
, rname
))
8044 rhs
= mark_rvalue_use (rhs
);
8045 return convert (type
, rhs
);
8051 The expression is implicitly converted (clause _conv_) to the
8052 cv-unqualified type of the left operand.
8054 We allow bad conversions here because by the time we get to this point
8055 we are committed to doing the conversion. If we end up doing a bad
8056 conversion, convert_like will complain. */
8057 if (!can_convert_arg_bad (type
, rhstype
, rhs
, flags
, complain
))
8059 /* When -Wno-pmf-conversions is use, we just silently allow
8060 conversions from pointers-to-members to plain pointers. If
8061 the conversion doesn't work, cp_convert will complain. */
8063 && TYPE_PTR_P (type
)
8064 && TYPE_PTRMEMFUNC_P (rhstype
))
8065 rhs
= cp_convert (strip_top_quals (type
), rhs
, complain
);
8068 if (complain
& tf_error
)
8070 /* If the right-hand side has unknown type, then it is an
8071 overloaded function. Call instantiate_type to get error
8073 if (rhstype
== unknown_type_node
)
8074 instantiate_type (type
, rhs
, tf_warning_or_error
);
8076 error ("cannot convert %qT to %qT for argument %qP to %qD",
8077 rhstype
, type
, parmnum
, fndecl
);
8081 case ICR_DEFAULT_ARGUMENT
:
8082 error ("cannot convert %qT to %qT in default argument",
8086 error ("cannot convert %qT to %qT in argument passing",
8089 case ICR_CONVERTING
:
8090 error ("cannot convert %qT to %qT",
8094 error ("cannot convert %qT to %qT in initialization",
8098 error ("cannot convert %qT to %qT in return",
8102 error ("cannot convert %qT to %qT in assignment",
8108 if (TYPE_PTR_P (rhstype
)
8109 && TYPE_PTR_P (type
)
8110 && CLASS_TYPE_P (TREE_TYPE (rhstype
))
8111 && CLASS_TYPE_P (TREE_TYPE (type
))
8112 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype
)))
8113 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8114 (TREE_TYPE (rhstype
))),
8115 "class type %qT is incomplete", TREE_TYPE (rhstype
));
8117 return error_mark_node
;
8120 if (warn_suggest_attribute_format
)
8122 const enum tree_code codel
= TREE_CODE (type
);
8123 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
8125 && check_missing_format_attribute (type
, rhstype
)
8126 && (complain
& tf_warning
))
8130 case ICR_DEFAULT_ARGUMENT
:
8132 warning (OPT_Wsuggest_attribute_format
,
8133 "parameter %qP of %qD might be a candidate "
8134 "for a format attribute", parmnum
, fndecl
);
8136 warning (OPT_Wsuggest_attribute_format
,
8137 "parameter might be a candidate "
8138 "for a format attribute");
8140 case ICR_CONVERTING
:
8141 warning (OPT_Wsuggest_attribute_format
,
8142 "target of conversion might be a candidate "
8143 "for a format attribute");
8146 warning (OPT_Wsuggest_attribute_format
,
8147 "target of initialization might be a candidate "
8148 "for a format attribute");
8151 warning (OPT_Wsuggest_attribute_format
,
8152 "return type might be a candidate "
8153 "for a format attribute");
8156 warning (OPT_Wsuggest_attribute_format
,
8157 "left-hand side of assignment might be a candidate "
8158 "for a format attribute");
8165 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8167 if (warn_parentheses
8168 && TREE_CODE (type
) == BOOLEAN_TYPE
8169 && TREE_CODE (rhs
) == MODIFY_EXPR
8170 && !TREE_NO_WARNING (rhs
)
8171 && TREE_CODE (TREE_TYPE (rhs
)) != BOOLEAN_TYPE
8172 && (complain
& tf_warning
))
8174 location_t loc
= EXPR_LOC_OR_LOC (rhs
, input_location
);
8176 warning_at (loc
, OPT_Wparentheses
,
8177 "suggest parentheses around assignment used as truth value");
8178 TREE_NO_WARNING (rhs
) = 1;
8181 return perform_implicit_conversion_flags (strip_top_quals (type
), rhs
,
8185 /* Convert RHS to be of type TYPE.
8186 If EXP is nonzero, it is the target of the initialization.
8187 ERRTYPE indicates what kind of error the implicit conversion is.
8189 Two major differences between the behavior of
8190 `convert_for_assignment' and `convert_for_initialization'
8191 are that references are bashed in the former, while
8192 copied in the latter, and aggregates are assigned in
8193 the former (operator=) while initialized in the
8196 If using constructor make sure no conversion operator exists, if one does
8197 exist, an ambiguity exists. */
8200 convert_for_initialization (tree exp
, tree type
, tree rhs
, int flags
,
8201 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
8202 tsubst_flags_t complain
)
8204 enum tree_code codel
= TREE_CODE (type
);
8206 enum tree_code coder
;
8208 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8209 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8210 if (TREE_CODE (rhs
) == NOP_EXPR
8211 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
8212 && codel
!= REFERENCE_TYPE
)
8213 rhs
= TREE_OPERAND (rhs
, 0);
8215 if (type
== error_mark_node
8216 || rhs
== error_mark_node
8217 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
8218 return error_mark_node
;
8220 if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
8221 && TREE_CODE (type
) != ARRAY_TYPE
8222 && (TREE_CODE (type
) != REFERENCE_TYPE
8223 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
8224 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
8225 && !TYPE_REFFN_P (type
))
8226 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
8227 rhs
= decay_conversion (rhs
, complain
);
8229 rhstype
= TREE_TYPE (rhs
);
8230 coder
= TREE_CODE (rhstype
);
8232 if (coder
== ERROR_MARK
)
8233 return error_mark_node
;
8235 /* We accept references to incomplete types, so we can
8236 return here before checking if RHS is of complete type. */
8238 if (codel
== REFERENCE_TYPE
)
8240 /* This should eventually happen in convert_arguments. */
8241 int savew
= 0, savee
= 0;
8244 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
8245 rhs
= initialize_reference (type
, rhs
, flags
, complain
);
8248 && (warningcount
+ werrorcount
> savew
|| errorcount
> savee
))
8249 inform (input_location
,
8250 "in passing argument %P of %q+D", parmnum
, fndecl
);
8256 exp
= require_complete_type_sfinae (exp
, complain
);
8257 if (exp
== error_mark_node
)
8258 return error_mark_node
;
8260 rhstype
= non_reference (rhstype
);
8262 type
= complete_type (type
);
8264 if (DIRECT_INIT_EXPR_P (type
, rhs
))
8265 /* Don't try to do copy-initialization if we already have
8266 direct-initialization. */
8269 if (MAYBE_CLASS_TYPE_P (type
))
8270 return perform_implicit_conversion_flags (type
, rhs
, complain
, flags
);
8272 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
,
8276 /* If RETVAL is the address of, or a reference to, a local variable or
8277 temporary give an appropriate warning. */
8280 maybe_warn_about_returning_address_of_local (tree retval
)
8282 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
8283 tree whats_returned
= retval
;
8287 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
8288 whats_returned
= TREE_OPERAND (whats_returned
, 1);
8289 else if (CONVERT_EXPR_P (whats_returned
)
8290 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
)
8291 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8296 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
8298 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8300 while (TREE_CODE (whats_returned
) == COMPONENT_REF
8301 || TREE_CODE (whats_returned
) == ARRAY_REF
)
8302 whats_returned
= TREE_OPERAND (whats_returned
, 0);
8304 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
8306 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
8307 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
8309 warning (OPT_Wreturn_local_addr
, "returning reference to temporary");
8312 if (VAR_P (whats_returned
)
8313 && DECL_NAME (whats_returned
)
8314 && TEMP_NAME_P (DECL_NAME (whats_returned
)))
8316 warning (OPT_Wreturn_local_addr
, "reference to non-lvalue returned");
8321 if (DECL_P (whats_returned
)
8322 && DECL_NAME (whats_returned
)
8323 && DECL_FUNCTION_SCOPE_P (whats_returned
)
8324 && !is_capture_proxy (whats_returned
)
8325 && !(TREE_STATIC (whats_returned
)
8326 || TREE_PUBLIC (whats_returned
)))
8328 if (TREE_CODE (valtype
) == REFERENCE_TYPE
)
8329 warning (OPT_Wreturn_local_addr
, "reference to local variable %q+D returned",
8331 else if (TREE_CODE (whats_returned
) == LABEL_DECL
)
8332 warning (OPT_Wreturn_local_addr
, "address of label %q+D returned",
8335 warning (OPT_Wreturn_local_addr
, "address of local variable %q+D "
8336 "returned", whats_returned
);
8341 /* Check that returning RETVAL from the current function is valid.
8342 Return an expression explicitly showing all conversions required to
8343 change RETVAL into the function return type, and to assign it to
8344 the DECL_RESULT for the function. Set *NO_WARNING to true if
8345 code reaches end of non-void function warning shouldn't be issued
8346 on this RETURN_EXPR. */
8349 check_return_expr (tree retval
, bool *no_warning
)
8352 /* The type actually returned by the function. */
8354 /* The type the function is declared to return, or void if
8355 the declared type is incomplete. */
8357 int fn_returns_value_p
;
8358 bool named_return_value_okay_p
;
8360 *no_warning
= false;
8362 if (flag_cilkplus
&& retval
&& contains_cilk_spawn_stmt (retval
))
8364 error_at (EXPR_LOCATION (retval
), "use of %<_Cilk_spawn%> in a return "
8365 "statement is not allowed");
8369 /* A `volatile' function is one that isn't supposed to return, ever.
8370 (This is a G++ extension, used to get better code for functions
8371 that call the `volatile' function.) */
8372 if (TREE_THIS_VOLATILE (current_function_decl
))
8373 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8375 /* Check for various simple errors. */
8376 if (DECL_DESTRUCTOR_P (current_function_decl
))
8379 error ("returning a value from a destructor");
8382 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
8384 if (in_function_try_handler
)
8385 /* If a return statement appears in a handler of the
8386 function-try-block of a constructor, the program is ill-formed. */
8387 error ("cannot return from a handler of a function-try-block of a constructor");
8389 /* You can't return a value from a constructor. */
8390 error ("returning a value from a constructor");
8394 if (processing_template_decl
)
8396 current_function_returns_value
= 1;
8397 if (check_for_bare_parameter_packs (retval
))
8398 retval
= error_mark_node
;
8402 functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
8404 /* Deduce auto return type from a return statement. */
8405 if (current_function_auto_return_pattern
)
8410 if (!retval
&& !is_auto (current_function_auto_return_pattern
))
8412 /* Give a helpful error message. */
8413 error ("return-statement with no value, in function returning %qT",
8414 current_function_auto_return_pattern
);
8415 inform (input_location
, "only plain %<auto%> return type can be "
8416 "deduced to %<void%>");
8417 type
= error_mark_node
;
8419 else if (retval
&& BRACE_ENCLOSED_INITIALIZER_P (retval
))
8421 error ("returning initializer list");
8422 type
= error_mark_node
;
8428 auto_node
= type_uses_auto (current_function_auto_return_pattern
);
8429 type
= do_auto_deduction (current_function_auto_return_pattern
,
8433 if (type
== error_mark_node
)
8435 else if (functype
== current_function_auto_return_pattern
)
8436 apply_deduced_return_type (current_function_decl
, type
);
8438 /* A mismatch should have been diagnosed in do_auto_deduction. */
8439 gcc_assert (same_type_p (type
, functype
));
8443 /* When no explicit return-value is given in a function with a named
8444 return value, the named return value is used. */
8445 result
= DECL_RESULT (current_function_decl
);
8446 valtype
= TREE_TYPE (result
);
8447 gcc_assert (valtype
!= NULL_TREE
);
8448 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
8449 if (!retval
&& DECL_NAME (result
) && fn_returns_value_p
)
8452 /* Check for a return statement with no return value in a function
8453 that's supposed to return a value. */
8454 if (!retval
&& fn_returns_value_p
)
8456 if (functype
!= error_mark_node
)
8457 permerror (input_location
, "return-statement with no value, in "
8458 "function returning %qT", valtype
);
8459 /* Remember that this function did return. */
8460 current_function_returns_value
= 1;
8461 /* And signal caller that TREE_NO_WARNING should be set on the
8462 RETURN_EXPR to avoid control reaches end of non-void function
8463 warnings in tree-cfg.c. */
8466 /* Check for a return statement with a value in a function that
8467 isn't supposed to return a value. */
8468 else if (retval
&& !fn_returns_value_p
)
8470 if (VOID_TYPE_P (TREE_TYPE (retval
)))
8471 /* You can return a `void' value from a function of `void'
8472 type. In that case, we have to evaluate the expression for
8473 its side-effects. */
8474 finish_expr_stmt (retval
);
8476 permerror (input_location
, "return-statement with a value, in function "
8477 "returning 'void'");
8478 current_function_returns_null
= 1;
8480 /* There's really no value to return, after all. */
8484 /* Remember that this function can sometimes return without a
8486 current_function_returns_null
= 1;
8488 /* Remember that this function did return a value. */
8489 current_function_returns_value
= 1;
8491 /* Check for erroneous operands -- but after giving ourselves a
8492 chance to provide an error about returning a value from a void
8494 if (error_operand_p (retval
))
8496 current_function_return_value
= error_mark_node
;
8497 return error_mark_node
;
8500 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8501 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == NEW_EXPR
8502 || DECL_OVERLOADED_OPERATOR_P (current_function_decl
) == VEC_NEW_EXPR
)
8503 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
8505 && retval
&& null_ptr_cst_p (retval
))
8506 warning (0, "%<operator new%> must not return NULL unless it is "
8507 "declared %<throw()%> (or -fcheck-new is in effect)");
8509 /* Effective C++ rule 15. See also start_function. */
8511 && DECL_NAME (current_function_decl
) == ansi_assopname(NOP_EXPR
))
8515 /* The function return type must be a reference to the current
8517 if (TREE_CODE (valtype
) == REFERENCE_TYPE
8518 && same_type_ignoring_top_level_qualifiers_p
8519 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
)))
8521 /* Returning '*this' is obviously OK. */
8522 if (retval
== current_class_ref
)
8524 /* If we are calling a function whose return type is the same of
8525 the current class reference, it is ok. */
8526 else if (INDIRECT_REF_P (retval
)
8527 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
8532 warning (OPT_Weffc__
, "%<operator=%> should return a reference to %<*this%>");
8535 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8537 [...] For a function with a class return type, if the expression
8538 in the return statement is the name of a local object, and the cv-
8539 unqualified type of the local object is the same as the function
8540 return type, an implementation is permitted to omit creating the tem-
8541 porary object to hold the function return value [...]
8543 So, if this is a value-returning function that always returns the same
8544 local variable, remember it.
8546 It might be nice to be more flexible, and choose the first suitable
8547 variable even if the function sometimes returns something else, but
8548 then we run the risk of clobbering the variable we chose if the other
8549 returned expression uses the chosen variable somehow. And people expect
8550 this restriction, anyway. (jason 2000-11-19)
8552 See finish_function and finalize_nrv for the rest of this optimization. */
8554 named_return_value_okay_p
=
8555 (retval
!= NULL_TREE
8556 /* Must be a local, automatic variable. */
8558 && DECL_CONTEXT (retval
) == current_function_decl
8559 && ! TREE_STATIC (retval
)
8560 /* And not a lambda or anonymous union proxy. */
8561 && !DECL_HAS_VALUE_EXPR_P (retval
)
8562 && (DECL_ALIGN (retval
) <= DECL_ALIGN (result
))
8563 /* The cv-unqualified type of the returned value must be the
8564 same as the cv-unqualified return type of the
8566 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval
))),
8567 (TYPE_MAIN_VARIANT (functype
)))
8568 /* And the returned value must be non-volatile. */
8569 && ! TYPE_VOLATILE (TREE_TYPE (retval
)));
8571 if (fn_returns_value_p
&& flag_elide_constructors
)
8573 if (named_return_value_okay_p
8574 && (current_function_return_value
== NULL_TREE
8575 || current_function_return_value
== retval
))
8576 current_function_return_value
= retval
;
8578 current_function_return_value
= error_mark_node
;
8581 /* We don't need to do any conversions when there's nothing being
8586 /* Do any required conversions. */
8587 if (retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
8588 /* No conversions are required. */
8592 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
8594 /* The functype's return type will have been set to void, if it
8595 was an incomplete type. Just treat this as 'return;' */
8596 if (VOID_TYPE_P (functype
))
8597 return error_mark_node
;
8599 /* Under C++0x [12.8/16 class.copy], a returned lvalue is sometimes
8600 treated as an rvalue for the purposes of overload resolution to
8601 favor move constructors over copy constructors.
8603 Note that these conditions are similar to, but not as strict as,
8604 the conditions for the named return value optimization. */
8605 if ((cxx_dialect
!= cxx98
)
8606 && ((VAR_P (retval
) && !DECL_HAS_VALUE_EXPR_P (retval
))
8607 || TREE_CODE (retval
) == PARM_DECL
)
8608 && DECL_CONTEXT (retval
) == current_function_decl
8609 && !TREE_STATIC (retval
)
8610 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval
))),
8611 (TYPE_MAIN_VARIANT (functype
)))
8612 /* This is only interesting for class type. */
8613 && CLASS_TYPE_P (functype
))
8614 flags
= flags
| LOOKUP_PREFER_RVALUE
;
8616 /* First convert the value to the function's return type, then
8617 to the type of return value's location to handle the
8618 case that functype is smaller than the valtype. */
8619 retval
= convert_for_initialization
8620 (NULL_TREE
, functype
, retval
, flags
, ICR_RETURN
, NULL_TREE
, 0,
8621 tf_warning_or_error
);
8622 retval
= convert (valtype
, retval
);
8624 /* If the conversion failed, treat this just like `return;'. */
8625 if (retval
== error_mark_node
)
8627 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8628 else if (! cfun
->returns_struct
8629 && TREE_CODE (retval
) == TARGET_EXPR
8630 && TREE_CODE (TREE_OPERAND (retval
, 1)) == AGGR_INIT_EXPR
)
8631 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
8632 TREE_OPERAND (retval
, 0));
8634 maybe_warn_about_returning_address_of_local (retval
);
8637 /* Actually copy the value returned into the appropriate location. */
8638 if (retval
&& retval
!= result
)
8639 retval
= build2 (INIT_EXPR
, TREE_TYPE (result
), result
, retval
);
8645 /* Returns nonzero if the pointer-type FROM can be converted to the
8646 pointer-type TO via a qualification conversion. If CONSTP is -1,
8647 then we return nonzero if the pointers are similar, and the
8648 cv-qualification signature of FROM is a proper subset of that of TO.
8650 If CONSTP is positive, then all outer pointers have been
8654 comp_ptr_ttypes_real (tree to
, tree from
, int constp
)
8656 bool to_more_cv_qualified
= false;
8657 bool is_opaque_pointer
= false;
8659 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
8661 if (TREE_CODE (to
) != TREE_CODE (from
))
8664 if (TREE_CODE (from
) == OFFSET_TYPE
8665 && !same_type_p (TYPE_OFFSET_BASETYPE (from
),
8666 TYPE_OFFSET_BASETYPE (to
)))
8669 /* Const and volatile mean something different for function types,
8670 so the usual checks are not appropriate. */
8671 if (TREE_CODE (to
) != FUNCTION_TYPE
&& TREE_CODE (to
) != METHOD_TYPE
)
8673 if (!at_least_as_qualified_p (to
, from
))
8676 if (!at_least_as_qualified_p (from
, to
))
8680 to_more_cv_qualified
= true;
8684 constp
&= TYPE_READONLY (to
);
8687 if (TREE_CODE (to
) == VECTOR_TYPE
)
8688 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
8690 if (!TYPE_PTR_P (to
) && !TYPE_PTRDATAMEM_P (to
))
8691 return ((constp
>= 0 || to_more_cv_qualified
)
8692 && (is_opaque_pointer
8693 || same_type_ignoring_top_level_qualifiers_p (to
, from
)));
8697 /* When comparing, say, char ** to char const **, this function takes
8698 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8699 types to this function. */
8702 comp_ptr_ttypes (tree to
, tree from
)
8704 return comp_ptr_ttypes_real (to
, from
, 1);
8707 /* Returns true iff FNTYPE is a non-class type that involves
8708 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8709 if a parameter type is ill-formed. */
8712 error_type_p (const_tree type
)
8716 switch (TREE_CODE (type
))
8722 case REFERENCE_TYPE
:
8724 return error_type_p (TREE_TYPE (type
));
8728 if (error_type_p (TREE_TYPE (type
)))
8730 for (t
= TYPE_ARG_TYPES (type
); t
; t
= TREE_CHAIN (t
))
8731 if (error_type_p (TREE_VALUE (t
)))
8736 if (TYPE_PTRMEMFUNC_P (type
))
8737 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type
));
8745 /* Returns true if to and from are (possibly multi-level) pointers to the same
8746 type or inheritance-related types, regardless of cv-quals. */
8749 ptr_reasonably_similar (const_tree to
, const_tree from
)
8751 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
8753 /* Any target type is similar enough to void. */
8754 if (VOID_TYPE_P (to
))
8755 return !error_type_p (from
);
8756 if (VOID_TYPE_P (from
))
8757 return !error_type_p (to
);
8759 if (TREE_CODE (to
) != TREE_CODE (from
))
8762 if (TREE_CODE (from
) == OFFSET_TYPE
8763 && comptypes (TYPE_OFFSET_BASETYPE (to
),
8764 TYPE_OFFSET_BASETYPE (from
),
8765 COMPARE_BASE
| COMPARE_DERIVED
))
8768 if (TREE_CODE (to
) == VECTOR_TYPE
8769 && vector_types_convertible_p (to
, from
, false))
8772 if (TREE_CODE (to
) == INTEGER_TYPE
8773 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
8776 if (TREE_CODE (to
) == FUNCTION_TYPE
)
8777 return !error_type_p (to
) && !error_type_p (from
);
8779 if (!TYPE_PTR_P (to
))
8781 /* When either type is incomplete avoid DERIVED_FROM_P,
8782 which may call complete_type (c++/57942). */
8783 bool b
= !COMPLETE_TYPE_P (to
) || !COMPLETE_TYPE_P (from
);
8785 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
8786 b
? COMPARE_STRICT
: COMPARE_BASE
| COMPARE_DERIVED
);
8791 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8792 pointer-to-member types) are the same, ignoring cv-qualification at
8796 comp_ptr_ttypes_const (tree to
, tree from
)
8798 bool is_opaque_pointer
= false;
8800 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
8802 if (TREE_CODE (to
) != TREE_CODE (from
))
8805 if (TREE_CODE (from
) == OFFSET_TYPE
8806 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
8807 TYPE_OFFSET_BASETYPE (to
)))
8810 if (TREE_CODE (to
) == VECTOR_TYPE
)
8811 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
8813 if (!TYPE_PTR_P (to
))
8814 return (is_opaque_pointer
8815 || same_type_ignoring_top_level_qualifiers_p (to
, from
));
8819 /* Returns the type qualifiers for this type, including the qualifiers on the
8820 elements for an array type. */
8823 cp_type_quals (const_tree type
)
8826 /* This CONST_CAST is okay because strip_array_types returns its
8827 argument unmodified and we assign it to a const_tree. */
8828 type
= strip_array_types (CONST_CAST_TREE (type
));
8829 if (type
== error_mark_node
8830 /* Quals on a FUNCTION_TYPE are memfn quals. */
8831 || TREE_CODE (type
) == FUNCTION_TYPE
)
8832 return TYPE_UNQUALIFIED
;
8833 quals
= TYPE_QUALS (type
);
8834 /* METHOD and REFERENCE_TYPEs should never have quals. */
8835 gcc_assert ((TREE_CODE (type
) != METHOD_TYPE
8836 && TREE_CODE (type
) != REFERENCE_TYPE
)
8837 || ((quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
))
8838 == TYPE_UNQUALIFIED
));
8842 /* Returns the function-ref-qualifier for TYPE */
8845 type_memfn_rqual (const_tree type
)
8847 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
8848 || TREE_CODE (type
) == METHOD_TYPE
);
8850 if (!FUNCTION_REF_QUALIFIED (type
))
8851 return REF_QUAL_NONE
;
8852 else if (FUNCTION_RVALUE_QUALIFIED (type
))
8853 return REF_QUAL_RVALUE
;
8855 return REF_QUAL_LVALUE
;
8858 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8862 type_memfn_quals (const_tree type
)
8864 if (TREE_CODE (type
) == FUNCTION_TYPE
)
8865 return TYPE_QUALS (type
);
8866 else if (TREE_CODE (type
) == METHOD_TYPE
)
8867 return cp_type_quals (class_of_this_parm (type
));
8872 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8873 MEMFN_QUALS and its ref-qualifier to RQUAL. */
8876 apply_memfn_quals (tree type
, cp_cv_quals memfn_quals
, cp_ref_qualifier rqual
)
8878 /* Could handle METHOD_TYPE here if necessary. */
8879 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
8880 if (TYPE_QUALS (type
) == memfn_quals
8881 && type_memfn_rqual (type
) == rqual
)
8884 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8886 tree result
= build_qualified_type (type
, memfn_quals
);
8887 result
= build_exception_variant (result
, TYPE_RAISES_EXCEPTIONS (type
));
8888 return build_ref_qualified_type (result
, rqual
);
8891 /* Returns nonzero if TYPE is const or volatile. */
8894 cv_qualified_p (const_tree type
)
8896 int quals
= cp_type_quals (type
);
8897 return (quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)) != 0;
8900 /* Returns nonzero if the TYPE contains a mutable member. */
8903 cp_has_mutable_p (const_tree type
)
8905 /* This CONST_CAST is okay because strip_array_types returns its
8906 argument unmodified and we assign it to a const_tree. */
8907 type
= strip_array_types (CONST_CAST_TREE(type
));
8909 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
8912 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
8913 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
8914 approximation. In particular, consider:
8917 struct S { int i; };
8918 const S s = { f(); }
8920 Here, we will make "s" as TREE_READONLY (because it is declared
8921 "const") -- only to reverse ourselves upon seeing that the
8922 initializer is non-constant. */
8925 cp_apply_type_quals_to_decl (int type_quals
, tree decl
)
8927 tree type
= TREE_TYPE (decl
);
8929 if (type
== error_mark_node
)
8932 if (TREE_CODE (decl
) == TYPE_DECL
)
8935 gcc_assert (!(TREE_CODE (type
) == FUNCTION_TYPE
8936 && type_quals
!= TYPE_UNQUALIFIED
));
8938 /* Avoid setting TREE_READONLY incorrectly. */
8939 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
8940 constructor can produce constant init, so rely on cp_finish_decl to
8941 clear TREE_READONLY if the variable has non-constant init. */
8943 /* If the type has (or might have) a mutable component, that component
8944 might be modified. */
8945 if (TYPE_HAS_MUTABLE_P (type
) || !COMPLETE_TYPE_P (type
))
8946 type_quals
&= ~TYPE_QUAL_CONST
;
8948 c_apply_type_quals_to_decl (type_quals
, decl
);
8951 /* Subroutine of casts_away_constness. Make T1 and T2 point at
8952 exemplar types such that casting T1 to T2 is casting away constness
8953 if and only if there is no implicit conversion from T1 to T2. */
8956 casts_away_constness_r (tree
*t1
, tree
*t2
, tsubst_flags_t complain
)
8961 /* [expr.const.cast]
8963 For multi-level pointer to members and multi-level mixed pointers
8964 and pointers to members (conv.qual), the "member" aspect of a
8965 pointer to member level is ignored when determining if a const
8966 cv-qualifier has been cast away. */
8967 /* [expr.const.cast]
8969 For two pointer types:
8971 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
8972 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
8975 casting from X1 to X2 casts away constness if, for a non-pointer
8976 type T there does not exist an implicit conversion (clause
8979 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
8983 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
8984 if ((!TYPE_PTR_P (*t1
) && !TYPE_PTRDATAMEM_P (*t1
))
8985 || (!TYPE_PTR_P (*t2
) && !TYPE_PTRDATAMEM_P (*t2
)))
8987 *t1
= cp_build_qualified_type (void_type_node
,
8988 cp_type_quals (*t1
));
8989 *t2
= cp_build_qualified_type (void_type_node
,
8990 cp_type_quals (*t2
));
8994 quals1
= cp_type_quals (*t1
);
8995 quals2
= cp_type_quals (*t2
);
8997 if (TYPE_PTRDATAMEM_P (*t1
))
8998 *t1
= TYPE_PTRMEM_POINTED_TO_TYPE (*t1
);
9000 *t1
= TREE_TYPE (*t1
);
9001 if (TYPE_PTRDATAMEM_P (*t2
))
9002 *t2
= TYPE_PTRMEM_POINTED_TO_TYPE (*t2
);
9004 *t2
= TREE_TYPE (*t2
);
9006 casts_away_constness_r (t1
, t2
, complain
);
9007 *t1
= build_pointer_type (*t1
);
9008 *t2
= build_pointer_type (*t2
);
9009 *t1
= cp_build_qualified_type (*t1
, quals1
);
9010 *t2
= cp_build_qualified_type (*t2
, quals2
);
9013 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9016 ??? This function returns non-zero if casting away qualifiers not
9017 just const. We would like to return to the caller exactly which
9018 qualifiers are casted away to give more accurate diagnostics.
9022 casts_away_constness (tree t1
, tree t2
, tsubst_flags_t complain
)
9024 if (TREE_CODE (t2
) == REFERENCE_TYPE
)
9026 /* [expr.const.cast]
9028 Casting from an lvalue of type T1 to an lvalue of type T2
9029 using a reference cast casts away constness if a cast from an
9030 rvalue of type "pointer to T1" to the type "pointer to T2"
9031 casts away constness. */
9032 t1
= (TREE_CODE (t1
) == REFERENCE_TYPE
? TREE_TYPE (t1
) : t1
);
9033 return casts_away_constness (build_pointer_type (t1
),
9034 build_pointer_type (TREE_TYPE (t2
)),
9038 if (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
9039 /* [expr.const.cast]
9041 Casting from an rvalue of type "pointer to data member of X
9042 of type T1" to the type "pointer to data member of Y of type
9043 T2" casts away constness if a cast from an rvalue of type
9044 "pointer to T1" to the type "pointer to T2" casts away
9046 return casts_away_constness
9047 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1
)),
9048 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2
)),
9051 /* Casting away constness is only something that makes sense for
9052 pointer or reference types. */
9053 if (!TYPE_PTR_P (t1
) || !TYPE_PTR_P (t2
))
9056 /* Top-level qualifiers don't matter. */
9057 t1
= TYPE_MAIN_VARIANT (t1
);
9058 t2
= TYPE_MAIN_VARIANT (t2
);
9059 casts_away_constness_r (&t1
, &t2
, complain
);
9060 if (!can_convert (t2
, t1
, complain
))
9066 /* If T is a REFERENCE_TYPE return the type to which T refers.
9067 Otherwise, return T itself. */
9070 non_reference (tree t
)
9072 if (t
&& TREE_CODE (t
) == REFERENCE_TYPE
)
9078 /* Return nonzero if REF is an lvalue valid for this language;
9079 otherwise, print an error message and return zero. USE says
9080 how the lvalue is being used and so selects the error message. */
9083 lvalue_or_else (tree ref
, enum lvalue_use use
, tsubst_flags_t complain
)
9085 cp_lvalue_kind kind
= lvalue_kind (ref
);
9087 if (kind
== clk_none
)
9089 if (complain
& tf_error
)
9090 lvalue_error (input_location
, use
);
9093 else if (kind
& (clk_rvalueref
|clk_class
))
9095 if (!(complain
& tf_error
))
9097 if (kind
& clk_class
)
9098 /* Make this a permerror because we used to accept it. */
9099 permerror (input_location
, "using temporary as lvalue");
9101 error ("using xvalue (rvalue reference) as lvalue");
9106 /* Return true if a user-defined literal operator is a raw operator. */
9109 check_raw_literal_operator (const_tree decl
)
9111 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9114 bool maybe_raw_p
= false;
9116 /* Count the number and type of arguments and check for ellipsis. */
9117 for (argtype
= argtypes
, arity
= 0;
9118 argtype
&& argtype
!= void_list_node
;
9119 ++arity
, argtype
= TREE_CHAIN (argtype
))
9121 tree t
= TREE_VALUE (argtype
);
9123 if (same_type_p (t
, const_string_type_node
))
9127 return false; /* Found ellipsis. */
9129 if (!maybe_raw_p
|| arity
!= 1)
9136 /* Return true if a user-defined literal operator has one of the allowed
9140 check_literal_operator_args (const_tree decl
,
9141 bool *long_long_unsigned_p
, bool *long_double_p
)
9143 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
9145 *long_long_unsigned_p
= false;
9146 *long_double_p
= false;
9147 if (processing_template_decl
|| processing_specialization
)
9148 return argtypes
== void_list_node
;
9155 /* Count the number and type of arguments and check for ellipsis. */
9156 for (argtype
= argtypes
, arity
= 0;
9157 argtype
&& argtype
!= void_list_node
;
9158 argtype
= TREE_CHAIN (argtype
))
9160 tree t
= TREE_VALUE (argtype
);
9165 bool maybe_raw_p
= false;
9167 if (cp_type_quals (t
) != TYPE_QUAL_CONST
)
9169 t
= TYPE_MAIN_VARIANT (t
);
9170 if ((maybe_raw_p
= same_type_p (t
, char_type_node
))
9171 || same_type_p (t
, wchar_type_node
)
9172 || same_type_p (t
, char16_type_node
)
9173 || same_type_p (t
, char32_type_node
))
9175 argtype
= TREE_CHAIN (argtype
);
9178 t
= TREE_VALUE (argtype
);
9179 if (maybe_raw_p
&& argtype
== void_list_node
)
9181 else if (same_type_p (t
, size_type_node
))
9190 else if (same_type_p (t
, long_long_unsigned_type_node
))
9193 *long_long_unsigned_p
= true;
9195 else if (same_type_p (t
, long_double_type_node
))
9198 *long_double_p
= true;
9200 else if (same_type_p (t
, char_type_node
))
9202 else if (same_type_p (t
, wchar_type_node
))
9204 else if (same_type_p (t
, char16_type_node
))
9206 else if (same_type_p (t
, char32_type_node
))
9212 return false; /* Found ellipsis. */
9214 if (arity
!= max_arity
)